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 30BSDCFG_SHARE="/usr/share/bsdconfig" 31. $BSDCFG_SHARE/common.subr || exit 1 32 33: ${TMPDIR:="/tmp"} 34 35die() { 36 echo $* 37 exit 1 38} 39 40if [ `uname -m` == powerpc ]; then 41 platform=`sysctl -n hw.platform` 42 if [ "$platform" == ps3 -o "$platform" == powernv ]; then 43 rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB) 44 mkdir -p $BSDINSTALL_CHROOT/boot/etc/ 45 echo FreeBSD=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' > $BSDINSTALL_CHROOT/boot/etc/kboot.conf 46 fi 47fi 48 49# Update the ESP (EFI System Partition) with the new bootloader 50if [ "$(uname -m)" = "amd64" ] || [ "$(uname -m)" = "i386" ]; then 51 X86_BOOTMETHOD=$(sysctl -n machdep.bootmethod) 52fi 53 54if [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "riscv" ] || [ "$X86_BOOTMETHOD" = "UEFI" ]; then 55 UFSBOOT_ESPS=$(cat $TMPDIR/bsdinstall-esps 2>/dev/null) 56 ZFSBOOT_DISKS=$(cat $TMPDIR/bsdinstall-zfsboot 2>/dev/null) 57 num_esps=0 58 59 if [ -n "$ZFSBOOT_DISKS" ]; then 60 # We're in a ZFS install environment 61 for disk in $ZFSBOOT_DISKS; do 62 index=$(gpart show "$disk" | cut -w -f 4,5 | grep "efi" | cut -w -f 1) 63 # Check that $index is an integer 64 [ -n "$index" ] && [ "$index" -eq "$index" ] && [ "$index" -ge 0 ] 2> /dev/null 65 if [ $? -ne 0 ]; then 66 continue 67 fi 68 69 if [ -e "/dev/${disk}p${index}" ]; then 70 ESPS="$ESPS ${disk}p${index}" 71 elif [ -e "/dev/${disk}s${index}" ]; then 72 ESPS="$ESPS ${disk}s${index}" 73 else 74 continue 75 fi 76 77 num_esps=$((num_esps + 1)) 78 done 79 fi 80 81 if [ -n "$UFSBOOT_ESPS" ]; then 82 # We're in a UFS install environment 83 for partition in $UFSBOOT_ESPS; do 84 ESPS="$ESPS $partition" 85 num_esps=$((num_esps + 1)) 86 done 87 fi 88 89 if [ -z "$ESPS" ]; then 90 # The installer hasn't given us any ESPs to use. 91 # Try and figure out which to use by looking for an 92 # unformatted efi partition 93 94 for geom in $(gpart status -sg | awk '{print $1}'); do 95 hasfreebsd=$(gpart show "${geom}" | cut -w -f 4,5 | grep "freebsd") 96 if [ -n "$hasfreebsd" ]; then 97 index=$(gpart show "${geom}" | cut -w -f 4,5 | grep "efi" | cut -w -f 1) 98 # Check that $index is a valid integer 99 [ -n "$index" ] && [ "$index" -eq "$index" ] && [ "$index" -ge 0 ] 2> /dev/null 100 if [ $? -ne 0 ]; then 101 continue 102 fi 103 104 mntpt=$(mktemp -d $TMPDIR/stand-test.XXXXXX) 105 if [ -e "/dev/${geom}p${index}" ]; then 106 dev=${geom}p${index} 107 elif [ -e "/dev/${geom}s${index}" ]; then 108 dev=/${geom}s${index} 109 else 110 continue 111 fi 112 113 # Try and mount it. If it fails, assume it's 114 # unformatted and should be used. 115 mount -t msdosfs -o ro "/dev/${dev}" "${mntpt}" 116 if [ $? -ne 0 ]; then 117 ESPS="$ESPS ${dev}" 118 num_esps=$((num_esps + 1)) 119 else 120 umount "${mntpt}" 121 fi 122 rmdir "${mntpt}" 123 fi 124 done 125 fi 126 127 case $(uname -m) in 128 arm64) ARCHBOOTNAME=aa64 ;; 129 amd64) ARCHBOOTNAME=x64 ;; 130 riscv) ARCHBOOTNAME=riscv64 ;; 131 # arm) ARCHBOOTNAME=arm ;; # No other support for arm install 132 # i386) ARCHBOOTNAME=ia32 ;; # no support for this in i386 kernels, rare machines 133 *) die "Unsupported arch $(uname -m) for UEFI install" 134 esac 135 BOOTDIR="/efi/boot" 136 BOOTNAME="${BOOTDIR}/boot${ARCHBOOTNAME}.efi" 137 FREEBSD_BOOTDIR="/efi/freebsd" 138 FREEBSD_BOOTNAME="${FREEBSD_BOOTDIR}/loader.efi" 139 140 for esp in $ESPS; do 141 f_dprintf "Formatting /dev/${esp} as FAT32" 142 newfs_msdos -F 32 -c 1 -L EFISYS "/dev/$esp" > /dev/null 2>&1 143 if [ $? -ne 0 ]; then 144 die "Failed to format ESP $esp as FAT32" 145 fi 146 147 mntpt=$(mktemp -d $TMPDIR/stand-test.XXXXXX) 148 f_dprintf "Mounting ESP /dev/${esp}" 149 mount -t msdosfs "/dev/${esp}" "${mntpt}" 150 if [ $? -ne 0 ]; then 151 die "Failed to mount ESP ${dev} on ${mntpt}" 152 fi 153 154 f_dprintf "Installing loader.efi onto ESP" 155 mkdir -p "${mntpt}/${FREEBSD_BOOTDIR}" "${mntpt}/${BOOTDIR}" 156 cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${FREEBSD_BOOTNAME}" 157 158 # 159 # The following shouldn't be necessary. UEFI defines a way to 160 # specifically select what to boot (which we do via 161 # efibootmgr). However, virtual environments often times lack 162 # support for the NV variables efibootmgr sets. In addition, 163 # some UEFI implementations have features that interfere with 164 # the setting of these variables. To combat that, we install the 165 # default removable media boot file as a fallback if it doesn't 166 # exist. We don't install it all the time since that can 167 # interfere with other installations on the drive (like rEFInd). 168 # 169 if [ ! -f "${mntpt}/${BOOTNAME}" ]; then 170 cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${BOOTNAME}" 171 fi 172 173 if [ "$num_esps" -gt 1 ]; then 174 bootlabel="FreeBSD (${esp})" 175 else 176 bootlabel="FreeBSD" 177 fi 178 179 f_dprintf "Creating UEFI boot entry" 180 efibootmgr --create --activate --label "$bootlabel" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null 181 182 f_dprintf "Unmounting ESP" 183 umount "${mntpt}" 184 rmdir "${mntpt}" 185 186 f_dprintf "Finished configuring /dev/${esp} as ESP" 187 done 188fi 189 190# Add boot0cfg for MBR BIOS booting? 191