aboutsummaryrefslogtreecommitdiffstats
path: root/UefiMonitorTest/UefiMonitorTest.c
blob: 1b6e0263e642203cdebdcaa23569adcb6dddece2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>

#include <Protocol/GraphicsOutput.h>

#include "UefiMonitorTest.h"
#include "fonts/System-8x16.h"

#define SWAP(A, B, C)                                               \
    C = A;                                                          \
    A = B;                                                          \
    B = C

#define GET_ICOLOR(Graphics, Ucolor)                                \
    (UINT32)(                                                       \
    (((Ucolor << Graphics->PixelShl[0]) >> Graphics->PixelShr[0]) & \
     Graphics->PixelMasks.RedMask) |                                \
    (((Ucolor << Graphics->PixelShl[1]) >> Graphics->PixelShr[1]) & \
     Graphics->PixelMasks.GreenMask) |                              \
    (((Ucolor << Graphics->PixelShl[2]) >> Graphics->PixelShr[2]) & \
     Graphics->PixelMasks.BlueMask)                                 \
    )

#define PUT_PUXEL(Graphics, X, Y, Icolor) \
    Graphics->BackBuffer[X + (Y * Graphics->Pitch)] = Icolor

CONST EFI_PIXEL_BITMASK mRgbPixelMasks = {
  0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
};

CONST EFI_PIXEL_BITMASK mBgrPixelMasks = {
  0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000
};

STATIC
VOID
ParseGraphicsPixelFormat (
  IN CONST EFI_PIXEL_BITMASK  *BitMask,
  OUT UINT32                  *PixelWidth,
  OUT INT8                    *PixelShl,
  OUT INT8                    *PixelShr
  )
{
  UINT8   Index;
  UINT32  *Masks;
  UINT32  MergedMasks;

  MergedMasks = 0;
  Masks       = (UINT32 *)BitMask;
  for (Index = 0; Index < 3; Index++) {
    ASSERT ((MergedMasks & Masks[Index]) == 0);

    PixelShl[Index] = (INT8)HighBitSet32 (Masks[Index]) - 23 + (Index * 8);
    if (PixelShl[Index] < 0) {
      PixelShr[Index] = -PixelShl[Index];
      PixelShl[Index] = 0;
    } else {
      PixelShr[Index] = 0;
    }

    DEBUG ((
      DEBUG_INFO,
      "Index %d: shl:%d shr:%d mask:%08x\n",
      Index,
      PixelShl[Index],
      PixelShr[Index],
      Masks[Index]
      ));

    MergedMasks = (UINT32)(MergedMasks | Masks[Index]);
  }

  MergedMasks = (UINT32)(MergedMasks | Masks[3]);

  ASSERT (MergedMasks != 0);
  *PixelWidth = (UINT32)((HighBitSet32 (MergedMasks) + 7) / 8);
  DEBUG ((DEBUG_INFO, "Bytes per pixel: %d\n", *PixelWidth));
}

