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