Skip to contentSkip to conversation

implement set individual variables to default

PATCH New Gerlando Falauto 2 participants 10 messages Sep 22, 2011 – Oct 21, 2011

lore
Share & tools
lore
#0 lore raw

cover implement set individual variables to default

This patch seriese implements the feature for resetting individual environment variables to their default values as described in:
http://www.denx.de/wiki/U-Boot/TaskSetEnvironmentDefaults
and enables it for all keymile boards. see commit msg of first patch for command usage.

Initial RFC was posted and the inputs were taken into account:
http://lists.denx.de/pipermail/u-boot/2011-September/100635.html

Gerlando Falauto (2):
env: set individual variables to default km/common: enable "env default" for all keymile boards

 README                              |    2 +
 common/cmd_nvedit.c                 |   22 ++++++--
 common/env_common.c                 |  109 +++++++++++++++++++++++++++++++++++
 include/config_cmd_all.h            |    1 +
 include/configs/km/keymile-common.h |    1 +
 include/environment.h               |    5 ++
 6 files changed, 135 insertions(+), 5 deletions(-)
#1 lore raw

PATCH 1/2 env: set individual variables to default

implement command "env default <vars>..." for resetting individual variables to their default values.

"env default -f" will always keep resetting the whole environment to default.

Notes:
- The feature is disabled by default; define CONFIG_CMD_DEFAULTENV_VARS to enable it.
- An existing variable not defined in the default environemnt will be unset.

---
 README                   |    2 +
 common/cmd_nvedit.c      |   22 +++++++--
 common/env_common.c      |  109 ++++++++++++++++++++++++++++++++++++++++++++++
 include/config_cmd_all.h |    1 +
 include/environment.h    |    5 ++
 5 files changed, 134 insertions(+), 5 deletions(-)
README +2 -0
index a43da97..4b28854 100644
@@ -705,6 +705,8 @@ The following options need to be configured:
 		CONFIG_CMD_CONSOLE	  coninfo
 		CONFIG_CMD_CRC32	* crc32
 		CONFIG_CMD_DATE		* support for RTC, date/time...
+		CONFIG_CMD_DEFAULTENV_VARS
+					* Reset individual variables to default
 		CONFIG_CMD_DHCP		* DHCP support
 		CONFIG_CMD_DIAG		* Diagnostics
 		CONFIG_CMD_DS4510	* ds4510 I2C gpio commands
