Skip to content

Commit 177358e

Browse files
committed
Update H5 app to use new NSC API
- Make the update and swap partitions secure and inaccessible from the app except via NSC API - Add a couple of necessary new NSC functions - Update the app to only use NSC API - Fix hal_flash_erase to account for secure addresses - Fix some bugs in xmodem implementation
1 parent e0e9217 commit 177358e

File tree

9 files changed

+101
-56
lines changed

9 files changed

+101
-56
lines changed

config/examples/stm32h5-tz-dualbank-otp-lms.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ DUALBANK_SWAP?=1
2121
WOLFBOOT_PARTITION_SIZE?=0xA0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
2323
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08160000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0C160000
2525
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0xFFFFFFFF
2626
FLAGS_HOME=0
2727
DISABLE_BACKUP=0

config/examples/stm32h5-tz-dualbank-otp.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ DUALBANK_SWAP?=1
2121
WOLFBOOT_PARTITION_SIZE?=0xA0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
2323
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08160000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0C160000
2525
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0xFFFFFFFF
2626
FLAGS_HOME=0
2727
DISABLE_BACKUP=0

config/examples/stm32h5-tz.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ DUALBANK_SWAP?=0
2121
WOLFBOOT_PARTITION_SIZE?=0xA0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
2323
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08100000
25-
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x081A0000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0C100000
25+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x0C1A0000
2626
FLAGS_HOME=0
2727
DISABLE_BACKUP=0
2828
WOLFCRYPT_TZ=1

config/examples/stm32h5.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ DUALBANK_SWAP?=0
2121
WOLFBOOT_PARTITION_SIZE?=0xA0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
2323
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08100000
25-
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x081A0000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0C100000
25+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x0C1A0000
2626
FLAGS_HOME=0
2727
DISABLE_BACKUP=0
2828
IMAGE_HEADER_SIZE?=1024

hal/stm32h5.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,14 @@
3232
#define PLL_SRC_HSE 1
3333

3434
#if TZ_SECURE()
35-
3635
static int is_flash_nonsecure(uint32_t address)
3736
{
38-
#ifndef DUALBANK_SWAP
39-
if (address >= WOLFBOOT_PARTITION_BOOT_ADDRESS) {
40-
return 1;
41-
}
42-
return 0;
43-
#else
44-
uint32_t in_bank_offset = (address & 0x000FFFFF);
45-
if (in_bank_offset >= (WOLFBOOT_PARTITION_BOOT_ADDRESS - FLASHMEM_ADDRESS_SPACE)) {
37+
if (address >= WOLFBOOT_PARTITION_BOOT_ADDRESS &&
38+
address < WOLFBOOT_PARTITION_BOOT_ADDRESS +
39+
WOLFBOOT_PARTITION_SIZE) {
4640
return 1;
4741
}
4842
return 0;
49-
#endif
5043
}
5144
#endif
5245

@@ -204,9 +197,15 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
204197
return -1;
205198

206199
#if TZ_SECURE()
207-
start_address = address | FLASH_SECURE_MMAP_BASE;
208-
if (is_flash_nonsecure(address)) {
209-
hal_tz_claim_nonsecure_area(address, len);
200+
if (address & FLASH_SECURE_MMAP_BIT) {
201+
/* Get address in non-secure address space */
202+
start_address = address & ~FLASH_SECURE_MMAP_BIT;
203+
}
204+
else {
205+
if (is_flash_nonsecure(address)) {
206+
hal_tz_claim_nonsecure_area(address, len);
207+
}
208+
start_address = address;
210209
}
211210
#else
212211
start_address = address;
@@ -218,8 +217,8 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
218217
uint32_t base;
219218
uint32_t bnksel = 0;
220219
base = FLASHMEM_ADDRESS_SPACE;
221-
reg = FLASH_CR & (~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_BER));
222-
if(p >= (FLASH_BANK2_BASE) && (p <= (FLASH_TOP) ))
220+
reg = FLASH_CR & (~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_SER | FLASH_CR_BER | FLASH_CR_PG | FLASH_CR_MER | FLASH_CR_BKSEL));
221+
if (p >= FLASH_BANK2_BASE && p <= FLASH_TOP)
223222
{
224223
base = FLASH_BANK2_BASE;
225224
bnksel = 1;
@@ -231,13 +230,13 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
231230
FLASH_CR = reg;
232231
ISB();
233232
FLASH_CR |= FLASH_CR_STRT;
234-
hal_flash_wait_complete(0);
233+
hal_flash_wait_complete(bnksel);
235234
}
236235
/* If the erase operation is completed, disable the associated bits */
237236
FLASH_CR &= ~FLASH_CR_SER ;
238237

