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