Skip to content

Commit 7cc3af7

Browse files
committed
Fix for the set_trailer_at with external flash to use 32-bit write using cached value (Many QSPI hardware peripherals do not support a single byte write). Fix delta build error with DISABLE_BACKUP. Added tests for updating both cores in build_flash.sh.
1 parent 297e101 commit 7cc3af7

File tree

9 files changed

+75
-63
lines changed

9 files changed

+75
-63
lines changed

config/examples/nrf5340.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ NO_ASM?=0
1111
NO_MPU=1
1212
ALLOW_DOWNGRADE?=0
1313
NVM_FLASH_WRITEONCE?=0
14+
DELTA_UPDATES?=1
1415

1516
SPMATH?=1
1617
RAM_CODE?=1
@@ -44,4 +45,8 @@ DEBUG?=0
4445
DEBUG_UART?=1
4546
USE_GCC=1
4647

48+
# Use larger block size for swapping sectors
49+
CFLAGS_EXTRA+=-DFLASHBUFFER_SIZE=0x1000
50+
4751
CFLAGS_EXTRA+=-DDEBUG_FLASH
52+
CFLAGS_EXTRA+=-DDEBUG_QSPI=1

config/examples/nrf5340_net.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ NO_ASM?=1
1111
NO_MPU=1
1212
ALLOW_DOWNGRADE?=0
1313
NVM_FLASH_WRITEONCE?=0
14+
DELTA_UPDATES?=1
1415

1516
SPMATH?=1
1617
RAM_CODE?=1

hal/nrf5340.c

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
#define SHARED_MEM_ADDR (0x20000000UL + (64 * 1024))
5454
#endif
5555
/* Shared memory states (mask, easier to check) */
56-
#define SHARED_STATUS_UNKNOWN 0
57-
#define SHARED_STATUS_READY 1
58-
#define SHARED_STATUS_UPDATE_START 2
59-
#define SHARED_STATUS_UPDATE_DONE 4
60-
#define SHARED_STATUS_DO_BOOT 8
56+
#define SHARED_STATUS_UNKNOWN 0x00
57+
#define SHARED_STATUS_READY 0x01
58+
#define SHARED_STATUS_UPDATE_START 0x02
59+
#define SHARED_STATUS_UPDATE_DONE 0x04
60+
#define SHARED_STATUS_DO_BOOT 0x08
6161

6262
#define SHAREM_MEM_MAGIC 0x5753484D /* WSHM */
6363

