README.u-boot_on_efi 9.1 KB

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