pie_boot/
lib.rs

1#![cfg_attr(target_os = "none", no_std)]
2#![cfg(target_os = "none")]
3
4#[cfg(target_arch = "aarch64")]
5#[path = "arch/aarch64/mod.rs"]
6mod arch;
7
8mod fdt;
9mod loader;
10mod staticcell;
11
12use heapless::Vec;
13pub use kdef_pgtable::{KIMAGE_VADDR, KLINER_OFFSET};
14use pie_boot_if::EarlyBootArgs;
15pub use pie_boot_if::{BootInfo, MemoryRegion, MemoryRegionKind, MemoryRegions};
16pub use pie_boot_macros::entry;
17#[allow(unused)]
18use pie_boot_macros::start_code;
19use staticcell::StaticCell;
20
21#[allow(unused)]
22static mut BOOT_ARGS: EarlyBootArgs = EarlyBootArgs::new();
23
24#[unsafe(link_section = ".data")]
25static BOOT_INFO: StaticCell<BootInfo> = StaticCell::new(BootInfo::new());
26#[unsafe(link_section = ".data")]
27static MEMORY_REGIONS: StaticCell<Vec<MemoryRegion, 128>> = StaticCell::new(Vec::new());
28
29unsafe extern "Rust" {
30    fn __pie_boot_main(args: &BootInfo);
31}
32
33fn virt_entry(args: &BootInfo) {
34    unsafe {
35        MEMORY_REGIONS.as_mut().clear();
36        let _ = MEMORY_REGIONS
37            .as_mut()
38            .extend_from_slice(&args.memory_regions);
39
40        *BOOT_INFO.as_mut() = args.clone();
41
42        if let Some(ptr) = BOOT_INFO.fdt {
43            fdt::setup(ptr);
44        }
45
46        if let Some(r) = mainmem_start_rsv(args) {
47            let _ = MEMORY_REGIONS.as_mut().push(r);
48        }
49        let regions = core::slice::from_raw_parts_mut(
50            MEMORY_REGIONS.as_mut().as_mut_ptr(),
51            MEMORY_REGIONS.len(),
52        );
53        BOOT_INFO.as_mut().memory_regions = regions.into();
54
55        __pie_boot_main(&BOOT_INFO);
56    }
57}
58
59pub fn boot_info() -> &'static BootInfo {
60    &BOOT_INFO
61}
62
63fn mainmem_start_rsv(args: &BootInfo) -> Option<MemoryRegion> {
64    let lma = args.kimage_start_lma as usize;
65
66    let mainmem = MEMORY_REGIONS.iter().find(|r| {
67        let is_ram = matches!(r.kind, MemoryRegionKind::Ram);
68        let in_range = r.start <= lma && r.end > lma;
69        is_ram && in_range
70    })?;
71
72    let start = mainmem.start;
73    unsafe extern "C" {
74        fn _idmap_text_end();
75    }
76    let end = _idmap_text_end as usize - args.kcode_offset();
77
78    Some(MemoryRegion {
79        kind: MemoryRegionKind::Reserved,
80        start,
81        end,
82    })
83}