Finished Aurix Protocol, AxBoot logs to file now

This commit is contained in:
Jozef Nagy 2025-05-24 21:12:40 +02:00
parent b1d59e02eb
commit d1a5d7d43d
Signed by untrusted user who does not match committer: crz
GPG key ID: 459A4811CEAC7068
37 changed files with 744 additions and 221 deletions

77
boot/platform/uefi/acpi.c Normal file
View file

@ -0,0 +1,77 @@
/*********************************************************************************/
/* Module Name: acpi.c */
/* Project: AurixOS */
/* */
/* Copyright (c) 2024-2025 Jozef Nagy */
/* */
/* This source is subject to the MIT License. */
/* See License.txt in the root of this repository. */
/* All other rights reserved. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/*********************************************************************************/
#include <lib/string.h>
#include <print.h>
#include <stdint.h>
#include <efi.h>
#include <efilib.h>
uintptr_t platform_get_rsdp()
{
EFI_GUID acpi10_guid = EFI_ACPI_10_TABLE_GUID;
EFI_GUID acpi20_guid = EFI_ACPI_20_TABLE_GUID;
uintptr_t rsdp_addr = 0;
for (EFI_UINTN i = 0; i < gSystemTable->NumberOfTableEntries; i++) {
if (memcmp(&(gSystemTable->ConfigurationTable[i].VendorGuid), &acpi10_guid, sizeof(EFI_GUID)) == 0) {
log("platform_get_rsdp(): Found RSDP (ACPI 1.0) at 0x%llx\n", gSystemTable->ConfigurationTable[i].VendorTable);
rsdp_addr = (uintptr_t)gSystemTable->ConfigurationTable[i].VendorTable;
}
// ACPI 2.0+ always takes higher priority over older version
if (memcmp(&(gSystemTable->ConfigurationTable[i].VendorGuid), &acpi20_guid, sizeof(EFI_GUID)) == 0) {
log("platform_get_rsdp(): Found RSDP (ACPI 2.0+) at 0x%llx\n", gSystemTable->ConfigurationTable[i].VendorTable);
return (uintptr_t)gSystemTable->ConfigurationTable[i].VendorTable;
}
}
if (rsdp_addr == 0) {
log("platform_get_rsdp(): RSDP not found!\n");
}
return rsdp_addr;
}
uintptr_t platform_get_smbios()
{
EFI_GUID smbios_guid = SMBIOS_TABLE_GUID;
EFI_GUID smbios3_guid = SMBIOS3_TABLE_GUID;
uintptr_t smbios_addr = 0;
for (EFI_UINTN i = 0; i < gSystemTable->NumberOfTableEntries; i++) {
if (memcmp(&(gSystemTable->ConfigurationTable[i].VendorGuid), &smbios_guid, sizeof(EFI_GUID)) == 0) {
log("platform_get_rsmbios(): Found SMBIOS at 0x%llx\n", gSystemTable->ConfigurationTable[i].VendorTable);
smbios_addr = (uintptr_t)gSystemTable->ConfigurationTable[i].VendorTable;
}
// SMBIOS3 always takes higher priority over older version
if (memcmp(&(gSystemTable->ConfigurationTable[i].VendorGuid), &smbios3_guid, sizeof(EFI_GUID)) == 0) {
log("platform_get_smbios(): Found SMBIOS3 at 0x%llx\n", gSystemTable->ConfigurationTable[i].VendorTable);
return (uintptr_t)gSystemTable->ConfigurationTable[i].VendorTable;
}
}
if (smbios_addr == 0) {
log("platform_get_smbios(): SMBIOS not found!\n");
}
return smbios_addr;
}

View file

