Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/aienos-kernel/src/display/mod.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / aienos-kernel / src / display / mod.rs` 258 lines ยท 8245 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! Early boot screen: text drawn straight into the firmware-provided 17 //! framebuffer (UEFI GOP) after boot services exit. No GPU driver is involved; 18 //! the firmware has already set the display mode. 19 20 pub mod font; 21 22 use core::fmt; 23 use core::ptr::write_volatile; 24 use font::{lit, GLYPH_HEIGHT, GLYPH_WIDTH}; 25 26 /// Byte order of a 32-bit framebuffer pixel, as reported by GOP. 27 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 28 pub enum PixelOrder { 29 /// Bytes are red, green, blue, reserved. 30 Rgb, 31 /// Bytes are blue, green, red, reserved. 32 Bgr, 33 } 34 35 /// Framebuffer geometry recorded before firmware exit. 36 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 37 pub struct FramebufferInfo { 38 pub base: u64, 39 pub size_bytes: u64, 40 pub width: usize, 41 pub height: usize, 42 /// Pixels per scanline (may exceed `width`). 43 pub stride: usize, 44 pub order: PixelOrder, 45 } 46 47 impl FramebufferInfo { 48 /// Rejects geometry that would write outside the reported buffer. 49 pub fn validated(self) -> Option { 50 let needed = (self.stride as u64) 51 .checked_mul(self.height as u64)? 52 .checked_mul(4)?; 53 (self.width > 0 54 && self.height > 0 55 && self.stride >= self.width 56 && needed <= self.size_bytes) 57 .then_some(self) 58 } 59 } 60 61 const CELL_W: usize = GLYPH_WIDTH + 1; 62 const CELL_H: usize = GLYPH_HEIGHT + 3; 63 /// Columns the text grid aims for; the scale grows until 80 cells fill the width. 64 const TARGET_COLUMNS: usize = 80; 65 const MARGIN_CELLS: usize = 1; 66 67 pub const BACKGROUND: (u8, u8, u8) = (0x10, 0x14, 0x1c); 68 pub const FOREGROUND: (u8, u8, u8) = (0xe8, 0xec, 0xf2); 69 pub const ACCENT: (u8, u8, u8) = (0x5c, 0xd6, 0x8a); 70 71 /// Text console over a linear 32-bit framebuffer. 72 pub struct Screen { 73 base: *mut u32, 74 info: FramebufferInfo, 75 scale: usize, 76 col: usize, 77 row: usize, 78 color: (u8, u8, u8), 79 } 80 81 impl Screen { 82 /// # Safety 83 /// `info` must describe memory that is mapped, writable and not used by 84 /// anything else for the lifetime of the screen. 85 pub unsafe fn new(info: FramebufferInfo) -> Option { 86 let info = info.validated()?; 87 Some(Self { 88 base: info.base as usize as *mut u32, 89 info, 90 scale: (info.width / (TARGET_COLUMNS * CELL_W)).max(1), 91 col: MARGIN_CELLS, 92 row: MARGIN_CELLS, 93 color: FOREGROUND, 94 }) 95 } 96 97 fn pixel(&self, (r, g, b): (u8, u8, u8)) -> u32 { 98 match self.info.order { 99 PixelOrder::Bgr => u32::from(r) << 16 | u32::from(g) << 8 | u32::from(b), 100 PixelOrder::Rgb => u32::from(b) << 16 | u32::from(g) << 8 | u32::from(r), 101 } 102 } 103 104 fn fill(&mut self, x0: usize, y0: usize, w: usize, h: usize, color: (u8, u8, u8)) { 105 let value = self.pixel(color); 106 for y in y0..(y0 + h).min(self.info.height) { 107 for x in x0..(x0 + w).min(self.info.width) { 108 // SAFETY: x < width <= stride and y < height, so the offset is 109 // inside the buffer checked by `FramebufferInfo::validated`. 110 unsafe { write_volatile(self.base.add(y * self.info.stride + x), value) }; 111 } 112 } 113 } 114 115 pub fn clear(&mut self) { 116 let (w, h) = (self.info.width, self.info.height); 117 self.fill(0, 0, w, h, BACKGROUND); 118 self.col = MARGIN_CELLS; 119 self.row = MARGIN_CELLS; 120 } 121 122 pub fn set_color(&mut self, color: (u8, u8, u8)) { 123 self.color = color; 124 } 125 126 pub fn columns(&self) -> usize { 127 self.info.width / (CELL_W * self.scale) 128 } 129 130 pub fn rows(&self) -> usize { 131 self.info.height / (CELL_H * self.scale) 132 } 133 134 fn newline(&mut self) { 135 self.col = MARGIN_CELLS; 136 self.row += 1; 137 } 138 139 pub fn put_char(&mut self, c: char) { 140 if c == '\n' { 141 self.newline(); 142 return; 143 } 144 if self.col + MARGIN_CELLS >= self.columns() { 145 self.newline(); 146 } 147 if self.row + MARGIN_CELLS >= self.rows() { 148 return; // Screen full: drop rather than scroll over earlier lines. 149 } 150 let s = self.scale; 151 let (x0, y0) = (self.col * CELL_W * s, self.row * CELL_H * s); 152 let color = self.color; 153 for gy in 0..GLYPH_HEIGHT { 154 for gx in 0..GLYPH_WIDTH { 155 if lit(c, gx, gy) { 156 self.fill(x0 + gx * s, y0 + gy * s, s, s, color); 157 } 158 } 159 } 160 self.col += 1; 161 } 162 163 /// Paint a blank line at the current row (used for in-place countdowns). 164 pub fn clear_row(&mut self) { 165 let s = self.scale; 166 let y = self.row * CELL_H * s; 167 let w = self.info.width; 168 self.fill(0, y, w, CELL_H * s, BACKGROUND); 169 self.col = MARGIN_CELLS; 170 } 171 } 172 173 impl fmt::Write for Screen { 174 fn write_str(&mut self, s: &str) -> fmt::Result { 175 s.chars().for_each(|c| self.put_char(c)); 176 Ok(()) 177 } 178 } 179 180 #[cfg(test)] 181 mod tests { 182 use super::*; 183 use core::fmt::Write; 184 185 fn screen(buf: &mut std::vec::Vec, width: usize, height: usize, stride: usize) -> Screen { 186 let info = FramebufferInfo { 187 base: buf.as_mut_ptr() as u64, 188 size_bytes: (buf.len() * 4) as u64, 189 width, 190 height, 191 stride, 192 order: PixelOrder::Bgr, 193 }; 194 unsafe { Screen::new(info) }.unwrap() 195 } 196 197 #[test] 198 fn rejects_geometry_larger_than_the_buffer() { 199 let info = FramebufferInfo { 200 base: 0x1000, 201 size_bytes: 100 * 100 * 4, 202 width: 100, 203 height: 101, 204 stride: 100, 205 order: PixelOrder::Rgb, 206 }; 207 assert!(info.validated().is_none()); 208 assert!(FramebufferInfo { 209 height: 100, 210 ..info 211 } 212 .validated() 213 .is_some()); 214 assert!(FramebufferInfo { 215 stride: 99, 216 height: 10, 217 ..info 218 } 219 .validated() 220 .is_none()); 221 } 222 223 #[test] 224 fn draws_glyph_pixels_and_respects_stride_padding() { 225 // 480 px wide gives scale 1; stride 500 leaves 20 px of padding per line. 226 let (w, h, stride) = (480, 120, 500); 227 let mut buf = std::vec![0u32; stride * h]; 228 let mut s = screen(&mut buf, w, h, stride); 229 s.clear(); 230 write!(s, "L").unwrap(); 231 let fg = 232 u32::from(FOREGROUND.0) << 16 | u32::from(FOREGROUND.1) << 8 | u32::from(FOREGROUND.2); 233 let (x0, y0) = (MARGIN_CELLS * CELL_W, MARGIN_CELLS * CELL_H); 234 let at = |x: usize, y: usize| buf[(y0 + y) * stride + x0 + x]; 235 // The L glyph: left column lit, bottom row lit, top-right dark. 236 assert_eq!(at(0, 0), fg); 237 assert_eq!(at(4, 6), fg); 238 assert_ne!(at(4, 0), fg); 239 // Padding beyond `width` is never touched. 240 assert!((0..h).all(|y| buf[y * stride + w..(y + 1) * stride] 241 .iter() 242 .all(|p| *p == 0))); 243 } 244 245 #[test] 246 fn scales_up_on_wide_screens_and_drops_text_past_the_last_row() { 247 let (w, h) = (1920, 40); 248 let mut buf = std::vec![0u32; w * h]; 249 let mut s = screen(&mut buf, w, h, w); 250 assert_eq!(s.columns(), 80); 251 assert_eq!(s.rows(), 1); 252 s.clear(); 253 write!(s, "HELLO").unwrap(); // Row 1 is past the last row at this height. 254 let bg = 255 u32::from(BACKGROUND.0) << 16 | u32::from(BACKGROUND.1) << 8 | u32::from(BACKGROUND.2); 256 assert!(buf.iter().all(|p| *p == bg)); 257 } 258 }