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