Skip to content

Commit dc091c4

Browse files
committed
WIP: port MCUboot
1 parent 2bbecf2 commit dc091c4

File tree

17 files changed

+1101
-26
lines changed

17 files changed

+1101
-26
lines changed

.github/workflows/build.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v4
1919
with:
20+
submodules: 'true'
2021
fetch-depth: 0 # fetch tags for versioning
2122

2223
- name: Cache toolchain
@@ -32,6 +33,10 @@ jobs:
3233
wget http://file-oss.mounriver.com/tools/${{ env.MRS_TOOLCHAIN }}.tar.xz
3334
tar -xvf ${{ env.MRS_TOOLCHAIN }}.tar.xz
3435
36+
- name: Install dependencies for MCUboot imgtool
37+
run: |
38+
pip3 install -e external/mcuboot/scripts/
39+
3540
- name: Build firmware
3641
run: |
3742
export PREFIX=${{ env.MRS_TOOLCHAIN }}/RISC-V_Embedded_GCC/bin/riscv-none-embed-

.gitmodules

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[submodule "external/mcuboot"]
2+
path = external/mcuboot
3+
url = https://github.com/mcu-tools/mcuboot.git
4+
branch = v2.1.0
5+
[submodule "external/tinycrypt"]
6+
path = external/tinycrypt
7+
url = https://github.com/intel/tinycrypt.git

Makefile

Lines changed: 101 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ TARGET = badgemagic-ch582
88
# building variables
99
######################################
1010
# Uncomment below line to enable debugging
11-
# DEBUG = 1
11+
DEBUG = 1
1212
# Uncomment below to build for USB-C version
13-
# USBC_VERSION = 1
13+
USBC_VERSION = 1
1414
# optimization for size
1515
OPT = -Os
1616

17+
OPENOCD ?= ../MRS_Toolchain_Linux_x64_V1.91/OpenOCD/bin/openocd
18+
1719

1820
#######################################
1921
# Get current version
@@ -55,7 +57,6 @@ CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_uart1.c \
5557
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_usb2dev.c \
5658
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_spi1.c \
5759
CH5xx_ble_firmware_library/RVMSIS/core_riscv.c \
58-
src/main.c \
5960
src/debug.c \
6061
src/leddrv.c \
6162
src/button.c \
@@ -83,7 +84,54 @@ src/resource.c \
8384
src/animation.c \
8485
src/font.c \
8586
src/power.c \
86-
87+
src/mcuboot.c \
88+
89+
MCUBOOT_DIR := external/mcuboot
90+
IMGTOOL_PY := $(MCUBOOT_DIR)/scripts/imgtool.py
91+
MCUBOOT_SRC_DIR := $(MCUBOOT_DIR)/boot/bootutil/src
92+
MCUBOOT_INC_DIR := $(MCUBOOT_DIR)/boot/bootutil/include
93+
94+
MCUBOOT_SRC_FILES += \
95+
$(MCUBOOT_SRC_DIR)/boot_record.c \
96+
$(MCUBOOT_SRC_DIR)/bootutil_misc.c \
97+
$(MCUBOOT_SRC_DIR)/bootutil_public.c \
98+
$(MCUBOOT_SRC_DIR)/caps.c \
99+
$(MCUBOOT_SRC_DIR)/encrypted.c \
100+
$(MCUBOOT_SRC_DIR)/fault_injection_hardening.c \
101+
$(MCUBOOT_SRC_DIR)/fault_injection_hardening_delay_rng_mbedtls.c \
102+
$(MCUBOOT_SRC_DIR)/image_ecdsa.c \
103+
$(MCUBOOT_SRC_DIR)/image_ed25519.c \
104+
$(MCUBOOT_SRC_DIR)/image_rsa.c \
105+
$(MCUBOOT_SRC_DIR)/image_validate.c \
106+
$(MCUBOOT_SRC_DIR)/loader.c \
107+
$(MCUBOOT_SRC_DIR)/swap_misc.c \
108+
$(MCUBOOT_SRC_DIR)/swap_move.c \
109+
$(MCUBOOT_SRC_DIR)/swap_scratch.c \
110+
$(MCUBOOT_SRC_DIR)/tlv.c
111+
112+
TINYCRYPT_DIR := external/tinycrypt/lib
113+
TINYCRYPT_SRC_DIR := $(TINYCRYPT_DIR)/source
114+
TINYCRYPT_INC_DIR := $(TINYCRYPT_DIR)/include
115+
116+
TINYCRYPT_SRC_FILES += \
117+
$(TINYCRYPT_SRC_DIR)/aes_decrypt.c \
118+
$(TINYCRYPT_SRC_DIR)/aes_encrypt.c \
119+
$(TINYCRYPT_SRC_DIR)/cbc_mode.c \
120+
$(TINYCRYPT_SRC_DIR)/ccm_mode.c \
121+
$(TINYCRYPT_SRC_DIR)/cmac_mode.c \
122+
$(TINYCRYPT_SRC_DIR)/ctr_mode.c \
123+
$(TINYCRYPT_SRC_DIR)/ctr_prng.c \
124+
$(TINYCRYPT_SRC_DIR)/hmac.c \
125+
$(TINYCRYPT_SRC_DIR)/hmac_prng.c \
126+
$(TINYCRYPT_SRC_DIR)/sha256.c \
127+
$(TINYCRYPT_SRC_DIR)/utils.c
128+
# ecc.c
129+
# ecc_dh.c
130+
# ecc_dsa.c
131+
# ecc_platform_specific.c
132+
133+
C_SOURCES += $(TINYCRYPT_SRC_FILES)
134+
C_SOURCES += $(MCUBOOT_SRC_FILES)
87135

