blob: afa91be62c85210e71cc5ad8e651795f455cd70e [file] [log] [blame]
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +02001// 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>
Ilias Apalodimasb0f75b62024-04-18 15:54:53 +030013#include <efi_variable.h>
14#include <u-boot/crc.h>
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +020015
16#define EFI_ST_MAX_DATA_SIZE 16
17#define EFI_ST_MAX_VARNAME_SIZE 40
18
19static struct efi_boot_services *boottime;
20static struct efi_runtime_services *runtime;
Heinrich Schuchardtfd956fc2020-07-06 17:06:07 +020021static const efi_guid_t guid_vendor0 = EFI_GLOBAL_VARIABLE_GUID;
Ilias Apalodimasb0f75b62024-04-18 15:54:53 +030022static const efi_guid_t __efi_runtime_data efi_rt_var_guid =
23 U_BOOT_EFI_RT_VAR_FILE_GUID;
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +020024
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() - execute unit test
42 *
43 * As runtime support is not implmented expect EFI_UNSUPPORTED to be returned.
44 */
45static int execute(void)
46{
47 efi_status_t ret;
Ilias Apalodimasb0f75b62024-04-18 15:54:53 +030048 efi_uintn_t len, avail, append_len = 17;
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +020049 u32 attr;
50 u8 v[16] = {0x5d, 0xd1, 0x5e, 0x51, 0x5a, 0x05, 0xc7, 0x0c,
51 0x35, 0x4a, 0xae, 0x87, 0xa5, 0xdf, 0x0f, 0x65,};
Ilias Apalodimasb0f75b62024-04-18 15:54:53 +030052 u8 v2[CONFIG_EFI_VAR_BUF_SIZE];
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +020053 u8 data[EFI_ST_MAX_DATA_SIZE];
Ilias Apalodimasb0f75b62024-04-18 15:54:53 +030054 u8 data2[CONFIG_EFI_VAR_BUF_SIZE];
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +020055 u16 varname[EFI_ST_MAX_VARNAME_SIZE];
56 efi_guid_t guid;
57 u64 max_storage, rem_storage, max_size;
58
Ilias Apalodimasb0f75b62024-04-18 15:54:53 +030059 memset(v2, 0x1, sizeof(v2));
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +020060 ret = runtime->query_variable_info(EFI_VARIABLE_BOOTSERVICE_ACCESS,
61 &max_storage, &rem_storage,
62 &max_size);
63 if (ret != EFI_UNSUPPORTED) {
64 efi_st_error("QueryVariableInfo failed\n");
65 return EFI_ST_FAILURE;
66 }
67
Simon Glass90975372022-01-23 12:55:12 -070068 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +020069 EFI_VARIABLE_BOOTSERVICE_ACCESS |
70 EFI_VARIABLE_RUNTIME_ACCESS,
71 3, v + 4);
Ilias Apalodimas86ba8692024-04-18 15:54:50 +030072 if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) {
Ilias Apalodimasb0f75b62024-04-18 15:54:53 +030073 efi_uintn_t prev_len, delta;
74 struct efi_var_entry *var;
75 struct efi_var_file *hdr;
76
Ilias Apalodimas86ba8692024-04-18 15:54:50 +030077 /* At runtime only non-volatile variables may be set. */
78 if (ret != EFI_INVALID_PARAMETER) {
79 efi_st_error("SetVariable failed\n");
80 return EFI_ST_FAILURE;
81 }
Ilias Apalodimasb0f75b62024-04-18 15:54:53 +030082
83 /* runtime atttribute must be set */
84 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
85 EFI_VARIABLE_BOOTSERVICE_ACCESS |
86 EFI_VARIABLE_NON_VOLATILE,
87 3, v + 4);
88 if (ret != EFI_INVALID_PARAMETER) {
89 efi_st_error("SetVariable failed\n");
90 return EFI_ST_FAILURE;
91 }
92
93 len = sizeof(data);
94 ret = runtime->get_variable(u"RTStorageVolatile",
95 &efi_rt_var_guid,
96 &attr, &len, data);
97 if (ret != EFI_SUCCESS) {
98 efi_st_error("GetVariable failed\n");
99 return EFI_ST_FAILURE;
100 }
101
102 if (len != sizeof(EFI_VAR_FILE_NAME) ||
103 memcmp(data, EFI_VAR_FILE_NAME, sizeof(EFI_VAR_FILE_NAME))) {
104 data[len - 1] = 0;
105 efi_st_error("RTStorageVolatile = %s\n", data);
106 return EFI_ST_FAILURE;
107 }
108
109 len = sizeof(data2);
110 ret = runtime->get_variable(u"VarToFile", &efi_rt_var_guid,
111 &attr, &len, data2);
112 if (ret != EFI_SUCCESS) {
113 efi_st_error("GetVariable failed\n");
114 return EFI_ST_FAILURE;
115 }
116 /*
117 * VarToFile size must change once a variable is inserted
118 * Store it now, we'll use it later
119 */
120 prev_len = len;
121 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
122 EFI_VARIABLE_BOOTSERVICE_ACCESS |
123 EFI_VARIABLE_RUNTIME_ACCESS |
124 EFI_VARIABLE_NON_VOLATILE,
125 sizeof(v2),
126 v2);
127 /*
128 * This will try to update VarToFile as well and must fail,
129 * without changing or deleting VarToFile
130 */
131 if (ret != EFI_OUT_OF_RESOURCES) {
132 efi_st_error("SetVariable failed\n");
133 return EFI_ST_FAILURE;
134 }
135 len = sizeof(data2);
136 ret = runtime->get_variable(u"VarToFile", &efi_rt_var_guid,
137 &attr, &len, data2);
138 if (ret != EFI_SUCCESS || prev_len != len) {
139 efi_st_error("Get/SetVariable failed\n");
140 return EFI_ST_FAILURE;
141 }
142
143 /* Add an 8byte aligned variable */
144 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
145 EFI_VARIABLE_BOOTSERVICE_ACCESS |
146 EFI_VARIABLE_RUNTIME_ACCESS |
147 EFI_VARIABLE_NON_VOLATILE,
148 sizeof(v), v);
149 if (ret != EFI_SUCCESS) {
150 efi_st_error("SetVariable failed\n");
151 return EFI_ST_FAILURE;
152 }
153
154 /* Delete it by setting the attrs to 0 */
155 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
156 0, sizeof(v), v);
157 if (ret != EFI_SUCCESS) {
158 efi_st_error("SetVariable failed\n");
159 return EFI_ST_FAILURE;
160 }
161
162 /* Add it back */
163 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
164 EFI_VARIABLE_BOOTSERVICE_ACCESS |
165 EFI_VARIABLE_RUNTIME_ACCESS |
166 EFI_VARIABLE_NON_VOLATILE,
167 sizeof(v), v);
168 if (ret != EFI_SUCCESS) {
169 efi_st_error("SetVariable failed\n");
170 return EFI_ST_FAILURE;
171 }
172
173 /* Delete it again by setting the size to 0 */
174 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
175 EFI_VARIABLE_BOOTSERVICE_ACCESS |
176 EFI_VARIABLE_RUNTIME_ACCESS |
177 EFI_VARIABLE_NON_VOLATILE,
178 0, NULL);
179 if (ret != EFI_SUCCESS) {
180 efi_st_error("SetVariable failed\n");
181 return EFI_ST_FAILURE;
182 }
183
184 /* Delete it again and make sure it's not there */
185 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
186 EFI_VARIABLE_BOOTSERVICE_ACCESS |
187 EFI_VARIABLE_RUNTIME_ACCESS |
188 EFI_VARIABLE_NON_VOLATILE,
189 0, NULL);
190 if (ret != EFI_NOT_FOUND) {
191 efi_st_error("SetVariable failed\n");
192 return EFI_ST_FAILURE;
193 }
194
195 /*
196 * Add a non-aligned variable
197 * VarToFile updates must include efi_st_var0
198 */
199 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
200 EFI_VARIABLE_BOOTSERVICE_ACCESS |
201 EFI_VARIABLE_RUNTIME_ACCESS |
202 EFI_VARIABLE_NON_VOLATILE,
203 9, v + 4);
204 if (ret != EFI_SUCCESS) {
205 efi_st_error("SetVariable failed\n");
206 return EFI_ST_FAILURE;
207 }
208 var = efi_var_mem_find(&guid_vendor0, u"efi_st_var0", NULL);
209 if (!var) {
210 efi_st_error("GetVariable failed\n");
211 return EFI_ST_FAILURE;
212 }
213 delta = efi_var_entry_len(var);
214 len = sizeof(data2);
215 ret = runtime->get_variable(u"VarToFile", &efi_rt_var_guid,
216 &attr, &len, data2);
217 if (ret != EFI_SUCCESS || prev_len + delta != len) {
218 efi_st_error("Get/SetVariable failed\n");
219 return EFI_ST_FAILURE;
220 }
221
222 /*
223 * Append on an existing variable must update VarToFile
224 * Our variable entries are 8-byte aligned.
225 * Adding a single byte will fit on the existing space
226 */
227 prev_len = len;
228 avail = efi_var_entry_len(var) -
229 (sizeof(u16) * (u16_strlen(var->name) + 1) + sizeof(*var)) -
230 var->length;
231 if (avail >= append_len)
232 delta = 0;
233 else
234 delta = ALIGN(append_len - avail, 8);
235 ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0,
236 EFI_VARIABLE_BOOTSERVICE_ACCESS |
237 EFI_VARIABLE_RUNTIME_ACCESS |
238 EFI_VARIABLE_APPEND_WRITE |
239 EFI_VARIABLE_NON_VOLATILE,
240 append_len, v2);
241 if (ret != EFI_SUCCESS) {
242 efi_st_error("SetVariable failed\n");
243 return EFI_ST_FAILURE;
244 }
245 len = sizeof(data2);
246 ret = runtime->get_variable(u"VarToFile", &efi_rt_var_guid,
247 &attr, &len, data2);
248 if (ret != EFI_SUCCESS) {
249 efi_st_error("GetVariable failed\n");
250 return EFI_ST_FAILURE;
251 }
252 if (prev_len + delta != len) {
253 efi_st_error("Unexpected VarToFile size");
254 return EFI_ST_FAILURE;
255 }
256
257 /* Make sure that variable contains a valid file */
258 hdr = (struct efi_var_file *)data2;
259 if (hdr->magic != EFI_VAR_FILE_MAGIC ||
260 len != hdr->length ||
261 hdr->crc32 != crc32(0, (u8 *)((uintptr_t)data2 + sizeof(struct efi_var_file)),
262 len - sizeof(struct efi_var_file))) {
263 efi_st_error("VarToFile invalid header\n");
264 return EFI_ST_FAILURE;
265 }
266
267 /* Variables that are BS, RT and volatile are RO after EBS */
268 ret = runtime->set_variable(u"VarToFile", &efi_rt_var_guid,
269 EFI_VARIABLE_BOOTSERVICE_ACCESS |
270 EFI_VARIABLE_RUNTIME_ACCESS |
271 EFI_VARIABLE_NON_VOLATILE,
272 sizeof(v), v);
273 if (ret != EFI_WRITE_PROTECTED) {
274 efi_st_error("Get/SetVariable failed\n");
275 return EFI_ST_FAILURE;
276 }
Ilias Apalodimas86ba8692024-04-18 15:54:50 +0300277 } else {
278 if (ret != EFI_UNSUPPORTED) {
279 efi_st_error("SetVariable failed\n");
280 return EFI_ST_FAILURE;
281 }
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +0200282 }
Heinrich Schuchardtfd956fc2020-07-06 17:06:07 +0200283 len = EFI_ST_MAX_DATA_SIZE;
Simon Glass90975372022-01-23 12:55:12 -0700284 ret = runtime->get_variable(u"PlatformLangCodes", &guid_vendor0,
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +0200285 &attr, &len, data);
Heinrich Schuchardtfd956fc2020-07-06 17:06:07 +0200286 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +0200287 efi_st_error("GetVariable failed\n");
288 return EFI_ST_FAILURE;
289 }
290 memset(&guid, 0, 16);
291 *varname = 0;
Heinrich Schuchardtfd956fc2020-07-06 17:06:07 +0200292 len = 2 * EFI_ST_MAX_VARNAME_SIZE;
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +0200293 ret = runtime->get_next_variable_name(&len, varname, &guid);
Heinrich Schuchardtfd956fc2020-07-06 17:06:07 +0200294 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0267f92019-06-20 16:18:48 +0200295 efi_st_error("GetNextVariableName failed\n");
296 return EFI_ST_FAILURE;
297 }
298
299 return EFI_ST_SUCCESS;
300}
301
302EFI_UNIT_TEST(variables_run) = {
303 .name = "variables at runtime",
304 .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
305 .setup = setup,
306 .execute = execute,
307};