]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatform.c
Code Scrub for ConPlatform.
[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 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 (This->DriverBindingHandle, ControllerHandle)) {
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, StdErrDev.
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 StdErrDev,
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 (This->DriverBindingHandle, ControllerHandle)) {
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 StdErrDev 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 StdErr 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 // If it is not a hot-plug device, first delete it from the ConInDev variable.
486 //
487 if (!IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
488 //
489 // Get the Device Path Protocol so the environment variables can be updated
490 //
491 Status = gBS->OpenProtocol (
492 ControllerHandle,
493 &gEfiDevicePathProtocolGuid,
494 (VOID **) &DevicePath,
495 This->DriverBindingHandle,
496 ControllerHandle,
497 EFI_OPEN_PROTOCOL_GET_PROTOCOL
498 );
499 if (!EFI_ERROR (Status)) {
500 //
501 // Remove DevicePath from ConInDev
502 //
503 ConPlatformUpdateDeviceVariable (
504 L"ConInDev",
505 DevicePath,
506 DELETE
507 );
508 }
509 }
510 //
511 // Uninstall the Console Device GUIDs from Controller Handle
512 //
513 ConPlatformUnInstallProtocol (
514 This,
515 ControllerHandle,
516 &gEfiConsoleInDeviceGuid
517 );
518
519 //
520 // Close the Simple Text Input Protocol
521 //
522 gBS->CloseProtocol (
523 ControllerHandle,
524 &gEfiSimpleTextInProtocolGuid,
525 This->DriverBindingHandle,
526 ControllerHandle
527 );
528
529 return EFI_SUCCESS;
530 }
531
532
533 /**
534 Stop this driver on ControllerHandle by removing Console Out Devcice GUID
535 and closing the Simple Text Output protocol on ControllerHandle.
536
537 @param This Protocol instance pointer.
538 @param ControllerHandle Handle of device to stop driver on
539 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
540 children is zero stop the entire bus driver.
541 @param ChildHandleBuffer List of Child Handles to Stop.
542
543 @retval EFI_SUCCESS This driver is removed ControllerHandle
544 @retval other This driver was not removed from this device
545
546 **/
547 EFI_STATUS
548 EFIAPI
549 ConPlatformTextOutDriverBindingStop (
550 IN EFI_DRIVER_BINDING_PROTOCOL *This,
551 IN EFI_HANDLE ControllerHandle,
552 IN UINTN NumberOfChildren,
553 IN EFI_HANDLE *ChildHandleBuffer
554 )
555 {
556 EFI_STATUS Status;
557 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
558
559 //
560 // If it is not a hot-plug device, first delete it from the ConOutDev and StdErrDev variable.
561 //
562 if (!IsHotPlugDevice (This->DriverBindingHandle, ControllerHandle)) {
563 //
564 // Get the Device Path Protocol so the environment variables can be updated
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 if (!EFI_ERROR (Status)) {
575 //
576 // Remove DevicePath from ConOutDev, and StdErrDev
577 //
578 ConPlatformUpdateDeviceVariable (
579 L"ConOutDev",
580 DevicePath,
581 DELETE
582 );
583 ConPlatformUpdateDeviceVariable (
584 L"ErrOutDev",
585 DevicePath,
586 DELETE
587 );
588 }
589 }
590 //
591 // Uninstall the Console Device GUIDs from Controller Handle
592 //
593 ConPlatformUnInstallProtocol (
594 This,
595 ControllerHandle,
596 &gEfiConsoleOutDeviceGuid
597 );
598
599 ConPlatformUnInstallProtocol (
600 This,
601 ControllerHandle,
602 &gEfiStandardErrorDeviceGuid
603 );
604
605 //
606 // Close the Simple Text Output Protocol
607 //
608 gBS->CloseProtocol (
609 ControllerHandle,
610 &gEfiSimpleTextOutProtocolGuid,
611 This->DriverBindingHandle,
612 ControllerHandle
613 );
614
615 return EFI_SUCCESS;
616 }
617
618
619 /**
620 Uninstall the specified protocol.
621
622 @param This Protocol instance pointer.
623 @param Handle Handle of device to uninstall protocol on.
624 @param ProtocolGuid The specified protocol need to be uninstalled.
625
626 **/
627 VOID
628 ConPlatformUnInstallProtocol (
629 IN EFI_DRIVER_BINDING_PROTOCOL *This,
630 IN EFI_HANDLE Handle,
631 IN EFI_GUID *ProtocolGuid
632 )
633 {
634 EFI_STATUS Status;
635
636 Status = gBS->OpenProtocol (
637 Handle,
638 ProtocolGuid,
639 NULL,
640 This->DriverBindingHandle,
641 Handle,
642 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
643 );
644
645 if (!EFI_ERROR (Status)) {
646 gBS->UninstallMultipleProtocolInterfaces (
647 Handle,
648 ProtocolGuid,
649 NULL,
650 NULL
651 );
652 }
653
654 return ;
655 }
656
657 /**
658 Get the necessary size of buffer and read the variabe.
659
660 First get the necessary size of buffer. Then read the
661 EFI variable (Name) and return a dynamically allocated
662 buffer. On failure return NULL.
663
664 @param Name String part of EFI variable name
665
666 @return Dynamically allocated memory that contains a copy of the EFI variable.
667 Caller is repsoncible freeing the buffer. Return NULL means Variable
668 was not read.
669
670 **/
671 VOID *
672 ConPlatformGetVariable (
673 IN CHAR16 *Name
674 )
675 {
676 EFI_STATUS Status;
677 VOID *Buffer;
678 UINTN BufferSize;
679
680 BufferSize = 0;
681 Buffer = NULL;
682
683 //
684 // Test to see if the variable exists. If it doesn't, return NULL.
685 //
686 Status = gRT->GetVariable (
687 Name,
688 &gEfiGlobalVariableGuid,
689 NULL,
690 &BufferSize,
691 Buffer
692 );
693
694 if (Status == EFI_BUFFER_TOO_SMALL) {
695 //
696 // Allocate the buffer to return
697 //
698 Buffer = AllocatePool (BufferSize);
699 if (Buffer == NULL) {
700 return NULL;
701 }
702 //
703 // Read variable into the allocated buffer.
704 //
705 Status = gRT->GetVariable (
706 Name,
707 &gEfiGlobalVariableGuid,
708 NULL,
709 &BufferSize,
710 Buffer
711 );
712 if (EFI_ERROR (Status)) {
713 FreePool (Buffer);
714 //
715 // To make sure Buffer is NULL if any error occurs.
716 //
717 Buffer = NULL;
718 }
719 }
720
721 return Buffer;
722 }
723
724 /**
725 Function compares a device path data structure to that of all the nodes of a
726 second device path instance.
727
728 @param Multi A pointer to a multi-instance device path data structure.
729 @param Single A pointer to a single-instance device path data structure.
730 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
731 points to the remaining device path data structure.
732 (remaining device path = Multi - Single.)
733 @param Delete If TRUE, means removing Single from Multi.
734 If FALSE, the routine just check whether Single matches
735 with any instance in Multi.
736
737 @retval EFI_SUCCESS If the Single is contained within Multi.
738 @retval EFI_NOT_FOUND If the Single is not contained within Multi.
739 @retval EFI_INVALID_PARAMETER Multi is NULL.
740 @retval EFI_INVALID_PARAMETER Single is NULL.
741 @retval EFI_INVALID_PARAMETER NewDevicePath is NULL when Delete is TRUE.
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_INVALID_PARAMETER;
763 }
764
765 //
766 // If performing Delete operation, the NewDevicePath must not be NULL.
767 //
768 if (Delete) {
769 if (NewDevicePath == NULL) {
770 return EFI_INVALID_PARAMETER;
771 }
772 }
773
774 TempDevicePath1 = NULL;
775
776 DevicePath = Multi;
777 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
778
779 //
780 // Search for the match of 'Single' in 'Multi'
781 //
782 while (DevicePathInst != NULL) {
783 if (CompareMem (Single, DevicePathInst, Size) == 0) {
784 if (!Delete) {
785 //
786 // If Delete is FALSE, return EFI_SUCCESS if Single is found in Multi.
787 //
788 FreePool (DevicePathInst);
789 return EFI_SUCCESS;
790 }
791 } else {
792 if (Delete) {
793 //
794 // If the node of Multi does not match Single, then added it back to the result.
795 // That is, the node matching Single will be dropped and deleted from result.
796 //
797 TempDevicePath2 = AppendDevicePathInstance (
798 TempDevicePath1,
799 DevicePathInst
800 );
801 if (TempDevicePath1 != NULL) {
802 FreePool (TempDevicePath1);
803 }
804 TempDevicePath1 = TempDevicePath2;
805 }
806 }
807
808 FreePool (DevicePathInst);
809 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
810 }
811
812 if (Delete) {
813 //
814 // Return the new device path data structure with specified node deleted.
815 //
816 *NewDevicePath = TempDevicePath1;
817 return EFI_SUCCESS;
818 }
819
820 return EFI_NOT_FOUND;
821 }
822
823 /**
824 Update console environment variables.
825
826 @param VariableName Console environment variables, ConOutDev, ConInDev
827 StdErrDev, ConIn or ConOut.
828 @param DevicePath Console devcie's device path.
829 @param Operation Variable operations, including APPEND, CHECK and DELETE.
830
831 @retval EFI_SUCCESS Variable operates successfully.
832 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
833 @retval other Variable updating failed.
834
835 **/
836 EFI_STATUS
837 ConPlatformUpdateDeviceVariable (
838 IN CHAR16 *VariableName,
839 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
840 IN CONPLATFORM_VAR_OPERATION Operation
841 )
842 {
843 EFI_STATUS Status;
844 EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;
845 EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;
846
847 VariableDevicePath = NULL;
848 NewVariableDevicePath = NULL;
849
850 //
851 // Get Variable according to variable name.
852 // The memory for Variable is allocated within ConPlatformGetVarible(),
853 // it is the caller's responsibility to free the memory before return.
854 //
855 VariableDevicePath = ConPlatformGetVariable (VariableName);
856
857 if (Operation != DELETE) {
858 //
859 // Match specified DevicePath in Console Variable.
860 //
861 Status = ConPlatformMatchDevicePaths (
862 VariableDevicePath,
863 DevicePath,
864 NULL,
865 FALSE
866 );
867
868 if ((Operation == CHECK) || (!EFI_ERROR (Status))) {
869 //
870 // Branch here includes 2 cases:
871 // 1. Operation is CHECK, simply return Status.
872 // 2. Operation is APPEND, and device path already exists in variable, also return.
873 //
874 if (VariableDevicePath != NULL) {
875 FreePool (VariableDevicePath);
876 }
877
878 return Status;
879 }
880 //
881 // We reach here to append a device path that does not exist in variable.
882 //
883 Status = EFI_SUCCESS;
884 NewVariableDevicePath = AppendDevicePathInstance (
885 VariableDevicePath,
886 DevicePath
887 );
888 if (NewVariableDevicePath == NULL) {
889 Status = EFI_OUT_OF_RESOURCES;
890 }
891
892 } else {
893 //
894 // We reach here to remove DevicePath from the environment variable that
895 // is a multi-instance device path.
896 //
897 Status = ConPlatformMatchDevicePaths (
898 VariableDevicePath,
899 DevicePath,
900 &NewVariableDevicePath,
901 TRUE
902 );
903 }
904
905 if (VariableDevicePath != NULL) {
906 FreePool (VariableDevicePath);
907 }
908
909 if (EFI_ERROR (Status)) {
910 return Status;
911 }
912
913 if (NewVariableDevicePath != NULL) {
914 //
915 // Update Console Environment Variable.
916 //
917 Status = gRT->SetVariable (
918 VariableName,
919 &gEfiGlobalVariableGuid,
920 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
921 GetDevicePathSize (NewVariableDevicePath),
922 NewVariableDevicePath
923 );
924
925 FreePool (NewVariableDevicePath);
926 }
927
928 return Status;
929 }
930
931 /**
932 Check if the device supports hot-plug.
933
934 @param DriverBindingHandle Protocol instance pointer.
935 @param ControllerHandle Handle of device to check.
936
937 @retval TRUE The devcie is a hot-plug device
938 @retval FALSE The devcie is not a hot-plug device.
939
940 **/
941 BOOLEAN
942 IsHotPlugDevice (
943 EFI_HANDLE DriverBindingHandle,
944 EFI_HANDLE ControllerHandle
945 )
946 {
947 EFI_STATUS Status;
948
949 //
950 // HotPlugDeviceGuid indicates ControllerHandle stands for a hot plug device.
951 //
952 Status = gBS->OpenProtocol (
953 ControllerHandle,
954 &gEfiHotPlugDeviceGuid,
955 NULL,
956 DriverBindingHandle,
957 ControllerHandle,
958 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
959 );
960 if (EFI_ERROR (Status)) {
961 return FALSE;
962 }
963
964 return TRUE;
965 }