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
140 changes: 135 additions & 5 deletions common/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,116 @@
#![no_std]

use serde::{Deserialize, Serialize};
use core::fmt;
use serde::{
Deserialize, Deserializer, Serialize, Serializer,
de::{Error as DeError, SeqAccess, Visitor},
ser::SerializeSeq,
};

/// Maximum number of preferred framebuffer resolutions.
pub const MAX_PREFERRED_RESOLUTIONS: usize = 16;

/// A fixed-capacity list of preferred framebuffer resolutions.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PreferredResolutions {
entries: [(u64, u64); MAX_PREFERRED_RESOLUTIONS],
len: usize,
}

impl PreferredResolutions {
pub fn new() -> Self {
Self::default()
}

pub fn push(&mut self, resolution: (u64, u64)) -> Result<(), (u64, u64)> {
if self.len == MAX_PREFERRED_RESOLUTIONS {
return Err(resolution);
}

self.entries[self.len] = resolution;
self.len += 1;
Ok(())
}

pub fn iter(&self) -> core::slice::Iter<'_, (u64, u64)> {
self.entries[..self.len].iter()
}

pub fn len(&self) -> usize {
self.len
}

pub fn is_empty(&self) -> bool {
self.len == 0
}
}

impl Default for PreferredResolutions {
fn default() -> Self {
Self {
entries: [(0, 0); MAX_PREFERRED_RESOLUTIONS],
len: 0,
}
}
}

impl Serialize for PreferredResolutions {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.len))?;

for resolution in self.iter() {
seq.serialize_element(resolution)?;
}

seq.end()
}
}

impl<'de> Deserialize<'de> for PreferredResolutions {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct PreferredResolutionsVisitor;

impl<'de> Visitor<'de> for PreferredResolutionsVisitor {
type Value = PreferredResolutions;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"a list of at most {} framebuffer resolutions",
MAX_PREFERRED_RESOLUTIONS
)
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut resolutions = PreferredResolutions::new();

while let Some(resolution) = seq.next_element()? {
if resolutions.len == MAX_PREFERRED_RESOLUTIONS {
return Err(A::Error::invalid_length(
MAX_PREFERRED_RESOLUTIONS + 1,
&self,
));
}

resolutions.push(resolution).unwrap();
}

Ok(resolutions)
}
}

deserializer.deserialize_seq(PreferredResolutionsVisitor)
}
}

/// Configures the boot behavior of the bootloader.
#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -43,16 +153,36 @@ 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.
/// Minimum framebuffer height required.
///
/// If this is not possible, the bootloader will fall back to a smaller format.
/// Modes below this height will not be selected.
pub minimum_framebuffer_height: Option<u64>,
/// Instructs the bootloader to set up a framebuffer format that has at least the given width.

/// Minimum framebuffer width required.
///
/// If this is not possible, the bootloader will fall back to a smaller format.
/// Modes below this width will not be selected.
pub minimum_framebuffer_width: Option<u64>,

/// Maximum framebuffer height allowed.
///
/// Modes above this height will not be selected.
pub maximum_framebuffer_height: Option<u64>,

/// Maximum framebuffer width allowed.
///
/// Modes above this width will not be selected.
pub maximum_framebuffer_width: Option<u64>,

/// Preferred framebuffer resolutions, ordered from most to least preferred.
///
/// The bootloader selects the first supported resolution satisfying
/// the configured minimum and maximum dimensions. If none match, it
/// selects the largest supported resolution satisfying those constraints.
/// At most [`MAX_PREFERRED_RESOLUTIONS`] entries are supported.
pub preferred_framebuffer_resolutions: PreferredResolutions,
}

/// An enum representing the available verbosity level filters of the logger.
Expand Down
75 changes: 46 additions & 29 deletions uefi/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#![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;

Check warning on line 5 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `crate::memory_descriptor::UefiMemoryDescriptor`