88136
# ASM sources
89137
ASM_SOURCES = \
@@ -121,11 +169,14 @@ MCU = $(CPU) $(FPU) $(FLOAT-ABI)
121169
AS_INCLUDES =
122170

123171
# C includes
124-
C_INCLUDES = \
172+
C_INCLUDES += \
125173
-ICH5xx_ble_firmware_library/StdPeriphDriver/inc \
126174
-ICH5xx_ble_firmware_library/RVMSIS \
127175
-ICH5xx_ble_firmware_library/Core \
128176
-ICH5xx_ble_firmware_library/BLE \
177+
-Iinc \
178+
-Iexternal/tinycrypt/lib/include \
179+
-I$(MCUBOOT_INC_DIR)
129180

130181
# compile gcc flags
131182
ASFLAGS = $(MCU) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
@@ -151,19 +202,26 @@ CFLAGS += -MMD -MP
151202
# LDFLAGS
152203
#######################################
153204
# link script
154-
LDSCRIPT = CH5xx_ble_firmware_library/Ld/Link.ld
205+
# LDSCRIPT = CH5xx_ble_firmware_library/Ld/Link.ld
155206

156207
# libraries
157208
LIBS = -lc -lm -lnosys \
158209
./CH5xx_ble_firmware_library/StdPeriphDriver/libISP583.a \
159210
./CH5xx_ble_firmware_library/BLE/LIBCH58xBLE.a \
160211

161212
LIBDIR =
162-
LDFLAGS = $(MCU) -mno-save-restore -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wunused -Wuninitialized -T $(LDSCRIPT) -nostartfiles -Xlinker --gc-sections -Wl,-Map=$(BUILD_DIR)/$(TARGET).map --specs=nano.specs $(LIBS)
213+
LDFLAGS = $(MCU) -mno-save-restore -fmessage-length=0 -fsigned-char \
214+
-ffunction-sections -fdata-sections -Wunused -Wuninitialized -nostartfiles \
215+
-Xlinker --gc-sections -Wl,-Map=$(BUILD_DIR)/$(TARGET).map \
216+
--specs=nano.specs $(LIBS) \
163217

164218
# default action: build all
165-
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
166-
219+
all: $(BUILD_DIR)/$(TARGET).elf \
220+
$(BUILD_DIR)/$(TARGET).hex \
221+
$(BUILD_DIR)/$(TARGET).bin \
222+
$(BUILD_DIR)/$(TARGET).signed.bin \
223+
$(BUILD_DIR)/mcuboot.bin \
224+
$(BUILD_DIR)/combined.bin \
167225

168226
#######################################
169227
# build the application
@@ -182,9 +240,14 @@ $(BUILD_DIR)/%.o: %.S Makefile
182240
@mkdir -pv $(dir $@)
183241
$(AS) -c $(CFLAGS) $< -o $@
184242

