diff --git a/common/config/src/lib.rs b/common/config/src/lib.rs index 2f0da026..41a1b19b 100644 --- a/common/config/src/lib.rs +++ b/common/config/src/lib.rs @@ -43,16 +43,31 @@ impl Default for BootConfig { /// Configuration for the frame buffer used for graphical output. #[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq, Clone, Copy)] +#[serde(default)] #[non_exhaustive] pub struct FrameBuffer { - /// Instructs the bootloader to set up a framebuffer format that has at least the given height. + /// Instructs the UEFI bootloader to prefer a framebuffer at least this high. /// - /// If this is not possible, the bootloader will fall back to a smaller format. + /// If no available mode meets the minimum, the best available mode within the + /// configured maximum dimensions is used instead. pub minimum_framebuffer_height: Option, - /// Instructs the bootloader to set up a framebuffer format that has at least the given width. + /// Instructs the UEFI bootloader to prefer a framebuffer at least this wide. /// - /// If this is not possible, the bootloader will fall back to a smaller format. + /// If no available mode meets the minimum, the best available mode within the + /// configured maximum dimensions is used instead. pub minimum_framebuffer_width: Option, + /// Preferred framebuffer height. Defaults to 800 pixels when unset. + /// + /// Minimum and maximum constraints take precedence over this preference. + pub preferred_framebuffer_height: Option, + /// Preferred framebuffer width. Defaults to 1280 pixels when unset. + /// + /// Minimum and maximum constraints take precedence over this preference. + pub preferred_framebuffer_width: Option, + /// Maximum framebuffer height. Modes larger than this are not selected. + pub maximum_framebuffer_height: Option, + /// Maximum framebuffer width. Modes larger than this are not selected. + pub maximum_framebuffer_width: Option, } /// An enum representing the available verbosity level filters of the logger. diff --git a/uefi/src/main.rs b/uefi/src/main.rs index 928a290d..db53a389 100644 --- a/uefi/src/main.rs +++ b/uefi/src/main.rs @@ -1,5 +1,5 @@ -#![no_std] -#![no_main] +#![cfg_attr(not(test), no_std)] +#![cfg_attr(not(test), no_main)] #![deny(unsafe_op_in_unsafe_fn)] use crate::memory_descriptor::UefiMemoryDescriptor; @@ -34,6 +34,9 @@ use x86_64::{ }; mod memory_descriptor; +mod mode_selection; + +use mode_selection::select_framebuffer_resolution; struct BootFile { disk: &'static CStr16, @@ -53,6 +56,7 @@ const RAMDISK_FILE: BootFile = BootFile { tftp: cstr8!("ramdisk"), }; +#[cfg(not(test))] #[entry] fn main() -> Status { let mut boot_mode = BootMode::Disk; @@ -351,29 +355,37 @@ fn init_logger(config: &BootConfig) -> Option { let gop_handle = boot::get_handle_for_protocol::().ok()?; let mut gop = boot::open_protocol_exclusive::(gop_handle).ok()?; - let mode = { - let modes = gop.modes(); - match ( - config - .frame_buffer - .minimum_framebuffer_height - .map(|v| usize::try_from(v).unwrap()), - config - .frame_buffer - .minimum_framebuffer_width - .map(|v| usize::try_from(v).unwrap()), - ) { - (Some(height), Some(width)) => modes - .filter(|m| { - let res = m.info().resolution(); - res.1 >= height && res.0 >= width - }) - .last(), - (Some(height), None) => modes.filter(|m| m.info().resolution().1 >= height).last(), - (None, Some(width)) => modes.filter(|m| m.info().resolution().0 >= width).last(), - _ => None, - } - }; + let selected_resolution = select_framebuffer_resolution( + gop.modes().map(|mode| mode.info().resolution()), + config + .frame_buffer + .minimum_framebuffer_width + .map(|width| usize::try_from(width).unwrap()), + config + .frame_buffer + .minimum_framebuffer_height + .map(|height| usize::try_from(height).unwrap()), + config + .frame_buffer + .maximum_framebuffer_width + .map(|width| usize::try_from(width).unwrap()), + config + .frame_buffer + .maximum_framebuffer_height + .map(|height| usize::try_from(height).unwrap()), + config + .frame_buffer + .preferred_framebuffer_width + .map(|width| usize::try_from(width).unwrap()), + config + .frame_buffer + .preferred_framebuffer_height + .map(|height| usize::try_from(height).unwrap()), + ); + let mode = selected_resolution.and_then(|resolution| { + gop.modes() + .find(|mode| mode.info().resolution() == resolution) + }); if let Some(mode) = mode { gop.set_mode(&mode) .expect("Failed to apply the desired display mode"); diff --git a/uefi/src/mode_selection.rs b/uefi/src/mode_selection.rs new file mode 100644 index 00000000..03f5b490 --- /dev/null +++ b/uefi/src/mode_selection.rs @@ -0,0 +1,148 @@ +//! Resolution selection for the UEFI Graphics Output Protocol. + +const DEFAULT_PREFERRED_WIDTH: usize = 1280; +const DEFAULT_PREFERRED_HEIGHT: usize = 800; + +/// Selects the available resolution that best matches the configured constraints. +/// +/// Minimum dimensions are hard lower bounds when a matching mode exists. Maximum +/// dimensions are hard upper bounds. If no mode satisfies the minimums, the +/// highest-resolution mode within the maximum dimensions is used as a fallback. +pub(crate) fn select_framebuffer_resolution( + resolutions: impl IntoIterator, + minimum_width: Option, + minimum_height: Option, + maximum_width: Option, + maximum_height: Option, + preferred_width: Option, + preferred_height: Option, +) -> Option<(usize, usize)> { + let target_width = preferred_width + .or(minimum_width) + .unwrap_or(DEFAULT_PREFERRED_WIDTH); + let target_height = preferred_height + .or(minimum_height) + .unwrap_or(DEFAULT_PREFERRED_HEIGHT); + + let mut best_fit: Option<((u128, u128, usize, usize), (usize, usize))> = None; + let mut fallback: Option<((u128, usize, usize), (usize, usize))> = None; + + for (width, height) in resolutions { + if maximum_width.is_some_and(|maximum| width > maximum) + || maximum_height.is_some_and(|maximum| height > maximum) + { + continue; + } + + let area = (width as u128) * (height as u128); + let fallback_key = (area, width, height); + if fallback + .as_ref() + .map_or(true, |(best_key, _)| fallback_key > *best_key) + { + fallback = Some((fallback_key, (width, height))); + } + + let meets_minimums = minimum_width.map_or(true, |minimum| width >= minimum) + && minimum_height.map_or(true, |minimum| height >= minimum); + if !meets_minimums { + continue; + } + + let width_delta = width.abs_diff(target_width) as u128; + let height_delta = height.abs_diff(target_height) as u128; + let distance = width_delta * width_delta + height_delta * height_delta; + let score = (distance, area, width, height); + if best_fit + .as_ref() + .map_or(true, |(best_score, _)| score < *best_score) + { + best_fit = Some((score, (width, height))); + } + } + + best_fit + .map(|(_, resolution)| resolution) + .or_else(|| fallback.map(|(_, resolution)| resolution)) +} + +#[cfg(test)] +mod tests { + use super::select_framebuffer_resolution; + + #[test] + fn defaults_to_a_reasonable_resolution_instead_of_firmware_order() { + let modes = [(800, 600), (3840, 2160), (1280, 800), (1920, 1080)]; + + assert_eq!( + select_framebuffer_resolution(modes, None, None, None, None, None, None), + Some((1280, 800)) + ); + } + + #[test] + fn prefers_the_closest_mode_that_meets_minimum_dimensions() { + let modes = [(2560, 1600), (1920, 1200), (1920, 1080), (3840, 2160)]; + + assert_eq!( + select_framebuffer_resolution(modes, Some(1920), Some(1080), None, None, None, None), + Some((1920, 1080)) + ); + } + + #[test] + fn preferred_dimensions_are_used_as_the_target() { + let modes = [(1280, 800), (1920, 1080), (2560, 1440)]; + + assert_eq!( + select_framebuffer_resolution(modes, None, None, None, None, Some(1920), Some(1080)), + Some((1920, 1080)) + ); + } + + #[test] + fn maximum_dimensions_are_never_exceeded() { + let modes = [(1280, 800), (1920, 1080), (2560, 1600)]; + + assert_eq!( + select_framebuffer_resolution( + modes, + None, + None, + Some(1920), + Some(1080), + Some(1920), + Some(1080) + ), + Some((1920, 1080)) + ); + } + + #[test] + fn falls_back_to_the_largest_mode_within_maximum_dimensions() { + let modes = [(800, 600), (1280, 800), (1920, 1080), (2560, 1440)]; + + assert_eq!( + select_framebuffer_resolution( + modes, + Some(3840), + Some(2160), + Some(1920), + Some(1080), + None, + None + ), + Some((1920, 1080)) + ); + } + + #[test] + fn returns_none_when_no_mode_satisfies_maximum_dimensions() { + let modes = [(1920, 1080), (2560, 1440)]; + + assert_eq!( + select_framebuffer_resolution(modes, None, None, Some(1280), Some(800), None, None), + None + ); + } +}