Skip to content

Commit 5856491

Browse files
committed
Detect actual pixel format instead of hardcoding it (BIOS)
1 parent b7ff05b commit 5856491

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/bin/bios.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
#[cfg(not(target_os = "none"))]
99
compile_error!("The bootloader crate must be compiled for the `x86_64-bootloader.json` target");
1010

11-
use bootloader::{binary::SystemInfo, boot_info::FrameBufferInfo};
11+
use bootloader::{
12+
binary::SystemInfo,
13+
boot_info::{FrameBufferInfo, PixelFormat},
14+
};
1215
use core::panic::PanicInfo;
1316
use core::slice;
1417
use usize_conversions::usize_from;
@@ -31,7 +34,9 @@ extern "C" {
3134
static VBEModeInfo_xresolution: u16;
3235
static VBEModeInfo_yresolution: u16;
3336
static VBEModeInfo_bitsperpixel: u8;
34-
// TODO color values (e.g. RGB or BGR)
37+
static VBEModeInfo_redfieldposition: u8;
38+
static VBEModeInfo_greenfieldposition: u8;
39+
static VBEModeInfo_bluefieldposition: u8;
3540
}
3641

3742
// Symbols defined in `linker.ld`
@@ -116,6 +121,7 @@ fn bootloader_main(
116121
}
117122

118123
let framebuffer_addr = PhysAddr::new(unsafe { VBEModeInfo_physbaseptr }.into());
124+
let mut error = None;
119125
let framebuffer_info = unsafe {
120126
let framebuffer_size =
121127
usize::from(VBEModeInfo_yresolution) * usize::from(VBEModeInfo_bytesperscanline);
@@ -127,9 +133,27 @@ fn bootloader_main(
127133
VBEModeInfo_yresolution.into(),
128134
bytes_per_pixel.into(),
129135
(VBEModeInfo_bytesperscanline / u16::from(bytes_per_pixel)).into(),
136+
match (
137+
VBEModeInfo_redfieldposition,
138+
VBEModeInfo_greenfieldposition,
139+
VBEModeInfo_bluefieldposition,
140+
) {
141+
(0, 8, 16) => PixelFormat::RGB,
142+
(16, 8, 0) => PixelFormat::BGR,
143+
(r, g, b) => {
144+
error = Some(("invalid rgb field positions", r, g, b));
145+
PixelFormat::RGB // default to RBG so that we can print something
146+
}
147+
},
130148
)
131149
};
132150

151+
log::info!("BIOS boot");
152+
153+
if let Some((msg, r, g, b)) = error {
154+
panic!("{}: r: {}, g: {}, b: {}", msg, r, g, b);
155+
}
156+
133157
let page_tables = create_page_tables(&mut frame_allocator);
134158

135159
let kernel = {
@@ -158,17 +182,18 @@ fn init_logger(
158182
vertical_resolution: usize,
159183
bytes_per_pixel: usize,
160184
stride: usize,
185+
pixel_format: PixelFormat,
161186
) -> FrameBufferInfo {
162187
let ptr = framebuffer_start.as_u64() as *mut u8;
163188
let slice = unsafe { slice::from_raw_parts_mut(ptr, framebuffer_size) };
164-
slice.fill(0xff);
189+
165190
let info = bootloader::boot_info::FrameBufferInfo {
166191
byte_len: framebuffer_size,
167192
horizontal_resolution,
168193
vertical_resolution,
169194
bytes_per_pixel,
170195
stride,
171-
pixel_format: bootloader::boot_info::PixelFormat::RGB, // TODO: don't hardcode
196+
pixel_format,
172197
};
173198

174199
bootloader::binary::init_logger(slice, info);

src/bin/uefi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ fn init_logger(st: &SystemTable<Boot>) -> (PhysAddr, FrameBufferInfo) {
164164
stride: mode_info.stride(),
165165
};
166166

167+
log::info!("UEFI boot");
168+
167169
bootloader::binary::init_logger(slice, info);
168170

169171
(PhysAddr::new(framebuffer.as_mut_ptr() as u64), info)

0 commit comments

Comments
 (0)