SubDir HAIKU_TOP src system boot platform u-boot ;

SubDirHdrs $(HAIKU_TOP) headers private kernel boot platform $(TARGET_BOOT_PLATFORM) ;

SubDirHdrs $(HAIKU_TOP) src system boot arch $(TARGET_ARCH) ;

UsePrivateHeaders [ FDirName kernel disk_device_manager ] ;
UsePrivateHeaders [ FDirName graphics common ] ;
UsePrivateHeaders [ FDirName graphics vesa ] ;
UsePrivateHeaders [ FDirName storage ] ;
UsePrivateHeaders [ FDirName kernel arch generic ] ;

# TODO: move this to arch/arm !

UsePrivateHeaders [ FDirName kernel arch $(TARGET_ARCH) board $(TARGET_BOOT_BOARD) ] ;

{
	local defines = _BOOT_MODE ;

	defines = [ FDefines $(defines) ] ;
	SubDirCcFlags $(defines) -g3 ;
	SubDirC++Flags $(defines) -fno-rtti -g3 ;
}

# we fake NetBSD since we don't have an OS ID yet for uimage,
# and we also fake a netbsd loader anyway.
local uImageFakeOS = "netbsd" ;

SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src add-ons accelerants common ] ;

# First build the non arch dependent parts
KernelMergeObject boot_platform_u-boot_common.o :
	start.cpp
	debug.cpp
	console.cpp
	serial.cpp
	devices.cpp
	keyboard.cpp
	menu.cpp
	cpu.cpp
	uimage.cpp
	video.cpp
	mmu.cpp
	$(genericPlatformSources)


	: -fno-pic
	: boot_platform_generic.a
;

KernelMergeObject boot_platform_u-boot.o :
	: :
	# must come first to have _start_* at correct locations
	boot_platform_u-boot_$(TARGET_ARCH).o
	boot_platform_u-boot_common.o
;

# bootable flash image
rule BuildUBootFlashImage image : haikuLoader : uboot_image
{
	Depends $(image) : $(haikuLoader) ;
	#Depends $(image) : $(archive) ;
	Depends $(image) : $(uboot_image) ;
	LocalClean clean : $(image) ;
	FLASH_IMAGE_BLOCK_SIZE on $(image) = 128k ;
	FLASH_IMAGE_BLOCK_COUNT on $(image) = 256 ;
	#FLASH_IMAGE_UBOOT_OFFSET on $(image) = 0 ;
	FLASH_IMAGE_LOADER_OFFSET on $(image) = 248 ;
	BuildUBootFlashImage1 $(image) : $(haikuLoader) $(uboot_image) ;
}


actions BuildUBootFlashImage1
{
	$(RM) $(<)

	# make an empty image
	dd of=$(<) \
		if=/dev/zero \
		bs=$(FLASH_IMAGE_BLOCK_SIZE) \
		count=$(FLASH_IMAGE_BLOCK_COUNT)

	# add u-boot
	dd of=$(<) \
		if=$(>[2]) \
		bs=$(FLASH_IMAGE_BLOCK_SIZE) \
		conv=notrunc

	# add root fs
#	dd of=$(<) \
#		if=$(>[3]) \
#		bs=$(FLASH_IMAGE_BLOCK_SIZE) \
#		conv=notrunc \
#		seek=$(FLASH_IMAGE_ROOTFS_OFFSET)

	# add haiku_loader (raw, elf or uimage)
	dd of=$(<) \
		if=$(>[1]) \
		bs=$(FLASH_IMAGE_BLOCK_SIZE) \
		conv=notrunc seek=$(FLASH_IMAGE_LOADER_OFFSET)
}

# uImage
rule BuildUImage image : data : args
{
	Depends $(image) : $(data) ;
	LocalClean clean : $(image) ;
	MKIMAGE_ARGS on $(image) = $(args) ;
	colon on $(image) = ":" ;
	local files = $(data:G=) ;
	BuildUImage1 $(image) : $(data) ;
}

actions BuildUImage1
{
	mkimage $(MKIMAGE_ARGS) -d $(>:J=$(colon)) $(<)
}

rule BuildUImageScript script : content
{
	LocalClean clean : $(script) ;
	SCRIPTCONTENT on $(script) = $(content) ;
	SCRIPTNAME on $(script) = $(script) ;
	FAKEOS on $(script) = $(uImageFakeOS) ;
	BuildUImageScript1 $(script) : $(content) ;
}

actions BuildUImageScript1
{
	rm -f $(<) $(<).txt
	echo '$(SCRIPTCONTENT)' > $(<).txt
	mkimage -A $(TARGET_ARCH) -O $(FAKEOS) -T script -C none -n $(SCRIPTNAME) \
		-d $(<).txt $(<)
	rm -f $(<).txt
}

# SD/mmc image rule
# requires sfdisk and mtools packages on linux
rule BuildUBootSDImage image : files
{
	Depends $(image) : $(files) ;
	SDIMAGE_BLOCK_SIZE on $(image) = 1M ;
	SDIMAGE_SIZE on $(image) = $(HAIKU_BOARD_SDIMAGE_SIZE) ;
	SDIMAGE_FDISK on $(image) = $(HOST_SFDISK) ;
	SDIMAGE_FDISK_SCRIPT on $(image) =
		"0,$(HAIKU_BOARD_SDIMAGE_FAT_SIZE),0c,-\\\n,,eb\\\n\\\n" ;
	SDIMAGE_FDISK_H on $(image) = 255 ;
	SDIMAGE_FDISK_S on $(image) = 63 ;
	SDIMAGE_FDISK_C on $(image) = ;
	SDIMAGE_FDISK_UNIT on $(image) = M ;
	BuildUBootSDImage1 $(image) : $(files) ;
}

