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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/// GOP framebuffer driver — renders text via a bitmap font.
///
/// Text surface (cursor, scrolling, colours) lives in `crate::vt`; this
/// module is the low-level rasteriser: glyph blits and pixel shifts on the
/// linear framebuffer.
use crate::font;
use crate::uefi::{GraphicsOutputProtocol, PIXEL_BGR, PIXEL_RGB};
use core::fmt;
use core::ptr;
/// Framebuffer state captured before ExitBootServices.
pub struct Framebuffer {
base: *mut u8,
#[allow(dead_code)]
width: usize,
height: usize,
pitch: usize, // bytes per scan line
bytes_per_pixel: usize,
bg: u32, // packed BGRA or RGBA background colour
bgr: bool,
}
static mut FB: Option<Framebuffer> = None;
/// Initialise the framebuffer from GOP. Call *before* ExitBootServices.
pub fn init(gop: &mut GraphicsOutputProtocol) {
let mode = unsafe { &*gop.mode };
let info = unsafe { &*mode.info };
let base = mode.framebuffer_base as *mut u8;
let width = info.horizontal_resolution as usize;
let height = info.vertical_resolution as usize;
let pitch = info.pixels_per_scan_line as usize * 4; // 32-bit pixel
let bpp = 4;
// Pick colours based on pixel format
let (fg, bg) = match info.pixel_format {
PIXEL_BGR => (0x00_FF_FF_FFu32, 0x00_00_00_00u32), // white fg, black bg
PIXEL_RGB => (0x00_FF_FF_FFu32, 0x00_00_00_00u32),
_ => (0x00_FF_FF_FFu32, 0x00_00_00_00u32),
};
let bgr = info.pixel_format == PIXEL_BGR;
let _ = fg;
let fb = Framebuffer {
base,
width,
height,
pitch,
bytes_per_pixel: bpp,
bg,
bgr,
};
fb.clear();
unsafe {
FB = Some(fb);
}
}
// ---------------------------------------------------------------------------
// Public text API — the text surface now lives in `crate::vt`; these wrappers
// keep callers (shell, macros) unchanged.
// ---------------------------------------------------------------------------
/// Draw a character at the current terminal cursor position and advance.
pub fn put_char(c: char) {
let b = if (c as u32) < 0x100 { c as u8 } else { b'?' };
crate::vt::put_char(b);
}
/// Write a string to the terminal.
pub fn write_str(s: &str) {
crate::vt::write_str(s);
}
/// Clear the terminal and home the cursor.
pub fn clear_screen() {
crate::vt::clear_all();
}
/// (cols, rows) of the text surface, from the current framebuffer mode.
pub fn dims() -> (usize, usize) {
unsafe {
match FB.as_ref() {
Some(f) => (f.width / 8, f.height / 16),
None => (0, 0),
}
}
}
/// Pack an (r, g, b) colour for the framebuffer's pixel format.
pub fn rgb(r: u8, g: u8, b: u8) -> u32 {
unsafe {
match FB.as_ref() {
Some(f) if f.bgr => ((r as u32) << 16) | ((g as u32) << 8) | b as u32,
_ => ((b as u32) << 16) | ((g as u32) << 8) | r as u32,
}
}
}
// ---------------------------------------------------------------------------
// Framebuffer implementation
// ---------------------------------------------------------------------------
impl Framebuffer {
fn clear(&self) {
let size = self.pitch * self.height;
// Fill with background colour (32-bit writes)
let mut p = self.base;
let end = unsafe { p.add(size) };
unsafe {
while p < end {
ptr::write_volatile(p as *mut u32, self.bg);
p = p.add(4);
}
}
}
/// Shift the whole framebuffer up one row (16 pixels) and clear the
/// exposed bottom row. Called by `vt::scroll_up`; the vt then re-blits
/// only the new bottom row.
pub(crate) fn shift_up(&self) {
// Move everything up one row (16 pixels)
let row_bytes = self.pitch * 16;
let total = self.pitch * self.height;
unsafe {
ptr::copy(self.base.add(row_bytes), self.base, total - row_bytes);
// Clear last row
let last_row = self.base.add(total - row_bytes);
for i in 0..(self.pitch * 16 / 4) {
ptr::write_volatile(last_row.add(i * 4) as *mut u32, self.bg);
}
}
}
}
/// Blit one glyph cell at (col, row) with explicit fg/bg colours.
pub(crate) fn blit_glyph(col: usize, row: usize, ch: u16, fg: (u8, u8, u8), bg: (u8, u8, u8)) {
if let Some(fb) = unsafe { FB.as_ref() } {
let glyph_idx = if ch < 256 { ch as usize } else { 32 };
let glyph = font::GLYPH_8X16;
let data = match glyph.get(glyph_idx) {
Some(d) => *d,
None => return,
};
let fg = rgb(fg.0, fg.1, fg.2);
let bg = rgb(bg.0, bg.1, bg.2);
let x = col * 8;
let y = row * 16;
for cy in 0..16 {
let row_bits = data[cy];
for cx in 0..8 {
let offset = (y + cy) * fb.pitch + (x + cx) * fb.bytes_per_pixel;
unsafe {
let px = if row_bits & (0x80 >> cx) != 0 { fg } else { bg };
ptr::write_volatile(fb.base.add(offset) as *mut u32, px);
}
}
}
}
}
/// Shift the framebuffer up one row and clear the bottom row.
pub(crate) fn shift_up() {
if let Some(fb) = unsafe { FB.as_ref() } {
fb.shift_up();
}
}
// ---------------------------------------------------------------------------
// fmt::Write wrapper
// ---------------------------------------------------------------------------
pub struct GopWriter;
impl fmt::Write for GopWriter {
fn write_str(&mut self, s: &str) -> fmt::Result {
write_str(s);
Ok(())
}
}
/// Write a formatted string to the framebuffer.
#[macro_export]
macro_rules! gop_print {
($($arg:tt)*) => {{
use core::fmt::Write;
let _ = write!(&mut $crate::gop::GopWriter, $($arg)*);
}};
}
#[macro_export]
macro_rules! gop_println {
() => { $crate::gop::write_str("\n"); };
($($arg:tt)*) => {{
$crate::gop_print!($($arg)*);
$crate::gop::write_str("\n");
}};
}
|