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 proto;
19 use std::env;
20 use std::fs::File;
21 use std::io::{BufRead, BufReader, Write};
22 use std::path::{Path, PathBuf};
23 use uuid::Uuid;
24
main() -> std::io::Result<()>25 fn main() -> std::io::Result<()> {
26 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
27
28 let mut buffer = File::create(out.join("user_ta_header.rs"))?;
29 buffer.write_all(include_bytes!("ta_static.rs"))?;
30
31 let tee_uuid = Uuid::parse_str(proto::UUID).unwrap();
32 let (time_low, time_mid, time_hi_and_version, clock_seq_and_node) = tee_uuid.as_fields();
33
34 write!(buffer, "\n")?;
35 write!(
36 buffer,
37 "const TA_UUID: optee_utee_sys::TEE_UUID = optee_utee_sys::TEE_UUID {{
38 timeLow: {:#x},
39 timeMid: {:#x},
40 timeHiAndVersion: {:#x},
41 clockSeqAndNode: {:#x?},
42 }};",
43 time_low, time_mid, time_hi_and_version, clock_seq_and_node
44 )?;
45
46 let mut aarch64_flag = true;
47 match env::var("TARGET") {
48 Ok(ref v) if v == "arm-unknown-linux-gnueabihf" => {
49 println!("cargo:rustc-link-arg=--no-warn-mismatch");
50 aarch64_flag = false;
51 },
52 _ => {}
53 };
54
55 let optee_os_dir = env::var("TA_DEV_KIT_DIR").unwrap();
56 let search_path = Path::new(&optee_os_dir).join("lib");
57
58 let optee_os_path = &PathBuf::from(optee_os_dir.clone());
59 let mut ta_lds = File::create(out.join("ta.lds"))?;
60 let f = File::open(optee_os_path.join("src/ta.ld.S"))?;
61 let f = BufReader::new(f);
62
63 for line in f.lines() {
64 let l = line?;
65
66 if aarch64_flag {
67 if l.starts_with('#') ||
68 l == "OUTPUT_FORMAT(\"elf32-littlearm\")" ||
69 l == "OUTPUT_ARCH(arm)" {
70 continue;
71 }
72 } else {
73 if l.starts_with('#') ||
74 l == "OUTPUT_FORMAT(\"elf64-littleaarch64\")" ||
75 l == "OUTPUT_ARCH(aarch64)" {
76 continue;
77 }
78 }
79
80 if l == "\t. = ALIGN(4096);" {
81 write!(ta_lds, "\t. = ALIGN(65536);\n")?;
82 } else {
83 write!(ta_lds, "{}\n", l)?;
84 }
85 }
86
87 println!("cargo:rustc-link-search={}", out.display());
88 println!("cargo:rerun-if-changed=ta.lds");
89
90 println!("cargo:rustc-link-search={}", search_path.display());
91 println!("cargo:rustc-link-lib=static=utee");
92 println!("cargo:rustc-link-lib=static=utils");
93 println!("cargo:rustc-link-arg=-Tta.lds");
94 println!("cargo:rustc-link-arg=-e__ta_entry");
95 println!("cargo:rustc-link-arg=-pie");
96 println!("cargo:rustc-link-arg=-Os");
97 println!("cargo:rustc-link-arg=--sort-section=alignment");
98
99 let mut dyn_list = File::create(out.join("dyn_list"))?;
100 write!(dyn_list, "{{ __elf_phdr_info; trace_ext_prefix; trace_level; ta_head; }};\n")?;
101 println!("cargo:rustc-link-arg=--dynamic-list=dyn_list");
102 Ok(())
103 }
104