[PATCH 2/2] uboot-envtools: support alternate default config

Bjørn Mork bjorn at mork.no
Thu Dec 10 07:40:28 EST 2020


Now that we can create an alternate configuration file, add support
for selecting it by using the alternate application names
`fw_printsys' or `fw_setsys'.

Signed-off-by: Bjørn Mork <bjorn at mork.no>
---
 package/boot/uboot-envtools/Makefile          |   2 +
 .../002-support-alternate-config.patch        |  91 ++++++++++
 .../patches/003-fix-usage-text.patch          | 167 ++++++++++++++++++
 3 files changed, 260 insertions(+)
 create mode 100644 package/boot/uboot-envtools/patches/002-support-alternate-config.patch
 create mode 100644 package/boot/uboot-envtools/patches/003-fix-usage-text.patch

diff --git a/package/boot/uboot-envtools/Makefile b/package/boot/uboot-envtools/Makefile
index 601627011d56..e7ecfefec73a 100644
--- a/package/boot/uboot-envtools/Makefile
+++ b/package/boot/uboot-envtools/Makefile
@@ -68,6 +68,8 @@ define Package/uboot-envtools/install
 	$(INSTALL_DIR) $(1)/usr/sbin
 	$(INSTALL_BIN) $(PKG_BUILD_DIR)/tools/env/fw_printenv $(1)/usr/sbin
 	$(LN) fw_printenv $(1)/usr/sbin/fw_setenv
+	$(LN) fw_printenv $(1)/usr/sbin/fw_printsys
+	$(LN) fw_printenv $(1)/usr/sbin/fw_setsys
 	$(INSTALL_DIR) $(1)/lib
 	$(INSTALL_DATA) ./files/uboot-envtools.sh $(1)/lib
 	$(INSTALL_DIR) $(1)/etc/uci-defaults
diff --git a/package/boot/uboot-envtools/patches/002-support-alternate-config.patch b/package/boot/uboot-envtools/patches/002-support-alternate-config.patch
new file mode 100644
index 000000000000..aa341545a03c
--- /dev/null
+++ b/package/boot/uboot-envtools/patches/002-support-alternate-config.patch
@@ -0,0 +1,91 @@
+From e5255e1ca3af000adb5ff686ea5c5b5b60fb7d9d Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Bj=C3=B8rn=20Mork?= <bmork at telenor.net>
+Date: Thu, 10 Dec 2020 12:32:21 +0100
+Subject: [PATCH 1/2] tools: env: add support for alternate config file
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+Some devices use two distinct environment partitions for
+different variable sets.  Add basic support for choosing
+between two configuration files based on application name.
+
+Signed-off-by: Bjørn Mork <bmork at telenor.net>
+---
+ tools/env/fw_env_main.c    | 28 +++++++++++++++++++++++++---
+ tools/env/fw_env_private.h |  4 +++-
+ 2 files changed, 28 insertions(+), 4 deletions(-)
+
+diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c
+index 1d193bd437d8..d67c2ed407e3 100644
+--- a/tools/env/fw_env_main.c
++++ b/tools/env/fw_env_main.c
+@@ -42,6 +42,12 @@
+ #define CMD_SETENV	"fw_setenv"
+ static int do_printenv;
+ 
++#ifdef CONFIG_SYSFILE
++#define CMD_PRINTSYS	"fw_printsys"
++#define CMD_SETSYS	"fw_setsys"
++static int do_sys;
++#endif
++
+ static struct option long_options[] = {
+ 	{"config", required_argument, NULL, 'c'},
+ 	{"help", no_argument, NULL, 'h'},
+@@ -117,7 +123,7 @@ static void parse_common_args(int argc, char *argv[])
+ 	int c;
+ 
+ #ifdef CONFIG_FILE
+-	env_opts.config_file = CONFIG_FILE;
++	env_opts.config_file = do_sys ? CONFIG_SYSFILE : CONFIG_FILE;
+ #endif
+ 
+ 	while ((c = getopt_long(argc, argv, ":a:c:l:h:v", long_options, NULL)) !=
+@@ -219,10 +225,26 @@ int main(int argc, char *argv[])
+ 		do_printenv = 1;
+ 	} else if (strcmp(_cmdname, CMD_SETENV) == 0) {
+ 		do_printenv = 0;
++#ifdef CONFIG_SYSFILE
++	} else if (strcmp(_cmdname, CMD_PRINTSYS) == 0) {
++		do_printenv = 1;
++		do_sys = 1;
++	} else if (strcmp(_cmdname, CMD_SETSYS) == 0) {
++		do_printenv = 0;
++		do_sys = 1;
++#endif
+ 	} else {
+ 		fprintf(stderr,
+-			"Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
+-			CMD_PRINTENV, CMD_SETENV, _cmdname);
++			"Identity crisis - may be called as `%s',"
++#ifdef CONFIG_SYSFILE
++			"`%s', `%s',"
++#endif
++			" or as `%s' but not as `%s'\n",
++			CMD_PRINTENV, CMD_SETENV,
++#ifdef CONFIG_SYSFILE
++			CMD_PRINTSYS, CMD_SETSYS,
++#endif
++			_cmdname);
+ 		exit(EXIT_FAILURE);
+ 	}
+ 
+diff --git a/tools/env/fw_env_private.h b/tools/env/fw_env_private.h
+index 86be16dabc62..35b9bfc70aee 100644
+--- a/tools/env/fw_env_private.h
++++ b/tools/env/fw_env_private.h
+@@ -23,7 +23,9 @@
+  */
+ #define CONFIG_FILE     "/etc/fw_env.config"
+ 
+-#ifndef CONFIG_FILE
++#ifdef CONFIG_FILE
++#define CONFIG_SYSFILE  "/etc/fw_sys.config"
++#else
+ #define HAVE_REDUND /* For systems with 2 env sectors */
+ #define DEVICE1_NAME      "/dev/mtd1"
+ #define DEVICE2_NAME      "/dev/mtd2"
+-- 
+2.29.2
+
diff --git a/package/boot/uboot-envtools/patches/003-fix-usage-text.patch b/package/boot/uboot-envtools/patches/003-fix-usage-text.patch
new file mode 100644
index 000000000000..220239ac2150
--- /dev/null
+++ b/package/boot/uboot-envtools/patches/003-fix-usage-text.patch
@@ -0,0 +1,167 @@
+From 2b3239c16749a175b189971a25025872774d690e Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Bj=C3=B8rn=20Mork?= <bmork at telenor.net>
+Date: Thu, 10 Dec 2020 12:54:53 +0100
+Subject: [PATCH 2/2] tools: env: fix usage text for alternate cmd names
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+Signed-off-by: Bjørn Mork <bmork at telenor.net>
+---
+ tools/env/fw_env_main.c | 65 ++++++++++++++++++++++-------------------
+ 1 file changed, 35 insertions(+), 30 deletions(-)
+
+diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c
+index d67c2ed407e3..034db42813e2 100644
+--- a/tools/env/fw_env_main.c
++++ b/tools/env/fw_env_main.c
+@@ -66,41 +66,46 @@ static int noheader;
+ /* getenv options */
+ static char *script_file;
+ 
+-void usage_printenv(void)
++void usage_common(const char *prog)
+ {
+-
++#ifdef CONFIG_FILE
++	const char *cfgfile = do_sys ? CONFIG_SYSFILE : CONFIG_FILE;
++#endif
+ 	fprintf(stderr,
+-		"Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
+-		"Print variables from U-Boot environment\n"
++		"Usage: %s [OPTIONS]... [VARIABLE]...\n"
++		"%s variables from U-Boot environment\n"
+ 		"\n"
+ 		" -h, --help           print this help.\n"
+ 		" -v, --version        display version\n"
+ #ifdef CONFIG_FILE
+-		" -c, --config         configuration file, default:" CONFIG_FILE "\n"
++		" -c, --config         configuration file, default:%s\n"
++#endif
++		" -l, --lock           lock node, default:/var/lock\n",
++		prog, do_printenv ? "Print" : "Modify"
++#ifdef CONFIG_FILE
++		, cfgfile
+ #endif
++		);
++}
++
++void usage_printenv(const char *prog)
++{
++	usage_common(prog);
++	fprintf(stderr,
+ 		" -n, --noheader       do not repeat variable name in output\n"
+-		" -l, --lock           lock node, default:/var/lock\n"
+ 		"\n");
+ }
+ 
+-void usage_env_set(void)
++void usage_env_set(const char *prog)
+ {
++	usage_common(prog);
+ 	fprintf(stderr,
+-		"Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
+-		"Modify variables in U-Boot environment\n"
+-		"\n"
+-		" -h, --help           print this help.\n"
+-		" -v, --version        display version\n"
+-#ifdef CONFIG_FILE
+-		" -c, --config         configuration file, default:" CONFIG_FILE "\n"
+-#endif
+-		" -l, --lock           lock node, default:/var/lock\n"
+ 		" -s, --script         batch mode to minimize writes\n"
+ 		"\n"
+ 		"Examples:\n"
+-		"  fw_setenv foo bar   set variable foo equal bar\n"
+-		"  fw_setenv foo       clear variable foo\n"
+-		"  fw_setenv --script file run batch script\n"
++		"  %s foo bar   set variable foo equal bar\n"
++		"  %s foo       clear variable foo\n"
++		"  %s --script file run batch script\n"
+ 		"\n"
+ 		"Script Syntax:\n"
+ 		"  key [space] value\n"
+@@ -115,10 +120,10 @@ void usage_env_set(void)
+ 		"  kernel_addr    400000\n"
+ 		"  foo            empty empty empty    empty empty empty\n"
+ 		"  bar\n"
+-		"\n");
++		"\n", prog, prog, prog);
+ }
+ 
+-static void parse_common_args(int argc, char *argv[])
++static void parse_common_args(char *prog, int argc, char *argv[])
+ {
+ 	int c;
+ 
+@@ -138,7 +143,7 @@ static void parse_common_args(int argc, char *argv[])
+ 			env_opts.lockname = optarg;
+ 			break;
+ 		case 'h':
+-			do_printenv ? usage_printenv() : usage_env_set();
++			do_printenv ? usage_printenv(prog) : usage_env_set(prog);
+ 			exit(EXIT_SUCCESS);
+ 			break;
+ 		case 'v':
+@@ -156,11 +161,11 @@ static void parse_common_args(int argc, char *argv[])
+ 	optind = 1;
+ }
+ 
+-int parse_printenv_args(int argc, char *argv[])
++int parse_printenv_args(char *prog, int argc, char *argv[])
+ {
+ 	int c;
+ 
+-	parse_common_args(argc, argv);
++	parse_common_args(prog, argc, argv);
+ 
+ 	while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
+ 		!= EOF) {
+@@ -175,7 +180,7 @@ int parse_printenv_args(int argc, char *argv[])
+ 			/* ignore common options */
+ 			break;
+ 		default: /* '?' */
+-			usage_printenv();
++			usage_printenv(prog);
+ 			exit(EXIT_FAILURE);
+ 			break;
+ 		}
+@@ -183,11 +188,11 @@ int parse_printenv_args(int argc, char *argv[])
+ 	return 0;
+ }
+ 
+-int parse_setenv_args(int argc, char *argv[])
++int parse_setenv_args(char *prog, int argc, char *argv[])
+ {
+ 	int c;
+ 
+-	parse_common_args(argc, argv);
++	parse_common_args(prog, argc, argv);
+ 
+ 	while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
+ 		!= EOF) {
+@@ -202,7 +207,7 @@ int parse_setenv_args(int argc, char *argv[])
+ 			/* ignore common options */
+ 			break;
+ 		default: /* '?' */
+-			usage_env_set();
++			usage_env_set(prog);
+ 			exit(EXIT_FAILURE);
+ 			break;
+ 		}
+@@ -249,10 +254,10 @@ int main(int argc, char *argv[])
+ 	}
+ 
+ 	if (do_printenv) {
+-		if (parse_printenv_args(argc, argv))
++		if (parse_printenv_args(_cmdname, argc, argv))
+ 			exit(EXIT_FAILURE);
+ 	} else {
+-		if (parse_setenv_args(argc, argv))
++		if (parse_setenv_args(_cmdname, argc, argv))
+ 			exit(EXIT_FAILURE);
+ 	}
+ 
+-- 
+2.29.2
+
-- 
2.20.1




More information about the openwrt-devel mailing list