MEDIA_TYPE = netboot image

NETBOOT_DIR_TARGETS = $(TEMP_INITRD) $(TEMP_KERNEL)
TARGET = $(NETBOOT_DIR) $(NETBOOT_TAR) $(MINIISO)
EXTRANAME = $(MEDIUM)/

MANIFEST-NETBOOT_DIR = "PXE boot directory for tftp server"
MANIFEST-NETBOOT_TAR = "tarball of PXE boot directory"
MANIFEST-MINIISO = "tiny CD image that boots the netboot installer"

gzipmagic = 8b1f
magic = $(shell od -x -N2 $(1) | head -1 | cut -d' ' -f2)
compress-type = $(if $(findstring $(gzipmagic),$(call magic,$(1))),gzip,none)

.PHONY: arch_netboot_dir
arch_netboot_dir:
	-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/arm64-efi; \
		cp -a $(TEMP_GRUB_EFI)/bootnetaa64.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/arm64-efi/* \
			$(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub/arm64-efi/; \
		grub-gencfg \
			KERNEL /$(NETBOOT_PATH)/linux \
			INITRD /$(NETBOOT_PATH)/initrd.gz \
			HEADER boot/arm64/grub/grub-efi.cfg \
		> $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub/grub.cfg; \
	fi

	# Generate images for X-Gene systems using U-Boot.
	# They don't currently support bootz.
	$(eval XU_INSTALL_PATH=$(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/xgene-uboot)
	mkdir -p $(XU_INSTALL_PATH)
	mkimage -A arm -O linux -T kernel -C $(call compress-type,$(TEMP_KERNEL)) -a 0x80000 -e 0x80000 -n "Ubuntu kernel" -d $(TEMP_KERNEL) $(XU_INSTALL_PATH)/uImage
	mkimage -A arm -O linux -T ramdisk -C none -a 0x0 -e 0x0 -n "debian-installer ramdisk" -d $(TEMP_INITRD) $(XU_INSTALL_PATH)/uInitrd
