bltgrid.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <efi.h>
  2. #include <efilib.h>
  3. extern EFI_GUID GraphicsOutputProtocol;
  4. static void
  5. fill_boxes(UINT32 *PixelBuffer, UINT32 Width, UINT32 Height)
  6. {
  7. UINT32 y, x = 0;
  8. /*
  9. * This assums BGRR, but it doesn't really matter; we pick red and
  10. * green so it'll just be blue/green if the pixel format is backwards.
  11. */
  12. EFI_GRAPHICS_OUTPUT_BLT_PIXEL Red = {0, 0, 0xff, 0},
  13. Green = {0, 0xff, 0, 0},
  14. *Color;
  15. for (y = 0; y < Height; y++) {
  16. Color = ((y / 32) % 2 == 0) ? &Red : &Green;
  17. for (x = 0; x < Width; x++) {
  18. if (x % 32 == 0 && x != 0)
  19. Color = (Color == &Red) ? &Green : &Red;
  20. PixelBuffer[y * Width + x] = *(UINT32 *)Color;
  21. }
  22. }
  23. }
  24. static void
  25. draw_boxes(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop)
  26. {
  27. int i, imax;
  28. EFI_STATUS rc;
  29. EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
  30. UINTN NumPixels;
  31. UINT32 *PixelBuffer;
  32. UINT32 BufferSize;
  33. if (gop->Mode) {
  34. imax = gop->Mode->MaxMode;
  35. } else {
  36. Print(L"gop->Mode is NULL\n");
  37. return;
  38. }
  39. for (i = 0; i < imax; i++) {
  40. UINTN SizeOfInfo;
  41. rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &SizeOfInfo,
  42. &info);
  43. if (EFI_ERROR(rc) && rc == EFI_NOT_STARTED) {
  44. Print(L"gop->QueryMode() returned %r\n", rc);
  45. Print(L"Trying to start GOP with SetMode().\n");
  46. rc = uefi_call_wrapper(gop->SetMode, 2, gop,
  47. gop->Mode ? gop->Mode->Mode : 0);
  48. rc = uefi_call_wrapper(gop->QueryMode, 4, gop, i,
  49. &SizeOfInfo, &info);
  50. }
  51. if (EFI_ERROR(rc)) {
  52. Print(L"%d: Bad response from QueryMode: %r (%d)\n",
  53. i, rc, rc);
  54. continue;
  55. }
  56. if (CompareMem(info, gop->Mode->Info, sizeof (*info)))
  57. continue;
  58. NumPixels = info->VerticalResolution * info->HorizontalResolution;
  59. BufferSize = NumPixels * sizeof(UINT32);
  60. PixelBuffer = AllocatePool(BufferSize);
  61. if (!PixelBuffer) {
  62. Print(L"Allocation of 0x%08lx bytes failed.\n",
  63. sizeof(UINT32) * NumPixels);
  64. return;
  65. }
  66. fill_boxes(PixelBuffer,
  67. info->HorizontalResolution, info->VerticalResolution);
  68. uefi_call_wrapper(gop->Blt, 10, gop,
  69. (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)PixelBuffer,
  70. EfiBltBufferToVideo,
  71. 0, 0, 0, 0,
  72. info->HorizontalResolution,
  73. info->VerticalResolution,
  74. 0);
  75. return;
  76. }
  77. Print(L"Never found the active video mode?\n");
  78. }
  79. static EFI_STATUS
  80. SetWatchdog(UINTN seconds)
  81. {
  82. EFI_STATUS rc;
  83. rc = uefi_call_wrapper(BS->SetWatchdogTimer, 4, seconds, 0x1ffff,
  84. 0, NULL);
  85. if (EFI_ERROR(rc)) {
  86. CHAR16 Buffer[64];
  87. StatusToString(Buffer, rc);
  88. Print(L"Bad response from QueryMode: %s (%d)\n", Buffer, rc);
  89. }
  90. return rc;
  91. }
  92. EFI_STATUS
  93. efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
  94. {
  95. EFI_STATUS rc;
  96. EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
  97. InitializeLib(image_handle, systab);
  98. SetWatchdog(10);
  99. rc = LibLocateProtocol(&GraphicsOutputProtocol, (void **)&gop);
  100. if (EFI_ERROR(rc)) {
  101. Print(L"Could not locate GOP: %r\n", rc);
  102. return rc;
  103. }
  104. if (!gop) {
  105. Print(L"LocateProtocol(GOP, &gop) returned %r but GOP is NULL\n", rc);
  106. return EFI_UNSUPPORTED;
  107. }
  108. draw_boxes(gop);
  109. SetWatchdog(0);
  110. return EFI_SUCCESS;
  111. }