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 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. 358The exact placement is complicated due to the diversity of 359installations, setups and situations. 360In this section, paths that are all lower case are Unix paths. 361Paths that are all upper case are relative to the ESP mount point, 362though they may appear as lower case on your system because the 363FAT filesystem of the ESP is case insensitive. 364.Pp 365Locate the ESP, which has its own partition type of 366.Dq efi : 367.Bd -literal -offset indent 368# gpart show nda0 369=> 40 7501476448 nda0 GPT (3.5T) 370 40 614400 1 efi (300M) 371 614440 7500862048 2 freebsd-zfs (3.5T) 372.Ed 373.Pp 374The name of the ESP on this system is 375.Pa nda0p1 . 376By default, this will be mounted on 377.Pa /boot/efi . 378To check: 379.Bd -literal -offset indent 380# mount | grep nda0p1 381/dev/nda0p1 on /boot/efi (msdosfs, local) 382.Ed 383If it's not mounted, you will need to mount it: 384.Bd -literal -offset indent 385# mount -t msdosfs /dev/nda0p1 /boot/efi 386.Ed 387.Pp 388.Xr efibootmgr 8 389reports what we booted from. 390.Bd -literal -offset indent 391# efibootmgr -v 392Boot to FW : false 393BootCurrent: 0001 394Timeout : 2 seconds 395BootOrder : 0000, 0001, 0003, 0004, 0005, 0006, 0001, 0008, 000A, 000B, 000C, 000E, 0007 396\&... 397+Boot0001* FreeBSD ZPOOL HD(1,GPT,b5d0f86b-265d-1e1b-18aa-0ed55e1e73bd,0x28,0x96000)/File(\EFI\FREEBSD\LOADER.EFI) 398 nda0p1:/EFI/FREEBSD/LOADER.EFI /boot/efi//EFI/FREEBSD/LOADER.EFI 399\&... 400.Ed 401Often there are several options, depending on the BIOS. 402The entry that we booted with is marked with a 403.Sq + 404at the start of the line, as shown above. 405So in this case, this firmware is using 406.Pa /EFI/FREEBSD/LOADER.EFI 407from the ESP. 408Often times it will be the UEFI 409.Dq default 410loader, which varies by architecture. 411.Bl -column -offset indent "Architecture" "Default Path" 412.It Sy Architecture Ta Sy Default Path 413.It amd64 Ta Pa /EFI/BOOT/BOOTX64.EFI 414.It arm Ta Pa /EFI/BOOT/BOOTARM.EFI 415.It arm64 Ta Pa /EFI/BOOT/BOOTAA64.EFI 416.It i386 Ta Pa /EFI/BOOT/BOOTIA32.EFI 417.It riscv Ta Pa /EFI/BOOT/BOOTRISCV64.EFI 418.El 419However, care must be taken: some multiple-boot environments rely on a special 420.Pa bootXXX.efi 421to function. 422Before updating a 423.Pa bootXXX.efi 424file, make sure it is the FreeBSD boot loader before updating it: 425.Bd -literal -offset indent 426# strings /boot/efi/EFI/BOOT/BOOTX64.EFI | grep FreeBSD | grep EFI 427FreeBSD/amd64 EFI loader, Revision 3.0 428.Ed 429.Pp 430.Xr bsdinstall 8 431copies 432.Pa loader.efi 433to the default name if there wasn't one there before. 434Check to see if they are copies before updating (with X64 substituted using the 435above table): 436.Bd -literal -offset indent 437# cmp /boot/efi/EFI/FREEBSD/LOADER.EFI /boot/efi/EFI/BOOT/BOOTX64.EFI 438.Ed 439Copy the loader: 440.Bd -literal -offset indent 441# cp /boot/loader.efi /boot/efi/EFI/FREEBSD/LOADER.EFI 442.Ed 443replacing the all caps part of the example with the proper path. 444.Pp 445If ESP path was 446.Pa /FREEBSD/LOADER.EFI 447and LOADER.EFI and BOOTX64.EFI were identical in the cmp step, 448copy the loader to the default location: 449.Bd -literal -offset indent 450# cp /boot/loader.efi /boot/efi/EFI/BOOT/BOOTX64.EFI 451.Ed 452.Pp 453Finally, if you mounted the ESP, you may wish to unmount it. 454.Bd -literal -offset indent 455# umount /boot/efi 456.Ed 457.Sh SEE ALSO 458.Xr loader 8 , 459.Xr uefi 8 460.Sh BUGS 461Non-x86 serial console handling is even more confusing and less well documented. 462.Pp 463Sometimes when the serial port speed isn't set, 9600 is used. 464Other times the result is typically 115200 since the speed remains unchanged 465from the default. 466.Pp 467U-Boot implements a subset of the UEFI standard. 468Some versions do not support fetching loader variables, so 469.Pa efibootmgr 470may not work. 471In addition, 472.Pa efibootmgr 473is not supported on armv7 or riscv. 474In these instances, the user has to understand what was booted to update 475it properly (and in most cases, it will be the FreeBSD path and the UEFI default 476so just copy loader.efi there if there are loaders there). 477Typically in these embedded situations, there is only one .efi file (loader.efi 478or a copy of loader.efi). 479The path to this file is typically the default removable path above. 480.Pp 481Managing booting multiple OSes on UEFI varies greatly, so extra caution when 482updating the UEFI default loader. 483.Pp 484The old, now obsolete, boot1.efi was installed as bootx64.efi in 485.Fx 10 486and earlier. 487Since it was quite limited in functionality, we created very small 488ESPs by default. 489A modern loader.efi will not fit. 490However, if the old boot1.efi still works, there's no need to update 491it since it will chain boot /boot/loader.efi from a copy that 492make installworld updates. 493