summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/env/fw_env.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index c6378ecf34..3ab1ae69c7 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -995,6 +995,7 @@ static int flash_write_buf(int dev, int fd, void *buf, size_t count)
of the data */
loff_t blockstart; /* running start of the current block -
MEMGETBADBLOCK needs 64 bits */
+ int was_locked; /* flash lock flag */
int rc;
/*
@@ -1080,6 +1081,12 @@ static int flash_write_buf(int dev, int fd, void *buf, size_t count)
}
erase.length = erasesize;
+ if (DEVTYPE(dev) != MTD_ABSENT) {
+ was_locked = ioctl(fd, MEMISLOCKED, &erase);
+ /* treat any errors as unlocked flash */
+ if (was_locked < 0)
+ was_locked = 0;
+ }
/* This only runs once on NOR flash and SPI-dataflash */
while (processed < write_total) {
@@ -1099,7 +1106,8 @@ static int flash_write_buf(int dev, int fd, void *buf, size_t count)
if (DEVTYPE(dev) != MTD_ABSENT) {
erase.start = blockstart;
- ioctl(fd, MEMUNLOCK, &erase);
+ if (was_locked)
+ ioctl(fd, MEMUNLOCK, &erase);
/* These do not need an explicit erase cycle */
if (DEVTYPE(dev) != MTD_DATAFLASH)
if (ioctl(fd, MEMERASE, &erase) != 0) {
@@ -1127,8 +1135,10 @@ static int flash_write_buf(int dev, int fd, void *buf, size_t count)
return -1;
}
- if (DEVTYPE(dev) != MTD_ABSENT)
- ioctl(fd, MEMLOCK, &erase);
+ if (DEVTYPE(dev) != MTD_ABSENT) {
+ if (was_locked)
+ ioctl(fd, MEMLOCK, &erase);
+ }
processed += erasesize;
block_seek = 0;
@@ -1149,7 +1159,9 @@ static int flash_flag_obsolete(int dev, int fd, off_t offset)
int rc;
struct erase_info_user erase;
char tmp = ENV_REDUND_OBSOLETE;
+ int was_locked; /* flash lock flag */
+ was_locked = ioctl(fd, MEMISLOCKED, &erase);
erase.start = DEVOFFSET(dev);
erase.length = DEVESIZE(dev);
/* This relies on the fact, that ENV_REDUND_OBSOLETE == 0 */
@@ -1159,9 +1171,11 @@ static int flash_flag_obsolete(int dev, int fd, off_t offset)
DEVNAME(dev));
return rc;
}
- ioctl(fd, MEMUNLOCK, &erase);
+ if (was_locked)
+ ioctl(fd, MEMUNLOCK, &erase);
rc = write(fd, &tmp, sizeof(tmp));
- ioctl(fd, MEMLOCK, &erase);
+ if (was_locked)
+ ioctl(fd, MEMLOCK, &erase);
if (rc < 0)
perror("Could not set obsolete flag");