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 29b6644f52SAlfredo Dal'Ava Junior 30a89559c2SYuri PankovBSDCFG_SHARE="/usr/share/bsdconfig" 31a89559c2SYuri Pankov. $BSDCFG_SHARE/common.subr || exit 1 32754bc3dcSChattrapat Sangmanee 33754bc3dcSChattrapat SangmaneeFREEBSD_BOOTLABEL=$OSNAME 34754bc3dcSChattrapat Sangmanee 3540c928e7SEmmanuel Vadotf_dprintf "%s: loading_includes..." "$0" 3640c928e7SEmmanuel 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 4540c928e7SEmmanuel Vadotdialog_uefi_entryname() 4640c928e7SEmmanuel Vadot{ 4740c928e7SEmmanuel Vadot local prompt="Please enter a name for the new entry" 4840c928e7SEmmanuel Vadot local hline= 4940c928e7SEmmanuel Vadot local value="$*" 5040c928e7SEmmanuel Vadot local height width 5140c928e7SEmmanuel Vadot 5240c928e7SEmmanuel Vadot f_dialog_inputbox_size height width \ 5340c928e7SEmmanuel Vadot "$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$value" "$hline" 5440c928e7SEmmanuel Vadot 5540c928e7SEmmanuel Vadot $DIALOG \ 5640c928e7SEmmanuel Vadot --title "$DIALOG_TITLE" \ 5740c928e7SEmmanuel Vadot --backtitle "$DIALOG_BACKTITLE" \ 5840c928e7SEmmanuel Vadot --hline "$hline" \ 5940c928e7SEmmanuel Vadot --ok-label "Ok" \ 6040c928e7SEmmanuel Vadot --no-cancel \ 6140c928e7SEmmanuel Vadot --inputbox "$prompt" \ 6240c928e7SEmmanuel Vadot $height $width "$value" \ 6340c928e7SEmmanuel Vadot 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD 6440c928e7SEmmanuel Vadot} 6540c928e7SEmmanuel Vadot 66494de51bSWarner Losh# Copy to the normal FreeBSD location. Also copy to the default location if it 67494de51bSWarner Losh# doesn't exist. This covers setups where UEFI NV variables can't be set and 68494de51bSWarner Losh# some buggy firmware, while preserving complex UEFI setups for multiple booting 69494de51bSWarner Losh# (rEFInd, etc). 70494de51bSWarner Loshuefi_copy_loader() 71494de51bSWarner Losh{ 72494de51bSWarner Losh local ldr=$1 73494de51bSWarner Losh local freebsd_dir=$2 74494de51bSWarner Losh local default_dir=$3 75494de51bSWarner Losh local dest=$4 76494de51bSWarner Losh 77494de51bSWarner Losh mkdir -p "${freebsd_dir}" "${default_dir}" 78494de51bSWarner Losh cp "${ldr}" "${freebsd_dir}" 79494de51bSWarner Losh if [ ! -f "${default_dir}/${dest}" ]; then 80494de51bSWarner Losh cp "${ldr}" "${default_dir}/${dest}" 81494de51bSWarner Losh fi 82494de51bSWarner Losh} 83494de51bSWarner Losh 8440c928e7SEmmanuel Vadotupdate_uefi_bootentry() 8540c928e7SEmmanuel Vadot{ 86*0fd91c48SAhmad Khalifa local nentries=$(efibootmgr | grep -c "${EFI_LABEL_NAME}$") 87*0fd91c48SAhmad Khalifa local loader_path=$1 88*0fd91c48SAhmad Khalifa 8940c928e7SEmmanuel Vadot # No entries so directly create one and return 9040c928e7SEmmanuel Vadot if [ ${nentries} -eq 0 ]; then 9140c928e7SEmmanuel Vadot f_dprintf "Creating UEFI boot entry" 92*0fd91c48SAhmad Khalifa efibootmgr --create --activate --label "$EFI_LABEL_NAME" --loader "${loader_path}" > /dev/null 9340c928e7SEmmanuel Vadot return 9440c928e7SEmmanuel Vadot fi 9540c928e7SEmmanuel Vadot 96b94e1926SJens Schweikhardt $DIALOG --backtitle "$OSNAME Installer" --title 'Boot Configuration' \ 97ebc6ff8dSEd Maste --yesno "One or more \"$OSNAME\" EFI boot manager entries already exist. Would you like to remove them all and add a new one?" 0 0 9840c928e7SEmmanuel Vadot if [ $? -eq $DIALOG_OK ]; then 99cc42ef53SBrad Davis for entry in $(efibootmgr | awk "\$NF == \"$EFI_LABEL_NAME\" { sub(/.*Boot/,\"\", \$1); sub(/\*/,\"\", \$1); print \$1 }"); do 10040c928e7SEmmanuel Vadot efibootmgr -B -b ${entry} 10140c928e7SEmmanuel Vadot done 102*0fd91c48SAhmad Khalifa efibootmgr --create --activate --label "$EFI_LABEL_NAME" --loader "${loader_path}" > /dev/null 10340c928e7SEmmanuel Vadot return 10440c928e7SEmmanuel Vadot fi 10540c928e7SEmmanuel Vadot 106cc42ef53SBrad Davis FREEBSD_BOOTLABEL=$(dialog_uefi_entryname "${EFI_LABEL_NAME}") 10740c928e7SEmmanuel Vadot [ $? -eq $DIALOG_CANCEL ] && exit 1 108*0fd91c48SAhmad Khalifa efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${loader_path}" > /dev/null 10940c928e7SEmmanuel Vadot} 11040c928e7SEmmanuel Vadot 111b94e1926SJens Schweikhardtf_dialog_title "Boot Configuration" 112cc42ef53SBrad Davisf_dialog_backtitle "$OSNAME Installer" 11340c928e7SEmmanuel Vadot 1148befcf7bSNathan Whitehornif [ `uname -m` == powerpc ]; then 1158befcf7bSNathan Whitehorn platform=`sysctl -n hw.platform` 1168befcf7bSNathan Whitehorn if [ "$platform" == ps3 -o "$platform" == powernv ]; then 1178befcf7bSNathan Whitehorn rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB) 118b6644f52SAlfredo Dal'Ava Junior kboot_conf=$BSDINSTALL_CHROOT/boot/etc/kboot.conf 1198befcf7bSNathan Whitehorn mkdir -p $BSDINSTALL_CHROOT/boot/etc/ 120b6644f52SAlfredo Dal'Ava Junior echo default=$FREEBSD_BOOTLABEL > $kboot_conf 121b6644f52SAlfredo Dal'Ava Junior echo $FREEBSD_BOOTLABEL=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' >> $kboot_conf 1228befcf7bSNathan Whitehorn fi 1238befcf7bSNathan Whitehornfi 1248befcf7bSNathan Whitehorn 1250b7472b3SNathan Whitehorn# Update the ESP (EFI System Partition) with the new bootloader if we have an ESP 126e77cf2a4SNathan Whitehornif [ -n "$(awk '{if ($2=="/boot/efi") printf("%s\n",$1);}' $PATH_FSTAB)" ]; then 127c6d56081SWarner Losh case $(uname -m) in 128c6d56081SWarner Losh arm64) ARCHBOOTNAME=aa64 ;; 129c6d56081SWarner Losh amd64) ARCHBOOTNAME=x64 ;; 1307b08a307SMitchell Horne riscv) ARCHBOOTNAME=riscv64 ;; 131c6d56081SWarner Losh # arm) ARCHBOOTNAME=arm ;; # No other support for arm install 132c6d56081SWarner Losh # i386) ARCHBOOTNAME=ia32 ;; # no support for this in i386 kernels, rare machines 133c6d56081SWarner Losh *) die "Unsupported arch $(uname -m) for UEFI install" 134c6d56081SWarner Losh esac 135599273f9SAhmad Khalifa 136494de51bSWarner Losh # Support the weird 32-bit firmware loading 64-bit kernels 137599273f9SAhmad Khalifa if [ `sysctl -n machdep.efi_arch` == i386 ]; then 138599273f9SAhmad Khalifa ARCHBOOTNAME=ia32 139599273f9SAhmad Khalifa file=loader_ia32.efi 140599273f9SAhmad Khalifa else 141599273f9SAhmad Khalifa file=loader.efi 142599273f9SAhmad Khalifa fi 143599273f9SAhmad Khalifa 144494de51bSWarner Losh # Copy the boot loader 1450b7472b3SNathan Whitehorn mntpt="$BSDINSTALL_CHROOT/boot/efi" 146599273f9SAhmad Khalifa f_dprintf "Installing ${file} onto ESP" 147494de51bSWarner Losh uefi_copy_loader "$BSDINSTALL_CHROOT/boot/${file}" \ 148494de51bSWarner Losh "${mntpt}/efi/freebsd" "${mntpt}/efi/boot" \ 149494de51bSWarner Losh boot${ARCHBOOTNAME}.efi 150db8b5613SRebecca Cran 151494de51bSWarner Losh # zfsboot records the extra esp partitions it creates to -esps. These 152494de51bSWarner Losh # are newfs'd at the time of creation. We don't support installing ufs 153494de51bSWarner Losh # over gmirror, so we only do this for ZFS. 154494de51bSWarner Losh esps=${TMPDIR:-"/tmp"}/bsdinstall-esps 155494de51bSWarner Losh if [ -f "$esps" ]; then 156*0fd91c48SAhmad Khalifa tmpmnt=$(mktemp -d -t bsdinstall-esp) 157494de51bSWarner Losh for dev in $(cat $esps); do 158494de51bSWarner Losh f_dprintf "Installing ${file} onto redundant ESP ${dev}" 159*0fd91c48SAhmad Khalifa mount -t msdos "$dev" "$tmpmnt" 160494de51bSWarner Losh uefi_copy_loader "$BSDINSTALL_CHROOT/boot/${file}" \ 161*0fd91c48SAhmad Khalifa "${tmpmnt}/efi/freebsd" "${tmpmnt}/efi/boot" \ 162494de51bSWarner Losh boot${ARCHBOOTNAME}.efi 163*0fd91c48SAhmad Khalifa umount "$tmpmnt" 164494de51bSWarner Losh done 165*0fd91c48SAhmad Khalifa rmdir "${tmpmnt}" 166c6d56081SWarner Losh fi 167c6d56081SWarner Losh 168*0fd91c48SAhmad Khalifa # Try to set the UEFI NV BootXXXX variables to record the boot location. 169*0fd91c48SAhmad Khalifa # Note that the ESP is mounted at ${mntpt}/efi. 170599273f9SAhmad Khalifa if [ "$BSDINSTALL_CONFIGCURRENT" ] && [ "$ARCHBOOTNAME" != ia32 ]; then 171*0fd91c48SAhmad Khalifa update_uefi_bootentry "${mntpt}/efi/freebsd/${file}" 17240923b0cSNathan Whitehorn fi 173db8b5613SRebecca Cran 1740b7472b3SNathan Whitehorn f_dprintf "Finished configuring ESP" 175db8b5613SRebecca Cranfi 176db8b5613SRebecca Cran 177db8b5613SRebecca Cran# Add boot0cfg for MBR BIOS booting? 178