[OpenWrt-Devel] [PATCH] fstools: Add support to read-only MTD partitions (eg. for recovery images)

brunompena at gmail.com brunompena at gmail.com
Tue Nov 5 18:01:09 EST 2019


From: Bruno Pena <brunompena at gmail.com>

Signed-off-by: Bruno Pena <brunompena at gmail.com>
---
 libfstools/mtd.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/libfstools/mtd.c b/libfstools/mtd.c
index 77c71ee..56dd1a9 100644
--- a/libfstools/mtd.c
+++ b/libfstools/mtd.c
@@ -36,20 +36,32 @@ struct mtd_volume {
 
 static struct driver mtd_driver;
 
+static int mtd_open_device(const char *dev)
+{
+	int ret;
+
+	if ((ret = open(dev, O_RDWR | O_SYNC)) > -1)
+		ULOG_INFO("Opened mtd device: %s (read-write)\n", dev);
+	else if ((ret = open(dev, O_RDONLY)) > -1)
+		ULOG_INFO("Opened mtd device: %s (read-only)\n", dev);
+
+	return ret;
+}
+
 static int mtd_open(const char *mtd, int block)
 {
 	FILE *fp;
 	char dev[PATH_MAX];
-	int i, ret, flags = O_RDWR | O_SYNC;
+	int i, ret;
 
 	if ((fp = fopen("/proc/mtd", "r"))) {
 		while (fgets(dev, sizeof(dev), fp)) {
 			if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
 				snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
-				ret = open(dev, flags);
+				ret = mtd_open_device(dev);
 				if (ret < 0) {
 					snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);
-					ret = open(dev, flags);
+					ret = mtd_open_device(dev);
 				}
 				fclose(fp);
 				return ret;
@@ -58,7 +70,7 @@ static int mtd_open(const char *mtd, int block)
 		fclose(fp);
 	}
 
-	return open(mtd, flags);
+	return mtd_open_device(mtd);
 }
 
 static void mtd_volume_close(struct mtd_volume *p)
-- 
2.23.0


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list