STATIC
VOID
PrepareGraphicsInfo (
  IN GRAPHICS_CONTEXT             *Graphics,
  IN EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop
  )
{
  CONST EFI_PIXEL_BITMASK  *BitMask;
  UINT32                   PixelWidth;
  INT8                     PixelShl[4];
  INT8                     PixelShr[4];

  ASSERT (Graphics != NULL);
  ASSERT (Gop != NULL);

  switch (Gop->Mode->Info->PixelFormat) {
    case PixelRedGreenBlueReserved8BitPerColor:
      BitMask = &mRgbPixelMasks;
      break;

    case PixelBlueGreenRedReserved8BitPerColor:
      BitMask = &mBgrPixelMasks;
      break;

    case PixelBitMask:
      BitMask = &Gop->Mode->Info->PixelInformation;
      break;

    case PixelBltOnly:
      ASSERT (FALSE);
      //return RETURN_UNSUPPORTED;

    default:
      ASSERT (FALSE);
      // return RETURN_INVALID_PARAMETER;
  }

  DEBUG ((
    DEBUG_INFO,
    "GOP information:\n"
    "Mode: %d\n"
    "Framebuffer address, size: %x, %d\n"
    "Screen width x height: %d x %d\n",
    Gop->Mode->Mode,
    Gop->Mode->FrameBufferBase, Gop->Mode->FrameBufferSize,
    Gop->Mode->Info->HorizontalResolution, Gop->Mode->Info->VerticalResolution
    ));

  ParseGraphicsPixelFormat (BitMask, &PixelWidth, PixelShl, PixelShr);
  ASSERT (PixelWidth == 4);

  Graphics->Gop         = Gop;
  Graphics->FrontBuffer = (UINT32 *)Gop->Mode->FrameBufferBase;
  Graphics->BufferSize  = Gop->Mode->FrameBufferSize;
  Graphics->BackBuffer  = AllocateCopyPool (Graphics->BufferSize, Graphics->FrontBuffer);
  ASSERT (Graphics->BackBuffer != NULL);
  Graphics->Width       = Gop->Mode->Info->HorizontalResolution;
  Graphics->Height      = Gop->Mode->Info->VerticalResolution;
  CopyMem (&Graphics->PixelMasks, BitMask, sizeof (*BitMask));
  CopyMem (Graphics->PixelShl, PixelShl, sizeof (PixelShl));
  CopyMem (Graphics->PixelShr, PixelShr, sizeof (PixelShr));
  Graphics->PixelWidth  = PixelWidth;
  Graphics->Pitch       = Gop->Mode->Info->PixelsPerScanLine;
}

STATIC
VOID
ForgetGraphicsInfo (
  IN GRAPHICS_CONTEXT *Graphics
  )
{
  ASSERT (Graphics != NULL);

  // Should we zero all the data structure?

  FreePool (Graphics->BackBuffer);
}

EFI_GRAPHICS_OUTPUT_PROTOCOL *
GetGraphicsOutputProtocol (
  VOID
  )
{
  EFI_STATUS                            Status;
  EFI_GRAPHICS_OUTPUT_PROTOCOL          *Gop;
  EFI_GRAPHICS_OUTPUT_MODE_INFORMATION  *ModeInfo;
  UINTN                                 SizeOfInfo;

  Status = gBS->LocateProtocol (
                  &gEfiGraphicsOutputProtocolGuid,
                  NULL,
                  (VOID **)&Gop
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Unable to locate GOP\n"));
    return NULL;
  }

  Status = Gop->QueryMode (
                  Gop,
                  (Gop->Mode == NULL) ? 0 : Gop->Mode->Mode,
                  &SizeOfInfo,
                  &ModeInfo
                  );
  if (Status == EFI_NOT_STARTED) {
    Status = Gop->SetMode (Gop, 0);
  }
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Unable to get native mode\n"));
    return NULL;
  }

  // TODO: free ModeInfo
  return Gop;
}

/**
  Draw a line using Bresenham's algorithm

  @retval  VOID
**/
STATIC
VOID
DrawLine (
  IN GRAPHICS_CONTEXT   *Graphics,
  IN UINTN              X0,
  IN UINTN              Y0,
  IN UINTN              X1,
  IN UINTN              Y1,
  GRAPHICS_PIXEL_COLOR  *Color
  )
{
  INTN    DeltaX;
  INTN    DeltaY;
  INTN    AbsDeltaX;
  INTN    AbsDeltaY;
  INTN    Correction;
  INT8    Direction;
  UINTN   X, Y, Z;
  BOOLEAN Reverse;
  UINT32  Ucolor;
  UINT32  Icolor;

  ASSERT (X0 >= 0 && X0 < Graphics->Width);
  ASSERT (Y0 >= 0 && Y0 < Graphics->Height);
  ASSERT (X1 >= 0 && X1 < Graphics->Width);
  ASSERT (Y1 >= 0 && Y1 < Graphics->Height);

  DeltaX = X1 - X0;
  DeltaY = Y1 - Y0;
  AbsDeltaX = ABS (DeltaX);
  AbsDeltaY = ABS (DeltaY);

  Reverse = FALSE;
  if (AbsDeltaX < AbsDeltaY) {
    SWAP (X0, Y0, Z);
    SWAP (X1, Y1, Z);
    Reverse = TRUE;
  }

  if (X0 > X1) {
    SWAP (X0, X1, Z);
    SWAP (Y0, Y1, Z);
  }

  DeltaX = X1 - X0;
  DeltaY = Y1 - Y0;
  AbsDeltaY = ABS (DeltaY) * 2;
  Correction = 0;
  Direction = (Y1 > Y0) ? 1 : -1;
  Ucolor = *(UINT32 *)Color;
  Icolor = GET_ICOLOR (Graphics, Ucolor);

  Y = Y0;
  for (X = X0; X <= X1; X++) {
    if (Reverse) {
      PUT_PUXEL (Graphics, Y, X, Icolor);
    } else {
      PUT_PUXEL (Graphics, X, Y, Icolor);
    }
    Correction += AbsDeltaY;

    if (Correction > DeltaX) {
      Y += Direction;
      Correction -= DeltaX * 2;
    }
  }
}

