2 Produces Simple Text Input Protocol, Simple Text Input Extended Protocol and
3 Simple Text Output Protocol upon Serial IO Protocol.
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 EFI_DRIVER_BINDING_PROTOCOL gTerminalDriverBinding
= {
23 TerminalDriverBindingSupported
,
24 TerminalDriverBindingStart
,
25 TerminalDriverBindingStop
,
32 EFI_GUID
*mTerminalType
[] = {
41 CHAR16
*mSerialConsoleNames
[] = {
42 L
"PC-ANSI Serial Console",
43 L
"VT-100 Serial Console",
44 L
"VT-100+ Serial Console",
45 L
"VT-UTF8 Serial Console",
46 L
"Tty Terminal Serial Console"
49 TERMINAL_DEV mTerminalDevTemplate
= {
50 TERMINAL_DEV_SIGNATURE
,
57 TerminalConInReadKeyStroke
,
62 TerminalConOutOutputString
,
63 TerminalConOutTestString
,
64 TerminalConOutQueryMode
,
65 TerminalConOutSetMode
,
66 TerminalConOutSetAttribute
,
67 TerminalConOutClearScreen
,
68 TerminalConOutSetCursorPosition
,
69 TerminalConOutEnableCursor
,
72 { // SimpleTextOutputMode
75 EFI_TEXT_ATTR (EFI_LIGHTGRAY
, EFI_BLACK
), // Attribute
80 NULL
, // TerminalConsoleModeData
86 NULL
, // EfiKeyFiFoForNotify
88 NULL
, // ControllerNameTable
90 NULL
, // TwoSecondTimeOut
100 { // SimpleTextInputEx
101 TerminalConInResetEx
,
102 TerminalConInReadKeyStrokeEx
,
104 TerminalConInSetState
,
105 TerminalConInRegisterKeyNotify
,
106 TerminalConInUnregisterKeyNotify
,
112 NULL
// KeyNotifyProcessEvent
115 TERMINAL_CONSOLE_MODE_DATA mTerminalConsoleModeData
[] = {
120 // New modes can be added here.
125 Convert the GUID representation of terminal type to enum type.
127 @param Guid The GUID representation of terminal type.
129 @return The terminal type in enum type.
132 TerminalTypeFromGuid (
138 for (Type
= 0; Type
< ARRAY_SIZE (mTerminalType
); Type
++) {
139 if (CompareGuid (Guid
, mTerminalType
[Type
])) {
147 Test to see if this driver supports Controller.
149 @param This Protocol instance pointer.
150 @param Controller Handle of device to test
151 @param RemainingDevicePath Optional parameter use to pick a specific child
154 @retval EFI_SUCCESS This driver supports this device.
155 @retval EFI_ALREADY_STARTED This driver is already running on this device.
156 @retval other This driver does not support this device.
161 TerminalDriverBindingSupported (
162 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
163 IN EFI_HANDLE Controller
,
164 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
168 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
169 EFI_SERIAL_IO_PROTOCOL
*SerialIo
;
170 VENDOR_DEVICE_PATH
*Node
;
173 // If remaining device path is not NULL, then make sure it is a
174 // device path that describes a terminal communications protocol.
176 if (RemainingDevicePath
!= NULL
) {
178 // Check if RemainingDevicePath is the End of Device Path Node,
179 // if yes, go on checking other conditions
181 if (!IsDevicePathEnd (RemainingDevicePath
)) {
183 // If RemainingDevicePath isn't the End of Device Path Node,
184 // check its validation
186 Node
= (VENDOR_DEVICE_PATH
*) RemainingDevicePath
;
188 if (Node
->Header
.Type
!= MESSAGING_DEVICE_PATH
||
189 Node
->Header
.SubType
!= MSG_VENDOR_DP
||
190 DevicePathNodeLength(&Node
->Header
) != sizeof(VENDOR_DEVICE_PATH
)) {
192 return EFI_UNSUPPORTED
;
196 // only supports PC ANSI, VT100, VT100+, VT-UTF8, and TtyTerm terminal types
198 if (TerminalTypeFromGuid (&Node
->Guid
) == ARRAY_SIZE (mTerminalType
)) {
199 return EFI_UNSUPPORTED
;
204 // Open the IO Abstraction(s) needed to perform the supported test
205 // The Controller must support the Serial I/O Protocol.
206 // This driver is a bus driver with at most 1 child device, so it is
207 // ok for it to be already started.
209 Status
= gBS
->OpenProtocol (
211 &gEfiSerialIoProtocolGuid
,
213 This
->DriverBindingHandle
,
215 EFI_OPEN_PROTOCOL_BY_DRIVER
217 if (Status
== EFI_ALREADY_STARTED
) {
221 if (EFI_ERROR (Status
)) {
226 // Close the I/O Abstraction(s) used to perform the supported test
230 &gEfiSerialIoProtocolGuid
,
231 This
->DriverBindingHandle
,
236 // Open the EFI Device Path protocol needed to perform the supported test
238 Status
= gBS
->OpenProtocol (
240 &gEfiDevicePathProtocolGuid
,
241 (VOID
**) &ParentDevicePath
,
242 This
->DriverBindingHandle
,
244 EFI_OPEN_PROTOCOL_BY_DRIVER
246 if (Status
== EFI_ALREADY_STARTED
) {
250 if (EFI_ERROR (Status
)) {
255 // Close protocol, don't use device path protocol in the Support() function
259 &gEfiDevicePathProtocolGuid
,
260 This
->DriverBindingHandle
,
269 Free notify functions list.
271 @param ListHead The list head
273 @retval EFI_SUCCESS Free the notify list successfully.
274 @retval EFI_INVALID_PARAMETER ListHead is NULL.
278 TerminalFreeNotifyList (
279 IN OUT LIST_ENTRY
*ListHead
282 TERMINAL_CONSOLE_IN_EX_NOTIFY
*NotifyNode
;
284 if (ListHead
== NULL
) {
285 return EFI_INVALID_PARAMETER
;
287 while (!IsListEmpty (ListHead
)) {
289 ListHead
->ForwardLink
,
290 TERMINAL_CONSOLE_IN_EX_NOTIFY
,
292 TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE
294 RemoveEntryList (ListHead
->ForwardLink
);
295 FreePool (NotifyNode
);
302 Initialize all the text modes which the terminal console supports.
304 It returns information for available text modes that the terminal can support.
306 @param[out] TextModeCount The total number of text modes that terminal console supports.
308 @return The buffer to the text modes column and row information.
309 Caller is responsible to free it when it's non-NULL.
312 TERMINAL_CONSOLE_MODE_DATA
*
313 InitializeTerminalConsoleTextMode (
314 OUT INT32
*TextModeCount
317 TERMINAL_CONSOLE_MODE_DATA
*TextModeData
;
319 ASSERT (TextModeCount
!= NULL
);
321 TextModeData
= AllocateCopyPool (sizeof (mTerminalConsoleModeData
), mTerminalConsoleModeData
);
322 if (TextModeData
== NULL
) {
325 *TextModeCount
= ARRAY_SIZE (mTerminalConsoleModeData
);
329 for (Index
= 0; Index
< *TextModeCount
; Index
++) {
330 DEBUG ((DEBUG_INFO
, "Terminal - Mode %d, Column = %d, Row = %d\n",
331 Index
, TextModeData
[Index
].Columns
, TextModeData
[Index
].Rows
));
338 Stop the terminal state machine.
340 @param TerminalDevice The terminal device.
343 StopTerminalStateMachine (
344 TERMINAL_DEV
*TerminalDevice
349 OriginalTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
351 gBS
->CloseEvent (TerminalDevice
->TimerEvent
);
352 gBS
->CloseEvent (TerminalDevice
->TwoSecondTimeOut
);
354 gBS
->RestoreTPL (OriginalTpl
);
358 Start the terminal state machine.
360 @param TerminalDevice The terminal device.
363 StartTerminalStateMachine (
364 TERMINAL_DEV
*TerminalDevice
368 Status
= gBS
->CreateEvent (
369 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
371 TerminalConInTimerHandler
,
373 &TerminalDevice
->TimerEvent
375 ASSERT_EFI_ERROR (Status
);
377 Status
= gBS
->SetTimer (
378 TerminalDevice
->TimerEvent
,
380 KEYBOARD_TIMER_INTERVAL
382 ASSERT_EFI_ERROR (Status
);
384 Status
= gBS
->CreateEvent (
389 &TerminalDevice
->TwoSecondTimeOut
391 ASSERT_EFI_ERROR (Status
);
395 Initialize the controller name table.
397 @param TerminalType The terminal type.
398 @param ControllerNameTable The controller name table.
400 @retval EFI_SUCCESS The controller name table is initialized successfully.
401 @retval others Return status of AddUnicodeString2 ().
404 InitializeControllerNameTable (
405 TERMINAL_TYPE TerminalType
,
406 EFI_UNICODE_STRING_TABLE
**ControllerNameTable
410 EFI_UNICODE_STRING_TABLE
*Table
;
412 ASSERT (TerminalType
< ARRAY_SIZE (mTerminalType
));
414 Status
= AddUnicodeString2 (
416 gTerminalComponentName
.SupportedLanguages
,
418 mSerialConsoleNames
[TerminalType
],
421 if (!EFI_ERROR (Status
)) {
422 Status
= AddUnicodeString2 (
424 gTerminalComponentName2
.SupportedLanguages
,
426 mSerialConsoleNames
[TerminalType
],
429 if (EFI_ERROR (Status
)) {
430 FreeUnicodeStringTable (Table
);
433 if (!EFI_ERROR (Status
)) {
434 *ControllerNameTable
= Table
;
440 Start this driver on Controller by opening a Serial IO protocol,
441 reading Device Path, and creating a child handle with a Simple Text In,
442 Simple Text In Ex and Simple Text Out protocol, and device path protocol.
443 And store Console Device Environment Variables.
445 @param This Protocol instance pointer.
446 @param Controller Handle of device to bind driver to
447 @param RemainingDevicePath Optional parameter use to pick a specific child
450 @retval EFI_SUCCESS This driver is added to Controller.
451 @retval EFI_ALREADY_STARTED This driver is already running on Controller.
452 @retval other This driver does not support this device.
457 TerminalDriverBindingStart (
458 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
459 IN EFI_HANDLE Controller
,
460 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
464 EFI_SERIAL_IO_PROTOCOL
*SerialIo
;
465 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
466 EFI_DEVICE_PATH_PROTOCOL
*Vendor
;
467 EFI_HANDLE SerialIoHandle
;
468 EFI_SERIAL_IO_MODE
*Mode
;
469 UINTN SerialInTimeOut
;
470 TERMINAL_DEV
*TerminalDevice
;
472 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfoBuffer
;
475 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*SimpleTextOutput
;
476 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*SimpleTextInput
;
477 EFI_UNICODE_STRING_TABLE
*ControllerNameTable
;
480 // Get the Device Path Protocol to build the device path of the child device
482 Status
= gBS
->OpenProtocol (
484 &gEfiDevicePathProtocolGuid
,
485 (VOID
**) &ParentDevicePath
,
486 This
->DriverBindingHandle
,
488 EFI_OPEN_PROTOCOL_BY_DRIVER
490 ASSERT ((Status
== EFI_SUCCESS
) || (Status
== EFI_ALREADY_STARTED
));
491 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
496 // Open the Serial I/O Protocol BY_DRIVER. It might already be started.
498 Status
= gBS
->OpenProtocol (
500 &gEfiSerialIoProtocolGuid
,
502 This
->DriverBindingHandle
,
504 EFI_OPEN_PROTOCOL_BY_DRIVER
506 ASSERT ((Status
== EFI_SUCCESS
) || (Status
== EFI_ALREADY_STARTED
));
507 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
511 if (!IsHotPlugDevice (ParentDevicePath
)) {
513 // if the serial device is a hot plug device, do not update the
514 // ConInDev, ConOutDev, and StdErrDev variables.
516 TerminalUpdateConsoleDevVariable (EFI_CON_IN_DEV_VARIABLE_NAME
, ParentDevicePath
);
517 TerminalUpdateConsoleDevVariable (EFI_CON_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
518 TerminalUpdateConsoleDevVariable (EFI_ERR_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
522 // Do not create any child for END remaining device path.
524 if ((RemainingDevicePath
!= NULL
) && IsDevicePathEnd (RemainingDevicePath
)) {
528 if (Status
== EFI_ALREADY_STARTED
) {
530 if (RemainingDevicePath
== NULL
) {
532 // If RemainingDevicePath is NULL or is the End of Device Path Node
538 // This driver can only produce one child per serial port.
539 // Change its terminal type as remaining device path requests.
541 Status
= gBS
->OpenProtocolInformation (
543 &gEfiSerialIoProtocolGuid
,
547 if (!EFI_ERROR (Status
)) {
548 Status
= EFI_NOT_FOUND
;
549 for (Index
= 0; Index
< EntryCount
; Index
++) {
550 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
551 Status
= gBS
->OpenProtocol (
552 OpenInfoBuffer
[Index
].ControllerHandle
,
553 &gEfiSimpleTextInProtocolGuid
,
554 (VOID
**) &SimpleTextInput
,
555 This
->DriverBindingHandle
,
557 EFI_OPEN_PROTOCOL_GET_PROTOCOL
559 if (!EFI_ERROR (Status
)) {
560 TerminalDevice
= TERMINAL_CON_IN_DEV_FROM_THIS (SimpleTextInput
);
561 TerminalType
= TerminalTypeFromGuid (&((VENDOR_DEVICE_PATH
*) RemainingDevicePath
)->Guid
);
562 ASSERT (TerminalType
< ARRAY_SIZE (mTerminalType
));
563 if (TerminalDevice
->TerminalType
!= TerminalType
) {
564 Status
= InitializeControllerNameTable (TerminalType
, &ControllerNameTable
);
565 if (!EFI_ERROR (Status
)) {
566 StopTerminalStateMachine (TerminalDevice
);
568 // Update the device path
570 Vendor
= TerminalDevice
->DevicePath
;
571 Status
= gBS
->LocateDevicePath (&gEfiSerialIoProtocolGuid
, &Vendor
, &SerialIoHandle
);
572 ASSERT_EFI_ERROR (Status
);
573 CopyGuid (&((VENDOR_DEVICE_PATH
*) Vendor
)->Guid
, mTerminalType
[TerminalType
]);
574 Status
= gBS
->ReinstallProtocolInterface (
575 TerminalDevice
->Handle
,
576 &gEfiDevicePathProtocolGuid
,
577 TerminalDevice
->DevicePath
,
578 TerminalDevice
->DevicePath
580 if (!EFI_ERROR (Status
)) {
581 TerminalDevice
->TerminalType
= TerminalType
;
582 StartTerminalStateMachine (TerminalDevice
);
583 FreeUnicodeStringTable (TerminalDevice
->ControllerNameTable
);
584 TerminalDevice
->ControllerNameTable
= ControllerNameTable
;
587 // Restore the device path on failure
589 CopyGuid (&((VENDOR_DEVICE_PATH
*) Vendor
)->Guid
, mTerminalType
[TerminalDevice
->TerminalType
]);
590 FreeUnicodeStringTable (ControllerNameTable
);
598 FreePool (OpenInfoBuffer
);
604 // Initialize the Terminal Dev
606 TerminalDevice
= AllocateCopyPool (sizeof (TERMINAL_DEV
), &mTerminalDevTemplate
);
607 if (TerminalDevice
== NULL
) {
608 Status
= EFI_OUT_OF_RESOURCES
;
612 if (RemainingDevicePath
== NULL
) {
614 // If RemainingDevicePath is NULL, use default terminal type
616 TerminalDevice
->TerminalType
= PcdGet8 (PcdDefaultTerminalType
);
619 // End of Device Path Node is handled in above.
621 ASSERT (!IsDevicePathEnd (RemainingDevicePath
));
623 // If RemainingDevicePath isn't the End of Device Path Node,
624 // Use the RemainingDevicePath to determine the terminal type
626 TerminalDevice
->TerminalType
= TerminalTypeFromGuid (&((VENDOR_DEVICE_PATH
*) RemainingDevicePath
)->Guid
);
628 ASSERT (TerminalDevice
->TerminalType
< ARRAY_SIZE (mTerminalType
));
629 TerminalDevice
->SerialIo
= SerialIo
;
632 // Build the component name for the child device
634 Status
= InitializeControllerNameTable (TerminalDevice
->TerminalType
, &TerminalDevice
->ControllerNameTable
);
635 if (EFI_ERROR (Status
)) {
640 // Build the device path for the child device
642 Status
= SetTerminalDevicePath (TerminalDevice
->TerminalType
, ParentDevicePath
, &TerminalDevice
->DevicePath
);
643 if (EFI_ERROR (Status
)) {
647 InitializeListHead (&TerminalDevice
->NotifyList
);
648 Status
= gBS
->CreateEvent (
651 TerminalConInWaitForKeyEx
,
653 &TerminalDevice
->SimpleInputEx
.WaitForKeyEx
655 ASSERT_EFI_ERROR (Status
);
657 Status
= gBS
->CreateEvent (
660 TerminalConInWaitForKey
,
662 &TerminalDevice
->SimpleInput
.WaitForKey
664 ASSERT_EFI_ERROR (Status
);
665 Status
= gBS
->CreateEvent (
668 KeyNotifyProcessHandler
,
670 &TerminalDevice
->KeyNotifyProcessEvent
672 ASSERT_EFI_ERROR (Status
);
675 // Allocates and initializes the FIFO buffer to be zero, used for accommodating
676 // the pre-read pending characters.
678 TerminalDevice
->RawFiFo
= AllocateZeroPool (sizeof (RAW_DATA_FIFO
));
679 if (TerminalDevice
->RawFiFo
== NULL
) {
682 TerminalDevice
->UnicodeFiFo
= AllocateZeroPool (sizeof (UNICODE_FIFO
));
683 if (TerminalDevice
->UnicodeFiFo
== NULL
) {
686 TerminalDevice
->EfiKeyFiFo
= AllocateZeroPool (sizeof (EFI_KEY_FIFO
));
687 if (TerminalDevice
->EfiKeyFiFo
== NULL
) {
690 TerminalDevice
->EfiKeyFiFoForNotify
= AllocateZeroPool (sizeof (EFI_KEY_FIFO
));
691 if (TerminalDevice
->EfiKeyFiFoForNotify
== NULL
) {
696 // Set the timeout value of serial buffer for keystroke response performance issue
698 Mode
= TerminalDevice
->SerialIo
->Mode
;
701 if (Mode
->BaudRate
!= 0) {
702 SerialInTimeOut
= (1 + Mode
->DataBits
+ Mode
->StopBits
) * 2 * 1000000 / (UINTN
) Mode
->BaudRate
;
705 Status
= TerminalDevice
->SerialIo
->SetAttributes (
706 TerminalDevice
->SerialIo
,
708 Mode
->ReceiveFifoDepth
,
709 (UINT32
) SerialInTimeOut
,
710 (EFI_PARITY_TYPE
) (Mode
->Parity
),
711 (UINT8
) Mode
->DataBits
,
712 (EFI_STOP_BITS_TYPE
) (Mode
->StopBits
)
714 if (EFI_ERROR (Status
)) {
716 // if set attributes operation fails, invalidate
717 // the value of SerialInTimeOut,thus make it
718 // inconsistent with the default timeout value
719 // of serial buffer. This will invoke the recalculation
720 // in the readkeystroke routine.
722 TerminalDevice
->SerialInTimeOut
= 0;
724 TerminalDevice
->SerialInTimeOut
= SerialInTimeOut
;
727 SimpleTextOutput
= &TerminalDevice
->SimpleTextOutput
;
728 SimpleTextInput
= &TerminalDevice
->SimpleInput
;
731 // Initialize SimpleTextOut instance
733 SimpleTextOutput
->Mode
= &TerminalDevice
->SimpleTextOutputMode
;
734 TerminalDevice
->TerminalConsoleModeData
= InitializeTerminalConsoleTextMode (
735 &SimpleTextOutput
->Mode
->MaxMode
737 if (TerminalDevice
->TerminalConsoleModeData
== NULL
) {
741 // For terminal devices, cursor is always visible
743 SimpleTextOutput
->Mode
->CursorVisible
= TRUE
;
744 Status
= SimpleTextOutput
->SetAttribute (SimpleTextOutput
, EFI_TEXT_ATTR (EFI_LIGHTGRAY
, EFI_BLACK
));
745 if (!EFI_ERROR (Status
)) {
746 Status
= SimpleTextOutput
->Reset (SimpleTextOutput
, FALSE
);
748 if (EFI_ERROR (Status
)) {
753 // Initialize SimpleTextInput instance
755 Status
= SimpleTextInput
->Reset (SimpleTextInput
, FALSE
);
756 if (EFI_ERROR (Status
)) {
760 Status
= gBS
->InstallMultipleProtocolInterfaces (
761 &TerminalDevice
->Handle
,
762 &gEfiSimpleTextInProtocolGuid
, &TerminalDevice
->SimpleInput
,
763 &gEfiSimpleTextInputExProtocolGuid
, &TerminalDevice
->SimpleInputEx
,
764 &gEfiSimpleTextOutProtocolGuid
, &TerminalDevice
->SimpleTextOutput
,
765 &gEfiDevicePathProtocolGuid
, TerminalDevice
->DevicePath
,
768 if (!EFI_ERROR (Status
)) {
769 Status
= gBS
->OpenProtocol (
771 &gEfiSerialIoProtocolGuid
,
772 (VOID
**) &TerminalDevice
->SerialIo
,
773 This
->DriverBindingHandle
,
774 TerminalDevice
->Handle
,
775 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
777 ASSERT_EFI_ERROR (Status
);
778 StartTerminalStateMachine (TerminalDevice
);
783 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
784 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
785 (EFI_PERIPHERAL_REMOTE_CONSOLE
| EFI_P_EC_CONTROLLER_ERROR
),
790 ASSERT (TerminalDevice
!= NULL
);
792 if (TerminalDevice
->SimpleInput
.WaitForKey
!= NULL
) {
793 gBS
->CloseEvent (TerminalDevice
->SimpleInput
.WaitForKey
);
795 if (TerminalDevice
->SimpleInputEx
.WaitForKeyEx
!= NULL
) {
796 gBS
->CloseEvent (TerminalDevice
->SimpleInputEx
.WaitForKeyEx
);
798 if (TerminalDevice
->KeyNotifyProcessEvent
!= NULL
) {
799 gBS
->CloseEvent (TerminalDevice
->KeyNotifyProcessEvent
);
802 if (TerminalDevice
->RawFiFo
!= NULL
) {
803 FreePool (TerminalDevice
->RawFiFo
);
805 if (TerminalDevice
->UnicodeFiFo
!= NULL
) {
806 FreePool (TerminalDevice
->UnicodeFiFo
);
808 if (TerminalDevice
->EfiKeyFiFo
!= NULL
) {
809 FreePool (TerminalDevice
->EfiKeyFiFo
);
811 if (TerminalDevice
->EfiKeyFiFoForNotify
!= NULL
) {
812 FreePool (TerminalDevice
->EfiKeyFiFoForNotify
);
815 if (TerminalDevice
->ControllerNameTable
!= NULL
) {
816 FreeUnicodeStringTable (TerminalDevice
->ControllerNameTable
);
819 if (TerminalDevice
->DevicePath
!= NULL
) {
820 FreePool (TerminalDevice
->DevicePath
);
823 if (TerminalDevice
->TerminalConsoleModeData
!= NULL
) {
824 FreePool (TerminalDevice
->TerminalConsoleModeData
);
827 FreePool (TerminalDevice
);
832 // Remove Parent Device Path from
833 // the Console Device Environment Variables
835 TerminalRemoveConsoleDevVariable (EFI_CON_IN_DEV_VARIABLE_NAME
, ParentDevicePath
);
836 TerminalRemoveConsoleDevVariable (EFI_CON_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
837 TerminalRemoveConsoleDevVariable (EFI_ERR_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
839 Status
= gBS
->CloseProtocol (
841 &gEfiSerialIoProtocolGuid
,
842 This
->DriverBindingHandle
,
845 ASSERT_EFI_ERROR (Status
);
847 Status
= gBS
->CloseProtocol (
849 &gEfiDevicePathProtocolGuid
,
850 This
->DriverBindingHandle
,
853 ASSERT_EFI_ERROR (Status
);
858 Stop this driver on Controller by closing Simple Text In, Simple Text
859 In Ex, Simple Text Out protocol, and removing parent device path from
860 Console Device Environment Variables.
862 @param This Protocol instance pointer.
863 @param Controller Handle of device to stop driver on
864 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
865 children is zero stop the entire bus driver.
866 @param ChildHandleBuffer List of Child Handles to Stop.
868 @retval EFI_SUCCESS This driver is removed Controller.
869 @retval other This driver could not be removed from this device.
874 TerminalDriverBindingStop (
875 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
876 IN EFI_HANDLE Controller
,
877 IN UINTN NumberOfChildren
,
878 IN EFI_HANDLE
*ChildHandleBuffer
883 BOOLEAN AllChildrenStopped
;
884 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*SimpleTextOutput
;
885 TERMINAL_DEV
*TerminalDevice
;
886 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
887 EFI_SERIAL_IO_PROTOCOL
*SerialIo
;
890 // Complete all outstanding transactions to Controller.
891 // Don't allow any new transaction to Controller to be started.
893 if (NumberOfChildren
== 0) {
895 // Close the bus driver
897 Status
= gBS
->OpenProtocol (
899 &gEfiDevicePathProtocolGuid
,
900 (VOID
**) &ParentDevicePath
,
901 This
->DriverBindingHandle
,
903 EFI_OPEN_PROTOCOL_GET_PROTOCOL
905 ASSERT_EFI_ERROR (Status
);
908 // Remove Parent Device Path from
909 // the Console Device Environment Variables
911 TerminalRemoveConsoleDevVariable (EFI_CON_IN_DEV_VARIABLE_NAME
, ParentDevicePath
);
912 TerminalRemoveConsoleDevVariable (EFI_CON_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
913 TerminalRemoveConsoleDevVariable (EFI_ERR_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
917 &gEfiSerialIoProtocolGuid
,
918 This
->DriverBindingHandle
,
924 &gEfiDevicePathProtocolGuid
,
925 This
->DriverBindingHandle
,
932 AllChildrenStopped
= TRUE
;
934 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
936 Status
= gBS
->OpenProtocol (
937 ChildHandleBuffer
[Index
],
938 &gEfiSimpleTextOutProtocolGuid
,
939 (VOID
**) &SimpleTextOutput
,
940 This
->DriverBindingHandle
,
941 ChildHandleBuffer
[Index
],
942 EFI_OPEN_PROTOCOL_GET_PROTOCOL
944 if (!EFI_ERROR (Status
)) {
946 TerminalDevice
= TERMINAL_CON_OUT_DEV_FROM_THIS (SimpleTextOutput
);
950 &gEfiSerialIoProtocolGuid
,
951 This
->DriverBindingHandle
,
952 ChildHandleBuffer
[Index
]
955 Status
= gBS
->UninstallMultipleProtocolInterfaces (
956 ChildHandleBuffer
[Index
],
957 &gEfiSimpleTextInProtocolGuid
,
958 &TerminalDevice
->SimpleInput
,
959 &gEfiSimpleTextInputExProtocolGuid
,
960 &TerminalDevice
->SimpleInputEx
,
961 &gEfiSimpleTextOutProtocolGuid
,
962 &TerminalDevice
->SimpleTextOutput
,
963 &gEfiDevicePathProtocolGuid
,
964 TerminalDevice
->DevicePath
,
967 if (EFI_ERROR (Status
)) {
970 &gEfiSerialIoProtocolGuid
,
972 This
->DriverBindingHandle
,
973 ChildHandleBuffer
[Index
],
974 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
978 FreeUnicodeStringTable (TerminalDevice
->ControllerNameTable
);
979 StopTerminalStateMachine (TerminalDevice
);
980 gBS
->CloseEvent (TerminalDevice
->SimpleInput
.WaitForKey
);
981 gBS
->CloseEvent (TerminalDevice
->SimpleInputEx
.WaitForKeyEx
);
982 gBS
->CloseEvent (TerminalDevice
->KeyNotifyProcessEvent
);
983 TerminalFreeNotifyList (&TerminalDevice
->NotifyList
);
984 FreePool (TerminalDevice
->DevicePath
);
985 FreePool (TerminalDevice
->TerminalConsoleModeData
);
986 FreePool (TerminalDevice
);
990 if (EFI_ERROR (Status
)) {
991 AllChildrenStopped
= FALSE
;
995 if (!AllChildrenStopped
) {
996 return EFI_DEVICE_ERROR
;
1003 Compare a device path data structure to that of all the nodes of a
1004 second device path instance.
1006 @param Multi A pointer to a multi-instance device path data structure.
1007 @param Single A pointer to a single-instance device path data structure.
1009 @retval TRUE If the Single is contained within Multi.
1010 @retval FALSE The Single is not match within Multi.
1015 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
1016 IN EFI_DEVICE_PATH_PROTOCOL
*Single
1019 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1020 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
1024 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
1026 // Search for the match of 'Single' in 'Multi'
1028 while (DevicePathInst
!= NULL
) {
1030 // If the single device path is found in multiple device paths,
1033 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
1034 FreePool (DevicePathInst
);
1038 FreePool (DevicePathInst
);
1039 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
1046 Update terminal device path in Console Device Environment Variables.
1048 @param VariableName The Console Device Environment Variable.
1049 @param ParentDevicePath The terminal device path to be updated.
1053 TerminalUpdateConsoleDevVariable (
1054 IN CHAR16
*VariableName
,
1055 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
1061 TERMINAL_TYPE TerminalType
;
1062 EFI_DEVICE_PATH_PROTOCOL
*Variable
;
1063 EFI_DEVICE_PATH_PROTOCOL
*NewVariable
;
1064 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1065 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
1068 // Get global variable and its size according to the name given.
1070 Status
= GetEfiGlobalVariable2 (VariableName
, (VOID
**)&Variable
, NULL
);
1071 if (Status
== EFI_NOT_FOUND
) {
1072 Status
= EFI_SUCCESS
;
1075 if (EFI_ERROR (Status
)) {
1080 // Append terminal device path onto the variable.
1082 for (TerminalType
= 0; TerminalType
< ARRAY_SIZE (mTerminalType
); TerminalType
++) {
1083 SetTerminalDevicePath (TerminalType
, ParentDevicePath
, &TempDevicePath
);
1085 if (TempDevicePath
!= NULL
) {
1086 if (!MatchDevicePaths (Variable
, TempDevicePath
)) {
1087 NewVariable
= AppendDevicePathInstance (Variable
, TempDevicePath
);
1088 if (NewVariable
!= NULL
) {
1089 if (Variable
!= NULL
) {
1090 FreePool (Variable
);
1092 Variable
= NewVariable
;
1096 FreePool (TempDevicePath
);
1101 VariableSize
= GetDevicePathSize (Variable
);
1103 Status
= gRT
->SetVariable (
1105 &gEfiGlobalVariableGuid
,
1106 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1111 if (EFI_ERROR (Status
)) {
1112 NameSize
= StrSize (VariableName
);
1113 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ VariableSize
);
1114 if (SetVariableStatus
!= NULL
) {
1115 CopyGuid (&SetVariableStatus
->Guid
, &gEfiGlobalVariableGuid
);
1116 SetVariableStatus
->NameSize
= NameSize
;
1117 SetVariableStatus
->DataSize
= VariableSize
;
1118 SetVariableStatus
->SetStatus
= Status
;
1119 SetVariableStatus
->Attributes
= EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1120 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
1121 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Variable
, VariableSize
);
1123 REPORT_STATUS_CODE_EX (
1125 PcdGet32 (PcdErrorCodeSetVariable
),
1128 &gEdkiiStatusCodeDataTypeVariableGuid
,
1130 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ VariableSize
1133 FreePool (SetVariableStatus
);
1137 FreePool (Variable
);
1144 Remove terminal device path from Console Device Environment Variables.
1146 @param VariableName Console Device Environment Variables.
1147 @param ParentDevicePath The terminal device path to be updated.
1151 TerminalRemoveConsoleDevVariable (
1152 IN CHAR16
*VariableName
,
1153 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
1161 TERMINAL_TYPE TerminalType
;
1162 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
1163 EFI_DEVICE_PATH_PROTOCOL
*Variable
;
1164 EFI_DEVICE_PATH_PROTOCOL
*OriginalVariable
;
1165 EFI_DEVICE_PATH_PROTOCOL
*NewVariable
;
1166 EFI_DEVICE_PATH_PROTOCOL
*SavedNewVariable
;
1167 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1172 // Get global variable and its size according to the name given.
1174 GetEfiGlobalVariable2 (VariableName
, (VOID
**)&Variable
, NULL
);
1175 if (Variable
== NULL
) {
1180 OriginalVariable
= Variable
;
1184 // Get first device path instance from Variable
1186 Instance
= GetNextDevicePathInstance (&Variable
, &InstanceSize
);
1187 if (Instance
== NULL
) {
1188 FreePool (OriginalVariable
);
1192 // Loop through all the device path instances of Variable
1196 // Loop through all the terminal types that this driver supports
1199 for (TerminalType
= 0; TerminalType
< ARRAY_SIZE (mTerminalType
); TerminalType
++) {
1201 SetTerminalDevicePath (TerminalType
, ParentDevicePath
, &TempDevicePath
);
1204 // Compare the generated device path to the current device path instance
1206 if (TempDevicePath
!= NULL
) {
1207 if (CompareMem (Instance
, TempDevicePath
, InstanceSize
) == 0) {
1212 FreePool (TempDevicePath
);
1216 // If a match was not found, then keep the current device path instance
1219 SavedNewVariable
= NewVariable
;
1220 NewVariable
= AppendDevicePathInstance (NewVariable
, Instance
);
1221 if (SavedNewVariable
!= NULL
) {
1222 FreePool (SavedNewVariable
);
1226 // Get next device path instance from Variable
1228 FreePool (Instance
);
1229 Instance
= GetNextDevicePathInstance (&Variable
, &InstanceSize
);
1230 } while (Instance
!= NULL
);
1232 FreePool (OriginalVariable
);
1235 VariableSize
= GetDevicePathSize (NewVariable
);
1237 Status
= gRT
->SetVariable (
1239 &gEfiGlobalVariableGuid
,
1240 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1245 // Shrinking variable with existing variable driver implementation shouldn't fail.
1247 ASSERT_EFI_ERROR (Status
);
1250 if (NewVariable
!= NULL
) {
1251 FreePool (NewVariable
);
1258 Build terminal device path according to terminal type.
1260 @param TerminalType The terminal type is PC ANSI, VT100, VT100+ or VT-UTF8.
1261 @param ParentDevicePath Parent device path.
1262 @param TerminalDevicePath Returned terminal device path, if building successfully.
1264 @retval EFI_UNSUPPORTED Terminal does not belong to the supported type.
1265 @retval EFI_OUT_OF_RESOURCES Generate terminal device path failed.
1266 @retval EFI_SUCCESS Build terminal device path successfully.
1270 SetTerminalDevicePath (
1271 IN TERMINAL_TYPE TerminalType
,
1272 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1273 OUT EFI_DEVICE_PATH_PROTOCOL
**TerminalDevicePath
1276 VENDOR_DEVICE_PATH Node
;
1278 ASSERT (TerminalType
< ARRAY_SIZE (mTerminalType
));
1279 Node
.Header
.Type
= MESSAGING_DEVICE_PATH
;
1280 Node
.Header
.SubType
= MSG_VENDOR_DP
;
1281 SetDevicePathNodeLength (&Node
.Header
, sizeof (VENDOR_DEVICE_PATH
));
1282 CopyGuid (&Node
.Guid
, mTerminalType
[TerminalType
]);
1285 // Append the terminal node onto parent device path
1286 // to generate a complete terminal device path.
1288 *TerminalDevicePath
= AppendDevicePathNode (
1290 (EFI_DEVICE_PATH_PROTOCOL
*) &Node
1292 if (*TerminalDevicePath
== NULL
) {
1293 return EFI_OUT_OF_RESOURCES
;
1300 The user Entry Point for module Terminal. The user code starts with this function.
1302 @param ImageHandle The firmware allocated handle for the EFI image.
1303 @param SystemTable A pointer to the EFI System Table.
1305 @retval EFI_SUCCESS The entry point is executed successfully.
1306 @retval other Some error occurs when executing this entry point.
1312 IN EFI_HANDLE ImageHandle
,
1313 IN EFI_SYSTEM_TABLE
*SystemTable
1319 // Install driver model protocol(s).
1321 Status
= EfiLibInstallDriverBindingComponentName2 (
1324 &gTerminalDriverBinding
,
1326 &gTerminalComponentName
,
1327 &gTerminalComponentName2
1329 ASSERT_EFI_ERROR (Status
);
1335 Check if the device supports hot-plug through its device path.
1337 This function could be updated to check more types of Hot Plug devices.
1338 Currently, it checks USB and PCCard device.
1340 @param DevicePath Pointer to device's device path.
1342 @retval TRUE The devcie is a hot-plug device
1343 @retval FALSE The devcie is not a hot-plug device.
1348 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1351 EFI_DEVICE_PATH_PROTOCOL
*CheckDevicePath
;
1353 CheckDevicePath
= DevicePath
;
1354 while (!IsDevicePathEnd (CheckDevicePath
)) {
1356 // Check device whether is hot plug device or not throught Device Path
1358 if ((DevicePathType (CheckDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1359 (DevicePathSubType (CheckDevicePath
) == MSG_USB_DP
||
1360 DevicePathSubType (CheckDevicePath
) == MSG_USB_CLASS_DP
||
1361 DevicePathSubType (CheckDevicePath
) == MSG_USB_WWID_DP
)) {
1363 // If Device is USB device
1367 if ((DevicePathType (CheckDevicePath
) == HARDWARE_DEVICE_PATH
) &&
1368 (DevicePathSubType (CheckDevicePath
) == HW_PCCARD_DP
)) {
1370 // If Device is PCCard
1375 CheckDevicePath
= NextDevicePathNode (CheckDevicePath
);