mtd: allow to use dump and verify on read-only devices

Dump and verify commands can be used on read-only devices.

Signed-off-by: Paweł Owoc <frut3k7@gmail.com>
Link: https://github.com/openwrt/openwrt/pull/20725
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
This commit is contained in:
Paweł Owoc 2025-11-09 11:21:37 +01:00 committed by Hauke Mehrtens
parent 7ea659503a
commit 693db5945b
12 changed files with 31 additions and 27 deletions

View 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)

View 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;

View 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);

View 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;

View 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.");

View 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);

View 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);

View 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);

View 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;

View 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);

View 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);

View 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);