From 1ebea02bba1ad9a74ce86f419fb2ea10b104b9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Fri, 16 Feb 2024 18:03:33 +0100 Subject: [PATCH 1/2] fix(uefi): enable print macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/macros.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 3a7db0f1..7ee46533 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -3,12 +3,18 @@ /// 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. @@ -16,15 +22,21 @@ macro_rules! print { /// 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 From be56cea8019929947613d8727ecdf64978e89dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Fri, 16 Feb 2024 18:05:41 +0100 Subject: [PATCH 2/2] feat(uefi): read application from file system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- Cargo.toml | 2 +- src/main.rs | 5 ++++- src/uefi.rs | 26 +++++++++++++++++++++++++- xtask/src/ci/qemu.rs | 1 + 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 736f146f..1dcc46cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index deb35ab1..7cdec0ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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")] diff --git a/src/uefi.rs b/src/uefi.rs index 1742dcce..3daa60f3 100644 --- a/src/uefi.rs +++ b/src/uefi.rs @@ -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 { + 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) -> 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); diff --git a/xtask/src/ci/qemu.rs b/xtask/src/ci/qemu.rs index 96e0599a..4b87496c 100644 --- a/xtask/src/ci/qemu.rs +++ b/xtask/src/ci/qemu.rs @@ -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();