mirror of https://github.com/wolfSSL/wolfBoot.git
F-3966: fix hal_flash_erase loop bound in stm32wb.c skipping final partial page
end_address = address + len - 1 (inclusive) combined with p < end_address (strict less-than) skipped the last page when len % FLASH_PAGE_SIZE == 1. Use the exclusive form end_address = address + len, mirroring the stm32h5 fix. Add WOLFBOOT_UNIT_TEST_FLASH_ERASE guards and a new unit test that proves the regression before the fix and passes after.pull/795/head
parent
889788d2b5
commit
2f53d0d1d9
|
|
@ -20,7 +20,9 @@
|
|||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#ifndef WOLFBOOT_UNIT_TEST_FLASH_ERASE
|
||||
#include "image.h"
|
||||
#endif
|
||||
|
||||
#ifndef __WOLFBOOT
|
||||
#undef WOLFSSL_STM32_PKA
|
||||
|
|
@ -34,7 +36,9 @@ PKA_HandleTypeDef hpka = { };
|
|||
/* STM32 WB register configuration */
|
||||
|
||||
/* Assembly helpers */
|
||||
#ifndef WOLFBOOT_UNIT_TEST_FLASH_ERASE
|
||||
#define DMB() __asm__ volatile ("dmb")
|
||||
#endif
|
||||
|
||||
/*** RCC ***/
|
||||
#ifndef RCC_BASE
|
||||
|
|
@ -90,10 +94,12 @@ PKA_HandleTypeDef hpka = { };
|
|||
#ifndef FLASH_BASE
|
||||
#define FLASH_BASE (0x58004000)
|
||||
#endif
|
||||
#ifndef WOLFBOOT_UNIT_TEST_FLASH_ERASE
|
||||
#define FLASH_ACR (*(volatile uint32_t *)(FLASH_BASE + 0x00))
|
||||
#define FLASH_KEY (*(volatile uint32_t *)(FLASH_BASE + 0x08))
|
||||
#define FLASH_SR (*(volatile uint32_t *)(FLASH_BASE + 0x10))
|
||||
#define FLASH_CR (*(volatile uint32_t *)(FLASH_BASE + 0x14))
|
||||
#endif /* !WOLFBOOT_UNIT_TEST_FLASH_ERASE */
|
||||
|
||||
#define FLASHMEM_ADDRESS_SPACE (0x08000000)
|
||||
#define FLASH_PAGE_SIZE (0x1000) /* 4KB */
|
||||
|
|
@ -126,6 +132,7 @@ PKA_HandleTypeDef hpka = { };
|
|||
#define FLASH_KEY2 (0xCDEF89ABUL)
|
||||
|
||||
|
||||
#ifndef WOLFBOOT_UNIT_TEST_FLASH_ERASE
|
||||
static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
|
||||
{
|
||||
uint32_t reg = FLASH_ACR;
|
||||
|
|
@ -143,9 +150,11 @@ static void RAMFUNCTION flash_clear_errors(void)
|
|||
{
|
||||
FLASH_SR |= ( FLASH_SR_SIZERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR | FLASH_SR_PROGERR);
|
||||
}
|
||||
#endif /* !WOLFBOOT_UNIT_TEST_FLASH_ERASE */
|
||||
|
||||
|
||||
|
||||
#ifndef WOLFBOOT_UNIT_TEST_FLASH_ERASE
|
||||
void RAMFUNCTION hal_flash_unlock(void)
|
||||
{
|
||||
flash_wait_complete();
|
||||
|
|
@ -211,6 +220,7 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
|
|||
FLASH_CR &= ~FLASH_CR_PG;
|
||||
return 0;
|
||||
}
|
||||
#endif /* !WOLFBOOT_UNIT_TEST_FLASH_ERASE */
|
||||
|
||||
|
||||
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
|
||||
|
|
@ -220,7 +230,7 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
|
|||
if (len == 0)
|
||||
return -1;
|
||||
address -= FLASHMEM_ADDRESS_SPACE;
|
||||
end_address = address + len - 1;
|
||||
end_address = address + len;
|
||||
flash_wait_complete();
|
||||
for (p = address; p < end_address; p += FLASH_PAGE_SIZE) {
|
||||
uint32_t reg;
|
||||
|
|
@ -236,6 +246,7 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#ifndef WOLFBOOT_UNIT_TEST_FLASH_ERASE
|
||||
static void clock_pll_off(void)
|
||||
{
|
||||
uint32_t reg32;
|
||||
|
|
@ -358,4 +369,5 @@ uint32_t HAL_GetTick(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* WOLFSSL_STM32_PKA */
|
||||
#endif /* !WOLFBOOT_UNIT_TEST_FLASH_ERASE */
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ TESTS+=unit-fit-gzip unit-fit-nogzip
|
|||
TESTS+=unit-fit-fpga
|
||||
TESTS+=unit-mpusize
|
||||
TESTS+=unit-flash-erase-h7
|
||||
TESTS+=unit-flash-erase-wb
|
||||
TESTS+=unit-otp-keystore
|
||||
TESTS+=unit-x86-paging-oob
|
||||
TESTS+=unit-fwtpm-nv-oob
|
||||
|
|
@ -285,6 +286,11 @@ unit-mpusize: ../../include/target.h unit-mpusize.c
|
|||
unit-flash-erase-h7: unit-flash-erase-h7.c ../../hal/stm32h7.c
|
||||
gcc -o $@ unit-flash-erase-h7.c $(CFLAGS) $(LDFLAGS)
|
||||
|
||||
# unit-flash-erase-wb includes hal/stm32wb.c directly (guarded to hal_flash_erase
|
||||
# via WOLFBOOT_UNIT_TEST_FLASH_ERASE), so stm32wb.c is not a separate input.
|
||||
unit-flash-erase-wb: unit-flash-erase-wb.c ../../hal/stm32wb.c
|
||||
gcc -o $@ unit-flash-erase-wb.c $(CFLAGS) $(LDFLAGS)
|
||||
|
||||
# unit-otp-keystore includes src/flash_otp_keystore.c directly (guarded to its
|
||||
# host-portable code via WOLFBOOT_UNIT_TEST_OTP_KEYSTORE), so it is not a
|
||||
# separate input.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,151 @@
|
|||
/* unit-flash-erase-wb.c
|
||||
*
|
||||
* Unit tests for the page-loop bound in hal_flash_erase() (hal/stm32wb.c).
|
||||
* Regression for F-3966: end_address = address + len - 1 (inclusive) combined
|
||||
* with p < end_address (strict) skips the final page when len % PAGE_SIZE == 1.
|
||||
*
|
||||
*
|
||||
* Copyright (C) 2026 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfBoot.
|
||||
*
|
||||
* wolfBoot is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfBoot is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
#include <check.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* hal/stm32wb.c is tightly coupled to STM32WB hardware registers.
|
||||
* Compile only hal_flash_erase() in isolation by defining this guard;
|
||||
* all other functions and hardware macros are excluded and replaced below. */
|
||||
#define WOLFBOOT_UNIT_TEST_FLASH_ERASE
|
||||
|
||||
/* RAMFUNCTION must be empty on the host */
|
||||
#define RAMFUNCTION
|
||||
|
||||
/* Mocked flash control registers */
|
||||
static uint32_t mock_FLASH_CR;
|
||||
static uint32_t mock_FLASH_SR;
|
||||
#define FLASH_CR mock_FLASH_CR
|
||||
#define FLASH_SR mock_FLASH_SR
|
||||
|
||||
/* Constants from hal/stm32wb.c (must mirror exactly).
|
||||
* FLASHMEM_ADDRESS_SPACE and FLASH_PAGE_SIZE are defined inside stm32wb.c
|
||||
* and therefore not repeated here. */
|
||||
#define FLASH_CR_STRT (1UL << 16)
|
||||
#define FLASH_CR_PER (1UL << 1)
|
||||
#define FLASH_CR_PG (1UL << 0)
|
||||
#define FLASH_CR_FSTPG (1UL << 18)
|
||||
#define FLASH_CR_PNB_SHIFT 3
|
||||
#define FLASH_CR_PNB_MASK 0xFFUL
|
||||
|
||||
/* Record the FLASH_CR value on each DMB() call that has STRT set — one entry
|
||||
* per page-erase command, exactly. */
|
||||
#define ERASE_LOG_MAX 64
|
||||
static uint32_t erase_cr[ERASE_LOG_MAX];
|
||||
static int erase_log_n;
|
||||
|
||||
#define DMB() do { \
|
||||
if ((mock_FLASH_CR & FLASH_CR_STRT) && erase_log_n < ERASE_LOG_MAX) { \
|
||||
erase_cr[erase_log_n] = mock_FLASH_CR; \
|
||||
erase_log_n++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* Stubs for functions called by hal_flash_erase().
|
||||
* Real hardware auto-clears STRT after the erase finishes; mirror that so
|
||||
* the bit does not leak into the next iteration's FLASH_CR setup. */
|
||||
static void flash_wait_complete(void) { mock_FLASH_CR &= ~FLASH_CR_STRT; }
|
||||
static void flash_clear_errors(void) {}
|
||||
|
||||
#include "../../hal/stm32wb.c"
|
||||
|
||||
/* Decode the page number field from a captured FLASH_CR value. */
|
||||
static uint32_t page_of(uint32_t cr)
|
||||
{
|
||||
return (cr >> FLASH_CR_PNB_SHIFT) & FLASH_CR_PNB_MASK;
|
||||
}
|
||||
|
||||
static void reset_mocks(void)
|
||||
{
|
||||
mock_FLASH_CR = 0;
|
||||
mock_FLASH_SR = 0;
|
||||
erase_log_n = 0;
|
||||
}
|
||||
|
||||
/* Erasing exactly one page must issue exactly one erase command. */
|
||||
START_TEST(test_erase_single_page_aligned)
|
||||
{
|
||||
reset_mocks();
|
||||
hal_flash_erase(0x08000000UL, FLASH_PAGE_SIZE);
|
||||
|
||||
ck_assert_int_eq(erase_log_n, 1);
|
||||
ck_assert_uint_eq(page_of(erase_cr[0]), 0);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/* Erasing two full pages must issue exactly two erase commands. */
|
||||
START_TEST(test_erase_two_pages_aligned)
|
||||
{
|
||||
reset_mocks();
|
||||
hal_flash_erase(0x08000000UL, 2 * FLASH_PAGE_SIZE);
|
||||
|
||||
ck_assert_int_eq(erase_log_n, 2);
|
||||
ck_assert_uint_eq(page_of(erase_cr[0]), 0);
|
||||
ck_assert_uint_eq(page_of(erase_cr[1]), 1);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/* Regression for F-3966: len = PAGE_SIZE + 1 spans two pages and both must be
|
||||
* erased. Before the fix, end_address = address + len - 1 landed exactly on
|
||||
* the start of page 1, so the strict `p < end_address` guard excluded it. */
|
||||
START_TEST(test_erase_unaligned_len_covers_last_page)
|
||||
{
|
||||
reset_mocks();
|
||||
/* len = 0x1001: bytes [0..0x1000], crossing into page 1 */
|
||||
hal_flash_erase(0x08000000UL, FLASH_PAGE_SIZE + 1);
|
||||
|
||||
ck_assert_int_eq(erase_log_n, 2);
|
||||
ck_assert_uint_eq(page_of(erase_cr[0]), 0);
|
||||
ck_assert_uint_eq(page_of(erase_cr[1]), 1);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
Suite *flash_erase_wb_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("flash-erase-wb");
|
||||
TCase *tc = tcase_create("flash-erase-wb");
|
||||
|
||||
tcase_add_test(tc, test_erase_single_page_aligned);
|
||||
tcase_add_test(tc, test_erase_two_pages_aligned);
|
||||
tcase_add_test(tc, test_erase_unaligned_len_covers_last_page);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int fails;
|
||||
Suite *s = flash_erase_wb_suite();
|
||||
SRunner *sr = srunner_create(s);
|
||||
|
||||
srunner_run_all(sr, CK_NORMAL);
|
||||
fails = srunner_ntests_failed(sr);
|
||||
srunner_free(sr);
|
||||
|
||||
return fails;
|
||||
}
|
||||
Loading…
Reference in New Issue