From: Bruno Pena Date: Sat, 4 Jan 2020 11:52:08 +0000 (+0100) Subject: fstools: Add support to read-only MTD partitions (eg. recovery images) X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=f5c7c1813f52e6d7b59ecfb2f9f95e69b05b1980;p=oweals%2Ffstools.git 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 --- 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)