]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeServicesLib/DxeServicesLib.c
Update the copyright notice format
[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
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <PiDxe.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/MemoryAllocationLib.h>\r
19#include <Library/UefiBootServicesTableLib.h>\r
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
b75a165d 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
91 @param FvHandle The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
92 @param NameGuid The GUID name of a Firmware File.\r
93 @param SectionType The Firmware Section type.\r
94 @param SectionInstance The instance number of Firmware Section to read from starting from 0.\r
95 @param Buffer On output, Buffer contains the the data read from the section in the Firmware File found.\r
96 @param Size On output, the size of Buffer.\r
97\r
98 @retval EFI_SUCCESS The image is found and data and size is returned.\r
b75a165d
LG
99 @retval EFI_NOT_FOUND The image specified by NameGuid and SectionType can't be found.\r
100 @retval EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
101 @retval EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
102 @retval EFI_ACCESS_DENIED The firmware volume containing the searched Firmware File is configured to disallow reads.\r
166152e8 103 \r
f80b0830 104**/\r
1c280088 105EFI_STATUS\r
eb9dd4d0 106InternalGetSectionFromFv (\r
166152e8 107 IN EFI_HANDLE FvHandle,\r
108 IN CONST EFI_GUID *NameGuid,\r
109 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 110 IN UINTN SectionInstance,\r
166152e8 111 OUT VOID **Buffer,\r
112 OUT UINTN *Size\r
1c280088 113 )\r
114{\r
166152e8 115 EFI_STATUS Status;\r
116 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
117 UINT32 AuthenticationStatus;\r
118\r
8cf43dd7 119 ASSERT (NameGuid != NULL);\r
120 ASSERT (Buffer != NULL);\r
121 ASSERT (Size != NULL);\r
122 \r
166152e8 123 ASSERT (FvHandle != NULL);\r
124\r
125 Status = gBS->HandleProtocol (\r
126 FvHandle,\r
127 &gEfiFirmwareVolume2ProtocolGuid,\r
128 (VOID **) &Fv\r
129 );\r
130 if (EFI_ERROR (Status)) {\r
7ca066f9 131 return EFI_NOT_FOUND;\r
166152e8 132 }\r
1c280088 133\r
134 //\r
135 // Read desired section content in NameGuid file\r
136 //\r
137 *Buffer = NULL;\r
138 *Size = 0;\r
139 Status = Fv->ReadSection (\r
140 Fv,\r
141 NameGuid,\r
142 SectionType,\r
ff197efb 143 SectionInstance,\r
1c280088 144 Buffer,\r
145 Size,\r
146 &AuthenticationStatus\r
147 );\r
148\r
149 if (EFI_ERROR (Status) && (SectionType == EFI_SECTION_TE)) {\r
150 //\r
166152e8 151 // Try reading PE32 section, if the required section is TE type \r
1c280088 152 //\r
153 *Buffer = NULL;\r
154 *Size = 0;\r
155 Status = Fv->ReadSection (\r
156 Fv,\r
157 NameGuid,\r
158 EFI_SECTION_PE32,\r
ff197efb 159 SectionInstance,\r
1c280088 160 Buffer,\r
161 Size,\r
162 &AuthenticationStatus\r
163 );\r
164 }\r
165\r
1c280088 166 return Status;\r
167}\r
168\r
b0d803fe 169\r
b0d803fe 170\r
f80b0830 171/**\r
b75a165d
LG
172 Searches all the availables firmware volumes and returns the first matching FFS section. \r
173\r
174 This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid. \r
175 The order that the firmware volumes is searched is not deterministic. For each FFS file found a search \r
176 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances \r
177 of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer. \r
178 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
179 It is the caller's responsibility to use FreePool() to free the allocated buffer. \r
180 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections \r
181 are retrieved from an FFS file based on SectionType and SectionInstance.\r
182\r
183 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
184 the search will be retried with a section type of EFI_SECTION_PE32.\r
185 This function must be called with a TPL <= TPL_NOTIFY.\r
186\r
187 If NameGuid is NULL, then ASSERT().\r
188 If Buffer is NULL, then ASSERT().\r
ff197efb 189 If Size is NULL, then ASSERT().\r
190\r
f80b0830 191\r
b75a165d
LG
192 @param NameGuid A pointer to to the FFS filename GUID to search for within \r
193 any of the firmware volumes in the platform. \r
194 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.\r
195 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.\r
196 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found. \r
3e5c3238 197 Is it the caller's responsibility to free this buffer using FreePool().\r
b75a165d
LG
198 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
199\r
200 @retval EFI_SUCCESS The specified FFS section was returned.\r
201 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
3e5c3238 202 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.\r
b75a165d
LG
203 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.\r
204 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that \r
205 contains the matching FFS section does not allow reads.\r
f80b0830 206**/\r
b0d803fe 207EFI_STATUS\r
208EFIAPI\r
eb9dd4d0 209GetSectionFromAnyFv (\r
b0d803fe 210 IN CONST EFI_GUID *NameGuid,\r
211 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 212 IN UINTN SectionInstance,\r
b0d803fe 213 OUT VOID **Buffer,\r
214 OUT UINTN *Size\r
215 )\r
216{\r
217 EFI_STATUS Status;\r
218 EFI_HANDLE *HandleBuffer;\r
219 UINTN HandleCount;\r
220 UINTN Index;\r
221 EFI_HANDLE FvHandle;\r
b0d803fe 222\r
223 //\r
224 // Search the FV that contain the caller's FFS first.\r
225 // FV builder can choose to build FFS into the this FV\r
226 // so that this implementation of GetSectionFromAnyFv\r
227 // will locate the FFS faster.\r
228 //\r
166152e8 229 FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
eb9dd4d0 230 Status = InternalGetSectionFromFv (\r
b0d803fe 231 FvHandle,\r
232 NameGuid,\r
233 SectionType,\r
ff197efb 234 SectionInstance,\r
b0d803fe 235 Buffer,\r
236 Size\r
237 );\r
238 if (!EFI_ERROR (Status)) {\r
239 return EFI_SUCCESS;\r
240 }\r
241\r
b0d803fe 242 HandleBuffer = NULL;\r
243 Status = gBS->LocateHandleBuffer (\r
244 ByProtocol,\r
245 &gEfiFirmwareVolume2ProtocolGuid,\r
246 NULL,\r
247 &HandleCount,\r
248 &HandleBuffer\r
249 );\r
250 if (EFI_ERROR (Status)) {\r
251 goto Done;\r
252 }\r
253\r
ff197efb 254 for (Index = 0; Index < HandleCount; Index++) {\r
b0d803fe 255 //\r
256 // Skip the FV that contain the caller's FFS\r
257 //\r
ff197efb 258 if (HandleBuffer[Index] != FvHandle) {\r
eb9dd4d0 259 Status = InternalGetSectionFromFv (\r
ff197efb 260 HandleBuffer[Index], \r
261 NameGuid, \r
262 SectionType, \r
263 SectionInstance,\r
264 Buffer, \r
265 Size\r
266 );\r
267\r
268 if (!EFI_ERROR (Status)) {\r
269 goto Done;\r
270 }\r
b0d803fe 271 }\r
272\r
b0d803fe 273 }\r
274\r
275 if (Index == HandleCount) {\r
276 Status = EFI_NOT_FOUND;\r
277 }\r
278\r
279Done:\r
280 \r
b0d803fe 281 if (HandleBuffer != NULL) { \r
282 FreePool(HandleBuffer);\r
283 }\r
284 return Status;\r
285 \r
286}\r
287\r
f80b0830 288/**\r
b75a165d
LG
289 Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section. \r
290\r
291 This function searches the firmware volume that the currently executing module was loaded \r
292 from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search \r
293 is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance \r
294 instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.\r
295 Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
296 It is the caller's responsibility to use FreePool() to free the allocated buffer. \r
297 See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from \r
298 an FFS file based on SectionType and SectionInstance.\r
299\r
300 If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.\r
301 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
302 the search will be retried with a section type of EFI_SECTION_PE32.\r
303 \r
304 This function must be called with a TPL <= TPL_NOTIFY.\r
305 If NameGuid is NULL, then ASSERT().\r
306 If Buffer is NULL, then ASSERT().\r
ff197efb 307 If Size is NULL, then ASSERT().\r
308\r
b75a165d
LG
309 @param NameGuid A pointer to to the FFS filename GUID to search for within \r
310 the firmware volumes that the currently executing module was loaded from.\r
311 @param SectionType Indicates the FFS section type to search for within the FFS file specified by NameGuid.\r
312 @param SectionInstance Indicates which section instance within the FFS file specified by NameGuid to retrieve.\r
313 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found. \r
3e5c3238 314 Is it the caller's responsibility to free this buffer using FreePool().\r
b75a165d
LG
315 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
316\r
317\r
318 @retval EFI_SUCCESS The specified FFS section was returned.\r
319 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
3e5c3238 320 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.\r
b75a165d
LG
321 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.\r
322 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that \r
323 contains the matching FFS section does not allow reads. \r
f80b0830 324**/\r
b0d803fe 325EFI_STATUS\r
326EFIAPI\r
eb9dd4d0 327GetSectionFromFv (\r
b0d803fe 328 IN CONST EFI_GUID *NameGuid,\r
329 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 330 IN UINTN SectionInstance,\r
b0d803fe 331 OUT VOID **Buffer,\r
332 OUT UINTN *Size\r
333 )\r
334{\r
eb9dd4d0 335 return InternalGetSectionFromFv (\r
336 InternalImageHandleToFvHandle(gImageHandle),\r
337 NameGuid,\r
338 SectionType,\r
339 SectionInstance,\r
340 Buffer,\r
341 Size\r
342 );\r
b0d803fe 343}\r
344\r
345\r
f80b0830 346/**\r
b75a165d
LG
347 Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.\r
348\r
349 This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.\r
350 If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType, \r
351 then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(), \r
352 and the size of the allocated buffer is returned in Size. It is the caller's responsibility \r
353 to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for \r
354 details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.\r
355\r
356 If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.\r
357 If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
358 the search will be retried with a section type of EFI_SECTION_PE32.\r
359 This function must be called with a TPL <= TPL_NOTIFY.\r
360 \r
361 If Buffer is NULL, then ASSERT().\r
ff197efb 362 If Size is NULL, then ASSERT().\r
363\r
b75a165d
LG
364\r
365 @param SectionType Indicates the FFS section type to search for within the FFS file \r
366 that the currently executing module was loaded from.\r
367 @param SectionInstance Indicates which section instance to retrieve within the FFS file \r
368 that the currently executing module was loaded from.\r
369 @param Buffer On output, a pointer to a callee allocated buffer containing the FFS file section that was found. \r
3e5c3238 370 Is it the caller's responsibility to free this buffer using FreePool().\r
b75a165d
LG
371 @param Size On output, a pointer to the size, in bytes, of Buffer.\r
372\r
373 @retval EFI_SUCCESS The specified FFS section was returned.\r
374 @retval EFI_NOT_FOUND The specified FFS section could not be found.\r
3e5c3238 375 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve the matching FFS section.\r
b75a165d
LG
376 @retval EFI_DEVICE_ERROR The FFS section could not be retrieves due to a device error.\r
377 @retval EFI_ACCESS_DENIED The FFS section could not be retrieves because the firmware volume that \r
378 contains the matching FFS section does not allow reads. \r
f80b0830 379 \r
380**/\r
b0d803fe 381EFI_STATUS\r
382EFIAPI\r
eb9dd4d0 383GetSectionFromFfs (\r
b0d803fe 384 IN EFI_SECTION_TYPE SectionType,\r
ff197efb 385 IN UINTN SectionInstance,\r
b0d803fe 386 OUT VOID **Buffer,\r
387 OUT UINTN *Size\r
388 )\r
389{\r
eb9dd4d0 390 return InternalGetSectionFromFv(\r
391 InternalImageHandleToFvHandle(gImageHandle),\r
392 &gEfiCallerIdGuid,\r
393 SectionType,\r
394 SectionInstance,\r
395 Buffer,\r
396 Size\r
397 );\r
b0d803fe 398}\r
399\r
84716614
LG
400\r
401/**\r
402 Get the image file buffer data and buffer size by its device path. \r
403 \r
ea6898b9 404 Access the file either from a firmware volume, from a file system interface, \r
84716614
LG
405 or from the load file interface.\r
406 \r
407 Allocate memory to store the found image. The caller is responsible to free memory.\r
408\r
409 If File is NULL, then NULL is returned.\r
410 If FileSize is NULL, then NULL is returned.\r
411 If AuthenticationStatus is NULL, then NULL is returned.\r
412\r
ea6898b9 413 @param[in] BootPolicy Policy for Open Image File.If TRUE, indicates that the request \r
414 originates from the boot manager, and that the boot manager is\r
415 attempting to load FilePath as a boot selection. If FALSE, \r
416 then FilePath must match an exact file to be loaded.\r
417 @param[in] FilePath Pointer to the device path of the file that is absracted to\r
418 the file buffer.\r
419 @param[out] FileSize Pointer to the size of the abstracted file buffer.\r
420 @param[out] AuthenticationStatus Pointer to a caller-allocated UINT32 in which the authentication\r
421 status is returned.\r
84716614
LG
422\r
423 @retval NULL File is NULL, or FileSize is NULL. Or the file can't be found.\r
424 @retval other The abstracted file buffer. The caller is responsible to free memory.\r
425**/\r
426VOID *\r
427EFIAPI\r
428GetFileBufferByFilePath (\r
429 IN BOOLEAN BootPolicy,\r
430 IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
431 OUT UINTN *FileSize,\r
432 OUT UINT32 *AuthenticationStatus\r
433 )\r
434{\r
435 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
436 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;\r
95dd343a 437 EFI_DEVICE_PATH_PROTOCOL *TempDevicePathNode;\r
84716614
LG
438 EFI_HANDLE Handle;\r
439 EFI_GUID *FvNameGuid;\r
440 EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVol;\r
441 EFI_SECTION_TYPE SectionType;\r
442 UINT8 *ImageBuffer;\r
443 UINTN ImageBufferSize;\r
444 EFI_FV_FILETYPE Type;\r
445 EFI_FV_FILE_ATTRIBUTES Attrib;\r
446 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
447 EFI_FILE_HANDLE FileHandle;\r
448 EFI_FILE_HANDLE LastHandle;\r
449 EFI_FILE_INFO *FileInfo;\r
450 UINTN FileInfoSize;\r
451 EFI_LOAD_FILE_PROTOCOL *LoadFile;\r
452 EFI_LOAD_FILE2_PROTOCOL *LoadFile2;\r
453 EFI_STATUS Status;\r
454\r
455 //\r
456 // Check input File device path.\r
457 //\r
458 if (FilePath == NULL || FileSize == NULL || AuthenticationStatus == NULL) {\r
459 return NULL;\r
460 }\r
461\r
462 //\r
463 // Init local variable\r
464 //\r
95dd343a 465 TempDevicePathNode = NULL;\r
84716614
LG
466 FvNameGuid = NULL;\r
467 FileInfo = NULL;\r
468 FileHandle = NULL;\r
469 ImageBuffer = NULL;\r
470 ImageBufferSize = 0;\r
471 *AuthenticationStatus = 0;\r
472 \r
473 //\r
474 // Copy File Device Path\r
475 //\r
476 OrigDevicePathNode = DuplicateDevicePath (FilePath);\r
477 if (OrigDevicePathNode == NULL) {\r
478 return NULL;\r
479 }\r
480\r
481 //\r
482 // Check whether this device path support FV2 protocol.\r
483 // Is so, this device path may contain a Image.\r
484 //\r
485 DevicePathNode = OrigDevicePathNode;\r
486 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePathNode, &Handle);\r
487 if (!EFI_ERROR (Status)) {\r
488 //\r
489 // For FwVol File system there is only a single file name that is a GUID.\r
490 //\r
491 FvNameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePathNode);\r
492 if (FvNameGuid == NULL) {\r
493 Status = EFI_INVALID_PARAMETER;\r
494 } else {\r
495 //\r
496 // Read image from the firmware file\r
497 //\r
498 Status = gBS->HandleProtocol (Handle, &gEfiFirmwareVolume2ProtocolGuid, (VOID**)&FwVol);\r
499 if (!EFI_ERROR (Status)) {\r
500 SectionType = EFI_SECTION_PE32;\r
501 ImageBuffer = NULL;\r
502 Status = FwVol->ReadSection (\r
503 FwVol,\r
504 FvNameGuid,\r
505 SectionType,\r
506 0,\r
507 (VOID **)&ImageBuffer,\r
508 &ImageBufferSize,\r
509 AuthenticationStatus\r
510 );\r
511 if (EFI_ERROR (Status)) {\r
512 //\r
513 // Try a raw file, since a PE32 SECTION does not exist\r
514 //\r
515 if (ImageBuffer != NULL) {\r
516 FreePool (ImageBuffer);\r
517 *AuthenticationStatus = 0;\r
518 }\r
519 ImageBuffer = NULL;\r
520 Status = FwVol->ReadFile (\r
521 FwVol,\r
522 FvNameGuid,\r
523 (VOID **)&ImageBuffer,\r
524 &ImageBufferSize,\r
525 &Type,\r
526 &Attrib,\r
527 AuthenticationStatus\r
528 );\r
529 }\r
530 }\r
531 }\r
532 goto Finish;\r
533 }\r
534\r
535 //\r
536 // Attempt to access the file via a file system interface\r
537 //\r
538 DevicePathNode = OrigDevicePathNode;\r
539 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathNode, &Handle);\r
540 if (!EFI_ERROR (Status)) {\r
541 Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID**)&Volume);\r
542 if (!EFI_ERROR (Status)) {\r
543 //\r
544 // Open the Volume to get the File System handle\r
545 //\r
546 Status = Volume->OpenVolume (Volume, &FileHandle);\r
547 if (!EFI_ERROR (Status)) {\r
95dd343a
LG
548 //\r
549 // Duplicate the device path to avoid the access to unaligned device path node.\r
550 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r
551 // nodes, It assures the fields in device path nodes are 2 byte aligned.\r
552 //\r
553 TempDevicePathNode = DuplicateDevicePath (DevicePathNode);\r
554 if (TempDevicePathNode == NULL) {\r
555 FileHandle->Close (FileHandle);\r
556 Status = EFI_OUT_OF_RESOURCES;\r
557 goto Finish;\r
558 }\r
84716614
LG
559 //\r
560 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
561 // directory information and filename can be seperate. The goal is to inch\r
562 // our way down each device path node and close the previous node\r
563 //\r
95dd343a 564 DevicePathNode = TempDevicePathNode;\r
84716614
LG
565 while (!IsDevicePathEnd (DevicePathNode) && !EFI_ERROR (Status)) {\r
566 if (DevicePathType (DevicePathNode) != MEDIA_DEVICE_PATH ||\r
567 DevicePathSubType (DevicePathNode) != MEDIA_FILEPATH_DP) {\r
568 Status = EFI_UNSUPPORTED;\r
569 break;\r
570 }\r
571 \r
572 LastHandle = FileHandle;\r
573 FileHandle = NULL;\r
574 \r
575 Status = LastHandle->Open (\r
576 LastHandle,\r
577 &FileHandle,\r
578 ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName,\r
579 EFI_FILE_MODE_READ,\r
580 0\r
581 );\r
582 \r
583 //\r
584 // Close the previous node\r
585 //\r
586 LastHandle->Close (LastHandle);\r
587 \r
588 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
589 }\r
590 \r
591 if (!EFI_ERROR (Status)) {\r
592 //\r
593 // We have found the file. Now we need to read it. Before we can read the file we need to\r
594 // figure out how big the file is.\r
595 //\r
596 FileInfo = NULL;\r
597 FileInfoSize = 0;\r
598 Status = FileHandle->GetInfo (\r
599 FileHandle,\r
600 &gEfiFileInfoGuid,\r
601 &FileInfoSize,\r
602 FileInfo\r
603 );\r
604 \r
605 if (Status == EFI_BUFFER_TOO_SMALL) {\r
606 FileInfo = AllocatePool (FileInfoSize);\r
607 if (FileInfo == NULL) {\r
608 Status = EFI_OUT_OF_RESOURCES;\r
609 } else {\r
610 Status = FileHandle->GetInfo (\r
611 FileHandle,\r
612 &gEfiFileInfoGuid,\r
613 &FileInfoSize,\r
614 FileInfo\r
615 );\r
616 }\r
617 }\r
618 \r
e191bb14 619 if (!EFI_ERROR (Status) && (FileInfo != NULL)) {\r
84716614
LG
620 //\r
621 // Allocate space for the file\r
622 //\r
623 ImageBuffer = AllocatePool ((UINTN)FileInfo->FileSize);\r
624 if (ImageBuffer == NULL) {\r
625 Status = EFI_OUT_OF_RESOURCES;\r
626 } else {\r
627 //\r
628 // Read the file into the buffer we allocated\r
629 //\r
630 ImageBufferSize = (UINTN)FileInfo->FileSize;\r
631 Status = FileHandle->Read (FileHandle, &ImageBufferSize, ImageBuffer);\r
632 }\r
633 }\r
634 }\r
635 //\r
95dd343a 636 // Close the file and Free FileInfo and TempDevicePathNode since we are done\r
84716614
LG
637 // \r
638 if (FileInfo != NULL) {\r
639 FreePool (FileInfo);\r
640 }\r
641 if (FileHandle != NULL) {\r
642 FileHandle->Close (FileHandle);\r
643 }\r
95dd343a 644 FreePool (TempDevicePathNode);\r
84716614
LG
645 }\r
646 }\r
647 goto Finish;\r
648 }\r
649\r
650 //\r
651 // Attempt to access the file via LoadFile2 interface\r
652 //\r
653 if (!BootPolicy) {\r
654 DevicePathNode = OrigDevicePathNode;\r
655 Status = gBS->LocateDevicePath (&gEfiLoadFile2ProtocolGuid, &DevicePathNode, &Handle);\r
656 if (!EFI_ERROR (Status)) {\r
657 Status = gBS->HandleProtocol (Handle, &gEfiLoadFile2ProtocolGuid, (VOID**)&LoadFile2);\r
658 if (!EFI_ERROR (Status)) {\r
659 //\r
660 // Call LoadFile2 with the correct buffer size\r
661 //\r
662 ImageBufferSize = 0;\r
663 ImageBuffer = NULL;\r
664 Status = LoadFile2->LoadFile (\r
665 LoadFile2,\r
666 DevicePathNode,\r
667 FALSE,\r
668 &ImageBufferSize,\r
669 ImageBuffer\r
670 );\r
671 if (Status == EFI_BUFFER_TOO_SMALL) {\r
672 ImageBuffer = AllocatePool (ImageBufferSize);\r
673 if (ImageBuffer == NULL) {\r
674 Status = EFI_OUT_OF_RESOURCES;\r
675 } else {\r
676 Status = LoadFile2->LoadFile (\r
677 LoadFile2,\r
678 DevicePathNode,\r
c083f7ca 679 FALSE,\r
84716614
LG
680 &ImageBufferSize,\r
681 ImageBuffer\r
682 );\r
683 }\r
684 }\r
685 }\r
686 goto Finish;\r
687 }\r
688 }\r
689\r
690 //\r
691 // Attempt to access the file via LoadFile interface\r
692 //\r
693 DevicePathNode = OrigDevicePathNode;\r
694 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &DevicePathNode, &Handle);\r
695 if (!EFI_ERROR (Status)) {\r
696 Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID**)&LoadFile);\r
697 if (!EFI_ERROR (Status)) {\r
698 //\r
699 // Call LoadFile with the correct buffer size\r
700 //\r
701 ImageBufferSize = 0;\r
702 ImageBuffer = NULL;\r
703 Status = LoadFile->LoadFile (\r
704 LoadFile,\r
705 DevicePathNode,\r
706 BootPolicy,\r
707 &ImageBufferSize,\r
708 ImageBuffer\r
709 );\r
710 if (Status == EFI_BUFFER_TOO_SMALL) {\r
711 ImageBuffer = AllocatePool (ImageBufferSize);\r
712 if (ImageBuffer == NULL) {\r
713 Status = EFI_OUT_OF_RESOURCES;\r
714 } else {\r
715 Status = LoadFile->LoadFile (\r
716 LoadFile,\r
717 DevicePathNode,\r
718 BootPolicy,\r
719 &ImageBufferSize,\r
720 ImageBuffer\r
721 );\r
722 }\r
723 }\r
724 }\r
725 }\r
726\r
727Finish:\r
728\r
729 if (EFI_ERROR (Status)) {\r
730 if (ImageBuffer != NULL) {\r
731 FreePool (ImageBuffer);\r
732 ImageBuffer = NULL;\r
733 }\r
734 *FileSize = 0;\r
735 } else {\r
736 *FileSize = ImageBufferSize;\r
737 }\r
738\r
739 FreePool (OrigDevicePathNode);\r
740\r
741 return ImageBuffer;\r
742}\r