-
Notifications
You must be signed in to change notification settings - Fork 160
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
Add uefi::system module #1237
Merged
phip1611
merged 4 commits into
rust-osdev:main
from
nicholasbishop:bishop-system-freestanding
Jul 15, 2024
Merged
Add uefi::system module #1237
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5cfc5f0
uefi: Add table::system_table_raw_panicking
nicholasbishop 7758fe4
Add uefi::system module
nicholasbishop fc637c8
uefi: Add standard derives for ConfigTableEntry
nicholasbishop 46e47c7
test-runner: Test the uefi::system module
nicholasbishop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
//! Functions for accessing fields of the system table. | ||
//! | ||
//! Some of these functions use a callback argument rather than returning a | ||
//! reference to the field directly. This pattern is used because some fields | ||
//! are allowed to change, and so a static lifetime cannot be used. | ||
//! | ||
//! Some functions can only be called while boot services are active, and will | ||
//! panic otherwise. See each function's documentation for details. | ||
|
||
use crate::proto::console::text::{Input, Output}; | ||
use crate::table::cfg::ConfigTableEntry; | ||
use crate::table::{self, Revision}; | ||
use crate::{CStr16, Char16}; | ||
use core::slice; | ||
|
||
/// Get the firmware vendor string. | ||
#[must_use] | ||
pub fn firmware_vendor() -> &'static CStr16 { | ||
let st = table::system_table_raw_panicking(); | ||
// SAFETY: valid per requirements of `set_system_table`. | ||
let st = unsafe { st.as_ref() }; | ||
|
||
let vendor: *const Char16 = st.firmware_vendor.cast(); | ||
|
||
// SAFETY: this assumes that the firmware vendor string is never mutated or freed. | ||
unsafe { CStr16::from_ptr(vendor) } | ||
} | ||
|
||
/// Get the firmware revision. | ||
#[must_use] | ||
pub fn firmware_revision() -> u32 { | ||
let st = table::system_table_raw_panicking(); | ||
// SAFETY: valid per requirements of `set_system_table`. | ||
let st = unsafe { st.as_ref() }; | ||
|
||
st.firmware_revision | ||
} | ||
|
||
/// Get the revision of the system table, which is defined to be the revision of | ||
/// the UEFI specification implemented by the firmware. | ||
#[must_use] | ||
pub fn uefi_revision() -> Revision { | ||
let st = table::system_table_raw_panicking(); | ||
// SAFETY: valid per requirements of `set_system_table`. | ||
let st = unsafe { st.as_ref() }; | ||
|
||
st.header.revision | ||
} | ||
|
||
/// Call `f` with a slice of [`ConfigTableEntry`]. Each entry provides access to | ||
/// a vendor-specific table. | ||
pub fn with_config_table<F, R>(f: F) -> R | ||
where | ||
F: Fn(&[ConfigTableEntry]) -> R, | ||
{ | ||
let st = table::system_table_raw_panicking(); | ||
// SAFETY: valid per requirements of `set_system_table`. | ||
let st = unsafe { st.as_ref() }; | ||
|
||
let ptr: *const ConfigTableEntry = st.configuration_table.cast(); | ||
let len = st.number_of_configuration_table_entries; | ||
let slice = if ptr.is_null() { | ||
&[] | ||
} else { | ||
unsafe { slice::from_raw_parts(ptr, len) } | ||
}; | ||
f(slice) | ||
} | ||
|
||
/// Call `f` with the [`Input`] protocol attached to stdin. | ||
/// | ||
/// # Panics | ||
/// | ||
/// This function will panic if called after exiting boot services, or if stdin | ||
/// is not available. | ||
pub fn with_stdin<F, R>(f: F) -> R | ||
where | ||
F: Fn(&mut Input) -> R, | ||
{ | ||
let st = table::system_table_raw_panicking(); | ||
// SAFETY: valid per requirements of `set_system_table`. | ||
let st = unsafe { st.as_ref() }; | ||
// The I/O protocols cannot be used after exiting boot services. | ||
assert!(!st.boot_services.is_null(), "boot services are not active"); | ||
assert!(!st.stdin.is_null(), "stdin is not available"); | ||
|
||
let stdin: *mut Input = st.stdin.cast(); | ||
|
||
// SAFETY: `Input` is a `repr(transparent)` wrapper around the raw input | ||
// type. The underlying pointer in the system table is assumed to be valid. | ||
let stdin = unsafe { &mut *stdin }; | ||
|
||
f(stdin) | ||
} | ||
|
||
/// Call `f` with the [`Output`] protocol attached to stdout. | ||
/// | ||
/// # Panics | ||
/// | ||
/// This function will panic if called after exiting boot services, or if stdout | ||
/// is not available. | ||
pub fn with_stdout<F, R>(f: F) -> R | ||
where | ||
F: Fn(&mut Output) -> R, | ||
{ | ||
let st = table::system_table_raw_panicking(); | ||
// SAFETY: valid per requirements of `set_system_table`. | ||
let st = unsafe { st.as_ref() }; | ||
// The I/O protocols cannot be used after exiting boot services. | ||
assert!(!st.boot_services.is_null(), "boot services are not active"); | ||
assert!(!st.stdout.is_null(), "stdout is not available"); | ||
|
||
let stdout: *mut Output = st.stdout.cast(); | ||
|
||
// SAFETY: `Output` is a `repr(transparent)` wrapper around the raw output | ||
// type. The underlying pointer in the system table is assumed to be valid. | ||
let stdout = unsafe { &mut *stdout }; | ||
|
||
f(stdout) | ||
} | ||
|
||
/// Call `f` with the [`Output`] protocol attached to stderr. | ||
/// | ||
/// # Panics | ||
/// | ||
/// This function will panic if called after exiting boot services, or if stderr | ||
/// is not available. | ||
pub fn with_stderr<F, R>(f: F) -> R | ||
where | ||
F: Fn(&mut Output) -> R, | ||
{ | ||
let st = table::system_table_raw_panicking(); | ||
// SAFETY: valid per requirements of `set_system_table`. | ||
let st = unsafe { st.as_ref() }; | ||
// The I/O protocols cannot be used after exiting boot services. | ||
assert!(!st.boot_services.is_null(), "boot services are not active"); | ||
assert!(!st.stderr.is_null(), "stderr is not available"); | ||
|
||
let stderr: *mut Output = st.stderr.cast(); | ||
|
||
// SAFETY: `Output` is a `repr(transparent)` wrapper around the raw output | ||
// type. The underlying pointer in the system table is assumed to be valid. | ||
let stderr = unsafe { &mut *stderr }; | ||
|
||
f(stderr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: In a follow-up: We might should add a
[new api]
"tag" for that in the changelog - this might ease consumers to understand what's going onThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I was thinking maybe just add a short paragraph between
# uefi
and## Added
.