blob: b22eaf900d866f096a4d705b1278fa74be8c382b [file] [log] [blame]
Juan Castilloa3487d12015-08-18 14:23:04 +01001#
2# Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7# Redistributions of source code must retain the above copyright notice, this
8# list of conditions and the following disclaimer.
9#
10# Redistributions in binary form must reproduce the above copyright notice,
11# this list of conditions and the following disclaimer in the documentation
12# and/or other materials provided with the distribution.
13#
14# Neither the name of ARM nor the names of its contributors may be used
15# to endorse or promote products derived from this software without specific
16# prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
Yatharth Kochar115352c2015-10-02 13:58:52 +010031# This table is used in converting lower case to upper case.
32uppercase_table:=a,A b,B c,C d,D e,E f,F g,G h,H i,I j,J k,K l,L m,M n,N o,O p,P q,Q r,R s,S t,T u,U v,V w,W x,X y,Y z,Z
33
34# Internal macro used for converting lower case to upper case.
35# $(1) = upper case table
36# $(2) = String to convert
37define uppercase_internal
38$(if $(1),$$(subst $(firstword $(1)),$(call uppercase_internal,$(wordlist 2,$(words $(1)),$(1)),$(2))),$(2))
39endef
40
41# A macro for converting a string to upper case
42# $(1) = String to convert
43define uppercase
44$(eval uppercase_result:=$(call uppercase_internal,$(uppercase_table),$(1)))$(uppercase_result)
45endef
46
Juan Castilloa3487d12015-08-18 14:23:04 +010047# Convenience function for adding build definitions
48# $(eval $(call add_define,FOO)) will have:
49# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
50define add_define
51 DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),)
52endef
53
54# Convenience function for verifying option has a boolean value
55# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
56define assert_boolean
57 $(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean))
58endef
59
60# IMG_LINKERFILE defines the linker script corresponding to a BL stage
61# $(1) = BL stage (2, 30, 31, 32, 33)
62define IMG_LINKERFILE
63 ${BUILD_DIR}/bl$(1).ld
64endef
65
66# IMG_MAPFILE defines the output file describing the memory map corresponding
67# to a BL stage
68# $(1) = BL stage (2, 30, 31, 32, 33)
69define IMG_MAPFILE
70 ${BUILD_DIR}/bl$(1).map
71endef
72
73# IMG_ELF defines the elf file corresponding to a BL stage
74# $(1) = BL stage (2, 30, 31, 32, 33)
75define IMG_ELF
76 ${BUILD_DIR}/bl$(1).elf
77endef
78
79# IMG_DUMP defines the symbols dump file corresponding to a BL stage
80# $(1) = BL stage (2, 30, 31, 32, 33)
81define IMG_DUMP
82 ${BUILD_DIR}/bl$(1).dump
83endef
84
85# IMG_BIN defines the default image file corresponding to a BL stage
86# $(1) = BL stage (2, 30, 31, 32, 33)
87define IMG_BIN
88 ${BUILD_PLAT}/bl$(1).bin
89endef
90
91# FIP_ADD_PAYLOAD appends the command line arguments required by the FIP tool
92# to package a new payload. Optionally, it adds the dependency on this payload
93# $(1) = payload filename (i.e. bl31.bin)
94# $(2) = command line option for the specified payload (i.e. --bl31)
95# $(3) = fip target dependency (optional) (i.e. bl31)
96define FIP_ADD_PAYLOAD
97 $(eval FIP_ARGS += $(2) $(1))
98 $(eval $(if $(3),FIP_DEPS += $(3)))
99endef
100
Yatharth Kochard1a93432015-10-12 12:33:47 +0100101# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation
Juan Castilloa3487d12015-08-18 14:23:04 +0100102# $(1) = parameter filename
103# $(2) = cert_create command line option for the specified parameter
104# $(3) = input parameter (false if empty)
105define CERT_ADD_CMD_OPT
106 $(eval $(if $(3),CRT_DEPS += $(1)))
107 $(eval CRT_ARGS += $(2) $(1))
108endef
109
110# FIP_ADD_IMG allows the platform to specify an image to be packed in the FIP
111# using a build option. It also adds a dependency on the image file, aborting
112# the build if the file does not exist.
Juan Castilloa72b6472015-12-10 15:49:17 +0000113# $(1) = build option to specify the image filename (SCP_BL2, BL33, etc)
114# $(2) = command line option for the fip_create tool (scp_bl2, bl33, etc)
Juan Castilloa3487d12015-08-18 14:23:04 +0100115# Example:
116# $(eval $(call FIP_ADD_IMG,BL33,--bl33))
117define FIP_ADD_IMG
118 CRT_DEPS += check_$(1)
119 FIP_DEPS += check_$(1)
120 $(call FIP_ADD_PAYLOAD,$(value $(1)),$(2))
121
122check_$(1):
123 $$(if $(value $(1)),,$$(error "Platform '${PLAT}' requires $(1). Please set $(1) to point to the right file"))
124endef
125
Yatharth Kochard1a93432015-10-12 12:33:47 +0100126# FWU_FIP_ADD_PAYLOAD appends the command line arguments required by the FIP tool
127# to package a new FWU payload. Optionally, it adds the dependency on this payload
128# $(1) = payload filename (e.g. ns_bl2u.bin)
Juan Castillo8e04dec2016-01-05 11:55:36 +0000129# $(2) = command line option for the specified payload (e.g. --fwu)
Yatharth Kochard1a93432015-10-12 12:33:47 +0100130# $(3) = fip target dependency (optional) (e.g. ns_bl2u)
131define FWU_FIP_ADD_PAYLOAD
132 $(eval $(if $(3),FWU_FIP_DEPS += $(3)))
133 $(eval FWU_FIP_ARGS += $(2) $(1))
134endef
135
136# FWU_CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation
137# $(1) = parameter filename
138# $(2) = cert_create command line option for the specified parameter
139# $(3) = input parameter (false if empty)
140define FWU_CERT_ADD_CMD_OPT
141 $(eval $(if $(3),FWU_CRT_DEPS += $(1)))
142 $(eval FWU_CRT_ARGS += $(2) $(1))
143endef
144
145# FWU_FIP_ADD_IMG allows the platform to pack a binary image in the FWU FIP
146# $(1) build option to specify the image filename (BL2U, NS_BL2U, etc)
147# $(2) command line option for the fip_create tool (bl2u, ns_bl2u, etc)
148# Example:
149# $(eval $(call FWU_FIP_ADD_IMG,BL2U,--bl2u))
150define FWU_FIP_ADD_IMG
151 FWU_CRT_DEPS += check_$(1)
152 FWU_FIP_DEPS += check_$(1)
153 $(call FWU_FIP_ADD_PAYLOAD,$(value $(1)),$(2))
154
155check_$(1):
156 $$(if $(value $(1)),,$$(error "Platform '${PLAT}' requires $(1). Please set $(1) to point to the right file"))
157endef
Juan Castilloa3487d12015-08-18 14:23:04 +0100158
159################################################################################
160# Auxiliary macros to build TF images from sources
161################################################################################
162
Juan Castillo396644b2015-10-22 11:34:44 +0100163# If no goal is specified in the command line, .DEFAULT_GOAL is used.
164# .DEFAULT_GOAL is defined in the main Makefile before including this file.
165ifeq ($(MAKECMDGOALS),)
166MAKECMDGOALS := $(.DEFAULT_GOAL)
167endif
168
Juan Castilloa3487d12015-08-18 14:23:04 +0100169define match_goals
170$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS))))
171endef
172
173# List of rules that involve building things
Yatharth Kocharb1c2fe02015-10-14 15:27:24 +0100174BUILD_TARGETS := all bl1 bl2 bl2u bl31 bl32 certificates fip
Juan Castilloa3487d12015-08-18 14:23:04 +0100175
176# Does the list of goals specified on the command line include a build target?
177ifneq ($(call match_goals,${BUILD_TARGETS}),)
178IS_ANYTHING_TO_BUILD := 1
179endif
180
181
182# MAKE_C builds a C source file and generates the dependency file
183# $(1) = output directory
184# $(2) = source file (%.c)
Yatharth Kochar115352c2015-10-02 13:58:52 +0100185# $(3) = BL stage (2, 2u, 30, 31, 32, 33)
Juan Castilloa3487d12015-08-18 14:23:04 +0100186define MAKE_C
187
188$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
189$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ)))
Yatharth Kochar115352c2015-10-02 13:58:52 +0100190$(eval IMAGE := IMAGE_BL$(call uppercase,$(3)))
Juan Castilloa3487d12015-08-18 14:23:04 +0100191
192$(OBJ): $(2)
193 @echo " CC $$<"
Yatharth Kochar115352c2015-10-02 13:58:52 +0100194 $$(Q)$$(CC) $$(CFLAGS) -D$(IMAGE) -c $$< -o $$@
Juan Castilloa3487d12015-08-18 14:23:04 +0100195
196
197$(PREREQUISITES): $(2)
198 @echo " DEPS $$@"
199 @mkdir -p $(1)
200 $$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$<
201
202ifdef IS_ANYTHING_TO_BUILD
203-include $(PREREQUISITES)
204endif
205
206endef
207
208
209# MAKE_S builds an assembly source file and generates the dependency file
210# $(1) = output directory
211# $(2) = assembly file (%.S)
Yatharth Kochar115352c2015-10-02 13:58:52 +0100212# $(3) = BL stage (2, 2u, 30, 31, 32, 33)
Juan Castilloa3487d12015-08-18 14:23:04 +0100213define MAKE_S
214
215$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
216$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ)))
Yatharth Kochar115352c2015-10-02 13:58:52 +0100217$(eval IMAGE := IMAGE_BL$(call uppercase,$(3)))
Juan Castilloa3487d12015-08-18 14:23:04 +0100218
219$(OBJ): $(2)
220 @echo " AS $$<"
Yatharth Kochar115352c2015-10-02 13:58:52 +0100221 $$(Q)$$(AS) $$(ASFLAGS) -D$(IMAGE) -c $$< -o $$@
Juan Castilloa3487d12015-08-18 14:23:04 +0100222
223$(PREREQUISITES): $(2)
224 @echo " DEPS $$@"
225 @mkdir -p $(1)
226 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$<
227
228ifdef IS_ANYTHING_TO_BUILD
229-include $(PREREQUISITES)
230endif
231
232endef
233
234
235# MAKE_LD generate the linker script using the C preprocessor
236# $(1) = output linker script
237# $(2) = input template
238define MAKE_LD
239
240$(eval PREREQUISITES := $(1).d)
241
242$(1): $(2)
243 @echo " PP $$<"
244 $$(Q)$$(AS) $$(ASFLAGS) -P -E -D__LINKER__ -o $$@ $$<
245
246$(PREREQUISITES): $(2)
247 @echo " DEPS $$@"
248 @mkdir -p $$(dir $$@)
249 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(1) -MF $$@ $$<
250
251ifdef IS_ANYTHING_TO_BUILD
252-include $(PREREQUISITES)
253endif
254
255endef
256
257
258# MAKE_OBJS builds both C and assembly source files
259# $(1) = output directory
260# $(2) = list of source files (both C and assembly)
261# $(3) = BL stage (2, 30, 31, 32, 33)
262define MAKE_OBJS
263 $(eval C_OBJS := $(filter %.c,$(2)))
264 $(eval REMAIN := $(filter-out %.c,$(2)))
265 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
266
267 $(eval S_OBJS := $(filter %.S,$(REMAIN)))
268 $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
269 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
270
271 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
272endef
273
274
275# NOTE: The line continuation '\' is required in the next define otherwise we
276# end up with a line-feed characer at the end of the last c filename.
277# Also bare this issue in mind if extending the list of supported filetypes.
278define SOURCES_TO_OBJS
279 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
280 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
281endef
282
283
284# MAKE_TOOL_ARGS macro defines the command line arguments for the FIP tool for
285# each BL image. Arguments:
286# $(1) = BL stage (2, 30, 31, 32, 33)
287# $(2) = Binary file
Juan Castillo8e04dec2016-01-05 11:55:36 +0000288# $(3) = FIP command line option (if empty, image will not be included in the FIP)
Juan Castilloa3487d12015-08-18 14:23:04 +0100289define MAKE_TOOL_ARGS
Juan Castillo8e04dec2016-01-05 11:55:36 +0000290 $(if $(3),$(eval $(call FIP_ADD_PAYLOAD,$(2),--$(3),bl$(1))))
Juan Castilloa3487d12015-08-18 14:23:04 +0100291endef
292
Patrick Georgi8a10e342016-01-28 14:46:18 +0100293# Allow overriding the timestamp, for example for reproducible builds, or to
294# synchronize timestamps across multiple projects.
295# This must be set to a C string (including quotes where applicable).
296BUILD_MESSAGE_TIMESTAMP ?= __TIME__", "__DATE__
Juan Castilloa3487d12015-08-18 14:23:04 +0100297
298# MAKE_BL macro defines the targets and options to build each BL image.
299# Arguments:
Yatharth Kochar115352c2015-10-02 13:58:52 +0100300# $(1) = BL stage (2, 2u, 30, 31, 32, 33)
Juan Castillo8e04dec2016-01-05 11:55:36 +0000301# $(2) = FIP command line option (if empty, image will not be included in the FIP)
Juan Castilloa3487d12015-08-18 14:23:04 +0100302define MAKE_BL
303 $(eval BUILD_DIR := ${BUILD_PLAT}/bl$(1))
Yatharth Kochar115352c2015-10-02 13:58:52 +0100304 $(eval BL_SOURCES := $(BL$(call uppercase,$(1))_SOURCES))
305 $(eval SOURCES := $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES))
Juan Castilloa3487d12015-08-18 14:23:04 +0100306 $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
307 $(eval LINKERFILE := $(call IMG_LINKERFILE,$(1)))
308 $(eval MAPFILE := $(call IMG_MAPFILE,$(1)))
309 $(eval ELF := $(call IMG_ELF,$(1)))
310 $(eval DUMP := $(call IMG_DUMP,$(1)))
311 $(eval BIN := $(call IMG_BIN,$(1)))
Yatharth Kochar115352c2015-10-02 13:58:52 +0100312 $(eval BL_LINKERFILE := $(BL$(call uppercase,$(1))_LINKERFILE))
Juan Castilloa3487d12015-08-18 14:23:04 +0100313
314 $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
Yatharth Kochar115352c2015-10-02 13:58:52 +0100315 $(eval $(call MAKE_LD,$(LINKERFILE),$(BL_LINKERFILE)))
Juan Castilloa3487d12015-08-18 14:23:04 +0100316
317$(BUILD_DIR):
318 $$(Q)mkdir -p "$$@"
319
320$(ELF): $(OBJS) $(LINKERFILE)
321 @echo " LD $$@"
Patrick Georgi8a10e342016-01-28 14:46:18 +0100322 @echo 'const char build_message[] = "Built : "$(BUILD_MESSAGE_TIMESTAMP); \
Juan Castilloa3487d12015-08-18 14:23:04 +0100323 const char version_string[] = "${VERSION_STRING}";' | \
324 $$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o
325 $$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \
326 $(BUILD_DIR)/build_message.o $(OBJS)
327
328$(DUMP): $(ELF)
329 @echo " OD $$@"
330 $${Q}$${OD} -dx $$< > $$@
331
332$(BIN): $(ELF)
333 @echo " BIN $$@"
334 $$(Q)$$(OC) -O binary $$< $$@
335 @echo
336 @echo "Built $$@ successfully"
337 @echo
338
339.PHONY: bl$(1)
340bl$(1): $(BUILD_DIR) $(BIN) $(DUMP)
341
342all: bl$(1)
343
344$(eval $(call MAKE_TOOL_ARGS,$(1),$(BIN),$(2)))
345
346endef
347