sdt.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. use crate::{fadt::Fadt, hpet::HpetTable, madt::Madt, mcfg::Mcfg, Acpi, AcpiError, AcpiHandler, AmlTable};
  2. use core::{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. signature: [u8; 4],
  60. length: u32,
  61. revision: u8,
  62. checksum: u8,
  63. oem_id: [u8; 6],
  64. oem_table_id: [u8; 8],
  65. oem_revision: u32,
  66. creator_id: u32,
  67. 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: &[u8; 4]) -> 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 raw_signature(&self) -> [u8; 4] {
  100. self.signature
  101. }
  102. pub fn signature<'a>(&'a self) -> &'a str {
  103. // Safe to unwrap because we check signature is valid UTF8 in `validate`
  104. str::from_utf8(&self.signature).unwrap()
  105. }
  106. pub fn length(&self) -> u32 {
  107. self.length
  108. }
  109. pub fn revision(&self) -> u8 {
  110. self.revision
  111. }
  112. pub fn oem_id<'a>(&'a self) -> &'a str {
  113. // Safe to unwrap because checked in `validate`
  114. str::from_utf8(&self.oem_id).unwrap()
  115. }
  116. pub fn oem_table_id<'a>(&'a self) -> &'a str {
  117. // Safe to unwrap because checked in `validate`
  118. str::from_utf8(&self.oem_table_id).unwrap()
  119. }
  120. }
  121. /// Takes the physical address of an SDT, and maps, clones and unmaps its header. Useful for
  122. /// finding out how big it is to map it correctly later.
  123. pub(crate) fn peek_at_sdt_header<H>(handler: &mut H, physical_address: usize) -> SdtHeader
  124. where
  125. H: AcpiHandler,
  126. {
  127. let mapping = handler.map_physical_region::<SdtHeader>(physical_address, mem::size_of::<SdtHeader>());
  128. let header = (*mapping).clone();
  129. handler.unmap_physical_region(mapping);
  130. header
  131. }
  132. /// This takes the physical address of an SDT, maps it correctly and dispatches it to whatever
  133. /// function parses that table.
  134. pub(crate) fn dispatch_sdt<H>(acpi: &mut Acpi, handler: &mut H, physical_address: usize) -> Result<(), AcpiError>
  135. where
  136. H: AcpiHandler,
  137. {
  138. let header = peek_at_sdt_header(handler, physical_address);
  139. trace!("Found ACPI table with signature {:?} and length {:?}", header.signature(), header.length());
  140. /*
  141. * For a recognised signature, a new physical mapping should be created with the correct type
  142. * and length, and then the dispatched to the correct function to actually parse the table.
  143. */
  144. match header.signature() {
  145. "FACP" => {
  146. let fadt_mapping = handler.map_physical_region::<Fadt>(physical_address, mem::size_of::<Fadt>());
  147. crate::fadt::parse_fadt(acpi, handler, &fadt_mapping)?;
  148. handler.unmap_physical_region(fadt_mapping);
  149. }
  150. "HPET" => {
  151. let hpet_mapping =
  152. handler.map_physical_region::<HpetTable>(physical_address, mem::size_of::<HpetTable>());
  153. crate::hpet::parse_hpet(acpi, &hpet_mapping)?;
  154. handler.unmap_physical_region(hpet_mapping);
  155. }
  156. "APIC" => {
  157. let madt_mapping = handler.map_physical_region::<Madt>(physical_address, header.length() as usize);
  158. crate::madt::parse_madt(acpi, handler, &madt_mapping)?;
  159. handler.unmap_physical_region(madt_mapping);
  160. }
  161. "MCFG" => {
  162. let mcfg_mapping = handler.map_physical_region::<Mcfg>(physical_address, header.length() as usize);
  163. crate::mcfg::parse_mcfg(acpi, &mcfg_mapping)?;
  164. handler.unmap_physical_region(mcfg_mapping);
  165. }
  166. "SSDT" => acpi.ssdts.push(AmlTable::new(physical_address, header.length())),
  167. signature => {
  168. /*
  169. * We don't recognise this signature. Early on, this probably just means we don't
  170. * have support yet, but later on maybe this should become an actual error
  171. */
  172. warn!("Unsupported SDT signature: {}. Skipping.", signature);
  173. }
  174. }
  175. Ok(())
  176. }