xref: /aosp_15_r20/external/coreboot/Makefile.mk (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1# SPDX-License-Identifier: GPL-2.0-only
2
3ifneq ($(NOCOMPILE),1)
4GIT:=$(shell git -C "$(top)" rev-parse --git-dir 1>/dev/null 2>&1 \
5	&& command -v git)
6else
7GIT:=
8endif
9
10#######################################################################
11# normalize Kconfig variables in a central place
12CONFIG_CBFS_PREFIX:=$(call strip_quotes,$(CONFIG_CBFS_PREFIX))
13CONFIG_FMDFILE:=$(call strip_quotes,$(CONFIG_FMDFILE))
14CONFIG_DEVICETREE:=$(call strip_quotes, $(CONFIG_DEVICETREE))
15CONFIG_OVERRIDE_DEVICETREE:=$(call strip_quotes, $(CONFIG_OVERRIDE_DEVICETREE))
16CONFIG_CHIPSET_DEVICETREE:=$(call strip_quotes, $(CONFIG_CHIPSET_DEVICETREE))
17CONFIG_MEMLAYOUT_LD_FILE:=$(call strip_quotes, $(CONFIG_MEMLAYOUT_LD_FILE))
18
19#######################################################################
20# misleadingly named, this is the coreboot version
21ifeq ($(KERNELVERSION),)
22ifeq ($(BUILD_TIMELESS),1)
23KERNELVERSION := -TIMELESS--LESSTIME-
24else
25KERNELVERSION := $(strip $(if $(GIT),\
26	$(shell git describe --abbrev=12 --dirty --always || git describe),\
27	$(if $(wildcard $(top)/.coreboot-version),\
28		$(shell cat $(top)/.coreboot-version),\
29		coreboot-unknown$(KERNELREVISION))))
30endif
31endif
32COREBOOT_EXPORTS += KERNELVERSION
33
34#######################################################################
35# Basic component discovery
36MAINBOARDDIR=$(call strip_quotes,$(CONFIG_MAINBOARD_DIR))
37VARIANT_DIR:=$(call strip_quotes,$(CONFIG_VARIANT_DIR))
38CARRIER_DIR:=$(call strip_quotes,$(CONFIG_CARRIER_DIR))
39COREBOOT_EXPORTS += MAINBOARDDIR VARIANT_DIR CARRIER_DIR
40
41## Final build results, which CBFSTOOL uses to create the final
42## rom image file, are placed under $(objcbfs).
43## These typically have suffixes .debug .elf .bin and .map
44objcbfs := $(obj)/cbfs/$(CONFIG_CBFS_PREFIX)
45additional-dirs += $(objcbfs)
46COREBOOT_EXPORTS += objcbfs
47
48## Based on the active configuration, Makefile conditionally collects
49## the required assembly includes and saves them in a file.
50## Such files that do not have a clear one-to-one relation to a source
51## file under src/ are placed and built under $(objgenerated)
52objgenerated := $(obj)/generated
53additional-dirs += $(objgenerated)
54COREBOOT_EXPORTS += objgenerated
55
56## CCACHE_EXTRAFILES can be set by individual rules to help CCACHE
57## discover dependencies it might not notice on its own (e.g. asm (".incbin")).
58COREBOOT_EXPORTS += CCACHE_EXTRAFILES
59
60#######################################################################
61# root rule to resolve if in build mode (ie. configuration exists)
62real-target: $(obj)/config.h coreboot files_added show_coreboot show_notices
63coreboot: $(obj)/coreboot.rom $(obj)/cbfstool $(obj)/rmodtool $(obj)/ifwitool $(obj)/cse_fpt $(obj)/cse_serger
64
65# This target can be used to run scripts or additional targets
66# after the build completes by creating a target named 'build_complete::'
67.PHONY: build_complete
68build_complete:: | coreboot
69
70# This target can be used to run rules after all files were added to CBFS,
71# for example to process FMAP regions or the entire image.
72.PHONY: files_added
73files_added:: | build_complete
74
75# This target should come just before the show_notices target.  If there
76# are no notices, the build should finish with the text of what was just
77# built.
78.PHONY: show_coreboot
79show_coreboot: | files_added
80	$(CBFSTOOL) $(obj)/coreboot.rom print -r $(subst $(spc),$(comma),$(all-regions))
81	printf "\nBuilt %s (%s)\n" $(MAINBOARDDIR) $(CONFIG_MAINBOARD_PART_NUMBER)
82	if [ -f "$(CCACHE_STATSLOG)" ]; then \
83		printf "\nccache statistics\n"; \
84		$(CCACHE) --show-log-stats -v; \
85	fi
86
87# This is intended to run at the *very end* of the build to show warnings
88# notices and the like.  If another target needs to be added, add it
89# BEFORE this target.
90.PHONY: show_notices
91show_notices:: | show_coreboot
92
93# This rule allows the site-local makefile to run before starting the actual
94# coreboot build. It should not be used in the regular coreboot makefiles.
95# Note: This gets run after the immediate makefile code like updating the
96# submodules, but before any other targets.
97.PHONY: site-local-target
98site-local-target::
99
100#######################################################################
101# our phony targets
102PHONY+= clean-abuild coreboot check-style build_complete
103
104#######################################################################
105# root source directories of coreboot
106# site-local Makefile.mk must go first to override default locations (for binaries etc.)
107subdirs-y := site-local
108
109subdirs-y += src/lib src/commonlib/ src/console src/device src/acpi src/superio/common
110subdirs-$(CONFIG_EC_ACPI) += src/ec/intel
111subdirs-y += src/ec/acpi $(wildcard src/ec/*/*) $(wildcard src/southbridge/*/*)
112subdirs-y += $(wildcard src/soc/*) $(wildcard src/soc/*/common) $(filter-out $(wildcard src/soc/*/common),$(wildcard src/soc/*/*))
113subdirs-y += $(wildcard src/northbridge/*/*)
114subdirs-y += $(filter-out src/superio/common,$(wildcard src/superio/*)) $(wildcard src/superio/*/*)
115subdirs-y += $(wildcard src/drivers/*) $(wildcard src/drivers/*/*) $(wildcard src/drivers/*/*/*)
116subdirs-y += src/cpu src/vendorcode
117subdirs-y += util/cbfstool util/sconfig util/nvramtool util/pgtblgen util/amdfwtool
118subdirs-y += util/futility util/marvell util/bincfg util/supermicro util/qemu
119subdirs-y += util/ifdtool
120subdirs-y += $(wildcard src/arch/*)
121subdirs-y += src/mainboard/$(MAINBOARDDIR)
122subdirs-y += src/security
123subdirs-y += payloads payloads/external
124subdirs-$(CONFIG_SBOM) += src/sbom
125subdirs-y += util/checklist util/testing
126
127#######################################################################
128# Add source classes and their build options
129classes-y := ramstage romstage bootblock decompressor postcar smm smmstub cpu_microcode verstage
130
131# Add a special 'all' class to add sources to all stages
132$(call add-special-class,all)
133all-handler = $(foreach class,bootblock verstage romstage postcar ramstage,$(eval $(class)-y += $(2)))
134
135$(call add-special-class,all_x86)
136all_x86-handler = $(foreach class,bootblock verstage_x86 romstage postcar ramstage,$(eval $(class)-y += $(2)))
137
138$(call add-special-class,verstage_x86)
139ifeq ($(CONFIG_ARCH_VERSTAGE_X86_32)$(CONFIG_ARCH_VERSTAGE_X86_64),y)
140verstage_x86-handler = $(eval verstage-y += $(2))
141else
142verstage_x86-handler =
143endif
144
145# Add dynamic classes for rmodules
146$(foreach supported_arch,$(ARCH_SUPPORTED), \
147	    $(eval $(call define_class,rmodules_$(supported_arch),$(supported_arch))))
148# Provide a macro to determine environment for free standing rmodules.
149$(foreach supported_arch,$(ARCH_SUPPORTED), \
150	$(eval rmodules_$(supported_arch)-generic-ccopts += -D__RMODULE__))
151
152#######################################################################
153# Helper functions for math, strings, and various file placement matters.
154# macros work on all formats understood by printf(1)
155# values are space separated if using more than one value
156#
157# int-add:        adds an arbitrary length list of integers
158# int-subtract:   subtracts the second of two integers from the first
159# int-multiply:   multiplies an arbitrary length list of integers
160# int-divide:     divides the first integer by the second
161# int-remainder:  arithmetic remainder of the first number divided by the second
162# int-shift-left: Shift $1 left by $2 bits
163# int-lt:         1 if the first value is less than the second.  0 otherwise
164# int-gt:         1 if the first values is greater than the second.  0 otherwise
165# int-eq:         1 if the two values are equal.  0 otherwise
166# int-align:      align $1 to $2 units
167# file-size:      returns the filesize of the given file
168# tolower:        returns the value in all lowercase
169# toupper:        returns the value in all uppercase
170# ws_to_under:    returns the value with any whitespace changed to underscores
171# get_fmap_value  returns the value of a given FMAP field from fmap_config.h
172_toint=$(shell printf "%d" $1)
173_tohex=$(shell printf 0x"%x" $1)
174_int-add2=$(shell expr $(call _toint,$1) + $(call _toint,$2))
175int-add=$(if $(filter 1,$(words $1)),$(strip $1),$(call int-add,$(call _int-add2,$(word 1,$1),$(word 2,$1)) $(wordlist 3,$(words $1),$1)))
176int-subtract=$(if $(filter 1,$(words $1)),$(strip $1),$(shell expr $(call _toint,$(word 1,$1)) - $(call _toint,$(word 2,$1))))
177_int-multiply2=$(shell expr $(call _toint,$1) \* $(call _toint,$2))
178int-multiply=$(if $(filter 1,$(words $1)),$(strip $1),$(call int-multiply,$(call _int-multiply2,$(word 1,$1),$(word 2,$1)) $(wordlist 3,$(words $1),$1)))
179int-divide=$(if $(filter 1,$(words $1)),$(strip $1),$(shell expr $(call _toint,$(word 1,$1)) / $(call _toint,$(word 2,$1))))
180int-remainder=$(if $(filter 1,$(words $1)),$(strip $1),$(shell expr $(call _toint,$(word 1,$1)) % $(call _toint,$(word 2,$1))))
181int-shift-left=$(shell echo -n $$(($(call _toint,$(word 1, $1)) << $(call _toint,$(word 2, $1)))))
182int-lt=$(if $(filter 1,$(words $1)),$(strip $1),$(shell expr $(call _toint,$(word 1,$1)) \< $(call _toint,$(word 2,$1))))
183int-gt=$(if $(filter 1,$(words $1)),$(strip $1),$(shell expr $(call _toint,$(word 1,$1)) \> $(call _toint,$(word 2,$1))))
184int-eq=$(if $(filter 1,$(words $1)),$(strip $1),$(shell expr $(call _toint,$(word 1,$1)) = $(call _toint,$(word 2,$1))))
185int-align=$(shell A=$(call _toint,$1) B=$(call _toint,$2); expr $$A + \( \( $$B - \( $$A % $$B \) \) % $$B \) )
186int-align-down=$(shell A=$(call _toint,$1) B=$(call _toint,$2); expr $$A - \( $$A % $$B \) )
187file-size=$(strip $(shell wc -c "$1" | cut -f 1 -d ' '))
188tolower=$(shell echo '$1' | tr '[:upper:]' '[:lower:]')
189toupper=$(shell echo '$1' | tr '[:lower:]' '[:upper:]')
190ws_to_under=$(shell echo '$1' | tr ' \t' '_')
191get_fmap_value=$(shell awk '$$2 == "$1" {print $$3}' $(obj)/fmap_config.h)
192
193#######################################################################
194# Helper functions for ramstage postprocess
195spc :=
196spc := $(spc) $(spc)
197comma := ,
198
199# Returns all files and dirs below `dir` (recursively).
200# files-below-dir,dir,files
201files-below-dir=$(filter $(1)%,$(2))
202
203# Returns all dirs below `dir` (recursively).
204# dirs-below-dir,dir,files
205dirs-below-dir=$(filter-out $(1),$(sort $(dir $(call files-below-dir,$(1),$(2)))))
206
207# Returns all files directly in `dir` (non-recursively).
208# files-in-dir,dir,files
209files-in-dir=$(filter-out $(addsuffix %,$(call dirs-below-dir,$(1),$(2))),$(call files-below-dir,$(1),$(2)))
210
211#######################################################################
212# reduce command line length by linking the objects of each
213# directory into an intermediate file
214ramstage-postprocess=$$(eval DEPENDENCIES+=$$(addsuffix .d,$$(basename $(1)))) \
215	$(foreach d,$(sort $(dir $(filter-out %.ld,$(1)))), \
216	$(eval $(d)ramstage.a: $(call files-in-dir,$(d),$(filter-out %.ld,$(1))); rm -f $$@ && $(AR_ramstage) rcsT $$@ $$^ ) \
217	$(eval ramstage-objs:=$(d)ramstage.a $(filter-out $(filter-out %.ld, $(call files-in-dir,$(d),$(1))),$(ramstage-objs))))
218
219decompressor-generic-ccopts += -D__DECOMPRESSOR__
220bootblock-generic-ccopts += -D__BOOTBLOCK__
221romstage-generic-ccopts += -D__ROMSTAGE__
222ramstage-generic-ccopts += -D__RAMSTAGE__
223ifeq ($(CONFIG_COVERAGE),y)
224ramstage-c-ccopts += -fprofile-arcs -ftest-coverage
225endif
226ifneq ($(GIT),)
227ifneq ($(UPDATED_SUBMODULES),1)
228$(info Updating git submodules.)
229# try to fetch non-optional submodules if the source is under git
230forgetthis:=$(shell git submodule update --init $(quiet_errors))
231# Checkout Cmocka repository
232forgetthis:=$(shell git submodule update --init --checkout 3rdparty/cmocka $(quiet_errors))
233ifeq ($(CONFIG_USE_BLOBS),y)
234# These items are necessary because each has update=none in .gitmodules.  They are ignored
235# until expressly requested and enabled with --checkout
236forgetthis:=$(shell git submodule update --init --checkout 3rdparty/blobs $(quiet_errors))
237forgetthis:=$(shell git submodule update --init --checkout 3rdparty/intel-microcode $(quiet_errors))
238ifeq ($(CONFIG_FSP_USE_REPO),y)
239forgetthis:=$(shell git submodule update --init --checkout 3rdparty/fsp $(quiet_errors))
240endif
241ifeq ($(CONFIG_USE_AMD_BLOBS),y)
242forgetthis:=$(shell git submodule update --init --checkout 3rdparty/amd_blobs $(quiet_errors))
243endif
244ifeq ($(CONFIG_USE_QC_BLOBS),y)
245forgetthis:=$(shell git submodule update --init --checkout 3rdparty/qc_blobs $(quiet_errors))
246endif
247endif
248UPDATED_SUBMODULES:=1
249COREBOOT_EXPORTS += UPDATED_SUBMODULES
250
251endif
252endif # GIT != ""
253
254postcar-c-deps:=$$(OPTION_TABLE_H)
255ramstage-c-deps:=$$(OPTION_TABLE_H)
256romstage-c-deps:=$$(OPTION_TABLE_H)
257verstage-c-deps:=$$(OPTION_TABLE_H)
258bootblock-c-deps:=$$(OPTION_TABLE_H)
259$(foreach type,ads adb, \
260  $(foreach stage,$(COREBOOT_STANDARD_STAGES), \
261    $(eval $(stage)-$(type)-deps := \
262	$(obj)/$(stage)/$(notdir $(KCONFIG_AUTOADS)) \
263	$(obj)/libgnat-$(ARCH-$(stage)-y)/libgnat.a)))
264
265# Add handler to copy linker scripts
266define generic-objs_ld_template_gen
267de$(EMPTY)fine $(1)-objs_ld_template
268$$(call src-to-obj,$1,$$(1).ld): $$(1).ld $(obj)/config.h $(obj)/fmap_config.h
269	@printf "    CP         $$$$(subst $$$$(obj)/,,$$$$(@))\n"
270	$$(CC_$(1)) -MMD $$(CPPFLAGS_$(1)) $$($(1)-ld-ccopts) $(PREPROCESS_ONLY) -include $(obj)/config.h -MT $$$$@ -o $$$$@ $$$$<
271en$(EMPTY)def
272endef
273
274# Add handler to deal with archives
275define generic-objs_a_template_gen
276de$(EMPTY)fine $(1)-objs_a_template
277$$(call src-to-obj,$1,$$(1).a): $$(1).a
278	@printf "    AR         $$$$(subst $$$$(obj)/,,$$$$(@))\n"
279	$$$$(AR_$(1)) rcsT $$$$@.tmp $$$$<
280	mv $$$$@.tmp $$$$@
281en$(EMPTY)def
282endef
283
284# Add handler to add no rules for manual files
285define generic-objs_manual_template_gen
286# do nothing
287endef
288
289#######################################################################
290# Add handler to compile ACPI's ASL
291# arg1: base file name
292# arg2: y or n for including in cbfs. defaults to y
293
294# Empty resource templates were marked as a warning in IASL with the comment
295# "This would appear to be worthless in real-world ASL code.", which is
296# possibly true in many cases.  In other cases it seems that an empty
297# ResourceTemplate is the correct code.
298# As it's valid ASL, disable the warning.
299EMPTY_RESOURCE_TEMPLATE_WARNING = 3150
300
301IASL_WARNINGS_LIST = $(EMPTY_RESOURCE_TEMPLATE_WARNING)
302
303IGNORED_IASL_WARNINGS = $(addprefix -vw , $(IASL_WARNINGS_LIST))
304
305define asl_template
306$(CONFIG_CBFS_PREFIX)/$(1).aml-file = $(obj)/$(1).aml
307$(CONFIG_CBFS_PREFIX)/$(1).aml-type = raw
308$(CONFIG_CBFS_PREFIX)/$(1).aml-compression = none
309ifeq ($(CONFIG_SOC_AMD_COMMON_BLOCK_LPC_SPI_DMA),y)
310$(CONFIG_CBFS_PREFIX)/$(1).aml-align = 64
311endif
312cbfs-files-$(if $(2),$(2),y) += $(CONFIG_CBFS_PREFIX)/$(1).aml
313$(eval DEPENDENCIES += $(obj)/$(1).d)
314$(obj)/$(1).aml: $(src)/mainboard/$(MAINBOARDDIR)/$(1).asl $(obj)/config.h
315	@printf "    IASL       $$(subst $(top)/,,$$(@))\n"
316	$(CC_ramstage) -x assembler-with-cpp -E -MMD -MT $$(@) $$(CPPFLAGS_ramstage) -D__ACPI__ -P -include $(src)/include/kconfig.h -I$(obj) -I$(src) -I$(src)/include -I$(src)/arch/$(ARCHDIR-$(ARCH-ramstage-y))/include -I$(src)/mainboard/$(MAINBOARDDIR) $$< -o $(obj)/$(1).asl
317	cd $$(dir $$@); $(IASL) $(IGNORED_IASL_WARNINGS) -we -p $$(notdir $$@) $(1).asl
318	echo "    IASL       "$(IASL_WARNINGS_LIST)" warning types were ignored!"
319	if ! $(IASL) -d $$@ 2>&1 | grep -Eq 'ACPI (Warning|Error)'; then	\
320		echo "    IASL       $$@ disassembled correctly.";		\
321		true;								\
322	else									\
323		echo "Error: Could not correctly disassemble $$@";		\
324		$(IASL) -d $$@;							\
325		false;								\
326	fi
327endef
328
329#######################################################################
330# Parse plaintext CMOS defaults into binary format
331# arg1: source file
332# arg2: binary file name
333cbfs-files-processor-nvramtool= \
334	$(eval $(2): $(1) $(top)/$(call strip_quotes,$(CONFIG_CMOS_LAYOUT_FILE)) | $(objutil)/nvramtool/nvramtool ; \
335		printf "    CREATE     $(2) (from $(1))\n"; \
336		$(objutil)/nvramtool/nvramtool -y $(top)/$(call strip_quotes,$(CONFIG_CMOS_LAYOUT_FILE)) -D $(2).tmp -p $(1) && \
337		mv $(2).tmp $(2))
338
339#######################################################################
340# Reduce a .config file by removing lines about default unset booleans
341# arg1: input
342# arg2: output
343define cbfs-files-processor-config
344	$(eval $(2): $(1) $(obj)/build.h $(objutil)/kconfig/conf; \
345		+printf "    CREATE     $(2) (from $(1))\n"; \
346		printf "# This image was built using coreboot " > $(2).tmp && \
347		grep "\<COREBOOT_VERSION\>" $(obj)/build.h |cut -d\" -f2 >> $(2).tmp && \
348		$(MAKE) DOTCONFIG=$(1) DEFCONFIG=$(2).tmp2 savedefconfig && \
349		cat $(2).tmp2 >> $(2).tmp && \
350		printf "# End of defconfig. Derivable values start here.\n" >> $(2).tmp && \
351		grep "^CONFIG" $(1) | grep -F -v -f $(2).tmp2 >> $(2).tmp && \
352		rm -f $(2).tmp2 && \
353		mv -f $(2).tmp $(2))
354endef
355
356#######################################################################
357# Add a file to CBFS with just type and compression values
358# arg1: name in CBFS
359# arg2: filename and path
360# arg3: type in CBFS
361# arg4: compression type
362define add-cbfs-file-simple
363	$(eval cbfs-files-y += $(1))
364	$(eval $(1)-file := $(2))
365	$(eval $(1)-type := $(3))
366	$(eval $(1)-compression := $(4))
367endef
368
369#######################################################################
370# Compile a C file with a bare struct definition into binary
371# arg1: C source file
372# arg2: binary file
373cbfs-files-processor-struct= \
374	$(eval $(2): $(1) $(obj)/build.h $(obj)/fmap_config.h $(KCONFIG_AUTOHEADER); \
375		printf "    CC+STRIP   $(1)\n"; \
376		$(CC_ramstage) -MMD $(CPPFLAGS_ramstage) $(CFLAGS_ramstage) --param asan-globals=0 $$(ramstage-c-ccopts) -include $(KCONFIG_AUTOHEADER) -MT $(2) -o $(2).tmp -c $(1) && \
377		$(OBJCOPY_ramstage) -O binary --only-section='.*data*' --only-section='.*bss*' \
378		--set-section-flags .*bss*=alloc,contents,load $(2).tmp $(2); \
379		rm -f $(2).tmp) \
380	$(eval DEPENDENCIES += $(2).d)
381
382#######################################################################
383# Convert image to YCC 4:2:0 JPEG
384#
385# In two steps:
386# 1. Convert to RGB colors, optionally resize and store as BMP.
387# 2. Round final size to multiples of 16, optionally swap colors,
388#    convert (back) to sRGB and store as JPEG.
389# The split is necessary because we don't know the exact, scaled
390# size due to aspect-ratio. Note: IM v7 would allow us to do the
391# calculations in one command using %[fx:...] syntax.
392#
393# arg1: image input file
394# arg2: output jpg
395cbfs-files-processor-jpg420= \
396	$(eval $(2): $(1) $(KCONFIG_AUTOCONFIG);				\
397		printf "    CONVERT    $$<\n";					\
398		res=$(CONFIG_BOOTSPLASH_CONVERT_RESOLUTION);			\
399		res=$$$$(convert $$<						\
400			-colorspace RGB						\
401			$$(BOOTSPLASH_RESIZE-y)					\
402			-format '%wx%h' -write info:				\
403			bmp:$$@);						\
404		convert $$@							\
405			$$(BOOTSPLASH_ROUND16)					\
406			$$(BOOTSPLASH_COLORSWAP-y)				\
407			-colorspace sRGB					\
408			-quality $$(CONFIG_BOOTSPLASH_CONVERT_QUALITY)%		\
409			-interlace none -colorspace YCC -sampling-factor 4:2:0	\
410			jpg:$$@)
411BOOTSPLASH_FLOOR = $$(($${res%%x*} & ~15))x$$(($${res\#\#*x} & ~15))
412BOOTSPLASH_RESIZE-$(CONFIG_BOOTSPLASH_CONVERT_RESIZE) = -resize $(BOOTSPLASH_FLOOR)
413BOOTSPLASH_CEIL = $$((($${res%%x*} + 15) & ~15))x$$((($${res\#\#*x} + 15) & ~15))
414BOOTSPLASH_ROUND16 = -background black -gravity center -extent $(BOOTSPLASH_CEIL)
415BOOTSPLASH_COLORSWAP-$(CONFIG_BOOTSPLASH_CONVERT_COLORSWAP) := -channel-fx 'red<=>blue'
416
417#######################################################################
418# Add handler for arbitrary files in CBFS
419$(call add-special-class,cbfs-files)
420cbfs-files-handler= \
421		$(eval tmp-cbfs-method:=$(word 2, $(subst :, ,$($(2)-file)))) \
422		$(eval $(2)-file:=$(call strip_quotes,$(word 1, $(subst :, ,$($(2)-file))))) \
423		$(eval tmp-cbfs-file:= ) \
424		$(if $($(2)-file), \
425			$(if $(wildcard $(1)$($(2)-file)), \
426				$(eval tmp-cbfs-file:= $(wildcard $(1)$($(2)-file))), \
427				$(eval tmp-cbfs-file:= $($(2)-file)))) \
428		$(if $(strip $($(2)-required)), \
429			$(if $(wildcard $(tmp-cbfs-file)),, \
430				$(info This build configuration requires $($(2)-required)) \
431				$(eval FAILBUILD:=1) \
432			)) \
433		$(if $(strip $($(2)-align)), \
434			$(if $(strip $($(2)-position)), \
435				$(info ERROR: It is not allowed to specify both alignment and position for $($(2)-file)) \
436				$(eval FAILBUILD:=1) \
437			)) \
438		$(if $(tmp-cbfs-method), \
439			$(eval tmp-old-cbfs-file:=$(tmp-cbfs-file)) \
440			$(eval tmp-cbfs-file:=$(shell mkdir -p $(obj)/mainboard/$(MAINBOARDDIR); mktemp $(obj)/mainboard/$(MAINBOARDDIR)/cbfs-file.XXXXXX).out) \
441			$(call cbfs-files-processor-$(tmp-cbfs-method),$(tmp-old-cbfs-file),$(tmp-cbfs-file))) \
442		$(if $(tmp-cbfs-file), \
443			$(eval cbfs-files += $(subst $(spc),*,$(tmp-cbfs-file)|$(2)|$($(2)-type)|$($(2)-compression)|$(strip $($(2)-position))|$($(2)-align)|$($(2)-options)))) \
444		$(eval $(2)-name:=) \
445		$(eval $(2)-type:=) \
446		$(eval $(2)-compression:=) \
447		$(eval $(2)-position:=) \
448		$(eval $(2)-required:=) \
449		$(eval $(2)-options:=) \
450		$(eval $(2)-align:=)
451
452#######################################################################
453# a variety of flags for our build
454CBFS_COMPRESS_FLAG:=none
455ifeq ($(CONFIG_COMPRESS_RAMSTAGE_LZMA),y)
456CBFS_COMPRESS_FLAG:=LZMA
457endif
458ifeq ($(CONFIG_COMPRESS_RAMSTAGE_LZ4),y)
459CBFS_COMPRESS_FLAG:=LZ4
460endif
461
462CBFS_PAYLOAD_COMPRESS_FLAG:=none
463ifeq ($(CONFIG_COMPRESSED_PAYLOAD_LZMA),y)
464CBFS_PAYLOAD_COMPRESS_FLAG:=LZMA
465endif
466ifeq ($(CONFIG_COMPRESSED_PAYLOAD_LZ4),y)
467CBFS_PAYLOAD_COMPRESS_FLAG:=LZ4
468endif
469
470CBFS_SECONDARY_PAYLOAD_COMPRESS_FLAG:=none
471ifeq ($(CONFIG_COMPRESS_SECONDARY_PAYLOAD),y)
472CBFS_SECONDARY_PAYLOAD_COMPRESS_FLAG:=LZMA
473endif
474
475CBFS_PRERAM_COMPRESS_FLAG:=none
476ifeq ($(CONFIG_COMPRESS_PRERAM_STAGES),y)
477CBFS_PRERAM_COMPRESS_FLAG:=LZ4
478endif
479
480ifneq ($(CONFIG_LOCALVERSION),"")
481COREBOOT_EXTRA_VERSION := -$(call strip_quotes,$(CONFIG_LOCALVERSION))
482COREBOOT_EXPORTS += COREBOOT_EXTRA_VERSION
483endif
484
485CPPFLAGS_common := -Isrc
486CPPFLAGS_common += -Isrc/include
487CPPFLAGS_common += -Isrc/commonlib/include
488CPPFLAGS_common += -Isrc/commonlib/bsd/include
489CPPFLAGS_common += -I$(obj)
490VBOOT_SOURCE ?= 3rdparty/vboot
491CPPFLAGS_common += -I$(VBOOT_SOURCE)/firmware/include
492CPPFLAGS_common += -include $(src)/include/kconfig.h
493CPPFLAGS_common += -include $(src)/include/rules.h
494CPPFLAGS_common += -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h
495CPPFLAGS_common += -I3rdparty
496CPPFLAGS_common += -D__BUILD_DIR__=\"$(obj)\"
497CPPFLAGS_common += -D__COREBOOT__
498
499ifeq ($(BUILD_TIMELESS),1)
500CPPFLAGS_common += -D__TIMELESS__
501endif
502
503ifeq ($(CONFIG_PCI_OPTION_ROM_RUN_YABEL)$(CONFIG_PCI_OPTION_ROM_RUN_REALMODE),y)
504CPPFLAGS_ramstage += -Isrc/device/oprom/include
505endif
506
507CFLAGS_common += -pipe
508CFLAGS_common += -g
509CFLAGS_common += -nostdinc
510CFLAGS_common += -std=gnu11
511CFLAGS_common += -nostdlib
512CFLAGS_common += -Wall
513CFLAGS_common += -Wundef
514CFLAGS_common += -Wstrict-prototypes
515CFLAGS_common += -Wmissing-prototypes
516CFLAGS_common += -Wwrite-strings
517CFLAGS_common += -Wredundant-decls
518CFLAGS_common += -Wno-trigraphs
519CFLAGS_common += -Wimplicit-fallthrough
520CFLAGS_common += -Wshadow
521CFLAGS_common += -Wdate-time
522CFLAGS_common += -Wtype-limits
523CFLAGS_common += -Wvla
524CFLAGS_common += -Wold-style-definition
525CFLAGS_common += -Wdangling-else
526CFLAGS_common += -Wmissing-include-dirs
527CFLAGS_common += -fno-common
528CFLAGS_common += -ffreestanding
529CFLAGS_common += -fno-builtin
530CFLAGS_common += -fomit-frame-pointer
531CFLAGS_common += -fstrict-aliasing
532CFLAGS_common += -ffunction-sections
533CFLAGS_common += -fdata-sections
534CFLAGS_common += -fno-pie
535CFLAGS_common += -Wstring-compare
536ifeq ($(CONFIG_COMPILER_GCC),y)
537CFLAGS_common += -Wold-style-declaration
538CFLAGS_common += -Wcast-function-type
539# Don't add these GCC specific flags when running scan-build
540ifeq ($(CCC_ANALYZER_OUTPUT_FORMAT),)
541CFLAGS_common += -Wno-packed-not-aligned
542CFLAGS_common += -fconserve-stack
543CFLAGS_common += -Wnull-dereference
544CFLAGS_common += -Wlogical-op
545CFLAGS_common += -Wduplicated-cond
546CFLAGS_common += -Wno-array-compare
547endif
548endif
549
550ADAFLAGS_common += -gnatp
551ADAFLAGS_common += -Wuninitialized
552ADAFLAGS_common += -Wall
553ADAFLAGS_common += -Werror
554ADAFLAGS_common += -pipe
555ADAFLAGS_common += -g
556ADAFLAGS_common += -nostdinc
557ADAFLAGS_common += -Wstrict-aliasing
558ADAFLAGS_common += -Wshadow
559ADAFLAGS_common += -fno-common
560ADAFLAGS_common += -fomit-frame-pointer
561ADAFLAGS_common += -ffunction-sections
562ADAFLAGS_common += -fdata-sections
563ADAFLAGS_common += -fno-pie
564# Ada warning options:
565#
566#  a   Activate most optional warnings.
567# .e   Activate every optional warnings.
568#  e   Treat warnings and style checks as errors.
569#
570#  D   Suppress warnings on implicit dereferences:
571#      As SPARK does not accept access types we have to map the
572#      dynamically chosen register locations to a static SPARK
573#      variable.
574#
575# .H   Suppress warnings on holes/gaps in records:
576#      We are modelling hardware here!
577#
578#  H   Suppress warnings on hiding:
579#      It's too annoying, you run out of ideas for identifiers fast.
580#
581#  T   Suppress warnings for tracking of deleted conditional code:
582#      We use static options to select code paths at compile time.
583#
584#  U   Suppress warnings on unused entities:
585#      Would have lots of warnings for unused register definitions,
586#      `withs` for debugging etc.
587#
588# .U   Deactivate warnings on unordered enumeration types:
589#      As SPARK doesn't support `pragma Ordered` by now, we don't
590#      use that, yet.
591#
592# .W   Suppress warnings on unnecessary Warnings Off pragmas:
593#      Things get really messy when you use different compiler
594#      versions, otherwise.
595# .Y   Disable information messages for why package spec needs body:
596#      Those messages are annoying. But don't forget to enable those,
597#      if you need the information.
598ADAFLAGS_common += -gnatwa.eeD.HHTU.U.W.Y
599# Disable style checks for now
600ADAFLAGS_common += -gnatyN
601
602LDFLAGS_common := --gc-sections
603LDFLAGS_common += -nostdlib
604LDFLAGS_common += --nmagic
605LDFLAGS_common += -static
606
607# Workaround for RISC-V linker bug, merge back into above line when fixed.
608# https://sourceware.org/bugzilla/show_bug.cgi?id=27180
609ifneq ($(CONFIG_ARCH_RISCV),y)
610LDFLAGS_common += --emit-relocs
611endif
612
613ifeq ($(CONFIG_WARNINGS_ARE_ERRORS),y)
614CFLAGS_common += -Werror
615endif
616ifneq ($(GDB_DEBUG),)
617CFLAGS_common += -Og
618ADAFLAGS_common += -Og
619else
620CFLAGS_common += -Os
621ADAFLAGS_common += -Os
622endif
623
624ifeq ($(CONFIG_DEBUG_ADA_CODE),y)
625ADAFLAGS_common += -gnata
626endif
627
628additional-dirs += $(objutil)/cbfstool $(objutil)/ifdtool \
629		   $(objutil)/options $(objutil)/amdfwtool \
630		   $(objutil)/cbootimage
631
632export $(COREBOOT_EXPORTS)
633
634#######################################################################
635# generate build support files
636
637build_h := $(obj)/build.h
638
639# We have to manually export variables that `genbuild_h.sh` uses
640# when we call it through the `$(shell)` function. This is fragile
641# but as variables newly added to `genbuild_h.sh` would just not
642# work, we'd notice that instantly at least.
643build_h_exports := BUILD_TIMELESS KERNELVERSION COREBOOT_EXTRA_VERSION
644
645# Report new `build.ht` as dependency if `build.h` differs.
646build_h_check := \
647	export $(foreach exp,$(build_h_exports),$(exp)="$($(exp))"); \
648	util/genbuild_h/genbuild_h.sh $(xcompile) \
649		>$(build_h)t 2>/dev/null; \
650	cmp -s $(build_h)t $(build_h) >/dev/null 2>&1 || echo $(build_h)t
651
652$(build_h): $$(shell $$(build_h_check))
653	@printf "    GEN        build.h\n"
654	mv $< $@
655
656$(obj)/build_info:
657	@echo 'COREBOOT_VERSION: $(call strip_quotes,$(KERNELVERSION))' > $@.tmp
658	@echo 'MAINBOARD_VENDOR: $(call strip_quotes,$(CONFIG_MAINBOARD_VENDOR))' >> $@.tmp
659	@echo 'MAINBOARD_PART_NUMBER: $(call strip_quotes,$(CONFIG_MAINBOARD_PART_NUMBER))' >> $@.tmp
660	mv $@.tmp $@
661
662#######################################################################
663# Build the tools
664CBFSTOOL:=$(objutil)/cbfstool/cbfstool
665FMAPTOOL:=$(objutil)/cbfstool/fmaptool
666RMODTOOL:=$(objutil)/cbfstool/rmodtool
667IFWITOOL:=$(objutil)/cbfstool/ifwitool
668IFITTOOL:=$(objutil)/cbfstool/ifittool
669AMDCOMPRESS:=$(objutil)/cbfstool/amdcompress
670CSE_FPT:=$(objutil)/cbfstool/cse_fpt
671CSE_SERGER:=$(objutil)/cbfstool/cse_serger
672
673$(obj)/cbfstool: $(CBFSTOOL)
674	cp $< $@
675
676$(obj)/fmaptool: $(FMAPTOOL)
677	cp $< $@
678
679$(obj)/rmodtool: $(RMODTOOL)
680	cp $< $@
681
682$(obj)/ifwitool: $(IFWITOOL)
683	cp $< $@
684
685$(obj)/ifittool: $(IFITTOOL)
686	cp $< $@
687
688$(obj)/amdcompress: $(AMDCOMPRESS)
689	cp $< $@
690
691$(obj)/cse_fpt: $(CSE_FPT)
692	cp $< $@
693
694$(obj)/cse_serger: $(CSE_SERGER)
695	cp $< $@
696
697_WINCHECK=$(shell uname -o 2> /dev/null)
698STACK=
699ifeq ($(_WINCHECK),Msys)
700	STACK=-Wl,--stack,16384000
701endif
702ifeq ($(_WINCHECK),Cygwin)
703	STACK=-Wl,--stack,16384000
704endif
705
706BINCFG:=$(objutil)/bincfg/bincfg
707
708IFDTOOL:=$(objutil)/ifdtool/ifdtool
709
710AMDFWTOOL:=$(objutil)/amdfwtool/amdfwtool
711AMDFWREAD:=$(objutil)/amdfwtool/amdfwread
712
713APCB_EDIT_TOOL:=$(top)/util/apcb/apcb_edit.py
714
715APCB_V3_EDIT_TOOL:=$(top)/util/apcb/apcb_v3_edit.py
716
717APCB_V3A_EDIT_TOOL:=$(top)/util/apcb/apcb_v3a_edit.py
718
719CBOOTIMAGE:=$(objutil)/cbootimage/cbootimage
720
721FUTILITY?=$(objutil)/futility/futility
722
723subdirs-y += util/nvidia
724
725$(obj)/config.h: $(objutil)/kconfig/conf
726
727#######################################################################
728# needed objects that every mainboard uses
729# Creation of these is architecture and mainboard independent
730DEVICETREE_FILE := $(src)/mainboard/$(MAINBOARDDIR)/$(CONFIG_DEVICETREE)
731
732SCONFIG_OPTIONS := --mainboard_devtree=$(DEVICETREE_FILE)
733
734ifneq ($(CONFIG_OVERRIDE_DEVICETREE),)
735OVERRIDE_DEVICETREE_FILE := $(src)/mainboard/$(MAINBOARDDIR)/$(CONFIG_OVERRIDE_DEVICETREE)
736SCONFIG_OPTIONS += --override_devtree=$(OVERRIDE_DEVICETREE_FILE)
737endif
738
739ifneq ($(CONFIG_CHIPSET_DEVICETREE),)
740CHIPSET_DEVICETREE_FILE := $(src)/$(CONFIG_CHIPSET_DEVICETREE)
741SCONFIG_OPTIONS += --chipset_devtree=$(CHIPSET_DEVICETREE_FILE)
742endif
743
744DEVICETREE_STATIC_C := $(obj)/mainboard/$(MAINBOARDDIR)/static.c
745SCONFIG_OPTIONS += --output_c=$(DEVICETREE_STATIC_C)
746
747DEVICETREE_STATIC_H := $(obj)/static.h
748SCONFIG_OPTIONS += --output_h=$(DEVICETREE_STATIC_H)
749
750DEVICETREE_DEVICENAMES_H := $(obj)/static_devices.h
751SCONFIG_OPTIONS += --output_d=$(DEVICETREE_DEVICENAMES_H)
752
753DEVICETREE_FWCONFIG_H := $(obj)/static_fw_config.h
754SCONFIG_OPTIONS += --output_f=$(DEVICETREE_FWCONFIG_H)
755
756$(DEVICETREE_STATIC_C): $(DEVICETREE_FILE) $(OVERRIDE_DEVICETREE_FILE) $(CHIPSET_DEVICETREE_FILE) $(objutil)/sconfig/sconfig
757	@printf "    SCONFIG    $(subst $(src)/,,$(<))\n"
758	mkdir -p $(dir $(DEVICETREE_STATIC_C))
759	$(objutil)/sconfig/sconfig $(SCONFIG_OPTIONS)
760
761ramstage-y+=$(DEVICETREE_STATIC_C)
762romstage-y+=$(DEVICETREE_STATIC_C)
763verstage-y+=$(DEVICETREE_STATIC_C)
764bootblock-y+=$(DEVICETREE_STATIC_C)
765postcar-y+=$(DEVICETREE_STATIC_C)
766smm-y+=$(DEVICETREE_STATIC_C)
767
768# Ensure static.c and static.h are created before any objects are compiled
769ramstage-c-deps+=$(DEVICETREE_STATIC_C)
770romstage-c-deps+=$(DEVICETREE_STATIC_C)
771verstage-c-deps+=$(DEVICETREE_STATIC_C)
772bootblock-c-deps+=$(DEVICETREE_STATIC_C)
773postcar-c-deps+=$(DEVICETREE_STATIC_C)
774smm-c-deps+=$(DEVICETREE_STATIC_C)
775
776# Ensure fmap_config.h are created before any objects are compiled
777ramstage-c-deps+=$(obj)/fmap_config.h
778romstage-c-deps+=$(obj)/fmap_config.h
779verstage-c-deps+=$(obj)/fmap_config.h
780bootblock-c-deps+=$(obj)/fmap_config.h
781postcar-c-deps+=$(obj)/fmap_config.h
782smm-c-deps+=$(obj)/fmap_config.h
783
784.PHONY: devicetree
785devicetree: $(DEVICETREE_STATIC_C)
786
787ramstage-y += $(CONFIG_MEMLAYOUT_LD_FILE)
788romstage-y += $(CONFIG_MEMLAYOUT_LD_FILE)
789bootblock-y += $(CONFIG_MEMLAYOUT_LD_FILE)
790verstage-y += $(CONFIG_MEMLAYOUT_LD_FILE)
791postcar-y += $(CONFIG_MEMLAYOUT_LD_FILE)
792decompressor-y += $(CONFIG_MEMLAYOUT_LD_FILE)
793
794#######################################################################
795# Clean up rules
796clean-abuild:
797	rm -rf coreboot-builds
798
799#######################################################################
800# Development utilities
801printcrt0s:
802	@echo crt0s=$(crt0s)
803	@echo ldscripts=$(ldscripts)
804
805update:
806	dongle.py -c /dev/term/1 $(obj)/coreboot.rom EOF
807
808check-style:
809	grep "^# DESCR:" util/lint/check-style | sed "s,.*DESCR: *,,"
810	echo "========"
811	util/lint/check-style
812	echo "========"
813
814gitconfig:
815	util/gitconfig/gitconfig.sh "$(MAKE)"
816
817install-git-commit-clangfmt:
818	cp util/scripts/prepare-commit-msg.clang-format .git/hooks/prepare-commit-msg
819
820include util/crossgcc/Makefile.mk
821
822.PHONY: tools
823tools: $(objutil)/kconfig/conf $(objutil)/kconfig/toada $(CBFSTOOL) $(objutil)/cbfstool/cbfs-compression-tool $(FMAPTOOL) $(RMODTOOL) $(IFWITOOL) $(objutil)/nvramtool/nvramtool $(objutil)/sconfig/sconfig $(IFDTOOL) $(CBOOTIMAGE) $(AMDFWTOOL) $(AMDCOMPRESS) $(FUTILITY) $(BINCFG) $(IFITTOOL) $(objutil)/supermicro/smcbiosinfo $(CSE_FPT) $(CSE_SERGER) $(AMDFWREAD)
824
825###########################################################################
826# Common recipes for all stages
827###########################################################################
828
829# loadaddr can determine the load address of a stage, which may be needed for
830# platform-specific image headers (only works *after* the stage has been built)
831loadaddr = $(shell $(OBJDUMP_$(1)) -p $(objcbfs)/$(1).debug | \
832	     sed -ne '/LOAD/s/^.*vaddr 0x\([0-9a-fA-F]\{8\}\).*$$/0x\1/p')
833
834# find-substr is required for stages like romstage_null and romstage_xip to
835# eliminate the _* part of the string
836find-substr = $(word 1,$(subst _, ,$(1)))
837
838# find-class is used to identify the class from the name of the stage
839# The input to this macro can be something like romstage.x or romstage.x.y
840# find-class recursively strips off the suffixes to extract the exact class name
841# e.g.: if romstage.x is provided to find-class, it will remove .x and return romstage
842# if romstage.x.y is provided, it will first remove .y, call find-class with romstage.x
843# and remove .x the next time and finally return romstage
844find-class = $(if $(filter $(1),$(basename $(1))),$(if $(CC_$(1)), $(1), $(call find-substr,$(1))),$(call find-class,$(basename $(1))))
845
846# Bootblocks are not CBFS stages. coreboot is currently expecting the bss to
847# be cleared by the loader of the stage. For ARM SoCs that means one needs to
848# include the bss section in the binary so the BootROM clears the bss on
849# loading of the bootblock stage. Achieve this by marking the bss section
850# loadable,allocatable, and data. Do the same for the .data section in case
851# the linker marked it NOBITS automatically because there are only zeroes in it.
852preserve-bss-flags := --set-section-flags .bss=load,alloc,data --set-section-flags .data=load,alloc,data
853
854# For Intel TXT files in the CBFS needs to be marked as 'Initial Boot Block'.
855# As CBFS attributes aren't cheap, only mark them if TXT is enabled.
856ifeq ($(CONFIG_INTEL_TXT),y)
857
858TXTIBB := --ibb
859
860else
861
862TXTIBB :=
863
864endif
865
866ifeq ($(CONFIG_COMPRESS_BOOTBLOCK),y)
867
868$(objcbfs)/bootblock.lz4: $(objcbfs)/bootblock.elf $(objutil)/cbfstool/cbfs-compression-tool
869	@printf "    LZ4        $(subst $(obj)/,,$(@))\n"
870	$(OBJCOPY_bootblock) $(preserve-bss-flags) $< $@.tmp
871	$(OBJCOPY_bootblock) -O binary $@.tmp
872	$(objutil)/cbfstool/cbfs-compression-tool rawcompress $@.tmp $@.tmp2 lz4
873	rm -f $@.tmp
874	mv $@.tmp2 $@
875
876# Put assembled decompressor+bootblock into bootblock.raw.elf so that SoC
877# Makefiles wrapping the bootblock in a header can always key off the same file.
878$(objcbfs)/bootblock.raw.elf: $(objcbfs)/decompressor.elf
879	@printf "    OBJCOPY    $(notdir $(@))\n"
880	$(OBJCOPY_bootblock) $(preserve-bss-flags) $< $@
881
882else	# CONFIG_COMPRESS_BOOTBLOCK
883
884$(objcbfs)/bootblock.raw.elf: $(objcbfs)/bootblock.elf
885	@printf "    OBJCOPY    $(notdir $(@))\n"
886	$(OBJCOPY_bootblock) $(preserve-bss-flags) $< $@
887
888endif	# CONFIG_COMPRESS_BOOTBLOCK
889
890$(objcbfs)/bootblock.raw.bin: $(objcbfs)/bootblock.raw.elf
891	@printf "    OBJCOPY    $(notdir $(@))\n"
892	$(OBJCOPY_bootblock) -O binary $< $@
893
894ifneq ($(CONFIG_HAVE_BOOTBLOCK),y)
895$(objcbfs)/bootblock.bin:
896	dd if=/dev/zero of=$@ bs=64 count=1
897endif
898
899$(objcbfs)/%.bin: $(objcbfs)/%.raw.bin
900	cp $< $@
901
902$(objcbfs)/%.map: $(objcbfs)/%.debug
903	$(eval class := $(call find-class,$(@F)))
904	$(NM_$(class)) -n $< | sort > $(basename $@).map
905
906$(objcbfs)/%.elf: $(objcbfs)/%.debug $(objcbfs)/%.map
907	$(eval class := $(call find-class,$(@F)))
908	@printf "    OBJCOPY    $(subst $(obj)/,,$(@))\n"
909	cp $< $@.tmp
910	$(OBJCOPY_$(class)) --strip-debug $@.tmp
911	$(OBJCOPY_$(class)) --add-gnu-debuglink=$< $@.tmp
912	mv $@.tmp $@
913
914
915###########################################################################
916# Build the final rom image
917###########################################################################
918
919# extract_nth - Return a subsection of the $file string
920#
921# the input string looks like this:
922# ./build/cbfs/fallback/romstage.elf|fallback/romstage|stage|none||64|--xip*-S*.car.data*-P*0x10000
923#
924# Sections:
925# 1 - Path and name of file [FILENAME: Added to cbfs-files-y list variable]
926# 2 - Name of file in cbfs  [$(FILENAME)-file]
927# 3 - File type:            [$(FILENAME)-type]
928#                bootblock, cbfs header, stage, payload, optionrom, bootsplash, raw, vsa,
929#                mbi, microcode, fsp, mrc, cmos_default, cmos_layout, spd, mrc_cache,
930#                mma, efi, deleted, null
931# 4 - Compression type      [$(FILENAME)-compression]
932#                      none, LZMA
933# 5 - Base address          [$(FILENAME)-position]
934# 6 - Alignment             [$(FILENAME)-align]
935# 7 - cbfstool flags        [$(FILENAME)-options]
936#
937# Input:
938#  $(1) = Section to extract
939#  $(2) = Input string
940#
941# Steps:
942# 1) replace all '|' characters with the sequence '- -' within the full string, prepended and appended with the character '-'
943# 2) extract the specified section from the string - this gets us the section surrounded by '-' characters
944# 3) remove the leading and trailing '-' characters
945# 4) replace all '*' characters with spaces
946extract_nth=$(subst *,$(spc),$(patsubst -%-,%,$(word $(1), $(subst |,- -,-$(2)-))))
947
948# regions-for-file - Returns a cbfstool regions parameter
949# $(call regions-for-file,$(filename))
950# returns "REGION1,REGION2,..."
951#
952# This is the default implementation. When using a boot strategy employing
953# multiple CBFSes in fmap regions, override it.
954regions-for-file ?= $(if $(value regions-for-file-$(1)), $(regions-for-file-$(1)), COREBOOT)
955
956ifeq ($(CONFIG_CBFS_AUTOGEN_ATTRIBUTES),y)
957	cbfs-autogen-attributes=-g
958endif
959
960# cbfs-add-cmd-for-region
961# $(call cbfs-add-cmd-for-region,file in extract_nth format,region name)
962#
963# CBFSTOOL_ADD_CMD_OPTIONS can be used by arch/SoC/mainboard to supply
964# add commands with any additional arguments for cbfstool.
965# Example: --ext-win-base <base> --ext-win-size <size>
966define cbfs-add-cmd-for-region
967	$(CBFSTOOL) $@.tmp \
968	add$(if $(filter stage,$(call extract_nth,3,$(1))),-stage)$(if \
969		$(filter payload,$(call extract_nth,3,$(1))),-payload)$(if \
970		$(filter flat-binary,$(call extract_nth,3,$(1))),-flat-binary) \
971	-f $(call extract_nth,1,$(1)) \
972	-n $(call extract_nth,2,$(1)) \
973	$(if $(filter-out flat-binary payload stage,$(call \
974		extract_nth,3,$(1))),-t $(call extract_nth,3,$(1))) \
975	$(if $(call extract_nth,4,$(1)),-c $(call extract_nth,4,$(1))) \
976	$(cbfs-autogen-attributes) \
977	-r $(2) \
978	$(if $(call extract_nth,6,$(1)),-a $(call extract_nth,6,$(file)), \
979		$(if $(call extract_nth,5,$(file)),-b $(call extract_nth,5,$(file)))) \
980		$(call extract_nth,7,$(1)) \
981	$(CBFSTOOL_ADD_CMD_OPTIONS)
982
983endef
984# Empty line before endef is necessary so cbfs-add-cmd-for-region ends in a
985# newline
986
987# cbfs-add-cmd
988# $(call cbfs-add-cmd,
989#          file in extract_nth format,
990#          region name,
991#          non-empty if file removal requested)
992define cbfs-add-cmd
993	printf "    CBFS       $(call extract_nth,2,$(1))\n"
994	$(if $(3),-$(CBFSTOOL) $@.tmp remove -n $(call extract_nth,2,$(file)) 2>/dev/null)
995	$(call cbfs-add-cmd-for-region,$(1),$(2))
996endef
997
998# list of files to add (using their file system names, not CBFS names),
999# for dependencies etc.
1000prebuilt-files = $(foreach file,$(cbfs-files), $(call extract_nth,1,$(file)))
1001
1002# $(all-regions)
1003# returns full list of fmap regions that we add files to
1004all-regions = $(sort $(subst $(comma),$(spc), \
1005	$(foreach file,$(cbfs-files), \
1006		$(call regions-for-file,$(call extract_nth,2,$(file))))))
1007
1008# $(call all-files-in-region,region name)
1009# returns elements in $(cbfs-files) that end up in that region, in the order
1010# they appear in $(cbfs-files)
1011all-files-in-region = $(foreach file,$(cbfs-files), \
1012	$(if $(filter $(1), \
1013		$(subst $(comma),$(spc),$(call regions-for-file,$(call extract_nth,2,$(file))))), \
1014		$(file)))
1015
1016# $(call update-file-for-region,file string from $(cbfs-files),region name)
1017# Update position and alignment according to overrides for region
1018# Doesn't check for invalid configurations (eg. resetting neither or both
1019# position and align)
1020# Returns the updated file string
1021update-file-for-region = \
1022	$(subst $(spc),*,$(call extract_nth,1,$(1))|$(call extract_nth,2,$(1))|$(call extract_nth,3,$(1))|$(call extract_nth,4,$(1))|$($(call extract_nth,2,$(1))-$(2)-position)|$($(call extract_nth,2,$(1))-$(2)-align)|$(call extract_nth,7,$(1)))
1023
1024# $(call placed-files-in-region,region name)
1025# like all-files-in-region, but updates the files to contain region overrides
1026# to position or alignment.
1027placed-files-in-region = $(foreach file,$(call all-files-in-region,$(1)), \
1028	$(if $($(call extract_nth,2,$(file))-$(1)-position), \
1029		$(if $($(call extract_nth,2,$(file))-$(1)-align), \
1030			$(error It is not allowed to specify both alignment and position for $(call extract_nth,2,$(file))-$(1))) \
1031		$(call update-file-for-region,$(file),$(1)), \
1032		$(if $($(call extract_nth,2,$(file))-$(1)-align), \
1033			$(call update-file-for-region,$(file),$(1)), \
1034			$(file))))
1035
1036# $(call sort-files,subset of $(cbfs-files))
1037# reorders the files in the given set to list files at fixed positions first,
1038# followed by aligned files and finally those with no constraints.
1039sort-files = \
1040	$(eval _tmp_fixed:=) \
1041	$(eval _tmp_aligned:=) \
1042	$(eval _tmp_regular:=) \
1043	$(foreach file,$(1), \
1044		$(if $(call extract_nth,5,$(file)),\
1045			$(eval _tmp_fixed += $(file)), \
1046		$(if $(call extract_nth,6,$(file)), \
1047			$(eval _tmp_aligned += $(file)), \
1048			$(eval _tmp_regular += $(file))))) \
1049	$(_tmp_fixed) $(_tmp_aligned) $(_tmp_regular)
1050
1051# command list to add files to CBFS
1052prebuild-files = $(foreach region,$(all-regions), \
1053	$(foreach file, \
1054		$(call sort-files,$(call placed-files-in-region,$(region))), \
1055		$(call cbfs-add-cmd,$(file),$(region),$(CONFIG_UPDATE_IMAGE))))
1056
1057# If no FMD file (Flashmap) is supplied by mainboard, fall back to a default
1058ifeq ($(CONFIG_FMDFILE),)
1059
1060ifeq ($(CONFIG_ARCH_X86),y)
1061
1062DEFAULT_FLASHMAP:=$(top)/util/cbfstool/default-x86.fmd
1063# check if IFD_CHIPSET is set and if yes generate a FMAP template from IFD descriptor
1064ifneq ($(CONFIG_IFD_CHIPSET),)
1065ifeq ($(CONFIG_HAVE_IFD_BIN),y)
1066DEFAULT_FLASHMAP:=$(obj)/fmap-template.fmd
1067$(DEFAULT_FLASHMAP): $(call strip_quotes,$(CONFIG_IFD_BIN_PATH)) $(IFDTOOL)
1068	echo "    IFDTOOL    -p $(CONFIG_IFD_CHIPSET) -F $@ $<"
1069	$(IFDTOOL) -p $(CONFIG_IFD_CHIPSET) -F $@ $<
1070endif # ifeq($(CONFIG_HAVE_IFD_BIN),y)
1071endif # ifneq($(CONFIG_IFD_CHIPSET),)
1072
1073# entire flash
1074FMAP_ROM_ADDR := $(call int-subtract, 0x100000000 $(CONFIG_ROM_SIZE))
1075FMAP_ROM_SIZE := $(CONFIG_ROM_SIZE)
1076# entire "BIOS" region (everything directly of concern to the host system)
1077# relative to ROM_BASE
1078FMAP_BIOS_BASE := $(call int-align, $(call int-subtract, $(CONFIG_ROM_SIZE) $(CONFIG_CBFS_SIZE)), 0x10000)
1079FMAP_BIOS_SIZE := $(call int-align-down, $(shell echo $(CONFIG_CBFS_SIZE) | tr A-F a-f), 0x10000)
1080# position and size of flashmap, relative to BIOS_BASE
1081
1082#
1083# X86 CONSOLE FMAP region
1084#
1085# position, size and entry line of CONSOLE relative to BIOS_BASE, if enabled
1086
1087FMAP_CURRENT_BASE := 0
1088
1089ifeq ($(CONFIG_CONSOLE_SPI_FLASH),y)
1090FMAP_CONSOLE_BASE := $(FMAP_CURRENT_BASE)
1091FMAP_CONSOLE_SIZE := $(CONFIG_CONSOLE_SPI_FLASH_BUFFER_SIZE)
1092FMAP_CONSOLE_ENTRY := CONSOLE@$(FMAP_CONSOLE_BASE) $(FMAP_CONSOLE_SIZE)
1093FMAP_CURRENT_BASE := $(call int-add, $(FMAP_CONSOLE_BASE) $(FMAP_CONSOLE_SIZE))
1094else
1095FMAP_CONSOLE_ENTRY :=
1096endif
1097
1098ifeq ($(CONFIG_CACHE_MRC_SETTINGS),y)
1099FMAP_MRC_CACHE_BASE := $(call int-align, $(FMAP_CURRENT_BASE), 0x10000)
1100FMAP_MRC_CACHE_SIZE := $(CONFIG_MRC_SETTINGS_CACHE_SIZE)
1101FMAP_MRC_CACHE_ENTRY := RW_MRC_CACHE@$(FMAP_MRC_CACHE_BASE) $(FMAP_MRC_CACHE_SIZE)
1102FMAP_CURRENT_BASE := $(call int-add, $(FMAP_MRC_CACHE_BASE) $(FMAP_MRC_CACHE_SIZE))
1103else
1104FMAP_MRC_CACHE_ENTRY :=
1105endif
1106
1107ifeq ($(CONFIG_SMMSTORE),y)
1108FMAP_SMMSTORE_BASE := $(call int-align, $(FMAP_CURRENT_BASE), 0x10000)
1109FMAP_SMMSTORE_SIZE := $(CONFIG_SMMSTORE_SIZE)
1110FMAP_SMMSTORE_ENTRY := SMMSTORE@$(FMAP_SMMSTORE_BASE) $(FMAP_SMMSTORE_SIZE)
1111FMAP_CURRENT_BASE := $(call int-add, $(FMAP_SMMSTORE_BASE) $(FMAP_SMMSTORE_SIZE))
1112else
1113FMAP_SMMSTORE_ENTRY :=
1114endif
1115
1116ifeq ($(CONFIG_SPD_CACHE_IN_FMAP),y)
1117FMAP_SPD_CACHE_BASE := $(call int-align, $(FMAP_CURRENT_BASE), 0x4000)
1118FMAP_SPD_CACHE_SIZE := $(call int-multiply, $(CONFIG_DIMM_MAX) $(CONFIG_DIMM_SPD_SIZE))
1119FMAP_SPD_CACHE_SIZE := $(call int-align, $(FMAP_SPD_CACHE_SIZE), 0x1000)
1120FMAP_SPD_CACHE_ENTRY := $(CONFIG_SPD_CACHE_FMAP_NAME)@$(FMAP_SPD_CACHE_BASE) $(FMAP_SPD_CACHE_SIZE)
1121FMAP_CURRENT_BASE := $(call int-add, $(FMAP_SPD_CACHE_BASE) $(FMAP_SPD_CACHE_SIZE))
1122else
1123FMAP_SPD_CACHE_ENTRY :=
1124endif
1125
1126ifeq ($(CONFIG_VPD),y)
1127FMAP_VPD_BASE := $(call int-align, $(FMAP_CURRENT_BASE), 0x4000)
1128FMAP_VPD_SIZE := $(CONFIG_VPD_FMAP_SIZE)
1129FMAP_VPD_ENTRY := $(CONFIG_VPD_FMAP_NAME)@$(FMAP_VPD_BASE) $(FMAP_VPD_SIZE)
1130FMAP_CURRENT_BASE := $(call int-add, $(FMAP_VPD_BASE) $(FMAP_VPD_SIZE))
1131else
1132FMAP_VPD_ENTRY :=
1133endif
1134
1135ifeq ($(CONFIG_INCLUDE_HSPHY_IN_FMAP),y)
1136FMAP_HSPHY_FW_BASE := $(call int-align, $(FMAP_CURRENT_BASE), 0x1000)
1137FMAP_HSPHY_FW_SIZE := $(CONFIG_HSPHY_FW_MAX_SIZE)
1138FMAP_HSPHY_FW_ENTRY := HSPHY_FW@$(FMAP_HSPHY_FW_BASE) $(FMAP_HSPHY_FW_SIZE)
1139FMAP_CURRENT_BASE := $(call int-add, $(FMAP_HSPHY_FW_BASE) $(FMAP_HSPHY_FW_SIZE))
1140else
1141FMAP_HSPHY_FW_ENTRY :=
1142endif
1143
1144#
1145# X86 FMAP region
1146#
1147#
1148# position, size
1149FMAP_FMAP_BASE := $(FMAP_CURRENT_BASE)
1150FMAP_FMAP_SIZE := 0x200
1151
1152#
1153# X86 COREBOOT default cbfs FMAP region
1154#
1155# position and size of CBFS, relative to BIOS_BASE
1156FMAP_CBFS_BASE := $(call int-add, $(FMAP_FMAP_BASE) $(FMAP_FMAP_SIZE))
1157FMAP_CBFS_SIZE := $(call int-subtract, $(FMAP_BIOS_SIZE) $(FMAP_CBFS_BASE))
1158
1159else # ifeq ($(CONFIG_ARCH_X86),y)
1160
1161DEFAULT_FLASHMAP:=$(top)/util/cbfstool/default.fmd
1162# entire flash
1163FMAP_ROM_ADDR := 0
1164FMAP_ROM_SIZE := $(CONFIG_ROM_SIZE)
1165# entire "BIOS" region (everything directly of concern to the host system)
1166# relative to ROM_BASE
1167FMAP_BIOS_BASE := 0
1168FMAP_BIOS_SIZE := $(CONFIG_CBFS_SIZE)
1169# position and size of flashmap, relative to BIOS_BASE
1170FMAP_FMAP_BASE := 0x20000
1171FMAP_FMAP_SIZE := 0x200
1172
1173FMAP_CURRENT_BASE := $(call int-add, $(FMAP_FMAP_BASE) $(FMAP_FMAP_SIZE))
1174
1175#
1176# NON-X86 CONSOLE FMAP region
1177#
1178# position, size and entry line of CONSOLE relative to BIOS_BASE, if enabled
1179ifeq ($(CONFIG_CONSOLE_SPI_FLASH),y)
1180FMAP_CONSOLE_BASE := $(FMAP_CURRENT_BASE)
1181FMAP_CONSOLE_SIZE := $(CONFIG_CONSOLE_SPI_FLASH_BUFFER_SIZE)
1182FMAP_CONSOLE_ENTRY := CONSOLE@$(FMAP_CONSOLE_BASE) $(FMAP_CONSOLE_SIZE)
1183FMAP_CURRENT_BASE := $(call int-add, $(FMAP_CONSOLE_BASE) $(FMAP_CONSOLE_SIZE))
1184else
1185FMAP_CONSOLE_ENTRY :=
1186endif
1187
1188#
1189# NON-X86 RW_MRC_CACHE FMAP region
1190#
1191# position, size and entry line of MRC_CACHE relative to BIOS_BASE, if enabled
1192ifeq ($(CONFIG_CACHE_MRC_SETTINGS),y)
1193FMAP_MRC_CACHE_BASE := $(call int-align, $(FMAP_CURRENT_BASE), 0x10000)
1194FMAP_MRC_CACHE_SIZE := $(CONFIG_MRC_SETTINGS_CACHE_SIZE)
1195FMAP_MRC_CACHE_ENTRY := RW_MRC_CACHE@$(FMAP_MRC_CACHE_BASE) $(FMAP_MRC_CACHE_SIZE)
1196FMAP_CURRENT_BASE := $(call int-add, $(FMAP_MRC_CACHE_BASE) $(FMAP_MRC_CACHE_SIZE))
1197else
1198FMAP_MRC_CACHE_ENTRY :=
1199endif
1200
1201#
1202# NON-X86 COREBOOT default cbfs FMAP region
1203#
1204# position and size of CBFS, relative to BIOS_BASE
1205FMAP_CBFS_BASE := $(FMAP_CURRENT_BASE)
1206FMAP_CBFS_SIZE := $(call int-subtract,$(FMAP_BIOS_SIZE) $(FMAP_CBFS_BASE))
1207
1208endif # ifeq ($(CONFIG_ARCH_X86),y)
1209
1210$(obj)/fmap.fmd: $(top)/Makefile.mk $(DEFAULT_FLASHMAP) $(obj)/config.h
1211	sed -e "s,##ROM_BASE##,$(FMAP_ROM_ADDR)," \
1212	    -e "s,##ROM_SIZE##,$(FMAP_ROM_SIZE)," \
1213	    -e "s,##BIOS_BASE##,$(FMAP_BIOS_BASE)," \
1214	    -e "s,##BIOS_SIZE##,$(FMAP_BIOS_SIZE)," \
1215	    -e "s,##FMAP_BASE##,$(FMAP_FMAP_BASE)," \
1216	    -e "s,##FMAP_SIZE##,$(FMAP_FMAP_SIZE)," \
1217	    -e "s,##CONSOLE_ENTRY##,$(FMAP_CONSOLE_ENTRY)," \
1218	    -e "s,##MRC_CACHE_ENTRY##,$(FMAP_MRC_CACHE_ENTRY)," \
1219	    -e "s,##SMMSTORE_ENTRY##,$(FMAP_SMMSTORE_ENTRY)," \
1220	    -e "s,##SPD_CACHE_ENTRY##,$(FMAP_SPD_CACHE_ENTRY)," \
1221	    -e "s,##VPD_ENTRY##,$(FMAP_VPD_ENTRY)," \
1222	    -e "s,##HSPHY_FW_ENTRY##,$(FMAP_HSPHY_FW_ENTRY)," \
1223	    -e "s,##CBFS_BASE##,$(FMAP_CBFS_BASE)," \
1224	    -e "s,##CBFS_SIZE##,$(FMAP_CBFS_SIZE)," \
1225		$(DEFAULT_FLASHMAP) > $@.tmp
1226	mv $@.tmp $@
1227else # ifeq ($(CONFIG_FMDFILE),)
1228$(obj)/fmap.fmd: $(CONFIG_FMDFILE) $(obj)/config.h
1229	$(HOSTCC) $(PREPROCESS_ONLY) -include $(obj)/config.h $< -o $@.pre
1230	mv $@.pre $@
1231endif # ifeq ($(CONFIG_FMDFILE),)
1232
1233# generated at the same time as fmap.fmap
1234$(obj)/fmap_config.h: $(obj)/fmap.fmap
1235	true
1236$(obj)/fmap.desc: $(obj)/fmap.fmap
1237	true
1238
1239$(obj)/fmap.fmap: $(obj)/fmap.fmd $(FMAPTOOL)
1240	echo "    FMAP       $(FMAPTOOL) -h $(obj)/fmap_config.h $< $@"
1241	$(FMAPTOOL) -h $(obj)/fmap_config.h -R $(obj)/fmap.desc $< $@
1242
1243ifeq ($(CONFIG_INTEL_ADD_TOP_SWAP_BOOTBLOCK),y)
1244TS_OPTIONS := -j $(CONFIG_INTEL_TOP_SWAP_BOOTBLOCK_SIZE)
1245endif
1246
1247ifneq ($(CONFIG_ARCH_X86),y)
1248add_bootblock = $(CBFSTOOL) $(1) write -u -r BOOTBLOCK -f $(2)
1249endif
1250
1251# coreboot.pre doesn't follow the standard Make conventions. It gets modified
1252# by multiple rules, and thus we can't compute the dependencies correctly.
1253$(shell rm -f $(obj)/coreboot.pre)
1254
1255ifneq ($(CONFIG_UPDATE_IMAGE),y)
1256$(obj)/coreboot.pre: $$(prebuilt-files) $(CBFSTOOL) $(obj)/fmap.fmap $(obj)/fmap.desc $(objcbfs)/bootblock.bin
1257	$(CBFSTOOL) $@.tmp create -M $(obj)/fmap.fmap -r $(shell cat $(obj)/fmap.desc)
1258	printf "    BOOTBLOCK\n"
1259	$(call add_bootblock,$@.tmp,$(objcbfs)/bootblock.bin)
1260	$(prebuild-files) true
1261	mv $@.tmp $@
1262else # ifneq ($(CONFIG_UPDATE_IMAGE),y)
1263.PHONY: $(obj)/coreboot.pre
1264$(obj)/coreboot.pre: $$(prebuilt-files) $(CBFSTOOL)
1265	mv $(obj)/coreboot.rom $@.tmp || \
1266	(echo "Error: You have UPDATE_IMAGE set in Kconfig, but have no existing image to update." && \
1267	echo "Exiting." && \
1268	false)
1269	$(prebuild-files) true
1270	mv $@.tmp $@
1271endif # ifneq ($(CONFIG_UPDATE_IMAGE),y)
1272
1273ifeq ($(CONFIG_HAVE_REFCODE_BLOB),y)
1274REFCODE_BLOB=$(obj)/refcode.rmod
1275$(REFCODE_BLOB): $(RMODTOOL)
1276	$(RMODTOOL) -i $(CONFIG_REFCODE_BLOB_FILE) -o $@
1277endif
1278
1279ifeq ($(CONFIG_HAVE_RAMSTAGE),y)
1280RAMSTAGE=$(objcbfs)/ramstage.elf
1281else
1282RAMSTAGE=
1283endif
1284
1285add_intermediate = \
1286	$(1): $(obj)/coreboot.pre $(2) | $(INTERMEDIATE) \
1287	$(eval INTERMEDIATE+=$(1)) $(eval PHONY+=$(1))
1288
1289$(obj)/coreboot.rom: $(obj)/coreboot.pre $(CBFSTOOL) $(IFITTOOL) $$(INTERMEDIATE)
1290	@printf "    CBFS       $(subst $(obj)/,,$(@))\n"
1291# The full ROM may be larger than the CBFS part, so create an empty
1292# file (filled with \377 = 0xff) and copy the CBFS image over it.
1293	dd if=/dev/zero bs=$(call _toint,$(CONFIG_ROM_SIZE)) count=1 2> /dev/null | tr '\000' '\377' > $@.tmp
1294	dd if=$(obj)/coreboot.pre of=$@.tmp bs=8192 conv=notrunc 2> /dev/null
1295ifeq ($(CONFIG_CPU_INTEL_FIRMWARE_INTERFACE_TABLE),y)
1296# Print final FIT table
1297	$(IFITTOOL) -f $@.tmp -D -r COREBOOT
1298# Print final TS BOOTBLOCK FIT table
1299ifeq ($(CONFIG_INTEL_ADD_TOP_SWAP_BOOTBLOCK),y)
1300	@printf "    TOP SWAP FIT table\n"
1301	$(IFITTOOL) -f $@.tmp -D $(TS_OPTIONS) -r COREBOOT
1302endif # CONFIG_INTEL_ADD_TOP_SWAP_BOOTBLOCK
1303endif # CONFIG_CPU_INTEL_FIRMWARE_INTERFACE_TABLE
1304	mv $@.tmp $@
1305	@printf "    CBFSLAYOUT  $(subst $(obj)/,,$(@))\n\n"
1306	$(CBFSTOOL) $@ layout
1307	@printf "    CBFSPRINT  $(subst $(obj)/,,$(@))\n\n"
1308ifeq ($(CONFIG_CBFS_VERIFICATION),y)
1309	line=$$($(CBFSTOOL) $@ print -kv 2>/dev/null | grep -F '[CBFS VERIFICATION (COREBOOT)]') ;\
1310	if ! printf "$$line" | grep -q 'fully valid'; then \
1311		echo "CBFS verification error: $$line" ;\
1312		exit 1 ;\
1313	fi
1314endif # CONFIG_CBFS_VERIFICATION
1315
1316ifeq ($(CONFIG_SEPARATE_ROMSTAGE),y)
1317cbfs-files-y += $(CONFIG_CBFS_PREFIX)/romstage
1318$(CONFIG_CBFS_PREFIX)/romstage-file := $(objcbfs)/romstage.elf
1319$(CONFIG_CBFS_PREFIX)/romstage-type := stage
1320$(CONFIG_CBFS_PREFIX)/romstage-compression := $(CBFS_PRERAM_COMPRESS_FLAG)
1321ifeq ($(CONFIG_ARCH_ROMSTAGE_ARM),y)
1322$(CONFIG_CBFS_PREFIX)/romstage-options := -b 0
1323endif
1324ifeq ($(CONFIG_ARCH_ROMSTAGE_X86_32)$(CONFIG_ARCH_ROMSTAGE_X86_64),y)
1325ifeq ($(CONFIG_NO_XIP_EARLY_STAGES),y)
1326$(CONFIG_CBFS_PREFIX)/romstage-options := -S ".car.data"
1327else
1328$(CONFIG_CBFS_PREFIX)/romstage-options := -S ".car.data,.data"
1329endif
1330
1331# If CAR does not support execution of code, romstage on x86 is expected to be
1332# xip.
1333ifneq ($(CONFIG_NO_XIP_EARLY_STAGES),y)
1334$(CONFIG_CBFS_PREFIX)/romstage-options += --xip
1335
1336# For efficient MTRR utilisation use natural alignment for romstage.
1337ifeq ($(CONFIG_SETUP_XIP_CACHE),y)
1338$(CONFIG_CBFS_PREFIX)/romstage-options += --pow2page
1339endif    # CONFIG_SETUP_XIP_CACHE
1340
1341endif   # CONFIG_NO_XIP_EARLY_STAGES
1342endif   # CONFIG_ARCH_ROMSTAGE_X86_32 / CONFIG_ARCH_ROMSTAGE_X86_64
1343ifeq ($(CONFIG_VBOOT_STARTS_IN_ROMSTAGE),y)
1344$(CONFIG_CBFS_PREFIX)/romstage-options += $(TXTIBB)
1345endif
1346else # CONFIG_SEPARATE_ROMSTAGE
1347postinclude-hooks += $$(eval bootblock-srcs += $$(romstage-srcs))
1348postinclude-hooks += $$(eval bootblock-libs += $$(romstage-libs))
1349endif
1350
1351cbfs-files-$(CONFIG_HAVE_RAMSTAGE) += $(CONFIG_CBFS_PREFIX)/ramstage
1352$(CONFIG_CBFS_PREFIX)/ramstage-file := $(RAMSTAGE)
1353$(CONFIG_CBFS_PREFIX)/ramstage-type := stage
1354$(CONFIG_CBFS_PREFIX)/ramstage-compression := $(CBFS_COMPRESS_FLAG)
1355# The AMD LPC SPI DMA controller requires source files to be 64 byte aligned.
1356ifeq ($(CONFIG_SOC_AMD_COMMON_BLOCK_LPC_SPI_DMA),y)
1357$(CONFIG_CBFS_PREFIX)/ramstage-align := 64
1358endif
1359
1360cbfs-files-$(CONFIG_HAVE_REFCODE_BLOB) += $(CONFIG_CBFS_PREFIX)/refcode
1361$(CONFIG_CBFS_PREFIX)/refcode-file := $(REFCODE_BLOB)
1362$(CONFIG_CBFS_PREFIX)/refcode-type := stage
1363$(CONFIG_CBFS_PREFIX)/refcode-compression := $(CBFS_COMPRESS_FLAG)
1364
1365cbfs-files-$(CONFIG_SEABIOS_VGA_COREBOOT) += vgaroms/seavgabios.bin
1366vgaroms/seavgabios.bin-file := $(CONFIG_PAYLOAD_VGABIOS_FILE)
1367vgaroms/seavgabios.bin-type := raw
1368
1369cbfs-files-$(CONFIG_INCLUDE_CONFIG_FILE) += config
1370config-file := $(DOTCONFIG):config
1371config-type := raw
1372config-compression := LZMA
1373
1374cbfs-files-$(CONFIG_INCLUDE_CONFIG_FILE) += revision
1375revision-file := $(obj)/build.h
1376revision-type := raw
1377
1378cbfs-files-y += build_info
1379build_info-file := $(obj)/build_info
1380build_info-type := raw
1381
1382ifeq ($(CONFIG_BOOTSPLASH_CONVERT),y)
1383ifeq ($(shell command -v convert),)
1384$(error CONFIG_BOOTSPLASH_CONVERT requires the convert program (part of ImageMagick))
1385endif
1386cbfs-files-$(CONFIG_BOOTSPLASH_IMAGE) += bootsplash.jpg
1387bootsplash.jpg-file := $(call strip_quotes,$(CONFIG_BOOTSPLASH_FILE)):jpg420
1388bootsplash.jpg-type := bootsplash
1389else
1390BOOTSPLASH_SUFFIX=$(suffix $(call strip_quotes,$(CONFIG_BOOTSPLASH_FILE)))
1391cbfs-files-$(CONFIG_BOOTSPLASH_IMAGE) += bootsplash$(BOOTSPLASH_SUFFIX)
1392bootsplash$(BOOTSPLASH_SUFFIX)-file := $(call strip_quotes,$(CONFIG_BOOTSPLASH_FILE))
1393bootsplash$(BOOTSPLASH_SUFFIX)-type := bootsplash
1394endif
1395
1396# Ensure that no payload segment overlaps with memory regions used by ramstage
1397# (not for x86 since it can relocate itself in that case)
1398ifneq ($(CONFIG_ARCH_X86),y)
1399check-ramstage-overlap-regions := ramstage
1400check-ramstage-overlap-files :=
1401ifneq ($(CONFIG_PAYLOAD_NONE),y)
1402check-ramstage-overlap-files += $(CONFIG_CBFS_PREFIX)/payload
1403endif
1404
1405# will output one or more lines of "<load address in hex> <memlen in decimal>"
1406cbfs-get-segments-cmd = $(CBFSTOOL) $(obj)/coreboot.pre print -v | sed -n \
1407	'\%$(1)%,\%^[^ ]\{4\}%s%    .*load: \(0x[0-9a-fA-F]*\),.*length: [0-9]*/\([0-9]*\).*%\1 \2%p'
1408
1409ramstage-symbol-addr-cmd = $(OBJDUMP_ramstage) -t $(objcbfs)/ramstage.elf | \
1410	sed -n '/ $(1)$$/s/^\([0-9a-fA-F]*\) .*/0x\1/p' | \
1411	uniq
1412
1413$(call add_intermediate, check-ramstage-overlaps)
1414	programs=$$($(foreach file,$(check-ramstage-overlap-files), \
1415		$(call cbfs-get-segments-cmd,$(file)) ; )) ; \
1416	regions=$$($(foreach region,$(check-ramstage-overlap-regions), \
1417		echo $(region) ; \
1418		$(call ramstage-symbol-addr-cmd,_$(region)) ; \
1419		$(call ramstage-symbol-addr-cmd,_e$(region)) ; )) ; \
1420	pstart= ; pend= ; \
1421	for x in $$programs; do \
1422	    if [ -z $$pstart ]; then pstart=$$(($$x)) ; continue ; fi ; \
1423	    pend=$$(($$pstart + $$x)) ; \
1424	    rname= ; rstart= ; rend= ; \
1425	    for y in $$regions ; do \
1426	        if [ -z $$rname ]; then rname=$$y ; continue ; fi ; \
1427	        if [ -z $$rstart ]; then rstart=$$(($$y)) ; continue ; fi ; \
1428	        rend=$$(($$y)) ; \
1429	        if [ $$pstart -lt $$rend -a $$rstart -lt $$pend ]; then \
1430	            echo "ERROR: Ramstage region _$$rname@($$rstart,$$rend) overlapped by($$pstart,$$pend):" \
1431	                 $(check-ramstage-overlap-files) ; \
1432	            exit 1 ; \
1433	        fi ; \
1434	        rname= ; rstart= ; rend= ; \
1435	    done ; \
1436	    pstart= ; pend= ; \
1437	done
1438
1439endif
1440