xref: /freebsd/release/tools/arm.subr (revision 994297b01b98816bea1abf45ae4bac1bc69ee7a0)
1#!/bin/sh
2#-
3# Copyright (c) 2015-2017 The FreeBSD Foundation
4# All rights reserved.
5#
6# Portions of this software were developed by Glen Barber
7# under sponsorship from the FreeBSD Foundation.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29#
30# Common subroutines used to build arm, arm64, or RISC-V SD card images.
31#
32# $FreeBSD$
33#
34
35cleanup() {
36	if [ -c "${DESTDIR}/dev/null" ]; then
37		umount_loop ${DESTDIR}/dev 2>/dev/null
38	fi
39	umount_loop ${DESTDIR}
40	if [ ! -z "${mddev}" ]; then
41		mdconfig -d -u ${mddev}
42	fi
43
44	return 0
45}
46
47umount_loop() {
48	DIR=$1
49	i=0
50	sync
51	while ! umount ${DIR}; do
52		i=$(( $i + 1 ))
53		if [ $i -ge 10 ]; then
54			# This should never happen.  But, it has happened.
55			echo "Cannot umount(8) ${DIR}"
56			echo "Something has gone horribly wrong."
57			return 1
58		fi
59		sleep 1
60	done
61
62	return 0
63}
64
65arm_create_disk() {
66	# Create the target raw file and temporary work directory.
67	chroot ${CHROOTDIR} gpart create -s ${PART_SCHEME} ${mddev}
68	if [ "${PART_SCHEME}" = "GPT" ]; then
69		chroot ${CHROOTDIR} gpart add -t efi -l efi -a 512k -s ${FAT_SIZE} ${mddev}
70		chroot ${CHROOTDIR} newfs_msdos -L efi -F ${FAT_TYPE} /dev/${mddev}p1
71		chroot ${CHROOTDIR} gpart add -t freebsd-ufs -l rootfs -a 64k ${mddev}
72		chroot ${CHROOTDIR} newfs -U -L rootfs /dev/${mddev}p2
73	fi
74	if [ "${PART_SCHEME}" = "MBR" ]; then
75		chroot ${CHROOTDIR} gpart add -t '!12' -a 512k -s ${FAT_SIZE} ${mddev}
76		chroot ${CHROOTDIR} gpart set -a active -i 1 ${mddev}
77		chroot ${CHROOTDIR} newfs_msdos -L msdosboot -F ${FAT_TYPE} /dev/${mddev}s1
78		chroot ${CHROOTDIR} gpart add -t freebsd ${mddev}
79		chroot ${CHROOTDIR} gpart create -s bsd ${mddev}s2
80		chroot ${CHROOTDIR} gpart add -t freebsd-ufs -a 64k /dev/${mddev}s2
81		chroot ${CHROOTDIR} newfs -U -L rootfs /dev/${mddev}s2a
82	fi
83
84	return 0
85}
86
87arm_create_user() {
88	# Create a default user account 'freebsd' with the password 'freebsd',
89	# and set the default password for the 'root' user to 'root'.
90	chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
91		groupadd freebsd -g 1001
92	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/home/freebsd
93	chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
94		useradd freebsd \
95		-m -M 0755 -w yes -n freebsd -u 1001 -g 1001 -G 0 \
96		-c 'FreeBSD User' -d '/home/freebsd' -s '/bin/csh'
97	chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
98		usermod root -w yes
99
100	return 0
101}
102
103arm_setup_usb_otg() {
104	# Set up virtual serial port over USB OTG / device mode.
105	echo >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
106	echo '# Required for USB OTG virtual serial port.' \
107		>> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
108	echo 'notify 100 {' \
109		>> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
110	echo '	match "system"		"DEVFS";' \
111		>> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
112	echo '	match "subsystem"	"CDEV";' \
113		>> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
114	echo '	match "type"		"CREATE";' \
115		>> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
116	echo '	match "cdev"		"ttyU[0-9]+";' \
117		>> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
118	echo '	action "/sbin/init q";' \
119		>> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
120	echo '};' \
121		>> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
122
123	echo '# USB OTG virtual serial port' \
124		>> ${CHROOTDIR}/${DESTDIR}/etc/ttys
125	echo 'ttyU0	"/usr/libexec/getty 3wire"	vt100	onifconsole  secure' \
126		>> ${CHROOTDIR}/${DESTDIR}/etc/ttys
127	echo 'ttyU1	"/usr/libexec/getty 3wire"	vt100	onifconsole  secure' \
128		>> ${CHROOTDIR}/${DESTDIR}/etc/ttys
129
130	echo '# Configure USB OTG; see usb_template(4).' \
131		>> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
132	echo 'hw.usb.template=3' \
133		>> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
134	echo 'umodem_load="YES"' \
135	     >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
136}
137
138arm64_setup_multicons() {
139	if [ "${EMBEDDED_TARGET_ARCH}" != "aarch64" ]; then
140		return
141	fi
142
143	echo '# Multiple console (serial+efi gop) enabled.' \
144		>> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
145	echo 'boot_multicons="YES"' \
146	     >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
147	echo 'boot_serial="YES"' \
148	     >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
149}
150
151arm_setup_fdt_overlays() {
152	if [ -z "${FDT_OVERLAYS}" ]; then
153		return
154	fi
155
156	echo '# DTB OVERLAYS' \
157		>> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
158	echo "fdt_overlays=\"${FDT_OVERLAYS}\"" \
159	     >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
160}
161
162arm_setup_minimal_loader() {
163	echo '# Disable the beastie menu and color' \
164		>> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
165	echo 'beastie_disable="YES"' \
166		>> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
167	echo 'loader_color="NO"' \
168		>> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
169}
170
171arm_install_base() {
172	if [ "${PART_SCHEME}" = "GPT" ]; then
173		chroot ${CHROOTDIR} mount /dev/${mddev}p2 ${DESTDIR}
174	fi
175	if [ "${PART_SCHEME}" = "MBR" ]; then
176		chroot ${CHROOTDIR} mount /dev/${mddev}s2a ${DESTDIR}
177	fi
178	_OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
179	REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
180	BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
181	export UNAME_r=${REVISION}-${BRANCH}
182	eval chroot ${CHROOTDIR} make -C ${WORLDDIR} \
183		TARGET=${EMBEDDED_TARGET} \
184		TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
185		DESTDIR=${DESTDIR} KERNCONF=${KERNEL} \
186		${CONF_FILES} installworld installkernel distribution
187	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/boot/efi
188	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/boot/msdos
189
190	arm_create_user
191	arm_setup_usb_otg
192	arm64_setup_multicons
193	arm_setup_fdt_overlays
194	arm_setup_minimal_loader
195	arm_do_quirk
196
197	echo '# Custom /etc/fstab for FreeBSD embedded images' \
198		> ${CHROOTDIR}/${DESTDIR}/etc/fstab
199	if [ "${PART_SCHEME}" = "GPT" ]; then
200		echo "/dev/ufs/rootfs		/		ufs	rw		1	1" \
201			>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
202		echo "/dev/msdosfs/EFI		/boot/efi	msdosfs	rw,noatime	0	0" \
203			>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
204	fi
205	if [ "${PART_SCHEME}" = "MBR" ]; then
206		echo "/dev/ufs/rootfs		/		ufs	rw		1	1" \
207			>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
208		echo "/dev/msdosfs/MSDOSBOOT	/boot/msdos 	msdosfs	rw,noatime	0	0" \
209			>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
210	fi
211	echo "tmpfs			/tmp		tmpfs	rw,mode=1777	0	0" \
212		>> ${CHROOTDIR}/${DESTDIR}/etc/fstab
213
214	local hostname
215	hostname="$(echo ${KERNEL} | tr '[:upper:]' '[:lower:]')"
216	echo "hostname=\"${hostname}\"" > ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
217	echo 'ifconfig_DEFAULT="DHCP"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
218	echo 'sshd_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
219	echo 'sendmail_enable="NONE"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
220	echo 'sendmail_submit_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
221	echo 'sendmail_outbound_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
222	echo 'sendmail_msp_queue_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
223	echo 'growfs_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
224
225	sync
226	umount_loop ${CHROOTDIR}/${DESTDIR}
227
228	return 0
229}
230
231arm_install_boot() {
232	FATMOUNT="${DESTDIR%${KERNEL}}/fat"
233	UFSMOUNT="${DESTDIR%${KERNEL}}/ufs"
234	chroot ${CHROOTDIR} mkdir -p "${FATMOUNT}" "${UFSMOUNT}"
235	if [ "${PART_SCHEME}" = "GPT" ]; then
236		dospart="/dev/${mddev}p1"
237		ufspart="/dev/${mddev}p2"
238	fi
239	if [ "${PART_SCHEME}" = "MBR" ]; then
240		dospart="/dev/${mddev}s1"
241		ufspart="/dev/${mddev}s2a"
242	fi
243
244	chroot ${CHROOTDIR} mount_msdosfs ${dospart} ${FATMOUNT}
245	chroot ${CHROOTDIR} mount ${ufspart} ${UFSMOUNT}
246
247	BOOTFILES="$(chroot ${CHROOTDIR} \
248		env TARGET=${EMBEDDED_TARGET} TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
249		WITH_UNIFIED_OBJDIR=yes \
250		make -C ${WORLDDIR}/stand -V .OBJDIR)"
251	BOOTFILES="$(chroot ${CHROOTDIR} realpath ${BOOTFILES})"
252
253	chroot ${CHROOTDIR} mkdir -p ${FATMOUNT}/EFI/BOOT
254	chroot ${CHROOTDIR} cp -p ${BOOTFILES}/efi/loader_lua/loader_lua.efi \
255		${FATMOUNT}/EFI/BOOT/$(efi_boot_name ${EMBEDDED_TARGET})
256
257	chroot ${CHROOTDIR} cp -R ${UFSMOUNT}/boot/dtb ${FATMOUNT}
258
259	chroot ${CHROOTDIR} touch ${UFSMOUNT}/firstboot
260	sync
261	umount_loop ${CHROOTDIR}/${FATMOUNT}
262	umount_loop ${CHROOTDIR}/${UFSMOUNT}
263	chroot ${CHROOTDIR} rmdir ${FATMOUNT}
264	chroot ${CHROOTDIR} rmdir ${UFSMOUNT}
265}
266
267arm_install_uboot() {
268	# Override in the ${EMBEDDED_TARGET}/${BOARDNAME}.conf file.
269
270	return 0
271}
272
273arm_do_quirk() {
274	# Override in the ${EMBEDDED_TARGET}/${BOARDNAME}.conf file.
275}
276