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>
16 #include <linux/kernel.h>
19 * From efi_variable.c,
21 * Mapping between UEFI variables and u-boot variables:
23 * efi_$guid_$varname = {attributes}(type)value
30 {EFI_VARIABLE_NON_VOLATILE
, "NV"},
31 {EFI_VARIABLE_BOOTSERVICE_ACCESS
, "BS"},
32 {EFI_VARIABLE_RUNTIME_ACCESS
, "RT"},
33 {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
, "AW"},
34 {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
, "AT"},
38 * efi_dump_single_var() - show information about a UEFI variable
40 * @name: Name of the variable
43 * Show information encoded in one UEFI variable
45 static void efi_dump_single_var(u16
*name
, efi_guid_t
*guid
)
55 ret
= EFI_CALL(efi_get_variable(name
, guid
, &attributes
, &size
, data
));
56 if (ret
== EFI_BUFFER_TOO_SMALL
) {
61 ret
= EFI_CALL(efi_get_variable(name
, guid
, &attributes
, &size
,
64 if (ret
== EFI_NOT_FOUND
) {
65 printf("Error: \"%ls\" not defined\n", name
);
68 if (ret
!= EFI_SUCCESS
)
72 for (count
= 0, i
= 0; i
< ARRAY_SIZE(efi_var_attrs
); i
++)
73 if (attributes
& efi_var_attrs
[i
].mask
) {
79 puts(efi_var_attrs
[i
].text
);
81 printf(", DataSize = 0x%zx\n", size
);
82 print_hex_dump(" ", DUMP_PREFIX_OFFSET
, 16, 1, data
, size
, true);
89 * efi_dump_vars() - show information about named UEFI variables
91 * @argc: Number of arguments (variables)
92 * @argv: Argument (variable name) array
93 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
95 * Show information encoded in named UEFI variables
97 static int efi_dump_vars(int argc
, char * const argv
[])
100 efi_uintn_t buf_size
, size
;
103 var_name16
= malloc(buf_size
);
105 return CMD_RET_FAILURE
;
107 for (; argc
> 0; argc
--, argv
++) {
108 size
= (utf8_utf16_strlen(argv
[0]) + 1) * sizeof(u16
);
109 if (buf_size
< size
) {
111 p
= realloc(var_name16
, buf_size
);
114 return CMD_RET_FAILURE
;
120 utf8_utf16_strcpy(&p
, argv
[0]);
122 efi_dump_single_var(var_name16
,
123 (efi_guid_t
*)&efi_global_variable_guid
);
128 return CMD_RET_SUCCESS
;
132 * efi_dump_vars() - show information about all the UEFI variables
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(void)
141 efi_uintn_t buf_size
, size
;
146 var_name16
= malloc(buf_size
);
148 return CMD_RET_FAILURE
;
153 ret
= EFI_CALL(efi_get_next_variable_name(&size
, var_name16
,
155 if (ret
== EFI_NOT_FOUND
)
157 if (ret
== EFI_BUFFER_TOO_SMALL
) {
159 p
= realloc(var_name16
, buf_size
);
162 return CMD_RET_FAILURE
;
165 ret
= EFI_CALL(efi_get_next_variable_name(&size
,
169 if (ret
!= EFI_SUCCESS
) {
171 return CMD_RET_FAILURE
;
174 efi_dump_single_var(var_name16
, &guid
);
179 return CMD_RET_SUCCESS
;
183 * do_env_print_efi() - show information about UEFI variables
185 * @cmdtp: Command table
186 * @flag: Command flag
187 * @argc: Number of arguments
188 * @argv: Argument array
189 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
191 * This function is for "env print -e" or "printenv -e" command:
192 * => env print -e [var [...]]
193 * If one or more variable names are specified, show information
194 * named UEFI variables, otherwise show all the UEFI variables.
196 int do_env_print_efi(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
200 /* Initialize EFI drivers */
201 ret
= efi_init_obj_list();
202 if (ret
!= EFI_SUCCESS
) {
203 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
204 ret
& ~EFI_ERROR_MASK
);
205 return CMD_RET_FAILURE
;
209 /* show specified UEFI variables */
210 return efi_dump_vars(--argc
, ++argv
);
212 /* enumerate and show all UEFI variables */
213 return efi_dump_var_all();
217 * append_value() - encode UEFI variable's value
218 * @bufp: Buffer of encoded UEFI variable's value
219 * @sizep: Size of buffer
220 * @data: data to be encoded into the value
221 * Return: 0 on success, -1 otherwise
223 * Interpret a given data string and append it to buffer.
224 * Buffer will be realloc'ed if necessary.
226 * Currently supported formats are:
227 * =0x0123...: Hexadecimal number
228 * =H0123...: Hexadecimal-byte array
229 * ="...", =S"..." or <string>:
232 static int append_value(char **bufp
, size_t *sizep
, char *data
)
234 char *tmp_buf
= NULL
, *new_buf
= NULL
, *value
;
235 unsigned long len
= 0;
237 if (!strncmp(data
, "=0x", 2)) { /* hexadecimal number */
244 unsigned long hex_value
;
249 if ((len
& 0x1)) /* not multiple of two */
260 /* convert hex hexadecimal number */
261 if (strict_strtoul(data
, 16, &hex_value
) < 0)
264 tmp_buf
= malloc(len
);
269 tmp_data
.u8
= hex_value
;
270 hex_ptr
= &tmp_data
.u8
;
271 } else if (len
== 2) {
272 tmp_data
.u16
= hex_value
;
273 hex_ptr
= &tmp_data
.u16
;
274 } else if (len
== 4) {
275 tmp_data
.u32
= hex_value
;
276 hex_ptr
= &tmp_data
.u32
;
278 tmp_data
.u64
= hex_value
;
279 hex_ptr
= &tmp_data
.u64
;
281 memcpy(tmp_buf
, hex_ptr
, len
);
284 } else if (!strncmp(data
, "=H", 2)) { /* hexadecimal-byte array */
287 if (len
& 0x1) /* not multiple of two */
291 tmp_buf
= malloc(len
);
295 if (hex2bin((u8
*)tmp_buf
, data
, len
) < 0) {
296 printf("Error: illegal hexadecimal string\n");
302 } else { /* string */
303 if (!strncmp(data
, "=\"", 2) || !strncmp(data
, "=S\"", 3)) {
309 len
= strlen(data
) - 1;
310 if (data
[len
] != '"')
318 new_buf
= realloc(*bufp
, *sizep
+ len
);
322 memcpy(new_buf
+ *sizep
, value
, len
);
333 * do_env_print_efi() - set UEFI variable
335 * @cmdtp: Command table
336 * @flag: Command flag
337 * @argc: Number of arguments
338 * @argv: Argument array
339 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
341 * This function is for "env set -e" or "setenv -e" command:
342 * => env set -e var [value ...]]
343 * Encode values specified and set given UEFI variable.
344 * If no value is specified, delete the variable.
346 int do_env_set_efi(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
348 char *var_name
, *value
= NULL
;
349 efi_uintn_t size
= 0;
350 u16
*var_name16
= NULL
, *p
;
357 return CMD_RET_USAGE
;
359 /* Initialize EFI drivers */
360 ret
= efi_init_obj_list();
361 if (ret
!= EFI_SUCCESS
) {
362 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
363 ret
& ~EFI_ERROR_MASK
);
364 return CMD_RET_FAILURE
;
367 attributes
= EFI_VARIABLE_BOOTSERVICE_ACCESS
|
368 EFI_VARIABLE_RUNTIME_ACCESS
;
369 if (!strcmp(argv
[1], "-nv")) {
370 attributes
|= EFI_VARIABLE_NON_VOLATILE
;
374 return CMD_RET_SUCCESS
;
386 for ( ; argc
> 0; argc
--, argv
++)
387 if (append_value(&value
, &size
, argv
[0]) < 0) {
388 printf("## Failed to process an argument, %s\n",
390 ret
= CMD_RET_FAILURE
;
395 len
= utf8_utf16_strnlen(var_name
, strlen(var_name
));
396 var_name16
= malloc((len
+ 1) * 2);
398 printf("## Out of memory\n");
399 ret
= CMD_RET_FAILURE
;
403 utf8_utf16_strncpy(&p
, var_name
, len
+ 1);
405 guid
= efi_global_variable_guid
;
406 ret
= EFI_CALL(efi_set_variable(var_name16
, &guid
, attributes
,
408 if (ret
== EFI_SUCCESS
) {
409 ret
= CMD_RET_SUCCESS
;
411 printf("## Failed to set EFI variable\n");
412 ret
= CMD_RET_FAILURE
;