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