[OpenWrt-Devel] [PATCH v2] fstools: Add support to read-only MTD partitions (eg. recovery images)
Bruno Pena
brunompena at gmail.com
Sat Jan 4 06:52:08 EST 2020
This patch enables fstools to open read-only MTD partitions, which in
turn also enables OpenWrt to boot from read-only partitions.
The use of read-only partitions is of special importance for WiFi-only
devices, where a protected read-only recovery image can be used in case
something goes wrong with the main firmware (eg. user gets locked out
due to bad settings, flash of an unbootable dev firmware, etc).
Signed-off-by: Bruno Pena <brunompena at gmail.com>
---
libfstools/mtd.c | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)
diff --git a/libfstools/mtd.c b/libfstools/mtd.c
index 77c71ee..aae633e 100644
--- a/libfstools/mtd.c
+++ b/libfstools/mtd.c
@@ -36,20 +36,31 @@ struct mtd_volume {
static struct driver mtd_driver;
+static int mtd_open_device(const char *dev)
+{
+ int ret;
+
+ ret = open(dev, O_RDWR | O_SYNC);
+ if (ret < 0)
+ ret = open(dev, O_RDONLY);
+
+ 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 +69,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.7.4
_______________________________________________
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