2
0

rsdp_search.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. use crate::{parse_validated_rsdp, rsdp::Rsdp, Acpi, AcpiError, AcpiHandler};
  2. use core::{mem, ops::RangeInclusive};
  3. /// The pointer to the EBDA (Extended Bios Data Area) start segment pointer
  4. const EBDA_START_SEGMENT_PTR: usize = 0x40e;
  5. /// The earliest (lowest) memory address an EBDA (Extended Bios Data Area) can start
  6. const EBDA_EARLIEST_START: usize = 0x80000;
  7. /// The end of the EBDA (Extended Bios Data Area)
  8. const EBDA_END: usize = 0x9ffff;
  9. /// The start of the main bios area below 1mb in which to search for the RSDP
  10. /// (Root System Description Pointer)
  11. const RSDP_BIOS_AREA_START: usize = 0xe0000;
  12. /// The end of the main bios area below 1mb in which to search for the RSDP
  13. /// (Root System Description Pointer)
  14. const RSDP_BIOS_AREA_END: usize = 0xfffff;
  15. /// The RSDP (Root System Description Pointer)'s signature, "RSD PTR " (note trailing space)
  16. const RSDP_SIGNATURE: &'static [u8; 8] = b"RSD PTR ";
  17. /// Find the begining of the EBDA (Extended Bios Data Area) and return `None` if the ptr at
  18. /// `0x40e` is invalid.
  19. pub fn find_search_areas<H>(handler: &mut H) -> [RangeInclusive<usize>; 2]
  20. where
  21. H: AcpiHandler,
  22. {
  23. // Read base segment from BIOS area. This is not always given by the bios, so it needs to be
  24. // checked. We left shift 4 because it is a segment ptr.
  25. let ebda_start_mapping =
  26. handler.map_physical_region::<u16>(EBDA_START_SEGMENT_PTR, mem::size_of::<u16>());
  27. let ebda_start = (*ebda_start_mapping as usize) << 4;
  28. handler.unmap_physical_region(ebda_start_mapping);
  29. [
  30. // Main bios area below 1 mb
  31. // In practice (from my [Restioson's] testing, at least), the RSDP is more often here than
  32. // the in EBDA. Also, if we cannot find the EBDA, then we don't want to search the largest
  33. // possible EBDA first.
  34. RSDP_BIOS_AREA_START..=RSDP_BIOS_AREA_END,
  35. // Check if base segment ptr is in valid range for EBDA base
  36. if (EBDA_EARLIEST_START..EBDA_END).contains(&ebda_start) {
  37. // First kb of EBDA
  38. ebda_start..=ebda_start + 1024
  39. } else {
  40. // We don't know where the EBDA starts, so just search the largest possible EBDA
  41. EBDA_EARLIEST_START..=EBDA_END
  42. },
  43. ]
  44. }
  45. /// This is the entry point of `acpi` if you have no information except that the machine is running
  46. /// BIOS and not UEFI. It maps the RSDP, works out what version of ACPI the hardware supports, and
  47. /// passes the physical address of the RSDT/XSDT to `parse_rsdt`.
  48. ///
  49. /// # Unsafety
  50. ///
  51. /// This function is unsafe because it may read from protected memory if the computer is using UEFI.
  52. /// Only use this function if you are sure the computer is using BIOS.
  53. pub unsafe fn search_for_rsdp_bios<H>(handler: &mut H) -> Result<Acpi, AcpiError>
  54. where
  55. H: AcpiHandler,
  56. {
  57. // The areas that will be searched for the RSDP
  58. let areas = find_search_areas(handler);
  59. // On x86 it is more efficient to map 4096 bytes at a time because of how paging works
  60. let mut area_mapping = handler.map_physical_region::<[[u8; 8]; 0x1000 / 8]>(
  61. areas[0].clone().next().unwrap() & !0xfff, // Get frame addr
  62. 0x1000,
  63. );
  64. // Signature is always on a 16 byte boundary so only search there
  65. for address in areas.iter().flat_map(|i| i.clone()).step_by(16) {
  66. let mut mapping_start = area_mapping.physical_start as usize;
  67. if !(mapping_start..mapping_start + 0x1000).contains(&address) {
  68. handler.unmap_physical_region(area_mapping);
  69. area_mapping = handler.map_physical_region::<[[u8; 8]; 0x1000 / 8]>(
  70. address & !0xfff, // Get frame addr
  71. 0x1000,
  72. );
  73. // Update if mapping remapped
  74. mapping_start = area_mapping.physical_start as usize;
  75. }
  76. let index = (address - mapping_start) / 8;
  77. let signature = (*area_mapping)[index];
  78. if signature != *RSDP_SIGNATURE {
  79. continue;
  80. }
  81. let rsdp_mapping = handler.map_physical_region::<Rsdp>(address, mem::size_of::<Rsdp>());
  82. if let Err(e) = (*rsdp_mapping).validate() {
  83. warn!("Invalid RSDP found at 0x{:x}: {:?}", address, e);
  84. continue;
  85. }
  86. handler.unmap_physical_region(area_mapping);
  87. return parse_validated_rsdp(handler, rsdp_mapping);
  88. }
  89. Err(AcpiError::NoValidRsdp)
  90. }