From: Torstein Eide <torsteine@gmail.com>
To: linux-mmc@vger.kernel.org
Cc: Torstein Eide <torsteine+linux@gmail.com>
Subject: [PATCH v2 3/5] mmc-utils: lsmmc: Accept /dev and /sys/block paths for register reads
Date: Fri, 15 May 2026 23:37:45 +0200 [thread overview]
Message-ID: <20260515213747.1452692-4-torsteine+linux@gmail.com> (raw)
In-Reply-To: <20260515213747.1452692-1-torsteine+linux@gmail.com>
Commands like 'cid read' previously required a raw sysfs device path
such as /sys/bus/mmc/devices/mmc0:0001/. Most users know their block
device as /dev/mmcblk0 or /sys/block/mmcblk0.
Add resolve_dev_path() which follows /sys/class/block/<dev>/device via
realpath() to reach the canonical sysfs device directory. Paths that
start with /dev/ or /sys/block/ are resolved automatically in
do_read_reg() before processing, and the resolved sysfs path is printed
so the caller can see which device was matched.
Signed-off-by: Torstein Eide <torsteine+linux@gmail.com>
---
docs/HOWTO.rst | 59 +++++++++++++++++++++++++++++++++++++++++--------
lsmmc.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 110 insertions(+), 9 deletions(-)
diff --git a/docs/HOWTO.rst b/docs/HOWTO.rst
index 7a27a50..76214a3 100644
--- a/docs/HOWTO.rst
+++ b/docs/HOWTO.rst
@@ -35,19 +35,60 @@ Running mmc-utils
Set user area write protection.
``csd read [-h] [-v] [-b bus_type] [-r register] <device path>``
- Print CSD data from <device path>. The device path should specify the csd sysfs file directory.
- if [bus_type] is passed (mmc or sd) the [register] content must be passed as well, and no need for device path.
- it is useful for cases we are getting the register value without having the actual platform.
+ Print CSD data from <device path>. The device path may be one of the following:
+ - sysfs device directory (/sys/devices/platform/fe310000.mmc/mmc_host/mmc0/mmc0:0001)
+ - device node (/dev/mmcblkN)
+ - sysfs block device entry (/sys/block/mmcblkN)
+ /dev/ and /sys/block/ paths are resolved to the sysfs device
+ directory automatically and the resolved path is printed.
+ If [-b bus_type] is passed (mmc or sd) the [-r register] content must
+ be passed as well, and no device path is required. Useful when the
+ register value is known without access to the actual hardware.
+
+ Example::
+
+ $ mmc csd read /dev/mmcblk0
+ sysfs: /sys/devices/platform/fe310000.mmc/mmc_host/mmc0/mmc0:0001
+ CSD Register: 00000000...
``cid read <device path>``
- Print CID data from <device path>. The device path should specify the cid sysfs file directory.
- if [bus_type] is passed (mmc or sd) the [register] content must be passed as well, and no need for device path.
- it is useful for cases we are getting the register value without having the actual platform.
+ Print CID data from <device path>. The device path may be one of the following:
+ - sysfs device directory (/sys/devices/platform/fe310000.mmc/mmc_host/mmc0/mmc0:0001)
+ - device node (/dev/mmcblkN)
+ - sysfs block device entry (/sys/block/mmcblkN)
+ /dev/ and /sys/block/ paths are resolved to the sysfs device
+ directory automatically and the resolved path is printed.
+ If [-b bus_type] is passed (mmc or sd) the [-r register] content must
+ be passed as well, and no device path is required. Useful when the
+ register value is known without access to the actual hardware.
+
+ Example::
+
+ $ mmc cid read /dev/mmcblk0
+ sysfs: /sys/devices/platform/fe310000.mmc/mmc_host/mmc0/mmc0:0001
+ Manufacturer ID: 0x15
+ OEM ID: 0x0100
+ Product name: MAG4FA
+ Product revision: 1.0
+ Serial number: 0x1a2b3c4d
+ Manufacturing date: 01/2021
``scr read <device path>``
- Print SCR data from <device path>. The device path should specify the scr sysfs file directory.
- if [bus_type] is passed (mmc or sd) the [register] content must be passed as well, and no need for device path.
- it is useful for cases we are getting the register value without having the actual platform.
+ Print SCR data from <device path>. The device path may be one of the following:
+ - sysfs device directory (/sys/devices/platform/fe320000.mmc/mmc_host/mmc1/mmc1:aaaa)
+ - device node (/dev/mmcblkN)
+ - sysfs block device entry (/sys/block/mmcblkN)
+ /dev/ and /sys/block/ paths are resolved to the sysfs device
+ directory automatically and the resolved path is printed.
+ If [-b bus_type] is passed (mmc or sd) the [-r register] content must
+ be passed as well, and no device path is required. Useful when the
+ register value is known without access to the actual hardware.
+
+ Example::
+
+ $ mmc scr read /dev/mmcblk1
+ sysfs: /sys/devices/platform/fe320000.mmc/mmc_host/mmc1/mmc1:aaaa
+ SCR Register: 0235800000000000
``ffu <image name> <device> [chunk-bytes]``
Default mode. Run Field Firmware Update with `<image name>` on `<device>`. `[chunk-bytes]` is optional and defaults to its max - 512k. Should be in decimal bytes and sector aligned.
diff --git a/lsmmc.c b/lsmmc.c
index 1c55b86..58d4609 100644
--- a/lsmmc.c
+++ b/lsmmc.c
@@ -281,6 +281,51 @@ static char *read_file_at(const char *dir, const char *name)
return strdup(start);
}
+static char *resolve_dev_path(const char *path)
+{
+ const char *devname = strrchr(path, '/');
+ char basedev[NAME_MAX + 1];
+ char syspath[PATH_MAX];
+ char resolved[PATH_MAX];
+ char *p, *type;
+
+ devname = devname ? devname + 1 : path;
+ if (*devname == '\0')
+ return NULL;
+
+ /* strip partition suffix: mmcblk0p1 -> mmcblk0 */
+ strncpy(basedev, devname, sizeof(basedev) - 1);
+ basedev[sizeof(basedev) - 1] = '\0';
+ p = basedev + strlen(basedev);
+ while (p > basedev && isdigit((unsigned char)p[-1]))
+ p--;
+ if (p > basedev && p[-1] == 'p')
+ p[-1] = '\0';
+
+ if (snprintf(syspath, sizeof(syspath), "/sys/class/block/%s/device",
+ basedev) >= PATH_MAX)
+ return NULL;
+
+ if (realpath(syspath, resolved) == NULL) {
+ fprintf(stderr, "Cannot resolve '%s': %s\n", path, strerror(errno));
+ return NULL;
+ }
+
+ type = read_file_at(resolved, "type");
+ if (!type) {
+ fprintf(stderr, "'%s' does not appear to be an MMC/SD device\n", path);
+ return NULL;
+ }
+ if (strcmp(type, "MMC") && strcmp(type, "SD")) {
+ fprintf(stderr, "'%s': unknown device type '%s'\n", path, type);
+ free(type);
+ return NULL;
+ }
+ free(type);
+
+ return strdup(resolved);
+}
+
/* Hexadecimal string parsing functions */
static char *to_binstr(char *hexstr)
{
@@ -2162,6 +2207,21 @@ static int do_read_reg(int argc, char **argv, enum REG_TYPE reg)
if (ret)
return ret;
+ if (cfg.dir &&
+ (strncmp(cfg.dir, "/dev/", 5) == 0 ||
+ strncmp(cfg.dir, "/sys/block/", 11) == 0)) {
+ char *sysfs = resolve_dev_path(cfg.dir);
+
+ if (!sysfs) {
+ free(cfg.dir);
+ return -1;
+ }
+
+ free(cfg.dir);
+ cfg.dir = sysfs;
+ printf("sysfs: %s\n", cfg.dir);
+ }
+
if (cfg.dir) {
ret = process_dir(&cfg, reg);
free(cfg.dir);
--
2.53.0
next prev parent reply other threads:[~2026-05-15 21:38 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-15 21:37 [PATCH v2 0/5] mmc-utils: improve lsmmc usability Torstein Eide
2026-05-15 21:37 ` [PATCH v2 1/5] mmc-utils: lsmmc: Refactor CID parsing into shared structs Torstein Eide
2026-05-15 21:37 ` [PATCH v2 2/5] mmc-utils: lsmmc: Use external .ids files for manufacturer lookup Torstein Eide
2026-05-15 21:37 ` Torstein Eide [this message]
2026-05-15 21:37 ` [PATCH v2 4/5] mmc-utils: lsmmc: Add mmc list command Torstein Eide
2026-05-15 21:37 ` [PATCH v2 5/5] mmc-utils: Add bash completion Torstein Eide
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=20260515213747.1452692-4-torsteine+linux@gmail.com \
--to=torsteine@gmail.com \
--cc=linux-mmc@vger.kernel.org \
--cc=torsteine+linux@gmail.com \
/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