blob: adb78470f2d64a1f1924e57d1316c65a1a740701 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clark15f3d742017-09-13 18:05:37 -04002/*
Heinrich Schuchardtc69addd2020-03-19 17:15:18 +00003 * UEFI runtime variable services
Rob Clark15f3d742017-09-13 18:05:37 -04004 *
Heinrich Schuchardtc69addd2020-03-19 17:15:18 +00005 * Copyright (c) 2017 Rob Clark
Rob Clark15f3d742017-09-13 18:05:37 -04006 */
7
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +02008#include <common.h>
Rob Clark15f3d742017-09-13 18:05:37 -04009#include <efi_loader.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060010#include <env_internal.h>
Heinrich Schuchardt147d5d32019-10-26 23:53:48 +020011#include <hexdump.h>
12#include <malloc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090013#include <rtc.h>
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +010014#include <search.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090015#include <linux/compat.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070016#include <u-boot/crc.h>
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +090017#include "../lib/crypto/pkcs7_parser.h"
18
19const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
20static bool efi_secure_boot;
Rob Clark15f3d742017-09-13 18:05:37 -040021
22#define READ_ONLY BIT(31)
23
24/*
25 * Mapping between EFI variables and u-boot variables:
26 *
27 * efi_$guid_$varname = {attributes}(type)value
28 *
29 * For example:
30 *
31 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
32 * "{ro,boot,run}(blob)0000000000000000"
33 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
34 * "(blob)00010000"
35 *
36 * The attributes are a comma separated list of these possible
37 * attributes:
38 *
39 * + ro - read-only
40 * + boot - boot-services access
41 * + run - runtime access
42 *
43 * NOTE: with current implementation, no variables are available after
44 * ExitBootServices, and all are persisted (if possible).
45 *
46 * If not specified, the attributes default to "{boot}".
47 *
48 * The required type is one of:
49 *
50 * + utf8 - raw utf8 string
51 * + blob - arbitrary length hex string
52 *
53 * Maybe a utf16 type would be useful to for a string value to be auto
54 * converted to utf16?
55 */
56
Heinrich Schuchardt44389cb2018-09-23 04:08:09 +020057#define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
Rob Clark15f3d742017-09-13 18:05:37 -040058
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +010059/**
60 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
61 * variable name
62 *
63 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
64 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
65 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
66 *
67 * @native: pointer to pointer to U-Boot variable name
68 * @variable_name: UEFI variable name
69 * @vendor: vendor GUID
70 * Return: status code
71 */
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020072static efi_status_t efi_to_native(char **native, const u16 *variable_name,
Heinrich Schuchardte06ae192018-12-30 20:53:51 +010073 const efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -040074{
75 size_t len;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020076 char *pos;
Rob Clark15f3d742017-09-13 18:05:37 -040077
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020078 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
79 *native = malloc(len);
80 if (!*native)
81 return EFI_OUT_OF_RESOURCES;
Rob Clark15f3d742017-09-13 18:05:37 -040082
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +020083 pos = *native;
84 pos += sprintf(pos, "efi_%pUl_", vendor);
85 utf16_utf8_strcpy(&pos, variable_name);
Rob Clark15f3d742017-09-13 18:05:37 -040086
87 return EFI_SUCCESS;
88}
89
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +010090/**
91 * prefix() - skip over prefix
92 *
93 * Skip over a prefix string.
94 *
95 * @str: string with prefix
96 * @prefix: prefix string
97 * Return: string without prefix, or NULL if prefix not found
98 */
Rob Clark15f3d742017-09-13 18:05:37 -040099static const char *prefix(const char *str, const char *prefix)
100{
101 size_t n = strlen(prefix);
102 if (!strncmp(prefix, str, n))
103 return str + n;
104 return NULL;
105}
106
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100107/**
108 * parse_attr() - decode attributes part of variable value
109 *
110 * Convert the string encoded attributes of a UEFI variable to a bit mask.
111 * TODO: Several attributes are not supported.
112 *
113 * @str: value of U-Boot variable
114 * @attrp: pointer to UEFI attributes
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900115 * @timep: pointer to time attribute
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100116 * Return: pointer to remainder of U-Boot variable value
117 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900118static const char *parse_attr(const char *str, u32 *attrp, u64 *timep)
Rob Clark15f3d742017-09-13 18:05:37 -0400119{
120 u32 attr = 0;
121 char sep = '{';
122
123 if (*str != '{') {
124 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
125 return str;
126 }
127
128 while (*str == sep) {
129 const char *s;
130
131 str++;
132
133 if ((s = prefix(str, "ro"))) {
134 attr |= READ_ONLY;
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900135 } else if ((s = prefix(str, "nv"))) {
136 attr |= EFI_VARIABLE_NON_VOLATILE;
Rob Clark15f3d742017-09-13 18:05:37 -0400137 } else if ((s = prefix(str, "boot"))) {
138 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
139 } else if ((s = prefix(str, "run"))) {
140 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900141 } else if ((s = prefix(str, "time="))) {
142 attr |= EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
143 hex2bin((u8 *)timep, s, sizeof(*timep));
144 s += sizeof(*timep) * 2;
145 } else if (*str == '}') {
146 break;
Rob Clark15f3d742017-09-13 18:05:37 -0400147 } else {
148 printf("invalid attribute: %s\n", str);
149 break;
150 }
151
152 str = s;
153 sep = ',';
154 }
155
156 str++;
157
158 *attrp = attr;
159
160 return str;
161}
162
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100163/**
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900164 * efi_secure_boot_enabled - return if secure boot is enabled or not
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100165 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900166 * Return: true if enabled, false if disabled
167 */
168bool efi_secure_boot_enabled(void)
169{
170 return efi_secure_boot;
171}
172
173#ifdef CONFIG_EFI_SECURE_BOOT
174static u8 pkcs7_hdr[] = {
175 /* SEQUENCE */
176 0x30, 0x82, 0x05, 0xc7,
177 /* OID: pkcs7-signedData */
178 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
179 /* Context Structured? */
180 0xa0, 0x82, 0x05, 0xb8,
181};
182
183/**
184 * efi_variable_parse_signature - parse a signature in variable
185 * @buf: Pointer to variable's value
186 * @buflen: Length of @buf
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100187 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900188 * Parse a signature embedded in variable's value and instantiate
189 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
190 * pkcs7's signedData, some header needed be prepended for correctly
191 * parsing authentication data, particularly for variable's.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100192 *
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900193 * Return: Pointer to pkcs7_message structure on success, NULL on error
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100194 */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900195static struct pkcs7_message *efi_variable_parse_signature(const void *buf,
196 size_t buflen)
197{
198 u8 *ebuf;
199 size_t ebuflen, len;
200 struct pkcs7_message *msg;
201
202 /*
203 * This is the best assumption to check if the binary is
204 * already in a form of pkcs7's signedData.
205 */
206 if (buflen > sizeof(pkcs7_hdr) &&
207 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
208 msg = pkcs7_parse_message(buf, buflen);
209 goto out;
210 }
211
212 /*
213 * Otherwise, we should add a dummy prefix sequence for pkcs7
214 * message parser to be able to process.
215 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
216 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
217 * TODO:
218 * The header should be composed in a more refined manner.
219 */
220 debug("Makeshift prefix added to authentication data\n");
221 ebuflen = sizeof(pkcs7_hdr) + buflen;
222 if (ebuflen <= 0x7f) {
223 debug("Data is too short\n");
224 return NULL;
225 }
226
227 ebuf = malloc(ebuflen);
228 if (!ebuf) {
229 debug("Out of memory\n");
230 return NULL;
231 }
232
233 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
234 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
235 len = ebuflen - 4;
236 ebuf[2] = (len >> 8) & 0xff;
237 ebuf[3] = len & 0xff;
238 len = ebuflen - 0x13;
239 ebuf[0x11] = (len >> 8) & 0xff;
240 ebuf[0x12] = len & 0xff;
241
242 msg = pkcs7_parse_message(ebuf, ebuflen);
243
244 free(ebuf);
245
246out:
247 if (IS_ERR(msg))
248 return NULL;
249
250 return msg;
251}
252
253/**
254 * efi_variable_authenticate - authenticate a variable
255 * @variable: Variable name in u16
256 * @vendor: Guid of variable
257 * @data_size: Size of @data
258 * @data: Pointer to variable's value
259 * @given_attr: Attributes to be given at SetVariable()
260 * @env_attr: Attributes that an existing variable holds
261 * @time: signed time that an existing variable holds
262 *
263 * Called by efi_set_variable() to verify that the input is correct.
264 * Will replace the given data pointer with another that points to
265 * the actual data to store in the internal memory.
266 * On success, @data and @data_size will be replaced with variable's
267 * actual data, excluding authentication data, and its size, and variable's
268 * attributes and signed time will also be returned in @env_attr and @time,
269 * respectively.
270 *
271 * Return: EFI_SUCCESS on success, status code (negative) on error
272 */
273static efi_status_t efi_variable_authenticate(u16 *variable,
274 const efi_guid_t *vendor,
275 efi_uintn_t *data_size,
276 const void **data, u32 given_attr,
277 u32 *env_attr, u64 *time)
278{
279 const struct efi_variable_authentication_2 *auth;
280 struct efi_signature_store *truststore, *truststore2;
281 struct pkcs7_message *var_sig;
282 struct efi_image_regions *regs;
283 struct efi_time timestamp;
284 struct rtc_time tm;
285 u64 new_time;
286 efi_status_t ret;
287
288 var_sig = NULL;
289 truststore = NULL;
290 truststore2 = NULL;
291 regs = NULL;
292 ret = EFI_SECURITY_VIOLATION;
293
294 if (*data_size < sizeof(struct efi_variable_authentication_2))
295 goto err;
296
297 /* authentication data */
298 auth = *data;
299 if (*data_size < (sizeof(auth->time_stamp)
300 + auth->auth_info.hdr.dwLength))
301 goto err;
302
303 if (guidcmp(&auth->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
304 goto err;
305
306 *data += sizeof(auth->time_stamp) + auth->auth_info.hdr.dwLength;
307 *data_size -= (sizeof(auth->time_stamp)
308 + auth->auth_info.hdr.dwLength);
309
310 memcpy(&timestamp, &auth->time_stamp, sizeof(timestamp));
311 memset(&tm, 0, sizeof(tm));
312 tm.tm_year = timestamp.year;
313 tm.tm_mon = timestamp.month;
314 tm.tm_mday = timestamp.day;
315 tm.tm_hour = timestamp.hour;
316 tm.tm_min = timestamp.minute;
317 tm.tm_sec = timestamp.second;
318 new_time = rtc_mktime(&tm);
319
320 if (!efi_secure_boot_enabled()) {
321 /* finished checking */
322 *time = new_time;
323 return EFI_SUCCESS;
324 }
325
326 if (new_time <= *time)
327 goto err;
328
329 /* data to be digested */
330 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 5, 1);
331 if (!regs)
332 goto err;
333 regs->max = 5;
334 efi_image_region_add(regs, (uint8_t *)variable,
335 (uint8_t *)variable
336 + u16_strlen(variable) * sizeof(u16), 1);
337 efi_image_region_add(regs, (uint8_t *)vendor,
338 (uint8_t *)vendor + sizeof(*vendor), 1);
339 efi_image_region_add(regs, (uint8_t *)&given_attr,
340 (uint8_t *)&given_attr + sizeof(given_attr), 1);
341 efi_image_region_add(regs, (uint8_t *)&timestamp,
342 (uint8_t *)&timestamp + sizeof(timestamp), 1);
343 efi_image_region_add(regs, (uint8_t *)*data,
344 (uint8_t *)*data + *data_size, 1);
345
346 /* variable's signature list */
347 if (auth->auth_info.hdr.dwLength < sizeof(auth->auth_info))
348 goto err;
349 var_sig = efi_variable_parse_signature(auth->auth_info.cert_data,
350 auth->auth_info.hdr.dwLength
351 - sizeof(auth->auth_info));
352 if (IS_ERR(var_sig)) {
353 debug("Parsing variable's signature failed\n");
354 var_sig = NULL;
355 goto err;
356 }
357
358 /* signature database used for authentication */
359 if (u16_strcmp(variable, L"PK") == 0 ||
360 u16_strcmp(variable, L"KEK") == 0) {
361 /* with PK */
362 truststore = efi_sigstore_parse_sigdb(L"PK");
363 if (!truststore)
364 goto err;
365 } else if (u16_strcmp(variable, L"db") == 0 ||
366 u16_strcmp(variable, L"dbx") == 0) {
367 /* with PK and KEK */
368 truststore = efi_sigstore_parse_sigdb(L"KEK");
369 truststore2 = efi_sigstore_parse_sigdb(L"PK");
370
371 if (!truststore) {
372 if (!truststore2)
373 goto err;
374
375 truststore = truststore2;
376 truststore2 = NULL;
377 }
378 } else {
379 /* TODO: support private authenticated variables */
380 goto err;
381 }
382
383 /* verify signature */
384 if (efi_signature_verify_with_sigdb(regs, var_sig, truststore, NULL)) {
385 debug("Verified\n");
386 } else {
387 if (truststore2 &&
388 efi_signature_verify_with_sigdb(regs, var_sig,
389 truststore2, NULL)) {
390 debug("Verified\n");
391 } else {
392 debug("Verifying variable's signature failed\n");
393 goto err;
394 }
395 }
396
397 /* finished checking */
398 *time = rtc_mktime(&tm);
399 ret = EFI_SUCCESS;
400
401err:
402 efi_sigstore_free(truststore);
403 efi_sigstore_free(truststore2);
404 pkcs7_free_message(var_sig);
405 free(regs);
406
407 return ret;
408}
409#else
410static efi_status_t efi_variable_authenticate(u16 *variable,
411 const efi_guid_t *vendor,
412 efi_uintn_t *data_size,
413 const void **data, u32 given_attr,
414 u32 *env_attr, u64 *time)
415{
416 return EFI_SUCCESS;
417}
418#endif /* CONFIG_EFI_SECURE_BOOT */
419
420static
421efi_status_t EFIAPI efi_get_variable_common(u16 *variable_name,
422 const efi_guid_t *vendor,
423 u32 *attributes,
424 efi_uintn_t *data_size, void *data,
425 bool is_non_volatile)
Rob Clark15f3d742017-09-13 18:05:37 -0400426{
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200427 char *native_name;
Rob Clark15f3d742017-09-13 18:05:37 -0400428 efi_status_t ret;
429 unsigned long in_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900430 const char *val = NULL, *s;
431 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400432 u32 attr;
433
Rob Clark15f3d742017-09-13 18:05:37 -0400434 if (!variable_name || !vendor || !data_size)
435 return EFI_EXIT(EFI_INVALID_PARAMETER);
436
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200437 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400438 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900439 return ret;
Rob Clark15f3d742017-09-13 18:05:37 -0400440
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200441 EFI_PRINT("get '%s'\n", native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400442
443 val = env_get(native_name);
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200444 free(native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400445 if (!val)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900446 return EFI_NOT_FOUND;
Rob Clark15f3d742017-09-13 18:05:37 -0400447
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900448 val = parse_attr(val, &attr, &time);
Rob Clark15f3d742017-09-13 18:05:37 -0400449
450 in_size = *data_size;
451
452 if ((s = prefix(val, "(blob)"))) {
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100453 size_t len = strlen(s);
Rob Clark15f3d742017-09-13 18:05:37 -0400454
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700455 /* number of hexadecimal digits must be even */
456 if (len & 1)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900457 return EFI_DEVICE_ERROR;
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700458
Rob Clark15f3d742017-09-13 18:05:37 -0400459 /* two characters per byte: */
Ivan Gorinovd3ea6b72018-05-11 13:18:25 -0700460 len /= 2;
Rob Clark15f3d742017-09-13 18:05:37 -0400461 *data_size = len;
462
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200463 if (in_size < len) {
464 ret = EFI_BUFFER_TOO_SMALL;
465 goto out;
466 }
Rob Clark15f3d742017-09-13 18:05:37 -0400467
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900468 if (!data) {
469 debug("Variable with no data shouldn't exist.\n");
470 return EFI_INVALID_PARAMETER;
471 }
Rob Clark15f3d742017-09-13 18:05:37 -0400472
Heinrich Schuchardt2d8aedc2019-01-18 12:31:54 +0100473 if (hex2bin(data, s, len))
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900474 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400475
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200476 EFI_PRINT("got value: \"%s\"\n", s);
Rob Clark15f3d742017-09-13 18:05:37 -0400477 } else if ((s = prefix(val, "(utf8)"))) {
478 unsigned len = strlen(s) + 1;
479
480 *data_size = len;
481
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200482 if (in_size < len) {
483 ret = EFI_BUFFER_TOO_SMALL;
484 goto out;
485 }
Rob Clark15f3d742017-09-13 18:05:37 -0400486
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900487 if (!data) {
488 debug("Variable with no data shouldn't exist.\n");
489 return EFI_INVALID_PARAMETER;
490 }
Rob Clark15f3d742017-09-13 18:05:37 -0400491
492 memcpy(data, s, len);
493 ((char *)data)[len] = '\0';
494
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200495 EFI_PRINT("got value: \"%s\"\n", (char *)data);
Rob Clark15f3d742017-09-13 18:05:37 -0400496 } else {
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200497 EFI_PRINT("invalid value: '%s'\n", val);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900498 return EFI_DEVICE_ERROR;
Rob Clark15f3d742017-09-13 18:05:37 -0400499 }
500
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200501out:
Rob Clark15f3d742017-09-13 18:05:37 -0400502 if (attributes)
503 *attributes = attr & EFI_VARIABLE_MASK;
504
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900505 return ret;
506}
507
508static
509efi_status_t EFIAPI efi_get_volatile_variable(u16 *variable_name,
510 const efi_guid_t *vendor,
511 u32 *attributes,
512 efi_uintn_t *data_size,
513 void *data)
514{
515 return efi_get_variable_common(variable_name, vendor, attributes,
516 data_size, data, false);
517}
518
519efi_status_t EFIAPI efi_get_nonvolatile_variable(u16 *variable_name,
520 const efi_guid_t *vendor,
521 u32 *attributes,
522 efi_uintn_t *data_size,
523 void *data)
524{
525 return efi_get_variable_common(variable_name, vendor, attributes,
526 data_size, data, true);
527}
528
529/**
530 * efi_efi_get_variable() - retrieve value of a UEFI variable
531 *
532 * This function implements the GetVariable runtime service.
533 *
534 * See the Unified Extensible Firmware Interface (UEFI) specification for
535 * details.
536 *
537 * @variable_name: name of the variable
538 * @vendor: vendor GUID
539 * @attributes: attributes of the variable
540 * @data_size: size of the buffer to which the variable value is copied
541 * @data: buffer to which the variable value is copied
542 * Return: status code
543 */
544efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
545 const efi_guid_t *vendor, u32 *attributes,
546 efi_uintn_t *data_size, void *data)
547{
548 efi_status_t ret;
549
550 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
551 data_size, data);
552
553 ret = efi_get_volatile_variable(variable_name, vendor, attributes,
554 data_size, data);
555 if (ret == EFI_NOT_FOUND)
556 ret = efi_get_nonvolatile_variable(variable_name, vendor,
557 attributes, data_size, data);
558
Heinrich Schuchardt05f728f2019-05-15 19:32:43 +0200559 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400560}
561
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100562static char *efi_variables_list;
563static char *efi_cur_variable;
564
565/**
566 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
567 * information
568 * @variable: whole data of u-boot variable (ie. name=value)
569 * @variable_name_size: size of variable_name buffer in byte
570 * @variable_name: name of uefi variable in u16, null-terminated
571 * @vendor: vendor's guid
572 * @attributes: attributes
573 *
574 * A uefi variable is encoded into a u-boot variable as described above.
575 * This function parses such a u-boot variable and retrieve uefi-related
576 * information into respective parameters. In return, variable_name_size
577 * is the size of variable name including NULL.
578 *
579 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200580 * the entire variable list has been returned,
581 * otherwise non-zero status code
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100582 */
583static efi_status_t parse_uboot_variable(char *variable,
584 efi_uintn_t *variable_name_size,
585 u16 *variable_name,
586 const efi_guid_t *vendor,
587 u32 *attributes)
588{
589 char *guid, *name, *end, c;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100590 size_t name_len;
591 efi_uintn_t old_variable_name_size;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900592 u64 time;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100593 u16 *p;
594
595 guid = strchr(variable, '_');
596 if (!guid)
597 return EFI_INVALID_PARAMETER;
598 guid++;
599 name = strchr(guid, '_');
600 if (!name)
601 return EFI_INVALID_PARAMETER;
602 name++;
603 end = strchr(name, '=');
604 if (!end)
605 return EFI_INVALID_PARAMETER;
606
607 name_len = end - name;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100608 old_variable_name_size = *variable_name_size;
609 *variable_name_size = sizeof(u16) * (name_len + 1);
610 if (old_variable_name_size < *variable_name_size)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100611 return EFI_BUFFER_TOO_SMALL;
Heinrich Schuchardt31d9b3a2020-03-20 19:04:34 +0100612
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100613 end++; /* point to value */
614
615 /* variable name */
616 p = variable_name;
617 utf8_utf16_strncpy(&p, name, name_len);
618 variable_name[name_len] = 0;
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100619
620 /* guid */
621 c = *(name - 1);
622 *(name - 1) = '\0'; /* guid need be null-terminated here */
623 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
624 *(name - 1) = c;
625
626 /* attributes */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900627 parse_attr(end, attributes, &time);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100628
629 return EFI_SUCCESS;
630}
631
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100632/**
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100633 * efi_get_next_variable_name() - enumerate the current variable names
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200634 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100635 * @variable_name_size: size of variable_name buffer in byte
636 * @variable_name: name of uefi variable's name in u16
637 * @vendor: vendor's guid
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100638 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100639 * This function implements the GetNextVariableName service.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100640 *
641 * See the Unified Extensible Firmware Interface (UEFI) specification for
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +0200642 * details.
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100643 *
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100644 * Return: status code
Heinrich Schuchardt0ffda472019-01-18 19:52:05 +0100645 */
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200646efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
647 u16 *variable_name,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +0100648 efi_guid_t *vendor)
Rob Clark15f3d742017-09-13 18:05:37 -0400649{
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100650 char *native_name, *variable;
651 ssize_t name_len, list_len;
652 char regex[256];
653 char * const regexlist[] = {regex};
654 u32 attributes;
655 int i;
656 efi_status_t ret;
657
Rob Clark238f88c2017-09-13 18:05:41 -0400658 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400659
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100660 if (!variable_name_size || !variable_name || !vendor)
Heinrich Schuchardt793a6252019-03-19 18:36:21 +0100661 return EFI_EXIT(EFI_INVALID_PARAMETER);
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100662
663 if (variable_name[0]) {
664 /* check null-terminated string */
665 for (i = 0; i < *variable_name_size; i++)
666 if (!variable_name[i])
667 break;
668 if (i >= *variable_name_size)
669 return EFI_EXIT(EFI_INVALID_PARAMETER);
670
671 /* search for the last-returned variable */
672 ret = efi_to_native(&native_name, variable_name, vendor);
673 if (ret)
674 return EFI_EXIT(ret);
675
676 name_len = strlen(native_name);
677 for (variable = efi_variables_list; variable && *variable;) {
678 if (!strncmp(variable, native_name, name_len) &&
679 variable[name_len] == '=')
680 break;
681
682 variable = strchr(variable, '\n');
683 if (variable)
684 variable++;
685 }
686
687 free(native_name);
688 if (!(variable && *variable))
689 return EFI_EXIT(EFI_INVALID_PARAMETER);
690
691 /* next variable */
692 variable = strchr(variable, '\n');
693 if (variable)
694 variable++;
695 if (!(variable && *variable))
696 return EFI_EXIT(EFI_NOT_FOUND);
697 } else {
698 /*
699 *new search: free a list used in the previous search
700 */
701 free(efi_variables_list);
702 efi_variables_list = NULL;
703 efi_cur_variable = NULL;
704
705 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
706 list_len = hexport_r(&env_htab, '\n',
707 H_MATCH_REGEX | H_MATCH_KEY,
708 &efi_variables_list, 0, 1, regexlist);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900709
Heinrich Schuchardtfd738482019-01-22 20:10:46 +0100710 if (list_len <= 1)
AKASHI Takahiroad3f36c2019-01-21 12:43:13 +0100711 return EFI_EXIT(EFI_NOT_FOUND);
712
713 variable = efi_variables_list;
714 }
715
716 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
717 vendor, &attributes);
718
719 return EFI_EXIT(ret);
Rob Clark15f3d742017-09-13 18:05:37 -0400720}
721
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900722static
723efi_status_t EFIAPI efi_set_variable_common(u16 *variable_name,
724 const efi_guid_t *vendor,
725 u32 attributes,
726 efi_uintn_t data_size,
727 const void *data,
728 bool ro_check,
729 bool is_non_volatile)
Rob Clark15f3d742017-09-13 18:05:37 -0400730{
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900731 char *native_name = NULL, *old_data = NULL, *val = NULL, *s;
732 efi_uintn_t old_size;
733 bool append, delete;
734 u64 time = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400735 u32 attr;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900736 efi_status_t ret = EFI_SUCCESS;
Rob Clark15f3d742017-09-13 18:05:37 -0400737
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900738 debug("%s: set '%s'\n", __func__, native_name);
Rob Clark15f3d742017-09-13 18:05:37 -0400739
Heinrich Schuchardtadba3962019-06-12 23:28:42 +0200740 if (!variable_name || !*variable_name || !vendor ||
741 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900742 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200743 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900744 goto err;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200745 }
Rob Clark15f3d742017-09-13 18:05:37 -0400746
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200747 ret = efi_to_native(&native_name, variable_name, vendor);
Rob Clark15f3d742017-09-13 18:05:37 -0400748 if (ret)
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900749 goto err;
Rob Clark15f3d742017-09-13 18:05:37 -0400750
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900751 /* check if a variable exists */
752 old_size = 0;
753 attr = 0;
754 ret = EFI_CALL(efi_get_variable(variable_name, vendor, &attr,
755 &old_size, NULL));
756 if (ret == EFI_BUFFER_TOO_SMALL) {
757 if ((is_non_volatile && !(attr & EFI_VARIABLE_NON_VOLATILE)) ||
758 (!is_non_volatile && (attr & EFI_VARIABLE_NON_VOLATILE))) {
759 ret = EFI_INVALID_PARAMETER;
760 goto err;
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200761 }
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900762 }
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900763
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900764 append = !!(attributes & EFI_VARIABLE_APPEND_WRITE);
765 attributes &= ~(u32)EFI_VARIABLE_APPEND_WRITE;
766 delete = !append && (!data_size || !attributes);
767
768 /* check attributes */
769 if (old_size) {
770 if (ro_check && (attr & READ_ONLY)) {
771 ret = EFI_WRITE_PROTECTED;
772 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900773 }
774
775 /* attributes won't be changed */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900776 if (!delete &&
777 ((ro_check && attr != attributes) ||
778 (!ro_check && ((attr & ~(u32)READ_ONLY)
779 != (attributes & ~(u32)READ_ONLY))))) {
AKASHI Takahiro186b5a42019-05-24 15:59:03 +0900780 ret = EFI_INVALID_PARAMETER;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900781 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900782 }
783 } else {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900784 if (delete || append) {
Heinrich Schuchardt8021bbe2019-09-26 21:40:18 +0200785 /*
786 * Trying to delete or to update a non-existent
787 * variable.
788 */
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900789 ret = EFI_NOT_FOUND;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900790 goto err;
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900791 }
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900792 }
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900793
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900794 if (((!u16_strcmp(variable_name, L"PK") ||
795 !u16_strcmp(variable_name, L"KEK")) &&
796 !guidcmp(vendor, &efi_global_variable_guid)) ||
797 ((!u16_strcmp(variable_name, L"db") ||
798 !u16_strcmp(variable_name, L"dbx")) &&
799 !guidcmp(vendor, &efi_guid_image_security_database))) {
800 /* authentication is mandatory */
801 if (!(attributes &
802 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
803 debug("%ls: AUTHENTICATED_WRITE_ACCESS required\n",
804 variable_name);
805 ret = EFI_INVALID_PARAMETER;
806 goto err;
807 }
808 }
809
810 /* authenticate a variable */
811 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
812 if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
813 ret = EFI_INVALID_PARAMETER;
814 goto err;
815 }
816 if (attributes &
817 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
818 ret = efi_variable_authenticate(variable_name, vendor,
819 &data_size, &data,
820 attributes, &attr,
821 &time);
822 if (ret != EFI_SUCCESS)
823 goto err;
824
825 /* last chance to check for delete */
826 if (!data_size)
827 delete = true;
828 }
829 } else {
830 if (attributes &
831 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
832 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
833 debug("Secure boot is not configured\n");
834 ret = EFI_INVALID_PARAMETER;
835 goto err;
836 }
837 }
838
839 /* delete a variable */
840 if (delete) {
841 /* !old_size case has been handled before */
842 val = NULL;
843 ret = EFI_SUCCESS;
844 goto out;
845 }
846
847 if (append) {
848 old_data = malloc(old_size);
849 if (!old_data) {
850 return EFI_OUT_OF_RESOURCES;
851 goto err;
852 }
853 ret = EFI_CALL(efi_get_variable(variable_name, vendor,
854 &attr, &old_size, old_data));
855 if (ret != EFI_SUCCESS)
856 goto err;
857 } else {
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900858 old_size = 0;
Rob Clark15f3d742017-09-13 18:05:37 -0400859 }
860
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900861 val = malloc(2 * old_size + 2 * data_size
862 + strlen("{ro,run,boot,nv,time=0123456701234567}(blob)")
863 + 1);
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +0200864 if (!val) {
865 ret = EFI_OUT_OF_RESOURCES;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900866 goto err;
Heinrich Schuchardtbd095f52018-10-02 05:30:05 +0200867 }
Rob Clark15f3d742017-09-13 18:05:37 -0400868
869 s = val;
870
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900871 /*
872 * store attributes
873 */
874 attributes &= (READ_ONLY |
875 EFI_VARIABLE_NON_VOLATILE |
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900876 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900877 EFI_VARIABLE_RUNTIME_ACCESS |
878 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS);
Rob Clark15f3d742017-09-13 18:05:37 -0400879 s += sprintf(s, "{");
880 while (attributes) {
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900881 attr = 1 << (ffs(attributes) - 1);
Rob Clark15f3d742017-09-13 18:05:37 -0400882
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900883 if (attr == READ_ONLY) {
884 s += sprintf(s, "ro");
885 } else if (attr == EFI_VARIABLE_NON_VOLATILE) {
AKASHI Takahiro7f334332019-06-04 15:52:06 +0900886 s += sprintf(s, "nv");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900887 } else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -0400888 s += sprintf(s, "boot");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900889 } else if (attr == EFI_VARIABLE_RUNTIME_ACCESS) {
Rob Clark15f3d742017-09-13 18:05:37 -0400890 s += sprintf(s, "run");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900891 } else if (attr ==
892 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
893 s += sprintf(s, "time=");
894 s = bin2hex(s, (u8 *)&time, sizeof(time));
895 }
Rob Clark15f3d742017-09-13 18:05:37 -0400896
897 attributes &= ~attr;
898 if (attributes)
899 s += sprintf(s, ",");
900 }
901 s += sprintf(s, "}");
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900902 s += sprintf(s, "(blob)");
AKASHI Takahiroe5023b72019-09-06 15:09:52 +0900903
Rob Clark15f3d742017-09-13 18:05:37 -0400904 /* store payload: */
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900905 if (append)
906 s = bin2hex(s, old_data, old_size);
Heinrich Schuchardt807899d2019-01-18 18:54:26 +0100907 s = bin2hex(s, data, data_size);
Rob Clark15f3d742017-09-13 18:05:37 -0400908 *s = '\0';
909
Heinrich Schuchardt55111c32019-04-04 21:36:44 +0200910 EFI_PRINT("setting: %s=%s\n", native_name, val);
Rob Clark15f3d742017-09-13 18:05:37 -0400911
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900912out:
Rob Clark15f3d742017-09-13 18:05:37 -0400913 if (env_set(native_name, val))
914 ret = EFI_DEVICE_ERROR;
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900915 else
916 ret = EFI_SUCCESS;
Rob Clark15f3d742017-09-13 18:05:37 -0400917
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900918err:
Heinrich Schuchardtaf1a9202018-08-31 21:31:31 +0200919 free(native_name);
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900920 free(old_data);
Rob Clark15f3d742017-09-13 18:05:37 -0400921 free(val);
922
AKASHI Takahirob0f49ee2020-04-14 11:51:41 +0900923 return ret;
924}
925
926static
927efi_status_t EFIAPI efi_set_volatile_variable(u16 *variable_name,
928 const efi_guid_t *vendor,
929 u32 attributes,
930 efi_uintn_t data_size,
931 const void *data,
932 bool ro_check)
933{
934 return efi_set_variable_common(variable_name, vendor, attributes,
935 data_size, data, ro_check, false);
936}
937
938efi_status_t EFIAPI efi_set_nonvolatile_variable(u16 *variable_name,
939 const efi_guid_t *vendor,
940 u32 attributes,
941 efi_uintn_t data_size,
942 const void *data,
943 bool ro_check)
944{
945 efi_status_t ret;
946
947 ret = efi_set_variable_common(variable_name, vendor, attributes,
948 data_size, data, ro_check, true);
949
950 return ret;
951}
952
953static efi_status_t efi_set_variable_internal(u16 *variable_name,
954 const efi_guid_t *vendor,
955 u32 attributes,
956 efi_uintn_t data_size,
957 const void *data,
958 bool ro_check)
959{
960 efi_status_t ret;
961
962 if (attributes & EFI_VARIABLE_NON_VOLATILE)
963 ret = efi_set_nonvolatile_variable(variable_name, vendor,
964 attributes,
965 data_size, data, ro_check);
966 else
967 ret = efi_set_volatile_variable(variable_name, vendor,
968 attributes, data_size, data,
969 ro_check);
970
971 return ret;
972}
973
974/**
975 * efi_set_variable() - set value of a UEFI variable
976 *
977 * This function implements the SetVariable runtime service.
978 *
979 * See the Unified Extensible Firmware Interface (UEFI) specification for
980 * details.
981 *
982 * @variable_name: name of the variable
983 * @vendor: vendor GUID
984 * @attributes: attributes of the variable
985 * @data_size: size of the buffer with the variable value
986 * @data: buffer with the variable value
987 * Return: status code
988 */
989efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
990 const efi_guid_t *vendor, u32 attributes,
991 efi_uintn_t data_size, const void *data)
992{
993 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
994 data_size, data);
995
996 /* READ_ONLY bit is not part of API */
997 attributes &= ~(u32)READ_ONLY;
998
999 return EFI_EXIT(efi_set_variable_internal(variable_name, vendor,
1000 attributes, data_size, data,
1001 true));
Rob Clark15f3d742017-09-13 18:05:37 -04001002}
Heinrich Schuchardt9b19dae2019-06-20 12:13:05 +02001003
1004/**
1005 * efi_query_variable_info() - get information about EFI variables
1006 *
1007 * This function implements the QueryVariableInfo() runtime service.
1008 *
1009 * See the Unified Extensible Firmware Interface (UEFI) specification for
1010 * details.
1011 *
1012 * @attributes: bitmask to select variables to be
1013 * queried
1014 * @maximum_variable_storage_size: maximum size of storage area for the
1015 * selected variable types
1016 * @remaining_variable_storage_size: remaining size of storage are for the
1017 * selected variable types
1018 * @maximum_variable_size: maximum size of a variable of the
1019 * selected type
1020 * Returns: status code
1021 */
1022efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
1023 u32 attributes,
1024 u64 *maximum_variable_storage_size,
1025 u64 *remaining_variable_storage_size,
1026 u64 *maximum_variable_size)
1027{
1028 return EFI_UNSUPPORTED;
1029}
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001030
1031/**
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001032 * efi_get_variable_runtime() - runtime implementation of GetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001033 *
1034 * @variable_name: name of the variable
1035 * @vendor: vendor GUID
1036 * @attributes: attributes of the variable
1037 * @data_size: size of the buffer to which the variable value is copied
1038 * @data: buffer to which the variable value is copied
1039 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001040 */
1041static efi_status_t __efi_runtime EFIAPI
1042efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1043 u32 *attributes, efi_uintn_t *data_size, void *data)
1044{
1045 return EFI_UNSUPPORTED;
1046}
1047
1048/**
1049 * efi_get_next_variable_name_runtime() - runtime implementation of
1050 * GetNextVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001051 *
1052 * @variable_name_size: size of variable_name buffer in byte
1053 * @variable_name: name of uefi variable's name in u16
1054 * @vendor: vendor's guid
1055 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001056 */
1057static efi_status_t __efi_runtime EFIAPI
1058efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
Heinrich Schuchardt82cd6c42020-03-22 18:28:20 +01001059 u16 *variable_name, efi_guid_t *vendor)
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001060{
1061 return EFI_UNSUPPORTED;
1062}
1063
1064/**
1065 * efi_set_variable_runtime() - runtime implementation of SetVariable()
Heinrich Schuchardtee1f0e42019-07-14 12:11:16 +02001066 *
1067 * @variable_name: name of the variable
1068 * @vendor: vendor GUID
1069 * @attributes: attributes of the variable
1070 * @data_size: size of the buffer with the variable value
1071 * @data: buffer with the variable value
1072 * Return: status code
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001073 */
1074static efi_status_t __efi_runtime EFIAPI
1075efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
1076 u32 attributes, efi_uintn_t data_size,
1077 const void *data)
1078{
1079 return EFI_UNSUPPORTED;
1080}
1081
1082/**
1083 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
1084 */
1085void efi_variables_boot_exit_notify(void)
1086{
1087 efi_runtime_services.get_variable = efi_get_variable_runtime;
1088 efi_runtime_services.get_next_variable_name =
1089 efi_get_next_variable_name_runtime;
1090 efi_runtime_services.set_variable = efi_set_variable_runtime;
1091 efi_update_table_header_crc32(&efi_runtime_services.hdr);
1092}
1093
1094/**
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +02001095 * efi_init_variables() - initialize variable services
1096 *
1097 * Return: status code
1098 */
1099efi_status_t efi_init_variables(void)
1100{
1101 return EFI_SUCCESS;
1102}