xref: /DragonOS/kernel/src/arch/riscv64/init/mod.rs (revision e28411791f090c421fe4b6fa5956fb1bd362a8d9)
1 use fdt::node::FdtNode;
2 use system_error::SystemError;
3 
4 use crate::{
5     arch::{driver::sbi::SbiDriver, mm::init::mm_early_init},
6     driver::{firmware::efi::init::efi_init, open_firmware::fdt::open_firmware_fdt_driver},
7     init::{boot_params, init::start_kernel},
8     kdebug, kinfo,
9     mm::{memblock::mem_block_manager, PhysAddr, VirtAddr},
10     print, println,
11 };
12 
13 use super::driver::sbi::console_putstr;
14 
15 #[derive(Debug)]
16 pub struct ArchBootParams {
17     /// 启动时的fdt物理地址
18     pub fdt_paddr: PhysAddr,
19     pub fdt_vaddr: Option<VirtAddr>,
20 }
21 
22 impl ArchBootParams {
23     pub const DEFAULT: Self = ArchBootParams {
24         fdt_paddr: PhysAddr::new(0),
25         fdt_vaddr: None,
26     };
27 
28     pub fn arch_fdt(&self) -> VirtAddr {
29         // 如果fdt_vaddr为None,则说明还没有进行内核虚拟地址空间的映射,此时返回物理地址
30         if self.fdt_vaddr.is_none() {
31             return VirtAddr::new(self.fdt_paddr.data());
32         }
33         self.fdt_vaddr.unwrap()
34     }
35 }
36 
37 static mut BOOT_HARTID: usize = 0;
38 static mut BOOT_FDT_PADDR: PhysAddr = PhysAddr::new(0);
39 
40 #[no_mangle]
41 unsafe extern "C" fn kernel_main(hartid: usize, fdt_paddr: usize) -> ! {
42     let fdt_paddr = PhysAddr::new(fdt_paddr);
43 
44     unsafe {
45         BOOT_HARTID = hartid;
46         BOOT_FDT_PADDR = fdt_paddr;
47     }
48 
49     start_kernel();
50 }
51 
52 #[inline(never)]
53 fn print_node(node: FdtNode<'_, '_>, n_spaces: usize) {
54     (0..n_spaces).for_each(|_| print!(" "));
55     println!("{}/", node.name);
56     node.properties().for_each(|p| {
57         (0..n_spaces + 4).for_each(|_| print!(" "));
58         println!("{}: {:?}", p.name, p.value);
59     });
60 
61     for child in node.children() {
62         print_node(child, n_spaces + 4);
63     }
64 }
65 
66 /// 解析fdt,获取内核启动参数
67 #[inline(never)]
68 unsafe fn parse_dtb() {
69     let fdt_paddr = boot_params().read().arch.fdt_paddr;
70     if fdt_paddr.is_null() {
71         panic!("Failed to get fdt address!");
72     }
73 
74     open_firmware_fdt_driver()
75         .early_scan_device_tree()
76         .expect("Failed to scan device tree at boottime.");
77 }
78 
79 #[inline(never)]
80 pub fn early_setup_arch() -> Result<(), SystemError> {
81     SbiDriver::early_init();
82     let hartid: usize = unsafe { BOOT_HARTID };
83     let fdt_paddr = unsafe { BOOT_FDT_PADDR };
84     boot_params().write().arch.fdt_paddr = fdt_paddr;
85 
86     kinfo!(
87         "DragonOS kernel is running on hart {}, fdt address:{:?}",
88         hartid,
89         fdt_paddr
90     );
91     mm_early_init();
92 
93     let fdt =
94         unsafe { fdt::Fdt::from_ptr(fdt_paddr.data() as *const u8).expect("Failed to parse fdt!") };
95     print_node(fdt.find_node("/").unwrap(), 0);
96 
97     unsafe { parse_dtb() };
98 
99     for x in mem_block_manager().to_iter() {
100         kdebug!("before efi: {x:?}");
101     }
102 
103     efi_init();
104 
105     open_firmware_fdt_driver().early_init_fdt_scan_reserved_mem();
106 
107     return Ok(());
108 }
109 
110 #[inline(never)]
111 pub fn setup_arch() -> Result<(), SystemError> {
112     // todo
113     return Ok(());
114 }
115 
116 #[inline(never)]
117 pub fn setup_arch_post() -> Result<(), SystemError> {
118     // todo
119     return Ok(());
120 }
121