# $NetBSD: armv7.conf,v 1.33 2019/04/13 10:58:11 jmcneill Exp $
# ARMv7 customization script used by mkimage
#
board=armv7
console=fb
resize=true

. ${DIR}/conf/evbarm.conf

# altera, exynos, sunxi, tegra
kernels_generic="GENERIC"
# non-FDTised / special kernels
kernels_beagle="BEAGLEBOARD BEAGLEBONE"
kernels_rpi="RPI2"

make_label() {
	make_label_evbarm
}

make_fstab() {
	make_fstab_evbarm
}

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

populate_common() {
	# Install EFI bootloader
	if [ -f "${release}/usr/mdec/bootarm.efi" ]; then
		mkdir -p "${mnt}/boot/EFI/BOOT"
		cp "${release}/usr/mdec/bootarm.efi" "${mnt}/boot/EFI/BOOT/bootarm.efi"

		# Install GENERIC kernel to root of the FFS partition
		${GZIP_CMD} -dc ${kernel}/netbsd-${kernels_generic}.gz > "${mnt}/netbsd"
		echo "./netbsd type=file uname=root gname=wheel mode=0755" \
		    >> "$tmp/selected_sets"
	fi

	# Install boot script
	cp ${DIR}/files/armv7_boot.cmd ${mnt}/boot/boot.cmd
	"${MKUBOOTIMAGE}" -A arm -C none -O netbsd -T script -a 0 -n "NetBSD/armv7 boot" "${mnt}/boot/boot.cmd" "${mnt}/boot/boot.scr"
}

populate_beagle() {
	:
}

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.img" ]; then
		echo "${bar} renaming netbsd-RPI2.img to kernel7.img ${bar}"
		mv "${mnt}/boot/netbsd-RPI2.img" "${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

		cat > ${mnt}/boot/config.txt << EOF
# UART settings, see https://www.raspberrypi.org/documentation/configuration/uart.md
enable_uart=1
force_turbo=0
EOF

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

	fi

	#
	# 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_altera() {
	:
}

populate_amlogic() {
	odroidc1_kernelimg=netbsd-GENERIC.ub

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

setenv bootargs "awge0.mac-address=\${ethaddr}"
setenv bootcmd "fatload mmc 0:1 0x21000000 ${odroidc1_kernelimg}; fatload mmc 0:1 0x20000000 meson8b-odroidc1.dtb; bootm 0x21000000 - 0x20000000"
run bootcmd
EOF
}

populate_sunxi() {
	:
}

populate_tegra() {
	:
}

populate() {
	echo "${bar} looking for kernels in ${kernel} ${bar}"
	kernels=""
	# .ub kernels
	for k in $kernels_generic $kernels_beagle; do
		f="${kernel}/netbsd-${k}.ub.gz"
		test -f "${f}" && kernels="${kernels} ${f}"
	done
	# .img kernels
	for k in $kernels_rpi; do
		f="${kernel}/netbsd-${k}.img.gz"
		test -f "${f}" && kernels="${kernels} ${f}"
	done
	# .dtb files
	for k in $kernels_generic $kernels_rpi $; do
		test -d "${KERNOBJDIR}/${k}" && \
		    dtbs="$(${MAKE} -C ${KERNOBJDIR}/${k} -v DTB)" || \
		    dtbs=
		for dtb in $dtbs; do
			f="${kernel}/${dtb}.gz"
			test -f "${f}" && kernels="${kernels} ${f}"
		done
	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 ||
			fail "Copy of ${k} to ${mnt}/boot/${tgt} failed"
	done

	# board specific configuration
	populate_altera
	populate_amlogic
	populate_beagle
	populate_rpi
	populate_sunxi
	populate_tegra

	# common configuration
	populate_common
}
