Skip to content

Instantly share code, notes, and snippets.

@agrif
Created July 7, 2020 00:03
Show Gist options
  • Save agrif/b8d3d8dbeb1f537e574aec268343f0f7 to your computer and use it in GitHub Desktop.
Save agrif/b8d3d8dbeb1f537e574aec268343f0f7 to your computer and use it in GitHub Desktop.
#![no_std]
#![no_main]
#![feature(abi_efiapi)]
use uefi::prelude::*;
use core::fmt::Write;
#[entry]
fn efi_main(image: Handle, st: SystemTable<Boot>) -> Status {
uefi_services::init(&st).expect_success("Failed to initialize services.");
let mut stdout = st.stdout();
stdout.reset(false).expect_success("Failed to reset stdout");
use uefi::proto::console::text::Color::*;
stdout.set_color(White, Cyan).expect_success("Failed to set colors");
stdout.clear().expect_success("Failed to clear");
writeln!(&mut stdout, "Hello, world!").expect("Failed to write");
let bs = st.boot_services();
use uefi::proto::loaded_image::LoadedImage;
let loaded: *const LoadedImage = bs.handle_protocol(image).expect_success("Failed to get loaded image.").get();
let mut tmpbuf = [0; 512];
let opt = unsafe {
loaded.as_ref().unwrap().load_options(&mut tmpbuf).expect("Failed to load options.")
};
writeln!(&mut stdout, "Options: {}", opt).expect("Failed to write");
let payloadpath = "\\EFI\\Boot\\payload.txt";
use uefi::proto::media::fs::SimpleFileSystem;
use uefi::table::boot::SearchType;
use uefi::proto::media::file::{FileMode, FileAttribute, FileType};
use uefi::proto::media::file::File;
let numfses = bs.locate_handle(SearchType::from_proto::<SimpleFileSystem>(), None).expect_success("Failed to get file handle.");
if numfses > 256 {
panic!("too many filesystems to inspect statically");
}
if numfses < 1 {
panic!("could not find EFI system partition");
}
let mut fses = [image; 256];
bs.locate_handle(SearchType::from_proto::<SimpleFileSystem>(), Some(&mut fses)).expect_success("Failed to get file handle.");
let fs: *mut SimpleFileSystem = bs.handle_protocol(fses[0]).expect_success("Failed to get EFI system partition").get();
let fs = unsafe { fs.as_mut().unwrap() };
let mut dir = fs.open_volume().expect_success("Failed to open EFI system directory");
writeln!(&mut stdout, "Opening: {}", payloadpath).expect("Failed to write");
let payload = dir.open(payloadpath, FileMode::Read, FileAttribute::empty()).expect_success("Failed to open payload");
if let FileType::Regular(mut f) = payload.into_type().expect_success("Failed to reify payload") {
let mut tmpbuf = [0; 512];
let size = f.read(&mut tmpbuf).expect_success("payload read failed");
let s = core::str::from_utf8(&tmpbuf[..size]).expect("payload not valid utf-8");
writeln!(&mut stdout, "Payload: {}", s).expect("Failed to write");
} else {
panic!("Payload was a directory??!");
}
loop {
bs.stall(1_000_000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment