Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
693 lines (614 loc) · 32.9 KB
/
Copy pathMakefile
File metadata and controls
693 lines (614 loc) · 32.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# falcon-boot: one repository, seven components, one non-recursive build.
#
# falcon-core/ EFI-free libraries every other component links: crypto,
# the filesystem drivers, LUKS2, GPT, the PE walker, the
# boot-prediction parsers, the manifest and the image
# readers. The core of the host test suite.
# falcon-efi/ the loader: a UEFI x64 PE32+ built by mingw cross-gcc,
# with no gnu-efi, no EDK2 and no libc
# falcon-cli/ host tools: falcon-boot-mkstick and falcon-boot-sign
# falcon-appliance/ the on-stick signing appliance: PID 1 and the recipe
# for the image it runs in
# falcon-winjump/ the WinPE jump binary, a Win64 console program
# falcon-hook/ the program appended to an ISO's initrd that rebuilds
# the ISO as a block device (freestanding Linux, built
# at two widths: x86-64 and i586)
# falcon-bios/ the opt-in BIOS loader: i586, flat 32-bit protected
# mode, three stages; not in `all` (`make bios`)
#
# Each component's build.mk lists its own sources and targets. This file holds
# what they share - toolchains, flag sets, the version stamp, the object
# layout, the test runner and the compile database - and includes them. There
# is one make invocation and one dependency graph; nothing recurses.
#
# Objects land in build/obj/<flavour>/<source path>.o, one flavour per way a
# source is compiled:
#
# efi-$(VARIANT) freestanding mingw, linked into the loader
# host hosted Linux, linked into the host tools
# winjump hosted mingw, linked into winjump.exe
# winjump32 hosted i686 mingw, linked into winjump32.exe
# hook, hook32 freestanding static Linux, x86-64 and i586: falcon-hook
# bios freestanding i586: falcon-bios's three stages
# test hosted Linux at -O0 with --coverage, linked into the tests
#
# The same falcon-core source is compiled in up to three flavours, which is
# the point of it: the loader, the tools and the tests run one implementation.
# Binaries land in build/bin/, fixtures and rig artifacts directly in build/.
#
# See docs/DESIGN.md for the architecture.
# ---- where the build runs ----
#
# In the harness container (tools/coverage/harness.sh), the toolchain CI
# runs. Outside it, this file reads no further: every goal is handed to the
# container, and a workstation needs docker and none of the toolchain.
# FALCON_HARNESS is set inside the container; FALCON_NATIVE=1 builds with
# the host's toolchain instead. Bash's completion parses the whole file
# natively, to list its targets.
#
# The host goals act on this machine: the container builds what each needs
# (falcon_needs.<goal>, checked against the build's own names below), then
# the goal runs here with those files taken as up to date.
FALCON_HOST_GOALS := install uninstall run run-secboot run-stick run-stick-secboot
falcon_needs.install := build/bin/falcon-boot-mkstick build/bin/falcon-boot-sign
falcon_needs.run := build/bin/falcon-boot.efi
falcon_needs.run-secboot := build/bin/falcon-boot-signed.efi
ifeq ($(FALCON_HARNESS)$(FALCON_NATIVE)$(__BASH_MAKE_COMPLETION__),)
# The flags as make parsed them (-j, -k, -n, the command line's variables),
# taken before a jobserver is attached that the container could not reach.
falcon_makeflags := $(MAKEFLAGS)
falcon_host := $(filter $(FALCON_HOST_GOALS),$(MAKECMDGOALS))
falcon_inner := $(filter-out $(FALCON_HOST_GOALS),$(MAKECMDGOALS)) \
$(foreach g,$(falcon_host),$(falcon_needs.$(g)))
.DEFAULT_GOAL := falcon-container
.PHONY: falcon-container $(MAKECMDGOALS)
# Once for all the goals. No goals at all is the inner make's default goal;
# host goals that need nothing built start no container.
ifneq ($(strip $(falcon_inner))$(if $(MAKECMDGOALS),,default),)
falcon-container:
+@MAKEFLAGS='$(subst ','\'',$(falcon_makeflags))' tools/coverage/harness.sh make $(falcon_inner)
else
falcon-container: ;
endif
$(filter-out $(FALCON_HOST_GOALS),$(MAKECMDGOALS)): falcon-container ; @:
$(falcon_host): falcon-container
+@$(MAKE) FALCON_NATIVE=1 $(addprefix -o ,$(falcon_needs.$@)) $@
else
# The last field of every version string. `release` is CI's on master
# alone (.gitlab-ci.yml); every other build is `dev`.
VARIANT ?= dev
# The component files below define targets before `all` is reached.
.DEFAULT_GOAL := all
# A recipe that fails after writing its target must not leave it behind.
# Several recipes check what they just built (falcon-bios's stage size and
# .bss guards) and exit 1; without this the rejected file stays, is newer
# than its inputs, and the next make passes without running the check.
.DELETE_ON_ERROR:
OBJ := build/obj
BIN := build/bin
# ---- the apps' images ----
#
# Named here, before any component is read, because the components' own rules
# (the rig, the trust fixtures) take them as prerequisites, and make expands a
# prerequisite when it reads the rule. The apps (apps/<app>/app.mk) link them
# under these names.
EFI := $(BIN)/falcon-boot.efi
EFI_SIGNED := $(BIN)/falcon-boot-signed.efi
EFI_STATION := $(BIN)/falcon-boot-station.efi
EFI_STATION_SIGNED := $(BIN)/falcon-boot-station-signed.efi
EFI_ESP_JUMP := $(BIN)/falcon-esp-jump.efi
EFI_ESP_JUMP_SIGNED := $(BIN)/falcon-esp-jump-signed.efi
EFI_AA64 := $(BIN)/falcon-boot-aa64.efi
EFI_AA64_SIGNED := $(BIN)/falcon-boot-aa64-signed.efi
EFI_STATION_AA64 := $(BIN)/falcon-boot-station-aa64.efi
EFI_ESP_JUMP_AA64 := $(BIN)/falcon-esp-jump-aa64.efi
BIOS_STAGE3 := $(BIN)/falcon-bios-stage3.bin
BIOS_STATION_STAGE3 := $(BIN)/falcon-bios-station-stage3.bin
BIOS_STATION_LEGACY_STAGE3 := $(BIN)/falcon-bios-station-legacy-stage3.bin
BIOS_JUMP_STAGE3 := $(BIN)/falcon-bios-jump-stage3.bin
JUMP_IMG := $(BIN)/falcon-jump.img
# ---- include roots ----
#
# One -I per component src/. A translation unit sees its own component and
# the components it is built against, never the rest: the UEFI loader
# efi+core, falcon-bios bios+core, and so on. A module directory name is
# unique within every include set a build uses, which is what lets
# <fs/exfat.h> mean one file wherever it is written; falcon-efi and
# falcon-bios never share a set, so both may have a pos/ or a win/, and the
# two halves of one route carry one name. tools/analyze.sh fails the gate on
# a name that appears twice within a set.
CORE_INC := -Ifalcon-core/src
EFI_INC := -Ifalcon-efi/src
CLI_INC := -Ifalcon-cli/src
APPLIANCE_INC := -Ifalcon-appliance/src
HOOK_INC := -Ifalcon-hook/src
BIOS_INC := -Ifalcon-bios/src
WINJUMP_INC := -Ifalcon-winjump/src
# ---- the freestanding EFI flavour ----
CC := x86_64-w64-mingw32-gcc
# Freestanding EFI codegen: no red zone (firmware interrupts land on our
# stack), no stack protector or probe (no runtime to service either), and
# -fshort-wchar so L"..." literals are the 16-bit CHAR16 the spec mandates.
# -mno-stack-arg-probe drops mingw's __chkstk_ms prologue call, which the
# directory walk's multi-page frames would otherwise pull libgcc in for:
# firmware hands us a fully committed stack, so there is no guard page to
# probe for, and -Wstack-usage is what actually polices frame size here.
# The warning set is the repo standard; -Wstack-usage guards the recursive
# directory walk, whose frames each carry a ~4.5 KB exfat_dir.
EFI_CFLAGS := -std=c11 -O2 -ffreestanding \
-fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone \
-mno-stack-arg-probe \
-Wall -Wextra -Wpedantic -Wshadow -Wunused -Wdouble-promotion \
-Wformat=2 -Wmissing-prototypes -Wstrict-prototypes \
-Wmissing-declarations -Wcast-align -Wcast-qual -Wnull-dereference \
-Wconversion -Wsign-conversion \
-Wstack-usage=32768 \
$(EFI_INC) $(CORE_INC) -Ibuild
# Subsystem 10 is EFI_APPLICATION. -shared is how mingw emits a PE with no
# CRT startup; --no-insert-timestamp keeps the output reproducible. -s drops
# the COFF symbol table ld would append after the last section: nothing
# reads it (no firmware, no debugger here), and sbsign takes those bytes for
# a gap between sections and warns on every image it signs.
EFI_LDFLAGS := -nostdlib -shared -Wl,--subsystem,10 -e efi_main \
-Wl,--no-insert-timestamp -s
EFI_OBJ := $(OBJ)/efi-$(VARIANT)
$(EFI_OBJ)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(EFI_CFLAGS) -MMD -MF $@.d -c -o $@ $<
# ---- the freestanding EFI flavour, AArch64 ----
#
# The same images for AArch64 UEFI, by llvm-mingw's clang: its driver takes
# the mingw link line above unchanged. AArch64 has no red zone to switch
# off, and clang spells the frame limit -Wframe-larger-than. Each app links
# here as <image>-aa64.efi. A source that is x86 only keeps its x86 body
# behind __x86_64__ and builds an equivalent, or a stub never reached,
# under __aarch64__, so the apps' source lists serve both.
CC_AA64 := aarch64-w64-mingw32-clang
OBJDUMP_AA64 := aarch64-w64-mingw32-objdump
EFI_AA64_CFLAGS := $(filter-out -mno-red-zone -Wstack-usage=%,$(EFI_CFLAGS)) \
-Wframe-larger-than=32768
# lld names the subsystem where GNU ld takes its number.
EFI_AA64_LDFLAGS := -nostdlib -shared -Wl,--subsystem,efi_application \
-e efi_main -Wl,--no-insert-timestamp -s
EFI_AA64_OBJ := $(OBJ)/efi-aa64-$(VARIANT)
# Sources with no AArch64 body at all, left out of every aa64 link: the
# x86 SIMD block functions, whose callers reach them only behind an x86
# guard (falcon-core/src/crypto/argon2.c, xts.c).
EFI_X86_ONLY := $(addprefix falcon-core/src/crypto/,aesni.c argon2_sse2.c \
argon2_ssse3.c argon2_avx2.c)
$(EFI_AA64_OBJ)/%.o: %.c
@mkdir -p $(@D)
$(CC_AA64) $(EFI_AA64_CFLAGS) -MMD -MF $@.d -c -o $@ $<
# ---- the hosted Linux flavour ----
#
# Ordinary Linux userland C, built by the HOST compiler, and no part of the
# EFI link: it is allowed a libc, it is allowed to fork and exec, and it must
# never end up inside the loader. Same warning set as the freestanding build,
# adapted for a hosted target: the stack protector and stack-clash protection
# are ON here (there is a runtime to service them and this code parses user
# input), where EFI-land has to switch them off.
CC_HOST ?= gcc
HOST_CFLAGS := -std=c11 -O2 -D_GNU_SOURCE \
-Wall -Wextra -Wpedantic -Wshadow -Wunused -Wdouble-promotion \
-Wformat=2 -Wformat-truncation \
-Wmissing-prototypes -Wstrict-prototypes \
-Wmissing-declarations -Wcast-align -Wcast-qual -Wnull-dereference \
-Wconversion -Wsign-conversion \
-fstack-protector-strong -fstack-clash-protection \
$(APPLIANCE_INC) $(CLI_INC) $(CORE_INC) -I.
HOST_LDFLAGS := -fstack-protector-strong
HOST_OBJ := $(OBJ)/host
$(HOST_OBJ)/%.o: %.c
@mkdir -p $(@D)
$(CC_HOST) $(HOST_CFLAGS) -MMD -MF $@.d -c -o $@ $<
# ---- the hosted Linux flavour, AArch64 ----
#
# The same userland for an arm64 machine, linked static: the appliance's
# PID 1 and sign tool in the AArch64 appliance, and the release's arm64
# host tools. clang and ld.lld (llvm-mingw's in the harness) against
# Debian's arm64 libc and libgcc, installed as a foreign architecture; the
# cross gcc conflicts with gcc-multilib.
CC_HOST_A64 ?= clang --target=aarch64-linux-gnu
LD_HOST_A64 := $(CC_HOST_A64) -fuse-ld=lld
HOST_A64_CFLAGS := $(HOST_CFLAGS)
HOST_A64_OBJ := $(OBJ)/host-a64
$(HOST_A64_OBJ)/%.o: %.c
@mkdir -p $(@D)
$(CC_HOST_A64) $(HOST_A64_CFLAGS) -MMD -MF $@.d -c -o $@ $<
# AES-NI intrinsics, this object only, in both flavours: -maes lets gcc emit
# those instructions anywhere in the TU, so nothing else may carry it. The
# CPUID probe that gates this path lives in crypto.c, built with the baseline
# flags, precisely so it cannot fault on the CPUs it exists to detect.
$(EFI_OBJ)/falcon-core/src/crypto/aesni.o: EFI_CFLAGS += -maes
$(HOST_OBJ)/falcon-core/src/crypto/aesni.o: HOST_CFLAGS += -maes
# Argon2id's block function, one object per instruction set
# (falcon-core/src/crypto/argon2_simd.h), on the same rule as AES-NI: each
# carries its own level's flag and nothing else does, and crypto.c's
# crypto_simd_level, built without them, decides which runs. SSE2 needs no
# flag on x86-64. The AVX2 object is assembled with every aligned vector
# move made unaligned in the EFI build: GCC for Windows targets cannot
# realign the stack to 32 bytes (PR 54412), and a ymm spill to a slot that
# is not would #GP in the firmware.
ARGON2_SIMD_SSSE3 := falcon-core/src/crypto/argon2_ssse3.o
ARGON2_SIMD_AVX2 := falcon-core/src/crypto/argon2_avx2.o
$(EFI_OBJ)/$(ARGON2_SIMD_SSSE3): EFI_CFLAGS += -mssse3
$(EFI_OBJ)/$(ARGON2_SIMD_AVX2): EFI_CFLAGS += -mavx2 -Wa,-muse-unaligned-vector-move
$(HOST_OBJ)/$(ARGON2_SIMD_SSSE3): HOST_CFLAGS += -mssse3
$(HOST_OBJ)/$(ARGON2_SIMD_AVX2): HOST_CFLAGS += -mavx2
# ---- version ----
SEMVER := $(shell cat release_version)
# Repo-standard full version: SEMVER_YYYYMMDD.HHMM.TYPE.
BUILD_STAMP := $(shell date +%Y%m%d.%H%M)
VERSION := $(SEMVER)_$(BUILD_STAMP).$(VARIANT)
# The stationary build (docs/DESIGN.md Section 12) and falcon jump (Section
# 13) name themselves apart, so a screen photo says which loader is running.
VERSION_STATION := $(SEMVER)_$(BUILD_STAMP).station-$(VARIANT)
VERSION_STATION_LEGACY := $(SEMVER)_$(BUILD_STAMP).station-legacy-$(VARIANT)
VERSION_JUMP := $(SEMVER)_$(BUILD_STAMP).jump-$(VARIANT)
# The version string carries a build timestamp, so it is recomputed on every
# make; the header is only rewritten when the string actually changed, which
# keeps an unchanged rebuild from relinking the world. It is a header rather
# than a -D because an -D with embedded quotes cannot survive a round trip
# through compile_commands.json.
version-stamp:
@mkdir -p build
@printf '/* generated by make; do not edit */\n#define FALCON_SEMVER "%s"\n#define FALCON_VERSION "%s"\n#define FALCON_VERSION_STATION "%s"\n#define FALCON_VERSION_STATION_LEGACY "%s"\n#define FALCON_VERSION_JUMP "%s"\n' \
'$(SEMVER)' '$(VERSION)' '$(VERSION_STATION)' '$(VERSION_STATION_LEGACY)' '$(VERSION_JUMP)' > build/version.h.tmp
@cmp -s build/version.h.tmp build/version.h || \
mv build/version.h.tmp build/version.h
@rm -f build/version.h.tmp
# Empty recipe: the file is produced by version-stamp, and not touching it
# here is what lets an unchanged version leave main.o alone.
build/version.h: version-stamp ;
# ---- the test flavour ----
#
# Every unit under test is EFI-free and reaches the outside world only through
# callbacks or caller-supplied buffers, so each compiles and runs unchanged in
# a host process; that is the whole reason they are shaped that way
# (docs/DESIGN.md Section 9). Each component's build.mk names its
# test sources and the units they exercise; every test binary also links the
# shared support in falcon-core/tests/support.
#
# falcon-core/src/lib/string.c is never compiled here. It defines memcpy and
# friends for the freestanding build, which would collide with glibc's; the
# hostshim directory, first on the include path, answers <lib/string.h> with
# the real <string.h> plus verbatim copies of the get_le* inlines.
# Override to test against images built somewhere else. Each fixture group
# skips itself cleanly when its image is absent, so a fresh checkout can run
# the suite before anyone has been asked for a sudo password.
FIXTURE_IMG ?= $(CURDIR)/build/test-exfat.img
EXT4_IMG ?= $(CURDIR)/build/test-ext4.img
EXT4_MANIFEST ?= $(CURDIR)/build/test-ext4.manifest
EXT4_BIGFILE ?= $(CURDIR)/build/test-ext4.bigfile
EXT4_NOCSUM_IMG ?= $(CURDIR)/build/test-ext4-nocsum.img
ISOVIEW_IMG ?= $(CURDIR)/build/test-isoview.iso
LUKS_IMG ?= $(CURDIR)/build/test-luks.img
LUKS_ARGON_IMG ?= $(CURDIR)/build/test-luks-argon.img
LUKS_MANIFEST ?= $(CURDIR)/build/test-luks.manifest
LUKS_REF ?= $(CURDIR)/build/test-luks.ref64k
TRUST_DIR ?= $(CURDIR)/build/test-trust
SHIM_DIR ?= $(CURDIR)/build/shim/bins
MEDIA_FIXTURE_DIR ?= $(CURDIR)/build
# The repo's standard warning set, minus the flags that only mean something
# for the freestanding EFI target (-ffreestanding, -fshort-wchar,
# -mno-red-zone, -mno-stack-arg-probe) and minus -Wstack-usage, which polices
# the loader's recursive walk rather than a test binary.
TEST_WARNS := -Wall -Wextra -Wpedantic -Wshadow -Wunused -Wunused-function \
-Wunused-variable -Wunused-parameter -Wunused-result \
-Wdouble-promotion -Wformat=2 -Wformat-truncation \
-Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations \
-Wcast-align -Wcast-qual -Wnull-dereference -Wconversion \
-Wsign-conversion
CMOCKA_CFLAGS := $(shell pkg-config --cflags cmocka 2>/dev/null)
CMOCKA_LIBS := $(shell pkg-config --libs cmocka 2>/dev/null)
TEST_SUPPORT := falcon-core/tests/support
# -D_POSIX_C_SOURCE because -std=c11 hides pread(); -O0 and --coverage because
# gcov line counts are only meaningful without the optimiser moving code.
TEST_CFLAGS := -std=c11 -O0 -g --coverage $(TEST_WARNS) \
-D_POSIX_C_SOURCE=200809L -D_FILE_OFFSET_BITS=64 \
-DFIXTURE_IMG='"$(FIXTURE_IMG)"' \
-DEXT4_IMG='"$(EXT4_IMG)"' \
-DEXT4_NOCSUM_IMG='"$(EXT4_NOCSUM_IMG)"' \
-DISOVIEW_IMG='"$(ISOVIEW_IMG)"' \
-DEXT4_MANIFEST='"$(EXT4_MANIFEST)"' \
-DEXT4_BIGFILE='"$(EXT4_BIGFILE)"' \
-DLUKS_IMG='"$(LUKS_IMG)"' \
-DLUKS_ARGON_IMG='"$(LUKS_ARGON_IMG)"' \
-DLUKS_MANIFEST='"$(LUKS_MANIFEST)"' \
-DLUKS_REF='"$(LUKS_REF)"' \
-DTRUST_DIR='"$(TRUST_DIR)"' \
-DSHIM_DIR='"$(SHIM_DIR)"' \
-DMEDIA_FIXTURE_DIR='"$(MEDIA_FIXTURE_DIR)"'
# A test object sees the include set its component builds with, not every
# component's at once. With one shared set, a header name two components both
# use would resolve to whichever came first on the line, and a BIOS test could
# compile against the UEFI loader's header without a word. The set is chosen
# by the source's top directory, so a unit linked into another component's
# test binary (cli's sign/ into the appliance's) keeps its own. hostshim comes
# first because it is how <lib/string.h> means libc in a hosted build.
TEST_INC_falcon-core := $(CORE_INC)
TEST_INC_falcon-efi := $(EFI_INC) $(CORE_INC)
TEST_INC_falcon-cli := $(CLI_INC) $(CORE_INC)
TEST_INC_falcon-appliance := $(APPLIANCE_INC) $(CLI_INC) $(CORE_INC)
TEST_INC_falcon-hook := $(HOOK_INC) $(CORE_INC)
TEST_INC_falcon-bios := $(BIOS_INC) $(CORE_INC)
TEST_INC_falcon-winjump := $(WINJUMP_INC) $(CORE_INC)
# $(call test_inc,<source path>)
test_inc = -I$(TEST_SUPPORT)/hostshim \
$(or $(TEST_INC_$(firstword $(subst /, ,$(1)))),\
$(error no test include set for $(1): add a TEST_INC_ line to the Makefile)) \
-I$(TEST_SUPPORT) $(CMOCKA_CFLAGS)
TEST_LDFLAGS := --coverage
TEST_LIBS := $(CMOCKA_LIBS)
TEST_OBJ := $(OBJ)/test
# argon2_test.c runs the lanes on pthreads, and plain gcov counters race
# there: a lost update came out of gcov as a negative branch count (GCC
# 68080), which gcovr refuses the whole report over. Atomic counters cost
# the suite about 60% when every unit has them, so only what those threads
# execute does.
TEST_THREADED := $(addprefix $(TEST_OBJ)/falcon-core/,src/crypto/lanes.o \
src/crypto/argon2.o src/crypto/argon2_sse2.o src/crypto/argon2_ssse3.o \
src/crypto/argon2_avx2.o src/crypto/blake2b.o tests/argon2_test.o)
$(TEST_THREADED): TEST_CFLAGS += -fprofile-update=atomic
$(TEST_OBJ)/%.o: %.c
@mkdir -p $(@D)
$(CC_HOST) $(TEST_CFLAGS) $(call test_inc,$<) -MMD -MF $@.d -c -o $@ $<
TEST_SUPPORT_SRCS := $(TEST_SUPPORT)/support.c $(TEST_SUPPORT)/synth.c
# Links one component's test binary: $(call test_bin,<name>,<sources>).
define test_bin
$(BIN)/falcon-$(1)-test: $$(patsubst %.c,$(TEST_OBJ)/%.o,$(2) $(TEST_SUPPORT_SRCS))
@mkdir -p $$(@D)
$(CC_HOST) $(TEST_LDFLAGS) -o $$@ $$^ $(TEST_LIBS)
endef
# ---- the components ----
include falcon-core/build.mk
# Before the loader: it embeds the hook and the WinPE jump binary, and its
# source list is expanded as it is read.
include falcon-hook/build.mk
include falcon-winjump/build.mk
include falcon-vdisk/build.mk
include falcon-efi/build.mk
include falcon-cli/build.mk
include falcon-appliance/build.mk
# The BIOS image is opt-in (`make bios`, not `all`): a BIOS install changes
# what a stick can reach (DESIGN Section 3.3). Its host-testable C is in
# `test` like everything else.
include falcon-bios/build.mk
# The loaders, each one app (apps/<app>/app.mk) composed from the platform
# modules above. After every component, because an app names their modules.
APPS := stick station esp-jump jump station-legacy
include $(APPS:%=apps/%/app.mk)
# The release artifacts, from what the components and apps build.
include release.mk
TESTS := core efi cli appliance hook bios winjump
TEST_BINS := $(TESTS:%=$(BIN)/falcon-%-test)
# The coverage report's units: what the test binaries link as units under
# test, less the two linked only as dependencies (util.c and payload.c; see
# falcon-cli/build.mk), plus the override arithmetic, which is a header its
# test includes. Derived from the components' own lists, so a unit added to a
# build.mk is reported without anyone remembering to edit a regex.
COV_UNITS := $(filter-out %/sign/util.c %/sign/payload.c,$(sort $(ALL_UUT_SRCS))) \
falcon-efi/src/iso/overlay.h
cov_empty :=
cov_space := $(cov_empty) $(cov_empty)
COV_FILTER := $(subst $(cov_space),|,$(patsubst %,%$$,$(subst .,\.,$(COV_UNITS))))
.PHONY: all test test-junit coverage test-fixtures check-tools analyze \
version-stamp clean
all: $(EFI) aa64 cli winjump compile_commands.json
# Runs every component's tests and reports all failures before failing.
# Stale counters from an earlier run would inflate the coverage report, so
# they go first.
test: $(TEST_BINS)
@find $(TEST_OBJ) -name '*.gcda' -delete 2>/dev/null || true
@rc=0; for t in $(TEST_BINS); do $$t || rc=1; done; exit $$rc
# The same run with cmocka writing one JUnit file per group instead of its
# usual output, for the pipeline's test report. cmocka's XML mode replaces
# stdout, so this is its own target and `make test` stays readable.
JUNIT_DIR := build/junit
test-junit: $(TEST_BINS)
@rm -rf $(JUNIT_DIR) && mkdir -p $(JUNIT_DIR)
@find $(TEST_OBJ) -name '*.gcda' -delete 2>/dev/null || true
@rc=0; for t in $(TEST_BINS); do \
CMOCKA_MESSAGE_OUTPUT=xml \
CMOCKA_XML_FILE=$(JUNIT_DIR)/$$(basename $$t)-%g.xml $$t || rc=1; \
done; exit $$rc
# One report over all four binaries: a unit two of them link (the manifest is
# read by both the efi and cli suites) accumulates both runs' counts in its
# one .gcda, which is the number that means something.
#
# Strict, unlike `test`. The image-backed suites skip when their fixtures are
# absent and a skip is not a failure, which is right for a machine that cannot
# build them (most need mkfs and a loop mount, so sudo) and wrong here:
# without them, falcon-core/src/fs/ext4.c, falcon-core/src/luks and
# falcon-core/src/luks/af.c sit at zero coverage while the overall figure
# looks fine. `make test-fixtures` builds them.
#
# suspicious-hits: the corruption sweeps call the crypto inner loops hundreds
# of millions of times, and gcovr treats any count past 2^32 as the symptom of
# a known gcov bug (GCC 68080) and refuses the whole report. Here the counts
# are real, so the threshold is raised rather than the check disabled:
# ignoring the error instead makes gcovr report those lines as UNCOVERED.
# The check arrived in gcovr 8.0; the harness image's 7.2 has neither it nor
# the option, so the option is passed only to a gcovr that knows it.
#
# COV_MIN, when set, is the line coverage the report must reach; the pipeline
# sets it and also takes the Cobertura file for the merge request's diff view.
COV_XML := build/cov/coverage.xml
COV_HITS := $(if $(shell gcovr --help 2>/dev/null | grep -e --gcov-suspicious-hits-threshold),\
--gcov-suspicious-hits-threshold 1000000000000)
coverage: export FALCON_TEST_STRICT = 1
coverage: test
@echo
@mkdir -p $(dir $(COV_XML))
@gcovr --root . $(TEST_OBJ) \
$(COV_HITS) \
--filter '$(COV_FILTER)' --txt --print-summary \
--cobertura $(COV_XML) \
$(if $(COV_MIN),--fail-under-line $(COV_MIN))
# The image-backed fixtures, together.
#
# These exist because the drivers they feed are from-scratch readers of
# formats someone else writes, and the only evidence worth having is that they
# agree with the implementation that produced the bytes: mkfs.exfat, mkfs.ext4
# and cryptsetup. Most need passwordless sudo for a loop mount, which is why
# they are a separate target rather than a prerequisite of `make test`.
#
# The cost of that choice, which is what this target is for: the suites that
# read these images SKIP when the images are absent, and a skipped cmocka
# suite does not fail. Without this target, a clean checkout runs its tests,
# reports green, and leaves the ext4 and LUKS readers at zero coverage.
test-fixtures: test-image test-ext4 test-jbd2 test-fat test-btrfs test-xfs test-luks test-isoview test-trust \
test-iso9660 test-udf test-wim test-uefiboot test-biosboot
# One fixture at a time, under -j too. mk-test-jbd2.sh snapshots a mounted
# filesystem whose committed metadata must not have reached its home blocks
# yet, and the other fixture scripts run a plain `sync`, which writes every
# filesystem's home; one running beside it leaves a jbd2 image with nothing to
# replay, and test_jbd2_fixtures_meaningful fails. (GNU make 4.4: with
# prerequisites, .NOTPARALLEL serialises only those.)
.NOTPARALLEL: test-fixtures
# Verify the toolchain before attempting a build. The EFI target needs the
# mingw cross-gcc that emits PE32+ directly; the test rig and the host tooling
# need the userland utilities we shell out to.
check-tools:
@tools/deps.sh check
# tools/deps.tsv is the one list: the Dockerfile's packages and the Manual's
# table are checked against it, so the inventory cannot drift three ways.
check-deps:
@tools/deps.sh sync
.PHONY: check-tools check-deps
# Static analysis gate: cppcheck + gcc -fanalyzer + clang-tidy, baseline
# managed in tools/analyze-baseline.txt (see tools/analyze.md).
# Built first: cppcheck reads compile_commands.json, whose entries include the
# generated blob sources (build/gen/*_blob.c). Without them it loads no
# project, checks nothing and reports zero findings, which is how the gate
# passed for a week without running cppcheck at all.
analyze: all bios check-deps $(COMPDB_AA64)
tools/analyze.sh
# ---- compile_commands.json ----
#
# gcc has no -MJ, so the database is assembled here, one list of
# source:object pairs per flavour. The command recorded is
# character-for-character the one the pattern rule runs, so clangd sees the
# real flags (and infers the mingw target from the compiler name).
#
# Each source appears ONCE. falcon-core is compiled in several flavours, and a
# database with two entries for one file would make every consumer analyse it
# twice: tools/analyze.sh treats a repeated finding as a new one, so the
# duplicate would show up as a regression in code nobody touched. So core
# sources are entered under the loader's flags, and only what the loader does
# not build gets a host entry. Test sources are entered under the test flags;
# they live outside every src/ directory, which is how tools/analyze.sh leaves
# them out of the gate.
CC_DB_EFI := $(foreach s,$(sort $(EFI_APP_SRCS)) $(PAYLOAD_SRC) $(STANDIN_SRC) $(RELAUNCH_SRC) $(TPM_PROBE_SRC),$(s):$(EFI_OBJ)/$(s:.c=.o))
CC_DB_HOST := $(foreach s,$(filter-out $(EFI_APP_SRCS),$(sort $(HOST_SRCS))),$(s):$(HOST_OBJ)/$(s:.c=.o))
CC_DB_WINJUMP := $(foreach s,$(WINJUMP_SRCS),$(s):$(WINJUMP_OBJ)/$(s:.c=.o))
CC_DB_TEST := $(foreach s,$(sort $(ALL_TEST_SRCS) $(TEST_SUPPORT_SRCS)),$(s):$(TEST_OBJ)/$(s:.c=.o))
# Test sources are entered one component at a time, each under the include
# set the pattern rule gives it, so clangd resolves a test's headers the way
# the compiler does.
CC_DB_TEST_COMPONENTS := $(sort $(foreach s,$(CC_DB_TEST),$(firstword $(subst /, ,$(s)))))
# A case arm ends in ')', which inside $(foreach ...) would close the call.
cc_rparen := )
CC_DB_TEST_INC = case "$$src" in \
$(foreach c,$(CC_DB_TEST_COMPONENTS),$(c)/*$(cc_rparen) inc=' $(strip $(call test_inc,$(c)/))';;) \
esac;
# falcon-bios's own C, the apps' BIOS entry points, and any falcon-core no
# other flavour builds (jump's FAT-only registry). The rest of the core it
# links is already entered under the loader's flags; without
# it, a component's worth of C would sit outside cppcheck, -fanalyzer and
# clang-tidy, which is not allowed to happen.
CC_DB_BIOS := $(foreach s,$(filter-out $(EFI_APP_SRCS) $(HOST_SRCS),$(sort $(BIOS_CSRCS))),$(s):$(BIOS_OBJ)/$(s:.c=.o))
# falcon-bios's assembly, under the flags its pattern rule assembles with, so
# an editor resolves biosdefs.h and the macros it defines. The analyzers are
# C tools and take .c entries only (tools/analyze.sh). jmod.ld.S is a linker
# script run through cpp, not assembly, and is left out.
CC_DB_BIOS_ASM := $(foreach s,$(sort $(filter-out %.ld.S,$(shell find falcon-bios -name '*.S'))),$(s):$(BIOS_OBJ)/$(s:.S=.o))
# falcon-hook's and falcon-vdisk's own C, each under its own freestanding
# flags; the core they link is entered above. vdisk is entered at x64, the
# width whose DDK headers every mingw ships.
CC_DB_HOOK := $(foreach s,$(filter-out $(EFI_APP_SRCS) $(HOST_SRCS) $(BIOS_CSRCS),$(sort $(HOOK_SRCS))),$(s):$(HOOK_OBJ)/$(s:.c=.o))
CC_DB_VDISK := $(VDISK_SRC):$(OBJ)/vdisk-x64/vdisk.o
# The flags as the JSON string's content, through the single-quoted shell
# word printf is given them in. A flag that carries quotes of its own (the
# tests' -DTRUST_DIR='"..."') closed that word, and the shell took both sets
# off, so the database recorded a bare path where the compiler is given a
# string, and clang read the path as identifiers. Double quotes become \"
# for JSON; single quotes become '\'' to survive the shell. What a reader
# then splits back (clang, analyze.sh's shlex) is the flag the build uses.
cc_db_sq := '\''
cc_db_q = $(subst ',$(cc_db_sq),$(subst ",\",$(1)))
# $(call cc_db_emit,<entries>,<compiler>,<flags>[,<shell setting inc per entry>])
define cc_db_emit
@for entry in $(1); do \
src=$${entry%%:*}; \
obj=$${entry##*:}; \
extra=""; \
inc=""; \
$(4) \
case "$$obj" in */crypto/aesni.o) extra=" -maes";; \
*/crypto/argon2_ssse3.o) extra=" -mssse3";; \
*/crypto/argon2_avx2.o) extra=" -mavx2";; esac; \
printf ' {\n' >> $@; \
printf ' "directory": "%s",\n' '$(CURDIR)' >> $@; \
printf ' "command": "%s %s%s%s -MMD -MF %s.d -c -o %s %s/%s",\n' \
'$(2)' '$(call cc_db_q,$(strip $(3)))' "$$inc" "$$extra" "$$obj" "$$obj" \
'$(CURDIR)' "$$src" >> $@; \
printf ' "file": "%s/%s"\n' '$(CURDIR)' "$$src" >> $@; \
printf ' },\n' >> $@; \
done
endef
# The database names the mingw include directory of the machine that wrote
# it, the harness's, which a workstation building in the container does not
# have. falcon-vdisk is the one component whose entries read mingw's own
# headers (the DDK); .clangd points the editor at this copy of them, links
# followed.
EDITOR_MINGW := build/editor/mingw-include
ifneq ($(VDISK_X64_DDK),)
all: $(EDITOR_MINGW)/.stamp
$(EDITOR_MINGW)/.stamp: $(VDISK_X64_DDK)/ntddk.h
rm -rf $(EDITOR_MINGW)
mkdir -p $(EDITOR_MINGW)
cp -rL $(VDISK_X64_DDK)/../. $(EDITOR_MINGW)/
touch $@
endif
# `make compdb` for people; the file target is what everything depends on.
compdb: compile_commands.json
.PHONY: compdb
compile_commands.json: Makefile $(wildcard falcon-*/build.mk apps/*/app.mk)
@echo '[' > $@
$(call cc_db_emit,$(CC_DB_EFI),$(CC),$(EFI_CFLAGS))
$(call cc_db_emit,$(CC_DB_BIOS),$(CC_HOST),$(BIOS_CFLAGS))
$(call cc_db_emit,$(CC_DB_BIOS_ASM),$(CC_HOST),$(BIOS_ASFLAGS))
$(call cc_db_emit,$(CC_DB_HOST),$(CC_HOST),$(HOST_CFLAGS))
$(call cc_db_emit,$(CC_DB_WINJUMP),$(CC),$(WINJUMP_CFLAGS))
$(call cc_db_emit,$(CC_DB_HOOK),$(CC_HOST),$(HOOK_CFLAGS) -ffunction-sections -fdata-sections)
$(call cc_db_emit,$(CC_DB_VDISK),x86_64-w64-mingw32-gcc,$(VDISK_CFLAGS) $(VDISK_X64_INC))
$(call cc_db_emit,$(CC_DB_TEST),$(CC_HOST),$(TEST_CFLAGS),$(CC_DB_TEST_INC))
@sed -i '$$ s/,$$//' $@
@echo ']' >> $@
# The AArch64 flavour's database, for the analyzer alone (tools/analyze.sh,
# tidy): the loader sources with an architecture branch, built as the aa64
# images build them, and the ARM64 winjump. The editor's database stays the
# x86-64 one; a file has one entry there. The target is spelled out, as
# llvm-mingw's wrapper adds it and clang-tidy runs no wrapper; it finds
# llvm-mingw's headers through the -isystem, as WINJUMP_SYSINC does gcc's.
COMPDB_AA64 := build/compdb-aa64/compile_commands.json
CC_DB_AA64_SRCS = $(shell grep -lE '__aarch64__|__x86_64__|EFI_ARCH_' \
$(filter-out $(EFI_X86_ONLY),$(sort $(EFI_APP_SRCS))))
LLVM_MINGW_A64_INC = $(shell d=$$(command -v $(CC_AA64)) && \
echo "-isystem $${d%/bin/*}/aarch64-w64-mingw32/include")
$(COMPDB_AA64): Makefile $(wildcard falcon-*/build.mk apps/*/app.mk)
@mkdir -p $(@D)
@echo '[' > $@
$(call cc_db_emit,$(foreach s,$(CC_DB_AA64_SRCS),$(s):$(EFI_AA64_OBJ)/$(s:.c=.o)),$(CC_AA64),--target=aarch64-w64-mingw32 $(EFI_AA64_CFLAGS))
$(call cc_db_emit,$(foreach s,$(WINJUMP_SRCS),$(s):$(WINJUMP_A64_OBJ)/$(s:.c=.o)),$(CC_WINJUMP_A64),--target=aarch64-w64-mingw32 $(WINJUMP_A64_CFLAGS) $(LLVM_MINGW_A64_INC))
@sed -i '$$ s/,$$//' $@
@echo ']' >> $@
clean:
rm -rf build compile_commands.json
# The host goals' needs, named before this file's build was read.
ifneq ($(falcon_needs.install) $(falcon_needs.run) $(falcon_needs.run-secboot),$(MKSTICK) $(SIGN) $(EFI) $(EFI_SIGNED))
$(error falcon_needs.* no longer name $(MKSTICK) $(SIGN) $(EFI) $(EFI_SIGNED))
endif
-include $(shell find $(OBJ) -name '*.o.d' 2>/dev/null)
endif