From ad877e5ca439b5ce9f54c1673b2bcc9607db828f Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 6 Sep 2026 19:14:52 +0800 Subject: refactor: earlyprintk and uart_16550 --- kernel/src/init/earlyprintk.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'kernel/src/init') diff --git a/kernel/src/init/earlyprintk.rs b/kernel/src/init/earlyprintk.rs index 01c50fa..a217a92 100644 --- a/kernel/src/init/earlyprintk.rs +++ b/kernel/src/init/earlyprintk.rs @@ -1,17 +1,26 @@ use spin::{lazylock::LazyLock, mutex::Mutex}; -use crate::io::output::serial::uart_16550::{SerialPortBitMask, Uart16550}; +use crate::io::output::serial::uart_16550::Uart16550; // TODO: use linkme instead -pub static EARLY_PRINTK_SERIAL: LazyLock> = LazyLock::new(|| { - Mutex::new(unsafe { Uart16550::init(SerialPortBitMask::COM1, [const { None }; 4]).unwrap() }) -}); +pub static EARLY_PRINTK_SERIAL: LazyLock>> = + LazyLock::new(|| match unsafe { Uart16550::init([const { None }; 4]) } { + Ok(o) => Some(Mutex::new(o)), + Err(_) => None, + }); #[macro_export] +// TODO: configurable via option or arguments +// or use a system similar to the drivers(`linkme`) macro_rules! printk { ($($arg:tt)*) => ({ use ::core::fmt::Write; - write!($crate::init::earlyprintk::EARLY_PRINTK_SERIAL.lock().coms[0].as_mut().unwrap(), $($arg)*) + match &*$crate::init::earlyprintk::EARLY_PRINTK_SERIAL { + // TODO: elim the unwrap + Some(m) => write!(m.lock().coms[0].as_mut().unwrap(), $($arg)*), + // TODO: if needed find a way to return an error here, but for now just ignore it + None => Ok(()), + } }); } @@ -19,6 +28,9 @@ macro_rules! printk { macro_rules! printkln { ($($arg:tt)*) => ({ use ::core::fmt::Write; - writeln!($crate::init::earlyprintk::EARLY_PRINTK_SERIAL.lock().coms[0].as_mut().unwrap(), $($arg)*) + match &*$crate::init::earlyprintk::EARLY_PRINTK_SERIAL { + Some(m) => writeln!(m.lock().coms[0].as_mut().unwrap(), $($arg)*), + None => Ok(()), + } }); } -- cgit v1.2.3