xref: /freebsd/usr.sbin/bsdinstall/scripts/bootconfig (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
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
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading_includes..." "$0"
34f_include $BSDCFG_SHARE/dialog.subr
35
36: ${TMPDIR:="/tmp"}
37
38die() {
39	echo $*
40	exit 1
41}
42
43dialog_uefi_entryname()
44{
45	local prompt="Please enter a name for the new entry"
46	local hline=
47	local value="$*"
48	local height width
49
50	f_dialog_inputbox_size height width \
51		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$value" "$hline"
52
53	$DIALOG \
54		--title "$DIALOG_TITLE"         \
55		--backtitle "$DIALOG_BACKTITLE" \
56		--hline "$hline"                \
57		--ok-label "Ok"                 \
58		--no-cancel                     \
59		--inputbox "$prompt"            \
60		$height $width "$value"         \
61		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
62}
63
64update_uefi_bootentry()
65{
66	nentries=$(efibootmgr | grep -c "${EFI_LABEL_NAME}$")
67	# No entries so directly create one and return
68	if [ ${nentries} -eq 0 ]; then
69		f_dprintf "Creating UEFI boot entry"
70		efibootmgr --create --activate --label "$EFI_LABEL_NAME" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
71		return
72	fi
73
74	$DIALOG --backtitle "$OSNAME Installer" --title 'Boot Configuration' \
75	    --yesno "There are multiple \"$OSNAME\" EFI boot entries. Would you like to remove them all and add a new one?" 0 0
76	if [ $? -eq $DIALOG_OK ]; then
77		for entry in $(efibootmgr | awk "\$NF == \"$EFI_LABEL_NAME\" { sub(/.*Boot/,\"\", \$1); sub(/\*/,\"\", \$1); print \$1 }"); do
78			efibootmgr -B -b ${entry}
79		done
80		efibootmgr --create --activate --label "$EFI_LABEL_NAME" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
81		return
82	fi
83
84	FREEBSD_BOOTLABEL=$(dialog_uefi_entryname "${EFI_LABEL_NAME}")
85	[ $? -eq $DIALOG_CANCEL ] && exit 1
86	efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
87}
88
89f_dialog_title "Boot Configuration"
90f_dialog_backtitle "$OSNAME Installer"
91
92if [ `uname -m` == powerpc ]; then
93	platform=`sysctl -n hw.platform`
94	if [ "$platform" == ps3 -o "$platform" == powernv ]; then
95		rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB)
96		kboot_conf=$BSDINSTALL_CHROOT/boot/etc/kboot.conf
97		mkdir -p $BSDINSTALL_CHROOT/boot/etc/
98		echo default=$FREEBSD_BOOTLABEL > $kboot_conf
99		echo $FREEBSD_BOOTLABEL=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' >> $kboot_conf
100	fi
101fi
102
103# Update the ESP (EFI System Partition) with the new bootloader if we have an ESP
104if [ -n "$(awk '{if ($2=="/boot/efi") printf("%s\n",$1);}' $PATH_FSTAB)" ]; then
105	case $(uname -m) in
106	    arm64)	ARCHBOOTNAME=aa64 ;;
107	    amd64)	ARCHBOOTNAME=x64 ;;
108	    riscv)	ARCHBOOTNAME=riscv64 ;;
109    #	    arm)	ARCHBOOTNAME=arm ;; # No other support for arm install
110    #	    i386)	ARCHBOOTNAME=ia32 ;; # no support for this in i386 kernels, rare machines
111	    *)		die "Unsupported arch $(uname -m) for UEFI install"
112	esac
113	BOOTDIR="/efi/boot"
114	BOOTNAME="${BOOTDIR}/boot${ARCHBOOTNAME}.efi"
115	FREEBSD_BOOTDIR="/efi/freebsd"
116	FREEBSD_BOOTNAME="${FREEBSD_BOOTDIR}/loader.efi"
117	mntpt="$BSDINSTALL_CHROOT/boot/efi"
118
119	f_dprintf "Installing loader.efi onto ESP"
120	mkdir -p "${mntpt}/${FREEBSD_BOOTDIR}" "${mntpt}/${BOOTDIR}"
121	cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${FREEBSD_BOOTNAME}"
122
123	#
124	# The following shouldn't be necessary. UEFI defines a way to
125	# specifically select what to boot (which we do via
126	# efibootmgr). However, virtual environments often times lack
127	# support for the NV variables efibootmgr sets. In addition,
128	# some UEFI implementations have features that interfere with
129	# the setting of these variables. To combat that, we install the
130	# default removable media boot file as a fallback if it doesn't
131	# exist. We don't install it all the time since that can
132	# interfere with other installations on the drive (like rEFInd).
133	#
134	if [ ! -f "${mntpt}/${BOOTNAME}" ]; then
135		cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${BOOTNAME}"
136	fi
137
138	if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
139		update_uefi_bootentry
140	fi
141
142	f_dprintf "Finished configuring ESP"
143fi
144
145# Add boot0cfg for MBR BIOS booting?
146