79 lines
2.1 KiB
C

#include "acpi.h"
#include "../types.h"
#include "../lnxboot.h"
#include "../util.h"
#include "../efi/memmap.h"
#include "../efi/guid.h"
#include "../efi/efi.h"
bool AcpiIsPresent = false;
struct AcpiRSDP* AcpiTheRSDP = 0;
struct AcpiXSDT* AcpiTheXSDT = 0;
u32 AcpiXSDTLength = 0;
void AcpiInitialize() {
// Check boot params
if (BootBootParams->acpi_rsdp_addr) {
AcpiTheRSDP = BootBootParams->acpi_rsdp_addr;
goto found;
}
// Check firmware
if (EfiPresent()) {
const struct EfiSystemTable* SystemTable = EfiGetSystemTable();
const struct EfiConfigurationTable* ConfigurationTable = EfiTranslatePointer(SystemTable->ConfigurationTable);
for (int i = 0; i < SystemTable->NumberOfTableEntries; i++) { // this used to break because the cckernel decided to give me one last fuck you
// and corrupt random portions of code, i really really hate DEFLATE.
if (CompareMemory(&ConfigurationTable[i].VendorGuid, &EFI_ACPI_20_TABLE_GUID, sizeof(EfiGuid))) {
AcpiTheRSDP = ConfigurationTable[i].VendorTable;
goto found;
}
}
}
goto fail;
found: // Is this a valid ACPI table?
if (!CompareMemory(AcpiTheRSDP->Signature, "RSD PTR ", 8)) goto fail;
// Check the sum
u8 Sum = 0;
for (int i = 0; i < sizeof(struct AcpiRSDP); i++) Sum += ((u8*)AcpiTheRSDP)[i];
if (Sum != 0) goto fail;
// Grab the XSDT
AcpiTheXSDT = AcpiTheRSDP->XSDTAddress;
// Is this a valid XSDT?
if (!CompareMemory(AcpiTheXSDT->Header.Signature, "XSDT", 4)) goto fail;
// Get the XSDT length
AcpiXSDTLength = (AcpiTheXSDT->Header.Length - sizeof(struct AcpiXSDT)) / 8;
AcpiIsPresent = true;
return;
fail: AcpiIsPresent = false;
return;
}
bool AcpiPresent() { return AcpiIsPresent; }
struct AcpiRSDP* AcpiGetRSDP() { return AcpiTheRSDP; }
struct AcpiXSDT* AcpiGetXSDT() { return AcpiTheXSDT; }
u32 AcpiGetXSDTLength() { return AcpiXSDTLength; }
void* AcpiGetTable(const char TableName[4]) {
// Let's not fuck this up this early
if (!AcpiPresent()) return NULL;
// Alright let's see here...
for (int i = 0; i < AcpiXSDTLength; i++) {
}
return 0;
}