blob: 3d5f38c689784f79a7d99f0b8881750525c20a08 [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;
Ilias Apalodimas9a039e02024-04-25 08:18:21 +030054 int test_ret;
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020055
Ilias Apalodimas9a039e02024-04-25 08:18:21 +030056 test_ret = efi_st_query_variable_common(runtime,
57 EFI_VARIABLE_BOOTSERVICE_ACCESS);
58 if (test_ret != EFI_ST_SUCCESS) {
59 efi_st_error("QueryVariableInfo failed\n");
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020060 return EFI_ST_FAILURE;
61 }
62 /* Set variable 0 */
Simon Glass90975372022-01-23 12:55:12 -070063 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020064 EFI_VARIABLE_BOOTSERVICE_ACCESS,
65 3, v + 4);
66 if (ret != EFI_SUCCESS) {
67 efi_st_error("SetVariable failed\n");
68 return EFI_ST_FAILURE;
69 }
Ivan Gorinovda168a42018-05-25 10:45:09 -070070 data[3] = 0xff;
71 len = 3;
Simon Glass90975372022-01-23 12:55:12 -070072 ret = runtime->get_variable(u"efi_st_var0", &guid_vendor0,
Ivan Gorinovda168a42018-05-25 10:45:09 -070073 &attr, &len, data);
74 if (ret != EFI_SUCCESS) {
75 efi_st_error("GetVariable failed\n");
76 return EFI_ST_FAILURE;
77 }
Heinrich Schuchardt5f946662019-05-04 19:48:38 +020078 if (memcmp(data, v + 4, 3)) {
Ivan Gorinovda168a42018-05-25 10:45:09 -070079 efi_st_error("GetVariable returned wrong value\n");
80 return EFI_ST_FAILURE;
81 }
82 if (data[3] != 0xff) {
83 efi_st_error("GetVariable wrote past the end of the buffer\n");
84 return EFI_ST_FAILURE;
85 }
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020086 /* Set variable 1 */
Simon Glass90975372022-01-23 12:55:12 -070087 ret = runtime->set_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020088 EFI_VARIABLE_BOOTSERVICE_ACCESS,
89 8, v);
90 if (ret != EFI_SUCCESS) {
91 efi_st_error("SetVariable failed\n");
92 return EFI_ST_FAILURE;
93 }
94 len = EFI_ST_MAX_DATA_SIZE;
Simon Glass90975372022-01-23 12:55:12 -070095 ret = runtime->get_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +020096 &attr, &len, data);
97 if (ret != EFI_SUCCESS) {
98 efi_st_error("GetVariable failed\n");
99 return EFI_ST_FAILURE;
100 }
101 if (len != 8) {
102 efi_st_error("GetVariable returned wrong length %u\n",
103 (unsigned int)len);
104 return EFI_ST_FAILURE;
105 }
Heinrich Schuchardt5f946662019-05-04 19:48:38 +0200106 if (memcmp(data, v, 8)) {
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200107 efi_st_error("GetVariable returned wrong value\n");
108 return EFI_ST_FAILURE;
109 }
110 /* Append variable 1 */
Simon Glass90975372022-01-23 12:55:12 -0700111 ret = runtime->set_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200112 EFI_VARIABLE_BOOTSERVICE_ACCESS |
113 EFI_VARIABLE_APPEND_WRITE,
114 7, v + 8);
115 if (ret != EFI_SUCCESS) {
AKASHI Takahirob4248cf2019-09-06 15:09:53 +0900116 efi_st_error("SetVariable(APPEND_WRITE) failed\n");
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200117 return EFI_ST_FAILURE;
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200118 }
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200119 len = EFI_ST_MAX_DATA_SIZE;
Simon Glass90975372022-01-23 12:55:12 -0700120 ret = runtime->get_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200121 &attr, &len, data);
122 if (ret != EFI_SUCCESS) {
123 efi_st_error("GetVariable failed\n");
124 return EFI_ST_FAILURE;
125 }
126 if (len != 15)
127 efi_st_todo("GetVariable returned wrong length %u\n",
128 (unsigned int)len);
129 if (memcmp(data, v, len))
130 efi_st_todo("GetVariable returned wrong value\n");
Masahisa Kojima93a8c242024-04-02 18:09:50 +0900131
132 /* Append variable 2, write to non-existent variable with datasize=0 */
Simon Glass90975372022-01-23 12:55:12 -0700133 ret = runtime->set_variable(u"efi_none", &guid_vendor1,
AKASHI Takahirob4248cf2019-09-06 15:09:53 +0900134 EFI_VARIABLE_BOOTSERVICE_ACCESS |
135 EFI_VARIABLE_APPEND_WRITE,
Masahisa Kojima93a8c242024-04-02 18:09:50 +0900136 0, v);
137 if (ret != EFI_SUCCESS) {
138 efi_st_error(
139 "SetVariable(APPEND_WRITE) with size 0 to non-existent variable returns wrong code\n");
140 return EFI_ST_FAILURE;
141 }
142 len = EFI_ST_MAX_DATA_SIZE;
143 ret = runtime->get_variable(u"efi_none", &guid_vendor1,
144 &attr, &len, data);
145 if (ret != EFI_NOT_FOUND) {
146 efi_st_error("Variable must not be created\n");
147 return EFI_ST_FAILURE;
148 }
149 /* Append variable 2, write to non-existent variable with valid data size*/
150 ret = runtime->set_variable(u"efi_none", &guid_vendor1,
151 EFI_VARIABLE_BOOTSERVICE_ACCESS |
152 EFI_VARIABLE_APPEND_WRITE,
AKASHI Takahirob4248cf2019-09-06 15:09:53 +0900153 15, v);
Masahisa Kojima93a8c242024-04-02 18:09:50 +0900154 if (ret != EFI_SUCCESS) {
155 efi_st_error("SetVariable(APPEND_WRITE) with valid size and data to non-existent variable must be succcessful\n");
156 return EFI_ST_FAILURE;
157 }
158 len = EFI_ST_MAX_DATA_SIZE;
159 ret = runtime->get_variable(u"efi_none", &guid_vendor1,
160 &attr, &len, data);
161 if (ret != EFI_SUCCESS) {
162 efi_st_error("GetVariable failed\n");
163 return EFI_ST_FAILURE;
164 }
165 if (len != 15)
166 efi_st_todo("GetVariable returned wrong length %u\n",
167 (unsigned int)len);
168 if (memcmp(data, v, len))
169 efi_st_todo("GetVariable returned wrong value\n");
170 /* Delete variable efi_none */
171 ret = runtime->set_variable(u"efi_none", &guid_vendor1,
172 0, 0, NULL);
173 if (ret != EFI_SUCCESS) {
174 efi_st_error("SetVariable failed\n");
175 return EFI_ST_FAILURE;
176 }
177 len = EFI_ST_MAX_DATA_SIZE;
178 ret = runtime->get_variable(u"efi_none", &guid_vendor1,
179 &attr, &len, data);
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200180 if (ret != EFI_NOT_FOUND) {
Masahisa Kojima93a8c242024-04-02 18:09:50 +0900181 efi_st_error("Variable was not deleted\n");
Heinrich Schuchardtf69962a2019-09-26 21:35:20 +0200182 return EFI_ST_FAILURE;
183 }
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200184 /* Enumerate variables */
Heinrich Schuchardtd6bed222022-12-18 06:08:58 +0000185
186 ret = runtime->get_next_variable_name(NULL, u"efi_st_var1", &guid);
187 if (ret != EFI_INVALID_PARAMETER) {
188 efi_st_error("GetNextVariableName missing parameter check\n");
189 return EFI_ST_FAILURE;
190 }
191
192 len = 24;
193 ret = runtime->get_next_variable_name(&len, NULL, &guid);
194 if (ret != EFI_INVALID_PARAMETER) {
195 efi_st_error("GetNextVariableName missing parameter check\n");
196 return EFI_ST_FAILURE;
197 }
198
199 len = 24;
200 ret = runtime->get_next_variable_name(&len, u"efi_st_var1", NULL);
201 if (ret != EFI_INVALID_PARAMETER) {
202 efi_st_error("GetNextVariableName missing parameter check\n");
203 return EFI_ST_FAILURE;
204 }
205
206 len = 1;
207 ret = runtime->get_next_variable_name(&len, u"", &guid);
208 if (ret != EFI_INVALID_PARAMETER) {
209 efi_st_error("GetNextVariableName missing parameter check\n");
210 return EFI_ST_FAILURE;
211 }
212
213 len = 16;
214 ret = runtime->get_next_variable_name(&len, u"efi_st_var1", &guid);
215 if (ret != EFI_INVALID_PARAMETER) {
216 efi_st_error("GetNextVariableName missing parameter check\n");
217 return EFI_ST_FAILURE;
218 }
219
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200220 boottime->set_mem(&guid, 16, 0);
221 *varname = 0;
222 flag = 0;
223 for (;;) {
224 len = EFI_ST_MAX_VARNAME_SIZE;
225 ret = runtime->get_next_variable_name(&len, varname, &guid);
226 if (ret == EFI_NOT_FOUND)
227 break;
228 if (ret != EFI_SUCCESS) {
AKASHI Takahirob3d562e2019-01-21 12:43:14 +0100229 efi_st_error("GetNextVariableName failed (%u)\n",
230 (unsigned int)ret);
231 return EFI_ST_FAILURE;
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200232 }
Heinrich Schuchardt5f946662019-05-04 19:48:38 +0200233 if (!memcmp(&guid, &guid_vendor0, sizeof(efi_guid_t)) &&
Heinrich Schuchardtc1691872020-03-20 19:20:17 +0100234 !efi_st_strcmp_16_8(varname, "efi_st_var0")) {
AKASHI Takahirob3d562e2019-01-21 12:43:14 +0100235 flag |= 1;
Heinrich Schuchardtc1691872020-03-20 19:20:17 +0100236 if (len != 24) {
237 efi_st_error("GetNextVariableName report wrong length %u, expected 24\n",
238 (unsigned int)len);
239 return EFI_ST_FAILURE;
240 }
241 }
Heinrich Schuchardt5f946662019-05-04 19:48:38 +0200242 if (!memcmp(&guid, &guid_vendor1, sizeof(efi_guid_t)) &&
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200243 !efi_st_strcmp_16_8(varname, "efi_st_var1"))
244 flag |= 2;
245 }
AKASHI Takahirob3d562e2019-01-21 12:43:14 +0100246 if (flag != 3) {
247 efi_st_error(
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200248 "GetNextVariableName did not return all variables\n");
AKASHI Takahirob3d562e2019-01-21 12:43:14 +0100249 return EFI_ST_FAILURE;
250 }
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200251 /* Delete variable 1 */
Simon Glass90975372022-01-23 12:55:12 -0700252 ret = runtime->set_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200253 0, 0, NULL);
254 if (ret != EFI_SUCCESS) {
255 efi_st_error("SetVariable failed\n");
256 return EFI_ST_FAILURE;
257 }
258 len = EFI_ST_MAX_DATA_SIZE;
Simon Glass90975372022-01-23 12:55:12 -0700259 ret = runtime->get_variable(u"efi_st_var1", &guid_vendor1,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200260 &attr, &len, data);
261 if (ret != EFI_NOT_FOUND) {
262 efi_st_error("Variable was not deleted\n");
263 return EFI_ST_FAILURE;
264 }
265 /* Delete variable 0 */
Simon Glass90975372022-01-23 12:55:12 -0700266 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200267 0, 0, NULL);
268 if (ret != EFI_SUCCESS) {
269 efi_st_error("SetVariable failed\n");
270 return EFI_ST_FAILURE;
271 }
272 len = EFI_ST_MAX_DATA_SIZE;
Simon Glass90975372022-01-23 12:55:12 -0700273 ret = runtime->get_variable(u"efi_st_var0", &guid_vendor0,
Heinrich Schuchardt549fbca2018-05-17 07:57:06 +0200274 &attr, &len, data);
275 if (ret != EFI_NOT_FOUND) {
276 efi_st_error("Variable was not deleted\n");
277 return EFI_ST_FAILURE;
278 }
279
280 return EFI_ST_SUCCESS;
281}
282
283EFI_UNIT_TEST(variables) = {
284 .name = "variables",
285 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
286 .setup = setup,
287 .execute = execute,
288};