]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/Image/ImageFile.c
Add comments and DoxyGen format for these files.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / ImageFile.c
CommitLineData
504214c4
LG
1/** @file\r
2 \r
3 Handle services to image file. \r
4 \r
1564e9ac 5Copyright (c) 2006 - 2008, Intel Corporation\r
cfe9de52 6All rights reserved. This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
28a00297 13\r
504214c4 14**/\r
28a00297 15\r
16#include <DxeMain.h>\r
17\r
18EFI_STATUS\r
19CoreOpenImageFile (\r
20 IN BOOLEAN BootPolicy,\r
21 IN VOID *SourceBuffer OPTIONAL,\r
22 IN UINTN SourceSize,\r
cfe9de52 23 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
28a00297 24 OUT EFI_HANDLE *DeviceHandle,\r
25 IN IMAGE_FILE_HANDLE *ImageFileHandle,\r
26 OUT UINT32 *AuthenticationStatus\r
27 )\r
28/*++\r
29\r
30Routine Description:\r
31\r
32 Opens a file for (simple) reading. The simple read abstraction\r
33 will access the file either from a memory copy, from a file\r
34 system interface, or from the load file interface.\r
35\r
36Arguments:\r
37\r
38 BootPolicy - Policy for Open Image File.\r
39 SourceBuffer - Pointer to the memory location containing copy\r
40 of the image to be loaded.\r
41 SourceSize - The size in bytes of SourceBuffer.\r
42 FilePath - The specific file path from which the image is loaded\r
43 DeviceHandle - Pointer to the return device handle.\r
44 ImageFileHandle - Pointer to the image file handle.\r
cfe9de52 45 AuthenticationStatus - Pointer to a caller-allocated UINT32 in which the authentication status is returned.\r
46\r
28a00297 47Returns:\r
48\r
49 EFI_SUCCESS - Image file successfully opened.\r
cfe9de52 50\r
28a00297 51 EFI_LOAD_ERROR - If the caller passed a copy of the file, and SourceSize is 0.\r
cfe9de52 52\r
28a00297 53 EFI_INVALID_PARAMETER - File path is not valid.\r
cfe9de52 54\r
28a00297 55 EFI_NOT_FOUND - File not found.\r
56\r
57--*/\r
58{\r
59 EFI_STATUS Status;\r
60 EFI_DEVICE_PATH_PROTOCOL *TempFilePath;\r
61 FILEPATH_DEVICE_PATH *FilePathNode;\r
62 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FwVolFilePathNode;\r
63 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r
64 EFI_FILE_HANDLE FileHandle;\r
65 EFI_FILE_HANDLE LastHandle;\r
66 EFI_LOAD_FILE_PROTOCOL *LoadFile;\r
0c2b5da8 67 EFI_FIRMWARE_VOLUME2_PROTOCOL *FwVol;\r
28a00297 68 EFI_SECTION_TYPE SectionType;\r
69 UINT8 *Pe32Buffer;\r
70 UINTN Pe32BufferSize;\r
71 EFI_FV_FILETYPE Type;\r
72 EFI_FV_FILE_ATTRIBUTES Attrib;\r
73 EFI_FILE_INFO *FileInfo;\r
74 UINTN FileInfoSize;\r
75 EFI_GUID *NameGuid;\r
1564e9ac 76 FILEPATH_DEVICE_PATH *OriginalFilePathNode;\r
28a00297 77\r
1564e9ac 78 OriginalFilePathNode = NULL;\r
28a00297 79 *AuthenticationStatus = 0;\r
80 ZeroMem (ImageFileHandle, sizeof (IMAGE_FILE_HANDLE));\r
81 ImageFileHandle->Signature = IMAGE_FILE_HANDLE_SIGNATURE;\r
82\r
83 //\r
84 // If the caller passed a copy of the file, then just use it\r
85 //\r
86 if (SourceBuffer != NULL) {\r
87 ImageFileHandle->Source = SourceBuffer;\r
88 ImageFileHandle->SourceSize = SourceSize;\r
89 *DeviceHandle = NULL;\r
cfe9de52 90 CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, FilePath, DeviceHandle);\r
28a00297 91 if (SourceSize > 0) {\r
92 Status = EFI_SUCCESS;\r
93 } else {\r
94 Status = EFI_LOAD_ERROR;\r
95 }\r
96 goto Done;\r
97 }\r
98\r
99 //\r
100 // Make sure FilePath is valid\r
101 //\r
cfe9de52 102 if (*FilePath == NULL) {\r
28a00297 103 return EFI_INVALID_PARAMETER;\r
104 }\r
105\r
106 //\r
107 // Check to see if it's in a Firmware Volume\r
108 //\r
cfe9de52 109 FwVolFilePathNode = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) *FilePath;\r
28a00297 110 Status = CoreDevicePathToInterface (\r
cfe9de52 111 &gEfiFirmwareVolume2ProtocolGuid,\r
112 (EFI_DEVICE_PATH_PROTOCOL **)&FwVolFilePathNode,\r
113 (VOID*)&FwVol,\r
28a00297 114 DeviceHandle\r
115 );\r
116 if (!EFI_ERROR (Status)) {\r
117 //\r
118 // For FwVol File system there is only a single file name that is a GUID.\r
119 //\r
120 NameGuid = EfiGetNameGuidFromFwVolDevicePathNode (FwVolFilePathNode);\r
121 if (NameGuid != NULL) {\r
122\r
123 SectionType = EFI_SECTION_PE32;\r
124 Pe32Buffer = NULL;\r
125 Status = FwVol->ReadSection (\r
cfe9de52 126 FwVol,\r
127 NameGuid,\r
128 SectionType,\r
28a00297 129 0,\r
130 (VOID **)&Pe32Buffer,\r
131 &Pe32BufferSize,\r
132 AuthenticationStatus\r
133 );\r
134 if (EFI_ERROR (Status)) {\r
135 //\r
136 // Try a raw file, since a PE32 SECTION does not exist\r
137 //\r
138 if (Pe32Buffer != NULL) {\r
139 CoreFreePool (Pe32Buffer);\r
140 *AuthenticationStatus = 0;\r
141 }\r
142 Pe32Buffer = NULL;\r
143 Status = FwVol->ReadFile (\r
cfe9de52 144 FwVol,\r
145 NameGuid,\r
28a00297 146 (VOID **)&Pe32Buffer,\r
147 &Pe32BufferSize,\r
148 &Type,\r
149 &Attrib,\r
150 AuthenticationStatus\r
151 );\r
152 }\r
cfe9de52 153\r
28a00297 154 if (!EFI_ERROR (Status)) {\r
155 //\r
156 // One of the reads passed so we are done\r
157 //\r
158 ImageFileHandle->Source = Pe32Buffer;\r
159 ImageFileHandle->SourceSize = Pe32BufferSize;\r
160 ImageFileHandle->FreeBuffer = TRUE;\r
161 goto Done;\r
162 }\r
163 }\r
164 }\r
165\r
166 //\r
167 // Attempt to access the file via a file system interface\r
168 //\r
cfe9de52 169 FilePathNode = (FILEPATH_DEVICE_PATH *) *FilePath;\r
28a00297 170 Status = CoreDevicePathToInterface (\r
cfe9de52 171 &gEfiSimpleFileSystemProtocolGuid,\r
172 (EFI_DEVICE_PATH_PROTOCOL **)&FilePathNode,\r
173 (VOID*)&Volume,\r
28a00297 174 DeviceHandle\r
175 );\r
176 if (!EFI_ERROR (Status)) {\r
177 //\r
178 // Open the Volume to get the File System handle\r
179 //\r
180 Status = Volume->OpenVolume (Volume, &FileHandle);\r
181 if (!EFI_ERROR (Status)) {\r
28a00297 182 //\r
1564e9ac 183 // Duplicate the device path to avoid the access to unaligned device path node.\r
184 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r
185 // nodes, It assures the fields in device path nodes are 2 byte aligned. \r
28a00297 186 //\r
1564e9ac 187 FilePathNode = (FILEPATH_DEVICE_PATH *)CoreDuplicateDevicePath((EFI_DEVICE_PATH_PROTOCOL *)(UINTN)FilePathNode);\r
188 if (FilePathNode == NULL) {\r
189 FileHandle->Close (FileHandle);\r
190 Status = EFI_OUT_OF_RESOURCES;\r
191 } else {\r
192 OriginalFilePathNode = FilePathNode;\r
193 //\r
194 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
195 // directory information and filename can be seperate. The goal is to inch\r
196 // our way down each device path node and close the previous node\r
197 //\r
198 while (!IsDevicePathEnd (&FilePathNode->Header)) {\r
199 if (DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH ||\r
200 DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP) {\r
201 Status = EFI_UNSUPPORTED;\r
202 }\r
203\r
204 if (EFI_ERROR (Status)) {\r
205 //\r
206 // Exit loop on Error\r
207 //\r
208 break;\r
209 }\r
210\r
211 LastHandle = FileHandle;\r
212 FileHandle = NULL;\r
213\r
214 Status = LastHandle->Open (\r
215 LastHandle,\r
216 &FileHandle,\r
217 FilePathNode->PathName,\r
218 EFI_FILE_MODE_READ,\r
219 0\r
220 );\r
28a00297 221\r
28a00297 222 //\r
1564e9ac 223 // Close the previous node\r
28a00297 224 //\r
1564e9ac 225 LastHandle->Close (LastHandle);\r
28a00297 226\r
1564e9ac 227 FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header);\r
228 }\r
28a00297 229 //\r
1564e9ac 230 // Free the allocated memory pool \r
28a00297 231 //\r
1564e9ac 232 CoreFreePool(OriginalFilePathNode);\r
28a00297 233 }\r
234\r
235 if (!EFI_ERROR (Status)) {\r
236 //\r
237 // We have found the file. Now we need to read it. Before we can read the file we need to\r
238 // figure out how big the file is.\r
239 //\r
240 FileInfo = NULL;\r
241 FileInfoSize = sizeof (EFI_FILE_INFO);\r
242 while (CoreGrowBuffer (&Status, (VOID **)&FileInfo, FileInfoSize)) {\r
243 //\r
244 // Automatically allocate buffer of the correct size and make the call\r
245 //\r
246 Status = FileHandle->GetInfo (\r
247 FileHandle,\r
248 &gEfiFileInfoGuid,\r
249 &FileInfoSize,\r
cfe9de52 250 FileInfo\r
28a00297 251 );\r
252 }\r
253 if (!EFI_ERROR (Status)) {\r
254 //\r
255 // Allocate space for the file\r
256 //\r
257 ImageFileHandle->Source = CoreAllocateBootServicesPool ((UINTN)FileInfo->FileSize);\r
258 if (ImageFileHandle->Source != NULL) {\r
259 //\r
260 // Read the file into the buffer we allocated\r
261 //\r
262 ImageFileHandle->SourceSize = (UINTN)FileInfo->FileSize;\r
263 ImageFileHandle->FreeBuffer = TRUE;\r
264 Status = FileHandle->Read (FileHandle, &ImageFileHandle->SourceSize, ImageFileHandle->Source);\r
265\r
266 //\r
267 // Close the file since we are done\r
268 //\r
269 FileHandle->Close (FileHandle);\r
270 } else {\r
271 Status = EFI_OUT_OF_RESOURCES;\r
272 }\r
273\r
274 goto Done;\r
275 }\r
276 }\r
277 }\r
cfe9de52 278 }\r
28a00297 279\r
280\r
281 //\r
282 // Try LoadFile style\r
283 //\r
284\r
cfe9de52 285 TempFilePath = *FilePath;\r
28a00297 286 Status = CoreDevicePathToInterface (\r
287 &gEfiLoadFileProtocolGuid,\r
288 &TempFilePath,\r
289 (VOID*)&LoadFile,\r
290 DeviceHandle\r
291 );\r
292 if (!EFI_ERROR (Status)) {\r
293 //\r
294 // Call LoadFile with the correct buffer size\r
295 //\r
296 while (CoreGrowBuffer (&Status, (VOID **)&ImageFileHandle->Source, ImageFileHandle->SourceSize)) {\r
297 Status = LoadFile->LoadFile (\r
298 LoadFile,\r
299 TempFilePath,\r
300 BootPolicy,\r
301 &ImageFileHandle->SourceSize,\r
302 ImageFileHandle->Source\r
303 );\r
304 //\r
305 // If success or other error happens, stop loop\r
306 //\r
307 if (Status != EFI_BUFFER_TOO_SMALL) {\r
308 break;\r
309 }\r
310 }\r
311\r
312 if (!EFI_ERROR (Status) || Status == EFI_ALREADY_STARTED) {\r
313 ImageFileHandle->FreeBuffer = TRUE;\r
314 goto Done;\r
315 }\r
316 }\r
317\r
318 //\r
319 // Nothing else to try\r
320 //\r
321 DEBUG ((EFI_D_LOAD|EFI_D_WARN, "CoreOpenImageFile: Device did not support a known load protocol\n"));\r
322 Status = EFI_NOT_FOUND;\r
323\r
324Done:\r
325\r
326 //\r
327 // If the file was not accessed, clean up\r
328 //\r
329 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
330 if (ImageFileHandle->FreeBuffer) {\r
331 //\r
332 // Free the source buffer if we allocated it\r
333 //\r
334 CoreFreePool (ImageFileHandle->Source);\r
335 }\r
336 }\r
337\r
338 return Status;\r
339}\r
340\r
341\r
342\r
343EFI_STATUS\r
344EFIAPI\r
345CoreReadImageFile (\r
346 IN VOID *UserHandle,\r
347 IN UINTN Offset,\r
348 IN OUT UINTN *ReadSize,\r
349 OUT VOID *Buffer\r
350 )\r
351/*++\r
352\r
353Routine Description:\r
354\r
355 Read image file (specified by UserHandle) into user specified buffer with specified offset\r
356 and length.\r
357\r
358Arguments:\r
359\r
360 UserHandle - Image file handle\r
cfe9de52 361\r
28a00297 362 Offset - Offset to the source file\r
cfe9de52 363\r
28a00297 364 ReadSize - For input, pointer of size to read;\r
365 For output, pointer of size actually read.\r
cfe9de52 366\r
28a00297 367 Buffer - Buffer to write into\r
368\r
369Returns:\r
370\r
371 EFI_SUCCESS - Successfully read the specified part of file into buffer.\r
372\r
373--*/\r
374{\r
375 UINTN EndPosition;\r
376 IMAGE_FILE_HANDLE *FHand;\r
377\r
378 FHand = (IMAGE_FILE_HANDLE *)UserHandle;\r
379 ASSERT (FHand->Signature == IMAGE_FILE_HANDLE_SIGNATURE);\r
380\r
381 //\r
382 // Move data from our local copy of the file\r
383 //\r
384 EndPosition = Offset + *ReadSize;\r
385 if (EndPosition > FHand->SourceSize) {\r
386 *ReadSize = (UINT32)(FHand->SourceSize - Offset);\r
cfe9de52 387 }\r
28a00297 388 if (Offset >= FHand->SourceSize) {\r
389 *ReadSize = 0;\r
390 }\r
391\r
392 CopyMem (Buffer, (CHAR8 *)FHand->Source + Offset, *ReadSize);\r
393 return EFI_SUCCESS;\r
394}\r
395\r
396EFI_STATUS\r
397CoreDevicePathToInterface (\r
398 IN EFI_GUID *Protocol,\r
399 IN EFI_DEVICE_PATH_PROTOCOL **FilePath,\r
400 OUT VOID **Interface,\r
401 OUT EFI_HANDLE *Handle\r
402 )\r
403/*++\r
404\r
405Routine Description:\r
406\r
407 Search a handle to a device on a specified device path that supports a specified protocol,\r
408 interface of that protocol on that handle is another output.\r
409\r
410Arguments:\r
411\r
412 Protocol - The protocol to search for\r
cfe9de52 413\r
28a00297 414 FilePath - The specified device path\r
cfe9de52 415\r
28a00297 416 Interface - Interface of the protocol on the handle\r
cfe9de52 417\r
28a00297 418 Handle - The handle to the device on the specified device path that supports the protocol.\r
cfe9de52 419\r
28a00297 420Returns:\r
421\r
422 Status code.\r
423\r
424--*/\r
425{\r
426 EFI_STATUS Status;\r
427\r
428 Status = CoreLocateDevicePath (Protocol, FilePath, Handle);\r
429 if (!EFI_ERROR (Status)) {\r
430 Status = CoreHandleProtocol (*Handle, Protocol, Interface);\r
431 }\r
432 return Status;\r
433}\r
434\r
435BOOLEAN\r
436CoreGrowBuffer (\r
437 IN OUT EFI_STATUS *Status,\r
438 IN OUT VOID **Buffer,\r
439 IN UINTN BufferSize\r
440 )\r
441/*++\r
442\r
443Routine Description:\r
444\r
445 Helper function called as part of the code needed\r
cfe9de52 446 to allocate the proper sized buffer for various\r
28a00297 447 EFI interfaces.\r
448\r
449Arguments:\r
450\r
451 Status - Current status\r
452\r
453 Buffer - Current allocated buffer, or NULL\r
454\r
455 BufferSize - Current buffer size needed\r
cfe9de52 456\r
28a00297 457Returns:\r
cfe9de52 458\r
459 TRUE - if the buffer was reallocated and the caller\r
28a00297 460 should try the API again.\r
461\r
462 FALSE - buffer could not be allocated and the caller\r
463 should not try the API again.\r
464\r
465--*/\r
466{\r
467 BOOLEAN TryAgain;\r
468\r
469 TryAgain = FALSE;\r
470 //\r
471 // If this is an initial request, buffer will be null with a new buffer size\r
472 //\r
473 if (*Buffer == NULL) {\r
474 *Status = EFI_BUFFER_TOO_SMALL;\r
475 }\r
476\r
477 if (BufferSize == 0) {\r
478 return TRUE;\r
479 }\r
480\r
481 //\r
482 // If the status code is "buffer too small", resize the buffer\r
483 //\r
cfe9de52 484\r
28a00297 485 if (*Status == EFI_BUFFER_TOO_SMALL) {\r
486 if (*Buffer != NULL) {\r
487 CoreFreePool (*Buffer);\r
488 }\r
489\r
490 *Buffer = CoreAllocateBootServicesPool (BufferSize);\r
491 if (*Buffer != NULL) {\r
492 TryAgain = TRUE;\r
cfe9de52 493 } else {\r
28a00297 494 *Status = EFI_OUT_OF_RESOURCES;\r
cfe9de52 495 }\r
28a00297 496 }\r
497\r
498 //\r
499 // If there's an error, free the buffer\r
500 //\r
501 if ((!TryAgain) && (EFI_ERROR (*Status)) && (*Buffer)) {\r
502 CoreFreePool (*Buffer);\r
503 *Buffer = NULL;\r
504 }\r
505\r
506 return TryAgain;\r
507}\r
508\r