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