m4sterchain commented on code in PR #178:
URL: 
https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/178#discussion_r2033115250


##########
optee-utee/src/tee_parameter.rs:
##########
@@ -0,0 +1,302 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::ParamType;
+use core::ops::{Index, IndexMut};
+use optee_utee_sys as raw;
+
+#[derive(Copy, Clone, Debug)]
+pub enum ParamIndex {
+    Arg0,
+    Arg1,
+    Arg2,
+    Arg3,
+}
+
+impl ParamIndex {
+    fn to_usize(self) -> usize {
+        match self {
+            ParamIndex::Arg0 => 0,
+            ParamIndex::Arg1 => 1,
+            ParamIndex::Arg2 => 2,
+            ParamIndex::Arg3 => 3,
+        }
+    }
+}
+
+enum ParamContent<'a> {
+    None,
+    MemrefInput {
+        buffer: &'a [u8],
+    },
+    MemrefOutput {
+        buffer: &'a mut [u8],
+        written: usize,
+    },
+    MemrefInout {
+        buffer: &'a mut [u8],
+        written: usize,
+    },
+    ValueInput {
+        a: u32,
+        b: u32,
+    },
+    ValueOutput {
+        a: u32,
+        b: u32,
+    },
+    ValueInout {
+        a: u32,
+        b: u32,
+    },
+}
+
+pub struct Param<'a> {
+    content: ParamContent<'a>,
+}
+
+impl<'a> Param<'a> {
+    fn new() -> Self {
+        Self {
+            content: ParamContent::None,
+        }
+    }
+
+    pub fn written_slice(&self) -> Option<&[u8]> {
+        match &self.content {
+            ParamContent::MemrefOutput { buffer, written } => 
Some(&buffer[..*written]),
+            ParamContent::MemrefInout { buffer, written } => 
Some(&buffer[..*written]),
+            _ => None,
+        }
+    }
+
+    pub fn output_value(&self) -> Option<(u32, u32)> {
+        match &self.content {
+            ParamContent::ValueOutput { a, b } => Some((*a, *b)),
+            ParamContent::ValueInout { a, b } => Some((*a, *b)),
+            _ => None,
+        }
+    }
+
+    fn get_type(&self) -> ParamType {
+        match &self.content {
+            ParamContent::None => ParamType::None,
+            ParamContent::MemrefInput { .. } => ParamType::MemrefInput,
+            ParamContent::MemrefOutput { .. } => ParamType::MemrefOutput,
+            ParamContent::MemrefInout { .. } => ParamType::MemrefInout,
+            ParamContent::ValueInput { .. } => ParamType::ValueInput,
+            ParamContent::ValueOutput { .. } => ParamType::ValueOutput,
+            ParamContent::ValueInout { .. } => ParamType::ValueInout,
+        }
+    }
+
+    fn get_raw_type(&self) -> u32 {
+        self.get_type() as u32
+    }
+
+    fn as_raw(&mut self) -> raw::TEE_Param {
+        match &mut self.content {
+            ParamContent::None => raw::TEE_Param {
+                memref: raw::Memref {
+                    buffer: core::ptr::null_mut(),
+                    size: 0,
+                },
+            },
+            ParamContent::MemrefInput { buffer } => raw::TEE_Param {
+                memref: raw::Memref {
+                    buffer: (*buffer).as_ptr() as *mut core::ffi::c_void,
+                    size: buffer.len(),
+                },
+            },
+            ParamContent::MemrefOutput { buffer, written: _ } => 
raw::TEE_Param {
+                memref: raw::Memref {
+                    buffer: (*buffer).as_mut_ptr() as *mut core::ffi::c_void,
+                    size: buffer.len(),
+                },
+            },
+            ParamContent::MemrefInout { buffer, written: _ } => raw::TEE_Param 
{
+                memref: raw::Memref {
+                    buffer: (*buffer).as_mut_ptr() as *mut core::ffi::c_void,
+                    size: buffer.len(),
+                },
+            },
+            ParamContent::ValueInput { a, b } => raw::TEE_Param {
+                value: raw::Value { a: *a, b: *b },
+            },
+            ParamContent::ValueInout { a, b } => raw::TEE_Param {
+                value: raw::Value { a: *a, b: *b },
+            },
+            ParamContent::ValueOutput { a, b } => raw::TEE_Param {
+                value: raw::Value { a: *a, b: *b },
+            },
+        }
+    }
+
+    fn update_size_from_raw(&mut self, raw_param: &raw::TEE_Param) {
+        match &mut self.content {
+            ParamContent::MemrefOutput { buffer: _, written } => {
+                *written = unsafe { raw_param.memref.size };
+            }
+            ParamContent::MemrefInout { buffer: _, written } => {
+                *written = unsafe { raw_param.memref.size };
+            }
+            _ => {}
+        }
+    }
+
+    fn update_value_from_raw(&mut self, raw_param: &raw::TEE_Param) {
+        match &mut self.content {
+            ParamContent::ValueInout { a, b } => {
+                *a = unsafe { raw_param.value.a };
+                *b = unsafe { raw_param.value.b };
+            }
+            ParamContent::ValueOutput { a, b } => {
+                *a = unsafe { raw_param.value.a };
+                *b = unsafe { raw_param.value.b };
+            }
+            _ => {}
+        }
+    }
+}
+
+/// The TeeParams struct is used to manage the parameters for TEE commands.
+pub struct TeeParams<'a> {
+    params: [Param<'a>; 4],
+}
+
+impl<'a> TeeParams<'a> {
+    pub fn new() -> Self {
+        Self {
+            params: [Param::new(), Param::new(), Param::new(), Param::new()],
+        }
+    }
+
+    // Method-chaining parameter setters
+    pub fn with_memref_in(mut self, idx: ParamIndex, buffer: &'a [u8]) -> Self 
{
+        self.params[idx.to_usize()].content = ParamContent::MemrefInput { 
buffer };
+        self
+    }
+
+    pub fn with_memref_out(mut self, idx: ParamIndex, buffer: &'a mut [u8]) -> 
Self {
+        self.params[idx.to_usize()].content = ParamContent::MemrefOutput { 
buffer, written: 0 };
+        self
+    }
+
+    pub fn with_memref_inout(mut self, idx: ParamIndex, buffer: &'a mut [u8]) 
-> Self {
+        self.params[idx.to_usize()].content = ParamContent::MemrefInout { 
buffer, written: 0 };
+        self
+    }
+
+    pub fn with_value_in(mut self, idx: ParamIndex, a: u32, b: u32) -> Self {
+        self.params[idx.to_usize()].content = ParamContent::ValueInput { a, b 
};
+        self
+    }
+
+    pub fn with_value_out(mut self, idx: ParamIndex, a: u32, b: u32) -> Self {
+        self.params[idx.to_usize()].content = ParamContent::ValueOutput { a, b 
};
+        self
+    }
+
+    pub fn with_value_inout(mut self, idx: ParamIndex, a: u32, b: u32) -> Self 
{
+        self.params[idx.to_usize()].content = ParamContent::ValueInout { a, b 
};
+        self
+    }
+
+    pub fn set_memref_in(&mut self, idx: ParamIndex, buffer: &'a [u8]) -> &mut 
Self {
+        self.params[idx.to_usize()].content = ParamContent::MemrefInput { 
buffer };
+        self
+    }
+
+    pub fn set_memref_out(&mut self, idx: ParamIndex, buffer: &'a mut [u8]) -> 
&mut Self {
+        self.params[idx.to_usize()].content = ParamContent::MemrefOutput { 
buffer, written: 0 };
+        self
+    }
+
+    pub fn set_memref_inout(&mut self, idx: ParamIndex, buffer: &'a mut [u8]) 
-> &mut Self {
+        self.params[idx.to_usize()].content = ParamContent::MemrefInout { 
buffer, written: 0 };
+        self
+    }
+
+    pub fn set_value_in(&mut self, idx: ParamIndex, a: u32, b: u32) -> &mut 
Self {
+        self.params[idx.to_usize()].content = ParamContent::ValueInput { a, b 
};
+        self
+    }
+
+    pub fn set_value_out(&mut self, idx: ParamIndex, a: u32, b: u32) -> &mut 
Self {
+        self.params[idx.to_usize()].content = ParamContent::ValueOutput { a, b 
};
+        self
+    }
+
+    pub fn set_value_inout(&mut self, idx: ParamIndex, a: u32, b: u32) -> &mut 
Self {
+        self.params[idx.to_usize()].content = ParamContent::ValueInout { a, b 
};
+        self
+    }
+
+    pub(crate) fn raw_param_types(&self) -> u32 {
+        let mut param_types = 0;
+        for (i, param) in self.params.iter().enumerate() {
+            param_types |= param.get_raw_type() << (i * 4);
+        }
+        param_types
+    }
+
+    pub(crate) fn as_raw(&mut self) -> [raw::TEE_Param; 4] {
+        [
+            self.params[0].as_raw(),
+            self.params[1].as_raw(),
+            self.params[2].as_raw(),
+            self.params[3].as_raw(),
+        ]
+    }
+
+    pub(crate) fn update_from_raw(&mut self, raw_params: &[raw::TEE_Param; 4]) 
{
+        // update the content for memref inout/out, and value inout/out
+        for (i, param) in self.params.iter_mut().enumerate() {
+            let raw_param = &raw_params[i];
+            match param.get_type() {
+                ParamType::MemrefOutput => {
+                    param.update_size_from_raw(raw_param);
+                }
+                ParamType::MemrefInout => {
+                    param.update_size_from_raw(raw_param);
+                }
+                ParamType::ValueInput => {
+                    param.update_value_from_raw(raw_param);
+                }
+                ParamType::ValueInout => {
+                    param.update_value_from_raw(raw_param);
+                }
+                _ => {}

Review Comment:
   Should we check against other param types? We can at least ensure 
   1. `ValueInput` content are never changed; if not, raise an error;
   2. out size for MemRef Out/InOut types always within the original bound; if 
not, raise an error;



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org
For additional commands, e-mail: dev-h...@teaclave.apache.org

Reply via email to