From 76de467bdcf33168424db92bb9eca782fe23a770 Mon Sep 17 00:00:00 2001 From: xwashere Date: Tue, 9 Apr 2024 14:01:02 -0400 Subject: [PATCH] [lcrash] ADD: Finish off AcpiInitialize --- lcrash/acpi/acpi.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ lcrash/efi/efi.c | 45 +++++++++++++++++++++++++++++++++++++++ lcrash/efi/efi.h | 18 ++++++++++++++++ lcrash/lnxboot.c | 4 ++++ lcrash/main.c | 22 +++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 lcrash/acpi/acpi.c create mode 100644 lcrash/efi/efi.c create mode 100644 lcrash/efi/efi.h create mode 100644 lcrash/lnxboot.c create mode 100644 lcrash/main.c diff --git a/lcrash/acpi/acpi.c b/lcrash/acpi/acpi.c new file mode 100644 index 0000000..82e4a1f --- /dev/null +++ b/lcrash/acpi/acpi.c @@ -0,0 +1,53 @@ +#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; + +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; + + AcpiIsPresent = true; + return; + +fail: AcpiIsPresent = false; + return; +} + +bool AcpiPresent() { return AcpiIsPresent; } +struct AcpiRSDP* AcpiGetRSDP() { return AcpiTheRSDP; } diff --git a/lcrash/efi/efi.c b/lcrash/efi/efi.c new file mode 100644 index 0000000..d4257eb --- /dev/null +++ b/lcrash/efi/efi.c @@ -0,0 +1,45 @@ +#include "efi.h" + +#include "../types.h" +#include "../lnxboot.h" + +#include "./memmap.h" + +static bool IsPresent = false; + +// XXX: This was static but something got fucked up when it was. +// investigate later +const struct EfiSystemTable* SystemTable = 0; + +void EfiInitialize() { + // Get the system table + if ((BootBootParams->efi_info.EfiSystemTable | BootBootParams->efi_info.EfiSystemTableHigh) != 0) { + SystemTable = (struct EfiSystemTable*)( + (uptr)BootBootParams->efi_info.EfiSystemTable | + ((uptr)BootBootParams->efi_info.EfiSystemTableHigh << 32) + ); + } else goto fail; + + // Load the memory map + if ((BootBootParams->efi_info.EfiMemoryMap | BootBootParams->efi_info.EfiMemoryMapHigh) != 0) { + if (BootBootParams->efi_info.EfiMemoryDescriptionSize == 0) goto fail; + + EfiLoadStoredMemoryMap( + (struct EfiMemoryDescription*)( + (u64)BootBootParams->efi_info.EfiMemoryMap | + ((u64)BootBootParams->efi_info.EfiMemoryMapHigh << 32) + ), + BootBootParams->efi_info.EfiMemoryMapSize / BootBootParams->efi_info.EfiMemoryDescriptionSize, + BootBootParams->efi_info.EfiMemoryDescriptionSize + ); + } else goto fail; + + IsPresent = true; + return; + +fail: IsPresent = false; + return; +} + +bool EfiPresent() { return IsPresent; } +const struct EfiSystemTable* EfiGetSystemTable() { return SystemTable; } diff --git a/lcrash/efi/efi.h b/lcrash/efi/efi.h new file mode 100644 index 0000000..7f3ee0a --- /dev/null +++ b/lcrash/efi/efi.h @@ -0,0 +1,18 @@ +#pragma once + +#include "../types.h" + +/** + * Initialize EFI code if EFI is present + */ +void EfiInitialize(); + +/** + * Check if EFI is present + */ +bool EfiPresent(); + +/** + * Get the EFI system table + */ +const struct EfiSystemTable* EfiGetSystemTable(); diff --git a/lcrash/lnxboot.c b/lcrash/lnxboot.c new file mode 100644 index 0000000..76aead2 --- /dev/null +++ b/lcrash/lnxboot.c @@ -0,0 +1,4 @@ +#include "lnxboot.h" + +struct setup_info* BootSetupInfo = 0; +struct boot_params* BootBootParams = 0; diff --git a/lcrash/main.c b/lcrash/main.c new file mode 100644 index 0000000..78d8b16 --- /dev/null +++ b/lcrash/main.c @@ -0,0 +1,22 @@ +/// lcrash entry point +/// super awesome... + +#include "types.h" +#include "lnxboot.h" + +#include "efi/efi.h" + +[[noreturn]] +void entry64(struct boot_params* BootParams) { + BootBootParams = BootParams; + BootSetupInfo = (void*)BootParams + 0x1f1; + + // Initialize EFI code if we had EFI + EfiInitialize(); + + // Initialize ACPI code if we have ACPI + AcpiInitialize(); + + // Hang :) + while (1) {} +}