Skip to contentSkip to conversation

teach 'env default' to optionally keep runtime variables

PATCH In review Rasmus Villemoes 3 participants 8 messages Oct 30, 2024 – Nov 16, 2024

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 392f90f8698..1f259801293 100644
@@ -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
#2 lore raw

PATCH 2/3 test: env: check that non-mentioned variables to "env default" are preserved

In reply to Rasmus Villemoes

Instead 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
index 4af05764fb8..e1dd1a14cb0 100644
@@ -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
#3 lore raw

PATCH 3/3 test: env: add some test cases for new "env default -k" flag

In reply to Rasmus Villemoes

Check 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
index e1dd1a14cb0..9f16a978f2a 100644
@@ -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
#4 lore raw
In reply to Rasmus Villemoes
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

#5 lore raw

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

#6 lore raw
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

#7 lore raw
In reply to Rasmus Villemoes
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