Formatting fixes and removed commented code only.

This commit is contained in:
Justin C. Miller
2018-03-23 09:04:51 -07:00
parent cdef82f06f
commit d438392ed5
7 changed files with 173 additions and 189 deletions

View File

@@ -4,75 +4,75 @@
static CHAR16 kernel_name[] = KERNEL_FILENAME;
EFI_STATUS loader_load_kernel(void **kernel_image, uint64_t *length) {
if (kernel_image == 0 || length == 0)
CHECK_EFI_STATUS_OR_RETURN(EFI_INVALID_PARAMETER, "NULL kernel_image or length pointer");
if (kernel_image == 0 || length == 0)
CHECK_EFI_STATUS_OR_RETURN(EFI_INVALID_PARAMETER, "NULL kernel_image or length pointer");
EFI_STATUS status;
EFI_GUID guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
EFI_HANDLE *handles = NULL;
UINTN handleCount = 0;
EFI_STATUS status;
EFI_GUID guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
EFI_HANDLE *handles = NULL;
UINTN handleCount = 0;
status = ST->BootServices->LocateHandleBuffer(
ByProtocol, &guid, NULL, &handleCount, &handles);
status = ST->BootServices->LocateHandleBuffer(
ByProtocol, &guid, NULL, &handleCount, &handles);
CHECK_EFI_STATUS_OR_RETURN(status, "LocateHandleBuffer");
for (unsigned i=0; i<handleCount; ++i) {
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fileSystem = NULL;
for (unsigned i=0; i<handleCount; ++i) {
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fileSystem = NULL;
status = ST->BootServices->HandleProtocol(
handles[i], &guid, (void**)&fileSystem);
CHECK_EFI_STATUS_OR_RETURN(status, "HandleProtocol");
status = ST->BootServices->HandleProtocol(
handles[i], &guid, (void**)&fileSystem);
CHECK_EFI_STATUS_OR_RETURN(status, "HandleProtocol");
EFI_FILE_PROTOCOL *root = NULL;
status = fileSystem->OpenVolume(fileSystem, &root);
CHECK_EFI_STATUS_OR_RETURN(status, "OpenVolume");
EFI_FILE_PROTOCOL *root = NULL;
status = fileSystem->OpenVolume(fileSystem, &root);
CHECK_EFI_STATUS_OR_RETURN(status, "OpenVolume");
EFI_FILE_PROTOCOL *file = NULL;
status = root->Open(
root,
&file,
kernel_name,
EFI_FILE_MODE_READ,
EFI_FILE_READ_ONLY|EFI_FILE_HIDDEN|EFI_FILE_SYSTEM);
EFI_FILE_PROTOCOL *file = NULL;
status = root->Open(
root,
&file,
kernel_name,
EFI_FILE_MODE_READ,
EFI_FILE_READ_ONLY|EFI_FILE_HIDDEN|EFI_FILE_SYSTEM);
if (!EFI_ERROR(status)) {
void *buffer = NULL;
EFI_GUID file_info_guid = EFI_FILE_INFO_ID;
UINTN buffer_size = sizeof(EFI_FILE_INFO) + sizeof(kernel_name);
if (!EFI_ERROR(status)) {
void *buffer = NULL;
EFI_GUID file_info_guid = EFI_FILE_INFO_ID;
UINTN buffer_size = sizeof(EFI_FILE_INFO) + sizeof(kernel_name);
status = ST->BootServices->AllocatePool(EfiLoaderCode, buffer_size, &buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel file info memory");
status = ST->BootServices->AllocatePool(EfiLoaderCode, buffer_size, &buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel file info memory");
status = file->GetInfo(file, &file_info_guid, &buffer_size, buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Getting kernel file info");
status = file->GetInfo(file, &file_info_guid, &buffer_size, buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Getting kernel file info");
buffer_size = ((EFI_FILE_INFO*)buffer)->FileSize;
buffer_size = ((EFI_FILE_INFO*)buffer)->FileSize;
status = ST->BootServices->FreePool(buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Freeing kernel file info memory");
status = ST->BootServices->FreePool(buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Freeing kernel file info memory");
UINTN page_count = ((buffer_size - 1) / 0x1000) + 1;
EFI_PHYSICAL_ADDRESS addr = KERNEL_PHYS_ADDRESS; // Try to load the kernel in at 1MiB
EFI_MEMORY_TYPE mem_type = KERNEL_MEMTYPE; // Special value to tell the kernel it's here
status = ST->BootServices->AllocatePages(AllocateAddress, mem_type, page_count, &addr);
if (status == EFI_NOT_FOUND) {
// couldn't get the address we wanted, try loading the kernel anywhere
status = ST->BootServices->AllocatePages(AllocateAnyPages, mem_type, page_count, &addr);
}
CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel pages");
UINTN page_count = ((buffer_size - 1) / 0x1000) + 1;
EFI_PHYSICAL_ADDRESS addr = KERNEL_PHYS_ADDRESS; // Try to load the kernel in at 1MiB
EFI_MEMORY_TYPE mem_type = KERNEL_MEMTYPE; // Special value to tell the kernel it's here
status = ST->BootServices->AllocatePages(AllocateAddress, mem_type, page_count, &addr);
if (status == EFI_NOT_FOUND) {
// couldn't get the address we wanted, try loading the kernel anywhere
status = ST->BootServices->AllocatePages(AllocateAnyPages, mem_type, page_count, &addr);
}
CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel pages");
buffer = (void*)addr;
status = file->Read(file, &buffer_size, buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Reading kernel file");
buffer = (void*)addr;
status = file->Read(file, &buffer_size, buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Reading kernel file");
status = file->Close(file);
CHECK_EFI_STATUS_OR_RETURN(status, "Closing kernel file handle");
status = file->Close(file);
CHECK_EFI_STATUS_OR_RETURN(status, "Closing kernel file handle");
*length = buffer_size;
*kernel_image = buffer;
return EFI_SUCCESS;
}
}
*length = buffer_size;
*kernel_image = buffer;
return EFI_SUCCESS;
}
}
return EFI_NOT_FOUND;
return EFI_NOT_FOUND;
}

View File

@@ -30,64 +30,48 @@ efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
EFI_LOADED_IMAGE *info = 0;
EFI_GUID image_proto = EFI_LOADED_IMAGE_PROTOCOL_GUID;
status = ST->BootServices->HandleProtocol(ImageHandle, &image_proto, (void **)&info);
CHECK_EFI_STATUS_OR_FAIL(status);
CHECK_EFI_STATUS_OR_FAIL(status);
con_status_ok();
con_status_begin(L"Loading kernel into memory...");
void *kernel_image = NULL;
con_status_begin(L"Loading kernel into memory...");
void *kernel_image = NULL;
uint64_t kernel_length = 0;
status = loader_load_kernel(&kernel_image, &kernel_length);
CHECK_EFI_STATUS_OR_FAIL(status);
status = loader_load_kernel(&kernel_image, &kernel_length);
CHECK_EFI_STATUS_OR_FAIL(status);
Print(L" %u bytes at 0x%x", kernel_length, kernel_image);
con_status_ok();
con_status_ok();
//memory_dump_map();
con_status_begin(L"Exiting boot services...");
UINTN memmap_size = 0, memmap_key = 0;
UINTN desc_size = 0;
UINT32 desc_version = 0;
EFI_MEMORY_DESCRIPTOR *memory_map;
UINT32 desc_version = 0;
EFI_MEMORY_DESCRIPTOR *memory_map;
status = memory_mark_address_for_update((void**)&ST);
status = memory_mark_address_for_update((void**)&ST);
CHECK_EFI_STATUS_OR_FAIL(status);
status = memory_mark_address_for_update((void**)&kernel_image);
status = memory_mark_address_for_update((void**)&kernel_image);
CHECK_EFI_STATUS_OR_FAIL(status);
status = memory_get_map(&memory_map,
&memmap_size, &memmap_key,
&desc_size, &desc_version);
status = memory_get_map(&memory_map,
&memmap_size, &memmap_key,
&desc_size, &desc_version);
CHECK_EFI_STATUS_OR_FAIL(status);
status = ST->BootServices->ExitBootServices(ImageHandle, memmap_key);
CHECK_EFI_STATUS_OR_FAIL(status);
status = memory_virtualize(
&kernel_image,
memory_map, memmap_size,
desc_size, desc_version);
status = memory_virtualize(
&kernel_image,
memory_map, memmap_size,
desc_size, desc_version);
CHECK_EFI_STATUS_OR_FAIL(status);
void (*kernel_main)() = kernel_image;
kernel_main();
/*
con_status_ok();
con_status_begin(L"Virtualizing memory...");
con_status_ok();
void (*kernel_main)() = kernel_image;
kernel_main();
con_status_begin(L"Setting virtual address map");
status = ST->RuntimeServices->SetVirtualAddressMap(
memmap_size, desc_size, desc_version, memmap);
CHECK_EFI_STATUS_OR_FAIL(status);
con_status_ok();
*/
void (*kernel_main)() = kernel_image;
kernel_main();
while (1) __asm__("hlt");
return status;

View File

@@ -8,107 +8,107 @@
const UINTN PAGE_SIZE = 4096;
const CHAR16 *memory_type_names[] = {
L"EfiReservedMemoryType",
L"EfiLoaderCode",
L"EfiLoaderData",
L"EfiBootServicesCode",
L"EfiBootServicesData",
L"EfiRuntimeServicesCode",
L"EfiRuntimeServicesData",
L"EfiConventionalMemory",
L"EfiUnusableMemory",
L"EfiACPIReclaimMemory",
L"EfiACPIMemoryNVS",
L"EfiMemoryMappedIO",
L"EfiMemoryMappedIOPortSpace",
L"EfiPalCode",
L"EfiPersistentMemory",
L"EfiReservedMemoryType",
L"EfiLoaderCode",
L"EfiLoaderData",
L"EfiBootServicesCode",
L"EfiBootServicesData",
L"EfiRuntimeServicesCode",
L"EfiRuntimeServicesData",
L"EfiConventionalMemory",
L"EfiUnusableMemory",
L"EfiACPIReclaimMemory",
L"EfiACPIMemoryNVS",
L"EfiMemoryMappedIO",
L"EfiMemoryMappedIOPortSpace",
L"EfiPalCode",
L"EfiPersistentMemory",
};
static const CHAR16 *memory_type_name(UINT32 value) {
if (value >= (sizeof(memory_type_names)/sizeof(CHAR16*)))
return L"Bad Type Value";
return memory_type_names[value];
if (value >= (sizeof(memory_type_names)/sizeof(CHAR16*)))
return L"Bad Type Value";
return memory_type_names[value];
}
void EFIAPI memory_update_addresses(EFI_EVENT UNUSED *event, void *context) {
ST->RuntimeServices->ConvertPointer(0, (void **)context);
ST->RuntimeServices->ConvertPointer(0, (void **)context);
}
EFI_STATUS memory_mark_address_for_update(void **pointer) {
EFI_EVENT event;
EFI_STATUS status;
EFI_EVENT event;
EFI_STATUS status;
status = ST->BootServices->CreateEvent(
EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
TPL_CALLBACK,
(EFI_EVENT_NOTIFY)&memory_update_addresses,
(void*)pointer,
&event);
status = ST->BootServices->CreateEvent(
EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
TPL_CALLBACK,
(EFI_EVENT_NOTIFY)&memory_update_addresses,
(void*)pointer,
&event);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to create memory update event");
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to create memory update event");
}
EFI_STATUS memory_virtualize(void **kernel_addr, EFI_MEMORY_DESCRIPTOR *memory_map,
UINTN memmap_size, UINTN desc_size, UINT32 desc_version) {
unsigned count = memmap_size / desc_size;
for (unsigned i=0; i<count; ++i) {
if (memory_map[i].Type == KERNEL_MEMTYPE)
memory_map[i].VirtualStart = (EFI_VIRTUAL_ADDRESS)KERNEL_VIRT_ADDRESS;
else
memory_map[i].VirtualStart = (EFI_VIRTUAL_ADDRESS)memory_map[i].PhysicalStart;
}
UINTN memmap_size, UINTN desc_size, UINT32 desc_version) {
unsigned count = memmap_size / desc_size;
for (unsigned i=0; i<count; ++i) {
if (memory_map[i].Type == KERNEL_MEMTYPE)
memory_map[i].VirtualStart = (EFI_VIRTUAL_ADDRESS)KERNEL_VIRT_ADDRESS;
else
memory_map[i].VirtualStart = (EFI_VIRTUAL_ADDRESS)memory_map[i].PhysicalStart;
}
return ST->RuntimeServices->SetVirtualAddressMap(memmap_size, desc_size, desc_version, memory_map);
return ST->RuntimeServices->SetVirtualAddressMap(memmap_size, desc_size, desc_version, memory_map);
}
EFI_STATUS memory_get_map(EFI_MEMORY_DESCRIPTOR **buffer, UINTN *buffer_size,
UINTN *key, UINTN *desc_size, UINT32 *desc_version) {
EFI_STATUS status;
UINTN *key, UINTN *desc_size, UINT32 *desc_version) {
EFI_STATUS status;
UINTN needs_size = 0;
status = ST->BootServices->GetMemoryMap(&needs_size, 0, key, desc_size, desc_version);
if (status != EFI_BUFFER_TOO_SMALL) {
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to load memory map");
}
UINTN needs_size = 0;
status = ST->BootServices->GetMemoryMap(&needs_size, 0, key, desc_size, desc_version);
if (status != EFI_BUFFER_TOO_SMALL) {
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to load memory map");
}
// Give some extra buffer to account for changes.
*buffer_size = needs_size + 256;
status = ST->BootServices->AllocatePool(EfiLoaderData, *buffer_size, (void**)buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to allocate space for memory map");
// Give some extra buffer to account for changes.
*buffer_size = needs_size + 256;
status = ST->BootServices->AllocatePool(EfiLoaderData, *buffer_size, (void**)buffer);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to allocate space for memory map");
status = ST->BootServices->GetMemoryMap(buffer_size, *buffer, key, desc_size, desc_version);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to load memory map");
return EFI_SUCCESS;
status = ST->BootServices->GetMemoryMap(buffer_size, *buffer, key, desc_size, desc_version);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to load memory map");
return EFI_SUCCESS;
}
EFI_STATUS memory_dump_map() {
EFI_MEMORY_DESCRIPTOR *buffer;
UINTN buffer_size, desc_size, key;
UINT32 desc_version;
EFI_MEMORY_DESCRIPTOR *buffer;
UINTN buffer_size, desc_size, key;
UINT32 desc_version;
EFI_STATUS status = memory_get_map(&buffer, &buffer_size, &key,
&desc_size, &desc_version);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to get memory map");
EFI_STATUS status = memory_get_map(&buffer, &buffer_size, &key,
&desc_size, &desc_version);
CHECK_EFI_STATUS_OR_RETURN(status, "Failed to get memory map");
const UINTN count = buffer_size / desc_size;
const UINTN count = buffer_size / desc_size;
Print(L"Memory map:\n");
Print(L"\t Descriptor Count: %d (%d bytes)\n", count, buffer_size);
Print(L"\t Version Key: %d\n", key);
Print(L"\tDescriptor Version: %d (%d bytes)\n\n", desc_version, desc_size);
Print(L"Memory map:\n");
Print(L"\t Descriptor Count: %d (%d bytes)\n", count, buffer_size);
Print(L"\t Version Key: %d\n", key);
Print(L"\tDescriptor Version: %d (%d bytes)\n\n", desc_version, desc_size);
EFI_MEMORY_DESCRIPTOR *end = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)buffer + buffer_size);
EFI_MEMORY_DESCRIPTOR *d = buffer;
while (d < end) {
UINTN size_bytes = d->NumberOfPages * PAGE_SIZE;
EFI_MEMORY_DESCRIPTOR *end = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)buffer + buffer_size);
EFI_MEMORY_DESCRIPTOR *d = buffer;
while (d < end) {
UINTN size_bytes = d->NumberOfPages * PAGE_SIZE;
Print(L"%23s ", memory_type_name(d->Type));
Print(L"%016llx (%3d pages)\n", d->PhysicalStart, size_bytes / 0x1000);
Print(L"%23s ", memory_type_name(d->Type));
Print(L"%016llx (%3d pages)\n", d->PhysicalStart, size_bytes / 0x1000);
d = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)d + desc_size);
}
d = (EFI_MEMORY_DESCRIPTOR *)((uint8_t *)d + desc_size);
}
ST->BootServices->FreePool(buffer);
return EFI_SUCCESS;
ST->BootServices->FreePool(buffer);
return EFI_SUCCESS;
}

View File

@@ -4,17 +4,17 @@
EFI_STATUS memory_mark_address_for_update(void **pointer);
EFI_STATUS memory_virtualize(
void **kernel_image,
EFI_MEMORY_DESCRIPTOR *memory_map,
UINTN memmap_size,
UINTN desc_size,
UINT32 desc_version);
void **kernel_image,
EFI_MEMORY_DESCRIPTOR *memory_map,
UINTN memmap_size,
UINTN desc_size,
UINT32 desc_version);
EFI_STATUS memory_get_map(
EFI_MEMORY_DESCRIPTOR **buffer,
UINTN *buffer_size,
UINTN *key,
UINTN *desc_size,
UINT32 *desc_version);
EFI_MEMORY_DESCRIPTOR **buffer,
UINTN *buffer_size,
UINTN *key,
UINTN *desc_size,
UINT32 *desc_version);
EFI_STATUS memory_dump_map();

View File

@@ -1,18 +1,18 @@
#include <efi.h>
struct ErrorCode {
EFI_STATUS code;
const CHAR16 *desc;
EFI_STATUS code;
const CHAR16 *desc;
};
extern struct ErrorCode ErrorCodeTable[];
const CHAR16 *util_error_message(EFI_STATUS status) {
int32_t i = -1;
while (ErrorCodeTable[++i].desc != NULL) {
if (ErrorCodeTable[i].code == status)
return ErrorCodeTable[i].desc;
}
int32_t i = -1;
while (ErrorCodeTable[++i].desc != NULL) {
if (ErrorCodeTable[i].code == status)
return ErrorCodeTable[i].desc;
}
return L"Unknown";
}
return L"Unknown";
}