]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeServicesLib/DxeServicesLib.c
Add more check for local FileInfo variable before it is used.
[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
84716614 5 Copyright (c) 2007 - 2009, Intel Corporation<BR>\r
1c280088 6 All rights reserved. This program and the accompanying materials\r
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
404 Access the file either from a a firmware volume, from a file system interface, \r
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
413 @param[in] BootPolicy \r
414 Policy for Open Image File.If TRUE, indicates that the request \r
415 originates from the boot manager, and that the boot manager is\r
416 attempting to load FilePath as a boot selection. If FALSE, \r
417 then FilePath must match an exact file to be loaded.\r
418 @param[in] FilePath Pointer to the device path of the file that is absracted to the file buffer.\r
419 @param[out] FileSize Pointer to the size of the abstracted file buffer.\r
420 @param[out] AuthenticationStatus \r
421 Pointer to a caller-allocated UINT32 in which\r
422 the authentication status is returned.\r
423\r
424 @retval NULL File is NULL, or FileSize is NULL. Or the file can't be found.\r
425 @retval other The abstracted file buffer. The caller is responsible to free memory.\r
426**/\r
427VOID *\r
428EFIAPI\r
429GetFileBufferByFilePath (\r
430 IN BOOLEAN BootPolicy,\r
431 IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
432 OUT UINTN *FileSize,\r
433 OUT UINT32 *AuthenticationStatus\r
434 )\r
435{\r
436 EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;\r
437 EFI_DEVICE_PATH_PROTOCOL *OrigDevicePathNode;\r
95dd343a 438 EFI_DEVICE_PATH_PROTOCOL *TempDevicePathNode;\r
84716614
LG
439 EFI_HANDLE Handle;\r
440 EFI_GUID *FvNameGuid;\r
441 EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVol;\r
442 EFI_SECTION_TYPE SectionType;\r
443 UINT8 *ImageBuffer;\r
444 UINTN ImageBufferSize;\r
445 EFI_FV_FILETYPE Type;\r
446 EFI_FV_FILE_ATTRIBUTES Attrib;\r
447 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
448 EFI_FILE_HANDLE FileHandle;\r
449 EFI_FILE_HANDLE LastHandle;\r
450 EFI_FILE_INFO *FileInfo;\r
451 UINTN FileInfoSize;\r
452 EFI_LOAD_FILE_PROTOCOL *LoadFile;\r
453 EFI_LOAD_FILE2_PROTOCOL *LoadFile2;\r
454 EFI_STATUS Status;\r
455\r
456 //\r
457 // Check input File device path.\r
458 //\r
459 if (FilePath == NULL || FileSize == NULL || AuthenticationStatus == NULL) {\r
460 return NULL;\r
461 }\r
462\r
463 //\r
464 // Init local variable\r
465 //\r
95dd343a 466 TempDevicePathNode = NULL;\r
84716614
LG
467 FvNameGuid = NULL;\r
468 FileInfo = NULL;\r
469 FileHandle = NULL;\r
470 ImageBuffer = NULL;\r
471 ImageBufferSize = 0;\r
472 *AuthenticationStatus = 0;\r
473 \r
474 //\r
475 // Copy File Device Path\r
476 //\r
477 OrigDevicePathNode = DuplicateDevicePath (FilePath);\r
478 if (OrigDevicePathNode == NULL) {\r
479 return NULL;\r
480 }\r
481\r
482 //\r
483 // Check whether this device path support FV2 protocol.\r
484 // Is so, this device path may contain a Image.\r
485 //\r
486 DevicePathNode = OrigDevicePathNode;\r
487 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePathNode, &Handle);\r
488 if (!EFI_ERROR (Status)) {\r
489 //\r
490 // For FwVol File system there is only a single file name that is a GUID.\r
491 //\r
492 FvNameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePathNode);\r
493 if (FvNameGuid == NULL) {\r
494 Status = EFI_INVALID_PARAMETER;\r
495 } else {\r
496 //\r
497 // Read image from the firmware file\r
498 //\r
499 Status = gBS->HandleProtocol (Handle, &gEfiFirmwareVolume2ProtocolGuid, (VOID**)&FwVol);\r
500 if (!EFI_ERROR (Status)) {\r
501 SectionType = EFI_SECTION_PE32;\r
502 ImageBuffer = NULL;\r
503 Status = FwVol->ReadSection (\r
504 FwVol,\r
505 FvNameGuid,\r
506 SectionType,\r
507 0,\r
508 (VOID **)&ImageBuffer,\r
509 &ImageBufferSize,\r
510 AuthenticationStatus\r
511 );\r
512 if (EFI_ERROR (Status)) {\r
513 //\r
514 // Try a raw file, since a PE32 SECTION does not exist\r
515 //\r
516 if (ImageBuffer != NULL) {\r
517 FreePool (ImageBuffer);\r
518 *AuthenticationStatus = 0;\r
519 }\r
520 ImageBuffer = NULL;\r
521 Status = FwVol->ReadFile (\r
522 FwVol,\r
523 FvNameGuid,\r
524 (VOID **)&ImageBuffer,\r
525 &ImageBufferSize,\r
526 &Type,\r
527 &Attrib,\r
528 AuthenticationStatus\r
529 );\r
530 }\r
531 }\r
532 }\r
533 goto Finish;\r
534 }\r
535\r
536 //\r
537 // Attempt to access the file via a file system interface\r
538 //\r
539 DevicePathNode = OrigDevicePathNode;\r
540 Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathNode, &Handle);\r
541 if (!EFI_ERROR (Status)) {\r
542 Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID**)&Volume);\r
543 if (!EFI_ERROR (Status)) {\r
544 //\r
545 // Open the Volume to get the File System handle\r
546 //\r
547 Status = Volume->OpenVolume (Volume, &FileHandle);\r
548 if (!EFI_ERROR (Status)) {\r
95dd343a
LG
549 //\r
550 // Duplicate the device path to avoid the access to unaligned device path node.\r
551 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r
552 // nodes, It assures the fields in device path nodes are 2 byte aligned.\r
553 //\r
554 TempDevicePathNode = DuplicateDevicePath (DevicePathNode);\r
555 if (TempDevicePathNode == NULL) {\r
556 FileHandle->Close (FileHandle);\r
557 Status = EFI_OUT_OF_RESOURCES;\r
558 goto Finish;\r
559 }\r
84716614
LG
560 //\r
561 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
562 // directory information and filename can be seperate. The goal is to inch\r
563 // our way down each device path node and close the previous node\r
564 //\r
95dd343a 565 DevicePathNode = TempDevicePathNode;\r
84716614
LG
566 while (!IsDevicePathEnd (DevicePathNode) && !EFI_ERROR (Status)) {\r
567 if (DevicePathType (DevicePathNode) != MEDIA_DEVICE_PATH ||\r
568 DevicePathSubType (DevicePathNode) != MEDIA_FILEPATH_DP) {\r
569 Status = EFI_UNSUPPORTED;\r
570 break;\r
571 }\r
572 \r
573 LastHandle = FileHandle;\r
574 FileHandle = NULL;\r
575 \r
576 Status = LastHandle->Open (\r
577 LastHandle,\r
578 &FileHandle,\r
579 ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName,\r
580 EFI_FILE_MODE_READ,\r
581 0\r
582 );\r
583 \r
584 //\r
585 // Close the previous node\r
586 //\r
587 LastHandle->Close (LastHandle);\r
588 \r
589 DevicePathNode = NextDevicePathNode (DevicePathNode);\r
590 }\r
591 \r
592 if (!EFI_ERROR (Status)) {\r
593 //\r
594 // We have found the file. Now we need to read it. Before we can read the file we need to\r
595 // figure out how big the file is.\r
596 //\r
597 FileInfo = NULL;\r
598 FileInfoSize = 0;\r
599 Status = FileHandle->GetInfo (\r
600 FileHandle,\r
601 &gEfiFileInfoGuid,\r
602 &FileInfoSize,\r
603 FileInfo\r
604 );\r
605 \r
606 if (Status == EFI_BUFFER_TOO_SMALL) {\r
607 FileInfo = AllocatePool (FileInfoSize);\r
608 if (FileInfo == NULL) {\r
609 Status = EFI_OUT_OF_RESOURCES;\r
610 } else {\r
611 Status = FileHandle->GetInfo (\r
612 FileHandle,\r
613 &gEfiFileInfoGuid,\r
614 &FileInfoSize,\r
615 FileInfo\r
616 );\r
617 }\r
618 }\r
619 \r
e191bb14 620 if (!EFI_ERROR (Status) && (FileInfo != NULL)) {\r
84716614
LG
621 //\r
622 // Allocate space for the file\r
623 //\r
624 ImageBuffer = AllocatePool ((UINTN)FileInfo->FileSize);\r
625 if (ImageBuffer == NULL) {\r
626 Status = EFI_OUT_OF_RESOURCES;\r
627 } else {\r
628 //\r
629 // Read the file into the buffer we allocated\r
630 //\r
631 ImageBufferSize = (UINTN)FileInfo->FileSize;\r
632 Status = FileHandle->Read (FileHandle, &ImageBufferSize, ImageBuffer);\r
633 }\r
634 }\r
635 }\r
636 //\r
95dd343a 637 // Close the file and Free FileInfo and TempDevicePathNode since we are done\r
84716614
LG
638 // \r
639 if (FileInfo != NULL) {\r
640 FreePool (FileInfo);\r
641 }\r
642 if (FileHandle != NULL) {\r
643 FileHandle->Close (FileHandle);\r
644 }\r
95dd343a 645 FreePool (TempDevicePathNode);\r
84716614
LG
646 }\r
647 }\r
648 goto Finish;\r
649 }\r
650\r
651 //\r
652 // Attempt to access the file via LoadFile2 interface\r
653 //\r
654 if (!BootPolicy) {\r
655 DevicePathNode = OrigDevicePathNode;\r
656 Status = gBS->LocateDevicePath (&gEfiLoadFile2ProtocolGuid, &DevicePathNode, &Handle);\r
657 if (!EFI_ERROR (Status)) {\r
658 Status = gBS->HandleProtocol (Handle, &gEfiLoadFile2ProtocolGuid, (VOID**)&LoadFile2);\r
659 if (!EFI_ERROR (Status)) {\r
660 //\r
661 // Call LoadFile2 with the correct buffer size\r
662 //\r
663 ImageBufferSize = 0;\r
664 ImageBuffer = NULL;\r
665 Status = LoadFile2->LoadFile (\r
666 LoadFile2,\r
667 DevicePathNode,\r
668 FALSE,\r
669 &ImageBufferSize,\r
670 ImageBuffer\r
671 );\r
672 if (Status == EFI_BUFFER_TOO_SMALL) {\r
673 ImageBuffer = AllocatePool (ImageBufferSize);\r
674 if (ImageBuffer == NULL) {\r
675 Status = EFI_OUT_OF_RESOURCES;\r
676 } else {\r
677 Status = LoadFile2->LoadFile (\r
678 LoadFile2,\r
679 DevicePathNode,\r
680 BootPolicy,\r
681 &ImageBufferSize,\r
682 ImageBuffer\r
683 );\r
684 }\r
685 }\r
686 }\r
687 goto Finish;\r
688 }\r
689 }\r
690\r
691 //\r
692 // Attempt to access the file via LoadFile interface\r
693 //\r
694 DevicePathNode = OrigDevicePathNode;\r
695 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &DevicePathNode, &Handle);\r
696 if (!EFI_ERROR (Status)) {\r
697 Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID**)&LoadFile);\r
698 if (!EFI_ERROR (Status)) {\r
699 //\r
700 // Call LoadFile with the correct buffer size\r
701 //\r
702 ImageBufferSize = 0;\r
703 ImageBuffer = NULL;\r
704 Status = LoadFile->LoadFile (\r
705 LoadFile,\r
706 DevicePathNode,\r
707 BootPolicy,\r
708 &ImageBufferSize,\r
709 ImageBuffer\r
710 );\r
711 if (Status == EFI_BUFFER_TOO_SMALL) {\r
712 ImageBuffer = AllocatePool (ImageBufferSize);\r
713 if (ImageBuffer == NULL) {\r
714 Status = EFI_OUT_OF_RESOURCES;\r
715 } else {\r
716 Status = LoadFile->LoadFile (\r
717 LoadFile,\r
718 DevicePathNode,\r
719 BootPolicy,\r
720 &ImageBufferSize,\r
721 ImageBuffer\r
722 );\r
723 }\r
724 }\r
725 }\r
726 }\r
727\r
728Finish:\r
729\r
730 if (EFI_ERROR (Status)) {\r
731 if (ImageBuffer != NULL) {\r
732 FreePool (ImageBuffer);\r
733 ImageBuffer = NULL;\r
734 }\r
735 *FileSize = 0;\r
736 } else {\r
737 *FileSize = ImageBufferSize;\r
738 }\r
739\r
740 FreePool (OrigDevicePathNode);\r
741\r
742 return ImageBuffer;\r
743}\r