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 IN BOOLEAN BootPolicy
,
21 IN VOID
*SourceBuffer OPTIONAL
,
23 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
24 OUT EFI_HANDLE
*DeviceHandle
,
25 IN IMAGE_FILE_HANDLE
*ImageFileHandle
,
26 OUT UINT32
*AuthenticationStatus
32 Opens a file for (simple) reading. The simple read abstraction
33 will access the file either from a memory copy, from a file
34 system interface, or from the load file interface.
38 BootPolicy - Policy for Open Image File.
39 SourceBuffer - Pointer to the memory location containing copy
40 of the image to be loaded.
41 SourceSize - The size in bytes of SourceBuffer.
42 FilePath - The specific file path from which the image is loaded
43 DeviceHandle - Pointer to the return device handle.
44 ImageFileHandle - Pointer to the image file handle.
45 AuthenticationStatus - Pointer to a caller-allocated UINT32 in which the authentication status is returned.
49 EFI_SUCCESS - Image file successfully opened.
51 EFI_LOAD_ERROR - If the caller passed a copy of the file, and SourceSize is 0.
53 EFI_INVALID_PARAMETER - File path is not valid.
55 EFI_NOT_FOUND - File not found.
60 EFI_DEVICE_PATH_PROTOCOL
*TempFilePath
;
61 FILEPATH_DEVICE_PATH
*FilePathNode
;
62 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*FwVolFilePathNode
;
63 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
64 EFI_FILE_HANDLE FileHandle
;
65 EFI_FILE_HANDLE LastHandle
;
66 EFI_LOAD_FILE_PROTOCOL
*LoadFile
;
67 EFI_FIRMWARE_VOLUME2_PROTOCOL
*FwVol
;
68 EFI_SECTION_TYPE SectionType
;
72 EFI_FV_FILE_ATTRIBUTES Attrib
;
73 EFI_FILE_INFO
*FileInfo
;
76 FILEPATH_DEVICE_PATH
*OriginalFilePathNode
;
78 OriginalFilePathNode
= NULL
;
79 *AuthenticationStatus
= 0;
80 ZeroMem (ImageFileHandle
, sizeof (IMAGE_FILE_HANDLE
));
81 ImageFileHandle
->Signature
= IMAGE_FILE_HANDLE_SIGNATURE
;
84 // If the caller passed a copy of the file, then just use it
86 if (SourceBuffer
!= NULL
) {
87 ImageFileHandle
->Source
= SourceBuffer
;
88 ImageFileHandle
->SourceSize
= SourceSize
;
90 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid
, FilePath
, DeviceHandle
);
94 Status
= EFI_LOAD_ERROR
;
100 // Make sure FilePath is valid
102 if (*FilePath
== NULL
) {
103 return EFI_INVALID_PARAMETER
;
107 // Check to see if it's in a Firmware Volume
109 FwVolFilePathNode
= (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH
*) *FilePath
;
110 Status
= CoreDevicePathToInterface (
111 &gEfiFirmwareVolume2ProtocolGuid
,
112 (EFI_DEVICE_PATH_PROTOCOL
**)&FwVolFilePathNode
,
116 if (!EFI_ERROR (Status
)) {
118 // For FwVol File system there is only a single file name that is a GUID.
120 NameGuid
= EfiGetNameGuidFromFwVolDevicePathNode (FwVolFilePathNode
);
121 if (NameGuid
!= NULL
) {
123 SectionType
= EFI_SECTION_PE32
;
125 Status
= FwVol
->ReadSection (
130 (VOID
**)&Pe32Buffer
,
134 if (EFI_ERROR (Status
)) {
136 // Try a raw file, since a PE32 SECTION does not exist
138 if (Pe32Buffer
!= NULL
) {
139 CoreFreePool (Pe32Buffer
);
140 *AuthenticationStatus
= 0;
143 Status
= FwVol
->ReadFile (
146 (VOID
**)&Pe32Buffer
,
154 if (!EFI_ERROR (Status
)) {
156 // One of the reads passed so we are done
158 ImageFileHandle
->Source
= Pe32Buffer
;
159 ImageFileHandle
->SourceSize
= Pe32BufferSize
;
160 ImageFileHandle
->FreeBuffer
= TRUE
;
167 // Attempt to access the file via a file system interface
169 FilePathNode
= (FILEPATH_DEVICE_PATH
*) *FilePath
;
170 Status
= CoreDevicePathToInterface (
171 &gEfiSimpleFileSystemProtocolGuid
,
172 (EFI_DEVICE_PATH_PROTOCOL
**)&FilePathNode
,
176 if (!EFI_ERROR (Status
)) {
178 // Open the Volume to get the File System handle
180 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
181 if (!EFI_ERROR (Status
)) {
183 // Duplicate the device path to avoid the access to unaligned device path node.
184 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
185 // nodes, It assures the fields in device path nodes are 2 byte aligned.
187 FilePathNode
= (FILEPATH_DEVICE_PATH
*)CoreDuplicateDevicePath((EFI_DEVICE_PATH_PROTOCOL
*)(UINTN
)FilePathNode
);
188 if (FilePathNode
== NULL
) {
189 FileHandle
->Close (FileHandle
);
190 Status
= EFI_OUT_OF_RESOURCES
;
192 OriginalFilePathNode
= FilePathNode
;
194 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
195 // directory information and filename can be seperate. The goal is to inch
196 // our way down each device path node and close the previous node
198 while (!IsDevicePathEnd (&FilePathNode
->Header
)) {
199 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
200 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
) {
201 Status
= EFI_UNSUPPORTED
;
204 if (EFI_ERROR (Status
)) {
206 // Exit loop on Error
211 LastHandle
= FileHandle
;
214 Status
= LastHandle
->Open (
217 FilePathNode
->PathName
,
223 // Close the previous node
225 LastHandle
->Close (LastHandle
);
227 FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
);
230 // Free the allocated memory pool
232 CoreFreePool(OriginalFilePathNode
);
235 if (!EFI_ERROR (Status
)) {
237 // We have found the file. Now we need to read it. Before we can read the file we need to
238 // figure out how big the file is.
241 FileInfoSize
= sizeof (EFI_FILE_INFO
);
242 while (CoreGrowBuffer (&Status
, (VOID
**)&FileInfo
, FileInfoSize
)) {
244 // Automatically allocate buffer of the correct size and make the call
246 Status
= FileHandle
->GetInfo (
253 if (!EFI_ERROR (Status
)) {
255 // Allocate space for the file
257 ImageFileHandle
->Source
= CoreAllocateBootServicesPool ((UINTN
)FileInfo
->FileSize
);
258 if (ImageFileHandle
->Source
!= NULL
) {
260 // Read the file into the buffer we allocated
262 ImageFileHandle
->SourceSize
= (UINTN
)FileInfo
->FileSize
;
263 ImageFileHandle
->FreeBuffer
= TRUE
;
264 Status
= FileHandle
->Read (FileHandle
, &ImageFileHandle
->SourceSize
, ImageFileHandle
->Source
);
267 // Close the file since we are done
269 FileHandle
->Close (FileHandle
);
271 Status
= EFI_OUT_OF_RESOURCES
;
282 // Try LoadFile style
285 TempFilePath
= *FilePath
;
286 Status
= CoreDevicePathToInterface (
287 &gEfiLoadFileProtocolGuid
,
292 if (!EFI_ERROR (Status
)) {
294 // Call LoadFile with the correct buffer size
296 while (CoreGrowBuffer (&Status
, (VOID
**)&ImageFileHandle
->Source
, ImageFileHandle
->SourceSize
)) {
297 Status
= LoadFile
->LoadFile (
301 &ImageFileHandle
->SourceSize
,
302 ImageFileHandle
->Source
305 // If success or other error happens, stop loop
307 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
312 if (!EFI_ERROR (Status
) || Status
== EFI_ALREADY_STARTED
) {
313 ImageFileHandle
->FreeBuffer
= TRUE
;
319 // Nothing else to try
321 DEBUG ((EFI_D_LOAD
|EFI_D_WARN
, "CoreOpenImageFile: Device did not support a known load protocol\n"));
322 Status
= EFI_NOT_FOUND
;
327 // If the file was not accessed, clean up
329 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
330 if (ImageFileHandle
->FreeBuffer
) {
332 // Free the source buffer if we allocated it
334 CoreFreePool (ImageFileHandle
->Source
);
348 IN OUT UINTN
*ReadSize
,
355 Read image file (specified by UserHandle) into user specified buffer with specified offset
360 UserHandle - Image file handle
362 Offset - Offset to the source file
364 ReadSize - For input, pointer of size to read;
365 For output, pointer of size actually read.
367 Buffer - Buffer to write into
371 EFI_SUCCESS - Successfully read the specified part of file into buffer.
376 IMAGE_FILE_HANDLE
*FHand
;
378 FHand
= (IMAGE_FILE_HANDLE
*)UserHandle
;
379 ASSERT (FHand
->Signature
== IMAGE_FILE_HANDLE_SIGNATURE
);
382 // Move data from our local copy of the file
384 EndPosition
= Offset
+ *ReadSize
;
385 if (EndPosition
> FHand
->SourceSize
) {
386 *ReadSize
= (UINT32
)(FHand
->SourceSize
- Offset
);
388 if (Offset
>= FHand
->SourceSize
) {
392 CopyMem (Buffer
, (CHAR8
*)FHand
->Source
+ Offset
, *ReadSize
);
397 CoreDevicePathToInterface (
398 IN EFI_GUID
*Protocol
,
399 IN EFI_DEVICE_PATH_PROTOCOL
**FilePath
,
400 OUT VOID
**Interface
,
401 OUT EFI_HANDLE
*Handle
407 Search a handle to a device on a specified device path that supports a specified protocol,
408 interface of that protocol on that handle is another output.
412 Protocol - The protocol to search for
414 FilePath - The specified device path
416 Interface - Interface of the protocol on the handle
418 Handle - The handle to the device on the specified device path that supports the protocol.
428 Status
= CoreLocateDevicePath (Protocol
, FilePath
, Handle
);
429 if (!EFI_ERROR (Status
)) {
430 Status
= CoreHandleProtocol (*Handle
, Protocol
, Interface
);
437 IN OUT EFI_STATUS
*Status
,
438 IN OUT VOID
**Buffer
,
445 Helper function called as part of the code needed
446 to allocate the proper sized buffer for various
451 Status - Current status
453 Buffer - Current allocated buffer, or NULL
455 BufferSize - Current buffer size needed
459 TRUE - if the buffer was reallocated and the caller
460 should try the API again.
462 FALSE - buffer could not be allocated and the caller
463 should not try the API again.
471 // If this is an initial request, buffer will be null with a new buffer size
473 if (*Buffer
== NULL
) {
474 *Status
= EFI_BUFFER_TOO_SMALL
;
477 if (BufferSize
== 0) {
482 // If the status code is "buffer too small", resize the buffer
485 if (*Status
== EFI_BUFFER_TOO_SMALL
) {
486 if (*Buffer
!= NULL
) {
487 CoreFreePool (*Buffer
);
490 *Buffer
= CoreAllocateBootServicesPool (BufferSize
);
491 if (*Buffer
!= NULL
) {
494 *Status
= EFI_OUT_OF_RESOURCES
;
499 // If there's an error, free the buffer
501 if ((!TryAgain
) && (EFI_ERROR (*Status
)) && (*Buffer
)) {
502 CoreFreePool (*Buffer
);