rsdp.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. use super::AcpiError;
  2. use core::{slice, str};
  3. /// The first structure found in ACPI. It just tells us where the RSDT is.
  4. ///
  5. /// On BIOS systems, it is either found in the first 1KB of the Extended Bios Data Area, or between
  6. /// 0x000E0000 and 0x000FFFFF. The signature is always on a 16 byte boundary. On (U)EFI, it may not
  7. /// be located in these locations, and so an address should be found in the EFI_SYSTEM_TABLE
  8. /// instead.
  9. ///
  10. /// The recommended way of locating the RSDP is to let the bootloader do it - Multiboot2 can pass a
  11. /// tag with the physical address of it. If this is not possible, a manual scan can be done.
  12. ///
  13. /// If `revision > 0`, (the hardware ACPI version is Version 2.0 or greater), the RSDP contains
  14. /// some new fields. For ACPI Version 1.0, these fields are not valid and should not be accessed.
  15. /// For ACPI Version 2.0+, `xsdt_address` should be used (truncated to `u32` on x86) instead of
  16. /// `rsdt_address`.
  17. #[repr(C, packed)]
  18. pub(crate) struct Rsdp {
  19. signature: [u8; 8],
  20. checksum: u8,
  21. oem_id: [u8; 6],
  22. revision: u8,
  23. rsdt_address: u32,
  24. /*
  25. * These fields are only valid for ACPI Version 2.0 and greater
  26. */
  27. length: u32,
  28. xsdt_address: u64,
  29. ext_checksum: u8,
  30. reserved: [u8; 3],
  31. }
  32. impl Rsdp {
  33. /// Checks that:
  34. /// 1) The signature is correct
  35. /// 2) The checksum is correct
  36. /// 3) For Version 2.0+, that the extension checksum is correct
  37. pub(crate) fn validate(&self) -> Result<(), AcpiError> {
  38. const RSDP_V1_LENGTH: usize = 20;
  39. // Check the signature
  40. if &self.signature != b"RSD PTR " {
  41. return Err(AcpiError::RsdpIncorrectSignature);
  42. }
  43. // Check the OEM id is valid UTF8 (allows use of unwrap)
  44. if str::from_utf8(&self.oem_id).is_err() {
  45. return Err(AcpiError::RsdpInvalidOemId);
  46. }
  47. /*
  48. * `self.length` doesn't exist on ACPI version 1.0, so we mustn't rely on it. Instead,
  49. * check for version 1.0 and use a hard-coded length instead.
  50. */
  51. let length = if self.revision > 0 {
  52. // For Version 2.0+, include the number of bytes specified by `length`
  53. self.length as usize
  54. } else {
  55. RSDP_V1_LENGTH
  56. };
  57. let bytes = unsafe { slice::from_raw_parts(self as *const Rsdp as *const u8, length) };
  58. let sum = bytes.iter().fold(0u8, |sum, &byte| sum.wrapping_add(byte));
  59. if sum != 0 {
  60. return Err(AcpiError::RsdpInvalidChecksum);
  61. }
  62. Ok(())
  63. }
  64. pub(crate) fn oem_id<'a>(&'a self) -> &'a str {
  65. str::from_utf8(&self.oem_id).unwrap()
  66. }
  67. pub(crate) fn revision(&self) -> u8 {
  68. self.revision
  69. }
  70. pub(crate) fn rsdt_address(&self) -> u32 {
  71. self.rsdt_address
  72. }
  73. pub(crate) fn xsdt_address(&self) -> u64 {
  74. assert!(self.revision > 0, "Tried to read extended RSDP field with ACPI Version 1.0");
  75. self.xsdt_address
  76. }
  77. }