3 Handle services to image file.
5 Copyright (c) 2006 - 2008, Intel Corporation
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
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.
20 Opens a file for (simple) reading. The simple read abstraction
21 will access the file either from a memory copy, from a file
22 system interface, or from the load file interface.
24 @param BootPolicy Policy for Open Image File.
25 @param SourceBuffer Pointer to the memory location containing copy
26 of the image to be loaded.
27 @param SourceSize The size in bytes of SourceBuffer.
28 @param FilePath The specific file path from which the image is
30 @param DeviceHandle Pointer to the return device handle.
31 @param ImageFileHandle Pointer to the image file handle.
32 @param AuthenticationStatus Pointer to a caller-allocated UINT32 in which
33 the authentication status is returned.
35 @retval EFI_SUCCESS Image file successfully opened.
36 @retval EFI_LOAD_ERROR If the caller passed a copy of the file, and
38 @retval EFI_INVALID_PARAMETER File path is not valid.
39 @retval EFI_NOT_FOUND File not found.
44 IN BOOLEAN BootPolicy
,
45 IN VOID
*SourceBuffer OPTIONAL
,
47 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
48 OUT EFI_HANDLE
*DeviceHandle
,
49 IN IMAGE_FILE_HANDLE
*ImageFileHandle
,
50 OUT UINT32
*AuthenticationStatus
54 EFI_DEVICE_PATH_PROTOCOL
*TempFilePath
;
55 FILEPATH_DEVICE_PATH
*FilePathNode
;
56 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*FwVolFilePathNode
;
57 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
58 EFI_FILE_HANDLE FileHandle
;
59 EFI_FILE_HANDLE LastHandle
;
60 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
61 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
62 EFI_SECTION_TYPE SectionType
;
66 EFI_FV_FILE_ATTRIBUTES Attrib
;
67 EFI_FILE_INFO
*FileInfo
;
70 FILEPATH_DEVICE_PATH
*OriginalFilePathNode
;
72 OriginalFilePathNode
= NULL
;
73 *AuthenticationStatus
= 0;
74 ZeroMem (ImageFileHandle
, sizeof (IMAGE_FILE_HANDLE
));
75 ImageFileHandle
->Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
78 // If the caller passed a copy of the file, then just use it
80 if (SourceBuffer
!= NULL
) {
81 ImageFileHandle
->Source
= SourceBuffer
;
82 ImageFileHandle
->SourceSize
= SourceSize
;
84 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, FilePath
, DeviceHandle
);
88 Status
= EFI_LOAD_ERROR
;
94 // Make sure FilePath is valid
96 if (*FilePath
== NULL
) {
97 return EFI_INVALID_PARAMETER
;
101 // Check to see if it's in a Firmware Volume
103 FwVolFilePathNode
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) *FilePath
;
104 Status
= CoreDevicePathToInterface (
105 &gEfiFirmwareVolume2ProtocolGuid
,
106 (EFI_DEVICE_PATH_PROTOCOL
**)&FwVolFilePathNode
,
110 if (!EFI_ERROR (Status
)) {
112 // For FwVol File system there is only a single file name that is a GUID.
114 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode (FwVolFilePathNode
);
115 if (NameGuid
!= NULL
) {
117 SectionType
= EFI_SECTION_PE32
;
119 Status
= FwVol
->ReadSection (
124 (VOID
**)&Pe32Buffer
,
128 if (EFI_ERROR (Status
)) {
130 // Try a raw file, since a PE32 SECTION does not exist
132 if (Pe32Buffer
!= NULL
) {
133 CoreFreePool (Pe32Buffer
);
134 *AuthenticationStatus
= 0;
137 Status
= FwVol
->ReadFile (
140 (VOID
**)&Pe32Buffer
,
148 if (!EFI_ERROR (Status
)) {
150 // One of the reads passed so we are done
152 ImageFileHandle
->Source
= Pe32Buffer
;
153 ImageFileHandle
->SourceSize
= Pe32BufferSize
;
154 ImageFileHandle
->FreeBuffer
= TRUE
;
161 // Attempt to access the file via a file system interface
163 FilePathNode
= (FILEPATH_DEVICE_PATH
*) *FilePath
;
164 Status
= CoreDevicePathToInterface (
165 &gEfiSimpleFileSystemProtocolGuid
,
166 (EFI_DEVICE_PATH_PROTOCOL
**)&FilePathNode
,
170 if (!EFI_ERROR (Status
)) {
172 // Open the Volume to get the File System handle
174 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
175 if (!EFI_ERROR (Status
)) {
177 // Duplicate the device path to avoid the access to unaligned device path node.
178 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
179 // nodes, It assures the fields in device path nodes are 2 byte aligned.
181 FilePathNode
= (FILEPATH_DEVICE_PATH
*)CoreDuplicateDevicePath((EFI_DEVICE_PATH_PROTOCOL
*)(UINTN
)FilePathNode
);
182 if (FilePathNode
== NULL
) {
183 FileHandle
->Close (FileHandle
);
184 Status
= EFI_OUT_OF_RESOURCES
;
186 OriginalFilePathNode
= FilePathNode
;
188 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
189 // directory information and filename can be seperate. The goal is to inch
190 // our way down each device path node and close the previous node
192 while (!IsDevicePathEnd (&FilePathNode
->Header
)) {
193 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
194 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
) {
195 Status
= EFI_UNSUPPORTED
;
198 if (EFI_ERROR (Status
)) {
200 // Exit loop on Error
205 LastHandle
= FileHandle
;
208 Status
= LastHandle
->Open (
211 FilePathNode
->PathName
,
217 // Close the previous node
219 LastHandle
->Close (LastHandle
);
221 FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
);
224 // Free the allocated memory pool
226 CoreFreePool(OriginalFilePathNode
);
229 if (!EFI_ERROR (Status
)) {
231 // We have found the file. Now we need to read it. Before we can read the file we need to
232 // figure out how big the file is.
235 FileInfoSize
= sizeof (EFI_FILE_INFO
);
236 while (CoreGrowBuffer (&Status
, (VOID
**)&FileInfo
, FileInfoSize
)) {
238 // Automatically allocate buffer of the correct size and make the call
240 Status
= FileHandle
->GetInfo (
247 if (!EFI_ERROR (Status
)) {
249 // Allocate space for the file
251 ImageFileHandle
->Source
= CoreAllocateBootServicesPool ((UINTN
)FileInfo
->FileSize
);
252 if (ImageFileHandle
->Source
!= NULL
) {
254 // Read the file into the buffer we allocated
256 ImageFileHandle
->SourceSize
= (UINTN
)FileInfo
->FileSize
;
257 ImageFileHandle
->FreeBuffer
= TRUE
;
258 Status
= FileHandle
->Read (FileHandle
, &ImageFileHandle
->SourceSize
, ImageFileHandle
->Source
);
261 // Close the file since we are done
263 FileHandle
->Close (FileHandle
);
265 Status
= EFI_OUT_OF_RESOURCES
;
276 // Try LoadFile style
279 TempFilePath
= *FilePath
;
280 Status
= CoreDevicePathToInterface (
281 &gEfiLoadFileProtocolGuid
,
286 if (!EFI_ERROR (Status
)) {
288 // Call LoadFile with the correct buffer size
290 while (CoreGrowBuffer (&Status
, (VOID
**)&ImageFileHandle
->Source
, ImageFileHandle
->SourceSize
)) {
291 Status
= LoadFile
->LoadFile (
295 &ImageFileHandle
->SourceSize
,
296 ImageFileHandle
->Source
299 // If success or other error happens, stop loop
301 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
306 if (!EFI_ERROR (Status
) || Status
== EFI_ALREADY_STARTED
) {
307 ImageFileHandle
->FreeBuffer
= TRUE
;
313 // Nothing else to try
315 DEBUG ((DEBUG_LOAD
|DEBUG_WARN
, "CoreOpenImageFile: Device did not support a known load protocol\n"));
316 Status
= EFI_NOT_FOUND
;
321 // If the file was not accessed, clean up
323 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
324 if (ImageFileHandle
->FreeBuffer
) {
326 // Free the source buffer if we allocated it
328 CoreFreePool (ImageFileHandle
->Source
);
339 Read image file (specified by UserHandle) into user specified buffer with specified offset
342 @param UserHandle Image file handle
343 @param Offset Offset to the source file
344 @param ReadSize For input, pointer of size to read; For output,
345 pointer of size actually read.
346 @param Buffer Buffer to write into
348 @retval EFI_SUCCESS Successfully read the specified part of file
357 IN OUT UINTN
*ReadSize
,
362 IMAGE_FILE_HANDLE
*FHand
;
364 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
365 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
368 // Move data from our local copy of the file
370 EndPosition
= Offset
+ *ReadSize
;
371 if (EndPosition
> FHand
->SourceSize
) {
372 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
374 if (Offset
>= FHand
->SourceSize
) {
378 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);
384 Search a handle to a device on a specified device path that supports a specified protocol,
385 interface of that protocol on that handle is another output.
387 @param Protocol The protocol to search for
388 @param FilePath The specified device path
389 @param Interface Interface of the protocol on the handle
390 @param Handle The handle to the device on the specified device
391 path that supports the protocol.
397 CoreDevicePathToInterface (
398 IN EFI_GUID
*Protocol
,
399 IN EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
400 OUT VOID
**Interface
,
401 OUT EFI_HANDLE
*Handle
406 Status
= CoreLocateDevicePath (Protocol
, FilePath
, Handle
);
407 if (!EFI_ERROR (Status
)) {
408 Status
= CoreHandleProtocol (*Handle
, Protocol
, Interface
);
415 Helper function called as part of the code needed
416 to allocate the proper sized buffer for various
419 @param Status Current status
420 @param Buffer Current allocated buffer, or NULL
421 @param BufferSize Current buffer size needed
423 @retval TRUE if the buffer was reallocated and the caller
424 should try the API again.
425 @retval FALSE buffer could not be allocated and the caller
426 should not try the API again.
431 IN OUT EFI_STATUS
*Status
,
432 IN OUT VOID
**Buffer
,
440 // If this is an initial request, buffer will be null with a new buffer size
442 if (*Buffer
== NULL
) {
443 *Status
= EFI_BUFFER_TOO_SMALL
;
446 if (BufferSize
== 0) {
451 // If the status code is "buffer too small", resize the buffer
454 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
455 if (*Buffer
!= NULL
) {
456 CoreFreePool (*Buffer
);
459 *Buffer
= CoreAllocateBootServicesPool (BufferSize
);
460 if (*Buffer
!= NULL
) {
463 *Status
= EFI_OUT_OF_RESOURCES
;
468 // If there's an error, free the buffer
470 if ((!TryAgain
) && (EFI_ERROR (*Status
)) && (*Buffer
)) {
471 CoreFreePool (*Buffer
);