Skip to contentSkip to conversation

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

PATCH Superseded Christoph Niedermaier 2 participants 3 messages Mar 21, 2025 – Mar 26, 2025

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cmd: env: select: Add output for available evironment targets

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: Marek Vasut <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Joe Hershberger <[email protected]>
Cc: Tom Rini <[email protected]>
---
V2: - Showing available environment targets by parameter "-l"
- Showing env usage message if env select is called without a target

---
 cmd/nvedit.c  | 19 ++++++++++++++++++-
 env/env.c     | 16 ++++++++++++++++
 include/env.h |  7 +++++++
 3 files changed, 41 insertions(+), 1 deletion(-)
cmd/nvedit.c +18 -1
index 1f259801293..de64094db4d 100644
@@ -499,6 +499,23 @@ 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[])
 {
+	if (!argv[1])
+		return CMD_RET_USAGE;
+
+	while (argc > 1 && **(argv + 1) == '-') {
+		char *arg = *++argv;
+
+		--argc;
+		while (*++arg) {
+			switch (*arg) {
+			case 'l': /* list */
+				return env_select_print_list() ? 1 : 0;
+			default:
+				return CMD_RET_USAGE;
+			}
+		}
+	}
+
 	return env_select(argv[1]) ? 1 : 0;
 }
 #endif
@@ -1189,7 +1206,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 bcc189e14db..c73b53a2c62 100644
@@ -347,6 +347,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
#1 lore raw
Quoted 28 lines · 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: Marek Vasut <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Joe Hershberger <[email protected]>
Cc: Tom Rini <[email protected]>
---
V2: - Showing available environment targets by parameter "-l"
- Showing env usage message if env select is called without a target

---
  cmd/nvedit.c  | 19 ++++++++++++++++++-
  env/env.c     | 16 ++++++++++++++++
  include/env.h |  7 +++++++
  3 files changed, 41 insertions(+), 1 deletion(-)
cmd/nvedit.c +0 -0
index 1f259801293..de64094db4d 100644

cmd/nvedit.c static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc, · line 505

Quoted 7 lines
cmd/nvedit.c +4 -0
@@ -499,6 +499,23 @@ 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[])
  {
+	if (!argv[1])
+		return CMD_RET_USAGE;
+
+	while (argc > 1 && **(argv + 1) == '-') {

Two higher level things:

- Can you first convert cmd/nvedit.h to use include/getopt.h and then use getopt() here too ? This hand rolled arg parsing is repulsive, the multiple ad-hoc copies of that are even worse.
- Please write a test for this, see test/cmd/ , maybe Simon can help with details of this ... ?

#2 lore raw

From: Marek Vasut <[email protected]>
Sent: Friday, March 21, 2025 8:23 PM

Quoted 38 lines · Christoph Niedermaier
Quoted 28 lines

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: Marek Vasut <[email protected]>
Cc: Patrick Delaunay <[email protected]>
Cc: Joe Hershberger <[email protected]>
Cc: Tom Rini <[email protected]>
---
V2: - Showing available environment targets by parameter "-l"
- Showing env usage message if env select is called without a target

---
  cmd/nvedit.c  | 19 ++++++++++++++++++-
  env/env.c     | 16 ++++++++++++++++
  include/env.h |  7 +++++++
  3 files changed, 41 insertions(+), 1 deletion(-)
cmd/nvedit.c +0 -0
index 1f259801293..de64094db4d 100644

cmd/nvedit.c static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc, · line 505

Quoted 7 lines
cmd/nvedit.c +4 -0
@@ -499,6 +499,23 @@ 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[])
  {
+	if (!argv[1])
+		return CMD_RET_USAGE;
+
+	while (argc > 1 && **(argv + 1) == '-') {

Two higher level things:

Quoted 3 lines

- Can you first convert cmd/nvedit.h to use include/getopt.h and then use getopt() here too ? This hand rolled arg parsing is repulsive, the multiple ad-hoc copies of that are even worse.

I will try.

Quoted 2 lines

- Please write a test for this, see test/cmd/ , maybe Simon can help with details of this ... ?

I will try.

Regards
Christoph