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]);
187 return CMD_RET_FAILURE;
190 return CMD_RET_SUCCESS;
194 * do_env_print_efi() - show information about UEFI variables
196 * @cmdtp: Command table
197 * @flag: Command flag
198 * @argc: Number of arguments
199 * @argv: Argument array
200 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
202 * This function is for "env print -e" or "printenv -e" command:
203 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
204 * If one or more variable names are specified, show information
205 * named UEFI variables, otherwise show all the UEFI variables.
207 int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
210 const efi_guid_t *guid_p = NULL;
215 /* Initialize EFI drivers */
216 ret = efi_init_obj_list();
217 if (ret != EFI_SUCCESS) {
218 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
219 ret & ~EFI_ERROR_MASK);
220 return CMD_RET_FAILURE;
223 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
224 if (!strcmp(argv[0], "-guid")) {
226 return CMD_RET_USAGE;
229 if (uuid_str_to_bin(argv[0], guid.b,
230 UUID_STR_FORMAT_GUID))
231 return CMD_RET_USAGE;
232 guid_p = (const efi_guid_t *)guid.b;
233 } else if (!strcmp(argv[0], "-n")) {
236 return CMD_RET_USAGE;
240 /* enumerate and show all UEFI variables */
241 return efi_dump_var_all(argc, argv, guid_p, verbose);
245 * append_value() - encode UEFI variable's value
246 * @bufp: Buffer of encoded UEFI variable's value
247 * @sizep: Size of buffer
248 * @data: data to be encoded into the value
249 * Return: 0 on success, -1 otherwise
251 * Interpret a given data string and append it to buffer.
252 * Buffer will be realloc'ed if necessary.
254 * Currently supported formats are:
255 * =0x0123...: Hexadecimal number
256 * =H0123...: Hexadecimal-byte array
257 * ="...", =S"..." or <string>:
260 static int append_value(char **bufp, size_t *sizep, char *data)
262 char *tmp_buf = NULL, *new_buf = NULL, *value;
263 unsigned long len = 0;
265 if (!strncmp(data, "=0x", 3)) { /* hexadecimal number */
272 unsigned long hex_value;
277 if ((len & 0x1)) /* not multiple of two */
288 /* convert hex hexadecimal number */
289 if (strict_strtoul(data, 16, &hex_value) < 0)
292 tmp_buf = malloc(len);
297 tmp_data.u8 = hex_value;
298 hex_ptr = &tmp_data.u8;
299 } else if (len == 2) {
300 tmp_data.u16 = hex_value;
301 hex_ptr = &tmp_data.u16;
302 } else if (len == 4) {
303 tmp_data.u32 = hex_value;
304 hex_ptr = &tmp_data.u32;
306 tmp_data.u64 = hex_value;
307 hex_ptr = &tmp_data.u64;
309 memcpy(tmp_buf, hex_ptr, len);
312 } else if (!strncmp(data, "=H", 2)) { /* hexadecimal-byte array */
315 if (len & 0x1) /* not multiple of two */
319 tmp_buf = malloc(len);
323 if (hex2bin((u8 *)tmp_buf, data, len) < 0) {
324 printf("Error: illegal hexadecimal string\n");
330 } else { /* string */
331 if (!strncmp(data, "=\"", 2) || !strncmp(data, "=S\"", 3)) {
337 len = strlen(data) - 1;
338 if (data[len] != '"')
346 new_buf = realloc(*bufp, *sizep + len);
350 memcpy(new_buf + *sizep, value, len);
361 * do_env_set_efi() - set UEFI variable
363 * @cmdtp: Command table
364 * @flag: Command flag
365 * @argc: Number of arguments
366 * @argv: Argument array
367 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
369 * This function is for "env set -e" or "setenv -e" command:
370 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
371 * [-i address,size] var, or
373 * Encode values specified and set given UEFI variable.
374 * If no value is specified, delete the variable.
376 int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
379 char *var_name, *value, *ep;
384 bool default_guid, verbose, value_on_memory;
389 return CMD_RET_USAGE;
391 /* Initialize EFI drivers */
392 ret = efi_init_obj_list();
393 if (ret != EFI_SUCCESS) {
394 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
395 ret & ~EFI_ERROR_MASK);
396 return CMD_RET_FAILURE;
400 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
401 * EFI_VARIABLE_RUNTIME_ACCESS;
406 guid = efi_global_variable_guid;
409 value_on_memory = false;
410 for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
411 if (!strcmp(argv[0], "-guid")) {
413 return CMD_RET_USAGE;
417 if (uuid_str_to_bin(argv[0], guid.b,
418 UUID_STR_FORMAT_GUID)) {
419 return CMD_RET_USAGE;
421 default_guid = false;
422 } else if (!strcmp(argv[0], "-bs")) {
423 attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
424 } else if (!strcmp(argv[0], "-rt")) {
425 attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
426 } else if (!strcmp(argv[0], "-nv")) {
427 attributes |= EFI_VARIABLE_NON_VOLATILE;
428 } else if (!strcmp(argv[0], "-at")) {
430 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
431 } else if (!strcmp(argv[0], "-a")) {
432 attributes |= EFI_VARIABLE_APPEND_WRITE;
433 } else if (!strcmp(argv[0], "-i")) {
434 /* data comes from memory */
436 return CMD_RET_USAGE;
440 addr = hextoul(argv[0], &ep);
442 return CMD_RET_USAGE;
444 /* 0 should be allowed for delete */
445 size = hextoul(++ep, NULL);
447 value_on_memory = true;
448 } else if (!strcmp(argv[0], "-v")) {
451 return CMD_RET_USAGE;
455 return CMD_RET_USAGE;
459 if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
460 !strcmp(var_name, "dbt"))
461 guid = efi_guid_image_security_database;
463 guid = efi_global_variable_guid;
467 printf("GUID: %pUl (%pUs)\n", &guid, &guid);
468 printf("Attributes: 0x%x\n", attributes);
473 value = map_sysmem(addr, 0);
475 for (argc--, argv++; argc > 0; argc--, argv++)
476 if (append_value(&value, &size, argv[0]) < 0) {
477 printf("## Failed to process an argument, %s\n",
479 ret = CMD_RET_FAILURE;
483 if (size && verbose) {
485 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
486 16, 1, value, size, true);
489 var_name16 = efi_convert_string(var_name);
491 printf("## Out of memory\n");
492 ret = CMD_RET_FAILURE;
495 ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
499 if (ret == EFI_SUCCESS) {
500 ret = CMD_RET_SUCCESS;
506 msg = " (not found)";
508 case EFI_WRITE_PROTECTED:
509 msg = " (read only)";
511 case EFI_INVALID_PARAMETER:
512 msg = " (invalid parameter)";
514 case EFI_SECURITY_VIOLATION:
515 msg = " (validation failed)";
517 case EFI_OUT_OF_RESOURCES:
518 msg = " (out of memory)";
524 printf("## Failed to set EFI variable%s\n", msg);
525 ret = CMD_RET_FAILURE;