summaryrefslogtreecommitdiff
path: root/kernel/src/io/panic_screen.rs
blob: dddf8f58aad4b8fd6873f812241ba7e527c89abd (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
58
use crate::io::vga_text::Color;
use core::fmt::Write;

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(&self) -> &[u8] {
        &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>) -> core::fmt::Result {
    use Color::*;
    let mut vga = crate::vga_lock!();
    vga.set_color_state(LightRed, Red);
    vga.clear();
    vga.put_const_byte_str(*b"uff-os");
    vga.new_lines(10);
    vga.set_color_state(White, Red);
    vga.put_arguments(format_args!(
        "{:^width$}",
        "<kernel panic>",
        width = vga.width()
    ));
    vga.new_lines(2);
    vga.set_color_state(Cyan, Red);

    let buffer = &mut [0u8; PANIC_SCREEN_MESSAGE_BUFFER_SIZE];
    let mut tbuffer = TextBuffer::new(buffer);
    core::fmt::write(
        &mut tbuffer,
        *args.unwrap_or(&format_args!("no panic information obtainable")),
    )?;
    for line in tbuffer.get().chunks(vga.width()) {
        vga.put_byte_str(line);
    }
    Ok(())
}