1 /** @file
2   Header file for Terminal driver.
3 
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 Copyright (C) 2016 Silicon Graphics, Inc. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 #ifndef _TERMINAL_H_
11 #define _TERMINAL_H_
12 
13 
14 #include <Uefi.h>
15 
16 #include <Guid/GlobalVariable.h>
17 #include <Guid/PcAnsi.h>
18 #include <Guid/TtyTerm.h>
19 #include <Guid/StatusCodeDataTypeVariable.h>
20 
21 #include <Protocol/SimpleTextOut.h>
22 #include <Protocol/SerialIo.h>
23 #include <Protocol/DevicePath.h>
24 #include <Protocol/SimpleTextIn.h>
25 #include <Protocol/SimpleTextInEx.h>
26 
27 #include <Library/DebugLib.h>
28 #include <Library/UefiDriverEntryPoint.h>
29 #include <Library/UefiLib.h>
30 #include <Library/ReportStatusCodeLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/MemoryAllocationLib.h>
33 #include <Library/UefiBootServicesTableLib.h>
34 #include <Library/UefiRuntimeServicesTableLib.h>
35 #include <Library/DevicePathLib.h>
36 #include <Library/PcdLib.h>
37 #include <Library/BaseLib.h>
38 
39 
40 #define RAW_FIFO_MAX_NUMBER 256
41 #define FIFO_MAX_NUMBER     128
42 
43 typedef struct {
44   UINT8 Head;
45   UINT8 Tail;
46   UINT8 Data[RAW_FIFO_MAX_NUMBER + 1];
47 } RAW_DATA_FIFO;
48 
49 typedef struct {
50   UINT8   Head;
51   UINT8   Tail;
52   UINT16  Data[FIFO_MAX_NUMBER + 1];
53 } UNICODE_FIFO;
54 
55 typedef struct {
56   UINT8         Head;
57   UINT8         Tail;
58   EFI_INPUT_KEY Data[FIFO_MAX_NUMBER + 1];
59 } EFI_KEY_FIFO;
60 
61 typedef struct {
62   UINTN   Columns;
63   UINTN   Rows;
64 } TERMINAL_CONSOLE_MODE_DATA;
65 
66 #define KEYBOARD_TIMER_INTERVAL         200000  // 0.02s
67 
68 #define TERMINAL_DEV_SIGNATURE  SIGNATURE_32 ('t', 'm', 'n', 'l')
69 
70 #define TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE SIGNATURE_32 ('t', 'm', 'e', 'n')
71 
72 typedef struct _TERMINAL_CONSOLE_IN_EX_NOTIFY {
73   UINTN                                 Signature;
74   EFI_KEY_DATA                          KeyData;
75   EFI_KEY_NOTIFY_FUNCTION               KeyNotificationFn;
76   LIST_ENTRY                            NotifyEntry;
77 } TERMINAL_CONSOLE_IN_EX_NOTIFY;
78 
79 typedef enum {
80   TerminalTypePcAnsi,
81   TerminalTypeVt100,
82   TerminalTypeVt100Plus,
83   TerminalTypeVtUtf8,
84   TerminalTypeTtyTerm
85 } TERMINAL_TYPE;
86 
87 typedef struct {
88   UINTN                               Signature;
89   EFI_HANDLE                          Handle;
90   TERMINAL_TYPE                       TerminalType;
91   EFI_SERIAL_IO_PROTOCOL              *SerialIo;
92   EFI_DEVICE_PATH_PROTOCOL            *DevicePath;
93   EFI_SIMPLE_TEXT_INPUT_PROTOCOL      SimpleInput;
94   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL     SimpleTextOutput;
95   EFI_SIMPLE_TEXT_OUTPUT_MODE         SimpleTextOutputMode;
96   TERMINAL_CONSOLE_MODE_DATA          *TerminalConsoleModeData;
97   UINTN                               SerialInTimeOut;
98   RAW_DATA_FIFO                       *RawFiFo;
99   UNICODE_FIFO                        *UnicodeFiFo;
100   EFI_KEY_FIFO                        *EfiKeyFiFo;
101   EFI_KEY_FIFO                        *EfiKeyFiFoForNotify;
102   EFI_UNICODE_STRING_TABLE            *ControllerNameTable;
103   EFI_EVENT                           TimerEvent;
104   EFI_EVENT                           TwoSecondTimeOut;
105   UINT32                              InputState;
106   UINT32                              ResetState;
107   UINT16                              TtyEscapeStr[3];
108   INTN                                TtyEscapeIndex;
109 
110   //
111   // Esc could not be output to the screen by user,
112   // but the terminal driver need to output it to
113   // the terminal emulation software to send control sequence.
114   // This boolean is used by the terminal driver only
115   // to indicate whether the Esc could be sent or not.
116   //
117   BOOLEAN                             OutputEscChar;
118   EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL   SimpleInputEx;
119   LIST_ENTRY                          NotifyList;
120   EFI_EVENT                           KeyNotifyProcessEvent;
121 } TERMINAL_DEV;
122 
123 #define INPUT_STATE_DEFAULT               0x00
124 #define INPUT_STATE_ESC                   0x01
125 #define INPUT_STATE_CSI                   0x02
126 #define INPUT_STATE_LEFTOPENBRACKET       0x04
127 #define INPUT_STATE_O                     0x08
128 #define INPUT_STATE_2                     0x10
129 #define INPUT_STATE_LEFTOPENBRACKET_2     0x20
130 
131 #define RESET_STATE_DEFAULT               0x00
132 #define RESET_STATE_ESC_R                 0x01
133 #define RESET_STATE_ESC_R_ESC_R           0x02
134 
135 #define TERMINAL_CON_IN_DEV_FROM_THIS(a)  CR (a, TERMINAL_DEV, SimpleInput, TERMINAL_DEV_SIGNATURE)
136 #define TERMINAL_CON_OUT_DEV_FROM_THIS(a) CR (a, TERMINAL_DEV, SimpleTextOutput, TERMINAL_DEV_SIGNATURE)
137 #define TERMINAL_CON_IN_EX_DEV_FROM_THIS(a)  CR (a, TERMINAL_DEV, SimpleInputEx, TERMINAL_DEV_SIGNATURE)
138 
139 typedef union {
140   UINT8 Utf8_1;
141   UINT8 Utf8_2[2];
142   UINT8 Utf8_3[3];
143 } UTF8_CHAR;
144 
145 #define LEFTOPENBRACKET           0x5b  // '['
146 #define ACAP                      0x41
147 #define BCAP                      0x42
148 #define CCAP                      0x43
149 #define DCAP                      0x44
150 
151 #define BACKSPACE                 8
152 #define ESC                       27
153 #define CSI                       0x9B
154 #define DEL                       127
155 #define BRIGHT_CONTROL_OFFSET     2
156 #define FOREGROUND_CONTROL_OFFSET 6
157 #define BACKGROUND_CONTROL_OFFSET 11
158 #define ROW_OFFSET                2
159 #define COLUMN_OFFSET             5
160 #define FW_BACK_OFFSET            2
161 
162 typedef struct {
163   UINT16  Unicode;
164   CHAR8   PcAnsi;
165   CHAR8   Ascii;
166 } UNICODE_TO_CHAR;
167 
168 //
169 // Global Variables
170 //
171 extern EFI_DRIVER_BINDING_PROTOCOL   gTerminalDriverBinding;
172 extern EFI_COMPONENT_NAME_PROTOCOL   gTerminalComponentName;
173 extern EFI_COMPONENT_NAME2_PROTOCOL  gTerminalComponentName2;
174 
175 /**
176   The user Entry Point for module Terminal. The user code starts with this function.
177 
178   @param[in] ImageHandle    The firmware allocated handle for the EFI image.
179   @param[in] SystemTable    A pointer to the EFI System Table.
180 
181   @retval EFI_SUCCESS       The entry point is executed successfully.
182   @retval other             Some error occurs when executing this entry point.
183 
184 **/
185 EFI_STATUS
186 EFIAPI
187 InitializeTerminal (
188   IN EFI_HANDLE         ImageHandle,
189   IN EFI_SYSTEM_TABLE   *SystemTable
190   );
191 
192 /**
193   Implements EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset().
194   This driver only perform dependent serial device reset regardless of
195   the value of ExtendeVerification
196 
197   @param  This                     Indicates the calling context.
198   @param  ExtendedVerification     Skip by this driver.
199 
200   @retval EFI_SUCCESS              The reset operation succeeds.
201   @retval EFI_DEVICE_ERROR         The dependent serial port reset fails.
202 
203 **/
204 EFI_STATUS
205 EFIAPI
206 TerminalConInReset (
207   IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL    *This,
208   IN  BOOLEAN                           ExtendedVerification
209   );
210 
211 
212 /**
213   Implements EFI_SIMPLE_TEXT_INPUT_PROTOCOL.ReadKeyStroke().
214 
215   @param  This                Indicates the calling context.
216   @param  Key                 A pointer to a buffer that is filled in with the
217                               keystroke information for the key that was sent
218                               from terminal.
219 
220   @retval EFI_SUCCESS         The keystroke information is returned successfully.
221   @retval EFI_NOT_READY       There is no keystroke data available.
222   @retval EFI_DEVICE_ERROR    The dependent serial device encounters error.
223 
224 **/
225 EFI_STATUS
226 EFIAPI
227 TerminalConInReadKeyStroke (
228   IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *This,
229   OUT EFI_INPUT_KEY                   *Key
230   );
231 
232 /**
233   Check if the key already has been registered.
234 
235   @param  RegsiteredData           A pointer to a buffer that is filled in with the
236                                    keystroke state data for the key that was
237                                    registered.
238   @param  InputData                A pointer to a buffer that is filled in with the
239                                    keystroke state data for the key that was
240                                    pressed.
241 
242   @retval TRUE                     Key be pressed matches a registered key.
243   @retval FALSE                    Match failed.
244 
245 **/
246 BOOLEAN
247 IsKeyRegistered (
248   IN EFI_KEY_DATA  *RegsiteredData,
249   IN EFI_KEY_DATA  *InputData
250   );
251 
252 /**
253   Event notification function for EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.WaitForKeyEx event
254   Signal the event if there is key available
255 
256   @param  Event                    Indicates the event that invoke this function.
257   @param  Context                  Indicates the calling context.
258 
259 **/
260 VOID
261 EFIAPI
262 TerminalConInWaitForKeyEx (
263   IN  EFI_EVENT       Event,
264   IN  VOID            *Context
265   );
266 
267 //
268 // Simple Text Input Ex protocol prototypes
269 //
270 
271 /**
272   Reset the input device and optionally run diagnostics
273 
274   @param  This                     Protocol instance pointer.
275   @param  ExtendedVerification     Driver may perform diagnostics on reset.
276 
277   @retval EFI_SUCCESS              The device was reset.
278   @retval EFI_DEVICE_ERROR         The device is not functioning properly and could
279                                    not be reset.
280 
281 **/
282 EFI_STATUS
283 EFIAPI
284 TerminalConInResetEx (
285   IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL  *This,
286   IN BOOLEAN                            ExtendedVerification
287   );
288 
289 /**
290   Reads the next keystroke from the input device. The WaitForKey Event can
291   be used to test for existence of a keystroke via WaitForEvent () call.
292 
293   @param  This                     Protocol instance pointer.
294   @param  KeyData                  A pointer to a buffer that is filled in with the
295                                    keystroke state data for the key that was
296                                    pressed.
297 
298   @retval EFI_SUCCESS              The keystroke information was returned.
299   @retval EFI_NOT_READY            There was no keystroke data available.
300   @retval EFI_DEVICE_ERROR         The keystroke information was not returned due
301                                    to hardware errors.
302   @retval EFI_INVALID_PARAMETER    KeyData is NULL.
303 
304 **/
305 EFI_STATUS
306 EFIAPI
307 TerminalConInReadKeyStrokeEx (
308   IN  EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This,
309   OUT EFI_KEY_DATA                      *KeyData
310   );
311 
312 /**
313   Set certain state for the input device.
314 
315   @param  This                     Protocol instance pointer.
316   @param  KeyToggleState           A pointer to the EFI_KEY_TOGGLE_STATE to set the
317                                    state for the input device.
318 
319   @retval EFI_SUCCESS              The device state was set successfully.
320   @retval EFI_DEVICE_ERROR         The device is not functioning correctly and
321                                    could not have the setting adjusted.
322   @retval EFI_UNSUPPORTED          The device does not have the ability to set its
323                                    state.
324   @retval EFI_INVALID_PARAMETER    KeyToggleState is NULL.
325 
326 **/
327 EFI_STATUS
328 EFIAPI
329 TerminalConInSetState (
330   IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL  *This,
331   IN EFI_KEY_TOGGLE_STATE               *KeyToggleState
332   );
333 
334 /**
335   Register a notification function for a particular keystroke for the input device.
336 
337   @param  This                     Protocol instance pointer.
338   @param  KeyData                  A pointer to a buffer that is filled in with the
339                                    keystroke information data for the key that was
340                                    pressed. If KeyData.Key, KeyData.KeyState.KeyToggleState
341                                    and KeyData.KeyState.KeyShiftState are 0, then any incomplete
342                                    keystroke will trigger a notification of the KeyNotificationFunction.
343   @param  KeyNotificationFunction  Points to the function to be called when the key
344                                    sequence is typed specified by KeyData. This notification function
345                                    should be called at <=TPL_CALLBACK.
346   @param  NotifyHandle             Points to the unique handle assigned to the
347                                    registered notification.
348 
349   @retval EFI_SUCCESS              The notification function was registered
350                                    successfully.
351   @retval EFI_OUT_OF_RESOURCES     Unable to allocate resources for necesssary data
352                                    structures.
353   @retval EFI_INVALID_PARAMETER    KeyData or NotifyHandle is NULL.
354 
355 **/
356 EFI_STATUS
357 EFIAPI
358 TerminalConInRegisterKeyNotify (
359   IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL  *This,
360   IN EFI_KEY_DATA                       *KeyData,
361   IN EFI_KEY_NOTIFY_FUNCTION            KeyNotificationFunction,
362   OUT VOID                              **NotifyHandle
363   );
364 
365 /**
366   Remove a registered notification function from a particular keystroke.
367 
368   @param  This                     Protocol instance pointer.
369   @param  NotificationHandle       The handle of the notification function being
370                                    unregistered.
371 
372   @retval EFI_SUCCESS              The notification function was unregistered
373                                    successfully.
374   @retval EFI_INVALID_PARAMETER    The NotificationHandle is invalid.
375   @retval EFI_NOT_FOUND            Can not find the matching entry in database.
376 
377 **/
378 EFI_STATUS
379 EFIAPI
380 TerminalConInUnregisterKeyNotify (
381   IN EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL  *This,
382   IN VOID                               *NotificationHandle
383   );
384 
385 /**
386   Event notification function for EFI_SIMPLE_TEXT_INPUT_PROTOCOL.WaitForKey event
387   Signal the event if there is key available
388 
389   @param  Event                    Indicates the event that invoke this function.
390   @param  Context                  Indicates the calling context.
391 
392 **/
393 VOID
394 EFIAPI
395 TerminalConInWaitForKey (
396   IN  EFI_EVENT     Event,
397   IN  VOID          *Context
398   );
399 
400 /**
401   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset().
402   If ExtendeVerification is TRUE, then perform dependent serial device reset,
403   and set display mode to mode 0.
404   If ExtendedVerification is FALSE, only set display mode to mode 0.
405 
406   @param  This                  Indicates the calling context.
407   @param  ExtendedVerification  Indicates that the driver may perform a more
408                                 exhaustive verification operation of the device
409                                 during reset.
410 
411   @retval EFI_SUCCESS           The reset operation succeeds.
412   @retval EFI_DEVICE_ERROR      The terminal is not functioning correctly or the serial port reset fails.
413 
414 **/
415 EFI_STATUS
416 EFIAPI
417 TerminalConOutReset (
418   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL    *This,
419   IN  BOOLEAN                            ExtendedVerification
420   );
421 
422 /**
423   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString().
424   The Unicode string will be converted to terminal expressible data stream
425   and send to terminal via serial port.
426 
427   @param  This                    Indicates the calling context.
428   @param  WString                 The Null-terminated Unicode string to be displayed
429                                   on the terminal screen.
430 
431   @retval EFI_SUCCESS             The string is output successfully.
432   @retval EFI_DEVICE_ERROR        The serial port fails to send the string out.
433   @retval EFI_WARN_UNKNOWN_GLYPH  Indicates that some of the characters in the Unicode string could not
434                                   be rendered and are skipped.
435   @retval EFI_UNSUPPORTED         If current display mode is out of range.
436 
437 **/
438 EFI_STATUS
439 EFIAPI
440 TerminalConOutOutputString (
441   IN   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,
442   IN  CHAR16                            *WString
443   );
444 
445 /**
446   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.TestString().
447   If one of the characters in the *Wstring is
448   neither valid Unicode drawing characters,
449   not ASCII code, then this function will return
450   EFI_UNSUPPORTED.
451 
452   @param  This              Indicates the calling context.
453   @param  WString           The Null-terminated Unicode string to be tested.
454 
455   @retval EFI_SUCCESS       The terminal is capable of rendering the output string.
456   @retval EFI_UNSUPPORTED   Some of the characters in the Unicode string cannot be rendered.
457 
458 **/
459 EFI_STATUS
460 EFIAPI
461 TerminalConOutTestString (
462   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,
463   IN  CHAR16                           *WString
464   );
465 
466 /**
467   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.QueryMode().
468   It returns information for an available text mode
469   that the terminal supports.
470   In this driver, we support text mode 80x25 (mode 0),
471   80x50 (mode 1), 100x31 (mode 2).
472 
473   @param This        Indicates the calling context.
474   @param ModeNumber  The mode number to return information on.
475   @param Columns     The returned columns of the requested mode.
476   @param Rows        The returned rows of the requested mode.
477 
478   @retval EFI_SUCCESS       The requested mode information is returned.
479   @retval EFI_UNSUPPORTED   The mode number is not valid.
480   @retval EFI_DEVICE_ERROR
481 
482 **/
483 EFI_STATUS
484 EFIAPI
485 TerminalConOutQueryMode (
486   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,
487   IN  UINTN                            ModeNumber,
488   OUT UINTN                            *Columns,
489   OUT UINTN                            *Rows
490   );
491 
492 /**
493   Implements EFI_SIMPLE_TEXT_OUT.SetMode().
494   Set the terminal to a specified display mode.
495   In this driver, we only support mode 0.
496 
497   @param This          Indicates the calling context.
498   @param ModeNumber    The text mode to set.
499 
500   @retval EFI_SUCCESS       The requested text mode is set.
501   @retval EFI_DEVICE_ERROR  The requested text mode cannot be set
502                             because of serial device error.
503   @retval EFI_UNSUPPORTED   The text mode number is not valid.
504 
505 **/
506 EFI_STATUS
507 EFIAPI
508 TerminalConOutSetMode (
509   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,
510   IN  UINTN                            ModeNumber
511   );
512 
513 /**
514   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute().
515 
516   @param This        Indicates the calling context.
517   @param Attribute   The attribute to set. Only bit0..6 are valid, all other bits
518                      are undefined and must be zero.
519 
520   @retval EFI_SUCCESS        The requested attribute is set.
521   @retval EFI_DEVICE_ERROR   The requested attribute cannot be set due to serial port error.
522   @retval EFI_UNSUPPORTED    The attribute requested is not defined by EFI spec.
523 
524 **/
525 EFI_STATUS
526 EFIAPI
527 TerminalConOutSetAttribute (
528   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,
529   IN  UINTN                            Attribute
530   );
531 
532 /**
533   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.ClearScreen().
534   It clears the ANSI terminal's display to the
535   currently selected background color.
536 
537   @param This     Indicates the calling context.
538 
539   @retval EFI_SUCCESS       The operation completed successfully.
540   @retval EFI_DEVICE_ERROR  The terminal screen cannot be cleared due to serial port error.
541   @retval EFI_UNSUPPORTED   The terminal is not in a valid display mode.
542 
543 **/
544 EFI_STATUS
545 EFIAPI
546 TerminalConOutClearScreen (
547   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This
548   );
549 
550 /**
551   Implements EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetCursorPosition().
552 
553   @param This      Indicates the calling context.
554   @param Column    The row to set cursor to.
555   @param Row       The column to set cursor to.
556 
557   @retval EFI_SUCCESS       The operation completed successfully.
558   @retval EFI_DEVICE_ERROR  The request fails due to serial port error.
559   @retval EFI_UNSUPPORTED   The terminal is not in a valid text mode, or the cursor position
560                             is invalid for current mode.
561 
562 **/
563 EFI_STATUS
564 EFIAPI
565 TerminalConOutSetCursorPosition (
566   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,
567   IN  UINTN                            Column,
568   IN  UINTN                            Row
569   );
570 
571 /**
572   Implements SIMPLE_TEXT_OUTPUT.EnableCursor().
573   In this driver, the cursor cannot be hidden.
574 
575   @param This      Indicates the calling context.
576   @param Visible   If TRUE, the cursor is set to be visible,
577                    If FALSE, the cursor is set to be invisible.
578 
579   @retval EFI_SUCCESS      The request is valid.
580   @retval EFI_UNSUPPORTED  The terminal does not support cursor hidden.
581 
582 **/
583 EFI_STATUS
584 EFIAPI
585 TerminalConOutEnableCursor (
586   IN  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL  *This,
587   IN  BOOLEAN                          Visible
588   );
589 
590 /**
591   Test to see if this driver supports Controller.
592 
593   @param  This                Protocol instance pointer.
594   @param  ControllerHandle    Handle of device to test
595   @param  RemainingDevicePath Optional parameter use to pick a specific child
596                               device to start.
597 
598   @retval EFI_SUCCESS         This driver supports this device.
599   @retval EFI_ALREADY_STARTED This driver is already running on this device.
600   @retval other               This driver does not support this device.
601 
602 **/
603 EFI_STATUS
604 EFIAPI
605 TerminalDriverBindingSupported (
606   IN EFI_DRIVER_BINDING_PROTOCOL    *This,
607   IN EFI_HANDLE                     ControllerHandle,
608   IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
609   );
610 
611 /**
612   Start this driver on Controller by opening a Serial IO protocol,
613   reading Device Path, and creating a child handle with a Simple Text In,
614   Simple Text In Ex and Simple Text Out protocol, and device path protocol.
615   And store Console Device Environment Variables.
616 
617   @param  This                 Protocol instance pointer.
618   @param  Controller           Handle of device to bind driver to
619   @param  RemainingDevicePath  Optional parameter use to pick a specific child
620                                device to start.
621 
622   @retval EFI_SUCCESS          This driver is added to Controller.
623   @retval EFI_ALREADY_STARTED  This driver is already running on Controller.
624   @retval other                This driver does not support this device.
625 
626 **/
627 EFI_STATUS
628 EFIAPI
629 TerminalDriverBindingStart (
630   IN EFI_DRIVER_BINDING_PROTOCOL    *This,
631   IN EFI_HANDLE                     Controller,
632   IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
633   );
634 
635 
636 /**
637   Stop this driver on Controller by closing Simple Text In, Simple Text
638   In Ex, Simple Text Out protocol, and removing parent device path from
639   Console Device Environment Variables.
640 
641   @param  This              Protocol instance pointer.
642   @param  Controller        Handle of device to stop driver on
643   @param  NumberOfChildren  Number of Handles in ChildHandleBuffer. If number of
644                             children is zero stop the entire bus driver.
645   @param  ChildHandleBuffer List of Child Handles to Stop.
646 
647   @retval EFI_SUCCESS       This driver is removed Controller.
648   @retval other             This driver could not be removed from this device.
649 
650 **/
651 EFI_STATUS
652 EFIAPI
653 TerminalDriverBindingStop (
654   IN  EFI_DRIVER_BINDING_PROTOCOL    *This,
655   IN  EFI_HANDLE                     Controller,
656   IN  UINTN                          NumberOfChildren,
657   IN  EFI_HANDLE                     *ChildHandleBuffer
658   );
659 
660 /**
661   Free notify functions list.
662 
663   @param  ListHead               The list head
664 
665   @retval EFI_SUCCESS            Free the notify list successfully.
666   @retval EFI_INVALID_PARAMETER  ListHead is NULL.
667 
668 **/
669 EFI_STATUS
670 TerminalFreeNotifyList (
671   IN OUT LIST_ENTRY           *ListHead
672   );
673 
674 /**
675   Retrieves a Unicode string that is the user readable name of the driver.
676 
677   This function retrieves the user readable name of a driver in the form of a
678   Unicode string. If the driver specified by This has a user readable name in
679   the language specified by Language, then a pointer to the driver name is
680   returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
681   by This does not support the language specified by Language,
682   then EFI_UNSUPPORTED is returned.
683 
684   @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
685                                 EFI_COMPONENT_NAME_PROTOCOL instance.
686 
687   @param  Language[in]          A pointer to a Null-terminated ASCII string
688                                 array indicating the language. This is the
689                                 language of the driver name that the caller is
690                                 requesting, and it must match one of the
691                                 languages specified in SupportedLanguages. The
692                                 number of languages supported by a driver is up
693                                 to the driver writer. Language is specified
694                                 in RFC 4646 or ISO 639-2 language code format.
695 
696   @param  DriverName[out]       A pointer to the Unicode string to return.
697                                 This Unicode string is the name of the
698                                 driver specified by This in the language
699                                 specified by Language.
700 
701   @retval EFI_SUCCESS           The Unicode string for the Driver specified by
702                                 This and the language specified by Language was
703                                 returned in DriverName.
704 
705   @retval EFI_INVALID_PARAMETER Language is NULL.
706 
707   @retval EFI_INVALID_PARAMETER DriverName is NULL.
708 
709   @retval EFI_UNSUPPORTED       The driver specified by This does not support
710                                 the language specified by Language.
711 
712 **/
713 EFI_STATUS
714 EFIAPI
715 TerminalComponentNameGetDriverName (
716   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
717   IN  CHAR8                        *Language,
718   OUT CHAR16                       **DriverName
719   );
720 
721 
722 /**
723   Retrieves a Unicode string that is the user readable name of the controller
724   that is being managed by a driver.
725 
726   This function retrieves the user readable name of the controller specified by
727   ControllerHandle and ChildHandle in the form of a Unicode string. If the
728   driver specified by This has a user readable name in the language specified by
729   Language, then a pointer to the controller name is returned in ControllerName,
730   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
731   managing the controller specified by ControllerHandle and ChildHandle,
732   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
733   support the language specified by Language, then EFI_UNSUPPORTED is returned.
734 
735   @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
736                                 EFI_COMPONENT_NAME_PROTOCOL instance.
737 
738   @param  ControllerHandle[in]  The handle of a controller that the driver
739                                 specified by This is managing.  This handle
740                                 specifies the controller whose name is to be
741                                 returned.
742 
743   @param  ChildHandle[in]       The handle of the child controller to retrieve
744                                 the name of.  This is an optional parameter that
745                                 may be NULL.  It will be NULL for device
746                                 drivers.  It will also be NULL for a bus drivers
747                                 that wish to retrieve the name of the bus
748                                 controller.  It will not be NULL for a bus
749                                 driver that wishes to retrieve the name of a
750                                 child controller.
751 
752   @param  Language[in]          A pointer to a Null-terminated ASCII string
753                                 array indicating the language.  This is the
754                                 language of the driver name that the caller is
755                                 requesting, and it must match one of the
756                                 languages specified in SupportedLanguages. The
757                                 number of languages supported by a driver is up
758                                 to the driver writer. Language is specified in
759                                 RFC 4646 or ISO 639-2 language code format.
760 
761   @param  ControllerName[out]   A pointer to the Unicode string to return.
762                                 This Unicode string is the name of the
763                                 controller specified by ControllerHandle and
764                                 ChildHandle in the language specified by
765                                 Language from the point of view of the driver
766                                 specified by This.
767 
768   @retval EFI_SUCCESS           The Unicode string for the user readable name in
769                                 the language specified by Language for the
770                                 driver specified by This was returned in
771                                 DriverName.
772 
773   @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
774 
775   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
776                                 EFI_HANDLE.
777 
778   @retval EFI_INVALID_PARAMETER Language is NULL.
779 
780   @retval EFI_INVALID_PARAMETER ControllerName is NULL.
781 
782   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
783                                 managing the controller specified by
784                                 ControllerHandle and ChildHandle.
785 
786   @retval EFI_UNSUPPORTED       The driver specified by This does not support
787                                 the language specified by Language.
788 
789 **/
790 EFI_STATUS
791 EFIAPI
792 TerminalComponentNameGetControllerName (
793   IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,
794   IN  EFI_HANDLE                                      ControllerHandle,
795   IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
796   IN  CHAR8                                           *Language,
797   OUT CHAR16                                          **ControllerName
798   );
799 
800 
801 //
802 // internal functions
803 //
804 
805 /**
806   Check for a pending key in the Efi Key FIFO or Serial device buffer.
807 
808   @param  This                     Indicates the calling context.
809 
810   @retval EFI_SUCCESS              There is key pending.
811   @retval EFI_NOT_READY            There is no key pending.
812   @retval EFI_DEVICE_ERROR         If Serial IO is not attached to serial device.
813 
814 **/
815 EFI_STATUS
816 TerminalConInCheckForKey (
817   IN  EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *This
818   );
819 
820 /**
821   Update terminal device path in Console Device Environment Variables.
822 
823   @param  VariableName           The Console Device Environment Variable.
824   @param  ParentDevicePath       The terminal device path to be updated.
825 
826   @return None.
827 
828 **/
829 VOID
830 TerminalUpdateConsoleDevVariable (
831   IN CHAR16                    *VariableName,
832   IN EFI_DEVICE_PATH_PROTOCOL  *ParentDevicePath
833   );
834 
835 /**
836   Remove console device variable.
837 
838   @param  VariableName           A pointer to the variable name.
839   @param  ParentDevicePath       A pointer to the parent device path.
840 
841 **/
842 VOID
843 TerminalRemoveConsoleDevVariable (
844   IN CHAR16                    *VariableName,
845   IN EFI_DEVICE_PATH_PROTOCOL  *ParentDevicePath
846   );
847 
848 /**
849   Build termial device path according to terminal type.
850 
851   @param  TerminalType           The terminal type is PC ANSI, VT100, VT100+ or VT-UTF8.
852   @param  ParentDevicePath       Parent device path.
853   @param  TerminalDevicePath     Returned terminal device path, if building successfully.
854 
855   @retval EFI_UNSUPPORTED        Terminal does not belong to the supported type.
856   @retval EFI_OUT_OF_RESOURCES   Generate terminal device path failed.
857   @retval EFI_SUCCESS            Build terminal device path successfully.
858 
859 **/
860 EFI_STATUS
861 SetTerminalDevicePath (
862   IN  TERMINAL_TYPE               TerminalType,
863   IN  EFI_DEVICE_PATH_PROTOCOL    *ParentDevicePath,
864   OUT EFI_DEVICE_PATH_PROTOCOL    **TerminalDevicePath
865   );
866 
867 /**
868   Get one key out of serial buffer.
869 
870   @param  SerialIo           Serial I/O protocl attached to the serial device.
871   @param  Input              The fetched key.
872 
873   @retval EFI_NOT_READY      If serial buffer is empty.
874   @retval EFI_DEVICE_ERROR   If reading serial buffer encounter error.
875   @retval EFI_SUCCESS        If reading serial buffer successfully, put
876                              the fetched key to the parameter output.
877 
878 **/
879 EFI_STATUS
880 GetOneKeyFromSerial (
881   EFI_SERIAL_IO_PROTOCOL  *SerialIo,
882   UINT8                   *Input
883   );
884 
885 /**
886   Insert one byte raw data into the Raw Data FIFO.
887 
888   @param  TerminalDevice       Terminal driver private structure.
889   @param  Input                The key will be input.
890 
891   @retval TRUE                 If insert successfully.
892   @retval FALSE                If Raw Data buffer is full before key insertion,
893                                and the key is lost.
894 
895 **/
896 BOOLEAN
897 RawFiFoInsertOneKey (
898   TERMINAL_DEV  *TerminalDevice,
899   UINT8         Input
900   );
901 
902 /**
903   Remove one pre-fetched key out of the Raw Data FIFO.
904 
905   @param  TerminalDevice       Terminal driver private structure.
906   @param  Output               The key will be removed.
907 
908   @retval TRUE                 If insert successfully.
909   @retval FALSE                If Raw Data FIFO buffer is empty before remove operation.
910 
911 **/
912 BOOLEAN
913 RawFiFoRemoveOneKey (
914   TERMINAL_DEV  *TerminalDevice,
915   UINT8         *Output
916   );
917 
918 /**
919   Clarify whether Raw Data FIFO buffer is empty.
920 
921   @param  TerminalDevice       Terminal driver private structure
922 
923   @retval TRUE                 If Raw Data FIFO buffer is empty.
924   @retval FALSE                If Raw Data FIFO buffer is not empty.
925 
926 **/
927 BOOLEAN
928 IsRawFiFoEmpty (
929   TERMINAL_DEV  *TerminalDevice
930   );
931 
932 /**
933   Clarify whether Raw Data FIFO buffer is full.
934 
935   @param  TerminalDevice       Terminal driver private structure
936 
937   @retval TRUE                 If Raw Data FIFO buffer is full.
938   @retval FALSE                If Raw Data FIFO buffer is not full.
939 
940 **/
941 BOOLEAN
942 IsRawFiFoFull (
943   TERMINAL_DEV  *TerminalDevice
944   );
945 
946 /**
947   Insert one pre-fetched key into the FIFO buffer.
948 
949   @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.
950   @param  Input                 The key will be input.
951 
952   @retval TRUE                  If insert successfully.
953   @retval FALSE                 If FIFO buffer is full before key insertion,
954                                 and the key is lost.
955 
956 **/
957 BOOLEAN
958 EfiKeyFiFoForNotifyInsertOneKey (
959   EFI_KEY_FIFO                  *EfiKeyFiFo,
960   EFI_INPUT_KEY                 *Input
961   );
962 
963 /**
964   Remove one pre-fetched key out of the FIFO buffer.
965 
966   @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.
967   @param  Output                The key will be removed.
968 
969   @retval TRUE                  If insert successfully.
970   @retval FALSE                 If FIFO buffer is empty before remove operation.
971 
972 **/
973 BOOLEAN
974 EfiKeyFiFoForNotifyRemoveOneKey (
975   EFI_KEY_FIFO                  *EfiKeyFiFo,
976   EFI_INPUT_KEY                 *Output
977   );
978 
979 /**
980   Clarify whether FIFO buffer is empty.
981 
982   @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.
983 
984   @retval TRUE                  If FIFO buffer is empty.
985   @retval FALSE                 If FIFO buffer is not empty.
986 
987 **/
988 BOOLEAN
989 IsEfiKeyFiFoForNotifyEmpty (
990   IN EFI_KEY_FIFO               *EfiKeyFiFo
991   );
992 
993 /**
994   Clarify whether FIFO buffer is full.
995 
996   @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.
997 
998   @retval TRUE                  If FIFO buffer is full.
999   @retval FALSE                 If FIFO buffer is not full.
1000 
1001 **/
1002 BOOLEAN
1003 IsEfiKeyFiFoForNotifyFull (
1004   EFI_KEY_FIFO                  *EfiKeyFiFo
1005   );
1006 
1007 /**
1008   Insert one pre-fetched key into the FIFO buffer.
1009 
1010   @param  TerminalDevice       Terminal driver private structure.
1011   @param  Key                  The key will be input.
1012 
1013   @retval TRUE                 If insert successfully.
1014   @retval FALSE                If FIFO buffer is full before key insertion,
1015                                and the key is lost.
1016 
1017 **/
1018 BOOLEAN
1019 EfiKeyFiFoInsertOneKey (
1020   TERMINAL_DEV      *TerminalDevice,
1021   EFI_INPUT_KEY     *Key
1022   );
1023 
1024 /**
1025   Remove one pre-fetched key out of the FIFO buffer.
1026 
1027   @param  TerminalDevice       Terminal driver private structure.
1028   @param  Output               The key will be removed.
1029 
1030   @retval TRUE                 If insert successfully.
1031   @retval FALSE                If FIFO buffer is empty before remove operation.
1032 
1033 **/
1034 BOOLEAN
1035 EfiKeyFiFoRemoveOneKey (
1036   TERMINAL_DEV  *TerminalDevice,
1037   EFI_INPUT_KEY *Output
1038   );
1039 
1040 /**
1041   Clarify whether FIFO buffer is empty.
1042 
1043   @param  TerminalDevice       Terminal driver private structure
1044 
1045   @retval TRUE                 If FIFO buffer is empty.
1046   @retval FALSE                If FIFO buffer is not empty.
1047 
1048 **/
1049 BOOLEAN
1050 IsEfiKeyFiFoEmpty (
1051   TERMINAL_DEV  *TerminalDevice
1052   );
1053 
1054 /**
1055   Clarify whether FIFO buffer is full.
1056 
1057   @param  TerminalDevice       Terminal driver private structure
1058 
1059   @retval TRUE                 If FIFO buffer is full.
1060   @retval FALSE                If FIFO buffer is not full.
1061 
1062 **/
1063 BOOLEAN
1064 IsEfiKeyFiFoFull (
1065   TERMINAL_DEV  *TerminalDevice
1066   );
1067 
1068 /**
1069   Insert one pre-fetched key into the Unicode FIFO buffer.
1070 
1071   @param  TerminalDevice       Terminal driver private structure.
1072   @param  Input                The key will be input.
1073 
1074   @retval TRUE                 If insert successfully.
1075   @retval FALSE                If Unicode FIFO buffer is full before key insertion,
1076                                and the key is lost.
1077 
1078 **/
1079 BOOLEAN
1080 UnicodeFiFoInsertOneKey (
1081   TERMINAL_DEV      *TerminalDevice,
1082   UINT16            Input
1083   );
1084 
1085 /**
1086   Remove one pre-fetched key out of the Unicode FIFO buffer.
1087   The caller should guarantee that Unicode FIFO buffer is not empty
1088   by IsUnicodeFiFoEmpty ().
1089 
1090   @param  TerminalDevice       Terminal driver private structure.
1091   @param  Output               The key will be removed.
1092 
1093 **/
1094 VOID
1095 UnicodeFiFoRemoveOneKey (
1096   TERMINAL_DEV  *TerminalDevice,
1097   UINT16        *Output
1098   );
1099 
1100 /**
1101   Clarify whether Unicode FIFO buffer is empty.
1102 
1103   @param  TerminalDevice       Terminal driver private structure
1104 
1105   @retval TRUE                 If Unicode FIFO buffer is empty.
1106   @retval FALSE                If Unicode FIFO buffer is not empty.
1107 
1108 **/
1109 BOOLEAN
1110 IsUnicodeFiFoEmpty (
1111   TERMINAL_DEV  *TerminalDevice
1112   );
1113 
1114 /**
1115   Clarify whether Unicode FIFO buffer is full.
1116 
1117   @param  TerminalDevice       Terminal driver private structure
1118 
1119   @retval TRUE                 If Unicode FIFO buffer is full.
1120   @retval FALSE                If Unicode FIFO buffer is not full.
1121 
1122 **/
1123 BOOLEAN
1124 IsUnicodeFiFoFull (
1125   TERMINAL_DEV  *TerminalDevice
1126   );
1127 
1128 
1129 /**
1130   Translate raw data into Unicode (according to different encode), and
1131   translate Unicode into key information. (according to different standard).
1132 
1133   @param  TerminalDevice       Terminal driver private structure.
1134 
1135 **/
1136 VOID
1137 TranslateRawDataToEfiKey (
1138   IN  TERMINAL_DEV      *TerminalDevice
1139   );
1140 
1141 //
1142 // internal functions for PC ANSI
1143 //
1144 
1145 /**
1146   Translate all raw data in the Raw FIFI into unicode, and insert
1147   them into Unicode FIFO.
1148 
1149   @param TerminalDevice          The terminal device.
1150 
1151 **/
1152 VOID
1153 AnsiRawDataToUnicode (
1154   IN  TERMINAL_DEV    *TerminalDevice
1155   );
1156 
1157 /**
1158   Converts a stream of Unicode characters from a terminal input device into EFI Keys that
1159   can be read through the Simple Input Protocol.
1160 
1161   The table below shows the keyboard input mappings that this function supports.
1162   If the ESC sequence listed in one of the columns is presented, then it is translated
1163   into the coorespoding EFI Scan Code.  If a matching sequence is not found, then the raw
1164   key strokes are converted into EFI Keys.
1165 
1166   2 seconds are allowed for an ESC sequence to be completed.  If the ESC sequence is not
1167   completed in 2 seconds, then the raw key strokes of the partial ESC sequence are
1168   converted into EFI Keys.
1169   There is one special input sequence that will force the system to reset.
1170   This is ESC R ESC r ESC R.
1171 
1172   Symbols used in table below
1173   ===========================
1174     ESC = 0x1B
1175     CSI = 0x9B
1176     DEL = 0x7f
1177     ^   = CTRL
1178   +=========+======+===========+==========+==========+
1179   |         | EFI  | UEFI 2.0  |          |          |
1180   |         | Scan |           |  VT100+  |          |
1181   |   KEY   | Code |  PC ANSI  |  VTUTF8  |   VT100  |
1182   +=========+======+===========+==========+==========+
1183   | NULL    | 0x00 |           |          |          |
1184   | UP      | 0x01 | ESC [ A   | ESC [ A  | ESC [ A  |
1185   | DOWN    | 0x02 | ESC [ B   | ESC [ B  | ESC [ B  |
1186   | RIGHT   | 0x03 | ESC [ C   | ESC [ C  | ESC [ C  |
1187   | LEFT    | 0x04 | ESC [ D   | ESC [ D  | ESC [ D  |
1188   | HOME    | 0x05 | ESC [ H   | ESC h    | ESC [ H  |
1189   | END     | 0x06 | ESC [ F   | ESC k    | ESC [ K  |
1190   | INSERT  | 0x07 | ESC [ @   | ESC +    | ESC [ @  |
1191   |         |      | ESC [ L   |          | ESC [ L  |
1192   | DELETE  | 0x08 | ESC [ X   | ESC -    | ESC [ P  |
1193   | PG UP   | 0x09 | ESC [ I   | ESC ?    | ESC [ V  |
1194   |         |      |           |          | ESC [ ?  |
1195   | PG DOWN | 0x0A | ESC [ G   | ESC /    | ESC [ U  |
1196   |         |      |           |          | ESC [ /  |
1197   | F1      | 0x0B | ESC [ M   | ESC 1    | ESC O P  |
1198   | F2      | 0x0C | ESC [ N   | ESC 2    | ESC O Q  |
1199   | F3      | 0x0D | ESC [ O   | ESC 3    | ESC O w  |
1200   | F4      | 0x0E | ESC [ P   | ESC 4    | ESC O x  |
1201   | F5      | 0x0F | ESC [ Q   | ESC 5    | ESC O t  |
1202   | F6      | 0x10 | ESC [ R   | ESC 6    | ESC O u  |
1203   | F7      | 0x11 | ESC [ S   | ESC 7    | ESC O q  |
1204   | F8      | 0x12 | ESC [ T   | ESC 8    | ESC O r  |
1205   | F9      | 0x13 | ESC [ U   | ESC 9    | ESC O p  |
1206   | F10     | 0x14 | ESC [ V   | ESC 0    | ESC O M  |
1207   | Escape  | 0x17 | ESC       | ESC      | ESC      |
1208   | F11     | 0x15 |           | ESC !    |          |
1209   | F12     | 0x16 |           | ESC @    |          |
1210   +=========+======+===========+==========+==========+
1211 
1212   Special Mappings
1213   ================
1214   ESC R ESC r ESC R = Reset System
1215 
1216 
1217   @param TerminalDevice   The terminal device to use to translate raw input into EFI Keys
1218 
1219 **/
1220 VOID
1221 UnicodeToEfiKey (
1222   IN  TERMINAL_DEV    *TerminalDevice
1223   );
1224 
1225 /**
1226   Check if input string is valid Ascii string, valid EFI control characters
1227   or valid text graphics.
1228 
1229   @param  TerminalDevice          The terminal device.
1230   @param  WString                 The input string.
1231 
1232   @retval EFI_UNSUPPORTED         If not all input characters are valid.
1233   @retval EFI_SUCCESS             If all input characters are valid.
1234 
1235 **/
1236 EFI_STATUS
1237 AnsiTestString (
1238   IN  TERMINAL_DEV    *TerminalDevice,
1239   IN  CHAR16          *WString
1240   );
1241 
1242 //
1243 // internal functions for VTUTF8
1244 //
1245 
1246 /**
1247   Translate all VT-UTF8 characters in the Raw FIFI into unicode characters,
1248   and insert them into Unicode FIFO.
1249 
1250   @param VtUtf8Device          The terminal device.
1251 
1252 **/
1253 VOID
1254 VTUTF8RawDataToUnicode (
1255   IN  TERMINAL_DEV    *VtUtf8Device
1256   );
1257 
1258 /**
1259   Check if input string is valid VT-UTF8 string.
1260 
1261   @param  TerminalDevice          The terminal device.
1262   @param  WString                 The input string.
1263 
1264   @retval EFI_SUCCESS             If all input characters are valid.
1265 
1266 **/
1267 EFI_STATUS
1268 VTUTF8TestString (
1269   IN  TERMINAL_DEV    *TerminalDevice,
1270   IN  CHAR16          *WString
1271   );
1272 
1273 /**
1274   Translate one Unicode character into VT-UTF8 characters.
1275 
1276   UTF8 Encoding Table
1277   Bits per Character | Unicode Character Range | Unicode Binary  Encoding |  UTF8 Binary Encoding
1278         0-7           |     0x0000 - 0x007F      |     00000000 0xxxxxxx     |   0xxxxxxx
1279         8-11          |     0x0080 - 0x07FF      |     00000xxx xxxxxxxx     |   110xxxxx 10xxxxxx
1280        12-16          |     0x0800 - 0xFFFF      |     xxxxxxxx xxxxxxxx     |   1110xxxx 10xxxxxx 10xxxxxx
1281 
1282 
1283   @param  Unicode          Unicode character need translating.
1284   @param  Utf8Char         Return VT-UTF8 character set.
1285   @param  ValidBytes       The count of valid VT-UTF8 characters. If
1286                            ValidBytes is zero, no valid VT-UTF8 returned.
1287 
1288 **/
1289 VOID
1290 UnicodeToUtf8 (
1291   IN  CHAR16      Unicode,
1292   OUT UTF8_CHAR   *Utf8Char,
1293   OUT UINT8       *ValidBytes
1294   );
1295 
1296 /**
1297   Get one valid VT-UTF8 characters set from Raw Data FIFO.
1298 
1299   @param  Utf8Device          The terminal device.
1300   @param  Utf8Char            Returned valid VT-UTF8 characters set.
1301   @param  ValidBytes          The count of returned VT-VTF8 characters.
1302                               If ValidBytes is zero, no valid VT-UTF8 returned.
1303 
1304 **/
1305 VOID
1306 GetOneValidUtf8Char (
1307   IN  TERMINAL_DEV      *Utf8Device,
1308   OUT UTF8_CHAR         *Utf8Char,
1309   OUT UINT8             *ValidBytes
1310   );
1311 
1312 /**
1313   Translate VT-UTF8 characters into one Unicode character.
1314 
1315   UTF8 Encoding Table
1316   Bits per Character | Unicode Character Range | Unicode Binary  Encoding |  UTF8 Binary Encoding
1317         0-7           |     0x0000 - 0x007F      |     00000000 0xxxxxxx     |   0xxxxxxx
1318         8-11          |     0x0080 - 0x07FF      |     00000xxx xxxxxxxx     |   110xxxxx 10xxxxxx
1319        12-16          |     0x0800 - 0xFFFF      |     xxxxxxxx xxxxxxxx     |   1110xxxx 10xxxxxx 10xxxxxx
1320 
1321 
1322   @param  Utf8Char         VT-UTF8 character set needs translating.
1323   @param  ValidBytes       The count of valid VT-UTF8 characters.
1324   @param  UnicodeChar      Returned unicode character.
1325 
1326 **/
1327 VOID
1328 Utf8ToUnicode (
1329   IN  UTF8_CHAR       Utf8Char,
1330   IN  UINT8           ValidBytes,
1331   OUT CHAR16          *UnicodeChar
1332   );
1333 
1334 //
1335 // functions for boxdraw unicode
1336 //
1337 
1338 /**
1339   Detects if a Unicode char is for Box Drawing text graphics.
1340 
1341   @param  Graphic      Unicode char to test.
1342   @param  PcAnsi       Optional pointer to return PCANSI equivalent of
1343                        Graphic.
1344   @param  Ascii        Optional pointer to return ASCII equivalent of
1345                        Graphic.
1346 
1347   @retval TRUE         If Graphic is a supported Unicode Box Drawing character.
1348 
1349 **/
1350 BOOLEAN
1351 TerminalIsValidTextGraphics (
1352   IN  CHAR16  Graphic,
1353   OUT CHAR8   *PcAnsi, OPTIONAL
1354   OUT CHAR8   *Ascii OPTIONAL
1355   );
1356 
1357 /**
1358   Detects if a valid ASCII char.
1359 
1360   @param  Ascii        An ASCII character.
1361 
1362   @retval TRUE         If it is a valid ASCII character.
1363   @retval FALSE        If it is not a valid ASCII character.
1364 
1365 **/
1366 BOOLEAN
1367 TerminalIsValidAscii (
1368   IN  CHAR16  Ascii
1369   );
1370 
1371 /**
1372   Detects if a valid EFI control character.
1373 
1374   @param  CharC        An input EFI Control character.
1375 
1376   @retval TRUE         If it is a valid EFI control character.
1377   @retval FALSE        If it is not a valid EFI control character.
1378 
1379 **/
1380 BOOLEAN
1381 TerminalIsValidEfiCntlChar (
1382   IN  CHAR16  CharC
1383   );
1384 
1385 /**
1386   Check if the device supports hot-plug through its device path.
1387 
1388   This function could be updated to check more types of Hot Plug devices.
1389   Currently, it checks USB and PCCard device.
1390 
1391   @param  DevicePath            Pointer to device's device path.
1392 
1393   @retval TRUE                  The devcie is a hot-plug device
1394   @retval FALSE                 The devcie is not a hot-plug device.
1395 
1396 **/
1397 BOOLEAN
1398 IsHotPlugDevice (
1399   IN  EFI_DEVICE_PATH_PROTOCOL    *DevicePath
1400   );
1401 
1402 /**
1403   Timer handler to poll the key from serial.
1404 
1405   @param  Event                    Indicates the event that invoke this function.
1406   @param  Context                  Indicates the calling context.
1407 **/
1408 VOID
1409 EFIAPI
1410 TerminalConInTimerHandler (
1411   IN EFI_EVENT            Event,
1412   IN VOID                 *Context
1413   );
1414 
1415 
1416 /**
1417   Process key notify.
1418 
1419   @param  Event                 Indicates the event that invoke this function.
1420   @param  Context               Indicates the calling context.
1421 **/
1422 VOID
1423 EFIAPI
1424 KeyNotifyProcessHandler (
1425   IN  EFI_EVENT                 Event,
1426   IN  VOID                      *Context
1427   );
1428 
1429 #endif
1430