sdt.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #[repr(C, packed)]
  6. pub struct ExtendedField<T: Copy, const MIN_REVISION: u8>(MaybeUninit<T>);
  7. impl<T: Copy, const MIN_REVISION: u8> ExtendedField<T, MIN_REVISION> {
  8. /// Access the field if it's present for the given revision of the table.
  9. ///
  10. /// ### Safety
  11. /// If a bogus ACPI version is passed, this function may access uninitialised data.
  12. pub unsafe fn access(&self, revision: u8) -> Option<T> {
  13. if revision >= MIN_REVISION {
  14. Some(unsafe { self.0.assume_init() })
  15. } else {
  16. None
  17. }
  18. }
  19. }
  20. /// All SDTs share the same header, and are `length` bytes long. The signature tells us which SDT
  21. /// this is.
  22. ///
  23. /// The ACPI Spec (Version 6.2) defines the following SDT signatures:
  24. /// "APIC" - Multiple APIC Descriptor Table (MADT)
  25. /// "BGRT" - Boot Graphics Resource Table
  26. /// "BERT" - Boot Error Record Table
  27. /// "CPEP" - Corrected Platform Error Polling Table
  28. /// "DSDT" - Differentiated System Descriptor Table
  29. /// "ECDT" - Embedded Controller Boot Resources Table
  30. /// "EINJ" - Error Injection Table
  31. /// "ERST" - Error Record Serialization Table
  32. /// "FACP" - Fixed ACPI Description Table (FADT)
  33. /// "FACS" - Firmware ACPI Control Structure
  34. /// "FPDT" - Firmware Performance Data Table
  35. /// "GTDT" - Generic Timer Description Table
  36. /// "HEST" - Hardware Error Source Table
  37. /// "HMAT" - Heterogeneous Memory Attributes Table
  38. /// "MSCT" - Maximum System Characteristics Table
  39. /// "MPST" - Memory Power State Table
  40. /// "NFIT" - NVDIMM Firmware Interface Table
  41. /// "OEMx" - Various OEM-specific tables
  42. /// "PDTT" - Platform Debug Trigger Table
  43. /// "PMTT" - Platform Memory Topology Table
  44. /// "PPTT" - Processor Properties Topology Table
  45. /// "PSDT" - Persistent System Description Table
  46. /// "RASF" - ACPI RAS Feature Table
  47. /// "RSDT" - Root System Descriptor Table
  48. /// "SBST" - Smart Battery Specification Table
  49. /// "SLIT" - System Locality Information Table
  50. /// "SRAT" - System Resource Affinity Table
  51. /// "SSDT" - Secondary System Description Table
  52. /// "XSDT" - eXtended System Descriptor Table
  53. ///
  54. /// We've come across some more ACPI tables in the wild:
  55. /// "WAET" - Windows ACPI Emulated device 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, PartialOrd, Ord)]
  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: &H, physical_address: usize) -> SdtHeader
  136. where
  137. H: AcpiHandler,
  138. {
  139. let mapping =
  140. unsafe { handler.map_physical_region::<SdtHeader>(physical_address, mem::size_of::<SdtHeader>()) };
  141. (*mapping).clone()
  142. }