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) 145. [Coding standards](#5-coding-standards) 15 5. [checkpatch](#51-checkpatch) 16 17# 1. Introduction 18The optee_os git, contains the source code for the TEE in Linux using the ARM(R) 19TrustZone(R) technology. This component meets the GlobalPlatform TEE System 20Architecture specification. It also provides the TEE Internal API v1.0 as 21defined by the Global Platform TEE Standard for the development of Trusted 22Applications. For a general overview of OP-TEE and to find out how to contribute, 23please see the [Notice.md](Notice.md) file. 24 25The Trusted OS is accessible from the Rich OS (Linux) using the 26[GlobalPlatform TEE Client API Specification v1.0](http://www.globalplatform.org/specificationsdevice.asp), 27which also is used to trigger secure execution of applications within the TEE. 28 29## 2. License 30The software is distributed mostly under the 31[BSD 2-Clause](http://opensource.org/licenses/BSD-2-Clause) open source 32license, apart from some files in the optee_os/lib/libutils directory which 33are distributed under the 34[BSD 3-Clause](http://opensource.org/licenses/BSD-3-Clause) or public domain 35licenses. 36 37## 3. Platforms supported 38Several platforms are supported. In order to manage slight differences 39between platforms, a `PLATFORM_FLAVOR` flag has been introduced. 40The `PLATFORM` and `PLATFORM_FLAVOR` flags define the whole configuration 41for a chip the where the Trusted OS runs. Note that there is also a 42composite form which makes it possible to append `PLATFORM_FLAVOR` directly, 43by adding a dash inbetween the names. The composite form is shown below 44for the different boards. For more specific details about build flags etc, 45please read the file [build_system.md](documentation/build_system.md). 46 47| Platform | Composite PLATFORM flag | 48|--------|--------| 49| [Foundation FVP](http://www.arm.com/fvp) |`PLATFORM=vexpress-fvp`| 50| [ARMs Juno Board](http://www.arm.com/products/tools/development-boards/versatile-express/juno-arm-development-platform.php) |`PLATFORM=vexpress-juno`| 51| [QEMU](http://wiki.qemu.org/Main_Page) |`PLATFORM=vexpress-qemu_virt`| 52| [STMicroelectronics b2120 - h310 / h410](http://www.st.com/web/en/catalog/mmc/FM131/SC999/SS1628/PF258776) |`PLATFORM=stm-cannes`| 53| [STMicroelectronics b2020-h416](http://www.st.com/web/catalog/mmc/FM131/SC999/SS1633/PF253155?sc=internet/imag_video/product/253155.jsp)|`PLATFORM=stm-orly2`| 54| [Allwinner A80 Board](http://www.allwinnertech.com/en/clq/processora/A80.html)|`PLATFORM=sunxi`| 55| [HiKey Board (HiSilicon Kirin 620)](https://www.96boards.org/products/hikey/)|`PLATFORM=hikey`| 56| MediaTek MT8173 EVB Board|`PLATFORM=mediatek-mt8173`| 57 58## 4. Get and build the software 59There are a couple of different build options depending on the target you are 60going to use. If you just want to get the software and compile it, then you 61should follow the instructions under the "Basic setup" below. In case you are 62going to run for a certain hardware or FVP, QEMU for example, then please follow 63the respective section instead. 64 65--- 66### 4.1 Basic setup 67#### 4.1.1 Get the compiler 68We will strive to use the latest available compiler from Linaro. Start by 69downloading and unpacking the compiler. Then export the PATH to the bin folder. 70 71``` 72$ cd $HOME 73$ mkdir toolchains 74$ cd toolchains 75$ wget http://releases.linaro.org/14.05/components/toolchain/binaries/gcc-linaro-arm-linux-gnueabihf-4.9-2014.05_linux.tar.xz 76$ tar xvf gcc-linaro-arm-linux-gnueabihf-4.9-2014.05_linux.tar.xz 77$ export PATH=$HOME/toolchains/gcc-linaro-arm-linux-gnueabihf-4.9-2014.05_linux/bin:$PATH 78``` 79 80#### 4.1.2 Download the source code 81``` 82$ cd $HOME 83$ mkdir devel 84$ cd devel 85$ git clone https://github.com/OP-TEE/optee_os.git 86``` 87 88#### 4.1.3 Build 89``` 90$ cd $HOME/devel/optee_os 91$ CROSS_COMPILE=arm-linux-gnueabihf- make 92``` 93 94#### 4.1.4 Compiler flags 95To be able to see the full command when building you could build using 96following flag: 97``` 98$ make V=1 99``` 100 101To enable debug builds use the following flag: 102``` 103$ make DEBUG=1 104``` 105 106OP-TEE supports a couple of different levels of debug prints for both TEE core 107itself and for the Trusted Applications. The level ranges from 1 to 4, where 108four is the most verbose. To set the level you use the following flag: 109``` 110$ make CFG_TEE_CORE_LOG_LEVEL=4 111``` 112 113--- 114### 4.2 Foundation Models 115By following this section will setup OP-TEE using FVP (Foundation Models and 116also Fast Models). You will have to download a script in this git and then run 117it, modify it slightly and then run it again. The reason for this is that we 118are not allowed to share Foundation models. I.e, the user has to download it 119from ARM directly. 120 121#### 4.2.1 Prerequisites 122To be able run this script you will need to install a couple of dependencies. On 123a Debian based system (Ubuntu, Mint etc.), you will at least need to install the 124following packages: 125 126``` 127$ sudo apt-get install uuid-dev 128``` 129and in case you are running on a 64bits system, then you will need to install 130the following packages. 131 132``` 133$ sudo apt-get install libc6:i386 libstdc++6:i386 libz1:i386 134``` 135 136#### 4.2.2 Download and setup FVP 137``` 138$ wget https://raw.githubusercontent.com/OP-TEE/optee_os/master/scripts/setup_fvp_optee.sh 139$ chmod 711 setup_fvp_optee.sh 140$ ./setup_fvp_optee.sh 141``` 142Follow the instructions to download Foundation Models and then update the first 143few lines under the "EDIT" section in the script. Note that if you are not 144working in Linaro and belongs to Security Working Group you will probably not 145have access to teetest.git, hence you should most likely leave this as it is. 146Run the script again. 147``` 148$ ./setup_fvp_optee.sh 149``` 150After about one hour (it's mainly cloning the kernel and edk2 that takes time) 151everything should have been cloned and built and you should be ready to use 152this. Pay attention to the line saying `OP-TEE and FVP setup completed.` that 153would be displayed when the script successfully ended. If you don't see this at 154the end, then something went wrong. 155 156#### 4.2.3 Compile 157During installation a couple of helper scripts were generated, the main reason 158for this is that there is a lot of interdependencies between the different 159software components and it's a bit tricky to point to the correct toolchains and 160to know in which order to build things. 161 162* `build_atf_opteed.sh`: This is used to build ARM-Trusted-Firmware and must be 163 called when you have updated any component that are included in the FIP (like 164 for example OP-TEE os). 165 166* `build_linux.sh`: This is used to build the Linux Kernel. 167 168* `build_normal.sh`: This is a pure helper script that build all the normal 169 world components (in correct order). 170 171* `build_optee_client.sh`: This will build OP-TEEs client library. 172 173* `build_optee_linuxkernel.sh`: This will build OP-TEEs Linux Kernel driver (as 174 a module). 175 176* `build_optee_os.sh`: Builds the Trusted OS itself. 177 178* `build_optee_tests.sh`: This will build the test suite (pay attention to the 179 access needed). 180 181* `build_secure.sh`: This is the helper script for the secure side that will 182 build all secure side components in the correct order. 183 184* `build_uefi.sh`: This will build Tianocore (UEFI). 185 186* `clean_gits.sh`: This will clean all gits. Beware that it will not reset the 187 commit to the one used when first cloning. Also note that it will only clean 188 git's (meaning that it will not clean Foundation models, toolchain folders). 189 190* `run_foundation.sh`: This is the script to use when starting FVP. 191 192* `update_rootfs.sh`: This script will update rootfs. For example when you have 193 updated normal world component, you will need to put them into rootfs. Calling 194 this script will do so. In case you are creating a new Trusted Application, 195 you must also edit filelist-tee.text in the gen_rootfs folder accordingly. 196 197Depending on how you are working you have the option to build components 198separately or you can build everything by running two of the scripts above. 199In case you want to make sure that everything was built and updated, we suggest 200that you call the scripts in the following order. 201``` 202$ ./build_secure.sh 203$ ./build_normal.sh 204``` 205By doing so all components should be (re-)built in the correct order and rootfs 206will be updated accordingly. 207 208#### 4.2.4 Run Foundation models and OP-TEE 209You simply run the script `run_foundation.sh`, load the module and start 210tee-supplicant. 211``` 212$ ./run_foundation.sh 213``` 214and in the console write 215``` 216root@FVP:/ modprobe optee_armtz 217root@FVP:/ tee-supplicant & 218``` 219Now everything has been set up and OP-TEE is ready to be used. 220 221#### 4.2.5 Known problems and limitations 222* The script `setup_fvp_optee.sh` doesn't do much error checking and doesn't 223 have many fallbacks in case of a problem. 224* The script `setup_fvp_optee.sh` setup things using absolute paths, i.e, you 225 cannot just copy a working environment to a new location. 226* In some situations you will get an error message about `undefined reference to 227 raise`. We know about this issue and it is being tracked in 228 [#issue95](https://github.com/OP-TEE/optee_os/issues/95) at GitHub. 229 230--- 231### 4.3 Juno 232Juno has been supported in OP-TEE since mid October 2014. 233 234#### WARNING: 235 236+ The ```setup_juno_optee.sh``` script provides a coherent set of components (OP-TEE client/driver/os, 237Linux kernel version 3-16.0-rc5) 238 239+ Further release will align the ARM Juno setup with other OP-TEE supported platforms: 240 241 + Linux kernel version alignment (3.18-rc1) with QEMU/FVP (DMA_BUF API change). 242 + Will need arch/arm/Kconfig patch(es) (i.e DMA_SHARED_BUFFER etc...). 243 244+ Temporary patch files required for linux kernel and juno dtb definition: 245 246 + config.linux-linaro-tracking.a226b22057c22b433caafc58eeae6e9b13ac6c8d.patch 247 + juno.dts.linux-linaro-tracking.a226b22057c22b433caafc58eeae6e9b13ac6c8d.patch 248 249#### 4.3.1 Prerequisites 250+ The following packages must be installed: 251 252``` 253$ sudo apt-get install zlib1g-dev libglib2.0-dev libpixman-1-dev libfdt-dev \ 254 libc6:i386 libstdc++6:i386 libz1:i386 cscope 255``` 256 257+ Download ARM Juno pre-built binaries: 258 259 + ARM Juno Pre-built binary bl30.bin (SCP runtime) 260 + ARM Juno Pre-built binary bl33.bin (UEFI) 261 + Download at http://community.arm.com/docs/DOC-8401 262 263 264#### 4.3.2 Download and install ARM Juno 265``` 266$ wget https://raw.githubusercontent.com/OP-TEE/optee_os/master/scripts/setup_juno_optee.sh 267$ chmod 711 setup_juno_optee.sh 268$ ./setup_juno_optee.sh 269``` 270 271#### 4.3.3 Build 272+ List of helper scripts generated during installation: 273 274* `build_atf_opteed.sh`: This is used to build ARM-Trusted-Firmware and must be 275 called when you have updated any component that are included in the FIP (like 276 for example OP-TEE os). 277 278* `build_linux.sh`: This is used to build the Linux Kernel. 279 280* `build_normal.sh`: This is a pure helper script that build all the normal 281 world components (in correct order). 282 283* `build_optee_client.sh`: This will build OP-TEEs client library. 284 285* `build_optee_linuxdriver.sh`: This will build OP-TEEs Linux Kernel driver (as 286 a module). 287 288* `build_optee_os.sh`: Builds the Trusted OS itself. 289 290* `build_optee_tests.sh`: This will build the test suite (pay attention to the 291 access needed). 292 293* `build_secure.sh`: This is the helper script for the secure side that will 294 build all secure side components in the correct order. 295 296* `clean_gits.sh`: This will clean all gits. Beware that it will not reset the 297 commit to the one used when first cloning. Also note that it will only clean 298 git's. 299 300+ Run the scripts in the following order: 301 302``` 303$ ./build_secure.sh 304$ ./build_normal.sh 305``` 306 307#### 4.3.4 Booting up ARM Juno 308 309+ Update the ARM Juno embedded flash memory (path: JUNO/SOFTWARE): 310 311 + bl1.bin 312 + fip.bin 313 + Image 314 + juno.dtb 315 316+ Copy OP-TEE binaries on the filesystem(*) located on the external USB key: 317 318 + user client libraries: libteec.so* 319 + supplicant: tee-supplicant 320 + driver modules: optee.ko. optee_armtz.ko 321 + CA: xtest 322 + TAs: *.ta 323 324+ Connect the USB key (filesystem) on any connector of the rear panel 325 326+ Connect a serial terminal (115200, 8, n, 1) 327to the upper 9-pin (UART0) connector. 328 329+ Connect the 12 volt power, then press the red button on the rear panel. 330 331Note: 332The default configuration is to automatically boot a Linux kernel, 333which expects to find a root filesystem on /dev/sda1 334(any one of the rear panel USB ports). 335 336(*)Download a minimal filesytem at: 337http://releases.linaro.org/14.02/openembedded/aarch64/ 338linaro-image-minimal-genericarmv8-20140223-649.rootfs.tar.gz 339 340UEFI offers a 10 second window to interrupt the boot sequence by pressing 341a key on the serial terminal, after which the kernel is launched. 342 343Once booted you will get the prompt: 344``` 345root@genericarmv8:~# 346``` 347 348#### 4.3.4 Run OP-TEE on ARM Juno 349Write in the console: 350``` 351root@genericarmv8:~# modprobe optee 352root@genericarmv8:~# tee-supplicant & 353``` 354Now everything has been set up and OP-TEE is ready to be used. 355 356#### 4.3.5 Known problems and limitations 357ARM Juno could be sensitive on the USB memory type (filesystem) 358Recommendation: Use USB memory 3.0 (ext3/ext4 filesystem) 359 360--- 361### 4.4 QEMU 362You can run OP-TEE using QEMU since October 2014. 363 364#### 4.4.1 Prerequisites 365To be able run this script you will need to install a couple of dependencies. On 366a Debian based system (Ubuntu, Mint etc.), you will at least need to install the 367following packages: 368 369``` 370$ sudo apt-get install zlib1g-dev libglib2.0-dev libpixman-1-dev libfdt-dev \ 371 libc6:i386 libstdc++6:i386 libz1:i386 cscope 372``` 373 374#### 4.4.2 Download and setup QEMU 375``` 376$ wget https://raw.githubusercontent.com/OP-TEE/optee_os/master/scripts/setup_qemu_optee.sh 377$ chmod 711 setup_qemu_optee.sh 378$ ./setup_qemu_optee.sh 379``` 380 381#### 4.4.3 Compile for QEMU 382During installation a couple of helper scripts were generated, the main reason 383for this is that there is a lot of interdependencies between the different 384software components and it's a bit tricky to point to the correct toolchains and 385to know in which order to build things. 386 387* `build_bios.sh`: This build the BIOS needed in QEMU 388 389* `build_linux.sh`: This is used to build the Linux Kernel. 390 391* `build_optee_client.sh`: This will build OP-TEEs client library. 392 393* `build_optee_linuxkernel.sh`: This will build OP-TEEs Linux Kernel driver (as 394 a module). 395 396* `build_optee_os.sh`: Builds the Trusted OS itself. 397 398* `build.sh`: Builds all software components in the correct order. 399 400* `run_qemu.sh`: This script starts QEMU. 401 402* `serial_0.sh`: Starts listening to QEMUs normal world UART console. 403 404* `serial_1.sh`: Starts listening to QEMUs secure world UART console. 405 406* `update_rootfs.sh`: This script will update rootfs. For example when you have 407 updated normal world component, you will need to put them into rootfs. Calling 408 this script will do so. In case you are creating a new Trusted Application, 409 you must also edit filelist-tee.text in the gen_rootfs folder accordingly. 410 411To build everything you will need to run the script `build.sh`, which will build 412all gits and in the correct order. 413 414#### 4.4.4 Boot and run QEMU and OP-TEE 415To run this you need to lunch two consoles for the UARTs and one console for 416QEMU itself, so in separate shell windows run: 417``` 418$ ./serial_0.sh 419``` 420``` 421$ ./serial_1.sh 422``` 423and finally 424``` 425$ ./run_qemu.sh 426... 427QEMU 2.1.50 monitor - type 'help' for more information 428(qemu) c 429``` 430 431In the window for serial_0 you will now get the normal world console and here 432you need to load and OP-TEEs Linux Kernel driver and also load tee-supplicant. 433This is done by the following lines: 434 435``` 436$ root@Vexpress:/ modprobe optee_armtz 437$ root@Vexpress:/ tee-supplicant & 438``` 439 440--- 441### 4.5 STMicroelectronics boards 442Currently OP-TEE is supported on Orly-2 (b2020-h416) and Cannes family (b2120 443both h310 and h410 chip). 444 445#### 4.5.1 Get the compiler for Orly-2 446Will be written soon. 447 448#### 4.5.2 Download the source code 449See section "4.1.2 Download the source code". 450 451#### 4.5.3 Build for Orly-2 452Will be written soon. 453 454For Orly-2 do as follows 455``` 456$ PLATFORM_FLAVOR=orly2 CROSS_COMPILE=arm-linux-gnueabihf- make 457``` 458 459For Cannes family do as follows 460``` 461$ PLATFORM_FLAVOR=cannes CROSS_COMPILE=arm-linux-gnueabihf- make 462``` 463 464#### 4.5.4 Prepare and install the images 465Will be written soon. 466 467For Orly-2 do as follows 468``` 469To be written. 470``` 471 472For Cannes family do as follows 473``` 474To be written. 475``` 476 477#### 4.5.5 Boot and run the software 478Will be written soon. All magic with STM and so on must be stated here. 479 480For Orly-2 do as follows 481``` 482To be written. 483``` 484 485For Cannes family do as follows 486``` 487To be written. 488``` 489 490--- 491### 4.6 Allwinner A80 492Allwinner A80 platform has been supported in OP-TEE since mid December 2014. 493#### 4.6.1 Get the compiler and source 494Follow the instructions in the "4.1 Basic setup". 495 496#### 4.6.2 Build 497``` 498$ cd optee_os 499$ export PLATFORM=sunxi 500$ export CROSS_COMPILE=arm-linux-gnueabihf- 501$ make 502``` 503 504#### 4.6.3 Prepare the images to run on A80 Board 505 506Download Allwinner A80 platform SDK. 507The SDK refer to Allwinner A80 platform SDK root directory. 508A80 SDK directory tree like this: 509``` 510SDK/ 511 Android 512 lichee 513``` 514Android include all Android source code, 515lichee include bootloader and linux kernel. 516 517##### 4.6.3.1 Copy OP-TEE output to package directory 518copy the OP-TEE output binary to SDK/lichee/tools/pack/sun9i/bin 519``` 520$ cd optee_os 521$ cp ./out/arm32-plat-sunxi/core/tee.bin SDK/lichee/tools/pack/sun9i/bin 522``` 523 524##### 4.6.3.2 Build linux kernel 525In lichee directory, Type the following commands: 526``` 527$ cd SDK/lichee 528$ ./build.sh 529``` 530 531##### 4.6.3.3 Build Android 532In Android directory, Type the following commands: 533``` 534$ cd SDK/android 535$ extract-bsp 536$ make -j 537``` 538 539##### 4.6.3.4 Create Android image 540In andoid directory, Type the following commands: 541``` 542$ cd SDK/android 543$ pack 544``` 545The output image will been signed internally when pack. 546The output image name is a80_android_board.img. 547 548##### 4.6.3.5 Download Android image 549Use Allwinner PhoenixSuit tool to download to A80 board. 550Choose the output image(a80_android_board.img), 551Choose download, 552Wait for the download to complete. 553 554#### 4.6.4 Boot and run the software on A80 Board 555When the host platform is Windows, Use a console application 556to connect A80 board uart0. In the console window, 557You can install OP-TEE linux kernel driver optee.ko, 558Load OP-TEE-Client daemon tee-supplicant, 559Run OP-TEE example hello world application. 560This is done by the following lines: 561``` 562$ insmod /system/vendor/modules/optee.ko 563$ /system/bin/tee-supplicant & 564$ /system/bin/tee-helloworld 565``` 566Enjoying OP-TEE on A80 board. 567 568## 5. Coding standards 569In this project we are trying to adhere to the same coding convention as used in 570the Linux kernel (see 571[CodingStyle](https://www.kernel.org/doc/Documentation/CodingStyle)). We achieve this by running 572[checkpatch](http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/scripts/checkpatch.pl) from Linux kernel. 573However there are a few exceptions that we had to make since the code also 574follows GlobalPlatform standards. The exceptions are as follows: 575 576- CamelCase for GlobalPlatform types are allowed. 577- And we also exclude checking third party code that we might use in this 578 project, such as LibTomCrypt, MPA, newlib (not in this particular git, but 579 those are also part of the complete TEE solution). The reason for excluding 580 and not fixing third party code is because we would probably deviate too much 581 from upstream and therefore it would be hard to rebase against those projects 582 later on (and we don't expect that it is easy to convince other software 583 projects to change coding style). 584 585### 5.1 checkpatch 586Since checkpatch is licensed under the terms of GNU GPL License Version 2, we 587cannot include this script directly into this project. Therefore we have 588written the Makefile so you need to explicitly point to the script by exporting 589an environment variable, namely CHECKPATCH. So, suppose that the source code for 590the Linux kernel is at `$HOME/devel/linux`, then you have to export like follows: 591 592 $ export CHECKPATCH=$HOME/devel/linux/scripts/checkpatch.pl 593thereafter it should be possible to use one of the different checkpatch targets 594in the [Makefile](Makefile). There are targets for checking all files, checking 595against latest commit, against a certain base-commit etc. For the details, read 596the [Makefile](Makefile). 597