]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/DxeServicesLib/DxeServicesLib.c
Minor grammatical work--mostly adding periods. Items with ONLY period added did...
[mirror_edk2.git] / MdePkg / Library / DxeServicesLib / DxeServicesLib.c
... / ...
CommitLineData
1/** @file\r
2 MDE DXE Services Library provides functions that simplify the development of DXE Drivers. \r
3 These functions help access data from sections of FFS files or from file path.\r
4\r
5 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
6 This program and the accompanying materials\r
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
9 http://opensource.org/licenses/bsd-license.php.\r
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
20#include <Library/DevicePathLib.h>\r
21#include <Library/UefiLib.h>\r
22#include <Library/DxeServicesLib.h>\r
23#include <Protocol/FirmwareVolume2.h>\r
24#include <Protocol/LoadedImage.h>\r
25#include <Protocol/LoadFile2.h>\r
26#include <Protocol/LoadFile.h>\r
27#include <Protocol/SimpleFileSystem.h>\r
28#include <Guid/FileInfo.h>\r
29\r
30/**\r
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
34\r
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
42\r
43 @retval EFI_HANDLE The device handle from which the Image is loaded from.\r
44\r
45**/\r
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
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
82 \r
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
85 \r
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
91 @param FvHandle The device handle that contains a instance of \r
92 EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
93 @param NameGuid The GUID name of a Firmware File.\r
94 @param SectionType The Firmware Section type.\r
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
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
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
110 \r
111**/\r
112EFI_STATUS\r
113InternalGetSectionFromFv (\r
114 IN EFI_HANDLE FvHandle,\r
115 IN CONST EFI_GUID *NameGuid,\r
116 IN EFI_SECTION_TYPE SectionType,\r
117 IN UINTN SectionInstance,\r
118 OUT VOID **Buffer,\r
119 OUT UINTN *Size\r
120 )\r
121{\r
122 EFI_STATUS Status;\r
123 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
124 UINT32 AuthenticationStatus;\r
125\r
126 ASSERT (NameGuid != NULL);\r
127 ASSERT (Buffer != NULL);\r
128 ASSERT (Size != NULL);\r
129 \r
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
138 return EFI_NOT_FOUND;\r
139 }\r
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
150 SectionInstance,\r
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
158 // Try reading PE32 section, if the required section is TE type \r
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
166 SectionInstance,\r
167 Buffer,\r
168 Size,\r
169 &AuthenticationStatus\r
170 );\r
171 }\r
172\r
173 return Status;\r
174}\r
175\r
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
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
201 FileInstance starts from zero.\r
202 @param SectionType Indicates the FFS section type to search for \r
203 within the FFS file \r
204 specified by FileType with FileInstance.\r
205 @param SectionInstance Indicates which section instance within the FFS file \r
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
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
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
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
285\r
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
315 FreePool(HandleBuffer);\r
316 }\r
317\r
318 return Status;\r
319}\r
320\r
321/**\r
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
339 If Size is NULL, then ASSERT().\r
340\r
341\r
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
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
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
362 contains the matching FFS section does not allow reads.\r
363**/\r
364EFI_STATUS\r
365EFIAPI\r
366GetSectionFromAnyFv (\r
367 IN CONST EFI_GUID *NameGuid,\r
368 IN EFI_SECTION_TYPE SectionType,\r
369 IN UINTN SectionInstance,\r
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
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
386 FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
387 Status = InternalGetSectionFromFv (\r
388 FvHandle,\r
389 NameGuid,\r
390 SectionType,\r
391 SectionInstance,\r
392 Buffer,\r
393 Size\r
394 );\r
395 if (!EFI_ERROR (Status)) {\r
396 return EFI_SUCCESS;\r
397 }\r
398\r
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
411 for (Index = 0; Index < HandleCount; Index++) {\r
412 //\r
413 // Skip the FV that contain the caller's FFS\r
414 //\r
415 if (HandleBuffer[Index] != FvHandle) {\r
416 Status = InternalGetSectionFromFv (\r
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
428 }\r
429\r
430 }\r
431\r
432 if (Index == HandleCount) {\r
433 Status = EFI_NOT_FOUND;\r
434 }\r
435\r
436Done:\r
437 \r
438 if (HandleBuffer != NULL) { \r
439 FreePool(HandleBuffer);\r
440 }\r
441 return Status;\r
442 \r
443}\r
444\r
445/**\r
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
464 If Size is NULL, then ASSERT().\r
465\r
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
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
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
489**/\r
490EFI_STATUS\r
491EFIAPI\r
492GetSectionFromFv (\r
493 IN CONST EFI_GUID *NameGuid,\r
494 IN EFI_SECTION_TYPE SectionType,\r
495 IN UINTN SectionInstance,\r
496 OUT VOID **Buffer,\r
497 OUT UINTN *Size\r
498 )\r
499{\r
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
508}\r
509\r
510\r
511/**\r
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
527 If Size is NULL, then ASSERT().\r
528\r
529\r
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
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
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
551 \r
552**/\r
553EFI_STATUS\r
554EFIAPI\r
555GetSectionFromFfs (\r
556 IN EFI_SECTION_TYPE SectionType,\r
557 IN UINTN SectionInstance,\r
558 OUT VOID **Buffer,\r
559 OUT UINTN *Size\r
560 )\r
561{\r
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
570}\r
571\r
572\r
573/**\r
574 Get the image file buffer data and buffer size by its device path. \r
575 \r
576 Access the file either from a firmware volume, from a file system interface, \r
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
581 If File is NULL, then NULL is returned.\r
582 If FileSize is NULL, then NULL is returned.\r
583 If AuthenticationStatus is NULL, then NULL is returned.\r
584\r
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
595 @param[out] AuthenticationStatus The pointer to a caller-allocated UINT32 \r
596 in which the authentication status is returned.\r
597\r
598 @retval NULL File is NULL, or FileSize is NULL. Or, the file can't be found.\r
599 @retval other The abstracted file buffer. The caller is responsible to free memory.\r
600**/\r
601VOID *\r
602EFIAPI\r
603GetFileBufferByFilePath (\r
604 IN BOOLEAN BootPolicy,\r
605 IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
606 OUT UINTN *FileSize,\r
607 OUT UINT32 *AuthenticationStatus\r
608 )\r
609{\r
610 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
611 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;\r
612 EFI_DEVICE_PATH_PROTOCOL *TempDevicePathNode;\r
613 EFI_HANDLE Handle;\r
614 EFI_GUID *FvNameGuid;\r
615 EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVol;\r
616 EFI_SECTION_TYPE SectionType;\r
617 UINT8 *ImageBuffer;\r
618 UINTN ImageBufferSize;\r
619 EFI_FV_FILETYPE Type;\r
620 EFI_FV_FILE_ATTRIBUTES Attrib;\r
621 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
622 EFI_FILE_HANDLE FileHandle;\r
623 EFI_FILE_HANDLE LastHandle;\r
624 EFI_FILE_INFO *FileInfo;\r
625 UINTN FileInfoSize;\r
626 EFI_LOAD_FILE_PROTOCOL *LoadFile;\r
627 EFI_LOAD_FILE2_PROTOCOL *LoadFile2;\r
628 EFI_STATUS Status;\r
629\r
630 //\r
631 // Check input File device path.\r
632 //\r
633 if (FilePath == NULL || FileSize == NULL || AuthenticationStatus == NULL) {\r
634 return NULL;\r
635 }\r
636\r
637 //\r
638 // Init local variable\r
639 //\r
640 TempDevicePathNode = NULL;\r
641 FvNameGuid = NULL;\r
642 FileInfo = NULL;\r
643 FileHandle = NULL;\r
644 ImageBuffer = NULL;\r
645 ImageBufferSize = 0;\r
646 *AuthenticationStatus = 0;\r
647 \r
648 //\r
649 // Copy File Device Path\r
650 //\r
651 OrigDevicePathNode = DuplicateDevicePath (FilePath);\r
652 if (OrigDevicePathNode == NULL) {\r
653 return NULL;\r
654 }\r
655\r
656 //\r
657 // Check whether this device path support FV2 protocol.\r
658 // Is so, this device path may contain a Image.\r
659 //\r
660 DevicePathNode = OrigDevicePathNode;\r
661 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePathNode, &Handle);\r
662 if (!EFI_ERROR (Status)) {\r
663 //\r
664 // For FwVol File system there is only a single file name that is a GUID.\r
665 //\r
666 FvNameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePathNode);\r
667 if (FvNameGuid == NULL) {\r
668 Status = EFI_INVALID_PARAMETER;\r
669 } else {\r
670 //\r
671 // Read image from the firmware file\r
672 //\r
673 Status = gBS->HandleProtocol (Handle, &gEfiFirmwareVolume2ProtocolGuid, (VOID**)&FwVol);\r
674 if (!EFI_ERROR (Status)) {\r
675 SectionType = EFI_SECTION_PE32;\r
676 ImageBuffer = NULL;\r
677 Status = FwVol->ReadSection (\r
678 FwVol,\r
679 FvNameGuid,\r
680 SectionType,\r
681 0,\r
682 (VOID **)&ImageBuffer,\r
683 &ImageBufferSize,\r
684 AuthenticationStatus\r
685 );\r
686 if (EFI_ERROR (Status)) {\r
687 //\r
688 // Try a raw file, since a PE32 SECTION does not exist\r
689 //\r
690 if (ImageBuffer != NULL) {\r
691 FreePool (ImageBuffer);\r
692 *AuthenticationStatus = 0;\r
693 }\r
694 ImageBuffer = NULL;\r
695 Status = FwVol->ReadFile (\r
696 FwVol,\r
697 FvNameGuid,\r
698 (VOID **)&ImageBuffer,\r
699 &ImageBufferSize,\r
700 &Type,\r
701 &Attrib,\r
702 AuthenticationStatus\r
703 );\r
704 }\r
705 }\r
706 }\r
707 goto Finish;\r
708 }\r
709\r
710 //\r
711 // Attempt to access the file via a file system interface\r
712 //\r
713 DevicePathNode = OrigDevicePathNode;\r
714 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathNode, &Handle);\r
715 if (!EFI_ERROR (Status)) {\r
716 Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID**)&Volume);\r
717 if (!EFI_ERROR (Status)) {\r
718 //\r
719 // Open the Volume to get the File System handle\r
720 //\r
721 Status = Volume->OpenVolume (Volume, &FileHandle);\r
722 if (!EFI_ERROR (Status)) {\r
723 //\r
724 // Duplicate the device path to avoid the access to unaligned device path node.\r
725 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r
726 // nodes, It assures the fields in device path nodes are 2 byte aligned.\r
727 //\r
728 TempDevicePathNode = DuplicateDevicePath (DevicePathNode);\r
729 if (TempDevicePathNode == NULL) {\r
730 FileHandle->Close (FileHandle);\r
731 Status = EFI_OUT_OF_RESOURCES;\r
732 goto Finish;\r
733 }\r
734 //\r
735 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
736 // directory information and filename can be seperate. The goal is to inch\r
737 // our way down each device path node and close the previous node\r
738 //\r
739 DevicePathNode = TempDevicePathNode;\r
740 while (!IsDevicePathEnd (DevicePathNode) && !EFI_ERROR (Status)) {\r
741 if (DevicePathType (DevicePathNode) != MEDIA_DEVICE_PATH ||\r
742 DevicePathSubType (DevicePathNode) != MEDIA_FILEPATH_DP) {\r
743 Status = EFI_UNSUPPORTED;\r
744 break;\r
745 }\r
746 \r
747 LastHandle = FileHandle;\r
748 FileHandle = NULL;\r
749 \r
750 Status = LastHandle->Open (\r
751 LastHandle,\r
752 &FileHandle,\r
753 ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName,\r
754 EFI_FILE_MODE_READ,\r
755 0\r
756 );\r
757 \r
758 //\r
759 // Close the previous node\r
760 //\r
761 LastHandle->Close (LastHandle);\r
762 \r
763 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
764 }\r
765 \r
766 if (!EFI_ERROR (Status)) {\r
767 //\r
768 // We have found the file. Now we need to read it. Before we can read the file we need to\r
769 // figure out how big the file is.\r
770 //\r
771 FileInfo = NULL;\r
772 FileInfoSize = 0;\r
773 Status = FileHandle->GetInfo (\r
774 FileHandle,\r
775 &gEfiFileInfoGuid,\r
776 &FileInfoSize,\r
777 FileInfo\r
778 );\r
779 \r
780 if (Status == EFI_BUFFER_TOO_SMALL) {\r
781 FileInfo = AllocatePool (FileInfoSize);\r
782 if (FileInfo == NULL) {\r
783 Status = EFI_OUT_OF_RESOURCES;\r
784 } else {\r
785 Status = FileHandle->GetInfo (\r
786 FileHandle,\r
787 &gEfiFileInfoGuid,\r
788 &FileInfoSize,\r
789 FileInfo\r
790 );\r
791 }\r
792 }\r
793 \r
794 if (!EFI_ERROR (Status) && (FileInfo != NULL)) {\r
795 //\r
796 // Allocate space for the file\r
797 //\r
798 ImageBuffer = AllocatePool ((UINTN)FileInfo->FileSize);\r
799 if (ImageBuffer == NULL) {\r
800 Status = EFI_OUT_OF_RESOURCES;\r
801 } else {\r
802 //\r
803 // Read the file into the buffer we allocated\r
804 //\r
805 ImageBufferSize = (UINTN)FileInfo->FileSize;\r
806 Status = FileHandle->Read (FileHandle, &ImageBufferSize, ImageBuffer);\r
807 }\r
808 }\r
809 }\r
810 //\r
811 // Close the file and Free FileInfo and TempDevicePathNode since we are done\r
812 // \r
813 if (FileInfo != NULL) {\r
814 FreePool (FileInfo);\r
815 }\r
816 if (FileHandle != NULL) {\r
817 FileHandle->Close (FileHandle);\r
818 }\r
819 FreePool (TempDevicePathNode);\r
820 }\r
821 }\r
822 goto Finish;\r
823 }\r
824\r
825 //\r
826 // Attempt to access the file via LoadFile2 interface\r
827 //\r
828 if (!BootPolicy) {\r
829 DevicePathNode = OrigDevicePathNode;\r
830 Status = gBS->LocateDevicePath (&gEfiLoadFile2ProtocolGuid, &DevicePathNode, &Handle);\r
831 if (!EFI_ERROR (Status)) {\r
832 Status = gBS->HandleProtocol (Handle, &gEfiLoadFile2ProtocolGuid, (VOID**)&LoadFile2);\r
833 if (!EFI_ERROR (Status)) {\r
834 //\r
835 // Call LoadFile2 with the correct buffer size\r
836 //\r
837 ImageBufferSize = 0;\r
838 ImageBuffer = NULL;\r
839 Status = LoadFile2->LoadFile (\r
840 LoadFile2,\r
841 DevicePathNode,\r
842 FALSE,\r
843 &ImageBufferSize,\r
844 ImageBuffer\r
845 );\r
846 if (Status == EFI_BUFFER_TOO_SMALL) {\r
847 ImageBuffer = AllocatePool (ImageBufferSize);\r
848 if (ImageBuffer == NULL) {\r
849 Status = EFI_OUT_OF_RESOURCES;\r
850 } else {\r
851 Status = LoadFile2->LoadFile (\r
852 LoadFile2,\r
853 DevicePathNode,\r
854 FALSE,\r
855 &ImageBufferSize,\r
856 ImageBuffer\r
857 );\r
858 }\r
859 }\r
860 }\r
861 goto Finish;\r
862 }\r
863 }\r
864\r
865 //\r
866 // Attempt to access the file via LoadFile interface\r
867 //\r
868 DevicePathNode = OrigDevicePathNode;\r
869 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &DevicePathNode, &Handle);\r
870 if (!EFI_ERROR (Status)) {\r
871 Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID**)&LoadFile);\r
872 if (!EFI_ERROR (Status)) {\r
873 //\r
874 // Call LoadFile with the correct buffer size\r
875 //\r
876 ImageBufferSize = 0;\r
877 ImageBuffer = NULL;\r
878 Status = LoadFile->LoadFile (\r
879 LoadFile,\r
880 DevicePathNode,\r
881 BootPolicy,\r
882 &ImageBufferSize,\r
883 ImageBuffer\r
884 );\r
885 if (Status == EFI_BUFFER_TOO_SMALL) {\r
886 ImageBuffer = AllocatePool (ImageBufferSize);\r
887 if (ImageBuffer == NULL) {\r
888 Status = EFI_OUT_OF_RESOURCES;\r
889 } else {\r
890 Status = LoadFile->LoadFile (\r
891 LoadFile,\r
892 DevicePathNode,\r
893 BootPolicy,\r
894 &ImageBufferSize,\r
895 ImageBuffer\r
896 );\r
897 }\r
898 }\r
899 }\r
900 }\r
901\r
902Finish:\r
903\r
904 if (EFI_ERROR (Status)) {\r
905 if (ImageBuffer != NULL) {\r
906 FreePool (ImageBuffer);\r
907 ImageBuffer = NULL;\r
908 }\r
909 *FileSize = 0;\r
910 } else {\r
911 *FileSize = ImageBufferSize;\r
912 }\r
913\r
914 FreePool (OrigDevicePathNode);\r
915\r
916 return ImageBuffer;\r
917}\r