Skip to content

Added macros print! and println! #430

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 12 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions uefi-services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern crate log;
extern crate uefi;

use core::ffi::c_void;
use core::fmt::Write;
use core::ptr::NonNull;

use cfg_if::cfg_if;
Expand Down Expand Up @@ -93,6 +94,53 @@ pub fn init(st: &mut SystemTable<Boot>) -> Result {
}
}

// Internal function for print macros.
#[doc(hidden)]
pub fn _print(args: core::fmt::Arguments) {
unsafe {
let st = SYSTEM_TABLE
.as_mut()
.expect("The system table handle is not available");

st.stdout()
.write_fmt(args)
.expect("Failed to write to stdout");
}
}

/// Prints to the standard output.
///
/// # Panics
/// Will panic if `SYSTEM_TABLE` is `None` (Before [init()] and after [uefi::prelude::SystemTable::exit_boot_services()]).
///
/// # Examples
/// ```
/// print!("");
/// print!("Hello World\n");
/// print!("Hello {}", "World");
/// ```
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::_print(core::format_args!($($arg)*)));
}

/// Prints to the standard output, with a newline.
///
/// # Panics
/// Will panic if `SYSTEM_TABLE` is `None` (Before [init()] and after [uefi::prelude::SystemTable::exit_boot_services()]).
///
/// # Examples
/// ```
/// println!();
/// println!("Hello World");
/// println!("Hello {}", "World");
/// ```
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::_print(core::format_args!("{}{}", core::format_args!($($arg)*), "\n")));
}

/// Set up logging
///
/// This is unsafe because you must arrange for the logger to be reset with
Expand Down
9 changes: 9 additions & 0 deletions uefi-test-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use alloc::string::String;
use uefi::prelude::*;
use uefi::proto::console::serial::Serial;
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams};
use uefi_services::{print, println};

mod boot;
mod proto;
Expand All @@ -28,6 +29,14 @@ fn efi_main(image: Handle, mut st: SystemTable<Boot>) -> Status {
st.firmware_vendor().as_str_in_buf(&mut buf).unwrap();
info!("Firmware Vendor: {}", buf.as_str());

// Test print! and println! macros.
let (print, println) = ("print!", "println!"); // necessary for clippy to ignore
print!("Testing {} macro with formatting: {:#010b} ", print, 155u8);
println!(
"Testing {} macro with formatting: {:#010b} ",
println, 155u8
);

// Reset the console before running all the other tests.
st.stdout().reset(false).expect("Failed to reset stdout");

Expand Down