1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
|
/** @file
Hotkey library functions.
Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "InternalBm.h"
//
// Lock for linked list
//
EFI_LOCK mBmHotkeyLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
LIST_ENTRY mBmHotkeyList = INITIALIZE_LIST_HEAD_VARIABLE (mBmHotkeyList);
EFI_EVENT mBmHotkeyTriggered = NULL;
BOOLEAN mBmHotkeyServiceStarted = FALSE;
UINTN mBmHotkeySupportCount = 0;
//
// Set OptionNumber as unassigned value to indicate the option isn't initialized
//
EFI_BOOT_MANAGER_LOAD_OPTION mBmHotkeyBootOption = { LoadOptionNumberUnassigned };
EFI_BOOT_MANAGER_KEY_OPTION *mBmContinueKeyOption = NULL;
VOID *mBmTxtInExRegistration = NULL;
/**
Return the buffer size of the EFI_BOOT_MANAGER_KEY_OPTION data.
@param KeyOption The input key option info.
@retval The buffer size of the key option data.
**/
UINTN
BmSizeOfKeyOption (
EFI_BOOT_MANAGER_KEY_OPTION *KeyOption
)
{
return OFFSET_OF (EFI_BOOT_MANAGER_KEY_OPTION, Keys)
+ KeyOption->KeyData.Options.InputKeyCount * sizeof (EFI_INPUT_KEY);
}
/**
Check whether the input key option is valid.
@param KeyOption Key option.
@param KeyOptionSize Size of the key option.
@retval TRUE Input key option is valid.
@retval FALSE Input key option is not valid.
**/
BOOLEAN
BmIsKeyOptionValid (
IN EFI_BOOT_MANAGER_KEY_OPTION *KeyOption,
IN UINTN KeyOptionSize
)
{
UINT16 OptionName[BM_OPTION_NAME_LEN];
UINT8 *BootOption;
UINTN BootOptionSize;
UINT32 Crc;
if (BmSizeOfKeyOption (KeyOption) != KeyOptionSize) {
return FALSE;
}
//
// Check whether corresponding Boot Option exist
//
UnicodeSPrint (
OptionName, sizeof (OptionName), L"%s%04x",
mBmLoadOptionName[LoadOptionTypeBoot], KeyOption->BootOption
);
GetEfiGlobalVariable2 (OptionName, (VOID **) &BootOption, &BootOptionSize);
if (BootOption == NULL) {
return FALSE;
}
//
// Check CRC for Boot Option
//
gBS->CalculateCrc32 (BootOption, BootOptionSize, &Crc);
FreePool (BootOption);
return (BOOLEAN) (KeyOption->BootOptionCrc == Crc);
}
/**
Check whether the input variable is an key option variable.
@param Name Input variable name.
@param Guid Input variable guid.
@param OptionNumber The option number of this key option variable.
@retval TRUE Input variable is a key option variable.
@retval FALSE Input variable is not a key option variable.
**/
BOOLEAN
BmIsKeyOptionVariable (
CHAR16 *Name,
EFI_GUID *Guid,
UINT16 *OptionNumber
)
{
UINTN Index;
UINTN Uint;
if (!CompareGuid (Guid, &gEfiGlobalVariableGuid) ||
(StrSize (Name) != sizeof (L"Key####")) ||
(StrnCmp (Name, L"Key", 3) != 0)
) {
return FALSE;
}
*OptionNumber = 0;
for (Index = 3; Index < 7; Index++) {
Uint = BmCharToUint (Name[Index]);
if (Uint == -1) {
return FALSE;
} else {
*OptionNumber = (UINT16) Uint + *OptionNumber * 0x10;
}
}
return TRUE;
}
typedef struct {
EFI_BOOT_MANAGER_KEY_OPTION *KeyOptions;
UINTN KeyOptionCount;
} BM_COLLECT_KEY_OPTIONS_PARAM;
/**
Visitor function to collect the key options from NV storage.
@param Name Variable name.
@param Guid Variable GUID.
@param Context The same context passed to BmForEachVariable.
**/
VOID
BmCollectKeyOptions (
CHAR16 *Name,
EFI_GUID *Guid,
VOID *Context
)
{
UINTN Index;
BM_COLLECT_KEY_OPTIONS_PARAM *Param;
EFI_BOOT_MANAGER_KEY_OPTION *KeyOption;
UINT16 OptionNumber;
UINTN KeyOptionSize;
Param = (BM_COLLECT_KEY_OPTIONS_PARAM *) Context;
if (BmIsKeyOptionVariable (Name, Guid, &OptionNumber)) {
GetEfiGlobalVariable2 (Name, (VOID**) &KeyOption, &KeyOptionSize);
ASSERT (KeyOption != NULL);
KeyOption->OptionNumber = OptionNumber;
if (BmIsKeyOptionValid (KeyOption, KeyOptionSize)) {
Param->KeyOptions = ReallocatePool (
Param->KeyOptionCount * sizeof (EFI_BOOT_MANAGER_KEY_OPTION),
(Param->KeyOptionCount + 1) * sizeof (EFI_BOOT_MANAGER_KEY_OPTION),
Param->KeyOptions
);
ASSERT (Param->KeyOptions != NULL);
//
// Insert the key option in order
//
for (Index = 0; Index < Param->KeyOptionCount; Index++) {
if (KeyOption->OptionNumber < Param->KeyOptions[Index].OptionNumber) {
break;
}
}
CopyMem (&Param->KeyOptions[Index + 1], &Param->KeyOptions[Index], (Param->KeyOptionCount - Index) * sizeof (EFI_BOOT_MANAGER_KEY_OPTION));
CopyMem (&Param->KeyOptions[Index], KeyOption, BmSizeOfKeyOption (KeyOption));
Param->KeyOptionCount++;
}
FreePool (KeyOption);
}
}
/**
Return the array of key options.
@param Count Return the number of key options.
@retval NULL No key option.
@retval Other Pointer to the key options.
**/
EFI_BOOT_MANAGER_KEY_OPTION *
BmGetKeyOptions (
OUT UINTN *Count
)
{
BM_COLLECT_KEY_OPTIONS_PARAM Param;
if (Count == NULL) {
return NULL;
}
Param.KeyOptions = NULL;
Param.KeyOptionCount = 0;
BmForEachVariable (BmCollectKeyOptions, (VOID *) &Param);
*Count = Param.KeyOptionCount;
return Param.KeyOptions;
}
/**
Callback function for event.
@param Event Event for this callback function.
@param Context Context pass to this function.
**/
VOID
EFIAPI
BmEmptyFunction (
IN EFI_EVENT Event,
IN VOID *Context
)
{
}
/**
Check whether the bit is set in the value.
@param Value The value need to be check.
@param Bit The bit filed need to be check.
@retval TRUE The bit is set.
@retval FALSE The bit is not set.
**/
BOOLEAN
BmBitSet (
IN UINT32 Value,
IN UINT32 Bit
)
{
return (BOOLEAN) ((Value & Bit) != 0);
}
/**
Initialize the KeyData and Key[] in the EFI_BOOT_MANAGER_KEY_OPTION.
@param Modifier Input key info.
@param Args Va_list info.
@param KeyOption Key info which need to update.
@retval EFI_SUCCESS Succeed to initialize the KeyData and Key[].
@return EFI_INVALID_PARAMETER Input parameter error.
**/
EFI_STATUS
BmInitializeKeyFields (
IN UINT32 Modifier,
IN VA_LIST Args,
OUT EFI_BOOT_MANAGER_KEY_OPTION *KeyOption
)
{
EFI_INPUT_KEY *Key;
if (KeyOption == NULL) {
return EFI_INVALID_PARAMETER;
}
Key = NULL;
while (KeyOption->KeyData.Options.InputKeyCount < sizeof (KeyOption->Keys) / sizeof (KeyOption->Keys[0])) {
Key = VA_ARG (Args, EFI_INPUT_KEY *);
if (Key == NULL) {
break;
}
CopyMem (
&KeyOption->Keys[KeyOption->KeyData.Options.InputKeyCount],
Key,
sizeof (EFI_INPUT_KEY)
);
KeyOption->KeyData.Options.InputKeyCount++;
}
if (Key != NULL) {
//
// Too many keys
//
return EFI_INVALID_PARAMETER;
}
if ((Modifier & ~(EFI_BOOT_MANAGER_SHIFT_PRESSED
| EFI_BOOT_MANAGER_CONTROL_PRESSED
| EFI_BOOT_MANAGER_ALT_PRESSED
| EFI_BOOT_MANAGER_LOGO_PRESSED
| EFI_BOOT_MANAGER_MENU_KEY_PRESSED
| EFI_BOOT_MANAGER_SYS_REQ_PRESSED
)) != 0) {
return EFI_INVALID_PARAMETER;
}
if (BmBitSet (Modifier, EFI_BOOT_MANAGER_SHIFT_PRESSED)) {
KeyOption->KeyData.Options.ShiftPressed = 1;
}
if (BmBitSet (Modifier, EFI_BOOT_MANAGER_CONTROL_PRESSED)) {
KeyOption->KeyData.Options.ControlPressed = 1;
}
if (BmBitSet (Modifier, EFI_BOOT_MANAGER_ALT_PRESSED)) {
KeyOption->KeyData.Options.AltPressed = 1;
}
if (BmBitSet (Modifier, EFI_BOOT_MANAGER_LOGO_PRESSED)) {
KeyOption->KeyData.Options.LogoPressed = 1;
}
if (BmBitSet (Modifier, EFI_BOOT_MANAGER_MENU_KEY_PRESSED)) {
KeyOption->KeyData.Options.MenuPressed = 1;
}
if (BmBitSet (Modifier, EFI_BOOT_MANAGER_SYS_REQ_PRESSED)) {
KeyOption->KeyData.Options.SysReqPressed = 1;
}
return EFI_SUCCESS;
}
/**
Try to boot the boot option triggered by hot key.
**/
VOID
EFIAPI
EfiBootManagerHotkeyBoot (
VOID
)
{
if (mBmHotkeyBootOption.OptionNumber != LoadOptionNumberUnassigned) {
EfiBootManagerBoot (&mBmHotkeyBootOption);
EfiBootManagerFreeLoadOption (&mBmHotkeyBootOption);
mBmHotkeyBootOption.OptionNumber = LoadOptionNumberUnassigned;
}
}
/**
This is the common notification function for HotKeys, it will be registered
with SimpleTextInEx protocol interface - RegisterKeyNotify() of ConIn handle.
@param KeyData A pointer to a buffer that is filled in with the keystroke
information for the key that was pressed.
@retval EFI_SUCCESS KeyData is successfully processed.
@return EFI_NOT_FOUND Fail to find boot option variable.
**/
EFI_STATUS
EFIAPI
BmHotkeyCallback (
IN EFI_KEY_DATA *KeyData
)
{
LIST_ENTRY *Link;
BM_HOTKEY *Hotkey;
CHAR16 OptionName[BM_OPTION_NAME_LEN];
EFI_STATUS Status;
EFI_KEY_DATA *HotkeyData;
if (mBmHotkeyBootOption.OptionNumber != LoadOptionNumberUnassigned) {
//
// Do not process sequential hotkey stroke until the current boot option returns
//
return EFI_SUCCESS;
}
DEBUG ((EFI_D_INFO, "[Bds]BmHotkeyCallback: %04x:%04x\n", KeyData->Key.ScanCode, KeyData->Key.UnicodeChar));
EfiAcquireLock (&mBmHotkeyLock);
for ( Link = GetFirstNode (&mBmHotkeyList)
; !IsNull (&mBmHotkeyList, Link)
; Link = GetNextNode (&mBmHotkeyList, Link)
) {
Hotkey = BM_HOTKEY_FROM_LINK (Link);
//
// Is this Key Stroke we are waiting for?
//
ASSERT (Hotkey->WaitingKey < (sizeof (Hotkey->KeyData) / sizeof (Hotkey->KeyData[0])));
HotkeyData = &Hotkey->KeyData[Hotkey->WaitingKey];
if ((KeyData->Key.ScanCode == HotkeyData->Key.ScanCode) &&
(KeyData->Key.UnicodeChar == HotkeyData->Key.UnicodeChar) &&
(((KeyData->KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) != 0) ?
(KeyData->KeyState.KeyShiftState == HotkeyData->KeyState.KeyShiftState) : TRUE
)
) {
//
// Receive an expecting key stroke, transit to next waiting state
//
Hotkey->WaitingKey++;
if (Hotkey->WaitingKey == Hotkey->CodeCount) {
//
// Reset to initial waiting state
//
Hotkey->WaitingKey = 0;
//
// Received the whole key stroke sequence
//
Status = gBS->SignalEvent (mBmHotkeyTriggered);
ASSERT_EFI_ERROR (Status);
if (!Hotkey->IsContinue) {
//
// Launch its BootOption
//
UnicodeSPrint (
OptionName, sizeof (OptionName), L"%s%04x",
mBmLoadOptionName[LoadOptionTypeBoot], Hotkey->BootOption
);
Status = EfiBootManagerVariableToLoadOption (OptionName, &mBmHotkeyBootOption);
DEBUG ((EFI_D_INFO, "[Bds]Hotkey for %s pressed - %r\n", OptionName, Status));
if (EFI_ERROR (Status)) {
mBmHotkeyBootOption.OptionNumber = LoadOptionNumberUnassigned;
}
} else {
DEBUG ((EFI_D_INFO, "[Bds]Continue key pressed!\n"));
}
}
} else {
//
// Receive an unexpected key stroke, reset to initial waiting state
//
Hotkey->WaitingKey = 0;
}
}
EfiReleaseLock (&mBmHotkeyLock);
return EFI_SUCCESS;
}
/**
Unregister hotkey notify list.
@param Hotkey Hotkey list.
@retval EFI_SUCCESS Unregister hotkey notify success.
@retval Others Unregister hotkey notify failed.
**/
EFI_STATUS
BmUnregisterHotkeyNotify (
IN BM_HOTKEY *Hotkey
)
{
EFI_STATUS Status;
UINTN Index;
UINTN KeyIndex;
EFI_HANDLE *Handles;
UINTN HandleCount;
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *TxtInEx;
VOID *NotifyHandle;
gBS->LocateHandleBuffer (
ByProtocol,
&gEfiSimpleTextInputExProtocolGuid,
NULL,
&HandleCount,
&Handles
);
for (Index = 0; Index < HandleCount; Index++) {
Status = gBS->HandleProtocol (Handles[Index], &gEfiSimpleTextInputExProtocolGuid, (VOID **) &TxtInEx);
ASSERT_EFI_ERROR (Status);
for (KeyIndex = 0; KeyIndex < Hotkey->CodeCount; KeyIndex++) {
Status = TxtInEx->RegisterKeyNotify (
TxtInEx,
&Hotkey->KeyData[KeyIndex],
BmHotkeyCallback,
&NotifyHandle
);
if (!EFI_ERROR (Status)) {
Status = TxtInEx->UnregisterKeyNotify (TxtInEx, NotifyHandle);
DEBUG ((EFI_D_INFO, "[Bds]UnregisterKeyNotify: %04x/%04x %r\n", Hotkey->KeyData[KeyIndex].Key.ScanCode, Hotkey->KeyData[KeyIndex].Key.UnicodeChar, Status));
}
}
}
return EFI_SUCCESS;
}
/**
Register hotkey notify list.
@param TxtInEx Pointer to EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL protocol.
@param Hotkey Hotkey list.
@retval EFI_SUCCESS Register hotkey notify success.
@retval Others Register hotkey notify failed.
**/
EFI_STATUS
BmRegisterHotkeyNotify (
IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *TxtInEx,
IN BM_HOTKEY *Hotkey
)
{
EFI_STATUS Status;
UINTN Index;
VOID *NotifyHandle;
for (Index = 0; Index < Hotkey->CodeCount; Index++) {
Status = TxtInEx->RegisterKeyNotify (
TxtInEx,
&Hotkey->KeyData[Index],
BmHotkeyCallback,
&NotifyHandle
);
DEBUG ((
EFI_D_INFO,
"[Bds]RegisterKeyNotify: %04x/%04x %08x/%02x %r\n",
Hotkey->KeyData[Index].Key.ScanCode,
Hotkey->KeyData[Index].Key.UnicodeChar,
Hotkey->KeyData[Index].KeyState.KeyShiftState,
Hotkey->KeyData[Index].KeyState.KeyToggleState,
Status
));
if (EFI_ERROR (Status)) {
//
// some of the hotkey registry failed
// do not unregister all in case we have both CTRL-ALT-P and CTRL-ALT-P-R
//
break;
}
}
return EFI_SUCCESS;
}
/**
Generate key shift state base on the input key option info.
@param Depth Which key is checked.
@param KeyOption Input key option info.
@param KeyShiftState Input key shift state.
@param KeyShiftStates Return possible key shift state array.
@param KeyShiftStateCount Possible key shift state count.
**/
VOID
BmGenerateKeyShiftState (
IN UINTN Depth,
IN EFI_BOOT_MANAGER_KEY_OPTION *KeyOption,
IN UINT32 KeyShiftState,
IN UINT32 *KeyShiftStates,
IN UINTN *KeyShiftStateCount
)
{
switch (Depth) {
case 0:
if (KeyOption->KeyData.Options.ShiftPressed) {
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState | EFI_RIGHT_SHIFT_PRESSED, KeyShiftStates, KeyShiftStateCount);
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState | EFI_LEFT_SHIFT_PRESSED, KeyShiftStates, KeyShiftStateCount);
} else {
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState, KeyShiftStates, KeyShiftStateCount);
}
break;
case 1:
if (KeyOption->KeyData.Options.ControlPressed) {
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState | EFI_RIGHT_CONTROL_PRESSED, KeyShiftStates, KeyShiftStateCount);
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState | EFI_LEFT_CONTROL_PRESSED, KeyShiftStates, KeyShiftStateCount);
} else {
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState, KeyShiftStates, KeyShiftStateCount);
}
break;
case 2:
if (KeyOption->KeyData.Options.AltPressed) {
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState | EFI_RIGHT_ALT_PRESSED, KeyShiftStates, KeyShiftStateCount);
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState | EFI_LEFT_ALT_PRESSED, KeyShiftStates, KeyShiftStateCount);
} else {
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState, KeyShiftStates, KeyShiftStateCount);
}
break;
case 3:
if (KeyOption->KeyData.Options.LogoPressed) {
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState | EFI_RIGHT_LOGO_PRESSED, KeyShiftStates, KeyShiftStateCount);
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState | EFI_LEFT_LOGO_PRESSED, KeyShiftStates, KeyShiftStateCount);
} else {
BmGenerateKeyShiftState (Depth + 1, KeyOption, KeyShiftState, KeyShiftStates, KeyShiftStateCount);
}
break;
case 4:
if (KeyOption->KeyData.Options.MenuPressed) {
KeyShiftState |= EFI_MENU_KEY_PRESSED;
}
if (KeyOption->KeyData.Options.SysReqPressed) {
KeyShiftState |= EFI_SYS_REQ_PRESSED;
}
KeyShiftStates[*KeyShiftStateCount] = KeyShiftState;
(*KeyShiftStateCount)++;
break;
}
}
/**
Add it to hot key database, register it to existing TxtInEx.
New TxtInEx will be automatically registered with all the hot key in dababase
@param KeyOption Input key option info.
**/
EFI_STATUS
BmProcessKeyOption (
IN EFI_BOOT_MANAGER_KEY_OPTION *KeyOption
)
{
EFI_STATUS Status;
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *TxtInEx;
EFI_HANDLE *Handles;
UINTN HandleCount;
UINTN HandleIndex;
UINTN Index;
BM_HOTKEY *Hotkey;
UINTN KeyIndex;
//
// 16 is enough to enumerate all the possible combination of LEFT_XXX and RIGHT_XXX
//
UINT32 KeyShiftStates[16];
UINTN KeyShiftStateCount;
if (KeyOption->KeyData.Options.InputKeyCount > mBmHotkeySupportCount) {
return EFI_UNSUPPORTED;
}
KeyShiftStateCount = 0;
BmGenerateKeyShiftState (0, KeyOption, EFI_SHIFT_STATE_VALID, KeyShiftStates, &KeyShiftStateCount);
ASSERT (KeyShiftStateCount <= sizeof (KeyShiftStates) / sizeof (KeyShiftStates[0]));
EfiAcquireLock (&mBmHotkeyLock);
for (Index = 0; Index < KeyShiftStateCount; Index++) {
Hotkey = AllocateZeroPool (sizeof (BM_HOTKEY));
ASSERT (Hotkey != NULL);
Hotkey->Signature = BM_HOTKEY_SIGNATURE;
Hotkey->BootOption = KeyOption->BootOption;
Hotkey->IsContinue = (BOOLEAN) (KeyOption == mBmContinueKeyOption);
Hotkey->CodeCount = (UINT8) KeyOption->KeyData.Options.InputKeyCount;
for (KeyIndex = 0; KeyIndex < Hotkey->CodeCount; KeyIndex++) {
CopyMem (&Hotkey->KeyData[KeyIndex].Key, &KeyOption->Keys[KeyIndex], sizeof (EFI_INPUT_KEY));
Hotkey->KeyData[KeyIndex].KeyState.KeyShiftState = KeyShiftStates[Index];
}
InsertTailList (&mBmHotkeyList, &Hotkey->Link);
gBS->LocateHandleBuffer (
ByProtocol,
&gEfiSimpleTextInputExProtocolGuid,
NULL,
&HandleCount,
&Handles
);
for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
Status = gBS->HandleProtocol (Handles[HandleIndex], &gEfiSimpleTextInputExProtocolGuid, (VOID **) &TxtInEx);
ASSERT_EFI_ERROR (Status);
BmRegisterHotkeyNotify (TxtInEx, Hotkey);
}
}
EfiReleaseLock (&mBmHotkeyLock);
return EFI_SUCCESS;
}
/**
Callback function for SimpleTextInEx protocol install events
@param Event the event that is signaled.
@param Context not used here.
**/
VOID
EFIAPI
BmTxtInExCallback (
IN EFI_EVENT Event,
IN VOID *Context
)
{
EFI_STATUS Status;
UINTN BufferSize;
EFI_HANDLE Handle;
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *TxtInEx;
LIST_ENTRY *Link;
while (TRUE) {
BufferSize = sizeof (EFI_HANDLE);
Status = gBS->LocateHandle (
ByRegisterNotify,
NULL,
mBmTxtInExRegistration,
&BufferSize,
&Handle
);
if (EFI_ERROR (Status)) {
//
// If no more notification events exist
//
return ;
}
Status = gBS->HandleProtocol (
Handle,
&gEfiSimpleTextInputExProtocolGuid,
(VOID **) &TxtInEx
);
ASSERT_EFI_ERROR (Status);
//
// Register the hot key notification for the existing items in the list
//
EfiAcquireLock (&mBmHotkeyLock);
for (Link = GetFirstNode (&mBmHotkeyList); !IsNull (&mBmHotkeyList, Link); Link = GetNextNode (&mBmHotkeyList, Link)) {
BmRegisterHotkeyNotify (TxtInEx, BM_HOTKEY_FROM_LINK (Link));
}
EfiReleaseLock (&mBmHotkeyLock);
}
}
/**
Free the key options returned from BmGetKeyOptions.
@param KeyOptions Pointer to the key options.
@param KeyOptionCount Number of the key options.
@retval EFI_SUCCESS The key options are freed.
@retval EFI_NOT_FOUND KeyOptions is NULL.
**/
EFI_STATUS
BmFreeKeyOptions (
IN EFI_BOOT_MANAGER_KEY_OPTION *KeyOptions,
IN UINTN KeyOptionCount
)
{
if (KeyOptions != NULL) {
FreePool (KeyOptions);
return EFI_SUCCESS;
} else {
return EFI_NOT_FOUND;
}
}
/**
Register the key option to exit the waiting of the Boot Manager timeout.
Platform should ensure that the continue key option isn't conflict with
other boot key options.
@param Modifier Key shift state.
@param ... Parameter list of pointer of EFI_INPUT_KEY.
@retval EFI_SUCCESS Successfully register the continue key option.
@retval EFI_ALREADY_STARTED The continue key option is already registered.
**/
EFI_STATUS
EFIAPI
EfiBootManagerRegisterContinueKeyOption (
IN UINT32 Modifier,
...
)
{
EFI_STATUS Status;
EFI_BOOT_MANAGER_KEY_OPTION KeyOption;
VA_LIST Args;
if (mBmContinueKeyOption != NULL) {
return EFI_ALREADY_STARTED;
}
ZeroMem (&KeyOption, sizeof (EFI_BOOT_MANAGER_KEY_OPTION));
VA_START (Args, Modifier);
Status = BmInitializeKeyFields (Modifier, Args, &KeyOption);
VA_END (Args);
if (!EFI_ERROR (Status)) {
mBmContinueKeyOption = AllocateCopyPool (sizeof (EFI_BOOT_MANAGER_KEY_OPTION), &KeyOption);
ASSERT (mBmContinueKeyOption != NULL);
if (mBmHotkeyServiceStarted) {
BmProcessKeyOption (mBmContinueKeyOption);
}
}
return Status;
}
/**
Stop the hotkey processing.
@param Event Event pointer related to hotkey service.
@param Context Context pass to this function.
**/
VOID
EFIAPI
BmStopHotkeyService (
IN EFI_EVENT Event,
IN VOID *Context
)
{
LIST_ENTRY *Link;
BM_HOTKEY *Hotkey;
DEBUG ((EFI_D_INFO, "[Bds]Stop Hotkey Service!\n"));
gBS->CloseEvent (Event);
EfiAcquireLock (&mBmHotkeyLock);
for (Link = GetFirstNode (&mBmHotkeyList); !IsNull (&mBmHotkeyList, Link); ) {
Hotkey = BM_HOTKEY_FROM_LINK (Link);
BmUnregisterHotkeyNotify (Hotkey);
Link = RemoveEntryList (Link);
FreePool (Hotkey);
}
EfiReleaseLock (&mBmHotkeyLock);
}
/**
Start the hot key service so that the key press can trigger the boot option.
@param HotkeyTriggered Return the waitable event and it will be signaled
when a valid hot key is pressed.
@retval EFI_SUCCESS The hot key service is started.
**/
EFI_STATUS
EFIAPI
EfiBootManagerStartHotkeyService (
IN EFI_EVENT *HotkeyTriggered
)
{
EFI_STATUS Status;
EFI_BOOT_MANAGER_KEY_OPTION *KeyOptions;
UINTN KeyOptionCount;
UINTN Index;
EFI_EVENT Event;
UINT32 *BootOptionSupport;
Status = GetEfiGlobalVariable2 (EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME, (VOID **) &BootOptionSupport, NULL);
ASSERT (BootOptionSupport != NULL);
if ((*BootOptionSupport & EFI_BOOT_OPTION_SUPPORT_KEY) != 0) {
mBmHotkeySupportCount = ((*BootOptionSupport & EFI_BOOT_OPTION_SUPPORT_COUNT) >> LowBitSet32 (EFI_BOOT_OPTION_SUPPORT_COUNT));
}
FreePool (BootOptionSupport);
if (mBmHotkeySupportCount == 0) {
DEBUG ((EFI_D_INFO, "Bds: BootOptionSupport NV variable forbids starting the hotkey service.\n"));
return EFI_UNSUPPORTED;
}
Status = gBS->CreateEvent (
EVT_NOTIFY_WAIT,
TPL_CALLBACK,
BmEmptyFunction,
NULL,
&mBmHotkeyTriggered
);
ASSERT_EFI_ERROR (Status);
if (HotkeyTriggered != NULL) {
*HotkeyTriggered = mBmHotkeyTriggered;
}
KeyOptions = BmGetKeyOptions (&KeyOptionCount);
for (Index = 0; Index < KeyOptionCount; Index ++) {
BmProcessKeyOption (&KeyOptions[Index]);
}
BmFreeKeyOptions (KeyOptions, KeyOptionCount);
if (mBmContinueKeyOption != NULL) {
BmProcessKeyOption (mBmContinueKeyOption);
}
EfiCreateProtocolNotifyEvent (
&gEfiSimpleTextInputExProtocolGuid,
TPL_CALLBACK,
BmTxtInExCallback,
NULL,
&mBmTxtInExRegistration
);
Status = EfiCreateEventReadyToBootEx (
TPL_CALLBACK,
BmStopHotkeyService,
NULL,
&Event
);
ASSERT_EFI_ERROR (Status);
mBmHotkeyServiceStarted = TRUE;
return Status;
}
/**
Add the key option.
It adds the key option variable and the key option takes affect immediately.
@param AddedOption Return the added key option.
@param BootOptionNumber The boot option number for the key option.
@param Modifier Key shift state.
@param ... Parameter list of pointer of EFI_INPUT_KEY.
@retval EFI_SUCCESS The key option is added.
@retval EFI_ALREADY_STARTED The hot key is already used by certain key option.
**/
EFI_STATUS
EFIAPI
EfiBootManagerAddKeyOptionVariable (
OUT EFI_BOOT_MANAGER_KEY_OPTION *AddedOption, OPTIONAL
IN UINT16 BootOptionNumber,
IN UINT32 Modifier,
...
)
{
EFI_STATUS Status;
VA_LIST Args;
VOID *BootOption;
UINTN BootOptionSize;
CHAR16 BootOptionName[BM_OPTION_NAME_LEN];
EFI_BOOT_MANAGER_KEY_OPTION KeyOption;
EFI_BOOT_MANAGER_KEY_OPTION *KeyOptions;
UINTN KeyOptionCount;
UINTN Index;
UINTN KeyOptionNumber;
CHAR16 KeyOptionName[sizeof ("Key####")];
UnicodeSPrint (
BootOptionName, sizeof (BootOptionName), L"%s%04x",
mBmLoadOptionName[LoadOptionTypeBoot], BootOptionNumber
);
GetEfiGlobalVariable2 (BootOptionName, &BootOption, &BootOptionSize);
if (BootOption == NULL) {
return EFI_NOT_FOUND;
}
ZeroMem (&KeyOption, sizeof (EFI_BOOT_MANAGER_KEY_OPTION));
KeyOption.BootOption = BootOptionNumber;
Status = gBS->CalculateCrc32 (BootOption, BootOptionSize, &KeyOption.BootOptionCrc);
ASSERT_EFI_ERROR (Status);
FreePool (BootOption);
VA_START (Args, Modifier);
Status = BmInitializeKeyFields (Modifier, Args, &KeyOption);
VA_END (Args);
if (EFI_ERROR (Status)) {
return Status;
}
KeyOptionNumber = LoadOptionNumberUnassigned;
//
// Check if the hot key sequence was defined already
//
KeyOptions = BmGetKeyOptions (&KeyOptionCount);
for (Index = 0; Index < KeyOptionCount; Index++) {
if ((KeyOptions[Index].KeyData.PackedValue == KeyOption.KeyData.PackedValue) &&
(CompareMem (KeyOptions[Index].Keys, KeyOption.Keys, KeyOption.KeyData.Options.InputKeyCount * sizeof (EFI_INPUT_KEY)) == 0)) {
break;
}
if ((KeyOptionNumber == LoadOptionNumberUnassigned) &&
(KeyOptions[Index].OptionNumber > Index)
){
KeyOptionNumber = Index;
}
}
BmFreeKeyOptions (KeyOptions, KeyOptionCount);
if (Index < KeyOptionCount) {
return EFI_ALREADY_STARTED;
}
if (KeyOptionNumber == LoadOptionNumberUnassigned) {
KeyOptionNumber = KeyOptionCount;
}
UnicodeSPrint (KeyOptionName, sizeof (KeyOptionName), L"Key%04x", KeyOptionNumber);
Status = gRT->SetVariable (
KeyOptionName,
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
BmSizeOfKeyOption (&KeyOption),
&KeyOption
);
if (!EFI_ERROR (Status)) {
//
// Return the Key Option in case needed by caller
//
if (AddedOption != NULL) {
CopyMem (AddedOption, &KeyOption, sizeof (EFI_BOOT_MANAGER_KEY_OPTION));
}
//
// Register the newly added hot key
// Calling this function before EfiBootManagerStartHotkeyService doesn't
// need to call BmProcessKeyOption
//
if (mBmHotkeyServiceStarted) {
BmProcessKeyOption (&KeyOption);
}
}
return Status;
}
/**
Delete the Key Option variable and unregister the hot key
@param DeletedOption Return the deleted key options.
@param Modifier Key shift state.
@param ... Parameter list of pointer of EFI_INPUT_KEY.
@retval EFI_SUCCESS The key option is deleted.
@retval EFI_NOT_FOUND The key option cannot be found.
**/
EFI_STATUS
EFIAPI
EfiBootManagerDeleteKeyOptionVariable (
IN EFI_BOOT_MANAGER_KEY_OPTION *DeletedOption, OPTIONAL
IN UINT32 Modifier,
...
)
{
EFI_STATUS Status;
UINTN Index;
VA_LIST Args;
EFI_BOOT_MANAGER_KEY_OPTION KeyOption;
EFI_BOOT_MANAGER_KEY_OPTION *KeyOptions;
UINTN KeyOptionCount;
LIST_ENTRY *Link;
BM_HOTKEY *Hotkey;
UINT32 ShiftState;
BOOLEAN Match;
CHAR16 KeyOptionName[sizeof ("Key####")];
ZeroMem (&KeyOption, sizeof (EFI_BOOT_MANAGER_KEY_OPTION));
VA_START (Args, Modifier);
Status = BmInitializeKeyFields (Modifier, Args, &KeyOption);
VA_END (Args);
if (EFI_ERROR (Status)) {
return Status;
}
EfiAcquireLock (&mBmHotkeyLock);
//
// Delete the key option from active hot key list
// Could have multiple entries when modifier isn't 0 because we map the ShiftPressed to RIGHT_SHIFT and RIGHT_SHIFT
//
for (Link = GetFirstNode (&mBmHotkeyList); !IsNull (&mBmHotkeyList, Link); ) {
Hotkey = BM_HOTKEY_FROM_LINK (Link);
Match = (BOOLEAN) (Hotkey->CodeCount == KeyOption.KeyData.Options.InputKeyCount);
for (Index = 0; Match && (Index < Hotkey->CodeCount); Index++) {
ShiftState = Hotkey->KeyData[Index].KeyState.KeyShiftState;
if (
(BmBitSet (ShiftState, EFI_RIGHT_SHIFT_PRESSED | EFI_LEFT_SHIFT_PRESSED) != KeyOption.KeyData.Options.ShiftPressed) ||
(BmBitSet (ShiftState, EFI_RIGHT_CONTROL_PRESSED | EFI_LEFT_CONTROL_PRESSED) != KeyOption.KeyData.Options.ControlPressed) ||
(BmBitSet (ShiftState, EFI_RIGHT_ALT_PRESSED | EFI_LEFT_ALT_PRESSED) != KeyOption.KeyData.Options.AltPressed) ||
(BmBitSet (ShiftState, EFI_RIGHT_LOGO_PRESSED | EFI_LEFT_LOGO_PRESSED) != KeyOption.KeyData.Options.LogoPressed) ||
(BmBitSet (ShiftState, EFI_MENU_KEY_PRESSED) != KeyOption.KeyData.Options.MenuPressed) ||
(BmBitSet (ShiftState, EFI_SYS_REQ_PRESSED) != KeyOption.KeyData.Options.SysReqPressed) ||
(CompareMem (&Hotkey->KeyData[Index].Key, &KeyOption.Keys[Index], sizeof (EFI_INPUT_KEY)) != 0)
) {
//
// Break when any field doesn't match
//
Match = FALSE;
break;
}
}
if (Match) {
Link = RemoveEntryList (Link);
FreePool (Hotkey);
} else {
Link = GetNextNode (&mBmHotkeyList, Link);
}
}
//
// Delete the key option from the variable
//
Status = EFI_NOT_FOUND;
KeyOptions = BmGetKeyOptions (&KeyOptionCount);
for (Index = 0; Index < KeyOptionCount; Index++) {
if ((KeyOptions[Index].KeyData.PackedValue == KeyOption.KeyData.PackedValue) &&
(CompareMem (
KeyOptions[Index].Keys, KeyOption.Keys,
KeyOption.KeyData.Options.InputKeyCount * sizeof (EFI_INPUT_KEY)) == 0)
) {
UnicodeSPrint (KeyOptionName, sizeof (KeyOptionName), L"Key%04x", KeyOptions[Index].OptionNumber);
Status = gRT->SetVariable (
KeyOptionName,
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
0,
NULL
);
//
// Return the deleted key option in case needed by caller
//
if (DeletedOption != NULL) {
CopyMem (DeletedOption, &KeyOptions[Index], sizeof (EFI_BOOT_MANAGER_KEY_OPTION));
}
break;
}
}
BmFreeKeyOptions (KeyOptions, KeyOptionCount);
EfiReleaseLock (&mBmHotkeyLock);
return Status;
}
|