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