Skip to main content

strat9_kernel/arch/x86_64/vga/
boot_progress.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! Pastel boot-progress bar drawn along the bottom of the framebuffer.
24//!
25//! Each boot milestone calls [`render`] with its ordinal; the bar fills
26//! proportionally and shows the current stage label. If the boot freezes, the
27//! bar simply stops growing at the last completed stage : a zero-cost visual
28//! "where did it hang" on hardware with no serial port (the target X13 case).
29//!
30//! All drawing goes through the guarded VGA primitives, so every call made
31//! before the framebuffer console is up : or under `console.mode=serial` : is a
32//! silent no-op. No allocation, no locks of our own: safe at any boot stage.
33
34use super::api;
35use crate::framebuffer::RgbColor;
36use core::sync::atomic::{AtomicUsize, Ordering};
37
38/// Total number of boot milestones. Must match the highest ordinal passed by
39/// the call sites in `kernel_main` / `init_apic_subsystem`.
40pub const TOTAL: usize = 11;
41
42// Light / pastel palette (the user asked for clair/pastel).
43/// Empty part of the track: soft lavender grey.
44pub(crate) const TRACK: RgbColor = RgbColor::new(0xDD, 0xDD, 0xEC);
45/// Completed part: pastel mint green.
46pub(crate) const FILL: RgbColor = RgbColor::new(0xA8, 0xE6, 0xCF);
47/// Thin border around the track: muted grey.
48pub(crate) const BORDER: RgbColor = RgbColor::new(0xB5, 0xB5, 0xC8);
49/// Label text: muted slate on a near-white strip.
50pub(crate) const LABEL_FG: RgbColor = RgbColor::new(0x4A, 0x4A, 0x5E);
51pub(crate) const LABEL_BG: RgbColor = RgbColor::new(0xF2, 0xF0, 0xF7);
52
53/// Reserved pixel height of the HUD band: label strip + gap + bar + padding.
54pub fn band_height(_h: usize, gh: usize) -> usize {
55    gh + 4 + gh.max(10) + 6
56}
57
58/// Shared HUD geometry: the writer reserves exactly this band and the bar
59/// draws into it, so the console text and the bar can never overlap.
60pub struct Layout {
61    pub band_y: usize,
62    pub band_h: usize,
63    pub label_y: usize,
64    pub bar_x: usize,
65    pub bar_y: usize,
66    pub bar_w: usize,
67    pub bar_h: usize,
68}
69
70/// Computes the HUD band layout for a `w`×`h` framebuffer.
71pub fn layout(w: usize, h: usize, gh: usize, status_h: usize) -> Layout {
72    let band_h = band_height(h, gh);
73    let band_y = h.saturating_sub(status_h + band_h);
74    let margin = (w / 20).max(8);
75    let bar_h = gh.max(10);
76    Layout {
77        band_y,
78        band_h,
79        label_y: band_y + 2,
80        bar_x: margin,
81        bar_y: band_y + 2 + gh + 2,
82        bar_w: w.saturating_sub(2 * margin),
83        bar_h,
84    }
85}
86
87// Last rendered milestone, replayed after a full-screen clear.
88static HAS_LAST: core::sync::atomic::AtomicBool = core::sync::atomic::AtomicBool::new(false);
89static LAST_STEP: AtomicUsize = AtomicUsize::new(0);
90static LAST_LABEL: spin::Mutex<([u8; 64], usize)> = spin::Mutex::new(([0u8; 64], 0));
91
92/// Remember the last drawn milestone so `clear_with()` can repaint it.
93pub(crate) fn remember(step: usize, label: &str) {
94    let bytes = label.as_bytes();
95    let mut n = bytes.len().min(64);
96    while n > 0 && !label.is_char_boundary(n) {
97        n -= 1;
98    }
99    if let Some(mut guard) = LAST_LABEL.try_lock() {
100        guard.0[..n].copy_from_slice(&bytes[..n]);
101        guard.1 = n;
102    }
103    LAST_STEP.store(step, Ordering::Relaxed);
104    HAS_LAST.store(true, Ordering::Relaxed);
105}
106
107/// Copy the last drawn milestone into `buf` and return it.
108pub(crate) fn last_render<'a>(buf: &'a mut [u8; 96]) -> Option<(usize, &'a str)> {
109    if !HAS_LAST.load(Ordering::Relaxed) {
110        return None;
111    }
112    let n = {
113        let guard = LAST_LABEL.try_lock()?;
114        let (bytes, len) = &*guard;
115        let n = (*len).min(buf.len());
116        buf[..n].copy_from_slice(&bytes[..n]);
117        n
118    };
119    let text = core::str::from_utf8(&buf[..n]).ok()?;
120    Some((LAST_STEP.load(Ordering::Relaxed), text))
121}
122
123/// Draws the bar at `step`/[`TOTAL`] with a short stage label.
124///
125/// All drawing happens in a single writer lock (`draw_boot_progress`), so the
126/// bar is repainted atomically and never interleaves with console text.
127pub fn render(step: usize, label: &str) {
128    if !api::is_available() {
129        return;
130    }
131    let step = step.min(TOTAL);
132    let _ = api::with_writer(|w| w.draw_boot_progress(step, label));
133}
134
135/// Writes `"[step/total] "` into `buf`, returning the number of bytes written.
136pub(crate) fn fmt_prefix(buf: &mut [u8], step: usize, total: usize) -> usize {
137    let mut len = 0;
138    push(buf, &mut len, b'[');
139    len = push_usize(buf, len, step);
140    push(buf, &mut len, b'/');
141    len = push_usize(buf, len, total);
142    push(buf, &mut len, b']');
143    push(buf, &mut len, b' ');
144    len
145}
146
147fn push(buf: &mut [u8], len: &mut usize, byte: u8) {
148    if *len < buf.len() {
149        buf[*len] = byte;
150        *len += 1;
151    }
152}
153
154fn push_usize(buf: &mut [u8], mut len: usize, mut value: usize) -> usize {
155    let mut digits = [0u8; 20];
156    let mut count = 0;
157    if value == 0 {
158        digits[count] = b'0';
159        count += 1;
160    } else {
161        while value > 0 {
162            digits[count] = b'0' + (value % 10) as u8;
163            value /= 10;
164            count += 1;
165        }
166    }
167    while count > 0 {
168        count -= 1;
169        push(buf, &mut len, digits[count]);
170    }
171    len
172}