2 Console Platform DXE Driver, install Console Device Guids and update Console
5 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
6 All rights reserved. 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.
16 #include "ConPlatform.h"
19 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextInDriverBinding
= {
20 ConPlatformTextInDriverBindingSupported
,
21 ConPlatformTextInDriverBindingStart
,
22 ConPlatformTextInDriverBindingStop
,
28 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextOutDriverBinding
= {
29 ConPlatformTextOutDriverBindingSupported
,
30 ConPlatformTextOutDriverBindingStart
,
31 ConPlatformTextOutDriverBindingStop
,
38 Entrypoint of this module.
40 This function is the entrypoint of this module. It installs Driver Binding
41 Protocols together with Component Name Protocols.
43 @param ImageHandle The firmware allocated handle for the EFI image.
44 @param SystemTable A pointer to the EFI System Table.
46 @retval EFI_SUCCESS The entry point is executed successfully.
51 InitializeConPlatform(
52 IN EFI_HANDLE ImageHandle
,
53 IN EFI_SYSTEM_TABLE
*SystemTable
58 Status
= EfiLibInstallDriverBindingComponentName2 (
61 &gConPlatformTextInDriverBinding
,
63 &gConPlatformComponentName
,
64 &gConPlatformComponentName2
66 ASSERT_EFI_ERROR (Status
);
68 Status
= EfiLibInstallDriverBindingComponentName2 (
71 &gConPlatformTextOutDriverBinding
,
73 &gConPlatformComponentName
,
74 &gConPlatformComponentName2
76 ASSERT_EFI_ERROR (Status
);
83 Test to see if EFI_SIMPLE_TEXT_INPUT_PROTOCOL is supported on ControllerHandle.
85 @param This Protocol instance pointer.
86 @param ControllerHandle Handle of device to test.
87 @param RemainingDevicePath Optional parameter use to pick a specific child
90 @retval EFI_SUCCESS This driver supports this device.
91 @retval other This driver does not support this device.
96 ConPlatformTextInDriverBindingSupported (
97 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
98 IN EFI_HANDLE ControllerHandle
,
99 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
102 return ConPlatformDriverBindingSupported (
105 &gEfiSimpleTextInProtocolGuid
111 Test to see if EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is supported on ControllerHandle.
113 @param This Protocol instance pointer.
114 @param ControllerHandle Handle of device to test.
115 @param RemainingDevicePath Optional parameter use to pick a specific child
118 @retval EFI_SUCCESS This driver supports this device.
119 @retval other This driver does not support this device.
124 ConPlatformTextOutDriverBindingSupported (
125 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
126 IN EFI_HANDLE ControllerHandle
,
127 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
130 return ConPlatformDriverBindingSupported (
133 &gEfiSimpleTextOutProtocolGuid
139 Test to see if the specified protocol is supported on ControllerHandle.
141 @param This Protocol instance pointer.
142 @param ControllerHandle Handle of device to test.
143 @param ProtocolGuid The specfic protocol.
145 @retval EFI_SUCCESS This driver supports this device.
146 @retval other This driver does not support this device.
150 ConPlatformDriverBindingSupported (
151 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
152 IN EFI_HANDLE ControllerHandle
,
153 IN EFI_GUID
*ProtocolGuid
160 // Test to see if this is a physical device by checking if
161 // it has a Device Path Protocol.
163 Status
= gBS
->OpenProtocol (
165 &gEfiDevicePathProtocolGuid
,
167 This
->DriverBindingHandle
,
169 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
171 if (EFI_ERROR (Status
)) {
175 // Test to see if this device supports the specified Protocol.
177 Status
= gBS
->OpenProtocol (
180 (VOID
**) &Interface
,
181 This
->DriverBindingHandle
,
183 EFI_OPEN_PROTOCOL_BY_DRIVER
185 if (EFI_ERROR (Status
)) {
192 This
->DriverBindingHandle
,
200 Start this driver on the device for console input.
202 Start this driver on ControllerHandle by opening Simple Text Input Protocol,
203 reading Device Path, and installing Console In Devcice GUID on ControllerHandle.
205 If this devcie is not one hot-plug devce, append its device path into the
206 console environment variables ConInDev.
208 @param This Protocol instance pointer.
209 @param ControllerHandle Handle of device to bind driver to
210 @param RemainingDevicePath Optional parameter use to pick a specific child
213 @retval EFI_SUCCESS This driver is added to ControllerHandle
214 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
215 @retval other This driver does not support this device.
220 ConPlatformTextInDriverBindingStart (
221 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
222 IN EFI_HANDLE ControllerHandle
,
223 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
227 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
228 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*TextIn
;
231 // Get the Device Path Protocol so the environment variables can be updated
233 Status
= gBS
->OpenProtocol (
235 &gEfiDevicePathProtocolGuid
,
236 (VOID
**) &DevicePath
,
237 This
->DriverBindingHandle
,
239 EFI_OPEN_PROTOCOL_GET_PROTOCOL
241 if (EFI_ERROR (Status
)) {
245 // Open the Simple Text Input Protocol BY_DRIVER
247 Status
= gBS
->OpenProtocol (
249 &gEfiSimpleTextInProtocolGuid
,
251 This
->DriverBindingHandle
,
253 EFI_OPEN_PROTOCOL_BY_DRIVER
255 if (EFI_ERROR (Status
)) {
259 // Check the device handle, if it is a hot plug device,
260 // do not put the device path into ConInDev, and install
261 // gEfiConsoleInDeviceGuid to the device handle directly.
262 // The policy is, make hot plug device plug in and play immediately.
264 if (IsHotPlugDevice (DevicePath
)) {
265 gBS
->InstallMultipleProtocolInterfaces (
267 &gEfiConsoleInDeviceGuid
,
273 // If it is not a hot-plug device, append the device path to the
274 // ConInDev environment variable
276 ConPlatformUpdateDeviceVariable (
283 // If the device path is successfully added to the ConIn environment variable,
284 // then install EfiConsoleInDeviceGuid onto ControllerHandle
286 Status
= ConPlatformUpdateDeviceVariable (
292 if (!EFI_ERROR (Status
)) {
293 gBS
->InstallMultipleProtocolInterfaces (
295 &gEfiConsoleInDeviceGuid
,
302 &gEfiSimpleTextInProtocolGuid
,
303 This
->DriverBindingHandle
,
313 Start this driver on the device for console output and stardard error output.
315 Start this driver on ControllerHandle by opening Simple Text Output Protocol,
316 reading Device Path, and installing Console Out Devcic GUID, Standard Error
317 Device GUID on ControllerHandle.
319 If this devcie is not one hot-plug devce, append its device path into the
320 console environment variables ConOutDev, ErrOutDev.
322 @param This Protocol instance pointer.
323 @param ControllerHandle Handle of device to bind driver to
324 @param RemainingDevicePath Optional parameter use to pick a specific child
327 @retval EFI_SUCCESS This driver is added to ControllerHandle
328 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
329 @retval other This driver does not support this device
334 ConPlatformTextOutDriverBindingStart (
335 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
336 IN EFI_HANDLE ControllerHandle
,
337 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
341 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
342 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*TextOut
;
348 // Get the Device Path Protocol so the environment variables can be updated
350 Status
= gBS
->OpenProtocol (
352 &gEfiDevicePathProtocolGuid
,
353 (VOID
**) &DevicePath
,
354 This
->DriverBindingHandle
,
356 EFI_OPEN_PROTOCOL_GET_PROTOCOL
358 if (EFI_ERROR (Status
)) {
362 // Open the Simple Text Output Protocol BY_DRIVER
364 Status
= gBS
->OpenProtocol (
366 &gEfiSimpleTextOutProtocolGuid
,
368 This
->DriverBindingHandle
,
370 EFI_OPEN_PROTOCOL_BY_DRIVER
372 if (EFI_ERROR (Status
)) {
376 // Check the device handle, if it is a hot plug device,
377 // do not put the device path into ConOutDev and ErrOutDev,
378 // and install gEfiConsoleOutDeviceGuid to the device handle directly.
379 // The policy is, make hot plug device plug in and play immediately.
381 if (IsHotPlugDevice (DevicePath
)) {
382 gBS
->InstallMultipleProtocolInterfaces (
384 &gEfiConsoleOutDeviceGuid
,
390 // If it is not a hot-plug device, first append the device path to the
391 // ConOutDev environment variable
393 ConPlatformUpdateDeviceVariable (
399 // Then append the device path to the ErrOutDev environment variable
401 ConPlatformUpdateDeviceVariable (
408 // If the device path is successfully added to the ConOut environment variable,
409 // then install EfiConsoleOutDeviceGuid onto ControllerHandle
411 Status
= ConPlatformUpdateDeviceVariable (
417 if (!EFI_ERROR (Status
)) {
419 Status
= gBS
->InstallMultipleProtocolInterfaces (
421 &gEfiConsoleOutDeviceGuid
,
427 // If the device path is successfully added to the ErrOut environment variable,
428 // then install EfiStandardErrorDeviceGuid onto ControllerHandle
430 Status
= ConPlatformUpdateDeviceVariable (
435 if (!EFI_ERROR (Status
)) {
437 gBS
->InstallMultipleProtocolInterfaces (
439 &gEfiStandardErrorDeviceGuid
,
448 &gEfiSimpleTextOutProtocolGuid
,
449 This
->DriverBindingHandle
,
459 Stop this driver on ControllerHandle by removing Console In Devcice GUID
460 and closing the Simple Text Input protocol on ControllerHandle.
462 @param This Protocol instance pointer.
463 @param ControllerHandle Handle of device to stop driver on
464 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
465 children is zero stop the entire bus driver.
466 @param ChildHandleBuffer List of Child Handles to Stop.
468 @retval EFI_SUCCESS This driver is removed ControllerHandle
469 @retval other This driver was not removed from this device
474 ConPlatformTextInDriverBindingStop (
475 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
476 IN EFI_HANDLE ControllerHandle
,
477 IN UINTN NumberOfChildren
,
478 IN EFI_HANDLE
*ChildHandleBuffer
482 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
485 // Get the Device Path Protocol firstly
487 Status
= gBS
->OpenProtocol (
489 &gEfiDevicePathProtocolGuid
,
490 (VOID
**) &DevicePath
,
491 This
->DriverBindingHandle
,
493 EFI_OPEN_PROTOCOL_GET_PROTOCOL
496 if (EFI_ERROR (Status
)) {
501 // If it is not a hot-plug device, first delete it from the ConInDev variable.
503 if (!IsHotPlugDevice (DevicePath
)) {
505 // Remove DevicePath from ConInDev
507 ConPlatformUpdateDeviceVariable (
515 // Uninstall the Console Device GUIDs from Controller Handle
517 ConPlatformUnInstallProtocol (
520 &gEfiConsoleInDeviceGuid
524 // Close the Simple Text Input Protocol
528 &gEfiSimpleTextInProtocolGuid
,
529 This
->DriverBindingHandle
,
538 Stop this driver on ControllerHandle by removing Console Out Devcice GUID
539 and closing the Simple Text Output protocol on ControllerHandle.
541 @param This Protocol instance pointer.
542 @param ControllerHandle Handle of device to stop driver on
543 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
544 children is zero stop the entire bus driver.
545 @param ChildHandleBuffer List of Child Handles to Stop.
547 @retval EFI_SUCCESS This driver is removed ControllerHandle
548 @retval other This driver was not removed from this device
553 ConPlatformTextOutDriverBindingStop (
554 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
555 IN EFI_HANDLE ControllerHandle
,
556 IN UINTN NumberOfChildren
,
557 IN EFI_HANDLE
*ChildHandleBuffer
561 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
564 // Get the Device Path Protocol firstly
566 Status
= gBS
->OpenProtocol (
568 &gEfiDevicePathProtocolGuid
,
569 (VOID
**) &DevicePath
,
570 This
->DriverBindingHandle
,
572 EFI_OPEN_PROTOCOL_GET_PROTOCOL
575 if (EFI_ERROR (Status
)) {
580 // If it is not a hot-plug device, first delete it from the ConOutDev and ErrOutDev variable.
582 if (!IsHotPlugDevice (DevicePath
)) {
584 // Remove DevicePath from ConOutDev, and ErrOutDev
586 ConPlatformUpdateDeviceVariable (
591 ConPlatformUpdateDeviceVariable (
599 // Uninstall the Console Device GUIDs from Controller Handle
601 ConPlatformUnInstallProtocol (
604 &gEfiConsoleOutDeviceGuid
607 ConPlatformUnInstallProtocol (
610 &gEfiStandardErrorDeviceGuid
614 // Close the Simple Text Output Protocol
618 &gEfiSimpleTextOutProtocolGuid
,
619 This
->DriverBindingHandle
,
628 Uninstall the specified protocol.
630 @param This Protocol instance pointer.
631 @param Handle Handle of device to uninstall protocol on.
632 @param ProtocolGuid The specified protocol need to be uninstalled.
636 ConPlatformUnInstallProtocol (
637 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
638 IN EFI_HANDLE Handle
,
639 IN EFI_GUID
*ProtocolGuid
644 Status
= gBS
->OpenProtocol (
648 This
->DriverBindingHandle
,
650 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
653 if (!EFI_ERROR (Status
)) {
654 gBS
->UninstallMultipleProtocolInterfaces (
666 Get the necessary size of buffer and read the variable.
668 First get the necessary size of buffer. Then read the
669 EFI variable (Name) and return a dynamically allocated
670 buffer. On failure return NULL.
672 @param Name String part of EFI variable name
674 @return Dynamically allocated memory that contains a copy of the EFI variable.
675 Caller is repsoncible freeing the buffer. Return NULL means Variable
680 ConPlatformGetVariable (
692 // Test to see if the variable exists. If it doesn't, return NULL.
694 Status
= gRT
->GetVariable (
696 &gEfiGlobalVariableGuid
,
702 if (Status
== EFI_BUFFER_TOO_SMALL
) {
704 // Allocate the buffer to return
706 Buffer
= AllocatePool (BufferSize
);
707 if (Buffer
== NULL
) {
711 // Read variable into the allocated buffer.
713 Status
= gRT
->GetVariable (
715 &gEfiGlobalVariableGuid
,
720 if (EFI_ERROR (Status
)) {
723 // To make sure Buffer is NULL if any error occurs.
733 Function compares a device path data structure to that of all the nodes of a
734 second device path instance.
736 @param Multi A pointer to a multi-instance device path data structure.
737 @param Single A pointer to a single-instance device path data structure.
738 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
739 points to the remaining device path data structure.
740 (remaining device path = Multi - Single.)
741 @param Delete If TRUE, means removing Single from Multi.
742 If FALSE, the routine just check whether Single matches
743 with any instance in Multi.
745 @retval EFI_SUCCESS If the Single is contained within Multi.
746 @retval EFI_NOT_FOUND If the Single is not contained within Multi.
747 @retval EFI_INVALID_PARAMETER Multi is NULL.
748 @retval EFI_INVALID_PARAMETER Single is NULL.
749 @retval EFI_INVALID_PARAMETER NewDevicePath is NULL when Delete is TRUE.
753 ConPlatformMatchDevicePaths (
754 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
755 IN EFI_DEVICE_PATH_PROTOCOL
*Single
,
756 OUT EFI_DEVICE_PATH_PROTOCOL
**NewDevicePath OPTIONAL
,
760 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
761 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath1
;
762 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath2
;
763 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
767 // The passed in DevicePath should not be NULL
769 if ((Multi
== NULL
) || (Single
== NULL
)) {
770 return EFI_INVALID_PARAMETER
;
774 // If performing Delete operation, the NewDevicePath must not be NULL.
777 if (NewDevicePath
== NULL
) {
778 return EFI_INVALID_PARAMETER
;
782 TempDevicePath1
= NULL
;
785 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
788 // Search for the match of 'Single' in 'Multi'
790 while (DevicePathInst
!= NULL
) {
791 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
794 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.
796 FreePool (DevicePathInst
);
802 // If the node of Multi does not match Single, then added it back to the result.
803 // That is, the node matching Single will be dropped and deleted from result.
805 TempDevicePath2
= AppendDevicePathInstance (
809 if (TempDevicePath1
!= NULL
) {
810 FreePool (TempDevicePath1
);
812 TempDevicePath1
= TempDevicePath2
;
816 FreePool (DevicePathInst
);
817 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
822 // Return the new device path data structure with specified node deleted.
824 *NewDevicePath
= TempDevicePath1
;
828 return EFI_NOT_FOUND
;
832 Update console environment variables.
834 @param VariableName Console environment variables, ConOutDev, ConInDev
835 ErrOutDev, ConIn ,ConOut or ErrOut.
836 @param DevicePath Console devcie's device path.
837 @param Operation Variable operations, including APPEND, CHECK and DELETE.
839 @retval EFI_SUCCESS Variable operates successfully.
840 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
841 @retval other Variable updating failed.
845 ConPlatformUpdateDeviceVariable (
846 IN CHAR16
*VariableName
,
847 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
848 IN CONPLATFORM_VAR_OPERATION Operation
852 EFI_DEVICE_PATH_PROTOCOL
*VariableDevicePath
;
853 EFI_DEVICE_PATH_PROTOCOL
*NewVariableDevicePath
;
855 VariableDevicePath
= NULL
;
856 NewVariableDevicePath
= NULL
;
859 // Get Variable according to variable name.
860 // The memory for Variable is allocated within ConPlatformGetVarible(),
861 // it is the caller's responsibility to free the memory before return.
863 VariableDevicePath
= ConPlatformGetVariable (VariableName
);
865 if (Operation
!= DELETE
) {
867 // Match specified DevicePath in Console Variable.
869 Status
= ConPlatformMatchDevicePaths (
876 if ((Operation
== CHECK
) || (!EFI_ERROR (Status
))) {
878 // Branch here includes 2 cases:
879 // 1. Operation is CHECK, simply return Status.
880 // 2. Operation is APPEND, and device path already exists in variable, also return.
882 if (VariableDevicePath
!= NULL
) {
883 FreePool (VariableDevicePath
);
889 // We reach here to append a device path that does not exist in variable.
891 Status
= EFI_SUCCESS
;
892 NewVariableDevicePath
= AppendDevicePathInstance (
896 if (NewVariableDevicePath
== NULL
) {
897 Status
= EFI_OUT_OF_RESOURCES
;
902 // We reach here to remove DevicePath from the environment variable that
903 // is a multi-instance device path.
905 Status
= ConPlatformMatchDevicePaths (
908 &NewVariableDevicePath
,
913 if (VariableDevicePath
!= NULL
) {
914 FreePool (VariableDevicePath
);
917 if (EFI_ERROR (Status
)) {
921 if (NewVariableDevicePath
!= NULL
) {
923 // Update Console Environment Variable.
925 Status
= gRT
->SetVariable (
927 &gEfiGlobalVariableGuid
,
928 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
929 GetDevicePathSize (NewVariableDevicePath
),
930 NewVariableDevicePath
933 FreePool (NewVariableDevicePath
);
940 Check if the device supports hot-plug through its device path.
942 This function could be updated to check more types of Hot Plug devices.
943 Currently, it checks USB and PCCard device.
945 @param DevicePath Pointer to device's device path.
947 @retval TRUE The devcie is a hot-plug device
948 @retval FALSE The devcie is not a hot-plug device.
953 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
957 // Check device whether is hot plug device or not throught Device Path
959 if ((DevicePathType (DevicePath
) == MESSAGING_DEVICE_PATH
) &&
960 (DevicePathSubType (DevicePath
) == MSG_USB_DP
||
961 DevicePathSubType (DevicePath
) == MSG_USB_CLASS_DP
||
962 DevicePathSubType (DevicePath
) == MSG_USB_WWID_DP
)) {
964 // If Device is USB device
968 if ((DevicePathType (DevicePath
) == HARDWARE_DEVICE_PATH
) &&
969 (DevicePathSubType (DevicePath
) == HW_PCCARD_DP
)) {
971 // If Device is PCCard