2 Mde PI library functions.
4 Copyright (c) 2007, 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/DxePiLib.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 Instance.
70 The search order for the section specified by SectionType within a Firmware File is defined by
71 EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection (). Please check Section 2.4 of Volume 3: Platform Initialization
72 Shared Architectural Elements for detailes.
74 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE will be used as Firmware Section type to read Firmware Section
75 data from the Firmware File. If no such section exists, EFI_SECTION_PE32 will be used as Firmware Section type to
76 read Firmware Section data from the Firmware File. If no such section specified is found to match ,
77 EFI_NOT_FOUND is returned.
79 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated
80 by this function. This function can be only called at TPL_NOTIFY and below.
82 If FvHandle is NULL, then ASSERT ();
83 If NameGuid is NULL, then ASSERT();
84 If Buffer is NULL, then ASSERT();
85 If Size is NULL, then ASSERT().
87 @param FvHandle The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.
88 @param NameGuid The GUID name of a Firmware File.
89 @param SectionType The Firmware Section type.
90 @param Instance The instance number of Firmware Section to read from starting from 0.
91 @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.
92 @param Size On output, the size of Buffer.
94 @retval EFI_SUCCESS The image is found and data and size is returned.
95 @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.
96 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.
97 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.
98 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.
99 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.
106 IN EFI_HANDLE FvHandle
,
107 IN CONST EFI_GUID
*NameGuid
,
108 IN EFI_SECTION_TYPE SectionType
,
115 EFI_FIRMWARE_VOLUME2_PROTOCOL
*Fv
;
116 UINT32 AuthenticationStatus
;
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
168 PiLibGetSectionFromAnyFv (
169 IN CONST EFI_GUID
*NameGuid
,
170 IN EFI_SECTION_TYPE SectionType
,
177 EFI_HANDLE
*HandleBuffer
;
184 // Search the FV that contain the caller's FFS first.
185 // FV builder can choose to build FFS into the this FV
186 // so that this implementation of GetSectionFromAnyFv
187 // will locate the FFS faster.
189 FvHandle
= InternalImageHandleToFvHandle (gImageHandle
);
190 Status
= GetSectionFromFv (
198 if (!EFI_ERROR (Status
)) {
202 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
205 Status
= gBS
->LocateHandleBuffer (
207 &gEfiFirmwareVolume2ProtocolGuid
,
212 if (EFI_ERROR (Status
)) {
216 for (Index
= 0; Index
< HandleCount
; ++Index
) {
218 // Skip the FV that contain the caller's FFS
220 if (HandleBuffer
[Index
] == FvHandle
) {
224 Status
= GetSectionFromFv (
233 if (!EFI_ERROR (Status
)) {
238 if (Index
== HandleCount
) {
239 Status
= EFI_NOT_FOUND
;
244 gBS
->RestoreTPL (OldTpl
);
246 if (HandleBuffer
!= NULL
) {
247 FreePool(HandleBuffer
);
256 PiLibGetSectionFromCurrentFv (
257 IN CONST EFI_GUID
*NameGuid
,
258 IN EFI_SECTION_TYPE SectionType
,
264 return GetSectionFromFv(
265 InternalImageHandleToFvHandle(gImageHandle
),
278 PiLibGetSectionFromCurrentFfs (
279 IN EFI_SECTION_TYPE SectionType
,
285 return GetSectionFromFv(
286 InternalImageHandleToFvHandle(gImageHandle
),