2 MDE DXE Services Library provides functions that simplify the development of DXE Drivers.
3 These functions help access data from sections of FFS files or from file path.
5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/DebugLib.h>
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/UefiBootServicesTableLib.h>
15 #include <Library/DevicePathLib.h>
16 #include <Library/UefiLib.h>
17 #include <Library/DxeServicesLib.h>
18 #include <Protocol/FirmwareVolume2.h>
19 #include <Protocol/LoadedImage.h>
20 #include <Protocol/LoadFile2.h>
21 #include <Protocol/LoadFile.h>
22 #include <Protocol/SimpleFileSystem.h>
23 #include <Guid/FileInfo.h>
26 Identify the device handle from which the Image is loaded from. As this device handle is passed to
27 GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL
28 protocol instance should be located succesfully by calling gBS->HandleProtocol ().
30 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed
31 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.
33 If ImageHandle is NULL, then ASSERT ();
34 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();
36 @param ImageHandle The firmware allocated handle for UEFI image.
38 @retval EFI_HANDLE The device handle from which the Image is loaded from.
42 InternalImageHandleToFvHandle (
43 EFI_HANDLE ImageHandle
47 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
49 ASSERT (ImageHandle
!= NULL
);
51 Status
= gBS
->HandleProtocol (
52 (EFI_HANDLE
*) ImageHandle
,
53 &gEfiLoadedImageProtocolGuid
,
54 (VOID
**) &LoadedImage
57 ASSERT_EFI_ERROR (Status
);
60 // The LoadedImage->DeviceHandle may be NULL.
61 // For example for DxeCore, there is LoadedImage protocol installed for it, but the
62 // LoadedImage->DeviceHandle could not be initialized before the FV2 (contain DxeCore)
63 // protocol is installed.
65 return LoadedImage
->DeviceHandle
;
70 Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware
71 Section type and instance number from the specified Firmware Volume.
73 This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to
74 carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed
75 by NameGuid, SectionType and SectionInstance.
77 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection ()
78 found in PI Specification.
80 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section
81 is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND
84 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated
85 by this function. This function can be only called at TPL_NOTIFY and below.
87 If NameGuid is NULL, then ASSERT();
88 If Buffer is NULL, then ASSERT();
89 If Size is NULL, then ASSERT().
91 @param FvHandle The device handle that contains a instance of
92 EFI_FIRMWARE_VOLUME2_PROTOCOL instance.
93 @param NameGuid The GUID name of a Firmware File.
94 @param SectionType The Firmware Section type.
95 @param SectionInstance The instance number of Firmware Section to
96 read from starting from 0.
97 @param Buffer On output, Buffer contains the the data read
98 from the section in the Firmware File found.
99 @param Size On output, the size of Buffer.
101 @retval EFI_SUCCESS The image is found and data and size is returned.
102 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType
104 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the
105 output data buffer or complete the operations.
106 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the
108 @retval EFI_ACCESS_DENIED The firmware volume containing the searched
109 Firmware File is configured to disallow reads.
113 InternalGetSectionFromFv (
114 IN EFI_HANDLE FvHandle
,
115 IN CONST EFI_GUID
*NameGuid
,
116 IN EFI_SECTION_TYPE SectionType
,
117 IN UINTN SectionInstance
,
123 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
124 UINT32 AuthenticationStatus
;
126 ASSERT (NameGuid
!= NULL
);
127 ASSERT (Buffer
!= NULL
);
128 ASSERT (Size
!= NULL
);
130 if (FvHandle
== NULL
) {
132 // Return EFI_NOT_FOUND directly for NULL FvHandle.
134 return EFI_NOT_FOUND
;
137 Status
= gBS
->HandleProtocol (
139 &gEfiFirmwareVolume2ProtocolGuid
,
142 if (EFI_ERROR (Status
)) {
143 return EFI_NOT_FOUND
;
147 // Read desired section content in NameGuid file
151 Status
= Fv
->ReadSection (
158 &AuthenticationStatus
161 if (EFI_ERROR (Status
) && (SectionType
== EFI_SECTION_TE
)) {
163 // Try reading PE32 section, if the required section is TE type
167 Status
= Fv
->ReadSection (
174 &AuthenticationStatus
182 Searches all the available firmware volumes and returns the first matching FFS section.
184 This function searches all the firmware volumes for FFS files with FV file type specified by FileType
185 The order that the firmware volumes is searched is not deterministic. For each available FV a search
186 is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType,
187 the FileInstance instance will be the matched FFS file. For each FFS file found a search
188 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
189 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
190 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
191 It is the caller's responsibility to use FreePool() to free the allocated buffer.
192 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
193 are retrieved from an FFS file based on SectionType and SectionInstance.
195 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
196 the search will be retried with a section type of EFI_SECTION_PE32.
197 This function must be called with a TPL <= TPL_NOTIFY.
199 If Buffer is NULL, then ASSERT().
200 If Size is NULL, then ASSERT().
202 @param FileType Indicates the FV file type to search for within all
204 @param FileInstance Indicates which file instance within all available
205 FVs specified by FileType.
206 FileInstance starts from zero.
207 @param SectionType Indicates the FFS section type to search for
209 specified by FileType with FileInstance.
210 @param SectionInstance Indicates which section instance within the FFS file
211 specified by FileType with FileInstance to retrieve.
212 SectionInstance starts from zero.
213 @param Buffer On output, a pointer to a callee allocated buffer
214 containing the FFS file section that was found.
215 Is it the caller's responsibility to free this
216 buffer using FreePool().
217 @param Size On output, a pointer to the size, in bytes, of Buffer.
219 @retval EFI_SUCCESS The specified FFS section was returned.
220 @retval EFI_NOT_FOUND The specified FFS section could not be found.
221 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
222 the matching FFS section.
223 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
225 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because
226 the firmware volume that
227 contains the matching FFS section does not allow reads.
231 GetSectionFromAnyFvByFileType (
232 IN EFI_FV_FILETYPE FileType
,
233 IN UINTN FileInstance
,
234 IN EFI_SECTION_TYPE SectionType
,
235 IN UINTN SectionInstance
,
241 EFI_HANDLE
*HandleBuffer
;
247 EFI_FV_FILE_ATTRIBUTES Attributes
;
248 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
250 ASSERT (Buffer
!= NULL
);
251 ASSERT (Size
!= NULL
);
254 // Locate all available FVs.
257 Status
= gBS
->LocateHandleBuffer (
259 &gEfiFirmwareVolume2ProtocolGuid
,
264 if (EFI_ERROR (Status
)) {
269 // Go through FVs one by one to find the required section data.
271 for (IndexFv
= 0; IndexFv
< HandleCount
; IndexFv
++) {
272 Status
= gBS
->HandleProtocol (
273 HandleBuffer
[IndexFv
],
274 &gEfiFirmwareVolume2ProtocolGuid
,
277 if (EFI_ERROR (Status
)) {
282 // Use Firmware Volume 2 Protocol to search for a file of type FileType in all FVs.
284 IndexFile
= FileInstance
+ 1;
287 Status
= Fv
->GetNextFile (Fv
, &Key
, &FileType
, &NameGuid
, &Attributes
, Size
);
288 if (EFI_ERROR (Status
)) {
292 } while (IndexFile
> 0);
295 // Fv File with the required FV file type is found.
296 // Search the section file in the found FV file.
298 if (IndexFile
== 0) {
299 Status
= InternalGetSectionFromFv (
300 HandleBuffer
[IndexFv
],
308 if (!EFI_ERROR (Status
)) {
315 // The required FFS section file is not found.
317 if (IndexFv
== HandleCount
) {
318 Status
= EFI_NOT_FOUND
;
322 if (HandleBuffer
!= NULL
) {
323 FreePool(HandleBuffer
);
330 Searches all the availables firmware volumes and returns the first matching FFS section.
332 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
333 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search
334 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
335 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
336 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
337 It is the caller's responsibility to use FreePool() to free the allocated buffer.
338 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
339 are retrieved from an FFS file based on SectionType and SectionInstance.
341 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
342 the search will be retried with a section type of EFI_SECTION_PE32.
343 This function must be called with a TPL <= TPL_NOTIFY.
345 If NameGuid is NULL, then ASSERT().
346 If Buffer is NULL, then ASSERT().
347 If Size is NULL, then ASSERT().
350 @param NameGuid A pointer to to the FFS filename GUID to search for
351 within any of the firmware volumes in the platform.
352 @param SectionType Indicates the FFS section type to search for within
353 the FFS file specified by NameGuid.
354 @param SectionInstance Indicates which section instance within the FFS file
355 specified by NameGuid to retrieve.
356 @param Buffer On output, a pointer to a callee allocated buffer
357 containing the FFS file section that was found.
358 Is it the caller's responsibility to free this buffer
360 @param Size On output, a pointer to the size, in bytes, of Buffer.
362 @retval EFI_SUCCESS The specified FFS section was returned.
363 @retval EFI_NOT_FOUND The specified FFS section could not be found.
364 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
365 retrieve the matching FFS section.
366 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
368 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
370 contains the matching FFS section does not allow reads.
374 GetSectionFromAnyFv (
375 IN CONST EFI_GUID
*NameGuid
,
376 IN EFI_SECTION_TYPE SectionType
,
377 IN UINTN SectionInstance
,
383 EFI_HANDLE
*HandleBuffer
;
389 // Search the FV that contain the caller's FFS first.
390 // FV builder can choose to build FFS into the this FV
391 // so that this implementation of GetSectionFromAnyFv
392 // will locate the FFS faster.
394 FvHandle
= InternalImageHandleToFvHandle (gImageHandle
);
395 Status
= InternalGetSectionFromFv (
403 if (!EFI_ERROR (Status
)) {
408 Status
= gBS
->LocateHandleBuffer (
410 &gEfiFirmwareVolume2ProtocolGuid
,
415 if (EFI_ERROR (Status
)) {
419 for (Index
= 0; Index
< HandleCount
; Index
++) {
421 // Skip the FV that contain the caller's FFS
423 if (HandleBuffer
[Index
] != FvHandle
) {
424 Status
= InternalGetSectionFromFv (
433 if (!EFI_ERROR (Status
)) {
440 if (Index
== HandleCount
) {
441 Status
= EFI_NOT_FOUND
;
446 if (HandleBuffer
!= NULL
) {
447 FreePool(HandleBuffer
);
454 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section.
456 This function searches the firmware volume that the currently executing module was loaded
457 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search
458 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance
459 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
460 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
461 It is the caller's responsibility to use FreePool() to free the allocated buffer.
462 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from
463 an FFS file based on SectionType and SectionInstance.
465 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.
466 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
467 the search will be retried with a section type of EFI_SECTION_PE32.
469 This function must be called with a TPL <= TPL_NOTIFY.
470 If NameGuid is NULL, then ASSERT().
471 If Buffer is NULL, then ASSERT().
472 If Size is NULL, then ASSERT().
474 @param NameGuid A pointer to to the FFS filename GUID to search for
475 within the firmware volumes that the currently
476 executing module was loaded from.
477 @param SectionType Indicates the FFS section type to search for within
478 the FFS file specified by NameGuid.
479 @param SectionInstance Indicates which section instance within the FFS file
480 specified by NameGuid to retrieve.
481 @param Buffer On output, a pointer to a callee allocated buffer
482 containing the FFS file section that was found.
483 Is it the caller's responsibility to free this buffer
485 @param Size On output, a pointer to the size, in bytes, of Buffer.
488 @retval EFI_SUCCESS The specified FFS section was returned.
489 @retval EFI_NOT_FOUND The specified FFS section could not be found.
490 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
491 the matching FFS section.
492 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
494 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
495 firmware volume that contains the matching FFS
496 section does not allow reads.
501 IN CONST EFI_GUID
*NameGuid
,
502 IN EFI_SECTION_TYPE SectionType
,
503 IN UINTN SectionInstance
,
508 return InternalGetSectionFromFv (
509 InternalImageHandleToFvHandle(gImageHandle
),
520 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.
522 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.
523 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType,
524 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(),
525 and the size of the allocated buffer is returned in Size. It is the caller's responsibility
526 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for
527 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.
529 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.
530 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
531 the search will be retried with a section type of EFI_SECTION_PE32.
532 This function must be called with a TPL <= TPL_NOTIFY.
534 If Buffer is NULL, then ASSERT().
535 If Size is NULL, then ASSERT().
538 @param SectionType Indicates the FFS section type to search for within
539 the FFS file that the currently executing module
541 @param SectionInstance Indicates which section instance to retrieve within
542 the FFS file that the currently executing module
544 @param Buffer On output, a pointer to a callee allocated buffer
545 containing the FFS file section that was found.
546 Is it the caller's responsibility to free this buffer
548 @param Size On output, a pointer to the size, in bytes, of Buffer.
550 @retval EFI_SUCCESS The specified FFS section was returned.
551 @retval EFI_NOT_FOUND The specified FFS section could not be found.
552 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
553 the matching FFS section.
554 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
556 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
557 firmware volume that contains the matching FFS
558 section does not allow reads.
564 IN EFI_SECTION_TYPE SectionType
,
565 IN UINTN SectionInstance
,
570 return InternalGetSectionFromFv(
571 InternalImageHandleToFvHandle(gImageHandle
),
582 Get the image file buffer data and buffer size by its device path.
584 Access the file either from a firmware volume, from a file system interface,
585 or from the load file interface.
587 Allocate memory to store the found image. The caller is responsible to free memory.
589 If FilePath is NULL, then NULL is returned.
590 If FileSize is NULL, then NULL is returned.
591 If AuthenticationStatus is NULL, then NULL is returned.
593 @param[in] BootPolicy Policy for Open Image File.If TRUE, indicates
594 that the request originates from the boot
595 manager, and that the boot manager is
596 attempting to load FilePath as a boot
597 selection. If FALSE, then FilePath must
598 match an exact file to be loaded.
599 @param[in] FilePath The pointer to the device path of the file
600 that is absracted to the file buffer.
601 @param[out] FileSize The pointer to the size of the abstracted
603 @param[out] AuthenticationStatus Pointer to the authentication status.
605 @retval NULL FilePath is NULL, or FileSize is NULL, or AuthenticationStatus is NULL, or the file can't be found.
606 @retval other The abstracted file buffer. The caller is responsible to free memory.
610 GetFileBufferByFilePath (
611 IN BOOLEAN BootPolicy
,
612 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
614 OUT UINT32
*AuthenticationStatus
617 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
618 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
619 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePathNode
;
621 EFI_GUID
*FvNameGuid
;
622 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
623 EFI_SECTION_TYPE SectionType
;
625 UINTN ImageBufferSize
;
626 EFI_FV_FILETYPE Type
;
627 EFI_FV_FILE_ATTRIBUTES Attrib
;
628 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
629 EFI_FILE_HANDLE FileHandle
;
630 EFI_FILE_HANDLE LastHandle
;
631 EFI_FILE_INFO
*FileInfo
;
633 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
634 EFI_LOAD_FILE2_PROTOCOL
*LoadFile2
;
638 // Check input File device path.
640 if (FilePath
== NULL
|| FileSize
== NULL
|| AuthenticationStatus
== NULL
) {
645 // Init local variable
647 TempDevicePathNode
= NULL
;
653 *AuthenticationStatus
= 0;
656 // Copy File Device Path
658 OrigDevicePathNode
= DuplicateDevicePath (FilePath
);
659 if (OrigDevicePathNode
== NULL
) {
664 // Check whether this device path support FV2 protocol.
665 // Is so, this device path may contain a Image.
667 DevicePathNode
= OrigDevicePathNode
;
668 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
669 if (!EFI_ERROR (Status
)) {
671 // For FwVol File system there is only a single file name that is a GUID.
673 FvNameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePathNode
);
674 if (FvNameGuid
== NULL
) {
675 Status
= EFI_INVALID_PARAMETER
;
678 // Read image from the firmware file
680 Status
= gBS
->HandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&FwVol
);
681 if (!EFI_ERROR (Status
)) {
682 SectionType
= EFI_SECTION_PE32
;
684 Status
= FwVol
->ReadSection (
689 (VOID
**)&ImageBuffer
,
693 if (EFI_ERROR (Status
)) {
695 // Try a raw file, since a PE32 SECTION does not exist
697 if (ImageBuffer
!= NULL
) {
698 FreePool (ImageBuffer
);
699 *AuthenticationStatus
= 0;
702 Status
= FwVol
->ReadFile (
705 (VOID
**)&ImageBuffer
,
714 if (!EFI_ERROR (Status
)) {
720 // Attempt to access the file via a file system interface
722 DevicePathNode
= OrigDevicePathNode
;
723 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
724 if (!EFI_ERROR (Status
)) {
725 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
726 if (!EFI_ERROR (Status
)) {
728 // Open the Volume to get the File System handle
730 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
731 if (!EFI_ERROR (Status
)) {
733 // Duplicate the device path to avoid the access to unaligned device path node.
734 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
735 // nodes, It assures the fields in device path nodes are 2 byte aligned.
737 TempDevicePathNode
= DuplicateDevicePath (DevicePathNode
);
738 if (TempDevicePathNode
== NULL
) {
739 FileHandle
->Close (FileHandle
);
741 // Setting Status to an EFI_ERROR value will cause the rest of
742 // the file system support below to be skipped.
744 Status
= EFI_OUT_OF_RESOURCES
;
747 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
748 // directory information and filename can be seperate. The goal is to inch
749 // our way down each device path node and close the previous node
751 DevicePathNode
= TempDevicePathNode
;
752 while (!EFI_ERROR (Status
) && !IsDevicePathEnd (DevicePathNode
)) {
753 if (DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
||
754 DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
) {
755 Status
= EFI_UNSUPPORTED
;
759 LastHandle
= FileHandle
;
762 Status
= LastHandle
->Open (
765 ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
,
771 // Close the previous node
773 LastHandle
->Close (LastHandle
);
775 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
778 if (!EFI_ERROR (Status
)) {
780 // We have found the file. Now we need to read it. Before we can read the file we need to
781 // figure out how big the file is.
785 Status
= FileHandle
->GetInfo (
792 if (Status
== EFI_BUFFER_TOO_SMALL
) {
793 FileInfo
= AllocatePool (FileInfoSize
);
794 if (FileInfo
== NULL
) {
795 Status
= EFI_OUT_OF_RESOURCES
;
797 Status
= FileHandle
->GetInfo (
806 if (!EFI_ERROR (Status
) && (FileInfo
!= NULL
)) {
807 if ((FileInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) {
809 // Allocate space for the file
811 ImageBuffer
= AllocatePool ((UINTN
)FileInfo
->FileSize
);
812 if (ImageBuffer
== NULL
) {
813 Status
= EFI_OUT_OF_RESOURCES
;
816 // Read the file into the buffer we allocated
818 ImageBufferSize
= (UINTN
)FileInfo
->FileSize
;
819 Status
= FileHandle
->Read (FileHandle
, &ImageBufferSize
, ImageBuffer
);
825 // Close the file and Free FileInfo and TempDevicePathNode since we are done
827 if (FileInfo
!= NULL
) {
830 if (FileHandle
!= NULL
) {
831 FileHandle
->Close (FileHandle
);
833 if (TempDevicePathNode
!= NULL
) {
834 FreePool (TempDevicePathNode
);
838 if (!EFI_ERROR (Status
)) {
844 // Attempt to access the file via LoadFile2 interface
847 DevicePathNode
= OrigDevicePathNode
;
848 Status
= gBS
->LocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &DevicePathNode
, &Handle
);
849 if (!EFI_ERROR (Status
)) {
850 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFile2ProtocolGuid
, (VOID
**)&LoadFile2
);
851 if (!EFI_ERROR (Status
)) {
853 // Call LoadFile2 with the correct buffer size
857 Status
= LoadFile2
->LoadFile (
864 if (Status
== EFI_BUFFER_TOO_SMALL
) {
865 ImageBuffer
= AllocatePool (ImageBufferSize
);
866 if (ImageBuffer
== NULL
) {
867 Status
= EFI_OUT_OF_RESOURCES
;
869 Status
= LoadFile2
->LoadFile (
879 if (!EFI_ERROR (Status
)) {
886 // Attempt to access the file via LoadFile interface
888 DevicePathNode
= OrigDevicePathNode
;
889 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &DevicePathNode
, &Handle
);
890 if (!EFI_ERROR (Status
)) {
891 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
892 if (!EFI_ERROR (Status
)) {
894 // Call LoadFile with the correct buffer size
898 Status
= LoadFile
->LoadFile (
905 if (Status
== EFI_BUFFER_TOO_SMALL
) {
906 ImageBuffer
= AllocatePool (ImageBufferSize
);
907 if (ImageBuffer
== NULL
) {
908 Status
= EFI_OUT_OF_RESOURCES
;
910 Status
= LoadFile
->LoadFile (
924 if (EFI_ERROR (Status
)) {
925 if (ImageBuffer
!= NULL
) {
926 FreePool (ImageBuffer
);
931 *FileSize
= ImageBufferSize
;
934 FreePool (OrigDevicePathNode
);
940 Searches all the available firmware volumes and returns the file device path of first matching
943 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
944 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search
945 is made for FFS sections of type SectionType.
947 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
948 the search will be retried with a section type of EFI_SECTION_PE32.
949 This function must be called with a TPL <= TPL_NOTIFY.
951 If NameGuid is NULL, then ASSERT().
953 @param NameGuid A pointer to to the FFS filename GUID to search for
954 within any of the firmware volumes in the platform.
955 @param SectionType Indicates the FFS section type to search for within
956 the FFS file specified by NameGuid.
957 @param SectionInstance Indicates which section instance within the FFS file
958 specified by NameGuid to retrieve.
959 @param FvFileDevicePath Device path for the target FFS
962 @retval EFI_SUCCESS The specified file device path of FFS section was returned.
963 @retval EFI_NOT_FOUND The specified file device path of FFS section could not be found.
964 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
966 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
967 firmware volume that contains the matching FFS section does not
969 @retval EFI_INVALID_PARAMETER FvFileDevicePath is NULL.
974 GetFileDevicePathFromAnyFv (
975 IN CONST EFI_GUID
*NameGuid
,
976 IN EFI_SECTION_TYPE SectionType
,
977 IN UINTN SectionInstance
,
978 OUT EFI_DEVICE_PATH_PROTOCOL
**FvFileDevicePath
982 EFI_HANDLE
*HandleBuffer
;
986 EFI_DEVICE_PATH_PROTOCOL
*FvDevicePath
;
987 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*TempFvFileDevicePath
;
991 if (FvFileDevicePath
== NULL
) {
992 return EFI_INVALID_PARAMETER
;
997 TempFvFileDevicePath
= NULL
;
1002 // Search the FV that contain the caller's FFS first.
1003 // FV builder can choose to build FFS into the this FV
1004 // so that this implementation of GetSectionFromAnyFv
1005 // will locate the FFS faster.
1007 FvHandle
= InternalImageHandleToFvHandle (gImageHandle
);
1008 Status
= InternalGetSectionFromFv (
1016 if (!EFI_ERROR (Status
)) {
1020 Status
= gBS
->LocateHandleBuffer (
1022 &gEfiFirmwareVolume2ProtocolGuid
,
1027 if (EFI_ERROR (Status
)) {
1031 for (Index
= 0; Index
< HandleCount
; Index
++) {
1033 // Skip the FV that contain the caller's FFS
1035 if (HandleBuffer
[Index
] != FvHandle
) {
1036 Status
= InternalGetSectionFromFv (
1037 HandleBuffer
[Index
],
1045 if (!EFI_ERROR (Status
)) {
1047 // Update FvHandle to the current handle.
1049 FvHandle
= HandleBuffer
[Index
];
1055 if (Index
== HandleCount
) {
1056 Status
= EFI_NOT_FOUND
;
1060 if (Status
== EFI_SUCCESS
) {
1062 // Build a device path to the file in the FV to pass into gBS->LoadImage
1064 Status
= gBS
->HandleProtocol (FvHandle
, &gEfiDevicePathProtocolGuid
, (VOID
**)&FvDevicePath
);
1065 if (EFI_ERROR (Status
)) {
1066 *FvFileDevicePath
= NULL
;
1068 TempFvFileDevicePath
= AllocateZeroPool (sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
) + END_DEVICE_PATH_LENGTH
);
1069 if (TempFvFileDevicePath
== NULL
) {
1070 *FvFileDevicePath
= NULL
;
1071 return EFI_OUT_OF_RESOURCES
;
1073 EfiInitializeFwVolDevicepathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*)TempFvFileDevicePath
, NameGuid
);
1074 SetDevicePathEndNode (NextDevicePathNode (TempFvFileDevicePath
));
1075 *FvFileDevicePath
= AppendDevicePath (
1077 (EFI_DEVICE_PATH_PROTOCOL
*)TempFvFileDevicePath
1079 FreePool (TempFvFileDevicePath
);
1083 if (Buffer
!= NULL
) {
1087 if (HandleBuffer
!= NULL
) {
1088 FreePool (HandleBuffer
);