]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/BdsDxe/Hotkey.c
Add assertion to make sure the pointer is not NULL.
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / BdsDxe / Hotkey.c
1 /** @file
2 Provides a way for 3rd party applications to register themselves for launch by the
3 Boot Manager based on hot key
4
5 Copyright (c) 2007 - 2012, 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
10
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.
13
14 **/
15
16 #include "Hotkey.h"
17
18
19 LIST_ENTRY mHotkeyList = INITIALIZE_LIST_HEAD_VARIABLE (mHotkeyList);
20 BDS_COMMON_OPTION *mHotkeyBootOption = NULL;
21 EFI_EVENT mHotkeyEvent;
22 VOID *mHotkeyRegistration;
23
24
25 /**
26 Check if the Key Option is valid or not.
27
28 @param KeyOption The Hot Key Option to be checked.
29
30 @retval TRUE The Hot Key Option is valid.
31 @retval FALSE The Hot Key Option is invalid.
32
33 **/
34 BOOLEAN
35 IsKeyOptionValid (
36 IN EFI_KEY_OPTION *KeyOption
37 )
38 {
39 UINT16 BootOptionName[10];
40 UINT8 *BootOptionVar;
41 UINTN BootOptionSize;
42 UINT32 Crc;
43
44 //
45 // Check whether corresponding Boot Option exist
46 //
47 UnicodeSPrint (BootOptionName, sizeof (BootOptionName), L"Boot%04x", KeyOption->BootOption);
48 BootOptionVar = BdsLibGetVariableAndSize (
49 BootOptionName,
50 &gEfiGlobalVariableGuid,
51 &BootOptionSize
52 );
53
54 if (BootOptionVar == NULL || BootOptionSize == 0) {
55 return FALSE;
56 }
57
58 //
59 // Check CRC for Boot Option
60 //
61 gBS->CalculateCrc32 (BootOptionVar, BootOptionSize, &Crc);
62 FreePool (BootOptionVar);
63
64 return (BOOLEAN) ((KeyOption->BootOptionCrc == Crc) ? TRUE : FALSE);
65 }
66
67 /**
68 Try to boot the boot option triggered by hotkey.
69 @retval EFI_SUCCESS There is HotkeyBootOption & it is processed
70 @retval EFI_NOT_FOUND There is no HotkeyBootOption
71 **/
72 EFI_STATUS
73 HotkeyBoot (
74 VOID
75 )
76 {
77 EFI_STATUS Status;
78 UINTN ExitDataSize;
79 CHAR16 *ExitData;
80
81 if (mHotkeyBootOption == NULL) {
82 return EFI_NOT_FOUND;
83 }
84
85 BdsLibConnectDevicePath (mHotkeyBootOption->DevicePath);
86
87 //
88 // Clear the screen before launch this BootOption
89 //
90 gST->ConOut->Reset (gST->ConOut, FALSE);
91
92 Status = BdsLibBootViaBootOption (mHotkeyBootOption, mHotkeyBootOption->DevicePath, &ExitDataSize, &ExitData);
93
94 if (EFI_ERROR (Status)) {
95 //
96 // Call platform action to indicate the boot fail
97 //
98 mHotkeyBootOption->StatusString = GetStringById (STRING_TOKEN (STR_BOOT_FAILED));
99 PlatformBdsBootFail (mHotkeyBootOption, Status, ExitData, ExitDataSize);
100 } else {
101 //
102 // Call platform action to indicate the boot success
103 //
104 mHotkeyBootOption->StatusString = GetStringById (STRING_TOKEN (STR_BOOT_SUCCEEDED));
105 PlatformBdsBootSuccess (mHotkeyBootOption);
106 }
107 FreePool (mHotkeyBootOption->Description);
108 FreePool (mHotkeyBootOption->DevicePath);
109 FreePool (mHotkeyBootOption->LoadOptions);
110 FreePool (mHotkeyBootOption);
111
112 mHotkeyBootOption = NULL;
113
114 return EFI_SUCCESS;
115 }
116
117 /**
118
119 This is the common notification function for HotKeys, it will be registered
120 with SimpleTextInEx protocol interface - RegisterKeyNotify() of ConIn handle.
121
122 @param KeyData A pointer to a buffer that is filled in with the keystroke
123 information for the key that was pressed.
124
125 @retval EFI_SUCCESS KeyData is successfully processed.
126 @return EFI_NOT_FOUND Fail to find boot option variable.
127 **/
128 EFI_STATUS
129 EFIAPI
130 HotkeyCallback (
131 IN EFI_KEY_DATA *KeyData
132 )
133 {
134 BOOLEAN HotkeyCatched;
135 LIST_ENTRY BootLists;
136 LIST_ENTRY *Link;
137 BDS_HOTKEY_OPTION *Hotkey;
138 UINT16 Buffer[10];
139 EFI_STATUS Status;
140 EFI_KEY_DATA *HotkeyData;
141
142 if (mHotkeyBootOption != NULL) {
143 //
144 // Do not process sequential hotkey stroke until the current boot option returns
145 //
146 return EFI_SUCCESS;
147 }
148
149 Status = EFI_SUCCESS;
150
151 for ( Link = GetFirstNode (&mHotkeyList)
152 ; !IsNull (&mHotkeyList, Link)
153 ; Link = GetNextNode (&mHotkeyList, Link)
154 ) {
155 HotkeyCatched = FALSE;
156 Hotkey = BDS_HOTKEY_OPTION_FROM_LINK (Link);
157
158 //
159 // Is this Key Stroke we are waiting for?
160 //
161 ASSERT (Hotkey->WaitingKey < (sizeof (Hotkey->KeyData) / sizeof (Hotkey->KeyData[0])));
162 HotkeyData = &Hotkey->KeyData[Hotkey->WaitingKey];
163 if ((KeyData->Key.ScanCode == HotkeyData->Key.ScanCode) &&
164 (KeyData->Key.UnicodeChar == HotkeyData->Key.UnicodeChar) &&
165 (((KeyData->KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) != 0) ?
166 (KeyData->KeyState.KeyShiftState == HotkeyData->KeyState.KeyShiftState) : TRUE
167 )
168 ) {
169 //
170 // For hotkey of key combination, transit to next waiting state
171 //
172 Hotkey->WaitingKey++;
173
174 if (Hotkey->WaitingKey == Hotkey->CodeCount) {
175 //
176 // Received the whole key stroke sequence
177 //
178 HotkeyCatched = TRUE;
179 }
180 } else {
181 //
182 // Receive an unexpected key stroke, reset to initial waiting state
183 //
184 Hotkey->WaitingKey = 0;
185 }
186
187 if (HotkeyCatched) {
188 //
189 // Reset to initial waiting state
190 //
191 Hotkey->WaitingKey = 0;
192
193 //
194 // Launch its BootOption
195 //
196 InitializeListHead (&BootLists);
197
198 UnicodeSPrint (Buffer, sizeof (Buffer), L"Boot%04x", Hotkey->BootOptionNumber);
199 mHotkeyBootOption = BdsLibVariableToOption (&BootLists, Buffer);
200 }
201 }
202
203 return Status;
204 }
205
206 /**
207 Register the common HotKey notify function to given SimpleTextInEx protocol instance.
208
209 @param SimpleTextInEx Simple Text Input Ex protocol instance
210
211 @retval EFI_SUCCESS Register hotkey notification function successfully.
212 @retval EFI_OUT_OF_RESOURCES Unable to allocate necessary data structures.
213
214 **/
215 EFI_STATUS
216 HotkeyRegisterNotify (
217 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleTextInEx
218 )
219 {
220 UINTN Index;
221 EFI_STATUS Status;
222 LIST_ENTRY *Link;
223 BDS_HOTKEY_OPTION *Hotkey;
224
225 //
226 // Register notification function for each hotkey
227 //
228 Link = GetFirstNode (&mHotkeyList);
229
230 while (!IsNull (&mHotkeyList, Link)) {
231 Hotkey = BDS_HOTKEY_OPTION_FROM_LINK (Link);
232
233 Index = 0;
234 do {
235 Status = SimpleTextInEx->RegisterKeyNotify (
236 SimpleTextInEx,
237 &Hotkey->KeyData[Index],
238 HotkeyCallback,
239 &Hotkey->NotifyHandle
240 );
241 if (EFI_ERROR (Status)) {
242 //
243 // some of the hotkey registry failed
244 //
245 return Status;
246 }
247 Index ++;
248 } while ((Index < Hotkey->CodeCount) && (Index < (sizeof (Hotkey->KeyData) / sizeof (EFI_KEY_DATA))));
249
250 Link = GetNextNode (&mHotkeyList, Link);
251 }
252
253 return EFI_SUCCESS;
254 }
255
256 /**
257 Callback function for SimpleTextInEx protocol install events
258
259 @param Event the event that is signaled.
260 @param Context not used here.
261
262 **/
263 VOID
264 EFIAPI
265 HotkeyEvent (
266 IN EFI_EVENT Event,
267 IN VOID *Context
268 )
269 {
270 EFI_STATUS Status;
271 UINTN BufferSize;
272 EFI_HANDLE Handle;
273 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleTextInEx;
274
275 while (TRUE) {
276 BufferSize = sizeof (EFI_HANDLE);
277 Status = gBS->LocateHandle (
278 ByRegisterNotify,
279 NULL,
280 mHotkeyRegistration,
281 &BufferSize,
282 &Handle
283 );
284 if (EFI_ERROR (Status)) {
285 //
286 // If no more notification events exist
287 //
288 return ;
289 }
290
291 Status = gBS->HandleProtocol (
292 Handle,
293 &gEfiSimpleTextInputExProtocolGuid,
294 (VOID **) &SimpleTextInEx
295 );
296 ASSERT_EFI_ERROR (Status);
297
298 HotkeyRegisterNotify (SimpleTextInEx);
299 }
300 }
301
302 /**
303 Insert Key Option to hotkey list.
304
305 @param KeyOption The Hot Key Option to be added to hotkey list.
306
307 @retval EFI_SUCCESS Add to hotkey list success.
308 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory resource.
309 **/
310 EFI_STATUS
311 HotkeyInsertList (
312 IN EFI_KEY_OPTION *KeyOption
313 )
314 {
315 BDS_HOTKEY_OPTION *HotkeyLeft;
316 BDS_HOTKEY_OPTION *HotkeyRight;
317 UINTN Index;
318 UINT32 KeyShiftStateLeft;
319 UINT32 KeyShiftStateRight;
320 EFI_INPUT_KEY *InputKey;
321 EFI_KEY_DATA *KeyData;
322
323 HotkeyLeft = AllocateZeroPool (sizeof (BDS_HOTKEY_OPTION));
324 if (HotkeyLeft == NULL) {
325 return EFI_OUT_OF_RESOURCES;
326 }
327
328 HotkeyLeft->Signature = BDS_HOTKEY_OPTION_SIGNATURE;
329 HotkeyLeft->BootOptionNumber = KeyOption->BootOption;
330 HotkeyLeft->CodeCount = (UINT8) KEY_OPTION_INPUT_KEY_COUNT (KeyOption);
331
332 //
333 // Map key shift state from KeyOptions to EFI_KEY_DATA.KeyState
334 //
335 KeyShiftStateRight = EFI_SHIFT_STATE_VALID;
336 if (KEY_OPTION_SHIFT_PRESSED (KeyOption)) {
337 KeyShiftStateRight |= EFI_RIGHT_SHIFT_PRESSED;
338 }
339 if (KEY_OPTION_CONTROL_PRESSED (KeyOption)) {
340 KeyShiftStateRight |= EFI_RIGHT_CONTROL_PRESSED;
341 }
342 if (KEY_OPTION_ALT_PRESSED (KeyOption)) {
343 KeyShiftStateRight |= EFI_RIGHT_ALT_PRESSED;
344 }
345 if (KEY_OPTION_LOGO_PRESSED (KeyOption)) {
346 KeyShiftStateRight |= EFI_RIGHT_LOGO_PRESSED;
347 }
348 if (KEY_OPTION_MENU_PRESSED (KeyOption)) {
349 KeyShiftStateRight |= EFI_MENU_KEY_PRESSED;
350 }
351 if (KEY_OPTION_SYS_REQ_PRESSED (KeyOption)) {
352 KeyShiftStateRight |= EFI_SYS_REQ_PRESSED;
353 }
354
355 KeyShiftStateLeft = (KeyShiftStateRight & 0xffffff00) | ((KeyShiftStateRight & 0xff) << 1);
356
357 InputKey = (EFI_INPUT_KEY *) (((UINT8 *) KeyOption) + sizeof (EFI_KEY_OPTION));
358
359 Index = 0;
360 KeyData = &HotkeyLeft->KeyData[0];
361 do {
362 //
363 // If Key CodeCount is 0, then only KeyData[0] is used;
364 // if Key CodeCount is n, then KeyData[0]~KeyData[n-1] are used
365 //
366 KeyData->Key.ScanCode = InputKey[Index].ScanCode;
367 KeyData->Key.UnicodeChar = InputKey[Index].UnicodeChar;
368 KeyData->KeyState.KeyShiftState = KeyShiftStateLeft;
369
370 Index++;
371 KeyData++;
372 } while (Index < HotkeyLeft->CodeCount);
373 InsertTailList (&mHotkeyList, &HotkeyLeft->Link);
374
375 if (KeyShiftStateLeft != KeyShiftStateRight) {
376 //
377 // Need an extra hotkey for shift key on right
378 //
379 HotkeyRight = AllocateCopyPool (sizeof (BDS_HOTKEY_OPTION), HotkeyLeft);
380 if (HotkeyRight == NULL) {
381 return EFI_OUT_OF_RESOURCES;
382 }
383
384 Index = 0;
385 KeyData = &HotkeyRight->KeyData[0];
386 do {
387 //
388 // Key.ScanCode and Key.UnicodeChar have already been initialized,
389 // only need to update KeyState.KeyShiftState
390 //
391 KeyData->KeyState.KeyShiftState = KeyShiftStateRight;
392
393 Index++;
394 KeyData++;
395 } while (Index < HotkeyRight->CodeCount);
396 InsertTailList (&mHotkeyList, &HotkeyRight->Link);
397 }
398
399 return EFI_SUCCESS;
400 }
401
402 /**
403 Return TRUE when the variable pointed by Name and Guid is a Key#### variable.
404
405 @param Name The name of the variable.
406 @param Guid The GUID of the variable.
407 @param OptionNumber Return the option number parsed from the Name.
408
409 @retval TRUE The variable pointed by Name and Guid is a Key#### variable.
410 @retval FALSE The variable pointed by Name and Guid isn't a Key#### variable.
411 **/
412 BOOLEAN
413 IsKeyOptionVariable (
414 CHAR16 *Name,
415 EFI_GUID *Guid,
416 UINT16 *OptionNumber
417 )
418 {
419 UINTN Index;
420
421 if (!CompareGuid (Guid, &gEfiGlobalVariableGuid) ||
422 (StrSize (Name) != sizeof (L"Key####")) ||
423 (StrnCmp (Name, L"Key", 3) != 0)
424 ) {
425 return FALSE;
426 }
427
428 *OptionNumber = 0;
429 for (Index = 3; Index < 7; Index++) {
430 if ((Name[Index] >= L'0') && (Name[Index] <= L'9')) {
431 *OptionNumber = *OptionNumber * 10 + Name[Index] - L'0';
432 } else if ((Name[Index] >= L'A') && (Name[Index] <= L'F')) {
433 *OptionNumber = *OptionNumber * 10 + Name[Index] - L'A';
434 } else {
435 return FALSE;
436 }
437 }
438
439 return TRUE;
440 }
441
442 /**
443 Return an array of key option numbers.
444
445 @param Count Return the count of key option numbers.
446
447 @return UINT16* Pointer to an array of key option numbers;
448 **/
449 UINT16 *
450 EFIAPI
451 HotkeyGetOptionNumbers (
452 OUT UINTN *Count
453 )
454 {
455 EFI_STATUS Status;
456 UINTN Index;
457 CHAR16 *Name;
458 EFI_GUID Guid;
459 UINTN NameSize;
460 UINTN NewNameSize;
461 UINT16 *OptionNumbers;
462 UINT16 OptionNumber;
463
464 if (Count == NULL) {
465 return NULL;
466 }
467
468 *Count = 0;
469 OptionNumbers = NULL;
470
471 NameSize = sizeof (CHAR16);
472 Name = AllocateZeroPool (NameSize);
473 ASSERT (Name != NULL);
474 while (TRUE) {
475 NewNameSize = NameSize;
476 Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);
477 if (Status == EFI_BUFFER_TOO_SMALL) {
478 Name = ReallocatePool (NameSize, NewNameSize, Name);
479 ASSERT (Name != NULL);
480 Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);
481 NameSize = NewNameSize;
482 }
483
484 if (Status == EFI_NOT_FOUND) {
485 break;
486 }
487 ASSERT_EFI_ERROR (Status);
488
489 if (IsKeyOptionVariable (Name ,&Guid, &OptionNumber)) {
490 OptionNumbers = ReallocatePool (
491 *Count * sizeof (UINT16),
492 (*Count + 1) * sizeof (UINT16),
493 OptionNumbers
494 );
495 ASSERT (OptionNumbers != NULL);
496 for (Index = 0; Index < *Count; Index++) {
497 if (OptionNumber < OptionNumbers[Index]) {
498 break;
499 }
500 }
501 CopyMem (&OptionNumbers[Index + 1], &OptionNumbers[Index], (*Count - Index) * sizeof (UINT16));
502 OptionNumbers[Index] = OptionNumber;
503 (*Count)++;
504 }
505 }
506
507 FreePool (Name);
508
509 return OptionNumbers;
510 }
511
512 /**
513
514 Process all the "Key####" variables, associate Hotkeys with corresponding Boot Options.
515
516 @retval EFI_SUCCESS Hotkey services successfully initialized.
517 **/
518 EFI_STATUS
519 InitializeHotkeyService (
520 VOID
521 )
522 {
523 EFI_STATUS Status;
524 UINT32 BootOptionSupport;
525 UINT16 *KeyOptionNumbers;
526 UINTN KeyOptionCount;
527 UINTN Index;
528 CHAR16 KeyOptionName[8];
529 EFI_KEY_OPTION *KeyOption;
530
531 //
532 // Export our capability - EFI_BOOT_OPTION_SUPPORT_KEY and EFI_BOOT_OPTION_SUPPORT_APP.
533 // with maximum number of key presses of 3
534 // Do not report the hotkey capability if PcdConInConnectOnDemand is enabled.
535 //
536 BootOptionSupport = EFI_BOOT_OPTION_SUPPORT_APP;
537 if (!PcdGetBool (PcdConInConnectOnDemand)) {
538 BootOptionSupport |= EFI_BOOT_OPTION_SUPPORT_KEY;
539 SET_BOOT_OPTION_SUPPORT_KEY_COUNT (BootOptionSupport, 3);
540 }
541
542 Status = gRT->SetVariable (
543 L"BootOptionSupport",
544 &gEfiGlobalVariableGuid,
545 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
546 sizeof (UINT32),
547 &BootOptionSupport
548 );
549 ASSERT_EFI_ERROR (Status);
550
551 KeyOptionNumbers = HotkeyGetOptionNumbers (&KeyOptionCount);
552 for (Index = 0; Index < KeyOptionCount; Index ++) {
553 UnicodeSPrint (KeyOptionName, sizeof (KeyOptionName), L"Key%04x", KeyOptionNumbers[Index]);
554 GetEfiGlobalVariable2 (KeyOptionName, (VOID **) &KeyOption, NULL);
555 ASSERT (KeyOption != NULL);
556 if (IsKeyOptionValid (KeyOption)) {
557 HotkeyInsertList (KeyOption);
558 }
559 FreePool (KeyOption);
560 }
561
562 if (KeyOptionNumbers != NULL) {
563 FreePool (KeyOptionNumbers);
564 }
565
566 //
567 // Register Protocol notify for Hotkey service
568 //
569 Status = gBS->CreateEvent (
570 EVT_NOTIFY_SIGNAL,
571 TPL_CALLBACK,
572 HotkeyEvent,
573 NULL,
574 &mHotkeyEvent
575 );
576 ASSERT_EFI_ERROR (Status);
577
578 //
579 // Register for protocol notifications on this event
580 //
581 Status = gBS->RegisterProtocolNotify (
582 &gEfiSimpleTextInputExProtocolGuid,
583 mHotkeyEvent,
584 &mHotkeyRegistration
585 );
586 ASSERT_EFI_ERROR (Status);
587
588 return Status;
589 }
590