1# Copyright 2013 The Chromium OS Authors. All rights reserved.
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)
40export SRCDIR
41BUILD = ${SRCDIR}/build
42export BUILD
43
44# Stuff for 'make install'
45INSTALL = install
46DESTDIR = /usr/local
47LIBDIR ?= lib
48
49# Default values
50DEV_DEBUG_FORCE=
51
52# Where exactly do the pieces go?
53#  UB_DIR = utility binary directory
54#  ULP_DIR = pkgconfig directory, usually /usr/lib/pkgconfig
55#  DF_DIR = utility defaults directory
56#  VB_DIR = vboot binary directory for dev-mode-only scripts
57ifeq (${MINIMAL},)
58# Host install just puts everything where it's told
59UB_DIR=${DESTDIR}/bin
60ULP_DIR=${DESTDIR}/${LIBDIR}/pkgconfig
61DF_DIR=${DESTDIR}/default
62VB_DIR=${DESTDIR}/bin
63else
64# Target install puts things into different places
65UB_DIR=${DESTDIR}/usr/bin
66ULP_DIR=${DESTDIR}/usr/${LIBDIR}/pkgconfig
67DF_DIR=${DESTDIR}/etc/default
68VB_DIR=${DESTDIR}/usr/share/vboot/bin
69endif
70
71# Where to install the (exportable) executables for testing?
72TEST_INSTALL_DIR = ${BUILD}/install_for_test
73
74# Verbose? Use V=1
75ifeq (${V},)
76Q := @
77endif
78
79# Quiet? Use QUIET=1
80ifeq (${QUIET},)
81PRINTF := printf
82else
83PRINTF := :
84endif
85
86# Architecture detection
87_machname := $(shell uname -m)
88HOST_ARCH ?= ${_machname}
89
90# ARCH and/or FIRMWARE_ARCH are defined by the Chromium OS ebuild.
91# Pick a sane target architecture if none is defined.
92ifeq (${ARCH},)
93  ARCH := ${HOST_ARCH}
94else ifeq (${ARCH},i386)
95  override ARCH := x86
96else ifeq (${ARCH},amd64)
97  override ARCH := x86_64
98endif
99
100# FIRMWARE_ARCH is only defined by the Chromium OS ebuild if compiling
101# for a firmware target (such as u-boot or depthcharge). It must map
102# to the same consistent set of architectures as the host.
103ifeq (${FIRMWARE_ARCH},i386)
104  override FIRMWARE_ARCH := x86
105else ifeq (${FIRMWARE_ARCH},amd64)
106  override FIRMWARE_ARCH := x86_64
107else ifeq (${FIRMWARE_ARCH},armv7)
108  override FIRMWARE_ARCH := arm
109endif
110
111# Provide default CC and CFLAGS for firmware builds; if you have any -D flags,
112# please add them after this point (e.g., -DVBOOT_DEBUG).
113#
114# TODO(crosbug.com/16808) We hard-code u-boot's compiler flags here just
115# temporarily. As we are still investigating which flags are necessary for
116# maintaining a compatible ABI, etc. between u-boot and vboot_reference.
117#
118# As a first step, this makes the setting of CC and CFLAGS here optional, to
119# permit a calling script or Makefile to set these.
120#
121# Flag ordering: arch, then -f, then -m, then -W
122DEBUG_FLAGS := $(if ${DEBUG},-g -O0,-Os)
123COMMON_FLAGS := -nostdinc -pipe \
124	-ffreestanding -fno-builtin -fno-stack-protector \
125	-Werror -Wall -Wstrict-prototypes ${DEBUG_FLAGS}
126
127# Note: FIRMWARE_ARCH is defined by the Chromium OS ebuild.
128ifeq (${FIRMWARE_ARCH}, arm)
129CC ?= armv7a-cros-linux-gnueabi-gcc
130CFLAGS ?= -march=armv5 \
131	-fno-common -ffixed-r8 \
132	-mfloat-abi=hard -marm -mabi=aapcs-linux -mno-thumb-interwork \
133	${COMMON_FLAGS}
134else ifeq (${FIRMWARE_ARCH}, x86)
135CC ?= i686-pc-linux-gnu-gcc
136# Drop -march=i386 to permit use of SSE instructions
137CFLAGS ?= \
138	-ffunction-sections -fvisibility=hidden -fno-strict-aliasing \
139	-fomit-frame-pointer -fno-toplevel-reorder -fno-dwarf2-cfi-asm \
140	-mpreferred-stack-boundary=2 \
141	${COMMON_FLAGS}
142else ifeq (${FIRMWARE_ARCH}, x86_64)
143CFLAGS ?= ${COMMON_FLAGS} \
144	-fvisibility=hidden -fno-strict-aliasing -fomit-frame-pointer
145else
146# FIRMWARE_ARCH not defined; assuming local compile.
147CC ?= gcc
148CFLAGS += -DCHROMEOS_ENVIRONMENT -Wall -Werror ${DEBUG_FLAGS}
149endif
150
151ifneq (${DEBUG},)
152CFLAGS += -DVBOOT_DEBUG
153endif
154
155ifeq (${DISABLE_NDEBUG},)
156CFLAGS += -DNDEBUG
157endif
158
159ifneq (${FORCE_LOGGING_ON},)
160CFLAGS += -DFORCE_LOGGING_ON=${FORCE_LOGGING_ON}
161endif
162
163ifneq (${PD_SYNC},)
164CFLAGS += -DPD_SYNC
165endif
166
167ifneq (${USE_MTD},)
168CFLAGS += -DUSE_MTD
169LDLIBS += -lmtdutils
170endif
171
172# NOTE: We don't use these files but they are useful for other packages to
173# query about required compiling/linking flags.
174PC_IN_FILES = vboot_host.pc.in
175
176# Create / use dependency files
177CFLAGS += -MMD -MF $@.d
178
179ifeq (${FIRMWARE_ARCH},)
180# Creates position independent code for non firmware target.
181CFLAGS += -fPIE
182endif
183
184# These are required to access large disks and files on 32-bit systems.
185CFLAGS += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
186
187# Code coverage
188ifneq (${COV},)
189  COV_FLAGS = -O0 --coverage -DCOVERAGE
190  CFLAGS += ${COV_FLAGS}
191  LDFLAGS += ${COV_FLAGS}
192  COV_INFO = ${BUILD}/coverage.info
193endif
194
195ifdef HAVE_MACOS
196  CFLAGS += -DHAVE_MACOS -Wno-deprecated-declarations
197endif
198
199# And a few more default utilities
200LD = ${CC}
201CXX ?= g++
202PKG_CONFIG ?= pkg-config
203
204# Static?
205ifneq (${STATIC},)
206LDFLAGS += -static
207PKG_CONFIG += --static
208endif
209
210# Determine QEMU architecture needed, if any
211ifeq (${ARCH},${HOST_ARCH})
212  # Same architecture; no need for QEMU
213  QEMU_ARCH :=
214else ifeq (${HOST_ARCH}-${ARCH},x86_64-x86)
215  # 64-bit host can run 32-bit targets directly
216  QEMU_ARCH :=
217else
218  QEMU_ARCH := ${ARCH}
219endif
220
221# The top of the chroot for qemu must be passed in via the SYSROOT environment
222# variable.  In the Chromium OS chroot, this is done automatically by the
223# ebuild.
224
225ifeq (${QEMU_ARCH},)
226  # Path to build output for running tests is same as for building
227  BUILD_RUN = ${BUILD}
228  SRC_RUN = ${SRCDIR}
229else
230  $(info Using qemu for testing.)
231  # Path to build output for running tests is different in the chroot
232  BUILD_RUN = $(subst ${SYSROOT},,${BUILD})
233  SRC_RUN = $(subst ${SYSROOT},,${SRCDIR})
234
235  QEMU_BIN = qemu-${QEMU_ARCH}
236  QEMU_RUN = ${BUILD_RUN}/${QEMU_BIN}
237  export QEMU_RUN
238
239  RUNTEST = tests/test_using_qemu.sh
240endif
241
242export BUILD_RUN
243
244##############################################################################
245# Now we need to describe everything we might want or need to build
246
247# Everything wants these headers.
248INCLUDES += \
249	-Ifirmware/include \
250	-Ifirmware/lib/include \
251	-Ifirmware/lib/cgptlib/include \
252	-Ifirmware/lib/cryptolib/include \
253	-Ifirmware/lib/tpm_lite/include \
254	-Ifirmware/2lib/include
255
256# If we're not building for a specific target, just stub out things like the
257# TPM commands and various external functions that are provided by the BIOS.
258ifeq (${FIRMWARE_ARCH},)
259INCLUDES += -Ihost/include -Ihost/lib/include
260endif
261
262# Firmware library, used by the other firmware components (depthcharge,
263# coreboot, etc.). It doesn't need exporting to some other place; they'll build
264# this source tree locally and link to it directly.
265FWLIB = ${BUILD}/vboot_fw.a
266
267# Smaller firmware library common to all vboot 2.x, used only for
268# 1) compile-time tests of the public API or
269# 2) linking with an actual 2.0 or 2.1 implementation
270FWLIB2X = ${BUILD}/vboot_fw2x.a
271
272# Vboot 2.0 (deprecated - see firmware/README)
273FWLIB20 = ${BUILD}/vboot_fw20.a
274# Vboot 2.1 (not yet ready - see firmware/README)
275FWLIB21 = ${BUILD}/vboot_fw21.a
276
277# Firmware library sources needed by VbInit() call
278VBINIT_SRCS = \
279	firmware/lib/crc8.c \
280	firmware/lib/utility.c \
281	firmware/lib/vboot_api_init.c \
282	firmware/lib/vboot_common_init.c \
283	firmware/lib/vboot_nvstorage.c \
284	firmware/lib/vboot_nvstorage_rollback.c \
285	firmware/lib/region-init.c \
286
287# Additional firmware library sources needed by VbSelectFirmware() call
288VBSF_SRCS = \
289	firmware/lib/cryptolib/padding.c \
290	firmware/lib/cryptolib/rsa.c \
291	firmware/lib/cryptolib/rsa_utility.c \
292	firmware/lib/cryptolib/sha1.c \
293	firmware/lib/cryptolib/sha256.c \
294	firmware/lib/cryptolib/sha512.c \
295	firmware/lib/cryptolib/sha_utility.c \
296	firmware/lib/stateful_util.c \
297	firmware/lib/vboot_api_firmware.c \
298	firmware/lib/vboot_common.c \
299	firmware/lib/vboot_firmware.c \
300	firmware/lib/region-fw.c \
301
302# Additional firmware library sources needed by VbSelectAndLoadKernel() call
303VBSLK_SRCS = \
304	firmware/lib/cgptlib/cgptlib.c \
305	firmware/lib/cgptlib/cgptlib_internal.c \
306	firmware/lib/cgptlib/crc32.c \
307	firmware/lib/gpt_misc.c \
308	firmware/lib/utility_string.c \
309	firmware/lib/vboot_api_kernel.c \
310	firmware/lib/vboot_audio.c \
311	firmware/lib/vboot_display.c \
312	firmware/lib/vboot_kernel.c \
313	firmware/lib/region-kernel.c \
314
315# Code common to both vboot 2.0 (old structs) and 2.1 (new structs)
316FWLIB2X_SRCS = \
317	firmware/2lib/2api.c \
318	firmware/2lib/2common.c \
319	firmware/2lib/2crc8.c \
320	firmware/2lib/2misc.c \
321	firmware/2lib/2nvstorage.c \
322	firmware/2lib/2rsa.c \
323	firmware/2lib/2secdata.c \
324	firmware/2lib/2sha1.c \
325	firmware/2lib/2sha256.c \
326	firmware/2lib/2sha512.c \
327	firmware/2lib/2sha_utility.c \
328	firmware/2lib/2tpm_bootmode.c
329
330FWLIB20_SRCS = \
331	firmware/lib20/api.c \
332	firmware/lib20/common.c \
333	firmware/lib20/misc.c \
334	firmware/lib20/packed_key.c
335
336FWLIB21_SRCS = \
337	firmware/lib21/api.c \
338	firmware/lib21/common.c \
339	firmware/lib21/misc.c \
340	firmware/lib21/packed_key.c
341
342# Support real TPM unless BIOS sets MOCK_TPM
343ifeq (${MOCK_TPM},)
344VBINIT_SRCS += \
345	firmware/lib/rollback_index.c \
346	firmware/lib/tpm_lite/tlcl.c
347
348VBSF_SRCS += \
349	firmware/lib/tpm_bootmode.c
350else
351VBINIT_SRCS += \
352	firmware/lib/mocked_rollback_index.c \
353	firmware/lib/tpm_lite/mocked_tlcl.c
354
355VBSF_SRCS += \
356	firmware/lib/mocked_tpm_bootmode.c
357endif
358
359ifeq (${FIRMWARE_ARCH},)
360# Include BIOS stubs in the firmware library when compiling for host
361# TODO: split out other stub funcs too
362VBINIT_SRCS += \
363	firmware/stub/tpm_lite_stub.c \
364	firmware/stub/utility_stub.c \
365	firmware/stub/vboot_api_stub_init.c \
366	firmware/stub/vboot_api_stub_region.c
367
368VBSF_SRCS += \
369	firmware/stub/vboot_api_stub_sf.c
370
371VBSLK_SRCS += \
372	firmware/stub/vboot_api_stub.c \
373	firmware/stub/vboot_api_stub_disk.c \
374	firmware/stub/vboot_api_stub_stream.c
375
376FWLIB2X_SRCS += \
377	firmware/2lib/2stub.c
378
379endif
380
381VBSF_SRCS += ${VBINIT_SRCS}
382FWLIB_SRCS += ${VBSF_SRCS} ${VBSLK_SRCS}
383
384VBINIT_OBJS = ${VBINIT_SRCS:%.c=${BUILD}/%.o}
385VBSF_OBJS = ${VBSF_SRCS:%.c=${BUILD}/%.o}
386ALL_OBJS +=  ${VBINIT_OBJS} ${VBSF_OBJS}
387
388FWLIB_OBJS = ${FWLIB_SRCS:%.c=${BUILD}/%.o}
389FWLIB2X_OBJS = ${FWLIB2X_SRCS:%.c=${BUILD}/%.o}
390FWLIB20_OBJS = ${FWLIB20_SRCS:%.c=${BUILD}/%.o}
391FWLIB21_OBJS = ${FWLIB21_SRCS:%.c=${BUILD}/%.o}
392ALL_OBJS += ${FWLIB_OBJS} ${FWLIB2X_OBJS} ${FWLIB20_OBJS} ${FWLIB21_OBJS}
393
394# Intermediate library for the vboot_reference utilities to link against.
395UTILLIB = ${BUILD}/libvboot_util.a
396UTILLIB21 = ${BUILD}/libvboot_util21.a
397
398UTILLIB_SRCS = \
399	cgpt/cgpt_create.c \
400	cgpt/cgpt_add.c \
401	cgpt/cgpt_boot.c \
402	cgpt/cgpt_show.c \
403	cgpt/cgpt_repair.c \
404	cgpt/cgpt_prioritize.c \
405	cgpt/cgpt_common.c \
406	futility/dump_kernel_config_lib.c \
407	host/arch/${ARCH}/lib/crossystem_arch.c \
408	host/lib/crossystem.c \
409	host/lib/file_keys.c \
410	host/lib/fmap.c \
411	host/lib/host_common.c \
412	host/lib/host_key.c \
413	host/lib/host_keyblock.c \
414	host/lib/host_misc.c \
415	host/lib/util_misc.c \
416	host/lib/host_signature.c \
417	host/lib/signature_digest.c
418
419UTILLIB_OBJS = ${UTILLIB_SRCS:%.c=${BUILD}/%.o}
420ALL_OBJS += ${UTILLIB_OBJS}
421
422UTILLIB21_SRCS += \
423	host/lib21/host_fw_preamble.c \
424	host/lib21/host_key.c \
425	host/lib21/host_keyblock.c \
426	host/lib21/host_misc.c \
427	host/lib21/host_signature.c
428
429UTILLIB21_OBJS = ${UTILLIB21_SRCS:%.c=${BUILD}/%.o}
430ALL_OBJS += ${UTILLIB21_OBJS}
431
432# Externally exported library for some target userspace apps to link with
433# (cryptohome, updater, etc.)
434HOSTLIB = ${BUILD}/libvboot_host.a
435
436HOSTLIB_SRCS = \
437	cgpt/cgpt_add.c \
438	cgpt/cgpt_boot.c \
439	cgpt/cgpt_common.c \
440	cgpt/cgpt_create.c \
441	cgpt/cgpt_prioritize.c \
442	firmware/lib/cgptlib/cgptlib_internal.c \
443	firmware/lib/cgptlib/crc32.c \
444	firmware/lib/crc8.c \
445	firmware/lib/gpt_misc.c \
446	firmware/lib/tpm_lite/tlcl.c \
447	firmware/lib/utility_string.c \
448	firmware/lib/vboot_nvstorage.c \
449	firmware/stub/tpm_lite_stub.c \
450	firmware/stub/utility_stub.c \
451	firmware/stub/vboot_api_stub_disk.c \
452	firmware/stub/vboot_api_stub_init.c \
453	firmware/stub/vboot_api_stub_sf.c \
454	futility/dump_kernel_config_lib.c \
455	host/arch/${ARCH}/lib/crossystem_arch.c \
456	host/lib/crossystem.c \
457	host/lib/extract_vmlinuz.c \
458	host/lib/fmap.c \
459	host/lib/host_misc.c
460
461HOSTLIB_OBJS = ${HOSTLIB_SRCS:%.c=${BUILD}/%.o}
462ALL_OBJS += ${HOSTLIB_OBJS}
463
464# Sigh. For historical reasons, the autoupdate installer must sometimes be a
465# 32-bit executable, even when everything else is 64-bit. But it only needs a
466# few functions, so let's just build those.
467TINYHOSTLIB = ${BUILD}/libtinyvboot_host.a
468
469TINYHOSTLIB_SRCS = \
470	cgpt/cgpt_add.c \
471	cgpt/cgpt_boot.c \
472	cgpt/cgpt_common.c \
473	cgpt/cgpt_create.c \
474	cgpt/cgpt_prioritize.c \
475	firmware/lib/cgptlib/cgptlib_internal.c \
476	firmware/lib/cgptlib/crc32.c \
477	firmware/lib/gpt_misc.c \
478	firmware/lib/utility_string.c \
479	firmware/stub/vboot_api_stub_disk.c \
480	firmware/stub/vboot_api_stub_sf.c \
481	firmware/stub/utility_stub.c \
482	futility/dump_kernel_config_lib.c \
483	host/lib/extract_vmlinuz.c
484
485TINYHOSTLIB_OBJS = ${TINYHOSTLIB_SRCS:%.c=${BUILD}/%.o}
486
487# ----------------------------------------------------------------------------
488# Now for the userspace binaries
489
490CGPT = ${BUILD}/cgpt/cgpt
491
492CGPT_SRCS = \
493	cgpt/cgpt.c \
494	cgpt/cgpt_add.c \
495	cgpt/cgpt_boot.c \
496	cgpt/cgpt_common.c \
497	cgpt/cgpt_create.c \
498	cgpt/cgpt_find.c \
499	cgpt/cgpt_legacy.c \
500	cgpt/cgpt_nor.c \
501	cgpt/cgpt_prioritize.c \
502	cgpt/cgpt_repair.c \
503	cgpt/cgpt_show.c \
504	cgpt/cmd_add.c \
505	cgpt/cmd_boot.c \
506	cgpt/cmd_create.c \
507	cgpt/cmd_find.c \
508	cgpt/cmd_legacy.c \
509	cgpt/cmd_prioritize.c \
510	cgpt/cmd_repair.c \
511	cgpt/cmd_show.c
512
513CGPT_OBJS = ${CGPT_SRCS:%.c=${BUILD}/%.o}
514
515ALL_OBJS += ${CGPT_OBJS}
516
517CGPT_WRAPPER = ${BUILD}/cgpt/cgpt_wrapper
518
519CGPT_WRAPPER_SRCS = \
520	cgpt/cgpt_nor.c \
521	cgpt/cgpt_wrapper.c
522
523CGPT_WRAPPER_OBJS = ${CGPT_WRAPPER_SRCS:%.c=${BUILD}/%.o}
524
525ALL_OBJS += ${CGPT_WRAPPER_OBJS}
526
527# Utility defaults
528UTIL_DEFAULTS = ${BUILD}/default/vboot_reference
529
530# Scripts to install directly (not compiled)
531UTIL_SCRIPTS = \
532	utility/dev_debug_vboot \
533	utility/enable_dev_usb_boot
534
535ifeq (${MINIMAL},)
536UTIL_SCRIPTS += \
537	utility/dev_make_keypair \
538	utility/vbutil_what_keys
539endif
540
541# These utilities should be linked statically.
542UTIL_NAMES_STATIC = \
543	utility/crossystem
544
545UTIL_NAMES = ${UTIL_NAMES_STATIC} \
546	utility/tpm_init_temp_fix \
547	utility/dumpRSAPublicKey \
548	utility/tpmc
549
550# TODO: Do we still need eficompress and efidecompress for anything?
551ifeq (${MINIMAL},)
552UTIL_NAMES += \
553	utility/bmpblk_font \
554	utility/bmpblk_utility \
555	utility/eficompress \
556	utility/efidecompress \
557	utility/load_kernel_test \
558	utility/pad_digest_utility \
559	utility/signature_digest_utility \
560	utility/verify_data
561endif
562
563UTIL_BINS_STATIC := $(addprefix ${BUILD}/,${UTIL_NAMES_STATIC})
564UTIL_BINS = $(addprefix ${BUILD}/,${UTIL_NAMES})
565ALL_OBJS += $(addsuffix .o,${UTIL_BINS} ${UTIL_BINS_STATIC})
566
567
568# Scripts for signing stuff.
569SIGNING_SCRIPTS = \
570	utility/tpm-nvsize \
571	utility/chromeos-tpm-recovery
572
573# These go in a different place.
574SIGNING_SCRIPTS_DEV = \
575	scripts/image_signing/resign_firmwarefd.sh \
576	scripts/image_signing/make_dev_firmware.sh \
577	scripts/image_signing/make_dev_ssd.sh \
578	scripts/image_signing/set_gbb_flags.sh
579
580# Installed, but not made executable.
581SIGNING_COMMON = scripts/image_signing/common_minimal.sh
582
583
584# The unified firmware utility will eventually replace all the others
585FUTIL_BIN = ${BUILD}/futility/futility
586# But we still need both static (tiny) and dynamic (with openssl) versions.
587FUTIL_STATIC_BIN = ${FUTIL_BIN}_s
588
589# These are the executables that are now built in to futility. We'll create
590# symlinks for these so the old names will still work.
591FUTIL_SYMLINKS = \
592	dump_fmap \
593	dump_kernel_config \
594	gbb_utility \
595	vbutil_firmware \
596	vbutil_kernel \
597	vbutil_key \
598	vbutil_keyblock
599
600FUTIL_STATIC_SRCS = \
601	futility/futility.c \
602	futility/cmd_dump_fmap.c \
603	futility/cmd_gbb_utility.c \
604	futility/misc.c
605
606FUTIL_SRCS = \
607	${FUTIL_STATIC_SRCS} \
608	futility/cmd_create.c \
609	futility/cmd_dump_kernel_config.c \
610	futility/cmd_load_fmap.c \
611	futility/cmd_pcr.c \
612	futility/cmd_show.c \
613	futility/cmd_sign.c \
614	futility/cmd_vbutil_firmware.c \
615	futility/cmd_vbutil_kernel.c \
616	futility/cmd_vbutil_key.c \
617	futility/cmd_vbutil_keyblock.c \
618	futility/file_type.c \
619	futility/traversal.c \
620	futility/vb1_helper.c
621
622# List of commands built in futility and futility_s.
623FUTIL_STATIC_CMD_LIST = ${BUILD}/gen/futility_static_cmds.c
624FUTIL_CMD_LIST = ${BUILD}/gen/futility_cmds.c
625
626# Workaround for TODO(crbug.com/437107).
627FUTIL_STATIC_WORKAROUND_SRCS = firmware/stub/vboot_api_stub_static_sf.c
628
629FUTIL_STATIC_OBJS = ${FUTIL_STATIC_SRCS:%.c=${BUILD}/%.o} \
630	${FUTIL_STATIC_WORKAROUND_SRCS:%.c=${BUILD}/%.o} \
631	${FUTIL_STATIC_CMD_LIST:%.c=%.o}
632FUTIL_OBJS = ${FUTIL_SRCS:%.c=${BUILD}/%.o} ${FUTIL_CMD_LIST:%.c=%.o}
633
634${FUTIL_OBJS}: INCLUDES += -Ihost/lib21/include -Ifirmware/lib21/include
635${FUTIL_BIN}: ${UTILLIB21}
636${FUTIL_BIN}: LIBS += ${UTILLIB21}
637
638ALL_OBJS += ${FUTIL_OBJS}
639
640
641# Library of handy test functions.
642TESTLIB = ${BUILD}/tests/test.a
643
644TESTLIB_SRCS = \
645	tests/test_common.c \
646	tests/timer_utils.c \
647	tests/crc32_test.c
648
649TESTLIB_OBJS = ${TESTLIB_SRCS:%.c=${BUILD}/%.o}
650TEST_OBJS += ${TESTLIB_OBJS}
651
652
653# And some compiled tests.
654TEST_NAMES = \
655	tests/cgptlib_test \
656	tests/rollback_index2_tests \
657	tests/rollback_index3_tests \
658	tests/rsa_padding_test \
659	tests/rsa_utility_tests \
660	tests/rsa_verify_benchmark \
661	tests/sha_benchmark \
662	tests/sha_tests \
663	tests/stateful_util_tests \
664	tests/tlcl_tests \
665	tests/tpm_bootmode_tests \
666	tests/utility_string_tests \
667	tests/utility_tests \
668	tests/vboot_api_init_tests \
669	tests/vboot_api_devmode_tests \
670	tests/vboot_api_firmware_tests \
671	tests/vboot_api_kernel_tests \
672	tests/vboot_api_kernel2_tests \
673	tests/vboot_api_kernel3_tests \
674	tests/vboot_api_kernel4_tests \
675	tests/vboot_audio_tests \
676	tests/vboot_common_tests \
677	tests/vboot_common2_tests \
678	tests/vboot_common3_tests \
679	tests/vboot_display_tests \
680	tests/vboot_firmware_tests \
681	tests/vboot_kernel_tests \
682	tests/vboot_nvstorage_test \
683	tests/verify_kernel \
684	tests/futility/binary_editor \
685	tests/futility/test_not_really
686
687ifdef REGION_READ
688TEST_NAMES += tests/vboot_region_tests
689endif
690
691TEST2X_NAMES = \
692	tests/vb2_api_tests \
693	tests/vb2_common_tests \
694	tests/vb2_misc_tests \
695	tests/vb2_nvstorage_tests \
696	tests/vb2_rsa_utility_tests \
697	tests/vb2_secdata_tests \
698	tests/vb2_sha_tests
699
700TEST20_NAMES = \
701	tests/vb20_api_tests \
702	tests/vb20_common_tests \
703	tests/vb20_common2_tests \
704	tests/vb20_verify_fw.c \
705	tests/vb20_common3_tests \
706	tests/vb20_misc_tests \
707	tests/vb20_rsa_padding_tests \
708	tests/vb20_verify_fw
709
710TEST21_NAMES = \
711	tests/vb21_api_tests \
712	tests/vb21_common_tests \
713	tests/vb21_common2_tests \
714	tests/vb21_misc_tests \
715	tests/vb21_host_fw_preamble_tests \
716	tests/vb21_host_key_tests \
717	tests/vb21_host_keyblock_tests \
718	tests/vb21_host_misc_tests \
719	tests/vb21_host_sig_tests
720
721TEST_NAMES += ${TEST2X_NAMES} ${TEST20_NAMES} ${TEST21_NAMES}
722
723# And a few more...
724TLCL_TEST_NAMES = \
725	tests/tpm_lite/tpmtest_earlyextend \
726	tests/tpm_lite/tpmtest_earlynvram \
727        tests/tpm_lite/tpmtest_earlynvram2 \
728	tests/tpm_lite/tpmtest_enable \
729	tests/tpm_lite/tpmtest_fastenable \
730	tests/tpm_lite/tpmtest_globallock \
731        tests/tpm_lite/tpmtest_redefine_unowned \
732        tests/tpm_lite/tpmtest_spaceperm \
733	tests/tpm_lite/tpmtest_testsetup \
734	tests/tpm_lite/tpmtest_timing \
735        tests/tpm_lite/tpmtest_writelimit
736
737TEST_NAMES += ${TLCL_TEST_NAMES}
738
739# Finally
740TEST_BINS = $(addprefix ${BUILD}/,${TEST_NAMES})
741TEST_OBJS += $(addsuffix .o,${TEST_BINS})
742
743TEST2X_BINS = $(addprefix ${BUILD}/,${TEST2X_NAMES})
744TEST20_BINS = $(addprefix ${BUILD}/,${TEST20_NAMES})
745TEST21_BINS = $(addprefix ${BUILD}/,${TEST21_NAMES})
746
747# Directory containing test keys
748TEST_KEYS = ${SRC_RUN}/tests/testkeys
749
750
751##############################################################################
752# Finally, some targets. High-level ones first.
753
754# Create output directories if necessary.  Do this via explicit shell commands
755# so it happens before trying to generate/include dependencies.
756SUBDIRS := firmware host cgpt utility futility tests tests/tpm_lite
757_dir_create := $(foreach d, \
758	$(shell find ${SUBDIRS} -name '*.c' -exec  dirname {} \; | sort -u), \
759	$(shell [ -d ${BUILD}/${d} ] || mkdir -p ${BUILD}/${d}))
760
761
762# Default target.
763.PHONY: all
764all: fwlib fwlib2x fwlib20 fwlib21 \
765	$(if ${FIRMWARE_ARCH},,host_stuff) \
766	$(if ${COV},coverage)
767
768# Host targets
769.PHONY: host_stuff
770host_stuff: utillib hostlib cgpt utils futil tests utillib21
771
772.PHONY: clean
773clean:
774	${Q}/bin/rm -rf ${BUILD}
775
776.PHONY: install
777install: cgpt_install utils_install signing_install futil_install \
778	pc_files_install
779
780.PHONY: install_mtd
781install_mtd: install cgpt_wrapper_install
782
783.PHONY: install_for_test
784install_for_test: override DESTDIR = ${TEST_INSTALL_DIR}
785install_for_test: install
786
787# Don't delete intermediate object files
788.SECONDARY:
789
790# ----------------------------------------------------------------------------
791# Firmware library
792
793# TPM-specific flags.  These depend on the particular TPM we're targeting for.
794# They are needed here only for compiling parts of the firmware code into
795# user-level tests.
796
797# TPM_BLOCKING_CONTINUESELFTEST is defined if TPM_ContinueSelfTest blocks until
798# the self test has completed.
799
800${FWLIB_OBJS}: CFLAGS += -DTPM_BLOCKING_CONTINUESELFTEST
801
802# TPM_MANUAL_SELFTEST is defined if the self test must be started manually
803# (with a call to TPM_ContinueSelfTest) instead of starting automatically at
804# power on.
805#
806# We sincerely hope that TPM_BLOCKING_CONTINUESELFTEST and TPM_MANUAL_SELFTEST
807# are not both defined at the same time.  (See comment in code.)
808
809# CFLAGS += -DTPM_MANUAL_SELFTEST
810
811ifeq (${FIRMWARE_ARCH},i386)
812# Unrolling loops in cryptolib makes it faster
813${FWLIB_OBJS}: CFLAGS += -DUNROLL_LOOPS
814${FWLIB2X_OBJS}: CFLAGS += -DUNROLL_LOOPS
815${FWLIB20_OBJS}: CFLAGS += -DUNROLL_LOOPS
816${FWLIB21_OBJS}: CFLAGS += -DUNROLL_LOOPS
817
818# Workaround for coreboot on x86, which will power off asynchronously
819# without giving us a chance to react. This is not an example of the Right
820# Way to do things. See chrome-os-partner:7689, and the commit message
821# that made this change.
822${FWLIB_OBJS}: CFLAGS += -DSAVE_LOCALE_IMMEDIATELY
823
824# On x86 we don't actually read the GBB data into RAM until it is needed.
825# Therefore it makes sense to cache it rather than reading it each time.
826# Enable this feature.
827${FWLIB_OBJS}: CFLAGS += -DCOPY_BMP_DATA
828endif
829
830ifdef REGION_READ
831${FWLIB_OBJS}: CFLAGS += -DREGION_READ
832endif
833
834ifeq (${FIRMWARE_ARCH},)
835# Disable rollback TPM when compiling locally, since otherwise
836# load_kernel_test attempts to talk to the TPM.
837${FWLIB_OBJS}: CFLAGS += -DDISABLE_ROLLBACK_TPM
838endif
839
840${FWLIB20_OBJS}: INCLUDES += -Ifirmware/lib20/include
841${FWLIB21_OBJS}: INCLUDES += -Ifirmware/lib21/include
842
843# Linktest ensures firmware lib doesn't rely on outside libraries
844${BUILD}/firmware/linktest/main_vbinit: ${VBINIT_OBJS}
845${BUILD}/firmware/linktest/main_vbinit: OBJS = ${VBINIT_OBJS}
846TEST_OBJS += ${BUILD}/firmware/linktest/main_vbinit.o
847${BUILD}/firmware/linktest/main_vbsf: ${VBSF_OBJS}
848${BUILD}/firmware/linktest/main_vbsf: OBJS = ${VBSF_OBJS}
849TEST_OBJS += ${BUILD}/firmware/linktest/main_vbsf.o
850${BUILD}/firmware/linktest/main: ${FWLIB}
851${BUILD}/firmware/linktest/main: LIBS = ${FWLIB}
852TEST_OBJS += ${BUILD}/firmware/linktest/main.o
853
854.PHONY: fwlinktest
855fwlinktest: \
856	${BUILD}/firmware/linktest/main_vbinit \
857	${BUILD}/firmware/linktest/main_vbsf \
858	${BUILD}/firmware/linktest/main
859
860.PHONY: fwlib
861fwlib: $(if ${FIRMWARE_ARCH},${FWLIB},fwlinktest)
862
863${FWLIB}: ${FWLIB_OBJS}
864	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
865	${Q}rm -f $@
866	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
867	${Q}ar qc $@ $^
868
869.PHONY: fwlib2x
870fwlib2x: ${FWLIB2X}
871
872${FWLIB2X}: ${FWLIB2X_OBJS}
873	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
874	${Q}rm -f $@
875	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
876	${Q}ar qc $@ $^
877
878.PHONY: fwlib20
879fwlib20: ${FWLIB20}
880
881${FWLIB20}: ${FWLIB2X_OBJS} ${FWLIB20_OBJS}
882	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
883	${Q}rm -f $@
884	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
885	${Q}ar qc $@ $^
886
887.PHONY: fwlib21
888fwlib21: ${FWLIB21}
889
890${FWLIB21}: ${FWLIB2X_OBJS} ${FWLIB21_OBJS}
891	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
892	${Q}rm -f $@
893	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
894	${Q}ar qc $@ $^
895
896# ----------------------------------------------------------------------------
897# Host library(s)
898
899# Link tests for local utilities
900${BUILD}/host/linktest/main: ${UTILLIB}
901${BUILD}/host/linktest/main: LIBS = ${UTILLIB}
902TEST_OBJS += ${BUILD}/host/linktest/main.o
903
904.PHONY: utillib
905utillib: ${UTILLIB} \
906	${BUILD}/host/linktest/main
907
908# TODO: better way to make .a than duplicating this recipe each time?
909${UTILLIB}: ${UTILLIB_OBJS} ${FWLIB_OBJS}
910	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
911	${Q}rm -f $@
912	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
913	${Q}ar qc $@ $^
914
915.PHONY: utillib21
916utillib21: ${UTILLIB21}
917
918${UTILLIB21}: INCLUDES += -Ihost/lib21/include -Ifirmware/lib21/include
919${UTILLIB21}: ${UTILLIB21_OBJS} ${FWLIB2X_OBJS} ${FWLIB21_OBJS}
920	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
921	${Q}rm -f $@
922	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
923	${Q}ar qc $@ $^
924
925
926# Link tests for external repos
927${BUILD}/host/linktest/extern: ${HOSTLIB}
928${BUILD}/host/linktest/extern: LIBS = ${HOSTLIB}
929${BUILD}/host/linktest/extern: LDLIBS += -static
930TEST_OBJS += ${BUILD}/host/linktest/extern.o
931
932.PHONY: hostlib
933hostlib: ${HOSTLIB} \
934	${BUILD}/host/linktest/extern
935
936# TODO: better way to make .a than duplicating this recipe each time?
937${HOSTLIB}: ${HOSTLIB_OBJS}
938	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
939	${Q}rm -f $@
940	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
941	${Q}ar qc $@ $^
942
943
944# Ugh. This is a very cut-down version of HOSTLIB just for the installer.
945.PHONY: tinyhostlib
946tinyhostlib: ${TINYHOSTLIB}
947	${Q}cp -f ${TINYHOSTLIB} ${HOSTLIB}
948
949${TINYHOSTLIB}: ${TINYHOSTLIB_OBJS}
950	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
951	${Q}rm -f $@
952	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
953	${Q}ar qc $@ $^
954
955# ----------------------------------------------------------------------------
956# CGPT library and utility
957
958.PHONY: cgpt_wrapper
959cgpt_wrapper: ${CGPT_WRAPPER}
960
961${CGPT_WRAPPER}: ${CGPT_WRAPPER_OBJS} ${UTILLIB}
962	@$(PRINTF) "    LD            $(subst ${BUILD}/,,$@)\n"
963	${Q}${LD} -o ${CGPT_WRAPPER} ${CFLAGS} $^
964
965.PHONY: cgpt
966cgpt: ${CGPT} ${CGPT_WRAPPER}
967
968${CGPT}: LDLIBS += -luuid
969
970${CGPT}: ${CGPT_OBJS} ${UTILLIB}
971	@${PRINTF} "    LDcgpt        $(subst ${BUILD}/,,$@)\n"
972	${Q}${LD} -o ${CGPT} ${CFLAGS} ${LDFLAGS} $^ ${LDLIBS}
973
974.PHONY: cgpt_install
975cgpt_install: ${CGPT}
976	@${PRINTF} "    INSTALL       CGPT\n"
977	${Q}mkdir -p ${UB_DIR}
978	${Q}${INSTALL} -t ${UB_DIR} $^
979
980.PHONY: cgpt_wrapper_install
981cgpt_wrapper_install: cgpt_install ${CGPT_WRAPPER}
982	@$(PRINTF) "    INSTALL       cgpt_wrapper\n"
983	${Q}${INSTALL} -t ${UB_DIR} ${CGPT_WRAPPER}
984	${Q}mv ${UB_DIR}/$(notdir ${CGPT}) \
985		${UB_DIR}/$(notdir ${CGPT}).bin
986	${Q}mv ${UB_DIR}/$(notdir ${CGPT_WRAPPER}) \
987		${UB_DIR}/$(notdir ${CGPT})
988
989# ----------------------------------------------------------------------------
990# Utilities
991
992# These have their own headers too.
993${BUILD}/utility/%: INCLUDES += -Iutility/include
994
995${UTIL_BINS} ${UTIL_BINS_STATIC}: ${UTILLIB}
996${UTIL_BINS} ${UTIL_BINS_STATIC}: LIBS = ${UTILLIB}
997
998# Utilities for auto-update toolkits must be statically linked.
999${UTIL_BINS_STATIC}: LDFLAGS += -static
1000
1001
1002.PHONY: utils
1003utils: ${UTIL_BINS} ${UTIL_SCRIPTS}
1004	${Q}cp -f ${UTIL_SCRIPTS} ${BUILD}/utility
1005	${Q}chmod a+rx $(patsubst %,${BUILD}/%,${UTIL_SCRIPTS})
1006
1007.PHONY: utils_install
1008utils_install: ${UTIL_BINS} ${UTIL_SCRIPTS} ${UTIL_DEFAULTS}
1009	@${PRINTF} "    INSTALL       UTILS\n"
1010	${Q}mkdir -p ${UB_DIR}
1011	${Q}${INSTALL} -t ${UB_DIR} ${UTIL_BINS} ${UTIL_SCRIPTS}
1012	${Q}mkdir -p ${DF_DIR}
1013	${Q}${INSTALL} -t ${DF_DIR} -m 'u=rw,go=r,a-s' ${UTIL_DEFAULTS}
1014
1015# And some signing stuff for the target
1016.PHONY: signing_install
1017signing_install: ${SIGNING_SCRIPTS} ${SIGNING_SCRIPTS_DEV} ${SIGNING_COMMON}
1018	@${PRINTF} "    INSTALL       SIGNING\n"
1019	${Q}mkdir -p ${UB_DIR} ${VB_DIR}
1020	${Q}${INSTALL} -t ${UB_DIR} ${SIGNING_SCRIPTS}
1021	${Q}${INSTALL} -t ${VB_DIR} ${SIGNING_SCRIPTS_DEV}
1022	${Q}${INSTALL} -t ${VB_DIR} -m 'u=rw,go=r,a-s' ${SIGNING_COMMON}
1023
1024# ----------------------------------------------------------------------------
1025# new Firmware Utility
1026
1027.PHONY: futil
1028futil: ${FUTIL_STATIC_BIN} ${FUTIL_BIN}
1029
1030${FUTIL_STATIC_BIN}: ${FUTIL_STATIC_OBJS} ${UTILLIB}
1031	@${PRINTF} "    LD            $(subst ${BUILD}/,,$@)\n"
1032	${Q}${LD} -o $@ ${CFLAGS} ${LDFLAGS} -static $^ ${LDLIBS}
1033
1034${FUTIL_BIN}: LDLIBS += ${CRYPTO_LIBS}
1035${FUTIL_BIN}: ${FUTIL_OBJS} ${UTILLIB}
1036	@${PRINTF} "    LD            $(subst ${BUILD}/,,$@)\n"
1037	${Q}${LD} -o $@ ${CFLAGS} ${LDFLAGS} $^ ${LDLIBS}
1038
1039.PHONY: futil_install
1040futil_install: ${FUTIL_BIN} ${FUTIL_STATIC_BIN}
1041	@${PRINTF} "    INSTALL       futility\n"
1042	${Q}mkdir -p ${UB_DIR}
1043	${Q}${INSTALL} -t ${UB_DIR} ${FUTIL_BIN} ${FUTIL_STATIC_BIN}
1044	${Q}for prog in ${FUTIL_SYMLINKS}; do \
1045		ln -sf futility "${UB_DIR}/$$prog"; done
1046
1047# ----------------------------------------------------------------------------
1048# Utility to generate TLCL structure definition header file.
1049
1050${BUILD}/utility/tlcl_generator: CFLAGS += -fpack-struct
1051
1052STRUCTURES_TMP=${BUILD}/tlcl_structures.tmp
1053STRUCTURES_SRC=firmware/lib/tpm_lite/include/tlcl_structures.h
1054
1055.PHONY: update_tlcl_structures
1056update_tlcl_structures: ${BUILD}/utility/tlcl_generator
1057	@${PRINTF} "    Rebuilding TLCL structures\n"
1058	${Q}${BUILD}/utility/tlcl_generator > ${STRUCTURES_TMP}
1059	${Q}cmp -s ${STRUCTURES_TMP} ${STRUCTURES_SRC} || \
1060		( echo "%% Updating structures.h %%" && \
1061		  cp ${STRUCTURES_TMP} ${STRUCTURES_SRC} )
1062
1063# ----------------------------------------------------------------------------
1064# Tests
1065
1066.PHONY: tests
1067tests: ${TEST_BINS}
1068
1069${TEST_BINS}: ${UTILLIB} ${TESTLIB}
1070${TEST_BINS}: INCLUDES += -Itests
1071${TEST_BINS}: LIBS = ${TESTLIB} ${UTILLIB}
1072
1073${TEST2X_BINS}: ${FWLIB2X}
1074${TEST2X_BINS}: LIBS += ${FWLIB2X}
1075
1076${TEST20_BINS}: ${FWLIB20}
1077${TEST20_BINS}: INCLUDES += -Ifirmware/lib20/include
1078${TEST20_BINS}: LIBS += ${FWLIB20}
1079
1080${TEST21_BINS}: ${UTILLIB21}
1081${TEST21_BINS}: INCLUDES += -Ihost/lib21/include -Ifirmware/lib21/include
1082${TEST21_BINS}: LIBS += ${UTILLIB21}
1083
1084${TESTLIB}: ${TESTLIB_OBJS}
1085	@${PRINTF} "    RM            $(subst ${BUILD}/,,$@)\n"
1086	${Q}rm -f $@
1087	@${PRINTF} "    AR            $(subst ${BUILD}/,,$@)\n"
1088	${Q}ar qc $@ $^
1089
1090
1091# ----------------------------------------------------------------------------
1092# Generic build rules. LIBS and OBJS can be overridden to tweak the generic
1093# rules for specific targets.
1094
1095${BUILD}/%: ${BUILD}/%.o ${OBJS} ${LIBS}
1096	@${PRINTF} "    LD            $(subst ${BUILD}/,,$@)\n"
1097	${Q}${LD} -o $@ ${CFLAGS} ${LDFLAGS} $< ${OBJS} ${LIBS} ${LDLIBS}
1098
1099${BUILD}/%.o: %.c
1100	@${PRINTF} "    CC            $(subst ${BUILD}/,,$@)\n"
1101	${Q}${CC} ${CFLAGS} ${INCLUDES} -c -o $@ $<
1102
1103${BUILD}/%.o: ${BUILD}/%.c
1104	@${PRINTF} "    CC            $(subst ${BUILD}/,,$@)\n"
1105	${Q}${CC} ${CFLAGS} ${INCLUDES} -c -o $@ $<
1106
1107# Rules to recompile a single source file for library and test
1108# TODO: is there a tidier way to do this?
1109${BUILD}/%_for_lib.o: CFLAGS += -DFOR_LIBRARY
1110${BUILD}/%_for_lib.o: %.c
1111	@${PRINTF} "    CC-for-lib    $(subst ${BUILD}/,,$@)\n"
1112	${Q}${CC} ${CFLAGS} ${INCLUDES} -c -o $@ $<
1113
1114${BUILD}/%_for_test.o: CFLAGS += -DFOR_TEST
1115${BUILD}/%_for_test.o: %.c
1116	@${PRINTF} "    CC-for-test   $(subst ${BUILD}/,,$@)\n"
1117	${Q}${CC} ${CFLAGS} ${INCLUDES} -c -o $@ $<
1118
1119# TODO: C++ files don't belong in vboot reference at all.  Convert to C.
1120${BUILD}/%.o: %.cc
1121	@${PRINTF} "    CXX           $(subst ${BUILD}/,,$@)\n"
1122	${Q}${CXX} ${CFLAGS} ${INCLUDES} -c -o $@ $<
1123
1124# ----------------------------------------------------------------------------
1125# Here are the special tweaks to the generic rules.
1126
1127# Always create the defaults file, since it depends on input variables
1128.PHONY: ${UTIL_DEFAULTS}
1129${UTIL_DEFAULTS}:
1130	@${PRINTF} "    CREATE        $(subst ${BUILD}/,,$@)\n"
1131	${Q}rm -f $@
1132	${Q}mkdir -p $(dir $@)
1133	${Q}echo '# Generated file. Do not edit.' > $@.tmp
1134	${Q}echo "DEV_DEBUG_FORCE=${DEV_DEBUG_FORCE}" >> $@.tmp
1135	${Q}mv -f $@.tmp $@
1136
1137# Some utilities need external crypto functions
1138CRYPTO_LIBS := $(shell ${PKG_CONFIG} --libs libcrypto)
1139
1140${BUILD}/utility/dumpRSAPublicKey: LDLIBS += ${CRYPTO_LIBS}
1141${BUILD}/utility/pad_digest_utility: LDLIBS += ${CRYPTO_LIBS}
1142${BUILD}/utility/signature_digest_utility: LDLIBS += ${CRYPTO_LIBS}
1143
1144${BUILD}/host/linktest/main: LDLIBS += ${CRYPTO_LIBS}
1145${BUILD}/tests/vboot_common2_tests: LDLIBS += ${CRYPTO_LIBS}
1146${BUILD}/tests/vboot_common3_tests: LDLIBS += ${CRYPTO_LIBS}
1147${BUILD}/tests/vb20_common2_tests: LDLIBS += ${CRYPTO_LIBS}
1148${BUILD}/tests/vb20_common3_tests: LDLIBS += ${CRYPTO_LIBS}
1149${BUILD}/tests/verify_kernel: LDLIBS += ${CRYPTO_LIBS}
1150
1151${TEST21_BINS}: LDLIBS += ${CRYPTO_LIBS}
1152
1153LZMA_LIBS := $(shell ${PKG_CONFIG} --libs liblzma)
1154YAML_LIBS := $(shell ${PKG_CONFIG} --libs yaml-0.1)
1155
1156${BUILD}/utility/bmpblk_utility: LD = ${CXX}
1157${BUILD}/utility/bmpblk_utility: LDLIBS = ${LZMA_LIBS} ${YAML_LIBS}
1158
1159BMPBLK_UTILITY_DEPS = \
1160	${BUILD}/utility/bmpblk_util.o \
1161	${BUILD}/utility/image_types.o \
1162	${BUILD}/utility/eficompress_for_lib.o \
1163	${BUILD}/utility/efidecompress_for_lib.o
1164
1165${BUILD}/utility/bmpblk_utility: OBJS = ${BMPBLK_UTILITY_DEPS}
1166${BUILD}/utility/bmpblk_utility: ${BMPBLK_UTILITY_DEPS}
1167ALL_OBJS += ${BMPBLK_UTILITY_DEPS}
1168
1169${BUILD}/utility/bmpblk_font: OBJS += ${BUILD}/utility/image_types.o
1170${BUILD}/utility/bmpblk_font: ${BUILD}/utility/image_types.o
1171ALL_OBJS += ${BUILD}/utility/image_types.o
1172
1173# Allow multiple definitions, so tests can mock functions from other libraries
1174${BUILD}/tests/%: CFLAGS += -Xlinker --allow-multiple-definition
1175${BUILD}/tests/%: LDLIBS += -lrt -luuid
1176${BUILD}/tests/%: LIBS += ${TESTLIB}
1177
1178${BUILD}/tests/rollback_index2_tests: OBJS += \
1179	${BUILD}/firmware/lib/rollback_index_for_test.o
1180${BUILD}/tests/rollback_index2_tests: \
1181	${BUILD}/firmware/lib/rollback_index_for_test.o
1182TEST_OBJS += ${BUILD}/firmware/lib/rollback_index_for_test.o
1183
1184${BUILD}/tests/tlcl_tests: OBJS += \
1185	${BUILD}/firmware/lib/tpm_lite/tlcl_for_test.o
1186${BUILD}/tests/tlcl_tests: \
1187	${BUILD}/firmware/lib/tpm_lite/tlcl_for_test.o
1188TEST_OBJS += ${BUILD}/firmware/lib/tpm_lite/tlcl_for_test.o
1189
1190${BUILD}/tests/vboot_audio_tests: OBJS += \
1191	${BUILD}/firmware/lib/vboot_audio_for_test.o
1192${BUILD}/tests/vboot_audio_tests: \
1193	${BUILD}/firmware/lib/vboot_audio_for_test.o
1194TEST_OBJS += ${BUILD}/firmware/lib/vboot_audio_for_test.o
1195
1196TLCL_TEST_BINS = $(addprefix ${BUILD}/,${TLCL_TEST_NAMES})
1197${TLCL_TEST_BINS}: OBJS += ${BUILD}/tests/tpm_lite/tlcl_tests.o
1198${TLCL_TEST_BINS}: ${BUILD}/tests/tpm_lite/tlcl_tests.o
1199TEST_OBJS += ${BUILD}/tests/tpm_lite/tlcl_tests.o
1200
1201# ----------------------------------------------------------------------------
1202# Here are the special rules that don't fit in the generic rules.
1203
1204# Generates the list of commands defined in futility by running grep in the
1205# source files looking for the DECLARE_FUTIL_COMMAND() macro usage.
1206${FUTIL_STATIC_CMD_LIST}: ${FUTIL_STATIC_SRCS}
1207${FUTIL_CMD_LIST}: ${FUTIL_SRCS}
1208${FUTIL_CMD_LIST} ${FUTIL_STATIC_CMD_LIST}:
1209	@${PRINTF} "    GEN           $(subst ${BUILD}/,,$@)\n"
1210	${Q}rm -f $@ $@_t $@_commands
1211	${Q}mkdir -p ${BUILD}/gen
1212	${Q}grep -hoRE '^DECLARE_FUTIL_COMMAND\([^,]+' $^ \
1213		| sed 's/DECLARE_FUTIL_COMMAND(\(.*\)/_CMD(\1)/' \
1214		| sort >>$@_commands
1215	${Q}./scripts/getversion.sh >> $@_t
1216	${Q}echo '#define _CMD(NAME) extern const struct' \
1217		'futil_cmd_t __cmd_##NAME;' >> $@_t
1218	${Q}cat $@_commands >> $@_t
1219	${Q}echo '#undef _CMD' >> $@_t
1220	${Q}echo '#define _CMD(NAME) &__cmd_##NAME,' >> $@_t
1221	${Q}echo 'const struct futil_cmd_t *const futil_cmds[] = {' >> $@_t
1222	${Q}cat $@_commands >> $@_t
1223	${Q}echo '0};  /* null-terminated */' >> $@_t
1224	${Q}echo '#undef _CMD' >> $@_t
1225	${Q}mv $@_t $@
1226	${Q}rm -f $@_commands
1227
1228##############################################################################
1229# Targets that exist just to run tests
1230
1231# Frequently-run tests
1232.PHONY: test_targets
1233test_targets:: runcgpttests runmisctests run2tests
1234
1235ifeq (${MINIMAL},)
1236# Bitmap utility isn't compiled for minimal variant
1237test_targets:: runbmptests runfutiltests
1238# Scripts don't work under qemu testing
1239# TODO: convert scripts to makefile so they can be called directly
1240test_targets:: runtestscripts
1241endif
1242
1243.PHONY: test_setup
1244test_setup:: cgpt utils futil tests install_for_test
1245
1246# Qemu setup for cross-compiled tests.  Need to copy qemu binary into the
1247# sysroot.
1248ifneq (${QEMU_ARCH},)
1249test_setup:: qemu_install
1250
1251.PHONY: qemu_install
1252qemu_install:
1253ifeq (${SYSROOT},)
1254	$(error SYSROOT must be set to the top of the target-specific root \
1255when cross-compiling for qemu-based tests to run properly.)
1256endif
1257	@${PRINTF} "    Copying qemu binary.\n"
1258	${Q}cp -fu /usr/bin/${QEMU_BIN} ${BUILD}/${QEMU_BIN}
1259	${Q}chmod a+rx ${BUILD}/${QEMU_BIN}
1260endif
1261
1262.PHONY: runtests
1263runtests: test_setup test_targets
1264
1265# Generate test keys
1266.PHONY: genkeys
1267genkeys: utils test_setup
1268	tests/gen_test_keys.sh
1269
1270# Generate test cases for fuzzing
1271.PHONY: genfuzztestcases
1272genfuzztestcases: utils test_setup
1273	tests/gen_fuzz_test_cases.sh
1274
1275.PHONY: runbmptests
1276runbmptests: test_setup
1277	cd tests/bitmaps && BMPBLK=${BUILD_RUN}/utility/bmpblk_utility \
1278		./TestBmpBlock.py -v
1279
1280.PHONY: runcgpttests
1281runcgpttests: test_setup
1282	${RUNTEST} ${BUILD_RUN}/tests/cgptlib_test
1283
1284.PHONY: runtestscripts
1285runtestscripts: test_setup genfuzztestcases
1286	tests/load_kernel_tests.sh
1287	tests/run_cgpt_tests.sh ${BUILD_RUN}/cgpt/cgpt
1288	tests/run_cgpt_tests.sh ${BUILD_RUN}/cgpt/cgpt -D 358400
1289	tests/run_preamble_tests.sh
1290	tests/run_rsa_tests.sh
1291	tests/run_vbutil_kernel_arg_tests.sh
1292	tests/run_vbutil_tests.sh
1293	tests/vb2_rsa_tests.sh
1294	tests/vb2_firmware_tests.sh
1295
1296.PHONY: runmisctests
1297runmisctests: test_setup
1298	${RUNTEST} ${BUILD_RUN}/tests/rollback_index2_tests
1299	${RUNTEST} ${BUILD_RUN}/tests/rollback_index3_tests
1300	${RUNTEST} ${BUILD_RUN}/tests/rsa_utility_tests
1301	${RUNTEST} ${BUILD_RUN}/tests/sha_tests
1302	${RUNTEST} ${BUILD_RUN}/tests/stateful_util_tests
1303	${RUNTEST} ${BUILD_RUN}/tests/tlcl_tests
1304	${RUNTEST} ${BUILD_RUN}/tests/tpm_bootmode_tests
1305	${RUNTEST} ${BUILD_RUN}/tests/utility_string_tests
1306	${RUNTEST} ${BUILD_RUN}/tests/utility_tests
1307	${RUNTEST} ${BUILD_RUN}/tests/vboot_api_devmode_tests
1308	${RUNTEST} ${BUILD_RUN}/tests/vboot_api_firmware_tests
1309	${RUNTEST} ${BUILD_RUN}/tests/vboot_api_init_tests
1310	${RUNTEST} ${BUILD_RUN}/tests/vboot_api_kernel_tests
1311	${RUNTEST} ${BUILD_RUN}/tests/vboot_api_kernel2_tests
1312	${RUNTEST} ${BUILD_RUN}/tests/vboot_api_kernel3_tests
1313	${RUNTEST} ${BUILD_RUN}/tests/vboot_api_kernel4_tests
1314	${RUNTEST} ${BUILD_RUN}/tests/vboot_audio_tests
1315	${RUNTEST} ${BUILD_RUN}/tests/vboot_common_tests
1316	${RUNTEST} ${BUILD_RUN}/tests/vboot_common2_tests ${TEST_KEYS}
1317	${RUNTEST} ${BUILD_RUN}/tests/vboot_common3_tests ${TEST_KEYS}
1318	${RUNTEST} ${BUILD_RUN}/tests/vboot_display_tests
1319	${RUNTEST} ${BUILD_RUN}/tests/vboot_firmware_tests
1320	${RUNTEST} ${BUILD_RUN}/tests/vboot_kernel_tests
1321	${RUNTEST} ${BUILD_RUN}/tests/vboot_nvstorage_test
1322
1323.PHONY: run2tests
1324run2tests: test_setup
1325	${RUNTEST} ${BUILD_RUN}/tests/vb2_api_tests
1326	${RUNTEST} ${BUILD_RUN}/tests/vb2_common_tests
1327	${RUNTEST} ${BUILD_RUN}/tests/vb2_misc_tests
1328	${RUNTEST} ${BUILD_RUN}/tests/vb2_nvstorage_tests
1329	${RUNTEST} ${BUILD_RUN}/tests/vb2_rsa_utility_tests
1330	${RUNTEST} ${BUILD_RUN}/tests/vb2_secdata_tests
1331	${RUNTEST} ${BUILD_RUN}/tests/vb2_sha_tests
1332	${RUNTEST} ${BUILD_RUN}/tests/vb20_api_tests
1333	${RUNTEST} ${BUILD_RUN}/tests/vb20_common_tests
1334	${RUNTEST} ${BUILD_RUN}/tests/vb20_common2_tests ${TEST_KEYS}
1335	${RUNTEST} ${BUILD_RUN}/tests/vb20_common3_tests ${TEST_KEYS}
1336	${RUNTEST} ${BUILD_RUN}/tests/vb20_misc_tests
1337	${RUNTEST} ${BUILD_RUN}/tests/vb21_api_tests
1338	${RUNTEST} ${BUILD_RUN}/tests/vb21_common_tests
1339	${RUNTEST} ${BUILD_RUN}/tests/vb21_common2_tests ${TEST_KEYS}
1340	${RUNTEST} ${BUILD_RUN}/tests/vb21_misc_tests
1341	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_fw_preamble_tests ${TEST_KEYS}
1342	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_key_tests ${TEST_KEYS}
1343	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_keyblock_tests ${TEST_KEYS}
1344	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_misc_tests
1345	${RUNTEST} ${BUILD_RUN}/tests/vb21_host_sig_tests ${TEST_KEYS}
1346
1347.PHONY: runfutiltests
1348runfutiltests: test_setup
1349	tests/futility/run_test_scripts.sh ${TEST_INSTALL_DIR}/bin
1350	${RUNTEST} ${BUILD_RUN}/tests/futility/test_not_really
1351
1352# Run long tests, including all permutations of encryption keys (instead of
1353# just the ones we use) and tests of currently-unused code.
1354# Not run by automated build.
1355.PHONY: runlongtests
1356runlongtests: test_setup genkeys genfuzztestcases
1357	${RUNTEST} ${BUILD_RUN}/tests/vboot_common2_tests ${TEST_KEYS} --all
1358	${RUNTEST} ${BUILD_RUN}/tests/vboot_common3_tests ${TEST_KEYS} --all
1359	${RUNTEST} ${BUILD_RUN}/tests/vb20_common2_tests ${TEST_KEYS} --all
1360	${RUNTEST} ${BUILD_RUN}/tests/vb20_common3_tests ${TEST_KEYS} --all
1361	${RUNTEST} ${BUILD_RUN}/tests/vb21_common2_tests ${TEST_KEYS} --all
1362	tests/run_preamble_tests.sh --all
1363	tests/run_vbutil_tests.sh --all
1364
1365# TODO: There were a number of ancient tests that hadn't been run in years.
1366# They were removed with https://chromium-review.googlesource.com/#/c/214610/
1367# Some day it might be nice to see what they were supposed to do.
1368
1369.PHONY: runalltests
1370runalltests: runtests runfutiltests runlongtests
1371
1372# Code coverage
1373.PHONY: coverage_init
1374coverage_init: test_setup
1375	rm -f ${COV_INFO}*
1376	lcov -c -i -d . -b . -o ${COV_INFO}.initial
1377
1378.PHONY: coverage_html
1379coverage_html:
1380	lcov -c -d . -b . -o ${COV_INFO}.tests
1381	lcov -a ${COV_INFO}.initial -a ${COV_INFO}.tests -o ${COV_INFO}.total
1382	lcov -r ${COV_INFO}.total '/usr/*' '*/linktest/*' -o ${COV_INFO}.local
1383	genhtml ${COV_INFO}.local -o ${BUILD}/coverage
1384# Generate addtional coverage stats just for firmware subdir, because the stats
1385# for the whole project don't include subdirectory summaries. This will print
1386# the summary for just the firmware sources.
1387	lcov -r ${COV_INFO}.local '*/stub/*' -o ${COV_INFO}.nostub
1388	lcov -e ${COV_INFO}.nostub '${SRCDIR}/firmware/*' \
1389		-o ${COV_INFO}.firmware
1390
1391.PHONY: coverage
1392ifeq (${COV},)
1393coverage:
1394	$(error Build coverage like this: make clean && COV=1 make)
1395else
1396coverage: coverage_init runtests coverage_html
1397endif
1398
1399# Include generated dependencies
1400ALL_DEPS += ${ALL_OBJS:%.o=%.o.d}
1401TEST_DEPS += ${TEST_OBJS:%.o=%.o.d}
1402-include ${ALL_DEPS}
1403-include ${TEST_DEPS}
1404
1405# We want to use only relative paths in cscope.files, especially since the
1406# paths inside and outside the chroot are different.
1407SRCDIRPAT=$(subst /,\/,${SRCDIR}/)
1408
1409# Note: vboot 2.0 is deprecated, so don't index those files
1410${BUILD}/cscope.files: test_setup
1411	${Q}rm -f $@
1412	${Q}cat ${ALL_DEPS} | tr -d ':\\' | tr ' ' '\012' | \
1413		grep -v /lib20/ | \
1414		sed -e "s/${SRCDIRPAT}//" | \
1415		egrep '\.[chS]$$' | sort | uniq > $@
1416
1417cmd_etags = etags -o ${BUILD}/TAGS $(shell cat ${BUILD}/cscope.files)
1418cmd_ctags = ctags -o ${BUILD}/tags $(shell cat ${BUILD}/cscope.files)
1419run_if_prog = $(if $(shell which $(1) 2>/dev/null),$(2),)
1420
1421.PHONY: tags TAGS xrefs
1422tags TAGS xrefs: ${BUILD}/cscope.files
1423	${Q}\rm -f ${BUILD}/tags ${BUILD}/TAGS
1424	${Q}$(call run_if_prog,etags,${cmd_etags})
1425	${Q}$(call run_if_prog,ctags,${cmd_ctags})
1426
1427PC_FILES = ${PC_IN_FILES:%.pc.in=${BUILD}/%.pc}
1428${PC_FILES}: ${PC_IN_FILES}
1429	${Q}sed \
1430		-e 's:@LDLIBS@:${LDLIBS}:' \
1431		-e 's:@LIBDIR@:${LIBDIR}:' \
1432		$< > $@
1433
1434.PHONY: pc_files_install
1435pc_files_install: ${PC_FILES}
1436	${Q}mkdir -p ${ULP_DIR}
1437	${Q}${INSTALL} -D -m 0644 $< ${ULP_DIR}/$(notdir $<)
1438