1 // SPDX-License-Identifier: GPL-2.0+
3 * Integrate UEFI variables to u-boot env interface
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
10 #include <efi_loader.h>
11 #include <efi_variable.h>
18 #include <u-boot/uuid.h>
19 #include <linux/kernel.h>
22 * From efi_variable.c,
24 * Mapping between UEFI variables and u-boot variables:
26 * efi_$guid_$varname = {attributes}(type)value
33 {EFI_VARIABLE_NON_VOLATILE
, "NV"},
34 {EFI_VARIABLE_BOOTSERVICE_ACCESS
, "BS"},
35 {EFI_VARIABLE_RUNTIME_ACCESS
, "RT"},
36 {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
, "AW"},
37 {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
, "AT"},
38 {EFI_VARIABLE_READ_ONLY
, "RO"},
42 * efi_dump_single_var() - show information about a UEFI variable
44 * @name: Name of the variable
46 * @verbose: if true, dump data
48 * Show information encoded in one UEFI variable
50 static void efi_dump_single_var(u16
*name
, const efi_guid_t
*guid
, bool verbose
)
62 ret
= efi_get_variable_int(name
, guid
, &attributes
, &size
, data
, &time
);
63 if (ret
== EFI_BUFFER_TOO_SMALL
) {
68 ret
= efi_get_variable_int(name
, guid
, &attributes
, &size
,
71 if (ret
== EFI_NOT_FOUND
) {
72 printf("Error: \"%ls\" not defined\n", name
);
75 if (ret
!= EFI_SUCCESS
)
79 printf("%ls:\n %pUl (%pUs)\n", name
, guid
, guid
);
80 if (attributes
& EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
)
81 printf(" %04d-%02d-%02d %02d:%02d:%02d\n", tm
.tm_year
,
82 tm
.tm_mon
, tm
.tm_mday
, tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
84 for (count
= 0, i
= 0; i
< ARRAY_SIZE(efi_var_attrs
); i
++)
85 if (attributes
& efi_var_attrs
[i
].mask
) {
89 puts(efi_var_attrs
[i
].text
);
91 printf(", DataSize = 0x%zx\n", size
);
93 print_hex_dump(" ", DUMP_PREFIX_OFFSET
, 16, 1,
100 static bool match_name(int argc
, char *const argv
[], u16
*var_name16
)
107 buflen
= utf16_utf8_strlen(var_name16
) + 1;
108 buf
= calloc(1, buflen
);
113 utf16_utf8_strcpy(&p
, var_name16
);
115 for (i
= 0; i
< argc
; argc
--, argv
++) {
116 if (!strcmp(buf
, argv
[i
])) {
129 * efi_dump_var_all() - show information about all the UEFI variables
131 * @argc: Number of arguments (variables)
132 * @argv: Argument (variable name) array
133 * @verbose: if true, dump data
134 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
136 * Show information encoded in all the UEFI variables
138 static int efi_dump_var_all(int argc
, char *const argv
[],
139 const efi_guid_t
*guid_p
, bool verbose
)
142 efi_uintn_t buf_size
, size
;
148 var_name16
= malloc(buf_size
);
150 return CMD_RET_FAILURE
;
155 ret
= efi_get_next_variable_name_int(&size
, var_name16
,
157 if (ret
== EFI_NOT_FOUND
)
159 if (ret
== EFI_BUFFER_TOO_SMALL
) {
161 p
= realloc(var_name16
, buf_size
);
164 return CMD_RET_FAILURE
;
167 ret
= efi_get_next_variable_name_int(&size
, var_name16
,
170 if (ret
!= EFI_SUCCESS
) {
172 return CMD_RET_FAILURE
;
175 if (guid_p
&& guidcmp(guid_p
, &guid
))
177 if (!argc
|| match_name(argc
, argv
, var_name16
)) {
179 efi_dump_single_var(var_name16
, &guid
, verbose
);
184 if (!match
&& argc
== 1) {
185 printf("Error: \"%s\" not defined\n", argv
[0]);
186 return CMD_RET_FAILURE
;
189 return CMD_RET_SUCCESS
;
193 * do_env_print_efi() - show information about UEFI variables
195 * @cmdtp: Command table
196 * @flag: Command flag
197 * @argc: Number of arguments
198 * @argv: Argument array
199 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
201 * This function is for "env print -e" or "printenv -e" command:
202 * => env print -e [-n] [-guid <guid> | -all] [var [...]]
203 * If one or more variable names are specified, show information
204 * named UEFI variables, otherwise show all the UEFI variables.
206 int do_env_print_efi(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
209 const efi_guid_t
*guid_p
= NULL
;
214 /* Initialize EFI drivers */
215 ret
= efi_init_obj_list();
216 if (ret
!= EFI_SUCCESS
) {
217 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
218 ret
& ~EFI_ERROR_MASK
);
219 return CMD_RET_FAILURE
;
222 for (argc
--, argv
++; argc
> 0 && argv
[0][0] == '-'; argc
--, argv
++) {
223 if (!strcmp(argv
[0], "-guid")) {
225 return CMD_RET_USAGE
;
228 if (uuid_str_to_bin(argv
[0], guid
.b
,
229 UUID_STR_FORMAT_GUID
))
230 return CMD_RET_USAGE
;
231 guid_p
= (const efi_guid_t
*)guid
.b
;
232 } else if (!strcmp(argv
[0], "-n")) {
235 return CMD_RET_USAGE
;
239 /* enumerate and show all UEFI variables */
240 return efi_dump_var_all(argc
, argv
, guid_p
, verbose
);
244 * append_value() - encode UEFI variable's value
245 * @bufp: Buffer of encoded UEFI variable's value
246 * @sizep: Size of buffer
247 * @data: data to be encoded into the value
248 * Return: 0 on success, -1 otherwise
250 * Interpret a given data string and append it to buffer.
251 * Buffer will be realloc'ed if necessary.
253 * Currently supported formats are:
254 * =0x0123...: Hexadecimal number
255 * =H0123...: Hexadecimal-byte array
256 * ="...", =S"..." or <string>:
259 static int append_value(char **bufp
, size_t *sizep
, char *data
)
261 char *tmp_buf
= NULL
, *new_buf
= NULL
, *value
;
262 unsigned long len
= 0;
264 if (!strncmp(data
, "=0x", 3)) { /* hexadecimal number */
271 unsigned long hex_value
;
276 if ((len
& 0x1)) /* not multiple of two */
287 /* convert hex hexadecimal number */
288 if (strict_strtoul(data
, 16, &hex_value
) < 0)
291 tmp_buf
= malloc(len
);
296 tmp_data
.u8
= hex_value
;
297 hex_ptr
= &tmp_data
.u8
;
298 } else if (len
== 2) {
299 tmp_data
.u16
= hex_value
;
300 hex_ptr
= &tmp_data
.u16
;
301 } else if (len
== 4) {
302 tmp_data
.u32
= hex_value
;
303 hex_ptr
= &tmp_data
.u32
;
305 tmp_data
.u64
= hex_value
;
306 hex_ptr
= &tmp_data
.u64
;
308 memcpy(tmp_buf
, hex_ptr
, len
);
311 } else if (!strncmp(data
, "=H", 2)) { /* hexadecimal-byte array */
314 if (len
& 0x1) /* not multiple of two */
318 tmp_buf
= malloc(len
);
322 if (hex2bin((u8
*)tmp_buf
, data
, len
) < 0) {
323 printf("Error: illegal hexadecimal string\n");
329 } else { /* string */
330 if (!strncmp(data
, "=\"", 2) || !strncmp(data
, "=S\"", 3)) {
336 len
= strlen(data
) - 1;
337 if (data
[len
] != '"')
345 new_buf
= realloc(*bufp
, *sizep
+ len
);
349 memcpy(new_buf
+ *sizep
, value
, len
);
360 * do_env_set_efi() - set UEFI variable
362 * @cmdtp: Command table
363 * @flag: Command flag
364 * @argc: Number of arguments
365 * @argv: Argument array
366 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
368 * This function is for "env set -e" or "setenv -e" command:
369 * => env set -e [-guid guid][-nv][-bs][-rt][-at][-a][-v]
370 * [-i address,size] var, or
372 * Encode values specified and set given UEFI variable.
373 * If no value is specified, delete the variable.
375 int do_env_set_efi(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
378 char *var_name
, *value
, *ep
;
383 bool default_guid
, verbose
, value_on_memory
;
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 var_name16
= efi_convert_string(var_name
);
490 printf("## Out of memory\n");
491 ret
= CMD_RET_FAILURE
;
494 ret
= efi_set_variable_int(var_name16
, &guid
, attributes
, size
, value
,
498 if (ret
== EFI_SUCCESS
) {
499 ret
= CMD_RET_SUCCESS
;
505 msg
= " (not found)";
507 case EFI_WRITE_PROTECTED
:
508 msg
= " (read only)";
510 case EFI_INVALID_PARAMETER
:
511 msg
= " (invalid parameter)";
513 case EFI_SECURITY_VIOLATION
:
514 msg
= " (validation failed)";
516 case EFI_OUT_OF_RESOURCES
:
517 msg
= " (out of memory)";
523 printf("## Failed to set EFI variable%s\n", msg
);
524 ret
= CMD_RET_FAILURE
;