2016-08-02 23:25:47 +00:00
modules :=
pwd := $( shell pwd )
packages := $( pwd ) /packages
build := $( pwd ) /build
config := $( pwd ) /build
2016-12-28 17:45:12 +00:00
INSTALL := $( pwd ) /install
2016-08-02 23:25:47 +00:00
2016-11-23 17:11:08 +00:00
# Currently supported targets are x230, chell and qemu
2016-12-01 19:03:55 +00:00
BOARD ?= qemu
2016-08-14 20:02:15 +00:00
2016-12-29 23:23:08 +00:00
# If musl-libc is being used in the initrd, set the heads_cc
# variable to point to it.
musl_dep := musl
heads_cc := $( INSTALL) /bin/musl-gcc
2016-11-29 16:19:48 +00:00
all : $( BOARD ) .rom
# Disable all built in rules
.SUFFIXES :
2016-08-02 23:25:47 +00:00
2016-08-03 12:40:51 +00:00
2016-11-23 17:11:08 +00:00
# Bring in all of the module definitions;
# these are the external pieces that will be downloaded and built
# as part of creating the Heads firmware image.
2016-08-03 12:40:51 +00:00
i n c l u d e m o d u l e s / *
2016-08-02 23:25:47 +00:00
2016-11-29 16:19:48 +00:00
# These will be built via their intermediate targets
# This increases the build time, so it is commented out for now
#all: $(foreach m,$(modules),$m.intermediate)
2016-08-02 23:25:47 +00:00
d e f i n e prefix =
$( foreach _ , $ 2, $ 1$ _ )
e n d e f
2016-09-10 21:36:36 +00:00
d e f i n e bins =
2016-08-03 12:40:51 +00:00
$( foreach m ,$ 1,$ ( call prefix ,$ ( build ) /$ ( $ m_dir ) /,$ ( $ m_output ) ) )
2016-08-02 23:25:47 +00:00
e n d e f
2016-09-10 21:36:36 +00:00
d e f i n e libs =
$( foreach m ,$ 1,$ ( call prefix ,$ ( build ) /$ ( $ m_dir ) /,$ ( $ m_libraries ) ) )
e n d e f
d e f i n e outputs =
$( foreach m ,$ 1,\
$( call bins,$m ) \
$( call libs,$m ) \
)
e n d e f
2016-08-02 23:25:47 +00:00
#
# Generate the targets for a module.
#
# Special variables like $@ must be written as $$@ to avoid
# expansion during the first evaluation.
#
d e f i n e define_module =
2016-08-19 15:31:07 +00:00
ifneq ( " $( $1 _repo) " ,"" )
# Checkout the tree instead and touch the canary file so that we know
# that the files are all present. No signature hashes are checked in
# this case, since we don't have a stable version to compare against.
$( build) /$( $1 _dir) /.canary:
git clone " $( $1 _repo) " " $( build) / $( $1 _dir) "
touch " $$ @ "
else
# Fetch and verify the source tar file
$( packages) /$( $1 _tar) :
2016-08-02 23:25:47 +00:00
wget -O " $$ @ " $( $1 _url)
2016-08-19 15:31:07 +00:00
$( packages) /.$1 _verify: $( packages) /$( $1 _tar)
2017-01-27 20:47:08 +00:00
echo " $( $1 _hash) $$ ^ " | sha256sum --check -
2016-08-02 23:25:47 +00:00
touch " $$ @ "
2016-08-19 15:31:07 +00:00
# Unpack the tar file and touch the canary so that we know
# that the files are all present
$( build) /$( $1 _dir) /.canary: $( packages) /.$1 _verify
2016-08-03 12:52:23 +00:00
tar -xf " $( packages) / $( $1 _tar) " -C " $( build) "
2016-08-03 22:10:44 +00:00
if [ -r patches/$1 -$( $1 _version) .patch ] ; then \
2016-12-29 23:23:08 +00:00
( cd $( build) /$( $1 _dir) ; patch -p1 ) \
< patches/$1 -$( $1 _version) .patch; \
2016-08-03 22:10:44 +00:00
fi
2016-08-02 23:25:47 +00:00
touch " $$ @ "
2016-08-19 15:31:07 +00:00
endif
2016-08-02 23:25:47 +00:00
2016-12-01 19:03:55 +00:00
ifeq " $( $1 _config) " ""
# There is no official .config file
2016-11-29 16:19:48 +00:00
$( build) /$( $1 _dir) /.config: $( build) /$( $1 _dir) /.canary
touch " $$ @ "
2016-12-01 19:03:55 +00:00
else
# Copy the stored config file into the unpacked directory
$( build) /$( $1 _dir) /.config: config/$( $1 _config) $( build) /$( $1 _dir) /.canary
cp -a " $$ < " " $$ @ "
2016-11-29 16:19:48 +00:00
endif
2016-12-13 18:10:21 +00:00
2016-08-02 23:25:47 +00:00
# Use the module's configure variable to build itself
$( build) /$( $1 _dir) /.configured: \
$( build) /$( $1 _dir) /.canary \
$( build) /$( $1 _dir) /.config
cd " $( build) / $( $1 _dir) " ; $( $1 _configure)
touch " $$ @ "
2016-08-03 01:23:18 +00:00
# Build the target after any dependencies
2016-11-29 16:19:48 +00:00
$( call outputs,$1 ) : $1 .intermediate
2016-08-03 01:23:18 +00:00
# Short hand target for the module
2016-11-29 16:19:48 +00:00
#$1: $(call outputs,$1)
2016-08-02 23:25:47 +00:00
2016-11-29 16:19:48 +00:00
# Target for all of the outputs, which depend on their dependent modules
2016-12-28 17:45:12 +00:00
$1 .intermediate: \
$( foreach d,$( $1 _depends) ,$( call outputs,$d ) ) \
2016-12-29 23:23:08 +00:00
$( foreach d,$( $1 _depends) ,$d .intermediate) \
2016-12-28 17:45:12 +00:00
$( build) /$( $1 _dir) /.configured
2016-11-29 16:19:48 +00:00
make -C " $( build) / $( $1 _dir) " $( $1 _target)
.INTERMEDIATE : $1.intermediate
2016-08-02 23:25:47 +00:00
e n d e f
$( foreach _ , $ ( modules ) , $ ( eval $ ( call define_module ,$ _ ) ) )
2016-07-25 14:08:53 +00:00
2017-01-04 15:31:27 +00:00
initrd_lib_dir := initrd/lib
2016-09-10 21:36:36 +00:00
initrd_bin_dir := initrd/bin
#
# Install a file into the initrd, if it changed from
# the destination file.
#
d e f i n e install =
2016-09-26 18:55:48 +00:00
cmp --quiet " $1 " " $2 " || \
cp -a " $1 " " $2 "
2016-09-10 21:36:36 +00:00
e n d e f
2016-08-03 01:23:18 +00:00
#
# Files that should be copied into the initrd
# THis should probably be done in a more scalable manner
#
2016-09-10 21:36:36 +00:00
d e f i n e initrd_bin_add =
$(initrd_bin_dir)/$(notdir $1) : $1
2016-09-26 18:55:48 +00:00
@if [ ! -d " $( initrd_bin_dir) " ] ; \
then mkdir -p " $( initrd_bin_dir) " ; \
fi
$( call install,$$ <,$$ @)
2016-09-10 21:36:36 +00:00
initrd_bins += $( initrd_bin_dir) /$( notdir $1 )
2016-08-03 01:23:18 +00:00
e n d e f
2016-09-10 21:36:36 +00:00
d e f i n e initrd_lib_add =
$(initrd_lib_dir)/$(notdir $1) : $1
2016-09-26 18:55:48 +00:00
@if [ ! -d " $( initrd_lib_dir) " ] ; \
then mkdir -p " $( initrd_lib_dir) " ; \
fi
$( call install,$$ <,$$ @)
2016-09-10 21:36:36 +00:00
initrd_libs += $( initrd_lib_dir) /$( notdir $1 )
e n d e f
$( foreach _ , $ ( call bins ,kexec ) , $ ( eval $ ( call initrd_bin_add ,$ _ ) ) )
$( foreach _ , $ ( call bins ,tpmtotp ) , $ ( eval $ ( call initrd_bin_add ,$ _ ) ) )
2016-12-01 19:03:55 +00:00
$( foreach _ , $ ( call bins ,cryptsetup ) , $ ( eval $ ( call initrd_bin_add ,$ _ ) ) )
2016-12-28 17:45:12 +00:00
$( foreach _ , $ ( call bins ,gpg ) , $ ( eval $ ( call initrd_bin_add ,$ _ ) ) )
$( foreach _ , $ ( call bins ,lvm 2) , $ ( eval $ ( call initrd_bin_add ,$ _ ) ) )
2016-09-10 21:36:36 +00:00
$( foreach _ , $ ( call libs ,tpmtotp ) , $ ( eval $ ( call initrd_lib_add ,$ _ ) ) )
$( foreach _ , $ ( call libs ,mbedtls ) , $ ( eval $ ( call initrd_lib_add ,$ _ ) ) )
$( foreach _ , $ ( call libs ,qrencode ) , $ ( eval $ ( call initrd_lib_add ,$ _ ) ) )
2016-12-28 17:45:12 +00:00
$( foreach _ , $ ( call libs ,lvm 2) , $ ( eval $ ( call initrd_lib_add ,$ _ ) ) )
2016-09-10 21:36:36 +00:00
2016-08-06 21:13:22 +00:00
#$(foreach _, $(call outputs,xen), $(eval $(call initrd_bin,$_)))
2016-08-03 01:23:18 +00:00
# hack to install busybox into the initrd
initrd_bins += initrd/bin/busybox
initrd/bin/busybox : $( build ) /$( busybox_dir ) /busybox
cmp --quiet " $@ " " $^ " || \
make \
-C $( build) /$( busybox_dir) \
2016-12-29 23:23:08 +00:00
CC = " $( heads_cc) " \
2016-08-03 01:23:18 +00:00
CONFIG_PREFIX = " $( pwd ) /initrd " \
2016-12-29 23:23:08 +00:00
-j 8 \
2016-08-03 01:23:18 +00:00
install
2016-08-04 21:38:00 +00:00
# hack to build cbmem from coreboot
2016-12-29 23:23:08 +00:00
# this must be built *AFTER* musl
2016-08-04 21:38:00 +00:00
initrd_bins += initrd/bin/cbmem
initrd/bin/cbmem : $( build ) /$( coreboot_dir ) /util /cbmem /cbmem
cmp --quiet " $^ " " $@ " \
|| cp " $^ " " $@ "
2016-12-29 23:23:08 +00:00
$(build)/$(coreboot_dir)/util/cbmem/cbmem : \
$( build) /$( coreboot_dir) /.canary \
musl.intermediate
make -C " $( dir $@ ) " CC = " $( heads_cc) "
2016-08-04 21:38:00 +00:00
2016-08-03 01:23:18 +00:00
# Update all of the libraries in the initrd based on the executables
# that were installed.
2016-09-10 21:36:36 +00:00
initrd_lib_install : $( initrd_bins ) $( initrd_libs )
-find initrd/bin -type f -a ! -name '*.sh' -print0 \
2016-08-06 21:13:22 +00:00
| xargs -0 strip
2016-12-28 17:45:12 +00:00
LD_LIBRARY_PATH = " $( INSTALL) /lib " \
2016-08-03 01:23:18 +00:00
./populate-lib \
2017-01-04 15:31:27 +00:00
$( initrd_lib_dir) \
2016-08-03 01:23:18 +00:00
initrd/bin/* \
initrd/sbin/* \
2017-01-04 15:31:27 +00:00
-strip $( initrd_lib_dir) /* ; true
2016-08-03 01:23:18 +00:00
#
# initrd image creation
#
# The initrd is constructed from various bits and pieces
2016-11-23 17:11:08 +00:00
# The cpio-clean program is used ensure that the files
# always have the same timestamp and appear in the same order.
2016-08-03 01:23:18 +00:00
#
2016-11-23 17:11:08 +00:00
# If there is no /dev/console, initrd can't startup.
2016-08-03 14:47:48 +00:00
# We have to force it to be included into the cpio image.
2016-11-23 17:11:08 +00:00
# Since we are picking up the system's /dev/console, there
# is a chance the build will not be reproducible (although
# unlikely that their device file has a different major/minor)
2016-08-03 01:23:18 +00:00
#
#
2016-09-10 21:36:36 +00:00
initrd.cpio : $( initrd_bins ) $( initrd_libs ) initrd_lib_install
2016-08-03 14:47:48 +00:00
cd ./initrd ; \
( \
echo "/dev" ; \
echo "/dev/console" ; \
find . \
) \
| cpio --quiet -H newc -o \
2016-08-14 20:02:15 +00:00
| ../cpio-clean \
2016-12-13 18:10:21 +00:00
> " ../ $@ .tmp "
2016-08-03 01:23:18 +00:00
if ! cmp --quiet " $@ " " $@ .tmp " ; then \
mv " $@ .tmp " " $@ " ; \
else \
echo " $@ : Unchanged " ; \
rm " $@ .tmp " ; \
fi
2016-11-29 16:19:48 +00:00
initrd.intermediate : initrd .cpio
2016-12-13 18:10:21 +00:00
2016-08-03 01:23:18 +00:00
2016-08-14 20:02:15 +00:00
# populate the coreboot initrd image from the one we built.
# 4.4 doesn't allow this, but building from head does.
2016-08-03 01:23:18 +00:00
$(call outputs,linux) : initrd .cpio
2016-08-14 20:02:15 +00:00
#$(call outputs,coreboot): $(build)/$(coreboot_dir)/initrd.cpio.xz
$(build)/$(coreboot_dir)/initrd.cpio.xz : initrd .cpio
2016-09-09 17:26:52 +00:00
xz --extreme < " $< " > " $@ "
2016-08-03 01:59:14 +00:00
# hack for the coreboot to find the linux kernel
$(build)/$(coreboot_dir)/bzImage : $( call outputs ,linux )
2016-08-03 12:40:51 +00:00
cmp --quiet " $@ " " $^ " || \
cp -a " $^ " " $@ "
$(call outputs,coreboot) : $( build ) /$( coreboot_dir ) /bzImage
2016-08-06 21:13:22 +00:00
2016-12-13 17:02:35 +00:00
# The coreboot gcc won't work for us since it doesn't have libc
2016-08-06 21:13:22 +00:00
#XGCC := $(build)/$(coreboot_dir)/util/crossgcc/xgcc/
#export CC := $(XGCC)/bin/x86_64-elf-gcc
#export LDFLAGS := -L/lib/x86_64-linux-gnu
2016-11-23 17:11:08 +00:00
x230.rom : $( build ) /$( coreboot_dir ) /x 230/coreboot .rom
2016-08-14 20:02:15 +00:00
dd if = " $< " of = " $@ " bs = 1M skip = 8
2016-11-23 17:11:08 +00:00
qemu.rom : $( build ) /$( coreboot_dir ) /qemu /coreboot .rom
cp -a " $< " " $@ "
2017-01-04 15:31:27 +00:00
clean-modules :
for dir in busybox-1.25.0 cryptsetup-1.7.3 gnupg-1.4.21 kexec-tools-2.0.12 libuuid-1.0.3 LVM2.2.02.168 mbedtls-2.3.0 popt-1.16 qrencode-3.4.4 tpmtotp-git ; do \
make -C build/$$ dir clean; \
rm build/$$ dir/.configured; \
done