teach 'env default' to optionally keep runtime variables
cover teach 'env default' to optionally keep runtime variables
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.
Rasmus Villemoes (3):
cmd/nvedit.c: teach 'env default' to optionally keep runtime variables
test: env: check that non-mentioned variables to "env default" are
preserved
test: env: add some test cases for new "env default -k" flag
cmd/nvedit.c | 8 ++++++-- test/env/cmd_ut_env.c | 45 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 5 deletions(-)
Signature · 1 line
2.47.0
PATCH 1/3 cmd/nvedit.c: teach 'env default' to optionally keep runtime variables
It can be useful to set all variables defined in the default environment to the value they have there, but without removing variables that are only defined at runtime. This can sort-of be done today, by using the "env default var1 var2 ..." variant, but that requires listing all variables defined in the default environment. It's much more convenient to be able to say
env default -k -a
The -k flag is also meaningful in the other case: If var1 is not defined in the default environment, but var2 is,
env default var1 var2
would emit a warning about var1 not being in the default env and thus being deleted. With -k, there's no warning, and var1 is kept as-is.
--- cmd/nvedit.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
cmd/nvedit.c +6 -2
@@ -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"
Signature · 1 line
2.47.0
PATCH 2/3 test: env: check that non-mentioned variables to "env default" are preserved
In reply to Rasmus VillemoesInstead of testing the same expected behaviour for both non_default_varX, test that when var1 is not in the default env but is mentioned in the "env default" cmdline, it is removed, while var2 is untouched.
--- test/env/cmd_ut_env.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
test/env/cmd_ut_env.c +3 -4
@@ -14,22 +14,21 @@ 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 (1 ,run_command ("env exists non_default_var2" ,0 )); +ut_asserteq (0 ,run_command ("env exists non_default_var2" ,0 ));ut_assert_console_end ();return 0 ;
Signature · 1 line
2.47.0
PATCH 3/3 test: env: add some test cases for new "env default -k" flag
In reply to Rasmus VillemoesCheck that the new -k flag works as expected.
This also adds a test of the -a flag, which was previously missing, and as the comment says, perhaps for a good reason. At least now we have a test for it in combination with -k (and -f, because the ethaddr variables otherwise cause complaining).
--- test/env/cmd_ut_env.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
test/env/cmd_ut_env.c +40 -0
@@ -31,6 +31,46 @@ static int env_test_env_cmd(struct unit_test_state *uts)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 )); +ut_assert_console_end (); + +ut_asserteq (1 ,run_command ("env exists non_default_var2" ,0 )); +ut_assert_console_end (); +#endif +return 0 ; }ENV_TEST (env_test_env_cmd,UTF_CONSOLE );
Signature · 1 line
2.47.0
Quoted 5 lines · Rasmus Villemoes
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.
Quoted 9 lines
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.
ping
Hi Rasmus,
Quoted 14 lines · Rasmus Villemoes
Quoted 11 lines · Rasmus Villemoes
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.
Quoted 3 lines
Quoted 3 lines
Looking around, the hashtab code already supports a flag that does exactly what I need, and exposing that is (morally) a one-liner.
I didn't see the patch, but can you add a test?
Regards,
Simon
Quoted 17 lines · Simon Glass
Hi Rasmus,
Quoted 14 lines · Rasmus Villemoes
Quoted 11 lines · Rasmus Villemoes
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.
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Looking around, the hashtab code already supports a flag that does exactly what I need, and exposing that is (morally) a one-liner.
I didn't see the patch, but can you add a test?
That's literally patch 3/3.
Rasmus
Quoted 5 lines · Rasmus Villemoes
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.
Quoted 7 lines
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.
[...]
Applied to u-boot/next, thanks!
Signature · 1 line
Tom