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