Skip to content

Commit

Permalink
deps: update to uefi-rs 0.21.0
Browse files Browse the repository at this point in the history
It simplifies our filesystem handling.
  • Loading branch information
RaitoBezarius committed May 20, 2023
1 parent 3fb74cf commit e29d621
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 86 deletions.
113 changes: 86 additions & 27 deletions rust/stub/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions rust/stub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ publish = false
rust-version = "1.68"

[dependencies]
uefi = { version = "0.20.0", default-features = false, features = [ "alloc", "global_allocator" ] }
uefi-services = { version = "0.17.0", default-features = false, features = [ "panic_handler", "logger" ] }
uefi = { version = "0.21.0", default-features = false, features = [ "alloc", "global_allocator" ] }
uefi-services = { version = "0.18.0", default-features = false, features = [ "panic_handler", "logger" ] }
goblin = { version = "0.6.1", default-features = false, features = [ "pe64", "alloc" ]}
bitflags = "2.2.1"

# Even in debug builds, we don't enable the debug logs, because they generate a lot of spam from goblin.
log = { version = "0.4.17", default-features = false, features = [ "max_level_info", "release_max_level_warn" ]}

# Use software implementation because the UEFI target seems to need it.
Expand Down
2 changes: 1 addition & 1 deletion rust/stub/src/efivars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ where
runtime_services.set_variable(name, vendor, attributes, &get_fallback_value()?)?;
}

uefi::Status::SUCCESS.into()
Ok(())
}

/// Exports systemd-stub style EFI variables
Expand Down
1 change: 0 additions & 1 deletion rust/stub/src/linux_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ impl InitrdLoader {
let mut proto = Box::pin(LoadFile2Protocol {
load_file: raw_load_file,
initrd_data,
_no_send_or_sync: Default::default(),
});

// Linux finds the right handle by looking for something that
Expand Down
42 changes: 8 additions & 34 deletions rust/stub/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ use sha2::{Digest, Sha256};
use tpm::tpm_available;
use uefi::{
prelude::*,
proto::{
loaded_image::LoadedImage,
media::file::{File, FileAttribute, FileMode},
},
proto::loaded_image::LoadedImage,
CStr16, CString16, Result,
};

use crate::{
linux_loader::InitrdLoader,
uefi_helpers::{booted_image_file, read_all},
uefi_helpers::booted_image_file,
};

type Hash = sha2::digest::Output<Sha256>;
Expand Down Expand Up @@ -127,7 +124,7 @@ fn boot_linux_unchecked(
let status = unsafe { kernel.start(handle, &system_table, kernel_cmdline) };

initrd_loader.uninstall(system_table.boot_services())?;
status.into()
status.to_result()
}

/// Boot the Linux kernel via the UEFI PE loader.
Expand Down Expand Up @@ -173,7 +170,7 @@ fn boot_linux_uefi(
.status();

initrd_loader.uninstall(system_table.boot_services())?;
status.into()
status.to_result()
}

#[entry]
Expand Down Expand Up @@ -203,33 +200,10 @@ fn main(handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
.boot_services()
.get_image_file_system(handle)
.expect("Failed to get file system handle");
let mut root = file_system
.open_volume()
.expect("Failed to find ESP root directory");

let mut kernel_file = root
.open(
&config.kernel_filename,
FileMode::Read,
FileAttribute::empty(),
)
.expect("Failed to open kernel file for reading")
.into_regular_file()
.expect("Kernel is not a regular file");

kernel_data = read_all(&mut kernel_file).expect("Failed to read kernel file into memory");

let mut initrd_file = root
.open(
&config.initrd_filename,
FileMode::Read,
FileAttribute::empty(),
)
.expect("Failed to open initrd for reading")
.into_regular_file()
.expect("Initrd is not a regular file");

initrd_data = read_all(&mut initrd_file).expect("Failed to read kernel file into memory");


kernel_data = file_system.read(&*config.kernel_filename).expect("Failed to read kernel file into memory");
initrd_data = file_system.read(&*config.initrd_filename).expect("Failed to read initrd file into memory");
}

let is_kernel_hash_correct = Sha256::digest(&kernel_data) == config.kernel_hash;
Expand Down
22 changes: 2 additions & 20 deletions rust/stub/src/uefi_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
use core::ffi::c_void;

use alloc::vec::Vec;

use uefi::{
prelude::BootServices,
proto::{loaded_image::LoadedImage, media::file::RegularFile},
proto::loaded_image::LoadedImage,
Result,
};

/// Read the whole file into a vector.
pub fn read_all(file: &mut RegularFile) -> Result<Vec<u8>> {
let mut buf = Vec::new();

loop {
let mut chunk = [0; 512];
let read_bytes = file.read(&mut chunk).map_err(|e| e.status())?;

if read_bytes == 0 {
break;
}

buf.extend_from_slice(&chunk[0..read_bytes]);
}

Ok(buf)
}

#[derive(Debug, Clone, Copy)]
pub struct PeInMemory {
image_base: *const c_void,
Expand Down

0 comments on commit e29d621

Please sign in to comment.