actions BuildUBootSDImage1
{
	$(RM) $(<)
	$(RM) $(<).mtools

	# make an empty image
	dd of=$(<) \
		if=/dev/zero \
		bs=$(SDIMAGE_BLOCK_SIZE) \
		count=$(SDIMAGE_SIZE)
	# partition it
	printf '$(SDIMAGE_FDISK_SCRIPT)' | $(SDIMAGE_FDISK) -f -D \
		-H $(SDIMAGE_FDISK_H) -S $(SDIMAGE_FDISK_S) \
		-u$(SDIMAGE_FDISK_UNIT) $(<)
		#-H $(SDIMAGE_FDISK_H) -S $(SDIMAGE_FDISK_S) -C \\\$(( $(SDIMAGE_SIZE) * 1024 * 1024 / $(SDIMAGE_FDISK_H) / $(SDIMAGE_FDISK_S))) \
	# generate mtools config
	echo 'drive i: file="$(<)" partition=1' > $(<).mtools
	# format the image
	MTOOLSRC=$(<).mtools mformat -v "Haiku" i:
	# populate
	MTOOLSRC=$(<).mtools mcopy $(>) i:
	# list content
	#MTOOLSRC=$(<).mtools mdir i:
	# cleanup
	$(RM) $(<).mtools
}

# uimage targets
BuildUImage haiku_loader.ub : haiku_loader :
	-A $(TARGET_ARCH) -O $(uImageFakeOS) -T kernel -C none
	-a $(HAIKU_BOARD_LOADER_BASE) -e $(HAIKU_BOARD_LOADER_ENTRY_RAW)
		-n 'haiku_loader $(TARGET_BOOT_BOARD)' ;

local tgzArchive = haiku-floppyboot.tgz ;

BuildUImage haiku_loader_linux.ub : haiku_loader :
	-A $(TARGET_ARCH) -O linux -T kernel -C none
	-a $(HAIKU_BOARD_LOADER_BASE) -e $(HAIKU_BOARD_LOADER_ENTRY_LINUX)
		-n 'haiku_loader $(TARGET_BOOT_BOARD)' ;

BuildUImage haiku_loader_nbsd.ub : haiku_loader $(tgzArchive) :
	-A $(TARGET_ARCH) -O $(uImageFakeOS) -T multi -C none
	-a $(HAIKU_BOARD_LOADER_BASE) -e $(HAIKU_BOARD_LOADER_ENTRY_NBSD)
		-n 'haiku_loader $(TARGET_BOOT_BOARD)' ;

if $(HAIKU_BOARD_SDIMAGE_UBOOT_SCRIPT_NAME) {
	BuildUImageScript $(HAIKU_BOARD_SDIMAGE_UBOOT_SCRIPT_NAME) :
		$(HAIKU_BOARD_SDIMAGE_UBOOT_SCRIPT) ;
}

if $(HAIKU_BOARD_MLO_IMAGE) && $(HAIKU_BOARD_MLO_IMAGE_URL) {
	DownloadFile $(HAIKU_BOARD_MLO_IMAGE) $(HAIKU_BOARD_MLO_IMAGE_URL) ;
}

if $(HAIKU_BOARD_UBOOT_IMAGE) && $(HAIKU_BOARD_UBOOT_IMAGE_URL) {
	# Download the u-boot binary file.
	ubootFile = [ DownloadFile $(HAIKU_BOARD_UBOOT_IMAGE)
		: $(HAIKU_BOARD_UBOOT_IMAGE_URL) ] ;
}

# flash image targets
BuildUBootFlashImage haiku-$(HAIKU_BOOT_BOARD)_flash_image_raw.img : haiku_loader :
	$(ubootFile) ;
NotFile haiku-flash-image ;
Depends haiku-flash-image : haiku-$(HAIKU_BOOT_BOARD)_flash_image_raw.img ;

#BuildUBootFlashImage haiku-$(HAIKU_BOOT_BOARD)_flash_image_elf.img : boot_loader_u-boot :
#	$(ubootFile) ;
#NotFile haiku-flash-elf-image ;
#Depends haiku-flash-elf-image : haiku-$(HAIKU_BOOT_BOARD)_flash_image_elf.img ;

BuildUBootFlashImage haiku-$(HAIKU_BOOT_BOARD)_flash_image_uimage.img : haiku_loader.ub :
	$(ubootFile) ;
NotFile haiku-flash-uimage ;
Depends haiku-flash-uimage : haiku-$(HAIKU_BOOT_BOARD)_flash_image_uimage.img ;

# SD/mmc image targets
BuildUBootSDImage haiku-$(HAIKU_BOOT_BOARD).mmc : $(HAIKU_BOARD_SDIMAGE_FILES) ;
NotFile haiku-mmc-image ;
Depends haiku-mmc-image : haiku-$(HAIKU_BOOT_BOARD).mmc ;

SEARCH on [ FGristFiles $(genericPlatformSources) ]
	= [ FDirName $(HAIKU_TOP) src system boot platform generic ] ;

# Tell the build system to where stage1.bin can be found, so it can be used
# elsewhere.
SEARCH on stage1.bin = $(SUBDIR) ;

SubInclude HAIKU_TOP src system boot platform u-boot arch ;
