2 Console Platfrom DXE Driver, install Console protocols.
4 Copyright (c) 2006 - 2007, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <ConPlatform.h>
18 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextInDriverBinding
= {
19 ConPlatformTextInDriverBindingSupported
,
20 ConPlatformTextInDriverBindingStart
,
21 ConPlatformTextInDriverBindingStop
,
27 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextOutDriverBinding
= {
28 ConPlatformTextOutDriverBindingSupported
,
29 ConPlatformTextOutDriverBindingStart
,
30 ConPlatformTextOutDriverBindingStop
,
37 The user Entry Point for module ConPlatform. The user code starts with this function.
39 @param[in] ImageHandle The firmware allocated handle for the EFI image.
40 @param[in] SystemTable A pointer to the EFI System Table.
42 @retval EFI_SUCCESS The entry point is executed successfully.
43 @retval other Some error occurs when executing this entry point.
48 InitializeConPlatform(
49 IN EFI_HANDLE ImageHandle
,
50 IN EFI_SYSTEM_TABLE
*SystemTable
56 // Install driver model protocol(s).
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
);
85 ConPlatformTextInDriverBindingSupported (
86 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
87 IN EFI_HANDLE ControllerHandle
,
88 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
96 (Standard DriverBinding Protocol Supported() function)
104 return ConPlatformDriverBindingSupported (
108 &gEfiSimpleTextInProtocolGuid
114 ConPlatformTextOutDriverBindingSupported (
115 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
116 IN EFI_HANDLE ControllerHandle
,
117 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
125 (Standard DriverBinding Protocol Supported() function)
133 return ConPlatformDriverBindingSupported (
137 &gEfiSimpleTextOutProtocolGuid
142 ConPlatformDriverBindingSupported (
143 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
144 IN EFI_HANDLE ControllerHandle
,
145 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
,
146 IN EFI_GUID
*ProtocolGuid
154 (Standard DriverBinding Protocol Supported() function)
166 // Test to see if this is a physical device by checking to see if
167 // it has a Device Path Protocol
169 Status
= gBS
->OpenProtocol (
171 &gEfiDevicePathProtocolGuid
,
173 This
->DriverBindingHandle
,
175 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
177 if (EFI_ERROR (Status
)) {
181 // Test to see if this device supports the Simple Text Output Protocol
183 Status
= gBS
->OpenProtocol (
186 (VOID
**) &Interface
,
187 This
->DriverBindingHandle
,
189 EFI_OPEN_PROTOCOL_BY_DRIVER
191 if (EFI_ERROR (Status
)) {
198 This
->DriverBindingHandle
,
207 ConPlatformTextInDriverBindingStart (
208 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
209 IN EFI_HANDLE ControllerHandle
,
210 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
218 (Standard DriverBinding Protocol Start() function)
226 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
227 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*TextIn
;
230 // Get the Device Path Protocol so the environment variables can be updated
232 Status
= gBS
->OpenProtocol (
234 &gEfiDevicePathProtocolGuid
,
235 (VOID
**) &DevicePath
,
236 This
->DriverBindingHandle
,
238 EFI_OPEN_PROTOCOL_GET_PROTOCOL
240 if (EFI_ERROR (Status
)) {
244 // Open the Simple Input Protocol BY_DRIVER
246 Status
= gBS
->OpenProtocol (
248 &gEfiSimpleTextInProtocolGuid
,
250 This
->DriverBindingHandle
,
252 EFI_OPEN_PROTOCOL_BY_DRIVER
254 if (EFI_ERROR (Status
)) {
258 // Check the device handle, if it is a hot plug device,
259 // do not put the device path into ConInDev, and install
260 // gEfiConsoleInDeviceGuid to the device handle directly.
261 // The policy is, make hot plug device plug in and play immediately.
263 if (IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
264 gBS
->InstallMultipleProtocolInterfaces (
266 &gEfiConsoleInDeviceGuid
,
272 // Append the device path to the ConInDev environment variable
274 ConPlatformUpdateDeviceVariable (
281 // If the device path is an instance in the ConIn environment variable,
282 // then install EfiConsoleInDeviceGuid onto ControllerHandle
284 Status
= ConPlatformUpdateDeviceVariable (
290 if (!EFI_ERROR (Status
)) {
291 gBS
->InstallMultipleProtocolInterfaces (
293 &gEfiConsoleInDeviceGuid
,
300 &gEfiSimpleTextInProtocolGuid
,
301 This
->DriverBindingHandle
,
312 ConPlatformTextOutDriverBindingStart (
313 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
314 IN EFI_HANDLE ControllerHandle
,
315 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
323 (Standard DriverBinding Protocol Start() function)
331 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
332 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*TextOut
;
338 // Get the Device Path Protocol so the environment variables can be updated
340 Status
= gBS
->OpenProtocol (
342 &gEfiDevicePathProtocolGuid
,
343 (VOID
**) &DevicePath
,
344 This
->DriverBindingHandle
,
346 EFI_OPEN_PROTOCOL_GET_PROTOCOL
348 if (EFI_ERROR (Status
)) {
352 // Open the Simple Text Output Protocol BY_DRIVER
354 Status
= gBS
->OpenProtocol (
356 &gEfiSimpleTextOutProtocolGuid
,
358 This
->DriverBindingHandle
,
360 EFI_OPEN_PROTOCOL_BY_DRIVER
362 if (EFI_ERROR (Status
)) {
366 // Check the device handle, if it is a hot plug device,
367 // do not put the device path into ConOutDev and StdErrDev,
368 // and install gEfiConsoleOutDeviceGuid to the device handle directly.
369 // The policy is, make hot plug device plug in and play immediately.
371 if (IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
372 gBS
->InstallMultipleProtocolInterfaces (
374 &gEfiConsoleOutDeviceGuid
,
380 // Append the device path to the ConOutDev environment variable
382 ConPlatformUpdateDeviceVariable (
388 // Append the device path to the StdErrDev environment variable
390 ConPlatformUpdateDeviceVariable (
397 // If the device path is an instance in the ConOut environment variable,
398 // then install EfiConsoleOutDeviceGuid onto ControllerHandle
400 Status
= ConPlatformUpdateDeviceVariable (
406 if (!EFI_ERROR (Status
)) {
408 Status
= gBS
->InstallMultipleProtocolInterfaces (
410 &gEfiConsoleOutDeviceGuid
,
416 // If the device path is an instance in the StdErr environment variable,
417 // then install EfiStandardErrorDeviceGuid onto ControllerHandle
419 Status
= ConPlatformUpdateDeviceVariable (
424 if (!EFI_ERROR (Status
)) {
426 gBS
->InstallMultipleProtocolInterfaces (
428 &gEfiStandardErrorDeviceGuid
,
437 &gEfiSimpleTextOutProtocolGuid
,
438 This
->DriverBindingHandle
,
449 ConPlatformTextInDriverBindingStop (
450 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
451 IN EFI_HANDLE ControllerHandle
,
452 IN UINTN NumberOfChildren
,
453 IN EFI_HANDLE
*ChildHandleBuffer
460 (Standard DriverBinding Protocol Stop() function)
469 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
472 // hot plug device is not included into the console associated variables,
473 // so no need to check variable for those hot plug devices.
475 if (!IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
477 // Get the Device Path Protocol so the environment variables can be updated
479 Status
= gBS
->OpenProtocol (
481 &gEfiDevicePathProtocolGuid
,
482 (VOID
**) &DevicePath
,
483 This
->DriverBindingHandle
,
485 EFI_OPEN_PROTOCOL_GET_PROTOCOL
487 if (!EFI_ERROR (Status
)) {
489 // Remove DevicePath from ConInDev
491 ConPlatformUpdateDeviceVariable (
499 // Uninstall the Console Device GUIDs from Controller Handle
501 ConPlatformUnInstallProtocol (
504 &gEfiConsoleInDeviceGuid
508 // Close the Simple Input Protocol
512 &gEfiSimpleTextInProtocolGuid
,
513 This
->DriverBindingHandle
,
522 ConPlatformTextOutDriverBindingStop (
523 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
524 IN EFI_HANDLE ControllerHandle
,
525 IN UINTN NumberOfChildren
,
526 IN EFI_HANDLE
*ChildHandleBuffer
533 (Standard DriverBinding Protocol Stop() function)
542 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
545 // hot plug device is not included into the console associated variables,
546 // so no need to check variable for those hot plug devices.
548 if (!IsHotPlugDevice (This
->DriverBindingHandle
, ControllerHandle
)) {
550 // Get the Device Path Protocol so the environment variables can be updated
552 Status
= gBS
->OpenProtocol (
554 &gEfiDevicePathProtocolGuid
,
555 (VOID
**) &DevicePath
,
556 This
->DriverBindingHandle
,
558 EFI_OPEN_PROTOCOL_GET_PROTOCOL
560 if (!EFI_ERROR (Status
)) {
562 // Remove DevicePath from ConOutDev, and StdErrDev
564 ConPlatformUpdateDeviceVariable (
569 ConPlatformUpdateDeviceVariable (
577 // Uninstall the Console Device GUIDs from Controller Handle
579 ConPlatformUnInstallProtocol (
582 &gEfiConsoleOutDeviceGuid
585 ConPlatformUnInstallProtocol (
588 &gEfiStandardErrorDeviceGuid
592 // Close the Simple Text Output Protocol
596 &gEfiSimpleTextOutProtocolGuid
,
597 This
->DriverBindingHandle
,
606 ConPlatformUnInstallProtocol (
607 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
608 IN EFI_HANDLE Handle
,
609 IN EFI_GUID
*ProtocolGuid
614 Status
= gBS
->OpenProtocol (
618 This
->DriverBindingHandle
,
620 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
623 if (!EFI_ERROR (Status
)) {
624 gBS
->UninstallMultipleProtocolInterfaces (
636 ConPlatformGetVariable (
642 Read the EFI variable (Name) and return a dynamically allocated
643 buffer, and the size of the buffer. On failure return NULL.
646 Name - String part of EFI variable name
649 Dynamically allocated memory that contains a copy of the EFI variable.
650 Caller is repsoncible freeing the buffer.
652 NULL - Variable was not read
664 // Test to see if the variable exists. If it doesn't reuturn NULL
666 Status
= gRT
->GetVariable (
668 &gEfiGlobalVariableGuid
,
674 if (Status
== EFI_BUFFER_TOO_SMALL
) {
676 // Allocate the buffer to return
678 Buffer
= AllocatePool (BufferSize
);
679 if (Buffer
== NULL
) {
683 // Read variable into the allocated buffer.
685 Status
= gRT
->GetVariable (
687 &gEfiGlobalVariableGuid
,
692 if (EFI_ERROR (Status
)) {
702 ConPlatformMatchDevicePaths (
703 IN EFI_DEVICE_PATH_PROTOCOL
* Multi
,
704 IN EFI_DEVICE_PATH_PROTOCOL
* Single
,
705 IN EFI_DEVICE_PATH_PROTOCOL
**NewDevicePath OPTIONAL
,
711 Function compares a device path data structure to that of all the nodes of a
712 second device path instance.
715 Multi - A pointer to a multi-instance device path data structure.
717 Single - A pointer to a single-instance device path data structure.
719 NewDevicePath - If Delete is TRUE, this parameter must not be null, and it
720 points to the remaining device path data structure.
721 (remaining device path = Multi - Single.)
723 Delete - If TRUE, means removing Single from Multi.
724 If FALSE, the routine just check whether Single matches
725 with any instance in Multi.
729 The function returns EFI_SUCCESS if the Single is contained within Multi.
730 Otherwise, EFI_NOT_FOUND is returned.
734 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
735 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
736 EFI_DEVICE_PATH_PROTOCOL
*DevicePathInst
;
740 // The passed in DevicePath should not be NULL
742 if ((!Multi
) || (!Single
)) {
743 return EFI_NOT_FOUND
;
746 // if performing Delete operation, the NewDevicePath must not be NULL.
748 TempDevicePath
= NULL
;
751 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
754 // search for the match of 'Single' in 'Multi'
756 while (DevicePathInst
) {
757 if (CompareMem (Single
, DevicePathInst
, Size
) == 0) {
759 FreePool (DevicePathInst
);
764 TempDevicePath
= AppendDevicePathInstance (
771 FreePool (DevicePathInst
);
772 DevicePathInst
= GetNextDevicePathInstance (&DevicePath
, &Size
);
776 *NewDevicePath
= TempDevicePath
;
780 return EFI_NOT_FOUND
;
784 ConPlatformUpdateDeviceVariable (
785 IN CHAR16
*VariableName
,
786 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
787 IN CONPLATFORM_VAR_OPERATION Operation
803 EFI_DEVICE_PATH_PROTOCOL
*VariableDevicePath
;
804 EFI_DEVICE_PATH_PROTOCOL
*NewVariableDevicePath
;
806 VariableDevicePath
= NULL
;
807 NewVariableDevicePath
= NULL
;
810 // Get Variable according to variable name.
811 // The memory for Variable is allocated within ConPlatformGetVarible(),
812 // it is the caller's responsibility to free the memory before return.
814 VariableDevicePath
= ConPlatformGetVariable (VariableName
);
816 if (Operation
!= DELETE
) {
818 Status
= ConPlatformMatchDevicePaths (
825 if ((Operation
== CHECK
) || (!EFI_ERROR (Status
))) {
827 // The device path is already in the variable
829 if (VariableDevicePath
!= NULL
) {
830 FreePool (VariableDevicePath
);
836 // The device path is not in variable. Append DevicePath to the
837 // environment variable that is a multi-instance device path.
839 Status
= EFI_SUCCESS
;
840 NewVariableDevicePath
= AppendDevicePathInstance (
844 if (NewVariableDevicePath
== NULL
) {
845 Status
= EFI_OUT_OF_RESOURCES
;
850 // Remove DevicePath from the environment variable that
851 // is a multi-instance device path.
853 Status
= ConPlatformMatchDevicePaths (
856 &NewVariableDevicePath
,
861 if (VariableDevicePath
!= NULL
) {
862 FreePool (VariableDevicePath
);
865 if (EFI_ERROR (Status
)) {
869 if (NewVariableDevicePath
!= NULL
) {
870 Status
= gRT
->SetVariable (
872 &gEfiGlobalVariableGuid
,
873 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
874 GetDevicePathSize (NewVariableDevicePath
),
875 NewVariableDevicePath
878 FreePool (NewVariableDevicePath
);
886 EFI_HANDLE DriverBindingHandle
,
887 EFI_HANDLE ControllerHandle
893 // HotPlugDeviceGuid indicates ControllerHandle stands for a hot plug device.
895 Status
= gBS
->OpenProtocol (
897 &gEfiHotPlugDeviceGuid
,
901 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
903 if (EFI_ERROR (Status
)) {