Skip to content

Commit c488219

Browse files
committed
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.
1 parent 4ca4daa commit c488219

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

uefi/src/helpers/println.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ use core::fmt::Write;
44
/// INTERNAL API! Helper for print macros.
55
#[doc(hidden)]
66
pub fn _print(args: core::fmt::Arguments) {
7-
system_table_boot()
8-
.expect("boot services are not active")
9-
.stdout()
10-
.write_fmt(args)
11-
.expect("Failed to write to stdout");
7+
if let Some(mut bs) = system_table_boot() {
8+
bs.stdout()
9+
.write_fmt(args)
10+
.expect("Failed to write to stdout");
11+
} else {
12+
// Ease debugging: Depending on logger, this might write to serial or
13+
// debugcon.
14+
log::debug!("You are using `print!` after the boot services have been exited.");
15+
}
1216
}
1317

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

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

0 commit comments

Comments
 (0)