STATIC
VOID
PutRect (
  IN GRAPHICS_CONTEXT   *Graphics,
  IN UINTN              X0,
  IN UINTN              Y0,
  IN UINTN              X1,
  IN UINTN              Y1,
  GRAPHICS_PIXEL_COLOR  *Color
  )
{
  UINT32 *Buffer;
  UINT32 Ucolor;
  UINT32 Icolor;
  UINT32 I, J;

  ASSERT (X0 >= 0 && X0 <= Graphics->Width);
  ASSERT (Y0 >= 0 && Y0 <= Graphics->Height);
  ASSERT (X1 >= 0 && X1 <= Graphics->Width && X1 >= X0);
  ASSERT (Y1 >= 0 && Y1 <= Graphics->Height && Y1 >= Y0);

  Buffer = Graphics->BackBuffer + Y0 * Graphics->Pitch;
  Ucolor = *(UINT32 *)Color;
  Icolor = GET_ICOLOR(Graphics, Ucolor);

  for (J = Y0; J < Y1; J++) {
    for (I = X0; I < X1; I++) {
      Buffer[I] = Icolor;
    }
    Buffer += Graphics->Pitch;
  }
}

/**
  Draws a character to the screen

  @retval  VOID
**/
STATIC
VOID
DrawChar (
  IN GRAPHICS_CONTEXT *Graphics,
  IN UINTN            X,
  IN UINTN            Y,
  IN UINT32           Icolor,
  IN CHAR16           Char
  )
{
  UINTN Index;
  UINTN l, c;

  Index = Char * (SYSTEM8X16_FONT_WIDTH * SYSTEM8X16_FONT_HEIGHT) - (SYSTEM8X16_FONT_WIDTH * SYSTEM8X16_FONT_HEIGHT);
  for (l = 0; l < SYSTEM8X16_FONT_HEIGHT; l++) {
    for (c = 0; c < SYSTEM8X16_FONT_WIDTH; c++) {
      if (gFontSystem8x16[Index] == 1) {
          PUT_PUXEL (Graphics, (X + c), (Y + l), Icolor);
      }
      Index++;
    }
  }
}

STATIC
EFI_STATUS
Run (
  IN GRAPHICS_CONTEXT *Graphics
  )
{
  BOOLEAN Running;

  Running = TRUE;

  while (Running == TRUE)
  {
    // Buffer swap:
    CopyMem (Graphics->FrontBuffer, Graphics->BackBuffer, Graphics->BufferSize);
    Running = FALSE;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
UefiMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_GRAPHICS_OUTPUT_PROTOCOL  *Gop;
  GRAPHICS_CONTEXT              Graphics;
  EFI_STATUS                    Status;

  Status = EFI_SUCCESS;

  Gop = GetGraphicsOutputProtocol ();
  if (Gop == NULL) {
    Print (L"Error: Getting a Graphical Output Protocol is failed\n");
    return EFI_NOT_FOUND;
  }

  PrepareGraphicsInfo (&Graphics, Gop);

  Status = Run (&Graphics);

  ForgetGraphicsInfo (&Graphics);

  return Status;
}