strat9_kernel/arch/x86_64/vga/
boot_progress.rs1use super::api;
35use crate::framebuffer::RgbColor;
36use core::sync::atomic::{AtomicUsize, Ordering};
37
38pub const TOTAL: usize = 11;
41
42pub(crate) const TRACK: RgbColor = RgbColor::new(0xDD, 0xDD, 0xEC);
45pub(crate) const FILL: RgbColor = RgbColor::new(0xA8, 0xE6, 0xCF);
47pub(crate) const BORDER: RgbColor = RgbColor::new(0xB5, 0xB5, 0xC8);
49pub(crate) const LABEL_FG: RgbColor = RgbColor::new(0x4A, 0x4A, 0x5E);
51pub(crate) const LABEL_BG: RgbColor = RgbColor::new(0xF2, 0xF0, 0xF7);
52
53pub fn band_height(_h: usize, gh: usize) -> usize {
55 gh + 4 + gh.max(10) + 6
56}
57
58pub 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
70pub 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
87static 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
92pub(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
107pub(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
123pub 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
135pub(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}