1 // SPDX-License-Identifier: GPL-2.0+
3 * Integrate UEFI variables to u-boot env interface
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
11 #include <efi_loader.h>
17 #include <linux/kernel.h>
20 * From efi_variable.c,
22 * Mapping between UEFI variables and u-boot variables:
24 * efi_$guid_$varname = {attributes}(type)value
31 {EFI_VARIABLE_NON_VOLATILE, "NV"},
32 {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"},
33 {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
34 {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
35 {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
42 /* signature database */
43 {EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"},
46 /* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
47 static char unknown_guid[37];
50 * efi_guid_to_str() - convert guid to readable name
53 * Return: string for GUID
55 * convert guid to readable name
57 static const char *efi_guid_to_str(const efi_guid_t *guid)
61 for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++)
62 if (!guidcmp(guid, &efi_guid_text[i].guid))
63 return efi_guid_text[i].text;
65 uuid_bin_to_str((unsigned char *)guid->b, unknown_guid,
66 UUID_STR_FORMAT_GUID);
72 * efi_dump_single_var() - show information about a UEFI variable
74 * @name: Name of the variable
76 * @verbose: if true, dump data
78 * Show information encoded in one UEFI variable
80 static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
90 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data));
91 if (ret == EFI_BUFFER_TOO_SMALL) {
96 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size,
99 if (ret == EFI_NOT_FOUND) {
100 printf("Error: \"%ls\" not defined\n", name);
103 if (ret != EFI_SUCCESS)
106 printf("%ls:\n %s:", name, efi_guid_to_str(guid));
107 for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
108 if (attributes & efi_var_attrs[i].mask) {
114 puts(efi_var_attrs[i].text);
116 printf(", DataSize = 0x%zx\n", size);
118 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
126 * efi_dump_vars() - show information about named UEFI variables
128 * @argc: Number of arguments (variables)
129 * @argv: Argument (variable name) array
130 * @verbose: if true, dump data
131 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
133 * Show information encoded in named UEFI variables
135 static int efi_dump_vars(int argc, char * const argv[],
136 const efi_guid_t *guid, bool verbose)
139 efi_uintn_t buf_size, size;
142 var_name16 = malloc(buf_size);
144 return CMD_RET_FAILURE;
146 for (; argc > 0; argc--, argv++) {
147 size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
148 if (buf_size < size) {
150 p = realloc(var_name16, buf_size);
153 return CMD_RET_FAILURE;
159 utf8_utf16_strcpy(&p, argv[0]);
161 efi_dump_single_var(var_name16, guid, verbose);
166 return CMD_RET_SUCCESS;
169 static bool match_name(int argc, char * const argv[], u16 *var_name16)
176 buflen = utf16_utf8_strlen(var_name16) + 1;
177 buf = calloc(1, buflen);
182 utf16_utf8_strcpy(&p, var_name16);
184 for (i = 0; i < argc; argc--, argv++) {
185 if (!strcmp(buf, argv[i])) {
198 * efi_dump_var_all() - show information about all the UEFI variables
200 * @argc: Number of arguments (variables)
201 * @argv: Argument (variable name) array
202 * @verbose: if true, dump data
203 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
205 * Show information encoded in all the UEFI variables
207 static int efi_dump_var_all(int argc, char * const argv[],
208 const efi_guid_t *guid_p, bool verbose)
211 efi_uintn_t buf_size, size;
216 /* simplified case */
217 return efi_dump_vars(argc, argv, guid_p, verbose);
220 var_name16 = malloc(buf_size);
222 return CMD_RET_FAILURE;
227 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
229 if (ret == EFI_NOT_FOUND)
231 if (ret == EFI_BUFFER_TOO_SMALL) {
233 p = realloc(var_name16, buf_size);
236 return CMD_RET_FAILURE;
239 ret = EFI_CALL(efi_get_next_variable_name(&size,
243 if (ret != EFI_SUCCESS) {
245 return CMD_RET_FAILURE;
248 if ((!guid_p || !guidcmp(guid_p, &guid)) &&
249 (!argc || match_name(argc, argv, var_name16)))
250 efi_dump_single_var(var_name16, &guid, verbose);
255 return CMD_RET_SUCCESS;
259 * do_env_print_efi() - show information about UEFI variables
261 * @cmdtp: Command table
262 * @flag: Command flag
263 * @argc: Number of arguments
264 * @argv: Argument array
265 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
267 * This function is for "env print -e" or "printenv -e" command:
268 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
269 * If one or more variable names are specified, show information
270 * named UEFI variables, otherwise show all the UEFI variables.
272 int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
275 const efi_guid_t *guid_p;
276 bool default_guid, guid_any, verbose;
279 /* Initialize EFI drivers */
280 ret = efi_init_obj_list();
281 if (ret != EFI_SUCCESS) {
282 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
283 ret & ~EFI_ERROR_MASK);
284 return CMD_RET_FAILURE;
290 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
291 if (!strcmp(argv[0], "-guid")) {
293 return CMD_RET_USAGE;
295 /* -a already specified */
296 if (!default_guid & guid_any)
297 return CMD_RET_USAGE;
301 if (uuid_str_to_bin(argv[0], guid.b,
302 UUID_STR_FORMAT_GUID))
303 return CMD_RET_USAGE;
304 default_guid = false;
305 } else if (!strcmp(argv[0], "-all")) {
306 /* -guid already specified */
307 if (!default_guid && !guid_any)
308 return CMD_RET_USAGE;
311 default_guid = false;
312 } else if (!strcmp(argv[0], "-n")) {
315 return CMD_RET_USAGE;
321 else if (default_guid)
322 guid_p = &efi_global_variable_guid;
324 guid_p = (const efi_guid_t *)guid.b;
326 /* enumerate and show all UEFI variables */
327 return efi_dump_var_all(argc, argv, guid_p, verbose);
331 * append_value() - encode UEFI variable's value
332 * @bufp: Buffer of encoded UEFI variable's value
333 * @sizep: Size of buffer
334 * @data: data to be encoded into the value
335 * Return: 0 on success, -1 otherwise
337 * Interpret a given data string and append it to buffer.
338 * Buffer will be realloc'ed if necessary.
340 * Currently supported formats are:
341 * =0x0123...: Hexadecimal number
342 * =H0123...: Hexadecimal-byte array
343 * ="...", =S"..." or <string>:
346 static int append_value(char **bufp, size_t *sizep, char *data)
348 char *tmp_buf = NULL, *new_buf = NULL, *value;
349 unsigned long len = 0;
351 if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
358 unsigned long hex_value;
363 if ((len & 0x1)) /* not multiple of two */
374 /* convert hex hexadecimal number */
375 if (strict_strtoul(data, 16, &hex_value) < 0)
378 tmp_buf = malloc(len);
383 tmp_data.u8 = hex_value;
384 hex_ptr = &tmp_data.u8;
385 } else if (len == 2) {
386 tmp_data.u16 = hex_value;
387 hex_ptr = &tmp_data.u16;
388 } else if (len == 4) {
389 tmp_data.u32 = hex_value;
390 hex_ptr = &tmp_data.u32;
392 tmp_data.u64 = hex_value;
393 hex_ptr = &tmp_data.u64;
395 memcpy(tmp_buf, hex_ptr, len);
398 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
401 if (len & 0x1) /* not multiple of two */
405 tmp_buf = malloc(len);
409 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
410 printf("Error: illegal hexadecimal string\n");
416 } else { /* string */
417 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
423 len = strlen(data) - 1;
424 if (data[len] != '"')
432 new_buf = realloc(*bufp, *sizep + len);
436 memcpy(new_buf + *sizep, value, len);
447 * do_env_set_efi() - set UEFI variable
449 * @cmdtp: Command table
450 * @flag: Command flag
451 * @argc: Number of arguments
452 * @argv: Argument array
453 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
455 * This function is for "env set -e" or "setenv -e" command:
456 * => env set -e [-guid guid][-nv][-bs][-rt][-a][-v]
457 * [-i address,size] var, or
459 * Encode values specified and set given UEFI variable.
460 * If no value is specified, delete the variable.
462 int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
464 char *var_name, *value, *ep;
469 bool default_guid, verbose, value_on_memory;
470 u16 *var_name16 = NULL, *p;
475 return CMD_RET_USAGE;
477 /* Initialize EFI drivers */
478 ret = efi_init_obj_list();
479 if (ret != EFI_SUCCESS) {
480 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
481 ret & ~EFI_ERROR_MASK);
482 return CMD_RET_FAILURE;
486 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
487 * EFI_VARIABLE_RUNTIME_ACCESS;
492 guid = efi_global_variable_guid;
495 value_on_memory = false;
496 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
497 if (!strcmp(argv[0], "-guid")) {
499 return CMD_RET_USAGE;
503 if (uuid_str_to_bin(argv[0], guid.b,
504 UUID_STR_FORMAT_GUID)) {
505 printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n");
506 return CMD_RET_FAILURE;
508 default_guid = false;
509 } else if (!strcmp(argv[0], "-bs")) {
510 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
511 } else if (!strcmp(argv[0], "-rt")) {
512 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
513 } else if (!strcmp(argv[0], "-nv")) {
514 attributes |= EFI_VARIABLE_NON_VOLATILE;
515 } else if (!strcmp(argv[0], "-a")) {
516 attributes |= EFI_VARIABLE_APPEND_WRITE;
517 } else if (!strcmp(argv[0], "-i")) {
518 /* data comes from memory */
520 return CMD_RET_USAGE;
524 addr = simple_strtoul(argv[0], &ep, 16);
526 return CMD_RET_USAGE;
528 size = simple_strtoul(++ep, NULL, 16);
530 return CMD_RET_FAILURE;
531 value_on_memory = true;
532 } else if (!strcmp(argv[0], "-v")) {
535 return CMD_RET_USAGE;
539 return CMD_RET_USAGE;
543 guid = efi_global_variable_guid;
546 printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
548 printf("Attributes: 0x%x\n", attributes);
553 value = map_sysmem(addr, 0);
555 for (argc--, argv++; argc > 0; argc--, argv++)
556 if (append_value(&value, &size, argv[0]) < 0) {
557 printf("## Failed to process an argument, %s\n",
559 ret = CMD_RET_FAILURE;
563 if (size && verbose) {
565 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
566 16, 1, value, size, true);
569 len = utf8_utf16_strnlen(var_name, strlen(var_name));
570 var_name16 = malloc((len + 1) * 2);
572 printf("## Out of memory\n");
573 ret = CMD_RET_FAILURE;
577 utf8_utf16_strncpy(&p, var_name, len + 1);
579 ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
582 if (ret == EFI_SUCCESS) {
583 ret = CMD_RET_SUCCESS;
589 msg = " (not found)";
591 case EFI_WRITE_PROTECTED:
592 msg = " (read only)";
594 case EFI_INVALID_PARAMETER:
595 msg = " (invalid parameter)";
597 case EFI_SECURITY_VIOLATION:
598 msg = " (validation failed)";
600 case EFI_OUT_OF_RESOURCES:
601 msg = " (out of memory)";
607 printf("## Failed to set EFI variable%s\n", msg);
608 ret = CMD_RET_FAILURE;