1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Rob Clark
11 #include <efi_loader.h>
13 #include <env_internal.h>
17 #define READ_ONLY BIT(31)
20 * Mapping between EFI variables and u-boot variables:
22 * efi_$guid_$varname = {attributes}(type)value
26 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported=
27 * "{ro,boot,run}(blob)0000000000000000"
28 * efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder=
31 * The attributes are a comma separated list of these possible
35 * + boot - boot-services access
36 * + run - runtime access
38 * NOTE: with current implementation, no variables are available after
39 * ExitBootServices, and all are persisted (if possible).
41 * If not specified, the attributes default to "{boot}".
43 * The required type is one of:
45 * + utf8 - raw utf8 string
46 * + blob - arbitrary length hex string
48 * Maybe a utf16 type would be useful to for a string value to be auto
52 #define PREFIX_LEN (strlen("efi_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx_"))
55 * efi_to_native() - convert the UEFI variable name and vendor GUID to U-Boot
58 * The U-Boot variable name is a concatenation of prefix 'efi', the hexstring
59 * encoded vendor GUID, and the UTF-8 encoded UEFI variable name separated by
60 * underscores, e.g. 'efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_BootOrder'.
62 * @native: pointer to pointer to U-Boot variable name
63 * @variable_name: UEFI variable name
64 * @vendor: vendor GUID
67 static efi_status_t efi_to_native(char **native, const u16 *variable_name,
68 const efi_guid_t *vendor)
73 len = PREFIX_LEN + utf16_utf8_strlen(variable_name) + 1;
74 *native = malloc(len);
76 return EFI_OUT_OF_RESOURCES;
79 pos += sprintf(pos, "efi_%pUl_", vendor);
80 utf16_utf8_strcpy(&pos, variable_name);
86 * prefix() - skip over prefix
88 * Skip over a prefix string.
90 * @str: string with prefix
91 * @prefix: prefix string
92 * Return: string without prefix, or NULL if prefix not found
94 static const char *prefix(const char *str, const char *prefix)
96 size_t n = strlen(prefix);
97 if (!strncmp(prefix, str, n))
103 * parse_attr() - decode attributes part of variable value
105 * Convert the string encoded attributes of a UEFI variable to a bit mask.
106 * TODO: Several attributes are not supported.
108 * @str: value of U-Boot variable
109 * @attrp: pointer to UEFI attributes
110 * Return: pointer to remainder of U-Boot variable value
112 static const char *parse_attr(const char *str, u32 *attrp)
118 *attrp = EFI_VARIABLE_BOOTSERVICE_ACCESS;
122 while (*str == sep) {
127 if ((s = prefix(str, "ro"))) {
129 } else if ((s = prefix(str, "nv"))) {
130 attr |= EFI_VARIABLE_NON_VOLATILE;
131 } else if ((s = prefix(str, "boot"))) {
132 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
133 } else if ((s = prefix(str, "run"))) {
134 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
136 printf("invalid attribute: %s\n", str);
152 * efi_get_variable() - retrieve value of a UEFI variable
154 * This function implements the GetVariable runtime service.
156 * See the Unified Extensible Firmware Interface (UEFI) specification for
159 * @variable_name: name of the variable
160 * @vendor: vendor GUID
161 * @attributes: attributes of the variable
162 * @data_size: size of the buffer to which the variable value is copied
163 * @data: buffer to which the variable value is copied
164 * Return: status code
166 efi_status_t EFIAPI efi_get_variable(u16 *variable_name,
167 const efi_guid_t *vendor, u32 *attributes,
168 efi_uintn_t *data_size, void *data)
172 unsigned long in_size;
176 EFI_ENTRY("\"%ls\" %pUl %p %p %p", variable_name, vendor, attributes,
179 if (!variable_name || !vendor || !data_size)
180 return EFI_EXIT(EFI_INVALID_PARAMETER);
182 ret = efi_to_native(&native_name, variable_name, vendor);
184 return EFI_EXIT(ret);
186 EFI_PRINT("get '%s'\n", native_name);
188 val = env_get(native_name);
191 return EFI_EXIT(EFI_NOT_FOUND);
193 val = parse_attr(val, &attr);
195 in_size = *data_size;
197 if ((s = prefix(val, "(blob)"))) {
198 size_t len = strlen(s);
200 /* number of hexadecimal digits must be even */
202 return EFI_EXIT(EFI_DEVICE_ERROR);
204 /* two characters per byte: */
209 ret = EFI_BUFFER_TOO_SMALL;
214 return EFI_EXIT(EFI_INVALID_PARAMETER);
216 if (hex2bin(data, s, len))
217 return EFI_EXIT(EFI_DEVICE_ERROR);
219 EFI_PRINT("got value: \"%s\"\n", s);
220 } else if ((s = prefix(val, "(utf8)"))) {
221 unsigned len = strlen(s) + 1;
226 ret = EFI_BUFFER_TOO_SMALL;
231 return EFI_EXIT(EFI_INVALID_PARAMETER);
233 memcpy(data, s, len);
234 ((char *)data)[len] = '\0';
236 EFI_PRINT("got value: \"%s\"\n", (char *)data);
238 EFI_PRINT("invalid value: '%s'\n", val);
239 return EFI_EXIT(EFI_DEVICE_ERROR);
244 *attributes = attr & EFI_VARIABLE_MASK;
246 return EFI_EXIT(ret);
249 static char *efi_variables_list;
250 static char *efi_cur_variable;
253 * parse_uboot_variable() - parse a u-boot variable and get uefi-related
255 * @variable: whole data of u-boot variable (ie. name=value)
256 * @variable_name_size: size of variable_name buffer in byte
257 * @variable_name: name of uefi variable in u16, null-terminated
258 * @vendor: vendor's guid
259 * @attributes: attributes
261 * A uefi variable is encoded into a u-boot variable as described above.
262 * This function parses such a u-boot variable and retrieve uefi-related
263 * information into respective parameters. In return, variable_name_size
264 * is the size of variable name including NULL.
266 * Return: EFI_SUCCESS if parsing is OK, EFI_NOT_FOUND when
267 * the entire variable list has been returned,
268 * otherwise non-zero status code
270 static efi_status_t parse_uboot_variable(char *variable,
271 efi_uintn_t *variable_name_size,
273 const efi_guid_t *vendor,
276 char *guid, *name, *end, c;
277 unsigned long name_len;
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 if (*variable_name_size < (name_len + 1)) {
294 *variable_name_size = name_len + 1;
295 return EFI_BUFFER_TOO_SMALL;
297 end++; /* point to value */
301 utf8_utf16_strncpy(&p, name, name_len);
302 variable_name[name_len] = 0;
303 *variable_name_size = name_len + 1;
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;
427 efi_status_t ret = EFI_SUCCESS;
430 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
433 /* TODO: implement APPEND_WRITE */
434 if (!variable_name || !*variable_name || !vendor ||
435 ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
436 !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)) ||
437 (attributes & EFI_VARIABLE_APPEND_WRITE)) {
438 ret = EFI_INVALID_PARAMETER;
442 ret = efi_to_native(&native_name, variable_name, vendor);
446 #define ACCESS_ATTR (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
448 if ((data_size == 0) || !(attributes & ACCESS_ATTR)) {
449 /* delete the variable: */
450 env_set(native_name, NULL);
455 val = env_get(native_name);
457 parse_attr(val, &attr);
459 /* We should not free val */
461 if (attr & READ_ONLY) {
462 ret = EFI_WRITE_PROTECTED;
467 * attributes won't be changed
468 * TODO: take care of APPEND_WRITE once supported
470 if (attr != attributes) {
471 ret = EFI_INVALID_PARAMETER;
476 val = malloc(2 * data_size + strlen("{ro,run,boot,nv}(blob)") + 1);
478 ret = EFI_OUT_OF_RESOURCES;
486 * TODO: several attributes are not supported
488 attributes &= (EFI_VARIABLE_NON_VOLATILE |
489 EFI_VARIABLE_BOOTSERVICE_ACCESS |
490 EFI_VARIABLE_RUNTIME_ACCESS);
491 s += sprintf(s, "{");
493 u32 attr = 1 << (ffs(attributes) - 1);
495 if (attr == EFI_VARIABLE_NON_VOLATILE)
496 s += sprintf(s, "nv");
497 else if (attr == EFI_VARIABLE_BOOTSERVICE_ACCESS)
498 s += sprintf(s, "boot");
499 else if (attr == EFI_VARIABLE_RUNTIME_ACCESS)
500 s += sprintf(s, "run");
504 s += sprintf(s, ",");
506 s += sprintf(s, "}");
509 s += sprintf(s, "(blob)");
510 s = bin2hex(s, data, data_size);
513 EFI_PRINT("setting: %s=%s\n", native_name, val);
515 if (env_set(native_name, val))
516 ret = EFI_DEVICE_ERROR;
522 return EFI_EXIT(ret);
526 * efi_query_variable_info() - get information about EFI variables
528 * This function implements the QueryVariableInfo() runtime service.
530 * See the Unified Extensible Firmware Interface (UEFI) specification for
533 * @attributes: bitmask to select variables to be
535 * @maximum_variable_storage_size: maximum size of storage area for the
536 * selected variable types
537 * @remaining_variable_storage_size: remaining size of storage are for the
538 * selected variable types
539 * @maximum_variable_size: maximum size of a variable of the
541 * Returns: status code
543 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
545 u64 *maximum_variable_storage_size,
546 u64 *remaining_variable_storage_size,
547 u64 *maximum_variable_size)
549 return EFI_UNSUPPORTED;
553 * efi_get_variable_runtime() - runtime implementation of GetVariable()
555 * @variable_name: name of the variable
556 * @vendor: vendor GUID
557 * @attributes: attributes of the variable
558 * @data_size: size of the buffer to which the variable value is copied
559 * @data: buffer to which the variable value is copied
560 * Return: status code
562 static efi_status_t __efi_runtime EFIAPI
563 efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
564 u32 *attributes, efi_uintn_t *data_size, void *data)
566 return EFI_UNSUPPORTED;
570 * efi_get_next_variable_name_runtime() - runtime implementation of
573 * @variable_name_size: size of variable_name buffer in byte
574 * @variable_name: name of uefi variable's name in u16
575 * @vendor: vendor's guid
576 * Return: status code
578 static efi_status_t __efi_runtime EFIAPI
579 efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
580 u16 *variable_name, const efi_guid_t *vendor)
582 return EFI_UNSUPPORTED;
586 * efi_set_variable_runtime() - runtime implementation of SetVariable()
588 * @variable_name: name of the variable
589 * @vendor: vendor GUID
590 * @attributes: attributes of the variable
591 * @data_size: size of the buffer with the variable value
592 * @data: buffer with the variable value
593 * Return: status code
595 static efi_status_t __efi_runtime EFIAPI
596 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *vendor,
597 u32 attributes, efi_uintn_t data_size,
600 return EFI_UNSUPPORTED;
604 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
606 void efi_variables_boot_exit_notify(void)
608 efi_runtime_services.get_variable = efi_get_variable_runtime;
609 efi_runtime_services.get_next_variable_name =
610 efi_get_next_variable_name_runtime;
611 efi_runtime_services.set_variable = efi_set_variable_runtime;
612 efi_update_table_header_crc32(&efi_runtime_services.hdr);
616 * efi_init_variables() - initialize variable services
618 * Return: status code
620 efi_status_t efi_init_variables(void)