2 Handle services to image file.
4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Opens a file for (simple) reading. The simple read abstraction
20 will access the file either from a memory copy, from a file
21 system interface, or from the load file interface.
23 @param BootPolicy Policy for Open Image File.
24 @param SourceBuffer Pointer to the memory location containing copy
25 of the image to be loaded.
26 @param SourceSize The size in bytes of SourceBuffer.
27 @param FilePath The specific file path from which the image is
29 @param DeviceHandle Pointer to the return device handle.
30 @param ImageFileHandle Pointer to the image file handle.
31 @param AuthenticationStatus Pointer to a caller-allocated UINT32 in which
32 the authentication status is returned.
34 @retval EFI_SUCCESS Image file successfully opened.
35 @retval EFI_LOAD_ERROR If the caller passed a copy of the file, and
37 @retval EFI_INVALID_PARAMETER File path is not valid.
38 @retval EFI_NOT_FOUND File not found.
43 IN BOOLEAN BootPolicy
,
44 IN VOID
*SourceBuffer OPTIONAL
,
46 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
47 OUT EFI_HANDLE
*DeviceHandle
,
48 IN IMAGE_FILE_HANDLE
*ImageFileHandle
,
49 OUT UINT32
*AuthenticationStatus
53 EFI_DEVICE_PATH_PROTOCOL
*TempFilePath
;
54 FILEPATH_DEVICE_PATH
*FilePathNode
;
55 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*FwVolFilePathNode
;
56 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
57 EFI_FILE_HANDLE FileHandle
;
58 EFI_FILE_HANDLE LastHandle
;
59 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
60 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
61 EFI_SECTION_TYPE SectionType
;
65 EFI_FV_FILE_ATTRIBUTES Attrib
;
66 EFI_FILE_INFO
*FileInfo
;
69 FILEPATH_DEVICE_PATH
*OriginalFilePathNode
;
71 OriginalFilePathNode
= NULL
;
72 *AuthenticationStatus
= 0;
73 ZeroMem (ImageFileHandle
, sizeof (IMAGE_FILE_HANDLE
));
74 ImageFileHandle
->Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
77 // If the caller passed a copy of the file, then just use it
79 if (SourceBuffer
!= NULL
) {
80 ImageFileHandle
->Source
= SourceBuffer
;
81 ImageFileHandle
->SourceSize
= SourceSize
;
83 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, FilePath
, DeviceHandle
);
87 Status
= EFI_LOAD_ERROR
;
93 // Make sure FilePath is valid
95 if (*FilePath
== NULL
) {
96 return EFI_INVALID_PARAMETER
;
100 // Check to see if it's in a Firmware Volume
102 FwVolFilePathNode
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) *FilePath
;
103 Status
= CoreDevicePathToInterface (
104 &gEfiFirmwareVolume2ProtocolGuid
,
105 (EFI_DEVICE_PATH_PROTOCOL
**)&FwVolFilePathNode
,
109 if (!EFI_ERROR (Status
)) {
111 // For FwVol File system there is only a single file name that is a GUID.
113 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode (FwVolFilePathNode
);
114 if (NameGuid
!= NULL
) {
116 SectionType
= EFI_SECTION_PE32
;
118 Status
= FwVol
->ReadSection (
123 (VOID
**)&Pe32Buffer
,
127 if (EFI_ERROR (Status
)) {
129 // Try a raw file, since a PE32 SECTION does not exist
131 if (Pe32Buffer
!= NULL
) {
132 CoreFreePool (Pe32Buffer
);
133 *AuthenticationStatus
= 0;
136 Status
= FwVol
->ReadFile (
139 (VOID
**)&Pe32Buffer
,
147 if (!EFI_ERROR (Status
)) {
149 // One of the reads passed so we are done
151 ImageFileHandle
->Source
= Pe32Buffer
;
152 ImageFileHandle
->SourceSize
= Pe32BufferSize
;
153 ImageFileHandle
->FreeBuffer
= TRUE
;
160 // Attempt to access the file via a file system interface
162 FilePathNode
= (FILEPATH_DEVICE_PATH
*) *FilePath
;
163 Status
= CoreDevicePathToInterface (
164 &gEfiSimpleFileSystemProtocolGuid
,
165 (EFI_DEVICE_PATH_PROTOCOL
**)&FilePathNode
,
169 if (!EFI_ERROR (Status
)) {
171 // Open the Volume to get the File System handle
173 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
174 if (!EFI_ERROR (Status
)) {
176 // Duplicate the device path to avoid the access to unaligned device path node.
177 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
178 // nodes, It assures the fields in device path nodes are 2 byte aligned.
180 FilePathNode
= (FILEPATH_DEVICE_PATH
*)CoreDuplicateDevicePath((EFI_DEVICE_PATH_PROTOCOL
*)(UINTN
)FilePathNode
);
181 if (FilePathNode
== NULL
) {
182 FileHandle
->Close (FileHandle
);
183 Status
= EFI_OUT_OF_RESOURCES
;
185 OriginalFilePathNode
= FilePathNode
;
187 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
188 // directory information and filename can be seperate. The goal is to inch
189 // our way down each device path node and close the previous node
191 while (!IsDevicePathEnd (&FilePathNode
->Header
)) {
192 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
193 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
) {
194 Status
= EFI_UNSUPPORTED
;
197 if (EFI_ERROR (Status
)) {
199 // Exit loop on Error
204 LastHandle
= FileHandle
;
207 Status
= LastHandle
->Open (
210 FilePathNode
->PathName
,
216 // Close the previous node
218 LastHandle
->Close (LastHandle
);
220 FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
);
223 // Free the allocated memory pool
225 CoreFreePool(OriginalFilePathNode
);
228 if (!EFI_ERROR (Status
)) {
230 // We have found the file. Now we need to read it. Before we can read the file we need to
231 // figure out how big the file is.
234 FileInfoSize
= sizeof (EFI_FILE_INFO
);
235 while (CoreGrowBuffer (&Status
, (VOID
**)&FileInfo
, FileInfoSize
)) {
237 // Automatically allocate buffer of the correct size and make the call
239 Status
= FileHandle
->GetInfo (
246 if (!EFI_ERROR (Status
)) {
248 // Allocate space for the file
250 ImageFileHandle
->Source
= CoreAllocateBootServicesPool ((UINTN
)FileInfo
->FileSize
);
251 if (ImageFileHandle
->Source
!= NULL
) {
253 // Read the file into the buffer we allocated
255 ImageFileHandle
->SourceSize
= (UINTN
)FileInfo
->FileSize
;
256 ImageFileHandle
->FreeBuffer
= TRUE
;
257 Status
= FileHandle
->Read (FileHandle
, &ImageFileHandle
->SourceSize
, ImageFileHandle
->Source
);
260 // Close the file since we are done
262 FileHandle
->Close (FileHandle
);
264 Status
= EFI_OUT_OF_RESOURCES
;
275 // Try LoadFile style
278 TempFilePath
= *FilePath
;
279 Status
= CoreDevicePathToInterface (
280 &gEfiLoadFileProtocolGuid
,
285 if (!EFI_ERROR (Status
)) {
287 // Call LoadFile with the correct buffer size
289 while (CoreGrowBuffer (&Status
, (VOID
**)&ImageFileHandle
->Source
, ImageFileHandle
->SourceSize
)) {
290 Status
= LoadFile
->LoadFile (
294 &ImageFileHandle
->SourceSize
,
295 ImageFileHandle
->Source
298 // If success or other error happens, stop loop
300 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
305 if (!EFI_ERROR (Status
) || Status
== EFI_ALREADY_STARTED
) {
306 ImageFileHandle
->FreeBuffer
= TRUE
;
312 // Nothing else to try
314 DEBUG ((DEBUG_LOAD
|DEBUG_WARN
, "CoreOpenImageFile: Device did not support a known load protocol\n"));
315 Status
= EFI_NOT_FOUND
;
320 // If the file was not accessed, clean up
322 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
323 if (ImageFileHandle
->FreeBuffer
) {
325 // Free the source buffer if we allocated it
327 CoreFreePool (ImageFileHandle
->Source
);
338 Read image file (specified by UserHandle) into user specified buffer with specified offset
341 @param UserHandle Image file handle
342 @param Offset Offset to the source file
343 @param ReadSize For input, pointer of size to read; For output,
344 pointer of size actually read.
345 @param Buffer Buffer to write into
347 @retval EFI_SUCCESS Successfully read the specified part of file
356 IN OUT UINTN
*ReadSize
,
361 IMAGE_FILE_HANDLE
*FHand
;
363 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
364 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
367 // Move data from our local copy of the file
369 EndPosition
= Offset
+ *ReadSize
;
370 if (EndPosition
> FHand
->SourceSize
) {
371 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
373 if (Offset
>= FHand
->SourceSize
) {
377 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);
383 Search a handle to a device on a specified device path that supports a specified protocol,
384 interface of that protocol on that handle is another output.
386 @param Protocol The protocol to search for
387 @param FilePath The specified device path
388 @param Interface Interface of the protocol on the handle
389 @param Handle The handle to the device on the specified device
390 path that supports the protocol.
396 CoreDevicePathToInterface (
397 IN EFI_GUID
*Protocol
,
398 IN EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
399 OUT VOID
**Interface
,
400 OUT EFI_HANDLE
*Handle
405 Status
= CoreLocateDevicePath (Protocol
, FilePath
, Handle
);
406 if (!EFI_ERROR (Status
)) {
407 Status
= CoreHandleProtocol (*Handle
, Protocol
, Interface
);
414 Helper function called as part of the code needed
415 to allocate the proper sized buffer for various
418 @param Status Current status
419 @param Buffer Current allocated buffer, or NULL
420 @param BufferSize Current buffer size needed
422 @retval TRUE if the buffer was reallocated and the caller
423 should try the API again.
424 @retval FALSE buffer could not be allocated and the caller
425 should not try the API again.
430 IN OUT EFI_STATUS
*Status
,
431 IN OUT VOID
**Buffer
,
439 // If this is an initial request, buffer will be null with a new buffer size
441 if (*Buffer
== NULL
) {
442 *Status
= EFI_BUFFER_TOO_SMALL
;
445 if (BufferSize
== 0) {
450 // If the status code is "buffer too small", resize the buffer
453 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
454 if (*Buffer
!= NULL
) {
455 CoreFreePool (*Buffer
);
458 *Buffer
= CoreAllocateBootServicesPool (BufferSize
);
459 if (*Buffer
!= NULL
) {
462 *Status
= EFI_OUT_OF_RESOURCES
;
467 // If there's an error, free the buffer
469 if ((!TryAgain
) && (EFI_ERROR (*Status
)) && (*Buffer
!= NULL
)) {
470 CoreFreePool (*Buffer
);