From f5c7c1813f52e6d7b59ecfb2f9f95e69b05b1980 Mon Sep 17 00:00:00 2001 From: Bruno Pena Date: Sat, 4 Jan 2020 12:52:08 +0100 Subject: [PATCH] fstools: Add support to read-only MTD partitions (eg. recovery images) 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 --- 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.25.1