@ -36,7 +36,7 @@ bool verify_secure_boot()
bool ret = 0;
if (!EFI_ERROR(gSystemTable->RuntimeServices->GetVariable(L"SecureBoot", &var_guid, NULL, &size, &val))) {
debug("verify_secure_boot(): Secure Boot Status: %u\n", val);
log("verify_secure_boot(): Secure Boot Status: %u\n", val);
ret = (bool)val;
if (!EFI_ERROR(gSystemTable->RuntimeServices->GetVariable(L"SetupMode", &var_guid, NULL, &size, &val)) && val != 0) {
@ -51,14 +51,14 @@ void load_drivers()
{
// EFI_STATUS status;
if (!verify_secure_boot()) {
debug("load_drivers(): Secure boot is enabled! Won't load drivers...\n");
log("load_drivers(): Secure boot is enabled! Won't load drivers...\n");
return;
}
// TODO: Create a vfs_list() function to get a list of files in a directory
// char *driver_path = "\\AxBoot\\drivers\\.efi";
// char *driver_binary;
// debug("load_drivers(): Loading '%s'...\n", driver_name);
// log("load_drivers(): Loading '%s'...\n", driver_name);
//
// size_t driver_size = vfs_read(driver_path, &driver_binary);
//
@ -76,13 +76,13 @@ void load_drivers()
//
// status = gSystemTable->BootServices->LoadImage(EFI_FALSE, gImageHandle, (EFI_DEVICE_PATH_PROTOCOL *)driver_devpath, driver_binary, driver_size, &driver_handle);
// if (EFI_ERROR(status)) {
// debug("load_drivers(): Failed to load driver '%s': %s (%llx)\n", driver_name, efi_status_to_str(status), status);
// log("load_drivers(): Failed to load driver '%s': %s (%llx)\n", driver_name, efi_status_to_str(status), status);
// return;
// }
//
// status = gSystemTable->BootServices->StartImage(driver_handle, NULL, NULL);
// if (EFI_ERROR(status)) {
// debug("load_drivers(): Failed to start driver '%s': %s (%llx)\n", driver_name, efi_status_to_str(status), status);
// log("load_drivers(): Failed to start driver '%s': %s (%llx)\n", driver_name, efi_status_to_str(status), status);
// return;
// }
}

View file

@ -23,15 +23,12 @@
#include <axboot.h>
#include <mm/mman.h>
#include <lib/string.h>
#include <power.h>
#include <print.h>
#include <stdint.h>
#include <stddef.h>
#define INI_IMPLEMENTATION
#include <config/ini.h>
EFI_HANDLE gImageHandle;
EFI_SYSTEM_TABLE *gSystemTable;
EFI_BOOT_SERVICES *gBootServices;
@ -96,3 +93,50 @@ EFI_STATUS uefi_entry(EFI_HANDLE ImageHandle,
axboot_init();
UNREACHABLE();
}
#define EXIT_BS_MAX_TRIES 10
void uefi_exit_bs(void)
{
EFI_UINTN map_key = 0;
EFI_UINTN map_size = 0;
EFI_MEMORY_DESCRIPTOR *map = NULL;
EFI_UINTN desc_size = 0;
EFI_UINT32 desc_ver = 0;
EFI_STATUS status;
int tries = 0;
do {
map_key = 0;
map_size = 0;
desc_size = 0;
desc_ver = 0;
map = NULL;
tries++;
debug("uefi_exit_bs(): Trying to exit Boot Services, try %u/%u...\n", tries, EXIT_BS_MAX_TRIES);
status = gBootServices->GetMemoryMap(&map_size, map, &map_key, &desc_size, &desc_ver);
if (EFI_ERROR(status) && status != EFI_BUFFER_TOO_SMALL) {
log("uefi_exit_bs(): Failed to acquire memory map key: %s (%llx)\n", efi_status_to_str(status), status);
continue;
}
status = gBootServices->ExitBootServices(gImageHandle, map_key);
if (EFI_ERROR(status)) {
log("uefi_exit_bs(): Failed to exit boot services: %s (%llx)\n", efi_status_to_str(status), status);
continue;
}
break;
} while (tries <= EXIT_BS_MAX_TRIES);
if (EFI_ERROR(status)) {
log("uefi_exit_bs(): Failed to exit Boot Services, rebooting!");
platform_reboot();
UNREACHABLE();
}
debug("uefi_exit_bs(): ExitBootServices() success!\n");
}

View file

@ -30,42 +30,46 @@
static int efi_type_to_axboot(uint32_t efi_type)
{
switch (efi_type) {
case EfiReservedMemoryType:
return MemMapReserved;
case EfiLoaderCode:
return MemMapFirmware;
case EfiLoaderData:
return MemMapFirmware;
case EfiBootServicesCode:
case EfiBootServicesData:
return MemMapFreeOnLoad;
case EfiRuntimeServicesCode:
case EfiRuntimeServicesData:
case EfiLoaderCode:
return MemMapFirmware;
case EfiConventionalMemory:
return MemMapUsable;
case EfiUnusableMemory:
return MemMapFaulty;
case EfiACPIReclaimMemory:
return MemMapACPIReclaimable;
case EfiACPIMemoryNVS:
return MemMapACPINVS;
case EfiMemoryMappedIO:
return MemMapACPIMappedIO;
case EfiMemoryMappedIOPortSpace:
return MemMapACPIMappedIOPortSpace;
case EfiPalCode:
return MemMapUsable;
case EfiPersistentMemory:
case EfiConventionalMemory:
return MemMapUsable;
return MemMapUsable;
case EfiUnacceptedMemoryType:
case EfiReservedMemoryType:
return MemMapReserved;
default:
return MemMapReserved;
}
}
axboot_memmap *get_memmap(pagetable *pm)
uint32_t get_memmap(axboot_memmap **map, pagetable *pm)
{
EFI_MEMORY_DESCRIPTOR *efi_map = NULL;
EFI_UINTN efi_map_key = 0;
@ -76,8 +80,8 @@ axboot_memmap *get_memmap(pagetable *pm)
status = gBootServices->GetMemoryMap(&size, efi_map, &efi_map_key, &desc_size, &desc_ver);
if (EFI_ERROR(status) && status != EFI_BUFFER_TOO_SMALL) {
debug("get_memmap(): GetMemoryMap() returned an error: %s (0x%llx)\n", efi_status_to_str(status), status);
return NULL;
log("get_memmap(): GetMemoryMap() returned an error: %s (0x%llx)\n", efi_status_to_str(status), status);
return 0;
}
efi_map = (EFI_MEMORY_DESCRIPTOR *)mem_alloc(size);
@ -90,8 +94,8 @@ axboot_memmap *get_memmap(pagetable *pm)
size += 2 * desc_size;
efi_map = (EFI_MEMORY_DESCRIPTOR *)mem_realloc(efi_map, size);
} else {
debug("get_memmap(): GetMemoryMap() returned an error: %s (0x%llx)\n", efi_status_to_str(status), status);
return NULL;
log("get_memmap(): GetMemoryMap() returned an error: %s (0x%llx)\n", efi_status_to_str(status), status);
return 0;
}
} while (status != EFI_SUCCESS);
@ -133,23 +137,19 @@ axboot_memmap *get_memmap(pagetable *pm)
cur_entry = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)cur_entry + desc_size);
}
axboot_memmap *new_map = (axboot_memmap *)mem_alloc(sizeof(axboot_memmap) * entry_count);
memset(new_map, 0, sizeof(axboot_memmap) * entry_count);
cur_entry = efi_map;
*map = mem_alloc(sizeof(axboot_memmap) * entry_count);
memset(*map, 0, sizeof(axboot_memmap) * entry_count);
// translate efi memmap to axboot memmap
for (uint32_t i = 0; i < entry_count; i++) {
new_map[i].base = cur_entry->PhysicalStart;
new_map[i].size = cur_entry->NumberOfPages * PAGE_SIZE;
new_map[i].type = efi_type_to_axboot(efi_map[i].Type);
debug("get_memmap(): Entry %d: base=0x%llx, size=%u bytes, type=%x\n", i, new_map[i].base, new_map[i].size, new_map[i].type);
(*map)[i].base = cur_entry->PhysicalStart;
(*map)[i].size = cur_entry->NumberOfPages * PAGE_SIZE;
(*map)[i].type = efi_type_to_axboot(efi_map[i].Type);
cur_entry = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)cur_entry + desc_size);
}
mem_free(efi_map);
return new_map;
return entry_count;
}

