1282566Sgjb#!/bin/sh
2282566Sgjb#-
3325899Sgjb# Copyright (c) 2015-2017 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#
30325899Sgjb# Common subroutines used to build arm SD card images.
31282566Sgjb#
32282566Sgjb# $FreeBSD: stable/10/release/tools/arm.subr 325899 2017-11-16 16:00:01Z 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}
68325899Sgjb	chroot ${CHROOTDIR} gpart add -t '!12' -a 512k -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
75282566Sgjb
76282566Sgjb	return 0
77282566Sgjb}
78282566Sgjb
79282693Sgjbarm_create_user() {
80282693Sgjb	# Create a default user account 'freebsd' with the password 'freebsd',
81282693Sgjb	# and set the default password for the 'root' user to 'root'.
82285115Sgjb	chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
83284154Sgjb		groupadd freebsd -g 1001
84284154Sgjb	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/home/freebsd
85285115Sgjb	chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
86284154Sgjb		useradd freebsd \
87282693Sgjb		-m -M 0755 -w yes -n freebsd -u 1001 -g 1001 -G 0 \
88285115Sgjb		-c 'FreeBSD User' -d '/home/freebsd' -s '/bin/csh'
89285115Sgjb	chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
90284154Sgjb		usermod root -w yes
91282693Sgjb
92282693Sgjb	return 0
93282693Sgjb}
94282693Sgjb
95282566Sgjbarm_install_base() {
96282693Sgjb	chroot ${CHROOTDIR} mount /dev/${mddev}s2a ${DESTDIR}
97282693Sgjb	eval chroot ${CHROOTDIR} make -C ${WORLDDIR} \
98282693Sgjb		TARGET=${EMBEDDED_TARGET} \
99282693Sgjb		TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
100282693Sgjb		DESTDIR=${DESTDIR} KERNCONF=${KERNEL} \
101282693Sgjb		installworld installkernel distribution
102282693Sgjb	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/boot/msdos
103282566Sgjb
104282693Sgjb	arm_create_user
105282693Sgjb
106282566Sgjb	echo '# Custom /etc/fstab for FreeBSD embedded images' \
107282693Sgjb		> ${CHROOTDIR}/${DESTDIR}/etc/fstab
108282693Sgjb	echo "/dev/ufs/rootfs   /       ufs     rw      1       1" \
109282693Sgjb		>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
110282566Sgjb	echo "/dev/msdosfs/MSDOSBOOT /boot/msdos msdosfs rw,noatime 0 0" \
111282693Sgjb		>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
112291417Sgjb	echo "tmpfs /tmp tmpfs rw,mode=1777,size=30m 0 0" \
113282693Sgjb		>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
114282566Sgjb
115282566Sgjb	local hostname
116282566Sgjb	hostname="$(echo ${KERNEL} | tr '[:upper:]' '[:lower:]')"
117282693Sgjb	echo "hostname=\"${hostname}\"" > ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
118282693Sgjb	echo 'ifconfig_DEFAULT="DHCP"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
119282693Sgjb	echo 'sshd_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
120282693Sgjb	echo 'sendmail_enable="NONE"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
121282693Sgjb	echo 'sendmail_submit_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
122282693Sgjb	echo 'sendmail_outbound_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
123282693Sgjb	echo 'sendmail_msp_queue_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
124282693Sgjb	echo 'growfs_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
125282566Sgjb
126282566Sgjb	sync
127282693Sgjb	umount_loop ${CHROOTDIR}/${DESTDIR}
128282566Sgjb
129282566Sgjb	return 0
130282566Sgjb}
131282566Sgjb
132282566Sgjbarm_install_uboot() {
133282566Sgjb	# Override in the arm/KERNEL.conf file.
134282566Sgjb
135282566Sgjb	return 0
136282566Sgjb}
137