Skip to content

Commit

Permalink
fw_setenv: fix bug when SPI flash write size != sector size
Browse files Browse the repository at this point in the history
Some SPI flash have a write size that is different (smaller) than the
sector size. For example, the sst25vf016b has an erase size of 4096,
but a write size of 1 byte. For this flash device, the write()
function will be called with a size equal to the sector size of 4096,
and the return value will incorrectly be set to 4096, indicating that
4096 bytes have been written to the device. However, after inspecting
the flash content, only the first byte is written to the device, and
the remaining 4095 bytes are all 0xFF.

Fix this incorrect behavior by adapting mtdwrite() to use the write
size obtained from the MEMGETINFO ioctl when writing data.

Signed-off-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>
Reviewed-by: Stefano Babic <sbabic@denx.de>
  • Loading branch information
hvilleneuve29 authored and sbabic committed Aug 5, 2022
1 parent 9ddccfc commit 44ecc1c
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/uboot_env.c
Expand Up @@ -712,6 +712,8 @@ static int mtdwrite(struct uboot_flash_env *dev, void *data)
sectors = dev->envsectors ? dev->envsectors : 1;
buf = data;
while (count > 0) {
int blockcount;

erase.start = start;

skip = is_nand_badblock(dev, start);
Expand Down Expand Up @@ -742,17 +744,26 @@ static int mtdwrite(struct uboot_flash_env *dev, void *data)
ret =-EIO;
goto devwrite_out;
}
if (lseek(dev->fd, start, SEEK_SET) < 0) {
ret =-EIO;
goto devwrite_out;
}
if (write(dev->fd, buf, blocksize) != blocksize) {
ret =-EIO;
goto devwrite_out;

blockcount = blocksize;

/* writesize can be different than the sector size. */

while (blockcount > 0) {
if (lseek(dev->fd, start, SEEK_SET) < 0) {
ret =-EIO;
goto devwrite_out;
}
if (write(dev->fd, buf, dev->mtdinfo.writesize) != dev->mtdinfo.writesize) {
ret =-EIO;
goto devwrite_out;
}

blockcount -= dev->mtdinfo.writesize;
start += dev->mtdinfo.writesize;
buf += dev->mtdinfo.writesize;
}
MTDLOCK(dev, &erase);
start += dev->sectorsize;
buf += blocksize;
count -= blocksize;
ret += blocksize;
}
Expand Down

0 comments on commit 44ecc1c

Please sign in to comment.