Skip to content

Commit ec5557b

Browse files
wip: Add uefi::system module
1 parent 37f7c01 commit ec5557b

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

uefi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub use uguid::guid;
114114
mod result;
115115
pub use result::{Error, Result, ResultExt, Status, StatusExt};
116116

117+
pub mod system;
117118
pub mod table;
118119

119120
pub mod proto;

uefi/src/system.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//! TODO
2+
3+
// TODO
4+
#![allow(clippy::missing_safety_doc)]
5+
6+
use crate::proto::console::text;
7+
use crate::table::{cfg, Boot, Revision, SystemTable};
8+
use crate::CStr16;
9+
use core::ptr::{self, NonNull};
10+
use core::slice;
11+
use core::sync::atomic::{AtomicPtr, Ordering};
12+
13+
static SYSTEM_TABLE: AtomicPtr<uefi_raw::table::system::SystemTable> =
14+
AtomicPtr::new(ptr::null_mut());
15+
16+
/// TODO
17+
pub unsafe fn set_system_table(system_table: *mut uefi_raw::table::system::SystemTable) {
18+
SYSTEM_TABLE.store(system_table, Ordering::Release);
19+
}
20+
21+
/// TODO
22+
pub(crate) fn system_table_maybe_null() -> *mut uefi_raw::table::system::SystemTable {
23+
SYSTEM_TABLE.load(Ordering::Acquire)
24+
}
25+
26+
/// TODO
27+
pub(crate) fn system_table() -> NonNull<uefi_raw::table::system::SystemTable> {
28+
let st = system_table_maybe_null();
29+
NonNull::new(st).expect("set_system_table has not been called")
30+
}
31+
32+
/// TODO
33+
#[must_use]
34+
pub fn system_table_boot() -> SystemTable<Boot> {
35+
unsafe { SystemTable::<Boot>::from_ptr(system_table().as_ptr().cast()) }.unwrap()
36+
}
37+
38+
// TODO: is static lifetime OK for these returned references?
39+
40+
/// Return the firmware vendor string
41+
#[must_use]
42+
pub fn firmware_vendor() -> &'static CStr16 {
43+
unsafe { CStr16::from_ptr(system_table().as_mut().firmware_vendor.cast()) }
44+
}
45+
46+
/// Return the firmware revision
47+
#[must_use]
48+
pub fn firmware_revision() -> u32 {
49+
unsafe { system_table().as_mut().firmware_revision }
50+
}
51+
52+
/// Returns the revision of this table, which is defined to be
53+
/// the revision of the UEFI specification implemented by the firmware.
54+
#[must_use]
55+
pub fn uefi_revision() -> Revision {
56+
unsafe { system_table().as_mut().header.revision }
57+
}
58+
59+
/// TODO
60+
pub fn with_config_table<F, R>(f: F) -> R
61+
where
62+
F: Fn(&[cfg::ConfigTableEntry]) -> R,
63+
{
64+
let st = unsafe { system_table().as_mut() };
65+
66+
let ptr: *const cfg::ConfigTableEntry = st.configuration_table.cast();
67+
let len = st.number_of_configuration_table_entries;
68+
let slice = if ptr.is_null() {
69+
&[]
70+
} else {
71+
unsafe { slice::from_raw_parts(ptr, len) }
72+
};
73+
f(slice)
74+
}
75+
76+
/// TODO
77+
pub fn with_stdout<F, R>(f: F) -> R
78+
where
79+
F: Fn(Option<&mut text::Output>) -> R,
80+
{
81+
let st = system_table_maybe_null();
82+
if st.is_null() {
83+
f(None)
84+
} else {
85+
let st = unsafe { &*st };
86+
let output_ptr: *mut text::Output = st.stdout.cast();
87+
if output_ptr.is_null() {
88+
f(None)
89+
} else {
90+
let output = unsafe { &mut *output_ptr };
91+
f(Some(output))
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)