1# 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 31# 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 47# 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 101# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation 102# $(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. 113# $(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) 115# 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 126# 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) 129# $(2) = command line option for the specified payload (e.g. --fwu) 130# $(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 158 159################################################################################ 160# Auxiliary macros to build TF images from sources 161################################################################################ 162 163# 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 169define match_goals 170$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS)))) 171endef 172 173# List of rules that involve building things 174BUILD_TARGETS := all bl1 bl2 bl2u bl31 bl32 certificates fip 175 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) 185# $(3) = BL stage (2, 2u, 30, 31, 32, 33) 186define MAKE_C 187 188$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 189$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 190$(eval IMAGE := IMAGE_BL$(call uppercase,$(3))) 191 192$(OBJ): $(2) 193 @echo " CC $$<" 194 $$(Q)$$(CC) $$(CFLAGS) -D$(IMAGE) -c $$< -o $$@ 195 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) 212# $(3) = BL stage (2, 2u, 30, 31, 32, 33) 213define MAKE_S 214 215$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 216$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 217$(eval IMAGE := IMAGE_BL$(call uppercase,$(3))) 218 219$(OBJ): $(2) 220 @echo " AS $$<" 221 $$(Q)$$(AS) $$(ASFLAGS) -D$(IMAGE) -c $$< -o $$@ 222 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 288# $(3) = FIP command line option (if empty, image will not be included in the FIP) 289define MAKE_TOOL_ARGS 290 $(if $(3),$(eval $(call FIP_ADD_PAYLOAD,$(2),--$(3),bl$(1)))) 291endef 292 293 294# MAKE_BL macro defines the targets and options to build each BL image. 295# Arguments: 296# $(1) = BL stage (2, 2u, 30, 31, 32, 33) 297# $(2) = FIP command line option (if empty, image will not be included in the FIP) 298define MAKE_BL 299 $(eval BUILD_DIR := ${BUILD_PLAT}/bl$(1)) 300 $(eval BL_SOURCES := $(BL$(call uppercase,$(1))_SOURCES)) 301 $(eval SOURCES := $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)) 302 $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 303 $(eval LINKERFILE := $(call IMG_LINKERFILE,$(1))) 304 $(eval MAPFILE := $(call IMG_MAPFILE,$(1))) 305 $(eval ELF := $(call IMG_ELF,$(1))) 306 $(eval DUMP := $(call IMG_DUMP,$(1))) 307 $(eval BIN := $(call IMG_BIN,$(1))) 308 $(eval BL_LINKERFILE := $(BL$(call uppercase,$(1))_LINKERFILE)) 309 310 $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1))) 311 $(eval $(call MAKE_LD,$(LINKERFILE),$(BL_LINKERFILE))) 312 313$(BUILD_DIR): 314 $$(Q)mkdir -p "$$@" 315 316$(ELF): $(OBJS) $(LINKERFILE) 317 @echo " LD $$@" 318 @echo 'const char build_message[] = "Built : "__TIME__", "__DATE__; \ 319 const char version_string[] = "${VERSION_STRING}";' | \ 320 $$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o 321 $$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \ 322 $(BUILD_DIR)/build_message.o $(OBJS) 323 324$(DUMP): $(ELF) 325 @echo " OD $$@" 326 $${Q}$${OD} -dx $$< > $$@ 327 328$(BIN): $(ELF) 329 @echo " BIN $$@" 330 $$(Q)$$(OC) -O binary $$< $$@ 331 @echo 332 @echo "Built $$@ successfully" 333 @echo 334 335.PHONY: bl$(1) 336bl$(1): $(BUILD_DIR) $(BIN) $(DUMP) 337 338all: bl$(1) 339 340$(eval $(call MAKE_TOOL_ARGS,$(1),$(BIN),$(2))) 341 342endef 343 344