]>
Commit | Line | Data |
---|---|---|
1c280088 | 1 | /** @file\r |
2 | Mde PI library functions.\r | |
3 | \r | |
8cf43dd7 | 4 | Copyright (c) 2007 - 2008, Intel Corporation<BR>\r |
1c280088 | 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 | 40 | EFI_HANDLE\r |
41 | InternalImageHandleToFvHandle (\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 | 100 | EFI_STATUS\r |
166152e8 | 101 | GetSectionFromFv (\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 | |
8cf43dd7 | 114 | ASSERT (NameGuid != NULL);\r |
115 | ASSERT (Buffer != NULL);\r | |
116 | ASSERT (Size != NULL);\r | |
117 | \r | |
166152e8 | 118 | ASSERT (FvHandle != NULL);\r |
119 | \r | |
120 | Status = gBS->HandleProtocol (\r | |
121 | FvHandle,\r | |
122 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
123 | (VOID **) &Fv\r | |
124 | );\r | |
125 | if (EFI_ERROR (Status)) {\r | |
126 | return Status;\r | |
127 | }\r | |
1c280088 | 128 | \r |
129 | //\r | |
130 | // Read desired section content in NameGuid file\r | |
131 | //\r | |
132 | *Buffer = NULL;\r | |
133 | *Size = 0;\r | |
134 | Status = Fv->ReadSection (\r | |
135 | Fv,\r | |
136 | NameGuid,\r | |
137 | SectionType,\r | |
ff197efb | 138 | SectionInstance,\r |
1c280088 | 139 | Buffer,\r |
140 | Size,\r | |
141 | &AuthenticationStatus\r | |
142 | );\r | |
143 | \r | |
144 | if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r | |
145 | //\r | |
166152e8 | 146 | // Try reading PE32 section, if the required section is TE type \r |
1c280088 | 147 | //\r |
148 | *Buffer = NULL;\r | |
149 | *Size = 0;\r | |
150 | Status = Fv->ReadSection (\r | |
151 | Fv,\r | |
152 | NameGuid,\r | |
153 | EFI_SECTION_PE32,\r | |
ff197efb | 154 | SectionInstance,\r |
1c280088 | 155 | Buffer,\r |
156 | Size,\r | |
157 | &AuthenticationStatus\r | |
158 | );\r | |
159 | }\r | |
160 | \r | |
1c280088 | 161 | return Status;\r |
162 | }\r | |
163 | \r | |
b0d803fe | 164 | \r |
b0d803fe | 165 | \r |
f80b0830 | 166 | /**\r |
ff197efb | 167 | Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r |
168 | \r | |
169 | PiLibGetSectionFromAnyFv () is used to read a specific section from a file within a firmware volume. The function\r | |
170 | will search the first file with the specified name in all firmware volumes in the system. The search order for firmware \r | |
171 | volumes in the system is determistic but abitrary if no new firmware volume is added into the system between \r | |
172 | each calls of this function. \r | |
173 | \r | |
174 | After the specific file is located, the function searches the specifc firmware section with type SectionType in this file. \r | |
175 | The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r | |
176 | found in PI Specification.\r | |
177 | \r | |
178 | If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r | |
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 \r | |
180 | is returned.\r | |
181 | \r | |
182 | The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r | |
183 | by this function. This function can only be called at TPL_NOTIFY and below.\r | |
184 | \r | |
185 | If NameGuid is NULL, then ASSERT();\r | |
186 | If Buffer is NULL, then ASSERT();\r | |
187 | If Size is NULL, then ASSERT().\r | |
188 | \r | |
189 | @param NameGuid Pointer to an EFI_GUID, which indicates the file name from which the requested \r | |
190 | section will be read. Type EFI_GUID is defined in \r | |
191 | InstallProtocolInterface() in the UEFI 2.0 specification. \r | |
192 | @param SectionType Indicates the section type to return. SectionType in conjunction with \r | |
193 | SectionInstance indicates which section to return. Type \r | |
194 | EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r | |
195 | @param SectionInstance Indicates which instance of sections with a type of SectionType to return. \r | |
196 | SectionType in conjunction with SectionInstance indicates which section to \r | |
197 | return. SectionInstance is zero based.\r | |
198 | @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not \r | |
199 | including the section header. Caller is responsible to free this memory.\r | |
200 | @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r | |
201 | *Buffer.\r | |
202 | \r | |
203 | @retval EFI_SUCCESS The image is found and data and size is returned.\r | |
204 | @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r | |
205 | @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r | |
206 | @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r | |
207 | @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r | |
f80b0830 | 208 | \r |
f80b0830 | 209 | **/\r |
b0d803fe | 210 | EFI_STATUS\r |
211 | EFIAPI\r | |
166152e8 | 212 | PiLibGetSectionFromAnyFv (\r |
b0d803fe | 213 | IN CONST EFI_GUID *NameGuid,\r |
214 | IN EFI_SECTION_TYPE SectionType,\r | |
ff197efb | 215 | IN UINTN SectionInstance,\r |
b0d803fe | 216 | OUT VOID **Buffer,\r |
217 | OUT UINTN *Size\r | |
218 | )\r | |
219 | {\r | |
220 | EFI_STATUS Status;\r | |
221 | EFI_HANDLE *HandleBuffer;\r | |
222 | UINTN HandleCount;\r | |
223 | UINTN Index;\r | |
224 | EFI_HANDLE FvHandle;\r | |
b0d803fe | 225 | \r |
226 | //\r | |
227 | // Search the FV that contain the caller's FFS first.\r | |
228 | // FV builder can choose to build FFS into the this FV\r | |
229 | // so that this implementation of GetSectionFromAnyFv\r | |
230 | // will locate the FFS faster.\r | |
231 | //\r | |
166152e8 | 232 | FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r |
b0d803fe | 233 | Status = GetSectionFromFv (\r |
234 | FvHandle,\r | |
235 | NameGuid,\r | |
236 | SectionType,\r | |
ff197efb | 237 | SectionInstance,\r |
b0d803fe | 238 | Buffer,\r |
239 | Size\r | |
240 | );\r | |
241 | if (!EFI_ERROR (Status)) {\r | |
242 | return EFI_SUCCESS;\r | |
243 | }\r | |
244 | \r | |
b0d803fe | 245 | HandleBuffer = NULL;\r |
246 | Status = gBS->LocateHandleBuffer (\r | |
247 | ByProtocol,\r | |
248 | &gEfiFirmwareVolume2ProtocolGuid,\r | |
249 | NULL,\r | |
250 | &HandleCount,\r | |
251 | &HandleBuffer\r | |
252 | );\r | |
253 | if (EFI_ERROR (Status)) {\r | |
254 | goto Done;\r | |
255 | }\r | |
256 | \r | |
ff197efb | 257 | for (Index = 0; Index < HandleCount; Index++) {\r |
b0d803fe | 258 | //\r |
259 | // Skip the FV that contain the caller's FFS\r | |
260 | //\r | |
ff197efb | 261 | if (HandleBuffer[Index] != FvHandle) {\r |
262 | Status = GetSectionFromFv (\r | |
263 | HandleBuffer[Index], \r | |
264 | NameGuid, \r | |
265 | SectionType, \r | |
266 | SectionInstance,\r | |
267 | Buffer, \r | |
268 | Size\r | |
269 | );\r | |
270 | \r | |
271 | if (!EFI_ERROR (Status)) {\r | |
272 | goto Done;\r | |
273 | }\r | |
b0d803fe | 274 | }\r |
275 | \r | |
b0d803fe | 276 | }\r |
277 | \r | |
278 | if (Index == HandleCount) {\r | |
279 | Status = EFI_NOT_FOUND;\r | |
280 | }\r | |
281 | \r | |
282 | Done:\r | |
283 | \r | |
b0d803fe | 284 | if (HandleBuffer != NULL) { \r |
285 | FreePool(HandleBuffer);\r | |
286 | }\r | |
287 | return Status;\r | |
288 | \r | |
289 | }\r | |
290 | \r | |
f80b0830 | 291 | /**\r |
ff197efb | 292 | Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r |
293 | \r | |
294 | PiLibGetSectionFromCurrentFv () is used to read a specific section from a file within the same firmware volume from which\r | |
295 | the running image is loaded. If the specific file is found, the function searches the specifc firmware section with type SectionType. \r | |
296 | The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r | |
297 | found in PI Specification.\r | |
298 | \r | |
299 | If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r | |
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 \r | |
301 | is returned.\r | |
302 | \r | |
303 | The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r | |
304 | by this function. This function can be only called at TPL_NOTIFY and below.\r | |
305 | \r | |
306 | If NameGuid is NULL, then ASSERT();\r | |
307 | If Buffer is NULL, then ASSERT();\r | |
308 | If Size is NULL, then ASSERT().\r | |
309 | \r | |
310 | @param NameGuid Pointer to an EFI_GUID, which indicates the file name from which the requested \r | |
311 | section will be read. Type EFI_GUID is defined in \r | |
312 | InstallProtocolInterface() in the UEFI 2.0 specification. \r | |
313 | @param SectionType Indicates the section type to return. SectionType in conjunction with \r | |
314 | SectionInstance indicates which section to return. Type \r | |
315 | EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r | |
316 | @param SectionInstance Indicates which instance of sections with a type of SectionType to return. \r | |
317 | SectionType in conjunction with SectionInstance indicates which section to \r | |
318 | return. SectionInstance is zero based.\r | |
319 | @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not \r | |
320 | including the section header. Caller is responsible to free this memory.\r | |
321 | @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r | |
322 | *Buffer.\r | |
323 | \r | |
324 | \r | |
325 | @retval EFI_SUCCESS The image is found and data and size is returned.\r | |
326 | @retval EFI_UNSUPPORTED FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.\r | |
327 | @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r | |
328 | @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r | |
329 | @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r | |
330 | @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r | |
f80b0830 | 331 | \r |
332 | **/\r | |
b0d803fe | 333 | EFI_STATUS\r |
334 | EFIAPI\r | |
166152e8 | 335 | PiLibGetSectionFromCurrentFv (\r |
b0d803fe | 336 | IN CONST EFI_GUID *NameGuid,\r |
337 | IN EFI_SECTION_TYPE SectionType,\r | |
ff197efb | 338 | IN UINTN SectionInstance,\r |
b0d803fe | 339 | OUT VOID **Buffer,\r |
340 | OUT UINTN *Size\r | |
341 | )\r | |
342 | {\r | |
343 | return GetSectionFromFv(\r | |
166152e8 | 344 | InternalImageHandleToFvHandle(gImageHandle),\r |
b0d803fe | 345 | NameGuid,\r |
346 | SectionType,\r | |
ff197efb | 347 | SectionInstance,\r |
b0d803fe | 348 | Buffer,\r |
349 | Size\r | |
350 | );\r | |
351 | }\r | |
352 | \r | |
353 | \r | |
f80b0830 | 354 | /**\r |
ff197efb | 355 | Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r |
356 | \r | |
357 | PiLibGetSectionFromCurrentFfs () searches the specifc firmware section with type SectionType in the same firmware file from\r | |
358 | which the running image is loaded. The details of this search order is defined in description of \r | |
359 | EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () found in PI Specification.\r | |
360 | \r | |
361 | If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r | |
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 \r | |
363 | is returned.\r | |
364 | \r | |
365 | \r | |
366 | The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r | |
367 | by this function. This function can only be called at TPL_NOTIFY and below.\r | |
368 | \r | |
369 | If Buffer is NULL, then ASSERT();\r | |
370 | If Size is NULL, then ASSERT().\r | |
371 | \r | |
372 | @param SectionType Indicates the section type to return. SectionType in conjunction with \r | |
373 | SectionInstance indicates which section to return. Type \r | |
374 | EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r | |
375 | @param SectionInstance Indicates which instance of sections with a type of SectionType to return. \r | |
376 | SectionType in conjunction with SectionInstance indicates which section to \r | |
377 | return. SectionInstance is zero based.\r | |
378 | @param Buffer Pointer to a pointer to a buffer in which the section contents are returned, not \r | |
379 | including the section header. Caller is responsible to free this memory.\r | |
380 | @param Size Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r | |
381 | *Buffer.\r | |
382 | \r | |
383 | @retval EFI_SUCCESS The image is found and data and size is returned.\r | |
384 | @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r | |
385 | @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r | |
386 | @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r | |
387 | @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r | |
f80b0830 | 388 | \r |
389 | **/\r | |
b0d803fe | 390 | EFI_STATUS\r |
391 | EFIAPI\r | |
166152e8 | 392 | PiLibGetSectionFromCurrentFfs (\r |
b0d803fe | 393 | IN EFI_SECTION_TYPE SectionType,\r |
ff197efb | 394 | IN UINTN SectionInstance,\r |
b0d803fe | 395 | OUT VOID **Buffer,\r |
396 | OUT UINTN *Size\r | |
397 | )\r | |
398 | {\r | |
399 | return GetSectionFromFv(\r | |
166152e8 | 400 | InternalImageHandleToFvHandle(gImageHandle),\r |
b0d803fe | 401 | &gEfiCallerIdGuid,\r |
402 | SectionType,\r | |
ff197efb | 403 | SectionInstance,\r |
b0d803fe | 404 | Buffer,\r |
405 | Size\r | |
406 | );\r | |
407 | }\r | |
408 | \r |