xref: /freebsd/usr.sbin/bsdinstall/scripts/bootconfig (revision b94e1926600785977c3eac2c2b82655420bc354d)
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
30b6644f52SAlfredo Dal'Ava Junior
31a89559c2SYuri PankovBSDCFG_SHARE="/usr/share/bsdconfig"
32a89559c2SYuri Pankov. $BSDCFG_SHARE/common.subr || exit 1
3340c928e7SEmmanuel Vadotf_dprintf "%s: loading_includes..." "$0"
3440c928e7SEmmanuel Vadotf_include $BSDCFG_SHARE/dialog.subr
35a89559c2SYuri Pankov
36a107ddbbSRebecca Cran: ${TMPDIR:="/tmp"}
37a107ddbbSRebecca Cran
38db8b5613SRebecca Crandie() {
39db8b5613SRebecca Cran	echo $*
40db8b5613SRebecca Cran	exit 1
41db8b5613SRebecca Cran}
42db8b5613SRebecca Cran
4340c928e7SEmmanuel Vadotdialog_uefi_entryname()
4440c928e7SEmmanuel Vadot{
4540c928e7SEmmanuel Vadot	local prompt="Please enter a name for the new entry"
4640c928e7SEmmanuel Vadot	local hline=
4740c928e7SEmmanuel Vadot	local value="$*"
4840c928e7SEmmanuel Vadot	local height width
4940c928e7SEmmanuel Vadot
5040c928e7SEmmanuel Vadot	f_dialog_inputbox_size height width \
5140c928e7SEmmanuel Vadot		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$value" "$hline"
5240c928e7SEmmanuel Vadot
5340c928e7SEmmanuel Vadot	$DIALOG \
5440c928e7SEmmanuel Vadot		--title "$DIALOG_TITLE"         \
5540c928e7SEmmanuel Vadot		--backtitle "$DIALOG_BACKTITLE" \
5640c928e7SEmmanuel Vadot		--hline "$hline"                \
5740c928e7SEmmanuel Vadot		--ok-label "Ok"                 \
5840c928e7SEmmanuel Vadot		--no-cancel                     \
5940c928e7SEmmanuel Vadot		--inputbox "$prompt"            \
6040c928e7SEmmanuel Vadot		$height $width "$value"         \
6140c928e7SEmmanuel Vadot		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
6240c928e7SEmmanuel Vadot}
6340c928e7SEmmanuel Vadot
6440c928e7SEmmanuel Vadotupdate_uefi_bootentry()
6540c928e7SEmmanuel Vadot{
66cc42ef53SBrad Davis	nentries=$(efibootmgr | grep -c "${EFI_LABEL_NAME}$")
6740c928e7SEmmanuel Vadot	# No entries so directly create one and return
6840c928e7SEmmanuel Vadot	if [ ${nentries} -eq 0 ]; then
6940c928e7SEmmanuel Vadot		f_dprintf "Creating UEFI boot entry"
70cc42ef53SBrad Davis		efibootmgr --create --activate --label "$EFI_LABEL_NAME" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
7140c928e7SEmmanuel Vadot		return
7240c928e7SEmmanuel Vadot	fi
7340c928e7SEmmanuel Vadot
74*b94e1926SJens Schweikhardt	$DIALOG --backtitle "$OSNAME Installer" --title 'Boot Configuration' \
75cc42ef53SBrad Davis	    --yesno "There are multiple \"$OSNAME\" EFI boot entries. Would you like to remove them all and add a new one?" 0 0
7640c928e7SEmmanuel Vadot	if [ $? -eq $DIALOG_OK ]; then
77cc42ef53SBrad Davis		for entry in $(efibootmgr | awk "\$NF == \"$EFI_LABEL_NAME\" { sub(/.*Boot/,\"\", \$1); sub(/\*/,\"\", \$1); print \$1 }"); do
7840c928e7SEmmanuel Vadot			efibootmgr -B -b ${entry}
7940c928e7SEmmanuel Vadot		done
80cc42ef53SBrad Davis		efibootmgr --create --activate --label "$EFI_LABEL_NAME" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
8140c928e7SEmmanuel Vadot		return
8240c928e7SEmmanuel Vadot	fi
8340c928e7SEmmanuel Vadot
84cc42ef53SBrad Davis	FREEBSD_BOOTLABEL=$(dialog_uefi_entryname "${EFI_LABEL_NAME}")
8540c928e7SEmmanuel Vadot	[ $? -eq $DIALOG_CANCEL ] && exit 1
8640c928e7SEmmanuel Vadot	efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
8740c928e7SEmmanuel Vadot}
8840c928e7SEmmanuel Vadot
89*b94e1926SJens Schweikhardtf_dialog_title "Boot Configuration"
90cc42ef53SBrad Davisf_dialog_backtitle "$OSNAME Installer"
9140c928e7SEmmanuel Vadot
928befcf7bSNathan Whitehornif [ `uname -m` == powerpc ]; then
938befcf7bSNathan Whitehorn	platform=`sysctl -n hw.platform`
948befcf7bSNathan Whitehorn	if [ "$platform" == ps3 -o "$platform" == powernv ]; then
958befcf7bSNathan Whitehorn		rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB)
96b6644f52SAlfredo Dal'Ava Junior		kboot_conf=$BSDINSTALL_CHROOT/boot/etc/kboot.conf
978befcf7bSNathan Whitehorn		mkdir -p $BSDINSTALL_CHROOT/boot/etc/
98b6644f52SAlfredo Dal'Ava Junior		echo default=$FREEBSD_BOOTLABEL > $kboot_conf
99b6644f52SAlfredo Dal'Ava Junior		echo $FREEBSD_BOOTLABEL=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' >> $kboot_conf
1008befcf7bSNathan Whitehorn	fi
1018befcf7bSNathan Whitehornfi
1028befcf7bSNathan Whitehorn
1030b7472b3SNathan Whitehorn# Update the ESP (EFI System Partition) with the new bootloader if we have an ESP
104e77cf2a4SNathan Whitehornif [ -n "$(awk '{if ($2=="/boot/efi") printf("%s\n",$1);}' $PATH_FSTAB)" ]; then
105c6d56081SWarner Losh	case $(uname -m) in
106c6d56081SWarner Losh	    arm64)	ARCHBOOTNAME=aa64 ;;
107c6d56081SWarner Losh	    amd64)	ARCHBOOTNAME=x64 ;;
1087b08a307SMitchell Horne	    riscv)	ARCHBOOTNAME=riscv64 ;;
109c6d56081SWarner Losh    #	    arm)	ARCHBOOTNAME=arm ;; # No other support for arm install
110c6d56081SWarner Losh    #	    i386)	ARCHBOOTNAME=ia32 ;; # no support for this in i386 kernels, rare machines
111c6d56081SWarner Losh	    *)		die "Unsupported arch $(uname -m) for UEFI install"
112c6d56081SWarner Losh	esac
113676b7d07SMitchell Horne	BOOTDIR="/efi/boot"
114676b7d07SMitchell Horne	BOOTNAME="${BOOTDIR}/boot${ARCHBOOTNAME}.efi"
115676b7d07SMitchell Horne	FREEBSD_BOOTDIR="/efi/freebsd"
116676b7d07SMitchell Horne	FREEBSD_BOOTNAME="${FREEBSD_BOOTDIR}/loader.efi"
1170b7472b3SNathan Whitehorn	mntpt="$BSDINSTALL_CHROOT/boot/efi"
118db8b5613SRebecca Cran
119a89559c2SYuri Pankov	f_dprintf "Installing loader.efi onto ESP"
120676b7d07SMitchell Horne	mkdir -p "${mntpt}/${FREEBSD_BOOTDIR}" "${mntpt}/${BOOTDIR}"
121676b7d07SMitchell Horne	cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${FREEBSD_BOOTNAME}"
122db8b5613SRebecca Cran
123c6d56081SWarner Losh	#
124c6d56081SWarner Losh	# The following shouldn't be necessary. UEFI defines a way to
125c6d56081SWarner Losh	# specifically select what to boot (which we do via
126c6d56081SWarner Losh	# efibootmgr). However, virtual environments often times lack
127c6d56081SWarner Losh	# support for the NV variables efibootmgr sets. In addition,
128c6d56081SWarner Losh	# some UEFI implementations have features that interfere with
129c6d56081SWarner Losh	# the setting of these variables. To combat that, we install the
130c6d56081SWarner Losh	# default removable media boot file as a fallback if it doesn't
131c6d56081SWarner Losh	# exist. We don't install it all the time since that can
132c6d56081SWarner Losh	# interfere with other installations on the drive (like rEFInd).
133c6d56081SWarner Losh	#
134c6d56081SWarner Losh	if [ ! -f "${mntpt}/${BOOTNAME}" ]; then
135c6d56081SWarner Losh		cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${BOOTNAME}"
136c6d56081SWarner Losh	fi
137c6d56081SWarner Losh
13840923b0cSNathan Whitehorn	if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
13940c928e7SEmmanuel Vadot		update_uefi_bootentry
14040923b0cSNathan Whitehorn	fi
141db8b5613SRebecca Cran
1420b7472b3SNathan Whitehorn	f_dprintf "Finished configuring ESP"
143db8b5613SRebecca Cranfi
144db8b5613SRebecca Cran
145db8b5613SRebecca Cran# Add boot0cfg for MBR BIOS booting?
146