1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #![no_main]
19 
20 use optee_utee::{
21     ta_close_session, ta_create, ta_destroy, ta_invoke_command, ta_open_session, trace_println,
22 };
23 use optee_utee::{Error, ErrorKind, Parameters, Result};
24 use std::io::Write;
25 use proto::{self, Command};
26 
handle_invoke(command: Command, input: proto::EnclaveInput) -> Result<proto::EnclaveOutput>27 fn handle_invoke(command: Command, input: proto::EnclaveInput) -> Result<proto::EnclaveOutput> {
28     match command {
29         Command::Hello => {
30             let output = proto::EnclaveOutput {
31                 message: format!("Hello, {}", input.message)
32             };
33             Ok(output)
34         },
35         Command::Bye => {
36             let output = proto::EnclaveOutput {
37                 message: format!("Bye, {}", input.message)
38             };
39             Ok(output)
40         },
41         _ => Err(Error::new(ErrorKind::BadParameters)),
42     }
43 }
44 
45 #[ta_create]
create() -> Result<()>46 fn create() -> Result<()> {
47     trace_println!("[+] TA create");
48     Ok(())
49 }
50 
51 #[ta_open_session]
open_session(_params: &mut Parameters) -> Result<()>52 fn open_session(_params: &mut Parameters) -> Result<()> {
53     trace_println!("[+] TA open session");
54     Ok(())
55 }
56 
57 #[ta_close_session]
close_session()58 fn close_session() {
59     trace_println!("[+] TA close session");
60 }
61 
62 #[ta_destroy]
destroy()63 fn destroy() {
64     trace_println!("[+] TA destroy");
65 }
66 
67 #[ta_invoke_command]
invoke_command(cmd_id: u32, params: &mut Parameters) -> Result<()>68 fn invoke_command(cmd_id: u32, params: &mut Parameters) -> Result<()> {
69     trace_println!("[+] TA invoke command");
70     let mut p0 = unsafe { params.0.as_memref().unwrap()};
71     let mut p1 = unsafe { params.1.as_memref().unwrap()};
72     let mut p2 = unsafe { params.2.as_value().unwrap() };
73 
74     let input: proto::EnclaveInput = proto::serde_json::from_slice(p0.buffer()).unwrap();
75     let output = handle_invoke(Command::from(cmd_id), input).unwrap();
76 
77     let output_vec = proto::serde_json::to_vec(&output).unwrap();
78     p1.buffer().write(&output_vec).unwrap();
79     p2.set_a(output_vec.len() as u32);
80 
81     Ok(())
82 }
83 
84 // TA configurations
85 const TA_FLAGS: u32 = 0;
86 const TA_DATA_SIZE: u32 = 64 * 1024;
87 const TA_STACK_SIZE: u32 = 4 * 1024;
88 const TA_VERSION: &[u8] = b"0.1\0";
89 const TA_DESCRIPTION: &[u8] = b"This is a hello world example.\0";
90 const EXT_PROP_VALUE_1: &[u8] = b"Hello World TA\0";
91 const EXT_PROP_VALUE_2: u32 = 0x0010;
92 const TRACE_LEVEL: i32 = 4;
93 const TRACE_EXT_PREFIX: &[u8] = b"TA\0";
94 const TA_FRAMEWORK_STACK_SIZE: u32 = 2048;
95 
96 include!(concat!(env!("OUT_DIR"), "/user_ta_header.rs"));
97