Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 62 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- Bump `uefi` from `0.38` to `0.41`
- When booting on UEFI, whatever is written to the UEFI console
(e.g., via the SIMPLE_TEXT_OUTPUT_PROTOCOL) is no longer replicated to the
serial device. The bootloader's own logger now takes full ownership of the
Expand Down Expand Up @@ -272,7 +273,7 @@ See our [migration guides](docs/migration/README.md) for details.
# 0.10.0 – 2021-04-06

- Rewrite for UEFI support ([#130](https://github.com/rust-osdev/bootloader/pull/130))
- Includes a new build process that no longer uses the `bootimage` crate. See the Readme for details.
- Includes a new build process that no longer uses the `bootimage` crate. See the Readme for details.

# 0.9.19 _(backport) – 2021-08-09

Expand Down
9 changes: 5 additions & 4 deletions uefi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ repository.workspace = true
bootloader_api = { workspace = true }
bootloader-x86_64-common = { workspace = true }
bootloader-boot-config = { workspace = true }
log = "0.4.14"
x86_64 = "0.15.2"
serde-json-core = "0.5.0"
uefi = "0.38.0"
log = "0.4.35"
x86_64 = "0.15.5"
serde-json-core = "0.7.0"
uefi = "0.41.0"
uefi-raw = "0.17.0"
125 changes: 125 additions & 0 deletions uefi/src/graphics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use bootloader_api::info::{self, FrameBufferInfo};
use bootloader_boot_config::BootConfig;
use bootloader_x86_64_common::{RawFrameBufferInfo, init_logger};
use core::slice;
use uefi::{
Identify,
boot::{self, OpenProtocolAttributes, OpenProtocolParams, SearchType},
prelude::Handle,
proto::console::{
gop::{GraphicsOutput, PixelFormat},
serial::Serial,
},
};
use x86_64::PhysAddr;

pub fn init_graphic(image_handle: Handle, config: &BootConfig) -> Option<RawFrameBufferInfo> {
let gop_handle = boot::get_handle_for_protocol::<GraphicsOutput>()
.map_err(|e| log::error!("No GOP handle found: {e:?}"))
.ok()?;

let mut gop = unsafe {
boot::open_protocol::<GraphicsOutput>(
OpenProtocolParams {
handle: gop_handle,
agent: image_handle,
controller: None,
},
OpenProtocolAttributes::GetProtocol,
)
.map_err(|e| log::error!("Failed to open GOP: {e:?}"))
.ok()?
};

let mode = {
let min_h = config
.frame_buffer
.minimum_framebuffer_height
.map(|v| usize::try_from(v).unwrap());
let min_w = config
.frame_buffer
.minimum_framebuffer_width
.map(|v| usize::try_from(v).unwrap());

match (min_h, min_w) {
(Some(h), Some(w)) => gop.modes().find(|m| {
let (mw, mh) = m.info().resolution();
mh >= h && mw >= w
}),
(Some(h), None) => gop.modes().find(|m| m.info().resolution().1 >= h),
(None, Some(w)) => gop.modes().find(|m| m.info().resolution().0 >= w),
_ => None,
}
};

if let Some(mode) = mode {
gop.set_mode(&mode).expect("Failed to set GOP display mode");
}

let mode_info = gop.current_mode_info();
let (width, height) = mode_info.resolution();
let stride = mode_info.stride();

let pixel_format = match mode_info.pixel_format() {
PixelFormat::Rgb => info::PixelFormat::Rgb,
PixelFormat::Bgr => info::PixelFormat::Bgr,
PixelFormat::Bitmask | PixelFormat::BltOnly => {
panic!("Bitmask and BltOnly pixel formats are not supported")
}
};

let mut pat = x86_64::registers::model_specific::Msr::new(0x277);
let mut pat_value = unsafe { pat.read() };

let mut wc_slot = None;
for i in 0..8 {
let type_byte = (pat_value >> (i * 8)) & 0xFF;
if type_byte == 0x01 {
wc_slot = Some(i);
break;
}
}

if wc_slot.is_none() {
pat_value = (pat_value & !(0xFF << 32)) | (0x01 << 32);
unsafe { pat.write(pat_value) };
}

let mut framebuffer = gop.frame_buffer();
let fb_size = framebuffer.size();
let bytes_per_pixel = if stride > 0 && height > 0 {
(fb_size / (stride * height)).max(4)
} else {
4
};
let slice = unsafe { slice::from_raw_parts_mut(framebuffer.as_mut_ptr(), fb_size) };

let info = FrameBufferInfo {
byte_len: fb_size,
width,
height,
pixel_format,
bytes_per_pixel,
stride,
};

// Added solely to ensure integration tests pass by detaching the UEFI serial driver from COM1.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this comment changed?

if let Ok(handles) = boot::locate_handle_buffer(SearchType::ByProtocol(&Serial::GUID)) {
for handle in handles.iter() {
let _ = boot::disconnect_controller(*handle, None, None);
}
}

init_logger(
slice,
info,
config.log_level,
config.frame_buffer_logging,
config.serial_logging,
);

Some(RawFrameBufferInfo {
addr: PhysAddr::new(framebuffer.as_mut_ptr() as u64),
info,
})
}
Loading
Loading