Advertisement
Guest User

Untitled

a guest
Apr 17th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.93 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.         UINTN map_key = 0;
  27.         UINTN descriptor_size = 0;
  28.         UINTN memory_map_size = 1024*1024;          /* 1 MB should be enough */
  29.     EFI_MEMORY_DESCRIPTOR *memory_map;
  30.     struct frame_buffer_descriptor frame_buffer;
  31.  
  32.     InitializeLib(ImageHandle, SystemTable);
  33.  
  34.     /* detecting GOP */
  35.     status = uefi_call_wrapper(BS->LocateProtocol, 3, &gopGuid, NULL, (void**)&gop);
  36.     if(EFI_ERROR(status))
  37.         Print(L"error: unable to locate GOP!\n");
  38.  
  39.     /* get the current mode */
  40.     status = uefi_call_wrapper(gop->QueryMode, 4, gop, gop->Mode==NULL?0:gop->Mode->Mode, &SizeOfInfo, &info);
  41.     /* this is needed to get the current video mode */
  42.     if (status == EFI_NOT_STARTED)
  43.         status = uefi_call_wrapper(gop->SetMode, 2, gop, 0);
  44.     if(EFI_ERROR(status)) {
  45.         Print(L"error: unable to get native video mode!\n");
  46.     } else {
  47.         nativeMode = gop->Mode->Mode;
  48.         numModes = gop->Mode->MaxMode;
  49.     }
  50.  
  51.     /* query available video modes and get the mode number for 1280 x 1024 resolution */
  52.     mode = -1;
  53.     for (i = 0; i < numModes; i++) {
  54.         status = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo, &info);
  55.         if (info->HorizontalResolution == 1280 && info->VerticalResolution == 1024 && info->PixelFormat == 1) {
  56.             mode = i;
  57.             break;
  58.         }
  59.     }
  60.  
  61.     if (mode == -1) {
  62.         Print(L"error: unable to get video mode for 1280 x 1024 resolution!\n");
  63.     } else {
  64.         /* set video mode and get the framebuffer */
  65.         status = uefi_call_wrapper(gop->SetMode, 2, gop, mode);
  66.         if(EFI_ERROR(status)) {
  67.             Print(L"error: unable to set video mode %03d\n", mode);
  68.         } else {
  69.             /* store framebuffer information */
  70.             frame_buffer.frame_buffer_base = (long unsigned int *) gop->Mode->FrameBufferBase;
  71.             frame_buffer.frame_buffer_size = gop->Mode->FrameBufferSize;
  72.             frame_buffer.horizontal_resolution = gop->Mode->Info->HorizontalResolution;
  73.             frame_buffer.vertical_resolution = gop->Mode->Info->VerticalResolution;
  74.             frame_buffer.pixels_per_scan_line = gop->Mode->Info->PixelsPerScanLine;
  75.         }
  76.     }
  77.  
  78.     memory_map = malloc(memory_map_size);
  79.     if (memory_map == NULL) {
  80.         Print(L"error: could not allocate memory for the memroy map!\n");
  81.     } else {
  82.         /* try to exit boot services 3 times */
  83.         for (int retries = 0; retries < 3; ++retries) {
  84.             /* get memory map */
  85.             status = uefi_call_wrapper(BS->GetMemoryMap, 4, &memory_map_size, memory_map, &map_key, &descriptor_size);
  86.             if(EFI_ERROR(status)) {
  87.                 Print(L"error: could not get memory map!\n");
  88.                 goto end;
  89.             } else {
  90.                 /* exit boot services */
  91.                 status = uefi_call_wrapper(BS->ExitBootServices, 2, ImageHandle, map_key);
  92.                 if (status == EFI_SUCCESS)
  93.                     break;
  94.             }
  95.         }
  96.         if(EFI_ERROR(status)) {
  97.             Print(L"error: could not exit boot services!\n");
  98.             goto end;
  99.         }
  100.     }
  101.  
  102.     /* red color */
  103.     fill_color(frame_buffer.horizontal_resolution, frame_buffer.vertical_resolution, frame_buffer.frame_buffer_base, 0xAA0000);
  104.  
  105. end:
  106.     free(memory_map);
  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. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement