Skip to content

boot: Add freestanding version of create_event #1280

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

Merged
merged 3 commits into from
Aug 6, 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
4 changes: 2 additions & 2 deletions uefi-test-runner/src/proto/media.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::string::ToString;
use core::cell::RefCell;
use core::ptr::NonNull;
use uefi::boot;
use uefi::data_types::Align;
use uefi::prelude::*;
use uefi::proto::media::block::BlockIO;
Expand Down Expand Up @@ -305,8 +306,7 @@ fn test_raw_disk_io2(handle: Handle, bt: &BootServices) {

unsafe {
// Create the completion event
let mut event = bt
.create_event(EventType::empty(), Tpl::NOTIFY, None, None)
let mut event = boot::create_event(EventType::empty(), Tpl::NOTIFY, None, None)
.expect("Failed to create disk I/O completion event");

// Initialise the task context
Expand Down
54 changes: 50 additions & 4 deletions uefi/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use crate::data_types::PhysicalAddress;
use crate::proto::device_path::DevicePath;
use crate::proto::{Protocol, ProtocolPointer};
use crate::util::opt_nonnull_to_ptr;
use core::ffi::c_void;
use core::ops::{Deref, DerefMut};
use core::ptr::{self, NonNull};
use core::slice;
use core::sync::atomic::{AtomicPtr, Ordering};
use uefi::{table, Char16, Handle, Result, Status, StatusExt};
use core::{mem, slice};
use uefi::{table, Char16, Event, Handle, Result, Status, StatusExt};

#[cfg(doc)]
use {
Expand All @@ -19,9 +20,10 @@ use {
};

pub use uefi::table::boot::{
AllocateType, LoadImageSource, OpenProtocolAttributes, OpenProtocolParams, SearchType,
AllocateType, EventNotifyFn, LoadImageSource, OpenProtocolAttributes, OpenProtocolParams,
SearchType,
};
pub use uefi_raw::table::boot::{MemoryType, Tpl};
pub use uefi_raw::table::boot::{EventType, MemoryType, Tpl};

/// Global image handle. This is only set by [`set_image_handle`], and it is
/// only read by [`image_handle`].
Expand Down Expand Up @@ -162,6 +164,50 @@ pub unsafe fn free_pool(ptr: NonNull<u8>) -> Result {
unsafe { (bt.free_pool)(ptr.as_ptr()) }.to_result()
}

/// Creates an event.
///
/// This function creates a new event of the specified type and returns it.
///
/// Events are created in a "waiting" state, and may switch to a "signaled"
/// state. If the event type has flag `NotifySignal` set, this will result in
/// a callback for the event being immediately enqueued at the `notify_tpl`
/// priority level. If the event type has flag `NotifyWait`, the notification
/// will be delivered next time `wait_for_event` or `check_event` is called.
/// In both cases, a `notify_fn` callback must be specified.
///
/// # Safety
///
/// This function is unsafe because callbacks must handle exit from boot
/// services correctly.
///
/// # Errors
///
/// * [`Status::INVALID_PARAMETER`]: an invalid combination of parameters was provided.
/// * [`Status::OUT_OF_RESOURCES`]: the event could not be allocated.
pub unsafe fn create_event(
event_ty: EventType,
notify_tpl: Tpl,
notify_fn: Option<EventNotifyFn>,
notify_ctx: Option<NonNull<c_void>>,
) -> Result<Event> {
let bt = boot_services_raw_panicking();
let bt = unsafe { bt.as_ref() };

let mut event = ptr::null_mut();

// Safety: the argument types of the function pointers are defined
// differently, but are compatible and can be safely transmuted.
let notify_fn: Option<uefi_raw::table::boot::EventNotifyFn> = mem::transmute(notify_fn);

let notify_ctx = opt_nonnull_to_ptr(notify_ctx);

// Now we're ready to call UEFI
(bt.create_event)(event_ty, notify_tpl, notify_fn, notify_ctx, &mut event).to_result_with_val(
// OK to unwrap: event is non-null for Status::SUCCESS.
|| Event::from_ptr(event).unwrap(),
)
}

/// Connect one or more drivers to a controller.
///
/// Usually one disconnects and then reconnects certain drivers
Expand Down
4 changes: 2 additions & 2 deletions uefi/src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,8 +1616,8 @@ impl<'guid> SearchType<'guid> {
}
}

/// Raw event notification function
type EventNotifyFn = unsafe extern "efiapi" fn(event: Event, context: Option<NonNull<c_void>>);
/// Event notification callback type.
pub type EventNotifyFn = unsafe extern "efiapi" fn(event: Event, context: Option<NonNull<c_void>>);

/// Timer events manipulation.
#[derive(Debug)]
Expand Down