Create uefi helpers module

This commit is contained in:
Julian Stecklina 2022-11-22 16:24:09 +01:00
parent 76e7635de8
commit 23d8929546
2 changed files with 23 additions and 22 deletions

View File

@ -5,8 +5,8 @@
extern crate alloc; extern crate alloc;
mod pe_section; mod pe_section;
mod uefi_helpers;
use alloc::vec::Vec;
use log::{debug, info}; use log::{debug, info};
use uefi::{ use uefi::{
prelude::*, prelude::*,
@ -20,7 +20,7 @@ use uefi::{
Result, Result,
}; };
use crate::pe_section::pe_section; use crate::{pe_section::pe_section, uefi_helpers::read_all};
fn print_logo(output: &mut Output) { fn print_logo(output: &mut Output) {
output.clear().unwrap(); output.clear().unwrap();
@ -40,24 +40,6 @@ fn print_logo(output: &mut Output) {
.unwrap(); .unwrap();
} }
fn read_all(image: &mut RegularFile) -> Result<Vec<u8>> {
let mut buf = Vec::new();
// TODO Can we do this nicer?
loop {
let mut chunk = [0; 512];
let read_bytes = image.read(&mut chunk).map_err(|e| e.status())?;
if read_bytes == 0 {
break;
}
buf.extend_from_slice(&chunk[0..read_bytes]);
}
Ok(buf)
}
fn image_file(boot_services: &BootServices, image: Handle) -> Result<RegularFile> { fn image_file(boot_services: &BootServices, image: Handle) -> Result<RegularFile> {
let mut file_system = boot_services.get_image_file_system(image)?; let mut file_system = boot_services.get_image_file_system(image)?;
let mut root = file_system.open_volume()?; let mut root = file_system.open_volume()?;
@ -103,8 +85,7 @@ 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 mut image_file = image_file(boot_services, handle).unwrap(); let image_data = read_all(&mut image_file(boot_services, handle).unwrap()).unwrap();
let image_data = read_all(&mut image_file).unwrap();
if let Some(data) = pe_section(&image_data, ".osrel") { if let Some(data) = pe_section(&image_data, ".osrel") {
info!("osrel = {}", core::str::from_utf8(data).unwrap_or("???")) info!("osrel = {}", core::str::from_utf8(data).unwrap_or("???"))

View File

@ -0,0 +1,20 @@
use alloc::vec::Vec;
use uefi::{proto::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)
}