# $NetBSD: rpi_inst.conf,v 1.19 2022/11/19 09:19:27 yamt Exp $
# Raspberry Pi customization script used by mkimage
#

board=rpi_inst
kerneldir=${KERNOBJDIR}/RPI_INSTALL
kernel=$kerneldir/netbsd-RPI_INSTALL.img

. ${DIR}/conf/evbarm.conf

image=$HOME/${board}.img

swap=8
extra=8		# spare space
init=8
boot=$(( 192 - ${init} ))

size=$(( 10485760 + ${swap} * 1024 * 512 + ${boot} * 1024 * 512 + ${init} * 1024 * 512 ))
msdosid=14

make_label() {
	make_label_evbarm
}

customize() {
	echo "${bar} creating directories ${bar}"
	mkdir ${mnt}/proc
}

make_fstab() {
	:
}

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

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

	cat > ${mnt}/boot/config.txt << EOF
#
upstream_kernel=1
os_prefix=dtb/
cmdline=../cmdline.txt
kernel=/kernel.img
# UART settings, https://www.raspberrypi.com/documentation/computers/configuration.html#configuring-uarts
enable_uart=1            
force_turbo=0
EOF

	if [ ! -f ${kernel} ]; then
		echo ${PROG}: Missing ${kernel} 1>&2
		exit 1
	fi

	echo "${bar} installing kernel ${bar}"
	case ${kernel} in
	*.gz)
		echo " ${kernel} uncompressed to kernel.img"
		${GZIP_CMD} -dc ${kernel} > ${mnt}/boot/kernel.img
		;;
	*)
		echo " ${kernel} copied"
		cp ${kernel} ${mnt}/boot/kernel.img
		;;
	esac || fail "copy of ${kernel} to  ${mnt}/boot/kernel.img failed"

	echo "${bar} installing firmware files ${bar}"
	(cd ${mnt}/boot &&
		for f in ${firmwarefiles}; do
			echo " $f"
			cp ${firmwaredir}/${f} . || exit 1
		done
	) || fail "Copy of firmware to ${mnt}/boot failed"

}
