[PATCH] env: introduce CONFIG_ENV_DOTVARS_TEMPORARY

Rasmus Villemoes rasmus.villemoes at prevas.dk
Wed Jan 8 14:42:57 CET 2020


The printenv command already by default hides variables beginning with
a dot. It can be useful to take that convention even further, and
prevent such variables from ever being stored persistently (and
ignored if they happen to exist in stable storage).

This way, one can freely use such variable names in script logic,
without worrying about random temporary variables leaking to
persistent storage and/or to/from another U-boot "session".

Shell variables can be used somewhat similarly, but they are not as
flexible, since many helper commands (e.g. setexpr, fdt) offer to
store their output in an environment variable, not a shell variable.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
---
 env/Kconfig     | 10 ++++++++++
 env/common.c    |  6 ++++--
 lib/hashtable.c |  3 +++
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/env/Kconfig b/env/Kconfig
index 4661082f0e..69fd2cae03 100644
--- a/env/Kconfig
+++ b/env/Kconfig
@@ -559,6 +559,16 @@ config SYS_RELOC_GD_ENV_ADDR
 	  Relocate the early env_addr pointer so we know it is not inside
 	  the binary. Some systems need this and for the rest, it doesn't hurt.
 
+config ENV_DOTVARS_TEMPORARY
+	bool "Ignore variables beginning with . when saving/loading the environment"
+	help
+	  If you select this option, environment variable names
+	  beginning with a dot (.) are skipped when writing the
+	  environment to persistent storage. Similarly, should the
+	  persistent storage somehow contain such a variable, it is
+	  ignored (i.e. not added to the runtime environment) when
+	  loading.
+
 config USE_DEFAULT_ENV_FILE
 	bool "Create default environment from file"
 	help
diff --git a/env/common.c b/env/common.c
index 0da21ee081..c23b490364 100644
--- a/env/common.c
+++ b/env/common.c
@@ -116,6 +116,7 @@ int env_set_default_vars(int nvars, char * const vars[], int flags)
 int env_import(const char *buf, int check)
 {
 	env_t *ep = (env_t *)buf;
+	int flag = IS_ENABLED(CONFIG_ENV_DOTVARS_TEMPORARY) ? H_HIDE_DOT : 0;
 
 	if (check) {
 		uint32_t crc;
@@ -128,7 +129,7 @@ int env_import(const char *buf, int check)
 		}
 	}
 
-	if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0,
+	if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flag, 0,
 			0, NULL)) {
 		gd->flags |= GD_FLG_ENV_READY;
 		return 0;
@@ -212,9 +213,10 @@ int env_export(env_t *env_out)
 {
 	char *res;
 	ssize_t	len;
+	int flag = IS_ENABLED(CONFIG_ENV_DOTVARS_TEMPORARY) ? H_HIDE_DOT : 0;
 
 	res = (char *)env_out->data;
-	len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
+	len = hexport_r(&env_htab, '\0', flag, &res, ENV_SIZE, 0, NULL);
 	if (len < 0) {
 		pr_err("Cannot export environment: errno = %d\n", errno);
 		return 1;
diff --git a/lib/hashtable.c b/lib/hashtable.c
index 907e8a642f..e05a097c75 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -928,6 +928,9 @@ int himport_r(struct hsearch_data *htab,
 		if (!drop_var_from_set(name, nvars, localvars))
 			continue;
 
+		if ((flag & H_HIDE_DOT) && *name == '.')
+			continue;
+
 		/* enter into hash table */
 		e.key = name;
 		e.data = value;
-- 
2.23.0



More information about the U-Boot mailing list