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
150 #define BRIGHT_CONTROL_OFFSET 2
151 #define FOREGROUND_CONTROL_OFFSET 6
152 #define BACKGROUND_CONTROL_OFFSET 11
154 #define COLUMN_OFFSET 5
165 extern EFI_DRIVER_BINDING_PROTOCOL gTerminalDriverBinding
;
166 extern EFI_COMPONENT_NAME_PROTOCOL gTerminalComponentName
;
167 extern EFI_COMPONENT_NAME2_PROTOCOL gTerminalComponentName2
;
169 extern EFI_GUID gSimpleTextInExNotifyGuid
;
176 IN EFI_HANDLE ImageHandle
,
177 IN EFI_SYSTEM_TABLE
*SystemTable
184 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
185 IN BOOLEAN ExtendedVerification
191 TerminalConInReadKeyStroke (
192 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
,
193 OUT EFI_INPUT_KEY
*Key
200 IN EFI_KEY_DATA
*RegsiteredData
,
201 IN EFI_KEY_DATA
*InputData
209 RegsiteredData - A pointer to a buffer that is filled in with the keystroke
210 state data for the key that was registered.
211 InputData - A pointer to a buffer that is filled in with the keystroke
212 state data for the key that was pressed.
215 TRUE - Key be pressed matches a registered key.
216 FLASE - Match failed.
223 TerminalConInWaitForKeyEx (
229 // Simple Text Input Ex protocol prototypes
234 TerminalConInResetEx (
235 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
236 IN BOOLEAN ExtendedVerification
241 Reset the input device and optionaly run diagnostics
244 This - Protocol instance pointer.
245 ExtendedVerification - Driver may perform diagnostics on reset.
248 EFI_SUCCESS - The device was reset.
249 EFI_DEVICE_ERROR - The device is not functioning properly and could
257 TerminalConInReadKeyStrokeEx (
258 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
259 OUT EFI_KEY_DATA
*KeyData
264 Reads the next keystroke from the input device. The WaitForKey Event can
265 be used to test for existance of a keystroke via WaitForEvent () call.
268 This - Protocol instance pointer.
269 KeyData - A pointer to a buffer that is filled in with the keystroke
270 state data for the key that was pressed.
273 EFI_SUCCESS - The keystroke information was returned.
274 EFI_NOT_READY - There was no keystroke data availiable.
275 EFI_DEVICE_ERROR - The keystroke information was not returned due to
277 EFI_INVALID_PARAMETER - KeyData is NULL.
284 TerminalConInSetState (
285 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
286 IN EFI_KEY_TOGGLE_STATE
*KeyToggleState
291 Set certain state for the input device.
294 This - Protocol instance pointer.
295 KeyToggleState - A pointer to the EFI_KEY_TOGGLE_STATE to set the
296 state for the input device.
299 EFI_SUCCESS - The device state was set successfully.
300 EFI_DEVICE_ERROR - The device is not functioning correctly and could
301 not have the setting adjusted.
302 EFI_UNSUPPORTED - The device does not have the ability to set its state.
303 EFI_INVALID_PARAMETER - KeyToggleState is NULL.
310 TerminalConInRegisterKeyNotify (
311 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
312 IN EFI_KEY_DATA
*KeyData
,
313 IN EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction
,
314 OUT EFI_HANDLE
*NotifyHandle
319 Register a notification function for a particular keystroke for the input device.
322 This - Protocol instance pointer.
323 KeyData - A pointer to a buffer that is filled in with the keystroke
324 information data for the key that was pressed.
325 KeyNotificationFunction - Points to the function to be called when the key
326 sequence is typed specified by KeyData.
327 NotifyHandle - Points to the unique handle assigned to the registered notification.
330 EFI_SUCCESS - The notification function was registered successfully.
331 EFI_OUT_OF_RESOURCES - Unable to allocate resources for necesssary data structures.
332 EFI_INVALID_PARAMETER - KeyData or NotifyHandle is NULL.
339 TerminalConInUnregisterKeyNotify (
340 IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*This
,
341 IN EFI_HANDLE NotificationHandle
346 Remove a registered notification function from a particular keystroke.
349 This - Protocol instance pointer.
350 NotificationHandle - The handle of the notification function being unregistered.
353 EFI_SUCCESS - The notification function was unregistered successfully.
354 EFI_INVALID_PARAMETER - The NotificationHandle is invalid.
355 EFI_NOT_FOUND - Can not find the matching entry in database.
362 TerminalConInWaitForKey (
370 TerminalConOutReset (
371 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
372 IN BOOLEAN ExtendedVerification
378 TerminalConOutOutputString (
379 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
386 TerminalConOutTestString (
387 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
394 TerminalConOutQueryMode (
395 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
404 TerminalConOutSetMode (
405 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
412 TerminalConOutSetAttribute (
413 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
420 TerminalConOutClearScreen (
421 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
427 TerminalConOutSetCursorPosition (
428 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
436 TerminalConOutEnableCursor (
437 IN EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*This
,
444 TerminalDriverBindingSupported (
445 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
446 IN EFI_HANDLE Controller
,
447 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
452 TerminalDriverBindingStart (
453 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
454 IN EFI_HANDLE Controller
,
455 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
460 TerminalDriverBindingStop (
461 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
462 IN EFI_HANDLE Controller
,
463 IN UINTN NumberOfChildren
,
464 IN EFI_HANDLE
*ChildHandleBuffer
468 Retrieves a Unicode string that is the user readable name of the driver.
470 This function retrieves the user readable name of a driver in the form of a
471 Unicode string. If the driver specified by This has a user readable name in
472 the language specified by Language, then a pointer to the driver name is
473 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
474 by This does not support the language specified by Language,
475 then EFI_UNSUPPORTED is returned.
477 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
478 EFI_COMPONENT_NAME_PROTOCOL instance.
480 @param Language[in] A pointer to a Null-terminated ASCII string
481 array indicating the language. This is the
482 language of the driver name that the caller is
483 requesting, and it must match one of the
484 languages specified in SupportedLanguages. The
485 number of languages supported by a driver is up
486 to the driver writer. Language is specified
487 in RFC 3066 or ISO 639-2 language code format.
489 @param DriverName[out] A pointer to the Unicode string to return.
490 This Unicode string is the name of the
491 driver specified by This in the language
492 specified by Language.
494 @retval EFI_SUCCESS The Unicode string for the Driver specified by
495 This and the language specified by Language was
496 returned in DriverName.
498 @retval EFI_INVALID_PARAMETER Language is NULL.
500 @retval EFI_INVALID_PARAMETER DriverName is NULL.
502 @retval EFI_UNSUPPORTED The driver specified by This does not support
503 the language specified by Language.
508 TerminalComponentNameGetDriverName (
509 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
511 OUT CHAR16
**DriverName
516 Retrieves a Unicode string that is the user readable name of the controller
517 that is being managed by a driver.
519 This function retrieves the user readable name of the controller specified by
520 ControllerHandle and ChildHandle in the form of a Unicode string. If the
521 driver specified by This has a user readable name in the language specified by
522 Language, then a pointer to the controller name is returned in ControllerName,
523 and EFI_SUCCESS is returned. If the driver specified by This is not currently
524 managing the controller specified by ControllerHandle and ChildHandle,
525 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
526 support the language specified by Language, then EFI_UNSUPPORTED is returned.
528 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
529 EFI_COMPONENT_NAME_PROTOCOL instance.
531 @param ControllerHandle[in] The handle of a controller that the driver
532 specified by This is managing. This handle
533 specifies the controller whose name is to be
536 @param ChildHandle[in] The handle of the child controller to retrieve
537 the name of. This is an optional parameter that
538 may be NULL. It will be NULL for device
539 drivers. It will also be NULL for a bus drivers
540 that wish to retrieve the name of the bus
541 controller. It will not be NULL for a bus
542 driver that wishes to retrieve the name of a
545 @param Language[in] A pointer to a Null-terminated ASCII string
546 array indicating the language. This is the
547 language of the driver name that the caller is
548 requesting, and it must match one of the
549 languages specified in SupportedLanguages. The
550 number of languages supported by a driver is up
551 to the driver writer. Language is specified in
552 RFC 3066 or ISO 639-2 language code format.
554 @param ControllerName[out] A pointer to the Unicode string to return.
555 This Unicode string is the name of the
556 controller specified by ControllerHandle and
557 ChildHandle in the language specified by
558 Language from the point of view of the driver
561 @retval EFI_SUCCESS The Unicode string for the user readable name in
562 the language specified by Language for the
563 driver specified by This was returned in
566 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
568 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
571 @retval EFI_INVALID_PARAMETER Language is NULL.
573 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
575 @retval EFI_UNSUPPORTED The driver specified by This is not currently
576 managing the controller specified by
577 ControllerHandle and ChildHandle.
579 @retval EFI_UNSUPPORTED The driver specified by This does not support
580 the language specified by Language.
585 TerminalComponentNameGetControllerName (
586 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
587 IN EFI_HANDLE ControllerHandle
,
588 IN EFI_HANDLE ChildHandle OPTIONAL
,
590 OUT CHAR16
**ControllerName
595 // internal functions
598 TerminalConInCheckForKey (
599 IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*This
604 TerminalUpdateConsoleDevVariable (
605 IN CHAR16
*VariableName
,
606 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
611 TerminalRemoveConsoleDevVariable (
612 IN CHAR16
*VariableName
,
613 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
618 TerminalGetVariableAndSize (
620 IN EFI_GUID
*VendorGuid
,
621 OUT UINTN
*VariableSize
626 SetTerminalDevicePath (
627 IN UINT8 TerminalType
,
628 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
629 OUT EFI_DEVICE_PATH_PROTOCOL
**TerminalDevicePath
635 IN TERMINAL_DEV
*TerminalDevice
640 InitializeUnicodeFiFo (
641 IN TERMINAL_DEV
*TerminalDevice
646 InitializeEfiKeyFiFo (
647 IN TERMINAL_DEV
*TerminalDevice
652 GetOneKeyFromSerial (
653 EFI_SERIAL_IO_PROTOCOL
*SerialIo
,
659 RawFiFoInsertOneKey (
660 TERMINAL_DEV
*TerminalDevice
,
666 RawFiFoRemoveOneKey (
667 TERMINAL_DEV
*TerminalDevice
,
674 TERMINAL_DEV
*TerminalDevice
680 TERMINAL_DEV
*TerminalDevice
685 EfiKeyFiFoInsertOneKey (
686 TERMINAL_DEV
*TerminalDevice
,
692 EfiKeyFiFoRemoveOneKey (
693 TERMINAL_DEV
*TerminalDevice
,
694 EFI_INPUT_KEY
*Output
700 TERMINAL_DEV
*TerminalDevice
706 TERMINAL_DEV
*TerminalDevice
711 UnicodeFiFoInsertOneKey (
712 TERMINAL_DEV
*TerminalDevice
,
718 UnicodeFiFoRemoveOneKey (
719 TERMINAL_DEV
*TerminalDevice
,
726 TERMINAL_DEV
*TerminalDevice
732 TERMINAL_DEV
*TerminalDevice
737 UnicodeFiFoGetKeyCount (
738 TERMINAL_DEV
*TerminalDevice
743 TranslateRawDataToEfiKey (
744 IN TERMINAL_DEV
*TerminalDevice
749 // internal functions for PC ANSI
752 AnsiRawDataToUnicode (
753 IN TERMINAL_DEV
*PcAnsiDevice
759 IN TERMINAL_DEV
*PcAnsiDevice
765 IN TERMINAL_DEV
*TerminalDevice
,
771 // internal functions for VT100
775 IN TERMINAL_DEV
*VT100Device
,
781 // internal functions for VT100Plus
784 VT100PlusTestString (
785 IN TERMINAL_DEV
*TerminalDevice
,
791 // internal functions for VTUTF8
794 VTUTF8RawDataToUnicode (
795 IN TERMINAL_DEV
*VtUtf8Device
801 IN TERMINAL_DEV
*TerminalDevice
,
809 OUT UTF8_CHAR
*Utf8Char
,
810 OUT UINT8
*ValidBytes
815 GetOneValidUtf8Char (
816 IN TERMINAL_DEV
*Utf8Device
,
817 OUT UTF8_CHAR
*Utf8Char
,
818 OUT UINT8
*ValidBytes
824 IN UTF8_CHAR Utf8Char
,
826 OUT CHAR16
*UnicodeChar
831 // functions for boxdraw unicode
834 TerminalIsValidTextGraphics (
836 OUT CHAR8
*PcAnsi
, OPTIONAL
837 OUT CHAR8
*Ascii OPTIONAL
842 TerminalIsValidAscii (
848 TerminalIsValidEfiCntlChar (