Skip to contentSkip to conversation

mtdinfo fixes (--all, etc.)

PATCH New Brian Norris 1 participant 4 messages Feb 9, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover mtdinfo fixes (--all, etc.)

These fixes help `mtdinfo --all' to work on legacy systems where sysfs is not supported in MTD.

Patch 1: submitted by Artem as a test patch
Patch 2: previously submitted by me and didn't apply directly to
mtd-utils.git, as it depended on Patch 1
Patch 3: trivial (new) grammar fixup patch

Hopefully this is easy to review and apply :)

Brian

Artem Bityutskiy (1):
limbtd: implement mtd_dev_present for old kernels

Brian Norris (2):
libmtd: fix segmentation fault on lib->mtd
mtdinfo: correct grammar on error message

 lib/libmtd.c        |   27 ++++++++++++++-------------
 lib/libmtd_int.h    |    1 +
 lib/libmtd_legacy.c |   25 +++++++++++++++++++++++++
 ubi-utils/mtdinfo.c |    2 +-
 4 files changed, 41 insertions(+), 14 deletions(-)
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 1/3 limbtd: implement mtd_dev_present for old kernels

From: Artem Bityutskiy <[email protected]>

Implement the 'legacy_dev_present()' function which will check whether an MTD device is present by scanning the /proc/mtd file when the MTD subsystem does not support sysfs (the case for pre-2.6.30 kernels).

This patch also moves the 'mtd_dev_present()' function to a slightly more logical position.

---
 lib/libmtd.c        |   25 ++++++++++++-------------
 lib/libmtd_int.h    |    1 +
 lib/libmtd_legacy.c |   25 +++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 13 deletions(-)
lib/libmtd.c +12 -13
index da9da00..2bb4e57 100644
@@ -641,6 +641,18 @@ void libmtd_close(libmtd_t desc)
 	free(lib);
 }
 
+int mtd_dev_present(libmtd_t desc, int mtd_num) {
+	struct stat st;
+	struct libmtd *lib = (struct libmtd *)desc;
+	char file[strlen(lib->mtd) + 10];
+
+	if (!lib->sysfs_supported)
+		return legacy_dev_present(mtd_num);
+
+	sprintf(file, lib->mtd, mtd_num);
+	return !stat(file, &st);
+}
+
 int mtd_get_info(libmtd_t desc, struct mtd_info *info)
 {
 	DIR *sysfs_mtd;
@@ -713,19 +725,6 @@ out_close:
 	return -1;
 }
 
-int mtd_dev_present(libmtd_t desc, int mtd_num) {
-	struct stat st;
-	struct libmtd *lib = (struct libmtd *)desc;
-	char file[strlen(lib->mtd) + 10];
-
-	if (!lib->sysfs_supported)
-		/* TODO: add legacy_dev_present() function */
-		return 1;
-
-	sprintf(file, lib->mtd, mtd_num);
-	return !stat(file, &st);
-}
-
 int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd)
 {
 	int ret;
lib/libmtd_int.h +1 -0
index bb48d35..7913e67 100644
@@ -95,6 +95,7 @@ struct libmtd
 };
 
 int legacy_libmtd_open(void);
+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);
lib/libmtd_legacy.c +25 -0
index f0646f3..1ae9946 100644
@@ -170,6 +170,31 @@ int legacy_libmtd_open(void)
 }
 
 /**
+ * legacy_dev_presentl - legacy version of 'mtd_dev_present()'.
+ * @info: the MTD device information is returned here
+ *
+ * When the kernel does not provide sysfs files for the MTD subsystem,
+ * fall-back to parsing the /proc/mtd file to determine whether an mtd device
+ * number @mtd_num is present.
+ */
+int legacy_dev_present(int mtd_num)
+{
+	int ret;
+	struct proc_parse_info pi;
+
+	ret = proc_parse_start(&pi);
+	if (ret)
+		return -1;
+
+	while (proc_parse_next(&pi)) {
+		if (pi.mtd_num == mtd_num)
+			return 1;
+	}
+
+	return 0;
+}
+
+/**
  * legacy_mtd_get_info - legacy version of 'mtd_get_info()'.
  * @info: the MTD device information is returned here
  *
Signature · 1 line
1.7.5.4
#2 lore raw

PATCH 2/3 libmtd: fix segmentation fault on lib->mtd

In reply to Brian Norris

Legacy systems do not initialize lib->mtd, so we shouldn't perform strlen(lib->mtd); this produces a segmentation fault. As this code isn't used in the legacy codepath, we can just move it down to an 'else' branch.

---
 lib/libmtd.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
lib/libmtd.c +5 -3
index 2bb4e57..fb4586c 100644
@@ -644,13 +644,15 @@ void libmtd_close(libmtd_t desc)
 int mtd_dev_present(libmtd_t desc, int mtd_num) {
 	struct stat st;
 	struct libmtd *lib = (struct libmtd *)desc;
-	char file[strlen(lib->mtd) + 10];
 
 	if (!lib->sysfs_supported)
 		return legacy_dev_present(mtd_num);
+	else {
+		char file[strlen(lib->mtd) + 10];
 
-	sprintf(file, lib->mtd, mtd_num);
-	return !stat(file, &st);
+		sprintf(file, lib->mtd, mtd_num);
+		return !stat(file, &st);
+	}
 }
 
 int mtd_get_info(libmtd_t desc, struct mtd_info *info)
Signature · 1 line
1.7.5.4
#3 lore raw

PATCH 3/3 mtdinfo: correct grammar on error message

In reply to Brian Norris
---
 ubi-utils/mtdinfo.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
ubi-utils/mtdinfo.c +1 -1
index d25595a..a9dc675 100644
@@ -356,7 +356,7 @@ static int print_general_info(libmtd_t libmtd, const struct mtd_info *mtd_info,
 		if (err == -1) {
 			if (errno == ENODEV)
 				continue;
-			return sys_errmsg("libmtd failed get MTD device %d "
+			return sys_errmsg("libmtd failed to get MTD device %d "
 					  "information", i);
 		}
 
Signature · 1 line
1.7.5.4