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 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_VOLUME2_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
;
98 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, &FilePath
, DeviceHandle
);
100 Status
= EFI_SUCCESS
;
102 Status
= EFI_LOAD_ERROR
;
108 // Make sure FilePath is valid
110 if (FilePath
== NULL
) {
111 return EFI_INVALID_PARAMETER
;
115 // Check to see if it's in a Firmware Volume
117 FwVolFilePathNode
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*)FilePath
;
118 Status
= CoreDevicePathToInterface (
119 &gEfiFirmwareVolume2ProtocolGuid
,
120 (EFI_DEVICE_PATH_PROTOCOL
**)&FwVolFilePathNode
,
124 if (!EFI_ERROR (Status
)) {
126 // For FwVol File system there is only a single file name that is a GUID.
128 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode (FwVolFilePathNode
);
129 if (NameGuid
!= NULL
) {
131 SectionType
= EFI_SECTION_PE32
;
133 Status
= FwVol
->ReadSection (
138 (VOID
**)&Pe32Buffer
,
142 if (EFI_ERROR (Status
)) {
144 // Try a raw file, since a PE32 SECTION does not exist
146 if (Pe32Buffer
!= NULL
) {
147 CoreFreePool (Pe32Buffer
);
148 *AuthenticationStatus
= 0;
151 Status
= FwVol
->ReadFile (
154 (VOID
**)&Pe32Buffer
,
162 if (!EFI_ERROR (Status
)) {
164 // One of the reads passed so we are done
166 ImageFileHandle
->Source
= Pe32Buffer
;
167 ImageFileHandle
->SourceSize
= Pe32BufferSize
;
168 ImageFileHandle
->FreeBuffer
= TRUE
;
175 // Attempt to access the file via a file system interface
177 FilePathNode
= (FILEPATH_DEVICE_PATH
*) FilePath
;
178 Status
= CoreDevicePathToInterface (
179 &gEfiSimpleFileSystemProtocolGuid
,
180 (EFI_DEVICE_PATH_PROTOCOL
**)&FilePathNode
,
184 if (!EFI_ERROR (Status
)) {
186 // Open the Volume to get the File System handle
188 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
189 if (!EFI_ERROR (Status
)) {
192 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
193 // directory information and filename can be seperate. The goal is to inch
194 // our way down each device path node and close the previous node
196 while (!IsDevicePathEnd (&FilePathNode
->Header
)) {
197 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
198 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
) {
199 Status
= EFI_UNSUPPORTED
;
202 if (EFI_ERROR (Status
)) {
204 // Exit loop on Error
209 LastHandle
= FileHandle
;
211 Status
= LastHandle
->Open (
214 FilePathNode
->PathName
,
220 // Close the previous node
222 LastHandle
->Close (LastHandle
);
224 FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
);
227 if (!EFI_ERROR (Status
)) {
229 // We have found the file. Now we need to read it. Before we can read the file we need to
230 // figure out how big the file is.
233 FileInfoSize
= sizeof (EFI_FILE_INFO
);
234 while (CoreGrowBuffer (&Status
, (VOID
**)&FileInfo
, FileInfoSize
)) {
236 // Automatically allocate buffer of the correct size and make the call
238 Status
= FileHandle
->GetInfo (
245 if (!EFI_ERROR (Status
)) {
247 // Allocate space for the file
249 ImageFileHandle
->Source
= CoreAllocateBootServicesPool ((UINTN
)FileInfo
->FileSize
);
250 if (ImageFileHandle
->Source
!= NULL
) {
252 // Read the file into the buffer we allocated
254 ImageFileHandle
->SourceSize
= (UINTN
)FileInfo
->FileSize
;
255 ImageFileHandle
->FreeBuffer
= TRUE
;
256 Status
= FileHandle
->Read (FileHandle
, &ImageFileHandle
->SourceSize
, ImageFileHandle
->Source
);
259 // Close the file since we are done
261 FileHandle
->Close (FileHandle
);
263 Status
= EFI_OUT_OF_RESOURCES
;
274 // Try LoadFile style
277 TempFilePath
= FilePath
;
278 Status
= CoreDevicePathToInterface (
279 &gEfiLoadFileProtocolGuid
,
284 if (!EFI_ERROR (Status
)) {
286 // Call LoadFile with the correct buffer size
288 while (CoreGrowBuffer (&Status
, (VOID
**)&ImageFileHandle
->Source
, ImageFileHandle
->SourceSize
)) {
289 Status
= LoadFile
->LoadFile (
293 &ImageFileHandle
->SourceSize
,
294 ImageFileHandle
->Source
297 // If success or other error happens, stop loop
299 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
304 if (!EFI_ERROR (Status
) || Status
== EFI_ALREADY_STARTED
) {
305 ImageFileHandle
->FreeBuffer
= TRUE
;
311 // Nothing else to try
313 DEBUG ((EFI_D_LOAD
|EFI_D_WARN
, "CoreOpenImageFile: Device did not support a known load protocol\n"));
314 Status
= EFI_NOT_FOUND
;
319 // If the file was not accessed, clean up
321 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
322 if (ImageFileHandle
->FreeBuffer
) {
324 // Free the source buffer if we allocated it
326 CoreFreePool (ImageFileHandle
->Source
);
340 IN OUT UINTN
*ReadSize
,
347 Read image file (specified by UserHandle) into user specified buffer with specified offset
352 UserHandle - Image file handle
354 Offset - Offset to the source file
356 ReadSize - For input, pointer of size to read;
357 For output, pointer of size actually read.
359 Buffer - Buffer to write into
363 EFI_SUCCESS - Successfully read the specified part of file into buffer.
368 IMAGE_FILE_HANDLE
*FHand
;
370 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
371 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
374 // Move data from our local copy of the file
376 EndPosition
= Offset
+ *ReadSize
;
377 if (EndPosition
> FHand
->SourceSize
) {
378 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
380 if (Offset
>= FHand
->SourceSize
) {
384 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);
389 CoreDevicePathToInterface (
390 IN EFI_GUID
*Protocol
,
391 IN EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
392 OUT VOID
**Interface
,
393 OUT EFI_HANDLE
*Handle
399 Search a handle to a device on a specified device path that supports a specified protocol,
400 interface of that protocol on that handle is another output.
404 Protocol - The protocol to search for
406 FilePath - The specified device path
408 Interface - Interface of the protocol on the handle
410 Handle - The handle to the device on the specified device path that supports the protocol.
420 Status
= CoreLocateDevicePath (Protocol
, FilePath
, Handle
);
421 if (!EFI_ERROR (Status
)) {
422 Status
= CoreHandleProtocol (*Handle
, Protocol
, Interface
);
429 IN OUT EFI_STATUS
*Status
,
430 IN OUT VOID
**Buffer
,
437 Helper function called as part of the code needed
438 to allocate the proper sized buffer for various
443 Status - Current status
445 Buffer - Current allocated buffer, or NULL
447 BufferSize - Current buffer size needed
451 TRUE - if the buffer was reallocated and the caller
452 should try the API again.
454 FALSE - buffer could not be allocated and the caller
455 should not try the API again.
463 // If this is an initial request, buffer will be null with a new buffer size
465 if (*Buffer
== NULL
) {
466 *Status
= EFI_BUFFER_TOO_SMALL
;
469 if (BufferSize
== 0) {
474 // If the status code is "buffer too small", resize the buffer
477 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
478 if (*Buffer
!= NULL
) {
479 CoreFreePool (*Buffer
);
482 *Buffer
= CoreAllocateBootServicesPool (BufferSize
);
483 if (*Buffer
!= NULL
) {
486 *Status
= EFI_OUT_OF_RESOURCES
;
491 // If there's an error, free the buffer
493 if ((!TryAgain
) && (EFI_ERROR (*Status
)) && (*Buffer
)) {
494 CoreFreePool (*Buffer
);