blob: 0ba564d81297c741c589ca669c0a2015a9ac2626 [file] [log] [blame]
Dan Handley610e7e12018-03-01 18:44:00 +00001Trusted Firmware-A for Raspberry Pi 3
2=====================================
Antonio Nino Diaz8869b482017-12-01 11:11:26 +00003
4.. section-numbering::
5 :suffix: .
6
7.. contents::
8
9The `Raspberry Pi 3`_ is an inexpensive single-board computer that contains four
Dan Handley610e7e12018-03-01 18:44:00 +000010Arm Cortex-A53 cores, which makes it possible to have a port of Trusted
11Firmware-A (TF-A).
Antonio Nino Diaz8869b482017-12-01 11:11:26 +000012
Dan Handley610e7e12018-03-01 18:44:00 +000013The following instructions explain how to use this port of the TF-A with the
14default distribution of `Raspbian`_ because that's the distribution officially
15supported by the Raspberry Pi Foundation. At the moment of writing this, the
16officially supported kernel is a AArch32 kernel. This doesn't mean that this
17port of TF-A can't boot a AArch64 kernel. The `Linux tree fork`_ maintained by
18the Foundation can be compiled for AArch64 by following the steps in
19`AArch64 kernel build instructions`_.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +000020
21**IMPORTANT NOTE**: This port isn't secure. All of the memory used is DRAM,
22which is available from both the Non-secure and Secure worlds. This port
23shouldn't be considered more than a prototype to play with and implement
24elements like PSCI to support the Linux kernel.
25
26Design
27------
28
29The SoC used by the Raspberry Pi 3 is the Broadcom BCM2837. It is a SoC with a
30VideoCore IV that acts as primary processor (and loads everything from the SD
31card) and is located between all Arm cores and the DRAM. Check the `Raspberry Pi
323 documentation`_ for more information.
33
34This explains why it is possible to change the execution state (AArch64/AArch32)
35depending on a few files on the SD card. We only care about the cases in which
36the cores boot in AArch64 mode.
37
38The 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
48This means that we can use the default AArch32 kernel provided in the official
Dan Handley610e7e12018-03-01 18:44:00 +000049`Raspbian`_ distribution by renaming it to ``kernel8.img``, while TF-A and
50anything else we need is in ``armstub8.bin``. This way we can forget about the
51default bootstrap code. When using a AArch64 kernel, it is only needed to make
52sure that the name on the SD card is ``kernel8.img``.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +000053
54Ideally, we want to load the kernel and have all cores available, which means
55that we need to make the secondary cores work in the way the kernel expects, as
56explained in `Secondary cores`_. In practice, a small bootstrap is needed
Dan Handley610e7e12018-03-01 18:44:00 +000057between TF-A and the kernel.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +000058
59To get the most out of a AArch32 kernel, we want to boot it in Hypervisor mode
60in AArch32. This means that BL33 can't be in EL2 in AArch64 mode. The
61architecture specifies that AArch32 Hypervisor mode isn't present when AArch64
62is used for EL2. When using a AArch64 kernel, it should simply start in EL2.
63
64Placement of images
65~~~~~~~~~~~~~~~~~~~
66
67The file ``armstub8.bin`` contains BL1 and the FIP. It is needed to add padding
68between them so that the addresses they are loaded to match the ones specified
Dan Handley610e7e12018-03-01 18:44:00 +000069when compiling TF-A.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +000070
71The device tree block is loaded by the VideoCore loader from an appropriate
72file, but we can specify the address it is loaded to in ``config.txt``.
73
74The file ``kernel8.img`` contains a kernel image that is loaded to the address
75specified in ``config.txt``. The `Linux kernel tree`_ has information about how
76a 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
86There are no similar restrictions for AArch64 kernels, as specified in the file
87``Documentation/arm64/booting.txt``.
88
89This means that we need to avoid the first 128 MiB of RAM when placing the
Dan Handley610e7e12018-03-01 18:44:00 +000090TF-A images (and specially the first 32 MiB, as they are directly used to
91place the uncompressed AArch32 kernel image. This way, both AArch32 and
Antonio Nino Diaz8869b482017-12-01 11:11:26 +000092AArch64 kernels can be placed at the same address.
93
94In the end, the images look like the following diagram when placed in memory.
95All addresses are Physical Addresses from the point of view of the Arm cores.
96Again, 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
98secure platform!
99
100::
101
102 0x00000000 +-----------------+
103 | ROM | BL1
Ying-Chun Liu (PaulLiu)51b146f2018-07-04 02:32:27 +0800104 0x00020000 +-----------------+
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000105 | 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 +-----------------+
Ying-Chun Liu (PaulLiu)64c6ff12018-06-13 20:53:08 +0800125 | Secure DRAM | BL32 (Secure payload)
Ying-Chun Liu (PaulLiu)78d42132018-07-11 23:32:59 +0800126 0x10C00000 +-----------------+
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000127 | Non-secure DRAM | BL33
128 0x11000000 +-----------------+
129 | |
130 | ... |
131 | |
132 0x3F000000 +-----------------+
133 | I/O |
134 0x40000000 +-----------------+
135
136The area between **0x10000000** and **0x11000000** has to be protected so that
Antonio Nino Diaz73ecb9f2018-06-13 13:34:46 +0100137the kernel doesn't use it. That is done by adding ``memmap=16M$256M`` to the
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000138command line passed to the kernel. See the `Setup SD card`_ instructions to see
139how to do it.
140
141The last 16 MiB of DRAM can only be accessed by the VideoCore, that has
142different mappings than the Arm cores in which the I/O addresses don't overlap
143the DRAM. The memory reserved to be used by the VideoCore is always placed at
144the end of the DRAM, so this space isn't wasted.
145
Dan Handley610e7e12018-03-01 18:44:00 +0000146Considering the 128 MiB allocated to the GPU and the 16 MiB allocated for
147TF-A, there are 880 MiB available for Linux.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000148
149Boot sequence
150~~~~~~~~~~~~~
151
Dan Handley610e7e12018-03-01 18:44:00 +0000152The boot sequence of TF-A is the usual one except when booting an AArch32
153kernel. In that case, BL33 is booted in AArch32 Hypervisor mode so that it
154can jump to the kernel in the same mode and let it take over that privilege
155level. If BL33 was running in EL2 in AArch64 (as in the default bootflow of
156TF-A) it could only jump to the kernel in AArch32 in Supervisor mode.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000157
158The `Linux kernel tree`_ has instructions on how to jump to the Linux kernel
159in ``Documentation/arm/Booting`` and ``Documentation/arm64/booting.txt``. The
160bootstrap should take care of this.
161
162Secondary cores
163~~~~~~~~~~~~~~~
164
165The kernel used by `Raspbian`_ doesn't have support for PSCI, so it is needed to
166use mailboxes to trap the secondary cores until they are ready to jump to the
167kernel. This mailbox is located at a different address in the AArch32 default
168kernel than in the AArch64 kernel.
169
Dan Handley610e7e12018-03-01 18:44:00 +0000170Also, this port of TF-A has another Trusted Mailbox in Shared BL RAM. During
171cold boot, all secondary cores wait in a loop until they are given given an
172address to jump to in this Mailbox (``bl31_warm_entrypoint``).
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000173
174Once BL31 has finished and the primary core has jumped to the BL33 payload, it
175has to call ``PSCI_CPU_ON`` to release the secondary CPUs from the wait loop.
176The payload then makes them wait in another waitloop listening from messages
177from the kernel. When the primary CPU jumps into the kernel, it will send an
178address to the mailbox so that the secondary CPUs jump to it and are recognised
179by the kernel.
180
181Build Instructions
182------------------
183
184To boot a AArch64 kernel, only the AArch64 toolchain is required.
185
186To boot a AArch32 kernel, both AArch64 and AArch32 toolchains are required. The
187AArch32 toolchain is needed for the AArch32 bootstrap needed to load a 32-bit
188kernel.
189
Dan Handley610e7e12018-03-01 18:44:00 +0000190First, clone and compile `Raspberry Pi 3 TF-A bootstrap`_. Choose the one
191needed for the architecture of your kernel.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000192
Dan Handley610e7e12018-03-01 18:44:00 +0000193Then compile TF-A. For a AArch32 kernel, use the following command line:
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000194
195.. code:: shell
196
197 CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \
198 RPI3_BL33_IN_AARCH32=1 \
Antonio Nino Diazdfe895d2018-07-12 08:58:38 +0100199 BL33=../rpi3-arm-tf-bootstrap/aarch32/el2-bootstrap.bin
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000200
201For a AArch64 kernel, use this other command line:
202
203.. code:: shell
204
205 CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \
Antonio Nino Diazdfe895d2018-07-12 08:58:38 +0100206 BL33=../rpi3-arm-tf-bootstrap/aarch64/el2-bootstrap.bin
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000207
Antonio Nino Diazdfe895d2018-07-12 08:58:38 +0100208The build system concatenates BL1 and the FIP so that the addresses match the
209ones in the memory map. The resulting file is ``armstub8.bin``, located in the
210build folder (e.g. ``build/rpi3/debug/armstub8.bin``). Now, follow the
211instructions in `Setup SD card`_.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000212
213The following build options are supported:
214
215- ``PRELOADED_BL33_BASE``: Specially useful because the file ``kernel8.img`` can
216 be loaded anywhere by modifying the file ``config.txt``. It doesn't have to
217 contain a kernel, it could have any arbitrary payload.
218
219- ``RESET_TO_BL31``: Set to 1 by default. If using a 32-bit kernel like
220 `Raspbian`_, the space used by BL1 can overwritten by the kernel when it is
221 being loaded. Even when using a AArch64 kernel the region used by
222 BL1 isn't protected and the kernel could overwrite it. The space used by BL31
223 is reserved by the command line passed to the kernel.
224
225- ``RPI3_BL33_IN_AARCH32``: This port can load a AArch64 or AArch32 BL33 image.
Dan Handley610e7e12018-03-01 18:44:00 +0000226 By default this option is 0, which means that TF-A will jump to BL33 in EL2
227 in AArch64 mode. If set to 1, it will jump to BL33 in Hypervisor in AArch32
228 mode.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000229
Ying-Chun Liu (PaulLiu)64c6ff12018-06-13 20:53:08 +0800230- ``BL32``: This port can load and run OP-TEE. The OP-TEE image is optional.
231 Please use the code from `here <https://github.com/OP-TEE/optee_os>`__.
232 Build the Trusted Firmware with option ``BL32=tee-header_v2.bin
233 BL32_EXTRA1=tee-pager_v2.bin BL32_EXTRA2=tee-pageable_v2.bin``
234 to put the binaries into the FIP.
235
Antonio Nino Diaz9abd78d2018-07-11 21:00:32 +0100236 Note: If OP-TEE is used it may be needed to add the following options to the
237 Linux command line so that the USB driver doesn't use FIQs:
238 ``dwc_otg.fiq_enable=0 dwc_otg.fiq_fsm_enable=0 dwc_otg.nak_holdoff=0``.
239 This will unfortunately reduce the performance of the USB driver. It is needed
240 when using Raspbian, for example.
241
Ying-Chun Liu (PaulLiu)4176e0f2018-07-05 14:55:21 +0800242- ``TRUSTED_BOARD_BOOT``: This port supports TBB. Set this option
243 ``TRUSTED_BOARD_BOOT=1`` to enable it. In order to use TBB, you might
244 want to set ``GENERATE_COT=1`` to let the contents of the FIP automatically
245 signed by the build process. The ROT key will be generated and output to
246 ``rot_key.pem`` in the build directory. It is able to set ROT_KEY to
247 your own key in PEM format.
248 Also in order to build, you need to clone mbedtls from
249 `here <https://github.com/ARMmbed/mbedtls>`__.
250 And set MBEDTLS_DIR to mbedtls source directory.
251
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000252The following is not currently supported:
253
Dan Handley610e7e12018-03-01 18:44:00 +0000254- AArch32 for TF-A itself.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000255
256- ``EL3_PAYLOAD_BASE``: The reason is that you can already load anything to any
257 address by changing the file ``armstub8.bin``, so there's no point in using
Dan Handley610e7e12018-03-01 18:44:00 +0000258 TF-A in this case.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000259
260- ``LOAD_IMAGE_V2=0``: Only version 2 is supported.
261
Antonio Nino Diaz1f470022018-03-27 09:39:47 +0100262- ``MULTI_CONSOLE_API=0``: The multi console API must be enabled. Note that the
263 crash console uses the internal 16550 driver functions directly in order to be
264 able to print error messages during early crashes before setting up the
265 multi console API.
266
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000267AArch64 kernel build instructions
268---------------------------------
269
270The following instructions show how to install and run a AArch64 kernel by
271using a SD card with the default `Raspbian`_ install as base. Skip them if you
272want to use the default 32-bit kernel.
273
274Note that this system won't be fully 64-bit because all the tools in the
275filesystem are 32-bit binaries, but it's a quick way to get it working, and it
276allows the user to run 64-bit binaries in addition to 32-bit binaries.
277
2781. Clone the `Linux tree fork`_ maintained by the Raspberry Pi Foundation. To
279 speed things up, do a shallow clone of the desired branch.
280
281.. code:: shell
282
283 git clone --depth=1 -b rpi-4.14.y https://github.com/raspberrypi/linux
284 cd linux
285
2862. Configure and compile the kernel. Adapt the number after ``-j`` so that it is
287 1.5 times the number of CPUs in your computer. This may take some time to
288 finish.
289
290.. code:: shell
291
292 make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig
293 make -j 6 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
294
2953. Copy the kernel image and the device tree to the SD card. Replace the path
296 by the corresponding path in your computers to the ``boot`` partition of the
297 SD card.
298
299.. code:: shell
300
301 cp arch/arm64/boot/Image /path/to/boot/kernel8.img
302 cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b.dtb /path/to/boot/
303
3044. Install the kernel modules. Replace the path by the corresponding path to the
305 filesystem partition of the SD card on your computer.
306
307.. code:: shell
308
309 make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \
310 INSTALL_MOD_PATH=/path/to/filesystem modules_install
311
3125. Follow the instructions in `Setup SD card`_ except for the step of renaming
313 the existing ``kernel7.img`` (we have already copied a AArch64 kernel).
314
315Setup SD card
316-------------
317
318The instructions assume that you have an SD card with a fresh install of
319`Raspbian`_ (or that, at least, the ``boot`` partition is untouched, or nearly
Antonio Nino Diaz1f470022018-03-27 09:39:47 +0100320untouched). They have been tested with the image available in 2018-03-13.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000321
3221. Insert the SD card and open the ``boot`` partition.
323
3242. Rename ``kernel7.img`` to ``kernel8.img``. This tricks the VideoCore
Dan Handley610e7e12018-03-01 18:44:00 +0000325 bootloader into booting the Arm cores in AArch64 mode, like TF-A needs,
326 even though the kernel is not compiled for AArch64.
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000327
3283. Copy ``armstub8.bin`` here. When ``kernel8.img`` is available, The VideoCore
329 bootloader will look for a file called ``armstub8.bin`` and load it at
330 address **0x0** instead of a predefined one.
331
Antonio Nino Diaz73ecb9f2018-06-13 13:34:46 +01003324. Open ``cmdline.txt`` and add ``memmap=16M$256M`` to prevent the kernel from
Dan Handley610e7e12018-03-01 18:44:00 +0000333 using the memory needed by TF-A. If you want to enable the serial port
334 "Mini UART", make sure that this file also contains
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000335 ``console=serial0,115200 console=tty1``.
336
337 Note that the 16 MiB reserved this way won't be available for Linux, the same
338 way as the memory reserved in DRAM for the GPU isn't available.
339
3405. Open ``config.txt`` and add the following lines at the end (``enable_uart=1``
341 is only needed to enable debugging through the Mini UART):
342
343::
344
345 enable_uart=1
346 kernel_address=0x01000000
347 device_tree_address=0x02000000
348
349If you connect a serial cable to the Mini UART and your computer, and connect
350to it (for example, with ``screen /dev/ttyUSB0 115200``) you should see some
351text. In the case of an AArch32 kernel, you should see something like this:
352
353::
354
355 NOTICE: Booting Trusted Firmware
356 NOTICE: BL1: v1.4(release):v1.4-329-g61e94684-dirty
357 NOTICE: BL1: Built : 00:09:25, Nov 6 2017
358 NOTICE: BL1: Booting BL2
359 NOTICE: BL2: v1.4(release):v1.4-329-g61e94684-dirty
360 NOTICE: BL2: Built : 00:09:25, Nov 6 2017
361 NOTICE: BL1: Booting BL31
362 NOTICE: BL31: v1.4(release):v1.4-329-g61e94684-dirty
363 NOTICE: BL31: Built : 00:09:25, Nov 6 2017
364 [ 0.266484] bcm2835-aux-uart 3f215040.serial: could not get clk: -517
365
366 Raspbian GNU/Linux 9 raspberrypi ttyS0
367 raspberrypi login:
368
369Just enter your credentials, everything should work as expected. Note that the
370HDMI output won't show any text during boot.
371
372.. _default Arm stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub7.S
373.. _default AArch64 stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub8.S
374.. _Linux kernel tree: https://github.com/torvalds/linux
375.. _Linux tree fork: https://github.com/raspberrypi/linux
376.. _Raspberry Pi 3: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
Dan Handley610e7e12018-03-01 18:44:00 +0000377.. _Raspberry Pi 3 TF-A bootstrap: https://github.com/AntonioND/rpi3-arm-tf-bootstrap
Antonio Nino Diaz8869b482017-12-01 11:11:26 +0000378.. _Raspberry Pi 3 documentation: https://www.raspberrypi.org/documentation/
379.. _Raspbian: https://www.raspberrypi.org/downloads/raspbian/