Advertisement
Guest User

Untitled

a guest
Apr 9th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. #define PAGE_MASK   0xfff
  2. #define PAGE_SHIFT  12
  3.  
  4. inline void __writecr0(CEfiU64 cr0)
  5. {
  6.     asm volatile("mov %%rax, %%cr0" :: "a"(cr0));
  7. }
  8.  
  9. inline CEfiU64 __readcr0()
  10. {
  11.     CEfiU64 cr0;
  12.     asm("mov %%cr0, %%rax" : "=a"(cr0));
  13.     return cr0;
  14. }
  15.  
  16. inline CEfiU64 __readcr3()
  17. {
  18.     CEfiU64 cr3;
  19.     asm("mov %%cr3, %%rax" : "=a"(cr3));
  20.     return cr3;
  21. }
  22.  
  23. CEFICALL CEfiStatus efi_main([[__maybe_unused__]] CEfiHandle handle,
  24.                              CEfiSystemTable *systemtable) {
  25.     h = handle;
  26.     st = systemtable;
  27.  
  28.     st->con_out->reset(st->con_out, false);
  29.  
  30.     st->con_out->set_attribute(st->con_out,
  31.                                C_EFI_BACKGROUND_RED | C_EFI_YELLOW);
  32.  
  33.     CEfiGuid lip_guid = C_EFI_LOADED_IMAGE_PROTOCOL_GUID;
  34.     CEfiLoadedImageProtocol* lip = C_EFI_NULL;
  35.     CEfiStatus status = st->boot_services->open_protocol(
  36.         h, &lip_guid, (void**)&lip, h, C_EFI_NULL,
  37.         C_EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  38.     if (C_EFI_ERROR(status)) {
  39.         error(u"Could not open Loaded Image Protocol\r\n");
  40.     }
  41.  
  42.     CEfiGuid sfsp_guid = C_EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
  43.     CEfiSimpleFileSystemProtocol* sfsp = C_EFI_NULL;
  44.     status = st->boot_services->open_protocol(
  45.         lip->device_handle, &sfsp_guid, (void**)&sfsp, h, C_EFI_NULL,
  46.         C_EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  47.     if (C_EFI_ERROR(status)) {
  48.         error(u"Could not open Simple File System Protocol\r\n");
  49.     }
  50.  
  51.     CEfiFileProtocol* root = C_EFI_NULL;
  52.     status = sfsp->openVolume(sfsp, &root);
  53.     if (C_EFI_ERROR(status)) {
  54.         error(u"Could not Open Volume for root directory in ESP\r\n");
  55.     }
  56.  
  57.     CEfiFileProtocol* file = C_EFI_NULL;
  58.     status = root->open(root, &file, u"\\EFI\\BOOT\\kernel.bin", C_EFI_FILE_MODE_READ, C_EFI_FILE_READ_ONLY);
  59.     if (C_EFI_ERROR(status)) {
  60.         error(u"Could not Open File\r\n");
  61.     }
  62.  
  63.     CEfiFileInfo file_info;
  64.     CEfiGuid fi_guid = C_EFI_FILE_INFO_ID;
  65.     CEfiUSize file_info_size = sizeof(file_info);
  66.     status = file->getInfo(file, &fi_guid, &file_info_size, &file_info);
  67.     if (C_EFI_ERROR(status)) {
  68.         error(u"Could not get file info\r\n");
  69.     }
  70.  
  71.     CEfiUSize file_size = file_info.fileSize;
  72.     print_string(u"file size: %llu\r\n", file_size);
  73.  
  74.     CEfiUSize pages = (file_size >> PAGE_SHIFT) + (file_size & PAGE_MASK ? 1 : 0);
  75.     CEfiPhysicalAddress phys_krnl;
  76.  
  77.     status = st->boot_services->allocate_pages(C_EFI_ALLOCATE_ANY_PAGES, C_EFI_LOADER_DATA, pages, &phys_krnl);
  78.     if (C_EFI_ERROR(status)) {
  79.         error(u"Could not allocate kernel\r\n");
  80.     }
  81.  
  82.     memset((void*)phys_krnl, 0, pages << PAGE_SHIFT);
  83.  
  84.     print_string(u"Physical address: 0x%llx - 0x%llx\r\n", phys_krnl, phys_krnl + (pages << PAGE_SHIFT) - 1);
  85.     print_string(u"Allocated %llu pages\r\n", pages);
  86.  
  87.     file->setPosisition(file, 0);
  88.     CEfiUSize read_bytes = file_size;
  89.     print_string(u"Attempting to read %llu bytes...", read_bytes);
  90.     file->read(file, &read_bytes, (void*)phys_krnl);
  91.     print_string(u"Read %llu bytes.\r\n", read_bytes);
  92.  
  93. #define CR0_WP (1 << 16)
  94.  
  95.     // Clear WP flag to allow rewriting page tables
  96.     __writecr0(__readcr0() & ~CR0_WP);
  97.  
  98.     // This is missing...
  99.     void *virt_krnl = map_kernel_and_framebuffer();
  100.  
  101.     __writecr0(__readcr0() | CR0_WP);
  102.  
  103.     jumpIntoKernel((void*)virt_krnl);
  104.  
  105.     return C_EFI_SUCCESS;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement