1 // SPDX-License-Identifier: GPL-2.0+
3 * UEFI runtime variable services
5 * Copyright (c) 2017 Rob Clark
9 #include <efi_loader.h>
10 #include <env_internal.h>
14 #include <u-boot/crc.h>
16 #define READ_ONLY BIT(31)
19 * Mapping between EFI variables and u-boot variables:
21 * efi_$guid_$varname = {attributes}(type)value
25 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
26 * "{ro,boot,run}(blob)0000000000000000"
27 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
30 * The attributes are a comma separated list of these possible
34 * + boot - boot-services access
35 * + run - runtime access
37 * NOTE: with current implementation, no variables are available after
38 * ExitBootServices, and all are persisted (if possible).
40 * If not specified, the attributes default to "{boot}".
42 * The required type is one of:
44 * + utf8 - raw utf8 string
45 * + blob - arbitrary length hex string
47 * Maybe a utf16 type would be useful to for a string value to be auto
51 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
54 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
57 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
58 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
59 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
61 * @native: pointer to pointer to U-Boot variable name
62 * @variable_name: UEFI variable name
63 * @vendor: vendor GUID
66 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
67 const efi_guid_t *vendor)
72 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
73 *native = malloc(len);
75 return EFI_OUT_OF_RESOURCES;
78 pos += sprintf(pos, "efi_%pUl_", vendor);
79 utf16_utf8_strcpy(&pos, variable_name);
85 * prefix() - skip over prefix
87 * Skip over a prefix string.
89 * @str: string with prefix
90 * @prefix: prefix string
91 * Return: string without prefix, or NULL if prefix not found
93 static const char *prefix(const char *str, const char *prefix)
95 size_t n = strlen(prefix);
96 if (!strncmp(prefix, str, n))
102 * parse_attr() - decode attributes part of variable value
104 * Convert the string encoded attributes of a UEFI variable to a bit mask.
105 * TODO: Several attributes are not supported.
107 * @str: value of U-Boot variable
108 * @attrp: pointer to UEFI attributes
109 * Return: pointer to remainder of U-Boot variable value
111 static const char *parse_attr(const char *str, u32 *attrp)
117 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
121 while (*str == sep) {
126 if ((s = prefix(str, "ro"))) {
128 } else if ((s = prefix(str, "nv"))) {
129 attr |= EFI_VARIABLE_NON_VOLATILE;
130 } else if ((s = prefix(str, "boot"))) {
131 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
132 } else if ((s = prefix(str, "run"))) {
133 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
135 printf("invalid attribute: %s\n", str);
151 * efi_get_variable() - retrieve value of a UEFI variable
153 * This function implements the GetVariable runtime service.
155 * See the Unified Extensible Firmware Interface (UEFI) specification for
158 * @variable_name: name of the variable
159 * @vendor: vendor GUID
160 * @attributes: attributes of the variable
161 * @data_size: size of the buffer to which the variable value is copied
162 * @data: buffer to which the variable value is copied
163 * Return: status code
165 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
166 const efi_guid_t *vendor, u32 *attributes,
167 efi_uintn_t *data_size, void *data)
171 unsigned long in_size;
175 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
178 if (!variable_name || !vendor || !data_size)
179 return EFI_EXIT(EFI_INVALID_PARAMETER);
181 ret = efi_to_native(&native_name, variable_name, vendor);
183 return EFI_EXIT(ret);
185 EFI_PRINT("get '%s'\n", native_name);
187 val = env_get(native_name);
190 return EFI_EXIT(EFI_NOT_FOUND);
192 val = parse_attr(val, &attr);
194 in_size = *data_size;
196 if ((s = prefix(val, "(blob)"))) {
197 size_t len = strlen(s);
199 /* number of hexadecimal digits must be even */
201 return EFI_EXIT(EFI_DEVICE_ERROR);
203 /* two characters per byte: */
208 ret = EFI_BUFFER_TOO_SMALL;
213 return EFI_EXIT(EFI_INVALID_PARAMETER);
215 if (hex2bin(data, s, len))
216 return EFI_EXIT(EFI_DEVICE_ERROR);
218 EFI_PRINT("got value: \"%s\"\n", s);
219 } else if ((s = prefix(val, "(utf8)"))) {
220 unsigned len = strlen(s) + 1;
225 ret = EFI_BUFFER_TOO_SMALL;
230 return EFI_EXIT(EFI_INVALID_PARAMETER);
232 memcpy(data, s, len);
233 ((char *)data)[len] = '\0';
235 EFI_PRINT("got value: \"%s\"\n", (char *)data);
237 EFI_PRINT("invalid value: '%s'\n", val);
238 return EFI_EXIT(EFI_DEVICE_ERROR);
243 *attributes = attr & EFI_VARIABLE_MASK;
245 return EFI_EXIT(ret);
248 static char *efi_variables_list;
249 static char *efi_cur_variable;
252 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
254 * @variable: whole data of u-boot variable (ie. name=value)
255 * @variable_name_size: size of variable_name buffer in byte
256 * @variable_name: name of uefi variable in u16, null-terminated
257 * @vendor: vendor's guid
258 * @attributes: attributes
260 * A uefi variable is encoded into a u-boot variable as described above.
261 * This function parses such a u-boot variable and retrieve uefi-related
262 * information into respective parameters. In return, variable_name_size
263 * is the size of variable name including NULL.
265 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
266 * the entire variable list has been returned,
267 * otherwise non-zero status code
269 static efi_status_t parse_uboot_variable(char *variable,
270 efi_uintn_t *variable_name_size,
272 const efi_guid_t *vendor,
275 char *guid, *name, *end, c;
277 efi_uintn_t old_variable_name_size;
280 guid = strchr(variable, '_');
282 return EFI_INVALID_PARAMETER;
284 name = strchr(guid, '_');
286 return EFI_INVALID_PARAMETER;
288 end = strchr(name, '=');
290 return EFI_INVALID_PARAMETER;
292 name_len = end - name;
293 old_variable_name_size = *variable_name_size;
294 *variable_name_size = sizeof(u16) * (name_len + 1);
295 if (old_variable_name_size < *variable_name_size)
296 return EFI_BUFFER_TOO_SMALL;
298 end++; /* point to value */
302 utf8_utf16_strncpy(&p, name, name_len);
303 variable_name[name_len] = 0;
307 *(name - 1) = '\0'; /* guid need be null-terminated here */
308 uuid_str_to_bin(guid, (unsigned char *)vendor, UUID_STR_FORMAT_GUID);
312 parse_attr(end, attributes);
318 * efi_get_next_variable_name() - enumerate the current variable names
320 * @variable_name_size: size of variable_name buffer in byte
321 * @variable_name: name of uefi variable's name in u16
322 * @vendor: vendor's guid
324 * This function implements the GetNextVariableName service.
326 * See the Unified Extensible Firmware Interface (UEFI) specification for
329 * Return: status code
331 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
333 const efi_guid_t *vendor)
335 char *native_name, *variable;
336 ssize_t name_len, list_len;
338 char * const regexlist[] = {regex};
343 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, vendor);
345 if (!variable_name_size || !variable_name || !vendor)
346 return EFI_EXIT(EFI_INVALID_PARAMETER);
348 if (variable_name[0]) {
349 /* check null-terminated string */
350 for (i = 0; i < *variable_name_size; i++)
351 if (!variable_name[i])
353 if (i >= *variable_name_size)
354 return EFI_EXIT(EFI_INVALID_PARAMETER);
356 /* search for the last-returned variable */
357 ret = efi_to_native(&native_name, variable_name, vendor);
359 return EFI_EXIT(ret);
361 name_len = strlen(native_name);
362 for (variable = efi_variables_list; variable && *variable;) {
363 if (!strncmp(variable, native_name, name_len) &&
364 variable[name_len] == '=')
367 variable = strchr(variable, '\n');
373 if (!(variable && *variable))
374 return EFI_EXIT(EFI_INVALID_PARAMETER);
377 variable = strchr(variable, '\n');
380 if (!(variable && *variable))
381 return EFI_EXIT(EFI_NOT_FOUND);
384 *new search: free a list used in the previous search
386 free(efi_variables_list);
387 efi_variables_list = NULL;
388 efi_cur_variable = NULL;
390 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_.*");
391 list_len = hexport_r(&env_htab, '\n',
392 H_MATCH_REGEX | H_MATCH_KEY,
393 &efi_variables_list, 0, 1, regexlist);
394 /* 1 indicates that no match was found */
396 return EFI_EXIT(EFI_NOT_FOUND);
398 variable = efi_variables_list;
401 ret = parse_uboot_variable(variable, variable_name_size, variable_name,
402 vendor, &attributes);
404 return EFI_EXIT(ret);
408 * efi_set_variable() - set value of a UEFI variable
410 * This function implements the SetVariable runtime service.
412 * See the Unified Extensible Firmware Interface (UEFI) specification for
415 * @variable_name: name of the variable
416 * @vendor: vendor GUID
417 * @attributes: attributes of the variable
418 * @data_size: size of the buffer with the variable value
419 * @data: buffer with the variable value
420 * Return: status code
422 efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
423 const efi_guid_t *vendor, u32 attributes,
424 efi_uintn_t data_size, const void *data)
426 char *native_name = NULL, *val = NULL, *s;
429 efi_status_t ret = EFI_SUCCESS;
432 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
435 if (!variable_name || !*variable_name || !vendor ||
436 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
437 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS))) {
438 ret = EFI_INVALID_PARAMETER;
442 ret = efi_to_native(&native_name, variable_name, vendor);
446 old_val = env_get(native_name);
448 old_val = parse_attr(old_val, &attr);
450 /* check read-only first */
451 if (attr & READ_ONLY) {
452 ret = EFI_WRITE_PROTECTED;
456 if ((data_size == 0 &&
457 !(attributes & EFI_VARIABLE_APPEND_WRITE)) ||
459 /* delete the variable: */
460 env_set(native_name, NULL);
465 /* attributes won't be changed */
466 if (attr != (attributes & ~EFI_VARIABLE_APPEND_WRITE)) {
467 ret = EFI_INVALID_PARAMETER;
471 if (attributes & EFI_VARIABLE_APPEND_WRITE) {
472 if (!prefix(old_val, "(blob)")) {
473 ret = EFI_DEVICE_ERROR;
476 old_size = strlen(old_val);
481 if (data_size == 0 || !attributes ||
482 (attributes & EFI_VARIABLE_APPEND_WRITE)) {
484 * Trying to delete or to update a non-existent
494 val = malloc(old_size + 2 * data_size
495 + strlen("{ro,run,boot,nv}(blob)") + 1);
497 ret = EFI_OUT_OF_RESOURCES;
503 /* store attributes */
504 attributes &= (EFI_VARIABLE_NON_VOLATILE |
505 EFI_VARIABLE_BOOTSERVICE_ACCESS |
506 EFI_VARIABLE_RUNTIME_ACCESS);
507 s += sprintf(s, "{");
509 u32 attr = 1 << (ffs(attributes) - 1);
511 if (attr == EFI_VARIABLE_NON_VOLATILE)
512 s += sprintf(s, "nv");
513 else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS)
514 s += sprintf(s, "boot");
515 else if (attr == EFI_VARIABLE_RUNTIME_ACCESS)
516 s += sprintf(s, "run");
520 s += sprintf(s, ",");
522 s += sprintf(s, "}");
526 s += sprintf(s, old_val);
528 s += sprintf(s, "(blob)");
531 s = bin2hex(s, data, data_size);
534 EFI_PRINT("setting: %s=%s\n", native_name, val);
536 if (env_set(native_name, val))
537 ret = EFI_DEVICE_ERROR;
543 return EFI_EXIT(ret);
547 * efi_query_variable_info() - get information about EFI variables
549 * This function implements the QueryVariableInfo() runtime service.
551 * See the Unified Extensible Firmware Interface (UEFI) specification for
554 * @attributes: bitmask to select variables to be
556 * @maximum_variable_storage_size: maximum size of storage area for the
557 * selected variable types
558 * @remaining_variable_storage_size: remaining size of storage are for the
559 * selected variable types
560 * @maximum_variable_size: maximum size of a variable of the
562 * Returns: status code
564 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
566 u64 *maximum_variable_storage_size,
567 u64 *remaining_variable_storage_size,
568 u64 *maximum_variable_size)
570 return EFI_UNSUPPORTED;
574 * efi_get_variable_runtime() - runtime implementation of GetVariable()
576 * @variable_name: name of the variable
577 * @vendor: vendor GUID
578 * @attributes: attributes of the variable
579 * @data_size: size of the buffer to which the variable value is copied
580 * @data: buffer to which the variable value is copied
581 * Return: status code
583 static efi_status_t __efi_runtime EFIAPI
584 efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
585 u32 *attributes, efi_uintn_t *data_size, void *data)
587 return EFI_UNSUPPORTED;
591 * efi_get_next_variable_name_runtime() - runtime implementation of
594 * @variable_name_size: size of variable_name buffer in byte
595 * @variable_name: name of uefi variable's name in u16
596 * @vendor: vendor's guid
597 * Return: status code
599 static efi_status_t __efi_runtime EFIAPI
600 efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
601 u16 *variable_name, const efi_guid_t *vendor)
603 return EFI_UNSUPPORTED;
607 * efi_set_variable_runtime() - runtime implementation of SetVariable()
609 * @variable_name: name of the variable
610 * @vendor: vendor GUID
611 * @attributes: attributes of the variable
612 * @data_size: size of the buffer with the variable value
613 * @data: buffer with the variable value
614 * Return: status code
616 static efi_status_t __efi_runtime EFIAPI
617 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
618 u32 attributes, efi_uintn_t data_size,
621 return EFI_UNSUPPORTED;
625 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
627 void efi_variables_boot_exit_notify(void)
629 efi_runtime_services.get_variable = efi_get_variable_runtime;
630 efi_runtime_services.get_next_variable_name =
631 efi_get_next_variable_name_runtime;
632 efi_runtime_services.set_variable = efi_set_variable_runtime;
633 efi_update_table_header_crc32(&efi_runtime_services.hdr);
637 * efi_init_variables() - initialize variable services
639 * Return: status code
641 efi_status_t efi_init_variables(void)