From 014046689ff3cb9fd4f893530cf40d4a89bf9da9 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Mon, 17 Aug 2026 09:30:30 +0200 Subject: [PATCH] F-7983: propagate OctoSPI status-read failures in octospi_wait_ready() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit octospi_wait_ready() pre-zeros sr and ignored the return value of octospi_cmd(). A failed status-register transfer therefore left sr == 0 and the do/while loop exited as if the flash were no longer BUSY, so a page program or sector erase that could not be confirmed completed was reported as successful by nor_flash_write()/nor_flash_erase() and, with it, by hal_flash_write/erase and ext_flash_write/erase. Make octospi_wait_ready() return -1 when the status read transfer fails, and abort both NOR mutation loops with -1 in that case, matching the existing error handling for the program/erase command transfers themselves. Note: the wait loop remains unbounded if the flash genuinely stays BUSY (stuck operation) — a separate concern from the false-success path fixed here; a bounded timeout would need a timing reference this HAL does not have and no sibling HAL currently uses one. Verified: make TARGET=stm32n6 (full build, wolfboot.bin + signed test-app image), tools/unit-tests suite green. Hardware fault injection required to reproduce, so no host unit test. --- hal/stm32n6.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/hal/stm32n6.c b/hal/stm32n6.c index 5f2f0efa..6c0a6433 100644 --- a/hal/stm32n6.c +++ b/hal/stm32n6.c @@ -144,14 +144,18 @@ static void RAMFUNCTION octospi_write_enable(void) NULL, 0, SPI_MODE_NONE, 0); } -static void RAMFUNCTION octospi_wait_ready(void) +static int RAMFUNCTION octospi_wait_ready(void) { uint8_t sr; do { sr = 0; - octospi_cmd(1, READ_SR_CMD, 0, SPI_MODE_NONE, - &sr, 1, SPI_MODE_SINGLE, 0); + /* A failed status-register transfer must not read as "ready": + * sr stays zero and the loop would exit as if the flash were idle. */ + if (octospi_cmd(1, READ_SR_CMD, 0, SPI_MODE_NONE, + &sr, 1, SPI_MODE_SINGLE, 0) < 0) + return -1; } while (sr & FLASH_SR_BUSY); + return 0; } static void RAMFUNCTION octospi_enable_mmap(void) @@ -737,7 +741,10 @@ static int RAMFUNCTION nor_flash_write(uint32_t offset, const uint8_t *data, if (ret < 0) break; - octospi_wait_ready(); + if (octospi_wait_ready() < 0) { + ret = -1; + break; + } offset += write_sz; data += write_sz; @@ -766,7 +773,10 @@ static int RAMFUNCTION nor_flash_erase(uint32_t offset, int len) if (ret < 0) break; - octospi_wait_ready(); + if (octospi_wait_ready() < 0) { + ret = -1; + break; + } offset += FLASH_SECTOR_SIZE; }