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.
18 Search a handle to a device on a specified device path that supports a specified protocol,
19 interface of that protocol on that handle is another output.
23 @param Protocol The protocol to search for
24 @param FilePath The specified device path
25 @param Interface Interface of the protocol on the handle
26 @param Handle The handle to the device on the specified device
27 path that supports the protocol.
33 CoreDevicePathToInterface (
34 IN EFI_GUID
*Protocol
,
35 IN EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
37 OUT EFI_HANDLE
*Handle
42 Status
= CoreLocateDevicePath (Protocol
, FilePath
, Handle
);
43 if (!EFI_ERROR (Status
)) {
44 Status
= CoreHandleProtocol (*Handle
, Protocol
, Interface
);
51 Opens a file for (simple) reading. The simple read abstraction
52 will access the file either from a memory copy, from a file
53 system interface, or from the load file interface.
55 @param BootPolicy Policy for Open Image File.
56 @param SourceBuffer Pointer to the memory location containing copy
57 of the image to be loaded.
58 @param SourceSize The size in bytes of SourceBuffer.
59 @param FilePath The specific file path from which the image is
61 @param DeviceHandle Pointer to the return device handle.
62 @param ImageFileHandle Pointer to the image file handle.
63 @param AuthenticationStatus Pointer to a caller-allocated UINT32 in which
64 the authentication status is returned.
66 @retval EFI_SUCCESS Image file successfully opened.
67 @retval EFI_LOAD_ERROR If the caller passed a copy of the file, and
69 @retval EFI_INVALID_PARAMETER File path is not valid.
70 @retval EFI_NOT_FOUND File not found.
75 IN BOOLEAN BootPolicy
,
76 IN VOID
*SourceBuffer OPTIONAL
,
78 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
79 OUT EFI_HANDLE
*DeviceHandle
,
80 IN IMAGE_FILE_HANDLE
*ImageFileHandle
,
81 OUT UINT32
*AuthenticationStatus
85 EFI_DEVICE_PATH_PROTOCOL
*TempFilePath
;
86 FILEPATH_DEVICE_PATH
*FilePathNode
;
87 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*FwVolFilePathNode
;
88 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
89 EFI_FILE_HANDLE FileHandle
;
90 EFI_FILE_HANDLE LastHandle
;
91 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
92 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
93 EFI_SECTION_TYPE SectionType
;
97 EFI_FV_FILE_ATTRIBUTES Attrib
;
98 EFI_FILE_INFO
*FileInfo
;
101 FILEPATH_DEVICE_PATH
*OriginalFilePathNode
;
103 OriginalFilePathNode
= NULL
;
104 *AuthenticationStatus
= 0;
105 ZeroMem (ImageFileHandle
, sizeof (IMAGE_FILE_HANDLE
));
106 ImageFileHandle
->Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
109 // If the caller passed a copy of the file, then just use it
111 if (SourceBuffer
!= NULL
) {
112 ImageFileHandle
->Source
= SourceBuffer
;
113 ImageFileHandle
->SourceSize
= SourceSize
;
114 *DeviceHandle
= NULL
;
115 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, FilePath
, DeviceHandle
);
116 if (SourceSize
> 0) {
117 Status
= EFI_SUCCESS
;
119 Status
= EFI_LOAD_ERROR
;
125 // Make sure FilePath is valid
127 if (*FilePath
== NULL
) {
128 return EFI_INVALID_PARAMETER
;
132 // Check to see if it's in a Firmware Volume
134 FwVolFilePathNode
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) *FilePath
;
135 Status
= CoreDevicePathToInterface (
136 &gEfiFirmwareVolume2ProtocolGuid
,
137 (EFI_DEVICE_PATH_PROTOCOL
**)&FwVolFilePathNode
,
141 if (!EFI_ERROR (Status
)) {
143 // For FwVol File system there is only a single file name that is a GUID.
145 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode (FwVolFilePathNode
);
146 if (NameGuid
!= NULL
) {
148 SectionType
= EFI_SECTION_PE32
;
150 Status
= FwVol
->ReadSection (
155 (VOID
**)&Pe32Buffer
,
159 if (EFI_ERROR (Status
)) {
161 // Try a raw file, since a PE32 SECTION does not exist
163 if (Pe32Buffer
!= NULL
) {
164 CoreFreePool (Pe32Buffer
);
165 *AuthenticationStatus
= 0;
168 Status
= FwVol
->ReadFile (
171 (VOID
**)&Pe32Buffer
,
179 if (!EFI_ERROR (Status
)) {
181 // One of the reads passed so we are done
183 ImageFileHandle
->Source
= Pe32Buffer
;
184 ImageFileHandle
->SourceSize
= Pe32BufferSize
;
185 ImageFileHandle
->FreeBuffer
= TRUE
;
192 // Attempt to access the file via a file system interface
194 FilePathNode
= (FILEPATH_DEVICE_PATH
*) *FilePath
;
195 Status
= CoreDevicePathToInterface (
196 &gEfiSimpleFileSystemProtocolGuid
,
197 (EFI_DEVICE_PATH_PROTOCOL
**)&FilePathNode
,
201 if (!EFI_ERROR (Status
)) {
203 // Open the Volume to get the File System handle
205 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
206 if (!EFI_ERROR (Status
)) {
208 // Duplicate the device path to avoid the access to unaligned device path node.
209 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
210 // nodes, It assures the fields in device path nodes are 2 byte aligned.
212 FilePathNode
= (FILEPATH_DEVICE_PATH
*)DuplicateDevicePath((EFI_DEVICE_PATH_PROTOCOL
*)(UINTN
)FilePathNode
);
213 if (FilePathNode
== NULL
) {
214 FileHandle
->Close (FileHandle
);
215 Status
= EFI_OUT_OF_RESOURCES
;
217 OriginalFilePathNode
= FilePathNode
;
219 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
220 // directory information and filename can be seperate. The goal is to inch
221 // our way down each device path node and close the previous node
223 while (!IsDevicePathEnd (&FilePathNode
->Header
)) {
224 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
225 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
) {
226 Status
= EFI_UNSUPPORTED
;
229 if (EFI_ERROR (Status
)) {
231 // Exit loop on Error
236 LastHandle
= FileHandle
;
239 Status
= LastHandle
->Open (
242 FilePathNode
->PathName
,
248 // Close the previous node
250 LastHandle
->Close (LastHandle
);
252 FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
);
255 // Free the allocated memory pool
257 CoreFreePool(OriginalFilePathNode
);
260 if (!EFI_ERROR (Status
)) {
262 // We have found the file. Now we need to read it. Before we can read the file we need to
263 // figure out how big the file is.
267 Status
= FileHandle
->GetInfo (
273 if (Status
== EFI_BUFFER_TOO_SMALL
) {
274 FileInfo
= AllocatePool (FileInfoSize
);
275 if (FileInfo
!= NULL
) {
276 Status
= FileHandle
->GetInfo (
283 Status
= EFI_OUT_OF_RESOURCES
;
288 if (!EFI_ERROR (Status
)) {
290 // Allocate space for the file
292 ImageFileHandle
->Source
= AllocatePool ((UINTN
)FileInfo
->FileSize
);
293 if (ImageFileHandle
->Source
!= NULL
) {
295 // Read the file into the buffer we allocated
297 ImageFileHandle
->SourceSize
= (UINTN
) FileInfo
->FileSize
;
298 ImageFileHandle
->FreeBuffer
= TRUE
;
299 Status
= FileHandle
->Read (FileHandle
, &ImageFileHandle
->SourceSize
, ImageFileHandle
->Source
);
302 // Close the file since we are done
304 FileHandle
->Close (FileHandle
);
305 CoreFreePool (FileInfo
);
307 Status
= EFI_OUT_OF_RESOURCES
;
318 // Try LoadFile style
321 TempFilePath
= *FilePath
;
322 Status
= CoreDevicePathToInterface (
323 &gEfiLoadFileProtocolGuid
,
328 if (!EFI_ERROR (Status
)) {
330 // Call LoadFile with the correct buffer size
332 ASSERT (ImageFileHandle
->SourceSize
== 0);
333 ASSERT (ImageFileHandle
->Source
== NULL
);
334 Status
= LoadFile
->LoadFile (
338 &ImageFileHandle
->SourceSize
,
339 ImageFileHandle
->Source
341 if (Status
== EFI_BUFFER_TOO_SMALL
) {
342 ImageFileHandle
->Source
= AllocatePool (ImageFileHandle
->SourceSize
);
343 if (ImageFileHandle
->Source
== NULL
) {
344 Status
= EFI_OUT_OF_RESOURCES
;
346 Status
= LoadFile
->LoadFile (
350 &ImageFileHandle
->SourceSize
,
351 ImageFileHandle
->Source
356 if (!EFI_ERROR (Status
)) {
357 ImageFileHandle
->FreeBuffer
= TRUE
;
363 // Nothing else to try
365 DEBUG ((DEBUG_LOAD
|DEBUG_WARN
, "CoreOpenImageFile: Device did not support a known load protocol\n"));
366 Status
= EFI_NOT_FOUND
;
370 // If the file was not accessed, clean up
372 if (EFI_ERROR (Status
)) {
373 if (ImageFileHandle
->FreeBuffer
) {
375 // Free the source buffer if we allocated it
377 CoreFreePool (ImageFileHandle
->Source
);
387 Read image file (specified by UserHandle) into user specified buffer with specified offset
390 @param UserHandle Image file handle
391 @param Offset Offset to the source file
392 @param ReadSize For input, pointer of size to read; For output,
393 pointer of size actually read.
394 @param Buffer Buffer to write into
396 @retval EFI_SUCCESS Successfully read the specified part of file
405 IN OUT UINTN
*ReadSize
,
410 IMAGE_FILE_HANDLE
*FHand
;
412 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
413 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
416 // Move data from our local copy of the file
418 EndPosition
= Offset
+ *ReadSize
;
419 if (EndPosition
> FHand
->SourceSize
) {
420 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
422 if (Offset
>= FHand
->SourceSize
) {
426 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);