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 - 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/DebugLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/DevicePathLib.h>
21 #include <Library/UefiLib.h>
22 #include <Library/DxeServicesLib.h>
23 #include <Protocol/FirmwareVolume2.h>
24 #include <Protocol/LoadedImage.h>
25 #include <Protocol/LoadFile2.h>
26 #include <Protocol/LoadFile.h>
27 #include <Protocol/SimpleFileSystem.h>
28 #include <Guid/FileInfo.h>
31 Identify the device handle from which the Image is loaded from. As this device handle is passed to
32 GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL
33 protocol instance should be located succesfully by calling gBS->HandleProtocol ().
35 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed
36 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.
38 If ImageHandle is NULL, then ASSERT ();
39 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();
41 @param ImageHandle The firmware allocated handle for UEFI image.
43 @retval EFI_HANDLE The device handle from which the Image is loaded from.
47 InternalImageHandleToFvHandle (
48 EFI_HANDLE ImageHandle
52 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
54 ASSERT (ImageHandle
!= NULL
);
56 Status
= gBS
->HandleProtocol (
57 (EFI_HANDLE
*) ImageHandle
,
58 &gEfiLoadedImageProtocolGuid
,
59 (VOID
**) &LoadedImage
62 ASSERT_EFI_ERROR (Status
);
64 return LoadedImage
->DeviceHandle
;
69 Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware
70 Section type and instance number from the specified Firmware Volume.
72 This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to
73 carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed
74 by NameGuid, SectionType and SectionInstance.
76 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection ()
77 found in PI Specification.
79 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section
80 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
83 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated
84 by this function. This function can be only called at TPL_NOTIFY and below.
86 If FvHandle is NULL, then ASSERT ();
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 ASSERT (FvHandle
!= NULL
);
132 Status
= gBS
->HandleProtocol (
134 &gEfiFirmwareVolume2ProtocolGuid
,
137 if (EFI_ERROR (Status
)) {
138 return EFI_NOT_FOUND
;
142 // Read desired section content in NameGuid file
146 Status
= Fv
->ReadSection (
153 &AuthenticationStatus
156 if (EFI_ERROR (Status
) && (SectionType
== EFI_SECTION_TE
)) {
158 // Try reading PE32 section, if the required section is TE type
162 Status
= Fv
->ReadSection (
169 &AuthenticationStatus
177 Searches all the available firmware volumes and returns the first matching FFS section.
179 This function searches all the firmware volumes for FFS files with FV file type specified by FileType
180 The order that the firmware volumes is searched is not deterministic. For each available FV a search
181 is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType,
182 the FileInstance instance will be the matched FFS file. For each FFS file found a search
183 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
184 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
185 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
186 It is the caller's responsibility to use FreePool() to free the allocated buffer.
187 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
188 are retrieved from an FFS file based on SectionType and SectionInstance.
190 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
191 the search will be retried with a section type of EFI_SECTION_PE32.
192 This function must be called with a TPL <= TPL_NOTIFY.
194 If Buffer is NULL, then ASSERT().
195 If Size is NULL, then ASSERT().
197 @param FileType Indicates the FV file type to search for within all
199 @param FileInstance Indicates which file instance within all available
200 FVs specified by FileType.
201 FileInstance starts from zero.
202 @param SectionType Indicates the FFS section type to search for
204 specified by FileType with FileInstance.
205 @param SectionInstance Indicates which section instance within the FFS file
206 specified by FileType with FileInstance to retrieve.
207 SectionInstance starts from zero.
208 @param Buffer On output, a pointer to a callee allocated buffer
209 containing the FFS file section that was found.
210 Is it the caller's responsibility to free this
211 buffer using FreePool().
212 @param Size On output, a pointer to the size, in bytes, of Buffer.
214 @retval EFI_SUCCESS The specified FFS section was returned.
215 @retval EFI_NOT_FOUND The specified FFS section could not be found.
216 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
217 the matching FFS section.
218 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
220 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because
221 the firmware volume that
222 contains the matching FFS section does not allow reads.
226 GetSectionFromAnyFvByFileType (
227 IN EFI_FV_FILETYPE FileType
,
228 IN UINTN FileInstance
,
229 IN EFI_SECTION_TYPE SectionType
,
230 IN UINTN SectionInstance
,
236 EFI_HANDLE
*HandleBuffer
;
242 EFI_FV_FILE_ATTRIBUTES Attributes
;
243 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
246 // Locate all available FVs.
249 Status
= gBS
->LocateHandleBuffer (
251 &gEfiFirmwareVolume2ProtocolGuid
,
256 if (EFI_ERROR (Status
)) {
261 // Go through FVs one by one to find the required section data.
263 for (IndexFv
= 0; IndexFv
< HandleCount
; IndexFv
++) {
264 Status
= gBS
->HandleProtocol (
265 HandleBuffer
[IndexFv
],
266 &gEfiFirmwareVolume2ProtocolGuid
,
269 if (EFI_ERROR (Status
)) {
274 // Use Firmware Volume 2 Protocol to search for a file of type FileType in all FVs.
276 IndexFile
= FileInstance
+ 1;
279 Status
= Fv
->GetNextFile (Fv
, &Key
, &FileType
, &NameGuid
, &Attributes
, Size
);
280 if (EFI_ERROR (Status
)) {
284 } while (IndexFile
> 0);
287 // Fv File with the required FV file type is found.
288 // Search the section file in the found FV file.
290 if (IndexFile
== 0) {
291 Status
= InternalGetSectionFromFv (
292 HandleBuffer
[IndexFv
],
300 if (!EFI_ERROR (Status
)) {
307 // The required FFS section file is not found.
309 if (IndexFv
== HandleCount
) {
310 Status
= EFI_NOT_FOUND
;
314 if (HandleBuffer
!= NULL
) {
315 FreePool(HandleBuffer
);
322 Searches all the availables firmware volumes and returns the first matching FFS section.
324 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
325 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search
326 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
327 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
328 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
329 It is the caller's responsibility to use FreePool() to free the allocated buffer.
330 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
331 are retrieved from an FFS file based on SectionType and SectionInstance.
333 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
334 the search will be retried with a section type of EFI_SECTION_PE32.
335 This function must be called with a TPL <= TPL_NOTIFY.
337 If NameGuid is NULL, then ASSERT().
338 If Buffer is NULL, then ASSERT().
339 If Size is NULL, then ASSERT().
342 @param NameGuid A pointer to to the FFS filename GUID to search for
343 within any of the firmware volumes in the platform.
344 @param SectionType Indicates the FFS section type to search for within
345 the FFS file specified by NameGuid.
346 @param SectionInstance Indicates which section instance within the FFS file
347 specified by NameGuid to retrieve.
348 @param Buffer On output, a pointer to a callee allocated buffer
349 containing the FFS file section that was found.
350 Is it the caller's responsibility to free this buffer
352 @param Size On output, a pointer to the size, in bytes, of Buffer.
354 @retval EFI_SUCCESS The specified FFS section was returned.
355 @retval EFI_NOT_FOUND The specified FFS section could not be found.
356 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
357 retrieve the matching FFS section.
358 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
360 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
362 contains the matching FFS section does not allow reads.
366 GetSectionFromAnyFv (
367 IN CONST EFI_GUID
*NameGuid
,
368 IN EFI_SECTION_TYPE SectionType
,
369 IN UINTN SectionInstance
,
375 EFI_HANDLE
*HandleBuffer
;
381 // Search the FV that contain the caller's FFS first.
382 // FV builder can choose to build FFS into the this FV
383 // so that this implementation of GetSectionFromAnyFv
384 // will locate the FFS faster.
386 FvHandle
= InternalImageHandleToFvHandle (gImageHandle
);
387 Status
= InternalGetSectionFromFv (
395 if (!EFI_ERROR (Status
)) {
400 Status
= gBS
->LocateHandleBuffer (
402 &gEfiFirmwareVolume2ProtocolGuid
,
407 if (EFI_ERROR (Status
)) {
411 for (Index
= 0; Index
< HandleCount
; Index
++) {
413 // Skip the FV that contain the caller's FFS
415 if (HandleBuffer
[Index
] != FvHandle
) {
416 Status
= InternalGetSectionFromFv (
425 if (!EFI_ERROR (Status
)) {
432 if (Index
== HandleCount
) {
433 Status
= EFI_NOT_FOUND
;
438 if (HandleBuffer
!= NULL
) {
439 FreePool(HandleBuffer
);
446 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section.
448 This function searches the firmware volume that the currently executing module was loaded
449 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search
450 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance
451 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
452 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
453 It is the caller's responsibility to use FreePool() to free the allocated buffer.
454 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from
455 an FFS file based on SectionType and SectionInstance.
457 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.
458 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
459 the search will be retried with a section type of EFI_SECTION_PE32.
461 This function must be called with a TPL <= TPL_NOTIFY.
462 If NameGuid is NULL, then ASSERT().
463 If Buffer is NULL, then ASSERT().
464 If Size is NULL, then ASSERT().
466 @param NameGuid A pointer to to the FFS filename GUID to search for
467 within the firmware volumes that the currently
468 executing module was loaded from.
469 @param SectionType Indicates the FFS section type to search for within
470 the FFS file specified by NameGuid.
471 @param SectionInstance Indicates which section instance within the FFS file
472 specified by NameGuid to retrieve.
473 @param Buffer On output, a pointer to a callee allocated buffer
474 containing the FFS file section that was found.
475 Is it the caller's responsibility to free this buffer
477 @param Size On output, a pointer to the size, in bytes, of Buffer.
480 @retval EFI_SUCCESS The specified FFS section was returned.
481 @retval EFI_NOT_FOUND The specified FFS section could not be found.
482 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
483 the matching FFS section.
484 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
486 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
487 firmware volume that contains the matching FFS
488 section does not allow reads.
493 IN CONST EFI_GUID
*NameGuid
,
494 IN EFI_SECTION_TYPE SectionType
,
495 IN UINTN SectionInstance
,
500 return InternalGetSectionFromFv (
501 InternalImageHandleToFvHandle(gImageHandle
),
512 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.
514 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.
515 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType,
516 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(),
517 and the size of the allocated buffer is returned in Size. It is the caller's responsibility
518 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for
519 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.
521 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.
522 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
523 the search will be retried with a section type of EFI_SECTION_PE32.
524 This function must be called with a TPL <= TPL_NOTIFY.
526 If Buffer is NULL, then ASSERT().
527 If Size is NULL, then ASSERT().
530 @param SectionType Indicates the FFS section type to search for within
531 the FFS file that the currently executing module
533 @param SectionInstance Indicates which section instance to retrieve within
534 the FFS file that the currently executing module
536 @param Buffer On output, a pointer to a callee allocated buffer
537 containing the FFS file section that was found.
538 Is it the caller's responsibility to free this buffer
540 @param Size On output, a pointer to the size, in bytes, of Buffer.
542 @retval EFI_SUCCESS The specified FFS section was returned.
543 @retval EFI_NOT_FOUND The specified FFS section could not be found.
544 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
545 the matching FFS section.
546 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
548 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
549 firmware volume that contains the matching FFS
550 section does not allow reads.
556 IN EFI_SECTION_TYPE SectionType
,
557 IN UINTN SectionInstance
,
562 return InternalGetSectionFromFv(
563 InternalImageHandleToFvHandle(gImageHandle
),
574 Get the image file buffer data and buffer size by its device path.
576 Access the file either from a firmware volume, from a file system interface,
577 or from the load file interface.
579 Allocate memory to store the found image. The caller is responsible to free memory.
581 If File is NULL, then NULL is returned.
582 If FileSize is NULL, then NULL is returned.
583 If AuthenticationStatus is NULL, then NULL is returned.
585 @param[in] BootPolicy Policy for Open Image File.If TRUE, indicates
586 that the request originates from the boot
587 manager, and that the boot manager is
588 attempting to load FilePath as a boot
589 selection. If FALSE, then FilePath must
590 match an exact file to be loaded.
591 @param[in] FilePath The pointer to the device path of the file
592 that is absracted to the file buffer.
593 @param[out] FileSize The pointer to the size of the abstracted
595 @param[out] AuthenticationStatus The pointer to a caller-allocated UINT32
596 in which the authentication status is returned.
598 @retval NULL File is NULL, or FileSize is NULL. Or, the file can't be found.
599 @retval other The abstracted file buffer. The caller is responsible to free memory.
603 GetFileBufferByFilePath (
604 IN BOOLEAN BootPolicy
,
605 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
607 OUT UINT32
*AuthenticationStatus
610 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
611 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
612 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePathNode
;
614 EFI_GUID
*FvNameGuid
;
615 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
616 EFI_SECTION_TYPE SectionType
;
618 UINTN ImageBufferSize
;
619 EFI_FV_FILETYPE Type
;
620 EFI_FV_FILE_ATTRIBUTES Attrib
;
621 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
622 EFI_FILE_HANDLE FileHandle
;
623 EFI_FILE_HANDLE LastHandle
;
624 EFI_FILE_INFO
*FileInfo
;
626 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
627 EFI_LOAD_FILE2_PROTOCOL
*LoadFile2
;
631 // Check input File device path.
633 if (FilePath
== NULL
|| FileSize
== NULL
|| AuthenticationStatus
== NULL
) {
638 // Init local variable
640 TempDevicePathNode
= NULL
;
646 *AuthenticationStatus
= 0;
649 // Copy File Device Path
651 OrigDevicePathNode
= DuplicateDevicePath (FilePath
);
652 if (OrigDevicePathNode
== NULL
) {
657 // Check whether this device path support FV2 protocol.
658 // Is so, this device path may contain a Image.
660 DevicePathNode
= OrigDevicePathNode
;
661 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
662 if (!EFI_ERROR (Status
)) {
664 // For FwVol File system there is only a single file name that is a GUID.
666 FvNameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePathNode
);
667 if (FvNameGuid
== NULL
) {
668 Status
= EFI_INVALID_PARAMETER
;
671 // Read image from the firmware file
673 Status
= gBS
->HandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&FwVol
);
674 if (!EFI_ERROR (Status
)) {
675 SectionType
= EFI_SECTION_PE32
;
677 Status
= FwVol
->ReadSection (
682 (VOID
**)&ImageBuffer
,
686 if (EFI_ERROR (Status
)) {
688 // Try a raw file, since a PE32 SECTION does not exist
690 if (ImageBuffer
!= NULL
) {
691 FreePool (ImageBuffer
);
692 *AuthenticationStatus
= 0;
695 Status
= FwVol
->ReadFile (
698 (VOID
**)&ImageBuffer
,
707 if (!EFI_ERROR (Status
)) {
713 // Attempt to access the file via a file system interface
715 DevicePathNode
= OrigDevicePathNode
;
716 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
717 if (!EFI_ERROR (Status
)) {
718 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
719 if (!EFI_ERROR (Status
)) {
721 // Open the Volume to get the File System handle
723 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
724 if (!EFI_ERROR (Status
)) {
726 // Duplicate the device path to avoid the access to unaligned device path node.
727 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
728 // nodes, It assures the fields in device path nodes are 2 byte aligned.
730 TempDevicePathNode
= DuplicateDevicePath (DevicePathNode
);
731 if (TempDevicePathNode
== NULL
) {
732 FileHandle
->Close (FileHandle
);
734 // Setting Status to an EFI_ERROR value will cause the rest of
735 // the file system support below to be skipped.
737 Status
= EFI_OUT_OF_RESOURCES
;
740 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
741 // directory information and filename can be seperate. The goal is to inch
742 // our way down each device path node and close the previous node
744 DevicePathNode
= TempDevicePathNode
;
745 while (!EFI_ERROR (Status
) && !IsDevicePathEnd (DevicePathNode
)) {
746 if (DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
||
747 DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
) {
748 Status
= EFI_UNSUPPORTED
;
752 LastHandle
= FileHandle
;
755 Status
= LastHandle
->Open (
758 ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
,
764 // Close the previous node
766 LastHandle
->Close (LastHandle
);
768 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
771 if (!EFI_ERROR (Status
)) {
773 // We have found the file. Now we need to read it. Before we can read the file we need to
774 // figure out how big the file is.
778 Status
= FileHandle
->GetInfo (
785 if (Status
== EFI_BUFFER_TOO_SMALL
) {
786 FileInfo
= AllocatePool (FileInfoSize
);
787 if (FileInfo
== NULL
) {
788 Status
= EFI_OUT_OF_RESOURCES
;
790 Status
= FileHandle
->GetInfo (
799 if (!EFI_ERROR (Status
) && (FileInfo
!= NULL
)) {
801 // Allocate space for the file
803 ImageBuffer
= AllocatePool ((UINTN
)FileInfo
->FileSize
);
804 if (ImageBuffer
== NULL
) {
805 Status
= EFI_OUT_OF_RESOURCES
;
808 // Read the file into the buffer we allocated
810 ImageBufferSize
= (UINTN
)FileInfo
->FileSize
;
811 Status
= FileHandle
->Read (FileHandle
, &ImageBufferSize
, ImageBuffer
);
816 // Close the file and Free FileInfo and TempDevicePathNode since we are done
818 if (FileInfo
!= NULL
) {
821 if (FileHandle
!= NULL
) {
822 FileHandle
->Close (FileHandle
);
824 if (TempDevicePathNode
!= NULL
) {
825 FreePool (TempDevicePathNode
);
829 if (!EFI_ERROR (Status
)) {
835 // Attempt to access the file via LoadFile2 interface
838 DevicePathNode
= OrigDevicePathNode
;
839 Status
= gBS
->LocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &DevicePathNode
, &Handle
);
840 if (!EFI_ERROR (Status
)) {
841 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFile2ProtocolGuid
, (VOID
**)&LoadFile2
);
842 if (!EFI_ERROR (Status
)) {
844 // Call LoadFile2 with the correct buffer size
848 Status
= LoadFile2
->LoadFile (
855 if (Status
== EFI_BUFFER_TOO_SMALL
) {
856 ImageBuffer
= AllocatePool (ImageBufferSize
);
857 if (ImageBuffer
== NULL
) {
858 Status
= EFI_OUT_OF_RESOURCES
;
860 Status
= LoadFile2
->LoadFile (
870 if (!EFI_ERROR (Status
)) {
877 // Attempt to access the file via LoadFile interface
879 DevicePathNode
= OrigDevicePathNode
;
880 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &DevicePathNode
, &Handle
);
881 if (!EFI_ERROR (Status
)) {
882 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
883 if (!EFI_ERROR (Status
)) {
885 // Call LoadFile with the correct buffer size
889 Status
= LoadFile
->LoadFile (
896 if (Status
== EFI_BUFFER_TOO_SMALL
) {
897 ImageBuffer
= AllocatePool (ImageBufferSize
);
898 if (ImageBuffer
== NULL
) {
899 Status
= EFI_OUT_OF_RESOURCES
;
901 Status
= LoadFile
->LoadFile (
915 if (EFI_ERROR (Status
)) {
916 if (ImageBuffer
!= NULL
) {
917 FreePool (ImageBuffer
);
922 *FileSize
= ImageBufferSize
;
925 FreePool (OrigDevicePathNode
);