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.
231 @param KeyOptionNumber Key option number for Key####
233 @retval EFI_SUCCESS Unregister hotkey successfully.
234 @retval EFI_NOT_FOUND No Key#### is found for the given Key Option number.
239 IN UINT16 KeyOptionNumber
242 UINT16 KeyOption
[10];
250 // Delete variable Key####
252 UnicodeSPrint (KeyOption
, sizeof (KeyOption
), L
"Key%04x", KeyOptionNumber
);
255 &gEfiGlobalVariableGuid
,
256 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
262 // Adjust key order array
264 KeyOrder
= BdsLibGetVariableAndSize (
266 &gEfiGlobalVariableGuid
,
269 if (KeyOrder
== NULL
) {
274 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
275 if (KeyOrder
[Index
] == KeyOptionNumber
) {
281 if (Index
!= KeyOrderSize
/ sizeof (UINT16
)) {
283 // KeyOptionNumber found in "KeyOrder", delete it
285 for (Index
= Index2Del
; Index
< KeyOrderSize
/ sizeof (UINT16
) - 1; Index
++) {
286 KeyOrder
[Index
] = KeyOrder
[Index
+ 1];
289 KeyOrderSize
-= sizeof (UINT16
);
292 Status
= gRT
->SetVariable (
294 &gEfiGlobalVariableGuid
,
295 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
307 This is the common notification function for HotKeys, it will be registered
308 with SimpleTextInEx protocol interface - RegisterKeyNotify() of ConIn handle.
311 @param KeyData A pointer to a buffer that is filled in with the keystroke
312 information for the key that was pressed.
314 @retval EFI_SUCCESS KeyData is successfully processed.
319 IN EFI_KEY_DATA
*KeyData
322 BOOLEAN HotkeyCatched
;
323 LIST_ENTRY BootLists
;
325 BDS_HOTKEY_OPTION
*Hotkey
;
327 BDS_COMMON_OPTION
*BootOption
;
331 EFI_KEY_DATA
*HotkeyData
;
333 if (mHotkeyCallbackPending
) {
335 // When responsing to a Hotkey, ignore sequential hotkey stroke until
336 // the current Boot#### load option returned
341 Status
= EFI_SUCCESS
;
342 Link
= GetFirstNode (&mHotkeyList
);
344 while (!IsNull (&mHotkeyList
, Link
)) {
345 HotkeyCatched
= FALSE
;
346 Hotkey
= BDS_HOTKEY_OPTION_FROM_LINK (Link
);
349 // Is this Key Stroke we are waiting for?
351 HotkeyData
= &Hotkey
->KeyData
[Hotkey
->WaitingKey
];
352 if ((KeyData
->Key
.ScanCode
== HotkeyData
->Key
.ScanCode
) &&
353 (KeyData
->Key
.UnicodeChar
== HotkeyData
->Key
.UnicodeChar
) &&
354 ((HotkeyData
->KeyState
.KeyShiftState
& EFI_SHIFT_STATE_VALID
) ? (KeyData
->KeyState
.KeyShiftState
== HotkeyData
->KeyState
.KeyShiftState
) : 1)) {
356 // Receive an expecting key stroke
358 if (Hotkey
->CodeCount
> 1) {
360 // For hotkey of key combination, transit to next waiting state
362 Hotkey
->WaitingKey
++;
364 if (Hotkey
->WaitingKey
== Hotkey
->CodeCount
) {
366 // Received the whole key stroke sequence
368 HotkeyCatched
= TRUE
;
372 // For hotkey of single key stroke
374 HotkeyCatched
= TRUE
;
378 // Receive an unexpected key stroke, reset to initial waiting state
380 Hotkey
->WaitingKey
= 0;
385 // Reset to initial waiting state
387 Hotkey
->WaitingKey
= 0;
390 // Launch its BootOption
392 InitializeListHead (&BootLists
);
394 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", Hotkey
->BootOptionNumber
);
395 BootOption
= BdsLibVariableToOption (&BootLists
, Buffer
);
396 BootOption
->BootCurrent
= Hotkey
->BootOptionNumber
;
397 BdsLibConnectDevicePath (BootOption
->DevicePath
);
400 // Clear the screen before launch this BootOption
402 gST
->ConOut
->Reset (gST
->ConOut
, FALSE
);
404 mHotkeyCallbackPending
= TRUE
;
405 Status
= BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
406 mHotkeyCallbackPending
= FALSE
;
408 if (EFI_ERROR (Status
)) {
410 // Call platform action to indicate the boot fail
412 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_FAILED
));
413 PlatformBdsBootFail (BootOption
, Status
, ExitData
, ExitDataSize
);
416 // Call platform action to indicate the boot success
418 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED
));
419 PlatformBdsBootSuccess (BootOption
);
423 Link
= GetNextNode (&mHotkeyList
, Link
);
431 Register the common HotKey notify function to given SimpleTextInEx protocol instance.
434 @param SimpleTextInEx Simple Text Input Ex protocol instance
436 @retval EFI_SUCCESS Register hotkey notification function successfully.
437 @retval EFI_OUT_OF_RESOURCES Unable to allocate necessary data structures.
441 HotkeyRegisterNotify (
442 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleTextInEx
448 BDS_HOTKEY_OPTION
*Hotkey
;
451 // Register notification function for each hotkey
453 Link
= GetFirstNode (&mHotkeyList
);
455 while (!IsNull (&mHotkeyList
, Link
)) {
456 Hotkey
= BDS_HOTKEY_OPTION_FROM_LINK (Link
);
460 Status
= SimpleTextInEx
->RegisterKeyNotify (
462 &Hotkey
->KeyData
[Index
],
464 &Hotkey
->NotifyHandle
466 if (EFI_ERROR (Status
)) {
468 // some of the hotkey registry failed
473 } while (Index
< Hotkey
->CodeCount
);
475 Link
= GetNextNode (&mHotkeyList
, Link
);
482 Callback function for SimpleTextInEx protocol install events
485 @param Event the event that is signaled.
486 @param Context not used here.
501 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleTextInEx
;
504 BufferSize
= sizeof (EFI_HANDLE
);
505 Status
= gBS
->LocateHandle (
512 if (EFI_ERROR (Status
)) {
514 // If no more notification events exist
519 Status
= gBS
->HandleProtocol (
521 &gEfiSimpleTextInputExProtocolGuid
,
522 (VOID
**) &SimpleTextInEx
524 ASSERT_EFI_ERROR (Status
);
526 HotkeyRegisterNotify (SimpleTextInEx
);
532 Insert Key Option to hotkey list.
535 @param KeyOption The Hot Key Option to be added to hotkey list.
537 @retval EFI_SUCCESS Add to hotkey list success.
542 IN EFI_KEY_OPTION
*KeyOption
545 BDS_HOTKEY_OPTION
*HotkeyLeft
;
546 BDS_HOTKEY_OPTION
*HotkeyRight
;
549 UINT32 KeyShiftStateLeft
;
550 UINT32 KeyShiftStateRight
;
551 EFI_INPUT_KEY
*InputKey
;
552 EFI_KEY_DATA
*KeyData
;
554 HotkeyLeft
= AllocateZeroPool (sizeof (BDS_HOTKEY_OPTION
));
555 if (HotkeyLeft
== NULL
) {
556 return EFI_OUT_OF_RESOURCES
;
559 HotkeyLeft
->Signature
= BDS_HOTKEY_OPTION_SIGNATURE
;
560 HotkeyLeft
->BootOptionNumber
= KeyOption
->BootOption
;
562 KeyOptions
= KeyOption
->KeyOptions
.PackedValue
;
564 HotkeyLeft
->CodeCount
= (UINT8
) GET_KEY_CODE_COUNT (KeyOptions
);
567 // Map key shift state from KeyOptions to EFI_KEY_DATA.KeyState
569 KeyShiftStateRight
= (KeyOptions
& EFI_KEY_OPTION_SHIFT
) |
570 ((KeyOptions
& EFI_KEY_OPTION_CONTROL
) << 1) |
571 ((KeyOptions
& EFI_KEY_OPTION_ALT
) << 2) |
572 ((KeyOptions
& EFI_KEY_OPTION_LOGO
) << 3) |
573 ((KeyOptions
& (EFI_KEY_OPTION_MENU
| EFI_KEY_OPTION_SYSREQ
)) << 4) |
574 EFI_SHIFT_STATE_VALID
;
576 KeyShiftStateLeft
= (KeyShiftStateRight
& 0xffffff00) | ((KeyShiftStateRight
& 0xff) << 1);
578 InputKey
= (EFI_INPUT_KEY
*) (((UINT8
*) KeyOption
) + sizeof (EFI_KEY_OPTION
));
581 KeyData
= &HotkeyLeft
->KeyData
[0];
584 // If Key CodeCount is 0, then only KeyData[0] is used;
585 // if Key CodeCount is n, then KeyData[0]~KeyData[n-1] are used
587 KeyData
->Key
.ScanCode
= InputKey
[Index
].ScanCode
;
588 KeyData
->Key
.UnicodeChar
= InputKey
[Index
].UnicodeChar
;
589 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateLeft
;
593 } while (Index
< HotkeyLeft
->CodeCount
);
594 InsertTailList (&mHotkeyList
, &HotkeyLeft
->Link
);
596 if (KeyShiftStateLeft
!= KeyShiftStateRight
) {
598 // Need an extra hotkey for shift key on right
600 HotkeyRight
= AllocateCopyPool (sizeof (BDS_HOTKEY_OPTION
), HotkeyLeft
);
601 if (HotkeyRight
== NULL
) {
602 return EFI_OUT_OF_RESOURCES
;
606 KeyData
= &HotkeyRight
->KeyData
[0];
609 // Key.ScanCode and Key.UnicodeChar have already been initialized,
610 // only need to update KeyState.KeyShiftState
612 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateRight
;
616 } while (Index
< HotkeyRight
->CodeCount
);
617 InsertTailList (&mHotkeyList
, &HotkeyRight
->Link
);
625 Process all the "Key####" variables, associate Hotkeys with corresponding Boot Options.
627 @retval EFI_SUCCESS Hotkey services successfully initialized.
628 @retval EFI_NOT_FOUND Can not find the "KeyOrder" variable
631 InitializeHotkeyService (
636 UINT32 BootOptionSupport
;
640 UINT16 KeyOptionName
[8];
642 EFI_KEY_OPTION
*KeyOption
;
645 // Export our capability - EFI_BOOT_OPTION_SUPPORT_KEY and EFI_BOOT_OPTION_SUPPORT_APP
647 BootOptionSupport
= EFI_BOOT_OPTION_SUPPORT_KEY
| EFI_BOOT_OPTION_SUPPORT_APP
;
648 Status
= gRT
->SetVariable (
649 L
"BootOptionSupport",
650 &gEfiGlobalVariableGuid
,
651 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
657 // Get valid Key Option List from private EFI variable "KeyOrder"
659 KeyOrder
= BdsLibGetVariableAndSize (
661 &gEfiGlobalVariableGuid
,
665 if (KeyOrder
== NULL
) {
666 return EFI_NOT_FOUND
;
669 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
670 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", KeyOrder
[Index
]);
671 KeyOption
= BdsLibGetVariableAndSize (
673 &gEfiGlobalVariableGuid
,
677 if (KeyOption
== NULL
|| !IsKeyOptionValid (KeyOption
)) {
678 UnregisterHotkey (KeyOrder
[Index
]);
680 HotkeyInsertList (KeyOption
);
685 // Register Protocol notify for Hotkey service
687 Status
= gBS
->CreateEvent (
694 ASSERT_EFI_ERROR (Status
);
697 // Register for protocol notifications on this event
699 Status
= gBS
->RegisterProtocolNotify (
700 &gEfiSimpleTextInputExProtocolGuid
,
704 ASSERT_EFI_ERROR (Status
);