# $NetBSD: rpi_inst.conf,v 1.11 2017/12/14 06:29:15 skrll 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
boot=112
init=8

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

make_label() {
	# compute all sizes in terms of sectors
	local totalsize=$(( ${newsize} * 1024 * 2 / 512 ))

	local swapsize=$(( ${swap} * 1024 ))
	local bootsize=$(( ${boot} * 1024 ))

	local bootoffset=$(( ${init} * 1024 ))
	local swapoffset=$(( ${bootoffset} + ${bootsize} ))

	local asize=$(( ${totalsize} - ${swapsize} - ${bootsize} - ${bootoffset} ))
	local aoffset=$(( ${swapoffset} + ${swapsize} ))

	local bps=512
	local spt=32
	local tpc=64
	local spc=2048
	local cylinders=$(( ${totalsize} / ${spc} ))

	cat << EOF
type: SCSI
disk: STORAGE DEVICE
label: fictitious
flags: removable
bytes/sector: ${bps}
sectors/track: ${spt}
tracks/cylinder: ${tpc}
sectors/cylinder: ${spc}
cylinders: ${cylinders}
total sectors: ${totalsize}
rpm: 3600
interleave: 1
trackskew: 0
cylinderskew: 0
headswitch: 0           # microseconds
track-to-track seek: 0  # microseconds
drivedata: 0 

8 partitions:
#     size         offset        fstype [fsize bsize cpg/sgs]
 a:   ${asize}     ${aoffset}    4.2BSD  ${fsize} ${bsize} 0  # 
 b:   ${swapsize}  ${swapoffset} swap                         #
 d:   ${totalsize} 0             unused      0     0          #
 e:   ${bootsize}  ${bootoffset} MSDOS                        #
EOF
}

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

make_fstab() {
	:
}

firmwaredir=$src/external/broadcom/rpi-firmware/dist
firmwarefiles="LICENCE.broadcom bootcode.bin fixup.dat fixup_cd.dat start.elf start_cd.elf"
dtbs="$(${MAKE} -C ${kerneldir} -v DTB)"

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
# UART settings, see https://www.raspberrypi.org/documentation/configuration/uart.md
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)
		${GZIP_CMD} -dc ${kernel} > ${mnt}/boot/kernel.img
		;;
	*)
		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
		for f in ${dtbs}; do
			echo " $f"
			cp ${kerneldir}/${f} . || exit 1
		done
	) || fail "Copy of firmware to ${mnt}/boot failed"

}
