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 The user Entry Point for module ConPlatform. The user code starts with this function.
40 @param ImageHandle The firmware allocated handle for the EFI image.
41 @param SystemTable A pointer to the EFI System Table.
43 @retval EFI_SUCCESS The entry point is executed successfully.
44 @retval other Some error occurs when executing this entry point.
49 InitializeConPlatform(
50 IN EFI_HANDLE ImageHandle
,
51 IN EFI_SYSTEM_TABLE
*SystemTable
57 // Install driver model protocol(s).
59 Status
= EfiLibInstallDriverBindingComponentName2 (
62 &gConPlatformTextInDriverBinding
,
64 &gConPlatformComponentName
,
65 &gConPlatformComponentName2
67 ASSERT_EFI_ERROR (Status
);
69 Status
= EfiLibInstallDriverBindingComponentName2 (
72 &gConPlatformTextOutDriverBinding
,
74 &gConPlatformComponentName
,
75 &gConPlatformComponentName2
77 ASSERT_EFI_ERROR (Status
);
85 Test to see if EFI Text In Protocol could be supported on the ControllerHandle.
87 @param This Protocol instance pointer.
88 @param ControllerHandle Handle of device to test.
89 @param RemainingDevicePath Optional parameter use to pick a specific child
92 @retval EFI_SUCCESS This driver supports this device.
93 @retval other This driver does not support this device.
98 ConPlatformTextInDriverBindingSupported (
99 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
100 IN EFI_HANDLE ControllerHandle
,
101 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
104 return ConPlatformDriverBindingSupported (
108 &gEfiSimpleTextInProtocolGuid
114 Test to see if EFI Text Out Protocol could be supported on the ControllerHandle.
116 @param This Protocol instance pointer.
117 @param ControllerHandle Handle of device to test.
118 @param RemainingDevicePath Optional parameter use to pick a specific child
121 @retval EFI_SUCCESS This driver supports this device.
122 @retval other This driver does not support this device.
127 ConPlatformTextOutDriverBindingSupported (
128 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
129 IN EFI_HANDLE ControllerHandle
,
130 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
133 return ConPlatformDriverBindingSupported (
137 &gEfiSimpleTextOutProtocolGuid
143 Test to see if the specified Protocol could be supported on the ControllerHandle.
145 @param This Protocol instance pointer.
146 @param ControllerHandle Handle of device to test.
147 @param RemainingDevicePath Optional parameter use to pick a specific child
149 @param ProtocolGuid The specfic protocol.
151 @retval EFI_SUCCESS This driver supports this device.
152 @retval other This driver does not support this device.
156 ConPlatformDriverBindingSupported (
157 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
158 IN EFI_HANDLE ControllerHandle
,
159 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
,
160 IN EFI_GUID
*ProtocolGuid
167 // Test to see if this is a physical device by checking if
168 // it has a Device Path Protocol.
170 Status
= gBS
->OpenProtocol (
172 &gEfiDevicePathProtocolGuid
,
174 This
->DriverBindingHandle
,
176 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
178 if (EFI_ERROR (Status
)) {
182 // Test to see if this device supports the specified Protocol.
184 Status
= gBS
->OpenProtocol (
187 (VOID
**) &Interface
,
188 This
->DriverBindingHandle
,
190 EFI_OPEN_PROTOCOL_BY_DRIVER
192 if (EFI_ERROR (Status
)) {
199 This
->DriverBindingHandle
,
207 Start this driver on ControllerHandle by opening Simple Text In protocol,
208 reading Device Path, and installing Console In Devcice GUID on ControllerHandle.
210 If this devcie is not one hot-plug devce, append its device path into the
211 console environment variables ConInDev.
213 @param This Protocol instance pointer.
214 @param ControllerHandle Handle of device to bind driver to
215 @param RemainingDevicePath Optional parameter use to pick a specific child
218 @retval EFI_SUCCESS This driver is added to ControllerHandle
219 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
220 @retval other This driver does not support this device.
225 ConPlatformTextInDriverBindingStart (
226 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
227 IN EFI_HANDLE ControllerHandle
,
228 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
232 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
233 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*TextIn
;
236 // Get the Device Path Protocol so the environment variables can be updated
238 Status
= gBS
->OpenProtocol (
240 &gEfiDevicePathProtocolGuid
,
241 (VOID
**) &DevicePath
,
242 This
->DriverBindingHandle
,
244 EFI_OPEN_PROTOCOL_GET_PROTOCOL
246 if (EFI_ERROR (Status
)) {
250 // Open the Simple Input Protocol BY_DRIVER
252 Status
= gBS
->OpenProtocol (
254 &gEfiSimpleTextInProtocolGuid
,
256 This
->DriverBindingHandle
,
258 EFI_OPEN_PROTOCOL_BY_DRIVER
260 if (EFI_ERROR (Status
)) {
264 // Check the device handle, if it is a hot plug device,
265 // do not put the device path into ConInDev, and install
266 // gEfiConsoleInDeviceGuid to the device handle directly.
267 // The policy is, make hot plug device plug in and play immediately.
269 if (IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
270 gBS
->InstallMultipleProtocolInterfaces (
272 &gEfiConsoleInDeviceGuid
,
278 // Append the device path to the ConInDev environment variable
280 ConPlatformUpdateDeviceVariable (
287 // If the device path is an instance in the ConIn environment variable,
288 // then install EfiConsoleInDeviceGuid onto ControllerHandle
290 Status
= ConPlatformUpdateDeviceVariable (
296 if (!EFI_ERROR (Status
)) {
297 gBS
->InstallMultipleProtocolInterfaces (
299 &gEfiConsoleInDeviceGuid
,
306 &gEfiSimpleTextInProtocolGuid
,
307 This
->DriverBindingHandle
,
317 Start this driver on ControllerHandle by opening Simple Text Out protocol,
318 reading Device Path, and installing Console Out Devcic GUID, Standard Error
319 Device GUID on ControllerHandle.
321 If this devcie is not one hot-plug devce, append its device path into the
322 console environment variables ConOutDev, StdErrDev.
324 @param This Protocol instance pointer.
325 @param ControllerHandle Handle of device to bind driver to
326 @param RemainingDevicePath Optional parameter use to pick a specific child
329 @retval EFI_SUCCESS This driver is added to ControllerHandle
330 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
331 @retval other This driver does not support this device
336 ConPlatformTextOutDriverBindingStart (
337 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
338 IN EFI_HANDLE ControllerHandle
,
339 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
343 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
344 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*TextOut
;
350 // Get the Device Path Protocol so the environment variables can be updated
352 Status
= gBS
->OpenProtocol (
354 &gEfiDevicePathProtocolGuid
,
355 (VOID
**) &DevicePath
,
356 This
->DriverBindingHandle
,
358 EFI_OPEN_PROTOCOL_GET_PROTOCOL
360 if (EFI_ERROR (Status
)) {
364 // Open the Simple Text Output Protocol BY_DRIVER
366 Status
= gBS
->OpenProtocol (
368 &gEfiSimpleTextOutProtocolGuid
,
370 This
->DriverBindingHandle
,
372 EFI_OPEN_PROTOCOL_BY_DRIVER
374 if (EFI_ERROR (Status
)) {
378 // Check the device handle, if it is a hot plug device,
379 // do not put the device path into ConOutDev and StdErrDev,
380 // and install gEfiConsoleOutDeviceGuid to the device handle directly.
381 // The policy is, make hot plug device plug in and play immediately.
383 if (IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
384 gBS
->InstallMultipleProtocolInterfaces (
386 &gEfiConsoleOutDeviceGuid
,
392 // Append the device path to the ConOutDev environment variable
394 ConPlatformUpdateDeviceVariable (
400 // Append the device path to the StdErrDev environment variable
402 ConPlatformUpdateDeviceVariable (
409 // If the device path is an instance in the ConOut environment variable,
410 // then install EfiConsoleOutDeviceGuid onto ControllerHandle
412 Status
= ConPlatformUpdateDeviceVariable (
418 if (!EFI_ERROR (Status
)) {
420 Status
= gBS
->InstallMultipleProtocolInterfaces (
422 &gEfiConsoleOutDeviceGuid
,
428 // If the device path is an instance in the StdErr environment variable,
429 // then install EfiStandardErrorDeviceGuid onto ControllerHandle
431 Status
= ConPlatformUpdateDeviceVariable (
436 if (!EFI_ERROR (Status
)) {
438 gBS
->InstallMultipleProtocolInterfaces (
440 &gEfiStandardErrorDeviceGuid
,
449 &gEfiSimpleTextOutProtocolGuid
,
450 This
->DriverBindingHandle
,
460 Stop this driver on ControllerHandle by removing Console In Devcice GUID
461 and closing the Simple Text In protocol on ControllerHandle.
463 @param This Protocol instance pointer.
464 @param ControllerHandle Handle of device to stop driver on
465 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
466 children is zero stop the entire bus driver.
467 @param ChildHandleBuffer List of Child Handles to Stop.
469 @retval EFI_SUCCESS This driver is removed ControllerHandle
470 @retval other This driver was not removed from this device
475 ConPlatformTextInDriverBindingStop (
476 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
477 IN EFI_HANDLE ControllerHandle
,
478 IN UINTN NumberOfChildren
,
479 IN EFI_HANDLE
*ChildHandleBuffer
483 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
486 // hot plug device is not included into the console associated variables,
487 // so no need to check variable for those hot plug devices.
489 if (!IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
491 // Get the Device Path Protocol so the environment variables can be updated
493 Status
= gBS
->OpenProtocol (
495 &gEfiDevicePathProtocolGuid
,
496 (VOID
**) &DevicePath
,
497 This
->DriverBindingHandle
,
499 EFI_OPEN_PROTOCOL_GET_PROTOCOL
501 if (!EFI_ERROR (Status
)) {
503 // Remove DevicePath from ConInDev
505 ConPlatformUpdateDeviceVariable (
513 // Uninstall the Console Device GUIDs from Controller Handle
515 ConPlatformUnInstallProtocol (
518 &gEfiConsoleInDeviceGuid
522 // Close the Simple Input Protocol
526 &gEfiSimpleTextInProtocolGuid
,
527 This
->DriverBindingHandle
,
536 Stop this driver on ControllerHandle by removing Console Out Devcice GUID
537 and closing the Simple Text Out protocol on ControllerHandle.
539 @param This Protocol instance pointer.
540 @param ControllerHandle Handle of device to stop driver on
541 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
542 children is zero stop the entire bus driver.
543 @param ChildHandleBuffer List of Child Handles to Stop.
545 @retval EFI_SUCCESS This driver is removed ControllerHandle
546 @retval other This driver was not removed from this device
551 ConPlatformTextOutDriverBindingStop (
552 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
553 IN EFI_HANDLE ControllerHandle
,
554 IN UINTN NumberOfChildren
,
555 IN EFI_HANDLE
*ChildHandleBuffer
559 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
562 // hot plug device is not included into the console associated variables,
563 // so no need to check variable for those hot plug devices.
565 if (!IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
567 // Get the Device Path Protocol so the environment variables can be updated
569 Status
= gBS
->OpenProtocol (
571 &gEfiDevicePathProtocolGuid
,
572 (VOID
**) &DevicePath
,
573 This
->DriverBindingHandle
,
575 EFI_OPEN_PROTOCOL_GET_PROTOCOL
577 if (!EFI_ERROR (Status
)) {
579 // Remove DevicePath from ConOutDev, and StdErrDev
581 ConPlatformUpdateDeviceVariable (
586 ConPlatformUpdateDeviceVariable (
594 // Uninstall the Console Device GUIDs from Controller Handle
596 ConPlatformUnInstallProtocol (
599 &gEfiConsoleOutDeviceGuid
602 ConPlatformUnInstallProtocol (
605 &gEfiStandardErrorDeviceGuid
609 // Close the Simple Text Output Protocol
613 &gEfiSimpleTextOutProtocolGuid
,
614 This
->DriverBindingHandle
,
623 Uninstall the specified protocol.
625 @param This Protocol instance pointer.
626 @param Handle Handle of device to uninstall protocol on.
627 @param ProtocolGuid The specified protocol need to be uninstalled.
633 ConPlatformUnInstallProtocol (
634 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
635 IN EFI_HANDLE Handle
,
636 IN EFI_GUID
*ProtocolGuid
641 Status
= gBS
->OpenProtocol (
645 This
->DriverBindingHandle
,
647 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
650 if (!EFI_ERROR (Status
)) {
651 gBS
->UninstallMultipleProtocolInterfaces (
663 Read the EFI variable (Name) and return a dynamically allocated
664 buffer, and the size of the buffer. On failure return NULL.
666 @param Name String part of EFI variable name
668 @return Dynamically allocated memory that contains a copy of the EFI variable.
669 Caller is repsoncible freeing the buffer. Return NULL means Variable
674 ConPlatformGetVariable (
686 // Test to see if the variable exists. If it doesn't, reuturn NULL.
688 Status
= gRT
->GetVariable (
690 &gEfiGlobalVariableGuid
,
696 if (Status
== EFI_BUFFER_TOO_SMALL
) {
698 // Allocate the buffer to return
700 Buffer
= AllocatePool (BufferSize
);
701 if (Buffer
== NULL
) {
705 // Read variable into the allocated buffer.
707 Status
= gRT
->GetVariable (
709 &gEfiGlobalVariableGuid
,
714 if (EFI_ERROR (Status
)) {
717 // To make sure Buffer is NULL if any error occurs.
727 Function compares a device path data structure to that of all the nodes of a
728 second device path instance.
731 @param Multi A pointer to a multi-instance device path data structure.
732 @param Single A pointer to a single-instance device path data structure.
733 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
734 points to the remaining device path data structure.
735 (remaining device path = Multi - Single.)
736 @param Delete If TRUE, means removing Single from Multi.
737 If FALSE, the routine just check whether Single matches
738 with any instance in Multi.
740 @retval EFI_SUCCESS If the Single is contained within Multi.
741 @retval EFI_NOT_FOUND If the Single is not contained within Multi.
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_NOT_FOUND
;
766 // If performing Delete operation, the NewDevicePath must not be NULL.
769 ASSERT (NewDevicePath
!= NULL
);
772 TempDevicePath1
= NULL
;
775 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
778 // Search for the match of 'Single' in 'Multi'
780 while (DevicePathInst
!= NULL
) {
781 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
784 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.
786 FreePool (DevicePathInst
);
792 // Append the mis-matched devcie path into remaining device path.
794 TempDevicePath2
= AppendDevicePathInstance (
798 if (TempDevicePath1
!= NULL
) {
799 FreePool (TempDevicePath1
);
801 TempDevicePath1
= TempDevicePath2
;
805 FreePool (DevicePathInst
);
806 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
811 // Return the remaining device path data structure
813 *NewDevicePath
= TempDevicePath1
;
817 return EFI_NOT_FOUND
;
821 Update console environment variables.
823 @param VariableName Console environment variables, ConOutDev, ConInDev
824 StdErrDev, ConIn or ConOut.
825 @param DevicePath Console devcie's device path.
826 @param Operation Variable operations, such as APPEND or DELETE.
828 @retval EFI_SUCCESS Variable operates successfully.
829 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
830 @retval other Variable updating failed.
834 ConPlatformUpdateDeviceVariable (
835 IN CHAR16
*VariableName
,
836 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
837 IN CONPLATFORM_VAR_OPERATION Operation
841 EFI_DEVICE_PATH_PROTOCOL
*VariableDevicePath
;
842 EFI_DEVICE_PATH_PROTOCOL
*NewVariableDevicePath
;
844 VariableDevicePath
= NULL
;
845 NewVariableDevicePath
= NULL
;
848 // Get Variable according to variable name.
849 // The memory for Variable is allocated within ConPlatformGetVarible(),
850 // it is the caller's responsibility to free the memory before return.
852 VariableDevicePath
= ConPlatformGetVariable (VariableName
);
854 if (Operation
!= DELETE
) {
856 // Match specified DevicePath in Console Variable.
858 Status
= ConPlatformMatchDevicePaths (
865 if ((Operation
== CHECK
) || (!EFI_ERROR (Status
))) {
867 // The device path is already in the variable
869 if (VariableDevicePath
!= NULL
) {
870 FreePool (VariableDevicePath
);
876 // The device path is not in variable. Append DevicePath to the
877 // environment variable that is a multi-instance device path.
879 Status
= EFI_SUCCESS
;
880 NewVariableDevicePath
= AppendDevicePathInstance (
884 if (NewVariableDevicePath
== NULL
) {
885 Status
= EFI_OUT_OF_RESOURCES
;
890 // Remove DevicePath from the environment variable that
891 // is a multi-instance device path.
893 Status
= ConPlatformMatchDevicePaths (
896 &NewVariableDevicePath
,
901 if (VariableDevicePath
!= NULL
) {
902 FreePool (VariableDevicePath
);
905 if (EFI_ERROR (Status
)) {
909 if (NewVariableDevicePath
!= NULL
) {
911 // Update Console Environment Variable.
913 Status
= gRT
->SetVariable (
915 &gEfiGlobalVariableGuid
,
916 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
917 GetDevicePathSize (NewVariableDevicePath
),
918 NewVariableDevicePath
921 FreePool (NewVariableDevicePath
);
928 Check if the device is one hot-plug supported.
930 @param DriverBindingHandle Protocol instance pointer.
931 @param ControllerHandle Handle of device to check.
933 @retval TRUE The devcie is a hot-plug device
934 @retval FALSE The devcie is not a hot-plug device.
939 EFI_HANDLE DriverBindingHandle
,
940 EFI_HANDLE ControllerHandle
946 // HotPlugDeviceGuid indicates ControllerHandle stands for a hot plug device.
948 Status
= gBS
->OpenProtocol (
950 &gEfiHotPlugDeviceGuid
,
954 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
956 if (EFI_ERROR (Status
)) {