From a527b22f0a30d66d4674624d16f4bb0ffe2e94d0 Mon Sep 17 00:00:00 2001 From: Brandon Maier Date: Mon, 12 Dec 2022 12:01:57 -0600 Subject: libmtd: Add function to get MTD info by device name This is a convenience function for end users. In some situations it's easier to reference MTD device's by their name then by MTD number, as the name may be more reliable if device partitioning is dynamic or for porting between systems. Signed-off-by: Brandon Maier Signed-off-by: David Oberhollenzer --- lib/libmtd_legacy.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'lib/libmtd_legacy.c') diff --git a/lib/libmtd_legacy.c b/lib/libmtd_legacy.c index 4eb4a70..e0ecf49 100644 --- a/lib/libmtd_legacy.c +++ b/lib/libmtd_legacy.c @@ -428,3 +428,41 @@ int legacy_get_dev_info1(int mtd_num, struct mtd_dev_info *mtd) sprintf(node, MTD_DEV_PATT, mtd_num); return legacy_get_dev_info(node, mtd); } + +/** + * legacy_get_dev_info2 - legacy version of `mtd_get_dev_info2()` + * @name: name of the MTD device + * @mtd: the MTD device information is returned here + * + * This function is similar to 'mtd_get_dev_info2()' and has the same + * conventions. + */ +int legacy_get_dev_info2(const char *name, struct mtd_dev_info *mtd) +{ + struct proc_parse_info pi; + int ret, mtd_num = -1; + + ret = proc_parse_start(&pi); + if (ret) + return -1; + + while (proc_parse_next(&pi)) { + if (!strcmp(name, pi.name)) { + // Device name collision + if (mtd_num >= 0) { + errmsg("Multiple MTD's found matching name %s", name); + errno = ENODEV; + return -1; + } + + mtd_num = pi.mtd_num; + } + } + + if (mtd_num < 0) { + errno = ENODEV; + return -1; + } + + return legacy_get_dev_info1(mtd_num, mtd); +} -- cgit v1.2.3