Skip to content

Commit afa4b72

Browse files
committed
Fixed test to work in trustzone
1 parent d9713d7 commit afa4b72

20 files changed

Lines changed: 782 additions & 241 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*.idb
4646
*.pdb
4747
*.gdb
48+
*.log
4849

4950
# automatically generated keys
5051
*.der

test-app/emu-test-apps/common/emu_hal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include "target.h"
44
#include "hal.h"
55

6+
#ifndef EMU_FLASH_SECTOR_SIZE
7+
#define EMU_FLASH_SECTOR_SIZE WOLFBOOT_SECTOR_SIZE
8+
#endif
9+
610
#if defined(EMU_STM32)
711
#define FLASH_BASE 0x40022000u
812
#define FLASH_NSKEYR (*(volatile uint32_t *)(FLASH_BASE + 0x004u))

test-app/emu-test-apps/common/emu_update.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ static uint32_t emu_current_version(void)
8888
{
8989
uintptr_t addr = (uintptr_t)WOLFBOOT_PARTITION_BOOT_ADDRESS;
9090

91+
#ifdef WOLFCRYPT_SECURE_MODE
92+
return wolfBoot_nsc_current_firmware_version();
93+
#else
9194
if (addr == 0u) {
9295
return emu_get_blob_version_addr(0u);
9396
}
9497
return wolfBoot_get_blob_version((uint8_t *)addr);
98+
#endif
9599
}
96100

97101
void emu_uart_putc(char c)
@@ -153,7 +157,9 @@ static void wait_for_update(uint32_t version)
153157

154158
memset(page, 0xFF, PAGESIZE);
155159

160+
#ifndef WOLFCRYPT_SECURE_MODE
156161
hal_flash_unlock();
162+
#endif
157163

158164
emu_uart_write(START);
159165
for (i = 3; i >= 0; i--) {
@@ -209,23 +215,47 @@ static void wait_for_update(uint32_t version)
209215
page_idx += psize;
210216
if ((page_idx == PAGESIZE) || (next_seq + (uint32_t)psize >= tot_len)) {
211217
uint32_t dst = (WOLFBOOT_PARTITION_UPDATE_ADDRESS + recv_seq + (uint32_t)psize) - (uint32_t)page_idx;
218+
uint32_t dst_off = (recv_seq + (uint32_t)psize) - (uint32_t)page_idx;
219+
#ifdef WOLFCRYPT_SECURE_MODE
220+
if ((dst_off % WOLFBOOT_SECTOR_SIZE) == 0u) {
221+
wolfBoot_nsc_erase_update(dst_off, WOLFBOOT_SECTOR_SIZE);
222+
}
223+
wolfBoot_nsc_write_update(dst_off, page, PAGESIZE);
224+
#else
212225
if ((dst % WOLFBOOT_SECTOR_SIZE) == 0u) {
213226
hal_flash_erase(dst, WOLFBOOT_SECTOR_SIZE);
214227
}
215228
hal_flash_write(dst, page, PAGESIZE);
229+
#endif
216230
memset(page, 0xFF, PAGESIZE);
217231
}
218232
next_seq += (uint32_t)psize;
219233
}
220234
ack(next_seq);
221235
if (next_seq >= tot_len) {
236+
uint32_t update_ver;
237+
#ifdef WOLFCRYPT_SECURE_MODE
238+
update_ver = wolfBoot_nsc_update_firmware_version();
239+
#else
240+
update_ver = wolfBoot_get_blob_version((uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS);
241+
#endif
242+
if (update_ver == 7u) {
243+
__asm volatile("bkpt #0x4D");
244+
break;
245+
}
246+
#ifdef WOLFCRYPT_SECURE_MODE
247+
wolfBoot_nsc_update_trigger();
248+
#else
222249
wolfBoot_update_trigger();
250+
#endif
223251
__asm volatile("bkpt #0x47");
224252
break;
225253
}
226254
}
227255

256+
#ifndef WOLFCRYPT_SECURE_MODE
228257
hal_flash_lock();
258+
#endif
229259

