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 100
147 #define MODE1_ROW_COUNT 31
152 #define BRIGHT_CONTROL_OFFSET 2
153 #define FOREGROUND_CONTROL_OFFSET 6
154 #define BACKGROUND_CONTROL_OFFSET 11
156 #define COLUMN_OFFSET 5
167 extern EFI_DRIVER_BINDING_PROTOCOL gTerminalDriverBinding
;
168 extern EFI_COMPONENT_NAME_PROTOCOL gTerminalComponentName
;
169 extern EFI_COMPONENT_NAME2_PROTOCOL gTerminalComponentName2
;
171 extern EFI_GUID gSimpleTextInExNotifyGuid
;
178 IN EFI_HANDLE ImageHandle
,
179 IN EFI_SYSTEM_TABLE
*SystemTable
186 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
187 IN BOOLEAN ExtendedVerification
193 TerminalConInReadKeyStroke (
194 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
195 OUT EFI_INPUT_KEY
*Key
202 IN EFI_KEY_DATA
*RegsiteredData
,
203 IN EFI_KEY_DATA
*InputData
211 RegsiteredData - A pointer to a buffer that is filled in with the keystroke
212 state data for the key that was registered.
213 InputData - A pointer to a buffer that is filled in with the keystroke
214 state data for the key that was pressed.
217 TRUE - Key be pressed matches a registered key.
218 FLASE - Match failed.
225 TerminalConInWaitForKeyEx (
231 // Simple Text Input Ex protocol prototypes
236 TerminalConInResetEx (
237 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
238 IN BOOLEAN ExtendedVerification
243 Reset the input device and optionaly run diagnostics
246 This - Protocol instance pointer.
247 ExtendedVerification - Driver may perform diagnostics on reset.
250 EFI_SUCCESS - The device was reset.
251 EFI_DEVICE_ERROR - The device is not functioning properly and could
259 TerminalConInReadKeyStrokeEx (
260 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
261 OUT EFI_KEY_DATA
*KeyData
266 Reads the next keystroke from the input device. The WaitForKey Event can
267 be used to test for existance of a keystroke via WaitForEvent () call.
270 This - Protocol instance pointer.
271 KeyData - A pointer to a buffer that is filled in with the keystroke
272 state data for the key that was pressed.
275 EFI_SUCCESS - The keystroke information was returned.
276 EFI_NOT_READY - There was no keystroke data availiable.
277 EFI_DEVICE_ERROR - The keystroke information was not returned due to
279 EFI_INVALID_PARAMETER - KeyData is NULL.
286 TerminalConInSetState (
287 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
288 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
293 Set certain state for the input device.
296 This - Protocol instance pointer.
297 KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the
298 state for the input device.
301 EFI_SUCCESS - The device state was set successfully.
302 EFI_DEVICE_ERROR - The device is not functioning correctly and could
303 not have the setting adjusted.
304 EFI_UNSUPPORTED - The device does not have the ability to set its state.
305 EFI_INVALID_PARAMETER - KeyToggleState is NULL.
312 TerminalConInRegisterKeyNotify (
313 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
314 IN EFI_KEY_DATA
*KeyData
,
315 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
316 OUT EFI_HANDLE
*NotifyHandle
321 Register a notification function for a particular keystroke for the input device.
324 This - Protocol instance pointer.
325 KeyData - A pointer to a buffer that is filled in with the keystroke
326 information data for the key that was pressed.
327 KeyNotificationFunction - Points to the function to be called when the key
328 sequence is typed specified by KeyData.
329 NotifyHandle - Points to the unique handle assigned to the registered notification.
332 EFI_SUCCESS - The notification function was registered successfully.
333 EFI_OUT_OF_RESOURCES - Unable to allocate resources for necesssary data structures.
334 EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL.
341 TerminalConInUnregisterKeyNotify (
342 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
343 IN EFI_HANDLE NotificationHandle
348 Remove a registered notification function from a particular keystroke.
351 This - Protocol instance pointer.
352 NotificationHandle - The handle of the notification function being unregistered.
355 EFI_SUCCESS - The notification function was unregistered successfully.
356 EFI_INVALID_PARAMETER - The NotificationHandle is invalid.
357 EFI_NOT_FOUND - Can not find the matching entry in database.
364 TerminalConInWaitForKey (
372 TerminalConOutReset (
373 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
374 IN BOOLEAN ExtendedVerification
380 TerminalConOutOutputString (
381 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
388 TerminalConOutTestString (
389 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
396 TerminalConOutQueryMode (
397 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
406 TerminalConOutSetMode (
407 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
414 TerminalConOutSetAttribute (
415 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
422 TerminalConOutClearScreen (
423 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
429 TerminalConOutSetCursorPosition (
430 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
438 TerminalConOutEnableCursor (
439 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
446 TerminalDriverBindingSupported (
447 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
448 IN EFI_HANDLE Controller
,
449 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
454 TerminalDriverBindingStart (
455 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
456 IN EFI_HANDLE Controller
,
457 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
462 TerminalDriverBindingStop (
463 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
464 IN EFI_HANDLE Controller
,
465 IN UINTN NumberOfChildren
,
466 IN EFI_HANDLE
*ChildHandleBuffer
470 Retrieves a Unicode string that is the user readable name of the driver.
472 This function retrieves the user readable name of a driver in the form of a
473 Unicode string. If the driver specified by This has a user readable name in
474 the language specified by Language, then a pointer to the driver name is
475 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
476 by This does not support the language specified by Language,
477 then EFI_UNSUPPORTED is returned.
479 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
480 EFI_COMPONENT_NAME_PROTOCOL instance.
482 @param Language[in] A pointer to a Null-terminated ASCII string
483 array indicating the language. This is the
484 language of the driver name that the caller is
485 requesting, and it must match one of the
486 languages specified in SupportedLanguages. The
487 number of languages supported by a driver is up
488 to the driver writer. Language is specified
489 in RFC 3066 or ISO 639-2 language code format.
491 @param DriverName[out] A pointer to the Unicode string to return.
492 This Unicode string is the name of the
493 driver specified by This in the language
494 specified by Language.
496 @retval EFI_SUCCESS The Unicode string for the Driver specified by
497 This and the language specified by Language was
498 returned in DriverName.
500 @retval EFI_INVALID_PARAMETER Language is NULL.
502 @retval EFI_INVALID_PARAMETER DriverName is NULL.
504 @retval EFI_UNSUPPORTED The driver specified by This does not support
505 the language specified by Language.
510 TerminalComponentNameGetDriverName (
511 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
513 OUT CHAR16
**DriverName
518 Retrieves a Unicode string that is the user readable name of the controller
519 that is being managed by a driver.
521 This function retrieves the user readable name of the controller specified by
522 ControllerHandle and ChildHandle in the form of a Unicode string. If the
523 driver specified by This has a user readable name in the language specified by
524 Language, then a pointer to the controller name is returned in ControllerName,
525 and EFI_SUCCESS is returned. If the driver specified by This is not currently
526 managing the controller specified by ControllerHandle and ChildHandle,
527 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
528 support the language specified by Language, then EFI_UNSUPPORTED is returned.
530 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
531 EFI_COMPONENT_NAME_PROTOCOL instance.
533 @param ControllerHandle[in] The handle of a controller that the driver
534 specified by This is managing. This handle
535 specifies the controller whose name is to be
538 @param ChildHandle[in] The handle of the child controller to retrieve
539 the name of. This is an optional parameter that
540 may be NULL. It will be NULL for device
541 drivers. It will also be NULL for a bus drivers
542 that wish to retrieve the name of the bus
543 controller. It will not be NULL for a bus
544 driver that wishes to retrieve the name of a
547 @param Language[in] A pointer to a Null-terminated ASCII string
548 array indicating the language. This is the
549 language of the driver name that the caller is
550 requesting, and it must match one of the
551 languages specified in SupportedLanguages. The
552 number of languages supported by a driver is up
553 to the driver writer. Language is specified in
554 RFC 3066 or ISO 639-2 language code format.
556 @param ControllerName[out] A pointer to the Unicode string to return.
557 This Unicode string is the name of the
558 controller specified by ControllerHandle and
559 ChildHandle in the language specified by
560 Language from the point of view of the driver
563 @retval EFI_SUCCESS The Unicode string for the user readable name in
564 the language specified by Language for the
565 driver specified by This was returned in
568 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
570 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
573 @retval EFI_INVALID_PARAMETER Language is NULL.
575 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
577 @retval EFI_UNSUPPORTED The driver specified by This is not currently
578 managing the controller specified by
579 ControllerHandle and ChildHandle.
581 @retval EFI_UNSUPPORTED The driver specified by This does not support
582 the language specified by Language.
587 TerminalComponentNameGetControllerName (
588 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
589 IN EFI_HANDLE ControllerHandle
,
590 IN EFI_HANDLE ChildHandle OPTIONAL
,
592 OUT CHAR16
**ControllerName
597 // internal functions
600 TerminalConInCheckForKey (
601 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
606 TerminalUpdateConsoleDevVariable (
607 IN CHAR16
*VariableName
,
608 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
613 TerminalRemoveConsoleDevVariable (
614 IN CHAR16
*VariableName
,
615 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
620 TerminalGetVariableAndSize (
622 IN EFI_GUID
*VendorGuid
,
623 OUT UINTN
*VariableSize
628 SetTerminalDevicePath (
629 IN UINT8 TerminalType
,
630 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
631 OUT EFI_DEVICE_PATH_PROTOCOL
**TerminalDevicePath
637 IN TERMINAL_DEV
*TerminalDevice
642 InitializeUnicodeFiFo (
643 IN TERMINAL_DEV
*TerminalDevice
648 InitializeEfiKeyFiFo (
649 IN TERMINAL_DEV
*TerminalDevice
654 GetOneKeyFromSerial (
655 EFI_SERIAL_IO_PROTOCOL
*SerialIo
,
661 RawFiFoInsertOneKey (
662 TERMINAL_DEV
*TerminalDevice
,
668 RawFiFoRemoveOneKey (
669 TERMINAL_DEV
*TerminalDevice
,
676 TERMINAL_DEV
*TerminalDevice
682 TERMINAL_DEV
*TerminalDevice
687 EfiKeyFiFoInsertOneKey (
688 TERMINAL_DEV
*TerminalDevice
,
694 EfiKeyFiFoRemoveOneKey (
695 TERMINAL_DEV
*TerminalDevice
,
696 EFI_INPUT_KEY
*Output
702 TERMINAL_DEV
*TerminalDevice
708 TERMINAL_DEV
*TerminalDevice
713 UnicodeFiFoInsertOneKey (
714 TERMINAL_DEV
*TerminalDevice
,
720 UnicodeFiFoRemoveOneKey (
721 TERMINAL_DEV
*TerminalDevice
,
728 TERMINAL_DEV
*TerminalDevice
734 TERMINAL_DEV
*TerminalDevice
739 UnicodeFiFoGetKeyCount (
740 TERMINAL_DEV
*TerminalDevice
745 TranslateRawDataToEfiKey (
746 IN TERMINAL_DEV
*TerminalDevice
751 // internal functions for PC ANSI
754 AnsiRawDataToUnicode (
755 IN TERMINAL_DEV
*PcAnsiDevice
761 IN TERMINAL_DEV
*PcAnsiDevice
767 IN TERMINAL_DEV
*TerminalDevice
,
773 // internal functions for VT100
777 IN TERMINAL_DEV
*VT100Device
,
783 // internal functions for VT100Plus
786 VT100PlusTestString (
787 IN TERMINAL_DEV
*TerminalDevice
,
793 // internal functions for VTUTF8
796 VTUTF8RawDataToUnicode (
797 IN TERMINAL_DEV
*VtUtf8Device
803 IN TERMINAL_DEV
*TerminalDevice
,
811 OUT UTF8_CHAR
*Utf8Char
,
812 OUT UINT8
*ValidBytes
817 GetOneValidUtf8Char (
818 IN TERMINAL_DEV
*Utf8Device
,
819 OUT UTF8_CHAR
*Utf8Char
,
820 OUT UINT8
*ValidBytes
826 IN UTF8_CHAR Utf8Char
,
828 OUT CHAR16
*UnicodeChar
833 // functions for boxdraw unicode
836 TerminalIsValidTextGraphics (
838 OUT CHAR8
*PcAnsi
, OPTIONAL
839 OUT CHAR8
*Ascii OPTIONAL
844 TerminalIsValidAscii (
850 TerminalIsValidEfiCntlChar (