blob: 8f81b79907298683139174b1f52f1ba85661dd74 [file] [log] [blame]
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +02001.. SPDX-License-Identifier: GPL-2.0+
2.. Copyright (C) 2015 Google, Inc
Heinrich Schuchardt32409242018-01-30 20:03:02 +01003
4U-Boot on EFI
5=============
6This document provides information about U-Boot running on top of EFI, either
7as an application or just as a means of getting U-Boot onto a new platform.
8
9
Heinrich Schuchardt32409242018-01-30 20:03:02 +010010Motivation
11----------
12Running U-Boot on EFI is useful in several situations:
13
14- You have EFI running on a board but U-Boot does not natively support it
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020015 fully yet. You can boot into U-Boot from EFI and use that until U-Boot is
16 fully ported
Heinrich Schuchardt32409242018-01-30 20:03:02 +010017
18- You need to use an EFI implementation (e.g. UEFI) because your vendor
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020019 requires it in order to provide support
Heinrich Schuchardt32409242018-01-30 20:03:02 +010020
21- You plan to use coreboot to boot into U-Boot but coreboot support does
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020022 not currently exist for your platform. In the meantime you can use U-Boot
23 on EFI and then move to U-Boot on coreboot when ready
Heinrich Schuchardt32409242018-01-30 20:03:02 +010024
25- You use EFI but want to experiment with a simpler alternative like U-Boot
26
27
28Status
29------
30Only x86 is supported at present. If you are using EFI on another architecture
31you may want to reconsider. However, much of the code is generic so could be
32ported.
33
34U-Boot supports running as an EFI application for 32-bit EFI only. This is
35not very useful since only a serial port is provided. You can look around at
36memory and type 'help' but that is about it.
37
38More usefully, U-Boot supports building itself as a payload for either 32-bit
39or 64-bit EFI. U-Boot is packaged up and loaded in its entirety by EFI. Once
40started, U-Boot changes to 32-bit mode (currently) and takes over the
41machine. You can use devices, boot a kernel, etc.
42
43
44Build Instructions
45------------------
46First choose a board that has EFI support and obtain an EFI implementation
47for that board. It will be either 32-bit or 64-bit. Alternatively, you can
48opt for using QEMU [1] and the OVMF [2], as detailed below.
49
50To build U-Boot as an EFI application (32-bit EFI required), enable CONFIG_EFI
Simon Glass49a643b2021-11-03 21:09:07 -060051and CONFIG_EFI_APP. The efi-x86_app config (efi-x86_app32_defconfig) is set up
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020052for this. Just build U-Boot as normal, e.g.::
Heinrich Schuchardt32409242018-01-30 20:03:02 +010053
Simon Glass49a643b2021-11-03 21:09:07 -060054 make efi-x86_app32_defconfig
Heinrich Schuchardt32409242018-01-30 20:03:02 +010055 make
56
Bin Meng858429b2018-06-12 08:36:18 -070057To build U-Boot as an EFI payload (32-bit or 64-bit EFI can be used), enable
58CONFIG_EFI, CONFIG_EFI_STUB, and select either CONFIG_EFI_STUB_32BIT or
59CONFIG_EFI_STUB_64BIT. The efi-x86_payload configs (efi-x86_payload32_defconfig
60and efi-x86_payload32_defconfig) are set up for this. Then build U-Boot as
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020061normal, e.g.::
Heinrich Schuchardt32409242018-01-30 20:03:02 +010062
Bin Meng858429b2018-06-12 08:36:18 -070063 make efi-x86_payload32_defconfig (or efi-x86_payload64_defconfig)
Heinrich Schuchardt32409242018-01-30 20:03:02 +010064 make
65
66You will end up with one of these files depending on what you build for:
67
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020068* u-boot-app.efi - U-Boot EFI application
69* u-boot-payload.efi - U-Boot EFI payload application
Heinrich Schuchardt32409242018-01-30 20:03:02 +010070
71
72Trying it out
73-------------
74QEMU is an emulator and it can emulate an x86 machine. Please make sure your
Bin Meng74efc6e2021-06-21 10:12:32 +080075QEMU version is 6.0.0 or above to test this. You can run the payload with
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020076something like this::
Heinrich Schuchardt32409242018-01-30 20:03:02 +010077
78 mkdir /tmp/efi
79 cp /path/to/u-boot*.efi /tmp/efi
Bin Meng74efc6e2021-06-21 10:12:32 +080080 qemu-system-x86_64 -pflash edk2-x86_64-code.fd -hda fat:rw:/tmp/efi/
Heinrich Schuchardt32409242018-01-30 20:03:02 +010081
82Add -nographic if you want to use the terminal for output. Once it starts
83type 'fs0:u-boot-payload.efi' to run the payload or 'fs0:u-boot-app.efi' to
Bin Meng74efc6e2021-06-21 10:12:32 +080084run the application. 'edk2-x86_64-code.fd' is the EFI 'BIOS'. QEMU already
85ships both 32-bit and 64-bit EFI BIOS images. For 32-bit EFI 'BIOS' image,
86use 'edk2-i386-code.fd'.
87
Heinrich Schuchardt32409242018-01-30 20:03:02 +010088
89To try it on real hardware, put u-boot-app.efi on a suitable boot medium,
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020090such as a USB stick. Then you can type something like this to start it::
Heinrich Schuchardt32409242018-01-30 20:03:02 +010091
92 fs0:u-boot-payload.efi
93
94(or fs0:u-boot-app.efi for the application)
95
96This will start the payload, copy U-Boot into RAM and start U-Boot. Note
97that EFI does not support booting a 64-bit application from a 32-bit
98EFI (or vice versa). Also it will often fail to print an error message if
99you get this wrong.
100
Simon Glassdaa53a22021-11-03 21:09:03 -0600101You may find the script `scripts/build-efi.sh` helpful for building and testing
102U-Boot on UEFI on QEMU. It also includes links to UEFI binaries dating from
1032021.
104
105See `Example run`_ for an example run.
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100106
107Inner workings
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200108--------------
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100109Here follow a few implementation notes for those who want to fiddle with
110this and perhaps contribute patches.
111
112The application and payload approaches sound similar but are in fact
113implemented completely differently.
114
115EFI Application
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200116~~~~~~~~~~~~~~~
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100117For the application the whole of U-Boot is built as a shared library. The
118efi_main() function is in lib/efi/efi_app.c. It sets up some basic EFI
119functions with efi_init(), sets up U-Boot global_data, allocates memory for
120U-Boot's malloc(), etc. and enters the normal init sequence (board_init_f()
121and board_init_r()).
122
123Since U-Boot limits its memory access to the allocated regions very little
124special code is needed. The CONFIG_EFI_APP option controls a few things
125that need to change so 'git grep CONFIG_EFI_APP' may be instructive.
126The CONFIG_EFI option controls more general EFI adjustments.
127
128The only available driver is the serial driver. This calls back into EFI
129'boot services' to send and receive characters. Although it is implemented
130as a serial driver the console device is not necessarilly serial. If you
131boot EFI with video output then the 'serial' device will operate on your
132target devices's display instead and the device's USB keyboard will also
133work if connected. If you have both serial and video output, then both
134consoles will be active. Even though U-Boot does the same thing normally,
135These are features of EFI, not U-Boot.
136
137Very little code is involved in implementing the EFI application feature.
138U-Boot is highly portable. Most of the difficulty is in modifying the
139Makefile settings to pass the right build flags. In particular there is very
140little x86-specific code involved - you can find most of it in
141arch/x86/cpu. Porting to ARM (which can also use EFI if you are brave
142enough) should be straightforward.
143
144Use the 'reset' command to get back to EFI.
145
146EFI Payload
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200147~~~~~~~~~~~
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100148The payload approach is a different kettle of fish. It works by building
149U-Boot exactly as normal for your target board, then adding the entire
150image (including device tree) into a small EFI stub application responsible
151for booting it. The stub application is built as a normal EFI application
152except that it has a lot of data attached to it.
153
154The stub application is implemented in lib/efi/efi_stub.c. The efi_main()
155function is called by EFI. It is responsible for copying U-Boot from its
156original location into memory, disabling EFI boot services and starting
157U-Boot. U-Boot then starts as normal, relocates, starts all drivers, etc.
158
159The stub application is architecture-dependent. At present it has some
160x86-specific code and a comment at the top of efi_stub.c describes this.
161
162While the stub application does allocate some memory from EFI this is not
163used by U-Boot (the payload). In fact when U-Boot starts it has all of the
164memory available to it and can operate as it pleases (but see the next
165section).
166
167Tables
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200168~~~~~~
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100169The payload can pass information to U-Boot in the form of EFI tables. At
170present this feature is used to pass the EFI memory map, an inordinately
171large list of memory regions. You can use the 'efi mem all' command to
172display this list. U-Boot uses the list to work out where to relocate
173itself.
174
175Although U-Boot can use any memory it likes, EFI marks some memory as used
176by 'run-time services', code that hangs around while U-Boot is running and
177is even present when Linux is running. This is common on x86 and provides
178a way for Linux to call back into the firmware to control things like CPU
179fan speed. U-Boot uses only 'conventional' memory, in EFI terminology. It
180will relocate itself to the top of the largest block of memory it can find
181below 4GB.
182
183Interrupts
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200184~~~~~~~~~~
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100185U-Boot drivers typically don't use interrupts. Since EFI enables interrupts
186it is possible that an interrupt will fire that U-Boot cannot handle. This
187seems to cause problems. For this reason the U-Boot payload runs with
188interrupts disabled at present.
189
19032/64-bit
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200191~~~~~~~~~
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100192While the EFI application can in principle be built as either 32- or 64-bit,
193only 32-bit is currently supported. This means that the application can only
194be used with 32-bit EFI.
195
196The payload stub can be build as either 32- or 64-bits. Only a small amount
197of code is built this way (see the extra- line in lib/efi/Makefile).
198Everything else is built as a normal U-Boot, so is always 32-bit on x86 at
199present.
200
Simon Glassdaa53a22021-11-03 21:09:03 -0600201Example run
202-----------
203
204This shows running with serial enabled (see `include/configs/efi-x86_app.h`)::
205
206 $ scripts/build-efi.sh -wsPr
207 Packaging efi-x86_app32
208 Running qemu-system-i386
209
210 BdsDxe: failed to load Boot0001 "UEFI QEMU HARDDISK QM00005 " from PciRoot(0x0)/Pci(0x3,0x0)/Sata(0x0,0xFFFF,0x0): Not Found
211 BdsDxe: loading Boot0002 "EFI Internal Shell" from Fv(7CB8BDC9-F8EB-4F34-AAEA-3EE4AF6516A1)/FvFile(7C04A583-9E3E-4F1C-AD65-E05268D0B4D1)
212 BdsDxe: starting Boot0002 "EFI Internal Shell" from Fv(7CB8BDC9-F8EB-4F34-AAEA-3EE4AF6516A1)/FvFile(7C04A583-9E3E-4F1C-AD65-E05268D0B4D1)
213
214 UEFI Interactive Shell v2.2
215 EDK II
216 UEFI v2.70 (EDK II, 0x00010000)
217 Mapping table
218 FS0: Alias(s):HD0a65535a1:;BLK1:
219 PciRoot(0x0)/Pci(0x3,0x0)/Sata(0x0,0xFFFF,0x0)/HD(1,GPT,0FFD5E61-3B0C-4326-8049-BDCDC910AF72,0x800,0xB000)
220 BLK0: Alias(s):
221 PciRoot(0x0)/Pci(0x3,0x0)/Sata(0x0,0xFFFF,0x0)
222
223 Press ESC in 5 seconds to skip startup.nsh or any other key to continue.
224 Shell> fs0:u-boot-app.efi
225 U-Boot EFI App (using allocated RAM address 47d4000) key=8d4, image=06a6f610
226 starting
227
228
229 U-Boot 2022.01-rc4 (Sep 19 2021 - 14:03:20 -0600)
230
231 CPU: x86, vendor Intel, device 663h
232 DRAM: 32 MiB
233 0: efi_media_0 PciRoot(0x0)/Pci(0x3,0x0)/Sata(0x0,0xFFFF,0x0)
234 1: <partition> PciRoot(0x0)/Pci(0x3,0x0)/Sata(0x0,0xFFFF,0x0)/HD(1,GPT,0FFD5E61-3B0C-4326-8049-BDCDC910AF72,0x800,0xB000)
235 Loading Environment from nowhere... OK
236 Model: EFI x86 Application
237 Hit any key to stop autoboot: 0
238
239 Partition Map for EFI device 0 -- Partition Type: EFI
240
241 Part Start LBA End LBA Name
242 Attributes
243 Type GUID
244 Partition GUID
245 1 0x00000800 0x0000b7ff "boot"
246 attrs: 0x0000000000000000
247 type: ebd0a0a2-b9e5-4433-87c0-68b6b72699c7
248 guid: 0ffd5e61-3b0c-4326-8049-bdcdc910af72
249 19 startup.nsh
250 528384 u-boot-app.efi
251 10181 NvVars
252
253 3 file(s), 0 dir(s)
254
255 => QEMU: Terminated
256
257
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100258Future work
259-----------
260This work could be extended in a number of ways:
261
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100262- Add ARM support
263
Simon Glass49a643b2021-11-03 21:09:07 -0600264- Add 64-bit application support (in progress)
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100265
266- Figure out how to solve the interrupt problem
267
Simon Glass2d4b33e2021-12-29 11:57:36 -0700268- Add more drivers to the application side (e.g.USB, environment access).
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100269
270- Avoid turning off boot services in the stub. Instead allow U-Boot to make
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200271 use of boot services in case it wants to. It is unclear what it might want
272 though.
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100273
274Where is the code?
275------------------
276lib/efi
277 payload stub, application, support code. Mostly arch-neutral
278
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100279arch/x86/cpu/efi
Bin Meng525c8612018-06-12 08:36:16 -0700280 x86 support code for running as an EFI application and payload
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100281
Bin Meng65820742018-06-12 08:36:24 -0700282board/efi/efi-x86_app/efi.c
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100283 x86 board code for running as an EFI application
284
Bin Meng858429b2018-06-12 08:36:18 -0700285board/efi/efi-x86_payload
286 generic x86 EFI payload board support code
287
Heinrich Schuchardt32409242018-01-30 20:03:02 +0100288common/cmd_efi.c
289 the 'efi' command
290
291--
292Ben Stoltz, Simon Glass
293Google, Inc
294July 2015
295
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200296* [1] http://www.qemu.org
Bin Meng74efc6e2021-06-21 10:12:32 +0800297* [2] https://github.com/tianocore/tianocore.github.io/wiki/OVMF