uefi.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. .. Copyright (c) 2018 Heinrich Schuchardt
  3. UEFI on U-Boot
  4. ==============
  5. The Unified Extensible Firmware Interface Specification (UEFI) [1] has become
  6. the default for booting on AArch64 and x86 systems. It provides a stable API for
  7. the interaction of drivers and applications with the firmware. The API comprises
  8. access to block storage, network, and console to name a few. The Linux kernel
  9. and boot loaders like GRUB or the FreeBSD loader can be executed.
  10. Development target
  11. ------------------
  12. The implementation of UEFI in U-Boot strives to reach the requirements described
  13. in the "Embedded Base Boot Requirements (EBBR) Specification - Release v1.0"
  14. [2]. The "Server Base Boot Requirements System Software on ARM Platforms" [3]
  15. describes a superset of the EBBR specification and may be used as further
  16. reference.
  17. A full blown UEFI implementation would contradict the U-Boot design principle
  18. "keep it small".
  19. Building U-Boot for UEFI
  20. ------------------------
  21. The UEFI standard supports only little-endian systems. The UEFI support can be
  22. activated for ARM and x86 by specifying::
  23. CONFIG_CMD_BOOTEFI=y
  24. CONFIG_EFI_LOADER=y
  25. in the .config file.
  26. Support for attaching virtual block devices, e.g. iSCSI drives connected by the
  27. loaded UEFI application [4], requires::
  28. CONFIG_BLK=y
  29. CONFIG_PARTITIONS=y
  30. Executing a UEFI binary
  31. ~~~~~~~~~~~~~~~~~~~~~~~
  32. The bootefi command is used to start UEFI applications or to install UEFI
  33. drivers. It takes two parameters::
  34. bootefi <image address> [fdt address]
  35. * image address - the memory address of the UEFI binary
  36. * fdt address - the memory address of the flattened device tree
  37. Below you find the output of an example session starting GRUB::
  38. => load mmc 0:2 ${fdt_addr_r} boot/dtb
  39. 29830 bytes read in 14 ms (2 MiB/s)
  40. => load mmc 0:1 ${kernel_addr_r} efi/debian/grubaa64.efi
  41. reading efi/debian/grubaa64.efi
  42. 120832 bytes read in 7 ms (16.5 MiB/s)
  43. => bootefi ${kernel_addr_r} ${fdt_addr_r}
  44. The environment variable 'bootargs' is passed as load options in the UEFI system
  45. table. The Linux kernel EFI stub uses the load options as command line
  46. arguments.
  47. Launching a UEFI binary from a FIT image
  48. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  49. A signed FIT image can be used to securely boot a UEFI image via the
  50. bootm command. This feature is available if U-Boot is configured with::
  51. CONFIG_BOOTM_EFI=y
  52. A sample configuration is provided as file doc/uImage.FIT/uefi.its.
  53. Below you find the output of an example session starting GRUB::
  54. => load mmc 0:1 ${kernel_addr_r} image.fit
  55. 4620426 bytes read in 83 ms (53.1 MiB/s)
  56. => bootm ${kernel_addr_r}#config-grub-nofdt
  57. ## Loading kernel from FIT Image at 40400000 ...
  58. Using 'config-grub-nofdt' configuration
  59. Verifying Hash Integrity ... sha256,rsa2048:dev+ OK
  60. Trying 'efi-grub' kernel subimage
  61. Description: GRUB EFI Firmware
  62. Created: 2019-11-20 8:18:16 UTC
  63. Type: Kernel Image (no loading done)
  64. Compression: uncompressed
  65. Data Start: 0x404000d0
  66. Data Size: 450560 Bytes = 440 KiB
  67. Hash algo: sha256
  68. Hash value: 4dbee00021112df618f58b3f7cf5e1595533d543094064b9ce991e8b054a9eec
  69. Verifying Hash Integrity ... sha256+ OK
  70. XIP Kernel Image (no loading done)
  71. ## Transferring control to EFI (at address 404000d0) ...
  72. Welcome to GRUB!
  73. See doc/uImage.FIT/howto.txt for an introduction to FIT images.
  74. Configuring UEFI secure boot
  75. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. The UEFI specification[1] defines a secure way of executing UEFI images
  77. by verifying a signature (or message digest) of image with certificates.
  78. This feature on U-Boot is enabled with::
  79. CONFIG_UEFI_SECURE_BOOT=y
  80. To make the boot sequence safe, you need to establish a chain of trust;
  81. In UEFI secure boot the chain trust is defined by the following UEFI variables
  82. * PK - Platform Key
  83. * KEK - Key Exchange Keys
  84. * db - white list database
  85. * dbx - black list database
  86. An in depth description of UEFI secure boot is beyond the scope of this
  87. document. Please, refer to the UEFI specification and available online
  88. documentation. Here is a simple example that you can follow for your initial
  89. attempt (Please note that the actual steps will depend on your system and
  90. environment.):
  91. Install the required tools on your host
  92. * openssl
  93. * efitools
  94. * sbsigntool
  95. Create signing keys and the key database on your host:
  96. The platform key
  97. .. code-block:: bash
  98. openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ \
  99. -keyout PK.key -out PK.crt -nodes -days 365
  100. cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
  101. PK.crt PK.esl;
  102. sign-efi-sig-list -c PK.crt -k PK.key PK PK.esl PK.auth
  103. The key exchange keys
  104. .. code-block:: bash
  105. openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ \
  106. -keyout KEK.key -out KEK.crt -nodes -days 365
  107. cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
  108. KEK.crt KEK.esl
  109. sign-efi-sig-list -c PK.crt -k PK.key KEK KEK.esl KEK.auth
  110. The whitelist database
  111. .. code-block:: bash
  112. $ openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db/ \
  113. -keyout db.key -out db.crt -nodes -days 365
  114. $ cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
  115. db.crt db.esl
  116. $ sign-efi-sig-list -c KEK.crt -k KEK.key db db.esl db.auth
  117. Copy the \*.auth files to media, say mmc, that is accessible from U-Boot.
  118. Sign an image with one of the keys in "db" on your host
  119. .. code-block:: bash
  120. sbsign --key db.key --cert db.crt helloworld.efi
  121. Now in U-Boot install the keys on your board::
  122. fatload mmc 0:1 <tmpaddr> PK.auth
  123. setenv -e -nv -bs -rt -at -i <tmpaddr>,$filesize PK
  124. fatload mmc 0:1 <tmpaddr> KEK.auth
  125. setenv -e -nv -bs -rt -at -i <tmpaddr>,$filesize KEK
  126. fatload mmc 0:1 <tmpaddr> db.auth
  127. setenv -e -nv -bs -rt -at -i <tmpaddr>,$filesize db
  128. Set up boot parameters on your board::
  129. efidebug boot add 1 HELLO mmc 0:1 /helloworld.efi.signed ""
  130. Now your board can run the signed image via the boot manager (see below).
  131. You can also try this sequence by running Pytest, test_efi_secboot,
  132. on the sandbox
  133. .. code-block:: bash
  134. cd <U-Boot source directory>
  135. pytest.py test/py/tests/test_efi_secboot/test_signed.py --bd sandbox
  136. Executing the boot manager
  137. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  138. The UEFI specification foresees to define boot entries and boot sequence via UEFI
  139. variables. Booting according to these variables is possible via::
  140. bootefi bootmgr [fdt address]
  141. As of U-Boot v2018.03 UEFI variables are not persisted and cannot be set at
  142. runtime.
  143. Executing the built in hello world application
  144. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  145. A hello world UEFI application can be built with::
  146. CONFIG_CMD_BOOTEFI_HELLO_COMPILE=y
  147. It can be embedded into the U-Boot binary with::
  148. CONFIG_CMD_BOOTEFI_HELLO=y
  149. The bootefi command is used to start the embedded hello world application::
  150. bootefi hello [fdt address]
  151. Below you find the output of an example session::
  152. => bootefi hello ${fdtcontroladdr}
  153. ## Starting EFI application at 01000000 ...
  154. WARNING: using memory device/image path, this may confuse some payloads!
  155. Hello, world!
  156. Running on UEFI 2.7
  157. Have SMBIOS table
  158. Have device tree
  159. Load options: root=/dev/sdb3 init=/sbin/init rootwait ro
  160. ## Application terminated, r = 0
  161. The environment variable fdtcontroladdr points to U-Boot's internal device tree
  162. (if available).
  163. Executing the built-in self-test
  164. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  165. An UEFI self-test suite can be embedded in U-Boot by building with::
  166. CONFIG_CMD_BOOTEFI_SELFTEST=y
  167. For testing the UEFI implementation the bootefi command can be used to start the
  168. self-test::
  169. bootefi selftest [fdt address]
  170. The environment variable 'efi_selftest' can be used to select a single test. If
  171. it is not provided all tests are executed except those marked as 'on request'.
  172. If the environment variable is set to 'list' a list of all tests is shown.
  173. Below you can find the output of an example session::
  174. => setenv efi_selftest simple network protocol
  175. => bootefi selftest
  176. Testing EFI API implementation
  177. Selected test: 'simple network protocol'
  178. Setting up 'simple network protocol'
  179. Setting up 'simple network protocol' succeeded
  180. Executing 'simple network protocol'
  181. DHCP Discover
  182. DHCP reply received from 192.168.76.2 (52:55:c0:a8:4c:02)
  183. as broadcast message.
  184. Executing 'simple network protocol' succeeded
  185. Tearing down 'simple network protocol'
  186. Tearing down 'simple network protocol' succeeded
  187. Boot services terminated
  188. Summary: 0 failures
  189. Preparing for reset. Press any key.
  190. The UEFI life cycle
  191. -------------------
  192. After the U-Boot platform has been initialized the UEFI API provides two kinds
  193. of services:
  194. * boot services
  195. * runtime services
  196. The API can be extended by loading UEFI drivers which come in two variants:
  197. * boot drivers
  198. * runtime drivers
  199. UEFI drivers are installed with U-Boot's bootefi command. With the same command
  200. UEFI applications can be executed.
  201. Loaded images of UEFI drivers stay in memory after returning to U-Boot while
  202. loaded images of applications are removed from memory.
  203. An UEFI application (e.g. an operating system) that wants to take full control
  204. of the system calls ExitBootServices. After a UEFI application calls
  205. ExitBootServices
  206. * boot services are not available anymore
  207. * timer events are stopped
  208. * the memory used by U-Boot except for runtime services is released
  209. * the memory used by boot time drivers is released
  210. So this is a point of no return. Afterwards the UEFI application can only return
  211. to U-Boot by rebooting.
  212. The UEFI object model
  213. ---------------------
  214. UEFI offers a flexible and expandable object model. The objects in the UEFI API
  215. are devices, drivers, and loaded images. These objects are referenced by
  216. handles.
  217. The interfaces implemented by the objects are referred to as protocols. These
  218. are identified by GUIDs. They can be installed and uninstalled by calling the
  219. appropriate boot services.
  220. Handles are created by the InstallProtocolInterface or the
  221. InstallMultipleProtocolinterfaces service if NULL is passed as handle.
  222. Handles are deleted when the last protocol has been removed with the
  223. UninstallProtocolInterface or the UninstallMultipleProtocolInterfaces service.
  224. Devices offer the EFI_DEVICE_PATH_PROTOCOL. A device path is the concatenation
  225. of device nodes. By their device paths all devices of a system are arranged in a
  226. tree.
  227. Drivers offer the EFI_DRIVER_BINDING_PROTOCOL. This protocol is used to connect
  228. a driver to devices (which are referenced as controllers in this context).
  229. Loaded images offer the EFI_LOADED_IMAGE_PROTOCOL. This protocol provides meta
  230. information about the image and a pointer to the unload callback function.
  231. The UEFI events
  232. ---------------
  233. In the UEFI terminology an event is a data object referencing a notification
  234. function which is queued for calling when the event is signaled. The following
  235. types of events exist:
  236. * periodic and single shot timer events
  237. * exit boot services events, triggered by calling the ExitBootServices() service
  238. * virtual address change events
  239. * memory map change events
  240. * read to boot events
  241. * reset system events
  242. * system table events
  243. * events that are only triggered programmatically
  244. Events can be created with the CreateEvent service and deleted with CloseEvent
  245. service.
  246. Events can be assigned to an event group. If any of the events in a group is
  247. signaled, all other events in the group are also set to the signaled state.
  248. The UEFI driver model
  249. ---------------------
  250. A driver is specific for a single protocol installed on a device. To install a
  251. driver on a device the ConnectController service is called. In this context
  252. controller refers to the device for which the driver is installed.
  253. The relevant drivers are identified using the EFI_DRIVER_BINDING_PROTOCOL. This
  254. protocol has has three functions:
  255. * supported - determines if the driver is compatible with the device
  256. * start - installs the driver by opening the relevant protocol with
  257. attribute EFI_OPEN_PROTOCOL_BY_DRIVER
  258. * stop - uninstalls the driver
  259. The driver may create child controllers (child devices). E.g. a driver for block
  260. IO devices will create the device handles for the partitions. The child
  261. controllers will open the supported protocol with the attribute
  262. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  263. A driver can be detached from a device using the DisconnectController service.
  264. U-Boot devices mapped as UEFI devices
  265. -------------------------------------
  266. Some of the U-Boot devices are mapped as UEFI devices
  267. * block IO devices
  268. * console
  269. * graphical output
  270. * network adapter
  271. As of U-Boot 2018.03 the logic for doing this is hard coded.
  272. The development target is to integrate the setup of these UEFI devices with the
  273. U-Boot driver model [5]. So when a U-Boot device is discovered a handle should
  274. be created and the device path protocol and the relevant IO protocol should be
  275. installed. The UEFI driver then would be attached by calling ConnectController.
  276. When a U-Boot device is removed DisconnectController should be called.
  277. UEFI devices mapped as U-Boot devices
  278. -------------------------------------
  279. UEFI drivers binaries and applications may create new (virtual) devices, install
  280. a protocol and call the ConnectController service. Now the matching UEFI driver
  281. is determined by iterating over the implementations of the
  282. EFI_DRIVER_BINDING_PROTOCOL.
  283. It is the task of the UEFI driver to create a corresponding U-Boot device and to
  284. proxy calls for this U-Boot device to the controller.
  285. In U-Boot 2018.03 this has only been implemented for block IO devices.
  286. UEFI uclass
  287. ~~~~~~~~~~~
  288. An UEFI uclass driver (lib/efi_driver/efi_uclass.c) has been created that
  289. takes care of initializing the UEFI drivers and providing the
  290. EFI_DRIVER_BINDING_PROTOCOL implementation for the UEFI drivers.
  291. A linker created list is used to keep track of the UEFI drivers. To create an
  292. entry in the list the UEFI driver uses the U_BOOT_DRIVER macro specifying
  293. UCLASS_EFI as the ID of its uclass, e.g::
  294. /* Identify as UEFI driver */
  295. U_BOOT_DRIVER(efi_block) = {
  296. .name = "EFI block driver",
  297. .id = UCLASS_EFI,
  298. .ops = &driver_ops,
  299. };
  300. The available operations are defined via the structure struct efi_driver_ops::
  301. struct efi_driver_ops {
  302. const efi_guid_t *protocol;
  303. const efi_guid_t *child_protocol;
  304. int (*bind)(efi_handle_t handle, void *interface);
  305. };
  306. When the supported() function of the EFI_DRIVER_BINDING_PROTOCOL is called the
  307. uclass checks if the protocol GUID matches the protocol GUID of the UEFI driver.
  308. In the start() function the bind() function of the UEFI driver is called after
  309. checking the GUID.
  310. The stop() function of the EFI_DRIVER_BINDING_PROTOCOL disconnects the child
  311. controllers created by the UEFI driver and the UEFI driver. (In U-Boot v2013.03
  312. this is not yet completely implemented.)
  313. UEFI block IO driver
  314. ~~~~~~~~~~~~~~~~~~~~
  315. The UEFI block IO driver supports devices exposing the EFI_BLOCK_IO_PROTOCOL.
  316. When connected it creates a new U-Boot block IO device with interface type
  317. IF_TYPE_EFI, adds child controllers mapping the partitions, and installs the
  318. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on these. This can be used together with the
  319. software iPXE to boot from iSCSI network drives [4].
  320. This driver is only available if U-Boot is configured with::
  321. CONFIG_BLK=y
  322. CONFIG_PARTITIONS=y
  323. Miscellaneous
  324. -------------
  325. Load file 2 protocol
  326. ~~~~~~~~~~~~~~~~~~~~
  327. The load file 2 protocol can be used by the Linux kernel to load the initial
  328. RAM disk. U-Boot can be configured to provide an implementation with::
  329. EFI_LOAD_FILE2_INITRD=y
  330. EFI_INITRD_FILESPEC=interface dev:part path_to_initrd
  331. Links
  332. -----
  333. * [1] http://uefi.org/specifications - UEFI specifications
  334. * [2] https://github.com/ARM-software/ebbr/releases/download/v1.0/ebbr-v1.0.pdf -
  335. Embedded Base Boot Requirements (EBBR) Specification - Release v1.0
  336. * [3] https://developer.arm.com/docs/den0044/latest/server-base-boot-requirements-system-software-on-arm-platforms-version-11 -
  337. Server Base Boot Requirements System Software on ARM Platforms - Version 1.1
  338. * [4] :doc:`iscsi`
  339. * [5] :doc:`../driver-model/index`