xref: /DragonOS/kernel/src/init/boot.rs (revision 886ce28516f9e3e5940840d1ae64ec3e9c8875fa)
1 use core::cmp::min;
2 
3 use acpi::rsdp::Rsdp;
4 use alloc::string::String;
5 use system_error::SystemError;
6 
7 use crate::{
8     arch::init::ArchBootParams,
9     driver::video::fbdev::base::BootTimeScreenInfo,
10     libs::lazy_init::Lazy,
11     mm::{PhysAddr, VirtAddr},
12 };
13 
14 use super::boot_params;
15 #[derive(Debug)]
16 pub struct BootParams {
17     pub screen_info: BootTimeScreenInfo,
18     bootloader_name: Option<String>,
19     #[allow(dead_code)]
20     pub arch: ArchBootParams,
21     boot_command_line: [u8; Self::BOOT_COMMAND_LINE_SIZE],
22     pub acpi: BootloaderAcpiArg,
23 }
24 
25 impl BootParams {
26     const DEFAULT: Self = BootParams {
27         screen_info: BootTimeScreenInfo::DEFAULT,
28         bootloader_name: None,
29         arch: ArchBootParams::DEFAULT,
30         boot_command_line: [0u8; Self::BOOT_COMMAND_LINE_SIZE],
31         acpi: BootloaderAcpiArg::NotProvided,
32     };
33 
34     /// 开机命令行参数字符串最大大小
35     pub const BOOT_COMMAND_LINE_SIZE: usize = 2048;
36 
37     pub(super) const fn new() -> Self {
38         Self::DEFAULT
39     }
40 
41     /// 开机命令行参数(原始字节数组)
42     #[allow(dead_code)]
43     pub fn boot_cmdline(&self) -> &[u8] {
44         &self.boot_command_line
45     }
46 
47     /// 开机命令行参数字符串
48     pub fn boot_cmdline_str(&self) -> &str {
49         core::str::from_utf8(self.boot_cmdline()).unwrap()
50     }
51 
52     pub fn bootloader_name(&self) -> Option<&str> {
53         self.bootloader_name.as_deref()
54     }
55 
56     /// 追加开机命令行参数
57     ///
58     /// 如果开机命令行参数已经满了,则不会追加。
59     /// 如果超过了最大长度,则截断。
60     ///
61     /// ## 参数
62     ///
63     /// - `data`:追加的数据
64     pub fn boot_cmdline_append(&mut self, data: &[u8]) {
65         if data.is_empty() {
66             return;
67         }
68 
69         let mut pos: Option<usize> = None;
70         // 寻找结尾
71         for (i, x) in self.boot_command_line.iter().enumerate() {
72             if *x == 0 {
73                 pos = Some(i);
74                 break;
75             }
76         }
77         let pos = pos.unwrap_or(self.boot_command_line.len() - 1) as isize;
78 
79         let avail = self.boot_command_line.len() as isize - pos - 1;
80         if avail <= 0 {
81             return;
82         }
83 
84         let len = min(avail as usize, data.len());
85         let pos = pos as usize;
86         self.boot_command_line[pos..pos + len].copy_from_slice(&data[0..len]);
87 
88         self.boot_command_line[pos + len] = 0;
89     }
90 
91     /// 获取FDT的虚拟地址
92     #[allow(dead_code)]
93     pub fn fdt(&self) -> Option<VirtAddr> {
94         #[cfg(target_arch = "riscv64")]
95         return Some(self.arch.arch_fdt());
96 
97         #[cfg(target_arch = "x86_64")]
98         return None;
99     }
100 
101     /// 获取FDT的物理地址
102     #[allow(dead_code)]
103     pub fn fdt_paddr(&self) -> Option<PhysAddr> {
104         #[cfg(target_arch = "riscv64")]
105         return Some(self.arch.fdt_paddr);
106 
107         #[cfg(target_arch = "x86_64")]
108         return None;
109     }
110 }
111 
112 /// 开机引导回调,用于初始化内核启动参数
113 pub trait BootCallbacks: Send + Sync {
114     /// 初始化引导程序名称
115     fn init_bootloader_name(&self) -> Result<Option<String>, SystemError>;
116     /// 初始化ACPI参数
117     fn init_acpi_args(&self) -> Result<BootloaderAcpiArg, SystemError>;
118     /// 初始化内核命令行参数
119     ///
120     /// 该函数应该把内核命令行参数追加到`boot_params().boot_cmdline`中
121     fn init_kernel_cmdline(&self) -> Result<(), SystemError>;
122     /// 初始化帧缓冲区信息
123     ///
124     /// - 该函数应该把帧缓冲区信息写入`scinfo`中。
125     /// - 该函数应该在内存管理初始化之前调用。
126     fn early_init_framebuffer_info(
127         &self,
128         scinfo: &mut BootTimeScreenInfo,
129     ) -> Result<(), SystemError>;
130 
131     /// 初始化内存块
132     fn early_init_memory_blocks(&self) -> Result<(), SystemError>;
133 }
134 
135 static BOOT_CALLBACKS: Lazy<&'static dyn BootCallbacks> = Lazy::new();
136 
137 /// 注册开机引导回调
138 pub fn register_boot_callbacks(callbacks: &'static dyn BootCallbacks) {
139     BOOT_CALLBACKS.init(callbacks);
140 }
141 
142 /// 获取开机引导回调
143 pub fn boot_callbacks() -> &'static dyn BootCallbacks {
144     let p = BOOT_CALLBACKS
145         .try_get()
146         .expect("Boot callbacks not initialized");
147 
148     *p
149 }
150 
151 pub(super) fn boot_callback_except_early() {
152     boot_callbacks()
153         .init_kernel_cmdline()
154         .expect("Failed to init kernel cmdline");
155     let mut boot_params = boot_params().write();
156     boot_params.bootloader_name = boot_callbacks()
157         .init_bootloader_name()
158         .expect("Failed to init bootloader name");
159     boot_params.acpi = boot_callbacks()
160         .init_acpi_args()
161         .unwrap_or(BootloaderAcpiArg::NotProvided);
162 }
163 
164 /// ACPI information from the bootloader.
165 #[derive(Copy, Clone, Debug)]
166 pub enum BootloaderAcpiArg {
167     /// The bootloader does not provide one, a manual search is needed.
168     NotProvided,
169     /// Physical address of the RSDP.
170     #[allow(dead_code)]
171     Rsdp(PhysAddr),
172     /// Address of RSDT provided in RSDP v1.
173     Rsdt(Rsdp),
174     /// Address of XSDT provided in RSDP v2+.
175     Xsdt(Rsdp),
176 }
177