-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
216 lines (177 loc) · 5 KB
/
Copy pathbuild.rs
File metadata and controls
216 lines (177 loc) · 5 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::{
env, fs,
path::{Path, PathBuf},
process::Command,
};
use walkdir::WalkDir;
const FIRMWARE_DIR: &str = "guest/firmware";
const BUILD_DIR: &str = "guest/firmware/build";
const ACPI_DIR: &str = "acpi";
fn tool(env_var: &str, default: &str) -> String {
env::var(env_var).unwrap_or_else(|_| default.to_owned())
}
fn build_acpi(asl: &str) {
let dsdt = format!("{ACPI_DIR}/DSDT.dsl");
run(asl, &["-tc", &dsdt], "failed to compile DSDT");
}
fn run(cmd: &str, args: &[&str], error: &str) {
let status = Command::new(cmd)
.args(args)
.status()
.unwrap_or_else(|e| panic!("{error}: {e}"));
if !status.success() {
panic!("{error}");
}
}
fn assemble(asm: &str, input: &str, output: &str, format: &str) {
run(
asm,
&["-f", format, input, "-o", output],
&format!("failed to assemble {input}"),
);
}
fn compile_c32(cc32: &str, input: &str, output: &str) {
run(
cc32,
&[
"-m32",
"-ffreestanding",
"-fno-stack-protector",
"-nostdlib",
"-O2",
"-c",
input,
"-o",
output,
],
&format!("failed to compile {input}"),
);
}
fn compile_c64(cc64: &str, input: &str, output: &str) {
run(
cc64,
&[
"-m64",
"-ffreestanding",
"-fno-stack-protector",
"-mno-red-zone",
"-mcmodel=kernel",
"-fno-pic",
"-fno-pie",
"-nostdlib",
"-O2",
"-c",
input,
"-o",
output,
],
&format!("failed to compile {input}"),
);
}
fn link(ld: &str, output: &str, linker_script: &str, inputs: &[&str], extra_args: &[&str]) {
let mut args = Vec::new();
args.extend_from_slice(extra_args);
args.extend_from_slice(&["-T", linker_script, "-o", output]);
args.extend_from_slice(inputs);
run(ld, &args, "linking failed");
}
fn objcopy_binary(objcopy: &str, input: &str, output: &str) {
run(
objcopy,
&["-O", "binary", input, output],
&format!("failed to objcopy {input}"),
);
}
fn build_firmware(asm: &str, asl: &str, cc32: &str, cc64: &str, ld: &str, objcopy: &str) {
fs::create_dir_all(BUILD_DIR).unwrap();
build_acpi(asl);
// ===== Assembly =====
let entry32_o = format!("{BUILD_DIR}/entry.o");
assemble(
asm,
&format!("{FIRMWARE_DIR}/assembly/entry.asm"),
&entry32_o,
"elf32",
);
let idt_o = format!("{BUILD_DIR}/idt_handlers.o");
assemble(
asm,
&format!("{FIRMWARE_DIR}/assembly/idt_handlers.asm"),
&idt_o,
"elf64",
);
let entry64_o = format!("{BUILD_DIR}/entry64.o");
assemble(
asm,
&format!("{FIRMWARE_DIR}/assembly/entry64.asm"),
&entry64_o,
"elf64",
);
// ===== C =====
let main32_o = format!("{BUILD_DIR}/main.o");
compile_c32(cc32, &format!("{FIRMWARE_DIR}/main.c"), &main32_o);
let main64_o = format!("{BUILD_DIR}/main64.o");
compile_c64(cc64, &format!("{FIRMWARE_DIR}/main64.c"), &main64_o);
// ===== 64-bit firmware =====
let main64_elf = format!("{BUILD_DIR}/main64.elf");
let main64_bin = format!("{BUILD_DIR}/main64.bin");
link(
ld,
&main64_elf,
&format!("{FIRMWARE_DIR}/linkerscript/linker64.ld"),
&[&idt_o, &entry64_o, &main64_o],
&[],
);
objcopy_binary(objcopy, &main64_elf, &main64_bin);
// ===== 32-bit firmware =====
let firmware_elf = format!("{BUILD_DIR}/out.elf");
let firmware_bin = format!("{BUILD_DIR}/out.bin");
link(
ld,
&firmware_elf,
&format!("{FIRMWARE_DIR}/linkerscript/linker.ld"),
&[&entry32_o, &main32_o],
&["-m", "elf_i386"],
);
objcopy_binary(objcopy, &firmware_elf, &firmware_bin);
}
fn build_tests(asm: &str) {
for entry in WalkDir::new("guest/tests")
.into_iter()
.filter_map(Result::ok)
{
let path = entry.path();
if !path.is_file() {
continue;
}
if path.extension().and_then(|e| e.to_str()) != Some("asm") {
continue;
}
compile_test(asm, path);
}
}
fn compile_test(asm: &str, path: &Path) {
println!("Compiling {}", path.display());
let output: PathBuf = path.with_extension("bin");
run(
asm,
&[
"-f",
"bin",
path.to_str().unwrap(),
"-o",
output.to_str().unwrap(),
],
&format!("failed to assemble {}", path.display()),
);
}
fn main() {
let asm = tool("FERRUM_ASM", "nasm");
let asl = tool("FERRUM_ASL", "iasl");
let cc32 = tool("FERRUM_CC32", "i686-elf-gcc");
let cc64 = tool("FERRUM_CC64", "gcc");
let ld = tool("FERRUM_LD", "ld");
let objcopy = tool("FERRUM_OBJCOPY", "objcopy");
build_firmware(&asm, &asl, &cc32, &cc64, &ld, &objcopy);
build_tests(&asm);
}