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"},
44 {EFI_IMAGE_SECURITY_DATABASE_GUID, "EFI_IMAGE_SECURITY_DATABASE_GUID"},
45 /* certificate type */
46 {EFI_CERT_SHA256_GUID, "EFI_CERT_SHA256_GUID"},
47 {EFI_CERT_X509_GUID, "EFI_CERT_X509_GUID"},
48 {EFI_CERT_TYPE_PKCS7_GUID, "EFI_CERT_TYPE_PKCS7_GUID"},
51 /* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
52 static char unknown_guid[37];
55 * efi_guid_to_str() - convert guid to readable name
58 * Return: string for GUID
60 * convert guid to readable name
62 static const char *efi_guid_to_str(const efi_guid_t *guid)
66 for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++)
67 if (!guidcmp(guid, &efi_guid_text[i].guid))
68 return efi_guid_text[i].text;
70 uuid_bin_to_str((unsigned char *)guid->b, unknown_guid,
71 UUID_STR_FORMAT_GUID);
77 * efi_dump_single_var() - show information about a UEFI variable
79 * @name: Name of the variable
81 * @verbose: if true, dump data
83 * Show information encoded in one UEFI variable
85 static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
95 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size, data));
96 if (ret == EFI_BUFFER_TOO_SMALL) {
101 ret = EFI_CALL(efi_get_variable(name, guid, &attributes, &size,
104 if (ret == EFI_NOT_FOUND) {
105 printf("Error: \"%ls\" not defined\n", name);
108 if (ret != EFI_SUCCESS)
111 printf("%ls:\n %s:", name, efi_guid_to_str(guid));
112 for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
113 if (attributes & efi_var_attrs[i].mask) {
119 puts(efi_var_attrs[i].text);
121 printf(", DataSize = 0x%zx\n", size);
123 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
131 * efi_dump_vars() - show information about named UEFI variables
133 * @argc: Number of arguments (variables)
134 * @argv: Argument (variable name) array
135 * @verbose: if true, dump data
136 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
138 * Show information encoded in named UEFI variables
140 static int efi_dump_vars(int argc, char * const argv[],
141 const efi_guid_t *guid, bool verbose)
144 efi_uintn_t buf_size, size;
147 var_name16 = malloc(buf_size);
149 return CMD_RET_FAILURE;
151 for (; argc > 0; argc--, argv++) {
152 size = (utf8_utf16_strlen(argv[0]) + 1) * sizeof(u16);
153 if (buf_size < size) {
155 p = realloc(var_name16, buf_size);
158 return CMD_RET_FAILURE;
164 utf8_utf16_strcpy(&p, argv[0]);
166 efi_dump_single_var(var_name16, guid, verbose);
171 return CMD_RET_SUCCESS;
174 static bool match_name(int argc, char * const argv[], u16 *var_name16)
181 buflen = utf16_utf8_strlen(var_name16) + 1;
182 buf = calloc(1, buflen);
187 utf16_utf8_strcpy(&p, var_name16);
189 for (i = 0; i < argc; argc--, argv++) {
190 if (!strcmp(buf, argv[i])) {
203 * efi_dump_var_all() - show information about all the UEFI variables
205 * @argc: Number of arguments (variables)
206 * @argv: Argument (variable name) array
207 * @verbose: if true, dump data
208 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
210 * Show information encoded in all the UEFI variables
212 static int efi_dump_var_all(int argc, char * const argv[],
213 const efi_guid_t *guid_p, bool verbose)
216 efi_uintn_t buf_size, size;
221 /* simplified case */
222 return efi_dump_vars(argc, argv, guid_p, verbose);
225 var_name16 = malloc(buf_size);
227 return CMD_RET_FAILURE;
232 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
234 if (ret == EFI_NOT_FOUND)
236 if (ret == EFI_BUFFER_TOO_SMALL) {
238 p = realloc(var_name16, buf_size);
241 return CMD_RET_FAILURE;
244 ret = EFI_CALL(efi_get_next_variable_name(&size,
248 if (ret != EFI_SUCCESS) {
250 return CMD_RET_FAILURE;
253 if ((!guid_p || !guidcmp(guid_p, &guid)) &&
254 (!argc || match_name(argc, argv, var_name16)))
255 efi_dump_single_var(var_name16, &guid, verbose);
260 return CMD_RET_SUCCESS;
264 * do_env_print_efi() - show information about UEFI variables
266 * @cmdtp: Command table
267 * @flag: Command flag
268 * @argc: Number of arguments
269 * @argv: Argument array
270 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
272 * This function is for "env print -e" or "printenv -e" command:
273 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
274 * If one or more variable names are specified, show information
275 * named UEFI variables, otherwise show all the UEFI variables.
277 int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
280 const efi_guid_t *guid_p;
281 bool default_guid, guid_any, verbose;
284 /* Initialize EFI drivers */
285 ret = efi_init_obj_list();
286 if (ret != EFI_SUCCESS) {
287 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
288 ret & ~EFI_ERROR_MASK);
289 return CMD_RET_FAILURE;
295 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
296 if (!strcmp(argv[0], "-guid")) {
298 return CMD_RET_USAGE;
300 /* -a already specified */
301 if (!default_guid && guid_any)
302 return CMD_RET_USAGE;
306 if (uuid_str_to_bin(argv[0], guid.b,
307 UUID_STR_FORMAT_GUID))
308 return CMD_RET_USAGE;
309 default_guid = false;
310 } else if (!strcmp(argv[0], "-all")) {
311 /* -guid already specified */
312 if (!default_guid && !guid_any)
313 return CMD_RET_USAGE;
316 default_guid = false;
317 } else if (!strcmp(argv[0], "-n")) {
320 return CMD_RET_USAGE;
326 else if (default_guid)
327 guid_p = &efi_global_variable_guid;
329 guid_p = (const efi_guid_t *)guid.b;
331 /* enumerate and show all UEFI variables */
332 return efi_dump_var_all(argc, argv, guid_p, verbose);
336 * append_value() - encode UEFI variable's value
337 * @bufp: Buffer of encoded UEFI variable's value
338 * @sizep: Size of buffer
339 * @data: data to be encoded into the value
340 * Return: 0 on success, -1 otherwise
342 * Interpret a given data string and append it to buffer.
343 * Buffer will be realloc'ed if necessary.
345 * Currently supported formats are:
346 * =0x0123...: Hexadecimal number
347 * =H0123...: Hexadecimal-byte array
348 * ="...", =S"..." or <string>:
351 static int append_value(char **bufp, size_t *sizep, char *data)
353 char *tmp_buf = NULL, *new_buf = NULL, *value;
354 unsigned long len = 0;
356 if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
363 unsigned long hex_value;
368 if ((len & 0x1)) /* not multiple of two */
379 /* convert hex hexadecimal number */
380 if (strict_strtoul(data, 16, &hex_value) < 0)
383 tmp_buf = malloc(len);
388 tmp_data.u8 = hex_value;
389 hex_ptr = &tmp_data.u8;
390 } else if (len == 2) {
391 tmp_data.u16 = hex_value;
392 hex_ptr = &tmp_data.u16;
393 } else if (len == 4) {
394 tmp_data.u32 = hex_value;
395 hex_ptr = &tmp_data.u32;
397 tmp_data.u64 = hex_value;
398 hex_ptr = &tmp_data.u64;
400 memcpy(tmp_buf, hex_ptr, len);
403 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
406 if (len & 0x1) /* not multiple of two */
410 tmp_buf = malloc(len);
414 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
415 printf("Error: illegal hexadecimal string\n");
421 } else { /* string */
422 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
428 len = strlen(data) - 1;
429 if (data[len] != '"')
437 new_buf = realloc(*bufp, *sizep + len);
441 memcpy(new_buf + *sizep, value, len);
452 * do_env_set_efi() - set UEFI variable
454 * @cmdtp: Command table
455 * @flag: Command flag
456 * @argc: Number of arguments
457 * @argv: Argument array
458 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
460 * This function is for "env set -e" or "setenv -e" command:
461 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
462 * [-i address,size] var, or
464 * Encode values specified and set given UEFI variable.
465 * If no value is specified, delete the variable.
467 int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
469 char *var_name, *value, *ep;
474 bool default_guid, verbose, value_on_memory;
475 u16 *var_name16 = NULL, *p;
480 return CMD_RET_USAGE;
482 /* Initialize EFI drivers */
483 ret = efi_init_obj_list();
484 if (ret != EFI_SUCCESS) {
485 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
486 ret & ~EFI_ERROR_MASK);
487 return CMD_RET_FAILURE;
491 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
492 * EFI_VARIABLE_RUNTIME_ACCESS;
497 guid = efi_global_variable_guid;
500 value_on_memory = false;
501 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
502 if (!strcmp(argv[0], "-guid")) {
504 return CMD_RET_USAGE;
508 if (uuid_str_to_bin(argv[0], guid.b,
509 UUID_STR_FORMAT_GUID)) {
510 printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n");
511 return CMD_RET_FAILURE;
513 default_guid = false;
514 } else if (!strcmp(argv[0], "-bs")) {
515 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
516 } else if (!strcmp(argv[0], "-rt")) {
517 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
518 } else if (!strcmp(argv[0], "-nv")) {
519 attributes |= EFI_VARIABLE_NON_VOLATILE;
520 } else if (!strcmp(argv[0], "-at")) {
522 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
523 } else if (!strcmp(argv[0], "-a")) {
524 attributes |= EFI_VARIABLE_APPEND_WRITE;
525 } else if (!strcmp(argv[0], "-i")) {
526 /* data comes from memory */
528 return CMD_RET_USAGE;
532 addr = simple_strtoul(argv[0], &ep, 16);
534 return CMD_RET_USAGE;
536 /* 0 should be allowed for delete */
537 size = simple_strtoul(++ep, NULL, 16);
539 value_on_memory = true;
540 } else if (!strcmp(argv[0], "-v")) {
543 return CMD_RET_USAGE;
547 return CMD_RET_USAGE;
551 if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
552 !strcmp(var_name, "dbt"))
553 guid = efi_guid_image_security_database;
555 guid = efi_global_variable_guid;
559 printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
561 printf("Attributes: 0x%x\n", attributes);
566 value = map_sysmem(addr, 0);
568 for (argc--, argv++; argc > 0; argc--, argv++)
569 if (append_value(&value, &size, argv[0]) < 0) {
570 printf("## Failed to process an argument, %s\n",
572 ret = CMD_RET_FAILURE;
576 if (size && verbose) {
578 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
579 16, 1, value, size, true);
582 len = utf8_utf16_strnlen(var_name, strlen(var_name));
583 var_name16 = malloc((len + 1) * 2);
585 printf("## Out of memory\n");
586 ret = CMD_RET_FAILURE;
590 utf8_utf16_strncpy(&p, var_name, len + 1);
592 ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
595 if (ret == EFI_SUCCESS) {
596 ret = CMD_RET_SUCCESS;
602 msg = " (not found)";
604 case EFI_WRITE_PROTECTED:
605 msg = " (read only)";
607 case EFI_INVALID_PARAMETER:
608 msg = " (invalid parameter)";
610 case EFI_SECURITY_VIOLATION:
611 msg = " (validation failed)";
613 case EFI_OUT_OF_RESOURCES:
614 msg = " (out of memory)";
620 printf("## Failed to set EFI variable%s\n", msg);
621 ret = CMD_RET_FAILURE;