2 Console Platfrom DXE Driver, install Console Device Guids and update Console
\r
3 Environment Variables.
\r
5 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
\r
6 All rights reserved. This program and the accompanying materials
\r
7 are licensed and made available under the terms and conditions of the BSD License
\r
8 which accompanies this distribution. The full text of the license may be found at
\r
9 http://opensource.org/licenses/bsd-license.php
\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
\r
16 #include <ConPlatform.h>
\r
19 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextInDriverBinding = {
\r
20 ConPlatformTextInDriverBindingSupported,
\r
21 ConPlatformTextInDriverBindingStart,
\r
22 ConPlatformTextInDriverBindingStop,
\r
28 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextOutDriverBinding = {
\r
29 ConPlatformTextOutDriverBindingSupported,
\r
30 ConPlatformTextOutDriverBindingStart,
\r
31 ConPlatformTextOutDriverBindingStop,
\r
38 The user Entry Point for module ConPlatform. The user code starts with this function.
\r
40 @param[in] ImageHandle The firmware allocated handle for the EFI image.
\r
41 @param[in] SystemTable A pointer to the EFI System Table.
\r
43 @retval EFI_SUCCESS The entry point is executed successfully.
\r
44 @retval other Some error occurs when executing this entry point.
\r
49 InitializeConPlatform(
\r
50 IN EFI_HANDLE ImageHandle,
\r
51 IN EFI_SYSTEM_TABLE *SystemTable
\r
57 // Install driver model protocol(s).
\r
59 Status = EfiLibInstallDriverBindingComponentName2 (
\r
62 &gConPlatformTextInDriverBinding,
\r
64 &gConPlatformComponentName,
\r
65 &gConPlatformComponentName2
\r
67 ASSERT_EFI_ERROR (Status);
\r
69 Status = EfiLibInstallDriverBindingComponentName2 (
\r
72 &gConPlatformTextOutDriverBinding,
\r
74 &gConPlatformComponentName,
\r
75 &gConPlatformComponentName2
\r
77 ASSERT_EFI_ERROR (Status);
\r
85 Test to see if EFI Text In Protocol could be supported on the ControllerHandle.
\r
87 @param This Protocol instance pointer.
\r
88 @param ControllerHandle Handle of device to test.
\r
89 @param RemainingDevicePath Optional parameter use to pick a specific child
\r
92 @retval EFI_SUCCESS This driver supports this device
\r
93 @retval other This driver does not support this device
\r
98 ConPlatformTextInDriverBindingSupported (
\r
99 IN EFI_DRIVER_BINDING_PROTOCOL *This,
\r
100 IN EFI_HANDLE ControllerHandle,
\r
101 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
\r
104 return ConPlatformDriverBindingSupported (
\r
107 RemainingDevicePath,
\r
108 &gEfiSimpleTextInProtocolGuid
\r
114 Test to see if EFI Text Out Protocol could be supported on the ControllerHandle.
\r
116 @param This Protocol instance pointer.
\r
117 @param ControllerHandle Handle of device to test.
\r
118 @param RemainingDevicePath Optional parameter use to pick a specific child
\r
121 @retval EFI_SUCCESS This driver supports this device
\r
122 @retval other This driver does not support this device
\r
127 ConPlatformTextOutDriverBindingSupported (
\r
128 IN EFI_DRIVER_BINDING_PROTOCOL *This,
\r
129 IN EFI_HANDLE ControllerHandle,
\r
130 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
\r
133 return ConPlatformDriverBindingSupported (
\r
136 RemainingDevicePath,
\r
137 &gEfiSimpleTextOutProtocolGuid
\r
143 Test to see if specific Protocol could be supported on the ControllerHandle.
\r
145 @param This Protocol instance pointer.
\r
146 @param ControllerHandle Handle of device to test.
\r
147 @param RemainingDevicePath Optional parameter use to pick a specific child
\r
149 @param ProtocolGuid The specfic protocol.
\r
151 @retval EFI_SUCCESS This driver supports this device
\r
152 @retval other This driver does not support this device
\r
156 ConPlatformDriverBindingSupported (
\r
157 IN EFI_DRIVER_BINDING_PROTOCOL *This,
\r
158 IN EFI_HANDLE ControllerHandle,
\r
159 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath,
\r
160 IN EFI_GUID *ProtocolGuid
\r
167 // Test to see if this is a physical device by checking to see if
\r
168 // it has a Device Path Protocol
\r
170 Status = gBS->OpenProtocol (
\r
172 &gEfiDevicePathProtocolGuid,
\r
174 This->DriverBindingHandle,
\r
176 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
\r
178 if (EFI_ERROR (Status)) {
\r
182 // Test to see if this device supports the specific Protocol
\r
184 Status = gBS->OpenProtocol (
\r
187 (VOID **) &Interface,
\r
188 This->DriverBindingHandle,
\r
190 EFI_OPEN_PROTOCOL_BY_DRIVER
\r
192 if (EFI_ERROR (Status)) {
\r
196 gBS->CloseProtocol (
\r
199 This->DriverBindingHandle,
\r
203 return EFI_SUCCESS;
\r
207 Start this driver on ControllerHandle by opening Simple Text In protocol,
\r
208 reading Device Path, and installing Console Devcice In GUID on ControllerHandle.
\r
210 If this devcie is not one hot-plug devce, append its device path into the
\r
211 console environment variables ConInDev.
\r
213 @param This Protocol instance pointer.
\r
214 @param ControllerHandle Handle of device to bind driver to
\r
215 @param RemainingDevicePath Optional parameter use to pick a specific child
\r
218 @retval EFI_SUCCESS This driver is added to ControllerHandle
\r
219 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
\r
220 @retval other This driver does not support this device
\r
225 ConPlatformTextInDriverBindingStart (
\r
226 IN EFI_DRIVER_BINDING_PROTOCOL *This,
\r
227 IN EFI_HANDLE ControllerHandle,
\r
228 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
\r
232 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
\r
233 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *TextIn;
\r
236 // Get the Device Path Protocol so the environment variables can be updated
\r
238 Status = gBS->OpenProtocol (
\r
240 &gEfiDevicePathProtocolGuid,
\r
241 (VOID **) &DevicePath,
\r
242 This->DriverBindingHandle,
\r
244 EFI_OPEN_PROTOCOL_GET_PROTOCOL
\r
246 if (EFI_ERROR (Status)) {
\r
250 // Open the Simple Input Protocol BY_DRIVER
\r
252 Status = gBS->OpenProtocol (
\r
254 &gEfiSimpleTextInProtocolGuid,
\r
256 This->DriverBindingHandle,
\r
258 EFI_OPEN_PROTOCOL_BY_DRIVER
\r
260 if (EFI_ERROR (Status)) {
\r
264 // Check the device handle, if it is a hot plug device,
\r
265 // do not put the device path into ConInDev, and install
\r
266 // gEfiConsoleInDeviceGuid to the device handle directly.
\r
267 // The policy is, make hot plug device plug in and play immediately.
\r
269 if (IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
\r
270 gBS->InstallMultipleProtocolInterfaces (
\r
272 &gEfiConsoleInDeviceGuid,
\r
278 // Append the device path to the ConInDev environment variable
\r
280 ConPlatformUpdateDeviceVariable (
\r
287 // If the device path is an instance in the ConIn environment variable,
\r
288 // then install EfiConsoleInDeviceGuid onto ControllerHandle
\r
290 Status = ConPlatformUpdateDeviceVariable (
\r
296 if (!EFI_ERROR (Status)) {
\r
297 gBS->InstallMultipleProtocolInterfaces (
\r
299 &gEfiConsoleInDeviceGuid,
\r
304 gBS->CloseProtocol (
\r
306 &gEfiSimpleTextInProtocolGuid,
\r
307 This->DriverBindingHandle,
\r
313 return EFI_SUCCESS;
\r
317 Start this driver on ControllerHandle by opening Simple Text Out protocol,
\r
318 reading Device Path, and installing Console Devcice Out GUID, Standard Error
\r
319 Device GUID on ControllerHandle.
\r
321 If this devcie is not one hot-plug devce, append its device path into the
\r
322 console environment variables ConOutDev, StdErrDev.
\r
324 @param This Protocol instance pointer.
\r
325 @param ControllerHandle Handle of device to bind driver to
\r
326 @param RemainingDevicePath Optional parameter use to pick a specific child
\r
329 @retval EFI_SUCCESS This driver is added to ControllerHandle
\r
330 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
\r
331 @retval other This driver does not support this device
\r
336 ConPlatformTextOutDriverBindingStart (
\r
337 IN EFI_DRIVER_BINDING_PROTOCOL *This,
\r
338 IN EFI_HANDLE ControllerHandle,
\r
339 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
\r
343 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
\r
344 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;
\r
350 // Get the Device Path Protocol so the environment variables can be updated
\r
352 Status = gBS->OpenProtocol (
\r
354 &gEfiDevicePathProtocolGuid,
\r
355 (VOID **) &DevicePath,
\r
356 This->DriverBindingHandle,
\r
358 EFI_OPEN_PROTOCOL_GET_PROTOCOL
\r
360 if (EFI_ERROR (Status)) {
\r
364 // Open the Simple Text Output Protocol BY_DRIVER
\r
366 Status = gBS->OpenProtocol (
\r
368 &gEfiSimpleTextOutProtocolGuid,
\r
369 (VOID **) &TextOut,
\r
370 This->DriverBindingHandle,
\r
372 EFI_OPEN_PROTOCOL_BY_DRIVER
\r
374 if (EFI_ERROR (Status)) {
\r
378 // Check the device handle, if it is a hot plug device,
\r
379 // do not put the device path into ConOutDev and StdErrDev,
\r
380 // and install gEfiConsoleOutDeviceGuid to the device handle directly.
\r
381 // The policy is, make hot plug device plug in and play immediately.
\r
383 if (IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
\r
384 gBS->InstallMultipleProtocolInterfaces (
\r
386 &gEfiConsoleOutDeviceGuid,
\r
392 // Append the device path to the ConOutDev environment variable
\r
394 ConPlatformUpdateDeviceVariable (
\r
400 // Append the device path to the StdErrDev environment variable
\r
402 ConPlatformUpdateDeviceVariable (
\r
409 // If the device path is an instance in the ConOut environment variable,
\r
410 // then install EfiConsoleOutDeviceGuid onto ControllerHandle
\r
412 Status = ConPlatformUpdateDeviceVariable (
\r
418 if (!EFI_ERROR (Status)) {
\r
420 Status = gBS->InstallMultipleProtocolInterfaces (
\r
422 &gEfiConsoleOutDeviceGuid,
\r
428 // If the device path is an instance in the StdErr environment variable,
\r
429 // then install EfiStandardErrorDeviceGuid onto ControllerHandle
\r
431 Status = ConPlatformUpdateDeviceVariable (
\r
436 if (!EFI_ERROR (Status)) {
\r
438 gBS->InstallMultipleProtocolInterfaces (
\r
440 &gEfiStandardErrorDeviceGuid,
\r
447 gBS->CloseProtocol (
\r
449 &gEfiSimpleTextOutProtocolGuid,
\r
450 This->DriverBindingHandle,
\r
456 return EFI_SUCCESS;
\r
460 Stop this driver on ControllerHandle by removing Console Devcice In GUID
\r
461 and closing the Simple Text In protocol on ControllerHandle.
\r
463 @param This Protocol instance pointer.
\r
464 @param ControllerHandle Handle of device to stop driver on
\r
465 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
\r
466 children is zero stop the entire bus driver.
\r
467 @param ChildHandleBuffer List of Child Handles to Stop.
\r
469 @retval EFI_SUCCESS This driver is removed ControllerHandle
\r
470 @retval other This driver was not removed from this device
\r
475 ConPlatformTextInDriverBindingStop (
\r
476 IN EFI_DRIVER_BINDING_PROTOCOL *This,
\r
477 IN EFI_HANDLE ControllerHandle,
\r
478 IN UINTN NumberOfChildren,
\r
479 IN EFI_HANDLE *ChildHandleBuffer
\r
483 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
\r
486 // hot plug device is not included into the console associated variables,
\r
487 // so no need to check variable for those hot plug devices.
\r
489 if (!IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
\r
491 // Get the Device Path Protocol so the environment variables can be updated
\r
493 Status = gBS->OpenProtocol (
\r
495 &gEfiDevicePathProtocolGuid,
\r
496 (VOID **) &DevicePath,
\r
497 This->DriverBindingHandle,
\r
499 EFI_OPEN_PROTOCOL_GET_PROTOCOL
\r
501 if (!EFI_ERROR (Status)) {
\r
503 // Remove DevicePath from ConInDev
\r
505 ConPlatformUpdateDeviceVariable (
\r
513 // Uninstall the Console Device GUIDs from Controller Handle
\r
515 ConPlatformUnInstallProtocol (
\r
518 &gEfiConsoleInDeviceGuid
\r
522 // Close the Simple Input Protocol
\r
524 gBS->CloseProtocol (
\r
526 &gEfiSimpleTextInProtocolGuid,
\r
527 This->DriverBindingHandle,
\r
531 return EFI_SUCCESS;
\r
536 Stop this driver on ControllerHandle by removing Console Devcice Out GUID
\r
537 and closing the Simple Text Out protocol on ControllerHandle.
\r
539 @param This Protocol instance pointer.
\r
540 @param ControllerHandle Handle of device to stop driver on
\r
541 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
\r
542 children is zero stop the entire bus driver.
\r
543 @param ChildHandleBuffer List of Child Handles to Stop.
\r
545 @retval EFI_SUCCESS This driver is removed ControllerHandle
\r
546 @retval other This driver was not removed from this device
\r
551 ConPlatformTextOutDriverBindingStop (
\r
552 IN EFI_DRIVER_BINDING_PROTOCOL *This,
\r
553 IN EFI_HANDLE ControllerHandle,
\r
554 IN UINTN NumberOfChildren,
\r
555 IN EFI_HANDLE *ChildHandleBuffer
\r
559 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
\r
562 // hot plug device is not included into the console associated variables,
\r
563 // so no need to check variable for those hot plug devices.
\r
565 if (!IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
\r
567 // Get the Device Path Protocol so the environment variables can be updated
\r
569 Status = gBS->OpenProtocol (
\r
571 &gEfiDevicePathProtocolGuid,
\r
572 (VOID **) &DevicePath,
\r
573 This->DriverBindingHandle,
\r
575 EFI_OPEN_PROTOCOL_GET_PROTOCOL
\r
577 if (!EFI_ERROR (Status)) {
\r
579 // Remove DevicePath from ConOutDev, and StdErrDev
\r
581 ConPlatformUpdateDeviceVariable (
\r
586 ConPlatformUpdateDeviceVariable (
\r
594 // Uninstall the Console Device GUIDs from Controller Handle
\r
596 ConPlatformUnInstallProtocol (
\r
599 &gEfiConsoleOutDeviceGuid
\r
602 ConPlatformUnInstallProtocol (
\r
605 &gEfiStandardErrorDeviceGuid
\r
609 // Close the Simple Text Output Protocol
\r
611 gBS->CloseProtocol (
\r
613 &gEfiSimpleTextOutProtocolGuid,
\r
614 This->DriverBindingHandle,
\r
618 return EFI_SUCCESS;
\r
623 Unstall the specific protocol.
\r
625 @param This Protocol instance pointer.
\r
626 @param Handle Handle of device to unstall protocol on.
\r
627 @param ProtocolGuid The specific protocol need to be uninstalled.
\r
633 ConPlatformUnInstallProtocol (
\r
634 IN EFI_DRIVER_BINDING_PROTOCOL *This,
\r
635 IN EFI_HANDLE Handle,
\r
636 IN EFI_GUID *ProtocolGuid
\r
641 Status = gBS->OpenProtocol (
\r
645 This->DriverBindingHandle,
\r
647 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
\r
650 if (!EFI_ERROR (Status)) {
\r
651 gBS->UninstallMultipleProtocolInterfaces (
\r
663 Read the EFI variable (Name) and return a dynamically allocated
\r
664 buffer, and the size of the buffer. On failure return NULL.
\r
667 @param Name String part of EFI variable name
\r
669 @return Dynamically allocated memory that contains a copy of the EFI variable.
\r
670 Caller is repsoncible freeing the buffer.
\r
671 NULL - Variable was not read
\r
675 ConPlatformGetVariable (
\r
687 // Test to see if the variable exists. If it doesn't reuturn NULL
\r
689 Status = gRT->GetVariable (
\r
691 &gEfiGlobalVariableGuid,
\r
697 if (Status == EFI_BUFFER_TOO_SMALL) {
\r
699 // Allocate the buffer to return
\r
701 Buffer = AllocatePool (BufferSize);
\r
702 if (Buffer == NULL) {
\r
706 // Read variable into the allocated buffer.
\r
708 Status = gRT->GetVariable (
\r
710 &gEfiGlobalVariableGuid,
\r
715 if (EFI_ERROR (Status)) {
\r
725 Function compares a device path data structure to that of all the nodes of a
\r
726 second device path instance.
\r
729 @param Multi A pointer to a multi-instance device path data structure.
\r
730 @param Single A pointer to a single-instance device path data structure.
\r
731 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
\r
732 points to the remaining device path data structure.
\r
733 (remaining device path = Multi - Single.)
\r
734 @param Delete If TRUE, means removing Single from Multi.
\r
735 If FALSE, the routine just check whether Single matches
\r
736 with any instance in Multi.
\r
738 @return The function returns EFI_SUCCESS if the Single is contained within Multi.
\r
739 Otherwise, EFI_NOT_FOUND is returned.
\r
743 ConPlatformMatchDevicePaths (
\r
744 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
\r
745 IN EFI_DEVICE_PATH_PROTOCOL *Single,
\r
746 IN EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,
\r
750 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
\r
751 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
\r
752 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;
\r
756 // The passed in DevicePath should not be NULL
\r
758 if ((Multi == NULL) || (Single == NULL)) {
\r
759 return EFI_NOT_FOUND;
\r
762 // if performing Delete operation, the NewDevicePath must not be NULL.
\r
764 TempDevicePath = NULL;
\r
766 DevicePath = Multi;
\r
767 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
\r
770 // search for the match of 'Single' in 'Multi'
\r
772 while (DevicePathInst != NULL) {
\r
773 if (CompareMem (Single, DevicePathInst, Size) == 0) {
\r
775 FreePool (DevicePathInst);
\r
776 return EFI_SUCCESS;
\r
780 TempDevicePath = AppendDevicePathInstance (
\r
787 FreePool (DevicePathInst);
\r
788 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
\r
792 *NewDevicePath = TempDevicePath;
\r
793 return EFI_SUCCESS;
\r
796 return EFI_NOT_FOUND;
\r
800 Update console devicein console environment variables.
\r
802 @param VariableName Console environment variables, ConOutDev, ConInDev
\r
803 StdErrDev, ConIn or ConOut.
\r
804 @param DevicePath Console devcie's device path.
\r
805 @param Operation Variable operations, such as APPEND or DELETE.
\r
807 @retval EFI_SUCCESS Variable operates successfully.
\r
808 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
\r
809 @retval other Variable updating failed.
\r
813 ConPlatformUpdateDeviceVariable (
\r
814 IN CHAR16 *VariableName,
\r
815 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
\r
816 IN CONPLATFORM_VAR_OPERATION Operation
\r
820 EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;
\r
821 EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;
\r
823 VariableDevicePath = NULL;
\r
824 NewVariableDevicePath = NULL;
\r
827 // Get Variable according to variable name.
\r
828 // The memory for Variable is allocated within ConPlatformGetVarible(),
\r
829 // it is the caller's responsibility to free the memory before return.
\r
831 VariableDevicePath = ConPlatformGetVariable (VariableName);
\r
833 if (Operation != DELETE) {
\r
835 Status = ConPlatformMatchDevicePaths (
\r
836 VariableDevicePath,
\r
842 if ((Operation == CHECK) || (!EFI_ERROR (Status))) {
\r
844 // The device path is already in the variable
\r
846 if (VariableDevicePath != NULL) {
\r
847 FreePool (VariableDevicePath);
\r
853 // The device path is not in variable. Append DevicePath to the
\r
854 // environment variable that is a multi-instance device path.
\r
856 Status = EFI_SUCCESS;
\r
857 NewVariableDevicePath = AppendDevicePathInstance (
\r
858 VariableDevicePath,
\r
861 if (NewVariableDevicePath == NULL) {
\r
862 Status = EFI_OUT_OF_RESOURCES;
\r
867 // Remove DevicePath from the environment variable that
\r
868 // is a multi-instance device path.
\r
870 Status = ConPlatformMatchDevicePaths (
\r
871 VariableDevicePath,
\r
873 &NewVariableDevicePath,
\r
878 if (VariableDevicePath != NULL) {
\r
879 FreePool (VariableDevicePath);
\r
882 if (EFI_ERROR (Status)) {
\r
886 if (NewVariableDevicePath != NULL) {
\r
887 Status = gRT->SetVariable (
\r
889 &gEfiGlobalVariableGuid,
\r
890 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
\r
891 GetDevicePathSize (NewVariableDevicePath),
\r
892 NewVariableDevicePath
\r
895 FreePool (NewVariableDevicePath);
\r
902 Check if the device is one hot-plug supported.
\r
904 @param DriverBindingHandle Protocol instance pointer.
\r
905 @param ControllerHandle Handle of device to check.
\r
907 @retval TRUE The devcie is a hot-plug device
\r
908 @retval FALSE The devcie is not a hot-plug device.
\r
913 EFI_HANDLE DriverBindingHandle,
\r
914 EFI_HANDLE ControllerHandle
\r
920 // HotPlugDeviceGuid indicates ControllerHandle stands for a hot plug device.
\r
922 Status = gBS->OpenProtocol (
\r
924 &gEfiHotPlugDeviceGuid,
\r
926 DriverBindingHandle,
\r
928 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
\r
930 if (EFI_ERROR (Status)) {
\r