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