# Common configuration and targets for armhf and arm64 systems.

# The font to load in GRUB
GRUB_FONT = /usr/share/grub/ascii.pf2

# Extract GRUB EFI files.
.PHONY: arm_grub_efi
arm_grub_efi: $(TEMP_DTBS)
ifeq ($(GRUB_EFI),y)
	efi-image -o $(TEMP_GRUB_EFI) -g $(GRUB_EFI_PLATFORM) \
		-e $(GRUB_EFI_NAME) -n $(NETBOOT_PATH) \
		-s $(EFI_SIGNED) -d $(TEMP_DTBS)
endif

# Supply GRUB EFI configuration.
.PHONY: arch_cd_info_dir
arch_cd_info_dir: arm_grub_efi
	-rm -f $(TEMP_CD_INFO_DIR)/*
	mkdir -p $(TEMP_CD_INFO_DIR)

	if [ "$(GRUB_EFI)" = y ]; then \
		set -e; \
		mkdir -p $(TEMP_CD_INFO_DIR)/grub/$(GRUB_EFI_PLATFORM); \
		cp -a $(TEMP_GRUB_EFI)/efi.img $(TEMP_CD_INFO_DIR)/grub/; \
		if [ "$(GRAPHICAL_INSTALLER)" = y ]; then \
			grub-gencfg \
				KERNEL /%install%/vmlinuz \
				INITRD /%install%/initrd.gz \
				INITRD_GTK /%install%/gtk/initrd.gz \
				HEADER boot/$(ARCH)/grub/grub-efi.cfg \
				IS_PURE_GTK "$(IS_PURE_GTK)" \
			> $(TEMP_CD_INFO_DIR)/grub/grub.cfg; \
		else \
			grub-gencfg \
				KERNEL /%install%/vmlinuz \
				INITRD /%install%/initrd.gz \
				HEADER boot/$(ARCH)/grub/grub-efi.cfg \
				IS_PURE_GTK "$(IS_PURE_GTK)" \
			> $(TEMP_CD_INFO_DIR)/grub/grub.cfg; \
		fi; \
		cp -a $(GRUB_FONT) $(TEMP_CD_INFO_DIR)/grub/font.pf2; \
		cp -a $(TEMP_GRUB_EFI)/boot/grub/$(GRUB_EFI_PLATFORM)/* \
			$(TEMP_CD_INFO_DIR)/grub/$(GRUB_EFI_PLATFORM)/; \
	fi

.PHONY: arch_miniiso
arch_miniiso: arm_grub_efi
	-rm -f $(TEMP_CD_TREE)/*
	mkdir -p $(TEMP_CD_TREE)

	ln -f $(TEMP_KERNEL) $(TEMP_CD_TREE)/linux
	ln -f $(TEMP_INITRD) $(TEMP_CD_TREE)/initrd.gz
	if [ "$(GRAPHICAL_INSTALLER)" = y ]; then \
		mkdir -p $(TEMP_CD_TREE)/gtk; \
		ln -f $(TEMP_INITRD) $(TEMP_CD_TREE)/gtk/initrd.gz; \
	fi

	mkdir -p $(TEMP_CD_TREE)/.disk
	echo "Debian GNU/Linux $(DEBIAN_VERSION) $(ARCH) - netboot mini.iso $(BUILD_DATE)"\
	> $(TEMP_CD_TREE)/.disk/info

	if [ "$(GRUB_EFI)" = y ]; then \
		set -e; \
		mkdir -p $(TEMP_CD_TREE)/boot/grub/$(GRUB_EFI_PLATFORM); \
		cp -a $(TEMP_GRUB_EFI)/efi.img $(TEMP_CD_TREE)/boot/grub/; \
		if [ "$(GRAPHICAL_INSTALLER)" = y ]; then \
			grub-gencfg \
				KERNEL /linux \
				INITRD /initrd.gz \
				INITRD_GTK /gtk/initrd.gz \
				HEADER boot/$(ARCH)/grub/grub-efi.cfg \
				IS_PURE_GTK "$(IS_PURE_GTK)" \
			> $(TEMP_CD_TREE)/boot/grub/grub.cfg; \
		else \
			grub-gencfg \
				KERNEL /linux \
				INITRD /initrd.gz \
				HEADER boot/$(ARCH)/grub/grub-efi.cfg \
			> $(TEMP_CD_TREE)/boot/grub/grub.cfg; \
		fi; \
		cp -a $(GRUB_FONT) $(TEMP_CD_TREE)/boot/grub/font.pf2; \
		cp -a $(TEMP_GRUB_EFI)/boot/grub/$(GRUB_EFI_PLATFORM)/* \
			$(TEMP_CD_TREE)/boot/grub/$(GRUB_EFI_PLATFORM)/; \
		mkdir -p $(TEMP_CD_TREE)/EFI/debian; \
		echo "search --file --set=root /.disk/info" >> $(TEMP_CD_TREE)/EFI/debian/grub.cfg; \
		echo "set prefix=(\$$root)/boot/grub" >> $(TEMP_CD_TREE)/EFI/debian/grub.cfg; \
		echo "source \$$prefix/$(GRUB_EFI_PLATFORM)/grub.cfg" >> $(TEMP_CD_TREE)/EFI/debian/grub.cfg; \
	fi

	$(clamp_mtimes) $(TEMP_CD_TREE)
	$(normalise_permissions) $(TEMP_CD_TREE)

	if [ "$(GRUB_EFI)" = y ]; then \
		xorriso -as mkisofs -r -J -c boot.cat \
			-boot-load-size 4 -boot-info-table \
			-eltorito-alt-boot \
			--efi-boot boot/grub/efi.img -no-emul-boot \
			-append_partition 2 0xef $(TEMP_CD_TREE)/boot/grub/efi.img \
			-partition_cyl_align all \
			-o $(TEMP_MINIISO) $(TEMP_CD_TREE); \
	fi

.PHONY: arch_netboot_dir
arch_netboot_dir: arm_grub_efi
	-rm -f $(TEMP_NETBOOT_DIR)
	mkdir -p $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)
	cp $(TEMP_KERNEL) $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/linux
	cp $(TEMP_INITRD) $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/initrd.gz

	if [ "$(GRUB_EFI)" = y ]; then \
		set -e; \
		mkdir -p $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub/$(GRUB_EFI_PLATFORM); \
		cp -a $(TEMP_GRUB_EFI)/*.efi $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH); \
		cp -a $(GRUB_FONT) $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub/font.pf2; \
		cp -a $(TEMP_GRUB_EFI)/boot/grub/$(GRUB_EFI_PLATFORM)/* \
			$(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub/$(GRUB_EFI_PLATFORM)/; \
		if [ "$(GRAPHICAL_INSTALLER)" = y ]; then \
			grub-gencfg \
				KERNEL /$(NETBOOT_PATH)/linux \
				INITRD /$(NETBOOT_PATH)/initrd.gz \
				INITRD_GTK /$(NETBOOT_PATH)/gtk/initrd.gz \
				HEADER boot/$(ARCH)/grub/grub-efi.cfg \
				IS_PURE_GTK "$(IS_PURE_GTK)" \
			> $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub/grub.cfg; \
		else \
			grub-gencfg \
				KERNEL /$(NETBOOT_PATH)/linux \
				INITRD /$(NETBOOT_PATH)/initrd.gz \
				HEADER boot/$(ARCH)/grub/grub-efi.cfg \
				IS_PURE_GTK "$(IS_PURE_GTK)" \
			> $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub/grub.cfg; \
		fi; \
	fi

# ChromeOS devices
.PHONY: arch_depthcharge
arch_depthcharge: $(TEMP_KERNEL) $(TEMP_INITRD_XZ) $(TEMP_DTBS)
	-rm -f $(TEMP_DEPTHCHARGE)/*
	mkdir -p $(TEMP_DEPTHCHARGE)
	for board in $(DEPTHCHARGE_BOARDS); do \
		depthchargectl build -v \
			--board $$board \
			--kernel-release $(KERNELVERSION) \
			--kernel $(TEMP_KERNEL) \
			--initramfs $(TEMP_INITRD_XZ) \
			--fdtdir $(TEMP_DTBS) \
			--root none \
			--kernel-cmdline "--- quiet" \
			--output $(TEMP_DEPTHCHARGE)/$${board}.kernel.img; \
		gen-hd-image -v -z \
			-X gpt \
			-p $$(($$(stat -c%s $(TEMP_DEPTHCHARGE)/$${board}.kernel.img) / 512 + 2048)) \
			-i $$(mktemp -d) \
			-o $(TEMP_DEPTHCHARGE)/$${board}.disk.img \
			-d FE3A2A5D-4F32-41A7-B725-ACCC3285A309 \
			$(TEMP_DEPTHCHARGE)/$${board}.kernel.img \
			2048; \
		pigz -9nmf $(TEMP_DEPTHCHARGE)/$${board}.kernel.img; \
	done

include config/linux.cfg
