blob: a35fbd331c1fa0ea137f5da49cc7b32c3011c47b [file] [log] [blame]
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +02001.. SPDX-License-Identifier: GPL-2.0+
2.. Copyright (c) 2018 Heinrich Schuchardt
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +01003
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +02004UEFI on U-Boot
5==============
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +01006
7The Unified Extensible Firmware Interface Specification (UEFI) [1] has become
8the default for booting on AArch64 and x86 systems. It provides a stable API for
9the interaction of drivers and applications with the firmware. The API comprises
10access to block storage, network, and console to name a few. The Linux kernel
11and boot loaders like GRUB or the FreeBSD loader can be executed.
12
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +020013Development target
14------------------
Heinrich Schuchardt9ba712d2019-03-28 08:09:16 +010015
Heinrich Schuchardtdc6f3f42019-04-10 08:04:38 +020016The implementation of UEFI in U-Boot strives to reach the requirements described
17in the "Embedded Base Boot Requirements (EBBR) Specification - Release v1.0"
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +020018[2]. The "Server Base Boot Requirements System Software on ARM Platforms" [3]
Heinrich Schuchardtdc6f3f42019-04-10 08:04:38 +020019describes a superset of the EBBR specification and may be used as further
20reference.
Heinrich Schuchardt9ba712d2019-03-28 08:09:16 +010021
22A full blown UEFI implementation would contradict the U-Boot design principle
23"keep it small".
24
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +020025Building U-Boot for UEFI
26------------------------
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +010027
Heinrich Schuchardt4f3cb4d2018-12-30 12:54:36 +010028The UEFI standard supports only little-endian systems. The UEFI support can be
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +020029activated for ARM and x86 by specifying::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +010030
31 CONFIG_CMD_BOOTEFI=y
32 CONFIG_EFI_LOADER=y
33
34in the .config file.
35
36Support for attaching virtual block devices, e.g. iSCSI drives connected by the
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +020037loaded UEFI application [4], requires::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +010038
39 CONFIG_BLK=y
40 CONFIG_PARTITIONS=y
41
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +020042Executing a UEFI binary
43~~~~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +010044
45The bootefi command is used to start UEFI applications or to install UEFI
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +020046drivers. It takes two parameters::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +010047
48 bootefi <image address> [fdt address]
49
50* image address - the memory address of the UEFI binary
51* fdt address - the memory address of the flattened device tree
52
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +020053Below you find the output of an example session starting GRUB::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +010054
55 => load mmc 0:2 ${fdt_addr_r} boot/dtb
56 29830 bytes read in 14 ms (2 MiB/s)
57 => load mmc 0:1 ${kernel_addr_r} efi/debian/grubaa64.efi
58 reading efi/debian/grubaa64.efi
59 120832 bytes read in 7 ms (16.5 MiB/s)
60 => bootefi ${kernel_addr_r} ${fdt_addr_r}
61
62The environment variable 'bootargs' is passed as load options in the UEFI system
63table. The Linux kernel EFI stub uses the load options as command line
64arguments.
65
Cristian Ciocaltea2dbab872019-12-24 18:05:41 +020066Launching a UEFI binary from a FIT image
67~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68
69A signed FIT image can be used to securely boot a UEFI image via the
70bootm command. This feature is available if U-Boot is configured with::
71
72 CONFIG_BOOTM_EFI=y
73
74A sample configuration is provided as file doc/uImage.FIT/uefi.its.
75
76Below you find the output of an example session starting GRUB::
77
78 => load mmc 0:1 ${kernel_addr_r} image.fit
79 4620426 bytes read in 83 ms (53.1 MiB/s)
80 => bootm ${kernel_addr_r}#config-grub-nofdt
81 ## Loading kernel from FIT Image at 40400000 ...
82 Using 'config-grub-nofdt' configuration
83 Verifying Hash Integrity ... sha256,rsa2048:dev+ OK
84 Trying 'efi-grub' kernel subimage
85 Description: GRUB EFI Firmware
86 Created: 2019-11-20 8:18:16 UTC
87 Type: Kernel Image (no loading done)
88 Compression: uncompressed
89 Data Start: 0x404000d0
90 Data Size: 450560 Bytes = 440 KiB
91 Hash algo: sha256
92 Hash value: 4dbee00021112df618f58b3f7cf5e1595533d543094064b9ce991e8b054a9eec
93 Verifying Hash Integrity ... sha256+ OK
94 XIP Kernel Image (no loading done)
95 ## Transferring control to EFI (at address 404000d0) ...
96 Welcome to GRUB!
97
98See doc/uImage.FIT/howto.txt for an introduction to FIT images.
99
AKASHI Takahirob2ace872020-04-14 11:51:54 +0900100Configuring UEFI secure boot
101~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
103UEFI specification[1] defines a secure way of executing UEFI images
104by verifying a signature (or message digest) of image with certificates.
105This feature on U-Boot is enabled with::
106
107 CONFIG_UEFI_SECURE_BOOT=y
108
109To make the boot sequence safe, you need to establish a chain of trust;
110In UEFI secure boot, you can make it with the UEFI variables, "PK"
111(Platform Key), "KEK" (Key Exchange Keys), "db" (white list database)
112and "dbx" (black list database).
113
114There are many online documents that describe what UEFI secure boot is
115and how it works. Please consult some of them for details.
116
117Here is a simple example that you can follow for your initial attempt
118(Please note that the actual steps would absolutely depend on your system
119and environment.):
120
1211. Install utility commands on your host
122 * openssl
123 * efitools
124 * sbsigntool
125
1262. Create signing keys and key database files on your host
127 for PK::
128
129 $ openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ \
130 -keyout PK.key -out PK.crt -nodes -days 365
131 $ cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
132 PK.crt PK.esl;
133 $ sign-efi-sig-list -c PK.crt -k PK.key PK PK.esl PK.auth
134
135 for KEK::
136
137 $ openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ \
138 -keyout KEK.key -out KEK.crt -nodes -days 365
139 $ cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
140 KEK.crt KEK.esl
141 $ sign-efi-sig-list -c PK.crt -k PK.key KEK KEK.esl KEK.auth
142
143 for db::
144
145 $ openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db/ \
146 -keyout db.key -out db.crt -nodes -days 365
147 $ cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
148 db.crt db.esl
149 $ sign-efi-sig-list -c KEK.crt -k KEK.key db db.esl db.auth
150
151 Copy \*.auth to media, say mmc, that is accessible from U-Boot.
152
1533. Sign an image with one key in "db" on your host::
154
155 $ sbsign --key db.key --cert db.crt helloworld.efi
156
1574. Install keys on your board::
158
159 ==> fatload mmc 0:1 <tmpaddr> PK.auth
160 ==> setenv -e -nv -bs -rt -at -i <tmpaddr>,$filesize PK
161 ==> fatload mmc 0:1 <tmpaddr> KEK.auth
162 ==> setenv -e -nv -bs -rt -at -i <tmpaddr>,$filesize KEK
163 ==> fatload mmc 0:1 <tmpaddr> db.auth
164 ==> setenv -e -nv -bs -rt -at -i <tmpaddr>,$filesize db
165
1665. Set up boot parameters on your board::
167
168 ==> efidebug boot add 1 HELLO mmc 0:1 /helloworld.efi.signed ""
169
170Then your board runs that image from Boot manager (See below).
171You can also try this sequence by running Pytest, test_efi_secboot,
172on sandbox::
173
174 $ cd <U-Boot source directory>
175 $ pytest.py test/py/tests/test_efi_secboot/test_signed.py --bd sandbox
176
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200177Executing the boot manager
178~~~~~~~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100179
Heinrich Schuchardt4f3cb4d2018-12-30 12:54:36 +0100180The UEFI specification foresees to define boot entries and boot sequence via UEFI
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200181variables. Booting according to these variables is possible via::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100182
183 bootefi bootmgr [fdt address]
184
185As of U-Boot v2018.03 UEFI variables are not persisted and cannot be set at
186runtime.
187
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200188Executing the built in hello world application
189~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100190
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200191A hello world UEFI application can be built with::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100192
193 CONFIG_CMD_BOOTEFI_HELLO_COMPILE=y
194
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200195It can be embedded into the U-Boot binary with::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100196
197 CONFIG_CMD_BOOTEFI_HELLO=y
198
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200199The bootefi command is used to start the embedded hello world application::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100200
201 bootefi hello [fdt address]
202
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200203Below you find the output of an example session::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100204
205 => bootefi hello ${fdtcontroladdr}
206 ## Starting EFI application at 01000000 ...
207 WARNING: using memory device/image path, this may confuse some payloads!
208 Hello, world!
209 Running on UEFI 2.7
210 Have SMBIOS table
211 Have device tree
212 Load options: root=/dev/sdb3 init=/sbin/init rootwait ro
213 ## Application terminated, r = 0
214
215The environment variable fdtcontroladdr points to U-Boot's internal device tree
216(if available).
217
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200218Executing the built-in self-test
219~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100220
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200221An UEFI self-test suite can be embedded in U-Boot by building with::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100222
223 CONFIG_CMD_BOOTEFI_SELFTEST=y
224
225For testing the UEFI implementation the bootefi command can be used to start the
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200226self-test::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100227
228 bootefi selftest [fdt address]
229
230The environment variable 'efi_selftest' can be used to select a single test. If
231it is not provided all tests are executed except those marked as 'on request'.
232If the environment variable is set to 'list' a list of all tests is shown.
233
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200234Below you can find the output of an example session::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100235
236 => setenv efi_selftest simple network protocol
237 => bootefi selftest
238 Testing EFI API implementation
239 Selected test: 'simple network protocol'
240 Setting up 'simple network protocol'
241 Setting up 'simple network protocol' succeeded
242 Executing 'simple network protocol'
243 DHCP Discover
244 DHCP reply received from 192.168.76.2 (52:55:c0:a8:4c:02)
245 as broadcast message.
246 Executing 'simple network protocol' succeeded
247 Tearing down 'simple network protocol'
248 Tearing down 'simple network protocol' succeeded
249 Boot services terminated
250 Summary: 0 failures
251 Preparing for reset. Press any key.
252
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200253The UEFI life cycle
254-------------------
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100255
256After the U-Boot platform has been initialized the UEFI API provides two kinds
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200257of services:
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100258
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200259* boot services
260* runtime services
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100261
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200262The API can be extended by loading UEFI drivers which come in two variants:
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100263
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200264* boot drivers
265* runtime drivers
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100266
267UEFI drivers are installed with U-Boot's bootefi command. With the same command
268UEFI applications can be executed.
269
270Loaded images of UEFI drivers stay in memory after returning to U-Boot while
271loaded images of applications are removed from memory.
272
273An UEFI application (e.g. an operating system) that wants to take full control
274of the system calls ExitBootServices. After a UEFI application calls
275ExitBootServices
276
277* boot services are not available anymore
278* timer events are stopped
279* the memory used by U-Boot except for runtime services is released
280* the memory used by boot time drivers is released
281
282So this is a point of no return. Afterwards the UEFI application can only return
283to U-Boot by rebooting.
284
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200285The UEFI object model
286---------------------
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100287
288UEFI offers a flexible and expandable object model. The objects in the UEFI API
289are devices, drivers, and loaded images. These objects are referenced by
290handles.
291
292The interfaces implemented by the objects are referred to as protocols. These
293are identified by GUIDs. They can be installed and uninstalled by calling the
294appropriate boot services.
295
296Handles are created by the InstallProtocolInterface or the
297InstallMultipleProtocolinterfaces service if NULL is passed as handle.
298
299Handles are deleted when the last protocol has been removed with the
300UninstallProtocolInterface or the UninstallMultipleProtocolInterfaces service.
301
302Devices offer the EFI_DEVICE_PATH_PROTOCOL. A device path is the concatenation
303of device nodes. By their device paths all devices of a system are arranged in a
304tree.
305
306Drivers offer the EFI_DRIVER_BINDING_PROTOCOL. This protocol is used to connect
307a driver to devices (which are referenced as controllers in this context).
308
309Loaded images offer the EFI_LOADED_IMAGE_PROTOCOL. This protocol provides meta
310information about the image and a pointer to the unload callback function.
311
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200312The UEFI events
313---------------
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100314
315In the UEFI terminology an event is a data object referencing a notification
316function which is queued for calling when the event is signaled. The following
317types of events exist:
318
319* periodic and single shot timer events
320* exit boot services events, triggered by calling the ExitBootServices() service
321* virtual address change events
322* memory map change events
323* read to boot events
324* reset system events
325* system table events
326* events that are only triggered programmatically
327
328Events can be created with the CreateEvent service and deleted with CloseEvent
329service.
330
331Events can be assigned to an event group. If any of the events in a group is
332signaled, all other events in the group are also set to the signaled state.
333
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200334The UEFI driver model
335---------------------
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100336
337A driver is specific for a single protocol installed on a device. To install a
338driver on a device the ConnectController service is called. In this context
339controller refers to the device for which the driver is installed.
340
341The relevant drivers are identified using the EFI_DRIVER_BINDING_PROTOCOL. This
342protocol has has three functions:
343
344* supported - determines if the driver is compatible with the device
345* start - installs the driver by opening the relevant protocol with
346 attribute EFI_OPEN_PROTOCOL_BY_DRIVER
347* stop - uninstalls the driver
348
349The driver may create child controllers (child devices). E.g. a driver for block
350IO devices will create the device handles for the partitions. The child
351controllers will open the supported protocol with the attribute
352EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
353
354A driver can be detached from a device using the DisconnectController service.
355
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200356U-Boot devices mapped as UEFI devices
357-------------------------------------
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100358
359Some of the U-Boot devices are mapped as UEFI devices
360
361* block IO devices
362* console
363* graphical output
364* network adapter
365
366As of U-Boot 2018.03 the logic for doing this is hard coded.
367
368The development target is to integrate the setup of these UEFI devices with the
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200369U-Boot driver model [5]. So when a U-Boot device is discovered a handle should
370be created and the device path protocol and the relevant IO protocol should be
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100371installed. The UEFI driver then would be attached by calling ConnectController.
372When a U-Boot device is removed DisconnectController should be called.
373
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200374UEFI devices mapped as U-Boot devices
375-------------------------------------
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100376
377UEFI drivers binaries and applications may create new (virtual) devices, install
378a protocol and call the ConnectController service. Now the matching UEFI driver
379is determined by iterating over the implementations of the
380EFI_DRIVER_BINDING_PROTOCOL.
381
382It is the task of the UEFI driver to create a corresponding U-Boot device and to
383proxy calls for this U-Boot device to the controller.
384
385In U-Boot 2018.03 this has only been implemented for block IO devices.
386
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200387UEFI uclass
388~~~~~~~~~~~
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100389
390An UEFI uclass driver (lib/efi_driver/efi_uclass.c) has been created that
391takes care of initializing the UEFI drivers and providing the
392EFI_DRIVER_BINDING_PROTOCOL implementation for the UEFI drivers.
393
394A linker created list is used to keep track of the UEFI drivers. To create an
395entry in the list the UEFI driver uses the U_BOOT_DRIVER macro specifying
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200396UCLASS_EFI as the ID of its uclass, e.g::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100397
398 /* Identify as UEFI driver */
399 U_BOOT_DRIVER(efi_block) = {
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200400 .name = "EFI block driver",
401 .id = UCLASS_EFI,
402 .ops = &driver_ops,
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100403 };
404
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200405The available operations are defined via the structure struct efi_driver_ops::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100406
407 struct efi_driver_ops {
408 const efi_guid_t *protocol;
409 const efi_guid_t *child_protocol;
410 int (*bind)(efi_handle_t handle, void *interface);
411 };
412
413When the supported() function of the EFI_DRIVER_BINDING_PROTOCOL is called the
414uclass checks if the protocol GUID matches the protocol GUID of the UEFI driver.
415In the start() function the bind() function of the UEFI driver is called after
416checking the GUID.
417The stop() function of the EFI_DRIVER_BINDING_PROTOCOL disconnects the child
418controllers created by the UEFI driver and the UEFI driver. (In U-Boot v2013.03
419this is not yet completely implemented.)
420
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200421UEFI block IO driver
422~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100423
424The UEFI block IO driver supports devices exposing the EFI_BLOCK_IO_PROTOCOL.
425
426When connected it creates a new U-Boot block IO device with interface type
427IF_TYPE_EFI, adds child controllers mapping the partitions, and installs the
428EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on these. This can be used together with the
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200429software iPXE to boot from iSCSI network drives [4].
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100430
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200431This driver is only available if U-Boot is configured with::
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100432
433 CONFIG_BLK=y
434 CONFIG_PARTITIONS=y
435
Heinrich Schuchardt71a7de42020-02-22 07:47:20 +0100436Miscellaneous
437-------------
438
439Load file 2 protocol
440~~~~~~~~~~~~~~~~~~~~
441
442The load file 2 protocol can be used by the Linux kernel to load the initial
443RAM disk. U-Boot can be configured to provide an implementation with::
444
445 EFI_LOAD_FILE2_INITRD=y
446 EFI_INITRD_FILESPEC=interface dev:part path_to_initrd
447
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200448Links
449-----
Heinrich Schuchardt1914e5b2018-03-02 19:58:50 +0100450
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200451* [1] http://uefi.org/specifications - UEFI specifications
452* [2] https://github.com/ARM-software/ebbr/releases/download/v1.0/ebbr-v1.0.pdf -
Heinrich Schuchardtdc6f3f42019-04-10 08:04:38 +0200453 Embedded Base Boot Requirements (EBBR) Specification - Release v1.0
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200454* [3] https://developer.arm.com/docs/den0044/latest/server-base-boot-requirements-system-software-on-arm-platforms-version-11 -
Heinrich Schuchardt9ba712d2019-03-28 08:09:16 +0100455 Server Base Boot Requirements System Software on ARM Platforms - Version 1.1
Heinrich Schuchardt73d95c22019-07-26 06:46:08 +0200456* [4] :doc:`iscsi`
457* [5] :doc:`../driver-model/index`