@@ -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
1515OPT = -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 \
5557CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_usb2dev.c \
5658CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_spi1.c \
5759CH5xx_ble_firmware_library/RVMSIS/core_riscv.c \
58- src/main.c \
5960src/debug.c \
6061src/leddrv.c \
6162src/button.c \
@@ -83,7 +84,54 @@ src/resource.c \
8384src/animation.c \
8485src/font.c \
8586src/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
89137ASM_SOURCES = \
@@ -121,11 +169,14 @@ MCU = $(CPU) $(FPU) $(FLOAT-ABI)
121169AS_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
131182ASFLAGS = $(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
157208LIBS = -lc -lm -lnosys \
158209 ./CH5xx_ble_firmware_library/StdPeriphDriver/libISP583.a \
159210 ./CH5xx_ble_firmware_library/BLE/LIBCH58xBLE.a \
160211
161212LIBDIR =
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,37 @@ $(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# ######################################
201270program : $(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;'
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+ python $(IMGTOOL_PY ) sign \
278+ --header-size 0x200 \
279+ --align 4 \
280+ -S 204800 \
281+ -v 1.0.0 \
282+ --pad-header \
283+ $< $@
284+
285+ isp : $(BUILD_DIR ) /$(TARGET ) .elf
286+ wchisp flash $(BUILD_DIR ) /$(TARGET ) .elf
203287
204- isp : $(BUILD_DIR ) /$( TARGET ) .bin
205- wchisp flash $(BUILD_DIR ) /$( TARGET ) .bin
288+ isp-bootld : $(BUILD_DIR ) /mcuboot.elf
289+ wchisp flash $(BUILD_DIR ) /mcuboot.elf -E
206290
207291# ######################################
208292# clean up
0 commit comments