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[in] ImageHandle The firmware allocated handle for the EFI image.
41 @param[in] 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 specific 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 to see 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 specific 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 Unstall the specific protocol.
625 @param This Protocol instance pointer.
626 @param Handle Handle of device to unstall protocol on.
627 @param ProtocolGuid The specific 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.
667 @param Name String part of EFI variable name
669 @return Dynamically allocated memory that contains a copy of the EFI variable.
670 Caller is repsoncible freeing the buffer.
671 NULL - Variable was not read
675 ConPlatformGetVariable (
687 // Test to see if the variable exists. If it doesn't reuturn NULL
689 Status
= gRT
->GetVariable (
691 &gEfiGlobalVariableGuid
,
697 if (Status
== EFI_BUFFER_TOO_SMALL
) {
699 // Allocate the buffer to return
701 Buffer
= AllocatePool (BufferSize
);
702 if (Buffer
== NULL
) {
706 // Read variable into the allocated buffer.
708 Status
= gRT
->GetVariable (
710 &gEfiGlobalVariableGuid
,
715 if (EFI_ERROR (Status
)) {
725 Function compares a device path data structure to that of all the nodes of a
726 second device path instance.
729 @param Multi A pointer to a multi-instance device path data structure.
730 @param Single A pointer to a single-instance device path data structure.
731 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
732 points to the remaining device path data structure.
733 (remaining device path = Multi - Single.)
734 @param Delete If TRUE, means removing Single from Multi.
735 If FALSE, the routine just check whether Single matches
736 with any instance in Multi.
738 @return The function returns EFI_SUCCESS if the Single is contained within Multi.
739 Otherwise, EFI_NOT_FOUND is returned.
743 ConPlatformMatchDevicePaths (
744 IN EFI_DEVICE_PATH_PROTOCOL
*Multi
,
745 IN EFI_DEVICE_PATH_PROTOCOL
*Single
,
746 IN EFI_DEVICE_PATH_PROTOCOL
**NewDevicePath OPTIONAL
,
750 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
751 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
752 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
756 // The passed in DevicePath should not be NULL
758 if ((Multi
== NULL
) || (Single
== NULL
)) {
759 return EFI_NOT_FOUND
;
762 // if performing Delete operation, the NewDevicePath must not be NULL.
764 TempDevicePath
= NULL
;
767 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
770 // search for the match of 'Single' in 'Multi'
772 while (DevicePathInst
!= NULL
) {
773 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
775 FreePool (DevicePathInst
);
780 TempDevicePath
= AppendDevicePathInstance (
787 FreePool (DevicePathInst
);
788 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
792 *NewDevicePath
= TempDevicePath
;
796 return EFI_NOT_FOUND
;
800 Update console devicein console environment variables.
802 @param VariableName Console environment variables, ConOutDev, ConInDev
803 StdErrDev, ConIn or ConOut.
804 @param DevicePath Console devcie's device path.
805 @param Operation Variable operations, such as APPEND or DELETE.
807 @retval EFI_SUCCESS Variable operates successfully.
808 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
809 @retval other Variable updating failed.
813 ConPlatformUpdateDeviceVariable (
814 IN CHAR16
*VariableName
,
815 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
816 IN CONPLATFORM_VAR_OPERATION Operation
820 EFI_DEVICE_PATH_PROTOCOL
*VariableDevicePath
;
821 EFI_DEVICE_PATH_PROTOCOL
*NewVariableDevicePath
;
823 VariableDevicePath
= NULL
;
824 NewVariableDevicePath
= NULL
;
827 // Get Variable according to variable name.
828 // The memory for Variable is allocated within ConPlatformGetVarible(),
829 // it is the caller's responsibility to free the memory before return.
831 VariableDevicePath
= ConPlatformGetVariable (VariableName
);
833 if (Operation
!= DELETE
) {
835 Status
= ConPlatformMatchDevicePaths (
842 if ((Operation
== CHECK
) || (!EFI_ERROR (Status
))) {
844 // The device path is already in the variable
846 if (VariableDevicePath
!= NULL
) {
847 FreePool (VariableDevicePath
);
853 // The device path is not in variable. Append DevicePath to the
854 // environment variable that is a multi-instance device path.
856 Status
= EFI_SUCCESS
;
857 NewVariableDevicePath
= AppendDevicePathInstance (
861 if (NewVariableDevicePath
== NULL
) {
862 Status
= EFI_OUT_OF_RESOURCES
;
867 // Remove DevicePath from the environment variable that
868 // is a multi-instance device path.
870 Status
= ConPlatformMatchDevicePaths (
873 &NewVariableDevicePath
,
878 if (VariableDevicePath
!= NULL
) {
879 FreePool (VariableDevicePath
);
882 if (EFI_ERROR (Status
)) {
886 if (NewVariableDevicePath
!= NULL
) {
887 Status
= gRT
->SetVariable (
889 &gEfiGlobalVariableGuid
,
890 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
891 GetDevicePathSize (NewVariableDevicePath
),
892 NewVariableDevicePath
895 FreePool (NewVariableDevicePath
);
902 Check if the device is one hot-plug supported.
904 @param DriverBindingHandle Protocol instance pointer.
905 @param ControllerHandle Handle of device to check.
907 @retval TRUE The devcie is a hot-plug device
908 @retval FALSE The devcie is not a hot-plug device.
913 EFI_HANDLE DriverBindingHandle
,
914 EFI_HANDLE ControllerHandle
920 // HotPlugDeviceGuid indicates ControllerHandle stands for a hot plug device.
922 Status
= gBS
->OpenProtocol (
924 &gEfiHotPlugDeviceGuid
,
928 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
930 if (EFI_ERROR (Status
)) {