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
;
26 Check if the Key Option is valid or not.
28 @param KeyOption The Hot Key Option to be checked.
30 @retval TRUE The Hot Key Option is valid.
31 @retval FALSE The Hot Key Option is invalid.
36 IN EFI_KEY_OPTION
*KeyOption
39 UINT16 BootOptionName
[10];
45 // Check whether corresponding Boot Option exist
47 UnicodeSPrint (BootOptionName
, sizeof (BootOptionName
), L
"Boot%04x", KeyOption
->BootOption
);
48 BootOptionVar
= BdsLibGetVariableAndSize (
50 &gEfiGlobalVariableGuid
,
54 if (BootOptionVar
== NULL
|| BootOptionSize
== 0) {
59 // Check CRC for Boot Option
61 gBS
->CalculateCrc32 (BootOptionVar
, BootOptionSize
, &Crc
);
62 FreePool (BootOptionVar
);
64 return (BOOLEAN
) ((KeyOption
->BootOptionCrc
== Crc
) ? TRUE
: FALSE
);
68 Create Key#### for the given hotkey.
70 @param KeyOption The Hot Key Option to be added.
71 @param KeyOptionNumber The key option number for Key#### (optional).
73 @retval EFI_SUCCESS Register hotkey successfully.
74 @retval EFI_INVALID_PARAMETER The hotkey option is invalid.
75 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory resource.
80 IN EFI_KEY_OPTION
*KeyOption
,
81 OUT UINT16
*KeyOptionNumber
84 UINT16 KeyOptionName
[10];
89 UINT16 MaxOptionNumber
;
90 UINT16 RegisterOptionNumber
;
91 EFI_KEY_OPTION
*TempOption
;
95 BOOLEAN UpdateBootOption
;
98 // Validate the given key option
100 if (!IsKeyOptionValid (KeyOption
)) {
101 return EFI_INVALID_PARAMETER
;
104 KeyOptionSize
= sizeof (EFI_KEY_OPTION
) + GET_KEY_CODE_COUNT (KeyOption
->KeyData
.PackedValue
) * sizeof (EFI_INPUT_KEY
);
105 UpdateBootOption
= FALSE
;
108 // check whether HotKey conflict with keys used by Setup Browser
110 KeyOrder
= BdsLibGetVariableAndSize (
112 &gEfiGlobalVariableGuid
,
115 if (KeyOrder
== NULL
) {
120 // Find free key option number
124 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
125 if (MaxOptionNumber
< KeyOrder
[Index
]) {
126 MaxOptionNumber
= KeyOrder
[Index
];
129 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", KeyOrder
[Index
]);
130 TempOption
= BdsLibGetVariableAndSize (
132 &gEfiGlobalVariableGuid
,
136 if (CompareMem (TempOption
, KeyOption
, TempOptionSize
) == 0) {
138 // Got the option, so just return
140 FreePool (TempOption
);
145 if (KeyOption
->KeyData
.PackedValue
== TempOption
->KeyData
.PackedValue
) {
146 if (GET_KEY_CODE_COUNT (KeyOption
->KeyData
.PackedValue
) == 0 ||
148 ((UINT8
*) TempOption
) + sizeof (EFI_KEY_OPTION
),
149 ((UINT8
*) KeyOption
) + sizeof (EFI_KEY_OPTION
),
150 KeyOptionSize
- sizeof (EFI_KEY_OPTION
)
153 // Hotkey is the same but BootOption changed, need update
155 UpdateBootOption
= TRUE
;
160 FreePool (TempOption
);
163 if (UpdateBootOption
) {
164 RegisterOptionNumber
= KeyOrder
[Index
];
165 FreePool (TempOption
);
167 RegisterOptionNumber
= (UINT16
) (MaxOptionNumber
+ 1);
170 if (KeyOptionNumber
!= NULL
) {
171 *KeyOptionNumber
= RegisterOptionNumber
;
175 // Create variable Key####
177 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", RegisterOptionNumber
);
178 Status
= gRT
->SetVariable (
180 &gEfiGlobalVariableGuid
,
181 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
185 if (EFI_ERROR (Status
)) {
191 // Update the key order variable - "KeyOrder"
193 if (!UpdateBootOption
) {
194 Index
= KeyOrderSize
/ sizeof (UINT16
);
195 KeyOrderSize
+= sizeof (UINT16
);
198 NewKeyOrder
= AllocatePool (KeyOrderSize
);
199 if (NewKeyOrder
== NULL
) {
201 return EFI_OUT_OF_RESOURCES
;
204 if (KeyOrder
!= NULL
) {
205 CopyMem (NewKeyOrder
, KeyOrder
, KeyOrderSize
);
208 NewKeyOrder
[Index
] = RegisterOptionNumber
;
210 Status
= gRT
->SetVariable (
212 &gEfiGlobalVariableGuid
,
213 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
219 FreePool (NewKeyOrder
);
226 Delete Key#### for the given Key Option number.
228 @param KeyOptionNumber Key option number for Key####
230 @retval EFI_SUCCESS Unregister hotkey successfully.
231 @retval EFI_NOT_FOUND No Key#### is found for the given Key Option number.
236 IN UINT16 KeyOptionNumber
239 UINT16 KeyOption
[10];
247 // Delete variable Key####
249 UnicodeSPrint (KeyOption
, sizeof (KeyOption
), L
"Key%04x", KeyOptionNumber
);
252 &gEfiGlobalVariableGuid
,
253 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
259 // Adjust key order array
261 KeyOrder
= BdsLibGetVariableAndSize (
263 &gEfiGlobalVariableGuid
,
266 if (KeyOrder
== NULL
) {
271 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
272 if (KeyOrder
[Index
] == KeyOptionNumber
) {
278 if (Index
!= KeyOrderSize
/ sizeof (UINT16
)) {
280 // KeyOptionNumber found in "KeyOrder", delete it
282 for (Index
= Index2Del
; Index
< KeyOrderSize
/ sizeof (UINT16
) - 1; Index
++) {
283 KeyOrder
[Index
] = KeyOrder
[Index
+ 1];
286 KeyOrderSize
-= sizeof (UINT16
);
289 Status
= gRT
->SetVariable (
291 &gEfiGlobalVariableGuid
,
292 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
304 This is the common notification function for HotKeys, it will be registered
305 with SimpleTextInEx protocol interface - RegisterKeyNotify() of ConIn handle.
307 @param KeyData A pointer to a buffer that is filled in with the keystroke
308 information for the key that was pressed.
310 @retval EFI_SUCCESS KeyData is successfully processed.
315 IN EFI_KEY_DATA
*KeyData
318 BOOLEAN HotkeyCatched
;
319 LIST_ENTRY BootLists
;
321 BDS_HOTKEY_OPTION
*Hotkey
;
323 BDS_COMMON_OPTION
*BootOption
;
327 EFI_KEY_DATA
*HotkeyData
;
329 if (mHotkeyCallbackPending
) {
331 // When responsing to a Hotkey, ignore sequential hotkey stroke until
332 // the current Boot#### load option returned
337 Status
= EFI_SUCCESS
;
338 Link
= GetFirstNode (&mHotkeyList
);
340 while (!IsNull (&mHotkeyList
, Link
)) {
341 HotkeyCatched
= FALSE
;
342 Hotkey
= BDS_HOTKEY_OPTION_FROM_LINK (Link
);
345 // Is this Key Stroke we are waiting for?
347 HotkeyData
= &Hotkey
->KeyData
[Hotkey
->WaitingKey
];
348 if ((KeyData
->Key
.ScanCode
== HotkeyData
->Key
.ScanCode
) &&
349 (KeyData
->Key
.UnicodeChar
== HotkeyData
->Key
.UnicodeChar
) &&
350 ((HotkeyData
->KeyState
.KeyShiftState
& EFI_SHIFT_STATE_VALID
) ? (KeyData
->KeyState
.KeyShiftState
== HotkeyData
->KeyState
.KeyShiftState
) : 1)) {
352 // Receive an expecting key stroke
354 if (Hotkey
->CodeCount
> 1) {
356 // For hotkey of key combination, transit to next waiting state
358 Hotkey
->WaitingKey
++;
360 if (Hotkey
->WaitingKey
== Hotkey
->CodeCount
) {
362 // Received the whole key stroke sequence
364 HotkeyCatched
= TRUE
;
368 // For hotkey of single key stroke
370 HotkeyCatched
= TRUE
;
374 // Receive an unexpected key stroke, reset to initial waiting state
376 Hotkey
->WaitingKey
= 0;
381 // Reset to initial waiting state
383 Hotkey
->WaitingKey
= 0;
386 // Launch its BootOption
388 InitializeListHead (&BootLists
);
390 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", Hotkey
->BootOptionNumber
);
391 BootOption
= BdsLibVariableToOption (&BootLists
, Buffer
);
392 BootOption
->BootCurrent
= Hotkey
->BootOptionNumber
;
393 BdsLibConnectDevicePath (BootOption
->DevicePath
);
396 // Clear the screen before launch this BootOption
398 gST
->ConOut
->Reset (gST
->ConOut
, FALSE
);
400 mHotkeyCallbackPending
= TRUE
;
401 Status
= BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
402 mHotkeyCallbackPending
= FALSE
;
404 if (EFI_ERROR (Status
)) {
406 // Call platform action to indicate the boot fail
408 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_FAILED
));
409 PlatformBdsBootFail (BootOption
, Status
, ExitData
, ExitDataSize
);
412 // Call platform action to indicate the boot success
414 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED
));
415 PlatformBdsBootSuccess (BootOption
);
419 Link
= GetNextNode (&mHotkeyList
, Link
);
426 Register the common HotKey notify function to given SimpleTextInEx protocol instance.
428 @param SimpleTextInEx Simple Text Input Ex protocol instance
430 @retval EFI_SUCCESS Register hotkey notification function successfully.
431 @retval EFI_OUT_OF_RESOURCES Unable to allocate necessary data structures.
435 HotkeyRegisterNotify (
436 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleTextInEx
442 BDS_HOTKEY_OPTION
*Hotkey
;
445 // Register notification function for each hotkey
447 Link
= GetFirstNode (&mHotkeyList
);
449 while (!IsNull (&mHotkeyList
, Link
)) {
450 Hotkey
= BDS_HOTKEY_OPTION_FROM_LINK (Link
);
454 Status
= SimpleTextInEx
->RegisterKeyNotify (
456 &Hotkey
->KeyData
[Index
],
458 &Hotkey
->NotifyHandle
460 if (EFI_ERROR (Status
)) {
462 // some of the hotkey registry failed
467 } while (Index
< Hotkey
->CodeCount
);
469 Link
= GetNextNode (&mHotkeyList
, Link
);
476 Callback function for SimpleTextInEx protocol install events
478 @param Event the event that is signaled.
479 @param Context not used here.
492 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleTextInEx
;
495 BufferSize
= sizeof (EFI_HANDLE
);
496 Status
= gBS
->LocateHandle (
503 if (EFI_ERROR (Status
)) {
505 // If no more notification events exist
510 Status
= gBS
->HandleProtocol (
512 &gEfiSimpleTextInputExProtocolGuid
,
513 (VOID
**) &SimpleTextInEx
515 ASSERT_EFI_ERROR (Status
);
517 HotkeyRegisterNotify (SimpleTextInEx
);
522 Insert Key Option to hotkey list.
524 @param KeyOption The Hot Key Option to be added to hotkey list.
526 @retval EFI_SUCCESS Add to hotkey list success.
527 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory resource.
531 IN EFI_KEY_OPTION
*KeyOption
534 BDS_HOTKEY_OPTION
*HotkeyLeft
;
535 BDS_HOTKEY_OPTION
*HotkeyRight
;
538 UINT32 KeyShiftStateLeft
;
539 UINT32 KeyShiftStateRight
;
540 EFI_INPUT_KEY
*InputKey
;
541 EFI_KEY_DATA
*KeyData
;
543 HotkeyLeft
= AllocateZeroPool (sizeof (BDS_HOTKEY_OPTION
));
544 if (HotkeyLeft
== NULL
) {
545 return EFI_OUT_OF_RESOURCES
;
548 HotkeyLeft
->Signature
= BDS_HOTKEY_OPTION_SIGNATURE
;
549 HotkeyLeft
->BootOptionNumber
= KeyOption
->BootOption
;
551 KeyOptions
= KeyOption
->KeyData
.PackedValue
;
553 HotkeyLeft
->CodeCount
= (UINT8
) GET_KEY_CODE_COUNT (KeyOptions
);
556 // Map key shift state from KeyOptions to EFI_KEY_DATA.KeyState
558 KeyShiftStateRight
= (KeyOptions
& EFI_KEY_OPTION_SHIFT
) |
559 ((KeyOptions
& EFI_KEY_OPTION_CONTROL
) << 1) |
560 ((KeyOptions
& EFI_KEY_OPTION_ALT
) << 2) |
561 ((KeyOptions
& EFI_KEY_OPTION_LOGO
) << 3) |
562 ((KeyOptions
& (EFI_KEY_OPTION_MENU
| EFI_KEY_OPTION_SYSREQ
)) << 4) |
563 EFI_SHIFT_STATE_VALID
;
565 KeyShiftStateLeft
= (KeyShiftStateRight
& 0xffffff00) | ((KeyShiftStateRight
& 0xff) << 1);
567 InputKey
= (EFI_INPUT_KEY
*) (((UINT8
*) KeyOption
) + sizeof (EFI_KEY_OPTION
));
570 KeyData
= &HotkeyLeft
->KeyData
[0];
573 // If Key CodeCount is 0, then only KeyData[0] is used;
574 // if Key CodeCount is n, then KeyData[0]~KeyData[n-1] are used
576 KeyData
->Key
.ScanCode
= InputKey
[Index
].ScanCode
;
577 KeyData
->Key
.UnicodeChar
= InputKey
[Index
].UnicodeChar
;
578 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateLeft
;
582 } while (Index
< HotkeyLeft
->CodeCount
);
583 InsertTailList (&mHotkeyList
, &HotkeyLeft
->Link
);
585 if (KeyShiftStateLeft
!= KeyShiftStateRight
) {
587 // Need an extra hotkey for shift key on right
589 HotkeyRight
= AllocateCopyPool (sizeof (BDS_HOTKEY_OPTION
), HotkeyLeft
);
590 if (HotkeyRight
== NULL
) {
591 return EFI_OUT_OF_RESOURCES
;
595 KeyData
= &HotkeyRight
->KeyData
[0];
598 // Key.ScanCode and Key.UnicodeChar have already been initialized,
599 // only need to update KeyState.KeyShiftState
601 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateRight
;
605 } while (Index
< HotkeyRight
->CodeCount
);
606 InsertTailList (&mHotkeyList
, &HotkeyRight
->Link
);
614 Process all the "Key####" variables, associate Hotkeys with corresponding Boot Options.
616 @retval EFI_SUCCESS Hotkey services successfully initialized.
617 @retval EFI_NOT_FOUND Can not find the "KeyOrder" variable
620 InitializeHotkeyService (
625 UINT32 BootOptionSupport
;
629 UINT16 KeyOptionName
[8];
631 EFI_KEY_OPTION
*KeyOption
;
634 // Export our capability - EFI_BOOT_OPTION_SUPPORT_KEY and EFI_BOOT_OPTION_SUPPORT_APP
636 BootOptionSupport
= EFI_BOOT_OPTION_SUPPORT_KEY
| EFI_BOOT_OPTION_SUPPORT_APP
;
637 Status
= gRT
->SetVariable (
638 L
"BootOptionSupport",
639 &gEfiGlobalVariableGuid
,
640 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
646 // Get valid Key Option List from private EFI variable "KeyOrder"
648 KeyOrder
= BdsLibGetVariableAndSize (
650 &gEfiGlobalVariableGuid
,
654 if (KeyOrder
== NULL
) {
655 return EFI_NOT_FOUND
;
658 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
659 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", KeyOrder
[Index
]);
660 KeyOption
= BdsLibGetVariableAndSize (
662 &gEfiGlobalVariableGuid
,
666 if (KeyOption
== NULL
|| !IsKeyOptionValid (KeyOption
)) {
667 UnregisterHotkey (KeyOrder
[Index
]);
669 HotkeyInsertList (KeyOption
);
674 // Register Protocol notify for Hotkey service
676 Status
= gBS
->CreateEvent (
683 ASSERT_EFI_ERROR (Status
);
686 // Register for protocol notifications on this event
688 Status
= gBS
->RegisterProtocolNotify (
689 &gEfiSimpleTextInputExProtocolGuid
,
693 ASSERT_EFI_ERROR (Status
);