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 - 2009, Intel Corporation<BR>
6 All rights reserved. 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
172 Searches all the availables firmware volumes and returns the first matching FFS section.
174 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
175 The order that the firmware volumes is searched is not deterministic. 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 NameGuid is NULL, then ASSERT().
188 If Buffer is NULL, then ASSERT().
189 If Size is NULL, then ASSERT().
192 @param NameGuid A pointer to to the FFS filename GUID to search for within
193 any of the firmware volumes in the platform.
194 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.
195 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.
196 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
197 Is it the caller's responsibility to free this buffer using FreePool().
198 @param Size On output, a pointer to the size, in bytes, of Buffer.
200 @retval EFI_SUCCESS The specified FFS section was returned.
201 @retval EFI_NOT_FOUND The specified FFS section could not be found.
202 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
203 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
204 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
205 contains the matching FFS section does not allow reads.
209 GetSectionFromAnyFv (
210 IN CONST EFI_GUID
*NameGuid
,
211 IN EFI_SECTION_TYPE SectionType
,
212 IN UINTN SectionInstance
,
218 EFI_HANDLE
*HandleBuffer
;
224 // Search the FV that contain the caller's FFS first.
225 // FV builder can choose to build FFS into the this FV
226 // so that this implementation of GetSectionFromAnyFv
227 // will locate the FFS faster.
229 FvHandle
= InternalImageHandleToFvHandle (gImageHandle
);
230 Status
= InternalGetSectionFromFv (
238 if (!EFI_ERROR (Status
)) {
243 Status
= gBS
->LocateHandleBuffer (
245 &gEfiFirmwareVolume2ProtocolGuid
,
250 if (EFI_ERROR (Status
)) {
254 for (Index
= 0; Index
< HandleCount
; Index
++) {
256 // Skip the FV that contain the caller's FFS
258 if (HandleBuffer
[Index
] != FvHandle
) {
259 Status
= InternalGetSectionFromFv (
268 if (!EFI_ERROR (Status
)) {
275 if (Index
== HandleCount
) {
276 Status
= EFI_NOT_FOUND
;
281 if (HandleBuffer
!= NULL
) {
282 FreePool(HandleBuffer
);
289 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section.
291 This function searches the firmware volume that the currently executing module was loaded
292 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search
293 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance
294 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
295 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
296 It is the caller's responsibility to use FreePool() to free the allocated buffer.
297 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from
298 an FFS file based on SectionType and SectionInstance.
300 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.
301 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
302 the search will be retried with a section type of EFI_SECTION_PE32.
304 This function must be called with a TPL <= TPL_NOTIFY.
305 If NameGuid is NULL, then ASSERT().
306 If Buffer is NULL, then ASSERT().
307 If Size is NULL, then ASSERT().
309 @param NameGuid A pointer to to the FFS filename GUID to search for within
310 the firmware volumes that the currently executing module was loaded from.
311 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.
312 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.
313 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
314 Is it the caller's responsibility to free this buffer using FreePool().
315 @param Size On output, a pointer to the size, in bytes, of Buffer.
318 @retval EFI_SUCCESS The specified FFS section was returned.
319 @retval EFI_NOT_FOUND The specified FFS section could not be found.
320 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
321 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
322 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
323 contains the matching FFS section does not allow reads.
328 IN CONST EFI_GUID
*NameGuid
,
329 IN EFI_SECTION_TYPE SectionType
,
330 IN UINTN SectionInstance
,
335 return InternalGetSectionFromFv (
336 InternalImageHandleToFvHandle(gImageHandle
),
347 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.
349 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.
350 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType,
351 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(),
352 and the size of the allocated buffer is returned in Size. It is the caller's responsibility
353 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for
354 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.
356 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.
357 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
358 the search will be retried with a section type of EFI_SECTION_PE32.
359 This function must be called with a TPL <= TPL_NOTIFY.
361 If Buffer is NULL, then ASSERT().
362 If Size is NULL, then ASSERT().
365 @param SectionType Indicates the FFS section type to search for within the FFS file
366 that the currently executing module was loaded from.
367 @param SectionInstance Indicates which section instance to retrieve within the FFS file
368 that the currently executing module was loaded from.
369 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
370 Is it the caller's responsibility to free this buffer using FreePool().
371 @param Size On output, a pointer to the size, in bytes, of Buffer.
373 @retval EFI_SUCCESS The specified FFS section was returned.
374 @retval EFI_NOT_FOUND The specified FFS section could not be found.
375 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
376 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
377 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
378 contains the matching FFS section does not allow reads.
384 IN EFI_SECTION_TYPE SectionType
,
385 IN UINTN SectionInstance
,
390 return InternalGetSectionFromFv(
391 InternalImageHandleToFvHandle(gImageHandle
),
402 Get the image file buffer data and buffer size by its device path.
404 Access the file either from a a firmware volume, from a file system interface,
405 or from the load file interface.
407 Allocate memory to store the found image. The caller is responsible to free memory.
409 If File is NULL, then NULL is returned.
410 If FileSize is NULL, then NULL is returned.
411 If AuthenticationStatus is NULL, then NULL is returned.
413 @param[in] BootPolicy
414 Policy for Open Image File.If TRUE, indicates that the request
415 originates from the boot manager, and that the boot manager is
416 attempting to load FilePath as a boot selection. If FALSE,
417 then FilePath must match an exact file to be loaded.
418 @param[in] FilePath Pointer to the device path of the file that is absracted to the file buffer.
419 @param[out] FileSize Pointer to the size of the abstracted file buffer.
420 @param[out] AuthenticationStatus
421 Pointer to a caller-allocated UINT32 in which
422 the authentication status is returned.
424 @retval NULL File is NULL, or FileSize is NULL. Or the file can't be found.
425 @retval other The abstracted file buffer. The caller is responsible to free memory.
429 GetFileBufferByFilePath (
430 IN BOOLEAN BootPolicy
,
431 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
433 OUT UINT32
*AuthenticationStatus
436 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
437 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
439 EFI_GUID
*FvNameGuid
;
440 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
441 EFI_SECTION_TYPE SectionType
;
443 UINTN ImageBufferSize
;
444 EFI_FV_FILETYPE Type
;
445 EFI_FV_FILE_ATTRIBUTES Attrib
;
446 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
447 EFI_FILE_HANDLE FileHandle
;
448 EFI_FILE_HANDLE LastHandle
;
449 EFI_FILE_INFO
*FileInfo
;
451 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
452 EFI_LOAD_FILE2_PROTOCOL
*LoadFile2
;
456 // Check input File device path.
458 if (FilePath
== NULL
|| FileSize
== NULL
|| AuthenticationStatus
== NULL
) {
463 // Init local variable
470 *AuthenticationStatus
= 0;
473 // Copy File Device Path
475 OrigDevicePathNode
= DuplicateDevicePath (FilePath
);
476 if (OrigDevicePathNode
== NULL
) {
481 // Check whether this device path support FV2 protocol.
482 // Is so, this device path may contain a Image.
484 DevicePathNode
= OrigDevicePathNode
;
485 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
486 if (!EFI_ERROR (Status
)) {
488 // For FwVol File system there is only a single file name that is a GUID.
490 FvNameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePathNode
);
491 if (FvNameGuid
== NULL
) {
492 Status
= EFI_INVALID_PARAMETER
;
495 // Read image from the firmware file
497 Status
= gBS
->HandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&FwVol
);
498 if (!EFI_ERROR (Status
)) {
499 SectionType
= EFI_SECTION_PE32
;
501 Status
= FwVol
->ReadSection (
506 (VOID
**)&ImageBuffer
,
510 if (EFI_ERROR (Status
)) {
512 // Try a raw file, since a PE32 SECTION does not exist
514 if (ImageBuffer
!= NULL
) {
515 FreePool (ImageBuffer
);
516 *AuthenticationStatus
= 0;
519 Status
= FwVol
->ReadFile (
522 (VOID
**)&ImageBuffer
,
535 // Attempt to access the file via a file system interface
537 DevicePathNode
= OrigDevicePathNode
;
538 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
539 if (!EFI_ERROR (Status
)) {
540 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
541 if (!EFI_ERROR (Status
)) {
543 // Open the Volume to get the File System handle
545 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
546 if (!EFI_ERROR (Status
)) {
548 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
549 // directory information and filename can be seperate. The goal is to inch
550 // our way down each device path node and close the previous node
552 while (!IsDevicePathEnd (DevicePathNode
) && !EFI_ERROR (Status
)) {
553 if (DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
||
554 DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
) {
555 Status
= EFI_UNSUPPORTED
;
559 LastHandle
= FileHandle
;
562 Status
= LastHandle
->Open (
565 ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
,
571 // Close the previous node
573 LastHandle
->Close (LastHandle
);
575 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
578 if (!EFI_ERROR (Status
)) {
580 // We have found the file. Now we need to read it. Before we can read the file we need to
581 // figure out how big the file is.
585 Status
= FileHandle
->GetInfo (
592 if (Status
== EFI_BUFFER_TOO_SMALL
) {
593 FileInfo
= AllocatePool (FileInfoSize
);
594 if (FileInfo
== NULL
) {
595 Status
= EFI_OUT_OF_RESOURCES
;
597 Status
= FileHandle
->GetInfo (
606 if (!EFI_ERROR (Status
)) {
608 // Allocate space for the file
610 ImageBuffer
= AllocatePool ((UINTN
)FileInfo
->FileSize
);
611 if (ImageBuffer
== NULL
) {
612 Status
= EFI_OUT_OF_RESOURCES
;
615 // Read the file into the buffer we allocated
617 ImageBufferSize
= (UINTN
)FileInfo
->FileSize
;
618 Status
= FileHandle
->Read (FileHandle
, &ImageBufferSize
, ImageBuffer
);
623 // Close the file and Free FileInfo since we are done
625 if (FileInfo
!= NULL
) {
628 if (FileHandle
!= NULL
) {
629 FileHandle
->Close (FileHandle
);
637 // Attempt to access the file via LoadFile2 interface
640 DevicePathNode
= OrigDevicePathNode
;
641 Status
= gBS
->LocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &DevicePathNode
, &Handle
);
642 if (!EFI_ERROR (Status
)) {
643 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFile2ProtocolGuid
, (VOID
**)&LoadFile2
);
644 if (!EFI_ERROR (Status
)) {
646 // Call LoadFile2 with the correct buffer size
650 Status
= LoadFile2
->LoadFile (
657 if (Status
== EFI_BUFFER_TOO_SMALL
) {
658 ImageBuffer
= AllocatePool (ImageBufferSize
);
659 if (ImageBuffer
== NULL
) {
660 Status
= EFI_OUT_OF_RESOURCES
;
662 Status
= LoadFile2
->LoadFile (
677 // Attempt to access the file via LoadFile interface
679 DevicePathNode
= OrigDevicePathNode
;
680 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &DevicePathNode
, &Handle
);
681 if (!EFI_ERROR (Status
)) {
682 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
683 if (!EFI_ERROR (Status
)) {
685 // Call LoadFile with the correct buffer size
689 Status
= LoadFile
->LoadFile (
696 if (Status
== EFI_BUFFER_TOO_SMALL
) {
697 ImageBuffer
= AllocatePool (ImageBufferSize
);
698 if (ImageBuffer
== NULL
) {
699 Status
= EFI_OUT_OF_RESOURCES
;
701 Status
= LoadFile
->LoadFile (
715 if (EFI_ERROR (Status
)) {
716 if (ImageBuffer
!= NULL
) {
717 FreePool (ImageBuffer
);
722 *FileSize
= ImageBufferSize
;
725 FreePool (OrigDevicePathNode
);