1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * efi_selftest_variables_runtime
4  *
5  * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
6  *
7  * This unit test checks the runtime services for variables after
8  * ExitBootServices():
9  * GetVariable, GetNextVariableName, SetVariable, QueryVariableInfo.
10  */
11 
12 #include <efi_selftest.h>
13 
14 #define EFI_ST_MAX_DATA_SIZE 16
15 #define EFI_ST_MAX_VARNAME_SIZE 40
16 
17 static struct efi_boot_services *boottime;
18 static struct efi_runtime_services *runtime;
19 static const efi_guid_t guid_vendor0 = EFI_GLOBAL_VARIABLE_GUID;
20 
21 /*
22  * Setup unit test.
23  *
24  * @handle	handle of the loaded image
25  * @systable	system table
26  */
setup(const efi_handle_t img_handle,const struct efi_system_table * systable)27 static int setup(const efi_handle_t img_handle,
28 		 const struct efi_system_table *systable)
29 {
30 	boottime = systable->boottime;
31 	runtime = systable->runtime;
32 
33 	return EFI_ST_SUCCESS;
34 }
35 
36 /**
37  * execute() - execute unit test
38  *
39  * As runtime support is not implmented expect EFI_UNSUPPORTED to be returned.
40  */
execute(void)41 static int execute(void)
42 {
43 	efi_status_t ret;
44 	efi_uintn_t len;
45 	u32 attr;
46 	u8 v[16] = {0x5d, 0xd1, 0x5e, 0x51, 0x5a, 0x05, 0xc7, 0x0c,
47 		    0x35, 0x4a, 0xae, 0x87, 0xa5, 0xdf, 0x0f, 0x65,};
48 	u8 data[EFI_ST_MAX_DATA_SIZE];
49 	u16 varname[EFI_ST_MAX_VARNAME_SIZE];
50 	efi_guid_t guid;
51 	u64 max_storage, rem_storage, max_size;
52 
53 	ret = runtime->query_variable_info(EFI_VARIABLE_BOOTSERVICE_ACCESS,
54 					   &max_storage, &rem_storage,
55 					   &max_size);
56 	if (ret != EFI_UNSUPPORTED) {
57 		efi_st_error("QueryVariableInfo failed\n");
58 		return EFI_ST_FAILURE;
59 	}
60 
61 	ret = runtime->set_variable(L"efi_st_var0", &guid_vendor0,
62 				    EFI_VARIABLE_BOOTSERVICE_ACCESS |
63 				    EFI_VARIABLE_RUNTIME_ACCESS,
64 				    3, v + 4);
65 	if (ret != EFI_UNSUPPORTED) {
66 		efi_st_error("SetVariable failed\n");
67 		return EFI_ST_FAILURE;
68 	}
69 	len = EFI_ST_MAX_DATA_SIZE;
70 	ret = runtime->get_variable(L"PlatformLangCodes", &guid_vendor0,
71 				    &attr, &len, data);
72 	if (ret != EFI_SUCCESS) {
73 		efi_st_error("GetVariable failed\n");
74 		return EFI_ST_FAILURE;
75 	}
76 	memset(&guid, 0, 16);
77 	*varname = 0;
78 	len = 2 * EFI_ST_MAX_VARNAME_SIZE;
79 	ret = runtime->get_next_variable_name(&len, varname, &guid);
80 	if (ret != EFI_SUCCESS) {
81 		efi_st_error("GetNextVariableName failed\n");
82 		return EFI_ST_FAILURE;
83 	}
84 
85 	return EFI_ST_SUCCESS;
86 }
87 
88 EFI_UNIT_TEST(variables_run) = {
89 	.name = "variables at runtime",
90 	.phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
91 	.setup = setup,
92 	.execute = execute,
93 };
94