sdt.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. use crate::{AcpiError, AcpiHandler};
  2. use core::{fmt, mem, mem::MaybeUninit, str};
  3. /// Represents a field which may or may not be present within an ACPI structure, depending on the version of ACPI
  4. /// that a system supports. If the field is not present, it is not safe to treat the data as initialised.
  5. #[derive(Clone, Copy)]
  6. #[repr(transparent)]
  7. pub struct ExtendedField<T: Copy, const MIN_REVISION: u8>(MaybeUninit<T>);
  8. impl<T: Copy, const MIN_REVISION: u8> ExtendedField<T, MIN_REVISION> {
  9. /// Access the field if it's present for the given revision of the table.
  10. ///
  11. /// ### Safety
  12. /// If a bogus ACPI version is passed, this function may access uninitialised data.
  13. pub unsafe fn access(&self, revision: u8) -> Option<T> {
  14. if revision >= MIN_REVISION {
  15. Some(unsafe { self.0.assume_init() })
  16. } else {
  17. None
  18. }
  19. }
  20. }
  21. /// All SDTs share the same header, and are `length` bytes long. The signature tells us which SDT
  22. /// this is.
  23. ///
  24. /// The ACPI Spec (Version 6.4) defines the following SDT signatures:
  25. ///
  26. /// * APIC - Multiple APIC Description Table (MADT)
  27. /// * BERT - Boot Error Record Table
  28. /// * BGRT - Boot Graphics Resource Table
  29. /// * CPEP - Corrected Platform Error Polling Table
  30. /// * DSDT - Differentiated System Description Table (DSDT)
  31. /// * ECDT - Embedded Controller Boot Resources Table
  32. /// * EINJ - Error Injection Table
  33. /// * ERST - Error Record Serialization Table
  34. /// * FACP - Fixed ACPI Description Table (FADT)
  35. /// * FACS - Firmware ACPI Control Structure
  36. /// * FPDT - Firmware Performance Data Table
  37. /// * GTDT - Generic Timer Description Table
  38. /// * HEST - Hardware Error Source Table
  39. /// * MSCT - Maximum System Characteristics Table
  40. /// * MPST - Memory Power StateTable
  41. /// * NFIT - NVDIMM Firmware Interface Table
  42. /// * OEMx - OEM Specific Information Tables
  43. /// * PCCT - Platform Communications Channel Table
  44. /// * PHAT - Platform Health Assessment Table
  45. /// * PMTT - Platform Memory Topology Table
  46. /// * PSDT - Persistent System Description Table
  47. /// * RASF - ACPI RAS Feature Table
  48. /// * RSDT - Root System Description Table
  49. /// * SBST - Smart Battery Specification Table
  50. /// * SDEV - Secure DEVices Table
  51. /// * SLIT - System Locality Distance Information Table
  52. /// * SRAT - System Resource Affinity Table
  53. /// * SSDT - Secondary System Description Table
  54. /// * XSDT - Extended System Description Table
  55. ///
  56. /// Acpi reserves the following signatures and the specifications for them can be found [here](https://uefi.org/acpi):
  57. ///
  58. /// * AEST - ARM Error Source Table
  59. /// * BDAT - BIOS Data ACPI Table
  60. /// * CDIT - Component Distance Information Table
  61. /// * CEDT - CXL Early Discovery Table
  62. /// * CRAT - Component Resource Attribute Table
  63. /// * CSRT - Core System Resource Table
  64. /// * DBGP - Debug Port Table
  65. /// * DBG2 - Debug Port Table 2 (note: ACPI 6.4 defines this as "DBPG2" but this is incorrect)
  66. /// * DMAR - DMA Remapping Table
  67. /// * DRTM -Dynamic Root of Trust for Measurement Table
  68. /// * ETDT - Event Timer Description Table (obsolete, superseeded by HPET)
  69. /// * HPET - IA-PC High Precision Event Timer Table
  70. /// * IBFT - iSCSI Boot Firmware Table
  71. /// * IORT - I/O Remapping Table
  72. /// * IVRS - I/O Virtualization Reporting Structure
  73. /// * LPIT - Low Power Idle Table
  74. /// * MCFG - PCI Express Memory-mapped Configuration Space base address description table
  75. /// * MCHI - Management Controller Host Interface table
  76. /// * MPAM - ARM Memory Partitioning And Monitoring table
  77. /// * MSDM - Microsoft Data Management Table
  78. /// * PRMT - Platform Runtime Mechanism Table
  79. /// * RGRT - Regulatory Graphics Resource Table
  80. /// * SDEI - Software Delegated Exceptions Interface table
  81. /// * SLIC - Microsoft Software Licensing table
  82. /// * SPCR - Microsoft Serial Port Console Redirection table
  83. /// * SPMI - Server Platform Management Interface table
  84. /// * STAO - _STA Override table
  85. /// * SVKL - Storage Volume Key Data table (Intel TDX only)
  86. /// * TCPA - Trusted Computing Platform Alliance Capabilities Table
  87. /// * TPM2 - Trusted Platform Module 2 Table
  88. /// * UEFI - Unified Extensible Firmware Interface Specification table
  89. /// * WAET - Windows ACPI Emulated Devices Table
  90. /// * WDAT - Watch Dog Action Table
  91. /// * WDRT - Watchdog Resource Table
  92. /// * WPBT - Windows Platform Binary Table
  93. /// * WSMT - Windows Security Mitigations Table
  94. /// * XENV - Xen Project
  95. #[derive(Clone, Copy)]
  96. #[repr(C, packed)]
  97. pub struct SdtHeader {
  98. pub signature: Signature,
  99. pub length: u32,
  100. pub revision: u8,
  101. pub checksum: u8,
  102. pub oem_id: [u8; 6],
  103. pub oem_table_id: [u8; 8],
  104. pub oem_revision: u32,
  105. pub creator_id: u32,
  106. pub creator_revision: u32,
  107. }
  108. impl SdtHeader {
  109. /// Check that:
  110. /// a) The signature matches the one given
  111. /// b) The checksum of the SDT is valid
  112. ///
  113. /// This assumes that the whole SDT is mapped.
  114. pub fn validate(&self, signature: Signature) -> Result<(), AcpiError> {
  115. // Check the signature
  116. if self.signature != signature {
  117. return Err(AcpiError::SdtInvalidSignature(signature));
  118. }
  119. // Check the OEM id
  120. if str::from_utf8(&self.oem_id).is_err() {
  121. return Err(AcpiError::SdtInvalidOemId(signature));
  122. }
  123. // Check the OEM table id
  124. if str::from_utf8(&self.oem_table_id).is_err() {
  125. return Err(AcpiError::SdtInvalidTableId(signature));
  126. }
  127. // Validate the checksum
  128. let self_ptr = self as *const SdtHeader as *const u8;
  129. let mut sum: u8 = 0;
  130. for i in 0..self.length {
  131. sum = sum.wrapping_add(unsafe { *(self_ptr.offset(i as isize)) } as u8);
  132. }
  133. if sum > 0 {
  134. return Err(AcpiError::SdtInvalidChecksum(signature));
  135. }
  136. Ok(())
  137. }
  138. pub fn oem_id(&self) -> &str {
  139. // Safe to unwrap because checked in `validate`
  140. str::from_utf8(&self.oem_id).unwrap()
  141. }
  142. pub fn oem_table_id(&self) -> &str {
  143. // Safe to unwrap because checked in `validate`
  144. str::from_utf8(&self.oem_table_id).unwrap()
  145. }
  146. }
  147. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
  148. #[repr(transparent)]
  149. pub struct Signature([u8; 4]);
  150. impl Signature {
  151. pub const RSDT: Signature = Signature(*b"RSDT");
  152. pub const XSDT: Signature = Signature(*b"XSDT");
  153. pub const FADT: Signature = Signature(*b"FACP");
  154. pub const HPET: Signature = Signature(*b"HPET");
  155. pub const MADT: Signature = Signature(*b"APIC");
  156. pub const MCFG: Signature = Signature(*b"MCFG");
  157. pub const SSDT: Signature = Signature(*b"SSDT");
  158. pub const BERT: Signature = Signature(*b"BERT");
  159. pub const BGRT: Signature = Signature(*b"BGRT");
  160. pub const CPEP: Signature = Signature(*b"CPEP");
  161. pub const DSDT: Signature = Signature(*b"DSDT");
  162. pub const ECDT: Signature = Signature(*b"ECDT");
  163. pub const EINJ: Signature = Signature(*b"EINJ");
  164. pub const ERST: Signature = Signature(*b"ERST");
  165. pub const FACS: Signature = Signature(*b"FACS");
  166. pub const FPDT: Signature = Signature(*b"FPDT");
  167. pub const GTDT: Signature = Signature(*b"GTDT");
  168. pub const HEST: Signature = Signature(*b"HEST");
  169. pub const MSCT: Signature = Signature(*b"MSCT");
  170. pub const MPST: Signature = Signature(*b"MPST");
  171. pub const NFIT: Signature = Signature(*b"NFIT");
  172. pub const PCCT: Signature = Signature(*b"PCCT");
  173. pub const PHAT: Signature = Signature(*b"PHAT");
  174. pub const PMTT: Signature = Signature(*b"PMTT");
  175. pub const PSDT: Signature = Signature(*b"PSDT");
  176. pub const RASF: Signature = Signature(*b"RASF");
  177. pub const SBST: Signature = Signature(*b"SBST");
  178. pub const SDEV: Signature = Signature(*b"SDEV");
  179. pub const SLIT: Signature = Signature(*b"SLIT");
  180. pub const SRAT: Signature = Signature(*b"SRAT");
  181. pub const AEST: Signature = Signature(*b"AEST");
  182. pub const BDAT: Signature = Signature(*b"BDAT");
  183. pub const CDIT: Signature = Signature(*b"CDIT");
  184. pub const CEDT: Signature = Signature(*b"CEDT");
  185. pub const CRAT: Signature = Signature(*b"CRAT");
  186. pub const CSRT: Signature = Signature(*b"CSRT");
  187. pub const DBGP: Signature = Signature(*b"DBGP");
  188. pub const DBG2: Signature = Signature(*b"DBG2");
  189. pub const DMAR: Signature = Signature(*b"DMAR");
  190. pub const DRTM: Signature = Signature(*b"DRTM");
  191. pub const ETDT: Signature = Signature(*b"ETDT");
  192. pub const IBFT: Signature = Signature(*b"IBFT");
  193. pub const IORT: Signature = Signature(*b"IORT");
  194. pub const IVRS: Signature = Signature(*b"IVRS");
  195. pub const LPIT: Signature = Signature(*b"LPIT");
  196. pub const MCHI: Signature = Signature(*b"MCHI");
  197. pub const MPAM: Signature = Signature(*b"MPAM");
  198. pub const MSDM: Signature = Signature(*b"MSDM");
  199. pub const PRMT: Signature = Signature(*b"PRMT");
  200. pub const RGRT: Signature = Signature(*b"RGRT");
  201. pub const SDEI: Signature = Signature(*b"SDEI");
  202. pub const SLIC: Signature = Signature(*b"SLIC");
  203. pub const SPCR: Signature = Signature(*b"SPCR");
  204. pub const SPMI: Signature = Signature(*b"SPMI");
  205. pub const STAO: Signature = Signature(*b"STAO");
  206. pub const SVKL: Signature = Signature(*b"SVKL");
  207. pub const TCPA: Signature = Signature(*b"TCPA");
  208. pub const TPM2: Signature = Signature(*b"TPM2");
  209. pub const UEFI: Signature = Signature(*b"UEFI");
  210. pub const WAET: Signature = Signature(*b"WAET");
  211. pub const WDAT: Signature = Signature(*b"WDAT");
  212. pub const WDRT: Signature = Signature(*b"WDRT");
  213. pub const WPBT: Signature = Signature(*b"WPBT");
  214. pub const WSMT: Signature = Signature(*b"WSMT");
  215. pub const XENV: Signature = Signature(*b"XENV");
  216. pub fn as_str(&self) -> &str {
  217. str::from_utf8(&self.0).unwrap()
  218. }
  219. }
  220. impl fmt::Display for Signature {
  221. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  222. write!(f, "{}", self.as_str())
  223. }
  224. }
  225. impl fmt::Debug for Signature {
  226. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  227. write!(f, "\"{}\"", self.as_str())
  228. }
  229. }
  230. /// Takes the physical address of an SDT, and maps, clones and unmaps its header. Useful for
  231. /// finding out how big it is to map it correctly later.
  232. pub(crate) fn peek_at_sdt_header<H>(handler: &H, physical_address: usize) -> SdtHeader
  233. where
  234. H: AcpiHandler,
  235. {
  236. let mapping =
  237. unsafe { handler.map_physical_region::<SdtHeader>(physical_address, mem::size_of::<SdtHeader>()) };
  238. *mapping
  239. }