]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatform.c
0cb07a2971aa2a5f505c3495bc28e339739c2221
[mirror_edk2.git] / MdeModulePkg / Universal / Console / ConPlatformDxe / ConPlatform.c
1 /** @file
2 Console Platform DXE Driver, install Console Device Guids and update Console
3 Environment Variables.
4
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
10
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.
13
14 **/
15
16 #include "ConPlatform.h"
17
18
19 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextInDriverBinding = {
20 ConPlatformTextInDriverBindingSupported,
21 ConPlatformTextInDriverBindingStart,
22 ConPlatformTextInDriverBindingStop,
23 0xa,
24 NULL,
25 NULL
26 };
27
28 EFI_DRIVER_BINDING_PROTOCOL gConPlatformTextOutDriverBinding = {
29 ConPlatformTextOutDriverBindingSupported,
30 ConPlatformTextOutDriverBindingStart,
31 ConPlatformTextOutDriverBindingStop,
32 0xa,
33 NULL,
34 NULL
35 };
36
37 /**
38 Entrypoint of this module.
39
40 This function is the entrypoint of this module. It installs Driver Binding
41 Protocols together with Component Name Protocols.
42
43 @param ImageHandle The firmware allocated handle for the EFI image.
44 @param SystemTable A pointer to the EFI System Table.
45
46 @retval EFI_SUCCESS The entry point is executed successfully.
47
48 **/
49 EFI_STATUS
50 EFIAPI
51 InitializeConPlatform(
52 IN EFI_HANDLE ImageHandle,
53 IN EFI_SYSTEM_TABLE *SystemTable
54 )
55 {
56 EFI_STATUS Status;
57
58 Status = EfiLibInstallDriverBindingComponentName2 (
59 ImageHandle,
60 SystemTable,
61 &gConPlatformTextInDriverBinding,
62 ImageHandle,
63 &gConPlatformComponentName,
64 &gConPlatformComponentName2
65 );
66 ASSERT_EFI_ERROR (Status);
67
68 Status = EfiLibInstallDriverBindingComponentName2 (
69 ImageHandle,
70 SystemTable,
71 &gConPlatformTextOutDriverBinding,
72 NULL,
73 &gConPlatformComponentName,
74 &gConPlatformComponentName2
75 );
76 ASSERT_EFI_ERROR (Status);
77
78 return EFI_SUCCESS;
79 }
80
81
82 /**
83 Test to see if EFI_SIMPLE_TEXT_INPUT_PROTOCOL is supported on ControllerHandle.
84
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.
89
90 @retval EFI_SUCCESS This driver supports this device.
91 @retval other This driver does not support this device.
92
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
100 )
101 {
102 return ConPlatformDriverBindingSupported (
103 This,
104 ControllerHandle,
105 &gEfiSimpleTextInProtocolGuid
106 );
107 }
108
109
110 /**
111 Test to see if EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is supported on ControllerHandle.
112
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.
117
118 @retval EFI_SUCCESS This driver supports this device.
119 @retval other This driver does not support this device.
120
121 **/
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
128 )
129 {
130 return ConPlatformDriverBindingSupported (
131 This,
132 ControllerHandle,
133 &gEfiSimpleTextOutProtocolGuid
134 );
135 }
136
137
138 /**
139 Test to see if the specified protocol is supported on ControllerHandle.
140
141 @param This Protocol instance pointer.
142 @param ControllerHandle Handle of device to test.
143 @param ProtocolGuid The specfic protocol.
144
145 @retval EFI_SUCCESS This driver supports this device.
146 @retval other This driver does not support this device.
147
148 **/
149 EFI_STATUS
150 ConPlatformDriverBindingSupported (
151 IN EFI_DRIVER_BINDING_PROTOCOL *This,
152 IN EFI_HANDLE ControllerHandle,
153 IN EFI_GUID *ProtocolGuid
154 )
155 {
156 EFI_STATUS Status;
157 VOID *Interface;
158
159 //
160 // Test to see if this is a physical device by checking if
161 // it has a Device Path Protocol.
162 //
163 Status = gBS->OpenProtocol (
164 ControllerHandle,
165 &gEfiDevicePathProtocolGuid,
166 NULL,
167 This->DriverBindingHandle,
168 ControllerHandle,
169 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
170 );
171 if (EFI_ERROR (Status)) {
172 return Status;
173 }
174 //
175 // Test to see if this device supports the specified Protocol.
176 //
177 Status = gBS->OpenProtocol (
178 ControllerHandle,
179 ProtocolGuid,
180 (VOID **) &Interface,
181 This->DriverBindingHandle,
182 ControllerHandle,
183 EFI_OPEN_PROTOCOL_BY_DRIVER
184 );
185 if (EFI_ERROR (Status)) {
186 return Status;
187 }
188
189 gBS->CloseProtocol (
190 ControllerHandle,
191 ProtocolGuid,
192 This->DriverBindingHandle,
193 ControllerHandle
194 );
195
196 return EFI_SUCCESS;
197 }
198
199 /**
200 Start this driver on the device for console input.
201
202 Start this driver on ControllerHandle by opening Simple Text Input Protocol,
203 reading Device Path, and installing Console In Devcice GUID on ControllerHandle.
204
205 If this devcie is not one hot-plug devce, append its device path into the
206 console environment variables ConInDev.
207
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.
212
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.
216
217 **/
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
224 )
225 {
226 EFI_STATUS Status;
227 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
228 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *TextIn;
229
230 //
231 // Get the Device Path Protocol so the environment variables can be updated
232 //
233 Status = gBS->OpenProtocol (
234 ControllerHandle,
235 &gEfiDevicePathProtocolGuid,
236 (VOID **) &DevicePath,
237 This->DriverBindingHandle,
238 ControllerHandle,
239 EFI_OPEN_PROTOCOL_GET_PROTOCOL
240 );
241 if (EFI_ERROR (Status)) {
242 return Status;
243 }
244 //
245 // Open the Simple Text Input Protocol BY_DRIVER
246 //
247 Status = gBS->OpenProtocol (
248 ControllerHandle,
249 &gEfiSimpleTextInProtocolGuid,
250 (VOID **) &TextIn,
251 This->DriverBindingHandle,
252 ControllerHandle,
253 EFI_OPEN_PROTOCOL_BY_DRIVER
254 );
255 if (EFI_ERROR (Status)) {
256 return Status;
257 }
258 //
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.
263 //
264 if (IsHotPlugDevice (DevicePath)) {
265 gBS->InstallMultipleProtocolInterfaces (
266 &ControllerHandle,
267 &gEfiConsoleInDeviceGuid,
268 NULL,
269 NULL
270 );
271 } else {
272 //
273 // If it is not a hot-plug device, append the device path to the
274 // ConInDev environment variable
275 //
276 ConPlatformUpdateDeviceVariable (
277 L"ConInDev",
278 DevicePath,
279 APPEND
280 );
281
282 //
283 // If the device path is successfully added to the ConIn environment variable,
284 // then install EfiConsoleInDeviceGuid onto ControllerHandle
285 //
286 Status = ConPlatformUpdateDeviceVariable (
287 L"ConIn",
288 DevicePath,
289 CHECK
290 );
291
292 if (!EFI_ERROR (Status)) {
293 gBS->InstallMultipleProtocolInterfaces (
294 &ControllerHandle,
295 &gEfiConsoleInDeviceGuid,
296 NULL,
297 NULL
298 );
299 } else {
300 gBS->CloseProtocol (
301 ControllerHandle,
302 &gEfiSimpleTextInProtocolGuid,
303 This->DriverBindingHandle,
304 ControllerHandle
305 );
306 }
307 }
308
309 return EFI_SUCCESS;
310 }
311
312 /**
313 Start this driver on the device for console output and stardard error output.
314
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.
318
319 If this devcie is not one hot-plug devce, append its device path into the
320 console environment variables ConOutDev, ErrOutDev.
321
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.
326
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
330
331 **/
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
338 )
339 {
340 EFI_STATUS Status;
341 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
342 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;
343 BOOLEAN NeedClose;
344
345 NeedClose = TRUE;
346
347 //
348 // Get the Device Path Protocol so the environment variables can be updated
349 //
350 Status = gBS->OpenProtocol (
351 ControllerHandle,
352 &gEfiDevicePathProtocolGuid,
353 (VOID **) &DevicePath,
354 This->DriverBindingHandle,
355 ControllerHandle,
356 EFI_OPEN_PROTOCOL_GET_PROTOCOL
357 );
358 if (EFI_ERROR (Status)) {
359 return Status;
360 }
361 //
362 // Open the Simple Text Output Protocol BY_DRIVER
363 //
364 Status = gBS->OpenProtocol (
365 ControllerHandle,
366 &gEfiSimpleTextOutProtocolGuid,
367 (VOID **) &TextOut,
368 This->DriverBindingHandle,
369 ControllerHandle,
370 EFI_OPEN_PROTOCOL_BY_DRIVER
371 );
372 if (EFI_ERROR (Status)) {
373 return Status;
374 }
375 //
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.
380 //
381 if (IsHotPlugDevice (DevicePath)) {
382 gBS->InstallMultipleProtocolInterfaces (
383 &ControllerHandle,
384 &gEfiConsoleOutDeviceGuid,
385 NULL,
386 NULL
387 );
388 } else {
389 //
390 // If it is not a hot-plug device, first append the device path to the
391 // ConOutDev environment variable
392 //
393 ConPlatformUpdateDeviceVariable (
394 L"ConOutDev",
395 DevicePath,
396 APPEND
397 );
398 //
399 // Then append the device path to the ErrOutDev environment variable
400 //
401 ConPlatformUpdateDeviceVariable (
402 L"ErrOutDev",
403 DevicePath,
404 APPEND
405 );
406
407 //
408 // If the device path is successfully added to the ConOut environment variable,
409 // then install EfiConsoleOutDeviceGuid onto ControllerHandle
410 //
411 Status = ConPlatformUpdateDeviceVariable (
412 L"ConOut",
413 DevicePath,
414 CHECK
415 );
416
417 if (!EFI_ERROR (Status)) {
418 NeedClose = FALSE;
419 Status = gBS->InstallMultipleProtocolInterfaces (
420 &ControllerHandle,
421 &gEfiConsoleOutDeviceGuid,
422 NULL,
423 NULL
424 );
425 }
426 //
427 // If the device path is successfully added to the ErrOut environment variable,
428 // then install EfiStandardErrorDeviceGuid onto ControllerHandle
429 //
430 Status = ConPlatformUpdateDeviceVariable (
431 L"ErrOut",
432 DevicePath,
433 CHECK
434 );
435 if (!EFI_ERROR (Status)) {
436 NeedClose = FALSE;
437 gBS->InstallMultipleProtocolInterfaces (
438 &ControllerHandle,
439 &gEfiStandardErrorDeviceGuid,
440 NULL,
441 NULL
442 );
443 }
444
445 if (NeedClose) {
446 gBS->CloseProtocol (
447 ControllerHandle,
448 &gEfiSimpleTextOutProtocolGuid,
449 This->DriverBindingHandle,
450 ControllerHandle
451 );
452 }
453 }
454
455 return EFI_SUCCESS;
456 }
457
458 /**
459 Stop this driver on ControllerHandle by removing Console In Devcice GUID
460 and closing the Simple Text Input protocol on ControllerHandle.
461
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.
467
468 @retval EFI_SUCCESS This driver is removed ControllerHandle
469 @retval other This driver was not removed from this device
470
471 **/
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
479 )
480 {
481 EFI_STATUS Status;
482 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
483
484 //
485 // Get the Device Path Protocol firstly
486 //
487 Status = gBS->OpenProtocol (
488 ControllerHandle,
489 &gEfiDevicePathProtocolGuid,
490 (VOID **) &DevicePath,
491 This->DriverBindingHandle,
492 ControllerHandle,
493 EFI_OPEN_PROTOCOL_GET_PROTOCOL
494 );
495
496 if (EFI_ERROR (Status)) {
497 return Status;
498 }
499
500 //
501 // If it is not a hot-plug device, first delete it from the ConInDev variable.
502 //
503 if (!IsHotPlugDevice (DevicePath)) {
504 //
505 // Remove DevicePath from ConInDev
506 //
507 ConPlatformUpdateDeviceVariable (
508 L"ConInDev",
509 DevicePath,
510 DELETE
511 );
512 }
513
514 //
515 // Uninstall the Console Device GUIDs from Controller Handle
516 //
517 ConPlatformUnInstallProtocol (
518 This,
519 ControllerHandle,
520 &gEfiConsoleInDeviceGuid
521 );
522
523 //
524 // Close the Simple Text Input Protocol
525 //
526 gBS->CloseProtocol (
527 ControllerHandle,
528 &gEfiSimpleTextInProtocolGuid,
529 This->DriverBindingHandle,
530 ControllerHandle
531 );
532
533 return EFI_SUCCESS;
534 }
535
536
537 /**
538 Stop this driver on ControllerHandle by removing Console Out Devcice GUID
539 and closing the Simple Text Output protocol on ControllerHandle.
540
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.
546
547 @retval EFI_SUCCESS This driver is removed ControllerHandle
548 @retval other This driver was not removed from this device
549
550 **/
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
558 )
559 {
560 EFI_STATUS Status;
561 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
562
563 //
564 // Get the Device Path Protocol firstly
565 //
566 Status = gBS->OpenProtocol (
567 ControllerHandle,
568 &gEfiDevicePathProtocolGuid,
569 (VOID **) &DevicePath,
570 This->DriverBindingHandle,
571 ControllerHandle,
572 EFI_OPEN_PROTOCOL_GET_PROTOCOL
573 );
574
575 if (EFI_ERROR (Status)) {
576 return Status;
577 }
578
579 //
580 // If it is not a hot-plug device, first delete it from the ConOutDev and ErrOutDev variable.
581 //
582 if (!IsHotPlugDevice (DevicePath)) {
583 //
584 // Remove DevicePath from ConOutDev, and ErrOutDev
585 //
586 ConPlatformUpdateDeviceVariable (
587 L"ConOutDev",
588 DevicePath,
589 DELETE
590 );
591 ConPlatformUpdateDeviceVariable (
592 L"ErrOutDev",
593 DevicePath,
594 DELETE
595 );
596 }
597
598 //
599 // Uninstall the Console Device GUIDs from Controller Handle
600 //
601 ConPlatformUnInstallProtocol (
602 This,
603 ControllerHandle,
604 &gEfiConsoleOutDeviceGuid
605 );
606
607 ConPlatformUnInstallProtocol (
608 This,
609 ControllerHandle,
610 &gEfiStandardErrorDeviceGuid
611 );
612
613 //
614 // Close the Simple Text Output Protocol
615 //
616 gBS->CloseProtocol (
617 ControllerHandle,
618 &gEfiSimpleTextOutProtocolGuid,
619 This->DriverBindingHandle,
620 ControllerHandle
621 );
622
623 return EFI_SUCCESS;
624 }
625
626
627 /**
628 Uninstall the specified protocol.
629
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.
633
634 **/
635 VOID
636 ConPlatformUnInstallProtocol (
637 IN EFI_DRIVER_BINDING_PROTOCOL *This,
638 IN EFI_HANDLE Handle,
639 IN EFI_GUID *ProtocolGuid
640 )
641 {
642 EFI_STATUS Status;
643
644 Status = gBS->OpenProtocol (
645 Handle,
646 ProtocolGuid,
647 NULL,
648 This->DriverBindingHandle,
649 Handle,
650 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
651 );
652
653 if (!EFI_ERROR (Status)) {
654 gBS->UninstallMultipleProtocolInterfaces (
655 Handle,
656 ProtocolGuid,
657 NULL,
658 NULL
659 );
660 }
661
662 return ;
663 }
664
665 /**
666 Get the necessary size of buffer and read the variable.
667
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.
671
672 @param Name String part of EFI variable name
673
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.
677
678 **/
679 VOID *
680 ConPlatformGetVariable (
681 IN CHAR16 *Name
682 )
683 {
684 EFI_STATUS Status;
685 VOID *Buffer;
686 UINTN BufferSize;
687
688 BufferSize = 0;
689 Buffer = NULL;
690
691 //
692 // Test to see if the variable exists. If it doesn't, return NULL.
693 //
694 Status = gRT->GetVariable (
695 Name,
696 &gEfiGlobalVariableGuid,
697 NULL,
698 &BufferSize,
699 Buffer
700 );
701
702 if (Status == EFI_BUFFER_TOO_SMALL) {
703 //
704 // Allocate the buffer to return
705 //
706 Buffer = AllocatePool (BufferSize);
707 if (Buffer == NULL) {
708 return NULL;
709 }
710 //
711 // Read variable into the allocated buffer.
712 //
713 Status = gRT->GetVariable (
714 Name,
715 &gEfiGlobalVariableGuid,
716 NULL,
717 &BufferSize,
718 Buffer
719 );
720 if (EFI_ERROR (Status)) {
721 FreePool (Buffer);
722 //
723 // To make sure Buffer is NULL if any error occurs.
724 //
725 Buffer = NULL;
726 }
727 }
728
729 return Buffer;
730 }
731
732 /**
733 Function compares a device path data structure to that of all the nodes of a
734 second device path instance.
735
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.
744
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.
750
751 **/
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
758 )
759 {
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;
765
766 //
767 // The passed in DevicePath should not be NULL
768 //
769 if ((Multi == NULL) || (Single == NULL)) {
770 return EFI_INVALID_PARAMETER;
771 }
772
773 //
774 // If performing Delete operation, the NewDevicePath must not be NULL.
775 //
776 if (Delete) {
777 if (NewDevicePath == NULL) {
778 return EFI_INVALID_PARAMETER;
779 }
780 }
781
782 TempDevicePath1 = NULL;
783
784 DevicePath = Multi;
785 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
786
787 //
788 // Search for the match of 'Single' in 'Multi'
789 //
790 while (DevicePathInst != NULL) {
791 if (CompareMem (Single, DevicePathInst, Size) == 0) {
792 if (!Delete) {
793 //
794 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.
795 //
796 FreePool (DevicePathInst);
797 return EFI_SUCCESS;
798 }
799 } else {
800 if (Delete) {
801 //
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.
804 //
805 TempDevicePath2 = AppendDevicePathInstance (
806 TempDevicePath1,
807 DevicePathInst
808 );
809 if (TempDevicePath1 != NULL) {
810 FreePool (TempDevicePath1);
811 }
812 TempDevicePath1 = TempDevicePath2;
813 }
814 }
815
816 FreePool (DevicePathInst);
817 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
818 }
819
820 if (Delete) {
821 //
822 // Return the new device path data structure with specified node deleted.
823 //
824 *NewDevicePath = TempDevicePath1;
825 return EFI_SUCCESS;
826 }
827
828 return EFI_NOT_FOUND;
829 }
830
831 /**
832 Update console environment variables.
833
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.
838
839 @retval EFI_SUCCESS Variable operates successfully.
840 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
841 @retval other Variable updating failed.
842
843 **/
844 EFI_STATUS
845 ConPlatformUpdateDeviceVariable (
846 IN CHAR16 *VariableName,
847 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
848 IN CONPLATFORM_VAR_OPERATION Operation
849 )
850 {
851 EFI_STATUS Status;
852 EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;
853 EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;
854
855 VariableDevicePath = NULL;
856 NewVariableDevicePath = NULL;
857
858 //
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.
862 //
863 VariableDevicePath = ConPlatformGetVariable (VariableName);
864
865 if (Operation != DELETE) {
866 //
867 // Match specified DevicePath in Console Variable.
868 //
869 Status = ConPlatformMatchDevicePaths (
870 VariableDevicePath,
871 DevicePath,
872 NULL,
873 FALSE
874 );
875
876 if ((Operation == CHECK) || (!EFI_ERROR (Status))) {
877 //
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.
881 //
882 if (VariableDevicePath != NULL) {
883 FreePool (VariableDevicePath);
884 }
885
886 return Status;
887 }
888 //
889 // We reach here to append a device path that does not exist in variable.
890 //
891 Status = EFI_SUCCESS;
892 NewVariableDevicePath = AppendDevicePathInstance (
893 VariableDevicePath,
894 DevicePath
895 );
896 if (NewVariableDevicePath == NULL) {
897 Status = EFI_OUT_OF_RESOURCES;
898 }
899
900 } else {
901 //
902 // We reach here to remove DevicePath from the environment variable that
903 // is a multi-instance device path.
904 //
905 Status = ConPlatformMatchDevicePaths (
906 VariableDevicePath,
907 DevicePath,
908 &NewVariableDevicePath,
909 TRUE
910 );
911 }
912
913 if (VariableDevicePath != NULL) {
914 FreePool (VariableDevicePath);
915 }
916
917 if (EFI_ERROR (Status)) {
918 return Status;
919 }
920
921 if (NewVariableDevicePath != NULL) {
922 //
923 // Update Console Environment Variable.
924 //
925 Status = gRT->SetVariable (
926 VariableName,
927 &gEfiGlobalVariableGuid,
928 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
929 GetDevicePathSize (NewVariableDevicePath),
930 NewVariableDevicePath
931 );
932
933 FreePool (NewVariableDevicePath);
934 }
935
936 return Status;
937 }
938
939 /**
940 Check if the device supports hot-plug through its device path.
941
942 This function could be updated to check more types of Hot Plug devices.
943 Currently, it checks USB and PCCard device.
944
945 @param DevicePath Pointer to device's device path.
946
947 @retval TRUE The devcie is a hot-plug device
948 @retval FALSE The devcie is not a hot-plug device.
949
950 **/
951 BOOLEAN
952 IsHotPlugDevice (
953 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
954 )
955 {
956 //
957 // Check device whether is hot plug device or not throught Device Path
958 //
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)) {
963 //
964 // If Device is USB device
965 //
966 return TRUE;
967 }
968 if ((DevicePathType (DevicePath) == HARDWARE_DEVICE_PATH) &&
969 (DevicePathSubType (DevicePath) == HW_PCCARD_DP)) {
970 //
971 // If Device is PCCard
972 //
973 return TRUE;
974 }
975
976 return FALSE;
977 }