blob: 43fb10f7978eeb24a6a4bf4d23811a0e39f5b6b0 [file] [log] [blame]
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +02001.. SPDX-License-Identifier: GPL-2.0+
2.. Copyright (c) 2018 Heinrich Schuchardt
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +01003
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +02004UEFI on U-Boot
5==============
Heinrich Schuchardt5fa03de2018-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 Schuchardtfd0b53f2019-07-26 06:46:08 +020013Development target
14------------------
Heinrich Schuchardta28d0732019-03-28 08:09:16 +010015
Heinrich Schuchardt9ec8f5e2019-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 Schuchardtfd0b53f2019-07-26 06:46:08 +020018[2]. The "Server Base Boot Requirements System Software on ARM Platforms" [3]
Heinrich Schuchardt9ec8f5e2019-04-10 08:04:38 +020019describes a superset of the EBBR specification and may be used as further
20reference.
Heinrich Schuchardta28d0732019-03-28 08:09:16 +010021
22A full blown UEFI implementation would contradict the U-Boot design principle
23"keep it small".
24
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020025Building U-Boot for UEFI
26------------------------
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +010027
Heinrich Schuchardt10288402018-12-30 12:54:36 +010028The UEFI standard supports only little-endian systems. The UEFI support can be
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020029activated for ARM and x86 by specifying::
Heinrich Schuchardt5fa03de2018-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 Schuchardtfd0b53f2019-07-26 06:46:08 +020037loaded UEFI application [4], requires::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +010038
39 CONFIG_BLK=y
40 CONFIG_PARTITIONS=y
41
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020042Executing a UEFI binary
43~~~~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +010044
45The bootefi command is used to start UEFI applications or to install UEFI
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +020046drivers. It takes two parameters::
Heinrich Schuchardt5fa03de2018-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 Schuchardtfd0b53f2019-07-26 06:46:08 +020053Below you find the output of an example session starting GRUB::
Heinrich Schuchardt5fa03de2018-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
Heinrich Schuchardt6b821592021-01-12 12:46:24 +010062When booting from a memory location it is unknown from which file it was loaded.
63Therefore the bootefi command uses the device path of the block device partition
64or the network adapter and the file name of the most recently loaded PE-COFF
65file when setting up the loaded image protocol.
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +010066
Cristian Ciocaltea62bb8902019-12-24 18:05:41 +020067Launching a UEFI binary from a FIT image
68~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69
70A signed FIT image can be used to securely boot a UEFI image via the
71bootm command. This feature is available if U-Boot is configured with::
72
73 CONFIG_BOOTM_EFI=y
74
75A sample configuration is provided as file doc/uImage.FIT/uefi.its.
76
77Below you find the output of an example session starting GRUB::
78
79 => load mmc 0:1 ${kernel_addr_r} image.fit
80 4620426 bytes read in 83 ms (53.1 MiB/s)
81 => bootm ${kernel_addr_r}#config-grub-nofdt
82 ## Loading kernel from FIT Image at 40400000 ...
83 Using 'config-grub-nofdt' configuration
84 Verifying Hash Integrity ... sha256,rsa2048:dev+ OK
85 Trying 'efi-grub' kernel subimage
86 Description: GRUB EFI Firmware
87 Created: 2019-11-20 8:18:16 UTC
88 Type: Kernel Image (no loading done)
89 Compression: uncompressed
90 Data Start: 0x404000d0
91 Data Size: 450560 Bytes = 440 KiB
92 Hash algo: sha256
93 Hash value: 4dbee00021112df618f58b3f7cf5e1595533d543094064b9ce991e8b054a9eec
94 Verifying Hash Integrity ... sha256+ OK
95 XIP Kernel Image (no loading done)
96 ## Transferring control to EFI (at address 404000d0) ...
97 Welcome to GRUB!
98
99See doc/uImage.FIT/howto.txt for an introduction to FIT images.
100
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900101Configuring UEFI secure boot
102~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200104The UEFI specification[1] defines a secure way of executing UEFI images
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900105by verifying a signature (or message digest) of image with certificates.
106This feature on U-Boot is enabled with::
107
108 CONFIG_UEFI_SECURE_BOOT=y
109
110To make the boot sequence safe, you need to establish a chain of trust;
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200111In UEFI secure boot the chain trust is defined by the following UEFI variables
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900112
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200113* PK - Platform Key
114* KEK - Key Exchange Keys
115* db - white list database
116* dbx - black list database
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900117
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200118An in depth description of UEFI secure boot is beyond the scope of this
119document. Please, refer to the UEFI specification and available online
120documentation. Here is a simple example that you can follow for your initial
121attempt (Please note that the actual steps will depend on your system and
122environment.):
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900123
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200124Install the required tools on your host
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900125
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200126* openssl
127* efitools
128* sbsigntool
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900129
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200130Create signing keys and the key database on your host:
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900131
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200132The platform key
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900133
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200134.. code-block:: bash
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900135
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200136 openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_PK/ \
137 -keyout PK.key -out PK.crt -nodes -days 365
138 cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
139 PK.crt PK.esl;
140 sign-efi-sig-list -c PK.crt -k PK.key PK PK.esl PK.auth
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900141
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200142The key exchange keys
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900143
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200144.. code-block:: bash
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900145
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200146 openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_KEK/ \
147 -keyout KEK.key -out KEK.crt -nodes -days 365
148 cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
149 KEK.crt KEK.esl
150 sign-efi-sig-list -c PK.crt -k PK.key KEK KEK.esl KEK.auth
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900151
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200152The whitelist database
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900153
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200154.. code-block:: bash
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900155
Heinrich Schuchardt200584c2020-12-12 09:15:12 +0100156 openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=TEST_db/ \
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200157 -keyout db.key -out db.crt -nodes -days 365
Heinrich Schuchardt200584c2020-12-12 09:15:12 +0100158 cert-to-efi-sig-list -g 11111111-2222-3333-4444-123456789abc \
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200159 db.crt db.esl
Heinrich Schuchardt200584c2020-12-12 09:15:12 +0100160 sign-efi-sig-list -c KEK.crt -k KEK.key db db.esl db.auth
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900161
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200162Copy the \*.auth files to media, say mmc, that is accessible from U-Boot.
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900163
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200164Sign an image with one of the keys in "db" on your host
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900165
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200166.. code-block:: bash
167
168 sbsign --key db.key --cert db.crt helloworld.efi
169
170Now in U-Boot install the keys on your board::
171
172 fatload mmc 0:1 <tmpaddr> PK.auth
Heinrich Schuchardtfa11c862020-08-24 08:27:49 +0200173 setenv -e -nv -bs -rt -at -i <tmpaddr>:$filesize PK
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200174 fatload mmc 0:1 <tmpaddr> KEK.auth
Heinrich Schuchardtfa11c862020-08-24 08:27:49 +0200175 setenv -e -nv -bs -rt -at -i <tmpaddr>:$filesize KEK
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200176 fatload mmc 0:1 <tmpaddr> db.auth
Heinrich Schuchardtfa11c862020-08-24 08:27:49 +0200177 setenv -e -nv -bs -rt -at -i <tmpaddr>:$filesize db
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200178
179Set up boot parameters on your board::
180
Ilias Apalodimas773c0902021-03-17 21:55:01 +0200181 efidebug boot add -b 1 HELLO mmc 0:1 /helloworld.efi.signed ""
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200182
Ilias Apalodimasc92aa4b2021-03-17 21:55:02 +0200183Since kernel 5.7 there's an alternative way of loading an initrd using
184LoadFile2 protocol if CONFIG_EFI_LOAD_FILE2_INITRD is enabled.
185The initrd path can be specified with::
186
187 efidebug boot add -b ABE0 'kernel' mmc 0:1 Image -i mmc 0:1 initrd
188
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200189Now your board can run the signed image via the boot manager (see below).
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900190You can also try this sequence by running Pytest, test_efi_secboot,
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200191on the sandbox
192
193.. code-block:: bash
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900194
Heinrich Schuchardt664ad182020-04-16 20:31:56 +0200195 cd <U-Boot source directory>
196 pytest.py test/py/tests/test_efi_secboot/test_signed.py --bd sandbox
AKASHI Takahiroe674d8d2020-04-14 11:51:54 +0900197
Heinrich Schuchardt87f43de2020-07-14 12:52:51 +0200198UEFI binaries may be signed by Microsoft using the following certificates:
199
200* KEK: Microsoft Corporation KEK CA 2011
201 http://go.microsoft.com/fwlink/?LinkId=321185.
202* db: Microsoft Windows Production PCA 2011
203 http://go.microsoft.com/fwlink/p/?linkid=321192.
204* db: Microsoft Corporation UEFI CA 2011
205 http://go.microsoft.com/fwlink/p/?linkid=321194.
206
Ilias Apalodimasef8bd412020-05-17 22:25:47 +0300207Using OP-TEE for EFI variables
208~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
209
210Instead of implementing UEFI variable services inside U-Boot they can
211also be provided in the secure world by a module for OP-TEE[1]. The
212interface between U-Boot and OP-TEE for variable services is enabled by
213CONFIG_EFI_MM_COMM_TEE=y.
214
215Tianocore EDK II's standalone management mode driver for variables can
216be linked to OP-TEE for this purpose. This module uses the Replay
217Protected Memory Block (RPMB) of an eMMC device for persisting
218non-volatile variables. When calling the variable services via the
219OP-TEE API U-Boot's OP-TEE supplicant relays calls to the RPMB driver
220which has to be enabled via CONFIG_SUPPORT_EMMC_RPMB=y.
221
Ilias Apalodimasa300e442021-04-01 13:35:38 +0300222EDK2 Build instructions
223***********************
224
225.. code-block:: bash
226
227 $ git clone https://github.com/tianocore/edk2.git
228 $ git clone https://github.com/tianocore/edk2-platforms.git
229 $ cd edk2
230 $ git submodule init && git submodule update --init --recursive
231 $ cd ..
232 $ export WORKSPACE=$(pwd)
233 $ export PACKAGES_PATH=$WORKSPACE/edk2:$WORKSPACE/edk2-platforms
234 $ export ACTIVE_PLATFORM="Platform/StandaloneMm/PlatformStandaloneMmPkg/PlatformStandaloneMmRpmb.dsc"
235 $ export GCC5_AARCH64_PREFIX=aarch64-linux-gnu-
236 $ source edk2/edksetup.sh
237 $ make -C edk2/BaseTools
238 $ build -p $ACTIVE_PLATFORM -b RELEASE -a AARCH64 -t GCC5 -n `nproc`
239
240OP-TEE Build instructions
241*************************
242
243.. code-block:: bash
244
245 $ git clone https://github.com/OP-TEE/optee_os.git
246 $ cd optee_os
247 $ ln -s ../Build/MmStandaloneRpmb/RELEASE_GCC5/FV/BL32_AP_MM.fd
248 $ export ARCH=arm
249 $ CROSS_COMPILE32=arm-linux-gnueabihf- make -j32 CFG_ARM64_core=y \
250 PLATFORM=<myboard> CFG_STMM_PATH=BL32_AP_MM.fd CFG_RPMB_FS=y \
Ilias Apalodimas63cc27a2021-12-27 10:08:15 +0200251 CFG_RPMB_FS_DEV_ID=0 CFG_CORE_HEAP_SIZE=524288 CFG_RPMB_WRITE_KEY=y \
252 CFG_CORE_DYN_SHM=y CFG_RPMB_TESTKEY=y CFG_REE_FS=n \
253 CFG_CORE_ARM64_PA_BITS=48 CFG_TEE_CORE_LOG_LEVEL=1 \
Ilias Apalodimasa300e442021-04-01 13:35:38 +0300254 CFG_TEE_TA_LOG_LEVEL=1 CFG_SCTLR_ALIGNMENT_CHECK=n
255
256U-Boot Build instructions
257*************************
258
259Although the StandAloneMM binary comes from EDK2, using and storing the
260variables is currently available in U-Boot only.
261
262.. code-block:: bash
263
264 $ git clone https://github.com/u-boot/u-boot.git
265 $ cd u-boot
266 $ export CROSS_COMPILE=aarch64-linux-gnu-
267 $ export ARCH=<arch>
268 $ make <myboard>_defconfig
269 $ make menuconfig
270
271Enable ``CONFIG_OPTEE``, ``CONFIG_CMD_OPTEE_RPMB`` and ``CONFIG_EFI_MM_COMM_TEE``
272
273.. warning::
274
275 - Your OP-TEE platform port must support Dynamic shared memory, since that's
276 the only kind of memory U-Boot supports for now.
277
278[1] https://optee.readthedocs.io/en/latest/building/efi_vars/stmm.html
Ilias Apalodimasef8bd412020-05-17 22:25:47 +0300279
AKASHI Takahiro60fc0c62021-10-07 15:23:31 +0900280Enabling UEFI Capsule Update feature
281~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
282
283Support has been added for the UEFI capsule update feature which
284enables updating the U-Boot image using the UEFI firmware management
285protocol (FMP). The capsules are not passed to the firmware through
286the UpdateCapsule runtime service. Instead, capsule-on-disk
287functionality is used for fetching the capsule from the EFI System
288Partition (ESP) by placing the capsule file under the
289\EFI\UpdateCapsule directory.
290
291The directory \EFI\UpdateCapsule is checked for capsules only within the
292EFI system partition on the device specified in the active boot option
293determined by reference to BootNext variable or BootOrder variable processing.
294The active Boot Variable is the variable with highest priority BootNext or
295within BootOrder that refers to a device found to be present. Boot variables
296in BootOrder but referring to devices not present are ignored when determining
297active boot variable.
298Before starting a capsule update make sure your capsules are installed in the
299correct ESP partition or set BootNext.
300
301Performing the update
302*********************
303
304Since U-boot doesn't currently support SetVariable at runtime there's a Kconfig
305option (CONFIG_EFI_IGNORE_OSINDICATIONS) to disable the OsIndications variable
306check. If that option is enabled just copy your capsule to \EFI\UpdateCapsule.
307
308If that option is disabled, you'll need to set the OsIndications variable with::
309
310 => setenv -e -nv -bs -rt -v OsIndications =0x04
311
312Finally, the capsule update can be initiated either by rebooting the board,
313which is the preferred method, or by issuing the following command::
314
315 => efidebug capsule disk-update
316
317**The efidebug command is should only be used during debugging/development.**
318
319Enabling Capsule Authentication
320*******************************
321
322The UEFI specification defines a way of authenticating the capsule to
323be updated by verifying the capsule signature. The capsule signature
324is computed and prepended to the capsule payload at the time of
325capsule generation. This signature is then verified by using the
326public key stored as part of the X509 certificate. This certificate is
327in the form of an efi signature list (esl) file, which is embedded as
328part of U-Boot.
329
330The capsule authentication feature can be enabled through the
331following config, in addition to the configs listed above for capsule
332update::
333
334 CONFIG_EFI_CAPSULE_AUTHENTICATE=y
335 CONFIG_EFI_CAPSULE_KEY_PATH=<path to .esl cert>
336
337The public and private keys used for the signing process are generated
338and used by the steps highlighted below::
339
340 1. Install utility commands on your host
341 * OPENSSL
342 * efitools
343
344 2. Create signing keys and certificate files on your host
345
346 $ openssl req -x509 -sha256 -newkey rsa:2048 -subj /CN=CRT/ \
347 -keyout CRT.key -out CRT.crt -nodes -days 365
348 $ cert-to-efi-sig-list CRT.crt CRT.esl
349
350 $ openssl x509 -in CRT.crt -out CRT.cer -outform DER
351 $ openssl x509 -inform DER -in CRT.cer -outform PEM -out CRT.pub.pem
352
353 $ openssl pkcs12 -export -out CRT.pfx -inkey CRT.key -in CRT.crt
354 $ openssl pkcs12 -in CRT.pfx -nodes -out CRT.pem
355
356The capsule file can be generated by using the GenerateCapsule.py
357script in EDKII::
358
359 $ ./BaseTools/BinWrappers/PosixLike/GenerateCapsule -e -o \
360 <capsule_file_name> --monotonic-count <val> --fw-version \
361 <val> --lsv <val> --guid \
362 e2bb9c06-70e9-4b14-97a3-5a7913176e3f --verbose \
363 --update-image-index <val> --signer-private-cert \
364 /path/to/CRT.pem --trusted-public-cert \
365 /path/to/CRT.pub.pem --other-public-cert /path/to/CRT.pub.pem \
366 <u-boot.bin>
367
368Place the capsule generated in the above step on the EFI System
369Partition under the EFI/UpdateCapsule directory
370
371Testing on QEMU
372***************
373
374Currently, support has been added on the QEMU ARM64 virt platform for
375updating the U-Boot binary as a raw image when the platform is booted
376in non-secure mode, i.e. with CONFIG_TFABOOT disabled. For this
377configuration, the QEMU platform needs to be booted with
378'secure=off'. The U-Boot binary placed on the first bank of the NOR
379flash at offset 0x0. The U-Boot environment is placed on the second
380NOR flash bank at offset 0x4000000.
381
382The capsule update feature is enabled with the following configuration
383settings::
384
385 CONFIG_MTD=y
386 CONFIG_FLASH_CFI_MTD=y
387 CONFIG_CMD_MTDPARTS=y
388 CONFIG_CMD_DFU=y
389 CONFIG_DFU_MTD=y
390 CONFIG_PCI_INIT_R=y
391 CONFIG_EFI_CAPSULE_ON_DISK=y
392 CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT=y
393 CONFIG_EFI_CAPSULE_FIRMWARE=y
394 CONFIG_EFI_CAPSULE_FIRMWARE_RAW=y
395
396In addition, the following config needs to be disabled(QEMU ARM specific)::
397
398 CONFIG_TFABOOT
399
400The capsule file can be generated by using the tools/mkeficapsule::
401
402 $ mkeficapsule --raw <u-boot.bin> --index 1 <capsule_file_name>
403
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200404Executing the boot manager
405~~~~~~~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100406
Heinrich Schuchardt8d343f82020-08-16 12:27:19 +0200407The UEFI specification foresees to define boot entries and boot sequence via
408UEFI variables. Booting according to these variables is possible via::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100409
410 bootefi bootmgr [fdt address]
411
Heinrich Schuchardt8d343f82020-08-16 12:27:19 +0200412As of U-Boot v2020.10 UEFI variables cannot be set at runtime. The U-Boot
413command 'efidebug' can be used to set the variables.
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100414
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200415Executing the built in hello world application
416~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100417
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200418A hello world UEFI application can be built with::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100419
420 CONFIG_CMD_BOOTEFI_HELLO_COMPILE=y
421
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200422It can be embedded into the U-Boot binary with::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100423
424 CONFIG_CMD_BOOTEFI_HELLO=y
425
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200426The bootefi command is used to start the embedded hello world application::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100427
428 bootefi hello [fdt address]
429
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200430Below you find the output of an example session::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100431
432 => bootefi hello ${fdtcontroladdr}
433 ## Starting EFI application at 01000000 ...
434 WARNING: using memory device/image path, this may confuse some payloads!
435 Hello, world!
436 Running on UEFI 2.7
437 Have SMBIOS table
438 Have device tree
439 Load options: root=/dev/sdb3 init=/sbin/init rootwait ro
440 ## Application terminated, r = 0
441
442The environment variable fdtcontroladdr points to U-Boot's internal device tree
443(if available).
444
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200445Executing the built-in self-test
446~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100447
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200448An UEFI self-test suite can be embedded in U-Boot by building with::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100449
450 CONFIG_CMD_BOOTEFI_SELFTEST=y
451
452For testing the UEFI implementation the bootefi command can be used to start the
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200453self-test::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100454
455 bootefi selftest [fdt address]
456
457The environment variable 'efi_selftest' can be used to select a single test. If
458it is not provided all tests are executed except those marked as 'on request'.
459If the environment variable is set to 'list' a list of all tests is shown.
460
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200461Below you can find the output of an example session::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100462
463 => setenv efi_selftest simple network protocol
464 => bootefi selftest
465 Testing EFI API implementation
466 Selected test: 'simple network protocol'
467 Setting up 'simple network protocol'
468 Setting up 'simple network protocol' succeeded
469 Executing 'simple network protocol'
470 DHCP Discover
471 DHCP reply received from 192.168.76.2 (52:55:c0:a8:4c:02)
472 as broadcast message.
473 Executing 'simple network protocol' succeeded
474 Tearing down 'simple network protocol'
475 Tearing down 'simple network protocol' succeeded
476 Boot services terminated
477 Summary: 0 failures
478 Preparing for reset. Press any key.
479
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200480The UEFI life cycle
481-------------------
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100482
483After the U-Boot platform has been initialized the UEFI API provides two kinds
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200484of services:
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100485
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200486* boot services
487* runtime services
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100488
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200489The API can be extended by loading UEFI drivers which come in two variants:
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100490
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200491* boot drivers
492* runtime drivers
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100493
494UEFI drivers are installed with U-Boot's bootefi command. With the same command
495UEFI applications can be executed.
496
497Loaded images of UEFI drivers stay in memory after returning to U-Boot while
498loaded images of applications are removed from memory.
499
500An UEFI application (e.g. an operating system) that wants to take full control
501of the system calls ExitBootServices. After a UEFI application calls
502ExitBootServices
503
504* boot services are not available anymore
505* timer events are stopped
506* the memory used by U-Boot except for runtime services is released
507* the memory used by boot time drivers is released
508
509So this is a point of no return. Afterwards the UEFI application can only return
510to U-Boot by rebooting.
511
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200512The UEFI object model
513---------------------
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100514
515UEFI offers a flexible and expandable object model. The objects in the UEFI API
516are devices, drivers, and loaded images. These objects are referenced by
517handles.
518
519The interfaces implemented by the objects are referred to as protocols. These
520are identified by GUIDs. They can be installed and uninstalled by calling the
521appropriate boot services.
522
523Handles are created by the InstallProtocolInterface or the
524InstallMultipleProtocolinterfaces service if NULL is passed as handle.
525
526Handles are deleted when the last protocol has been removed with the
527UninstallProtocolInterface or the UninstallMultipleProtocolInterfaces service.
528
529Devices offer the EFI_DEVICE_PATH_PROTOCOL. A device path is the concatenation
530of device nodes. By their device paths all devices of a system are arranged in a
531tree.
532
533Drivers offer the EFI_DRIVER_BINDING_PROTOCOL. This protocol is used to connect
534a driver to devices (which are referenced as controllers in this context).
535
536Loaded images offer the EFI_LOADED_IMAGE_PROTOCOL. This protocol provides meta
537information about the image and a pointer to the unload callback function.
538
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200539The UEFI events
540---------------
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100541
542In the UEFI terminology an event is a data object referencing a notification
543function which is queued for calling when the event is signaled. The following
544types of events exist:
545
546* periodic and single shot timer events
547* exit boot services events, triggered by calling the ExitBootServices() service
548* virtual address change events
549* memory map change events
550* read to boot events
551* reset system events
552* system table events
553* events that are only triggered programmatically
554
555Events can be created with the CreateEvent service and deleted with CloseEvent
556service.
557
558Events can be assigned to an event group. If any of the events in a group is
559signaled, all other events in the group are also set to the signaled state.
560
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200561The UEFI driver model
562---------------------
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100563
564A driver is specific for a single protocol installed on a device. To install a
565driver on a device the ConnectController service is called. In this context
566controller refers to the device for which the driver is installed.
567
568The relevant drivers are identified using the EFI_DRIVER_BINDING_PROTOCOL. This
569protocol has has three functions:
570
571* supported - determines if the driver is compatible with the device
572* start - installs the driver by opening the relevant protocol with
573 attribute EFI_OPEN_PROTOCOL_BY_DRIVER
574* stop - uninstalls the driver
575
576The driver may create child controllers (child devices). E.g. a driver for block
577IO devices will create the device handles for the partitions. The child
578controllers will open the supported protocol with the attribute
579EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
580
581A driver can be detached from a device using the DisconnectController service.
582
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200583U-Boot devices mapped as UEFI devices
584-------------------------------------
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100585
586Some of the U-Boot devices are mapped as UEFI devices
587
588* block IO devices
589* console
590* graphical output
591* network adapter
592
593As of U-Boot 2018.03 the logic for doing this is hard coded.
594
595The development target is to integrate the setup of these UEFI devices with the
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200596U-Boot driver model [5]. So when a U-Boot device is discovered a handle should
597be created and the device path protocol and the relevant IO protocol should be
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100598installed. The UEFI driver then would be attached by calling ConnectController.
599When a U-Boot device is removed DisconnectController should be called.
600
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200601UEFI devices mapped as U-Boot devices
602-------------------------------------
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100603
604UEFI drivers binaries and applications may create new (virtual) devices, install
605a protocol and call the ConnectController service. Now the matching UEFI driver
606is determined by iterating over the implementations of the
607EFI_DRIVER_BINDING_PROTOCOL.
608
609It is the task of the UEFI driver to create a corresponding U-Boot device and to
610proxy calls for this U-Boot device to the controller.
611
612In U-Boot 2018.03 this has only been implemented for block IO devices.
613
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200614UEFI uclass
615~~~~~~~~~~~
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100616
617An UEFI uclass driver (lib/efi_driver/efi_uclass.c) has been created that
618takes care of initializing the UEFI drivers and providing the
619EFI_DRIVER_BINDING_PROTOCOL implementation for the UEFI drivers.
620
621A linker created list is used to keep track of the UEFI drivers. To create an
622entry in the list the UEFI driver uses the U_BOOT_DRIVER macro specifying
Simon Glass15c4d672021-12-04 08:56:30 -0700623UCLASS_EFI_LOADER as the ID of its uclass, e.g::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100624
625 /* Identify as UEFI driver */
626 U_BOOT_DRIVER(efi_block) = {
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200627 .name = "EFI block driver",
Simon Glass15c4d672021-12-04 08:56:30 -0700628 .id = UCLASS_EFI_LOADER,
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200629 .ops = &driver_ops,
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100630 };
631
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200632The available operations are defined via the structure struct efi_driver_ops::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100633
634 struct efi_driver_ops {
635 const efi_guid_t *protocol;
636 const efi_guid_t *child_protocol;
637 int (*bind)(efi_handle_t handle, void *interface);
638 };
639
640When the supported() function of the EFI_DRIVER_BINDING_PROTOCOL is called the
641uclass checks if the protocol GUID matches the protocol GUID of the UEFI driver.
642In the start() function the bind() function of the UEFI driver is called after
643checking the GUID.
644The stop() function of the EFI_DRIVER_BINDING_PROTOCOL disconnects the child
645controllers created by the UEFI driver and the UEFI driver. (In U-Boot v2013.03
646this is not yet completely implemented.)
647
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200648UEFI block IO driver
649~~~~~~~~~~~~~~~~~~~~
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100650
651The UEFI block IO driver supports devices exposing the EFI_BLOCK_IO_PROTOCOL.
652
653When connected it creates a new U-Boot block IO device with interface type
Simon Glass15c4d672021-12-04 08:56:30 -0700654IF_TYPE_EFI_LOADER, adds child controllers mapping the partitions, and installs
655the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on these. This can be used together with the
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200656software iPXE to boot from iSCSI network drives [4].
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100657
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200658This driver is only available if U-Boot is configured with::
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100659
660 CONFIG_BLK=y
661 CONFIG_PARTITIONS=y
662
Heinrich Schuchardtc4d45422020-02-22 07:47:20 +0100663Miscellaneous
664-------------
665
666Load file 2 protocol
667~~~~~~~~~~~~~~~~~~~~
668
669The load file 2 protocol can be used by the Linux kernel to load the initial
670RAM disk. U-Boot can be configured to provide an implementation with::
671
672 EFI_LOAD_FILE2_INITRD=y
Ilias Apalodimasc92aa4b2021-03-17 21:55:02 +0200673
674When the option is enabled the user can add the initrd path with the efidebug
675command.
676
677Load options Boot#### have a FilePathList[] member. The first element of
678the array (FilePathList[0]) is the EFI binary to execute. When an initrd
679is specified the Device Path for the initrd is denoted by a VenMedia node
680with the EFI_INITRD_MEDIA_GUID. Each entry of the array is terminated by the
681'end of entire device path' subtype (0xff). If a user wants to define multiple
682initrds, those must by separated by the 'end of this instance' identifier of
683the end node (0x01).
684
685So our final format of the FilePathList[] is::
686
687 Loaded image - end node (0xff) - VenMedia - initrd_1 - [end node (0x01) - initrd_n ...] - end node (0xff)
Heinrich Schuchardtc4d45422020-02-22 07:47:20 +0100688
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200689Links
690-----
Heinrich Schuchardt5fa03de2018-03-02 19:58:50 +0100691
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200692* [1] http://uefi.org/specifications - UEFI specifications
693* [2] https://github.com/ARM-software/ebbr/releases/download/v1.0/ebbr-v1.0.pdf -
Heinrich Schuchardt9ec8f5e2019-04-10 08:04:38 +0200694 Embedded Base Boot Requirements (EBBR) Specification - Release v1.0
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200695* [3] https://developer.arm.com/docs/den0044/latest/server-base-boot-requirements-system-software-on-arm-platforms-version-11 -
Heinrich Schuchardta28d0732019-03-28 08:09:16 +0100696 Server Base Boot Requirements System Software on ARM Platforms - Version 1.1
Heinrich Schuchardtfd0b53f2019-07-26 06:46:08 +0200697* [4] :doc:`iscsi`
698* [5] :doc:`../driver-model/index`