Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(uefi): read application from file system #300

Merged
merged 2 commits into from
Feb 16, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exclusive_cell = "0.1"
spinning_top = "0.3"

[target.'cfg(target_os = "uefi")'.dependencies]
uefi = "0.26"
uefi = { version = "0.26", features = ["alloc"] }
uefi-services = { version = "0.23", features = ["qemu"] }
qemu-exit = "3"

Expand Down
28 changes: 20 additions & 8 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,40 @@
/// Adapted from [`std::print`].
///
/// [`std::print`]: https://doc.rust-lang.org/stable/std/macro.print.html
#[cfg(target_os = "none")]
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{
$crate::_print(::core::format_args!($($arg)*));
}};
($($arg:tt)*) => {
#[cfg(target_os = "none")]
{
$crate::_print(::core::format_args!($($arg)*));
}
#[cfg(target_os = "uefi")]
{
::uefi_services::print!($($arg)*);
}
};
}

/// Prints to the standard output, with a newline.
///
/// Adapted from [`std::println`].
///
/// [`std::println`]: https://doc.rust-lang.org/stable/std/macro.println.html
#[cfg(target_os = "none")]
#[macro_export]
macro_rules! println {
() => {
$crate::print!("\n")
};
($($arg:tt)*) => {{
$crate::_print(::core::format_args!("{}\n", format_args!($($arg)*)));
}};
($($arg:tt)*) => {
#[cfg(target_os = "none")]
{
$crate::_print(::core::format_args!("{}\n", format_args!($($arg)*)));
}
#[cfg(target_os = "uefi")]
{
::uefi_services::println!($($arg)*);
}
};
}

/// Prints and returns the value of a given expression for quick and dirty
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ mod none;
#[cfg(target_os = "uefi")]
mod uefi;

#[cfg(all(target_arch = "x86_64", target_os = "none", not(feature = "fc")))]
#[cfg(any(
target_os = "uefi",
all(target_arch = "x86_64", target_os = "none", not(feature = "fc"))
))]
extern crate alloc;

#[cfg(target_os = "none")]
Expand Down
26 changes: 25 additions & 1 deletion src/uefi.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
use alloc::string::String;
use alloc::vec::Vec;

use qemu_exit::QEMUExit;
use uefi::fs::{FileSystem, Path};
use uefi::prelude::*;

fn read_app(bt: &BootServices) -> Vec<u8> {
let fs = bt
.get_image_file_system(bt.image_handle())
.expect("should open file system");

let path = Path::new(cstr16!(r"\efi\boot\hermit-app"));

let data = FileSystem::new(fs)
.read(path)
.expect("should read file content");

let len = data.len();
log::info!("Read Hermit application from \"{path}\" (size = {len} B)");

data
}

// Entry Point of the Uefi Loader
#[entry]
fn loader_main(_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
uefi_services::init(&mut system_table).unwrap();

log::info!("Hello, UEFI!");
let app = read_app(system_table.boot_services());

let string = String::from_utf8(app).unwrap();
println!("{string}");

let custom_exit_success = 3;
let qemu_exit_handle = qemu_exit::X86::new(0xf4, custom_exit_success);
Expand Down
1 change: 1 addition & 0 deletions xtask/src/ci/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl Qemu {

sh.create_dir("target/esp/efi/boot")?;
sh.copy_file(self.build.dist_object(), "target/esp/efi/boot/bootx64.efi")?;
sh.write_file("target/esp/efi/boot/hermit-app", "Hello, UEFI!\n")?;
}

let target = self.build.target();
Expand Down