lfbgrid.c 4.1 KB

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