Skip to contentSkip to conversation

cmd: env: select: Add output for available environment targets

PATCH Superseded Christoph Niedermaier 3 participants 11 messages May 9, 2025 – Jun 6, 2025

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover cmd: env: select: Add output for available environment targets

Add the "-l" parameter to the "env select" command to print the available environment targets, convert all argument parsing in the file nvedit.c to getopt() and check that with unit tests.

The first patch converts the parsing of arguments to getopt() for all env commands. The second one adds unit tests for checking the env command. These were used to check the env commands before and after the conversion. The third patch adds the actual new parameter "-l" for the "env select" command.

Christoph Niedermaier (3):
cmd: nvedit: Convert the parsing of arguments to getopt()
test: cmd: nvedit: Add basic unit tests
cmd: env: select: Add output for available environment targets

 cmd/nvedit.c      | 353 +++++++++----------
 env/Kconfig       |   1 +
 env/env.c         |  16 +
 include/env.h     |   7 +
 test/cmd/Makefile |   1 +
 test/cmd/nvedit.c | 844 ++++++++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c     |   2 +
 7 files changed, 1051 insertions(+), 173 deletions(-)
 create mode 100644 test/cmd/nvedit.c

---
Cc: Andy Shevchenko <[email protected]>
Cc: Christian Marangi <[email protected]>
Cc: Heinrich Schuchardt <[email protected]>
Cc: Ilias Apalodimas <[email protected]>
Cc: Jerome Forissier <[email protected]>
Cc: Joe Hershberger <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Venkatesh Yadav Abbarapu <[email protected]>

Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/3 cmd: nvedit: Convert the parsing of arguments to getopt()

The entire parsing of the arguments is done manually. Improve this by converting all parsing of arguments to getopt().

---
Cc: Andy Shevchenko <[email protected]>
Cc: Christian Marangi <[email protected]>
Cc: Heinrich Schuchardt <[email protected]>
Cc: Ilias Apalodimas <[email protected]>
Cc: Jerome Forissier <[email protected]>
Cc: Joe Hershberger <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Venkatesh Yadav Abbarapu <[email protected]>
---
V3: - Add this patch to the series

---
 cmd/nvedit.c | 335 +++++++++++++++++++++++++--------------------------
 env/Kconfig  |   1 +
 2 files changed, 164 insertions(+), 172 deletions(-)
cmd/nvedit.c +163 -172
index 1f259801293..e5e89b51e0a 100644
@@ -41,6 +41,7 @@
 #include <linux/stddef.h>
 #include <asm/byteorder.h>
 #include <asm/io.h>
+#include <getopt.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -92,19 +93,26 @@ static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
 	int i;
 	int rcode = 0;
 	int env_flag = H_HIDE_DOT;
