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.
5 Copyright (c) 2007 - 2008, 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/DxeServicesLib.h>
21 #include <Protocol/FirmwareVolume2.h>
22 #include <Protocol/LoadedImage.h>
26 Identify the device handle from which the Image is loaded from. As this device handle is passed to
27 GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL
28 protocol instance should be located succesfully by calling gBS->HandleProtocol ().
30 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed
31 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.
33 If ImageHandle is NULL, then ASSERT ();
34 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();
36 @param ImageHandle The firmware allocated handle for UEFI image.
38 @retval EFI_HANDLE The device handle from which the Image is loaded from.
42 InternalImageHandleToFvHandle (
43 EFI_HANDLE ImageHandle
47 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
49 ASSERT (ImageHandle
!= NULL
);
51 Status
= gBS
->HandleProtocol (
52 (EFI_HANDLE
*) ImageHandle
,
53 &gEfiLoadedImageProtocolGuid
,
54 (VOID
**) &LoadedImage
57 ASSERT_EFI_ERROR (Status
);
59 return LoadedImage
->DeviceHandle
;
64 Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware
65 Section type and instance number from the specified Firmware Volume.
67 This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to
68 carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed
69 by NameGuid, SectionType and SectionInstance.
71 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection ()
72 found in PI Specification.
74 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section
75 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
78 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated
79 by this function. This function can be only called at TPL_NOTIFY and below.
81 If FvHandle is NULL, then ASSERT ();
82 If NameGuid is NULL, then ASSERT();
83 If Buffer is NULL, then ASSERT();
84 If Size is NULL, then ASSERT().
86 @param FvHandle The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.
87 @param NameGuid The GUID name of a Firmware File.
88 @param SectionType The Firmware Section type.
89 @param SectionInstance The instance number of Firmware Section to read from starting from 0.
90 @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.
91 @param Size On output, the size of Buffer.
93 @retval EFI_SUCCESS The image is found and data and size is returned.
94 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.
95 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.
96 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.
97 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.
101 InternalGetSectionFromFv (
102 IN EFI_HANDLE FvHandle
,
103 IN CONST EFI_GUID
*NameGuid
,
104 IN EFI_SECTION_TYPE SectionType
,
105 IN UINTN SectionInstance
,
111 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
112 UINT32 AuthenticationStatus
;
114 ASSERT (NameGuid
!= NULL
);
115 ASSERT (Buffer
!= NULL
);
116 ASSERT (Size
!= NULL
);
118 ASSERT (FvHandle
!= NULL
);
120 Status
= gBS
->HandleProtocol (
122 &gEfiFirmwareVolume2ProtocolGuid
,
125 if (EFI_ERROR (Status
)) {
126 return EFI_NOT_FOUND
;
130 // Read desired section content in NameGuid file
134 Status
= Fv
->ReadSection (
141 &AuthenticationStatus
144 if (EFI_ERROR (Status
) && (SectionType
== EFI_SECTION_TE
)) {
146 // Try reading PE32 section, if the required section is TE type
150 Status
= Fv
->ReadSection (
157 &AuthenticationStatus
167 Searches all the availables firmware volumes and returns the first matching FFS section.
169 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.
170 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search
171 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances
172 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
173 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
174 It is the caller's responsibility to use FreePool() to free the allocated buffer.
175 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections
176 are retrieved from an FFS file based on SectionType and SectionInstance.
178 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
179 the search will be retried with a section type of EFI_SECTION_PE32.
180 This function must be called with a TPL <= TPL_NOTIFY.
182 If NameGuid is NULL, then ASSERT().
183 If Buffer is NULL, then ASSERT().
184 If Size is NULL, then ASSERT().
187 @param NameGuid A pointer to to the FFS filename GUID to search for within
188 any of the firmware volumes in the platform.
189 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.
190 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.
191 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
192 Is it the caller's responsibility to free this buffer using FreePool().
193 @param Size On output, a pointer to the size, in bytes, of Buffer.
195 @retval EFI_SUCCESS The specified FFS section was returned.
196 @retval EFI_NOT_FOUND The specified FFS section could not be found.
197 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
198 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
199 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
200 contains the matching FFS section does not allow reads.
204 GetSectionFromAnyFv (
205 IN CONST EFI_GUID
*NameGuid
,
206 IN EFI_SECTION_TYPE SectionType
,
207 IN UINTN SectionInstance
,
213 EFI_HANDLE
*HandleBuffer
;
219 // Search the FV that contain the caller's FFS first.
220 // FV builder can choose to build FFS into the this FV
221 // so that this implementation of GetSectionFromAnyFv
222 // will locate the FFS faster.
224 FvHandle
= InternalImageHandleToFvHandle (gImageHandle
);
225 Status
= InternalGetSectionFromFv (
233 if (!EFI_ERROR (Status
)) {
238 Status
= gBS
->LocateHandleBuffer (
240 &gEfiFirmwareVolume2ProtocolGuid
,
245 if (EFI_ERROR (Status
)) {
249 for (Index
= 0; Index
< HandleCount
; Index
++) {
251 // Skip the FV that contain the caller's FFS
253 if (HandleBuffer
[Index
] != FvHandle
) {
254 Status
= InternalGetSectionFromFv (
263 if (!EFI_ERROR (Status
)) {
270 if (Index
== HandleCount
) {
271 Status
= EFI_NOT_FOUND
;
276 if (HandleBuffer
!= NULL
) {
277 FreePool(HandleBuffer
);
284 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section.
286 This function searches the firmware volume that the currently executing module was loaded
287 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search
288 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance
289 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.
290 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.
291 It is the caller's responsibility to use FreePool() to free the allocated buffer.
292 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from
293 an FFS file based on SectionType and SectionInstance.
295 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.
296 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
297 the search will be retried with a section type of EFI_SECTION_PE32.
299 This function must be called with a TPL <= TPL_NOTIFY.
300 If NameGuid is NULL, then ASSERT().
301 If Buffer is NULL, then ASSERT().
302 If Size is NULL, then ASSERT().
304 @param NameGuid A pointer to to the FFS filename GUID to search for within
305 the firmware volumes that the currently executing module was loaded from.
306 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.
307 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.
308 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
309 Is it the caller's responsibility to free this buffer using FreePool().
310 @param Size On output, a pointer to the size, in bytes, of Buffer.
313 @retval EFI_SUCCESS The specified FFS section was returned.
314 @retval EFI_NOT_FOUND The specified FFS section could not be found.
315 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
316 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
317 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
318 contains the matching FFS section does not allow reads.
323 IN CONST EFI_GUID
*NameGuid
,
324 IN EFI_SECTION_TYPE SectionType
,
325 IN UINTN SectionInstance
,
330 return InternalGetSectionFromFv (
331 InternalImageHandleToFvHandle(gImageHandle
),
342 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.
344 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.
345 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType,
346 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(),
347 and the size of the allocated buffer is returned in Size. It is the caller's responsibility
348 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for
349 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.
351 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.
352 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,
353 the search will be retried with a section type of EFI_SECTION_PE32.
354 This function must be called with a TPL <= TPL_NOTIFY.
356 If Buffer is NULL, then ASSERT().
357 If Size is NULL, then ASSERT().
360 @param SectionType Indicates the FFS section type to search for within the FFS file
361 that the currently executing module was loaded from.
362 @param SectionInstance Indicates which section instance to retrieve within the FFS file
363 that the currently executing module was loaded from.
364 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found.
365 Is it the caller's responsibility to free this buffer using FreePool().
366 @param Size On output, a pointer to the size, in bytes, of Buffer.
368 @retval EFI_SUCCESS The specified FFS section was returned.
369 @retval EFI_NOT_FOUND The specified FFS section could not be found.
370 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.
371 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.
372 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that
373 contains the matching FFS section does not allow reads.
379 IN EFI_SECTION_TYPE SectionType
,
380 IN UINTN SectionInstance
,
385 return InternalGetSectionFromFv(
386 InternalImageHandleToFvHandle(gImageHandle
),