common/cmd_nvedit.c +17 -5
index e8b116d..ea1db22 100644
@@ -581,11 +581,20 @@ int envmatch(uchar *s1, int i2)
 
 static int do_env_default(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	if ((argc != 2) || (strcmp(argv[1], "-f") != 0))
-		return cmd_usage(cmdtp);
-
-	set_default_env("## Resetting to default environment\n");
-	return 0;
+	if ((argc == 2) && (strcmp(argv[1], "-f")) == 0) {
+		/* Reset the whole environment */
+		set_default_env("## Resetting to default environment\n");
+		return 0;
+	}
+#ifdef CONFIG_CMD_DEFAULTENV_VARS
+	/* Check that we have at least one argument */
+	else if (argc >= 2) {
+		/* Reset individual variables */
+		env_default_vars(argc-1, argv+1);
+		return 0;
+	}
+#endif
+	return cmd_usage(cmdtp);
 }
 
 static int do_env_delete(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
@@ -911,6 +920,9 @@ U_BOOT_CMD(
 	"ask name [message] [size] - ask for environment variable\nenv "
 #endif
 	"default -f - reset default environment\n"
+#if defined(CONFIG_CMD_DEFAULTENV_VARS)
+	"env default var [...] - reset variable(s) to their default value\n"
+#endif
 #if defined(CONFIG_CMD_EDITENV)
 	"env edit name - edit environment variable\n"
 #endif
common/env_common.c +109 -0

115 lines not on this page · raw message

include/config_cmd_all.h +1 -0
index 9716f9c..e728eae 100644
@@ -25,6 +25,7 @@
 #define CONFIG_CMD_CDP		/* Cisco Discovery Protocol	*/
 #define CONFIG_CMD_CONSOLE	/* coninfo			*/
 #define CONFIG_CMD_DATE		/* support for RTC, date/time...*/
+#define CONFIG_CMD_DEFAULTENV_VARS /* default individ variables */
 #define CONFIG_CMD_DHCP		/* DHCP Support			*/
 #define CONFIG_CMD_DIAG		/* Diagnostics			*/
 #define CONFIG_CMD_DISPLAY	/* Display support		*/
include/environment.h +5 -0
index 6394a96..0be92b6 100644
@@ -171,6 +171,11 @@ void env_crc_update (void);
 /* [re]set to the default environment */
 void set_default_env(const char *s);
 
+#ifdef CONFIG_CMD_DEFAULTENV_VARS
+/* [re]set individual variables to their value in the default environment */
+int env_default_vars(int nvars, char * const vars[]);
+#endif
+
 /* Import from binary representation into hash table */
 int env_import(const char *buf, int check);
 
Signature · 1 line
1.7.1
#2 lore raw

PATCH 1/2 env: set individual variables to default

Dear Gerlando Falauto,

Quoted 5 lines · In message <[email protected]> you…

implement command "env default <vars>..." for resetting individual variables to their default values.

"env default -f" will always keep resetting the whole environment to default.

I don't think this is a good idea.

The "-f" should be used to allow resetting write-protected variables like serial# or ethaddr. With no variable names given, it will therefore always be needed (assuming you have such protected variables in your environment). But it should also work when arguments are given, i. e.

env default ethaddr

is supposed to fail, because ethaddr is write protected. To reset this, you should use

env default -f ethaddr

common/env_common.c void set_default_env(const char *s) · line 203

Quoted 5 lines
common/env_common.c +5 -0
+#ifdef CONFIG_CMD_DEFAULTENV_VARS
+/*
+ * import individual variables from an external environment
+ * (e.g. default environment).
+ * Most of this code comes straight from himport_r().

Indeed. This is way too much code copied. Please factor this out into a separate function to avoid such duplication (which is always a maintenance nightmare).

...

common/env_common.c void set_default_env(const char *s) · line 254

Quoted 6 lines
common/env_common.c +6 -0
+			/* deal with "name" and "name=" entries (delete var) */
+			if (*dp == '\0' || *(dp + 1) == '\0' ||
+				*dp == sep || *(dp + 1) == sep) {
+				if (*dp == '=')
+					*dp++ = '\0';
+				*dp++ = '\0';	/* terminate name */

Here the comment (about the "delete var") is even wrong.

common/env_common.c void set_default_env(const char *s) · line 283

Quoted 4 lines
common/env_common.c +4 -0
+		/*
+		 * size check needed for text without '\0' termination
+		 * (e.g. default environment)
+		 */

What makes you think the default environment was NOT '\0' terminated?

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
It became apparent that one reason why the Ice Giants were  known  as
the  Ice  Giants  was  because they were, well, giants. The other was
that they were made of ice.              -Terry Pratchett, _Sourcery_
#3 lore raw

PATCH 1/2 env: set individual variables to default

Quoted 7 lines · Wolfgang Denk
Quoted 2 lines

"env default -f" will always keep resetting the whole environment to default.

I don't think this is a good idea.

The "-f" should be used to allow resetting write-protected variables like serial# or ethaddr.

I see what you mean, it's the same rationale behind

env set [-f] var

right? Point is, has "-f" ever been implemented in "env set"?

=> env set -f ethaddr
=> printenv
-f=ethaddr
...

Quoted 2 lines

With no variable names given, it will therefore always be needed (assuming you have such protected variables in your environment).

OK, so "env default -f" should reset all variables, including protected ones. Should there also be some way to reset *ALL BUT* protected variables? Like "env default" or "env default all" as suggested in the wiki page? Or such operation doesn't make any sense indeed (i.e., all variables means ALL OF THEM)?

Quoted 9 lines

common/env_common.c void set_default_env(const char *s) · line 203

Quoted 5 lines
common/env_common.c +5 -0
+#ifdef CONFIG_CMD_DEFAULTENV_VARS
+/*
+ * import individual variables from an external environment
+ * (e.g. default environment).
+ * Most of this code comes straight from himport_r().

Indeed. This is way too much code copied. Please factor this out into a separate function to avoid such duplication (which is always a maintenance nightmare).

While we're on this subject, do you think it would make any sense to *import* individual variables?
If yes, what could the syntax be?

env import [flags] addr [size] <vars...>

would make the optional argument "size" ambiguous (unless we take for granted that variable names cannot start with a digit...).

Quoted 9 lines

...

common/env_common.c void set_default_env(const char *s) · line 254

Quoted 6 lines
common/env_common.c +6 -0
+			/* deal with "name" and "name=" entries (delete var) */
+			if (*dp == '\0' || *(dp + 1) == '\0' ||
+				*dp == sep || *(dp + 1) == sep) {
+				if (*dp == '=')
+					*dp++ = '\0';
+				*dp++ = '\0';	/* terminate name */

Here the comment (about the "delete var") is even wrong.

What do you mean? It's what himport_r() does (but please see also my final comments).

Quoted 7 lines

common/env_common.c void set_default_env(const char *s) · line 283

Quoted 5 lines
common/env_common.c +4 -0
+		/*
+		 * size check needed for text without '\0' termination
+		 * (e.g. default environment)
+		 */

What makes you think the default environment was NOT '\0' terminated?

Sorry, my mistake.

Anyway, the whole thing was based on the assumption that it could make some sense at some point to import individual variables from a downloaded file. If that's the case, code should be factored together with himport_r() as you suggested. If not, most of this code (the part checking for comments, tabs, '\0'...) can be safely deleted (and I don't think factoring the rest would be necessary).

Thank you,
Gerlando Falauto

#4 lore raw

PATCH 1/2 env: set individual variables to default

Dear Gerlando Falauto,

Quoted 6 lines · In message <[email protected]> you wrote:

I see what you mean, it's the same rationale behind

env set [-f] var

right? Point is, has "-f" ever been implemented in "env set"?

No, it has not (and there is also no such thing as "setenv -f ..."). Not yet, that is :-)

Quoted 3 lines

OK, so "env default -f" should reset all variables, including protected ones. Should there also be some way to reset *ALL BUT* protected variables? Like "env default" or "env default all" as suggested in the wiki page?

Yes, that would be nice. With a warning about the not changed ones.

Quoted 2 lines

While we're on this subject, do you think it would make any sense to *import* individual variables?

Tough question. In theory yes, it would make perfect sense. On the other hand, this is a boot loader, and we should not try to be feature-complete. So far I haven't seen a use case for this.

Quoted 3 lines

If yes, what could the syntax be?

env import [flags] addr [size] <vars...>

env import -n name[,..] [other_flags] addr [size]

?

Quoted 2 lines

would make the optional argument "size" ambiguous (unless we take for granted that variable names cannot start with a digit...).

But they can, at least until now.

Quoted 10 lines
Quoted 9 lines

...

common/env_common.c void set_default_env(const char *s) · line 254

Quoted 6 lines
common/env_common.c +6 -0
+			/* deal with "name" and "name=" entries (delete var) */
+			if (*dp == '\0' || *(dp + 1) == '\0' ||
+				*dp == sep || *(dp + 1) == sep) {
+				if (*dp == '=')
+					*dp++ = '\0';
+				*dp++ = '\0';	/* terminate name */

Here the comment (about the "delete var") is even wrong.

Quoted 2 lines

What do you mean? It's what himport_r() does (but please see also my final comments).

Yes, but himport_r() actually does the delete:

		...
		if (hdelete_r(name, htab) == 0)
			debug("DELETE ERROR ##############################\n");
		...

You removed this part, so the comment became wrong.

Quoted 6 lines

Anyway, the whole thing was based on the assumption that it could make some sense at some point to import individual variables from a downloaded file. If that's the case, code should be factored together with himport_r() as you suggested. If not, most of this code (the part checking for comments, tabs, '\0'...) can be safely deleted (and I don't think factoring the rest would be necessary).

I agree that it makes sense to generalize and clean up this interface. It makes sense to select individual variables, and it makes sense to unify the "-f" handling to enforce actions on protected variables (while without "-f" only actions on the "normal" variables should be done).

I can even imagine introducing a new variable that contains the name of the write-protected variables (and probably other properties, like being excluded from saveenv, etc.) - this has been discussed a number of times before, now we have the code base in place to actually implement it.

All we need to do is extend the struct entry (in "include/search.h") by an "int flags"), and we can there register properties like read-only, don't-save etc. In a first step this could be added transparently - so we could remove all the special handling of "ethaddr", "serial#" etc. in common/cmd_nvedit.c; then we could unify this to include "eth1addr" etc as well; then we could extend it to read the names of such variables and their properties from a variable, etc.

... there is plenty of ideas someone could pick up...

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"The bad reputation UNIX has gotten is totally undeserved, laid on by
people who don't understand, who have not gotten in there  and  tried
anything."          -- Jim Joyce, owner of Jim Joyce's UNIX Bookstore
#5 lore raw

PATCH 1/2 env: set individual variables to default

On 09/23/2011 11:55 AM, Wolfgang Denk wrote:

[...]

Quoted 6 lines
Quoted 2 lines

While we're on this subject, do you think it would make any sense to *import* individual variables?

Tough question. In theory yes, it would make perfect sense. On the other hand, this is a boot loader, and we should not try to be feature-complete. So far I haven't seen a use case for this.

Well, at Keymile we are essentially using "env import" as a way to have "default" variables stored in an external file (e.g., because they are only needed for development and not for production, and that not only reduces the site of the environments, but also makes updates easier). To this extent, "env import" and "env default" sort of have the same purpose...

Quoted 7 lines
Quoted 3 lines

If yes, what could the syntax be?

env import [flags] addr [size]<vars...>

env import -n name[,..] [other_flags] addr [size]

?

Uhm, wouldn't that make the syntax completely unrelated to all other commands, leading to confusion?

How about something like:

env import [-f] [flags] addr [size] [-n name[ ...]] env default [-f] -a|-n name[ ...]
env set [-f] name [val ...]

?
Where:
-a in "env default" would be the way to prevent the inadverent user
from wrecking the environment by mistake.
-f forces overwriting of read-only or write-once variables -n allows to be selective about which variables should be restored
from default/external environment

Quoted 4 lines
Quoted 2 lines

would make the optional argument "size" ambiguous (unless we take for granted that variable names cannot start with a digit...).

But they can, at least until now.

Slightly off-topic: how about variables starting with "-"?

[...]

Quoted 28 lines
Quoted 6 lines

Anyway, the whole thing was based on the assumption that it could make some sense at some point to import individual variables from a downloaded file. If that's the case, code should be factored together with himport_r() as you suggested. If not, most of this code (the part checking for comments, tabs, '\0'...) can be safely deleted (and I don't think factoring the rest would be necessary).

