stm32/mboot: Improve mass erase to erase all non-protected pages.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2023-11-29 17:20:03 +11:00
parent b7b99522e4
commit 7002a19be2
1 changed files with 17 additions and 8 deletions

View File

@ -449,14 +449,6 @@ static bool flash_is_modifiable_addr_range(uint32_t addr, uint32_t len) {
|| addr >= (uint32_t)&_mboot_protected_flash_end_exclusive; || addr >= (uint32_t)&_mboot_protected_flash_end_exclusive;
} }
static int mboot_flash_mass_erase(void) {
// Erase all flash pages after mboot.
uint32_t start_addr = (uint32_t)&_mboot_protected_flash_end_exclusive;
uint32_t num_words = (FLASH_END + 1 - start_addr) / sizeof(uint32_t);
int ret = flash_erase(start_addr, num_words);
return ret;
}
static int mboot_flash_page_erase(uint32_t addr, uint32_t *next_addr) { static int mboot_flash_page_erase(uint32_t addr, uint32_t *next_addr) {
// Compute start and end address of the sector being erased. // Compute start and end address of the sector being erased.
uint32_t sector_size = 0; uint32_t sector_size = 0;
@ -488,6 +480,23 @@ static int mboot_flash_page_erase(uint32_t addr, uint32_t *next_addr) {
return 0; return 0;
} }
static int mboot_flash_mass_erase(void) {
// Erase all flash pages except those disallowed because they overlap with mboot.
uint32_t addr = FLASH_START;
while (addr <= FLASH_END) {
int ret = mboot_flash_page_erase(addr, &addr);
if (ret != 0 && ret != -MBOOT_ERRNO_FLASH_ERASE_DISALLOWED) {
return ret;
}
}
// Reset any errors from disallowed page erases.
dfu_context.status = DFU_STATUS_OK;
dfu_context.error = 0;
return 0;
}
static int mboot_flash_write(uint32_t addr, const uint8_t *src8, size_t len) { static int mboot_flash_write(uint32_t addr, const uint8_t *src8, size_t len) {
bool valid = flash_is_valid_addr(addr); bool valid = flash_is_valid_addr(addr);
if (!valid || !flash_is_modifiable_addr_range(addr, len)) { if (!valid || !flash_is_modifiable_addr_range(addr, len)) {