3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
30 IN BOOLEAN BootPolicy
,
31 IN VOID
*SourceBuffer OPTIONAL
,
33 IN OUT EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
34 OUT EFI_HANDLE
*DeviceHandle
,
35 IN IMAGE_FILE_HANDLE
*ImageFileHandle
,
36 OUT UINT32
*AuthenticationStatus
42 Opens a file for (simple) reading. The simple read abstraction
43 will access the file either from a memory copy, from a file
44 system interface, or from the load file interface.
48 BootPolicy - Policy for Open Image File.
49 SourceBuffer - Pointer to the memory location containing copy
50 of the image to be loaded.
51 SourceSize - The size in bytes of SourceBuffer.
52 FilePath - The specific file path from which the image is loaded
53 DeviceHandle - Pointer to the return device handle.
54 ImageFileHandle - Pointer to the image file handle.
55 AuthenticationStatus - Pointer to a caller-allocated UINT32 in which the authentication status is returned.
59 EFI_SUCCESS - Image file successfully opened.
61 EFI_LOAD_ERROR - If the caller passed a copy of the file, and SourceSize is 0.
63 EFI_INVALID_PARAMETER - File path is not valid.
65 EFI_NOT_FOUND - File not found.
70 EFI_DEVICE_PATH_PROTOCOL
*TempFilePath
;
71 FILEPATH_DEVICE_PATH
*FilePathNode
;
72 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*FwVolFilePathNode
;
73 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
74 EFI_FILE_HANDLE FileHandle
;
75 EFI_FILE_HANDLE LastHandle
;
76 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
77 EFI_FIRMWARE_VOLUME_PROTOCOL
*FwVol
;
78 EFI_SECTION_TYPE SectionType
;
82 EFI_FV_FILE_ATTRIBUTES Attrib
;
83 EFI_FILE_INFO
*FileInfo
;
87 *AuthenticationStatus
= 0;
88 ZeroMem (ImageFileHandle
, sizeof (IMAGE_FILE_HANDLE
));
89 ImageFileHandle
->Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
92 // If the caller passed a copy of the file, then just use it
94 if (SourceBuffer
!= NULL
) {
95 ImageFileHandle
->Source
= SourceBuffer
;
96 ImageFileHandle
->SourceSize
= SourceSize
;
101 Status
= EFI_LOAD_ERROR
;
107 // Make sure FilePath is valid
109 if (FilePath
== NULL
) {
110 return EFI_INVALID_PARAMETER
;
114 // Check to see if it's in a Firmware Volume
116 FwVolFilePathNode
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*)FilePath
;
117 Status
= CoreDevicePathToInterface (
118 &gEfiFirmwareVolumeProtocolGuid
,
119 (EFI_DEVICE_PATH_PROTOCOL
**)&FwVolFilePathNode
,
123 if (!EFI_ERROR (Status
)) {
125 // For FwVol File system there is only a single file name that is a GUID.
127 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode (FwVolFilePathNode
);
128 if (NameGuid
!= NULL
) {
130 SectionType
= EFI_SECTION_PE32
;
132 Status
= FwVol
->ReadSection (
137 (VOID
**)&Pe32Buffer
,
141 if (EFI_ERROR (Status
)) {
143 // Try a raw file, since a PE32 SECTION does not exist
145 if (Pe32Buffer
!= NULL
) {
146 CoreFreePool (Pe32Buffer
);
147 *AuthenticationStatus
= 0;
150 Status
= FwVol
->ReadFile (
153 (VOID
**)&Pe32Buffer
,
161 if (!EFI_ERROR (Status
)) {
163 // One of the reads passed so we are done
165 ImageFileHandle
->Source
= Pe32Buffer
;
166 ImageFileHandle
->SourceSize
= Pe32BufferSize
;
167 ImageFileHandle
->FreeBuffer
= TRUE
;
174 // Attempt to access the file via a file system interface
176 FilePathNode
= (FILEPATH_DEVICE_PATH
*) FilePath
;
177 Status
= CoreDevicePathToInterface (
178 &gEfiSimpleFileSystemProtocolGuid
,
179 (EFI_DEVICE_PATH_PROTOCOL
**)&FilePathNode
,
183 if (!EFI_ERROR (Status
)) {
185 // Open the Volume to get the File System handle
187 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
188 if (!EFI_ERROR (Status
)) {
191 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
192 // directory information and filename can be seperate. The goal is to inch
193 // our way down each device path node and close the previous node
195 while (!IsDevicePathEnd (&FilePathNode
->Header
)) {
196 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
197 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
) {
198 Status
= EFI_UNSUPPORTED
;
201 if (EFI_ERROR (Status
)) {
203 // Exit loop on Error
208 LastHandle
= FileHandle
;
210 Status
= LastHandle
->Open (
213 FilePathNode
->PathName
,
219 // Close the previous node
221 LastHandle
->Close (LastHandle
);
223 FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
);
226 if (!EFI_ERROR (Status
)) {
228 // We have found the file. Now we need to read it. Before we can read the file we need to
229 // figure out how big the file is.
232 FileInfoSize
= sizeof (EFI_FILE_INFO
);
233 while (CoreGrowBuffer (&Status
, (VOID
**)&FileInfo
, FileInfoSize
)) {
235 // Automatically allocate buffer of the correct size and make the call
237 Status
= FileHandle
->GetInfo (
244 if (!EFI_ERROR (Status
)) {
246 // Allocate space for the file
248 ImageFileHandle
->Source
= CoreAllocateBootServicesPool ((UINTN
)FileInfo
->FileSize
);
249 if (ImageFileHandle
->Source
!= NULL
) {
251 // Read the file into the buffer we allocated
253 ImageFileHandle
->SourceSize
= (UINTN
)FileInfo
->FileSize
;
254 ImageFileHandle
->FreeBuffer
= TRUE
;
255 Status
= FileHandle
->Read (FileHandle
, &ImageFileHandle
->SourceSize
, ImageFileHandle
->Source
);
258 // Close the file since we are done
260 FileHandle
->Close (FileHandle
);
262 Status
= EFI_OUT_OF_RESOURCES
;
273 // Try LoadFile style
276 TempFilePath
= FilePath
;
277 Status
= CoreDevicePathToInterface (
278 &gEfiLoadFileProtocolGuid
,
283 if (!EFI_ERROR (Status
)) {
285 // Call LoadFile with the correct buffer size
287 while (CoreGrowBuffer (&Status
, (VOID
**)&ImageFileHandle
->Source
, ImageFileHandle
->SourceSize
)) {
288 Status
= LoadFile
->LoadFile (
292 &ImageFileHandle
->SourceSize
,
293 ImageFileHandle
->Source
296 // If success or other error happens, stop loop
298 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
303 if (!EFI_ERROR (Status
) || Status
== EFI_ALREADY_STARTED
) {
304 ImageFileHandle
->FreeBuffer
= TRUE
;
310 // Nothing else to try
312 DEBUG ((EFI_D_LOAD
|EFI_D_WARN
, "CoreOpenImageFile: Device did not support a known load protocol\n"));
313 Status
= EFI_NOT_FOUND
;
318 // If the file was not accessed, clean up
320 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
321 if (ImageFileHandle
->FreeBuffer
) {
323 // Free the source buffer if we allocated it
325 CoreFreePool (ImageFileHandle
->Source
);
339 IN OUT UINTN
*ReadSize
,
346 Read image file (specified by UserHandle) into user specified buffer with specified offset
351 UserHandle - Image file handle
353 Offset - Offset to the source file
355 ReadSize - For input, pointer of size to read;
356 For output, pointer of size actually read.
358 Buffer - Buffer to write into
362 EFI_SUCCESS - Successfully read the specified part of file into buffer.
367 IMAGE_FILE_HANDLE
*FHand
;
369 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
370 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
373 // Move data from our local copy of the file
375 EndPosition
= Offset
+ *ReadSize
;
376 if (EndPosition
> FHand
->SourceSize
) {
377 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
379 if (Offset
>= FHand
->SourceSize
) {
383 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);
388 CoreDevicePathToInterface (
389 IN EFI_GUID
*Protocol
,
390 IN EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
391 OUT VOID
**Interface
,
392 OUT EFI_HANDLE
*Handle
398 Search a handle to a device on a specified device path that supports a specified protocol,
399 interface of that protocol on that handle is another output.
403 Protocol - The protocol to search for
405 FilePath - The specified device path
407 Interface - Interface of the protocol on the handle
409 Handle - The handle to the device on the specified device path that supports the protocol.
419 Status
= CoreLocateDevicePath (Protocol
, FilePath
, Handle
);
420 if (!EFI_ERROR (Status
)) {
421 Status
= CoreHandleProtocol (*Handle
, Protocol
, Interface
);
428 IN OUT EFI_STATUS
*Status
,
429 IN OUT VOID
**Buffer
,
436 Helper function called as part of the code needed
437 to allocate the proper sized buffer for various
442 Status - Current status
444 Buffer - Current allocated buffer, or NULL
446 BufferSize - Current buffer size needed
450 TRUE - if the buffer was reallocated and the caller
451 should try the API again.
453 FALSE - buffer could not be allocated and the caller
454 should not try the API again.
462 // If this is an initial request, buffer will be null with a new buffer size
464 if (*Buffer
== NULL
) {
465 *Status
= EFI_BUFFER_TOO_SMALL
;
468 if (BufferSize
== 0) {
473 // If the status code is "buffer too small", resize the buffer
476 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
477 if (*Buffer
!= NULL
) {
478 CoreFreePool (*Buffer
);
481 *Buffer
= CoreAllocateBootServicesPool (BufferSize
);
482 if (*Buffer
!= NULL
) {
485 *Status
= EFI_OUT_OF_RESOURCES
;
490 // If there's an error, free the buffer
492 if ((!TryAgain
) && (EFI_ERROR (*Status
)) && (*Buffer
)) {
493 CoreFreePool (*Buffer
);