rpi3.rst 20 KB

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