]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatform.c
e40e2cc804ea6bcea4e6e34679a9b7ed427c0e10
[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 - 2010, 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 BOOLEAN IsInConInVariable;
230
231 //
232 // Get the Device Path Protocol so the environment variables can be updated
233 //
234 Status = gBS->OpenProtocol (
235 ControllerHandle,
236 &gEfiDevicePathProtocolGuid,
237 (VOID **) &DevicePath,
238 This->DriverBindingHandle,
239 ControllerHandle,
240 EFI_OPEN_PROTOCOL_GET_PROTOCOL
241 );
242 if (EFI_ERROR (Status)) {
243 return Status;
244 }
245 //
246 // Open the Simple Text Input Protocol BY_DRIVER
247 //
248 Status = gBS->OpenProtocol (
249 ControllerHandle,
250 &gEfiSimpleTextInProtocolGuid,
251 (VOID **) &TextIn,
252 This->DriverBindingHandle,
253 ControllerHandle,
254 EFI_OPEN_PROTOCOL_BY_DRIVER
255 );
256 if (EFI_ERROR (Status)) {
257 return Status;
258 }
259 //
260 // Check if the device path is in ConIn Variable
261 //
262 IsInConInVariable = FALSE;
263 Status = ConPlatformUpdateDeviceVariable (
264 L"ConIn",
265 DevicePath,
266 Check
267 );
268 if (!EFI_ERROR (Status)) {
269 IsInConInVariable = TRUE;
270 }
271
272 //
273 // Check the device path, if it is a hot plug device,
274 // do not put the device path into ConInDev, and install
275 // gEfiConsoleInDeviceGuid to the device handle directly.
276 // The policy is, make hot plug device plug in and play immediately.
277 //
278 if (IsHotPlugDevice (DevicePath)) {
279 gBS->InstallMultipleProtocolInterfaces (
280 &ControllerHandle,
281 &gEfiConsoleInDeviceGuid,
282 NULL,
283 NULL
284 );
285 //
286 // Append the device path to ConInDev only if it is in ConIn variable.
287 //
288 if (IsInConInVariable) {
289 ConPlatformUpdateDeviceVariable (
290 L"ConInDev",
291 DevicePath,
292 Append
293 );
294 }
295 } else {
296 //
297 // If it is not a hot-plug device, append the device path to the
298 // ConInDev environment variable
299 //
300 ConPlatformUpdateDeviceVariable (
301 L"ConInDev",
302 DevicePath,
303 Append
304 );
305
306 //
307 // If the device path is an instance in the ConIn environment variable,
308 // then install EfiConsoleInDeviceGuid onto ControllerHandle
309 //
310 if (IsInConInVariable) {
311 gBS->InstallMultipleProtocolInterfaces (
312 &ControllerHandle,
313 &gEfiConsoleInDeviceGuid,
314 NULL,
315 NULL
316 );
317 } else {
318 gBS->CloseProtocol (
319 ControllerHandle,
320 &gEfiSimpleTextInProtocolGuid,
321 This->DriverBindingHandle,
322 ControllerHandle
323 );
324 }
325 }
326
327 return EFI_SUCCESS;
328 }
329
330 /**
331 Start this driver on the device for console output and stardard error output.
332
333 Start this driver on ControllerHandle by opening Simple Text Output Protocol,
334 reading Device Path, and installing Console Out Devcic GUID, Standard Error
335 Device GUID on ControllerHandle.
336
337 If this devcie is not one hot-plug devce, append its device path into the
338 console environment variables ConOutDev, ErrOutDev.
339
340 @param This Protocol instance pointer.
341 @param ControllerHandle Handle of device to bind driver to
342 @param RemainingDevicePath Optional parameter use to pick a specific child
343 device to start.
344
345 @retval EFI_SUCCESS This driver is added to ControllerHandle
346 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
347 @retval other This driver does not support this device
348
349 **/
350 EFI_STATUS
351 EFIAPI
352 ConPlatformTextOutDriverBindingStart (
353 IN EFI_DRIVER_BINDING_PROTOCOL *This,
354 IN EFI_HANDLE ControllerHandle,
355 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
356 )
357 {
358 EFI_STATUS Status;
359 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
360 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;
361 BOOLEAN NeedClose;
362 BOOLEAN IsInConOutVariable;
363 BOOLEAN IsInErrOutVariable;
364
365 NeedClose = TRUE;
366
367 //
368 // Get the Device Path Protocol so the environment variables can be updated
369 //
370 Status = gBS->OpenProtocol (
371 ControllerHandle,
372 &gEfiDevicePathProtocolGuid,
373 (VOID **) &DevicePath,
374 This->DriverBindingHandle,
375 ControllerHandle,
376 EFI_OPEN_PROTOCOL_GET_PROTOCOL
377 );
378 if (EFI_ERROR (Status)) {
379 return Status;
380 }
381 //
382 // Open the Simple Text Output Protocol BY_DRIVER
383 //
384 Status = gBS->OpenProtocol (
385 ControllerHandle,
386 &gEfiSimpleTextOutProtocolGuid,
387 (VOID **) &TextOut,
388 This->DriverBindingHandle,
389 ControllerHandle,
390 EFI_OPEN_PROTOCOL_BY_DRIVER
391 );
392 if (EFI_ERROR (Status)) {
393 return Status;
394 }
395 //
396 // Check if the device path is in ConOut & ErrOut Variable
397 //
398 IsInConOutVariable = FALSE;
399 Status = ConPlatformUpdateDeviceVariable (
400 L"ConOut",
401 DevicePath,
402 Check
403 );
404 if (!EFI_ERROR (Status)) {
405 IsInConOutVariable = TRUE;
406 }
407
408 IsInErrOutVariable = FALSE;
409 Status = ConPlatformUpdateDeviceVariable (
410 L"ErrOut",
411 DevicePath,
412 Check
413 );
414 if (!EFI_ERROR (Status)) {
415 IsInErrOutVariable = TRUE;
416 }
417
418 //
419 // Check the device path, if it is a hot plug device,
420 // do not put the device path into ConOutDev and ErrOutDev,
421 // and install gEfiConsoleOutDeviceGuid to the device handle directly.
422 // The policy is, make hot plug device plug in and play immediately.
423 //
424 if (IsHotPlugDevice (DevicePath)) {
425 gBS->InstallMultipleProtocolInterfaces (
426 &ControllerHandle,
427 &gEfiConsoleOutDeviceGuid,
428 NULL,
429 NULL
430 );
431 //
432 // Append the device path to ConOutDev only if it is in ConOut variable.
433 //
434 if (IsInConOutVariable) {
435 ConPlatformUpdateDeviceVariable (
436 L"ConOutDev",
437 DevicePath,
438 Append
439 );
440 }
441 //
442 // Append the device path to ErrOutDev only if it is in ErrOut variable.
443 //
444 if (IsInErrOutVariable) {
445 ConPlatformUpdateDeviceVariable (
446 L"ErrOutDev",
447 DevicePath,
448 Append
449 );
450 }
451 } else {
452 //
453 // If it is not a hot-plug device, first append the device path to the
454 // ConOutDev environment variable
455 //
456 ConPlatformUpdateDeviceVariable (
457 L"ConOutDev",
458 DevicePath,
459 Append
460 );
461 //
462 // Then append the device path to the ErrOutDev environment variable
463 //
464 ConPlatformUpdateDeviceVariable (
465 L"ErrOutDev",
466 DevicePath,
467 Append
468 );
469
470 //
471 // If the device path is an instance in the ConOut environment variable,
472 // then install EfiConsoleOutDeviceGuid onto ControllerHandle
473 //
474 if (IsInConOutVariable) {
475 NeedClose = FALSE;
476 Status = gBS->InstallMultipleProtocolInterfaces (
477 &ControllerHandle,
478 &gEfiConsoleOutDeviceGuid,
479 NULL,
480 NULL
481 );
482 }
483 //
484 // If the device path is an instance in the ErrOut environment variable,
485 // then install EfiStandardErrorDeviceGuid onto ControllerHandle
486 //
487 if (IsInErrOutVariable) {
488 NeedClose = FALSE;
489 gBS->InstallMultipleProtocolInterfaces (
490 &ControllerHandle,
491 &gEfiStandardErrorDeviceGuid,
492 NULL,
493 NULL
494 );
495 }
496
497 if (NeedClose) {
498 gBS->CloseProtocol (
499 ControllerHandle,
500 &gEfiSimpleTextOutProtocolGuid,
501 This->DriverBindingHandle,
502 ControllerHandle
503 );
504 }
505 }
506
507 return EFI_SUCCESS;
508 }
509
510 /**
511 Stop this driver on ControllerHandle by removing Console In Devcice GUID
512 and closing the Simple Text Input protocol on ControllerHandle.
513
514 @param This Protocol instance pointer.
515 @param ControllerHandle Handle of device to stop driver on
516 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
517 children is zero stop the entire bus driver.
518 @param ChildHandleBuffer List of Child Handles to Stop.
519
520 @retval EFI_SUCCESS This driver is removed ControllerHandle
521 @retval other This driver was not removed from this device
522
523 **/
524 EFI_STATUS
525 EFIAPI
526 ConPlatformTextInDriverBindingStop (
527 IN EFI_DRIVER_BINDING_PROTOCOL *This,
528 IN EFI_HANDLE ControllerHandle,
529 IN UINTN NumberOfChildren,
530 IN EFI_HANDLE *ChildHandleBuffer
531 )
532 {
533 EFI_STATUS Status;
534 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
535
536 //
537 // Get the Device Path Protocol firstly
538 //
539 Status = gBS->OpenProtocol (
540 ControllerHandle,
541 &gEfiDevicePathProtocolGuid,
542 (VOID **) &DevicePath,
543 This->DriverBindingHandle,
544 ControllerHandle,
545 EFI_OPEN_PROTOCOL_GET_PROTOCOL
546 );
547 //
548 // If there is device path on ControllerHandle
549 //
550 if (!EFI_ERROR (Status)) {
551 //
552 // Remove DevicePath from ConInDev if exists.
553 //
554 ConPlatformUpdateDeviceVariable (
555 L"ConInDev",
556 DevicePath,
557 Delete
558 );
559 }
560
561 //
562 // Uninstall the Console Device GUIDs from Controller Handle
563 //
564 ConPlatformUnInstallProtocol (
565 This,
566 ControllerHandle,
567 &gEfiConsoleInDeviceGuid
568 );
569
570 //
571 // Close the Simple Text Input Protocol
572 //
573 gBS->CloseProtocol (
574 ControllerHandle,
575 &gEfiSimpleTextInProtocolGuid,
576 This->DriverBindingHandle,
577 ControllerHandle
578 );
579
580 return EFI_SUCCESS;
581 }
582
583
584 /**
585 Stop this driver on ControllerHandle by removing Console Out Devcice GUID
586 and closing the Simple Text Output protocol on ControllerHandle.
587
588 @param This Protocol instance pointer.
589 @param ControllerHandle Handle of device to stop driver on
590 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
591 children is zero stop the entire bus driver.
592 @param ChildHandleBuffer List of Child Handles to Stop.
593
594 @retval EFI_SUCCESS This driver is removed ControllerHandle
595 @retval other This driver was not removed from this device
596
597 **/
598 EFI_STATUS
599 EFIAPI
600 ConPlatformTextOutDriverBindingStop (
601 IN EFI_DRIVER_BINDING_PROTOCOL *This,
602 IN EFI_HANDLE ControllerHandle,
603 IN UINTN NumberOfChildren,
604 IN EFI_HANDLE *ChildHandleBuffer
605 )
606 {
607 EFI_STATUS Status;
608 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
609
610 //
611 // Get the Device Path Protocol firstly
612 //
613 Status = gBS->OpenProtocol (
614 ControllerHandle,
615 &gEfiDevicePathProtocolGuid,
616 (VOID **) &DevicePath,
617 This->DriverBindingHandle,
618 ControllerHandle,
619 EFI_OPEN_PROTOCOL_GET_PROTOCOL
620 );
621 if (!EFI_ERROR (Status)) {
622 //
623 // Remove DevicePath from ConOutDev and ErrOutDev if exists.
624 //
625 ConPlatformUpdateDeviceVariable (
626 L"ConOutDev",
627 DevicePath,
628 Delete
629 );
630 ConPlatformUpdateDeviceVariable (
631 L"ErrOutDev",
632 DevicePath,
633 Delete
634 );
635 }
636
637 //
638 // Uninstall the Console Device GUIDs from Controller Handle
639 //
640 ConPlatformUnInstallProtocol (
641 This,
642 ControllerHandle,
643 &gEfiConsoleOutDeviceGuid
644 );
645
646 ConPlatformUnInstallProtocol (
647 This,
648 ControllerHandle,
649 &gEfiStandardErrorDeviceGuid
650 );
651
652 //
653 // Close the Simple Text Output Protocol
654 //
655 gBS->CloseProtocol (
656 ControllerHandle,
657 &gEfiSimpleTextOutProtocolGuid,
658 This->DriverBindingHandle,
659 ControllerHandle
660 );
661
662 return EFI_SUCCESS;
663 }
664
665
666 /**
667 Uninstall the specified protocol.
668
669 @param This Protocol instance pointer.
670 @param Handle Handle of device to uninstall protocol on.
671 @param ProtocolGuid The specified protocol need to be uninstalled.
672
673 **/
674 VOID
675 ConPlatformUnInstallProtocol (
676 IN EFI_DRIVER_BINDING_PROTOCOL *This,
677 IN EFI_HANDLE Handle,
678 IN EFI_GUID *ProtocolGuid
679 )
680 {
681 EFI_STATUS Status;
682
683 Status = gBS->OpenProtocol (
684 Handle,
685 ProtocolGuid,
686 NULL,
687 This->DriverBindingHandle,
688 Handle,
689 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
690 );
691
692 if (!EFI_ERROR (Status)) {
693 gBS->UninstallMultipleProtocolInterfaces (
694 Handle,
695 ProtocolGuid,
696 NULL,
697 NULL
698 );
699 }
700
701 return ;
702 }
703
704 /**
705 Get the necessary size of buffer and read the variable.
706
707 First get the necessary size of buffer. Then read the
708 EFI variable (Name) and return a dynamically allocated
709 buffer. On failure return NULL.
710
711 @param Name String part of EFI variable name
712
713 @return Dynamically allocated memory that contains a copy of the EFI variable.
714 Caller is repsoncible freeing the buffer. Return NULL means Variable
715 was not read.
716
717 **/
718 VOID *
719 ConPlatformGetVariable (
720 IN CHAR16 *Name
721 )
722 {
723 EFI_STATUS Status;
724 VOID *Buffer;
725 UINTN BufferSize;
726
727 BufferSize = 0;
728 Buffer = NULL;
729
730 //
731 // Test to see if the variable exists. If it doesn't, return NULL.
732 //
733 Status = gRT->GetVariable (
734 Name,
735 &gEfiGlobalVariableGuid,
736 NULL,
737 &BufferSize,
738 Buffer
739 );
740
741 if (Status == EFI_BUFFER_TOO_SMALL) {
742 //
743 // Allocate the buffer to return
744 //
745 Buffer = AllocatePool (BufferSize);
746 if (Buffer == NULL) {
747 return NULL;
748 }
749 //
750 // Read variable into the allocated buffer.
751 //
752 Status = gRT->GetVariable (
753 Name,
754 &gEfiGlobalVariableGuid,
755 NULL,
756 &BufferSize,
757 Buffer
758 );
759 if (EFI_ERROR (Status)) {
760 FreePool (Buffer);
761 //
762 // To make sure Buffer is NULL if any error occurs.
763 //
764 Buffer = NULL;
765 }
766 }
767
768 return Buffer;
769 }
770
771 /**
772 Function compares a device path data structure to that of all the nodes of a
773 second device path instance.
774
775 @param Multi A pointer to a multi-instance device path data structure.
776 @param Single A pointer to a single-instance device path data structure.
777 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
778 points to the remaining device path data structure.
779 (remaining device path = Multi - Single.)
780 @param Delete If TRUE, means removing Single from Multi.
781 If FALSE, the routine just check whether Single matches
782 with any instance in Multi.
783
784 @retval EFI_SUCCESS If the Single is contained within Multi.
785 @retval EFI_NOT_FOUND If the Single is not contained within Multi.
786 @retval EFI_INVALID_PARAMETER Multi is NULL.
787 @retval EFI_INVALID_PARAMETER Single is NULL.
788 @retval EFI_INVALID_PARAMETER NewDevicePath is NULL when Delete is TRUE.
789
790 **/
791 EFI_STATUS
792 ConPlatformMatchDevicePaths (
793 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
794 IN EFI_DEVICE_PATH_PROTOCOL *Single,
795 OUT EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,
796 IN BOOLEAN Delete
797 )
798 {
799 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
800 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath1;
801 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath2;
802 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;
803 UINTN Size;
804
805 //
806 // The passed in DevicePath should not be NULL
807 //
808 if ((Multi == NULL) || (Single == NULL)) {
809 return EFI_INVALID_PARAMETER;
810 }
811
812 //
813 // If performing Delete operation, the NewDevicePath must not be NULL.
814 //
815 if (Delete) {
816 if (NewDevicePath == NULL) {
817 return EFI_INVALID_PARAMETER;
818 }
819 }
820
821 TempDevicePath1 = NULL;
822
823 DevicePath = Multi;
824 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
825
826 //
827 // Search for the match of 'Single' in 'Multi'
828 //
829 while (DevicePathInst != NULL) {
830 if (CompareMem (Single, DevicePathInst, Size) == 0) {
831 if (!Delete) {
832 //
833 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.
834 //
835 FreePool (DevicePathInst);
836 return EFI_SUCCESS;
837 }
838 } else {
839 if (Delete) {
840 //
841 // If the node of Multi does not match Single, then added it back to the result.
842 // That is, the node matching Single will be dropped and deleted from result.
843 //
844 TempDevicePath2 = AppendDevicePathInstance (
845 TempDevicePath1,
846 DevicePathInst
847 );
848 if (TempDevicePath1 != NULL) {
849 FreePool (TempDevicePath1);
850 }
851 TempDevicePath1 = TempDevicePath2;
852 }
853 }
854
855 FreePool (DevicePathInst);
856 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
857 }
858
859 if (Delete) {
860 //
861 // Return the new device path data structure with specified node deleted.
862 //
863 *NewDevicePath = TempDevicePath1;
864 return EFI_SUCCESS;
865 }
866
867 return EFI_NOT_FOUND;
868 }
869
870 /**
871 Update console environment variables.
872
873 @param VariableName Console environment variables, ConOutDev, ConInDev
874 ErrOutDev, ConIn ,ConOut or ErrOut.
875 @param DevicePath Console devcie's device path.
876 @param Operation Variable operations, including APPEND, CHECK and DELETE.
877
878 @retval EFI_SUCCESS Variable operates successfully.
879 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
880 @retval other Variable updating failed.
881
882 **/
883 EFI_STATUS
884 ConPlatformUpdateDeviceVariable (
885 IN CHAR16 *VariableName,
886 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
887 IN CONPLATFORM_VAR_OPERATION Operation
888 )
889 {
890 EFI_STATUS Status;
891 EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;
892 EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;
893
894 VariableDevicePath = NULL;
895 NewVariableDevicePath = NULL;
896
897 //
898 // Get Variable according to variable name.
899 // The memory for Variable is allocated within ConPlatformGetVarible(),
900 // it is the caller's responsibility to free the memory before return.
901 //
902 VariableDevicePath = ConPlatformGetVariable (VariableName);
903
904 if (Operation != Delete) {
905 //
906 // Match specified DevicePath in Console Variable.
907 //
908 Status = ConPlatformMatchDevicePaths (
909 VariableDevicePath,
910 DevicePath,
911 NULL,
912 FALSE
913 );
914
915 if ((Operation == Check) || (!EFI_ERROR (Status))) {
916 //
917 // Branch here includes 2 cases:
918 // 1. Operation is CHECK, simply return Status.
919 // 2. Operation is APPEND, and device path already exists in variable, also return.
920 //
921 if (VariableDevicePath != NULL) {
922 FreePool (VariableDevicePath);
923 }
924
925 return Status;
926 }
927 //
928 // We reach here to append a device path that does not exist in variable.
929 //
930 Status = EFI_SUCCESS;
931 NewVariableDevicePath = AppendDevicePathInstance (
932 VariableDevicePath,
933 DevicePath
934 );
935 if (NewVariableDevicePath == NULL) {
936 Status = EFI_OUT_OF_RESOURCES;
937 }
938
939 } else {
940 //
941 // We reach here to remove DevicePath from the environment variable that
942 // is a multi-instance device path.
943 //
944 Status = ConPlatformMatchDevicePaths (
945 VariableDevicePath,
946 DevicePath,
947 &NewVariableDevicePath,
948 TRUE
949 );
950 }
951
952 if (VariableDevicePath != NULL) {
953 FreePool (VariableDevicePath);
954 }
955
956 if (EFI_ERROR (Status)) {
957 return Status;
958 }
959
960 if (NewVariableDevicePath != NULL) {
961 //
962 // Update Console Environment Variable.
963 //
964 Status = gRT->SetVariable (
965 VariableName,
966 &gEfiGlobalVariableGuid,
967 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
968 GetDevicePathSize (NewVariableDevicePath),
969 NewVariableDevicePath
970 );
971
972 FreePool (NewVariableDevicePath);
973 }
974
975 return Status;
976 }
977
978 /**
979 Check if the device supports hot-plug through its device path.
980
981 This function could be updated to check more types of Hot Plug devices.
982 Currently, it checks USB and PCCard device.
983
984 @param DevicePath Pointer to device's device path.
985
986 @retval TRUE The devcie is a hot-plug device
987 @retval FALSE The devcie is not a hot-plug device.
988
989 **/
990 BOOLEAN
991 IsHotPlugDevice (
992 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
993 )
994 {
995 EFI_DEVICE_PATH_PROTOCOL *CheckDevicePath;
996
997 CheckDevicePath = DevicePath;
998 while (!IsDevicePathEnd (CheckDevicePath)) {
999 //
1000 // Check device whether is hot plug device or not throught Device Path
1001 //
1002 if ((DevicePathType (CheckDevicePath) == MESSAGING_DEVICE_PATH) &&
1003 (DevicePathSubType (CheckDevicePath) == MSG_USB_DP ||
1004 DevicePathSubType (CheckDevicePath) == MSG_USB_CLASS_DP ||
1005 DevicePathSubType (CheckDevicePath) == MSG_USB_WWID_DP)) {
1006 //
1007 // If Device is USB device
1008 //
1009 return TRUE;
1010 }
1011 if ((DevicePathType (CheckDevicePath) == HARDWARE_DEVICE_PATH) &&
1012 (DevicePathSubType (CheckDevicePath) == HW_PCCARD_DP)) {
1013 //
1014 // If Device is PCCard
1015 //
1016 return TRUE;
1017 }
1018
1019 CheckDevicePath = NextDevicePathNode (CheckDevicePath);
1020 }
1021
1022 return FALSE;
1023 }
1024