From 8489736eac7be570a3fa09793a368aaf232ef74e Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 1 Oct 2024 11:50:08 -0700 Subject: [PATCH] Fixed and improved erase of remainder of partition logic and logging. Added support for nRF5340 core synchronization (`NRF_SYNC_CORES`). Added test for `WOLFBOOT_FLASH_MULTI_SECTOR_ERASE`. --- .github/workflows/test-configs.yml | 8 +++++ config/examples/nrf5340.config | 5 ++- hal/nrf5340.c | 16 ++++++++-- src/update_flash.c | 49 +++++++++++++----------------- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/.github/workflows/test-configs.yml b/.github/workflows/test-configs.yml index 5cc2c511..b34ba299 100644 --- a/.github/workflows/test-configs.yml +++ b/.github/workflows/test-configs.yml @@ -208,6 +208,14 @@ jobs: config-file: ./config/examples/sim.config make-args: SPMATH=1 WOLFBOOT_SMALL_STACK=0 WOLFBOOT_HUGE_STACK=1 + sim_multi_sector_erase: + uses: ./.github/workflows/test-build.yml + with: + arch: host + config-file: ./config/examples/sim.config + make-args: CFLAGS_EXTRA=-DWOLFBOOT_FLASH_MULTI_SECTOR_ERASE + + # TODO: SP math with small stack has issues stm32c0: diff --git a/config/examples/nrf5340.config b/config/examples/nrf5340.config index 5a7ba646..1c2480e7 100644 --- a/config/examples/nrf5340.config +++ b/config/examples/nrf5340.config @@ -45,7 +45,10 @@ DEBUG?=0 DEBUG_UART?=1 USE_GCC=1 -# Use larger block size for swapping sectors +# Optionally wait for network core to boot before starting application core +CFLAGS_EXTRA+=-DNRF_SYNC_CORES + +# Use larger block size for swapping sectors (performance improvement) CFLAGS_EXTRA+=-DFLASHBUFFER_SIZE=0x1000 # Enable optional power control pin (active low) P1.00 diff --git a/hal/nrf5340.c b/hal/nrf5340.c index a6c8f2b9..cf85cf08 100644 --- a/hal/nrf5340.c +++ b/hal/nrf5340.c @@ -468,7 +468,7 @@ static void hal_net_check_version(void) wolfBoot_printf("Waiting for net core update to finish...\n"); - /* wait for update_done */ + /* wait for update_done - note longer wait */ ret = hal_shm_status_wait(&shm->net, SHARED_STATUS_UPDATE_DONE, 5000000); if (ret == 0) { @@ -543,17 +543,27 @@ void hal_prepare_boot(void) #ifdef TARGET_nrf5340_net if (do_update) { - /* signal application core of update */ - /* Reopen image and refresh information */ + /* signal application core update done */ struct wolfBoot_image img; + /* Reopen image and refresh information */ hal_net_get_image(&img, &shm->net); wolfBoot_printf("Network version (after update): 0x%x\n", shm->net.version); hal_shm_status_set(&shm->net, SHARED_STATUS_UPDATE_DONE); } + else { + hal_shm_status_set(&shm->net, SHARED_STATUS_DO_BOOT); + } #endif #ifdef TARGET_nrf5340_app +#ifdef NRF_SYNC_CORES + /* if core synchronization enabled, then wait for update_done or do_boot */ + wolfBoot_printf("Waiting for network core...\n"); + (void)hal_shm_status_wait(&shm->net, + (SHARED_STATUS_UPDATE_DONE | SHARED_STATUS_DO_BOOT), 1000000); +#endif + /* Restore defaults preventing network core from accessing shared SDRAM */ SPU_EXTDOMAIN_PERM(0) = (SPU_EXTDOMAIN_PERM_SECATTR_NONSECURE | SPU_EXTDOMAIN_PERM_UNLOCK); diff --git a/src/update_flash.c b/src/update_flash.c index 07269844..d63f573c 100644 --- a/src/update_flash.c +++ b/src/update_flash.c @@ -265,7 +265,6 @@ static int wolfBoot_swap_and_final_erase(int resume) wb_flash_write(boot, tmpBootPos, (void*)tmpBuffer, sizeof(tmpBuffer)); } /* erase the last boot sector(s) */ - wolfBoot_printf("Erasing unused boot sectors...\n"); wb_flash_erase(boot, WOLFBOOT_PARTITION_SIZE - eraseLen, eraseLen); /* set the encryption key */ #ifdef EXT_ENCRYPTED @@ -282,7 +281,6 @@ static int wolfBoot_swap_and_final_erase(int resume) /* mark boot as TESTING */ wolfBoot_set_partition_state(PART_BOOT, IMG_STATE_TESTING); /* erase the last sector(s) of update */ - wolfBoot_printf("Erasing unused update sectors...\n"); wb_flash_erase(update, WOLFBOOT_PARTITION_SIZE - eraseLen, eraseLen); return 0; } @@ -509,9 +507,6 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed) uint32_t up_v; #endif uint32_t cur_ver, upd_ver; -#ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE - size_t remainderBytes; -#endif wolfBoot_printf("Staring Update (fallback allowed %d)\n", fallback_allowed); @@ -668,37 +663,30 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed) } } -#ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE -/* Performant option: Erase remainder of flash sectors in one HAL command */ - #ifdef NVM_FLASH_WRITEONCE - /* erase up until the start of the second-to-last sector for writeonce */ - remainderBytes = - WOLFBOOT_PARTITION_SIZE - (sector * sector_size) - (2 * sector_size); + /* erase up until the start of the second-to-last sector for writeonce */ + size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size) - (2 * sector_size); #else - /* erase up until the start of the last sector */ - remainderBytes = - WOLFBOOT_PARTITION_SIZE - (sector * sector_size) - sector_size; + /* erase up until the start of the last sector */ + size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size) - sector_size; #endif - wb_flash_erase(&boot, sector * sector_size, remainderBytes); - wb_flash_erase(&update, sector * sector_size, remainderBytes); -#else /* WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */ -/* Smaller code size option: Iterate over every remaining sector and erase it - * individually. Required on some targets (stm32f4) to pass code size check */ + wolfBoot_printf("Erasing remainder of partition (%d sectors)...\n", + size/sector_size); - /* erase to the last sector, writeonce has 2 sectors */ - while((sector * sector_size) < WOLFBOOT_PARTITION_SIZE - - sector_size - #ifdef NVM_FLASH_WRITEONCE - * 2 - #endif - ) { +#ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE + /* Performant option: Erase remainder of flash sectors in one HAL command */ + wb_flash_erase(&boot, sector * sector_size, size); + wb_flash_erase(&update, sector * sector_size, size); +#else + /* Smaller code size option: Iterate over every remaining sector and erase + * individually. Required on some targets (like stm32f4) due to code size */ + while (size >= sector_size) { wb_flash_erase(&boot, sector * sector_size, sector_size); wb_flash_erase(&update, sector * sector_size, sector_size); sector++; + size -= sector_size; } - #endif /* !WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */ /* start re-entrant final erase, return code is only for resumption in @@ -728,9 +716,14 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed) wolfBoot_copy_sector(&update, &boot, sector); sector++; } - while ((sector * sector_size) < WOLFBOOT_PARTITION_SIZE) { + /* erase remainder of partition */ + size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size); + wolfBoot_printf("Erasing remainder of partition (%d sectors)...\n", + size/sector_size); + while (size >= sector_size) { wb_flash_erase(&boot, sector * sector_size, sector_size); sector++; + size -= sector_size; } wolfBoot_set_partition_state(PART_BOOT, IMG_STATE_SUCCESS);