Skip to main content

proka_bootloader/
lib.rs

1//! This crate provides the struct, enums about the Proka
2//! bootloader, including the boot information, and so on.
3//! 
4//! # About proka bootloader
5//! Well, this bootloader is for Proka Kernel, which will obey
6//! its standard. For more information, see <url>.
7
8#![no_std]
9#![no_main]
10#![feature(custom_test_frameworks)]
11#![test_runner(self::test_runner)]
12#![reexport_test_harness_main = "test_main"]
13
14#[cfg(feature = "loader_main")]
15pub mod loader_main;
16pub mod output;
17pub mod memory;
18use self::output::Framebuffer;
19use self::memory::MemoryMap;
20
21/// This struct is the boot information struct, which provides
22/// the basic information, *memory map*, and so on.
23#[repr(C, packed)]
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub struct BootInfo {
26    /// The boot mode, see the [`BootMode`] enum.
27    boot_mode: BootMode,
28    framebuffer: Framebuffer,
29    memmap: MemoryMap,
30}
31
32impl BootInfo {
33    /// Initialize a new boot info object.
34    ///
35    /// Note: this object will be initialized by loader
36    /// automatically, so if you are a kernel developer, do
37    /// not use this method, because you needn't and unusable.
38    #[cfg(feature = "loader_main")]
39    pub fn new(boot_mode: BootMode, memmap: MemoryMap, fb: Framebuffer) -> Self {
40        Self {
41            boot_mode,
42            memmap,
43            framebuffer: fb
44        }
45    }
46
47    /// Get the boot mode.
48    pub const fn boot_mode(&self) -> BootMode {
49        self.boot_mode
50    }
51
52    /// Get the framebuffer info.
53    pub const fn framebuffer(&self) -> Framebuffer {
54        self.framebuffer
55    }
56
57    /// Get the memory map.
58    pub const fn memory(&self) -> MemoryMap {
59        self.memmap
60    }
61}
62
63/// This is the boot mode, only support 2 modes, which are legacy(BIOS) and UEFI.
64#[repr(C)]
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub enum BootMode {
67    /// The Legacy boot mode, also called BIOS boot mode.
68    ///
69    /// This mode is for older machine, and we needs implement
70    /// lots of things in it.
71    Legacy,
72
73    /// The UEFI boot mode, which is the newer mode. Lots of
74    /// new machines uses it.
75    ///
76    /// Also, some machine only support it (such as mine awa).
77    Uefi,
78}
79
80/// Get the bootinfo.
81///
82/// The BootInfo is pre-copied & fixed at the dedicated constant physical address
83/// 0x10000 by UEFI boot stage, never modified nor released in kernel lifetime.
84///
85/// # Safety
86/// Caller must ensure **before invoking**:
87/// 1. Address `0x10000` is allocated & filled with valid initialized BootInfo;
88/// 2. This range is reserved, never overwritten/freed by kernel/UEFI;
89/// 3. No mutable aliasing exists for this memory region.
90///
91/// These steps are already guaranteed by the bootloader, so invocation is generally safe
92/// in normal kernel runtime.
93///
94/// # Returns
95/// - &'static BootInfo: immutable static reference to the pre-filled BootInfo
96pub const fn get_bootinfo() -> &'static BootInfo {
97    const BI_PHYS: u64 = 0x10000;
98    unsafe { &*(BI_PHYS as *const BootInfo) }
99}
100
101#[cfg(test)]
102fn test_runner(tests: &[&dyn Fn()]) {
103    for test in tests {
104        test();
105    }
106}