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 33die() { 34 echo $* 35 exit 1 36} 37 38if [ `uname -m` == powerpc ]; then 39 platform=`sysctl -n hw.platform` 40 if [ "$platform" == ps3 -o "$platform" == powernv ]; then 41 rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB) 42 mkdir -p $BSDINSTALL_CHROOT/boot/etc/ 43 echo FreeBSD=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' > $BSDINSTALL_CHROOT/boot/etc/kboot.conf 44 fi 45fi 46 47# Update the ESP (EFI System Partition) with the new bootloader 48if [ "$(uname -m)" = "amd64" ] || [ "$(uname -m)" = "i386" ]; then 49 X86_BOOTMETHOD=$(sysctl -n machdep.bootmethod) 50fi 51 52if [ "$(uname -m)" = "arm64" ] || [ "$X86_BOOTMETHOD" = "UEFI" ]; then 53 UFSBOOT_ESPS=$(cat /tmp/bsdinstall-esps 2>/dev/null) 54 ZFSBOOT_DISKS=$(cat /tmp/bsdinstall-zfsboot 2>/dev/null) 55 num_esps=0 56 57 if [ -n "$ZFSBOOT_DISKS" ]; then 58 # We're in a ZFS install environment 59 for disk in $ZFSBOOT_DISKS; do 60 index=$(gpart show "$disk" | cut -w -f 4,5 | grep "efi" | cut -w -f 1) 61 # Check that $index is an integer 62 [ -n "$index" ] && [ "$index" -eq "$index" ] && [ "$index" -ge 0 ] 2> /dev/null 63 if [ $? -ne 0 ]; then 64 continue 65 fi 66 67 if [ -e "/dev/${disk}p${index}" ]; then 68 ESPS="$ESPS ${disk}p${index}" 69 elif [ -e "/dev/${disk}s${index}" ]; then 70 ESPS="$ESPS ${disk}s${index}" 71 else 72 continue 73 fi 74 75 num_esps=$((num_esps + 1)) 76 done 77 fi 78 79 if [ -n "$UFSBOOT_ESPS" ]; then 80 # We're in a UFS install environment 81 for partition in $UFSBOOT_ESPS; do 82 ESPS="$ESPS $partition" 83 num_esps=$((num_esps + 1)) 84 done 85 fi 86 87 if [ -z "$ESPS" ]; then 88 # The installer hasn't given us any ESPs to use. 89 # Try and figure out which to use by looking for an 90 # unformatted efi partition 91 92 for geom in $(gpart status -sg | awk '{print $1}'); do 93 hasfreebsd=$(gpart show "${geom}" | cut -w -f 4,5 | grep "freebsd") 94 if [ -n "$hasfreebsd" ]; then 95 index=$(gpart show "${geom}" | cut -w -f 4,5 | grep "efi" | cut -w -f 1) 96 # Check that $index is a valid integer 97 [ -n "$index" ] && [ "$index" -eq "$index" ] && [ "$index" -ge 0 ] 2> /dev/null 98 if [ $? -ne 0 ]; then 99 continue 100 fi 101 102 mntpt=$(mktemp -d /tmp/stand-test.XXXXXX) 103 if [ -e "/dev/${geom}p${index}" ]; then 104 dev=${geom}p${index} 105 elif [ -e "/dev/${geom}s${index}" ]; then 106 dev=/${geom}s${index} 107 else 108 continue 109 fi 110 111 # Try and mount it. If it fails, assume it's 112 # unformatted and should be used. 113 mount -t msdosfs -o ro "/dev/${dev}" "${mntpt}" 114 if [ $? -ne 0 ]; then 115 ESPS="$ESPS ${dev}" 116 num_esps=$((num_esps + 1)) 117 else 118 umount "${mntpt}" 119 fi 120 rmdir "${mntpt}" 121 fi 122 done 123 fi 124 125 for esp in $ESPS; do 126 f_dprintf "Formatting /dev/${esp} as FAT32" 127 newfs_msdos -F 32 -c 1 -L EFISYS "/dev/$esp" > /dev/null 2>&1 128 if [ $? -ne 0 ]; then 129 die "Failed to format ESP $esp as FAT32" 130 fi 131 132 mntpt=$(mktemp -d /tmp/stand-test.XXXXXX) 133 f_dprintf "Mounting ESP /dev/${esp}" 134 mount -t msdosfs "/dev/${esp}" "${mntpt}" 135 if [ $? -ne 0 ]; then 136 die "Failed to mount ESP ${dev} on ${mntpt}" 137 fi 138 139 f_dprintf "Installing loader.efi onto ESP" 140 mkdir -p "$mntpt/EFI/freebsd" 141 cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/EFI/freebsd/loader.efi" 142 143 if [ "$num_esps" -gt 1 ]; then 144 bootlabel="FreeBSD (${esp})" 145 else 146 bootlabel="FreeBSD" 147 fi 148 149 f_dprintf "Creating UEFI boot entry" 150 efibootmgr --create --activate --label "$bootlabel" --loader "${mntpt}/EFI/freebsd/loader.efi" > /dev/null 151 152 f_dprintf "Unmounting ESP" 153 umount "${mntpt}" 154 rmdir "${mntpt}" 155 156 f_dprintf "Finished configuring /dev/${esp} as ESP" 157 done 158fi 159 160# Add boot0cfg for MBR BIOS booting? 161