18befcf7bSNathan Whitehorn#!/bin/sh 28befcf7bSNathan Whitehorn#- 3db8b5613SRebecca Cran# Copyright (c) 2018 Rebecca Cran 48befcf7bSNathan Whitehorn# Copyright (c) 2017 Nathan Whitehorn 58befcf7bSNathan Whitehorn# All rights reserved. 68befcf7bSNathan Whitehorn# 78befcf7bSNathan Whitehorn# Redistribution and use in source and binary forms, with or without 88befcf7bSNathan Whitehorn# modification, are permitted provided that the following conditions 98befcf7bSNathan Whitehorn# are met: 108befcf7bSNathan Whitehorn# 1. Redistributions of source code must retain the above copyright 118befcf7bSNathan Whitehorn# notice, this list of conditions and the following disclaimer. 128befcf7bSNathan Whitehorn# 2. Redistributions in binary form must reproduce the above copyright 138befcf7bSNathan Whitehorn# notice, this list of conditions and the following disclaimer in the 148befcf7bSNathan Whitehorn# documentation and/or other materials provided with the distribution. 158befcf7bSNathan Whitehorn# 168befcf7bSNathan Whitehorn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 178befcf7bSNathan Whitehorn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188befcf7bSNathan Whitehorn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198befcf7bSNathan Whitehorn# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 208befcf7bSNathan Whitehorn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218befcf7bSNathan Whitehorn# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228befcf7bSNathan Whitehorn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238befcf7bSNathan Whitehorn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248befcf7bSNathan Whitehorn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258befcf7bSNathan Whitehorn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268befcf7bSNathan Whitehorn# SUCH DAMAGE. 278befcf7bSNathan Whitehorn# 288befcf7bSNathan Whitehorn# $FreeBSD$ 298befcf7bSNathan Whitehorn 30060c97abSAlfredo Dal'Ava Junior 31060c97abSAlfredo Dal'Ava JuniorFREEBSD_BOOTLABEL="FreeBSD" 32060c97abSAlfredo Dal'Ava Junior 33a89559c2SYuri PankovBSDCFG_SHARE="/usr/share/bsdconfig" 34a89559c2SYuri Pankov. $BSDCFG_SHARE/common.subr || exit 1 35*477fbe67SEmmanuel Vadotf_dprintf "%s: loading_includes..." "$0" 36*477fbe67SEmmanuel Vadotf_include $BSDCFG_SHARE/dialog.subr 37a89559c2SYuri Pankov 38a107ddbbSRebecca Cran: ${TMPDIR:="/tmp"} 39a107ddbbSRebecca Cran 40db8b5613SRebecca Crandie() { 41db8b5613SRebecca Cran echo $* 42db8b5613SRebecca Cran exit 1 43db8b5613SRebecca Cran} 44db8b5613SRebecca Cran 45*477fbe67SEmmanuel Vadotdialog_uefi_entryname() 46*477fbe67SEmmanuel Vadot{ 47*477fbe67SEmmanuel Vadot local prompt="Please enter a name for the new entry" 48*477fbe67SEmmanuel Vadot local hline= 49*477fbe67SEmmanuel Vadot local value="$*" 50*477fbe67SEmmanuel Vadot local height width 51*477fbe67SEmmanuel Vadot 52*477fbe67SEmmanuel Vadot f_dialog_inputbox_size height width \ 53*477fbe67SEmmanuel Vadot "$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$value" "$hline" 54*477fbe67SEmmanuel Vadot 55*477fbe67SEmmanuel Vadot $DIALOG \ 56*477fbe67SEmmanuel Vadot --title "$DIALOG_TITLE" \ 57*477fbe67SEmmanuel Vadot --backtitle "$DIALOG_BACKTITLE" \ 58*477fbe67SEmmanuel Vadot --hline "$hline" \ 59*477fbe67SEmmanuel Vadot --ok-label "Ok" \ 60*477fbe67SEmmanuel Vadot --no-cancel \ 61*477fbe67SEmmanuel Vadot --inputbox "$prompt" \ 62*477fbe67SEmmanuel Vadot $height $width "$value" \ 63*477fbe67SEmmanuel Vadot 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 64*477fbe67SEmmanuel Vadot} 65*477fbe67SEmmanuel Vadot 66*477fbe67SEmmanuel Vadotupdate_uefi_bootentry() 67*477fbe67SEmmanuel Vadot{ 68*477fbe67SEmmanuel Vadot nentries=$(efibootmgr | grep -c 'FreeBSD$') 69*477fbe67SEmmanuel Vadot # No entries so directly create one and return 70*477fbe67SEmmanuel Vadot if [ ${nentries} -eq 0 ]; then 71*477fbe67SEmmanuel Vadot f_dprintf "Creating UEFI boot entry" 72*477fbe67SEmmanuel Vadot efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null 73*477fbe67SEmmanuel Vadot return 74*477fbe67SEmmanuel Vadot fi 75*477fbe67SEmmanuel Vadot 76*477fbe67SEmmanuel Vadot $DIALOG --backtitle 'FreeBSD Installer' --title 'Boot configuration' \ 77*477fbe67SEmmanuel Vadot --yesno 'There is multiple "FreeBSD" efi boot entries, would you like to remove them all and add a new one?' 0 0 78*477fbe67SEmmanuel Vadot if [ $? -eq $DIALOG_OK ]; then 79*477fbe67SEmmanuel Vadot for entry in $(efibootmgr | awk '$NF == "FreeBSD" { sub(/.*Boot/,"", $1); sub(/\*/,"", $1); print $1 }'); do 80*477fbe67SEmmanuel Vadot efibootmgr -B -b ${entry} 81*477fbe67SEmmanuel Vadot done 82*477fbe67SEmmanuel Vadot efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null 83*477fbe67SEmmanuel Vadot return 84*477fbe67SEmmanuel Vadot fi 85*477fbe67SEmmanuel Vadot 86*477fbe67SEmmanuel Vadot FREEBSD_BOOTLABEL=$(dialog_uefi_entryname "${FREEBSD_BOOTLABEL}") 87*477fbe67SEmmanuel Vadot [ $? -eq $DIALOG_CANCEL ] && exit 1 88*477fbe67SEmmanuel Vadot efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null 89*477fbe67SEmmanuel Vadot} 90*477fbe67SEmmanuel Vadot 91*477fbe67SEmmanuel Vadotf_dialog_title "Boot configuration" 92*477fbe67SEmmanuel Vadotf_dialog_backtitle "FreeBSD Installer" 93*477fbe67SEmmanuel Vadot 948befcf7bSNathan Whitehornif [ `uname -m` == powerpc ]; then 958befcf7bSNathan Whitehorn platform=`sysctl -n hw.platform` 968befcf7bSNathan Whitehorn if [ "$platform" == ps3 -o "$platform" == powernv ]; then 978befcf7bSNathan Whitehorn rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB) 98060c97abSAlfredo Dal'Ava Junior kboot_conf=$BSDINSTALL_CHROOT/boot/etc/kboot.conf 998befcf7bSNathan Whitehorn mkdir -p $BSDINSTALL_CHROOT/boot/etc/ 100060c97abSAlfredo Dal'Ava Junior echo default=$FREEBSD_BOOTLABEL > $kboot_conf 101060c97abSAlfredo Dal'Ava Junior echo $FREEBSD_BOOTLABEL=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' >> $kboot_conf 1028befcf7bSNathan Whitehorn fi 1038befcf7bSNathan Whitehornfi 1048befcf7bSNathan Whitehorn 105611d8311SNathan Whitehorn# Update the ESP (EFI System Partition) with the new bootloader if we have an ESP 106611d8311SNathan Whitehornif [ -n "$(awk '{if ($2=="/boot/efi") printf("%s\n",$1);}' $PATH_FSTAB)" ]; then 107c6d56081SWarner Losh case $(uname -m) in 108c6d56081SWarner Losh arm64) ARCHBOOTNAME=aa64 ;; 109c6d56081SWarner Losh amd64) ARCHBOOTNAME=x64 ;; 110c415d0dfSMitchell Horne riscv) ARCHBOOTNAME=riscv64 ;; 111c6d56081SWarner Losh # arm) ARCHBOOTNAME=arm ;; # No other support for arm install 112c6d56081SWarner Losh # i386) ARCHBOOTNAME=ia32 ;; # no support for this in i386 kernels, rare machines 113c6d56081SWarner Losh *) die "Unsupported arch $(uname -m) for UEFI install" 114c6d56081SWarner Losh esac 11514640ac7SMitchell Horne BOOTDIR="/efi/boot" 11614640ac7SMitchell Horne BOOTNAME="${BOOTDIR}/boot${ARCHBOOTNAME}.efi" 11714640ac7SMitchell Horne FREEBSD_BOOTDIR="/efi/freebsd" 11814640ac7SMitchell Horne FREEBSD_BOOTNAME="${FREEBSD_BOOTDIR}/loader.efi" 119611d8311SNathan Whitehorn mntpt="$BSDINSTALL_CHROOT/boot/efi" 120db8b5613SRebecca Cran 121a89559c2SYuri Pankov f_dprintf "Installing loader.efi onto ESP" 12214640ac7SMitchell Horne mkdir -p "${mntpt}/${FREEBSD_BOOTDIR}" "${mntpt}/${BOOTDIR}" 12314640ac7SMitchell Horne cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${FREEBSD_BOOTNAME}" 124db8b5613SRebecca Cran 125c6d56081SWarner Losh # 126c6d56081SWarner Losh # The following shouldn't be necessary. UEFI defines a way to 127c6d56081SWarner Losh # specifically select what to boot (which we do via 128c6d56081SWarner Losh # efibootmgr). However, virtual environments often times lack 129c6d56081SWarner Losh # support for the NV variables efibootmgr sets. In addition, 130c6d56081SWarner Losh # some UEFI implementations have features that interfere with 131c6d56081SWarner Losh # the setting of these variables. To combat that, we install the 132c6d56081SWarner Losh # default removable media boot file as a fallback if it doesn't 133c6d56081SWarner Losh # exist. We don't install it all the time since that can 134c6d56081SWarner Losh # interfere with other installations on the drive (like rEFInd). 135c6d56081SWarner Losh # 136c6d56081SWarner Losh if [ ! -f "${mntpt}/${BOOTNAME}" ]; then 137c6d56081SWarner Losh cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${BOOTNAME}" 138c6d56081SWarner Losh fi 139c6d56081SWarner Losh 1405a4dffacSNathan Whitehorn if [ "$BSDINSTALL_CONFIGCURRENT" ]; then 141*477fbe67SEmmanuel Vadot update_uefi_bootentry 1425a4dffacSNathan Whitehorn fi 143db8b5613SRebecca Cran 144611d8311SNathan Whitehorn f_dprintf "Finished configuring ESP" 145db8b5613SRebecca Cranfi 146db8b5613SRebecca Cran 147db8b5613SRebecca Cran# Add boot0cfg for MBR BIOS booting? 148