/
kernel_compile.rs
184 lines (153 loc) Β· 4.85 KB
/
kernel_compile.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
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
/// Module that pretends to build a Linux kernel
use rand::prelude::*;
use regex::Regex;
use crate::utils::csleep;
use crate::CFILES_LIST;
use crate::parse_args::AppConfig;
/// Generate a build step for a header file
fn gen_header(arch: &str, rng: &mut ThreadRng) -> String {
const RARE_CMDS: &[&str] = &[
"SYSTBL ",
"SYSHDR ",
];
const CMDS: &[&str] = &[
"WRAP ",
"CHK ",
"UPD ",
];
let cmd = if rng.gen_bool(1.0 / 15.0) {
RARE_CMDS.choose(rng).unwrap_or(&"")
} else {
CMDS.choose(rng).unwrap_or(&"")
};
let cfile = &CFILES_LIST.choose(rng).unwrap_or(&"");
let mut file = format!("{}h", &cfile[..cfile.len() - 1]);
if file.starts_with("arch") {
let re = Regex::new(r"arch/([a-z0-9_])+/").unwrap();
file = re.replace(&file, format!("arch/{}/", arch).as_str()).into_owned();
}
format!(" {} {}", cmd, file)
}
/// Generate a build step for an object file
fn gen_object(arch: &str, rng: &mut ThreadRng) -> String {
const RARE_CMDS: &[&str] = &[
"HOSTCC ",
"AS ",
];
let cmd = if rng.gen_bool(1.0 / 15.0) {
RARE_CMDS.choose(rng).unwrap_or(&"")
} else if rng.gen_bool(0.33) {
"AR "
} else {
"CC "
};
let cfile = &CFILES_LIST.choose(rng).unwrap_or(&"");
let mut file = format!("{}o", cfile[..cfile.len() - 1].to_owned());
if file.starts_with("arch") {
let re = Regex::new(r"arch/([a-z0-9_])+/").unwrap();
file = re.replace(&file, format!("arch/{}/", arch).as_str()).into_owned();
}
format!(" {} {}", cmd, file)
}
/// Generate a 'special' build step
fn gen_special(arch: &str, rng: &mut ThreadRng) -> String {
const SPECIALS: &[&str] = &[
"HOSTLD arch/ARCH/tools/relocs",
"HOSTLD scripts/mod/modpost",
"MKELF scripts/mod/elfconfig.h",
"LDS arch/ARCH/entry/vdso/vdso32/vdso32.lds",
"LDS arch/ARCH/kernel/vmlinux.lds",
"LDS arch/ARCH/realmode/rm/realmode.lds",
"LDS arch/ARCH/boot/compressed/vmlinux.lds",
"EXPORTS arch/ARCH/lib/lib-ksyms.o",
"EXPORTS lib/lib-ksyms.o",
"MODPOST vmlinux.o",
"SORTEX vmlinux",
"SYSMAP System.map",
"VOFFSET arch/ARCH/boot/compressed/../voffset.h",
"OBJCOPY arch/ARCH/entry/vdso/vdso32.so",
"OBJCOPY arch/ARCH/realmode/rm/realmode.bin",
"OBJCOPY arch/ARCH/boot/compressed/vmlinux.bin",
"OBJCOPY arch/ARCH/boot/vmlinux.bin",
"VDSO2C arch/ARCH/entry/vdso/vdso-image-32.c",
"VDSO arch/ARCH/entry/vdso/vdso32.so.dbg",
"RELOCS arch/ARCH/realmode/rm/realmode.relocs",
"PASYMS arch/ARCH/realmode/rm/pasyms.h",
"XZKERN arch/ARCH/boot/compressed/vmlinux.bin.xz",
"MKPIGGY arch/ARCH/boot/compressed/piggy.S",
"DATAREL arch/ARCH/boot/compressed/vmlinux",
"ZOFFSET arch/ARCH/boot/zoffset.h",
];
let special = SPECIALS.choose(rng).unwrap_or(&"").to_string();
let special = special.replace("ARCH", arch);
format!(" {}", special)
}
/// Generates a line from `make` output
fn gen_line(arch: &str, rng: &mut ThreadRng) -> String {
if rng.gen_bool(1.0 / 50.0) {
gen_special(arch, rng)
} else if rng.gen_bool(0.1) {
gen_header(arch, rng)
} else {
gen_object(arch, rng)
}
}
pub fn run(appconfig: &AppConfig) {
let mut rng = thread_rng();
let num_lines = rng.gen_range(50, 500);
const ARCHES: &[&str] = &[
"alpha",
"arc",
"arm",
"arm64",
"blackfin",
"c6x",
"cris",
"frv",
"h8300",
"hexagon",
"ia64",
"m32r",
"m68k",
"metag",
"microblaze",
"mips",
"mn10300",
"nios2",
"openrisc",
"parisc",
"powerpc",
"s390",
"score",
"sh",
"sparc",
"tile",
"um",
"unicore32",
"x86",
"xtensa",
];
let arch = ARCHES.choose(&mut rng).unwrap_or(&"x86");
for _ in 1..num_lines {
let line = gen_line(arch, &mut rng);
let sleep_length = rng.gen_range(10, 1000);
println!("{}", line);
csleep(sleep_length);
if appconfig.should_exit() {
return;
}
}
println!("BUILD arch/{}/boot/bzImage", arch);
println!();
let bytes: u32 = rng.gen_range(9000, 1_000_000);
let padded_bytes: u32 = rng.gen_range(bytes, 1_100_000);
println!("Setup is {} bytes (padded to {} bytes).",
bytes,
padded_bytes);
let system: u32 = rng.gen_range(300, 3000);
println!("System is {} kB", system);
let crc: u32 = rng.gen_range(0x1000_0000, 0xffff_ffff);
println!("CRC {:x}", crc);
println!("Kernel: arch/{}/boot/bzImage is ready (#1)", arch);
println!();
}