xref: /freebsd/usr.sbin/bsdinstall/scripts/bootconfig (revision 32cd3ee5901ea33d41ff550e5f40ce743c8d4165)
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
32
33FREEBSD_BOOTLABEL=$OSNAME
34
35f_dprintf "%s: loading_includes..." "$0"
36f_include $BSDCFG_SHARE/dialog.subr
37
38: ${TMPDIR:="/tmp"}
39
40die() {
41	echo $*
42	exit 1
43}
44
45dialog_uefi_entryname()
46{
47	local prompt="Please enter a name for the new entry"
48	local hline=
49	local value="$*"
50	local height width
51
52	f_dialog_inputbox_size height width \
53		"$DIALOG_TITLE" "$DIALOG_BACKTITLE" "$prompt" "$value" "$hline"
54
55	$DIALOG \
56		--title "$DIALOG_TITLE"         \
57		--backtitle "$DIALOG_BACKTITLE" \
58		--hline "$hline"                \
59		--ok-label "Ok"                 \
60		--no-cancel                     \
61		--inputbox "$prompt"            \
62		$height $width "$value"         \
63		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
64}
65
66# Copy to the normal FreeBSD location. Also copy to the default location if it
67# doesn't exist. This covers setups where UEFI NV variables can't be set and
68# some buggy firmware, while preserving complex UEFI setups for multiple booting
69# (rEFInd, etc).
70uefi_copy_loader()
71{
72    local ldr=$1
73    local freebsd_dir=$2
74    local default_dir=$3
75    local dest=$4
76
77    mkdir -p "${freebsd_dir}" "${default_dir}"
78    cp "${ldr}" "${freebsd_dir}"
79    if [ ! -f "${default_dir}/${dest}" ]; then
80	cp "${ldr}" "${default_dir}/${dest}"
81    fi
82}
83
84update_uefi_bootentry()
85{
86	local nentries=$(efibootmgr | grep -c "${EFI_LABEL_NAME}$")
87	local loader_path=$1
88
89	# No entries so directly create one and return
90	if [ ${nentries} -eq 0 ]; then
91		f_dprintf "Creating UEFI boot entry"
92		efibootmgr --create --activate --label "$EFI_LABEL_NAME" --loader "${loader_path}" > /dev/null
93		return
94	fi
95
96	$DIALOG --backtitle "$OSNAME Installer" --title 'Boot Configuration' \
97	    --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
98	if [ $? -eq $DIALOG_OK ]; then
99		for entry in $(efibootmgr | awk "\$NF == \"$EFI_LABEL_NAME\" { sub(/.*Boot/,\"\", \$1); sub(/\*/,\"\", \$1); print \$1 }"); do
100			efibootmgr -B -b ${entry}
101		done
102		efibootmgr --create --activate --label "$EFI_LABEL_NAME" --loader "${loader_path}" > /dev/null
103		return
104	fi
105
106	FREEBSD_BOOTLABEL=$(dialog_uefi_entryname "${EFI_LABEL_NAME}")
107	[ $? -eq $DIALOG_CANCEL ] && exit 1
108	efibootmgr --create --activate --label "$FREEBSD_BOOTLABEL" --loader "${loader_path}" > /dev/null
109}
110
111f_dialog_title "Boot Configuration"
112f_dialog_backtitle "$OSNAME Installer"
113
114if [ `uname -m` == powerpc ]; then
115	platform=`sysctl -n hw.platform`
116	if [ "$platform" == ps3 -o "$platform" == powernv ]; then
117		rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB)
118		kboot_conf=$BSDINSTALL_CHROOT/boot/etc/kboot.conf
119		mkdir -p $BSDINSTALL_CHROOT/boot/etc/
120		echo default=$FREEBSD_BOOTLABEL > $kboot_conf
121		echo $FREEBSD_BOOTLABEL=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' >> $kboot_conf
122	fi
123fi
124
125# Update the ESP (EFI System Partition) with the new bootloader if we have an ESP
126if [ -n "$(awk '{if ($2=="/boot/efi") printf("%s\n",$1);}' $PATH_FSTAB)" ]; then
127	case $(uname -m) in
128	    arm64)	ARCHBOOTNAME=aa64 ;;
129	    amd64)	ARCHBOOTNAME=x64 ;;
130	    riscv)	ARCHBOOTNAME=riscv64 ;;
131    #	    arm)	ARCHBOOTNAME=arm ;; # No other support for arm install
132    #	    i386)	ARCHBOOTNAME=ia32 ;; # no support for this in i386 kernels, rare machines
133	    *)		die "Unsupported arch $(uname -m) for UEFI install"
134	esac
135
136	# Support the weird 32-bit firmware loading 64-bit kernels
137	if [ `sysctl -n machdep.efi_arch` == i386 ]; then
138		ARCHBOOTNAME=ia32
139		file=loader_ia32.efi
140	else
141		file=loader.efi
142	fi
143
144	# Copy the boot loader
145	mntpt="$BSDINSTALL_CHROOT/boot/efi"
146	f_dprintf "Installing ${file} onto ESP"
147	uefi_copy_loader "$BSDINSTALL_CHROOT/boot/${file}" \
148		"${mntpt}/efi/freebsd" "${mntpt}/efi/boot" \
149		boot${ARCHBOOTNAME}.efi
150
151	# zfsboot records the extra esp partitions it creates to -esps.  These
152	# are newfs'd at the time of creation. We don't support installing ufs
153	# over gmirror, so we only do this for ZFS.
154	esps=${TMPDIR:-"/tmp"}/bsdinstall-esps
155	if [ -f "$esps" ]; then
156		tmpmnt=$(mktemp -d -t bsdinstall-esp)
157		for dev in $(cat $esps); do
158			f_dprintf "Installing ${file} onto redundant ESP ${dev}"
159			mount -t msdos "$dev" "$tmpmnt"
160			uefi_copy_loader "$BSDINSTALL_CHROOT/boot/${file}" \
161				"${tmpmnt}/efi/freebsd" "${tmpmnt}/efi/boot" \
162				boot${ARCHBOOTNAME}.efi
163			umount "$tmpmnt"
164		done
165		rmdir "${tmpmnt}"
166	fi
167
168	# Try to set the UEFI NV BootXXXX variables to record the boot location.
169	# Note that the ESP is mounted at ${mntpt}/efi.
170	if [ "$BSDINSTALL_CONFIGCURRENT" ] && [ "$ARCHBOOTNAME" != ia32 ]; then
171		update_uefi_bootentry "${mntpt}/efi/freebsd/${file}"
172	fi
173
174	f_dprintf "Finished configuring ESP"
175fi
176
177# Add boot0cfg for MBR BIOS booting?
178