Skip to content

Commit 8489736

Browse files
dgarskedanielinux
authored andcommitted
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.
1 parent 832ac47 commit 8489736

4 files changed

Lines changed: 46 additions & 32 deletions

File tree

.github/workflows/test-configs.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ jobs:
208208
config-file: ./config/examples/sim.config
209209
make-args: SPMATH=1 WOLFBOOT_SMALL_STACK=0 WOLFBOOT_HUGE_STACK=1
210210

211+
sim_multi_sector_erase:
212+
uses: ./.github/workflows/test-build.yml
213+
with:
214+
arch: host
215+
config-file: ./config/examples/sim.config
216+
make-args: CFLAGS_EXTRA=-DWOLFBOOT_FLASH_MULTI_SECTOR_ERASE
217+
218+
211219
# TODO: SP math with small stack has issues
212220

213221
stm32c0:

config/examples/nrf5340.config

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ DEBUG?=0
4545
DEBUG_UART?=1
4646
USE_GCC=1
4747

48-
# Use larger block size for swapping sectors
48+
# Optionally wait for network core to boot before starting application core
49+
CFLAGS_EXTRA+=-DNRF_SYNC_CORES
50+
51+
# Use larger block size for swapping sectors (performance improvement)
4952
CFLAGS_EXTRA+=-DFLASHBUFFER_SIZE=0x1000
5053

5154
# Enable optional power control pin (active low) P1.00

hal/nrf5340.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ static void hal_net_check_version(void)
468468

469469
wolfBoot_printf("Waiting for net core update to finish...\n");
470470

471-
/* wait for update_done */
471+
/* wait for update_done - note longer wait */
472472
ret = hal_shm_status_wait(&shm->net,
473473
SHARED_STATUS_UPDATE_DONE, 5000000);
474474
if (ret == 0) {
@@ -543,17 +543,27 @@ void hal_prepare_boot(void)
543543

544544
#ifdef TARGET_nrf5340_net
545545
if (do_update) {
546-
/* signal application core of update */
547-
/* Reopen image and refresh information */
546+
/* signal application core update done */
548547
struct wolfBoot_image img;
548+
/* Reopen image and refresh information */
549549
hal_net_get_image(&img, &shm->net);
550550
wolfBoot_printf("Network version (after update): 0x%x\n",
551551
shm->net.version);
552552
hal_shm_status_set(&shm->net, SHARED_STATUS_UPDATE_DONE);
553553
}
554+
else {
555+
hal_shm_status_set(&shm->net, SHARED_STATUS_DO_BOOT);
556+
}
554557
#endif
555558

556559
#ifdef TARGET_nrf5340_app
560+
#ifdef NRF_SYNC_CORES
561+
/* if core synchronization enabled, then wait for update_done or do_boot */
562+
wolfBoot_printf("Waiting for network core...\n");
563+
(void)hal_shm_status_wait(&shm->net,
564+
(SHARED_STATUS_UPDATE_DONE | SHARED_STATUS_DO_BOOT), 1000000);
565+
#endif
566+
557567
/* Restore defaults preventing network core from accessing shared SDRAM */
558568
SPU_EXTDOMAIN_PERM(0) =
559569
(SPU_EXTDOMAIN_PERM_SECATTR_NONSECURE | SPU_EXTDOMAIN_PERM_UNLOCK);

src/update_flash.c

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ static int wolfBoot_swap_and_final_erase(int resume)
265265
wb_flash_write(boot, tmpBootPos, (void*)tmpBuffer, sizeof(tmpBuffer));
266266
}
267267
/* erase the last boot sector(s) */
268-
wolfBoot_printf("Erasing unused boot sectors...\n");
269268
wb_flash_erase(boot, WOLFBOOT_PARTITION_SIZE - eraseLen, eraseLen);
270269
/* set the encryption key */
271270
#ifdef EXT_ENCRYPTED
@@ -282,7 +281,6 @@ static int wolfBoot_swap_and_final_erase(int resume)
282281
/* mark boot as TESTING */
283282
wolfBoot_set_partition_state(PART_BOOT, IMG_STATE_TESTING);
284283
/* erase the last sector(s) of update */
285-
wolfBoot_printf("Erasing unused update sectors...\n");
286284
wb_flash_erase(update, WOLFBOOT_PARTITION_SIZE - eraseLen, eraseLen);
287285
return 0;
288286
}
@@ -509,9 +507,6 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
509507
uint32_t up_v;
510508
#endif
511509
uint32_t cur_ver, upd_ver;
512-
#ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE
513-
size_t remainderBytes;
514-
#endif
515510

