imx8m: config: convert to bootm_size
[platform/kernel/u-boot.git] / doc / uefi / uefi.rst
1 .. SPDX-License-Identifier: GPL-2.0+
2 .. Copyright (c) 2018 Heinrich Schuchardt
3
4 UEFI on U-Boot
5 ==============
6
7 The Unified Extensible Firmware Interface Specification (UEFI) [1] has become
8 the default for booting on AArch64 and x86 systems. It provides a stable API for
9 the interaction of drivers and applications with the firmware. The API comprises
10 access to block storage, network, and console to name a few. The Linux kernel
11 and boot loaders like GRUB or the FreeBSD loader can be executed.
12
13 Development target
14 ------------------
15
16 The implementation of UEFI in U-Boot strives to reach the requirements described
17 in the "Embedded Base Boot Requirements (EBBR) Specification - Release v1.0"
18 [2]. The "Server Base Boot Requirements System Software on ARM Platforms" [3]
19 describes a superset of the EBBR specification and may be used as further
20 reference.
21
22 A full blown UEFI implementation would contradict the U-Boot design principle
23 "keep it small".
24
25 Building U-Boot for UEFI
26 ------------------------
27
28 The UEFI standard supports only little-endian systems. The UEFI support can be
29 activated for ARM and x86 by specifying::
30
31     CONFIG_CMD_BOOTEFI=y
32     CONFIG_EFI_LOADER=y
33
34 in the .config file.
35
36 Support for attaching virtual block devices, e.g. iSCSI drives connected by the
37 loaded UEFI application [4], requires::
38
39     CONFIG_BLK=y
40     CONFIG_PARTITIONS=y
41
42 Executing a UEFI binary
43 ~~~~~~~~~~~~~~~~~~~~~~~
44
45 The bootefi command is used to start UEFI applications or to install UEFI
46 drivers. It takes two parameters::
47
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
53 Below you find the output of an example session starting GRUB::
54
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
62 The environment variable 'bootargs' is passed as load options in the UEFI system
63 table. The Linux kernel EFI stub uses the load options as command line
64 arguments.
65
66 Launching a UEFI binary from a FIT image
67 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68
69 A signed FIT image can be used to securely boot a UEFI image via the
70 bootm command. This feature is available if U-Boot is configured with::
71
72     CONFIG_BOOTM_EFI=y
73
74 A sample configuration is provided as file doc/uImage.FIT/uefi.its.
75
76 Below 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
98 See doc/uImage.FIT/howto.txt for an introduction to FIT images.
99
100 Configuring UEFI secure boot
101 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
103 The UEFI specification[1] defines a secure way of executing UEFI images
104 by verifying a signature (or message digest) of image with certificates.
105 This feature on U-Boot is enabled with::
106
107     CONFIG_UEFI_SECURE_BOOT=y
108
109 To make the boot sequence safe, you need to establish a chain of trust;
110 In UEFI secure boot the chain trust is defined by the following UEFI variables
111
112 * PK - Platform Key
113 * KEK - Key Exchange Keys
114 * db - white list database
115 * dbx - black list database
116
117 An in depth description of UEFI secure boot is beyond the scope of this
118 document. Please, refer to the UEFI specification and available online
119 documentation. Here is a simple example that you can follow for your initial
120 attempt (Please note that the actual steps will depend on your system and
121 environment.):
122
123 Install the required tools on your host
124
125 * openssl
126 * efitools
127 * sbsigntool
128
129 Create signing keys and the key database on your host:
130
131 The platform key
132
133 .. code-block:: bash
134
135     openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ \
136             -keyout PK.key -out PK.crt -nodes -days 365
137     cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
138             PK.crt PK.esl;
139     sign-efi-sig-list -c PK.crt -k PK.key PK PK.esl PK.auth
140
141 The key exchange keys
142
143 .. code-block:: bash
144
145     openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ \
146             -keyout KEK.key -out KEK.crt -nodes -days 365
147     cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
148             KEK.crt KEK.esl
149     sign-efi-sig-list -c PK.crt -k PK.key KEK KEK.esl KEK.auth
150
151 The whitelist database
152
153 .. code-block:: bash
154
155     $ openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db/ \
156             -keyout db.key -out db.crt -nodes -days 365
157     $ cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
158             db.crt db.esl
159     $ sign-efi-sig-list -c KEK.crt -k KEK.key db db.esl db.auth
160
161 Copy the \*.auth files to media, say mmc, that is accessible from U-Boot.
162
163 Sign an image with one of the keys in "db" on your host
164
165 .. code-block:: bash
166
167     sbsign --key db.key --cert db.crt helloworld.efi
168
169 Now in U-Boot install the keys on your board::
170
171     fatload mmc 0:1 <tmpaddr> PK.auth
172     setenv -e -nv -bs -rt -at -i <tmpaddr>,$filesize PK
173     fatload mmc 0:1 <tmpaddr> KEK.auth
174     setenv -e -nv -bs -rt -at -i <tmpaddr>,$filesize KEK
175     fatload mmc 0:1 <tmpaddr> db.auth
176     setenv -e -nv -bs -rt -at -i <tmpaddr>,$filesize db
177
178 Set up boot parameters on your board::
179
180     efidebug boot add 1 HELLO mmc 0:1 /helloworld.efi.signed ""
181
182 Now your board can run the signed image via the boot manager (see below).
183 You can also try this sequence by running Pytest, test_efi_secboot,
184 on the sandbox
185
186 .. code-block:: bash
187
188     cd <U-Boot source directory>
189     pytest.py test/py/tests/test_efi_secboot/test_signed.py --bd sandbox
190
191 UEFI binaries may be signed by Microsoft using the following certificates:
192
193 * KEK: Microsoft Corporation KEK CA 2011
194   http://go.microsoft.com/fwlink/?LinkId=321185.
195 * db: Microsoft Windows Production PCA 2011
196   http://go.microsoft.com/fwlink/p/?linkid=321192.
197 * db: Microsoft Corporation UEFI CA 2011
198   http://go.microsoft.com/fwlink/p/?linkid=321194.
199
200 Using OP-TEE for EFI variables
201 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
202
203 Instead of implementing UEFI variable services inside U-Boot they can
204 also be provided in the secure world by a module for OP-TEE[1]. The
205 interface between U-Boot and OP-TEE for variable services is enabled by
206 CONFIG_EFI_MM_COMM_TEE=y.
207
208 Tianocore EDK II's standalone management mode driver for variables can
209 be linked to OP-TEE for this purpose. This module uses the Replay
210 Protected Memory Block (RPMB) of an eMMC device for persisting
211 non-volatile variables. When calling the variable services via the
212 OP-TEE API U-Boot's OP-TEE supplicant relays calls to the RPMB driver
213 which has to be enabled via CONFIG_SUPPORT_EMMC_RPMB=y.
214
215 [1] https://optee.readthedocs.io/ - OP-TEE documentation
216
217 Executing the boot manager
218 ~~~~~~~~~~~~~~~~~~~~~~~~~~
219
220 The UEFI specification foresees to define boot entries and boot sequence via UEFI
221 variables. Booting according to these variables is possible via::
222
223     bootefi bootmgr [fdt address]
224
225 As of U-Boot v2018.03 UEFI variables are not persisted and cannot be set at
226 runtime.
227
228 Executing the built in hello world application
229 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
230
231 A hello world UEFI application can be built with::
232
233     CONFIG_CMD_BOOTEFI_HELLO_COMPILE=y
234
235 It can be embedded into the U-Boot binary with::
236
237     CONFIG_CMD_BOOTEFI_HELLO=y
238
239 The bootefi command is used to start the embedded hello world application::
240
241     bootefi hello [fdt address]
242
243 Below you find the output of an example session::
244
245     => bootefi hello ${fdtcontroladdr}
246     ## Starting EFI application at 01000000 ...
247     WARNING: using memory device/image path, this may confuse some payloads!
248     Hello, world!
249     Running on UEFI 2.7
250     Have SMBIOS table
251     Have device tree
252     Load options: root=/dev/sdb3 init=/sbin/init rootwait ro
253     ## Application terminated, r = 0
254
255 The environment variable fdtcontroladdr points to U-Boot's internal device tree
256 (if available).
257
258 Executing the built-in self-test
259 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
260
261 An UEFI self-test suite can be embedded in U-Boot by building with::
262
263     CONFIG_CMD_BOOTEFI_SELFTEST=y
264
265 For testing the UEFI implementation the bootefi command can be used to start the
266 self-test::
267
268     bootefi selftest [fdt address]
269
270 The environment variable 'efi_selftest' can be used to select a single test. If
271 it is not provided all tests are executed except those marked as 'on request'.
272 If the environment variable is set to 'list' a list of all tests is shown.
273
274 Below you can find the output of an example session::
275
276     => setenv efi_selftest simple network protocol
277     => bootefi selftest
278     Testing EFI API implementation
279     Selected test: 'simple network protocol'
280     Setting up 'simple network protocol'
281     Setting up 'simple network protocol' succeeded
282     Executing 'simple network protocol'
283     DHCP Discover
284     DHCP reply received from 192.168.76.2 (52:55:c0:a8:4c:02)
285       as broadcast message.
286     Executing 'simple network protocol' succeeded
287     Tearing down 'simple network protocol'
288     Tearing down 'simple network protocol' succeeded
289     Boot services terminated
290     Summary: 0 failures
291     Preparing for reset. Press any key.
292
293 The UEFI life cycle
294 -------------------
295
296 After the U-Boot platform has been initialized the UEFI API provides two kinds
297 of services:
298
299 * boot services
300 * runtime services
301
302 The API can be extended by loading UEFI drivers which come in two variants:
303
304 * boot drivers
305 * runtime drivers
306
307 UEFI drivers are installed with U-Boot's bootefi command. With the same command
308 UEFI applications can be executed.
309
310 Loaded images of UEFI drivers stay in memory after returning to U-Boot while
311 loaded images of applications are removed from memory.
312
313 An UEFI application (e.g. an operating system) that wants to take full control
314 of the system calls ExitBootServices. After a UEFI application calls
315 ExitBootServices
316
317 * boot services are not available anymore
318 * timer events are stopped
319 * the memory used by U-Boot except for runtime services is released
320 * the memory used by boot time drivers is released
321
322 So this is a point of no return. Afterwards the UEFI application can only return
323 to U-Boot by rebooting.
324
325 The UEFI object model
326 ---------------------
327
328 UEFI offers a flexible and expandable object model. The objects in the UEFI API
329 are devices, drivers, and loaded images. These objects are referenced by
330 handles.
331
332 The interfaces implemented by the objects are referred to as protocols. These
333 are identified by GUIDs. They can be installed and uninstalled by calling the
334 appropriate boot services.
335
336 Handles are created by the InstallProtocolInterface or the
337 InstallMultipleProtocolinterfaces service if NULL is passed as handle.
338
339 Handles are deleted when the last protocol has been removed with the
340 UninstallProtocolInterface or the UninstallMultipleProtocolInterfaces service.
341
342 Devices offer the EFI_DEVICE_PATH_PROTOCOL. A device path is the concatenation
343 of device nodes. By their device paths all devices of a system are arranged in a
344 tree.
345
346 Drivers offer the EFI_DRIVER_BINDING_PROTOCOL. This protocol is used to connect
347 a driver to devices (which are referenced as controllers in this context).
348
349 Loaded images offer the EFI_LOADED_IMAGE_PROTOCOL. This protocol provides meta
350 information about the image and a pointer to the unload callback function.
351
352 The UEFI events
353 ---------------
354
355 In the UEFI terminology an event is a data object referencing a notification
356 function which is queued for calling when the event is signaled. The following
357 types of events exist:
358
359 * periodic and single shot timer events
360 * exit boot services events, triggered by calling the ExitBootServices() service
361 * virtual address change events
362 * memory map change events
363 * read to boot events
364 * reset system events
365 * system table events
366 * events that are only triggered programmatically
367
368 Events can be created with the CreateEvent service and deleted with CloseEvent
369 service.
370
371 Events can be assigned to an event group. If any of the events in a group is
372 signaled, all other events in the group are also set to the signaled state.
373
374 The UEFI driver model
375 ---------------------
376
377 A driver is specific for a single protocol installed on a device. To install a
378 driver on a device the ConnectController service is called. In this context
379 controller refers to the device for which the driver is installed.
380
381 The relevant drivers are identified using the EFI_DRIVER_BINDING_PROTOCOL. This
382 protocol has has three functions:
383
384 * supported - determines if the driver is compatible with the device
385 * start - installs the driver by opening the relevant protocol with
386   attribute EFI_OPEN_PROTOCOL_BY_DRIVER
387 * stop - uninstalls the driver
388
389 The driver may create child controllers (child devices). E.g. a driver for block
390 IO devices will create the device handles for the partitions. The child
391 controllers  will open the supported protocol with the attribute
392 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
393
394 A driver can be detached from a device using the DisconnectController service.
395
396 U-Boot devices mapped as UEFI devices
397 -------------------------------------
398
399 Some of the U-Boot devices are mapped as UEFI devices
400
401 * block IO devices
402 * console
403 * graphical output
404 * network adapter
405
406 As of U-Boot 2018.03 the logic for doing this is hard coded.
407
408 The development target is to integrate the setup of these UEFI devices with the
409 U-Boot driver model [5]. So when a U-Boot device is discovered a handle should
410 be created and the device path protocol and the relevant IO protocol should be
411 installed. The UEFI driver then would be attached by calling ConnectController.
412 When a U-Boot device is removed DisconnectController should be called.
413
414 UEFI devices mapped as U-Boot devices
415 -------------------------------------
416
417 UEFI drivers binaries and applications may create new (virtual) devices, install
418 a protocol and call the ConnectController service. Now the matching UEFI driver
419 is determined by iterating over the implementations of the
420 EFI_DRIVER_BINDING_PROTOCOL.
421
422 It is the task of the UEFI driver to create a corresponding U-Boot device and to
423 proxy calls for this U-Boot device to the controller.
424
425 In U-Boot 2018.03 this has only been implemented for block IO devices.
426
427 UEFI uclass
428 ~~~~~~~~~~~
429
430 An UEFI uclass driver (lib/efi_driver/efi_uclass.c) has been created that
431 takes care of initializing the UEFI drivers and providing the
432 EFI_DRIVER_BINDING_PROTOCOL implementation for the UEFI drivers.
433
434 A linker created list is used to keep track of the UEFI drivers. To create an
435 entry in the list the UEFI driver uses the U_BOOT_DRIVER macro specifying
436 UCLASS_EFI as the ID of its uclass, e.g::
437
438     /* Identify as UEFI driver */
439     U_BOOT_DRIVER(efi_block) = {
440         .name  = "EFI block driver",
441         .id    = UCLASS_EFI,
442         .ops   = &driver_ops,
443     };
444
445 The available operations are defined via the structure struct efi_driver_ops::
446
447     struct efi_driver_ops {
448         const efi_guid_t *protocol;
449         const efi_guid_t *child_protocol;
450         int (*bind)(efi_handle_t handle, void *interface);
451     };
452
453 When the supported() function of the EFI_DRIVER_BINDING_PROTOCOL is called the
454 uclass checks if the protocol GUID matches the protocol GUID of the UEFI driver.
455 In the start() function the bind() function of the UEFI driver is called after
456 checking the GUID.
457 The stop() function of the EFI_DRIVER_BINDING_PROTOCOL disconnects the child
458 controllers created by the UEFI driver and the UEFI driver. (In U-Boot v2013.03
459 this is not yet completely implemented.)
460
461 UEFI block IO driver
462 ~~~~~~~~~~~~~~~~~~~~
463
464 The UEFI block IO driver supports devices exposing the EFI_BLOCK_IO_PROTOCOL.
465
466 When connected it creates a new U-Boot block IO device with interface type
467 IF_TYPE_EFI, adds child controllers mapping the partitions, and installs the
468 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on these. This can be used together with the
469 software iPXE to boot from iSCSI network drives [4].
470
471 This driver is only available if U-Boot is configured with::
472
473     CONFIG_BLK=y
474     CONFIG_PARTITIONS=y
475
476 Miscellaneous
477 -------------
478
479 Load file 2 protocol
480 ~~~~~~~~~~~~~~~~~~~~
481
482 The load file 2 protocol can be used by the Linux kernel to load the initial
483 RAM disk. U-Boot can be configured to provide an implementation with::
484
485     EFI_LOAD_FILE2_INITRD=y
486     EFI_INITRD_FILESPEC=interface dev:part path_to_initrd
487
488 Links
489 -----
490
491 * [1] http://uefi.org/specifications - UEFI specifications
492 * [2] https://github.com/ARM-software/ebbr/releases/download/v1.0/ebbr-v1.0.pdf -
493   Embedded Base Boot Requirements (EBBR) Specification - Release v1.0
494 * [3] https://developer.arm.com/docs/den0044/latest/server-base-boot-requirements-system-software-on-arm-platforms-version-11 -
495   Server Base Boot Requirements System Software on ARM Platforms - Version 1.1
496 * [4] :doc:`iscsi`
497 * [5] :doc:`../driver-model/index`