1# SPDX-License-Identifier: GPL-2.0+
2#
3# (C) Copyright 2000-2011
4# Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5#
6# (C) Copyright 2011
7# Daniel Schwierzeck, daniel.schwierzeck@googlemail.com.
8#
9# (C) Copyright 2011
10# Texas Instruments Incorporated - http://www.ti.com/
11# Aneesh V <aneesh@ti.com>
12# Based on top-level Makefile.
13#
14
15src := $(obj)
16
17# Create output directory if not already present
18_dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
19
20include $(srctree)/scripts/Kbuild.include
21
22-include include/config/auto.conf
23-include $(obj)/include/autoconf.mk
24
25KBUILD_CPPFLAGS += -DCONFIG_SPL_BUILD
26ifeq ($(CONFIG_TPL_BUILD),y)
27KBUILD_CPPFLAGS += -DCONFIG_TPL_BUILD
28endif
29
30ifeq ($(CONFIG_TPL_BUILD),y)
31SPL_BIN := u-boot-tpl
32else
33SPL_BIN := u-boot-spl
34endif
35
36ifdef CONFIG_SPL_BUILD
37SPL_ := SPL_
38ifeq ($(CONFIG_TPL_BUILD),y)
39SPL_TPL_ := TPL_
40else
41SPL_TPL_ := SPL_
42endif
43else
44SPL_ :=
45SPL_TPL_ :=
46endif
47
48ifeq ($(obj)$(CONFIG_SUPPORT_SPL),spl)
49$(error You cannot build SPL without enabling CONFIG_SUPPORT_SPL)
50endif
51ifeq ($(obj)$(CONFIG_SUPPORT_TPL),tpl)
52$(error You cannot build TPL without enabling CONFIG_SUPPORT_TPL)
53endif
54
55include $(srctree)/config.mk
56include $(srctree)/arch/$(ARCH)/Makefile
57
58include $(srctree)/scripts/Makefile.lib
59
60# Enable garbage collection of un-used sections for SPL
61KBUILD_CFLAGS += -ffunction-sections -fdata-sections
62LDFLAGS_FINAL += --gc-sections
63
64# FIX ME
65cpp_flags := $(KBUILD_CPPFLAGS) $(PLATFORM_CPPFLAGS) $(UBOOTINCLUDE) \
66							$(NOSTDINC_FLAGS)
67c_flags := $(KBUILD_CFLAGS) $(cpp_flags)
68
69HAVE_VENDOR_COMMON_LIB = $(if $(wildcard $(srctree)/board/$(VENDOR)/common/Makefile),y,n)
70
71libs-y += $(if $(BOARDDIR),board/$(BOARDDIR)/)
72libs-$(HAVE_VENDOR_COMMON_LIB) += board/$(VENDOR)/common/
73
74libs-$(CONFIG_SPL_FRAMEWORK) += common/spl/
75libs-y += common/init/
76
77# Special handling for a few options which support SPL/TPL
78ifeq ($(CONFIG_TPL_BUILD),y)
79libs-$(CONFIG_TPL_LIBCOMMON_SUPPORT) += common/ cmd/ env/
80libs-$(CONFIG_TPL_LIBGENERIC_SUPPORT) += lib/
81else
82libs-$(CONFIG_SPL_LIBCOMMON_SUPPORT) += common/ cmd/ env/
83libs-$(CONFIG_SPL_LIBGENERIC_SUPPORT) += lib/
84ifdef CONFIG_SPL_FRAMEWORK
85libs-$(CONFIG_PARTITIONS) += disk/
86endif
87endif
88
89libs-y += drivers/
90libs-$(CONFIG_SPL_USB_GADGET) += drivers/usb/dwc3/
91libs-y += dts/
92libs-y += fs/
93libs-$(CONFIG_SPL_POST_MEM_SUPPORT) += post/drivers/
94libs-$(CONFIG_SPL_NET_SUPPORT) += net/
95
96head-y		:= $(addprefix $(obj)/,$(head-y))
97libs-y		:= $(addprefix $(obj)/,$(libs-y))
98u-boot-spl-dirs	:= $(patsubst %/,%,$(filter %/, $(libs-y)))
99
100libs-y := $(patsubst %/, %/built-in.o, $(libs-y))
101
102# Add GCC lib
103ifeq ($(CONFIG_USE_PRIVATE_LIBGCC),y)
104PLATFORM_LIBGCC = arch/$(ARCH)/lib/lib.a
105PLATFORM_LIBS := $(filter-out %/lib.a, $(filter-out -lgcc, $(PLATFORM_LIBS))) $(PLATFORM_LIBGCC)
106endif
107
108u-boot-spl-init := $(head-y)
109u-boot-spl-main := $(libs-y)
110ifdef CONFIG_$(SPL_TPL_)OF_PLATDATA
111u-boot-spl-platdata := $(obj)/dts/dt-platdata.o
112endif
113
114# Linker Script
115# First test whether there's a linker-script for the specific stage defined...
116ifneq ($(CONFIG_$(SPL_TPL_)LDSCRIPT),)
117# need to strip off double quotes
118LDSCRIPT := $(addprefix $(srctree)/,$(CONFIG_$(SPL_TPL_)LDSCRIPT:"%"=%))
119else
120# ...then fall back to the generic SPL linker-script
121ifneq ($(CONFIG_SPL_LDSCRIPT),)
122# need to strip off double quotes
123LDSCRIPT := $(addprefix $(srctree)/,$(CONFIG_SPL_LDSCRIPT:"%"=%))
124endif
125endif
126
127ifeq ($(wildcard $(LDSCRIPT)),)
128	LDSCRIPT := $(srctree)/board/$(BOARDDIR)/u-boot-spl.lds
129endif
130ifeq ($(wildcard $(LDSCRIPT)),)
131	LDSCRIPT := $(srctree)/$(CPUDIR)/u-boot-spl.lds
132endif
133ifeq ($(wildcard $(LDSCRIPT)),)
134	LDSCRIPT := $(srctree)/arch/$(ARCH)/cpu/u-boot-spl.lds
135endif
136ifeq ($(wildcard $(LDSCRIPT)),)
137$(error could not find linker script)
138endif
139
140# Special flags for CPP when processing the linker script.
141# Pass the version down so we can handle backwards compatibility
142# on the fly.
143LDPPFLAGS += \
144	-include $(srctree)/include/u-boot/u-boot.lds.h \
145	-include $(objtree)/include/config.h \
146	-DCPUDIR=$(CPUDIR) \
147	$(shell $(LD) --version | \
148	  sed -ne 's/GNU ld version \([0-9][0-9]*\)\.\([0-9][0-9]*\).*/-DLD_MAJOR=\1 -DLD_MINOR=\2/p')
149
150MKIMAGEOUTPUT ?= /dev/null
151
152quiet_cmd_mkimage = MKIMAGE $@
153cmd_mkimage = $(objtree)/tools/mkimage $(MKIMAGEFLAGS_$(@F)) -d $< $@ \
154	>$(MKIMAGEOUTPUT) $(if $(KBUILD_VERBOSE:0=), && cat $(MKIMAGEOUTPUT))
155
156quiet_cmd_mkfitimage = MKIMAGE $@
157cmd_mkfitimage = $(objtree)/tools/mkimage $(MKIMAGEFLAGS_$(@F)) -f $(SPL_ITS) -E $@ \
158	$(if $(KBUILD_VERBOSE:1=), MKIMAGEOUTPUT)
159
160MKIMAGEFLAGS_MLO = -T omapimage -a $(CONFIG_SPL_TEXT_BASE)
161
162MKIMAGEFLAGS_MLO.byteswap = -T omapimage -n byteswap -a $(CONFIG_SPL_TEXT_BASE)
163
164MLO MLO.byteswap: $(obj)/u-boot-spl.bin FORCE
165	$(call if_changed,mkimage)
166
167ifeq ($(CONFIG_SYS_SOC),"at91")
168MKIMAGEFLAGS_boot.bin = -T atmelimage
169
170ifeq ($(CONFIG_SPL_GENERATE_ATMEL_PMECC_HEADER),y)
171MKIMAGEFLAGS_boot.bin += -n $(shell $(obj)/../tools/atmel_pmecc_params)
172
173boot.bin: $(obj)/../tools/atmel_pmecc_params
174endif
175
176boot.bin: $(obj)/u-boot-spl.bin FORCE
177	$(call if_changed,mkimage)
178else
179ifdef CONFIG_ARCH_ZYNQ
180MKIMAGEFLAGS_boot.bin = -T zynqimage -R $(srctree)/$(CONFIG_BOOT_INIT_FILE)
181endif
182ifdef CONFIG_ARCH_ZYNQMP
183ifneq ($(CONFIG_PMUFW_INIT_FILE),"")
184spl/boot.bin: zynqmp-check-pmufw
185zynqmp-check-pmufw: FORCE
186	( cd $(srctree) && test -r $(CONFIG_PMUFW_INIT_FILE) ) \
187		|| ( echo "Cannot read $(CONFIG_PMUFW_INIT_FILE)" && false )
188endif
189MKIMAGEFLAGS_boot.bin = -T zynqmpimage -R $(srctree)/$(CONFIG_BOOT_INIT_FILE) \
190	-n "$(shell cd $(srctree); readlink -f $(CONFIG_PMUFW_INIT_FILE))"
191endif
192
193spl/boot.bin: $(obj)/u-boot-spl.bin FORCE
194	$(call if_changed,mkimage)
195endif
196
197ALL-y	+= $(obj)/$(SPL_BIN).bin
198
199ifdef CONFIG_SAMSUNG
200ALL-y	+= $(obj)/$(BOARD)-spl.bin
201endif
202
203ifdef CONFIG_ARCH_SOCFPGA
204ALL-y	+= $(obj)/$(SPL_BIN).sfp
205endif
206
207ifdef CONFIG_ARCH_SUNXI
208ALL-y	+= $(obj)/sunxi-spl.bin
209
210ifdef CONFIG_NAND_SUNXI
211ALL-y	+= $(obj)/sunxi-spl-with-ecc.bin
212endif
213endif
214
215ifeq ($(CONFIG_SYS_SOC),"at91")
216ALL-y	+= boot.bin
217endif
218
219ALL-$(CONFIG_SPL_X86_16BIT_INIT) += $(obj)/u-boot-x86-16bit-spl.bin
220
221ALL-$(CONFIG_ARCH_ZYNQ)		+= $(obj)/boot.bin
222ALL-$(CONFIG_ARCH_ZYNQMP)	+= $(obj)/boot.bin
223
224ALL-$(CONFIG_ARCH_MEDIATEK)	+= $(obj)/u-boot-spl-mtk.bin
225
226all:	$(ALL-y)
227
228quiet_cmd_cat = CAT     $@
229cmd_cat = cat $(filter-out $(PHONY), $^) > $@
230
231quiet_cmd_copy = COPY    $@
232      cmd_copy = cp $< $@
233
234ifneq ($(CONFIG_SPL_MULTI_DTB_FIT),y)
235FINAL_DTB_CONTAINER = $(obj)/$(SPL_BIN).dtb
236else ifeq ($(CONFIG_SPL_MULTI_DTB_FIT_LZO),y)
237FINAL_DTB_CONTAINER = $(obj)/$(SPL_BIN).multidtb.fit.lzo
238else ifeq ($(CONFIG_SPL_MULTI_DTB_FIT_GZIP),y)
239FINAL_DTB_CONTAINER = $(obj)/$(SPL_BIN).multidtb.fit.gz
240else
241FINAL_DTB_CONTAINER = $(obj)/$(SPL_BIN).multidtb.fit
242endif
243
244
245ifeq ($(CONFIG_$(SPL_TPL_)OF_CONTROL)$(CONFIG_OF_SEPARATE)$(CONFIG_$(SPL_TPL_)OF_PLATDATA),yy)
246$(obj)/$(SPL_BIN)-dtb.bin: $(obj)/$(SPL_BIN)-nodtb.bin \
247		$(if $(CONFIG_SPL_SEPARATE_BSS),,$(obj)/$(SPL_BIN)-pad.bin) \
248		$(FINAL_DTB_CONTAINER)  FORCE
249	$(call if_changed,cat)
250
251$(obj)/$(SPL_BIN).bin: $(obj)/$(SPL_BIN)-dtb.bin FORCE
252	$(call if_changed,copy)
253else
254$(obj)/$(SPL_BIN).bin: $(obj)/$(SPL_BIN)-nodtb.bin FORCE
255	$(call if_changed,copy)
256endif
257
258# Create a file that pads from the end of u-boot-spl-nodtb.bin to bss_end
259$(obj)/$(SPL_BIN)-pad.bin: $(obj)/$(SPL_BIN)
260	@bss_size_str=$(shell $(NM) $< | awk 'BEGIN {size = 0} /__bss_size/ {size = $$1} END {print "ibase=16; " toupper(size)}' | bc); \
261	dd if=/dev/zero of=$@ bs=1 count=$${bss_size_str} 2>/dev/null;
262
263$(obj)/$(SPL_BIN).dtb: dts/dt-spl.dtb FORCE
264	$(call if_changed,copy)
265
266pythonpath = PYTHONPATH=scripts/dtc/pylibfdt
267
268quiet_cmd_dtocc = DTOC C  $@
269cmd_dtocc = $(pythonpath) $(srctree)/tools/dtoc/dtoc -d $(obj)/$(SPL_BIN).dtb -o $@ platdata
270
271quiet_cmd_dtoch = DTOC H  $@
272cmd_dtoch = $(pythonpath) $(srctree)/tools/dtoc/dtoc -d $(obj)/$(SPL_BIN).dtb -o $@ struct
273
274quiet_cmd_plat = PLAT    $@
275cmd_plat = $(CC) $(c_flags) -c $< -o $@
276
277$(obj)/dts/dt-platdata.o: $(obj)/dts/dt-platdata.c \
278		include/generated/dt-structs-gen.h
279	$(call if_changed,plat)
280
281PHONY += dts_dir
282dts_dir:
283	$(shell [ -d $(obj)/dts ] || mkdir -p $(obj)/dts)
284
285include/generated/dt-structs-gen.h: $(obj)/$(SPL_BIN).dtb dts_dir FORCE
286	$(call if_changed,dtoch)
287
288$(obj)/dts/dt-platdata.c: $(obj)/$(SPL_BIN).dtb dts_dir FORCE
289	$(call if_changed,dtocc)
290
291ifdef CONFIG_SAMSUNG
292ifdef CONFIG_VAR_SIZE_SPL
293VAR_SIZE_PARAM = --vs
294else
295VAR_SIZE_PARAM =
296endif
297$(obj)/$(BOARD)-spl.bin: $(obj)/u-boot-spl.bin
298	$(if $(wildcard $(objtree)/spl/board/samsung/$(BOARD)/tools/mk$(BOARD)spl),\
299	$(objtree)/spl/board/samsung/$(BOARD)/tools/mk$(BOARD)spl,\
300	$(objtree)/tools/mkexynosspl) $(VAR_SIZE_PARAM) $< $@
301endif
302
303quiet_cmd_objcopy = OBJCOPY $@
304cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
305
306OBJCOPYFLAGS_$(SPL_BIN)-nodtb.bin = $(SPL_OBJCFLAGS) -O binary \
307		$(if $(CONFIG_SPL_X86_16BIT_INIT),-R .start16 -R .resetvec)
308
309$(obj)/$(SPL_BIN)-nodtb.bin: $(obj)/$(SPL_BIN) FORCE
310	$(call if_changed,objcopy)
311
312OBJCOPYFLAGS_u-boot-x86-16bit-spl.bin := -O binary -j .start16 -j .resetvec
313$(obj)/u-boot-x86-16bit-spl.bin: $(obj)/u-boot-spl FORCE
314	$(call if_changed,objcopy)
315
316LDFLAGS_$(SPL_BIN) += -T u-boot-spl.lds $(LDFLAGS_FINAL)
317
318# Avoid 'Not enough room for program headers' error on binutils 2.28 onwards.
319LDFLAGS_$(SPL_BIN) += $(call ld-option, --no-dynamic-linker)
320
321# First try the best-match (i.e. SPL_TEXT_BASE for SPL, TPL_TEXT_BASE for TPL)
322ifneq ($(CONFIG_$(SPL_TPL_)TEXT_BASE),)
323LDFLAGS_$(SPL_BIN) += -Ttext $(CONFIG_$(SPL_TPL_)TEXT_BASE)
324else
325# And then fall back to just testing for SPL_TEXT_BASE, even if in TPL mode
326ifneq ($(CONFIG_SPL_TEXT_BASE),)
327LDFLAGS_$(SPL_BIN) += -Ttext $(CONFIG_SPL_TEXT_BASE)
328endif
329endif
330
331ifdef CONFIG_TARGET_SOCFPGA_ARRIA10
332MKIMAGEFLAGS_$(SPL_BIN).sfp = -T socfpgaimage_v1
333else
334MKIMAGEFLAGS_$(SPL_BIN).sfp = -T socfpgaimage
335endif
336$(obj)/$(SPL_BIN).sfp: $(obj)/$(SPL_BIN).bin FORCE
337	$(call if_changed,mkimage)
338
339quiet_cmd_mksunxiboot = MKSUNXI $@
340cmd_mksunxiboot = $(objtree)/tools/mksunxiboot \
341			--default-dt $(CONFIG_DEFAULT_DEVICE_TREE) $< $@
342$(obj)/sunxi-spl.bin: $(obj)/$(SPL_BIN).bin FORCE
343	$(call if_changed,mksunxiboot)
344
345quiet_cmd_sunxi_spl_image_builder = SUNXI_SPL_IMAGE_BUILDER $@
346cmd_sunxi_spl_image_builder = $(objtree)/tools/sunxi-spl-image-builder \
347				-c $(CONFIG_NAND_SUNXI_SPL_ECC_STRENGTH)/$(CONFIG_NAND_SUNXI_SPL_ECC_SIZE) \
348				-p $(CONFIG_SYS_NAND_PAGE_SIZE) \
349				-o $(CONFIG_SYS_NAND_OOBSIZE) \
350				-u $(CONFIG_NAND_SUNXI_SPL_USABLE_PAGE_SIZE) \
351				-e $(CONFIG_SYS_NAND_BLOCK_SIZE) \
352				-s -b $< $@
353$(obj)/sunxi-spl-with-ecc.bin: $(obj)/sunxi-spl.bin
354	$(call if_changed,sunxi_spl_image_builder)
355
356
357# MediaTek's specific SPL build
358MKIMAGEFLAGS_u-boot-spl-mtk.bin = -T mtk_image \
359	-a $(CONFIG_SPL_TEXT_BASE) -e $(CONFIG_SPL_TEXT_BASE) \
360	-n "$(patsubst "%",%,$(CONFIG_MTK_BROM_HEADER_INFO))"
361
362$(obj)/u-boot-spl-mtk.bin: $(obj)/u-boot-spl.bin FORCE
363	$(call if_changed,mkimage)
364
365# Rule to link u-boot-spl
366# May be overridden by arch/$(ARCH)/config.mk
367quiet_cmd_u-boot-spl ?= LD      $@
368      cmd_u-boot-spl ?= (cd $(obj) && $(LD) $(LDFLAGS) $(LDFLAGS_$(@F)) \
369		       $(patsubst $(obj)/%,%,$(u-boot-spl-init)) --start-group \
370		       $(patsubst $(obj)/%,%,$(u-boot-spl-main))  \
371		       $(patsubst $(obj)/%,%,$(u-boot-spl-platdata)) \
372		       --end-group \
373		       $(PLATFORM_LIBS) -Map $(SPL_BIN).map -o $(SPL_BIN))
374
375$(obj)/$(SPL_BIN): $(u-boot-spl-platdata) $(u-boot-spl-init) \
376		$(u-boot-spl-main) $(obj)/u-boot-spl.lds FORCE
377	$(call if_changed,u-boot-spl)
378
379$(sort $(u-boot-spl-init) $(u-boot-spl-main)): $(u-boot-spl-dirs) ;
380
381PHONY += $(u-boot-spl-dirs)
382$(u-boot-spl-dirs): $(u-boot-spl-platdata)
383	$(Q)$(MAKE) $(build)=$@
384
385quiet_cmd_cpp_lds = LDS     $@
386cmd_cpp_lds = $(CPP) -Wp,-MD,$(depfile) $(cpp_flags) $(LDPPFLAGS) -ansi \
387		-D__ASSEMBLY__ -x assembler-with-cpp -std=c99 -P -o $@ $<
388
389$(obj)/u-boot-spl.lds: $(LDSCRIPT) FORCE
390	$(call if_changed_dep,cpp_lds)
391
392# read all saved command lines
393
394targets := $(wildcard $(sort $(targets)))
395cmd_files := $(wildcard $(obj)/.*.cmd $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
396
397ifneq ($(cmd_files),)
398  $(cmd_files): ;	# Do not try to update included dependency files
399  include $(cmd_files)
400endif
401
402PHONY += FORCE
403FORCE:
404
405PHONY += dtbs
406dtbs:
407	$(Q)$(MAKE) $(build)=dts dtbs
408
409# Declare the contents of the .PHONY variable as phony.  We keep that
410# information in a variable so we can use it in if_changed and friends.
411.PHONY: $(PHONY)
412
413SHRUNK_ARCH_DTB = $(patsubst %,$(obj)/dts/%.dtb,$(subst ",,$(CONFIG_SPL_OF_LIST)))
414.SECONDEXPANSION:
415$(SHRUNK_ARCH_DTB): $$(patsubst $(obj)/dts/%, arch/$(ARCH)/dts/%, $$@)
416	$(call if_changed,fdtgrep)
417
418MKIMAGEFLAGS_$(SPL_BIN).multidtb.fit = -f auto -A $(ARCH) -T firmware -C none -O u-boot \
419	-n "Multi DTB fit image for $(SPL_BIN)" -E \
420	$(patsubst %,-b %,$(SHRUNK_ARCH_DTB))
421
422$(obj)/$(SPL_BIN).multidtb.fit: /dev/null $(SHRUNK_ARCH_DTB) FORCE
423	$(call if_changed,mkimage)
424ifneq ($(SOURCE_DATE_EPOCH),)
425	touch -d @$(SOURCE_DATE_EPOCH) $(obj)/$(SPL_BIN).multidtb.fit
426	chmod 0600 $(obj)/$(SPL_BIN).multidtb.fit
427endif
428
429$(obj)/$(SPL_BIN).multidtb.fit.gz: $(obj)/$(SPL_BIN).multidtb.fit
430	@gzip -kf9 $< > $@
431
432$(obj)/$(SPL_BIN).multidtb.fit.lzo: $(obj)/$(SPL_BIN).multidtb.fit
433	@lzop -f9 $< > $@
434
435ifdef CONFIG_ARCH_K3
436tispl.bin: $(obj)/u-boot-spl-nodtb.bin $(SHRUNK_ARCH_DTB) $(SPL_ITS) FORCE
437	$(call if_changed,mkfitimage)
438endif
439