]> www.infradead.org Git - mtd-utils.git/commitdiff
libmtd: Add function to get MTD info by device name
authorBrandon Maier <brandon.maier@collins.com>
Mon, 12 Dec 2022 18:01:57 +0000 (12:01 -0600)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Tue, 30 May 2023 09:12:12 +0000 (11:12 +0200)
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 <brandon.maier@collins.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
include/libmtd.h
lib/libmtd.c
lib/libmtd_int.h
lib/libmtd_legacy.c

index 6ab0de55ff12d83ded883b3159a44545584964a0..f909a16adf639f7f8be1ab0aa508d8cf7487a94a 100644 (file)
@@ -130,7 +130,7 @@ int mtd_get_info(libmtd_t desc, struct mtd_info *info);
 /**
  * mtd_get_dev_info - get information about an MTD device.
  * @desc: MTD library descriptor
- * @node: name of the MTD device node
+ * @node: path of the MTD device node
  * @mtd: the MTD device information is returned here
  *
  * This function gets information about MTD device defined by the @node device
@@ -151,6 +151,17 @@ int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd);
  */
 int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd);
 
+/**
+ * mtd_get_dev_info2 - get information about an MTD device.
+ * @desc: MTD library descriptor
+ * @name: name of the MTD device
+ * @mtd: the MTD device information is returned here
+ *
+ * This function is identical to 'mtd_get_dev_info()' except that it accepts
+ * MTD device's name, not MTD character device.
+ */
+int mtd_get_dev_info2(libmtd_t desc, const char *name, struct mtd_dev_info *mtd);
+
 /**
  * mtd_lock - lock eraseblocks.
  * @desc: MTD library descriptor
index 4aee947a4a90e061fc99f96eec328ca38cf2d0ec..f588e09b8007c438ebbaca897c903162881d4873 100644 (file)
@@ -427,7 +427,7 @@ static int type_str2int(const char *str)
 /**
  * dev_node2num - find MTD device number by its character device node.
  * @lib: MTD library descriptor
- * @node: name of the MTD device node
+ * @node: path of the MTD device node
  * @mtd_num: MTD device number is returned here
  *
  * This function returns %0 in case of success and %-1 in case of failure.
@@ -476,6 +476,58 @@ static int dev_node2num(struct libmtd *lib, const char *node, int *mtd_num)
        return -1;
 }
 
+/**
+ * dev_name2num - find MTD device number by its MTD name
+ * @lib: MTD library descriptor
+ * @name: name of the MTD device
+ * @mtd_num: MTD device number is returned here
+ *
+ * This function returns %0 in case of success and %-1 in case of failure.
+ */
+static int dev_name2num(struct libmtd *lib, const char *name, int *mtd_num)
+{
+       struct mtd_info info;
+       char name2[MTD_NAME_MAX + 1];
+       int i, mtd_num_tmp = -1;
+
+       if (mtd_get_info((libmtd_t *)lib, &info))
+               return -1;
+
+       for (i = info.lowest_mtd_num; i <= info.highest_mtd_num; i++) {
+               int ret;
+
+               ret = dev_read_data(lib->mtd_name, i, name2,
+                                   MTD_NAME_MAX + 1);
+               if (ret < 0) {
+                       if (errno == ENOENT)
+                               continue;
+                       if (!errno)
+                               break;
+                       return -1;
+               }
+               name2[ret - 1] = '\0';
+
+               if (!strcmp(name, name2)) {
+                       // Device name collision
+                       if (mtd_num_tmp >= 0) {
+                               errmsg("Multiple MTD's found matching name %s", name);
+                               errno = ENODEV;
+                               return -1;
+                       }
+
+                       mtd_num_tmp = i;
+               }
+       }
+
+       if (mtd_num_tmp < 0) {
+               errno = ENODEV;
+               return -1;
+       }
+
+       *mtd_num = mtd_num_tmp;
+       return 0;
+}
+
 /**
  * sysfs_is_supported - check whether the MTD sub-system supports MTD.
  * @lib: MTD library descriptor
@@ -817,6 +869,20 @@ int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd)
        return mtd_get_dev_info1(desc, mtd_num, mtd);
 }
 
+int mtd_get_dev_info2(libmtd_t desc, const char *name, struct mtd_dev_info *mtd)
+{
+       int mtd_num;
+       struct libmtd *lib = (struct libmtd *)desc;
+
+       if (!lib->sysfs_supported)
+               return legacy_get_dev_info2(name, mtd);
+
+       if (dev_name2num(lib, name, &mtd_num))
+               return -1;
+
+       return mtd_get_dev_info1(desc, mtd_num, mtd);
+}
+
 static inline int mtd_ioctl_error(const struct mtd_dev_info *mtd, int eb,
                                  const char *sreq)
 {
index c0514d2589c655b830988318b55d452a9d27d6d0..79200f77c27e8e487926786fc7d9044aeb9416a0 100644 (file)
@@ -106,6 +106,7 @@ int legacy_dev_present(int mtd_num);
 int legacy_mtd_get_info(struct mtd_info *info);
 int legacy_get_dev_info(const char *node, struct mtd_dev_info *mtd);
 int legacy_get_dev_info1(int dev_num, struct mtd_dev_info *mtd);
+int legacy_get_dev_info2(const char *name, struct mtd_dev_info *mtd);
 int legacy_get_mtd_oobavail(const char *node);
 int legacy_get_mtd_oobavail1(int mtd_num);
 
index 4eb4a70919c478bec26f45e82bd13c1ef33709ef..e0ecf49abec5636973d503b0371e938d887d7c7c 100644 (file)
@@ -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);
+}