Skip to content

Commit 58ee068

Browse files
uefi: Add set_main macro
1 parent 1fc0285 commit 58ee068

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

uefi/src/macros.rs

+33
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,36 @@ macro_rules! cstr16 {
7878
unsafe { $crate::CStr16::from_u16_with_nul_unchecked(S) }
7979
}};
8080
}
81+
82+
/// Set the entry point for a UEFI executable.
83+
///
84+
/// This macro takes one argument, the function to run when the executable is
85+
/// launched. That function must take no arguments and return a [`uefi::Result`].
86+
///
87+
/// # Example
88+
///
89+
/// ```
90+
/// uefi::set_main!(main);
91+
///
92+
/// fn main() -> uefi::Result {
93+
/// Ok(())
94+
/// }
95+
/// ```
96+
#[macro_export]
97+
macro_rules! set_main {
98+
($main:ident) => {
99+
#[export_name = "efi_main"]
100+
fn efi_main(
101+
image: ::uefi::Handle,
102+
system_table: *mut ::core::ffi::c_void,
103+
) -> ::uefi::Status {
104+
unsafe { ::uefi::boot::set_image_handle(image) };
105+
unsafe { ::uefi::system::set_system_table(system_table.cast()) };
106+
let result = $main();
107+
match result {
108+
Ok(()) => ::uefi::Status::SUCCESS,
109+
Err(err) => err.status(),
110+
}
111+
}
112+
};
113+
}

uefi/src/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! This includes the system table types, `Status` codes, etc.
44
5-
pub use crate::{cstr16, cstr8, entry, Handle, ResultExt, Status, StatusExt};
5+
pub use crate::{cstr16, cstr8, entry, set_main, Handle, ResultExt, Status, StatusExt};
66

77
// Import the basic table types.
88
pub use crate::table::boot::BootServices;

0 commit comments

Comments
 (0)