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 - 2010, Intel Corporation. All rights reserved.<BR>
6 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
) + KeyOption
->KeyData
.Options
.InputKeyCount
* 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
,
135 ASSERT (TempOption
!= NULL
);
137 if (CompareMem (TempOption
, KeyOption
, TempOptionSize
) == 0) {
139 // Got the option, so just return
141 FreePool (TempOption
);
146 if (KeyOption
->KeyData
.PackedValue
== TempOption
->KeyData
.PackedValue
) {
147 if (KeyOption
->KeyData
.Options
.InputKeyCount
== 0 ||
149 ((UINT8
*) TempOption
) + sizeof (EFI_KEY_OPTION
),
150 ((UINT8
*) KeyOption
) + sizeof (EFI_KEY_OPTION
),
151 KeyOptionSize
- sizeof (EFI_KEY_OPTION
)
154 // Hotkey is the same but BootOption changed, need update
156 UpdateBootOption
= TRUE
;
161 FreePool (TempOption
);
164 if (UpdateBootOption
) {
165 RegisterOptionNumber
= KeyOrder
[Index
];
166 FreePool (TempOption
);
168 RegisterOptionNumber
= (UINT16
) (MaxOptionNumber
+ 1);
171 if (KeyOptionNumber
!= NULL
) {
172 *KeyOptionNumber
= RegisterOptionNumber
;
176 // Create variable Key####
178 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", RegisterOptionNumber
);
179 Status
= gRT
->SetVariable (
181 &gEfiGlobalVariableGuid
,
182 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
186 if (EFI_ERROR (Status
)) {
192 // Update the key order variable - "KeyOrder"
194 if (!UpdateBootOption
) {
195 Index
= KeyOrderSize
/ sizeof (UINT16
);
196 KeyOrderSize
+= sizeof (UINT16
);
199 NewKeyOrder
= AllocatePool (KeyOrderSize
);
200 if (NewKeyOrder
== NULL
) {
202 return EFI_OUT_OF_RESOURCES
;
205 if (KeyOrder
!= NULL
) {
206 CopyMem (NewKeyOrder
, KeyOrder
, KeyOrderSize
);
209 NewKeyOrder
[Index
] = RegisterOptionNumber
;
211 Status
= gRT
->SetVariable (
213 &gEfiGlobalVariableGuid
,
214 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
220 FreePool (NewKeyOrder
);
227 Delete Key#### for the given Key Option number.
229 @param KeyOptionNumber Key option number for Key####
231 @retval EFI_SUCCESS Unregister hotkey successfully.
232 @retval EFI_NOT_FOUND No Key#### is found for the given Key Option number.
237 IN UINT16 KeyOptionNumber
240 UINT16 KeyOption
[10];
248 // Delete variable Key####
250 UnicodeSPrint (KeyOption
, sizeof (KeyOption
), L
"Key%04x", KeyOptionNumber
);
253 &gEfiGlobalVariableGuid
,
254 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
260 // Adjust key order array
262 KeyOrder
= BdsLibGetVariableAndSize (
264 &gEfiGlobalVariableGuid
,
267 if (KeyOrder
== NULL
) {
272 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
273 if (KeyOrder
[Index
] == KeyOptionNumber
) {
279 if (Index
!= KeyOrderSize
/ sizeof (UINT16
)) {
281 // KeyOptionNumber found in "KeyOrder", delete it
283 for (Index
= Index2Del
; Index
< KeyOrderSize
/ sizeof (UINT16
) - 1; Index
++) {
284 KeyOrder
[Index
] = KeyOrder
[Index
+ 1];
287 KeyOrderSize
-= sizeof (UINT16
);
290 Status
= gRT
->SetVariable (
292 &gEfiGlobalVariableGuid
,
293 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
305 This is the common notification function for HotKeys, it will be registered
306 with SimpleTextInEx protocol interface - RegisterKeyNotify() of ConIn handle.
308 @param KeyData A pointer to a buffer that is filled in with the keystroke
309 information for the key that was pressed.
311 @retval EFI_SUCCESS KeyData is successfully processed.
312 @return EFI_NOT_FOUND Fail to find boot option variable.
317 IN EFI_KEY_DATA
*KeyData
320 BOOLEAN HotkeyCatched
;
321 LIST_ENTRY BootLists
;
323 BDS_HOTKEY_OPTION
*Hotkey
;
325 BDS_COMMON_OPTION
*BootOption
;
329 EFI_KEY_DATA
*HotkeyData
;
331 if (mHotkeyCallbackPending
) {
333 // When responsing to a Hotkey, ignore sequential hotkey stroke until
334 // the current Boot#### load option returned
339 Status
= EFI_SUCCESS
;
340 Link
= GetFirstNode (&mHotkeyList
);
342 while (!IsNull (&mHotkeyList
, Link
)) {
343 HotkeyCatched
= FALSE
;
344 Hotkey
= BDS_HOTKEY_OPTION_FROM_LINK (Link
);
347 // Is this Key Stroke we are waiting for?
349 ASSERT (Hotkey
->WaitingKey
< (sizeof (Hotkey
->KeyData
) / sizeof (Hotkey
->KeyData
[0])));
350 HotkeyData
= &Hotkey
->KeyData
[Hotkey
->WaitingKey
];
351 if ((KeyData
->Key
.ScanCode
== HotkeyData
->Key
.ScanCode
) &&
352 (KeyData
->Key
.UnicodeChar
== HotkeyData
->Key
.UnicodeChar
) &&
353 (((HotkeyData
->KeyState
.KeyShiftState
& EFI_SHIFT_STATE_VALID
) != 0) ? (KeyData
->KeyState
.KeyShiftState
== HotkeyData
->KeyState
.KeyShiftState
) : TRUE
)) {
355 // Receive an expecting key stroke
357 if (Hotkey
->CodeCount
> 1) {
359 // For hotkey of key combination, transit to next waiting state
361 Hotkey
->WaitingKey
++;
363 if (Hotkey
->WaitingKey
== Hotkey
->CodeCount
) {
365 // Received the whole key stroke sequence
367 HotkeyCatched
= TRUE
;
371 // For hotkey of single key stroke
373 HotkeyCatched
= TRUE
;
377 // Receive an unexpected key stroke, reset to initial waiting state
379 Hotkey
->WaitingKey
= 0;
384 // Reset to initial waiting state
386 Hotkey
->WaitingKey
= 0;
389 // Launch its BootOption
391 InitializeListHead (&BootLists
);
393 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", Hotkey
->BootOptionNumber
);
394 BootOption
= BdsLibVariableToOption (&BootLists
, Buffer
);
395 if (BootOption
== NULL
) {
396 return EFI_NOT_FOUND
;
398 BootOption
->BootCurrent
= Hotkey
->BootOptionNumber
;
399 BdsLibConnectDevicePath (BootOption
->DevicePath
);
402 // Clear the screen before launch this BootOption
404 gST
->ConOut
->Reset (gST
->ConOut
, FALSE
);
406 mHotkeyCallbackPending
= TRUE
;
407 Status
= BdsLibBootViaBootOption (BootOption
, BootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
408 mHotkeyCallbackPending
= FALSE
;
410 if (EFI_ERROR (Status
)) {
412 // Call platform action to indicate the boot fail
414 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_FAILED
));
415 PlatformBdsBootFail (BootOption
, Status
, ExitData
, ExitDataSize
);
418 // Call platform action to indicate the boot success
420 BootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED
));
421 PlatformBdsBootSuccess (BootOption
);
425 Link
= GetNextNode (&mHotkeyList
, Link
);
432 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
) && (Index
< (sizeof (Hotkey
->KeyData
) / sizeof (EFI_KEY_DATA
))));
475 Link
= GetNextNode (&mHotkeyList
, Link
);
482 Callback function for SimpleTextInEx protocol install events
484 @param Event the event that is signaled.
485 @param Context not used here.
498 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleTextInEx
;
501 BufferSize
= sizeof (EFI_HANDLE
);
502 Status
= gBS
->LocateHandle (
509 if (EFI_ERROR (Status
)) {
511 // If no more notification events exist
516 Status
= gBS
->HandleProtocol (
518 &gEfiSimpleTextInputExProtocolGuid
,
519 (VOID
**) &SimpleTextInEx
521 ASSERT_EFI_ERROR (Status
);
523 HotkeyRegisterNotify (SimpleTextInEx
);
528 Insert Key Option to hotkey list.
530 @param KeyOption The Hot Key Option to be added to hotkey list.
532 @retval EFI_SUCCESS Add to hotkey list success.
533 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory resource.
537 IN EFI_KEY_OPTION
*KeyOption
540 BDS_HOTKEY_OPTION
*HotkeyLeft
;
541 BDS_HOTKEY_OPTION
*HotkeyRight
;
543 EFI_BOOT_KEY_DATA KeyOptions
;
544 UINT32 KeyShiftStateLeft
;
545 UINT32 KeyShiftStateRight
;
546 EFI_INPUT_KEY
*InputKey
;
547 EFI_KEY_DATA
*KeyData
;
549 HotkeyLeft
= AllocateZeroPool (sizeof (BDS_HOTKEY_OPTION
));
550 if (HotkeyLeft
== NULL
) {
551 return EFI_OUT_OF_RESOURCES
;
554 HotkeyLeft
->Signature
= BDS_HOTKEY_OPTION_SIGNATURE
;
555 HotkeyLeft
->BootOptionNumber
= KeyOption
->BootOption
;
557 KeyOptions
= KeyOption
->KeyData
;
559 HotkeyLeft
->CodeCount
= (UINT8
) KeyOptions
.Options
.InputKeyCount
;
562 // Map key shift state from KeyOptions to EFI_KEY_DATA.KeyState
564 KeyShiftStateRight
= EFI_SHIFT_STATE_VALID
;
565 if (KeyOptions
.Options
.ShiftPressed
) {
566 KeyShiftStateRight
|= EFI_RIGHT_SHIFT_PRESSED
;
568 if (KeyOptions
.Options
.ControlPressed
) {
569 KeyShiftStateRight
|= EFI_RIGHT_CONTROL_PRESSED
;
571 if (KeyOptions
.Options
.AltPressed
) {
572 KeyShiftStateRight
|= EFI_RIGHT_ALT_PRESSED
;
574 if (KeyOptions
.Options
.LogoPressed
) {
575 KeyShiftStateRight
|= EFI_RIGHT_LOGO_PRESSED
;
577 if (KeyOptions
.Options
.MenuPressed
) {
578 KeyShiftStateRight
|= EFI_MENU_KEY_PRESSED
;
580 if (KeyOptions
.Options
.SysReqPressed
) {
581 KeyShiftStateRight
|= EFI_SYS_REQ_PRESSED
;
585 KeyShiftStateLeft
= (KeyShiftStateRight
& 0xffffff00) | ((KeyShiftStateRight
& 0xff) << 1);
587 InputKey
= (EFI_INPUT_KEY
*) (((UINT8
*) KeyOption
) + sizeof (EFI_KEY_OPTION
));
590 KeyData
= &HotkeyLeft
->KeyData
[0];
593 // If Key CodeCount is 0, then only KeyData[0] is used;
594 // if Key CodeCount is n, then KeyData[0]~KeyData[n-1] are used
596 KeyData
->Key
.ScanCode
= InputKey
[Index
].ScanCode
;
597 KeyData
->Key
.UnicodeChar
= InputKey
[Index
].UnicodeChar
;
598 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateLeft
;
602 } while (Index
< HotkeyLeft
->CodeCount
);
603 InsertTailList (&mHotkeyList
, &HotkeyLeft
->Link
);
605 if (KeyShiftStateLeft
!= KeyShiftStateRight
) {
607 // Need an extra hotkey for shift key on right
609 HotkeyRight
= AllocateCopyPool (sizeof (BDS_HOTKEY_OPTION
), HotkeyLeft
);
610 if (HotkeyRight
== NULL
) {
611 return EFI_OUT_OF_RESOURCES
;
615 KeyData
= &HotkeyRight
->KeyData
[0];
618 // Key.ScanCode and Key.UnicodeChar have already been initialized,
619 // only need to update KeyState.KeyShiftState
621 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateRight
;
625 } while (Index
< HotkeyRight
->CodeCount
);
626 InsertTailList (&mHotkeyList
, &HotkeyRight
->Link
);
634 Process all the "Key####" variables, associate Hotkeys with corresponding Boot Options.
636 @retval EFI_SUCCESS Hotkey services successfully initialized.
637 @retval EFI_NOT_FOUND Can not find the "KeyOrder" variable
640 InitializeHotkeyService (
645 UINT32 BootOptionSupport
;
649 UINT16 KeyOptionName
[8];
651 EFI_KEY_OPTION
*KeyOption
;
654 // Export our capability - EFI_BOOT_OPTION_SUPPORT_KEY and EFI_BOOT_OPTION_SUPPORT_APP
655 // with maximum number of key presses of 3
657 BootOptionSupport
= EFI_BOOT_OPTION_SUPPORT_KEY
| EFI_BOOT_OPTION_SUPPORT_APP
;
658 SET_BOOT_OPTION_SUPPORT_KEY_COUNT (BootOptionSupport
, 3);
659 Status
= gRT
->SetVariable (
660 L
"BootOptionSupport",
661 &gEfiGlobalVariableGuid
,
662 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
668 // Get valid Key Option List from private EFI variable "KeyOrder"
670 KeyOrder
= BdsLibGetVariableAndSize (
672 &gEfiGlobalVariableGuid
,
676 if (KeyOrder
== NULL
) {
677 return EFI_NOT_FOUND
;
680 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
681 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", KeyOrder
[Index
]);
682 KeyOption
= BdsLibGetVariableAndSize (
684 &gEfiGlobalVariableGuid
,
688 if (KeyOption
== NULL
|| !IsKeyOptionValid (KeyOption
)) {
689 UnregisterHotkey (KeyOrder
[Index
]);
691 HotkeyInsertList (KeyOption
);
696 // Register Protocol notify for Hotkey service
698 Status
= gBS
->CreateEvent (
705 ASSERT_EFI_ERROR (Status
);
708 // Register for protocol notifications on this event
710 Status
= gBS
->RegisterProtocolNotify (
711 &gEfiSimpleTextInputExProtocolGuid
,
715 ASSERT_EFI_ERROR (Status
);