# $NetBSD: evbarm.conf,v 1.27 2017/05/13 10:44:58 hubertf Exp $
# evbarm shared config
#
image=$HOME/${board}.img

MACHINE=evbarm

swap=256
extra=48		# spare space
init=8
boot=$((192 - ${init}))
ffsoffset=$(( (${init} + ${boot} + ${swap}) / 2 ))m

size=0		# autocompute
msdosid=12

mkdir -p ${mnt}/boot

make_label_evbarm() {
	# 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                         #
 c:   ${totalsize} 0             unused      0     0          #
 e:   ${bootsize}  ${bootoffset} MSDOS                        #
EOF
}

make_fstab_evbarm_normal() {
	cat > ${mnt}/etc/fstab << EOF
# NetBSD /etc/fstab
# See /usr/share/examples/fstab/ for more examples.
/dev/ld0a	/		ffs	rw,noatime	1 1
/dev/ld0b	none		swap	sw	0 0
/dev/ld0e	/boot		msdos	rw	1 1
kernfs		/kern		kernfs	rw
ptyfs		/dev/pts	ptyfs	rw
procfs		/proc		procfs	rw
tmpfs		/var/shm	tmpfs	rw,-m1777,-sram%25
EOF
}

# From Richard Neswold's:
# http://rich-tbp.blogspot.com/2013/03/netbsd-on-rpi-minimizing-disk-writes.html
# Also for the postfix stuff below
make_fstab_evbarm_minwrites() {
	cat > ${mnt}/etc/fstab << EOF
# NetBSD /etc/fstab
# See /usr/share/examples/fstab/ for more examples.
/dev/ld0a	/			ffs	rw,log,noatime,nodevmtime 1 1
/dev/ld0b	none			swap	sw			  0 0
/dev/ld0e	/boot			msdos	rw			  1 1
kernfs		/kern			kernfs	rw
ptyfs		/dev/pts		ptyfs	rw
procfs		/proc			procfs	rw
tmpfs		/tmp			tmpfs	rw,-s32M
tmpfs		/var/log		tmpfs	rw,union,-s32M
tmpfs		/var/run		tmpfs	rw,union,-s1M
tmpfs		/var/mail		tmpfs	rw,union,-s10M
tmpfs		/var/spool/postfix	tmpfs	rw,union,-s20M
tmpfs		/var/db/postfix		tmpfs	rw,union,-s1M
tmpfs		/var/chroot		tmpfs	rw,union,-s10M
tmpfs		/var/shm		tmpfs	rw,-m1777,-sram%25
EOF
}

make_fstab_evbarm() {
	if $minwrites; then
		make_fstab_evbarm_minwrites
	else
		make_fstab_evbarm_normal
	fi
	echo "./etc/fstab type=file uname=root gname=wheel mode=0644" \
	    >> "$tmp/selected_sets"

	# Missing mount points from fstab
	echo "./proc type=dir uname=root gname=wheel mode=0755" \
	    >> "$tmp/selected_sets"
	echo "./kern type=dir uname=root gname=wheel mode=0755" \
	    >> "$tmp/selected_sets"
}

customize_evbarm() {
	if $minwrites; then
		mkdir ${mnt}/etc/postfix
		(umask 022
		sed -e 's/fifo/unix/' < ${release}/etc/postfix/master.cf > \
		    ${mnt}/etc/postfix/master.cf)
	fi
	
	cp ${release}/etc/rc.conf ${mnt}/etc/rc.conf
	cat >> ${mnt}/etc/rc.conf << EOF
rc_configured=YES
hostname=${board}
sshd=YES
dhcpcd=YES
ntpd=YES
ntpd_flags="-g"
EOF

	if $resize; then
		cat >> ${mnt}/etc/rc.conf << EOF
resize_disklabel=YES
resize_disklabel_disk=ld0
resize_disklabel_part=a
resize_root=YES
resize_root_flags="-p"
resize_root_postcmd="/sbin/reboot -n"
EOF
	fi

	echo "./etc/rc.conf type=file uname=root gname=wheel mode=0644" \
	    >> "$tmp/selected_sets"

	mkdir ${mnt}/etc/rc.d
	cp ${DIR}/files/resize_disklabel ${mnt}/etc/rc.d/resize_disklabel
	echo "./etc/rc.d/resize_disklabel type=file uname=root gname=wheel mode=0555" \
	    >> "$tmp/selected_sets"

	if [ ! -f ${release}/dev/MAKEDEV ]; then
		echo ${PROG}: Missing ${release}/dev/MAKEDEV 1>&2
		exit 1
	fi
	echo "${bar} running MAKEDEV ${bar}"
	${HOST_SH} ${release}/dev/MAKEDEV -s all | sed -e 's:^\./:\./dev/:' \
	    >> "$tmp/selected_sets"

	echo "${bar} fixing up permissions"
	echo "./boot type=dir uname=root gname=wheel mode=0755" \
	    >> "$tmp/selected_sets"
}
