xref: /rk3399_ARM-atf/Makefile (revision 0260eb0d15adab03c63dcda908fe6ac015da129c)
1#
2# Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
7#
8# Trusted Firmware Version
9#
10VERSION_MAJOR			:= 2
11VERSION_MINOR			:= 6
12
13# Default goal is build all images
14.DEFAULT_GOAL			:= all
15
16# Avoid any implicit propagation of command line variable definitions to
17# sub-Makefiles, like CFLAGS that we reserved for the firmware images'
18# usage. Other command line options like "-s" are still propagated as usual.
19MAKEOVERRIDES =
20
21MAKE_HELPERS_DIRECTORY := make_helpers/
22include ${MAKE_HELPERS_DIRECTORY}build_macros.mk
23include ${MAKE_HELPERS_DIRECTORY}build_env.mk
24
25################################################################################
26# Default values for build configurations, and their dependencies
27################################################################################
28
29include ${MAKE_HELPERS_DIRECTORY}defaults.mk
30
31# Assertions enabled for DEBUG builds by default
32ENABLE_ASSERTIONS		:= ${DEBUG}
33ENABLE_PMF			:= ${ENABLE_RUNTIME_INSTRUMENTATION}
34PLAT				:= ${DEFAULT_PLAT}
35
36################################################################################
37# Checkpatch script options
38################################################################################
39
40CHECKCODE_ARGS		:=	--no-patch
41# Do not check the coding style on imported library files or documentation files
42INC_ARM_DIRS_TO_CHECK	:=	$(sort $(filter-out                     \
43					include/drivers/arm/cryptocell,	\
44					$(wildcard include/drivers/arm/*)))
45INC_ARM_DIRS_TO_CHECK	+=	include/drivers/arm/cryptocell/*.h
46INC_DRV_DIRS_TO_CHECK	:=	$(sort $(filter-out			\
47					include/drivers/arm,		\
48					$(wildcard include/drivers/*)))
49INC_LIB_DIRS_TO_CHECK	:=	$(sort $(filter-out			\
50					include/lib/libfdt		\
51					include/lib/libc,		\
52					$(wildcard include/lib/*)))
53INC_DIRS_TO_CHECK	:=	$(sort $(filter-out			\
54					include/lib			\
55					include/drivers,		\
56					$(wildcard include/*)))
57LIB_DIRS_TO_CHECK	:=	$(sort $(filter-out			\
58					lib/compiler-rt			\
59					lib/libfdt%			\
60					lib/libc,			\
61					$(wildcard lib/*)))
62ROOT_DIRS_TO_CHECK	:=	$(sort $(filter-out			\
63					lib				\
64					include				\
65					docs				\
66					%.rst,				\
67					$(wildcard *)))
68CHECK_PATHS		:=	${ROOT_DIRS_TO_CHECK}			\
69				${INC_DIRS_TO_CHECK}			\
70				${INC_LIB_DIRS_TO_CHECK}		\
71				${LIB_DIRS_TO_CHECK}			\
72				${INC_DRV_DIRS_TO_CHECK}		\
73				${INC_ARM_DIRS_TO_CHECK}
74
75
76################################################################################
77# Process build options
78################################################################################
79
80# Verbose flag
81ifeq (${V},0)
82        Q:=@
83        ECHO:=@echo
84        CHECKCODE_ARGS	+=	--no-summary --terse
85else
86        Q:=
87        ECHO:=$(ECHO_QUIET)
88endif
89
90ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
91        Q:=@
92        ECHO:=$(ECHO_QUIET)
93endif
94
95export Q ECHO
96
97# The cert_create tool cannot generate certificates individually, so we use the
98# target 'certificates' to create them all
99ifneq (${GENERATE_COT},0)
100        FIP_DEPS += certificates
101        FWU_FIP_DEPS += fwu_certificates
102endif
103
104# Process BRANCH_PROTECTION value and set
105# Pointer Authentication and Branch Target Identification flags
106ifeq (${BRANCH_PROTECTION},0)
107	# Default value turns off all types of branch protection
108	BP_OPTION := none
109else ifneq (${ARCH},aarch64)
110        $(error BRANCH_PROTECTION requires AArch64)
111else ifeq (${BRANCH_PROTECTION},1)
112	# Enables all types of branch protection features
113	BP_OPTION := standard
114	ENABLE_BTI := 1
115	ENABLE_PAUTH := 1
116else ifeq (${BRANCH_PROTECTION},2)
117	# Return address signing to its standard level
118	BP_OPTION := pac-ret
119	ENABLE_PAUTH := 1
120else ifeq (${BRANCH_PROTECTION},3)
121	# Extend the signing to include leaf functions
122	BP_OPTION := pac-ret+leaf
123	ENABLE_PAUTH := 1
124else ifeq (${BRANCH_PROTECTION},4)
125	# Turn on branch target identification mechanism
126	BP_OPTION := bti
127	ENABLE_BTI := 1
128else
129        $(error Unknown BRANCH_PROTECTION value ${BRANCH_PROTECTION})
130endif
131
132# FEAT_RME
133ifeq (${ENABLE_RME},1)
134# RME doesn't support PIE
135ifneq (${ENABLE_PIE},0)
136        $(error ENABLE_RME does not support PIE)
137endif
138# RME requires AARCH64
139ifneq (${ARCH},aarch64)
140        $(error ENABLE_RME requires AArch64)
141endif
142# RME requires el2 context to be saved for now.
143CTX_INCLUDE_EL2_REGS := 1
144CTX_INCLUDE_AARCH32_REGS := 0
145ARM_ARCH_MAJOR := 8
146ARM_ARCH_MINOR := 6
147endif
148
149# USE_SPINLOCK_CAS requires AArch64 build
150ifeq (${USE_SPINLOCK_CAS},1)
151ifneq (${ARCH},aarch64)
152        $(error USE_SPINLOCK_CAS requires AArch64)
153endif
154endif
155
156# USE_DEBUGFS experimental feature recommended only in debug builds
157ifeq (${USE_DEBUGFS},1)
158ifeq (${DEBUG},1)
159        $(warning DEBUGFS experimental feature is enabled.)
160else
161        $(warning DEBUGFS experimental, recommended in DEBUG builds ONLY)
162endif
163endif
164
165ifneq (${DECRYPTION_SUPPORT},none)
166ENC_ARGS += -f ${FW_ENC_STATUS}
167ENC_ARGS += -k ${ENC_KEY}
168ENC_ARGS += -n ${ENC_NONCE}
169FIP_DEPS += enctool
170FWU_FIP_DEPS += enctool
171endif
172
173################################################################################
174# Toolchain
175################################################################################
176
177HOSTCC			:=	gcc
178export HOSTCC
179
180CC			:=	${CROSS_COMPILE}gcc
181CPP			:=	${CROSS_COMPILE}cpp
182AS			:=	${CROSS_COMPILE}gcc
183AR			:=	${CROSS_COMPILE}ar
184LINKER			:=	${CROSS_COMPILE}ld
185OC			:=	${CROSS_COMPILE}objcopy
186OD			:=	${CROSS_COMPILE}objdump
187NM			:=	${CROSS_COMPILE}nm
188PP			:=	${CROSS_COMPILE}gcc -E
189DTC			:=	dtc
190
191# Use ${LD}.bfd instead if it exists (as absolute path or together with $PATH).
192ifneq ($(strip $(wildcard ${LD}.bfd) \
193	$(foreach dir,$(subst :, ,${PATH}),$(wildcard ${dir}/${LINKER}.bfd))),)
194LINKER			:=	${LINKER}.bfd
195endif
196
197ifeq (${ARM_ARCH_MAJOR},7)
198target32-directive	= 	-target arm-none-eabi
199# Will set march32-directive from platform configuration
200else
201target32-directive	= 	-target armv8a-none-eabi
202
203# Set the compiler's target architecture profile based on
204# ARM_ARCH_MAJOR ARM_ARCH_MINOR options
205ifeq (${ARM_ARCH_MINOR},0)
206march32-directive	= 	-march=armv${ARM_ARCH_MAJOR}-a
207march64-directive	= 	-march=armv${ARM_ARCH_MAJOR}-a
208else
209march32-directive	= 	-march=armv${ARM_ARCH_MAJOR}.${ARM_ARCH_MINOR}-a
210march64-directive	= 	-march=armv${ARM_ARCH_MAJOR}.${ARM_ARCH_MINOR}-a
211endif
212endif
213
214# Memory tagging is supported in architecture Armv8.5-A AArch64 and onwards
215ifeq ($(ARCH), aarch64)
216# Check if revision is greater than or equal to 8.5
217ifeq "8.5" "$(word 1, $(sort 8.5 $(ARM_ARCH_MAJOR).$(ARM_ARCH_MINOR)))"
218mem_tag_arch_support	= 	yes
219endif
220endif
221
222# Get architecture feature modifiers
223arch-features		=	${ARM_ARCH_FEATURE}
224
225# Enable required options for memory stack tagging.
226# Currently, these options are enabled only for clang and armclang compiler.
227ifeq (${SUPPORT_STACK_MEMTAG},yes)
228ifdef mem_tag_arch_support
229# Check for armclang and clang compilers
230ifneq ( ,$(filter $(notdir $(CC)),armclang clang))
231# Add "memtag" architecture feature modifier if not specified
232ifeq ( ,$(findstring memtag,$(arch-features)))
233arch-features       	:=       $(arch-features)+memtag
234endif	# memtag
235ifeq ($(notdir $(CC)),armclang)
236TF_CFLAGS		+=	-mmemtag-stack
237else ifeq ($(notdir $(CC)),clang)
238TF_CFLAGS		+=	-fsanitize=memtag
239endif	# armclang
240endif	# armclang clang
241else
242$(error "Error: stack memory tagging is not supported for architecture \
243	${ARCH},armv${ARM_ARCH_MAJOR}.${ARM_ARCH_MINOR}-a")
244endif	# mem_tag_arch_support
245endif	# SUPPORT_STACK_MEMTAG
246
247# Set the compiler's architecture feature modifiers
248ifneq ($(arch-features), none)
249# Strip "none+" from arch-features
250arch-features		:=	$(subst none+,,$(arch-features))
251ifeq ($(ARCH), aarch32)
252march32-directive	:=	$(march32-directive)+$(arch-features)
253else
254march64-directive	:=	$(march64-directive)+$(arch-features)
255endif
256# Print features
257$(info Arm Architecture Features specified: $(subst +, ,$(arch-features)))
258endif	# arch-features
259
260# Determine if FEAT_RNG is supported
261ENABLE_FEAT_RNG		=	$(if $(findstring rng,${arch-features}),1,0)
262
263# Determine if FEAT_SB is supported
264ENABLE_FEAT_SB		=	$(if $(findstring sb,${arch-features}),1,0)
265
266ifeq "8.5" "$(word 1, $(sort 8.5 $(ARM_ARCH_MAJOR).$(ARM_ARCH_MINOR)))"
267ENABLE_FEAT_SB		= 	1
268endif
269
270# Determine and enable FEAT_FGT to access HDFGRTR_EL2 register for v8.6 and higher versions.
271ifeq "8.6" "$(word 1, $(sort 8.6 $(ARM_ARCH_MAJOR).$(ARM_ARCH_MINOR)))"
272ENABLE_FEAT_FGT		=	1
273endif
274
275# Determine and enable FEAT_ECV to access CNTPOFF_EL2 register for v8.6 and higher versions.
276ifeq "8.6" "$(word 1, $(sort 8.6 $(ARM_ARCH_MAJOR).$(ARM_ARCH_MINOR)))"
277ENABLE_FEAT_ECV		=	1
278endif
279
280ifeq "8.4" "$(word 1, $(sort 8.4 $(ARM_ARCH_MAJOR).$(ARM_ARCH_MINOR)))"
281ENABLE_FEAT_DIT		= 	1
282endif
283
284ifneq ($(findstring armclang,$(notdir $(CC))),)
285TF_CFLAGS_aarch32	=	-target arm-arm-none-eabi $(march32-directive)
286TF_CFLAGS_aarch64	=	-target aarch64-arm-none-eabi $(march64-directive)
287LD			=	$(LINKER)
288AS			=	$(CC) -c -x assembler-with-cpp $(TF_CFLAGS_$(ARCH))
289CPP			=	$(CC) -E $(TF_CFLAGS_$(ARCH))
290PP			=	$(CC) -E $(TF_CFLAGS_$(ARCH))
291else ifneq ($(findstring clang,$(notdir $(CC))),)
292CLANG_CCDIR		=	$(if $(filter-out ./,$(dir $(CC))),$(dir $(CC)),)
293TF_CFLAGS_aarch32	=	$(target32-directive) $(march32-directive)
294TF_CFLAGS_aarch64	=	-target aarch64-elf $(march64-directive)
295LD			=	$(CLANG_CCDIR)ld.lld
296ifeq (, $(shell which $(LD)))
297$(error "No $(LD) in PATH, make sure it is installed or set LD to a different linker")
298endif
299AS			=	$(CC) -c -x assembler-with-cpp $(TF_CFLAGS_$(ARCH))
300CPP			=	$(CC) -E
301PP			=	$(CC) -E
302else ifneq ($(findstring gcc,$(notdir $(CC))),)
303TF_CFLAGS_aarch32	=	$(march32-directive)
304TF_CFLAGS_aarch64	=	$(march64-directive)
305ifeq ($(ENABLE_LTO),1)
306	# Enable LTO only for aarch64
307	ifeq (${ARCH},aarch64)
308		LTO_CFLAGS	=	-flto
309		# Use gcc as a wrapper for the ld, recommended for LTO
310		LINKER		:=	${CROSS_COMPILE}gcc
311	endif
312endif
313LD			=	$(LINKER)
314else
315TF_CFLAGS_aarch32	=	$(march32-directive)
316TF_CFLAGS_aarch64	=	$(march64-directive)
317LD			=	$(LINKER)
318endif
319
320# Process Debug flag
321$(eval $(call add_define,DEBUG))
322ifneq (${DEBUG}, 0)
323        BUILD_TYPE	:=	debug
324        TF_CFLAGS	+= 	-g
325
326        ifneq ($(findstring clang,$(notdir $(CC))),)
327             ASFLAGS		+= 	-g
328        else
329             ASFLAGS		+= 	-g -Wa,--gdwarf-2
330        endif
331
332        # Use LOG_LEVEL_INFO by default for debug builds
333        LOG_LEVEL	:=	40
334else
335        BUILD_TYPE	:=	release
336        # Use LOG_LEVEL_NOTICE by default for release builds
337        LOG_LEVEL	:=	20
338endif
339
340# Default build string (git branch and commit)
341ifeq (${BUILD_STRING},)
342        BUILD_STRING  :=  $(shell git describe --always --dirty --tags 2> /dev/null)
343endif
344VERSION_STRING    :=  v${VERSION_MAJOR}.${VERSION_MINOR}(${BUILD_TYPE}):${BUILD_STRING}
345
346ifeq (${AARCH32_INSTRUCTION_SET},A32)
347TF_CFLAGS_aarch32	+=	-marm
348else ifeq (${AARCH32_INSTRUCTION_SET},T32)
349TF_CFLAGS_aarch32	+=	-mthumb
350else
351$(error Error: Unknown AArch32 instruction set ${AARCH32_INSTRUCTION_SET})
352endif
353
354TF_CFLAGS_aarch32	+=	-mno-unaligned-access
355TF_CFLAGS_aarch64	+=	-mgeneral-regs-only -mstrict-align
356
357ifneq (${BP_OPTION},none)
358TF_CFLAGS_aarch64	+=	-mbranch-protection=${BP_OPTION}
359endif
360
361ASFLAGS_aarch32		=	$(march32-directive)
362ASFLAGS_aarch64		=	$(march64-directive)
363
364# General warnings
365WARNINGS		:=	-Wall -Wmissing-include-dirs -Wunused	\
366				-Wdisabled-optimization -Wvla -Wshadow	\
367				-Wno-unused-parameter -Wredundant-decls
368
369# Additional warnings
370# Level 1
371WARNING1 := -Wextra
372WARNING1 += -Wmissing-format-attribute
373WARNING1 += -Wmissing-prototypes
374WARNING1 += -Wold-style-definition
375
376# Level 2
377WARNING2 := -Waggregate-return
378WARNING2 += -Wcast-align
379WARNING2 += -Wnested-externs
380
381WARNING3 := -Wbad-function-cast
382WARNING3 += -Wcast-qual
383WARNING3 += -Wconversion
384WARNING3 += -Wpacked
385WARNING3 += -Wpointer-arith
386WARNING3 += -Wswitch-default
387
388ifeq (${W},1)
389WARNINGS += $(WARNING1)
390else ifeq (${W},2)
391WARNINGS += $(WARNING1) $(WARNING2)
392else ifeq (${W},3)
393WARNINGS += $(WARNING1) $(WARNING2) $(WARNING3)
394endif
395
396# Compiler specific warnings
397ifeq ($(findstring clang,$(notdir $(CC))),)
398# not using clang
399WARNINGS	+=		-Wunused-but-set-variable -Wmaybe-uninitialized	\
400				-Wpacked-bitfield-compat -Wshift-overflow=2 \
401				-Wlogical-op
402else
403# using clang
404WARNINGS	+=		-Wshift-overflow -Wshift-sign-overflow \
405				-Wlogical-op-parentheses
406endif
407
408ifneq (${E},0)
409ERRORS := -Werror
410endif
411
412CPPFLAGS		=	${DEFINES} ${INCLUDES} ${MBEDTLS_INC} -nostdinc	\
413				$(ERRORS) $(WARNINGS)
414ASFLAGS			+=	$(CPPFLAGS) $(ASFLAGS_$(ARCH))			\
415				-ffreestanding -Wa,--fatal-warnings
416TF_CFLAGS		+=	$(CPPFLAGS) $(TF_CFLAGS_$(ARCH))		\
417				-ffunction-sections -fdata-sections		\
418				-ffreestanding -fno-builtin -fno-common		\
419				-Os -std=gnu99
420
421ifeq (${SANITIZE_UB},on)
422TF_CFLAGS		+=	-fsanitize=undefined -fno-sanitize-recover
423endif
424ifeq (${SANITIZE_UB},trap)
425TF_CFLAGS		+=	-fsanitize=undefined -fno-sanitize-recover	\
426				-fsanitize-undefined-trap-on-error
427endif
428
429GCC_V_OUTPUT		:=	$(shell $(CC) -v 2>&1)
430
431# LD = armlink
432ifneq ($(findstring armlink,$(notdir $(LD))),)
433TF_LDFLAGS		+=	--diag_error=warning --lto_level=O1
434TF_LDFLAGS		+=	--remove --info=unused,unusedsymbols
435TF_LDFLAGS		+=	$(TF_LDFLAGS_$(ARCH))
436
437# LD = gcc (used when GCC LTO is enabled)
438else ifneq ($(findstring gcc,$(notdir $(LD))),)
439# Pass ld options with Wl or Xlinker switches
440TF_LDFLAGS		+=	-Wl,--fatal-warnings -O1
441TF_LDFLAGS		+=	-Wl,--gc-sections
442ifeq ($(ENABLE_LTO),1)
443	ifeq (${ARCH},aarch64)
444		TF_LDFLAGS	+=	-flto -fuse-linker-plugin
445	endif
446endif
447# GCC automatically adds fix-cortex-a53-843419 flag when used to link
448# which breaks some builds, so disable if errata fix is not explicitly enabled
449ifneq (${ERRATA_A53_843419},1)
450	TF_LDFLAGS	+= 	-mno-fix-cortex-a53-843419
451endif
452TF_LDFLAGS		+= 	-nostdlib
453TF_LDFLAGS		+=	$(subst --,-Xlinker --,$(TF_LDFLAGS_$(ARCH)))
454
455# LD = gcc-ld (ld) or llvm-ld (ld.lld) or other
456else
457TF_LDFLAGS		+=	--fatal-warnings -O1
458TF_LDFLAGS		+=	--gc-sections
459# ld.lld doesn't recognize the errata flags,
460# therefore don't add those in that case
461ifeq ($(findstring ld.lld,$(notdir $(LD))),)
462TF_LDFLAGS		+=	$(TF_LDFLAGS_$(ARCH))
463endif
464endif
465
466DTC_FLAGS		+=	-I dts -O dtb
467DTC_CPPFLAGS		+=	-P -nostdinc -Iinclude -Ifdts -undef \
468				-x assembler-with-cpp $(DEFINES)
469
470ifeq ($(MEASURED_BOOT),1)
471DTC_CPPFLAGS		+=	-DMEASURED_BOOT -DBL2_HASH_SIZE=${TCG_DIGEST_SIZE}
472endif
473
474################################################################################
475# Common sources and include directories
476################################################################################
477include lib/compiler-rt/compiler-rt.mk
478
479BL_COMMON_SOURCES	+=	common/bl_common.c			\
480				common/tf_log.c				\
481				common/${ARCH}/debug.S			\
482				drivers/console/multi_console.c		\
483				lib/${ARCH}/cache_helpers.S		\
484				lib/${ARCH}/misc_helpers.S		\
485				plat/common/plat_bl_common.c		\
486				plat/common/plat_log_common.c		\
487				plat/common/${ARCH}/plat_common.c	\
488				plat/common/${ARCH}/platform_helpers.S	\
489				${COMPILER_RT_SRCS}
490
491ifeq ($(notdir $(CC)),armclang)
492BL_COMMON_SOURCES	+=	lib/${ARCH}/armclang_printf.S
493endif
494
495ifeq (${SANITIZE_UB},on)
496BL_COMMON_SOURCES	+=	plat/common/ubsan.c
497endif
498
499INCLUDES		+=	-Iinclude				\
500				-Iinclude/arch/${ARCH}			\
501				-Iinclude/lib/cpus/${ARCH}		\
502				-Iinclude/lib/el3_runtime/${ARCH}	\
503				${PLAT_INCLUDES}			\
504				${SPD_INCLUDES}
505
506include common/backtrace/backtrace.mk
507
508################################################################################
509# Generic definitions
510################################################################################
511
512include ${MAKE_HELPERS_DIRECTORY}plat_helpers.mk
513
514ifeq (${BUILD_BASE},)
515     BUILD_BASE		:=	./build
516endif
517BUILD_PLAT		:=	$(abspath ${BUILD_BASE})/${PLAT}/${BUILD_TYPE}
518
519SPDS			:=	$(sort $(filter-out none, $(patsubst services/spd/%,%,$(wildcard services/spd/*))))
520
521# Platforms providing their own TBB makefile may override this value
522INCLUDE_TBBR_MK		:=	1
523
524
525################################################################################
526# Include SPD Makefile if one has been specified
527################################################################################
528
529ifneq (${SPD},none)
530    ifeq (${ARCH},aarch32)
531        $(error "Error: SPD is incompatible with AArch32.")
532    endif
533
534    ifdef EL3_PAYLOAD_BASE
535        $(warning "SPD and EL3_PAYLOAD_BASE are incompatible build options.")
536        $(warning "The SPD and its BL32 companion will be present but ignored.")
537    endif
538
539    ifeq (${SPD},spmd)
540        # SPMD is located in std_svc directory
541        SPD_DIR := std_svc
542
543        ifeq ($(SPMD_SPM_AT_SEL2),1)
544            ifeq ($(CTX_INCLUDE_EL2_REGS),0)
545                $(error SPMD with SPM at S-EL2 requires CTX_INCLUDE_EL2_REGS option)
546            endif
547        endif
548
549        ifeq ($(findstring optee_sp,$(ARM_SPMC_MANIFEST_DTS)),optee_sp)
550            DTC_CPPFLAGS	+=	-DOPTEE_SP_FW_CONFIG
551        endif
552
553        ifeq ($(TS_SP_FW_CONFIG),1)
554            DTC_CPPFLAGS	+=	-DTS_SP_FW_CONFIG
555        endif
556
557        ifneq ($(ARM_BL2_SP_LIST_DTS),)
558            DTC_CPPFLAGS += -DARM_BL2_SP_LIST_DTS=$(ARM_BL2_SP_LIST_DTS)
559        endif
560
561        ifneq ($(SP_LAYOUT_FILE),)
562            BL2_ENABLE_SP_LOAD := 1
563        endif
564    else
565        # All other SPDs in spd directory
566        SPD_DIR := spd
567    endif
568
569    # We expect to locate an spd.mk under the specified SPD directory
570    SPD_MAKE	:=	$(wildcard services/${SPD_DIR}/${SPD}/${SPD}.mk)
571
572    ifeq (${SPD_MAKE},)
573        $(error Error: No services/${SPD_DIR}/${SPD}/${SPD}.mk located)
574    endif
575    $(info Including ${SPD_MAKE})
576    include ${SPD_MAKE}
577
578    # If there's BL32 companion for the chosen SPD, we expect that the SPD's
579    # Makefile would set NEED_BL32 to "yes". In this case, the build system
580    # supports two mutually exclusive options:
581    # * BL32 is built from source: then BL32_SOURCES must contain the list
582    #   of source files to build BL32
583    # * BL32 is a prebuilt binary: then BL32 must point to the image file
584    #   that will be included in the FIP
585    # If both BL32_SOURCES and BL32 are defined, the binary takes precedence
586    # over the sources.
587endif
588
589################################################################################
590# Include rmmd Makefile if RME is enabled
591################################################################################
592
593ifneq (${ENABLE_RME},0)
594ifneq (${ARCH},aarch64)
595	$(error ENABLE_RME requires AArch64)
596endif
597include services/std_svc/rmmd/rmmd.mk
598$(warning "RME is an experimental feature")
599endif
600
601################################################################################
602# Include the platform specific Makefile after the SPD Makefile (the platform
603# makefile may use all previous definitions in this file)
604################################################################################
605
606include ${PLAT_MAKEFILE_FULL}
607
608$(eval $(call MAKE_PREREQ_DIR,${BUILD_PLAT}))
609
610ifeq (${ARM_ARCH_MAJOR},7)
611include make_helpers/armv7-a-cpus.mk
612endif
613
614PIE_FOUND		:=	$(findstring --enable-default-pie,${GCC_V_OUTPUT})
615ifneq ($(PIE_FOUND),)
616	TF_CFLAGS	+=	-fno-PIE
617endif
618
619ifneq ($(findstring gcc,$(notdir $(LD))),)
620	PIE_LDFLAGS	+=	-Wl,-pie -Wl,--no-dynamic-linker
621else
622	PIE_LDFLAGS	+=	-pie --no-dynamic-linker
623endif
624
625ifeq ($(ENABLE_PIE),1)
626ifeq ($(BL2_AT_EL3),1)
627ifneq ($(BL2_IN_XIP_MEM),1)
628	BL2_CFLAGS	+=	-fpie
629	BL2_LDFLAGS	+=	$(PIE_LDFLAGS)
630endif
631endif
632	BL31_CFLAGS	+=	-fpie
633	BL31_LDFLAGS	+=	$(PIE_LDFLAGS)
634	BL32_CFLAGS	+=	-fpie
635	BL32_LDFLAGS	+=	$(PIE_LDFLAGS)
636endif
637
638ifeq (${ARCH},aarch64)
639BL1_CPPFLAGS += -DIMAGE_AT_EL3
640ifeq ($(BL2_AT_EL3),1)
641BL2_CPPFLAGS += -DIMAGE_AT_EL3
642else
643BL2_CPPFLAGS += -DIMAGE_AT_EL1
644endif
645BL2U_CPPFLAGS += -DIMAGE_AT_EL1
646BL31_CPPFLAGS += -DIMAGE_AT_EL3
647BL32_CPPFLAGS += -DIMAGE_AT_EL1
648endif
649
650# Include the CPU specific operations makefile, which provides default
651# values for all CPU errata workarounds and CPU specific optimisations.
652# This can be overridden by the platform.
653include lib/cpus/cpu-ops.mk
654
655ifeq (${ARCH},aarch32)
656NEED_BL32 := yes
657
658################################################################################
659# Build `AARCH32_SP` as BL32 image for AArch32
660################################################################################
661ifneq (${AARCH32_SP},none)
662# We expect to locate an sp.mk under the specified AARCH32_SP directory
663AARCH32_SP_MAKE	:=	$(wildcard bl32/${AARCH32_SP}/${AARCH32_SP}.mk)
664
665ifeq (${AARCH32_SP_MAKE},)
666  $(error Error: No bl32/${AARCH32_SP}/${AARCH32_SP}.mk located)
667endif
668
669$(info Including ${AARCH32_SP_MAKE})
670include ${AARCH32_SP_MAKE}
671endif
672
673endif
674
675################################################################################
676# Include libc if not overridden
677################################################################################
678ifeq (${OVERRIDE_LIBC},0)
679include lib/libc/libc.mk
680endif
681
682################################################################################
683# Check incompatible options
684################################################################################
685
686ifdef EL3_PAYLOAD_BASE
687        ifdef PRELOADED_BL33_BASE
688                $(warning "PRELOADED_BL33_BASE and EL3_PAYLOAD_BASE are \
689                incompatible build options. EL3_PAYLOAD_BASE has priority.")
690        endif
691        ifneq (${GENERATE_COT},0)
692                $(error "GENERATE_COT and EL3_PAYLOAD_BASE are incompatible build options.")
693        endif
694        ifneq (${TRUSTED_BOARD_BOOT},0)
695                $(error "TRUSTED_BOARD_BOOT and EL3_PAYLOAD_BASE are incompatible build options.")
696        endif
697endif
698
699ifeq (${NEED_BL33},yes)
700        ifdef EL3_PAYLOAD_BASE
701                $(warning "BL33 image is not needed when option \
702                BL33_PAYLOAD_BASE is used and won't be added to the FIP file.")
703        endif
704        ifdef PRELOADED_BL33_BASE
705                $(warning "BL33 image is not needed when option \
706                PRELOADED_BL33_BASE is used and won't be added to the FIP \
707                file.")
708        endif
709endif
710
711# When building for systems with hardware-assisted coherency, there's no need to
712# use USE_COHERENT_MEM. Require that USE_COHERENT_MEM must be set to 0 too.
713ifeq ($(HW_ASSISTED_COHERENCY)-$(USE_COHERENT_MEM),1-1)
714$(error USE_COHERENT_MEM cannot be enabled with HW_ASSISTED_COHERENCY)
715endif
716
717#For now, BL2_IN_XIP_MEM is only supported when BL2_AT_EL3 is 1.
718ifeq ($(BL2_AT_EL3)-$(BL2_IN_XIP_MEM),0-1)
719$(error "BL2_IN_XIP_MEM is only supported when BL2_AT_EL3 is enabled")
720endif
721
722# For RAS_EXTENSION, require that EAs are handled in EL3 first
723ifeq ($(RAS_EXTENSION),1)
724    ifneq ($(HANDLE_EA_EL3_FIRST),1)
725        $(error For RAS_EXTENSION, HANDLE_EA_EL3_FIRST must also be 1)
726    endif
727endif
728
729# When FAULT_INJECTION_SUPPORT is used, require that RAS_EXTENSION is enabled
730ifeq ($(FAULT_INJECTION_SUPPORT),1)
731    ifneq ($(RAS_EXTENSION),1)
732        $(error For FAULT_INJECTION_SUPPORT, RAS_EXTENSION must also be 1)
733    endif
734endif
735
736# DYN_DISABLE_AUTH can be set only when TRUSTED_BOARD_BOOT=1
737ifeq ($(DYN_DISABLE_AUTH), 1)
738    ifeq (${TRUSTED_BOARD_BOOT}, 0)
739        $(error "TRUSTED_BOARD_BOOT must be enabled for DYN_DISABLE_AUTH to be set.")
740    endif
741endif
742
743ifneq ($(filter 1,${MEASURED_BOOT} ${TRUSTED_BOARD_BOOT}),)
744    CRYPTO_SUPPORT := 1
745else
746    CRYPTO_SUPPORT := 0
747endif
748
749# SDEI_IN_FCONF is only supported when SDEI_SUPPORT is enabled.
750ifeq ($(SDEI_SUPPORT)-$(SDEI_IN_FCONF),0-1)
751$(error "SDEI_IN_FCONF is only supported when SDEI_SUPPORT is enabled")
752endif
753
754# If pointer authentication is used in the firmware, make sure that all the
755# registers associated to it are also saved and restored.
756# Not doing it would leak the value of the keys used by EL3 to EL1 and S-EL1.
757ifeq ($(ENABLE_PAUTH),1)
758    ifeq ($(CTX_INCLUDE_PAUTH_REGS),0)
759        $(error Pointer Authentication requires CTX_INCLUDE_PAUTH_REGS=1)
760    endif
761endif
762
763ifeq ($(CTX_INCLUDE_PAUTH_REGS),1)
764    ifneq (${ARCH},aarch64)
765        $(error CTX_INCLUDE_PAUTH_REGS requires AArch64)
766    endif
767endif
768
769ifeq ($(CTX_INCLUDE_MTE_REGS),1)
770    ifneq (${ARCH},aarch64)
771        $(error CTX_INCLUDE_MTE_REGS requires AArch64)
772    endif
773endif
774
775ifeq ($(PSA_FWU_SUPPORT),1)
776    $(info PSA_FWU_SUPPORT is an experimental feature)
777endif
778
779ifeq (${ARM_XLAT_TABLES_LIB_V1}, 1)
780    ifeq (${ALLOW_RO_XLAT_TABLES}, 1)
781        $(error "ALLOW_RO_XLAT_TABLES requires translation tables library v2")
782    endif
783endif
784
785ifneq (${DECRYPTION_SUPPORT},none)
786    ifeq (${TRUSTED_BOARD_BOOT}, 0)
787        $(error TRUSTED_BOARD_BOOT must be enabled for DECRYPTION_SUPPORT to be set)
788    endif
789endif
790
791# SME/SVE only supported on AArch64
792ifeq (${ARCH},aarch32)
793    ifeq (${ENABLE_SME_FOR_NS},1)
794        $(error "ENABLE_SME_FOR_NS cannot be used with ARCH=aarch32")
795    endif
796    ifeq (${ENABLE_SVE_FOR_NS},1)
797        # Warning instead of error due to CI dependency on this
798        $(error "ENABLE_SVE_FOR_NS cannot be used with ARCH=aarch32")
799    endif
800endif
801
802# Ensure ENABLE_RME is not used with SME
803ifeq (${ENABLE_RME},1)
804    ifeq (${ENABLE_SME_FOR_NS},1)
805        $(error "ENABLE_SME_FOR_NS cannot be used with ENABLE_RME")
806    endif
807endif
808
809# Secure SME/SVE requires the non-secure component as well
810ifeq (${ENABLE_SME_FOR_SWD},1)
811    ifeq (${ENABLE_SME_FOR_NS},0)
812        $(error "ENABLE_SME_FOR_SWD requires ENABLE_SME_FOR_NS")
813    endif
814endif
815ifeq (${ENABLE_SVE_FOR_SWD},1)
816    ifeq (${ENABLE_SVE_FOR_NS},0)
817        $(error "ENABLE_SVE_FOR_SWD requires ENABLE_SVE_FOR_NS")
818    endif
819endif
820
821# SVE and SME cannot be used with CTX_INCLUDE_FPREGS since secure manager does
822# its own context management including FPU registers.
823ifeq (${CTX_INCLUDE_FPREGS},1)
824    ifeq (${ENABLE_SME_FOR_NS},1)
825        $(error "ENABLE_SME_FOR_NS cannot be used with CTX_INCLUDE_FPREGS")
826    endif
827    ifeq (${ENABLE_SVE_FOR_NS},1)
828        # Warning instead of error due to CI dependency on this
829        $(warning "ENABLE_SVE_FOR_NS cannot be used with CTX_INCLUDE_FPREGS")
830        $(warning "Forced ENABLE_SVE_FOR_NS=0")
831        override ENABLE_SVE_FOR_NS	:= 0
832    endif
833endif
834
835################################################################################
836# Process platform overrideable behaviour
837################################################################################
838
839ifdef BL1_SOURCES
840NEED_BL1 := yes
841endif
842
843ifdef BL2_SOURCES
844	NEED_BL2 := yes
845
846	# Using BL2 implies that a BL33 image also needs to be supplied for the FIP and
847	# Certificate generation tools. This flag can be overridden by the platform.
848	ifdef EL3_PAYLOAD_BASE
849                # If booting an EL3 payload there is no need for a BL33 image
850                # in the FIP file.
851                NEED_BL33		:=	no
852        else
853                ifdef PRELOADED_BL33_BASE
854                        # If booting a BL33 preloaded image there is no need of
855                        # another one in the FIP file.
856                        NEED_BL33		:=	no
857                else
858                        NEED_BL33		?=	yes
859                endif
860        endif
861endif
862
863ifdef BL2U_SOURCES
864NEED_BL2U := yes
865endif
866
867# If SCP_BL2 is given, we always want FIP to include it.
868ifdef SCP_BL2
869        NEED_SCP_BL2		:=	yes
870endif
871
872# For AArch32, BL31 is not currently supported.
873ifneq (${ARCH},aarch32)
874    ifdef BL31_SOURCES
875        # When booting an EL3 payload, there is no need to compile the BL31 image nor
876        # put it in the FIP.
877        ifndef EL3_PAYLOAD_BASE
878            NEED_BL31 := yes
879        endif
880    endif
881endif
882
883# Process TBB related flags
884ifneq (${GENERATE_COT},0)
885        # Common cert_create options
886        ifneq (${CREATE_KEYS},0)
887                $(eval CRT_ARGS += -n)
888                $(eval FWU_CRT_ARGS += -n)
889                ifneq (${SAVE_KEYS},0)
890                        $(eval CRT_ARGS += -k)
891                        $(eval FWU_CRT_ARGS += -k)
892                endif
893        endif
894        # Include TBBR makefile (unless the platform indicates otherwise)
895        ifeq (${INCLUDE_TBBR_MK},1)
896                include make_helpers/tbbr/tbbr_tools.mk
897        endif
898endif
899
900ifneq (${FIP_ALIGN},0)
901FIP_ARGS += --align ${FIP_ALIGN}
902endif
903
904ifdef FDT_SOURCES
905NEED_FDT := yes
906endif
907
908################################################################################
909# Include libraries' Makefile that are used in all BL
910################################################################################
911
912include lib/stack_protector/stack_protector.mk
913
914################################################################################
915# Auxiliary tools (fiptool, cert_create, etc)
916################################################################################
917
918# Variables for use with Certificate Generation Tool
919CRTTOOLPATH		?=	tools/cert_create
920CRTTOOL			?=	${CRTTOOLPATH}/cert_create${BIN_EXT}
921
922# Variables for use with Firmware Encryption Tool
923ENCTOOLPATH		?=	tools/encrypt_fw
924ENCTOOL			?=	${ENCTOOLPATH}/encrypt_fw${BIN_EXT}
925
926# Variables for use with Firmware Image Package
927FIPTOOLPATH		?=	tools/fiptool
928FIPTOOL			?=	${FIPTOOLPATH}/fiptool${BIN_EXT}
929
930# Variables for use with sptool
931SPTOOLPATH		?=	tools/sptool
932SPTOOL			?=	${SPTOOLPATH}/sptool${BIN_EXT}
933SP_MK_GEN		?=	${SPTOOLPATH}/sp_mk_generator.py
934
935# Variables for use with ROMLIB
936ROMLIBPATH		?=	lib/romlib
937
938# Variable for use with Python
939PYTHON			?=	python3
940
941# Variables for use with PRINT_MEMORY_MAP
942PRINT_MEMORY_MAP_PATH		?=	tools/memory
943PRINT_MEMORY_MAP		?=	${PRINT_MEMORY_MAP_PATH}/print_memory_map.py
944
945# Variables for use with documentation build using Sphinx tool
946DOCS_PATH		?=	docs
947
948################################################################################
949# Include BL specific makefiles
950################################################################################
951
952ifeq (${NEED_BL1},yes)
953include bl1/bl1.mk
954endif
955
956ifeq (${NEED_BL2},yes)
957include bl2/bl2.mk
958endif
959
960ifeq (${NEED_BL2U},yes)
961include bl2u/bl2u.mk
962endif
963
964ifeq (${NEED_BL31},yes)
965include bl31/bl31.mk
966endif
967
968################################################################################
969# Build options checks
970################################################################################
971
972$(eval $(call assert_booleans,\
973    $(sort \
974        ALLOW_RO_XLAT_TABLES \
975        BL2_ENABLE_SP_LOAD \
976        COLD_BOOT_SINGLE_CPU \
977        CREATE_KEYS \
978        CTX_INCLUDE_AARCH32_REGS \
979        CTX_INCLUDE_FPREGS \
980        CTX_INCLUDE_PAUTH_REGS \
981        CTX_INCLUDE_MTE_REGS \
982        CTX_INCLUDE_EL2_REGS \
983        CTX_INCLUDE_NEVE_REGS \
984        DEBUG \
985        DISABLE_MTPMU \
986        DYN_DISABLE_AUTH \
987        EL3_EXCEPTION_HANDLING \
988        ENABLE_AMU \
989        ENABLE_AMU_AUXILIARY_COUNTERS \
990        ENABLE_AMU_FCONF \
991        AMU_RESTRICT_COUNTERS \
992        ENABLE_ASSERTIONS \
993        ENABLE_MPAM_FOR_LOWER_ELS \
994        ENABLE_PIE \
995        ENABLE_PMF \
996        ENABLE_PSCI_STAT \
997        ENABLE_RME \
998        ENABLE_RUNTIME_INSTRUMENTATION \
999        ENABLE_SME_FOR_NS \
1000        ENABLE_SME_FOR_SWD \
1001        ENABLE_SPE_FOR_LOWER_ELS \
1002        ENABLE_SVE_FOR_NS \
1003        ENABLE_SVE_FOR_SWD \
1004        ERROR_DEPRECATED \
1005        FAULT_INJECTION_SUPPORT \
1006        GENERATE_COT \
1007        GICV2_G0_FOR_EL3 \
1008        HANDLE_EA_EL3_FIRST \
1009        HW_ASSISTED_COHERENCY \
1010        INVERTED_MEMMAP \
1011        MEASURED_BOOT \
1012        NS_TIMER_SWITCH \
1013        OVERRIDE_LIBC \
1014        PL011_GENERIC_UART \
1015        PROGRAMMABLE_RESET_ADDRESS \
1016        PSCI_EXTENDED_STATE_ID \
1017        RAS_EXTENSION \
1018        RESET_TO_BL31 \
1019        SAVE_KEYS \
1020        SEPARATE_CODE_AND_RODATA \
1021        SEPARATE_NOBITS_REGION \
1022        SPIN_ON_BL1_EXIT \
1023        SPM_MM \
1024        SPMD_SPM_AT_SEL2 \
1025        TRUSTED_BOARD_BOOT \
1026        CRYPTO_SUPPORT \
1027        USE_COHERENT_MEM \
1028        USE_DEBUGFS \
1029        ARM_IO_IN_DTB \
1030        SDEI_IN_FCONF \
1031        SEC_INT_DESC_IN_FCONF \
1032        USE_ROMLIB \
1033        USE_TBBR_DEFS \
1034        WARMBOOT_ENABLE_DCACHE_EARLY \
1035        BL2_AT_EL3 \
1036        BL2_IN_XIP_MEM \
1037        BL2_INV_DCACHE \
1038        USE_SPINLOCK_CAS \
1039        ENCRYPT_BL31 \
1040        ENCRYPT_BL32 \
1041        ERRATA_SPECULATIVE_AT \
1042        RAS_TRAP_LOWER_EL_ERR_ACCESS \
1043        COT_DESC_IN_DTB \
1044        USE_SP804_TIMER \
1045        ENABLE_FEAT_RNG \
1046        ENABLE_FEAT_SB \
1047        ENABLE_FEAT_DIT \
1048        PSA_FWU_SUPPORT \
1049        ENABLE_TRBE_FOR_NS \
1050        ENABLE_SYS_REG_TRACE_FOR_NS \
1051        ENABLE_TRF_FOR_NS \
1052        ENABLE_FEAT_HCX \
1053        ENABLE_MPMM \
1054        ENABLE_MPMM_FCONF \
1055        ENABLE_FEAT_FGT \
1056        ENABLE_FEAT_AMUv1 \
1057        ENABLE_FEAT_ECV \
1058)))
1059
1060$(eval $(call assert_numerics,\
1061    $(sort \
1062        ARM_ARCH_MAJOR \
1063        ARM_ARCH_MINOR \
1064        BRANCH_PROTECTION \
1065        FW_ENC_STATUS \
1066        NR_OF_FW_BANKS \
1067        NR_OF_IMAGES_IN_FW_BANK \
1068)))
1069
1070ifdef KEY_SIZE
1071        $(eval $(call assert_numeric,KEY_SIZE))
1072endif
1073
1074ifeq ($(filter $(SANITIZE_UB), on off trap),)
1075        $(error "Invalid value for SANITIZE_UB: can be one of on, off, trap")
1076endif
1077
1078################################################################################
1079# Add definitions to the cpp preprocessor based on the current build options.
1080# This is done after including the platform specific makefile to allow the
1081# platform to overwrite the default options
1082################################################################################
1083
1084$(eval $(call add_defines,\
1085    $(sort \
1086        ALLOW_RO_XLAT_TABLES \
1087        ARM_ARCH_MAJOR \
1088        ARM_ARCH_MINOR \
1089        BL2_ENABLE_SP_LOAD \
1090        COLD_BOOT_SINGLE_CPU \
1091        CTX_INCLUDE_AARCH32_REGS \
1092        CTX_INCLUDE_FPREGS \
1093        CTX_INCLUDE_PAUTH_REGS \
1094        EL3_EXCEPTION_HANDLING \
1095        CTX_INCLUDE_MTE_REGS \
1096        CTX_INCLUDE_EL2_REGS \
1097        CTX_INCLUDE_NEVE_REGS \
1098        DECRYPTION_SUPPORT_${DECRYPTION_SUPPORT} \
1099        DISABLE_MTPMU \
1100        ENABLE_AMU \
1101        ENABLE_AMU_AUXILIARY_COUNTERS \
1102        ENABLE_AMU_FCONF \
1103        AMU_RESTRICT_COUNTERS \
1104        ENABLE_ASSERTIONS \
1105        ENABLE_BTI \
1106        ENABLE_MPAM_FOR_LOWER_ELS \
1107        ENABLE_PAUTH \
1108        ENABLE_PIE \
1109        ENABLE_PMF \
1110        ENABLE_PSCI_STAT \
1111        ENABLE_RME \
1112        ENABLE_RUNTIME_INSTRUMENTATION \
1113        ENABLE_SME_FOR_NS \
1114        ENABLE_SME_FOR_SWD \
1115        ENABLE_SPE_FOR_LOWER_ELS \
1116        ENABLE_SVE_FOR_NS \
1117        ENABLE_SVE_FOR_SWD \
1118        ENCRYPT_BL31 \
1119        ENCRYPT_BL32 \
1120        ERROR_DEPRECATED \
1121        FAULT_INJECTION_SUPPORT \
1122        GICV2_G0_FOR_EL3 \
1123        HANDLE_EA_EL3_FIRST \
1124        HW_ASSISTED_COHERENCY \
1125        LOG_LEVEL \
1126        MEASURED_BOOT \
1127        NS_TIMER_SWITCH \
1128        PL011_GENERIC_UART \
1129        PLAT_${PLAT} \
1130        PROGRAMMABLE_RESET_ADDRESS \
1131        PSCI_EXTENDED_STATE_ID \
1132        RAS_EXTENSION \
1133        RESET_TO_BL31 \
1134        SEPARATE_CODE_AND_RODATA \
1135        SEPARATE_NOBITS_REGION \
1136        RECLAIM_INIT_CODE \
1137        SPD_${SPD} \
1138        SPIN_ON_BL1_EXIT \
1139        SPM_MM \
1140        SPMD_SPM_AT_SEL2 \
1141        TRUSTED_BOARD_BOOT \
1142        CRYPTO_SUPPORT \
1143        TRNG_SUPPORT \
1144        USE_COHERENT_MEM \
1145        USE_DEBUGFS \
1146        ARM_IO_IN_DTB \
1147        SDEI_IN_FCONF \
1148        SEC_INT_DESC_IN_FCONF \
1149        USE_ROMLIB \
1150        USE_TBBR_DEFS \
1151        WARMBOOT_ENABLE_DCACHE_EARLY \
1152        BL2_AT_EL3 \
1153        BL2_IN_XIP_MEM \
1154        BL2_INV_DCACHE \
1155        USE_SPINLOCK_CAS \
1156        ERRATA_SPECULATIVE_AT \
1157        RAS_TRAP_LOWER_EL_ERR_ACCESS \
1158        COT_DESC_IN_DTB \
1159        USE_SP804_TIMER \
1160        ENABLE_FEAT_RNG \
1161        ENABLE_FEAT_SB \
1162        ENABLE_FEAT_DIT \
1163        NR_OF_FW_BANKS \
1164        NR_OF_IMAGES_IN_FW_BANK \
1165        PSA_FWU_SUPPORT \
1166        ENABLE_TRBE_FOR_NS \
1167        ENABLE_SYS_REG_TRACE_FOR_NS \
1168        ENABLE_TRF_FOR_NS \
1169        ENABLE_FEAT_HCX \
1170        ENABLE_MPMM \
1171        ENABLE_MPMM_FCONF \
1172        ENABLE_FEAT_FGT \
1173        ENABLE_FEAT_AMUv1 \
1174        ENABLE_FEAT_ECV \
1175)))
1176
1177ifeq (${SANITIZE_UB},trap)
1178        $(eval $(call add_define,MONITOR_TRAPS))
1179endif
1180
1181# Define the EL3_PAYLOAD_BASE flag only if it is provided.
1182ifdef EL3_PAYLOAD_BASE
1183        $(eval $(call add_define,EL3_PAYLOAD_BASE))
1184else
1185        # Define the PRELOADED_BL33_BASE flag only if it is provided and
1186        # EL3_PAYLOAD_BASE is not defined, as it has priority.
1187        ifdef PRELOADED_BL33_BASE
1188                $(eval $(call add_define,PRELOADED_BL33_BASE))
1189        endif
1190endif
1191
1192# Define the DYN_DISABLE_AUTH flag only if set.
1193ifeq (${DYN_DISABLE_AUTH},1)
1194$(eval $(call add_define,DYN_DISABLE_AUTH))
1195endif
1196
1197ifneq ($(findstring armlink,$(notdir $(LD))),)
1198$(eval $(call add_define,USE_ARM_LINK))
1199endif
1200
1201# Generate and include sp_gen.mk if SPD is spmd and SP_LAYOUT_FILE is defined
1202ifeq (${SPD},spmd)
1203ifdef SP_LAYOUT_FILE
1204        -include $(BUILD_PLAT)/sp_gen.mk
1205        FIP_DEPS += sp
1206        CRT_DEPS += sp
1207        NEED_SP_PKG := yes
1208else
1209        ifeq (${SPMD_SPM_AT_SEL2},1)
1210            $(error "SPMD with SPM at S-EL2 require SP_LAYOUT_FILE")
1211        endif
1212endif
1213endif
1214
1215################################################################################
1216# Build targets
1217################################################################################
1218
1219.PHONY:	all msg_start clean realclean distclean cscope locate-checkpatch checkcodebase checkpatch fiptool sptool fip sp fwu_fip certtool dtbs memmap doc enctool
1220.SUFFIXES:
1221
1222all: msg_start
1223
1224msg_start:
1225	@echo "Building ${PLAT}"
1226
1227ifeq (${ERROR_DEPRECATED},0)
1228# Check if deprecated declarations and cpp warnings should be treated as error or not.
1229ifneq ($(findstring clang,$(notdir $(CC))),)
1230    CPPFLAGS		+= 	-Wno-error=deprecated-declarations
1231else
1232    CPPFLAGS		+= 	-Wno-error=deprecated-declarations -Wno-error=cpp
1233endif
1234endif # !ERROR_DEPRECATED
1235
1236$(eval $(call MAKE_LIB_DIRS))
1237$(eval $(call MAKE_LIB,c))
1238
1239# Expand build macros for the different images
1240ifeq (${NEED_BL1},yes)
1241BL1_SOURCES := $(sort ${BL1_SOURCES})
1242
1243$(eval $(call MAKE_BL,bl1))
1244endif
1245
1246ifeq (${NEED_BL2},yes)
1247ifeq (${BL2_AT_EL3}, 0)
1248FIP_BL2_ARGS := tb-fw
1249endif
1250
1251BL2_SOURCES := $(sort ${BL2_SOURCES})
1252
1253$(if ${BL2}, $(eval $(call TOOL_ADD_IMG,bl2,--${FIP_BL2_ARGS})),\
1254	$(eval $(call MAKE_BL,bl2,${FIP_BL2_ARGS})))
1255endif
1256
1257ifeq (${NEED_SCP_BL2},yes)
1258$(eval $(call TOOL_ADD_IMG,scp_bl2,--scp-fw))
1259endif
1260
1261ifeq (${NEED_BL31},yes)
1262BL31_SOURCES += ${SPD_SOURCES}
1263# Sort BL31 source files to remove duplicates
1264BL31_SOURCES := $(sort ${BL31_SOURCES})
1265ifneq (${DECRYPTION_SUPPORT},none)
1266$(if ${BL31}, $(eval $(call TOOL_ADD_IMG,bl31,--soc-fw,,$(ENCRYPT_BL31))),\
1267	$(eval $(call MAKE_BL,bl31,soc-fw,,$(ENCRYPT_BL31))))
1268else
1269$(if ${BL31}, $(eval $(call TOOL_ADD_IMG,bl31,--soc-fw)),\
1270	$(eval $(call MAKE_BL,bl31,soc-fw)))
1271endif
1272endif
1273
1274# If a BL32 image is needed but neither BL32 nor BL32_SOURCES is defined, the
1275# build system will call TOOL_ADD_IMG to print a warning message and abort the
1276# process. Note that the dependency on BL32 applies to the FIP only.
1277ifeq (${NEED_BL32},yes)
1278# Sort BL32 source files to remove duplicates
1279BL32_SOURCES := $(sort ${BL32_SOURCES})
1280BUILD_BL32 := $(if $(BL32),,$(if $(BL32_SOURCES),1))
1281
1282ifneq (${DECRYPTION_SUPPORT},none)
1283$(if ${BUILD_BL32}, $(eval $(call MAKE_BL,bl32,tos-fw,,$(ENCRYPT_BL32))),\
1284	$(eval $(call TOOL_ADD_IMG,bl32,--tos-fw,,$(ENCRYPT_BL32))))
1285else
1286$(if ${BUILD_BL32}, $(eval $(call MAKE_BL,bl32,tos-fw)),\
1287	$(eval $(call TOOL_ADD_IMG,bl32,--tos-fw)))
1288endif
1289endif
1290
1291# If RMM image is needed but RMM is not defined, Test Realm Payload (TRP)
1292# needs to be built from RMM_SOURCES.
1293ifeq (${NEED_RMM},yes)
1294# Sort RMM source files to remove duplicates
1295RMM_SOURCES := $(sort ${RMM_SOURCES})
1296BUILD_RMM := $(if $(RMM),,$(if $(RMM_SOURCES),1))
1297
1298$(if ${BUILD_RMM}, $(eval $(call MAKE_BL,rmm,rmm-fw)),\
1299         $(eval $(call TOOL_ADD_IMG,rmm,--rmm-fw)))
1300endif
1301
1302# Add the BL33 image if required by the platform
1303ifeq (${NEED_BL33},yes)
1304$(eval $(call TOOL_ADD_IMG,bl33,--nt-fw))
1305endif
1306
1307ifeq (${NEED_BL2U},yes)
1308$(if ${BL2U}, $(eval $(call TOOL_ADD_IMG,bl2u,--ap-fwu-cfg,FWU_)),\
1309	$(eval $(call MAKE_BL,bl2u,ap-fwu-cfg,FWU_)))
1310endif
1311
1312# Expand build macros for the different images
1313ifeq (${NEED_FDT},yes)
1314    $(eval $(call MAKE_DTBS,$(BUILD_PLAT)/fdts,$(FDT_SOURCES)))
1315endif
1316
1317# Add Secure Partition packages
1318ifeq (${NEED_SP_PKG},yes)
1319$(BUILD_PLAT)/sp_gen.mk: ${SP_MK_GEN} ${SP_LAYOUT_FILE} | ${BUILD_PLAT}
1320	${Q}${PYTHON} "$<" "$@" $(filter-out $<,$^) $(BUILD_PLAT) ${COT}
1321sp: $(SPTOOL) $(DTBS) $(BUILD_PLAT)/sp_gen.mk
1322	${Q}$(SPTOOL) $(SPTOOL_ARGS)
1323	@${ECHO_BLANK_LINE}
1324	@echo "Built SP Images successfully"
1325	@${ECHO_BLANK_LINE}
1326endif
1327
1328locate-checkpatch:
1329ifndef CHECKPATCH
1330	$(error "Please set CHECKPATCH to point to the Linux checkpatch.pl file, eg: CHECKPATCH=../linux/scripts/checkpatch.pl")
1331else
1332ifeq (,$(wildcard ${CHECKPATCH}))
1333	$(error "The file CHECKPATCH points to cannot be found, use eg: CHECKPATCH=../linux/scripts/checkpatch.pl")
1334endif
1335endif
1336
1337clean:
1338	@echo "  CLEAN"
1339	$(call SHELL_REMOVE_DIR,${BUILD_PLAT})
1340ifdef UNIX_MK
1341	${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean
1342else
1343# Clear the MAKEFLAGS as we do not want
1344# to pass the gnumake flags to nmake.
1345	${Q}set MAKEFLAGS= && ${MSVC_NMAKE} /nologo /f ${FIPTOOLPATH}/Makefile.msvc FIPTOOLPATH=$(subst /,\,$(FIPTOOLPATH)) FIPTOOL=$(subst /,\,$(FIPTOOL)) clean
1346endif
1347	${Q}${MAKE} PLAT=${PLAT} --no-print-directory -C ${CRTTOOLPATH} clean
1348	${Q}${MAKE} PLAT=${PLAT} --no-print-directory -C ${ENCTOOLPATH} clean
1349	${Q}${MAKE} --no-print-directory -C ${ROMLIBPATH} clean
1350
1351realclean distclean:
1352	@echo "  REALCLEAN"
1353	$(call SHELL_REMOVE_DIR,${BUILD_BASE})
1354	$(call SHELL_DELETE_ALL, ${CURDIR}/cscope.*)
1355ifdef UNIX_MK
1356	${Q}${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean
1357else
1358# Clear the MAKEFLAGS as we do not want
1359# to pass the gnumake flags to nmake.
1360	${Q}set MAKEFLAGS= && ${MSVC_NMAKE} /nologo /f ${FIPTOOLPATH}/Makefile.msvc FIPTOOLPATH=$(subst /,\,$(FIPTOOLPATH)) FIPTOOL=$(subst /,\,$(FIPTOOL)) realclean
1361endif
1362	${Q}${MAKE} --no-print-directory -C ${SPTOOLPATH} clean
1363	${Q}${MAKE} PLAT=${PLAT} --no-print-directory -C ${CRTTOOLPATH} clean
1364	${Q}${MAKE} PLAT=${PLAT} --no-print-directory -C ${ENCTOOLPATH} realclean
1365	${Q}${MAKE} --no-print-directory -C ${ROMLIBPATH} clean
1366
1367checkcodebase:		locate-checkpatch
1368	@echo "  CHECKING STYLE"
1369	@if test -d .git ; then						\
1370		git ls-files | grep -E -v 'libfdt|libc|docs|\.rst' |	\
1371		while read GIT_FILE ;					\
1372		do ${CHECKPATCH} ${CHECKCODE_ARGS} -f $$GIT_FILE ;	\
1373		done ;							\
1374	else								\
1375		 find . -type f -not -iwholename "*.git*"		\
1376		 -not -iwholename "*build*"				\
1377		 -not -iwholename "*libfdt*"				\
1378		 -not -iwholename "*libc*"				\
1379		 -not -iwholename "*docs*"				\
1380		 -not -iwholename "*.rst"				\
1381		 -exec ${CHECKPATCH} ${CHECKCODE_ARGS} -f {} \; ;	\
1382	fi
1383
1384checkpatch:		locate-checkpatch
1385	@echo "  CHECKING STYLE"
1386	@if test -n "${CHECKPATCH_OPTS}"; then				\
1387		echo "    with ${CHECKPATCH_OPTS} option(s)";		\
1388	fi
1389	${Q}COMMON_COMMIT=$$(git merge-base HEAD ${BASE_COMMIT});	\
1390	for commit in `git rev-list --no-merges $$COMMON_COMMIT..HEAD`;	\
1391	do								\
1392		printf "\n[*] Checking style of '$$commit'\n\n";	\
1393		git log --format=email "$$commit~..$$commit"		\
1394			-- ${CHECK_PATHS} |				\
1395			${CHECKPATCH} ${CHECKPATCH_OPTS} - || true;	\
1396		git diff --format=email "$$commit~..$$commit"		\
1397			-- ${CHECK_PATHS} |				\
1398			${CHECKPATCH}  ${CHECKPATCH_OPTS} - || true;	\
1399	done
1400
1401certtool: ${CRTTOOL}
1402
1403${CRTTOOL}: FORCE
1404	${Q}${MAKE} PLAT=${PLAT} USE_TBBR_DEFS=${USE_TBBR_DEFS} COT=${COT} OPENSSL_DIR=${OPENSSL_DIR} CRTTOOL=${CRTTOOL} --no-print-directory -C ${CRTTOOLPATH}
1405	@${ECHO_BLANK_LINE}
1406	@echo "Built $@ successfully"
1407	@${ECHO_BLANK_LINE}
1408
1409ifneq (${GENERATE_COT},0)
1410certificates: ${CRT_DEPS} ${CRTTOOL}
1411	${Q}${CRTTOOL} ${CRT_ARGS}
1412	@${ECHO_BLANK_LINE}
1413	@echo "Built $@ successfully"
1414	@echo "Certificates can be found in ${BUILD_PLAT}"
1415	@${ECHO_BLANK_LINE}
1416endif
1417
1418${BUILD_PLAT}/${FIP_NAME}: ${FIP_DEPS} ${FIPTOOL}
1419	$(eval ${CHECK_FIP_CMD})
1420	${Q}${FIPTOOL} create ${FIP_ARGS} $@
1421	${Q}${FIPTOOL} info $@
1422	@${ECHO_BLANK_LINE}
1423	@echo "Built $@ successfully"
1424	@${ECHO_BLANK_LINE}
1425
1426ifneq (${GENERATE_COT},0)
1427fwu_certificates: ${FWU_CRT_DEPS} ${CRTTOOL}
1428	${Q}${CRTTOOL} ${FWU_CRT_ARGS}
1429	@${ECHO_BLANK_LINE}
1430	@echo "Built $@ successfully"
1431	@echo "FWU certificates can be found in ${BUILD_PLAT}"
1432	@${ECHO_BLANK_LINE}
1433endif
1434
1435${BUILD_PLAT}/${FWU_FIP_NAME}: ${FWU_FIP_DEPS} ${FIPTOOL}
1436	$(eval ${CHECK_FWU_FIP_CMD})
1437	${Q}${FIPTOOL} create ${FWU_FIP_ARGS} $@
1438	${Q}${FIPTOOL} info $@
1439	@${ECHO_BLANK_LINE}
1440	@echo "Built $@ successfully"
1441	@${ECHO_BLANK_LINE}
1442
1443fiptool: ${FIPTOOL}
1444fip: ${BUILD_PLAT}/${FIP_NAME}
1445fwu_fip: ${BUILD_PLAT}/${FWU_FIP_NAME}
1446
1447${FIPTOOL}: FORCE
1448ifdef UNIX_MK
1449	${Q}${MAKE} CPPFLAGS="-DVERSION='\"${VERSION_STRING}\"'" FIPTOOL=${FIPTOOL} OPENSSL_DIR=${OPENSSL_DIR} --no-print-directory -C ${FIPTOOLPATH}
1450else
1451# Clear the MAKEFLAGS as we do not want
1452# to pass the gnumake flags to nmake.
1453	${Q}set MAKEFLAGS= && ${MSVC_NMAKE} /nologo /f ${FIPTOOLPATH}/Makefile.msvc FIPTOOLPATH=$(subst /,\,$(FIPTOOLPATH)) FIPTOOL=$(subst /,\,$(FIPTOOL))
1454endif
1455
1456sptool: ${SPTOOL}
1457${SPTOOL}: FORCE
1458	${Q}${MAKE} CPPFLAGS="-DVERSION='\"${VERSION_STRING}\"'" SPTOOL=${SPTOOL} --no-print-directory -C ${SPTOOLPATH}
1459
1460romlib.bin: libraries FORCE
1461	${Q}${MAKE} PLAT_DIR=${PLAT_DIR} BUILD_PLAT=${BUILD_PLAT} ENABLE_BTI=${ENABLE_BTI} ARM_ARCH_MINOR=${ARM_ARCH_MINOR} INCLUDES='${INCLUDES}' DEFINES='${DEFINES}' --no-print-directory -C ${ROMLIBPATH} all
1462
1463# Call print_memory_map tool
1464memmap: all
1465	${Q}${PYTHON} ${PRINT_MEMORY_MAP} ${BUILD_PLAT} ${INVERTED_MEMMAP}
1466
1467doc:
1468	@echo "  BUILD DOCUMENTATION"
1469	${Q}${MAKE} --no-print-directory -C ${DOCS_PATH} html
1470
1471enctool: ${ENCTOOL}
1472
1473${ENCTOOL}: FORCE
1474	${Q}${MAKE} PLAT=${PLAT} BUILD_INFO=0 OPENSSL_DIR=${OPENSSL_DIR} ENCTOOL=${ENCTOOL} --no-print-directory -C ${ENCTOOLPATH}
1475	@${ECHO_BLANK_LINE}
1476	@echo "Built $@ successfully"
1477	@${ECHO_BLANK_LINE}
1478
1479cscope:
1480	@echo "  CSCOPE"
1481	${Q}find ${CURDIR} -name "*.[chsS]" > cscope.files
1482	${Q}cscope -b -q -k
1483
1484help:
1485	@echo "usage: ${MAKE} [PLAT=<platform>] [OPTIONS] [TARGET]"
1486	@echo ""
1487	@echo "PLAT is used to specify which platform you wish to build."
1488	@echo "If no platform is specified, PLAT defaults to: ${DEFAULT_PLAT}"
1489	@echo ""
1490	@echo "platform = ${PLATFORM_LIST}"
1491	@echo ""
1492	@echo "Please refer to the User Guide for a list of all supported options."
1493	@echo "Note that the build system doesn't track dependencies for build "
1494	@echo "options. Therefore, if any of the build options are changed "
1495	@echo "from a previous build, a clean build must be performed."
1496	@echo ""
1497	@echo "Supported Targets:"
1498	@echo "  all            Build all individual bootloader binaries"
1499	@echo "  bl1            Build the BL1 binary"
1500	@echo "  bl2            Build the BL2 binary"
1501	@echo "  bl2u           Build the BL2U binary"
1502	@echo "  bl31           Build the BL31 binary"
1503	@echo "  bl32           Build the BL32 binary. If ARCH=aarch32, then "
1504	@echo "                 this builds secure payload specified by AARCH32_SP"
1505	@echo "  certificates   Build the certificates (requires 'GENERATE_COT=1')"
1506	@echo "  fip            Build the Firmware Image Package (FIP)"
1507	@echo "  fwu_fip        Build the FWU Firmware Image Package (FIP)"
1508	@echo "  checkcodebase  Check the coding style of the entire source tree"
1509	@echo "  checkpatch     Check the coding style on changes in the current"
1510	@echo "                 branch against BASE_COMMIT (default origin/master)"
1511	@echo "  clean          Clean the build for the selected platform"
1512	@echo "  cscope         Generate cscope index"
1513	@echo "  distclean      Remove all build artifacts for all platforms"
1514	@echo "  certtool       Build the Certificate generation tool"
1515	@echo "  enctool        Build the Firmware encryption tool"
1516	@echo "  fiptool        Build the Firmware Image Package (FIP) creation tool"
1517	@echo "  sp             Build the Secure Partition Packages"
1518	@echo "  sptool         Build the Secure Partition Package creation tool"
1519	@echo "  dtbs           Build the Device Tree Blobs (if required for the platform)"
1520	@echo "  memmap         Print the memory map of the built binaries"
1521	@echo "  doc            Build html based documentation using Sphinx tool"
1522	@echo ""
1523	@echo "Note: most build targets require PLAT to be set to a specific platform."
1524	@echo ""
1525	@echo "example: build all targets for the FVP platform:"
1526	@echo "  CROSS_COMPILE=aarch64-none-elf- make PLAT=fvp all"
1527
1528.PHONY: FORCE
1529FORCE:;
1530