]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/UefiBootManagerLib/BmHotkey.c
MdeModulePkg/UefiBootManagerLib: Remove assertion
[mirror_edk2.git] / MdeModulePkg / Library / UefiBootManagerLib / BmHotkey.c
index af303e8469ab8e9fd542581c2e25a370cf7f28ef..d18ce02eb3461a0ad52cedcbea73ec130ec9383c 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Hotkey library functions.\r
 \r
-Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -41,7 +42,7 @@ VOID                         *mBmTxtInExRegistration  = NULL;
 **/\r
 UINTN\r
 BmSizeOfKeyOption (\r
-  EFI_BOOT_MANAGER_KEY_OPTION  *KeyOption\r
+  IN CONST EFI_BOOT_MANAGER_KEY_OPTION  *KeyOption\r
   )\r
 {\r
   return OFFSET_OF (EFI_BOOT_MANAGER_KEY_OPTION, Keys)\r
@@ -60,8 +61,8 @@ BmSizeOfKeyOption (
 **/\r
 BOOLEAN\r
 BmIsKeyOptionValid (\r
-  IN EFI_BOOT_MANAGER_KEY_OPTION     *KeyOption,\r
-  IN UINTN                           KeyOptionSize\r
+  IN CONST EFI_BOOT_MANAGER_KEY_OPTION *KeyOption,\r
+  IN       UINTN                       KeyOptionSize\r
 )\r
 {\r
   UINT16   OptionName[BM_OPTION_NAME_LEN];\r
@@ -157,16 +158,15 @@ BmCollectKeyOptions (
 {\r
   UINTN                        Index;\r
   BM_COLLECT_KEY_OPTIONS_PARAM *Param;\r
-  EFI_BOOT_MANAGER_KEY_OPTION  *KeyOption;\r
+  VOID                         *KeyOption;\r
   UINT16                       OptionNumber;\r
   UINTN                        KeyOptionSize;\r
 \r
   Param = (BM_COLLECT_KEY_OPTIONS_PARAM *) Context;\r
 \r
   if (BmIsKeyOptionVariable (Name, Guid, &OptionNumber)) {\r
-    GetEfiGlobalVariable2 (Name, (VOID**) &KeyOption, &KeyOptionSize);\r
+    GetEfiGlobalVariable2 (Name, &KeyOption, &KeyOptionSize);\r
     ASSERT (KeyOption != NULL);\r
-    KeyOption->OptionNumber = OptionNumber;\r
     if (BmIsKeyOptionValid (KeyOption, KeyOptionSize)) {\r
       Param->KeyOptions = ReallocatePool (\r
                             Param->KeyOptionCount * sizeof (EFI_BOOT_MANAGER_KEY_OPTION),\r
@@ -178,12 +178,13 @@ BmCollectKeyOptions (
       // Insert the key option in order\r
       //\r
       for (Index = 0; Index < Param->KeyOptionCount; Index++) {\r
-        if (KeyOption->OptionNumber < Param->KeyOptions[Index].OptionNumber) {\r
+        if (OptionNumber < Param->KeyOptions[Index].OptionNumber) {\r
           break;\r
         }\r
       }\r
       CopyMem (&Param->KeyOptions[Index + 1], &Param->KeyOptions[Index], (Param->KeyOptionCount - Index) * sizeof (EFI_BOOT_MANAGER_KEY_OPTION));\r
-      CopyMem (&Param->KeyOptions[Index], KeyOption, BmSizeOfKeyOption (KeyOption));\r
+      CopyMem (&Param->KeyOptions[Index], KeyOption, KeyOptionSize);\r
+      Param->KeyOptions[Index].OptionNumber = OptionNumber;\r
       Param->KeyOptionCount++;\r
     }\r
     FreePool (KeyOption);\r
@@ -219,21 +220,6 @@ BmGetKeyOptions (
   return Param.KeyOptions;\r
 }\r
 \r
-/**\r
-  Callback function for event.\r
-  \r
-  @param    Event          Event for this callback function.\r
-  @param    Context        Context pass to this function.\r
-**/\r
-VOID\r
-EFIAPI\r
-BmEmptyFunction (\r
-  IN EFI_EVENT                Event,\r
-  IN VOID                     *Context\r
-  )\r
-{\r
-}\r
-\r
 /**\r
   Check whether the bit is set in the value.\r
 \r
@@ -440,6 +426,55 @@ BmHotkeyCallback (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Return the active Simple Text Input Ex handle array.\r
+  If the SystemTable.ConsoleInHandle is NULL, the function returns all\r
+  founded Simple Text Input Ex handles.\r
+  Otherwise, it just returns the ConsoleInHandle.\r
+\r
+  @param Count  Return the handle count.\r
+\r
+  @retval The active console handles.\r
+**/\r
+EFI_HANDLE *\r
+BmGetActiveConsoleIn (\r
+  OUT UINTN                             *Count\r
+  )\r
+{\r
+  EFI_STATUS                            Status;\r
+  EFI_HANDLE                            *Handles;\r
+\r
+  Handles = NULL;\r
+  *Count  = 0;\r
+\r
+  if (gST->ConsoleInHandle != NULL) {\r
+    Status = gBS->OpenProtocol (\r
+                    gST->ConsoleInHandle,\r
+                    &gEfiSimpleTextInputExProtocolGuid,\r
+                    NULL,\r
+                    gImageHandle,\r
+                    NULL,\r
+                    EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
+                    );\r
+    if (!EFI_ERROR (Status)) {\r
+      Handles = AllocateCopyPool (sizeof (EFI_HANDLE), &gST->ConsoleInHandle);\r
+      if (Handles != NULL) {\r
+        *Count = 1;\r
+      }\r
+    }\r
+  } else {\r
+    Status = gBS->LocateHandleBuffer (\r
+                    ByProtocol,\r
+                    &gEfiSimpleTextInputExProtocolGuid,\r
+                    NULL,\r
+                    Count,\r
+                    &Handles\r
+                    );\r
+  }\r
+\r
+  return Handles;\r
+}\r
+\r
 /**\r
   Unregister hotkey notify list.\r
 \r
@@ -461,13 +496,7 @@ BmUnregisterHotkeyNotify (
   EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL     *TxtInEx;\r
   VOID                                  *NotifyHandle;\r
 \r
-  gBS->LocateHandleBuffer (\r
-          ByProtocol,\r
-          &gEfiSimpleTextInputExProtocolGuid,\r
-          NULL,\r
-          &HandleCount,\r
-          &Handles\r
-          );\r
+  Handles = BmGetActiveConsoleIn (&HandleCount);\r
   for (Index = 0; Index < HandleCount; Index++) {\r
     Status = gBS->HandleProtocol (Handles[Index], &gEfiSimpleTextInputExProtocolGuid, (VOID **) &TxtInEx);\r
     ASSERT_EFI_ERROR (Status);\r
@@ -485,6 +514,10 @@ BmUnregisterHotkeyNotify (
     }\r
   }\r
 \r
+  if (Handles != NULL) {\r
+    FreePool (Handles);\r
+  }\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -632,10 +665,12 @@ BmProcessKeyOption (
 \r
   KeyShiftStateCount = 0;\r
   BmGenerateKeyShiftState (0, KeyOption, EFI_SHIFT_STATE_VALID, KeyShiftStates, &KeyShiftStateCount);\r
-  ASSERT (KeyShiftStateCount <= sizeof (KeyShiftStates) / sizeof (KeyShiftStates[0]));\r
+  ASSERT (KeyShiftStateCount <= ARRAY_SIZE (KeyShiftStates));\r
 \r
   EfiAcquireLock (&mBmHotkeyLock);\r
 \r
+  Handles = BmGetActiveConsoleIn (&HandleCount);\r
+\r
   for (Index = 0; Index < KeyShiftStateCount; Index++) {\r
     Hotkey = AllocateZeroPool (sizeof (BM_HOTKEY));\r
     ASSERT (Hotkey != NULL);\r
@@ -651,13 +686,6 @@ BmProcessKeyOption (
     }\r
     InsertTailList (&mBmHotkeyList, &Hotkey->Link);\r
 \r
-    gBS->LocateHandleBuffer (\r
-            ByProtocol,\r
-            &gEfiSimpleTextInputExProtocolGuid,\r
-            NULL,\r
-            &HandleCount,\r
-            &Handles\r
-            );\r
     for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {\r
       Status = gBS->HandleProtocol (Handles[HandleIndex], &gEfiSimpleTextInputExProtocolGuid, (VOID **) &TxtInEx);\r
       ASSERT_EFI_ERROR (Status);\r
@@ -665,6 +693,9 @@ BmProcessKeyOption (
     }\r
   }\r
 \r
+  if (Handles != NULL) {\r
+    FreePool (Handles);\r
+  }\r
   EfiReleaseLock (&mBmHotkeyLock);\r
 \r
   return EFI_SUCCESS;\r
@@ -839,13 +870,13 @@ EfiBootManagerStartHotkeyService (
   EFI_EVENT                    Event;\r
   UINT32                       *BootOptionSupport;\r
 \r
-  Status = GetEfiGlobalVariable2 (EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME, (VOID **) &BootOptionSupport, NULL);\r
-  ASSERT (BootOptionSupport != NULL);\r
-\r
-  if ((*BootOptionSupport & EFI_BOOT_OPTION_SUPPORT_KEY)  != 0) {\r
-    mBmHotkeySupportCount = ((*BootOptionSupport & EFI_BOOT_OPTION_SUPPORT_COUNT) >> LowBitSet32 (EFI_BOOT_OPTION_SUPPORT_COUNT));\r
+  GetEfiGlobalVariable2 (EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME, (VOID **) &BootOptionSupport, NULL);\r
+  if (BootOptionSupport != NULL) {\r
+    if ((*BootOptionSupport & EFI_BOOT_OPTION_SUPPORT_KEY)  != 0) {\r
+      mBmHotkeySupportCount = ((*BootOptionSupport & EFI_BOOT_OPTION_SUPPORT_COUNT) >> LowBitSet32 (EFI_BOOT_OPTION_SUPPORT_COUNT));\r
+    }\r
+    FreePool (BootOptionSupport);\r
   }\r
-  FreePool (BootOptionSupport);\r
 \r
   if (mBmHotkeySupportCount == 0) {\r
     DEBUG ((EFI_D_INFO, "Bds: BootOptionSupport NV variable forbids starting the hotkey service.\n"));\r
@@ -855,7 +886,7 @@ EfiBootManagerStartHotkeyService (
   Status = gBS->CreateEvent (\r
                   EVT_NOTIFY_WAIT,\r
                   TPL_CALLBACK,\r
-                  BmEmptyFunction,\r
+                  EfiEventEmptyFunction,\r
                   NULL,\r
                   &mBmHotkeyTriggered\r
                   );\r
@@ -875,13 +906,20 @@ EfiBootManagerStartHotkeyService (
     BmProcessKeyOption (mBmContinueKeyOption);\r
   }\r
 \r
-  EfiCreateProtocolNotifyEvent (\r
-    &gEfiSimpleTextInputExProtocolGuid,\r
-    TPL_CALLBACK,\r
-    BmTxtInExCallback,\r
-    NULL,\r
-    &mBmTxtInExRegistration\r
-    );\r
+  //\r
+  // Hook hotkey on every future SimpleTextInputEx instance when\r
+  // SystemTable.ConsoleInHandle == NULL, which means the console\r
+  // manager (ConSplitter) is absent.\r
+  //\r
+  if (gST->ConsoleInHandle == NULL) {\r
+    EfiCreateProtocolNotifyEvent (\r
+      &gEfiSimpleTextInputExProtocolGuid,\r
+      TPL_CALLBACK,\r
+      BmTxtInExCallback,\r
+      NULL,\r
+      &mBmTxtInExRegistration\r
+      );\r
+  }\r
 \r
   Status = EfiCreateEventReadyToBootEx (\r
              TPL_CALLBACK,\r
@@ -891,7 +929,6 @@ EfiBootManagerStartHotkeyService (
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-\r
   mBmHotkeyServiceStarted = TRUE;\r
   return Status;\r
 }\r