I agree that it makes sense to generalize and clean up this interface. It makes sense to select individual variables, and it makes sense to unify the "-f" handling to enforce actions on protected variables (while without "-f" only actions on the "normal" variables should be done).

I can even imagine introducing a new variable that contains the name of the write-protected variables (and probably other properties, like being excluded from saveenv, etc.) - this has been discussed a number of times before, now we have the code base in place to actually implement it.

All we need to do is extend the struct entry (in "include/search.h") by an "int flags"), and we can there register properties like read-only, don't-save etc. In a first step this could be added transparently - so we could remove all the special handling of "ethaddr", "serial#" etc. in common/cmd_nvedit.c; then we could unify this to include "eth1addr" etc as well; then we could extend it to read the names of such variables and their properties from a variable, etc.

Quoted 2 lines

... there is plenty of ideas someone could pick up...

OK, one step at a time.
But it's definitely worth knowing your thoughts before starting. Thank you for sharing them!

Best,
Gerlando Falauto

#6 lore raw

PATCH 1/2 env: set individual variables to default

Dear Gerlando Falauto,

Quoted 5 lines · In message <[email protected]> you wrote:
Quoted 4 lines

env import -n name[,..] [other_flags] addr [size]

?

Quoted 8 lines

Uhm, wouldn't that make the syntax completely unrelated to all other commands, leading to confusion?

