mod.rs 4.4 KB

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