]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/DxeServicesLib/DxeServicesLib.c
MdePkg/Library/Dxe: Fix various typos
[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 - 2018, Intel Corporation. All rights reserved.<BR>\r
6 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9**/\r
10\r
11#include <PiDxe.h>\r
12#include <Library/DebugLib.h>\r
13#include <Library/MemoryAllocationLib.h>\r
14#include <Library/UefiBootServicesTableLib.h>\r
15#include <Library/DevicePathLib.h>\r
16#include <Library/UefiLib.h>\r
17#include <Library/DxeServicesLib.h>\r
18#include <Protocol/FirmwareVolume2.h>\r
19#include <Protocol/LoadedImage.h>\r
20#include <Protocol/LoadFile2.h>\r
21#include <Protocol/LoadFile.h>\r
22#include <Protocol/SimpleFileSystem.h>\r
23#include <Guid/FileInfo.h>\r
24\r
25/**\r
26 Identify the device handle from which the Image is loaded from. As this device handle is passed to\r
27 GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL\r
28 protocol instance should be located successfully by calling gBS->HandleProtocol ().\r
29\r
30 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed\r
31 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.\r
32\r
33 If ImageHandle is NULL, then ASSERT ();\r
34 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();\r
35\r
36 @param ImageHandle The firmware allocated handle for UEFI image.\r
37\r
38 @retval EFI_HANDLE The device handle from which the Image is loaded from.\r
39\r
40**/\r
41EFI_HANDLE\r
42InternalImageHandleToFvHandle (\r
43 EFI_HANDLE ImageHandle\r
44 )\r
45{\r
46 EFI_STATUS Status;\r
47 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
48\r
49 ASSERT (ImageHandle != NULL);\r
50\r
51 Status = gBS->HandleProtocol (\r
52 ImageHandle,\r
53 &gEfiLoadedImageProtocolGuid,\r
54 (VOID **) &LoadedImage\r
55 );\r
56\r
57 ASSERT_EFI_ERROR (Status);\r
58\r
59 //\r
60 // The LoadedImage->DeviceHandle may be NULL.\r
61 // For example for DxeCore, there is LoadedImage protocol installed for it, but the\r
62 // LoadedImage->DeviceHandle could not be initialized before the FV2 (contain DxeCore)\r
63 // protocol is installed.\r
64 //\r
65 return LoadedImage->DeviceHandle;\r
66\r
67}\r
68\r
69/**\r
70 Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware\r
71 Section type and instance number from the specified Firmware Volume.\r
72\r
73 This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to\r
74 carry out the Firmware Volume read operation. The function then reads the Firmware Section found specified\r
75 by NameGuid, SectionType and SectionInstance.\r
76\r
77 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection ()\r
78 found in PI Specification.\r
79\r
80 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section\r
81 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
82 is returned.\r
83\r
84 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated\r
85 by this function. This function can be only called at TPL_NOTIFY and below.\r
86\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 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 if (FvHandle == NULL) {\r
131 //\r
132 // Return EFI_NOT_FOUND directly for NULL FvHandle.\r
133 //\r
134 return EFI_NOT_FOUND;\r
135 }\r
136\r
137 Status = gBS->HandleProtocol (\r
138 FvHandle,\r
139 &gEfiFirmwareVolume2ProtocolGuid,\r
140 (VOID **) &Fv\r
141 );\r
142 if (EFI_ERROR (Status)) {\r
143 return EFI_NOT_FOUND;\r
144 }\r
145\r
146 //\r
147 // Read desired section content in NameGuid file\r
148 //\r
149 *Buffer = NULL;\r
150 *Size = 0;\r
151 Status = Fv->ReadSection (\r
152 Fv,\r
153 NameGuid,\r
154 SectionType,\r
155 SectionInstance,\r
156 Buffer,\r
157 Size,\r
158 &AuthenticationStatus\r
159 );\r
160\r
161 if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r
162 //\r
163 // Try reading PE32 section, if the required section is TE type\r
164 //\r
165 *Buffer = NULL;\r
166 *Size = 0;\r
167 Status = Fv->ReadSection (\r
168 Fv,\r
169 NameGuid,\r
170 EFI_SECTION_PE32,\r
171 SectionInstance,\r
172 Buffer,\r
173 Size,\r
174 &AuthenticationStatus\r
175 );\r
176 }\r
177\r
178 return Status;\r
179}\r
180\r
181/**\r
182 Searches all the available firmware volumes and returns the first matching FFS section.\r
183\r
184 This function searches all the firmware volumes for FFS files with FV file type specified by FileType\r
185 The order that the firmware volumes is searched is not deterministic. For each available FV a search\r
186 is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType,\r
187 the FileInstance instance will be the matched FFS file. For each FFS file found a search\r
188 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances\r
189 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.\r
190 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.\r
191 It is the caller's responsibility to use FreePool() to free the allocated buffer.\r
192 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections\r
193 are retrieved from an FFS file based on SectionType and SectionInstance.\r
194\r
195 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,\r
196 the search will be retried with a section type of EFI_SECTION_PE32.\r
197 This function must be called with a TPL <= TPL_NOTIFY.\r
198\r
199 If Buffer is NULL, then ASSERT().\r
200 If Size is NULL, then ASSERT().\r
201\r
202 @param FileType Indicates the FV file type to search for within all\r
203 available FVs.\r
204 @param FileInstance Indicates which file instance within all available\r
205 FVs specified by FileType.\r
206 FileInstance starts from zero.\r
207 @param SectionType Indicates the FFS section type to search for\r
208 within the FFS file\r
209 specified by FileType with FileInstance.\r
210 @param SectionInstance Indicates which section instance within the FFS file\r
211 specified by FileType with FileInstance to retrieve.\r
212 SectionInstance starts from zero.\r
213 @param Buffer On output, a pointer to a callee allocated buffer\r
214 containing the FFS file section that was found.\r
215 Is it the caller's responsibility to free this\r
216 buffer using FreePool().\r
217 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
218\r
219 @retval EFI_SUCCESS The specified FFS section was returned.\r
220 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
221 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve\r
222 the matching FFS section.\r
223 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a\r
224 device error.\r
225 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because\r
226 the firmware volume that\r
227 contains the matching FFS section does not allow reads.\r
228**/\r
229EFI_STATUS\r
230EFIAPI\r
231GetSectionFromAnyFvByFileType (\r
232 IN EFI_FV_FILETYPE FileType,\r
233 IN UINTN FileInstance,\r
234 IN EFI_SECTION_TYPE SectionType,\r
235 IN UINTN SectionInstance,\r
236 OUT VOID **Buffer,\r
237 OUT UINTN *Size\r
238 )\r
239{\r
240 EFI_STATUS Status;\r
241 EFI_HANDLE *HandleBuffer;\r
242 UINTN HandleCount;\r
243 UINTN IndexFv;\r
244 UINTN IndexFile;\r
245 UINTN Key;\r
246 EFI_GUID NameGuid;\r
247 EFI_FV_FILE_ATTRIBUTES Attributes;\r
248 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
249\r
250 ASSERT (Buffer != NULL);\r
251 ASSERT (Size != NULL);\r
252\r
253 //\r
254 // Locate all available FVs.\r
255 //\r
256 HandleBuffer = NULL;\r
257 Status = gBS->LocateHandleBuffer (\r
258 ByProtocol,\r
259 &gEfiFirmwareVolume2ProtocolGuid,\r
260 NULL,\r
261 &HandleCount,\r
262 &HandleBuffer\r
263 );\r
264 if (EFI_ERROR (Status)) {\r
265 return Status;\r
266 }\r
267\r
268 //\r
269 // Go through FVs one by one to find the required section data.\r
270 //\r
271 for (IndexFv = 0; IndexFv < HandleCount; IndexFv++) {\r
272 Status = gBS->HandleProtocol (\r
273 HandleBuffer[IndexFv],\r
274 &gEfiFirmwareVolume2ProtocolGuid,\r
275 (VOID **)&Fv\r
276 );\r
277 if (EFI_ERROR (Status)) {\r
278 continue;\r
279 }\r
280\r
281 //\r
282 // Use Firmware Volume 2 Protocol to search for a file of type FileType in all FVs.\r
283 //\r
284 IndexFile = FileInstance + 1;\r
285 Key = 0;\r
286 do {\r
287 Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, Size);\r
288 if (EFI_ERROR (Status)) {\r
289 break;\r
290 }\r
291 IndexFile --;\r
292 } while (IndexFile > 0);\r
293\r
294 //\r
295 // Fv File with the required FV file type is found.\r
296 // Search the section file in the found FV file.\r
297 //\r
298 if (IndexFile == 0) {\r
299 Status = InternalGetSectionFromFv (\r
300 HandleBuffer[IndexFv],\r
301 &NameGuid,\r
302 SectionType,\r
303 SectionInstance,\r
304 Buffer,\r
305 Size\r
306 );\r
307\r
308 if (!EFI_ERROR (Status)) {\r
309 goto Done;\r
310 }\r
311 }\r
312 }\r
313\r
314 //\r
315 // The required FFS section file is not found.\r
316 //\r
317 if (IndexFv == HandleCount) {\r
318 Status = EFI_NOT_FOUND;\r
319 }\r
320\r
321Done:\r
322 if (HandleBuffer != NULL) {\r
323 FreePool(HandleBuffer);\r
324 }\r
325\r
326 return Status;\r
327}\r
328\r
329/**\r
330 Searches all the availables firmware volumes and returns the first matching FFS section.\r
331\r
332 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.\r
333 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search\r
334 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances\r
335 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.\r
336 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.\r
337 It is the caller's responsibility to use FreePool() to free the allocated buffer.\r
338 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections\r
339 are retrieved from an FFS file based on SectionType and SectionInstance.\r
340\r
341 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,\r
342 the search will be retried with a section type of EFI_SECTION_PE32.\r
343 This function must be called with a TPL <= TPL_NOTIFY.\r
344\r
345 If NameGuid is NULL, then ASSERT().\r
346 If Buffer is NULL, then ASSERT().\r
347 If Size is NULL, then ASSERT().\r
348\r
349\r
350 @param NameGuid A pointer to to the FFS filename GUID to search for\r
351 within any of the firmware volumes in the platform.\r
352 @param SectionType Indicates the FFS section type to search for within\r
353 the FFS file specified by NameGuid.\r
354 @param SectionInstance Indicates which section instance within the FFS file\r
355 specified by NameGuid to retrieve.\r
356 @param Buffer On output, a pointer to a callee allocated buffer\r
357 containing the FFS file section that was found.\r
358 Is it the caller's responsibility to free this buffer\r
359 using FreePool().\r
360 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
361\r
362 @retval EFI_SUCCESS The specified FFS section was returned.\r
363 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
364 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
365 retrieve the matching FFS section.\r
366 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a\r
367 device error.\r
368 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the\r
369 firmware volume that\r
370 contains the matching FFS section does not allow reads.\r
371**/\r
372EFI_STATUS\r
373EFIAPI\r
374GetSectionFromAnyFv (\r
375 IN CONST EFI_GUID *NameGuid,\r
376 IN EFI_SECTION_TYPE SectionType,\r
377 IN UINTN SectionInstance,\r
378 OUT VOID **Buffer,\r
379 OUT UINTN *Size\r
380 )\r
381{\r
382 EFI_STATUS Status;\r
383 EFI_HANDLE *HandleBuffer;\r
384 UINTN HandleCount;\r
385 UINTN Index;\r
386 EFI_HANDLE FvHandle;\r
387\r
388 //\r
389 // Search the FV that contain the caller's FFS first.\r
390 // FV builder can choose to build FFS into the this FV\r
391 // so that this implementation of GetSectionFromAnyFv\r
392 // will locate the FFS faster.\r
393 //\r
394 FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
395 Status = InternalGetSectionFromFv (\r
396 FvHandle,\r
397 NameGuid,\r
398 SectionType,\r
399 SectionInstance,\r
400 Buffer,\r
401 Size\r
402 );\r
403 if (!EFI_ERROR (Status)) {\r
404 return EFI_SUCCESS;\r
405 }\r
406\r
407 HandleBuffer = NULL;\r
408 Status = gBS->LocateHandleBuffer (\r
409 ByProtocol,\r
410 &gEfiFirmwareVolume2ProtocolGuid,\r
411 NULL,\r
412 &HandleCount,\r
413 &HandleBuffer\r
414 );\r
415 if (EFI_ERROR (Status)) {\r
416 goto Done;\r
417 }\r
418\r
419 for (Index = 0; Index < HandleCount; Index++) {\r
420 //\r
421 // Skip the FV that contain the caller's FFS\r
422 //\r
423 if (HandleBuffer[Index] != FvHandle) {\r
424 Status = InternalGetSectionFromFv (\r
425 HandleBuffer[Index],\r
426 NameGuid,\r
427 SectionType,\r
428 SectionInstance,\r
429 Buffer,\r
430 Size\r
431 );\r
432\r
433 if (!EFI_ERROR (Status)) {\r
434 goto Done;\r
435 }\r
436 }\r
437\r
438 }\r
439\r
440 if (Index == HandleCount) {\r
441 Status = EFI_NOT_FOUND;\r
442 }\r
443\r
444Done:\r
445\r
446 if (HandleBuffer != NULL) {\r
447 FreePool(HandleBuffer);\r
448 }\r
449 return Status;\r
450\r
451}\r
452\r
453/**\r
454 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section.\r
455\r
456 This function searches the firmware volume that the currently executing module was loaded\r
457 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search\r
458 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance\r
459 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.\r
460 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size.\r
461 It is the caller's responsibility to use FreePool() to free the allocated buffer.\r
462 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from\r
463 an FFS file based on SectionType and SectionInstance.\r
464\r
465 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.\r
466 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,\r
467 the search will be retried with a section type of EFI_SECTION_PE32.\r
468\r
469 This function must be called with a TPL <= TPL_NOTIFY.\r
470 If NameGuid is NULL, then ASSERT().\r
471 If Buffer is NULL, then ASSERT().\r
472 If Size is NULL, then ASSERT().\r
473\r
474 @param NameGuid A pointer to to the FFS filename GUID to search for\r
475 within the firmware volumes that the currently\r
476 executing module was loaded from.\r
477 @param SectionType Indicates the FFS section type to search for within\r
478 the FFS file specified by NameGuid.\r
479 @param SectionInstance Indicates which section instance within the FFS file\r
480 specified by NameGuid to retrieve.\r
481 @param Buffer On output, a pointer to a callee allocated buffer\r
482 containing the FFS file section that was found.\r
483 Is it the caller's responsibility to free this buffer\r
484 using FreePool().\r
485 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
486\r
487\r
488 @retval EFI_SUCCESS The specified FFS section was returned.\r
489 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
490 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve\r
491 the matching FFS section.\r
492 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a\r
493 device error.\r
494 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the\r
495 firmware volume that contains the matching FFS\r
496 section does not allow reads.\r
497**/\r
498EFI_STATUS\r
499EFIAPI\r
500GetSectionFromFv (\r
501 IN CONST EFI_GUID *NameGuid,\r
502 IN EFI_SECTION_TYPE SectionType,\r
503 IN UINTN SectionInstance,\r
504 OUT VOID **Buffer,\r
505 OUT UINTN *Size\r
506 )\r
507{\r
508 return InternalGetSectionFromFv (\r
509 InternalImageHandleToFvHandle(gImageHandle),\r
510 NameGuid,\r
511 SectionType,\r
512 SectionInstance,\r
513 Buffer,\r
514 Size\r
515 );\r
516}\r
517\r
518\r
519/**\r
520 Searches the FFS file the currently executing module was loaded from and returns the first matching FFS section.\r
521\r
522 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.\r
523 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType,\r
524 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(),\r
525 and the size of the allocated buffer is returned in Size. It is the caller's responsibility\r
526 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for\r
527 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.\r
528\r
529 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.\r
530 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,\r
531 the search will be retried with a section type of EFI_SECTION_PE32.\r
532 This function must be called with a TPL <= TPL_NOTIFY.\r
533\r
534 If Buffer is NULL, then ASSERT().\r
535 If Size is NULL, then ASSERT().\r
536\r
537\r
538 @param SectionType Indicates the FFS section type to search for within\r
539 the FFS file that the currently executing module\r
540 was loaded from.\r
541 @param SectionInstance Indicates which section instance to retrieve within\r
542 the FFS file that the currently executing module\r
543 was loaded from.\r
544 @param Buffer On output, a pointer to a callee allocated buffer\r
545 containing the FFS file section that was found.\r
546 Is it the caller's responsibility to free this buffer\r
547 using FreePool().\r
548 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
549\r
550 @retval EFI_SUCCESS The specified FFS section was returned.\r
551 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
552 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve\r
553 the matching FFS section.\r
554 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a\r
555 device error.\r
556 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the\r
557 firmware volume that contains the matching FFS\r
558 section does not allow reads.\r
559\r
560**/\r
561EFI_STATUS\r
562EFIAPI\r
563GetSectionFromFfs (\r
564 IN EFI_SECTION_TYPE SectionType,\r
565 IN UINTN SectionInstance,\r
566 OUT VOID **Buffer,\r
567 OUT UINTN *Size\r
568 )\r
569{\r
570 return InternalGetSectionFromFv(\r
571 InternalImageHandleToFvHandle(gImageHandle),\r
572 &gEfiCallerIdGuid,\r
573 SectionType,\r
574 SectionInstance,\r
575 Buffer,\r
576 Size\r
577 );\r
578}\r
579\r
580\r
581/**\r
582 Get the image file buffer data and buffer size by its device path.\r
583\r
584 Access the file either from a firmware volume, from a file system interface,\r
585 or from the load file interface.\r
586\r
587 Allocate memory to store the found image. The caller is responsible to free memory.\r
588\r
589 If FilePath is NULL, then NULL is returned.\r
590 If FileSize is NULL, then NULL is returned.\r
591 If AuthenticationStatus is NULL, then NULL is returned.\r
592\r
593 @param[in] BootPolicy Policy for Open Image File.If TRUE, indicates\r
594 that the request originates from the boot\r
595 manager, and that the boot manager is\r
596 attempting to load FilePath as a boot\r
597 selection. If FALSE, then FilePath must\r
598 match an exact file to be loaded.\r
599 @param[in] FilePath The pointer to the device path of the file\r
600 that is abstracted to the file buffer.\r
601 @param[out] FileSize The pointer to the size of the abstracted\r
602 file buffer.\r
603 @param[out] AuthenticationStatus Pointer to the authentication status.\r
604\r
605 @retval NULL FilePath is NULL, or FileSize is NULL, or AuthenticationStatus is NULL, or the file can't be found.\r
606 @retval other The abstracted file buffer. The caller is responsible to free memory.\r
607**/\r
608VOID *\r
609EFIAPI\r
610GetFileBufferByFilePath (\r
611 IN BOOLEAN BootPolicy,\r
612 IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
613 OUT UINTN *FileSize,\r
614 OUT UINT32 *AuthenticationStatus\r
615 )\r
616{\r
617 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
618 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;\r
619 EFI_DEVICE_PATH_PROTOCOL *TempDevicePathNode;\r
620 EFI_HANDLE Handle;\r
621 EFI_GUID *FvNameGuid;\r
622 EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVol;\r
623 EFI_SECTION_TYPE SectionType;\r
624 UINT8 *ImageBuffer;\r
625 UINTN ImageBufferSize;\r
626 EFI_FV_FILETYPE Type;\r
627 EFI_FV_FILE_ATTRIBUTES Attrib;\r
628 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
629 EFI_FILE_HANDLE FileHandle;\r
630 EFI_FILE_HANDLE LastHandle;\r
631 EFI_FILE_INFO *FileInfo;\r
632 UINTN FileInfoSize;\r
633 EFI_LOAD_FILE_PROTOCOL *LoadFile;\r
634 EFI_LOAD_FILE2_PROTOCOL *LoadFile2;\r
635 EFI_STATUS Status;\r
636\r
637 //\r
638 // Check input File device path.\r
639 //\r
640 if (FilePath == NULL || FileSize == NULL || AuthenticationStatus == NULL) {\r
641 return NULL;\r
642 }\r
643\r
644 //\r
645 // Init local variable\r
646 //\r
647 TempDevicePathNode = NULL;\r
648 FvNameGuid = NULL;\r
649 FileInfo = NULL;\r
650 FileHandle = NULL;\r
651 ImageBuffer = NULL;\r
652 ImageBufferSize = 0;\r
653 *AuthenticationStatus = 0;\r
654\r
655 //\r
656 // Copy File Device Path\r
657 //\r
658 OrigDevicePathNode = DuplicateDevicePath (FilePath);\r
659 if (OrigDevicePathNode == NULL) {\r
660 return NULL;\r
661 }\r
662\r
663 //\r
664 // Check whether this device path support FV2 protocol.\r
665 // Is so, this device path may contain a Image.\r
666 //\r
667 DevicePathNode = OrigDevicePathNode;\r
668 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePathNode, &Handle);\r
669 if (!EFI_ERROR (Status)) {\r
670 //\r
671 // For FwVol File system there is only a single file name that is a GUID.\r
672 //\r
673 FvNameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePathNode);\r
674 if (FvNameGuid == NULL) {\r
675 Status = EFI_INVALID_PARAMETER;\r
676 } else {\r
677 //\r
678 // Read image from the firmware file\r
679 //\r
680 Status = gBS->HandleProtocol (Handle, &gEfiFirmwareVolume2ProtocolGuid, (VOID**)&FwVol);\r
681 if (!EFI_ERROR (Status)) {\r
682 SectionType = EFI_SECTION_PE32;\r
683 ImageBuffer = NULL;\r
684 Status = FwVol->ReadSection (\r
685 FwVol,\r
686 FvNameGuid,\r
687 SectionType,\r
688 0,\r
689 (VOID **)&ImageBuffer,\r
690 &ImageBufferSize,\r
691 AuthenticationStatus\r
692 );\r
693 if (EFI_ERROR (Status)) {\r
694 //\r
695 // Try a raw file, since a PE32 SECTION does not exist\r
696 //\r
697 if (ImageBuffer != NULL) {\r
698 FreePool (ImageBuffer);\r
699 *AuthenticationStatus = 0;\r
700 }\r
701 ImageBuffer = NULL;\r
702 Status = FwVol->ReadFile (\r
703 FwVol,\r
704 FvNameGuid,\r
705 (VOID **)&ImageBuffer,\r
706 &ImageBufferSize,\r
707 &Type,\r
708 &Attrib,\r
709 AuthenticationStatus\r
710 );\r
711 }\r
712 }\r
713 }\r
714 if (!EFI_ERROR (Status)) {\r
715 goto Finish;\r
716 }\r
717 }\r
718\r
719 //\r
720 // Attempt to access the file via a file system interface\r
721 //\r
722 DevicePathNode = OrigDevicePathNode;\r
723 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathNode, &Handle);\r
724 if (!EFI_ERROR (Status)) {\r
725 Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID**)&Volume);\r
726 if (!EFI_ERROR (Status)) {\r
727 //\r
728 // Open the Volume to get the File System handle\r
729 //\r
730 Status = Volume->OpenVolume (Volume, &FileHandle);\r
731 if (!EFI_ERROR (Status)) {\r
732 //\r
733 // Duplicate the device path to avoid the access to unaligned device path node.\r
734 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r
735 // nodes, It assures the fields in device path nodes are 2 byte aligned.\r
736 //\r
737 TempDevicePathNode = DuplicateDevicePath (DevicePathNode);\r
738 if (TempDevicePathNode == NULL) {\r
739 FileHandle->Close (FileHandle);\r
740 //\r
741 // Setting Status to an EFI_ERROR value will cause the rest of\r
742 // the file system support below to be skipped.\r
743 //\r
744 Status = EFI_OUT_OF_RESOURCES;\r
745 }\r
746 //\r
747 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
748 // directory information and filename can be separate. The goal is to inch\r
749 // our way down each device path node and close the previous node\r
750 //\r
751 DevicePathNode = TempDevicePathNode;\r
752 while (!EFI_ERROR (Status) && !IsDevicePathEnd (DevicePathNode)) {\r
753 if (DevicePathType (DevicePathNode) != MEDIA_DEVICE_PATH ||\r
754 DevicePathSubType (DevicePathNode) != MEDIA_FILEPATH_DP) {\r
755 Status = EFI_UNSUPPORTED;\r
756 break;\r
757 }\r
758\r
759 LastHandle = FileHandle;\r
760 FileHandle = NULL;\r
761\r
762 Status = LastHandle->Open (\r
763 LastHandle,\r
764 &FileHandle,\r
765 ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName,\r
766 EFI_FILE_MODE_READ,\r
767 0\r
768 );\r
769\r
770 //\r
771 // Close the previous node\r
772 //\r
773 LastHandle->Close (LastHandle);\r
774\r
775 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
776 }\r
777\r
778 if (!EFI_ERROR (Status)) {\r
779 //\r
780 // We have found the file. Now we need to read it. Before we can read the file we need to\r
781 // figure out how big the file is.\r
782 //\r
783 FileInfo = NULL;\r
784 FileInfoSize = 0;\r
785 Status = FileHandle->GetInfo (\r
786 FileHandle,\r
787 &gEfiFileInfoGuid,\r
788 &FileInfoSize,\r
789 FileInfo\r
790 );\r
791\r
792 if (Status == EFI_BUFFER_TOO_SMALL) {\r
793 FileInfo = AllocatePool (FileInfoSize);\r
794 if (FileInfo == NULL) {\r
795 Status = EFI_OUT_OF_RESOURCES;\r
796 } else {\r
797 Status = FileHandle->GetInfo (\r
798 FileHandle,\r
799 &gEfiFileInfoGuid,\r
800 &FileInfoSize,\r
801 FileInfo\r
802 );\r
803 }\r
804 }\r
805\r
806 if (!EFI_ERROR (Status) && (FileInfo != NULL)) {\r
807 if ((FileInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r
808 //\r
809 // Allocate space for the file\r
810 //\r
811 ImageBuffer = AllocatePool ((UINTN)FileInfo->FileSize);\r
812 if (ImageBuffer == NULL) {\r
813 Status = EFI_OUT_OF_RESOURCES;\r
814 } else {\r
815 //\r
816 // Read the file into the buffer we allocated\r
817 //\r
818 ImageBufferSize = (UINTN)FileInfo->FileSize;\r
819 Status = FileHandle->Read (FileHandle, &ImageBufferSize, ImageBuffer);\r
820 }\r
821 }\r
822 }\r
823 }\r
824 //\r
825 // Close the file and Free FileInfo and TempDevicePathNode since we are done\r
826 //\r
827 if (FileInfo != NULL) {\r
828 FreePool (FileInfo);\r
829 }\r
830 if (FileHandle != NULL) {\r
831 FileHandle->Close (FileHandle);\r
832 }\r
833 if (TempDevicePathNode != NULL) {\r
834 FreePool (TempDevicePathNode);\r
835 }\r
836 }\r
837 }\r
838 if (!EFI_ERROR (Status)) {\r
839 goto Finish;\r
840 }\r
841 }\r
842\r
843 //\r
844 // Attempt to access the file via LoadFile2 interface\r
845 //\r
846 if (!BootPolicy) {\r
847 DevicePathNode = OrigDevicePathNode;\r
848 Status = gBS->LocateDevicePath (&gEfiLoadFile2ProtocolGuid, &DevicePathNode, &Handle);\r
849 if (!EFI_ERROR (Status)) {\r
850 Status = gBS->HandleProtocol (Handle, &gEfiLoadFile2ProtocolGuid, (VOID**)&LoadFile2);\r
851 if (!EFI_ERROR (Status)) {\r
852 //\r
853 // Call LoadFile2 with the correct buffer size\r
854 //\r
855 ImageBufferSize = 0;\r
856 ImageBuffer = NULL;\r
857 Status = LoadFile2->LoadFile (\r
858 LoadFile2,\r
859 DevicePathNode,\r
860 FALSE,\r
861 &ImageBufferSize,\r
862 ImageBuffer\r
863 );\r
864 if (Status == EFI_BUFFER_TOO_SMALL) {\r
865 ImageBuffer = AllocatePool (ImageBufferSize);\r
866 if (ImageBuffer == NULL) {\r
867 Status = EFI_OUT_OF_RESOURCES;\r
868 } else {\r
869 Status = LoadFile2->LoadFile (\r
870 LoadFile2,\r
871 DevicePathNode,\r
872 FALSE,\r
873 &ImageBufferSize,\r
874 ImageBuffer\r
875 );\r
876 }\r
877 }\r
878 }\r
879 if (!EFI_ERROR (Status)) {\r
880 goto Finish;\r
881 }\r
882 }\r
883 }\r
884\r
885 //\r
886 // Attempt to access the file via LoadFile interface\r
887 //\r
888 DevicePathNode = OrigDevicePathNode;\r
889 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &DevicePathNode, &Handle);\r
890 if (!EFI_ERROR (Status)) {\r
891 Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID**)&LoadFile);\r
892 if (!EFI_ERROR (Status)) {\r
893 //\r
894 // Call LoadFile with the correct buffer size\r
895 //\r
896 ImageBufferSize = 0;\r
897 ImageBuffer = NULL;\r
898 Status = LoadFile->LoadFile (\r
899 LoadFile,\r
900 DevicePathNode,\r
901 BootPolicy,\r
902 &ImageBufferSize,\r
903 ImageBuffer\r
904 );\r
905 if (Status == EFI_BUFFER_TOO_SMALL) {\r
906 ImageBuffer = AllocatePool (ImageBufferSize);\r
907 if (ImageBuffer == NULL) {\r
908 Status = EFI_OUT_OF_RESOURCES;\r
909 } else {\r
910 Status = LoadFile->LoadFile (\r
911 LoadFile,\r
912 DevicePathNode,\r
913 BootPolicy,\r
914 &ImageBufferSize,\r
915 ImageBuffer\r
916 );\r
917 }\r
918 }\r
919 }\r
920 }\r
921\r
922Finish:\r
923\r
924 if (EFI_ERROR (Status)) {\r
925 if (ImageBuffer != NULL) {\r
926 FreePool (ImageBuffer);\r
927 ImageBuffer = NULL;\r
928 }\r
929 *FileSize = 0;\r
930 } else {\r
931 *FileSize = ImageBufferSize;\r
932 }\r
933\r
934 FreePool (OrigDevicePathNode);\r
935\r
936 return ImageBuffer;\r
937}\r
938\r
939/**\r
940 Searches all the available firmware volumes and returns the file device path of first matching\r
941 FFS section.\r
942\r
943 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.\r
944 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search\r
945 is made for FFS sections of type SectionType.\r
946\r
947 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,\r
948 the search will be retried with a section type of EFI_SECTION_PE32.\r
949 This function must be called with a TPL <= TPL_NOTIFY.\r
950\r
951 If NameGuid is NULL, then ASSERT().\r
952\r
953 @param NameGuid A pointer to to the FFS filename GUID to search for\r
954 within any of the firmware volumes in the platform.\r
955 @param SectionType Indicates the FFS section type to search for within\r
956 the FFS file specified by NameGuid.\r
957 @param SectionInstance Indicates which section instance within the FFS file\r
958 specified by NameGuid to retrieve.\r
959 @param FvFileDevicePath Device path for the target FFS\r
960 file.\r
961\r
962 @retval EFI_SUCCESS The specified file device path of FFS section was returned.\r
963 @retval EFI_NOT_FOUND The specified file device path of FFS section could not be found.\r
964 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a\r
965 device error.\r
966 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the\r
967 firmware volume that contains the matching FFS section does not\r
968 allow reads.\r
969 @retval EFI_INVALID_PARAMETER FvFileDevicePath is NULL.\r
970\r
971**/\r
972EFI_STATUS\r
973EFIAPI\r
974GetFileDevicePathFromAnyFv (\r
975 IN CONST EFI_GUID *NameGuid,\r
976 IN EFI_SECTION_TYPE SectionType,\r
977 IN UINTN SectionInstance,\r
978 OUT EFI_DEVICE_PATH_PROTOCOL **FvFileDevicePath\r
979 )\r
980{\r
981 EFI_STATUS Status;\r
982 EFI_HANDLE *HandleBuffer;\r
983 UINTN HandleCount;\r
984 UINTN Index;\r
985 EFI_HANDLE FvHandle;\r
986 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
987 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *TempFvFileDevicePath;\r
988 VOID *Buffer;\r
989 UINTN Size;\r
990\r
991 if (FvFileDevicePath == NULL) {\r
992 return EFI_INVALID_PARAMETER;\r
993 }\r
994\r
995 HandleBuffer = NULL;\r
996 FvDevicePath = NULL;\r
997 TempFvFileDevicePath = NULL;\r
998 Buffer = NULL;\r
999 Size = 0;\r
1000\r
1001 //\r
1002 // Search the FV that contain the caller's FFS first.\r
1003 // FV builder can choose to build FFS into the this FV\r
1004 // so that this implementation of GetSectionFromAnyFv\r
1005 // will locate the FFS faster.\r
1006 //\r
1007 FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
1008 Status = InternalGetSectionFromFv (\r
1009 FvHandle,\r
1010 NameGuid,\r
1011 SectionType,\r
1012 SectionInstance,\r
1013 &Buffer,\r
1014 &Size\r
1015 );\r
1016 if (!EFI_ERROR (Status)) {\r
1017 goto Done;\r
1018 }\r
1019\r
1020 Status = gBS->LocateHandleBuffer (\r
1021 ByProtocol,\r
1022 &gEfiFirmwareVolume2ProtocolGuid,\r
1023 NULL,\r
1024 &HandleCount,\r
1025 &HandleBuffer\r
1026 );\r
1027 if (EFI_ERROR (Status)) {\r
1028 goto Done;\r
1029 }\r
1030\r
1031 for (Index = 0; Index < HandleCount; Index++) {\r
1032 //\r
1033 // Skip the FV that contain the caller's FFS\r
1034 //\r
1035 if (HandleBuffer[Index] != FvHandle) {\r
1036 Status = InternalGetSectionFromFv (\r
1037 HandleBuffer[Index],\r
1038 NameGuid,\r
1039 SectionType,\r
1040 SectionInstance,\r
1041 &Buffer,\r
1042 &Size\r
1043 );\r
1044\r
1045 if (!EFI_ERROR (Status)) {\r
1046 //\r
1047 // Update FvHandle to the current handle.\r
1048 //\r
1049 FvHandle = HandleBuffer[Index];\r
1050 goto Done;\r
1051 }\r
1052 }\r
1053 }\r
1054\r
1055 if (Index == HandleCount) {\r
1056 Status = EFI_NOT_FOUND;\r
1057 }\r
1058\r
1059Done:\r
1060 if (Status == EFI_SUCCESS) {\r
1061 //\r
1062 // Build a device path to the file in the FV to pass into gBS->LoadImage\r
1063 //\r
1064 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1065 if (EFI_ERROR (Status)) {\r
1066 *FvFileDevicePath = NULL;\r
1067 } else {\r
1068 TempFvFileDevicePath = AllocateZeroPool (sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + END_DEVICE_PATH_LENGTH);\r
1069 if (TempFvFileDevicePath == NULL) {\r
1070 *FvFileDevicePath = NULL;\r
1071 return EFI_OUT_OF_RESOURCES;\r
1072 }\r
1073 EfiInitializeFwVolDevicepathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH*)TempFvFileDevicePath, NameGuid);\r
1074 SetDevicePathEndNode (NextDevicePathNode (TempFvFileDevicePath));\r
1075 *FvFileDevicePath = AppendDevicePath (\r
1076 FvDevicePath,\r
1077 (EFI_DEVICE_PATH_PROTOCOL *)TempFvFileDevicePath\r
1078 );\r
1079 FreePool (TempFvFileDevicePath);\r
1080 }\r
1081 }\r
1082\r
1083 if (Buffer != NULL) {\r
1084 FreePool (Buffer);\r
1085 }\r
1086\r
1087 if (HandleBuffer != NULL) {\r
1088 FreePool (HandleBuffer);\r
1089 }\r
1090\r
1091 return Status;\r
1092}\r