xref: /aosp_15_r20/external/vboot_reference/Makefile (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1# Copyright 2013 The ChromiumOS Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# This Makefile normally builds in a 'build' subdir, but use
6#
7#    make BUILD=<dir>
8#
9# to put the output somewhere else.
10
11##############################################################################
12# Make variables come in two flavors, immediate or deferred.
13#
14#   Variable definitions are parsed like this:
15#
16#        IMMEDIATE = DEFERRED
17#    or
18#        IMMEDIATE := IMMEDIATE
19#
20#   Rules are parsed this way:
21#
22#        IMMEDIATE : IMMEDIATE
23#           DEFERRED
24#
25# So you can assign variables in any order if they're only to be used in
26# actions, but if you use a variable in either the target or prerequisite of a
27# rule, the rule will be constructed using only the top-down, immediate value.
28#
29# So we'll try to define all the variables first. Then the rules.
30#
31
32##############################################################################
33# Configuration variables come first.
34#
35# Our convention is that we only use := for variables that will never be
36# changed or appended. They must be defined before being used anywhere.
37
38# We should only run pwd once, not every time we refer to ${BUILD}.
39SRCDIR := $(shell pwd)
40BUILD = ${SRCDIR}/build
41export BUILD
42
43# Stuff for 'make install'
44INSTALL = install
45DESTDIR = /
46LIBDIR ?= lib
47
48# Default values
49DEV_DEBUG_FORCE=
50
51# Where exactly do the pieces go?
52#  UB_DIR = utility binary directory
53#  ULP_DIR = pkgconfig directory, usually /usr/lib/pkgconfig
54#  UI_DIR = include directory for library headers
55#  US_DIR = shared data directory (for static content like devkeys)
56#  DF_DIR = utility defaults directory
57#  VB_DIR = vboot binary directory for dev-mode-only scripts
58#  DUT_TEST_DIR = vboot dut tests binary directory
59UB_DIR=${DESTDIR}/usr/bin
60UL_DIR=${DESTDIR}/usr/${LIBDIR}
61ULP_DIR=${UL_DIR}/pkgconfig
62UI_DIR=${DESTDIR}/usr/include/vboot
63US_DIR=${DESTDIR}/usr/share/vboot
64DF_DIR=${DESTDIR}/etc/default
65VB_DIR=${US_DIR}/bin
66DUT_TEST_DIR=${US_DIR}/tests
67
68# Where to install the (exportable) executables for testing?
69TEST_INSTALL_DIR = ${BUILD}/install_for_test
70
71# Set when installing into the SDK instead of building for a board sysroot.
72SDK_BUILD ?=
73
74# Verbose? Use V=1
75ifeq ($(filter-out 0,${V}),)
76Q := @
77endif
78
79# Quiet? Use QUIET=1
80ifeq ($(filter-out 0,${QUIET}),)
81PRINTF := printf
82else
83PRINTF := :
84endif
85
86# ARCH and/or FIRMWARE_ARCH are defined by the ChromiumOS ebuild.
87# Pick a valid target architecture if none is defined.
88ifeq (${ARCH},)
89  ARCH := $(shell uname -m)
90endif
91
92ifeq (${ARCH},armv7l)
93  override ARCH := arm
94else ifeq (${ARCH},aarch64)
95  override ARCH := arm64
96else ifeq (${ARCH},i386)
97  override ARCH := x86
98else ifeq (${ARCH},i686)
99  override ARCH := x86
100else ifeq (${ARCH},amd64)
101  override ARCH := x86_64
102endif
103
104ifneq (,$(filter arm arm64,${ARCH}))
105	ARCH_DIR := arm
106else ifneq (,$(filter x86 x86_64,${ARCH}))
107	ARCH_DIR := x86
108else
109	ARCH_DIR := stub
110endif
111
112# Provide default CC and CFLAGS for firmware builds; if you have any -D flags,
113# please add them after this point (e.g., -DVBOOT_DEBUG).
114DEBUG_FLAGS := $(if $(filter-out 0,${DEBUG}),-g -Og,-g -Os)
115WERROR := -Werror
116FIRMWARE_FLAGS := -nostdinc -ffreestanding -fno-builtin -fno-stack-protector
117COMMON_FLAGS := -pipe ${WERROR} -Wall -Wstrict-prototypes -Wtype-limits \
118	-Wundef -Wmissing-prototypes -Wno-trigraphs -Wredundant-decls -Wshadow \
119	-Wwrite-strings -Wstrict-aliasing -Wdate-time \
120	-Wint-conversion -ffunction-sections -fdata-sections \
121	-Wformat -Wno-format-security -std=gnu11 ${DEBUG_FLAGS} ${CPPFLAGS}
122
123# test_ccflag
124# $(1): compiler flags to test
125# $(2): code to insert into test snippet
126# returns: $(1) if compiler was successful, empty string otherwise
127test_ccflag = $(shell \
128	printf "$(2)\nvoid _start(void) {}\n" | \
129	$(CC) -nostdlib -Werror $(1) -xc -c - -o /dev/null \
130	>/dev/null 2>&1 && echo "$(1)")
131
132COMMON_FLAGS += $(call test_ccflag,-Wimplicit-fallthrough)
133COMMON_FLAGS += $(call test_ccflag,-Wno-address-of-packed-member)
134COMMON_FLAGS += $(call test_ccflag,-Wno-unknown-warning)
135COMMON_FLAGS += $(call test_ccflag,-Wincompatible-function-pointer-types)
136
137TEST_FLAGS := $(call test_ccflag,-Wno-address-of-packed-member)
138
139# FIRMWARE_ARCH is only defined by the ChromiumOS ebuild if compiling
140# for a firmware target (coreboot or depthcharge). It must map to the same
141# consistent set of architectures as the host.
142ifeq (${FIRMWARE_ARCH},i386)
143  override FIRMWARE_ARCH := x86
144else ifeq (${FIRMWARE_ARCH},amd64)
145  override FIRMWARE_ARCH := x86_64
146else ifneq ($(filter arm64 armv7 armv8 armv8_64,${FIRMWARE_ARCH}),)
147  override FIRMWARE_ARCH := arm
148endif
149
150ifeq (${FIRMWARE_ARCH},arm)
151CC ?= armv7a-cros-linux-gnueabihf-gcc
152CFLAGS ?= -march=armv5 -fno-common -ffixed-r8 -mfloat-abi=hard -marm
153	-mabi=aapcs-linux -mno-thumb-interwork ${FIRMWARE_FLAGS} ${COMMON_FLAGS}
154else ifeq (${FIRMWARE_ARCH},x86)
155CC ?= i686-pc-linux-gnu-gcc
156# Drop -march=i386 to permit use of SSE instructions
157CFLAGS ?= -fvisibility=hidden -fomit-frame-pointer \
158	-fno-toplevel-reorder -fno-dwarf2-cfi-asm -mpreferred-stack-boundary=2 \
159	${FIRMWARE_FLAGS} ${COMMON_FLAGS}
160else ifeq (${FIRMWARE_ARCH},x86_64)
161CFLAGS ?= ${FIRMWARE_FLAGS} ${COMMON_FLAGS} -fvisibility=hidden \
162	-fomit-frame-pointer
163else ifeq (${FIRMWARE_ARCH},mock)
164FIRMWARE_STUB := 1
165CFLAGS += ${TEST_FLAGS}
166else ifneq (${FIRMWARE_ARCH},)
167$(error Unexpected FIRMWARE_ARCH ${FIRMWARE_ARCH})
168else
169# FIRMWARE_ARCH not defined; assuming local compile.
170FIRMWARE_STUB := 1
171CC ?= gcc
172CFLAGS += -DCHROMEOS_ENVIRONMENT ${COMMON_FLAGS}
173endif
174
175# Needs -Wl because LD is actually set to CC by default.
176LDFLAGS += -Wl,--gc-sections
177
178ifneq ($(filter-out 0,${DEBUG})$(filter-out 0,${TEST_PRINT}),)
179CFLAGS += -DVBOOT_DEBUG
180endif
181
182ifneq ($(filter-out 0,${NDEBUG}),)
183CFLAGS += -DNDEBUG
184endif
185
186ifneq ($(filter-out 0,${TPM2_MODE}),)
187CFLAGS += -DTPM2_MODE
188endif
189
190# Support devices with GPT in SPI-NOR (for nand device)
191# TODO(b:184812319): Consider removing this code if nobody uses it.
192ifneq ($(filter-out 0,${GPT_SPI_NOR}),)
193CFLAGS += -DGPT_SPI_NOR
194endif
195
196# Enable EC early firmware selection.
197ifneq ($(filter-out 0,${EC_EFS}),)
198CFLAGS += -DEC_EFS=1
199else
200CFLAGS += -DEC_EFS=0
201endif
202
203# Some tests need to be disabled when using mocked_secdata_tpm.
204ifneq ($(filter-out 0,${MOCK_TPM}),)
205CFLAGS += -DMOCK_TPM
206endif
207
208# EXTERNAL_TPM_CLEAR_REQUEST indicates whether we want to use the external
209# tpm_clear_request tool or not.
210ifneq ($(filter-out 0,${EXTERNAL_TPM_CLEAR_REQUEST}),)
211CFLAGS += -DEXTERNAL_TPM_CLEAR_REQUEST=1
212else
213CFLAGS += -DEXTERNAL_TPM_CLEAR_REQUEST=0
214endif
215
216# Configurable temporary directory for host tools
217VBOOT_TMP_DIR := /tmp
218CFLAGS += -DVBOOT_TMP_DIR=\"${VBOOT_TMP_DIR}\"
219
220# Directory used by crossystem to create a lock file
221CROSSYSTEM_LOCK_DIR := /run/lock
222CFLAGS += -DCROSSYSTEM_LOCK_DIR=\"${CROSSYSTEM_LOCK_DIR}\"
223
224# NOTE: We don't use these files but they are useful for other packages to
225# query about required compiling/linking flags.
226PC_IN_FILES = vboot_host.pc.in
227
228# Create / use dependency files
229CFLAGS += -MMD -MF $@.d
230
231ifeq (${FIRMWARE_ARCH},)
232# Creates position independent code for non firmware target.
233CFLAGS += -fPIC
234endif
235
236CFLAGS += -D_GNU_SOURCE
237
238# This is required to access large disks and files on 32-bit systems,
239# but if the environment doesn't support it, at least compile support
240# for what is possible.
241# Pass through cflags_use_64bits to evaluate it only once, here.
242HASH_CONST := \#
243cflags_use_64bits := $(call test_ccflag,$\
244		     -D_FILE_OFFSET_BITS=64,$\
245		     ${HASH_CONST}include <fts.h>)
246CFLAGS += $(cflags_use_64bits)
247
248# Code coverage
249ifneq ($(filter-out 0,${COV}),)
250  COV_FLAGS = -Og --coverage -DCOVERAGE
251  CFLAGS += ${COV_FLAGS}
252  LDFLAGS += ${COV_FLAGS}
253  COV_INFO = ${BUILD}/coverage.info
254endif
255
256ifdef HAVE_MACOS
257  CFLAGS += -DHAVE_MACOS -Wno-deprecated-declarations
258endif
259
260# Musl doesn't have execinfo.h.
261ifndef HAVE_MUSL
262  CFLAGS += -DHAVE_EXECINFO_H
263endif
264
265# And a few more default utilities
266LD = ${CC}
267CXX ?= g++
268PKG_CONFIG ?= pkg-config
269
270# Static?
271ifneq ($(filter-out 0,${STATIC}),)
272LDFLAGS += -static
273PKG_CONFIG += --static
274endif
275
276ifneq (${FUZZ_FLAGS},)
277CFLAGS += ${FUZZ_FLAGS}
278endif
279
280# Optional Libraries
281LIBZIP_VERSION := $(shell ${PKG_CONFIG} --modversion libzip 2>/dev/null)
282HAVE_LIBZIP := $(if ${LIBZIP_VERSION},1)
283ifneq ($(filter-out 0,${HAVE_LIBZIP}),)
284  CFLAGS += -DHAVE_LIBZIP $(shell ${PKG_CONFIG} --cflags libzip)
285  LIBZIP_LIBS := $(shell ${PKG_CONFIG} --libs libzip)
286endif
287
288LIBARCHIVE_VERSION := $(shell ${PKG_CONFIG} --modversion libarchive 2>/dev/null)
289HAVE_LIBARCHIVE := $(if ${LIBARCHIVE_VERSION},1)
290ifneq ($(filter-out 0,${HAVE_LIBARCHIVE}),)
291  CFLAGS += -DHAVE_LIBARCHIVE $(shell ${PKG_CONFIG} --cflags libarchive)
292  LIBARCHIVE_LIBS := $(shell ${PKG_CONFIG} --libs libarchive)
293endif
294
295HAVE_CROSID := $(shell ${PKG_CONFIG} --exists crosid && echo 1)
296ifeq ($(HAVE_CROSID),1)
297  CFLAGS += -DHAVE_CROSID $(shell ${PKG_CONFIG} --cflags crosid)
298  CROSID_LIBS := $(shell ${PKG_CONFIG} --libs crosid)
299endif
300
301HAVE_NSS := $(shell ${PKG_CONFIG} --exists nss && echo 1)
302ifeq ($(HAVE_NSS),1)
303  CFLAGS += -DHAVE_NSS $(shell ${PKG_CONFIG} --cflags nss)
304  # The LIBS is not needed because we only use the header.
305else
306  $(warning Missing NSS. PKCS11 signing not supported. Install libnss3 to enable this feature.)
307endif
308
309# Get major version of openssl (e.g. version 3.0.5 -> "3")
310OPENSSL_VERSION := $(shell ${PKG_CONFIG} --modversion openssl | cut -d. -f1)
311
312# A test wrapper can be specified. Tests are run inside the wrapper eg:
313# make RUNTEST=env runtests
314RUNTEST =
315# The Path to the $BUILD inside the runtest wrapper, used by the test scripts.
316# The top of the chroot for RUNTEST must be passed in via the SYSROOT
317# environment variable.  In the ChromiumOS chroot, this is done automatically by
318# the ebuild.
319export BUILD_RUN = $(subst ${SYSROOT},,${BUILD})
320# Path to the $SRCDIR inside the wrapper, the test scripts rederive this.
321SRC_RUN = $(subst ${SYSROOT},,${SRCDIR})
322
323##############################################################################
324# The default target is here, to allow dependencies to be expressed below
325# without accidentally changing the default target.
326
327# Default target.
328.PHONY: all
329all: fwlib futil utillib hostlib cgpt tlcl util_files \
330	$(if $(filter x86_64,${ARCH}),$(if $(filter clang,${CC}),fuzzers)) \
331	$(if $(filter-out 0,${COV}),coverage)
332
333##############################################################################
334# Now we need to describe everything we might want or need to build
335
336# Everything wants these headers.
337INCLUDES += \
338	-Ifirmware/include \
339	-Ifirmware/lib/include \
340	-Ifirmware/lib/cgptlib/include \
341	-Ifirmware/lib/tpm_lite/include \
342	-Ifirmware/2lib/include
343
344# If we're not building for a specific target, just stub out things like the
345# TPM commands and various external functions that are provided by the BIOS.
346ifneq (${FIRMWARE_STUB},)
347INCLUDES += -Ihost/include -Ihost/lib/include
348INCLUDES += -Ihost/lib21/include
349ifeq ($(shell uname -s), OpenBSD)
350INCLUDES += -I/usr/local/include
351endif
352endif
353
354# Firmware library, used by the other firmware components (depthcharge,
355# coreboot, etc.). It doesn't need exporting to some other place; they'll build
356# this source tree locally and link to it directly.
357FWLIB = ${BUILD}/vboot_fw.a
358
359# Separate TPM lightweight command library (TLCL)
360TLCL = ${BUILD}/tlcl.a
361
362FWLIB_SRCS = \
363	firmware/2lib/2api.c \
364	firmware/2lib/2auxfw_sync.c \
365	firmware/2lib/2common.c \
366	firmware/2lib/2context.c \
367	firmware/2lib/2crc8.c \
368	firmware/2lib/2crypto.c \
369	firmware/2lib/2ec_sync.c \
370	firmware/2lib/2firmware.c \
371	firmware/2lib/2gbb.c \
372	firmware/2lib/2hmac.c \
373	firmware/2lib/2kernel.c \
374	firmware/2lib/2load_kernel.c \
375	firmware/2lib/2misc.c \
376	firmware/2lib/2nvstorage.c \
377	firmware/2lib/2packed_key.c \
378	firmware/2lib/2recovery_reasons.c \
379	firmware/2lib/2rsa.c \
380	firmware/2lib/2secdata_firmware.c \
381	firmware/2lib/2secdata_fwmp.c \
382	firmware/2lib/2secdata_kernel.c \
383	firmware/2lib/2sha1.c \
384	firmware/2lib/2sha256.c \
385	firmware/2lib/2sha512.c \
386	firmware/2lib/2sha_utility.c \
387	firmware/2lib/2struct.c \
388	firmware/2lib/2stub_hwcrypto.c \
389	firmware/2lib/2tpm_bootmode.c \
390	firmware/lib/cgptlib/cgptlib.c \
391	firmware/lib/cgptlib/cgptlib_internal.c \
392	firmware/lib/cgptlib/crc32.c \
393	firmware/lib/gpt_misc.c \
394	firmware/lib20/api_kernel.c \
395	firmware/lib20/kernel.c
396
397# TPM lightweight command library
398ifeq ($(filter-out 0,${TPM2_MODE}),)
399TLCL_SRCS = \
400	firmware/lib/tpm_lite/tlcl.c
401else
402# TODO(apronin): tests for TPM2 case?
403TLCL_SRCS = \
404	firmware/lib/tpm2_lite/tlcl.c \
405	firmware/lib/tpm2_lite/marshaling.c
406endif
407
408# Support real TPM unless MOCK_TPM is set
409ifneq ($(filter-out 0,${MOCK_TPM}),)
410FWLIB_SRCS += \
411	firmware/lib/tpm_lite/mocked_tlcl.c
412endif
413
414ifneq ($(filter-out 0,${X86_SHA_EXT}),)
415CFLAGS += -DX86_SHA_EXT
416FWLIB_SRCS += \
417	firmware/2lib/2hwcrypto.c \
418	firmware/2lib/2sha256_x86.c
419endif
420
421ifneq ($(filter-out 0,${ARMV8_CRYPTO_EXT}),)
422CFLAGS += -DARMV8_CRYPTO_EXT
423FWLIB_SRCS += \
424	firmware/2lib/2hwcrypto.c \
425	firmware/2lib/2sha256_arm.c
426FWLIB_ASMS += \
427	firmware/2lib/sha256_armv8a_ce_a64.S
428endif
429
430ifneq ($(filter-out 0,${ARM64_RSA_ACCELERATION}),)
431CFLAGS += -DARM64_RSA_ACCELERATION
432FWLIB_SRCS += \
433	firmware/2lib/2modpow_neon.c
434endif
435
436ifneq ($(filter-out 0,${VB2_X86_RSA_ACCELERATION}),)
437CFLAGS += -DVB2_X86_RSA_ACCELERATION
438FWLIB_SRCS += \
439	firmware/2lib/2modpow_sse2.c
440endif
441
442ifneq (,$(filter arm64 x86 x86_64,${ARCH}))
443ENABLE_HWCRYPTO_RSA_TESTS := 1
444endif
445
446# Even if X86_SHA_EXT is 0 we need cflags since this will be compiled for tests
447${BUILD}/firmware/2lib/2sha256_x86.o: CFLAGS += -mssse3 -mno-avx -msha
448
449${BUILD}/firmware/2lib/2modpow_sse2.o: CFLAGS += -msse2 -mno-avx
450
451ifneq (${FIRMWARE_STUB},)
452# Include BIOS stubs in the firmware library when compiling for host
453# TODO: split out other stub funcs too
454FWLIB_SRCS += \
455	firmware/stub/tpm_lite_stub.c \
456	firmware/stub/vboot_api_stub_disk.c \
457	firmware/stub/vboot_api_stub_stream.c \
458	firmware/2lib/2stub.c
459endif
460
461FWLIB_OBJS = ${FWLIB_SRCS:%.c=${BUILD}/%.o} ${FWLIB_ASMS:%.S=${BUILD}/%.o}
462TLCL_OBJS = ${TLCL_SRCS:%.c=${BUILD}/%.o}
463ALL_OBJS += ${FWLIB_OBJS} ${TLCL_OBJS}
464
465# Maintain behaviour of default on.
466USE_FLASHROM ?= 1
467
468ifneq ($(filter-out 0,${USE_FLASHROM}),)
469$(info building with libflashrom support)
470FLASHROM_LIBS := $(shell ${PKG_CONFIG} --libs flashrom)
471COMMONLIB_SRCS += \
472	host/lib/flashrom.c \
473	host/lib/flashrom_drv.c
474CFLAGS += -DUSE_FLASHROM
475endif
476COMMONLIB_SRCS += \
477	host/lib/subprocess.c \
478	host/lib/cbfstool.c
479
480# Intermediate library for the vboot_reference utilities to link against.
481UTILLIB = ${BUILD}/libvboot_util.a
482
483# Avoid build failures outside the chroot on Ubuntu 2022.04
484# e.g.:
485# host/lib/host_key2.c:103:17: error: ‘RSA_free’ is deprecated: Since OpenSSL 3.0
486# [-Werror=deprecated-declarations]
487ifeq ($(OPENSSL_VERSION),3)
488${UTILLIB}: CFLAGS += -Wno-error=deprecated-declarations
489endif
490
491UTILLIB_SRCS = \
492	cgpt/cgpt_add.c \
493	cgpt/cgpt_boot.c \
494	cgpt/cgpt_common.c \
495	cgpt/cgpt_create.c \
496	cgpt/cgpt_edit.c \
497	cgpt/cgpt_prioritize.c \
498	cgpt/cgpt_repair.c \
499	cgpt/cgpt_show.c \
500	futility/dump_kernel_config_lib.c \
501	host/arch/${ARCH_DIR}/lib/crossystem_arch.c \
502	host/lib/chromeos_config.c \
503	host/lib/crossystem.c \
504	host/lib/crypto.c \
505	host/lib/file_keys.c \
506	$(COMMONLIB_SRCS) \
507	host/lib/fmap.c \
508	host/lib/host_common.c \
509	host/lib/host_key2.c \
510	host/lib/host_keyblock.c \
511	host/lib/host_misc.c \
512	host/lib/host_signature.c \
513	host/lib/host_signature2.c \
514	host/lib/signature_digest.c \
515	host/lib/util_misc.c \
516	host/lib21/host_common.c \
517	host/lib21/host_key.c \
518	host/lib21/host_misc.c \
519	host/lib21/host_signature.c
520
521ifeq ($(HAVE_NSS),1)
522UTILLIB_SRCS += \
523	host/lib/host_p11.c
524else
525UTILLIB_SRCS += \
526	host/lib/host_p11_stub.c
527endif
528
529UTILLIB_OBJS = ${UTILLIB_SRCS:%.c=${BUILD}/%.o}
530ALL_OBJS += ${UTILLIB_OBJS}
531
532# Externally exported library for some target userspace apps to link with
533# (cryptohome, updater, etc.)
534HOSTLIB = ${BUILD}/libvboot_host.so
535HOSTLIB_STATIC = ${BUILD}/libvboot_host.a
536
537# For testing purposes files contianing some libvboot_host symbols.
538HOSTLIB_DEF = ${BUILD}/tests/libvboot_host_def.txt
539HOSTLIB_UNDEF = ${BUILD}/tests/libvboot_host_undef.txt
540
541HOSTLIB_SRCS = \
542	cgpt/cgpt_add.c \
543	cgpt/cgpt_boot.c \
544	cgpt/cgpt_common.c \
545	cgpt/cgpt_create.c \
546	cgpt/cgpt_edit.c \
547	cgpt/cgpt_find.c \
548	cgpt/cgpt_prioritize.c \
549	cgpt/cgpt_repair.c \
550	cgpt/cgpt_show.c \
551	firmware/2lib/2common.c \
552	firmware/2lib/2context.c \
553	firmware/2lib/2crc8.c \
554	firmware/2lib/2crypto.c \
555	firmware/2lib/2hmac.c \
556	firmware/2lib/2nvstorage.c \
557	firmware/2lib/2recovery_reasons.c \
558	firmware/2lib/2rsa.c \
559	firmware/2lib/2sha1.c \
560	firmware/2lib/2sha256.c \
561	firmware/2lib/2sha512.c \
562	firmware/2lib/2sha_utility.c \
563	firmware/2lib/2struct.c \
564	firmware/2lib/2stub.c \
565	firmware/2lib/2stub_hwcrypto.c \
566	firmware/lib/cgptlib/cgptlib_internal.c \
567	firmware/lib/cgptlib/crc32.c \
568	firmware/lib/gpt_misc.c \
569	firmware/stub/tpm_lite_stub.c \
570	firmware/stub/vboot_api_stub_disk.c \
571	futility/dump_kernel_config_lib.c \
572	host/arch/${ARCH_DIR}/lib/crossystem_arch.c \
573	host/lib/chromeos_config.c \
574	host/lib/crossystem.c \
575	host/lib/crypto.c \
576	host/lib/extract_vmlinuz.c \
577	$(COMMONLIB_SRCS) \
578	host/lib/fmap.c \
579	host/lib/host_misc.c \
580	host/lib21/host_misc.c \
581	${TLCL_SRCS}
582
583ifneq ($(filter-out 0,${GPT_SPI_NOR}),)
584HOSTLIB_SRCS += cgpt/cgpt_nor.c
585endif
586
587HOSTLIB_OBJS = ${HOSTLIB_SRCS:%.c=${BUILD}/%.o}
588ALL_OBJS += ${HOSTLIB_OBJS}
589
590# ----------------------------------------------------------------------------
591# Now for the userspace binaries
592
593CGPT = ${BUILD}/cgpt/cgpt
594
595CGPT_SRCS = \
596	cgpt/cgpt.c \
597	cgpt/cgpt_add.c \
598	cgpt/cgpt_boot.c \
599	cgpt/cgpt_common.c \
600	cgpt/cgpt_create.c \
601	cgpt/cgpt_edit.c \
602	cgpt/cgpt_find.c \
603	cgpt/cgpt_legacy.c \
604	cgpt/cgpt_prioritize.c \
605	cgpt/cgpt_repair.c \
606	cgpt/cgpt_show.c \
607	cgpt/cmd_add.c \
608	cgpt/cmd_boot.c \
609	cgpt/cmd_create.c \
610	cgpt/cmd_edit.c \
611	cgpt/cmd_find.c \
612	cgpt/cmd_legacy.c \
613	cgpt/cmd_prioritize.c \
614	cgpt/cmd_repair.c \
615	cgpt/cmd_show.c
616
617ifneq ($(filter-out 0,${GPT_SPI_NOR}),)
618CGPT_SRCS += cgpt/cgpt_nor.c
619endif
620
621CGPT_OBJS = ${CGPT_SRCS:%.c=${BUILD}/%.o}
622
623ALL_OBJS += ${CGPT_OBJS}
624
625CGPT_WRAPPER = ${BUILD}/cgpt/cgpt_wrapper
626
627CGPT_WRAPPER_SRCS = \
628	cgpt/cgpt_nor.c \
629	cgpt/cgpt_wrapper.c
630
631CGPT_WRAPPER_OBJS = ${CGPT_WRAPPER_SRCS:%.c=${BUILD}/%.o}
632
633ALL_OBJS += ${CGPT_WRAPPER_OBJS}
634
635# Utility defaults
636UTIL_DEFAULTS = ${BUILD}/default/vboot_reference
637
638# Scripts to install directly (not compiled)
639UTIL_SCRIPT_NAMES_SDK = \
640	utility/dev_make_keypair \
641	utility/vbutil_what_keys
642UTIL_SCRIPT_NAMES_BOARD = \
643	utility/chromeos-tpm-recovery \
644	utility/dev_debug_vboot \
645	utility/enable_dev_usb_boot \
646	utility/tpm-nvsize
647
648UTIL_BIN_NAMES_SDK = \
649	utility/dumpRSAPublicKey \
650	utility/load_kernel_test \
651	utility/pad_digest_utility \
652	utility/signature_digest_utility \
653	utility/verify_data
654UTIL_BIN_NAMES_BOARD = \
655	utility/dumpRSAPublicKey \
656	utility/tpmc
657
658ifneq ($(filter-out 0,${USE_FLASHROM}),)
659UTIL_BIN_NAMES_BOARD += utility/crossystem
660endif
661
662UTIL_SCRIPTS_SDK = $(addprefix ${BUILD}/,${UTIL_SCRIPT_NAMES_SDK})
663UTIL_SCRIPTS_BOARD = $(addprefix ${BUILD}/,${UTIL_SCRIPT_NAMES_BOARD})
664UTIL_BINS_SDK = $(addprefix ${BUILD}/,${UTIL_BIN_NAMES_SDK})
665UTIL_BINS_BOARD = $(addprefix ${BUILD}/,${UTIL_BIN_NAMES_BOARD})
666UTIL_FILES_SDK = ${UTIL_BINS_SDK} ${UTIL_SCRIPTS_SDK}
667UTIL_FILES_BOARD = ${UTIL_BINS_BOARD} ${UTIL_SCRIPTS_BOARD}
668ALL_OBJS += $(addsuffix .o,${UTIL_BINS_SDK})
669ALL_OBJS += $(addsuffix .o,${UTIL_BINS_BOARD})
670
671
672# Signing scripts that are also useful on DUTs.
673SIGNING_SCRIPTS_BOARD = \
674	scripts/image_signing/make_dev_firmware.sh \
675	scripts/image_signing/make_dev_ssd.sh \
676	scripts/image_signing/resign_firmwarefd.sh \
677	scripts/image_signing/common_minimal.sh
678
679# SDK installations have some extra scripts.
680SIGNING_SCRIPTS_SDK = \
681	scripts/image_signing/make_dev_firmware.sh \
682	scripts/image_signing/make_dev_ssd.sh \
683	scripts/image_signing/resign_firmwarefd.sh \
684	scripts/image_signing/swap_ec_rw \
685	scripts/image_signing/common_minimal.sh
686
687# Unified firmware utility.
688FUTIL_BIN = ${BUILD}/futility/futility
689
690# These are the executables that are now built in to futility. We'll create
691# symlinks for these so the old names will still work.
692FUTIL_SYMLINKS = \
693	dump_fmap \
694	dump_kernel_config \
695	gbb_utility \
696	vbutil_firmware \
697	vbutil_kernel \
698	vbutil_key \
699	vbutil_keyblock
700
701FUTIL_SRCS = \
702	futility/futility.c \
703	futility/cmd_create.c \
704	futility/cmd_dump_fmap.c \
705	futility/cmd_dump_kernel_config.c \
706	futility/cmd_flash_util.c \
707	futility/cmd_gbb_utility.c \
708	futility/cmd_gscvd.c \
709	futility/cmd_load_fmap.c \
710	futility/cmd_pcr.c \
711	futility/cmd_read.c \
712	futility/cmd_show.c \
713	futility/cmd_sign.c \
714	futility/cmd_update.c \
715	futility/cmd_vbutil_firmware.c \
716	futility/cmd_vbutil_kernel.c \
717	futility/cmd_vbutil_key.c \
718	futility/cmd_vbutil_keyblock.c \
719	futility/file_type_bios.c \
720	futility/file_type.c \
721	futility/file_type_rwsig.c \
722	futility/file_type_usbpd1.c \
723	futility/flash_helpers.c \
724	futility/platform_csme.c \
725	futility/misc.c \
726	futility/vb1_helper.c \
727	futility/vb2_helper.c
728
729ifneq ($(filter-out 0,${USE_FLASHROM}),)
730FUTIL_SRCS += host/lib/flashrom_drv.c \
731	futility/updater_archive.c \
732	futility/updater_dut.c \
733	futility/updater_manifest.c \
734	futility/updater_quirks.c \
735	futility/updater_utils.c \
736	futility/updater.c
737endif
738
739# List of commands built in futility.
740FUTIL_CMD_LIST = ${BUILD}/gen/futility_cmds.c
741
742FUTIL_OBJS = ${FUTIL_SRCS:%.c=${BUILD}/%.o} ${FUTIL_CMD_LIST:%.c=%.o}
743
744${FUTIL_OBJS}: INCLUDES += -Ihost/lib21/include
745
746# Avoid build failures outside the chroot on Ubuntu 2022.04
747# e.g.:
748# futility/cmd_create.c:161:9: warning: ‘RSA_free’ is deprecated: Since OpenSSL 3.0
749# [-Wdeprecated-declarations]
750ifeq ($(OPENSSL_VERSION),3)
751${FUTIL_OBJS}: CFLAGS += -Wno-error=deprecated-declarations
752endif
753
754ALL_OBJS += ${FUTIL_OBJS}
755
756
757# Library of handy test functions.
758TESTLIB = ${BUILD}/tests/test.a
759
760TEST_COMMON_DIR = tests/common
761
762TESTLIB_SRCS += $(wildcard $(TEST_COMMON_DIR)/*.c)
763TESTLIB_SRCS += tests/crc32_test.c
764
765TESTLIB_OBJS = ${TESTLIB_SRCS:%.c=${BUILD}/%.o}
766TEST_OBJS += ${TESTLIB_OBJS}
767
768
769# And some compiled tests.
770TEST_NAMES = \
771	tests/cbfstool_tests \
772	tests/cgptlib_test \
773	tests/chromeos_config_tests \
774	tests/gpt_misc_tests \
775	tests/sha_benchmark \
776	tests/subprocess_tests \
777	tests/verify_kernel
778
779ifeq ($(filter-out 0,${MOCK_TPM})$(filter-out 0,${TPM2_MODE}),)
780# tlcl_tests only works when MOCK_TPM is disabled
781# TODO(apronin): tests for TPM2 case?
782TEST_NAMES += \
783	tests/tlcl_tests
784endif
785
786TEST_FUTIL_NAMES = \
787	tests/futility/binary_editor \
788	tests/futility/test_file_types \
789	tests/futility/test_not_really
790
791TEST_NAMES += ${TEST_FUTIL_NAMES}
792
793TEST2X_NAMES = \
794	tests/vb2_api_tests \
795	tests/vb2_auxfw_sync_tests \
796	tests/vb2_common_tests \
797	tests/vb2_common2_tests \
798	tests/vb2_common3_tests \
799	tests/vb2_crypto_tests \
800	tests/vb2_ec_sync_tests \
801	tests/vb2_firmware_tests \
802	tests/vb2_gbb_init_tests \
803	tests/vb2_gbb_tests \
804	tests/vb2_host_flashrom_tests \
805	tests/vb2_host_key_tests \
806	tests/vb2_host_nvdata_flashrom_tests \
807	tests/vb2_inject_kernel_subkey_tests \
808	tests/vb2_kernel_tests \
809	tests/vb2_load_kernel_tests \
810	tests/vb2_load_kernel2_tests \
811	tests/vb2_misc_tests \
812	tests/vb2_misc2_tests \
813	tests/vb2_nvstorage_tests \
814	tests/vb2_rsa_utility_tests \
815	tests/vb2_recovery_reasons_tests \
816	tests/vb2_secdata_firmware_tests \
817	tests/vb2_secdata_fwmp_tests \
818	tests/vb2_secdata_kernel_tests \
819	tests/vb2_sha_api_tests \
820	tests/vb2_sha_tests \
821	tests/hmac_test
822
823TEST20_NAMES = \
824	tests/vb20_api_kernel_tests \
825	tests/vb20_kernel_tests \
826	tests/vb20_rsa_padding_tests \
827	tests/vb20_verify_fw
828
829TEST21_NAMES = \
830	tests/vb21_host_common2_tests \
831	tests/vb21_host_common_tests \
832	tests/vb21_host_key_tests \
833	tests/vb21_host_misc_tests \
834	tests/vb21_host_sig_tests
835
836TEST_NAMES += ${TEST2X_NAMES} ${TEST20_NAMES} ${TEST21_NAMES}
837
838# Tests which should be run on dut
839ifeq (${ARCH}, x86_64)
840DUT_TEST_NAMES += tests/vb2_sha256_x86_tests
841endif
842
843HWCRYPTO_RSA_TESTS = \
844	tests/vb20_hwcrypto_rsa_padding_tests \
845	tests/vb20_hwcrypto_verify_fw
846
847TEST_NAMES += ${DUT_TEST_NAMES}
848
849ifeq (${ENABLE_HWCRYPTO_RSA_TESTS},1)
850TEST20_NAMES += ${HWCRYPTO_RSA_TESTS}
851endif
852
853# And a few more...
854ifeq ($(filter-out 0,${TPM2_MODE}),)
855TLCL_TEST_NAMES = \
856	tests/tpm_lite/tpmtest_earlyextend \
857	tests/tpm_lite/tpmtest_earlynvram \
858	tests/tpm_lite/tpmtest_earlynvram2 \
859	tests/tpm_lite/tpmtest_enable \
860	tests/tpm_lite/tpmtest_fastenable \
861	tests/tpm_lite/tpmtest_globallock \
862	tests/tpm_lite/tpmtest_redefine_unowned \
863	tests/tpm_lite/tpmtest_spaceperm \
864	tests/tpm_lite/tpmtest_testsetup \
865	tests/tpm_lite/tpmtest_timing \
866	tests/tpm_lite/tpmtest_writelimit
867else
868# TODO(apronin): tests for TPM2 case?
869TLCL_TEST_NAMES =
870endif
871
872TEST_NAMES += ${TLCL_TEST_NAMES}
873
874# Finally
875TEST_BINS = $(addprefix ${BUILD}/,${TEST_NAMES})
876TEST_OBJS += $(addsuffix .o,${TEST_BINS})
877
878TEST_FUTIL_BINS = $(addprefix ${BUILD}/,${TEST_FUTIL_NAMES})
879TEST2X_BINS = $(addprefix ${BUILD}/,${TEST2X_NAMES})
880TEST20_BINS = $(addprefix ${BUILD}/,${TEST20_NAMES})
881TEST21_BINS = $(addprefix ${BUILD}/,${TEST21_NAMES})
882
883# Directory containing test keys
884TEST_KEYS = ${SRC_RUN}/tests/testkeys
885
886# ----------------------------------------------------------------------------
887# Fuzzing binaries
888
889FUZZ_TEST_NAMES = \
890	tests/cgpt_fuzzer \
891	tests/vb2_keyblock_fuzzer \
892	tests/vb2_preamble_fuzzer
893
894FUZZ_TEST_BINS = $(addprefix ${BUILD}/,${FUZZ_TEST_NAMES})
895
896##############################################################################
897# Finally, some targets. High-level ones first.
898
899# Create output directories if necessary.  Do this via explicit shell commands
900# so it happens before trying to generate/include dependencies.
901SUBDIRS := firmware host cgpt utility futility tests tests/tpm_lite
902_dir_create := $(foreach d, \
903	$(shell find ${SUBDIRS} -name '*.c' -exec  dirname {} \; | sort -u), \
904	$(shell [ -d ${BUILD}/${d} ] || mkdir -p ${BUILD}/${d}))
905
906.PHONY: clean
907clean:
908	${Q}/bin/rm -rf ${BUILD}
909
910.PHONY: install
911install: cgpt_install signing_install futil_install pc_files_install \
912	lib_install $(if ${SDK_BUILD},,util_install_defaults) \
913	$(foreach f,$(if ${SDK_BUILD},${UTIL_FILES_SDK},${UTIL_FILES_BOARD}), \
914		util_install-$(patsubst ${BUILD}/%,%,${f}))
915
916.PHONY: install_dev
917install_dev: devkeys_install headers_install
918
919.PHONY: install_mtd
920install_mtd: install cgpt_wrapper_install
921
922.PHONY: install_for_test
923install_for_test: override DESTDIR = ${TEST_INSTALL_DIR}
924install_for_test: test_setup install \
925	$(foreach f,${UTIL_FILES_SDK} ${UTIL_FILES_BOARD}, \
926		util_install-$(patsubst ${BUILD}/%,%,${f}))
927
928# Don't delete intermediate object files
929.SECONDARY:
930
931# ----------------------------------------------------------------------------
932# Firmware library
933
934# TPM-specific flags.  These depend on the particular TPM we're targeting for.
935# They are needed here only for compiling parts of the firmware code into
936# user-level tests.
937
938# TPM_BLOCKING_CONTINUESELFTEST is defined if TPM_ContinueSelfTest blocks until
939# the self test has completed.
940
941${TLCL_OBJS}: CFLAGS += -DTPM_BLOCKING_CONTINUESELFTEST
942
943# TPM_MANUAL_SELFTEST is defined if the self test must be started manually
944# (with a call to TPM_ContinueSelfTest) instead of starting automatically at
945# power on.
946#
947# We sincerely hope that TPM_BLOCKING_CONTINUESELFTEST and TPM_MANUAL_SELFTEST
948# are not both defined at the same time.  (See comment in code.)
949
950# CFLAGS += -DTPM_MANUAL_SELFTEST
951
952# NOTE: UNROLL_LOOPS *only* affects SHA256, *not* SHA512. This seems to have
953# been a conscious decision at some point (see b/35501356) but whether it still
954# holds up in all situations on all architectures today might need to be
955# reevaluated. For now, since we currently always use SHA256 for (non-recovery)
956# kernel bodies and don't unroll loops for firmware verification, it's not very
957# relevant in practice. To unroll SHA512, UNROLL_LOOPS_SHA512 would need to be
958# defined.
959ifneq ($(filter-out 0,$(UNROLL_LOOPS)),)
960$(info vboot SHA256 built with unrolled loops (faster, larger code size))
961CFLAGS += -DUNROLL_LOOPS
962else
963$(info vboot SHA256 built with tight loops (slower, smaller code size))
964endif
965
966.PHONY: fwlib
967fwlib: $(if ${FIRMWARE_ARCH},${FWLIB},)
968
969${FWLIB}: ${FWLIB_OBJS}
970	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
971	${Q}rm -f $@
972	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
973	${Q}ar qcT $@ $^
974
975.PHONY: tlcl
976tlcl: ${TLCL}
977
978${TLCL}: ${TLCL_OBJS}
979	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
980	${Q}rm -f $@
981	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
982	${Q}ar qcT $@ $^
983
984# ----------------------------------------------------------------------------
985# Host library(s)
986
987# Some UTILLIB files need dlopen(), doesn't hurt to just link it everywhere.
988LDLIBS += -ldl
989ifneq ($(filter-out 0,${USE_FLASHROM}),)
990${HOSTLIB}: LDLIBS += ${FLASHROM_LIBS}
991endif
992
993.PHONY: utillib
994utillib: ${UTILLIB}
995
996# TODO: better way to make .a than duplicating this recipe each time?
997${UTILLIB}: ${UTILLIB_OBJS} ${FWLIB_OBJS} ${TLCL_OBJS}
998	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
999	${Q}rm -f $@
1000	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
1001	${Q}ar qcT $@ $^
1002
1003.PHONY: hostlib
1004hostlib: ${HOSTLIB} ${HOSTLIB_STATIC}
1005
1006# TODO: better way to make .a than duplicating this recipe each time?
1007${HOSTLIB_STATIC}: ${HOSTLIB_OBJS}
1008	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
1009	${Q}rm -f $@
1010	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
1011	${Q}ar qcT $@ $^
1012
1013${HOSTLIB}: ${HOSTLIB_OBJS}
1014	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
1015	${Q}rm -f $@
1016	@${PRINTF} "    LD            $(subst ${BUILD}/,,$@)\n"
1017	${Q}${LD} ${LDFLAGS} ${LDLIBS} -shared -Wl,-soname,$(subst ${BUILD}/,,$@) $^ -o $@
1018
1019${HOSTLIB_DEF}: ${HOSTLIB_STATIC}
1020	@${PRINTF} "    NMd           $(subst ${BUILD}/,,$@)\n"
1021	${Q}nm --defined-only --format=just-symbols $^ > $@
1022
1023${HOSTLIB_UNDEF}: ${HOSTLIB_STATIC}
1024	@${PRINTF} "    NMu           $(subst ${BUILD}/,,$@)\n"
1025	${Q}nm --undefined-only --format=just-symbols $^ > $@
1026
1027
1028.PHONY: headers_install
1029headers_install:
1030	@${PRINTF} "    INSTALL       HEADERS\n"
1031	${Q}mkdir -p ${UI_DIR}
1032	${Q}${INSTALL} -t ${UI_DIR} -m644 \
1033		host/include/* \
1034		firmware/2lib/include/2crypto.h \
1035		firmware/2lib/include/2recovery_reasons.h \
1036		firmware/2lib/include/2sysincludes.h \
1037		firmware/include/gpt.h \
1038		firmware/include/tlcl.h \
1039		firmware/include/tss_constants.h \
1040		firmware/include/tpm1_tss_constants.h \
1041		firmware/include/tpm2_tss_constants.h
1042
1043.PHONY: lib_install
1044lib_install: ${HOSTLIB} ${HOSTLIB_STATIC}
1045	@${PRINTF} "    INSTALL       HOSTLIB\n"
1046	${Q}mkdir -p ${UL_DIR}
1047	${Q}${INSTALL} -t ${UL_DIR} -m644 $^
1048
1049.PHONY: devkeys_install
1050devkeys_install:
1051	@${PRINTF} "    INSTALL       DEVKEYS\n"
1052	${Q}mkdir -p ${US_DIR}/devkeys
1053	${Q}${INSTALL} -t ${US_DIR}/devkeys -m644 \
1054		`find tests/devkeys -type f -maxdepth 1`
1055
1056# ----------------------------------------------------------------------------
1057# CGPT library and utility
1058
1059.PHONY: cgpt_wrapper
1060cgpt_wrapper: ${CGPT_WRAPPER}
1061
1062${CGPT_WRAPPER}: ${CGPT_WRAPPER_OBJS} ${UTILLIB}
1063	@$(PRINTF) "    LD            $(subst ${BUILD}/,,$@)\n"
1064	${Q}${LD} -o ${CGPT_WRAPPER} ${LDFLAGS} $^ ${LDLIBS}
1065
1066.PHONY: cgpt
1067cgpt: ${CGPT} $(if $(filter-out 0,${GPT_SPI_NOR}),cgpt_wrapper)
1068
1069# on FreeBSD: install misc/e2fsprogs-libuuid from ports,
1070# or e2fsprogs-libuuid from its binary package system.
1071# on OpenBSD: install sysutils/e2fsprogs from ports,
1072# or e2fsprogs from its binary package system, to install uuid/uid.h
1073${CGPT}: LDLIBS += -luuid
1074
1075${CGPT}: ${CGPT_OBJS} ${UTILLIB}
1076	@${PRINTF} "    LDcgpt        $(subst ${BUILD}/,,$@)\n"
1077	${Q}${LD} -o ${CGPT} ${LDFLAGS} $^ ${LDLIBS}
1078
1079.PHONY: cgpt_install
1080cgpt_install: ${CGPT}
1081	@${PRINTF} "    INSTALL       CGPT\n"
1082	${Q}mkdir -p ${UB_DIR}
1083	${Q}${INSTALL} -t ${UB_DIR} $^
1084
1085.PHONY: cgpt_wrapper_install
1086cgpt_wrapper_install: cgpt_install ${CGPT_WRAPPER}
1087	@$(PRINTF) "    INSTALL       cgpt_wrapper\n"
1088	${Q}${INSTALL} -t ${UB_DIR} ${CGPT_WRAPPER}
1089	${Q}mv ${UB_DIR}/$(notdir ${CGPT}) \
1090		${UB_DIR}/$(notdir ${CGPT}).bin
1091	${Q}mv ${UB_DIR}/$(notdir ${CGPT_WRAPPER}) \
1092		${UB_DIR}/$(notdir ${CGPT})
1093
1094# ----------------------------------------------------------------------------
1095# Utilities
1096
1097.PHONY: util_files
1098util_files: $(if ${SDK_BUILD},${UTIL_FILES_SDK},${UTIL_FILES_BOARD})
1099
1100# These have their own headers too.
1101${BUILD}/utility/%: INCLUDES += -Iutility/include
1102
1103# Avoid build failures outside the chroot on Ubuntu 2022.04
1104ifeq ($(OPENSSL_VERSION),3)
1105${BUILD}/utility/%: CFLAGS += -Wno-error=deprecated-declarations
1106endif
1107
1108${UTIL_BINS_SDK}: ${UTILLIB}
1109${UTIL_BINS_SDK}: LIBS = ${UTILLIB}
1110${UTIL_BINS_BOARD}: ${UTILLIB}
1111${UTIL_BINS_BOARD}: LIBS = ${UTILLIB}
1112
1113${UTIL_SCRIPTS_SDK} ${UTIL_SCRIPTS_BOARD}: ${BUILD}/%: %
1114	${Q}cp -f $< $@
1115	${Q}chmod a+rx $@
1116
1117define UTIL_INSTALL_template
1118.PHONY: util_install-$(1)
1119util_install-$(1): $$(addprefix $${BUILD}/,$(1))
1120	@${PRINTF} "    INSTALL       $(1)\n"
1121	${Q}mkdir -p $${UB_DIR}
1122	${Q}${INSTALL} -t $${UB_DIR} $$<
1123endef
1124
1125$(foreach f, $(sort ${UTIL_FILES_SDK} ${UTIL_FILES_BOARD}), \
1126	$(eval $(call UTIL_INSTALL_template,$(patsubst ${BUILD}/%,%,${f}))))
1127
1128.PHONY: util_install_defaults
1129util_install_defaults: ${UTIL_DEFAULTS}
1130	${Q}mkdir -p ${DF_DIR}
1131	${Q}${INSTALL} -t ${DF_DIR} -m 'u=rw,go=r,a-s' ${UTIL_DEFAULTS}
1132
1133# And some signing stuff for the target
1134.PHONY: signing_install
1135signing_install: $(if ${SDK_BUILD},\
1136		   ${SIGNING_SCRIPTS_SDK},${SIGNING_SCRIPTS_BOARD})
1137	@${PRINTF} "    INSTALL       SIGNING\n"
1138	${Q}mkdir -p ${VB_DIR}
1139	${Q}${INSTALL} -t ${VB_DIR} $^
1140
1141# ----------------------------------------------------------------------------
1142# Firmware Utility
1143
1144.PHONY: futil
1145futil: ${FUTIL_BIN}
1146
1147# FUTIL_LIBS is shared by FUTIL_BIN and TEST_FUTIL_BINS.
1148FUTIL_LIBS = ${CROSID_LIBS} ${CRYPTO_LIBS} ${LIBZIP_LIBS} ${LIBARCHIVE_LIBS} \
1149	${FLASHROM_LIBS}
1150
1151${FUTIL_BIN}: LDLIBS += ${FUTIL_LIBS}
1152${FUTIL_BIN}: ${FUTIL_OBJS} ${UTILLIB} ${FWLIB}
1153	@${PRINTF} "    LD            $(subst ${BUILD}/,,$@)\n"
1154	${Q}${LD} -o $@ ${LDFLAGS} $^ ${LDLIBS}
1155
1156.PHONY: futil_install
1157futil_install: ${FUTIL_BIN}
1158	@${PRINTF} "    INSTALL       futility\n"
1159	${Q}mkdir -p ${UB_DIR}
1160	${Q}${INSTALL} -t ${UB_DIR} ${FUTIL_BIN}
1161	${Q}for prog in ${FUTIL_SYMLINKS}; do \
1162		ln -sf futility "${UB_DIR}/$$prog"; done
1163
1164# ----------------------------------------------------------------------------
1165# Utility to generate TLCL structure definition header file.
1166
1167${BUILD}/utility/tlcl_generator: CFLAGS += -fpack-struct
1168
1169STRUCTURES_TMP=${BUILD}/tlcl_structures.tmp
1170STRUCTURES_SRC=firmware/lib/tpm_lite/include/tlcl_structures.h
1171
1172.PHONY: update_tlcl_structures
1173update_tlcl_structures: ${BUILD}/utility/tlcl_generator
1174	@${PRINTF} "    Rebuilding TLCL structures\n"
1175	${Q}${BUILD}/utility/tlcl_generator > ${STRUCTURES_TMP}
1176	${Q}cmp -s ${STRUCTURES_TMP} ${STRUCTURES_SRC} || \
1177		( echo "%% Updating structures.h %%" && \
1178		  cp ${STRUCTURES_TMP} ${STRUCTURES_SRC} )
1179
1180# ----------------------------------------------------------------------------
1181# Tests
1182
1183.PHONY: tests
1184tests: ${TEST_BINS}
1185
1186${TEST_BINS}: ${UTILLIB} ${TESTLIB}
1187${TEST_BINS}: INCLUDES += -Itests
1188${TEST_BINS}: LIBS = ${TESTLIB} ${UTILLIB}
1189
1190# Futility tests need almost everything that futility needs.
1191${TEST_FUTIL_BINS}: ${FUTIL_OBJS} ${UTILLIB}
1192${TEST_FUTIL_BINS}: INCLUDES += -Ifutility
1193${TEST_FUTIL_BINS}: OBJS += ${FUTIL_OBJS} ${UTILLIB}
1194${TEST_FUTIL_BINS}: LDLIBS += ${FUTIL_LIBS}
1195
1196${TEST2X_BINS}: ${FWLIB}
1197${TEST2X_BINS}: LIBS += ${FWLIB}
1198
1199${TEST20_BINS}: ${FWLIB}
1200${TEST20_BINS}: LIBS += ${FWLIB}
1201${TEST20_BINS}: LDLIBS += ${CRYPTO_LIBS}
1202
1203${TESTLIB}: ${TESTLIB_OBJS}
1204	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
1205	${Q}rm -f $@
1206	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
1207	${Q}ar qcT $@ $^
1208
1209DUT_TEST_BINS = $(addprefix ${BUILD}/,${DUT_TEST_NAMES})
1210
1211# Special build for sha256_x86 test
1212${BUILD}/tests/vb2_sha256_x86_tests: \
1213	${BUILD}/firmware/2lib/2sha256_x86.o ${BUILD}/firmware/2lib/2hwcrypto.o
1214${BUILD}/tests/vb2_sha256_x86_tests: \
1215	LIBS += ${BUILD}/firmware/2lib/2sha256_x86.o ${BUILD}/firmware/2lib/2hwcrypto.o
1216
1217ifeq (${ENABLE_HWCRYPTO_RSA_TESTS},1)
1218define enable_hwcrypto_rsa_tests
1219${BUILD}/$(1): CFLAGS += -DENABLE_HWCRYPTO_RSA_TESTS
1220ifeq (${ARCH},arm64)
1221${BUILD}/$(1): CFLAGS += -DARM64_RSA_ACCELERATION
1222${BUILD}/$(1): ${BUILD}/firmware/2lib/2modpow_neon.o
1223${BUILD}/$(1): LIBS += ${BUILD}/firmware/2lib/2modpow_neon.o
1224else
1225${BUILD}/$(1): CFLAGS += -DVB2_X86_RSA_ACCELERATION
1226${BUILD}/$(1): ${BUILD}/firmware/2lib/2modpow_sse2.o
1227${BUILD}/$(1): LIBS += ${BUILD}/firmware/2lib/2modpow_sse2.o
1228endif
1229endef
1230
1231$(foreach test, ${HWCRYPTO_RSA_TESTS}, \
1232	$(eval $(call enable_hwcrypto_rsa_tests,${test})))
1233endif
1234
1235.PHONY: install_dut_test
1236install_dut_test: ${DUT_TEST_BINS}
1237ifneq ($(strip ${DUT_TEST_BINS}),)
1238	@${PRINTF} "    INSTALL       DUT TESTS\n"
1239	${Q}mkdir -p ${DUT_TEST_DIR}
1240	${Q}${INSTALL} -t ${DUT_TEST_DIR} $^
1241endif
1242
1243# ----------------------------------------------------------------------------
1244# Fuzzers
1245
1246.PHONY: fuzzers
1247fuzzers: ${FUZZ_TEST_BINS}
1248
1249${FUZZ_TEST_BINS}: ${FWLIB}
1250${FUZZ_TEST_BINS}: LIBS = ${FWLIB}
1251${FUZZ_TEST_BINS}: LDFLAGS += -fsanitize=fuzzer
1252
1253# ----------------------------------------------------------------------------
1254# Generic build rules. LIBS and OBJS can be overridden to tweak the generic
1255# rules for specific targets.
1256
1257${BUILD}/%: ${BUILD}/%.o ${OBJS} ${LIBS}
1258	@${PRINTF} "    LD            $(subst ${BUILD}/,,$@)\n"
1259	${Q}${LD} -o $@ ${LDFLAGS} $< ${OBJS} ${LIBS} ${LDLIBS}
1260
1261${BUILD}/%.o: %.c
1262	@${PRINTF} "    CC            $(subst ${BUILD}/,,$@)\n"
1263	${Q}${CC} ${CFLAGS} ${INCLUDES} -c -o $@ $<
1264
1265${BUILD}/%.o: ${BUILD}/%.c
1266	@${PRINTF} "    CC            $(subst ${BUILD}/,,$@)\n"
1267	${Q}${CC} ${CFLAGS} ${INCLUDES} -c -o $@ $<
1268
1269${BUILD}/%.o: %.S
1270	@${PRINTF} "    CC            $(subst ${BUILD}/,,$@)\n"
1271	${Q}${CC} ${CFLAGS} ${INCLUDES} -c -o $@ $<
1272
1273# ----------------------------------------------------------------------------
1274# Here are the special tweaks to the generic rules.
1275
1276# Always create the defaults file, since it depends on input variables
1277.PHONY: ${UTIL_DEFAULTS}
1278${UTIL_DEFAULTS}:
1279	@${PRINTF} "    CREATE        $(subst ${BUILD}/,,$@)\n"
1280	${Q}rm -f $@
1281	${Q}mkdir -p $(dir $@)
1282	${Q}echo '# Generated file. Do not edit.' > $@.tmp
1283	${Q}echo "DEV_DEBUG_FORCE=${DEV_DEBUG_FORCE}" >> $@.tmp
1284	${Q}mv -f $@.tmp $@
1285
1286# Some utilities need external crypto functions
1287CRYPTO_LIBS := $(shell ${PKG_CONFIG} --libs libcrypto)
1288ifeq ($(shell uname -s), FreeBSD)
1289CRYPTO_LIBS += -lcrypto
1290endif
1291ifeq ($(shell uname -s), OpenBSD)
1292LDFLAGS += -Wl,-z,notext
1293endif
1294
1295${BUILD}/utility/dumpRSAPublicKey: LDLIBS += ${CRYPTO_LIBS}
1296${BUILD}/utility/pad_digest_utility: LDLIBS += ${CRYPTO_LIBS}
1297${BUILD}/utility/signature_digest_utility: LDLIBS += ${CRYPTO_LIBS}
1298${BUILD}/utility/verify_data: LDLIBS += ${CRYPTO_LIBS}
1299
1300${BUILD}/tests/vb2_host_key_tests: LDLIBS += ${CRYPTO_LIBS}
1301${BUILD}/tests/vb2_common2_tests: LDLIBS += ${CRYPTO_LIBS}
1302${BUILD}/tests/vb2_common3_tests: LDLIBS += ${CRYPTO_LIBS}
1303${BUILD}/tests/verify_kernel: LDLIBS += ${CRYPTO_LIBS}
1304${BUILD}/tests/hmac_test: LDLIBS += ${CRYPTO_LIBS}
1305
1306${TEST21_BINS}: LDLIBS += ${CRYPTO_LIBS}
1307
1308${BUILD}/tests/%: LDLIBS += -lrt -luuid
1309${BUILD}/tests/%: LIBS += ${TESTLIB}
1310
1311ifeq ($(filter-out 0,${TPM2_MODE}),)
1312# TODO(apronin): tests for TPM2 case?
1313TLCL_TEST_BINS = $(addprefix ${BUILD}/,${TLCL_TEST_NAMES})
1314${TLCL_TEST_BINS}: OBJS += ${BUILD}/tests/tpm_lite/tlcl_tests.o
1315${TLCL_TEST_BINS}: ${BUILD}/tests/tpm_lite/tlcl_tests.o
1316TEST_OBJS += ${BUILD}/tests/tpm_lite/tlcl_tests.o
1317endif
1318
1319# ----------------------------------------------------------------------------
1320# Here are the special rules that don't fit in the generic rules.
1321
1322# Generates the list of commands defined in futility by running grep in the
1323# source files looking for the DECLARE_FUTIL_COMMAND() macro usage.
1324${FUTIL_CMD_LIST}: ${FUTIL_SRCS}
1325	@${PRINTF} "    GEN           $(subst ${BUILD}/,,$@)\n"
1326	${Q}rm -f $@ $@_t $@_commands
1327	${Q}mkdir -p ${BUILD}/gen
1328	${Q}grep -hoRE '^DECLARE_FUTIL_COMMAND\([^,]+' $^ \
1329		| sed 's/DECLARE_FUTIL_COMMAND(\(.*\)/_CMD(\1)/' \
1330		| sort >>$@_commands
1331	${Q}./scripts/getversion.sh >> $@_t
1332	${Q}echo '#define _CMD(NAME) extern const struct' \
1333		'futil_cmd_t __cmd_##NAME;' >> $@_t
1334	${Q}cat $@_commands >> $@_t
1335	${Q}echo '#undef _CMD' >> $@_t
1336	${Q}echo '#define _CMD(NAME) &__cmd_##NAME,' >> $@_t
1337	${Q}echo 'const struct futil_cmd_t *const futil_cmds[] = {' >> $@_t
1338	${Q}cat $@_commands >> $@_t
1339	${Q}echo '0};  /* null-terminated */' >> $@_t
1340	${Q}echo '#undef _CMD' >> $@_t
1341	${Q}mv $@_t $@
1342	${Q}rm -f $@_commands
1343
1344##############################################################################
1345# Targets that exist just to run tests
1346
1347.PHONY: test_setup
1348test_setup:: cgpt ${UTIL_FILES_SDK} ${UTIL_FILES_BOARD} futil tests
1349
1350# Generate test keys
1351.PHONY: genkeys
1352genkeys: install_for_test
1353	${RUNTEST} ${SRC_RUN}/tests/gen_test_keys.sh
1354
1355# Generate test cases
1356.PHONY: gentestcases
1357gentestcases: install_for_test
1358	${RUNTEST} ${SRC_RUN}/tests/gen_test_cases.sh
1359
1360# Generate test cases for fuzzing
1361.PHONY: genfuzztestcases
1362genfuzztestcases: install_for_test
1363	${RUNTEST} ${SRC_RUN}/tests/gen_fuzz_test_cases.sh
1364
1365.PHONY: runcgpttests
1366runcgpttests: install_for_test
1367	${RUNTEST} ${BUILD_RUN}/tests/cgptlib_test
1368
1369.PHONY: runtestscripts
1370runtestscripts: install_for_test ${HOSTLIB_DEF} ${HOSTLIB_UNDEF}
1371	${RUNTEST} ${SRC_RUN}/scripts/image_signing/sign_android_unittests.sh
1372	${RUNTEST} ${SRC_RUN}/scripts/image_signing/sign_uefi_unittest.py
1373	${RUNTEST} $(SRC_RUN)/scripts/image_signing/lib/generate_android_cloud_config_unittest.py
1374	${RUNTEST} ${SRC_RUN}/tests/load_kernel_tests.sh
1375	${RUNTEST} ${SRC_RUN}/tests/run_cgpt_tests.sh ${BUILD_RUN}/cgpt/cgpt
1376	${RUNTEST} ${SRC_RUN}/tests/run_cgpt_tests.sh ${BUILD_RUN}/cgpt/cgpt -D 358400
1377	${RUNTEST} ${SRC_RUN}/tests/run_preamble_tests.sh
1378	${RUNTEST} ${SRC_RUN}/tests/run_vbutil_kernel_arg_tests.sh
1379	${RUNTEST} ${SRC_RUN}/tests/run_vbutil_tests.sh
1380	${RUNTEST} ${SRC_RUN}/tests/swap_ec_rw_tests.sh
1381	${RUNTEST} ${SRC_RUN}/tests/vb2_rsa_tests.sh
1382	${RUNTEST} ${SRC_RUN}/tests/vb2_firmware_tests.sh
1383	${RUNTEST} ${SRC_RUN}/tests/vhost_reference.sh ${HOSTLIB_DEF} ${HOSTLIB_UNDEF}
1384
1385.PHONY: runmisctests
1386runmisctests: install_for_test
1387	${RUNTEST} ${BUILD_RUN}/tests/cbfstool_tests
1388	${RUNTEST} ${BUILD_RUN}/tests/gpt_misc_tests
1389	${RUNTEST} ${BUILD_RUN}/tests/subprocess_tests
1390ifeq ($(filter-out 0,${MOCK_TPM})$(filter-out 0,${TPM2_MODE}),)
1391# tlcl_tests only works when MOCK_TPM is disabled
1392	${RUNTEST} ${BUILD_RUN}/tests/tlcl_tests
1393endif
1394
1395.PHONY: run2tests
1396run2tests: install_for_test
1397	${RUNTEST} ${BUILD_RUN}/tests/vb2_api_tests
1398	${RUNTEST} ${BUILD_RUN}/tests/vb2_auxfw_sync_tests
1399	${RUNTEST} ${BUILD_RUN}/tests/vb2_common_tests
1400	${RUNTEST} ${BUILD_RUN}/tests/vb2_common2_tests ${TEST_KEYS}
1401	${RUNTEST} ${BUILD_RUN}/tests/vb2_common3_tests ${TEST_KEYS}
1402	${RUNTEST} ${BUILD_RUN}/tests/vb2_crypto_tests
1403	${RUNTEST} ${BUILD_RUN}/tests/vb2_ec_sync_tests
1404	${RUNTEST} ${BUILD_RUN}/tests/vb2_firmware_tests
1405	${RUNTEST} ${BUILD_RUN}/tests/vb2_gbb_init_tests
1406	${RUNTEST} ${BUILD_RUN}/tests/vb2_gbb_tests
1407	${RUNTEST} ${BUILD_RUN}/tests/vb2_host_key_tests
1408	${RUNTEST} ${BUILD_RUN}/tests/vb2_inject_kernel_subkey_tests
1409	${RUNTEST} ${BUILD_RUN}/tests/vb2_load_kernel_tests
1410	${RUNTEST} ${BUILD_RUN}/tests/vb2_load_kernel2_tests
1411	${RUNTEST} ${BUILD_RUN}/tests/vb2_kernel_tests
1412	${RUNTEST} ${BUILD_RUN}/tests/vb2_misc_tests
1413	${RUNTEST} ${BUILD_RUN}/tests/vb2_misc2_tests
1414	${RUNTEST} ${BUILD_RUN}/tests/vb2_nvstorage_tests
1415	${RUNTEST} ${BUILD_RUN}/tests/vb2_rsa_utility_tests
1416	${RUNTEST} ${BUILD_RUN}/tests/vb2_secdata_firmware_tests
1417	${RUNTEST} ${BUILD_RUN}/tests/vb2_secdata_fwmp_tests
1418	${RUNTEST} ${BUILD_RUN}/tests/vb2_secdata_kernel_tests
1419	${RUNTEST} ${BUILD_RUN}/tests/vb2_sha_api_tests
1420	${RUNTEST} ${BUILD_RUN}/tests/vb2_sha_tests
1421	${RUNTEST} ${BUILD_RUN}/tests/vb20_api_kernel_tests
1422	${RUNTEST} ${BUILD_RUN}/tests/vb20_kernel_tests
1423	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_common_tests
1424	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_common2_tests ${TEST_KEYS}
1425	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_key_tests ${TEST_KEYS} ${BUILD_RUN}
1426	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_misc_tests ${BUILD_RUN}
1427	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_sig_tests ${TEST_KEYS}
1428	${RUNTEST} ${BUILD_RUN}/tests/hmac_test
1429
1430.PHONY: runfutiltests
1431runfutiltests: install_for_test
1432	${RUNTEST} ${SRC_RUN}/tests/futility/run_test_scripts.sh
1433	${RUNTEST} ${BUILD_RUN}/tests/futility/test_file_types
1434	${RUNTEST} ${BUILD_RUN}/tests/futility/test_not_really
1435
1436# Test all permutations of encryption keys, instead of just the ones we use.
1437# Not run by automated build.
1438.PHONY: runlongtests
1439runlongtests: install_for_test genkeys genfuzztestcases
1440	${RUNTEST} ${BUILD_RUN}/tests/vb2_common2_tests ${TEST_KEYS} --all
1441	${RUNTEST} ${BUILD_RUN}/tests/vb2_common3_tests ${TEST_KEYS} --all
1442	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_common2_tests ${TEST_KEYS} --all
1443	${RUNTEST} ${SRC_RUN}/tests/run_preamble_tests.sh --all
1444	${RUNTEST} ${SRC_RUN}/tests/run_vbutil_tests.sh --all
1445
1446.PHONY: rununittests
1447rununittests: runcgpttests runmisctests run2tests
1448
1449# Print a big green success message at the end of all tests. If you don't see
1450# that, you know there was an error somewhere further up.
1451.PHONY: runtests
1452runtests: rununittests runtestscripts runfutiltests
1453	${Q}echo -e "\nruntests: \E[32;1mALL TESTS PASSED SUCCESSFULLY!\E[0;m\n"
1454
1455# Code coverage
1456.PHONY: coverage
1457ifeq ($(filter-out 0,${COV}),)
1458coverage:
1459	$(error Build coverage like this: make clean && COV=1 make coverage)
1460else
1461.PHONY: coverage_init
1462coverage_init: install_for_test
1463	rm -f ${COV_INFO}*
1464	lcov -c -i -d . -b . -o ${COV_INFO}.initial
1465
1466.PHONY: coverage_html
1467coverage_html: coverage_init runtests
1468	lcov -c -d . -b . -o ${COV_INFO}.tests
1469	lcov -a ${COV_INFO}.initial -a ${COV_INFO}.tests -o ${COV_INFO}.total
1470	lcov -r ${COV_INFO}.total '/usr/*' -o ${COV_INFO}.local
1471	genhtml ${COV_INFO}.local -o ${BUILD}/coverage
1472# Generate addtional coverage stats just for firmware subdir, because the stats
1473# for the whole project don't include subdirectory summaries. This will print
1474# the summary for just the firmware sources.
1475	lcov -r ${COV_INFO}.local '*/stub/*' -o ${COV_INFO}.nostub
1476	lcov -e ${COV_INFO}.nostub '${SRCDIR}/firmware/*' \
1477		-o ${COV_INFO}.firmware
1478
1479coverage: coverage_init runtests coverage_html
1480endif
1481
1482# Include generated dependencies
1483ALL_DEPS += ${ALL_OBJS:%.o=%.o.d}
1484TEST_DEPS += ${TEST_OBJS:%.o=%.o.d}
1485-include ${ALL_DEPS}
1486-include ${TEST_DEPS}
1487
1488# We want to use only relative paths in cscope.files, especially since the
1489# paths inside and outside the chroot are different.
1490SRCDIRPAT=$(subst /,\/,${SRCDIR}/)
1491
1492# Note: vboot 2.0 is deprecated, so don't index those files
1493${BUILD}/cscope.files: all install_for_test
1494	${Q}rm -f $@
1495	${Q}cat ${ALL_DEPS} | tr -d ':\\' | tr ' ' '\012' | \
1496		grep -v /lib20/ | \
1497		sed -e "s/${SRCDIRPAT}//" | \
1498		egrep '\.[chS]$$' | sort | uniq > $@
1499
1500cmd_etags = etags -o ${BUILD}/TAGS $(shell cat ${BUILD}/cscope.files)
1501cmd_ctags = ctags -o ${BUILD}/tags $(shell cat ${BUILD}/cscope.files)
1502run_if_prog = $(if $(shell which $(1) 2>/dev/null),$(2),)
1503
1504.PHONY: tags TAGS xrefs
1505tags TAGS xrefs: ${BUILD}/cscope.files
1506	${Q}\rm -f ${BUILD}/tags ${BUILD}/TAGS
1507	${Q}$(call run_if_prog,etags,${cmd_etags})
1508	${Q}$(call run_if_prog,ctags,${cmd_ctags})
1509
1510PC_FILES = ${PC_IN_FILES:%.pc.in=${BUILD}/%.pc}
1511${PC_FILES}: ${PC_IN_FILES}
1512	${Q}sed \
1513		-e 's:@LDLIBS@:${LDLIBS}:' \
1514		-e 's:@LIBDIR@:${LIBDIR}:' \
1515		$< > $@
1516
1517.PHONY: pc_files_install
1518pc_files_install: ${PC_FILES}
1519	${Q}mkdir -p ${ULP_DIR}
1520	${Q}${INSTALL} -D -m 0644 $< ${ULP_DIR}/$(notdir $<)
1521