rsdp.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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)] // only repr(C) would add unwanted padding before first_section
  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)]
  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. /// Contains pointer to boot loader image handle.
  40. #[derive(Debug)]
  41. #[repr(C)]
  42. pub struct EFIImageHandle32 {
  43. typ: u32,
  44. size: u32,
  45. pointer: u32,
  46. }
  47. /// Contains pointer to boot loader image handle.
  48. #[derive(Debug)]
  49. #[repr(C)]
  50. pub struct EFIImageHandle64 {
  51. typ: u32,
  52. size: u32,
  53. pointer: u64,
  54. }
  55. /// If the image has relocatable header tag, this tag contains the image's
  56. /// base physical address.
  57. #[derive(Debug)]
  58. #[repr(C)]
  59. pub struct ImageLoadPhysAddr {
  60. typ: u32,
  61. size: u32,
  62. load_base_addr: u32,
  63. }
  64. /// This tag contains a copy of RSDP as defined per ACPI 1.0 specification.
  65. #[derive(Clone, Copy, Debug)]
  66. #[repr(C, packed)]
  67. pub struct RsdpV1Tag {
  68. typ: u32,
  69. size: u32,
  70. signature: [u8; 8],
  71. checksum: u8,
  72. oem_id: [u8; 6],
  73. revision: u8,
  74. rsdt_address: u32, // This is the PHYSICAL address of the RSDT
  75. }
  76. impl RsdpV1Tag {
  77. /// The "RSD PTR " marker singature.
  78. ///
  79. /// This is originally a 8-byte C string (not null terminated!) that must contain "RSD PTR "
  80. pub fn signature(&self) -> Option<&str> {
  81. str::from_utf8(&self.signature).ok()
  82. }
  83. /// Validation of the RSDPv1 checksum
  84. pub fn checksum_is_valid(&self) -> bool {
  85. let bytes =
  86. unsafe { slice::from_raw_parts(self as *const _ as *const u8, RSDPV1_LENGTH + 8) };
  87. bytes[8..]
  88. .iter()
  89. .fold(0u8, |acc, val| acc.wrapping_add(*val))
  90. == 0
  91. }
  92. /// An OEM-supplied string that identifies the OEM.
  93. pub fn oem_id(&self) -> Option<&str> {
  94. str::from_utf8(&self.oem_id).ok()
  95. }
  96. /// The revision of the ACPI.
  97. pub fn revision(&self) -> u8 {
  98. self.revision
  99. }
  100. /// The physical (I repeat: physical) address of the RSDT table.
  101. pub fn rsdt_address(&self) -> usize {
  102. self.rsdt_address as usize
  103. }
  104. }
  105. /// This tag contains a copy of RSDP as defined per ACPI 2.0 or later specification.
  106. #[derive(Clone, Copy, Debug)]
  107. #[repr(C, packed)]
  108. pub struct RsdpV2Tag {
  109. typ: u32,
  110. size: u32,
  111. signature: [u8; 8],
  112. checksum: u8,
  113. oem_id: [u8; 6],
  114. revision: u8,
  115. _rsdt_address: u32,
  116. length: u32,
  117. xsdt_address: u64, // This is the PHYSICAL address of the XSDT
  118. ext_checksum: u8,
  119. _reserved: [u8; 3],
  120. }
  121. impl RsdpV2Tag {
  122. /// The "RSD PTR " marker singature.
  123. ///
  124. /// This is originally a 8-byte C string (not null terminated!) that must contain "RSD PTR ".
  125. pub fn signature(&self) -> Option<&str> {
  126. str::from_utf8(&self.signature).ok()
  127. }
  128. /// Validation of the RSDPv2 extended checksum
  129. pub fn checksum_is_valid(&self) -> bool {
  130. let bytes = unsafe {
  131. slice::from_raw_parts(self as *const _ as *const u8, self.length as usize + 8)
  132. };
  133. bytes[8..]
  134. .iter()
  135. .fold(0u8, |acc, val| acc.wrapping_add(*val))
  136. == 0
  137. }
  138. /// An OEM-supplied string that identifies the OEM.
  139. pub fn oem_id(&self) -> Option<&str> {
  140. str::from_utf8(&self.oem_id).ok()
  141. }
  142. /// The revision of the ACPI.
  143. pub fn revision(&self) -> u8 {
  144. self.revision
  145. }
  146. /// Physical address of the XSDT table.
  147. ///
  148. /// On x86, this is truncated from 64-bit to 32-bit.
  149. pub fn xsdt_address(&self) -> usize {
  150. self.xsdt_address as usize
  151. }
  152. /// This field is used to calculate the checksum of the entire table, including both checksum fields.
  153. pub fn ext_checksum(&self) -> u8 {
  154. self.ext_checksum
  155. }
  156. }