MEDIA_TYPE = netboot image
TARGET = $(TEMP_INITRD) $(TEMP_KERNEL) all-generic
EXTRANAME = $(MEDIUM)
INITRD_FS = initramfs

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)/device-tree "Device Tree files for generic ARM"
	update-manifest $(SOME_DEST)/$(EXTRANAME)/vmlinuz "Linux kernel for generic ARM"
	update-manifest $(SOME_DEST)/$(EXTRANAME)/initrd.gz "initrd for generic ARM"

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

	# Append the device-tree file
	cat $(TEMP_KERNEL) $(TEMP)/tree/lib/firmware/${KERNELVERSION}/device-tree/omap4-panda.dtb > $(INSTALL_PATH)/kernel

	# Generate uImage/uInitrd
	mkimage -A arm -O linux -T kernel -C none -a 0x80008000 -e 0x80008000 -n "Ubuntu kernel" -d $(INSTALL_PATH)/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

	# Generate boot.scrs
	mkimage -A arm -T script -C none -n "Ubuntu boot script (serial)" -d boot/arm/boot.script-omap4-serial $(INSTALL_PATH)/boot.scr-serial
	mkimage -A arm -T script -C none -n "Ubuntu boot script (framebuffer)" -d boot/arm/boot.script-omap4-fb $(INSTALL_PATH)/boot.scr-fb

	# Create DD'able filesystems
	mkdosfs -C $(INSTALL_PATH)/boot.img-fat-serial 73712
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/uImage ::uImage
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/uInitrd ::uInitrd
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial /usr/lib/u-boot/omap4_panda/MLO ::MLO
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial /usr/lib/u-boot/omap4_panda/u-boot.img ::u-boot.img
	cp $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/boot.img-fat-fb
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/boot.scr-serial ::boot.scr
	mcopy -i $(INSTALL_PATH)/boot.img-fat-fb $(INSTALL_PATH)/boot.scr-fb ::boot.scr
	boot/arm/generate-partitioned-filesystem $(INSTALL_PATH)/boot.img-fat-fb $(INSTALL_PATH)/boot.img-fb
	boot/arm/generate-partitioned-filesystem $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/boot.img-serial
	gzip -9 $(INSTALL_PATH)/boot.img-serial
	gzip -9 $(INSTALL_PATH)/boot.img-fb
	rm $(INSTALL_PATH)/boot.img-fat-serial
	rm $(INSTALL_PATH)/boot.img-fat-fb
	rm $(INSTALL_PATH)/kernel

	# Publish SPL
	cp /usr/lib/u-boot/omap4_panda/MLO $(INSTALL_PATH)/MLO
	cp /usr/lib/u-boot/omap4_panda/u-boot.img $(INSTALL_PATH)/u-boot.img

	# Generate manifests
	update-manifest $(INSTALL_PATH)/uImage "Linux kernel for OMAP4 Boards"
	update-manifest $(INSTALL_PATH)/uInitrd "initrd for OMAP4 Boards"
	update-manifest $(INSTALL_PATH)/boot.scr-fb "Boot script for booting OMAP4 netinstall initrd and kernel from SD card with framebuffer"
	update-manifest $(INSTALL_PATH)/boot.scr-serial "Boot script for booting OMAP4 netinstall initrd and kernel from SD card with serial"
	update-manifest $(INSTALL_PATH)/boot.img-serial.gz "Boot image for booting OMAP4 netinstall with serial output"
	update-manifest $(INSTALL_PATH)/boot.img-fb.gz "Boot image for booting OMAP netinstall with framebuffer output"
	update-manifest $(INSTALL_PATH)/MLO "MMC Loader for OMAP4 Pandaboards"
	update-manifest $(INSTALL_PATH)/u-boot.img "Universal Bootloader for OMAP4 Pandaboards"

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

	# Append the device-tree file
	cat $(TEMP_KERNEL) $(TEMP)/tree/lib/firmware/${KERNELVERSION}/device-tree/omap3-beagle-xm.dtb > $(INSTALL_PATH)/kernel

	# Generate uImage/uInitrd
	mkimage -A arm -O linux -T kernel -C none -a 0x80008000 -e 0x80008000 -n "Ubuntu kernel" -d $(INSTALL_PATH)/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

	# Generate boot.scrs
	mkimage -A arm -T script -C none -n "Ubuntu boot script (serial)" -d boot/arm/boot.script-omap-serial $(INSTALL_PATH)/boot.scr-serial
	mkimage -A arm -T script -C none -n "Ubuntu boot script (framebuffer)" -d boot/arm/boot.script-omap-fb $(INSTALL_PATH)/boot.scr-fb

	# Create DD'able filesystems
	mkdosfs -C $(INSTALL_PATH)/boot.img-fat-serial 73712
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/uImage ::uImage
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/uInitrd ::uInitrd
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial /usr/lib/u-boot/omap3_beagle/MLO ::MLO
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial /usr/lib/u-boot/omap3_beagle/u-boot.img ::u-boot.img
	cp $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/boot.img-fat-fb
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/boot.scr-serial ::boot.scr
	mcopy -i $(INSTALL_PATH)/boot.img-fat-fb $(INSTALL_PATH)/boot.scr-fb ::boot.scr
	boot/arm/generate-partitioned-filesystem $(INSTALL_PATH)/boot.img-fat-fb $(INSTALL_PATH)/boot.img-fb
	boot/arm/generate-partitioned-filesystem $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/boot.img-serial
	gzip -9 $(INSTALL_PATH)/boot.img-serial
	gzip -9 $(INSTALL_PATH)/boot.img-fb
	rm $(INSTALL_PATH)/boot.img-fat-serial
	rm $(INSTALL_PATH)/boot.img-fat-fb
	rm $(INSTALL_PATH)/kernel

	# Publish SPL
	cp /usr/lib/u-boot/omap3_beagle/MLO $(INSTALL_PATH)/MLO
	cp /usr/lib/u-boot/omap3_beagle/u-boot.img $(INSTALL_PATH)/u-boot.img

	# Generate manifests
	update-manifest $(INSTALL_PATH)/uImage "Linux kernel for OMAP4 Boards"
	update-manifest $(INSTALL_PATH)/uInitrd "initrd for OMAP4 Boards"
	update-manifest $(INSTALL_PATH)/boot.scr-fb "Boot script for booting OMAP3 netinstall initrd and kernel from SD card with framebuffer"
	update-manifest $(INSTALL_PATH)/boot.scr-serial "Boot script for booting OMAP3 netinstall initrd and kernel from SD card with serial"
	update-manifest $(INSTALL_PATH)/boot.img-serial.gz "Boot image for booting OMAP3 netinstall with serial output"
	update-manifest $(INSTALL_PATH)/boot.img-fb.gz "Boot image for booting OMAP3 netinstall with framebuffer output"
	update-manifest $(INSTALL_PATH)/MLO "MMC Loader for OMAP3 Beagleboards"
	update-manifest $(INSTALL_PATH)/u-boot.img "Universal Bootloader for OMAP3 Beagleboards"

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

	# Append the device-tree file
	cat $(TEMP_KERNEL) $(TEMP)/tree/lib/firmware/${KERNELVERSION}/device-tree/imx6q-wandboard.dtb > $(INSTALL_PATH)/kernel

	# Generate uImage/uInitrd
	mkimage -A arm -O linux -T kernel -C none -a 0x10008000 -e 0x10008000 -n "Ubuntu kernel" -d $(INSTALL_PATH)/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
	cp boot/arm/uEnv.txt-wandboard-serial $(INSTALL_PATH)/uEnv.txt-serial
	cp /usr/lib/u-boot/wandboard/SPL $(INSTALL_PATH)/SPL
	cp /usr/lib/u-boot/wandboard/u-boot.img $(INSTALL_PATH)/u-boot.img

	# Create DD'able filesystems
	mkdosfs -C $(INSTALL_PATH)/boot.img-fat-serial 73712
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/uImage ::uImage
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/uInitrd ::uInitrd
	mcopy -i $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/uEnv.txt-serial ::uEnv.txt
	boot/arm/generate-partitioned-filesystem $(INSTALL_PATH)/boot.img-fat-serial $(INSTALL_PATH)/boot.img-serial 1048576
	dd if=$(INSTALL_PATH)/SPL of=$(INSTALL_PATH)/boot.img-serial bs=512 seek=2 conv=notrunc
	dd if=$(INSTALL_PATH)/u-boot.img of=$(INSTALL_PATH)/boot.img-serial bs=512 seek=138 conv=notrunc
	gzip -9 $(INSTALL_PATH)/boot.img-serial
	rm $(INSTALL_PATH)/boot.img-fat-serial
	rm $(INSTALL_PATH)/kernel

	# Generate manifests
	update-manifest $(INSTALL_PATH)/uImage "Linux kernel for WandBoard quad"
	update-manifest $(INSTALL_PATH)/uInitrd "initrd for WandBoard quad"
	update-manifest $(INSTALL_PATH)/uEnv.txt-serial "Boot script for WandBoard quad with serial"
	update-manifest $(INSTALL_PATH)/u-boot.imx "Universal Bootloader for WandBoard quad"
	update-manifest $(INSTALL_PATH)/boot.img-serial.gz "Boot image for booting WandBoard quad netinstall with serial output"

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

	# Copy the kernel and initrd that we plan to use raw
	cp -a $(TEMP_KERNEL) $(INSTALL_PATH)/vmlinuz
	cp -a $(TEMP_INITRD) $(INSTALL_PATH)/initrd.gz

	# Copy the tegra DTBs to our install path
	cp $(TREE)/lib/firmware/${KERNELVERSION}/device-tree/tegra*.dtb $(INSTALL_PATH)/

	# Generate boot.scrs
	mkimage -A arm -T script -C none -n "Ubuntu boot script (dhcp) (serial)" -d boot/arm/boot.script-tegra-dhcp-serial $(INSTALL_PATH)/boot.scr-tegra-dhcp-serial
	mkimage -A arm -T script -C none -n "Ubuntu boot script (disk-based) (serial)" -d boot/arm/boot.script-tegra-disk-serial $(INSTALL_PATH)/boot.scr-tegra-disk-serial

	# Create DD'able filesystems
	mkdosfs -C $(INSTALL_PATH)/boot.img-fat-tegra-serial 73712
	mcopy -i $(INSTALL_PATH)/boot.img-fat-tegra-serial $(INSTALL_PATH)/vmlinuz ::vmlinuz
	mcopy -i $(INSTALL_PATH)/boot.img-fat-tegra-serial $(INSTALL_PATH)/initrd.gz ::initrd.gz
	mcopy -i $(INSTALL_PATH)/boot.img-fat-tegra-serial $(INSTALL_PATH)/tegra*.dtb ::
	mcopy -i $(INSTALL_PATH)/boot.img-fat-tegra-serial $(INSTALL_PATH)/boot.scr-tegra-disk-serial ::boot.scr
	boot/arm/generate-partitioned-filesystem $(INSTALL_PATH)/boot.img-fat-tegra-serial $(INSTALL_PATH)/boot.img-tegra-serial

	gzip -9 $(INSTALL_PATH)/boot.img-tegra-serial
	rm $(INSTALL_PATH)/boot.img-fat-tegra-serial

	update-manifest $(INSTALL_PATH)/boot.scr-tegra-dhcp-serial "Boot script for booting Tegra netinstall, from a DHCP server, with serial output"
	update-manifest $(INSTALL_PATH)/boot.scr-tegra-disk-serial "Boot script for booting Tegra netinstall, from disk, with serial output"
	update-manifest $(INSTALL_PATH)/boot.img-tegra-serial.gz "Boot image for booting Tegra netinstall with serial output"

all-generic: generic beagleboard pandaboard wandboard tegra
