lfbgrid.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <efi.h>
  2. #include <efilib.h>
  3. extern EFI_GUID GraphicsOutputProtocol;
  4. #define be32_to_cpu(x) __builtin_bswap32(x)
  5. static void
  6. fill_boxes(UINT32 *PixelBuffer, UINT32 Width, UINT32 Height, UINT32 Pitch,
  7. EFI_GRAPHICS_PIXEL_FORMAT Format, EFI_PIXEL_BITMASK Info )
  8. {
  9. UINT32 Red, Green;
  10. UINT32 y, x, color;
  11. switch(Format) {
  12. case PixelRedGreenBlueReserved8BitPerColor:
  13. Red = be32_to_cpu(0xff000000);
  14. Green = be32_to_cpu(0x00ff0000);
  15. break;
  16. case PixelBlueGreenRedReserved8BitPerColor:
  17. Red = be32_to_cpu(0x0000ff00);
  18. Green = be32_to_cpu(0x00ff0000);
  19. break;
  20. case PixelBitMask:
  21. Red = Info.RedMask;
  22. Green = Info.GreenMask;
  23. break;
  24. case PixelBltOnly:
  25. return;
  26. default:
  27. Print(L"Invalid pixel format\n");
  28. return;
  29. }
  30. for (y = 0; y < Height; y++) {
  31. color = ((y / 32) % 2 == 0) ? Red : Green;
  32. for (x = 0; x < Width; x++) {
  33. if (x % 32 == 0 && x != 0)
  34. color = (color == Red) ? Green : Red;
  35. PixelBuffer[y * Pitch + x] = color;
  36. }
  37. }
  38. }
  39. static void
  40. draw_boxes(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
  41. {
  42. int i, imax;
  43. EFI_STATUS rc;
  44. EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
  45. UINTN NumPixels;
  46. UINT32 *PixelBuffer;
  47. UINT32 CopySize, BufferSize;
  48. #if defined(__x86_64__) || defined(__aarch64__) || \
  49. (defined (__riscv) && __riscv_xlen == 64) || \
  50. defined(__loongarch64)
  51. UINT64 FrameBufferAddr;
  52. #elif defined(__i386__) || defined(__arm__)
  53. UINT32 FrameBufferAddr;
  54. #else
  55. #error YOUR ARCH HERE
  56. #endif
  57. if (gop->Mode) {
  58. imax = gop->Mode->MaxMode;
  59. } else {
  60. Print(L"gop->Mode is NULL\n");
  61. return;
  62. }
  63. for (i = 0; i < imax; i++) {
  64. UINTN SizeOfInfo;
  65. rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo,
  66. &info);
  67. if (rc == EFI_NOT_STARTED) {
  68. Print(L"gop->QueryMode() returned %r\n", rc);
  69. Print(L"Trying to start GOP with SetMode().\n");
  70. rc = uefi_call_wrapper(gop->SetMode, 2, gop,
  71. gop->Mode ? gop->Mode->Mode : 0);
  72. rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i,
  73. &SizeOfInfo, &info);
  74. }
  75. if (EFI_ERROR(rc)) {
  76. Print(L"%d: Bad response from QueryMode: %r (%d)\n",
  77. i, rc, rc);
  78. continue;
  79. }
  80. if (CompareMem(info, gop->Mode->Info, sizeof (*info)))
  81. continue;
  82. NumPixels = info->VerticalResolution * info->PixelsPerScanLine;
  83. BufferSize = NumPixels * sizeof(UINT32);
  84. if (BufferSize == gop->Mode->FrameBufferSize) {
  85. CopySize = BufferSize;
  86. } else {
  87. CopySize = BufferSize < gop->Mode->FrameBufferSize ?
  88. BufferSize : gop->Mode->FrameBufferSize;
  89. Print(L"height * pitch * pixelsize = %lu buf fb size is %lu; using %lu\n",
  90. BufferSize, gop->Mode->FrameBufferSize, CopySize);
  91. }
  92. PixelBuffer = AllocatePool(BufferSize);
  93. if (!PixelBuffer) {
  94. Print(L"Allocation of 0x%08lx bytes failed.\n",
  95. sizeof(UINT32) * NumPixels);
  96. return;
  97. }
  98. fill_boxes(PixelBuffer, info->HorizontalResolution,
  99. info->VerticalResolution, info->PixelsPerScanLine,
  100. info->PixelFormat, info->PixelInformation);
  101. if (info->PixelFormat == PixelBltOnly) {
  102. Print(L"No linear framebuffer on this device.\n");
  103. return;
  104. }
  105. #if defined(__x86_64__) || defined(__aarch64__) || \
  106. (defined (__riscv) && __riscv_xlen == 64) || \
  107. defined(__loongarch64)
  108. FrameBufferAddr = (UINT64)gop->Mode->FrameBufferBase;
  109. #elif defined(__i386__) || defined(__arm__)
  110. FrameBufferAddr = (UINT32)(UINT64)gop->Mode->FrameBufferBase;
  111. #else
  112. #error YOUR ARCH HERE
  113. #endif
  114. CopyMem((VOID *)FrameBufferAddr, PixelBuffer, CopySize);
  115. return;
  116. }
  117. Print(L"Never found the active video mode?\n");
  118. }
  119. static EFI_STATUS
  120. SetWatchdog(UINTN seconds)
  121. {
  122. EFI_STATUS rc;
  123. rc = uefi_call_wrapper(BS->SetWatchdogTimer, 4, seconds, 0x1ffff,
  124. 0, NULL);
  125. if (EFI_ERROR(rc)) {
  126. CHAR16 Buffer[64];
  127. StatusToString(Buffer, rc);
  128. Print(L"Bad response from QueryMode: %s (%d)\n", Buffer, rc);
  129. }
  130. return rc;
  131. }
  132. EFI_STATUS
  133. efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
  134. {
  135. EFI_STATUS rc;
  136. EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
  137. InitializeLib(image_handle, systab);
  138. SetWatchdog(10);
  139. rc = LibLocateProtocol(&GraphicsOutputProtocol, (void **)&gop);
  140. if (EFI_ERROR(rc)) {
  141. Print(L"Could not locate GOP: %r\n", rc);
  142. return rc;
  143. }
  144. if (!gop) {
  145. Print(L"LocateProtocol(GOP, &gop) returned %r but GOP is NULL\n", rc);
  146. return EFI_UNSUPPORTED;
  147. }
  148. draw_boxes(gop);
  149. SetWatchdog(0);
  150. return EFI_SUCCESS;
  151. }