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 invokation 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 (BL30, BL33, etc) 114# $(2) = command line option for the fip_create tool (bl30, 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 127################################################################################ 128# Auxiliary macros to build TF images from sources 129################################################################################ 130 131# If no goal is specified in the command line, .DEFAULT_GOAL is used. 132# .DEFAULT_GOAL is defined in the main Makefile before including this file. 133ifeq ($(MAKECMDGOALS),) 134MAKECMDGOALS := $(.DEFAULT_GOAL) 135endif 136 137define match_goals 138$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS)))) 139endef 140 141# List of rules that involve building things 142BUILD_TARGETS := all bl1 bl2 bl31 bl32 certificates fip 143 144# Does the list of goals specified on the command line include a build target? 145ifneq ($(call match_goals,${BUILD_TARGETS}),) 146IS_ANYTHING_TO_BUILD := 1 147endif 148 149 150# MAKE_C builds a C source file and generates the dependency file 151# $(1) = output directory 152# $(2) = source file (%.c) 153# $(3) = BL stage (2, 2u, 30, 31, 32, 33) 154define MAKE_C 155 156$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 157$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 158$(eval IMAGE := IMAGE_BL$(call uppercase,$(3))) 159 160$(OBJ): $(2) 161 @echo " CC $$<" 162 $$(Q)$$(CC) $$(CFLAGS) -D$(IMAGE) -c $$< -o $$@ 163 164 165$(PREREQUISITES): $(2) 166 @echo " DEPS $$@" 167 @mkdir -p $(1) 168 $$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$< 169 170ifdef IS_ANYTHING_TO_BUILD 171-include $(PREREQUISITES) 172endif 173 174endef 175 176 177# MAKE_S builds an assembly source file and generates the dependency file 178# $(1) = output directory 179# $(2) = assembly file (%.S) 180# $(3) = BL stage (2, 2u, 30, 31, 32, 33) 181define MAKE_S 182 183$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 184$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 185$(eval IMAGE := IMAGE_BL$(call uppercase,$(3))) 186 187$(OBJ): $(2) 188 @echo " AS $$<" 189 $$(Q)$$(AS) $$(ASFLAGS) -D$(IMAGE) -c $$< -o $$@ 190 191$(PREREQUISITES): $(2) 192 @echo " DEPS $$@" 193 @mkdir -p $(1) 194 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$< 195 196ifdef IS_ANYTHING_TO_BUILD 197-include $(PREREQUISITES) 198endif 199 200endef 201 202 203# MAKE_LD generate the linker script using the C preprocessor 204# $(1) = output linker script 205# $(2) = input template 206define MAKE_LD 207 208$(eval PREREQUISITES := $(1).d) 209 210$(1): $(2) 211 @echo " PP $$<" 212 $$(Q)$$(AS) $$(ASFLAGS) -P -E -D__LINKER__ -o $$@ $$< 213 214$(PREREQUISITES): $(2) 215 @echo " DEPS $$@" 216 @mkdir -p $$(dir $$@) 217 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(1) -MF $$@ $$< 218 219ifdef IS_ANYTHING_TO_BUILD 220-include $(PREREQUISITES) 221endif 222 223endef 224 225 226# MAKE_OBJS builds both C and assembly source files 227# $(1) = output directory 228# $(2) = list of source files (both C and assembly) 229# $(3) = BL stage (2, 30, 31, 32, 33) 230define MAKE_OBJS 231 $(eval C_OBJS := $(filter %.c,$(2))) 232 $(eval REMAIN := $(filter-out %.c,$(2))) 233 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3)))) 234 235 $(eval S_OBJS := $(filter %.S,$(REMAIN))) 236 $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 237 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3)))) 238 239 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 240endef 241 242 243# NOTE: The line continuation '\' is required in the next define otherwise we 244# end up with a line-feed characer at the end of the last c filename. 245# Also bare this issue in mind if extending the list of supported filetypes. 246define SOURCES_TO_OBJS 247 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ 248 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) 249endef 250 251 252# MAKE_TOOL_ARGS macro defines the command line arguments for the FIP tool for 253# each BL image. Arguments: 254# $(1) = BL stage (2, 30, 31, 32, 33) 255# $(2) = Binary file 256# $(3) = In FIP (false if empty) 257define MAKE_TOOL_ARGS 258 $(if $(3),$(eval $(call FIP_ADD_PAYLOAD,$(2),--bl$(1),bl$(1)))) 259endef 260 261 262# MAKE_BL macro defines the targets and options to build each BL image. 263# Arguments: 264# $(1) = BL stage (2, 2u, 30, 31, 32, 33) 265# $(2) = In FIP (false if empty) 266define MAKE_BL 267 $(eval BUILD_DIR := ${BUILD_PLAT}/bl$(1)) 268 $(eval BL_SOURCES := $(BL$(call uppercase,$(1))_SOURCES)) 269 $(eval SOURCES := $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)) 270 $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 271 $(eval LINKERFILE := $(call IMG_LINKERFILE,$(1))) 272 $(eval MAPFILE := $(call IMG_MAPFILE,$(1))) 273 $(eval ELF := $(call IMG_ELF,$(1))) 274 $(eval DUMP := $(call IMG_DUMP,$(1))) 275 $(eval BIN := $(call IMG_BIN,$(1))) 276 $(eval BL_LINKERFILE := $(BL$(call uppercase,$(1))_LINKERFILE)) 277 278 $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1))) 279 $(eval $(call MAKE_LD,$(LINKERFILE),$(BL_LINKERFILE))) 280 281$(BUILD_DIR): 282 $$(Q)mkdir -p "$$@" 283 284$(ELF): $(OBJS) $(LINKERFILE) 285 @echo " LD $$@" 286 @echo 'const char build_message[] = "Built : "__TIME__", "__DATE__; \ 287 const char version_string[] = "${VERSION_STRING}";' | \ 288 $$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o 289 $$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \ 290 $(BUILD_DIR)/build_message.o $(OBJS) 291 292$(DUMP): $(ELF) 293 @echo " OD $$@" 294 $${Q}$${OD} -dx $$< > $$@ 295 296$(BIN): $(ELF) 297 @echo " BIN $$@" 298 $$(Q)$$(OC) -O binary $$< $$@ 299 @echo 300 @echo "Built $$@ successfully" 301 @echo 302 303.PHONY: bl$(1) 304bl$(1): $(BUILD_DIR) $(BIN) $(DUMP) 305 306all: bl$(1) 307 308$(eval $(call MAKE_TOOL_ARGS,$(1),$(BIN),$(2))) 309 310endef 311 312