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
;
127 ASSERT (NameGuid
!= NULL
);
128 ASSERT (Buffer
!= NULL
);
129 ASSERT (Size
!= NULL
);
131 ASSERT (FvHandle
!= NULL
);
133 Status
= gBS
->HandleProtocol (
135 &gEfiFirmwareVolume2ProtocolGuid
,
138 if (EFI_ERROR (Status
)) {
139 return EFI_NOT_FOUND
;
143 // Read desired section content in NameGuid file
147 Status
= Fv
->ReadSection (
154 &AuthenticationStatus
157 if (EFI_ERROR (Status
) && (SectionType
== EFI_SECTION_TE
)) {
159 // Try reading PE32 section, if the required section is TE type
163 Status
= Fv
->ReadSection (
170 &AuthenticationStatus
174 if (!EFI_ERROR (Status
)) {
176 // The found buffer by FV protocol is allocated by gBS AllocatePool() service.
177 // Copy the found buffer to the allocated buffer by AllocatePool().
178 // So, the returned buffer can be freed by FreePool().
180 TempBuffer
= AllocateCopyPool (*Size
, *Buffer
);
181 gBS
->FreePool (*Buffer
);
182 *Buffer
= TempBuffer
;
189 Searches all the available firmware volumes and returns the first matching FFS section.
191 This function searches all the firmware volumes for FFS files with FV file type specified by FileType
192 The order that the firmware volumes is searched is not deterministic. For each available FV a search
193 is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType,
194 the FileInstance instance will be the matched FFS file. For each FFS file found a search
195 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
196 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
197 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
198 It is the caller's responsibility to use FreePool() to free the allocated buffer.
199 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
200 are retrieved from an FFS file based on SectionType and SectionInstance.
202 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
203 the search will be retried with a section type of EFI_SECTION_PE32.
204 This function must be called with a TPL <= TPL_NOTIFY.
206 If Buffer is NULL, then ASSERT().
207 If Size is NULL, then ASSERT().
209 @param FileType Indicates the FV file type to search for within all
211 @param FileInstance Indicates which file instance within all available
212 FVs specified by FileType.
213 FileInstance starts from zero.
214 @param SectionType Indicates the FFS section type to search for
216 specified by FileType with FileInstance.
217 @param SectionInstance Indicates which section instance within the FFS file
218 specified by FileType with FileInstance to retrieve.
219 SectionInstance starts from zero.
220 @param Buffer On output, a pointer to a callee allocated buffer
221 containing the FFS file section that was found.
222 Is it the caller's responsibility to free this
223 buffer using FreePool().
224 @param Size On output, a pointer to the size, in bytes, of Buffer.
226 @retval EFI_SUCCESS The specified FFS section was returned.
227 @retval EFI_NOT_FOUND The specified FFS section could not be found.
228 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
229 the matching FFS section.
230 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
232 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because
233 the firmware volume that
234 contains the matching FFS section does not allow reads.
238 GetSectionFromAnyFvByFileType (
239 IN EFI_FV_FILETYPE FileType
,
240 IN UINTN FileInstance
,
241 IN EFI_SECTION_TYPE SectionType
,
242 IN UINTN SectionInstance
,
248 EFI_HANDLE
*HandleBuffer
;
254 EFI_FV_FILE_ATTRIBUTES Attributes
;
255 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
258 // Locate all available FVs.
261 Status
= gBS
->LocateHandleBuffer (
263 &gEfiFirmwareVolume2ProtocolGuid
,
268 if (EFI_ERROR (Status
)) {
273 // Go through FVs one by one to find the required section data.
275 for (IndexFv
= 0; IndexFv
< HandleCount
; IndexFv
++) {
276 Status
= gBS
->HandleProtocol (
277 HandleBuffer
[IndexFv
],
278 &gEfiFirmwareVolume2ProtocolGuid
,
281 if (EFI_ERROR (Status
)) {
286 // Use Firmware Volume 2 Protocol to search for a file of type FileType in all FVs.
288 IndexFile
= FileInstance
+ 1;
291 Status
= Fv
->GetNextFile (Fv
, &Key
, &FileType
, &NameGuid
, &Attributes
, Size
);
292 if (EFI_ERROR (Status
)) {
296 } while (IndexFile
> 0);
299 // Fv File with the required FV file type is found.
300 // Search the section file in the found FV file.
302 if (IndexFile
== 0) {
303 Status
= InternalGetSectionFromFv (
304 HandleBuffer
[IndexFv
],
312 if (!EFI_ERROR (Status
)) {
319 // The required FFS section file is not found.
321 if (IndexFv
== HandleCount
) {
322 Status
= EFI_NOT_FOUND
;
326 if (HandleBuffer
!= NULL
) {
328 // HandleBuffer is allocated by gBS AllocatePool() service.
329 // So, gBS FreePool() service is used to free HandleBuffer.
331 gBS
->FreePool (HandleBuffer
);
338 Searches all the availables firmware volumes and returns the first matching FFS section.
340 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
341 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search
342 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
343 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
344 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
345 It is the caller's responsibility to use FreePool() to free the allocated buffer.
346 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
347 are retrieved from an FFS file based on SectionType and SectionInstance.
349 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
350 the search will be retried with a section type of EFI_SECTION_PE32.
351 This function must be called with a TPL <= TPL_NOTIFY.
353 If NameGuid is NULL, then ASSERT().
354 If Buffer is NULL, then ASSERT().
355 If Size is NULL, then ASSERT().
358 @param NameGuid A pointer to to the FFS filename GUID to search for
359 within any of the firmware volumes in the platform.
360 @param SectionType Indicates the FFS section type to search for within
361 the FFS file specified by NameGuid.
362 @param SectionInstance Indicates which section instance within the FFS file
363 specified by NameGuid to retrieve.
364 @param Buffer On output, a pointer to a callee allocated buffer
365 containing the FFS file section that was found.
366 Is it the caller's responsibility to free this buffer
368 @param Size On output, a pointer to the size, in bytes, of Buffer.
370 @retval EFI_SUCCESS The specified FFS section was returned.
371 @retval EFI_NOT_FOUND The specified FFS section could not be found.
372 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
373 retrieve the matching FFS section.
374 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
376 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
378 contains the matching FFS section does not allow reads.
382 GetSectionFromAnyFv (
383 IN CONST EFI_GUID
*NameGuid
,
384 IN EFI_SECTION_TYPE SectionType
,
385 IN UINTN SectionInstance
,
391 EFI_HANDLE
*HandleBuffer
;
397 // Search the FV that contain the caller's FFS first.
398 // FV builder can choose to build FFS into the this FV
399 // so that this implementation of GetSectionFromAnyFv
400 // will locate the FFS faster.
402 FvHandle
= InternalImageHandleToFvHandle (gImageHandle
);
403 Status
= InternalGetSectionFromFv (
411 if (!EFI_ERROR (Status
)) {
416 Status
= gBS
->LocateHandleBuffer (
418 &gEfiFirmwareVolume2ProtocolGuid
,
423 if (EFI_ERROR (Status
)) {
427 for (Index
= 0; Index
< HandleCount
; Index
++) {
429 // Skip the FV that contain the caller's FFS
431 if (HandleBuffer
[Index
] != FvHandle
) {
432 Status
= InternalGetSectionFromFv (
441 if (!EFI_ERROR (Status
)) {
448 if (Index
== HandleCount
) {
449 Status
= EFI_NOT_FOUND
;
454 if (HandleBuffer
!= NULL
) {
456 // HandleBuffer is allocated by gBS AllocatePool() service.
457 // So, gBS FreePool() service is used to free HandleBuffer.
459 gBS
->FreePool (HandleBuffer
);
466 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section.
468 This function searches the firmware volume that the currently executing module was loaded
469 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search
470 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance
471 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
472 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
473 It is the caller's responsibility to use FreePool() to free the allocated buffer.
474 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from
475 an FFS file based on SectionType and SectionInstance.
477 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.
478 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
479 the search will be retried with a section type of EFI_SECTION_PE32.
481 This function must be called with a TPL <= TPL_NOTIFY.
482 If NameGuid is NULL, then ASSERT().
483 If Buffer is NULL, then ASSERT().
484 If Size is NULL, then ASSERT().
486 @param NameGuid A pointer to to the FFS filename GUID to search for
487 within the firmware volumes that the currently
488 executing module was loaded from.
489 @param SectionType Indicates the FFS section type to search for within
490 the FFS file specified by NameGuid.
491 @param SectionInstance Indicates which section instance within the FFS file
492 specified by NameGuid to retrieve.
493 @param Buffer On output, a pointer to a callee allocated buffer
494 containing the FFS file section that was found.
495 Is it the caller's responsibility to free this buffer
497 @param Size On output, a pointer to the size, in bytes, of Buffer.
500 @retval EFI_SUCCESS The specified FFS section was returned.
501 @retval EFI_NOT_FOUND The specified FFS section could not be found.
502 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
503 the matching FFS section.
504 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
506 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
507 firmware volume that contains the matching FFS
508 section does not allow reads.
513 IN CONST EFI_GUID
*NameGuid
,
514 IN EFI_SECTION_TYPE SectionType
,
515 IN UINTN SectionInstance
,
520 return InternalGetSectionFromFv (
521 InternalImageHandleToFvHandle(gImageHandle
),
532 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.
534 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.
535 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType,
536 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(),
537 and the size of the allocated buffer is returned in Size. It is the caller's responsibility
538 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for
539 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.
541 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.
542 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
543 the search will be retried with a section type of EFI_SECTION_PE32.
544 This function must be called with a TPL <= TPL_NOTIFY.
546 If Buffer is NULL, then ASSERT().
547 If Size is NULL, then ASSERT().
550 @param SectionType Indicates the FFS section type to search for within
551 the FFS file that the currently executing module
553 @param SectionInstance Indicates which section instance to retrieve within
554 the FFS file that the currently executing module
556 @param Buffer On output, a pointer to a callee allocated buffer
557 containing the FFS file section that was found.
558 Is it the caller's responsibility to free this buffer
560 @param Size On output, a pointer to the size, in bytes, of Buffer.
562 @retval EFI_SUCCESS The specified FFS section was returned.
563 @retval EFI_NOT_FOUND The specified FFS section could not be found.
564 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve
565 the matching FFS section.
566 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a
568 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the
569 firmware volume that contains the matching FFS
570 section does not allow reads.
576 IN EFI_SECTION_TYPE SectionType
,
577 IN UINTN SectionInstance
,
582 return InternalGetSectionFromFv(
583 InternalImageHandleToFvHandle(gImageHandle
),
594 Get the image file buffer data and buffer size by its device path.
596 Access the file either from a firmware volume, from a file system interface,
597 or from the load file interface.
599 Allocate memory to store the found image. The caller is responsible to free memory.
601 If File is NULL, then NULL is returned.
602 If FileSize is NULL, then NULL is returned.
603 If AuthenticationStatus is NULL, then NULL is returned.
605 @param[in] BootPolicy Policy for Open Image File.If TRUE, indicates
606 that the request originates from the boot
607 manager, and that the boot manager is
608 attempting to load FilePath as a boot
609 selection. If FALSE, then FilePath must
610 match an exact file to be loaded.
611 @param[in] FilePath The pointer to the device path of the file
612 that is absracted to the file buffer.
613 @param[out] FileSize The pointer to the size of the abstracted
615 @param[out] AuthenticationStatus The pointer to a caller-allocated UINT32
616 in which the authentication status is returned.
618 @retval NULL File is NULL, or FileSize is NULL. Or, the file can't be found.
619 @retval other The abstracted file buffer. The caller is responsible to free memory.
623 GetFileBufferByFilePath (
624 IN BOOLEAN BootPolicy
,
625 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
627 OUT UINT32
*AuthenticationStatus
630 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
631 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
632 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePathNode
;
634 EFI_GUID
*FvNameGuid
;
635 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
636 EFI_SECTION_TYPE SectionType
;
639 UINTN ImageBufferSize
;
640 EFI_FV_FILETYPE Type
;
641 EFI_FV_FILE_ATTRIBUTES Attrib
;
642 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
643 EFI_FILE_HANDLE FileHandle
;
644 EFI_FILE_HANDLE LastHandle
;
645 EFI_FILE_INFO
*FileInfo
;
647 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
648 EFI_LOAD_FILE2_PROTOCOL
*LoadFile2
;
652 // Check input File device path.
654 if (FilePath
== NULL
|| FileSize
== NULL
|| AuthenticationStatus
== NULL
) {
659 // Init local variable
661 TempDevicePathNode
= NULL
;
668 *AuthenticationStatus
= 0;
671 // Copy File Device Path
673 OrigDevicePathNode
= DuplicateDevicePath (FilePath
);
674 if (OrigDevicePathNode
== NULL
) {
679 // Check whether this device path support FV2 protocol.
680 // Is so, this device path may contain a Image.
682 DevicePathNode
= OrigDevicePathNode
;
683 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
684 if (!EFI_ERROR (Status
)) {
686 // For FwVol File system there is only a single file name that is a GUID.
688 FvNameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePathNode
);
689 if (FvNameGuid
== NULL
) {
690 Status
= EFI_INVALID_PARAMETER
;
693 // Read image from the firmware file
695 Status
= gBS
->HandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&FwVol
);
696 if (!EFI_ERROR (Status
)) {
697 SectionType
= EFI_SECTION_PE32
;
699 Status
= FwVol
->ReadSection (
704 (VOID
**)&ImageBuffer
,
708 if (EFI_ERROR (Status
)) {
710 // Try a raw file, since a PE32 SECTION does not exist
712 if (ImageBuffer
!= NULL
) {
713 FreePool (ImageBuffer
);
714 *AuthenticationStatus
= 0;
717 Status
= FwVol
->ReadFile (
720 (VOID
**)&ImageBuffer
,
727 if (!EFI_ERROR (Status
)) {
729 // The found buffer by FV protocol is allocated by gBS AllocatePool() service.
730 // Copy the found buffer to the allocated buffer by AllocatePool().
731 // Then, this returned buffer can be freed by FreePool().
733 TempBuffer
= AllocateCopyPool (ImageBufferSize
, ImageBuffer
);
734 gBS
->FreePool (ImageBuffer
);
735 ImageBuffer
= TempBuffer
;
743 // Attempt to access the file via a file system interface
745 DevicePathNode
= OrigDevicePathNode
;
746 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
747 if (!EFI_ERROR (Status
)) {
748 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
749 if (!EFI_ERROR (Status
)) {
751 // Open the Volume to get the File System handle
753 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
754 if (!EFI_ERROR (Status
)) {
756 // Duplicate the device path to avoid the access to unaligned device path node.
757 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
758 // nodes, It assures the fields in device path nodes are 2 byte aligned.
760 TempDevicePathNode
= DuplicateDevicePath (DevicePathNode
);
761 if (TempDevicePathNode
== NULL
) {
762 FileHandle
->Close (FileHandle
);
763 Status
= EFI_OUT_OF_RESOURCES
;
767 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
768 // directory information and filename can be seperate. The goal is to inch
769 // our way down each device path node and close the previous node
771 DevicePathNode
= TempDevicePathNode
;
772 while (!IsDevicePathEnd (DevicePathNode
) && !EFI_ERROR (Status
)) {
773 if (DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
||
774 DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
) {
775 Status
= EFI_UNSUPPORTED
;
779 LastHandle
= FileHandle
;
782 Status
= LastHandle
->Open (
785 ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
,
791 // Close the previous node
793 LastHandle
->Close (LastHandle
);
795 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
798 if (!EFI_ERROR (Status
)) {
800 // We have found the file. Now we need to read it. Before we can read the file we need to
801 // figure out how big the file is.
805 Status
= FileHandle
->GetInfo (
812 if (Status
== EFI_BUFFER_TOO_SMALL
) {
813 FileInfo
= AllocatePool (FileInfoSize
);
814 if (FileInfo
== NULL
) {
815 Status
= EFI_OUT_OF_RESOURCES
;
817 Status
= FileHandle
->GetInfo (
826 if (!EFI_ERROR (Status
) && (FileInfo
!= NULL
)) {
828 // Allocate space for the file
830 ImageBuffer
= AllocatePool ((UINTN
)FileInfo
->FileSize
);
831 if (ImageBuffer
== NULL
) {
832 Status
= EFI_OUT_OF_RESOURCES
;
835 // Read the file into the buffer we allocated
837 ImageBufferSize
= (UINTN
)FileInfo
->FileSize
;
838 Status
= FileHandle
->Read (FileHandle
, &ImageBufferSize
, ImageBuffer
);
843 // Close the file and Free FileInfo and TempDevicePathNode since we are done
845 if (FileInfo
!= NULL
) {
848 if (FileHandle
!= NULL
) {
849 FileHandle
->Close (FileHandle
);
851 FreePool (TempDevicePathNode
);
858 // Attempt to access the file via LoadFile2 interface
861 DevicePathNode
= OrigDevicePathNode
;
862 Status
= gBS
->LocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &DevicePathNode
, &Handle
);
863 if (!EFI_ERROR (Status
)) {
864 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFile2ProtocolGuid
, (VOID
**)&LoadFile2
);
865 if (!EFI_ERROR (Status
)) {
867 // Call LoadFile2 with the correct buffer size
871 Status
= LoadFile2
->LoadFile (
878 if (Status
== EFI_BUFFER_TOO_SMALL
) {
879 ImageBuffer
= AllocatePool (ImageBufferSize
);
880 if (ImageBuffer
== NULL
) {
881 Status
= EFI_OUT_OF_RESOURCES
;
883 Status
= LoadFile2
->LoadFile (
898 // Attempt to access the file via LoadFile interface
900 DevicePathNode
= OrigDevicePathNode
;
901 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &DevicePathNode
, &Handle
);
902 if (!EFI_ERROR (Status
)) {
903 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
904 if (!EFI_ERROR (Status
)) {
906 // Call LoadFile with the correct buffer size
910 Status
= LoadFile
->LoadFile (
917 if (Status
== EFI_BUFFER_TOO_SMALL
) {
918 ImageBuffer
= AllocatePool (ImageBufferSize
);
919 if (ImageBuffer
== NULL
) {
920 Status
= EFI_OUT_OF_RESOURCES
;
922 Status
= LoadFile
->LoadFile (
936 if (EFI_ERROR (Status
)) {
937 if (ImageBuffer
!= NULL
) {
938 FreePool (ImageBuffer
);
943 *FileSize
= ImageBufferSize
;
946 FreePool (OrigDevicePathNode
);