README.efi 9.1 KB

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