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 optee_teec_sys as raw; 19 use libc::{c_char}; 20 use crate::{Result, Error, ErrorKind}; 21 22 #[repr(C)] 23 pub struct PluginMethod { 24 pub name: *const c_char, 25 pub uuid: raw::TEEC_UUID, 26 pub init: fn() -> Result<()>, 27 pub invoke: fn( 28 cmd: u32, 29 sub_cmd: u32, 30 data: *mut c_char, 31 in_len: u32, 32 out_len: *mut u32, 33 ) -> Result<()>, 34 } 35 36 /// struct PluginParameters { 37 /// @cmd: u32, plugin cmd, defined in proto/ 38 /// @sub_cmd: u32, plugin subcmd, defined in proto/ 39 /// @inout: &'a mut [u8], input/output buffer shared with TA and plugin 40 /// @outlen, length of output sent to TA 41 /// } 42 pub struct PluginParameters<'a> { 43 pub cmd: u32, 44 pub sub_cmd: u32, 45 pub inout: &'a mut [u8], 46 outlen: usize, 47 } 48 impl<'a> PluginParameters<'a> { new(cmd: u32, sub_cmd: u32, inout: &'a mut [u8]) -> Self49 pub fn new(cmd: u32, sub_cmd: u32, inout: &'a mut [u8]) -> Self { 50 Self { 51 cmd: cmd, 52 sub_cmd: sub_cmd, 53 inout: inout, 54 outlen: 0 as usize, 55 } 56 } set_buf_from_slice(&mut self, sendslice: &[u8]) -> Result<()>57 pub fn set_buf_from_slice(&mut self, sendslice: &[u8]) -> Result<()> { 58 if self.inout.len() < sendslice.len() { 59 println!("Overflow: Input length is less than output length"); 60 return Err(Error::new(ErrorKind::Security)); 61 } 62 self.outlen = sendslice.len() as usize; 63 self.inout[..self.outlen].copy_from_slice(&sendslice); 64 Ok(()) 65 } get_out_slice(&self) -> &[u8]66 pub fn get_out_slice(&self) -> &[u8] { 67 &self.inout[..self.outlen] 68 } 69 } 70