blob: 219faaff5be2653bc888bd61a0e5e0b9c54d03bb [file] [log] [blame]
Antonio Nino Diaz8869b482017-12-01 11:11:26 +00001Arm Trusted Firmware 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
10Cortex-A53 cores, which makes it possible to have a port of the Arm Trusted
11Firmware.
12
13The following instructions explain how to use this port of the Trusted Firmware
14with the default distribution of `Raspbian`_ because that's the distribution
15officially supported by the Raspberry Pi Foundation. At the moment of writing
16this, the officially supported kernel is a AArch32 kernel. This doesn't mean
17that this port of the Trusted Firmware can't boot a AArch64 kernel. The `Linux
18tree fork`_ maintained by the Foundation can be compiled for AArch64 by
19following the steps in `AArch64 kernel build instructions`_.
20
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
49`Raspbian`_ distribution by renaming it to ``kernel8.img``, while the Trusted
50Firmware and anything else we need is in ``armstub8.bin``. This way we can
51forget about the default bootstrap code. When using a AArch64 kernel, it is only
52needed to make sure that the name on the SD card is ``kernel8.img``.
53
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
57between the Trusted Firmware and the kernel.
58
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
69when compiling the Trusted Firmware.
70
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
90Trusted Firmware images (and specially the first 32 MiB, as they are directly
91used to place the uncompressed AArch32 kernel image. This way, both AArch32 and
92AArch64 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
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
136The area between **0x10000000** and **0x11000000** has to be protected so that
137the kernel doesn't use it. That is done by adding ``memmap=256M$16M`` to the
138command 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
146Considering the 128 MiB allocated to the GPU and the 16 MiB allocated for the
147Trusted Firmware, there are 880 MiB available for Linux.
148
149Boot sequence
150~~~~~~~~~~~~~
151
152The boot sequence of the Trusted Firmware is the usual one except when booting
153a AArch32 kernel. In that case, BL33 is booted in AArch32 Hypervisor mode so
154that it can jump to the kernel in the same mode and let it take over that
155privilege level. If BL33 was running in EL2 in AArch64 (as in the default
156bootflow of the Trusted Firmware) it could only jump to the kernel in AArch32 in
157Supervisor mode.
158
159The `Linux kernel tree`_ has instructions on how to jump to the Linux kernel
160in ``Documentation/arm/Booting`` and ``Documentation/arm64/booting.txt``. The
161bootstrap should take care of this.
162
163Secondary cores
164~~~~~~~~~~~~~~~
165
166The kernel used by `Raspbian`_ doesn't have support for PSCI, so it is needed to
167use mailboxes to trap the secondary cores until they are ready to jump to the
168kernel. This mailbox is located at a different address in the AArch32 default
169kernel than in the AArch64 kernel.
170
171Also, this port of the Trusted Firmware has another Trusted Mailbox in Shared BL
172RAM. During cold boot, all secondary cores wait in a loop until they are given
173given an address to jump to in this Mailbox (``bl31_warm_entrypoint``).
174
175Once BL31 has finished and the primary core has jumped to the BL33 payload, it
176has to call ``PSCI_CPU_ON`` to release the secondary CPUs from the wait loop.
177The payload then makes them wait in another waitloop listening from messages
178from the kernel. When the primary CPU jumps into the kernel, it will send an
179address to the mailbox so that the secondary CPUs jump to it and are recognised
180by the kernel.
181
182Build Instructions
183------------------
184
185To boot a AArch64 kernel, only the AArch64 toolchain is required.
186
187To boot a AArch32 kernel, both AArch64 and AArch32 toolchains are required. The
188AArch32 toolchain is needed for the AArch32 bootstrap needed to load a 32-bit
189kernel.
190
191First, clone and compile `Raspberry Pi 3 Arm Trusted Firmware bootstrap`_.
192Choose the one needed for the architecture of your kernel.
193
194Then compile the Arm Trusted Firmware. For a AArch32 kernel, use the following
195command line:
196
197.. code:: shell
198
199 CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \
200 RPI3_BL33_IN_AARCH32=1 \
201 BL33=../rpi3-arm-tf-bootstrap/aarch32/el2-bootstrap.bin \
202 all fip
203
204For a AArch64 kernel, use this other command line:
205
206.. code:: shell
207
208 CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rpi3 \
209 BL33=../rpi3-arm-tf-bootstrap/aarch64/el2-bootstrap.bin \
210 all fip
211
212Then, join BL1 and the FIP with the following instructions (replace ``release``
213by ``debug`` if you set the build option ``DEBUG=1``):
214
215.. code:: shell
216
217 cp build/rpi3/release/bl1.bin bl1.pad.bin
218 truncate --size=65536 bl1.pad.bin
219 cat bl1.pad.bin build/rpi3/release/fip.bin > armstub8.bin
220
221The resulting file, ``armstub8.bin``, contains BL1 and the FIP in the place they
222need to be for the Trusted Firmware to boot correctly. Now, follow the
223instructions in `Setup SD card`_.
224
225The following build options are supported:
226
227- ``PRELOADED_BL33_BASE``: Specially useful because the file ``kernel8.img`` can
228 be loaded anywhere by modifying the file ``config.txt``. It doesn't have to
229 contain a kernel, it could have any arbitrary payload.
230
231- ``RESET_TO_BL31``: Set to 1 by default. If using a 32-bit kernel like
232 `Raspbian`_, the space used by BL1 can overwritten by the kernel when it is
233 being loaded. Even when using a AArch64 kernel the region used by
234 BL1 isn't protected and the kernel could overwrite it. The space used by BL31
235 is reserved by the command line passed to the kernel.
236
237- ``RPI3_BL33_IN_AARCH32``: This port can load a AArch64 or AArch32 BL33 image.
238 By default this option is 0, which means that the Trusted Firmware will jump
239 to BL33 in EL2 in AArch64 mode. If set to 1, it will jump to BL33 in
240 Hypervisor in AArch32 mode.
241
242The following is not currently supported:
243
244- AArch32 for the Trusted Firmware itself.
245
246- ``EL3_PAYLOAD_BASE``: The reason is that you can already load anything to any
247 address by changing the file ``armstub8.bin``, so there's no point in using
248 the Trusted Firmware in this case.
249
250- ``LOAD_IMAGE_V2=0``: Only version 2 is supported.
251
252AArch64 kernel build instructions
253---------------------------------
254
255The following instructions show how to install and run a AArch64 kernel by
256using a SD card with the default `Raspbian`_ install as base. Skip them if you
257want to use the default 32-bit kernel.
258
259Note that this system won't be fully 64-bit because all the tools in the
260filesystem are 32-bit binaries, but it's a quick way to get it working, and it
261allows the user to run 64-bit binaries in addition to 32-bit binaries.
262
2631. Clone the `Linux tree fork`_ maintained by the Raspberry Pi Foundation. To
264 speed things up, do a shallow clone of the desired branch.
265
266.. code:: shell
267
268 git clone --depth=1 -b rpi-4.14.y https://github.com/raspberrypi/linux
269 cd linux
270
2712. Configure and compile the kernel. Adapt the number after ``-j`` so that it is
272 1.5 times the number of CPUs in your computer. This may take some time to
273 finish.
274
275.. code:: shell
276
277 make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig
278 make -j 6 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
279
2803. Copy the kernel image and the device tree to the SD card. Replace the path
281 by the corresponding path in your computers to the ``boot`` partition of the
282 SD card.
283
284.. code:: shell
285
286 cp arch/arm64/boot/Image /path/to/boot/kernel8.img
287 cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b.dtb /path/to/boot/
288
2894. Install the kernel modules. Replace the path by the corresponding path to the
290 filesystem partition of the SD card on your computer.
291
292.. code:: shell
293
294 make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \
295 INSTALL_MOD_PATH=/path/to/filesystem modules_install
296
2975. Follow the instructions in `Setup SD card`_ except for the step of renaming
298 the existing ``kernel7.img`` (we have already copied a AArch64 kernel).
299
300Setup SD card
301-------------
302
303The instructions assume that you have an SD card with a fresh install of
304`Raspbian`_ (or that, at least, the ``boot`` partition is untouched, or nearly
305untouched). They have been tested with the image available in 2017-09-07.
306
3071. Insert the SD card and open the ``boot`` partition.
308
3092. Rename ``kernel7.img`` to ``kernel8.img``. This tricks the VideoCore
310 bootloader into booting the Arm cores in AArch64 mode, like the Trusted
311 Firmware needs, even though the kernel is not compiled for AArch64.
312
3133. Copy ``armstub8.bin`` here. When ``kernel8.img`` is available, The VideoCore
314 bootloader will look for a file called ``armstub8.bin`` and load it at
315 address **0x0** instead of a predefined one.
316
3174. Open ``cmdline.txt`` and add ``memmap=256M$16M`` to prevent the kernel from
318 using the memory needed by the Trusted Firmware. If you want to enable the
319 serial port "Mini UART", make sure that this file also contains
320 ``console=serial0,115200 console=tty1``.
321
322 Note that the 16 MiB reserved this way won't be available for Linux, the same
323 way as the memory reserved in DRAM for the GPU isn't available.
324
3255. Open ``config.txt`` and add the following lines at the end (``enable_uart=1``
326 is only needed to enable debugging through the Mini UART):
327
328::
329
330 enable_uart=1
331 kernel_address=0x01000000
332 device_tree_address=0x02000000
333
334If you connect a serial cable to the Mini UART and your computer, and connect
335to it (for example, with ``screen /dev/ttyUSB0 115200``) you should see some
336text. In the case of an AArch32 kernel, you should see something like this:
337
338::
339
340 NOTICE: Booting Trusted Firmware
341 NOTICE: BL1: v1.4(release):v1.4-329-g61e94684-dirty
342 NOTICE: BL1: Built : 00:09:25, Nov 6 2017
343 NOTICE: BL1: Booting BL2
344 NOTICE: BL2: v1.4(release):v1.4-329-g61e94684-dirty
345 NOTICE: BL2: Built : 00:09:25, Nov 6 2017
346 NOTICE: BL1: Booting BL31
347 NOTICE: BL31: v1.4(release):v1.4-329-g61e94684-dirty
348 NOTICE: BL31: Built : 00:09:25, Nov 6 2017
349 [ 0.266484] bcm2835-aux-uart 3f215040.serial: could not get clk: -517
350
351 Raspbian GNU/Linux 9 raspberrypi ttyS0
352 raspberrypi login:
353
354Just enter your credentials, everything should work as expected. Note that the
355HDMI output won't show any text during boot.
356
357.. _default Arm stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub7.S
358.. _default AArch64 stub: https://github.com/raspberrypi/tools/blob/master/armstubs/armstub8.S
359.. _Linux kernel tree: https://github.com/torvalds/linux
360.. _Linux tree fork: https://github.com/raspberrypi/linux
361.. _Raspberry Pi 3: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
362.. _Raspberry Pi 3 Arm Trusted Firmware bootstrap: https://github.com/AntonioND/rpi3-arm-tf-bootstrap
363.. _Raspberry Pi 3 documentation: https://www.raspberrypi.org/documentation/
364.. _Raspbian: https://www.raspberrypi.org/downloads/raspbian/