blob: 298b94e342e4cc389df587c45e8b4d067b0feaf3 [file] [log] [blame]
Heinrich Schuchardtf3b50562018-01-30 20:03:02 +01001#
2# Copyright (C) 2015 Google, Inc
3#
4# SPDX-License-Identifier: GPL-2.0+
5#
6
7U-Boot on EFI
8=============
9This document provides information about U-Boot running on top of EFI, either
10as an application or just as a means of getting U-Boot onto a new platform.
11
12
13=========== Table of Contents ===========
14
15Motivation
16Status
17Build Instructions
18Trying it out
19Inner workings
20EFI Application
21EFI Payload
22Tables
23Interrupts
2432/64-bit
25Future work
26Where is the code?
27
28
29Motivation
30----------
31Running U-Boot on EFI is useful in several situations:
32
33- You have EFI running on a board but U-Boot does not natively support it
34fully yet. You can boot into U-Boot from EFI and use that until U-Boot is
35fully ported
36
37- You need to use an EFI implementation (e.g. UEFI) because your vendor
38requires it in order to provide support
39
40- You plan to use coreboot to boot into U-Boot but coreboot support does
41not currently exist for your platform. In the meantime you can use U-Boot
42on EFI and then move to U-Boot on coreboot when ready
43
44- You use EFI but want to experiment with a simpler alternative like U-Boot
45
46
47Status
48------
49Only x86 is supported at present. If you are using EFI on another architecture
50you may want to reconsider. However, much of the code is generic so could be
51ported.
52
53U-Boot supports running as an EFI application for 32-bit EFI only. This is
54not very useful since only a serial port is provided. You can look around at
55memory and type 'help' but that is about it.
56
57More usefully, U-Boot supports building itself as a payload for either 32-bit
58or 64-bit EFI. U-Boot is packaged up and loaded in its entirety by EFI. Once
59started, U-Boot changes to 32-bit mode (currently) and takes over the
60machine. You can use devices, boot a kernel, etc.
61
62
63Build Instructions
64------------------
65First choose a board that has EFI support and obtain an EFI implementation
66for that board. It will be either 32-bit or 64-bit. Alternatively, you can
67opt for using QEMU [1] and the OVMF [2], as detailed below.
68
69To build U-Boot as an EFI application (32-bit EFI required), enable CONFIG_EFI
70and CONFIG_EFI_APP. The efi-x86 config (efi-x86_defconfig) is set up for this.
71Just build U-Boot as normal, e.g.
72
73 make efi-x86_defconfig
74 make
75
76To build U-Boot as an EFI payload (32-bit or 64-bit EFI can be used), adjust an
77existing config (like qemu-x86_defconfig) to enable CONFIG_EFI, CONFIG_EFI_STUB
78and either CONFIG_EFI_STUB_32BIT or CONFIG_EFI_STUB_64BIT. All of these are
79boolean Kconfig options. Then build U-Boot as normal, e.g.
80
81 make qemu-x86_defconfig
82 make
83
84You will end up with one of these files depending on what you build for:
85
86 u-boot-app.efi - U-Boot EFI application
87 u-boot-payload.efi - U-Boot EFI payload application
88
89
90Trying it out
91-------------
92QEMU is an emulator and it can emulate an x86 machine. Please make sure your
93QEMU version is 2.3.0 or above to test this. You can run the payload with
94something like this:
95
96 mkdir /tmp/efi
97 cp /path/to/u-boot*.efi /tmp/efi
98 qemu-system-x86_64 -bios bios.bin -hda fat:/tmp/efi/
99
100Add -nographic if you want to use the terminal for output. Once it starts
101type 'fs0:u-boot-payload.efi' to run the payload or 'fs0:u-boot-app.efi' to
102run the application. 'bios.bin' is the EFI 'BIOS'. Check [2] to obtain a
103prebuilt EFI BIOS for QEMU or you can build one from source as well.
104
105To try it on real hardware, put u-boot-app.efi on a suitable boot medium,
106such as a USB stick. Then you can type something like this to start it:
107
108 fs0:u-boot-payload.efi
109
110(or fs0:u-boot-app.efi for the application)
111
112This will start the payload, copy U-Boot into RAM and start U-Boot. Note
113that EFI does not support booting a 64-bit application from a 32-bit
114EFI (or vice versa). Also it will often fail to print an error message if
115you get this wrong.
116
117
118Inner workings
119==============
120Here follow a few implementation notes for those who want to fiddle with
121this and perhaps contribute patches.
122
123The application and payload approaches sound similar but are in fact
124implemented completely differently.
125
126EFI Application
127---------------
128For the application the whole of U-Boot is built as a shared library. The
129efi_main() function is in lib/efi/efi_app.c. It sets up some basic EFI
130functions with efi_init(), sets up U-Boot global_data, allocates memory for
131U-Boot's malloc(), etc. and enters the normal init sequence (board_init_f()
132and board_init_r()).
133
134Since U-Boot limits its memory access to the allocated regions very little
135special code is needed. The CONFIG_EFI_APP option controls a few things
136that need to change so 'git grep CONFIG_EFI_APP' may be instructive.
137The CONFIG_EFI option controls more general EFI adjustments.
138
139The only available driver is the serial driver. This calls back into EFI
140'boot services' to send and receive characters. Although it is implemented
141as a serial driver the console device is not necessarilly serial. If you
142boot EFI with video output then the 'serial' device will operate on your
143target devices's display instead and the device's USB keyboard will also
144work if connected. If you have both serial and video output, then both
145consoles will be active. Even though U-Boot does the same thing normally,
146These are features of EFI, not U-Boot.
147
148Very little code is involved in implementing the EFI application feature.
149U-Boot is highly portable. Most of the difficulty is in modifying the
150Makefile settings to pass the right build flags. In particular there is very
151little x86-specific code involved - you can find most of it in
152arch/x86/cpu. Porting to ARM (which can also use EFI if you are brave
153enough) should be straightforward.
154
155Use the 'reset' command to get back to EFI.
156
157EFI Payload
158-----------
159The payload approach is a different kettle of fish. It works by building
160U-Boot exactly as normal for your target board, then adding the entire
161image (including device tree) into a small EFI stub application responsible
162for booting it. The stub application is built as a normal EFI application
163except that it has a lot of data attached to it.
164
165The stub application is implemented in lib/efi/efi_stub.c. The efi_main()
166function is called by EFI. It is responsible for copying U-Boot from its
167original location into memory, disabling EFI boot services and starting
168U-Boot. U-Boot then starts as normal, relocates, starts all drivers, etc.
169
170The stub application is architecture-dependent. At present it has some
171x86-specific code and a comment at the top of efi_stub.c describes this.
172
173While the stub application does allocate some memory from EFI this is not
174used by U-Boot (the payload). In fact when U-Boot starts it has all of the
175memory available to it and can operate as it pleases (but see the next
176section).
177
178Tables
179------
180The payload can pass information to U-Boot in the form of EFI tables. At
181present this feature is used to pass the EFI memory map, an inordinately
182large list of memory regions. You can use the 'efi mem all' command to
183display this list. U-Boot uses the list to work out where to relocate
184itself.
185
186Although U-Boot can use any memory it likes, EFI marks some memory as used
187by 'run-time services', code that hangs around while U-Boot is running and
188is even present when Linux is running. This is common on x86 and provides
189a way for Linux to call back into the firmware to control things like CPU
190fan speed. U-Boot uses only 'conventional' memory, in EFI terminology. It
191will relocate itself to the top of the largest block of memory it can find
192below 4GB.
193
194Interrupts
195----------
196U-Boot drivers typically don't use interrupts. Since EFI enables interrupts
197it is possible that an interrupt will fire that U-Boot cannot handle. This
198seems to cause problems. For this reason the U-Boot payload runs with
199interrupts disabled at present.
200
20132/64-bit
202---------
203While the EFI application can in principle be built as either 32- or 64-bit,
204only 32-bit is currently supported. This means that the application can only
205be used with 32-bit EFI.
206
207The payload stub can be build as either 32- or 64-bits. Only a small amount
208of code is built this way (see the extra- line in lib/efi/Makefile).
209Everything else is built as a normal U-Boot, so is always 32-bit on x86 at
210present.
211
212Future work
213-----------
214This work could be extended in a number of ways:
215
216- Add a generic x86 EFI payload configuration. At present you need to modify
217an existing one, but mostly the low-level x86 code is disabled when booting
218on EFI anyway, so a generic 'EFI' board could be created with a suitable set
219of drivers enabled.
220
221- Add ARM support
222
223- Add 64-bit application support
224
225- Figure out how to solve the interrupt problem
226
227- Add more drivers to the application side (e.g. video, block devices, USB,
228environment access). This would mostly be an academic exercise as a strong
229use case is not readily apparent, but it might be fun.
230
231- Avoid turning off boot services in the stub. Instead allow U-Boot to make
232use of boot services in case it wants to. It is unclear what it might want
233though.
234
235Where is the code?
236------------------
237lib/efi
238 payload stub, application, support code. Mostly arch-neutral
239
240arch/x86/lib/efi
241 helper functions for the fake DRAM init, etc. These can be used by
242 any board that runs as a payload.
243
244arch/x86/cpu/efi
245 x86 support code for running as an EFI application
246
247board/efi/efi-x86/efi.c
248 x86 board code for running as an EFI application
249
250common/cmd_efi.c
251 the 'efi' command
252
253--
254Ben Stoltz, Simon Glass
255Google, Inc
256July 2015
257
258[1] http://www.qemu.org
259[2] http://www.tianocore.org/ovmf/