Skip to content

Add support for a configuration file #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = "2021"
members = [
"api",
"common",
"common/config",
"uefi",
"bios/boot_sector",
"bios/stage-*",
Expand All @@ -36,6 +37,7 @@ repository = "https://github.com/rust-osdev/bootloader"
[workspace.dependencies]
bootloader_api = { version = "0.11.0", path = "api" }
bootloader-x86_64-common = { version = "0.11.0", path = "common" }
bootloader-boot-config = { version = "0.11.0", path = "common/config" }
bootloader-x86_64-bios-common = { version = "0.11.0", path = "bios/common" }

[features]
Expand All @@ -49,7 +51,7 @@ fatfs = "0.3.4"
tempfile = "3.3.0"
mbrman = { version = "0.5.1", optional = true }
gpt = { version = "3.0.0", optional = true }
bootloader-x86_64-common = { version = "0.11.0", path = "common" }
bootloader-boot-config = { version = "0.11.0", path = "common/config" }
serde_json = "1.0.91"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct BootloaderConfig {
/// on the screen.
#[deprecated(
since = "0.11.1",
note = "This field is being obsolete because now it's at the JSON configuration file"
note = "The frame buffer is now configured through the `BootConfig` struct when creating the bootable disk image"
)]
pub frame_buffer: FrameBuffer,
}
Expand Down
1 change: 1 addition & 0 deletions bios/stage-4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description = "Fourth BIOS stage of the `bootloader` crate"
bootloader_api = { workspace = true }
bootloader-x86_64-common = { workspace = true }
bootloader-x86_64-bios-common = { workspace = true }
bootloader-boot-config = { workspace = true }
log = "0.4.14"
x86_64 = "0.14.8"
rsdp = "2.0.0"
Expand Down
13 changes: 7 additions & 6 deletions bios/stage-4/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#![no_std]
#![no_main]
#![allow(deprecated)]

use crate::memory_descriptor::MemoryRegion;
use bootloader_api::info::{FrameBufferInfo, PixelFormat};
use bootloader_boot_config::{BootConfig, LevelFilter};
use bootloader_x86_64_bios_common::{BiosFramebufferInfo, BiosInfo, E820MemoryRegion};
use bootloader_x86_64_common::RawFrameBufferInfo;
use bootloader_x86_64_common::{
config::{BootConfig, LevelFilter},
legacy_memory_region::LegacyFrameAllocator,
load_and_switch_to_kernel, Kernel, PageTables, SystemInfo,
legacy_memory_region::LegacyFrameAllocator, load_and_switch_to_kernel, Kernel, PageTables,
SystemInfo,
};
use core::{cmp, slice};
use usize_conversions::usize_from;
Expand Down Expand Up @@ -136,19 +135,21 @@ pub extern "C" fn _start(info: &mut BiosInfo) -> ! {
}
};

#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_height.is_none() {
config.frame_buffer.minimum_framebuffer_height =
kernel.config.frame_buffer.minimum_framebuffer_height;
}
#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_width.is_none() {
config.frame_buffer.minimum_framebuffer_width =
kernel.config.frame_buffer.minimum_framebuffer_width;
}
let framebuffer_info = init_logger(
info.framebuffer,
config.log_level,
config.frame_buffer_logger_status,
config.serial_logger_status,
config.frame_buffer_logging,
config.serial_logging,
);

