diff options
author | Brandon Maier <brandon.maier@collins.com> | 2022-12-12 12:01:58 -0600 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-05-30 11:12:12 +0200 |
commit | 730148bc94411f13a0171204e872b0760fbde185 (patch) | |
tree | 8b07dd5ef274c0e4e36944f4ef6929399a77e56f /lib/common.c | |
parent | a527b22f0a30d66d4674624d16f4bb0ffe2e94d0 (diff) |
mtd-utils: Add new syntax to get devices by name
This introduces a new feature to the MTD command line utilities that
allows MTD devices to be referenced by name instead of device node. For
example this looks like:
> # Display info for the MTD device with name "data"
> mtdinfo mtd:data
> # Copy file to MTD device with name "data"
> flashcp /my/file mtd:data
This follows the syntax supported by the kernel which allows MTD
device's to be mounted by name[1].
Add the function mtd_find_dev_node() that accepts an MTD "identifier"
and returns the MTD's device node. The function accepts a string
starting with "mtd:" which it treats as the MTD's name. It then attempts
to search for the MTD, and if found maps it back to the /dev/mtdX device
node. If the string does not start with "mtd:", then assume it's the old
style and refers directly to a MTD device node.
The function is then hooked into existing tools like flashcp, mtdinfo,
flash_unlock, etc. To load in the new MTD parsing code in a consistent
way across programs.
[1] http://www.linux-mtd.infradead.org/faq/jffs2.html#L_mtdblock
Signed-off-by: Brandon Maier <brandon.maier@collins.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/common.c')
-rw-r--r-- | lib/common.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/common.c b/lib/common.c index 8041878..e278593 100644 --- a/lib/common.c +++ b/lib/common.c @@ -33,6 +33,9 @@ #include <stdlib.h> #include <unistd.h> #include "common.h" +#include "libmtd.h" + +#define MTD_DEV_PATT "/dev/mtd%d" /** * get_multiplier - convert size specifier to an integer multiplier. @@ -162,3 +165,46 @@ int util_srand(void) srand(seed); return 0; } + +/** + * mtd_find_dev_node - Find the device node for an MTD + * @id: Identifier for the MTD. this can be the device node itself, or + * "mtd:<name>" to look up MTD by name + * + * This is a helper function to convert MTD device identifiers into their + * device node. + * + * Returns a pointer to a string containing the device node that must be + * free'd, or NULL on failure. + */ +char *mtd_find_dev_node(const char *id) +{ + struct mtd_dev_info info; + struct libmtd_t *lib_mtd; + char *node; + int ret; + + if (strncmp(id, "mtd:", 4)) { + /* Assume @id is the device node */ + return strdup(id); + } + + /* Search for MTD matching name */ + id += 4; + + lib_mtd = libmtd_open(); + if (!lib_mtd) + return NULL; + + ret = mtd_get_dev_info2(lib_mtd, id, &info); + libmtd_close(lib_mtd); + if (ret < 0) + return NULL; + + node = malloc(strlen(MTD_DEV_PATT) + 20); + if (!node) + return NULL; + + sprintf(node, MTD_DEV_PATT, info.mtd_num); + return node; +} |