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