Skip to content

Commit 39cf059

Browse files
uefi: Use RuntimeServices struct from uefi-raw
1 parent 71e2938 commit 39cf059

File tree

2 files changed

+44
-93
lines changed

2 files changed

+44
-93
lines changed

uefi/src/table/runtime.rs

Lines changed: 34 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! UEFI services available at runtime, even after the OS boots.
22
3-
use super::{Header, Revision};
3+
use super::Revision;
44
use crate::table::boot::MemoryDescriptor;
5-
use crate::{CStr16, Char16, Error, Guid, Result, Status, StatusExt};
6-
use core::ffi::c_void;
5+
use crate::{CStr16, Error, Guid, Result, Status, StatusExt};
76
use core::fmt::{Debug, Formatter};
87
use core::mem::MaybeUninit;
98
use core::{fmt, ptr};
@@ -32,76 +31,21 @@ use {
3231
///
3332
/// [`SystemTable::runtime_services`]: crate::table::SystemTable::runtime_services
3433
#[repr(C)]
35-
pub struct RuntimeServices {
36-
header: Header,
37-
get_time:
38-
unsafe extern "efiapi" fn(time: *mut Time, capabilities: *mut TimeCapabilities) -> Status,
39-
set_time: unsafe extern "efiapi" fn(time: &Time) -> Status,
40-
get_wakeup_time:
41-
unsafe extern "efiapi" fn(enabled: *mut u8, pending: *mut u8, time: *mut Time) -> Status,
42-
set_wakeup_time: unsafe extern "efiapi" fn(enable: u8, time: *const Time) -> Status,
43-
pub(crate) set_virtual_address_map: unsafe extern "efiapi" fn(
44-
map_size: usize,
45-
desc_size: usize,
46-
desc_version: u32,
47-
virtual_map: *mut MemoryDescriptor,
48-
) -> Status,
49-
convert_pointer:
50-
unsafe extern "efiapi" fn(debug_disposition: usize, address: *mut *const c_void) -> Status,
51-
get_variable: unsafe extern "efiapi" fn(
52-
variable_name: *const Char16,
53-
vendor_guid: *const Guid,
54-
attributes: *mut VariableAttributes,
55-
data_size: *mut usize,
56-
data: *mut u8,
57-
) -> Status,
58-
get_next_variable_name: unsafe extern "efiapi" fn(
59-
variable_name_size: *mut usize,
60-
variable_name: *mut u16,
61-
vendor_guid: *mut Guid,
62-
) -> Status,
63-
set_variable: unsafe extern "efiapi" fn(
64-
variable_name: *const Char16,
65-
vendor_guid: *const Guid,
66-
attributes: VariableAttributes,
67-
data_size: usize,
68-
data: *const u8,
69-
) -> Status,
70-
get_next_high_monotonic_count: unsafe extern "efiapi" fn(high_count: *mut u32) -> Status,
71-
reset: unsafe extern "efiapi" fn(
72-
rt: ResetType,
73-
74-
status: Status,
75-
data_size: usize,
76-
data: *const u8,
77-
) -> !,
78-
79-
// UEFI 2.0 Capsule Services.
80-
update_capsule: usize,
81-
query_capsule_capabilities: usize,
82-
83-
// Miscellaneous UEFI 2.0 Service.
84-
query_variable_info: unsafe extern "efiapi" fn(
85-
attributes: VariableAttributes,
86-
maximum_variable_storage_size: *mut u64,
87-
remaining_variable_storage_size: *mut u64,
88-
maximum_variable_size: *mut u64,
89-
) -> Status,
90-
}
34+
pub struct RuntimeServices(uefi_raw::table::runtime::RuntimeServices);
9135

9236
impl RuntimeServices {
9337
/// Query the current time and date information
9438
pub fn get_time(&self) -> Result<Time> {
9539
let mut time = MaybeUninit::<Time>::uninit();
96-
unsafe { (self.get_time)(time.as_mut_ptr(), ptr::null_mut()) }
40+
unsafe { (self.0.get_time)(time.as_mut_ptr().cast(), ptr::null_mut()) }
9741
.to_result_with_val(|| unsafe { time.assume_init() })
9842
}
9943

10044
/// Query the current time and date information and the RTC capabilities
10145
pub fn get_time_and_caps(&self) -> Result<(Time, TimeCapabilities)> {
10246
let mut time = MaybeUninit::<Time>::uninit();
10347
let mut caps = MaybeUninit::<TimeCapabilities>::uninit();
104-
unsafe { (self.get_time)(time.as_mut_ptr(), caps.as_mut_ptr()) }
48+
unsafe { (self.0.get_time)(time.as_mut_ptr().cast(), caps.as_mut_ptr()) }
10549
.to_result_with_val(|| unsafe { (time.assume_init(), caps.assume_init()) })
10650
}
10751

@@ -115,16 +59,17 @@ impl RuntimeServices {
11559
/// Undefined behavior could happen if multiple tasks try to
11660
/// use this function at the same time without synchronisation.
11761
pub unsafe fn set_time(&mut self, time: &Time) -> Result {
118-
(self.set_time)(time).to_result()
62+
let time: *const Time = time;
63+
(self.0.set_time)(time.cast()).to_result()
11964
}
12065

12166
/// Get the size (in bytes) of a variable. This can be used to find out how
12267
/// big of a buffer should be passed in to `get_variable`.
12368
pub fn get_variable_size(&self, name: &CStr16, vendor: &VariableVendor) -> Result<usize> {
12469
let mut data_size = 0;
12570
let status = unsafe {
126-
(self.get_variable)(
127-
name.as_ptr(),
71+
(self.0.get_variable)(
72+
name.as_ptr().cast(),
12873
&vendor.0,
12974
ptr::null_mut(),
13075
&mut data_size,
@@ -154,8 +99,8 @@ impl RuntimeServices {
15499
let mut attributes = VariableAttributes::empty();
155100
let mut data_size = buf.len();
156101
unsafe {
157-
(self.get_variable)(
158-
name.as_ptr(),
102+
(self.0.get_variable)(
103+
name.as_ptr().cast(),
159104
&vendor.0,
160105
&mut attributes,
161106
&mut data_size,
@@ -178,8 +123,8 @@ impl RuntimeServices {
178123
let mut data = Vec::with_capacity(data_size);
179124

180125
let status = unsafe {
181-
(self.get_variable)(
182-
name.as_ptr(),
126+
(self.0.get_variable)(
127+
name.as_ptr().cast(),
183128
&vendor.0,
184129
&mut attributes,
185130
&mut data_size,
@@ -212,7 +157,7 @@ impl RuntimeServices {
212157
loop {
213158
let mut name_size_in_bytes = name.len() * mem::size_of::<u16>();
214159
status = unsafe {
215-
(self.get_next_variable_name)(
160+
(self.0.get_next_variable_name)(
216161
&mut name_size_in_bytes,
217162
name.as_mut_ptr(),
218163
&mut vendor,
@@ -275,8 +220,8 @@ impl RuntimeServices {
275220
data: &[u8],
276221
) -> Result {
277222
unsafe {
278-
(self.set_variable)(
279-
name.as_ptr(),
223+
(self.0.set_variable)(
224+
name.as_ptr().cast(),
280225
&vendor.0,
281226
attributes,
282227
data.len(),
@@ -302,13 +247,13 @@ impl RuntimeServices {
302247
&self,
303248
attributes: VariableAttributes,
304249
) -> Result<VariableStorageInfo> {
305-
if self.header.revision < Revision::EFI_2_00 {
250+
if self.0.header.revision < Revision::EFI_2_00 {
306251
return Err(Status::UNSUPPORTED.into());
307252
}
308253

309254
let mut info = VariableStorageInfo::default();
310255
unsafe {
311-
(self.query_variable_info)(
256+
(self.0.query_variable_info)(
312257
attributes,
313258
&mut info.maximum_variable_storage_size,
314259
&mut info.remaining_variable_storage_size,
@@ -331,7 +276,17 @@ impl RuntimeServices {
331276
None => (0, ptr::null()),
332277
};
333278

334-
unsafe { (self.reset)(rt, status, size, data) }
279+
unsafe { (self.0.reset_system)(rt, status, size, data) }
280+
}
281+
282+
pub(crate) unsafe fn set_virtual_address_map(
283+
&self,
284+
map_size: usize,
285+
desc_size: usize,
286+
desc_version: u32,
287+
virtual_map: *mut MemoryDescriptor,
288+
) -> Status {
289+
(self.0.set_virtual_address_map)(map_size, desc_size, desc_version, virtual_map)
335290
}
336291
}
337292

@@ -342,14 +297,14 @@ impl super::Table for RuntimeServices {
342297
impl Debug for RuntimeServices {
343298
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
344299
f.debug_struct("RuntimeServices")
345-
.field("header", &self.header)
346-
.field("get_time", &(self.get_time as *const u64))
347-
.field("set_time", &(self.set_time as *const u64))
300+
.field("header", &self.0.header)
301+
.field("get_time", &(self.0.get_time as *const u64))
302+
.field("set_time", &(self.0.set_time as *const u64))
348303
.field(
349304
"set_virtual_address_map",
350-
&(self.set_virtual_address_map as *const u64),
305+
&(self.0.set_virtual_address_map as *const u64),
351306
)
352-
.field("reset", &(self.reset as *const u64))
307+
.field("reset", &(self.0.reset_system as *const u64))
353308
.finish()
354309
}
355310
}

uefi/src/table/system.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -318,20 +318,16 @@ impl SystemTable<Runtime> {
318318
let entry_size = core::mem::size_of::<MemoryDescriptor>();
319319
let entry_version = MemoryDescriptor::VERSION;
320320
let map_ptr = map.as_mut_ptr();
321-
((*(*self.table).runtime).set_virtual_address_map)(
322-
map_size,
323-
entry_size,
324-
entry_version,
325-
map_ptr,
326-
)
327-
.to_result_with_val(|| {
328-
let new_table_ref =
329-
&mut *(new_system_table_virtual_addr as usize as *mut SystemTableImpl);
330-
Self {
331-
table: new_table_ref,
332-
_marker: PhantomData,
333-
}
334-
})
321+
(*(*self.table).runtime)
322+
.set_virtual_address_map(map_size, entry_size, entry_version, map_ptr)
323+
.to_result_with_val(|| {
324+
let new_table_ref =
325+
&mut *(new_system_table_virtual_addr as usize as *mut SystemTableImpl);
326+
Self {
327+
table: new_table_ref,
328+
_marker: PhantomData,
329+
}
330+
})
335331
}
336332

337333
/// Return the address of the SystemTable that resides in a UEFI runtime services

0 commit comments

Comments
 (0)