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, Result, Uuid};
19 use optee_utee_sys as raw;
20 #[cfg(not(feature = "std"))]
21 use alloc::vec::Vec;
22 #[cfg(not(feature = "std"))]
23 use alloc::borrow::ToOwned;
24 
25 pub struct LoadablePlugin {
26     uuid: Uuid
27 }
28 
29 impl LoadablePlugin {
new(uuid: &Uuid) -> Self30     pub fn new(uuid: &Uuid) -> Self {
31         Self { uuid: uuid.to_owned() }
32     }
invoke(&mut self, command_id: u32, subcommand_id: u32, data: &[u8]) -> Result<Vec<u8>>33     pub fn invoke(&mut self, command_id: u32, subcommand_id: u32, data: &[u8]) -> Result<Vec<u8>> {
34         let raw_uuid: Uuid = self.uuid;
35         let mut outlen: usize = 0;
36         match unsafe {
37             raw::tee_invoke_supp_plugin(
38                 raw_uuid.as_raw_ptr(),
39                 command_id as u32,
40                 subcommand_id as u32,
41                 data.as_ptr() as _,
42                 data.len(),
43                 &mut outlen as *mut usize,
44             )
45         } {
46             raw::TEE_SUCCESS => {
47                 assert!(outlen <= (data.len()));
48                 let mut outbuf = vec![0; outlen];
49                 outbuf.copy_from_slice(&data[..outlen]);
50 
51                 Ok(outbuf)
52             },
53             code => Err(Error::from_raw_error(code)),
54         }
55 
56     }
57 }
58