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 EFI_FIRMWARE_VOLUME2_PROTOCOL instance.
92 @param NameGuid The GUID name of a Firmware File.
93 @param SectionType The Firmware Section type.
94 @param SectionInstance The instance number of Firmware Section to read from starting from 0.
95 @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.
96 @param Size On output, the size of Buffer.
98 @retval EFI_SUCCESS The image is found and data and size is returned.
99 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.
100 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.
101 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.
102 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.
106 InternalGetSectionFromFv (
107 IN EFI_HANDLE FvHandle
,
108 IN CONST EFI_GUID
*NameGuid
,
109 IN EFI_SECTION_TYPE SectionType
,
110 IN UINTN SectionInstance
,
116 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
117 UINT32 AuthenticationStatus
;
119 ASSERT (NameGuid
!= NULL
);
120 ASSERT (Buffer
!= NULL
);
121 ASSERT (Size
!= NULL
);
123 ASSERT (FvHandle
!= NULL
);
125 Status
= gBS
->HandleProtocol (
127 &gEfiFirmwareVolume2ProtocolGuid
,
130 if (EFI_ERROR (Status
)) {
131 return EFI_NOT_FOUND
;
135 // Read desired section content in NameGuid file
139 Status
= Fv
->ReadSection (
146 &AuthenticationStatus
149 if (EFI_ERROR (Status
) && (SectionType
== EFI_SECTION_TE
)) {
151 // Try reading PE32 section, if the required section is TE type
155 Status
= Fv
->ReadSection (
162 &AuthenticationStatus
170 Searches all the available firmware volumes and returns the first matching FFS section.
172 This function searches all the firmware volumes for FFS files with FV file type specified by FileType
173 The order that the firmware volumes is searched is not deterministic. For each available FV a search
174 is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType,
175 the FileInstance instance will be the matched FFS file. For each FFS file found a search
176 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
177 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
178 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
179 It is the caller's responsibility to use FreePool() to free the allocated buffer.
180 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
181 are retrieved from an FFS file based on SectionType and SectionInstance.
183 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
184 the search will be retried with a section type of EFI_SECTION_PE32.
185 This function must be called with a TPL <= TPL_NOTIFY.
187 If Buffer is NULL, then ASSERT().
188 If Size is NULL, then ASSERT().
190 @param FileType Indicates the FV file type to search for within all available FVs.
191 @param FileInstance Indicates which file instance within all available FVs specified by FileType.
192 FileInstance starts from zero.
193 @param SectionType Indicates the FFS section type to search for within the FFS file
194 specified by FileType with FileInstance.
195 @param SectionInstance Indicates which section instance within the FFS file
196 specified by FileType with FileInstance to retrieve. SectionInstance starts from zero.
197 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
198 Is it the caller's responsibility to free this buffer using FreePool().
199 @param Size On output, a pointer to the size, in bytes, of Buffer.
201 @retval EFI_SUCCESS The specified FFS section was returned.
202 @retval EFI_NOT_FOUND The specified FFS section could not be found.
203 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
204 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
205 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
206 contains the matching FFS section does not allow reads.
210 GetSectionFromAnyFvByFileType (
211 IN EFI_FV_FILETYPE FileType
,
212 IN UINTN FileInstance
,
213 IN EFI_SECTION_TYPE SectionType
,
214 IN UINTN SectionInstance
,
220 EFI_HANDLE
*HandleBuffer
;
226 EFI_FV_FILE_ATTRIBUTES Attributes
;
227 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
230 // Locate all available FVs.
233 Status
= gBS
->LocateHandleBuffer (
235 &gEfiFirmwareVolume2ProtocolGuid
,
240 if (EFI_ERROR (Status
)) {
245 // Go through FVs one by one to find the required section data.
247 for (IndexFv
= 0; IndexFv
< HandleCount
; IndexFv
++) {
248 Status
= gBS
->HandleProtocol (
249 HandleBuffer
[IndexFv
],
250 &gEfiFirmwareVolume2ProtocolGuid
,
253 if (EFI_ERROR (Status
)) {
258 // Use Firmware Volume 2 Protocol to search for a file of type FileType in all FVs.
260 IndexFile
= FileInstance
+ 1;
263 Status
= Fv
->GetNextFile (Fv
, &Key
, &FileType
, &NameGuid
, &Attributes
, Size
);
264 if (EFI_ERROR (Status
)) {
268 } while (IndexFile
> 0);
271 // Fv File with the required FV file type is found.
272 // Search the section file in the found FV file.
274 if (IndexFile
== 0) {
275 Status
= InternalGetSectionFromFv (
276 HandleBuffer
[IndexFv
],
284 if (!EFI_ERROR (Status
)) {
291 // The required FFS section file is not found.
293 if (IndexFv
== HandleCount
) {
294 Status
= EFI_NOT_FOUND
;
298 if (HandleBuffer
!= NULL
) {
299 FreePool(HandleBuffer
);
306 Searches all the availables firmware volumes and returns the first matching FFS section.
308 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
309 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search
310 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
311 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
312 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
313 It is the caller's responsibility to use FreePool() to free the allocated buffer.
314 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
315 are retrieved from an FFS file based on SectionType and SectionInstance.
317 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
318 the search will be retried with a section type of EFI_SECTION_PE32.
319 This function must be called with a TPL <= TPL_NOTIFY.
321 If NameGuid is NULL, then ASSERT().
322 If Buffer is NULL, then ASSERT().
323 If Size is NULL, then ASSERT().
326 @param NameGuid A pointer to to the FFS filename GUID to search for within
327 any of the firmware volumes in the platform.
328 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.
329 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.
330 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
331 Is it the caller's responsibility to free this buffer using FreePool().
332 @param Size On output, a pointer to the size, in bytes, of Buffer.
334 @retval EFI_SUCCESS The specified FFS section was returned.
335 @retval EFI_NOT_FOUND The specified FFS section could not be found.
336 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
337 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
338 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
339 contains the matching FFS section does not allow reads.
343 GetSectionFromAnyFv (
344 IN CONST EFI_GUID
*NameGuid
,
345 IN EFI_SECTION_TYPE SectionType
,
346 IN UINTN SectionInstance
,
352 EFI_HANDLE
*HandleBuffer
;
358 // Search the FV that contain the caller's FFS first.
359 // FV builder can choose to build FFS into the this FV
360 // so that this implementation of GetSectionFromAnyFv
361 // will locate the FFS faster.
363 FvHandle
= InternalImageHandleToFvHandle (gImageHandle
);
364 Status
= InternalGetSectionFromFv (
372 if (!EFI_ERROR (Status
)) {
377 Status
= gBS
->LocateHandleBuffer (
379 &gEfiFirmwareVolume2ProtocolGuid
,
384 if (EFI_ERROR (Status
)) {
388 for (Index
= 0; Index
< HandleCount
; Index
++) {
390 // Skip the FV that contain the caller's FFS
392 if (HandleBuffer
[Index
] != FvHandle
) {
393 Status
= InternalGetSectionFromFv (
402 if (!EFI_ERROR (Status
)) {
409 if (Index
== HandleCount
) {
410 Status
= EFI_NOT_FOUND
;
415 if (HandleBuffer
!= NULL
) {
416 FreePool(HandleBuffer
);
423 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section.
425 This function searches the firmware volume that the currently executing module was loaded
426 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search
427 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance
428 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
429 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
430 It is the caller's responsibility to use FreePool() to free the allocated buffer.
431 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from
432 an FFS file based on SectionType and SectionInstance.
434 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.
435 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
436 the search will be retried with a section type of EFI_SECTION_PE32.
438 This function must be called with a TPL <= TPL_NOTIFY.
439 If NameGuid is NULL, then ASSERT().
440 If Buffer is NULL, then ASSERT().
441 If Size is NULL, then ASSERT().
443 @param NameGuid A pointer to to the FFS filename GUID to search for within
444 the firmware volumes that the currently executing module was loaded from.
445 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.
446 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.
447 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
448 Is it the caller's responsibility to free this buffer using FreePool().
449 @param Size On output, a pointer to the size, in bytes, of Buffer.
452 @retval EFI_SUCCESS The specified FFS section was returned.
453 @retval EFI_NOT_FOUND The specified FFS section could not be found.
454 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
455 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
456 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
457 contains the matching FFS section does not allow reads.
462 IN CONST EFI_GUID
*NameGuid
,
463 IN EFI_SECTION_TYPE SectionType
,
464 IN UINTN SectionInstance
,
469 return InternalGetSectionFromFv (
470 InternalImageHandleToFvHandle(gImageHandle
),
481 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.
483 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.
484 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType,
485 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(),
486 and the size of the allocated buffer is returned in Size. It is the caller's responsibility
487 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for
488 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.
490 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.
491 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
492 the search will be retried with a section type of EFI_SECTION_PE32.
493 This function must be called with a TPL <= TPL_NOTIFY.
495 If Buffer is NULL, then ASSERT().
496 If Size is NULL, then ASSERT().
499 @param SectionType Indicates the FFS section type to search for within the FFS file
500 that the currently executing module was loaded from.
501 @param SectionInstance Indicates which section instance to retrieve within the FFS file
502 that the currently executing module was loaded from.
503 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
504 Is it the caller's responsibility to free this buffer using FreePool().
505 @param Size On output, a pointer to the size, in bytes, of Buffer.
507 @retval EFI_SUCCESS The specified FFS section was returned.
508 @retval EFI_NOT_FOUND The specified FFS section could not be found.
509 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
510 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
511 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
512 contains the matching FFS section does not allow reads.
518 IN EFI_SECTION_TYPE SectionType
,
519 IN UINTN SectionInstance
,
524 return InternalGetSectionFromFv(
525 InternalImageHandleToFvHandle(gImageHandle
),
536 Get the image file buffer data and buffer size by its device path.
538 Access the file either from a firmware volume, from a file system interface,
539 or from the load file interface.
541 Allocate memory to store the found image. The caller is responsible to free memory.
543 If File is NULL, then NULL is returned.
544 If FileSize is NULL, then NULL is returned.
545 If AuthenticationStatus is NULL, then NULL is returned.
547 @param[in] BootPolicy Policy for Open Image File.If TRUE, indicates that the request
548 originates from the boot manager, and that the boot manager is
549 attempting to load FilePath as a boot selection. If FALSE,
550 then FilePath must match an exact file to be loaded.
551 @param[in] FilePath The pointer to the device path of the file that is absracted to
553 @param[out] FileSize The pointer to the size of the abstracted file buffer.
554 @param[out] AuthenticationStatus The pointer to a caller-allocated UINT32 in which the authentication
557 @retval NULL File is NULL, or FileSize is NULL. Or the file can't be found.
558 @retval other The abstracted file buffer. The caller is responsible to free memory.
562 GetFileBufferByFilePath (
563 IN BOOLEAN BootPolicy
,
564 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
566 OUT UINT32
*AuthenticationStatus
569 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
570 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
571 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePathNode
;
573 EFI_GUID
*FvNameGuid
;
574 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
575 EFI_SECTION_TYPE SectionType
;
577 UINTN ImageBufferSize
;
578 EFI_FV_FILETYPE Type
;
579 EFI_FV_FILE_ATTRIBUTES Attrib
;
580 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
581 EFI_FILE_HANDLE FileHandle
;
582 EFI_FILE_HANDLE LastHandle
;
583 EFI_FILE_INFO
*FileInfo
;
585 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
586 EFI_LOAD_FILE2_PROTOCOL
*LoadFile2
;
590 // Check input File device path.
592 if (FilePath
== NULL
|| FileSize
== NULL
|| AuthenticationStatus
== NULL
) {
597 // Init local variable
599 TempDevicePathNode
= NULL
;
605 *AuthenticationStatus
= 0;
608 // Copy File Device Path
610 OrigDevicePathNode
= DuplicateDevicePath (FilePath
);
611 if (OrigDevicePathNode
== NULL
) {
616 // Check whether this device path support FV2 protocol.
617 // Is so, this device path may contain a Image.
619 DevicePathNode
= OrigDevicePathNode
;
620 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
621 if (!EFI_ERROR (Status
)) {
623 // For FwVol File system there is only a single file name that is a GUID.
625 FvNameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePathNode
);
626 if (FvNameGuid
== NULL
) {
627 Status
= EFI_INVALID_PARAMETER
;
630 // Read image from the firmware file
632 Status
= gBS
->HandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&FwVol
);
633 if (!EFI_ERROR (Status
)) {
634 SectionType
= EFI_SECTION_PE32
;
636 Status
= FwVol
->ReadSection (
641 (VOID
**)&ImageBuffer
,
645 if (EFI_ERROR (Status
)) {
647 // Try a raw file, since a PE32 SECTION does not exist
649 if (ImageBuffer
!= NULL
) {
650 FreePool (ImageBuffer
);
651 *AuthenticationStatus
= 0;
654 Status
= FwVol
->ReadFile (
657 (VOID
**)&ImageBuffer
,
670 // Attempt to access the file via a file system interface
672 DevicePathNode
= OrigDevicePathNode
;
673 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
674 if (!EFI_ERROR (Status
)) {
675 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
676 if (!EFI_ERROR (Status
)) {
678 // Open the Volume to get the File System handle
680 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
681 if (!EFI_ERROR (Status
)) {
683 // Duplicate the device path to avoid the access to unaligned device path node.
684 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
685 // nodes, It assures the fields in device path nodes are 2 byte aligned.
687 TempDevicePathNode
= DuplicateDevicePath (DevicePathNode
);
688 if (TempDevicePathNode
== NULL
) {
689 FileHandle
->Close (FileHandle
);
690 Status
= EFI_OUT_OF_RESOURCES
;
694 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
695 // directory information and filename can be seperate. The goal is to inch
696 // our way down each device path node and close the previous node
698 DevicePathNode
= TempDevicePathNode
;
699 while (!IsDevicePathEnd (DevicePathNode
) && !EFI_ERROR (Status
)) {
700 if (DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
||
701 DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
) {
702 Status
= EFI_UNSUPPORTED
;
706 LastHandle
= FileHandle
;
709 Status
= LastHandle
->Open (
712 ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
,
718 // Close the previous node
720 LastHandle
->Close (LastHandle
);
722 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
725 if (!EFI_ERROR (Status
)) {
727 // We have found the file. Now we need to read it. Before we can read the file we need to
728 // figure out how big the file is.
732 Status
= FileHandle
->GetInfo (
739 if (Status
== EFI_BUFFER_TOO_SMALL
) {
740 FileInfo
= AllocatePool (FileInfoSize
);
741 if (FileInfo
== NULL
) {
742 Status
= EFI_OUT_OF_RESOURCES
;
744 Status
= FileHandle
->GetInfo (
753 if (!EFI_ERROR (Status
) && (FileInfo
!= NULL
)) {
755 // Allocate space for the file
757 ImageBuffer
= AllocatePool ((UINTN
)FileInfo
->FileSize
);
758 if (ImageBuffer
== NULL
) {
759 Status
= EFI_OUT_OF_RESOURCES
;
762 // Read the file into the buffer we allocated
764 ImageBufferSize
= (UINTN
)FileInfo
->FileSize
;
765 Status
= FileHandle
->Read (FileHandle
, &ImageBufferSize
, ImageBuffer
);
770 // Close the file and Free FileInfo and TempDevicePathNode since we are done
772 if (FileInfo
!= NULL
) {
775 if (FileHandle
!= NULL
) {
776 FileHandle
->Close (FileHandle
);
778 FreePool (TempDevicePathNode
);
785 // Attempt to access the file via LoadFile2 interface
788 DevicePathNode
= OrigDevicePathNode
;
789 Status
= gBS
->LocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &DevicePathNode
, &Handle
);
790 if (!EFI_ERROR (Status
)) {
791 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFile2ProtocolGuid
, (VOID
**)&LoadFile2
);
792 if (!EFI_ERROR (Status
)) {
794 // Call LoadFile2 with the correct buffer size
798 Status
= LoadFile2
->LoadFile (
805 if (Status
== EFI_BUFFER_TOO_SMALL
) {
806 ImageBuffer
= AllocatePool (ImageBufferSize
);
807 if (ImageBuffer
== NULL
) {
808 Status
= EFI_OUT_OF_RESOURCES
;
810 Status
= LoadFile2
->LoadFile (
825 // Attempt to access the file via LoadFile interface
827 DevicePathNode
= OrigDevicePathNode
;
828 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &DevicePathNode
, &Handle
);
829 if (!EFI_ERROR (Status
)) {
830 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
831 if (!EFI_ERROR (Status
)) {
833 // Call LoadFile with the correct buffer size
837 Status
= LoadFile
->LoadFile (
844 if (Status
== EFI_BUFFER_TOO_SMALL
) {
845 ImageBuffer
= AllocatePool (ImageBufferSize
);
846 if (ImageBuffer
== NULL
) {
847 Status
= EFI_OUT_OF_RESOURCES
;
849 Status
= LoadFile
->LoadFile (
863 if (EFI_ERROR (Status
)) {
864 if (ImageBuffer
!= NULL
) {
865 FreePool (ImageBuffer
);
870 *FileSize
= ImageBufferSize
;
873 FreePool (OrigDevicePathNode
);