Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
resolver = "3"
members = [
"optee-teec",
"optee-teec-macros",
"optee-teec-plugin-bindgen",
"optee-teec-sys",
"optee-teec-systest",
"optee-utee",
Expand Down Expand Up @@ -52,8 +52,10 @@ uuid = { version = "1.23", default-features = false }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
quote = "1.0"
syn = { version = "2.0", features = ["full"] }
proc-macro2 = "1.0.92"
libc = "0.2"
rand = "0.10"
once_cell = "1.20.2"
serde = { version = "1.0.228" }
serde_json = { version = "1.0.149" }
log = "0.4.29"
215 changes: 0 additions & 215 deletions crates/optee-teec-macros/src/lib.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
# under the License.

[package]
name = "optee-teec-macros"
description = "Procedural macros for TEE client API."
name = "optee-teec-plugin-bindgen"
description = "generate binding for teec plugin"
version.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
edition.workspace = true

[lib]
proc-macro = true

[dependencies]
quote.workspace = true
proc-macro2.workspace = true
uuid.workspace = true
syn.workspace = true
140 changes: 140 additions & 0 deletions crates/optee-teec-plugin-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// 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 std::path::PathBuf;

pub struct Config {
name: String,
uuid: uuid::Uuid,
init_fn_name: String,
invoke_fn_name: String,
dest: Option<PathBuf>,
}

impl Config {
pub fn new(name: &str, uuid: uuid::Uuid, init_fn_name: &str, invoke_fn_name: &str) -> Self {
Self {
name: name.to_owned(),
uuid,
init_fn_name: init_fn_name.to_owned(),
invoke_fn_name: invoke_fn_name.to_owned(),
dest: None,
}
}
pub fn build(&self) -> std::io::Result<()> {
let codes = generate_binding(
&self.name,
&self.uuid,
&self.init_fn_name,
&self.invoke_fn_name,
)
.to_string();
let out_path = self.get_out_path();
if let Ok(v) = std::fs::read(&out_path)
&& v.eq(codes.as_bytes())
{
return Ok(());
}

if let Some(parent_dir) = out_path.parent() {
std::fs::create_dir_all(parent_dir)?;
}
std::fs::write(out_path, codes.as_bytes())
}
}

impl Config {
fn get_out_path(&self) -> PathBuf {
match self.dest.as_ref() {
Some(v) => v.clone(),
None => {
let out_dir = PathBuf::from(
std::env::var("OUT_DIR").expect("Infallible when using in build.rs"),
);
out_dir.join("plugin_static.rs")
}
}
}
}

pub fn generate_binding(
name: &str,
uuid: &uuid::Uuid,
init_fn_name: &str,
invoke_fn_name: &str,
) -> proc_macro2::TokenStream {
let (uuid_f1, uuid_f2, uuid_f3, uuid_f4) = uuid.as_fields();
let name_bytes_with_null = format!("{}\0", name);
let init_fn_name = quote::format_ident!("{}", init_fn_name);
let invoke_fn_name = quote::format_ident!("{}", invoke_fn_name);
quote::quote! {
const _: () = {
use core::ffi::{c_char, c_void};
use optee_teec::raw::{PluginMethod, TEEC_Result, TEEC_SUCCESS, TEEC_UUID, size_t};
const _: fn() -> optee_teec::Result<()> = #init_fn_name;
const _: fn(_: &mut optee_teec::PluginParameters) -> optee_teec::Result<()> = #invoke_fn_name;

unsafe extern "C" fn __plugin_init() -> TEEC_Result {
match #init_fn_name() {
Ok(()) => TEEC_SUCCESS,
Err(err) => err.raw_code(),
}
}

unsafe extern "C" fn __plugin_invoke(
cmd: u32,
sub_cmd: u32,
data: *mut c_void,
in_len: size_t,
out_len: *mut size_t,
) -> TEEC_Result {
let mut parameter = match unsafe {
optee_teec::PluginParameters::from_raw(
cmd,
sub_cmd,
data as *mut c_char,
in_len,
out_len,
)
} {
Ok(v) => v,
Err(err) => return err.raw_code(),
};

match #invoke_fn_name(&mut parameter) {
Ok(()) => TEEC_SUCCESS,
Err(err) => err.raw_code(),
}
}

static PLUGIN_NAME: &str = #name_bytes_with_null;

#[unsafe(no_mangle)]
pub static mut plugin_method: PluginMethod = PluginMethod {
name: PLUGIN_NAME.as_ptr() as *const _,
uuid: TEEC_UUID {
timeLow: #uuid_f1,
timeMid: #uuid_f2,
timeHiAndVersion: #uuid_f3,
clockSeqAndNode: [#(#uuid_f4),*],
},
init: __plugin_init,
invoke: __plugin_invoke,
};
};
}
}
Loading
Loading