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