PN="hpipaq214-mmcboot"
PR="r0"
DESCRIPTION="Single file mini-bootloader for iPAQ 21x to boot From MMC card via it's OEM Boot Module"
SECTION="bootloaders"
LICENSE="GPL"
PROVIDES = "virtual/bootloader"
COMPATIBLE_MACHINE = "ipaq214"

DEPENDS += "linux-hpipaq214"

#command line for kernel: 179:2 = MMC 2nd partition, 31:0 = Flash 1st partition
# (Must escape any '/'s here)
#MMCBOOT_KERNEL_CMDLINE = "console=ttyS2,38400n8 root=\/dev\/mmcblk0p2 rootdelay=3 rw init=\/sbin\/init"
MMCBOOT_KERNEL_CMDLINE = "root=\/dev\/mmcblk0p2 rootdelay=3 rw init=\/sbin\/init"
#MMCBOOT_KERNEL_CMDLINE = "root=31:0 rootfstype=jffs2 rootdelay=3 rw init=\/sbin\/init"

#SRC_URI = "file:///ipaqLinux/web/files/hpipaq214-mmcboot-1.0-src.tar.gz"
SRC_URI="http://www.oliford.co.uk/hpipaq214/files/hpipaq214-mmcboot-1.0-src.tar.gz"

S = "${WORKDIR}/${PN}"

do_compile() {

	#we want to compile in the kernel
	ln -sf ${DEPLOY_DIR_IMAGE}/zImage-ipaq214.bin zImage-ipaq214.bin

	#insert the kernel command line
	sed -i "s/#define KERNEL_CMDLINE .*/#define KERNEL_CMDLINE \"${MMCBOOT_KERNEL_CMDLINE}\"/" tags.c

	#create a checksum of the kernel to check on boot
	${BUILD_CC} checksum.c -o checksum
	CHECKSUM=`./checksum zImage-ipaq214.bin | grep checksum | sed "s/checksum: //g"`;
	echo "Replacing image checksum with 0x$CHECKSUM";
	sed -i "s/#define KERN_CHECKSUM .*/#define KERN_CHECKSUM 0x$CHECKSUM/" mmcboot.c
	

	${CC} -nostdlib -c mmcbootCrt.S
	${CC} -nostdlib -c mmcboot.c
	${CC} -nostdlib -c tags.c
	${CC} -nostdlib -c led.c

#	Object ${S}/mmcbootCrt.o not included in the following line as the link script seems to include it already in OE.
#	${LD} -o mmcboot -Ttext 0x83900000 mmcboot.lds mmcboot.o tags.o led.o
	${LD} -o mmcboot -Ttext 0x83900000 mmcboot.lds

	${OBJCOPY} -R .note -R .comment -O binary mmcboot mmcboot.bin
}

# put in images directory and make a symlink with the name of the file to put on the FAT16 system.
MMCBOOT_IMAGE ?= "mmcboot-${MACHINE}-${PV}-${PR}.bin"
MMCBOOT_SYMLINK ?= "BLDIAG.NB0"

do_deploy () {
	install -d ${DEPLOY_DIR_IMAGE}
	install -m 0644 mmcboot.bin ${DEPLOY_DIR_IMAGE}/${MMCBOOT_IMAGE}

	cd ${DEPLOY_DIR_IMAGE}
	rm -f ${MMCBOOT_SYMLINK}
	ln -sf ${MMCBOOT_IMAGE} ${MMCBOOT_SYMLINK}
}
do_deploy[dirs] = "${S}"
addtask deploy before do_build after do_compile

