Reduce dependency on efi library functions

This commit is contained in:
Justin C. Miller
2018-03-31 17:32:10 -07:00
parent a09a25d3e6
commit 165ed28cd3
10 changed files with 375 additions and 109 deletions

View File

@@ -4,7 +4,12 @@
static CHAR16 kernel_name[] = KERNEL_FILENAME;
EFI_STATUS
loader_load_kernel(void **kernel_image, uint64_t *kernel_length, void **kernel_data, uint64_t *data_length)
loader_load_kernel(
EFI_BOOT_SERVICES *bootsvc,
void **kernel_image,
uint64_t *kernel_length,
void **kernel_data,
uint64_t *data_length)
{
if (kernel_image == 0 || kernel_length == 0)
CHECK_EFI_STATUS_OR_RETURN(EFI_INVALID_PARAMETER, "NULL kernel_image or length pointer");
@@ -17,13 +22,13 @@ loader_load_kernel(void **kernel_image, uint64_t *kernel_length, void **kernel_d
EFI_HANDLE *handles = NULL;
UINTN handleCount = 0;
status = ST->BootServices->LocateHandleBuffer(ByProtocol, &guid, NULL, &handleCount, &handles);
status = bootsvc->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;
status = ST->BootServices->HandleProtocol(handles[i], &guid, (void **)&fileSystem);
status = bootsvc->HandleProtocol(handles[i], &guid, (void **)&fileSystem);
CHECK_EFI_STATUS_OR_RETURN(status, "HandleProtocol");
EFI_FILE_PROTOCOL *root = NULL;
@@ -39,7 +44,7 @@ loader_load_kernel(void **kernel_image, uint64_t *kernel_length, void **kernel_d
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);
status = bootsvc->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);
@@ -47,20 +52,20 @@ loader_load_kernel(void **kernel_image, uint64_t *kernel_length, void **kernel_d
buffer_size = ((EFI_FILE_INFO *)buffer)->FileSize;
status = ST->BootServices->FreePool(buffer);
status = bootsvc->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;
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);
status = bootsvc->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);
bootsvc->AllocatePages(AllocateAnyPages, mem_type, page_count, &addr);
}
CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel pages");
Print(L"\n Allocating %u pages at 0x%x", page_count, addr);
con_printf(L"\n Allocating %u pages at 0x%x", page_count, addr);
buffer = (void *)addr;
status = file->Read(file, &buffer_size, buffer);
@@ -75,18 +80,18 @@ loader_load_kernel(void **kernel_image, uint64_t *kernel_length, void **kernel_d
addr += page_count * 0x1000; // Get the next page after the kernel pages
mem_type = KERNEL_DATA_MEMTYPE; // Special value for kernel data
page_count = ((*data_length - 1) / 0x1000) + 1;
status = ST->BootServices->AllocatePages(AllocateAddress, mem_type, page_count, &addr);
status = bootsvc->AllocatePages(AllocateAddress, mem_type, page_count, &addr);
if (status == EFI_NOT_FOUND) {
// couldn't get the address we wanted, try loading anywhere
status =
ST->BootServices->AllocatePages(AllocateAnyPages, mem_type, page_count, &addr);
bootsvc->AllocatePages(AllocateAnyPages, mem_type, page_count, &addr);
}
CHECK_EFI_STATUS_OR_RETURN(status, "Allocating kernel data pages");
Print(L"\n Allocating %u pages at 0x%x", page_count, addr);
con_printf(L"\n Allocating %u pages at 0x%x", page_count, addr);
*data_length = page_count * 0x1000;
*kernel_data = (void *)addr;
ST->BootServices->SetMem(*kernel_data, *data_length, 0);
bootsvc->SetMem(*kernel_data, *data_length, 0);
return EFI_SUCCESS;
}