# $NetBSD: rpi.conf,v 1.29 2015/04/19 18:28:31 hubertf Exp $
# Raspberry Pi customization script used by mkimage
#

board=rpi
kernel=$src/sys/arch/evbarm/compile/RPI/netbsd-RPI.bin
resize=true

. ${DIR}/conf/evbarm.conf

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

make_fstab() {
	make_fstab_evbarm
}

make_label() {
	make_label_evbarm
}

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

	#
	# If arm_freq is specified in config.txt, set CPU frequency
	# to match at boot time.
	#
	cp ${release}/etc/rc.local ${mnt}/etc/rc.local
	cat >> ${mnt}/etc/rc.local << EOF
if /sbin/sysctl -q machdep.cpu.frequency.max; then
	cpufreq_max=\$(/sbin/sysctl -n machdep.cpu.frequency.max)
	cpufreq_cur=\$(/sbin/sysctl -n machdep.cpu.frequency.current)
	if [ ! "\$cpufreq_max" = "\$cpufreq_cur" ]; then
		/sbin/sysctl -w machdep.cpu.frequency.target=\$cpufreq_max
	fi
fi
EOF
	echo "./etc/rc.local type=file uname=root gname=wheel mode=0644" \
	    >> "$tmp/selected_sets"
}

populate() {
	rpi2_kernel="$(echo $kernel | sed 's/RPI/RPI2/g')"

	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
	if [ ! -f ${kernel} ]; then
		echo ${PROG}: Missing ${kernel} 1>&2
		exit 1
	fi

	echo "${bar} installing RPI kernel ${bar}"
	case ${kernel} in
	*.gz)
		gzip -dc ${kernel} > ${mnt}/boot/kernel.img
		;;
	*)
		cp ${kernel} ${mnt}/boot/kernel.img
		;;
	esac

	echo "${bar} installing RPI2 kernel ${bar}"
	case ${rpi2_kernel} in
	*.gz)
		gzip -dc ${rpi2_kernel} > ${mnt}/boot/kernel7.img
		;;
	*)
		cp ${rpi_kernel} ${mnt}/boot/kernel7.img
		;;
	esac

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

}
