lfbgrid.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. UINT64 FrameBufferAddr;
  51. #elif defined(__i386__) || defined(__arm__)
  52. UINT32 FrameBufferAddr;
  53. #else
  54. #error YOUR ARCH HERE
  55. #endif
  56. if (gop->Mode) {
  57. imax = gop->Mode->MaxMode;
  58. } else {
  59. Print(L"gop->Mode is NULL\n");
  60. return;
  61. }
  62. for (i = 0; i < imax; i++) {
  63. UINTN SizeOfInfo;
  64. rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo,
  65. &info);
  66. if (EFI_ERROR(rc) && rc == EFI_NOT_STARTED) {
  67. Print(L"gop->QueryMode() returned %r\n", rc);
  68. Print(L"Trying to start GOP with SetMode().\n");
  69. rc = uefi_call_wrapper(gop->SetMode, 2, gop,
  70. gop->Mode ? gop->Mode->Mode : 0);
  71. rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i,
  72. &SizeOfInfo, &info);
  73. }
  74. if (EFI_ERROR(rc)) {
  75. Print(L"%d: Bad response from QueryMode: %r (%d)\n",
  76. i, rc, rc);
  77. continue;
  78. }
  79. if (CompareMem(info, gop->Mode->Info, sizeof (*info)))
  80. continue;
  81. NumPixels = info->VerticalResolution * info->PixelsPerScanLine;
  82. BufferSize = NumPixels * sizeof(UINT32);
  83. if (BufferSize == gop->Mode->FrameBufferSize) {
  84. CopySize = BufferSize;
  85. } else {
  86. CopySize = BufferSize < gop->Mode->FrameBufferSize ?
  87. BufferSize : gop->Mode->FrameBufferSize;
  88. Print(L"height * pitch * pixelsize = %lu buf fb size is %lu; using %lu\n",
  89. BufferSize, gop->Mode->FrameBufferSize, CopySize);
  90. }
  91. PixelBuffer = AllocatePool(BufferSize);
  92. if (!PixelBuffer) {
  93. Print(L"Allocation of 0x%08lx bytes failed.\n",
  94. sizeof(UINT32) * NumPixels);
  95. return;
  96. }
  97. fill_boxes(PixelBuffer, info->HorizontalResolution,
  98. info->VerticalResolution, info->PixelsPerScanLine,
  99. info->PixelFormat, info->PixelInformation);
  100. if (info->PixelFormat == PixelBltOnly) {
  101. Print(L"No linear framebuffer on this device.\n");
  102. return;
  103. }
  104. #if defined(__x86_64__) || defined(__aarch64__) || \
  105. (defined (__riscv) && __riscv_xlen == 64)
  106. FrameBufferAddr = (UINT64)gop->Mode->FrameBufferBase;
  107. #elif defined(__i386__) || defined(__arm__)
  108. FrameBufferAddr = (UINT32)(UINT64)gop->Mode->FrameBufferBase;
  109. #else
  110. #error YOUR ARCH HERE
  111. #endif
  112. CopyMem((VOID *)FrameBufferAddr, PixelBuffer, CopySize);
  113. return;
  114. }
  115. Print(L"Never found the active video mode?\n");
  116. }
  117. static EFI_STATUS
  118. SetWatchdog(UINTN seconds)
  119. {
  120. EFI_STATUS rc;
  121. rc = uefi_call_wrapper(BS->SetWatchdogTimer, 4, seconds, 0x1ffff,
  122. 0, NULL);
  123. if (EFI_ERROR(rc)) {
  124. CHAR16 Buffer[64];
  125. StatusToString(Buffer, rc);
  126. Print(L"Bad response from QueryMode: %s (%d)\n", Buffer, rc);
  127. }
  128. return rc;
  129. }
  130. EFI_STATUS
  131. efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
  132. {
  133. EFI_STATUS rc;
  134. EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
  135. InitializeLib(image_handle, systab);
  136. SetWatchdog(10);
  137. rc = LibLocateProtocol(&GraphicsOutputProtocol, (void **)&gop);
  138. if (EFI_ERROR(rc)) {
  139. Print(L"Could not locate GOP: %r\n", rc);
  140. return rc;
  141. }
  142. if (!gop) {
  143. Print(L"LocateProtocol(GOP, &gop) returned %r but GOP is NULL\n", rc);
  144. return EFI_UNSUPPORTED;
  145. }
  146. draw_boxes(gop);
  147. SetWatchdog(0);
  148. return EFI_SUCCESS;
  149. }