+	struct getopt_state gs;
+	int opt;
 
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "ea")) > 0) {
+		switch (opt) {
+		case 'e':
 #if defined(CONFIG_CMD_NVEDIT_EFI)
-	if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
-		return do_env_print_efi(cmdtp, flag, --argc, ++argv);
+			return do_env_print_efi(cmdtp, flag, argc - gs.index, &argv[gs.index]);
 #endif
-
-	if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
-		argc--;
-		argv++;
-		env_flag &= ~H_HIDE_DOT;
+			break;
+		case 'a':
+			env_flag &= ~H_HIDE_DOT;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
 	}
 
-	if (argc == 1) {
+	if (gs.index == argc) {
 		/* print all env vars */
 		rcode = env_print(NULL, env_flag);
 		if (!rcode)
@@ -116,7 +124,7 @@ static int do_env_print(struct cmd_tbl *cmdtp, int flag, int argc,
 
 	/* print selected env vars */
 	env_flag &= ~H_HIDE_DOT;
-	for (i = 1; i < argc; ++i) {
+	for (i = gs.index; i < argc; ++i) {
 		int rc = env_print(argv[i], env_flag);
 		if (!rc) {
 			printf("## Error: \"%s\" not defined\n", argv[i]);
@@ -133,6 +141,8 @@ static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
 {
 	char *res = NULL;
 	int len, grep_how, grep_what;
+	struct getopt_state gs;
+	int opt;
 
 	if (argc < 2)
 		return CMD_RET_USAGE;
@@ -140,36 +150,31 @@ static int do_env_grep(struct cmd_tbl *cmdtp, int flag,
 	grep_how  = H_MATCH_SUBSTR;	/* default: substring search	*/
 	grep_what = H_MATCH_BOTH;	/* default: grep names and values */
 
-	while (--argc > 0 && **++argv == '-') {
-		char *arg = *argv;
-		while (*++arg) {
-			switch (*arg) {
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "envb")) > 0) {
+		switch (opt) {
 #ifdef CONFIG_REGEX
-			case 'e':		/* use regex matching */
-				grep_how  = H_MATCH_REGEX;
-				break;
+		case 'e':		/* use regex matching */
+			grep_how  = H_MATCH_REGEX;
+			break;
 #endif
-			case 'n':		/* grep for name */
-				grep_what = H_MATCH_KEY;
-				break;
-			case 'v':		/* grep for value */
-				grep_what = H_MATCH_DATA;
-				break;
-			case 'b':		/* grep for both */
-				grep_what = H_MATCH_BOTH;
-				break;
-			case '-':
-				goto DONE;
-			default:
-				return CMD_RET_USAGE;
-			}
+		case 'n':		/* grep for name */
+			grep_what = H_MATCH_KEY;
+			break;
+		case 'v':		/* grep for value */
+			grep_what = H_MATCH_DATA;
+			break;
+		case 'b':		/* grep for both */
+			grep_what = H_MATCH_BOTH;
+			break;
+		default:
+			return CMD_RET_USAGE;
 		}
 	}
 
-DONE:
 	len = hexport_r(&env_htab, '\n',
 			flag | grep_what | grep_how,
-			&res, 0, argc, argv);
+			&res, 0, argc - gs.index, &argv[gs.index]);
 
 	if (len > 0) {
 		puts(res);
@@ -510,37 +515,34 @@ static int do_env_default(struct cmd_tbl *cmdtp, int flag,
 			  int argc, char *const argv[])
 {
 	int all = 0, env_flag = H_INTERACTIVE;
-
-	debug("Initial value for argc=%d\n", argc);
-	while (--argc > 0 && **++argv == '-') {
-		char *arg = *argv;
-
-		while (*++arg) {
-			switch (*arg) {
-			case 'a':		/* default all */
-				all = 1;
-				break;
-			case 'f':		/* force */
-				env_flag |= H_FORCE;
-				break;
-			case 'k':
-				env_flag |= H_NOCLEAR;
-				break;
-			default:
-				return cmd_usage(cmdtp);
-			}
+	struct getopt_state gs;
+	int opt;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "afk")) > 0) {
+		switch (opt) {
+		case 'a':		/* default all */
+			all = 1;
+			break;
+		case 'f':		/* force */
+			env_flag |= H_FORCE;
+			break;
+		case 'k':
+			env_flag |= H_NOCLEAR;
+			break;
+		default:
+			return cmd_usage(cmdtp);
 		}
 	}
-	debug("Final value for argc=%d\n", argc);
-	if (all && (argc == 0)) {
+	if (all && (argc == gs.index)) {
 		/* Reset the whole environment */
 		env_set_default("## Resetting to default environment\n",
 				env_flag);
 		return 0;
 	}
-	if (!all && (argc > 0)) {
+	if (!all && (argc > gs.index)) {
 		/* Reset individual variables */
-		env_set_default_vars(argc, argv, env_flag);
+		env_set_default_vars(argc - gs.index, &argv[gs.index], env_flag);
 		return 0;
 	}
 
@@ -552,32 +554,26 @@ static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
 {
 	int env_flag = H_INTERACTIVE;
 	int ret = 0;
+	struct getopt_state gs;
+	int opt;
+	int i;
 
-	debug("Initial value for argc=%d\n", argc);
-	while (argc > 1 && **(argv + 1) == '-') {
-		char *arg = *++argv;
-
-		--argc;
-		while (*++arg) {
-			switch (*arg) {
-			case 'f':		/* force */
-				env_flag |= H_FORCE;
-				break;
-			default:
-				return CMD_RET_USAGE;
-			}
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "f")) > 0) {
+		switch (opt) {
+		case 'f':		/* force */
+			env_flag |= H_FORCE;
+			break;
+		default:
+			return CMD_RET_USAGE;
 		}
 	}
-	debug("Final value for argc=%d\n", argc);
 
 	env_inc_id();
 
-	while (--argc > 0) {
-		char *name = *++argv;
-
-		if (hdelete_r(name, &env_htab, env_flag))
+	for (i = gs.index; i < argc; i++)
+		if (hdelete_r(argv[i], &env_htab, env_flag))
 			ret = 1;
-	}
 
 	return ret;
 }
@@ -633,64 +629,58 @@ static int do_env_export(struct cmd_tbl *cmdtp, int flag,
 {
 	char	buf[32];
 	ulong	addr;
-	char	*ptr, *cmd, *res;
+	char	*ptr, *res;
 	size_t	size = 0;
 	ssize_t	len;
 	env_t	*envp;
 	char	sep = '\n';
 	int	chk = 0;
 	int	fmt = 0;
-
-	cmd = *argv;
-
-	while (--argc > 0 && **++argv == '-') {
-		char *arg = *argv;
-		while (*++arg) {
-			switch (*arg) {
-			case 'b':		/* raw binary format */
-				if (fmt++)
-					goto sep_err;
-				sep = '\0';
-				break;
-			case 'c':		/* external checksum format */
-				if (fmt++)
-					goto sep_err;
-				sep = '\0';
-				chk = 1;
-				break;
-			case 's':		/* size given */
-				if (--argc <= 0)
-					return cmd_usage(cmdtp);
-				size = hextoul(*++argv, NULL);
-				goto NXTARG;
-			case 't':		/* text format */
-				if (fmt++)
-					goto sep_err;
-				sep = '\n';
-				break;
-			default:
-				return CMD_RET_USAGE;
-			}
+	struct getopt_state gs;
+	int opt;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "bcs:t")) > 0) {
+		switch (opt) {
+		case 'b':		/* raw binary format */
+			if (fmt++)
+				goto sep_err;
+			sep = '\0';
+			break;
+		case 'c':		/* external checksum format */
+			if (fmt++)
+				goto sep_err;
+			sep = '\0';
+			chk = 1;
+			break;
+		case 's':		/* size given */
+			size = hextoul(gs.arg, NULL);
+			break;
+		case 't':		/* text format */
+			if (fmt++)
+				goto sep_err;
+			sep = '\n';
+			break;
+		default:
+			return CMD_RET_USAGE;
 		}
-NXTARG:		;
 	}
 
-	if (argc < 1)
+	if (argc == gs.index)
 		return CMD_RET_USAGE;
 
-	addr = hextoul(argv[0], NULL);
+	addr = hextoul(argv[gs.index], NULL);
 	ptr = map_sysmem(addr, size);
 
+	gs.index++;
+
 	if (size)
 		memset(ptr, '\0', size);
 
-	argc--;
-	argv++;
-
 	if (sep) {		/* export as text file */
 		len = hexport_r(&env_htab, sep,
 				H_MATCH_KEY | H_MATCH_IDENT,
-				&ptr, size, argc, argv);
+				&ptr, size, argc - gs.index, &argv[gs.index]);
 		if (len < 0) {
 			pr_err("## Error: Cannot export environment: errno = %d\n",
 			       errno);
@@ -711,7 +701,7 @@ NXTARG:		;
 
 	len = hexport_r(&env_htab, '\0',
 			H_MATCH_KEY | H_MATCH_IDENT,
-			&res, ENV_SIZE, argc, argv);
+			&res, ENV_SIZE, argc - gs.index, &argv[gs.index]);
 	if (len < 0) {
 		pr_err("## Error: Cannot export environment: errno = %d\n",
 		       errno);
@@ -731,7 +721,7 @@ NXTARG:		;
 
 sep_err:
 	printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
-	       cmd);
+	       argv[0]);
 	return 1;
 }
 #endif
@@ -765,7 +755,7 @@ static int do_env_import(struct cmd_tbl *cmdtp, int flag,
 			 int argc, char *const argv[])
 {
 	ulong	addr;
-	char	*cmd, *ptr;
+	char	*ptr;
 	char	sep = '\n';
 	int	chk = 0;
 	int	fmt = 0;
@@ -773,42 +763,40 @@ static int do_env_import(struct cmd_tbl *cmdtp, int flag,
 	int	crlf_is_lf = 0;
 	int	wl = 0;
 	size_t	size;
-
-	cmd = *argv;
-
-	while (--argc > 0 && **++argv == '-') {
-		char *arg = *argv;
-		while (*++arg) {
-			switch (*arg) {
-			case 'b':		/* raw binary format */
-				if (fmt++)
-					goto sep_err;
-				sep = '\0';
-				break;
-			case 'c':		/* external checksum format */
-				if (fmt++)
-					goto sep_err;
-				sep = '\0';
-				chk = 1;
-				break;
-			case 't':		/* text format */
-				if (fmt++)
-					goto sep_err;
-				sep = '\n';
-				break;
-			case 'r':		/* handle CRLF like LF */
-				crlf_is_lf = 1;
-				break;
-			case 'd':
-				del = 1;
-				break;
-			default:
-				return CMD_RET_USAGE;
-			}
+	struct getopt_state gs;
+	int opt;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "bctrd")) > 0) {
+		switch (opt) {
+		case 'b':		/* raw binary format */
+			if (fmt++)
+				goto sep_err;
+			sep = '\0';
+			break;
+		case 'c':		/* external checksum format */
+			if (fmt++)
+				goto sep_err;
+			sep = '\0';
+			chk = 1;
+			break;
+		case 't':		/* text format */
+			if (fmt++)
+				goto sep_err;
+			sep = '\n';
+			break;
+		case 'r':		/* handle CRLF like LF */
+			crlf_is_lf = 1;
+			break;
+		case 'd':
+			del = 1;
+			break;
+		default:
+			return CMD_RET_USAGE;
 		}
 	}
 
-	if (argc < 1)
+	if (argc == gs.index)
 		return CMD_RET_USAGE;
 
 	if (!fmt)
@@ -817,11 +805,13 @@ static int do_env_import(struct cmd_tbl *cmdtp, int flag,
 	if (sep != '\n' && crlf_is_lf )
 		crlf_is_lf = 0;
 
-	addr = hextoul(argv[0], NULL);
+	addr = hextoul(argv[gs.index], NULL);
 	ptr = map_sysmem(addr, 0);
 
-	if (argc >= 2 && strcmp(argv[1], "-")) {
-		size = hextoul(argv[1], NULL);
+	gs.index++;
+
+	if (argc - gs.index >= 1 && strcmp(argv[gs.index], "-")) {
+		size = hextoul(argv[gs.index], NULL);
 	} else if (chk) {
 		puts("## Error: external checksum format must pass size\n");
 		return CMD_RET_FAILURE;
@@ -844,7 +834,7 @@ static int do_env_import(struct cmd_tbl *cmdtp, int flag,
 		printf("## Info: input data size = %zu = 0x%zX\n", size, size);
 	}
 
-	if (argc > 2)
+	if (argc - gs.index > 1)
 		wl = 1;
 
 	if (chk) {
@@ -866,8 +856,10 @@ static int do_env_import(struct cmd_tbl *cmdtp, int flag,
 		ptr = (char *)ep->data;
 	}
 
+	gs.index++;
+
 	if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
-		       crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
+		       crlf_is_lf, wl ? argc - gs.index : 0, wl ? &argv[gs.index] : NULL)) {
 		pr_err("## Error: Environment import failed: errno = %d\n",
 		       errno);
 		return 1;
@@ -878,7 +870,7 @@ static int do_env_import(struct cmd_tbl *cmdtp, int flag,
 
 sep_err:
 	printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
-		cmd);
+		argv[0]);
 	return 1;
 }
 #endif
@@ -971,6 +963,8 @@ static int do_env_info(struct cmd_tbl *cmdtp, int flag,
 	int eval_flags = 0;
 	int eval_results = 0;
 	bool quiet = false;
+	struct getopt_state gs;
+	int opt;
 #if defined(CONFIG_CMD_SAVEENV) && !IS_ENABLED(CONFIG_ENV_IS_DEFAULT)
 	enum env_location loc;
 #endif
@@ -980,23 +974,20 @@ static int do_env_info(struct cmd_tbl *cmdtp, int flag,
 		return print_env_info();
 
 	/* process options */
-	while (--argc > 0 && **++argv == '-') {
-		char *arg = *argv;
-
-		while (*++arg) {
-			switch (*arg) {
-			case 'd':
-				eval_flags |= ENV_INFO_IS_DEFAULT;
-				break;
-			case 'p':
-				eval_flags |= ENV_INFO_IS_PERSISTED;
-				break;
-			case 'q':
-				quiet = true;
-				break;
-			default:
-				return CMD_RET_USAGE;
-			}
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "dpq")) > 0) {
+		switch (opt) {
+		case 'd':
+			eval_flags |= ENV_INFO_IS_DEFAULT;
+			break;
+		case 'p':
+			eval_flags |= ENV_INFO_IS_PERSISTED;
+			break;
+		case 'q':
+			quiet = true;
+			break;
+		default:
+			return CMD_RET_USAGE;
 		}
 	}
 
env/Kconfig +1 -0
index 9f5ec44601e..9b6cce62999 100644
@@ -2,6 +2,7 @@ menu "Environment"
 
 config ENV_SUPPORT
 	def_bool y
+	select GETOPT
 
 config ENV_SOURCE_FILE
 	string "Environment file to use"
Signature · 1 line
2.30.2
#2 lore raw

PATCH v3 2/3 test: cmd: nvedit: Add basic unit tests

In reply to Christoph Niedermaier

Add basic unit tests for argument checking of the env command.

---
Cc: Andy Shevchenko <[email protected]>
Cc: Christian Marangi <[email protected]>
Cc: Heinrich Schuchardt <[email protected]>
Cc: Ilias Apalodimas <[email protected]>
Cc: Jerome Forissier <[email protected]>
Cc: Joe Hershberger <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Venkatesh Yadav Abbarapu <[email protected]>
---
V3: - Add this patch to the series

---
 test/cmd/Makefile |   1 +
 test/cmd/nvedit.c | 844 ++++++++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c     |   2 +
 3 files changed, 847 insertions(+)
 create mode 100644 test/cmd/nvedit.c
test/cmd/Makefile +1 -0
index 595e4cfcada..4149e82e5ea 100644
@@ -23,6 +23,7 @@ obj-$(CONFIG_CMD_LOADM) += loadm.o
 obj-$(CONFIG_CMD_MEMINFO) += meminfo.o
 obj-$(CONFIG_CMD_MEMORY) += mem_copy.o
 obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o
+obj-$(CONFIG_ENV_SUPPORT) += nvedit.o
 ifdef CONFIG_CMD_PCI
 obj-$(CONFIG_CMD_PCI_MPS) += pci_mps.o
 endif
test/cmd/nvedit.c new file +844 -0

844 lines not on this page · raw message

test/cmd_ut.c +2 -0
index 44e5fdfdaa6..0ab31b400f5 100644
@@ -67,6 +67,7 @@ SUITE_DECL(log);
 SUITE_DECL(mbr);
 SUITE_DECL(measurement);
 SUITE_DECL(mem);
+SUITE_DECL(nvedit);
 SUITE_DECL(optee);
 SUITE_DECL(pci_mps);
 SUITE_DECL(seama);
@@ -94,6 +95,7 @@ static struct suite suites[] = {
 	SUITE(mbr, "mbr command"),
 	SUITE(measurement, "TPM-based measured boot"),
 	SUITE(mem, "memory-related commands"),
+	SUITE(nvedit, "env command"),
 	SUITE(optee, "OP-TEE"),
 	SUITE(pci_mps, "PCI Express Maximum Payload Size"),
 	SUITE(seama, "seama command parameters loading and decoding"),
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH V3 2/3] test: cmd: nvedit: Add basic unit tests

Quoted 3 lines · Christoph Niedermaier

Add basic unit tests for argument checking of the env command.

[snip]

test/cmd/nvedit.c line 837

Quoted 2 lines
test/cmd/nvedit.c new file +2 -0
+	/* Binary format with checksum and with wrong size */
+	ut_asserteq(1, run_commandf("env import -c ${loadaddr} %x", offsetof(env_t, data)));

This will be "unsigned long int" on 64bit or "unsigned int" on 32bit, and so we can't use %x here.

Signature · 1 line
Tom
#4 lore raw

RE: [PATCH V3 2/3] test: cmd: nvedit: Add basic unit tests

From: Tom Rini <[email protected]>
Sent: Tuesday, June 3, 2025 10:13 PM

Quoted 11 lines · Christoph Niedermaier
Quoted 4 lines

Add basic unit tests for argument checking of the env command.

[snip]

test/cmd/nvedit.c line 837

Quoted 2 lines
test/cmd/nvedit.c new file +2 -0
+	/* Binary format with checksum and with wrong size */
+	ut_asserteq(1, run_commandf("env import -c ${loadaddr} %x", offsetof(env_t, data)));

This will be "unsigned long int" on 64bit or "unsigned int" on 32bit, and so we can't use %x here.

I will have a look at it and make a new version. I may not get round to it straight away.

Thanks and regards
Christoph

#5 lore raw

PATCH v3 3/3 cmd: env: select: Add output for available environment targets

In reply to Christoph Niedermaier

Add parameter "-l" for printing available environment targets. The active target is marked with an asterisk. This is done by adding the function env_select_print_list().

If "env select" is called without a target parameter the result is "Select Environment on <NULL>: driver not found". Replace this not so useful output by showing the env usage message instead.

---
Cc: Andy Shevchenko <[email protected]>
Cc: Christian Marangi <[email protected]>
Cc: Heinrich Schuchardt <[email protected]>
Cc: Ilias Apalodimas <[email protected]>
Cc: Jerome Forissier <[email protected]>
Cc: Joe Hershberger <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Venkatesh Yadav Abbarapu <[email protected]>
---
V2: - Showing available environment targets by parameter "-l"
- Showing env usage message if env select is called without a target
V3: - Convert the parsing of arguments to getopt()

---
 cmd/nvedit.c  | 18 +++++++++++++++++-
 env/env.c     | 16 ++++++++++++++++
 include/env.h |  7 +++++++
 3 files changed, 40 insertions(+), 1 deletion(-)
cmd/nvedit.c +17 -1
index e5e89b51e0a..10fd93d4a45 100644
@@ -504,6 +504,22 @@ static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
 static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
 			 char *const argv[])
 {
+	struct getopt_state gs;
+	int opt;
+
+	if (!argv[1])
+		return CMD_RET_USAGE;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "l")) > 0) {
+		switch (opt) {
+		case 'l': /* list */
+			return env_select_print_list() ? 1 : 0;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
 	return env_select(argv[1]) ? 1 : 0;
 }
 #endif
@@ -1180,7 +1196,7 @@ U_BOOT_LONGHELP(env,
 	"env load - load environment\n"
 #endif
 #if defined(CONFIG_CMD_NVEDIT_SELECT)
-	"env select [target] - select environment target\n"
+	"env select [-l] [target] - list/select environment target(s)\n"
 #endif
 #if defined(CONFIG_CMD_NVEDIT_EFI)
 	"env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
env/env.c +16 -0
index dbaeedc3c3b..f94976d4271 100644
@@ -350,6 +350,22 @@ int env_init(void)
 	return ret;
 }
 
+int env_select_print_list(void)
+{
+	struct env_driver *drv;
+	int prio;
+
+	printf("Available Environment targets:\n");
+	for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
+		if (gd->env_load_prio == prio)
+			printf("* ");
+		else
+			printf("  ");
+		printf("%s\n", drv->name);
+	}
+	return 0;
+}
+
 int env_select(const char *name)
 {
 	struct env_driver *drv;
include/env.h +7 -0
index 01c3eeae7e2..4553c7bc109 100644
@@ -286,6 +286,13 @@ int env_save(void);
  */
 int env_erase(void);
 
+/**
+ * env_select_print_list() - Print available environment targets
+ *
+ * Return: 0 if OK, -ve on error
+ */
+int env_select_print_list(void);
+
 /**
  * env_select() - Select the environment storage
  *
Signature · 1 line
2.30.2
#6 lore raw
In reply to Christoph Niedermaier
Quoted 9 lines · Christoph Niedermaier

Add the "-l" parameter to the "env select" command to print the available environment targets, convert all argument parsing in the file nvedit.c to getopt() and check that with unit tests.

The first patch converts the parsing of arguments to getopt() for all env commands. The second one adds unit tests for checking the env command. These were used to check the env commands before and after the conversion. The third patch adds the actual new parameter "-l" for the "env select" command.

What is the binary size increase with this change, please?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

From: Andy Shevchenko <[email protected]>
Sent: Monday, May 12, 2025 9:38 AM

Quoted 5 lines · Christoph Niedermaier
Quoted 4 lines

Add the "-l" parameter to the "env select" command to print the available environment targets, convert all argument parsing in the file nvedit.c to getopt() and check that with unit tests.

Quoted 7 lines
Quoted 5 lines

The first patch converts the parsing of arguments to getopt() for all env commands. The second one adds unit tests for checking the env command. These were used to check the env commands before and after the conversion. The third patch adds the actual new parameter "-l" for the "env select" command.

What is the binary size increase with this change, please?

I don't know exactly which binary size I should look at? I have looked at the object file of nvedit.c:

I built it with the config dh_imx6_defconfig.

Before my patches:

$ ls -la cmd/nvedit.o 
-rw-r--r-- 1 developer developer 57644 May 12 10:03 cmd/nvedit.o

After my patches:

$ ls -la cmd/nvedit.o 
-rw-r--r-- 1 developer developer 59292 May 12 10:06 cmd/nvedit.o

So the increase is 1648 Bytes (+2.86%).

Regards
Christoph

#8 lore raw
Quoted 21 lines · Christoph Niedermaier

From: Andy Shevchenko <[email protected]>
Sent: Monday, May 12, 2025 9:38 AM

Quoted 5 lines · Christoph Niedermaier
Quoted 4 lines

Add the "-l" parameter to the "env select" command to print the available environment targets, convert all argument parsing in the file nvedit.c to getopt() and check that with unit tests.

Quoted 7 lines
Quoted 5 lines

The first patch converts the parsing of arguments to getopt() for all env commands. The second one adds unit tests for checking the env command. These were used to check the env commands before and after the conversion. The third patch adds the actual new parameter "-l" for the "env select" command.

What is the binary size increase with this change, please?

I don't know exactly which binary size I should look at? I have looked at the object file of nvedit.c:

I built it with the config dh_imx6_defconfig.

Before my patches:

Quoted 8 lines
$ ls -la cmd/nvedit.o 
-rw-r--r-- 1 developer developer 57644 May 12 10:03 cmd/nvedit.o

After my patches:

$ ls -la cmd/nvedit.o 
-rw-r--r-- 1 developer developer 59292 May 12 10:06 cmd/nvedit.o

So the increase is 1648 Bytes (+2.86%).

U-Boot binary. And better to use bloat-o-meter script for that, it shows the breakdown in more precise terms.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

From: Andy Shevchenko <[email protected]>
Sent: Monday, May 12, 2025 10:38 AM

Quoted 25 lines · Christoph Niedermaier
Quoted 24 lines

From: Andy Shevchenko <[email protected]>
Sent: Monday, May 12, 2025 9:38 AM

Quoted 5 lines · Christoph Niedermaier
Quoted 4 lines

Add the "-l" parameter to the "env select" command to print the available environment targets, convert all argument parsing in the file nvedit.c to getopt() and check that with unit tests.

Quoted 7 lines
Quoted 5 lines

The first patch converts the parsing of arguments to getopt() for all env commands. The second one adds unit tests for checking the env command. These were used to check the env commands before and after the conversion. The third patch adds the actual new parameter "-l" for the "env select" command.

What is the binary size increase with this change, please?

I don't know exactly which binary size I should look at? I have looked at the object file of nvedit.c:

I built it with the config dh_imx6_defconfig.

Before my patches:

$ ls -la cmd/nvedit.o
-rw-r--r-- 1 developer developer 57644 May 12 10:03 cmd/nvedit.o
Quoted 8 lines
Quoted 5 lines

After my patches:

$ ls -la cmd/nvedit.o
-rw-r--r-- 1 developer developer 59292 May 12 10:06 cmd/nvedit.o

So the increase is 1648 Bytes (+2.86%).

U-Boot binary. And better to use bloat-o-meter script for that, it shows the breakdown in more precise terms.

With bloat-o-meter it looks like this (before and after my patches):

./bloat-o-meter -p arm-linux-gnueabihf- u-boot_before u-boot_after add/remove: 5/1 grow/shrink: 4/2 up/down: 1152/-652 (500)

Function                                     old     new   delta
bdinfo_print_all                               -     404    +404
__getopt.constprop                             -     292    +292
substitute                                     -     156    +156
print_eth                                      -     124    +124
print_bi_dram                                  -      72     +72
do_env_default                               116     152     +36
do_env_import                                424     456     +32
do_env_print                                 740     764     +24
do_env_delete                                 92     104     +12
do_env_export                                336     328      -8
substitute.lto_priv                          156       -    -156
do_bdinfo                                    580      92    -488

Total: Before=501681, After=502181, chg +0.10%

Regards
Christoph

#10 lore raw
Quoted 53 lines · Christoph Niedermaier

From: Andy Shevchenko <[email protected]>
Sent: Monday, May 12, 2025 10:38 AM

Quoted 25 lines · Christoph Niedermaier
Quoted 24 lines

From: Andy Shevchenko <[email protected]>
Sent: Monday, May 12, 2025 9:38 AM

Quoted 5 lines · Christoph Niedermaier
Quoted 4 lines

Add the "-l" parameter to the "env select" command to print the available environment targets, convert all argument parsing in the file nvedit.c to getopt() and check that with unit tests.

Quoted 7 lines
Quoted 5 lines

The first patch converts the parsing of arguments to getopt() for all env commands. The second one adds unit tests for checking the env command. These were used to check the env commands before and after the conversion. The third patch adds the actual new parameter "-l" for the "env select" command.

What is the binary size increase with this change, please?

I don't know exactly which binary size I should look at? I have looked at the object file of nvedit.c:

I built it with the config dh_imx6_defconfig.

Before my patches:

$ ls -la cmd/nvedit.o
-rw-r--r-- 1 developer developer 57644 May 12 10:03 cmd/nvedit.o
Quoted 8 lines
Quoted 5 lines

After my patches:

$ ls -la cmd/nvedit.o
-rw-r--r-- 1 developer developer 59292 May 12 10:06 cmd/nvedit.o

So the increase is 1648 Bytes (+2.86%).

U-Boot binary. And better to use bloat-o-meter script for that, it shows the breakdown in more precise terms.

With bloat-o-meter it looks like this (before and after my patches):

./bloat-o-meter -p arm-linux-gnueabihf- u-boot_before u-boot_after add/remove: 5/1 grow/shrink: 4/2 up/down: 1152/-652 (500)

Function                                     old     new   delta
bdinfo_print_all                               -     404    +404
__getopt.constprop                             -     292    +292
substitute                                     -     156    +156
print_eth                                      -     124    +124
print_bi_dram                                  -      72     +72
do_env_default                               116     152     +36
do_env_import                                424     456     +32
do_env_print                                 740     764     +24
do_env_delete                                 92     104     +12
do_env_export                                336     328      -8
substitute.lto_priv                          156       -    -156
do_bdinfo                                    580      92    -488
Quoted 1 line

Total: Before=501681, After=502181, chg +0.10%

Not bad. Thank you for sharing!

Signature · 2 lines
With Best Regards,
Andy Shevchenko