]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxePlatDriOverLib/PlatDriOverLib.c
1. Fixed bugs in DxeNetLib to meet consistence with network module DriverBinding...
[mirror_edk2.git] / MdeModulePkg / Library / DxePlatDriOverLib / PlatDriOverLib.c
1 /** @file
2
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
8
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.
11
12 Module Name:
13
14 PlatDriOverLib.c
15
16 Abstract:
17
18
19 **/
20
21 #include "PlatDriOver.h"
22
23 STATIC LIST_ENTRY mDevicePathStack = INITIALIZE_LIST_HEAD_VARIABLE (mDevicePathStack);
24
25
26 /**
27 Install the Platform Driver Override Protocol, and ensure there is only one Platform Driver Override Protocol
28 in the system.
29
30 @param gPlatformDriverOverride PlatformDriverOverride protocol interface which
31 needs to be installed
32
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
36
37 **/
38 EFI_STATUS
39 EFIAPI
40 InstallPlatformDriverOverrideProtocol (
41 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL *gPlatformDriverOverride
42 )
43 {
44 EFI_HANDLE Handle;
45 EFI_STATUS Status;
46 UINTN HandleCount;
47 EFI_HANDLE *HandleBuffer;
48
49 //
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.
53 //
54 Status = gBS->LocateHandleBuffer (
55 ByProtocol,
56 &gEfiPlatformDriverOverrideProtocolGuid,
57 NULL,
58 &HandleCount,
59 &HandleBuffer
60 );
61 //
62 // If there was no error, assume there is an installation and return error
63 //
64 if (!EFI_ERROR (Status)) {
65 if (HandleBuffer != NULL) {
66 FreePool (HandleBuffer);
67 }
68 return EFI_ALREADY_STARTED;
69 }
70
71 //
72 // Install platform driver override protocol
73 //
74 Handle = NULL;
75 Status = gBS->InstallProtocolInterface (
76 &Handle,
77 &gEfiPlatformDriverOverrideProtocolGuid,
78 EFI_NATIVE_INTERFACE,
79 gPlatformDriverOverride
80 );
81 return Status;
82 }
83
84
85 /**
86 Free all the mapping database memory resource and initialize the mapping list entry
87
88 @param MappingDataBase Mapping database list entry pointer
89
90 @retval EFI_INVALID_PARAMETER mapping database list entry is NULL
91 @retval EFI_SUCCESS Free success
92
93 **/
94 EFI_STATUS
95 EFIAPI
96 FreeMappingDatabase (
97 IN OUT LIST_ENTRY *MappingDataBase
98 )
99 {
100 LIST_ENTRY *OverrideItemListIndex;
101 LIST_ENTRY *ImageInfoListIndex;
102 PLATFORM_OVERRIDE_ITEM *OverrideItem;
103 DRIVER_IMAGE_INFO *DriverImageInfo;
104
105 if (MappingDataBase == NULL) {
106 return EFI_INVALID_PARAMETER;
107 }
108
109 OverrideItemListIndex = MappingDataBase->ForwardLink;
110 while (OverrideItemListIndex != MappingDataBase){
111 OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);
112 //
113 // Free PLATFORM_OVERRIDE_ITEM.ControllerDevicePath[]
114 //
115 if (OverrideItem->ControllerDevicePath != NULL){
116 FreePool(OverrideItem->ControllerDevicePath);
117 }
118
119 ImageInfoListIndex = OverrideItem->DriverInfoList.ForwardLink;
120 while (ImageInfoListIndex != &OverrideItem->DriverInfoList){
121 //
122 // Free all DRIVER_IMAGE_INFO.DriverImagePath[]
123 //
124 DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
125 if (DriverImageInfo->DriverImagePath != NULL) {
126 FreePool(DriverImageInfo->DriverImagePath);
127 }
128 //
129 // Free DRIVER_IMAGE_INFO itself
130 //
131 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
132 RemoveEntryList (&DriverImageInfo->Link);
133 FreePool (DriverImageInfo);
134 }
135 //
136 // Free PLATFORM_OVERRIDE_ITEM itself
137 //
138 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
139 RemoveEntryList (&OverrideItem->Link);
140 FreePool (OverrideItem);
141 }
142
143 InitializeListHead (MappingDataBase);
144 return EFI_SUCCESS;
145 }
146
147
148 /**
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.
151 VariableLayout{
152 //
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, ....
157 //
158 UINT32 NotEnd;
159 //
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.
162 //
163 UINT32 SIGNATURE; //EFI_SIGNATURE_32('p','d','o','i')
164 UINT32 DriverNum;
165 EFI_DEVICE_PATH_PROTOCOL ControllerDevicePath[];
166 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
167 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
168 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
169 ......
170 UINT32 SIGNATURE;
171 UINT32 DriverNum;
172 EFI_DEVICE_PATH_PROTOCOL ControllerDevicePath[];
173 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
174 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
175 EFI_DEVICE_PATH_PROTOCOL DriverDevicePath[];
176 ......
177 }
178 typedef struct _PLATFORM_OVERRIDE_ITEM{
179 UINTN Signature; //EFI_SIGNATURE_32('p','d','o','i')
180 LIST_ENTRY Link;
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')
187 LIST_ENTRY Link;
188 EFI_HANDLE ImageHandle;
189 EFI_DEVICE_PATH_PROTOCOL *DriverImagePath;
190 BOOLEAN UnLoadable;
191 BOOLEAN UnStartable;
192 } DRIVER_IMAGE_INFO;
193
194 @param MappingDataBase Mapping database list entry pointer
195
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
200 successfully
201
202 **/
203 EFI_STATUS
204 EFIAPI
205 InitOverridesMapping (
206 OUT LIST_ENTRY *MappingDataBase
207 )
208 {
209 UINTN BufferSize;
210 VOID *VariableBuffer;
211 UINT8 *VariableIndex;
212 UINTN VariableNum;
213 CHAR16 OverrideVariableName[40];
214 UINT32 NotEnd;
215 UINT32 DriverNumber;
216 PLATFORM_OVERRIDE_ITEM *OverrideItem;
217 DRIVER_IMAGE_INFO *DriverImageInfo;
218 BOOLEAN Corrupted;
219 UINT32 Signature;
220 EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath;
221 EFI_DEVICE_PATH_PROTOCOL *DriverDevicePath;
222 UINTN Index;
223
224 if (MappingDataBase == NULL) {
225 return EFI_INVALID_PARAMETER;
226 }
227
228 VariableNum = 0;
229 Corrupted = FALSE;
230 //
231 // Check the environment variable(s) that contain the override mappings .
232 //
233 VariableBuffer = GetVariableAndSize (L"PlatDriOver", &gEfiOverrideVariableGuid, &BufferSize);
234 ASSERT ((UINTN) VariableBuffer % sizeof(UINTN) == 0);
235 VariableNum ++;
236 if (VariableBuffer == NULL) {
237 return EFI_NOT_FOUND;
238 }
239
240 do {
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);
249 //
250 // Check SIGNATURE
251 //
252 Signature = *(UINT32 *) VariableIndex;
253 VariableIndex = VariableIndex + sizeof (UINT32);
254 if (Signature != PLATFORM_OVERRIDE_ITEM_SIGNATURE) {
255 FreePool (OverrideItem);
256 Corrupted = TRUE;
257 break;
258 }
259 //
260 // Get DriverNum
261 //
262 DriverNumber = *(UINT32*) VariableIndex;
263 OverrideItem->DriverInfoNum = DriverNumber;
264 VariableIndex = VariableIndex + sizeof (UINT32);
265 //
266 // Get ControllerDevicePath[]
267 //
268 ControllerDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) VariableIndex;
269 OverrideItem->ControllerDevicePath = DuplicateDevicePath (ControllerDevicePath);
270 VariableIndex = VariableIndex + GetDevicePathSize (ControllerDevicePath);
271 //
272 // Align the VariableIndex since the controller device path may not be aligned, refer to the SaveOverridesMapping()
273 //
274 VariableIndex += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));
275
276 //
277 // Get all DriverDevicePath[]
278 //
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;
283
284 DriverDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) VariableIndex;
285 DriverImageInfo->DriverImagePath = DuplicateDevicePath (DriverDevicePath);
286 VariableIndex = VariableIndex + GetDevicePathSize (DriverDevicePath);
287 //
288 // Align the VariableIndex since the driver image device path may not be aligned, refer to the SaveOverridesMapping()
289 //
290 VariableIndex += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));
291
292 InsertTailList (&OverrideItem->DriverInfoList, &DriverImageInfo->Link);
293 }
294 InsertTailList (MappingDataBase, &OverrideItem->Link);
295 }
296
297 FreePool (VariableBuffer);
298 if (Corrupted) {
299 FreeMappingDatabase (MappingDataBase);
300 return EFI_VOLUME_CORRUPTED;
301 }
302
303 //
304 // If has other variable(PlatDriOver1, PlatDriOver2, PlatDriOver3.....), get them.
305 // NotEnd indicate whether current variable is the end variable.
306 //
307 if (NotEnd != 0) {
308 UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver%d", VariableNum);
309 VariableBuffer = GetVariableAndSize (OverrideVariableName, &gEfiOverrideVariableGuid, &BufferSize);
310 ASSERT ((UINTN) VariableBuffer % sizeof(UINTN) == 0);
311 VariableNum ++;
312 if (VariableBuffer == NULL) {
313 FreeMappingDatabase (MappingDataBase);
314 return EFI_VOLUME_CORRUPTED;
315 }
316 }
317
318 } while (NotEnd != 0);
319
320 return EFI_SUCCESS;
321 }
322
323
324 /**
325 Calculate the needed size in NV variable for recording a specific PLATFORM_OVERRIDE_ITEM info
326
327 @param OverrideItemListIndex a list entry point to a specific
328 PLATFORM_OVERRIDE_ITEM
329
330 @return The needed size number
331
332 **/
333 UINTN
334 EFIAPI
335 GetOneItemNeededSize (
336 IN LIST_ENTRY *OverrideItemListIndex
337 )
338 {
339 UINTN NeededSize;
340 PLATFORM_OVERRIDE_ITEM *OverrideItem;
341 LIST_ENTRY *ImageInfoListIndex;
342 DRIVER_IMAGE_INFO *DriverImageInfo;
343
344
345 NeededSize = 0;
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
350 //
351 // Align the controller device path
352 //
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
359 //
360 // Align the driver image device path
361 //
362 NeededSize += ((sizeof(UINT32) - ((UINTN) GetDevicePathSize (DriverImageInfo->DriverImagePath))) \
363 & (sizeof(UINT32) - 1));
364 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
365 }
366
367 return NeededSize;
368 }
369
370
371
372 /**
373 Save the memory mapping database into NV environment variable(s)
374
375 @param MappingDataBase Mapping database list entry pointer
376
377 @retval EFI_INVALID_PARAMETER MappingDataBase pointer is null
378 @retval EFI_SUCCESS Save memory mapping database successfully
379
380 **/
381 EFI_STATUS
382 EFIAPI
383 SaveOverridesMapping (
384 IN LIST_ENTRY *MappingDataBase
385 )
386 {
387 EFI_STATUS Status;
388 VOID *VariableBuffer;
389 UINT8 *VariableIndex;
390 UINTN NumIndex;
391 CHAR16 OverrideVariableName[40];
392 UINT32 NotEnd;
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;
399 UINTN SavedSize;
400 UINT64 MaximumVariableStorageSize;
401 UINT64 RemainingVariableStorageSize;
402 UINT64 MaximumVariableSize;
403 UINTN OneItemNeededSize;
404
405 if (MappingDataBase == NULL) {
406 return EFI_INVALID_PARAMETER;
407 }
408
409 if (MappingDataBase->ForwardLink == MappingDataBase) {
410 Status = DeleteOverridesVariables ();
411 return EFI_SUCCESS;
412 }
413
414 //
415 // Get the the maximum size of an individual EFI variable in current system
416 //
417 gRT->QueryVariableInfo (
418 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
419 &MaximumVariableStorageSize,
420 &RemainingVariableStorageSize,
421 &MaximumVariableSize
422 );
423
424 NumIndex = 0;
425 OverrideItemListIndex = MappingDataBase->ForwardLink;
426 while (OverrideItemListIndex != MappingDataBase) {
427 //
428 // Try to find the most proper variable size which <= MaximumVariableSize, but can contain mapping info as much as possible
429 //
430 VariableNeededSize = 0;
431 VariableNeededSize += sizeof (UINT32); //BOOLEAN NotEnd;
432 ItemIndex = OverrideItemListIndex;
433 NotEnd = FALSE;
434
435 while (ItemIndex != MappingDataBase){
436 OneItemNeededSize = GetOneItemNeededSize (ItemIndex);
437 if ((VariableNeededSize +
438 OneItemNeededSize +
439 sizeof (VARIABLE_HEADER) +
440 StrSize (L"PlatDriOver ")
441 ) >= MaximumVariableSize
442 ) {
443 NotEnd = TRUE;
444 break;
445 }
446
447 VariableNeededSize += GetOneItemNeededSize (ItemIndex);
448 ItemIndex = ItemIndex->ForwardLink;
449 }
450
451 if (NotEnd) {
452 if (VariableNeededSize == sizeof (UINT32)) {
453 //
454 // If an individual EFI variable cannot contain a single Item, return error
455 //
456 return EFI_OUT_OF_RESOURCES;
457 }
458 }
459
460 //
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
463 //
464 VariableBuffer = AllocateZeroPool (VariableNeededSize);
465 ASSERT ((UINTN) VariableBuffer % sizeof(UINTN) == 0);
466
467 //
468 // Fill the variable buffer according to MappingDataBase
469 //
470 SavedSize = 0;
471 VariableIndex = VariableBuffer;
472 *(UINT32 *) VariableIndex = NotEnd;
473 VariableIndex += sizeof (UINT32); // pass NoEnd
474 SavedSize += sizeof (UINT32);
475 //
476 // ItemIndex points to the next PLATFORM_OVERRIDE_ITEM which is not covered by VariableNeededSize
477 //
478 while (OverrideItemListIndex != ItemIndex){
479 *(UINT32 *) VariableIndex = PLATFORM_OVERRIDE_ITEM_SIGNATURE;
480 VariableIndex += sizeof (UINT32); // pass SIGNATURE
481 SavedSize += sizeof (UINT32);
482
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);
487
488 CopyMem (VariableIndex, OverrideItem->ControllerDevicePath, GetDevicePathSize (OverrideItem->ControllerDevicePath));
489 VariableIndex += GetDevicePathSize (OverrideItem->ControllerDevicePath); // pass ControllerDevicePath
490 SavedSize += GetDevicePathSize (OverrideItem->ControllerDevicePath);
491
492 //
493 // Align the VariableIndex since the controller device path may not be aligned
494 //
495 SavedSize += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));
496 VariableIndex += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));
497
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);
504 //
505 // Align the VariableIndex since the driver image device path may not be aligned
506 //
507 SavedSize += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));
508 VariableIndex += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));
509 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
510 }
511
512 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
513 }
514
515 ASSERT (SavedSize == VariableNeededSize);
516
517 if (NumIndex == 0) {
518 UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver");
519 } else {
520 UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver%d", NumIndex );
521 }
522
523 Status = gRT->SetVariable (
524 OverrideVariableName,
525 &gEfiOverrideVariableGuid,
526 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
527 VariableNeededSize,
528 VariableBuffer
529 );
530 ASSERT (!EFI_ERROR(Status));
531
532 NumIndex ++;
533 FreePool (VariableBuffer);
534 }
535
536 return EFI_SUCCESS;
537 }
538
539 /**
540 Get the first Binding protocol which has the specific image handle
541
542 @param Image Image handle
543
544 @return Pointer into the Binding Protocol interface
545
546 **/
547 EFI_DRIVER_BINDING_PROTOCOL *
548 EFIAPI
549 GetBindingProtocolFromImageHandle (
550 IN EFI_HANDLE ImageHandle,
551 OUT EFI_HANDLE *BindingHandle
552 )
553 {
554 EFI_STATUS Status;
555 UINTN Index;
556 UINTN DriverBindingHandleCount;
557 EFI_HANDLE *DriverBindingHandleBuffer;
558 EFI_DRIVER_BINDING_PROTOCOL *DriverBindingInterface;
559
560 if (BindingHandle == NULL || ImageHandle == NULL) {
561 return NULL;
562 }
563 //
564 // Get all driver which support binding protocol in second page
565 //
566 DriverBindingHandleCount = 0;
567 Status = gBS->LocateHandleBuffer (
568 ByProtocol,
569 &gEfiDriverBindingProtocolGuid,
570 NULL,
571 &DriverBindingHandleCount,
572 &DriverBindingHandleBuffer
573 );
574 if (EFI_ERROR (Status) || (DriverBindingHandleCount == 0)) {
575 return NULL;
576 }
577
578 for (Index = 0; Index < DriverBindingHandleCount; Index++) {
579 DriverBindingInterface =NULL;
580 Status = gBS->OpenProtocol (
581 DriverBindingHandleBuffer[Index],
582 &gEfiDriverBindingProtocolGuid,
583 (VOID **) &DriverBindingInterface,
584 NULL,
585 NULL,
586 EFI_OPEN_PROTOCOL_GET_PROTOCOL
587 );
588 if (EFI_ERROR (Status)) {
589 continue;
590 }
591
592 if (DriverBindingInterface->ImageHandle == ImageHandle) {
593 *BindingHandle = DriverBindingHandleBuffer[Index];
594 FreePool (DriverBindingHandleBuffer);
595 return DriverBindingInterface;
596 }
597 }
598
599 FreePool (DriverBindingHandleBuffer);
600 *BindingHandle = NULL;
601 return NULL;
602 }
603
604 /**
605 return the current TPL, copied from the EDKII glue lib
606
607 @param VOID
608
609 @return Current TPL
610
611 **/
612 EFI_TPL
613 GetCurrentTpl (
614 VOID
615 )
616 {
617 EFI_TPL Tpl;
618
619 Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
620 gBS->RestoreTPL (Tpl);
621
622 return Tpl;
623 }
624
625
626 /**
627 Retrieves the image handle of the platform override driver for a controller in the system from the memory mapping database.
628
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
637 pointer
638 @param CallerImageHandle The caller driver's image handle, for
639 UpdateFvFileDevicePath use.
640
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
644 to GetDriverPath().
645 @retval EFI_NOT_FOUND A driver override for ControllerHandle was not
646 found.
647 @retval EFI_UNSUPPORTED The operation is not supported.
648 @retval EFI_SUCCESS The driver override for ControllerHandle was
649 returned in DriverImagePath.
650
651 **/
652 EFI_STATUS
653 EFIAPI
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
660 )
661 {
662 EFI_STATUS Status;
663 EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath;
664 BOOLEAN ControllerFound;
665 BOOLEAN ImageFound;
666 EFI_HANDLE *ImageHandleBuffer;
667 UINTN ImageHandleCount;
668 UINTN Index;
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;
679 EFI_HANDLE Handle;
680 EFI_DEVICE_PATH_PROTOCOL *LoadedImageHandleDevicePath;
681 EFI_DEVICE_PATH_PROTOCOL *TatalFilePath;
682 EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL *BusSpecificDriverOverride;
683 UINTN DevicePathSize;
684
685 //
686 // Check that ControllerHandle is a valid handle
687 //
688 if (ControllerHandle == NULL) {
689 return EFI_INVALID_PARAMETER;
690 }
691
692 Status = gBS->HandleProtocol (
693 ControllerHandle,
694 &gEfiDevicePathProtocolGuid,
695 (VOID **) &ControllerDevicePath
696 );
697 if (EFI_ERROR (Status) || ControllerDevicePath == NULL) {
698 return EFI_INVALID_PARAMETER;
699 }
700
701 //
702 // Search ControllerDevicePath in MappingDataBase
703 //
704 OverrideItem = NULL;
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)) {
711 if (CompareMem (
712 ControllerDevicePath,
713 OverrideItem->ControllerDevicePath,
714 GetDevicePathSize (OverrideItem->ControllerDevicePath)
715 ) == 0
716 ) {
717 ControllerFound = TRUE;
718 break;
719 }
720
721 }
722 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
723 }
724
725 if (!ControllerFound) {
726 return EFI_NOT_FOUND;
727 }
728 //
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().
731 //
732 if (*DriverImageHandle != NULL) {
733 if (*DriverImageHandle != OverrideItem->LastReturnedImageHandle) {
734 return EFI_INVALID_PARAMETER;
735 }
736 }
737 //
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.
742 //
743 if (CheckExistInStack(OverrideItem->ControllerDevicePath)) {
744 //
745 // There is a dependecy dead loop if the ControllerDevicePath appear in stack twice
746 //
747 return EFI_UNSUPPORTED;
748 }
749 PushDevPathStack (OverrideItem->ControllerDevicePath);
750
751 //
752 // Check every override driver, try to load and start them
753 //
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) {
758 //
759 // Skip if the image is unloadable or unstartable
760 //
761 if ((!DriverImageInfo->UnLoadable) && ((!DriverImageInfo->UnStartable))) {
762 TempDriverImagePath = DriverImageInfo->DriverImagePath;
763 //
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.
766 //
767 Status = UpdateFvFileDevicePath (&TempDriverImagePath, NULL, CallerImageHandle);
768 if (!EFI_ERROR (Status)) {
769 FreePool(DriverImageInfo->DriverImagePath);
770 DriverImageInfo->DriverImagePath = TempDriverImagePath;
771 }
772 //
773 // Get all Loaded Image protocol to check whether the driver image has been loaded and started
774 //
775 ImageFound = FALSE;
776 ImageHandleCount = 0;
777 Status = gBS->LocateHandleBuffer (
778 ByProtocol,
779 &gEfiLoadedImageProtocolGuid,
780 NULL,
781 &ImageHandleCount,
782 &ImageHandleBuffer
783 );
784 if (EFI_ERROR (Status) || (ImageHandleCount == 0)) {
785 return EFI_NOT_FOUND;
786 }
787
788 for(Index = 0; Index < ImageHandleCount; Index ++) {
789 Status = gBS->HandleProtocol (
790 ImageHandleBuffer[Index],
791 &gEfiLoadedImageProtocolGuid,
792 (VOID **) &LoadedImage
793 );
794 if (EFI_ERROR (Status)) {
795 continue;
796 }
797
798 //
799 // Get the driver image total file path
800 //
801 LoadedImageHandleDevicePath = NULL;
802 Status = gBS->HandleProtocol (
803 LoadedImage->DeviceHandle,
804 &gEfiDevicePathProtocolGuid,
805 (VOID **) &LoadedImageHandleDevicePath
806 );
807 if (EFI_ERROR (Status)) {
808 //
809 // Maybe Not all LoadedImage->DeviceHandle has valid value. Skip the invalid image.
810 //
811 continue;
812 }
813
814 TatalFilePath = AppendDevicePath (LoadedImageHandleDevicePath, LoadedImage->FilePath);
815
816 DevicePathSize = GetDevicePathSize (DriverImageInfo->DriverImagePath);
817 if (DevicePathSize == GetDevicePathSize (TatalFilePath)) {
818 if (CompareMem (
819 DriverImageInfo->DriverImagePath,
820 TatalFilePath,
821 GetDevicePathSize (TatalFilePath)
822 ) == 0
823 ) {
824 ImageFound = TRUE;
825 break;
826 }
827 }
828 }
829
830 if (ImageFound) {
831 //
832 // Find its related driver binding protocol
833 // Driver binding handle may be different with its driver's Image handle,
834 //
835 DriverBindingHandle = NULL;
836 DriverBinding = GetBindingProtocolFromImageHandle (
837 ImageHandleBuffer[Index],
838 &DriverBindingHandle
839 );
840 ASSERT (DriverBinding != NULL);
841 DriverImageInfo->ImageHandle = ImageHandleBuffer[Index];
842 } else if (GetCurrentTpl() <= TPL_CALLBACK){
843 //
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
846 //
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.
851 //
852 Status = ConnectDevicePath (DriverImageInfo->DriverImagePath);
853 //
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
855 //
856 TempDriverImagePath = DriverImageInfo->DriverImagePath;
857 gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &TempDriverImagePath, &Handle);
858 //
859 // Get the Bus Specific Driver Override Protocol instance on the Controller Handle
860 //
861 Status = gBS->HandleProtocol(
862 Handle,
863 &gEfiBusSpecificDriverOverrideProtocolGuid,
864 (VOID **) &BusSpecificDriverOverride
865 );
866 if (!EFI_ERROR (Status) && (BusSpecificDriverOverride != NULL)) {
867 ImageHandle = NULL;
868 Status = BusSpecificDriverOverride->GetDriver (
869 BusSpecificDriverOverride,
870 &ImageHandle
871 );
872 if (!EFI_ERROR (Status)) {
873 //
874 // Find its related driver binding protocol
875 // Driver binding handle may be different with its driver's Image handle
876 //
877 DriverBindingHandle = NULL;
878 DriverBinding = GetBindingProtocolFromImageHandle (
879 ImageHandle,
880 &DriverBindingHandle
881 );
882 ASSERT (DriverBinding != NULL);
883 DriverImageInfo->ImageHandle = ImageHandle;
884 }
885 }
886 //
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
889 //
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)
895 ) {
896 //
897 // Try to load the driver
898 //
899 TempDriverImagePath = DriverImageInfo->DriverImagePath;
900 Status = gBS->LoadImage (
901 FALSE,
902 CallerImageHandle,
903 TempDriverImagePath,
904 NULL,
905 0,
906 &ImageHandle
907 );
908 if (!EFI_ERROR (Status)) {
909 //
910 // Try to start the driver
911 //
912 Status = gBS->StartImage (ImageHandle, NULL, NULL);
913 if (EFI_ERROR (Status)){
914 DriverImageInfo->UnStartable = TRUE;
915 DriverImageInfo->ImageHandle = NULL;
916 } else {
917 //
918 // Find its related driver binding protocol
919 // Driver binding handle may be different with its driver's Image handle
920 //
921 DriverBindingHandle = NULL;
922 DriverBinding = GetBindingProtocolFromImageHandle (
923 ImageHandle,
924 &DriverBindingHandle
925 );
926 ASSERT (DriverBinding != NULL);
927 DriverImageInfo->ImageHandle = ImageHandle;
928 }
929 } else {
930 DriverImageInfo->UnLoadable = TRUE;
931 DriverImageInfo->ImageHandle = NULL;
932 }
933 }
934 }
935 FreePool (ImageHandleBuffer);
936 }
937 }
938 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
939 }
940 //
941 // Finish try to load and start the override driver of a controller, popup the controller's device path
942 //
943 PopDevPathStack (NULL);
944
945 //
946 // return the DriverImageHandle for ControllerHandle
947 //
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;
956 return EFI_SUCCESS;
957 } else if (*DriverImageHandle == DriverImageInfo->ImageHandle){
958 FoundLastReturned = TRUE;
959 }
960 }
961 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
962 }
963
964 return EFI_NOT_FOUND;
965 }
966
967
968 /**
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.
972
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
979
980 @return EFI_INVALID_PARAMETER
981 @return EFI_NOT_FOUND
982 @return EFI_SUCCESS
983
984 **/
985 EFI_STATUS
986 EFIAPI
987 CheckMapping (
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
993 )
994 {
995 LIST_ENTRY *OverrideItemListIndex;
996 PLATFORM_OVERRIDE_ITEM *OverrideItem;
997 LIST_ENTRY *ImageInfoListIndex;
998 DRIVER_IMAGE_INFO *DriverImageInfo;
999 BOOLEAN Found;
1000 UINT32 ImageNO;
1001 UINTN DevicePathSize;
1002
1003 //
1004 // Check that ControllerHandle is a valid handle
1005 //
1006 if (ControllerDevicePath == NULL) {
1007 return EFI_INVALID_PARAMETER;
1008 }
1009 if (MappingDataBase == NULL) {
1010 return EFI_INVALID_PARAMETER;
1011 }
1012 //
1013 // Search ControllerDevicePath in MappingDataBase
1014 //
1015 Found = FALSE;
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)) {
1022 if (CompareMem (
1023 ControllerDevicePath,
1024 OverrideItem->ControllerDevicePath,
1025 GetDevicePathSize (OverrideItem->ControllerDevicePath)
1026 ) == 0
1027 ) {
1028 Found = TRUE;
1029 break;
1030 }
1031 }
1032 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
1033 }
1034
1035 if (!Found) {
1036 return EFI_NOT_FOUND;
1037 }
1038
1039 ASSERT (OverrideItem->DriverInfoNum != 0);
1040 if (DriverInfoNum != NULL) {
1041 *DriverInfoNum = OverrideItem->DriverInfoNum;
1042 }
1043
1044
1045 if (DriverImageDevicePath == NULL) {
1046 return EFI_SUCCESS;
1047 }
1048 //
1049 // return the DriverImageHandle for ControllerHandle
1050 //
1051 ImageNO = 0;
1052 Found = FALSE;
1053 ImageInfoListIndex = OverrideItem->DriverInfoList.ForwardLink;
1054 while (ImageInfoListIndex != &OverrideItem->DriverInfoList){
1055 DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
1056 ImageNO++;
1057 DevicePathSize = GetDevicePathSize (DriverImageDevicePath);
1058 if (DevicePathSize == GetDevicePathSize (DriverImageInfo->DriverImagePath)) {
1059 if (CompareMem (
1060 DriverImageDevicePath,
1061 DriverImageInfo->DriverImagePath,
1062 GetDevicePathSize (DriverImageInfo->DriverImagePath)
1063 ) == 0
1064 ) {
1065 Found = TRUE;
1066 break;
1067 }
1068 }
1069 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
1070 }
1071
1072 if (!Found) {
1073 return EFI_NOT_FOUND;
1074 } else {
1075 if (DriverImageNO != NULL) {
1076 *DriverImageNO = ImageNO;
1077 }
1078 return EFI_SUCCESS;
1079 }
1080
1081 }
1082
1083
1084 /**
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.
1087
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
1093
1094 @return EFI_INVALID_PARAMETER
1095 @return EFI_ALREADY_STARTED
1096 @return EFI_SUCCESS
1097
1098 **/
1099 EFI_STATUS
1100 EFIAPI
1101 InsertDriverImage (
1102 IN EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath,
1103 IN EFI_DEVICE_PATH_PROTOCOL *DriverImageDevicePath,
1104 IN LIST_ENTRY *MappingDataBase,
1105 IN UINT32 DriverImageNO
1106 )
1107 {
1108 EFI_STATUS Status;
1109 LIST_ENTRY *OverrideItemListIndex;
1110 PLATFORM_OVERRIDE_ITEM *OverrideItem;
1111 LIST_ENTRY *ImageInfoListIndex;
1112 DRIVER_IMAGE_INFO *DriverImageInfo;
1113 BOOLEAN Found;
1114 UINT32 ImageNO;
1115 UINTN DevicePathSize;
1116
1117 //
1118 // Check that ControllerHandle is a valid handle
1119 //
1120 if (ControllerDevicePath == NULL) {
1121 return EFI_INVALID_PARAMETER;
1122 }
1123 if (DriverImageDevicePath == NULL) {
1124 return EFI_INVALID_PARAMETER;
1125 }
1126 if (MappingDataBase == NULL) {
1127 return EFI_INVALID_PARAMETER;
1128 }
1129
1130 Status = CheckMapping (
1131 ControllerDevicePath,
1132 DriverImageDevicePath,
1133 MappingDataBase,
1134 NULL,
1135 NULL
1136 );
1137 if (Status == EFI_SUCCESS) {
1138 return EFI_ALREADY_STARTED;
1139 }
1140
1141 //
1142 // Search the input ControllerDevicePath in MappingDataBase
1143 //
1144 Found = FALSE;
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)) {
1151 if (CompareMem (
1152 ControllerDevicePath,
1153 OverrideItem->ControllerDevicePath,
1154 GetDevicePathSize (OverrideItem->ControllerDevicePath)
1155 ) == 0
1156 ) {
1157 Found = TRUE;
1158 break;
1159 }
1160 }
1161 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
1162 }
1163 //
1164 // If cannot find, this is a new controller item
1165 // Add the Controller related PLATFORM_OVERRIDE_ITEM structrue in mapping data base
1166 //
1167 if (!Found) {
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);
1174 }
1175
1176 //
1177 // Prepare the driver image related DRIVER_IMAGE_INFO structure.
1178 //
1179 DriverImageInfo = AllocateZeroPool (sizeof (DRIVER_IMAGE_INFO));
1180 ASSERT (DriverImageInfo != NULL);
1181 DriverImageInfo->Signature = DRIVER_IMAGE_INFO_SIGNATURE;
1182 DriverImageInfo->DriverImagePath = DuplicateDevicePath (DriverImageDevicePath);
1183 //
1184 // Find the driver image wantted order location
1185 //
1186 ImageNO = 0;
1187 Found = FALSE;
1188 ImageInfoListIndex = OverrideItem->DriverInfoList.ForwardLink;
1189 while (ImageInfoListIndex != &OverrideItem->DriverInfoList){
1190 if (ImageNO == (DriverImageNO - 1)) {
1191 //
1192 // find the wantted order location, insert it
1193 //
1194 InsertTailList (ImageInfoListIndex, &DriverImageInfo->Link);
1195 OverrideItem->DriverInfoNum ++;
1196 Found = TRUE;
1197 break;
1198 }
1199 //DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
1200 ImageNO++;
1201 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
1202 }
1203
1204 if (!Found) {
1205 //
1206 // if not find the wantted order location, add it as last item of the controller mapping item
1207 //
1208 InsertTailList (&OverrideItem->DriverInfoList, &DriverImageInfo->Link);
1209 OverrideItem->DriverInfoNum ++;
1210 }
1211
1212 return EFI_SUCCESS;
1213 }
1214
1215
1216 /**
1217 Delete a controller's override driver from the mapping database.
1218
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
1224
1225 @return EFI_INVALID_PARAMETER
1226 @return EFI_NOT_FOUND
1227 @return EFI_SUCCESS
1228
1229 **/
1230 EFI_STATUS
1231 EFIAPI
1232 DeleteDriverImage (
1233 IN EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath,
1234 IN EFI_DEVICE_PATH_PROTOCOL *DriverImageDevicePath,
1235 IN LIST_ENTRY *MappingDataBase
1236 )
1237 {
1238 EFI_STATUS Status;
1239 LIST_ENTRY *OverrideItemListIndex;
1240 PLATFORM_OVERRIDE_ITEM *OverrideItem;
1241 LIST_ENTRY *ImageInfoListIndex;
1242 DRIVER_IMAGE_INFO *DriverImageInfo;
1243 BOOLEAN Found;
1244 UINTN DevicePathSize;
1245
1246 //
1247 // Check that ControllerHandle is a valid handle
1248 //
1249 if (ControllerDevicePath == NULL) {
1250 return EFI_INVALID_PARAMETER;
1251 }
1252
1253 if (MappingDataBase == NULL) {
1254 return EFI_INVALID_PARAMETER;
1255 }
1256
1257 Status = CheckMapping (
1258 ControllerDevicePath,
1259 DriverImageDevicePath,
1260 MappingDataBase,
1261 NULL,
1262 NULL
1263 );
1264 if (Status == EFI_NOT_FOUND) {
1265 return EFI_NOT_FOUND;
1266 }
1267
1268 //
1269 // Search ControllerDevicePath in MappingDataBase
1270 //
1271 Found = FALSE;
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)) {
1278 if (CompareMem (
1279 ControllerDevicePath,
1280 OverrideItem->ControllerDevicePath,
1281 GetDevicePathSize (OverrideItem->ControllerDevicePath)
1282 ) == 0
1283 ) {
1284 Found = TRUE;
1285 break;
1286 }
1287 }
1288 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
1289 }
1290
1291 ASSERT (Found);
1292 ASSERT (OverrideItem->DriverInfoNum != 0);
1293 //
1294 //
1295 //
1296 Found = FALSE;
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)) {
1304 if (CompareMem (
1305 DriverImageDevicePath,
1306 DriverImageInfo->DriverImagePath,
1307 GetDevicePathSize (DriverImageInfo->DriverImagePath)
1308 ) == 0
1309 ) {
1310 Found = TRUE;
1311 FreePool(DriverImageInfo->DriverImagePath);
1312 RemoveEntryList (&DriverImageInfo->Link);
1313 OverrideItem->DriverInfoNum --;
1314 break;
1315 }
1316 }
1317 } else {
1318 Found = TRUE;
1319 FreePool(DriverImageInfo->DriverImagePath);
1320 RemoveEntryList (&DriverImageInfo->Link);
1321 OverrideItem->DriverInfoNum --;
1322 }
1323 }
1324
1325 if (DriverImageDevicePath == NULL) {
1326 ASSERT (OverrideItem->DriverInfoNum == 0);
1327 }
1328
1329 if (OverrideItem->DriverInfoNum == 0) {
1330 FreePool(OverrideItem->ControllerDevicePath);
1331 RemoveEntryList (&OverrideItem->Link);
1332 FreePool (OverrideItem);
1333 }
1334
1335 if (!Found) {
1336 return EFI_NOT_FOUND;
1337 }
1338
1339 return EFI_SUCCESS;
1340 }
1341
1342
1343 /**
1344 Deletes all environment variable(s) that contain the override mappings from Controller Device Path to
1345 a set of Driver Device Paths.
1346
1347 None
1348
1349 @return EFI_SUCCESS
1350
1351 **/
1352 EFI_STATUS
1353 EFIAPI
1354 DeleteOverridesVariables (
1355 VOID
1356 )
1357 {
1358 EFI_STATUS Status;
1359 VOID *VariableBuffer;
1360 UINTN VariableNum;
1361 UINTN BufferSize;
1362 UINTN Index;
1363 CHAR16 OverrideVariableName[40];
1364
1365 //
1366 // Get environment variable(s) number
1367 //
1368 VariableNum =0;
1369 VariableBuffer = GetVariableAndSize (L"PlatDriOver", &gEfiOverrideVariableGuid, &BufferSize);
1370 VariableNum ++;
1371 if (VariableBuffer == NULL) {
1372 return EFI_NOT_FOUND;
1373 }
1374 //
1375 // Check NotEnd to get all PlatDriOverX variable(s)
1376 //
1377 while (*(UINT32*)VariableBuffer) {
1378 UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver%d", VariableNum);
1379 VariableBuffer = GetVariableAndSize (OverrideVariableName, &gEfiOverrideVariableGuid, &BufferSize);
1380 VariableNum ++;
1381 ASSERT (VariableBuffer != NULL);
1382 }
1383
1384 Status = gRT->SetVariable (
1385 L"PlatDriOver",
1386 &gEfiOverrideVariableGuid,
1387 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1388 0,
1389 NULL
1390 );
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,
1398 0,
1399 NULL
1400 );
1401 ASSERT (!EFI_ERROR (Status));
1402 }
1403 return EFI_SUCCESS;
1404 }
1405
1406
1407 /**
1408 Push a controller device path into a globle device path list
1409
1410 @param ControllerDevicePath The controller device path need to push into
1411 stack
1412
1413 @return EFI_SUCCESS
1414
1415 **/
1416 EFI_STATUS
1417 EFIAPI
1418 PushDevPathStack (
1419 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1420 )
1421 {
1422 DEVICE_PATH_STACK_ITEM *DevicePathStackItem;
1423
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);
1429 return EFI_SUCCESS;
1430 }
1431
1432
1433 /**
1434 Pop a controller device path from a globle device path list
1435
1436 @param ControllerDevicePath The controller device path retrieved from stack
1437
1438 @return EFI_SUCCESS
1439 @return EFI_NOT_FOUND
1440
1441 **/
1442 EFI_STATUS
1443 EFIAPI
1444 PopDevPathStack (
1445 OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
1446 )
1447 {
1448 DEVICE_PATH_STACK_ITEM *DevicePathStackItem;
1449 LIST_ENTRY *ItemListIndex;
1450
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);
1456 }
1457 FreePool (DevicePathStackItem->DevicePath);
1458 RemoveEntryList (&DevicePathStackItem->Link);
1459 FreePool (DevicePathStackItem);
1460 return EFI_SUCCESS;
1461 }
1462 return EFI_NOT_FOUND;
1463 }
1464
1465
1466 /**
1467 Check whether a controller device path is in a globle device path list
1468
1469 @param ControllerDevicePath The controller device path need to check
1470
1471 @return True
1472 @return False
1473
1474 **/
1475 BOOLEAN
1476 EFIAPI
1477 CheckExistInStack (
1478 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1479 )
1480 {
1481 DEVICE_PATH_STACK_ITEM *DevicePathStackItem;
1482 LIST_ENTRY *ItemListIndex;
1483 BOOLEAN Found;
1484 UINTN DevicePathSize;
1485
1486 Found = FALSE;
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)) {
1492 if (CompareMem (
1493 DevicePath,
1494 DevicePathStackItem->DevicePath,
1495 GetDevicePathSize (DevicePathStackItem->DevicePath)
1496 ) == 0
1497 ) {
1498 Found = TRUE;
1499 break;
1500 }
1501 }
1502 ItemListIndex = ItemListIndex->BackLink;
1503 }
1504
1505 return Found;
1506 }
1507
1508
1509 /**
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.
1514
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
1519
1520 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1521 parameter
1522 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1523 guild at all
1524 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it
1525 is valid
1526 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1527 and return the updated device path in DevicePath
1528
1529 **/
1530 EFI_STATUS
1531 EFIAPI
1532 UpdateFvFileDevicePath (
1533 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,
1534 IN EFI_GUID *FileGuid,
1535 IN EFI_HANDLE CallerImageHandle
1536 )
1537 {
1538 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1539 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
1540 EFI_STATUS Status;
1541 EFI_GUID *GuidPoint;
1542 UINTN Index;
1543 UINTN FvHandleCount;
1544 EFI_HANDLE *FvHandleBuffer;
1545 EFI_FV_FILETYPE Type;
1546 UINTN Size;
1547 EFI_FV_FILE_ATTRIBUTES Attributes;
1548 UINT32 AuthenticationStatus;
1549 BOOLEAN FindFvFile;
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;
1555 BOOLEAN HasFVNode;
1556
1557 if ((DevicePath == NULL) || (*DevicePath == NULL)) {
1558 return EFI_INVALID_PARAMETER;
1559 }
1560
1561 //
1562 // Check whether the device path point to the default the input Fv file
1563 //
1564 TempDevicePath = *DevicePath;
1565 LastDeviceNode = TempDevicePath;
1566 while (!EfiIsDevicePathEnd (TempDevicePath)) {
1567 LastDeviceNode = TempDevicePath;
1568 TempDevicePath = EfiNextDevicePathNode (TempDevicePath);
1569 }
1570 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (
1571 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode
1572 );
1573 if (GuidPoint == NULL) {
1574 //
1575 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1576 //
1577 return EFI_UNSUPPORTED;
1578 }
1579
1580 if (FileGuid != NULL) {
1581 if (!CompareGuid (GuidPoint, FileGuid)) {
1582 //
1583 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1584 //
1585 return EFI_UNSUPPORTED;
1586 }
1587 } else {
1588 FileGuid = GuidPoint;
1589 }
1590
1591 //
1592 // Check to see if the device path contain memory map node
1593 //
1594 TempDevicePath = *DevicePath;
1595 HasFVNode = FALSE;
1596 while (!EfiIsDevicePathEnd (TempDevicePath)) {
1597 //
1598 // Use old Device Path
1599 //
1600 if (DevicePathType (TempDevicePath) == HARDWARE_DEVICE_PATH &&
1601 DevicePathSubType (TempDevicePath) == HW_MEMMAP_DP) {
1602 HasFVNode = TRUE;
1603 break;
1604 }
1605 TempDevicePath = EfiNextDevicePathNode (TempDevicePath);
1606 }
1607
1608 if (!HasFVNode) {
1609 return EFI_UNSUPPORTED;
1610 }
1611
1612 //
1613 // Check whether the input Fv file device path is valid
1614 //
1615 TempDevicePath = *DevicePath;
1616 FoundFvHandle = NULL;
1617 Status = gBS->LocateDevicePath (
1618 &gEfiFirmwareVolume2ProtocolGuid,
1619 &TempDevicePath,
1620 &FoundFvHandle
1621 );
1622 if (!EFI_ERROR (Status)) {
1623 Status = gBS->HandleProtocol (
1624 FoundFvHandle,
1625 &gEfiFirmwareVolume2ProtocolGuid,
1626 (VOID **) &Fv
1627 );
1628 if (!EFI_ERROR (Status)) {
1629 //
1630 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1631 //
1632 Status = Fv->ReadFile (
1633 Fv,
1634 FileGuid,
1635 NULL,
1636 &Size,
1637 &Type,
1638 &Attributes,
1639 &AuthenticationStatus
1640 );
1641 if (!EFI_ERROR (Status)) {
1642 return EFI_ALREADY_STARTED;
1643 }
1644 }
1645 }
1646
1647 //
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
1650 //
1651 FindFvFile = FALSE;
1652 FoundFvHandle = NULL;
1653 Status = gBS->HandleProtocol (
1654 CallerImageHandle,
1655 &gEfiLoadedImageProtocolGuid,
1656 (VOID **) &LoadedImage
1657 );
1658 if (!EFI_ERROR (Status)) {
1659 Status = gBS->HandleProtocol (
1660 LoadedImage->DeviceHandle,
1661 &gEfiFirmwareVolume2ProtocolGuid,
1662 (VOID **) &Fv
1663 );
1664 if (!EFI_ERROR (Status)) {
1665 Status = Fv->ReadFile (
1666 Fv,
1667 FileGuid,
1668 NULL,
1669 &Size,
1670 &Type,
1671 &Attributes,
1672 &AuthenticationStatus
1673 );
1674 if (!EFI_ERROR (Status)) {
1675 FindFvFile = TRUE;
1676 FoundFvHandle = LoadedImage->DeviceHandle;
1677 }
1678 }
1679 }
1680 //
1681 // Second, if fail to find, try to enumerate all FV
1682 //
1683 if (!FindFvFile) {
1684 gBS->LocateHandleBuffer (
1685 ByProtocol,
1686 &gEfiFirmwareVolume2ProtocolGuid,
1687 NULL,
1688 &FvHandleCount,
1689 &FvHandleBuffer
1690 );
1691 for (Index = 0; Index < FvHandleCount; Index++) {
1692 gBS->HandleProtocol (
1693 FvHandleBuffer[Index],
1694 &gEfiFirmwareVolume2ProtocolGuid,
1695 (VOID **) &Fv
1696 );
1697
1698 Status = Fv->ReadFile (
1699 Fv,
1700 FileGuid,
1701 NULL,
1702 &Size,
1703 &Type,
1704 &Attributes,
1705 &AuthenticationStatus
1706 );
1707 if (EFI_ERROR (Status)) {
1708 //
1709 // Skip if input Fv file not in the FV
1710 //
1711 continue;
1712 }
1713 FindFvFile = TRUE;
1714 FoundFvHandle = FvHandleBuffer[Index];
1715 break;
1716 }
1717 }
1718
1719 if (FindFvFile) {
1720 //
1721 // Build the shell device path
1722 //
1723 NewDevicePath = DevicePathFromHandle (FoundFvHandle);
1724 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);
1725 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);
1726 *DevicePath = NewDevicePath;
1727 return EFI_SUCCESS;
1728 }
1729 return EFI_NOT_FOUND;
1730 }
1731
1732
1733 /**
1734 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
1735 buffer, and the size of the buffer. If failure return NULL.
1736
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
1740 read
1741
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
1745
1746 **/
1747 VOID *
1748 EFIAPI
1749 GetVariableAndSize (
1750 IN CHAR16 *Name,
1751 IN EFI_GUID *VendorGuid,
1752 OUT UINTN *VariableSize
1753 )
1754 {
1755 EFI_STATUS Status;
1756 UINTN BufferSize;
1757 VOID *Buffer;
1758
1759 Buffer = NULL;
1760
1761 //
1762 // Pass in a zero size buffer to find the required buffer size.
1763 //
1764 BufferSize = 0;
1765 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
1766 if (Status == EFI_BUFFER_TOO_SMALL) {
1767 //
1768 // Allocate the buffer to return
1769 //
1770 Buffer = AllocateZeroPool (BufferSize);
1771 if (Buffer == NULL) {
1772 return NULL;
1773 }
1774 //
1775 // Read variable into the allocated buffer.
1776 //
1777 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
1778 if (EFI_ERROR (Status)) {
1779 BufferSize = 0;
1780 }
1781 }
1782
1783 *VariableSize = BufferSize;
1784 return Buffer;
1785 }
1786
1787
1788 /**
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.
1793
1794 @param DevicePathToConnect The device path which will be connected, it can
1795 be a multi-instance device path
1796
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
1801 path node failed
1802
1803 **/
1804 EFI_STATUS
1805 EFIAPI
1806 ConnectDevicePath (
1807 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect
1808 )
1809 {
1810 EFI_STATUS Status;
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;
1816 EFI_HANDLE Handle;
1817 EFI_HANDLE PreviousHandle;
1818 UINTN Size;
1819
1820 if (DevicePathToConnect == NULL) {
1821 return EFI_SUCCESS;
1822 }
1823
1824 DevicePath = DuplicateDevicePath (DevicePathToConnect);
1825 CopyOfDevicePath = DevicePath;
1826 if (DevicePath == NULL) {
1827 return EFI_OUT_OF_RESOURCES;
1828 }
1829
1830 do {
1831 //
1832 // The outer loop handles multi instance device paths.
1833 // Only console variables contain multiple instance device paths.
1834 //
1835 // After this call DevicePath points to the next Instance
1836 //
1837 Instance = GetNextDevicePathInstance (&DevicePath, &Size);
1838 Next = Instance;
1839 while (!IsDevicePathEndType (Next)) {
1840 Next = NextDevicePathNode (Next);
1841 }
1842
1843 SetDevicePathEndNode (Next);
1844
1845 //
1846 // Start the real work of connect with RemainingDevicePath
1847 //
1848 PreviousHandle = NULL;
1849 do {
1850 //
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.
1854 //
1855 RemainingDevicePath = Instance;
1856 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
1857
1858 if (!EFI_ERROR (Status)) {
1859 if (Handle == PreviousHandle) {
1860 //
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
1866 //
1867 Status = gDS->Dispatch ();
1868 }
1869
1870 if (!EFI_ERROR (Status)) {
1871 PreviousHandle = Handle;
1872 //
1873 // Connect all drivers that apply to Handle and RemainingDevicePath,
1874 // the Recursive flag is FALSE so only one level will be expanded.
1875 //
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
1879 //
1880 // 1. If the connect fail, RemainingDevicepath and handle will not
1881 // change, so next time will do the dispatch, then dispatch's status
1882 // will take effect
1883 // 2. If the connect success, the RemainingDevicepath and handle will
1884 // change, then avoid the dispatch, we have chance to continue the
1885 // next connection
1886 //
1887 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
1888 }
1889 }
1890 //
1891 // Loop until RemainingDevicePath is an empty device path
1892 //
1893 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
1894
1895 } while (DevicePath != NULL);
1896
1897 if (CopyOfDevicePath != NULL) {
1898 FreePool (CopyOfDevicePath);
1899 }
1900 //
1901 // All handle with DevicePath exists in the handle database
1902 //
1903 return Status;
1904 }