View file

@ -35,11 +35,11 @@ bool platform_is_reboot_to_fw_possible()
Status = gSystemTable->RuntimeServices->GetVariable(L"OsIndicationsSupported", &GlobalVarGuid, NULL, &OsIndicationsSize, &OsIndications);
if (!EFI_ERROR(Status)) {
if (OsIndications & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) {
debug("platform_is_reboot_to_fw_possible(): Boot to firmware UI is possible!\n");
log("platform_is_reboot_to_fw_possible(): Boot to firmware UI is possible!\n");
return true;
}
} else {
debug("platform_is_reboot_to_fw_possible(): Failed to get OsIndications variable: 0x%llx (%s) \n", Status, efi_status_to_str(Status));
log("platform_is_reboot_to_fw_possible(): Failed to get OsIndications variable: 0x%llx (%s) \n", Status, efi_status_to_str(Status));
}
return false;
@ -58,7 +58,7 @@ void platform_reboot_to_fw()
Status = gSystemTable->RuntimeServices->SetVariable(L"OsIndications", &GlobalVarGuid, EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, OsIndicationsSize, &OsIndications);
if (EFI_ERROR(Status)) {
debug("platform_reboot_to_fw(): Failed to set OsIndications variable: 0x%llx (%s) \n", Status, efi_status_to_str(Status));
log("platform_reboot_to_fw(): Failed to set OsIndications variable: 0x%llx (%s) \n", Status, efi_status_to_str(Status));
}
gSystemTable->RuntimeServices->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);

