mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] env: suggest global/nv command when calling setenv on non-existent parameter
@ 2024-07-26 11:25 Ahmad Fatoum
  2024-07-30  8:11 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2024-07-26 11:25 UTC (permalink / raw)
  To: barebox; +Cc: Sven Püschel, Casey Reeves, Ahmad Fatoum

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@pengutronix.de>
Reported-by: Casey Reeves <contact@xogium.me>
Signed-off-by: Ahmad Fatoum <a.fatoum@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




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] env: suggest global/nv command when calling setenv on non-existent parameter
  2024-07-26 11:25 [PATCH] env: suggest global/nv command when calling setenv on non-existent parameter Ahmad Fatoum
@ 2024-07-30  8:11 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2024-07-30  8:11 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum; +Cc: Sven Püschel, Casey Reeves


On Fri, 26 Jul 2024 13:25:56 +0200, Ahmad Fatoum wrote:
> 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:
> 
> [...]

Applied, thanks!

[1/1] env: suggest global/nv command when calling setenv on non-existent parameter
      https://git.pengutronix.de/cgit/barebox/commit/?id=ffe1597ec5b6 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-07-30  8:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-26 11:25 [PATCH] env: suggest global/nv command when calling setenv on non-existent parameter Ahmad Fatoum
2024-07-30  8:11 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox