README.uefi 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <!--
  2. SPDX-License-Identifier: GPL-2.0+
  3. Copyright (c) 2018 Heinrich Schuchardt
  4. -->
  5. # UEFI on U-Boot
  6. The Unified Extensible Firmware Interface Specification (UEFI) [1] has become
  7. the default for booting on AArch64 and x86 systems. It provides a stable API for
  8. the interaction of drivers and applications with the firmware. The API comprises
  9. access to block storage, network, and console to name a few. The Linux kernel
  10. and boot loaders like GRUB or the FreeBSD loader can be executed.
  11. ## Building for UEFI
  12. The UEFI standard supports only little endian systems. The UEFI support can be
  13. activated for ARM and x86 by specifying
  14. CONFIG_CMD_BOOTEFI=y
  15. CONFIG_EFI_LOADER=y
  16. in the .config file.
  17. Support for attaching virtual block devices, e.g. iSCSI drives connected by the
  18. loaded UEFI application [3], requires
  19. CONFIG_BLK=y
  20. CONFIG_PARTITIONS=y
  21. ### Executing a UEFI binary
  22. The bootefi command is used to start UEFI applications or to install UEFI
  23. drivers. It takes two parameters
  24. bootefi <image address> [fdt address]
  25. * image address - the memory address of the UEFI binary
  26. * fdt address - the memory address of the flattened device tree
  27. Below you find the output of an example session starting GRUB.
  28. => load mmc 0:2 ${fdt_addr_r} boot/dtb
  29. 29830 bytes read in 14 ms (2 MiB/s)
  30. => load mmc 0:1 ${kernel_addr_r} efi/debian/grubaa64.efi
  31. reading efi/debian/grubaa64.efi
  32. 120832 bytes read in 7 ms (16.5 MiB/s)
  33. => bootefi ${kernel_addr_r} ${fdt_addr_r}
  34. The environment variable 'bootargs' is passed as load options in the UEFI system
  35. table. The Linux kernel EFI stub uses the load options as command line
  36. arguments.
  37. ### Executing the boot manager
  38. The UEFI specfication foresees to define boot entries and boot sequence via UEFI
  39. variables. Booting according to these variables is possible via
  40. bootefi bootmgr [fdt address]
  41. As of U-Boot v2018.03 UEFI variables are not persisted and cannot be set at
  42. runtime.
  43. ### Executing the built in hello world application
  44. A hello world UEFI application can be built with
  45. CONFIG_CMD_BOOTEFI_HELLO_COMPILE=y
  46. It can be embedded into the U-Boot binary with
  47. CONFIG_CMD_BOOTEFI_HELLO=y
  48. The bootefi command is used to start the embedded hello world application.
  49. bootefi hello [fdt address]
  50. Below you find the output of an example session.
  51. => bootefi hello ${fdtcontroladdr}
  52. ## Starting EFI application at 01000000 ...
  53. WARNING: using memory device/image path, this may confuse some payloads!
  54. Hello, world!
  55. Running on UEFI 2.7
  56. Have SMBIOS table
  57. Have device tree
  58. Load options: root=/dev/sdb3 init=/sbin/init rootwait ro
  59. ## Application terminated, r = 0
  60. The environment variable fdtcontroladdr points to U-Boot's internal device tree
  61. (if available).
  62. ### Executing the built-in selftest
  63. An UEFI selftest suite can be embedded in U-Boot by building with
  64. CONFIG_CMD_BOOTEFI_SELFTEST=y
  65. For testing the UEFI implementation the bootefi command can be used to start the
  66. selftest.
  67. bootefi selftest [fdt address]
  68. The environment variable 'efi_selftest' can be used to select a single test. If
  69. it is not provided all tests are executed except those marked as 'on request'.
  70. If the environment variable is set to 'list' a list of all tests is shown.
  71. Below you can find the output of an example session.
  72. => setenv efi_selftest simple network protocol
  73. => bootefi selftest
  74. Testing EFI API implementation
  75. Selected test: 'simple network protocol'
  76. Setting up 'simple network protocol'
  77. Setting up 'simple network protocol' succeeded
  78. Executing 'simple network protocol'
  79. DHCP Discover
  80. DHCP reply received from 192.168.76.2 (52:55:c0:a8:4c:02)
  81. as broadcast message.
  82. Executing 'simple network protocol' succeeded
  83. Tearing down 'simple network protocol'
  84. Tearing down 'simple network protocol' succeeded
  85. Boot services terminated
  86. Summary: 0 failures
  87. Preparing for reset. Press any key.
  88. ## The UEFI life cycle
  89. After the U-Boot platform has been initialized the UEFI API provides two kinds
  90. of services
  91. * boot services and
  92. * runtime services.
  93. The API can be extended by loading UEFI drivers which come in two variants
  94. * boot drivers and
  95. * runtime drivers.
  96. UEFI drivers are installed with U-Boot's bootefi command. With the same command
  97. UEFI applications can be executed.
  98. Loaded images of UEFI drivers stay in memory after returning to U-Boot while
  99. loaded images of applications are removed from memory.
  100. An UEFI application (e.g. an operating system) that wants to take full control
  101. of the system calls ExitBootServices. After a UEFI application calls
  102. ExitBootServices
  103. * boot services are not available anymore
  104. * timer events are stopped
  105. * the memory used by U-Boot except for runtime services is released
  106. * the memory used by boot time drivers is released
  107. So this is a point of no return. Afterwards the UEFI application can only return
  108. to U-Boot by rebooting.
  109. ## The UEFI object model
  110. UEFI offers a flexible and expandable object model. The objects in the UEFI API
  111. are devices, drivers, and loaded images. These objects are referenced by
  112. handles.
  113. The interfaces implemented by the objects are referred to as protocols. These
  114. are identified by GUIDs. They can be installed and uninstalled by calling the
  115. appropriate boot services.
  116. Handles are created by the InstallProtocolInterface or the
  117. InstallMultipleProtocolinterfaces service if NULL is passed as handle.
  118. Handles are deleted when the last protocol has been removed with the
  119. UninstallProtocolInterface or the UninstallMultipleProtocolInterfaces service.
  120. Devices offer the EFI_DEVICE_PATH_PROTOCOL. A device path is the concatenation
  121. of device nodes. By their device paths all devices of a system are arranged in a
  122. tree.
  123. Drivers offer the EFI_DRIVER_BINDING_PROTOCOL. This protocol is used to connect
  124. a driver to devices (which are referenced as controllers in this context).
  125. Loaded images offer the EFI_LOADED_IMAGE_PROTOCOL. This protocol provides meta
  126. information about the image and a pointer to the unload callback function.
  127. ## The UEFI events
  128. In the UEFI terminology an event is a data object referencing a notification
  129. function which is queued for calling when the event is signaled. The following
  130. types of events exist:
  131. * periodic and single shot timer events
  132. * exit boot services events, triggered by calling the ExitBootServices() service
  133. * virtual address change events
  134. * memory map change events
  135. * read to boot events
  136. * reset system events
  137. * system table events
  138. * events that are only triggered programmatically
  139. Events can be created with the CreateEvent service and deleted with CloseEvent
  140. service.
  141. Events can be assigned to an event group. If any of the events in a group is
  142. signaled, all other events in the group are also set to the signaled state.
  143. ## The UEFI driver model
  144. A driver is specific for a single protocol installed on a device. To install a
  145. driver on a device the ConnectController service is called. In this context
  146. controller refers to the device for which the driver is installed.
  147. The relevant drivers are identified using the EFI_DRIVER_BINDING_PROTOCOL. This
  148. protocol has has three functions:
  149. * supported - determines if the driver is compatible with the device
  150. * start - installs the driver by opening the relevant protocol with
  151. attribute EFI_OPEN_PROTOCOL_BY_DRIVER
  152. * stop - uninstalls the driver
  153. The driver may create child controllers (child devices). E.g. a driver for block
  154. IO devices will create the device handles for the partitions. The child
  155. controllers will open the supported protocol with the attribute
  156. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  157. A driver can be detached from a device using the DisconnectController service.
  158. ## U-Boot devices mapped as UEFI devices
  159. Some of the U-Boot devices are mapped as UEFI devices
  160. * block IO devices
  161. * console
  162. * graphical output
  163. * network adapter
  164. As of U-Boot 2018.03 the logic for doing this is hard coded.
  165. The development target is to integrate the setup of these UEFI devices with the
  166. U-Boot driver model. So when a U-Boot device is discovered a handle should be
  167. created and the device path protocol and the relevant IO protocol should be
  168. installed. The UEFI driver then would be attached by calling ConnectController.
  169. When a U-Boot device is removed DisconnectController should be called.
  170. ## UEFI devices mapped as U-Boot devices
  171. UEFI drivers binaries and applications may create new (virtual) devices, install
  172. a protocol and call the ConnectController service. Now the matching UEFI driver
  173. is determined by iterating over the implementations of the
  174. EFI_DRIVER_BINDING_PROTOCOL.
  175. It is the task of the UEFI driver to create a corresponding U-Boot device and to
  176. proxy calls for this U-Boot device to the controller.
  177. In U-Boot 2018.03 this has only been implemented for block IO devices.
  178. ### UEFI uclass
  179. An UEFI uclass driver (lib/efi_driver/efi_uclass.c) has been created that
  180. takes care of initializing the UEFI drivers and providing the
  181. EFI_DRIVER_BINDING_PROTOCOL implementation for the UEFI drivers.
  182. A linker created list is used to keep track of the UEFI drivers. To create an
  183. entry in the list the UEFI driver uses the U_BOOT_DRIVER macro specifying
  184. UCLASS_EFI as the ID of its uclass, e.g.
  185. /* Identify as UEFI driver */
  186. U_BOOT_DRIVER(efi_block) = {
  187. .name = "EFI block driver",
  188. .id = UCLASS_EFI,
  189. .ops = &driver_ops,
  190. };
  191. The available operations are defined via the structure struct efi_driver_ops.
  192. struct efi_driver_ops {
  193. const efi_guid_t *protocol;
  194. const efi_guid_t *child_protocol;
  195. int (*bind)(efi_handle_t handle, void *interface);
  196. };
  197. When the supported() function of the EFI_DRIVER_BINDING_PROTOCOL is called the
  198. uclass checks if the protocol GUID matches the protocol GUID of the UEFI driver.
  199. In the start() function the bind() function of the UEFI driver is called after
  200. checking the GUID.
  201. The stop() function of the EFI_DRIVER_BINDING_PROTOCOL disconnects the child
  202. controllers created by the UEFI driver and the UEFI driver. (In U-Boot v2013.03
  203. this is not yet completely implemented.)
  204. ### UEFI block IO driver
  205. The UEFI block IO driver supports devices exposing the EFI_BLOCK_IO_PROTOCOL.
  206. When connected it creates a new U-Boot block IO device with interface type
  207. IF_TYPE_EFI, adds child controllers mapping the partitions, and installs the
  208. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on these. This can be used together with the
  209. software iPXE to boot from iSCSI network drives [3].
  210. This driver is only available if U-Boot is configured with
  211. CONFIG_BLK=y
  212. CONFIG_PARTITIONS=y
  213. ## TODOs as of U-Boot 2018.03
  214. * unimplemented or incompletely implemented boot services
  215. * Exit - call unload function, unload applications only
  216. * ReinstallProtocolInterface
  217. * UnloadImage
  218. * unimplemented events
  219. * EVT_RUNTIME
  220. * EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
  221. * event groups
  222. * data model
  223. * manage events in a linked list
  224. * manage configuration tables in a linked list
  225. * UEFI drivers
  226. * support DisconnectController for UEFI block devices.
  227. * support for CONFIG_EFI_LOADER in the sandbox (CONFIG_SANDBOX=y)
  228. * UEFI variables
  229. * persistence
  230. * runtime support
  231. * support bootefi booting ARMv7 in non-secure mode (CONFIG_ARMV7_NONSEC=y)
  232. ## Links
  233. * [1](http://uefi.org/specifications)
  234. http://uefi.org/specifications - UEFI specifications
  235. * [2](./driver-model/README.txt) doc/driver-model/README.txt - Driver model
  236. * [3](./README.iscsi) doc/README.iscsi - iSCSI booting with U-Boot and iPXE