]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatform.c
Use #include "XXX.h" for module internal header files.
[mirror_edk2.git] / MdeModulePkg / Universal / Console / ConPlatformDxe / ConPlatform.c
1 /** @file
2 Console Platfrom 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 The user Entry Point for module ConPlatform. The user code starts with this function.
39
40 @param ImageHandle The firmware allocated handle for the EFI image.
41 @param SystemTable A pointer to the EFI System Table.
42
43 @retval EFI_SUCCESS The entry point is executed successfully.
44 @retval other Some error occurs when executing this entry point.
45
46 **/
47 EFI_STATUS
48 EFIAPI
49 InitializeConPlatform(
50 IN EFI_HANDLE ImageHandle,
51 IN EFI_SYSTEM_TABLE *SystemTable
52 )
53 {
54 EFI_STATUS Status;
55
56 //
57 // Install driver model protocol(s).
58 //
59 Status = EfiLibInstallDriverBindingComponentName2 (
60 ImageHandle,
61 SystemTable,
62 &gConPlatformTextInDriverBinding,
63 ImageHandle,
64 &gConPlatformComponentName,
65 &gConPlatformComponentName2
66 );
67 ASSERT_EFI_ERROR (Status);
68
69 Status = EfiLibInstallDriverBindingComponentName2 (
70 ImageHandle,
71 SystemTable,
72 &gConPlatformTextOutDriverBinding,
73 NULL,
74 &gConPlatformComponentName,
75 &gConPlatformComponentName2
76 );
77 ASSERT_EFI_ERROR (Status);
78
79
80 return Status;
81 }
82
83
84 /**
85 Test to see if EFI Text In Protocol could be supported on the ControllerHandle.
86
87 @param This Protocol instance pointer.
88 @param ControllerHandle Handle of device to test.
89 @param RemainingDevicePath Optional parameter use to pick a specific child
90 device to start.
91
92 @retval EFI_SUCCESS This driver supports this device.
93 @retval other This driver does not support this device.
94
95 **/
96 EFI_STATUS
97 EFIAPI
98 ConPlatformTextInDriverBindingSupported (
99 IN EFI_DRIVER_BINDING_PROTOCOL *This,
100 IN EFI_HANDLE ControllerHandle,
101 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
102 )
103 {
104 return ConPlatformDriverBindingSupported (
105 This,
106 ControllerHandle,
107 RemainingDevicePath,
108 &gEfiSimpleTextInProtocolGuid
109 );
110 }
111
112
113 /**
114 Test to see if EFI Text Out Protocol could be supported on the ControllerHandle.
115
116 @param This Protocol instance pointer.
117 @param ControllerHandle Handle of device to test.
118 @param RemainingDevicePath Optional parameter use to pick a specific child
119 device to start.
120
121 @retval EFI_SUCCESS This driver supports this device.
122 @retval other This driver does not support this device.
123
124 **/
125 EFI_STATUS
126 EFIAPI
127 ConPlatformTextOutDriverBindingSupported (
128 IN EFI_DRIVER_BINDING_PROTOCOL *This,
129 IN EFI_HANDLE ControllerHandle,
130 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
131 )
132 {
133 return ConPlatformDriverBindingSupported (
134 This,
135 ControllerHandle,
136 RemainingDevicePath,
137 &gEfiSimpleTextOutProtocolGuid
138 );
139 }
140
141
142 /**
143 Test to see if the specified Protocol could be supported on the ControllerHandle.
144
145 @param This Protocol instance pointer.
146 @param ControllerHandle Handle of device to test.
147 @param RemainingDevicePath Optional parameter use to pick a specific child
148 device to start.
149 @param ProtocolGuid The specfic protocol.
150
151 @retval EFI_SUCCESS This driver supports this device.
152 @retval other This driver does not support this device.
153
154 **/
155 EFI_STATUS
156 ConPlatformDriverBindingSupported (
157 IN EFI_DRIVER_BINDING_PROTOCOL *This,
158 IN EFI_HANDLE ControllerHandle,
159 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath,
160 IN EFI_GUID *ProtocolGuid
161 )
162 {
163 EFI_STATUS Status;
164 VOID *Interface;
165
166 //
167 // Test to see if this is a physical device by checking if
168 // it has a Device Path Protocol.
169 //
170 Status = gBS->OpenProtocol (
171 ControllerHandle,
172 &gEfiDevicePathProtocolGuid,
173 NULL,
174 This->DriverBindingHandle,
175 ControllerHandle,
176 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
177 );
178 if (EFI_ERROR (Status)) {
179 return Status;
180 }
181 //
182 // Test to see if this device supports the specified Protocol.
183 //
184 Status = gBS->OpenProtocol (
185 ControllerHandle,
186 ProtocolGuid,
187 (VOID **) &Interface,
188 This->DriverBindingHandle,
189 ControllerHandle,
190 EFI_OPEN_PROTOCOL_BY_DRIVER
191 );
192 if (EFI_ERROR (Status)) {
193 return Status;
194 }
195
196 gBS->CloseProtocol (
197 ControllerHandle,
198 ProtocolGuid,
199 This->DriverBindingHandle,
200 ControllerHandle
201 );
202
203 return EFI_SUCCESS;
204 }
205
206 /**
207 Start this driver on ControllerHandle by opening Simple Text In protocol,
208 reading Device Path, and installing Console In Devcice GUID on ControllerHandle.
209
210 If this devcie is not one hot-plug devce, append its device path into the
211 console environment variables ConInDev.
212
213 @param This Protocol instance pointer.
214 @param ControllerHandle Handle of device to bind driver to
215 @param RemainingDevicePath Optional parameter use to pick a specific child
216 device to start.
217
218 @retval EFI_SUCCESS This driver is added to ControllerHandle
219 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
220 @retval other This driver does not support this device.
221
222 **/
223 EFI_STATUS
224 EFIAPI
225 ConPlatformTextInDriverBindingStart (
226 IN EFI_DRIVER_BINDING_PROTOCOL *This,
227 IN EFI_HANDLE ControllerHandle,
228 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
229 )
230 {
231 EFI_STATUS Status;
232 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
233 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *TextIn;
234
235 //
236 // Get the Device Path Protocol so the environment variables can be updated
237 //
238 Status = gBS->OpenProtocol (
239 ControllerHandle,
240 &gEfiDevicePathProtocolGuid,
241 (VOID **) &DevicePath,
242 This->DriverBindingHandle,
243 ControllerHandle,
244 EFI_OPEN_PROTOCOL_GET_PROTOCOL
245 );
246 if (EFI_ERROR (Status)) {
247 return Status;
248 }
249 //
250 // Open the Simple Input Protocol BY_DRIVER
251 //
252 Status = gBS->OpenProtocol (
253 ControllerHandle,
254 &gEfiSimpleTextInProtocolGuid,
255 (VOID **) &TextIn,
256 This->DriverBindingHandle,
257 ControllerHandle,
258 EFI_OPEN_PROTOCOL_BY_DRIVER
259 );
260 if (EFI_ERROR (Status)) {
261 return Status;
262 }
263 //
264 // Check the device handle, if it is a hot plug device,
265 // do not put the device path into ConInDev, and install
266 // gEfiConsoleInDeviceGuid to the device handle directly.
267 // The policy is, make hot plug device plug in and play immediately.
268 //
269 if (IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
270 gBS->InstallMultipleProtocolInterfaces (
271 &ControllerHandle,
272 &gEfiConsoleInDeviceGuid,
273 NULL,
274 NULL
275 );
276 } else {
277 //
278 // Append the device path to the ConInDev environment variable
279 //
280 ConPlatformUpdateDeviceVariable (
281 L"ConInDev",
282 DevicePath,
283 APPEND
284 );
285
286 //
287 // If the device path is an instance in the ConIn environment variable,
288 // then install EfiConsoleInDeviceGuid onto ControllerHandle
289 //
290 Status = ConPlatformUpdateDeviceVariable (
291 L"ConIn",
292 DevicePath,
293 CHECK
294 );
295
296 if (!EFI_ERROR (Status)) {
297 gBS->InstallMultipleProtocolInterfaces (
298 &ControllerHandle,
299 &gEfiConsoleInDeviceGuid,
300 NULL,
301 NULL
302 );
303 } else {
304 gBS->CloseProtocol (
305 ControllerHandle,
306 &gEfiSimpleTextInProtocolGuid,
307 This->DriverBindingHandle,
308 ControllerHandle
309 );
310 }
311 }
312
313 return EFI_SUCCESS;
314 }
315
316 /**
317 Start this driver on ControllerHandle by opening Simple Text Out protocol,
318 reading Device Path, and installing Console Out Devcic GUID, Standard Error
319 Device GUID on ControllerHandle.
320
321 If this devcie is not one hot-plug devce, append its device path into the
322 console environment variables ConOutDev, StdErrDev.
323
324 @param This Protocol instance pointer.
325 @param ControllerHandle Handle of device to bind driver to
326 @param RemainingDevicePath Optional parameter use to pick a specific child
327 device to start.
328
329 @retval EFI_SUCCESS This driver is added to ControllerHandle
330 @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle
331 @retval other This driver does not support this device
332
333 **/
334 EFI_STATUS
335 EFIAPI
336 ConPlatformTextOutDriverBindingStart (
337 IN EFI_DRIVER_BINDING_PROTOCOL *This,
338 IN EFI_HANDLE ControllerHandle,
339 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
340 )
341 {
342 EFI_STATUS Status;
343 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
344 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *TextOut;
345 BOOLEAN NeedClose;
346
347 NeedClose = TRUE;
348
349 //
350 // Get the Device Path Protocol so the environment variables can be updated
351 //
352 Status = gBS->OpenProtocol (
353 ControllerHandle,
354 &gEfiDevicePathProtocolGuid,
355 (VOID **) &DevicePath,
356 This->DriverBindingHandle,
357 ControllerHandle,
358 EFI_OPEN_PROTOCOL_GET_PROTOCOL
359 );
360 if (EFI_ERROR (Status)) {
361 return Status;
362 }
363 //
364 // Open the Simple Text Output Protocol BY_DRIVER
365 //
366 Status = gBS->OpenProtocol (
367 ControllerHandle,
368 &gEfiSimpleTextOutProtocolGuid,
369 (VOID **) &TextOut,
370 This->DriverBindingHandle,
371 ControllerHandle,
372 EFI_OPEN_PROTOCOL_BY_DRIVER
373 );
374 if (EFI_ERROR (Status)) {
375 return Status;
376 }
377 //
378 // Check the device handle, if it is a hot plug device,
379 // do not put the device path into ConOutDev and StdErrDev,
380 // and install gEfiConsoleOutDeviceGuid to the device handle directly.
381 // The policy is, make hot plug device plug in and play immediately.
382 //
383 if (IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
384 gBS->InstallMultipleProtocolInterfaces (
385 &ControllerHandle,
386 &gEfiConsoleOutDeviceGuid,
387 NULL,
388 NULL
389 );
390 } else {
391 //
392 // Append the device path to the ConOutDev environment variable
393 //
394 ConPlatformUpdateDeviceVariable (
395 L"ConOutDev",
396 DevicePath,
397 APPEND
398 );
399 //
400 // Append the device path to the StdErrDev environment variable
401 //
402 ConPlatformUpdateDeviceVariable (
403 L"ErrOutDev",
404 DevicePath,
405 APPEND
406 );
407
408 //
409 // If the device path is an instance in the ConOut environment variable,
410 // then install EfiConsoleOutDeviceGuid onto ControllerHandle
411 //
412 Status = ConPlatformUpdateDeviceVariable (
413 L"ConOut",
414 DevicePath,
415 CHECK
416 );
417
418 if (!EFI_ERROR (Status)) {
419 NeedClose = FALSE;
420 Status = gBS->InstallMultipleProtocolInterfaces (
421 &ControllerHandle,
422 &gEfiConsoleOutDeviceGuid,
423 NULL,
424 NULL
425 );
426 }
427 //
428 // If the device path is an instance in the StdErr environment variable,
429 // then install EfiStandardErrorDeviceGuid onto ControllerHandle
430 //
431 Status = ConPlatformUpdateDeviceVariable (
432 L"ErrOut",
433 DevicePath,
434 CHECK
435 );
436 if (!EFI_ERROR (Status)) {
437 NeedClose = FALSE;
438 gBS->InstallMultipleProtocolInterfaces (
439 &ControllerHandle,
440 &gEfiStandardErrorDeviceGuid,
441 NULL,
442 NULL
443 );
444 }
445
446 if (NeedClose) {
447 gBS->CloseProtocol (
448 ControllerHandle,
449 &gEfiSimpleTextOutProtocolGuid,
450 This->DriverBindingHandle,
451 ControllerHandle
452 );
453 }
454 }
455
456 return EFI_SUCCESS;
457 }
458
459 /**
460 Stop this driver on ControllerHandle by removing Console In Devcice GUID
461 and closing the Simple Text In protocol on ControllerHandle.
462
463 @param This Protocol instance pointer.
464 @param ControllerHandle Handle of device to stop driver on
465 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
466 children is zero stop the entire bus driver.
467 @param ChildHandleBuffer List of Child Handles to Stop.
468
469 @retval EFI_SUCCESS This driver is removed ControllerHandle
470 @retval other This driver was not removed from this device
471
472 **/
473 EFI_STATUS
474 EFIAPI
475 ConPlatformTextInDriverBindingStop (
476 IN EFI_DRIVER_BINDING_PROTOCOL *This,
477 IN EFI_HANDLE ControllerHandle,
478 IN UINTN NumberOfChildren,
479 IN EFI_HANDLE *ChildHandleBuffer
480 )
481 {
482 EFI_STATUS Status;
483 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
484
485 //
486 // hot plug device is not included into the console associated variables,
487 // so no need to check variable for those hot plug devices.
488 //
489 if (!IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
490 //
491 // Get the Device Path Protocol so the environment variables can be updated
492 //
493 Status = gBS->OpenProtocol (
494 ControllerHandle,
495 &gEfiDevicePathProtocolGuid,
496 (VOID **) &DevicePath,
497 This->DriverBindingHandle,
498 ControllerHandle,
499 EFI_OPEN_PROTOCOL_GET_PROTOCOL
500 );
501 if (!EFI_ERROR (Status)) {
502 //
503 // Remove DevicePath from ConInDev
504 //
505 ConPlatformUpdateDeviceVariable (
506 L"ConInDev",
507 DevicePath,
508 DELETE
509 );
510 }
511 }
512 //
513 // Uninstall the Console Device GUIDs from Controller Handle
514 //
515 ConPlatformUnInstallProtocol (
516 This,
517 ControllerHandle,
518 &gEfiConsoleInDeviceGuid
519 );
520
521 //
522 // Close the Simple Input Protocol
523 //
524 gBS->CloseProtocol (
525 ControllerHandle,
526 &gEfiSimpleTextInProtocolGuid,
527 This->DriverBindingHandle,
528 ControllerHandle
529 );
530
531 return EFI_SUCCESS;
532 }
533
534
535 /**
536 Stop this driver on ControllerHandle by removing Console Out Devcice GUID
537 and closing the Simple Text Out protocol on ControllerHandle.
538
539 @param This Protocol instance pointer.
540 @param ControllerHandle Handle of device to stop driver on
541 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
542 children is zero stop the entire bus driver.
543 @param ChildHandleBuffer List of Child Handles to Stop.
544
545 @retval EFI_SUCCESS This driver is removed ControllerHandle
546 @retval other This driver was not removed from this device
547
548 **/
549 EFI_STATUS
550 EFIAPI
551 ConPlatformTextOutDriverBindingStop (
552 IN EFI_DRIVER_BINDING_PROTOCOL *This,
553 IN EFI_HANDLE ControllerHandle,
554 IN UINTN NumberOfChildren,
555 IN EFI_HANDLE *ChildHandleBuffer
556 )
557 {
558 EFI_STATUS Status;
559 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
560
561 //
562 // hot plug device is not included into the console associated variables,
563 // so no need to check variable for those hot plug devices.
564 //
565 if (!IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
566 //
567 // Get the Device Path Protocol so the environment variables can be updated
568 //
569 Status = gBS->OpenProtocol (
570 ControllerHandle,
571 &gEfiDevicePathProtocolGuid,
572 (VOID **) &DevicePath,
573 This->DriverBindingHandle,
574 ControllerHandle,
575 EFI_OPEN_PROTOCOL_GET_PROTOCOL
576 );
577 if (!EFI_ERROR (Status)) {
578 //
579 // Remove DevicePath from ConOutDev, and StdErrDev
580 //
581 ConPlatformUpdateDeviceVariable (
582 L"ConOutDev",
583 DevicePath,
584 DELETE
585 );
586 ConPlatformUpdateDeviceVariable (
587 L"ErrOutDev",
588 DevicePath,
589 DELETE
590 );
591 }
592 }
593 //
594 // Uninstall the Console Device GUIDs from Controller Handle
595 //
596 ConPlatformUnInstallProtocol (
597 This,
598 ControllerHandle,
599 &gEfiConsoleOutDeviceGuid
600 );
601
602 ConPlatformUnInstallProtocol (
603 This,
604 ControllerHandle,
605 &gEfiStandardErrorDeviceGuid
606 );
607
608 //
609 // Close the Simple Text Output Protocol
610 //
611 gBS->CloseProtocol (
612 ControllerHandle,
613 &gEfiSimpleTextOutProtocolGuid,
614 This->DriverBindingHandle,
615 ControllerHandle
616 );
617
618 return EFI_SUCCESS;
619 }
620
621
622 /**
623 Uninstall the specified protocol.
624
625 @param This Protocol instance pointer.
626 @param Handle Handle of device to uninstall protocol on.
627 @param ProtocolGuid The specified protocol need to be uninstalled.
628
629 @return None.
630
631 **/
632 VOID
633 ConPlatformUnInstallProtocol (
634 IN EFI_DRIVER_BINDING_PROTOCOL *This,
635 IN EFI_HANDLE Handle,
636 IN EFI_GUID *ProtocolGuid
637 )
638 {
639 EFI_STATUS Status;
640
641 Status = gBS->OpenProtocol (
642 Handle,
643 ProtocolGuid,
644 NULL,
645 This->DriverBindingHandle,
646 Handle,
647 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
648 );
649
650 if (!EFI_ERROR (Status)) {
651 gBS->UninstallMultipleProtocolInterfaces (
652 Handle,
653 ProtocolGuid,
654 NULL,
655 NULL
656 );
657 }
658
659 return ;
660 }
661
662 /**
663 Read the EFI variable (Name) and return a dynamically allocated
664 buffer, and the size of the buffer. On failure return NULL.
665
666 @param Name String part of EFI variable name
667
668 @return Dynamically allocated memory that contains a copy of the EFI variable.
669 Caller is repsoncible freeing the buffer. Return NULL means Variable
670 was not read.
671
672 **/
673 VOID *
674 ConPlatformGetVariable (
675 IN CHAR16 *Name
676 )
677 {
678 EFI_STATUS Status;
679 VOID *Buffer;
680 UINTN BufferSize;
681
682 BufferSize = 0;
683 Buffer = NULL;
684
685 //
686 // Test to see if the variable exists. If it doesn't, reuturn NULL.
687 //
688 Status = gRT->GetVariable (
689 Name,
690 &gEfiGlobalVariableGuid,
691 NULL,
692 &BufferSize,
693 Buffer
694 );
695
696 if (Status == EFI_BUFFER_TOO_SMALL) {
697 //
698 // Allocate the buffer to return
699 //
700 Buffer = AllocatePool (BufferSize);
701 if (Buffer == NULL) {
702 return NULL;
703 }
704 //
705 // Read variable into the allocated buffer.
706 //
707 Status = gRT->GetVariable (
708 Name,
709 &gEfiGlobalVariableGuid,
710 NULL,
711 &BufferSize,
712 Buffer
713 );
714 if (EFI_ERROR (Status)) {
715 FreePool (Buffer);
716 //
717 // To make sure Buffer is NULL if any error occurs.
718 //
719 Buffer = NULL;
720 }
721 }
722
723 return Buffer;
724 }
725
726 /**
727 Function compares a device path data structure to that of all the nodes of a
728 second device path instance.
729
730
731 @param Multi A pointer to a multi-instance device path data structure.
732 @param Single A pointer to a single-instance device path data structure.
733 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
734 points to the remaining device path data structure.
735 (remaining device path = Multi - Single.)
736 @param Delete If TRUE, means removing Single from Multi.
737 If FALSE, the routine just check whether Single matches
738 with any instance in Multi.
739
740 @retval EFI_SUCCESS If the Single is contained within Multi.
741 @retval EFI_NOT_FOUND If the Single is not contained within Multi.
742
743 **/
744 EFI_STATUS
745 ConPlatformMatchDevicePaths (
746 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
747 IN EFI_DEVICE_PATH_PROTOCOL *Single,
748 OUT EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,
749 IN BOOLEAN Delete
750 )
751 {
752 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
753 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath1;
754 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath2;
755 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;
756 UINTN Size;
757
758 //
759 // The passed in DevicePath should not be NULL
760 //
761 if ((Multi == NULL) || (Single == NULL)) {
762 return EFI_NOT_FOUND;
763 }
764
765 //
766 // If performing Delete operation, the NewDevicePath must not be NULL.
767 //
768 if (Delete) {
769 ASSERT (NewDevicePath != NULL);
770 }
771
772 TempDevicePath1 = NULL;
773
774 DevicePath = Multi;
775 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
776
777 //
778 // Search for the match of 'Single' in 'Multi'
779 //
780 while (DevicePathInst != NULL) {
781 if (CompareMem (Single, DevicePathInst, Size) == 0) {
782 if (!Delete) {
783 //
784 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.
785 //
786 FreePool (DevicePathInst);
787 return EFI_SUCCESS;
788 }
789 } else {
790 if (Delete) {
791 //
792 // Append the mis-matched devcie path into remaining device path.
793 //
794 TempDevicePath2 = AppendDevicePathInstance (
795 TempDevicePath1,
796 DevicePathInst
797 );
798 if (TempDevicePath1 != NULL) {
799 FreePool (TempDevicePath1);
800 }
801 TempDevicePath1 = TempDevicePath2;
802 }
803 }
804
805 FreePool (DevicePathInst);
806 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
807 }
808
809 if (Delete) {
810 //
811 // Return the remaining device path data structure
812 //
813 *NewDevicePath = TempDevicePath1;
814 return EFI_SUCCESS;
815 }
816
817 return EFI_NOT_FOUND;
818 }
819
820 /**
821 Update console environment variables.
822
823 @param VariableName Console environment variables, ConOutDev, ConInDev
824 StdErrDev, ConIn or ConOut.
825 @param DevicePath Console devcie's device path.
826 @param Operation Variable operations, such as APPEND or DELETE.
827
828 @retval EFI_SUCCESS Variable operates successfully.
829 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
830 @retval other Variable updating failed.
831
832 **/
833 EFI_STATUS
834 ConPlatformUpdateDeviceVariable (
835 IN CHAR16 *VariableName,
836 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
837 IN CONPLATFORM_VAR_OPERATION Operation
838 )
839 {
840 EFI_STATUS Status;
841 EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;
842 EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;
843
844 VariableDevicePath = NULL;
845 NewVariableDevicePath = NULL;
846
847 //
848 // Get Variable according to variable name.
849 // The memory for Variable is allocated within ConPlatformGetVarible(),
850 // it is the caller's responsibility to free the memory before return.
851 //
852 VariableDevicePath = ConPlatformGetVariable (VariableName);
853
854 if (Operation != DELETE) {
855 //
856 // Match specified DevicePath in Console Variable.
857 //
858 Status = ConPlatformMatchDevicePaths (
859 VariableDevicePath,
860 DevicePath,
861 NULL,
862 FALSE
863 );
864
865 if ((Operation == CHECK) || (!EFI_ERROR (Status))) {
866 //
867 // The device path is already in the variable
868 //
869 if (VariableDevicePath != NULL) {
870 FreePool (VariableDevicePath);
871 }
872
873 return Status;
874 }
875 //
876 // The device path is not in variable. Append DevicePath to the
877 // environment variable that is a multi-instance device path.
878 //
879 Status = EFI_SUCCESS;
880 NewVariableDevicePath = AppendDevicePathInstance (
881 VariableDevicePath,
882 DevicePath
883 );
884 if (NewVariableDevicePath == NULL) {
885 Status = EFI_OUT_OF_RESOURCES;
886 }
887
888 } else {
889 //
890 // Remove DevicePath from the environment variable that
891 // is a multi-instance device path.
892 //
893 Status = ConPlatformMatchDevicePaths (
894 VariableDevicePath,
895 DevicePath,
896 &NewVariableDevicePath,
897 TRUE
898 );
899 }
900
901 if (VariableDevicePath != NULL) {
902 FreePool (VariableDevicePath);
903 }
904
905 if (EFI_ERROR (Status)) {
906 return Status;
907 }
908
909 if (NewVariableDevicePath != NULL) {
910 //
911 // Update Console Environment Variable.
912 //
913 Status = gRT->SetVariable (
914 VariableName,
915 &gEfiGlobalVariableGuid,
916 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
917 GetDevicePathSize (NewVariableDevicePath),
918 NewVariableDevicePath
919 );
920
921 FreePool (NewVariableDevicePath);
922 }
923
924 return Status;
925 }
926
927 /**
928 Check if the device is one hot-plug supported.
929
930 @param DriverBindingHandle Protocol instance pointer.
931 @param ControllerHandle Handle of device to check.
932
933 @retval TRUE The devcie is a hot-plug device
934 @retval FALSE The devcie is not a hot-plug device.
935
936 **/
937 BOOLEAN
938 IsHotPlugDevice (
939 EFI_HANDLE DriverBindingHandle,
940 EFI_HANDLE ControllerHandle
941 )
942 {
943 EFI_STATUS Status;
944
945 //
946 // HotPlugDeviceGuid indicates ControllerHandle stands for a hot plug device.
947 //
948 Status = gBS->OpenProtocol (
949 ControllerHandle,
950 &gEfiHotPlugDeviceGuid,
951 NULL,
952 DriverBindingHandle,
953 ControllerHandle,
954 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
955 );
956 if (EFI_ERROR (Status)) {
957 return FALSE;
958 }
959
960 return TRUE;
961 }