Advertisement
Guest User

Untitled

a guest
Apr 16th, 2024
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 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. /* initialize terminal driver */
  79. /* tty_init(frame_buffer); */
  80.  
  81. memory_map = malloc(memory_map_size);
  82. if (memory_map == NULL) {
  83. Print(L"error: could not allocate memory for the memroy map!\n");
  84. } else {
  85. /* try to exit boot services 3 times */
  86. for (int retries = 0; retries < 3; ++retries) {
  87. /* get memory map */
  88. status = uefi_call_wrapper(BS->GetMemoryMap, 4, &memory_map_size, memory_map, &map_key, &descriptor_size);
  89. if(EFI_ERROR(status)) {
  90. Print(L"error: could not get memory map!\n");
  91. goto end;
  92. } else {
  93. /* exit boot services */
  94. status = uefi_call_wrapper(BS->ExitBootServices, 2, ImageHandle, map_key);
  95. if (status == EFI_SUCCESS)
  96. break;
  97. }
  98. }
  99. if(EFI_ERROR(status)) {
  100. Print(L"error: could not exit boot services!\n");
  101. goto end;
  102. }
  103. }
  104.  
  105. /* test: write a char onto the terminal */
  106. /* write_char("A", 0, 0, 0xFFFFFF, 0x00000); */
  107.  
  108. /* red color */
  109. fill_color(frame_buffer->horizontal_resolution, frame_buffer->vertical_resolution, frame_buffer->frame_buffer_base, 0xAA0000);
  110.  
  111. end:
  112. free(memory_map);
  113.  
  114. /* hang here */
  115. while(1) {
  116. }
  117.  
  118. return 1;
  119. }
  120.  
  121. void fill_color(unsigned short horizontal_resolution, unsigned short vertical_resolution, long unsigned int *frame_buffer_base, long unsigned int bg_color)
  122. {
  123. unsigned int pixels = horizontal_resolution * vertical_resolution;
  124. uint32_t* addr = frame_buffer_base;
  125.  
  126. while (pixels--) {
  127. *addr++ = bg_color;
  128. }
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement