README.efi 12 KB

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