tools/env: lookup dev_type directly from flash_read_buf/flash_write_buf

flash_write_buf already looks up size/offset/#sector from struct
envdev_s. It can look up mtd_type as well. Same applies to
flash_read_buf. Makes the interface simpler

Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
This commit is contained in:
Andreas Fenkart 2016-08-29 23:16:59 +02:00 committed by Tom Rini
parent c6012bbce6
commit ff95e579cf

28
tools/env/fw_env.c vendored
View File

@ -689,7 +689,7 @@ static int flash_bad_block(int fd, uint8_t mtd_type, loff_t blockstart)
* the DEVOFFSET (dev) block. On NOR the loop is only run once. * the DEVOFFSET (dev) block. On NOR the loop is only run once.
*/ */
static int flash_read_buf (int dev, int fd, void *buf, size_t count, static int flash_read_buf (int dev, int fd, void *buf, size_t count,
off_t offset, uint8_t mtd_type) off_t offset)
{ {
size_t blocklen; /* erase / write length - one block on NAND, size_t blocklen; /* erase / write length - one block on NAND,
0 on NOR */ 0 on NOR */
@ -706,7 +706,7 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
/* Offset inside a block */ /* Offset inside a block */
block_seek = offset - blockstart; block_seek = offset - blockstart;
if (mtd_type == MTD_NANDFLASH) { if (DEVTYPE(dev) == MTD_NANDFLASH) {
/* /*
* NAND: calculate which blocks we are reading. We have * NAND: calculate which blocks we are reading. We have
* to read one block at a time to skip bad blocks. * to read one block at a time to skip bad blocks.
@ -722,7 +722,7 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
/* This only runs once on NOR flash */ /* This only runs once on NOR flash */
while (processed < count) { while (processed < count) {
rc = flash_bad_block(fd, mtd_type, blockstart); rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
if (rc < 0) /* block test failed */ if (rc < 0) /* block test failed */
return -1; return -1;
@ -770,7 +770,7 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
* erase and write the whole data at once. * erase and write the whole data at once.
*/ */
static int flash_write_buf (int dev, int fd, void *buf, size_t count, static int flash_write_buf (int dev, int fd, void *buf, size_t count,
off_t offset, uint8_t mtd_type) off_t offset)
{ {
void *data; void *data;
struct erase_info_user erase; struct erase_info_user erase;
@ -793,7 +793,7 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
/* /*
* For mtd devices only offset and size of the environment do matter * For mtd devices only offset and size of the environment do matter
*/ */
if (mtd_type == MTD_ABSENT) { if (DEVTYPE(dev) == MTD_ABSENT) {
blocklen = count; blocklen = count;
erase_len = blocklen; erase_len = blocklen;
blockstart = offset; blockstart = offset;
@ -834,8 +834,7 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
return -1; return -1;
} }
rc = flash_read_buf (dev, fd, data, write_total, erase_offset, rc = flash_read_buf(dev, fd, data, write_total, erase_offset);
mtd_type);
if (write_total != rc) if (write_total != rc)
return -1; return -1;
@ -862,7 +861,7 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
data = buf; data = buf;
} }
if (mtd_type == MTD_NANDFLASH) { if (DEVTYPE(dev) == MTD_NANDFLASH) {
/* /*
* NAND: calculate which blocks we are writing. We have * NAND: calculate which blocks we are writing. We have
* to write one block at a time to skip bad blocks. * to write one block at a time to skip bad blocks.
@ -876,7 +875,7 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
/* This only runs once on NOR flash and SPI-dataflash */ /* This only runs once on NOR flash and SPI-dataflash */
while (processed < write_total) { while (processed < write_total) {
rc = flash_bad_block(fd, mtd_type, blockstart); rc = flash_bad_block(fd, DEVTYPE(dev), blockstart);
if (rc < 0) /* block test failed */ if (rc < 0) /* block test failed */
return rc; return rc;
@ -890,11 +889,11 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
continue; continue;
} }
if (mtd_type != MTD_ABSENT) { if (DEVTYPE(dev) != MTD_ABSENT) {
erase.start = blockstart; erase.start = blockstart;
ioctl(fd, MEMUNLOCK, &erase); ioctl(fd, MEMUNLOCK, &erase);
/* These do not need an explicit erase cycle */ /* These do not need an explicit erase cycle */
if (mtd_type != MTD_DATAFLASH) if (DEVTYPE(dev) != MTD_DATAFLASH)
if (ioctl(fd, MEMERASE, &erase) != 0) { if (ioctl(fd, MEMERASE, &erase) != 0) {
fprintf(stderr, fprintf(stderr,
"MTD erase error on %s: %s\n", "MTD erase error on %s: %s\n",
@ -921,7 +920,7 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
return -1; return -1;
} }
if (mtd_type != MTD_ABSENT) if (DEVTYPE(dev) != MTD_ABSENT)
ioctl(fd, MEMLOCK, &erase); ioctl(fd, MEMLOCK, &erase);
processed += erasesize; processed += erasesize;
@ -1008,8 +1007,7 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
#endif #endif
rc = flash_write_buf(dev_target, fd_target, environment.image, rc = flash_write_buf(dev_target, fd_target, environment.image,
CUR_ENVSIZE, DEVOFFSET(dev_target), CUR_ENVSIZE, DEVOFFSET(dev_target));
DEVTYPE(dev_target));
if (rc < 0) if (rc < 0)
return rc; return rc;
@ -1033,7 +1031,7 @@ static int flash_read (int fd)
int rc; int rc;
rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE, rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
DEVOFFSET(dev_current), DEVTYPE(dev_current)); DEVOFFSET(dev_current));
if (rc != CUR_ENVSIZE) if (rc != CUR_ENVSIZE)
return -1; return -1;