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"},
43 * efi_dump_single_var() - show information about a UEFI variable
45 * @name: Name of the variable
47 * @verbose: if true, dump data
49 * Show information encoded in one UEFI variable
51 static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
63 ret = efi_get_variable_int(name, guid, &attributes, &size, data, &time);
64 if (ret == EFI_BUFFER_TOO_SMALL) {
69 ret = efi_get_variable_int(name, guid, &attributes, &size,
72 if (ret == EFI_NOT_FOUND) {
73 printf("Error: \"%ls\" not defined\n", name);
76 if (ret != EFI_SUCCESS)
80 printf("%ls:\n %pUl (%pUs)\n", name, guid, guid);
81 if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
82 printf(" %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year,
83 tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
85 for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
86 if (attributes & efi_var_attrs[i].mask) {
90 puts(efi_var_attrs[i].text);
92 printf(", DataSize = 0x%zx\n", size);
94 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
101 static bool match_name(int argc, char *const argv[], u16 *var_name16)
108 buflen = utf16_utf8_strlen(var_name16) + 1;
109 buf = calloc(1, buflen);
114 utf16_utf8_strcpy(&p, var_name16);
116 for (i = 0; i < argc; argc--, argv++) {
117 if (!strcmp(buf, argv[i])) {
130 * efi_dump_var_all() - show information about all the UEFI variables
132 * @argc: Number of arguments (variables)
133 * @argv: Argument (variable name) array
134 * @verbose: if true, dump data
135 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
137 * Show information encoded in all the UEFI variables
139 static int efi_dump_var_all(int argc, char *const argv[],
140 const efi_guid_t *guid_p, bool verbose)
143 efi_uintn_t buf_size, size;
149 var_name16 = malloc(buf_size);
151 return CMD_RET_FAILURE;
156 ret = efi_get_next_variable_name_int(&size, var_name16,
158 if (ret == EFI_NOT_FOUND)
160 if (ret == EFI_BUFFER_TOO_SMALL) {
162 p = realloc(var_name16, buf_size);
165 return CMD_RET_FAILURE;
168 ret = efi_get_next_variable_name_int(&size, var_name16,
171 if (ret != EFI_SUCCESS) {
173 return CMD_RET_FAILURE;
176 if (guid_p && guidcmp(guid_p, &guid))
178 if (!argc || match_name(argc, argv, var_name16)) {
180 efi_dump_single_var(var_name16, &guid, verbose);
185 if (!match && argc == 1)
186 printf("Error: \"%s\" not defined\n", argv[0]);
188 return CMD_RET_SUCCESS;
192 * do_env_print_efi() - show information about UEFI variables
194 * @cmdtp: Command table
195 * @flag: Command flag
196 * @argc: Number of arguments
197 * @argv: Argument array
198 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
200 * This function is for "env print -e" or "printenv -e" command:
201 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
202 * If one or more variable names are specified, show information
203 * named UEFI variables, otherwise show all the UEFI variables.
205 int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
208 const efi_guid_t *guid_p = NULL;
213 /* Initialize EFI drivers */
214 ret = efi_init_obj_list();
215 if (ret != EFI_SUCCESS) {
216 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
217 ret & ~EFI_ERROR_MASK);
218 return CMD_RET_FAILURE;
221 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
222 if (!strcmp(argv[0], "-guid")) {
224 return CMD_RET_USAGE;
227 if (uuid_str_to_bin(argv[0], guid.b,
228 UUID_STR_FORMAT_GUID))
229 return CMD_RET_USAGE;
230 guid_p = (const efi_guid_t *)guid.b;
231 } else if (!strcmp(argv[0], "-n")) {
234 return CMD_RET_USAGE;
238 /* enumerate and show all UEFI variables */
239 return efi_dump_var_all(argc, argv, guid_p, verbose);
243 * append_value() - encode UEFI variable's value
244 * @bufp: Buffer of encoded UEFI variable's value
245 * @sizep: Size of buffer
246 * @data: data to be encoded into the value
247 * Return: 0 on success, -1 otherwise
249 * Interpret a given data string and append it to buffer.
250 * Buffer will be realloc'ed if necessary.
252 * Currently supported formats are:
253 * =0x0123...: Hexadecimal number
254 * =H0123...: Hexadecimal-byte array
255 * ="...", =S"..." or <string>:
258 static int append_value(char **bufp, size_t *sizep, char *data)
260 char *tmp_buf = NULL, *new_buf = NULL, *value;
261 unsigned long len = 0;
263 if (!strncmp(data, "=0x", 2)) { /* hexadecimal number */
270 unsigned long hex_value;
275 if ((len & 0x1)) /* not multiple of two */
286 /* convert hex hexadecimal number */
287 if (strict_strtoul(data, 16, &hex_value) < 0)
290 tmp_buf = malloc(len);
295 tmp_data.u8 = hex_value;
296 hex_ptr = &tmp_data.u8;
297 } else if (len == 2) {
298 tmp_data.u16 = hex_value;
299 hex_ptr = &tmp_data.u16;
300 } else if (len == 4) {
301 tmp_data.u32 = hex_value;
302 hex_ptr = &tmp_data.u32;
304 tmp_data.u64 = hex_value;
305 hex_ptr = &tmp_data.u64;
307 memcpy(tmp_buf, hex_ptr, len);
310 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
313 if (len & 0x1) /* not multiple of two */
317 tmp_buf = malloc(len);
321 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
322 printf("Error: illegal hexadecimal string\n");
328 } else { /* string */
329 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
335 len = strlen(data) - 1;
336 if (data[len] != '"')
344 new_buf = realloc(*bufp, *sizep + len);
348 memcpy(new_buf + *sizep, value, len);
359 * do_env_set_efi() - set UEFI variable
361 * @cmdtp: Command table
362 * @flag: Command flag
363 * @argc: Number of arguments
364 * @argv: Argument array
365 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
367 * This function is for "env set -e" or "setenv -e" command:
368 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
369 * [-i address,size] var, or
371 * Encode values specified and set given UEFI variable.
372 * If no value is specified, delete the variable.
374 int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
377 char *var_name, *value, *ep;
382 bool default_guid, verbose, value_on_memory;
383 u16 *var_name16 = NULL, *p;
388 return CMD_RET_USAGE;
390 /* Initialize EFI drivers */
391 ret = efi_init_obj_list();
392 if (ret != EFI_SUCCESS) {
393 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
394 ret & ~EFI_ERROR_MASK);
395 return CMD_RET_FAILURE;
399 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
400 * EFI_VARIABLE_RUNTIME_ACCESS;
405 guid = efi_global_variable_guid;
408 value_on_memory = false;
409 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
410 if (!strcmp(argv[0], "-guid")) {
412 return CMD_RET_USAGE;
416 if (uuid_str_to_bin(argv[0], guid.b,
417 UUID_STR_FORMAT_GUID)) {
418 return CMD_RET_USAGE;
420 default_guid = false;
421 } else if (!strcmp(argv[0], "-bs")) {
422 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
423 } else if (!strcmp(argv[0], "-rt")) {
424 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
425 } else if (!strcmp(argv[0], "-nv")) {
426 attributes |= EFI_VARIABLE_NON_VOLATILE;
427 } else if (!strcmp(argv[0], "-at")) {
429 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
430 } else if (!strcmp(argv[0], "-a")) {
431 attributes |= EFI_VARIABLE_APPEND_WRITE;
432 } else if (!strcmp(argv[0], "-i")) {
433 /* data comes from memory */
435 return CMD_RET_USAGE;
439 addr = hextoul(argv[0], &ep);
441 return CMD_RET_USAGE;
443 /* 0 should be allowed for delete */
444 size = hextoul(++ep, NULL);
446 value_on_memory = true;
447 } else if (!strcmp(argv[0], "-v")) {
450 return CMD_RET_USAGE;
454 return CMD_RET_USAGE;
458 if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
459 !strcmp(var_name, "dbt"))
460 guid = efi_guid_image_security_database;
462 guid = efi_global_variable_guid;
466 printf("GUID: %pUl (%pUs)\n", &guid, &guid);
467 printf("Attributes: 0x%x\n", attributes);
472 value = map_sysmem(addr, 0);
474 for (argc--, argv++; argc > 0; argc--, argv++)
475 if (append_value(&value, &size, argv[0]) < 0) {
476 printf("## Failed to process an argument, %s\n",
478 ret = CMD_RET_FAILURE;
482 if (size && verbose) {
484 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
485 16, 1, value, size, true);
488 len = utf8_utf16_strnlen(var_name, strlen(var_name));
489 var_name16 = malloc((len + 1) * 2);
491 printf("## Out of memory\n");
492 ret = CMD_RET_FAILURE;
496 utf8_utf16_strncpy(&p, var_name, len + 1);
498 ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
501 if (ret == EFI_SUCCESS) {
502 ret = CMD_RET_SUCCESS;
508 msg = " (not found)";
510 case EFI_WRITE_PROTECTED:
511 msg = " (read only)";
513 case EFI_INVALID_PARAMETER:
514 msg = " (invalid parameter)";
516 case EFI_SECURITY_VIOLATION:
517 msg = " (validation failed)";
519 case EFI_OUT_OF_RESOURCES:
520 msg = " (out of memory)";
526 printf("## Failed to set EFI variable%s\n", msg);
527 ret = CMD_RET_FAILURE;