1.\" 2.\" SPDX-License-Identifier: BSD-2-Clause 3.\" 4.\" Copyright (c) 2019-2022 Netflix, Inc 5.\" Copyright (c) 2022 Mateusz Piotrowski <0mp@FreeBSD.org> 6.\" Copyright 2022 The FreeBSD Foundation 7.\" 8.\" Part of this documentation was written by 9.\" Konstantin Belousov <kib@FreeBSD.org> under sponsorship 10.\" from the FreeBSD Foundation. 11.\" 12.\" Redistribution and use in source and binary forms, with or without 13.\" modification, are permitted provided that the following conditions 14.\" are met: 15.\" 1. Redistributions of source code must retain the above copyright 16.\" notice, this list of conditions and the following disclaimer. 17.\" 2. Redistributions in binary form must reproduce the above copyright 18.\" notice, this list of conditions and the following disclaimer in the 19.\" documentation and/or other materials provided with the distribution. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" SUCH DAMAGE. 32.\" 33.Dd September 3, 2024 34.Dt LOADER.EFI 8 35.Os 36.Sh NAME 37.Nm loader.efi 38.Nd UEFI kernel loader 39.Sh DESCRIPTION 40On UEFI systems, 41.Nm 42loads the kernel. 43.Pp 44.Xr boot1.efi 8 45is used to load 46.Nm 47when it is placed within a UFS or ZFS file system. 48Alternatively, 49.Nm 50is used directly when configured with 51.Xr efibootmgr 8 , 52or when placed directly as the default boot program as described in 53.Xr uefi 8 . 54When a system is built using 55.Xr bsdinstall 8 , 56.Nm 57will be used directly. 58.Ss Console Considerations 59The EFI BIOS provides a generic console. 60In 61.Nm 62this is selected by specifying 63.Dq efi 64using the 65.Dv console 66variable. 67.Nm 68examines the 69.Dv 8be4df61-93ca-11d2-aa0d-00e098032b8c-ConOut 70UEFI environment variable to guess what the 71.Dq efi 72console points to. 73.Nm 74will output its prompts and menus to all the places specified by ConOut. 75However, the 76.Fx 77kernel has a limitation when more than one console is present. 78The kernel outputs to all configured consoles. 79Only the primary console will get the log messages from the 80.Xr rc 8 81system, and prompts for things like 82.Xr geli 8 83passwords. 84If 85.Nm 86finds a video device first, then 87.Nm 88tells the kernel to use the video console as primary. 89Likewise, if a serial device is first in the 90.Dv ConOut 91list, the serial port will be the primary console. 92.Pp 93If there is no 94.Dv ConOut 95variable, both serial and video are attempted. 96.Nm 97uses the 98.Dq efi 99console for the video (which may or may not work) and 100.Dq comconsole 101for the serial on 102.Dv COM1 103at the default baud rate. 104The kernel will use a dual console, with the video console 105primary if a UEFI graphics device is detected, or the serial console 106as primary if not. 107.Pp 108On x86 platforms, if you wish to redirect the loader's output to a serial port 109when the EFI BIOS doesn't support it, or to a serial port that isn't the one the 110EFI BIOS redirects its output to, set 111.Dv console 112to 113.Dq comconsole . 114The default port is 115.Dv COM1 116with an I/O address of 0x3f8. 117.Dv comconsole_port 118is used to set this to a different port address. 119.Dv comconsole_speed 120is used to set the of the serial port (the default is 9600). 121If you have 122.Dv console 123set to 124.Dq efi,comconsole 125you will get output on both the EFI console and the serial port. 126If this causes a doubling of characters, set 127.Dv console 128to 129.Dq efi , 130since your EFI BIOS is redirecting to the serial port already. 131.Pp 132If your EFI BIOS redirects the serial port, you may need to tell the kernel 133which address to use. 134EFI uses ACPI's UID to identify the serial port, but 135.Nm 136does not have an ACPI parser, so it cannot convert that to an I/O port. 137The 138.Fx 139kernel initializes its consoles before it can decode ACPI resources. 140The 141.Fx 142kernel will look at the 143.Dv hw.uart.console 144variable to set its serial console. 145Its format should be described in 146.Xr uart 4 147but is not. 148Set it to 149.Dq io:0x3f8,br:115200 150with the proper port address. 151PCI or memory mapped ports are beyond the scope of this man page. 152.Pp 153The serial ports are assigned as follows on IBM PC compatible systems: 154.Bl -column -offset indent ".Sy Windows Name" ".Sy I/O Port Address" ".Sy Typical FreeBSD device" 155.It Sy Windows Name Ta Sy I/O Port Address Ta Sy Typical FreeBSD device 156.It COM1 Ta 0x3f8 Ta Pa /dev/uart0 157.It COM2 Ta 0x2f8 Ta Pa /dev/uart1 158.It COM3 Ta 0x3e8 Ta Pa /dev/uart2 159.It COM4 Ta 0x2e8 Ta Pa /dev/uart3 160.El 161.Pp 162Though 163.Dv COM3 164and 165.Dv COM4 166can vary. 167.Pp 168.Ss Primary Console 169The primary console is set using the boot flags. 170These command line arguments set corresponding flags for the kernel. 171These flags can be controlled by setting loader environment variables 172to 173.Dq yes 174or 175.Dq no . 176Boot flags may be set on the command line to the boot command. 177Inside the kernel, the RB_ flags are used to control behavior, sometimes 178in architecturally specific ways and are included to aid in discovery 179of any behavior not covered in this document. 180.Bl -column -offset indent ".Sy boot flag" ".Sy loader variable" ".Sy Kernel RB_ flag" 181.It Sy boot flag Ta Sy loader variable Ta Sy Kernel RB_ flag 182.It Fl a Ta Dv boot_askme Ta Va RB_ASKNAME 183.It Fl c Ta Dv boot_cdrom Ta Va RB_CDROM 184.It Fl d Ta Dv boot_ddb Ta Va RB_KDB 185.It Fl r Ta Dv boot_dfltroot Ta Va RB_DFLTROOT 186.It Fl D Ta Dv boot_multiple Ta Va RB_MULTIPLE 187.It Fl m Ta Dv boot_mute Ta Va RB_MUTE 188.It Fl g Ta Dv boot_gdb Ta Va RB_GDB 189.It Fl h Ta Dv boot_serial Ta Va RB_SERIAL 190.It Fl p Ta Dv boot_pause Ta Va RB_PAUSE 191.It Fl P Ta Dv boot_probe Ta Va RB_PROBE 192.It Fl s Ta Dv boot_single Ta Va RB_SINGLE 193.It Fl v Ta Dv boot_verbose Ta Va RB_VERBOSE 194.El 195.Pp 196And the following flags determine the primary console: 197.Bl -column -offset xxx "Flags" "RB_SERIAL | RB_MULTIPLE" "Kernel Consoles" "Primary Console" 198.It Sy Flags Ta Sy Kernel Flags Ta Sy Kernel Consoles Ta Sy Primary Console 199.It none Ta 0 Ta Video Ta Video 200.It Fl h Ta RB_SERIAL Ta Serial Ta Serial 201.It Fl D Ta RB_MULTIPLE Ta Serial, Video Ta Video 202.It Fl Dh Ta RB_SERIAL | RB_MULTIPLE Ta Serial, Video Ta Serial 203.El 204.Pp 205.Nm 206does not implement the probe 207.Fl P 208functionality where we use the video console if a keyboard is connected and a 209serial console otherwise. 210.Ss Additional Environment Variables 211.Nm 212loads some extra variables early in startup from 213.Pa /efi/freebsd/loader.env 214from the EFI partition. 215Only simple variables can be set here. 216It can be useful to specify the root filesystem: 217.Bd -literal -offset indent 218rootdev=disk0s1a 219.Ed 220.Ss Staging Slop 221The kernel must parse the firmware memory map tables to know what memory 222it can use. 223Since it must allocate memory to do this, 224.Nm 225ensures there's extra memory available, called 226.Dq slop , 227after everything it loads 228.Po 229the kernel, modules and metadata 230.Pc 231for the kernel to bootstrap the memory allocator. 232.Pp 233By default, amd64 reserves 8MB. 234The 235.Ic staging_slop 236command allows for tuning the slop size. 237It takes a single argument, the size of the slop in bytes. 238.Ss amd64 Nocopy 239.Nm 240will load the kernel into memory that is 2MB aligned below 4GB. 241It cannot load to a fixed address because the UEFI firmware may reserve 242arbitrary memory for its use at runtime. 243Prior to 244.Fx 13.1 , 245kernels retained the old BIOS-boot protocol of loading at exactly 2MB. 246Such kernels must be copied from their loaded location to 2MB prior 247starting them up. 248The 249.Ic copy_staging 250command is used to enable this copying for older kernels. 251It takes a single argument 252which can be one of 253.Bl -tag -width disable 254.It Ar disable 255Force-disable copying staging area to 256.Ad 2M . 257.It Ar enable 258Force-enable copying staging area to 259.Ad 2M . 260.It Ar auto 261Selects the behaviour based on the kernel's capability of boostraping 262from non-2M physical base. 263The kernel reports this capability by exporting the symbol 264.Va kernphys . 265.El 266.Pp 267Arm64 loaders have operated in the 268.Sq nocopy 269mode from their inception, so there is no 270.Ic copy_staging 271command on that platform. 272Riscv, 32-bit arm and arm64 have always loaded at any 273.Ad 2MB 274aligned location, so do not provide 275.Ic copy_staging . 276.Pp 277.Bd -ragged -offset indent 278.Sy Note. 279BIOS loaders on i386 and amd64 put the staging area starting 280at the physical address 281.Ad 2M , 282then enable paging with identical mapping for the low 283.Ad 1G . 284The initial port of 285.Nm 286followed the same scheme for handing control to the kernel, 287since it avoided modifications for the loader/kernel hand-off protocol, 288and for the kernel page table bootstrap. 289.Pp 290This approach is incompatible with the UEFI specification, 291and as a practical matter, caused troubles on many boards, 292because UEFI firmware is free to use any memory for its own needs. 293Applications like 294.Nm 295must only use memory explicitly allocated using boot interfaces. 296The original way also potentially destroyed UEFI runtime interfaces data. 297.Pp 298Eventually, 299.Nm 300and the kernel were improved to avoid this problem. 301.Ed 302.Ss amd64 Faults 303Because it executes in x86 protected mode, the amd64 version of 304.Nm 305is susceptible to CPU faults due to programmer mistakes and 306memory corruption. 307To make debugging such faults easier, amd64 308.Nm 309can provide detailed reporting of the CPU state at the time 310of the fault. 311.Pp 312The 313.Ic grab_faults 314command installs a handler for faults directly in the IDT, 315avoiding the use of the UEFI debugging interface 316.Fn EFI_DEBUG_SUPPORT_PROTOCOL.RegisterExceptionCallback . 317That interface is left available for advanced debuggers in 318the UEFI environment. 319The 320.Ic ungrab_faults 321command tries to deinstall the fault handler, returning TSS and IDT 322CPU tables to their pre-installation state. 323The 324.Ic fault 325command produces a fault in the 326.Nm 327environment for testing purposes, by executing the 328.Ic ud2 329processor instruction. 330.Sh FILES 331.Bl -tag -width "/boot/loader.efi" 332.It Pa /boot/loader.efi 333The location of the UEFI kernel loader within the system. 334.El 335.Ss EFI System Partition 336.Nm 337is installed on the ESP (EFI System Partition) in one of the following locations: 338.Bl -tag -width "efi/freebsd/loader.efi" 339.It Pa efi/boot/bootXXX.efi 340The default location for any EFI loader 341.Po see 342.Xr uefi 8 343for values to replace 344.Ql XXX 345with 346.Pc . 347.It Pa efi/freebsd/loader.efi 348The location reserved specifically for the 349.Fx 350EFI loader. 351.El 352.Pp 353The default location for the ESP mount point is documented in 354.Xr hier 7 . 355.Sh EXAMPLES 356.Ss Updating loader.efi on the ESP 357The following example shows how to install a new 358.Nm 359on the ESP. 360The exact placement is complicated due to the diversity of 361installations, setups and situations. 362In this section, paths that are all lower case are Unix paths. 363Paths that are all upper case are relative to the ESP mount point, 364though they may appear as lower case on your system because the 365FAT filesystem of the ESP is case insensitive. 366.Pp 367Locate the ESP, which has its own partition type of 368.Dq efi : 369.Bd -literal -offset indent 370# gpart show nda0 371=> 40 7501476448 nda0 GPT (3.5T) 372 40 614400 1 efi (300M) 373 614440 7500862048 2 freebsd-zfs (3.5T) 374.Ed 375.Pp 376The name of the ESP on this system is 377.Pa nda0p1 . 378By default, this will be mounted on 379.Pa /boot/efi . 380To check: 381.Bd -literal -offset indent 382# mount | grep nda0p1 383/dev/nda0p1 on /boot/efi (msdosfs, local) 384.Ed 385.Pp 386If it's not mounted, you will need to mount it: 387.Bd -literal -offset indent 388# mount -t msdosfs /dev/nda0p1 /boot/efi 389.Ed 390.Pp 391.Xr efibootmgr 8 392reports what we booted from. 393.Bd -literal -offset indent 394# efibootmgr -v 395Boot to FW : false 396BootCurrent: 0001 397Timeout : 2 seconds 398BootOrder : 0000, 0001, 0003, 0004, 0005, 0006, 0001, 0008, 000A, 000B, 000C, 000E, 0007 399\&... 400+Boot0001* FreeBSD ZPOOL HD(1,GPT,b5d0f86b-265d-1e1b-18aa-0ed55e1e73bd,0x28,0x96000)/File(\eEFI\eFREEBSD\eLOADER.EFI) 401 nda0p1:/EFI/FREEBSD/LOADER.EFI /boot/efi//EFI/FREEBSD/LOADER.EFI 402\&... 403.Ed 404.Pp 405Often there are several options, depending on the BIOS. 406The entry that we booted with is marked with a 407.Sq + 408at the start of the line, as shown above. 409So in this case, this firmware is using 410.Pa /EFI/FREEBSD/LOADER.EFI 411from the ESP. 412Often times it will be the UEFI 413.Dq default 414loader, which varies by architecture. 415.Bl -column -offset indent "Architecture" "Default Path" 416.It Sy Architecture Ta Sy Default Path 417.It amd64 Ta Pa /EFI/BOOT/BOOTX64.EFI 418.It arm Ta Pa /EFI/BOOT/BOOTARM.EFI 419.It arm64 Ta Pa /EFI/BOOT/BOOTAA64.EFI 420.It i386 Ta Pa /EFI/BOOT/BOOTIA32.EFI 421.It riscv Ta Pa /EFI/BOOT/BOOTRISCV64.EFI 422.El 423.Pp 424However, care must be taken: some multiple-boot environments rely on a special 425.Pa bootXXX.efi 426to function. 427Before updating a 428.Pa bootXXX.efi 429file, make sure it is the FreeBSD boot loader before updating it: 430.Bd -literal -offset indent 431# strings /boot/efi/EFI/BOOT/BOOTX64.EFI | grep FreeBSD | grep EFI 432FreeBSD/amd64 EFI loader, Revision 3.0 433.Ed 434.Pp 435.Xr bsdinstall 8 436copies 437.Pa loader.efi 438to the default name if there wasn't one there before. 439Check to see if they are copies before updating (with X64 substituted using the 440above table): 441.Bd -literal -offset indent 442# cmp /boot/efi/EFI/FREEBSD/LOADER.EFI /boot/efi/EFI/BOOT/BOOTX64.EFI 443.Ed 444.Pp 445Copy the loader: 446.Bd -literal -offset indent 447# cp /boot/loader.efi /boot/efi/EFI/FREEBSD/LOADER.EFI 448.Ed 449.Pp 450replacing the all caps part of the example with the proper path. 451.Pp 452If ESP path was 453.Pa /FREEBSD/LOADER.EFI 454and LOADER.EFI and BOOTX64.EFI were identical in the cmp step, 455copy the loader to the default location: 456.Bd -literal -offset indent 457# cp /boot/loader.efi /boot/efi/EFI/BOOT/BOOTX64.EFI 458.Ed 459.Pp 460Finally, if you mounted the ESP, you may wish to unmount it. 461.Bd -literal -offset indent 462# umount /boot/efi 463.Ed 464.Sh SEE ALSO 465.Xr loader 8 , 466.Xr uefi 8 467.Sh BUGS 468Non-x86 serial console handling is even more confusing and less well documented. 469.Pp 470Sometimes when the serial port speed isn't set, 9600 is used. 471Other times the result is typically 115200 since the speed remains unchanged 472from the default. 473.Pp 474U-Boot implements a subset of the UEFI standard. 475Some versions do not support fetching loader variables, so 476.Pa efibootmgr 477may not work. 478In addition, 479.Pa efibootmgr 480is not supported on armv7 or riscv. 481In these instances, the user has to understand what was booted to update 482it properly (and in most cases, it will be the FreeBSD path and the UEFI default 483so just copy loader.efi there if there are loaders there). 484Typically in these embedded situations, there is only one .efi file (loader.efi 485or a copy of loader.efi). 486The path to this file is typically the default removable path above. 487.Pp 488Managing booting multiple OSes on UEFI varies greatly, so extra caution when 489updating the UEFI default loader. 490.Pp 491The old, now obsolete, boot1.efi was installed as bootx64.efi in 492.Fx 10 493and earlier. 494Since it was quite limited in functionality, we created very small 495ESPs by default. 496A modern loader.efi will not fit. 497However, if the old boot1.efi still works, there's no need to update 498it since it will chain boot /boot/loader.efi from a copy that 499make installworld updates. 500