drivers: flash: spi_nor: improve diagnostics/behavior of erase

Erase can only succeed if the address is sector-aligned, and the span
to erase is an integer multiple of the sector size.  Validate this
before starting the process of erasing things.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2020-05-28 10:04:13 -05:00 committed by Maureen Helm
commit f6cfdf79ba

View file

@ -385,11 +385,21 @@ static int spi_nor_erase(struct device *dev, off_t addr, size_t size)
const struct spi_nor_config *params = dev->config_info; const struct spi_nor_config *params = dev->config_info;
int ret = 0; int ret = 0;
/* should be between 0 and flash size */ /* erase area must be subregion of device */
if ((addr < 0) || ((size + addr) > params->size)) { if ((addr < 0) || ((size + addr) > params->size)) {
return -ENODEV; return -ENODEV;
} }
/* address must be sector-aligned */
if (!SPI_NOR_IS_SECTOR_ALIGNED(addr)) {
return -EINVAL;
}
/* size must be a multiple of sectors */
if ((size % SPI_NOR_SECTOR_SIZE) != 0) {
return -EINVAL;
}
acquire_device(dev); acquire_device(dev);
while (size) { while (size) {