Skip to content

Commit eb175cd

Browse files
dgarskedanielinux
authored andcommitted
Switch network core to use external flash HAL, but map to shared memory. Allows using update_flash logic including encrypted and delta updates.
1 parent f2b929a commit eb175cd

3 files changed

Lines changed: 94 additions & 16 deletions

File tree

config/examples/nrf5340_net.config

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ RAM_CODE?=1
1919
DUALBANK_SWAP?=0
2020
FLAGS_HOME=0
2121
DISABLE_BACKUP=1
22-
EXT_FLASH?=0
22+
# Implementation maps to shared application core memory
23+
EXT_FLASH?=1
2324
SPI_FLASH?=0
2425
QSPI_FLASH?=0
2526

@@ -35,11 +36,11 @@ WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x0100C000
3536
# Application Partition Size (184KB)
3637
WOLFBOOT_PARTITION_SIZE?=0x2E000
3738

38-
# Flash offset for update (not used - handled by application core)
39-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0100C000
39+
# Flash offset for update (provided by application core to shared memory)
40+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x100000
4041

41-
# Flash offset for swap (not used - handled by application core)
42-
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x103A800
42+
# Flash offset for swap (uses shared memory)
43+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x12E000
4344

4445
V?=0
4546
DEBUG?=0

hal/nrf5340.c

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ typedef struct {
7474

7575
/* application places firmware here */
7676
uint8_t data[FLASH_SIZE_NET];
77+
/* used as "swap" */
78+
uint8_t swap[FLASH_PAGESZ_NET];
7779
} SharedMem_t;
7880
static SharedMem_t* shm = (SharedMem_t*)SHARED_MEM_ADDR;
7981

82+
#ifdef TARGET_nrf5340_net
83+
static int do_update = 0;
84+
#endif
85+
8086

8187
/* UART */
8288
#ifdef DEBUG_UART
@@ -232,6 +238,71 @@ void RAMFUNCTION hal_flash_lock(void)
232238
{
233239
}
234240

241+
#ifdef TARGET_nrf5340_net
242+
/* external flash is access application core shared memory directly */
243+
244+
/* calculates location in shared memory */
245+
static uintptr_t ext_flash_addr_calc(uintptr_t address)
246+
{
247+
if (address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS) {
248+
if (address >= WOLFBOOT_PARTITION_SWAP_ADDRESS) {
249+
address -= WOLFBOOT_PARTITION_SWAP_ADDRESS;
250+
}
251+
else { /* update */
252+
address -= WOLFBOOT_PARTITION_UPDATE_ADDRESS;
253+
}
254+
}
255+
/* check address */
256+
if (address >= (FLASH_SIZE_NET + FLASH_PAGESZ_NET)) {
257+
address = 0;
258+
}
259+
return address;
260+
}
261+
262+
int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
263+
{
264+
uintptr_t addr = ext_flash_addr_calc(address);
265+
#ifdef DEBUG_FLASH
266+
wolfBoot_printf("Ext Write: Len %d, Addr 0x%x (off 0x%x) -> 0x%x\n",
267+
len, address, addr, data);
268+
#endif
269+
memcpy(shm->data + addr, data, len);
270+
return 0;
271+
}
272+
273+
int ext_flash_read(uintptr_t address, uint8_t *data, int len)
274+
{
275+
uintptr_t addr = ext_flash_addr_calc(address);
276+
#ifdef DEBUG_FLASH
277+
wolfBoot_printf("Ext Read: Len %d, Addr 0x%x (off 0x%x) -> %p\n",
278+
len, address, addr, data);
279+
#endif
280+
281+
memcpy(data, shm->data + addr, len);
282+
return len;
283+
}
284+
285+
int ext_flash_erase(uintptr_t address, int len)
286+
{
287+
uintptr_t addr = ext_flash_addr_calc(address);
288+
#ifdef DEBUG_FLASH
289+
wolfBoot_printf("Ext Erase: Len %d, Addr 0x%x (off 0x%x)\n",
290+
len, address, addr);
291+
#endif
292+
memset(shm->data + addr, FLASH_BYTE_ERASED, len);
293+
return 0;
294+
}
295+
296+
void ext_flash_lock(void)
297+
{
298+
/* no op */
299+
}
300+
void ext_flash_unlock(void)
301+
{
302+
/* no op */
303+
}
304+
#endif /* TARGET_nrf5340_net */
305+
235306
static void clock_init(void)
236307
{
237308
#ifndef TARGET_nrf5340_net
@@ -423,18 +494,12 @@ static void hal_net_check_version(void)
423494
if (ret == 0 && shm->app.status == SHARED_STATUS_UPDATE_START) {
424495
wolfBoot_printf("Starting update: Ver %d->%d, Size %d->%d\n",
425496
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);
497+
do_update = 1;
430498

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);
499+
/* trigger update */
500+
wolfBoot_set_partition_state(PART_UPDATE, IMG_STATE_UPDATING);
436501

437-
/* continue booting - boot process will validate image hash/signature */
502+
/* proceed to update_flash routines */
438503
}
439504
#endif /* TARGET_nrf5340_* */
440505
exit:
@@ -474,6 +539,18 @@ void hal_prepare_boot(void)
474539
//WOLFBOOT_ORIGIN
475540
//BOOTLOADER_PARTITION_SIZE
476541

542+
#ifdef TARGET_nrf5340_net
543+
if (do_update) {
544+
/* signal application core of update */
545+
/* Reopen image and refresh information */
546+
struct wolfBoot_image img;
547+
hal_net_get_image(&img, &shm->net);
548+
wolfBoot_printf("Network version (after update): 0x%x\n",
549+
shm->net.version);
550+
hal_shm_status_set(&shm->net, SHARED_STATUS_UPDATE_DONE);
551+
}
552+
#endif
553+
477554
#ifdef TARGET_nrf5340_app
478555
/* Restore defaults preventing network core from accessing shared SDRAM */
479556
SPU_EXTDOMAIN_PERM(0) =

src/qspi_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ int spi_flash_read(uint32_t address, void *data, int len)
408408
);
409409

410410
#ifdef DEBUG_QSPI
411-
wolfBoot_printf("QSPI Flash Read: Ret %d, Cmd 0x%x, Len %d , 0x%x -> %p\n",
411+
wolfBoot_printf("QSPI Flash Read: Ret %d, Cmd 0x%x, Len %d, 0x%x -> %p\n",
412412
ret, FLASH_READ_CMD, len, address, data);
413413
#endif
414414

0 commit comments

Comments
 (0)