xref: /freebsd/usr.sbin/bsdinstall/scripts/bootconfig (revision 0b7472b3d8d2f1e90fade5236b44fd98d8e396c2)
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
30a89559c2SYuri PankovBSDCFG_SHARE="/usr/share/bsdconfig"
31a89559c2SYuri Pankov. $BSDCFG_SHARE/common.subr || exit 1
32a89559c2SYuri Pankov
33a107ddbbSRebecca Cran: ${TMPDIR:="/tmp"}
34a107ddbbSRebecca Cran
35db8b5613SRebecca Crandie() {
36db8b5613SRebecca Cran	echo $*
37db8b5613SRebecca Cran	exit 1
38db8b5613SRebecca Cran}
39db8b5613SRebecca Cran
408befcf7bSNathan Whitehornif [ `uname -m` == powerpc ]; then
418befcf7bSNathan Whitehorn	platform=`sysctl -n hw.platform`
428befcf7bSNathan Whitehorn	if [ "$platform" == ps3 -o "$platform" == powernv ]; then
438befcf7bSNathan Whitehorn		rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB)
448befcf7bSNathan Whitehorn		mkdir -p $BSDINSTALL_CHROOT/boot/etc/
45fdb600afSJustin Hibbits		echo FreeBSD=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' > $BSDINSTALL_CHROOT/boot/etc/kboot.conf
468befcf7bSNathan Whitehorn	fi
478befcf7bSNathan Whitehornfi
488befcf7bSNathan Whitehorn
49*0b7472b3SNathan Whitehorn# Update the ESP (EFI System Partition) with the new bootloader if we have an ESP
50*0b7472b3SNathan Whitehornif [ -d "$BSDINSTALL_CHROOT/boot/efi" ]; then
51c6d56081SWarner Losh	case $(uname -m) in
52c6d56081SWarner Losh	    arm64)	ARCHBOOTNAME=aa64 ;;
53c6d56081SWarner Losh	    amd64)	ARCHBOOTNAME=x64 ;;
547b08a307SMitchell Horne	    riscv)	ARCHBOOTNAME=riscv64 ;;
55c6d56081SWarner Losh    #	    arm)	ARCHBOOTNAME=arm ;; # No other support for arm install
56c6d56081SWarner Losh    #	    i386)	ARCHBOOTNAME=ia32 ;; # no support for this in i386 kernels, rare machines
57c6d56081SWarner Losh	    *)		die "Unsupported arch $(uname -m) for UEFI install"
58c6d56081SWarner Losh	esac
59676b7d07SMitchell Horne	BOOTDIR="/efi/boot"
60676b7d07SMitchell Horne	BOOTNAME="${BOOTDIR}/boot${ARCHBOOTNAME}.efi"
61676b7d07SMitchell Horne	FREEBSD_BOOTDIR="/efi/freebsd"
62676b7d07SMitchell Horne	FREEBSD_BOOTNAME="${FREEBSD_BOOTDIR}/loader.efi"
63*0b7472b3SNathan Whitehorn	mntpt="$BSDINSTALL_CHROOT/boot/efi"
64db8b5613SRebecca Cran
65a89559c2SYuri Pankov	f_dprintf "Installing loader.efi onto ESP"
66676b7d07SMitchell Horne	mkdir -p "${mntpt}/${FREEBSD_BOOTDIR}" "${mntpt}/${BOOTDIR}"
67676b7d07SMitchell Horne	cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${FREEBSD_BOOTNAME}"
68db8b5613SRebecca Cran
69c6d56081SWarner Losh	#
70c6d56081SWarner Losh	# The following shouldn't be necessary. UEFI defines a way to
71c6d56081SWarner Losh	# specifically select what to boot (which we do via
72c6d56081SWarner Losh	# efibootmgr). However, virtual environments often times lack
73c6d56081SWarner Losh	# support for the NV variables efibootmgr sets. In addition,
74c6d56081SWarner Losh	# some UEFI implementations have features that interfere with
75c6d56081SWarner Losh	# the setting of these variables. To combat that, we install the
76c6d56081SWarner Losh	# default removable media boot file as a fallback if it doesn't
77c6d56081SWarner Losh	# exist. We don't install it all the time since that can
78c6d56081SWarner Losh	# interfere with other installations on the drive (like rEFInd).
79c6d56081SWarner Losh	#
80c6d56081SWarner Losh	if [ ! -f "${mntpt}/${BOOTNAME}" ]; then
81c6d56081SWarner Losh		cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${BOOTNAME}"
82c6d56081SWarner Losh	fi
83c6d56081SWarner Losh
84db8b5613SRebecca Cran	bootlabel="FreeBSD"
85db8b5613SRebecca Cran
86a89559c2SYuri Pankov	f_dprintf "Creating UEFI boot entry"
87676b7d07SMitchell Horne	efibootmgr --create --activate --label "$bootlabel" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
88db8b5613SRebecca Cran
89*0b7472b3SNathan Whitehorn	f_dprintf "Finished configuring ESP"
90db8b5613SRebecca Cranfi
91db8b5613SRebecca Cran
92db8b5613SRebecca Cran# Add boot0cfg for MBR BIOS booting?
93