]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxePiLib/DxePiLib.c
Add DXE_CORE to supported module type. This library instance is able to support DxeCo...
[mirror_edk2.git] / MdePkg / Library / DxePiLib / DxePiLib.c
1 /** @file
2 Mde PI library functions.
3
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
9
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.
12
13 **/
14
15 #include <PiDxe.h>
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>
22
23
24 /**
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 ().
28
29 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed
30 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.
31
32 If ImageHandle is NULL, then ASSERT ();
33 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();
34
35 @param ImageHandle The firmware allocated handle for UEFI image.
36
37 @retval EFI_HANDLE The device handle from which the Image is loaded from.
38
39 **/
40 EFI_HANDLE
41 InternalImageHandleToFvHandle (
42 EFI_HANDLE ImageHandle
43 )
44 {
45 EFI_STATUS Status;
46 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
47
48 ASSERT (ImageHandle != NULL);
49
50 Status = gBS->HandleProtocol (
51 (EFI_HANDLE *) ImageHandle,
52 &gEfiLoadedImageProtocolGuid,
53 (VOID **) &LoadedImage
54 );
55
56 ASSERT_EFI_ERROR (Status);
57
58 return LoadedImage->DeviceHandle;
59
60 }
61
62 /**
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.
65
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.
69
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.
73
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.
78
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.
81
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().
86
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.
93
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.
100
101 **/
102
103
104 EFI_STATUS
105 GetSectionFromFv (
106 IN EFI_HANDLE FvHandle,
107 IN CONST EFI_GUID *NameGuid,
108 IN EFI_SECTION_TYPE SectionType,
109 IN UINTN Instance,
110 OUT VOID **Buffer,
111 OUT UINTN *Size
112 )
113 {
114 EFI_STATUS Status;
115 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
116 UINT32 AuthenticationStatus;
117
118 ASSERT (FvHandle != NULL);
119
120 Status = gBS->HandleProtocol (
121 FvHandle,
122 &gEfiFirmwareVolume2ProtocolGuid,
123 (VOID **) &Fv
124 );
125 if (EFI_ERROR (Status)) {
126 return Status;
127 }
128
129 //
130 // Read desired section content in NameGuid file
131 //
132 *Buffer = NULL;
133 *Size = 0;
134 Status = Fv->ReadSection (
135 Fv,
136 NameGuid,
137 SectionType,
138 0,
139 Buffer,
140 Size,
141 &AuthenticationStatus
142 );
143
144 if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {
145 //
146 // Try reading PE32 section, if the required section is TE type
147 //
148 *Buffer = NULL;
149 *Size = 0;
150 Status = Fv->ReadSection (
151 Fv,
152 NameGuid,
153 EFI_SECTION_PE32,
154 0,
155 Buffer,
156 Size,
157 &AuthenticationStatus
158 );
159 }
160
161 return Status;
162 }
163
164
165
166 EFI_STATUS
167 EFIAPI
168 PiLibGetSectionFromAnyFv (
169 IN CONST EFI_GUID *NameGuid,
170 IN EFI_SECTION_TYPE SectionType,
171 IN UINTN Instance,
172 OUT VOID **Buffer,
173 OUT UINTN *Size
174 )
175 {
176 EFI_STATUS Status;
177 EFI_HANDLE *HandleBuffer;
178 UINTN HandleCount;
179 UINTN Index;
180 EFI_HANDLE FvHandle;
181 EFI_TPL OldTpl;
182
183 //
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.
188 //
189 FvHandle = InternalImageHandleToFvHandle (gImageHandle);
190 Status = GetSectionFromFv (
191 FvHandle,
192 NameGuid,
193 SectionType,
194 Instance,
195 Buffer,
196 Size
197 );
198 if (!EFI_ERROR (Status)) {
199 return EFI_SUCCESS;
200 }
201
202 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
203
204 HandleBuffer = NULL;
205 Status = gBS->LocateHandleBuffer (
206 ByProtocol,
207 &gEfiFirmwareVolume2ProtocolGuid,
208 NULL,
209 &HandleCount,
210 &HandleBuffer
211 );
212 if (EFI_ERROR (Status)) {
213 goto Done;
214 }
215
216 for (Index = 0; Index < HandleCount; ++Index) {
217 //
218 // Skip the FV that contain the caller's FFS
219 //
220 if (HandleBuffer[Index] == FvHandle) {
221 continue;
222 }
223
224 Status = GetSectionFromFv (
225 HandleBuffer[Index],
226 NameGuid,
227 SectionType,
228 Instance,
229 Buffer,
230 Size
231 );
232
233 if (!EFI_ERROR (Status)) {
234 goto Done;
235 }
236 }
237
238 if (Index == HandleCount) {
239 Status = EFI_NOT_FOUND;
240 }
241
242 Done:
243
244 gBS->RestoreTPL (OldTpl);
245
246 if (HandleBuffer != NULL) {
247 FreePool(HandleBuffer);
248 }
249 return Status;
250
251 }
252
253
254 EFI_STATUS
255 EFIAPI
256 PiLibGetSectionFromCurrentFv (
257 IN CONST EFI_GUID *NameGuid,
258 IN EFI_SECTION_TYPE SectionType,
259 IN UINTN Instance,
260 OUT VOID **Buffer,
261 OUT UINTN *Size
262 )
263 {
264 return GetSectionFromFv(
265 InternalImageHandleToFvHandle(gImageHandle),
266 NameGuid,
267 SectionType,
268 Instance,
269 Buffer,
270 Size
271 );
272 }
273
274
275
276 EFI_STATUS
277 EFIAPI
278 PiLibGetSectionFromCurrentFfs (
279 IN EFI_SECTION_TYPE SectionType,
280 IN UINTN Instance,
281 OUT VOID **Buffer,
282 OUT UINTN *Size
283 )
284 {
285 return GetSectionFromFv(
286 InternalImageHandleToFvHandle(gImageHandle),
287 &gEfiCallerIdGuid,
288 SectionType,
289 Instance,
290 Buffer,
291 Size
292 );
293 }
294