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