11a72a751SLoGin use core::intrinsics::unreachable; 21a72a751SLoGin 345626c85SLoGin use fdt::node::FdtNode; 445626c85SLoGin 545626c85SLoGin use crate::{ 6*7a29d4fcSLoGin arch::{mm::init::mm_early_init, MMArch}, 7*7a29d4fcSLoGin driver::{ 8*7a29d4fcSLoGin firmware::efi::init::efi_init, open_firmware::fdt::open_firmware_fdt_driver, 9*7a29d4fcSLoGin tty::serial::serial8250::send_to_default_serial8250_port, 10*7a29d4fcSLoGin }, 1145626c85SLoGin init::{boot_params, init_before_mem_init}, 12*7a29d4fcSLoGin kdebug, kinfo, 13*7a29d4fcSLoGin mm::{MemoryManagementArch, PhysAddr, VirtAddr}, 1445626c85SLoGin print, println, 1545626c85SLoGin }; 1645626c85SLoGin 1745626c85SLoGin #[derive(Debug)] 1845626c85SLoGin pub struct ArchBootParams { 1945626c85SLoGin /// 启动时的fdt物理地址 2045626c85SLoGin pub fdt_paddr: PhysAddr, 21*7a29d4fcSLoGin pub fdt_vaddr: Option<VirtAddr>, 2245626c85SLoGin } 2345626c85SLoGin 2445626c85SLoGin impl ArchBootParams { 2545626c85SLoGin pub const DEFAULT: Self = ArchBootParams { 2645626c85SLoGin fdt_paddr: PhysAddr::new(0), 27*7a29d4fcSLoGin fdt_vaddr: None, 2845626c85SLoGin }; 29*7a29d4fcSLoGin 30*7a29d4fcSLoGin pub fn arch_fdt(&self) -> VirtAddr { 31*7a29d4fcSLoGin // 如果fdt_vaddr为None,则说明还没有进行内核虚拟地址空间的映射,此时返回物理地址 32*7a29d4fcSLoGin if self.fdt_vaddr.is_none() { 33*7a29d4fcSLoGin return VirtAddr::new(self.fdt_paddr.data()); 34*7a29d4fcSLoGin } 35*7a29d4fcSLoGin self.fdt_vaddr.unwrap() 36*7a29d4fcSLoGin } 3745626c85SLoGin } 381a72a751SLoGin 391a72a751SLoGin #[no_mangle] 40666cffedSLoGin unsafe extern "C" fn kernel_main(hartid: usize, fdt_paddr: usize) -> ! { 41666cffedSLoGin let fdt_paddr = PhysAddr::new(fdt_paddr); 42*7a29d4fcSLoGin 431a72a751SLoGin init_before_mem_init(); 44*7a29d4fcSLoGin extern "C" { 45*7a29d4fcSLoGin fn BSP_IDLE_STACK_SPACE(); 46*7a29d4fcSLoGin } 47*7a29d4fcSLoGin kdebug!("BSP_IDLE_STACK_SPACE={:#x}", BSP_IDLE_STACK_SPACE as u64); 48*7a29d4fcSLoGin kdebug!("PAGE_ADDRESS_SIZE={}", MMArch::PAGE_ADDRESS_SIZE); 49*7a29d4fcSLoGin kdebug!("PAGE_ADDRESS_SHIFT={}", MMArch::PAGE_ADDRESS_SHIFT); 50*7a29d4fcSLoGin 5145626c85SLoGin boot_params().write().arch.fdt_paddr = fdt_paddr; 52666cffedSLoGin kinfo!( 53666cffedSLoGin "DragonOS kernel is running on hart {}, fdt address:{:?}", 54666cffedSLoGin hartid, 55666cffedSLoGin fdt_paddr 56666cffedSLoGin ); 5745626c85SLoGin 58*7a29d4fcSLoGin mm_early_init(); 59*7a29d4fcSLoGin 6045626c85SLoGin let fdt = fdt::Fdt::from_ptr(fdt_paddr.data() as *const u8).expect("Failed to parse fdt!"); 6145626c85SLoGin print_node(fdt.find_node("/").unwrap(), 0); 6245626c85SLoGin 6345626c85SLoGin parse_dtb(); 6445626c85SLoGin 65*7a29d4fcSLoGin efi_init(); 66*7a29d4fcSLoGin 671a72a751SLoGin loop {} 681a72a751SLoGin unreachable() 691a72a751SLoGin } 7045626c85SLoGin 71*7a29d4fcSLoGin #[inline(never)] 7245626c85SLoGin fn print_node(node: FdtNode<'_, '_>, n_spaces: usize) { 7345626c85SLoGin (0..n_spaces).for_each(|_| print!(" ")); 7445626c85SLoGin println!("{}/", node.name); 7545626c85SLoGin node.properties().for_each(|p| { 7645626c85SLoGin (0..n_spaces + 4).for_each(|_| print!(" ")); 7745626c85SLoGin println!("{}: {:?}", p.name, p.value); 7845626c85SLoGin }); 7945626c85SLoGin 8045626c85SLoGin for child in node.children() { 8145626c85SLoGin print_node(child, n_spaces + 4); 8245626c85SLoGin } 8345626c85SLoGin } 8445626c85SLoGin 8545626c85SLoGin /// 解析fdt,获取内核启动参数 86*7a29d4fcSLoGin #[inline(never)] 8745626c85SLoGin unsafe fn parse_dtb() { 8845626c85SLoGin let fdt_paddr = boot_params().read().arch.fdt_paddr; 8945626c85SLoGin if fdt_paddr.is_null() { 9045626c85SLoGin panic!("Failed to get fdt address!"); 9145626c85SLoGin } 9245626c85SLoGin 9345626c85SLoGin open_firmware_fdt_driver() 9445626c85SLoGin .early_scan_device_tree() 9545626c85SLoGin .expect("Failed to scan device tree at boottime."); 9645626c85SLoGin } 97