How about something like:

env import [-f] [flags] addr [size] [-n name[ ...]] env default [-f] -a|-n name[ ...]
env set [-f] name [val ...]

No. That's even worse. If you don't like the comma separation,we could as well accept multiple -n args:

env import -n name [-n name1 ... ] [other_flags] addr [size]

Quoted 3 lines

Where:
-a in "env default" would be the way to prevent the inadverent user
from wrecking the environment by mistake.

What does -a stand for? "all" ?

Quoted 1 line

Slightly off-topic: how about variables starting with "-"?

Should be no problem in general, except for the pathological cases
like variable names "-f" etc. But there has to be a penalty for such
stupid names :-)

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Insufficient facts always invite danger.
	-- Spock, "Space Seed", stardate 3141.9
#7 lore raw

PATCH 1/2 env: set individual variables to default

Quoted 11 lines · Wolfgang Denk

Dear Gerlando Falauto,

Quoted 8 lines · In message<[email protected]> you wrote:
Quoted 4 lines

env import -n name[,..] [other_flags] addr [size]

?

Uhm, wouldn't that make the syntax completely unrelated to all other commands, leading to confusion?

Quoted 7 lines
Quoted 5 lines

How about something like:

env import [-f] [flags] addr [size] [-n name[ ...]] env default [-f] -a|-n name[ ...]
env set [-f] name [val ...]

