2 Console Platfrom 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 (This
->DriverBindingHandle
, ControllerHandle
)) {
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, StdErrDev.
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 StdErrDev,
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 (This
->DriverBindingHandle
, ControllerHandle
)) {
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 StdErrDev 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 StdErr 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 // If it is not a hot-plug device, first delete it from the ConInDev variable.
487 if (!IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
489 // Get the Device Path Protocol so the environment variables can be updated
491 Status
= gBS
->OpenProtocol (
493 &gEfiDevicePathProtocolGuid
,
494 (VOID
**) &DevicePath
,
495 This
->DriverBindingHandle
,
497 EFI_OPEN_PROTOCOL_GET_PROTOCOL
499 if (!EFI_ERROR (Status
)) {
501 // Remove DevicePath from ConInDev
503 ConPlatformUpdateDeviceVariable (
511 // Uninstall the Console Device GUIDs from Controller Handle
513 ConPlatformUnInstallProtocol (
516 &gEfiConsoleInDeviceGuid
520 // Close the Simple Text Input Protocol
524 &gEfiSimpleTextInProtocolGuid
,
525 This
->DriverBindingHandle
,
534 Stop this driver on ControllerHandle by removing Console Out Devcice GUID
535 and closing the Simple Text Output protocol on ControllerHandle.
537 @param This Protocol instance pointer.
538 @param ControllerHandle Handle of device to stop driver on
539 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
540 children is zero stop the entire bus driver.
541 @param ChildHandleBuffer List of Child Handles to Stop.
543 @retval EFI_SUCCESS This driver is removed ControllerHandle
544 @retval other This driver was not removed from this device
549 ConPlatformTextOutDriverBindingStop (
550 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
551 IN EFI_HANDLE ControllerHandle
,
552 IN UINTN NumberOfChildren
,
553 IN EFI_HANDLE
*ChildHandleBuffer
557 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
560 // If it is not a hot-plug device, first delete it from the ConOutDev and StdErrDev variable.
562 if (!IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
564 // Get the Device Path Protocol so the environment variables can be updated
566 Status
= gBS
->OpenProtocol (
568 &gEfiDevicePathProtocolGuid
,
569 (VOID
**) &DevicePath
,
570 This
->DriverBindingHandle
,
572 EFI_OPEN_PROTOCOL_GET_PROTOCOL
574 if (!EFI_ERROR (Status
)) {
576 // Remove DevicePath from ConOutDev, and StdErrDev
578 ConPlatformUpdateDeviceVariable (
583 ConPlatformUpdateDeviceVariable (
591 // Uninstall the Console Device GUIDs from Controller Handle
593 ConPlatformUnInstallProtocol (
596 &gEfiConsoleOutDeviceGuid
599 ConPlatformUnInstallProtocol (
602 &gEfiStandardErrorDeviceGuid
606 // Close the Simple Text Output Protocol
610 &gEfiSimpleTextOutProtocolGuid
,
611 This
->DriverBindingHandle
,
620 Uninstall the specified protocol.
622 @param This Protocol instance pointer.
623 @param Handle Handle of device to uninstall protocol on.
624 @param ProtocolGuid The specified protocol need to be uninstalled.
628 ConPlatformUnInstallProtocol (
629 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
630 IN EFI_HANDLE Handle
,
631 IN EFI_GUID
*ProtocolGuid
636 Status
= gBS
->OpenProtocol (
640 This
->DriverBindingHandle
,
642 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
645 if (!EFI_ERROR (Status
)) {
646 gBS
->UninstallMultipleProtocolInterfaces (
658 Get the necessary size of buffer and read the variabe.
660 First get the necessary size of buffer. Then read the
661 EFI variable (Name) and return a dynamically allocated
662 buffer. On failure return NULL.
664 @param Name String part of EFI variable name
666 @return Dynamically allocated memory that contains a copy of the EFI variable.
667 Caller is repsoncible freeing the buffer. Return NULL means Variable
672 ConPlatformGetVariable (
684 // Test to see if the variable exists. If it doesn't, return NULL.
686 Status
= gRT
->GetVariable (
688 &gEfiGlobalVariableGuid
,
694 if (Status
== EFI_BUFFER_TOO_SMALL
) {
696 // Allocate the buffer to return
698 Buffer
= AllocatePool (BufferSize
);
699 if (Buffer
== NULL
) {
703 // Read variable into the allocated buffer.
705 Status
= gRT
->GetVariable (
707 &gEfiGlobalVariableGuid
,
712 if (EFI_ERROR (Status
)) {
715 // To make sure Buffer is NULL if any error occurs.
725 Function compares a device path data structure to that of all the nodes of a
726 second device path instance.
728 @param Multi A pointer to a multi-instance device path data structure.
729 @param Single A pointer to a single-instance device path data structure.
730 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
731 points to the remaining device path data structure.
732 (remaining device path = Multi - Single.)
733 @param Delete If TRUE, means removing Single from Multi.
734 If FALSE, the routine just check whether Single matches
735 with any instance in Multi.
737 @retval EFI_SUCCESS If the Single is contained within Multi.
738 @retval EFI_NOT_FOUND If the Single is not contained within Multi.
739 @retval EFI_INVALID_PARAMETER Multi is NULL.
740 @retval EFI_INVALID_PARAMETER Single is NULL.
741 @retval EFI_INVALID_PARAMETER NewDevicePath is NULL when Delete is TRUE.
745 ConPlatformMatchDevicePaths (
746 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
747 IN EFI_DEVICE_PATH_PROTOCOL
*Single
,
748 OUT EFI_DEVICE_PATH_PROTOCOL
**NewDevicePath OPTIONAL
,
752 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
753 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath1
;
754 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath2
;
755 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
759 // The passed in DevicePath should not be NULL
761 if ((Multi
== NULL
) || (Single
== NULL
)) {
762 return EFI_INVALID_PARAMETER
;
766 // If performing Delete operation, the NewDevicePath must not be NULL.
769 if (NewDevicePath
== NULL
) {
770 return EFI_INVALID_PARAMETER
;
774 TempDevicePath1
= NULL
;
777 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
780 // Search for the match of 'Single' in 'Multi'
782 while (DevicePathInst
!= NULL
) {
783 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
786 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.
788 FreePool (DevicePathInst
);
794 // If the node of Multi does not match Single, then added it back to the result.
795 // That is, the node matching Single will be dropped and deleted from result.
797 TempDevicePath2
= AppendDevicePathInstance (
801 if (TempDevicePath1
!= NULL
) {
802 FreePool (TempDevicePath1
);
804 TempDevicePath1
= TempDevicePath2
;
808 FreePool (DevicePathInst
);
809 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
814 // Return the new device path data structure with specified node deleted.
816 *NewDevicePath
= TempDevicePath1
;
820 return EFI_NOT_FOUND
;
824 Update console environment variables.
826 @param VariableName Console environment variables, ConOutDev, ConInDev
827 StdErrDev, ConIn or ConOut.
828 @param DevicePath Console devcie's device path.
829 @param Operation Variable operations, including APPEND, CHECK and DELETE.
831 @retval EFI_SUCCESS Variable operates successfully.
832 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
833 @retval other Variable updating failed.
837 ConPlatformUpdateDeviceVariable (
838 IN CHAR16
*VariableName
,
839 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
840 IN CONPLATFORM_VAR_OPERATION Operation
844 EFI_DEVICE_PATH_PROTOCOL
*VariableDevicePath
;
845 EFI_DEVICE_PATH_PROTOCOL
*NewVariableDevicePath
;
847 VariableDevicePath
= NULL
;
848 NewVariableDevicePath
= NULL
;
851 // Get Variable according to variable name.
852 // The memory for Variable is allocated within ConPlatformGetVarible(),
853 // it is the caller's responsibility to free the memory before return.
855 VariableDevicePath
= ConPlatformGetVariable (VariableName
);
857 if (Operation
!= DELETE
) {
859 // Match specified DevicePath in Console Variable.
861 Status
= ConPlatformMatchDevicePaths (
868 if ((Operation
== CHECK
) || (!EFI_ERROR (Status
))) {
870 // Branch here includes 2 cases:
871 // 1. Operation is CHECK, simply return Status.
872 // 2. Operation is APPEND, and device path already exists in variable, also return.
874 if (VariableDevicePath
!= NULL
) {
875 FreePool (VariableDevicePath
);
881 // We reach here to append a device path that does not exist in variable.
883 Status
= EFI_SUCCESS
;
884 NewVariableDevicePath
= AppendDevicePathInstance (
888 if (NewVariableDevicePath
== NULL
) {
889 Status
= EFI_OUT_OF_RESOURCES
;
894 // We reach here to remove DevicePath from the environment variable that
895 // is a multi-instance device path.
897 Status
= ConPlatformMatchDevicePaths (
900 &NewVariableDevicePath
,
905 if (VariableDevicePath
!= NULL
) {
906 FreePool (VariableDevicePath
);
909 if (EFI_ERROR (Status
)) {
913 if (NewVariableDevicePath
!= NULL
) {
915 // Update Console Environment Variable.
917 Status
= gRT
->SetVariable (
919 &gEfiGlobalVariableGuid
,
920 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
921 GetDevicePathSize (NewVariableDevicePath
),
922 NewVariableDevicePath
925 FreePool (NewVariableDevicePath
);
932 Check if the device supports hot-plug.
934 @param DriverBindingHandle Protocol instance pointer.
935 @param ControllerHandle Handle of device to check.
937 @retval TRUE The devcie is a hot-plug device
938 @retval FALSE The devcie is not a hot-plug device.
943 EFI_HANDLE DriverBindingHandle
,
944 EFI_HANDLE ControllerHandle
950 // HotPlugDeviceGuid indicates ControllerHandle stands for a hot plug device.
952 Status
= gBS
->OpenProtocol (
954 &gEfiHotPlugDeviceGuid
,
958 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
960 if (EFI_ERROR (Status
)) {