uefi.c (3569B)
1 #include "uefi.h" 2 #include "utils.h" 3 EFI_SYSTEM_TABLE *SystemTable; 4 5 EFI_STATUS EFIAPI 6 EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *systab) 7 { 8 // init global variable. 9 SystemTable = systab; 10 11 EFI_STATUS stat; 12 13 // Output firmware bender. 14 SystemTable->ConOut->ClearScreen(SystemTable->ConOut); 15 efi_printf("vendor: %w\n", SystemTable->FirmwareVendor); 16 efi_printf("EfiMain: 0x%x\n", EfiMain); 17 18 // Output memory map information. 19 UINTN mmsize = 8196; 20 char mmbuf[8196]; 21 EFI_MEMORY_DESCRIPTOR *mmap; 22 UINTN mkey; 23 UINTN dsize; 24 UINT32 dver; 25 mmap = (EFI_MEMORY_DESCRIPTOR *) mmbuf; 26 stat = SystemTable->BootServices->GetMemoryMap(&mmsize, mmap, &mkey, &dsize, &dver); 27 if (stat != EFI_SUCCESS) { 28 efi_printf("get memory map: %d\n", stat); 29 return stat; 30 } 31 32 for (; mmap < (EFI_MEMORY_DESCRIPTOR *)(mmbuf + mmsize); 33 mmap = (EFI_MEMORY_DESCRIPTOR *) (((char *)mmap) + dsize)) { 34 if (mmap->Type != EfiConventionalMemory) { 35 continue; 36 } 37 efi_printf("%d\t%x\t%x\t%x\t%x\n", mmap->Type, mmap->VirtualStart, 38 mmap->PhysicalStart, mmap->NumberOfPages, mmap->Attribute); 39 } 40 41 EFI_GRAPHICS_OUTPUT_PROTOCOL *gop; 42 stat = open_gop(ImageHandle, &gop); 43 if (stat != EFI_SUCCESS) { 44 efi_printf("open_gop: %d\n", stat); 45 return stat; 46 } 47 UINT32 *frame_buffer = (UINT32 *)gop->Mode->FrameBufferBase; 48 for (UINTN i = 0; i < gop->Mode->FrameBufferSize/4; i++) { 49 frame_buffer[i] = 0x427f94; 50 } 51 efi_printf("frame buffer: base: %x, size: %x\n", frame_buffer, 52 gop->Mode->FrameBufferSize); 53 54 EFI_FILE_PROTOCOL *root, *kernel; 55 stat = open_root(ImageHandle, &root); 56 if (stat != EFI_SUCCESS) { 57 efi_printf("open_root: %d\n", stat); 58 return stat; 59 } 60 stat = root->Open(root, &kernel, L"setos.elf", 61 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, 0); 62 if (stat != EFI_SUCCESS) { 63 efi_printf("open file \"kernel.elf\": %d\n", stat); 64 return stat; 65 } 66 ElfHdr kernel_ehdr; 67 stat = read_ehdr(kernel, &kernel_ehdr); 68 if (stat != EFI_SUCCESS) { 69 efi_printf("read ehdr of \"kernel.elf\": %d\n", stat); 70 return stat; 71 } 72 efi_printf("type:\t%x\nphoff:\t%x\nphentsize:\t%x\nphnum:\t%x\n", 73 kernel_ehdr.type, kernel_ehdr.phoff, 74 kernel_ehdr.phentsize, kernel_ehdr.phnum); 75 if (kernel_ehdr.phnum > 32) { 76 efi_printf("too many phdrs\n"); 77 return EFI_BUFFER_TOO_SMALL; 78 } 79 Phdr kernel_phdr[32]; 80 stat = read_phdr(kernel, &kernel_ehdr, kernel_phdr); 81 if (stat != EFI_SUCCESS) { 82 efi_printf("read phdr of \"kernel.elf\": %d\n", stat); 83 return stat; 84 } 85 for (UINT16 i = 0; i < kernel_ehdr.phnum; i++) { 86 efi_printf("type: %x, offset: %x, vaddr: %x\n", 87 kernel_phdr[i].type, kernel_phdr[i].offset, kernel_phdr[i].vaddr); 88 } 89 stat = load_elf(kernel, &kernel_ehdr, kernel_phdr); 90 if (stat != EFI_SUCCESS) { 91 efi_printf("load \"kernel.elf\": %d\n", stat); 92 return stat; 93 } 94 stat = kernel->Close(kernel); 95 if (stat != EFI_SUCCESS) { 96 efi_printf("close file: %d\n", stat); 97 return stat; 98 } 99 100 efi_printf("frame buffer: base: %x, %x, size: %x\n", frame_buffer, 101 gop->Mode->FrameBufferBase, 102 gop->Mode->FrameBufferSize); 103 104 mmsize = 8196; 105 stat = SystemTable->BootServices->GetMemoryMap(&mmsize, mmap, &mkey, &dsize, &dver); 106 if (stat != EFI_SUCCESS) { 107 efi_printf("get memory map: %d\n", stat); 108 return stat; 109 } 110 stat = SystemTable->BootServices->ExitBootServices(ImageHandle, mkey); 111 if (stat != EFI_SUCCESS) { 112 efi_printf("exit boot services: %d\n", stat); 113 return stat; 114 } 115 116 typedef void (* Kernel) (EFI_GRAPHICS_OUTPUT_PROTOCOL *); 117 ((Kernel) kernel_ehdr.entry)(gop); 118 119 return EFI_SUCCESS; 120 }