No. That's even worse.

You mean that options should always precede the main argument?

Quoted 4 lines

If you don't like the comma separation,we
could as well accept multiple -n args:

env import -n name [-n name1 ... ] [other_flags] addr [size]

It's not that I don't like comma separation. It's just that maybe the syntax should be consistent between commands... that's all. But since it's a very unusual (and possibly unused) command, I am probably worrying too much.

Quoted 5 lines
Quoted 3 lines

Where:
-a in "env default" would be the way to prevent the inadverent user
from wrecking the environment by mistake.

What does -a stand for? "all" ?

Yes. Any comments on this?
How should we implement "env default" for the whole env?
1) "env default"
2) "env deafult -a"
3) "env default all"

Quoted 5 lines
Quoted 1 line

Slightly off-topic: how about variables starting with "-"?

Should be no problem in general, except for the pathological cases
like variable names "-f" etc. But there has to be a penalty for such
stupid names :-)

I can't think of any shell or programming language allowing for variables starting with "-" or digits...

Thasnks,
Gerlando Falauto

#8 lore raw

PATCH 2/2 km/common: enable "env default" for all keymile boards

In reply to Gerlando Falauto
---
 include/configs/km/keymile-common.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
include/configs/km/keymile-common.h +1 -0
index bc1edaf..501227f 100644
@@ -37,6 +37,7 @@
 
 #define CONFIG_CMD_ASKENV
 #define CONFIG_CMD_DHCP
+#define CONFIG_CMD_DEFAULTENV_VARS
 #define CONFIG_CMD_ECHO
 #define CONFIG_CMD_IMMAP
 #define CONFIG_CMD_MII
Signature · 1 line
1.7.1
#9 lore raw

PATCH 2/2 km/common: enable "env default" for all keymile boards

Dear Gerlando Falauto,

Quoted 5 lines · In message <[email protected]> you…
---
 include/configs/km/keymile-common.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
If the hours are long enough and the pay  is  short  enough,  someone
will say it's women's work.