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
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32
33: ${TMPDIR:="/tmp"}
34
35die() {
36	echo $*
37	exit 1
38}
39
40if [ `uname -m` == powerpc ]; then
41	platform=`sysctl -n hw.platform`
42	if [ "$platform" == ps3 -o "$platform" == powernv ]; then
43		rootpart=$(awk '{ if($2 == "/") printf("%s:%s\n", $3, $1); }' $PATH_FSTAB)
44		mkdir -p $BSDINSTALL_CHROOT/boot/etc/
45		echo FreeBSD=\'/kernel/kernel kernelname=/boot/kernel/kernel vfs.root.mountfrom=${rootpart}\' > $BSDINSTALL_CHROOT/boot/etc/kboot.conf
46	fi
47fi
48
49# Update the ESP (EFI System Partition) with the new bootloader if we have an ESP
50if [ -n "$(awk '{if ($2=="/boot/efi") printf("%s\n",$1);}' $PATH_FSTAB)" ]; then
51	case $(uname -m) in
52	    arm64)	ARCHBOOTNAME=aa64 ;;
53	    amd64)	ARCHBOOTNAME=x64 ;;
54	    riscv)	ARCHBOOTNAME=riscv64 ;;
55    #	    arm)	ARCHBOOTNAME=arm ;; # No other support for arm install
56    #	    i386)	ARCHBOOTNAME=ia32 ;; # no support for this in i386 kernels, rare machines
57	    *)		die "Unsupported arch $(uname -m) for UEFI install"
58	esac
59	BOOTDIR="/efi/boot"
60	BOOTNAME="${BOOTDIR}/boot${ARCHBOOTNAME}.efi"
61	FREEBSD_BOOTDIR="/efi/freebsd"
62	FREEBSD_BOOTNAME="${FREEBSD_BOOTDIR}/loader.efi"
63	mntpt="$BSDINSTALL_CHROOT/boot/efi"
64
65	f_dprintf "Installing loader.efi onto ESP"
66	mkdir -p "${mntpt}/${FREEBSD_BOOTDIR}" "${mntpt}/${BOOTDIR}"
67	cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${FREEBSD_BOOTNAME}"
68
69	#
70	# The following shouldn't be necessary. UEFI defines a way to
71	# specifically select what to boot (which we do via
72	# efibootmgr). However, virtual environments often times lack
73	# support for the NV variables efibootmgr sets. In addition,
74	# some UEFI implementations have features that interfere with
75	# the setting of these variables. To combat that, we install the
76	# default removable media boot file as a fallback if it doesn't
77	# exist. We don't install it all the time since that can
78	# interfere with other installations on the drive (like rEFInd).
79	#
80	if [ ! -f "${mntpt}/${BOOTNAME}" ]; then
81		cp "$BSDINSTALL_CHROOT/boot/loader.efi" "${mntpt}/${BOOTNAME}"
82	fi
83
84	bootlabel="FreeBSD"
85
86	if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
87		f_dprintf "Creating UEFI boot entry"
88		efibootmgr --create --activate --label "$bootlabel" --loader "${mntpt}/${FREEBSD_BOOTNAME}" > /dev/null
89	fi
90
91	f_dprintf "Finished configuring ESP"
92fi
93
94# Add boot0cfg for MBR BIOS booting?
95