summaryrefslogtreecommitdiff
path: root/QcomModulePkg/Library/BootLib/MenuKeysDetection.c
blob: 38b8a1cb23e0a6e14913938983c382034b12a3fe (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * * Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 *  with the distribution.
 *   * Neither the name of The Linux Foundation nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "AutoGen.h"
#include <Library/DebugLib.h>
#include <Library/DeviceInfo.h>
#include <Library/DeviceInfo.h>
#include <Library/DrawUI.h>
#include <Library/FastbootMenu.h>
#include <Library/KeyPad.h>
#include <Library/LinuxLoaderLib.h>
#include <Library/MenuKeysDetection.h>
#include <Library/Recovery.h>
#include <Library/ShutdownServices.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UnlockMenu.h>
#include <Library/VerifiedBootMenu.h>
#include <Uefi.h>

#include <Protocol/EFIVerifiedBoot.h>

STATIC UINT64 StartTimer;
STATIC EFI_EVENT CallbackKeyDetection;

typedef VOID (*Keys_Action_Func) (OPTION_MENU_INFO *gMsgInfo);

/* Device's action when the volume or power key is pressed
 * Up_Action_Func:   The device's action when the volume up key is pressed
 * Down_Action_Func: The device's action when the volume down key is pressed
 * Enter_Action_Func: The device's action when the power up key is pressed
 */
typedef struct {
  Keys_Action_Func Up_Action_Func;
  Keys_Action_Func Down_Action_Func;
  Keys_Action_Func Enter_Action_Func;
} PAGES_ACTION;

/* Exit the key's detection */
VOID ExitMenuKeysDetection (VOID)
{
  if (FixedPcdGetBool (EnableDisplayMenu)) {
    /* Close the timer and event */
    if (CallbackKeyDetection) {
      gBS->SetTimer (CallbackKeyDetection, TimerCancel, 0);
      gBS->CloseEvent (CallbackKeyDetection);
      CallbackKeyDetection = NULL;
    }
    DEBUG ((EFI_D_INFO, "Exit key detection timer\n"));

    /* Clear the screen */
    gST->ConOut->ClearScreen (gST->ConOut);

    /* Show boot logo */
    RestoreBootLogoBitBuffer ();
  }
}

/* Waiting for exit the menu keys' detection
 * The CallbackKeyDetection would be null when the device is timeout
 * or the user chooses to exit keys' detection.
 * Clear the screen and show the penguin on the screen
 */
VOID WaitForExitKeysDetection (VOID)
{
  if (FixedPcdGetBool (EnableDisplayMenu)) {
    /* Waiting for exit menu keys detection if there is no any usr action
    * otherwise it will do the action base on the keys detection event
    */
    while (CallbackKeyDetection) {
      MicroSecondDelay (10000);
    }
  }
}

STATIC VOID
UpdateDeviceStatus (OPTION_MENU_INFO *MsgInfo, INTN Reason)
{
  CHAR8 FfbmPageBuffer[FFBM_MODE_BUF_SIZE] = "";

  /* Clear the screen */
  gST->ConOut->ClearScreen (gST->ConOut);

  switch (Reason) {
  case RECOVER:
    if (MsgInfo->Info.MenuType == DISPLAY_MENU_UNLOCK ||
        MsgInfo->Info.MenuType == DISPLAY_MENU_LOCK ||
        MsgInfo->Info.MenuType == DISPLAY_MENU_LOCK_CRITICAL ||
        MsgInfo->Info.MenuType == DISPLAY_MENU_UNLOCK_CRITICAL)
      ResetDeviceUnlockStatus (MsgInfo->Info.MenuType);

    RebootDevice (RECOVERY_MODE);
    break;
  case RESTART:
    RebootDevice (NORMAL_MODE);
    break;
  case POWEROFF:
    ShutdownDevice ();
    break;
  case FASTBOOT:
    RebootDevice (FASTBOOT_MODE);
    break;
  case CONTINUE:
    /* Continue boot, no need to detect the keys'status */
    ExitMenuKeysDetection ();
    break;
  case BACK:
    VerifiedBootMenuShowScreen (MsgInfo, MsgInfo->LastMenuType);
    StartTimer = GetTimerCountms ();
    break;
  case FFBM:
    AsciiSPrint (FfbmPageBuffer, sizeof (FfbmPageBuffer), "ffbm-00");
    WriteToPartition (&gEfiMiscPartitionGuid, FfbmPageBuffer,
                      sizeof (FfbmPageBuffer));
    RebootDevice (NORMAL_MODE);
    break;
  case QMMI:
    AsciiSPrint (FfbmPageBuffer, sizeof (FfbmPageBuffer), "ffbm-02");
    WriteToPartition (&gEfiMiscPartitionGuid, FfbmPageBuffer,
                      sizeof (FfbmPageBuffer));
    RebootDevice (NORMAL_MODE);
    break;
  }
}

STATIC VOID
UpdateBackground (OPTION_MENU_INFO *MenuInfo,
                  UINT32 LastIndex,
                  UINT32 CurentIndex)
{
  EFI_STATUS Status, Status0;
  UINT32 LastOption = MenuInfo->Info.OptionItems[LastIndex];
  UINT32 CurrentOption = MenuInfo->Info.OptionItems[CurentIndex];

  Status =
      UpdateMsgBackground (&MenuInfo->Info.MsgInfo[CurrentOption], BGR_BLUE);
  Status0 = UpdateMsgBackground (&MenuInfo->Info.MsgInfo[LastOption],
                                 MenuInfo->Info.MsgInfo[LastOption].BgColor);
  if (Status != EFI_SUCCESS || Status0 != EFI_SUCCESS) {
    DEBUG ((EFI_D_ERROR, "Failed to update option item's background\n"));
    return;
  }
}

/* Update select option's background when volume up key is pressed */
STATIC VOID
MenuVolumeUpFunc (OPTION_MENU_INFO *MenuInfo)
{
  EFI_STATUS Status;
  UINT32 OptionItem = 0;

  UINT32 LastIndex = MenuInfo->Info.OptionIndex;
  UINT32 CurentIndex = LastIndex - 1;

  if (LastIndex == 0 || LastIndex > MenuInfo->Info.OptionNum - 1) {
    CurentIndex = MenuInfo->Info.OptionNum - 1;
    LastIndex = 0;
  }

  MenuInfo->Info.OptionIndex = CurentIndex;
  OptionItem = MenuInfo->Info.OptionItems[CurentIndex];

  if (MenuInfo->Info.MenuType == DISPLAY_MENU_FASTBOOT) {
    Status = UpdateFastbootOptionItem (OptionItem, NULL);
    if (Status != EFI_SUCCESS) {
      DEBUG ((EFI_D_ERROR, "Failed to update fastboot option item\n"));
      return;
    }
  } else {
    UpdateBackground (MenuInfo, LastIndex, CurentIndex);
  }
}

/* Update select option's background when volume down key is pressed */
STATIC VOID
MenuVolumeDownFunc (OPTION_MENU_INFO *MenuInfo)
{
  EFI_STATUS Status;
  UINT32 OptionItem = 0;

  UINT32 LastIndex = MenuInfo->Info.OptionIndex;
  UINT32 CurentIndex = LastIndex + 1;

  if (LastIndex >= MenuInfo->Info.OptionNum - 1) {
    CurentIndex = 0;
    LastIndex = MenuInfo->Info.OptionNum - 1;
  }

  MenuInfo->Info.OptionIndex = CurentIndex;
  OptionItem = MenuInfo->Info.OptionItems[CurentIndex];

  if (MenuInfo->Info.MenuType == DISPLAY_MENU_FASTBOOT) {
    Status = UpdateFastbootOptionItem (OptionItem, NULL);
    if (Status != EFI_SUCCESS) {
      DEBUG ((EFI_D_ERROR, "Failed to update fastboot option item\n"));
      return;
    }
  } else {
    UpdateBackground (MenuInfo, LastIndex, CurentIndex);
  }
}

/* Update device's status via select option */
STATIC VOID
PowerKeyFunc (OPTION_MENU_INFO *MenuInfo)
{
  int Reason = -1;
  UINT32 OptionIndex = MenuInfo->Info.OptionIndex;
  UINT32 OptionItem = 0;
  STATIC BOOLEAN IsRefresh;

  switch (MenuInfo->Info.MenuType) {
  case DISPLAY_MENU_YELLOW:
  case DISPLAY_MENU_ORANGE:
    if (!IsRefresh) {
      /* If the power key is pressed for the first time:
       * Update the warning message and recalculate the timeout
       */
      StartTimer = GetTimerCountms ();
      VerifiedBootMenuUpdateShowScreen (MenuInfo);
      IsRefresh = TRUE;
    } else {
      Reason = CONTINUE;
    }
    break;
  case DISPLAY_MENU_EIO:
    Reason = CONTINUE;
    break;
  case DISPLAY_MENU_MORE_OPTION:
  case DISPLAY_MENU_UNLOCK:
  case DISPLAY_MENU_UNLOCK_CRITICAL:
  case DISPLAY_MENU_LOCK:
  case DISPLAY_MENU_LOCK_CRITICAL:
  case DISPLAY_MENU_FASTBOOT:
    if (OptionIndex < MenuInfo->Info.OptionNum) {
      OptionItem = MenuInfo->Info.OptionItems[OptionIndex];
      Reason = MenuInfo->Info.MsgInfo[OptionItem].Action;
    }
    break;
  default:
    DEBUG ((EFI_D_ERROR, "Unsupported menu type\n"));
    break;
  }

  if (Reason != -1) {
    UpdateDeviceStatus (MenuInfo, Reason);
  }
}

STATIC PAGES_ACTION MenuPagesAction[] = {
        [DISPLAY_MENU_UNLOCK] =
            {
                MenuVolumeUpFunc, MenuVolumeDownFunc, PowerKeyFunc,
            },
        [DISPLAY_MENU_UNLOCK_CRITICAL] =
            {
                MenuVolumeUpFunc, MenuVolumeDownFunc, PowerKeyFunc,
            },
        [DISPLAY_MENU_LOCK] =
            {
                MenuVolumeUpFunc, MenuVolumeDownFunc, PowerKeyFunc,
            },
        [DISPLAY_MENU_LOCK_CRITICAL] =
            {
                MenuVolumeUpFunc, MenuVolumeDownFunc, PowerKeyFunc,
            },
        [DISPLAY_MENU_YELLOW] =
            {
                NULL, NULL, PowerKeyFunc,
            },
        [DISPLAY_MENU_ORANGE] =
            {
                NULL, NULL, PowerKeyFunc,
            },
        [DISPLAY_MENU_EIO] =
            {
                NULL, NULL, PowerKeyFunc,
            },
        [DISPLAY_MENU_MORE_OPTION] =
            {
                MenuVolumeUpFunc, MenuVolumeDownFunc, PowerKeyFunc,
            },
        [DISPLAY_MENU_FASTBOOT] =
            {
                MenuVolumeUpFunc, MenuVolumeDownFunc, PowerKeyFunc,
            },
};

/* Start key detection timer
 * The timer be signaled once after 50ms from the current time
 */
STATIC VOID
StartKeyDetectTimer ()
{
  EFI_STATUS Status;

  Status = gBS->SetTimer (CallbackKeyDetection, TimerRelative, 500000);
  if (Status != EFI_SUCCESS) {
    DEBUG ((EFI_D_ERROR, "ERROR: Failed to set keys detection Timer: %r\n",
            Status));
    ExitMenuKeysDetection ();
  }
}

/**
  Handle key detection's status
  @param[in] Event      The event of key's detection.
  @param[in] Context    The parameter of key's detection.
  @retval EFI_SUCCESS   The entry point is executed successfully.
  @retval other         Some error occurs when executing this entry point.
 **/
STATIC VOID EFIAPI
MenuKeysHandler (IN EFI_EVENT Event, IN VOID *Context)
{
  UINT64 TimerDiff;
  EFI_STATUS Status = EFI_SUCCESS;
  OPTION_MENU_INFO *MenuInfo = Context;
  UINT32 CurrentKey = SCAN_NULL;
  STATIC UINT32 LastKey = SCAN_NULL;
  STATIC UINT64 KeyPressStartTime;

  if (MenuInfo->Info.TimeoutTime > 0) {
    TimerDiff = GetTimerCountms () - StartTimer;
    if (TimerDiff > (MenuInfo->Info.TimeoutTime) * 1000) {
      ExitMenuKeysDetection ();
      if ((MenuInfo->Info.MenuType == DISPLAY_MENU_EIO) ||
          ((MenuInfo->Info.MsgInfo->Action == POWEROFF) &&
           ((MenuInfo->Info.MenuType == DISPLAY_MENU_YELLOW) ||
            (MenuInfo->Info.MenuType == DISPLAY_MENU_ORANGE))))
        ShutdownDevice ();
      return;
    }
  }

  Status = GetKeyPress (&CurrentKey);
  if (Status != EFI_SUCCESS && Status != EFI_NOT_READY) {
    DEBUG ((EFI_D_ERROR, "Error reading key status: %r\n", Status));
    goto Exit;
  }

  /* Initialize the key press start time when the key is pressed or released */
  if (LastKey != CurrentKey)
    KeyPressStartTime = GetTimerCountms ();

  /* Check whether there is user key action that needed to do.
   * There is key pressed currently. SCAN_NULL means there is no keystroke
   * data available. Treat SCAN_NULL as a special key.
   * If there is any key pressed above KEY_HOLD_TIME_MS time. Do action
   * If the current key is SCAN_NULL, it means the key is released. if the last
   * key is not NULL then do action or do nothing.
   */
  TimerDiff = GetTimerCountms () - KeyPressStartTime;
  if (TimerDiff > KEY_HOLD_TIME_MS || CurrentKey == SCAN_NULL) {
    switch (LastKey) {
    case SCAN_UP:
      if (MenuPagesAction[MenuInfo->Info.MenuType].Up_Action_Func != NULL)
        MenuPagesAction[MenuInfo->Info.MenuType].Up_Action_Func (MenuInfo);
      break;
    case SCAN_DOWN:
      if (MenuPagesAction[MenuInfo->Info.MenuType].Down_Action_Func != NULL)
        MenuPagesAction[MenuInfo->Info.MenuType].Down_Action_Func (MenuInfo);
      break;
    case SCAN_SUSPEND:
      if (MenuPagesAction[MenuInfo->Info.MenuType].Enter_Action_Func != NULL)
        MenuPagesAction[MenuInfo->Info.MenuType].Enter_Action_Func (MenuInfo);
      break;
    default:
      break;
    }
    KeyPressStartTime = GetTimerCountms ();
  }

  LastKey = CurrentKey;
  StartKeyDetectTimer ();
  return;

Exit:
  ExitMenuKeysDetection ();
  return;
}

/**
  Create a event and timer to detect key's status
  @param[in] mMenuInfo    The option menu info.
  @retval EFI_SUCCESS     The entry point is executed successfully.
  @retval other           Some error occurs when executing this entry point.
 **/
EFI_STATUS EFIAPI
MenuKeysDetectionInit (IN VOID *mMenuInfo)
{
  EFI_STATUS Status = EFI_SUCCESS;
  OPTION_MENU_INFO *MenuInfo = mMenuInfo;

  if (FixedPcdGetBool (EnableDisplayMenu)) {
    StartTimer = GetTimerCountms ();

    /* Close the timer and event firstly */
    if (CallbackKeyDetection) {
      gBS->SetTimer (CallbackKeyDetection, TimerCancel, 0);
      gBS->CloseEvent (CallbackKeyDetection);
    }

    /* Create event for handle key status */
    Status =
        gBS->CreateEvent (EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
                          MenuKeysHandler, MenuInfo, &CallbackKeyDetection);
    DEBUG ((EFI_D_VERBOSE, "Create keys detection event: %r\n", Status));

    if (!EFI_ERROR (Status) && CallbackKeyDetection)
      StartKeyDetectTimer ();
  }
  return Status;
}