xref: /DragonOS/kernel/src/arch/x86_64/init/boot.rs (revision 52dc4c3ee87b31fee4f567f776fd8ad58fbfd9a5)
1 use system_error::SystemError;
2 
3 use crate::arch::init::multiboot::early_multiboot_init;
4 
5 use super::multiboot2::early_multiboot2_init;
6 
7 const BOOT_ENTRY_TYPE_MULTIBOOT: u64 = 1;
8 const BOOT_ENTRY_TYPE_MULTIBOOT2: u64 = 2;
9 const BOOT_ENTRY_TYPE_LINUX_32: u64 = 3;
10 const BOOT_ENTRY_TYPE_LINUX_64: u64 = 4;
11 
12 #[derive(Debug)]
13 #[repr(u64)]
14 enum BootProtocol {
15     Multiboot = 1,
16     Multiboot2,
17     Linux32,
18     Linux64,
19 }
20 
21 impl TryFrom<u64> for BootProtocol {
22     type Error = SystemError;
23 
24     fn try_from(value: u64) -> Result<Self, Self::Error> {
25         match value {
26             BOOT_ENTRY_TYPE_MULTIBOOT => Ok(BootProtocol::Multiboot),
27             BOOT_ENTRY_TYPE_MULTIBOOT2 => Ok(BootProtocol::Multiboot2),
28             BOOT_ENTRY_TYPE_LINUX_32 => Ok(BootProtocol::Linux32),
29             BOOT_ENTRY_TYPE_LINUX_64 => Ok(BootProtocol::Linux64),
30             _ => Err(SystemError::EINVAL),
31         }
32     }
33 }
34 
35 #[inline(never)]
36 pub(super) fn early_boot_init(
37     boot_entry_type: u64,
38     arg1: u64,
39     arg2: u64,
40 ) -> Result<(), SystemError> {
41     let boot_protocol = BootProtocol::try_from(boot_entry_type)?;
42     match boot_protocol {
43         BootProtocol::Multiboot => early_multiboot_init(arg1 as u32, arg2),
44         BootProtocol::Multiboot2 => early_multiboot2_init(arg1 as u32, arg2),
45         BootProtocol::Linux32 => {
46             // linux32_init(arg1, arg2);
47             unimplemented!();
48         }
49         BootProtocol::Linux64 => {
50             // linux64_init(arg1, arg2);
51             unimplemented!();
52         }
53     }
54 }
55