1Trusted Firmware-A for Raspberry Pi 3 2===================================== 3 4.. section-numbering:: 5 :suffix: . 6 7.. contents:: 8 9The `Raspberry Pi 3`_ is an inexpensive single-board computer that contains four 10Arm Cortex-A53 cores. 11 12The following instructions explain how to use this port of the TF-A with the 13default distribution of `Raspbian`_ because that's the distribution officially 14supported by the Raspberry Pi Foundation. At the moment of writing this, the 15officially supported kernel is a AArch32 kernel. This doesn't mean that this 16port of TF-A can't boot a AArch64 kernel. The `Linux tree fork`_ maintained by 17the Foundation can be compiled for AArch64 by following the steps in 18`AArch64 kernel build instructions`_. 19 20**IMPORTANT NOTE**: This port isn't secure. All of the memory used is DRAM, 21which is available from both the Non-secure and Secure worlds. This port 22shouldn't be considered more than a prototype to play with and implement 23elements like PSCI to support the Linux kernel. 24 25Design 26------ 27 28The SoC used by the Raspberry Pi 3 is the Broadcom BCM2837. It is a SoC with a 29VideoCore IV that acts as primary processor (and loads everything from the SD 30card) and is located between all Arm cores and the DRAM. Check the `Raspberry Pi 313 documentation`_ for more information. 32 33This explains why it is possible to change the execution state (AArch64/AArch32) 34depending on a few files on the SD card. We only care about the cases in which 35the cores boot in AArch64 mode. 36 37The rules are simple: 38 39- If a file called ``kernel8.img`` is located on the ``boot`` partition of the 40 SD card, it will load it and execute in EL2 in AArch64. Basically, it executes 41 a `default AArch64 stub`_ at address **0x0** that jumps to the kernel. 42 43- If there is also a file called ``armstub8.bin``, it will load it at address 44 **0x0** (instead of the default stub) and execute it in EL3 in AArch64. All 45 the cores are powered on at the same time and start at address **0x0**. 46 47This means that we can use the default AArch32 kernel provided in the official 48`Raspbian`_ distribution by renaming it to ``kernel8.img``, while TF-A and 49anything else we need is in ``armstub8.bin``. This way we can forget about the 50default bootstrap code. When using a AArch64 kernel, it is only needed to make 51sure that the name on the SD card is ``kernel8.img``. 52 53Ideally, we want to load the kernel and have all cores available, which means 54that we need to make the secondary cores work in the way the kernel expects, as 55explained in `Secondary cores`_. In practice, a small bootstrap is needed 56between TF-A and the kernel. 57 58To get the most out of a AArch32 kernel, we want to boot it in Hypervisor mode 59in AArch32. This means that BL33 can't be in EL2 in AArch64 mode. The 60architecture specifies that AArch32 Hypervisor mode isn't present when AArch64 61is used for EL2. When using a AArch64 kernel, it should simply start in EL2. 62 63Placement of images 64~~~~~~~~~~~~~~~~~~~ 65 66The file ``armstub8.bin`` contains BL1 and the FIP. It is needed to add padding 67between them so that the addresses they are loaded to match the ones specified 68when compiling TF-A. This is done automatically by the build system. 69 70The device tree block is loaded by the VideoCore loader from an appropriate 71file, but we can specify the address it is loaded to in ``config.txt``. 72 73The file ``kernel8.img`` contains a kernel image that is loaded to the address 74specified in ``config.txt``. The `Linux kernel tree`_ has information about how 75a AArch32 Linux kernel image is loaded in ``Documentation/arm/Booting``: 76 77:: 78 79 The zImage may also be placed in system RAM and called there. The 80 kernel should be placed in the first 128MiB of RAM. It is recommended 81 that it is loaded above 32MiB in order to avoid the need to relocate 82 prior to decompression, which will make the boot process slightly 83 faster. 84 85There are no similar restrictions for AArch64 kernels, as specified in the file 86``Documentation/arm64/booting.txt``. 87 88This means that we need to avoid the first 128 MiB of RAM when placing the 89TF-A images (and specially the first 32 MiB, as they are directly used to 90place the uncompressed AArch32 kernel image. This way, both AArch32 and 91AArch64 kernels can be placed at the same address. 92 93In the end, the images look like the following diagram when placed in memory. 94All addresses are Physical Addresses from the point of view of the Arm cores. 95Again, note that this is all just part of the same DRAM that goes from 96**0x00000000** to **0x3F000000**, it just has different names to simulate a real 97secure platform! 98 99:: 100 101 0x00000000 +-----------------+ 102 | ROM | BL1 103 0x00020000 +-----------------+ 104 | FIP | 105 0x00200000 +-----------------+ 106 | | 107 | ... | 108 | | 109 0x01000000 +-----------------+ 110 | DTB | (Loaded by the VideoCore) 111 +-----------------+ 112 | | 113 | ... | 114 | | 115 0x02000000 +-----------------+ 116 | Kernel | (Loaded by the VideoCore) 117 +-----------------+ 118 | | 119 | ... | 120 | | 121 0x10000000 +-----------------+ 122 | Secure SRAM | BL2, BL31 123 0x10100000 +-----------------+ 124 | Secure DRAM | BL32 (Secure payload) 125 0x11000000 +-----------------+ 126 | Non-secure DRAM | BL33 127 +-----------------+ 128 | | 129 | ... | 130 | | 131 0x3F000000 +-----------------+ 132 | I/O | 133 0x40000000 +-----------------+ 134 135The area between **0x10000000** and **0x11000000** has to be manually protected 136so that the kernel doesn't use it. The current port tries to modify the live DTB 137to add a memreserve region that reserves the previously mentioned area. 138 139If this is not possible, the user may manually add ``memmap=16M$256M`` to the 140command line passed to the kernel in ``cmdline.txt``. See the `Setup SD card`_ 141instructions to see how to do it. This system is strongly discouraged. 142 143The last 16 MiB of DRAM can only be accessed by the VideoCore, that has 144different mappings than the Arm cores in which the I/O addresses don't overlap 145the DRAM. The memory reserved to be used by the VideoCore is always placed at 146the end of the DRAM, so this space isn't wasted. 147 148Considering the 128 MiB allocated to the GPU and the 16 MiB allocated for 149TF-A, there are 880 MiB available for Linux. 150 151Boot sequence 152~~~~~~~~~~~~~ 153 154The boot sequence of TF-A is the usual one except when booting an AArch32 155kernel. In that case, BL33 is booted in AArch32 Hypervisor mode so that it 156can jump to the kernel in the same mode and let it take over that privilege 157level. If BL33 was running in EL2 in AArch64 (as in the default bootflow of 158TF-A) it could only jump to the kernel in AArch32 in Supervisor mode. 159 160The `Linux kernel tree`_ has instructions on how to jump to the Linux kernel 161in ``Documentation/arm/Booting`` and ``Documentation/arm64/booting.txt``. The 162bootstrap should take care of this. 163 164This port support a direct boot of the Linux kernel from the firmware (as a BL33 165image). Alternatively, U-Boot or other bootloaders may be used. 166 167Secondary cores 168~~~~~~~~~~~~~~~ 169 170This port of the Trusted Firmware-A supports ``PSCI_CPU_ON``, 171`PSCI_SYSTEM_RESET`` and ``PSCI_SYSTEM_OFF``. The last one doesn't really turn 172the system off, it simply reboots it and asks the VideoCore firmware to keep it 173in a low power mode permanently. 174 175The kernel used by `Raspbian`_ doesn't have support for PSCI, so it is needed to 176use mailboxes to trap the secondary cores until they are ready to jump to the 177kernel. This mailbox is located at a different address in the AArch32 default 178kernel than in the AArch64 kernel. 179 180Kernels with PSCI support can use the PSCI calls instead for a cleaner boot. 181 182Also, this port of TF-A has another Trusted Mailbox in Shared BL RAM. During 183cold boot, all secondary cores wait in a loop until they are given given an 184address to jump to in this Mailbox (``bl31_warm_entrypoint``). 185 186Once BL31 has finished and the primary core has jumped to the BL33 payload, it 187has to call ``PSCI_CPU_ON`` to release the secondary CPUs from the wait loop. 188The payload then makes them wait in another waitloop listening from messages 189from the kernel. When the primary CPU jumps into the kernel, it will send an 190address to the mailbox so that the secondary CPUs jump to it and are recognised 191by the kernel. 192 193Build Instructions 194------------------ 195 196To boot a AArch64 kernel, only the AArch64 toolchain is required. 197 198To boot a AArch32 kernel, both AArch64 and AArch32 toolchains are required. The 199AArch32 toolchain is needed for the AArch32 bootstrap needed to load a 32-bit 200kernel. 201 202The build system concatenates BL1 and the FIP so that the addresses match the 203ones in the memory map. The resulting file is ``armstub8.bin``, located in the 204build folder (e.g. ``build/rpi3/debug/armstub8.bin``). To know how to use this 205file, follow the instructions in `Setup SD card`_. 206 207The following build options are supported: 208 209- ``RPI3_BL33_IN_AARCH32``: This port can load a AArch64 or AArch32 BL33 image. 210 By default this option is 0, which means that TF-A will jump to BL33 in EL2 211 in AArch64 mode. If set to 1, it will jump to BL33 in Hypervisor in AArch32 212 mode. 213 214- ``PRELOADED_BL33_BASE``: Used to specify the address of a BL33 binary that has 215 been preloaded by any other system than using the firmware. ``BL33`` isn't 216 needed in the build command line if this option is used. Specially useful 217 because the file ``kernel8.img`` can be loaded anywhere by modifying the file 218 ``config.txt``. It doesn't have to contain a kernel, it could have any 219 arbitrary payload. 220 221- ``RPI3_DIRECT_LINUX_BOOT``: Disabled by default. Set to 1 to enable the direct 222 boot of the Linux kernel from the firmware. Option ``RPI3_PRELOADED_DTB_BASE`` 223 is mandatory when the direct Linux kernel boot is used. Options 224 ``PRELOADED_BL33_BASE`` will most likely be needed as well because it is 225 unlikely that the kernel image will fit in the space reserved for BL33 images. 226 This option can be combined with ``RPI3_BL33_IN_AARCH32`` in order to boot a 227 32-bit kernel. The only thing this option does is to set the arguments in 228 registers x0-x3 or r0-r2 as expected by the kernel. 229 230- ``RPI3_PRELOADED_DTB_BASE``: Auxiliary build option needed when using 231 ``RPI3_DIRECT_LINUX_BOOT=1``. This option allows to specify the location of a 232 DTB in memory. 233 234- ``BL32``: This port can load and run OP-TEE. The OP-TEE image is optional. 235 Please use the code from `here <https://github.com/OP-TEE/optee_os>`__. 236 Build the Trusted Firmware with option ``BL32=tee-header_v2.bin 237 BL32_EXTRA1=tee-pager_v2.bin BL32_EXTRA2=tee-pageable_v2.bin`` 238 to put the binaries into the FIP. 239 240 Note: If OP-TEE is used it may be needed to add the following options to the 241 Linux command line so that the USB driver doesn't use FIQs: 242 ``dwc_otg.fiq_enable=0 dwc_otg.fiq_fsm_enable=0 dwc_otg.nak_holdoff=0``. 243 This will unfortunately reduce the performance of the USB driver. It is needed 244 when using Raspbian, for example. 245 246- ``TRUSTED_BOARD_BOOT``: This port supports TBB. Set this option to 1 to enable 247 it. In order to use TBB, you might want to set ``GENERATE_COT=1`` to let the 248 contents of the FIP automatically signed by the build process. The ROT key 249 will be generated and output to ``rot_key.pem`` in the build directory. It is 250 able to set ROT_KEY to your own key in PEM format. Also in order to build, 251 you need to clone mbed TLS from `here <https://github.com/ARMmbed/mbedtls>`__. 252 ``MBEDTLS_DIR`` must point at the mbed TLS source directory. 253 254- ``ENABLE_STACK_PROTECTOR``: Disabled by default. It uses the hardware RNG of 255 the board. 256 257The following is not currently supported: 258 259- AArch32 for TF-A itself. 260 261- ``EL3_PAYLOAD_BASE``: The reason is that you can already load anything to any 262 address by changing the file ``armstub8.bin``, so there's no point in using 263 TF-A in this case. 264 265- ``MULTI_CONSOLE_API=0``: The multi console API must be enabled. Note that the 266 crash console uses the internal 16550 driver functions directly in order to be 267 able to print error messages during early crashes before setting up the 268 multi console API. 269 270Building the firmware for kernels that don't support PSCI 271~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 272 273This is the case for the 32-bit image of Raspbian, for example. 64-bit kernels 274always support PSCI, but they may not know that the system understands PSCI due 275to an incorrect DTB file. 276 277First, clone and compile the 32-bit version of the `Raspberry Pi 3 TF-A 278bootstrap`_. Choose the one needed for the architecture of your kernel. 279 280Then compile TF-A. For a 32-bit kernel, use the following command line: 281 282.. code:: shell 283 284 CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \ 285 RPI3_BL33_IN_AARCH32=1 \ 286 BL33=../rpi3-arm-tf-bootstrap/aarch32/el2-bootstrap.bin 287 288For a 64-bit kernel, use this other command line: 289 290.. code:: shell 291 292 CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \ 293 BL33=../rpi3-arm-tf-bootstrap/aarch64/el2-bootstrap.bin 294 295However, enabling PSCI support in a 64-bit kernel is really easy. In the 296repository `Raspberry Pi 3 TF-A bootstrap`_ there is a patch that can be applied 297to the Linux kernel tree maintained by the Raspberry Pi foundation. It modifes 298the DTS to tell the kernel to use PSCI. Once this patch is applied, follow the 299instructions in `AArch64 kernel build instructions`_ to get a working 64-bit 300kernel image and supporting files. 301 302Building the firmware for kernels that support PSCI 303~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 304 305For a 64-bit kernel: 306 307.. code:: shell 308 309 CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \ 310 PRELOADED_BL33_BASE=0x02000000 \ 311 RPI3_PRELOADED_DTB_BASE=0x01000000 \ 312 RPI3_DIRECT_LINUX_BOOT=1 313 314For a 32-bit kernel: 315 316.. code:: shell 317 318 CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \ 319 PRELOADED_BL33_BASE=0x02000000 \ 320 RPI3_PRELOADED_DTB_BASE=0x01000000 \ 321 RPI3_DIRECT_LINUX_BOOT=1 \ 322 RPI3_BL33_IN_AARCH32=1 323 324AArch64 kernel build instructions 325--------------------------------- 326 327The following instructions show how to install and run a AArch64 kernel by 328using a SD card with the default `Raspbian`_ install as base. Skip them if you 329want to use the default 32-bit kernel. 330 331Note that this system won't be fully 64-bit because all the tools in the 332filesystem are 32-bit binaries, but it's a quick way to get it working, and it 333allows the user to run 64-bit binaries in addition to 32-bit binaries. 334 3351. Clone the `Linux tree fork`_ maintained by the Raspberry Pi Foundation. To 336 speed things up, do a shallow clone of the desired branch. 337 338.. code:: shell 339 340 git clone --depth=1 -b rpi-4.18.y https://github.com/raspberrypi/linux 341 cd linux 342 3432. Configure and compile the kernel. Adapt the number after ``-j`` so that it is 344 1.5 times the number of CPUs in your computer. This may take some time to 345 finish. 346 347.. code:: shell 348 349 make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig 350 make -j 6 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- 351 3523. Copy the kernel image and the device tree to the SD card. Replace the path 353 by the corresponding path in your computers to the ``boot`` partition of the 354 SD card. 355 356.. code:: shell 357 358 cp arch/arm64/boot/Image /path/to/boot/kernel8.img 359 cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b.dtb /path/to/boot/ 360 cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b-plus.dtb /path/to/boot/ 361 3624. Install the kernel modules. Replace the path by the corresponding path to the 363 filesystem partition of the SD card on your computer. 364 365.. code:: shell 366 367 make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \ 368 INSTALL_MOD_PATH=/path/to/filesystem modules_install 369 3705. Follow the instructions in `Setup SD card`_ except for the step of renaming 371 the existing ``kernel7.img`` (we have already copied a AArch64 kernel). 372 373Setup SD card 374------------- 375 376The instructions assume that you have an SD card with a fresh install of 377`Raspbian`_ (or that, at least, the ``boot`` partition is untouched, or nearly 378untouched). They have been tested with the image available in 2018-03-13. 379 3801. Insert the SD card and open the ``boot`` partition. 381 3822. Rename ``kernel7.img`` to ``kernel8.img``. This tricks the VideoCore 383 bootloader into booting the Arm cores in AArch64 mode, like TF-A needs, 384 even though the kernel is not compiled for AArch64. 385 3863. Copy ``armstub8.bin`` here. When ``kernel8.img`` is available, The VideoCore 387 bootloader will look for a file called ``armstub8.bin`` and load it at 388 address **0x0** instead of a predefined one. 389 3904. To enable the serial port "Mini UART" in Linux, open ``cmdline.txt`` and add 391 ``console=serial0,115200 console=tty1``. 392 3935. Open ``config.txt`` and add the following lines at the end (``enable_uart=1`` 394 is only needed to enable debugging through the Mini UART): 395 396:: 397 398 enable_uart=1 399 kernel_address=0x02000000 400 device_tree_address=0x01000000 401 402If you connect a serial cable to the Mini UART and your computer, and connect 403to it (for example, with ``screen /dev/ttyUSB0 115200``) you should see some 404text. In the case of an AArch32 kernel, you should see something like this: 405 406:: 407 408 NOTICE: Booting Trusted Firmware 409 NOTICE: BL1: v1.4(release):v1.4-329-g61e94684-dirty 410 NOTICE: BL1: Built : 00:09:25, Nov 6 2017 411 NOTICE: BL1: Booting BL2 412 NOTICE: BL2: v1.4(release):v1.4-329-g61e94684-dirty 413 NOTICE: BL2: Built : 00:09:25, Nov 6 2017 414 NOTICE: BL1: Booting BL31 415 NOTICE: BL31: v1.4(release):v1.4-329-g61e94684-dirty 416 NOTICE: BL31: Built : 00:09:25, Nov 6 2017 417 [ 0.266484] bcm2835-aux-uart 3f215040.serial: could not get clk: -517 418 419 Raspbian GNU/Linux 9 raspberrypi ttyS0 420 raspberrypi login: 421 422Just enter your credentials, everything should work as expected. Note that the 423HDMI output won't show any text during boot. 424 425.. _default Arm stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub7.S 426.. _default AArch64 stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub8.S 427.. _Linux kernel tree: https://github.com/torvalds/linux 428.. _Linux tree fork: https://github.com/raspberrypi/linux 429.. _Raspberry Pi 3: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/ 430.. _Raspberry Pi 3 TF-A bootstrap: https://github.com/AntonioND/rpi3-arm-tf-bootstrap 431.. _Raspberry Pi 3 documentation: https://www.raspberrypi.org/documentation/ 432.. _Raspbian: https://www.raspberrypi.org/downloads/raspbian/ 433