sdt.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. use crate::{AcpiError, AcpiHandler, AcpiResult, AcpiTable, PhysicalMapping};
  2. use core::{fmt, 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(Debug, 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(Debug, 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. /// Whether values of header fields are permitted.
  110. fn validate_header_fields(&self, signature: Signature) -> AcpiResult<()> {
  111. // Check the signature
  112. if self.signature != signature || str::from_utf8(&self.signature.0).is_err() {
  113. return Err(AcpiError::SdtInvalidSignature(signature));
  114. }
  115. // Check the OEM id
  116. if str::from_utf8(&self.oem_id).is_err() {
  117. return Err(AcpiError::SdtInvalidOemId(signature));
  118. }
  119. // Check the OEM table id
  120. if str::from_utf8(&self.oem_table_id).is_err() {
  121. return Err(AcpiError::SdtInvalidTableId(signature));
  122. }
  123. Ok(())
  124. }
  125. /// Whether table is valid according to checksum.
  126. fn validate_checksum(&self, signature: Signature) -> AcpiResult<()> {
  127. // SAFETY: Entire table is mapped.
  128. let table_bytes =
  129. unsafe { core::slice::from_raw_parts((self as *const SdtHeader).cast::<u8>(), self.length as usize) };
  130. let sum = table_bytes.iter().fold(0u8, |sum, &byte| sum.wrapping_add(byte));
  131. if sum == 0 {
  132. Ok(())
  133. } else {
  134. Err(AcpiError::SdtInvalidChecksum(signature))
  135. }
  136. }
  137. /// Checks that:
  138. ///
  139. /// 1. The signature matches the one given.
  140. /// 2. The values of various fields in the header are allowed.
  141. /// 3. The checksum of the SDT is valid.
  142. ///
  143. /// This assumes that the whole SDT is mapped.
  144. pub fn validate(&self, signature: Signature) -> AcpiResult<()> {
  145. self.validate_header_fields(signature)?;
  146. self.validate_checksum(signature)?;
  147. Ok(())
  148. }
  149. /// Validates header, proceeding with checking entire table and returning a [`PhysicalMapping`] to it if
  150. /// successful.
  151. ///
  152. /// The same checks are performed as [`SdtHeader::validate`], but `header_mapping` does not have to map the
  153. /// entire table when calling. This is useful to avoid completely mapping a table that will be immediately
  154. /// unmapped if it does not have a particular signature or has an invalid header.
  155. pub(crate) fn validate_lazy<H: AcpiHandler, T: AcpiTable>(
  156. header_mapping: PhysicalMapping<H, Self>,
  157. handler: H,
  158. ) -> AcpiResult<PhysicalMapping<H, T>> {
  159. header_mapping.validate_header_fields(T::SIGNATURE)?;
  160. // Reuse `header_mapping` to access the rest of the table if the latter is already mapped entirely
  161. let table_length = header_mapping.length as usize;
  162. let table_mapping = if header_mapping.mapped_length() >= table_length {
  163. // Avoid requesting table unmap twice (from both `header_mapping` and `table_mapping`)
  164. let header_mapping = core::mem::ManuallyDrop::new(header_mapping);
  165. // SAFETY: `header_mapping` maps entire table.
  166. unsafe {
  167. PhysicalMapping::new(
  168. header_mapping.physical_start(),
  169. header_mapping.virtual_start().cast::<T>(),
  170. table_length,
  171. header_mapping.mapped_length(),
  172. handler,
  173. )
  174. }
  175. } else {
  176. // Unmap header as soon as possible
  177. let table_phys_start = header_mapping.physical_start();
  178. drop(header_mapping);
  179. // SAFETY: `table_phys_start` is the physical address of the header and the rest of the table.
  180. unsafe { handler.map_physical_region(table_phys_start, table_length) }
  181. };
  182. // This is usually redundant compared to simply calling `validate_checksum` but respects custom
  183. // `AcpiTable::validate` implementations.
  184. table_mapping.validate()?;
  185. Ok(table_mapping)
  186. }
  187. pub fn oem_id(&self) -> &str {
  188. // Safe to unwrap because checked in `validate`
  189. str::from_utf8(&self.oem_id).unwrap()
  190. }
  191. pub fn oem_table_id(&self) -> &str {
  192. // Safe to unwrap because checked in `validate`
  193. str::from_utf8(&self.oem_table_id).unwrap()
  194. }
  195. }
  196. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
  197. #[repr(transparent)]
  198. pub struct Signature([u8; 4]);
  199. impl Signature {
  200. pub const RSDT: Signature = Signature(*b"RSDT");
  201. pub const XSDT: Signature = Signature(*b"XSDT");
  202. pub const FADT: Signature = Signature(*b"FACP");
  203. pub const HPET: Signature = Signature(*b"HPET");
  204. pub const MADT: Signature = Signature(*b"APIC");
  205. pub const MCFG: Signature = Signature(*b"MCFG");
  206. pub const SSDT: Signature = Signature(*b"SSDT");
  207. pub const BERT: Signature = Signature(*b"BERT");
  208. pub const BGRT: Signature = Signature(*b"BGRT");
  209. pub const CPEP: Signature = Signature(*b"CPEP");
  210. pub const DSDT: Signature = Signature(*b"DSDT");
  211. pub const ECDT: Signature = Signature(*b"ECDT");
  212. pub const EINJ: Signature = Signature(*b"EINJ");
  213. pub const ERST: Signature = Signature(*b"ERST");
  214. pub const FACS: Signature = Signature(*b"FACS");
  215. pub const FPDT: Signature = Signature(*b"FPDT");
  216. pub const GTDT: Signature = Signature(*b"GTDT");
  217. pub const HEST: Signature = Signature(*b"HEST");
  218. pub const MSCT: Signature = Signature(*b"MSCT");
  219. pub const MPST: Signature = Signature(*b"MPST");
  220. pub const NFIT: Signature = Signature(*b"NFIT");
  221. pub const PCCT: Signature = Signature(*b"PCCT");
  222. pub const PHAT: Signature = Signature(*b"PHAT");
  223. pub const PMTT: Signature = Signature(*b"PMTT");
  224. pub const PSDT: Signature = Signature(*b"PSDT");
  225. pub const RASF: Signature = Signature(*b"RASF");
  226. pub const SBST: Signature = Signature(*b"SBST");
  227. pub const SDEV: Signature = Signature(*b"SDEV");
  228. pub const SLIT: Signature = Signature(*b"SLIT");
  229. pub const SRAT: Signature = Signature(*b"SRAT");
  230. pub const AEST: Signature = Signature(*b"AEST");
  231. pub const BDAT: Signature = Signature(*b"BDAT");
  232. pub const CDIT: Signature = Signature(*b"CDIT");
  233. pub const CEDT: Signature = Signature(*b"CEDT");
  234. pub const CRAT: Signature = Signature(*b"CRAT");
  235. pub const CSRT: Signature = Signature(*b"CSRT");
  236. pub const DBGP: Signature = Signature(*b"DBGP");
  237. pub const DBG2: Signature = Signature(*b"DBG2");
  238. pub const DMAR: Signature = Signature(*b"DMAR");
  239. pub const DRTM: Signature = Signature(*b"DRTM");
  240. pub const ETDT: Signature = Signature(*b"ETDT");
  241. pub const IBFT: Signature = Signature(*b"IBFT");
  242. pub const IORT: Signature = Signature(*b"IORT");
  243. pub const IVRS: Signature = Signature(*b"IVRS");
  244. pub const LPIT: Signature = Signature(*b"LPIT");
  245. pub const MCHI: Signature = Signature(*b"MCHI");
  246. pub const MPAM: Signature = Signature(*b"MPAM");
  247. pub const MSDM: Signature = Signature(*b"MSDM");
  248. pub const PRMT: Signature = Signature(*b"PRMT");
  249. pub const RGRT: Signature = Signature(*b"RGRT");
  250. pub const SDEI: Signature = Signature(*b"SDEI");
  251. pub const SLIC: Signature = Signature(*b"SLIC");
  252. pub const SPCR: Signature = Signature(*b"SPCR");
  253. pub const SPMI: Signature = Signature(*b"SPMI");
  254. pub const STAO: Signature = Signature(*b"STAO");
  255. pub const SVKL: Signature = Signature(*b"SVKL");
  256. pub const TCPA: Signature = Signature(*b"TCPA");
  257. pub const TPM2: Signature = Signature(*b"TPM2");
  258. pub const UEFI: Signature = Signature(*b"UEFI");
  259. pub const WAET: Signature = Signature(*b"WAET");
  260. pub const WDAT: Signature = Signature(*b"WDAT");
  261. pub const WDRT: Signature = Signature(*b"WDRT");
  262. pub const WPBT: Signature = Signature(*b"WPBT");
  263. pub const WSMT: Signature = Signature(*b"WSMT");
  264. pub const XENV: Signature = Signature(*b"XENV");
  265. pub fn as_str(&self) -> &str {
  266. str::from_utf8(&self.0).unwrap()
  267. }
  268. }
  269. impl fmt::Display for Signature {
  270. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  271. write!(f, "{}", self.as_str())
  272. }
  273. }
  274. impl fmt::Debug for Signature {
  275. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  276. write!(f, "\"{}\"", self.as_str())
  277. }
  278. }