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<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 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 Policy for Open Image File.If TRUE, indicates that the request
414 originates from the boot manager, and that the boot manager is
415 attempting to load FilePath as a boot selection. If FALSE,
416 then FilePath must match an exact file to be loaded.
417 @param[in] FilePath Pointer to the device path of the file that is absracted to
419 @param[out] FileSize Pointer to the size of the abstracted file buffer.
420 @param[out] AuthenticationStatus Pointer to a caller-allocated UINT32 in which the authentication
423 @retval NULL File is NULL, or FileSize is NULL. Or the file can't be found.
424 @retval other The abstracted file buffer. The caller is responsible to free memory.
428 GetFileBufferByFilePath (
429 IN BOOLEAN BootPolicy
,
430 IN CONST EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
432 OUT UINT32
*AuthenticationStatus
435 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
436 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
437 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePathNode
;
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
465 TempDevicePathNode
= NULL
;
471 *AuthenticationStatus
= 0;
474 // Copy File Device Path
476 OrigDevicePathNode
= DuplicateDevicePath (FilePath
);
477 if (OrigDevicePathNode
== NULL
) {
482 // Check whether this device path support FV2 protocol.
483 // Is so, this device path may contain a Image.
485 DevicePathNode
= OrigDevicePathNode
;
486 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
487 if (!EFI_ERROR (Status
)) {
489 // For FwVol File system there is only a single file name that is a GUID.
491 FvNameGuid
= EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) DevicePathNode
);
492 if (FvNameGuid
== NULL
) {
493 Status
= EFI_INVALID_PARAMETER
;
496 // Read image from the firmware file
498 Status
= gBS
->HandleProtocol (Handle
, &gEfiFirmwareVolume2ProtocolGuid
, (VOID
**)&FwVol
);
499 if (!EFI_ERROR (Status
)) {
500 SectionType
= EFI_SECTION_PE32
;
502 Status
= FwVol
->ReadSection (
507 (VOID
**)&ImageBuffer
,
511 if (EFI_ERROR (Status
)) {
513 // Try a raw file, since a PE32 SECTION does not exist
515 if (ImageBuffer
!= NULL
) {
516 FreePool (ImageBuffer
);
517 *AuthenticationStatus
= 0;
520 Status
= FwVol
->ReadFile (
523 (VOID
**)&ImageBuffer
,
536 // Attempt to access the file via a file system interface
538 DevicePathNode
= OrigDevicePathNode
;
539 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
540 if (!EFI_ERROR (Status
)) {
541 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
542 if (!EFI_ERROR (Status
)) {
544 // Open the Volume to get the File System handle
546 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
547 if (!EFI_ERROR (Status
)) {
549 // Duplicate the device path to avoid the access to unaligned device path node.
550 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
551 // nodes, It assures the fields in device path nodes are 2 byte aligned.
553 TempDevicePathNode
= DuplicateDevicePath (DevicePathNode
);
554 if (TempDevicePathNode
== NULL
) {
555 FileHandle
->Close (FileHandle
);
556 Status
= EFI_OUT_OF_RESOURCES
;
560 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
561 // directory information and filename can be seperate. The goal is to inch
562 // our way down each device path node and close the previous node
564 DevicePathNode
= TempDevicePathNode
;
565 while (!IsDevicePathEnd (DevicePathNode
) && !EFI_ERROR (Status
)) {
566 if (DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
||
567 DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
) {
568 Status
= EFI_UNSUPPORTED
;
572 LastHandle
= FileHandle
;
575 Status
= LastHandle
->Open (
578 ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
,
584 // Close the previous node
586 LastHandle
->Close (LastHandle
);
588 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
591 if (!EFI_ERROR (Status
)) {
593 // We have found the file. Now we need to read it. Before we can read the file we need to
594 // figure out how big the file is.
598 Status
= FileHandle
->GetInfo (
605 if (Status
== EFI_BUFFER_TOO_SMALL
) {
606 FileInfo
= AllocatePool (FileInfoSize
);
607 if (FileInfo
== NULL
) {
608 Status
= EFI_OUT_OF_RESOURCES
;
610 Status
= FileHandle
->GetInfo (
619 if (!EFI_ERROR (Status
) && (FileInfo
!= NULL
)) {
621 // Allocate space for the file
623 ImageBuffer
= AllocatePool ((UINTN
)FileInfo
->FileSize
);
624 if (ImageBuffer
== NULL
) {
625 Status
= EFI_OUT_OF_RESOURCES
;
628 // Read the file into the buffer we allocated
630 ImageBufferSize
= (UINTN
)FileInfo
->FileSize
;
631 Status
= FileHandle
->Read (FileHandle
, &ImageBufferSize
, ImageBuffer
);
636 // Close the file and Free FileInfo and TempDevicePathNode since we are done
638 if (FileInfo
!= NULL
) {
641 if (FileHandle
!= NULL
) {
642 FileHandle
->Close (FileHandle
);
644 FreePool (TempDevicePathNode
);
651 // Attempt to access the file via LoadFile2 interface
654 DevicePathNode
= OrigDevicePathNode
;
655 Status
= gBS
->LocateDevicePath (&gEfiLoadFile2ProtocolGuid
, &DevicePathNode
, &Handle
);
656 if (!EFI_ERROR (Status
)) {
657 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFile2ProtocolGuid
, (VOID
**)&LoadFile2
);
658 if (!EFI_ERROR (Status
)) {
660 // Call LoadFile2 with the correct buffer size
664 Status
= LoadFile2
->LoadFile (
671 if (Status
== EFI_BUFFER_TOO_SMALL
) {
672 ImageBuffer
= AllocatePool (ImageBufferSize
);
673 if (ImageBuffer
== NULL
) {
674 Status
= EFI_OUT_OF_RESOURCES
;
676 Status
= LoadFile2
->LoadFile (
691 // Attempt to access the file via LoadFile interface
693 DevicePathNode
= OrigDevicePathNode
;
694 Status
= gBS
->LocateDevicePath (&gEfiLoadFileProtocolGuid
, &DevicePathNode
, &Handle
);
695 if (!EFI_ERROR (Status
)) {
696 Status
= gBS
->HandleProtocol (Handle
, &gEfiLoadFileProtocolGuid
, (VOID
**)&LoadFile
);
697 if (!EFI_ERROR (Status
)) {
699 // Call LoadFile with the correct buffer size
703 Status
= LoadFile
->LoadFile (
710 if (Status
== EFI_BUFFER_TOO_SMALL
) {
711 ImageBuffer
= AllocatePool (ImageBufferSize
);
712 if (ImageBuffer
== NULL
) {
713 Status
= EFI_OUT_OF_RESOURCES
;
715 Status
= LoadFile
->LoadFile (
729 if (EFI_ERROR (Status
)) {
730 if (ImageBuffer
!= NULL
) {
731 FreePool (ImageBuffer
);
736 *FileSize
= ImageBufferSize
;
739 FreePool (OrigDevicePathNode
);