2 Implementation of the shared functions to do the platform driver vverride mapping.
4 Copyright (c) 2007 - 2009, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "InternalPlatDriOverrideDxe.h"
17 #define PLATFORM_OVERRIDE_ITEM_SIGNATURE SIGNATURE_32('p','d','o','i')
18 typedef struct _PLATFORM_OVERRIDE_ITEM
{
22 EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
;
24 /// List of DRIVER_IMAGE_INFO
26 LIST_ENTRY DriverInfoList
;
27 EFI_HANDLE LastReturnedImageHandle
;
28 } PLATFORM_OVERRIDE_ITEM
;
30 #define DRIVER_IMAGE_INFO_SIGNATURE SIGNATURE_32('p','d','i','i')
31 typedef struct _DRIVER_IMAGE_INFO
{
34 EFI_HANDLE ImageHandle
;
35 EFI_DEVICE_PATH_PROTOCOL
*DriverImagePath
;
40 #define DEVICE_PATH_STACK_ITEM_SIGNATURE SIGNATURE_32('d','p','s','i')
41 typedef struct _DEVICE_PATH_STACK_ITEM
{
44 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
45 } DEVICE_PATH_STACK_ITEM
;
48 LIST_ENTRY mDevicePathStack
= INITIALIZE_LIST_HEAD_VARIABLE (mDevicePathStack
);
51 Push a controller device path into a globle device path list.
53 @param DevicePath The controller device path to push into stack
55 @retval EFI_SUCCESS Device path successfully pushed into the stack.
61 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
64 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
66 DevicePathStackItem
= AllocateZeroPool (sizeof (DEVICE_PATH_STACK_ITEM
));
67 ASSERT (DevicePathStackItem
!= NULL
);
68 DevicePathStackItem
->Signature
= DEVICE_PATH_STACK_ITEM_SIGNATURE
;
69 DevicePathStackItem
->DevicePath
= DuplicateDevicePath (DevicePath
);
70 InsertTailList (&mDevicePathStack
, &DevicePathStackItem
->Link
);
76 Pop a controller device path from a globle device path list
78 @param DevicePath The controller device path popped from stack
80 @retval EFI_SUCCESS Controller device path successfully popped.
81 @retval EFI_NOT_FOUND Stack is empty.
87 OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
90 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
91 LIST_ENTRY
*ItemListIndex
;
93 ItemListIndex
= mDevicePathStack
.BackLink
;
95 // Check if the stack is empty
97 if (ItemListIndex
!= &mDevicePathStack
){
98 DevicePathStackItem
= CR(ItemListIndex
, DEVICE_PATH_STACK_ITEM
, Link
, DEVICE_PATH_STACK_ITEM_SIGNATURE
);
99 if (DevicePath
!= NULL
) {
100 *DevicePath
= DuplicateDevicePath (DevicePathStackItem
->DevicePath
);
102 FreePool (DevicePathStackItem
->DevicePath
);
103 RemoveEntryList (&DevicePathStackItem
->Link
);
104 FreePool (DevicePathStackItem
);
107 return EFI_NOT_FOUND
;
112 Check whether a controller device path is in a globle device path list
114 @param DevicePath The controller device path to check
116 @retval TRUE DevicePath exists in the stack.
117 @retval FALSE DevicePath does not exist in the stack.
123 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
126 DEVICE_PATH_STACK_ITEM
*DevicePathStackItem
;
127 LIST_ENTRY
*ItemListIndex
;
128 UINTN DevicePathSize
;
130 ItemListIndex
= mDevicePathStack
.BackLink
;
131 while (ItemListIndex
!= &mDevicePathStack
){
132 DevicePathStackItem
= CR(ItemListIndex
, DEVICE_PATH_STACK_ITEM
, Link
, DEVICE_PATH_STACK_ITEM_SIGNATURE
);
133 DevicePathSize
= GetDevicePathSize (DevicePath
);
134 if (DevicePathSize
== GetDevicePathSize (DevicePathStackItem
->DevicePath
)) {
135 if (CompareMem (DevicePath
, DevicePathStackItem
->DevicePath
, DevicePathSize
) == 0) {
139 ItemListIndex
= ItemListIndex
->BackLink
;
146 Update the FV file device path if it is not valid.
148 According to a file GUID, check a Fv file device path is valid. If it is invalid,
149 try to return the valid device path.
150 FV address maybe changes for memory layout adjust from time to time, use this funciton
151 could promise the Fv file device path is right.
153 @param DevicePath On input, the FV file device path to check
154 On output, the updated valid FV file device path
155 @param FileGuid The FV file GUID
156 @param CallerImageHandle Image handle of the caller
158 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
160 @retval EFI_UNSUPPORTED the input DevicePath does not contain FV file
162 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to FV file, it
164 @retval EFI_SUCCESS Successfully updated the invalid DevicePath,
165 and return the updated device path in DevicePath
170 UpdateFvFileDevicePath (
171 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
,
172 IN EFI_GUID
*FileGuid
,
173 IN EFI_HANDLE CallerImageHandle
176 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
177 EFI_DEVICE_PATH_PROTOCOL
*LastDeviceNode
;
182 EFI_HANDLE
*FvHandleBuffer
;
183 EFI_FV_FILETYPE Type
;
185 EFI_FV_FILE_ATTRIBUTES Attributes
;
186 UINT32 AuthenticationStatus
;
188 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
189 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
190 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode
;
191 EFI_HANDLE FoundFvHandle
;
192 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
195 if (DevicePath
== NULL
) {
196 return EFI_INVALID_PARAMETER
;
199 if (*DevicePath
== NULL
) {
200 return EFI_INVALID_PARAMETER
;
204 // Check whether the device path points to the default the input FV file
206 TempDevicePath
= *DevicePath
;
207 LastDeviceNode
= TempDevicePath
;
208 while (!IsDevicePathEnd (TempDevicePath
)) {
209 LastDeviceNode
= TempDevicePath
;
210 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
212 GuidPoint
= EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) LastDeviceNode
);
213 if (GuidPoint
== NULL
) {
215 // If this option does not point to a FV file, just return EFI_UNSUPPORTED.
217 return EFI_UNSUPPORTED
;
220 if (FileGuid
!= NULL
) {
221 if (!CompareGuid (GuidPoint
, FileGuid
)) {
223 // If the FV file is not the input file GUID, just return EFI_UNSUPPORTED
225 return EFI_UNSUPPORTED
;
228 FileGuid
= GuidPoint
;
232 // Check to see if the device path contains memory map node
234 TempDevicePath
= *DevicePath
;
236 while (!IsDevicePathEnd (TempDevicePath
)) {
238 // Use old Device Path
240 if (DevicePathType (TempDevicePath
) == HARDWARE_DEVICE_PATH
&&
241 DevicePathSubType (TempDevicePath
) == HW_MEMMAP_DP
) {
245 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
249 return EFI_UNSUPPORTED
;
253 // Check whether the input Fv file device path is valid
255 TempDevicePath
= *DevicePath
;
256 FoundFvHandle
= NULL
;
257 Status
= gBS
->LocateDevicePath (
258 &gEfiFirmwareVolume2ProtocolGuid
,
262 if (!EFI_ERROR (Status
)) {
263 Status
= gBS
->HandleProtocol (
265 &gEfiFirmwareVolume2ProtocolGuid
,
268 if (!EFI_ERROR (Status
)) {
270 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
272 Status
= Fv
->ReadFile (
279 &AuthenticationStatus
281 if (!EFI_ERROR (Status
)) {
282 return EFI_ALREADY_STARTED
;
288 // Look for the input wanted FV file in current FV
289 // First, try to look for in Caller own FV. Caller and input wanted FV file usually are in the same FV
292 FoundFvHandle
= NULL
;
293 Status
= gBS
->HandleProtocol (
295 &gEfiLoadedImageProtocolGuid
,
296 (VOID
**) &LoadedImage
298 if (!EFI_ERROR (Status
)) {
299 Status
= gBS
->HandleProtocol (
300 LoadedImage
->DeviceHandle
,
301 &gEfiFirmwareVolume2ProtocolGuid
,
304 if (!EFI_ERROR (Status
)) {
305 Status
= Fv
->ReadFile (
312 &AuthenticationStatus
314 if (!EFI_ERROR (Status
)) {
316 FoundFvHandle
= LoadedImage
->DeviceHandle
;
321 // Second, if fail to find, try to enumerate all FV
324 gBS
->LocateHandleBuffer (
326 &gEfiFirmwareVolume2ProtocolGuid
,
331 for (Index
= 0; Index
< FvHandleCount
; Index
++) {
332 gBS
->HandleProtocol (
333 FvHandleBuffer
[Index
],
334 &gEfiFirmwareVolume2ProtocolGuid
,
338 Status
= Fv
->ReadFile (
345 &AuthenticationStatus
347 if (EFI_ERROR (Status
)) {
349 // Skip if input Fv file not in the FV
354 FoundFvHandle
= FvHandleBuffer
[Index
];
361 // Build the shell device path
363 NewDevicePath
= DevicePathFromHandle (FoundFvHandle
);
364 EfiInitializeFwVolDevicepathNode (&FvFileNode
, FileGuid
);
365 NewDevicePath
= AppendDevicePathNode (NewDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) &FvFileNode
);
366 *DevicePath
= NewDevicePath
;
369 return EFI_NOT_FOUND
;
373 Gets the data and size of a variable.
375 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
376 buffer, and the size of the buffer. If failure return NULL.
378 @param Name String part of EFI variable name
379 @param VendorGuid GUID part of EFI variable name
380 @param VariableSize Returns the size of the EFI variable that was
383 @return Dynamically allocated memory that contains a copy of the EFI variable.
384 Caller is responsible freeing the buffer.
385 @retval NULL Variable was not read
392 IN EFI_GUID
*VendorGuid
,
393 OUT UINTN
*VariableSize
403 // Pass in a zero size buffer to find the required buffer size.
406 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
407 if (Status
== EFI_BUFFER_TOO_SMALL
) {
409 // Allocate the buffer to return
411 Buffer
= AllocateZeroPool (BufferSize
);
412 if (Buffer
== NULL
) {
416 // Read variable into the allocated buffer.
418 Status
= gRT
->GetVariable (Name
, VendorGuid
, NULL
, &BufferSize
, Buffer
);
419 if (EFI_ERROR (Status
)) {
424 *VariableSize
= BufferSize
;
429 Connect to the handle to a device on the device path.
431 This function will create all handles associate with every device
432 path node. If the handle associate with one device path node can not
433 be created success, then still give one chance to do the dispatch,
434 which load the missing drivers if possible.
436 @param DevicePathToConnect The device path which will be connected, it can
437 be a multi-instance device path
439 @retval EFI_SUCCESS All handles associate with every device path
440 node have been created
441 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
442 @retval EFI_NOT_FOUND Create the handle associate with one device
449 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePathToConnect
453 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
454 EFI_DEVICE_PATH_PROTOCOL
*CopyOfDevicePath
;
455 EFI_DEVICE_PATH_PROTOCOL
*Instance
;
456 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
457 EFI_DEVICE_PATH_PROTOCOL
*Next
;
459 EFI_HANDLE PreviousHandle
;
462 if (DevicePathToConnect
== NULL
) {
466 DevicePath
= DuplicateDevicePath (DevicePathToConnect
);
467 CopyOfDevicePath
= DevicePath
;
468 if (DevicePath
== NULL
) {
469 return EFI_OUT_OF_RESOURCES
;
474 // The outer loop handles multi instance device paths.
475 // Only console variables contain multiple instance device paths.
477 // After this call DevicePath points to the next Instance
479 Instance
= GetNextDevicePathInstance (&DevicePath
, &Size
);
480 ASSERT (Instance
!= NULL
);
483 while (!IsDevicePathEndType (Next
)) {
484 Next
= NextDevicePathNode (Next
);
487 SetDevicePathEndNode (Next
);
490 // Start the real work of connect with RemainingDevicePath
492 PreviousHandle
= NULL
;
495 // Find the handle that best matches the Device Path. If it is only a
496 // partial match the remaining part of the device path is returned in
497 // RemainingDevicePath.
499 RemainingDevicePath
= Instance
;
500 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &Handle
);
502 if (!EFI_ERROR (Status
)) {
503 if (Handle
== PreviousHandle
) {
505 // If no forward progress is made try invoking the Dispatcher.
506 // A new FV may have been added to the system an new drivers
508 // Status == EFI_SUCCESS means a driver was dispatched
509 // Status == EFI_NOT_FOUND means no new drivers were dispatched
511 Status
= gDS
->Dispatch ();
514 if (!EFI_ERROR (Status
)) {
515 PreviousHandle
= Handle
;
517 // Connect all drivers that apply to Handle and RemainingDevicePath,
518 // the Recursive flag is FALSE so only one level will be expanded.
520 // Do not check the connect status here, if the connect controller fail,
521 // then still give the chance to do dispatch, because partial
522 // RemainingDevicepath may be in the new FV
524 // 1. If the connect fails, RemainingDevicepath and handle will not
525 // change, so next time will do the dispatch, then dispatch's status
527 // 2. If the connect succeeds, the RemainingDevicepath and handle will
528 // change, then avoid the dispatch, we have chance to continue the
531 gBS
->ConnectController (Handle
, NULL
, RemainingDevicePath
, FALSE
);
535 // Loop until RemainingDevicePath is an empty device path
537 } while (!EFI_ERROR (Status
) && !IsDevicePathEnd (RemainingDevicePath
));
539 } while (DevicePath
!= NULL
);
541 if (CopyOfDevicePath
!= NULL
) {
542 FreePool (CopyOfDevicePath
);
545 // All handle with DevicePath exists in the handle database
551 Free all the mapping database memory resource and initialize the mapping list entry.
553 @param MappingDataBase Mapping database list entry pointer
555 @retval EFI_SUCCESS Mapping database successfully freed
556 @retval EFI_INVALID_PARAMETER MappingDataBase is NULL
561 FreeMappingDatabase (
562 IN OUT LIST_ENTRY
*MappingDataBase
565 LIST_ENTRY
*OverrideItemListIndex
;
566 LIST_ENTRY
*ImageInfoListIndex
;
567 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
568 DRIVER_IMAGE_INFO
*DriverImageInfo
;
570 if (MappingDataBase
== NULL
) {
571 return EFI_INVALID_PARAMETER
;
574 OverrideItemListIndex
= GetFirstNode (MappingDataBase
);
575 while (!IsNull (MappingDataBase
, OverrideItemListIndex
)) {
576 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
578 // Free PLATFORM_OVERRIDE_ITEM.ControllerDevicePath[]
580 if (OverrideItem
->ControllerDevicePath
!= NULL
){
581 FreePool (OverrideItem
->ControllerDevicePath
);
584 ImageInfoListIndex
= GetFirstNode (&OverrideItem
->DriverInfoList
);
585 while (!IsNull (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
)) {
587 // Free DRIVER_IMAGE_INFO.DriverImagePath[]
589 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
590 if (DriverImageInfo
->DriverImagePath
!= NULL
) {
591 FreePool(DriverImageInfo
->DriverImagePath
);
594 // Free DRIVER_IMAGE_INFO itself
596 ImageInfoListIndex
= GetNextNode (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
);
597 RemoveEntryList (&DriverImageInfo
->Link
);
598 FreePool (DriverImageInfo
);
601 // Free PLATFORM_OVERRIDE_ITEM itself
603 OverrideItemListIndex
= GetNextNode (MappingDataBase
, OverrideItemListIndex
);
604 RemoveEntryList (&OverrideItem
->Link
);
605 FreePool (OverrideItem
);
608 InitializeListHead (MappingDataBase
);
614 Create the mapping database according to variable.
616 Read the environment variable(s) that contain the override mappings from Controller Device Path to
617 a set of Driver Device Paths, and create the mapping database in memory with those variable info.
620 // NotEnd indicate whether the variable is the last one, and has no subsequent variable need to load.
621 // Each variable has MaximumVariableSize limitation, so we maybe need multiple variables to store
622 // large mapping infos.
623 // The variable(s) name rule is PlatDriOver, PlatDriOver1, PlatDriOver2, ....
625 UINT32 NotEnd; //Zero is the last one.
627 // The entry which contains the mapping that Controller Device Path to a set of Driver Device Paths
628 // There are often multi mapping entries in a variable.
630 UINT32 SIGNATURE; //SIGNATURE_32('p','d','o','i')
632 EFI_DEVICE_PATH_PROTOCOL ControllerDevicePath[];
633 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
634 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
635 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
637 UINT32 NotEnd; //Zero is the last one.
640 EFI_DEVICE_PATH_PROTOCOL ControllerDevicePath[];
641 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
642 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
643 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
647 @param MappingDataBase Mapping database list entry pointer
649 @retval EFI_SUCCESS Create the mapping database in memory successfully
650 @retval EFI_INVALID_PARAMETER MappingDataBase pointer is null
651 @retval EFI_NOT_FOUND Cannot find the 'PlatDriOver' NV variable
652 @retval EFI_VOLUME_CORRUPTED The found NV variable is corrupted
657 InitOverridesMapping (
658 OUT LIST_ENTRY
*MappingDataBase
662 VOID
*VariableBuffer
;
663 UINT8
*VariableIndex
;
665 CHAR16 OverrideVariableName
[40];
668 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
669 DRIVER_IMAGE_INFO
*DriverImageInfo
;
672 EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
;
673 EFI_DEVICE_PATH_PROTOCOL
*DriverDevicePath
;
676 if (MappingDataBase
== NULL
) {
677 return EFI_INVALID_PARAMETER
;
681 // Check the environment variable(s) that contain the override mappings .
683 VariableBuffer
= GetVariableAndSize (L
"PlatDriOver", &gEfiCallerIdGuid
, &BufferSize
);
684 ASSERT ((UINTN
) VariableBuffer
% sizeof(UINTN
) == 0);
685 if (VariableBuffer
== NULL
) {
686 return EFI_NOT_FOUND
;
690 // Traverse all variables.
695 VariableIndex
= VariableBuffer
;
699 NotEnd
= *(UINT32
*) VariableIndex
;
701 // Traverse the entries containing the mapping that Controller Device Path
702 // to a set of Driver Device Paths within this variable.
704 VariableIndex
= VariableIndex
+ sizeof (UINT32
);
705 while (VariableIndex
< ((UINT8
*)VariableBuffer
+ BufferSize
)) {
707 // Check signature of this entry
709 Signature
= *(UINT32
*) VariableIndex
;
710 if (Signature
!= PLATFORM_OVERRIDE_ITEM_SIGNATURE
) {
715 // Create PLATFORM_OVERRIDE_ITEM for this mapping
717 OverrideItem
= AllocateZeroPool (sizeof (PLATFORM_OVERRIDE_ITEM
));
718 ASSERT (OverrideItem
!= NULL
);
719 OverrideItem
->Signature
= PLATFORM_OVERRIDE_ITEM_SIGNATURE
;
720 InitializeListHead (&OverrideItem
->DriverInfoList
);
721 VariableIndex
= VariableIndex
+ sizeof (UINT32
);
725 DriverNumber
= *(UINT32
*) VariableIndex
;
726 OverrideItem
->DriverInfoNum
= DriverNumber
;
727 VariableIndex
= VariableIndex
+ sizeof (UINT32
);
729 // Get ControllerDevicePath[]
731 ControllerDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) VariableIndex
;
732 OverrideItem
->ControllerDevicePath
= DuplicateDevicePath (ControllerDevicePath
);
733 VariableIndex
= VariableIndex
+ GetDevicePathSize (ControllerDevicePath
);
735 // Align the VariableIndex since the controller device path may not be aligned, refer to the SaveOverridesMapping()
737 VariableIndex
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
740 // Get all DriverImageDevicePath[]
742 for (Index
= 0; Index
< DriverNumber
; Index
++) {
744 // Create DRIVER_IMAGE_INFO for this DriverDevicePath[]
746 DriverImageInfo
= AllocateZeroPool (sizeof (DRIVER_IMAGE_INFO
));
747 ASSERT (DriverImageInfo
!= NULL
);
748 DriverImageInfo
->Signature
= DRIVER_IMAGE_INFO_SIGNATURE
;
750 DriverDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) VariableIndex
;
751 DriverImageInfo
->DriverImagePath
= DuplicateDevicePath (DriverDevicePath
);
752 VariableIndex
= VariableIndex
+ GetDevicePathSize (DriverDevicePath
);
754 // Align the VariableIndex since the driver image device path may not be aligned, refer to the SaveOverridesMapping()
756 VariableIndex
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
758 InsertTailList (&OverrideItem
->DriverInfoList
, &DriverImageInfo
->Link
);
760 InsertTailList (MappingDataBase
, &OverrideItem
->Link
);
763 FreePool (VariableBuffer
);
765 FreeMappingDatabase (MappingDataBase
);
766 return EFI_VOLUME_CORRUPTED
;
770 // If there are additional variables (PlatDriOver1, PlatDriOver2, PlatDriOver3.....), get them.
771 // NotEnd indicates whether current variable is the end variable.
774 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", VariableNum
++);
775 VariableBuffer
= GetVariableAndSize (OverrideVariableName
, &gEfiCallerIdGuid
, &BufferSize
);
776 ASSERT ((UINTN
) VariableBuffer
% sizeof(UINTN
) == 0);
777 if (VariableBuffer
== NULL
) {
778 FreeMappingDatabase (MappingDataBase
);
779 return EFI_VOLUME_CORRUPTED
;
783 } while (NotEnd
!= 0);
790 Calculate the needed size in NV variable for recording a specific PLATFORM_OVERRIDE_ITEM info.
792 @param OverrideItemListIndex Pointer to the list of a specific PLATFORM_OVERRIDE_ITEM
794 @return The needed size number
799 GetOneItemNeededSize (
800 IN LIST_ENTRY
*OverrideItemListIndex
804 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
805 LIST_ENTRY
*ImageInfoListIndex
;
806 DRIVER_IMAGE_INFO
*DriverImageInfo
;
807 UINTN DevicePathSize
;
810 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
811 NeededSize
+= sizeof (UINT32
); //UINT32 SIGNATURE;
812 NeededSize
+= sizeof (UINT32
); //UINT32 DriverNum;
813 DevicePathSize
= GetDevicePathSize (OverrideItem
->ControllerDevicePath
);
814 NeededSize
+= DevicePathSize
; // ControllerDevicePath
816 // Align the controller device path
818 NeededSize
+= ((sizeof(UINT32
) - DevicePathSize
) & (sizeof(UINT32
) - 1));
820 // Traverse the Driver Info List of this Override Item
822 ImageInfoListIndex
= GetFirstNode (&OverrideItem
->DriverInfoList
);
823 while (!IsNull (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
)) {
824 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
825 DevicePathSize
= GetDevicePathSize (DriverImageInfo
->DriverImagePath
);
826 NeededSize
+= DevicePathSize
; //DriverDevicePath
828 // Align the driver image device path
830 NeededSize
+= ((sizeof(UINT32
) - DevicePathSize
) & (sizeof(UINT32
) - 1));
831 ImageInfoListIndex
= GetNextNode (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
);
838 Deletes all environment variable(s) that contain the override mappings from Controller Device Path to
839 a set of Driver Device Paths.
841 @retval EFI_SUCCESS Delete all variable(s) successfully.
846 DeleteOverridesVariables (
851 VOID
*VariableBuffer
;
855 CHAR16 OverrideVariableName
[40];
858 // Get environment variable(s) number
861 VariableBuffer
= GetVariableAndSize (L
"PlatDriOver", &gEfiCallerIdGuid
, &BufferSize
);
863 if (VariableBuffer
== NULL
) {
864 return EFI_NOT_FOUND
;
867 // Check NotEnd to get all PlatDriOverX variable(s)
869 while ((*(UINT32
*)VariableBuffer
) != 0) {
870 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", VariableNum
);
871 VariableBuffer
= GetVariableAndSize (OverrideVariableName
, &gEfiCallerIdGuid
, &BufferSize
);
873 ASSERT (VariableBuffer
!= NULL
);
877 // Delete PlatDriOver and all additional variables, if exist.
879 Status
= gRT
->SetVariable (
882 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
886 ASSERT (!EFI_ERROR (Status
));
887 for (Index
= 1; Index
< VariableNum
; Index
++) {
888 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", Index
);
889 Status
= gRT
->SetVariable (
890 OverrideVariableName
,
892 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
896 ASSERT (!EFI_ERROR (Status
));
903 Save the memory mapping database into NV environment variable(s).
905 @param MappingDataBase Mapping database list entry pointer
907 @retval EFI_SUCCESS Save memory mapping database successfully
908 @retval EFI_INVALID_PARAMETER MappingDataBase pointer is null
913 SaveOverridesMapping (
914 IN LIST_ENTRY
*MappingDataBase
918 VOID
*VariableBuffer
;
919 UINT8
*VariableIndex
;
921 CHAR16 OverrideVariableName
[40];
923 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
924 DRIVER_IMAGE_INFO
*DriverImageInfo
;
925 LIST_ENTRY
*OverrideItemListIndex
;
926 LIST_ENTRY
*ItemIndex
;
927 LIST_ENTRY
*ImageInfoListIndex
;
928 UINTN VariableNeededSize
;
929 UINT64 MaximumVariableStorageSize
;
930 UINT64 RemainingVariableStorageSize
;
931 UINT64 MaximumVariableSize
;
932 UINTN OneItemNeededSize
;
934 if (MappingDataBase
== NULL
) {
935 return EFI_INVALID_PARAMETER
;
938 if (IsListEmpty (MappingDataBase
)) {
939 Status
= DeleteOverridesVariables ();
944 // Get the the maximum size of an individual EFI variable in current system
946 gRT
->QueryVariableInfo (
947 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
948 &MaximumVariableStorageSize
,
949 &RemainingVariableStorageSize
,
954 OverrideItemListIndex
= GetFirstNode (MappingDataBase
);
955 while (!IsNull (MappingDataBase
, OverrideItemListIndex
)) {
957 // Try to find the most proper variable size which <= MaximumVariableSize,
958 // but can contain mapping info as much as possible
960 VariableNeededSize
= sizeof (UINT32
); // NotEnd;
961 ItemIndex
= OverrideItemListIndex
;
964 // Traverse all PLATFORM_OVERRIDE_ITEMs and get the total size.
966 while (!IsNull (MappingDataBase
, ItemIndex
)) {
967 OneItemNeededSize
= GetOneItemNeededSize (ItemIndex
);
969 // If the total size exceeds the MaximumVariableSize, then we must use
970 // multiple variables.
972 if ((VariableNeededSize
+
974 sizeof (VARIABLE_HEADER
) +
975 StrSize (L
"PlatDriOver ")
976 ) >= MaximumVariableSize
982 VariableNeededSize
+= OneItemNeededSize
;
983 ItemIndex
= GetNextNode (MappingDataBase
, ItemIndex
);
987 if (VariableNeededSize
== sizeof (UINT32
)) {
989 // If an individual EFI variable cannot contain a single Item, return error
991 return EFI_OUT_OF_RESOURCES
;
996 // VariableNeededSize is the most proper variable size, allocate variable buffer
997 // ItemIndex now points to the next PLATFORM_OVERRIDE_ITEM which is not covered by VariableNeededSize
999 VariableBuffer
= AllocateZeroPool (VariableNeededSize
);
1000 ASSERT (VariableBuffer
!= NULL
);
1001 ASSERT ((UINTN
) VariableBuffer
% sizeof(UINTN
) == 0);
1004 // Fill the variable buffer according to MappingDataBase
1006 VariableIndex
= VariableBuffer
;
1007 *(UINT32
*) VariableIndex
= NotEnd
;
1008 VariableIndex
+= sizeof (UINT32
); // pass NotEnd
1010 // ItemIndex points to the next PLATFORM_OVERRIDE_ITEM which is not covered by VariableNeededSize
1012 while (OverrideItemListIndex
!= ItemIndex
){
1013 *(UINT32
*) VariableIndex
= PLATFORM_OVERRIDE_ITEM_SIGNATURE
;
1014 VariableIndex
+= sizeof (UINT32
); // pass SIGNATURE
1016 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1017 *(UINT32
*) VariableIndex
= OverrideItem
->DriverInfoNum
;
1018 VariableIndex
+= sizeof (UINT32
); // pass DriverNum
1020 CopyMem (VariableIndex
, OverrideItem
->ControllerDevicePath
, GetDevicePathSize (OverrideItem
->ControllerDevicePath
));
1021 VariableIndex
+= GetDevicePathSize (OverrideItem
->ControllerDevicePath
); // pass ControllerDevicePath
1024 // Align the VariableIndex since the controller device path may not be aligned
1026 VariableIndex
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
1028 // Save the Driver Info List of this PLATFORM_OVERRIDE_ITEM
1030 ImageInfoListIndex
= GetFirstNode (&OverrideItem
->DriverInfoList
);
1031 while (!IsNull (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
)) {
1032 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1033 CopyMem (VariableIndex
, DriverImageInfo
->DriverImagePath
, GetDevicePathSize (DriverImageInfo
->DriverImagePath
));
1034 VariableIndex
+= GetDevicePathSize (DriverImageInfo
->DriverImagePath
); // pass DriverImageDevicePath
1036 // Align the VariableIndex since the driver image device path may not be aligned
1038 VariableIndex
+= ((sizeof(UINT32
) - ((UINTN
) (VariableIndex
))) & (sizeof(UINT32
) - 1));
1039 ImageInfoListIndex
= GetNextNode (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
);
1042 OverrideItemListIndex
= GetNextNode (MappingDataBase
, OverrideItemListIndex
);
1045 ASSERT (((UINTN
)VariableIndex
- (UINTN
)VariableBuffer
) == VariableNeededSize
);
1047 if (NumIndex
== 0) {
1048 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver");
1050 UnicodeSPrint (OverrideVariableName
, sizeof (OverrideVariableName
), L
"PlatDriOver%d", NumIndex
);
1053 Status
= gRT
->SetVariable (
1054 OverrideVariableName
,
1056 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_NON_VOLATILE
,
1060 ASSERT (!EFI_ERROR(Status
));
1063 FreePool (VariableBuffer
);
1070 Get the first Binding protocol which has the specific image handle.
1072 @param ImageHandle The Image handle
1073 @param BindingHandle The BindingHandle of the found Driver Binding protocol.
1074 If Binding protocol is not found, it is set to NULL.
1076 @return Pointer into the Binding Protocol interface
1077 @retval NULL The paramter is not valid or the binding protocol is not found.
1080 EFI_DRIVER_BINDING_PROTOCOL
*
1082 GetBindingProtocolFromImageHandle (
1083 IN EFI_HANDLE ImageHandle
,
1084 OUT EFI_HANDLE
*BindingHandle
1089 UINTN DriverBindingHandleCount
;
1090 EFI_HANDLE
*DriverBindingHandleBuffer
;
1091 EFI_DRIVER_BINDING_PROTOCOL
*DriverBindingInterface
;
1093 if (BindingHandle
== NULL
|| ImageHandle
== NULL
) {
1097 // Get all drivers which support driver binding protocol
1099 DriverBindingHandleCount
= 0;
1100 Status
= gBS
->LocateHandleBuffer (
1102 &gEfiDriverBindingProtocolGuid
,
1104 &DriverBindingHandleCount
,
1105 &DriverBindingHandleBuffer
1107 if (EFI_ERROR (Status
) || (DriverBindingHandleCount
== 0)) {
1111 for (Index
= 0; Index
< DriverBindingHandleCount
; Index
++) {
1112 DriverBindingInterface
= NULL
;
1113 Status
= gBS
->OpenProtocol (
1114 DriverBindingHandleBuffer
[Index
],
1115 &gEfiDriverBindingProtocolGuid
,
1116 (VOID
**) &DriverBindingInterface
,
1119 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1121 if (EFI_ERROR (Status
)) {
1125 if (DriverBindingInterface
->ImageHandle
== ImageHandle
) {
1126 *BindingHandle
= DriverBindingHandleBuffer
[Index
];
1127 FreePool (DriverBindingHandleBuffer
);
1128 return DriverBindingInterface
;
1133 // If no Driver Binding Protocol instance is found
1135 FreePool (DriverBindingHandleBuffer
);
1136 *BindingHandle
= NULL
;
1141 Return the current TPL.
1153 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
1154 gBS
->RestoreTPL (Tpl
);
1161 Retrieves the image handle of the platform override driver for a controller in
1162 the system from the memory mapping database.
1164 @param ControllerHandle The device handle of the controller to check if
1165 a driver override exists.
1166 @param DriverImageHandle On input, the previously returnd driver image handle.
1167 On output, a pointer to the next driver handle.
1168 Passing in a pointer to NULL, will return the
1169 first driver handle for ControllerHandle.
1170 @param MappingDataBase Mapping database list entry pointer
1171 @param CallerImageHandle The caller driver's image handle, for
1172 UpdateFvFileDevicePath use.
1174 @retval EFI_INVALID_PARAMETER The handle specified by ControllerHandle is not
1175 a valid handle. Or DriverImagePath is not a
1176 device path that was returned on a previous call
1178 @retval EFI_NOT_FOUND A driver override for ControllerHandle was not
1180 @retval EFI_UNSUPPORTED The operation is not supported.
1181 @retval EFI_SUCCESS The driver override for ControllerHandle was
1182 returned in DriverImagePath.
1187 GetDriverFromMapping (
1188 IN EFI_HANDLE ControllerHandle
,
1189 IN OUT EFI_HANDLE
*DriverImageHandle
,
1190 IN LIST_ENTRY
*MappingDataBase
,
1191 IN EFI_HANDLE CallerImageHandle
1195 EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
;
1196 BOOLEAN ControllerFound
;
1198 EFI_HANDLE
*ImageHandleBuffer
;
1199 UINTN ImageHandleCount
;
1201 EFI_DRIVER_BINDING_PROTOCOL
*DriverBinding
;
1202 EFI_HANDLE DriverBindingHandle
;
1203 BOOLEAN FoundLastReturned
;
1204 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1205 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1206 LIST_ENTRY
*OverrideItemListIndex
;
1207 LIST_ENTRY
*ImageInfoListIndex
;
1208 EFI_DEVICE_PATH_PROTOCOL
*TempDriverImagePath
;
1209 EFI_HANDLE ImageHandle
;
1211 EFI_DEVICE_PATH_PROTOCOL
*LoadedImageDevicePath
;
1212 EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL
*BusSpecificDriverOverride
;
1213 UINTN DevicePathSize
;
1216 // Check that ControllerHandle is a valid handle
1218 if (ControllerHandle
== NULL
) {
1219 return EFI_INVALID_PARAMETER
;
1222 // Get the device path of ControllerHandle
1224 Status
= gBS
->HandleProtocol (
1226 &gEfiDevicePathProtocolGuid
,
1227 (VOID
**) &ControllerDevicePath
1229 if (EFI_ERROR (Status
) || ControllerDevicePath
== NULL
) {
1230 return EFI_INVALID_PARAMETER
;
1234 // Search ControllerDevicePath in MappingDataBase
1236 OverrideItem
= NULL
;
1237 ControllerFound
= FALSE
;
1238 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1240 OverrideItemListIndex
= GetFirstNode (MappingDataBase
);
1241 while (!IsNull (MappingDataBase
, OverrideItemListIndex
)) {
1242 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1243 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1245 ControllerDevicePath
,
1246 OverrideItem
->ControllerDevicePath
,
1250 ControllerFound
= TRUE
;
1254 OverrideItemListIndex
= GetNextNode (MappingDataBase
, OverrideItemListIndex
);
1257 if (!ControllerFound
) {
1258 return EFI_NOT_FOUND
;
1261 // Passing in a pointer to NULL, will return the first driver device path for ControllerHandle.
1262 // Check whether the driverImagePath is not a device path that was returned on a previous call to GetDriverPath().
1264 if (*DriverImageHandle
!= NULL
) {
1265 if (*DriverImageHandle
!= OverrideItem
->LastReturnedImageHandle
) {
1266 return EFI_INVALID_PARAMETER
;
1270 // The GetDriverPath() may be called recursively, because it use ConnectDevicePath() internally,
1271 // so should check whether there is a dead loop.
1272 // Here use a controller device path stack to record all processed controller device path during a GetDriverPath() call,
1273 // and check the controller device path whether appear again during the GetDriverPath() call.
1275 if (CheckExistInStack (OverrideItem
->ControllerDevicePath
)) {
1277 // There is a dependecy dead loop if the ControllerDevicePath appear in stack twice
1279 return EFI_UNSUPPORTED
;
1281 PushDevPathStack (OverrideItem
->ControllerDevicePath
);
1284 // Check every override driver, try to load and start them
1286 ImageInfoListIndex
= GetFirstNode (&OverrideItem
->DriverInfoList
);
1287 while (!IsNull (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
)) {
1288 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1289 if (DriverImageInfo
->ImageHandle
== NULL
) {
1291 // Skip if the image is unloadable or unstartable
1293 if ((!DriverImageInfo
->UnLoadable
) && ((!DriverImageInfo
->UnStartable
))) {
1294 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
1296 // If the image device path contains an FV node, check the FV file device path is valid.
1297 // If it is invalid, try to return the valid device path.
1298 // FV address maybe changes for memory layout adjust from time to time,
1299 // use this funciton could promise the FV file device path is right.
1301 Status
= UpdateFvFileDevicePath (&TempDriverImagePath
, NULL
, CallerImageHandle
);
1302 if (!EFI_ERROR (Status
)) {
1303 FreePool (DriverImageInfo
->DriverImagePath
);
1304 DriverImageInfo
->DriverImagePath
= TempDriverImagePath
;
1307 // Get all Loaded Image protocol to check whether the driver image has been loaded and started
1310 ImageHandleCount
= 0;
1311 Status
= gBS
->LocateHandleBuffer (
1313 &gEfiLoadedImageProtocolGuid
,
1318 if (EFI_ERROR (Status
) || (ImageHandleCount
== 0)) {
1319 return EFI_NOT_FOUND
;
1322 for(Index
= 0; Index
< ImageHandleCount
; Index
++) {
1324 // Get the EFI Loaded Image Device Path Protocol
1326 LoadedImageDevicePath
= NULL
;
1327 Status
= gBS
->HandleProtocol (
1328 ImageHandleBuffer
[Index
],
1329 &gEfiLoadedImageDevicePathProtocolGuid
,
1330 (VOID
**) &LoadedImageDevicePath
1332 if (EFI_ERROR (Status
)) {
1334 // Maybe not all EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL existed.
1339 DevicePathSize
= GetDevicePathSize (DriverImageInfo
->DriverImagePath
);
1340 if (DevicePathSize
== GetDevicePathSize (LoadedImageDevicePath
)) {
1342 DriverImageInfo
->DriverImagePath
,
1343 LoadedImageDevicePath
,
1344 GetDevicePathSize (LoadedImageDevicePath
)
1355 // Find its related driver binding protocol
1356 // Driver binding handle may be different with its driver's Image Handle.
1358 DriverBindingHandle
= NULL
;
1359 DriverBinding
= GetBindingProtocolFromImageHandle (
1360 ImageHandleBuffer
[Index
],
1361 &DriverBindingHandle
1363 ASSERT (DriverBinding
!= NULL
);
1364 DriverImageInfo
->ImageHandle
= ImageHandleBuffer
[Index
];
1365 } else if (GetCurrentTpl() <= TPL_CALLBACK
){
1367 // The driver image has not been loaded and started. Try to load and start it now.
1368 // Try to connect all device in the driver image path.
1370 // Note: LoadImage() and StartImage() should be called under CALLBACK TPL in theory, but
1371 // since many device need to be connected in CALLBACK level environment( e.g. Usb devices )
1372 // and the Fat and Patition driver can endure executing in CALLBACK level in fact, so here permit
1373 // to use LoadImage() and StartImage() in CALLBACK TPL.
1375 Status
= ConnectDevicePath (DriverImageInfo
->DriverImagePath
);
1377 // check whether it points to a PCI Option Rom image,
1378 // and try to use bus override protocol to get its first option rom image driver
1380 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
1381 gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &TempDriverImagePath
, &Handle
);
1383 // Get the Bus Specific Driver Override Protocol instance on the Controller Handle
1385 Status
= gBS
->HandleProtocol(
1387 &gEfiBusSpecificDriverOverrideProtocolGuid
,
1388 (VOID
**) &BusSpecificDriverOverride
1390 if (!EFI_ERROR (Status
) && (BusSpecificDriverOverride
!= NULL
)) {
1392 Status
= BusSpecificDriverOverride
->GetDriver (
1393 BusSpecificDriverOverride
,
1396 if (!EFI_ERROR (Status
)) {
1398 // Find its related driver binding protocol
1399 // Driver binding handle may be different with its driver's Image handle
1401 DriverBindingHandle
= NULL
;
1402 DriverBinding
= GetBindingProtocolFromImageHandle (
1404 &DriverBindingHandle
1406 ASSERT (DriverBinding
!= NULL
);
1407 DriverImageInfo
->ImageHandle
= ImageHandle
;
1411 // Skip if any device cannot be connected now, future passes through GetDriver() may be able to load that driver.
1412 // Only file path media or FwVol Device Path Node remain if all device is connected
1414 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
1415 gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &TempDriverImagePath
, &Handle
);
1416 if (((DevicePathType (TempDriverImagePath
) == MEDIA_DEVICE_PATH
) &&
1417 (DevicePathSubType (TempDriverImagePath
) == MEDIA_FILEPATH_DP
)) ||
1418 (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) TempDriverImagePath
) != NULL
)
1421 // Try to load the driver
1423 TempDriverImagePath
= DriverImageInfo
->DriverImagePath
;
1424 Status
= gBS
->LoadImage (
1427 TempDriverImagePath
,
1432 if (!EFI_ERROR (Status
)) {
1434 // Try to start the driver
1436 Status
= gBS
->StartImage (ImageHandle
, NULL
, NULL
);
1437 if (EFI_ERROR (Status
)){
1438 DriverImageInfo
->UnStartable
= TRUE
;
1439 DriverImageInfo
->ImageHandle
= NULL
;
1442 // Find its related driver binding protocol
1443 // Driver binding handle may be different with its driver's Image handle
1445 DriverBindingHandle
= NULL
;
1446 DriverBinding
= GetBindingProtocolFromImageHandle (
1448 &DriverBindingHandle
1450 ASSERT (DriverBinding
!= NULL
);
1451 DriverImageInfo
->ImageHandle
= ImageHandle
;
1454 DriverImageInfo
->UnLoadable
= TRUE
;
1455 DriverImageInfo
->ImageHandle
= NULL
;
1459 FreePool (ImageHandleBuffer
);
1462 ImageInfoListIndex
= GetNextNode (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
);
1465 // Finish try to load and start the override driver of a controller, popup the controller's device path
1467 PopDevPathStack (NULL
);
1470 // return the DriverImageHandle for ControllerHandle
1472 FoundLastReturned
= FALSE
;
1473 ImageInfoListIndex
= GetFirstNode (&OverrideItem
->DriverInfoList
);
1474 while (!IsNull (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
)) {
1475 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1476 if (DriverImageInfo
->ImageHandle
!= NULL
) {
1477 if ((*DriverImageHandle
== NULL
) || FoundLastReturned
) {
1479 // If DriverImageHandle is NULL, then we just need to return the first driver.
1480 // If FoundLastReturned, this means we have just encountered the previously returned driver.
1481 // For both cases, we just return the image handle of this driver.
1483 OverrideItem
->LastReturnedImageHandle
= DriverImageInfo
->ImageHandle
;
1484 *DriverImageHandle
= DriverImageInfo
->ImageHandle
;
1486 } else if (*DriverImageHandle
== DriverImageInfo
->ImageHandle
){
1488 // We have found the previously returned driver.
1490 FoundLastReturned
= TRUE
;
1493 ImageInfoListIndex
= GetNextNode (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
);
1496 return EFI_NOT_FOUND
;
1501 Check mapping database whether already has the mapping info which
1502 records the input Controller to input DriverImage.
1504 @param ControllerDevicePath The controller device path is to be check.
1505 @param DriverImageDevicePath The driver image device path is to be check.
1506 @param MappingDataBase Mapping database list entry pointer
1507 @param DriverInfoNum the controller's total override driver number
1508 @param DriverImageNO The driver order number for the input DriverImage.
1509 If the DriverImageDevicePath is NULL, DriverImageNO is not set.
1511 @retval EFI_INVALID_PARAMETER ControllerDevicePath or MappingDataBase is NULL.
1512 @retval EFI_NOT_FOUND ControllerDevicePath is not found in MappingDataBase or
1513 DriverImageDevicePath is not found in the found DriverImage Info list.
1514 @retval EFI_SUCCESS The controller's total override driver number and
1515 input DriverImage's order number is correctly return.
1520 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
1521 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath OPTIONAL
,
1522 IN LIST_ENTRY
*MappingDataBase
,
1523 OUT UINT32
*DriverInfoNum OPTIONAL
,
1524 OUT UINT32
*DriverImageNO OPTIONAL
1527 LIST_ENTRY
*OverrideItemListIndex
;
1528 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1529 LIST_ENTRY
*ImageInfoListIndex
;
1530 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1533 UINTN DevicePathSize
;
1535 if (ControllerDevicePath
== NULL
) {
1536 return EFI_INVALID_PARAMETER
;
1538 if (MappingDataBase
== NULL
) {
1539 return EFI_INVALID_PARAMETER
;
1543 // Search ControllerDevicePath in MappingDataBase
1546 OverrideItem
= NULL
;
1547 OverrideItemListIndex
= GetFirstNode (MappingDataBase
);
1548 while (!IsNull (MappingDataBase
, OverrideItemListIndex
)) {
1549 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1550 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1551 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1553 ControllerDevicePath
,
1554 OverrideItem
->ControllerDevicePath
,
1562 OverrideItemListIndex
= GetNextNode (MappingDataBase
, OverrideItemListIndex
);
1567 // ControllerDevicePath is not in MappingDataBase
1569 return EFI_NOT_FOUND
;
1572 ASSERT (OverrideItem
->DriverInfoNum
!= 0);
1573 if (DriverInfoNum
!= NULL
) {
1574 *DriverInfoNum
= OverrideItem
->DriverInfoNum
;
1578 // If DriverImageDevicePath is NULL, skip checking DriverImageDevicePath
1579 // in the controller's Driver Image Info List
1581 if (DriverImageDevicePath
== NULL
) {
1585 // return the DriverImageHandle for ControllerHandle
1589 ImageInfoListIndex
= GetFirstNode (&OverrideItem
->DriverInfoList
);
1590 while (!IsNull (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
)) {
1591 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1593 DevicePathSize
= GetDevicePathSize (DriverImageDevicePath
);
1594 if (DevicePathSize
== GetDevicePathSize (DriverImageInfo
->DriverImagePath
)) {
1596 DriverImageDevicePath
,
1597 DriverImageInfo
->DriverImagePath
,
1598 GetDevicePathSize (DriverImageInfo
->DriverImagePath
)
1605 ImageInfoListIndex
= GetNextNode (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
);
1610 // DriverImageDevicePath is not found in the controller's Driver Image Info List
1612 return EFI_NOT_FOUND
;
1614 if (DriverImageNO
!= NULL
) {
1615 *DriverImageNO
= ImageNO
;
1623 Insert a driver image as a controller's override driver into the mapping database.
1624 The driver image's order number is indicated by DriverImageNO.
1626 @param ControllerDevicePath The controller device path need to add a
1627 override driver image item
1628 @param DriverImageDevicePath The driver image device path need to be insert
1629 @param MappingDataBase Mapping database list entry pointer
1630 @param DriverImageNO The inserted order number. If this number is taken,
1631 the larger available number will be used.
1633 @retval EFI_INVALID_PARAMETER ControllerDevicePath is NULL, or DriverImageDevicePath is NULL
1634 or MappingDataBase is NULL
1635 @retval EFI_ALREADY_STARTED The input Controller to input DriverImage has been
1636 recorded into the mapping database.
1637 @retval EFI_SUCCESS The Controller and DriverImage are inserted into
1638 the mapping database successfully.
1644 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
1645 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
1646 IN LIST_ENTRY
*MappingDataBase
,
1647 IN UINT32 DriverImageNO
1651 LIST_ENTRY
*OverrideItemListIndex
;
1652 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1653 LIST_ENTRY
*ImageInfoListIndex
;
1654 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1657 UINTN DevicePathSize
;
1659 if (ControllerDevicePath
== NULL
) {
1660 return EFI_INVALID_PARAMETER
;
1662 if (DriverImageDevicePath
== NULL
) {
1663 return EFI_INVALID_PARAMETER
;
1665 if (MappingDataBase
== NULL
) {
1666 return EFI_INVALID_PARAMETER
;
1670 // If the driver is already in the controller's Driver Image Info List,
1671 // just return EFI_ALREADY_STARTED.
1673 Status
= CheckMapping (
1674 ControllerDevicePath
,
1675 DriverImageDevicePath
,
1680 if (Status
== EFI_SUCCESS
) {
1681 return EFI_ALREADY_STARTED
;
1685 // Search the input ControllerDevicePath in MappingDataBase
1688 OverrideItem
= NULL
;
1689 OverrideItemListIndex
= GetFirstNode (MappingDataBase
);
1690 while (!IsNull (MappingDataBase
, OverrideItemListIndex
)) {
1691 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1692 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1693 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1695 ControllerDevicePath
,
1696 OverrideItem
->ControllerDevicePath
,
1704 OverrideItemListIndex
= GetNextNode (MappingDataBase
, OverrideItemListIndex
);
1707 // If cannot find, this is a new controller item
1708 // Add the Controller related PLATFORM_OVERRIDE_ITEM structrue in mapping data base
1711 OverrideItem
= AllocateZeroPool (sizeof (PLATFORM_OVERRIDE_ITEM
));
1712 ASSERT (OverrideItem
!= NULL
);
1713 OverrideItem
->Signature
= PLATFORM_OVERRIDE_ITEM_SIGNATURE
;
1714 OverrideItem
->ControllerDevicePath
= DuplicateDevicePath (ControllerDevicePath
);
1715 InitializeListHead (&OverrideItem
->DriverInfoList
);
1716 InsertTailList (MappingDataBase
, &OverrideItem
->Link
);
1720 // Prepare the driver image related DRIVER_IMAGE_INFO structure.
1722 DriverImageInfo
= AllocateZeroPool (sizeof (DRIVER_IMAGE_INFO
));
1723 ASSERT (DriverImageInfo
!= NULL
);
1724 DriverImageInfo
->Signature
= DRIVER_IMAGE_INFO_SIGNATURE
;
1725 DriverImageInfo
->DriverImagePath
= DuplicateDevicePath (DriverImageDevicePath
);
1727 // Find the driver image wanted order location
1731 ImageInfoListIndex
= GetFirstNode (&OverrideItem
->DriverInfoList
);
1732 while (!IsNull (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
)) {
1733 if (ImageNO
== (DriverImageNO
- 1)) {
1735 // find the wanted order location, insert it
1737 InsertTailList (ImageInfoListIndex
, &DriverImageInfo
->Link
);
1738 OverrideItem
->DriverInfoNum
++;
1743 ImageInfoListIndex
= GetNextNode (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
);
1748 // if not find the wanted order location, add it as last item of the controller mapping item
1750 InsertTailList (&OverrideItem
->DriverInfoList
, &DriverImageInfo
->Link
);
1751 OverrideItem
->DriverInfoNum
++;
1759 Delete a controller's override driver from the mapping database.
1761 @param ControllerDevicePath The controller device path will be deleted
1762 when all drivers images on it are removed.
1763 @param DriverImageDevicePath The driver image device path will be delete.
1764 If NULL, all driver image will be delete.
1765 @param MappingDataBase Mapping database list entry pointer
1767 @retval EFI_INVALID_PARAMETER ControllerDevicePath is NULL, or MappingDataBase is NULL
1768 @retval EFI_NOT_FOUND ControllerDevicePath is not found in MappingDataBase or
1769 DriverImageDevicePath is not found in the found DriverImage Info list.
1770 @retval EFI_SUCCESS Delete the specified driver successfully.
1776 IN EFI_DEVICE_PATH_PROTOCOL
*ControllerDevicePath
,
1777 IN EFI_DEVICE_PATH_PROTOCOL
*DriverImageDevicePath
,
1778 IN LIST_ENTRY
*MappingDataBase
1782 LIST_ENTRY
*OverrideItemListIndex
;
1783 PLATFORM_OVERRIDE_ITEM
*OverrideItem
;
1784 LIST_ENTRY
*ImageInfoListIndex
;
1785 DRIVER_IMAGE_INFO
*DriverImageInfo
;
1787 UINTN DevicePathSize
;
1789 if (ControllerDevicePath
== NULL
) {
1790 return EFI_INVALID_PARAMETER
;
1793 if (MappingDataBase
== NULL
) {
1794 return EFI_INVALID_PARAMETER
;
1798 // If ControllerDevicePath is not found in mapping database, return EFI_NOT_FOUND.
1800 Status
= CheckMapping (
1801 ControllerDevicePath
,
1802 DriverImageDevicePath
,
1807 if (EFI_ERROR (Status
)) {
1808 return EFI_NOT_FOUND
;
1812 // Search ControllerDevicePath in MappingDataBase
1815 OverrideItem
= NULL
;
1816 OverrideItemListIndex
= GetFirstNode (MappingDataBase
);
1817 while (!IsNull (MappingDataBase
, OverrideItemListIndex
)) {
1818 OverrideItem
= CR(OverrideItemListIndex
, PLATFORM_OVERRIDE_ITEM
, Link
, PLATFORM_OVERRIDE_ITEM_SIGNATURE
);
1819 DevicePathSize
= GetDevicePathSize (ControllerDevicePath
);
1820 if (DevicePathSize
== GetDevicePathSize (OverrideItem
->ControllerDevicePath
)) {
1822 ControllerDevicePath
,
1823 OverrideItem
->ControllerDevicePath
,
1831 OverrideItemListIndex
= GetNextNode (MappingDataBase
, OverrideItemListIndex
);
1835 ASSERT (OverrideItem
->DriverInfoNum
!= 0);
1838 ImageInfoListIndex
= GetFirstNode (&OverrideItem
->DriverInfoList
);
1839 while (!IsNull (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
)) {
1840 DriverImageInfo
= CR(ImageInfoListIndex
, DRIVER_IMAGE_INFO
, Link
, DRIVER_IMAGE_INFO_SIGNATURE
);
1841 ImageInfoListIndex
= GetNextNode (&OverrideItem
->DriverInfoList
, ImageInfoListIndex
);
1842 if (DriverImageDevicePath
!= NULL
) {
1844 // Search for the specified DriverImageDevicePath and remove it, then break.
1846 DevicePathSize
= GetDevicePathSize (DriverImageDevicePath
);
1847 if (DevicePathSize
== GetDevicePathSize (DriverImageInfo
->DriverImagePath
)) {
1849 DriverImageDevicePath
,
1850 DriverImageInfo
->DriverImagePath
,
1851 GetDevicePathSize (DriverImageInfo
->DriverImagePath
)
1855 FreePool(DriverImageInfo
->DriverImagePath
);
1856 RemoveEntryList (&DriverImageInfo
->Link
);
1857 OverrideItem
->DriverInfoNum
--;
1863 // Remove all existing driver image info entries, so no break here.
1866 FreePool(DriverImageInfo
->DriverImagePath
);
1867 RemoveEntryList (&DriverImageInfo
->Link
);
1868 OverrideItem
->DriverInfoNum
--;
1873 // Confirm all driver image info entries have been removed,
1874 // if DriverImageDevicePath is NULL.
1876 if (DriverImageDevicePath
== NULL
) {
1877 ASSERT (OverrideItem
->DriverInfoNum
== 0);
1880 // If Override Item has no driver image info entry, then delete this item.
1882 if (OverrideItem
->DriverInfoNum
== 0) {
1883 FreePool(OverrideItem
->ControllerDevicePath
);
1884 RemoveEntryList (&OverrideItem
->Link
);
1885 FreePool (OverrideItem
);
1890 // DriverImageDevicePath is not NULL and cannot be found in the controller's
1891 // driver image info list.
1893 return EFI_NOT_FOUND
;