-
Notifications
You must be signed in to change notification settings - Fork 45
/
build.rs
125 lines (101 loc) · 3.5 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*++
Licensed under the Apache-2.0 license.
File Name:
build.rs
Abstract:
Build script for Caliptra ROM.
--*/
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
fn preprocess(filename: &str, defines: &[(String, String)]) -> Vec<u8> {
let mut cmd = Command::new("cc");
cmd.arg("-E");
for (key, val) in defines {
cmd.arg(format!("-D{key}={val}"));
}
cmd.arg(filename);
let out = cmd.output().unwrap();
if !out.status.success() {
panic!(
"failed to use cc preprocessor {} {}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
}
out.stdout
}
fn workspace_dir() -> PathBuf {
let output = std::process::Command::new(env!("CARGO"))
.arg("locate-project")
.arg("--workspace")
.arg("--message-format=plain")
.output()
.unwrap()
.stdout;
let cargo_path = Path::new(std::str::from_utf8(&output).unwrap().trim());
cargo_path.parent().unwrap().to_path_buf()
}
fn be_bytes_to_words(src: &[u8]) -> Vec<u32> {
let mut dst = Vec::<u32>::new();
for i in (0..src.len()).step_by(4) {
dst.push(u32::from_be_bytes(src[i..i + 4].try_into().unwrap()));
}
dst
}
fn main() {
if cfg!(not(feature = "std")) {
use std::fs;
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
fs::write(out_dir.join("rom.ld"), include_bytes!("src/rom.ld")).unwrap();
let preprocessor_vars: Vec<_> = env::vars()
.filter(|(k, _)| k.starts_with("CARGO_"))
.collect();
std::fs::write(
out_dir.join("start_preprocessed.S"),
preprocess("src/start.S", &preprocessor_vars),
)
.unwrap();
println!("cargo:rustc-link-search={}", out_dir.display());
println!("cargo:rustc-link-arg=-Trom.ld");
println!("cargo:rerun-if-changed=src/rom.ld");
println!("cargo:rerun-if-changed=src/start.S");
println!("cargo:rerun-if-changed=build.rs");
}
if cfg!(feature = "fake-rom") {
use x509_parser::nom::Parser;
use x509_parser::prelude::{FromDer, X509CertificateParser};
use x509_parser::signature_value::EcdsaSigValue;
let ws_dir = workspace_dir();
let ldev_file_path =
ws_dir.join("test/tests/caliptra_integration_tests/smoke_testdata/ldevid_cert.der");
println!(
"cargo:rerun-if-changed={}",
ldev_file_path.to_str().unwrap()
);
let ldev_file = std::fs::read(ldev_file_path).unwrap();
let mut parser = X509CertificateParser::new();
let (_, cert) = parser.parse(&ldev_file).unwrap();
let tbs = cert.tbs_certificate.as_ref();
let (_, sig) = EcdsaSigValue::from_der(cert.signature_value.as_ref()).unwrap();
// Get words of Signature r and s
let mut r = sig.r.as_ref();
r = &r[r.len() - 48..];
let r_words = be_bytes_to_words(r);
let mut s = sig.s.as_ref();
s = &s[s.len() - 48..];
let s_words = be_bytes_to_words(s);
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
std::fs::write(out_dir.join("ldev_tbs.der"), tbs).unwrap();
std::fs::write(
out_dir.join("ldev_sig_r_words.txt"),
format!("{:?}", r_words),
)
.unwrap();
std::fs::write(
out_dir.join("ldev_sig_s_words.txt"),
format!("{:?}", s_words),
)
.unwrap();
}
}