# $NetBSD: armv7.conf,v 1.7.2.5 2015/08/30 09:56:19 martin Exp $
# ARMv7 customization script used by mkimage
#
board=armv7
console=fb
resize=true

. ${DIR}/conf/evbarm.conf

kernels_beagle="BEAGLEBOARD BEAGLEBONE"
kernels_awin="BPI CUBIEBOARD CUBIETRUCK HUMMINGBIRD_A31"
kernels_rpi="RPI2"
kernels_amlogic="ODROID-C1"

make_label() {
	make_label_evbarm
}

make_fstab() {
	make_fstab_evbarm
}

customize() {
	customize_evbarm
	cat >> ${mnt}/etc/rc.conf << EOF
mdnsd=YES
wscons=YES
devpubd=YES
EOF
}

populate_beagle() {
	bboard_kernelimg=netbsd-BEAGLEBOARD.ub
	bboard_loadaddr=81000000
	bbone_kernelimg=netbsd-BEAGLEBONE.ub
	bbone_loadaddr=82000000

	# Create a uEnv.txt to auto boot the correct kernel
	cat >> ${mnt}/boot/uEnv.txt << EOF
loaduimage=if test \$board = am335x; then fatload mmc 0 ${bbone_loadaddr} ${bbone_kernelimg}; bootm ${bbone_loadaddr} root=ld0a; else fatload mmc 0 ${bboard_loadaddr} ${bboard_kernelimg}; bootm ${bboard_loadaddr} root=ld0a; fi
EOF
}

populate_awin() {
	cat >> ${mnt}/boot/uEnv.txt << EOF
bootargs=root=ld0a console=${console}
uenvcmd=mmc dev 0; mmc rescan; if test \$fdtfile = sun7i-a20-cubieboard2.dtb; then setenv kernel netbsd-CUBIEBOARD.ub; elif test \$fdtfile = sun7i-a20-cubietruck.dtb; then setenv kernel netbsd-CUBIETRUCK.ub; elif test \$fdtfile = sun6i-a31-hummingbird.dtb; then setenv kernel netbsd-HUMMINGBIRD_A31.ub; elif test \$fdtfile = sun7i-a20-bananapi.dtb; then setenv kernel netbsd-BPI.ub; fi; fatload mmc 0:1 82000000 \$kernel; bootm 82000000
EOF
}

populate_rpi() {
	firmwaredir=${src}/external/broadcom/rpi-firmware/dist
	firmwarefiles="LICENCE.broadcom bootcode.bin fixup.dat fixup_cd.dat start.elf start_cd.elf"

	# RPI2 kernel needs to be installed as kernel7.img
	if [ -f "${mnt}/boot/netbsd-RPI2.bin" ]; then
		echo "${bar} renaming netbsd-RPI2.bin to kernel7.img ${bar}"
		mv "${mnt}/boot/netbsd-RPI2.bin" "${mnt}/boot/kernel7.img"

		cat > ${mnt}/boot/cmdline.txt << EOF
root=ld0a console=${console}
#fb=1280x1024		# to select a mode, otherwise try EDID
#fb=disable		# to disable fb completely
EOF

	        echo "${bar} installing firmware files ${bar}"
		(cd ${mnt}/boot &&
			for f in ${firmwarefiles}; do
				echo " $f"
				cp ${firmwaredir}/${f} .
			done
        	)

	fi
}

populate_amlogic() {
	odroidc1_kernelimg=netbsd-ODROID-C1.ub

	# Create a boot.ini for Amlogic U-Boot
	cat >> ${mnt}/boot/boot.ini << EOF
ODROIDC-UBOOT-CONFIG

setenv bootargs "root=ld0f awge0.mac-address=\${ethaddr} console=${console}"
setenv bootcmd "fatload mmc 0:1 0x21000000 ${odroidc1_kernelimg}; bootm 0x21000000"
run bootcmd
EOF
}

populate() {
	echo "${bar} looking for kernels in ${kernel} ${bar}"
	kernels=""
	# .ub kernels
	for k in $kernels_beagle $kernels_awin $kernels_amlogic; do
		f="${kernel}/netbsd-${k}.ub.gz"
		test -f "${f}" && kernels="${kernels} ${f}"
	done
	# .bin kernels
	for k in $kernels_rpi; do
		f="${kernel}/netbsd-${k}.bin.gz"
		test -f "${f}" && kernels="${kernels} ${f}"
	done

	# install kernels to /boot partition
	for k in ${kernels}; do
		tgt="$(basename ${k} | sed 's/\.gz$//')"
		echo "${bar} installing ${k} to /boot/${tgt} ${bar}"
		case "${k}" in
		*.gz)
			${GZIP_CMD} -dc "${k}" > "${mnt}/boot/${tgt}"
			;;
		*)
			cp "${k}" "${mnt}/boot/${tgt}"
			;;
		esac
	done

	# board specific configuration
	populate_beagle
	populate_awin
	populate_rpi
	populate_amlogic
}
