]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - release/tools/arm.subr
MFC r336598-r336600, r336721
[FreeBSD/FreeBSD.git] / release / tools / arm.subr
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 SD card images.
31 #
32 # $FreeBSD$
33 #
34
35 cleanup() {
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
47 umount_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
65 arm_create_disk() {
66         # Create the target raw file and temporary work directory.
67         chroot ${CHROOTDIR} gpart create -s ${PART_SCHEME} ${mddev}
68         chroot ${CHROOTDIR} gpart add -t '!12' -a 512k -s ${FAT_SIZE} ${mddev}
69         chroot ${CHROOTDIR} gpart set -a active -i 1 ${mddev}
70         chroot ${CHROOTDIR} newfs_msdos -L msdosboot -F ${FAT_TYPE} /dev/${mddev}s1
71         chroot ${CHROOTDIR} gpart add -t freebsd ${mddev}
72         chroot ${CHROOTDIR} gpart create -s bsd ${mddev}s2
73         chroot ${CHROOTDIR} gpart add -t freebsd-ufs -a 64k /dev/${mddev}s2
74         chroot ${CHROOTDIR} newfs -U -L rootfs /dev/${mddev}s2a
75
76         return 0
77 }
78
79 arm_create_user() {
80         # Create a default user account 'freebsd' with the password 'freebsd',
81         # and set the default password for the 'root' user to 'root'.
82         chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
83                 groupadd freebsd -g 1001
84         chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/home/freebsd
85         chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
86                 useradd freebsd \
87                 -m -M 0755 -w yes -n freebsd -u 1001 -g 1001 -G 0 \
88                 -c 'FreeBSD User' -d '/home/freebsd' -s '/bin/csh'
89         chroot ${CHROOTDIR} /usr/sbin/pw -R ${DESTDIR} \
90                 usermod root -w yes
91
92         return 0
93 }
94
95 arm_install_base() {
96         chroot ${CHROOTDIR} mount /dev/${mddev}s2a ${DESTDIR}
97         eval chroot ${CHROOTDIR} make -C ${WORLDDIR} \
98                 TARGET=${EMBEDDED_TARGET} \
99                 TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
100                 DESTDIR=${DESTDIR} KERNCONF=${KERNEL} \
101                 installworld installkernel distribution
102         chroot ${CHROOTDIR} mkdir -p ${DESTDIR}/boot/msdos
103
104         arm_create_user
105
106         echo '# Custom /etc/fstab for FreeBSD embedded images' \
107                 > ${CHROOTDIR}/${DESTDIR}/etc/fstab
108         echo "/dev/ufs/rootfs   /       ufs     rw      1       1" \
109                 >> ${CHROOTDIR}/${DESTDIR}/etc/fstab
110         echo "/dev/msdosfs/MSDOSBOOT /boot/msdos msdosfs rw,noatime 0 0" \
111                 >> ${CHROOTDIR}/${DESTDIR}/etc/fstab
112         echo "tmpfs /tmp tmpfs rw,mode=1777,size=50m 0 0" \
113                 >> ${CHROOTDIR}/${DESTDIR}/etc/fstab
114
115         local hostname
116         hostname="$(echo ${KERNEL} | tr '[:upper:]' '[:lower:]')"
117         echo "hostname=\"${hostname}\"" > ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
118         echo 'ifconfig_DEFAULT="DHCP"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
119         echo 'sshd_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
120         echo 'sendmail_enable="NONE"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
121         echo 'sendmail_submit_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
122         echo 'sendmail_outbound_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
123         echo 'sendmail_msp_queue_enable="NO"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
124         echo 'growfs_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
125         echo 'kldxref_enable="YES"' >> ${CHROOTDIR}/${DESTDIR}/etc/rc.conf
126
127         sync
128         umount_loop ${CHROOTDIR}/${DESTDIR}
129
130         return 0
131 }
132
133 arm_install_boot() {
134         FATMOUNT="${DESTDIR%${KERNEL}}/fat"
135         UFSMOUNT="${DESTDIR%${KERNEL}}/ufs"
136         chroot ${CHROOTDIR} mkdir -p "${FATMOUNT}" "${UFSMOUNT}"
137         chroot ${CHROOTDIR} mount_msdosfs /dev/${mddev}s1 ${FATMOUNT}
138         chroot ${CHROOTDIR} mount /dev/${mddev}s2a ${UFSMOUNT}
139
140         if [ "${EMBEDDED_TARGET}" == "arm" ]; then
141         chroot ${CHROOTDIR} cp -p ${UFSMOUNT}/boot/ubldr.bin \
142                 ${FATMOUNT}/ubldr.bin
143         fi
144
145         BOOTFILES="$(chroot ${CHROOTDIR} \
146                 env TARGET=${EMBEDDED_TARGET} TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
147                 WITH_UNIFIED_OBJDIR=yes \
148                 make -C ${WORLDDIR}/stand -V .OBJDIR)"
149         BOOTFILES="$(chroot ${CHROOTDIR} realpath ${BOOTFILES})"
150
151         chroot ${CHROOTDIR} mkdir -p ${FATMOUNT}/EFI/BOOT
152         chroot ${CHROOTDIR} cp -p ${BOOTFILES}/efi/loader/loader.efi \
153                 ${FATMOUNT}/EFI/BOOT/$(efi_boot_name ${EMBEDDED_TARGET})
154
155         chroot ${CHROOTDIR} touch ${UFSMOUNT}/firstboot
156         sync
157         umount_loop ${CHROOTDIR}/${FATMOUNT}
158         umount_loop ${CHROOTDIR}/${UFSMOUNT}
159         chroot ${CHROOTDIR} rmdir ${FATMOUNT}
160         chroot ${CHROOTDIR} rmdir ${UFSMOUNT}
161 }
162
163 arm_install_uboot() {
164         # Override in the arm/KERNEL.conf file.
165
166         return 0
167 }