Skip to content

uefi: _print more failsafe #1239

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

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions uefi/src/helpers/println.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ use core::fmt::Write;
/// INTERNAL API! Helper for print macros.
#[doc(hidden)]
pub fn _print(args: core::fmt::Arguments) {
system_table_boot()
.expect("boot services are not active")
.stdout()
.write_fmt(args)
.expect("Failed to write to stdout");
if let Some(mut bs) = system_table_boot() {
bs.stdout()
.write_fmt(args)
.expect("Failed to write to stdout");
} else {
// Ease debugging: Depending on logger, this might write to serial or
// debugcon.
log::debug!("You are using `print!` after the boot services have been exited.");
}
}

/// Prints to the standard output.
/// Prints to the standard output of the UEFI boot service console.
///
/// # Usage
/// Use this similar to `print!` from the Rust standard library, but only
/// as long as boot services have not been exited.
///
/// You should never use this macro in a custom Logger ([`log::Log`] impl) to
/// prevent a circular runtime dependency.
///
/// # Panics
/// Will panic if `SYSTEM_TABLE` is `None` (Before [`uefi::helpers::init()`] and
Expand All @@ -28,7 +39,15 @@ macro_rules! print {
($($arg:tt)*) => ($crate::helpers::_print(core::format_args!($($arg)*)));
}

/// Prints to the standard output, with a newline.
/// Prints to the standard output of the UEFI boot service console, but with a
/// newline.
///
/// # Usage
/// Use this similar to `println!` from the Rust standard library, but only
/// as long as boot services have not been exited.
///
/// You should never use this macro in a custom Logger ([`log::Log`] impl) to
/// prevent a circular runtime dependency.
///
/// # Panics
/// Will panic if `SYSTEM_TABLE` is `None` (Before [`uefi::helpers::init()`] and
Expand Down