diff options
author | Tom Rini <trini@konsulko.com> | 2024-11-15 16:36:24 -0600 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2024-11-15 16:36:24 -0600 |
commit | c71d451033db9ed1ea3ac46fa81a09ec9c58918c (patch) | |
tree | 7c0f20eb3af9f282c391f4d073fc426be350ef2c | |
parent | 030ec147af0b5f3fc9e74435f78878055687910b (diff) | |
parent | 1bf25c775010290bb4239180b17684b1657488fd (diff) |
Merge patch series "teach 'env default' to optionally keep runtime variables"
Rasmus Villemoes <ravi@prevas.dk> says:
Doing bringup of a board, part of my bootstrap logic is in U-Boot. So
when tweaking that logic, I was bitten by a previous completed
bootstrap having left a copy of the environment on the device, which
was imported and thus overrided the new logic.
So I thought, "ok, I'll just make sure to put 'env default -a' as the
first part of the bootstrap logic so I'm not bitten again". Alas, my
logic also relies on certain variables that are set by C code
(e.g. for detecting board variant), and doing 'env default -a' also
eliminates those.
Looking around, the hashtab code already supports a flag that does
exactly what I need, and exposing that is (morally) a one-liner.
Link: https://lore.kernel.org/r/20241030213404.2894247-1-ravi@prevas.dk
-rw-r--r-- | cmd/nvedit.c | 8 | ||||
-rw-r--r-- | test/env/cmd_ut_env.c | 45 |
2 files changed, 48 insertions, 5 deletions
diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 392f90f8698..1f259801293 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -523,6 +523,9 @@ static int do_env_default(struct cmd_tbl *cmdtp, int flag, case 'f': /* force */ env_flag |= H_FORCE; break; + case 'k': + env_flag |= H_NOCLEAR; + break; default: return cmd_usage(cmdtp); } @@ -1133,8 +1136,9 @@ U_BOOT_LONGHELP(env, #if defined(CONFIG_CMD_ENV_CALLBACK) "callbacks - print callbacks and their associated variables\nenv " #endif - "default [-f] -a - [forcibly] reset default environment\n" - "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n" + "default [-k] [-f] -a - [forcibly] reset default environment\n" + "env default [-k] [-f] var [...] - [forcibly] reset variable(s) to their default values\n" + " \"-k\": keep variables not defined in default environment\n" "env delete [-f] var [...] - [forcibly] delete variable(s)\n" #if defined(CONFIG_CMD_EDITENV) "env edit name - edit environment variable\n" diff --git a/test/env/cmd_ut_env.c b/test/env/cmd_ut_env.c index 4af05764fb8..9f16a978f2a 100644 --- a/test/env/cmd_ut_env.c +++ b/test/env/cmd_ut_env.c @@ -14,16 +14,54 @@ static int env_test_env_cmd(struct unit_test_state *uts) ut_assertok(run_command("setenv non_default_var1 1", 0)); ut_assert_console_end(); - ut_assertok(run_command("setenv non_default_var2 1", 0)); + ut_assertok(run_command("setenv non_default_var2 2", 0)); ut_assert_console_end(); ut_assertok(run_command("env print non_default_var1", 0)); ut_assert_nextline("non_default_var1=1"); ut_assert_console_end(); - ut_assertok(run_command("env default non_default_var1 non_default_var2", 0)); + ut_assertok(run_command("env default non_default_var1", 0)); ut_assert_nextline("WARNING: 'non_default_var1' not in imported env, deleting it!"); - ut_assert_nextline("WARNING: 'non_default_var2' not in imported env, deleting it!"); + ut_assert_console_end(); + + ut_asserteq(1, run_command("env exists non_default_var1", 0)); + ut_assert_console_end(); + + ut_asserteq(0, run_command("env exists non_default_var2", 0)); + ut_assert_console_end(); + + ut_assertok(run_command("setenv non_default_var1 3", 0)); + ut_assert_console_end(); + + ut_assertok(run_command("env default -k non_default_var1", 0)); + ut_assert_console_end(); + + ut_asserteq(0, run_command("env exists non_default_var1", 0)); + ut_assert_console_end(); + + ut_asserteq(0, run_command("env exists non_default_var2", 0)); + ut_assert_console_end(); + + ut_assertok(run_command("env default -k -a -f", 0)); + ut_assert_nextline("## Resetting to default environment"); + ut_assert_console_end(); + + ut_asserteq(0, run_command("env exists non_default_var1", 0)); + ut_assert_console_end(); + + ut_asserteq(0, run_command("env exists non_default_var2", 0)); + ut_assert_console_end(); + + /* + * While the following test of "env default -a" by itself + * works, it unfortunately causes an unrelated test case, + * env_test_fdt_import(), to fail, because the "from_fdt" + * variable would be removed. + */ +#if 0 + ut_assertok(run_command("env default -a", 0)); + ut_assert_nextline("## Resetting to default environment"); ut_assert_console_end(); ut_asserteq(1, run_command("env exists non_default_var1", 0)); @@ -31,6 +69,7 @@ static int env_test_env_cmd(struct unit_test_state *uts) ut_asserteq(1, run_command("env exists non_default_var2", 0)); ut_assert_console_end(); +#endif return 0; } |