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