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>
15 #include <linux/kernel.h>
18 * From efi_variable.c,
20 * Mapping between UEFI variables and u-boot variables:
22 * efi_$guid_$varname = {attributes}(type)value
29 {EFI_VARIABLE_NON_VOLATILE
, "NV"},
30 {EFI_VARIABLE_BOOTSERVICE_ACCESS
, "BS"},
31 {EFI_VARIABLE_RUNTIME_ACCESS
, "RT"},
32 {EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
, "AW"},
33 {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
, "AT"},
37 * efi_dump_single_var() - show information about a UEFI variable
39 * @name: Name of the variable
42 * Show information encoded in one UEFI variable
44 static void efi_dump_single_var(u16
*name
, efi_guid_t
*guid
)
54 ret
= EFI_CALL(efi_get_variable(name
, guid
, &attributes
, &size
, data
));
55 if (ret
== EFI_BUFFER_TOO_SMALL
) {
60 ret
= EFI_CALL(efi_get_variable(name
, guid
, &attributes
, &size
,
63 if (ret
== EFI_NOT_FOUND
) {
64 printf("Error: \"%ls\" not defined\n", name
);
67 if (ret
!= EFI_SUCCESS
)
71 for (count
= 0, i
= 0; i
< ARRAY_SIZE(efi_var_attrs
); i
++)
72 if (attributes
& efi_var_attrs
[i
].mask
) {
78 puts(efi_var_attrs
[i
].text
);
80 printf(", DataSize = 0x%zx\n", size
);
81 print_hex_dump(" ", DUMP_PREFIX_OFFSET
, 16, 1, data
, size
, true);
88 * efi_dump_vars() - show information about named UEFI variables
90 * @argc: Number of arguments (variables)
91 * @argv: Argument (variable name) array
92 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
94 * Show information encoded in named UEFI variables
96 static int efi_dump_vars(int argc
, char * const argv
[])
99 efi_uintn_t buf_size
, size
;
102 var_name16
= malloc(buf_size
);
104 return CMD_RET_FAILURE
;
106 for (; argc
> 0; argc
--, argv
++) {
107 size
= (utf8_utf16_strlen(argv
[0]) + 1) * sizeof(u16
);
108 if (buf_size
< size
) {
110 p
= realloc(var_name16
, buf_size
);
113 return CMD_RET_FAILURE
;
119 utf8_utf16_strcpy(&p
, argv
[0]);
121 efi_dump_single_var(var_name16
,
122 (efi_guid_t
*)&efi_global_variable_guid
);
127 return CMD_RET_SUCCESS
;
131 * efi_dump_vars() - show information about all the UEFI variables
133 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
135 * Show information encoded in all the UEFI variables
137 static int efi_dump_var_all(void)
140 efi_uintn_t buf_size
, size
;
145 var_name16
= malloc(buf_size
);
147 return CMD_RET_FAILURE
;
152 ret
= EFI_CALL(efi_get_next_variable_name(&size
, var_name16
,
154 if (ret
== EFI_NOT_FOUND
)
156 if (ret
== EFI_BUFFER_TOO_SMALL
) {
158 p
= realloc(var_name16
, buf_size
);
161 return CMD_RET_FAILURE
;
164 ret
= EFI_CALL(efi_get_next_variable_name(&size
,
168 if (ret
!= EFI_SUCCESS
) {
170 return CMD_RET_FAILURE
;
173 efi_dump_single_var(var_name16
, &guid
);
178 return CMD_RET_SUCCESS
;
182 * do_env_print_efi() - show information about UEFI variables
184 * @cmdtp: Command table
185 * @flag: Command flag
186 * @argc: Number of arguments
187 * @argv: Argument array
188 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
190 * This function is for "env print -e" or "printenv -e" command:
191 * => env print -e [var [...]]
192 * If one or more variable names are specified, show information
193 * named UEFI variables, otherwise show all the UEFI variables.
195 int do_env_print_efi(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
199 /* Initialize EFI drivers */
200 ret
= efi_init_obj_list();
201 if (ret
!= EFI_SUCCESS
) {
202 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
203 ret
& ~EFI_ERROR_MASK
);
204 return CMD_RET_FAILURE
;
208 /* show specified UEFI variables */
209 return efi_dump_vars(--argc
, ++argv
);
211 /* enumerate and show all UEFI variables */
212 return efi_dump_var_all();
216 * append_value() - encode UEFI variable's value
217 * @bufp: Buffer of encoded UEFI variable's value
218 * @sizep: Size of buffer
219 * @data: data to be encoded into the value
220 * Return: 0 on success, -1 otherwise
222 * Interpret a given data string and append it to buffer.
223 * Buffer will be realloc'ed if necessary.
225 * Currently supported formats are:
226 * =0x0123...: Hexadecimal number
227 * =H0123...: Hexadecimal-byte array
228 * ="...", =S"..." or <string>:
231 static int append_value(char **bufp
, size_t *sizep
, char *data
)
233 char *tmp_buf
= NULL
, *new_buf
= NULL
, *value
;
234 unsigned long len
= 0;
236 if (!strncmp(data
, "=0x", 2)) { /* hexadecimal number */
243 unsigned long hex_value
;
248 if ((len
& 0x1)) /* not multiple of two */
259 /* convert hex hexadecimal number */
260 if (strict_strtoul(data
, 16, &hex_value
) < 0)
263 tmp_buf
= malloc(len
);
268 tmp_data
.u8
= hex_value
;
269 hex_ptr
= &tmp_data
.u8
;
270 } else if (len
== 2) {
271 tmp_data
.u16
= hex_value
;
272 hex_ptr
= &tmp_data
.u16
;
273 } else if (len
== 4) {
274 tmp_data
.u32
= hex_value
;
275 hex_ptr
= &tmp_data
.u32
;
277 tmp_data
.u64
= hex_value
;
278 hex_ptr
= &tmp_data
.u64
;
280 memcpy(tmp_buf
, hex_ptr
, len
);
283 } else if (!strncmp(data
, "=H", 2)) { /* hexadecimal-byte array */
286 if (len
& 0x1) /* not multiple of two */
290 tmp_buf
= malloc(len
);
294 if (hex2bin((u8
*)tmp_buf
, data
, len
) < 0) {
295 printf("Error: illegal hexadecimal string\n");
301 } else { /* string */
302 if (!strncmp(data
, "=\"", 2) || !strncmp(data
, "=S\"", 3)) {
308 len
= strlen(data
) - 1;
309 if (data
[len
] != '"')
317 new_buf
= realloc(*bufp
, *sizep
+ len
);
321 memcpy(new_buf
+ *sizep
, value
, len
);
332 * do_env_print_efi() - set UEFI variable
334 * @cmdtp: Command table
335 * @flag: Command flag
336 * @argc: Number of arguments
337 * @argv: Argument array
338 * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
340 * This function is for "env set -e" or "setenv -e" command:
341 * => env set -e var [value ...]]
342 * Encode values specified and set given UEFI variable.
343 * If no value is specified, delete the variable.
345 int do_env_set_efi(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
347 char *var_name
, *value
= NULL
;
348 efi_uintn_t size
= 0;
349 u16
*var_name16
= NULL
, *p
;
356 return CMD_RET_USAGE
;
358 /* Initialize EFI drivers */
359 ret
= efi_init_obj_list();
360 if (ret
!= EFI_SUCCESS
) {
361 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
362 ret
& ~EFI_ERROR_MASK
);
363 return CMD_RET_FAILURE
;
366 attributes
= EFI_VARIABLE_BOOTSERVICE_ACCESS
|
367 EFI_VARIABLE_RUNTIME_ACCESS
;
368 if (!strcmp(argv
[1], "-nv")) {
369 attributes
|= EFI_VARIABLE_NON_VOLATILE
;
373 return CMD_RET_SUCCESS
;
385 for ( ; argc
> 0; argc
--, argv
++)
386 if (append_value(&value
, &size
, argv
[0]) < 0) {
387 printf("## Failed to process an argument, %s\n",
389 ret
= CMD_RET_FAILURE
;
394 len
= utf8_utf16_strnlen(var_name
, strlen(var_name
));
395 var_name16
= malloc((len
+ 1) * 2);
397 printf("## Out of memory\n");
398 ret
= CMD_RET_FAILURE
;
402 utf8_utf16_strncpy(&p
, var_name
, len
+ 1);
404 guid
= efi_global_variable_guid
;
405 ret
= EFI_CALL(efi_set_variable(var_name16
, &guid
, attributes
,
407 if (ret
== EFI_SUCCESS
) {
408 ret
= CMD_RET_SUCCESS
;
410 printf("## Failed to set EFI variable\n");
411 ret
= CMD_RET_FAILURE
;