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 September 4, 2022 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. Only simple variables can be set here. It can be useful to specify the root filesystem: 213.Bd -literal -offset indent 214rootdev=disk0s1a 215.Ed 216.Ss Staging Slop 217The kernel must parse the firmware memory map tables to know what memory 218it can use. 219Since it must allocate memory to do this, 220.Nm 221ensures there's extra memory available, called 222.Dq slop , 223after everything it loads 224.Po 225the kernel, modules and metadata 226.Pc 227for the kernel to bootstrap the memory allocator. 228.Pp 229By default, amd64 reserves 8MB. 230The 231.Ic staging_slop 232command allows for tuning the slop size. 233It takes a single argument, the size of the slop in bytes. 234.Ss amd64 Nocopy 235.Nm 236will load the kernel into memory that is 2MB aligned below 4GB. 237It cannot load to a fixed address because the UEFI firmware may reserve 238arbitrary memory for its use at runtime. 239Prior to 240.Fx 13.1 , 241kernels retained the old BIOS-boot protocol of loading at exactly 2MB. 242Such kernels must be copied from their loaded location to 2MB prior 243starting them up. 244The 245.Ic copy_staging 246command is used to enable this copying for older kernels. 247It takes a single argument 248which can be one of 249.Bl -tag -width disable 250.It Ar disable 251Force-disable copying staging area to 252.Ad 2M . 253.It Ar enable 254Force-enable copying staging area to 255.Ad 2M . 256.It Ar auto 257Selects the behaviour based on the kernel's capability of boostraping 258from non-2M physical base. 259The kernel reports this capability by exporting the symbol 260.Va kernphys . 261.El 262.Pp 263Arm64 loaders have operated in the 264.Sq nocopy 265mode from their inception, so there is no 266.Ic copy_staging 267command on that platform. 268Riscv, 32-bit arm and arm64 have always loaded at any 269.Ad 2MB 270aligned location, so do not provide 271.Ic copy_staging . 272.Pp 273.Bd -ragged -offset indent 274.Sy Note. 275BIOS loaders on i386 and amd64 put the staging area starting 276at the physical address 277.Ad 2M , 278then enable paging with identical mapping for the low 279.Ad 1G . 280The initial port of 281.Nm 282followed the same scheme for handing control to the kernel, 283since it avoided modifications for the loader/kernel hand-off protocol, 284and for the kernel page table bootstrap. 285.Pp 286This approach is incompatible with the UEFI specification, 287and as a practical matter, caused troubles on many boards, 288because UEFI firmware is free to use any memory for its own needs. 289Applications like 290.Nm 291must only use memory explicitly allocated using boot interfaces. 292The original way also potentially destroyed UEFI runtime interfaces data. 293.Pp 294Eventually, 295.Nm 296and the kernel were improved to avoid this problem. 297.Ed 298.Ss amd64 Faults 299Because it executes in x86 protected mode, the amd64 version of 300.Nm 301is susceptible to CPU faults due to programmer mistakes and 302memory corruption. 303To make debugging such faults easier, amd64 304.Nm 305can provide detailed reporting of the CPU state at the time 306of the fault. 307.Pp 308The 309.Ic grab_faults 310command installs a handler for faults directly in the IDT, 311avoiding the use of the UEFI debugging interface 312.Fn EFI_DEBUG_SUPPORT_PROTOCOL.RegisterExceptionCallback . 313That interface is left available for advanced debuggers in 314the UEFI environment. 315The 316.Ic ungrab_faults 317command tries to deinstall the fault handler, returning TSS and IDT 318CPU tables to their pre-installation state. 319The 320.Ic fault 321command produces a fault in the 322.Nm 323environment for testing purposes, by executing the 324.Ic ud2 325processor instruction. 326.Sh FILES 327.Bl -tag -width "/boot/loader.efi" 328.It Pa /boot/loader.efi 329The location of the UEFI kernel loader within the system. 330.El 331.Ss EFI System Partition 332.Nm 333is installed on the ESP (EFI System Partition) in one of the following locations: 334.Bl -tag -width "efi/freebsd/loader.efi" 335.It Pa efi/boot/bootXXX.efi 336The default location for any EFI loader 337.Po see 338.Xr uefi 8 339for values to replace 340.Ql XXX 341with 342.Pc . 343.It Pa efi/freebsd/loader.efi 344The location reserved specifically for the 345.Fx 346EFI loader. 347.El 348.Pp 349The default location for the ESP mount point is documented in 350.Xr hier 7 . 351.Sh EXAMPLES 352.Ss Updating loader.efi on the ESP 353The following examples shows how to install a new 354.Nm 355on the ESP. 356.Pp 357First, find the partition of type 358.Dq efi : 359.Bd -literal -offset indent 360# gpart list | grep -Ew '(Name|efi)' 3611. Name: nvd0p1 362 type: efi 3632. Name: nvd0p2 3643. Name: nvd0p3 3654. Name: nvd0p4 3661. Name: nvd0 367.Ed 368.Pp 369The name of the ESP on this system is 370.Pa nvd0p1 . 371.Pp 372Second, let's mount the ESP, copy 373.Nm 374to the special location reserved for 375.Fx 376EFI loaders, and unmount once finished: 377.Bd -literal -offset indent 378# mount_msdosfs /dev/nvd0p1 /boot/efi 379# cp /boot/loader.efi /boot/efi/efi/freebsd/loader.efi 380# umount /boot/efi 381.Ed 382.Sh SEE ALSO 383.Xr loader 8 , 384.Xr uefi 8 385.Sh BUGS 386Systems that do not have a 387.Dv ConOut 388variable set are not conformant with the standard, and likely have unexpected 389results. 390.Pp 391Non-x86 serial console handling is even more confusing and less well documented. 392.Pp 393Sometimes when the serial port speed isn't set, 9600 is used. 394Other times the result is typically 115200 since the speed remains unchanged 395from the default. 396