Skip to content

Commit 8ede408

Browse files
authored
Merge pull request #1227 from rust-osdev/bishop-allow-zero-arg-entry
uefi-macros: Allow zero-param function in the entry macro
2 parents cc7a14c + 6b149bb commit 8ede408

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

uefi-macros/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# uefi-macros - [Unreleased]
22

3+
## Changed
4+
5+
- The `entry` macro now accepts a function with zero arguments in addition to
6+
the two-argument form.
7+
38

49
# uefi-macros - 0.14.0 (2024-07-02)
510

uefi-macros/src/lib.rs

+35-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use proc_macro2::TokenStream as TokenStream2;
88
use quote::{quote, quote_spanned, TokenStreamExt};
99
use syn::spanned::Spanned;
1010
use syn::{
11-
parse_macro_input, parse_quote, Error, Expr, ExprLit, ExprPath, FnArg, Ident, ItemFn,
12-
ItemStruct, Lit, Pat, Visibility,
11+
parse_macro_input, parse_quote, parse_quote_spanned, Error, Expr, ExprLit, ExprPath, FnArg,
12+
Ident, ItemFn, ItemStruct, Lit, Pat, Visibility,
1313
};
1414

1515
macro_rules! err {
@@ -121,18 +121,34 @@ fn get_function_arg_name(f: &ItemFn, arg_index: usize, errors: &mut TokenStream2
121121
/// Custom attribute for a UEFI executable entry point.
122122
///
123123
/// This attribute modifies a function to mark it as the entry point for
124-
/// a UEFI executable. The function must have two parameters, [`Handle`]
125-
/// and [`SystemTable<Boot>`], and return a [`Status`]. The function can
126-
/// optionally be `unsafe`.
124+
/// a UEFI executable. The function:
125+
/// * Must return [`Status`].
126+
/// * Must have either zero parameters or two: [`Handle`] and [`SystemTable<Boot>`].
127+
/// * Can optionally be `unsafe`.
127128
///
128129
/// Due to internal implementation details the parameters must both be
129130
/// named, so `arg` or `_arg` are allowed, but not `_`.
130131
///
131-
/// The [`BootServices::set_image_handle`] function will be called
132-
/// automatically with the image [`Handle`] argument.
132+
/// The global system table pointer and global image handle will be set
133+
/// automatically.
133134
///
134135
/// # Examples
135136
///
137+
/// With no arguments:
138+
///
139+
/// ```no_run
140+
/// #![no_main]
141+
///
142+
/// use uefi::prelude::*;
143+
///
144+
/// #[entry]
145+
/// fn main() -> Status {
146+
/// Status::SUCCESS
147+
/// }
148+
/// ```
149+
///
150+
/// With two arguments:
151+
///
136152
/// ```no_run
137153
/// #![no_main]
138154
///
@@ -180,6 +196,18 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
180196
));
181197
}
182198

199+
let signature_span = f.sig.span();
200+
201+
// If the user doesn't specify any arguments to the entry function, fill in
202+
// the image handle and system table arguments automatically.
203+
if f.sig.inputs.is_empty() {
204+
f.sig.inputs = parse_quote_spanned!(
205+
signature_span=>
206+
image_handle: ::uefi::Handle,
207+
system_table: ::uefi::table::SystemTable<::uefi::table::Boot>
208+
);
209+
}
210+
183211
let image_handle_ident = get_function_arg_name(&f, 0, &mut errors);
184212
let system_table_ident = get_function_arg_name(&f, 1, &mut errors);
185213

@@ -188,8 +216,6 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
188216
return errors.into();
189217
}
190218

191-
let signature_span = f.sig.span();
192-
193219
f.sig.abi = Some(syn::parse2(quote_spanned! (signature_span=> extern "efiapi")).unwrap());
194220

195221
// allow the entry function to be unsafe (by moving the keyword around so that it actually works)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use uefi::{entry, Status};
2+
3+
#[entry]
4+
fn efi_main() -> Status {
5+
Status::SUCCESS
6+
}
7+
8+
// trybuild requires a `main` function.
9+
fn main() {}

0 commit comments

Comments
 (0)