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 - 2011, 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 BDS_COMMON_OPTION
*mHotkeyBootOption
= NULL
;
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
,
304 Try to boot the boot option triggered by hotkey.
315 if (mHotkeyBootOption
!= NULL
) {
316 BdsLibConnectDevicePath (mHotkeyBootOption
->DevicePath
);
319 // Clear the screen before launch this BootOption
321 gST
->ConOut
->Reset (gST
->ConOut
, FALSE
);
323 Status
= BdsLibBootViaBootOption (mHotkeyBootOption
, mHotkeyBootOption
->DevicePath
, &ExitDataSize
, &ExitData
);
325 if (EFI_ERROR (Status
)) {
327 // Call platform action to indicate the boot fail
329 mHotkeyBootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_FAILED
));
330 PlatformBdsBootFail (mHotkeyBootOption
, Status
, ExitData
, ExitDataSize
);
333 // Call platform action to indicate the boot success
335 mHotkeyBootOption
->StatusString
= GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED
));
336 PlatformBdsBootSuccess (mHotkeyBootOption
);
338 FreePool (mHotkeyBootOption
->Description
);
339 FreePool (mHotkeyBootOption
->DevicePath
);
340 FreePool (mHotkeyBootOption
->LoadOptions
);
341 FreePool (mHotkeyBootOption
);
343 mHotkeyBootOption
= NULL
;
349 This is the common notification function for HotKeys, it will be registered
350 with SimpleTextInEx protocol interface - RegisterKeyNotify() of ConIn handle.
352 @param KeyData A pointer to a buffer that is filled in with the keystroke
353 information for the key that was pressed.
355 @retval EFI_SUCCESS KeyData is successfully processed.
356 @return EFI_NOT_FOUND Fail to find boot option variable.
361 IN EFI_KEY_DATA
*KeyData
364 BOOLEAN HotkeyCatched
;
365 LIST_ENTRY BootLists
;
367 BDS_HOTKEY_OPTION
*Hotkey
;
370 EFI_KEY_DATA
*HotkeyData
;
372 if (mHotkeyBootOption
!= NULL
) {
374 // Do not process sequential hotkey stroke until the current boot option returns
379 Status
= EFI_SUCCESS
;
381 for ( Link
= GetFirstNode (&mHotkeyList
)
382 ; !IsNull (&mHotkeyList
, Link
)
383 ; Link
= GetNextNode (&mHotkeyList
, Link
)
385 HotkeyCatched
= FALSE
;
386 Hotkey
= BDS_HOTKEY_OPTION_FROM_LINK (Link
);
389 // Is this Key Stroke we are waiting for?
391 ASSERT (Hotkey
->WaitingKey
< (sizeof (Hotkey
->KeyData
) / sizeof (Hotkey
->KeyData
[0])));
392 HotkeyData
= &Hotkey
->KeyData
[Hotkey
->WaitingKey
];
393 if ((KeyData
->Key
.ScanCode
== HotkeyData
->Key
.ScanCode
) &&
394 (KeyData
->Key
.UnicodeChar
== HotkeyData
->Key
.UnicodeChar
) &&
395 (((KeyData
->KeyState
.KeyShiftState
& EFI_SHIFT_STATE_VALID
) != 0) ?
396 (KeyData
->KeyState
.KeyShiftState
== HotkeyData
->KeyState
.KeyShiftState
) : TRUE
400 // For hotkey of key combination, transit to next waiting state
402 Hotkey
->WaitingKey
++;
404 if (Hotkey
->WaitingKey
== Hotkey
->CodeCount
) {
406 // Received the whole key stroke sequence
408 HotkeyCatched
= TRUE
;
412 // Receive an unexpected key stroke, reset to initial waiting state
414 Hotkey
->WaitingKey
= 0;
419 // Reset to initial waiting state
421 Hotkey
->WaitingKey
= 0;
424 // Launch its BootOption
426 InitializeListHead (&BootLists
);
428 UnicodeSPrint (Buffer
, sizeof (Buffer
), L
"Boot%04x", Hotkey
->BootOptionNumber
);
429 mHotkeyBootOption
= BdsLibVariableToOption (&BootLists
, Buffer
);
437 Register the common HotKey notify function to given SimpleTextInEx protocol instance.
439 @param SimpleTextInEx Simple Text Input Ex protocol instance
441 @retval EFI_SUCCESS Register hotkey notification function successfully.
442 @retval EFI_OUT_OF_RESOURCES Unable to allocate necessary data structures.
446 HotkeyRegisterNotify (
447 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleTextInEx
453 BDS_HOTKEY_OPTION
*Hotkey
;
456 // Register notification function for each hotkey
458 Link
= GetFirstNode (&mHotkeyList
);
460 while (!IsNull (&mHotkeyList
, Link
)) {
461 Hotkey
= BDS_HOTKEY_OPTION_FROM_LINK (Link
);
465 Status
= SimpleTextInEx
->RegisterKeyNotify (
467 &Hotkey
->KeyData
[Index
],
469 &Hotkey
->NotifyHandle
471 if (EFI_ERROR (Status
)) {
473 // some of the hotkey registry failed
478 } while ((Index
< Hotkey
->CodeCount
) && (Index
< (sizeof (Hotkey
->KeyData
) / sizeof (EFI_KEY_DATA
))));
480 Link
= GetNextNode (&mHotkeyList
, Link
);
487 Callback function for SimpleTextInEx protocol install events
489 @param Event the event that is signaled.
490 @param Context not used here.
503 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleTextInEx
;
506 BufferSize
= sizeof (EFI_HANDLE
);
507 Status
= gBS
->LocateHandle (
514 if (EFI_ERROR (Status
)) {
516 // If no more notification events exist
521 Status
= gBS
->HandleProtocol (
523 &gEfiSimpleTextInputExProtocolGuid
,
524 (VOID
**) &SimpleTextInEx
526 ASSERT_EFI_ERROR (Status
);
528 HotkeyRegisterNotify (SimpleTextInEx
);
533 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.
538 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory resource.
542 IN EFI_KEY_OPTION
*KeyOption
545 BDS_HOTKEY_OPTION
*HotkeyLeft
;
546 BDS_HOTKEY_OPTION
*HotkeyRight
;
548 EFI_BOOT_KEY_DATA KeyOptions
;
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
->KeyData
;
564 HotkeyLeft
->CodeCount
= (UINT8
) KeyOptions
.Options
.InputKeyCount
;
567 // Map key shift state from KeyOptions to EFI_KEY_DATA.KeyState
569 KeyShiftStateRight
= EFI_SHIFT_STATE_VALID
;
570 if (KeyOptions
.Options
.ShiftPressed
) {
571 KeyShiftStateRight
|= EFI_RIGHT_SHIFT_PRESSED
;
573 if (KeyOptions
.Options
.ControlPressed
) {
574 KeyShiftStateRight
|= EFI_RIGHT_CONTROL_PRESSED
;
576 if (KeyOptions
.Options
.AltPressed
) {
577 KeyShiftStateRight
|= EFI_RIGHT_ALT_PRESSED
;
579 if (KeyOptions
.Options
.LogoPressed
) {
580 KeyShiftStateRight
|= EFI_RIGHT_LOGO_PRESSED
;
582 if (KeyOptions
.Options
.MenuPressed
) {
583 KeyShiftStateRight
|= EFI_MENU_KEY_PRESSED
;
585 if (KeyOptions
.Options
.SysReqPressed
) {
586 KeyShiftStateRight
|= EFI_SYS_REQ_PRESSED
;
590 KeyShiftStateLeft
= (KeyShiftStateRight
& 0xffffff00) | ((KeyShiftStateRight
& 0xff) << 1);
592 InputKey
= (EFI_INPUT_KEY
*) (((UINT8
*) KeyOption
) + sizeof (EFI_KEY_OPTION
));
595 KeyData
= &HotkeyLeft
->KeyData
[0];
598 // If Key CodeCount is 0, then only KeyData[0] is used;
599 // if Key CodeCount is n, then KeyData[0]~KeyData[n-1] are used
601 KeyData
->Key
.ScanCode
= InputKey
[Index
].ScanCode
;
602 KeyData
->Key
.UnicodeChar
= InputKey
[Index
].UnicodeChar
;
603 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateLeft
;
607 } while (Index
< HotkeyLeft
->CodeCount
);
608 InsertTailList (&mHotkeyList
, &HotkeyLeft
->Link
);
610 if (KeyShiftStateLeft
!= KeyShiftStateRight
) {
612 // Need an extra hotkey for shift key on right
614 HotkeyRight
= AllocateCopyPool (sizeof (BDS_HOTKEY_OPTION
), HotkeyLeft
);
615 if (HotkeyRight
== NULL
) {
616 return EFI_OUT_OF_RESOURCES
;
620 KeyData
= &HotkeyRight
->KeyData
[0];
623 // Key.ScanCode and Key.UnicodeChar have already been initialized,
624 // only need to update KeyState.KeyShiftState
626 KeyData
->KeyState
.KeyShiftState
= KeyShiftStateRight
;
630 } while (Index
< HotkeyRight
->CodeCount
);
631 InsertTailList (&mHotkeyList
, &HotkeyRight
->Link
);
639 Process all the "Key####" variables, associate Hotkeys with corresponding Boot Options.
641 @retval EFI_SUCCESS Hotkey services successfully initialized.
642 @retval EFI_NOT_FOUND Can not find the "KeyOrder" variable
645 InitializeHotkeyService (
650 UINT32 BootOptionSupport
;
654 UINT16 KeyOptionName
[8];
656 EFI_KEY_OPTION
*KeyOption
;
659 // Export our capability - EFI_BOOT_OPTION_SUPPORT_KEY and EFI_BOOT_OPTION_SUPPORT_APP
660 // with maximum number of key presses of 3
662 BootOptionSupport
= EFI_BOOT_OPTION_SUPPORT_KEY
| EFI_BOOT_OPTION_SUPPORT_APP
;
663 SET_BOOT_OPTION_SUPPORT_KEY_COUNT (BootOptionSupport
, 3);
664 Status
= gRT
->SetVariable (
665 L
"BootOptionSupport",
666 &gEfiGlobalVariableGuid
,
667 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
673 // Get valid Key Option List from private EFI variable "KeyOrder"
675 KeyOrder
= BdsLibGetVariableAndSize (
677 &gEfiGlobalVariableGuid
,
681 if (KeyOrder
== NULL
) {
682 return EFI_NOT_FOUND
;
685 for (Index
= 0; Index
< KeyOrderSize
/ sizeof (UINT16
); Index
++) {
686 UnicodeSPrint (KeyOptionName
, sizeof (KeyOptionName
), L
"Key%04x", KeyOrder
[Index
]);
687 KeyOption
= BdsLibGetVariableAndSize (
689 &gEfiGlobalVariableGuid
,
693 if (KeyOption
== NULL
|| !IsKeyOptionValid (KeyOption
)) {
694 UnregisterHotkey (KeyOrder
[Index
]);
696 HotkeyInsertList (KeyOption
);
701 // Register Protocol notify for Hotkey service
703 Status
= gBS
->CreateEvent (
710 ASSERT_EFI_ERROR (Status
);
713 // Register for protocol notifications on this event
715 Status
= gBS
->RegisterProtocolNotify (
716 &gEfiSimpleTextInputExProtocolGuid
,
720 ASSERT_EFI_ERROR (Status
);