3 # Copyright (c) 2015-2017 The FreeBSD Foundation
6 # Portions of this software were developed by Glen Barber
7 # under sponsorship from the FreeBSD Foundation.
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
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.
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
30 # Common subroutines used to build arm, arm64, or RISC-V SD card images.
36 if [ -c "${DESTDIR}/dev/null" ]; then
37 umount_loop ${DESTDIR}/dev 2>/dev/null
39 umount_loop ${DESTDIR}
40 if [ ! -z "${mddev}" ]; then
41 mdconfig -d -u ${mddev}
51 while ! umount ${DIR}; do
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."
66 if [ $(sysctl -n kern.geom.part.mbr.enforce_chs) != 0 ]; then
70 # Create the target raw file and temporary work directory.
71 chroot ${CHROOTDIR} gpart create -s ${PART_SCHEME} ${mddev}
72 if [ "${PART_SCHEME}" = "GPT" ]; then
73 chroot ${CHROOTDIR} gpart add -t efi -l efi -a 512k -s ${FAT_SIZE} ${mddev}
74 chroot ${CHROOTDIR} newfs_msdos -L efi -F ${FAT_TYPE} /dev/${mddev}p1
75 chroot ${CHROOTDIR} gpart add -t freebsd-ufs -l rootfs -a 64k ${mddev}
76 chroot ${CHROOTDIR} newfs -U -L rootfs /dev/${mddev}p2
78 if [ "${PART_SCHEME}" = "MBR" ]; then
79 chroot ${CHROOTDIR} gpart add -t '!12' -a 512k -s ${FAT_SIZE} ${mddev}
80 chroot ${CHROOTDIR} gpart set -a active -i 1 ${mddev}
81 chroot ${CHROOTDIR} newfs_msdos -L efi -F ${FAT_TYPE} /dev/${mddev}s1
82 chroot ${CHROOTDIR} gpart add -t freebsd ${mddev}
83 chroot ${CHROOTDIR} gpart create -s bsd ${mddev}s2
84 chroot ${CHROOTDIR} gpart add -t freebsd-ufs -a 64k -b 64k ${mddev}s2
85 chroot ${CHROOTDIR} newfs -U -L rootfs /dev/${mddev}s2a
92 # Create a default user account 'freebsd' with the password 'freebsd',
93 # and set the default password for the 'root' user to 'root'.
94 chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
95 groupadd freebsd -g 1001
96 chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/home/freebsd
97 chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
99 -m -M 0755 -w yes -n freebsd -u 1001 -g 1001 -G 0 \
100 -c 'FreeBSD User' -d '/home/freebsd' -s '/bin/csh'
101 chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
107 arm_setup_usb_otg() {
108 # Set up virtual serial port over USB OTG / device mode.
109 echo >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
110 echo '# Required for USB OTG virtual serial port.' \
111 >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
112 echo 'notify 100 {' \
113 >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
114 echo ' match "system" "DEVFS";' \
115 >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
116 echo ' match "subsystem" "CDEV";' \
117 >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
118 echo ' match "type" "CREATE";' \
119 >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
120 echo ' match "cdev" "ttyU[0-9]+";' \
121 >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
122 echo ' action "/sbin/init q";' \
123 >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
125 >> ${CHROOTDIR}/${DESTDIR}/etc/devd.conf
127 echo '# USB OTG virtual serial port' \
128 >> ${CHROOTDIR}/${DESTDIR}/etc/ttys
129 echo 'ttyU0 "/usr/libexec/getty 3wire" vt100 onifconsole secure' \
130 >> ${CHROOTDIR}/${DESTDIR}/etc/ttys
131 echo 'ttyU1 "/usr/libexec/getty 3wire" vt100 onifconsole secure' \
132 >> ${CHROOTDIR}/${DESTDIR}/etc/ttys
134 echo '# Configure USB OTG; see usb_template(4).' \
135 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
136 echo 'hw.usb.template=3' \
137 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
138 echo 'umodem_load="YES"' \
139 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
142 arm64_setup_multicons() {
143 if [ "${EMBEDDED_TARGET_ARCH}" != "aarch64" ]; then
147 echo '# Multiple console (serial+efi gop) enabled.' \
148 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
149 echo 'boot_multicons="YES"' \
150 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
151 echo 'boot_serial="YES"' \
152 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
155 arm_setup_fdt_overlays() {
156 if [ -z "${FDT_OVERLAYS}" ]; then
160 echo '# DTB OVERLAYS' \
161 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
162 echo "fdt_overlays=\"${FDT_OVERLAYS}\"" \
163 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
166 arm_setup_minimal_loader() {
167 echo '# Disable the beastie menu and color' \
168 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
169 echo 'beastie_disable="YES"' \
170 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
171 echo 'loader_color="NO"' \
172 >> ${CHROOTDIR}/${DESTDIR}/boot/loader.conf
176 if [ "${PART_SCHEME}" = "GPT" ]; then
177 chroot ${CHROOTDIR} mount /dev/${mddev}p2 ${DESTDIR}
179 if [ "${PART_SCHEME}" = "MBR" ]; then
180 chroot ${CHROOTDIR} mount /dev/${mddev}s2a ${DESTDIR}
182 _OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
183 REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
184 BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
185 export UNAME_r=${REVISION}-${BRANCH}
186 eval chroot ${CHROOTDIR} make -C ${WORLDDIR} \
187 TARGET=${EMBEDDED_TARGET} \
188 TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
189 DESTDIR=${DESTDIR} KERNCONF=${KERNEL} \
190 ${CONF_FILES} installworld installkernel distribution
191 chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/boot/efi
192 # Compatibility symlink to /boot/msdos for 13.1 and earlier
193 chroot ${CHROOTDIR} ln -s efi ${DESTDIR}/boot/msdos
197 arm64_setup_multicons
198 arm_setup_fdt_overlays
199 arm_setup_minimal_loader
202 echo '# Custom /etc/fstab for FreeBSD embedded images' \
203 > ${CHROOTDIR}/${DESTDIR}/etc/fstab
204 if [ "${PART_SCHEME}" = "GPT" ]; then
205 echo "/dev/ufs/rootfs / ufs rw 1 1" \
206 >> ${CHROOTDIR}/${DESTDIR}/etc/fstab
207 echo "/dev/msdosfs/EFI /boot/efi msdosfs rw,noatime 0 0" \
208 >> ${CHROOTDIR}/${DESTDIR}/etc/fstab
210 if [ "${PART_SCHEME}" = "MBR" ]; then
211 echo "/dev/ufs/rootfs / ufs rw 1 1" \
212 >> ${CHROOTDIR}/${DESTDIR}/etc/fstab
213 echo "/dev/msdosfs/EFI /boot/efi msdosfs rw,noatime 0 0" \
214 >> ${CHROOTDIR}/${DESTDIR}/etc/fstab
216 echo "tmpfs /tmp tmpfs rw,mode=1777 0 0" \
217 >> ${CHROOTDIR}/${DESTDIR}/etc/fstab
220 hostname="$(echo ${KERNEL} | tr '[:upper:]' '[:lower:]')"
221 echo "hostname=\"${hostname}\"" > ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
222 echo 'ifconfig_DEFAULT="DHCP"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
223 echo 'sshd_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
224 echo 'sendmail_enable="NONE"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
225 echo 'sendmail_submit_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
226 echo 'sendmail_outbound_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
227 echo 'sendmail_msp_queue_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
228 echo 'growfs_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
231 umount_loop ${CHROOTDIR}/${DESTDIR}
237 FATMOUNT="${DESTDIR%${KERNEL}}/fat"
238 UFSMOUNT="${DESTDIR%${KERNEL}}/ufs"
239 chroot ${CHROOTDIR} mkdir -p "${FATMOUNT}" "${UFSMOUNT}"
240 if [ "${PART_SCHEME}" = "GPT" ]; then
241 dospart="/dev/${mddev}p1"
242 ufspart="/dev/${mddev}p2"
244 if [ "${PART_SCHEME}" = "MBR" ]; then
245 dospart="/dev/${mddev}s1"
246 ufspart="/dev/${mddev}s2a"
249 chroot ${CHROOTDIR} mount_msdosfs ${dospart} ${FATMOUNT}
250 chroot ${CHROOTDIR} mount ${ufspart} ${UFSMOUNT}
252 BOOTFILES="$(chroot ${CHROOTDIR} \
253 env TARGET=${EMBEDDED_TARGET} TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
254 WITH_UNIFIED_OBJDIR=yes \
255 make -C ${WORLDDIR}/stand -V .OBJDIR)"
256 BOOTFILES="$(chroot ${CHROOTDIR} realpath ${BOOTFILES})"
258 chroot ${CHROOTDIR} mkdir -p ${FATMOUNT}/EFI/BOOT
259 chroot ${CHROOTDIR} cp -p ${BOOTFILES}/efi/loader_lua/loader_lua.efi \
260 ${FATMOUNT}/EFI/BOOT/$(efi_boot_name ${EMBEDDED_TARGET})
262 chroot ${CHROOTDIR} cp -R ${UFSMOUNT}/boot/dtb ${FATMOUNT}
264 chroot ${CHROOTDIR} touch ${UFSMOUNT}/firstboot
266 umount_loop ${CHROOTDIR}/${FATMOUNT}
267 umount_loop ${CHROOTDIR}/${UFSMOUNT}
268 chroot ${CHROOTDIR} rmdir ${FATMOUNT}
269 chroot ${CHROOTDIR} rmdir ${UFSMOUNT}
272 arm_install_uboot() {
273 # Override in the ${EMBEDDED_TARGET}/${BOARDNAME}.conf file.
279 # Override in the ${EMBEDDED_TARGET}/${BOARDNAME}.conf file.