|
6 | 6 |
|
7 | 7 | #include <Kernel/Arch/x86_64/Firmware/ACPI.h> |
8 | 8 | #include <Kernel/Arch/x86_64/Firmware/PCBIOS/Mapper.h> |
| 9 | +#include <Kernel/Firmware/ACPI/Definitions.h> |
9 | 10 | #include <Kernel/Memory/MemoryManager.h> |
10 | 11 |
|
11 | 12 | namespace Kernel::ACPI::StaticParsing { |
12 | 13 |
|
| 14 | +static bool is_rsdp_valid(u8 const* rsdp, size_t region_size) |
| 15 | +{ |
| 16 | + if (region_size < sizeof(Structures::RSDPDescriptor)) |
| 17 | + return false; |
| 18 | + |
| 19 | + u8 revision = reinterpret_cast<Structures::RSDPDescriptor const*>(rsdp)->revision; |
| 20 | + |
| 21 | + u8 checksum = 0; |
| 22 | + for (size_t i = 0; i < sizeof(Structures::RSDPDescriptor); ++i) |
| 23 | + checksum += rsdp[i]; |
| 24 | + |
| 25 | + if (checksum != 0) |
| 26 | + return false; |
| 27 | + |
| 28 | + if (revision == 0) |
| 29 | + // Checksum matched and there's nothing more to check. |
| 30 | + return true; |
| 31 | + |
| 32 | + if (region_size < sizeof(Structures::RSDPDescriptor20)) |
| 33 | + return false; |
| 34 | + |
| 35 | + checksum = 0; |
| 36 | + for (size_t i = 0; i < sizeof(Structures::RSDPDescriptor20); ++i) |
| 37 | + checksum += rsdp[i]; |
| 38 | + return checksum == 0; |
| 39 | +} |
| 40 | + |
13 | 41 | // https://uefi.org/specs/ACPI/6.5/05_ACPI_Software_Programming_Model.html#finding-the-rsdp-on-ia-pc-systems |
14 | 42 | Optional<PhysicalAddress> find_rsdp_in_ia_pc_specific_memory_locations() |
15 | 43 | { |
16 | 44 | constexpr auto signature = "RSD PTR "sv; |
| 45 | + |
| 46 | + auto locate_rsdp = [&signature](Memory::MappedROM mapping) -> Optional<PhysicalAddress> { |
| 47 | + while (true) { |
| 48 | + u8 const* rsdp = mapping.pointer_to_chunk_starting_with(signature, 16); |
| 49 | + if (!rsdp) |
| 50 | + return {}; |
| 51 | + if (is_rsdp_valid(rsdp, static_cast<size_t>(mapping.end() - rsdp))) |
| 52 | + return mapping.paddr_of(rsdp); |
| 53 | + mapping.offset = (mapping.paddr_of(rsdp).get() - mapping.paddr.get()) + signature.length(); |
| 54 | + mapping.size -= static_cast<size_t>(rsdp - mapping.base()) + signature.length(); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
17 | 58 | auto ebda_or_error = map_ebda(); |
18 | 59 | if (!ebda_or_error.is_error()) { |
19 | | - auto rsdp = ebda_or_error.value().find_chunk_starting_with(signature, 16); |
20 | | - if (rsdp.has_value()) |
21 | | - return rsdp; |
| 60 | + auto maybe_rsdp = locate_rsdp(ebda_or_error.release_value()); |
| 61 | + if (maybe_rsdp.has_value()) |
| 62 | + return maybe_rsdp.value(); |
22 | 63 | } |
23 | 64 | auto bios_or_error = map_bios(); |
24 | 65 | if (!bios_or_error.is_error()) { |
25 | | - auto rsdp = bios_or_error.value().find_chunk_starting_with(signature, 16); |
26 | | - if (rsdp.has_value()) |
27 | | - return rsdp; |
| 66 | + auto maybe_rsdp = locate_rsdp(bios_or_error.release_value()); |
| 67 | + if (maybe_rsdp.has_value()) |
| 68 | + return maybe_rsdp.value(); |
28 | 69 | } |
29 | 70 |
|
30 | 71 | // On some systems the RSDP may be located in ACPI NVS or reclaimable memory regions |
@@ -52,9 +93,9 @@ Optional<PhysicalAddress> find_rsdp_in_ia_pc_specific_memory_locations() |
52 | 93 | mapping.size = memory_range.length; |
53 | 94 | mapping.paddr = memory_range.start; |
54 | 95 |
|
55 | | - auto rsdp = mapping.find_chunk_starting_with(signature, 16); |
56 | | - if (rsdp.has_value()) |
57 | | - return rsdp; |
| 96 | + auto maybe_rsdp = locate_rsdp(move(mapping)); |
| 97 | + if (maybe_rsdp.has_value()) |
| 98 | + return maybe_rsdp.value(); |
58 | 99 | } |
59 | 100 |
|
60 | 101 | return {}; |
|
0 commit comments