Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

uefi-macros: Allow zero-param function in the entry macro #1227

Merged
merged 1 commit into from
Jul 7, 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
5 changes: 5 additions & 0 deletions uefi-macros/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# uefi-macros - [Unreleased]

## Changed

- The `entry` macro now accepts a function with zero arguments in addition to
the two-argument form.


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

Expand Down
44 changes: 35 additions & 9 deletions uefi-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned, TokenStreamExt};
use syn::spanned::Spanned;
use syn::{
parse_macro_input, parse_quote, Error, Expr, ExprLit, ExprPath, FnArg, Ident, ItemFn,
ItemStruct, Lit, Pat, Visibility,
parse_macro_input, parse_quote, parse_quote_spanned, Error, Expr, ExprLit, ExprPath, FnArg,
Ident, ItemFn, ItemStruct, Lit, Pat, Visibility,
};

macro_rules! err {
Expand Down Expand Up @@ -121,18 +121,34 @@ fn get_function_arg_name(f: &ItemFn, arg_index: usize, errors: &mut TokenStream2
/// Custom attribute for a UEFI executable entry point.
///
/// This attribute modifies a function to mark it as the entry point for
/// a UEFI executable. The function must have two parameters, [`Handle`]
/// and [`SystemTable<Boot>`], and return a [`Status`]. The function can
/// optionally be `unsafe`.
/// a UEFI executable. The function:
/// * Must return [`Status`].
/// * Must have either zero parameters or two: [`Handle`] and [`SystemTable<Boot>`].
/// * Can optionally be `unsafe`.
///
/// Due to internal implementation details the parameters must both be
/// named, so `arg` or `_arg` are allowed, but not `_`.
///
/// The [`BootServices::set_image_handle`] function will be called
/// automatically with the image [`Handle`] argument.
/// The global system table pointer and global image handle will be set
/// automatically.
///
/// # Examples
///
/// With no arguments:
///
/// ```no_run
/// #![no_main]
///
/// use uefi::prelude::*;
///
/// #[entry]
/// fn main() -> Status {
/// Status::SUCCESS
/// }
/// ```
///
/// With two arguments:
///
/// ```no_run
/// #![no_main]
///
Expand Down Expand Up @@ -180,6 +196,18 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
));
}

let signature_span = f.sig.span();

// If the user doesn't specify any arguments to the entry function, fill in
// the image handle and system table arguments automatically.
if f.sig.inputs.is_empty() {
f.sig.inputs = parse_quote_spanned!(
signature_span=>
image_handle: ::uefi::Handle,
system_table: ::uefi::table::SystemTable<::uefi::table::Boot>
);
}

let image_handle_ident = get_function_arg_name(&f, 0, &mut errors);
let system_table_ident = get_function_arg_name(&f, 1, &mut errors);

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

let signature_span = f.sig.span();

f.sig.abi = Some(syn::parse2(quote_spanned! (signature_span=> extern "efiapi")).unwrap());

// allow the entry function to be unsafe (by moving the keyword around so that it actually works)
Expand Down
9 changes: 9 additions & 0 deletions uefi-macros/tests/ui/pass/entry_no_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use uefi::{entry, Status};

#[entry]
fn efi_main() -> Status {
Status::SUCCESS
}

// trybuild requires a `main` function.
fn main() {}