mtd: allow to use dump and verify on read-only devices
authorPaweł Owoc <[email protected]>
Sun, 9 Nov 2025 10:21:37 +0000 (11:21 +0100)
committerHauke Mehrtens <[email protected]>
Mon, 24 Nov 2025 23:43:23 +0000 (00:43 +0100)
Dump and verify commands can be used on read-only devices.

Signed-off-by: Paweł Owoc <[email protected]>
Link: https://github.com/openwrt/openwrt/pull/20725
Signed-off-by: Hauke Mehrtens <[email protected]>
12 files changed:
package/system/mtd/Makefile
package/system/mtd/src/fis.c
package/system/mtd/src/imagetag.c
package/system/mtd/src/jffs2.c
package/system/mtd/src/linksys_bootcount.c
package/system/mtd/src/mtd.c
package/system/mtd/src/mtd.h
package/system/mtd/src/seama.c
package/system/mtd/src/tpl_ramips_recoveryflag.c
package/system/mtd/src/trx.c
package/system/mtd/src/wrg.c
package/system/mtd/src/wrgg.c

index d45f06fc4354de145861060b40286bae992b4f41..d83e3599d86251958f6f07d5199d56a3df4cfe45 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 include $(INCLUDE_DIR)/kernel.mk
 
 PKG_NAME:=mtd
-PKG_RELEASE:=26
+PKG_RELEASE:=27
 
 PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
 STAMP_PREPARED := $(STAMP_PREPARED)_$(call confvar,CONFIG_MTD_REDBOOT_PARTS)
index 8bde9af6dc699b5fa616db44117280a6557d7506..8f719901b801b404353a9a88f9ede37b247af851 100644 (file)
@@ -67,12 +67,12 @@ fis_open(void)
        if (fis_fd >= 0)
                fis_close();
 
-       fis_fd = mtd_check_open("FIS directory");
+       fis_fd = mtd_check_open("FIS directory", true);
        if (fis_fd < 0)
                goto error;
 
        close(fis_fd);
-       fis_fd = mtd_open("FIS directory", true);
+       fis_fd = mtd_open("FIS directory", true, true);
        if (fis_fd < 0)
                goto error;
 
index 47642478f57ea9428c15faa80404121f70424c92..84aadf9cd6ec716891cf85ddbb1bff02610484da 100644 (file)
@@ -195,7 +195,7 @@ trx_fixup(int fd, const char *name)
                goto err;
        }
 
-       bfd = mtd_open(name, true);
+       bfd = mtd_open(name, true, true);
        ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
        if (!ptr || (ptr == (void *) -1)) {
                perror("mmap");
@@ -269,7 +269,7 @@ trx_check(int imagefd, const char *mtd, char *buf, int *len)
        }
 
        /* check if image fits to mtd device */
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                exit(1);
@@ -308,7 +308,7 @@ mtd_fixtrx(const char *mtd, size_t offset, size_t data_size)
        block_offset = offset & ~(erasesize - 1);
        offset -= block_offset;
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                exit(1);
index 2cfc878b10cf6e32b6c7cb2d7615dacc32b87df5..cdf9cc1ccd51e43ac32c39d4ee3c8bb1221273b9 100644 (file)
@@ -287,7 +287,7 @@ int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir)
 {
        int err = -1, fdeof = 0;
 
-       outfd = mtd_check_open(mtd);
+       outfd = mtd_check_open(mtd, true);
        if (outfd < 0)
                return -1;
 
index 3ec0b61718c3d847a554fc8946a51e2ddee5442f..9de617d086a0b921ef04bd59d3b91e975be7f461 100644 (file)
@@ -83,7 +83,7 @@ int mtd_resetbc(const char *mtd)
 
        DLOG_OPEN();
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
 
        if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) {
                DLOG_ERR("Unable to obtain mtd_info for given partition name.");
index fc7071d940833d2595b1cf57bc14e6671f8d5dc1..be6de2f63e72fafbfdb4999c0f6f4b55980fc663 100644 (file)
@@ -96,15 +96,19 @@ int jffs2_skip_bytes=0;
 int mtdtype = 0;
 uint32_t opt_trxmagic = TRX_MAGIC;
 
-int mtd_open(const char *mtd, bool block)
+int mtd_open(const char *mtd, bool block, bool write_mode)
 {
        FILE *fp;
        char dev[PATH_MAX];
        int i;
        int ret;
-       int flags = O_RDWR | O_SYNC;
+       int flags = O_RDONLY;
        char name[PATH_MAX];
 
+       if(write_mode) {
+               flags = O_RDWR | O_SYNC;
+       }
+
        snprintf(name, sizeof(name), "\"%s\"", mtd);
        if ((fp = fopen("/proc/mtd", "r"))) {
                while (fgets(dev, sizeof(dev), fp)) {
@@ -124,12 +128,12 @@ int mtd_open(const char *mtd, bool block)
        return open(mtd, flags);
 }
 
-int mtd_check_open(const char *mtd)
+int mtd_check_open(const char *mtd, bool write_mode)
 {
        struct mtd_info_user mtdInfo;
        int fd;
 
-       fd = mtd_open(mtd, false);
+       fd = mtd_open(mtd, false, write_mode);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                return -1;
@@ -253,7 +257,7 @@ static int mtd_check(const char *mtd)
                        next++;
                }
 
-               fd = mtd_check_open(mtd);
+               fd = mtd_check_open(mtd, true);
                if (fd < 0)
                        return 0;
 
@@ -290,7 +294,7 @@ mtd_unlock(const char *mtd)
                        next++;
                }
 
-               fd = mtd_check_open(mtd);
+               fd = mtd_check_open(mtd, true);
                if(fd < 0) {
                        fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                        exit(1);
@@ -321,7 +325,7 @@ mtd_erase(const char *mtd)
        if (quiet < 2)
                fprintf(stderr, "Erasing %s ...\n", mtd);
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                exit(1);
@@ -357,7 +361,7 @@ mtd_dump(const char *mtd, int part_offset, int size)
        if (quiet < 2)
                fprintf(stderr, "Dumping %s ...\n", mtd);
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, false);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                return -1;
@@ -416,7 +420,7 @@ mtd_verify(const char *mtd, char *file)
                return -1;
        }
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, false);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                return -1;
@@ -551,7 +555,7 @@ resume:
                next++;
        }
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                exit(1);
index 5d56113b1c63435fe225f3ed27dad423beb0b68e..d34e94fdec4d1cbf4401c672c24e2fd568383219 100644 (file)
@@ -15,8 +15,8 @@ extern int mtdsize;
 extern int erasesize;
 extern uint32_t opt_trxmagic;
 
