@@ -8,12 +8,14 @@ TARGET = badgemagic-ch582
8
8
# building variables
9
9
# #####################################
10
10
# Uncomment below line to enable debugging
11
- # DEBUG = 1
11
+ DEBUG = 1
12
12
# Uncomment below to build for USB-C version
13
- # USBC_VERSION = 1
13
+ USBC_VERSION = 1
14
14
# optimization for size
15
15
OPT = -Os
16
16
17
+ OPENOCD ?= ../MRS_Toolchain_Linux_x64_V1.91/OpenOCD/bin/openocd
18
+
17
19
18
20
# ######################################
19
21
# Get current version
@@ -55,7 +57,6 @@ CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_uart1.c \
55
57
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_usb2dev.c \
56
58
CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_spi1.c \
57
59
CH5xx_ble_firmware_library/RVMSIS/core_riscv.c \
58
- src/main.c \
59
60
src/debug.c \
60
61
src/leddrv.c \
61
62
src/button.c \
@@ -83,7 +84,54 @@ src/resource.c \
83
84
src/animation.c \
84
85
src/font.c \
85
86
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 )
87
135
88
136
# ASM sources
89
137
ASM_SOURCES = \
@@ -121,11 +169,14 @@ MCU = $(CPU) $(FPU) $(FLOAT-ABI)
121
169
AS_INCLUDES =
122
170
123
171
# C includes
124
- C_INCLUDES = \
172
+ C_INCLUDES + = \
125
173
-ICH5xx_ble_firmware_library/StdPeriphDriver/inc \
126
174
-ICH5xx_ble_firmware_library/RVMSIS \
127
175
-ICH5xx_ble_firmware_library/Core \
128
176
-ICH5xx_ble_firmware_library/BLE \
177
+ -Iinc \
178
+ -Iexternal/tinycrypt/lib/include \
179
+ -I$(MCUBOOT_INC_DIR )
129
180
130
181
# compile gcc flags
131
182
ASFLAGS = $(MCU ) $(AS_INCLUDES ) $(OPT ) -Wall -fdata-sections -ffunction-sections
@@ -151,19 +202,26 @@ CFLAGS += -MMD -MP
151
202
# LDFLAGS
152
203
# ######################################
153
204
# link script
154
- LDSCRIPT = CH5xx_ble_firmware_library/Ld/Link.ld
205
+ # LDSCRIPT = CH5xx_ble_firmware_library/Ld/Link.ld
155
206
156
207
# libraries
157
208
LIBS = -lc -lm -lnosys \
158
209
./CH5xx_ble_firmware_library/StdPeriphDriver/libISP583.a \
159
210
./CH5xx_ble_firmware_library/BLE/LIBCH58xBLE.a \
160
211
161
212
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 ) \
163
217
164
218
# 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 \
167
225
168
226
# ######################################
169
227
# build the application
@@ -182,9 +240,14 @@ $(BUILD_DIR)/%.o: %.S Makefile
182
240
@mkdir -pv $(dir $@ )
183
241
$(AS ) -c $(CFLAGS ) $< -o $@
184
242
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
186
249
@mkdir -pv $(dir $@ )
187
- $(CC ) $(OBJECTS ) $(LDFLAGS ) -o $@
250
+ $(CC ) $(OBJECTS ) $(BUILD_DIR ) /src/boot-entry.o $( LDFLAGS ) -T bootloader.ld -o $@
188
251
$(SZ ) $@
189
252
190
253
$(BUILD_DIR ) /% .hex : $(BUILD_DIR ) /% .elf
@@ -193,16 +256,37 @@ $(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf
193
256
194
257
$(BUILD_DIR ) /% .bin : $(BUILD_DIR ) /% .elf
195
258
@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 \
197
266
198
267
# ######################################
199
268
# Program
200
269
# ######################################
201
270
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;'
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
203
287
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
206
290
207
291
# ######################################
208
292
# clean up
0 commit comments