@@ -205,7 +205,7 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
205205
{
206206
uint32_t end = address + len - 1;
207207
uint32_t p;
208-
#ifdef DEBUG_FLASH
208+
#if defined(DEBUG_FLASH) && DEBUG_FLASH > 1
209209
wolfBoot_printf("Internal Flash Erase: addr 0x%x, len %d\n", address, len);
210210
#endif
211211
/* mask to page start address */
@@ -354,6 +354,7 @@ static int hal_shm_status_wait(ShmInfo_t* info, uint32_t status,
354354
return ret;
355355
}
356356

357+
/* Handles network core updates */
357358
static void hal_net_check_version(void)
358359
{
359360
int ret;
@@ -374,51 +375,8 @@ static void hal_net_check_version(void)
374375
/* check if network core can continue booting or needs to wait for update */
375376
if (ret != 0 || shm->app.version <= shm->net.version) {
376377
wolfBoot_printf("Network Core: Releasing for boot\n");
377-
hal_shm_status_set(&shm->app, SHARED_STATUS_DO_BOOT);
378378
}
379379
else {
380-
wolfBoot_printf("Network Core: Holding for update\n");
381-
}
382-
#else /* TARGET_nrf5340_net */
383-
hal_net_get_image(&img, &shm->net);
384-
hal_shm_status_set(&shm->net, SHARED_STATUS_READY);
385-
386-
/* wait for do_boot or update from app core */
387-
ret = hal_shm_status_wait(&shm->app,
388-
(SHARED_STATUS_UPDATE_START | SHARED_STATUS_DO_BOOT), 1000000);
389-
/* are we updating? */
390-
if (ret == 0 && shm->app.status == SHARED_STATUS_UPDATE_START) {
391-
wolfBoot_printf("Starting update: Ver %d->%d, Size %d->%d\n",
392-
shm->net.version, shm->app.version, shm->net.size, shm->net.size);
393-
/* Erase network core boot flash */
394-
hal_flash_erase((uintptr_t)img.hdr, shm->app.size);
395-
/* Write new firmware to internal flash */
396-
hal_flash_write((uintptr_t)img.hdr, shm->data, shm->app.size);
397-
398-
/* Reopen image and refresh information */
399-
hal_net_get_image(&img, &shm->net);
400-
wolfBoot_printf("Network version (after update): 0x%x\n",
401-
shm->net.version);
402-
hal_shm_status_set(&shm->net, SHARED_STATUS_UPDATE_DONE);
403-
404-
/* continue booting - boot process will validate image hash/signature */
405-
}
406-
#endif /* TARGET_nrf5340_* */
407-
exit:
408-
wolfBoot_printf("Status: App %d (ver %d), Net %d (ver %d)\n",
409-
shm->app.status, shm->app.version, shm->net.status, shm->net.version);
410-
}
411-
412-
#ifdef TARGET_nrf5340_app
413-
void hal_net_check_update(void)
414-
{
415-
int ret;
416-
uint32_t timeout;
417-
struct wolfBoot_image img;
418-
419-
/* handle update for network core */
420-
ret = hal_net_get_image(&img, &shm->app);
421-
if (ret == 0 && shm->app.version > shm->net.version) {
422380
wolfBoot_printf("Found Network Core update: Ver %d->%d, Size %d->%d\n",
423381
shm->net.version, shm->app.version, shm->net.size, shm->app.size);
424382

@@ -452,8 +410,37 @@ void hal_net_check_update(void)
452410
}
453411
/* inform network core to boot */
454412
hal_shm_status_set(&shm->app, SHARED_STATUS_DO_BOOT);
413+
#else /* TARGET_nrf5340_net */
414+
hal_net_get_image(&img, &shm->net);
415+
hal_shm_status_set(&shm->net, SHARED_STATUS_READY);
416+
417+
/* wait for do_boot or update from app core */
418+
wolfBoot_printf("Waiting for status from app core...\n");
419+
ret = hal_shm_status_wait(&shm->app,
420+
(SHARED_STATUS_UPDATE_START | SHARED_STATUS_DO_BOOT), 1000000);
421+
422+
/* are we updating? */
423+
if (ret == 0 && shm->app.status == SHARED_STATUS_UPDATE_START) {
424+
wolfBoot_printf("Starting update: Ver %d->%d, Size %d->%d\n",
425+
shm->net.version, shm->app.version, shm->net.size, shm->net.size);
426+
/* Erase network core boot flash */
427+
hal_flash_erase((uintptr_t)img.hdr, shm->app.size);
428+
/* Write new firmware to internal flash */
429+
hal_flash_write((uintptr_t)img.hdr, shm->data, shm->app.size);
430+
431+
/* Reopen image and refresh information */
432+
hal_net_get_image(&img, &shm->net);
433+
wolfBoot_printf("Network version (after update): 0x%x\n",
434+
shm->net.version);
435+
hal_shm_status_set(&shm->net, SHARED_STATUS_UPDATE_DONE);
436+
437+
/* continue booting - boot process will validate image hash/signature */
438+
}
439+
#endif /* TARGET_nrf5340_* */
440+
exit:
441+
wolfBoot_printf("Status: App %d (ver %d), Net %d (ver %d)\n",
442+
shm->app.status, shm->app.version, shm->net.status, shm->net.version);
455443
}
456-
#endif
457444

458445
void hal_init(void)
459446
{
@@ -488,8 +475,6 @@ void hal_prepare_boot(void)
488475
//BOOTLOADER_PARTITION_SIZE
489476

490477
#ifdef TARGET_nrf5340_app
491-
hal_net_check_update();
492-
493478
/* Restore defaults preventing network core from accessing shared SDRAM */
494479
SPU_EXTDOMAIN_PERM(0) =
495480
(SPU_EXTDOMAIN_PERM_SECATTR_NONSECURE | SPU_EXTDOMAIN_PERM_UNLOCK);

hal/spi/spi_drv_nrf5340.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
#define QSPI_IO3_PIN 16
8080
#endif
8181

82-
#ifndef QSPI_CLOCK_MHZ /* default 24MHz (up to 96MHz) */
83-
#define QSPI_CLOCK_MHZ 24000000UL
82+
#ifndef QSPI_CLOCK_MHZ /* default 48MHz (up to 96MHz) */
83+
#define QSPI_CLOCK_MHZ 48000000UL
8484
#endif
8585

8686
/* MX25R6435F */

include/spi_drv.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ int qspi_transfer(uint8_t fmode, const uint8_t cmd,
112112
uint8_t* data, uint32_t dataSz, uint32_t dataMode
113113
);
114114

115-
#if !defined(DEBUG_QSPI) && defined(DEBUG_UART)
116-
#define DEBUG_QSPI 1
117-
#endif
118115
#endif /* QSPI_FLASH || OCTOSPI_FLASH */
119116

120117
#ifndef SPI_CS_FLASH

src/libwolfboot.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,12 @@ static void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val)
453453
if (part == PART_BOOT) {
454454
#ifdef EXT_FLASH
455455
if (FLAGS_BOOT_EXT()) {
456+
/* use ext_cache and 32-bit writes to avoid any underlying hardware
457+
* issues with 1-byte write */
458+
ext_cache &= ~0xFF;
459+
ext_cache |= val;
456460
ext_flash_check_write(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at),
457-
(void *)&val, 1);
461+
(void *)&ext_cache, sizeof(uint32_t));
458462
}
459463
else
460464
#endif
@@ -465,8 +469,12 @@ static void RAMFUNCTION set_trailer_at(uint8_t part, uint32_t at, uint8_t val)
465469
else if (part == PART_UPDATE) {
466470
#ifdef EXT_FLASH
467471
if (FLAGS_UPDATE_EXT()) {
472+
/* use ext_cache and 32-bit writes to avoid any underlying hardware
473+
* issues with 1-byte write */
474+
ext_cache &= ~0xFF;
475+
ext_cache |= val;
468476
ext_flash_check_write(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at),
469-
(void *)&val, 1);
477+
(void *)&ext_cache, sizeof(uint32_t));
470478
}
471479
else
472480
#endif

