commit c1b98d4f44547fb85418110fe68a2b7dbf11c384
parent bad712b2a7aa5f6bfdff6470b0d06d767385d1dd
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 29 Mar 2024 09:50:44 +0900
load kernel
Diffstat:
M | loader.c | | | 30 | ++++++++++++------------------ |
1 file changed, 12 insertions(+), 18 deletions(-)
diff --git a/loader.c b/loader.c
@@ -55,18 +55,12 @@ EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *systab)
efi_printf("open file \"kernel.elf\": %d\n", stat);
return stat;
}
- EFI_PHYSICAL_ADDRESS kernel_base = 0x100000;
- UINTN fib_size = sizeof(EFI_FILE_INFO) + sizeof(CHAR16) * 11;
- char fibuf[sizeof(EFI_FILE_INFO) + sizeof(CHAR16) * 11]; // determined at compile time?
- EFI_FILE_INFO *fileInfo = (EFI_FILE_INFO *) fibuf;
- stat = kernel->GetInfo(kernel, &EFI_FILE_INFO_ID, &fib_size, (VOID *)fibuf);
+ ElfHdr kernel_ehdr;
+ stat = read_ehdr(kernel, &kernel_ehdr);
if (stat != EFI_SUCCESS) {
- efi_printf("get info of \"kernel.elf\": %d\n", stat);
+ efi_printf("read ehdr of \"kernel.elf\": %d\n", stat);
return stat;
}
- UINT64 kernel_size = fileInfo->FileSize;
- ElfHdr kernel_ehdr;
- read_ehdr(kernel, &kernel_ehdr);
efi_printf("type:\t%x\nphoff:\t%x\nphentsize:\t%x\nphnum:\t%x\n",
kernel_ehdr.type, kernel_ehdr.phoff,
kernel_ehdr.phentsize, kernel_ehdr.phnum);
@@ -75,28 +69,28 @@ EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *systab)
return EFI_BUFFER_TOO_SMALL;
}
Phdr kernel_phdr[32];
- read_phdr(kernel, &kernel_ehdr, kernel_phdr);
+ stat = read_phdr(kernel, &kernel_ehdr, kernel_phdr);
+ if (stat != EFI_SUCCESS) {
+ efi_printf("read phdr of \"kernel.elf\": %d\n", stat);
+ return stat;
+ }
for (UINT16 i = 0; i < kernel_ehdr.phnum; i++) {
efi_printf("type: %x, offset: %x, vaddr: %x\n",
kernel_phdr[i].type, kernel_phdr[i].offset, kernel_phdr[i].vaddr);
}
-
- stat = kernel->SetPosition(kernel, 0x1000);
- if (stat != EFI_SUCCESS) {
- efi_printf("set position of \"kernel.elf\": %d\n", stat);
- return stat;
- }
- stat = kernel->Read(kernel, &kernel_size, (VOID *)kernel_base);
+ stat = load_elf(kernel, &kernel_ehdr, kernel_phdr);
if (stat != EFI_SUCCESS) {
- efi_printf("read file \"kernel.elf\": %d\n", stat);
+ efi_printf("load \"kernel.elf\": %d\n", stat);
return stat;
}
+
stat = kernel->Close(kernel);
if (stat != EFI_SUCCESS) {
efi_printf("close file: %d\n", stat);
return stat;
}
+ EFI_PHYSICAL_ADDRESS kernel_base = 0x100000;
typedef void (* Kernel) (void);
((Kernel) kernel_base)();