Skip to content

Commit 0843768

Browse files
wip
1 parent cb53880 commit 0843768

File tree

2 files changed

+70
-44
lines changed

2 files changed

+70
-44
lines changed

uefi/src/boot.rs

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::table::boot::{
1212
AllocateType, BootServices, EventNotifyFn, EventType, LoadImageSource, MemoryMap, MemoryType,
1313
OpenProtocolAttributes, OpenProtocolParams, SearchType, TimerTrigger, Tpl, IMAGE_HANDLE,
1414
};
15-
use crate::{system, Char16, Error, Event, Guid, Handle, Result, Status, StatusExt};
15+
use crate::table::{Boot, SystemTable};
16+
use crate::{table, Char16, Error, Event, Guid, Handle, Result, Status, StatusExt};
1617
use core::ffi::c_void;
1718
use core::mem::MaybeUninit;
1819
use core::ops::{Deref, DerefMut};
@@ -21,14 +22,21 @@ use core::slice;
2122
use core::sync::atomic::Ordering;
2223

2324
// TODO
25+
fn boot_services() -> NonNull<BootServices> {
26+
let st = table::system_table_boot().expect("boot services are not active");
27+
let ptr: *const _ = st.boot_services();
28+
NonNull::new(ptr.cast_mut()).unwrap()
29+
}
30+
2431
fn boot_services_raw() -> NonNull<uefi_raw::table::boot::BootServices> {
25-
let st = unsafe { system::system_table().as_mut() };
26-
NonNull::new(st.boot_services).expect("boot services are not active")
32+
// OK to cast: `BootServices` is a `repr(transparent)` wrapper around
33+
// the raw type.
34+
boot_services().cast()
2735
}
2836

