2 Provides a way for 3rd party applications to register themselves for launch by the
3 Boot Manager based on hot key
5 Copyright (c) 2007 - 2008, Intel Corporation. <BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 LIST_ENTRY mHotkeyList
= INITIALIZE_LIST_HEAD_VARIABLE (mHotkeyList
);
20 BOOLEAN mHotkeyCallbackPending
= FALSE
;
21 EFI_EVENT mHotkeyEvent
;
22 VOID
*mHotkeyRegistration
;
27 Check if the Key Option is valid or not.
30 @param KeyOption The Hot Key Option to be checked.
32 @retval TRUE The Hot Key Option is valid.
33 @retval FALSE The Hot Key Option is invalid.
38 IN EFI_KEY_OPTION
*KeyOption
41 UINT16 BootOptionName
[10];
47 // Check whether corresponding Boot Option exist
49 UnicodeSPrint (BootOptionName
, sizeof (BootOptionName
), L
"Boot%04x", KeyOption
->BootOption
);
50 BootOptionVar
= BdsLibGetVariableAndSize (
52 &gEfiGlobalVariableGuid
,
56 if (BootOptionVar
== NULL
|| BootOptionSize
== 0) {
61 // Check CRC for Boot Option
63 gBS
->CalculateCrc32 (BootOptionVar
, BootOptionSize
, &Crc
);
64 FreePool (BootOptionVar
);
66 return (BOOLEAN
) ((KeyOption
->BootOptionCrc
== Crc
) ? TRUE
: FALSE
);
71 Create Key#### for the given hotkey.
74 @param KeyOption The Hot Key Option to be added.
75 @param KeyOptionNumber The key option number for Key#### (optional).
77 @retval EFI_SUCCESS Register hotkey successfully.
78 @retval EFI_INVALID_PARAMETER The hotkey option is invalid.
83 IN EFI_KEY_OPTION
*KeyOption
,
84 OUT UINT16
*KeyOptionNumber
87 UINT16 KeyOptionName
[10];
92 UINT16 MaxOptionNumber
;
93 UINT16 RegisterOptionNumber
;
94 EFI_KEY_OPTION
*TempOption
;
98 BOOLEAN UpdateBootOption
;
101 // Validate the given key option
103 if (!IsKeyOptionValid (KeyOption
)) {
104 return EFI_INVALID_PARAMETER
;
107 KeyOptionSize
= sizeof (EFI_KEY_OPTION
) + GET_KEY_CODE_COUNT (KeyOption
->KeyOptions
.PackedValue
) * sizeof (EFI_INPUT_KEY
);
108 UpdateBootOption
= FALSE
;
111 // check whether HotKey conflict with keys used by Setup Browser
114 KeyOrder
= BdsLibGetVariableAndSize (
116 &gEfiGlobalVariableGuid
,
119 if (KeyOrder
== NULL
) {
124 // Find free key option number
128 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
129 if (MaxOptionNumber
< KeyOrder
[Index
]) {
130 MaxOptionNumber
= KeyOrder
[Index
];
133 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", KeyOrder
[Index
]);
134 TempOption
= BdsLibGetVariableAndSize (
136 &gEfiGlobalVariableGuid
,
140 if (CompareMem (TempOption
, KeyOption
, TempOptionSize
) == 0) {
142 // Got the option, so just return
144 FreePool (TempOption
);
149 if (KeyOption
->KeyOptions
.PackedValue
== TempOption
->KeyOptions
.PackedValue
) {
150 if (GET_KEY_CODE_COUNT (KeyOption
->KeyOptions
.PackedValue
) == 0 ||
152 ((UINT8
*) TempOption
) + sizeof (EFI_KEY_OPTION
),
153 ((UINT8
*) KeyOption
) + sizeof (EFI_KEY_OPTION
),
154 KeyOptionSize
- sizeof (EFI_KEY_OPTION
)
157 // Hotkey is the same but BootOption changed, need update
159 UpdateBootOption
= TRUE
;
164 FreePool (TempOption
);
167 if (UpdateBootOption
) {
168 RegisterOptionNumber
= KeyOrder
[Index
];
169 FreePool (TempOption
);
171 RegisterOptionNumber
= (UINT16
) (MaxOptionNumber
+ 1);
174 if (KeyOptionNumber
!= NULL
) {
175 *KeyOptionNumber
= RegisterOptionNumber
;
179 // Create variable Key####
181 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", RegisterOptionNumber
);
182 Status
= gRT
->SetVariable (
184 &gEfiGlobalVariableGuid
,
185 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
189 if (EFI_ERROR (Status
)) {
190 gBS
->FreePool (KeyOrder
);
195 // Update the key order variable - "KeyOrder"
197 if (!UpdateBootOption
) {
198 Index
= KeyOrderSize
/ sizeof (UINT16
);
199 KeyOrderSize
+= sizeof (UINT16
);
202 NewKeyOrder
= AllocatePool (KeyOrderSize
);
203 if (NewKeyOrder
== NULL
) {
204 return EFI_OUT_OF_RESOURCES
;
207 if (KeyOrder
!= NULL
) {
208 CopyMem (NewKeyOrder
, KeyOrder
, KeyOrderSize
);
211 NewKeyOrder
[Index
] = RegisterOptionNumber
;
213 Status
= gRT
->SetVariable (
215 &gEfiGlobalVariableGuid
,
216 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
222 FreePool (NewKeyOrder
);
229 Delete Key#### for the given Key Option number.
232 @param KeyOptionNumber Key option number for Key####
234 @retval EFI_SUCCESS Unregister hotkey successfully.
235 @retval EFI_NOT_FOUND No Key#### is found for the given Key Option number.
240 IN UINT16 KeyOptionNumber
243 UINT16 KeyOption
[10];
251 // Delete variable Key####
253 UnicodeSPrint (KeyOption
, sizeof (KeyOption
), L
"Key%04x", KeyOptionNumber
);
256 &gEfiGlobalVariableGuid
,
257 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
263 // Adjust key order array
265 KeyOrder
= BdsLibGetVariableAndSize (
267 &gEfiGlobalVariableGuid
,
270 if (KeyOrder
== NULL
) {
275 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
276 if (KeyOrder
[Index
] == KeyOptionNumber
) {
282 if (Index
!= KeyOrderSize
/ sizeof (UINT16
)) {
284 // KeyOptionNumber found in "KeyOrder", delete it
286 for (Index
= Index2Del
; Index
< KeyOrderSize
/ sizeof (UINT16
) - 1; Index
++) {
287 KeyOrder
[Index
] = KeyOrder
[Index
+ 1];
290 KeyOrderSize
-= sizeof (UINT16
);
293 Status
= gRT
->SetVariable (
295 &gEfiGlobalVariableGuid
,
296 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
308 This is the common notification function for HotKeys, it will be registered
309 with SimpleTextInEx protocol interface - RegisterKeyNotify() of ConIn handle.
312 @param KeyData A pointer to a buffer that is filled in with the keystroke
313 information for the key that was pressed.
315 @retval EFI_SUCCESS KeyData is successfully processed.
320 IN EFI_KEY_DATA
*KeyData
323 BOOLEAN HotkeyCatched
;
324 LIST_ENTRY BootLists
;
326 BDS_HOTKEY_OPTION
*Hotkey
;
328 BDS_COMMON_OPTION
*BootOption
;
333 EFI_KEY_DATA
*HotkeyData
;
335 if (mHotkeyCallbackPending
) {
337 // When responsing to a Hotkey, ignore sequential hotkey stroke until
338 // the current Boot#### load option returned
343 Status
= EFI_SUCCESS
;
344 Link
= GetFirstNode (&mHotkeyList
);
346 while (!IsNull (&mHotkeyList
, Link
)) {
347 HotkeyCatched
= FALSE
;
348 Hotkey
= BDS_HOTKEY_OPTION_FROM_LINK (Link
);
351 // Is this Key Stroke we are waiting for?
353 HotkeyData
= &Hotkey
->KeyData
[Hotkey
->WaitingKey
];
354 if ((KeyData
->Key
.ScanCode
== HotkeyData
->Key
.ScanCode
) &&
355 (KeyData
->Key
.UnicodeChar
== HotkeyData
->Key
.UnicodeChar
) &&
356 ((HotkeyData
->KeyState
.KeyShiftState
& EFI_SHIFT_STATE_VALID
) ? (KeyData
->KeyState
.KeyShiftState
== HotkeyData
->KeyState
.KeyShiftState
) : 1)) {
358 // Receive an expecting key stroke
360 if (Hotkey
->CodeCount
> 1) {
362 // For hotkey of key combination, transit to next waiting state
364 Hotkey
->WaitingKey
++;
366 if (Hotkey
->WaitingKey
== Hotkey
->CodeCount
) {
368 // Received the whole key stroke sequence
370 HotkeyCatched
= TRUE
;
374 // For hotkey of single key stroke
376 HotkeyCatched
= TRUE
;
380 // Receive an unexpected key stroke, reset to initial waiting state
382 Hotkey
->WaitingKey
= 0;
387 // Reset to initial waiting state
389 Hotkey
->WaitingKey
= 0;
392 // Launch its BootOption
394 InitializeListHead (&BootLists
);
396 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", Hotkey
->BootOptionNumber
);
397 BootOption
= BdsLibVariableToOption (&BootLists
, Buffer
);
398 BootOption
->BootCurrent
= Hotkey
->BootOptionNumber
;
399 BdsLibConnectDevicePath (BootOption
->DevicePath
);
402 // Clear the screen before launch this BootOption
404 gST
->ConOut
->Reset (gST
->ConOut
, FALSE
);
407 // BdsLibBootViaBootOption() is expected to be invoked at TPL level TPL_APPLICATION,
408 // so raise the TPL to TPL_APPLICATION first, then restore it
410 OldTpl
= gBS
->RaiseTPL (TPL_APPLICATION
);
412 mHotkeyCallbackPending
= TRUE
;
413 Status
= BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
414 mHotkeyCallbackPending
= FALSE
;
416 gBS
->RestoreTPL (OldTpl
);
418 if (EFI_ERROR (Status
)) {
420 // Call platform action to indicate the boot fail
422 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_FAILED
));
423 PlatformBdsBootFail (BootOption
, Status
, ExitData
, ExitDataSize
);
426 // Call platform action to indicate the boot success
428 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED
));
429 PlatformBdsBootSuccess (BootOption
);
433 Link
= GetNextNode (&mHotkeyList
, Link
);
441 Register the common HotKey notify function to given SimpleTextInEx protocol instance.
444 @param SimpleTextInEx Simple Text Input Ex protocol instance
446 @retval EFI_SUCCESS Register hotkey notification function successfully.
447 @retval EFI_OUT_OF_RESOURCES Unable to allocate necessary data structures.
451 HotkeyRegisterNotify (
452 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleTextInEx
458 BDS_HOTKEY_OPTION
*Hotkey
;
461 // Register notification function for each hotkey
463 Link
= GetFirstNode (&mHotkeyList
);
465 while (!IsNull (&mHotkeyList
, Link
)) {
466 Hotkey
= BDS_HOTKEY_OPTION_FROM_LINK (Link
);
470 Status
= SimpleTextInEx
->RegisterKeyNotify (
472 &Hotkey
->KeyData
[Index
],
474 &Hotkey
->NotifyHandle
476 if (EFI_ERROR (Status
)) {
478 // some of the hotkey registry failed
483 } while (Index
< Hotkey
->CodeCount
);
485 Link
= GetNextNode (&mHotkeyList
, Link
);
492 Callback function for SimpleTextInEx protocol install events
495 @param Event the event that is signaled.
496 @param Context not used here.
511 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleTextInEx
;
514 BufferSize
= sizeof (EFI_HANDLE
);
515 Status
= gBS
->LocateHandle (
522 if (EFI_ERROR (Status
)) {
524 // If no more notification events exist
529 Status
= gBS
->HandleProtocol (
531 &gEfiSimpleTextInputExProtocolGuid
,
532 (VOID
**) &SimpleTextInEx
534 ASSERT_EFI_ERROR (Status
);
536 HotkeyRegisterNotify (SimpleTextInEx
);
542 Insert Key Option to hotkey list.
545 @param KeyOption The Hot Key Option to be added to hotkey list.
547 @retval EFI_SUCCESS Add to hotkey list success.
552 IN EFI_KEY_OPTION
*KeyOption
555 BDS_HOTKEY_OPTION
*HotkeyLeft
;
556 BDS_HOTKEY_OPTION
*HotkeyRight
;
559 UINT32 KeyShiftStateLeft
;
560 UINT32 KeyShiftStateRight
;
561 EFI_INPUT_KEY
*InputKey
;
562 EFI_KEY_DATA
*KeyData
;
564 HotkeyLeft
= AllocateZeroPool (sizeof (BDS_HOTKEY_OPTION
));
565 if (HotkeyLeft
== NULL
) {
566 return EFI_OUT_OF_RESOURCES
;
569 HotkeyLeft
->Signature
= BDS_HOTKEY_OPTION_SIGNATURE
;
570 HotkeyLeft
->BootOptionNumber
= KeyOption
->BootOption
;
572 KeyOptions
= KeyOption
->KeyOptions
.PackedValue
;
574 HotkeyLeft
->CodeCount
= (UINT8
) GET_KEY_CODE_COUNT (KeyOptions
);
577 // Map key shift state from KeyOptions to EFI_KEY_DATA.KeyState
579 KeyShiftStateRight
= (KeyOptions
& EFI_KEY_OPTION_SHIFT
) |
580 ((KeyOptions
& EFI_KEY_OPTION_CONTROL
) << 1) |
581 ((KeyOptions
& EFI_KEY_OPTION_ALT
) << 2) |
582 ((KeyOptions
& EFI_KEY_OPTION_LOGO
) << 3) |
583 ((KeyOptions
& (EFI_KEY_OPTION_MENU
| EFI_KEY_OPTION_SYSREQ
)) << 4) |
584 EFI_SHIFT_STATE_VALID
;
586 KeyShiftStateLeft
= (KeyShiftStateRight
& 0xffffff00) | ((KeyShiftStateRight
& 0xff) << 1);
588 InputKey
= (EFI_INPUT_KEY
*) (((UINT8
*) KeyOption
) + sizeof (EFI_KEY_OPTION
));
591 KeyData
= &HotkeyLeft
->KeyData
[0];
594 // If Key CodeCount is 0, then only KeyData[0] is used;
595 // if Key CodeCount is n, then KeyData[0]~KeyData[n-1] are used
597 KeyData
->Key
.ScanCode
= InputKey
[Index
].ScanCode
;
598 KeyData
->Key
.UnicodeChar
= InputKey
[Index
].UnicodeChar
;
599 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateLeft
;
603 } while (Index
< HotkeyLeft
->CodeCount
);
604 InsertTailList (&mHotkeyList
, &HotkeyLeft
->Link
);
606 if (KeyShiftStateLeft
!= KeyShiftStateRight
) {
608 // Need an extra hotkey for shift key on right
610 HotkeyRight
= AllocateCopyPool (sizeof (BDS_HOTKEY_OPTION
), HotkeyLeft
);
611 if (HotkeyRight
== NULL
) {
612 return EFI_OUT_OF_RESOURCES
;
616 KeyData
= &HotkeyRight
->KeyData
[0];
619 // Key.ScanCode and Key.UnicodeChar have already been initialized,
620 // only need to update KeyState.KeyShiftState
622 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateRight
;
626 } while (Index
< HotkeyRight
->CodeCount
);
627 InsertTailList (&mHotkeyList
, &HotkeyRight
->Link
);
635 Process all the "Key####" variables, associate Hotkeys with corresponding Boot Options.
640 @retval EFI_SUCCESS Hotkey services successfully initialized.
644 InitializeHotkeyService (
649 UINT32 BootOptionSupport
;
653 UINT16 KeyOptionName
[8];
655 EFI_KEY_OPTION
*KeyOption
;
658 // Export our capability - EFI_BOOT_OPTION_SUPPORT_KEY and EFI_BOOT_OPTION_SUPPORT_APP
660 BootOptionSupport
= EFI_BOOT_OPTION_SUPPORT_KEY
| EFI_BOOT_OPTION_SUPPORT_APP
;
661 Status
= gRT
->SetVariable (
662 L
"BootOptionSupport",
663 &gEfiGlobalVariableGuid
,
664 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
670 // Get valid Key Option List from private EFI variable "KeyOrder"
672 KeyOrder
= BdsLibGetVariableAndSize (
674 &gEfiGlobalVariableGuid
,
678 if (KeyOrder
== NULL
) {
679 return EFI_NOT_FOUND
;
682 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
683 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", KeyOrder
[Index
]);
684 KeyOption
= BdsLibGetVariableAndSize (
686 &gEfiGlobalVariableGuid
,
690 if (KeyOption
== NULL
|| !IsKeyOptionValid (KeyOption
)) {
691 UnregisterHotkey (KeyOrder
[Index
]);
693 HotkeyInsertList (KeyOption
);
698 // Register Protocol notify for Hotkey service
700 Status
= gBS
->CreateEvent (
707 ASSERT_EFI_ERROR (Status
);
710 // Register for protocol notifications on this event
712 Status
= gBS
->RegisterProtocolNotify (
713 &gEfiSimpleTextInputExProtocolGuid
,
717 ASSERT_EFI_ERROR (Status
);