6 # Installs/updates the necessary boot blocks for the desired boot environment
8 # Lightly tested.. Intended to be installed, but until it matures, it will just
9 # be a boot tool for regression testing.
11 # insert code here to guess what you have -- yikes!
13 # Minimum size of FAT filesystems, in KB.
31 gpart show $dev | tail +2 | awk '$4 == "'$part'" { print $3; }'
36 case ${TARGET:-$(uname -m)} in
37 amd64) echo bootx64 ;;
38 arm64) echo bootaa64 ;;
39 i386) echo bootia32 ;;
41 *) die "machine type $(uname -m) doesn't support UEFI" ;;
46 local file sizekb loader device mntpt fatbits efibootname
52 if [ "$sizekb" -ge "$fat32min" ]; then
54 elif [ "$sizekb" -ge "$fat16min" ]; then
60 dd if=/dev/zero of="${file}" bs=1k count="${sizekb}"
61 device=$(mdconfig -a -t vnode -f "${file}")
62 newfs_msdos -F "${fatbits}" -c 1 -L EFISYS "/dev/${device}" > /dev/null 2>&1
63 mntpt=$(mktemp -d /tmp/stand-test.XXXXXX)
64 mount -t msdosfs "/dev/${device}" "${mntpt}"
65 mkdir -p "${mntpt}/EFI/BOOT"
66 efibootname=$(get_uefi_bootname)
67 cp "${loader}" "${mntpt}/EFI/BOOT/${efibootname}.efi"
70 mdconfig -d -u "${device}"
74 local dev file mntpt fstype efibootname kbfree loadersize efibootfile
75 local isboot1 existingbootentryloaderfile bootorder bootentry
81 mntpt=$(mktemp -d /tmp/stand-test.XXXXXX)
83 # See if we're using an existing (formatted) ESP
84 fstype=$(fstyp "${dev}")
86 if [ "${fstype}" != "msdosfs" ]; then
87 newfs_msdos -F 32 -c 1 -L EFISYS "${dev}" > /dev/null 2>&1
90 mount -t msdosfs "${dev}" "${mntpt}"
92 die "Failed to mount ${dev} as an msdosfs filesystem"
95 echo "Mounted ESP ${dev} on ${mntpt}"
97 efibootname=$(get_uefi_bootname)
98 kbfree=$(df -k "${mntpt}" | tail -1 | cut -w -f 4)
99 loadersize=$(stat -f %z "${file}")
100 loadersize=$((loadersize / 1024))
102 # Check if /EFI/BOOT/BOOTxx.EFI is the FreeBSD boot1.efi
103 # If it is, remove it to avoid leaving stale files around
104 efibootfile="${mntpt}/EFI/BOOT/${efibootname}.efi"
105 if [ -f "${efibootfile}" ]; then
106 isboot1=$(strings "${efibootfile}" | grep "FreeBSD EFI boot block")
108 if [ -n "${isboot1}" ] && [ "$kbfree" -lt "${loadersize}" ]; then
109 echo "Only ${kbfree}KB space remaining: removing old FreeBSD boot1.efi file /EFI/BOOT/${efibootname}.efi"
111 rmdir "${mntpt}/EFI/BOOT"
113 echo "${kbfree}KB space remaining on ESP: renaming old boot1.efi file /EFI/BOOT/${efibootname}.efi /EFI/BOOT/${efibootname}-old.efi"
114 mv "${efibootfile}" "${mntpt}/EFI/BOOT/${efibootname}-old.efi"
118 if [ ! -f "${mntpt}/EFI/freebsd/loader.efi" ] && [ "$kbfree" -lt "$loadersize" ]; then
121 echo "Failed to update the EFI System Partition ${dev}"
122 echo "Insufficient space remaining for ${file}"
123 echo "Run e.g \"mount -t msdosfs ${dev} /mnt\" to inspect it for files that can be removed."
127 mkdir -p "${mntpt}/EFI/freebsd"
129 # Keep a copy of the existing loader.efi in case there's a problem with the new one
130 if [ -f "${mntpt}/EFI/freebsd/loader.efi" ] && [ "$kbfree" -gt "$((loadersize * 2))" ]; then
131 cp "${mntpt}/EFI/freebsd/loader.efi" "${mntpt}/EFI/freebsd/loader-old.efi"
134 echo "Copying loader to /EFI/freebsd on ESP"
135 cp "${file}" "${mntpt}/EFI/freebsd/loader.efi"
137 existingbootentryloaderfile=$(efibootmgr -v | grep "${mntpt}//EFI/freebsd/loader.efi")
139 if [ -z "$existingbootentryloaderfile" ]; then
140 # Try again without the double forward-slash in the path
141 existingbootentryloaderfile=$(efibootmgr -v | grep "${mntpt}/EFI/freebsd/loader.efi")
144 if [ -z "$existingbootentryloaderfile" ]; then
145 echo "Creating UEFI boot entry for FreeBSD"
146 efibootmgr --create --label FreeBSD --loader "${mntpt}/EFI/freebsd/loader.efi" > /dev/null
147 if [ $? -ne 0 ]; then
148 die "Failed to create new boot entry"
151 # When creating new entries, efibootmgr doesn't mark them active, so we need to
152 # do so. It doesn't make it easy to find which entry it just added, so rely on
153 # the fact that it places the new entry first in BootOrder.
154 bootorder=$(efivar --name 8be4df61-93ca-11d2-aa0d-00e098032b8c-BootOrder --print --no-name --hex | head -1)
155 bootentry=$(echo "${bootorder}" | cut -w -f 3)$(echo "${bootorder}" | cut -w -f 2)
156 echo "Marking UEFI boot entry ${bootentry} active"
157 efibootmgr --activate "${bootentry}" > /dev/null
159 echo "Existing UEFI FreeBSD boot entry found: not creating a new one"
164 echo "Finished updating ESP"
168 local file loaderfile
173 if [ -f "$file" ]; then
174 make_esp_file ${file} ${fat32min} ${loaderfile}
176 make_esp_device ${file} ${loaderfile}
184 s=$(find-part $dev "!239")
185 if [ -z "$s" ] ; then
186 s=$(find-part $dev "efi")
187 if [ -z "$s" ] ; then
188 die "No ESP slice found"
191 make_esp /dev/${dev}s${s} ${dst}/boot/loader.efi
198 idx=$(find-part $dev "efi")
199 if [ -z "$idx" ] ; then
200 die "No ESP partition found"
202 make_esp /dev/${dev}p${idx} ${dst}/boot/loader.efi
205 boot_nogeli_gpt_ufs_legacy() {
209 idx=$(find-part $dev "freebsd-boot")
210 if [ -z "$idx" ] ; then
211 die "No freebsd-boot partition found"
213 doit gpart bootcode -b ${gpt0} -p ${gpt2} -i $idx $dev
216 boot_nogeli_gpt_ufs_uefi() {
220 boot_nogeli_gpt_ufs_both() {
221 boot_nogeli_gpt_ufs_legacy $1 $2 $3
222 boot_nogeli_gpt_ufs_uefi $1 $2 $3
225 boot_nogeli_gpt_zfs_legacy() {
229 idx=$(find-part $dev "freebsd-boot")
230 if [ -z "$idx" ] ; then
231 die "No freebsd-boot partition found"
233 doit gpart bootcode -b ${gpt0} -p ${gptzfs2} -i $idx $dev
236 boot_nogeli_gpt_zfs_uefi() {
240 boot_nogeli_gpt_zfs_both() {
241 boot_nogeli_gpt_zfs_legacy $1 $2 $3
242 boot_nogeli_gpt_zfs_uefi $1 $2 $3
245 boot_nogeli_mbr_ufs_legacy() {
249 doit gpart bootcode -b ${mbr0} ${dev}
250 s=$(find-part $dev "freebsd")
251 if [ -z "$s" ] ; then
252 die "No freebsd slice found"
254 doit gpart bootcode -p ${mbr2} ${dev}s${s}
257 boot_nogeli_mbr_ufs_uefi() {
261 boot_nogeli_mbr_ufs_both() {
262 boot_nogeli_mbr_ufs_legacy $1 $2 $3
263 boot_nogeli_mbr_ufs_uefi $1 $2 $3
266 boot_nogeli_mbr_zfs_legacy() {
270 # search to find the BSD slice
271 s=$(find-part $dev "freebsd")
272 if [ -z "$s" ] ; then
273 die "No BSD slice found"
275 idx=$(find-part ${dev}s${s} "freebsd-zfs")
276 if [ -z "$idx" ] ; then
277 die "No freebsd-zfs slice found"
279 # search to find the freebsd-zfs partition within the slice
280 # Or just assume it is 'a' because it has to be since it fails otherwise
281 doit gpart bootcode -b ${dst}/boot/mbr ${dev}
282 dd if=${dst}/boot/zfsboot of=/tmp/zfsboot1 count=1
283 doit gpart bootcode -b /tmp/zfsboot1 ${dev}s${s} # Put boot1 into the start of part
284 sysctl kern.geom.debugflags=0x10 # Put boot2 into ZFS boot slot
285 doit dd if=${dst}/boot/zfsboot of=/dev/${dev}s${s}a skip=1 seek=1024
286 sysctl kern.geom.debugflags=0x0
289 boot_nogeli_mbr_zfs_uefi() {
293 boot_nogeli_mbr_zfs_both() {
294 boot_nogeli_mbr_zfs_legacy $1 $2 $3
295 boot_nogeli_mbr_zfs_uefi $1 $2 $3
298 boot_geli_gpt_ufs_legacy() {
299 boot_nogeli_gpt_ufs_legacy $1 $2 $3
302 boot_geli_gpt_ufs_uefi() {
303 boot_nogeli_gpt_ufs_uefi $1 $2 $3
306 boot_geli_gpt_ufs_both() {
307 boot_nogeli_gpt_ufs_both $1 $2 $3
310 boot_geli_gpt_zfs_legacy() {
311 boot_nogeli_gpt_zfs_legacy $1 $2 $3
314 boot_geli_gpt_zfs_uefi() {
315 boot_nogeli_gpt_zfs_uefi $1 $2 $3
318 boot_geli_gpt_zfs_both() {
319 boot_nogeli_gpt_zfs_both $1 $2 $3
322 # GELI+MBR is not a valid configuration
323 boot_geli_mbr_ufs_legacy() {
327 boot_geli_mbr_ufs_uefi() {
331 boot_geli_mbr_ufs_both() {
335 boot_geli_mbr_zfs_legacy() {
339 boot_geli_mbr_zfs_uefi() {
343 boot_geli_mbr_zfs_both() {
347 boot_nogeli_vtoc8_ufs_ofw() {
351 # For non-native builds, ensure that geom_part(4) supports VTOC8.
352 kldload geom_part_vtoc8.ko
353 doit gpart bootcode -p ${vtoc8} ${dev}
357 printf 'Usage: %s -b bios [-d destdir] -f fs [-g geli] [-h] [-o optargs] -s scheme <bootdev>\n' "$0"
359 printf ' bootdev device to install the boot code on\n'
360 printf ' -b bios bios type: legacy, uefi or both\n'
361 printf ' -d destdir destination filesystem root\n'
362 printf ' -f fs filesystem type: ufs or zfs\n'
363 printf ' -g geli yes or no\n'
364 printf ' -h this help/usage text\n'
365 printf ' -o optargs optional arguments\n'
366 printf ' -s scheme mbr or gpt\n'
372 # Note: we really don't support geli boot in this script yet.
375 while getopts "b:d:f:g:ho:s:" opt; do
388 [Yy][Ee][Ss]|geli) geli=geli ;;
405 if [ -n "${scheme}" ] && [ -n "${fs}" ] && [ -n "${bios}" ]; then
410 # For gpt, we need to install pmbr as the primary boot loader
412 gpt0=${srcroot}/boot/pmbr
413 gpt2=${srcroot}/boot/gptboot
414 gptzfs2=${srcroot}/boot/gptzfsboot
416 # For MBR, we have lots of choices, but select mbr, boot0 has issues with UEFI
417 mbr0=${srcroot}/boot/mbr
418 mbr2=${srcroot}/boot/boot
421 vtoc8=${srcroot}/boot/boot1
425 # Check if we've been given arguments. If not, this script is probably being
426 # sourced, so we shouldn't run anything.
427 if [ -n "${dev}" ]; then
428 eval boot_${geli}_${scheme}_${fs}_${bios} $dev $srcroot $opts || echo "Unsupported boot env: ${geli}-${scheme}-${fs}-${bios}"