Port PlatformDriOverrideDxe into R9.
[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 gBS->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 /**
541 Retrieves the image handle of the platform override driver for a controller in the system from the memory mapping database.
542
543 @param This A pointer to the
544 EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL instance.
545 @param ControllerHandle The device handle of the controller to check if
546 a driver override exists.
547 @param DriverImageHandle On output, a pointer to the next driver handle.
548 Passing in a pointer to NULL, will return the
549 first driver handle for ControllerHandle.
550 @param MappingDataBase MappingDataBase - Mapping database list entry
551 pointer
552 @param CallerImageHandle The caller driver's image handle, for
553 UpdateFvFileDevicePath use.
554
555 @retval EFI_INVALID_PARAMETER The handle specified by ControllerHandle is not
556 a valid handle. Or DriverImagePath is not a
557 device path that was returned on a previous call
558 to GetDriverPath().
559 @retval EFI_NOT_FOUND A driver override for ControllerHandle was not
560 found.
561 @retval EFI_UNSUPPORTED The operation is not supported.
562 @retval EFI_SUCCESS The driver override for ControllerHandle was
563 returned in DriverImagePath.
564
565 **/
566 EFI_STATUS
567 EFIAPI
568 GetDriverFromMapping (
569 IN EFI_PLATFORM_DRIVER_OVERRIDE_PROTOCOL * This,
570 IN EFI_HANDLE ControllerHandle,
571 IN OUT EFI_HANDLE * DriverImageHandle,
572 IN LIST_ENTRY * MappingDataBase,
573 IN EFI_HANDLE CallerImageHandle
574 )
575 {
576 EFI_STATUS Status;
577 EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath;
578 BOOLEAN ControllerFound;
579 BOOLEAN ImageFound;
580 EFI_HANDLE *ImageHandleBuffer;
581 UINTN ImageHandleCount;
582 UINTN Index;
583 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
584 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
585 BOOLEAN FoundLastReturned;
586 PLATFORM_OVERRIDE_ITEM *OverrideItem;
587 DRIVER_IMAGE_INFO *DriverImageInfo;
588 LIST_ENTRY *OverrideItemListIndex;
589 LIST_ENTRY *ImageInfoListIndex;
590 EFI_DEVICE_PATH_PROTOCOL *TempDriverImagePath;
591 EFI_HANDLE ImageHandle;
592 EFI_HANDLE Handle;
593 EFI_DEVICE_PATH_PROTOCOL *LoadedImageHandleDevicePath;
594 EFI_DEVICE_PATH_PROTOCOL *TatalFilePath;
595 EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL *BusSpecificDriverOverride;
596 UINTN DevicePathSize;
597
598 //
599 // Check that ControllerHandle is a valid handle
600 //
601 if (ControllerHandle == NULL) {
602 return EFI_INVALID_PARAMETER;
603 }
604
605 Status = gBS->HandleProtocol (
606 ControllerHandle,
607 &gEfiDevicePathProtocolGuid,
608 (VOID **) &ControllerDevicePath
609 );
610 if (EFI_ERROR (Status) || ControllerDevicePath == NULL) {
611 return EFI_INVALID_PARAMETER;
612 }
613
614 //
615 // Search ControllerDevicePath in MappingDataBase
616 //
617 OverrideItem = NULL;
618 ControllerFound = FALSE;
619 OverrideItemListIndex = MappingDataBase->ForwardLink;
620 while (OverrideItemListIndex != MappingDataBase){
621 OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);
622 DevicePathSize = GetDevicePathSize (ControllerDevicePath);
623 if (DevicePathSize == GetDevicePathSize (OverrideItem->ControllerDevicePath)) {
624 if (CompareMem (
625 ControllerDevicePath,
626 OverrideItem->ControllerDevicePath,
627 GetDevicePathSize (OverrideItem->ControllerDevicePath)
628 ) == 0
629 ) {
630 ControllerFound = TRUE;
631 break;
632 }
633
634 }
635 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
636 }
637
638 if (!ControllerFound) {
639 return EFI_NOT_FOUND;
640 }
641 //
642 // Passing in a pointer to NULL, will return the first driver device path for ControllerHandle.
643 // Check whether the driverImagePath is not a device path that was returned on a previous call to GetDriverPath().
644 //
645 if (*DriverImageHandle != NULL) {
646 if (*DriverImageHandle != OverrideItem->LastReturnedImageHandle) {
647 return EFI_INVALID_PARAMETER;
648 }
649 }
650 //
651 // The GetDriverPath() maybe called recursively, because it use ConnectDevicePath() internally,
652 // so should check whether there is a dead loop.
653 // Here use a controller device path stack to record all processed controller device path during a GetDriverPath() call,
654 // and check the controller device path whether appear again during the GetDriverPath() call.
655 //
656 if (CheckExistInStack(OverrideItem->ControllerDevicePath)) {
657 //
658 // There is a dependecy dead loop if the ControllerDevicePath appear in stack twice
659 //
660 return EFI_UNSUPPORTED;
661 }
662 PushDevPathStack (OverrideItem->ControllerDevicePath);
663
664 //
665 // Check every override driver, try to load and start them
666 //
667 ImageInfoListIndex = OverrideItem->DriverInfoList.ForwardLink;
668 while (ImageInfoListIndex != &OverrideItem->DriverInfoList){
669 DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
670 if (DriverImageInfo->ImageHandle == NULL) {
671 //
672 // Skip if the image is unloadable or unstartable
673 //
674 if ((!DriverImageInfo->UnLoadable) && ((!DriverImageInfo->UnStartable))) {
675 TempDriverImagePath = DriverImageInfo->DriverImagePath;
676 //
677 // If the image device path contain a FV node, check the Fv file device path is valid. If it is invalid, try to return the valid device path.
678 // FV address maybe changes for memory layout adjust from time to time, use this funciton could promise the Fv file device path is right.
679 //
680 Status = UpdateFvFileDevicePath (&TempDriverImagePath, NULL, CallerImageHandle);
681 if (!EFI_ERROR (Status)) {
682 FreePool(DriverImageInfo->DriverImagePath);
683 DriverImageInfo->DriverImagePath = TempDriverImagePath;
684 }
685 //
686 // Get all Loaded Image protocol to check whether the driver image has been loaded and started
687 //
688 ImageFound = FALSE;
689 ImageHandleCount = 0;
690 Status = gBS->LocateHandleBuffer (
691 ByProtocol,
692 &gEfiLoadedImageProtocolGuid,
693 NULL,
694 &ImageHandleCount,
695 &ImageHandleBuffer
696 );
697 if (EFI_ERROR (Status) || (ImageHandleCount == 0)) {
698 return EFI_NOT_FOUND;
699 }
700
701 for(Index = 0; Index < ImageHandleCount; Index ++) {
702 Status = gBS->HandleProtocol (
703 ImageHandleBuffer[Index],
704 &gEfiLoadedImageProtocolGuid,
705 (VOID **) &LoadedImage
706 );
707 if (EFI_ERROR (Status)) {
708 continue;
709 }
710
711 //
712 // Get the driver image total file path
713 //
714 LoadedImageHandleDevicePath = NULL;
715 Status = gBS->HandleProtocol (
716 LoadedImage->DeviceHandle,
717 &gEfiDevicePathProtocolGuid,
718 (VOID **) &LoadedImageHandleDevicePath
719 );
720 if (EFI_ERROR (Status)) {
721 //
722 // Maybe Not all LoadedImage->DeviceHandle has valid value. Skip the invalid image.
723 //
724 continue;
725 }
726
727 TatalFilePath = AppendDevicePath (LoadedImageHandleDevicePath, LoadedImage->FilePath);
728
729 DevicePathSize = GetDevicePathSize (DriverImageInfo->DriverImagePath);
730 if (DevicePathSize == GetDevicePathSize (TatalFilePath)) {
731 if (CompareMem (
732 DriverImageInfo->DriverImagePath,
733 TatalFilePath,
734 GetDevicePathSize (TatalFilePath)
735 ) == 0
736 ) {
737 ImageFound = TRUE;
738 break;
739 }
740 }
741 }
742
743 if (ImageFound) {
744 Status = gBS->HandleProtocol (
745 ImageHandleBuffer[Index],
746 &gEfiDriverBindingProtocolGuid,
747 (VOID **) &DriverBinding
748 );
749 ASSERT (!EFI_ERROR (Status));
750 DriverImageInfo->ImageHandle = ImageHandleBuffer[Index];
751 } else {
752 //
753 // The driver image has not been loaded and started, need try to load and start it now
754 // Try to connect all device in the driver image path
755 //
756 Status = ConnectDevicePath (DriverImageInfo->DriverImagePath);
757 //
758 // check whether it points to a PCI Option Rom image, and try to use bus override protocol to get its first option rom image driver
759 //
760 TempDriverImagePath = DriverImageInfo->DriverImagePath;
761 gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &TempDriverImagePath, &Handle);
762 //
763 // Get the Bus Specific Driver Override Protocol instance on the Controller Handle
764 //
765 Status = gBS->HandleProtocol(
766 Handle,
767 &gEfiBusSpecificDriverOverrideProtocolGuid,
768 (VOID **) &BusSpecificDriverOverride
769 );
770 if (!EFI_ERROR (Status) && (BusSpecificDriverOverride != NULL)) {
771 ImageHandle = NULL;
772 Status = BusSpecificDriverOverride->GetDriver (
773 BusSpecificDriverOverride,
774 &ImageHandle
775 );
776 if (!EFI_ERROR (Status)) {
777 Status = gBS->HandleProtocol (
778 ImageHandle,
779 &gEfiDriverBindingProtocolGuid,
780 (VOID **) &DriverBinding
781 );
782 ASSERT (!EFI_ERROR (Status));
783 DriverImageInfo->ImageHandle = ImageHandle;
784 }
785 }
786 //
787 // Skip if any device cannot be connected now, future passes through GetDriver() may be able to load that driver.
788 // Only file path media or FwVol Device Path Node remain if all device is connected
789 //
790 TempDriverImagePath = DriverImageInfo->DriverImagePath;
791 gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &TempDriverImagePath, &Handle);
792 if (((DevicePathType (TempDriverImagePath) == MEDIA_DEVICE_PATH) &&
793 (DevicePathSubType (TempDriverImagePath) == MEDIA_FILEPATH_DP)) ||
794 (EfiGetNameGuidFromFwVolDevicePathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) TempDriverImagePath) != NULL)
795 ) {
796 //
797 // Try to load the driver
798 //
799 TempDriverImagePath = DriverImageInfo->DriverImagePath;
800 Status = gBS->LoadImage (
801 FALSE,
802 CallerImageHandle,
803 TempDriverImagePath,
804 NULL,
805 0,
806 &ImageHandle
807 );
808 if (!EFI_ERROR (Status)) {
809 //
810 // Try to start the driver
811 //
812 Status = gBS->StartImage (ImageHandle, NULL, NULL);
813 if (EFI_ERROR (Status)){
814 DriverImageInfo->UnStartable = TRUE;
815 DriverImageInfo->ImageHandle = NULL;
816 } else {
817 Status = gBS->HandleProtocol (
818 ImageHandle,
819 &gEfiDriverBindingProtocolGuid,
820 (VOID **) &DriverBinding
821 );
822 ASSERT (!EFI_ERROR (Status));
823 DriverImageInfo->ImageHandle = ImageHandle;
824 }
825 } else {
826 DriverImageInfo->UnLoadable = TRUE;
827 DriverImageInfo->ImageHandle = NULL;
828 }
829 }
830 }
831 FreePool (ImageHandleBuffer);
832 }
833 }
834 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
835 }
836 //
837 // Finish try to load and start the override driver of a controller, popup the controller's device path
838 //
839 PopDevPathStack (NULL);
840
841 //
842 // return the DriverImageHandle for ControllerHandle
843 //
844 FoundLastReturned = FALSE;
845 ImageInfoListIndex = OverrideItem->DriverInfoList.ForwardLink;
846 while (ImageInfoListIndex != &OverrideItem->DriverInfoList){
847 DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
848 if (DriverImageInfo->ImageHandle != NULL) {
849 if ((*DriverImageHandle == NULL) || FoundLastReturned) {
850 OverrideItem->LastReturnedImageHandle = DriverImageInfo->ImageHandle;
851 *DriverImageHandle = DriverImageInfo->ImageHandle;
852 return EFI_SUCCESS;
853 } else if (*DriverImageHandle == DriverImageInfo->ImageHandle){
854 FoundLastReturned = TRUE;
855 }
856 }
857 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
858 }
859
860 return EFI_NOT_FOUND;
861 }
862
863
864 /**
865 Check mapping database whether already has the mapping info which
866 records the input Controller to input DriverImage.
867 If has, the controller's total override driver number and input DriverImage's order number is return.
868
869 @param ControllerDevicePath The controller device path need to add a
870 override driver image item
871 @param DriverImageDevicePath The driver image device path need to be insert
872 @param MappingDataBase Mapping database list entry pointer
873 @param DriverInfoNum the controller's total override driver number
874 @param DriverImageNO The inserted order number
875
876 @return EFI_INVALID_PARAMETER
877 @return EFI_NOT_FOUND
878 @return EFI_SUCCESS
879
880 **/
881 EFI_STATUS
882 EFIAPI
883 CheckMapping (
884 IN EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath,
885 IN EFI_DEVICE_PATH_PROTOCOL *DriverImageDevicePath,
886 IN LIST_ENTRY * MappingDataBase,
887 OUT UINT32 *DriverInfoNum,
888 OUT UINT32 *DriverImageNO
889 )
890 {
891 LIST_ENTRY *OverrideItemListIndex;
892 PLATFORM_OVERRIDE_ITEM *OverrideItem;
893 LIST_ENTRY *ImageInfoListIndex;
894 DRIVER_IMAGE_INFO *DriverImageInfo;
895 BOOLEAN Found;
896 UINT32 ImageNO;
897 UINTN DevicePathSize;
898
899 //
900 // Check that ControllerHandle is a valid handle
901 //
902 if (ControllerDevicePath == NULL) {
903 return EFI_INVALID_PARAMETER;
904 }
905 if (MappingDataBase == NULL) {
906 return EFI_INVALID_PARAMETER;
907 }
908 //
909 // Search ControllerDevicePath in MappingDataBase
910 //
911 Found = FALSE;
912 OverrideItem = NULL;
913 OverrideItemListIndex = MappingDataBase->ForwardLink;
914 while (OverrideItemListIndex != MappingDataBase){
915 OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);
916 DevicePathSize = GetDevicePathSize (ControllerDevicePath);
917 if (DevicePathSize == GetDevicePathSize (OverrideItem->ControllerDevicePath)) {
918 if (CompareMem (
919 ControllerDevicePath,
920 OverrideItem->ControllerDevicePath,
921 GetDevicePathSize (OverrideItem->ControllerDevicePath)
922 ) == 0
923 ) {
924 Found = TRUE;
925 break;
926 }
927 }
928 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
929 }
930
931 if (!Found) {
932 return EFI_NOT_FOUND;
933 }
934
935 ASSERT (OverrideItem->DriverInfoNum != 0);
936 if (DriverInfoNum != NULL) {
937 *DriverInfoNum = OverrideItem->DriverInfoNum;
938 }
939
940
941 if (DriverImageDevicePath == NULL) {
942 return EFI_SUCCESS;
943 }
944 //
945 // return the DriverImageHandle for ControllerHandle
946 //
947 ImageNO = 0;
948 Found = FALSE;
949 ImageInfoListIndex = OverrideItem->DriverInfoList.ForwardLink;
950 while (ImageInfoListIndex != &OverrideItem->DriverInfoList){
951 DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
952 ImageNO++;
953 DevicePathSize = GetDevicePathSize (DriverImageDevicePath);
954 if (DevicePathSize == GetDevicePathSize (DriverImageInfo->DriverImagePath)) {
955 if (CompareMem (
956 DriverImageDevicePath,
957 DriverImageInfo->DriverImagePath,
958 GetDevicePathSize (DriverImageInfo->DriverImagePath)
959 ) == 0
960 ) {
961 Found = TRUE;
962 break;
963 }
964 }
965 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
966 }
967
968 if (!Found) {
969 return EFI_NOT_FOUND;
970 } else {
971 if (DriverImageNO != NULL) {
972 *DriverImageNO = ImageNO;
973 }
974 return EFI_SUCCESS;
975 }
976
977 }
978
979
980 /**
981 Insert a driver image as a controller's override driver into the mapping database.
982 The driver image's order number is indicated by DriverImageNO.
983
984 @param ControllerDevicePath The controller device path need to add a
985 override driver image item
986 @param DriverImageDevicePath The driver image device path need to be insert
987 @param MappingDataBase Mapping database list entry pointer
988 @param DriverImageNO The inserted order number
989
990 @return EFI_INVALID_PARAMETER
991 @return EFI_ALREADY_STARTED
992 @return EFI_SUCCESS
993
994 **/
995 EFI_STATUS
996 EFIAPI
997 InsertDriverImage (
998 IN EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath,
999 IN EFI_DEVICE_PATH_PROTOCOL *DriverImageDevicePath,
1000 IN LIST_ENTRY *MappingDataBase,
1001 IN UINT32 DriverImageNO
1002 )
1003 {
1004 EFI_STATUS Status;
1005 LIST_ENTRY *OverrideItemListIndex;
1006 PLATFORM_OVERRIDE_ITEM *OverrideItem;
1007 LIST_ENTRY *ImageInfoListIndex;
1008 DRIVER_IMAGE_INFO *DriverImageInfo;
1009 BOOLEAN Found;
1010 UINT32 ImageNO;
1011 UINTN DevicePathSize;
1012
1013 //
1014 // Check that ControllerHandle is a valid handle
1015 //
1016 if (ControllerDevicePath == NULL) {
1017 return EFI_INVALID_PARAMETER;
1018 }
1019 if (DriverImageDevicePath == NULL) {
1020 return EFI_INVALID_PARAMETER;
1021 }
1022 if (MappingDataBase == NULL) {
1023 return EFI_INVALID_PARAMETER;
1024 }
1025
1026 Status = CheckMapping (
1027 ControllerDevicePath,
1028 DriverImageDevicePath,
1029 MappingDataBase,
1030 NULL,
1031 NULL
1032 );
1033 if (Status == EFI_SUCCESS) {
1034 return EFI_ALREADY_STARTED;
1035 }
1036
1037 //
1038 // Search the input ControllerDevicePath in MappingDataBase
1039 //
1040 Found = FALSE;
1041 OverrideItem = NULL;
1042 OverrideItemListIndex = MappingDataBase->ForwardLink;
1043 while (OverrideItemListIndex != MappingDataBase){
1044 OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);
1045 DevicePathSize = GetDevicePathSize (ControllerDevicePath);
1046 if (DevicePathSize == GetDevicePathSize (OverrideItem->ControllerDevicePath)) {
1047 if (CompareMem (
1048 ControllerDevicePath,
1049 OverrideItem->ControllerDevicePath,
1050 GetDevicePathSize (OverrideItem->ControllerDevicePath)
1051 ) == 0
1052 ) {
1053 Found = TRUE;
1054 break;
1055 }
1056 }
1057 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
1058 }
1059 //
1060 // If cannot find, this is a new controller item
1061 // Add the Controller related PLATFORM_OVERRIDE_ITEM structrue in mapping data base
1062 //
1063 if (!Found) {
1064 OverrideItem = AllocateZeroPool (sizeof (PLATFORM_OVERRIDE_ITEM));
1065 ASSERT (OverrideItem != NULL);
1066 OverrideItem->Signature = PLATFORM_OVERRIDE_ITEM_SIGNATURE;
1067 OverrideItem->ControllerDevicePath = DuplicateDevicePath (ControllerDevicePath);
1068 InitializeListHead (&OverrideItem->DriverInfoList);
1069 InsertTailList (MappingDataBase, &OverrideItem->Link);
1070 }
1071
1072 //
1073 // Prepare the driver image related DRIVER_IMAGE_INFO structure.
1074 //
1075 DriverImageInfo = AllocateZeroPool (sizeof (DRIVER_IMAGE_INFO));
1076 ASSERT (DriverImageInfo != NULL);
1077 DriverImageInfo->Signature = DRIVER_IMAGE_INFO_SIGNATURE;
1078 DriverImageInfo->DriverImagePath = DuplicateDevicePath (DriverImageDevicePath);
1079 //
1080 // Find the driver image wantted order location
1081 //
1082 ImageNO = 0;
1083 Found = FALSE;
1084 ImageInfoListIndex = OverrideItem->DriverInfoList.ForwardLink;
1085 while (ImageInfoListIndex != &OverrideItem->DriverInfoList){
1086 if (ImageNO == (DriverImageNO - 1)) {
1087 //
1088 // find the wantted order location, insert it
1089 //
1090 InsertTailList (ImageInfoListIndex, &DriverImageInfo->Link);
1091 OverrideItem->DriverInfoNum ++;
1092 Found = TRUE;
1093 break;
1094 }
1095 //DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
1096 ImageNO++;
1097 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
1098 }
1099
1100 if (!Found) {
1101 //
1102 // if not find the wantted order location, add it as last item of the controller mapping item
1103 //
1104 InsertTailList (&OverrideItem->DriverInfoList, &DriverImageInfo->Link);
1105 OverrideItem->DriverInfoNum ++;
1106 }
1107
1108 return EFI_SUCCESS;
1109 }
1110
1111
1112 /**
1113 Delete a controller's override driver from the mapping database.
1114
1115 @param ControllerDevicePath The controller device path need to add a
1116 override driver image item
1117 @param DriverImageDevicePath The driver image device path need to be insert
1118 @param MappingDataBase Mapping database list entry pointer
1119 @param DriverImageNO The inserted order number
1120
1121 @return EFI_INVALID_PARAMETER
1122 @return EFI_NOT_FOUND
1123 @return EFI_SUCCESS
1124
1125 **/
1126 EFI_STATUS
1127 EFIAPI
1128 DeleteDriverImage (
1129 IN EFI_DEVICE_PATH_PROTOCOL *ControllerDevicePath,
1130 IN EFI_DEVICE_PATH_PROTOCOL *DriverImageDevicePath,
1131 IN LIST_ENTRY *MappingDataBase
1132 )
1133 {
1134 EFI_STATUS Status;
1135 LIST_ENTRY *OverrideItemListIndex;
1136 PLATFORM_OVERRIDE_ITEM *OverrideItem;
1137 LIST_ENTRY *ImageInfoListIndex;
1138 DRIVER_IMAGE_INFO *DriverImageInfo;
1139 BOOLEAN Found;
1140 UINTN DevicePathSize;
1141
1142 //
1143 // Check that ControllerHandle is a valid handle
1144 //
1145 if (ControllerDevicePath == NULL) {
1146 return EFI_INVALID_PARAMETER;
1147 }
1148
1149 if (MappingDataBase == NULL) {
1150 return EFI_INVALID_PARAMETER;
1151 }
1152
1153 Status = CheckMapping (
1154 ControllerDevicePath,
1155 DriverImageDevicePath,
1156 MappingDataBase,
1157 NULL,
1158 NULL
1159 );
1160 if (Status == EFI_NOT_FOUND) {
1161 return EFI_NOT_FOUND;
1162 }
1163
1164 //
1165 // Search ControllerDevicePath in MappingDataBase
1166 //
1167 Found = FALSE;
1168 OverrideItem = NULL;
1169 OverrideItemListIndex = MappingDataBase->ForwardLink;
1170 while (OverrideItemListIndex != MappingDataBase){
1171 OverrideItem = CR(OverrideItemListIndex, PLATFORM_OVERRIDE_ITEM, Link, PLATFORM_OVERRIDE_ITEM_SIGNATURE);
1172 DevicePathSize = GetDevicePathSize (ControllerDevicePath);
1173 if (DevicePathSize == GetDevicePathSize (OverrideItem->ControllerDevicePath)) {
1174 if (CompareMem (
1175 ControllerDevicePath,
1176 OverrideItem->ControllerDevicePath,
1177 GetDevicePathSize (OverrideItem->ControllerDevicePath)
1178 ) == 0
1179 ) {
1180 Found = TRUE;
1181 break;
1182 }
1183 }
1184 OverrideItemListIndex = OverrideItemListIndex->ForwardLink;
1185 }
1186
1187 ASSERT (Found);
1188 ASSERT (OverrideItem->DriverInfoNum != 0);
1189 //
1190 //
1191 //
1192 Found = FALSE;
1193 ImageInfoListIndex = OverrideItem->DriverInfoList.ForwardLink;
1194 while (ImageInfoListIndex != &OverrideItem->DriverInfoList){
1195 DriverImageInfo = CR(ImageInfoListIndex, DRIVER_IMAGE_INFO, Link, DRIVER_IMAGE_INFO_SIGNATURE);
1196 ImageInfoListIndex = ImageInfoListIndex->ForwardLink;
1197 if (DriverImageDevicePath != NULL) {
1198 DevicePathSize = GetDevicePathSize (DriverImageDevicePath);
1199 if (DevicePathSize == GetDevicePathSize (DriverImageInfo->DriverImagePath)) {
1200 if (CompareMem (
1201 DriverImageDevicePath,
1202 DriverImageInfo->DriverImagePath,
1203 GetDevicePathSize (DriverImageInfo->DriverImagePath)
1204 ) == 0
1205 ) {
1206 Found = TRUE;
1207 FreePool(DriverImageInfo->DriverImagePath);
1208 RemoveEntryList (&DriverImageInfo->Link);
1209 OverrideItem->DriverInfoNum --;
1210 break;
1211 }
1212 }
1213 } else {
1214 Found = TRUE;
1215 FreePool(DriverImageInfo->DriverImagePath);
1216 RemoveEntryList (&DriverImageInfo->Link);
1217 OverrideItem->DriverInfoNum --;
1218 }
1219 }
1220
1221 if (DriverImageDevicePath == NULL) {
1222 ASSERT (OverrideItem->DriverInfoNum == 0);
1223 }
1224
1225 if (OverrideItem->DriverInfoNum == 0) {
1226 FreePool(OverrideItem->ControllerDevicePath);
1227 RemoveEntryList (&OverrideItem->Link);
1228 FreePool (OverrideItem);
1229 }
1230
1231 if (!Found) {
1232 return EFI_NOT_FOUND;
1233 }
1234
1235 return EFI_SUCCESS;
1236 }
1237
1238
1239 /**
1240 Deletes all environment variable(s) that contain the override mappings from Controller Device Path to
1241 a set of Driver Device Paths.
1242
1243 None
1244
1245 @return EFI_SUCCESS
1246
1247 **/
1248 EFI_STATUS
1249 EFIAPI
1250 DeleteOverridesVariables (
1251 VOID
1252 )
1253 {
1254 EFI_STATUS Status;
1255 VOID *VariableBuffer;
1256 UINTN VariableNum;
1257 UINTN BufferSize;
1258 UINTN Index;
1259 CHAR16 OverrideVariableName[40];
1260
1261 //
1262 // Get environment variable(s) number
1263 //
1264 VariableNum =0;
1265 VariableBuffer = GetVariableAndSize (L"PlatDriOver", &gEfiOverrideVariableGuid, &BufferSize);
1266 VariableNum ++;
1267 if (VariableBuffer == NULL) {
1268 return EFI_NOT_FOUND;
1269 }
1270 //
1271 // Check NotEnd to get all PlatDriOverX variable(s)
1272 //
1273 while (*(UINT32*)VariableBuffer) {
1274 UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver%d", VariableNum);
1275 VariableBuffer = GetVariableAndSize (OverrideVariableName, &gEfiOverrideVariableGuid, &BufferSize);
1276 VariableNum ++;
1277 ASSERT (VariableBuffer != NULL);
1278 }
1279
1280 Status = gRT->SetVariable (
1281 L"PlatDriOver",
1282 &gEfiOverrideVariableGuid,
1283 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1284 0,
1285 NULL
1286 );
1287 ASSERT (!EFI_ERROR (Status));
1288 for (Index = 1; Index < VariableNum; Index++) {
1289 UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver%d", Index);
1290 Status = gRT->SetVariable (
1291 OverrideVariableName,
1292 &gEfiOverrideVariableGuid,
1293 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
1294 0,
1295 NULL
1296 );
1297 ASSERT (!EFI_ERROR (Status));
1298 }
1299 return EFI_SUCCESS;
1300 }
1301
1302
1303 /**
1304 Push a controller device path into a globle device path list
1305
1306 @param ControllerDevicePath The controller device path need to push into
1307 stack
1308
1309 @return EFI_SUCCESS
1310
1311 **/
1312 EFI_STATUS
1313 EFIAPI
1314 PushDevPathStack (
1315 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1316 )
1317 {
1318 DEVICE_PATH_STACK_ITEM *DevicePathStackItem;
1319
1320 DevicePathStackItem = AllocateZeroPool (sizeof (DEVICE_PATH_STACK_ITEM));
1321 ASSERT (DevicePathStackItem != NULL);
1322 DevicePathStackItem->Signature = DEVICE_PATH_STACK_ITEM_SIGNATURE;
1323 DevicePathStackItem->DevicePath = DuplicateDevicePath (DevicePath);
1324 InsertTailList (&mDevicePathStack, &DevicePathStackItem->Link);
1325 return EFI_SUCCESS;
1326 }
1327
1328
1329 /**
1330 Pop a controller device path from a globle device path list
1331
1332 @param ControllerDevicePath The controller device path retrieved from stack
1333
1334 @return EFI_SUCCESS
1335 @return EFI_NOT_FOUND
1336
1337 **/
1338 EFI_STATUS
1339 EFIAPI
1340 PopDevPathStack (
1341 OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
1342 )
1343 {
1344 DEVICE_PATH_STACK_ITEM *DevicePathStackItem;
1345 LIST_ENTRY *ItemListIndex;
1346
1347 ItemListIndex = mDevicePathStack.BackLink;
1348 if (ItemListIndex != &mDevicePathStack){
1349 DevicePathStackItem = CR(ItemListIndex, DEVICE_PATH_STACK_ITEM, Link, DEVICE_PATH_STACK_ITEM_SIGNATURE);
1350 if (DevicePath != NULL) {
1351 *DevicePath = DuplicateDevicePath (DevicePathStackItem->DevicePath);
1352 }
1353 FreePool (DevicePathStackItem->DevicePath);
1354 RemoveEntryList (&DevicePathStackItem->Link);
1355 FreePool (DevicePathStackItem);
1356 return EFI_SUCCESS;
1357 }
1358 return EFI_NOT_FOUND;
1359 }
1360
1361
1362 /**
1363 Check whether a controller device path is in a globle device path list
1364
1365 @param ControllerDevicePath The controller device path need to check
1366
1367 @return True
1368 @return False
1369
1370 **/
1371 BOOLEAN
1372 EFIAPI
1373 CheckExistInStack (
1374 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
1375 )
1376 {
1377 DEVICE_PATH_STACK_ITEM *DevicePathStackItem;
1378 LIST_ENTRY *ItemListIndex;
1379 BOOLEAN Found;
1380 UINTN DevicePathSize;
1381
1382 Found = FALSE;
1383 ItemListIndex = mDevicePathStack.BackLink;
1384 while (ItemListIndex != &mDevicePathStack){
1385 DevicePathStackItem = CR(ItemListIndex, DEVICE_PATH_STACK_ITEM, Link, DEVICE_PATH_STACK_ITEM_SIGNATURE);
1386 DevicePathSize = GetDevicePathSize (DevicePath);
1387 if (DevicePathSize == GetDevicePathSize (DevicePathStackItem->DevicePath)) {
1388 if (CompareMem (
1389 DevicePath,
1390 DevicePathStackItem->DevicePath,
1391 GetDevicePathSize (DevicePathStackItem->DevicePath)
1392 ) == 0
1393 ) {
1394 Found = TRUE;
1395 break;
1396 }
1397 }
1398 ItemListIndex = ItemListIndex->BackLink;
1399 }
1400
1401 return Found;
1402 }
1403
1404
1405 /**
1406 According to a file guild, check a Fv file device path is valid. If it is invalid,
1407 try to return the valid device path.
1408 FV address maybe changes for memory layout adjust from time to time, use this funciton
1409 could promise the Fv file device path is right.
1410
1411 @param DevicePath on input, the Fv file device path need to check
1412 on output, the updated valid Fv file device path
1413 @param FileGuid the Fv file guild
1414 @param CallerImageHandle
1415
1416 @retval EFI_INVALID_PARAMETER the input DevicePath or FileGuid is invalid
1417 parameter
1418 @retval EFI_UNSUPPORTED the input DevicePath does not contain Fv file
1419 guild at all
1420 @retval EFI_ALREADY_STARTED the input DevicePath has pointed to Fv file, it
1421 is valid
1422 @retval EFI_SUCCESS has successfully updated the invalid DevicePath,
1423 and return the updated device path in DevicePath
1424
1425 **/
1426 EFI_STATUS
1427 EFIAPI
1428 UpdateFvFileDevicePath (
1429 IN OUT EFI_DEVICE_PATH_PROTOCOL ** DevicePath,
1430 IN EFI_GUID *FileGuid,
1431 IN EFI_HANDLE CallerImageHandle
1432 )
1433 {
1434 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
1435 EFI_DEVICE_PATH_PROTOCOL *LastDeviceNode;
1436 EFI_STATUS Status;
1437 EFI_GUID *GuidPoint;
1438 UINTN Index;
1439 UINTN FvHandleCount;
1440 EFI_HANDLE *FvHandleBuffer;
1441 EFI_FV_FILETYPE Type;
1442 UINTN Size;
1443 EFI_FV_FILE_ATTRIBUTES Attributes;
1444 UINT32 AuthenticationStatus;
1445 BOOLEAN FindFvFile;
1446 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
1447 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
1448 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH FvFileNode;
1449 EFI_HANDLE FoundFvHandle;
1450 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
1451 BOOLEAN HasFVNode;
1452
1453 if ((DevicePath == NULL) || (*DevicePath == NULL)) {
1454 return EFI_INVALID_PARAMETER;
1455 }
1456
1457 //
1458 // Check whether the device path point to the default the input Fv file
1459 //
1460 TempDevicePath = *DevicePath;
1461 LastDeviceNode = TempDevicePath;
1462 while (!EfiIsDevicePathEnd (TempDevicePath)) {
1463 LastDeviceNode = TempDevicePath;
1464 TempDevicePath = EfiNextDevicePathNode (TempDevicePath);
1465 }
1466 GuidPoint = EfiGetNameGuidFromFwVolDevicePathNode (
1467 (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LastDeviceNode
1468 );
1469 if (GuidPoint == NULL) {
1470 //
1471 // if this option does not points to a Fv file, just return EFI_UNSUPPORTED
1472 //
1473 return EFI_UNSUPPORTED;
1474 }
1475
1476 if (FileGuid != NULL) {
1477 if (!CompareGuid (GuidPoint, FileGuid)) {
1478 //
1479 // If the Fv file is not the input file guid, just return EFI_UNSUPPORTED
1480 //
1481 return EFI_UNSUPPORTED;
1482 }
1483 } else {
1484 FileGuid = GuidPoint;
1485 }
1486
1487 //
1488 // Check to see if the device path contain memory map node
1489 //
1490 TempDevicePath = *DevicePath;
1491 HasFVNode = FALSE;
1492 while (!EfiIsDevicePathEnd (TempDevicePath)) {
1493 //
1494 // Use old Device Path
1495 //
1496 if (DevicePathType (TempDevicePath) == HARDWARE_DEVICE_PATH &&
1497 DevicePathSubType (TempDevicePath) == HW_MEMMAP_DP) {
1498 HasFVNode = TRUE;
1499 break;
1500 }
1501 TempDevicePath = EfiNextDevicePathNode (TempDevicePath);
1502 }
1503
1504 if (!HasFVNode) {
1505 return EFI_UNSUPPORTED;
1506 }
1507
1508 //
1509 // Check whether the input Fv file device path is valid
1510 //
1511 TempDevicePath = *DevicePath;
1512 FoundFvHandle = NULL;
1513 Status = gBS->LocateDevicePath (
1514 &gEfiFirmwareVolume2ProtocolGuid,
1515 &TempDevicePath,
1516 &FoundFvHandle
1517 );
1518 if (!EFI_ERROR (Status)) {
1519 Status = gBS->HandleProtocol (
1520 FoundFvHandle,
1521 &gEfiFirmwareVolume2ProtocolGuid,
1522 (VOID **) &Fv
1523 );
1524 if (!EFI_ERROR (Status)) {
1525 //
1526 // Set FV ReadFile Buffer as NULL, only need to check whether input Fv file exist there
1527 //
1528 Status = Fv->ReadFile (
1529 Fv,
1530 FileGuid,
1531 NULL,
1532 &Size,
1533 &Type,
1534 &Attributes,
1535 &AuthenticationStatus
1536 );
1537 if (!EFI_ERROR (Status)) {
1538 return EFI_ALREADY_STARTED;
1539 }
1540 }
1541 }
1542
1543 //
1544 // Look for the input wanted FV file in current FV
1545 // First, try to look for in Caller own FV. Caller and input wanted FV file usually are in the same FV
1546 //
1547 FindFvFile = FALSE;
1548 FoundFvHandle = NULL;
1549 Status = gBS->HandleProtocol (
1550 CallerImageHandle,
1551 &gEfiLoadedImageProtocolGuid,
1552 (VOID **) &LoadedImage
1553 );
1554 if (!EFI_ERROR (Status)) {
1555 Status = gBS->HandleProtocol (
1556 LoadedImage->DeviceHandle,
1557 &gEfiFirmwareVolume2ProtocolGuid,
1558 (VOID **) &Fv
1559 );
1560 if (!EFI_ERROR (Status)) {
1561 Status = Fv->ReadFile (
1562 Fv,
1563 FileGuid,
1564 NULL,
1565 &Size,
1566 &Type,
1567 &Attributes,
1568 &AuthenticationStatus
1569 );
1570 if (!EFI_ERROR (Status)) {
1571 FindFvFile = TRUE;
1572 FoundFvHandle = LoadedImage->DeviceHandle;
1573 }
1574 }
1575 }
1576 //
1577 // Second, if fail to find, try to enumerate all FV
1578 //
1579 if (!FindFvFile) {
1580 gBS->LocateHandleBuffer (
1581 ByProtocol,
1582 &gEfiFirmwareVolume2ProtocolGuid,
1583 NULL,
1584 &FvHandleCount,
1585 &FvHandleBuffer
1586 );
1587 for (Index = 0; Index < FvHandleCount; Index++) {
1588 gBS->HandleProtocol (
1589 FvHandleBuffer[Index],
1590 &gEfiFirmwareVolume2ProtocolGuid,
1591 (VOID **) &Fv
1592 );
1593
1594 Status = Fv->ReadFile (
1595 Fv,
1596 FileGuid,
1597 NULL,
1598 &Size,
1599 &Type,
1600 &Attributes,
1601 &AuthenticationStatus
1602 );
1603 if (EFI_ERROR (Status)) {
1604 //
1605 // Skip if input Fv file not in the FV
1606 //
1607 continue;
1608 }
1609 FindFvFile = TRUE;
1610 FoundFvHandle = FvHandleBuffer[Index];
1611 break;
1612 }
1613 }
1614
1615 if (FindFvFile) {
1616 //
1617 // Build the shell device path
1618 //
1619 NewDevicePath = DevicePathFromHandle (FoundFvHandle);
1620 EfiInitializeFwVolDevicepathNode (&FvFileNode, FileGuid);
1621 NewDevicePath = AppendDevicePathNode (NewDevicePath, (EFI_DEVICE_PATH_PROTOCOL *) &FvFileNode);
1622 *DevicePath = NewDevicePath;
1623 return EFI_SUCCESS;
1624 }
1625 return EFI_NOT_FOUND;
1626 }
1627
1628
1629 /**
1630 Read the EFI variable (VendorGuid/Name) and return a dynamically allocated
1631 buffer, and the size of the buffer. If failure return NULL.
1632
1633 @param Name String part of EFI variable name
1634 @param VendorGuid GUID part of EFI variable name
1635 @param VariableSize Returns the size of the EFI variable that was
1636 read
1637
1638 @return Dynamically allocated memory that contains a copy of the EFI variable.
1639 @return Caller is responsible freeing the buffer.
1640 @retval NULL Variable was not read
1641
1642 **/
1643 VOID *
1644 EFIAPI
1645 GetVariableAndSize (
1646 IN CHAR16 *Name,
1647 IN EFI_GUID *VendorGuid,
1648 OUT UINTN *VariableSize
1649 )
1650 {
1651 EFI_STATUS Status;
1652 UINTN BufferSize;
1653 VOID *Buffer;
1654
1655 Buffer = NULL;
1656
1657 //
1658 // Pass in a zero size buffer to find the required buffer size.
1659 //
1660 BufferSize = 0;
1661 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
1662 if (Status == EFI_BUFFER_TOO_SMALL) {
1663 //
1664 // Allocate the buffer to return
1665 //
1666 Buffer = AllocateZeroPool (BufferSize);
1667 if (Buffer == NULL) {
1668 return NULL;
1669 }
1670 //
1671 // Read variable into the allocated buffer.
1672 //
1673 Status = gRT->GetVariable (Name, VendorGuid, NULL, &BufferSize, Buffer);
1674 if (EFI_ERROR (Status)) {
1675 BufferSize = 0;
1676 }
1677 }
1678
1679 *VariableSize = BufferSize;
1680 return Buffer;
1681 }
1682
1683
1684 /**
1685 This function will create all handles associate with every device
1686 path node. If the handle associate with one device path node can not
1687 be created success, then still give one chance to do the dispatch,
1688 which load the missing drivers if possible.
1689
1690 @param DevicePathToConnect The device path which will be connected, it can
1691 be a multi-instance device path
1692
1693 @retval EFI_SUCCESS All handles associate with every device path
1694 node have been created
1695 @retval EFI_OUT_OF_RESOURCES There is no resource to create new handles
1696 @retval EFI_NOT_FOUND Create the handle associate with one device
1697 path node failed
1698
1699 **/
1700 EFI_STATUS
1701 EFIAPI
1702 ConnectDevicePath (
1703 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathToConnect
1704 )
1705 {
1706 EFI_STATUS Status;
1707 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1708 EFI_DEVICE_PATH_PROTOCOL *CopyOfDevicePath;
1709 EFI_DEVICE_PATH_PROTOCOL *Instance;
1710 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
1711 EFI_DEVICE_PATH_PROTOCOL *Next;
1712 EFI_HANDLE Handle;
1713 EFI_HANDLE PreviousHandle;
1714 UINTN Size;
1715
1716 if (DevicePathToConnect == NULL) {
1717 return EFI_SUCCESS;
1718 }
1719
1720 DevicePath = DuplicateDevicePath (DevicePathToConnect);
1721 CopyOfDevicePath = DevicePath;
1722 if (DevicePath == NULL) {
1723 return EFI_OUT_OF_RESOURCES;
1724 }
1725
1726 do {
1727 //
1728 // The outer loop handles multi instance device paths.
1729 // Only console variables contain multiple instance device paths.
1730 //
1731 // After this call DevicePath points to the next Instance
1732 //
1733 Instance = GetNextDevicePathInstance (&DevicePath, &Size);
1734 Next = Instance;
1735 while (!IsDevicePathEndType (Next)) {
1736 Next = NextDevicePathNode (Next);
1737 }
1738
1739 SetDevicePathEndNode (Next);
1740
1741 //
1742 // Start the real work of connect with RemainingDevicePath
1743 //
1744 PreviousHandle = NULL;
1745 do {
1746 //
1747 // Find the handle that best matches the Device Path. If it is only a
1748 // partial match the remaining part of the device path is returned in
1749 // RemainingDevicePath.
1750 //
1751 RemainingDevicePath = Instance;
1752 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &Handle);
1753
1754 if (!EFI_ERROR (Status)) {
1755 if (Handle == PreviousHandle) {
1756 //
1757 // If no forward progress is made try invoking the Dispatcher.
1758 // A new FV may have been added to the system an new drivers
1759 // may now be found.
1760 // Status == EFI_SUCCESS means a driver was dispatched
1761 // Status == EFI_NOT_FOUND means no new drivers were dispatched
1762 //
1763 Status = gDS->Dispatch ();
1764 }
1765
1766 if (!EFI_ERROR (Status)) {
1767 PreviousHandle = Handle;
1768 //
1769 // Connect all drivers that apply to Handle and RemainingDevicePath,
1770 // the Recursive flag is FALSE so only one level will be expanded.
1771 //
1772 // Do not check the connect status here, if the connect controller fail,
1773 // then still give the chance to do dispatch, because partial
1774 // RemainingDevicepath may be in the new FV
1775 //
1776 // 1. If the connect fail, RemainingDevicepath and handle will not
1777 // change, so next time will do the dispatch, then dispatch's status
1778 // will take effect
1779 // 2. If the connect success, the RemainingDevicepath and handle will
1780 // change, then avoid the dispatch, we have chance to continue the
1781 // next connection
1782 //
1783 gBS->ConnectController (Handle, NULL, RemainingDevicePath, FALSE);
1784 }
1785 }
1786 //
1787 // Loop until RemainingDevicePath is an empty device path
1788 //
1789 } while (!EFI_ERROR (Status) && !IsDevicePathEnd (RemainingDevicePath));
1790
1791 } while (DevicePath != NULL);
1792
1793 if (CopyOfDevicePath != NULL) {
1794 FreePool (CopyOfDevicePath);
1795 }
1796 //
1797 // All handle with DevicePath exists in the handle database
1798 //
1799 return Status;
1800 }