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# Convenience function for adding build definitions 32# $(eval $(call add_define,FOO)) will have: 33# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise 34define add_define 35 DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),) 36endef 37 38# Convenience function for verifying option has a boolean value 39# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1 40define assert_boolean 41 $(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean)) 42endef 43 44# IMG_LINKERFILE defines the linker script corresponding to a BL stage 45# $(1) = BL stage (2, 30, 31, 32, 33) 46define IMG_LINKERFILE 47 ${BUILD_DIR}/bl$(1).ld 48endef 49 50# IMG_MAPFILE defines the output file describing the memory map corresponding 51# to a BL stage 52# $(1) = BL stage (2, 30, 31, 32, 33) 53define IMG_MAPFILE 54 ${BUILD_DIR}/bl$(1).map 55endef 56 57# IMG_ELF defines the elf file corresponding to a BL stage 58# $(1) = BL stage (2, 30, 31, 32, 33) 59define IMG_ELF 60 ${BUILD_DIR}/bl$(1).elf 61endef 62 63# IMG_DUMP defines the symbols dump file corresponding to a BL stage 64# $(1) = BL stage (2, 30, 31, 32, 33) 65define IMG_DUMP 66 ${BUILD_DIR}/bl$(1).dump 67endef 68 69# IMG_BIN defines the default image file corresponding to a BL stage 70# $(1) = BL stage (2, 30, 31, 32, 33) 71define IMG_BIN 72 ${BUILD_PLAT}/bl$(1).bin 73endef 74 75# FIP_ADD_PAYLOAD appends the command line arguments required by the FIP tool 76# to package a new payload. Optionally, it adds the dependency on this payload 77# $(1) = payload filename (i.e. bl31.bin) 78# $(2) = command line option for the specified payload (i.e. --bl31) 79# $(3) = fip target dependency (optional) (i.e. bl31) 80define FIP_ADD_PAYLOAD 81 $(eval FIP_ARGS += $(2) $(1)) 82 $(eval $(if $(3),FIP_DEPS += $(3))) 83endef 84 85# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invokation 86# $(1) = parameter filename 87# $(2) = cert_create command line option for the specified parameter 88# $(3) = input parameter (false if empty) 89define CERT_ADD_CMD_OPT 90 $(eval $(if $(3),CRT_DEPS += $(1))) 91 $(eval CRT_ARGS += $(2) $(1)) 92endef 93 94# FIP_ADD_IMG allows the platform to specify an image to be packed in the FIP 95# using a build option. It also adds a dependency on the image file, aborting 96# the build if the file does not exist. 97# $(1) = build option to specify the image filename (BL30, BL33, etc) 98# $(2) = command line option for the fip_create tool (bl30, bl33, etc) 99# Example: 100# $(eval $(call FIP_ADD_IMG,BL33,--bl33)) 101define FIP_ADD_IMG 102 CRT_DEPS += check_$(1) 103 FIP_DEPS += check_$(1) 104 $(call FIP_ADD_PAYLOAD,$(value $(1)),$(2)) 105 106check_$(1): 107 $$(if $(value $(1)),,$$(error "Platform '${PLAT}' requires $(1). Please set $(1) to point to the right file")) 108endef 109 110 111################################################################################ 112# Auxiliary macros to build TF images from sources 113################################################################################ 114 115# If no goal is specified in the command line, .DEFAULT_GOAL is used. 116# .DEFAULT_GOAL is defined in the main Makefile before including this file. 117ifeq ($(MAKECMDGOALS),) 118MAKECMDGOALS := $(.DEFAULT_GOAL) 119endif 120 121define match_goals 122$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS)))) 123endef 124 125# List of rules that involve building things 126BUILD_TARGETS := all bl1 bl2 bl31 bl32 certificates fip 127 128# Does the list of goals specified on the command line include a build target? 129ifneq ($(call match_goals,${BUILD_TARGETS}),) 130IS_ANYTHING_TO_BUILD := 1 131endif 132 133 134# MAKE_C builds a C source file and generates the dependency file 135# $(1) = output directory 136# $(2) = source file (%.c) 137# $(3) = BL stage (2, 30, 31, 32, 33) 138define MAKE_C 139 140$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 141$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 142 143$(OBJ): $(2) 144 @echo " CC $$<" 145 $$(Q)$$(CC) $$(CFLAGS) -DIMAGE_BL$(3) -c $$< -o $$@ 146 147 148$(PREREQUISITES): $(2) 149 @echo " DEPS $$@" 150 @mkdir -p $(1) 151 $$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$< 152 153ifdef IS_ANYTHING_TO_BUILD 154-include $(PREREQUISITES) 155endif 156 157endef 158 159 160# MAKE_S builds an assembly source file and generates the dependency file 161# $(1) = output directory 162# $(2) = assembly file (%.S) 163# $(3) = BL stage (2, 30, 31, 32, 33) 164define MAKE_S 165 166$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 167$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 168 169$(OBJ): $(2) 170 @echo " AS $$<" 171 $$(Q)$$(AS) $$(ASFLAGS) -DIMAGE_BL$(3) -c $$< -o $$@ 172 173$(PREREQUISITES): $(2) 174 @echo " DEPS $$@" 175 @mkdir -p $(1) 176 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$< 177 178ifdef IS_ANYTHING_TO_BUILD 179-include $(PREREQUISITES) 180endif 181 182endef 183 184 185# MAKE_LD generate the linker script using the C preprocessor 186# $(1) = output linker script 187# $(2) = input template 188define MAKE_LD 189 190$(eval PREREQUISITES := $(1).d) 191 192$(1): $(2) 193 @echo " PP $$<" 194 $$(Q)$$(AS) $$(ASFLAGS) -P -E -D__LINKER__ -o $$@ $$< 195 196$(PREREQUISITES): $(2) 197 @echo " DEPS $$@" 198 @mkdir -p $$(dir $$@) 199 $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(1) -MF $$@ $$< 200 201ifdef IS_ANYTHING_TO_BUILD 202-include $(PREREQUISITES) 203endif 204 205endef 206 207 208# MAKE_OBJS builds both C and assembly source files 209# $(1) = output directory 210# $(2) = list of source files (both C and assembly) 211# $(3) = BL stage (2, 30, 31, 32, 33) 212define MAKE_OBJS 213 $(eval C_OBJS := $(filter %.c,$(2))) 214 $(eval REMAIN := $(filter-out %.c,$(2))) 215 $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3)))) 216 217 $(eval S_OBJS := $(filter %.S,$(REMAIN))) 218 $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 219 $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3)))) 220 221 $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 222endef 223 224 225# NOTE: The line continuation '\' is required in the next define otherwise we 226# end up with a line-feed characer at the end of the last c filename. 227# Also bare this issue in mind if extending the list of supported filetypes. 228define SOURCES_TO_OBJS 229 $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ 230 $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) 231endef 232 233 234# MAKE_TOOL_ARGS macro defines the command line arguments for the FIP tool for 235# each BL image. Arguments: 236# $(1) = BL stage (2, 30, 31, 32, 33) 237# $(2) = Binary file 238# $(3) = In FIP (false if empty) 239define MAKE_TOOL_ARGS 240 $(if $(3),$(eval $(call FIP_ADD_PAYLOAD,$(2),--bl$(1),bl$(1)))) 241endef 242 243 244# MAKE_BL macro defines the targets and options to build each BL image. 245# Arguments: 246# $(1) = BL stage (2, 30, 31, 32, 33) 247# $(2) = In FIP (false if empty) 248define MAKE_BL 249 $(eval BUILD_DIR := ${BUILD_PLAT}/bl$(1)) 250 $(eval SOURCES := $(BL$(1)_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)) 251 $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 252 $(eval LINKERFILE := $(call IMG_LINKERFILE,$(1))) 253 $(eval MAPFILE := $(call IMG_MAPFILE,$(1))) 254 $(eval ELF := $(call IMG_ELF,$(1))) 255 $(eval DUMP := $(call IMG_DUMP,$(1))) 256 $(eval BIN := $(call IMG_BIN,$(1))) 257 258 $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1))) 259 $(eval $(call MAKE_LD,$(LINKERFILE),$(BL$(1)_LINKERFILE))) 260 261$(BUILD_DIR): 262 $$(Q)mkdir -p "$$@" 263 264$(ELF): $(OBJS) $(LINKERFILE) 265 @echo " LD $$@" 266 @echo 'const char build_message[] = "Built : "__TIME__", "__DATE__; \ 267 const char version_string[] = "${VERSION_STRING}";' | \ 268 $$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o 269 $$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \ 270 $(BUILD_DIR)/build_message.o $(OBJS) 271 272$(DUMP): $(ELF) 273 @echo " OD $$@" 274 $${Q}$${OD} -dx $$< > $$@ 275 276$(BIN): $(ELF) 277 @echo " BIN $$@" 278 $$(Q)$$(OC) -O binary $$< $$@ 279 @echo 280 @echo "Built $$@ successfully" 281 @echo 282 283.PHONY: bl$(1) 284bl$(1): $(BUILD_DIR) $(BIN) $(DUMP) 285 286all: bl$(1) 287 288$(eval $(call MAKE_TOOL_ARGS,$(1),$(BIN),$(2))) 289 290endef 291 292