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.

Signed-off-by: Christoph Niedermaier <cniederma...@dh-electronics.com>
---
Cc: Andy Shevchenko <andriy.shevche...@linux.intel.com>
Cc: Christian Marangi <ansuels...@gmail.com>
Cc: Heinrich Schuchardt <xypron.g...@gmx.de>
Cc: Ilias Apalodimas <ilias.apalodi...@linaro.org>
Cc: Jerome Forissier <jerome.foriss...@linaro.org>
Cc: Joe Hershberger <joe.hershber...@ni.com>
Cc: Marek Vasut <ma...@denx.de>
Cc: Michal Simek <michal.si...@amd.com>
Cc: Patrick Delaunay <patrick.delau...@foss.st.com>
Cc: Rasmus Villemoes <r...@prevas.dk>
Cc: Simon Glass <s...@chromium.org>
Cc: Tom Rini <tr...@konsulko.com>
Cc: Venkatesh Yadav Abbarapu <venkatesh.abbar...@amd.com>
---
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(-)

diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index e5e89b51e0a..10fd93d4a45 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -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"
diff --git a/env/env.c b/env/env.c
index dbaeedc3c3b..f94976d4271 100644
--- a/env/env.c
+++ b/env/env.c
@@ -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;
diff --git a/include/env.h b/include/env.h
index 01c3eeae7e2..4553c7bc109 100644
--- a/include/env.h
+++ b/include/env.h
@@ -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
  *
-- 
2.30.2

Reply via email to