aboutsummaryrefslogtreecommitdiffstats
path: root/UefiMonitorTest/UefiMonitorTest.c
blob: 52d2c3dd929838c078881ac60f64768de7189765 (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
#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/HiiLib.h>
#include <Library/UefiHiiServicesLib.h>

#include "UefiMonitorTest.h"
#include "MainMenu.h"
#include "SettingsMenu.h"
#include "tests/SolidColors.h"
#include <tests/Grayscale.h>
#include "tests/Gradients.h"
#include "tests/ChessBoard.h"

STATIC CONST UMT_STATE_ACTIONS mStateActions[UMT_STATE_END] = {
  { MainMenuInit, MainMenuDoit, MainMenuTip, MainMenuChangeParam, MainMenuChangeValue },
  { SettingsMenuInit, SettingsMenuDoit, SettingsMenuTip, SettingsChangeParam, SettingsMenuChangeValue },
  { SolidColorsTestInit, SolidColorsTestDoit, SolidColorsTestTip, SolidColorsTestChangeParam, SolidColorsTestChangeValue },
  { GrayscaleTestInit, GrayscaleTestDoit, GrayscaleTestTip, GrayscaleTestChangeParam, GrayscaleTestChangeValue },
  { GradientsTestInit, GradientsTestDoit, GradientsTestTip, GradientsTestChangeParam, GradientsTestChangeValue },
  { ChessBoardTestInit, ChessBoardTestDoit, ChessBoardTestTip, ChessBoardTestChangeParam, ChessBoardTestChangeValue }
};

EFI_HII_HANDLE gUmtHiiHandle = NULL;

STATIC
EFI_STATUS
RegisterHiiPackage (
  IN  EFI_HANDLE      ImageHandle,
  OUT EFI_HII_HANDLE  *HiiHandle
  )
{
  EFI_STATUS                  Status;
  EFI_HII_PACKAGE_LIST_HEADER *PackageList;

  // Retrieve HII package list from ImageHandle
  Status = gBS->OpenProtocol (
                  ImageHandle,
                  &gEfiHiiPackageListProtocolGuid,
                  (VOID **)&PackageList,
                  ImageHandle,
                  NULL,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Failed to open EFI_HII_PACKAGE_LIST_PROTOCOL\n"));
    return Status;
  }

  Status = gHiiDatabase->NewPackageList(gHiiDatabase, PackageList, NULL, HiiHandle);
  if (EFI_ERROR(Status)) {
    DEBUG ((DEBUG_ERROR, "Failed to add HII Package list to HII database: %r\n", Status));
    return Status;
  }

  return Status;
}

STATIC
EFI_STATUS
UnregisterHiiPackage (
  IN  EFI_HII_HANDLE  HiiHandle
  )
{
  EFI_STATUS  Status;

  Status = gHiiDatabase->RemovePackageList(gHiiDatabase, HiiHandle);
  if (EFI_ERROR(Status)) {
    DEBUG ((DEBUG_WARN, "Failed to remove HII Package list from HII database: %r\n", Status));
  }

  return Status;
}

STATIC
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;
}

STATIC
VOID
ChangeCtxState (
  IN  UMT_CONTEXT     *Ctx,
  IN  enum UMT_STATE  State
  )
{
  if (State >= UMT_STATE_END)
    return;

  Ctx->State   = State;
  Ctx->ShowTip = FALSE;
  Ctx->Actions = &mStateActions[State];
  Ctx->Actions->Init (Ctx);
}

/**
  Handle user key input. Will route it to other components handle function

  @retval VOID
**/
STATIC
VOID
HandleInput (
  IN UMT_CONTEXT *Ctx
  )
{
  EFI_KEY_DATA  KeyData;
  EFI_STATUS    Status;

  // TODO: check mouse activity

  // Check keyboard activity
  Status = gBS->CheckEvent (Ctx->Graphics->TextInputEx->WaitForKeyEx);
  if (EFI_ERROR (Status))
    return;

  Status = Ctx->Graphics->TextInputEx->ReadKeyStrokeEx (Ctx->Graphics->TextInputEx, &KeyData);
  if (EFI_ERROR (Status))
    return;

  switch (KeyData.Key.ScanCode) {
    case SCAN_UP:
      Ctx->Actions->ChangeParam (Ctx, -1);
      break;

    case SCAN_DOWN:
      Ctx->Actions->ChangeParam (Ctx, +1);
      break;

    case SCAN_RIGHT:
      Ctx->Actions->ChangeValue (Ctx, +1);
      break;

    case SCAN_LEFT:
      Ctx->Actions->ChangeValue (Ctx, -1);
      break;

    case SCAN_F1...SCAN_F11:
      ChangeCtxState (Ctx, KeyData.Key.ScanCode - SCAN_F1 + UMT_STATE_SOLID_COLORS_TEST);
      break;

    case SCAN_F12:
      ChangeCtxState (Ctx, UMT_STATE_SETTINGS);
      break;

    case SCAN_ESC:
      if (Ctx->State != UMT_STATE_MAIN_MENU) {
        ChangeCtxState (Ctx, UMT_STATE_MAIN_MENU);
        break;
      }

      Ctx->Running = FALSE;
      break;

    default:
      break;
  }

  if (KeyData.Key.ScanCode == SCAN_NULL && KeyData.Key.UnicodeChar == L' ') {
    Ctx->ShowTip = !Ctx->ShowTip;
    Ctx->Actions->Tip (Ctx);
  }
}

STATIC
EFI_STATUS
Run (
  IN GRAPHICS_CONTEXT *Graphics
  )
{
  UMT_CONTEXT Ctx;

  Ctx.Running   = TRUE;
  Ctx.Graphics  = Graphics;
  ChangeCtxState (&Ctx, UMT_STATE_MAIN_MENU);

  while (Ctx.Running == TRUE)
  {
    HandleInput (&Ctx);

    Ctx.Actions->Doit (&Ctx);

    // Buffer swap:
    CopyMem (Graphics->FrontBuffer, Graphics->BackBuffer, Graphics->BufferSize);
  }

  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;

  Status = RegisterHiiPackage (ImageHandle, &gUmtHiiHandle);
  if (EFI_ERROR(Status)) {
    Print (L"Error: Failed to register HII Package list\n");
    return Status;
  }

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

  Status = PrepareGraphicsInfo (&Graphics, Gop);
  if (EFI_ERROR(Status)) {
    Print (L"Error: Preparing graphics information is failed. %r\n", Status);
    return EFI_NOT_FOUND;
  }

  Status = Run (&Graphics);

  ForgetGraphicsInfo (&Graphics);
  gST->ConOut->ClearScreen(gST->ConOut);

  UnregisterHiiPackage (gUmtHiiHandle);

  return Status;
}