rsdp.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /// The tag that the bootloader passes will depend on the ACPI version the hardware supports.
  2. /// For ACPI Version 1.0, a `RsdpV1Tag` will be provided, which can be accessed from
  3. /// `BootInformation` using the `rsdp_v1_tag` function. For subsequent versions of ACPI, a
  4. /// `RsdpV2Tag` will be provided, which can be accessed with `rsdp_v2_tag`.
  5. ///
  6. /// Even though the bootloader should give the address of the real RSDP/XSDT, the checksum and
  7. /// signature should be manually verified.
  8. use core::slice;
  9. use core::str;
  10. const RSDPV1_LENGTH: usize = 20;
  11. /// EFI system table in 32 bit mode
  12. #[derive(Clone, Copy, Debug)]
  13. #[repr(C, packed)]
  14. pub struct EFISdt32 {
  15. typ: u32,
  16. size: u32,
  17. pointer: u32,
  18. }
  19. impl EFISdt32 {
  20. /// The Physical address of a i386 EFI system table.
  21. pub fn sdt_address(&self) -> usize {
  22. self.pointer as usize
  23. }
  24. }
  25. /// EFI system table in 64 bit mode
  26. #[derive(Clone, Copy, Debug)]
  27. #[repr(C, packed)]
  28. pub struct EFISdt64 {
  29. typ: u32,
  30. size: u32,
  31. pointer: u64,
  32. }
  33. impl EFISdt64 {
  34. /// The Physical address of a x86_64 EFI system table.
  35. pub fn sdt_address(&self) -> usize {
  36. self.pointer as usize
  37. }
  38. }
  39. /// This tag contains a copy of RSDP as defined per ACPI 1.0 specification.
  40. #[derive(Clone, Copy, Debug)]
  41. #[repr(C, packed)]
  42. pub struct RsdpV1Tag {
  43. typ: u32,
  44. size: u32,
  45. signature: [u8; 8],
  46. checksum: u8,
  47. oem_id: [u8; 6],
  48. revision: u8,
  49. rsdt_address: u32, // This is the PHYSICAL address of the RSDT
  50. }
  51. impl RsdpV1Tag {
  52. /// The "RSD PTR " marker singature.
  53. ///
  54. /// This is originally a 8-byte C string (not null terminated!) that must contain "RSD PTR "
  55. pub fn signature<'a>(&'a self) -> Option<&'a str> {
  56. str::from_utf8(&self.signature).ok()
  57. }
  58. /// Validation of the RSDPv1 checksum
  59. pub fn checksum_is_valid(&self) -> bool {
  60. let bytes = unsafe {
  61. slice::from_raw_parts(
  62. self as *const _ as *const u8,
  63. RSDPV1_LENGTH + 8
  64. )
  65. };
  66. bytes[8..].iter().fold(0u8, |acc, val| acc.wrapping_add(*val)) == 0
  67. }
  68. /// An OEM-supplied string that identifies the OEM.
  69. pub fn oem_id<'a>(&'a self) -> Option<&'a str> {
  70. str::from_utf8(&self.oem_id).ok()
  71. }
  72. /// The revision of the ACPI.
  73. pub fn revision(&self) -> u8 {
  74. self.revision
  75. }
  76. /// The physical (I repeat: physical) address of the RSDT table.
  77. pub fn rsdt_address(&self) -> usize {
  78. self.rsdt_address as usize
  79. }
  80. }
  81. /// This tag contains a copy of RSDP as defined per ACPI 2.0 or later specification.
  82. #[derive(Clone, Copy, Debug)]
  83. #[repr(C, packed)]
  84. pub struct RsdpV2Tag {
  85. typ: u32,
  86. size: u32,
  87. signature: [u8; 8],
  88. checksum: u8,
  89. oem_id: [u8; 6],
  90. revision: u8,
  91. _rsdt_address: u32,
  92. length: u32,
  93. xsdt_address: u64, // This is the PHYSICAL address of the XSDT
  94. ext_checksum: u8,
  95. _reserved: [u8; 3],
  96. }
  97. impl RsdpV2Tag {
  98. /// The "RSD PTR " marker singature.
  99. ///
  100. /// This is originally a 8-byte C string (not null terminated!) that must contain "RSD PTR ".
  101. pub fn signature<'a>(&'a self) -> Option<&'a str> {
  102. str::from_utf8(&self.signature).ok()
  103. }
  104. /// Validation of the RSDPv2 extended checksum
  105. pub fn checksum_is_valid(&self) -> bool {
  106. let bytes = unsafe {
  107. slice::from_raw_parts(
  108. self as *const _ as *const u8,
  109. self.length as usize + 8
  110. )
  111. };
  112. bytes[8..].iter().fold(0u8, |acc, val| acc.wrapping_add(*val)) == 0
  113. }
  114. /// An OEM-supplied string that identifies the OEM.
  115. pub fn oem_id<'a>(&'a self) -> Option<&'a str> {
  116. str::from_utf8(&self.oem_id).ok()
  117. }
  118. /// The revision of the ACPI.
  119. pub fn revision(&self) -> u8 {
  120. self.revision
  121. }
  122. /// Physical address of the XSDT table.
  123. ///
  124. /// On x86, this is truncated from 64-bit to 32-bit.
  125. pub fn xsdt_address(&self) -> usize {
  126. self.xsdt_address as usize
  127. }
  128. /// This field is used to calculate the checksum of the entire table, including both checksum fields.
  129. pub fn ext_checksum(&self) -> u8 {
  130. self.ext_checksum
  131. }
  132. }