145626c85SLoGin use fdt::node::FdtNode; 25b59005fSLoGin use system_error::SystemError; 345626c85SLoGin 445626c85SLoGin use crate::{ 5cb23beb2SLoGin arch::{driver::sbi::SbiDriver, mm::init::mm_early_init}, 6*338f6903SLoGin driver::{ 7*338f6903SLoGin firmware::efi::init::efi_init, irqchip::riscv_intc::riscv_intc_init, 8*338f6903SLoGin open_firmware::fdt::open_firmware_fdt_driver, 9*338f6903SLoGin }, 105b59005fSLoGin init::{boot_params, init::start_kernel}, 117a29d4fcSLoGin kdebug, kinfo, 125b59005fSLoGin mm::{memblock::mem_block_manager, PhysAddr, VirtAddr}, 1345626c85SLoGin print, println, 14*338f6903SLoGin smp::cpu::ProcessorId, 1545626c85SLoGin }; 1645626c85SLoGin 17*338f6903SLoGin use super::{cpu::init_local_context, driver::sbi::console_putstr}; 18cb23beb2SLoGin 1945626c85SLoGin #[derive(Debug)] 2045626c85SLoGin pub struct ArchBootParams { 2145626c85SLoGin /// 启动时的fdt物理地址 2245626c85SLoGin pub fdt_paddr: PhysAddr, 237a29d4fcSLoGin pub fdt_vaddr: Option<VirtAddr>, 24*338f6903SLoGin 25*338f6903SLoGin pub boot_hartid: ProcessorId, 2645626c85SLoGin } 2745626c85SLoGin 2845626c85SLoGin impl ArchBootParams { 2945626c85SLoGin pub const DEFAULT: Self = ArchBootParams { 3045626c85SLoGin fdt_paddr: PhysAddr::new(0), 317a29d4fcSLoGin fdt_vaddr: None, 32*338f6903SLoGin boot_hartid: ProcessorId::new(0), 3345626c85SLoGin }; 347a29d4fcSLoGin 357a29d4fcSLoGin pub fn arch_fdt(&self) -> VirtAddr { 367a29d4fcSLoGin // 如果fdt_vaddr为None,则说明还没有进行内核虚拟地址空间的映射,此时返回物理地址 377a29d4fcSLoGin if self.fdt_vaddr.is_none() { 387a29d4fcSLoGin return VirtAddr::new(self.fdt_paddr.data()); 397a29d4fcSLoGin } 407a29d4fcSLoGin self.fdt_vaddr.unwrap() 417a29d4fcSLoGin } 4245626c85SLoGin } 431a72a751SLoGin 44*338f6903SLoGin static mut BOOT_HARTID: u32 = 0; 455b59005fSLoGin static mut BOOT_FDT_PADDR: PhysAddr = PhysAddr::new(0); 465b59005fSLoGin 471a72a751SLoGin #[no_mangle] 48666cffedSLoGin unsafe extern "C" fn kernel_main(hartid: usize, fdt_paddr: usize) -> ! { 49666cffedSLoGin let fdt_paddr = PhysAddr::new(fdt_paddr); 507a29d4fcSLoGin 515b59005fSLoGin unsafe { 52*338f6903SLoGin BOOT_HARTID = hartid as u32; 535b59005fSLoGin BOOT_FDT_PADDR = fdt_paddr; 54453452ccSLoGin } 55453452ccSLoGin 565b59005fSLoGin start_kernel(); 571a72a751SLoGin } 5845626c85SLoGin 597a29d4fcSLoGin #[inline(never)] 6045626c85SLoGin fn print_node(node: FdtNode<'_, '_>, n_spaces: usize) { 6145626c85SLoGin (0..n_spaces).for_each(|_| print!(" ")); 6245626c85SLoGin println!("{}/", node.name); 6345626c85SLoGin node.properties().for_each(|p| { 6445626c85SLoGin (0..n_spaces + 4).for_each(|_| print!(" ")); 6545626c85SLoGin println!("{}: {:?}", p.name, p.value); 6645626c85SLoGin }); 6745626c85SLoGin 6845626c85SLoGin for child in node.children() { 6945626c85SLoGin print_node(child, n_spaces + 4); 7045626c85SLoGin } 7145626c85SLoGin } 7245626c85SLoGin 7345626c85SLoGin /// 解析fdt,获取内核启动参数 747a29d4fcSLoGin #[inline(never)] 7545626c85SLoGin unsafe fn parse_dtb() { 7645626c85SLoGin let fdt_paddr = boot_params().read().arch.fdt_paddr; 7745626c85SLoGin if fdt_paddr.is_null() { 7845626c85SLoGin panic!("Failed to get fdt address!"); 7945626c85SLoGin } 8045626c85SLoGin 8145626c85SLoGin open_firmware_fdt_driver() 8245626c85SLoGin .early_scan_device_tree() 8345626c85SLoGin .expect("Failed to scan device tree at boottime."); 8445626c85SLoGin } 855b59005fSLoGin 865b59005fSLoGin #[inline(never)] 875b59005fSLoGin pub fn early_setup_arch() -> Result<(), SystemError> { 88cb23beb2SLoGin SbiDriver::early_init(); 89*338f6903SLoGin let hartid = unsafe { BOOT_HARTID }; 905b59005fSLoGin let fdt_paddr = unsafe { BOOT_FDT_PADDR }; 91*338f6903SLoGin 92*338f6903SLoGin let mut arch_boot_params_guard = boot_params().write(); 93*338f6903SLoGin arch_boot_params_guard.arch.fdt_paddr = fdt_paddr; 94*338f6903SLoGin arch_boot_params_guard.arch.boot_hartid = ProcessorId::new(hartid); 95*338f6903SLoGin 96*338f6903SLoGin drop(arch_boot_params_guard); 97cb23beb2SLoGin 985b59005fSLoGin kinfo!( 995b59005fSLoGin "DragonOS kernel is running on hart {}, fdt address:{:?}", 1005b59005fSLoGin hartid, 1015b59005fSLoGin fdt_paddr 1025b59005fSLoGin ); 1035b59005fSLoGin mm_early_init(); 1045b59005fSLoGin 1055b59005fSLoGin let fdt = 1065b59005fSLoGin unsafe { fdt::Fdt::from_ptr(fdt_paddr.data() as *const u8).expect("Failed to parse fdt!") }; 1075b59005fSLoGin print_node(fdt.find_node("/").unwrap(), 0); 1085b59005fSLoGin 1095b59005fSLoGin unsafe { parse_dtb() }; 1105b59005fSLoGin 1115b59005fSLoGin for x in mem_block_manager().to_iter() { 1125b59005fSLoGin kdebug!("before efi: {x:?}"); 1135b59005fSLoGin } 1145b59005fSLoGin 1155b59005fSLoGin efi_init(); 1165b59005fSLoGin 1175b59005fSLoGin open_firmware_fdt_driver().early_init_fdt_scan_reserved_mem(); 1185b59005fSLoGin 1195b59005fSLoGin return Ok(()); 1205b59005fSLoGin } 1215b59005fSLoGin 1225b59005fSLoGin #[inline(never)] 1235b59005fSLoGin pub fn setup_arch() -> Result<(), SystemError> { 124*338f6903SLoGin init_local_context(); 1255b59005fSLoGin return Ok(()); 1265b59005fSLoGin } 1275b59005fSLoGin 1285b59005fSLoGin #[inline(never)] 1295b59005fSLoGin pub fn setup_arch_post() -> Result<(), SystemError> { 1305b59005fSLoGin // todo 1315b59005fSLoGin return Ok(()); 1325b59005fSLoGin } 133