1# OP-TEE Trusted OS 2## Contents 3<!--- TOC generated using http://doctoc.herokuapp.com/ --> 41. [Introduction](#1-introduction) 52. [License](#2-license) 63. [Platforms supported](#3-platforms-supported) 74. [Get and build the software](#4-get-and-build-the-software) 8 4. [Basic setup](#41-basic-setup) 9 4. [Foundation Models](#42-foundation-models) 10 4. [ARM Juno board](#43-juno) 11 4. [QEMU](#44-qemu) 12 4. [STMicroelectronics boards](#45-stmicroelectronics-boards) 13 4. [Allwinner A80](#46-allwinner-a80) 14 4. [Mediatek MT8173 EVB](#47-mediatek-mt8173-evb) 15 4. [HiKey Board](#48-hikey-board) 165. [Coding standards](#5-coding-standards) 17 5. [checkpatch](#51-checkpatch) 186. [repo manifests](#6-repo-manifests) 19 6. [Install repo](#61-install-repo) 20 6. [Get the source code](#62-get-the-source-code) 21 6. [Targets](#621-targets) 22 6. [Branches](#622-branches) 23 6. [Get the toolchains](#623-get-the-toolchains) 24 6. [QEMU](#63-qemu) 25 6. [FVP](#64-fvp) 26 6. [Hikey](#65-hikey) 27 6. [MT8173-EVB](#66-mt8173-evb) 28 6. [Tips and tricks](#67-tips-and-tricks) 29 6. [Reference existing project to speed up repo sync](#671-reference-existing-project-to-speed-up-repo-sync) 30 6. [Use ccache](#672-use-ccache) 31 32# 1. Introduction 33The optee_os git, contains the source code for the TEE in Linux using the ARM(R) 34TrustZone(R) technology. This component meets the GlobalPlatform TEE System 35Architecture specification. It also provides the TEE Internal API v1.0 as 36defined by the Global Platform TEE Standard for the development of Trusted 37Applications. For a general overview of OP-TEE and to find out how to contribute, 38please see the [Notice.md](Notice.md) file. 39 40The Trusted OS is accessible from the Rich OS (Linux) using the 41[GlobalPlatform TEE Client API Specification v1.0](http://www.globalplatform.org/specificationsdevice.asp), 42which also is used to trigger secure execution of applications within the TEE. 43 44## 2. License 45The software is distributed mostly under the 46[BSD 2-Clause](http://opensource.org/licenses/BSD-2-Clause) open source 47license, apart from some files in the optee_os/lib/libutils directory which 48are distributed under the 49[BSD 3-Clause](http://opensource.org/licenses/BSD-3-Clause) or public domain 50licenses. 51 52## 3. Platforms supported 53Several platforms are supported. In order to manage slight differences 54between platforms, a `PLATFORM_FLAVOR` flag has been introduced. 55The `PLATFORM` and `PLATFORM_FLAVOR` flags define the whole configuration 56for a chip the where the Trusted OS runs. Note that there is also a 57composite form which makes it possible to append `PLATFORM_FLAVOR` directly, 58by adding a dash inbetween the names. The composite form is shown below 59for the different boards. For more specific details about build flags etc, 60please read the file [build_system.md](documentation/build_system.md). 61 62| Platform | Composite PLATFORM flag | 63|--------|--------| 64| [Foundation FVP](http://www.arm.com/fvp) |`PLATFORM=vexpress-fvp`| 65| [ARMs Juno Board](http://www.arm.com/products/tools/development-boards/versatile-express/juno-arm-development-platform.php) |`PLATFORM=vexpress-juno`| 66| [QEMU](http://wiki.qemu.org/Main_Page) |`PLATFORM=vexpress-qemu_virt`| 67| [STMicroelectronics b2120 - h310 / h410](http://www.st.com/web/en/catalog/mmc/FM131/SC999/SS1628/PF258776) |`PLATFORM=stm-cannes`| 68| [STMicroelectronics b2020-h416](http://www.st.com/web/catalog/mmc/FM131/SC999/SS1633/PF253155?sc=internet/imag_video/product/253155.jsp)|`PLATFORM=stm-orly2`| 69| [Allwinner A80 Board](http://www.allwinnertech.com/en/clq/processora/A80.html)|`PLATFORM=sunxi`| 70| [HiKey Board (HiSilicon Kirin 620)](https://www.96boards.org/products/hikey/)|`PLATFORM=hikey`| 71| MediaTek MT8173 EVB Board|`PLATFORM=mediatek-mt8173`| 72 73## 4. Get and build the software 74There are a couple of different build options depending on the target you are 75going to use. If you just want to get the software and compile it, then you 76should follow the instructions under the "Basic setup" below. In case you are 77going to run for a certain hardware or FVP, QEMU for example, then please follow 78the respective section instead. 79 80--- 81### 4.1 Basic setup 82#### 4.1.1 Get the compiler 83We will strive to use the latest available compiler from Linaro. Start by 84downloading and unpacking the compiler. Then export the PATH to the bin folder. 85 86``` 87$ cd $HOME 88$ mkdir toolchains 89$ cd toolchains 90$ wget http://releases.linaro.org/14.05/components/toolchain/binaries/gcc-linaro-arm-linux-gnueabihf-4.9-2014.05_linux.tar.xz 91$ tar xvf gcc-linaro-arm-linux-gnueabihf-4.9-2014.05_linux.tar.xz 92$ export PATH=$HOME/toolchains/gcc-linaro-arm-linux-gnueabihf-4.9-2014.05_linux/bin:$PATH 93``` 94 95#### 4.1.2 Download the source code 96``` 97$ cd $HOME 98$ mkdir devel 99$ cd devel 100$ git clone https://github.com/OP-TEE/optee_os.git 101``` 102 103#### 4.1.3 Build 104``` 105$ cd $HOME/devel/optee_os 106$ CROSS_COMPILE=arm-linux-gnueabihf- make 107``` 108 109#### 4.1.4 Compiler flags 110To be able to see the full command when building you could build using 111following flag: 112``` 113$ make V=1 114``` 115 116To enable debug builds use the following flag: 117``` 118$ make DEBUG=1 119``` 120 121OP-TEE supports a couple of different levels of debug prints for both TEE core 122itself and for the Trusted Applications. The level ranges from 1 to 4, where 123four is the most verbose. To set the level you use the following flag: 124``` 125$ make CFG_TEE_CORE_LOG_LEVEL=4 126``` 127 128--- 129### 4.2 Foundation Models 130 131See section [6. repo manifests]((#6-repo-manifests). 132 133--- 134### 4.3 Juno 135Juno has been supported in OP-TEE since mid October 2014. 136 137#### WARNING: 138 139+ The ```setup_juno_optee.sh``` script provides a coherent set of components (OP-TEE client/driver/os, 140Linux kernel version 3-16.0-rc5) 141 142+ Further release will align the ARM Juno setup with other OP-TEE supported platforms: 143 144 + Linux kernel version alignment (3.18-rc1) with QEMU/FVP (DMA_BUF API change). 145 + Will need arch/arm/Kconfig patch(es) (i.e DMA_SHARED_BUFFER etc...). 146 147+ Temporary patch files required for linux kernel and juno dtb definition: 148 149 + config.linux-linaro-tracking.a226b22057c22b433caafc58eeae6e9b13ac6c8d.patch 150 + juno.dts.linux-linaro-tracking.a226b22057c22b433caafc58eeae6e9b13ac6c8d.patch 151 152#### 4.3.1 Prerequisites 153+ The following packages must be installed: 154 155``` 156$ sudo apt-get install zlib1g-dev libglib2.0-dev libpixman-1-dev libfdt-dev \ 157 libc6:i386 libstdc++6:i386 libz1:i386 cscope 158``` 159 160+ Download ARM Juno pre-built binaries: 161 162 + ARM Juno Pre-built binary bl30.bin (SCP runtime) 163 + ARM Juno Pre-built binary bl33.bin (UEFI) 164 + Download at http://community.arm.com/docs/DOC-8401 165 166 167#### 4.3.2 Download and install ARM Juno 168``` 169$ wget https://raw.githubusercontent.com/OP-TEE/optee_os/master/scripts/setup_juno_optee.sh 170$ chmod 711 setup_juno_optee.sh 171$ ./setup_juno_optee.sh 172``` 173 174#### 4.3.3 Build 175+ List of helper scripts generated during installation: 176 177* `build_atf_opteed.sh`: This is used to build ARM-Trusted-Firmware and must be 178 called when you have updated any component that are included in the FIP (like 179 for example OP-TEE os). 180 181* `build_linux.sh`: This is used to build the Linux Kernel. 182 183* `build_normal.sh`: This is a pure helper script that build all the normal 184 world components (in correct order). 185 186* `build_optee_client.sh`: This will build OP-TEEs client library. 187 188* `build_optee_linuxdriver.sh`: This will build OP-TEEs Linux Kernel driver (as 189 a module). 190 191* `build_optee_os.sh`: Builds the Trusted OS itself. 192 193* `build_optee_tests.sh`: This will build the test suite (pay attention to the 194 access needed). 195 196* `build_secure.sh`: This is the helper script for the secure side that will 197 build all secure side components in the correct order. 198 199* `clean_gits.sh`: This will clean all gits. Beware that it will not reset the 200 commit to the one used when first cloning. Also note that it will only clean 201 git's. 202 203+ Run the scripts in the following order: 204 205``` 206$ ./build_secure.sh 207$ ./build_normal.sh 208``` 209 210#### 4.3.4 Booting up ARM Juno 211 212+ Update the ARM Juno embedded flash memory (path: JUNO/SOFTWARE): 213 214 + bl1.bin 215 + fip.bin 216 + Image 217 + juno.dtb 218 219+ Copy OP-TEE binaries on the filesystem(*) located on the external USB key: 220 221 + user client libraries: libteec.so* 222 + supplicant: tee-supplicant 223 + driver modules: optee.ko. optee_armtz.ko 224 + CA: xtest 225 + TAs: *.ta 226 227+ Connect the USB key (filesystem) on any connector of the rear panel 228 229+ Connect a serial terminal (115200, 8, n, 1) 230to the upper 9-pin (UART0) connector. 231 232+ Connect the 12 volt power, then press the red button on the rear panel. 233 234Note: 235The default configuration is to automatically boot a Linux kernel, 236which expects to find a root filesystem on /dev/sda1 237(any one of the rear panel USB ports). 238 239(*)Download a minimal filesytem at: 240http://releases.linaro.org/14.02/openembedded/aarch64/ 241linaro-image-minimal-genericarmv8-20140223-649.rootfs.tar.gz 242 243UEFI offers a 10 second window to interrupt the boot sequence by pressing 244a key on the serial terminal, after which the kernel is launched. 245 246Once booted you will get the prompt: 247``` 248root@genericarmv8:~# 249``` 250 251#### 4.3.4 Run OP-TEE on ARM Juno 252Write in the console: 253``` 254root@genericarmv8:~# modprobe optee 255root@genericarmv8:~# tee-supplicant & 256``` 257Now everything has been set up and OP-TEE is ready to be used. 258 259#### 4.3.5 Known problems and limitations 260ARM Juno could be sensitive on the USB memory type (filesystem) 261Recommendation: Use USB memory 3.0 (ext3/ext4 filesystem) 262 263--- 264### 4.4 QEMU 265 266Please refer to section [6. repo manifests](#6-repo-manifests). 267 268--- 269### 4.5 STMicroelectronics boards 270Currently OP-TEE is supported on Orly-2 (b2020-h416) and Cannes family (b2120 271both h310 and h410 chip). 272 273#### 4.5.1 Get the compiler for Orly-2 274Will be written soon. 275 276#### 4.5.2 Download the source code 277See section "4.1.2 Download the source code". 278 279#### 4.5.3 Build for Orly-2 280Will be written soon. 281 282For Orly-2 do as follows 283``` 284$ PLATFORM_FLAVOR=orly2 CROSS_COMPILE=arm-linux-gnueabihf- make 285``` 286 287For Cannes family do as follows 288``` 289$ PLATFORM_FLAVOR=cannes CROSS_COMPILE=arm-linux-gnueabihf- make 290``` 291 292#### 4.5.4 Prepare and install the images 293Will be written soon. 294 295For Orly-2 do as follows 296``` 297To be written. 298``` 299 300For Cannes family do as follows 301``` 302To be written. 303``` 304 305#### 4.5.5 Boot and run the software 306Will be written soon. All magic with STM and so on must be stated here. 307 308For Orly-2 do as follows 309``` 310To be written. 311``` 312 313For Cannes family do as follows 314``` 315To be written. 316``` 317 318--- 319### 4.6 Allwinner A80 320Allwinner A80 platform has been supported in OP-TEE since mid December 2014. 321#### 4.6.1 Get the compiler and source 322Follow the instructions in the "4.1 Basic setup". 323 324#### 4.6.2 Build 325``` 326$ cd optee_os 327$ export PLATFORM=sunxi 328$ export CROSS_COMPILE=arm-linux-gnueabihf- 329$ make 330``` 331 332#### 4.6.3 Prepare the images to run on A80 Board 333 334Download Allwinner A80 platform SDK. 335The SDK refer to Allwinner A80 platform SDK root directory. 336A80 SDK directory tree like this: 337``` 338SDK/ 339 Android 340 lichee 341``` 342Android include all Android source code, 343lichee include bootloader and linux kernel. 344 345##### 4.6.3.1 Copy OP-TEE output to package directory 346copy the OP-TEE output binary to SDK/lichee/tools/pack/sun9i/bin 347``` 348$ cd optee_os 349$ cp ./out/arm32-plat-sunxi/core/tee.bin SDK/lichee/tools/pack/sun9i/bin 350``` 351 352##### 4.6.3.2 Build linux kernel 353In lichee directory, Type the following commands: 354``` 355$ cd SDK/lichee 356$ ./build.sh 357``` 358 359##### 4.6.3.3 Build Android 360In Android directory, Type the following commands: 361``` 362$ cd SDK/android 363$ extract-bsp 364$ make -j 365``` 366 367##### 4.6.3.4 Create Android image 368In andoid directory, Type the following commands: 369``` 370$ cd SDK/android 371$ pack 372``` 373The output image will been signed internally when pack. 374The output image name is a80_android_board.img. 375 376##### 4.6.3.5 Download Android image 377Use Allwinner PhoenixSuit tool to download to A80 board. 378Choose the output image(a80_android_board.img), 379Choose download, 380Wait for the download to complete. 381 382#### 4.6.4 Boot and run the software on A80 Board 383When the host platform is Windows, Use a console application 384to connect A80 board uart0. In the console window, 385You can install OP-TEE linux kernel driver optee.ko, 386Load OP-TEE-Client daemon tee-supplicant, 387Run OP-TEE example hello world application. 388This is done by the following lines: 389``` 390$ insmod /system/vendor/modules/optee.ko 391$ /system/bin/tee-supplicant & 392$ /system/bin/tee-helloworld 393``` 394Enjoying OP-TEE on A80 board. 395 396--- 397### 4.7 Mediatek MT8173 EVB 398Please refer to [8173 wiki](https://github.com/ibanezchen/linux-8173/wiki) 399to setup MT8173 evaluation board. 400 401To build the software, please see section [6. repo manifests](#6-repo-manifests). 402 403--- 404### 4.8 HiKey board 405[HiKey](https://www.96boards.org/products/hikey/) is a 96Boards Consumer 406Edition compliant board equipped with a HiSilicon Kirin 620 SoC (8-core, 40764-bit ARM Cortex A53). It can run OP-TEE in 32- and 64-bit modes. 408 409To build for HiKey, please refer to [6. repo manifests](#6-repo-manifests). 410 411## 5. Coding standards 412In this project we are trying to adhere to the same coding convention as used in 413the Linux kernel (see 414[CodingStyle](https://www.kernel.org/doc/Documentation/CodingStyle)). We achieve this by running 415[checkpatch](http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/scripts/checkpatch.pl) from Linux kernel. 416However there are a few exceptions that we had to make since the code also 417follows GlobalPlatform standards. The exceptions are as follows: 418 419- CamelCase for GlobalPlatform types are allowed. 420- And we also exclude checking third party code that we might use in this 421 project, such as LibTomCrypt, MPA, newlib (not in this particular git, but 422 those are also part of the complete TEE solution). The reason for excluding 423 and not fixing third party code is because we would probably deviate too much 424 from upstream and therefore it would be hard to rebase against those projects 425 later on (and we don't expect that it is easy to convince other software 426 projects to change coding style). 427 428### 5.1 checkpatch 429Since checkpatch is licensed under the terms of GNU GPL License Version 2, we 430cannot include this script directly into this project. Therefore we have 431written the Makefile so you need to explicitly point to the script by exporting 432an environment variable, namely CHECKPATCH. So, suppose that the source code for 433the Linux kernel is at `$HOME/devel/linux`, then you have to export like follows: 434 435 $ export CHECKPATCH=$HOME/devel/linux/scripts/checkpatch.pl 436thereafter it should be possible to use one of the different checkpatch targets 437in the [Makefile](Makefile). There are targets for checking all files, checking 438against latest commit, against a certain base-commit etc. For the details, read 439the [Makefile](Makefile). 440 441## 6. repo manifests 442 443A Git repository is available at https://github.com/OP-TEE/manifest where you 444will find configuration files for use with the Android 'repo' tool. 445This sections explains how to use it. 446 447### 6.1. Install repo 448Follow the instructions under the "Installing Repo" section 449[here](https://source.android.com/source/downloading.html). 450 451### 6.2. Get the source code 452``` 453$ mkdir -p $HOME/devel/optee 454$ cd $HOME/devel/optee 455$ repo init -u https://github.com/OP-TEE/manifest.git -m ${TARGET}.xml [-b ${BRANCH}] 456$ repo sync 457``` 458 459#### 6.2.1 Targets 460* QEMU: default.xml 461* FVP: fvp.xml 462* Hikey: hikey.xml 463* MediaTek MT8173 EVB Board: mt8173-evb.xml 464 465#### 6.2.2 Branches 466Currently we are only using one branch, i.e, the master branch. 467 468#### 6.2.3 Get the toolchains 469``` 470$ cd build 471$ make toolchains 472``` 473 474**Notes**<br> 475* The folder could be at any location, we are just giving a suggestion by 476 saying `$HOME/devel/optee`. 477* `repo sync` can take an additional parameter -j to sync multiple remotes. For 478 example `repo sync -j3` will sync three remotes in parallel. 479 480### 6.3. QEMU 481After getting the source and toolchain, just run: 482``` 483$ make all run 484``` 485and everything should compile and at the end QEMU should start. 486 487### 6.4. FVP 488After getting the source and toolchain you must also get the get Foundation 489Model 490([link](http://www.arm.com/products/tools/models/fast-models/foundation-model.php)) 491and untar it to the forest root, then just run: 492``` 493$ make all run 494``` 495and everything should compile and at the end FVP should start. 496 497### 6.5. Hikey 498After running `make` above, follow the instructions at 499[flash-binaries-to-emmc](https://github.com/96boards/documentation/wiki/HiKeyUEFI#flash-binaries-to-emmc-) 500to flash all the required images to and boot the board. 501 502Location of files/images mentioned in the link above: 503* ```$HOME/devel/optee/burn-boot/hisi-idt.py``` 504* ```$HOME/devel/optee/l-loader/l-loader.bin``` 505* ```$HOME/devel/optee/l-loader/ptable.img``` 506* ```$HOME/devel/optee/arm-trusted-firmware/build/hikey/release/fip.bin``` 507* ```$HOME/devel/optee/out/boot-fat.uefi.img``` 508 509### 6.6. MT8173-EVB 510After getting the source and toolchain, please run: 511 512``` 513$ make all run 514``` 515 516When `< waiting for device >` prompt appears, press reset button 517 518### 6.7 Tips and tricks 519#### 6.7.1 Reference existing project to speed up repo sync 520Doing a `repo init`, `repo sync` from scratch can take a fair amount of time. 521The main reason for that is simply because of the size of some of the gits we 522are using, like for the Linux kernel and EDK2. With repo you can reference an 523existing forest and by doing so you can speed up repo sync to instead taking ~20 524seconds instead of an hour. The way to do this are as follows. 525 5261. Start by setup a clean forest that you will not touch, in this example, let 527 us call that `optee-ref` and put that under for `$HOME/devel/optee-ref`. This 528 step will take roughly an hour. 5292. Then setup a cronjob (`crontab -e`) that does a `repo sync` in this folder 530 particular folder once a night (that is more than enough). 5313. Now you should setup your actual tree which you are going to use as your 532 working tree. The way to do this is almost the same as stated in the 533 instructions above, the only difference is that you reference the other local 534 forest when running `repo init`, like this 535 ``` 536 repo init -u https://github.com/OP-TEE/manifest.git --reference /home/jbech/devel/optee-ref 537 ``` 5384. The rest is the same above, but now it will only take a couple of seconds to 539 clone a forest. 540 541Normally step 1 and 2 above is something you will only do once. Also if you 542ignore step 2, then you will still get the latest from official git trees, since 543repo will also check for updates that aren't at the local reference. 544 545#### 6.7.2. Use ccache 546ccache is a tool that caches build object-files etc locally on the disc and can 547speed up build time significantly in subsequent builds. On Debian-based systems 548(Ubuntu, Mint etc) you simply install it by running: 549``` 550$ sudo apt-get install ccache 551``` 552 553The helper makefiles are configured to automatically find and use ccache if 554ccache is installed on your system, so other than having it installed you don't 555have to think about anything. 556 557