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 - 2017, 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
));
493 // Open the Serial I/O Protocol BY_DRIVER. It might already be started.
495 Status
= gBS
->OpenProtocol (
497 &gEfiSerialIoProtocolGuid
,
499 This
->DriverBindingHandle
,
501 EFI_OPEN_PROTOCOL_BY_DRIVER
503 ASSERT ((Status
== EFI_SUCCESS
) || (Status
== EFI_ALREADY_STARTED
));
505 if (!IsHotPlugDevice (ParentDevicePath
)) {
507 // if the serial device is a hot plug device, do not update the
508 // ConInDev, ConOutDev, and StdErrDev variables.
510 TerminalUpdateConsoleDevVariable (EFI_CON_IN_DEV_VARIABLE_NAME
, ParentDevicePath
);
511 TerminalUpdateConsoleDevVariable (EFI_CON_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
512 TerminalUpdateConsoleDevVariable (EFI_ERR_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
516 // Do not create any child for END remaining device path.
518 if ((RemainingDevicePath
!= NULL
) && IsDevicePathEnd (RemainingDevicePath
)) {
522 if (Status
== EFI_ALREADY_STARTED
) {
524 if (RemainingDevicePath
== NULL
) {
526 // If RemainingDevicePath is NULL or is the End of Device Path Node
532 // This driver can only produce one child per serial port.
533 // Change its terminal type as remaining device path requests.
535 Status
= gBS
->OpenProtocolInformation (
537 &gEfiSerialIoProtocolGuid
,
541 if (!EFI_ERROR (Status
)) {
542 Status
= EFI_NOT_FOUND
;
543 for (Index
= 0; Index
< EntryCount
; Index
++) {
544 if ((OpenInfoBuffer
[Index
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) != 0) {
545 Status
= gBS
->OpenProtocol (
546 OpenInfoBuffer
[Index
].ControllerHandle
,
547 &gEfiSimpleTextInProtocolGuid
,
548 (VOID
**) &SimpleTextInput
,
549 This
->DriverBindingHandle
,
551 EFI_OPEN_PROTOCOL_GET_PROTOCOL
553 if (!EFI_ERROR (Status
)) {
554 TerminalDevice
= TERMINAL_CON_IN_DEV_FROM_THIS (SimpleTextInput
);
555 TerminalType
= TerminalTypeFromGuid (&((VENDOR_DEVICE_PATH
*) RemainingDevicePath
)->Guid
);
556 ASSERT (TerminalType
< ARRAY_SIZE (mTerminalType
));
557 if (TerminalDevice
->TerminalType
!= TerminalType
) {
558 Status
= InitializeControllerNameTable (TerminalType
, &ControllerNameTable
);
559 if (!EFI_ERROR (Status
)) {
560 StopTerminalStateMachine (TerminalDevice
);
562 // Update the device path
564 Vendor
= TerminalDevice
->DevicePath
;
565 Status
= gBS
->LocateDevicePath (&gEfiSerialIoProtocolGuid
, &Vendor
, &SerialIoHandle
);
566 ASSERT_EFI_ERROR (Status
);
567 CopyGuid (&((VENDOR_DEVICE_PATH
*) Vendor
)->Guid
, mTerminalType
[TerminalType
]);
568 Status
= gBS
->ReinstallProtocolInterface (
569 TerminalDevice
->Handle
,
570 &gEfiDevicePathProtocolGuid
,
571 TerminalDevice
->DevicePath
,
572 TerminalDevice
->DevicePath
574 if (!EFI_ERROR (Status
)) {
575 TerminalDevice
->TerminalType
= TerminalType
;
576 StartTerminalStateMachine (TerminalDevice
);
577 FreeUnicodeStringTable (TerminalDevice
->ControllerNameTable
);
578 TerminalDevice
->ControllerNameTable
= ControllerNameTable
;
581 // Restore the device path on failure
583 CopyGuid (&((VENDOR_DEVICE_PATH
*) Vendor
)->Guid
, mTerminalType
[TerminalDevice
->TerminalType
]);
584 FreeUnicodeStringTable (ControllerNameTable
);
592 FreePool (OpenInfoBuffer
);
598 // Initialize the Terminal Dev
600 TerminalDevice
= AllocateCopyPool (sizeof (TERMINAL_DEV
), &mTerminalDevTemplate
);
601 if (TerminalDevice
== NULL
) {
602 Status
= EFI_OUT_OF_RESOURCES
;
606 if (RemainingDevicePath
== NULL
) {
608 // If RemainingDevicePath is NULL, use default terminal type
610 TerminalDevice
->TerminalType
= PcdGet8 (PcdDefaultTerminalType
);
613 // End of Device Path Node is handled in above.
615 ASSERT (!IsDevicePathEnd (RemainingDevicePath
));
617 // If RemainingDevicePath isn't the End of Device Path Node,
618 // Use the RemainingDevicePath to determine the terminal type
620 TerminalDevice
->TerminalType
= TerminalTypeFromGuid (&((VENDOR_DEVICE_PATH
*) RemainingDevicePath
)->Guid
);
622 ASSERT (TerminalDevice
->TerminalType
< ARRAY_SIZE (mTerminalType
));
623 TerminalDevice
->SerialIo
= SerialIo
;
626 // Build the component name for the child device
628 Status
= InitializeControllerNameTable (TerminalDevice
->TerminalType
, &TerminalDevice
->ControllerNameTable
);
629 if (EFI_ERROR (Status
)) {
634 // Build the device path for the child device
636 Status
= SetTerminalDevicePath (TerminalDevice
->TerminalType
, ParentDevicePath
, &TerminalDevice
->DevicePath
);
637 if (EFI_ERROR (Status
)) {
641 InitializeListHead (&TerminalDevice
->NotifyList
);
642 Status
= gBS
->CreateEvent (
645 TerminalConInWaitForKeyEx
,
647 &TerminalDevice
->SimpleInputEx
.WaitForKeyEx
649 ASSERT_EFI_ERROR (Status
);
651 Status
= gBS
->CreateEvent (
654 TerminalConInWaitForKey
,
656 &TerminalDevice
->SimpleInput
.WaitForKey
658 ASSERT_EFI_ERROR (Status
);
659 Status
= gBS
->CreateEvent (
662 KeyNotifyProcessHandler
,
664 &TerminalDevice
->KeyNotifyProcessEvent
666 ASSERT_EFI_ERROR (Status
);
669 // Allocates and initializes the FIFO buffer to be zero, used for accommodating
670 // the pre-read pending characters.
672 TerminalDevice
->RawFiFo
= AllocateZeroPool (sizeof (RAW_DATA_FIFO
));
673 if (TerminalDevice
->RawFiFo
== NULL
) {
676 TerminalDevice
->UnicodeFiFo
= AllocateZeroPool (sizeof (UNICODE_FIFO
));
677 if (TerminalDevice
->UnicodeFiFo
== NULL
) {
680 TerminalDevice
->EfiKeyFiFo
= AllocateZeroPool (sizeof (EFI_KEY_FIFO
));
681 if (TerminalDevice
->EfiKeyFiFo
== NULL
) {
684 TerminalDevice
->EfiKeyFiFoForNotify
= AllocateZeroPool (sizeof (EFI_KEY_FIFO
));
685 if (TerminalDevice
->EfiKeyFiFoForNotify
== NULL
) {
690 // Set the timeout value of serial buffer for keystroke response performance issue
692 Mode
= TerminalDevice
->SerialIo
->Mode
;
695 if (Mode
->BaudRate
!= 0) {
696 SerialInTimeOut
= (1 + Mode
->DataBits
+ Mode
->StopBits
) * 2 * 1000000 / (UINTN
) Mode
->BaudRate
;
699 Status
= TerminalDevice
->SerialIo
->SetAttributes (
700 TerminalDevice
->SerialIo
,
702 Mode
->ReceiveFifoDepth
,
703 (UINT32
) SerialInTimeOut
,
704 (EFI_PARITY_TYPE
) (Mode
->Parity
),
705 (UINT8
) Mode
->DataBits
,
706 (EFI_STOP_BITS_TYPE
) (Mode
->StopBits
)
708 if (EFI_ERROR (Status
)) {
710 // if set attributes operation fails, invalidate
711 // the value of SerialInTimeOut,thus make it
712 // inconsistent with the default timeout value
713 // of serial buffer. This will invoke the recalculation
714 // in the readkeystroke routine.
716 TerminalDevice
->SerialInTimeOut
= 0;
718 TerminalDevice
->SerialInTimeOut
= SerialInTimeOut
;
721 SimpleTextOutput
= &TerminalDevice
->SimpleTextOutput
;
722 SimpleTextInput
= &TerminalDevice
->SimpleInput
;
725 // Initialize SimpleTextOut instance
727 SimpleTextOutput
->Mode
= &TerminalDevice
->SimpleTextOutputMode
;
728 TerminalDevice
->TerminalConsoleModeData
= InitializeTerminalConsoleTextMode (
729 &SimpleTextOutput
->Mode
->MaxMode
731 if (TerminalDevice
->TerminalConsoleModeData
== NULL
) {
735 // For terminal devices, cursor is always visible
737 SimpleTextOutput
->Mode
->CursorVisible
= TRUE
;
738 Status
= SimpleTextOutput
->SetAttribute (SimpleTextOutput
, EFI_TEXT_ATTR (EFI_LIGHTGRAY
, EFI_BLACK
));
739 if (!EFI_ERROR (Status
)) {
740 Status
= SimpleTextOutput
->Reset (SimpleTextOutput
, FALSE
);
742 if (EFI_ERROR (Status
)) {
747 // Initialize SimpleTextInput instance
749 Status
= SimpleTextInput
->Reset (SimpleTextInput
, FALSE
);
750 if (EFI_ERROR (Status
)) {
754 Status
= gBS
->InstallMultipleProtocolInterfaces (
755 &TerminalDevice
->Handle
,
756 &gEfiSimpleTextInProtocolGuid
, &TerminalDevice
->SimpleInput
,
757 &gEfiSimpleTextInputExProtocolGuid
, &TerminalDevice
->SimpleInputEx
,
758 &gEfiSimpleTextOutProtocolGuid
, &TerminalDevice
->SimpleTextOutput
,
759 &gEfiDevicePathProtocolGuid
, TerminalDevice
->DevicePath
,
762 if (!EFI_ERROR (Status
)) {
763 Status
= gBS
->OpenProtocol (
765 &gEfiSerialIoProtocolGuid
,
766 (VOID
**) &TerminalDevice
->SerialIo
,
767 This
->DriverBindingHandle
,
768 TerminalDevice
->Handle
,
769 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
771 ASSERT_EFI_ERROR (Status
);
772 StartTerminalStateMachine (TerminalDevice
);
777 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
778 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
779 (EFI_PERIPHERAL_REMOTE_CONSOLE
| EFI_P_EC_CONTROLLER_ERROR
),
784 ASSERT (TerminalDevice
!= NULL
);
786 if (TerminalDevice
->SimpleInput
.WaitForKey
!= NULL
) {
787 gBS
->CloseEvent (TerminalDevice
->SimpleInput
.WaitForKey
);
789 if (TerminalDevice
->SimpleInputEx
.WaitForKeyEx
!= NULL
) {
790 gBS
->CloseEvent (TerminalDevice
->SimpleInputEx
.WaitForKeyEx
);
792 if (TerminalDevice
->KeyNotifyProcessEvent
!= NULL
) {
793 gBS
->CloseEvent (TerminalDevice
->KeyNotifyProcessEvent
);
796 if (TerminalDevice
->RawFiFo
!= NULL
) {
797 FreePool (TerminalDevice
->RawFiFo
);
799 if (TerminalDevice
->UnicodeFiFo
!= NULL
) {
800 FreePool (TerminalDevice
->UnicodeFiFo
);
802 if (TerminalDevice
->EfiKeyFiFo
!= NULL
) {
803 FreePool (TerminalDevice
->EfiKeyFiFo
);
805 if (TerminalDevice
->EfiKeyFiFoForNotify
!= NULL
) {
806 FreePool (TerminalDevice
->EfiKeyFiFoForNotify
);
809 if (TerminalDevice
->ControllerNameTable
!= NULL
) {
810 FreeUnicodeStringTable (TerminalDevice
->ControllerNameTable
);
813 if (TerminalDevice
->DevicePath
!= NULL
) {
814 FreePool (TerminalDevice
->DevicePath
);
817 if (TerminalDevice
->TerminalConsoleModeData
!= NULL
) {
818 FreePool (TerminalDevice
->TerminalConsoleModeData
);
821 FreePool (TerminalDevice
);
826 // Remove Parent Device Path from
827 // the Console Device Environment Variables
829 TerminalRemoveConsoleDevVariable (EFI_CON_IN_DEV_VARIABLE_NAME
, ParentDevicePath
);
830 TerminalRemoveConsoleDevVariable (EFI_CON_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
831 TerminalRemoveConsoleDevVariable (EFI_ERR_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
833 Status
= gBS
->CloseProtocol (
835 &gEfiSerialIoProtocolGuid
,
836 This
->DriverBindingHandle
,
839 ASSERT_EFI_ERROR (Status
);
841 Status
= gBS
->CloseProtocol (
843 &gEfiDevicePathProtocolGuid
,
844 This
->DriverBindingHandle
,
847 ASSERT_EFI_ERROR (Status
);
852 Stop this driver on Controller by closing Simple Text In, Simple Text
853 In Ex, Simple Text Out protocol, and removing parent device path from
854 Console Device Environment Variables.
856 @param This Protocol instance pointer.
857 @param Controller Handle of device to stop driver on
858 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
859 children is zero stop the entire bus driver.
860 @param ChildHandleBuffer List of Child Handles to Stop.
862 @retval EFI_SUCCESS This driver is removed Controller.
863 @retval other This driver could not be removed from this device.
868 TerminalDriverBindingStop (
869 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
870 IN EFI_HANDLE Controller
,
871 IN UINTN NumberOfChildren
,
872 IN EFI_HANDLE
*ChildHandleBuffer
877 BOOLEAN AllChildrenStopped
;
878 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*SimpleTextOutput
;
879 TERMINAL_DEV
*TerminalDevice
;
880 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
881 EFI_SERIAL_IO_PROTOCOL
*SerialIo
;
884 // Complete all outstanding transactions to Controller.
885 // Don't allow any new transaction to Controller to be started.
887 if (NumberOfChildren
== 0) {
889 // Close the bus driver
891 Status
= gBS
->OpenProtocol (
893 &gEfiDevicePathProtocolGuid
,
894 (VOID
**) &ParentDevicePath
,
895 This
->DriverBindingHandle
,
897 EFI_OPEN_PROTOCOL_GET_PROTOCOL
899 ASSERT_EFI_ERROR (Status
);
902 // Remove Parent Device Path from
903 // the Console Device Environment Variables
905 TerminalRemoveConsoleDevVariable (EFI_CON_IN_DEV_VARIABLE_NAME
, ParentDevicePath
);
906 TerminalRemoveConsoleDevVariable (EFI_CON_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
907 TerminalRemoveConsoleDevVariable (EFI_ERR_OUT_DEV_VARIABLE_NAME
, ParentDevicePath
);
911 &gEfiSerialIoProtocolGuid
,
912 This
->DriverBindingHandle
,
918 &gEfiDevicePathProtocolGuid
,
919 This
->DriverBindingHandle
,
926 AllChildrenStopped
= TRUE
;
928 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
930 Status
= gBS
->OpenProtocol (
931 ChildHandleBuffer
[Index
],
932 &gEfiSimpleTextOutProtocolGuid
,
933 (VOID
**) &SimpleTextOutput
,
934 This
->DriverBindingHandle
,
935 ChildHandleBuffer
[Index
],
936 EFI_OPEN_PROTOCOL_GET_PROTOCOL
938 if (!EFI_ERROR (Status
)) {
940 TerminalDevice
= TERMINAL_CON_OUT_DEV_FROM_THIS (SimpleTextOutput
);
944 &gEfiSerialIoProtocolGuid
,
945 This
->DriverBindingHandle
,
946 ChildHandleBuffer
[Index
]
949 Status
= gBS
->UninstallMultipleProtocolInterfaces (
950 ChildHandleBuffer
[Index
],
951 &gEfiSimpleTextInProtocolGuid
,
952 &TerminalDevice
->SimpleInput
,
953 &gEfiSimpleTextInputExProtocolGuid
,
954 &TerminalDevice
->SimpleInputEx
,
955 &gEfiSimpleTextOutProtocolGuid
,
956 &TerminalDevice
->SimpleTextOutput
,
957 &gEfiDevicePathProtocolGuid
,
958 TerminalDevice
->DevicePath
,
961 if (EFI_ERROR (Status
)) {
964 &gEfiSerialIoProtocolGuid
,
966 This
->DriverBindingHandle
,
967 ChildHandleBuffer
[Index
],
968 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
972 FreeUnicodeStringTable (TerminalDevice
->ControllerNameTable
);
973 StopTerminalStateMachine (TerminalDevice
);
974 gBS
->CloseEvent (TerminalDevice
->SimpleInput
.WaitForKey
);
975 gBS
->CloseEvent (TerminalDevice
->SimpleInputEx
.WaitForKeyEx
);
976 gBS
->CloseEvent (TerminalDevice
->KeyNotifyProcessEvent
);
977 TerminalFreeNotifyList (&TerminalDevice
->NotifyList
);
978 FreePool (TerminalDevice
->DevicePath
);
979 FreePool (TerminalDevice
->TerminalConsoleModeData
);
980 FreePool (TerminalDevice
);
984 if (EFI_ERROR (Status
)) {
985 AllChildrenStopped
= FALSE
;
989 if (!AllChildrenStopped
) {
990 return EFI_DEVICE_ERROR
;
997 Update terminal device path in Console Device Environment Variables.
999 @param VariableName The Console Device Environment Variable.
1000 @param ParentDevicePath The terminal device path to be updated.
1004 TerminalUpdateConsoleDevVariable (
1005 IN CHAR16
*VariableName
,
1006 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
1012 TERMINAL_TYPE TerminalType
;
1013 EFI_DEVICE_PATH_PROTOCOL
*Variable
;
1014 EFI_DEVICE_PATH_PROTOCOL
*NewVariable
;
1015 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1016 EDKII_SET_VARIABLE_STATUS
*SetVariableStatus
;
1019 // Get global variable and its size according to the name given.
1021 GetEfiGlobalVariable2 (VariableName
, (VOID
**)&Variable
, NULL
);
1022 if (Variable
== NULL
) {
1027 // Append terminal device path onto the variable.
1029 for (TerminalType
= 0; TerminalType
< ARRAY_SIZE (mTerminalType
); TerminalType
++) {
1030 SetTerminalDevicePath (TerminalType
, ParentDevicePath
, &TempDevicePath
);
1031 NewVariable
= AppendDevicePathInstance (Variable
, TempDevicePath
);
1032 ASSERT (NewVariable
!= NULL
);
1033 if (Variable
!= NULL
) {
1034 FreePool (Variable
);
1037 if (TempDevicePath
!= NULL
) {
1038 FreePool (TempDevicePath
);
1041 Variable
= NewVariable
;
1044 VariableSize
= GetDevicePathSize (Variable
);
1046 Status
= gRT
->SetVariable (
1048 &gEfiGlobalVariableGuid
,
1049 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1054 if (EFI_ERROR (Status
)) {
1055 NameSize
= StrSize (VariableName
);
1056 SetVariableStatus
= AllocatePool (sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ VariableSize
);
1057 if (SetVariableStatus
!= NULL
) {
1058 CopyGuid (&SetVariableStatus
->Guid
, &gEfiGlobalVariableGuid
);
1059 SetVariableStatus
->NameSize
= NameSize
;
1060 SetVariableStatus
->DataSize
= VariableSize
;
1061 SetVariableStatus
->SetStatus
= Status
;
1062 SetVariableStatus
->Attributes
= EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
;
1063 CopyMem (SetVariableStatus
+ 1, VariableName
, NameSize
);
1064 CopyMem (((UINT8
*) (SetVariableStatus
+ 1)) + NameSize
, Variable
, VariableSize
);
1066 REPORT_STATUS_CODE_EX (
1068 PcdGet32 (PcdErrorCodeSetVariable
),
1071 &gEdkiiStatusCodeDataTypeVariableGuid
,
1073 sizeof (EDKII_SET_VARIABLE_STATUS
) + NameSize
+ VariableSize
1076 FreePool (SetVariableStatus
);
1080 FreePool (Variable
);
1087 Remove terminal device path from Console Device Environment Variables.
1089 @param VariableName Console Device Environment Variables.
1090 @param ParentDevicePath The terminal device path to be updated.
1094 TerminalRemoveConsoleDevVariable (
1095 IN CHAR16
*VariableName
,
1096 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
1104 TERMINAL_TYPE TerminalType
;
1105 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
1106 EFI_DEVICE_PATH_PROTOCOL
*Variable
;
1107 EFI_DEVICE_PATH_PROTOCOL
*OriginalVariable
;
1108 EFI_DEVICE_PATH_PROTOCOL
*NewVariable
;
1109 EFI_DEVICE_PATH_PROTOCOL
*SavedNewVariable
;
1110 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1115 // Get global variable and its size according to the name given.
1117 GetEfiGlobalVariable2 (VariableName
, (VOID
**)&Variable
, NULL
);
1118 if (Variable
== NULL
) {
1123 OriginalVariable
= Variable
;
1127 // Get first device path instance from Variable
1129 Instance
= GetNextDevicePathInstance (&Variable
, &InstanceSize
);
1130 if (Instance
== NULL
) {
1131 FreePool (OriginalVariable
);
1135 // Loop through all the device path instances of Variable
1139 // Loop through all the terminal types that this driver supports
1142 for (TerminalType
= 0; TerminalType
< ARRAY_SIZE (mTerminalType
); TerminalType
++) {
1144 SetTerminalDevicePath (TerminalType
, ParentDevicePath
, &TempDevicePath
);
1147 // Compare the generated device path to the current device path instance
1149 if (TempDevicePath
!= NULL
) {
1150 if (CompareMem (Instance
, TempDevicePath
, InstanceSize
) == 0) {
1155 FreePool (TempDevicePath
);
1159 // If a match was not found, then keep the current device path instance
1162 SavedNewVariable
= NewVariable
;
1163 NewVariable
= AppendDevicePathInstance (NewVariable
, Instance
);
1164 if (SavedNewVariable
!= NULL
) {
1165 FreePool (SavedNewVariable
);
1169 // Get next device path instance from Variable
1171 FreePool (Instance
);
1172 Instance
= GetNextDevicePathInstance (&Variable
, &InstanceSize
);
1173 } while (Instance
!= NULL
);
1175 FreePool (OriginalVariable
);
1178 VariableSize
= GetDevicePathSize (NewVariable
);
1180 Status
= gRT
->SetVariable (
1182 &gEfiGlobalVariableGuid
,
1183 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1188 // Shrinking variable with existing variable driver implementation shouldn't fail.
1190 ASSERT_EFI_ERROR (Status
);
1193 if (NewVariable
!= NULL
) {
1194 FreePool (NewVariable
);
1201 Build terminal device path according to terminal type.
1203 @param TerminalType The terminal type is PC ANSI, VT100, VT100+ or VT-UTF8.
1204 @param ParentDevicePath Parent device path.
1205 @param TerminalDevicePath Returned terminal device path, if building successfully.
1207 @retval EFI_UNSUPPORTED Terminal does not belong to the supported type.
1208 @retval EFI_OUT_OF_RESOURCES Generate terminal device path failed.
1209 @retval EFI_SUCCESS Build terminal device path successfully.
1213 SetTerminalDevicePath (
1214 IN TERMINAL_TYPE TerminalType
,
1215 IN EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
,
1216 OUT EFI_DEVICE_PATH_PROTOCOL
**TerminalDevicePath
1219 VENDOR_DEVICE_PATH Node
;
1221 ASSERT (TerminalType
< ARRAY_SIZE (mTerminalType
));
1222 Node
.Header
.Type
= MESSAGING_DEVICE_PATH
;
1223 Node
.Header
.SubType
= MSG_VENDOR_DP
;
1224 SetDevicePathNodeLength (&Node
, sizeof (VENDOR_DEVICE_PATH
));
1225 CopyGuid (&Node
.Guid
, mTerminalType
[TerminalType
]);
1228 // Append the terminal node onto parent device path
1229 // to generate a complete terminal device path.
1231 *TerminalDevicePath
= AppendDevicePathNode (
1233 (EFI_DEVICE_PATH_PROTOCOL
*) &Node
1235 if (*TerminalDevicePath
== NULL
) {
1236 return EFI_OUT_OF_RESOURCES
;
1243 The user Entry Point for module Terminal. The user code starts with this function.
1245 @param ImageHandle The firmware allocated handle for the EFI image.
1246 @param SystemTable A pointer to the EFI System Table.
1248 @retval EFI_SUCCESS The entry point is executed successfully.
1249 @retval other Some error occurs when executing this entry point.
1255 IN EFI_HANDLE ImageHandle
,
1256 IN EFI_SYSTEM_TABLE
*SystemTable
1262 // Install driver model protocol(s).
1264 Status
= EfiLibInstallDriverBindingComponentName2 (
1267 &gTerminalDriverBinding
,
1269 &gTerminalComponentName
,
1270 &gTerminalComponentName2
1272 ASSERT_EFI_ERROR (Status
);
1278 Check if the device supports hot-plug through its device path.
1280 This function could be updated to check more types of Hot Plug devices.
1281 Currently, it checks USB and PCCard device.
1283 @param DevicePath Pointer to device's device path.
1285 @retval TRUE The devcie is a hot-plug device
1286 @retval FALSE The devcie is not a hot-plug device.
1291 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1294 EFI_DEVICE_PATH_PROTOCOL
*CheckDevicePath
;
1296 CheckDevicePath
= DevicePath
;
1297 while (!IsDevicePathEnd (CheckDevicePath
)) {
1299 // Check device whether is hot plug device or not throught Device Path
1301 if ((DevicePathType (CheckDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1302 (DevicePathSubType (CheckDevicePath
) == MSG_USB_DP
||
1303 DevicePathSubType (CheckDevicePath
) == MSG_USB_CLASS_DP
||
1304 DevicePathSubType (CheckDevicePath
) == MSG_USB_WWID_DP
)) {
1306 // If Device is USB device
1310 if ((DevicePathType (CheckDevicePath
) == HARDWARE_DEVICE_PATH
) &&
1311 (DevicePathSubType (CheckDevicePath
) == HW_PCCARD_DP
)) {
1313 // If Device is PCCard
1318 CheckDevicePath
= NextDevicePathNode (CheckDevicePath
);