[PATCH] env: suggest global/nv command when calling setenv on non-existent parameter

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Jul 26 04:25:56 PDT 2024


Unlike regular environment variables, device parameters have to be
explicitly created before they are used:

  global.linux.bootargs.memsize="mem=3072M"
  Cannot set parameter global.linux.bootargs.memsize: No such device

The common remedy is to remove the first dot to call the global command
instead that will create the device parameter and then set it:

  global linux.bootargs.memsize="mem=3072M"

This is a common source of confusion and new users trip over it so
often, that we should do something about it.

Initially I wanted the setting of linux.bootargs.* linux.mtdparts.*
and linux.blkdevparts.* to automatically create the variable.
This is easier said than done though and would need a rework of existing
code as well as violation of the current layering.

Instead, we take the easy way out and just suggest the user that the
user create the variable in question if it's not there yet:

  Parameter not found. Did you mean to create it with: global linux.bootargs.memsize='mem=3072M' ?

While at it, we also replace strerror() with %pe.

Reported-by: Sven Püschel <s.pueschel at pengutronix.de>
Reported-by: Casey Reeves <contact at xogium.me>
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/env.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/common/env.c b/common/env.c
index c231bc8b1d7a..1e57e09e9de7 100644
--- a/common/env.c
+++ b/common/env.c
@@ -255,11 +255,19 @@ int setenv(const char *_name, const char *value)
 	char *name = strdup(_name);
 	int ret = 0;
 	struct list_head *list;
+	const char *dot;
 
-	if (strchr(name, '.')) {
+	dot = strchr(name, '.');
+	if (dot) {
 		ret = dev_setenv(name, value);
-		if (ret)
-			eprintf("Cannot set parameter %s: %s\n", name, strerror(-ret));
+		if (IS_ENABLED(CONFIG_LONGHELP) && ret == -ENODEV &&
+		    (strstarts(name, "nv.") || strstarts(name, "global."))) {
+			eprintf("Parameter not found. Did you mean to create it with: %.*s %s='%s' ?\n",
+				(int)(dot - name), name, dot + 1, value);
+		} else if (ret) {
+			eprintf("Cannot set parameter %s: %pe\n", name, ERR_PTR(ret));
+		}
+
 		goto out;
 	}
 
-- 
2.39.2




More information about the barebox mailing list