From a65998945d7590356c02ca689dc28b800ac9fd1e Mon Sep 17 00:00:00 2001 From: nikstur Date: Wed, 23 Nov 2022 18:04:57 +0100 Subject: [PATCH] lanzatool: implement relative esp paths --- rust/lanzatool/src/stub.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/rust/lanzatool/src/stub.rs b/rust/lanzatool/src/stub.rs index 286ade0..c589ced 100644 --- a/rust/lanzatool/src/stub.rs +++ b/rust/lanzatool/src/stub.rs @@ -16,11 +16,12 @@ pub fn assemble( // objcopy copies files into the PE binary. That's why we have to write the contents // of some bootspec properties to disk let kernel_cmdline_file = Path::new("/tmp/kernel_cmdline"); - fs::write(kernel_cmdline_file, kernel_cmdline.join(" "))?; let kernel_path_file = Path::new("/tmp/kernel_path"); - fs::write(kernel_path_file, path_to_string(kernel_path))?; let initrd_path_file = Path::new("/tmp/initrd_path"); - fs::write(initrd_path_file, path_to_string(initrd_path))?; + + fs::write(kernel_cmdline_file, kernel_cmdline.join(" "))?; + fs::write(kernel_path_file, efi_relative_path_string(kernel_path))?; + fs::write(initrd_path_file, efi_relative_path_string(initrd_path))?; let pe_binary = fs::read(lanzaboote_bin)?; let pe = goblin::pe::PE::parse(&pe_binary)?; @@ -77,6 +78,19 @@ pub fn assemble( Ok(lanzaboote_image) } +fn efi_relative_path_string(path: &Path) -> String { + let relative_path = path + .strip_prefix("esp") + .expect("Failed to make path relative to esp") + .to_owned(); + let relative_path_string = relative_path + .into_os_string() + .into_string() + .expect("Failed to convert path '{}' to a relative string path") + .replace("/", "\\"); + format!("\\{}", &relative_path_string) +} + // All Linux file paths should be convertable to strings fn path_to_string(path: &Path) -> String { path.to_owned()