]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxePiLib/DxePiLib.c
1) Update comments for API in DxePiLib.h
[mirror_edk2.git] / MdePkg / Library / DxePiLib / DxePiLib.c
CommitLineData
1c280088 1/** @file\r
2 Mde PI library functions.\r
3\r
4 Copyright (c) 2007, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16#include <Library/DebugLib.h>\r
17#include <Library/MemoryAllocationLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
166152e8 19#include <Library/DxePiLib.h>\r
1c280088 20#include <Protocol/FirmwareVolume2.h>\r
21#include <Protocol/LoadedImage.h>\r
22\r
23\r
24/**\r
166152e8 25 Identify the device handle from which the Image is loaded from. As this device handle is passed to\r
26 GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL \r
27 protocol instance should be located succesfully by calling gBS->HandleProtocol ().\r
1c280088 28\r
166152e8 29 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed\r
30 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.\r
31 \r
32 If ImageHandle is NULL, then ASSERT ();\r
33 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();\r
34 \r
35 @param ImageHandle The firmware allocated handle for UEFI image.\r
1c280088 36\r
166152e8 37 @retval EFI_HANDLE The device handle from which the Image is loaded from.\r
1c280088 38\r
39**/\r
166152e8 40EFI_HANDLE\r
41InternalImageHandleToFvHandle (\r
42 EFI_HANDLE ImageHandle\r
43 )\r
44{\r
45 EFI_STATUS Status;\r
46 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
47 \r
48 ASSERT (ImageHandle != NULL);\r
49\r
50 Status = gBS->HandleProtocol (\r
51 (EFI_HANDLE *) ImageHandle,\r
52 &gEfiLoadedImageProtocolGuid,\r
53 (VOID **) &LoadedImage\r
54 );\r
55\r
56 ASSERT_EFI_ERROR (Status);\r
57\r
58 return LoadedImage->DeviceHandle;\r
59\r
60}\r
61\r
62/**\r
63 Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware \r
64 Section type and instance number from the specified Firmware Volume.\r
65 \r
66 This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to \r
67 carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed \r
ff197efb 68 by NameGuid, SectionType and SectionInstance. \r
166152e8 69 \r
ff197efb 70 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
71 found in PI Specification.\r
166152e8 72 \r
ff197efb 73 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
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 \r
75 is returned.\r
166152e8 76 \r
77 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
78 by this function. This function can be only called at TPL_NOTIFY and below.\r
79 \r
80 If FvHandle is NULL, then ASSERT ();\r
81 If NameGuid is NULL, then ASSERT();\r
82 If Buffer is NULL, then ASSERT();\r
83 If Size is NULL, then ASSERT().\r
84\r
ff197efb 85 @param FvHandle The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
86 @param NameGuid The GUID name of a Firmware File.\r
87 @param SectionType The Firmware Section type.\r
88 @param SectionInstance The instance number of Firmware Section to read from starting from 0.\r
166152e8 89 @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.\r
90 @param Size On output, the size of Buffer.\r
91 \r
ff197efb 92 @retval EFI_SUCCESS The image is found and data and size is returned.\r
93 @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.\r
94 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r
95 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
96 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
97 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r
166152e8 98 \r
f80b0830 99**/\r
1c280088 100EFI_STATUS\r
166152e8 101GetSectionFromFv (\r
102 IN EFI_HANDLE FvHandle,\r
103 IN CONST EFI_GUID *NameGuid,\r
104 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 105 IN UINTN SectionInstance,\r
166152e8 106 OUT VOID **Buffer,\r
107 OUT UINTN *Size\r
1c280088 108 )\r
109{\r
166152e8 110 EFI_STATUS Status;\r
111 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
112 UINT32 AuthenticationStatus;\r
113\r
114 ASSERT (FvHandle != NULL);\r
115\r
116 Status = gBS->HandleProtocol (\r
117 FvHandle,\r
118 &gEfiFirmwareVolume2ProtocolGuid,\r
119 (VOID **) &Fv\r
120 );\r
121 if (EFI_ERROR (Status)) {\r
122 return Status;\r
123 }\r
1c280088 124\r
125 //\r
126 // Read desired section content in NameGuid file\r
127 //\r
128 *Buffer = NULL;\r
129 *Size = 0;\r
130 Status = Fv->ReadSection (\r
131 Fv,\r
132 NameGuid,\r
133 SectionType,\r
ff197efb 134 SectionInstance,\r
1c280088 135 Buffer,\r
136 Size,\r
137 &AuthenticationStatus\r
138 );\r
139\r
140 if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r
141 //\r
166152e8 142 // Try reading PE32 section, if the required section is TE type \r
1c280088 143 //\r
144 *Buffer = NULL;\r
145 *Size = 0;\r
146 Status = Fv->ReadSection (\r
147 Fv,\r
148 NameGuid,\r
149 EFI_SECTION_PE32,\r
ff197efb 150 SectionInstance,\r
1c280088 151 Buffer,\r
152 Size,\r
153 &AuthenticationStatus\r
154 );\r
155 }\r
156\r
1c280088 157 return Status;\r
158}\r
159\r
b0d803fe 160\r
b0d803fe 161\r
f80b0830 162/**\r
ff197efb 163 Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r
164\r
165 PiLibGetSectionFromAnyFv () is used to read a specific section from a file within a firmware volume. The function\r
166 will search the first file with the specified name in all firmware volumes in the system. The search order for firmware \r
167 volumes in the system is determistic but abitrary if no new firmware volume is added into the system between \r
168 each calls of this function. \r
169\r
170 After the specific file is located, the function searches the specifc firmware section with type SectionType in this file. \r
171 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
172 found in PI Specification.\r
173\r
174 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
175 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 \r
176 is returned.\r
177\r
178 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
179 by this function. This function can only be called at TPL_NOTIFY and below.\r
180\r
181 If NameGuid is NULL, then ASSERT();\r
182 If Buffer is NULL, then ASSERT();\r
183 If Size is NULL, then ASSERT().\r
184\r
185 @param NameGuid Pointer to an EFI_GUID, which indicates the file name from which the requested \r
186 section will be read. Type EFI_GUID is defined in \r
187 InstallProtocolInterface() in the UEFI 2.0 specification. \r
188 @param SectionType Indicates the section type to return. SectionType in conjunction with \r
189 SectionInstance indicates which section to return. Type \r
190 EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r
191 @param SectionInstance Indicates which instance of sections with a type of SectionType to return. \r
192 SectionType in conjunction with SectionInstance indicates which section to \r
193 return. SectionInstance is zero based.\r
194 @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not \r
195 including the section header. Caller is responsible to free this memory.\r
196 @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r
197 *Buffer.\r
198\r
199 @retval EFI_SUCCESS The image is found and data and size is returned.\r
200 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r
201 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
202 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
203 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r
f80b0830 204\r
f80b0830 205**/\r
b0d803fe 206EFI_STATUS\r
207EFIAPI\r
166152e8 208PiLibGetSectionFromAnyFv (\r
b0d803fe 209 IN CONST EFI_GUID *NameGuid,\r
210 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 211 IN UINTN SectionInstance,\r
b0d803fe 212 OUT VOID **Buffer,\r
213 OUT UINTN *Size\r
214 )\r
215{\r
216 EFI_STATUS Status;\r
217 EFI_HANDLE *HandleBuffer;\r
218 UINTN HandleCount;\r
219 UINTN Index;\r
220 EFI_HANDLE FvHandle;\r
b0d803fe 221\r
222 //\r
223 // Search the FV that contain the caller's FFS first.\r
224 // FV builder can choose to build FFS into the this FV\r
225 // so that this implementation of GetSectionFromAnyFv\r
226 // will locate the FFS faster.\r
227 //\r
166152e8 228 FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
b0d803fe 229 Status = GetSectionFromFv (\r
230 FvHandle,\r
231 NameGuid,\r
232 SectionType,\r
ff197efb 233 SectionInstance,\r
b0d803fe 234 Buffer,\r
235 Size\r
236 );\r
237 if (!EFI_ERROR (Status)) {\r
238 return EFI_SUCCESS;\r
239 }\r
240\r
b0d803fe 241 HandleBuffer = NULL;\r
242 Status = gBS->LocateHandleBuffer (\r
243 ByProtocol,\r
244 &gEfiFirmwareVolume2ProtocolGuid,\r
245 NULL,\r
246 &HandleCount,\r
247 &HandleBuffer\r
248 );\r
249 if (EFI_ERROR (Status)) {\r
250 goto Done;\r
251 }\r
252\r
ff197efb 253 for (Index = 0; Index < HandleCount; Index++) {\r
b0d803fe 254 //\r
255 // Skip the FV that contain the caller's FFS\r
256 //\r
ff197efb 257 if (HandleBuffer[Index] != FvHandle) {\r
258 Status = GetSectionFromFv (\r
259 HandleBuffer[Index], \r
260 NameGuid, \r
261 SectionType, \r
262 SectionInstance,\r
263 Buffer, \r
264 Size\r
265 );\r
266\r
267 if (!EFI_ERROR (Status)) {\r
268 goto Done;\r
269 }\r
b0d803fe 270 }\r
271\r
b0d803fe 272 }\r
273\r
274 if (Index == HandleCount) {\r
275 Status = EFI_NOT_FOUND;\r
276 }\r
277\r
278Done:\r
279 \r
b0d803fe 280 if (HandleBuffer != NULL) { \r
281 FreePool(HandleBuffer);\r
282 }\r
283 return Status;\r
284 \r
285}\r
286\r
f80b0830 287/**\r
ff197efb 288 Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r
289\r
290 PiLibGetSectionFromCurrentFv () is used to read a specific section from a file within the same firmware volume from which\r
291 the running image is loaded. If the specific file is found, the function searches the specifc firmware section with type SectionType. \r
292 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
293 found in PI Specification.\r
294\r
295 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
296 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 \r
297 is returned.\r
298\r
299 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
300 by this function. This function can be only called at TPL_NOTIFY and below.\r
301\r
302 If NameGuid is NULL, then ASSERT();\r
303 If Buffer is NULL, then ASSERT();\r
304 If Size is NULL, then ASSERT().\r
305\r
306 @param NameGuid Pointer to an EFI_GUID, which indicates the file name from which the requested \r
307 section will be read. Type EFI_GUID is defined in \r
308 InstallProtocolInterface() in the UEFI 2.0 specification. \r
309 @param SectionType Indicates the section type to return. SectionType in conjunction with \r
310 SectionInstance indicates which section to return. Type \r
311 EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r
312 @param SectionInstance Indicates which instance of sections with a type of SectionType to return. \r
313 SectionType in conjunction with SectionInstance indicates which section to \r
314 return. SectionInstance is zero based.\r
315 @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not \r
316 including the section header. Caller is responsible to free this memory.\r
317 @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r
318 *Buffer.\r
319\r
320\r
321 @retval EFI_SUCCESS The image is found and data and size is returned.\r
322 @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.\r
323 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r
324 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
325 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
326 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r
f80b0830 327 \r
328**/\r
b0d803fe 329EFI_STATUS\r
330EFIAPI\r
166152e8 331PiLibGetSectionFromCurrentFv (\r
b0d803fe 332 IN CONST EFI_GUID *NameGuid,\r
333 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 334 IN UINTN SectionInstance,\r
b0d803fe 335 OUT VOID **Buffer,\r
336 OUT UINTN *Size\r
337 )\r
338{\r
339 return GetSectionFromFv(\r
166152e8 340 InternalImageHandleToFvHandle(gImageHandle),\r
b0d803fe 341 NameGuid,\r
342 SectionType,\r
ff197efb 343 SectionInstance,\r
b0d803fe 344 Buffer,\r
345 Size\r
346 );\r
347}\r
348\r
349\r
f80b0830 350/**\r
ff197efb 351 Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r
352\r
353 PiLibGetSectionFromCurrentFfs () searches the specifc firmware section with type SectionType in the same firmware file from\r
354 which the running image is loaded. The details of this search order is defined in description of \r
355 EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () found in PI Specification.\r
356\r
357 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
358 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 \r
359 is returned.\r
360\r
361\r
362 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
363 by this function. This function can only be called at TPL_NOTIFY and below.\r
364\r
365 If Buffer is NULL, then ASSERT();\r
366 If Size is NULL, then ASSERT().\r
367\r
368 @param SectionType Indicates the section type to return. SectionType in conjunction with \r
369 SectionInstance indicates which section to return. Type \r
370 EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r
371 @param SectionInstance Indicates which instance of sections with a type of SectionType to return. \r
372 SectionType in conjunction with SectionInstance indicates which section to \r
373 return. SectionInstance is zero based.\r
374 @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not \r
375 including the section header. Caller is responsible to free this memory.\r
376 @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r
377 *Buffer.\r
378\r
379 @retval EFI_SUCCESS The image is found and data and size is returned.\r
380 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r
381 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
382 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
383 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r
f80b0830 384 \r
385**/\r
b0d803fe 386EFI_STATUS\r
387EFIAPI\r
166152e8 388PiLibGetSectionFromCurrentFfs (\r
b0d803fe 389 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 390 IN UINTN SectionInstance,\r
b0d803fe 391 OUT VOID **Buffer,\r
392 OUT UINTN *Size\r
393 )\r
394{\r
395 return GetSectionFromFv(\r
166152e8 396 InternalImageHandleToFvHandle(gImageHandle),\r
b0d803fe 397 &gEfiCallerIdGuid,\r
398 SectionType,\r
ff197efb 399 SectionInstance,\r
b0d803fe 400 Buffer,\r
401 Size\r
402 );\r
403}\r
404\r