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