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 - 2012, 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 FilePath 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 Pointer to the authentication status.
597 @retval NULL FilePath is NULL, or FileSize is NULL, or AuthenticationStatus is NULL, or the file can't be found.
598 @retval other The abstracted file buffer. The caller is responsible to free memory.
602 GetFileBufferByFilePath (
603 IN BOOLEAN BootPolicy
,
604 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
606 OUT UINT32
*AuthenticationStatus
609 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
610 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
611 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePathNode
;
613 EFI_GUID
*FvNameGuid
;
614 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
615 EFI_SECTION_TYPE SectionType
;
617 UINTN ImageBufferSize
;
618 EFI_FV_FILETYPE Type
;
619 EFI_FV_FILE_ATTRIBUTES Attrib
;
620 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
621 EFI_FILE_HANDLE FileHandle
;
622 EFI_FILE_HANDLE LastHandle
;
623 EFI_FILE_INFO
*FileInfo
;
625 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
626 EFI_LOAD_FILE2_PROTOCOL
*LoadFile2
;
630 // Check input File device path.
632 if (FilePath
== NULL
|| FileSize
== NULL
|| AuthenticationStatus
== NULL
) {
637 // Init local variable
639 TempDevicePathNode
= NULL
;
645 *AuthenticationStatus
= 0;
648 // Copy File Device Path
650 OrigDevicePathNode
= DuplicateDevicePath (FilePath
);
651 if (OrigDevicePathNode
== NULL
) {
656 // Check whether this device path support FV2 protocol.
657 // Is so, this device path may contain a Image.
659 DevicePathNode
= OrigDevicePathNode
;
660 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
661 if (!EFI_ERROR (Status
)) {
663 // For FwVol File system there is only a single file name that is a GUID.
665 FvNameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePathNode
);
666 if (FvNameGuid
== NULL
) {
667 Status
= EFI_INVALID_PARAMETER
;
670 // Read image from the firmware file
672 Status
= gBS
->HandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&FwVol
);
673 if (!EFI_ERROR (Status
)) {
674 SectionType
= EFI_SECTION_PE32
;
676 Status
= FwVol
->ReadSection (
681 (VOID
**)&ImageBuffer
,
685 if (EFI_ERROR (Status
)) {
687 // Try a raw file, since a PE32 SECTION does not exist
689 if (ImageBuffer
!= NULL
) {
690 FreePool (ImageBuffer
);
691 *AuthenticationStatus
= 0;
694 Status
= FwVol
->ReadFile (
697 (VOID
**)&ImageBuffer
,
706 if (!EFI_ERROR (Status
)) {
712 // Attempt to access the file via a file system interface
714 DevicePathNode
= OrigDevicePathNode
;
715 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
716 if (!EFI_ERROR (Status
)) {
717 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
718 if (!EFI_ERROR (Status
)) {
720 // Open the Volume to get the File System handle
722 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
723 if (!EFI_ERROR (Status
)) {
725 // Duplicate the device path to avoid the access to unaligned device path node.
726 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
727 // nodes, It assures the fields in device path nodes are 2 byte aligned.
729 TempDevicePathNode
= DuplicateDevicePath (DevicePathNode
);
730 if (TempDevicePathNode
== NULL
) {
731 FileHandle
->Close (FileHandle
);
733 // Setting Status to an EFI_ERROR value will cause the rest of
734 // the file system support below to be skipped.
736 Status
= EFI_OUT_OF_RESOURCES
;
739 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
740 // directory information and filename can be seperate. The goal is to inch
741 // our way down each device path node and close the previous node
743 DevicePathNode
= TempDevicePathNode
;
744 while (!EFI_ERROR (Status
) && !IsDevicePathEnd (DevicePathNode
)) {
745 if (DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
||
746 DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
) {
747 Status
= EFI_UNSUPPORTED
;
751 LastHandle
= FileHandle
;
754 Status
= LastHandle
->Open (
757 ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
,
763 // Close the previous node
765 LastHandle
->Close (LastHandle
);
767 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
770 if (!EFI_ERROR (Status
)) {
772 // We have found the file. Now we need to read it. Before we can read the file we need to
773 // figure out how big the file is.
777 Status
= FileHandle
->GetInfo (
784 if (Status
== EFI_BUFFER_TOO_SMALL
) {
785 FileInfo
= AllocatePool (FileInfoSize
);
786 if (FileInfo
== NULL
) {
787 Status
= EFI_OUT_OF_RESOURCES
;
789 Status
= FileHandle
->GetInfo (
798 if (!EFI_ERROR (Status
) && (FileInfo
!= NULL
)) {
800 // Allocate space for the file
802 ImageBuffer
= AllocatePool ((UINTN
)FileInfo
->FileSize
);
803 if (ImageBuffer
== NULL
) {
804 Status
= EFI_OUT_OF_RESOURCES
;
807 // Read the file into the buffer we allocated
809 ImageBufferSize
= (UINTN
)FileInfo
->FileSize
;
810 Status
= FileHandle
->Read (FileHandle
, &ImageBufferSize
, ImageBuffer
);
815 // Close the file and Free FileInfo and TempDevicePathNode since we are done
817 if (FileInfo
!= NULL
) {
820 if (FileHandle
!= NULL
) {
821 FileHandle
->Close (FileHandle
);
823 if (TempDevicePathNode
!= NULL
) {
824 FreePool (TempDevicePathNode
);
828 if (!EFI_ERROR (Status
)) {
834 // Attempt to access the file via LoadFile2 interface
837 DevicePathNode
= OrigDevicePathNode
;
838 Status
= gBS
->LocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &DevicePathNode
, &Handle
);
839 if (!EFI_ERROR (Status
)) {
840 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFile2ProtocolGuid
, (VOID
**)&LoadFile2
);
841 if (!EFI_ERROR (Status
)) {
843 // Call LoadFile2 with the correct buffer size
847 Status
= LoadFile2
->LoadFile (
854 if (Status
== EFI_BUFFER_TOO_SMALL
) {
855 ImageBuffer
= AllocatePool (ImageBufferSize
);
856 if (ImageBuffer
== NULL
) {
857 Status
= EFI_OUT_OF_RESOURCES
;
859 Status
= LoadFile2
->LoadFile (
869 if (!EFI_ERROR (Status
)) {
876 // Attempt to access the file via LoadFile interface
878 DevicePathNode
= OrigDevicePathNode
;
879 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &DevicePathNode
, &Handle
);
880 if (!EFI_ERROR (Status
)) {
881 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
882 if (!EFI_ERROR (Status
)) {
884 // Call LoadFile with the correct buffer size
888 Status
= LoadFile
->LoadFile (
895 if (Status
== EFI_BUFFER_TOO_SMALL
) {
896 ImageBuffer
= AllocatePool (ImageBufferSize
);
897 if (ImageBuffer
== NULL
) {
898 Status
= EFI_OUT_OF_RESOURCES
;
900 Status
= LoadFile
->LoadFile (
914 if (EFI_ERROR (Status
)) {
915 if (ImageBuffer
!= NULL
) {
916 FreePool (ImageBuffer
);
921 *FileSize
= ImageBufferSize
;
924 FreePool (OrigDevicePathNode
);