Skip to content

Commit 45bb415

Browse files
uefi: Remove static references from SystemTable implementation
Static references have stronger guarantees than raw pointers, and since these fields are internal anyway let's avoid the risk of UB and keep them as raw pointers.
1 parent 936c4e1 commit 45bb415

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

uefi/src/table/system.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl SystemTableView for Runtime {}
4747
/// will be provided to replace it.
4848
#[repr(transparent)]
4949
pub struct SystemTable<View: SystemTableView> {
50-
table: &'static SystemTableImpl,
50+
table: *const SystemTableImpl,
5151
_marker: PhantomData<View>,
5252
}
5353

@@ -56,28 +56,28 @@ impl<View: SystemTableView> SystemTable<View> {
5656
/// Return the firmware vendor string
5757
#[must_use]
5858
pub fn firmware_vendor(&self) -> &CStr16 {
59-
unsafe { CStr16::from_ptr(self.table.fw_vendor) }
59+
unsafe { CStr16::from_ptr((*self.table).fw_vendor) }
6060
}
6161

6262
/// Return the firmware revision
6363
#[must_use]
6464
pub const fn firmware_revision(&self) -> u32 {
65-
self.table.fw_revision
65+
unsafe { (*self.table).fw_revision }
6666
}
6767

6868
/// Returns the revision of this table, which is defined to be
6969
/// the revision of the UEFI specification implemented by the firmware.
7070
#[must_use]
7171
pub const fn uefi_revision(&self) -> Revision {
72-
self.table.header.revision
72+
unsafe { (*self.table).header.revision }
7373
}
7474

7575
/// Returns the config table entries, a linear array of structures
7676
/// pointing to other system-specific tables.
7777
#[allow(clippy::missing_const_for_fn)] // Required until we bump the MSRV.
7878
#[must_use]
7979
pub fn config_table(&self) -> &[cfg::ConfigTableEntry] {
80-
unsafe { slice::from_raw_parts(self.table.cfg_table, self.table.nr_cfg) }
80+
unsafe { slice::from_raw_parts((*self.table).cfg_table, (*self.table).nr_cfg) }
8181
}
8282

8383
/// Creates a new `SystemTable<View>` from a raw address. The address might
@@ -112,29 +112,29 @@ impl<View: SystemTableView> SystemTable<View> {
112112
impl SystemTable<Boot> {
113113
/// Returns the standard input protocol.
114114
pub fn stdin(&mut self) -> &mut text::Input {
115-
unsafe { &mut *self.table.stdin }
115+
unsafe { &mut *(*self.table).stdin }
116116
}
117117

118118
/// Returns the standard output protocol.
119119
pub fn stdout(&mut self) -> &mut text::Output {
120-
unsafe { &mut *self.table.stdout.cast() }
120+
unsafe { &mut *(*self.table).stdout.cast() }
121121
}
122122

123123
/// Returns the standard error protocol.
124124
pub fn stderr(&mut self) -> &mut text::Output {
125-
unsafe { &mut *self.table.stderr.cast() }
125+
unsafe { &mut *(*self.table).stderr.cast() }
126126
}
127127

128128
/// Access runtime services
129129
#[must_use]
130130
pub const fn runtime_services(&self) -> &RuntimeServices {
131-
self.table.runtime
131+
unsafe { &*(*self.table).runtime }
132132
}
133133

134134
/// Access boot services
135135
#[must_use]
136136
pub const fn boot_services(&self) -> &BootServices {
137-
unsafe { &*self.table.boot }
137+
unsafe { &*(*self.table).boot }
138138
}
139139

140140
/// Get the size in bytes of the buffer to allocate for storing the memory
@@ -275,7 +275,7 @@ impl SystemTable<Boot> {
275275

276276
impl Debug for SystemTable<Boot> {
277277
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
278-
self.table.fmt(f)
278+
unsafe { &*self.table }.fmt(f)
279279
}
280280
}
281281

@@ -292,7 +292,7 @@ impl SystemTable<Runtime> {
292292
/// "Calling Conventions" chapter of the UEFI specification for details.
293293
#[must_use]
294294
pub const unsafe fn runtime_services(&self) -> &RuntimeServices {
295-
self.table.runtime
295+
&*(*self.table).runtime
296296
}
297297

298298
/// Changes the runtime addressing mode of EFI firmware from physical to virtual.
@@ -318,22 +318,27 @@ impl SystemTable<Runtime> {
318318
let entry_size = core::mem::size_of::<MemoryDescriptor>();
319319
let entry_version = crate::table::boot::MEMORY_DESCRIPTOR_VERSION;
320320
let map_ptr = map.as_mut_ptr();
321-
(self.table.runtime.set_virtual_address_map)(map_size, entry_size, entry_version, map_ptr)
322-
.into_with_val(|| {
323-
let new_table_ref =
324-
&mut *(new_system_table_virtual_addr as usize as *mut SystemTableImpl);
325-
Self {
326-
table: new_table_ref,
327-
_marker: PhantomData,
328-
}
329-
})
321+
((*(*self.table).runtime).set_virtual_address_map)(
322+
map_size,
323+
entry_size,
324+
entry_version,
325+
map_ptr,
326+
)
327+
.into_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+
})
330335
}
331336

332337
/// Return the address of the SystemTable that resides in a UEFI runtime services
333338
/// memory region.
334339
#[must_use]
335340
pub fn get_current_system_table_addr(&self) -> u64 {
336-
self.table as *const _ as usize as u64
341+
self.table as u64
337342
}
338343
}
339344

@@ -351,7 +356,7 @@ struct SystemTableImpl {
351356
stderr_handle: Handle,
352357
stderr: *mut text::Output,
353358
/// Runtime services table.
354-
runtime: &'static RuntimeServices,
359+
runtime: *const RuntimeServices,
355360
/// Boot services table.
356361
boot: *const BootServices,
357362
/// Number of entries in the configuration table.

0 commit comments

Comments
 (0)