From c6b591c6c10fca5b538d4c70d08eb218a3dd70c8 Mon Sep 17 00:00:00 2001 From: ivila <390810839@qq.com> Date: Thu, 19 Dec 2024 14:28:52 +0800 Subject: [PATCH] Add optee-utee-build crate 1. add optee-utee-build crate to make building process simpler 2. use optee-utee-build to build hello_world example 3. fix pipeline --- .github/workflows/ci.yml | 4 +- .licenserc.yaml | 1 + examples/hello_world-rs/ta/Cargo.toml | 2 +- examples/hello_world-rs/ta/Makefile | 2 +- examples/hello_world-rs/ta/build.rs | 86 +---- examples/hello_world-rs/ta/src/main.rs | 12 - optee-utee-build/Cargo.toml | 32 ++ optee-utee-build/src/builder.rs | 119 ++++++ optee-utee-build/src/code_generator.rs | 355 ++++++++++++++++++ optee-utee-build/src/error.rs | 42 +++ optee-utee-build/src/lib.rs | 32 ++ optee-utee-build/src/linker.rs | 175 +++++++++ optee-utee-build/src/ta_config.rs | 137 +++++++ .../test_files/test_edition_2024_result.rs | 92 +++++ .../test_edition_before_2024_result.rs | 86 ++--- 15 files changed, 1031 insertions(+), 146 deletions(-) create mode 100644 optee-utee-build/Cargo.toml create mode 100644 optee-utee-build/src/builder.rs create mode 100644 optee-utee-build/src/code_generator.rs create mode 100644 optee-utee-build/src/error.rs create mode 100644 optee-utee-build/src/lib.rs create mode 100644 optee-utee-build/src/linker.rs create mode 100644 optee-utee-build/src/ta_config.rs create mode 100644 optee-utee-build/test_files/test_edition_2024_result.rs rename examples/hello_world-rs/ta/ta_static.rs => optee-utee-build/test_files/test_edition_before_2024_result.rs (68%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 72b59cc4..88230e1f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -196,7 +196,7 @@ jobs: curl https://storage.googleapis.com/git-repo-downloads/repo-1 > ~/bin/repo && chmod a+x ~/bin/repo export PATH=~/bin:$PATH mkdir -p ~/optee-qemuv8 && cd ~/optee-qemuv8 && - repo init -u https://github.com/OP-TEE/manifest.git -m qemu_v8.xml && + repo init -u https://github.com/OP-TEE/manifest.git -b 4.4.0 -m qemu_v8.xml && repo sync -j4 --no-clone-bundle - name: Build images and run tests run: | @@ -227,7 +227,7 @@ jobs: curl https://storage.googleapis.com/git-repo-downloads/repo-1 > ~/bin/repo && chmod a+x ~/bin/repo export PATH=~/bin:$PATH mkdir -p ~/optee-qemuv8 && cd ~/optee-qemuv8 && - repo init -u https://github.com/OP-TEE/manifest.git -m qemu_v8.xml && + repo init -u https://github.com/OP-TEE/manifest.git -b 4.4.0 -m qemu_v8.xml && repo sync -j4 --no-clone-bundle - name: Build images and run tests run: | diff --git a/.licenserc.yaml b/.licenserc.yaml index 0d6d2dc1..1137091a 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -31,3 +31,4 @@ header: - 'DISCLAIMER-WIP' - '*.json' - 'examples/tls_server-rs/ta/test-ca/**' + - 'optee-utee-build/test_files/**' diff --git a/examples/hello_world-rs/ta/Cargo.toml b/examples/hello_world-rs/ta/Cargo.toml index badab895..820925e6 100644 --- a/examples/hello_world-rs/ta/Cargo.toml +++ b/examples/hello_world-rs/ta/Cargo.toml @@ -30,8 +30,8 @@ optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys" } optee-utee = { path = "../../../optee-utee" } [build_dependencies] -uuid = { version = "1.6.1", default-features = false } proto = { path = "../proto" } +optee-utee-build = { path = "../../../optee-utee-build" } [profile.release] panic = "abort" diff --git a/examples/hello_world-rs/ta/Makefile b/examples/hello_world-rs/ta/Makefile index 72a9cb3f..c1c4344a 100644 --- a/examples/hello_world-rs/ta/Makefile +++ b/examples/hello_world-rs/ta/Makefile @@ -20,7 +20,7 @@ UUID ?= $(shell cat "../uuid.txt") TARGET ?= aarch64-unknown-linux-gnu CROSS_COMPILE ?= aarch64-linux-gnu- OBJCOPY := $(CROSS_COMPILE)objcopy -LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)ld.bfd\" +LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\" TA_SIGN_KEY ?= $(TA_DEV_KIT_DIR)/keys/default_ta.pem SIGN := $(TA_DEV_KIT_DIR)/scripts/sign_encrypt.py diff --git a/examples/hello_world-rs/ta/build.rs b/examples/hello_world-rs/ta/build.rs index fa0d1954..8c440273 100644 --- a/examples/hello_world-rs/ta/build.rs +++ b/examples/hello_world-rs/ta/build.rs @@ -16,88 +16,10 @@ // under the License. use proto; -use std::env; -use std::fs::File; -use std::io::{BufRead, BufReader, Write}; -use std::path::{Path, PathBuf}; -use uuid::Uuid; +use optee_utee_build::{TAConfig, RustEdition, Error}; -fn main() -> std::io::Result<()> { - let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); +fn main() -> Result<(), Error> { + let config = TAConfig::new_standard("0.1", "This is a hello world example.", "Hello World TA"); + optee_utee_build::build(RustEdition::Before2024, proto::UUID, config) - let mut buffer = File::create(out.join("user_ta_header.rs"))?; - buffer.write_all(include_bytes!("ta_static.rs"))?; - - let tee_uuid = Uuid::parse_str(proto::UUID).unwrap(); - let (time_low, time_mid, time_hi_and_version, clock_seq_and_node) = tee_uuid.as_fields(); - - write!(buffer, "\n")?; - write!( - buffer, - "const TA_UUID: optee_utee_sys::TEE_UUID = optee_utee_sys::TEE_UUID {{ - timeLow: {:#x}, - timeMid: {:#x}, - timeHiAndVersion: {:#x}, - clockSeqAndNode: {:#x?}, -}};", - time_low, time_mid, time_hi_and_version, clock_seq_and_node - )?; - - let mut aarch64_flag = true; - match env::var("TARGET_TA") { - Ok(ref v) if v == "arm-unknown-linux-gnueabihf" || v == "arm-unknown-optee" => { - println!("cargo:rustc-link-arg=--no-warn-mismatch"); - aarch64_flag = false; - }, - _ => {} - }; - - let optee_os_dir = env::var("TA_DEV_KIT_DIR").unwrap(); - let search_path = Path::new(&optee_os_dir).join("lib"); - - let optee_os_path = &PathBuf::from(optee_os_dir.clone()); - let mut ta_lds = File::create(out.join("ta.lds"))?; - let f = File::open(optee_os_path.join("src/ta.ld.S"))?; - let f = BufReader::new(f); - - for line in f.lines() { - let l = line?; - - if aarch64_flag { - if l.starts_with('#') || - l == "OUTPUT_FORMAT(\"elf32-littlearm\")" || - l == "OUTPUT_ARCH(arm)" { - continue; - } - } else { - if l.starts_with('#') || - l == "OUTPUT_FORMAT(\"elf64-littleaarch64\")" || - l == "OUTPUT_ARCH(aarch64)" { - continue; - } - } - - if l == "\t. = ALIGN(4096);" { - write!(ta_lds, "\t. = ALIGN(65536);\n")?; - } else { - write!(ta_lds, "{}\n", l)?; - } - } - - println!("cargo:rustc-link-search={}", out.display()); - println!("cargo:rerun-if-changed=ta.lds"); - - println!("cargo:rustc-link-search={}", search_path.display()); - println!("cargo:rustc-link-lib=static=utee"); - println!("cargo:rustc-link-lib=static=utils"); - println!("cargo:rustc-link-arg=-Tta.lds"); - println!("cargo:rustc-link-arg=-e__ta_entry"); - println!("cargo:rustc-link-arg=-pie"); - println!("cargo:rustc-link-arg=-Os"); - println!("cargo:rustc-link-arg=--sort-section=alignment"); - - let mut dyn_list = File::create(out.join("dyn_list"))?; - write!(dyn_list, "{{ __elf_phdr_info; trace_ext_prefix; trace_level; ta_head; }};\n")?; - println!("cargo:rustc-link-arg=--dynamic-list=dyn_list"); - Ok(()) } diff --git a/examples/hello_world-rs/ta/src/main.rs b/examples/hello_world-rs/ta/src/main.rs index 8ef0567b..e29f9d68 100644 --- a/examples/hello_world-rs/ta/src/main.rs +++ b/examples/hello_world-rs/ta/src/main.rs @@ -63,16 +63,4 @@ fn invoke_command(cmd_id: u32, params: &mut Parameters) -> Result<()> { } } -// TA configurations -const TA_FLAGS: u32 = 0; -const TA_DATA_SIZE: u32 = 32 * 1024; -const TA_STACK_SIZE: u32 = 2 * 1024; -const TA_VERSION: &[u8] = b"0.1\0"; -const TA_DESCRIPTION: &[u8] = b"This is a hello world example.\0"; -const EXT_PROP_VALUE_1: &[u8] = b"Hello World TA\0"; -const EXT_PROP_VALUE_2: u32 = 0x0010; -const TRACE_LEVEL: i32 = 4; -const TRACE_EXT_PREFIX: &[u8] = b"TA\0"; -const TA_FRAMEWORK_STACK_SIZE: u32 = 2048; - include!(concat!(env!("OUT_DIR"), "/user_ta_header.rs")); diff --git a/optee-utee-build/Cargo.toml b/optee-utee-build/Cargo.toml new file mode 100644 index 00000000..9e14ea22 --- /dev/null +++ b/optee-utee-build/Cargo.toml @@ -0,0 +1,32 @@ +# 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. + +[package] +name = "optee-utee-build" +version = "0.2.0" +authors = ["Teaclave Contributors "] +license = "Apache-2.0" +repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git" +edition = "2018" +description = "Build tool for TA" + +[dependencies] +uuid = "1.11.0" +quote = "1.0.37" +proc-macro2 = "1.0.92" +syn = "2.0.90" +prettyplease = "0.2.25" diff --git a/optee-utee-build/src/builder.rs b/optee-utee-build/src/builder.rs new file mode 100644 index 00000000..a12bcdf5 --- /dev/null +++ b/optee-utee-build/src/builder.rs @@ -0,0 +1,119 @@ +// 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::fs::File; +use std::io::Write; +use std::path::PathBuf; + +use crate::HeaderFileGenerator; +use crate::Error; +use crate::RustEdition; +use crate::TAConfig; +use crate::{Linker, LinkerType}; + +const DEFAULT_HEADER_FILE_NAME: &str = "user_ta_header.rs"; + +/// The Builder of TA, use it to handle file generation and linking stuff +/// +/// Usage: +/// +/// ```no_run +/// use optee_utee_build::{TAConfig, Builder, RustEdition}; +/// # use optee_utee_build::Error; +/// # fn main() -> Result<(), Error> { +/// let ta_config = TAConfig::new_standard("0.1.0", "example", "example"); +/// let uuid = "d93c2970-b1a6-4b86-90ac-b42830e78d9b"; +/// Builder::new(RustEdition::Before2024, ta_config).build(uuid)?; +/// # Ok(()) +/// # } +/// ``` +/// +/// There are some difference when cargo use different linkers, we will try +/// to detect the linker automatically, you can use set it manually if you +/// met some problems with it. +/// ```no_run +/// use optee_utee_build::{TAConfig, Builder, RustEdition, LinkerType}; +/// # use optee_utee_build::Error; +/// # fn main() -> Result<(), Error> { +/// let ta_config = TAConfig::new_standard("0.1.0", "example", "example"); +/// let uuid = "d93c2970-b1a6-4b86-90ac-b42830e78d9b"; +/// Builder::new(RustEdition::Before2024, ta_config).linker_type(LinkerType::LD).build(uuid)?; +/// # Ok(()) +/// # } +/// ``` +pub struct Builder { + out_dir: Option, + edition: RustEdition, + header_file_name: Option, + ta_config: TAConfig, + linker_type: Option, +} + +impl Builder { + pub fn new(edition: RustEdition, ta_config: TAConfig) -> Self { + Self { + out_dir: Option::None, + header_file_name: Option::None, + linker_type: Option::None, + edition, + ta_config, + } + } + pub fn out_dir>(mut self, path: P) -> Self { + self.out_dir = Option::Some(path.into()); + self + } + pub fn header_file_name>(mut self, file_name: S) -> Self { + self.header_file_name = Option::Some(file_name.into()); + self + } + pub fn linker_type(mut self, linker_type: LinkerType) -> Self { + self.linker_type = Option::Some(linker_type); + self + } + pub fn build(self, uuid: &str) -> Result<(), Error> { + let out_dir = match self.out_dir.clone() { + Some(v) => v, + None => PathBuf::from(std::env::var("OUT_DIR")?), + }; + self.write_header_file(out_dir.clone(), uuid)?; + self.link(out_dir)?; + Ok(()) + } +} + +impl Builder { + fn write_header_file(&self, out: PathBuf, uuid: &str) -> Result<(), Error> { + let out_header_file_name = out.join(match self.header_file_name.as_ref() { + Some(v) => v.as_str(), + None => DEFAULT_HEADER_FILE_NAME, + }); + let mut buffer = File::create(out_header_file_name.clone())?; + let header_codes = + HeaderFileGenerator::new(self.edition.clone()).generate(&self.ta_config, uuid)?; + buffer.write_all(header_codes.as_bytes())?; + Ok(()) + } + + fn link(&self, out_dir: PathBuf) -> Result<(), Error> { + let linker = match self.linker_type.as_ref() { + Option::Some(v) => Linker::new(v.clone()), + Option::None => Linker::auto(), + }; + linker.link_all(out_dir) + } +} diff --git a/optee-utee-build/src/code_generator.rs b/optee-utee-build/src/code_generator.rs new file mode 100644 index 00000000..7e469251 --- /dev/null +++ b/optee-utee-build/src/code_generator.rs @@ -0,0 +1,355 @@ +// 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::Error; +use crate::{PropertyValue, TAConfig}; +use quote::{format_ident, quote}; +use std::str::FromStr; + +#[derive(Clone)] +pub enum RustEdition { + Before2024, + Edition2024, +} + +/// Generator of head file, use it to generate a header file and then included in user codes. +/// +/// Use only if you just want to generate a header file, and do the linking job yourself, +/// or you should use Builder instead. +/// Examples: +/// ```rust +/// use optee_utee_build::{HeaderFileGenerator, TAConfig, RustEdition}; +/// # use optee_utee_build::Error; +/// # fn main() -> Result<(), Error> { +/// let uuid: &str = "26509cec-4a2b-4935-87ab-762d89fbf0b0"; +/// let ta_config = TAConfig::new_standard("0.1.0", "example", "example"); +/// let codes = HeaderFileGenerator::new(RustEdition::Before2024).generate(&ta_config, uuid)?; +/// # Ok(()) +/// # } +/// ``` +pub struct HeaderFileGenerator { + code: proc_macro2::TokenStream, + edition: RustEdition, +} + +impl HeaderFileGenerator { + pub fn new(edition: RustEdition) -> Self { + Self { + code: quote!(), + edition, + } + } + pub fn generate(mut self, conf: &TAConfig, uuid: &str) -> Result { + self.write_includes(); + self.write_configurations(conf); + self.write_trace(conf); + self.write_properties(conf)?; + self.write_ta_head(conf, uuid)?; + self.write_ta_heap(); + + let f = syn::parse2(self.code).unwrap(); + Ok(prettyplease::unparse(&f)) + } +} + +impl HeaderFileGenerator { + fn write_includes(&mut self) { + self.code.extend(quote! { + use core::ffi::*; + use core::mem; + use core::primitive::u64; + }); + } + + fn write_trace(&mut self, conf: &TAConfig) { + let trace_ext = string_to_binary_codes(&conf.trace_ext_prefix); + let trace_level = conf.trace_level; + let no_mangle_attribute = self.edition.no_mangle_attribute_codes(); + self.code.extend(quote! { + #no_mangle_attribute + pub static mut trace_level: c_int = #trace_level; + + #no_mangle_attribute + pub static trace_ext_prefix: &[u8] = #trace_ext; + + #no_mangle_attribute + pub unsafe extern "C" fn tahead_get_trace_level() -> c_int { + unsafe { return trace_level; } + } + }) + } + fn write_configurations(&mut self, conf: &TAConfig) { + let ta_version = string_to_binary_codes(&conf.ta_version); + let ta_description = string_to_binary_codes(&conf.ta_description); + let ta_flags = conf.ta_flags; + let ta_data_size = conf.ta_data_size; + let ta_stack_size = conf.ta_stack_size; + self.code.extend(quote! { + const TA_FLAGS: u32 = #ta_flags; + const TA_DATA_SIZE: u32 = #ta_data_size; + const TA_STACK_SIZE: u32 = #ta_stack_size; + const TA_VERSION: &[u8] = #ta_version; + const TA_DESCRIPTION: &[u8] = #ta_description; + }); + } + fn write_properties(&mut self, conf: &TAConfig) -> Result<(), Error> { + let mut ext_property_codes = + Vec::::with_capacity(conf.ext_properties.len()); + for (index, prop) in conf.ext_properties.iter().enumerate() { + let var_name = format!("EXT_PROP_VALUE_{}", index + 1); + let rust_type_name_codes = property_value_rust_type_declaration_codes(&prop.value); + let value_codes = property_value_data_codes(&prop.value)?; + let var_name_codes = format_ident!("{}", var_name); + self.code.extend(quote! { + const #var_name_codes: #rust_type_name_codes = #value_codes; + }); + let prop_name_codes = string_to_binary_codes(&prop.name); + let utee_type_name_codes = property_value_utee_type_codes(&prop.value); + let utee_value_conv_codes = property_value_as_utee_value_codes(&var_name, &prop.value); + ext_property_codes.push(quote! { + optee_utee_sys::user_ta_property { + name: #prop_name_codes.as_ptr(), + prop_type: #utee_type_name_codes, + value: #utee_value_conv_codes, + } + }); + } + + const ORIGIN_PROPERTY_LEN: usize = 7; + let property_len = ORIGIN_PROPERTY_LEN + conf.ext_properties.len(); + let no_mangle_attribute = self.edition.no_mangle_attribute_codes(); + self.code.extend(quote! { + static FLAG_BOOL: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_SINGLE_INSTANCE) != 0; + static FLAG_MULTI: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_MULTI_SESSION) != 0; + static FLAG_INSTANCE: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_INSTANCE_KEEP_ALIVE) != 0; + #no_mangle_attribute + pub static ta_num_props: usize = #property_len; + #no_mangle_attribute + pub static ta_props: [optee_utee_sys::user_ta_property; #property_len] = [ + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_SINGLE_INSTANCE, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL, + value: &FLAG_BOOL as *const bool as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_MULTI_SESSION, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL, + value: &FLAG_MULTI as *const bool as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_KEEP_ALIVE, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL, + value: &FLAG_INSTANCE as *const bool as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_DATA_SIZE, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32, + value: &TA_DATA_SIZE as *const u32 as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_STACK_SIZE, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32, + value: &TA_STACK_SIZE as *const u32 as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_VERSION, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING, + value: TA_VERSION as *const [u8] as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_DESCRIPTION, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING, + value: TA_DESCRIPTION as *const [u8] as *mut _, + }, + #(#ext_property_codes),* + ]; + }); + + Ok(()) + } + fn write_ta_head(&mut self, conf: &TAConfig, uuid_str: &str) -> Result<(), Error> { + let uuid_value_codes = string_to_tee_uuid_value_codes(uuid_str)?; + let stack_size = conf.ta_stack_size + conf.ta_framework_stack_size; + let no_mangle_attribute = self.edition.no_mangle_attribute_codes(); + let ta_head_session_attribute = self.edition.link_section_attribute_codes(".ta_head"); + self.code.extend(quote! { + #no_mangle_attribute + #ta_head_session_attribute + pub static ta_head: optee_utee_sys::ta_head = optee_utee_sys::ta_head { + uuid: #uuid_value_codes, + stack_size: #stack_size, + flags: TA_FLAGS, + depr_entry: u64::MAX, + }; + }); + + Ok(()) + } + fn write_ta_heap(&mut self) { + let no_mangle_attribute = self.edition.no_mangle_attribute_codes(); + let bss_session_attribute = self.edition.link_section_attribute_codes(".bss"); + self.code.extend(quote! { + #no_mangle_attribute + #bss_session_attribute + pub static ta_heap: [u8; TA_DATA_SIZE as usize] = [0; TA_DATA_SIZE as usize]; + + #no_mangle_attribute + pub static ta_heap_size: usize = mem::size_of::() * TA_DATA_SIZE as usize; + }) + } +} + +fn property_value_data_codes(value: &PropertyValue) -> Result { + match value { + PropertyValue::U32(v) => Ok(quote! { #v }), + PropertyValue::U64(v) => Ok(quote! { #v }), + PropertyValue::Bool(v) => Ok(quote! { #v }), + PropertyValue::UUID(v) => string_to_tee_uuid_value_codes(v.as_str()), + PropertyValue::Str(v) => Ok(string_to_binary_codes(v)), + PropertyValue::BinaryBlock(v) => Ok(string_to_binary_codes(v)), + PropertyValue::Identity(login, uuid) => identity_to_tee_identity_value_codes(login.clone(), &uuid), + } +} + +fn string_to_tee_uuid_value_codes(uuid_str: &str) -> Result { + let uuid = uuid::Uuid::parse_str(uuid_str)?; + let (time_low, time_mid, time_hi_and_version, clock_seq_and_node) = uuid.as_fields(); + Ok(quote! { + optee_utee_sys::TEE_UUID { + timeLow: #time_low, + timeMid: #time_mid, + timeHiAndVersion: #time_hi_and_version, + clockSeqAndNode: [#(#clock_seq_and_node),* ], + } + }) +} + +fn identity_to_tee_identity_value_codes(login: u32, uuid_str: &str) -> Result { + let tee_uuid_codes = string_to_tee_uuid_value_codes(uuid_str)?; + Ok(quote! { + optee_utee_sys::TEE_Identity { + login: #login, + uuid: #tee_uuid_codes, + } + }) +} + +fn property_value_rust_type_declaration_codes(value: &PropertyValue) -> proc_macro2::TokenStream { + proc_macro2::TokenStream::from_str(match value { + PropertyValue::U32(_) => "u32", + PropertyValue::U64(_) => "u64", + PropertyValue::Bool(_) => "bool", + PropertyValue::UUID(_) => "optee_utee_sys::TEE_UUID", + PropertyValue::Str(_) => "&[u8]", + PropertyValue::BinaryBlock(_) => "&[u8]", + PropertyValue::Identity(..) => "optee_utee_sys::TEE_Identity", + }) + .unwrap() +} + +fn property_value_as_utee_value_codes( + var_name: &str, + value: &PropertyValue, +) -> proc_macro2::TokenStream { + proc_macro2::TokenStream::from_str( + match value { + PropertyValue::U32(_) => format!("&{} as *const u32 as *mut _", var_name), + PropertyValue::U64(_) => format!("&{} as *const u64 as *mut _", var_name), + PropertyValue::Bool(_) => format!("&{} as *const bool as *mut _", var_name), + PropertyValue::UUID(_) => { + format!("&{} as *const optee_utee_sys::TEE_UUID as *mut _", var_name) + } + PropertyValue::Str(_) => format!("{} as *const [u8] as *mut _", var_name), + PropertyValue::BinaryBlock(_) => format!("{} as *const [u8] as *mut _", var_name), + PropertyValue::Identity(..) => format!( + "&{} as *const optee_utee_sys::TEE_Identity as *mut _", + var_name + ), + } + .as_str(), + ) + .unwrap() +} + +fn property_value_utee_type_codes(value: &PropertyValue) -> proc_macro2::TokenStream { + proc_macro2::TokenStream::from_str(match value { + PropertyValue::U32(_) => "optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32", + PropertyValue::U64(_) => "optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U64", + PropertyValue::Bool(_) => "optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL", + PropertyValue::UUID(_) => "optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_UUID", + PropertyValue::Str(_) => "optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING", + PropertyValue::BinaryBlock(_) => { + "optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BINARY_BLOCK" + } + PropertyValue::Identity(..) => { + "optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_IDENTITY" + } + }) + .unwrap() +} + +fn string_to_binary_codes(s: &str) -> proc_macro2::TokenStream { + let wrapped = format!("b\"{}\\0\"", s); + proc_macro2::TokenStream::from_str(&wrapped).unwrap() +} + +impl RustEdition { + fn no_mangle_attribute_codes(&self) -> proc_macro2::TokenStream { + match self { + RustEdition::Before2024 => quote! { #[no_mangle] }, + RustEdition::Edition2024 => quote! { #[unsafe(no_mangle)] }, + } + } + fn link_section_attribute_codes(&self, session_name: &str) -> proc_macro2::TokenStream { + match self { + RustEdition::Before2024 => quote! { #[link_section = #session_name] }, + RustEdition::Edition2024 => quote! { #[unsafe(link_section = #session_name)] }, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_edition_before_2024() { + let conf = TAConfig::new_standard( + "0.1.0", + "test_before_2024", + "test before 2024", + ); + let uuid = "26509cec-4a2b-4935-87ab-762d89fbf0b0"; + let generator = HeaderFileGenerator::new(RustEdition::Before2024); + let codes = generator.generate(&conf, uuid).unwrap(); + let exp_result = include_str!("../test_files/test_edition_before_2024_result.rs"); + assert_eq!(codes, exp_result); + } + #[test] + fn test_edition_2024() { + let conf = TAConfig::new_standard( + "0.1.0", + "test_edition_2024", + "test edition 2024", + ); + let uuid = "26509cec-4a2b-4935-87ab-762d89fbf0b0"; + let generator = HeaderFileGenerator::new(RustEdition::Edition2024); + let codes = generator.generate(&conf, uuid).unwrap(); + let exp_result = include_str!("../test_files/test_edition_2024_result.rs"); + assert_eq!(codes, exp_result); + } +} diff --git a/optee-utee-build/src/error.rs b/optee-utee-build/src/error.rs new file mode 100644 index 00000000..d29b1811 --- /dev/null +++ b/optee-utee-build/src/error.rs @@ -0,0 +1,42 @@ +// 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. + +#[derive(Debug)] +pub enum Error { + Env(std::env::VarError), + IO(std::io::Error), + UUID(uuid::Error), + PropertyNotFound(String), +} + +impl From for Error { + fn from(value: std::io::Error) -> Self { + Self::IO(value) + } +} + +impl From for Error { + fn from(value: std::env::VarError) -> Self { + Self::Env(value) + } +} + +impl From for Error { + fn from(value: uuid::Error) -> Self { + Self::UUID(value) + } +} diff --git a/optee-utee-build/src/lib.rs b/optee-utee-build/src/lib.rs new file mode 100644 index 00000000..86dfbc64 --- /dev/null +++ b/optee-utee-build/src/lib.rs @@ -0,0 +1,32 @@ +// 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. + +mod builder; +mod linker; +mod code_generator; +mod error; +mod ta_config; + +pub use builder::*; +pub use code_generator::*; +pub use error::Error; +pub use ta_config::*; +pub use linker::*; + +pub fn build(edition: RustEdition, uuid: &str, config: TAConfig) -> Result<(), Error> { + Builder::new(edition, config).build(uuid) +} diff --git a/optee-utee-build/src/linker.rs b/optee-utee-build/src/linker.rs new file mode 100644 index 00000000..83454854 --- /dev/null +++ b/optee-utee-build/src/linker.rs @@ -0,0 +1,175 @@ +// 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::env; +use std::fs::File; +use std::io::Write; +use std::io::{BufRead, BufReader}; +use std::path::PathBuf; + +use crate::Error; + +/// The type of the linker, there are difference when using gcc/cc or ld/lld as linker, +/// For example, `--sort-section=alignment` parameter changes to `-Wl,--sort-section=alignment` +/// when using gcc as linker. +/// +/// CC: gcc, cc, etc. +/// LD: ld, lld, ld.bfd, ld.gold, etc. +#[derive(Debug, Clone)] +pub enum LinkerType { + CC, + LD, +} + +/// Linker of ta, use it to handle all linking stuff +/// +/// Use only if you just want to handle the linking stuff, and use a hand written user_ta_header.rs, +/// or you should use Builder instead +/// Usage: +/// +/// ```no_run +/// use optee_utee_build::Linker; +/// use std::env; +/// # use optee_utee_build::Error; +/// # fn main() -> Result<(), Error> { +/// let out_dir = env::var("OUT_DIR")?; +/// Linker::auto().link_all(out_dir)?; +/// # Ok(()) +/// # } +/// ``` +/// +/// We detect the type of the linker automatically, you can set it manually if you met some +/// problems with it. +/// ```no_run +/// use optee_utee_build::{Linker, LinkerType}; +/// use std::env; +/// # use optee_utee_build::Error; +/// # fn main() -> Result<(), Error> { +/// let out_dir = env::var("OUT_DIR")?; +/// Linker::new(LinkerType::CC).link_all(out_dir)?; +/// # Ok(()) +/// # } +/// ``` +/// +pub struct Linker { + linker_type: LinkerType, +} + +impl Linker { + pub fn new(linker_type: LinkerType) -> Self { + Self { linker_type } + } + pub fn auto() -> Self { + Self { linker_type: Self::auto_detect_linker_type() } + } + pub fn link_all>(self, out_dir: P) -> Result<(), Error> { + const ENV_TA_DEV_KIT_DIR: &str = "TA_DEV_KIT_DIR"; + println!("cargo:rerun-if-env-changed={}", ENV_TA_DEV_KIT_DIR); + let ta_dev_kit_dir = PathBuf::from(std::env::var(ENV_TA_DEV_KIT_DIR)?); + let out_dir: PathBuf = out_dir.into(); + + self.write_and_set_linker_script(out_dir.clone(), ta_dev_kit_dir.clone())?; + + let search_path = ta_dev_kit_dir.join("lib"); + println!("cargo:rustc-link-search={}", search_path.display()); + println!("cargo:rustc-link-lib=static=utee"); + println!("cargo:rustc-link-lib=static=utils"); + println!("cargo:rustc-link-arg=-e__ta_entry"); + println!("cargo:rustc-link-arg=-pie"); + println!("cargo:rustc-link-arg=-Os"); + match self.linker_type { + LinkerType::CC => println!("cargo:rustc-link-arg=-Wl,--sort-section=alignment"), + LinkerType::LD => println!("cargo:rustc-link-arg=--sort-section=alignment"), + }; + let mut dyn_list = File::create(out_dir.join("dyn_list"))?; + write!( + dyn_list, + "{{ __elf_phdr_info; trace_ext_prefix; trace_level; ta_head; }};\n" + )?; + match self.linker_type { + LinkerType::CC => println!("cargo:rustc-link-arg=-Wl,--dynamic-list=dyn_list"), + LinkerType::LD => println!("cargo:rustc-link-arg=--dynamic-list=dyn_list"), + } + + Ok(()) + } +} + +impl Linker { + // generate a link script file for cc/ld, and link to it + fn write_and_set_linker_script(&self, out_dir: PathBuf, ta_dev_kit_dir: PathBuf) -> Result<(), Error> { + const ENV_TARGET_TA: &str = "TARGET_TA"; + println!("cargo:rerun-if-env-changed={}", ENV_TARGET_TA); + let mut aarch64_flag = true; + match env::var(ENV_TARGET_TA) { + Ok(ref v) if v == "arm-unknown-linux-gnueabihf" || v == "arm-unknown-optee" => { + match self.linker_type { + LinkerType::CC => println!("cargo:rustc-link-arg=-Wl,--no-warn-mismatch"), + LinkerType::LD => println!("cargo:rustc-link-arg=--no-warn-mismatch"), + }; + aarch64_flag = false; + } + _ => {} + }; + + let f = BufReader::new(File::open(ta_dev_kit_dir.join("src/ta.ld.S"))?); + let ta_lds_file_path = out_dir.join("ta.lds"); + let mut ta_lds = File::create(ta_lds_file_path.clone())?; + for line in f.lines() { + let l = line?; + + if aarch64_flag { + if l.starts_with('#') + || l == "OUTPUT_FORMAT(\"elf32-littlearm\")" + || l == "OUTPUT_ARCH(arm)" + { + continue; + } + } else { + if l.starts_with('#') + || l == "OUTPUT_FORMAT(\"elf64-littleaarch64\")" + || l == "OUTPUT_ARCH(aarch64)" + { + continue; + } + } + + if l == "\t. = ALIGN(4096);" { + write!(ta_lds, "\t. = ALIGN(65536);\n")?; + } else { + write!(ta_lds, "{}\n", l)?; + } + } + + println!("cargo:rustc-link-search={}", out_dir.display()); + println!("cargo:rerun-if-changed={}", ta_lds_file_path.display()); + println!("cargo:rustc-link-arg=-T{}", ta_lds_file_path.display()); + Ok(()) + } + + fn auto_detect_linker_type() -> LinkerType { + const ENV_RUSTC_LINKER: &str = "RUSTC_LINKER"; + println!("cargo:rerun-if-env-changed={}", ENV_RUSTC_LINKER); + match env::var(ENV_RUSTC_LINKER) { + Ok(ref linker_name) if linker_name.ends_with("ld") || linker_name.ends_with("ld.bfd") => { + return LinkerType::LD; + }, + _ => {}, + }; + return LinkerType::CC; + } +} diff --git a/optee-utee-build/src/ta_config.rs b/optee-utee-build/src/ta_config.rs new file mode 100644 index 00000000..8386802b --- /dev/null +++ b/optee-utee-build/src/ta_config.rs @@ -0,0 +1,137 @@ +// 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. + +pub const EXT_PROP_NAME_GP_TA_DESCRIPTION: &str = "gp.ta.description"; +pub const EXT_PROP_NAME_GP_TA_VERSION: &str = "gp.ta.version"; + +/// Configuration options for TA +/// +/// Examples +/// +/// # use a standard configuration +/// ```rust +/// use optee_utee_build::TAConfig; +/// let ta_config = TAConfig::new_standard( +/// "0.1.0", +/// "hello world", +/// "this is a hello world TA", +/// ); +/// ``` +/// +/// # make some modifications +/// ```rust +/// use optee_utee_build::TAConfig; +/// let ta_config = TAConfig::new_standard( +/// "0.1.0", +/// "hello world", +/// "this is a hello world TA", +/// ).ta_stack_size(10 * 1024).ta_data_size(32 * 1024); +/// ``` +#[derive(Debug, Clone)] +pub struct TAConfig { + pub ta_flags: u32, + pub ta_data_size: u32, + pub ta_stack_size: u32, + pub ta_version: String, + pub ta_description: String, + pub trace_level: i32, + pub trace_ext_prefix: String, + pub ta_framework_stack_size: u32, + pub ext_properties: Vec, +} + +impl TAConfig { + pub fn new_standard(ta_version: &str, ta_description: &str, gp_ta_description: &str) -> Self { + Self { + ta_flags: 0, + ta_data_size: 1 * 1024 * 1024, + ta_stack_size: 2 * 1024, + ta_version: ta_version.to_string(), + ta_description: ta_description.to_string(), + trace_level: 4, + trace_ext_prefix: "TA".to_string(), + ta_framework_stack_size: 2048, + ext_properties: vec![ + Property::new_gp_ta_description(gp_ta_description), + Property::new_gp_ta_version(0x0010), + ], + } + } + pub fn ta_flags(mut self, flags: u32) -> Self { + self.ta_flags = flags; + self + } + pub fn ta_stack_size(mut self, stack_size: u32) -> Self { + self.ta_stack_size = stack_size; + self + } + pub fn ta_data_size(mut self, size: u32) -> Self { + self.ta_data_size = size; + self + } + pub fn trace_level(mut self, level: i32) -> Self { + self.trace_level = level; + self + } + pub fn trace_ext_prefix>(mut self, prefix: S) -> Self { + self.trace_ext_prefix = prefix.into(); + self + } + pub fn ta_framework_stack_size(mut self, stack_size: u32) -> Self { + self.ta_framework_stack_size = stack_size; + self + } + pub fn add_ext_property(mut self, name: &str, value: PropertyValue) -> Self { + self.ext_properties.push(Property::new(name, value)); + self + } +} + +#[derive(Debug, Clone)] +pub enum PropertyValue { + Bool(bool), + U32(u32), + UUID(String), + Identity(u32, String), + Str(String), + BinaryBlock(String), + U64(u64), +} + +#[derive(Debug, Clone)] +pub struct Property { + pub name: String, + pub value: PropertyValue, +} + +impl Property { + pub fn new(name: &str, value: PropertyValue) -> Self { + Self { + name: name.to_string(), + value, + } + } + pub fn new_gp_ta_description(desc: &str) -> Self { + Self::new( + EXT_PROP_NAME_GP_TA_DESCRIPTION, + PropertyValue::Str(desc.to_string()), + ) + } + pub fn new_gp_ta_version(version: u32) -> Self { + Self::new(EXT_PROP_NAME_GP_TA_VERSION, PropertyValue::U32(version)) + } +} diff --git a/optee-utee-build/test_files/test_edition_2024_result.rs b/optee-utee-build/test_files/test_edition_2024_result.rs new file mode 100644 index 00000000..14740a84 --- /dev/null +++ b/optee-utee-build/test_files/test_edition_2024_result.rs @@ -0,0 +1,92 @@ +use core::ffi::*; +use core::mem; +use core::primitive::u64; +const TA_FLAGS: u32 = 0u32; +const TA_DATA_SIZE: u32 = 1048576u32; +const TA_STACK_SIZE: u32 = 2048u32; +const TA_VERSION: &[u8] = b"0.1.0\0"; +const TA_DESCRIPTION: &[u8] = b"test_edition_2024\0"; +#[unsafe(no_mangle)] +pub static mut trace_level: c_int = 4i32; +#[unsafe(no_mangle)] +pub static trace_ext_prefix: &[u8] = b"TA\0"; +#[unsafe(no_mangle)] +pub unsafe extern "C" fn tahead_get_trace_level() -> c_int { + unsafe { + return trace_level; + } +} +const EXT_PROP_VALUE_1: &[u8] = b"test edition 2024\0"; +const EXT_PROP_VALUE_2: u32 = 16u32; +static FLAG_BOOL: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_SINGLE_INSTANCE) != 0; +static FLAG_MULTI: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_MULTI_SESSION) != 0; +static FLAG_INSTANCE: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_INSTANCE_KEEP_ALIVE) + != 0; +#[unsafe(no_mangle)] +pub static ta_num_props: usize = 9usize; +#[unsafe(no_mangle)] +pub static ta_props: [optee_utee_sys::user_ta_property; 9usize] = [ + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_SINGLE_INSTANCE, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL, + value: &FLAG_BOOL as *const bool as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_MULTI_SESSION, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL, + value: &FLAG_MULTI as *const bool as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_KEEP_ALIVE, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL, + value: &FLAG_INSTANCE as *const bool as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_DATA_SIZE, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32, + value: &TA_DATA_SIZE as *const u32 as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_STACK_SIZE, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32, + value: &TA_STACK_SIZE as *const u32 as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_VERSION, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING, + value: TA_VERSION as *const [u8] as *mut _, + }, + optee_utee_sys::user_ta_property { + name: optee_utee_sys::TA_PROP_STR_DESCRIPTION, + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING, + value: TA_DESCRIPTION as *const [u8] as *mut _, + }, + optee_utee_sys::user_ta_property { + name: b"gp.ta.description\0".as_ptr(), + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING, + value: EXT_PROP_VALUE_1 as *const [u8] as *mut _, + }, + optee_utee_sys::user_ta_property { + name: b"gp.ta.version\0".as_ptr(), + prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32, + value: &EXT_PROP_VALUE_2 as *const u32 as *mut _, + }, +]; +#[unsafe(no_mangle)] +#[unsafe(link_section = ".ta_head")] +pub static ta_head: optee_utee_sys::ta_head = optee_utee_sys::ta_head { + uuid: optee_utee_sys::TEE_UUID { + timeLow: 642817260u32, + timeMid: 18987u16, + timeHiAndVersion: 18741u16, + clockSeqAndNode: [135u8, 171u8, 118u8, 45u8, 137u8, 251u8, 240u8, 176u8], + }, + stack_size: 4096u32, + flags: TA_FLAGS, + depr_entry: u64::MAX, +}; +#[unsafe(no_mangle)] +#[unsafe(link_section = ".bss")] +pub static ta_heap: [u8; TA_DATA_SIZE as usize] = [0; TA_DATA_SIZE as usize]; +#[unsafe(no_mangle)] +pub static ta_heap_size: usize = mem::size_of::() * TA_DATA_SIZE as usize; diff --git a/examples/hello_world-rs/ta/ta_static.rs b/optee-utee-build/test_files/test_edition_before_2024_result.rs similarity index 68% rename from examples/hello_world-rs/ta/ta_static.rs rename to optee-utee-build/test_files/test_edition_before_2024_result.rs index 20e1d977..ea1a6f95 100644 --- a/examples/hello_world-rs/ta/ta_static.rs +++ b/optee-utee-build/test_files/test_edition_before_2024_result.rs @@ -1,54 +1,31 @@ -// 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 core::ffi::{c_int, c_void}; +use core::ffi::*; use core::mem; use core::primitive::u64; - +const TA_FLAGS: u32 = 0u32; +const TA_DATA_SIZE: u32 = 1048576u32; +const TA_STACK_SIZE: u32 = 2048u32; +const TA_VERSION: &[u8] = b"0.1.0\0"; +const TA_DESCRIPTION: &[u8] = b"test_before_2024\0"; #[no_mangle] -pub static mut trace_level: c_int = TRACE_LEVEL; - +pub static mut trace_level: c_int = 4i32; #[no_mangle] -pub static trace_ext_prefix: &[u8] = TRACE_EXT_PREFIX; - +pub static trace_ext_prefix: &[u8] = b"TA\0"; #[no_mangle] -#[link_section = ".ta_head"] -pub static ta_head: optee_utee_sys::ta_head = optee_utee_sys::ta_head { - uuid: TA_UUID, - stack_size: TA_STACK_SIZE + TA_FRAMEWORK_STACK_SIZE, - flags: TA_FLAGS, - depr_entry: u64::MAX, -}; - -#[no_mangle] -#[link_section = ".bss"] -pub static ta_heap: [u8; TA_DATA_SIZE as usize] = [0; TA_DATA_SIZE as usize]; - -#[no_mangle] -pub static ta_heap_size: usize = mem::size_of::() * TA_DATA_SIZE as usize; +pub unsafe extern "C" fn tahead_get_trace_level() -> c_int { + unsafe { + return trace_level; + } +} +const EXT_PROP_VALUE_1: &[u8] = b"test before 2024\0"; +const EXT_PROP_VALUE_2: u32 = 16u32; static FLAG_BOOL: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_SINGLE_INSTANCE) != 0; static FLAG_MULTI: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_MULTI_SESSION) != 0; -static FLAG_INSTANCE: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_INSTANCE_KEEP_ALIVE) != 0; - +static FLAG_INSTANCE: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_INSTANCE_KEEP_ALIVE) + != 0; #[no_mangle] -pub static ta_num_props: usize = 9; - +pub static ta_num_props: usize = 9usize; #[no_mangle] -pub static ta_props: [optee_utee_sys::user_ta_property; 9] = [ +pub static ta_props: [optee_utee_sys::user_ta_property; 9usize] = [ optee_utee_sys::user_ta_property { name: optee_utee_sys::TA_PROP_STR_SINGLE_INSTANCE, prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL, @@ -85,18 +62,31 @@ pub static ta_props: [optee_utee_sys::user_ta_property; 9] = [ value: TA_DESCRIPTION as *const [u8] as *mut _, }, optee_utee_sys::user_ta_property { - name: "gp.ta.description\0".as_ptr(), + name: b"gp.ta.description\0".as_ptr(), prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING, value: EXT_PROP_VALUE_1 as *const [u8] as *mut _, }, optee_utee_sys::user_ta_property { - name: "gp.ta.version\0".as_ptr(), + name: b"gp.ta.version\0".as_ptr(), prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32, value: &EXT_PROP_VALUE_2 as *const u32 as *mut _, }, ]; - #[no_mangle] -pub unsafe extern "C" fn tahead_get_trace_level() -> c_int { - return trace_level; -} +#[link_section = ".ta_head"] +pub static ta_head: optee_utee_sys::ta_head = optee_utee_sys::ta_head { + uuid: optee_utee_sys::TEE_UUID { + timeLow: 642817260u32, + timeMid: 18987u16, + timeHiAndVersion: 18741u16, + clockSeqAndNode: [135u8, 171u8, 118u8, 45u8, 137u8, 251u8, 240u8, 176u8], + }, + stack_size: 4096u32, + flags: TA_FLAGS, + depr_entry: u64::MAX, +}; +#[no_mangle] +#[link_section = ".bss"] +pub static ta_heap: [u8; TA_DATA_SIZE as usize] = [0; TA_DATA_SIZE as usize]; +#[no_mangle] +pub static ta_heap_size: usize = mem::size_of::() * TA_DATA_SIZE as usize;