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_CALL(efi_get_next_variable_name(&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_CALL(efi_get_next_variable_name(&size
,
206 if (ret
!= EFI_SUCCESS
) {
208 return CMD_RET_FAILURE
;
211 if (guid_p
&& guidcmp(guid_p
, &guid
))
213 if (!argc
|| match_name(argc
, argv
, var_name16
)) {
215 efi_dump_single_var(var_name16
, &guid
, verbose
);
220 if (!match
&& argc
== 1)
221 printf("Error: \"%s\" not defined\n", argv
[0]);
223 return CMD_RET_SUCCESS
;
227 * do_env_print_efi() - show information about UEFI variables
229 * @cmdtp: Command table
230 * @flag: Command flag
231 * @argc: Number of arguments
232 * @argv: Argument array
233 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
235 * This function is for "env print -e" or "printenv -e" command:
236 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
237 * If one or more variable names are specified, show information
238 * named UEFI variables, otherwise show all the UEFI variables.
240 int do_env_print_efi(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
243 const efi_guid_t
*guid_p
= NULL
;
248 /* Initialize EFI drivers */
249 ret
= efi_init_obj_list();
250 if (ret
!= EFI_SUCCESS
) {
251 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
252 ret
& ~EFI_ERROR_MASK
);
253 return CMD_RET_FAILURE
;
256 for (argc
--, argv
++; argc
> 0 && argv
[0][0] == '-'; argc
--, argv
++) {
257 if (!strcmp(argv
[0], "-guid")) {
259 return CMD_RET_USAGE
;
262 if (uuid_str_to_bin(argv
[0], guid
.b
,
263 UUID_STR_FORMAT_GUID
))
264 return CMD_RET_USAGE
;
265 guid_p
= (const efi_guid_t
*)guid
.b
;
266 } else if (!strcmp(argv
[0], "-n")) {
269 return CMD_RET_USAGE
;
273 /* enumerate and show all UEFI variables */
274 return efi_dump_var_all(argc
, argv
, guid_p
, verbose
);
278 * append_value() - encode UEFI variable's value
279 * @bufp: Buffer of encoded UEFI variable's value
280 * @sizep: Size of buffer
281 * @data: data to be encoded into the value
282 * Return: 0 on success, -1 otherwise
284 * Interpret a given data string and append it to buffer.
285 * Buffer will be realloc'ed if necessary.
287 * Currently supported formats are:
288 * =0x0123...: Hexadecimal number
289 * =H0123...: Hexadecimal-byte array
290 * ="...", =S"..." or <string>:
293 static int append_value(char **bufp
, size_t *sizep
, char *data
)
295 char *tmp_buf
= NULL
, *new_buf
= NULL
, *value
;
296 unsigned long len
= 0;
298 if (!strncmp(data
, "=0x", 2)) { /* hexadecimal number */
305 unsigned long hex_value
;
310 if ((len
& 0x1)) /* not multiple of two */
321 /* convert hex hexadecimal number */
322 if (strict_strtoul(data
, 16, &hex_value
) < 0)
325 tmp_buf
= malloc(len
);
330 tmp_data
.u8
= hex_value
;
331 hex_ptr
= &tmp_data
.u8
;
332 } else if (len
== 2) {
333 tmp_data
.u16
= hex_value
;
334 hex_ptr
= &tmp_data
.u16
;
335 } else if (len
== 4) {
336 tmp_data
.u32
= hex_value
;
337 hex_ptr
= &tmp_data
.u32
;
339 tmp_data
.u64
= hex_value
;
340 hex_ptr
= &tmp_data
.u64
;
342 memcpy(tmp_buf
, hex_ptr
, len
);
345 } else if (!strncmp(data
, "=H", 2)) { /* hexadecimal-byte array */
348 if (len
& 0x1) /* not multiple of two */
352 tmp_buf
= malloc(len
);
356 if (hex2bin((u8
*)tmp_buf
, data
, len
) < 0) {
357 printf("Error: illegal hexadecimal string\n");
363 } else { /* string */
364 if (!strncmp(data
, "=\"", 2) || !strncmp(data
, "=S\"", 3)) {
370 len
= strlen(data
) - 1;
371 if (data
[len
] != '"')
379 new_buf
= realloc(*bufp
, *sizep
+ len
);
383 memcpy(new_buf
+ *sizep
, value
, len
);
394 * do_env_set_efi() - set UEFI variable
396 * @cmdtp: Command table
397 * @flag: Command flag
398 * @argc: Number of arguments
399 * @argv: Argument array
400 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
402 * This function is for "env set -e" or "setenv -e" command:
403 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
404 * [-i address,size] var, or
406 * Encode values specified and set given UEFI variable.
407 * If no value is specified, delete the variable.
409 int do_env_set_efi(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
412 char *var_name
, *value
, *ep
;
417 bool default_guid
, verbose
, value_on_memory
;
418 u16
*var_name16
= NULL
, *p
;
423 return CMD_RET_USAGE
;
425 /* Initialize EFI drivers */
426 ret
= efi_init_obj_list();
427 if (ret
!= EFI_SUCCESS
) {
428 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
429 ret
& ~EFI_ERROR_MASK
);
430 return CMD_RET_FAILURE
;
434 * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
435 * EFI_VARIABLE_RUNTIME_ACCESS;
440 guid
= efi_global_variable_guid
;
443 value_on_memory
= false;
444 for (argc
--, argv
++; argc
> 0 && argv
[0][0] == '-'; argc
--, argv
++) {
445 if (!strcmp(argv
[0], "-guid")) {
447 return CMD_RET_USAGE
;
451 if (uuid_str_to_bin(argv
[0], guid
.b
,
452 UUID_STR_FORMAT_GUID
)) {
453 return CMD_RET_USAGE
;
455 default_guid
= false;
456 } else if (!strcmp(argv
[0], "-bs")) {
457 attributes
|= EFI_VARIABLE_BOOTSERVICE_ACCESS
;
458 } else if (!strcmp(argv
[0], "-rt")) {
459 attributes
|= EFI_VARIABLE_RUNTIME_ACCESS
;
460 } else if (!strcmp(argv
[0], "-nv")) {
461 attributes
|= EFI_VARIABLE_NON_VOLATILE
;
462 } else if (!strcmp(argv
[0], "-at")) {
464 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
;
465 } else if (!strcmp(argv
[0], "-a")) {
466 attributes
|= EFI_VARIABLE_APPEND_WRITE
;
467 } else if (!strcmp(argv
[0], "-i")) {
468 /* data comes from memory */
470 return CMD_RET_USAGE
;
474 addr
= hextoul(argv
[0], &ep
);
476 return CMD_RET_USAGE
;
478 /* 0 should be allowed for delete */
479 size
= hextoul(++ep
, NULL
);
481 value_on_memory
= true;
482 } else if (!strcmp(argv
[0], "-v")) {
485 return CMD_RET_USAGE
;
489 return CMD_RET_USAGE
;
493 if (!strcmp(var_name
, "db") || !strcmp(var_name
, "dbx") ||
494 !strcmp(var_name
, "dbt"))
495 guid
= efi_guid_image_security_database
;
497 guid
= efi_global_variable_guid
;
501 printf("GUID: %pUl %s\n", &guid
,
502 efi_guid_to_str((const efi_guid_t
*)&guid
));
503 printf("Attributes: 0x%x\n", attributes
);
508 value
= map_sysmem(addr
, 0);
510 for (argc
--, argv
++; argc
> 0; argc
--, argv
++)
511 if (append_value(&value
, &size
, argv
[0]) < 0) {
512 printf("## Failed to process an argument, %s\n",
514 ret
= CMD_RET_FAILURE
;
518 if (size
&& verbose
) {
520 print_hex_dump(" ", DUMP_PREFIX_OFFSET
,
521 16, 1, value
, size
, true);
524 len
= utf8_utf16_strnlen(var_name
, strlen(var_name
));
525 var_name16
= malloc((len
+ 1) * 2);
527 printf("## Out of memory\n");
528 ret
= CMD_RET_FAILURE
;
532 utf8_utf16_strncpy(&p
, var_name
, len
+ 1);
534 ret
= efi_set_variable_int(var_name16
, &guid
, attributes
, size
, value
,
537 if (ret
== EFI_SUCCESS
) {
538 ret
= CMD_RET_SUCCESS
;
544 msg
= " (not found)";
546 case EFI_WRITE_PROTECTED
:
547 msg
= " (read only)";
549 case EFI_INVALID_PARAMETER
:
550 msg
= " (invalid parameter)";
552 case EFI_SECURITY_VIOLATION
:
553 msg
= " (validation failed)";
555 case EFI_OUT_OF_RESOURCES
:
556 msg
= " (out of memory)";
562 printf("## Failed to set EFI variable%s\n", msg
);
563 ret
= CMD_RET_FAILURE
;