1# SPDX-License-Identifier: GPL-2.0
2
3# to be include'd by arch/$(ARCH)/boot/Makefile after setting
4# EFI_ZBOOT_PAYLOAD, EFI_ZBOOT_BFD_TARGET and EFI_ZBOOT_MACH_TYPE
5
6quiet_cmd_copy_and_pad = PAD     $@
7      cmd_copy_and_pad = cp $< $@ && \
8			 truncate -s $(shell hexdump -s16 -n4 -e '"%u"' $<) $@
9
10# Pad the file to the size of the uncompressed image in memory, including BSS
11$(obj)/vmlinux.bin: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE
12	$(call if_changed,copy_and_pad)
13
14comp-type-$(CONFIG_KERNEL_GZIP)		:= gzip
15comp-type-$(CONFIG_KERNEL_LZ4)		:= lz4
16comp-type-$(CONFIG_KERNEL_LZMA)		:= lzma
17comp-type-$(CONFIG_KERNEL_LZO)		:= lzo
18comp-type-$(CONFIG_KERNEL_XZ)		:= xzkern
19comp-type-$(CONFIG_KERNEL_ZSTD)		:= zstd22
20
21# in GZIP, the appended le32 carrying the uncompressed size is part of the
22# format, but in other cases, we just append it at the end for convenience,
23# causing the original tools to complain when checking image integrity.
24# So disregard it when calculating the payload size in the zimage header.
25zboot-method-y                         := $(comp-type-y)_with_size
26zboot-size-len-y                       := 12
27
28zboot-method-$(CONFIG_KERNEL_GZIP)     := gzip
29zboot-size-len-$(CONFIG_KERNEL_GZIP)   := 8
30
31# Copy the SizeOfHeaders and SizeOfCode fields from the payload to the end of
32# the compressed image. Note that this presupposes a PE header offset of 64
33# bytes, which is what arm64, RISC-V and LoongArch use.
34quiet_cmd_compwithsize = $(quiet_cmd_$(zboot-method-y))
35      cmd_compwithsize = $(cmd_$(zboot-method-y)) && ( \
36			   dd status=none if=$< bs=4 count=1 skip=37 ; \
37			   dd status=none if=$< bs=4 count=1 skip=23 ) >> $@
38
39$(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
40	$(call if_changed,compwithsize)
41
42OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) \
43			  --rename-section .data=.gzdata,load,alloc,readonly,contents
44$(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
45	$(call if_changed,objcopy)
46
47AFLAGS_zboot-header.o += -DMACHINE_TYPE=IMAGE_FILE_MACHINE_$(EFI_ZBOOT_MACH_TYPE) \
48			 -DZBOOT_EFI_PATH="\"$(realpath $(obj)/vmlinuz.efi.elf)\"" \
49			 -DZBOOT_SIZE_LEN=$(zboot-size-len-y) \
50			 -DCOMP_TYPE="\"$(comp-type-y)\""
51
52$(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FORCE
53	$(call if_changed_rule,as_o_S)
54
55ZBOOT_DEPS := $(obj)/zboot-header.o $(objtree)/drivers/firmware/efi/libstub/lib.a
56
57LDFLAGS_vmlinuz.efi.elf := -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds
58$(obj)/vmlinuz.efi.elf: $(obj)/vmlinuz.o $(ZBOOT_DEPS) FORCE
59	$(call if_changed,ld)
60
61OBJCOPYFLAGS_vmlinuz.efi := -O binary
62$(obj)/vmlinuz.efi: $(obj)/vmlinuz.efi.elf FORCE
63	$(call if_changed,objcopy)
64
65targets += zboot-header.o vmlinux.bin vmlinuz vmlinuz.o vmlinuz.efi.elf vmlinuz.efi
66