1a0509109SArd Biesheuvel# SPDX-License-Identifier: GPL-2.0
2a0509109SArd Biesheuvel
3a0509109SArd Biesheuvel# to be include'd by arch/$(ARCH)/boot/Makefile after setting
4538bc0f4SArd Biesheuvel# EFI_ZBOOT_PAYLOAD, EFI_ZBOOT_BFD_TARGET, EFI_ZBOOT_MACH_TYPE and
5538bc0f4SArd Biesheuvel# EFI_ZBOOT_FORWARD_CFI
6a0509109SArd Biesheuvel
7bca2f3a9SArd Biesheuvelquiet_cmd_copy_and_pad = PAD     $@
8bca2f3a9SArd Biesheuvel      cmd_copy_and_pad = cp $< $@ && \
9bca2f3a9SArd Biesheuvel			 truncate -s $(shell hexdump -s16 -n4 -e '"%u"' $<) $@
10bca2f3a9SArd Biesheuvel
11bca2f3a9SArd Biesheuvel# Pad the file to the size of the uncompressed image in memory, including BSS
12bca2f3a9SArd Biesheuvel$(obj)/vmlinux.bin: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE
13bca2f3a9SArd Biesheuvel	$(call if_changed,copy_and_pad)
14bca2f3a9SArd Biesheuvel
15a0509109SArd Biesheuvelcomp-type-$(CONFIG_KERNEL_GZIP)		:= gzip
16a0509109SArd Biesheuvelcomp-type-$(CONFIG_KERNEL_LZ4)		:= lz4
17a0509109SArd Biesheuvelcomp-type-$(CONFIG_KERNEL_LZMA)		:= lzma
18a0509109SArd Biesheuvelcomp-type-$(CONFIG_KERNEL_LZO)		:= lzo
19a0509109SArd Biesheuvelcomp-type-$(CONFIG_KERNEL_XZ)		:= xzkern
20a0509109SArd Biesheuvelcomp-type-$(CONFIG_KERNEL_ZSTD)		:= zstd22
21a0509109SArd Biesheuvel
22bca2f3a9SArd Biesheuvel# in GZIP, the appended le32 carrying the uncompressed size is part of the
23bca2f3a9SArd Biesheuvel# format, but in other cases, we just append it at the end for convenience,
24bca2f3a9SArd Biesheuvel# causing the original tools to complain when checking image integrity.
25bca2f3a9SArd Biesheuvel# So disregard it when calculating the payload size in the zimage header.
26bca2f3a9SArd Biesheuvelzboot-method-y                         := $(comp-type-y)_with_size
27026b8579SArd Biesheuvelzboot-size-len-y                       := 4
28a0509109SArd Biesheuvel
29bca2f3a9SArd Biesheuvelzboot-method-$(CONFIG_KERNEL_GZIP)     := gzip
30026b8579SArd Biesheuvelzboot-size-len-$(CONFIG_KERNEL_GZIP)   := 0
31bca2f3a9SArd Biesheuvel
32bca2f3a9SArd Biesheuvel$(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
33026b8579SArd Biesheuvel	$(call if_changed,$(zboot-method-y))
34a0509109SArd Biesheuvel
35*095aabe3SArd Biesheuvel# avoid eager evaluation to prevent references to non-existent build artifacts
36*095aabe3SArd BiesheuvelOBJCOPYFLAGS_vmlinuz.o = -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
37a0509109SArd Biesheuvel			  --rename-section .data=.gzdata,load,alloc,readonly,contents
38a0509109SArd Biesheuvel$(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
39a0509109SArd Biesheuvel	$(call if_changed,objcopy)
40a0509109SArd Biesheuvel
41538bc0f4SArd Biesheuvelaflags-zboot-header-$(EFI_ZBOOT_FORWARD_CFI) := \
42538bc0f4SArd Biesheuvel		-DPE_DLL_CHAR_EX=IMAGE_DLLCHARACTERISTICS_EX_FORWARD_CFI_COMPAT
43538bc0f4SArd Biesheuvel
44a0509109SArd BiesheuvelAFLAGS_zboot-header.o += -DMACHINE_TYPE=IMAGE_FILE_MACHINE_$(EFI_ZBOOT_MACH_TYPE) \
45a0509109SArd Biesheuvel			 -DZBOOT_EFI_PATH="\"$(realpath $(obj)/vmlinuz.efi.elf)\"" \
46bca2f3a9SArd Biesheuvel			 -DZBOOT_SIZE_LEN=$(zboot-size-len-y) \
47538bc0f4SArd Biesheuvel			 -DCOMP_TYPE="\"$(comp-type-y)\"" \
48538bc0f4SArd Biesheuvel			 $(aflags-zboot-header-y)
49a0509109SArd Biesheuvel
50a0509109SArd Biesheuvel$(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FORCE
51a0509109SArd Biesheuvel	$(call if_changed_rule,as_o_S)
52a0509109SArd Biesheuvel
53a0509109SArd BiesheuvelZBOOT_DEPS := $(obj)/zboot-header.o $(objtree)/drivers/firmware/efi/libstub/lib.a
54a0509109SArd Biesheuvel
55a0509109SArd BiesheuvelLDFLAGS_vmlinuz.efi.elf := -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds
56a0509109SArd Biesheuvel$(obj)/vmlinuz.efi.elf: $(obj)/vmlinuz.o $(ZBOOT_DEPS) FORCE
57a0509109SArd Biesheuvel	$(call if_changed,ld)
58a0509109SArd Biesheuvel
59f57fb375SArd BiesheuvelOBJCOPYFLAGS_vmlinuz.efi := -O binary
60f57fb375SArd Biesheuvel$(obj)/vmlinuz.efi: $(obj)/vmlinuz.efi.elf FORCE
61a0509109SArd Biesheuvel	$(call if_changed,objcopy)
62a0509109SArd Biesheuvel
63bca2f3a9SArd Biesheuveltargets += zboot-header.o vmlinux.bin vmlinuz vmlinuz.o vmlinuz.efi.elf vmlinuz.efi
64