summaryrefslogtreecommitdiff
path: root/kernel/src/io/panic_screen.rs
blob: ce2c118bec18428c3f68dd424a4c40d4280b4d3b (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use core::fmt::Write;
use crate::io::vga_text::{OStream, CharState, Color};

const PANIC_SCREEN_MESSAGE_BUFFER_SIZE: usize = 2048;

struct TextBuffer<'a> {
    dst: &'a mut [u8],
    len: usize
}

impl<'a> TextBuffer<'a> {
    fn new(dst: &'a mut [u8]) -> Self {
        Self { dst, len: 0 }
    }
    fn get(&'a mut self) -> &'a mut [u8] {
        &mut self.dst[..self.len]
    }
}

impl<'a> core::fmt::Write for TextBuffer<'a> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        if s.len() + self.len > self.dst.len() {
            Err(core::fmt::Error)
        } else {
            let s = s.as_bytes();
            (&mut self.dst[self.len..self.len+s.len()]).clone_from_slice(s);
            Ok(self.len += s.len())
        }
    }
}

pub fn show(args: Option<&core::fmt::Arguments>) {
    let mut stderr = OStream::new();
    stderr.set_state(CharState::from_colors(
        Color::LightRed,
        Color::Red,
    ));
    stderr.clear();
    stderr.print(b"uff-os");
    stderr.set_row(10);
    stderr.set_state(CharState::from_colors(
        Color::White,
        Color::Red,
    ));
    stderr.print_centered(b"<kernel panic>");
    stderr.set_row(14);
    stderr.set_state(CharState::from_colors(
        Color::Cyan,
        Color::Red,
    ));
    stderr.set_centered(true);
    let buffer: &mut [u8] = &mut [b' '; PANIC_SCREEN_MESSAGE_BUFFER_SIZE];
    let mut tbuffer = TextBuffer::new(buffer);
    let _ = core::fmt::write(&mut tbuffer, *args.unwrap_or(&format_args!("no panic information obtainable")));
    stderr.print(tbuffer.get());
    stderr.set_centered(false);
}