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(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
281 const efi_guid_t *guid_p;
282 bool default_guid, guid_any, verbose;
285 /* Initialize EFI drivers */
286 ret = efi_init_obj_list();
287 if (ret != EFI_SUCCESS) {
288 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
289 ret & ~EFI_ERROR_MASK);
290 return CMD_RET_FAILURE;
296 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
297 if (!strcmp(argv[0], "-guid")) {
299 return CMD_RET_USAGE;
301 /* -a already specified */
302 if (!default_guid && guid_any)
303 return CMD_RET_USAGE;
307 if (uuid_str_to_bin(argv[0], guid.b,
308 UUID_STR_FORMAT_GUID))
309 return CMD_RET_USAGE;
310 default_guid = false;
311 } else if (!strcmp(argv[0], "-all")) {
312 /* -guid already specified */
313 if (!default_guid && !guid_any)
314 return CMD_RET_USAGE;
317 default_guid = false;
318 } else if (!strcmp(argv[0], "-n")) {
321 return CMD_RET_USAGE;
327 else if (default_guid)
328 guid_p = &efi_global_variable_guid;
330 guid_p = (const efi_guid_t *)guid.b;
332 /* enumerate and show all UEFI variables */
333 return efi_dump_var_all(argc, argv, guid_p, verbose);
337 * append_value() - encode UEFI variable's value
338 * @bufp: Buffer of encoded UEFI variable's value
339 * @sizep: Size of buffer
340 * @data: data to be encoded into the value
341 * Return: 0 on success, -1 otherwise
343 * Interpret a given data string and append it to buffer.
344 * Buffer will be realloc'ed if necessary.
346 * Currently supported formats are:
347 * =0x0123...: Hexadecimal number
348 * =H0123...: Hexadecimal-byte array
349 * ="...", =S"..." or <string>:
352 static int append_value(char **bufp, size_t *sizep, char *data)
354 char *tmp_buf = NULL, *new_buf = NULL, *value;
355 unsigned long len = 0;
357 if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
364 unsigned long hex_value;
369 if ((len & 0x1)) /* not multiple of two */
380 /* convert hex hexadecimal number */
381 if (strict_strtoul(data, 16, &hex_value) < 0)
384 tmp_buf = malloc(len);
389 tmp_data.u8 = hex_value;
390 hex_ptr = &tmp_data.u8;
391 } else if (len == 2) {
392 tmp_data.u16 = hex_value;
393 hex_ptr = &tmp_data.u16;
394 } else if (len == 4) {
395 tmp_data.u32 = hex_value;
396 hex_ptr = &tmp_data.u32;
398 tmp_data.u64 = hex_value;
399 hex_ptr = &tmp_data.u64;
401 memcpy(tmp_buf, hex_ptr, len);
404 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
407 if (len & 0x1) /* not multiple of two */
411 tmp_buf = malloc(len);
415 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
416 printf("Error: illegal hexadecimal string\n");
422 } else { /* string */
423 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
429 len = strlen(data) - 1;
430 if (data[len] != '"')
438 new_buf = realloc(*bufp, *sizep + len);
442 memcpy(new_buf + *sizep, value, len);
453 * do_env_set_efi() - set UEFI variable
455 * @cmdtp: Command table
456 * @flag: Command flag
457 * @argc: Number of arguments
458 * @argv: Argument array
459 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
461 * This function is for "env set -e" or "setenv -e" command:
462 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
463 * [-i address,size] var, or
465 * Encode values specified and set given UEFI variable.
466 * If no value is specified, delete the variable.
468 int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
470 char *var_name, *value, *ep;
475 bool default_guid, verbose, value_on_memory;
476 u16 *var_name16 = NULL, *p;
481 return CMD_RET_USAGE;
483 /* Initialize EFI drivers */
484 ret = efi_init_obj_list();
485 if (ret != EFI_SUCCESS) {
486 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
487 ret & ~EFI_ERROR_MASK);
488 return CMD_RET_FAILURE;
492 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
493 * EFI_VARIABLE_RUNTIME_ACCESS;
498 guid = efi_global_variable_guid;
501 value_on_memory = false;
502 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
503 if (!strcmp(argv[0], "-guid")) {
505 return CMD_RET_USAGE;
509 if (uuid_str_to_bin(argv[0], guid.b,
510 UUID_STR_FORMAT_GUID)) {
511 printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n");
512 return CMD_RET_FAILURE;
514 default_guid = false;
515 } else if (!strcmp(argv[0], "-bs")) {
516 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
517 } else if (!strcmp(argv[0], "-rt")) {
518 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
519 } else if (!strcmp(argv[0], "-nv")) {
520 attributes |= EFI_VARIABLE_NON_VOLATILE;
521 } else if (!strcmp(argv[0], "-at")) {
523 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
524 } else if (!strcmp(argv[0], "-a")) {
525 attributes |= EFI_VARIABLE_APPEND_WRITE;
526 } else if (!strcmp(argv[0], "-i")) {
527 /* data comes from memory */
529 return CMD_RET_USAGE;
533 addr = simple_strtoul(argv[0], &ep, 16);
535 return CMD_RET_USAGE;
537 /* 0 should be allowed for delete */
538 size = simple_strtoul(++ep, NULL, 16);
540 value_on_memory = true;
541 } else if (!strcmp(argv[0], "-v")) {
544 return CMD_RET_USAGE;
548 return CMD_RET_USAGE;
552 if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
553 !strcmp(var_name, "dbt"))
554 guid = efi_guid_image_security_database;
556 guid = efi_global_variable_guid;
560 printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
562 printf("Attributes: 0x%x\n", attributes);
567 value = map_sysmem(addr, 0);
569 for (argc--, argv++; argc > 0; argc--, argv++)
570 if (append_value(&value, &size, argv[0]) < 0) {
571 printf("## Failed to process an argument, %s\n",
573 ret = CMD_RET_FAILURE;
577 if (size && verbose) {
579 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
580 16, 1, value, size, true);
583 len = utf8_utf16_strnlen(var_name, strlen(var_name));
584 var_name16 = malloc((len + 1) * 2);
586 printf("## Out of memory\n");
587 ret = CMD_RET_FAILURE;
591 utf8_utf16_strncpy(&p, var_name, len + 1);
593 ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
596 if (ret == EFI_SUCCESS) {
597 ret = CMD_RET_SUCCESS;
603 msg = " (not found)";
605 case EFI_WRITE_PROTECTED:
606 msg = " (read only)";
608 case EFI_INVALID_PARAMETER:
609 msg = " (invalid parameter)";
611 case EFI_SECURITY_VIOLATION:
612 msg = " (validation failed)";
614 case EFI_OUT_OF_RESOURCES:
615 msg = " (out of memory)";
621 printf("## Failed to set EFI variable%s\n", msg);
622 ret = CMD_RET_FAILURE;