2 Implementation for EFI_SIMPLE_TEXT_INPUT_PROTOCOL protocol.
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Reads the next keystroke from the input device. The WaitForKey Event can
20 be used to test for existence of a keystroke via WaitForEvent () call.
22 @param TerminalDevice Terminal driver private structure
23 @param KeyData A pointer to a buffer that is filled in with the
24 keystroke state data for the key that was
27 @retval EFI_SUCCESS The keystroke information was returned.
28 @retval EFI_NOT_READY There was no keystroke data available.
29 @retval EFI_INVALID_PARAMETER KeyData is NULL.
34 IN TERMINAL_DEV
*TerminalDevice
,
35 OUT EFI_KEY_DATA
*KeyData
38 if (KeyData
== NULL
) {
39 return EFI_INVALID_PARAMETER
;
42 if (!EfiKeyFiFoRemoveOneKey (TerminalDevice
, &KeyData
->Key
)) {
46 KeyData
->KeyState
.KeyShiftState
= 0;
47 KeyData
->KeyState
.KeyToggleState
= 0;
55 Implements EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset().
56 This driver only perform dependent serial device reset regardless of
57 the value of ExtendeVerification
59 @param This Indicates the calling context.
60 @param ExtendedVerification Skip by this driver.
62 @retval EFI_SUCCESS The reset operation succeeds.
63 @retval EFI_DEVICE_ERROR The dependent serial port reset fails.
69 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
70 IN BOOLEAN ExtendedVerification
74 TERMINAL_DEV
*TerminalDevice
;
76 TerminalDevice
= TERMINAL_CON_IN_DEV_FROM_THIS (This
);
79 // Report progress code here
81 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
83 (EFI_PERIPHERAL_REMOTE_CONSOLE
| EFI_P_PC_RESET
),
84 TerminalDevice
->DevicePath
87 Status
= TerminalDevice
->SerialIo
->Reset (TerminalDevice
->SerialIo
);
90 // Make all the internal buffer empty for keys
92 TerminalDevice
->RawFiFo
->Head
= TerminalDevice
->RawFiFo
->Tail
;
93 TerminalDevice
->UnicodeFiFo
->Head
= TerminalDevice
->UnicodeFiFo
->Tail
;
94 TerminalDevice
->EfiKeyFiFo
->Head
= TerminalDevice
->EfiKeyFiFo
->Tail
;
96 if (EFI_ERROR (Status
)) {
97 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
98 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
99 (EFI_PERIPHERAL_REMOTE_CONSOLE
| EFI_P_EC_CONTROLLER_ERROR
),
100 TerminalDevice
->DevicePath
108 Implements EFI_SIMPLE_TEXT_INPUT_PROTOCOL.ReadKeyStroke().
110 @param This Indicates the calling context.
111 @param Key A pointer to a buffer that is filled in with the
112 keystroke information for the key that was sent
115 @retval EFI_SUCCESS The keystroke information is returned successfully.
116 @retval EFI_NOT_READY There is no keystroke data available.
117 @retval EFI_DEVICE_ERROR The dependent serial device encounters error.
122 TerminalConInReadKeyStroke (
123 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
124 OUT EFI_INPUT_KEY
*Key
127 TERMINAL_DEV
*TerminalDevice
;
129 EFI_KEY_DATA KeyData
;
132 // get TERMINAL_DEV from "This" parameter.
134 TerminalDevice
= TERMINAL_CON_IN_DEV_FROM_THIS (This
);
136 Status
= ReadKeyStrokeWorker (TerminalDevice
, &KeyData
);
137 if (EFI_ERROR (Status
)) {
141 CopyMem (Key
, &KeyData
.Key
, sizeof (EFI_INPUT_KEY
));
148 Check if the key already has been registered.
150 If both RegsiteredData and InputData is NULL, then ASSERT().
152 @param RegsiteredData A pointer to a buffer that is filled in with the
153 keystroke state data for the key that was
155 @param InputData A pointer to a buffer that is filled in with the
156 keystroke state data for the key that was
159 @retval TRUE Key be pressed matches a registered key.
160 @retval FLASE Match failed.
165 IN EFI_KEY_DATA
*RegsiteredData
,
166 IN EFI_KEY_DATA
*InputData
169 ASSERT (RegsiteredData
!= NULL
&& InputData
!= NULL
);
171 if ((RegsiteredData
->Key
.ScanCode
!= InputData
->Key
.ScanCode
) ||
172 (RegsiteredData
->Key
.UnicodeChar
!= InputData
->Key
.UnicodeChar
)) {
182 Event notification function for EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
183 Signal the event if there is key available
185 @param Event Indicates the event that invoke this function.
186 @param Context Indicates the calling context.
191 TerminalConInWaitForKeyEx (
196 TerminalConInWaitForKey (Event
, Context
);
200 // Simple Text Input Ex protocol functions
204 Reset the input device and optionally run diagnostics
206 @param This Protocol instance pointer.
207 @param ExtendedVerification Driver may perform diagnostics on reset.
209 @retval EFI_SUCCESS The device was reset.
210 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
216 TerminalConInResetEx (
217 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
218 IN BOOLEAN ExtendedVerification
222 TERMINAL_DEV
*TerminalDevice
;
224 TerminalDevice
= TERMINAL_CON_IN_EX_DEV_FROM_THIS (This
);
226 Status
= TerminalDevice
->SimpleInput
.Reset (&TerminalDevice
->SimpleInput
, ExtendedVerification
);
227 if (EFI_ERROR (Status
)) {
228 return EFI_DEVICE_ERROR
;
237 Reads the next keystroke from the input device. The WaitForKey Event can
238 be used to test for existence of a keystroke via WaitForEvent () call.
240 @param This Protocol instance pointer.
241 @param KeyData A pointer to a buffer that is filled in with the
242 keystroke state data for the key that was
245 @retval EFI_SUCCESS The keystroke information was returned.
246 @retval EFI_NOT_READY There was no keystroke data available.
247 @retval EFI_DEVICE_ERROR The keystroke information was not returned due
249 @retval EFI_INVALID_PARAMETER KeyData is NULL.
254 TerminalConInReadKeyStrokeEx (
255 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
256 OUT EFI_KEY_DATA
*KeyData
259 TERMINAL_DEV
*TerminalDevice
;
261 if (KeyData
== NULL
) {
262 return EFI_INVALID_PARAMETER
;
265 TerminalDevice
= TERMINAL_CON_IN_EX_DEV_FROM_THIS (This
);
267 return ReadKeyStrokeWorker (TerminalDevice
, KeyData
);
273 Set certain state for the input device.
275 @param This Protocol instance pointer.
276 @param KeyToggleState A pointer to the EFI_KEY_TOGGLE_STATE to set the
277 state for the input device.
279 @retval EFI_SUCCESS The device state was set successfully.
280 @retval EFI_DEVICE_ERROR The device is not functioning correctly and
281 could not have the setting adjusted.
282 @retval EFI_UNSUPPORTED The device does not have the ability to set its
284 @retval EFI_INVALID_PARAMETER KeyToggleState is NULL.
289 TerminalConInSetState (
290 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
291 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
294 if (KeyToggleState
== NULL
) {
295 return EFI_INVALID_PARAMETER
;
303 Register a notification function for a particular keystroke for the input device.
305 @param This Protocol instance pointer.
306 @param KeyData A pointer to a buffer that is filled in with the
307 keystroke information data for the key that was
309 @param KeyNotificationFunction Points to the function to be called when the key
310 sequence is typed specified by KeyData.
311 @param NotifyHandle Points to the unique handle assigned to the
312 registered notification.
314 @retval EFI_SUCCESS The notification function was registered
316 @retval EFI_OUT_OF_RESOURCES Unable to allocate resources for necessary data
318 @retval EFI_INVALID_PARAMETER KeyData or NotifyHandle is NULL.
323 TerminalConInRegisterKeyNotify (
324 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
325 IN EFI_KEY_DATA
*KeyData
,
326 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
327 OUT EFI_HANDLE
*NotifyHandle
330 TERMINAL_DEV
*TerminalDevice
;
331 TERMINAL_CONSOLE_IN_EX_NOTIFY
*NewNotify
;
333 LIST_ENTRY
*NotifyList
;
334 TERMINAL_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
336 if (KeyData
== NULL
|| NotifyHandle
== NULL
|| KeyNotificationFunction
== NULL
) {
337 return EFI_INVALID_PARAMETER
;
340 TerminalDevice
= TERMINAL_CON_IN_EX_DEV_FROM_THIS (This
);
343 // Return EFI_SUCCESS if the (KeyData, NotificationFunction) is already registered.
345 NotifyList
= &TerminalDevice
->NotifyList
;
346 for (Link
= GetFirstNode (NotifyList
); !IsNull (NotifyList
,Link
); Link
= GetNextNode (NotifyList
,Link
)) {
349 TERMINAL_CONSOLE_IN_EX_NOTIFY
,
351 TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE
353 if (IsKeyRegistered (&CurrentNotify
->KeyData
, KeyData
)) {
354 if (CurrentNotify
->KeyNotificationFn
== KeyNotificationFunction
) {
355 *NotifyHandle
= CurrentNotify
->NotifyHandle
;
362 // Allocate resource to save the notification function
364 NewNotify
= (TERMINAL_CONSOLE_IN_EX_NOTIFY
*) AllocateZeroPool (sizeof (TERMINAL_CONSOLE_IN_EX_NOTIFY
));
365 if (NewNotify
== NULL
) {
366 return EFI_OUT_OF_RESOURCES
;
369 NewNotify
->Signature
= TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE
;
370 NewNotify
->KeyNotificationFn
= KeyNotificationFunction
;
371 NewNotify
->NotifyHandle
= (EFI_HANDLE
) NewNotify
;
372 CopyMem (&NewNotify
->KeyData
, KeyData
, sizeof (KeyData
));
373 InsertTailList (&TerminalDevice
->NotifyList
, &NewNotify
->NotifyEntry
);
375 *NotifyHandle
= NewNotify
->NotifyHandle
;
382 Remove a registered notification function from a particular keystroke.
384 @param This Protocol instance pointer.
385 @param NotificationHandle The handle of the notification function being
388 @retval EFI_SUCCESS The notification function was unregistered
390 @retval EFI_INVALID_PARAMETER The NotificationHandle is invalid.
395 TerminalConInUnregisterKeyNotify (
396 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
397 IN EFI_HANDLE NotificationHandle
400 TERMINAL_DEV
*TerminalDevice
;
402 TERMINAL_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
403 LIST_ENTRY
*NotifyList
;
405 if (NotificationHandle
== NULL
) {
406 return EFI_INVALID_PARAMETER
;
409 TerminalDevice
= TERMINAL_CON_IN_EX_DEV_FROM_THIS (This
);
411 NotifyList
= &TerminalDevice
->NotifyList
;
412 for (Link
= GetFirstNode (NotifyList
); !IsNull (NotifyList
,Link
); Link
= GetNextNode (NotifyList
,Link
)) {
415 TERMINAL_CONSOLE_IN_EX_NOTIFY
,
417 TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE
419 if (CurrentNotify
->NotifyHandle
== NotificationHandle
) {
421 // Remove the notification function from NotifyList and free resources
423 RemoveEntryList (&CurrentNotify
->NotifyEntry
);
425 gBS
->FreePool (CurrentNotify
);
431 // Can not find the matching entry in database.
433 return EFI_INVALID_PARAMETER
;
437 Translate raw data into Unicode (according to different encode), and
438 translate Unicode into key information. (according to different standard).
440 @param TerminalDevice Terminal driver private structure.
444 TranslateRawDataToEfiKey (
445 IN TERMINAL_DEV
*TerminalDevice
448 switch (TerminalDevice
->TerminalType
) {
453 AnsiRawDataToUnicode (TerminalDevice
);
454 UnicodeToEfiKey (TerminalDevice
);
459 // Process all the raw data in the RawFIFO,
460 // put the processed key into UnicodeFIFO.
462 VTUTF8RawDataToUnicode (TerminalDevice
);
465 // Translate all the Unicode data in the UnicodeFIFO to Efi key,
466 // then put into EfiKeyFIFO.
468 UnicodeToEfiKey (TerminalDevice
);
475 Event notification function for EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey event
476 Signal the event if there is key available
478 @param Event Indicates the event that invoke this function.
479 @param Context Indicates the calling context.
484 TerminalConInWaitForKey (
490 // Someone is waiting on the keystroke event, if there's
491 // a key pending, signal the event
493 if (!IsEfiKeyFiFoEmpty ((TERMINAL_DEV
*) Context
)) {
495 gBS
->SignalEvent (Event
);
500 Timer handler to poll the key from serial.
502 @param Event Indicates the event that invoke this function.
503 @param Context Indicates the calling context.
507 TerminalConInTimerHandler (
513 TERMINAL_DEV
*TerminalDevice
;
516 EFI_SERIAL_IO_MODE
*Mode
;
517 EFI_SERIAL_IO_PROTOCOL
*SerialIo
;
518 UINTN SerialInTimeOut
;
520 TerminalDevice
= (TERMINAL_DEV
*) Context
;
522 SerialIo
= TerminalDevice
->SerialIo
;
523 if (SerialIo
== NULL
) {
527 // if current timeout value for serial device is not identical with
528 // the value saved in TERMINAL_DEV structure, then recalculate the
529 // timeout value again and set serial attribute according to this value.
531 Mode
= SerialIo
->Mode
;
532 if (Mode
->Timeout
!= TerminalDevice
->SerialInTimeOut
) {
535 if (Mode
->BaudRate
!= 0) {
537 // According to BAUD rate to calculate the timeout value.
539 SerialInTimeOut
= (1 + Mode
->DataBits
+ Mode
->StopBits
) * 2 * 1000000 / (UINTN
) Mode
->BaudRate
;
542 Status
= SerialIo
->SetAttributes (
545 Mode
->ReceiveFifoDepth
,
546 (UINT32
) SerialInTimeOut
,
547 (EFI_PARITY_TYPE
) (Mode
->Parity
),
548 (UINT8
) Mode
->DataBits
,
549 (EFI_STOP_BITS_TYPE
) (Mode
->StopBits
)
552 if (EFI_ERROR (Status
)) {
553 TerminalDevice
->SerialInTimeOut
= 0;
555 TerminalDevice
->SerialInTimeOut
= SerialInTimeOut
;
559 // Check whether serial buffer is empty.
561 Status
= SerialIo
->GetControl (SerialIo
, &Control
);
563 if ((Control
& EFI_SERIAL_INPUT_BUFFER_EMPTY
) == 0) {
565 // Fetch all the keys in the serial buffer,
566 // and insert the byte stream into RawFIFO.
568 while (!IsRawFiFoFull (TerminalDevice
)) {
570 Status
= GetOneKeyFromSerial (TerminalDevice
->SerialIo
, &Input
);
572 if (EFI_ERROR (Status
)) {
573 if (Status
== EFI_DEVICE_ERROR
) {
574 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
575 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
576 (EFI_PERIPHERAL_REMOTE_CONSOLE
| EFI_P_EC_INPUT_ERROR
),
577 TerminalDevice
->DevicePath
583 RawFiFoInsertOneKey (TerminalDevice
, Input
);
588 // Translate all the raw data in RawFIFO into EFI Key,
589 // according to different terminal type supported.
591 TranslateRawDataToEfiKey (TerminalDevice
);
595 Get one key out of serial buffer.
597 @param SerialIo Serial I/O protocol attached to the serial device.
598 @param Output The fetched key.
600 @retval EFI_NOT_READY If serial buffer is empty.
601 @retval EFI_DEVICE_ERROR If reading serial buffer encounter error.
602 @retval EFI_SUCCESS If reading serial buffer successfully, put
603 the fetched key to the parameter output.
607 GetOneKeyFromSerial (
608 EFI_SERIAL_IO_PROTOCOL
*SerialIo
,
619 // Read one key from serial I/O device.
621 Status
= SerialIo
->Read (SerialIo
, &Size
, Output
);
623 if (EFI_ERROR (Status
)) {
625 if (Status
== EFI_TIMEOUT
) {
626 return EFI_NOT_READY
;
629 return EFI_DEVICE_ERROR
;
634 return EFI_NOT_READY
;
641 Insert one byte raw data into the Raw Data FIFO.
643 @param TerminalDevice Terminal driver private structure.
644 @param Input The key will be input.
646 @retval TRUE If insert successfully.
647 @retval FLASE If Raw Data buffer is full before key insertion,
652 RawFiFoInsertOneKey (
653 TERMINAL_DEV
*TerminalDevice
,
659 Tail
= TerminalDevice
->RawFiFo
->Tail
;
661 if (IsRawFiFoFull (TerminalDevice
)) {
668 TerminalDevice
->RawFiFo
->Data
[Tail
] = Input
;
670 TerminalDevice
->RawFiFo
->Tail
= (UINT8
) ((Tail
+ 1) % (RAW_FIFO_MAX_NUMBER
+ 1));
676 Remove one pre-fetched key out of the Raw Data FIFO.
678 @param TerminalDevice Terminal driver private structure.
679 @param Output The key will be removed.
681 @retval TRUE If insert successfully.
682 @retval FLASE If Raw Data FIFO buffer is empty before remove operation.
686 RawFiFoRemoveOneKey (
687 TERMINAL_DEV
*TerminalDevice
,
693 Head
= TerminalDevice
->RawFiFo
->Head
;
695 if (IsRawFiFoEmpty (TerminalDevice
)) {
703 *Output
= TerminalDevice
->RawFiFo
->Data
[Head
];
705 TerminalDevice
->RawFiFo
->Head
= (UINT8
) ((Head
+ 1) % (RAW_FIFO_MAX_NUMBER
+ 1));
711 Clarify whether Raw Data FIFO buffer is empty.
713 @param TerminalDevice Terminal driver private structure
715 @retval TRUE If Raw Data FIFO buffer is empty.
716 @retval FLASE If Raw Data FIFO buffer is not empty.
721 TERMINAL_DEV
*TerminalDevice
724 if (TerminalDevice
->RawFiFo
->Head
== TerminalDevice
->RawFiFo
->Tail
) {
732 Clarify whether Raw Data FIFO buffer is full.
734 @param TerminalDevice Terminal driver private structure
736 @retval TRUE If Raw Data FIFO buffer is full.
737 @retval FLASE If Raw Data FIFO buffer is not full.
742 TERMINAL_DEV
*TerminalDevice
748 Tail
= TerminalDevice
->RawFiFo
->Tail
;
749 Head
= TerminalDevice
->RawFiFo
->Head
;
751 if (((Tail
+ 1) % (RAW_FIFO_MAX_NUMBER
+ 1)) == Head
) {
760 Insert one pre-fetched key into the FIFO buffer.
762 @param TerminalDevice Terminal driver private structure.
763 @param Key The key will be input.
765 @retval TRUE If insert successfully.
766 @retval FLASE If FIFO buffer is full before key insertion,
771 EfiKeyFiFoInsertOneKey (
772 TERMINAL_DEV
*TerminalDevice
,
778 LIST_ENTRY
*NotifyList
;
779 TERMINAL_CONSOLE_IN_EX_NOTIFY
*CurrentNotify
;
780 EFI_KEY_DATA KeyData
;
782 Tail
= TerminalDevice
->EfiKeyFiFo
->Tail
;
784 CopyMem (&KeyData
.Key
, Key
, sizeof (EFI_INPUT_KEY
));
785 KeyData
.KeyState
.KeyShiftState
= 0;
786 KeyData
.KeyState
.KeyToggleState
= 0;
789 // Invoke notification functions if exist
791 NotifyList
= &TerminalDevice
->NotifyList
;
792 for (Link
= GetFirstNode (NotifyList
); !IsNull (NotifyList
,Link
); Link
= GetNextNode (NotifyList
,Link
)) {
795 TERMINAL_CONSOLE_IN_EX_NOTIFY
,
797 TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE
799 if (IsKeyRegistered (&CurrentNotify
->KeyData
, &KeyData
)) {
800 CurrentNotify
->KeyNotificationFn (&KeyData
);
803 if (IsEfiKeyFiFoFull (TerminalDevice
)) {
805 // Efi Key FIFO is full
810 CopyMem (&TerminalDevice
->EfiKeyFiFo
->Data
[Tail
], Key
, sizeof (EFI_INPUT_KEY
));
812 TerminalDevice
->EfiKeyFiFo
->Tail
= (UINT8
) ((Tail
+ 1) % (FIFO_MAX_NUMBER
+ 1));
818 Remove one pre-fetched key out of the FIFO buffer.
820 @param TerminalDevice Terminal driver private structure.
821 @param Output The key will be removed.
823 @retval TRUE If insert successfully.
824 @retval FLASE If FIFO buffer is empty before remove operation.
828 EfiKeyFiFoRemoveOneKey (
829 TERMINAL_DEV
*TerminalDevice
,
830 EFI_INPUT_KEY
*Output
835 Head
= TerminalDevice
->EfiKeyFiFo
->Head
;
836 ASSERT (Head
< FIFO_MAX_NUMBER
+ 1);
838 if (IsEfiKeyFiFoEmpty (TerminalDevice
)) {
842 Output
->ScanCode
= SCAN_NULL
;
843 Output
->UnicodeChar
= 0;
847 *Output
= TerminalDevice
->EfiKeyFiFo
->Data
[Head
];
849 TerminalDevice
->EfiKeyFiFo
->Head
= (UINT8
) ((Head
+ 1) % (FIFO_MAX_NUMBER
+ 1));
855 Clarify whether FIFO buffer is empty.
857 @param TerminalDevice Terminal driver private structure
859 @retval TRUE If FIFO buffer is empty.
860 @retval FLASE If FIFO buffer is not empty.
865 TERMINAL_DEV
*TerminalDevice
868 if (TerminalDevice
->EfiKeyFiFo
->Head
== TerminalDevice
->EfiKeyFiFo
->Tail
) {
876 Clarify whether FIFO buffer is full.
878 @param TerminalDevice Terminal driver private structure
880 @retval TRUE If FIFO buffer is full.
881 @retval FLASE If FIFO buffer is not full.
886 TERMINAL_DEV
*TerminalDevice
892 Tail
= TerminalDevice
->EfiKeyFiFo
->Tail
;
893 Head
= TerminalDevice
->EfiKeyFiFo
->Head
;
895 if (((Tail
+ 1) % (FIFO_MAX_NUMBER
+ 1)) == Head
) {
904 Insert one pre-fetched key into the Unicode FIFO buffer.
906 @param TerminalDevice Terminal driver private structure.
907 @param Input The key will be input.
909 @retval TRUE If insert successfully.
910 @retval FLASE If Unicode FIFO buffer is full before key insertion,
915 UnicodeFiFoInsertOneKey (
916 TERMINAL_DEV
*TerminalDevice
,
922 Tail
= TerminalDevice
->UnicodeFiFo
->Tail
;
923 ASSERT (Tail
< FIFO_MAX_NUMBER
+ 1);
926 if (IsUnicodeFiFoFull (TerminalDevice
)) {
928 // Unicode FIFO is full
933 TerminalDevice
->UnicodeFiFo
->Data
[Tail
] = Input
;
935 TerminalDevice
->UnicodeFiFo
->Tail
= (UINT8
) ((Tail
+ 1) % (FIFO_MAX_NUMBER
+ 1));
941 Remove one pre-fetched key out of the Unicode FIFO buffer.
943 @param TerminalDevice Terminal driver private structure.
944 @param Output The key will be removed.
946 @retval TRUE If insert successfully.
947 @retval FLASE If Unicode FIFO buffer is empty before remove operation.
951 UnicodeFiFoRemoveOneKey (
952 TERMINAL_DEV
*TerminalDevice
,
958 Head
= TerminalDevice
->UnicodeFiFo
->Head
;
959 ASSERT (Head
< FIFO_MAX_NUMBER
+ 1);
961 if (IsUnicodeFiFoEmpty (TerminalDevice
)) {
969 *Output
= TerminalDevice
->UnicodeFiFo
->Data
[Head
];
971 TerminalDevice
->UnicodeFiFo
->Head
= (UINT8
) ((Head
+ 1) % (FIFO_MAX_NUMBER
+ 1));
977 Clarify whether Unicode FIFO buffer is empty.
979 @param TerminalDevice Terminal driver private structure
981 @retval TRUE If Unicode FIFO buffer is empty.
982 @retval FLASE If Unicode FIFO buffer is not empty.
987 TERMINAL_DEV
*TerminalDevice
990 if (TerminalDevice
->UnicodeFiFo
->Head
== TerminalDevice
->UnicodeFiFo
->Tail
) {
998 Clarify whether Unicode FIFO buffer is full.
1000 @param TerminalDevice Terminal driver private structure
1002 @retval TRUE If Unicode FIFO buffer is full.
1003 @retval FLASE If Unicode FIFO buffer is not full.
1008 TERMINAL_DEV
*TerminalDevice
1014 Tail
= TerminalDevice
->UnicodeFiFo
->Tail
;
1015 Head
= TerminalDevice
->UnicodeFiFo
->Head
;
1017 if (((Tail
+ 1) % (FIFO_MAX_NUMBER
+ 1)) == Head
) {
1026 Count Unicode FIFO buffer.
1028 @param TerminalDevice Terminal driver private structure
1030 @return The count in bytes of Unicode FIFO.
1034 UnicodeFiFoGetKeyCount (
1035 TERMINAL_DEV
*TerminalDevice
1041 Tail
= TerminalDevice
->UnicodeFiFo
->Tail
;
1042 Head
= TerminalDevice
->UnicodeFiFo
->Head
;
1045 return (UINT8
) (Tail
- Head
);
1047 return (UINT8
) (Tail
+ FIFO_MAX_NUMBER
+ 1 - Head
);
1052 Update the Unicode characters from a terminal input device into EFI Keys FIFO.
1054 @param TerminalDevice The terminal device to use to translate raw input into EFI Keys
1058 UnicodeToEfiKeyFlushState (
1059 IN TERMINAL_DEV
*TerminalDevice
1065 InputState
= TerminalDevice
->InputState
;
1067 if (IsEfiKeyFiFoFull (TerminalDevice
)) {
1071 if ((InputState
& INPUT_STATE_ESC
) != 0) {
1072 Key
.ScanCode
= SCAN_ESC
;
1073 Key
.UnicodeChar
= 0;
1074 EfiKeyFiFoInsertOneKey (TerminalDevice
, &Key
);
1077 if ((InputState
& INPUT_STATE_CSI
) != 0) {
1078 Key
.ScanCode
= SCAN_NULL
;
1079 Key
.UnicodeChar
= CSI
;
1080 EfiKeyFiFoInsertOneKey (TerminalDevice
, &Key
);
1083 if ((InputState
& INPUT_STATE_LEFTOPENBRACKET
) != 0) {
1084 Key
.ScanCode
= SCAN_NULL
;
1085 Key
.UnicodeChar
= LEFTOPENBRACKET
;
1086 EfiKeyFiFoInsertOneKey (TerminalDevice
, &Key
);
1089 if ((InputState
& INPUT_STATE_O
) != 0) {
1090 Key
.ScanCode
= SCAN_NULL
;
1091 Key
.UnicodeChar
= 'O';
1092 EfiKeyFiFoInsertOneKey (TerminalDevice
, &Key
);
1095 if ((InputState
& INPUT_STATE_2
) != 0) {
1096 Key
.ScanCode
= SCAN_NULL
;
1097 Key
.UnicodeChar
= '2';
1098 EfiKeyFiFoInsertOneKey (TerminalDevice
, &Key
);
1102 // Cancel the timer.
1105 TerminalDevice
->TwoSecondTimeOut
,
1110 TerminalDevice
->InputState
= INPUT_STATE_DEFAULT
;
1115 Converts a stream of Unicode characters from a terminal input device into EFI Keys that
1116 can be read through the Simple Input Protocol.
1118 The table below shows the keyboard input mappings that this function supports.
1119 If the ESC sequence listed in one of the columns is presented, then it is translated
1120 into the corresponding EFI Scan Code. If a matching sequence is not found, then the raw
1121 key strokes are converted into EFI Keys.
1123 2 seconds are allowed for an ESC sequence to be completed. If the ESC sequence is not
1124 completed in 2 seconds, then the raw key strokes of the partial ESC sequence are
1125 converted into EFI Keys.
1126 There is one special input sequence that will force the system to reset.
1127 This is ESC R ESC r ESC R.
1129 Note: current implementation support terminal types include: PC ANSI, VT100+/VTUTF8, VT100.
1130 The table below is not same with UEFI Spec 2.3 Appendix B Table 201(not support ANSI X3.64 /
1131 DEC VT200-500 and extra support PC ANSI, VT100)since UEFI Table 201 is just an example.
1133 Symbols used in table below
1134 ===========================
1140 +=========+======+===========+==========+==========+
1141 | | EFI | UEFI 2.0 | | |
1142 | | Scan | | VT100+ | |
1143 | KEY | Code | PC ANSI | VTUTF8 | VT100 |
1144 +=========+======+===========+==========+==========+
1145 | NULL | 0x00 | | | |
1146 | UP | 0x01 | ESC [ A | ESC [ A | ESC [ A |
1147 | DOWN | 0x02 | ESC [ B | ESC [ B | ESC [ B |
1148 | RIGHT | 0x03 | ESC [ C | ESC [ C | ESC [ C |
1149 | LEFT | 0x04 | ESC [ D | ESC [ D | ESC [ D |
1150 | HOME | 0x05 | ESC [ H | ESC h | ESC [ H |
1151 | END | 0x06 | ESC [ F | ESC k | ESC [ K |
1152 | INSERT | 0x07 | ESC [ @ | ESC + | ESC [ @ |
1153 | | | ESC [ L | | ESC [ L |
1154 | DELETE | 0x08 | ESC [ X | ESC - | ESC [ P |
1155 | PG UP | 0x09 | ESC [ I | ESC ? | ESC [ V |
1157 | PG DOWN | 0x0A | ESC [ G | ESC / | ESC [ U |
1159 | F1 | 0x0B | ESC [ M | ESC 1 | ESC O P |
1160 | F2 | 0x0C | ESC [ N | ESC 2 | ESC O Q |
1161 | F3 | 0x0D | ESC [ O | ESC 3 | ESC O w |
1162 | F4 | 0x0E | ESC [ P | ESC 4 | ESC O x |
1163 | F5 | 0x0F | ESC [ Q | ESC 5 | ESC O t |
1164 | F6 | 0x10 | ESC [ R | ESC 6 | ESC O u |
1165 | F7 | 0x11 | ESC [ S | ESC 7 | ESC O q |
1166 | F8 | 0x12 | ESC [ T | ESC 8 | ESC O r |
1167 | F9 | 0x13 | ESC [ U | ESC 9 | ESC O p |
1168 | F10 | 0x14 | ESC [ V | ESC 0 | ESC O M |
1169 | Escape | 0x17 | ESC | ESC | ESC |
1170 | F11 | 0x15 | | ESC ! | |
1171 | F12 | 0x16 | | ESC @ | |
1172 +=========+======+===========+==========+==========+
1176 ESC R ESC r ESC R = Reset System
1178 @param TerminalDevice The terminal device to use to translate raw input into EFI Keys
1183 IN TERMINAL_DEV
*TerminalDevice
1187 EFI_STATUS TimerStatus
;
1190 BOOLEAN SetDefaultResetState
;
1192 TimerStatus
= gBS
->CheckEvent (TerminalDevice
->TwoSecondTimeOut
);
1194 if (!EFI_ERROR (TimerStatus
)) {
1195 UnicodeToEfiKeyFlushState (TerminalDevice
);
1196 TerminalDevice
->ResetState
= RESET_STATE_DEFAULT
;
1199 while (!IsUnicodeFiFoEmpty (TerminalDevice
) && !IsEfiKeyFiFoFull (TerminalDevice
)) {
1201 if (TerminalDevice
->InputState
!= INPUT_STATE_DEFAULT
) {
1203 // Check to see if the 2 seconds timer has expired
1205 TimerStatus
= gBS
->CheckEvent (TerminalDevice
->TwoSecondTimeOut
);
1206 if (!EFI_ERROR (TimerStatus
)) {
1207 UnicodeToEfiKeyFlushState (TerminalDevice
);
1208 TerminalDevice
->ResetState
= RESET_STATE_DEFAULT
;
1213 // Fetch one Unicode character from the Unicode FIFO
1215 UnicodeFiFoRemoveOneKey (TerminalDevice
, &UnicodeChar
);
1217 SetDefaultResetState
= TRUE
;
1219 switch (TerminalDevice
->InputState
) {
1220 case INPUT_STATE_DEFAULT
:
1224 case INPUT_STATE_ESC
:
1226 if (UnicodeChar
== LEFTOPENBRACKET
) {
1227 TerminalDevice
->InputState
|= INPUT_STATE_LEFTOPENBRACKET
;
1228 TerminalDevice
->ResetState
= RESET_STATE_DEFAULT
;
1232 if (UnicodeChar
== 'O' && TerminalDevice
->TerminalType
== VT100TYPE
) {
1233 TerminalDevice
->InputState
|= INPUT_STATE_O
;
1234 TerminalDevice
->ResetState
= RESET_STATE_DEFAULT
;
1238 Key
.ScanCode
= SCAN_NULL
;
1240 if (TerminalDevice
->TerminalType
== VT100PLUSTYPE
||
1241 TerminalDevice
->TerminalType
== VTUTF8TYPE
) {
1242 switch (UnicodeChar
) {
1244 Key
.ScanCode
= SCAN_F1
;
1247 Key
.ScanCode
= SCAN_F2
;
1250 Key
.ScanCode
= SCAN_F3
;
1253 Key
.ScanCode
= SCAN_F4
;
1256 Key
.ScanCode
= SCAN_F5
;
1259 Key
.ScanCode
= SCAN_F6
;
1262 Key
.ScanCode
= SCAN_F7
;
1265 Key
.ScanCode
= SCAN_F8
;
1268 Key
.ScanCode
= SCAN_F9
;
1271 Key
.ScanCode
= SCAN_F10
;
1274 Key
.ScanCode
= SCAN_F11
;
1277 Key
.ScanCode
= SCAN_F12
;
1280 Key
.ScanCode
= SCAN_HOME
;
1283 Key
.ScanCode
= SCAN_END
;
1286 Key
.ScanCode
= SCAN_INSERT
;
1289 Key
.ScanCode
= SCAN_DELETE
;
1292 Key
.ScanCode
= SCAN_PAGE_DOWN
;
1295 Key
.ScanCode
= SCAN_PAGE_UP
;
1302 switch (UnicodeChar
) {
1304 if (TerminalDevice
->ResetState
== RESET_STATE_DEFAULT
) {
1305 TerminalDevice
->ResetState
= RESET_STATE_ESC_R
;
1306 SetDefaultResetState
= FALSE
;
1307 } else if (TerminalDevice
->ResetState
== RESET_STATE_ESC_R_ESC_R
) {
1308 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
1310 Key
.ScanCode
= SCAN_NULL
;
1313 if (TerminalDevice
->ResetState
== RESET_STATE_ESC_R
) {
1314 TerminalDevice
->ResetState
= RESET_STATE_ESC_R_ESC_R
;
1315 SetDefaultResetState
= FALSE
;
1317 Key
.ScanCode
= SCAN_NULL
;
1323 if (SetDefaultResetState
) {
1324 TerminalDevice
->ResetState
= RESET_STATE_DEFAULT
;
1327 if (Key
.ScanCode
!= SCAN_NULL
) {
1328 Key
.UnicodeChar
= 0;
1329 EfiKeyFiFoInsertOneKey (TerminalDevice
, &Key
);
1330 TerminalDevice
->InputState
= INPUT_STATE_DEFAULT
;
1331 UnicodeToEfiKeyFlushState (TerminalDevice
);
1335 UnicodeToEfiKeyFlushState (TerminalDevice
);
1339 case INPUT_STATE_ESC
| INPUT_STATE_O
:
1341 TerminalDevice
->ResetState
= RESET_STATE_DEFAULT
;
1343 Key
.ScanCode
= SCAN_NULL
;
1345 if (TerminalDevice
->TerminalType
== VT100TYPE
) {
1346 switch (UnicodeChar
) {
1348 Key
.ScanCode
= SCAN_F1
;
1351 Key
.ScanCode
= SCAN_F2
;
1354 Key
.ScanCode
= SCAN_F3
;
1357 Key
.ScanCode
= SCAN_F4
;
1360 Key
.ScanCode
= SCAN_F5
;
1363 Key
.ScanCode
= SCAN_F6
;
1366 Key
.ScanCode
= SCAN_F7
;
1369 Key
.ScanCode
= SCAN_F8
;
1372 Key
.ScanCode
= SCAN_F9
;
1375 Key
.ScanCode
= SCAN_F10
;
1382 if (Key
.ScanCode
!= SCAN_NULL
) {
1383 Key
.UnicodeChar
= 0;
1384 EfiKeyFiFoInsertOneKey (TerminalDevice
, &Key
);
1385 TerminalDevice
->InputState
= INPUT_STATE_DEFAULT
;
1386 UnicodeToEfiKeyFlushState (TerminalDevice
);
1390 UnicodeToEfiKeyFlushState (TerminalDevice
);
1394 case INPUT_STATE_ESC
| INPUT_STATE_LEFTOPENBRACKET
:
1396 TerminalDevice
->ResetState
= RESET_STATE_DEFAULT
;
1398 Key
.ScanCode
= SCAN_NULL
;
1400 if (TerminalDevice
->TerminalType
== PCANSITYPE
||
1401 TerminalDevice
->TerminalType
== VT100TYPE
||
1402 TerminalDevice
->TerminalType
== VT100PLUSTYPE
||
1403 TerminalDevice
->TerminalType
== VTUTF8TYPE
) {
1404 switch (UnicodeChar
) {
1406 Key
.ScanCode
= SCAN_UP
;
1409 Key
.ScanCode
= SCAN_DOWN
;
1412 Key
.ScanCode
= SCAN_RIGHT
;
1415 Key
.ScanCode
= SCAN_LEFT
;
1418 if (TerminalDevice
->TerminalType
== PCANSITYPE
||
1419 TerminalDevice
->TerminalType
== VT100TYPE
) {
1420 Key
.ScanCode
= SCAN_HOME
;
1424 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1425 Key
.ScanCode
= SCAN_END
;
1429 if (TerminalDevice
->TerminalType
== VT100TYPE
) {
1430 Key
.ScanCode
= SCAN_END
;
1435 if (TerminalDevice
->TerminalType
== PCANSITYPE
||
1436 TerminalDevice
->TerminalType
== VT100TYPE
) {
1437 Key
.ScanCode
= SCAN_INSERT
;
1441 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1442 Key
.ScanCode
= SCAN_DELETE
;
1446 if (TerminalDevice
->TerminalType
== VT100TYPE
) {
1447 Key
.ScanCode
= SCAN_DELETE
;
1448 } else if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1449 Key
.ScanCode
= SCAN_F4
;
1453 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1454 Key
.ScanCode
= SCAN_PAGE_UP
;
1458 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1459 Key
.ScanCode
= SCAN_F10
;
1462 if (TerminalDevice
->TerminalType
== VT100TYPE
) {
1463 Key
.ScanCode
= SCAN_PAGE_UP
;
1467 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1468 Key
.ScanCode
= SCAN_PAGE_DOWN
;
1472 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1473 Key
.ScanCode
= SCAN_F9
;
1476 if (TerminalDevice
->TerminalType
== VT100TYPE
) {
1477 Key
.ScanCode
= SCAN_PAGE_DOWN
;
1481 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1482 Key
.ScanCode
= SCAN_F1
;
1486 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1487 Key
.ScanCode
= SCAN_F2
;
1491 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1492 Key
.ScanCode
= SCAN_F3
;
1496 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1497 Key
.ScanCode
= SCAN_F5
;
1501 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1502 Key
.ScanCode
= SCAN_F6
;
1506 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1507 Key
.ScanCode
= SCAN_F7
;
1511 if (TerminalDevice
->TerminalType
== PCANSITYPE
) {
1512 Key
.ScanCode
= SCAN_F8
;
1520 if (Key
.ScanCode
!= SCAN_NULL
) {
1521 Key
.UnicodeChar
= 0;
1522 EfiKeyFiFoInsertOneKey (TerminalDevice
, &Key
);
1523 TerminalDevice
->InputState
= INPUT_STATE_DEFAULT
;
1524 UnicodeToEfiKeyFlushState (TerminalDevice
);
1528 UnicodeToEfiKeyFlushState (TerminalDevice
);
1535 // Invalid state. This should never happen.
1539 UnicodeToEfiKeyFlushState (TerminalDevice
);
1544 if (UnicodeChar
== ESC
) {
1545 TerminalDevice
->InputState
= INPUT_STATE_ESC
;
1548 if (UnicodeChar
== CSI
) {
1549 TerminalDevice
->InputState
= INPUT_STATE_CSI
;
1552 if (TerminalDevice
->InputState
!= INPUT_STATE_DEFAULT
) {
1553 Status
= gBS
->SetTimer(
1554 TerminalDevice
->TwoSecondTimeOut
,
1558 ASSERT_EFI_ERROR (Status
);
1562 if (SetDefaultResetState
) {
1563 TerminalDevice
->ResetState
= RESET_STATE_DEFAULT
;
1566 if (UnicodeChar
== DEL
) {
1567 Key
.ScanCode
= SCAN_DELETE
;
1568 Key
.UnicodeChar
= 0;
1570 Key
.ScanCode
= SCAN_NULL
;
1571 Key
.UnicodeChar
= UnicodeChar
;
1574 EfiKeyFiFoInsertOneKey (TerminalDevice
, &Key
);