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 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_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
670 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
671 EFI_HANDLE DriverBindingHandle
;
672 BOOLEAN FoundLastReturned
;
673 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
674 DRIVER_IMAGE_INFO
*DriverImageInfo
;
675 LIST_ENTRY
*OverrideItemListIndex
;
676 LIST_ENTRY
*ImageInfoListIndex
;
677 EFI_DEVICE_PATH_PROTOCOL
*TempDriverImagePath
;
678 EFI_HANDLE ImageHandle
;
680 EFI_DEVICE_PATH_PROTOCOL
*LoadedImageHandleDevicePath
;
681 EFI_DEVICE_PATH_PROTOCOL
*TatalFilePath
;
682 EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL
*BusSpecificDriverOverride
;
683 UINTN DevicePathSize
;
686 // Check that ControllerHandle is a valid handle
688 if (ControllerHandle
== NULL
) {
689 return EFI_INVALID_PARAMETER
;
692 Status
= gBS
->HandleProtocol (
694 &gEfiDevicePathProtocolGuid
,
695 (VOID
**) &ControllerDevicePath
697 if (EFI_ERROR (Status
) || ControllerDevicePath
== NULL
) {
698 return EFI_INVALID_PARAMETER
;
702 // Search ControllerDevicePath in MappingDataBase
705 ControllerFound
= FALSE
;
706 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
707 while (OverrideItemListIndex
!= MappingDataBase
){
708 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
709 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
710 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
712 ControllerDevicePath
,
713 OverrideItem
->ControllerDevicePath
,
714 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
717 ControllerFound
= TRUE
;
722 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
725 if (!ControllerFound
) {
726 return EFI_NOT_FOUND
;
729 // Passing in a pointer to NULL, will return the first driver device path for ControllerHandle.
730 // Check whether the driverImagePath is not a device path that was returned on a previous call to GetDriverPath().
732 if (*DriverImageHandle
!= NULL
) {
733 if (*DriverImageHandle
!= OverrideItem
->LastReturnedImageHandle
) {
734 return EFI_INVALID_PARAMETER
;
738 // The GetDriverPath() maybe called recursively, because it use ConnectDevicePath() internally,
739 // so should check whether there is a dead loop.
740 // Here use a controller device path stack to record all processed controller device path during a GetDriverPath() call,
741 // and check the controller device path whether appear again during the GetDriverPath() call.
743 if (CheckExistInStack(OverrideItem
->ControllerDevicePath
)) {
745 // There is a dependecy dead loop if the ControllerDevicePath appear in stack twice
747 return EFI_UNSUPPORTED
;
749 PushDevPathStack (OverrideItem
->ControllerDevicePath
);
752 // Check every override driver, try to load and start them
754 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
755 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
756 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
757 if (DriverImageInfo
->ImageHandle
== NULL
) {
759 // Skip if the image is unloadable or unstartable
761 if ((!DriverImageInfo
->UnLoadable
) && ((!DriverImageInfo
->UnStartable
))) {
762 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
764 // 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.
765 // FV address maybe changes for memory layout adjust from time to time, use this funciton could promise the Fv file device path is right.
767 Status
= UpdateFvFileDevicePath (&TempDriverImagePath
, NULL
, CallerImageHandle
);
768 if (!EFI_ERROR (Status
)) {
769 FreePool(DriverImageInfo
->DriverImagePath
);
770 DriverImageInfo
->DriverImagePath
= TempDriverImagePath
;
773 // Get all Loaded Image protocol to check whether the driver image has been loaded and started
776 ImageHandleCount
= 0;
777 Status
= gBS
->LocateHandleBuffer (
779 &gEfiLoadedImageProtocolGuid
,
784 if (EFI_ERROR (Status
) || (ImageHandleCount
== 0)) {
785 return EFI_NOT_FOUND
;
788 for(Index
= 0; Index
< ImageHandleCount
; Index
++) {
789 Status
= gBS
->HandleProtocol (
790 ImageHandleBuffer
[Index
],
791 &gEfiLoadedImageProtocolGuid
,
792 (VOID
**) &LoadedImage
794 if (EFI_ERROR (Status
)) {
799 // Get the driver image total file path
801 LoadedImageHandleDevicePath
= NULL
;
802 Status
= gBS
->HandleProtocol (
803 LoadedImage
->DeviceHandle
,
804 &gEfiDevicePathProtocolGuid
,
805 (VOID
**) &LoadedImageHandleDevicePath
807 if (EFI_ERROR (Status
)) {
809 // Maybe Not all LoadedImage->DeviceHandle has valid value. Skip the invalid image.
814 TatalFilePath
= AppendDevicePath (LoadedImageHandleDevicePath
, LoadedImage
->FilePath
);
816 DevicePathSize
= GetDevicePathSize (DriverImageInfo
->DriverImagePath
);
817 if (DevicePathSize
== GetDevicePathSize (TatalFilePath
)) {
819 DriverImageInfo
->DriverImagePath
,
821 GetDevicePathSize (TatalFilePath
)
832 // Find its related driver binding protocol
833 // Driver binding handle may be different with its driver's Image handle,
835 DriverBindingHandle
= NULL
;
836 DriverBinding
= GetBindingProtocolFromImageHandle (
837 ImageHandleBuffer
[Index
],
840 ASSERT (DriverBinding
!= NULL
);
841 DriverImageInfo
->ImageHandle
= ImageHandleBuffer
[Index
];
842 } else if (GetCurrentTpl() <= TPL_CALLBACK
){
844 // The driver image has not been loaded and started, need try to load and start it now
845 // Try to connect all device in the driver image path
847 // Note: LoadImage() and StartImage() should be called under CALLBACK TPL in theory, but
848 // since many device need to be connected in CALLBACK level environment( e.g. Usb devices )
849 // and the Fat and Patition driver can endure executing in CALLBACK level in fact, so here permit
850 // to use LoadImage() and StartImage() in CALLBACK TPL.
852 Status
= ConnectDevicePath (DriverImageInfo
->DriverImagePath
);
854 // 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
856 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
857 gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &TempDriverImagePath
, &Handle
);
859 // Get the Bus Specific Driver Override Protocol instance on the Controller Handle
861 Status
= gBS
->HandleProtocol(
863 &gEfiBusSpecificDriverOverrideProtocolGuid
,
864 (VOID
**) &BusSpecificDriverOverride
866 if (!EFI_ERROR (Status
) && (BusSpecificDriverOverride
!= NULL
)) {
868 Status
= BusSpecificDriverOverride
->GetDriver (
869 BusSpecificDriverOverride
,
872 if (!EFI_ERROR (Status
)) {
874 // Find its related driver binding protocol
875 // Driver binding handle may be different with its driver's Image handle
877 DriverBindingHandle
= NULL
;
878 DriverBinding
= GetBindingProtocolFromImageHandle (
882 ASSERT (DriverBinding
!= NULL
);
883 DriverImageInfo
->ImageHandle
= ImageHandle
;
887 // Skip if any device cannot be connected now, future passes through GetDriver() may be able to load that driver.
888 // Only file path media or FwVol Device Path Node remain if all device is connected
890 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
891 gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &TempDriverImagePath
, &Handle
);
892 if (((DevicePathType (TempDriverImagePath
) == MEDIA_DEVICE_PATH
) &&
893 (DevicePathSubType (TempDriverImagePath
) == MEDIA_FILEPATH_DP
)) ||
894 (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) TempDriverImagePath
) != NULL
)
897 // Try to load the driver
899 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
900 Status
= gBS
->LoadImage (
908 if (!EFI_ERROR (Status
)) {
910 // Try to start the driver
912 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
913 if (EFI_ERROR (Status
)){
914 DriverImageInfo
->UnStartable
= TRUE
;
915 DriverImageInfo
->ImageHandle
= NULL
;
918 // Find its related driver binding protocol
919 // Driver binding handle may be different with its driver's Image handle
921 DriverBindingHandle
= NULL
;
922 DriverBinding
= GetBindingProtocolFromImageHandle (
926 ASSERT (DriverBinding
!= NULL
);
927 DriverImageInfo
->ImageHandle
= ImageHandle
;
930 DriverImageInfo
->UnLoadable
= TRUE
;
931 DriverImageInfo
->ImageHandle
= NULL
;
935 FreePool (ImageHandleBuffer
);
938 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
941 // Finish try to load and start the override driver of a controller, popup the controller's device path
943 PopDevPathStack (NULL
);
946 // return the DriverImageHandle for ControllerHandle
948 FoundLastReturned
= FALSE
;
949 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
950 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
951 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
952 if (DriverImageInfo
->ImageHandle
!= NULL
) {
953 if ((*DriverImageHandle
== NULL
) || FoundLastReturned
) {
954 OverrideItem
->LastReturnedImageHandle
= DriverImageInfo
->ImageHandle
;
955 *DriverImageHandle
= DriverImageInfo
->ImageHandle
;
957 } else if (*DriverImageHandle
== DriverImageInfo
->ImageHandle
){
958 FoundLastReturned
= TRUE
;
961 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
964 return EFI_NOT_FOUND
;
969 Check mapping database whether already has the mapping info which
970 records the input Controller to input DriverImage.
971 If has, the controller's total override driver number and input DriverImage's order number is return.
973 @param ControllerDevicePath The controller device path need to add a
974 override driver image item
975 @param DriverImageDevicePath The driver image device path need to be insert
976 @param MappingDataBase Mapping database list entry pointer
977 @param DriverInfoNum the controller's total override driver number
978 @param DriverImageNO The inserted order number
980 @return EFI_INVALID_PARAMETER
981 @return EFI_NOT_FOUND
988 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
989 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
990 IN LIST_ENTRY
* MappingDataBase
,
991 OUT UINT32
*DriverInfoNum
,
992 OUT UINT32
*DriverImageNO
995 LIST_ENTRY
*OverrideItemListIndex
;
996 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
997 LIST_ENTRY
*ImageInfoListIndex
;
998 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1001 UINTN DevicePathSize
;
1004 // Check that ControllerHandle is a valid handle
1006 if (ControllerDevicePath
== NULL
) {
1007 return EFI_INVALID_PARAMETER
;
1009 if (MappingDataBase
== NULL
) {
1010 return EFI_INVALID_PARAMETER
;
1013 // Search ControllerDevicePath in MappingDataBase
1016 OverrideItem
= NULL
;
1017 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
1018 while (OverrideItemListIndex
!= MappingDataBase
){
1019 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1020 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1021 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1023 ControllerDevicePath
,
1024 OverrideItem
->ControllerDevicePath
,
1025 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
1032 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
1036 return EFI_NOT_FOUND
;
1039 ASSERT (OverrideItem
->DriverInfoNum
!= 0);
1040 if (DriverInfoNum
!= NULL
) {
1041 *DriverInfoNum
= OverrideItem
->DriverInfoNum
;
1045 if (DriverImageDevicePath
== NULL
) {
1049 // return the DriverImageHandle for ControllerHandle
1053 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
1054 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
1055 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1057 DevicePathSize
= GetDevicePathSize (DriverImageDevicePath
);
1058 if (DevicePathSize
== GetDevicePathSize (DriverImageInfo
->DriverImagePath
)) {
1060 DriverImageDevicePath
,
1061 DriverImageInfo
->DriverImagePath
,
1062 GetDevicePathSize (DriverImageInfo
->DriverImagePath
)
1069 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
1073 return EFI_NOT_FOUND
;
1075 if (DriverImageNO
!= NULL
) {
1076 *DriverImageNO
= ImageNO
;
1085 Insert a driver image as a controller's override driver into the mapping database.
1086 The driver image's order number is indicated by DriverImageNO.
1088 @param ControllerDevicePath The controller device path need to add a
1089 override driver image item
1090 @param DriverImageDevicePath The driver image device path need to be insert
1091 @param MappingDataBase Mapping database list entry pointer
1092 @param DriverImageNO The inserted order number
1094 @return EFI_INVALID_PARAMETER
1095 @return EFI_ALREADY_STARTED
1102 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
1103 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
1104 IN LIST_ENTRY
*MappingDataBase
,
1105 IN UINT32 DriverImageNO
1109 LIST_ENTRY
*OverrideItemListIndex
;
1110 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1111 LIST_ENTRY
*ImageInfoListIndex
;
1112 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1115 UINTN DevicePathSize
;
1118 // Check that ControllerHandle is a valid handle
1120 if (ControllerDevicePath
== NULL
) {
1121 return EFI_INVALID_PARAMETER
;
1123 if (DriverImageDevicePath
== NULL
) {
1124 return EFI_INVALID_PARAMETER
;
1126 if (MappingDataBase
== NULL
) {
1127 return EFI_INVALID_PARAMETER
;
1130 Status
= CheckMapping (
1131 ControllerDevicePath
,
1132 DriverImageDevicePath
,
1137 if (Status
== EFI_SUCCESS
) {
1138 return EFI_ALREADY_STARTED
;
1142 // Search the input ControllerDevicePath in MappingDataBase
1145 OverrideItem
= NULL
;
1146 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
1147 while (OverrideItemListIndex
!= MappingDataBase
){
1148 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1149 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1150 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1152 ControllerDevicePath
,
1153 OverrideItem
->ControllerDevicePath
,
1154 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
1161 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
1164 // If cannot find, this is a new controller item
1165 // Add the Controller related PLATFORM_OVERRIDE_ITEM structrue in mapping data base
1168 OverrideItem
= AllocateZeroPool (sizeof (PLATFORM_OVERRIDE_ITEM
));
1169 ASSERT (OverrideItem
!= NULL
);
1170 OverrideItem
->Signature
= PLATFORM_OVERRIDE_ITEM_SIGNATURE
;
1171 OverrideItem
->ControllerDevicePath
= DuplicateDevicePath (ControllerDevicePath
);
1172 InitializeListHead (&OverrideItem
->DriverInfoList
);
1173 InsertTailList (MappingDataBase
, &OverrideItem
->Link
);
1177 // Prepare the driver image related DRIVER_IMAGE_INFO structure.
1179 DriverImageInfo
= AllocateZeroPool (sizeof (DRIVER_IMAGE_INFO
));
1180 ASSERT (DriverImageInfo
!= NULL
);
1181 DriverImageInfo
->Signature
= DRIVER_IMAGE_INFO_SIGNATURE
;
1182 DriverImageInfo
->DriverImagePath
= DuplicateDevicePath (DriverImageDevicePath
);
1184 // Find the driver image wantted order location
1188 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
1189 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
1190 if (ImageNO
== (DriverImageNO
- 1)) {
1192 // find the wantted order location, insert it
1194 InsertTailList (ImageInfoListIndex
, &DriverImageInfo
->Link
);
1195 OverrideItem
->DriverInfoNum
++;
1199 //DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
1201 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
1206 // if not find the wantted order location, add it as last item of the controller mapping item
1208 InsertTailList (&OverrideItem
->DriverInfoList
, &DriverImageInfo
->Link
);
1209 OverrideItem
->DriverInfoNum
++;
1217 Delete a controller's override driver from the mapping database.
1219 @param ControllerDevicePath The controller device path need to add a
1220 override driver image item
1221 @param DriverImageDevicePath The driver image device path need to be insert
1222 @param MappingDataBase Mapping database list entry pointer
1223 @param DriverImageNO The inserted order number
1225 @return EFI_INVALID_PARAMETER
1226 @return EFI_NOT_FOUND
1233 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
1234 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
1235 IN LIST_ENTRY
*MappingDataBase
1239 LIST_ENTRY
*OverrideItemListIndex
;
1240 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1241 LIST_ENTRY
*ImageInfoListIndex
;
1242 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1244 UINTN DevicePathSize
;
1247 // Check that ControllerHandle is a valid handle
1249 if (ControllerDevicePath
== NULL
) {
1250 return EFI_INVALID_PARAMETER
;
1253 if (MappingDataBase
== NULL
) {
1254 return EFI_INVALID_PARAMETER
;
1257 Status
= CheckMapping (
1258 ControllerDevicePath
,
1259 DriverImageDevicePath
,
1264 if (Status
== EFI_NOT_FOUND
) {
1265 return EFI_NOT_FOUND
;
1269 // Search ControllerDevicePath in MappingDataBase
1272 OverrideItem
= NULL
;
1273 OverrideItemListIndex
= MappingDataBase
->ForwardLink
;
1274 while (OverrideItemListIndex
!= MappingDataBase
){
1275 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1276 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1277 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1279 ControllerDevicePath
,
1280 OverrideItem
->ControllerDevicePath
,
1281 GetDevicePathSize (OverrideItem
->ControllerDevicePath
)
1288 OverrideItemListIndex
= OverrideItemListIndex
->ForwardLink
;
1292 ASSERT (OverrideItem
->DriverInfoNum
!= 0);
1297 ImageInfoListIndex
= OverrideItem
->DriverInfoList
.ForwardLink
;
1298 while (ImageInfoListIndex
!= &OverrideItem
->DriverInfoList
){
1299 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1300 ImageInfoListIndex
= ImageInfoListIndex
->ForwardLink
;
1301 if (DriverImageDevicePath
!= NULL
) {
1302 DevicePathSize
= GetDevicePathSize (DriverImageDevicePath
);
1303 if (DevicePathSize
== GetDevicePathSize (DriverImageInfo
->DriverImagePath
)) {
1305 DriverImageDevicePath
,
1306 DriverImageInfo
->DriverImagePath
,
1307 GetDevicePathSize (DriverImageInfo
->DriverImagePath
)
1311 FreePool(DriverImageInfo
->DriverImagePath
);
1312 RemoveEntryList (&DriverImageInfo
->Link
);
1313 OverrideItem
->DriverInfoNum
--;
1319 FreePool(DriverImageInfo
->DriverImagePath
);
1320 RemoveEntryList (&DriverImageInfo
->Link
);
1321 OverrideItem
->DriverInfoNum
--;
1325 if (DriverImageDevicePath
== NULL
) {
1326 ASSERT (OverrideItem
->DriverInfoNum
== 0);
1329 if (OverrideItem
->DriverInfoNum
== 0) {
1330 FreePool(OverrideItem
->ControllerDevicePath
);
1331 RemoveEntryList (&OverrideItem
->Link
);
1332 FreePool (OverrideItem
);
1336 return EFI_NOT_FOUND
;
1344 Deletes all environment variable(s) that contain the override mappings from Controller Device Path to
1345 a set of Driver Device Paths.
1354 DeleteOverridesVariables (
1359 VOID
*VariableBuffer
;
1363 CHAR16 OverrideVariableName
[40];
1366 // Get environment variable(s) number
1369 VariableBuffer
= GetVariableAndSize (L
"PlatDriOver", &gEfiOverrideVariableGuid
, &BufferSize
);
1371 if (VariableBuffer
== NULL
) {
1372 return EFI_NOT_FOUND
;
1375 // Check NotEnd to get all PlatDriOverX variable(s)
1377 while (*(UINT32
*)VariableBuffer
) {
1378 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", VariableNum
);
1379 VariableBuffer
= GetVariableAndSize (OverrideVariableName
, &gEfiOverrideVariableGuid
, &BufferSize
);
1381 ASSERT (VariableBuffer
!= NULL
);
1384 Status
= gRT
->SetVariable (
1386 &gEfiOverrideVariableGuid
,
1387 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1391 ASSERT (!EFI_ERROR (Status
));
1392 for (Index
= 1; Index
< VariableNum
; Index
++) {
1393 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", Index
);
1394 Status
= gRT
->SetVariable (
1395 OverrideVariableName
,
1396 &gEfiOverrideVariableGuid
,
1397 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1401 ASSERT (!EFI_ERROR (Status
));
1408 Push a controller device path into a globle device path list
1410 @param ControllerDevicePath The controller device path need to push into
1419 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1422 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
1424 DevicePathStackItem
= AllocateZeroPool (sizeof (DEVICE_PATH_STACK_ITEM
));
1425 ASSERT (DevicePathStackItem
!= NULL
);
1426 DevicePathStackItem
->Signature
= DEVICE_PATH_STACK_ITEM_SIGNATURE
;
1427 DevicePathStackItem
->DevicePath
= DuplicateDevicePath (DevicePath
);
1428 InsertTailList (&mDevicePathStack
, &DevicePathStackItem
->Link
);
1434 Pop a controller device path from a globle device path list
1436 @param ControllerDevicePath The controller device path retrieved from stack
1439 @return EFI_NOT_FOUND
1445 OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
1448 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
1449 LIST_ENTRY
*ItemListIndex
;
1451 ItemListIndex
= mDevicePathStack
.BackLink
;
1452 if (ItemListIndex
!= &mDevicePathStack
){
1453 DevicePathStackItem
= CR(ItemListIndex
, DEVICE_PATH_STACK_ITEM
, Link
, DEVICE_PATH_STACK_ITEM_SIGNATURE
);
1454 if (DevicePath
!= NULL
) {
1455 *DevicePath
= DuplicateDevicePath (DevicePathStackItem
->DevicePath
);
1457 FreePool (DevicePathStackItem
->DevicePath
);
1458 RemoveEntryList (&DevicePathStackItem
->Link
);
1459 FreePool (DevicePathStackItem
);
1462 return EFI_NOT_FOUND
;
1467 Check whether a controller device path is in a globle device path list
1469 @param ControllerDevicePath The controller device path need to check
1478 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
1481 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
1482 LIST_ENTRY
*ItemListIndex
;
1484 UINTN DevicePathSize
;
1487 ItemListIndex
= mDevicePathStack
.BackLink
;
1488 while (ItemListIndex
!= &mDevicePathStack
){
1489 DevicePathStackItem
= CR(ItemListIndex
, DEVICE_PATH_STACK_ITEM
, Link
, DEVICE_PATH_STACK_ITEM_SIGNATURE
);
1490 DevicePathSize
= GetDevicePathSize (DevicePath
);
1491 if (DevicePathSize
== GetDevicePathSize (DevicePathStackItem
->DevicePath
)) {
1494 DevicePathStackItem
->DevicePath
,
1495 GetDevicePathSize (DevicePathStackItem
->DevicePath
)
1502 ItemListIndex
= ItemListIndex
->BackLink
;
1510 According to a file guild, check a Fv file device path is valid. If it is invalid,
1511 try to return the valid device path.
1512 FV address maybe changes for memory layout adjust from time to time, use this funciton
1513 could promise the Fv file device path is right.
1515 @param DevicePath on input, the Fv file device path need to check
1516 on output, the updated valid Fv file device path
1517 @param FileGuid the Fv file guild
1518 @param CallerImageHandle
1520 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1522 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1524 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it
1526 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1527 and return the updated device path in DevicePath
1532 UpdateFvFileDevicePath (
1533 IN OUT EFI_DEVICE_PATH_PROTOCOL
** DevicePath
,
1534 IN EFI_GUID
*FileGuid
,
1535 IN EFI_HANDLE CallerImageHandle
1538 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1539 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
1541 EFI_GUID
*GuidPoint
;
1543 UINTN FvHandleCount
;
1544 EFI_HANDLE
*FvHandleBuffer
;
1545 EFI_FV_FILETYPE Type
;
1547 EFI_FV_FILE_ATTRIBUTES Attributes
;
1548 UINT32 AuthenticationStatus
;
1550 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1551 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
1552 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
1553 EFI_HANDLE FoundFvHandle
;
1554 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
1557 if ((DevicePath
== NULL
) || (*DevicePath
== NULL
)) {
1558 return EFI_INVALID_PARAMETER
;
1562 // Check whether the device path point to the default the input Fv file
1564 TempDevicePath
= *DevicePath
;
1565 LastDeviceNode
= TempDevicePath
;
1566 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1567 LastDeviceNode
= TempDevicePath
;
1568 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1570 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode (
1571 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
1573 if (GuidPoint
== NULL
) {
1575 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1577 return EFI_UNSUPPORTED
;
1580 if (FileGuid
!= NULL
) {
1581 if (!CompareGuid (GuidPoint
, FileGuid
)) {
1583 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1585 return EFI_UNSUPPORTED
;
1588 FileGuid
= GuidPoint
;
1592 // Check to see if the device path contain memory map node
1594 TempDevicePath
= *DevicePath
;
1596 while (!EfiIsDevicePathEnd (TempDevicePath
)) {
1598 // Use old Device Path
1600 if (DevicePathType (TempDevicePath
) == HARDWARE_DEVICE_PATH
&&
1601 DevicePathSubType (TempDevicePath
) == HW_MEMMAP_DP
) {
1605 TempDevicePath
= EfiNextDevicePathNode (TempDevicePath
);
1609 return EFI_UNSUPPORTED
;
1613 // Check whether the input Fv file device path is valid
1615 TempDevicePath
= *DevicePath
;
1616 FoundFvHandle
= NULL
;
1617 Status
= gBS
->LocateDevicePath (
1618 &gEfiFirmwareVolume2ProtocolGuid
,
1622 if (!EFI_ERROR (Status
)) {
1623 Status
= gBS
->HandleProtocol (
1625 &gEfiFirmwareVolume2ProtocolGuid
,
1628 if (!EFI_ERROR (Status
)) {
1630 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1632 Status
= Fv
->ReadFile (
1639 &AuthenticationStatus
1641 if (!EFI_ERROR (Status
)) {
1642 return EFI_ALREADY_STARTED
;
1648 // Look for the input wanted FV file in current FV
1649 // First, try to look for in Caller own FV. Caller and input wanted FV file usually are in the same FV
1652 FoundFvHandle
= NULL
;
1653 Status
= gBS
->HandleProtocol (
1655 &gEfiLoadedImageProtocolGuid
,
1656 (VOID
**) &LoadedImage
1658 if (!EFI_ERROR (Status
)) {
1659 Status
= gBS
->HandleProtocol (
1660 LoadedImage
->DeviceHandle
,
1661 &gEfiFirmwareVolume2ProtocolGuid
,
1664 if (!EFI_ERROR (Status
)) {
1665 Status
= Fv
->ReadFile (
1672 &AuthenticationStatus
1674 if (!EFI_ERROR (Status
)) {
1676 FoundFvHandle
= LoadedImage
->DeviceHandle
;
1681 // Second, if fail to find, try to enumerate all FV
1684 gBS
->LocateHandleBuffer (
1686 &gEfiFirmwareVolume2ProtocolGuid
,
1691 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
1692 gBS
->HandleProtocol (
1693 FvHandleBuffer
[Index
],
1694 &gEfiFirmwareVolume2ProtocolGuid
,
1698 Status
= Fv
->ReadFile (
1705 &AuthenticationStatus
1707 if (EFI_ERROR (Status
)) {
1709 // Skip if input Fv file not in the FV
1714 FoundFvHandle
= FvHandleBuffer
[Index
];
1721 // Build the shell device path
1723 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
1724 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
1725 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
1726 *DevicePath
= NewDevicePath
;
1729 return EFI_NOT_FOUND
;
1734 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
1735 buffer, and the size of the buffer. If failure return NULL.
1737 @param Name String part of EFI variable name
1738 @param VendorGuid GUID part of EFI variable name
1739 @param VariableSize Returns the size of the EFI variable that was
1742 @return Dynamically allocated memory that contains a copy of the EFI variable.
1743 @return Caller is responsible freeing the buffer.
1744 @retval NULL Variable was not read
1749 GetVariableAndSize (
1751 IN EFI_GUID
*VendorGuid
,
1752 OUT UINTN
*VariableSize
1762 // Pass in a zero size buffer to find the required buffer size.
1765 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
1766 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1768 // Allocate the buffer to return
1770 Buffer
= AllocateZeroPool (BufferSize
);
1771 if (Buffer
== NULL
) {
1775 // Read variable into the allocated buffer.
1777 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
1778 if (EFI_ERROR (Status
)) {
1783 *VariableSize
= BufferSize
;
1789 This function will create all handles associate with every device
1790 path node. If the handle associate with one device path node can not
1791 be created success, then still give one chance to do the dispatch,
1792 which load the missing drivers if possible.
1794 @param DevicePathToConnect The device path which will be connected, it can
1795 be a multi-instance device path
1797 @retval EFI_SUCCESS All handles associate with every device path
1798 node have been created
1799 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
1800 @retval EFI_NOT_FOUND Create the handle associate with one device
1807 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
1811 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1812 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
1813 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
1814 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
1815 EFI_DEVICE_PATH_PROTOCOL
*Next
;
1817 EFI_HANDLE PreviousHandle
;
1820 if (DevicePathToConnect
== NULL
) {
1824 DevicePath
= DuplicateDevicePath (DevicePathToConnect
);
1825 CopyOfDevicePath
= DevicePath
;
1826 if (DevicePath
== NULL
) {
1827 return EFI_OUT_OF_RESOURCES
;
1832 // The outer loop handles multi instance device paths.
1833 // Only console variables contain multiple instance device paths.
1835 // After this call DevicePath points to the next Instance
1837 Instance
= GetNextDevicePathInstance (&DevicePath
, &Size
);
1839 while (!IsDevicePathEndType (Next
)) {
1840 Next
= NextDevicePathNode (Next
);
1843 SetDevicePathEndNode (Next
);
1846 // Start the real work of connect with RemainingDevicePath
1848 PreviousHandle
= NULL
;
1851 // Find the handle that best matches the Device Path. If it is only a
1852 // partial match the remaining part of the device path is returned in
1853 // RemainingDevicePath.
1855 RemainingDevicePath
= Instance
;
1856 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
1858 if (!EFI_ERROR (Status
)) {
1859 if (Handle
== PreviousHandle
) {
1861 // If no forward progress is made try invoking the Dispatcher.
1862 // A new FV may have been added to the system an new drivers
1863 // may now be found.
1864 // Status == EFI_SUCCESS means a driver was dispatched
1865 // Status == EFI_NOT_FOUND means no new drivers were dispatched
1867 Status
= gDS
->Dispatch ();
1870 if (!EFI_ERROR (Status
)) {
1871 PreviousHandle
= Handle
;
1873 // Connect all drivers that apply to Handle and RemainingDevicePath,
1874 // the Recursive flag is FALSE so only one level will be expanded.
1876 // Do not check the connect status here, if the connect controller fail,
1877 // then still give the chance to do dispatch, because partial
1878 // RemainingDevicepath may be in the new FV
1880 // 1. If the connect fail, RemainingDevicepath and handle will not
1881 // change, so next time will do the dispatch, then dispatch's status
1883 // 2. If the connect success, the RemainingDevicepath and handle will
1884 // change, then avoid the dispatch, we have chance to continue the
1887 gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
1891 // Loop until RemainingDevicePath is an empty device path
1893 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
1895 } while (DevicePath
!= NULL
);
1897 if (CopyOfDevicePath
!= NULL
) {
1898 FreePool (CopyOfDevicePath
);
1901 // All handle with DevicePath exists in the handle database