Trusted Firmware 是 Arm 公司針對 TrustZone 和 Platform Security Architecture 開發的基礎建設,本任務理解 Trusted Firmware-A (TF-A) 及探討 Linux 在 TF-A 的系統啟動流程分析。預計產出:
以 Arm Developer 網站為主要依據 (避免羅列 Arm, Linaro, Pengutronix 以外的技術文件,原始程式碼不在此限),搭配閱讀以下簡報 (和錄影):
解讀針對 Arm 處理器搭配 Trusted Firmware-A (TF-A),Linux 核心的啟動流程、如何確保 TF-A 的作用,並列舉關鍵程式碼及元件。
以 Raspberry Pi 4B 作為主要實驗平台,搭配硬體 JTAG probe 分析和追蹤,不僅列出關鍵程式碼,需要搭配實驗來確認關鍵行為。
相較於傳統硬碟 (HDD) 與固態硬碟 (SSD), Raspberry Pi 使用 SD card 作為儲存裝置,用以存放系統軟體,包含實驗所需的 bootloader、 firmware 以及作業系統。因此 SD card 需要先進行磁碟分割 (2 partitions),並將其格式化。
lsblk
):fdisk
分割出實驗所需的兩個磁區 (boot partition & rootfs partition):/boot
中的 pre-compiled binaries:/boot
中已編譯的檔案複製到 boot partition:u-boot
的資源:defconfig
,編譯後預期在 u-boot
工作目錄中得到二進制檔案 u-boot.bin
:u-boot.bin
複製到 boot partition:boot_cmd.txt
,並加入 boot commands:mkimage
,得到預期的 boot script,並將 boot.scr
複製到 boot partition:config.txt
,並加入設定:linux
的資源:board_defconfig
,編譯之後預期在 linux
工作目錄下得到核心映像檔 arch/arm64/boot/Image
:Image
& bcm2711-rpi-4-b.dtb
) 複製到 boot partition:Trusted Firmware-A (TF-A) 是基於 ARMv8-A 以及 ARMv7-A 架構下,對於 Secure world software 的參考實作,並且包含 例外層級 3 (EL3) 的 Secure monitor。
對照 TrustZone Architecture, Trusted Firmware-A 已發布的參考實作包含:
Reference of Figure 1: Trusted Firmware Deep Dive
依據 Arm 發布的 Firmware Design Document, TF-A 實作的啟動流程取決於執行狀態,在 AArch64 的架構下可以將啟動流程分為 5 個階段,按照其執行順序分別為:
Boot Loader stage | Execution |
---|---|
stage 1 (BL1) | AP Boot ROM |
stage 2 (BL2) | Trusted Boot Firmware |
stage 3-1 (BL31) | EL3 Runtime Firmware |
stage 3-2 (BL32) | Secure-EL1 Payload |
stage 3-3 (BL33) | Non-trusted Firmware |
其中 BL31 的映像檔將由 BL2 進行載入,然後 BL1 會將控制權移交給 BL31 ,並且 BL31 在 AArch64 架構下的特權為 EL3。此外 BL31 會被鏈結並載入到平台特定的 base address。由 BL31 所實作的功能包含:
Reference of Figure 2: ARM Trusted Firmware roadmap and progress
VERBOSE
(we can check logging macro in debug.h
),編譯後預期在 arm-trusted-firmware
工作目錄下得到二進制檔案 /build/rpi4/debug/bl31.bin
,並將 bl31.bin
複製到 boot partition。config.txt
加入以下設定:umount
兩個分區 (boot partition & rootfs partition),將 SD card 插回 Raspberry Pi 4 並接上 serial cable 後將其開機,透過 Host 的 serial console 來存取和操作 Raspberry Pi 4:screen
,預期看到 Raspberry Pi 4 從開機至啟動核心的 TF-A boot log 以及 kernel log:arg0
) 傳遞到下一個啟動階段,以實際編譯 /bl31/bl31_main.c
為例:/plat/arm/common/arm_bl2_setup.c
分析,BL1 透過 arg1
將 meminfo_t
結構的位址傳遞給 BL2:/plat/arm/common/arm_bl31_setup.c
(Line 29-30) 分析, BL2 會透過 arg0
將要執行的下一個 image 的列表傳遞給 EL3 Runtime software (BL31):Devices based on Cortex-A processors benefit from TrustZone technology, which provides infrastructure for security in the hardware. These devices feature secure and non-secure processor modes, with a hard boundary between secure and non-secure software, as well as restricted access to hardware resources from non-secure code.
Each bootloader image can be divided in 2 parts,
PROGBITS
sections andNOBITS
sections.
For BL31, a platform can specify an alternate location for
NOBITS
sections (other than immediately following PROGBITS sections) by settingSEPARATE_NOBITS_REGION
to 1 and definingBL31_NOBITS_BASE
andBL31_NOBITS_LIMIT
.
Reference of Figure 3: LCU14 500 ARM Trusted Firmware