Clean up gEfiHotPlugDeviceGuid in ConPlatformDxe.
[edk2.git] / MdeModulePkg / Universal / Console / ConPlatformDxe / ConPlatform.c
blob0cb07a2971aa2a5f505c3495bc28e339739c2221
1 /** @file
2 Console Platform DXE Driver, install Console Device Guids and update Console
3 Environment Variables.
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.
14 **/
16 #include "ConPlatform.h"
19 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextInDriverBinding = {
20 ConPlatformTextInDriverBindingSupported,
21 ConPlatformTextInDriverBindingStart,
22 ConPlatformTextInDriverBindingStop,
23 0xa,
24 NULL,
25 NULL
28 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextOutDriverBinding = {
29 ConPlatformTextOutDriverBindingSupported,
30 ConPlatformTextOutDriverBindingStart,
31 ConPlatformTextOutDriverBindingStop,
32 0xa,
33 NULL,
34 NULL
37 /**
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.
48 **/
49 EFI_STATUS
50 EFIAPI
51 InitializeConPlatform(
52 IN EFI_HANDLE ImageHandle,
53 IN EFI_SYSTEM_TABLE *SystemTable
56 EFI_STATUS Status;
58 Status = EfiLibInstallDriverBindingComponentName2 (
59 ImageHandle,
60 SystemTable,
61 &gConPlatformTextInDriverBinding,
62 ImageHandle,
63 &gConPlatformComponentName,
64 &gConPlatformComponentName2
66 ASSERT_EFI_ERROR (Status);
68 Status = EfiLibInstallDriverBindingComponentName2 (
69 ImageHandle,
70 SystemTable,
71 &gConPlatformTextOutDriverBinding,
72 NULL,
73 &gConPlatformComponentName,
74 &gConPlatformComponentName2
76 ASSERT_EFI_ERROR (Status);
78 return EFI_SUCCESS;
82 /**
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
88 device to start.
90 @retval EFI_SUCCESS This driver supports this device.
91 @retval other This driver does not support this device.
93 **/
94 EFI_STATUS
95 EFIAPI
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 (
103 This,
104 ControllerHandle,
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
116 device to start.
118 @retval EFI_SUCCESS This driver supports this device.
119 @retval other This driver does not support this device.
122 EFI_STATUS
123 EFIAPI
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 (
131 This,
132 ControllerHandle,
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.
149 EFI_STATUS
150 ConPlatformDriverBindingSupported (
151 IN EFI_DRIVER_BINDING_PROTOCOL *This,
152 IN EFI_HANDLE ControllerHandle,
153 IN EFI_GUID *ProtocolGuid
156 EFI_STATUS Status;
157 VOID *Interface;
160 // Test to see if this is a physical device by checking if
161 // it has a Device Path Protocol.
163 Status = gBS->OpenProtocol (
164 ControllerHandle,
165 &gEfiDevicePathProtocolGuid,
166 NULL,
167 This->DriverBindingHandle,
168 ControllerHandle,
169 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
171 if (EFI_ERROR (Status)) {
172 return Status;
175 // Test to see if this device supports the specified Protocol.
177 Status = gBS->OpenProtocol (
178 ControllerHandle,
179 ProtocolGuid,
180 (VOID **) &Interface,
181 This->DriverBindingHandle,
182 ControllerHandle,
183 EFI_OPEN_PROTOCOL_BY_DRIVER
185 if (EFI_ERROR (Status)) {
186 return Status;
189 gBS->CloseProtocol (
190 ControllerHandle,
191 ProtocolGuid,
192 This->DriverBindingHandle,
193 ControllerHandle
196 return EFI_SUCCESS;
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
211 device to start.
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.
218 EFI_STATUS
219 EFIAPI
220 ConPlatformTextInDriverBindingStart (
221 IN EFI_DRIVER_BINDING_PROTOCOL *This,
222 IN EFI_HANDLE ControllerHandle,
223 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
226 EFI_STATUS Status;
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 (
234 ControllerHandle,
235 &gEfiDevicePathProtocolGuid,
236 (VOID **) &DevicePath,
237 This->DriverBindingHandle,
238 ControllerHandle,
239 EFI_OPEN_PROTOCOL_GET_PROTOCOL
241 if (EFI_ERROR (Status)) {
242 return Status;
245 // Open the Simple Text Input Protocol BY_DRIVER
247 Status = gBS->OpenProtocol (
248 ControllerHandle,
249 &gEfiSimpleTextInProtocolGuid,
250 (VOID **) &TextIn,
251 This->DriverBindingHandle,
252 ControllerHandle,
253 EFI_OPEN_PROTOCOL_BY_DRIVER
255 if (EFI_ERROR (Status)) {
256 return 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 (DevicePath)) {
265 gBS->InstallMultipleProtocolInterfaces (
266 &ControllerHandle,
267 &gEfiConsoleInDeviceGuid,
268 NULL,
269 NULL
271 } else {
273 // If it is not a hot-plug device, append the device path to the
274 // ConInDev environment variable
276 ConPlatformUpdateDeviceVariable (
277 L"ConInDev",
278 DevicePath,
279 APPEND
283 // If the device path is successfully added to the ConIn environment variable,
284 // then install EfiConsoleInDeviceGuid onto ControllerHandle
286 Status = ConPlatformUpdateDeviceVariable (
287 L"ConIn",
288 DevicePath,
289 CHECK
292 if (!EFI_ERROR (Status)) {
293 gBS->InstallMultipleProtocolInterfaces (
294 &ControllerHandle,
295 &gEfiConsoleInDeviceGuid,
296 NULL,
297 NULL
299 } else {
300 gBS->CloseProtocol (
301 ControllerHandle,
302 &gEfiSimpleTextInProtocolGuid,
303 This->DriverBindingHandle,
304 ControllerHandle
309 return EFI_SUCCESS;
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, ErrOutDev.
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
325 device to start.
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
332 EFI_STATUS
333 EFIAPI
334 ConPlatformTextOutDriverBindingStart (
335 IN EFI_DRIVER_BINDING_PROTOCOL *This,
336 IN EFI_HANDLE ControllerHandle,
337 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
340 EFI_STATUS Status;
341 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
342 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;
343 BOOLEAN NeedClose;
345 NeedClose = TRUE;
348 // Get the Device Path Protocol so the environment variables can be updated
350 Status = gBS->OpenProtocol (
351 ControllerHandle,
352 &gEfiDevicePathProtocolGuid,
353 (VOID **) &DevicePath,
354 This->DriverBindingHandle,
355 ControllerHandle,
356 EFI_OPEN_PROTOCOL_GET_PROTOCOL
358 if (EFI_ERROR (Status)) {
359 return Status;
362 // Open the Simple Text Output Protocol BY_DRIVER
364 Status = gBS->OpenProtocol (
365 ControllerHandle,
366 &gEfiSimpleTextOutProtocolGuid,
367 (VOID **) &TextOut,
368 This->DriverBindingHandle,
369 ControllerHandle,
370 EFI_OPEN_PROTOCOL_BY_DRIVER
372 if (EFI_ERROR (Status)) {
373 return Status;
376 // Check the device handle, if it is a hot plug device,
377 // do not put the device path into ConOutDev and ErrOutDev,
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 (DevicePath)) {
382 gBS->InstallMultipleProtocolInterfaces (
383 &ControllerHandle,
384 &gEfiConsoleOutDeviceGuid,
385 NULL,
386 NULL
388 } else {
390 // If it is not a hot-plug device, first append the device path to the
391 // ConOutDev environment variable
393 ConPlatformUpdateDeviceVariable (
394 L"ConOutDev",
395 DevicePath,
396 APPEND
399 // Then append the device path to the ErrOutDev environment variable
401 ConPlatformUpdateDeviceVariable (
402 L"ErrOutDev",
403 DevicePath,
404 APPEND
408 // If the device path is successfully added to the ConOut environment variable,
409 // then install EfiConsoleOutDeviceGuid onto ControllerHandle
411 Status = ConPlatformUpdateDeviceVariable (
412 L"ConOut",
413 DevicePath,
414 CHECK
417 if (!EFI_ERROR (Status)) {
418 NeedClose = FALSE;
419 Status = gBS->InstallMultipleProtocolInterfaces (
420 &ControllerHandle,
421 &gEfiConsoleOutDeviceGuid,
422 NULL,
423 NULL
427 // If the device path is successfully added to the ErrOut environment variable,
428 // then install EfiStandardErrorDeviceGuid onto ControllerHandle
430 Status = ConPlatformUpdateDeviceVariable (
431 L"ErrOut",
432 DevicePath,
433 CHECK
435 if (!EFI_ERROR (Status)) {
436 NeedClose = FALSE;
437 gBS->InstallMultipleProtocolInterfaces (
438 &ControllerHandle,
439 &gEfiStandardErrorDeviceGuid,
440 NULL,
441 NULL
445 if (NeedClose) {
446 gBS->CloseProtocol (
447 ControllerHandle,
448 &gEfiSimpleTextOutProtocolGuid,
449 This->DriverBindingHandle,
450 ControllerHandle
455 return EFI_SUCCESS;
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
472 EFI_STATUS
473 EFIAPI
474 ConPlatformTextInDriverBindingStop (
475 IN EFI_DRIVER_BINDING_PROTOCOL *This,
476 IN EFI_HANDLE ControllerHandle,
477 IN UINTN NumberOfChildren,
478 IN EFI_HANDLE *ChildHandleBuffer
481 EFI_STATUS Status;
482 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
485 // Get the Device Path Protocol firstly
487 Status = gBS->OpenProtocol (
488 ControllerHandle,
489 &gEfiDevicePathProtocolGuid,
490 (VOID **) &DevicePath,
491 This->DriverBindingHandle,
492 ControllerHandle,
493 EFI_OPEN_PROTOCOL_GET_PROTOCOL
496 if (EFI_ERROR (Status)) {
497 return Status;
501 // If it is not a hot-plug device, first delete it from the ConInDev variable.
503 if (!IsHotPlugDevice (DevicePath)) {
505 // Remove DevicePath from ConInDev
507 ConPlatformUpdateDeviceVariable (
508 L"ConInDev",
509 DevicePath,
510 DELETE
515 // Uninstall the Console Device GUIDs from Controller Handle
517 ConPlatformUnInstallProtocol (
518 This,
519 ControllerHandle,
520 &gEfiConsoleInDeviceGuid
524 // Close the Simple Text Input Protocol
526 gBS->CloseProtocol (
527 ControllerHandle,
528 &gEfiSimpleTextInProtocolGuid,
529 This->DriverBindingHandle,
530 ControllerHandle
533 return EFI_SUCCESS;
538 Stop this driver on ControllerHandle by removing Console Out Devcice GUID
539 and closing the Simple Text Output protocol on ControllerHandle.
541 @param This Protocol instance pointer.
542 @param ControllerHandle Handle of device to stop driver on
543 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
544 children is zero stop the entire bus driver.
545 @param ChildHandleBuffer List of Child Handles to Stop.
547 @retval EFI_SUCCESS This driver is removed ControllerHandle
548 @retval other This driver was not removed from this device
551 EFI_STATUS
552 EFIAPI
553 ConPlatformTextOutDriverBindingStop (
554 IN EFI_DRIVER_BINDING_PROTOCOL *This,
555 IN EFI_HANDLE ControllerHandle,
556 IN UINTN NumberOfChildren,
557 IN EFI_HANDLE *ChildHandleBuffer
560 EFI_STATUS Status;
561 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
564 // Get the Device Path Protocol firstly
566 Status = gBS->OpenProtocol (
567 ControllerHandle,
568 &gEfiDevicePathProtocolGuid,
569 (VOID **) &DevicePath,
570 This->DriverBindingHandle,
571 ControllerHandle,
572 EFI_OPEN_PROTOCOL_GET_PROTOCOL
575 if (EFI_ERROR (Status)) {
576 return Status;
580 // If it is not a hot-plug device, first delete it from the ConOutDev and ErrOutDev variable.
582 if (!IsHotPlugDevice (DevicePath)) {
584 // Remove DevicePath from ConOutDev, and ErrOutDev
586 ConPlatformUpdateDeviceVariable (
587 L"ConOutDev",
588 DevicePath,
589 DELETE
591 ConPlatformUpdateDeviceVariable (
592 L"ErrOutDev",
593 DevicePath,
594 DELETE
599 // Uninstall the Console Device GUIDs from Controller Handle
601 ConPlatformUnInstallProtocol (
602 This,
603 ControllerHandle,
604 &gEfiConsoleOutDeviceGuid
607 ConPlatformUnInstallProtocol (
608 This,
609 ControllerHandle,
610 &gEfiStandardErrorDeviceGuid
614 // Close the Simple Text Output Protocol
616 gBS->CloseProtocol (
617 ControllerHandle,
618 &gEfiSimpleTextOutProtocolGuid,
619 This->DriverBindingHandle,
620 ControllerHandle
623 return EFI_SUCCESS;
628 Uninstall the specified protocol.
630 @param This Protocol instance pointer.
631 @param Handle Handle of device to uninstall protocol on.
632 @param ProtocolGuid The specified protocol need to be uninstalled.
635 VOID
636 ConPlatformUnInstallProtocol (
637 IN EFI_DRIVER_BINDING_PROTOCOL *This,
638 IN EFI_HANDLE Handle,
639 IN EFI_GUID *ProtocolGuid
642 EFI_STATUS Status;
644 Status = gBS->OpenProtocol (
645 Handle,
646 ProtocolGuid,
647 NULL,
648 This->DriverBindingHandle,
649 Handle,
650 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
653 if (!EFI_ERROR (Status)) {
654 gBS->UninstallMultipleProtocolInterfaces (
655 Handle,
656 ProtocolGuid,
657 NULL,
658 NULL
662 return ;
666 Get the necessary size of buffer and read the variable.
668 First get the necessary size of buffer. Then read the
669 EFI variable (Name) and return a dynamically allocated
670 buffer. On failure return NULL.
672 @param Name String part of EFI variable name
674 @return Dynamically allocated memory that contains a copy of the EFI variable.
675 Caller is repsoncible freeing the buffer. Return NULL means Variable
676 was not read.
679 VOID *
680 ConPlatformGetVariable (
681 IN CHAR16 *Name
684 EFI_STATUS Status;
685 VOID *Buffer;
686 UINTN BufferSize;
688 BufferSize = 0;
689 Buffer = NULL;
692 // Test to see if the variable exists. If it doesn't, return NULL.
694 Status = gRT->GetVariable (
695 Name,
696 &gEfiGlobalVariableGuid,
697 NULL,
698 &BufferSize,
699 Buffer
702 if (Status == EFI_BUFFER_TOO_SMALL) {
704 // Allocate the buffer to return
706 Buffer = AllocatePool (BufferSize);
707 if (Buffer == NULL) {
708 return NULL;
711 // Read variable into the allocated buffer.
713 Status = gRT->GetVariable (
714 Name,
715 &gEfiGlobalVariableGuid,
716 NULL,
717 &BufferSize,
718 Buffer
720 if (EFI_ERROR (Status)) {
721 FreePool (Buffer);
723 // To make sure Buffer is NULL if any error occurs.
725 Buffer = NULL;
729 return Buffer;
733 Function compares a device path data structure to that of all the nodes of a
734 second device path instance.
736 @param Multi A pointer to a multi-instance device path data structure.
737 @param Single A pointer to a single-instance device path data structure.
738 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
739 points to the remaining device path data structure.
740 (remaining device path = Multi - Single.)
741 @param Delete If TRUE, means removing Single from Multi.
742 If FALSE, the routine just check whether Single matches
743 with any instance in Multi.
745 @retval EFI_SUCCESS If the Single is contained within Multi.
746 @retval EFI_NOT_FOUND If the Single is not contained within Multi.
747 @retval EFI_INVALID_PARAMETER Multi is NULL.
748 @retval EFI_INVALID_PARAMETER Single is NULL.
749 @retval EFI_INVALID_PARAMETER NewDevicePath is NULL when Delete is TRUE.
752 EFI_STATUS
753 ConPlatformMatchDevicePaths (
754 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
755 IN EFI_DEVICE_PATH_PROTOCOL *Single,
756 OUT EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,
757 IN BOOLEAN Delete
760 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
761 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath1;
762 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath2;
763 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;
764 UINTN Size;
767 // The passed in DevicePath should not be NULL
769 if ((Multi == NULL) || (Single == NULL)) {
770 return EFI_INVALID_PARAMETER;
774 // If performing Delete operation, the NewDevicePath must not be NULL.
776 if (Delete) {
777 if (NewDevicePath == NULL) {
778 return EFI_INVALID_PARAMETER;
782 TempDevicePath1 = NULL;
784 DevicePath = Multi;
785 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
788 // Search for the match of 'Single' in 'Multi'
790 while (DevicePathInst != NULL) {
791 if (CompareMem (Single, DevicePathInst, Size) == 0) {
792 if (!Delete) {
794 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.
796 FreePool (DevicePathInst);
797 return EFI_SUCCESS;
799 } else {
800 if (Delete) {
802 // If the node of Multi does not match Single, then added it back to the result.
803 // That is, the node matching Single will be dropped and deleted from result.
805 TempDevicePath2 = AppendDevicePathInstance (
806 TempDevicePath1,
807 DevicePathInst
809 if (TempDevicePath1 != NULL) {
810 FreePool (TempDevicePath1);
812 TempDevicePath1 = TempDevicePath2;
816 FreePool (DevicePathInst);
817 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
820 if (Delete) {
822 // Return the new device path data structure with specified node deleted.
824 *NewDevicePath = TempDevicePath1;
825 return EFI_SUCCESS;
828 return EFI_NOT_FOUND;
832 Update console environment variables.
834 @param VariableName Console environment variables, ConOutDev, ConInDev
835 ErrOutDev, ConIn ,ConOut or ErrOut.
836 @param DevicePath Console devcie's device path.
837 @param Operation Variable operations, including APPEND, CHECK and DELETE.
839 @retval EFI_SUCCESS Variable operates successfully.
840 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
841 @retval other Variable updating failed.
844 EFI_STATUS
845 ConPlatformUpdateDeviceVariable (
846 IN CHAR16 *VariableName,
847 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
848 IN CONPLATFORM_VAR_OPERATION Operation
851 EFI_STATUS Status;
852 EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;
853 EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;
855 VariableDevicePath = NULL;
856 NewVariableDevicePath = NULL;
859 // Get Variable according to variable name.
860 // The memory for Variable is allocated within ConPlatformGetVarible(),
861 // it is the caller's responsibility to free the memory before return.
863 VariableDevicePath = ConPlatformGetVariable (VariableName);
865 if (Operation != DELETE) {
867 // Match specified DevicePath in Console Variable.
869 Status = ConPlatformMatchDevicePaths (
870 VariableDevicePath,
871 DevicePath,
872 NULL,
873 FALSE
876 if ((Operation == CHECK) || (!EFI_ERROR (Status))) {
878 // Branch here includes 2 cases:
879 // 1. Operation is CHECK, simply return Status.
880 // 2. Operation is APPEND, and device path already exists in variable, also return.
882 if (VariableDevicePath != NULL) {
883 FreePool (VariableDevicePath);
886 return Status;
889 // We reach here to append a device path that does not exist in variable.
891 Status = EFI_SUCCESS;
892 NewVariableDevicePath = AppendDevicePathInstance (
893 VariableDevicePath,
894 DevicePath
896 if (NewVariableDevicePath == NULL) {
897 Status = EFI_OUT_OF_RESOURCES;
900 } else {
902 // We reach here to remove DevicePath from the environment variable that
903 // is a multi-instance device path.
905 Status = ConPlatformMatchDevicePaths (
906 VariableDevicePath,
907 DevicePath,
908 &NewVariableDevicePath,
909 TRUE
913 if (VariableDevicePath != NULL) {
914 FreePool (VariableDevicePath);
917 if (EFI_ERROR (Status)) {
918 return Status;
921 if (NewVariableDevicePath != NULL) {
923 // Update Console Environment Variable.
925 Status = gRT->SetVariable (
926 VariableName,
927 &gEfiGlobalVariableGuid,
928 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
929 GetDevicePathSize (NewVariableDevicePath),
930 NewVariableDevicePath
933 FreePool (NewVariableDevicePath);
936 return Status;
940 Check if the device supports hot-plug through its device path.
942 This function could be updated to check more types of Hot Plug devices.
943 Currently, it checks USB and PCCard device.
945 @param DevicePath Pointer to device's device path.
947 @retval TRUE The devcie is a hot-plug device
948 @retval FALSE The devcie is not a hot-plug device.
951 BOOLEAN
952 IsHotPlugDevice (
953 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
957 // Check device whether is hot plug device or not throught Device Path
959 if ((DevicePathType (DevicePath) == MESSAGING_DEVICE_PATH) &&
960 (DevicePathSubType (DevicePath) == MSG_USB_DP ||
961 DevicePathSubType (DevicePath) == MSG_USB_CLASS_DP ||
962 DevicePathSubType (DevicePath) == MSG_USB_WWID_DP)) {
964 // If Device is USB device
966 return TRUE;
968 if ((DevicePathType (DevicePath) == HARDWARE_DEVICE_PATH) &&
969 (DevicePathSubType (DevicePath) == HW_PCCARD_DP)) {
971 // If Device is PCCard
973 return TRUE;
976 return FALSE;