Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/src/integrate/dxe_core.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ In this example, we will talk about how to configure the loggers provided by Pat
```rust
# extern crate patina;
# extern crate log;
use patina::peripheral::serial::uart::UartNull; // UartPl011 (AARCH64) and Uart16550 (X64) exist
use patina::peripheral::serial::uart::UartNull; // UartPl011 (AARCH64), Uart16550 (X64) and UartQemuDebug (X64) exist
use patina::debug::log::{Format, SerialLogger};
use log::LevelFilter;

Expand Down
13 changes: 12 additions & 1 deletion sdk/patina/src/debug/log/serial_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! SPDX-License-Identifier: Apache-2.0
//!
use crate::peripheral::serial::{SerialIO, shared::SharedSerial};
use crate::peripheral::serial::{shared::SharedSerial, SerialIO};
use core::marker::Send;
use spin::Mutex;

Expand Down Expand Up @@ -57,6 +57,11 @@ where
}
}

/// Initialize serial port.
pub fn init(&self) {
let _ = self.serial_port.init();
}

/// Switches the logger to blocking (spinning) serial acquisition, consuming and returning `self`.
///
/// In blocking mode, each record is written under a logger-level lock so concurrent cores
Expand Down Expand Up @@ -150,6 +155,7 @@ mod tests {
] {
let (mock, buffer) = recording_serial();
let logger = Logger::new(format, &[], LevelFilter::Trace, mock);
logger.init();
let args = format_args!("hello");
logger.log(&log::Record::builder().args(args).level(level).target("test").build());
assert_eq!(String::from_utf8(buffer.lock().clone()).expect("valid utf8"), expected);
Expand All @@ -159,12 +165,14 @@ mod tests {
#[test]
fn test_serial_logger_filters() {
let logger = Logger::new(Format::Standard, &[], LevelFilter::Warn, MockSerialIO::new());
logger.init();
assert!(logger.enabled(&metadata(Level::Error, "any")));
assert!(logger.enabled(&metadata(Level::Warn, "any")));
assert!(!logger.enabled(&metadata(Level::Info, "any")));

let filters = &[("noisy::module", LevelFilter::Off), ("app", LevelFilter::Debug)];
let logger = Logger::new(Format::Standard, filters, LevelFilter::Info, MockSerialIO::new());
logger.init();
assert!(!logger.enabled(&metadata(Level::Error, "noisy::module::inner")));
assert!(logger.enabled(&metadata(Level::Debug, "app::sub")));
assert!(!logger.enabled(&metadata(Level::Trace, "app::sub")));
Expand All @@ -177,6 +185,7 @@ mod tests {
let mut mock = MockSerialIO::new();
mock.expect_write().never();
let logger = Logger::new(Format::Standard, &[], LevelFilter::Warn, mock);
logger.init();

let args = format_args!("should be filtered out");
logger.log(&log::Record::builder().args(args).level(Level::Info).target("test").build());
Expand All @@ -187,6 +196,7 @@ mod tests {
fn test_serial_logger_blocking_writes_intact_record() {
let (mock, buffer) = recording_serial();
let logger = Logger::new(Format::Standard, &[], LevelFilter::Trace, mock).with_blocking();
logger.init();
assert!(logger.blocking);

let args = format_args!("hello");
Expand All @@ -206,6 +216,7 @@ mod tests {

let (mock, buffer) = recording_serial();
let logger = Logger::new(Format::Standard, &[], LevelFilter::Trace, mock).with_blocking();
logger.init();

thread::scope(|scope| {
for message in ["aaaa", "bbbb"] {
Expand Down
5 changes: 5 additions & 0 deletions sdk/patina/src/peripheral/serial/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ mod uart_16550;
#[cfg(target_arch = "x86_64")]
pub use uart_16550::Uart16550;

#[cfg(target_arch = "x86_64")]
mod qemudebug;
#[cfg(target_arch = "x86_64")]
pub use qemudebug::UartQemuDebug;

mod pl011;
pub use pl011::UartPl011;
86 changes: 86 additions & 0 deletions sdk/patina/src/peripheral/serial/uart/qemudebug.rs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to advertise that this available in the DXE Core integration example in the mdbook by updating this comment to include UartQemuDebug:

use patina::peripheral::serial::uart::UartNull; // UartPl011 (AARCH64) and Uart16550 (X64) exist

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! [`UartQemuDebug`] - [`SerialIO`](crate::peripheral::serial::SerialIO) implementation.
//!
//! ## License
//!
//! SPDX-License-Identifier: Apache-2.0
//!

use core::arch::asm;

/// interface for the qemu debug console
///
/// The `isa-debugcon` device logs any data written to the port
/// specified via `iobase` property. On reads it returns 0xe9. The

Check warning on line 13 in sdk/patina/src/peripheral/serial/uart/qemudebug.rs

View workflow job for this annotation

GitHub Actions / Run / Basic Repo CI

Unknown word (iobase)
/// latter allows presense detection, it is not possible to actually
/// read input data, this is a output-only device.
///
/// By convention `iobase=0x402` is used for firmware logging.

Check warning on line 17 in sdk/patina/src/peripheral/serial/uart/qemudebug.rs

View workflow job for this annotation

GitHub Actions / Run / Basic Repo CI

Unknown word (iobase)
///
/// typical usage:
/// ```
/// qemu-system-x86_64 \
/// -chardev file,path=/path/to/firmware.log,id=fw \

