1282566Sgjb#!/bin/sh
2282566Sgjb#-
3282566Sgjb# Copyright (c) 2015 The FreeBSD Foundation
4282566Sgjb# All rights reserved.
5282566Sgjb#
6282566Sgjb# Portions of this software were developed by Glen Barber
7282566Sgjb# under sponsorship from the FreeBSD Foundation.
8282566Sgjb#
9282566Sgjb# Redistribution and use in source and binary forms, with or without
10282566Sgjb# modification, are permitted provided that the following conditions
11282566Sgjb# are met:
12282566Sgjb# 1. Redistributions of source code must retain the above copyright
13282566Sgjb#    notice, this list of conditions and the following disclaimer.
14282566Sgjb# 2. Redistributions in binary form must reproduce the above copyright
15282566Sgjb#    notice, this list of conditions and the following disclaimer in the
16282566Sgjb#    documentation and/or other materials provided with the distribution.
17282566Sgjb#
18282566Sgjb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19282566Sgjb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20282566Sgjb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21282566Sgjb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22282566Sgjb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23282566Sgjb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24282566Sgjb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25282566Sgjb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26282566Sgjb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27282566Sgjb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28282566Sgjb# SUCH DAMAGE.
29282566Sgjb#
30282566Sgjb# Common subroutines used to build arm/armv6 images.
31282566Sgjb#
32282566Sgjb# $FreeBSD: releng/10.2/release/tools/arm.subr 285609 2015-07-15 18:18:07Z gjb $
33282566Sgjb#
34282566Sgjb
35282566Sgjbcleanup() {
36282566Sgjb	if [ -c "${DESTDIR}/dev/null" ]; then
37282566Sgjb		umount_loop ${DESTDIR}/dev 2>/dev/null
38282566Sgjb	fi
39282566Sgjb	umount_loop ${DESTDIR}
40282566Sgjb	if [ ! -z "${mddev}" ]; then
41282566Sgjb		mdconfig -d -u ${mddev}
42282566Sgjb	fi
43282566Sgjb
44282566Sgjb	return 0
45282566Sgjb}
46282566Sgjb
47282566Sgjbumount_loop() {
48282566Sgjb	DIR=$1
49282566Sgjb	i=0
50282566Sgjb	sync
51282566Sgjb	while ! umount ${DIR}; do
52282566Sgjb		i=$(( $i + 1 ))
53282566Sgjb		if [ $i -ge 10 ]; then
54282566Sgjb			# This should never happen.  But, it has happened.
55282566Sgjb			echo "Cannot umount(8) ${DIR}"
56282566Sgjb			echo "Something has gone horribly wrong."
57282566Sgjb			return 1
58282566Sgjb		fi
59282566Sgjb		sleep 1
60282566Sgjb	done
61282566Sgjb
62282566Sgjb	return 0
63282566Sgjb}
64282566Sgjb
65282566Sgjbarm_create_disk() {
66282566Sgjb	# Create the target raw file and temporary work directory.
67282693Sgjb	chroot ${CHROOTDIR} gpart create -s ${PART_SCHEME} ${mddev}
68282693Sgjb	chroot ${CHROOTDIR} gpart add -t '!12' -a 63 -s ${FAT_SIZE} ${mddev}
69282693Sgjb	chroot ${CHROOTDIR} gpart set -a active -i 1 ${mddev}
70282693Sgjb	chroot ${CHROOTDIR} newfs_msdos -L msdosboot -F ${FAT_TYPE} /dev/${mddev}s1
71282693Sgjb	chroot ${CHROOTDIR} gpart add -t freebsd ${mddev}
72282693Sgjb	chroot ${CHROOTDIR} gpart create -s bsd ${mddev}s2
73282693Sgjb	chroot ${CHROOTDIR} gpart add -t freebsd-ufs -a 64k /dev/${mddev}s2
74282693Sgjb	chroot ${CHROOTDIR} newfs -U -L rootfs /dev/${mddev}s2a
75284155Sgjb	chroot ${CHROOTDIR} tunefs -N enable /dev/${mddev}s2a
76282566Sgjb
77282566Sgjb	return 0
78282566Sgjb}
79282566Sgjb
80282693Sgjbarm_create_user() {
81282693Sgjb	# Create a default user account 'freebsd' with the password 'freebsd',
82282693Sgjb	# and set the default password for the 'root' user to 'root'.
83285115Sgjb	chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
84284154Sgjb		groupadd freebsd -g 1001
85284154Sgjb	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/home/freebsd
86285115Sgjb	chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
87284154Sgjb		useradd freebsd \
88282693Sgjb		-m -M 0755 -w yes -n freebsd -u 1001 -g 1001 -G 0 \
89285115Sgjb		-c 'FreeBSD User' -d '/home/freebsd' -s '/bin/csh'
90285115Sgjb	chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
91284154Sgjb		usermod root -w yes
92282693Sgjb
93282693Sgjb	return 0
94282693Sgjb}
95282693Sgjb
96282566Sgjbarm_install_base() {
97282693Sgjb	chroot ${CHROOTDIR} mount /dev/${mddev}s2a ${DESTDIR}
98282693Sgjb	eval chroot ${CHROOTDIR} make -C ${WORLDDIR} \
99282693Sgjb		TARGET=${EMBEDDED_TARGET} \
100282693Sgjb		TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
101282693Sgjb		DESTDIR=${DESTDIR} KERNCONF=${KERNEL} \
102282693Sgjb		installworld installkernel distribution
103282693Sgjb	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/boot/msdos
104282566Sgjb
105282693Sgjb	arm_create_user
106282693Sgjb
107282566Sgjb	echo '# Custom /etc/fstab for FreeBSD embedded images' \
108282693Sgjb		> ${CHROOTDIR}/${DESTDIR}/etc/fstab
109282693Sgjb	echo "/dev/ufs/rootfs   /       ufs     rw      1       1" \
110282693Sgjb		>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
111282566Sgjb	echo "/dev/msdosfs/MSDOSBOOT /boot/msdos msdosfs rw,noatime 0 0" \
112282693Sgjb		>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
113282566Sgjb	echo "md /tmp mfs rw,noatime,-s30m 0 0" \
114282693Sgjb		>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
115282566Sgjb
116282566Sgjb	local hostname
117282566Sgjb	hostname="$(echo ${KERNEL} | tr '[:upper:]' '[:lower:]')"
118282693Sgjb	echo "hostname=\"${hostname}\"" > ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
119282693Sgjb	echo 'ifconfig_DEFAULT="DHCP"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
120282693Sgjb	echo 'sshd_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
121282693Sgjb	echo 'sendmail_enable="NONE"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
122282693Sgjb	echo 'sendmail_submit_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
123282693Sgjb	echo 'sendmail_outbound_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
124282693Sgjb	echo 'sendmail_msp_queue_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
125282693Sgjb	echo 'growfs_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
126282566Sgjb
127282566Sgjb	sync
128282693Sgjb	umount_loop ${CHROOTDIR}/${DESTDIR}
129282566Sgjb
130282566Sgjb	return 0
131282566Sgjb}
132282566Sgjb
133282566Sgjbarm_install_uboot() {
134282566Sgjb	# Override in the arm/KERNEL.conf file.
135282566Sgjb
136282566Sgjb	return 0
137282566Sgjb}
138