Skip to content

Commit

Permalink
uefi: Add set_main macro
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasbishop committed May 2, 2024
1 parent 256578a commit 9121ad1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions uefi/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,36 @@ macro_rules! cstr16 {
unsafe { $crate::CStr16::from_u16_with_nul_unchecked(S) }
}};
}

/// Set the entry point for a UEFI executable.
///
/// This macro takes one argument, the function to run when the executable is
/// launched. That function must take no arguments and return a [`uefi::Result`].
///
/// # Example
///
/// ```
/// uefi::set_main!(main);
///
/// fn main() -> uefi::Result {
/// Ok(())
/// }
/// ```
#[macro_export]
macro_rules! set_main {
($main:ident) => {
#[export_name = "efi_main"]
fn efi_main(
image: ::uefi::Handle,
system_table: *mut ::core::ffi::c_void,
) -> ::uefi::Status {
unsafe { ::uefi::boot::set_image_handle(image) };
unsafe { ::uefi::system::set_system_table(system_table.cast()) };
let result = $main();
match result {
Ok(()) => ::uefi::Status::SUCCESS,
Err(err) => err.status(),
}
}
};
}
2 changes: 1 addition & 1 deletion uefi/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! This includes the system table types, `Status` codes, etc.

pub use crate::{cstr16, cstr8, entry, Handle, ResultExt, Status, StatusExt};
pub use crate::{cstr16, cstr8, entry, set_main, Handle, ResultExt, Status, StatusExt};

// Import the basic table types.
pub use crate::table::boot::BootServices;
Expand Down

0 comments on commit 9121ad1

Please sign in to comment.