Check warning on line 22 in sdk/patina/src/peripheral/serial/uart/qemudebug.rs

View workflow job for this annotation

GitHub Actions / Run / Basic Repo CI

Unknown word (chardev)
/// -device isa-debugcon,iobase=0x402,chardev=fw \

Check warning on line 23 in sdk/patina/src/peripheral/serial/uart/qemudebug.rs

View workflow job for this annotation

GitHub Actions / Run / Basic Repo CI

Unknown word (chardev)

Check warning on line 23 in sdk/patina/src/peripheral/serial/uart/qemudebug.rs

View workflow job for this annotation

GitHub Actions / Run / Basic Repo CI

Unknown word (iobase)
/// $moreargs

Check warning on line 24 in sdk/patina/src/peripheral/serial/uart/qemudebug.rs

View workflow job for this annotation

GitHub Actions / Run / Basic Repo CI

Unknown word (moreargs)
/// ```
#[derive(Debug)]
pub struct UartQemuDebug {
io_base: u16,
present: bool,
}

impl UartQemuDebug {
// read value indicating the device is present
const PROBE: u8 = 0xe9;

/// create `UartQemuDebug` with io base specified
///
/// # Safety
///
/// The caller must ensure `io_base` points to a valid isa-debugcon io port.
pub const unsafe fn new(io_base: u16) -> Self {
Self { io_base, present: false }
}

fn outb(&self, value: u8) {
// SAFETY: write output byte to isa-debugcon device
unsafe {
asm!("outb dx, al", in("al") value, in("dx") self.io_base,
options(nomem,nostack,preserves_flags)
)
}
}

fn inb(&self) -> u8 {
// SAFETY: read device detection byte from isa-debugcon device
unsafe {
let ret: u8;
asm!("inb al, dx", in("dx") self.io_base, out("al") ret);
ret
}
}
}

impl crate::peripheral::serial::SerialIO for UartQemuDebug {
fn init(&mut self) {
let value = self.inb();
self.present = value == Self::PROBE;
}

fn write(&mut self, buffer: &[u8]) {
if self.present {
for c in buffer {
self.outb(*c);
}
}
}

fn read(&mut self) -> u8 {
// PANIC: Would loop forever, better to panic.
panic!();
}

fn try_read(&mut self) -> Option<u8> {
None
}
}
Loading