提交 e387efbd 编写于 作者: O Oliver Metz 提交者: Tom Rini

fw_env: fix writing environment for mtd devices

Signed-off-by: NOliver Metz <oliver@freetz.org>
Tested-by: NLuka Perkov <luka@openwrt.org>
上级 23ef62d7
...@@ -727,27 +727,39 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count, ...@@ -727,27 +727,39 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
MEMGETBADBLOCK needs 64 bits */ MEMGETBADBLOCK needs 64 bits */
int rc; int rc;
blocklen = DEVESIZE (dev); /*
* For mtd devices only offset and size of the environment do matter
*/
if (mtd_type == MTD_ABSENT) {
blocklen = count;
top_of_range = offset + count;
erase_len = blocklen;
blockstart = offset;
block_seek = 0;
write_total = blocklen;
} else {
blocklen = DEVESIZE(dev);
top_of_range = ((DEVOFFSET(dev) / blocklen) + top_of_range = ((DEVOFFSET(dev) / blocklen) +
ENVSECTORS (dev)) * blocklen; ENVSECTORS(dev)) * blocklen;
erase_offset = (offset / blocklen) * blocklen; erase_offset = (offset / blocklen) * blocklen;
/* Maximum area we may use */ /* Maximum area we may use */
erase_len = top_of_range - erase_offset; erase_len = top_of_range - erase_offset;
blockstart = erase_offset; blockstart = erase_offset;
/* Offset inside a block */ /* Offset inside a block */
block_seek = offset - erase_offset; block_seek = offset - erase_offset;
/* /*
* Data size we actually have to write: from the start of the block * Data size we actually write: from the start of the block
* to the start of the data, then count bytes of data, and to the * to the start of the data, then count bytes of data, and
* end of the block * to the end of the block
*/ */
write_total = ((block_seek + count + blocklen - 1) / write_total = ((block_seek + count + blocklen - 1) /
blocklen) * blocklen; blocklen) * blocklen;
}
/* /*
* Support data anywhere within erase sectors: read out the complete * Support data anywhere within erase sectors: read out the complete
...@@ -818,17 +830,18 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count, ...@@ -818,17 +830,18 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
continue; continue;
} }
erase.start = blockstart; if (mtd_type != MTD_ABSENT) {
ioctl (fd, MEMUNLOCK, &erase); erase.start = blockstart;
/* These do not need an explicit erase cycle */ ioctl(fd, MEMUNLOCK, &erase);
if (mtd_type != MTD_ABSENT && /* These do not need an explicit erase cycle */
mtd_type != MTD_DATAFLASH) if (mtd_type != MTD_DATAFLASH)
if (ioctl (fd, MEMERASE, &erase) != 0) { if (ioctl(fd, MEMERASE, &erase) != 0) {
fprintf (stderr, "MTD erase error on %s: %s\n", fprintf(stderr,
DEVNAME (dev), "MTD erase error on %s: %s\n",
strerror (errno)); DEVNAME(dev), strerror(errno));
return -1; return -1;
} }
}
if (lseek (fd, blockstart, SEEK_SET) == -1) { if (lseek (fd, blockstart, SEEK_SET) == -1) {
fprintf (stderr, fprintf (stderr,
...@@ -847,7 +860,8 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count, ...@@ -847,7 +860,8 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
return -1; return -1;
} }
ioctl (fd, MEMLOCK, &erase); if (mtd_type != MTD_ABSENT)
ioctl(fd, MEMLOCK, &erase);
processed += blocklen; processed += blocklen;
block_seek = 0; block_seek = 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册