Advertisement
Guest User

Untitled

a guest
Apr 17th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. /*
  2.  * Boot loader written using gnu-efi.
  3.  */
  4. #include <efi.h>
  5. #include <efilib.h>
  6. #include <stdatomic.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include "include/frame_buffer.h"
  12.  
  13. void fill_color(unsigned short horizontal_resolution, unsigned short vertical_resolution, long unsigned int *frame_buffer_base, long unsigned int bg_color);
  14.  
  15. EFI_STATUS
  16. EFIAPI
  17. efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
  18. {
  19.     EFI_STATUS status;
  20.     EFI_GUID gopGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  21.     EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
  22.     EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
  23.     UINTN SizeOfInfo, numModes, nativeMode;
  24.     int i;
  25.     uint32_t mode;
  26.     struct frame_buffer_descriptor frame_buffer;
  27.     uint8_t mmap[4900];
  28.     UINTN msize = sizeof(mmap);
  29.     UINTN mkey = 0;
  30.     UINTN dsize = 0;
  31.     UINT32 dversion;
  32.  
  33.  
  34.     InitializeLib(ImageHandle, SystemTable);
  35.  
  36.     /* detecting GOP */
  37.     status = uefi_call_wrapper(BS->LocateProtocol, 3, &gopGuid, NULL, (void**)&gop);
  38.     if(EFI_ERROR(status))
  39.         Print(L"error: unable to locate GOP!\n");
  40.  
  41.     /* get the current mode */
  42.     status = uefi_call_wrapper(gop->QueryMode, 4, gop, gop->Mode==NULL?0:gop->Mode->Mode, &SizeOfInfo, &info);
  43.     /* this is needed to get the current video mode */
  44.     if (status == EFI_NOT_STARTED)
  45.         status = uefi_call_wrapper(gop->SetMode, 2, gop, 0);
  46.     if(EFI_ERROR(status)) {
  47.         Print(L"error: unable to get native video mode!\n");
  48.     } else {
  49.         nativeMode = gop->Mode->Mode;
  50.         numModes = gop->Mode->MaxMode;
  51.     }
  52.  
  53.     /* query available video modes and get the mode number for 1280 x 1024 resolution */
  54.     mode = -1;
  55.     for (i = 0; i < numModes; i++) {
  56.         status = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo, &info);
  57.         if (info->HorizontalResolution == 1280 && info->VerticalResolution == 1024 && info->PixelFormat == 1) {
  58.             mode = i;
  59.             break;
  60.         }
  61.     }
  62.  
  63.     if (mode == -1) {
  64.         Print(L"error: unable to get video mode for 1280 x 1024 resolution!\n");
  65.     } else {
  66.         /* set video mode and get the framebuffer */
  67.         status = uefi_call_wrapper(gop->SetMode, 2, gop, mode);
  68.         if(EFI_ERROR(status)) {
  69.             Print(L"error: unable to set video mode %03d\n", mode);
  70.         } else {
  71.             /* store framebuffer information */
  72.             frame_buffer.frame_buffer_base = (long unsigned int *) gop->Mode->FrameBufferBase;
  73.             frame_buffer.frame_buffer_size = gop->Mode->FrameBufferSize;
  74.             frame_buffer.horizontal_resolution = gop->Mode->Info->HorizontalResolution;
  75.             frame_buffer.vertical_resolution = gop->Mode->Info->VerticalResolution;
  76.             frame_buffer.pixels_per_scan_line = gop->Mode->Info->PixelsPerScanLine;
  77.         }
  78.     }
  79.  
  80.     Print(L"fb_base=%p\n", (void *) frame_buffer.frame_buffer_base);
  81.  
  82.     /* try to exit boot services 3 times */
  83.     for (i = 0; i < 3; i++) {
  84.         /* get memory map */
  85.         status = uefi_call_wrapper(BS->GetMemoryMap, 5, &msize, &mmap, &mkey, &dsize, NULL);
  86.         if(EFI_ERROR(status)) {
  87.             Print(L"error: could not get memory map!\n");
  88.             goto end;
  89.         } else if (status == EFI_SUCCESS) {
  90.             Print(L"got memory map!\n");
  91.             Print(L"mkey=%d\n", (int) mkey);
  92.             /* exit boot services */
  93.             status = uefi_call_wrapper(BS->ExitBootServices, 2, ImageHandle, mkey);
  94.             if (status == EFI_SUCCESS)
  95.                 break;
  96.         }
  97.     }
  98.     if(status == EFI_INVALID_PARAMETER) {
  99.         Print(L"error: exit boot services: map key is incorrect!\n");
  100.         goto end;
  101.     }
  102.  
  103.     /* red color */
  104.     /* fill_color(frame_buffer.horizontal_resolution, frame_buffer.vertical_resolution, frame_buffer.frame_buffer_base, 0xAA0000); */
  105.  
  106. end:
  107.  
  108.     /* hang here */
  109.     while(1) {
  110.     }
  111.  
  112.     return 1;
  113. }
  114.  
  115. void fill_color(unsigned short horizontal_resolution, unsigned short vertical_resolution, long unsigned int *frame_buffer_base, long unsigned int bg_color)
  116. {
  117.    
  118.     unsigned int pixels = horizontal_resolution * vertical_resolution;
  119.     uint32_t* addr = frame_buffer_base;
  120.    
  121.     while (pixels--) {
  122.         *addr++ = bg_color;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement