diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 19:14:52 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-09-06 19:14:52 +0800 |
| commit | ad877e5ca439b5ce9f54c1673b2bcc9607db828f (patch) | |
| tree | c2d7ef3112259e366a4d09bd2f9c7e55202904bd /kernel/src/init | |
| parent | 4820b2dd73543a6fbd5db547d009529d0fe11ccd (diff) | |
refactor: earlyprintk and uart_16550
Diffstat (limited to 'kernel/src/init')
| -rw-r--r-- | kernel/src/init/earlyprintk.rs | 24 |
1 files changed, 18 insertions, 6 deletions
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<Mutex<Uart16550>> = LazyLock::new(|| { - Mutex::new(unsafe { Uart16550::init(SerialPortBitMask::COM1, [const { None }; 4]).unwrap() }) -}); +pub static EARLY_PRINTK_SERIAL: LazyLock<Option<Mutex<Uart16550>>> = + 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(()), + } }); } |
