U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Cc: Sean Anderson <seanga2@gmail.com>, Simon Glass <sjg@chromium.org>,
	Andrew Goodbody <andrew.goodbody@linaro.org>,
	Heiko Schocher <hs@nabladev.com>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Jerome Forissier <jerome.forissier@arm.com>,
	Joe Hershberger <joe.hershberger@ni.com>,
	"Kory Maincent (TI.com)" <kory.maincent@bootlin.com>,
	Mattijs Korpershoek <mkorpershoek@kernel.org>,
	Mikhail Kshevetskiy <mikhail.kshevetskiy@iopsys.eu>,
	Quentin Schulz <quentin.schulz@cherry.de>,
	Tom Rini <trini@konsulko.com>
Subject: [RFC PATCH 08/11] cmd: hash: Use getopt() for option parsing
Date: Fri, 15 May 2026 14:32:59 -0600	[thread overview]
Message-ID: <20260515203311.2555651-9-sjg@chromium.org> (raw)
In-Reply-To: <20260515203311.2555651-1-sjg@chromium.org>

The open-coded check for -v only matches it as the first argument, so
'hash sha256 -v 0 4 <expected>' treats -v as the algorithm name and
fails. Replace with getopt(), which also lets -v appear anywhere in the
command line.

Guard the option-parsing loop with IS_ENABLED(CONFIG_HASH_VERIFY) so it
is compiled out when verification is disabled. After parsing,
getopt_pop() pulls the algorithm off the positional list, strlower()
normalises it, and the tail is forwarded to hash_command() unchanged.

CMD_HASH selects GETOPT so the parser is linked in on boards that did
not already enable it.

Tweak the var declarations to use Reverse Christmas Tree.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 cmd/Kconfig |  1 +
 cmd/hash.c  | 37 +++++++++++++++++++++----------------
 2 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 709696c3c41..eb7c85c1fe9 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -2798,6 +2798,7 @@ config CMD_BLOB
 
 config CMD_HASH
 	bool "Support 'hash' command"
+	select GETOPT
 	select HASH
 	help
 	  This provides a way to hash data in memory using various supported
diff --git a/cmd/hash.c b/cmd/hash.c
index 96d0e443a5b..566bb617328 100644
--- a/cmd/hash.c
+++ b/cmd/hash.c
@@ -11,8 +11,9 @@
 
 #include <command.h>
 #include <env.h>
+#include <getopt.h>
 #include <hash.h>
-#include <linux/ctype.h>
+#include <linux/string.h>
 
 #if IS_ENABLED(CONFIG_HASH_VERIFY)
 #define HARGS 6
@@ -23,25 +24,29 @@
 static int do_hash(struct cmd_tbl *cmdtp, int flag, int argc,
 		   char *const argv[])
 {
-	char *s;
 	int flags = HASH_FLAG_ENV;
+	struct getopt_state gs;
+	char *algo;
 
-	if (argc < 4)
-		return CMD_RET_USAGE;
+	getopt_init_state(&gs, argc, argv);
+	if (IS_ENABLED(CONFIG_HASH_VERIFY)) {
+		int opt;
 
-#if IS_ENABLED(CONFIG_HASH_VERIFY)
-	if (!strcmp(argv[1], "-v")) {
-		flags |= HASH_FLAG_VERIFY;
-		argc--;
-		argv++;
+		while ((opt = getopt(&gs, "v")) > 0) {
+			if (opt != 'v')
+				return CMD_RET_USAGE;
+			flags |= HASH_FLAG_VERIFY;
+		}
 	}
-#endif
-	/* Move forward to 'algorithm' parameter */
-	argc--;
-	argv++;
-	for (s = *argv; *s; s++)
-		*s = tolower(*s);
-	return hash_command(*argv, flags, cmdtp, flag, argc - 1, argv + 1);
+
+	/* Need at least: algorithm address count */
+	if (gs.nonopts < 3)
+		return CMD_RET_USAGE;
+
+	algo = strlower(getopt_pop(&gs));
+
+	return hash_command(algo, flags, cmdtp, flag,
+			    gs.nonopts, &gs.args[gs.index]);
 }
 
 U_BOOT_CMD(
-- 
2.43.0


  parent reply	other threads:[~2026-05-15 20:34 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-15 20:32 [RFC PATCH 00/11] Tidy command option parsing and use it a bit Simon Glass
2026-05-15 20:32 ` [RFC PATCH 01/11] lib: string: Add strlower() Simon Glass
2026-05-15 20:32 ` [RFC PATCH 02/11] cmd: ini: Use strlower() to normalise case Simon Glass
2026-05-15 20:32 ` [RFC PATCH 03/11] fs: fat: " Simon Glass
2026-05-15 20:32 ` [RFC PATCH 04/11] boot: pxe_utils: Use strlower() in get_string() Simon Glass
2026-05-15 20:32 ` [RFC PATCH 05/11] lib: getopt: Permute by default with inline reorder Simon Glass
2026-05-15 21:37   ` Sean Anderson
2026-05-20 20:42     ` Simon Glass
2026-05-15 20:32 ` [RFC PATCH 06/11] lib: getopt: Add getopt_pop() helper Simon Glass
2026-05-15 21:40   ` Sean Anderson
2026-05-20 20:41     ` Simon Glass
2026-05-15 20:32 ` [RFC PATCH 07/11] cmd: echo: Use getopt() with '+' prefix for option parsing Simon Glass
2026-05-15 21:58   ` Sean Anderson
2026-05-20 20:41     ` Simon Glass
2026-05-15 20:32 ` Simon Glass [this message]
2026-05-15 20:33 ` [RFC PATCH 09/11] cmd: nvedit: Use getopt() in env grep Simon Glass
2026-05-15 20:33 ` [RFC PATCH 10/11] cmd: nvedit: Use getopt() in env export and env import Simon Glass
2026-05-15 20:33 ` [RFC PATCH 11/11] doc: commands: Recommend getopt() for option parsing Simon Glass
2026-05-15 21:43 ` [RFC PATCH 00/11] Tidy command option parsing and use it a bit Tom Rini
2026-05-15 21:59   ` Sean Anderson
2026-05-15 22:06     ` Sean Anderson
2026-05-15 22:21       ` Tom Rini
2026-05-15 22:27         ` Sean Anderson
2026-05-20 20:40           ` Simon Glass

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=20260515203311.2555651-9-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=andrew.goodbody@linaro.org \
    --cc=hs@nabladev.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jerome.forissier@arm.com \
    --cc=joe.hershberger@ni.com \
    --cc=kory.maincent@bootlin.com \
    --cc=mikhail.kshevetskiy@iopsys.eu \
    --cc=mkorpershoek@kernel.org \
    --cc=quentin.schulz@cherry.de \
    --cc=seanga2@gmail.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.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