Add command to set an environment variable to an EFI variable

We may want to make things conditional on EFI variable state

Signed-off-by: Matthew Garrett <mgarrett@aurora.tech>
Reviewed-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Matthew Garrett
2024-11-23 11:55:09 -08:00
committed by Simon Glass
parent de05434022
commit 652a64a686
3 changed files with 138 additions and 0 deletions

View File

@@ -761,6 +761,10 @@ config CMD_NVEDIT_SELECT
help help
Select the compiled-in persistent storage of environment variables. Select the compiled-in persistent storage of environment variables.
config CMD_EFI_GET_ENV
bool "efi get env"
help
Set an environment variable to the contents of an EFI variable
endmenu endmenu
menu "Memory commands" menu "Memory commands"

View File

@@ -70,6 +70,7 @@ obj-$(CONFIG_CMD_EEPROM) += eeprom.o
obj-$(CONFIG_EFI) += efi.o efi_common.o obj-$(CONFIG_EFI) += efi.o efi_common.o
obj-$(CONFIG_CMD_EFIDEBUG) += efidebug.o efi_common.o obj-$(CONFIG_CMD_EFIDEBUG) += efidebug.o efi_common.o
obj-$(CONFIG_CMD_EFICONFIG) += eficonfig.o obj-$(CONFIG_CMD_EFICONFIG) += eficonfig.o
obj-$(CONFIG_CMD_EFI_GET_ENV) += efigetenv.o
ifdef CONFIG_CMD_EFICONFIG ifdef CONFIG_CMD_EFICONFIG
ifdef CONFIG_EFI_MM_COMM_TEE ifdef CONFIG_EFI_MM_COMM_TEE
obj-$(CONFIG_EFI_SECURE_BOOT) += eficonfig_sbkey.o obj-$(CONFIG_EFI_SECURE_BOOT) += eficonfig_sbkey.o

133
cmd/efigetenv.c Normal file
View File

@@ -0,0 +1,133 @@
// SPDX-License-Identifier: GPL-2.0+
#include <charset.h>
#include <command.h>
#include <efi_loader.h>
#include <efi_variable.h>
#include <env.h>
#include <hexdump.h>
#include <malloc.h>
#include <uuid.h>
/* Set a U-Boot environment variable to the contents of a UEFI variable */
int do_efi_get_env(struct cmd_tbl *cmdtb, int flat, int argc, char *const argv[])
{
u16 *var_name = NULL;
char *strdata = NULL;
efi_uintn_t size = 0;
bool var_content_is_utf16_string = false;
efi_status_t ret;
efi_guid_t guid;
u8 *data = NULL;
u32 attributes;
size_t len;
u64 time;
u16 *p;
ret = efi_init_obj_list();
if (ret != EFI_SUCCESS) {
printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
ret & ~EFI_ERROR_MASK);
return CMD_RET_FAILURE;
}
argv++;
argc--;
if (argc != 3 && argc != 4)
return CMD_RET_USAGE;
if (argc == 4) {
if (strcmp(argv[0], "-s"))
return CMD_RET_USAGE;
var_content_is_utf16_string = true;
argv++;
argc--;
}
len = utf8_utf16_strnlen(argv[0], strlen(argv[0]));
var_name = malloc((len + 1) * 2);
if (!var_name) {
printf("## Out of memory\n");
return CMD_RET_FAILURE;
}
p = var_name;
utf8_utf16_strncpy(&p, argv[0], len + 1);
if (uuid_str_to_bin(argv[1], guid.b, UUID_STR_FORMAT_GUID)) {
ret = CMD_RET_USAGE;
goto out;
}
ret = efi_get_variable_int(var_name, &guid, &attributes, &size, data,
&time);
if (ret == EFI_BUFFER_TOO_SMALL) {
data = malloc(size);
if (!data) {
printf("## Out of memory\n");
ret = CMD_RET_FAILURE;
goto out;
}
ret = efi_get_variable_int(var_name, &guid, &attributes,
&size, data, &time);
}
if (ret == EFI_NOT_FOUND) {
printf("Error: \"%ls\" not defined\n", var_name);
ret = CMD_RET_FAILURE;
goto out;
}
if (ret != EFI_SUCCESS) {
printf("Error: Cannot read variable, r = %lu\n",
ret & ~EFI_ERROR_MASK);
ret = CMD_RET_FAILURE;
goto out;
}
if (var_content_is_utf16_string) {
char *p;
len = utf16_utf8_strnlen((u16 *)data, size / 2);
strdata = malloc(len + 1);
if (!strdata) {
printf("## Out of memory\n");
ret = CMD_RET_FAILURE;
goto out;
}
p = strdata;
utf16_utf8_strncpy(&p, (u16 *)data, size / 2);
} else {
len = size * 2;
strdata = malloc(len + 1);
if (!strdata) {
printf("## Out of memory\n");
ret = CMD_RET_FAILURE;
goto out;
}
bin2hex(strdata, data, size);
}
strdata[len] = '\0';
ret = env_set(argv[2], strdata);
if (ret) {
ret = CMD_RET_FAILURE;
goto out;
}
ret = CMD_RET_SUCCESS;
out:
free(strdata);
free(data);
free(var_name);
return ret;
}
U_BOOT_CMD(
efigetenv, 5, 4, do_efi_get_env,
"set environment variable to content of EFI variable",
"[-s] name guid envvar\n"
" - set environment variable 'envvar' to the EFI variable 'name'-'guid'\n"
" \"-s\": Interpret the EFI variable value as a UTF-16 string\n"
);