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>
12 #include <efi_variable.h>
20 #include <linux/kernel.h>
23 * From efi_variable.c,
25 * Mapping between UEFI variables and u-boot variables:
27 * efi_$guid_$varname = {attributes}(type)value
34 {EFI_VARIABLE_NON_VOLATILE, "NV"},
35 {EFI_VARIABLE_BOOTSERVICE_ACCESS, "BS"},
36 {EFI_VARIABLE_RUNTIME_ACCESS, "RT"},
37 {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS, "AW"},
38 {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
39 {EFI_VARIABLE_READ_ONLY, "RO"},
46 /* signature database */
47 {EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"},
48 {EFI_IMAGE_SECURITY_DATABASE_GUID, "EFI_IMAGE_SECURITY_DATABASE_GUID"},
49 /* certificate type */
50 {EFI_CERT_SHA256_GUID, "EFI_CERT_SHA256_GUID"},
51 {EFI_CERT_X509_GUID, "EFI_CERT_X509_GUID"},
52 {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"},
55 /* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
56 static char unknown_guid[37];
59 * efi_guid_to_str() - convert guid to readable name
62 * Return: string for GUID
64 * convert guid to readable name
66 static const char *efi_guid_to_str(const efi_guid_t *guid)
70 for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++)
71 if (!guidcmp(guid, &efi_guid_text[i].guid))
72 return efi_guid_text[i].text;
74 uuid_bin_to_str((unsigned char *)guid->b, unknown_guid,
75 UUID_STR_FORMAT_GUID);
81 * efi_dump_single_var() - show information about a UEFI variable
83 * @name: Name of the variable
85 * @verbose: if true, dump data
87 * Show information encoded in one UEFI variable
89 static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
101 ret = efi_get_variable_int(name, guid, &attributes, &size, data, &time);
102 if (ret == EFI_BUFFER_TOO_SMALL) {
107 ret = efi_get_variable_int(name, guid, &attributes, &size,
110 if (ret == EFI_NOT_FOUND) {
111 printf("Error: \"%ls\" not defined\n", name);
114 if (ret != EFI_SUCCESS)
117 rtc_to_tm(time, &tm);
118 printf("%ls:\n %s:\n", name, efi_guid_to_str(guid));
119 if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
120 printf(" %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year,
121 tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
123 for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
124 if (attributes & efi_var_attrs[i].mask) {
128 puts(efi_var_attrs[i].text);
130 printf(", DataSize = 0x%zx\n", size);
132 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
140 * efi_dump_vars() - show information about named UEFI variables
142 * @argc: Number of arguments (variables)
143 * @argv: Argument (variable name) array
144 * @verbose: if true, dump data
145 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
147 * Show information encoded in named UEFI variables
149 static int efi_dump_vars(int argc, char *const argv[],
150 const efi_guid_t *guid, bool verbose)
153 efi_uintn_t buf_size, size;
156 var_name16 = malloc(buf_size);
158 return CMD_RET_FAILURE;
160 for (; argc > 0; argc--, argv++) {
161 size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
162 if (buf_size < size) {
164 p = realloc(var_name16, buf_size);
167 return CMD_RET_FAILURE;
173 utf8_utf16_strcpy(&p, argv[0]);
175 efi_dump_single_var(var_name16, guid, verbose);
180 return CMD_RET_SUCCESS;
183 static bool match_name(int argc, char *const argv[], u16 *var_name16)
190 buflen = utf16_utf8_strlen(var_name16) + 1;
191 buf = calloc(1, buflen);
196 utf16_utf8_strcpy(&p, var_name16);
198 for (i = 0; i < argc; argc--, argv++) {
199 if (!strcmp(buf, argv[i])) {
212 * efi_dump_var_all() - show information about all the UEFI variables
214 * @argc: Number of arguments (variables)
215 * @argv: Argument (variable name) array
216 * @verbose: if true, dump data
217 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
219 * Show information encoded in all the UEFI variables
221 static int efi_dump_var_all(int argc, char *const argv[],
222 const efi_guid_t *guid_p, bool verbose)
225 efi_uintn_t buf_size, size;
230 /* simplified case */
231 return efi_dump_vars(argc, argv, guid_p, verbose);
234 var_name16 = malloc(buf_size);
236 return CMD_RET_FAILURE;
241 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
243 if (ret == EFI_NOT_FOUND)
245 if (ret == EFI_BUFFER_TOO_SMALL) {
247 p = realloc(var_name16, buf_size);
250 return CMD_RET_FAILURE;
253 ret = EFI_CALL(efi_get_next_variable_name(&size,
257 if (ret != EFI_SUCCESS) {
259 return CMD_RET_FAILURE;
262 if ((!guid_p || !guidcmp(guid_p, &guid)) &&
263 (!argc || match_name(argc, argv, var_name16)))
264 efi_dump_single_var(var_name16, &guid, verbose);
269 return CMD_RET_SUCCESS;
273 * do_env_print_efi() - show information about UEFI variables
275 * @cmdtp: Command table
276 * @flag: Command flag
277 * @argc: Number of arguments
278 * @argv: Argument array
279 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
281 * This function is for "env print -e" or "printenv -e" command:
282 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
283 * If one or more variable names are specified, show information
284 * named UEFI variables, otherwise show all the UEFI variables.
286 int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
290 const efi_guid_t *guid_p;
291 bool default_guid, guid_any, verbose;
294 /* Initialize EFI drivers */
295 ret = efi_init_obj_list();
296 if (ret != EFI_SUCCESS) {
297 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
298 ret & ~EFI_ERROR_MASK);
299 return CMD_RET_FAILURE;
305 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
306 if (!strcmp(argv[0], "-guid")) {
308 return CMD_RET_USAGE;
310 /* -a already specified */
311 if (!default_guid && guid_any)
312 return CMD_RET_USAGE;
316 if (uuid_str_to_bin(argv[0], guid.b,
317 UUID_STR_FORMAT_GUID))
318 return CMD_RET_USAGE;
319 default_guid = false;
320 } else if (!strcmp(argv[0], "-all")) {
321 /* -guid already specified */
322 if (!default_guid && !guid_any)
323 return CMD_RET_USAGE;
326 default_guid = false;
327 } else if (!strcmp(argv[0], "-n")) {
330 return CMD_RET_USAGE;
336 else if (default_guid)
337 guid_p = &efi_global_variable_guid;
339 guid_p = (const efi_guid_t *)guid.b;
341 /* enumerate and show all UEFI variables */
342 return efi_dump_var_all(argc, argv, guid_p, verbose);
346 * append_value() - encode UEFI variable's value
347 * @bufp: Buffer of encoded UEFI variable's value
348 * @sizep: Size of buffer
349 * @data: data to be encoded into the value
350 * Return: 0 on success, -1 otherwise
352 * Interpret a given data string and append it to buffer.
353 * Buffer will be realloc'ed if necessary.
355 * Currently supported formats are:
356 * =0x0123...: Hexadecimal number
357 * =H0123...: Hexadecimal-byte array
358 * ="...", =S"..." or <string>:
361 static int append_value(char **bufp, size_t *sizep, char *data)
363 char *tmp_buf = NULL, *new_buf = NULL, *value;
364 unsigned long len = 0;
366 if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
373 unsigned long hex_value;
378 if ((len & 0x1)) /* not multiple of two */
389 /* convert hex hexadecimal number */
390 if (strict_strtoul(data, 16, &hex_value) < 0)
393 tmp_buf = malloc(len);
398 tmp_data.u8 = hex_value;
399 hex_ptr = &tmp_data.u8;
400 } else if (len == 2) {
401 tmp_data.u16 = hex_value;
402 hex_ptr = &tmp_data.u16;
403 } else if (len == 4) {
404 tmp_data.u32 = hex_value;
405 hex_ptr = &tmp_data.u32;
407 tmp_data.u64 = hex_value;
408 hex_ptr = &tmp_data.u64;
410 memcpy(tmp_buf, hex_ptr, len);
413 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
416 if (len & 0x1) /* not multiple of two */
420 tmp_buf = malloc(len);
424 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
425 printf("Error: illegal hexadecimal string\n");
431 } else { /* string */
432 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
438 len = strlen(data) - 1;
439 if (data[len] != '"')
447 new_buf = realloc(*bufp, *sizep + len);
451 memcpy(new_buf + *sizep, value, len);
462 * do_env_set_efi() - set UEFI variable
464 * @cmdtp: Command table
465 * @flag: Command flag
466 * @argc: Number of arguments
467 * @argv: Argument array
468 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
470 * This function is for "env set -e" or "setenv -e" command:
471 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
472 * [-i address,size] var, or
474 * Encode values specified and set given UEFI variable.
475 * If no value is specified, delete the variable.
477 int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
480 char *var_name, *value, *ep;
485 bool default_guid, verbose, value_on_memory;
486 u16 *var_name16 = NULL, *p;
491 return CMD_RET_USAGE;
493 /* Initialize EFI drivers */
494 ret = efi_init_obj_list();
495 if (ret != EFI_SUCCESS) {
496 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
497 ret & ~EFI_ERROR_MASK);
498 return CMD_RET_FAILURE;
502 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
503 * EFI_VARIABLE_RUNTIME_ACCESS;
508 guid = efi_global_variable_guid;
511 value_on_memory = false;
512 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
513 if (!strcmp(argv[0], "-guid")) {
515 return CMD_RET_USAGE;
519 if (uuid_str_to_bin(argv[0], guid.b,
520 UUID_STR_FORMAT_GUID)) {
521 printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n");
522 return CMD_RET_FAILURE;
524 default_guid = false;
525 } else if (!strcmp(argv[0], "-bs")) {
526 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
527 } else if (!strcmp(argv[0], "-rt")) {
528 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
529 } else if (!strcmp(argv[0], "-nv")) {
530 attributes |= EFI_VARIABLE_NON_VOLATILE;
531 } else if (!strcmp(argv[0], "-at")) {
533 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
534 } else if (!strcmp(argv[0], "-a")) {
535 attributes |= EFI_VARIABLE_APPEND_WRITE;
536 } else if (!strcmp(argv[0], "-i")) {
537 /* data comes from memory */
539 return CMD_RET_USAGE;
543 addr = simple_strtoul(argv[0], &ep, 16);
545 return CMD_RET_USAGE;
547 /* 0 should be allowed for delete */
548 size = simple_strtoul(++ep, NULL, 16);
550 value_on_memory = true;
551 } else if (!strcmp(argv[0], "-v")) {
554 return CMD_RET_USAGE;
558 return CMD_RET_USAGE;
562 if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
563 !strcmp(var_name, "dbt"))
564 guid = efi_guid_image_security_database;
566 guid = efi_global_variable_guid;
570 printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
572 printf("Attributes: 0x%x\n", attributes);
577 value = map_sysmem(addr, 0);
579 for (argc--, argv++; argc > 0; argc--, argv++)
580 if (append_value(&value, &size, argv[0]) < 0) {
581 printf("## Failed to process an argument, %s\n",
583 ret = CMD_RET_FAILURE;
587 if (size && verbose) {
589 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
590 16, 1, value, size, true);
593 len = utf8_utf16_strnlen(var_name, strlen(var_name));
594 var_name16 = malloc((len + 1) * 2);
596 printf("## Out of memory\n");
597 ret = CMD_RET_FAILURE;
601 utf8_utf16_strncpy(&p, var_name, len + 1);
603 ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
606 if (ret == EFI_SUCCESS) {
607 ret = CMD_RET_SUCCESS;
613 msg = " (not found)";
615 case EFI_WRITE_PROTECTED:
616 msg = " (read only)";
618 case EFI_INVALID_PARAMETER:
619 msg = " (invalid parameter)";
621 case EFI_SECURITY_VIOLATION:
622 msg = " (validation failed)";
624 case EFI_OUT_OF_RESOURCES:
625 msg = " (out of memory)";
631 printf("## Failed to set EFI variable%s\n", msg);
632 ret = CMD_RET_FAILURE;