From 693db5945b6ad0b2c0ff5bf46261f5d21a6b153d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Owoc?= Date: Sun, 9 Nov 2025 11:21:37 +0100 Subject: [PATCH] mtd: allow to use dump and verify on read-only devices MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Dump and verify commands can be used on read-only devices. Signed-off-by: Paweł Owoc Link: https://github.com/openwrt/openwrt/pull/20725 Signed-off-by: Hauke Mehrtens --- package/system/mtd/Makefile | 2 +- package/system/mtd/src/fis.c | 4 ++-- package/system/mtd/src/imagetag.c | 6 ++--- package/system/mtd/src/jffs2.c | 2 +- package/system/mtd/src/linksys_bootcount.c | 2 +- package/system/mtd/src/mtd.c | 24 +++++++++++-------- package/system/mtd/src/mtd.h | 4 ++-- package/system/mtd/src/seama.c | 2 +- .../system/mtd/src/tpl_ramips_recoveryflag.c | 2 +- package/system/mtd/src/trx.c | 6 ++--- package/system/mtd/src/wrg.c | 2 +- package/system/mtd/src/wrgg.c | 2 +- 12 files changed, 31 insertions(+), 27 deletions(-) diff --git a/package/system/mtd/Makefile b/package/system/mtd/Makefile index d45f06fc43..d83e3599d8 100644 --- a/package/system/mtd/Makefile +++ b/package/system/mtd/Makefile @@ -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) diff --git a/package/system/mtd/src/fis.c b/package/system/mtd/src/fis.c index 8bde9af6dc..8f719901b8 100644 --- a/package/system/mtd/src/fis.c +++ b/package/system/mtd/src/fis.c @@ -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; diff --git a/package/system/mtd/src/imagetag.c b/package/system/mtd/src/imagetag.c index 47642478f5..84aadf9cd6 100644 --- a/package/system/mtd/src/imagetag.c +++ b/package/system/mtd/src/imagetag.c @@ -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); diff --git a/package/system/mtd/src/jffs2.c b/package/system/mtd/src/jffs2.c index 2cfc878b10..cdf9cc1ccd 100644 --- a/package/system/mtd/src/jffs2.c +++ b/package/system/mtd/src/jffs2.c @@ -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; diff --git a/package/system/mtd/src/linksys_bootcount.c b/package/system/mtd/src/linksys_bootcount.c index 3ec0b61718..9de617d086 100644 --- a/package/system/mtd/src/linksys_bootcount.c +++ b/package/system/mtd/src/linksys_bootcount.c @@ -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."); diff --git a/package/system/mtd/src/mtd.c b/package/system/mtd/src/mtd.c index fc7071d940..be6de2f63e 100644 --- a/package/system/mtd/src/mtd.c +++ b/package/system/mtd/src/mtd.c @@ -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); diff --git a/package/system/mtd/src/mtd.h b/package/system/mtd/src/mtd.h index 5d56113b1c..d34e94fdec 100644 --- a/package/system/mtd/src/mtd.h +++ b/package/system/mtd/src/mtd.h @@ -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); diff --git a/package/system/mtd/src/seama.c b/package/system/mtd/src/seama.c index 1f66adc439..824beb09a7 100644 --- a/package/system/mtd/src/seama.c +++ b/package/system/mtd/src/seama.c @@ -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); diff --git a/package/system/mtd/src/tpl_ramips_recoveryflag.c b/package/system/mtd/src/tpl_ramips_recoveryflag.c index 3711e01317..15c90d8cf4 100644 --- a/package/system/mtd/src/tpl_ramips_recoveryflag.c +++ b/package/system/mtd/src/tpl_ramips_recoveryflag.c @@ -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; diff --git a/package/system/mtd/src/trx.c b/package/system/mtd/src/trx.c index 494cc3c91e..f4657dc371 100644 --- a/package/system/mtd/src/trx.c +++ b/package/system/mtd/src/trx.c @@ -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); diff --git a/package/system/mtd/src/wrg.c b/package/system/mtd/src/wrg.c index 879cf1bbe0..505b483d1c 100644 --- a/package/system/mtd/src/wrg.c +++ b/package/system/mtd/src/wrg.c @@ -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); diff --git a/package/system/mtd/src/wrgg.c b/package/system/mtd/src/wrgg.c index c62f9f5507..03af15e03c 100644 --- a/package/system/mtd/src/wrgg.c +++ b/package/system/mtd/src/wrgg.c @@ -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); -- 2.30.2