sdt.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. use crate::{fadt::Fadt, hpet::HpetTable, madt::Madt, mcfg::Mcfg, Acpi, AcpiError, AcpiHandler, AmlTable};
  2. use core::{fmt, mem, mem::MaybeUninit, str};
  3. use log::{trace, warn};
  4. pub const ACPI_VERSION_2_0: u8 = 20;
  5. /// Represents a field which may or may not be present within an ACPI structure, depending on the version of ACPI
  6. /// that a system supports. If the field is not present, it is not safe to treat the data as initialised.
  7. #[repr(C, packed)]
  8. pub struct ExtendedField<T: Copy, const MIN_VERSION: u8>(MaybeUninit<T>);
  9. impl<T: Copy, const MIN_VERSION: u8> ExtendedField<T, MIN_VERSION> {
  10. /// Access the field if it's present for the given ACPI version. You should get this version from another ACPI
  11. /// structure, such as the RSDT/XSDT.
  12. ///
  13. /// ### Safety
  14. /// If a bogus ACPI version is passed, this function may access uninitialised data, which is unsafe.
  15. pub unsafe fn access(&self, version: u8) -> Option<T> {
  16. if version >= MIN_VERSION {
  17. Some(self.0.assume_init())
  18. } else {
  19. None
  20. }
  21. }
  22. }
  23. /// All SDTs share the same header, and are `length` bytes long. The signature tells us which SDT
  24. /// this is.
  25. ///
  26. /// The ACPI Spec (Version 6.2) defines the following SDT signatures:
  27. /// "APIC" - Multiple APIC Descriptor Table (MADT)
  28. /// "BGRT" - Boot Graphics Resource Table
  29. /// "BERT" - Boot Error Record Table
  30. /// "CPEP" - Corrected Platform Error Polling Table
  31. /// "DSDT" - Differentiated System Descriptor Table
  32. /// "ECDT" - Embedded Controller Boot Resources Table
  33. /// "EINJ" - Error Injection Table
  34. /// "ERST" - Error Record Serialization Table
  35. /// "FACP" - Fixed ACPI Description Table (FADT)
  36. /// "FACS" - Firmware ACPI Control Structure
  37. /// "FPDT" - Firmware Performance Data Table
  38. /// "GTDT" - Generic Timer Description Table
  39. /// "HEST" - Hardware Error Source Table
  40. /// "HMAT" - Heterogeneous Memory Attributes Table
  41. /// "MSCT" - Maximum System Characteristics Table
  42. /// "MPST" - Memory Power State Table
  43. /// "NFIT" - NVDIMM Firmware Interface Table
  44. /// "OEMx" - Various OEM-specific tables
  45. /// "PDTT" - Platform Debug Trigger Table
  46. /// "PMTT" - Platform Memory Topology Table
  47. /// "PPTT" - Processor Properties Topology Table
  48. /// "PSDT" - Persistent System Description Table
  49. /// "RASF" - ACPI RAS Feature Table
  50. /// "RSDT" - Root System Descriptor Table
  51. /// "SBST" - Smart Battery Specification Table
  52. /// "SLIT" - System Locality Information Table
  53. /// "SRAT" - System Resource Affinity Table
  54. /// "SSDT" - Secondary System Description Table
  55. /// "XSDT" - eXtended System Descriptor Table
  56. #[derive(Clone, Copy)]
  57. #[repr(C, packed)]
  58. pub struct SdtHeader {
  59. pub signature: Signature,
  60. pub length: u32,
  61. pub revision: u8,
  62. pub checksum: u8,
  63. pub oem_id: [u8; 6],
  64. pub oem_table_id: [u8; 8],
  65. pub oem_revision: u32,
  66. pub creator_id: u32,
  67. pub creator_revision: u32,
  68. }
  69. impl SdtHeader {
  70. /// Check that:
  71. /// a) The signature matches the one given
  72. /// b) The checksum of the SDT
  73. ///
  74. /// This assumes that the whole SDT is mapped.
  75. pub fn validate(&self, signature: Signature) -> Result<(), AcpiError> {
  76. // Check the signature
  77. if self.signature != signature {
  78. return Err(AcpiError::SdtInvalidSignature(signature));
  79. }
  80. // Check the OEM id
  81. if str::from_utf8(&self.oem_id).is_err() {
  82. return Err(AcpiError::SdtInvalidOemId(signature));
  83. }
  84. // Check the OEM table id
  85. if str::from_utf8(&self.oem_table_id).is_err() {
  86. return Err(AcpiError::SdtInvalidTableId(signature));
  87. }
  88. // Validate the checksum
  89. let self_ptr = self as *const SdtHeader as *const u8;
  90. let mut sum: u8 = 0;
  91. for i in 0..self.length {
  92. sum = sum.wrapping_add(unsafe { *(self_ptr.offset(i as isize)) } as u8);
  93. }
  94. if sum > 0 {
  95. return Err(AcpiError::SdtInvalidChecksum(signature));
  96. }
  97. Ok(())
  98. }
  99. pub fn oem_id<'a>(&'a self) -> &'a str {
  100. // Safe to unwrap because checked in `validate`
  101. str::from_utf8(&self.oem_id).unwrap()
  102. }
  103. pub fn oem_table_id<'a>(&'a self) -> &'a str {
  104. // Safe to unwrap because checked in `validate`
  105. str::from_utf8(&self.oem_table_id).unwrap()
  106. }
  107. }
  108. #[derive(Clone, Copy, PartialEq, Eq)]
  109. #[repr(transparent)]
  110. pub struct Signature([u8; 4]);
  111. impl Signature {
  112. pub const RSDT: Signature = Signature(*b"RSDT");
  113. pub const XSDT: Signature = Signature(*b"XSDT");
  114. pub const FADT: Signature = Signature(*b"FACP");
  115. pub const HPET: Signature = Signature(*b"HPET");
  116. pub const MADT: Signature = Signature(*b"APIC");
  117. pub const MCFG: Signature = Signature(*b"MCFG");
  118. pub const SSDT: Signature = Signature(*b"SSDT");
  119. pub fn as_str(&self) -> &str {
  120. str::from_utf8(&self.0).unwrap()
  121. }
  122. }
  123. impl fmt::Display for Signature {
  124. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  125. write!(f, "{}", self.as_str())
  126. }
  127. }
  128. impl fmt::Debug for Signature {
  129. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  130. write!(f, "\"{}\"", self.as_str())
  131. }
  132. }
  133. /// Takes the physical address of an SDT, and maps, clones and unmaps its header. Useful for
  134. /// finding out how big it is to map it correctly later.
  135. pub(crate) fn peek_at_sdt_header<H>(handler: &mut H, physical_address: usize) -> SdtHeader
  136. where
  137. H: AcpiHandler,
  138. {
  139. let mapping = handler.map_physical_region::<SdtHeader>(physical_address, mem::size_of::<SdtHeader>());
  140. let header = (*mapping).clone();
  141. handler.unmap_physical_region(mapping);
  142. header
  143. }
  144. /// This takes the physical address of an SDT, maps it correctly and dispatches it to whatever
  145. /// function parses that table.
  146. pub(crate) fn dispatch_sdt<H>(acpi: &mut Acpi, handler: &mut H, physical_address: usize) -> Result<(), AcpiError>
  147. where
  148. H: AcpiHandler,
  149. {
  150. let header = peek_at_sdt_header(handler, physical_address);
  151. trace!("Found ACPI table with signature {:?} and length {:?}", header.signature, header.length);
  152. /*
  153. * For a recognised signature, a new physical mapping should be created with the correct type
  154. * and length, and then the dispatched to the correct function to actually parse the table.
  155. */
  156. match header.signature {
  157. Signature::FADT => {
  158. let fadt_mapping = handler.map_physical_region::<Fadt>(physical_address, mem::size_of::<Fadt>());
  159. crate::fadt::parse_fadt(acpi, handler, &fadt_mapping)?;
  160. handler.unmap_physical_region(fadt_mapping);
  161. }
  162. Signature::HPET => {
  163. let hpet_mapping =
  164. handler.map_physical_region::<HpetTable>(physical_address, mem::size_of::<HpetTable>());
  165. crate::hpet::parse_hpet(acpi, &hpet_mapping)?;
  166. handler.unmap_physical_region(hpet_mapping);
  167. }
  168. Signature::MADT => {
  169. let madt_mapping = handler.map_physical_region::<Madt>(physical_address, header.length as usize);
  170. crate::madt::parse_madt(acpi, handler, &madt_mapping)?;
  171. handler.unmap_physical_region(madt_mapping);
  172. }
  173. Signature::MCFG => {
  174. let mcfg_mapping = handler.map_physical_region::<Mcfg>(physical_address, header.length as usize);
  175. crate::mcfg::parse_mcfg(acpi, &mcfg_mapping)?;
  176. handler.unmap_physical_region(mcfg_mapping);
  177. }
  178. Signature::SSDT => acpi.ssdts.push(AmlTable::new(physical_address, header.length)),
  179. signature => {
  180. /*
  181. * We don't recognise this signature. Early on, this probably just means we don't
  182. * have support yet, but later on maybe this should become an actual error
  183. */
  184. warn!("Unsupported SDT signature: {}. Skipping.", signature);
  185. }
  186. }
  187. Ok(())
  188. }