uefi.rst 17 KB

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