if let Some(err) = error_loading_config {
Expand Down
3 changes: 1 addition & 2 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository.workspace = true

[dependencies]
bootloader_api = { workspace = true }
bootloader-boot-config = { workspace = true }
conquer-once = { version = "0.3.2", default-features = false }
spinning_top = "0.2.4"
usize_conversions = "0.2.0"
Expand All @@ -19,8 +20,6 @@ raw-cpuid = "10.2.0"
rand = { version = "0.8.4", default-features = false }
rand_hc = "0.3.1"
uart_16550 = "0.2.18"
serde-json-core = "0.5.0"
serde = { version = "1.0.152", default-features = false, features = ["derive"] }
log = "0.4.17"

[dependencies.noto-sans-mono-bitmap]
Expand Down
10 changes: 10 additions & 0 deletions common/config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "bootloader-boot-config"
version.workspace = true
edition = "2021"
description = "The runtime configurations that are saved in a JSON file for the bootloader crate"
license.workspace = true
repository.workspace = true

[dependencies]
serde = { version = "1.0.152", default-features = false, features = ["derive"] }
19 changes: 5 additions & 14 deletions common/src/config.rs → common/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![no_std]

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Default)]
pub struct BootConfig {
/// Configuration for the frame buffer that can be used by the kernel to display pixels
/// on the screen.
Expand All @@ -16,24 +18,13 @@ pub struct BootConfig {
///
/// Enabled by default.
#[serde(default = "default_logger_status")]
pub frame_buffer_logger_status: bool,
pub frame_buffer_logging: bool,

/// Whether the bootloader should print log messages to the serial port when booting.
///
/// Enabled by default.
#[serde(default = "default_logger_status")]
pub serial_logger_status: bool,
}

impl Default for BootConfig {
fn default() -> Self {
Self {
frame_buffer: Default::default(),
log_level: Default::default(),
frame_buffer_logger_status: true,
serial_logger_status: true,
}
}
pub serial_logging: bool,
}

/// Configuration for the frame buffer used for graphical output.
Expand Down
8 changes: 2 additions & 6 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
#![feature(step_trait)]
#![deny(unsafe_op_in_unsafe_fn)]

use crate::{
config::LevelFilter,
legacy_memory_region::{LegacyFrameAllocator, LegacyMemoryRegion},
};
use crate::legacy_memory_region::{LegacyFrameAllocator, LegacyMemoryRegion};
use bootloader_api::{
config::Mapping,
info::{FrameBuffer, FrameBufferInfo, MemoryRegion, TlsTemplate},
BootInfo, BootloaderConfig,
};
use bootloader_boot_config::LevelFilter;
use core::{alloc::Layout, arch::asm, mem::MaybeUninit, slice};
use level_4_entries::UsedLevel4Entries;
use usize_conversions::FromUsize;
Expand All @@ -23,8 +21,6 @@ use x86_64::{
};
use xmas_elf::ElfFile;

/// Provides a type with the runtime configurations that are saved in a JSON file.
pub mod config;
/// Provides a function to gather entropy and build a RNG.
mod entropy;
/// Provides a type that logs output as text to pixel-based framebuffers.
Expand Down
2 changes: 1 addition & 1 deletion src/bios/mbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn create_mbr_disk(
assert_eq!(
disk.stream_position()
.context("failed to get disk image seek position")?,
<u32 as Into<u64>>::into(second_stage_start_sector * SECTOR_SIZE)
u64::from(second_stage_start_sector * SECTOR_SIZE)
);
io::copy(&mut second_stage, &mut disk)
.context("failed to copy second stage binary to MBR disk image")?;
Expand Down
8 changes: 4 additions & 4 deletions src/bios/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::fat;
use anyhow::Context;
use bootloader_x86_64_common::config::BootConfig;
use bootloader_boot_config::BootConfig;
use std::io::Write;
use std::{
collections::BTreeMap,
Expand Down Expand Up @@ -30,14 +30,14 @@ impl BiosBoot {
}
}

/// Add a ramdisk file to the image
/// Add a ramdisk file to the image.
pub fn set_ramdisk(&mut self, ramdisk_path: &Path) -> &mut Self {
self.ramdisk = Some(ramdisk_path.to_owned());
self
}

/// Add a JSON configuration file to the disk image
pub fn set_config_file(&mut self, config: &BootConfig) -> &mut Self {
/// Configures the runtime behavior of the bootloader.
pub fn set_boot_config(&mut self, config: &BootConfig) -> &mut Self {
self.config = Some(serde_json::to_string(&config).expect("failed to serialize BootConfig"));
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use bios::BiosBoot;
#[cfg(feature = "uefi")]
pub use uefi::UefiBoot;

pub use bootloader_x86_64_common::config::BootConfig;
pub use bootloader_boot_config::BootConfig;

const KERNEL_FILE_NAME: &str = "kernel-x86_64";
const RAMDISK_FILE_NAME: &str = "ramdisk";
Expand Down
8 changes: 4 additions & 4 deletions src/uefi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::fat;
use anyhow::Context;
use bootloader_x86_64_common::config::BootConfig;
use bootloader_boot_config::BootConfig;
use std::io::Write;
use std::{
collections::BTreeMap,
Expand Down Expand Up @@ -28,14 +28,14 @@ impl UefiBoot {
}
}

/// Add a ramdisk file to the disk image
/// Add a ramdisk file to the disk image.
pub fn set_ramdisk(&mut self, ramdisk_path: &Path) -> &mut Self {
self.ramdisk = Some(ramdisk_path.to_owned());
self
}

/// Add a JSON configuration file to the disk image
pub fn set_config_file(&mut self, config: &BootConfig) -> &mut Self {
/// Configures the runtime behavior of the bootloader.
pub fn set_boot_config(&mut self, config: &BootConfig) -> &mut Self {
self.config = Some(serde_json::to_string(&config).expect("failed to serialize BootConfig"));
self
}
Expand Down
4 changes: 2 additions & 2 deletions tests/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fn custom_options_boot() {
let config = BootConfig {
frame_buffer: Default::default(),
log_level: Default::default(),
frame_buffer_logger_status: false,
serial_logger_status: true,
frame_buffer_logging: false,
serial_logging: true,
};
run_test_kernel_internal(
env!("CARGO_BIN_FILE_TEST_KERNEL_CONFIG_FILE_basic_boot_broken_config_file"),
Expand Down
4 changes: 2 additions & 2 deletions tests/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn run_test_kernel_internal(
uefi_builder.set_ramdisk(rdp);
}
if let Some(cfp) = config_file_path {
uefi_builder.set_config_file(cfp);
uefi_builder.set_boot_config(cfp);
}
uefi_builder.create_disk_image(&gpt_path).unwrap();

Expand All @@ -64,7 +64,7 @@ pub fn run_test_kernel_internal(
bios_builder.set_ramdisk(rdp);
}
if let Some(cfp) = config_file_path {
bios_builder.set_config_file(cfp);
bios_builder.set_boot_config(cfp);
}
bios_builder.create_disk_image(&mbr_path).unwrap();

Expand Down
1 change: 1 addition & 0 deletions uefi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository.workspace = true
[dependencies]
bootloader_api = { workspace = true }
bootloader-x86_64-common = { workspace = true }
bootloader-boot-config = { workspace = true }
log = "0.4.14"
uefi = "0.18.0"
x86_64 = "0.14.8"
Expand Down
22 changes: 11 additions & 11 deletions uefi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#![no_main]
#![feature(abi_efiapi)]
#![deny(unsafe_op_in_unsafe_fn)]
#![allow(deprecated)]

use crate::memory_descriptor::UefiMemoryDescriptor;
use bootloader_api::info::FrameBufferInfo;
use bootloader_boot_config::BootConfig;
use bootloader_x86_64_common::{
config::BootConfig, legacy_memory_region::LegacyFrameAllocator, Kernel, RawFrameBufferInfo,
SystemInfo,
legacy_memory_region::LegacyFrameAllocator, Kernel, RawFrameBufferInfo, SystemInfo,
};
use core::{
cell::UnsafeCell,
Expand Down Expand Up @@ -76,13 +75,12 @@ fn main_inner(image: Handle, mut st: SystemTable<Boot>) -> Status {

let mut boot_mode = BootMode::Disk;
let config_file = load_config_file(image, &mut st, boot_mode);
let config_file: Option<&[u8]> = match config_file {
Some(config) => Some(config),
None => None,
};

let mut error_loading_config: Option<serde_json_core::de::Error> = None;
let mut config: BootConfig = match config_file.map(serde_json_core::from_slice).transpose() {
let mut config: BootConfig = match config_file
.as_deref()
.map(serde_json_core::from_slice)
.transpose()
{
Ok(data) => data.unwrap_or_default().0,
Err(err) => {
error_loading_config = Some(err);
Expand All @@ -98,10 +96,12 @@ fn main_inner(image: Handle, mut st: SystemTable<Boot>) -> Status {
}
let kernel = kernel.expect("Failed to load kernel");

#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_height.is_none() {
config.frame_buffer.minimum_framebuffer_height =
kernel.config.frame_buffer.minimum_framebuffer_height;
}
#[allow(deprecated)]
if config.frame_buffer.minimum_framebuffer_width.is_none() {
config.frame_buffer.minimum_framebuffer_width =
kernel.config.frame_buffer.minimum_framebuffer_width;
Expand Down Expand Up @@ -545,8 +545,8 @@ fn init_logger(
slice,
info,
config.log_level,
config.frame_buffer_logger_status,
config.serial_logger_status,
config.frame_buffer_logging,
config.serial_logging,
);

Some(RawFrameBufferInfo {
Expand Down