]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatform.c
Fix function comment to follows doxygen format.
[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[in] ImageHandle The firmware allocated handle for the EFI image.
41 @param[in] 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 specific 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 to see 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 specific 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 Unstall the specific protocol.
624
625 @param This Protocol instance pointer.
626 @param Handle Handle of device to unstall protocol on.
627 @param ProtocolGuid The specific 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
667 @param Name String part of EFI variable name
668
669 @return Dynamically allocated memory that contains a copy of the EFI variable.
670 Caller is repsoncible freeing the buffer.
671 NULL - Variable was not read
672
673 **/
674 VOID *
675 ConPlatformGetVariable (
676 IN CHAR16 *Name
677 )
678 {
679 EFI_STATUS Status;
680 VOID *Buffer;
681 UINTN BufferSize;
682
683 BufferSize = 0;
684 Buffer = NULL;
685
686 //
687 // Test to see if the variable exists. If it doesn't reuturn NULL
688 //
689 Status = gRT->GetVariable (
690 Name,
691 &gEfiGlobalVariableGuid,
692 NULL,
693 &BufferSize,
694 Buffer
695 );
696
697 if (Status == EFI_BUFFER_TOO_SMALL) {
698 //
699 // Allocate the buffer to return
700 //
701 Buffer = AllocatePool (BufferSize);
702 if (Buffer == NULL) {
703 return NULL;
704 }
705 //
706 // Read variable into the allocated buffer.
707 //
708 Status = gRT->GetVariable (
709 Name,
710 &gEfiGlobalVariableGuid,
711 NULL,
712 &BufferSize,
713 Buffer
714 );
715 if (EFI_ERROR (Status)) {
716 FreePool (Buffer);
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
729 @param Multi A pointer to a multi-instance device path data structure.
730 @param Single A pointer to a single-instance device path data structure.
731 @param NewDevicePath If Delete is TRUE, this parameter must not be null, and it
732 points to the remaining device path data structure.
733 (remaining device path = Multi - Single.)
734 @param Delete If TRUE, means removing Single from Multi.
735 If FALSE, the routine just check whether Single matches
736 with any instance in Multi.
737
738 @return The function returns EFI_SUCCESS if the Single is contained within Multi.
739 Otherwise, EFI_NOT_FOUND is returned.
740
741 **/
742 EFI_STATUS
743 ConPlatformMatchDevicePaths (
744 IN EFI_DEVICE_PATH_PROTOCOL *Multi,
745 IN EFI_DEVICE_PATH_PROTOCOL *Single,
746 IN EFI_DEVICE_PATH_PROTOCOL **NewDevicePath OPTIONAL,
747 IN BOOLEAN Delete
748 )
749 {
750 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
751 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
752 EFI_DEVICE_PATH_PROTOCOL *DevicePathInst;
753 UINTN Size;
754
755 //
756 // The passed in DevicePath should not be NULL
757 //
758 if ((Multi == NULL) || (Single == NULL)) {
759 return EFI_NOT_FOUND;
760 }
761 //
762 // if performing Delete operation, the NewDevicePath must not be NULL.
763 //
764 TempDevicePath = NULL;
765
766 DevicePath = Multi;
767 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
768
769 //
770 // search for the match of 'Single' in 'Multi'
771 //
772 while (DevicePathInst != NULL) {
773 if (CompareMem (Single, DevicePathInst, Size) == 0) {
774 if (!Delete) {
775 FreePool (DevicePathInst);
776 return EFI_SUCCESS;
777 }
778 } else {
779 if (Delete) {
780 TempDevicePath = AppendDevicePathInstance (
781 NULL,
782 DevicePathInst
783 );
784 }
785 }
786
787 FreePool (DevicePathInst);
788 DevicePathInst = GetNextDevicePathInstance (&DevicePath, &Size);
789 }
790
791 if (Delete) {
792 *NewDevicePath = TempDevicePath;
793 return EFI_SUCCESS;
794 }
795
796 return EFI_NOT_FOUND;
797 }
798
799 /**
800 Update console devicein console environment variables.
801
802 @param VariableName Console environment variables, ConOutDev, ConInDev
803 StdErrDev, ConIn or ConOut.
804 @param DevicePath Console devcie's device path.
805 @param Operation Variable operations, such as APPEND or DELETE.
806
807 @retval EFI_SUCCESS Variable operates successfully.
808 @retval EFI_OUT_OF_RESOURCES If variable cannot be appended.
809 @retval other Variable updating failed.
810
811 **/
812 EFI_STATUS
813 ConPlatformUpdateDeviceVariable (
814 IN CHAR16 *VariableName,
815 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
816 IN CONPLATFORM_VAR_OPERATION Operation
817 )
818 {
819 EFI_STATUS Status;
820 EFI_DEVICE_PATH_PROTOCOL *VariableDevicePath;
821 EFI_DEVICE_PATH_PROTOCOL *NewVariableDevicePath;
822
823 VariableDevicePath = NULL;
824 NewVariableDevicePath = NULL;
825
826 //
827 // Get Variable according to variable name.
828 // The memory for Variable is allocated within ConPlatformGetVarible(),
829 // it is the caller's responsibility to free the memory before return.
830 //
831 VariableDevicePath = ConPlatformGetVariable (VariableName);
832
833 if (Operation != DELETE) {
834
835 Status = ConPlatformMatchDevicePaths (
836 VariableDevicePath,
837 DevicePath,
838 NULL,
839 FALSE
840 );
841
842 if ((Operation == CHECK) || (!EFI_ERROR (Status))) {
843 //
844 // The device path is already in the variable
845 //
846 if (VariableDevicePath != NULL) {
847 FreePool (VariableDevicePath);
848 }
849
850 return Status;
851 }
852 //
853 // The device path is not in variable. Append DevicePath to the
854 // environment variable that is a multi-instance device path.
855 //
856 Status = EFI_SUCCESS;
857 NewVariableDevicePath = AppendDevicePathInstance (
858 VariableDevicePath,
859 DevicePath
860 );
861 if (NewVariableDevicePath == NULL) {
862 Status = EFI_OUT_OF_RESOURCES;
863 }
864
865 } else {
866 //
867 // Remove DevicePath from the environment variable that
868 // is a multi-instance device path.
869 //
870 Status = ConPlatformMatchDevicePaths (
871 VariableDevicePath,
872 DevicePath,
873 &NewVariableDevicePath,
874 TRUE
875 );
876 }
877
878 if (VariableDevicePath != NULL) {
879 FreePool (VariableDevicePath);
880 }
881
882 if (EFI_ERROR (Status)) {
883 return Status;
884 }
885
886 if (NewVariableDevicePath != NULL) {
887 Status = gRT->SetVariable (
888 VariableName,
889 &gEfiGlobalVariableGuid,
890 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
891 GetDevicePathSize (NewVariableDevicePath),
892 NewVariableDevicePath
893 );
894
895 FreePool (NewVariableDevicePath);
896 }
897
898 return Status;
899 }
900
901 /**
902 Check if the device is one hot-plug supported.
903
904 @param DriverBindingHandle Protocol instance pointer.
905 @param ControllerHandle Handle of device to check.
906
907 @retval TRUE The devcie is a hot-plug device
908 @retval FALSE The devcie is not a hot-plug device.
909
910 **/
911 BOOLEAN
912 IsHotPlugDevice (
913 EFI_HANDLE DriverBindingHandle,
914 EFI_HANDLE ControllerHandle
915 )
916 {
917 EFI_STATUS Status;
918
919 //
920 // HotPlugDeviceGuid indicates ControllerHandle stands for a hot plug device.
921 //
922 Status = gBS->OpenProtocol (
923 ControllerHandle,
924 &gEfiHotPlugDeviceGuid,
925 NULL,
926 DriverBindingHandle,
927 ControllerHandle,
928 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
929 );
930 if (EFI_ERROR (Status)) {
931 return FALSE;
932 }
933
934 return TRUE;
935 }