239238
#if TZ_SECURE()
240-
if (is_flash_nonsecure(address)) {
239+
if (!(address & FLASH_SECURE_MMAP_BIT) && is_flash_nonsecure(address)) {
241240
hal_tz_release_nonsecure_area();
242241
}
243242
#endif

hal/stm32h5.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#endif
5050

5151
#define FLASH_SECURE_MMAP_BASE (0x0C000000)
52+
#define FLASH_SECURE_MMAP_BIT (0x04000000)
5253

5354
#define RCC_CR (*(volatile uint32_t *)(RCC_BASE + 0x00)) /* RM0481 - Table 108 */
5455
#define RCC_CR_PLL3RDY (1 << 29) /* RM0481 - Table 108 */

include/wolfboot/wolfboot.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,23 @@ int wolfBoot_erase_encrypt_key(void);
420420
*/
421421

422422
/* Call wolfBoot_success from non-secure application */
423-
424423
__attribute__((cmse_nonsecure_entry))
425424
void wolfBoot_nsc_success(void);
426425

427426
/* Call wolfBoot_update_trigger from non-secure application */
428427
__attribute__((cmse_nonsecure_entry))
429428
void wolfBoot_nsc_update_trigger(void);
430429

430+
/* Call wolfBoot_get_image_version from non-secure application */
431+
__attribute__((cmse_nonsecure_entry))
432+
uint32_t wolfBoot_nsc_get_image_version(uint8_t part);
433+
#define wolfBoot_nsc_current_firmware_version() wolfBoot_nsc_get_image_version(PART_BOOT)
434+
#define wolfBoot_nsc_update_firmware_version() wolfBoot_nsc_get_image_version(PART_UPDATE)
435+
436+
/* Call wolfBoot_get_partition_state from non-secure application */
437+
__attribute__((cmse_nonsecure_entry))
438+
int wolfBoot_nsc_get_partition_state(uint8_t part, uint8_t *st);
439+
431440
/* Erase one or more sectors in the update partition.
432441
* - address: offset within the update partition ('0' corresponds to PARTITION_UPDATE_ADDRESS)
433442
* - len: size, in bytes

src/libwolfboot.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,25 +2051,48 @@ void wolfBoot_nsc_update_trigger(void)
20512051
wolfBoot_update_trigger();
20522052
}
20532053

2054+
__attribute__((cmse_nonsecure_entry))
2055+
uint32_t wolfBoot_nsc_get_image_version(uint8_t part)
2056+
{
2057+
return wolfBoot_get_image_version(part);
2058+
}
2059+
2060+
__attribute__((cmse_nonsecure_entry))
2061+
int wolfBoot_nsc_get_partition_state(uint8_t part, uint8_t *st)
2062+
{
2063+
return wolfBoot_get_partition_state(part, st);
2064+
}
2065+
20542066
__attribute__((cmse_nonsecure_entry))
20552067
int wolfBoot_nsc_erase_update(uint32_t address, uint32_t len)
20562068
{
2069+
int ret;
2070+
20572071
if (address > WOLFBOOT_PARTITION_SIZE)
20582072
return -1;
20592073
if (address + len > WOLFBOOT_PARTITION_SIZE)
20602074
return -1;
2061-
return hal_flash_erase(address + WOLFBOOT_PARTITION_UPDATE_ADDRESS, len);
20622075

2076+
hal_flash_unlock();
2077+
ret = hal_flash_erase(address + WOLFBOOT_PARTITION_UPDATE_ADDRESS, len);
2078+
hal_flash_lock();
2079+
return ret;
20632080
}
20642081

20652082
__attribute__((cmse_nonsecure_entry))
20662083
int wolfBoot_nsc_write_update(uint32_t address, const uint8_t *buf, uint32_t len)
20672084
{
2085+
int ret;
2086+
20682087
if (address > WOLFBOOT_PARTITION_SIZE)
20692088
return -1;
20702089
if (address + len > WOLFBOOT_PARTITION_SIZE)
20712090
return -1;
2072-
return hal_flash_write(address + WOLFBOOT_PARTITION_UPDATE_ADDRESS, buf, len);
2091+
2092+
hal_flash_unlock();
2093+
ret = hal_flash_write(address + WOLFBOOT_PARTITION_UPDATE_ADDRESS, buf, len);
2094+
hal_flash_lock();
2095+
return ret;
20732096
}
20742097

20752098
#endif

test-app/app_stm32h5.c

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,22 @@ static int cmd_update_xmodem(const char *args)
267267
{
268268
int ret = -1;
269269
uint8_t xpkt[XMODEM_PACKET_SIZE];
270-
uint32_t dst_flash = (uint32_t)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
270+
uint32_t dst_offset = 0;
271271
uint8_t pkt_num = 0, pkt_num_expected=0xFF;
272272
uint32_t pkt_size = XMODEM_PACKET_SIZE;
273+
uint32_t t_size = 0;
273274
uint32_t update_ver = 0;
274275
uint32_t now = jiffies;
275276
uint32_t i = 0;
276277
uint8_t pkt_num_inv;
277278
uint8_t crc, calc_crc;
278279
int transfer_started = 0;
280+
int eot_expected = 0;
279281

280282

281283
printf("Erasing update partition...");
282284
fflush(stdout);
283-
hal_flash_unlock();
284-
hal_flash_erase(dst_flash, WOLFBOOT_PARTITION_SIZE);
285+
wolfBoot_nsc_erase_update(dst_offset, WOLFBOOT_PARTITION_SIZE);
285286
printf("Done.\r\n");
286287

287288
printf("Waiting for XMODEM transfer...\r\n");
@@ -303,6 +304,8 @@ static int cmd_update_xmodem(const char *args)
303304
}
304305
} else {
305306
now = jiffies;
307+
if (i == 0 && xpkt[0] == XEOT)
308+
break;
306309
i += ret;
307310
}
308311
}
@@ -313,6 +316,12 @@ static int cmd_update_xmodem(const char *args)
313316
extra_led_on();
314317
break;
315318
}
319+
else if (eot_expected) {
320+
ret = 1;
321+
uart_tx(XNAK);
322+
break;
323+
}
324+
316325
if (xpkt[0] != XSOH) {
317326
continue;
318327
}
@@ -335,10 +344,9 @@ static int cmd_update_xmodem(const char *args)
335344
crc = xpkt[XMODEM_PACKET_SIZE - 1];
336345
calc_crc = crc8(xpkt, XMODEM_PACKET_SIZE - 1);
337346
if (crc == calc_crc) {
338-
uint32_t t_size;
339347
/* CRC is valid */
340348
memcpy(xpkt_payload, xpkt + 3, XMODEM_PAYLOAD_SIZE);
341-
ret = hal_flash_write(dst_flash, xpkt_payload, XMODEM_PAYLOAD_SIZE);
349+
ret = wolfBoot_nsc_write_update(dst_offset, xpkt_payload, XMODEM_PAYLOAD_SIZE);
342350
if (ret != 0) {
343351
xcancel();
344352
printf("Error writing to flash\r\n");
@@ -347,15 +355,16 @@ static int cmd_update_xmodem(const char *args)
347355
uart_tx(XACK);
348356
pkt_num++;
349357
pkt_num_expected++;
350-
dst_flash += XMODEM_PAYLOAD_SIZE;
351-
t_size = *((uint32_t *)(WOLFBOOT_PARTITION_UPDATE_ADDRESS + 4));
352-
t_size += IMAGE_HEADER_SIZE;
353-
if ((uint32_t)dst_flash >= (WOLFBOOT_PARTITION_UPDATE_ADDRESS + t_size)) {
354-
ret = 0;
355-
extra_led_off();
356-
break;
358+
dst_offset += XMODEM_PAYLOAD_SIZE;
359+
if (t_size == 0) {
360+
/* At first packet, save expected partition size */
361+
t_size = *(uint32_t *)(xpkt_payload + 4);
362+
t_size += IMAGE_HEADER_SIZE;
357363
}
358-
uart_tx(XACK);
364+
if (dst_offset >= t_size) {
365+
eot_expected = 1;
366+
}
367+
/*uart_tx(XACK);*/
359368
} else {
360369
uart_tx(XNAK);
361370
}
@@ -367,17 +376,22 @@ static int cmd_update_xmodem(const char *args)
367376
uart_tx('\r');
368377

369378
printf("End of transfer. ret: %d\r\n", ret);
370-
update_ver = wolfBoot_update_firmware_version();
371-
if (update_ver != 0) {
372-
printf("New firmware version: 0x%lx\r\n", update_ver);
373-
printf("Triggering update...\r\n");
374-
wolfBoot_update_trigger();
375-
printf("Update completed successfully.\r\n");
376-
} else {
377-
printf("No valid image in update partition\r\n");
379+
if (ret != 0) {
380+
printf("Transfer failed\r\n");
381+
}
382+
else {
383+
printf("Transfer succeeded\r\n");
384+
update_ver = wolfBoot_nsc_update_firmware_version();
385+
if (update_ver != 0) {
386+
printf("New firmware version: 0x%lx\r\n", update_ver);
387+
printf("Triggering update...\r\n");
388+
wolfBoot_nsc_update_trigger();
389+
printf("Update written successfully. Reboot to apply.\r\n");
390+
} else {
391+
printf("No valid image in update partition\r\n");
392+
}
378393
}
379394

380-
hal_flash_lock();
381395
return ret;
382396
}
383397

@@ -427,17 +441,17 @@ static int cmd_info(const char *args)
427441
uint16_t hdrSz;
428442
uint8_t boot_part_state = IMG_STATE_NEW, update_part_state = IMG_STATE_NEW;
429443

430-
cur_fw_version = wolfBoot_current_firmware_version();
431-
update_fw_version = wolfBoot_update_firmware_version();
444+
cur_fw_version = wolfBoot_nsc_current_firmware_version();
445+
update_fw_version = wolfBoot_nsc_update_firmware_version();
432446

433-
wolfBoot_get_partition_state(PART_BOOT, &boot_part_state);
434-
wolfBoot_get_partition_state(PART_UPDATE, &update_part_state);
447+
wolfBoot_nsc_get_partition_state(PART_BOOT, &boot_part_state);
448+
wolfBoot_nsc_get_partition_state(PART_UPDATE, &update_part_state);
435449

436450
printf("\r\n");
437451
printf("System information\r\n");
438452
printf("====================================\r\n");
439453
printf("Flash banks are %sswapped.\r\n", ((FLASH_OPTSR_CUR & (FLASH_OPTSR_SWAP_BANK)) == 0)?"not ":"");
440-
printf("Firmware version : 0x%lx\r\n", wolfBoot_current_firmware_version());
454+
printf("Firmware version : 0x%lx\r\n", cur_fw_version);
441455
printf("Current firmware state: %s\r\n", part_state_name(boot_part_state));
442456
if (update_fw_version != 0) {
443457
if (update_part_state == IMG_STATE_UPDATING)
@@ -482,7 +496,7 @@ static int cmd_info(const char *args)
482496

483497
static int cmd_success(const char *args)
484498
{
485-
wolfBoot_success();
499+
wolfBoot_nsc_success();
486500
printf("update success confirmed.\r\n");
487501
return 0;
488502
}
@@ -741,14 +755,13 @@ void main(void)
741755
int ret;
742756
uint32_t app_version;
743757

744-
745758
/* Turn on boot LED */
746759
boot_led_on();
747760

748761
/* Enable SysTick */
749762
systick_enable();
750763

751-
app_version = wolfBoot_current_firmware_version();
764+
app_version = wolfBoot_nsc_current_firmware_version();
752765

753766
nvic_irq_setprio(NVIC_USART3_IRQN, 0);
754767
nvic_irq_enable(NVIC_USART3_IRQN);

0 commit comments

Comments
 (0)