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 use crate::{Error, ErrorKind, Result};
19 use optee_utee_sys as raw;
20 use std::marker;
21 
22 pub struct Parameters(pub Parameter, pub Parameter, pub Parameter, pub Parameter);
23 
24 impl Parameters {
from_raw(tee_params: &mut [raw::TEE_Param; 4], param_types: u32) -> Self25     pub fn from_raw(tee_params: &mut [raw::TEE_Param; 4], param_types: u32) -> Self {
26         let (f0, f1, f2, f3) = ParamTypes::from(param_types).into_flags();
27         let p0 = Parameter::from_raw(&mut tee_params[0], f0);
28         let p1 = Parameter::from_raw(&mut tee_params[1], f1);
29         let p2 = Parameter::from_raw(&mut tee_params[2], f2);
30         let p3 = Parameter::from_raw(&mut tee_params[3], f3);
31 
32         Parameters(p0, p1, p2, p3)
33     }
34 }
35 
36 pub struct ParamValue<'parameter> {
37     raw: *mut raw::Value,
38     param_type: ParamType,
39     _marker: marker::PhantomData<&'parameter mut u32>,
40 }
41 
42 impl<'parameter> ParamValue<'parameter> {
a(&self) -> u3243     pub fn a(&self) -> u32 {
44         unsafe { (*self.raw).a }
45     }
46 
b(&self) -> u3247     pub fn b(&self) -> u32 {
48         unsafe { (*self.raw).b }
49     }
50 
set_a(&mut self, a: u32)51     pub fn set_a(&mut self, a: u32) {
52         unsafe {
53             (*self.raw).a = a;
54         }
55     }
56 
set_b(&mut self, b: u32)57     pub fn set_b(&mut self, b: u32) {
58         unsafe {
59             (*self.raw).b = b;
60         }
61     }
62 
param_type(&self) -> ParamType63     pub fn param_type(&self) -> ParamType {
64         self.param_type
65     }
66 }
67 
68 pub struct ParamMemref<'parameter> {
69     raw: *mut raw::Memref,
70     param_type: ParamType,
71     _marker: marker::PhantomData<&'parameter mut [u8]>,
72 }
73 
74 impl<'parameter> ParamMemref<'parameter> {
buffer(&mut self) -> &mut [u8]75     pub fn buffer(&mut self) -> &mut [u8] {
76         unsafe {
77             std::slice::from_raw_parts_mut((*self.raw).buffer as *mut u8, (*self.raw).size as usize)
78         }
79     }
80 
param_type(&self) -> ParamType81     pub fn param_type(&self) -> ParamType {
82         self.param_type
83     }
84 
raw(&mut self) -> *mut raw::Memref85     pub fn raw(&mut self) -> *mut raw::Memref {
86         self.raw
87     }
88 
set_updated_size(&mut self, size: usize)89     pub fn set_updated_size(&mut self, size: usize) {
90         unsafe { (*self.raw).size = size as u32};
91     }
92 }
93 
94 pub struct Parameter {
95     pub raw: *mut raw::TEE_Param,
96     pub param_type: ParamType,
97 }
98 
99 impl Parameter {
from_raw(ptr: *mut raw::TEE_Param, param_type: ParamType) -> Self100     pub fn from_raw(ptr: *mut raw::TEE_Param, param_type: ParamType) -> Self {
101         Self {
102             raw: ptr,
103             param_type: param_type,
104         }
105     }
106 
as_value(&mut self) -> Result<ParamValue>107     pub unsafe fn as_value(&mut self) -> Result<ParamValue> {
108         match self.param_type {
109             ParamType::ValueInput | ParamType::ValueInout | ParamType::ValueOutput => {
110                 Ok(ParamValue {
111                     raw: &mut (*self.raw).value,
112                     param_type: self.param_type,
113                     _marker: marker::PhantomData,
114                 })
115             }
116             _ => Err(Error::new(ErrorKind::BadParameters)),
117         }
118     }
119 
as_memref(&mut self) -> Result<ParamMemref>120     pub unsafe fn as_memref(&mut self) -> Result<ParamMemref> {
121         match self.param_type {
122             ParamType::MemrefInout | ParamType::MemrefInput | ParamType::MemrefOutput => {
123                 Ok(ParamMemref {
124                     raw: &mut (*self.raw).memref,
125                     param_type: self.param_type,
126                     _marker: marker::PhantomData,
127                 })
128             }
129             _ => Err(Error::new(ErrorKind::BadParameters)),
130         }
131     }
132 
raw(&self) -> *mut raw::TEE_Param133     pub fn raw(&self) -> *mut raw::TEE_Param {
134         self.raw
135     }
136 }
137 
138 pub struct ParamTypes(u32);
139 
140 impl ParamTypes {
into_flags(&self) -> (ParamType, ParamType, ParamType, ParamType)141     pub fn into_flags(&self) -> (ParamType, ParamType, ParamType, ParamType) {
142         (
143             (0x000fu32 & self.0).into(),
144             ((0x00f0u32 & self.0) >> 4).into(),
145             ((0x0f00u32 & self.0) >> 8).into(),
146             ((0xf000u32 & self.0) >> 12).into(),
147         )
148     }
149 }
150 
151 impl From<u32> for ParamTypes {
from(value: u32) -> Self152     fn from(value: u32) -> Self {
153         ParamTypes(value)
154     }
155 }
156 
157 #[derive(Copy, Clone)]
158 pub enum ParamType {
159     None = 0,
160     ValueInput = 1,
161     ValueOutput = 2,
162     ValueInout = 3,
163     MemrefInput = 5,
164     MemrefOutput = 6,
165     MemrefInout = 7,
166 }
167 
168 impl From<u32> for ParamType {
from(value: u32) -> Self169     fn from(value: u32) -> Self {
170         match value {
171             0 => ParamType::None,
172             1 => ParamType::ValueInput,
173             2 => ParamType::ValueOutput,
174             3 => ParamType::ValueInout,
175             5 => ParamType::MemrefInput,
176             6 => ParamType::MemrefOutput,
177             7 => ParamType::MemrefInout,
178             _ => ParamType::None,
179         }
180     }
181 }
182