From 695f5489b65f7d2939609ad0b785efec17602f98 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Sat, 7 May 2022 18:35:34 -0400 Subject: [PATCH 01/11] Add print! and println! Created two macros for printing to stdout --- uefi-services/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/uefi-services/src/lib.rs b/uefi-services/src/lib.rs index 8252ad277..c0838d186 100644 --- a/uefi-services/src/lib.rs +++ b/uefi-services/src/lib.rs @@ -28,6 +28,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; @@ -95,6 +96,49 @@ pub fn init(st: &mut SystemTable) -> Result { } } +// Interal function for print macros +#[doc(hidden)] +pub fn _print(args: core::fmt::Arguments) { + unsafe { + if let Some(st) = &mut SYSTEM_TABLE { + 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 [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 [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 From 850c5a33e885c58a2b5b07cbaab23581d5aaf7c6 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Sat, 7 May 2022 18:36:59 -0400 Subject: [PATCH 02/11] Added panic! when SYSTEM_TABLE is None --- uefi-services/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/uefi-services/src/lib.rs b/uefi-services/src/lib.rs index c0838d186..9586f13a3 100644 --- a/uefi-services/src/lib.rs +++ b/uefi-services/src/lib.rs @@ -102,6 +102,8 @@ pub fn _print(args: core::fmt::Arguments) { unsafe { if let Some(st) = &mut SYSTEM_TABLE { st.stdout().write_fmt(args).expect("Failed to write to stdout"); + } else { + panic!("SYSTEM_TABLE is None"); } } } From 69d7c7df8ba585aa9978db3d113e0b00b89d3e79 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Sat, 7 May 2022 22:18:56 -0400 Subject: [PATCH 03/11] Fixed Typo --- uefi-services/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uefi-services/src/lib.rs b/uefi-services/src/lib.rs index 9586f13a3..4e6565550 100644 --- a/uefi-services/src/lib.rs +++ b/uefi-services/src/lib.rs @@ -96,7 +96,7 @@ pub fn init(st: &mut SystemTable) -> Result { } } -// Interal function for print macros +// Internal function for print macros #[doc(hidden)] pub fn _print(args: core::fmt::Arguments) { unsafe { From e2d627db578546aeebff31d448ae3bce32f4a534 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Thu, 19 May 2022 00:45:00 -0400 Subject: [PATCH 04/11] Update lib.rs --- uefi-services/src/lib.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/uefi-services/src/lib.rs b/uefi-services/src/lib.rs index 4e6565550..f92521f7b 100644 --- a/uefi-services/src/lib.rs +++ b/uefi-services/src/lib.rs @@ -96,23 +96,25 @@ pub fn init(st: &mut SystemTable) -> Result { } } -// Internal function for print macros +// Internal function for print macros. #[doc(hidden)] pub fn _print(args: core::fmt::Arguments) { unsafe { - if let Some(st) = &mut SYSTEM_TABLE { - st.stdout().write_fmt(args).expect("Failed to write to stdout"); - } else { - panic!("SYSTEM_TABLE is None"); - } + let st = SYSTEM_TABLE + .as_ref() + .expect("The system table handle is not available"); + + st.stdout() + .write_fmt(args) + .expect("Failed to write to stdout"); } } -/// Prints to the standard output -/// +/// Prints to the standard output. +/// /// # Panics -/// Will panic if SYSTEM_TABLE is None (Before [init] and after [exit_boot_services]) -/// +/// Will panic if `SYSTEM_TABLE` is `None` (Before [init] and after [exit_boot_services]). +/// /// # Examples /// ``` /// print!(""); @@ -124,11 +126,11 @@ macro_rules! print { ($($arg:tt)*) => ($crate::_print(core::format_args!($($arg)*))); } -/// Prints to the standard output, with a newline -/// +/// Prints to the standard output, with a newline. +/// /// # Panics -/// Will panic if SYSTEM_TABLE is None (Before [init] and after [exit_boot_services]) -/// +/// Will panic if `SYSTEM_TABLE` is `None` (Before [init] and after [exit_boot_services]). +/// /// # Examples /// ``` /// println!(); From 01f9b47f7142304bbe54f75a9963e885c3c09c53 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Thu, 19 May 2022 00:58:08 -0400 Subject: [PATCH 05/11] Update lib.rs --- uefi-services/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uefi-services/src/lib.rs b/uefi-services/src/lib.rs index f92521f7b..12e3d2e9d 100644 --- a/uefi-services/src/lib.rs +++ b/uefi-services/src/lib.rs @@ -101,7 +101,7 @@ pub fn init(st: &mut SystemTable) -> Result { pub fn _print(args: core::fmt::Arguments) { unsafe { let st = SYSTEM_TABLE - .as_ref() + .as_mut() .expect("The system table handle is not available"); st.stdout() From 6f92cf2e0999a49de2f84ee2d5f4c86d1a76ebbb Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Thu, 19 May 2022 01:09:02 -0400 Subject: [PATCH 06/11] Update lib.rs --- uefi-services/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uefi-services/src/lib.rs b/uefi-services/src/lib.rs index 12e3d2e9d..4c08f6f35 100644 --- a/uefi-services/src/lib.rs +++ b/uefi-services/src/lib.rs @@ -113,7 +113,7 @@ pub fn _print(args: core::fmt::Arguments) { /// Prints to the standard output. /// /// # Panics -/// Will panic if `SYSTEM_TABLE` is `None` (Before [init] and after [exit_boot_services]). +/// Will panic if `SYSTEM_TABLE` is `None` (Before [init()] and after [uefi::prelude::SystemTable::exit_boot_services()]). /// /// # Examples /// ``` @@ -129,7 +129,7 @@ macro_rules! print { /// Prints to the standard output, with a newline. /// /// # Panics -/// Will panic if `SYSTEM_TABLE` is `None` (Before [init] and after [exit_boot_services]). +/// Will panic if `SYSTEM_TABLE` is `None` (Before [init()] and after [uefi::prelude::SystemTable::exit_boot_services()]). /// /// # Examples /// ``` From 38f591db8444d2e932f4276c75d47966fa5ca2e8 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Mon, 1 Aug 2022 11:31:44 -0400 Subject: [PATCH 07/11] Added print! and println! tests Tests formatting with macros. --- uefi-test-runner/src/main.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/uefi-test-runner/src/main.rs b/uefi-test-runner/src/main.rs index 28a02dd30..c20bf18bf 100644 --- a/uefi-test-runner/src/main.rs +++ b/uefi-test-runner/src/main.rs @@ -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; @@ -28,6 +29,10 @@ fn efi_main(image: Handle, mut st: SystemTable) -> Status { st.firmware_vendor().as_str_in_buf(&mut buf).unwrap(); info!("Firmware Vendor: {}", buf.as_str()); + // Test print! and println! macros + 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"); From 038685749825994255def62577670c676836c00b Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Mon, 1 Aug 2022 11:32:35 -0400 Subject: [PATCH 08/11] Update main.rs Forgot period --- uefi-test-runner/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uefi-test-runner/src/main.rs b/uefi-test-runner/src/main.rs index c20bf18bf..3989ffcec 100644 --- a/uefi-test-runner/src/main.rs +++ b/uefi-test-runner/src/main.rs @@ -29,7 +29,7 @@ fn efi_main(image: Handle, mut st: SystemTable) -> Status { st.firmware_vendor().as_str_in_buf(&mut buf).unwrap(); info!("Firmware Vendor: {}", buf.as_str()); - // Test print! and println! macros + // Test print! and println! macros. print!("Testing {} macro with formatting: {:#010b} ", "print!", 155u8); println!("Testing {} macro with formatting: {:#010b} ", "println!", 155u8); From 5eeb2dc53a49a3ca394be5eaf55ddea80d6d296c Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Mon, 1 Aug 2022 11:34:54 -0400 Subject: [PATCH 09/11] Update main.rs Formatting --- uefi-test-runner/src/main.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/uefi-test-runner/src/main.rs b/uefi-test-runner/src/main.rs index 3989ffcec..c973c68ca 100644 --- a/uefi-test-runner/src/main.rs +++ b/uefi-test-runner/src/main.rs @@ -30,8 +30,14 @@ fn efi_main(image: Handle, mut st: SystemTable) -> Status { info!("Firmware Vendor: {}", buf.as_str()); // Test print! and println! macros. - print!("Testing {} macro with formatting: {:#010b} ", "print!", 155u8); - println!("Testing {} macro with formatting: {:#010b} ", "println!", 155u8); + 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"); From 531f28c53a3231b853b2ae0e59044a74fa0d9ff8 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Mon, 1 Aug 2022 11:39:27 -0400 Subject: [PATCH 10/11] Update main.rs clippy doesn't like constant strs in formatting --- uefi-test-runner/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/uefi-test-runner/src/main.rs b/uefi-test-runner/src/main.rs index c973c68ca..e84bee16d 100644 --- a/uefi-test-runner/src/main.rs +++ b/uefi-test-runner/src/main.rs @@ -30,13 +30,14 @@ fn efi_main(image: Handle, mut st: SystemTable) -> Status { 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 + print, 155u8 ); println!( "Testing {} macro with formatting: {:#010b} ", - "println!", 155u8 + println, 155u8 ); // Reset the console before running all the other tests. From 2dd3f3530a190837a7a8314706496cdae1edfaf9 Mon Sep 17 00:00:00 2001 From: Jonah Henriksson <33059163+JonahPlusPlus@users.noreply.github.com> Date: Mon, 1 Aug 2022 11:40:55 -0400 Subject: [PATCH 11/11] Update main.rs clippy again --- uefi-test-runner/src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/uefi-test-runner/src/main.rs b/uefi-test-runner/src/main.rs index e84bee16d..c3277f996 100644 --- a/uefi-test-runner/src/main.rs +++ b/uefi-test-runner/src/main.rs @@ -31,10 +31,7 @@ fn efi_main(image: Handle, mut st: SystemTable) -> Status { // Test print! and println! macros. let (print, println) = ("print!", "println!"); // necessary for clippy to ignore - print!( - "Testing {} macro with formatting: {:#010b} ", - print, 155u8 - ); + print!("Testing {} macro with formatting: {:#010b} ", print, 155u8); println!( "Testing {} macro with formatting: {:#010b} ", println, 155u8