185-
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
243+
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) $(BUILD_DIR)/src/main.o Makefile app.ld
244+
@mkdir -pv $(dir $@)
245+
$(CC) $(OBJECTS) $(BUILD_DIR)/src/main.o -T app.ld $(LDFLAGS) -o $@
246+
$(SZ) $@
247+
248+
$(BUILD_DIR)/mcuboot.elf: $(OBJECTS) $(BUILD_DIR)/src/boot-entry.o Makefile bootloader.ld
186249
@mkdir -pv $(dir $@)
187-
$(CC) $(OBJECTS) $(LDFLAGS) -o $@
250+
$(CC) $(OBJECTS) $(BUILD_DIR)/src/boot-entry.o $(LDFLAGS) -T bootloader.ld -o $@
188251
$(SZ) $@
189252

190253
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf
@@ -193,16 +256,38 @@ $(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf
193256

194257
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf
195258
@mkdir -pv $(dir $@)
196-
$(BIN) $< $@
259+
$(BIN) $< $@
260+
261+
$(BUILD_DIR)/combined.bin: $(BUILD_DIR)/mcuboot.bin $(BUILD_DIR)/$(TARGET).signed.bin
262+
srec_cat $(BUILD_DIR)/mcuboot.bin -Binary -offset 0 \
263+
-fill 0xff 0 0x0010000 \
264+
$(BUILD_DIR)/$(TARGET).signed.bin -Binary -offset 0x00010000 \
265+
-o $@ -Binary \
197266

198267
#######################################
199268
# Program
200269
#######################################
201270
program: $(BUILD_DIR)/$(TARGET).elf
202-
sudo wch-openocd -f /usr/share/wch-openocd/openocd/scripts/interface/wch-riscv.cfg -c 'init; halt; program $(BUILD_DIR)/$(TARGET).elf; reset; wlink_reset_resume; exit;'
203-
204-
isp: $(BUILD_DIR)/$(TARGET).bin
205-
wchisp flash $(BUILD_DIR)/$(TARGET).bin
271+
$(OPENOCD) -f interface/wch-riscv.cfg -c 'init; halt; program $(BUILD_DIR)/combined.bin ; reset; wlink_reset_resume; exit;'
272+
273+
debug:
274+
$(OPENOCD) -f debug.cfg
275+
276+
$(BUILD_DIR)/$(TARGET).signed.bin: $(BUILD_DIR)/$(TARGET).bin
277+
# --header-size 0x200
278+
python $(IMGTOOL_PY) sign \
279+
--header-size 0x200 \
280+
--align 4 \
281+
-S 204800 \
282+
-v 1.0.0 \
283+
--pad-header \
284+
$< $@
285+
286+
isp: $(BUILD_DIR)/$(TARGET).elf
287+
wchisp flash $(BUILD_DIR)/$(TARGET).elf
288+
289+
isp-bootld: $(BUILD_DIR)/mcuboot.elf
290+
wchisp flash $(BUILD_DIR)/mcuboot.elf -E
206291

207292
#######################################
208293
# clean up

app.ld

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
ENTRY( _start )
2+
3+
MEMORY
4+
{
5+
FLASH (rx) : ORIGIN = 0x00010000, LENGTH = 384k
6+
RAM (xrw) : ORIGIN = 0x20003800, LENGTH = 32K
7+
}
8+
9+
SECTIONS
10+
{
11+
.init :
12+
{
13+
_sinit = .;
14+
. = ALIGN(4);
15+
KEEP(*(SORT_NONE(.init)))
16+
. = ALIGN(4);
17+
_einit = .;
18+
} >FLASH AT>FLASH
19+
20+
/* .vector :
21+
{
22+
*(.vector);
23+
} >FLASH AT>FLASH */
24+
25+
.highcodelalign :
26+
{
27+
. = ALIGN(4);
28+
PROVIDE(_highcode_lma = .);
29+
} >FLASH AT>FLASH
30+
31+
.highcode :
32+
{
33+
. = ALIGN(4);
34+
PROVIDE(_highcode_vma_start = .);
35+
*(.vector);
36+
KEEP(*(SORT_NONE(.vector_handler)))
37+
*(.highcode);
38+
*(.highcode.*);
39+
. = ALIGN(4);
40+
PROVIDE(_highcode_vma_end = .);
41+
} >RAM AT>FLASH
42+
43+
.text :
44+
{
45+
. = ALIGN(4);
46+
KEEP(*(SORT_NONE(.handle_reset)))
47+
*(.text)
48+
*(.text.*)
49+
*(.rodata)
50+
*(.rodata*)
51+
*(.sdata2.*)
52+
*(.glue_7)
53+
*(.glue_7t)
54+
*(.gnu.linkonce.t.*)
55+
. = ALIGN(4);
56+
} >FLASH AT>FLASH
57+
58+
.fini :
59+
{
60+
KEEP(*(SORT_NONE(.fini)))
61+
. = ALIGN(4);
62+
} >FLASH AT>FLASH
63+
64+
PROVIDE( _etext = . );
65+
PROVIDE( _eitcm = . );
66+
67+
.preinit_array :
68+
{
69+
PROVIDE_HIDDEN (__preinit_array_start = .);
70+
KEEP (*(.preinit_array))
71+
PROVIDE_HIDDEN (__preinit_array_end = .);
72+
} >FLASH AT>FLASH
73+
74+
.init_array :
75+
{
76+
PROVIDE_HIDDEN (__init_array_start = .);
77+
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
78+
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
79+
PROVIDE_HIDDEN (__init_array_end = .);
80+
} >FLASH AT>FLASH
81+
82+
.fini_array :
83+
{
84+
PROVIDE_HIDDEN (__fini_array_start = .);
85+
KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
86+
KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
87+
PROVIDE_HIDDEN (__fini_array_end = .);
88+
} >FLASH AT>FLASH
89+
90+
.ctors :
91+
{
92+
/* gcc uses crtbegin.o to find the start of
93+
the constructors, so we make sure it is
94+
first. Because this is a wildcard, it
95+
doesn't matter if the user does not
96+
actually link against crtbegin.o; the
97+
linker won't look for a file to match a
98+
wildcard. The wildcard also means that it
99+
doesn't matter which directory crtbegin.o
100+
is in. */
101+
KEEP (*crtbegin.o(.ctors))
102+
KEEP (*crtbegin?.o(.ctors))
103+
104+
/* We don't want to include the .ctor section from
105+
the crtend.o file until after the sorted ctors.
106+
The .ctor section from the crtend file contains the
107+
end of ctors marker and it must be last */
108+
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
109+
KEEP (*(SORT(.ctors.*)))
110+
KEEP (*(.ctors))
111+
} >FLASH AT>FLASH
112+
113+
.dtors :
114+
{
115+
KEEP (*crtbegin.o(.dtors))
116+
KEEP (*crtbegin?.o(.dtors))
117+
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
118+
KEEP (*(SORT(.dtors.*)))
119+
KEEP (*(.dtors))
120+
} >FLASH AT>FLASH
121+
122+
.dalign :
123+
{
124+
. = ORIGIN(RAM) + MAX(0x800 , SIZEOF(.highcode));
125+
} >RAM AT>FLASH
126+
127+
.dlalign :
128+
{
129+
. = ALIGN(4);
130+
PROVIDE(_data_lma = .);
131+
} >FLASH AT>FLASH
132+
133+
.data :
134+
{
135+
. = ALIGN(4);
136+
PROVIDE(_data_vma = .);
137+
*(.gnu.linkonce.r.*)
138+
*(.data .data.*)
139+
*(.gnu.linkonce.d.*)
140+
. = ALIGN(8);
141+
PROVIDE( __global_pointer$ = . + 0x800 );
142+
*(.sdata .sdata.*)
143+
*(.gnu.linkonce.s.*)
144+
. = ALIGN(8);
145+
*(.srodata.cst16)
146+
*(.srodata.cst8)
147+
*(.srodata.cst4)
148+
*(.srodata.cst2)
149+
*(.srodata .srodata.*)
150+
. = ALIGN(4);
151+
PROVIDE( _edata = .);
152+
} >RAM AT>FLASH
153+
154+
.bss :
155+
{
156+
. = ALIGN(4);
157+
PROVIDE( _sbss = .);
158+
*(.sbss*)
159+
*(.gnu.linkonce.sb.*)
160+
*(.bss*)
161+
*(.gnu.linkonce.b.*)
162+
*(COMMON*)
163+
. = ALIGN(4);
164+
PROVIDE( _ebss = .);
165+
} >RAM AT>FLASH
166+
PROVIDE( _end = _ebss);
167+
PROVIDE( end = . );
168+
169+
.stack ORIGIN(RAM)+LENGTH(RAM) :
170+
{
171+
. = ALIGN(4);
172+
PROVIDE(_eusrstack = . );
173+
} >RAM
174+
}

0 commit comments

Comments
 (0)