Move PE parsing into its own module

This commit is contained in:
Julian Stecklina 2022-11-22 16:18:12 +01:00
parent 9aab0d27da
commit 76e7635de8
4 changed files with 73 additions and 16 deletions

View File

@ -26,11 +26,23 @@ version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f2d21333b679bbbac680b3eb45c86937e42f69277028f4e97b599b80b86c253" checksum = "1f2d21333b679bbbac680b3eb45c86937e42f69277028f4e97b599b80b86c253"
[[package]]
name = "goblin"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "572564d6cba7d09775202c8e7eebc4d534d5ae36578ab402fb21e182a0ac9505"
dependencies = [
"log",
"plain",
"scroll",
]
[[package]] [[package]]
name = "lanzaboote" name = "lanzaboote"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"ed25519-compact", "ed25519-compact",
"goblin",
"log", "log",
"uefi", "uefi",
"uefi-services", "uefi-services",
@ -45,6 +57,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "plain"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.47" version = "1.0.47"
@ -63,6 +81,26 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "scroll"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04c565b551bafbef4157586fa379538366e4385d42082f255bfd96e4fe8519da"
dependencies = [
"scroll_derive",
]
[[package]]
name = "scroll_derive"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdbda6ac5cd1321e724fa9cee216f3a61885889b896f073b8f82322789c5250e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.103" version = "1.0.103"

View File

@ -5,10 +5,11 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
uefi = { version = "0.18.0", features = [ "alloc", "logger", "exts" ] } uefi = { version = "0.18.0", default-features = false, features = [ "alloc", "exts" ] }
uefi-services = "0.15.0" uefi-services = { version = "0.15.0", default-features = false, features = [ "panic_handler" ] }
log = "0.4.17" log = "0.4.17"
ed25519-compact = { version = "2.0.2", default-features = false, features = [] } ed25519-compact = { version = "2.0.2", default-features = false, features = [] }
goblin = { version = "0.6.0", default-features = false, features = [ "pe64", "alloc" ]}
[profile.release] [profile.release]
opt-level = "s" opt-level = "s"

View File

@ -4,26 +4,24 @@
extern crate alloc; extern crate alloc;
mod pe_section;
use alloc::vec::Vec; use alloc::vec::Vec;
use log::debug; use log::{debug, info};
use uefi::{ use uefi::{
prelude::*, prelude::*,
proto::{ proto::{
console::text::Output, console::text::Output,
device_path::{ device_path::text::{AllowShortcuts, DevicePathToText, DisplayOnly},
text::{AllowShortcuts, DevicePathToText, DisplayOnly}, loaded_image::LoadedImage,
DevicePath, media::file::{File, FileAttribute, FileMode, RegularFile},
},
loaded_image::{self, LoadedImage},
media::{
file::{File, FileAttribute, FileMode, RegularFile},
fs::SimpleFileSystem,
},
}, },
table::boot::{OpenProtocolAttributes, OpenProtocolParams}, table::boot::{OpenProtocolAttributes, OpenProtocolParams},
Error, Result, Result,
}; };
use crate::pe_section::pe_section;
fn print_logo(output: &mut Output) { fn print_logo(output: &mut Output) {
output.clear().unwrap(); output.clear().unwrap();
@ -36,6 +34,7 @@ fn print_logo(output: &mut Output) {
| |/ _` | '_ \\|_ / _` | '_ \\ / _ \\ / _ \\| __|\r | |/ _` | '_ \\|_ / _` | '_ \\ / _ \\ / _ \\| __|\r
| | (_| | | | |/ / (_| | |_) | (_) | (_) | |_ \r | | (_| | | | |/ / (_| | |_) | (_) | (_) | |_ \r
|_|\\__,_|_| |_/___\\__,_|_.__/ \\___/ \\___/ \\__|\r |_|\\__,_|_| |_/___\\__,_|_.__/ \\___/ \\___/ \\__|\r
\r
" "
)) ))
.unwrap(); .unwrap();
@ -103,13 +102,18 @@ fn main(handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
let boot_services = system_table.boot_services(); let boot_services = system_table.boot_services();
let image_file = image_file(boot_services, handle).unwrap(); {
let mut image_file = image_file(boot_services, handle).unwrap();
let image_data = read_all(&mut image_file).unwrap();
if let Some(data) = pe_section(&image_data, ".osrel") {
info!("osrel = {}", core::str::from_utf8(data).unwrap_or("???"))
}
}
let mut file_system = boot_services.get_image_file_system(handle).unwrap(); let mut file_system = boot_services.get_image_file_system(handle).unwrap();
let mut root = file_system.open_volume().unwrap(); let mut root = file_system.open_volume().unwrap();
debug!("Found root");
let mut file = root let mut file = root
.open(cstr16!("linux.efi"), FileMode::Read, FileAttribute::empty()) .open(cstr16!("linux.efi"), FileMode::Read, FileAttribute::empty())
.unwrap() .unwrap()

View File

@ -0,0 +1,14 @@
pub fn pe_section<'a>(file_data: &'a [u8], section_name: &str) -> Option<&'a [u8]> {
let pe_binary = goblin::pe::PE::parse(file_data).ok()?;
pe_binary
.sections
.iter()
.find(|s| s.name().unwrap() == section_name)
.and_then(|s| {
let section_start: usize = s.pointer_to_raw_data.try_into().ok()?;
let section_end: usize = section_start + usize::try_from(s.size_of_raw_data).ok()?;
Some(&file_data[section_start..section_end])
})
}