]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeServicesLib/DxeServicesLib.c
Make sure gBS FreePool() is used to free the buffer always allocated by gBS AllocateP...
[mirror_edk2.git] / MdePkg / Library / DxeServicesLib / DxeServicesLib.c
CommitLineData
1c280088 1/** @file\r
b75a165d 2 MDE DXE Services Library provides functions that simplify the development of DXE Drivers. \r
84716614 3 These functions help access data from sections of FFS files or from file path.\r
1c280088 4\r
19388d29
HT
5 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
6 This program and the accompanying materials\r
1c280088 7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
2fc59a00 9 http://opensource.org/licenses/bsd-license.php.\r
1c280088 10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <PiDxe.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/MemoryAllocationLib.h>\r
19#include <Library/UefiBootServicesTableLib.h>\r
84716614
LG
20#include <Library/DevicePathLib.h>\r
21#include <Library/UefiLib.h>\r
eb9dd4d0 22#include <Library/DxeServicesLib.h>\r
1c280088 23#include <Protocol/FirmwareVolume2.h>\r
24#include <Protocol/LoadedImage.h>\r
84716614
LG
25#include <Protocol/LoadFile2.h>\r
26#include <Protocol/LoadFile.h>\r
27#include <Protocol/SimpleFileSystem.h>\r
28#include <Guid/FileInfo.h>\r
1c280088 29\r
30/**\r
166152e8 31 Identify the device handle from which the Image is loaded from. As this device handle is passed to\r
32 GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL \r
33 protocol instance should be located succesfully by calling gBS->HandleProtocol ().\r
1c280088 34\r
166152e8 35 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed\r
36 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.\r
37 \r
38 If ImageHandle is NULL, then ASSERT ();\r
39 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();\r
40 \r
41 @param ImageHandle The firmware allocated handle for UEFI image.\r
1c280088 42\r
58380e9c 43 @retval EFI_HANDLE The device handle from which the Image is loaded from.\r
1c280088 44\r
45**/\r
166152e8 46EFI_HANDLE\r
47InternalImageHandleToFvHandle (\r
48 EFI_HANDLE ImageHandle\r
49 )\r
50{\r
51 EFI_STATUS Status;\r
52 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
53 \r
54 ASSERT (ImageHandle != NULL);\r
55\r
56 Status = gBS->HandleProtocol (\r
57 (EFI_HANDLE *) ImageHandle,\r
58 &gEfiLoadedImageProtocolGuid,\r
59 (VOID **) &LoadedImage\r
60 );\r
61\r
62 ASSERT_EFI_ERROR (Status);\r
63\r
64 return LoadedImage->DeviceHandle;\r
65\r
66}\r
67\r
68/**\r
b75a165d
LG
69 Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware \r
70 Section type and instance number from the specified Firmware Volume.\r
71\r
72 This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to \r
73 carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed \r
74 by NameGuid, SectionType and SectionInstance. \r
75 \r
76 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
77 found in PI Specification.\r
78 \r
79 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
80 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
81 is returned.\r
166152e8 82 \r
b75a165d
LG
83 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
84 by this function. This function can be only called at TPL_NOTIFY and below.\r
166152e8 85 \r
b75a165d
LG
86 If FvHandle is NULL, then ASSERT ();\r
87 If NameGuid is NULL, then ASSERT();\r
88 If Buffer is NULL, then ASSERT();\r
89 If Size is NULL, then ASSERT().\r
90\r
58380e9c 91 @param FvHandle The device handle that contains a instance of \r
92 EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
b75a165d
LG
93 @param NameGuid The GUID name of a Firmware File.\r
94 @param SectionType The Firmware Section type.\r
58380e9c 95 @param SectionInstance The instance number of Firmware Section to \r
96 read from starting from 0.\r
97 @param Buffer On output, Buffer contains the the data read \r
98 from the section in the Firmware File found.\r
b75a165d
LG
99 @param Size On output, the size of Buffer.\r
100\r
101 @retval EFI_SUCCESS The image is found and data and size is returned.\r
58380e9c 102 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType \r
103 can't be found.\r
104 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the \r
105 output data buffer or complete the operations.\r
106 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the \r
107 Firmware Volume.\r
108 @retval EFI_ACCESS_DENIED The firmware volume containing the searched \r
109 Firmware File is configured to disallow reads.\r
166152e8 110 \r
f80b0830 111**/\r
1c280088 112EFI_STATUS\r
eb9dd4d0 113InternalGetSectionFromFv (\r
166152e8 114 IN EFI_HANDLE FvHandle,\r
115 IN CONST EFI_GUID *NameGuid,\r
116 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 117 IN UINTN SectionInstance,\r
166152e8 118 OUT VOID **Buffer,\r
119 OUT UINTN *Size\r
1c280088 120 )\r
121{\r
166152e8 122 EFI_STATUS Status;\r
123 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
124 UINT32 AuthenticationStatus;\r
0433d8f0 125 VOID* TempBuffer;\r
166152e8 126\r
8cf43dd7 127 ASSERT (NameGuid != NULL);\r
128 ASSERT (Buffer != NULL);\r
129 ASSERT (Size != NULL);\r
130 \r
166152e8 131 ASSERT (FvHandle != NULL);\r
132\r
133 Status = gBS->HandleProtocol (\r
134 FvHandle,\r
135 &gEfiFirmwareVolume2ProtocolGuid,\r
136 (VOID **) &Fv\r
137 );\r
138 if (EFI_ERROR (Status)) {\r
7ca066f9 139 return EFI_NOT_FOUND;\r
166152e8 140 }\r
1c280088 141\r
142 //\r
143 // Read desired section content in NameGuid file\r
144 //\r
145 *Buffer = NULL;\r
146 *Size = 0;\r
147 Status = Fv->ReadSection (\r
148 Fv,\r
149 NameGuid,\r
150 SectionType,\r
ff197efb 151 SectionInstance,\r
1c280088 152 Buffer,\r
153 Size,\r
154 &AuthenticationStatus\r
155 );\r
156\r
157 if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r
158 //\r
166152e8 159 // Try reading PE32 section, if the required section is TE type \r
1c280088 160 //\r
161 *Buffer = NULL;\r
162 *Size = 0;\r
163 Status = Fv->ReadSection (\r
164 Fv,\r
165 NameGuid,\r
166 EFI_SECTION_PE32,\r
ff197efb 167 SectionInstance,\r
1c280088 168 Buffer,\r
169 Size,\r
170 &AuthenticationStatus\r
171 );\r
172 }\r
173\r
0433d8f0
LG
174 if (!EFI_ERROR (Status)) {\r
175 //\r
176 // The found buffer by FV protocol is allocated by gBS AllocatePool() service. \r
177 // Copy the found buffer to the allocated buffer by AllocatePool().\r
178 // So, the returned buffer can be freed by FreePool().\r
179 //\r
180 TempBuffer = AllocateCopyPool (*Size, *Buffer);\r
181 gBS->FreePool (*Buffer);\r
182 *Buffer = TempBuffer;\r
183 }\r
184\r
1c280088 185 return Status;\r
186}\r
187\r
682cee4c
LG
188/**\r
189 Searches all the available firmware volumes and returns the first matching FFS section. \r
190\r
191 This function searches all the firmware volumes for FFS files with FV file type specified by FileType\r
192 The order that the firmware volumes is searched is not deterministic. For each available FV a search \r
193 is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType, \r
194 the FileInstance instance will be the matched FFS file. For each FFS file found a search \r
195 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances \r
196 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer. \r
197 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
198 It is the caller's responsibility to use FreePool() to free the allocated buffer. \r
199 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections \r
200 are retrieved from an FFS file based on SectionType and SectionInstance.\r
201\r
202 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
203 the search will be retried with a section type of EFI_SECTION_PE32.\r
204 This function must be called with a TPL <= TPL_NOTIFY.\r
205\r
206 If Buffer is NULL, then ASSERT().\r
207 If Size is NULL, then ASSERT().\r
208\r
58380e9c 209 @param FileType Indicates the FV file type to search for within all \r
210 available FVs.\r
211 @param FileInstance Indicates which file instance within all available \r
212 FVs specified by FileType.\r
682cee4c 213 FileInstance starts from zero.\r
58380e9c 214 @param SectionType Indicates the FFS section type to search for \r
215 within the FFS file \r
682cee4c
LG
216 specified by FileType with FileInstance.\r
217 @param SectionInstance Indicates which section instance within the FFS file \r
58380e9c 218 specified by FileType with FileInstance to retrieve. \r
219 SectionInstance starts from zero.\r
220 @param Buffer On output, a pointer to a callee allocated buffer \r
221 containing the FFS file section that was found.\r
222 Is it the caller's responsibility to free this \r
223 buffer using FreePool().\r
682cee4c
LG
224 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
225\r
226 @retval EFI_SUCCESS The specified FFS section was returned.\r
227 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
58380e9c 228 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve \r
229 the matching FFS section.\r
230 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a \r
231 device error.\r
232 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because \r
233 the firmware volume that \r
682cee4c
LG
234 contains the matching FFS section does not allow reads.\r
235**/\r
236EFI_STATUS\r
237EFIAPI\r
238GetSectionFromAnyFvByFileType (\r
239 IN EFI_FV_FILETYPE FileType,\r
240 IN UINTN FileInstance,\r
241 IN EFI_SECTION_TYPE SectionType,\r
242 IN UINTN SectionInstance,\r
243 OUT VOID **Buffer,\r
244 OUT UINTN *Size\r
245 )\r
246{\r
247 EFI_STATUS Status;\r
248 EFI_HANDLE *HandleBuffer;\r
249 UINTN HandleCount;\r
250 UINTN IndexFv;\r
251 UINTN IndexFile;\r
252 UINTN Key;\r
253 EFI_GUID NameGuid;\r
254 EFI_FV_FILE_ATTRIBUTES Attributes;\r
255 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
256\r
257 //\r
258 // Locate all available FVs.\r
259 //\r
260 HandleBuffer = NULL;\r
261 Status = gBS->LocateHandleBuffer (\r
262 ByProtocol,\r
263 &gEfiFirmwareVolume2ProtocolGuid,\r
264 NULL,\r
265 &HandleCount,\r
266 &HandleBuffer\r
267 );\r
268 if (EFI_ERROR (Status)) {\r
269 return Status;\r
270 }\r
271\r
272 //\r
273 // Go through FVs one by one to find the required section data.\r
274 //\r
275 for (IndexFv = 0; IndexFv < HandleCount; IndexFv++) {\r
276 Status = gBS->HandleProtocol (\r
277 HandleBuffer[IndexFv],\r
278 &gEfiFirmwareVolume2ProtocolGuid,\r
279 (VOID **)&Fv\r
280 );\r
281 if (EFI_ERROR (Status)) {\r
282 continue;\r
283 }\r
284\r
285 //\r
286 // Use Firmware Volume 2 Protocol to search for a file of type FileType in all FVs.\r
287 //\r
288 IndexFile = FileInstance + 1;\r
289 Key = 0;\r
290 do {\r
291 Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, Size);\r
292 if (EFI_ERROR (Status)) {\r
293 break;\r
294 }\r
295 IndexFile --;\r
296 } while (IndexFile > 0);\r
b0d803fe 297\r
682cee4c
LG
298 //\r
299 // Fv File with the required FV file type is found.\r
300 // Search the section file in the found FV file.\r
301 //\r
302 if (IndexFile == 0) {\r
303 Status = InternalGetSectionFromFv (\r
304 HandleBuffer[IndexFv], \r
305 &NameGuid,\r
306 SectionType,\r
307 SectionInstance,\r
308 Buffer,\r
309 Size\r
310 );\r
311\r
312 if (!EFI_ERROR (Status)) {\r
313 goto Done;\r
314 }\r
315 }\r
316 }\r
317\r
318 //\r
319 // The required FFS section file is not found. \r
320 //\r
321 if (IndexFv == HandleCount) {\r
322 Status = EFI_NOT_FOUND;\r
323 }\r
324\r
325Done:\r
326 if (HandleBuffer != NULL) { \r
0433d8f0
LG
327 //\r
328 // HandleBuffer is allocated by gBS AllocatePool() service. \r
329 // So, gBS FreePool() service is used to free HandleBuffer.\r
330 //\r
331 gBS->FreePool (HandleBuffer);\r
682cee4c
LG
332 }\r
333\r
334 return Status;\r
335}\r
b0d803fe 336\r
f80b0830 337/**\r
b75a165d
LG
338 Searches all the availables firmware volumes and returns the first matching FFS section. \r
339\r
340 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid. \r
341 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search \r
342 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances \r
343 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer. \r
344 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
345 It is the caller's responsibility to use FreePool() to free the allocated buffer. \r
346 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections \r
347 are retrieved from an FFS file based on SectionType and SectionInstance.\r
348\r
349 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
350 the search will be retried with a section type of EFI_SECTION_PE32.\r
351 This function must be called with a TPL <= TPL_NOTIFY.\r
352\r
353 If NameGuid is NULL, then ASSERT().\r
354 If Buffer is NULL, then ASSERT().\r
ff197efb 355 If Size is NULL, then ASSERT().\r
356\r
f80b0830 357\r
58380e9c 358 @param NameGuid A pointer to to the FFS filename GUID to search for \r
359 within any of the firmware volumes in the platform. \r
360 @param SectionType Indicates the FFS section type to search for within \r
361 the FFS file specified by NameGuid.\r
362 @param SectionInstance Indicates which section instance within the FFS file \r
363 specified by NameGuid to retrieve.\r
364 @param Buffer On output, a pointer to a callee allocated buffer \r
365 containing the FFS file section that was found. \r
366 Is it the caller's responsibility to free this buffer \r
367 using FreePool().\r
b75a165d
LG
368 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
369\r
370 @retval EFI_SUCCESS The specified FFS section was returned.\r
371 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
58380e9c 372 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to \r
373 retrieve the matching FFS section.\r
374 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a \r
375 device error.\r
376 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the \r
377 firmware volume that \r
b75a165d 378 contains the matching FFS section does not allow reads.\r
f80b0830 379**/\r
b0d803fe 380EFI_STATUS\r
381EFIAPI\r
eb9dd4d0 382GetSectionFromAnyFv (\r
b0d803fe 383 IN CONST EFI_GUID *NameGuid,\r
384 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 385 IN UINTN SectionInstance,\r
b0d803fe 386 OUT VOID **Buffer,\r
387 OUT UINTN *Size\r
388 )\r
389{\r
390 EFI_STATUS Status;\r
391 EFI_HANDLE *HandleBuffer;\r
392 UINTN HandleCount;\r
393 UINTN Index;\r
394 EFI_HANDLE FvHandle;\r
b0d803fe 395\r
396 //\r
397 // Search the FV that contain the caller's FFS first.\r
398 // FV builder can choose to build FFS into the this FV\r
399 // so that this implementation of GetSectionFromAnyFv\r
400 // will locate the FFS faster.\r
401 //\r
166152e8 402 FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
eb9dd4d0 403 Status = InternalGetSectionFromFv (\r
b0d803fe 404 FvHandle,\r
405 NameGuid,\r
406 SectionType,\r
ff197efb 407 SectionInstance,\r
b0d803fe 408 Buffer,\r
409 Size\r
410 );\r
411 if (!EFI_ERROR (Status)) {\r
412 return EFI_SUCCESS;\r
413 }\r
414\r
b0d803fe 415 HandleBuffer = NULL;\r
416 Status = gBS->LocateHandleBuffer (\r
417 ByProtocol,\r
418 &gEfiFirmwareVolume2ProtocolGuid,\r
419 NULL,\r
420 &HandleCount,\r
421 &HandleBuffer\r
422 );\r
423 if (EFI_ERROR (Status)) {\r
424 goto Done;\r
425 }\r
426\r
ff197efb 427 for (Index = 0; Index < HandleCount; Index++) {\r
b0d803fe 428 //\r
429 // Skip the FV that contain the caller's FFS\r
430 //\r
ff197efb 431 if (HandleBuffer[Index] != FvHandle) {\r
eb9dd4d0 432 Status = InternalGetSectionFromFv (\r
ff197efb 433 HandleBuffer[Index], \r
434 NameGuid, \r
435 SectionType, \r
436 SectionInstance,\r
437 Buffer, \r
438 Size\r
439 );\r
440\r
441 if (!EFI_ERROR (Status)) {\r
442 goto Done;\r
443 }\r
b0d803fe 444 }\r
445\r
b0d803fe 446 }\r
447\r
448 if (Index == HandleCount) {\r
449 Status = EFI_NOT_FOUND;\r
450 }\r
451\r
452Done:\r
453 \r
0433d8f0
LG
454 if (HandleBuffer != NULL) {\r
455 //\r
456 // HandleBuffer is allocated by gBS AllocatePool() service. \r
457 // So, gBS FreePool() service is used to free HandleBuffer.\r
458 //\r
459 gBS->FreePool (HandleBuffer);\r
b0d803fe 460 }\r
461 return Status;\r
462 \r
463}\r
464\r
f80b0830 465/**\r
b75a165d
LG
466 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section. \r
467\r
468 This function searches the firmware volume that the currently executing module was loaded \r
469 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search \r
470 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance \r
471 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.\r
472 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
473 It is the caller's responsibility to use FreePool() to free the allocated buffer. \r
474 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from \r
475 an FFS file based on SectionType and SectionInstance.\r
476\r
477 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.\r
478 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
479 the search will be retried with a section type of EFI_SECTION_PE32.\r
480 \r
481 This function must be called with a TPL <= TPL_NOTIFY.\r
482 If NameGuid is NULL, then ASSERT().\r
483 If Buffer is NULL, then ASSERT().\r
ff197efb 484 If Size is NULL, then ASSERT().\r
485\r
58380e9c 486 @param NameGuid A pointer to to the FFS filename GUID to search for \r
487 within the firmware volumes that the currently \r
488 executing module was loaded from.\r
489 @param SectionType Indicates the FFS section type to search for within \r
490 the FFS file specified by NameGuid.\r
491 @param SectionInstance Indicates which section instance within the FFS file \r
492 specified by NameGuid to retrieve.\r
493 @param Buffer On output, a pointer to a callee allocated buffer \r
494 containing the FFS file section that was found. \r
495 Is it the caller's responsibility to free this buffer \r
496 using FreePool().\r
b75a165d
LG
497 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
498\r
499\r
500 @retval EFI_SUCCESS The specified FFS section was returned.\r
501 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
58380e9c 502 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve \r
503 the matching FFS section.\r
504 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a \r
505 device error.\r
506 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the \r
507 firmware volume that contains the matching FFS \r
508 section does not allow reads. \r
f80b0830 509**/\r
b0d803fe 510EFI_STATUS\r
511EFIAPI\r
eb9dd4d0 512GetSectionFromFv (\r
b0d803fe 513 IN CONST EFI_GUID *NameGuid,\r
514 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 515 IN UINTN SectionInstance,\r
b0d803fe 516 OUT VOID **Buffer,\r
517 OUT UINTN *Size\r
518 )\r
519{\r
eb9dd4d0 520 return InternalGetSectionFromFv (\r
521 InternalImageHandleToFvHandle(gImageHandle),\r
522 NameGuid,\r
523 SectionType,\r
524 SectionInstance,\r
525 Buffer,\r
526 Size\r
527 );\r
b0d803fe 528}\r
529\r
530\r
f80b0830 531/**\r
b75a165d
LG
532 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.\r
533\r
534 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.\r
535 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType, \r
536 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(), \r
537 and the size of the allocated buffer is returned in Size. It is the caller's responsibility \r
538 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for \r
539 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.\r
540\r
541 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.\r
542 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
543 the search will be retried with a section type of EFI_SECTION_PE32.\r
544 This function must be called with a TPL <= TPL_NOTIFY.\r
545 \r
546 If Buffer is NULL, then ASSERT().\r
ff197efb 547 If Size is NULL, then ASSERT().\r
548\r
b75a165d 549\r
58380e9c 550 @param SectionType Indicates the FFS section type to search for within \r
551 the FFS file that the currently executing module \r
552 was loaded from.\r
553 @param SectionInstance Indicates which section instance to retrieve within \r
554 the FFS file that the currently executing module \r
555 was loaded from.\r
556 @param Buffer On output, a pointer to a callee allocated buffer \r
557 containing the FFS file section that was found. \r
558 Is it the caller's responsibility to free this buffer \r
559 using FreePool().\r
b75a165d
LG
560 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
561\r
562 @retval EFI_SUCCESS The specified FFS section was returned.\r
563 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
58380e9c 564 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve \r
565 the matching FFS section.\r
566 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a \r
567 device error.\r
568 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the \r
569 firmware volume that contains the matching FFS \r
570 section does not allow reads. \r
f80b0830 571 \r
572**/\r
b0d803fe 573EFI_STATUS\r
574EFIAPI\r
eb9dd4d0 575GetSectionFromFfs (\r
b0d803fe 576 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 577 IN UINTN SectionInstance,\r
b0d803fe 578 OUT VOID **Buffer,\r
579 OUT UINTN *Size\r
580 )\r
581{\r
eb9dd4d0 582 return InternalGetSectionFromFv(\r
583 InternalImageHandleToFvHandle(gImageHandle),\r
584 &gEfiCallerIdGuid,\r
585 SectionType,\r
586 SectionInstance,\r
587 Buffer,\r
588 Size\r
589 );\r
b0d803fe 590}\r
591\r
84716614
LG
592\r
593/**\r
594 Get the image file buffer data and buffer size by its device path. \r
595 \r
ea6898b9 596 Access the file either from a firmware volume, from a file system interface, \r
84716614
LG
597 or from the load file interface.\r
598 \r
599 Allocate memory to store the found image. The caller is responsible to free memory.\r
600\r
601 If File is NULL, then NULL is returned.\r
602 If FileSize is NULL, then NULL is returned.\r
603 If AuthenticationStatus is NULL, then NULL is returned.\r
604\r
58380e9c 605 @param[in] BootPolicy Policy for Open Image File.If TRUE, indicates \r
606 that the request originates from the boot \r
607 manager, and that the boot manager is\r
608 attempting to load FilePath as a boot\r
609 selection. If FALSE, then FilePath must \r
610 match an exact file to be loaded.\r
611 @param[in] FilePath The pointer to the device path of the file\r
612 that is absracted to the file buffer.\r
613 @param[out] FileSize The pointer to the size of the abstracted \r
614 file buffer.\r
615 @param[out] AuthenticationStatus The pointer to a caller-allocated UINT32 \r
616 in which the authentication status is returned.\r
617\r
618 @retval NULL File is NULL, or FileSize is NULL. Or, the file can't be found.\r
84716614
LG
619 @retval other The abstracted file buffer. The caller is responsible to free memory.\r
620**/\r
621VOID *\r
622EFIAPI\r
623GetFileBufferByFilePath (\r
624 IN BOOLEAN BootPolicy,\r
625 IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
626 OUT UINTN *FileSize,\r
627 OUT UINT32 *AuthenticationStatus\r
628 )\r
629{\r
630 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
631 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;\r
95dd343a 632 EFI_DEVICE_PATH_PROTOCOL *TempDevicePathNode;\r
84716614
LG
633 EFI_HANDLE Handle;\r
634 EFI_GUID *FvNameGuid;\r
635 EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVol;\r
636 EFI_SECTION_TYPE SectionType;\r
637 UINT8 *ImageBuffer;\r
0433d8f0 638 UINT8 *TempBuffer;\r
84716614
LG
639 UINTN ImageBufferSize;\r
640 EFI_FV_FILETYPE Type;\r
641 EFI_FV_FILE_ATTRIBUTES Attrib;\r
642 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
643 EFI_FILE_HANDLE FileHandle;\r
644 EFI_FILE_HANDLE LastHandle;\r
645 EFI_FILE_INFO *FileInfo;\r
646 UINTN FileInfoSize;\r
647 EFI_LOAD_FILE_PROTOCOL *LoadFile;\r
648 EFI_LOAD_FILE2_PROTOCOL *LoadFile2;\r
649 EFI_STATUS Status;\r
650\r
651 //\r
652 // Check input File device path.\r
653 //\r
654 if (FilePath == NULL || FileSize == NULL || AuthenticationStatus == NULL) {\r
655 return NULL;\r
656 }\r
657\r
658 //\r
659 // Init local variable\r
660 //\r
95dd343a 661 TempDevicePathNode = NULL;\r
84716614
LG
662 FvNameGuid = NULL;\r
663 FileInfo = NULL;\r
664 FileHandle = NULL;\r
665 ImageBuffer = NULL;\r
0433d8f0 666 TempBuffer = NULL;\r
84716614
LG
667 ImageBufferSize = 0;\r
668 *AuthenticationStatus = 0;\r
669 \r
670 //\r
671 // Copy File Device Path\r
672 //\r
673 OrigDevicePathNode = DuplicateDevicePath (FilePath);\r
674 if (OrigDevicePathNode == NULL) {\r
675 return NULL;\r
676 }\r
677\r
678 //\r
679 // Check whether this device path support FV2 protocol.\r
680 // Is so, this device path may contain a Image.\r
681 //\r
682 DevicePathNode = OrigDevicePathNode;\r
683 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePathNode, &Handle);\r
684 if (!EFI_ERROR (Status)) {\r
685 //\r
686 // For FwVol File system there is only a single file name that is a GUID.\r
687 //\r
688 FvNameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePathNode);\r
689 if (FvNameGuid == NULL) {\r
690 Status = EFI_INVALID_PARAMETER;\r
691 } else {\r
692 //\r
693 // Read image from the firmware file\r
694 //\r
695 Status = gBS->HandleProtocol (Handle, &gEfiFirmwareVolume2ProtocolGuid, (VOID**)&FwVol);\r
696 if (!EFI_ERROR (Status)) {\r
697 SectionType = EFI_SECTION_PE32;\r
698 ImageBuffer = NULL;\r
699 Status = FwVol->ReadSection (\r
700 FwVol,\r
701 FvNameGuid,\r
702 SectionType,\r
703 0,\r
704 (VOID **)&ImageBuffer,\r
705 &ImageBufferSize,\r
706 AuthenticationStatus\r
707 );\r
708 if (EFI_ERROR (Status)) {\r
709 //\r
710 // Try a raw file, since a PE32 SECTION does not exist\r
711 //\r
712 if (ImageBuffer != NULL) {\r
713 FreePool (ImageBuffer);\r
714 *AuthenticationStatus = 0;\r
715 }\r
716 ImageBuffer = NULL;\r
717 Status = FwVol->ReadFile (\r
718 FwVol,\r
719 FvNameGuid,\r
720 (VOID **)&ImageBuffer,\r
721 &ImageBufferSize,\r
722 &Type,\r
723 &Attrib,\r
724 AuthenticationStatus\r
725 );\r
726 }\r
0433d8f0
LG
727 if (!EFI_ERROR (Status)) {\r
728 //\r
729 // The found buffer by FV protocol is allocated by gBS AllocatePool() service. \r
730 // Copy the found buffer to the allocated buffer by AllocatePool().\r
731 // Then, this returned buffer can be freed by FreePool().\r
732 //\r
733 TempBuffer = AllocateCopyPool (ImageBufferSize, ImageBuffer);\r
734 gBS->FreePool (ImageBuffer);\r
735 ImageBuffer = TempBuffer;\r
736 }\r
84716614
LG
737 }\r
738 }\r
739 goto Finish;\r
740 }\r
741\r
742 //\r
743 // Attempt to access the file via a file system interface\r
744 //\r
745 DevicePathNode = OrigDevicePathNode;\r
746 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathNode, &Handle);\r
747 if (!EFI_ERROR (Status)) {\r
748 Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID**)&Volume);\r
749 if (!EFI_ERROR (Status)) {\r
750 //\r
751 // Open the Volume to get the File System handle\r
752 //\r
753 Status = Volume->OpenVolume (Volume, &FileHandle);\r
754 if (!EFI_ERROR (Status)) {\r
95dd343a
LG
755 //\r
756 // Duplicate the device path to avoid the access to unaligned device path node.\r
757 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r
758 // nodes, It assures the fields in device path nodes are 2 byte aligned.\r
759 //\r
760 TempDevicePathNode = DuplicateDevicePath (DevicePathNode);\r
761 if (TempDevicePathNode == NULL) {\r
762 FileHandle->Close (FileHandle);\r
763 Status = EFI_OUT_OF_RESOURCES;\r
764 goto Finish;\r
765 }\r
84716614
LG
766 //\r
767 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
768 // directory information and filename can be seperate. The goal is to inch\r
769 // our way down each device path node and close the previous node\r
770 //\r
95dd343a 771 DevicePathNode = TempDevicePathNode;\r
84716614
LG
772 while (!IsDevicePathEnd (DevicePathNode) && !EFI_ERROR (Status)) {\r
773 if (DevicePathType (DevicePathNode) != MEDIA_DEVICE_PATH ||\r
774 DevicePathSubType (DevicePathNode) != MEDIA_FILEPATH_DP) {\r
775 Status = EFI_UNSUPPORTED;\r
776 break;\r
777 }\r
778 \r
779 LastHandle = FileHandle;\r
780 FileHandle = NULL;\r
781 \r
782 Status = LastHandle->Open (\r
783 LastHandle,\r
784 &FileHandle,\r
785 ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName,\r
786 EFI_FILE_MODE_READ,\r
787 0\r
788 );\r
789 \r
790 //\r
791 // Close the previous node\r
792 //\r
793 LastHandle->Close (LastHandle);\r
794 \r
795 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
796 }\r
797 \r
798 if (!EFI_ERROR (Status)) {\r
799 //\r
800 // We have found the file. Now we need to read it. Before we can read the file we need to\r
801 // figure out how big the file is.\r
802 //\r
803 FileInfo = NULL;\r
804 FileInfoSize = 0;\r
805 Status = FileHandle->GetInfo (\r
806 FileHandle,\r
807 &gEfiFileInfoGuid,\r
808 &FileInfoSize,\r
809 FileInfo\r
810 );\r
811 \r
812 if (Status == EFI_BUFFER_TOO_SMALL) {\r
813 FileInfo = AllocatePool (FileInfoSize);\r
814 if (FileInfo == NULL) {\r
815 Status = EFI_OUT_OF_RESOURCES;\r
816 } else {\r
817 Status = FileHandle->GetInfo (\r
818 FileHandle,\r
819 &gEfiFileInfoGuid,\r
820 &FileInfoSize,\r
821 FileInfo\r
822 );\r
823 }\r
824 }\r
825 \r
e191bb14 826 if (!EFI_ERROR (Status) && (FileInfo != NULL)) {\r
84716614
LG
827 //\r
828 // Allocate space for the file\r
829 //\r
830 ImageBuffer = AllocatePool ((UINTN)FileInfo->FileSize);\r
831 if (ImageBuffer == NULL) {\r
832 Status = EFI_OUT_OF_RESOURCES;\r
833 } else {\r
834 //\r
835 // Read the file into the buffer we allocated\r
836 //\r
837 ImageBufferSize = (UINTN)FileInfo->FileSize;\r
838 Status = FileHandle->Read (FileHandle, &ImageBufferSize, ImageBuffer);\r
839 }\r
840 }\r
841 }\r
842 //\r
95dd343a 843 // Close the file and Free FileInfo and TempDevicePathNode since we are done\r
84716614
LG
844 // \r
845 if (FileInfo != NULL) {\r
846 FreePool (FileInfo);\r
847 }\r
848 if (FileHandle != NULL) {\r
849 FileHandle->Close (FileHandle);\r
850 }\r
95dd343a 851 FreePool (TempDevicePathNode);\r
84716614
LG
852 }\r
853 }\r
854 goto Finish;\r
855 }\r
856\r
857 //\r
858 // Attempt to access the file via LoadFile2 interface\r
859 //\r
860 if (!BootPolicy) {\r
861 DevicePathNode = OrigDevicePathNode;\r
862 Status = gBS->LocateDevicePath (&gEfiLoadFile2ProtocolGuid, &DevicePathNode, &Handle);\r
863 if (!EFI_ERROR (Status)) {\r
864 Status = gBS->HandleProtocol (Handle, &gEfiLoadFile2ProtocolGuid, (VOID**)&LoadFile2);\r
865 if (!EFI_ERROR (Status)) {\r
866 //\r
867 // Call LoadFile2 with the correct buffer size\r
868 //\r
869 ImageBufferSize = 0;\r
870 ImageBuffer = NULL;\r
871 Status = LoadFile2->LoadFile (\r
872 LoadFile2,\r
873 DevicePathNode,\r
874 FALSE,\r
875 &ImageBufferSize,\r
876 ImageBuffer\r
877 );\r
878 if (Status == EFI_BUFFER_TOO_SMALL) {\r
879 ImageBuffer = AllocatePool (ImageBufferSize);\r
880 if (ImageBuffer == NULL) {\r
881 Status = EFI_OUT_OF_RESOURCES;\r
882 } else {\r
883 Status = LoadFile2->LoadFile (\r
884 LoadFile2,\r
885 DevicePathNode,\r
c083f7ca 886 FALSE,\r
84716614
LG
887 &ImageBufferSize,\r
888 ImageBuffer\r
889 );\r
890 }\r
891 }\r
892 }\r
893 goto Finish;\r
894 }\r
895 }\r
896\r
897 //\r
898 // Attempt to access the file via LoadFile interface\r
899 //\r
900 DevicePathNode = OrigDevicePathNode;\r
901 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &DevicePathNode, &Handle);\r
902 if (!EFI_ERROR (Status)) {\r
903 Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID**)&LoadFile);\r
904 if (!EFI_ERROR (Status)) {\r
905 //\r
906 // Call LoadFile with the correct buffer size\r
907 //\r
908 ImageBufferSize = 0;\r
909 ImageBuffer = NULL;\r
910 Status = LoadFile->LoadFile (\r
911 LoadFile,\r
912 DevicePathNode,\r
913 BootPolicy,\r
914 &ImageBufferSize,\r
915 ImageBuffer\r
916 );\r
917 if (Status == EFI_BUFFER_TOO_SMALL) {\r
918 ImageBuffer = AllocatePool (ImageBufferSize);\r
919 if (ImageBuffer == NULL) {\r
920 Status = EFI_OUT_OF_RESOURCES;\r
921 } else {\r
922 Status = LoadFile->LoadFile (\r
923 LoadFile,\r
924 DevicePathNode,\r
925 BootPolicy,\r
926 &ImageBufferSize,\r
927 ImageBuffer\r
928 );\r
929 }\r
930 }\r
931 }\r
932 }\r
933\r
934Finish:\r
935\r
936 if (EFI_ERROR (Status)) {\r
937 if (ImageBuffer != NULL) {\r
938 FreePool (ImageBuffer);\r
939 ImageBuffer = NULL;\r
940 }\r
941 *FileSize = 0;\r
942 } else {\r
943 *FileSize = ImageBufferSize;\r
944 }\r
945\r
946 FreePool (OrigDevicePathNode);\r
947\r
948 return ImageBuffer;\r
949}\r