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

deps: update to uefi-rs 0.21.0 #178

Merged
merged 1 commit into from
May 20, 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
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.

4 changes: 2 additions & 2 deletions rust/stub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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"

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
54 changes: 12 additions & 42 deletions rust/stub/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,9 @@ use pe_loader::Image;
use pe_section::{pe_section, pe_section_as_string};
use sha2::{Digest, Sha256};
use tpm::tpm_available;
use uefi::{
prelude::*,
proto::{
loaded_image::LoadedImage,
media::file::{File, FileAttribute, FileMode},
},
CStr16, CString16, Result,
};

use crate::{
linux_loader::InitrdLoader,
uefi_helpers::{booted_image_file, read_all},
};
use uefi::{prelude::*, proto::loaded_image::LoadedImage, CStr16, CString16, Result};

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

type Hash = sha2::digest::Output<Sha256>;

Expand Down Expand Up @@ -127,7 +117,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 +163,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 +193,13 @@ 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
25 changes: 1 addition & 24 deletions rust/stub/src/uefi_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
use core::ffi::c_void;

use alloc::vec::Vec;
use uefi::{
prelude::BootServices,
proto::{loaded_image::LoadedImage, media::file::RegularFile},
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)
}
use uefi::{prelude::BootServices, proto::loaded_image::LoadedImage, Result};

#[derive(Debug, Clone, Copy)]
pub struct PeInMemory {
Expand Down