]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bsdinstall/scripts/bootconfig
Update to Zstandard 1.3.8
[FreeBSD/FreeBSD.git] / usr.sbin / bsdinstall / scripts / bootconfig
1 #!/bin/sh
2 #-
3 # Copyright (c) 2018 Rebecca Cran
4 # Copyright (c) 2017 Nathan Whitehorn
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # $FreeBSD$
29
30 die() {
31         echo $*
32         exit 1
33 }
34
35 if [ `uname -m` == powerpc ]; then
36         platform=`sysctl -n hw.platform`
37         if [ "$platform" == ps3 -o "$platform" == powernv ]; then
38                 rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB)
39                 mkdir -p $BSDINSTALL_CHROOT/boot/etc/
40                 echo FreeBSD=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' > $BSDINSTALL_CHROOT/boot/etc/kboot.conf
41         fi
42 fi
43
44 # Update the ESP (EFI System Partition) with the new bootloader
45 if [ "$(uname -m)" = "amd64" ] || [ "$(uname -m)" = "i386" ]; then
46         X86_BOOTMETHOD=$(sysctl -n machdep.bootmethod)
47 fi
48
49 if [ "$(uname -m)" = "arm64" ] || [ "$X86_BOOTMETHOD" = "UEFI" ]; then
50         UFSBOOT_ESPS=$(cat /tmp/bsdinstall-esps)
51         num_esps=0
52
53         if [ -n "$ZFSBOOT_DISKS" ]; then
54                 # We're in a ZFS install environment
55                 for disk in $ZFSBOOT_DISKS; do
56                         index=$(gpart show "$disk" | cut -w -f 4,5 | grep "efi" | cut -w -f 1)
57                         # Check that $index is an integer
58                         [ -n "$index" ] && [ "$index" -eq "$index" ] && [ "$index" -ge 0 ] 2> /dev/null
59                         if [ $? -ne 0 ]; then
60                                 continue
61                         fi
62
63                         if [ -e "/dev/${disk}p${index}" ]; then
64                                 ESPS="$ESPS ${disk}p${index}"
65                         elif [ -e "/dev/${disk}s${index}" ]; then
66                                 ESPS="$ESPS ${disk}s${index}"
67                         else
68                                 continue
69                         fi
70
71                         num_esps=$((num_esps + 1))
72                 done
73         fi
74
75         if [ -n "$UFSBOOT_ESPS" ]; then
76                 # We're in a UFS install environment
77                 for partition in $UFSBOOT_ESPS; do
78                         ESPS="$ESPS $partition"
79                         num_esps=$((num_esps + 1))
80                 done
81         fi
82
83         if [ -z "$ESPS" ]; then
84                 # The installer hasn't given us any ESPs to use.
85                 # Try and figure out which to use by looking for an
86                 # unformatted efi partition
87                 for disk in $(sysctl -n kern.disks); do
88                         hasfreebsd=$(gpart show "$disk" | cut -w -f 4,5 | grep "freebsd")
89                         if [ -n "$hasfreebsd" ]; then
90                                 index=$(gpart show "$disk" | cut -w -f 4,5 | grep "efi" | cut -w -f 1)
91                                 # Check that $index is a valid integer
92                                 [ -n "$index" ] && [ "$index" -eq "$index" ] && [ "$index" -ge 0 ] 2> /dev/null 
93                                 if [ $? -ne 0 ]; then
94                                         continue
95                                 fi
96
97                                 mntpt=$(mktemp -d /tmp/stand-test.XXXXXX)
98                                 if [ -e "/dev/${disk}p${index}" ]; then
99                                         dev=${disk}p${index}
100                                 elif [ -e "/dev/${disk}s${index}" ]; then
101                                         dev=/${disk}s${index}
102                                 else
103                                         continue
104                                 fi
105
106                                 # Try and mount it. If it fails, assume it's
107                                 # unformatted and should be used.
108                                 mount -t msdosfs "/dev/${dev}" "${mntpt}"
109                                 if [ $? -ne 0 ]; then
110                                         ESPS="$ESPS ${dev}"
111                                         num_esps=$((num_esps + 1))
112                                 else
113                                         umount "${mntpt}"
114                                 fi
115                                 rmdir "${mntpt}"
116                         fi
117                 done
118         fi
119
120         for esp in $ESPS; do
121                 newfs_msdos -F 32 -c 1 -L EFISYS "/dev/$esp" > /dev/null 2>&1
122                 if [ $? -ne 0 ]; then
123                         die "Failed to format ESP $esp as FAT32"
124                 fi
125
126                 mntpt=$(mktemp -d /tmp/stand-test.XXXXXX)
127                 mount -t msdosfs "/dev/${esp}" "${mntpt}"
128                 if [ $? -ne 0 ]; then
129                         die "Failed to mount ESP ${dev} on ${mntpt}"
130                 fi
131
132                 mkdir -p "$mntpt/EFI/freebsd"
133                 cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/EFI/freebsd/loader.efi"
134
135                 if [ "$num_esps" -gt 1 ]; then
136                         bootlabel="FreeBSD (${esp})"
137                 else
138                         bootlabel="FreeBSD"
139                 fi
140
141                 efibootmgr --create --label "$bootlabel" --loader "${mntpt}/EFI/freebsd/loader.efi" > /dev/null
142
143                 umount "${mntpt}"
144                 rmdir "${mntpt}"
145
146                 # When creating new entries, efibootmgr doesn't mark them active, so we need to
147                 # do so. It doesn't make it easy to find which entry it just added, so rely on
148                 # the fact that it places the new entry first in BootOrder.
149                 bootorder=$(efivar --name 8be4df61-93ca-11d2-aa0d-00e098032b8c-BootOrder --print --no-name --hex | head -1)
150                 bootentry=$(echo "$bootorder" | cut -w -f 3)$(echo "$bootorder" | cut -w -f 2)
151                 efibootmgr --activate "$bootentry" > /dev/null
152         done
153 fi
154
155 # Add boot0cfg for MBR BIOS booting?