Check warning on line 5 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `crate::memory_descriptor::UefiMemoryDescriptor`
use bootloader_api::info::FrameBufferInfo;
use bootloader_boot_config::BootConfig;
use bootloader_x86_64_common::{
Kernel, RawFrameBufferInfo, SystemInfo, legacy_memory_region::LegacyFrameAllocator,

Check warning on line 9 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused imports: `SystemInfo` and `legacy_memory_region::LegacyFrameAllocator`

Check warning on line 9 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

unused imports: `SystemInfo` and `legacy_memory_region::LegacyFrameAllocator`
};
use core::net::Ipv4Addr;
use core::{ptr, slice};
use uefi::boot::SearchType;
use uefi::mem::memory_map::{MemoryMap, MemoryMapMut};

Check warning on line 14 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused imports: `MemoryMapMut` and `MemoryMap`

Check warning on line 14 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

unused imports: `MemoryMapMut` and `MemoryMap`
use uefi::proto::console::serial::Serial;
use uefi::table::cfg::ConfigTableEntry;

Check warning on line 16 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `uefi::table::cfg::ConfigTableEntry`

Check warning on line 16 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `uefi::table::cfg::ConfigTableEntry`
use uefi::{
CStr8, CStr16, Identify, boot,
boot::{AllocateType, MemoryType},
cstr8, cstr16,
prelude::{Status, entry},

Check warning on line 21 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused imports: `Status` and `entry`

Check warning on line 21 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

unused imports: `Status` and `entry`
proto::{
ProtocolPointer,
console::gop::{GraphicsOutput, PixelFormat},
Expand All @@ -34,25 +34,29 @@
};

mod memory_descriptor;
mod mode_selection;

use mode_selection::select_framebuffer_resolution;

struct BootFile {

Check warning on line 41 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

struct `BootFile` is never constructed

Check warning on line 41 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

struct `BootFile` is never constructed
disk: &'static CStr16,
tftp: &'static CStr8,
}

const KERNEL_FILE: BootFile = BootFile {

Check warning on line 46 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

constant `KERNEL_FILE` is never used

Check warning on line 46 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

constant `KERNEL_FILE` is never used
disk: cstr16!("kernel-x86_64"),
tftp: cstr8!("kernel-x86_64"),
};
const CONFIG_FILE: BootFile = BootFile {

Check warning on line 50 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

constant `CONFIG_FILE` is never used

Check warning on line 50 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

constant `CONFIG_FILE` is never used
disk: cstr16!("boot.json"),
tftp: cstr8!("boot.json"),
};
const RAMDISK_FILE: BootFile = BootFile {

Check warning on line 54 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Clippy

constant `RAMDISK_FILE` is never used

Check warning on line 54 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

constant `RAMDISK_FILE` is never used
disk: cstr16!("ramdisk"),
tftp: cstr8!("ramdisk"),
};

#[cfg(not(test))]
#[entry]
fn main() -> Status {
let mut boot_mode = BootMode::Disk;
Expand Down Expand Up @@ -168,7 +172,7 @@
Tftp,
}

fn load_ramdisk(boot_mode: BootMode) -> Option<&'static mut [u8]> {

Check warning on line 175 in uefi/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

function `load_ramdisk` is never used
load_file_from_boot_method(&RAMDISK_FILE, boot_mode)
}

Expand Down Expand Up @@ -238,7 +242,7 @@
}

fn allocate_loader_data(size: usize) -> &'static mut [u8] {
let mut ptr = boot::allocate_pages(
let ptr = boot::allocate_pages(
AllocateType::AnyPages,
MemoryType::LOADER_DATA,
((size - 1) / 4096) + 1,
Expand Down Expand Up @@ -351,33 +355,46 @@
let gop_handle = boot::get_handle_for_protocol::<GraphicsOutput>().ok()?;
let mut gop = boot::open_protocol_exclusive::<GraphicsOutput>(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_resolutions
.iter()
.map(|(width, height)| {
(
usize::try_from(*width).unwrap(),
usize::try_from(*height).unwrap(),
)
}),
);
let Some(selected_resolution) = selected_resolution else {
log::warn!("No framebuffer resolution satisfies the configured constraints");
return None;
};
if let Some(mode) = mode {
gop.set_mode(&mode)
.expect("Failed to apply the desired display mode");
}

let mode = gop
.modes()
.find(|mode| mode.info().resolution() == selected_resolution)?;

gop.set_mode(&mode)
.expect("Failed to apply the desired display mode");

let mode_info = gop.current_mode_info();
let mut framebuffer = gop.frame_buffer();
Expand Down
Loading
Loading