mirror of
https://github.com/Ansuel/openwrt.git
synced 2025-12-16 15:01:32 +00:00
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:
parent
7ea659503a
commit
693db5945b
@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
|
|||||||
include $(INCLUDE_DIR)/kernel.mk
|
include $(INCLUDE_DIR)/kernel.mk
|
||||||
|
|
||||||
PKG_NAME:=mtd
|
PKG_NAME:=mtd
|
||||||
PKG_RELEASE:=26
|
PKG_RELEASE:=27
|
||||||
|
|
||||||
PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
|
PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
|
||||||
STAMP_PREPARED := $(STAMP_PREPARED)_$(call confvar,CONFIG_MTD_REDBOOT_PARTS)
|
STAMP_PREPARED := $(STAMP_PREPARED)_$(call confvar,CONFIG_MTD_REDBOOT_PARTS)
|
||||||
|
|||||||
@ -67,12 +67,12 @@ fis_open(void)
|
|||||||
if (fis_fd >= 0)
|
if (fis_fd >= 0)
|
||||||
fis_close();
|
fis_close();
|
||||||
|
|
||||||
fis_fd = mtd_check_open("FIS directory");
|
fis_fd = mtd_check_open("FIS directory", true);
|
||||||
if (fis_fd < 0)
|
if (fis_fd < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
close(fis_fd);
|
close(fis_fd);
|
||||||
fis_fd = mtd_open("FIS directory", true);
|
fis_fd = mtd_open("FIS directory", true, true);
|
||||||
if (fis_fd < 0)
|
if (fis_fd < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
|
|||||||
@ -195,7 +195,7 @@ trx_fixup(int fd, const char *name)
|
|||||||
goto err;
|
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);
|
ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
|
||||||
if (!ptr || (ptr == (void *) -1)) {
|
if (!ptr || (ptr == (void *) -1)) {
|
||||||
perror("mmap");
|
perror("mmap");
|
||||||
@ -269,7 +269,7 @@ trx_check(int imagefd, const char *mtd, char *buf, int *len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* check if image fits to mtd device */
|
/* check if image fits to mtd device */
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -308,7 +308,7 @@ mtd_fixtrx(const char *mtd, size_t offset, size_t data_size)
|
|||||||
block_offset = offset & ~(erasesize - 1);
|
block_offset = offset & ~(erasesize - 1);
|
||||||
offset -= block_offset;
|
offset -= block_offset;
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
@ -287,7 +287,7 @@ int mtd_write_jffs2(const char *mtd, const char *filename, const char *dir)
|
|||||||
{
|
{
|
||||||
int err = -1, fdeof = 0;
|
int err = -1, fdeof = 0;
|
||||||
|
|
||||||
outfd = mtd_check_open(mtd);
|
outfd = mtd_check_open(mtd, true);
|
||||||
if (outfd < 0)
|
if (outfd < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
|||||||
@ -83,7 +83,7 @@ int mtd_resetbc(const char *mtd)
|
|||||||
|
|
||||||
DLOG_OPEN();
|
DLOG_OPEN();
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
|
|
||||||
if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) {
|
if (ioctl(fd, MEMGETINFO, &mtd_info) < 0) {
|
||||||
DLOG_ERR("Unable to obtain mtd_info for given partition name.");
|
DLOG_ERR("Unable to obtain mtd_info for given partition name.");
|
||||||
|
|||||||
@ -96,15 +96,19 @@ int jffs2_skip_bytes=0;
|
|||||||
int mtdtype = 0;
|
int mtdtype = 0;
|
||||||
uint32_t opt_trxmagic = TRX_MAGIC;
|
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;
|
FILE *fp;
|
||||||
char dev[PATH_MAX];
|
char dev[PATH_MAX];
|
||||||
int i;
|
int i;
|
||||||
int ret;
|
int ret;
|
||||||
int flags = O_RDWR | O_SYNC;
|
int flags = O_RDONLY;
|
||||||
char name[PATH_MAX];
|
char name[PATH_MAX];
|
||||||
|
|
||||||
|
if(write_mode) {
|
||||||
|
flags = O_RDWR | O_SYNC;
|
||||||
|
}
|
||||||
|
|
||||||
snprintf(name, sizeof(name), "\"%s\"", mtd);
|
snprintf(name, sizeof(name), "\"%s\"", mtd);
|
||||||
if ((fp = fopen("/proc/mtd", "r"))) {
|
if ((fp = fopen("/proc/mtd", "r"))) {
|
||||||
while (fgets(dev, sizeof(dev), fp)) {
|
while (fgets(dev, sizeof(dev), fp)) {
|
||||||
@ -124,12 +128,12 @@ int mtd_open(const char *mtd, bool block)
|
|||||||
return open(mtd, flags);
|
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;
|
struct mtd_info_user mtdInfo;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
fd = mtd_open(mtd, false);
|
fd = mtd_open(mtd, false, write_mode);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
return -1;
|
return -1;
|
||||||
@ -253,7 +257,7 @@ static int mtd_check(const char *mtd)
|
|||||||
next++;
|
next++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -290,7 +294,7 @@ mtd_unlock(const char *mtd)
|
|||||||
next++;
|
next++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -321,7 +325,7 @@ mtd_erase(const char *mtd)
|
|||||||
if (quiet < 2)
|
if (quiet < 2)
|
||||||
fprintf(stderr, "Erasing %s ...\n", mtd);
|
fprintf(stderr, "Erasing %s ...\n", mtd);
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -357,7 +361,7 @@ mtd_dump(const char *mtd, int part_offset, int size)
|
|||||||
if (quiet < 2)
|
if (quiet < 2)
|
||||||
fprintf(stderr, "Dumping %s ...\n", mtd);
|
fprintf(stderr, "Dumping %s ...\n", mtd);
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, false);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
return -1;
|
return -1;
|
||||||
@ -416,7 +420,7 @@ mtd_verify(const char *mtd, char *file)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, false);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
return -1;
|
return -1;
|
||||||
@ -551,7 +555,7 @@ resume:
|
|||||||
next++;
|
next++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
@ -15,8 +15,8 @@ extern int mtdsize;
|
|||||||
extern int erasesize;
|
extern int erasesize;
|
||||||
extern uint32_t opt_trxmagic;
|
extern uint32_t opt_trxmagic;
|
||||||
|
|
||||||
extern int mtd_open(const char *mtd, bool block);
|
extern int mtd_open(const char *mtd, bool block, bool write_mode);
|
||||||
extern int mtd_check_open(const char *mtd);
|
extern int mtd_check_open(const char *mtd, bool write_mode);
|
||||||
extern int mtd_block_is_bad(int fd, int offset);
|
extern int mtd_block_is_bad(int fd, int offset);
|
||||||
extern int mtd_erase_block(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);
|
extern int mtd_write_buffer(int fd, const char *buf, int offset, int length);
|
||||||
|
|||||||
@ -121,7 +121,7 @@ mtd_fixseama(const char *mtd, size_t offset, size_t data_size)
|
|||||||
block_offset = offset & ~(erasesize - 1);
|
block_offset = offset & ~(erasesize - 1);
|
||||||
offset -= block_offset;
|
offset -= block_offset;
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
@ -49,7 +49,7 @@ int mtd_tpl_recoverflag_write(const char *mtd, const bool recovery_active)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
ret = -1;
|
ret = -1;
|
||||||
|
|||||||
@ -83,7 +83,7 @@ trx_fixup(int fd, const char *name)
|
|||||||
goto err;
|
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);
|
ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
|
||||||
if (!ptr || (ptr == (void *) -1)) {
|
if (!ptr || (ptr == (void *) -1)) {
|
||||||
perror("mmap");
|
perror("mmap");
|
||||||
@ -138,7 +138,7 @@ trx_check(int imagefd, const char *mtd, char *buf, int *len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* check if image fits to mtd device */
|
/* check if image fits to mtd device */
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -168,7 +168,7 @@ mtd_fixtrx(const char *mtd, size_t offset, size_t data_size)
|
|||||||
if (quiet < 2)
|
if (quiet < 2)
|
||||||
fprintf(stderr, "Trying to fix trx header in %s at 0x%zx...\n", mtd, offset);
|
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) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
@ -141,7 +141,7 @@ mtd_fixwrg(const char *mtd, size_t offset, size_t data_size)
|
|||||||
block_offset = offset & ~(erasesize - 1);
|
block_offset = offset & ~(erasesize - 1);
|
||||||
offset -= block_offset;
|
offset -= block_offset;
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
@ -119,7 +119,7 @@ mtd_fixwrgg(const char *mtd, size_t offset, size_t data_size)
|
|||||||
block_offset = offset & ~(erasesize - 1);
|
block_offset = offset & ~(erasesize - 1);
|
||||||
offset -= block_offset;
|
offset -= block_offset;
|
||||||
|
|
||||||
fd = mtd_check_open(mtd);
|
fd = mtd_check_open(mtd, true);
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user