From: Yao Zi <me@ziyao.cc>
To: Peng Fan <peng.fan@nxp.com>,
Jaehoon Chung <jh80.chung@samsung.com>,
Tom Rini <trini@konsulko.com>,
Jerome Forissier <jerome.forissier@linaro.org>,
Mattijs Korpershoek <mkorpershoek@kernel.org>
Cc: u-boot@lists.denx.de, Yao Zi <me@ziyao.cc>
Subject: [PATCH 2/3] cmd: mmc: Check whether arguments are valid numbers in dev subcommand
Date: Fri, 30 Jan 2026 18:03:52 +0000 [thread overview]
Message-ID: <20260130180353.42205-3-me@ziyao.cc> (raw)
In-Reply-To: <20260130180353.42205-1-me@ziyao.cc>
Currently when any of speed_mode, part, or dev fails to be parse as a
number, no error is reported. In this case __init_mmc_device() is called
with weird arguments, probably zeroes if there's no digit prefixing the
argument, which is especially confusing when the invocation occasionally
succeeds.
Let's check whether arguments are valid numbers without trailing
characters. This is quite helpful for speed_mode: it requires an index
instead of a mode name, one may easily pass in a string, which will be
parsed as zero (MMC_LEGACY), without carefully reading the
documentation, then finds the MMC device is under an unexpected mode.
Signed-off-by: Yao Zi <me@ziyao.cc>
---
cmd/mmc.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/cmd/mmc.c b/cmd/mmc.c
index f2a59af087cb..512bd482ae82 100644
--- a/cmd/mmc.c
+++ b/cmd/mmc.c
@@ -559,15 +559,25 @@ static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
{
enum bus_mode speed_mode = MMC_MODES_END;
int dev = curr_device, part = 0, ret;
+ char *endp;
struct mmc *mmc;
switch (argc) {
case 4:
- speed_mode = (int)dectoul(argv[3], NULL);
+ speed_mode = (int)dectoul(argv[3], &endp);
+ if (*endp) {
+ printf("Invalid speed mode index '%s', "
+ "did you specify a mode name?\n", argv[3]);
+ return CMD_RET_USAGE;
+ }
+
fallthrough;
case 3:
- part = (int)dectoul(argv[2], NULL);
- if (part > PART_ACCESS_MASK) {
+ part = (int)dectoul(argv[2], &endp);
+ if (*endp) {
+ printf("Invalid part number '%s'\n", argv[2]);
+ return CMD_RET_USAGE;
+ } else if (part > PART_ACCESS_MASK) {
printf("#part_num shouldn't be larger than %d\n",
PART_ACCESS_MASK);
return CMD_RET_FAILURE;
@@ -575,7 +585,12 @@ static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
fallthrough;
case 2:
- dev = (int)dectoul(argv[1], NULL);
+ dev = (int)dectoul(argv[1], &endp);
+ if (*endp) {
+ printf("Invalid device number '%s'\n", argv[1]);
+ return CMD_RET_USAGE;
+ }
+
fallthrough;
case 1:
mmc = __init_mmc_device(dev, true, speed_mode);
--
2.52.0
next prev parent reply other threads:[~2026-01-30 18:04 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-30 18:03 [PATCH 0/3] Code improvements and argument checks for mmc dev subcommand Yao Zi
2026-01-30 18:03 ` [PATCH 1/3] cmd: mmc: Simplify dev subcommand handling Yao Zi
2026-01-30 18:03 ` Yao Zi [this message]
2026-01-30 18:03 ` [PATCH 3/3] cmd: mmc: Return symbolic value when part switching fails in mmc dev Yao Zi
2026-02-02 7:51 ` [PATCH 0/3] Code improvements and argument checks for mmc dev subcommand Anshul Dalal
2026-02-03 11:55 ` Peng Fan
2026-02-03 14:18 ` Peng Fan (OSS)
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=20260130180353.42205-3-me@ziyao.cc \
--to=me@ziyao.cc \
--cc=jerome.forissier@linaro.org \
--cc=jh80.chung@samsung.com \
--cc=mkorpershoek@kernel.org \
--cc=peng.fan@nxp.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.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