516511
wolfBoot_printf("Staring Update (fallback allowed %d)\n", fallback_allowed);
517512

@@ -668,37 +663,30 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
668663
}
669664
}
670665

671-
#ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE
672-
/* Performant option: Erase remainder of flash sectors in one HAL command */
673-
674666
#ifdef NVM_FLASH_WRITEONCE
675-
/* erase up until the start of the second-to-last sector for writeonce */
676-
remainderBytes =
677-
WOLFBOOT_PARTITION_SIZE - (sector * sector_size) - (2 * sector_size);
667+
/* erase up until the start of the second-to-last sector for writeonce */
668+
size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size) - (2 * sector_size);
678669
#else
679-
/* erase up until the start of the last sector */
680-
remainderBytes =
681-
WOLFBOOT_PARTITION_SIZE - (sector * sector_size) - sector_size;
670+
/* erase up until the start of the last sector */
671+
size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size) - sector_size;
682672
#endif
683-
wb_flash_erase(&boot, sector * sector_size, remainderBytes);
684-
wb_flash_erase(&update, sector * sector_size, remainderBytes);
685673

686-
#else /* WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */
687-
/* Smaller code size option: Iterate over every remaining sector and erase it
688-
* individually. Required on some targets (stm32f4) to pass code size check */
674+
wolfBoot_printf("Erasing remainder of partition (%d sectors)...\n",
675+
size/sector_size);
689676

690-
/* erase to the last sector, writeonce has 2 sectors */
691-
while((sector * sector_size) < WOLFBOOT_PARTITION_SIZE -
692-
sector_size
693-
#ifdef NVM_FLASH_WRITEONCE
694-
* 2
695-
#endif
696-
) {
677+
#ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE
678+
/* Performant option: Erase remainder of flash sectors in one HAL command */
679+
wb_flash_erase(&boot, sector * sector_size, size);
680+
wb_flash_erase(&update, sector * sector_size, size);
681+
#else
682+
/* Smaller code size option: Iterate over every remaining sector and erase
683+
* individually. Required on some targets (like stm32f4) due to code size */
684+
while (size >= sector_size) {
697685
wb_flash_erase(&boot, sector * sector_size, sector_size);
698686
wb_flash_erase(&update, sector * sector_size, sector_size);
699687
sector++;
688+
size -= sector_size;
700689
}
701-
702690
#endif /* !WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */
703691

704692
/* start re-entrant final erase, return code is only for resumption in
@@ -728,9 +716,14 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
728716
wolfBoot_copy_sector(&update, &boot, sector);
729717
sector++;
730718
}
731-
while ((sector * sector_size) < WOLFBOOT_PARTITION_SIZE) {
719+
/* erase remainder of partition */
720+
size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size);
721+
wolfBoot_printf("Erasing remainder of partition (%d sectors)...\n",
722+
size/sector_size);
723+
while (size >= sector_size) {
732724
wb_flash_erase(&boot, sector * sector_size, sector_size);
733725
sector++;
726+
size -= sector_size;
734727
}
735728
wolfBoot_set_partition_state(PART_BOOT, IMG_STATE_SUCCESS);
736729

0 commit comments

Comments
 (0)