Debian Codegraph @

ports/stm32/mboot/Makefile

raw
1 # Select the board to build for:
2 ifdef BOARD_DIR
3 # Custom board path - remove trailing slash and get the final component of
4 # the path as the board name.
5 BOARD ?= $(notdir $(BOARD_DIR:/=))
6 else
7 # If not given on the command line, then default to PYBV10.
8 BOARD ?= PYBV10
9 BOARD_DIR ?= $(abspath ../boards/$(BOARD))
10 endif
11
12 # If the build directory is not given, make it reflect the board name.
13 BUILD ?= build-$(BOARD)
14
15 # Set USE_MBOOT to 1 so that TEXT0_ADDR gets set properly for those boards
16 # that can be built with or without mboot.
17 USE_MBOOT ?= 1
18
19 # Set MBOOT_ENABLE_PACKING to 1 to enable DFU packing with encryption and signing.
20 # Ensure the MBOOT_PACK_xxx values match stm32/Makefile, to build matching application firmware.
21 MBOOT_ENABLE_PACKING ?= 0
22 MBOOT_PACK_CHUNKSIZE ?= 16384
23 MBOOT_PACK_KEYS_FILE ?= $(BOARD_DIR)/mboot_keys.h
24
25 # Sanity check that the board configuration directory exists
26 ifeq ($(wildcard $(BOARD_DIR)/.),)
27 $(error Invalid BOARD specified: $(BOARD_DIR))
28 endif
29
30 # Enable BUILDING_MBOOT so boards can configure their .mk file accordingly.
31 BUILDING_MBOOT = 1
32
33 include ../../../py/mkenv.mk
34 include $(BOARD_DIR)/mpconfigboard.mk
35
36 # A board can set MBOOT_TEXT0_ADDR to a custom location where mboot should reside.
37 ifeq ($(MCU_SERIES),n6)
38 MBOOT_TEXT0_ADDR ?= 0x34180400
39 MBOOT_LD_FILES ?= stm32_memory_n6.ld stm32_sections.ld
40 else
41 MBOOT_TEXT0_ADDR ?= 0x08000000
42 MBOOT_LD_FILES ?= stm32_memory.ld stm32_sections.ld
43 endif
44
45 # The string in MBOOT_VERSION (default defined in version.c if not defined by a
46 # board) will be stored in the final MBOOT_VERSION_ALLOCATED_BYTES bytes of mboot flash.
47 # A board can change the size of this region by defining MBOOT_VERSION_ALLOCATED_BYTES.
48 MBOOT_VERSION_ALLOCATED_BYTES ?= 64
49 MBOOT_VERSION_INCLUDE_OPTIONS ?= 1 # if set to 1, this will append build options to version string (see version.c)
50
51 CROSS_COMPILE ?= arm-none-eabi-
52 USBDEV_DIR=usbdev
53 DFU=$(TOP)/tools/dfu.py
54 PYDFU ?= $(TOP)/tools/pydfu.py
55 BOOTLOADER_DFU_USB_VID ?= 0x0483
56 BOOTLOADER_DFU_USB_PID ?= 0xDF11
57 STFLASH ?= st-flash
58 OPENOCD ?= openocd
59 OPENOCD_CONFIG ?= boards/openocd_stm32f4.cfg
60
61 include ../stm32.mk
62
63 INC += -I.
64 INC += -I..
65 INC += -I$(TOP)
66 INC += -I$(BUILD)
67 INC += -I$(TOP)/lib/cmsis/inc
68 INC += -I$(STM32LIB_CMSIS_ABS)/Include
69 INC += -I$(STM32LIB_HAL_ABS)/Inc
70 INC += -I../$(USBDEV_DIR)/core/inc -I../$(USBDEV_DIR)/class/inc
71
72 # Standard C functions like memset need to be compiled with special flags so
73 # the compiler does not optimise these functions in terms of themselves.
74 CFLAGS_BUILTIN ?= -ffreestanding -fno-builtin -fno-lto
75
76 CFLAGS += $(INC) -Wall -Wpointer-arith -Wdouble-promotion -Wfloat-conversion -Werror -std=gnu99 -nostdlib $(CFLAGS_EXTRA)
77 CFLAGS += -D$(CMSIS_MCU)
78 CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES))
79 CFLAGS += $(COPT)
80 CFLAGS += -I$(BOARD_DIR)
81 CFLAGS += -DSTM32_HAL_H='<stm32$(MCU_SERIES)xx_hal.h>'
82 CFLAGS += -DBOARD_$(BOARD)
83 CFLAGS += -DMBOOT_VTOR=$(MBOOT_TEXT0_ADDR)
84 CFLAGS += -DAPPLICATION_ADDR=$(TEXT0_ADDR)
85 CFLAGS += -DFFCONF_H=\"ports/stm32/mboot/ffconf.h\"
86 CFLAGS += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT
87 CFLAGS += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT -DLFS2_READONLY
88 CFLAGS += -DBUILDING_MBOOT=$(BUILDING_MBOOT)
89 CFLAGS += -DMICROPY_HW_STM32WB_FLASH_SYNCRONISATION=0
90 CFLAGS += -DUSBD_ENABLE_VENDOR_DEVICE_REQUESTS=1
91 CFLAGS += -DBOOTLOADER_DFU_USB_VID=$(BOOTLOADER_DFU_USB_VID) -DBOOTLOADER_DFU_USB_PID=$(BOOTLOADER_DFU_USB_PID)
92 ifdef MBOOT_VERSION
93 CFLAGS += -DMBOOT_VERSION=\"$(MBOOT_VERSION)\"
94 endif
95 CFLAGS += -DMBOOT_VERSION_ALLOCATED_BYTES=$(MBOOT_VERSION_ALLOCATED_BYTES) -DMBOOT_VERSION_INCLUDE_OPTIONS=$(MBOOT_VERSION_INCLUDE_OPTIONS)
96
97 LDFLAGS += -nostdlib -L . $(addprefix -T,$(MBOOT_LD_FILES)) -Map=$(@:.elf=.map) --cref
98 LDFLAGS += --defsym mboot_version_len=$(MBOOT_VERSION_ALLOCATED_BYTES)
99 LIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
100
101 # Remove uncalled code from the final image.
102 CFLAGS += -fdata-sections -ffunction-sections
103 LDFLAGS += --gc-sections
104
105 # Debugging/Optimization
106 ifeq ($(DEBUG), 1)
107 CFLAGS += -g -DPENDSV_DEBUG
108 COPT = -Og
109 else
110 COPT += -Os -DNDEBUG
111 endif
112
113 $(BUILD)/shared/libc/string0.o: CFLAGS += $(CFLAGS_BUILTIN)
114 LIB_SRC_C += \
115 shared/libc/string0.c \
116 lib/littlefs/lfs1.c \
117 lib/littlefs/lfs1_util.c \
118 lib/littlefs/lfs2.c \
119 lib/littlefs/lfs2_util.c \
120 lib/oofatfs/ff.c \
121 lib/oofatfs/ffunicode.c \
122 lib/uzlib/adler32.c \
123 lib/uzlib/crc32.c \
124 lib/uzlib/header.c \
125 lib/uzlib/tinflate.c
126
127 SRC_C += \
128 adc.c \
129 main.c \
130 elem.c \
131 fsload.c \
132 gzstream.c \
133 pack.c \
134 sdcard.c \
135 ui.c \
136 vfs_fat.c \
137 vfs_lfs.c \
138 vfs_raw.c \
139 version.c \
140 drivers/bus/softspi.c \
141 drivers/bus/softqspi.c \
142 drivers/memory/spiflash.c \
143 ports/stm32/flash.c \
144 ports/stm32/i2cslave.c \
145 ports/stm32/powerctrlboot.c \
146 ports/stm32/qspi.c \
147 ports/stm32/usbd_conf.c \
148 ports/stm32/xspi.c \
149 $(wildcard $(BOARD_DIR)/*.c)
150
151 SRC_O += \
152 $(STARTUP_FILE) \
153 $(SYSTEM_FILE) \
154
155 ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f0 g0 l0))
156 SRC_O += ports/stm32/resethandler_m0.o
157 else
158 SRC_O += ports/stm32/resethandler.o
159 endif
160
161 ifeq ($(MBOOT_ENABLE_PACKING), 1)
162
163 SRC_C += lib/libhydrogen/hydrogen.c
164
165 CFLAGS += -DMBOOT_ENABLE_PACKING=1 -DPARTICLE -DPLATFORM_ID=3
166 CFLAGS += -DMBOOT_PACK_CHUNKSIZE=$(MBOOT_PACK_CHUNKSIZE)
167 CFLAGS += -DMBOOT_PACK_KEYS_FILE=\"$(MBOOT_PACK_KEYS_FILE)\"
168 endif
169
170 $(BUILD)/$(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_ll_usb.o: CFLAGS += -Wno-attributes
171 SRC_HAL += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
172 hal.c \
173 hal_cortex.c \
174 hal_dma.c \
175 hal_pcd.c \
176 hal_pcd_ex.c \
177 hal_pwr_ex.c \
178 hal_rcc.c \
179 hal_rcc_ex.c \
180 ll_rcc.c \
181 ll_usb.c \
182 )
183
184 ifneq ($(MCU_SERIES),n6)
185 SRC_HAL += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
186 hal_flash.c \
187 hal_flash_ex.c \
188 )
189 endif
190
191 ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 h7))
192 SRC_HAL += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
193 hal_mmc.c \
194 hal_sd.c \
195 ll_sdmmc.c \
196 )
197 endif
198
199 ifeq ($(MCU_SERIES),n6)
200 SRC_HAL += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
201 hal_bsec.c \
202 )
203 endif
204
205 SRC_USBDEV += $(addprefix ports/stm32/$(USBDEV_DIR)/,\
206 core/src/usbd_core.c \
207 core/src/usbd_ctlreq.c \
208 core/src/usbd_ioreq.c \
209 )
210
211 OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
212 OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
213 OBJ += $(addprefix $(BUILD)/, $(SRC_O))
214 OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o))
215 OBJ += $(addprefix $(BUILD)/, $(SRC_USBDEV:.c=.o))
216
217 all: $(TOP)/lib/stm32lib/README.md $(BUILD)/firmware.dfu $(BUILD)/firmware.hex
218
219 # For convenience, automatically fetch required submodules if they don't exist
220 $(TOP)/lib/stm32lib/README.md:
221 $(ECHO) "stm32lib submodule not found, fetching it now..."
222 (cd $(TOP) && git submodule update --init lib/stm32lib)
223
224 .PHONY: deploy deploy-stlink deploy-trusted
225
226 deploy: $(BUILD)/firmware.dfu
227 $(ECHO) "Writing $< to the board"
228 $(Q)$(PYTHON) $(PYDFU) -u $<
229
230 deploy-stlink: $(BUILD)/firmware.dfu
231 $(ECHO) "Writing $< to the board via ST-LINK"
232 $(Q)$(STFLASH) write $(BUILD)/firmware.bin $(MBOOT_TEXT0_ADDR)
233
234 deploy-trusted: $(BUILD)/firmware-trusted.bin
235 $(STM32_CUBE_PROGRAMMER)/bin/STM32_Programmer.sh -c port=SWD mode=HOTPLUG ap=1 -el $(DKEL) -w $^ 0x70000000 -hardRst
236
237 $(BUILD)/firmware.bin: $(BUILD)/firmware.elf
238 $(ECHO) "Create $@"
239 $(Q)$(OBJCOPY) -O binary -j .isr_vector -j .text -j .data -j .mboot_version_text $^ $(BUILD)/firmware.bin
240
241 $(BUILD)/firmware.dfu: $(BUILD)/firmware.bin
242 $(ECHO) "Create $@"
243 $(Q)$(PYTHON) $(DFU) -b $(MBOOT_TEXT0_ADDR):$(BUILD)/firmware.bin $@
244
245 $(BUILD)/firmware.hex: $(BUILD)/firmware.elf
246 $(ECHO) "Create $@"
247 $(Q)$(OBJCOPY) -O ihex $< $@
248
249 $(BUILD)/firmware.elf: $(OBJ)
250 $(ECHO) "LINK $@"
251 $(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
252 $(Q)$(SIZE) $@
253
254 $(BUILD)/firmware-trusted.bin: $(BUILD)/firmware.bin
255 /bin/rm -f $@
256 $(STM32_CUBE_PROGRAMMER)/bin/STM32_SigningTool_CLI -bin $^ -nk -of 0x80000000 -t fsbl -o $@ -hv $(STM32_N6_HEADER_VERSION)
257
258 #########################################
259 # Rules to generate header files
260
261 MAKE_PINS = ../boards/make-pins.py
262 PREFIX_FILE = ../boards/stm32f4xx_prefix.c
263 BOARD_PINS = $(BOARD_DIR)/pins.csv
264 HEADER_BUILD = $(BUILD)/genhdr
265 GEN_QSTRDEFS_GENERATED = $(HEADER_BUILD)/qstrdefs.generated.h
266 GEN_ROOT_POINTERS = $(HEADER_BUILD)/root_pointers.h
267 GEN_PINS_SRC = $(BUILD)/pins_$(BOARD).c
268 GEN_PINS_HDR = $(HEADER_BUILD)/pins.h
269 GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h
270 GEN_PINS_AF_DEFS = $(HEADER_BUILD)/pins_af_defs.h
271 GEN_MPVERSION = $(HEADER_BUILD)/mpversion.h
272
273 $(OBJ): $(GEN_QSTRDEFS_GENERATED) $(GEN_ROOT_POINTERS) $(GEN_PINS_AF_DEFS) $(GEN_MPVERSION)
274
275 $(HEADER_BUILD):
276 $(MKDIR) -p $(BUILD)/genhdr
277
278 $(GEN_QSTRDEFS_GENERATED): | $(HEADER_BUILD)
279 $(Q)echo "// empty" > $@
280
281 $(GEN_ROOT_POINTERS): | $(HEADER_BUILD)
282 $(Q)echo "// empty" > $@
283
284 $(GEN_PINS_AF_DEFS): $(BOARD_PINS) $(MAKE_PINS) ../$(AF_FILE) $(PREFIX_FILE) | $(HEADER_BUILD)
285 $(ECHO) "GEN $@"
286 $(Q)$(PYTHON) $(MAKE_PINS) --board-csv $(BOARD_PINS) --af-csv ../$(AF_FILE) --prefix $(PREFIX_FILE) \
287 --output-source $(GEN_PINS_SRC) --output-header $(GEN_PINS_HDR) \
288 --output-af-const $(GEN_PINS_AF_CONST) --output-af-defs $(GEN_PINS_AF_DEFS) \
289 --mboot-mode
290
291 $(GEN_MPVERSION): | $(HEADER_BUILD)
292 $(PYTHON) ../../../py/makeversionhdr.py $@
293
294 #########################################
295
296 vpath %.S . $(TOP)
297 $(BUILD)/%.o: %.S
298 $(ECHO) "CC $<"
299 $(Q)$(CC) $(CFLAGS) -c -o $@ $<
300
301 vpath %.s . $(TOP)
302 $(BUILD)/%.o: %.s
303 $(ECHO) "AS $<"
304 $(Q)$(AS) -o $@ $<
305
306 define compile_c
307 $(ECHO) "CC $<"
308 $(Q)$(CC) $(CFLAGS) -c -MD -o $@ $<
309 @# The following fixes the dependency file.
310 @# See http://make.paulandlesley.org/autodep.html for details.
311 @# Regex adjusted from the above to play better with Windows paths, etc.
312 @$(CP) $(@:.o=.d) $(@:.o=.P); \
313 $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \
314 -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \
315 $(RM) -f $(@:.o=.d)
316 endef
317
318 vpath %.c . $(TOP)
319 $(BUILD)/%.o: %.c
320 $(call compile_c)
321
322 # $(sort $(var)) removes duplicates
323 #
324 # The net effect of this, is it causes the objects to depend on the
325 # object directories (but only for existence), and the object directories
326 # will be created if they don't exist.
327 OBJ_DIRS = $(sort $(dir $(OBJ)))
328 $(OBJ): | $(OBJ_DIRS)
329 $(OBJ_DIRS):
330 $(MKDIR) -p $@
331
332 clean:
333 $(RM) -rf $(BUILD) $(CLEAN_EXTRA)
334 .PHONY: clean
335
336 ###########################################
337
338 -include $(OBJ:.o=.P)