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, Inc. 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 November 22, 2023 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 161Though 162.Dv COM3 163and 164.Dv COM4 165can vary. 166.Pp 167.Ss Primary Console 168The primary console is set using the boot flags. 169These command line arguments set corresponding flags for the kernel. 170These flags can be controlled by setting loader environment variables 171to 172.Dq yes 173or 174.Dq no . 175Boot flags may be set on the command line to the boot command. 176Inside the kernel, the RB_ flags are used to control behavior, sometimes 177in architecturally specific ways and are included to aid in discovery 178of any behavior not covered in this document. 179.Bl -column -offset indent ".Sy boot flag" ".Sy loader variable" ".Sy Kernel RB_ flag" 180.It Sy boot flag Ta Sy loader variable Ta Sy Kernel RB_ flag 181.It Fl a Ta Dv boot_askme Ta Va RB_ASKNAME 182.It Fl c Ta Dv boot_cdrom Ta Va RB_CDROM 183.It Fl d Ta Dv boot_ddb Ta Va RB_KDB 184.It Fl r Ta Dv boot_dfltroot Ta Va RB_DFLTROOT 185.It Fl D Ta Dv boot_multiple Ta Va RB_MULTIPLE 186.It Fl m Ta Dv boot_mute Ta Va RB_MUTE 187.It Fl g Ta Dv boot_gdb Ta Va RB_GDB 188.It Fl h Ta Dv boot_serial Ta Va RB_SERIAL 189.It Fl p Ta Dv boot_pause Ta Va RB_PAUSE 190.It Fl P Ta Dv boot_probe Ta Va RB_PROBE 191.It Fl s Ta Dv boot_single Ta Va RB_SINGLE 192.It Fl v Ta Dv boot_verbose Ta Va RB_VERBOSE 193.El 194And the following flags determine the primary console: 195.Bl -column -offset indent ".Sy Flags" ".Sy Kernel Flags" ".Sy Kernel Consoles" ".Sy Primary Console" 196.It Sy Flags Ta Sy Kernel Flags Ta Sy Kernel Consoles Ta Sy Primary Console 197.It none Ta 0 Ta Video Ta Video 198.It Fl h Ta RB_SERIAL Ta Serial Ta Serial 199.It Fl D Ta RB_MULTIPLE Ta Serial, Video Ta Video 200.It Fl Dh Ta RB_SERIAL | RB_MULTIPLE Ta Serial, Video Ta Serial 201.El 202.Pp 203.Nm 204does not implement the probe 205.Fl P 206functionality where we use the video console if a keyboard is connected and a 207serial console otherwise. 208.Ss Additional Environment Variables 209.Nm 210loads some extra variables early in startup from 211.Pa /efi/freebsd/loader.env 212from the EFI partition. 213Only simple variables can be set here. 214It can be useful to specify the root filesystem: 215.Bd -literal -offset indent 216rootdev=disk0s1a 217.Ed 218.Ss Staging Slop 219The kernel must parse the firmware memory map tables to know what memory 220it can use. 221Since it must allocate memory to do this, 222.Nm 223ensures there's extra memory available, called 224.Dq slop , 225after everything it loads 226.Po 227the kernel, modules and metadata 228.Pc 229for the kernel to bootstrap the memory allocator. 230.Pp 231By default, amd64 reserves 8MB. 232The 233.Ic staging_slop 234command allows for tuning the slop size. 235It takes a single argument, the size of the slop in bytes. 236.Ss amd64 Nocopy 237.Nm 238will load the kernel into memory that is 2MB aligned below 4GB. 239It cannot load to a fixed address because the UEFI firmware may reserve 240arbitrary memory for its use at runtime. 241Prior to 242.Fx 13.1 , 243kernels retained the old BIOS-boot protocol of loading at exactly 2MB. 244Such kernels must be copied from their loaded location to 2MB prior 245starting them up. 246The 247.Ic copy_staging 248command is used to enable this copying for older kernels. 249It takes a single argument 250which can be one of 251.Bl -tag -width disable 252.It Ar disable 253Force-disable copying staging area to 254.Ad 2M . 255.It Ar enable 256Force-enable copying staging area to 257.Ad 2M . 258.It Ar auto 259Selects the behaviour based on the kernel's capability of boostraping 260from non-2M physical base. 261The kernel reports this capability by exporting the symbol 262.Va kernphys . 263.El 264.Pp 265Arm64 loaders have operated in the 266.Sq nocopy 267mode from their inception, so there is no 268.Ic copy_staging 269command on that platform. 270Riscv, 32-bit arm and arm64 have always loaded at any 271.Ad 2MB 272aligned location, so do not provide 273.Ic copy_staging . 274.Pp 275.Bd -ragged -offset indent 276.Sy Note. 277BIOS loaders on i386 and amd64 put the staging area starting 278at the physical address 279.Ad 2M , 280then enable paging with identical mapping for the low 281.Ad 1G . 282The initial port of 283.Nm 284followed the same scheme for handing control to the kernel, 285since it avoided modifications for the loader/kernel hand-off protocol, 286and for the kernel page table bootstrap. 287.Pp 288This approach is incompatible with the UEFI specification, 289and as a practical matter, caused troubles on many boards, 290because UEFI firmware is free to use any memory for its own needs. 291Applications like 292.Nm 293must only use memory explicitly allocated using boot interfaces. 294The original way also potentially destroyed UEFI runtime interfaces data. 295.Pp 296Eventually, 297.Nm 298and the kernel were improved to avoid this problem. 299.Ed 300.Ss amd64 Faults 301Because it executes in x86 protected mode, the amd64 version of 302.Nm 303is susceptible to CPU faults due to programmer mistakes and 304memory corruption. 305To make debugging such faults easier, amd64 306.Nm 307can provide detailed reporting of the CPU state at the time 308of the fault. 309.Pp 310The 311.Ic grab_faults 312command installs a handler for faults directly in the IDT, 313avoiding the use of the UEFI debugging interface 314.Fn EFI_DEBUG_SUPPORT_PROTOCOL.RegisterExceptionCallback . 315That interface is left available for advanced debuggers in 316the UEFI environment. 317The 318.Ic ungrab_faults 319command tries to deinstall the fault handler, returning TSS and IDT 320CPU tables to their pre-installation state. 321The 322.Ic fault 323command produces a fault in the 324.Nm 325environment for testing purposes, by executing the 326.Ic ud2 327processor instruction. 328.Sh FILES 329.Bl -tag -width "/boot/loader.efi" 330.It Pa /boot/loader.efi 331The location of the UEFI kernel loader within the system. 332.El 333.Ss EFI System Partition 334.Nm 335is installed on the ESP (EFI System Partition) in one of the following locations: 336.Bl -tag -width "efi/freebsd/loader.efi" 337.It Pa efi/boot/bootXXX.efi 338The default location for any EFI loader 339.Po see 340.Xr uefi 8 341for values to replace 342.Ql XXX 343with 344.Pc . 345.It Pa efi/freebsd/loader.efi 346The location reserved specifically for the 347.Fx 348EFI loader. 349.El 350.Pp 351The default location for the ESP mount point is documented in 352.Xr hier 7 . 353.Sh EXAMPLES 354.Ss Updating loader.efi on the ESP 355The following example shows how to install a new 356.Nm 357on the ESP. 358.Pp 359First, find the partition of type 360.Dq efi : 361.Bd -literal -offset indent 362# gpart list | grep -Ew '(Name|efi)' 3631. Name: nvd0p1 364 type: efi 3652. Name: nvd0p2 3663. Name: nvd0p3 3674. Name: nvd0p4 3681. Name: nvd0 369.Ed 370.Pp 371The name of the ESP on this system is 372.Pa nvd0p1 . 373.Pp 374Second, let's mount the ESP, copy 375.Nm 376to the special location reserved for 377.Fx 378EFI loaders, and unmount once finished: 379.Bd -literal -offset indent 380# mount_msdosfs /dev/nvd0p1 /boot/efi 381# cp /boot/loader.efi /boot/efi/efi/freebsd/loader.efi 382# umount /boot/efi 383.Ed 384.Sh SEE ALSO 385.Xr loader 8 , 386.Xr uefi 8 387.Sh BUGS 388Systems that do not have a 389.Dv ConOut 390variable set are not conformant with the standard, and likely have unexpected 391results. 392.Pp 393Non-x86 serial console handling is even more confusing and less well documented. 394.Pp 395Sometimes when the serial port speed isn't set, 9600 is used. 396Other times the result is typically 115200 since the speed remains unchanged 397from the default. 398