MEDIA_TYPE = netboot image
TARGET = $(NETBOOT_DIR) $(NETBOOT_TAR) all-generic
EXTRANAME = $(MEDIUM)/
INITRD_FS = initramfs

MANIFEST-INITRD = "netboot initrd"
MANIFEST-KERNEL = "kernel image to netboot"

NETBOOT_DIR_TARGETS = $(TEMP_INITRD) $(TEMP_KERNEL)
MANIFEST-NETBOOT_DIR = "PXE boot directory for tftp server"
MANIFEST-NETBOOT_TAR = "tarball of PXE boot directory"

generic:
	mkdir -p $(SOME_DEST)/$(EXTRANAME)/
	cp $(TEMP_KERNEL) $(SOME_DEST)/$(EXTRANAME)/vmlinuz
	cp $(TEMP_INITRD) $(SOME_DEST)/$(EXTRANAME)/initrd.gz
	update-manifest $(SOME_DEST)/$(EXTRANAME)vmlinuz "Linux kernel for generic ARM64"
	update-manifest $(SOME_DEST)/$(EXTRANAME)initrd.gz "initrd for generic ARM64"


xgene:
	# Make sure our build environment is clean
	$(eval INSTALL_PATH=$(SOME_DEST)/netboot/xgene)
	rm -rf $(INSTALL_PATH)
	mkdir -p $(INSTALL_PATH)

	# Generate uImage/uInitrd
	mkimage -A arm -O linux -T kernel -C none -a 0x80000 -e 0x80000 -n "Ubuntu kernel" -d $(TEMP_KERNEL) $(INSTALL_PATH)/uImage
	mkimage -A arm -O linux -T ramdisk -C none -a 0x0 -e 0x0 -n "debian-installer ramdisk" -d $(TEMP_INITRD) $(INSTALL_PATH)/uInitrd

	if [ -f "$(TEMP)/tree/lib/firmware/${KERNELVERSION}/device-tree/apm-mustang.dtb" ]; then \
	  install -m644 $(TEMP)/tree/lib/firmware/${KERNELVERSION}/device-tree/apm-mustang.dtb $(INSTALL_PATH); \
	elif [ -f "$(TEMP)/tree/lib/firmware/${KERNELVERSION}/device-tree/apm/apm-mustang.dtb" ]; then \
	  install -m644 $(TEMP)/tree/lib/firmware/${KERNELVERSION}/device-tree/apm/apm-mustang.dtb $(INSTALL_PATH); \
	else \
	  echo "Couldn't find apm-mustang.dtb"; \
	  exit 1; \
	fi

	update-manifest $(INSTALL_PATH)/uImage "Linux kernel for X-Gene Boards"
	update-manifest $(INSTALL_PATH)/uInitrd "initrd for X-Gene Boards"
	update-manifest $(INSTALL_PATH)/apm-mustang.dtb "Device Tree file for APM X-Gene Mustang Board"

arch_netboot_dir: arm64_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/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/; \
		cp -f boot/arm64/grub/grub-efi.cfg \
			$(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub/grub.cfg; \
	fi

all-generic: generic xgene arch_netboot_dir