-extern int mtd_open(const char *mtd, bool block);
-extern int mtd_check_open(const char *mtd);
+extern int mtd_open(const char *mtd, bool block, bool write_mode);
+extern int mtd_check_open(const char *mtd, bool write_mode);
 extern int mtd_block_is_bad(int fd, int offset);
 extern int mtd_erase_block(int fd, int offset);
 extern int mtd_write_buffer(int fd, const char *buf, int offset, int length);
index 1f66adc439b7dadf36b050287c8af7d3e8292986..824beb09a74e34ee87fce7327e5917c76abdc338 100644 (file)
@@ -121,7 +121,7 @@ mtd_fixseama(const char *mtd, size_t offset, size_t data_size)
        block_offset = offset & ~(erasesize - 1);
        offset -= block_offset;
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                exit(1);
index 3711e013177b834e531a8c7bda6197a34acf840a..15c90d8cf45b4fb1c9fbef88303cdcb9856fee4e 100644 (file)
@@ -49,7 +49,7 @@ int mtd_tpl_recoverflag_write(const char *mtd, const bool recovery_active)
                return -1;
        }
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if (fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                ret = -1;
index 494cc3c91e2b741eea6669c78b826dd68d400acb..f4657dc3711798f98f570795eb3699f4cc5bea2c 100644 (file)
@@ -83,7 +83,7 @@ trx_fixup(int fd, const char *name)
                goto err;
        }
 
-       bfd = mtd_open(name, true);
+       bfd = mtd_open(name, true, true);
        ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
        if (!ptr || (ptr == (void *) -1)) {
                perror("mmap");
@@ -138,7 +138,7 @@ trx_check(int imagefd, const char *mtd, char *buf, int *len)
        }
 
        /* check if image fits to mtd device */
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                exit(1);
@@ -168,7 +168,7 @@ mtd_fixtrx(const char *mtd, size_t offset, size_t data_size)
        if (quiet < 2)
                fprintf(stderr, "Trying to fix trx header in %s at 0x%zx...\n", mtd, offset);
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                exit(1);
index 879cf1bbe0a323dd68e32702c01633589939bc1d..505b483d1ca1d92a1cd7e7cdc77090659a3fb6a2 100644 (file)
@@ -141,7 +141,7 @@ mtd_fixwrg(const char *mtd, size_t offset, size_t data_size)
        block_offset = offset & ~(erasesize - 1);
        offset -= block_offset;
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                exit(1);
index c62f9f5507363508954d4ad6a84e256f2c2d4cf4..03af15e03c79d279e2c983a313f3d76efbbc9a61 100644 (file)
@@ -119,7 +119,7 @@ mtd_fixwrgg(const char *mtd, size_t offset, size_t data_size)
        block_offset = offset & ~(erasesize - 1);
        offset -= block_offset;
 
-       fd = mtd_check_open(mtd);
+       fd = mtd_check_open(mtd, true);
        if(fd < 0) {
                fprintf(stderr, "Could not open mtd device: %s\n", mtd);
                exit(1);