
From: Simon Glass <sjg@chromium.org> Change the default behavior of 'printenv -e' to show only EFI variable names. The previous verbose output is now available with the -v flag. This makes the command more user-friendly for quick variable listing. Update documentation to reflect the new behavior and provide examples of all three output modes: default (names only), -n (details without hex dump), and -v (full verbose output). It might be nicer to use -d to enable the dump, rather than have it on by default. Co-developed-by: Claude <noreply@anthropic.com> Signed-off-by: Simon Glass <sjg@chromium.org> --- cmd/nvedit.c | 5 +- cmd/nvedit_efi.c | 65 +++++++++++-------- doc/usage/cmd/env.rst | 7 +- doc/usage/cmd/printenv.rst | 20 ++++-- .../py/tests/test_efi_secboot/test_authvar.py | 46 ++++++------- 5 files changed, 84 insertions(+), 59 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 1f259801293..8dbe8a03fd5 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -1174,7 +1174,7 @@ U_BOOT_LONGHELP(env, #endif "env print [-a | name ...] - print environment\n" #if defined(CONFIG_CMD_NVEDIT_EFI) - "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n" + "env print -e [-guid guid] [-n] [-v] [name ...] print UEFI environment\n" #endif #if defined(CONFIG_CMD_RUN) "env run var [...] - run commands in an environment variable\n" @@ -1221,10 +1221,11 @@ U_BOOT_CMD_COMPLETE( "print environment variables", "[-a]\n - print [all] values of all environment variables\n" #if defined(CONFIG_CMD_NVEDIT_EFI) - "printenv -e [-guid guid][-n] [name ...]\n" + "printenv -e [-guid guid][-n] [-v] [name ...]\n" " - print UEFI variable 'name' or all the variables\n" " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n" " \"-n\": suppress dumping variable's value\n" + " \"-v\": show GUID, flags, size; also dump (without -n)\n" #endif "printenv name ...\n" " - print value of environment variable 'name'", diff --git a/cmd/nvedit_efi.c b/cmd/nvedit_efi.c index 32b7d049074..12c98a5d5c4 100644 --- a/cmd/nvedit_efi.c +++ b/cmd/nvedit_efi.c @@ -43,11 +43,13 @@ static const struct { * * @name: Name of the variable * @guid: Vendor GUID - * @verbose: if true, dump data + * @verbose: if true, show detailed information + * @nodump: if true, don't show hexadecimal dump * * Show information encoded in one UEFI variable */ -static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose) +static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, + bool verbose, bool nodump) { u32 attributes; u8 *data; @@ -75,23 +77,27 @@ static void efi_dump_single_var(u16 *name, const efi_guid_t *guid, bool verbose) if (ret != EFI_SUCCESS) goto out; - rtc_to_tm(time, &tm); - printf("%ls:\n %pUl (%pUs)\n", name, guid, guid); - if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) - printf(" %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year, - tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - printf(" "); - for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++) - if (attributes & efi_var_attrs[i].mask) { - if (count) - putc('|'); - count++; - puts(efi_var_attrs[i].text); - } - printf(", DataSize = 0x%zx\n", size); - if (verbose) - print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, - data, size, true); + if (verbose) { + rtc_to_tm(time, &tm); + printf("%ls:\n %pUl (%pUs)\n", name, guid, guid); + if (attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) + printf(" %04d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year, + tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); + printf(" "); + for (count = 0, i = 0; i < ARRAY_SIZE(efi_var_attrs); i++) + if (attributes & efi_var_attrs[i].mask) { + if (count) + putc('|'); + count++; + puts(efi_var_attrs[i].text); + } + printf(", DataSize = 0x%zx\n", size); + if (!nodump) + print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, + data, size, true); + } else { + printf("%ls\n", name); + } out: free(data); @@ -130,13 +136,15 @@ out: * * @argc: Number of arguments (variables) * @argv: Argument (variable name) array - * @verbose: if true, dump data + * @guid_p: GUID to filter by, or NULL for all + * @verbose: if true, show detailed information + * @nodump: if true, don't show hexadecimal dump * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE * * Show information encoded in all the UEFI variables */ static int efi_dump_var_all(int argc, char *const argv[], - const efi_guid_t *guid_p, bool verbose) + const efi_guid_t *guid_p, bool verbose, bool nodump) { u16 *var_name16, *p; efi_uintn_t buf_size, size; @@ -176,7 +184,7 @@ static int efi_dump_var_all(int argc, char *const argv[], continue; if (!argc || match_name(argc, argv, var_name16)) { match = true; - efi_dump_single_var(var_name16, &guid, verbose); + efi_dump_single_var(var_name16, &guid, verbose, nodump); } } free(var_name16); @@ -199,16 +207,18 @@ static int efi_dump_var_all(int argc, char *const argv[], * Return: CMD_RET_SUCCESS on success, or CMD_RET_RET_FAILURE * * This function is for "env print -e" or "printenv -e" command: - * => env print -e [-n] [-guid <guid> | -all] [var [...]] + * => env print -e [-v] [-guid <guid> | -all] [var [...]] * If one or more variable names are specified, show information * named UEFI variables, otherwise show all the UEFI variables. + * By default, only variable names are shown. Use -v for verbose output. */ int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { const efi_guid_t *guid_p = NULL; efi_guid_t guid; - bool verbose = true; + bool verbose = false; + bool nodump = false; efi_status_t ret; /* Initialize EFI drivers */ @@ -230,14 +240,17 @@ int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc, return CMD_RET_USAGE; guid_p = (const efi_guid_t *)guid.b; } else if (!strcmp(argv[0], "-n")) { - verbose = false; + verbose = true; + nodump = true; + } else if (!strcmp(argv[0], "-v")) { + verbose = true; } else { return CMD_RET_USAGE; } } /* enumerate and show all UEFI variables */ - return efi_dump_var_all(argc, argv, guid_p, verbose); + return efi_dump_var_all(argc, argv, guid_p, verbose, nodump); } /** diff --git a/doc/usage/cmd/env.rst b/doc/usage/cmd/env.rst index b65d85b6681..a1d90c6fca3 100644 --- a/doc/usage/cmd/env.rst +++ b/doc/usage/cmd/env.rst @@ -25,7 +25,7 @@ Synopsis env info [-d] [-p] [-q] env load env print [-a | name ...] - env print -e [-guid guid] [-n] [name ...] + env print -e [-guid guid] [-n] [-v] [name ...] env run var [...] env save env select [target] @@ -232,7 +232,10 @@ in UEFI variables. print only the UEFI variables matching this GUID (any by default) with guid format = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx". \-n - suppress dumping variable's value for UEFI. + don't show hexadecimal dump of value for UEFI variables. + \-v + show verbose output for UEFI variables including GUID, attributes, data + size and hexadecimal dump of value. Run ~~~ diff --git a/doc/usage/cmd/printenv.rst b/doc/usage/cmd/printenv.rst index dfdb3624934..cecfcf8bcda 100644 --- a/doc/usage/cmd/printenv.rst +++ b/doc/usage/cmd/printenv.rst @@ -12,7 +12,7 @@ Synopsis :: printenv [-a] [name ...] - printenv -e [-guid guid][-n] [name] + printenv -e [-guid guid][-n][-v] [name] Description ----------- @@ -32,6 +32,10 @@ The printenv command is used to print environment or UEFI variables. \-n don't show hexadecimal dump of value +\-v + show verbose output including GUID, attributes, data size and hexadecimal + dump of value (if not -n) + name Variable name. If no name is provided, all variables are printed. Multiple environment variable names may be specified. @@ -64,20 +68,24 @@ environment variables: Environment size: 653/8188 bytes => -The next example shows the effect of the *-n* flag when displaying an UEFI -variable and how to specify a vendor GUID: +The next example shows the different output modes when displaying UEFI +variables and how to specify a vendor GUID. By default, only the variable +name is shown. The *-v* flag shows full verbose output, while *-n* shows +details but omits the hexadecimal dump: :: - => printenv -e -guid 8be4df61-93ca-11d2-aa0d-00e098032b8c PlatformLangCodes + => printenv -e PlatformLangCodes + PlatformLangCodes + => printenv -e -v -n PlatformLangCodes PlatformLangCodes: 8be4df61-93ca-11d2-aa0d-00e098032b8c (EFI_GLOBAL_VARIABLE_GUID) BS|RT|RO, DataSize = 0x6 - 00000000: 65 6e 2d 55 53 00 en-US. - => printenv -e -n PlatformLangCodes + => printenv -e -v -guid 8be4df61-93ca-11d2-aa0d-00e098032b8c PlatformLangCodes PlatformLangCodes: 8be4df61-93ca-11d2-aa0d-00e098032b8c (EFI_GLOBAL_VARIABLE_GUID) BS|RT|RO, DataSize = 0x6 + 00000000: 65 6e 2d 55 53 00 en-US. => Configuration diff --git a/test/py/tests/test_efi_secboot/test_authvar.py b/test/py/tests/test_efi_secboot/test_authvar.py index 7b45f8fb814..3750f302dba 100644 --- a/test/py/tests/test_efi_secboot/test_authvar.py +++ b/test/py/tests/test_efi_secboot/test_authvar.py @@ -27,11 +27,11 @@ class TestEfiAuthVar(object): # Test Case 1a, Initial secure state output = ubman.run_command_list([ 'host bind 0 %s' % disk_img, - 'printenv -e SecureBoot']) + 'printenv -e -v SecureBoot']) assert '00000000: 00' in ''.join(output) output = ubman.run_command( - 'printenv -e SetupMode') + 'printenv -e -v SetupMode') assert '00000000: 01' in output with ubman.log.section('Test Case 1b'): @@ -46,14 +46,14 @@ class TestEfiAuthVar(object): output = ubman.run_command_list([ 'fatload host 0:1 4000000 PK.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK', - 'printenv -e -n PK']) + 'printenv -e -v PK']) assert 'PK:' in ''.join(output) output = ubman.run_command( - 'printenv -e SecureBoot') + 'printenv -e -v SecureBoot') assert '00000000: 01' in output output = ubman.run_command( - 'printenv -e SetupMode') + 'printenv -e -v SetupMode') assert '00000000: 00' in output with ubman.log.section('Test Case 1d'): @@ -78,11 +78,11 @@ class TestEfiAuthVar(object): output = ubman.run_command_list([ 'fatload host 0:1 4000000 KEK.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK', - 'printenv -e -n KEK']) + 'printenv -e -v KEK']) assert 'KEK:' in ''.join(output) output = ubman.run_command( - 'printenv -e SecureBoot') + 'printenv -e -v SecureBoot') assert '00000000: 01' in output with ubman.log.section('Test Case 1f'): @@ -95,12 +95,12 @@ class TestEfiAuthVar(object): output = ubman.run_command_list([ 'fatload host 0:1 4000000 db.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db', - 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + 'printenv -e -v -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) assert 'Failed to set EFI variable' not in ''.join(output) assert 'db:' in ''.join(output) output = ubman.run_command( - 'printenv -e SecureBoot') + 'printenv -e -v SecureBoot') assert '00000000: 01' in output with ubman.log.section('Test Case 1g'): @@ -113,12 +113,12 @@ class TestEfiAuthVar(object): output = ubman.run_command_list([ 'fatload host 0:1 4000000 dbx.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize dbx', - 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f dbx']) + 'printenv -e -v -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f dbx']) assert 'Failed to set EFI variable' not in ''.join(output) assert 'dbx:' in ''.join(output) output = ubman.run_command( - 'printenv -e SecureBoot') + 'printenv -e -v SecureBoot') assert '00000000: 01' in output def test_efi_var_auth2(self, ubman, efi_boot_env): @@ -137,7 +137,7 @@ class TestEfiAuthVar(object): 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK', 'fatload host 0:1 4000000 db.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db', - 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + 'printenv -e -v -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) assert 'Failed to set EFI variable' not in ''.join(output) assert 'db:' in ''.join(output) @@ -158,7 +158,7 @@ class TestEfiAuthVar(object): output = ubman.run_command_list([ 'fatload host 0:1 4000000 db1.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db', - 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + 'printenv -e -v -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) assert 'Failed to set EFI variable' not in ''.join(output) assert 'db:' in ''.join(output) @@ -178,7 +178,7 @@ class TestEfiAuthVar(object): 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK', 'fatload host 0:1 4000000 db.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db', - 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + 'printenv -e -v -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) assert 'Failed to set EFI variable' not in ''.join(output) assert 'db:' in ''.join(output) @@ -199,7 +199,7 @@ class TestEfiAuthVar(object): output = ubman.run_command_list([ 'fatload host 0:1 4000000 db2.auth', 'setenv -e -nv -bs -rt -at -a -i 4000000:$filesize db', - 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + 'printenv -e -v -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) assert 'Failed to set EFI variable' not in ''.join(output) assert 'db:' in ''.join(output) @@ -219,13 +219,13 @@ class TestEfiAuthVar(object): 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK', 'fatload host 0:1 4000000 db.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db', - 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + 'printenv -e -v -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) assert 'Failed to set EFI variable' not in ''.join(output) assert 'db:' in ''.join(output) output = ubman.run_command_list([ 'setenv -e -nv -bs -rt db', - 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + 'printenv -e -v -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) assert 'Failed to set EFI variable' in ''.join(output) assert 'db:' in ''.join(output) @@ -233,7 +233,7 @@ class TestEfiAuthVar(object): # Test Case 4b, update without correct signature/data output = ubman.run_command_list([ 'setenv -e -nv -bs -rt -at db', - 'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) + 'printenv -e -v -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f db']) assert 'Failed to set EFI variable' in ''.join(output) assert 'db:' in ''.join(output) @@ -253,14 +253,14 @@ class TestEfiAuthVar(object): 'setenv -e -nv -bs -rt -at -i 4000000:$filesize KEK', 'fatload host 0:1 4000000 db.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize db', - 'printenv -e -n PK']) + 'printenv -e -v PK']) assert 'Failed to set EFI variable' not in ''.join(output) assert 'PK:' in ''.join(output) output = ubman.run_command_list([ 'fatload host 0:1 4000000 PK_null.esl', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK', - 'printenv -e -n PK']) + 'printenv -e -v PK']) assert 'Failed to set EFI variable' in ''.join(output) assert 'PK:' in ''.join(output) @@ -269,13 +269,13 @@ class TestEfiAuthVar(object): output = ubman.run_command_list([ 'fatload host 0:1 4000000 PK_null.auth', 'setenv -e -nv -bs -rt -at -i 4000000:$filesize PK', - 'printenv -e -n PK']) + 'printenv -e -v PK']) assert 'Failed to set EFI variable' not in ''.join(output) assert '\"PK\" not defined' in ''.join(output) output = ubman.run_command( - 'printenv -e SecureBoot') + 'printenv -e -v SecureBoot') assert '00000000: 00' in output output = ubman.run_command( - 'printenv -e SetupMode') + 'printenv -e -v SetupMode') assert '00000000: 01' in output -- 2.43.0