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