1 use core::intrinsics::unreachable; 2 3 use fdt::node::FdtNode; 4 5 use crate::{ 6 driver::open_firmware::fdt::open_firmware_fdt_driver, 7 init::{boot_params, init_before_mem_init}, 8 kinfo, 9 mm::{PhysAddr, VirtAddr}, 10 print, println, 11 }; 12 13 #[derive(Debug)] 14 pub struct ArchBootParams { 15 /// 启动时的fdt物理地址 16 pub fdt_paddr: PhysAddr, 17 } 18 19 impl ArchBootParams { 20 pub const DEFAULT: Self = ArchBootParams { 21 fdt_paddr: PhysAddr::new(0), 22 }; 23 } 24 25 #[no_mangle] 26 unsafe extern "C" fn kernel_main(hartid: usize, fdt_paddr: usize) -> ! { 27 let fdt_paddr = PhysAddr::new(fdt_paddr); 28 init_before_mem_init(); 29 boot_params().write().arch.fdt_paddr = fdt_paddr; 30 kinfo!( 31 "DragonOS kernel is running on hart {}, fdt address:{:?}", 32 hartid, 33 fdt_paddr 34 ); 35 36 let fdt = fdt::Fdt::from_ptr(fdt_paddr.data() as *const u8).expect("Failed to parse fdt!"); 37 print_node(fdt.find_node("/").unwrap(), 0); 38 39 parse_dtb(); 40 41 loop {} 42 unreachable() 43 } 44 45 fn print_node(node: FdtNode<'_, '_>, n_spaces: usize) { 46 (0..n_spaces).for_each(|_| print!(" ")); 47 println!("{}/", node.name); 48 node.properties().for_each(|p| { 49 (0..n_spaces + 4).for_each(|_| print!(" ")); 50 println!("{}: {:?}", p.name, p.value); 51 }); 52 53 for child in node.children() { 54 print_node(child, n_spaces + 4); 55 } 56 } 57 58 /// 解析fdt,获取内核启动参数 59 unsafe fn parse_dtb() { 60 let fdt_paddr = boot_params().read().arch.fdt_paddr; 61 if fdt_paddr.is_null() { 62 panic!("Failed to get fdt address!"); 63 } 64 65 open_firmware_fdt_driver() 66 .set_fdt_vaddr(VirtAddr::new(fdt_paddr.data())) 67 .unwrap(); 68 open_firmware_fdt_driver() 69 .early_scan_device_tree() 70 .expect("Failed to scan device tree at boottime."); 71 } 72