View file

@ -33,7 +33,7 @@ void get_datetime(struct datetime *dt)
status = gSystemTable->RuntimeServices->GetTime(&uefi_dt, NULL);
if (EFI_ERROR(status)) {
debug("get_datetime(): Failed to acquire current time, setting to 1970/01/01 00:00:00!\n");
log("get_datetime(): Failed to acquire current time, setting to 1970/01/01 00:00:00!\n");
return;
}

View file

@ -40,7 +40,7 @@ bool get_framebuffer(uint32_t **fb_addr, struct fb_mode **available_modes, int *
Status = gBootServices->LocateProtocol(&gop_guid, NULL, (void**)&gop);
if (EFI_ERROR(Status)) {
debug("get_framebuffer(): Unable to locate GOP: %s (0x%llx)\n", efi_status_to_str(Status), Status);
log("get_framebuffer(): Unable to locate GOP: %s (0x%llx)\n", efi_status_to_str(Status), Status);
}
Status = gop->QueryMode(gop, gop->Mode == NULL ? 0 : gop->Mode->Mode, &SizeOfInfo, &mode_info);
@ -48,14 +48,16 @@ bool get_framebuffer(uint32_t **fb_addr, struct fb_mode **available_modes, int *
if (Status == EFI_NOT_STARTED) {
Status = gop->SetMode(gop, 0);
} else if (EFI_ERROR(Status)) {
debug("Unable to get native mode\n");
log("Unable to get native mode\n");
return false;
} else {
nativeMode = gop->Mode->Mode;
numModes = gop->Mode->MaxMode;
}
*total_modes = numModes;
if (total_modes != NULL) {
*total_modes = numModes;
}
*available_modes = (struct fb_mode *)mem_alloc(sizeof(struct fb_mode) * numModes);
*fb_addr = (uint32_t *)gop->Mode->FrameBufferBase;
@ -74,7 +76,7 @@ bool get_framebuffer(uint32_t **fb_addr, struct fb_mode **available_modes, int *
} else if (mode_info->PixelFormat == PixelBlueGreenRedReserved8BitPerColor) {
(*available_modes)[i].format = FB_BGRA;
} else {
debug("get_framebuffer(): Unknown framebuffer format, assuming BGRA...\n");
log("get_framebuffer(): Unknown framebuffer format, assuming BGRA...\n");
(*available_modes)[i].format = FB_BGRA;
}