Debian Codegraph @

gnu-efi/apps/lfbgrid.c

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