# $NetBSD: armv7.conf,v 1.50 2026/04/26 12:59:13 tsutsui Exp $
# ARMv7 customization script used by mkimage
#
board=armv7
console=fb
resize=true

. ${DIR}/conf/evbarm.conf

kernels_generic="GENERIC"

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() {
	if [ -f "${release}/usr/mdec/bootarm.efi" ]; then
		# Install EFI bootloader
		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"

		cp ${DIR}/files/armv7_boot.cmd ${mnt}/boot/boot.cmd
		cp ${DIR}/files/boot.cfg ${mnt}/boot/boot.cfg
	else
		# Fallback to non-EFI boot command
		cp ${DIR}/files/armv7_boot_nonefi.cmd ${mnt}/boot/boot.cmd
	fi

	# Install boot script
	[ -n "${MKREPRO_TIMESTAMP}" ] && \
	    MKUBOOTIMAGEARGS="-t ${MKREPRO_TIMESTAMP}"
	"${MKUBOOTIMAGE}" -A arm -C none -O netbsd -T script \
	    ${MKUBOOTIMAGEARGS} -a 0 -n "NetBSD/armv7 boot" \
	    "${mnt}/boot/boot.cmd" "${mnt}/boot/boot.scr"
}

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

	# The GENERIC .img kernel img is used for RPI2
	if [ -f "${mnt}/boot/netbsd-GENERIC.img" ]; then
		cat > "${mnt}/boot/cmdline.txt" << EOF
# use "root=ld1a" for RPI4
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
#
upstream_kernel=1
os_prefix=dtb/broadcom/
cmdline=/cmdline.txt
kernel=/netbsd-GENERIC.img
# Boot options, see https://www.raspberrypi.com/documentation/computers/config_txt.html#boot-options
kernel_address=0x01000040
# UART settings, see https://www.raspberrypi.com/documentation/computers/configuration.html#configuring-uarts
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_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 dtb/meson8b-odroidc1.dtb; bootm 0x21000000 - 0x20000000"
run bootcmd
EOF
}

populate() {
	echo "${bar} looking for kernels in ${kernel} ${bar}"
	kernels=""
	# .ub kernels
	for k in $kernels_generic; do
		f="${kernel}/netbsd-${k}.ub.gz"
		test -f "${f}" && kernels="${kernels} ${f}"
	done
	# .img kernels
	for k in $kernels_generic; do
		f="${kernel}/netbsd-${k}.img.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 ||
			fail "Copy of ${k} to ${mnt}/boot/${tgt} failed"
	done

	# board specific configuration
	populate_amlogic
	populate_rpi

	# common configuration
	populate_common
}
