From 7efdee686ceb50e706d5a3dacb9714de45e8fb22 Mon Sep 17 00:00:00 2001 From: Yuan Zhuang Date: Thu, 9 Jan 2025 07:10:56 +0000 Subject: [PATCH] projects/web3: update building scripts - sync Makefiles from other examples - use the `optee-utee-build` crate for building TA Signed-off-by: Yuan Zhuang --- projects/web3/eth_wallet/Makefile | 16 ++-- projects/web3/eth_wallet/host/Makefile | 14 ++-- projects/web3/eth_wallet/ta/Cargo.toml | 3 +- projects/web3/eth_wallet/ta/Makefile | 12 +-- projects/web3/eth_wallet/ta/build.rs | 94 ++------------------- projects/web3/eth_wallet/ta/src/main.rs | 12 --- projects/web3/eth_wallet/ta/ta_static.rs | 102 ----------------------- 7 files changed, 32 insertions(+), 221 deletions(-) delete mode 100644 projects/web3/eth_wallet/ta/ta_static.rs diff --git a/projects/web3/eth_wallet/Makefile b/projects/web3/eth_wallet/Makefile index c266055b..a7a3dec8 100644 --- a/projects/web3/eth_wallet/Makefile +++ b/projects/web3/eth_wallet/Makefile @@ -22,11 +22,17 @@ CROSS_COMPILE_TA ?= aarch64-linux-gnu- TARGET_HOST ?= aarch64-unknown-linux-gnu TARGET_TA ?= aarch64-unknown-linux-gnu -all: - $(q)make -C host TARGET_HOST=$(TARGET_HOST) \ - CROSS_COMPILE_HOST=$(CROSS_COMPILE_HOST) - $(q)make -C ta TARGET_TA=$(TARGET_TA) \ - CROSS_COMPILE_TA=$(CROSS_COMPILE_TA) +.PHONY: host ta all clean + +all: host ta + +host: + $(q)make -C host TARGET=$(TARGET_HOST) \ + CROSS_COMPILE=$(CROSS_COMPILE_HOST) + +ta: + $(q)make -C ta TARGET=$(TARGET_TA) \ + CROSS_COMPILE=$(CROSS_COMPILE_TA) clean: $(q)make -C host clean diff --git a/projects/web3/eth_wallet/host/Makefile b/projects/web3/eth_wallet/host/Makefile index b1c50c41..3f2f4314 100644 --- a/projects/web3/eth_wallet/host/Makefile +++ b/projects/web3/eth_wallet/host/Makefile @@ -17,16 +17,16 @@ NAME := eth_wallet-rs -TARGET_HOST ?= aarch64-unknown-linux-gnu -CROSS_COMPILE_HOST ?= aarch64-linux-gnu- -OBJCOPY := $(CROSS_COMPILE_HOST)objcopy -LINKER_CFG := target.$(TARGET_HOST).linker=\"$(CROSS_COMPILE_HOST)gcc\" +TARGET ?= aarch64-unknown-linux-gnu +CROSS_COMPILE ?= aarch64-linux-gnu- +OBJCOPY := $(CROSS_COMPILE)objcopy +LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\" -OUT_DIR := $(CURDIR)/target/$(TARGET_HOST)/release +OUT_DIR := $(CURDIR)/target/$(TARGET)/release ifeq ($(STD),) all: - @echo "Please `export STD=y` then rerun `source environment` to build the STD version" + @echo "Please \`export STD=y\` then rerun \`source environment\` to build the STD version" else all: host strip endif @@ -38,4 +38,4 @@ strip: host @$(OBJCOPY) --strip-unneeded $(OUT_DIR)/$(NAME) $(OUT_DIR)/$(NAME) clean: - @cargo clean + @cargo clean \ No newline at end of file diff --git a/projects/web3/eth_wallet/ta/Cargo.toml b/projects/web3/eth_wallet/ta/Cargo.toml index e118e599..bc864b4d 100644 --- a/projects/web3/eth_wallet/ta/Cargo.toml +++ b/projects/web3/eth_wallet/ta/Cargo.toml @@ -21,7 +21,7 @@ version = "0.3.0" authors = ["Teaclave Contributors "] license = "Apache-2.0" repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git" -description = "An example of Rust OP-TEE TrustZone SDK." +description = "An example of Ethereum wallet TA." edition = "2018" [dependencies] @@ -43,6 +43,7 @@ bincode = "1.3.3" [build-dependencies] uuid = { version = "1.8", default-features = false } proto = { path = "../proto" } +optee-utee-build = { path = "../../../../optee-utee-build" } [profile.release] lto = false diff --git a/projects/web3/eth_wallet/ta/Makefile b/projects/web3/eth_wallet/ta/Makefile index 46037a8e..d948521b 100644 --- a/projects/web3/eth_wallet/ta/Makefile +++ b/projects/web3/eth_wallet/ta/Makefile @@ -19,10 +19,11 @@ UUID ?= $(shell cat "../uuid.txt") -TARGET_TA ?= aarch64-unknown-linux-gnu -CROSS_COMPILE_TA ?= aarch64-linux-gnu- +TARGET ?= aarch64-unknown-linux-gnu +CROSS_COMPILE ?= aarch64-linux-gnu- OBJCOPY := $(CROSS_COMPILE_TA)objcopy -LINKER_CFG := target.$(TARGET_TA).linker=\"$(CROSS_COMPILE_TA)ld.bfd\" +# Configure the linker to use GCC, which works on both cross-compilation and ARM machines +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 @@ -30,14 +31,13 @@ OUT_DIR := $(CURDIR)/target/$(TARGET_TA)/release ifeq ($(STD),) all: - @echo "Please `export STD=y` then rerun `source environment` to build the STD version" + @echo "Please \`export STD=y\` then rerun \`source environment\` to build the STD version" else all: ta strip sign endif -# set the cross compile for building inner libraries, such as C libraries in ring ta: - @CROSS_COMPILE=$(CROSS_COMPILE_TA) xargo build --target $(TARGET_TA) --release --config $(LINKER_CFG) + @xargo build --target $(TARGET) --release --config $(LINKER_CFG) strip: ta @$(OBJCOPY) --strip-unneeded $(OUT_DIR)/ta $(OUT_DIR)/stripped_ta diff --git a/projects/web3/eth_wallet/ta/build.rs b/projects/web3/eth_wallet/ta/build.rs index 9efe628c..2352649d 100644 --- a/projects/web3/eth_wallet/ta/build.rs +++ b/projects/web3/eth_wallet/ta/build.rs @@ -16,93 +16,11 @@ // 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::{Error, RustEdition, TaConfig}; -fn main() -> std::io::Result<()> { - let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); - - 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(()) +fn main() -> Result<(), Error> { + let ta_config = TaConfig::new_default_with_cargo_env(proto::UUID)? + .ta_data_size(1024 * 1024) + .ta_stack_size(128 * 1024); + optee_utee_build::build(RustEdition::Before2024, ta_config) } diff --git a/projects/web3/eth_wallet/ta/src/main.rs b/projects/web3/eth_wallet/ta/src/main.rs index d889a26c..5f72179e 100644 --- a/projects/web3/eth_wallet/ta/src/main.rs +++ b/projects/web3/eth_wallet/ta/src/main.rs @@ -166,16 +166,4 @@ fn invoke_command(cmd_id: u32, params: &mut Parameters) -> optee_utee::Result<() Ok(()) } -// TA configurations -const TA_FLAGS: u32 = 0; -const TA_DATA_SIZE: u32 = 1024 * 1024; -const TA_STACK_SIZE: u32 = 128 * 1024; -const TA_VERSION: &[u8] = b"0.3\0"; -const TA_DESCRIPTION: &[u8] = b"This is an example of Ethereum wallet TA\0"; -const EXT_PROP_VALUE_1: &[u8] = b"Ethereum wallet 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/projects/web3/eth_wallet/ta/ta_static.rs b/projects/web3/eth_wallet/ta/ta_static.rs deleted file mode 100644 index 20e1d977..00000000 --- a/projects/web3/eth_wallet/ta/ta_static.rs +++ /dev/null @@ -1,102 +0,0 @@ -// 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::mem; -use core::primitive::u64; - -#[no_mangle] -pub static mut trace_level: c_int = TRACE_LEVEL; - -#[no_mangle] -pub static trace_ext_prefix: &[u8] = TRACE_EXT_PREFIX; - -#[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; -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] -pub static ta_num_props: usize = 9; - -#[no_mangle] -pub static ta_props: [optee_utee_sys::user_ta_property; 9] = [ - 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: "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(), - 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; -}