3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 #include <Protocol/SimpleTextOut.h>
29 #include <Protocol/SerialIo.h>
30 #include <Guid/GlobalVariable.h>
31 #include <Protocol/DevicePath.h>
32 #include <Protocol/SimpleTextIn.h>
33 #include <Protocol/SimpleTextInEx.h>
34 #include <Guid/HotPlugDevice.h>
35 #include <Guid/PcAnsi.h>
36 #include <Library/DebugLib.h>
37 #include <Library/UefiDriverEntryPoint.h>
38 #include <Library/UefiLib.h>
39 #include <Library/ReportStatusCodeLib.h>
40 #include <Library/BaseMemoryLib.h>
41 #include <Library/MemoryAllocationLib.h>
42 #include <Library/UefiBootServicesTableLib.h>
43 #include <Library/UefiRuntimeServicesTableLib.h>
44 #include <Library/DevicePathLib.h>
45 #include <Library/PcdLib.h>
46 #include <Library/BaseLib.h>
49 #define RAW_FIFO_MAX_NUMBER 256
50 #define FIFO_MAX_NUMBER 128
55 UINT8 Data
[RAW_FIFO_MAX_NUMBER
+ 1];
61 UINT16 Data
[FIFO_MAX_NUMBER
+ 1];
67 EFI_INPUT_KEY Data
[FIFO_MAX_NUMBER
+ 1];
70 #define TERMINAL_DEV_SIGNATURE EFI_SIGNATURE_32 ('t', 'm', 'n', 'l')
72 #define TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE EFI_SIGNATURE_32 ('t', 'm', 'e', 'n')
74 typedef struct _TERMINAL_CONSOLE_IN_EX_NOTIFY
{
76 EFI_HANDLE NotifyHandle
;
78 EFI_KEY_NOTIFY_FUNCTION KeyNotificationFn
;
79 LIST_ENTRY NotifyEntry
;
80 } TERMINAL_CONSOLE_IN_EX_NOTIFY
;
85 EFI_SERIAL_IO_PROTOCOL
*SerialIo
;
86 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
87 EFI_SIMPLE_TEXT_INPUT_PROTOCOL SimpleInput
;
88 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL SimpleTextOutput
;
89 EFI_SIMPLE_TEXT_OUTPUT_MODE SimpleTextOutputMode
;
90 UINTN SerialInTimeOut
;
91 RAW_DATA_FIFO RawFiFo
;
92 UNICODE_FIFO UnicodeFiFo
;
93 EFI_KEY_FIFO EfiKeyFiFo
;
94 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
95 EFI_EVENT TwoSecondTimeOut
;
100 // Esc could not be output to the screen by user,
101 // but the terminal driver need to output it to
102 // the terminal emulation software to send control sequence.
103 // This boolean is used by the terminal driver only
104 // to indicate whether the Esc could be sent or not.
106 BOOLEAN OutputEscChar
;
107 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL SimpleInputEx
;
108 LIST_ENTRY NotifyList
;
111 #define INPUT_STATE_DEFAULT 0x00
112 #define INPUT_STATE_ESC 0x01
113 #define INPUT_STATE_CSI 0x02
114 #define INPUT_STATE_LEFTOPENBRACKET 0x04
115 #define INPUT_STATE_O 0x08
116 #define INPUT_STATE_2 0x10
118 #define RESET_STATE_DEFAULT 0x00
119 #define RESET_STATE_ESC_R 0x01
120 #define RESET_STATE_ESC_R_ESC_r 0x02
122 #define TERMINAL_CON_IN_DEV_FROM_THIS(a) CR (a, TERMINAL_DEV, SimpleInput, TERMINAL_DEV_SIGNATURE)
123 #define TERMINAL_CON_OUT_DEV_FROM_THIS(a) CR (a, TERMINAL_DEV, SimpleTextOutput, TERMINAL_DEV_SIGNATURE)
124 #define TERMINAL_CON_IN_EX_DEV_FROM_THIS(a) CR (a, TERMINAL_DEV, SimpleInputEx, TERMINAL_DEV_SIGNATURE)
134 #define VT100PlusType 2
137 #define LEFTOPENBRACKET 0x5b // '['
143 #define MODE0_COLUMN_COUNT 80
144 #define MODE0_ROW_COUNT 25
146 #define MODE1_COLUMN_COUNT 80
147 #define MODE1_ROW_COUNT 50
149 #define MODE2_COLUMN_COUNT 100
150 #define MODE2_ROW_COUNT 31
156 #define BRIGHT_CONTROL_OFFSET 2
157 #define FOREGROUND_CONTROL_OFFSET 6
158 #define BACKGROUND_CONTROL_OFFSET 11
160 #define COLUMN_OFFSET 5
171 extern EFI_DRIVER_BINDING_PROTOCOL gTerminalDriverBinding
;
172 extern EFI_COMPONENT_NAME_PROTOCOL gTerminalComponentName
;
173 extern EFI_COMPONENT_NAME2_PROTOCOL gTerminalComponentName2
;
175 extern EFI_GUID gSimpleTextInExNotifyGuid
;
182 IN EFI_HANDLE ImageHandle
,
183 IN EFI_SYSTEM_TABLE
*SystemTable
190 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
191 IN BOOLEAN ExtendedVerification
197 TerminalConInReadKeyStroke (
198 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
199 OUT EFI_INPUT_KEY
*Key
206 IN EFI_KEY_DATA
*RegsiteredData
,
207 IN EFI_KEY_DATA
*InputData
215 RegsiteredData - A pointer to a buffer that is filled in with the keystroke
216 state data for the key that was registered.
217 InputData - A pointer to a buffer that is filled in with the keystroke
218 state data for the key that was pressed.
221 TRUE - Key be pressed matches a registered key.
222 FLASE - Match failed.
229 TerminalConInWaitForKeyEx (
235 // Simple Text Input Ex protocol prototypes
240 TerminalConInResetEx (
241 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
242 IN BOOLEAN ExtendedVerification
247 Reset the input device and optionaly run diagnostics
250 This - Protocol instance pointer.
251 ExtendedVerification - Driver may perform diagnostics on reset.
254 EFI_SUCCESS - The device was reset.
255 EFI_DEVICE_ERROR - The device is not functioning properly and could
263 TerminalConInReadKeyStrokeEx (
264 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
265 OUT EFI_KEY_DATA
*KeyData
270 Reads the next keystroke from the input device. The WaitForKey Event can
271 be used to test for existance of a keystroke via WaitForEvent () call.
274 This - Protocol instance pointer.
275 KeyData - A pointer to a buffer that is filled in with the keystroke
276 state data for the key that was pressed.
279 EFI_SUCCESS - The keystroke information was returned.
280 EFI_NOT_READY - There was no keystroke data availiable.
281 EFI_DEVICE_ERROR - The keystroke information was not returned due to
283 EFI_INVALID_PARAMETER - KeyData is NULL.
290 TerminalConInSetState (
291 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
292 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
297 Set certain state for the input device.
300 This - Protocol instance pointer.
301 KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the
302 state for the input device.
305 EFI_SUCCESS - The device state was set successfully.
306 EFI_DEVICE_ERROR - The device is not functioning correctly and could
307 not have the setting adjusted.
308 EFI_UNSUPPORTED - The device does not have the ability to set its state.
309 EFI_INVALID_PARAMETER - KeyToggleState is NULL.
316 TerminalConInRegisterKeyNotify (
317 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
318 IN EFI_KEY_DATA
*KeyData
,
319 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
320 OUT EFI_HANDLE
*NotifyHandle
325 Register a notification function for a particular keystroke for the input device.
328 This - Protocol instance pointer.
329 KeyData - A pointer to a buffer that is filled in with the keystroke
330 information data for the key that was pressed.
331 KeyNotificationFunction - Points to the function to be called when the key
332 sequence is typed specified by KeyData.
333 NotifyHandle - Points to the unique handle assigned to the registered notification.
336 EFI_SUCCESS - The notification function was registered successfully.
337 EFI_OUT_OF_RESOURCES - Unable to allocate resources for necesssary data structures.
338 EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL.
345 TerminalConInUnregisterKeyNotify (
346 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
347 IN EFI_HANDLE NotificationHandle
352 Remove a registered notification function from a particular keystroke.
355 This - Protocol instance pointer.
356 NotificationHandle - The handle of the notification function being unregistered.
359 EFI_SUCCESS - The notification function was unregistered successfully.
360 EFI_INVALID_PARAMETER - The NotificationHandle is invalid.
361 EFI_NOT_FOUND - Can not find the matching entry in database.
368 TerminalConInWaitForKey (
376 TerminalConOutReset (
377 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
378 IN BOOLEAN ExtendedVerification
384 TerminalConOutOutputString (
385 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
392 TerminalConOutTestString (
393 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
400 TerminalConOutQueryMode (
401 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
410 TerminalConOutSetMode (
411 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
418 TerminalConOutSetAttribute (
419 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
426 TerminalConOutClearScreen (
427 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
433 TerminalConOutSetCursorPosition (
434 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
442 TerminalConOutEnableCursor (
443 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
450 TerminalDriverBindingSupported (
451 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
452 IN EFI_HANDLE Controller
,
453 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
458 TerminalDriverBindingStart (
459 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
460 IN EFI_HANDLE Controller
,
461 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
466 TerminalDriverBindingStop (
467 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
468 IN EFI_HANDLE Controller
,
469 IN UINTN NumberOfChildren
,
470 IN EFI_HANDLE
*ChildHandleBuffer
474 Retrieves a Unicode string that is the user readable name of the driver.
476 This function retrieves the user readable name of a driver in the form of a
477 Unicode string. If the driver specified by This has a user readable name in
478 the language specified by Language, then a pointer to the driver name is
479 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
480 by This does not support the language specified by Language,
481 then EFI_UNSUPPORTED is returned.
483 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
484 EFI_COMPONENT_NAME_PROTOCOL instance.
486 @param Language[in] A pointer to a Null-terminated ASCII string
487 array indicating the language. This is the
488 language of the driver name that the caller is
489 requesting, and it must match one of the
490 languages specified in SupportedLanguages. The
491 number of languages supported by a driver is up
492 to the driver writer. Language is specified
493 in RFC 3066 or ISO 639-2 language code format.
495 @param DriverName[out] A pointer to the Unicode string to return.
496 This Unicode string is the name of the
497 driver specified by This in the language
498 specified by Language.
500 @retval EFI_SUCCESS The Unicode string for the Driver specified by
501 This and the language specified by Language was
502 returned in DriverName.
504 @retval EFI_INVALID_PARAMETER Language is NULL.
506 @retval EFI_INVALID_PARAMETER DriverName is NULL.
508 @retval EFI_UNSUPPORTED The driver specified by This does not support
509 the language specified by Language.
514 TerminalComponentNameGetDriverName (
515 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
517 OUT CHAR16
**DriverName
522 Retrieves a Unicode string that is the user readable name of the controller
523 that is being managed by a driver.
525 This function retrieves the user readable name of the controller specified by
526 ControllerHandle and ChildHandle in the form of a Unicode string. If the
527 driver specified by This has a user readable name in the language specified by
528 Language, then a pointer to the controller name is returned in ControllerName,
529 and EFI_SUCCESS is returned. If the driver specified by This is not currently
530 managing the controller specified by ControllerHandle and ChildHandle,
531 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
532 support the language specified by Language, then EFI_UNSUPPORTED is returned.
534 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
535 EFI_COMPONENT_NAME_PROTOCOL instance.
537 @param ControllerHandle[in] The handle of a controller that the driver
538 specified by This is managing. This handle
539 specifies the controller whose name is to be
542 @param ChildHandle[in] The handle of the child controller to retrieve
543 the name of. This is an optional parameter that
544 may be NULL. It will be NULL for device
545 drivers. It will also be NULL for a bus drivers
546 that wish to retrieve the name of the bus
547 controller. It will not be NULL for a bus
548 driver that wishes to retrieve the name of a
551 @param Language[in] A pointer to a Null-terminated ASCII string
552 array indicating the language. This is the
553 language of the driver name that the caller is
554 requesting, and it must match one of the
555 languages specified in SupportedLanguages. The
556 number of languages supported by a driver is up
557 to the driver writer. Language is specified in
558 RFC 3066 or ISO 639-2 language code format.
560 @param ControllerName[out] A pointer to the Unicode string to return.
561 This Unicode string is the name of the
562 controller specified by ControllerHandle and
563 ChildHandle in the language specified by
564 Language from the point of view of the driver
567 @retval EFI_SUCCESS The Unicode string for the user readable name in
568 the language specified by Language for the
569 driver specified by This was returned in
572 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
574 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
577 @retval EFI_INVALID_PARAMETER Language is NULL.
579 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
581 @retval EFI_UNSUPPORTED The driver specified by This is not currently
582 managing the controller specified by
583 ControllerHandle and ChildHandle.
585 @retval EFI_UNSUPPORTED The driver specified by This does not support
586 the language specified by Language.
591 TerminalComponentNameGetControllerName (
592 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
593 IN EFI_HANDLE ControllerHandle
,
594 IN EFI_HANDLE ChildHandle OPTIONAL
,
596 OUT CHAR16
**ControllerName
601 // internal functions
604 TerminalConInCheckForKey (
605 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
610 TerminalUpdateConsoleDevVariable (
611 IN CHAR16
*VariableName
,
612 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
617 TerminalRemoveConsoleDevVariable (
618 IN CHAR16
*VariableName
,
619 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
624 TerminalGetVariableAndSize (
626 IN EFI_GUID
*VendorGuid
,
627 OUT UINTN
*VariableSize
632 SetTerminalDevicePath (
633 IN UINT8 TerminalType
,
634 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
635 OUT EFI_DEVICE_PATH_PROTOCOL
**TerminalDevicePath
641 IN TERMINAL_DEV
*TerminalDevice
646 InitializeUnicodeFiFo (
647 IN TERMINAL_DEV
*TerminalDevice
652 InitializeEfiKeyFiFo (
653 IN TERMINAL_DEV
*TerminalDevice
658 GetOneKeyFromSerial (
659 EFI_SERIAL_IO_PROTOCOL
*SerialIo
,
665 RawFiFoInsertOneKey (
666 TERMINAL_DEV
*TerminalDevice
,
672 RawFiFoRemoveOneKey (
673 TERMINAL_DEV
*TerminalDevice
,
680 TERMINAL_DEV
*TerminalDevice
686 TERMINAL_DEV
*TerminalDevice
691 EfiKeyFiFoInsertOneKey (
692 TERMINAL_DEV
*TerminalDevice
,
698 EfiKeyFiFoRemoveOneKey (
699 TERMINAL_DEV
*TerminalDevice
,
700 EFI_INPUT_KEY
*Output
706 TERMINAL_DEV
*TerminalDevice
712 TERMINAL_DEV
*TerminalDevice
717 UnicodeFiFoInsertOneKey (
718 TERMINAL_DEV
*TerminalDevice
,
724 UnicodeFiFoRemoveOneKey (
725 TERMINAL_DEV
*TerminalDevice
,
732 TERMINAL_DEV
*TerminalDevice
738 TERMINAL_DEV
*TerminalDevice
743 UnicodeFiFoGetKeyCount (
744 TERMINAL_DEV
*TerminalDevice
749 TranslateRawDataToEfiKey (
750 IN TERMINAL_DEV
*TerminalDevice
755 // internal functions for PC ANSI
758 AnsiRawDataToUnicode (
759 IN TERMINAL_DEV
*PcAnsiDevice
765 IN TERMINAL_DEV
*PcAnsiDevice
771 IN TERMINAL_DEV
*TerminalDevice
,
777 // internal functions for VT100
781 IN TERMINAL_DEV
*VT100Device
,
787 // internal functions for VT100Plus
790 VT100PlusTestString (
791 IN TERMINAL_DEV
*TerminalDevice
,
797 // internal functions for VTUTF8
800 VTUTF8RawDataToUnicode (
801 IN TERMINAL_DEV
*VtUtf8Device
807 IN TERMINAL_DEV
*TerminalDevice
,
815 OUT UTF8_CHAR
*Utf8Char
,
816 OUT UINT8
*ValidBytes
821 GetOneValidUtf8Char (
822 IN TERMINAL_DEV
*Utf8Device
,
823 OUT UTF8_CHAR
*Utf8Char
,
824 OUT UINT8
*ValidBytes
830 IN UTF8_CHAR Utf8Char
,
832 OUT CHAR16
*UnicodeChar
837 // functions for boxdraw unicode
840 TerminalIsValidTextGraphics (
842 OUT CHAR8
*PcAnsi
, OPTIONAL
843 OUT CHAR8
*Ascii OPTIONAL
848 TerminalIsValidAscii (
854 TerminalIsValidEfiCntlChar (