3 Copyright (c) 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include "PlatDriOver.h"
23 LIST_ENTRY mDevicePathStack
= INITIALIZE_LIST_HEAD_VARIABLE (mDevicePathStack
);
27 Install the Platform Driver Override Protocol, and ensure there is only one Platform Driver Override Protocol
30 @param gPlatformDriverOverride PlatformDriverOverride protocol interface which
33 @retval EFI_ALREADY_STARTED There has been a Platform Driver Override
34 Protocol in the system, cannot install it again.
35 @retval Other Returned by InstallProtocolInterface
40 InstallPlatformDriverOverrideProtocol (
41 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL
*gPlatformDriverOverride
47 EFI_HANDLE
*HandleBuffer
;
50 // There will be only one platform driver override protocol in the system
51 // If there is another out there, someone is trying to install us again,
52 // Fail that scenario.
54 Status
= gBS
->LocateHandleBuffer (
56 &gEfiPlatformDriverOverrideProtocolGuid
,
62 // If there was no error, assume there is an installation and return error
64 if (!EFI_ERROR (Status
)) {
65 if (HandleBuffer
!= NULL
) {
66 FreePool (HandleBuffer
);
68 return EFI_ALREADY_STARTED
;
72 // Install platform driver override protocol
75 Status
= gBS
->InstallProtocolInterface (
77 &gEfiPlatformDriverOverrideProtocolGuid
,
79 gPlatformDriverOverride
86 Free all the mapping database memory resource and initialize the mapping list entry
88 @param MappingDataBase Mapping database list entry pointer
90 @retval EFI_INVALID_PARAMETER mapping database list entry is NULL
91 @retval EFI_SUCCESS Free success
97 IN OUT LIST_ENTRY
*MappingDataBase
100 LIST_ENTRY
*OverrideItemListIndex
;
101 LIST_ENTRY
*ImageInfoListIndex
;
102 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
103 DRIVER_IMAGE_INFO
*DriverImageInfo
;
105 if (MappingDataBase
== NULL
) {
106 return EFI_INVALID_PARAMETER
;
109 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
110 while (OverrideItemListIndex
!= MappingDataBase
){
111 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
113 // Free PLATFORM_OVERRIDE_ITEM.ControllerDevicePath[]
115 if (OverrideItem
->ControllerDevicePath
!= NULL
){
116 FreePool(OverrideItem
->ControllerDevicePath
);
119 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
120 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
122 // Free all DRIVER_IMAGE_INFO.DriverImagePath[]
124 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
125 if (DriverImageInfo
->DriverImagePath
!= NULL
) {
126 FreePool(DriverImageInfo
->DriverImagePath
);
129 // Free DRIVER_IMAGE_INFO itself
131 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
132 RemoveEntryList (&DriverImageInfo
->Link
);
133 FreePool (DriverImageInfo
);
136 // Free PLATFORM_OVERRIDE_ITEM itself
138 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
139 RemoveEntryList (&OverrideItem
->Link
);
140 FreePool (OverrideItem
);
143 InitializeListHead (MappingDataBase
);
149 Read the environment variable(s) that contain the override mappings from Controller Device Path to
150 a set of Driver Device Paths, and create the mapping database in memory with those variable info.
153 // NotEnd indicate whether the variable is the last one, and has no subsequent variable need to load.
154 // Each variable has MaximumVariableSize limitation, so we maybe need multi variables to store
155 // large mapping infos.
156 // The variable(s) name rule is PlatDriOver, PlatDriOver1, PlatDriOver2, ....
160 // The entry which contains the mapping that Controller Device Path to a set of Driver Device Paths
161 // There are often multi mapping entries in a variable.
163 UINT32 SIGNATURE; //EFI_SIGNATURE_32('p','d','o','i')
165 EFI_DEVICE_PATH_PROTOCOL ControllerDevicePath[];
166 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
167 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
168 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
172 EFI_DEVICE_PATH_PROTOCOL ControllerDevicePath[];
173 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
174 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
175 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
178 typedef struct _PLATFORM_OVERRIDE_ITEM{
179 UINTN Signature; //EFI_SIGNATURE_32('p','d','o','i')
181 UINT32 DriverInfoNum;
182 EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath;
183 LIST_ENTRY DriverInfoList; //DRIVER_IMAGE_INFO List
184 } PLATFORM_OVERRIDE_ITEM;
185 typedef struct _DRIVER_IMAGE_INFO{
186 UINTN Signature; //EFI_SIGNATURE_32('p','d','i','i')
188 EFI_HANDLE ImageHandle;
189 EFI_DEVICE_PATH_PROTOCOL *DriverImagePath;
194 @param MappingDataBase Mapping database list entry pointer
196 @retval EFI_INVALID_PARAMETER MappingDataBase pointer is null
197 @retval EFI_NOT_FOUND Cannot find the 'PlatDriOver' NV variable
198 @retval EFI_VOLUME_CORRUPTED The found NV variable is corrupted
199 @retval EFI_SUCCESS Create the mapping database in memory
205 InitOverridesMapping (
206 OUT LIST_ENTRY
*MappingDataBase
210 VOID
*VariableBuffer
;
211 UINT8
*VariableIndex
;
213 CHAR16 OverrideVariableName
[40];
216 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
217 DRIVER_IMAGE_INFO
*DriverImageInfo
;
220 EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
;
221 EFI_DEVICE_PATH_PROTOCOL
*DriverDevicePath
;
224 if (MappingDataBase
== NULL
) {
225 return EFI_INVALID_PARAMETER
;
231 // Check the environment variable(s) that contain the override mappings .
233 VariableBuffer
= GetVariableAndSize (L
"PlatDriOver", &gEfiOverrideVariableGuid
, &BufferSize
);
234 ASSERT ((UINTN
) VariableBuffer
% sizeof(UINTN
) == 0);
236 if (VariableBuffer
== NULL
) {
237 return EFI_NOT_FOUND
;
241 VariableIndex
= VariableBuffer
;
242 NotEnd
= *(UINT32
*) VariableIndex
;
243 VariableIndex
= VariableIndex
+ sizeof (UINT32
);
244 while (VariableIndex
< ((UINT8
*)VariableBuffer
+ BufferSize
)) {
245 OverrideItem
= AllocateZeroPool (sizeof (PLATFORM_OVERRIDE_ITEM
));
246 ASSERT (OverrideItem
!= NULL
);
247 OverrideItem
->Signature
= PLATFORM_OVERRIDE_ITEM_SIGNATURE
;
248 InitializeListHead (&OverrideItem
->DriverInfoList
);
252 Signature
= *(UINT32
*) VariableIndex
;
253 VariableIndex
= VariableIndex
+ sizeof (UINT32
);
254 if (Signature
!= PLATFORM_OVERRIDE_ITEM_SIGNATURE
) {
255 FreePool (OverrideItem
);
262 DriverNumber
= *(UINT32
*) VariableIndex
;
263 OverrideItem
->DriverInfoNum
= DriverNumber
;
264 VariableIndex
= VariableIndex
+ sizeof (UINT32
);
266 // Get ControllerDevicePath[]
268 ControllerDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) VariableIndex
;
269 OverrideItem
->ControllerDevicePath
= DuplicateDevicePath (ControllerDevicePath
);
270 VariableIndex
= VariableIndex
+ GetDevicePathSize (ControllerDevicePath
);
272 // Align the VariableIndex since the controller device path may not be aligned, refer to the SaveOverridesMapping()
274 VariableIndex
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
277 // Get all DriverDevicePath[]
279 for (Index
= 0; Index
< DriverNumber
; Index
++) {
280 DriverImageInfo
= AllocateZeroPool (sizeof (DRIVER_IMAGE_INFO
));
281 ASSERT (DriverImageInfo
!= NULL
);
282 DriverImageInfo
->Signature
= DRIVER_IMAGE_INFO_SIGNATURE
;
284 DriverDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) VariableIndex
;
285 DriverImageInfo
->DriverImagePath
= DuplicateDevicePath (DriverDevicePath
);
286 VariableIndex
= VariableIndex
+ GetDevicePathSize (DriverDevicePath
);
288 // Align the VariableIndex since the driver image device path may not be aligned, refer to the SaveOverridesMapping()
290 VariableIndex
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
292 InsertTailList (&OverrideItem
->DriverInfoList
, &DriverImageInfo
->Link
);
294 InsertTailList (MappingDataBase
, &OverrideItem
->Link
);
297 FreePool (VariableBuffer
);
299 FreeMappingDatabase (MappingDataBase
);
300 return EFI_VOLUME_CORRUPTED
;
304 // If has other variable(PlatDriOver1, PlatDriOver2, PlatDriOver3.....), get them.
305 // NotEnd indicate whether current variable is the end variable.
308 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", VariableNum
);
309 VariableBuffer
= GetVariableAndSize (OverrideVariableName
, &gEfiOverrideVariableGuid
, &BufferSize
);
310 ASSERT ((UINTN
) VariableBuffer
% sizeof(UINTN
) == 0);
312 if (VariableBuffer
== NULL
) {
313 FreeMappingDatabase (MappingDataBase
);
314 return EFI_VOLUME_CORRUPTED
;
318 } while (NotEnd
!= 0);
325 Calculate the needed size in NV variable for recording a specific PLATFORM_OVERRIDE_ITEM info
327 @param OverrideItemListIndex a list entry point to a specific
328 PLATFORM_OVERRIDE_ITEM
330 @return The needed size number
335 GetOneItemNeededSize (
336 IN LIST_ENTRY
*OverrideItemListIndex
340 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
341 LIST_ENTRY
*ImageInfoListIndex
;
342 DRIVER_IMAGE_INFO
*DriverImageInfo
;
346 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
347 NeededSize
+= sizeof (UINT32
); //UINT32 SIGNATURE;
348 NeededSize
+= sizeof (UINT32
); //UINT32 DriverNum;
349 NeededSize
+= GetDevicePathSize (OverrideItem
->ControllerDevicePath
); // ControllerDevicePath
351 // Align the controller device path
353 NeededSize
+= ((sizeof(UINT32
) - ((UINTN
) GetDevicePathSize (OverrideItem
->ControllerDevicePath
))) \
354 & (sizeof(UINT32
) - 1));
355 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
356 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
357 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
358 NeededSize
+= GetDevicePathSize (DriverImageInfo
->DriverImagePath
); //DriverDevicePath
360 // Align the driver image device path
362 NeededSize
+= ((sizeof(UINT32
) - ((UINTN
) GetDevicePathSize (DriverImageInfo
->DriverImagePath
))) \
363 & (sizeof(UINT32
) - 1));
364 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
373 Save the memory mapping database into NV environment variable(s)
375 @param MappingDataBase Mapping database list entry pointer
377 @retval EFI_INVALID_PARAMETER MappingDataBase pointer is null
378 @retval EFI_SUCCESS Save memory mapping database successfully
383 SaveOverridesMapping (
384 IN LIST_ENTRY
*MappingDataBase
388 VOID
*VariableBuffer
;
389 UINT8
*VariableIndex
;
391 CHAR16 OverrideVariableName
[40];
393 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
394 DRIVER_IMAGE_INFO
*DriverImageInfo
;
395 LIST_ENTRY
*OverrideItemListIndex
;
396 LIST_ENTRY
*ItemIndex
;
397 LIST_ENTRY
*ImageInfoListIndex
;
398 UINTN VariableNeededSize
;
400 UINT64 MaximumVariableStorageSize
;
401 UINT64 RemainingVariableStorageSize
;
402 UINT64 MaximumVariableSize
;
403 UINTN OneItemNeededSize
;
405 if (MappingDataBase
== NULL
) {
406 return EFI_INVALID_PARAMETER
;
409 if (MappingDataBase
->ForwardLink
== MappingDataBase
) {
410 Status
= DeleteOverridesVariables ();
415 // Get the the maximum size of an individual EFI variable in current system
417 gRT
->QueryVariableInfo (
418 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
419 &MaximumVariableStorageSize
,
420 &RemainingVariableStorageSize
,
425 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
426 while (OverrideItemListIndex
!= MappingDataBase
) {
428 // Try to find the most proper variable size which <= MaximumVariableSize, but can contain mapping info as much as possible
430 VariableNeededSize
= 0;
431 VariableNeededSize
+= sizeof (UINT32
); //BOOLEAN NotEnd;
432 ItemIndex
= OverrideItemListIndex
;
435 while (ItemIndex
!= MappingDataBase
){
436 OneItemNeededSize
= GetOneItemNeededSize (ItemIndex
);
437 if ((VariableNeededSize
+
439 sizeof (VARIABLE_HEADER
) +
440 StrSize (L
"PlatDriOver ")
441 ) >= MaximumVariableSize
447 VariableNeededSize
+= GetOneItemNeededSize (ItemIndex
);
448 ItemIndex
= ItemIndex
->ForwardLink
;
452 if (VariableNeededSize
== sizeof (UINT32
)) {
454 // If an individual EFI variable cannot contain a single Item, return error
456 return EFI_OUT_OF_RESOURCES
;
461 // VariableNeededSize is the most proper variable size, allocate variable buffer
462 // ItemIndex now points to the next PLATFORM_OVERRIDE_ITEM which is not covered by VariableNeededSize
464 VariableBuffer
= AllocateZeroPool (VariableNeededSize
);
465 ASSERT ((UINTN
) VariableBuffer
% sizeof(UINTN
) == 0);
468 // Fill the variable buffer according to MappingDataBase
471 VariableIndex
= VariableBuffer
;
472 *(UINT32
*) VariableIndex
= NotEnd
;
473 VariableIndex
+= sizeof (UINT32
); // pass NoEnd
474 SavedSize
+= sizeof (UINT32
);
476 // ItemIndex points to the next PLATFORM_OVERRIDE_ITEM which is not covered by VariableNeededSize
478 while (OverrideItemListIndex
!= ItemIndex
){
479 *(UINT32
*) VariableIndex
= PLATFORM_OVERRIDE_ITEM_SIGNATURE
;
480 VariableIndex
+= sizeof (UINT32
); // pass SIGNATURE
481 SavedSize
+= sizeof (UINT32
);
483 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
484 *(UINT32
*) VariableIndex
= OverrideItem
->DriverInfoNum
;
485 VariableIndex
+= sizeof (UINT32
); // pass DriverNum
486 SavedSize
+= sizeof (UINT32
);
488 CopyMem (VariableIndex
, OverrideItem
->ControllerDevicePath
, GetDevicePathSize (OverrideItem
->ControllerDevicePath
));
489 VariableIndex
+= GetDevicePathSize (OverrideItem
->ControllerDevicePath
); // pass ControllerDevicePath
490 SavedSize
+= GetDevicePathSize (OverrideItem
->ControllerDevicePath
);
493 // Align the VariableIndex since the controller device path may not be aligned
495 SavedSize
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
496 VariableIndex
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
498 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
499 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
500 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
501 CopyMem (VariableIndex
, DriverImageInfo
->DriverImagePath
, GetDevicePathSize (DriverImageInfo
->DriverImagePath
));
502 VariableIndex
+= GetDevicePathSize (DriverImageInfo
->DriverImagePath
); // pass DriverImageDevicePath
503 SavedSize
+= GetDevicePathSize (DriverImageInfo
->DriverImagePath
);
505 // Align the VariableIndex since the driver image device path may not be aligned
507 SavedSize
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
508 VariableIndex
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
509 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
512 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
515 ASSERT (SavedSize
== VariableNeededSize
);
518 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver");
520 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", NumIndex
);
523 Status
= gRT
->SetVariable (
524 OverrideVariableName
,
525 &gEfiOverrideVariableGuid
,
526 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
530 ASSERT (!EFI_ERROR(Status
));
533 FreePool (VariableBuffer
);
540 Get the first Binding protocol which has the specific image handle
542 @param Image Image handle
544 @return Pointer into the Binding Protocol interface
547 EFI_DRIVER_BINDING_PROTOCOL
*
549 GetBindingProtocolFromImageHandle (
550 IN EFI_HANDLE ImageHandle
,
551 OUT EFI_HANDLE
*BindingHandle
556 UINTN DriverBindingHandleCount
;
557 EFI_HANDLE
*DriverBindingHandleBuffer
;
558 EFI_DRIVER_BINDING_PROTOCOL
*DriverBindingInterface
;
560 if (BindingHandle
== NULL
|| ImageHandle
== NULL
) {
564 // Get all driver which support binding protocol in second page
566 DriverBindingHandleCount
= 0;
567 Status
= gBS
->LocateHandleBuffer (
569 &gEfiDriverBindingProtocolGuid
,
571 &DriverBindingHandleCount
,
572 &DriverBindingHandleBuffer
574 if (EFI_ERROR (Status
) || (DriverBindingHandleCount
== 0)) {
578 for (Index
= 0; Index
< DriverBindingHandleCount
; Index
++) {
579 DriverBindingInterface
=NULL
;
580 Status
= gBS
->OpenProtocol (
581 DriverBindingHandleBuffer
[Index
],
582 &gEfiDriverBindingProtocolGuid
,
583 (VOID
**) &DriverBindingInterface
,
586 EFI_OPEN_PROTOCOL_GET_PROTOCOL
588 if (EFI_ERROR (Status
)) {
592 if (DriverBindingInterface
->ImageHandle
== ImageHandle
) {
593 *BindingHandle
= DriverBindingHandleBuffer
[Index
];
594 FreePool (DriverBindingHandleBuffer
);
595 return DriverBindingInterface
;
599 FreePool (DriverBindingHandleBuffer
);
600 *BindingHandle
= NULL
;
605 return the current TPL, copied from the EDKII glue lib
619 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
620 gBS
->RestoreTPL (Tpl
);
627 Retrieves the image handle of the platform override driver for a controller in the system from the memory mapping database.
629 @param This A pointer to the
630 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL instance.
631 @param ControllerHandle The device handle of the controller to check if
632 a driver override exists.
633 @param DriverImageHandle On output, a pointer to the next driver handle.
634 Passing in a pointer to NULL, will return the
635 first driver handle for ControllerHandle.
636 @param MappingDataBase MappingDataBase - Mapping database list entry
638 @param CallerImageHandle The caller driver's image handle, for
639 UpdateFvFileDevicePath use.
641 @retval EFI_INVALID_PARAMETER The handle specified by ControllerHandle is not
642 a valid handle. Or DriverImagePath is not a
643 device path that was returned on a previous call
645 @retval EFI_NOT_FOUND A driver override for ControllerHandle was not
647 @retval EFI_UNSUPPORTED The operation is not supported.
648 @retval EFI_SUCCESS The driver override for ControllerHandle was
649 returned in DriverImagePath.
654 GetDriverFromMapping (
655 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL
* This
,
656 IN EFI_HANDLE ControllerHandle
,
657 IN OUT EFI_HANDLE
* DriverImageHandle
,
658 IN LIST_ENTRY
* MappingDataBase
,
659 IN EFI_HANDLE CallerImageHandle
663 EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
;
664 BOOLEAN ControllerFound
;
666 EFI_HANDLE
*ImageHandleBuffer
;
667 UINTN ImageHandleCount
;
669 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
670 EFI_HANDLE DriverBindingHandle
;
671 BOOLEAN FoundLastReturned
;
672 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
673 DRIVER_IMAGE_INFO
*DriverImageInfo
;
674 LIST_ENTRY
*OverrideItemListIndex
;
675 LIST_ENTRY
*ImageInfoListIndex
;
676 EFI_DEVICE_PATH_PROTOCOL
*TempDriverImagePath
;
677 EFI_HANDLE ImageHandle
;
679 EFI_DEVICE_PATH_PROTOCOL
*LoadedImageDevicePath
;
680 EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL
*BusSpecificDriverOverride
;
681 UINTN DevicePathSize
;
684 // Check that ControllerHandle is a valid handle
686 if (ControllerHandle
== NULL
) {
687 return EFI_INVALID_PARAMETER
;
690 Status
= gBS
->HandleProtocol (
692 &gEfiDevicePathProtocolGuid
,
693 (VOID
**) &ControllerDevicePath
695 if (EFI_ERROR (Status
) || ControllerDevicePath
== NULL
) {
696 return EFI_INVALID_PARAMETER
;
700 // Search ControllerDevicePath in MappingDataBase
703 ControllerFound
= FALSE
;
704 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
705 while (OverrideItemListIndex
!= MappingDataBase
){
706 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
707 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
708 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
710 ControllerDevicePath
,
711 OverrideItem
->ControllerDevicePath
,
712 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
715 ControllerFound
= TRUE
;
720 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
723 if (!ControllerFound
) {
724 return EFI_NOT_FOUND
;
727 // Passing in a pointer to NULL, will return the first driver device path for ControllerHandle.
728 // Check whether the driverImagePath is not a device path that was returned on a previous call to GetDriverPath().
730 if (*DriverImageHandle
!= NULL
) {
731 if (*DriverImageHandle
!= OverrideItem
->LastReturnedImageHandle
) {
732 return EFI_INVALID_PARAMETER
;
736 // The GetDriverPath() maybe called recursively, because it use ConnectDevicePath() internally,
737 // so should check whether there is a dead loop.
738 // Here use a controller device path stack to record all processed controller device path during a GetDriverPath() call,
739 // and check the controller device path whether appear again during the GetDriverPath() call.
741 if (CheckExistInStack(OverrideItem
->ControllerDevicePath
)) {
743 // There is a dependecy dead loop if the ControllerDevicePath appear in stack twice
745 return EFI_UNSUPPORTED
;
747 PushDevPathStack (OverrideItem
->ControllerDevicePath
);
750 // Check every override driver, try to load and start them
752 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
753 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
754 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
755 if (DriverImageInfo
->ImageHandle
== NULL
) {
757 // Skip if the image is unloadable or unstartable
759 if ((!DriverImageInfo
->UnLoadable
) && ((!DriverImageInfo
->UnStartable
))) {
760 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
762 // If the image device path contain a FV node, check the Fv file device path is valid. If it is invalid, try to return the valid device path.
763 // FV address maybe changes for memory layout adjust from time to time, use this funciton could promise the Fv file device path is right.
765 Status
= UpdateFvFileDevicePath (&TempDriverImagePath
, NULL
, CallerImageHandle
);
766 if (!EFI_ERROR (Status
)) {
767 FreePool(DriverImageInfo
->DriverImagePath
);
768 DriverImageInfo
->DriverImagePath
= TempDriverImagePath
;
771 // Get all Loaded Image protocol to check whether the driver image has been loaded and started
774 ImageHandleCount
= 0;
775 Status
= gBS
->LocateHandleBuffer (
777 &gEfiLoadedImageProtocolGuid
,
782 if (EFI_ERROR (Status
) || (ImageHandleCount
== 0)) {
783 return EFI_NOT_FOUND
;
786 for(Index
= 0; Index
< ImageHandleCount
; Index
++) {
788 // Get the EFI Loaded Image Device Path Protocol
790 LoadedImageDevicePath
= NULL
;
791 Status
= gBS
->HandleProtocol (
792 ImageHandleBuffer
[Index
],
793 &gEfiLoadedImageDevicePathProtocolGuid
,
794 (VOID
**) &LoadedImageDevicePath
796 if (EFI_ERROR (Status
)) {
798 // Maybe Not all EFI Loaded Image Device Path Protocol existed.
803 DevicePathSize
= GetDevicePathSize (DriverImageInfo
->DriverImagePath
);
804 if (DevicePathSize
== GetDevicePathSize (LoadedImageDevicePath
)) {
806 DriverImageInfo
->DriverImagePath
,
807 LoadedImageDevicePath
,
808 GetDevicePathSize (LoadedImageDevicePath
)
819 // Find its related driver binding protocol
820 // Driver binding handle may be different with its driver's Image handle,
822 DriverBindingHandle
= NULL
;
823 DriverBinding
= GetBindingProtocolFromImageHandle (
824 ImageHandleBuffer
[Index
],
827 ASSERT (DriverBinding
!= NULL
);
828 DriverImageInfo
->ImageHandle
= ImageHandleBuffer
[Index
];
829 } else if (GetCurrentTpl() <= TPL_CALLBACK
){
831 // The driver image has not been loaded and started, need try to load and start it now
832 // Try to connect all device in the driver image path
834 // Note: LoadImage() and StartImage() should be called under CALLBACK TPL in theory, but
835 // since many device need to be connected in CALLBACK level environment( e.g. Usb devices )
836 // and the Fat and Patition driver can endure executing in CALLBACK level in fact, so here permit
837 // to use LoadImage() and StartImage() in CALLBACK TPL.
839 Status
= ConnectDevicePath (DriverImageInfo
->DriverImagePath
);
841 // check whether it points to a PCI Option Rom image, and try to use bus override protocol to get its first option rom image driver
843 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
844 gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &TempDriverImagePath
, &Handle
);
846 // Get the Bus Specific Driver Override Protocol instance on the Controller Handle
848 Status
= gBS
->HandleProtocol(
850 &gEfiBusSpecificDriverOverrideProtocolGuid
,
851 (VOID
**) &BusSpecificDriverOverride
853 if (!EFI_ERROR (Status
) && (BusSpecificDriverOverride
!= NULL
)) {
855 Status
= BusSpecificDriverOverride
->GetDriver (
856 BusSpecificDriverOverride
,
859 if (!EFI_ERROR (Status
)) {
861 // Find its related driver binding protocol
862 // Driver binding handle may be different with its driver's Image handle
864 DriverBindingHandle
= NULL
;
865 DriverBinding
= GetBindingProtocolFromImageHandle (
869 ASSERT (DriverBinding
!= NULL
);
870 DriverImageInfo
->ImageHandle
= ImageHandle
;
874 // Skip if any device cannot be connected now, future passes through GetDriver() may be able to load that driver.
875 // Only file path media or FwVol Device Path Node remain if all device is connected
877 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
878 gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &TempDriverImagePath
, &Handle
);
879 if (((DevicePathType (TempDriverImagePath
) == MEDIA_DEVICE_PATH
) &&
880 (DevicePathSubType (TempDriverImagePath
) == MEDIA_FILEPATH_DP
)) ||
881 (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) TempDriverImagePath
) != NULL
)
884 // Try to load the driver
886 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
887 Status
= gBS
->LoadImage (
895 if (!EFI_ERROR (Status
)) {
897 // Try to start the driver
899 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
900 if (EFI_ERROR (Status
)){
901 DriverImageInfo
->UnStartable
= TRUE
;
902 DriverImageInfo
->ImageHandle
= NULL
;
905 // Find its related driver binding protocol
906 // Driver binding handle may be different with its driver's Image handle
908 DriverBindingHandle
= NULL
;
909 DriverBinding
= GetBindingProtocolFromImageHandle (
913 ASSERT (DriverBinding
!= NULL
);
914 DriverImageInfo
->ImageHandle
= ImageHandle
;
917 DriverImageInfo
->UnLoadable
= TRUE
;
918 DriverImageInfo
->ImageHandle
= NULL
;
922 FreePool (ImageHandleBuffer
);
925 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
928 // Finish try to load and start the override driver of a controller, popup the controller's device path
930 PopDevPathStack (NULL
);
933 // return the DriverImageHandle for ControllerHandle
935 FoundLastReturned
= FALSE
;
936 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
937 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
938 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
939 if (DriverImageInfo
->ImageHandle
!= NULL
) {
940 if ((*DriverImageHandle
== NULL
) || FoundLastReturned
) {
941 OverrideItem
->LastReturnedImageHandle
= DriverImageInfo
->ImageHandle
;
942 *DriverImageHandle
= DriverImageInfo
->ImageHandle
;
944 } else if (*DriverImageHandle
== DriverImageInfo
->ImageHandle
){
945 FoundLastReturned
= TRUE
;
948 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
951 return EFI_NOT_FOUND
;
956 Check mapping database whether already has the mapping info which
957 records the input Controller to input DriverImage.
958 If has, the controller's total override driver number and input DriverImage's order number is return.
960 @param ControllerDevicePath The controller device path need to add a
961 override driver image item
962 @param DriverImageDevicePath The driver image device path need to be insert
963 @param MappingDataBase Mapping database list entry pointer
964 @param DriverInfoNum the controller's total override driver number
965 @param DriverImageNO The inserted order number
967 @return EFI_INVALID_PARAMETER
968 @return EFI_NOT_FOUND
975 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
976 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
977 IN LIST_ENTRY
* MappingDataBase
,
978 OUT UINT32
*DriverInfoNum
,
979 OUT UINT32
*DriverImageNO
982 LIST_ENTRY
*OverrideItemListIndex
;
983 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
984 LIST_ENTRY
*ImageInfoListIndex
;
985 DRIVER_IMAGE_INFO
*DriverImageInfo
;
988 UINTN DevicePathSize
;
991 // Check that ControllerHandle is a valid handle
993 if (ControllerDevicePath
== NULL
) {
994 return EFI_INVALID_PARAMETER
;
996 if (MappingDataBase
== NULL
) {
997 return EFI_INVALID_PARAMETER
;
1000 // Search ControllerDevicePath in MappingDataBase
1003 OverrideItem
= NULL
;
1004 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
1005 while (OverrideItemListIndex
!= MappingDataBase
){
1006 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1007 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1008 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1010 ControllerDevicePath
,
1011 OverrideItem
->ControllerDevicePath
,
1012 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
1019 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
1023 return EFI_NOT_FOUND
;
1026 ASSERT (OverrideItem
->DriverInfoNum
!= 0);
1027 if (DriverInfoNum
!= NULL
) {
1028 *DriverInfoNum
= OverrideItem
->DriverInfoNum
;
1032 if (DriverImageDevicePath
== NULL
) {
1036 // return the DriverImageHandle for ControllerHandle
1040 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
1041 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
1042 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1044 DevicePathSize
= GetDevicePathSize (DriverImageDevicePath
);
1045 if (DevicePathSize
== GetDevicePathSize (DriverImageInfo
->DriverImagePath
)) {
1047 DriverImageDevicePath
,
1048 DriverImageInfo
->DriverImagePath
,
1049 GetDevicePathSize (DriverImageInfo
->DriverImagePath
)
1056 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
1060 return EFI_NOT_FOUND
;
1062 if (DriverImageNO
!= NULL
) {
1063 *DriverImageNO
= ImageNO
;
1072 Insert a driver image as a controller's override driver into the mapping database.
1073 The driver image's order number is indicated by DriverImageNO.
1075 @param ControllerDevicePath The controller device path need to add a
1076 override driver image item
1077 @param DriverImageDevicePath The driver image device path need to be insert
1078 @param MappingDataBase Mapping database list entry pointer
1079 @param DriverImageNO The inserted order number
1081 @return EFI_INVALID_PARAMETER
1082 @return EFI_ALREADY_STARTED
1089 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
1090 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
1091 IN LIST_ENTRY
*MappingDataBase
,
1092 IN UINT32 DriverImageNO
1096 LIST_ENTRY
*OverrideItemListIndex
;
1097 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1098 LIST_ENTRY
*ImageInfoListIndex
;
1099 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1102 UINTN DevicePathSize
;
1105 // Check that ControllerHandle is a valid handle
1107 if (ControllerDevicePath
== NULL
) {
1108 return EFI_INVALID_PARAMETER
;
1110 if (DriverImageDevicePath
== NULL
) {
1111 return EFI_INVALID_PARAMETER
;
1113 if (MappingDataBase
== NULL
) {
1114 return EFI_INVALID_PARAMETER
;
1117 Status
= CheckMapping (
1118 ControllerDevicePath
,
1119 DriverImageDevicePath
,
1124 if (Status
== EFI_SUCCESS
) {
1125 return EFI_ALREADY_STARTED
;
1129 // Search the input ControllerDevicePath in MappingDataBase
1132 OverrideItem
= NULL
;
1133 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
1134 while (OverrideItemListIndex
!= MappingDataBase
){
1135 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1136 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1137 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1139 ControllerDevicePath
,
1140 OverrideItem
->ControllerDevicePath
,
1141 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
1148 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
1151 // If cannot find, this is a new controller item
1152 // Add the Controller related PLATFORM_OVERRIDE_ITEM structrue in mapping data base
1155 OverrideItem
= AllocateZeroPool (sizeof (PLATFORM_OVERRIDE_ITEM
));
1156 ASSERT (OverrideItem
!= NULL
);
1157 OverrideItem
->Signature
= PLATFORM_OVERRIDE_ITEM_SIGNATURE
;
1158 OverrideItem
->ControllerDevicePath
= DuplicateDevicePath (ControllerDevicePath
);
1159 InitializeListHead (&OverrideItem
->DriverInfoList
);
1160 InsertTailList (MappingDataBase
, &OverrideItem
->Link
);
1164 // Prepare the driver image related DRIVER_IMAGE_INFO structure.
1166 DriverImageInfo
= AllocateZeroPool (sizeof (DRIVER_IMAGE_INFO
));
1167 ASSERT (DriverImageInfo
!= NULL
);
1168 DriverImageInfo
->Signature
= DRIVER_IMAGE_INFO_SIGNATURE
;
1169 DriverImageInfo
->DriverImagePath
= DuplicateDevicePath (DriverImageDevicePath
);
1171 // Find the driver image wantted order location
1175 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
1176 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
1177 if (ImageNO
== (DriverImageNO
- 1)) {
1179 // find the wantted order location, insert it
1181 InsertTailList (ImageInfoListIndex
, &DriverImageInfo
->Link
);
1182 OverrideItem
->DriverInfoNum
++;
1186 //DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
1188 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
1193 // if not find the wantted order location, add it as last item of the controller mapping item
1195 InsertTailList (&OverrideItem
->DriverInfoList
, &DriverImageInfo
->Link
);
1196 OverrideItem
->DriverInfoNum
++;
1204 Delete a controller's override driver from the mapping database.
1206 @param ControllerDevicePath The controller device path need to add a
1207 override driver image item
1208 @param DriverImageDevicePath The driver image device path need to be insert
1209 @param MappingDataBase Mapping database list entry pointer
1210 @param DriverImageNO The inserted order number
1212 @return EFI_INVALID_PARAMETER
1213 @return EFI_NOT_FOUND
1220 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
1221 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
1222 IN LIST_ENTRY
*MappingDataBase
1226 LIST_ENTRY
*OverrideItemListIndex
;
1227 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1228 LIST_ENTRY
*ImageInfoListIndex
;
1229 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1231 UINTN DevicePathSize
;
1234 // Check that ControllerHandle is a valid handle
1236 if (ControllerDevicePath
== NULL
) {
1237 return EFI_INVALID_PARAMETER
;
1240 if (MappingDataBase
== NULL
) {
1241 return EFI_INVALID_PARAMETER
;
1244 Status
= CheckMapping (
1245 ControllerDevicePath
,
1246 DriverImageDevicePath
,
1251 if (Status
== EFI_NOT_FOUND
) {
1252 return EFI_NOT_FOUND
;
1256 // Search ControllerDevicePath in MappingDataBase
1259 OverrideItem
= NULL
;
1260 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
1261 while (OverrideItemListIndex
!= MappingDataBase
){
1262 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1263 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1264 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1266 ControllerDevicePath
,
1267 OverrideItem
->ControllerDevicePath
,
1268 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
1275 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
1279 ASSERT (OverrideItem
->DriverInfoNum
!= 0);
1284 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
1285 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
1286 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1287 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
1288 if (DriverImageDevicePath
!= NULL
) {
1289 DevicePathSize
= GetDevicePathSize (DriverImageDevicePath
);
1290 if (DevicePathSize
== GetDevicePathSize (DriverImageInfo
->DriverImagePath
)) {
1292 DriverImageDevicePath
,
1293 DriverImageInfo
->DriverImagePath
,
1294 GetDevicePathSize (DriverImageInfo
->DriverImagePath
)
1298 FreePool(DriverImageInfo
->DriverImagePath
);
1299 RemoveEntryList (&DriverImageInfo
->Link
);
1300 OverrideItem
->DriverInfoNum
--;
1306 FreePool(DriverImageInfo
->DriverImagePath
);
1307 RemoveEntryList (&DriverImageInfo
->Link
);
1308 OverrideItem
->DriverInfoNum
--;
1312 if (DriverImageDevicePath
== NULL
) {
1313 ASSERT (OverrideItem
->DriverInfoNum
== 0);
1316 if (OverrideItem
->DriverInfoNum
== 0) {
1317 FreePool(OverrideItem
->ControllerDevicePath
);
1318 RemoveEntryList (&OverrideItem
->Link
);
1319 FreePool (OverrideItem
);
1323 return EFI_NOT_FOUND
;
1331 Deletes all environment variable(s) that contain the override mappings from Controller Device Path to
1332 a set of Driver Device Paths.
1341 DeleteOverridesVariables (
1346 VOID
*VariableBuffer
;
1350 CHAR16 OverrideVariableName
[40];
1353 // Get environment variable(s) number
1356 VariableBuffer
= GetVariableAndSize (L
"PlatDriOver", &gEfiOverrideVariableGuid
, &BufferSize
);
1358 if (VariableBuffer
== NULL
) {
1359 return EFI_NOT_FOUND
;
1362 // Check NotEnd to get all PlatDriOverX variable(s)
1364 while (*(UINT32
*)VariableBuffer
) {
1365 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", VariableNum
);
1366 VariableBuffer
= GetVariableAndSize (OverrideVariableName
, &gEfiOverrideVariableGuid
, &BufferSize
);
1368 ASSERT (VariableBuffer
!= NULL
);
1371 Status
= gRT
->SetVariable (
1373 &gEfiOverrideVariableGuid
,
1374 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1378 ASSERT (!EFI_ERROR (Status
));
1379 for (Index
= 1; Index
< VariableNum
; Index
++) {
1380 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", Index
);
1381 Status
= gRT
->SetVariable (
1382 OverrideVariableName
,
1383 &gEfiOverrideVariableGuid
,
1384 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1388 ASSERT (!EFI_ERROR (Status
));
1395 Push a controller device path into a globle device path list
1397 @param ControllerDevicePath The controller device path need to push into
1406 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1409 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
1411 DevicePathStackItem
= AllocateZeroPool (sizeof (DEVICE_PATH_STACK_ITEM
));
1412 ASSERT (DevicePathStackItem
!= NULL
);
1413 DevicePathStackItem
->Signature
= DEVICE_PATH_STACK_ITEM_SIGNATURE
;
1414 DevicePathStackItem
->DevicePath
= DuplicateDevicePath (DevicePath
);
1415 InsertTailList (&mDevicePathStack
, &DevicePathStackItem
->Link
);
1421 Pop a controller device path from a globle device path list
1423 @param ControllerDevicePath The controller device path retrieved from stack
1426 @return EFI_NOT_FOUND
1432 OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1435 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
1436 LIST_ENTRY
*ItemListIndex
;
1438 ItemListIndex
= mDevicePathStack
.BackLink
;
1439 if (ItemListIndex
!= &mDevicePathStack
){
1440 DevicePathStackItem
= CR(ItemListIndex
, DEVICE_PATH_STACK_ITEM
, Link
, DEVICE_PATH_STACK_ITEM_SIGNATURE
);
1441 if (DevicePath
!= NULL
) {
1442 *DevicePath
= DuplicateDevicePath (DevicePathStackItem
->DevicePath
);
1444 FreePool (DevicePathStackItem
->DevicePath
);
1445 RemoveEntryList (&DevicePathStackItem
->Link
);
1446 FreePool (DevicePathStackItem
);
1449 return EFI_NOT_FOUND
;
1454 Check whether a controller device path is in a globle device path list
1456 @param ControllerDevicePath The controller device path need to check
1465 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1468 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
1469 LIST_ENTRY
*ItemListIndex
;
1471 UINTN DevicePathSize
;
1474 ItemListIndex
= mDevicePathStack
.BackLink
;
1475 while (ItemListIndex
!= &mDevicePathStack
){
1476 DevicePathStackItem
= CR(ItemListIndex
, DEVICE_PATH_STACK_ITEM
, Link
, DEVICE_PATH_STACK_ITEM_SIGNATURE
);
1477 DevicePathSize
= GetDevicePathSize (DevicePath
);
1478 if (DevicePathSize
== GetDevicePathSize (DevicePathStackItem
->DevicePath
)) {
1481 DevicePathStackItem
->DevicePath
,
1482 GetDevicePathSize (DevicePathStackItem
->DevicePath
)
1489 ItemListIndex
= ItemListIndex
->BackLink
;
1497 According to a file guild, check a Fv file device path is valid. If it is invalid,
1498 try to return the valid device path.
1499 FV address maybe changes for memory layout adjust from time to time, use this funciton
1500 could promise the Fv file device path is right.
1502 @param DevicePath on input, the Fv file device path need to check
1503 on output, the updated valid Fv file device path
1504 @param FileGuid the Fv file guild
1505 @param CallerImageHandle
1507 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1509 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1511 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it
1513 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1514 and return the updated device path in DevicePath
1519 UpdateFvFileDevicePath (
1520 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1521 IN EFI_GUID
*FileGuid
,
1522 IN EFI_HANDLE CallerImageHandle
1525 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1526 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1528 EFI_GUID
*GuidPoint
;
1530 UINTN FvHandleCount
;
1531 EFI_HANDLE
*FvHandleBuffer
;
1532 EFI_FV_FILETYPE Type
;
1534 EFI_FV_FILE_ATTRIBUTES Attributes
;
1535 UINT32 AuthenticationStatus
;
1537 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1538 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1539 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1540 EFI_HANDLE FoundFvHandle
;
1541 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1544 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1545 return EFI_INVALID_PARAMETER
;
1549 // Check whether the device path point to the default the input Fv file
1551 TempDevicePath
= *DevicePath
;
1552 LastDeviceNode
= TempDevicePath
;
1553 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1554 LastDeviceNode
= TempDevicePath
;
1555 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1557 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1558 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1560 if (GuidPoint
== NULL
) {
1562 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1564 return EFI_UNSUPPORTED
;
1567 if (FileGuid
!= NULL
) {
1568 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1570 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1572 return EFI_UNSUPPORTED
;
1575 FileGuid
= GuidPoint
;
1579 // Check to see if the device path contain memory map node
1581 TempDevicePath
= *DevicePath
;
1583 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1585 // Use old Device Path
1587 if (DevicePathType (TempDevicePath
) == HARDWARE_DEVICE_PATH
&&
1588 DevicePathSubType (TempDevicePath
) == HW_MEMMAP_DP
) {
1592 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1596 return EFI_UNSUPPORTED
;
1600 // Check whether the input Fv file device path is valid
1602 TempDevicePath
= *DevicePath
;
1603 FoundFvHandle
= NULL
;
1604 Status
= gBS
->LocateDevicePath (
1605 &gEfiFirmwareVolume2ProtocolGuid
,
1609 if (!EFI_ERROR (Status
)) {
1610 Status
= gBS
->HandleProtocol (
1612 &gEfiFirmwareVolume2ProtocolGuid
,
1615 if (!EFI_ERROR (Status
)) {
1617 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1619 Status
= Fv
->ReadFile (
1626 &AuthenticationStatus
1628 if (!EFI_ERROR (Status
)) {
1629 return EFI_ALREADY_STARTED
;
1635 // Look for the input wanted FV file in current FV
1636 // First, try to look for in Caller own FV. Caller and input wanted FV file usually are in the same FV
1639 FoundFvHandle
= NULL
;
1640 Status
= gBS
->HandleProtocol (
1642 &gEfiLoadedImageProtocolGuid
,
1643 (VOID
**) &LoadedImage
1645 if (!EFI_ERROR (Status
)) {
1646 Status
= gBS
->HandleProtocol (
1647 LoadedImage
->DeviceHandle
,
1648 &gEfiFirmwareVolume2ProtocolGuid
,
1651 if (!EFI_ERROR (Status
)) {
1652 Status
= Fv
->ReadFile (
1659 &AuthenticationStatus
1661 if (!EFI_ERROR (Status
)) {
1663 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1668 // Second, if fail to find, try to enumerate all FV
1671 gBS
->LocateHandleBuffer (
1673 &gEfiFirmwareVolume2ProtocolGuid
,
1678 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1679 gBS
->HandleProtocol (
1680 FvHandleBuffer
[Index
],
1681 &gEfiFirmwareVolume2ProtocolGuid
,
1685 Status
= Fv
->ReadFile (
1692 &AuthenticationStatus
1694 if (EFI_ERROR (Status
)) {
1696 // Skip if input Fv file not in the FV
1701 FoundFvHandle
= FvHandleBuffer
[Index
];
1708 // Build the shell device path
1710 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1711 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1712 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1713 *DevicePath
= NewDevicePath
;
1716 return EFI_NOT_FOUND
;
1721 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
1722 buffer, and the size of the buffer. If failure return NULL.
1724 @param Name String part of EFI variable name
1725 @param VendorGuid GUID part of EFI variable name
1726 @param VariableSize Returns the size of the EFI variable that was
1729 @return Dynamically allocated memory that contains a copy of the EFI variable.
1730 @return Caller is responsible freeing the buffer.
1731 @retval NULL Variable was not read
1736 GetVariableAndSize (
1738 IN EFI_GUID
*VendorGuid
,
1739 OUT UINTN
*VariableSize
1749 // Pass in a zero size buffer to find the required buffer size.
1752 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
1753 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1755 // Allocate the buffer to return
1757 Buffer
= AllocateZeroPool (BufferSize
);
1758 if (Buffer
== NULL
) {
1762 // Read variable into the allocated buffer.
1764 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
1765 if (EFI_ERROR (Status
)) {
1770 *VariableSize
= BufferSize
;
1776 This function will create all handles associate with every device
1777 path node. If the handle associate with one device path node can not
1778 be created success, then still give one chance to do the dispatch,
1779 which load the missing drivers if possible.
1781 @param DevicePathToConnect The device path which will be connected, it can
1782 be a multi-instance device path
1784 @retval EFI_SUCCESS All handles associate with every device path
1785 node have been created
1786 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
1787 @retval EFI_NOT_FOUND Create the handle associate with one device
1794 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
1798 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1799 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
1800 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
1801 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
1802 EFI_DEVICE_PATH_PROTOCOL
*Next
;
1804 EFI_HANDLE PreviousHandle
;
1807 if (DevicePathToConnect
== NULL
) {
1811 DevicePath
= DuplicateDevicePath (DevicePathToConnect
);
1812 CopyOfDevicePath
= DevicePath
;
1813 if (DevicePath
== NULL
) {
1814 return EFI_OUT_OF_RESOURCES
;
1819 // The outer loop handles multi instance device paths.
1820 // Only console variables contain multiple instance device paths.
1822 // After this call DevicePath points to the next Instance
1824 Instance
= GetNextDevicePathInstance (&DevicePath
, &Size
);
1826 while (!IsDevicePathEndType (Next
)) {
1827 Next
= NextDevicePathNode (Next
);
1830 SetDevicePathEndNode (Next
);
1833 // Start the real work of connect with RemainingDevicePath
1835 PreviousHandle
= NULL
;
1838 // Find the handle that best matches the Device Path. If it is only a
1839 // partial match the remaining part of the device path is returned in
1840 // RemainingDevicePath.
1842 RemainingDevicePath
= Instance
;
1843 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
1845 if (!EFI_ERROR (Status
)) {
1846 if (Handle
== PreviousHandle
) {
1848 // If no forward progress is made try invoking the Dispatcher.
1849 // A new FV may have been added to the system an new drivers
1850 // may now be found.
1851 // Status == EFI_SUCCESS means a driver was dispatched
1852 // Status == EFI_NOT_FOUND means no new drivers were dispatched
1854 Status
= gDS
->Dispatch ();
1857 if (!EFI_ERROR (Status
)) {
1858 PreviousHandle
= Handle
;
1860 // Connect all drivers that apply to Handle and RemainingDevicePath,
1861 // the Recursive flag is FALSE so only one level will be expanded.
1863 // Do not check the connect status here, if the connect controller fail,
1864 // then still give the chance to do dispatch, because partial
1865 // RemainingDevicepath may be in the new FV
1867 // 1. If the connect fail, RemainingDevicepath and handle will not
1868 // change, so next time will do the dispatch, then dispatch's status
1870 // 2. If the connect success, the RemainingDevicepath and handle will
1871 // change, then avoid the dispatch, we have chance to continue the
1874 gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
1878 // Loop until RemainingDevicePath is an empty device path
1880 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
1882 } while (DevicePath
!= NULL
);
1884 if (CopyOfDevicePath
!= NULL
) {
1885 FreePool (CopyOfDevicePath
);
1888 // All handle with DevicePath exists in the handle database