From f6cfdf79ba50abcc010bbfdc92a9518108ead4ce Mon Sep 17 00:00:00 2001 From: Peter Bigot Date: Thu, 28 May 2020 10:04:13 -0500 Subject: [PATCH] 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 --- drivers/flash/spi_nor.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/flash/spi_nor.c b/drivers/flash/spi_nor.c index d5bbff4be2f..75e4e8f6490 100644 --- a/drivers/flash/spi_nor.c +++ b/drivers/flash/spi_nor.c @@ -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; int ret = 0; - /* should be between 0 and flash size */ + /* erase area must be subregion of device */ if ((addr < 0) || ((size + addr) > params->size)) { 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); while (size) {