Skip to contentSkip to conversation

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

PATCH In review Christoph Niedermaier 2 participants 9 messages Jul 18, 2025 – Aug 19, 2025

lore
Share & tools
lore
#0 lore raw

v5 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 avoids stopping unit test on console output "## Error: ". The third one adds unit tests for checking the env command. These were used to check the env commands before and after the conversion. The fourth 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/py: Avoid stopping the test on console output "## Error: "
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       | 857 ++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c           |   2 +
 test/py/console_base.py |   2 -
 8 files changed, 1064 insertions(+), 175 deletions(-)
 create mode 100644 test/cmd/nvedit.c

---
Cc: Andy Shevchenko <[email protected]>
Cc: Casey Connolly <[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: Mattijs Korpershoek <[email protected]>
Cc: Michael Walle <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Quentin Schulz <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Varadarajan Narayanan <[email protected]>
Cc: "Vincent Stehlé" <[email protected]>
Cc: Weijie Gao <[email protected]>

Signature · 1 line
2.30.2
#1 lore raw

PATCH v5 1/4 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: Casey Connolly <[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: Mattijs Korpershoek <[email protected]>
Cc: Michael Walle <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Quentin Schulz <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Varadarajan Narayanan <[email protected]>
Cc: "Vincent Stehlé" <[email protected]>
Cc: Weijie Gao <[email protected]>
---
V3: - Add this patch to the series
V4: - No changes
V5: - No changes

---
 cmd/nvedit.c | 335 +++++++++++++++++++++++++--------------------------
 env/Kconfig  |   1 +
 2 files changed, 164 insertions(+), 172 deletions(-)
cmd/nvedit.c +163 -172
index 11c3cea882b..945db536bf2 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 c667e8f2da1..558bc5202de 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 v5 2/4 test/py: Avoid stopping the test on console output "## Error: "

In reply to Christoph Niedermaier

Some tests deliberately provoke an error, but if the console output is "## Error: " pytest catches it as an error and cancels the test. Remove this to enable this type of tests.

---
Cc: Andy Shevchenko <[email protected]>
Cc: Casey Connolly <[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: Mattijs Korpershoek <[email protected]>
Cc: Michael Walle <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Quentin Schulz <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Varadarajan Narayanan <[email protected]>
Cc: "Vincent Stehlé" <[email protected]>
Cc: Weijie Gao <[email protected]>
---
V5: - Add this patch to the series

---
 test/py/console_base.py | 2 --
 1 file changed, 2 deletions(-)
test/py/console_base.py +0 -2
index 88d444b44b8..1ac42ef83a0 100644
@@ -21,7 +21,6 @@ pattern_u_boot_spl_signon = re.compile('(U-Boot SPL \\d{4}\\.\\d{2}[^\r\n]*\\))'
 pattern_u_boot_main_signon = re.compile('(U-Boot \\d{4}\\.\\d{2}[^\r\n]*\\))')
 pattern_stop_autoboot_prompt = re.compile('Hit any key to stop autoboot: ')
 pattern_unknown_command = re.compile('Unknown command \'.*\' - try \'help\'')
-pattern_error_notification = re.compile('## Error: ')
 pattern_error_please_reset = re.compile('### ERROR ### Please RESET the board ###')
 pattern_ready_prompt = re.compile('{lab ready in (.*)s: (.*)}')
 pattern_lab_mode = re.compile('{lab mode.*}')
@@ -45,7 +44,6 @@ bad_pattern_defs = (
     ('main_signon', pattern_u_boot_main_signon),
     ('stop_autoboot_prompt', pattern_stop_autoboot_prompt),
     ('unknown_command', pattern_unknown_command),
-    ('error_notification', pattern_error_notification),
     ('error_please_reset', pattern_error_please_reset),
 )
 
Signature · 1 line
2.30.2
#3 lore raw

PATCH v5 3/4 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: Casey Connolly <[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: Mattijs Korpershoek <[email protected]>
Cc: Michael Walle <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Quentin Schulz <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Varadarajan Narayanan <[email protected]>
Cc: "Vincent Stehlé" <[email protected]>
Cc: Weijie Gao <[email protected]>
---
V3: - Add this patch to the series
V4: - Replace offsetof calculated size with a fixed size
- Remove "env delete stdout" due to reproducibility problems
- Cover also systems without redundant environment
- Add comments to import examples
- Write the import examples for each test into memory
V5: - Remove binary format with checksum without size nvedit test,
because there are different default sizes on different platforms and the checksum is therefore different.

---
 test/cmd/Makefile |   1 +
 test/cmd/nvedit.c | 857 ++++++++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c     |   2 +
 3 files changed, 860 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 +857 -0

857 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
#4 lore raw

PATCH v5 4/4 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: Casey Connolly <[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: Mattijs Korpershoek <[email protected]>
Cc: Michael Walle <[email protected]>
Cc: Michal Simek <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Quentin Schulz <[email protected]>
Cc: Rasmus Villemoes <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Varadarajan Narayanan <[email protected]>
Cc: "Vincent Stehlé" <[email protected]>
Cc: Weijie Gao <[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()
V4: - No changes
V5: - No changes

---
 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 945db536bf2..604ce71f269 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 7a9c96b4078..fb55215254c 100644
@@ -353,6 +353,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
#5 lore raw
In reply to Christoph Niedermaier
Quoted 11 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 avoids stopping unit test on console output "## Error: ". The third one adds unit tests for checking the env command. These were used to check the env commands before and after the conversion. The fourth patch adds the actual new parameter "-l" for the "env select" command.

Quoted 5 lines

Christoph Niedermaier (3):
cmd: nvedit: Convert the parsing of arguments to getopt() test/py: Avoid stopping the test on console output "## Error: "
test: cmd: nvedit: Add basic unit tests
cmd: env: select: Add output for available environment targets

I'm sorry, but it seems like a lot of the tests fail in CI?
https://source.denx.de/u-boot/u-boot/-/jobs/1209804
https://source.denx.de/u-boot/u-boot/-/jobs/1209794
For example.

Signature · 1 line
Tom
#6 lore raw

From: Tom Rini <[email protected]>
Sent: Monday, July 28, 2025 9:03 PM

Quoted 19 lines · Christoph Niedermaier
Quoted 12 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.

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

Quoted 5 lines

Christoph Niedermaier (3):
cmd: nvedit: Convert the parsing of arguments to getopt() test/py: Avoid stopping the test on console output "## Error: "
test: cmd: nvedit: Add basic unit tests
cmd: env: select: Add output for available environment targets

Quoted 4 lines

I'm sorry, but it seems like a lot of the tests fail in CI?
https://source.denx.de/u-boot/u-boot/-/jobs/1209804
https://source.denx.de/u-boot/u-boot/-/jobs/1209794
For example.

I think I fixed my test, but broke other tests in the process. I'm very sorry. I will try to fix the problem, but it is difficult to reproduce the entire CI locally. I will try my best, sorry again.

Regards
Christoph

#7 lore raw
Quoted 21 lines · Christoph Niedermaier

From: Tom Rini <[email protected]>
Sent: Monday, July 28, 2025 9:03 PM

Quoted 19 lines · Christoph Niedermaier
Quoted 17 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.

The first patch converts the parsing of arguments to getopt() for all env commands. The second one avoids stopping unit test on console output "## Error: ". The third one adds unit tests for checking the env command. These were used to check the env commands before and after the conversion. The fourth 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/py: Avoid stopping the test on console output "## Error: "
test: cmd: nvedit: Add basic unit tests
cmd: env: select: Add output for available environment targets

Quoted 8 lines
Quoted 4 lines

I'm sorry, but it seems like a lot of the tests fail in CI?
https://source.denx.de/u-boot/u-boot/-/jobs/1209804
https://source.denx.de/u-boot/u-boot/-/jobs/1209794
For example.

I think I fixed my test, but broke other tests in the process. I'm very sorry. I will try to fix the problem, but it is difficult to reproduce the entire CI locally. I will try my best, sorry again.

You can trigger CI runs too via GitHub / Azure, BTW:
https://docs.u-boot.org/en/latest/develop/ci_testing.html

Signature · 1 line
Tom
#8 lore raw

From: Tom Rini <[email protected]>
Sent: Thursday, July 31, 2025 6:06 PM

Quoted 27 lines · Christoph Niedermaier
Quoted 26 lines

From: Tom Rini <[email protected]>
Sent: Monday, July 28, 2025 9:03 PM

Quoted 23 lines · Christoph Niedermaier
Quoted 12 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.

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

Quoted 5 lines

Christoph Niedermaier (3):
cmd: nvedit: Convert the parsing of arguments to getopt() test/py: Avoid stopping the test on console output "## Error: "
test: cmd: nvedit: Add basic unit tests
cmd: env: select: Add output for available environment targets

I'm sorry, but it seems like a lot of the tests fail in CI?
https://source.denx.de/u-boot/u-boot/-/jobs/1209804
https://source.denx.de/u-boot/u-boot/-/jobs/1209794
For example.

Quoted 6 lines
Quoted 3 lines

I think I fixed my test, but broke other tests in the process. I'm very sorry. I will try to fix the problem, but it is difficult to reproduce the entire CI locally. I will try my best, sorry again.

You can trigger CI runs too via GitHub / Azure, BTW:
https://docs.u-boot.org/en/latest/develop/ci_testing.html

Thanks for your support. I'll take a look at it, but sorry, I can't get to it right now.

Thanks and regards
Christoph