Qualcomm TrustZone 4.0.5 编译开发文档
目的
基于 TZ4.0.5 版本开发 TZ 安全应用入门
前提
本文基于 SDM450 平台,针对 TZ4.0.5 版本的编译开发验证过程。
针对 TZ4.0.5 的以下基本是通用的,不同的 TZ 版本有差异,不能通用。
编译
想编译 TZ4.0.5 环境,根据高通 sp80-p3255-4 的文档,主要有以下工作要做。
下载相关版本的 LLVM 编译器、aarch64 和 arm 的编译器
编译环境配置
LLVM:
下载地址 https://chipcode.qti.qualcomm.com
使用版本:Snapdragon-llvm-4.0.11-linux64.tar.gz
Aarch64 编译器:
https://releases.linaro.org/components/toolchain/binaries/4.9-2017.01/aarch64-elf/gcc-linaro-4.
9.4-2017.01-x86_64_aarch64-elf.tar.xz
ARM 编译器:
https://releases.linaro.org/components/toolchain/binaries/4.9-2017.01/arm-linux-gnueabihf/gcc
-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz
例:解压放到/home/kuili/prebuild 目录下
脚本修改
目录结构:
/home/kuili/TZ.BF.4.0.5
/home/kuili/prebuild/Snapdragon-llvm-4.0.11
/home/kuili/prebuild/gcc-linaro-arm-linux-gnueabihf-4.9
/home/kuili/aarch64-none-elf-4.9
TZ.BF.4.0.5 的 Setup.sh 修改如下:
cd TZ.BF.4.0.5/trustzone_images/build/ms,setup.sh 修改部分如下
PYTHON_PATH=/usr/bin
MAKE_PATH=/usr/bin
export ARMTOOLS=ARMCT6 #FIXME
export LLVMTOOLS=LLVM
export LLVMROOT=/home/kuili/prebuild/Snapdragon-llvm-4.0.11
export LLVMBIN=$LLVMROOT/bin
export LLVMLIB=$LLVMROOT/lib/clang/4.0.11/lib/linux
export MUSLPATH=$LLVMROOT/aarch64-none-elf/libc
export MUSL32PATH=$LLVMROOT/armv7-linux-gnueabi/libc
export LLVMINC=$MUSLPATH/include
export LLVM32INC=$MUSL32PATH/include
export LLVMTOOLPATH=$LLVMROOT/tools/bin
export GNUROOT=/home/kuili/prebuild/aarch64-none-elf-4.9
export GNUARM7=/home/kuili/prebuild/gcc-linaro-arm-linux-gnueabihf-4.9
export PATH=$GNUTOOL:$PATH
llvm.py 修改:
第一处:
277
278
279
if do_64:
# delete -aarch64-strict-align -Werror by kuili
LLVMFlags += " -target aarch64-linux-gnu -w -mcpu=cortex-a53 -mllvm
-enable-global-merge=false -nostdinc "
280
#LLVMFlags += " -target aarch64-linux-gnu -Werror
-mllvm
-aarch64-strict-align -mcpu=cortex-a53 -mllvm -enable-global-merge=false -nostdinc "
else:
第二处:
296
297
298
299
do_32 = True
# delete -mcpu=krait2 -Werror by kuili
LLVMFlags += " -target arm-linux-gnueabi -mfloat-abi=softfp -mfpu=none
-mcpu=cortex-a7
-enable-android-compat
-mllvm
-fno-vectorize-loops
-mno-unaligned-access "
执行编译:
cd TZ.BF.4.0.5/trustzone_images/build/ms
./build.sh CHIPSET=msm8953 devcfg sampleapp
编译产物
按照以上编译会在 build/ms/bin 目录下生成 smplap64.mbn、smplap32.mbn、devcfg.mbn
文件,如果想单独调试 sampleapp 应用,可以单独把 smplap32.mbn 或者 smplap64.mbn 的
应用放到板子上运行测试,使用系统自带的 nonsecure 的应用程序 qseecom_sample_client
来验证,代码位置位于 HLOS 源码目录下的 vendor/qcom/proprietary/securemsm/sampleclient。
如果想烧录完整的验证,则需要把 NON-HLOS.bin 文件编译出来,通过 fastboot 烧录到
板子上,进入 amss/MSM8953.LA.3.2/common/build 目录下,编译生成 NON-HLOS.bin 文件
烧录到板子上:
adb reboot bootloader
fastboot flash modem NON-HLOS.bin
fastboot reboot
系统启动后,会把 NON-HLOS.bin 放在/dev/block/mmcblk0p1 分区,通过命令来验证:
qseecom_sample_client v smplap64 0 1 来验证,secure app,即 TZ 安全代码执行的 log
可以通过 cat /sys/kernel/debug/tzdbg/qsee_log 来查看
Secure APP 开发
组织架构
QSEE 4.0 安全应用架构图
例如以上调用的案例 qseecom_sample_client 是运行在 NON Secure World,为一个普通的应
用程序,通过 QSEE 提供的封装库暴露的接口,调用到 kernel 的 QSEE 驱动,再通过 SCM driver
的 特 殊 指 令 进 入 安 全 世 界 Secure World,TZ 世 界 也 提 供 了 一 套 Applib , 来 开 发 Secure
Applications,以上基于 TZ 源码编译出来的 sampleapp 即使 Secure Applications 一个比较完成
的 demo 示例,也是一个自我测试验证 TZ 功能的应用程序,我们针对 TZ 提供的安全开发都
可以借鉴。
接口文档
接口文档在 80-nh537-2 高通文档的第 8 章节,介绍了所有安全应用暴露出来的接口,
这里只给基本的大类,可以查看详细的官方文档。以下对可能会使用的比较多的做一下说明。
BLSP:
Modifies the ownership of QUP, GPIO, BAM, and SMMU for a specified
subsystem
(TZ 未见到有任何地方的使用,暂时只能按字面理解为修改特定子系统的 GPIO
等硬件相关属性)
Bulletin Board
Posts a note to the QSEE bulletin board under a specific category
提供了公告板的功能,可分类写入或者读取公告板,同类的写入,会覆盖上一次
Crypto engine Pipe
Sets the key of the crypto engine pipe.
设置 CE 的密钥
Cipher
cipher context for encrypt/decrypt operations
Clock
Sets the crypto/BIMC/SNOC clock bandwidth
CMAC
Using the specified hash algorithm, creates a cipher MAC
Comstr
Configuration
Retrieves the property configuration value
Core
Counter
Crypto
Locks or unlocks the crypto hardware by XPU-protecting the resource. This function
might do nothing based on whether the chipset supports a dedicated TZ crypto engine
Dcache
a memory region in the cache. The data in the cache is not written back to
main memory
ECC
Generates public and private elliptic curve cryptography (ECC) keys
Embedded Secure Element
Starts the Embedded Secure Element (eSE) service and performs initialization
Fuse
Hash
Hash 接口
Heap
堆接口
HMAC
Using the specified hash algorithm, creates a hash MAC
I2C
I2C TZ 接口暴露
Interrupts
中断禁用控制接口
KDF
Key derivation function (KDF) key derivation algorithm
Key Manager
密钥管理获取
Keystore
密钥库的操作接口,获取、解密验证、删除密钥
Logging
日志
Message Passing
OEM buffer
获取 OEM buffer 中的数据
OEM Utilities
PRNG
RSA
提供 1024、2048 位的 RSA 加解密、签名功能
Secure Display
Allows or blocks HLOS from stopping Secure Display via the sd_ctrl syscall
Secure Channel
消息发送过程是被加密的
Services
类似 UNIX 的消息和共享内存的功能提供
SFS
SFS 的文件读写接口
SPI
Opens access to an SPI device, allowing application control
针对 TZ 应用下可读写的 SPI 设备
Storage
Reads、Writes and Modify a number of sectors from the client storage device
Synchronization
Performs a busy wait (spin) for the input number of microseconds
Timer
Retrieves the up time, in milliseconds, from bootup
TLMM
Retrieves a GPIO ID (key) based on a signal name string. On success, this locks the
GPIO to be modifiable only by the holder of the key
Address Translation
Translates a nonsecure virtual address to a physical address
Unified AES
The supported algorithm
types are as follows:
SW_CIPHER_ALG_AES128
SW_CIPHER_ALG_AES192
SW_CIPHER_ALG_AES256
Unified DES
The supported algorithms
are SW_CIPHER_ALG_DES and SW_CIPHER_ALG_DES3
Unified SHA
Creates and initializes a crypto context for a specified hash algorithm. The supported
algorithms are as follows:
SW_AUTH_ALG_SHA1
SW_AUTH_ALG_SHA256
SW_AUTH_ALG_SHA224
SW_AUTH_ALG_SHA384
SW_AUTH_ALG_SHA512