From 52f53ef73c8d47900861279f4dafa01331ba49b7 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Sat, 13 Jul 2024 11:25:04 +0200 Subject: [PATCH] uefi: _print more failsafe When one accidentally call println! after exit_boot_services, one now at least gets some more debugging info why the machine suddenly reboots. Specifically, this prints now info to `integration-test-debugcon.log` in such a case. --- uefi/src/helpers/println.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/uefi/src/helpers/println.rs b/uefi/src/helpers/println.rs index 2a30eb1d5..2b9903e92 100644 --- a/uefi/src/helpers/println.rs +++ b/uefi/src/helpers/println.rs @@ -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 @@ -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