[U-Boot] [PATCH] Allow U-Boot scripts to be placed in a .env file

Simon Glass sjg at chromium.org
Wed Apr 17 01:36:53 CEST 2013


At present U-Boot environment variables, and thus scripts, are defined
by CONFIG_EXTRA_ENV_SETTINGS. It is painful to add large amounts of text
to this file and dealing with quoting and newlines is harder than it
should be. It would be better if we could just type the script into a
text file and have it included by U-Boot.

Add a feature that brings in a .env file associated with the board
config, if present. To use it, create a file in include/configs
with the same name as you could board config file, except with a
.env extension instead of a .h extension. The variables should be
separated by \0. Comments are permitted, using # as the first character
in a line.

Signed-off-by: Simon Glass <sjg at chromium.org>
---
 Makefile                     | 25 ++++++++++++++++++++++++-
 README                       | 28 ++++++++++++++++++++++++++++
 include/env_default.h        |  2 ++
 mkconfig                     |  4 ++++
 tools/scripts/env2string.sed |  7 +++++++
 5 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 tools/scripts/env2string.sed

diff --git a/Makefile b/Makefile
index 252fc6c..f400d21 100644
--- a/Makefile
+++ b/Makefile
@@ -680,7 +680,7 @@ $(obj)include/autoconf.mk.dep: $(obj)include/config.h include/common.h
 	$(CC) -x c -DDO_DEPS_ONLY -M $(CFLAGS) $(CPPFLAGS) \
 		-MQ $(obj)include/autoconf.mk include/common.h > $@
 
-$(obj)include/autoconf.mk: $(obj)include/config.h
+$(obj)include/generated/autoconf.mk.base: $(obj)include/config.h
 	@$(XECHO) Generating $@ ; \
 	set -e ; \
 	: Extract the config macros ; \
@@ -688,6 +688,29 @@ $(obj)include/autoconf.mk: $(obj)include/config.h
 		sed -n -f tools/scripts/define2mk.sed > $@.tmp && \
 	mv $@.tmp $@
 
+ENV_HEADER = $(obj)include/generated/environment.h
+
+$(obj)include/generated/environment.inc: $(obj)include/generated/autoconf.mk.base
+	@$(XECHO) Generating $@ ; \
+	set -e ; \
+	: Process the environment file ; \
+	envf=$$(sed -n -e '/CONFIG_EXTRA_ENV_SCRIPT/ { s/CONFIG_EXTRA_ENV_SCRIPT=\"\(.*\)\"/\1/;  p }' \
+		$<) ; \
+	echo -n "CONFIG_EXTRA_ENV_TEXT=\"" >$@ ; \
+	echo -n "#define CONFIG_EXTRA_ENV_TEXT \"" >$(ENV_HEADER) ; \
+	if [ -f "$(src)include/configs/$${envf}" ]; then \
+		: Change newline to \n, and quote quotes ; \
+		sed -e 's/^\#.*//' "$(src)include/configs/$${envf}" | \
+			sed -f tools/scripts/env2string.sed | \
+			sed -e 's/"/\\"/g' | \
+			tr -d '\n' | tee -a $(ENV_HEADER) >>$@ ; \
+	fi ; \
+	echo "\"" >>$@
+	echo "\"" >>$(ENV_HEADER)
+
+$(obj)include/autoconf.mk: $(obj)include/generated/environment.inc
+	cat $(obj)include/generated/autoconf.mk.base $< >$@
+
 $(obj)include/generated/generic-asm-offsets.h:	$(obj)include/autoconf.mk.dep \
 	$(obj)lib/asm-offsets.s
 	@$(XECHO) Generating $@
diff --git a/README b/README
index 0bc0af5..6076c90 100644
--- a/README
+++ b/README
@@ -4245,6 +4245,34 @@ environment. As long as you don't save the environment you are
 working with an in-memory copy. In case the Flash area containing the
 environment is erased by accident, a default environment is provided.
 
+The default environment is created in include/env_default.h, and can be
+augmented by various CONFIG defines. See that file for details. In
+particular you can define CONFIG_EXTRA_ENV_SETTINGS in your board file
+to add environment variables (see 'CONFIG_EXTRA_ENV_SETTINGS' above
+for details).
+
+It is also possible to create a .env file in include/configs for your
+board. For example, for snapper9260 you would create a text file called
+include/configs/snapper9260.env containing the environment text. This
+file can include comments (lines starting with #) and blank lines. As
+with CONFIG_EXTRA_ENV_SETTINGS you must add a \0 at the end of each
+variable (except the last). For example:
+
+bootcmd=
+# U-Boot script for booting
+
+if [ -z ${tftpserverip} ]; then
+	echo "Use 'setenv tftpserverip a.b.c.d' to set your machine IP address."
+fi
+
+usb start; setenv autoload n; bootp;
+tftpboot ${tftpserverip}:;
+bootm
+\0failed=
+# Print a message when boot fails
+echo "Boot failed - please check your image"
+
+
 Some configuration options can be set using Environment Variables.
 
 List of environment variables (most likely not complete):
diff --git a/include/env_default.h b/include/env_default.h
index 39c5b7c..a394df5 100644
--- a/include/env_default.h
+++ b/include/env_default.h
@@ -137,6 +137,8 @@ const uchar default_environment[] = {
 #ifdef	CONFIG_EXTRA_ENV_SETTINGS
 	CONFIG_EXTRA_ENV_SETTINGS
 #endif
+	/* This is created in the Makefile */
+	CONFIG_EXTRA_ENV_TEXT
 	"\0"
 #ifdef DEFAULT_ENV_INSTANCE_EMBEDDED
 	}
diff --git a/mkconfig b/mkconfig
index 73f852e..c54b730 100755
--- a/mkconfig
+++ b/mkconfig
@@ -171,10 +171,14 @@ echo "#define CONFIG_SYS_BOARD \"${board}\"" >> config.h
 
 cat << EOF >> config.h
 #define CONFIG_BOARDDIR board/$BOARDDIR
+#define CONFIG_EXTRA_ENV_SCRIPT ${CONFIG_NAME}.env
 #include <config_cmd_defaults.h>
 #include <config_defaults.h>
 #include <configs/${CONFIG_NAME}.h>
 #include <asm/config.h>
+#if !defined(DO_DEPS_ONLY) && !defined(__ASSEMBLY__)
+#include <generated/environment.h>
+#endif
 #include <config_fallbacks.h>
 #include <config_uncmd_spl.h>
 EOF
diff --git a/tools/scripts/env2string.sed b/tools/scripts/env2string.sed
new file mode 100644
index 0000000..0ffc0e7
--- /dev/null
+++ b/tools/scripts/env2string.sed
@@ -0,0 +1,7 @@
+#
+# Sed script to parse a text file containing an environment and convert it
+# to a C string
+#
+
+# Change newlines to \n
+{ :q;N;s/\n/\\n/g; t q}
-- 
1.8.1.3



More information about the U-Boot mailing list