1a0509109SArd Biesheuvel# SPDX-License-Identifier: GPL-2.0
2a0509109SArd Biesheuvel
3a0509109SArd Biesheuvel# to be include'd by arch/$(ARCH)/boot/Makefile after setting
4*538bc0f4SArd Biesheuvel# EFI_ZBOOT_PAYLOAD, EFI_ZBOOT_BFD_TARGET, EFI_ZBOOT_MACH_TYPE and
5*538bc0f4SArd 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
27bca2f3a9SArd Biesheuvelzboot-size-len-y                       := 12
28a0509109SArd Biesheuvel
29bca2f3a9SArd Biesheuvelzboot-method-$(CONFIG_KERNEL_GZIP)     := gzip
30bca2f3a9SArd Biesheuvelzboot-size-len-$(CONFIG_KERNEL_GZIP)   := 8
31bca2f3a9SArd Biesheuvel
32bca2f3a9SArd Biesheuvel# Copy the SizeOfHeaders and SizeOfCode fields from the payload to the end of
33bca2f3a9SArd Biesheuvel# the compressed image. Note that this presupposes a PE header offset of 64
34bca2f3a9SArd Biesheuvel# bytes, which is what arm64, RISC-V and LoongArch use.
35bca2f3a9SArd Biesheuvelquiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
36bca2f3a9SArd Biesheuvel      cmd_compwithsize = $(cmd_$(zboot-method-y)) && ( \
37bca2f3a9SArd Biesheuvel			   dd status=none if=$< bs=4 count=1 skip=37 ; \
38bca2f3a9SArd Biesheuvel			   dd status=none if=$< bs=4 count=1 skip=23 ) >> $@
39bca2f3a9SArd Biesheuvel
40bca2f3a9SArd Biesheuvel$(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
411f1ba325SArd Biesheuvel	$(call if_changed,compwithsize)
42a0509109SArd Biesheuvel
43a0509109SArd BiesheuvelOBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) \
44a0509109SArd Biesheuvel			  --rename-section .data=.gzdata,load,alloc,readonly,contents
45a0509109SArd Biesheuvel$(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
46a0509109SArd Biesheuvel	$(call if_changed,objcopy)
47a0509109SArd Biesheuvel
48*538bc0f4SArd Biesheuvelaflags-zboot-header-$(EFI_ZBOOT_FORWARD_CFI) := \
49*538bc0f4SArd Biesheuvel		-DPE_DLL_CHAR_EX=IMAGE_DLLCHARACTERISTICS_EX_FORWARD_CFI_COMPAT
50*538bc0f4SArd Biesheuvel
51a0509109SArd BiesheuvelAFLAGS_zboot-header.o += -DMACHINE_TYPE=IMAGE_FILE_MACHINE_$(EFI_ZBOOT_MACH_TYPE) \
52a0509109SArd Biesheuvel			 -DZBOOT_EFI_PATH="\"$(realpath $(obj)/vmlinuz.efi.elf)\"" \
53bca2f3a9SArd Biesheuvel			 -DZBOOT_SIZE_LEN=$(zboot-size-len-y) \
54*538bc0f4SArd Biesheuvel			 -DCOMP_TYPE="\"$(comp-type-y)\"" \
55*538bc0f4SArd Biesheuvel			 $(aflags-zboot-header-y)
56a0509109SArd Biesheuvel
57a0509109SArd Biesheuvel$(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FORCE
58a0509109SArd Biesheuvel	$(call if_changed_rule,as_o_S)
59a0509109SArd Biesheuvel
60a0509109SArd BiesheuvelZBOOT_DEPS := $(obj)/zboot-header.o $(objtree)/drivers/firmware/efi/libstub/lib.a
61a0509109SArd Biesheuvel
62a0509109SArd BiesheuvelLDFLAGS_vmlinuz.efi.elf := -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds
63a0509109SArd Biesheuvel$(obj)/vmlinuz.efi.elf: $(obj)/vmlinuz.o $(ZBOOT_DEPS) FORCE
64a0509109SArd Biesheuvel	$(call if_changed,ld)
65a0509109SArd Biesheuvel
66f57fb375SArd BiesheuvelOBJCOPYFLAGS_vmlinuz.efi := -O binary
67f57fb375SArd Biesheuvel$(obj)/vmlinuz.efi: $(obj)/vmlinuz.efi.elf FORCE
68a0509109SArd Biesheuvel	$(call if_changed,objcopy)
69a0509109SArd Biesheuvel
70bca2f3a9SArd Biesheuveltargets += zboot-header.o vmlinux.bin vmlinuz vmlinuz.o vmlinuz.efi.elf vmlinuz.efi
71