This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathefi.rs
61 lines (52 loc) · 1.89 KB
/
efi.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
use std::io::Write;
use std::path::Path;
use std::process::Command;
use tempfile::NamedTempFile;
use super::BootableToplevel;
use crate::Result;
pub struct EfiProgram {
pub source: BootableToplevel,
}
impl EfiProgram {
pub fn new(source: BootableToplevel) -> Self {
Self { source }
}
pub fn write_unified_efi(&self, objcopy: &Path, outpath: &Path, stub: &Path) -> Result<()> {
let generation_path = &self.source.toplevel.0;
let mut kernel_params = NamedTempFile::new()?;
write!(
kernel_params,
"init={} {}",
self.source.init.display(),
self.source.kernel_params.join(" ")
)?;
// Offsets taken from one of systemd's EFI tests:
// https://github.com/systemd/systemd/blob/01d0123f044d6c090b6ac2f6d304de2bdb19ae3b/test/test-efi-create-disk.sh#L32-L38
let status = Command::new(objcopy)
.args(&[
"--add-section",
&format!(".osrel={}/etc/os-release", generation_path.display()),
"--change-section-vma",
".osrel=0x20000",
"--add-section",
&format!(".cmdline={}", kernel_params.path().display()),
"--change-section-vma",
".cmdline=0x30000",
"--add-section",
&format!(".linux={}/kernel", generation_path.display()),
"--change-section-vma",
".linux=0x2000000",
"--add-section",
&format!(".initrd={}/initrd", generation_path.display()),
"--change-section-vma",
".initrd=0x3000000",
&stub.display().to_string(),
&outpath.display().to_string(),
])
.status()?;
if !status.success() {
return Err("failed to write unified efi".into());
}
Ok(())
}
}