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