cmd: env: extend "env [set|print] -e" to manage UEFI variables

With this patch, when setting UEFI variable with "env set -e" command,
we will be able to
- specify vendor guid with "-guid guid",
- specify variable attributes,  BOOTSERVICE_ACCESS, RUNTIME_ACCESS,
  respectively with "-bs" and "-rt",
- append a value instead of overwriting with "-a",
- use memory as variable's value instead of explicit values given
  at the command line with "-i address,size"

If guid is not explicitly given, default value will be used.

Meanwhile, "env print -e," will be modified so that it will NOT dump
a variable's value if '-n' is specified.

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
This commit is contained in:
AKASHI Takahiro 2019-10-24 15:17:13 +09:00 committed by Heinrich Schuchardt
parent ee5041451e
commit 051aa89f46
2 changed files with 253 additions and 45 deletions

View File

@ -1387,7 +1387,7 @@ static char env_help_text[] =
#endif #endif
"env print [-a | name ...] - print environment\n" "env print [-a | name ...] - print environment\n"
#if defined(CONFIG_CMD_NVEDIT_EFI) #if defined(CONFIG_CMD_NVEDIT_EFI)
"env print -e [name ...] - print UEFI environment\n" "env print -e [-guid guid|-all][-n] [name ...] - print UEFI environment\n"
#endif #endif
#if defined(CONFIG_CMD_RUN) #if defined(CONFIG_CMD_RUN)
"env run var [...] - run commands in an environment variable\n" "env run var [...] - run commands in an environment variable\n"
@ -1399,7 +1399,8 @@ static char env_help_text[] =
#endif #endif
#endif #endif
#if defined(CONFIG_CMD_NVEDIT_EFI) #if defined(CONFIG_CMD_NVEDIT_EFI)
"env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n" "env set -e [-nv][-bs][-rt][-a][-i addr,size][-v] name [arg ...]\n"
" - set UEFI variable; unset if '-i' or 'arg' not specified\n"
#endif #endif
"env set [-f] name [arg ...]\n"; "env set [-f] name [arg ...]\n";
#endif #endif
@ -1428,8 +1429,9 @@ U_BOOT_CMD_COMPLETE(
"print environment variables", "print environment variables",
"[-a]\n - print [all] values of all environment variables\n" "[-a]\n - print [all] values of all environment variables\n"
#if defined(CONFIG_CMD_NVEDIT_EFI) #if defined(CONFIG_CMD_NVEDIT_EFI)
"printenv -e [name ...]\n" "printenv -e [-guid guid|-all][-n] [name ...]\n"
" - print UEFI variable 'name' or all the variables\n" " - print UEFI variable 'name' or all the variables\n"
" \"-n\": suppress dumping variable's value\n"
#endif #endif
"printenv name ...\n" "printenv name ...\n"
" - print value of environment variable 'name'", " - print value of environment variable 'name'",
@ -1459,9 +1461,16 @@ U_BOOT_CMD_COMPLETE(
setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
"set environment variables", "set environment variables",
#if defined(CONFIG_CMD_NVEDIT_EFI) #if defined(CONFIG_CMD_NVEDIT_EFI)
"-e [-nv] name [value ...]\n" "-e [-guid guid][-nv][-bs][-rt][-a][-v]\n"
" [-i addr,size name], or [name [value ...]]\n"
" - set UEFI variable 'name' to 'value' ...'\n" " - set UEFI variable 'name' to 'value' ...'\n"
" 'nv' option makes the variable non-volatile\n" " \"-guid\": set vendor guid\n"
" \"-nv\": set non-volatile attribute\n"
" \"-bs\": set boot-service attribute\n"
" \"-rt\": set runtime attribute\n"
" \"-a\": append-write\n"
" \"-i addr,size\": use <addr,size> as variable's value\n"
" \"-v\": verbose message\n"
" - delete UEFI variable 'name' if 'value' not specified\n" " - delete UEFI variable 'name' if 'value' not specified\n"
#endif #endif
"setenv [-f] name value ...\n" "setenv [-f] name value ...\n"

View File

@ -13,6 +13,7 @@
#include <exports.h> #include <exports.h>
#include <hexdump.h> #include <hexdump.h>
#include <malloc.h> #include <malloc.h>
#include <mapmem.h>
#include <linux/kernel.h> #include <linux/kernel.h>
/* /*
@ -34,15 +35,49 @@ static const struct {
{EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"}, {EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS, "AT"},
}; };
static const struct {
efi_guid_t guid;
char *text;
} efi_guid_text[] = {
/* signature database */
{EFI_GLOBAL_VARIABLE_GUID, "EFI_GLOBAL_VARIABLE_GUID"},
};
/* "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
static char unknown_guid[37];
/**
* efi_guid_to_str() - convert guid to readable name
*
* @guid: GUID
* Return: string for GUID
*
* convert guid to readable name
*/
static const char *efi_guid_to_str(const efi_guid_t *guid)
{
int i;
for (i = 0; i < ARRAY_SIZE(efi_guid_text); i++)
if (!guidcmp(guid, &efi_guid_text[i].guid))
return efi_guid_text[i].text;
uuid_bin_to_str((unsigned char *)guid->b, unknown_guid,
UUID_STR_FORMAT_GUID);
return unknown_guid;
}
/** /**
* efi_dump_single_var() - show information about a UEFI variable * efi_dump_single_var() - show information about a UEFI variable
* *
* @name: Name of the variable * @name: Name of the variable
* @guid: Vendor GUID * @guid: Vendor GUID
* @verbose: if true, dump data
* *
* Show information encoded in one UEFI variable * Show information encoded in one UEFI variable
*/ */
static void efi_dump_single_var(u16 *name, efi_guid_t *guid) static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose)
{ {
u32 attributes; u32 attributes;
u8 *data; u8 *data;
@ -68,7 +103,7 @@ static void efi_dump_single_var(u16 *name, efi_guid_t *guid)
if (ret != EFI_SUCCESS) if (ret != EFI_SUCCESS)
goto out; goto out;
printf("%ls:", name); printf("%ls:\n %s:", name, efi_guid_to_str(guid));
for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++) for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++)
if (attributes & efi_var_attrs[i].mask) { if (attributes & efi_var_attrs[i].mask) {
if (count) if (count)
@ -79,7 +114,9 @@ static void efi_dump_single_var(u16 *name, efi_guid_t *guid)
puts(efi_var_attrs[i].text); puts(efi_var_attrs[i].text);
} }
printf(", DataSize = 0x%zx\n", size); printf(", DataSize = 0x%zx\n", size);
print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, data, size, true); if (verbose)
print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
data, size, true);
out: out:
free(data); free(data);
@ -90,11 +127,13 @@ out:
* *
* @argc: Number of arguments (variables) * @argc: Number of arguments (variables)
* @argv: Argument (variable name) array * @argv: Argument (variable name) array
* @verbose: if true, dump data
* Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
* *
* Show information encoded in named UEFI variables * Show information encoded in named UEFI variables
*/ */
static int efi_dump_vars(int argc, char * const argv[]) static int efi_dump_vars(int argc, char * const argv[],
const efi_guid_t *guid, bool verbose)
{ {
u16 *var_name16, *p; u16 *var_name16, *p;
efi_uintn_t buf_size, size; efi_uintn_t buf_size, size;
@ -119,8 +158,7 @@ static int efi_dump_vars(int argc, char * const argv[])
p = var_name16; p = var_name16;
utf8_utf16_strcpy(&p, argv[0]); utf8_utf16_strcpy(&p, argv[0]);
efi_dump_single_var(var_name16, efi_dump_single_var(var_name16, guid, verbose);
(efi_guid_t *)&efi_global_variable_guid);
} }
free(var_name16); free(var_name16);
@ -128,20 +166,56 @@ static int efi_dump_vars(int argc, char * const argv[])
return CMD_RET_SUCCESS; return CMD_RET_SUCCESS;
} }
static bool match_name(int argc, char * const argv[], u16 *var_name16)
{
char *buf, *p;
size_t buflen;
int i;
bool result = false;
buflen = utf16_utf8_strlen(var_name16) + 1;
buf = calloc(1, buflen);
if (!buf)
return result;
p = buf;
utf16_utf8_strcpy(&p, var_name16);
for (i = 0; i < argc; argc--, argv++) {
if (!strcmp(buf, argv[i])) {
result = true;
goto out;
}
}
out:
free(buf);
return result;
}
/** /**
* efi_dump_vars() - show information about all the UEFI variables * efi_dump_var_all() - show information about all the UEFI variables
* *
* @argc: Number of arguments (variables)
* @argv: Argument (variable name) array
* @verbose: if true, dump data
* Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
* *
* Show information encoded in all the UEFI variables * Show information encoded in all the UEFI variables
*/ */
static int efi_dump_var_all(void) static int efi_dump_var_all(int argc, char * const argv[],
const efi_guid_t *guid_p, bool verbose)
{ {
u16 *var_name16, *p; u16 *var_name16, *p;
efi_uintn_t buf_size, size; efi_uintn_t buf_size, size;
efi_guid_t guid; efi_guid_t guid;
efi_status_t ret; efi_status_t ret;
if (argc && guid_p)
/* simplified case */
return efi_dump_vars(argc, argv, guid_p, verbose);
buf_size = 128; buf_size = 128;
var_name16 = malloc(buf_size); var_name16 = malloc(buf_size);
if (!var_name16) if (!var_name16)
@ -171,7 +245,9 @@ static int efi_dump_var_all(void)
return CMD_RET_FAILURE; return CMD_RET_FAILURE;
} }
efi_dump_single_var(var_name16, &guid); if ((!guid_p || !guidcmp(guid_p, &guid)) &&
(!argc || match_name(argc, argv, var_name16)))
efi_dump_single_var(var_name16, &guid, verbose);
} }
free(var_name16); free(var_name16);
@ -189,12 +265,15 @@ static int efi_dump_var_all(void)
* Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
* *
* This function is for "env print -e" or "printenv -e" command: * This function is for "env print -e" or "printenv -e" command:
* => env print -e [var [...]] * => env print -e [-n] [-guid <guid> | -all] [var [...]]
* If one or more variable names are specified, show information * If one or more variable names are specified, show information
* named UEFI variables, otherwise show all the UEFI variables. * named UEFI variables, otherwise show all the UEFI variables.
*/ */
int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{ {
efi_guid_t guid;
const efi_guid_t *guid_p;
bool default_guid, guid_any, verbose;
efi_status_t ret; efi_status_t ret;
/* Initialize EFI drivers */ /* Initialize EFI drivers */
@ -205,12 +284,47 @@ int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
return CMD_RET_FAILURE; return CMD_RET_FAILURE;
} }
if (argc > 1) default_guid = true;
/* show specified UEFI variables */ guid_any = false;
return efi_dump_vars(--argc, ++argv); verbose = true;
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp(argv[0], "-guid")) {
if (argc == 1)
return CMD_RET_USAGE;
/* -a already specified */
if (!default_guid & guid_any)
return CMD_RET_USAGE;
argc--;
argv++;
if (uuid_str_to_bin(argv[0], guid.b,
UUID_STR_FORMAT_GUID))
return CMD_RET_USAGE;
default_guid = false;
} else if (!strcmp(argv[0], "-all")) {
/* -guid already specified */
if (!default_guid && !guid_any)
return CMD_RET_USAGE;
guid_any = true;
default_guid = false;
} else if (!strcmp(argv[0], "-n")) {
verbose = false;
} else {
return CMD_RET_USAGE;
}
}
if (guid_any)
guid_p = NULL;
else if (default_guid)
guid_p = &efi_global_variable_guid;
else
guid_p = (const efi_guid_t *)guid.b;
/* enumerate and show all UEFI variables */ /* enumerate and show all UEFI variables */
return efi_dump_var_all(); return efi_dump_var_all(argc, argv, guid_p, verbose);
} }
/** /**
@ -339,18 +453,22 @@ out:
* Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE
* *
* This function is for "env set -e" or "setenv -e" command: * This function is for "env set -e" or "setenv -e" command:
* => env set -e var [value ...]] * => env set -e [-guid guid][-nv][-bs][-rt][-a][-v]
* [-i address,size] var, or
* var [value ...]
* Encode values specified and set given UEFI variable. * Encode values specified and set given UEFI variable.
* If no value is specified, delete the variable. * If no value is specified, delete the variable.
*/ */
int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{ {
char *var_name, *value = NULL; char *var_name, *value, *ep;
efi_uintn_t size = 0; ulong addr;
u16 *var_name16 = NULL, *p; efi_uintn_t size;
size_t len;
efi_guid_t guid; efi_guid_t guid;
u32 attributes; u32 attributes;
bool default_guid, verbose, value_on_memory;
u16 *var_name16 = NULL, *p;
size_t len;
efi_status_t ret; efi_status_t ret;
if (argc == 1) if (argc == 1)
@ -364,32 +482,88 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
return CMD_RET_FAILURE; return CMD_RET_FAILURE;
} }
attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS | /*
EFI_VARIABLE_RUNTIME_ACCESS; * attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
if (!strcmp(argv[1], "-nv")) { * EFI_VARIABLE_RUNTIME_ACCESS;
attributes |= EFI_VARIABLE_NON_VOLATILE; */
argc--; value = NULL;
argv++; size = 0;
if (argc == 1) attributes = 0;
return CMD_RET_SUCCESS; guid = efi_global_variable_guid;
default_guid = true;
verbose = false;
value_on_memory = false;
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp(argv[0], "-guid")) {
if (argc == 1)
return CMD_RET_USAGE;
argc--;
argv++;
if (uuid_str_to_bin(argv[0], guid.b,
UUID_STR_FORMAT_GUID)) {
printf("## Guid not specified or in XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format\n");
return CMD_RET_FAILURE;
}
default_guid = false;
} else if (!strcmp(argv[0], "-bs")) {
attributes |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
} else if (!strcmp(argv[0], "-rt")) {
attributes |= EFI_VARIABLE_RUNTIME_ACCESS;
} else if (!strcmp(argv[0], "-nv")) {
attributes |= EFI_VARIABLE_NON_VOLATILE;
} else if (!strcmp(argv[0], "-a")) {
attributes |= EFI_VARIABLE_APPEND_WRITE;
} else if (!strcmp(argv[0], "-i")) {
/* data comes from memory */
if (argc == 1)
return CMD_RET_USAGE;
argc--;
argv++;
addr = simple_strtoul(argv[0], &ep, 16);
if (*ep != ',')
return CMD_RET_USAGE;
size = simple_strtoul(++ep, NULL, 16);
if (!size)
return CMD_RET_FAILURE;
value_on_memory = true;
} else if (!strcmp(argv[0], "-v")) {
verbose = true;
} else {
return CMD_RET_USAGE;
}
}
if (!argc)
return CMD_RET_USAGE;
var_name = argv[0];
if (default_guid)
guid = efi_global_variable_guid;
if (verbose) {
printf("GUID: %s\n", efi_guid_to_str((const efi_guid_t *)
&guid));
printf("Attributes: 0x%x\n", attributes);
} }
var_name = argv[1]; /* for value */
if (argc == 2) { if (value_on_memory)
/* delete */ value = map_sysmem(addr, 0);
value = NULL; else if (argc > 1)
size = 0; for (argc--, argv++; argc > 0; argc--, argv++)
} else { /* set */
argc -= 2;
argv += 2;
for ( ; argc > 0; argc--, argv++)
if (append_value(&value, &size, argv[0]) < 0) { if (append_value(&value, &size, argv[0]) < 0) {
printf("## Failed to process an argument, %s\n", printf("## Failed to process an argument, %s\n",
argv[0]); argv[0]);
ret = CMD_RET_FAILURE; ret = CMD_RET_FAILURE;
goto out; goto out;
} }
if (size && verbose) {
printf("Value:\n");
print_hex_dump(" ", DUMP_PREFIX_OFFSET,
16, 1, value, size, true);
} }
len = utf8_utf16_strnlen(var_name, strlen(var_name)); len = utf8_utf16_strnlen(var_name, strlen(var_name));
@ -402,17 +576,42 @@ int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
p = var_name16; p = var_name16;
utf8_utf16_strncpy(&p, var_name, len + 1); utf8_utf16_strncpy(&p, var_name, len + 1);
guid = efi_global_variable_guid;
ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes, ret = EFI_CALL(efi_set_variable(var_name16, &guid, attributes,
size, value)); size, value));
unmap_sysmem(value);
if (ret == EFI_SUCCESS) { if (ret == EFI_SUCCESS) {
ret = CMD_RET_SUCCESS; ret = CMD_RET_SUCCESS;
} else { } else {
printf("## Failed to set EFI variable\n"); const char *msg;
switch (ret) {
case EFI_NOT_FOUND:
msg = " (not found)";
break;
case EFI_WRITE_PROTECTED:
msg = " (read only)";
break;
case EFI_INVALID_PARAMETER:
msg = " (invalid parameter)";
break;
case EFI_SECURITY_VIOLATION:
msg = " (validation failed)";
break;
case EFI_OUT_OF_RESOURCES:
msg = " (out of memory)";
break;
default:
msg = "";
break;
}
printf("## Failed to set EFI variable%s\n", msg);
ret = CMD_RET_FAILURE; ret = CMD_RET_FAILURE;
} }
out: out:
free(value); if (value_on_memory)
unmap_sysmem(value);
else
free(value);
free(var_name16); free(var_name16);
return ret; return ret;