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 STATIC 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 gBS
->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
);
541 Retrieves the image handle of the platform override driver for a controller in the system from the memory mapping database.
543 @param This A pointer to the
544 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL instance.
545 @param ControllerHandle The device handle of the controller to check if
546 a driver override exists.
547 @param DriverImageHandle On output, a pointer to the next driver handle.
548 Passing in a pointer to NULL, will return the
549 first driver handle for ControllerHandle.
550 @param MappingDataBase MappingDataBase - Mapping database list entry
552 @param CallerImageHandle The caller driver's image handle, for
553 UpdateFvFileDevicePath use.
555 @retval EFI_INVALID_PARAMETER The handle specified by ControllerHandle is not
556 a valid handle. Or DriverImagePath is not a
557 device path that was returned on a previous call
559 @retval EFI_NOT_FOUND A driver override for ControllerHandle was not
561 @retval EFI_UNSUPPORTED The operation is not supported.
562 @retval EFI_SUCCESS The driver override for ControllerHandle was
563 returned in DriverImagePath.
568 GetDriverFromMapping (
569 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL
* This
,
570 IN EFI_HANDLE ControllerHandle
,
571 IN OUT EFI_HANDLE
* DriverImageHandle
,
572 IN LIST_ENTRY
* MappingDataBase
,
573 IN EFI_HANDLE CallerImageHandle
577 EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
;
578 BOOLEAN ControllerFound
;
580 EFI_HANDLE
*ImageHandleBuffer
;
581 UINTN ImageHandleCount
;
583 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
584 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
585 BOOLEAN FoundLastReturned
;
586 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
587 DRIVER_IMAGE_INFO
*DriverImageInfo
;
588 LIST_ENTRY
*OverrideItemListIndex
;
589 LIST_ENTRY
*ImageInfoListIndex
;
590 EFI_DEVICE_PATH_PROTOCOL
*TempDriverImagePath
;
591 EFI_HANDLE ImageHandle
;
593 EFI_DEVICE_PATH_PROTOCOL
*LoadedImageHandleDevicePath
;
594 EFI_DEVICE_PATH_PROTOCOL
*TatalFilePath
;
595 EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL
*BusSpecificDriverOverride
;
596 UINTN DevicePathSize
;
599 // Check that ControllerHandle is a valid handle
601 if (ControllerHandle
== NULL
) {
602 return EFI_INVALID_PARAMETER
;
605 Status
= gBS
->HandleProtocol (
607 &gEfiDevicePathProtocolGuid
,
608 (VOID
**) &ControllerDevicePath
610 if (EFI_ERROR (Status
) || ControllerDevicePath
== NULL
) {
611 return EFI_INVALID_PARAMETER
;
615 // Search ControllerDevicePath in MappingDataBase
618 ControllerFound
= FALSE
;
619 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
620 while (OverrideItemListIndex
!= MappingDataBase
){
621 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
622 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
623 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
625 ControllerDevicePath
,
626 OverrideItem
->ControllerDevicePath
,
627 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
630 ControllerFound
= TRUE
;
635 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
638 if (!ControllerFound
) {
639 return EFI_NOT_FOUND
;
642 // Passing in a pointer to NULL, will return the first driver device path for ControllerHandle.
643 // Check whether the driverImagePath is not a device path that was returned on a previous call to GetDriverPath().
645 if (*DriverImageHandle
!= NULL
) {
646 if (*DriverImageHandle
!= OverrideItem
->LastReturnedImageHandle
) {
647 return EFI_INVALID_PARAMETER
;
651 // The GetDriverPath() maybe called recursively, because it use ConnectDevicePath() internally,
652 // so should check whether there is a dead loop.
653 // Here use a controller device path stack to record all processed controller device path during a GetDriverPath() call,
654 // and check the controller device path whether appear again during the GetDriverPath() call.
656 if (CheckExistInStack(OverrideItem
->ControllerDevicePath
)) {
658 // There is a dependecy dead loop if the ControllerDevicePath appear in stack twice
660 return EFI_UNSUPPORTED
;
662 PushDevPathStack (OverrideItem
->ControllerDevicePath
);
665 // Check every override driver, try to load and start them
667 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
668 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
669 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
670 if (DriverImageInfo
->ImageHandle
== NULL
) {
672 // Skip if the image is unloadable or unstartable
674 if ((!DriverImageInfo
->UnLoadable
) && ((!DriverImageInfo
->UnStartable
))) {
675 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
677 // 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.
678 // FV address maybe changes for memory layout adjust from time to time, use this funciton could promise the Fv file device path is right.
680 Status
= UpdateFvFileDevicePath (&TempDriverImagePath
, NULL
, CallerImageHandle
);
681 if (!EFI_ERROR (Status
)) {
682 FreePool(DriverImageInfo
->DriverImagePath
);
683 DriverImageInfo
->DriverImagePath
= TempDriverImagePath
;
686 // Get all Loaded Image protocol to check whether the driver image has been loaded and started
689 ImageHandleCount
= 0;
690 Status
= gBS
->LocateHandleBuffer (
692 &gEfiLoadedImageProtocolGuid
,
697 if (EFI_ERROR (Status
) || (ImageHandleCount
== 0)) {
698 return EFI_NOT_FOUND
;
701 for(Index
= 0; Index
< ImageHandleCount
; Index
++) {
702 Status
= gBS
->HandleProtocol (
703 ImageHandleBuffer
[Index
],
704 &gEfiLoadedImageProtocolGuid
,
705 (VOID
**) &LoadedImage
707 if (EFI_ERROR (Status
)) {
712 // Get the driver image total file path
714 LoadedImageHandleDevicePath
= NULL
;
715 Status
= gBS
->HandleProtocol (
716 LoadedImage
->DeviceHandle
,
717 &gEfiDevicePathProtocolGuid
,
718 (VOID
**) &LoadedImageHandleDevicePath
720 if (EFI_ERROR (Status
)) {
722 // Maybe Not all LoadedImage->DeviceHandle has valid value. Skip the invalid image.
727 TatalFilePath
= AppendDevicePath (LoadedImageHandleDevicePath
, LoadedImage
->FilePath
);
729 DevicePathSize
= GetDevicePathSize (DriverImageInfo
->DriverImagePath
);
730 if (DevicePathSize
== GetDevicePathSize (TatalFilePath
)) {
732 DriverImageInfo
->DriverImagePath
,
734 GetDevicePathSize (TatalFilePath
)
744 Status
= gBS
->HandleProtocol (
745 ImageHandleBuffer
[Index
],
746 &gEfiDriverBindingProtocolGuid
,
747 (VOID
**) &DriverBinding
749 ASSERT (!EFI_ERROR (Status
));
750 DriverImageInfo
->ImageHandle
= ImageHandleBuffer
[Index
];
753 // The driver image has not been loaded and started, need try to load and start it now
754 // Try to connect all device in the driver image path
756 Status
= ConnectDevicePath (DriverImageInfo
->DriverImagePath
);
758 // 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
760 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
761 gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &TempDriverImagePath
, &Handle
);
763 // Get the Bus Specific Driver Override Protocol instance on the Controller Handle
765 Status
= gBS
->HandleProtocol(
767 &gEfiBusSpecificDriverOverrideProtocolGuid
,
768 (VOID
**) &BusSpecificDriverOverride
770 if (!EFI_ERROR (Status
) && (BusSpecificDriverOverride
!= NULL
)) {
772 Status
= BusSpecificDriverOverride
->GetDriver (
773 BusSpecificDriverOverride
,
776 if (!EFI_ERROR (Status
)) {
777 Status
= gBS
->HandleProtocol (
779 &gEfiDriverBindingProtocolGuid
,
780 (VOID
**) &DriverBinding
782 ASSERT (!EFI_ERROR (Status
));
783 DriverImageInfo
->ImageHandle
= ImageHandle
;
787 // Skip if any device cannot be connected now, future passes through GetDriver() may be able to load that driver.
788 // Only file path media or FwVol Device Path Node remain if all device is connected
790 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
791 gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &TempDriverImagePath
, &Handle
);
792 if (((DevicePathType (TempDriverImagePath
) == MEDIA_DEVICE_PATH
) &&
793 (DevicePathSubType (TempDriverImagePath
) == MEDIA_FILEPATH_DP
)) ||
794 (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) TempDriverImagePath
) != NULL
)
797 // Try to load the driver
799 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
800 Status
= gBS
->LoadImage (
808 if (!EFI_ERROR (Status
)) {
810 // Try to start the driver
812 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
813 if (EFI_ERROR (Status
)){
814 DriverImageInfo
->UnStartable
= TRUE
;
815 DriverImageInfo
->ImageHandle
= NULL
;
817 Status
= gBS
->HandleProtocol (
819 &gEfiDriverBindingProtocolGuid
,
820 (VOID
**) &DriverBinding
822 ASSERT (!EFI_ERROR (Status
));
823 DriverImageInfo
->ImageHandle
= ImageHandle
;
826 DriverImageInfo
->UnLoadable
= TRUE
;
827 DriverImageInfo
->ImageHandle
= NULL
;
831 FreePool (ImageHandleBuffer
);
834 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
837 // Finish try to load and start the override driver of a controller, popup the controller's device path
839 PopDevPathStack (NULL
);
842 // return the DriverImageHandle for ControllerHandle
844 FoundLastReturned
= FALSE
;
845 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
846 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
847 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
848 if (DriverImageInfo
->ImageHandle
!= NULL
) {
849 if ((*DriverImageHandle
== NULL
) || FoundLastReturned
) {
850 OverrideItem
->LastReturnedImageHandle
= DriverImageInfo
->ImageHandle
;
851 *DriverImageHandle
= DriverImageInfo
->ImageHandle
;
853 } else if (*DriverImageHandle
== DriverImageInfo
->ImageHandle
){
854 FoundLastReturned
= TRUE
;
857 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
860 return EFI_NOT_FOUND
;
865 Check mapping database whether already has the mapping info which
866 records the input Controller to input DriverImage.
867 If has, the controller's total override driver number and input DriverImage's order number is return.
869 @param ControllerDevicePath The controller device path need to add a
870 override driver image item
871 @param DriverImageDevicePath The driver image device path need to be insert
872 @param MappingDataBase Mapping database list entry pointer
873 @param DriverInfoNum the controller's total override driver number
874 @param DriverImageNO The inserted order number
876 @return EFI_INVALID_PARAMETER
877 @return EFI_NOT_FOUND
884 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
885 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
886 IN LIST_ENTRY
* MappingDataBase
,
887 OUT UINT32
*DriverInfoNum
,
888 OUT UINT32
*DriverImageNO
891 LIST_ENTRY
*OverrideItemListIndex
;
892 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
893 LIST_ENTRY
*ImageInfoListIndex
;
894 DRIVER_IMAGE_INFO
*DriverImageInfo
;
897 UINTN DevicePathSize
;
900 // Check that ControllerHandle is a valid handle
902 if (ControllerDevicePath
== NULL
) {
903 return EFI_INVALID_PARAMETER
;
905 if (MappingDataBase
== NULL
) {
906 return EFI_INVALID_PARAMETER
;
909 // Search ControllerDevicePath in MappingDataBase
913 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
914 while (OverrideItemListIndex
!= MappingDataBase
){
915 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
916 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
917 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
919 ControllerDevicePath
,
920 OverrideItem
->ControllerDevicePath
,
921 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
928 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
932 return EFI_NOT_FOUND
;
935 ASSERT (OverrideItem
->DriverInfoNum
!= 0);
936 if (DriverInfoNum
!= NULL
) {
937 *DriverInfoNum
= OverrideItem
->DriverInfoNum
;
941 if (DriverImageDevicePath
== NULL
) {
945 // return the DriverImageHandle for ControllerHandle
949 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
950 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
951 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
953 DevicePathSize
= GetDevicePathSize (DriverImageDevicePath
);
954 if (DevicePathSize
== GetDevicePathSize (DriverImageInfo
->DriverImagePath
)) {
956 DriverImageDevicePath
,
957 DriverImageInfo
->DriverImagePath
,
958 GetDevicePathSize (DriverImageInfo
->DriverImagePath
)
965 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
969 return EFI_NOT_FOUND
;
971 if (DriverImageNO
!= NULL
) {
972 *DriverImageNO
= ImageNO
;
981 Insert a driver image as a controller's override driver into the mapping database.
982 The driver image's order number is indicated by DriverImageNO.
984 @param ControllerDevicePath The controller device path need to add a
985 override driver image item
986 @param DriverImageDevicePath The driver image device path need to be insert
987 @param MappingDataBase Mapping database list entry pointer
988 @param DriverImageNO The inserted order number
990 @return EFI_INVALID_PARAMETER
991 @return EFI_ALREADY_STARTED
998 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
999 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
1000 IN LIST_ENTRY
*MappingDataBase
,
1001 IN UINT32 DriverImageNO
1005 LIST_ENTRY
*OverrideItemListIndex
;
1006 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1007 LIST_ENTRY
*ImageInfoListIndex
;
1008 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1011 UINTN DevicePathSize
;
1014 // Check that ControllerHandle is a valid handle
1016 if (ControllerDevicePath
== NULL
) {
1017 return EFI_INVALID_PARAMETER
;
1019 if (DriverImageDevicePath
== NULL
) {
1020 return EFI_INVALID_PARAMETER
;
1022 if (MappingDataBase
== NULL
) {
1023 return EFI_INVALID_PARAMETER
;
1026 Status
= CheckMapping (
1027 ControllerDevicePath
,
1028 DriverImageDevicePath
,
1033 if (Status
== EFI_SUCCESS
) {
1034 return EFI_ALREADY_STARTED
;
1038 // Search the input ControllerDevicePath in MappingDataBase
1041 OverrideItem
= NULL
;
1042 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
1043 while (OverrideItemListIndex
!= MappingDataBase
){
1044 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1045 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1046 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1048 ControllerDevicePath
,
1049 OverrideItem
->ControllerDevicePath
,
1050 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
1057 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
1060 // If cannot find, this is a new controller item
1061 // Add the Controller related PLATFORM_OVERRIDE_ITEM structrue in mapping data base
1064 OverrideItem
= AllocateZeroPool (sizeof (PLATFORM_OVERRIDE_ITEM
));
1065 ASSERT (OverrideItem
!= NULL
);
1066 OverrideItem
->Signature
= PLATFORM_OVERRIDE_ITEM_SIGNATURE
;
1067 OverrideItem
->ControllerDevicePath
= DuplicateDevicePath (ControllerDevicePath
);
1068 InitializeListHead (&OverrideItem
->DriverInfoList
);
1069 InsertTailList (MappingDataBase
, &OverrideItem
->Link
);
1073 // Prepare the driver image related DRIVER_IMAGE_INFO structure.
1075 DriverImageInfo
= AllocateZeroPool (sizeof (DRIVER_IMAGE_INFO
));
1076 ASSERT (DriverImageInfo
!= NULL
);
1077 DriverImageInfo
->Signature
= DRIVER_IMAGE_INFO_SIGNATURE
;
1078 DriverImageInfo
->DriverImagePath
= DuplicateDevicePath (DriverImageDevicePath
);
1080 // Find the driver image wantted order location
1084 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
1085 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
1086 if (ImageNO
== (DriverImageNO
- 1)) {
1088 // find the wantted order location, insert it
1090 InsertTailList (ImageInfoListIndex
, &DriverImageInfo
->Link
);
1091 OverrideItem
->DriverInfoNum
++;
1095 //DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
1097 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
1102 // if not find the wantted order location, add it as last item of the controller mapping item
1104 InsertTailList (&OverrideItem
->DriverInfoList
, &DriverImageInfo
->Link
);
1105 OverrideItem
->DriverInfoNum
++;
1113 Delete a controller's override driver from the mapping database.
1115 @param ControllerDevicePath The controller device path need to add a
1116 override driver image item
1117 @param DriverImageDevicePath The driver image device path need to be insert
1118 @param MappingDataBase Mapping database list entry pointer
1119 @param DriverImageNO The inserted order number
1121 @return EFI_INVALID_PARAMETER
1122 @return EFI_NOT_FOUND
1129 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
1130 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
1131 IN LIST_ENTRY
*MappingDataBase
1135 LIST_ENTRY
*OverrideItemListIndex
;
1136 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1137 LIST_ENTRY
*ImageInfoListIndex
;
1138 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1140 UINTN DevicePathSize
;
1143 // Check that ControllerHandle is a valid handle
1145 if (ControllerDevicePath
== NULL
) {
1146 return EFI_INVALID_PARAMETER
;
1149 if (MappingDataBase
== NULL
) {
1150 return EFI_INVALID_PARAMETER
;
1153 Status
= CheckMapping (
1154 ControllerDevicePath
,
1155 DriverImageDevicePath
,
1160 if (Status
== EFI_NOT_FOUND
) {
1161 return EFI_NOT_FOUND
;
1165 // Search ControllerDevicePath in MappingDataBase
1168 OverrideItem
= NULL
;
1169 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
1170 while (OverrideItemListIndex
!= MappingDataBase
){
1171 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1172 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1173 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1175 ControllerDevicePath
,
1176 OverrideItem
->ControllerDevicePath
,
1177 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
1184 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
1188 ASSERT (OverrideItem
->DriverInfoNum
!= 0);
1193 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
1194 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
1195 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1196 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
1197 if (DriverImageDevicePath
!= NULL
) {
1198 DevicePathSize
= GetDevicePathSize (DriverImageDevicePath
);
1199 if (DevicePathSize
== GetDevicePathSize (DriverImageInfo
->DriverImagePath
)) {
1201 DriverImageDevicePath
,
1202 DriverImageInfo
->DriverImagePath
,
1203 GetDevicePathSize (DriverImageInfo
->DriverImagePath
)
1207 FreePool(DriverImageInfo
->DriverImagePath
);
1208 RemoveEntryList (&DriverImageInfo
->Link
);
1209 OverrideItem
->DriverInfoNum
--;
1215 FreePool(DriverImageInfo
->DriverImagePath
);
1216 RemoveEntryList (&DriverImageInfo
->Link
);
1217 OverrideItem
->DriverInfoNum
--;
1221 if (DriverImageDevicePath
== NULL
) {
1222 ASSERT (OverrideItem
->DriverInfoNum
== 0);
1225 if (OverrideItem
->DriverInfoNum
== 0) {
1226 FreePool(OverrideItem
->ControllerDevicePath
);
1227 RemoveEntryList (&OverrideItem
->Link
);
1228 FreePool (OverrideItem
);
1232 return EFI_NOT_FOUND
;
1240 Deletes all environment variable(s) that contain the override mappings from Controller Device Path to
1241 a set of Driver Device Paths.
1250 DeleteOverridesVariables (
1255 VOID
*VariableBuffer
;
1259 CHAR16 OverrideVariableName
[40];
1262 // Get environment variable(s) number
1265 VariableBuffer
= GetVariableAndSize (L
"PlatDriOver", &gEfiOverrideVariableGuid
, &BufferSize
);
1267 if (VariableBuffer
== NULL
) {
1268 return EFI_NOT_FOUND
;
1271 // Check NotEnd to get all PlatDriOverX variable(s)
1273 while (*(UINT32
*)VariableBuffer
) {
1274 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", VariableNum
);
1275 VariableBuffer
= GetVariableAndSize (OverrideVariableName
, &gEfiOverrideVariableGuid
, &BufferSize
);
1277 ASSERT (VariableBuffer
!= NULL
);
1280 Status
= gRT
->SetVariable (
1282 &gEfiOverrideVariableGuid
,
1283 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1287 ASSERT (!EFI_ERROR (Status
));
1288 for (Index
= 1; Index
< VariableNum
; Index
++) {
1289 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", Index
);
1290 Status
= gRT
->SetVariable (
1291 OverrideVariableName
,
1292 &gEfiOverrideVariableGuid
,
1293 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1297 ASSERT (!EFI_ERROR (Status
));
1304 Push a controller device path into a globle device path list
1306 @param ControllerDevicePath The controller device path need to push into
1315 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1318 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
1320 DevicePathStackItem
= AllocateZeroPool (sizeof (DEVICE_PATH_STACK_ITEM
));
1321 ASSERT (DevicePathStackItem
!= NULL
);
1322 DevicePathStackItem
->Signature
= DEVICE_PATH_STACK_ITEM_SIGNATURE
;
1323 DevicePathStackItem
->DevicePath
= DuplicateDevicePath (DevicePath
);
1324 InsertTailList (&mDevicePathStack
, &DevicePathStackItem
->Link
);
1330 Pop a controller device path from a globle device path list
1332 @param ControllerDevicePath The controller device path retrieved from stack
1335 @return EFI_NOT_FOUND
1341 OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1344 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
1345 LIST_ENTRY
*ItemListIndex
;
1347 ItemListIndex
= mDevicePathStack
.BackLink
;
1348 if (ItemListIndex
!= &mDevicePathStack
){
1349 DevicePathStackItem
= CR(ItemListIndex
, DEVICE_PATH_STACK_ITEM
, Link
, DEVICE_PATH_STACK_ITEM_SIGNATURE
);
1350 if (DevicePath
!= NULL
) {
1351 *DevicePath
= DuplicateDevicePath (DevicePathStackItem
->DevicePath
);
1353 FreePool (DevicePathStackItem
->DevicePath
);
1354 RemoveEntryList (&DevicePathStackItem
->Link
);
1355 FreePool (DevicePathStackItem
);
1358 return EFI_NOT_FOUND
;
1363 Check whether a controller device path is in a globle device path list
1365 @param ControllerDevicePath The controller device path need to check
1374 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1377 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
1378 LIST_ENTRY
*ItemListIndex
;
1380 UINTN DevicePathSize
;
1383 ItemListIndex
= mDevicePathStack
.BackLink
;
1384 while (ItemListIndex
!= &mDevicePathStack
){
1385 DevicePathStackItem
= CR(ItemListIndex
, DEVICE_PATH_STACK_ITEM
, Link
, DEVICE_PATH_STACK_ITEM_SIGNATURE
);
1386 DevicePathSize
= GetDevicePathSize (DevicePath
);
1387 if (DevicePathSize
== GetDevicePathSize (DevicePathStackItem
->DevicePath
)) {
1390 DevicePathStackItem
->DevicePath
,
1391 GetDevicePathSize (DevicePathStackItem
->DevicePath
)
1398 ItemListIndex
= ItemListIndex
->BackLink
;
1406 According to a file guild, check a Fv file device path is valid. If it is invalid,
1407 try to return the valid device path.
1408 FV address maybe changes for memory layout adjust from time to time, use this funciton
1409 could promise the Fv file device path is right.
1411 @param DevicePath on input, the Fv file device path need to check
1412 on output, the updated valid Fv file device path
1413 @param FileGuid the Fv file guild
1414 @param CallerImageHandle
1416 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1418 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1420 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it
1422 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1423 and return the updated device path in DevicePath
1428 UpdateFvFileDevicePath (
1429 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1430 IN EFI_GUID
*FileGuid
,
1431 IN EFI_HANDLE CallerImageHandle
1434 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1435 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1437 EFI_GUID
*GuidPoint
;
1439 UINTN FvHandleCount
;
1440 EFI_HANDLE
*FvHandleBuffer
;
1441 EFI_FV_FILETYPE Type
;
1443 EFI_FV_FILE_ATTRIBUTES Attributes
;
1444 UINT32 AuthenticationStatus
;
1446 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1447 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1448 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1449 EFI_HANDLE FoundFvHandle
;
1450 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1453 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1454 return EFI_INVALID_PARAMETER
;
1458 // Check whether the device path point to the default the input Fv file
1460 TempDevicePath
= *DevicePath
;
1461 LastDeviceNode
= TempDevicePath
;
1462 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1463 LastDeviceNode
= TempDevicePath
;
1464 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1466 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1467 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1469 if (GuidPoint
== NULL
) {
1471 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1473 return EFI_UNSUPPORTED
;
1476 if (FileGuid
!= NULL
) {
1477 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1479 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1481 return EFI_UNSUPPORTED
;
1484 FileGuid
= GuidPoint
;
1488 // Check to see if the device path contain memory map node
1490 TempDevicePath
= *DevicePath
;
1492 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1494 // Use old Device Path
1496 if (DevicePathType (TempDevicePath
) == HARDWARE_DEVICE_PATH
&&
1497 DevicePathSubType (TempDevicePath
) == HW_MEMMAP_DP
) {
1501 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1505 return EFI_UNSUPPORTED
;
1509 // Check whether the input Fv file device path is valid
1511 TempDevicePath
= *DevicePath
;
1512 FoundFvHandle
= NULL
;
1513 Status
= gBS
->LocateDevicePath (
1514 &gEfiFirmwareVolume2ProtocolGuid
,
1518 if (!EFI_ERROR (Status
)) {
1519 Status
= gBS
->HandleProtocol (
1521 &gEfiFirmwareVolume2ProtocolGuid
,
1524 if (!EFI_ERROR (Status
)) {
1526 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1528 Status
= Fv
->ReadFile (
1535 &AuthenticationStatus
1537 if (!EFI_ERROR (Status
)) {
1538 return EFI_ALREADY_STARTED
;
1544 // Look for the input wanted FV file in current FV
1545 // First, try to look for in Caller own FV. Caller and input wanted FV file usually are in the same FV
1548 FoundFvHandle
= NULL
;
1549 Status
= gBS
->HandleProtocol (
1551 &gEfiLoadedImageProtocolGuid
,
1552 (VOID
**) &LoadedImage
1554 if (!EFI_ERROR (Status
)) {
1555 Status
= gBS
->HandleProtocol (
1556 LoadedImage
->DeviceHandle
,
1557 &gEfiFirmwareVolume2ProtocolGuid
,
1560 if (!EFI_ERROR (Status
)) {
1561 Status
= Fv
->ReadFile (
1568 &AuthenticationStatus
1570 if (!EFI_ERROR (Status
)) {
1572 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1577 // Second, if fail to find, try to enumerate all FV
1580 gBS
->LocateHandleBuffer (
1582 &gEfiFirmwareVolume2ProtocolGuid
,
1587 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1588 gBS
->HandleProtocol (
1589 FvHandleBuffer
[Index
],
1590 &gEfiFirmwareVolume2ProtocolGuid
,
1594 Status
= Fv
->ReadFile (
1601 &AuthenticationStatus
1603 if (EFI_ERROR (Status
)) {
1605 // Skip if input Fv file not in the FV
1610 FoundFvHandle
= FvHandleBuffer
[Index
];
1617 // Build the shell device path
1619 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1620 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1621 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1622 *DevicePath
= NewDevicePath
;
1625 return EFI_NOT_FOUND
;
1630 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
1631 buffer, and the size of the buffer. If failure return NULL.
1633 @param Name String part of EFI variable name
1634 @param VendorGuid GUID part of EFI variable name
1635 @param VariableSize Returns the size of the EFI variable that was
1638 @return Dynamically allocated memory that contains a copy of the EFI variable.
1639 @return Caller is responsible freeing the buffer.
1640 @retval NULL Variable was not read
1645 GetVariableAndSize (
1647 IN EFI_GUID
*VendorGuid
,
1648 OUT UINTN
*VariableSize
1658 // Pass in a zero size buffer to find the required buffer size.
1661 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
1662 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1664 // Allocate the buffer to return
1666 Buffer
= AllocateZeroPool (BufferSize
);
1667 if (Buffer
== NULL
) {
1671 // Read variable into the allocated buffer.
1673 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
1674 if (EFI_ERROR (Status
)) {
1679 *VariableSize
= BufferSize
;
1685 This function will create all handles associate with every device
1686 path node. If the handle associate with one device path node can not
1687 be created success, then still give one chance to do the dispatch,
1688 which load the missing drivers if possible.
1690 @param DevicePathToConnect The device path which will be connected, it can
1691 be a multi-instance device path
1693 @retval EFI_SUCCESS All handles associate with every device path
1694 node have been created
1695 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
1696 @retval EFI_NOT_FOUND Create the handle associate with one device
1703 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
1707 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1708 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
1709 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
1710 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
1711 EFI_DEVICE_PATH_PROTOCOL
*Next
;
1713 EFI_HANDLE PreviousHandle
;
1716 if (DevicePathToConnect
== NULL
) {
1720 DevicePath
= DuplicateDevicePath (DevicePathToConnect
);
1721 CopyOfDevicePath
= DevicePath
;
1722 if (DevicePath
== NULL
) {
1723 return EFI_OUT_OF_RESOURCES
;
1728 // The outer loop handles multi instance device paths.
1729 // Only console variables contain multiple instance device paths.
1731 // After this call DevicePath points to the next Instance
1733 Instance
= GetNextDevicePathInstance (&DevicePath
, &Size
);
1735 while (!IsDevicePathEndType (Next
)) {
1736 Next
= NextDevicePathNode (Next
);
1739 SetDevicePathEndNode (Next
);
1742 // Start the real work of connect with RemainingDevicePath
1744 PreviousHandle
= NULL
;
1747 // Find the handle that best matches the Device Path. If it is only a
1748 // partial match the remaining part of the device path is returned in
1749 // RemainingDevicePath.
1751 RemainingDevicePath
= Instance
;
1752 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
1754 if (!EFI_ERROR (Status
)) {
1755 if (Handle
== PreviousHandle
) {
1757 // If no forward progress is made try invoking the Dispatcher.
1758 // A new FV may have been added to the system an new drivers
1759 // may now be found.
1760 // Status == EFI_SUCCESS means a driver was dispatched
1761 // Status == EFI_NOT_FOUND means no new drivers were dispatched
1763 Status
= gDS
->Dispatch ();
1766 if (!EFI_ERROR (Status
)) {
1767 PreviousHandle
= Handle
;
1769 // Connect all drivers that apply to Handle and RemainingDevicePath,
1770 // the Recursive flag is FALSE so only one level will be expanded.
1772 // Do not check the connect status here, if the connect controller fail,
1773 // then still give the chance to do dispatch, because partial
1774 // RemainingDevicepath may be in the new FV
1776 // 1. If the connect fail, RemainingDevicepath and handle will not
1777 // change, so next time will do the dispatch, then dispatch's status
1779 // 2. If the connect success, the RemainingDevicepath and handle will
1780 // change, then avoid the dispatch, we have chance to continue the
1783 gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
1787 // Loop until RemainingDevicePath is an empty device path
1789 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
1791 } while (DevicePath
!= NULL
);
1793 if (CopyOfDevicePath
!= NULL
) {
1794 FreePool (CopyOfDevicePath
);
1797 // All handle with DevicePath exists in the handle database