kernel/
earlycon.rs

1//! Early console for generic architecture.
2//! 
3//! This module provides a simple early console interface for the kernel. It is
4//! used to print messages before the kernel heap is initialized.
5//!
6//! The early console is architecture-specific and must be implemented for each
7//! architecture. 
8
9use core::fmt::Write;
10
11use crate::arch::early_putc;
12
13#[macro_export]
14macro_rules! early_print {
15    ($($arg:tt)*) => ($crate::earlycon::print(format_args!($($arg)*)));
16}
17
18#[macro_export]
19macro_rules! early_println {
20    ($fmt:expr) => ($crate::early_print!(concat!($fmt, "\n")));
21    ($fmt:expr, $($arg:tt)*) => ($crate::early_print!(concat!($fmt, "\n"), $($arg)*));
22}
23
24pub fn print(args: core::fmt::Arguments) {
25    let mut writer = EarlyConsole {};
26    writer.write_fmt(args).unwrap();
27}
28
29struct EarlyConsole;
30
31impl Write for EarlyConsole {
32    fn write_str(&mut self, s: &str) -> core::fmt::Result {
33        for c in s.bytes() {
34            if c == b'\n' {
35                early_putc(b'\r');
36            }
37            early_putc(c);
38        }
39        Ok(())
40    }
41}