2 Mde PI library functions.
4 Copyright (c) 2007 - 2008, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/DebugLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/DxeServicesLib.h>
20 #include <Protocol/FirmwareVolume2.h>
21 #include <Protocol/LoadedImage.h>
25 Identify the device handle from which the Image is loaded from. As this device handle is passed to
26 GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL
27 protocol instance should be located succesfully by calling gBS->HandleProtocol ().
29 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed
30 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.
32 If ImageHandle is NULL, then ASSERT ();
33 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();
35 @param ImageHandle The firmware allocated handle for UEFI image.
37 @retval EFI_HANDLE The device handle from which the Image is loaded from.
41 InternalImageHandleToFvHandle (
42 EFI_HANDLE ImageHandle
46 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
48 ASSERT (ImageHandle
!= NULL
);
50 Status
= gBS
->HandleProtocol (
51 (EFI_HANDLE
*) ImageHandle
,
52 &gEfiLoadedImageProtocolGuid
,
53 (VOID
**) &LoadedImage
56 ASSERT_EFI_ERROR (Status
);
58 return LoadedImage
->DeviceHandle
;
63 Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware
64 Section type and instance number from the specified Firmware Volume.
66 This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to
67 carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed
68 by NameGuid, SectionType and SectionInstance.
70 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection ()
71 found in PI Specification.
73 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section
74 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
77 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated
78 by this function. This function can be only called at TPL_NOTIFY and below.
80 If FvHandle is NULL, then ASSERT ();
81 If NameGuid is NULL, then ASSERT();
82 If Buffer is NULL, then ASSERT();
83 If Size is NULL, then ASSERT().
85 @param FvHandle The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.
86 @param NameGuid The GUID name of a Firmware File.
87 @param SectionType The Firmware Section type.
88 @param SectionInstance The instance number of Firmware Section to read from starting from 0.
89 @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.
90 @param Size On output, the size of Buffer.
92 @retval EFI_SUCCESS The image is found and data and size is returned.
93 @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.
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
)) {
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 Locates a requested firmware section within a file and returns it to a buffer allocated by this function.
169 GetSectionFromAnyFv () is used to read a specific section from a file within a firmware volume. The function
170 will search the first file with the specified name in all firmware volumes in the system. The search order for firmware
171 volumes in the system is determistic but abitrary if no new firmware volume is added into the system between
172 each calls of this function.
174 After the specific file is located, the function searches the specifc firmware section with type SectionType in this file.
175 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection ()
176 found in PI Specification.
178 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section
179 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
182 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated
183 by this function. This function can only be called at TPL_NOTIFY and below.
185 If NameGuid is NULL, then ASSERT();
186 If Buffer is NULL, then ASSERT();
187 If Size is NULL, then ASSERT().
189 @param NameGuid Pointer to an EFI_GUID, which indicates the file name from which the requested
190 section will be read. Type EFI_GUID is defined in
191 InstallProtocolInterface() in the UEFI 2.0 specification.
192 @param SectionType Indicates the section type to return. SectionType in conjunction with
193 SectionInstance indicates which section to return. Type
194 EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.
195 @param SectionInstance Indicates which instance of sections with a type of SectionType to return.
196 SectionType in conjunction with SectionInstance indicates which section to
197 return. SectionInstance is zero based.
198 @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not
199 including the section header. Caller is responsible to free this memory.
200 @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by
203 @retval EFI_SUCCESS The image is found and data and size is returned.
204 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.
205 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.
206 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.
207 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.
212 GetSectionFromAnyFv (
213 IN CONST EFI_GUID
*NameGuid
,
214 IN EFI_SECTION_TYPE SectionType
,
215 IN UINTN SectionInstance
,
221 EFI_HANDLE
*HandleBuffer
;
227 // Search the FV that contain the caller's FFS first.
228 // FV builder can choose to build FFS into the this FV
229 // so that this implementation of GetSectionFromAnyFv
230 // will locate the FFS faster.
232 FvHandle
= InternalImageHandleToFvHandle (gImageHandle
);
233 Status
= InternalGetSectionFromFv (
241 if (!EFI_ERROR (Status
)) {
246 Status
= gBS
->LocateHandleBuffer (
248 &gEfiFirmwareVolume2ProtocolGuid
,
253 if (EFI_ERROR (Status
)) {
257 for (Index
= 0; Index
< HandleCount
; Index
++) {
259 // Skip the FV that contain the caller's FFS
261 if (HandleBuffer
[Index
] != FvHandle
) {
262 Status
= InternalGetSectionFromFv (
271 if (!EFI_ERROR (Status
)) {
278 if (Index
== HandleCount
) {
279 Status
= EFI_NOT_FOUND
;
284 if (HandleBuffer
!= NULL
) {
285 FreePool(HandleBuffer
);
292 Locates a requested firmware section within a file and returns it to a buffer allocated by this function.
294 GetSectionFromFv () is used to read a specific section from a file within the same firmware volume from which
295 the running image is loaded. If the specific file is found, the function searches the specifc firmware section with type SectionType.
296 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection ()
297 found in PI Specification.
299 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section
300 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
303 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated
304 by this function. This function can be only called at TPL_NOTIFY and below.
306 If NameGuid is NULL, then ASSERT();
307 If Buffer is NULL, then ASSERT();
308 If Size is NULL, then ASSERT().
310 @param NameGuid Pointer to an EFI_GUID, which indicates the file name from which the requested
311 section will be read. Type EFI_GUID is defined in
312 InstallProtocolInterface() in the UEFI 2.0 specification.
313 @param SectionType Indicates the section type to return. SectionType in conjunction with
314 SectionInstance indicates which section to return. Type
315 EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.
316 @param SectionInstance Indicates which instance of sections with a type of SectionType to return.
317 SectionType in conjunction with SectionInstance indicates which section to
318 return. SectionInstance is zero based.
319 @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not
320 including the section header. Caller is responsible to free this memory.
321 @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by
325 @retval EFI_SUCCESS The image is found and data and size is returned.
326 @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.
327 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.
328 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.
329 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.
330 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.
336 IN CONST EFI_GUID
*NameGuid
,
337 IN EFI_SECTION_TYPE SectionType
,
338 IN UINTN SectionInstance
,
343 return InternalGetSectionFromFv (
344 InternalImageHandleToFvHandle(gImageHandle
),
355 Locates a requested firmware section within a file and returns it to a buffer allocated by this function.
357 GetSectionFromFfs () searches the specifc firmware section with type SectionType in the same firmware file from
358 which the running image is loaded. The details of this search order is defined in description of
359 EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () found in PI Specification.
361 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section
362 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
366 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated
367 by this function. This function can only be called at TPL_NOTIFY and below.
369 If Buffer is NULL, then ASSERT();
370 If Size is NULL, then ASSERT().
372 @param SectionType Indicates the section type to return. SectionType in conjunction with
373 SectionInstance indicates which section to return. Type
374 EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.
375 @param SectionInstance Indicates which instance of sections with a type of SectionType to return.
376 SectionType in conjunction with SectionInstance indicates which section to
377 return. SectionInstance is zero based.
378 @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not
379 including the section header. Caller is responsible to free this memory.
380 @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by
383 @retval EFI_SUCCESS The image is found and data and size is returned.
384 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.
385 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.
386 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.
387 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.
393 IN EFI_SECTION_TYPE SectionType
,
394 IN UINTN SectionInstance
,
399 return InternalGetSectionFromFv(
400 InternalImageHandleToFvHandle(gImageHandle
),