public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] [PATCH] Allow U-Boot scripts to be placed in a .env file
@ 2013-04-16 23:36 Simon Glass
  2013-04-17  5:44 ` Wolfgang Denk
  0 siblings, 1 reply; 17+ messages in thread
From: Simon Glass @ 2013-04-16 23:36 UTC (permalink / raw)
  To: u-boot

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@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

^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2013-05-06 13:14 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-16 23:36 [U-Boot] [PATCH] Allow U-Boot scripts to be placed in a .env file Simon Glass
2013-04-17  5:44 ` Wolfgang Denk
2013-04-17 22:26   ` Otavio Salvador
2013-04-23 21:32   ` Simon Glass
2013-04-23 23:33     ` Tom Rini
2013-04-24  0:29       ` Simon Glass
2013-04-24  2:14         ` Otavio Salvador
2013-04-24  2:36           ` Simon Glass
2013-04-24  2:44             ` Otavio Salvador
2013-04-24  3:05               ` Simon Glass
2013-04-24  3:20                 ` Otavio Salvador
2013-04-24  3:43                   ` Simon Glass
2013-04-24  5:36                   ` Wolfgang Denk
2013-04-26  8:34                     ` Smithlife
2013-04-24  5:34                 ` Wolfgang Denk
2013-05-06 13:02   ` Rob Herring
2013-05-06 13:14     ` Tom Rini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox