Skip to content

Commit bcfb423

Browse files
committed
Added 'emu-test-apps' + test.sh script for m33mu
1 parent 8577188 commit bcfb423

33 files changed

Lines changed: 1962 additions & 1 deletion

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ tools/delta/bmpatch
9191
# otp-keystore-gen binary
9292
tools/keytools/otp/otp-keystore-gen
9393

94+
# test-server binary
95+
test-app/emu-test-apps/*/test-update-server
96+
9497
# Vim swap files
9598
.*.swp
9699

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ hal/$(TARGET).o:
340340

341341
keytools_check: keytools
342342

343+
test-emu:
344+
$(MAKE) -C test-app/emu-test-apps WOLFBOOT_ROOT="$(CURDIR)" test-emu
345+
343346
# Generate the initial signing key (only if not using user-provided keys)
344347
# - Creates wolfboot_signing_private_key.der when USER_PRIVATE_KEY is not set
345348
# - If CERT_CHAIN_VERIFY is enabled and USER_CERT_CHAIN not provided, also generates cert chain with leaf key

test-app/emu-test-apps/Makefile

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
-include ../../.config
2+
include ../../tools/config.mk
3+
include ../../options.mk
4+
include ../../tools/test.mk
5+
6+
ifneq ($(wildcard $(WOLFBOOT_ROOT)/lib/wolfssl/wolfcrypt/src/asn.c),)
7+
WOLFBOOT_LIB_WOLFSSL:=$(WOLFBOOT_ROOT)/lib/wolfssl
8+
else ifneq ($(wildcard $(WOLFBOOT_ROOT)/../wolfssl/wolfcrypt/src/asn.c),)
9+
WOLFBOOT_LIB_WOLFSSL:=$(WOLFBOOT_ROOT)/../wolfssl
10+
endif
11+
export WOLFBOOT_LIB_WOLFSSL
12+
13+
WOLFBOOT_ROOT?=$(abspath ../..)
14+
15+
EMU_VERSION?=1
16+
EMU_EXPECT_VERSION?=$(EMU_VERSION)
17+
PRIVATE_KEY?=$(WOLFBOOT_ROOT)/wolfboot_signing_private_key.der
18+
19+
M33MU?=$(WOLFBOOT_ROOT)/../m33mu/build/m33mu
20+
21+
ifeq ($(TARGET),stm32h563)
22+
EMU_DIR=stm32h563
23+
EMU_CPU=stm32h563
24+
else ifeq ($(TARGET),stm32h5)
25+
EMU_DIR=stm32h563
26+
EMU_CPU=stm32h563
27+
else ifeq ($(TARGET),stm32u585)
28+
EMU_DIR=stm32u585
29+
EMU_CPU=stm32u585
30+
else ifeq ($(TARGET),stm32u5)
31+
EMU_DIR=stm32u585
32+
EMU_CPU=stm32u585
33+
else ifeq ($(TARGET),stm32l552)
34+
EMU_DIR=stm32l552
35+
EMU_CPU=stm32l552
36+
else ifeq ($(TARGET),stm32l5)
37+
EMU_DIR=stm32l552
38+
EMU_CPU=stm32l552
39+
else ifeq ($(TARGET),nrf5340)
40+
EMU_DIR=nrf5340
41+
EMU_CPU=nrf5340
42+
else ifeq ($(TARGET),mcxw)
43+
EMU_DIR=mcxw71
44+
EMU_CPU=mcxw71c
45+
else ifeq ($(TARGET),mcxw71)
46+
EMU_DIR=mcxw71
47+
EMU_CPU=mcxw71c
48+
else
49+
EMU_DIR=
50+
EMU_CPU=
51+
endif
52+
53+
ifeq ($(strip $(EMU_DIR)),)
54+
$(error Unsupported or unset TARGET=$(TARGET). Use TARGET=stm32h563|stm32h5|stm32u585|stm32u5|stm32l552|stm32l5|nrf5340|mcxw)
55+
endif
56+
57+
EMU_PATH=$(WOLFBOOT_ROOT)/test-app/emu-test-apps/$(EMU_DIR)
58+
59+
.PHONY: all clean test-emu sign-emu run-emu
60+
61+
all:
62+
$(MAKE) -C $(EMU_PATH) clean app.bin
63+
64+
sign-emu: all $(WOLFBOOT_ROOT)/wolfboot_signing_private_key.der
65+
@cp $(EMU_PATH)/app.bin $(EMU_PATH)/image.bin
66+
$(SIGN_ENV) $(SIGN_TOOL) $(SIGN_OPTIONS) $(EMU_PATH)/image.bin $(PRIVATE_KEY) $(EMU_VERSION)
67+
68+
run-emu: sign-emu
69+
@echo "[EMU] CPU=$(EMU_CPU) VERSION=$(EMU_VERSION)"
70+
@$(M33MU) --cpu $(EMU_CPU) --uart-stdout --no-tz --boot-offset=$(IMAGE_HEADER_SIZE) \
71+
$(EMU_PATH)/image_v$(EMU_VERSION)_signed.bin --timeout 2 \
72+
| tee $(EMU_PATH)/emu_uart.log
73+
@grep -q "get_version=$(EMU_EXPECT_VERSION)" $(EMU_PATH)/emu_uart.log
74+
75+
# Entry point for top-level make test-emu
76+
# Builds, signs, runs, and validates UART output.
77+
78+
test-emu: run-emu
79+
80+
clean:
81+
$(MAKE) -C $(EMU_PATH) clean
82+
@rm -f $(EMU_PATH)/image.bin $(EMU_PATH)/image_v*_signed.bin $(EMU_PATH)/emu_uart.log
83+
84+
$(WOLFBOOT_ROOT)/wolfboot_signing_private_key.der:
85+
$(MAKE) -C $(WOLFBOOT_ROOT) WOLFBOOT_LIB_WOLFSSL="$(WOLFBOOT_LIB_WOLFSSL)" wolfboot_signing_private_key.der
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef EMU_APP_H
2+
#define EMU_APP_H
3+
4+
#include <stdint.h>
5+
6+
void emu_uart_init(void);
7+
int emu_uart_read(uint8_t *c);
8+
void emu_uart_write(uint8_t c);
9+
10+
#endif /* EMU_APP_H */
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <stdint.h>
2+
#include <string.h>
3+
#include "target.h"
4+
#include "hal.h"
5+
6+
#if defined(EMU_STM32)
7+
#define FLASH_BASE 0x40022000u
8+
#define FLASH_NSKEYR (*(volatile uint32_t *)(FLASH_BASE + 0x004u))
9+
#define FLASH_NSCR (*(volatile uint32_t *)(FLASH_BASE + 0x028u))
10+
#define FLASH_KEY1 0x45670123u
11+
#define FLASH_KEY2 0xCDEF89ABu
12+
#define FLASH_CR_LOCK (1u << 0)
13+
#define FLASH_CR_PG (1u << 1)
14+
#define FLASH_CR_SER (1u << 2)
15+
#define FLASH_CR_STRT (1u << 5)
16+
#define FLASH_CR_SNB_SHIFT 6
17+
#define FLASH_CR_SNB_MASK (0x7fu << FLASH_CR_SNB_SHIFT)
18+
#endif
19+
20+
#if defined(EMU_NRF5340)
21+
#define NVMC_BASE 0x40039000u
22+
#define NVMC_CONFIG (*(volatile uint32_t *)(NVMC_BASE + 0x504u))
23+
#endif
24+
25+
void hal_init(void)
26+
{
27+
}
28+
29+
void hal_prepare_boot(void)
30+
{
31+
}
32+
33+
int hal_flash_write(uint32_t address, const uint8_t *data, int len)
34+
{
35+
if (data == 0 || len <= 0) {
36+
return 0;
37+
}
38+
memcpy((void *)address, data, (size_t)len);
39+
return 0;
40+
}
41+
42+
int hal_flash_erase(uint32_t address, int len)
43+
{
44+
#if defined(EMU_NRF5340)
45+
(void)address;
46+
(void)len;
47+
return 0;
48+
#else
49+
uint32_t end;
50+
#if defined(EMU_STM32)
51+
uint32_t base = WOLFBOOT_PARTITION_BOOT_ADDRESS;
52+
uint32_t sector = EMU_FLASH_SECTOR_SIZE;
53+
uint32_t start_sector;
54+
uint32_t end_sector;
55+
uint32_t snb;
56+
static uint32_t last_erase_sector = 0xFFFFFFFFu;
57+
#endif
58+
if (len <= 0) {
59+
return 0;
60+
}
61+
#if defined(EMU_STM32)
62+
if (sector == 0u) {
63+
return 0;
64+
}
65+
if (address < base) {
66+
return 0;
67+
}
68+
end = address + (uint32_t)len;
69+
start_sector = (address - base) / sector;
70+
end_sector = ((end - 1u) - base) / sector;
71+
for (snb = start_sector; snb <= end_sector; ++snb) {
72+
if (snb == last_erase_sector) {
73+
continue;
74+
}
75+
uint32_t cr = FLASH_NSCR & ~FLASH_CR_SNB_MASK;
76+
cr |= FLASH_CR_SER | (snb << FLASH_CR_SNB_SHIFT);
77+
FLASH_NSCR = cr;
78+
FLASH_NSCR = cr | FLASH_CR_STRT;
79+
last_erase_sector = snb;
80+
}
81+
FLASH_NSCR &= ~FLASH_CR_SER;
82+
#else
83+
end = address + (uint32_t)len;
84+
memset((void *)address, 0xFF, (size_t)(end - address));
85+
#endif
86+
return 0;
87+
#endif
88+
}
89+
90+
void hal_flash_unlock(void)
91+
{
92+
#if defined(EMU_STM32)
93+
if ((FLASH_NSCR & FLASH_CR_LOCK) != 0u) {
94+
FLASH_NSKEYR = FLASH_KEY1;
95+
FLASH_NSKEYR = FLASH_KEY2;
96+
}
97+
FLASH_NSCR |= FLASH_CR_PG;
98+
#elif defined(EMU_NRF5340)
99+
NVMC_CONFIG = 1u;
100+
#endif
101+
}
102+
103+
void hal_flash_lock(void)
104+
{
105+
#if defined(EMU_STM32)
106+
FLASH_NSCR &= ~FLASH_CR_PG;
107+
FLASH_NSCR |= FLASH_CR_LOCK;
108+
#elif defined(EMU_NRF5340)
109+
NVMC_CONFIG = 0u;
110+
#endif
111+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdint.h>
2+
3+
extern void Reset_Handler(void);
4+
extern unsigned long _estack;
5+
6+
static void default_handler(void)
7+
{
8+
while (1) {
9+
}
10+
}
11+
12+
void NMI_Handler(void) __attribute__((weak, alias("default_handler")));
13+
void HardFault_Handler(void) __attribute__((weak, alias("default_handler")));
14+
void MemManage_Handler(void) __attribute__((weak, alias("default_handler")));
15+
void BusFault_Handler(void) __attribute__((weak, alias("default_handler")));
16+
void UsageFault_Handler(void) __attribute__((weak, alias("default_handler")));
17+
void SVC_Handler(void) __attribute__((weak, alias("default_handler")));
18+
void DebugMon_Handler(void) __attribute__((weak, alias("default_handler")));
19+
void PendSV_Handler(void) __attribute__((weak, alias("default_handler")));
20+
void SysTick_Handler(void) __attribute__((weak, alias("default_handler")));
21+
22+
__attribute__((section(".isr_vector")))
23+
const uint32_t vector_table[16 + 48] = {
24+
[0] = (uint32_t)&_estack,
25+
[1] = (uint32_t)&Reset_Handler,
26+
[2] = (uint32_t)&NMI_Handler,
27+
[3] = (uint32_t)&HardFault_Handler,
28+
[4] = (uint32_t)&MemManage_Handler,
29+
[5] = (uint32_t)&BusFault_Handler,
30+
[6] = (uint32_t)&UsageFault_Handler,
31+
[7] = 0, [8] = 0, [9] = 0, [10] = 0,
32+
[11] = (uint32_t)&SVC_Handler,
33+
[12] = (uint32_t)&DebugMon_Handler,
34+
[13] = 0,
35+
[14] = (uint32_t)&PendSV_Handler,
36+
[15] = (uint32_t)&SysTick_Handler,
37+
[16 ... 63] = (uint32_t)&default_handler
38+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdint.h>
2+
3+
extern uint32_t _sidata;
4+
extern uint32_t _sdata;
5+
extern uint32_t _edata;
6+
extern uint32_t _sbss;
7+
extern uint32_t _ebss;
8+
extern void __libc_init_array(void);
9+
10+
int main(void);
11+
12+
void Reset_Handler(void)
13+
{
14+
uint32_t *src;
15+
uint32_t *dst;
16+
17+
src = &_sidata;
18+
for (dst = &_sdata; dst < &_edata; ) {
19+
*dst++ = *src++;
20+
}
21+
22+
for (dst = &_sbss; dst < &_ebss; ) {
23+
*dst++ = 0u;
24+
}
25+
26+
__libc_init_array();
27+
(void)main();
28+
29+
while (1) {
30+
__asm volatile("wfi");
31+
}
32+
}

0 commit comments

Comments
 (0)