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 serde::{Deserialize, Serialize};
25 use std::io::Write;
26 use proto::Command;
27 
28 #[ta_create]
create() -> Result<()>29 fn create() -> Result<()> {
30     trace_println!("[+] TA create");
31     Ok(())
32 }
33 
34 #[ta_open_session]
open_session(_params: &mut Parameters) -> Result<()>35 fn open_session(_params: &mut Parameters) -> Result<()> {
36     trace_println!("[+] TA open session");
37     Ok(())
38 }
39 
40 #[ta_close_session]
close_session()41 fn close_session() {
42     trace_println!("[+] TA close session");
43 }
44 
45 #[ta_destroy]
destroy()46 fn destroy() {
47     trace_println!("[+] TA destroy");
48 }
49 
50 #[derive(Serialize, Deserialize, Debug)]
51 struct Point {
52     x: i32,
53     y: i32,
54 }
55 
56 #[ta_invoke_command]
invoke_command(cmd_id: u32, params: &mut Parameters) -> Result<()>57 fn invoke_command(cmd_id: u32, params: &mut Parameters) -> Result<()> {
58     trace_println!("[+] TA invoke command");
59     match Command::from(cmd_id) {
60         Command::DefaultOp => {
61             let mut p = unsafe { params.0.as_memref().unwrap() };
62             let mut buffer = p.buffer();
63             let point = Point { x: 1, y: 2 };
64 
65             // Convert the Point to a JSON string.
66             let serialized = serde_json::to_string(&point).unwrap();
67             let len = buffer.write(serialized.as_bytes()).unwrap();
68 
69             // update size of output buffer
70             unsafe { (*p.raw()).size = len as u32 };
71 
72             // Prints serialized = {"x":1,"y":2}
73             trace_println!("serialized = {}", serialized);
74 
75             // Convert the JSON string back to a Point.
76             let deserialized: Point = serde_json::from_str(&serialized).unwrap();
77 
78             // Prints deserialized = Point { x: 1, y: 2 }
79             trace_println!("deserialized = {:?}", deserialized);
80 
81             Ok(())
82         }
83         _ => Err(Error::new(ErrorKind::BadParameters)),
84     }
85 }
86 
87 // TA configurations
88 const TA_FLAGS: u32 = 0;
89 const TA_DATA_SIZE: u32 = 64 * 1024;
90 const TA_STACK_SIZE: u32 = 4 * 1024;
91 const TA_VERSION: &[u8] = b"0.1\0";
92 const TA_DESCRIPTION: &[u8] = b"This is a serde example.\0";
93 const EXT_PROP_VALUE_1: &[u8] = b"Serde TA\0";
94 const EXT_PROP_VALUE_2: u32 = 0x0010;
95 const TRACE_LEVEL: i32 = 4;
96 const TRACE_EXT_PREFIX: &[u8] = b"TA\0";
97 const TA_FRAMEWORK_STACK_SIZE: u32 = 2048;
98 
99 include!(concat!(env!("OUT_DIR"), "/user_ta_header.rs"));
100