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