lanzatool: detrashify

This commit is contained in:
nikstur 2022-11-23 17:16:08 +01:00
parent de451fa5af
commit 5dbb8e7452
3 changed files with 33 additions and 18 deletions

View File

@ -1,8 +1,8 @@
{ {
"v1": { "v1": {
"init": "/nix/store/7zrsjhxi0c93m2l89rj8jdp9khm8fc6s-nixos-system-tuxedo-22.11.20221115.85d6b39/init", "init": "/run/current-system/init",
"initrd": "/nix/store/7a4plccwni1sldhyra75f7m44xgsgiqw-initrd-linux-6.0.8/initrd", "initrd": "/run/current-system/initrd",
"kernel": "/nix/store/nsw0422iwp4linayqx727pi4fdyja0wv-linux-6.0.8/bzImage", "kernel": "/run/current-system/kernel",
"kernelParams": [ "kernelParams": [
"amd_iommu=on", "amd_iommu=on",
"amd_iommu=pt", "amd_iommu=pt",
@ -13,12 +13,12 @@
"systemd.unified_cgroup_hierarchy=1", "systemd.unified_cgroup_hierarchy=1",
"loglevel=4" "loglevel=4"
], ],
"label": "NixOS 21.11.20210810.dirty (Linux 5.15.30)", "label": "LanzaOS",
"toplevel": "/nix/store/7zrsjhxi0c93m2l89rj8jdp9khm8fc6s-nixos-system-tuxedo-22.11.20221115.85d6b39", "toplevel": "/run/current-system",
"extension": { "extension": {
"esp": "test", "esp": "esp",
"bootctl": "/nix/store/89366aivz6v8a34yyni2m04ca9hwrl92-systemd-250.4/bin/bootctl", "bootctl": "/run/current-system/sw/bin/bootctl",
"osRelease": "/nix/store/734vglb01ssz73wlihad7xa9yzvwlvx6-etc-os-release" "osRelease": "/etc/os-release"
} }
} }
} }

View File

@ -21,7 +21,7 @@ pub fn install(_: &Path, bootspec: &Path, lanzaboote_bin: &Path) -> Result<()> {
&esp_paths.kernel, &esp_paths.kernel,
&esp_paths.initrd, &esp_paths.initrd,
) )
.unwrap(); .expect("Failed to assemble stub");
// Copy the files to the ESP // Copy the files to the ESP
fs::create_dir_all(&esp_paths.nixos)?; fs::create_dir_all(&esp_paths.nixos)?;

View File

@ -18,20 +18,29 @@ pub fn assemble(
let kernel_cmdline_file = Path::new("/tmp/kernel_cmdline"); let kernel_cmdline_file = Path::new("/tmp/kernel_cmdline");
fs::write(kernel_cmdline_file, kernel_cmdline.join(" "))?; fs::write(kernel_cmdline_file, kernel_cmdline.join(" "))?;
let kernel_path_file = Path::new("/tmp/kernel_path"); let kernel_path_file = Path::new("/tmp/kernel_path");
fs::write(kernel_path_file, kernel_path.to_str().unwrap())?; fs::write(kernel_path_file, path_to_string(kernel_path))?;
let initrd_path_file = Path::new("/tmp/initrd_path"); let initrd_path_file = Path::new("/tmp/initrd_path");
fs::write(initrd_path_file, initrd_path.to_str().unwrap())?; fs::write(initrd_path_file, path_to_string(initrd_path))?;
let pe_binary = fs::read(lanzaboote_bin)?; let pe_binary = fs::read(lanzaboote_bin)?;
let pe = goblin::pe::PE::parse(&pe_binary)?; let pe = goblin::pe::PE::parse(&pe_binary)?;
let image_base = pe
.header
.optional_header
.expect("Failed to find optional header, you're fucked")
.windows_fields
.image_base;
let os_release_offs = u64::from( let os_release_offs = u64::from(
pe.sections pe.sections
.iter() .last()
.find(|s| s.name().unwrap() == ".sdmagic") .and_then(|s| Some(s.virtual_size + s.virtual_address))
.and_then(|s| Some(s.size_of_raw_data + s.virtual_address)) .expect("Failed to find the offset"),
.unwrap(),
); );
// The Virtual Memory Addresss (VMA) is relative ot the image base, aka the image base
// needs to be added to the virtual address to get the actual (but still virtual address)
let os_release_offs = os_release_offs + image_base;
let kernel_cmdline_offs = os_release_offs + file_size(os_release)?; let kernel_cmdline_offs = os_release_offs + file_size(os_release)?;
let initrd_path_offs = kernel_cmdline_offs + file_size(kernel_cmdline_file)?; let initrd_path_offs = kernel_cmdline_offs + file_size(kernel_cmdline_file)?;
@ -54,8 +63,8 @@ pub fn assemble(
format!(".kernelp={}", path_to_string(kernel_path_file)), format!(".kernelp={}", path_to_string(kernel_path_file)),
String::from("--change-section-vma"), String::from("--change-section-vma"),
format!(".kernelp={:#x}", kernel_path_offs), format!(".kernelp={:#x}", kernel_path_offs),
lanzaboote_bin.to_str().unwrap().to_owned(), path_to_string(lanzaboote_bin),
String::from("stub.efi"), String::from("lanzaboote-image.efi"),
]; ];
let status = Command::new("objcopy").args(&args).status()?; let status = Command::new("objcopy").args(&args).status()?;
@ -68,7 +77,13 @@ pub fn assemble(
// All Linux file paths should be convertable to strings // All Linux file paths should be convertable to strings
fn path_to_string(path: &Path) -> String { fn path_to_string(path: &Path) -> String {
path.to_owned().into_os_string().into_string().unwrap() path.to_owned()
.into_os_string()
.into_string()
.expect(&format!(
"Failed to convert path '{}' to a string",
path.display()
))
} }
fn file_size(path: &Path) -> Result<u64> { fn file_size(path: &Path) -> Result<u64> {