src/qspi_flash.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ int spi_flash_write(uint32_t address, const void *data, int len)
422422
uint32_t xferSz, page, pages, idx = 0;
423423
uintptr_t addr;
424424

425+
#ifdef DEBUG_QSPI
426+
wolfBoot_printf("QSPI Flash Write: Len %d, %p -> 0x%x\n",
427+
len, data, address);
428+
#endif
429+
425430
/* write by page */
426431
pages = ((len + (FLASH_PAGE_SIZE-1)) / FLASH_PAGE_SIZE);
427432
for (page = 0; page < pages; page++) {
@@ -444,8 +449,9 @@ int spi_flash_write(uint32_t address, const void *data, int len)
444449
xferSz, QSPI_DATA_MODE /* Data */
445450
);
446451
#ifdef DEBUG_QSPI
447-
wolfBoot_printf("QSPI Flash Write: Ret %d, Cmd 0x%x, Len %d , 0x%x -> %p\n",
448-
ret, FLASH_WRITE_CMD, xferSz, address, ptr);
452+
wolfBoot_printf("QSPI Flash Sector Write: "
453+
"Ret %d, Cmd 0x%x, Len %d, %p -> 0x%x\n",
454+
ret, FLASH_WRITE_CMD, xferSz, ptr, address);
449455
#endif
450456
if (ret != 0)
451457
break;

src/update_flash.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,11 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
438438
wb_flash_erase(boot, sector * WOLFBOOT_SECTOR_SIZE, WOLFBOOT_SECTOR_SIZE);
439439
sector++;
440440
}
441+
#ifndef DISABLE_BACKUP
441442
/* start re-entrant final erase, return code is only for resumption in
442443
* wolfBoot_start*/
443444
wolfBoot_swap_and_final_erase(0);
445+
#endif
444446
out:
445447
#ifdef EXT_FLASH
446448
ext_flash_lock();

tools/scripts/nrf5340/build_flash.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@ cp factory.bin tools/scripts/nrf5340/factory_app.bin
2828
tools/keytools/sign --ecc256 test-app/image.bin wolfboot_signing_private_key.der 2
2929
cp test-app/image_v2_signed.bin tools/scripts/nrf5340/image_v2_signed_app.bin
3030

31+
# Create a bin footer with wolfBoot trailer "BOOT" and "p" (ASCII for 0x70 == IMG_STATE_UPDATING):
32+
echo -n "pBOOT" > tools/scripts/nrf5340/trigger_magic.bin
33+
./tools/bin-assemble/bin-assemble \
34+
tools/scripts/nrf5340/update_app_v2.bin \
35+
0x0 tools/scripts/nrf5340/image_v2_signed_app.bin \
36+
0xEDFFB tools/scripts/nrf5340/trigger_magic.bin
37+
38+
3139
# Convert to HEX format for programmer tool
3240
arm-none-eabi-objcopy -I binary -O ihex --change-addresses 0x00000000 tools/scripts/nrf5340/factory_app.bin tools/scripts/nrf5340/factory_app.hex
3341
arm-none-eabi-objcopy -I binary -O ihex --change-addresses 0x01000000 tools/scripts/nrf5340/factory_net.bin tools/scripts/nrf5340/factory_net.hex
3442

35-
arm-none-eabi-objcopy -I binary -O ihex --change-addresses 0x10000000 tools/scripts/nrf5340/image_v2_signed_app.bin tools/scripts/nrf5340/image_v2_signed_app.hex
43+
arm-none-eabi-objcopy -I binary -O ihex --change-addresses 0x10000000 tools/scripts/nrf5340/update_app_v2.bin tools/scripts/nrf5340/update_app_v2.hex
3644
arm-none-eabi-objcopy -I binary -O ihex --change-addresses 0x10100000 tools/scripts/nrf5340/image_v2_signed_net.bin tools/scripts/nrf5340/image_v2_signed_net.hex
3745

3846

@@ -42,7 +50,7 @@ if [ "$1" == "erase" ]; then
4250
fi
4351

4452
# Program external flash
45-
nrfjprog -f nrf53 --program tools/scripts/nrf5340/image_v2_signed_app.hex --verify
53+
nrfjprog -f nrf53 --program tools/scripts/nrf5340/update_app_v2.hex --verify
4654
nrfjprog -f nrf53 --program tools/scripts/nrf5340/image_v2_signed_net.hex --verify
4755

4856

0 commit comments

Comments
 (0)