OP-TEE: Môi trường & Gỡ lỗi (Phần 1)
Thiết lập môi trường OP-TEE trên QEMU ARM v8, cấu hình debug với GDB và phân tích quá trình boot ARM TrustZone.
1. Cấu hình môi trường
1.1. Thiết lập môi trường qemu_v8
Tham khảo: OP-TEE Prerequisites
Tải source code và build:
1
2
3
4
5
repo init -u https://github.com/OP-TEE/manifest.git -m qemu_v8.xml
repo sync -c -j8
cd build
make toolchains
make run
Chạy với phiên bản cụ thể:
1
make -f qemu_v8.mk run-only
1.2. Build với DEBUG mode
Tham khảo: OP-TEE Debug Guide
1
2
3
cd build
make DEBUG=1 -f qemu_v8.mk all
make DEBUG=1 -f qemu_v8.mk run-only
Sau khi chạy thành công, hệ thống sẽ hiển thị 2 terminal: một cho Secure World và một cho Normal World.
2. Gỡ lỗi với GDB
2.1. Kết nối GDB
Makefile đã cấu hình sẵn tùy chọn -s -S
để có thể debug cả Normal World và Secure World.
Cấu hình Makefile cho debugging
Thiết lập GDB:
1
gdb-multiarch /home/kien/workspace/src/trusted-firmware-a/build/qemu/debug/bl1/bl1.elf
Trong GDB console:
set architecture aarch64
target remote localhost:1234
Đặt breakpoint và tiếp tục thực thi:
break bl1_main
continue
2.2. Symbol files cho các boot stages
Danh sách các file symbol cho từng giai đoạn boot:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# BL1 (Boot ROM)
bl1: /home/kien/workspace/src/trusted-firmware-a/build/qemu/debug/bl1/bl1.elf
# BL2 (Trusted Boot Firmware)
bl2: /home/kien/workspace/src/trusted-firmware-a/build/qemu/debug/bl2/bl2.elf
# BL31 (EL3 Runtime Software)
bl31: /home/kien/workspace/src/trusted-firmware-a/build/qemu/debug/bl31/bl31.elf
# BL32 (TEE OS)
bl32: /home/kien/workspace/src/optee_os/out/arm/core/tee.elf
# BL33 (UEFI)
bl33: edk2 (không phải u-boot do chạy trên QEMU)
2.3. Load symbols theo giai đoạn
Khi debug, cần load symbol file tương ứng với giai đoạn hiện tại:
file /home/kien/workspace/src/trusted-firmware-a/build/qemu/debug/bl1/bl1.elf
file /home/kien/workspace/src/trusted-firmware-a/build/qemu/debug/bl2/bl2.elf
file /home/kien/workspace/src/trusted-firmware-a/build/qemu/debug/bl31/bl31.elf
file /home/kien/workspace/src/optee_os/out/arm/core/tee.elf
3. Kiến trúc bảo mật ARM
3.1. Exception Levels
Kiến trúc ARM v8-A với các Exception Levels
ARM v8-A được chia thành 4 Exception Levels:
- EL0: Ứng dụng người dùng (Client Applications, Trusted Applications)
- EL1: Hệ điều hành (Linux, OP-TEE OS)
- EL2: Hypervisor
- EL3: Secure Monitor (ARM Trusted Firmware)
3.2. Security States
Hệ thống được chia thành hai “thế giới” bảo mật:
- Non-secure World: Linux OS và Client Applications (CA)
- Secure World: OP-TEE OS và Trusted Applications (TA)
Hai trạng thái bảo mật trong ARM TrustZone
4. Quá trình khởi động OP-TEE
4.1. Boot sequence
Sơ đồ quá trình khởi động OP-TEE
Chi tiết quá trình khởi động:
1
2
3
4
5
6
7
8
9
10
11
bl31_entrypoint (trusted-firmware-a/bl31/aarch64/bl31_entrypoint.S)
└── bl31_main (trusted-firmware-a/bl31/bl31_main.c)
├── runtime_svc_init (trusted-firmware-a/common/runtime_svc.c)
│ └── opteed_setup (trusted-firmware-a/services/spd/opteed/opteed_main.c)
│ ├── bl31_plat_get_next_image_ep_info(SECURE)
│ ├── opteed_init_optee_ep_state
│ └── bl31_register_bl32_init(&opteed_init)
├── bl32_init // opteed_init() đã được đăng ký
│ └── opteed_synchronous_sp_entry(optee_ctx)
│ └── opteed_enter_sp(&optee_ctx->c_rt_ctx) // ERET → S-EL1 → OP-TEE start
└── bl31_prepare_next_image_entry // chuyển sang UEFI
4.2. Debug breakpoints
Để debug hiệu quả, cần đặt breakpoint theo từng giai đoạn boot và load symbol file tương ứng trước khi thực hiện debug.
Chúc mừng! Bạn đã thiết lập thành công môi trường OP-TEE và có thể bắt đầu debug các thành phần trong Secure World.