2 Hotkey library functions.
4 Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalBm.h"
18 // Lock for linked list
20 EFI_LOCK mBmHotkeyLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
21 LIST_ENTRY mBmHotkeyList
= INITIALIZE_LIST_HEAD_VARIABLE (mBmHotkeyList
);
22 EFI_EVENT mBmHotkeyTriggered
= NULL
;
23 BOOLEAN mBmHotkeyServiceStarted
= FALSE
;
24 UINTN mBmHotkeySupportCount
= 0;
27 // Set OptionNumber as unassigned value to indicate the option isn't initialized
29 EFI_BOOT_MANAGER_LOAD_OPTION mBmHotkeyBootOption
= { LoadOptionNumberUnassigned
};
31 EFI_BOOT_MANAGER_KEY_OPTION
*mBmContinueKeyOption
= NULL
;
32 VOID
*mBmTxtInExRegistration
= NULL
;
36 Return the buffer size of the EFI_BOOT_MANAGER_KEY_OPTION data.
38 @param KeyOption The input key option info.
40 @retval The buffer size of the key option data.
44 EFI_BOOT_MANAGER_KEY_OPTION
*KeyOption
47 return OFFSET_OF (EFI_BOOT_MANAGER_KEY_OPTION
, Keys
)
48 + KeyOption
->KeyData
.Options
.InputKeyCount
* sizeof (EFI_INPUT_KEY
);
53 Check whether the input key option is valid.
55 @param KeyOption Key option.
56 @param KeyOptionSize Size of the key option.
58 @retval TRUE Input key option is valid.
59 @retval FALSE Input key option is not valid.
63 IN EFI_BOOT_MANAGER_KEY_OPTION
*KeyOption
,
64 IN UINTN KeyOptionSize
67 UINT16 OptionName
[BM_OPTION_NAME_LEN
];
72 if (BmSizeOfKeyOption (KeyOption
) != KeyOptionSize
) {
77 // Check whether corresponding Boot Option exist
80 OptionName
, sizeof (OptionName
), L
"%s%04x",
81 mBmLoadOptionName
[LoadOptionTypeBoot
], KeyOption
->BootOption
83 GetEfiGlobalVariable2 (OptionName
, (VOID
**) &BootOption
, &BootOptionSize
);
85 if (BootOption
== NULL
) {
90 // Check CRC for Boot Option
92 gBS
->CalculateCrc32 (BootOption
, BootOptionSize
, &Crc
);
93 FreePool (BootOption
);
95 return (BOOLEAN
) (KeyOption
->BootOptionCrc
== Crc
);
100 Check whether the input variable is an key option variable.
102 @param Name Input variable name.
103 @param Guid Input variable guid.
104 @param OptionNumber The option number of this key option variable.
106 @retval TRUE Input variable is a key option variable.
107 @retval FALSE Input variable is not a key option variable.
110 BmIsKeyOptionVariable (
119 if (!CompareGuid (Guid
, &gEfiGlobalVariableGuid
) ||
120 (StrSize (Name
) != sizeof (L
"Key####")) ||
121 (StrnCmp (Name
, L
"Key", 3) != 0)
127 for (Index
= 3; Index
< 7; Index
++) {
128 Uint
= BmCharToUint (Name
[Index
]);
132 *OptionNumber
= (UINT16
) Uint
+ *OptionNumber
* 0x10;
140 EFI_BOOT_MANAGER_KEY_OPTION
*KeyOptions
;
141 UINTN KeyOptionCount
;
142 } BM_COLLECT_KEY_OPTIONS_PARAM
;
145 Visitor function to collect the key options from NV storage.
147 @param Name Variable name.
148 @param Guid Variable GUID.
149 @param Context The same context passed to BmForEachVariable.
152 BmCollectKeyOptions (
159 BM_COLLECT_KEY_OPTIONS_PARAM
*Param
;
160 EFI_BOOT_MANAGER_KEY_OPTION
*KeyOption
;
164 Param
= (BM_COLLECT_KEY_OPTIONS_PARAM
*) Context
;
166 if (BmIsKeyOptionVariable (Name
, Guid
, &OptionNumber
)) {
167 GetEfiGlobalVariable2 (Name
, (VOID
**) &KeyOption
, &KeyOptionSize
);
168 ASSERT (KeyOption
!= NULL
);
169 KeyOption
->OptionNumber
= OptionNumber
;
170 if (BmIsKeyOptionValid (KeyOption
, KeyOptionSize
)) {
171 Param
->KeyOptions
= ReallocatePool (
172 Param
->KeyOptionCount
* sizeof (EFI_BOOT_MANAGER_KEY_OPTION
),
173 (Param
->KeyOptionCount
+ 1) * sizeof (EFI_BOOT_MANAGER_KEY_OPTION
),
176 ASSERT (Param
->KeyOptions
!= NULL
);
178 // Insert the key option in order
180 for (Index
= 0; Index
< Param
->KeyOptionCount
; Index
++) {
181 if (KeyOption
->OptionNumber
< Param
->KeyOptions
[Index
].OptionNumber
) {
185 CopyMem (&Param
->KeyOptions
[Index
+ 1], &Param
->KeyOptions
[Index
], (Param
->KeyOptionCount
- Index
) * sizeof (EFI_BOOT_MANAGER_KEY_OPTION
));
186 CopyMem (&Param
->KeyOptions
[Index
], KeyOption
, BmSizeOfKeyOption (KeyOption
));
187 Param
->KeyOptionCount
++;
189 FreePool (KeyOption
);
194 Return the array of key options.
196 @param Count Return the number of key options.
198 @retval NULL No key option.
199 @retval Other Pointer to the key options.
201 EFI_BOOT_MANAGER_KEY_OPTION
*
206 BM_COLLECT_KEY_OPTIONS_PARAM Param
;
212 Param
.KeyOptions
= NULL
;
213 Param
.KeyOptionCount
= 0;
215 BmForEachVariable (BmCollectKeyOptions
, (VOID
*) &Param
);
217 *Count
= Param
.KeyOptionCount
;
219 return Param
.KeyOptions
;
223 Callback function for event.
225 @param Event Event for this callback function.
226 @param Context Context pass to this function.
238 Check whether the bit is set in the value.
240 @param Value The value need to be check.
241 @param Bit The bit filed need to be check.
243 @retval TRUE The bit is set.
244 @retval FALSE The bit is not set.
252 return (BOOLEAN
) ((Value
& Bit
) != 0);
256 Initialize the KeyData and Key[] in the EFI_BOOT_MANAGER_KEY_OPTION.
258 @param Modifier Input key info.
259 @param Args Va_list info.
260 @param KeyOption Key info which need to update.
262 @retval EFI_SUCCESS Succeed to initialize the KeyData and Key[].
263 @return EFI_INVALID_PARAMETER Input parameter error.
266 BmInitializeKeyFields (
269 OUT EFI_BOOT_MANAGER_KEY_OPTION
*KeyOption
274 if (KeyOption
== NULL
) {
275 return EFI_INVALID_PARAMETER
;
279 while (KeyOption
->KeyData
.Options
.InputKeyCount
< sizeof (KeyOption
->Keys
) / sizeof (KeyOption
->Keys
[0])) {
280 Key
= VA_ARG (Args
, EFI_INPUT_KEY
*);
285 &KeyOption
->Keys
[KeyOption
->KeyData
.Options
.InputKeyCount
],
287 sizeof (EFI_INPUT_KEY
)
289 KeyOption
->KeyData
.Options
.InputKeyCount
++;
296 return EFI_INVALID_PARAMETER
;
299 if ((Modifier
& ~(EFI_BOOT_MANAGER_SHIFT_PRESSED
300 | EFI_BOOT_MANAGER_CONTROL_PRESSED
301 | EFI_BOOT_MANAGER_ALT_PRESSED
302 | EFI_BOOT_MANAGER_LOGO_PRESSED
303 | EFI_BOOT_MANAGER_MENU_KEY_PRESSED
304 | EFI_BOOT_MANAGER_SYS_REQ_PRESSED
306 return EFI_INVALID_PARAMETER
;
309 if (BmBitSet (Modifier
, EFI_BOOT_MANAGER_SHIFT_PRESSED
)) {
310 KeyOption
->KeyData
.Options
.ShiftPressed
= 1;
312 if (BmBitSet (Modifier
, EFI_BOOT_MANAGER_CONTROL_PRESSED
)) {
313 KeyOption
->KeyData
.Options
.ControlPressed
= 1;
315 if (BmBitSet (Modifier
, EFI_BOOT_MANAGER_ALT_PRESSED
)) {
316 KeyOption
->KeyData
.Options
.AltPressed
= 1;
318 if (BmBitSet (Modifier
, EFI_BOOT_MANAGER_LOGO_PRESSED
)) {
319 KeyOption
->KeyData
.Options
.LogoPressed
= 1;
321 if (BmBitSet (Modifier
, EFI_BOOT_MANAGER_MENU_KEY_PRESSED
)) {
322 KeyOption
->KeyData
.Options
.MenuPressed
= 1;
324 if (BmBitSet (Modifier
, EFI_BOOT_MANAGER_SYS_REQ_PRESSED
)) {
325 KeyOption
->KeyData
.Options
.SysReqPressed
= 1;
332 Try to boot the boot option triggered by hot key.
336 EfiBootManagerHotkeyBoot (
340 if (mBmHotkeyBootOption
.OptionNumber
!= LoadOptionNumberUnassigned
) {
341 EfiBootManagerBoot (&mBmHotkeyBootOption
);
342 EfiBootManagerFreeLoadOption (&mBmHotkeyBootOption
);
343 mBmHotkeyBootOption
.OptionNumber
= LoadOptionNumberUnassigned
;
348 This is the common notification function for HotKeys, it will be registered
349 with SimpleTextInEx protocol interface - RegisterKeyNotify() of ConIn handle.
351 @param KeyData A pointer to a buffer that is filled in with the keystroke
352 information for the key that was pressed.
354 @retval EFI_SUCCESS KeyData is successfully processed.
355 @return EFI_NOT_FOUND Fail to find boot option variable.
360 IN EFI_KEY_DATA
*KeyData
365 CHAR16 OptionName
[BM_OPTION_NAME_LEN
];
367 EFI_KEY_DATA
*HotkeyData
;
369 if (mBmHotkeyBootOption
.OptionNumber
!= LoadOptionNumberUnassigned
) {
371 // Do not process sequential hotkey stroke until the current boot option returns
376 DEBUG ((EFI_D_INFO
, "[Bds]BmHotkeyCallback: %04x:%04x\n", KeyData
->Key
.ScanCode
, KeyData
->Key
.UnicodeChar
));
378 EfiAcquireLock (&mBmHotkeyLock
);
379 for ( Link
= GetFirstNode (&mBmHotkeyList
)
380 ; !IsNull (&mBmHotkeyList
, Link
)
381 ; Link
= GetNextNode (&mBmHotkeyList
, Link
)
383 Hotkey
= BM_HOTKEY_FROM_LINK (Link
);
386 // Is this Key Stroke we are waiting for?
388 ASSERT (Hotkey
->WaitingKey
< (sizeof (Hotkey
->KeyData
) / sizeof (Hotkey
->KeyData
[0])));
389 HotkeyData
= &Hotkey
->KeyData
[Hotkey
->WaitingKey
];
390 if ((KeyData
->Key
.ScanCode
== HotkeyData
->Key
.ScanCode
) &&
391 (KeyData
->Key
.UnicodeChar
== HotkeyData
->Key
.UnicodeChar
) &&
392 (((KeyData
->KeyState
.KeyShiftState
& EFI_SHIFT_STATE_VALID
) != 0) ?
393 (KeyData
->KeyState
.KeyShiftState
== HotkeyData
->KeyState
.KeyShiftState
) : TRUE
398 // Receive an expecting key stroke, transit to next waiting state
400 Hotkey
->WaitingKey
++;
402 if (Hotkey
->WaitingKey
== Hotkey
->CodeCount
) {
404 // Reset to initial waiting state
406 Hotkey
->WaitingKey
= 0;
408 // Received the whole key stroke sequence
410 Status
= gBS
->SignalEvent (mBmHotkeyTriggered
);
411 ASSERT_EFI_ERROR (Status
);
413 if (!Hotkey
->IsContinue
) {
415 // Launch its BootOption
418 OptionName
, sizeof (OptionName
), L
"%s%04x",
419 mBmLoadOptionName
[LoadOptionTypeBoot
], Hotkey
->BootOption
421 Status
= EfiBootManagerVariableToLoadOption (OptionName
, &mBmHotkeyBootOption
);
422 DEBUG ((EFI_D_INFO
, "[Bds]Hotkey for %s pressed - %r\n", OptionName
, Status
));
423 if (EFI_ERROR (Status
)) {
424 mBmHotkeyBootOption
.OptionNumber
= LoadOptionNumberUnassigned
;
427 DEBUG ((EFI_D_INFO
, "[Bds]Continue key pressed!\n"));
432 // Receive an unexpected key stroke, reset to initial waiting state
434 Hotkey
->WaitingKey
= 0;
438 EfiReleaseLock (&mBmHotkeyLock
);
444 Return the active Simple Text Input Ex handle array.
445 If the SystemTable.ConsoleInHandle is NULL, the function returns all
446 founded Simple Text Input Ex handles.
447 Otherwise, it just returns the ConsoleInHandle.
449 @param Count Return the handle count.
451 @retval The active console handles.
454 BmGetActiveConsoleIn (
464 if (gST
->ConsoleInHandle
!= NULL
) {
465 Status
= gBS
->OpenProtocol (
466 gST
->ConsoleInHandle
,
467 &gEfiSimpleTextInputExProtocolGuid
,
471 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
473 if (!EFI_ERROR (Status
)) {
474 Handles
= AllocateCopyPool (sizeof (EFI_HANDLE
*), &gST
->ConsoleInHandle
);
478 Status
= gBS
->LocateHandleBuffer (
480 &gEfiSimpleTextInputExProtocolGuid
,
491 Unregister hotkey notify list.
493 @param Hotkey Hotkey list.
495 @retval EFI_SUCCESS Unregister hotkey notify success.
496 @retval Others Unregister hotkey notify failed.
499 BmUnregisterHotkeyNotify (
508 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*TxtInEx
;
511 Handles
= BmGetActiveConsoleIn (&HandleCount
);
512 for (Index
= 0; Index
< HandleCount
; Index
++) {
513 Status
= gBS
->HandleProtocol (Handles
[Index
], &gEfiSimpleTextInputExProtocolGuid
, (VOID
**) &TxtInEx
);
514 ASSERT_EFI_ERROR (Status
);
515 for (KeyIndex
= 0; KeyIndex
< Hotkey
->CodeCount
; KeyIndex
++) {
516 Status
= TxtInEx
->RegisterKeyNotify (
518 &Hotkey
->KeyData
[KeyIndex
],
522 if (!EFI_ERROR (Status
)) {
523 Status
= TxtInEx
->UnregisterKeyNotify (TxtInEx
, NotifyHandle
);
524 DEBUG ((EFI_D_INFO
, "[Bds]UnregisterKeyNotify: %04x/%04x %r\n", Hotkey
->KeyData
[KeyIndex
].Key
.ScanCode
, Hotkey
->KeyData
[KeyIndex
].Key
.UnicodeChar
, Status
));
529 if (Handles
!= NULL
) {
537 Register hotkey notify list.
539 @param TxtInEx Pointer to EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL protocol.
540 @param Hotkey Hotkey list.
542 @retval EFI_SUCCESS Register hotkey notify success.
543 @retval Others Register hotkey notify failed.
546 BmRegisterHotkeyNotify (
547 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*TxtInEx
,
555 for (Index
= 0; Index
< Hotkey
->CodeCount
; Index
++) {
556 Status
= TxtInEx
->RegisterKeyNotify (
558 &Hotkey
->KeyData
[Index
],
564 "[Bds]RegisterKeyNotify: %04x/%04x %08x/%02x %r\n",
565 Hotkey
->KeyData
[Index
].Key
.ScanCode
,
566 Hotkey
->KeyData
[Index
].Key
.UnicodeChar
,
567 Hotkey
->KeyData
[Index
].KeyState
.KeyShiftState
,
568 Hotkey
->KeyData
[Index
].KeyState
.KeyToggleState
,
571 if (EFI_ERROR (Status
)) {
573 // some of the hotkey registry failed
574 // do not unregister all in case we have both CTRL-ALT-P and CTRL-ALT-P-R
584 Generate key shift state base on the input key option info.
586 @param Depth Which key is checked.
587 @param KeyOption Input key option info.
588 @param KeyShiftState Input key shift state.
589 @param KeyShiftStates Return possible key shift state array.
590 @param KeyShiftStateCount Possible key shift state count.
593 BmGenerateKeyShiftState (
595 IN EFI_BOOT_MANAGER_KEY_OPTION
*KeyOption
,
596 IN UINT32 KeyShiftState
,
597 IN UINT32
*KeyShiftStates
,
598 IN UINTN
*KeyShiftStateCount
603 if (KeyOption
->KeyData
.Options
.ShiftPressed
) {
604 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
| EFI_RIGHT_SHIFT_PRESSED
, KeyShiftStates
, KeyShiftStateCount
);
605 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
| EFI_LEFT_SHIFT_PRESSED
, KeyShiftStates
, KeyShiftStateCount
);
607 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
, KeyShiftStates
, KeyShiftStateCount
);
612 if (KeyOption
->KeyData
.Options
.ControlPressed
) {
613 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
| EFI_RIGHT_CONTROL_PRESSED
, KeyShiftStates
, KeyShiftStateCount
);
614 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
| EFI_LEFT_CONTROL_PRESSED
, KeyShiftStates
, KeyShiftStateCount
);
616 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
, KeyShiftStates
, KeyShiftStateCount
);
621 if (KeyOption
->KeyData
.Options
.AltPressed
) {
622 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
| EFI_RIGHT_ALT_PRESSED
, KeyShiftStates
, KeyShiftStateCount
);
623 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
| EFI_LEFT_ALT_PRESSED
, KeyShiftStates
, KeyShiftStateCount
);
625 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
, KeyShiftStates
, KeyShiftStateCount
);
629 if (KeyOption
->KeyData
.Options
.LogoPressed
) {
630 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
| EFI_RIGHT_LOGO_PRESSED
, KeyShiftStates
, KeyShiftStateCount
);
631 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
| EFI_LEFT_LOGO_PRESSED
, KeyShiftStates
, KeyShiftStateCount
);
633 BmGenerateKeyShiftState (Depth
+ 1, KeyOption
, KeyShiftState
, KeyShiftStates
, KeyShiftStateCount
);
637 if (KeyOption
->KeyData
.Options
.MenuPressed
) {
638 KeyShiftState
|= EFI_MENU_KEY_PRESSED
;
640 if (KeyOption
->KeyData
.Options
.SysReqPressed
) {
641 KeyShiftState
|= EFI_SYS_REQ_PRESSED
;
643 KeyShiftStates
[*KeyShiftStateCount
] = KeyShiftState
;
644 (*KeyShiftStateCount
)++;
650 Add it to hot key database, register it to existing TxtInEx.
651 New TxtInEx will be automatically registered with all the hot key in dababase
653 @param KeyOption Input key option info.
657 IN EFI_BOOT_MANAGER_KEY_OPTION
*KeyOption
661 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*TxtInEx
;
669 // 16 is enough to enumerate all the possible combination of LEFT_XXX and RIGHT_XXX
671 UINT32 KeyShiftStates
[16];
672 UINTN KeyShiftStateCount
;
674 if (KeyOption
->KeyData
.Options
.InputKeyCount
> mBmHotkeySupportCount
) {
675 return EFI_UNSUPPORTED
;
678 KeyShiftStateCount
= 0;
679 BmGenerateKeyShiftState (0, KeyOption
, EFI_SHIFT_STATE_VALID
, KeyShiftStates
, &KeyShiftStateCount
);
680 ASSERT (KeyShiftStateCount
<= sizeof (KeyShiftStates
) / sizeof (KeyShiftStates
[0]));
682 EfiAcquireLock (&mBmHotkeyLock
);
684 Handles
= BmGetActiveConsoleIn (&HandleCount
);
686 for (Index
= 0; Index
< KeyShiftStateCount
; Index
++) {
687 Hotkey
= AllocateZeroPool (sizeof (BM_HOTKEY
));
688 ASSERT (Hotkey
!= NULL
);
690 Hotkey
->Signature
= BM_HOTKEY_SIGNATURE
;
691 Hotkey
->BootOption
= KeyOption
->BootOption
;
692 Hotkey
->IsContinue
= (BOOLEAN
) (KeyOption
== mBmContinueKeyOption
);
693 Hotkey
->CodeCount
= (UINT8
) KeyOption
->KeyData
.Options
.InputKeyCount
;
695 for (KeyIndex
= 0; KeyIndex
< Hotkey
->CodeCount
; KeyIndex
++) {
696 CopyMem (&Hotkey
->KeyData
[KeyIndex
].Key
, &KeyOption
->Keys
[KeyIndex
], sizeof (EFI_INPUT_KEY
));
697 Hotkey
->KeyData
[KeyIndex
].KeyState
.KeyShiftState
= KeyShiftStates
[Index
];
699 InsertTailList (&mBmHotkeyList
, &Hotkey
->Link
);
701 for (HandleIndex
= 0; HandleIndex
< HandleCount
; HandleIndex
++) {
702 Status
= gBS
->HandleProtocol (Handles
[HandleIndex
], &gEfiSimpleTextInputExProtocolGuid
, (VOID
**) &TxtInEx
);
703 ASSERT_EFI_ERROR (Status
);
704 BmRegisterHotkeyNotify (TxtInEx
, Hotkey
);
708 if (Handles
!= NULL
) {
711 EfiReleaseLock (&mBmHotkeyLock
);
717 Callback function for SimpleTextInEx protocol install events
719 @param Event the event that is signaled.
720 @param Context not used here.
733 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*TxtInEx
;
737 BufferSize
= sizeof (EFI_HANDLE
);
738 Status
= gBS
->LocateHandle (
741 mBmTxtInExRegistration
,
745 if (EFI_ERROR (Status
)) {
747 // If no more notification events exist
752 Status
= gBS
->HandleProtocol (
754 &gEfiSimpleTextInputExProtocolGuid
,
757 ASSERT_EFI_ERROR (Status
);
760 // Register the hot key notification for the existing items in the list
762 EfiAcquireLock (&mBmHotkeyLock
);
763 for (Link
= GetFirstNode (&mBmHotkeyList
); !IsNull (&mBmHotkeyList
, Link
); Link
= GetNextNode (&mBmHotkeyList
, Link
)) {
764 BmRegisterHotkeyNotify (TxtInEx
, BM_HOTKEY_FROM_LINK (Link
));
766 EfiReleaseLock (&mBmHotkeyLock
);
771 Free the key options returned from BmGetKeyOptions.
773 @param KeyOptions Pointer to the key options.
774 @param KeyOptionCount Number of the key options.
776 @retval EFI_SUCCESS The key options are freed.
777 @retval EFI_NOT_FOUND KeyOptions is NULL.
781 IN EFI_BOOT_MANAGER_KEY_OPTION
*KeyOptions
,
782 IN UINTN KeyOptionCount
785 if (KeyOptions
!= NULL
) {
786 FreePool (KeyOptions
);
789 return EFI_NOT_FOUND
;
794 Register the key option to exit the waiting of the Boot Manager timeout.
795 Platform should ensure that the continue key option isn't conflict with
796 other boot key options.
798 @param Modifier Key shift state.
799 @param ... Parameter list of pointer of EFI_INPUT_KEY.
801 @retval EFI_SUCCESS Successfully register the continue key option.
802 @retval EFI_ALREADY_STARTED The continue key option is already registered.
806 EfiBootManagerRegisterContinueKeyOption (
812 EFI_BOOT_MANAGER_KEY_OPTION KeyOption
;
815 if (mBmContinueKeyOption
!= NULL
) {
816 return EFI_ALREADY_STARTED
;
819 ZeroMem (&KeyOption
, sizeof (EFI_BOOT_MANAGER_KEY_OPTION
));
820 VA_START (Args
, Modifier
);
821 Status
= BmInitializeKeyFields (Modifier
, Args
, &KeyOption
);
824 if (!EFI_ERROR (Status
)) {
825 mBmContinueKeyOption
= AllocateCopyPool (sizeof (EFI_BOOT_MANAGER_KEY_OPTION
), &KeyOption
);
826 ASSERT (mBmContinueKeyOption
!= NULL
);
827 if (mBmHotkeyServiceStarted
) {
828 BmProcessKeyOption (mBmContinueKeyOption
);
836 Stop the hotkey processing.
838 @param Event Event pointer related to hotkey service.
839 @param Context Context pass to this function.
843 BmStopHotkeyService (
851 DEBUG ((EFI_D_INFO
, "[Bds]Stop Hotkey Service!\n"));
852 gBS
->CloseEvent (Event
);
854 EfiAcquireLock (&mBmHotkeyLock
);
855 for (Link
= GetFirstNode (&mBmHotkeyList
); !IsNull (&mBmHotkeyList
, Link
); ) {
856 Hotkey
= BM_HOTKEY_FROM_LINK (Link
);
857 BmUnregisterHotkeyNotify (Hotkey
);
858 Link
= RemoveEntryList (Link
);
861 EfiReleaseLock (&mBmHotkeyLock
);
865 Start the hot key service so that the key press can trigger the boot option.
867 @param HotkeyTriggered Return the waitable event and it will be signaled
868 when a valid hot key is pressed.
870 @retval EFI_SUCCESS The hot key service is started.
874 EfiBootManagerStartHotkeyService (
875 IN EFI_EVENT
*HotkeyTriggered
879 EFI_BOOT_MANAGER_KEY_OPTION
*KeyOptions
;
880 UINTN KeyOptionCount
;
883 UINT32
*BootOptionSupport
;
885 Status
= GetEfiGlobalVariable2 (EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME
, (VOID
**) &BootOptionSupport
, NULL
);
886 ASSERT (BootOptionSupport
!= NULL
);
888 if ((*BootOptionSupport
& EFI_BOOT_OPTION_SUPPORT_KEY
) != 0) {
889 mBmHotkeySupportCount
= ((*BootOptionSupport
& EFI_BOOT_OPTION_SUPPORT_COUNT
) >> LowBitSet32 (EFI_BOOT_OPTION_SUPPORT_COUNT
));
891 FreePool (BootOptionSupport
);
893 if (mBmHotkeySupportCount
== 0) {
894 DEBUG ((EFI_D_INFO
, "Bds: BootOptionSupport NV variable forbids starting the hotkey service.\n"));
895 return EFI_UNSUPPORTED
;
898 Status
= gBS
->CreateEvent (
905 ASSERT_EFI_ERROR (Status
);
907 if (HotkeyTriggered
!= NULL
) {
908 *HotkeyTriggered
= mBmHotkeyTriggered
;
911 KeyOptions
= BmGetKeyOptions (&KeyOptionCount
);
912 for (Index
= 0; Index
< KeyOptionCount
; Index
++) {
913 BmProcessKeyOption (&KeyOptions
[Index
]);
915 BmFreeKeyOptions (KeyOptions
, KeyOptionCount
);
917 if (mBmContinueKeyOption
!= NULL
) {
918 BmProcessKeyOption (mBmContinueKeyOption
);
922 // Hook hotkey on every future SimpleTextInputEx instance when
923 // SystemTable.ConsoleInHandle == NULL, which means the console
924 // manager (ConSplitter) is absent.
926 if (gST
->ConsoleInHandle
== NULL
) {
927 EfiCreateProtocolNotifyEvent (
928 &gEfiSimpleTextInputExProtocolGuid
,
932 &mBmTxtInExRegistration
936 Status
= EfiCreateEventReadyToBootEx (
942 ASSERT_EFI_ERROR (Status
);
944 mBmHotkeyServiceStarted
= TRUE
;
950 It adds the key option variable and the key option takes affect immediately.
952 @param AddedOption Return the added key option.
953 @param BootOptionNumber The boot option number for the key option.
954 @param Modifier Key shift state.
955 @param ... Parameter list of pointer of EFI_INPUT_KEY.
957 @retval EFI_SUCCESS The key option is added.
958 @retval EFI_ALREADY_STARTED The hot key is already used by certain key option.
962 EfiBootManagerAddKeyOptionVariable (
963 OUT EFI_BOOT_MANAGER_KEY_OPTION
*AddedOption
, OPTIONAL
964 IN UINT16 BootOptionNumber
,
972 UINTN BootOptionSize
;
973 CHAR16 BootOptionName
[BM_OPTION_NAME_LEN
];
974 EFI_BOOT_MANAGER_KEY_OPTION KeyOption
;
975 EFI_BOOT_MANAGER_KEY_OPTION
*KeyOptions
;
976 UINTN KeyOptionCount
;
978 UINTN KeyOptionNumber
;
979 CHAR16 KeyOptionName
[sizeof ("Key####")];
982 BootOptionName
, sizeof (BootOptionName
), L
"%s%04x",
983 mBmLoadOptionName
[LoadOptionTypeBoot
], BootOptionNumber
985 GetEfiGlobalVariable2 (BootOptionName
, &BootOption
, &BootOptionSize
);
987 if (BootOption
== NULL
) {
988 return EFI_NOT_FOUND
;
991 ZeroMem (&KeyOption
, sizeof (EFI_BOOT_MANAGER_KEY_OPTION
));
992 KeyOption
.BootOption
= BootOptionNumber
;
993 Status
= gBS
->CalculateCrc32 (BootOption
, BootOptionSize
, &KeyOption
.BootOptionCrc
);
994 ASSERT_EFI_ERROR (Status
);
995 FreePool (BootOption
);
997 VA_START (Args
, Modifier
);
998 Status
= BmInitializeKeyFields (Modifier
, Args
, &KeyOption
);
1000 if (EFI_ERROR (Status
)) {
1004 KeyOptionNumber
= LoadOptionNumberUnassigned
;
1006 // Check if the hot key sequence was defined already
1008 KeyOptions
= BmGetKeyOptions (&KeyOptionCount
);
1009 for (Index
= 0; Index
< KeyOptionCount
; Index
++) {
1010 if ((KeyOptions
[Index
].KeyData
.PackedValue
== KeyOption
.KeyData
.PackedValue
) &&
1011 (CompareMem (KeyOptions
[Index
].Keys
, KeyOption
.Keys
, KeyOption
.KeyData
.Options
.InputKeyCount
* sizeof (EFI_INPUT_KEY
)) == 0)) {
1015 if ((KeyOptionNumber
== LoadOptionNumberUnassigned
) &&
1016 (KeyOptions
[Index
].OptionNumber
> Index
)
1018 KeyOptionNumber
= Index
;
1021 BmFreeKeyOptions (KeyOptions
, KeyOptionCount
);
1023 if (Index
< KeyOptionCount
) {
1024 return EFI_ALREADY_STARTED
;
1027 if (KeyOptionNumber
== LoadOptionNumberUnassigned
) {
1028 KeyOptionNumber
= KeyOptionCount
;
1031 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", KeyOptionNumber
);
1033 Status
= gRT
->SetVariable (
1035 &gEfiGlobalVariableGuid
,
1036 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1037 BmSizeOfKeyOption (&KeyOption
),
1040 if (!EFI_ERROR (Status
)) {
1042 // Return the Key Option in case needed by caller
1044 if (AddedOption
!= NULL
) {
1045 CopyMem (AddedOption
, &KeyOption
, sizeof (EFI_BOOT_MANAGER_KEY_OPTION
));
1049 // Register the newly added hot key
1050 // Calling this function before EfiBootManagerStartHotkeyService doesn't
1051 // need to call BmProcessKeyOption
1053 if (mBmHotkeyServiceStarted
) {
1054 BmProcessKeyOption (&KeyOption
);
1062 Delete the Key Option variable and unregister the hot key
1064 @param DeletedOption Return the deleted key options.
1065 @param Modifier Key shift state.
1066 @param ... Parameter list of pointer of EFI_INPUT_KEY.
1068 @retval EFI_SUCCESS The key option is deleted.
1069 @retval EFI_NOT_FOUND The key option cannot be found.
1073 EfiBootManagerDeleteKeyOptionVariable (
1074 IN EFI_BOOT_MANAGER_KEY_OPTION
*DeletedOption
, OPTIONAL
1082 EFI_BOOT_MANAGER_KEY_OPTION KeyOption
;
1083 EFI_BOOT_MANAGER_KEY_OPTION
*KeyOptions
;
1084 UINTN KeyOptionCount
;
1089 CHAR16 KeyOptionName
[sizeof ("Key####")];
1091 ZeroMem (&KeyOption
, sizeof (EFI_BOOT_MANAGER_KEY_OPTION
));
1092 VA_START (Args
, Modifier
);
1093 Status
= BmInitializeKeyFields (Modifier
, Args
, &KeyOption
);
1096 if (EFI_ERROR (Status
)) {
1100 EfiAcquireLock (&mBmHotkeyLock
);
1102 // Delete the key option from active hot key list
1103 // Could have multiple entries when modifier isn't 0 because we map the ShiftPressed to RIGHT_SHIFT and RIGHT_SHIFT
1105 for (Link
= GetFirstNode (&mBmHotkeyList
); !IsNull (&mBmHotkeyList
, Link
); ) {
1106 Hotkey
= BM_HOTKEY_FROM_LINK (Link
);
1107 Match
= (BOOLEAN
) (Hotkey
->CodeCount
== KeyOption
.KeyData
.Options
.InputKeyCount
);
1109 for (Index
= 0; Match
&& (Index
< Hotkey
->CodeCount
); Index
++) {
1110 ShiftState
= Hotkey
->KeyData
[Index
].KeyState
.KeyShiftState
;
1112 (BmBitSet (ShiftState
, EFI_RIGHT_SHIFT_PRESSED
| EFI_LEFT_SHIFT_PRESSED
) != KeyOption
.KeyData
.Options
.ShiftPressed
) ||
1113 (BmBitSet (ShiftState
, EFI_RIGHT_CONTROL_PRESSED
| EFI_LEFT_CONTROL_PRESSED
) != KeyOption
.KeyData
.Options
.ControlPressed
) ||
1114 (BmBitSet (ShiftState
, EFI_RIGHT_ALT_PRESSED
| EFI_LEFT_ALT_PRESSED
) != KeyOption
.KeyData
.Options
.AltPressed
) ||
1115 (BmBitSet (ShiftState
, EFI_RIGHT_LOGO_PRESSED
| EFI_LEFT_LOGO_PRESSED
) != KeyOption
.KeyData
.Options
.LogoPressed
) ||
1116 (BmBitSet (ShiftState
, EFI_MENU_KEY_PRESSED
) != KeyOption
.KeyData
.Options
.MenuPressed
) ||
1117 (BmBitSet (ShiftState
, EFI_SYS_REQ_PRESSED
) != KeyOption
.KeyData
.Options
.SysReqPressed
) ||
1118 (CompareMem (&Hotkey
->KeyData
[Index
].Key
, &KeyOption
.Keys
[Index
], sizeof (EFI_INPUT_KEY
)) != 0)
1121 // Break when any field doesn't match
1129 Link
= RemoveEntryList (Link
);
1132 Link
= GetNextNode (&mBmHotkeyList
, Link
);
1137 // Delete the key option from the variable
1139 Status
= EFI_NOT_FOUND
;
1140 KeyOptions
= BmGetKeyOptions (&KeyOptionCount
);
1141 for (Index
= 0; Index
< KeyOptionCount
; Index
++) {
1142 if ((KeyOptions
[Index
].KeyData
.PackedValue
== KeyOption
.KeyData
.PackedValue
) &&
1144 KeyOptions
[Index
].Keys
, KeyOption
.Keys
,
1145 KeyOption
.KeyData
.Options
.InputKeyCount
* sizeof (EFI_INPUT_KEY
)) == 0)
1147 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", KeyOptions
[Index
].OptionNumber
);
1148 Status
= gRT
->SetVariable (
1150 &gEfiGlobalVariableGuid
,
1151 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1156 // Return the deleted key option in case needed by caller
1158 if (DeletedOption
!= NULL
) {
1159 CopyMem (DeletedOption
, &KeyOptions
[Index
], sizeof (EFI_BOOT_MANAGER_KEY_OPTION
));
1164 BmFreeKeyOptions (KeyOptions
, KeyOptionCount
);
1166 EfiReleaseLock (&mBmHotkeyLock
);