mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: "Sven Püschel" <s.pueschel@pengutronix.de>,
	"Casey Reeves" <contact@xogium.me>,
	"Ahmad Fatoum" <a.fatoum@pengutronix.de>
Subject: [PATCH] env: suggest global/nv command when calling setenv on non-existent parameter
Date: Fri, 26 Jul 2024 13:25:56 +0200	[thread overview]
Message-ID: <20240726112556.3548806-1-a.fatoum@pengutronix.de> (raw)

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




             reply	other threads:[~2024-07-26 11:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-26 11:25 Ahmad Fatoum [this message]
2024-07-30  8:11 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240726112556.3548806-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=contact@xogium.me \
    --cc=s.pueschel@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox