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>
18 #include <linux/kernel.h>
21 * From efi_variable.c,
23 * Mapping between UEFI variables and u-boot variables:
25 * efi_$guid_$varname = {attributes}(type)value
32 {EFI_VARIABLE_NON_VOLATILE, "NV"},
33 {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"},
34 {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
35 {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
36 {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
43 /* signature database */
44 {EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"},
45 {EFI_IMAGE_SECURITY_DATABASE_GUID, "EFI_IMAGE_SECURITY_DATABASE_GUID"},
46 /* certificate type */
47 {EFI_CERT_SHA256_GUID, "EFI_CERT_SHA256_GUID"},
48 {EFI_CERT_X509_GUID, "EFI_CERT_X509_GUID"},
49 {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"},
52 /* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
53 static char unknown_guid[37];
56 * efi_guid_to_str() - convert guid to readable name
59 * Return: string for GUID
61 * convert guid to readable name
63 static const char *efi_guid_to_str(const efi_guid_t *guid)
67 for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++)
68 if (!guidcmp(guid, &efi_guid_text[i].guid))
69 return efi_guid_text[i].text;
71 uuid_bin_to_str((unsigned char *)guid->b, unknown_guid,
72 UUID_STR_FORMAT_GUID);
78 * efi_dump_single_var() - show information about a UEFI variable
80 * @name: Name of the variable
82 * @verbose: if true, dump data
84 * Show information encoded in one UEFI variable
86 static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
96 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data));
97 if (ret == EFI_BUFFER_TOO_SMALL) {
102 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size,
105 if (ret == EFI_NOT_FOUND) {
106 printf("Error: \"%ls\" not defined\n", name);
109 if (ret != EFI_SUCCESS)
112 printf("%ls:\n %s:", name, efi_guid_to_str(guid));
113 for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
114 if (attributes & efi_var_attrs[i].mask) {
120 puts(efi_var_attrs[i].text);
122 printf(", DataSize = 0x%zx\n", size);
124 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
132 * efi_dump_vars() - show information about named UEFI variables
134 * @argc: Number of arguments (variables)
135 * @argv: Argument (variable name) array
136 * @verbose: if true, dump data
137 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
139 * Show information encoded in named UEFI variables
141 static int efi_dump_vars(int argc, char *const argv[],
142 const efi_guid_t *guid, bool verbose)
145 efi_uintn_t buf_size, size;
148 var_name16 = malloc(buf_size);
150 return CMD_RET_FAILURE;
152 for (; argc > 0; argc--, argv++) {
153 size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
154 if (buf_size < size) {
156 p = realloc(var_name16, buf_size);
159 return CMD_RET_FAILURE;
165 utf8_utf16_strcpy(&p, argv[0]);
167 efi_dump_single_var(var_name16, guid, verbose);
172 return CMD_RET_SUCCESS;
175 static bool match_name(int argc, char *const argv[], u16 *var_name16)
182 buflen = utf16_utf8_strlen(var_name16) + 1;
183 buf = calloc(1, buflen);
188 utf16_utf8_strcpy(&p, var_name16);
190 for (i = 0; i < argc; argc--, argv++) {
191 if (!strcmp(buf, argv[i])) {
204 * efi_dump_var_all() - show information about all the UEFI variables
206 * @argc: Number of arguments (variables)
207 * @argv: Argument (variable name) array
208 * @verbose: if true, dump data
209 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
211 * Show information encoded in all the UEFI variables
213 static int efi_dump_var_all(int argc, char *const argv[],
214 const efi_guid_t *guid_p, bool verbose)
217 efi_uintn_t buf_size, size;
222 /* simplified case */
223 return efi_dump_vars(argc, argv, guid_p, verbose);
226 var_name16 = malloc(buf_size);
228 return CMD_RET_FAILURE;
233 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
235 if (ret == EFI_NOT_FOUND)
237 if (ret == EFI_BUFFER_TOO_SMALL) {
239 p = realloc(var_name16, buf_size);
242 return CMD_RET_FAILURE;
245 ret = EFI_CALL(efi_get_next_variable_name(&size,
249 if (ret != EFI_SUCCESS) {
251 return CMD_RET_FAILURE;
254 if ((!guid_p || !guidcmp(guid_p, &guid)) &&
255 (!argc || match_name(argc, argv, var_name16)))
256 efi_dump_single_var(var_name16, &guid, verbose);
261 return CMD_RET_SUCCESS;
265 * do_env_print_efi() - show information about UEFI variables
267 * @cmdtp: Command table
268 * @flag: Command flag
269 * @argc: Number of arguments
270 * @argv: Argument array
271 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
273 * This function is for "env print -e" or "printenv -e" command:
274 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
275 * If one or more variable names are specified, show information
276 * named UEFI variables, otherwise show all the UEFI variables.
278 int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
282 const efi_guid_t *guid_p;
283 bool default_guid, guid_any, verbose;
286 /* Initialize EFI drivers */
287 ret = efi_init_obj_list();
288 if (ret != EFI_SUCCESS) {
289 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
290 ret & ~EFI_ERROR_MASK);
291 return CMD_RET_FAILURE;
297 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
298 if (!strcmp(argv[0], "-guid")) {
300 return CMD_RET_USAGE;
302 /* -a already specified */
303 if (!default_guid && guid_any)
304 return CMD_RET_USAGE;
308 if (uuid_str_to_bin(argv[0], guid.b,
309 UUID_STR_FORMAT_GUID))
310 return CMD_RET_USAGE;
311 default_guid = false;
312 } else if (!strcmp(argv[0], "-all")) {
313 /* -guid already specified */
314 if (!default_guid && !guid_any)
315 return CMD_RET_USAGE;
318 default_guid = false;
319 } else if (!strcmp(argv[0], "-n")) {
322 return CMD_RET_USAGE;
328 else if (default_guid)
329 guid_p = &efi_global_variable_guid;
331 guid_p = (const efi_guid_t *)guid.b;
333 /* enumerate and show all UEFI variables */
334 return efi_dump_var_all(argc, argv, guid_p, verbose);
338 * append_value() - encode UEFI variable's value
339 * @bufp: Buffer of encoded UEFI variable's value
340 * @sizep: Size of buffer
341 * @data: data to be encoded into the value
342 * Return: 0 on success, -1 otherwise
344 * Interpret a given data string and append it to buffer.
345 * Buffer will be realloc'ed if necessary.
347 * Currently supported formats are:
348 * =0x0123...: Hexadecimal number
349 * =H0123...: Hexadecimal-byte array
350 * ="...", =S"..." or <string>:
353 static int append_value(char **bufp, size_t *sizep, char *data)
355 char *tmp_buf = NULL, *new_buf = NULL, *value;
356 unsigned long len = 0;
358 if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
365 unsigned long hex_value;
370 if ((len & 0x1)) /* not multiple of two */
381 /* convert hex hexadecimal number */
382 if (strict_strtoul(data, 16, &hex_value) < 0)
385 tmp_buf = malloc(len);
390 tmp_data.u8 = hex_value;
391 hex_ptr = &tmp_data.u8;
392 } else if (len == 2) {
393 tmp_data.u16 = hex_value;
394 hex_ptr = &tmp_data.u16;
395 } else if (len == 4) {
396 tmp_data.u32 = hex_value;
397 hex_ptr = &tmp_data.u32;
399 tmp_data.u64 = hex_value;
400 hex_ptr = &tmp_data.u64;
402 memcpy(tmp_buf, hex_ptr, len);
405 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
408 if (len & 0x1) /* not multiple of two */
412 tmp_buf = malloc(len);
416 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
417 printf("Error: illegal hexadecimal string\n");
423 } else { /* string */
424 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
430 len = strlen(data) - 1;
431 if (data[len] != '"')
439 new_buf = realloc(*bufp, *sizep + len);
443 memcpy(new_buf + *sizep, value, len);
454 * do_env_set_efi() - set UEFI variable
456 * @cmdtp: Command table
457 * @flag: Command flag
458 * @argc: Number of arguments
459 * @argv: Argument array
460 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
462 * This function is for "env set -e" or "setenv -e" command:
463 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
464 * [-i address,size] var, or
466 * Encode values specified and set given UEFI variable.
467 * If no value is specified, delete the variable.
469 int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
472 char *var_name, *value, *ep;
477 bool default_guid, verbose, value_on_memory;
478 u16 *var_name16 = NULL, *p;
483 return CMD_RET_USAGE;
485 /* Initialize EFI drivers */
486 ret = efi_init_obj_list();
487 if (ret != EFI_SUCCESS) {
488 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
489 ret & ~EFI_ERROR_MASK);
490 return CMD_RET_FAILURE;
494 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
495 * EFI_VARIABLE_RUNTIME_ACCESS;
500 guid = efi_global_variable_guid;
503 value_on_memory = false;
504 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
505 if (!strcmp(argv[0], "-guid")) {
507 return CMD_RET_USAGE;
511 if (uuid_str_to_bin(argv[0], guid.b,
512 UUID_STR_FORMAT_GUID)) {
513 printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n");
514 return CMD_RET_FAILURE;
516 default_guid = false;
517 } else if (!strcmp(argv[0], "-bs")) {
518 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
519 } else if (!strcmp(argv[0], "-rt")) {
520 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
521 } else if (!strcmp(argv[0], "-nv")) {
522 attributes |= EFI_VARIABLE_NON_VOLATILE;
523 } else if (!strcmp(argv[0], "-at")) {
525 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
526 } else if (!strcmp(argv[0], "-a")) {
527 attributes |= EFI_VARIABLE_APPEND_WRITE;
528 } else if (!strcmp(argv[0], "-i")) {
529 /* data comes from memory */
531 return CMD_RET_USAGE;
535 addr = simple_strtoul(argv[0], &ep, 16);
537 return CMD_RET_USAGE;
539 /* 0 should be allowed for delete */
540 size = simple_strtoul(++ep, NULL, 16);
542 value_on_memory = true;
543 } else if (!strcmp(argv[0], "-v")) {
546 return CMD_RET_USAGE;
550 return CMD_RET_USAGE;
554 if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
555 !strcmp(var_name, "dbt"))
556 guid = efi_guid_image_security_database;
558 guid = efi_global_variable_guid;
562 printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
564 printf("Attributes: 0x%x\n", attributes);
569 value = map_sysmem(addr, 0);
571 for (argc--, argv++; argc > 0; argc--, argv++)
572 if (append_value(&value, &size, argv[0]) < 0) {
573 printf("## Failed to process an argument, %s\n",
575 ret = CMD_RET_FAILURE;
579 if (size && verbose) {
581 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
582 16, 1, value, size, true);
585 len = utf8_utf16_strnlen(var_name, strlen(var_name));
586 var_name16 = malloc((len + 1) * 2);
588 printf("## Out of memory\n");
589 ret = CMD_RET_FAILURE;
593 utf8_utf16_strncpy(&p, var_name, len + 1);
595 ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
598 if (ret == EFI_SUCCESS) {
599 ret = CMD_RET_SUCCESS;
605 msg = " (not found)";
607 case EFI_WRITE_PROTECTED:
608 msg = " (read only)";
610 case EFI_INVALID_PARAMETER:
611 msg = " (invalid parameter)";
613 case EFI_SECURITY_VIOLATION:
614 msg = " (validation failed)";
616 case EFI_OUT_OF_RESOURCES:
617 msg = " (out of memory)";
623 printf("## Failed to set EFI variable%s\n", msg);
624 ret = CMD_RET_FAILURE;