summaryrefslogtreecommitdiff
path: root/kernel/src/init/earlyprintk.rs
blob: ae26a2fb981eab20a9c793aa570e58c53ca1e38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use spin::{lazylock::LazyLock, mutex::Mutex};

use crate::io::output::serial::uart_16550::Uart16550;

// TODO: use linkme instead
pub static EARLY_PRINTK_SERIAL: LazyLock<Option<Mutex<Uart16550>>> = LazyLock::new(
    || match unsafe { Uart16550::init([Some(Default::default()), None, None, None]) } {
        Ok(o) => Some(Mutex::new(o)),
        Err(_) => None,
    },
);

// TODO: configurable via option or arguments
// or use a system similar to the drivers(`linkme`)
pub macro printk($($arg:tt)*) {{
	use ::core::fmt::Write;
	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(()),
	}
}}

pub macro printlnk($($arg:tt)*) {{
	use ::core::fmt::Write;
	match &*$crate::init::earlyprintk::EARLY_PRINTK_SERIAL {
		Some(m) => writeln!(m.lock().coms[0].as_mut().unwrap(), $($arg)*),
		None => Ok(()),
	}
}}