Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some more lanzatool refactoring #43

Merged
merged 3 commits into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions rust/lanzatool/src/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn lanzaboote_image(
let kernel_path_file = write_to_tmp(
target_dir,
"kernel-esp-path",
esp_relative_path_string(esp, kernel_path)?,
esp_relative_uefi_path(esp, kernel_path)?,
)?;
let kernel_hash_file = write_to_tmp(
target_dir,
Expand All @@ -43,7 +43,7 @@ pub fn lanzaboote_image(
let initrd_path_file = write_to_tmp(
target_dir,
"initrd-esp-path",
esp_relative_path_string(esp, initrd_path)?,
esp_relative_uefi_path(esp, initrd_path)?,
)?;
let initrd_hash_file = write_to_tmp(
target_dir,
Expand Down Expand Up @@ -167,17 +167,24 @@ fn write_to_tmp(
Ok(path)
}

fn esp_relative_path_string(esp: &Path, path: &Path) -> Result<String> {
/// Convert a path to an UEFI path relative to the specified ESP.
fn esp_relative_uefi_path(esp: &Path, path: &Path) -> Result<String> {
let relative_path = path
.strip_prefix(esp)
.with_context(|| format!("Failed to strip prefix: {:?} from path: {:?}", esp, path))?
.to_owned();
let relative_path_string = relative_path
.into_os_string()
.into_string()
.expect("Failed to convert path '{}' to a relative string path")
.replace('/', "\\");
Ok(format!("\\{}", &relative_path_string))
.with_context(|| format!("Failed to strip esp prefix: {:?} from: {:?}", esp, path))?;
let uefi_path = uefi_path(relative_path)?;
Ok(format!("\\{}", &uefi_path))
}

/// Convert a path to a UEFI string representation.
///
/// This might not _necessarily_ produce a valid UEFI path, since some UEFI implementations might
/// not support UTF-8 strings. A Rust String, however, is _always_ valid UTF-8.
fn uefi_path(path: &Path) -> Result<String> {
nikstur marked this conversation as resolved.
Show resolved Hide resolved
path.to_str()
.to_owned()
.map(|x| x.replace('/', "\\"))
.with_context(|| format!("Failed to convert {:?} to an UEFI path", path))
}

fn stub_offset(binary: &Path) -> Result<u64> {
Expand Down Expand Up @@ -205,13 +212,34 @@ fn image_base(pe: &PE) -> u64 {
}

fn file_size(path: impl AsRef<Path>) -> Result<u64> {
Ok(fs::File::open(&path)
Ok(fs::metadata(&path)
.with_context(|| {
format!(
"Failed to read file to calculate its size: {:?}",
"Failed to read file metadata to calculate its size: {:?}",
path.as_ref()
)
})?
.metadata()?
.size())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn convert_to_valid_uefi_path_relative_to_esp() {
let esp = Path::new("esp");
let path = Path::new("esp/lanzaboote/is/great.txt");
let converted_path = esp_relative_uefi_path(esp, path).unwrap();
let expected_path = String::from("\\lanzaboote\\is\\great.txt");
assert_eq!(converted_path, expected_path);
}

#[test]
fn convert_to_valid_uefi_path() {
let path = Path::new("lanzaboote/is/great.txt");
let converted_path = uefi_path(path).unwrap();
let expected_path = String::from("lanzaboote\\is\\great.txt");
assert_eq!(converted_path, expected_path);
}
}
6 changes: 4 additions & 2 deletions rust/lanzatool/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::Result;
use anyhow::{Context, Result};

pub struct KeyPair {
pub private_key: PathBuf,
Expand Down Expand Up @@ -32,7 +32,9 @@ impl KeyPair {
let output = Command::new("sbsign").args(&args).output()?;

if !output.status.success() {
std::io::stderr().write_all(&output.stderr).unwrap();
std::io::stderr()
.write_all(&output.stderr)
.context("Failed to write output of sbsign to stderr")?;
return Err(anyhow::anyhow!(
"Failed to sign file using sbsign with args `{:?}`",
&args
Expand Down