rsdp.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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<'a>(&'a self) -> Option<&'a 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 = unsafe {
  86. slice::from_raw_parts(
  87. self as *const _ as *const u8,
  88. RSDPV1_LENGTH + 8
  89. )
  90. };
  91. bytes[8..].iter().fold(0u8, |acc, val| acc.wrapping_add(*val)) == 0
  92. }
  93. /// An OEM-supplied string that identifies the OEM.
  94. pub fn oem_id<'a>(&'a self) -> Option<&'a str> {
  95. str::from_utf8(&self.oem_id).ok()
  96. }
  97. /// The revision of the ACPI.
  98. pub fn revision(&self) -> u8 {
  99. self.revision
  100. }
  101. /// The physical (I repeat: physical) address of the RSDT table.
  102. pub fn rsdt_address(&self) -> usize {
  103. self.rsdt_address as usize
  104. }
  105. }
  106. /// This tag contains a copy of RSDP as defined per ACPI 2.0 or later specification.
  107. #[derive(Clone, Copy, Debug)]
  108. #[repr(C, packed)]
  109. pub struct RsdpV2Tag {
  110. typ: u32,
  111. size: u32,
  112. signature: [u8; 8],
  113. checksum: u8,
  114. oem_id: [u8; 6],
  115. revision: u8,
  116. _rsdt_address: u32,
  117. length: u32,
  118. xsdt_address: u64, // This is the PHYSICAL address of the XSDT
  119. ext_checksum: u8,
  120. _reserved: [u8; 3],
  121. }
  122. impl RsdpV2Tag {
  123. /// The "RSD PTR " marker singature.
  124. ///
  125. /// This is originally a 8-byte C string (not null terminated!) that must contain "RSD PTR ".
  126. pub fn signature<'a>(&'a self) -> Option<&'a str> {
  127. str::from_utf8(&self.signature).ok()
  128. }
  129. /// Validation of the RSDPv2 extended checksum
  130. pub fn checksum_is_valid(&self) -> bool {
  131. let bytes = unsafe {
  132. slice::from_raw_parts(
  133. self as *const _ as *const u8,
  134. self.length as usize + 8
  135. )
  136. };
  137. bytes[8..].iter().fold(0u8, |acc, val| acc.wrapping_add(*val)) == 0
  138. }
  139. /// An OEM-supplied string that identifies the OEM.
  140. pub fn oem_id<'a>(&'a self) -> Option<&'a str> {
  141. str::from_utf8(&self.oem_id).ok()
  142. }
  143. /// The revision of the ACPI.
  144. pub fn revision(&self) -> u8 {
  145. self.revision
  146. }
  147. /// Physical address of the XSDT table.
  148. ///
  149. /// On x86, this is truncated from 64-bit to 32-bit.
  150. pub fn xsdt_address(&self) -> usize {
  151. self.xsdt_address as usize
  152. }
  153. /// This field is used to calculate the checksum of the entire table, including both checksum fields.
  154. pub fn ext_checksum(&self) -> u8 {
  155. self.ext_checksum
  156. }
  157. }