230260
while (1) {
231261
__asm volatile("wfi");
@@ -242,7 +272,11 @@ int main(void)
242272
printf("get_version=%lu\n", (unsigned long)version);
243273

244274
if (version == 4u) {
275+
#ifdef WOLFCRYPT_SECURE_MODE
276+
wolfBoot_nsc_success();
277+
#else
245278
wolfBoot_success();
279+
#endif
246280
__asm volatile("bkpt #0x4A");
247281
while (1) {
248282
__asm volatile("wfi");
@@ -255,7 +289,11 @@ int main(void)
255289
}
256290
}
257291
if (version == 8u) {
292+
#ifdef WOLFCRYPT_SECURE_MODE
293+
wolfBoot_nsc_success();
294+
#else
258295
wolfBoot_success();
296+
#endif
259297
__asm volatile("bkpt #0x4E");
260298
while (1) {
261299
__asm volatile("wfi");

test-app/emu-test-apps/mcxw71/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ OBJCOPY ?= arm-none-eabi-objcopy
44
CFLAGS := -mcpu=cortex-m33 -mthumb -mcmse -Os -ffreestanding -fdata-sections -ffunction-sections -g -ggdb
55
MCXW71_SDK ?= ../../../../MCXW71
66
CFLAGS += -I. -I../common -I../../../include
7+
CFLAGS += -DIMAGE_HEADER_SIZE=$(IMAGE_HEADER_SIZE)
78
CFLAGS += -DCPU_MCXW716CMFPA
89
CFLAGS += -I$(MCXW71_SDK)/devices/MCXW716C -I$(MCXW71_SDK)/devices/MCXW716C/periph2
910
CFLAGS += -I$(MCXW71_SDK)/CMSIS/Core/Include
1011
LDFLAGS := -nostdlib -T target.ld -Wl,-gc-sections
1112
LDLIBS := -Wl,--start-group -lc -lm -lgcc -lnosys -Wl,--end-group
13+
NSC_OBJ :=
14+
ifeq ($(TZEN),1)
15+
CFLAGS += -DWOLFCRYPT_SECURE_MODE
16+
NSC_OBJ := ../../../src/wc_secure_calls.o
17+
endif
1218

1319
VPATH := ../common
1420

1521
COMMON_SRCS := emu_ivt.c emu_startup.c emu_syscalls.c emu_update.c emu_hal.c
1622
LIBWOLFBOOT_SRC := ../../../src/libwolfboot.c
1723
SRCS := $(COMMON_SRCS) uart.c
18-
OBJS := $(SRCS:.c=.o) libwolfboot.o
24+
OBJS := $(SRCS:.c=.o) libwolfboot.o $(NSC_OBJ)
1925

2026
all: app.bin
2127

@@ -32,6 +38,6 @@ libwolfboot.o: $(LIBWOLFBOOT_SRC)
3238
$(CC) $(CFLAGS) -c $< -o $@
3339

3440
clean:
35-
rm -f $(OBJS) app.elf app.bin
41+
rm -f $(SRCS:.c=.o) libwolfboot.o app.elf app.bin
3642

3743
.PHONY: all clean

test-app/emu-test-apps/mcxw71/target.h

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Minimal linker script for MCXW716C memory map */
2+
MEMORY
3+
{
4+
FLASH (rx) : ORIGIN = @FLASH_ORIGIN@, LENGTH = 0x000FFF00
5+
RAM (rwx) : ORIGIN = @FLASH_ORIGIN@, LENGTH = 0x0001C000
6+
}
7+
8+
_estack = ORIGIN(RAM) + LENGTH(RAM);
9+
_sidata = LOADADDR(.data);
10+
11+
SECTIONS
12+
{
13+
.isr_vector :
14+
{
15+
KEEP(*(.isr_vector))
16+
} > FLASH
17+
18+
.text :
19+
{
20+
*(.text*)
21+
*(.rodata*)
22+
*(.ARM.extab* .gnu.linkonce.armextab.*)
23+
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
24+
*(.glue_7)
25+
*(.glue_7t)
26+
*(.eh_frame)
27+
} > FLASH
28+
29+
.preinit_array :
30+
{
31+
__preinit_array_start = .;
32+
KEEP(*(.preinit_array*))
33+
__preinit_array_end = .;
34+
} > FLASH
35+
36+
.init_array :
37+
{
38+
__init_array_start = .;
39+
KEEP(*(.init_array*))
40+
__init_array_end = .;
41+
} > FLASH
42+
43+
.fini_array :
44+
{
45+
__fini_array_start = .;
46+
KEEP(*(.fini_array*))
47+
__fini_array_end = .;
48+
} > FLASH
49+
50+
.data :
51+
{
52+
_sdata = .;
53+
*(.data*)
54+
_edata = .;
55+
} > RAM AT > FLASH
56+
57+
.bss (NOLOAD) :
58+
{
59+
_sbss = .;
60+
*(.bss*)
61+
*(COMMON)
62+
_ebss = .;
63+
} > RAM
64+
}

test-app/emu-test-apps/nrf5340/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ OBJCOPY ?= arm-none-eabi-objcopy
33

44
CFLAGS := -mcpu=cortex-m33 -mthumb -mcmse -Os -ffreestanding -fdata-sections -ffunction-sections -g -ggdb
55
CFLAGS += -I. -I../common -I../../../include -DEMU_NRF5340
6+
CFLAGS += -DIMAGE_HEADER_SIZE=$(IMAGE_HEADER_SIZE)
67
LDFLAGS := -nostdlib -T target.ld -Wl,-gc-sections
78
LDLIBS := -Wl,--start-group -lc -lm -lgcc -lnosys -Wl,--end-group
9+
NSC_OBJ :=
10+
ifeq ($(TZEN),1)
11+
CFLAGS += -DWOLFCRYPT_SECURE_MODE
12+
NSC_OBJ := ../../../src/wc_secure_calls.o
13+
endif
814

915
VPATH := ../common
1016

1117
COMMON_SRCS := emu_ivt.c emu_startup.c emu_syscalls.c emu_update.c emu_hal.c
1218
LIBWOLFBOOT_SRC := ../../../src/libwolfboot.c
1319
SRCS := $(COMMON_SRCS) uart.c
14-
OBJS := $(SRCS:.c=.o) libwolfboot.o
20+
OBJS := $(SRCS:.c=.o) libwolfboot.o $(NSC_OBJ)
1521

1622
all: app.bin
1723

@@ -28,6 +34,6 @@ libwolfboot.o: $(LIBWOLFBOOT_SRC)
2834
$(CC) $(CFLAGS) -c $< -o $@
2935

3036
clean:
31-
rm -f $(OBJS) app.elf app.bin
37+
rm -f $(SRCS:.c=.o) libwolfboot.o app.elf app.bin
3238

3339
.PHONY: all clean

test-app/emu-test-apps/nrf5340/target.h

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* Minimal linker script for nRF5340 (application core) memory map */
2+
MEMORY
3+
{
4+
FLASH (rx) : ORIGIN = @FLASH_ORIGIN@, LENGTH = 0x000FFF00
5+
RAM (rwx) : ORIGIN = @FLASH_ORIGIN@, LENGTH = 0x00080000
6+
}
7+
8+
_estack = ORIGIN(RAM) + LENGTH(RAM);
9+
_sidata = LOADADDR(.data);
10+
11+
SECTIONS
12+
{
13+
.isr_vector :
14+
{
15+
KEEP(*(.isr_vector))
16+
} > FLASH
17+
18+
.text :
19+
{
20+
*(.text*)
21+
*(.rodata*)
22+
*(.ARM.extab* .gnu.linkonce.armextab.*)
23+
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
24+
*(.glue_7)
25+
*(.glue_7t)
26+
*(.eh_frame)
27+
} > FLASH
28+
29+
.preinit_array :
30+
{
31+
__preinit_array_start = .;
32+
KEEP(*(.preinit_array*))
33+
__preinit_array_end = .;
34+
} > FLASH
35+
36+
.init_array :
37+
{
38+
__init_array_start = .;
39+
KEEP(*(.init_array*))
40+
__init_array_end = .;
41+
} > FLASH
42+
43+
.fini_array :
44+
{
45+
__fini_array_start = .;
46+
KEEP(*(.fini_array*))
47+
__fini_array_end = .;
48+
} > FLASH
49+
50+
.data :
51+
{
52+
_sdata = .;
53+
*(.data*)
54+
_edata = .;
55+
} > RAM AT > FLASH
56+
57+
.bss (NOLOAD) :
58+
{
59+
_sbss = .;
60+
*(.bss*)
61+
*(COMMON)
62+
_ebss = .;
63+
} > RAM
64+
65+
.testpage ORIGIN(FLASH) + LENGTH(FLASH) - 0x1000 :
66+
{
67+
KEEP(*(.testpage*))
68+
} > FLASH
69+
}

test-app/emu-test-apps/stm32h563/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ OBJCOPY ?= arm-none-eabi-objcopy
33

44
CFLAGS := -mcpu=cortex-m33 -mthumb -mcmse -Os -ffreestanding -fdata-sections -ffunction-sections -g -ggdb
55
CFLAGS += -I. -I../common -I../../../include -DEMU_STM32
6+
CFLAGS += -DIMAGE_HEADER_SIZE=$(IMAGE_HEADER_SIZE)
67
LDFLAGS := -nostdlib -T target.ld -Wl,-gc-sections
78
LDLIBS := -Wl,--start-group -lc -lm -lgcc -lnosys -Wl,--end-group
9+
NSC_OBJ :=
10+
ifeq ($(TZEN),1)
11+
CFLAGS += -DWOLFCRYPT_SECURE_MODE
12+
NSC_OBJ := ../../../src/wc_secure_calls.o
13+
endif
814

915
VPATH := ../common
1016

1117
COMMON_SRCS := emu_ivt.c emu_startup.c emu_syscalls.c emu_update.c emu_hal.c
1218
LIBWOLFBOOT_SRC := ../../../src/libwolfboot.c
1319
SRCS := $(COMMON_SRCS) uart.c
14-
OBJS := $(SRCS:.c=.o) libwolfboot.o
20+
OBJS := $(SRCS:.c=.o) libwolfboot.o $(NSC_OBJ)
1521

1622
all: app.bin
1723

@@ -28,6 +34,6 @@ libwolfboot.o: $(LIBWOLFBOOT_SRC)
2834
$(CC) $(CFLAGS) -c $< -o $@
2935

3036
clean:
31-
rm -f $(OBJS) app.elf app.bin
37+
rm -f $(SRCS:.c=.o) libwolfboot.o app.elf app.bin
3238

3339
.PHONY: all clean

0 commit comments

Comments
 (0)