xref: /DragonOS/kernel/src/driver/firmware/efi/mod.rs (revision f2022a8a1cc4a8e2a85e9061e036e9c491a2fa00)
1 use system_error::SystemError;
2 
3 use crate::{libs::rwlock::RwLock, mm::PhysAddr};
4 
5 use self::{guid::DragonStubPayloadEFI, memmap::EFIMemoryMapInfo};
6 
7 mod fdt;
8 pub mod guid;
9 pub mod init;
10 pub mod memmap;
11 pub mod tables;
12 
13 static EFI_MANAGER: EFIManager = EFIManager::new();
14 
15 /// EFI管理器
16 ///
17 /// 数据成员可参考: https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/efi.h#620
18 #[derive(Debug)]
19 pub struct EFIManager {
20     inner: RwLock<InnerEFIManager>,
21 }
22 
23 #[inline(always)]
24 pub fn efi_manager() -> &'static EFIManager {
25     &EFI_MANAGER
26 }
27 
28 #[derive(Debug)]
29 struct InnerEFIManager {
30     pub mmap: EFIMemoryMapInfo,
31     /// EFI模块启动时状态的标识
32     pub init_flags: EFIInitFlags,
33     /// runtime services的物理地址
34     pub runtime_paddr: Option<PhysAddr>,
35     /// runtime services的版本号
36     pub runtime_service_version: Option<uefi_raw::table::Revision>,
37     pub dragonstub_load_info: Option<DragonStubPayloadEFI>,
38     /// uefi 内存属性表的物理地址
39     pub memory_attribute_table_paddr: Option<PhysAddr>,
40     /// uefi 内存保留表的物理地址
41     pub memreserve_table_paddr: Option<PhysAddr>,
42 }
43 
44 impl EFIManager {
45     const fn new() -> Self {
46         EFIManager {
47             inner: RwLock::new(InnerEFIManager {
48                 mmap: EFIMemoryMapInfo::DEFAULT,
49                 init_flags: EFIInitFlags::empty(),
50                 runtime_paddr: None,
51                 runtime_service_version: None,
52                 dragonstub_load_info: None,
53                 memory_attribute_table_paddr: None,
54                 memreserve_table_paddr: None,
55             }),
56         }
57     }
58 
59     pub fn desc_version(&self) -> usize {
60         return self.inner.read().mmap.desc_version;
61     }
62 
63     /// 内核加载的地址、大小的信息
64     #[allow(dead_code)]
65     pub fn kernel_load_info(&self) -> Option<DragonStubPayloadEFI> {
66         return self.inner.read().dragonstub_load_info;
67     }
68 
69     /// 检查是否为有效的system table表头
70     ///
71     /// ## 参数
72     ///
73     /// - header: system table表头
74     /// - min_major: 最小的major版本号。如果不满足,则会输出Warning,并返回Ok
75     ///
76     /// ## 返回
77     ///
78     /// - Ok(()): 检查通过
79     /// - Err(SystemError::EINVAL): header无效
80     pub fn check_system_table_header(
81         &self,
82         header: &uefi_raw::table::Header,
83         min_major: u16,
84     ) -> Result<(), SystemError> {
85         if header.signature != uefi_raw::table::system::SystemTable::SIGNATURE {
86             kerror!("System table signature mismatch!");
87             return Err(SystemError::EINVAL);
88         }
89 
90         if header.revision.major() < min_major {
91             kwarn!(
92                 "System table version: {:?}, expected {}.00 or greater!",
93                 header.revision,
94                 min_major
95             );
96         }
97 
98         return Ok(());
99     }
100 }
101 
102 // 在Rust中,我们使用枚举和bitflags来表示这些宏
103 bitflags! {
104     pub struct EFIInitFlags: u32 {
105         /// 当前使用EFI启动
106         const BOOT = 1 << 0;
107         /// 是否可以使用EFI配置表
108         const CONFIG_TABLES = 1 << 1;
109         /// 是否可以使用运行时服务
110         const RUNTIME_SERVICES = 1 << 2;
111         /// 是否可以使用EFI内存映射
112         const MEMMAP = 1 << 3;
113         /// 固件是否为64位
114         const EFI_64BIT = 1 << 4;
115         /// 访问是否通过虚拟化接口
116         const PARAVIRT = 1 << 5;
117         /// 第一架构特定位
118         const ARCH_1 = 1 << 6;
119         /// 打印附加运行时调试信息
120         const DBG = 1 << 7;
121         /// 是否可以在运行时数据区域映射非可执行
122         const NX_PE_DATA = 1 << 8;
123         /// 固件是否发布了一个EFI_MEMORY_ATTRIBUTES表
124         const MEM_ATTR = 1 << 9;
125         /// 内核是否配置为忽略软保留
126         const MEM_NO_SOFT_RESERVE = 1 << 10;
127         /// 是否可以使用EFI引导服务内存段
128         const PRESERVE_BS_REGIONS = 1 << 11;
129 
130     }
131 }
132