Commit 80e42f28 authored by Ofir Shemesh's avatar Ofir Shemesh Committed by Benjamin Cabé
Browse files

drivers: flash: flash_mcux_flexspi_nor: Add validation checks

Added area_is_subregion() to validate offset and size within bounds.
Ensured read, write, and erase operations check for valid memory regions.
Added null buffer check in read and write functions to prevent errors.

Signed-off-by: default avatarOfir Shemesh <ofirshemesh777@gmail.com>
parent 484a513a
Loading
Loading
Loading
Loading
+31 −0
Changes for drivers/flash/flash_mcux_flexspi_nor.c: 31 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -143,6 +143,14 @@ static const uint32_t flash_flexspi_nor_base_lut[][MEMC_FLEXSPI_CMD_PER_SEQ] = {
	},
};

static ALWAYS_INLINE bool area_is_subregion(const struct device *dev, off_t offset, size_t size)
{
	struct flash_flexspi_nor_data *data = dev->data;

	return ((offset >= 0) && (offset < data->size) &&
		((data->size - offset) >= size));
}

/* Helper so we can read flash ID without flash access for XIP */
static int flash_flexspi_nor_read_id_helper(struct flash_flexspi_nor_data *data,
		uint8_t *vendor_id)
@@ -316,6 +324,15 @@ static int flash_flexspi_nor_read(const struct device *dev, off_t offset,
		void *buffer, size_t len)
{
	struct flash_flexspi_nor_data *data = dev->data;

	if (!buffer) {
		return -EINVAL;
	}

	if (!area_is_subregion(dev, offset, len)) {
		return -EINVAL;
	}

	uint8_t *src = memc_flexspi_get_ahb_address(&data->controller,
						    data->port,
						    offset);
@@ -329,6 +346,15 @@ static int flash_flexspi_nor_write(const struct device *dev, off_t offset,
		const void *buffer, size_t len)
{
	struct flash_flexspi_nor_data *data = dev->data;

	if (!buffer) {
		return -EINVAL;
	}

	if (!area_is_subregion(dev, offset, len)) {
		return -EINVAL;
	}

	size_t size = len;
	uint8_t *src = (uint8_t *) buffer;
	int i;
@@ -393,6 +419,11 @@ static int flash_flexspi_nor_erase(const struct device *dev, off_t offset,
		size_t size)
{
	struct flash_flexspi_nor_data *data = dev->data;

	if (!area_is_subregion(dev, offset, size)) {
		return -EINVAL;
	}

	const size_t num_sectors = size / SPI_NOR_SECTOR_SIZE;
	const size_t num_blocks = size / SPI_NOR_BLOCK_SIZE;