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