sdt.rs 6.1 KB

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