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