Skip to content

Commit 0ce77a4

Browse files
authored
Merge pull request #590 from danielinux/flash-write-as-nsc
NSC functions: add helper to facilitate updates as trustzone non-secure callable
2 parents 21707c7 + cd347b5 commit 0ce77a4

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

hal/rp2350.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,21 @@ void hal_prepare_boot(void)
224224

225225
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
226226
{
227-
flash_range_program(address - XIP_BASE, data, len);
227+
uint8_t cache[WOLFBOOT_SECTOR_SIZE];
228+
uint32_t written = 0;
229+
uint32_t sz;
230+
if (((uintptr_t)data & 0x20000000UL) == 0) {
231+
/* Not in RAM: copy to cache before writing */
232+
while (written < len) {
233+
sz = WOLFBOOT_SECTOR_SIZE;
234+
if (sz > (len - written))
235+
sz = len - written;
236+
memcpy(cache, data + written, sz);
237+
flash_range_program(address - XIP_BASE + written, cache, sz);
238+
written += sz;
239+
}
240+
} else
241+
flash_range_program(address - XIP_BASE, data, len);
228242
return 0;
229243
}
230244

include/wolfboot/wolfboot.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,38 @@ int wolfBoot_set_encrypt_key(const uint8_t *key, const uint8_t *nonce);
412412
int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce);
413413
int wolfBoot_erase_encrypt_key(void);
414414

415+
#if !defined(__WOLFBOOT) && defined(WOLFCRYPT_SECURE_MODE)
416+
417+
/* Applications can access update success/trigger and flash erase/write
418+
* via non-secure callable, to facilitate updates
419+
*/
420+
421+
/* Call wolfBoot_success from non-secure application */
422+
423+
__attribute__((cmse_nonsecure_entry))
424+
void wolfBoot_nsc_success(void);
425+
426+
/* Call wolfBoot_update_trigger from non-secure application */
427+
__attribute__((cmse_nonsecure_entry))
428+
void wolfBoot_nsc_update_trigger(void);
429+
430+
/* Erase one or more sectors in the update partition.
431+
* - address: offset within the update partition ('0' corresponds to PARTITION_UPDATE_ADDRESS)
432+
* - len: size, in bytes
433+
*/
434+
__attribute__((cmse_nonsecure_entry))
435+
int wolfBoot_nsc_erase_update(uint32_t address, uint32_t len);
436+
437+
/* Write the content of buffer `buf` and size `len` to the update partition,
438+
* at offset address, from non-secure application
439+
* - address: offset within the update partition ('0' corresponds to PARTITION_UPDATE_ADDRESS)
440+
* - len: size, in bytes
441+
*/
442+
__attribute__((cmse_nonsecure_entry))
443+
int wolfBoot_nsc_write_update(uint32_t address, const uint8_t *buf, uint32_t len);
444+
445+
#endif /* !__WOLFBOOT && WOLFCRYPT_SECURE_MODE */
446+
415447

416448
#ifdef __cplusplus
417449
}

src/libwolfboot.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,3 +1999,40 @@ int wolfBoot_ram_decrypt(uint8_t *src, uint8_t *dst)
19991999
}
20002000
#endif /* MMU */
20012001
#endif /* EXT_ENCRYPTED */
2002+
2003+
#if defined(__WOLFBOOT) && defined(WOLFCRYPT_SECURE_MODE)
2004+
__attribute__((cmse_nonsecure_entry))
2005+
void wolfBoot_nsc_success(void)
2006+
{
2007+
wolfBoot_success();
2008+
}
2009+
2010+
__attribute__((cmse_nonsecure_entry))
2011+
void wolfBoot_nsc_update_trigger(void)
2012+
{
2013+
wolfBoot_update_trigger();
2014+
}
2015+
2016+
__attribute__((cmse_nonsecure_entry))
2017+
int wolfBoot_nsc_erase_update(uint32_t address, uint32_t len)
2018+
{
2019+
if (address > WOLFBOOT_PARTITION_SIZE)
2020+
return -1;
2021+
if (address + len > WOLFBOOT_PARTITION_SIZE)
2022+
return -1;
2023+
return hal_flash_erase(address + WOLFBOOT_PARTITION_UPDATE_ADDRESS, len);
2024+
2025+
}
2026+
2027+
__attribute__((cmse_nonsecure_entry))
2028+
int wolfBoot_nsc_write_update(uint32_t address, const uint8_t *buf, uint32_t len)
2029+
{
2030+
if (address > WOLFBOOT_PARTITION_SIZE)
2031+
return -1;
2032+
if (address + len > WOLFBOOT_PARTITION_SIZE)
2033+
return -1;
2034+
return hal_flash_write(address + WOLFBOOT_PARTITION_UPDATE_ADDRESS, buf, len);
2035+
}
2036+
2037+
#endif
2038+

0 commit comments

Comments
 (0)