blob: c7a3fdbaa67d71ff6b6fbd6a72937758af00a094 [file] [log] [blame]
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * efi_selftest_variables
4 *
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
Heinrich Schuchardt702e7782018-09-27 20:44:40 +02007 * This unit test checks the runtime services for variables:
8 * GetVariable, GetNextVariableName, SetVariable, QueryVariableInfo.
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +02009 */
10
11#include <efi_selftest.h>
12
13#define EFI_ST_MAX_DATA_SIZE 16
Heinrich Schuchardtc1691872020-03-20 19:20:17 +010014#define EFI_ST_MAX_VARNAME_SIZE 80
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020015
16static struct efi_boot_services *boottime;
17static struct efi_runtime_services *runtime;
Heinrich Schuchardte06ae192018-12-30 20:53:51 +010018static const efi_guid_t guid_vendor0 =
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020019 EFI_GUID(0x67029eb5, 0x0af2, 0xf6b1,
20 0xda, 0x53, 0xfc, 0xb5, 0x66, 0xdd, 0x1c, 0xe6);
Heinrich Schuchardte06ae192018-12-30 20:53:51 +010021static const efi_guid_t guid_vendor1 =
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020022 EFI_GUID(0xff629290, 0x1fc1, 0xd73f,
23 0x8f, 0xb1, 0x32, 0xf9, 0x0c, 0xa0, 0x42, 0xea);
24
25/*
26 * Setup unit test.
27 *
28 * @handle handle of the loaded image
29 * @systable system table
30 */
31static int setup(const efi_handle_t img_handle,
32 const struct efi_system_table *systable)
33{
34 boottime = systable->boottime;
35 runtime = systable->runtime;
36
37 return EFI_ST_SUCCESS;
38}
39
40/*
41 * Execute unit test.
42 */
43static int execute(void)
44{
45 efi_status_t ret;
46 efi_uintn_t len;
47 u32 attr;
48 u8 v[16] = {0x5d, 0xd1, 0x5e, 0x51, 0x5a, 0x05, 0xc7, 0x0c,
49 0x35, 0x4a, 0xae, 0x87, 0xa5, 0xdf, 0x0f, 0x65,};
Ivan Gorinovda168a42018-05-25 10:45:09 -070050 u8 data[EFI_ST_MAX_DATA_SIZE];
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020051 u16 varname[EFI_ST_MAX_VARNAME_SIZE];
52 int flag;
53 efi_guid_t guid;
54 u64 max_storage, rem_storage, max_size;
55
56 ret = runtime->query_variable_info(EFI_VARIABLE_BOOTSERVICE_ACCESS,
57 &max_storage, &rem_storage,
58 &max_size);
59 if (ret != EFI_SUCCESS) {
60 efi_st_todo("QueryVariableInfo failed\n");
61 } else if (!max_storage || !rem_storage || !max_size) {
62 efi_st_error("QueryVariableInfo: wrong info\n");
63 return EFI_ST_FAILURE;
64 }
65 /* Set variable 0 */
Simon Glass90975372022-01-23 12:55:12 -070066 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020067 EFI_VARIABLE_BOOTSERVICE_ACCESS,
68 3, v + 4);
69 if (ret != EFI_SUCCESS) {
70 efi_st_error("SetVariable failed\n");
71 return EFI_ST_FAILURE;
72 }
Ivan Gorinovda168a42018-05-25 10:45:09 -070073 data[3] = 0xff;
74 len = 3;
Simon Glass90975372022-01-23 12:55:12 -070075 ret = runtime->get_variable(u"efi_st_var0", &guid_vendor0,
Ivan Gorinovda168a42018-05-25 10:45:09 -070076 &attr, &len, data);
77 if (ret != EFI_SUCCESS) {
78 efi_st_error("GetVariable failed\n");
79 return EFI_ST_FAILURE;
80 }
Heinrich Schuchardt5f946662019-05-04 19:48:38 +020081 if (memcmp(data, v + 4, 3)) {
Ivan Gorinovda168a42018-05-25 10:45:09 -070082 efi_st_error("GetVariable returned wrong value\n");
83 return EFI_ST_FAILURE;
84 }
85 if (data[3] != 0xff) {
86 efi_st_error("GetVariable wrote past the end of the buffer\n");
87 return EFI_ST_FAILURE;
88 }
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020089 /* Set variable 1 */
Simon Glass90975372022-01-23 12:55:12 -070090 ret = runtime->set_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020091 EFI_VARIABLE_BOOTSERVICE_ACCESS,
92 8, v);
93 if (ret != EFI_SUCCESS) {
94 efi_st_error("SetVariable failed\n");
95 return EFI_ST_FAILURE;
96 }
97 len = EFI_ST_MAX_DATA_SIZE;
Simon Glass90975372022-01-23 12:55:12 -070098 ret = runtime->get_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020099 &attr, &len, data);
100 if (ret != EFI_SUCCESS) {
101 efi_st_error("GetVariable failed\n");
102 return EFI_ST_FAILURE;
103 }
104 if (len != 8) {
105 efi_st_error("GetVariable returned wrong length %u\n",
106 (unsigned int)len);
107 return EFI_ST_FAILURE;
108 }
Heinrich Schuchardt5f946662019-05-04 19:48:38 +0200109 if (memcmp(data, v, 8)) {
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200110 efi_st_error("GetVariable returned wrong value\n");
111 return EFI_ST_FAILURE;
112 }
113 /* Append variable 1 */
Simon Glass90975372022-01-23 12:55:12 -0700114 ret = runtime->set_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200115 EFI_VARIABLE_BOOTSERVICE_ACCESS |
116 EFI_VARIABLE_APPEND_WRITE,
117 7, v + 8);
118 if (ret != EFI_SUCCESS) {
AKASHI Takahirob4248cf2019-09-06 15:09:53 +0900119 efi_st_error("SetVariable(APPEND_WRITE) failed\n");
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200120 return EFI_ST_FAILURE;
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200121 }
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200122 len = EFI_ST_MAX_DATA_SIZE;
Simon Glass90975372022-01-23 12:55:12 -0700123 ret = runtime->get_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200124 &attr, &len, data);
125 if (ret != EFI_SUCCESS) {
126 efi_st_error("GetVariable failed\n");
127 return EFI_ST_FAILURE;
128 }
129 if (len != 15)
130 efi_st_todo("GetVariable returned wrong length %u\n",
131 (unsigned int)len);
132 if (memcmp(data, v, len))
133 efi_st_todo("GetVariable returned wrong value\n");
AKASHI Takahirob4248cf2019-09-06 15:09:53 +0900134 /* Append variable 2 */
Simon Glass90975372022-01-23 12:55:12 -0700135 ret = runtime->set_variable(u"efi_none", &guid_vendor1,
AKASHI Takahirob4248cf2019-09-06 15:09:53 +0900136 EFI_VARIABLE_BOOTSERVICE_ACCESS |
137 EFI_VARIABLE_APPEND_WRITE,
138 15, v);
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200139 if (ret != EFI_NOT_FOUND) {
AKASHI Takahirob4248cf2019-09-06 15:09:53 +0900140 efi_st_error("SetVariable(APPEND_WRITE) with size 0 to non-existent variable returns wrong code\n");
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200141 return EFI_ST_FAILURE;
142 }
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200143 /* Enumerate variables */
Heinrich Schuchardtd6bed222022-12-18 06:08:58 +0000144
145 ret = runtime->get_next_variable_name(NULL, u"efi_st_var1", &guid);
146 if (ret != EFI_INVALID_PARAMETER) {
147 efi_st_error("GetNextVariableName missing parameter check\n");
148 return EFI_ST_FAILURE;
149 }
150
151 len = 24;
152 ret = runtime->get_next_variable_name(&len, NULL, &guid);
153 if (ret != EFI_INVALID_PARAMETER) {
154 efi_st_error("GetNextVariableName missing parameter check\n");
155 return EFI_ST_FAILURE;
156 }
157
158 len = 24;
159 ret = runtime->get_next_variable_name(&len, u"efi_st_var1", NULL);
160 if (ret != EFI_INVALID_PARAMETER) {
161 efi_st_error("GetNextVariableName missing parameter check\n");
162 return EFI_ST_FAILURE;
163 }
164
165 len = 1;
166 ret = runtime->get_next_variable_name(&len, u"", &guid);
167 if (ret != EFI_INVALID_PARAMETER) {
168 efi_st_error("GetNextVariableName missing parameter check\n");
169 return EFI_ST_FAILURE;
170 }
171
172 len = 16;
173 ret = runtime->get_next_variable_name(&len, u"efi_st_var1", &guid);
174 if (ret != EFI_INVALID_PARAMETER) {
175 efi_st_error("GetNextVariableName missing parameter check\n");
176 return EFI_ST_FAILURE;
177 }
178
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200179 boottime->set_mem(&guid, 16, 0);
180 *varname = 0;
181 flag = 0;
182 for (;;) {
183 len = EFI_ST_MAX_VARNAME_SIZE;
184 ret = runtime->get_next_variable_name(&len, varname, &guid);
185 if (ret == EFI_NOT_FOUND)
186 break;
187 if (ret != EFI_SUCCESS) {
AKASHI Takahirob3d562e2019-01-21 12:43:14 +0100188 efi_st_error("GetNextVariableName failed (%u)\n",
189 (unsigned int)ret);
190 return EFI_ST_FAILURE;
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200191 }
Heinrich Schuchardt5f946662019-05-04 19:48:38 +0200192 if (!memcmp(&guid, &guid_vendor0, sizeof(efi_guid_t)) &&
Heinrich Schuchardtc1691872020-03-20 19:20:17 +0100193 !efi_st_strcmp_16_8(varname, "efi_st_var0")) {
AKASHI Takahirob3d562e2019-01-21 12:43:14 +0100194 flag |= 1;
Heinrich Schuchardtc1691872020-03-20 19:20:17 +0100195 if (len != 24) {
196 efi_st_error("GetNextVariableName report wrong length %u, expected 24\n",
197 (unsigned int)len);
198 return EFI_ST_FAILURE;
199 }
200 }
Heinrich Schuchardt5f946662019-05-04 19:48:38 +0200201 if (!memcmp(&guid, &guid_vendor1, sizeof(efi_guid_t)) &&
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200202 !efi_st_strcmp_16_8(varname, "efi_st_var1"))
203 flag |= 2;
204 }
AKASHI Takahirob3d562e2019-01-21 12:43:14 +0100205 if (flag != 3) {
206 efi_st_error(
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200207 "GetNextVariableName did not return all variables\n");
AKASHI Takahirob3d562e2019-01-21 12:43:14 +0100208 return EFI_ST_FAILURE;
209 }
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200210 /* Delete variable 1 */
Simon Glass90975372022-01-23 12:55:12 -0700211 ret = runtime->set_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200212 0, 0, NULL);
213 if (ret != EFI_SUCCESS) {
214 efi_st_error("SetVariable failed\n");
215 return EFI_ST_FAILURE;
216 }
217 len = EFI_ST_MAX_DATA_SIZE;
Simon Glass90975372022-01-23 12:55:12 -0700218 ret = runtime->get_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200219 &attr, &len, data);
220 if (ret != EFI_NOT_FOUND) {
221 efi_st_error("Variable was not deleted\n");
222 return EFI_ST_FAILURE;
223 }
224 /* Delete variable 0 */
Simon Glass90975372022-01-23 12:55:12 -0700225 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200226 0, 0, NULL);
227 if (ret != EFI_SUCCESS) {
228 efi_st_error("SetVariable failed\n");
229 return EFI_ST_FAILURE;
230 }
231 len = EFI_ST_MAX_DATA_SIZE;
Simon Glass90975372022-01-23 12:55:12 -0700232 ret = runtime->get_variable(u"efi_st_var0", &guid_vendor0,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200233 &attr, &len, data);
234 if (ret != EFI_NOT_FOUND) {
235 efi_st_error("Variable was not deleted\n");
236 return EFI_ST_FAILURE;
237 }
238
239 return EFI_ST_SUCCESS;
240}
241
242EFI_UNIT_TEST(variables) = {
243 .name = "variables",
244 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
245 .setup = setup,
246 .execute = execute,
247};