[U-Boot] [PATCH] Add "memenv" command, to set environment from a memory area

Alessandro Rubini rubini-list at gnudd.com
Fri Oct 31 23:18:26 CET 2008


From: Alessandro Rubini <rubini at unipv.it>

This command reads a memory area (up to a specified length) and
assigns the content to an environment variable. Any newlines are turned
into semicolons, and any hash is makes all the rest of the line to be turned
into spaces.

The command is meant to allow downloading a shell script from the network
or a storage device and then run it using the "run" command. This greatly
simplifies the task of upgrading a system from within the boot loader,
or just running some tests automatically, without the need to define the
specific commands at compile time.

Signed-off-by: Alessandro Rubini <rubini at unipv.it>
---
 README              |    2 ++
 common/cmd_nvedit.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/README b/README
index ebee20f..ef989b4 100644
--- a/README
+++ b/README
@@ -594,6 +594,7 @@ The following options need to be configured:
 		CONFIG_CMD_KGDB		* kgdb
 		CONFIG_CMD_LOADB	  loadb
 		CONFIG_CMD_LOADS	  loads
+		CONFIG_CMD_MEMENV	* set environment from memory
 		CONFIG_CMD_MEMORY	  md, mm, nm, mw, cp, cmp, crc, base,
 					  loop, loopw, mtest
 		CONFIG_CMD_MISC		  Misc functions like sleep etc
@@ -2802,6 +2803,7 @@ sspi	- SPI utility commands
 base	- print or set address offset
 printenv- print environment variables
 setenv	- set environment variables
+memenv	- set variable from memory are, stripping newlines
 saveenv - save environment variables to persistent storage
 protect - enable or disable FLASH write protection
 erase	- erase FLASH memory
diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index d280cb0..2c4581b 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -488,6 +488,44 @@ int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 #endif
 
 /************************************************************************
+ * Prompt for environment variable
+ */
+
+#if defined(CONFIG_CMD_MEMENV)
+int do_memenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	unsigned long len, i;
+	char *addr;
+
+	if (argc != 4) {
+		printf ("Usage:\n%s\n", cmdtp->usage);
+		return 1;
+	}
+	addr = (char *)simple_strtol(argv[2], NULL, 16);
+	len = simple_strtol(argv[3], NULL, 16);
+	if (!addr || !len) {
+		printf ("Usage:\n%s\n", cmdtp->usage);
+		return 1;
+	}
+	addr[len] = '\0';
+	for (i=0; i<len; i++) {
+		/* turn newlines into semicolon */
+		if (addr[i]=='\n') addr[i] = ';';
+		/* ignore dos-style newlines */
+		if (addr[i]=='\r') addr[i] = ' ';
+		/* accept sh-comments and discard them */
+		if (addr[i]=='#') {
+			while (addr[i] && addr[i] != '\n')
+				addr[i++] = ' ';
+			i--;
+		}
+	}
+	setenv(argv[1], addr);
+	return 0;
+}
+#endif /* CMD_MEMENV */
+
+/************************************************************************
  * Look up variable from environment,
  * return address of storage for that variable,
  * or NULL if not found
@@ -626,6 +664,17 @@ U_BOOT_CMD(
 );
 #endif
 
+#if defined(CONFIG_CMD_MEMENV)
+
+U_BOOT_CMD(
+	memenv, 4, 0, do_memenv,
+	"memenv  - get multi-line environment variable from memory area\n",
+	"name addr maxlen\n"
+	"    - set multi-line environment variable 'name' from addr 'addr'\n"
+);
+
+#endif
+
 #if defined(CONFIG_CMD_RUN)
 int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
 U_BOOT_CMD(
-- 
1.6.0.2


More information about the U-Boot mailing list