From 2d15a830172675b1685cb413de4187fb9f646695 Mon Sep 17 00:00:00 2001 From: Ruiyu Ni Date: Fri, 18 Mar 2016 15:48:49 +0800 Subject: [PATCH] MdeModulePkg/Bds: BDS hotkey shouldn't work on inactive consoles Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ruiyu Ni Reviewed-by: Feng Tian Reviewed-by: Sunny Wang --- .../Library/UefiBootManagerLib/BmHotkey.c | 96 ++++++++++++++----- 1 file changed, 73 insertions(+), 23 deletions(-) diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmHotkey.c b/MdeModulePkg/Library/UefiBootManagerLib/BmHotkey.c index af303e8469..4cc4fb4554 100644 --- a/MdeModulePkg/Library/UefiBootManagerLib/BmHotkey.c +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmHotkey.c @@ -1,7 +1,7 @@ /** @file Hotkey library functions. -Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -440,6 +440,54 @@ BmHotkeyCallback ( return EFI_SUCCESS; } +/** + Return the active Simple Text Input Ex handle array. + If the SystemTable.ConsoleInHandle is NULL, the function returns all + founded Simple Text Input Ex handles. + Otherwise, it just returns the ConsoleInHandle. + + @param Count Return the handle count. + + @retval The active console handles. +**/ +EFI_HANDLE * +BmGetActiveConsoleIn ( + OUT UINTN *Count + ) +{ + EFI_STATUS Status; + EFI_HANDLE *Handles; + + if (gST->ConsoleInHandle != NULL) { + Status = gBS->OpenProtocol ( + gST->ConsoleInHandle, + &gEfiSimpleTextInputExProtocolGuid, + NULL, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_TEST_PROTOCOL + ); + if (!EFI_ERROR (Status)) { + Handles = AllocateCopyPool (sizeof (EFI_HANDLE *), &gST->ConsoleInHandle); + *Count = 1; + } + } else { + Status = gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiSimpleTextInputExProtocolGuid, + NULL, + Count, + &Handles + ); + } + if (EFI_ERROR (Status)) { + Handles = NULL; + *Count = 0; + } + + return Handles; +} + /** Unregister hotkey notify list. @@ -461,13 +509,7 @@ BmUnregisterHotkeyNotify ( EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *TxtInEx; VOID *NotifyHandle; - gBS->LocateHandleBuffer ( - ByProtocol, - &gEfiSimpleTextInputExProtocolGuid, - NULL, - &HandleCount, - &Handles - ); + Handles = BmGetActiveConsoleIn (&HandleCount); for (Index = 0; Index < HandleCount; Index++) { Status = gBS->HandleProtocol (Handles[Index], &gEfiSimpleTextInputExProtocolGuid, (VOID **) &TxtInEx); ASSERT_EFI_ERROR (Status); @@ -485,6 +527,10 @@ BmUnregisterHotkeyNotify ( } } + if (Handles != NULL) { + FreePool (Handles); + } + return EFI_SUCCESS; } @@ -636,6 +682,8 @@ BmProcessKeyOption ( EfiAcquireLock (&mBmHotkeyLock); + Handles = BmGetActiveConsoleIn (&HandleCount); + for (Index = 0; Index < KeyShiftStateCount; Index++) { Hotkey = AllocateZeroPool (sizeof (BM_HOTKEY)); ASSERT (Hotkey != NULL); @@ -651,13 +699,6 @@ BmProcessKeyOption ( } InsertTailList (&mBmHotkeyList, &Hotkey->Link); - gBS->LocateHandleBuffer ( - ByProtocol, - &gEfiSimpleTextInputExProtocolGuid, - NULL, - &HandleCount, - &Handles - ); for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) { Status = gBS->HandleProtocol (Handles[HandleIndex], &gEfiSimpleTextInputExProtocolGuid, (VOID **) &TxtInEx); ASSERT_EFI_ERROR (Status); @@ -665,6 +706,9 @@ BmProcessKeyOption ( } } + if (Handles != NULL) { + FreePool (Handles); + } EfiReleaseLock (&mBmHotkeyLock); return EFI_SUCCESS; @@ -875,13 +919,20 @@ EfiBootManagerStartHotkeyService ( BmProcessKeyOption (mBmContinueKeyOption); } - EfiCreateProtocolNotifyEvent ( - &gEfiSimpleTextInputExProtocolGuid, - TPL_CALLBACK, - BmTxtInExCallback, - NULL, - &mBmTxtInExRegistration - ); + // + // Hook hotkey on every future SimpleTextInputEx instance when + // SystemTable.ConsoleInHandle == NULL, which means the console + // manager (ConSplitter) is absent. + // + if (gST->ConsoleInHandle == NULL) { + EfiCreateProtocolNotifyEvent ( + &gEfiSimpleTextInputExProtocolGuid, + TPL_CALLBACK, + BmTxtInExCallback, + NULL, + &mBmTxtInExRegistration + ); + } Status = EfiCreateEventReadyToBootEx ( TPL_CALLBACK, @@ -891,7 +942,6 @@ EfiBootManagerStartHotkeyService ( ); ASSERT_EFI_ERROR (Status); - mBmHotkeyServiceStarted = TRUE; return Status; } -- 2.39.2