Skip to content

Commit aedc51a

Browse files
pdgendtcfriedt
authored andcommitted
drivers: flash: mcux flexspi nor: Fix write on arbitrary offset
If a write offset isn't a multiple of the nor page size, and the length is too large to fit within a single page, it could wrap around in that page. Tested on i.MX RT1064 internal flash using NVS settings Signed-off-by: Pieter De Gendt <[email protected]>
1 parent dea03d4 commit aedc51a

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

drivers/flash/flash_mcux_flexspi_mx25um51345g.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,11 @@ static int flash_flexspi_nor_write(const struct device *dev, off_t offset,
335335
}
336336

337337
while (len) {
338-
i = MIN(SPI_NOR_PAGE_SIZE, len);
338+
/* If the offset isn't a multiple of the NOR page size, we first need
339+
* to write the remaining part that fits, otherwise the write could
340+
* be wrapped around within the same page
341+
*/
342+
i = MIN(SPI_NOR_PAGE_SIZE - (offset % SPI_NOR_PAGE_SIZE), len);
339343
#ifdef CONFIG_FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
340344
memcpy(nor_write_buf, src, i);
341345
#endif

drivers/flash/flash_mcux_flexspi_nor.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,11 @@ static int flash_flexspi_nor_write(const struct device *dev, off_t offset,
349349
}
350350

351351
while (len) {
352-
i = MIN(SPI_NOR_PAGE_SIZE, len);
352+
/* If the offset isn't a multiple of the NOR page size, we first need
353+
* to write the remaining part that fits, otherwise the write could
354+
* be wrapped around within the same page
355+
*/
356+
i = MIN(SPI_NOR_PAGE_SIZE - (offset % SPI_NOR_PAGE_SIZE), len);
353357
#ifdef CONFIG_FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
354358
memcpy(nor_write_buf, src, i);
355359
#endif

0 commit comments

Comments
 (0)