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 static const char unknown_guid[] = "";
58 * efi_guid_to_str() - convert guid to readable name
61 * Return: string for GUID
63 * convert guid to readable name
65 static const char *efi_guid_to_str(const efi_guid_t *guid)
69 for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++)
70 if (!guidcmp(guid, &efi_guid_text[i].guid))
71 return efi_guid_text[i].text;
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)
97 ret = efi_get_variable_int(name, guid, &attributes, &size, data, &time);
98 if (ret == EFI_BUFFER_TOO_SMALL) {
103 ret = efi_get_variable_int(name, guid, &attributes, &size,
106 if (ret == EFI_NOT_FOUND) {
107 printf("Error: \"%ls\" not defined\n", name);
110 if (ret != EFI_SUCCESS)
113 rtc_to_tm(time, &tm);
114 printf("%ls:\n %pUl %s\n", name, guid, efi_guid_to_str(guid));
115 if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
116 printf(" %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year,
117 tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
119 for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
120 if (attributes & efi_var_attrs[i].mask) {
124 puts(efi_var_attrs[i].text);
126 printf(", DataSize = 0x%zx\n", size);
128 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
135 static bool match_name(int argc, char *const argv[], u16 *var_name16)
142 buflen = utf16_utf8_strlen(var_name16) + 1;
143 buf = calloc(1, buflen);
148 utf16_utf8_strcpy(&p, var_name16);
150 for (i = 0; i < argc; argc--, argv++) {
151 if (!strcmp(buf, argv[i])) {
164 * efi_dump_var_all() - show information about all the UEFI variables
166 * @argc: Number of arguments (variables)
167 * @argv: Argument (variable name) array
168 * @verbose: if true, dump data
169 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
171 * Show information encoded in all the UEFI variables
173 static int efi_dump_var_all(int argc, char *const argv[],
174 const efi_guid_t *guid_p, bool verbose)
177 efi_uintn_t buf_size, size;
183 var_name16 = malloc(buf_size);
185 return CMD_RET_FAILURE;
190 ret = efi_get_next_variable_name_int(&size, var_name16,
192 if (ret == EFI_NOT_FOUND)
194 if (ret == EFI_BUFFER_TOO_SMALL) {
196 p = realloc(var_name16, buf_size);
199 return CMD_RET_FAILURE;
202 ret = efi_get_next_variable_name_int(&size, var_name16,
205 if (ret != EFI_SUCCESS) {
207 return CMD_RET_FAILURE;
210 if (guid_p && guidcmp(guid_p, &guid))
212 if (!argc || match_name(argc, argv, var_name16)) {
214 efi_dump_single_var(var_name16, &guid, verbose);
219 if (!match && argc == 1)
220 printf("Error: \"%s\" not defined\n", argv[0]);
222 return CMD_RET_SUCCESS;
226 * do_env_print_efi() - show information about UEFI variables
228 * @cmdtp: Command table
229 * @flag: Command flag
230 * @argc: Number of arguments
231 * @argv: Argument array
232 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
234 * This function is for "env print -e" or "printenv -e" command:
235 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
236 * If one or more variable names are specified, show information
237 * named UEFI variables, otherwise show all the UEFI variables.
239 int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
242 const efi_guid_t *guid_p = NULL;
247 /* Initialize EFI drivers */
248 ret = efi_init_obj_list();
249 if (ret != EFI_SUCCESS) {
250 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
251 ret & ~EFI_ERROR_MASK);
252 return CMD_RET_FAILURE;
255 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
256 if (!strcmp(argv[0], "-guid")) {
258 return CMD_RET_USAGE;
261 if (uuid_str_to_bin(argv[0], guid.b,
262 UUID_STR_FORMAT_GUID))
263 return CMD_RET_USAGE;
264 guid_p = (const efi_guid_t *)guid.b;
265 } else if (!strcmp(argv[0], "-n")) {
268 return CMD_RET_USAGE;
272 /* enumerate and show all UEFI variables */
273 return efi_dump_var_all(argc, argv, guid_p, verbose);
277 * append_value() - encode UEFI variable's value
278 * @bufp: Buffer of encoded UEFI variable's value
279 * @sizep: Size of buffer
280 * @data: data to be encoded into the value
281 * Return: 0 on success, -1 otherwise
283 * Interpret a given data string and append it to buffer.
284 * Buffer will be realloc'ed if necessary.
286 * Currently supported formats are:
287 * =0x0123...: Hexadecimal number
288 * =H0123...: Hexadecimal-byte array
289 * ="...", =S"..." or <string>:
292 static int append_value(char **bufp, size_t *sizep, char *data)
294 char *tmp_buf = NULL, *new_buf = NULL, *value;
295 unsigned long len = 0;
297 if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
304 unsigned long hex_value;
309 if ((len & 0x1)) /* not multiple of two */
320 /* convert hex hexadecimal number */
321 if (strict_strtoul(data, 16, &hex_value) < 0)
324 tmp_buf = malloc(len);
329 tmp_data.u8 = hex_value;
330 hex_ptr = &tmp_data.u8;
331 } else if (len == 2) {
332 tmp_data.u16 = hex_value;
333 hex_ptr = &tmp_data.u16;
334 } else if (len == 4) {
335 tmp_data.u32 = hex_value;
336 hex_ptr = &tmp_data.u32;
338 tmp_data.u64 = hex_value;
339 hex_ptr = &tmp_data.u64;
341 memcpy(tmp_buf, hex_ptr, len);
344 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
347 if (len & 0x1) /* not multiple of two */
351 tmp_buf = malloc(len);
355 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
356 printf("Error: illegal hexadecimal string\n");
362 } else { /* string */
363 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
369 len = strlen(data) - 1;
370 if (data[len] != '"')
378 new_buf = realloc(*bufp, *sizep + len);
382 memcpy(new_buf + *sizep, value, len);
393 * do_env_set_efi() - set UEFI variable
395 * @cmdtp: Command table
396 * @flag: Command flag
397 * @argc: Number of arguments
398 * @argv: Argument array
399 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
401 * This function is for "env set -e" or "setenv -e" command:
402 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
403 * [-i address,size] var, or
405 * Encode values specified and set given UEFI variable.
406 * If no value is specified, delete the variable.
408 int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
411 char *var_name, *value, *ep;
416 bool default_guid, verbose, value_on_memory;
417 u16 *var_name16 = NULL, *p;
422 return CMD_RET_USAGE;
424 /* Initialize EFI drivers */
425 ret = efi_init_obj_list();
426 if (ret != EFI_SUCCESS) {
427 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
428 ret & ~EFI_ERROR_MASK);
429 return CMD_RET_FAILURE;
433 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
434 * EFI_VARIABLE_RUNTIME_ACCESS;
439 guid = efi_global_variable_guid;
442 value_on_memory = false;
443 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
444 if (!strcmp(argv[0], "-guid")) {
446 return CMD_RET_USAGE;
450 if (uuid_str_to_bin(argv[0], guid.b,
451 UUID_STR_FORMAT_GUID)) {
452 return CMD_RET_USAGE;
454 default_guid = false;
455 } else if (!strcmp(argv[0], "-bs")) {
456 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
457 } else if (!strcmp(argv[0], "-rt")) {
458 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
459 } else if (!strcmp(argv[0], "-nv")) {
460 attributes |= EFI_VARIABLE_NON_VOLATILE;
461 } else if (!strcmp(argv[0], "-at")) {
463 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
464 } else if (!strcmp(argv[0], "-a")) {
465 attributes |= EFI_VARIABLE_APPEND_WRITE;
466 } else if (!strcmp(argv[0], "-i")) {
467 /* data comes from memory */
469 return CMD_RET_USAGE;
473 addr = hextoul(argv[0], &ep);
475 return CMD_RET_USAGE;
477 /* 0 should be allowed for delete */
478 size = hextoul(++ep, NULL);
480 value_on_memory = true;
481 } else if (!strcmp(argv[0], "-v")) {
484 return CMD_RET_USAGE;
488 return CMD_RET_USAGE;
492 if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
493 !strcmp(var_name, "dbt"))
494 guid = efi_guid_image_security_database;
496 guid = efi_global_variable_guid;
500 printf("GUID: %pUl %s\n", &guid,
501 efi_guid_to_str((const efi_guid_t *)&guid));
502 printf("Attributes: 0x%x\n", attributes);
507 value = map_sysmem(addr, 0);
509 for (argc--, argv++; argc > 0; argc--, argv++)
510 if (append_value(&value, &size, argv[0]) < 0) {
511 printf("## Failed to process an argument, %s\n",
513 ret = CMD_RET_FAILURE;
517 if (size && verbose) {
519 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
520 16, 1, value, size, true);
523 len = utf8_utf16_strnlen(var_name, strlen(var_name));
524 var_name16 = malloc((len + 1) * 2);
526 printf("## Out of memory\n");
527 ret = CMD_RET_FAILURE;
531 utf8_utf16_strncpy(&p, var_name, len + 1);
533 ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
536 if (ret == EFI_SUCCESS) {
537 ret = CMD_RET_SUCCESS;
543 msg = " (not found)";
545 case EFI_WRITE_PROTECTED:
546 msg = " (read only)";
548 case EFI_INVALID_PARAMETER:
549 msg = " (invalid parameter)";
551 case EFI_SECURITY_VIOLATION:
552 msg = " (validation failed)";
554 case EFI_OUT_OF_RESOURCES:
555 msg = " (out of memory)";
561 printf("## Failed to set EFI variable%s\n", msg);
562 ret = CMD_RET_FAILURE;