29-
fn boot_services() -> NonNull<BootServices> {
30-
let st = unsafe { system::system_table().as_mut() };
31-
NonNull::new(st.boot_services.cast()).expect("boot services are not active")
37+
#[track_caller]
38+
fn stboot() -> SystemTable<Boot> {
39+
table::system_table_boot().expect("boot services are not available")
3240
}
3341

3442
/// Get the [`Handle`] of the currently-executing image.
@@ -74,7 +82,7 @@ pub fn allocate_pages(
7482
mem_ty: MemoryType,
7583
count: usize,
7684
) -> Result<PhysicalAddress> {
77-
unsafe { boot_services().as_mut() }.allocate_pages(ty, mem_ty, count)
85+
stboot().boot_services().allocate_pages(ty, mem_ty, count)
7886
}
7987

8088
/// Frees memory pages allocated by UEFI.
@@ -86,7 +94,7 @@ pub fn allocate_pages(
8694
/// * [`uefi::Status::NOT_FOUND`]
8795
/// * [`uefi::Status::INVALID_PARAMETER`]
8896
pub unsafe fn free_pages(addr: PhysicalAddress, count: usize) -> Result {
89-
unsafe { boot_services().as_mut() }.free_pages(addr, count)
97+
stboot().boot_services().free_pages(addr, count)
9098
}
9199

92100
/// Raises a task's priority level and returns its previous level.
@@ -130,7 +138,7 @@ pub unsafe fn raise_tpl(tpl: Tpl) -> TplGuard {
130138
/// * [`uefi::Status::BUFFER_TOO_SMALL`]
131139
/// * [`uefi::Status::INVALID_PARAMETER`]
132140
pub fn memory_map(mt: MemoryType) -> Result<MemoryMap> {
133-
unsafe { boot_services().as_mut() }.memory_map(mt)
141+
stboot().boot_services().memory_map(mt)
134142
}
135143

136144
/// Allocates from a memory pool. The pointer will be 8-byte aligned.
@@ -142,7 +150,7 @@ pub fn memory_map(mt: MemoryType) -> Result<MemoryMap> {
142150
/// * [`uefi::Status::OUT_OF_RESOURCES`]
143151
/// * [`uefi::Status::INVALID_PARAMETER`]
144152
pub fn allocate_pool(mem_ty: MemoryType, size: usize) -> Result<NonNull<u8>> {
145-
unsafe { boot_services().as_mut() }.allocate_pool(mem_ty, size)
153+
stboot().boot_services().allocate_pool(mem_ty, size)
146154
}
147155

148156
/// Frees memory allocated from a pool.
@@ -154,7 +162,7 @@ pub fn allocate_pool(mem_ty: MemoryType, size: usize) -> Result<NonNull<u8>> {
154162
/// * [`uefi::Status::INVALID_PARAMETER`]
155163
#[allow(clippy::not_unsafe_ptr_arg_deref)]
156164
pub unsafe fn free_pool(addr: *mut u8) -> Result {
157-
unsafe { boot_services().as_mut() }.free_pool(addr)
165+
stboot().boot_services().free_pool(addr)
158166
}
159167

160168
/// Creates an event
@@ -185,7 +193,9 @@ pub unsafe fn create_event(
185193
notify_fn: Option<EventNotifyFn>,
186194
notify_ctx: Option<NonNull<c_void>>,
187195
) -> Result<Event> {
188-
unsafe { boot_services().as_mut() }.create_event(event_ty, notify_tpl, notify_fn, notify_ctx)
196+
stboot()
197+
.boot_services()
198+
.create_event(event_ty, notify_tpl, notify_fn, notify_ctx)
189199
}
190200

191201
/// Creates a new `Event` of type `event_type`. The event's notification function, context,
@@ -234,7 +244,7 @@ pub unsafe fn create_event_ex(
234244
notify_ctx: Option<NonNull<c_void>>,
235245
event_group: Option<NonNull<Guid>>,
236246
) -> Result<Event> {
237-
unsafe { boot_services().as_mut() }.create_event_ex(
247+
stboot().boot_services().create_event_ex(
238248
event_type,
239249
notify_tpl,
240250
notify_fn,
@@ -251,7 +261,7 @@ pub unsafe fn create_event_ex(
251261
///
252262
/// * [`uefi::Status::INVALID_PARAMETER`]
253263
pub fn set_timer(event: &Event, trigger_time: TimerTrigger) -> Result {
254-
unsafe { boot_services().as_mut() }.set_timer(event, trigger_time)
264+
stboot().boot_services().set_timer(event, trigger_time)
255265
}
256266

257267
/// Stops execution until an event is signaled.
@@ -289,7 +299,7 @@ pub fn set_timer(event: &Event, trigger_time: TimerTrigger) -> Result {
289299
/// * [`uefi::Status::INVALID_PARAMETER`]
290300
/// * [`uefi::Status::UNSUPPORTED`]
291301
pub fn wait_for_event(events: &mut [Event]) -> Result<usize, Option<usize>> {
292-
unsafe { boot_services().as_mut() }.wait_for_event(events)
302+
stboot().boot_services().wait_for_event(events)
293303
}
294304

295305
/// Place 'event' in the signaled stated. If 'event' is already in the signaled state,
@@ -311,7 +321,7 @@ pub fn wait_for_event(events: &mut [Event]) -> Result<usize, Option<usize>> {
311321
///
312322
/// Currently, (as of UEFI Spec v2.9) this only returns `EFI_SUCCESS`.
313323
pub fn signal_event(event: &Event) -> Result {
314-
unsafe { boot_services().as_mut() }.signal_event(event)
324+
stboot().boot_services().signal_event(event)
315325
}
316326

317327
/// Removes `event` from any event group to which it belongs and closes it. If `event` was
@@ -328,7 +338,7 @@ pub fn signal_event(event: &Event) -> Result {
328338
///
329339
/// * [`uefi::Status::INVALID_PARAMETER`]
330340
pub fn close_event(event: Event) -> Result {
331-
unsafe { boot_services().as_mut() }.close_event(event)
341+
stboot().boot_services().close_event(event)
332342
}
333343

334344
/// Checks to see if an event is signaled, without blocking execution to wait for it.
@@ -345,7 +355,7 @@ pub fn close_event(event: Event) -> Result {
345355
///
346356
/// * [`uefi::Status::INVALID_PARAMETER`]
347357
pub fn check_event(event: Event) -> Result<bool> {
348-
unsafe { boot_services().as_mut() }.check_event(event)
358+
stboot().boot_services().check_event(event)
349359
}
350360

351361
/// Installs a protocol interface on a device handle. If the inner `Option` in `handle` is `None`,
@@ -370,7 +380,9 @@ pub unsafe fn install_protocol_interface(
370380
protocol: &Guid,
371381
interface: *mut c_void,
372382
) -> Result<Handle> {
373-
unsafe { boot_services().as_mut() }.install_protocol_interface(handle, protocol, interface)
383+
stboot()
384+
.boot_services()
385+
.install_protocol_interface(handle, protocol, interface)
374386
}
375387

376388
/// Reinstalls a protocol interface on a device handle. `old_interface` is replaced with `new_interface`.
@@ -398,7 +410,7 @@ pub unsafe fn reinstall_protocol_interface(
398410
old_interface: *mut c_void,
399411
new_interface: *mut c_void,
400412
) -> Result<()> {
401-
unsafe { boot_services().as_mut() }.reinstall_protocol_interface(
413+
stboot().boot_services().reinstall_protocol_interface(
402414
handle,
403415
protocol,
404416
old_interface,
@@ -430,7 +442,9 @@ pub unsafe fn uninstall_protocol_interface(
430442
protocol: &Guid,
431443
interface: *mut c_void,
432444
) -> Result<()> {
433-
unsafe { boot_services().as_mut() }.uninstall_protocol_interface(handle, protocol, interface)
445+
stboot()
446+
.boot_services()
447+
.uninstall_protocol_interface(handle, protocol, interface)
434448
}
435449

436450
/// Registers `event` to be signalled whenever a protocol interface is registered for
@@ -450,7 +464,9 @@ pub unsafe fn uninstall_protocol_interface(
450464
/// * [`uefi::Status::OUT_OF_RESOURCES`]
451465
/// * [`uefi::Status::INVALID_PARAMETER`]
452466
pub fn register_protocol_notify(protocol: &Guid, event: Event) -> Result<(Event, SearchType)> {
453-
unsafe { boot_services().as_mut() }.register_protocol_notify(protocol, event)
467+
stboot()
468+
.boot_services()
469+
.register_protocol_notify(protocol, event)
454470
}
455471

456472
/// Enumerates all handles installed on the system which match a certain query.
@@ -471,7 +487,7 @@ pub fn locate_handle(
471487
search_ty: SearchType,
472488
output: Option<&mut [MaybeUninit<Handle>]>,
473489
) -> Result<usize> {
474-
unsafe { boot_services().as_mut() }.locate_handle(search_ty, output)
490+
stboot().boot_services().locate_handle(search_ty, output)
475491
}
476492

477493
/// Locates the handle to a device on the device path that supports the specified protocol.
@@ -494,7 +510,9 @@ pub fn locate_handle(
494510
pub fn locate_device_path<P: ProtocolPointer + ?Sized>(
495511
device_path: &mut &DevicePath,
496512
) -> Result<Handle> {
497-
unsafe { boot_services().as_mut() }.locate_device_path::<P>(device_path)
513+
stboot()
514+
.boot_services()
515+
.locate_device_path::<P>(device_path)
498516
}
499517

500518
/// Find an arbitrary handle that supports a particular
@@ -533,7 +551,7 @@ pub fn locate_device_path<P: ProtocolPointer + ?Sized>(
533551
///
534552
/// Returns [`NOT_FOUND`] if no handles support the requested protocol.
535553
pub fn get_handle_for_protocol<P: ProtocolPointer + ?Sized>() -> Result<Handle> {
536-
unsafe { boot_services().as_mut() }.get_handle_for_protocol::<P>()
554+
stboot().boot_services().get_handle_for_protocol::<P>()
537555
}
538556

539557
/// Load an EFI image into memory and return a [`Handle`] to the image.
@@ -569,7 +587,9 @@ pub fn get_handle_for_protocol<P: ProtocolPointer + ?Sized>() -> Result<Handle>
569587
/// * [`uefi::Status::ACCESS_DENIED`]
570588
/// * [`uefi::Status::SECURITY_VIOLATION`]
571589
pub fn load_image(parent_image_handle: Handle, source: LoadImageSource) -> uefi::Result<Handle> {
572-
unsafe { boot_services().as_mut() }.load_image(parent_image_handle, source)
590+
stboot()
591+
.boot_services()
592+
.load_image(parent_image_handle, source)
573593
}
574594

575595
/// Unload an EFI image.
@@ -586,7 +606,7 @@ pub fn load_image(parent_image_handle: Handle, source: LoadImageSource) -> uefi:
586606
/// * [`uefi::Status::UNSUPPORTED`]
587607
/// * [`uefi::Status::INVALID_PARAMETER`]
588608
pub fn unload_image(image_handle: Handle) -> Result {
589-
unsafe { boot_services().as_mut() }.unload_image(image_handle)
609+
stboot().boot_services().unload_image(image_handle)
590610
}
591611

592612
/// Transfer control to a loaded image's entry point.
@@ -602,7 +622,7 @@ pub fn unload_image(image_handle: Handle) -> Result {
602622
///
603623
/// * [`uefi::Status::UNSUPPORTED`]
604624
pub fn start_image(image_handle: Handle) -> Result {
605-
unsafe { boot_services().as_mut() }.start_image(image_handle)
625+
stboot().boot_services().start_image(image_handle)
606626
}
607627

608628
/// Exits the UEFI application and returns control to the UEFI component
@@ -620,12 +640,9 @@ pub unsafe fn exit(
620640
exit_data_size: usize,
621641
exit_data: *mut Char16,
622642
) -> ! {
623-
unsafe { boot_services().as_mut() }.exit(
624-
image_handle,
625-
exit_status,
626-
exit_data_size,
627-
exit_data.cast(),
628-
)
643+
stboot()
644+
.boot_services()
645+
.exit(image_handle, exit_status, exit_data_size, exit_data.cast())
629646
}
630647

631648
/// Exit the UEFI boot services.
@@ -657,7 +674,8 @@ pub unsafe fn exit(
657674
/// caller, the system will be reset.
658675
#[must_use]
659676
pub unsafe fn exit_boot_services(memory_type: MemoryType) -> MemoryMap {
660-
system::system_table_boot()
677+
table::system_table_boot()
678+
.expect("boot services are not active")
661679
.exit_boot_services(memory_type)
662680
.1
663681
}
@@ -666,7 +684,7 @@ pub unsafe fn exit_boot_services(memory_type: MemoryType) -> MemoryMap {
666684
///
667685
/// The time is in microseconds.
668686
pub fn stall(time: usize) {
669-
unsafe { boot_services().as_mut() }.stall(time)
687+
stboot().boot_services().stall(time)
670688
}
671689

672690
/// Adds, updates, or removes a configuration table entry
@@ -690,7 +708,9 @@ pub fn stall(time: usize) {
690708
/// * [`uefi::Status::NOT_FOUND`]
691709
/// * [`uefi::Status::OUT_OF_RESOURCES`]
692710
pub unsafe fn install_configuration_table(guid_entry: &Guid, table_ptr: *const c_void) -> Result {
693-
unsafe { boot_services().as_mut() }.install_configuration_table(guid_entry, table_ptr)
711+
stboot()
712+
.boot_services()
713+
.install_configuration_table(guid_entry, table_ptr)
694714
}
695715

696716
/// Set the watchdog timer.
@@ -720,7 +740,9 @@ pub unsafe fn install_configuration_table(guid_entry: &Guid, table_ptr: *const c
720740
/// * [`uefi::Status::UNSUPPORTED`]
721741
/// * [`uefi::Status::DEVICE_ERROR`]
722742
pub fn set_watchdog_timer(timeout: usize, watchdog_code: u64, data: Option<&mut [u16]>) -> Result {
723-
unsafe { boot_services().as_mut() }.set_watchdog_timer(timeout, watchdog_code, data)
743+
stboot()
744+
.boot_services()
745+
.set_watchdog_timer(timeout, watchdog_code, data)
724746
}
725747

726748
/// Connect one or more drivers to a controller.
@@ -742,7 +764,7 @@ pub fn connect_controller(
742764
remaining_device_path: Option<&DevicePath>,
743765
recursive: bool,
744766
) -> Result {
745-
unsafe { boot_services().as_mut() }.connect_controller(
767+
stboot().boot_services().connect_controller(
746768
controller,
747769
driver_image,
748770
remaining_device_path,
@@ -766,7 +788,9 @@ pub fn disconnect_controller(
766788
driver_image: Option<Handle>,
767789
child: Option<Handle>,
768790
) -> Result {
769-
unsafe { boot_services().as_mut() }.disconnect_controller(controller, driver_image, child)
791+
stboot()
792+
.boot_services()
793+
.disconnect_controller(controller, driver_image, child)
770794
}
771795

772796
/// Open a protocol interface for a handle.
@@ -880,7 +904,7 @@ pub fn open_protocol_exclusive<P: ProtocolPointer + ?Sized>(
880904
/// * [`uefi::Status::ACCESS_DENIED`]
881905
/// * [`uefi::Status::ALREADY_STARTED`]
882906
pub fn test_protocol<P: ProtocolPointer + ?Sized>(params: OpenProtocolParams) -> Result<()> {
883-
unsafe { boot_services().as_mut() }.test_protocol::<P>(params)
907+
stboot().boot_services().test_protocol::<P>(params)
884908
}
885909

886910
/// Get the list of protocol interface [`Guids`][Guid] that are installed

uefi/src/runtime.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ use crate::table::runtime::{
88
ResetType, RuntimeServices, Time, TimeCapabilities, VariableAttributes, VariableStorageInfo,
99
VariableVendor,
1010
};
11-
use crate::{system, CStr16, Result, Status};
11+
use crate::{table, CStr16, Result, Status};
1212
use core::ptr::NonNull;
1313

1414
#[cfg(feature = "alloc")]
1515
use {crate::table::runtime::VariableKey, alloc::boxed::Box, alloc::vec::Vec};
1616

1717
fn runtime_services() -> NonNull<RuntimeServices> {
18-
let st = unsafe { system::system_table().as_mut() };
19-
NonNull::new(st.runtime_services.cast()).expect("runtime services are not active")
18+
// TODO
19+
let st = table::system_table_runtime().expect("runtime services are not available");
20+
let ptr: *const _ = unsafe { st.runtime_services() };
21+
NonNull::new(ptr.cast_mut()).unwrap()
2022
}
2123

2224
/// Query the current time and date information

0 commit comments

Comments
 (0)