# $NetBSD: arm64.conf,v 1.6 2019/03/03 11:44:18 jmcneill Exp $
# ARM64 customization script used by mkimage
#
board=arm64
console=fb
resize=true

. ${DIR}/conf/evbarm.conf

kernel_GENERIC64="GENERIC64"

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)
ec2_init=\$(dev_exists ena0)
EOF
}

populate_common() {
	# Add EC2 init script
	cp ${DIR}/files/ec2_init ${mnt}/etc/rc.d/ec2_init
	echo "./etc/rc.d/ec2_init type=file uname=root gname=wheel mode=0555" \
	    >> "$tmp/selected_sets"

	# Rename kernel to netbsd.img
	mv "${mnt}/boot/netbsd-${kernel_GENERIC64}.img" "${mnt}/boot/netbsd.img"

	# Install EFI bootloader
	mkdir -p "${mnt}/boot/EFI/BOOT"
	cp "${release}/usr/mdec/bootaa64.efi" "${mnt}/boot/EFI/BOOT/bootaa64.efi"

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

populate_allwinner() {
	# U-Boot expects 64-bit DTB files to live in an allwinner/ subdirectory
	mkdir -p "${mnt}/boot/dtb/allwinner"
	mv "${mnt}"/boot/sun50i-*.dtb "${mnt}/boot/dtb/allwinner/"
}

populate_amlogic() {
	# U-Boot expects 64-bit DTB files to live in an amlogic/ subdirectory
	mkdir -p "${mnt}/boot/dtb/amlogic"
	mv "${mnt}"/boot/meson-*.dtb "${mnt}/boot/dtb/amlogic/"
}

populate_rockchip() {
	# U-Boot expects 64-bit DTB files to live in a rockchip/ subdirectory
	mkdir -p "${mnt}/boot/dtb/rockchip"
	mv "${mnt}"/boot/rk3328-*.dtb "${mnt}/boot/dtb/rockchip/"
	mv "${mnt}"/boot/rk3399-*.dtb "${mnt}/boot/dtb/rockchip/"
}

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

	cat > "${mnt}/boot/cmdline.txt" << EOF
root=ld0a console=${console}
EOF

	cat > "${mnt}/boot/config.txt" << EOF
arm_64bit=1
kernel=netbsd.img
kernel_address=0x200000
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
	)
}

populate_nvidia() {
	# Move tegra dtb files to /boot/dtb
	mkdir -p "${mnt}/boot/dtb"
	mv "${mnt}"/boot/tegra*.dtb "${mnt}/boot/dtb/"
}

populate() {
	echo "${bar} looking for kernel in ${kernel} ${bar}"
	kernels=""
	k="$kernel_GENERIC64"

	# .imgkernel
	f="${kernel}/netbsd-${k}.img.gz"
	test -f "${f}" && kernels="${kernels} ${f}"

	# .dtb files
	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

	# install files 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

	# SoC specific configuration
	populate_allwinner
	populate_amlogic
	populate_nvidia
	populate_rockchip

	# Board specific configuration
	populate_rpi

	# common configuration
	populate_common
}
