]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeServicesLib/DxeServicesLib.c
Roll back the previous change in MdePkg BaseLib Ia32 Thunk16.S and EcpPkg GlueLib...
[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
19388d29
HT
5 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
6 This program and the accompanying materials\r
1c280088 7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
2fc59a00 9 http://opensource.org/licenses/bsd-license.php.\r
1c280088 10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <PiDxe.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/MemoryAllocationLib.h>\r
19#include <Library/UefiBootServicesTableLib.h>\r
84716614
LG
20#include <Library/DevicePathLib.h>\r
21#include <Library/UefiLib.h>\r
eb9dd4d0 22#include <Library/DxeServicesLib.h>\r
1c280088 23#include <Protocol/FirmwareVolume2.h>\r
24#include <Protocol/LoadedImage.h>\r
84716614
LG
25#include <Protocol/LoadFile2.h>\r
26#include <Protocol/LoadFile.h>\r
27#include <Protocol/SimpleFileSystem.h>\r
28#include <Guid/FileInfo.h>\r
1c280088 29\r
30/**\r
166152e8 31 Identify the device handle from which the Image is loaded from. As this device handle is passed to\r
32 GetSectionFromFv as the identifier for a Firmware Volume, an EFI_FIRMWARE_VOLUME2_PROTOCOL \r
33 protocol instance should be located succesfully by calling gBS->HandleProtocol ().\r
1c280088 34\r
166152e8 35 This function locates the EFI_LOADED_IMAGE_PROTOCOL instance installed\r
36 on ImageHandle. It then returns EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle.\r
37 \r
38 If ImageHandle is NULL, then ASSERT ();\r
39 If failed to locate a EFI_LOADED_IMAGE_PROTOCOL on ImageHandle, then ASSERT ();\r
40 \r
41 @param ImageHandle The firmware allocated handle for UEFI image.\r
1c280088 42\r
58380e9c 43 @retval EFI_HANDLE The device handle from which the Image is loaded from.\r
1c280088 44\r
45**/\r
166152e8 46EFI_HANDLE\r
47InternalImageHandleToFvHandle (\r
48 EFI_HANDLE ImageHandle\r
49 )\r
50{\r
51 EFI_STATUS Status;\r
52 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
53 \r
54 ASSERT (ImageHandle != NULL);\r
55\r
56 Status = gBS->HandleProtocol (\r
57 (EFI_HANDLE *) ImageHandle,\r
58 &gEfiLoadedImageProtocolGuid,\r
59 (VOID **) &LoadedImage\r
60 );\r
61\r
62 ASSERT_EFI_ERROR (Status);\r
63\r
64 return LoadedImage->DeviceHandle;\r
65\r
66}\r
67\r
68/**\r
b75a165d
LG
69 Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware \r
70 Section type and instance number from the specified Firmware Volume.\r
71\r
72 This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to \r
73 carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed \r
74 by NameGuid, SectionType and SectionInstance. \r
75 \r
76 The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
77 found in PI Specification.\r
78 \r
79 If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
80 is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
81 is returned.\r
166152e8 82 \r
b75a165d
LG
83 The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
84 by this function. This function can be only called at TPL_NOTIFY and below.\r
166152e8 85 \r
b75a165d
LG
86 If FvHandle is NULL, then ASSERT ();\r
87 If NameGuid is NULL, then ASSERT();\r
88 If Buffer is NULL, then ASSERT();\r
89 If Size is NULL, then ASSERT().\r
90\r
58380e9c 91 @param FvHandle The device handle that contains a instance of \r
92 EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
b75a165d
LG
93 @param NameGuid The GUID name of a Firmware File.\r
94 @param SectionType The Firmware Section type.\r
58380e9c 95 @param SectionInstance The instance number of Firmware Section to \r
96 read from starting from 0.\r
97 @param Buffer On output, Buffer contains the the data read \r
98 from the section in the Firmware File found.\r
b75a165d
LG
99 @param Size On output, the size of Buffer.\r
100\r
101 @retval EFI_SUCCESS The image is found and data and size is returned.\r
58380e9c 102 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType \r
103 can't be found.\r
104 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the \r
105 output data buffer or complete the operations.\r
106 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the \r
107 Firmware Volume.\r
108 @retval EFI_ACCESS_DENIED The firmware volume containing the searched \r
109 Firmware File is configured to disallow reads.\r
166152e8 110 \r
f80b0830 111**/\r
1c280088 112EFI_STATUS\r
eb9dd4d0 113InternalGetSectionFromFv (\r
166152e8 114 IN EFI_HANDLE FvHandle,\r
115 IN CONST EFI_GUID *NameGuid,\r
116 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 117 IN UINTN SectionInstance,\r
166152e8 118 OUT VOID **Buffer,\r
119 OUT UINTN *Size\r
1c280088 120 )\r
121{\r
166152e8 122 EFI_STATUS Status;\r
123 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
124 UINT32 AuthenticationStatus;\r
125\r
8cf43dd7 126 ASSERT (NameGuid != NULL);\r
127 ASSERT (Buffer != NULL);\r
128 ASSERT (Size != NULL);\r
129 \r
166152e8 130 ASSERT (FvHandle != NULL);\r
131\r
132 Status = gBS->HandleProtocol (\r
133 FvHandle,\r
134 &gEfiFirmwareVolume2ProtocolGuid,\r
135 (VOID **) &Fv\r
136 );\r
137 if (EFI_ERROR (Status)) {\r
7ca066f9 138 return EFI_NOT_FOUND;\r
166152e8 139 }\r
1c280088 140\r
141 //\r
142 // Read desired section content in NameGuid file\r
143 //\r
144 *Buffer = NULL;\r
145 *Size = 0;\r
146 Status = Fv->ReadSection (\r
147 Fv,\r
148 NameGuid,\r
149 SectionType,\r
ff197efb 150 SectionInstance,\r
1c280088 151 Buffer,\r
152 Size,\r
153 &AuthenticationStatus\r
154 );\r
155\r
156 if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r
157 //\r
166152e8 158 // Try reading PE32 section, if the required section is TE type \r
1c280088 159 //\r
160 *Buffer = NULL;\r
161 *Size = 0;\r
162 Status = Fv->ReadSection (\r
163 Fv,\r
164 NameGuid,\r
165 EFI_SECTION_PE32,\r
ff197efb 166 SectionInstance,\r
1c280088 167 Buffer,\r
168 Size,\r
169 &AuthenticationStatus\r
170 );\r
171 }\r
172\r
1c280088 173 return Status;\r
174}\r
175\r
682cee4c
LG
176/**\r
177 Searches all the available firmware volumes and returns the first matching FFS section. \r
178\r
179 This function searches all the firmware volumes for FFS files with FV file type specified by FileType\r
180 The order that the firmware volumes is searched is not deterministic. For each available FV a search \r
181 is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType, \r
182 the FileInstance instance will be the matched FFS file. For each FFS file found a search \r
183 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances \r
184 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer. \r
185 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
186 It is the caller's responsibility to use FreePool() to free the allocated buffer. \r
187 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections \r
188 are retrieved from an FFS file based on SectionType and SectionInstance.\r
189\r
190 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
191 the search will be retried with a section type of EFI_SECTION_PE32.\r
192 This function must be called with a TPL <= TPL_NOTIFY.\r
193\r
194 If Buffer is NULL, then ASSERT().\r
195 If Size is NULL, then ASSERT().\r
196\r
58380e9c 197 @param FileType Indicates the FV file type to search for within all \r
198 available FVs.\r
199 @param FileInstance Indicates which file instance within all available \r
200 FVs specified by FileType.\r
682cee4c 201 FileInstance starts from zero.\r
58380e9c 202 @param SectionType Indicates the FFS section type to search for \r
203 within the FFS file \r
682cee4c
LG
204 specified by FileType with FileInstance.\r
205 @param SectionInstance Indicates which section instance within the FFS file \r
58380e9c 206 specified by FileType with FileInstance to retrieve. \r
207 SectionInstance starts from zero.\r
208 @param Buffer On output, a pointer to a callee allocated buffer \r
209 containing the FFS file section that was found.\r
210 Is it the caller's responsibility to free this \r
211 buffer using FreePool().\r
682cee4c
LG
212 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
213\r
214 @retval EFI_SUCCESS The specified FFS section was returned.\r
215 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
58380e9c 216 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve \r
217 the matching FFS section.\r
218 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a \r
219 device error.\r
220 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because \r
221 the firmware volume that \r
682cee4c
LG
222 contains the matching FFS section does not allow reads.\r
223**/\r
224EFI_STATUS\r
225EFIAPI\r
226GetSectionFromAnyFvByFileType (\r
227 IN EFI_FV_FILETYPE FileType,\r
228 IN UINTN FileInstance,\r
229 IN EFI_SECTION_TYPE SectionType,\r
230 IN UINTN SectionInstance,\r
231 OUT VOID **Buffer,\r
232 OUT UINTN *Size\r
233 )\r
234{\r
235 EFI_STATUS Status;\r
236 EFI_HANDLE *HandleBuffer;\r
237 UINTN HandleCount;\r
238 UINTN IndexFv;\r
239 UINTN IndexFile;\r
240 UINTN Key;\r
241 EFI_GUID NameGuid;\r
242 EFI_FV_FILE_ATTRIBUTES Attributes;\r
243 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
244\r
245 //\r
246 // Locate all available FVs.\r
247 //\r
248 HandleBuffer = NULL;\r
249 Status = gBS->LocateHandleBuffer (\r
250 ByProtocol,\r
251 &gEfiFirmwareVolume2ProtocolGuid,\r
252 NULL,\r
253 &HandleCount,\r
254 &HandleBuffer\r
255 );\r
256 if (EFI_ERROR (Status)) {\r
257 return Status;\r
258 }\r
259\r
260 //\r
261 // Go through FVs one by one to find the required section data.\r
262 //\r
263 for (IndexFv = 0; IndexFv < HandleCount; IndexFv++) {\r
264 Status = gBS->HandleProtocol (\r
265 HandleBuffer[IndexFv],\r
266 &gEfiFirmwareVolume2ProtocolGuid,\r
267 (VOID **)&Fv\r
268 );\r
269 if (EFI_ERROR (Status)) {\r
270 continue;\r
271 }\r
272\r
273 //\r
274 // Use Firmware Volume 2 Protocol to search for a file of type FileType in all FVs.\r
275 //\r
276 IndexFile = FileInstance + 1;\r
277 Key = 0;\r
278 do {\r
279 Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, Size);\r
280 if (EFI_ERROR (Status)) {\r
281 break;\r
282 }\r
283 IndexFile --;\r
284 } while (IndexFile > 0);\r
b0d803fe 285\r
682cee4c
LG
286 //\r
287 // Fv File with the required FV file type is found.\r
288 // Search the section file in the found FV file.\r
289 //\r
290 if (IndexFile == 0) {\r
291 Status = InternalGetSectionFromFv (\r
292 HandleBuffer[IndexFv], \r
293 &NameGuid,\r
294 SectionType,\r
295 SectionInstance,\r
296 Buffer,\r
297 Size\r
298 );\r
299\r
300 if (!EFI_ERROR (Status)) {\r
301 goto Done;\r
302 }\r
303 }\r
304 }\r
305\r
306 //\r
307 // The required FFS section file is not found. \r
308 //\r
309 if (IndexFv == HandleCount) {\r
310 Status = EFI_NOT_FOUND;\r
311 }\r
312\r
313Done:\r
314 if (HandleBuffer != NULL) { \r
116b142d 315 FreePool(HandleBuffer);\r
682cee4c
LG
316 }\r
317\r
318 return Status;\r
319}\r
b0d803fe 320\r
f80b0830 321/**\r
b75a165d
LG
322 Searches all the availables firmware volumes and returns the first matching FFS section. \r
323\r
324 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid. \r
325 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search \r
326 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances \r
327 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer. \r
328 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
329 It is the caller's responsibility to use FreePool() to free the allocated buffer. \r
330 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections \r
331 are retrieved from an FFS file based on SectionType and SectionInstance.\r
332\r
333 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
334 the search will be retried with a section type of EFI_SECTION_PE32.\r
335 This function must be called with a TPL <= TPL_NOTIFY.\r
336\r
337 If NameGuid is NULL, then ASSERT().\r
338 If Buffer is NULL, then ASSERT().\r
ff197efb 339 If Size is NULL, then ASSERT().\r
340\r
f80b0830 341\r
58380e9c 342 @param NameGuid A pointer to to the FFS filename GUID to search for \r
343 within any of the firmware volumes in the platform. \r
344 @param SectionType Indicates the FFS section type to search for within \r
345 the FFS file specified by NameGuid.\r
346 @param SectionInstance Indicates which section instance within the FFS file \r
347 specified by NameGuid to retrieve.\r
348 @param Buffer On output, a pointer to a callee allocated buffer \r
349 containing the FFS file section that was found. \r
350 Is it the caller's responsibility to free this buffer \r
351 using FreePool().\r
b75a165d
LG
352 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
353\r
354 @retval EFI_SUCCESS The specified FFS section was returned.\r
355 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
58380e9c 356 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to \r
357 retrieve the matching FFS section.\r
358 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a \r
359 device error.\r
360 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the \r
361 firmware volume that \r
b75a165d 362 contains the matching FFS section does not allow reads.\r
f80b0830 363**/\r
b0d803fe 364EFI_STATUS\r
365EFIAPI\r
eb9dd4d0 366GetSectionFromAnyFv (\r
b0d803fe 367 IN CONST EFI_GUID *NameGuid,\r
368 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 369 IN UINTN SectionInstance,\r
b0d803fe 370 OUT VOID **Buffer,\r
371 OUT UINTN *Size\r
372 )\r
373{\r
374 EFI_STATUS Status;\r
375 EFI_HANDLE *HandleBuffer;\r
376 UINTN HandleCount;\r
377 UINTN Index;\r
378 EFI_HANDLE FvHandle;\r
b0d803fe 379\r
380 //\r
381 // Search the FV that contain the caller's FFS first.\r
382 // FV builder can choose to build FFS into the this FV\r
383 // so that this implementation of GetSectionFromAnyFv\r
384 // will locate the FFS faster.\r
385 //\r
166152e8 386 FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
eb9dd4d0 387 Status = InternalGetSectionFromFv (\r
b0d803fe 388 FvHandle,\r
389 NameGuid,\r
390 SectionType,\r
ff197efb 391 SectionInstance,\r
b0d803fe 392 Buffer,\r
393 Size\r
394 );\r
395 if (!EFI_ERROR (Status)) {\r
396 return EFI_SUCCESS;\r
397 }\r
398\r
b0d803fe 399 HandleBuffer = NULL;\r
400 Status = gBS->LocateHandleBuffer (\r
401 ByProtocol,\r
402 &gEfiFirmwareVolume2ProtocolGuid,\r
403 NULL,\r
404 &HandleCount,\r
405 &HandleBuffer\r
406 );\r
407 if (EFI_ERROR (Status)) {\r
408 goto Done;\r
409 }\r
410\r
ff197efb 411 for (Index = 0; Index < HandleCount; Index++) {\r
b0d803fe 412 //\r
413 // Skip the FV that contain the caller's FFS\r
414 //\r
ff197efb 415 if (HandleBuffer[Index] != FvHandle) {\r
eb9dd4d0 416 Status = InternalGetSectionFromFv (\r
ff197efb 417 HandleBuffer[Index], \r
418 NameGuid, \r
419 SectionType, \r
420 SectionInstance,\r
421 Buffer, \r
422 Size\r
423 );\r
424\r
425 if (!EFI_ERROR (Status)) {\r
426 goto Done;\r
427 }\r
b0d803fe 428 }\r
429\r
b0d803fe 430 }\r
431\r
432 if (Index == HandleCount) {\r
433 Status = EFI_NOT_FOUND;\r
434 }\r
435\r
436Done:\r
437 \r
116b142d
LG
438 if (HandleBuffer != NULL) { \r
439 FreePool(HandleBuffer);\r
b0d803fe 440 }\r
441 return Status;\r
442 \r
443}\r
444\r
f80b0830 445/**\r
b75a165d
LG
446 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section. \r
447\r
448 This function searches the firmware volume that the currently executing module was loaded \r
449 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search \r
450 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance \r
451 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.\r
452 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
453 It is the caller's responsibility to use FreePool() to free the allocated buffer. \r
454 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from \r
455 an FFS file based on SectionType and SectionInstance.\r
456\r
457 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.\r
458 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
459 the search will be retried with a section type of EFI_SECTION_PE32.\r
460 \r
461 This function must be called with a TPL <= TPL_NOTIFY.\r
462 If NameGuid is NULL, then ASSERT().\r
463 If Buffer is NULL, then ASSERT().\r
ff197efb 464 If Size is NULL, then ASSERT().\r
465\r
58380e9c 466 @param NameGuid A pointer to to the FFS filename GUID to search for \r
467 within the firmware volumes that the currently \r
468 executing module was loaded from.\r
469 @param SectionType Indicates the FFS section type to search for within \r
470 the FFS file specified by NameGuid.\r
471 @param SectionInstance Indicates which section instance within the FFS file \r
472 specified by NameGuid to retrieve.\r
473 @param Buffer On output, a pointer to a callee allocated buffer \r
474 containing the FFS file section that was found. \r
475 Is it the caller's responsibility to free this buffer \r
476 using FreePool().\r
b75a165d
LG
477 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
478\r
479\r
480 @retval EFI_SUCCESS The specified FFS section was returned.\r
481 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
58380e9c 482 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve \r
483 the matching FFS section.\r
484 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a \r
485 device error.\r
486 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the \r
487 firmware volume that contains the matching FFS \r
488 section does not allow reads. \r
f80b0830 489**/\r
b0d803fe 490EFI_STATUS\r
491EFIAPI\r
eb9dd4d0 492GetSectionFromFv (\r
b0d803fe 493 IN CONST EFI_GUID *NameGuid,\r
494 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 495 IN UINTN SectionInstance,\r
b0d803fe 496 OUT VOID **Buffer,\r
497 OUT UINTN *Size\r
498 )\r
499{\r
eb9dd4d0 500 return InternalGetSectionFromFv (\r
501 InternalImageHandleToFvHandle(gImageHandle),\r
502 NameGuid,\r
503 SectionType,\r
504 SectionInstance,\r
505 Buffer,\r
506 Size\r
507 );\r
b0d803fe 508}\r
509\r
510\r
f80b0830 511/**\r
b75a165d
LG
512 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.\r
513\r
514 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.\r
515 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType, \r
516 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(), \r
517 and the size of the allocated buffer is returned in Size. It is the caller's responsibility \r
518 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for \r
519 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.\r
520\r
521 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.\r
522 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
523 the search will be retried with a section type of EFI_SECTION_PE32.\r
524 This function must be called with a TPL <= TPL_NOTIFY.\r
525 \r
526 If Buffer is NULL, then ASSERT().\r
ff197efb 527 If Size is NULL, then ASSERT().\r
528\r
b75a165d 529\r
58380e9c 530 @param SectionType Indicates the FFS section type to search for within \r
531 the FFS file that the currently executing module \r
532 was loaded from.\r
533 @param SectionInstance Indicates which section instance to retrieve within \r
534 the FFS file that the currently executing module \r
535 was loaded from.\r
536 @param Buffer On output, a pointer to a callee allocated buffer \r
537 containing the FFS file section that was found. \r
538 Is it the caller's responsibility to free this buffer \r
539 using FreePool().\r
b75a165d
LG
540 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
541\r
542 @retval EFI_SUCCESS The specified FFS section was returned.\r
543 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
58380e9c 544 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve \r
545 the matching FFS section.\r
546 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a \r
547 device error.\r
548 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the \r
549 firmware volume that contains the matching FFS \r
550 section does not allow reads. \r
f80b0830 551 \r
552**/\r
b0d803fe 553EFI_STATUS\r
554EFIAPI\r
eb9dd4d0 555GetSectionFromFfs (\r
b0d803fe 556 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 557 IN UINTN SectionInstance,\r
b0d803fe 558 OUT VOID **Buffer,\r
559 OUT UINTN *Size\r
560 )\r
561{\r
eb9dd4d0 562 return InternalGetSectionFromFv(\r
563 InternalImageHandleToFvHandle(gImageHandle),\r
564 &gEfiCallerIdGuid,\r
565 SectionType,\r
566 SectionInstance,\r
567 Buffer,\r
568 Size\r
569 );\r
b0d803fe 570}\r
571\r
84716614
LG
572\r
573/**\r
574 Get the image file buffer data and buffer size by its device path. \r
575 \r
ea6898b9 576 Access the file either from a firmware volume, from a file system interface, \r
84716614
LG
577 or from the load file interface.\r
578 \r
579 Allocate memory to store the found image. The caller is responsible to free memory.\r
580\r
581 If File is NULL, then NULL is returned.\r
582 If FileSize is NULL, then NULL is returned.\r
583 If AuthenticationStatus is NULL, then NULL is returned.\r
584\r
58380e9c 585 @param[in] BootPolicy Policy for Open Image File.If TRUE, indicates \r
586 that the request originates from the boot \r
587 manager, and that the boot manager is\r
588 attempting to load FilePath as a boot\r
589 selection. If FALSE, then FilePath must \r
590 match an exact file to be loaded.\r
591 @param[in] FilePath The pointer to the device path of the file\r
592 that is absracted to the file buffer.\r
593 @param[out] FileSize The pointer to the size of the abstracted \r
594 file buffer.\r
595 @param[out] AuthenticationStatus The pointer to a caller-allocated UINT32 \r
596 in which the authentication status is returned.\r
597\r
598 @retval NULL File is NULL, or FileSize is NULL. Or, the file can't be found.\r
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
84716614
LG
800 //\r
801 // Allocate space for the file\r
802 //\r
803 ImageBuffer = AllocatePool ((UINTN)FileInfo->FileSize);\r
804 if (ImageBuffer == NULL) {\r
805 Status = EFI_OUT_OF_RESOURCES;\r
806 } else {\r
807 //\r
808 // Read the file into the buffer we allocated\r
809 //\r
810 ImageBufferSize = (UINTN)FileInfo->FileSize;\r
811 Status = FileHandle->Read (FileHandle, &ImageBufferSize, ImageBuffer);\r
812 }\r
813 }\r
814 }\r
815 //\r
95dd343a 816 // Close the file and Free FileInfo and TempDevicePathNode since we are done\r
84716614
LG
817 // \r
818 if (FileInfo != NULL) {\r
819 FreePool (FileInfo);\r
820 }\r
821 if (FileHandle != NULL) {\r
822 FileHandle->Close (FileHandle);\r
823 }\r
c119933d 824 if (TempDevicePathNode != NULL) {\r
825 FreePool (TempDevicePathNode);\r
826 }\r
84716614
LG
827 }\r
828 }\r
c119933d 829 if (!EFI_ERROR (Status)) {\r
830 goto Finish;\r
831 }\r
84716614
LG
832 }\r
833\r
834 //\r
835 // Attempt to access the file via LoadFile2 interface\r
836 //\r
837 if (!BootPolicy) {\r
838 DevicePathNode = OrigDevicePathNode;\r
839 Status = gBS->LocateDevicePath (&gEfiLoadFile2ProtocolGuid, &DevicePathNode, &Handle);\r
840 if (!EFI_ERROR (Status)) {\r
841 Status = gBS->HandleProtocol (Handle, &gEfiLoadFile2ProtocolGuid, (VOID**)&LoadFile2);\r
842 if (!EFI_ERROR (Status)) {\r
843 //\r
844 // Call LoadFile2 with the correct buffer size\r
845 //\r
846 ImageBufferSize = 0;\r
847 ImageBuffer = NULL;\r
848 Status = LoadFile2->LoadFile (\r
849 LoadFile2,\r
850 DevicePathNode,\r
851 FALSE,\r
852 &ImageBufferSize,\r
853 ImageBuffer\r
854 );\r
855 if (Status == EFI_BUFFER_TOO_SMALL) {\r
856 ImageBuffer = AllocatePool (ImageBufferSize);\r
857 if (ImageBuffer == NULL) {\r
858 Status = EFI_OUT_OF_RESOURCES;\r
859 } else {\r
860 Status = LoadFile2->LoadFile (\r
861 LoadFile2,\r
862 DevicePathNode,\r
c083f7ca 863 FALSE,\r
84716614
LG
864 &ImageBufferSize,\r
865 ImageBuffer\r
866 );\r
867 }\r
868 }\r
869 }\r
c119933d 870 if (!EFI_ERROR (Status)) {\r
871 goto Finish;\r
872 }\r
84716614
LG
873 }\r
874 }\r
875\r
876 //\r
877 // Attempt to access the file via LoadFile interface\r
878 //\r
879 DevicePathNode = OrigDevicePathNode;\r
880 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &DevicePathNode, &Handle);\r
881 if (!EFI_ERROR (Status)) {\r
882 Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID**)&LoadFile);\r
883 if (!EFI_ERROR (Status)) {\r
884 //\r
885 // Call LoadFile with the correct buffer size\r
886 //\r
887 ImageBufferSize = 0;\r
888 ImageBuffer = NULL;\r
889 Status = LoadFile->LoadFile (\r
890 LoadFile,\r
891 DevicePathNode,\r
892 BootPolicy,\r
893 &ImageBufferSize,\r
894 ImageBuffer\r
895 );\r
896 if (Status == EFI_BUFFER_TOO_SMALL) {\r
897 ImageBuffer = AllocatePool (ImageBufferSize);\r
898 if (ImageBuffer == NULL) {\r
899 Status = EFI_OUT_OF_RESOURCES;\r
900 } else {\r
901 Status = LoadFile->LoadFile (\r
902 LoadFile,\r
903 DevicePathNode,\r
904 BootPolicy,\r
905 &ImageBufferSize,\r
906 ImageBuffer\r
907 );\r
908 }\r
909 }\r
910 }\r
911 }\r
912\r
913Finish:\r
914\r
915 if (EFI_ERROR (Status)) {\r
916 if (ImageBuffer != NULL) {\r
917 FreePool (ImageBuffer);\r
918 ImageBuffer = NULL;\r
919 }\r
920 *FileSize = 0;\r
921 } else {\r
922 *FileSize = ImageBufferSize;\r
923 }\r
924\r
925 FreePool (OrigDevicePathNode);\r
926\r
927 return ImageBuffer;\r
928}\r