Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 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 | |
Yatharth Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 31 | # This table is used in converting lower case to upper case. |
| 32 | uppercase_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 |
| 37 | define uppercase_internal |
| 38 | $(if $(1),$$(subst $(firstword $(1)),$(call uppercase_internal,$(wordlist 2,$(words $(1)),$(1)),$(2))),$(2)) |
| 39 | endef |
| 40 | |
| 41 | # A macro for converting a string to upper case |
| 42 | # $(1) = String to convert |
| 43 | define uppercase |
| 44 | $(eval uppercase_result:=$(call uppercase_internal,$(uppercase_table),$(1)))$(uppercase_result) |
| 45 | endef |
| 46 | |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 47 | # Convenience function for adding build definitions |
| 48 | # $(eval $(call add_define,FOO)) will have: |
| 49 | # -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise |
| 50 | define add_define |
| 51 | DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),) |
| 52 | endef |
| 53 | |
| 54 | # Convenience function for verifying option has a boolean value |
| 55 | # $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1 |
| 56 | define assert_boolean |
| 57 | $(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean)) |
| 58 | endef |
| 59 | |
| 60 | # IMG_LINKERFILE defines the linker script corresponding to a BL stage |
| 61 | # $(1) = BL stage (2, 30, 31, 32, 33) |
| 62 | define IMG_LINKERFILE |
| 63 | ${BUILD_DIR}/bl$(1).ld |
| 64 | endef |
| 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) |
| 69 | define IMG_MAPFILE |
| 70 | ${BUILD_DIR}/bl$(1).map |
| 71 | endef |
| 72 | |
| 73 | # IMG_ELF defines the elf file corresponding to a BL stage |
| 74 | # $(1) = BL stage (2, 30, 31, 32, 33) |
| 75 | define IMG_ELF |
| 76 | ${BUILD_DIR}/bl$(1).elf |
| 77 | endef |
| 78 | |
| 79 | # IMG_DUMP defines the symbols dump file corresponding to a BL stage |
| 80 | # $(1) = BL stage (2, 30, 31, 32, 33) |
| 81 | define IMG_DUMP |
| 82 | ${BUILD_DIR}/bl$(1).dump |
| 83 | endef |
| 84 | |
| 85 | # IMG_BIN defines the default image file corresponding to a BL stage |
| 86 | # $(1) = BL stage (2, 30, 31, 32, 33) |
| 87 | define IMG_BIN |
| 88 | ${BUILD_PLAT}/bl$(1).bin |
| 89 | endef |
| 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) |
| 96 | define FIP_ADD_PAYLOAD |
| 97 | $(eval FIP_ARGS += $(2) $(1)) |
| 98 | $(eval $(if $(3),FIP_DEPS += $(3))) |
| 99 | endef |
| 100 | |
Yatharth Kochar | d1a9343 | 2015-10-12 12:33:47 +0100 | [diff] [blame] | 101 | # CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 102 | # $(1) = parameter filename |
| 103 | # $(2) = cert_create command line option for the specified parameter |
| 104 | # $(3) = input parameter (false if empty) |
| 105 | define CERT_ADD_CMD_OPT |
| 106 | $(eval $(if $(3),CRT_DEPS += $(1))) |
| 107 | $(eval CRT_ARGS += $(2) $(1)) |
| 108 | endef |
| 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 Castillo | a72b647 | 2015-12-10 15:49:17 +0000 | [diff] [blame] | 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) |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 115 | # Example: |
| 116 | # $(eval $(call FIP_ADD_IMG,BL33,--bl33)) |
| 117 | define FIP_ADD_IMG |
| 118 | CRT_DEPS += check_$(1) |
| 119 | FIP_DEPS += check_$(1) |
| 120 | $(call FIP_ADD_PAYLOAD,$(value $(1)),$(2)) |
| 121 | |
| 122 | check_$(1): |
| 123 | $$(if $(value $(1)),,$$(error "Platform '${PLAT}' requires $(1). Please set $(1) to point to the right file")) |
| 124 | endef |
| 125 | |
Yatharth Kochar | d1a9343 | 2015-10-12 12:33:47 +0100 | [diff] [blame] | 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. --ns_bl2u) |
| 130 | # $(3) = fip target dependency (optional) (e.g. ns_bl2u) |
| 131 | define FWU_FIP_ADD_PAYLOAD |
| 132 | $(eval $(if $(3),FWU_FIP_DEPS += $(3))) |
| 133 | $(eval FWU_FIP_ARGS += $(2) $(1)) |
| 134 | endef |
| 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) |
| 140 | define FWU_CERT_ADD_CMD_OPT |
| 141 | $(eval $(if $(3),FWU_CRT_DEPS += $(1))) |
| 142 | $(eval FWU_CRT_ARGS += $(2) $(1)) |
| 143 | endef |
| 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)) |
| 150 | define 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 | |
| 155 | check_$(1): |
| 156 | $$(if $(value $(1)),,$$(error "Platform '${PLAT}' requires $(1). Please set $(1) to point to the right file")) |
| 157 | endef |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 158 | |
| 159 | ################################################################################ |
| 160 | # Auxiliary macros to build TF images from sources |
| 161 | ################################################################################ |
| 162 | |
Juan Castillo | 396644b | 2015-10-22 11:34:44 +0100 | [diff] [blame] | 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. |
| 165 | ifeq ($(MAKECMDGOALS),) |
| 166 | MAKECMDGOALS := $(.DEFAULT_GOAL) |
| 167 | endif |
| 168 | |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 169 | define match_goals |
| 170 | $(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS)))) |
| 171 | endef |
| 172 | |
| 173 | # List of rules that involve building things |
Yatharth Kochar | b1c2fe0 | 2015-10-14 15:27:24 +0100 | [diff] [blame] | 174 | BUILD_TARGETS := all bl1 bl2 bl2u bl31 bl32 certificates fip |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 175 | |
| 176 | # Does the list of goals specified on the command line include a build target? |
| 177 | ifneq ($(call match_goals,${BUILD_TARGETS}),) |
| 178 | IS_ANYTHING_TO_BUILD := 1 |
| 179 | endif |
| 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 Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 185 | # $(3) = BL stage (2, 2u, 30, 31, 32, 33) |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 186 | define MAKE_C |
| 187 | |
| 188 | $(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) |
| 189 | $(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) |
Yatharth Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 190 | $(eval IMAGE := IMAGE_BL$(call uppercase,$(3))) |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 191 | |
| 192 | $(OBJ): $(2) |
| 193 | @echo " CC $$<" |
Yatharth Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 194 | $$(Q)$$(CC) $$(CFLAGS) -D$(IMAGE) -c $$< -o $$@ |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 195 | |
| 196 | |
| 197 | $(PREREQUISITES): $(2) |
| 198 | @echo " DEPS $$@" |
| 199 | @mkdir -p $(1) |
| 200 | $$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$< |
| 201 | |
| 202 | ifdef IS_ANYTHING_TO_BUILD |
| 203 | -include $(PREREQUISITES) |
| 204 | endif |
| 205 | |
| 206 | endef |
| 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 Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 212 | # $(3) = BL stage (2, 2u, 30, 31, 32, 33) |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 213 | define MAKE_S |
| 214 | |
| 215 | $(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) |
| 216 | $(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) |
Yatharth Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 217 | $(eval IMAGE := IMAGE_BL$(call uppercase,$(3))) |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 218 | |
| 219 | $(OBJ): $(2) |
| 220 | @echo " AS $$<" |
Yatharth Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 221 | $$(Q)$$(AS) $$(ASFLAGS) -D$(IMAGE) -c $$< -o $$@ |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 222 | |
| 223 | $(PREREQUISITES): $(2) |
| 224 | @echo " DEPS $$@" |
| 225 | @mkdir -p $(1) |
| 226 | $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$< |
| 227 | |
| 228 | ifdef IS_ANYTHING_TO_BUILD |
| 229 | -include $(PREREQUISITES) |
| 230 | endif |
| 231 | |
| 232 | endef |
| 233 | |
| 234 | |
| 235 | # MAKE_LD generate the linker script using the C preprocessor |
| 236 | # $(1) = output linker script |
| 237 | # $(2) = input template |
| 238 | define 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 | |
| 251 | ifdef IS_ANYTHING_TO_BUILD |
| 252 | -include $(PREREQUISITES) |
| 253 | endif |
| 254 | |
| 255 | endef |
| 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) |
| 262 | define 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))) |
| 272 | endef |
| 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. |
| 278 | define SOURCES_TO_OBJS |
| 279 | $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ |
| 280 | $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) |
| 281 | endef |
| 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) = In FIP (false if empty) |
| 289 | define MAKE_TOOL_ARGS |
| 290 | $(if $(3),$(eval $(call FIP_ADD_PAYLOAD,$(2),--bl$(1),bl$(1)))) |
| 291 | endef |
| 292 | |
| 293 | |
| 294 | # MAKE_BL macro defines the targets and options to build each BL image. |
| 295 | # Arguments: |
Yatharth Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 296 | # $(1) = BL stage (2, 2u, 30, 31, 32, 33) |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 297 | # $(2) = In FIP (false if empty) |
| 298 | define MAKE_BL |
| 299 | $(eval BUILD_DIR := ${BUILD_PLAT}/bl$(1)) |
Yatharth Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 300 | $(eval BL_SOURCES := $(BL$(call uppercase,$(1))_SOURCES)) |
| 301 | $(eval SOURCES := $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)) |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 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))) |
Yatharth Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 308 | $(eval BL_LINKERFILE := $(BL$(call uppercase,$(1))_LINKERFILE)) |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 309 | |
| 310 | $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1))) |
Yatharth Kochar | 115352c | 2015-10-02 13:58:52 +0100 | [diff] [blame] | 311 | $(eval $(call MAKE_LD,$(LINKERFILE),$(BL_LINKERFILE))) |
Juan Castillo | a3487d1 | 2015-08-18 14:23:04 +0100 | [diff] [blame] | 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) |
| 336 | bl$(1): $(BUILD_DIR) $(BIN) $(DUMP) |
| 337 | |
| 338 | all: bl$(1) |
| 339 | |
| 340 | $(eval $(call MAKE_TOOL_ARGS,$(1),$(BIN),$(2))) |
| 341 | |
| 342 | endef |
| 343 | |