summaryrefslogtreecommitdiff
path: root/EdkModulePkg/Core/Dxe/Image/ImageFile.c
blob: 999cd694ad5f03722cd73712d70c6247b0981ac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*++

Copyright (c) 2006, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:
  
  ImageFile.c


Abstract:

  


Revision History

--*/

#include <DxeMain.h>


VOID
CoreDevicePathToFileName (
  IN  FILEPATH_DEVICE_PATH      *FilePath,
  OUT CHAR16                    **String
  );



EFI_STATUS
CoreOpenImageFile (
  IN BOOLEAN                        BootPolicy,
  IN VOID                           *SourceBuffer   OPTIONAL,
  IN UINTN                          SourceSize,
  IN OUT EFI_DEVICE_PATH_PROTOCOL   *FilePath,
  OUT EFI_HANDLE                    *DeviceHandle,
  IN IMAGE_FILE_HANDLE              *ImageFileHandle,
  OUT UINT32                        *AuthenticationStatus
  )
/*++

Routine Description:

    Opens a file for (simple) reading.  The simple read abstraction
    will access the file either from a memory copy, from a file
    system interface, or from the load file interface.

Arguments:

  BootPolicy    - Policy for Open Image File.
  SourceBuffer  - Pointer to the memory location containing copy
                  of the image to be loaded.
  SourceSize    - The size in bytes of SourceBuffer.
  FilePath      - The specific file path from which the image is loaded
  DeviceHandle  - Pointer to the return device handle.
  ImageFileHandle      - Pointer to the image file handle.
  AuthenticationStatus - Pointer to a caller-allocated UINT32 in which the authentication status is returned. 
    
Returns:

    EFI_SUCCESS     - Image file successfully opened.
    
    EFI_LOAD_ERROR  - If the caller passed a copy of the file, and SourceSize is 0.
    
    EFI_INVALID_PARAMETER   - File path is not valid.
    
    EFI_NOT_FOUND   - File not found.

--*/
{
  EFI_STATUS                        Status;
  EFI_DEVICE_PATH_PROTOCOL          *TempFilePath;
  FILEPATH_DEVICE_PATH              *FilePathNode;
  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FwVolFilePathNode;
  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL   *Volume;
  EFI_FILE_HANDLE                   FileHandle;
  EFI_FILE_HANDLE                   LastHandle;
  EFI_LOAD_FILE_PROTOCOL            *LoadFile;
  EFI_FIRMWARE_VOLUME_PROTOCOL      *FwVol;
  EFI_SECTION_TYPE                  SectionType;
  UINT8                             *Pe32Buffer;
  UINTN                             Pe32BufferSize;
  EFI_FV_FILETYPE                   Type;
  EFI_FV_FILE_ATTRIBUTES            Attrib;
  EFI_FILE_INFO                     *FileInfo;
  UINTN                             FileInfoSize;
  EFI_GUID                          *NameGuid;

  *AuthenticationStatus = 0;
  ZeroMem (ImageFileHandle, sizeof (IMAGE_FILE_HANDLE));
  ImageFileHandle->Signature = IMAGE_FILE_HANDLE_SIGNATURE;

  //
  // If the caller passed a copy of the file, then just use it
  //
  if (SourceBuffer != NULL) {
    ImageFileHandle->Source     = SourceBuffer;
    ImageFileHandle->SourceSize = SourceSize;
    *DeviceHandle     = NULL;
    if (SourceSize > 0) {
      Status = EFI_SUCCESS;
    } else {
      Status = EFI_LOAD_ERROR;
    }
    goto Done;
  }

  //
  // Make sure FilePath is valid
  //
  if (FilePath == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // Check to see if it's in a Firmware Volume
  //
  FwVolFilePathNode = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)FilePath;
  Status = CoreDevicePathToInterface (
            &gEfiFirmwareVolumeProtocolGuid, 
            (EFI_DEVICE_PATH_PROTOCOL **)&FwVolFilePathNode, 
            (VOID*)&FwVol, 
            DeviceHandle
            );
  if (!EFI_ERROR (Status)) {
    //
    // For FwVol File system there is only a single file name that is a GUID.
    //
    NameGuid = EfiGetNameGuidFromFwVolDevicePathNode (FwVolFilePathNode);
    if (NameGuid != NULL) {

      SectionType = EFI_SECTION_PE32;
      Pe32Buffer  = NULL;
      Status = FwVol->ReadSection (
                        FwVol, 
                        NameGuid,  
                        SectionType,   
                        0,
                        (VOID **)&Pe32Buffer,
                        &Pe32BufferSize,
                        AuthenticationStatus
                        );
      if (EFI_ERROR (Status)) {
        //
        // Try a raw file, since a PE32 SECTION does not exist
        //
        if (Pe32Buffer != NULL) {
          CoreFreePool (Pe32Buffer);
          *AuthenticationStatus = 0;
        }
        Pe32Buffer = NULL;
        Status = FwVol->ReadFile (
                          FwVol, 
                          NameGuid, 
                          (VOID **)&Pe32Buffer,
                          &Pe32BufferSize,
                          &Type,
                          &Attrib,
                          AuthenticationStatus
                          );
      }
            
      if (!EFI_ERROR (Status)) {
        //
        // One of the reads passed so we are done
        //
        ImageFileHandle->Source = Pe32Buffer;
        ImageFileHandle->SourceSize = Pe32BufferSize;
        ImageFileHandle->FreeBuffer = TRUE;
        goto Done;
      }
    }
  }

  //
  // Attempt to access the file via a file system interface
  //
  FilePathNode = (FILEPATH_DEVICE_PATH *) FilePath;
  Status = CoreDevicePathToInterface (
            &gEfiSimpleFileSystemProtocolGuid, 
            (EFI_DEVICE_PATH_PROTOCOL **)&FilePathNode, 
            (VOID*)&Volume, 
            DeviceHandle
            );
  if (!EFI_ERROR (Status)) {
    //
    // Open the Volume to get the File System handle
    //
    Status = Volume->OpenVolume (Volume, &FileHandle);
    if (!EFI_ERROR (Status)) {
     
      //
      // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
      //  directory information and filename can be seperate. The goal is to inch
      //  our way down each device path node and close the previous node
      //
      while (!IsDevicePathEnd (&FilePathNode->Header)) {
        if (DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH ||
            DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP) {
          Status = EFI_UNSUPPORTED;
        }

        if (EFI_ERROR (Status)) {
          //
          // Exit loop on Error
          //
          break;
        }

        LastHandle = FileHandle;
        FileHandle = NULL;
        Status = LastHandle->Open (
                              LastHandle,
                              &FileHandle,
                              FilePathNode->PathName,
                              EFI_FILE_MODE_READ,
                              0
                              );

        //
        // Close the previous node
        //
        LastHandle->Close (LastHandle);

        FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header);
      }

      if (!EFI_ERROR (Status)) {
        //
        // We have found the file. Now we need to read it. Before we can read the file we need to
        // figure out how big the file is.
        //
        FileInfo = NULL;
        FileInfoSize = sizeof (EFI_FILE_INFO);
        while (CoreGrowBuffer (&Status, (VOID **)&FileInfo, FileInfoSize)) {
          //
          // Automatically allocate buffer of the correct size and make the call
          //
          Status = FileHandle->GetInfo (
                                FileHandle,
                                &gEfiFileInfoGuid,
                                &FileInfoSize,
                                FileInfo                               
                                );
        }
        if (!EFI_ERROR (Status)) {
          //
          // Allocate space for the file
          //
          ImageFileHandle->Source = CoreAllocateBootServicesPool ((UINTN)FileInfo->FileSize);
          if (ImageFileHandle->Source != NULL) {
            //
            // Read the file into the buffer we allocated
            //
            ImageFileHandle->SourceSize = (UINTN)FileInfo->FileSize;
            ImageFileHandle->FreeBuffer = TRUE;
            Status = FileHandle->Read (FileHandle, &ImageFileHandle->SourceSize, ImageFileHandle->Source);

            //
            // Close the file since we are done
            //
            FileHandle->Close (FileHandle);
          } else {
            Status = EFI_OUT_OF_RESOURCES;
          }

          goto Done;
        }
      }
    }
  } 


  //
  // Try LoadFile style
  //

  TempFilePath = FilePath;
  Status = CoreDevicePathToInterface (
              &gEfiLoadFileProtocolGuid,
              &TempFilePath,
              (VOID*)&LoadFile,
              DeviceHandle
              );
  if (!EFI_ERROR (Status)) {
    //
    // Call LoadFile with the correct buffer size
    //
    while (CoreGrowBuffer (&Status, (VOID **)&ImageFileHandle->Source, ImageFileHandle->SourceSize)) {
      Status = LoadFile->LoadFile (
                           LoadFile,
                           TempFilePath,
                           BootPolicy,
                           &ImageFileHandle->SourceSize,
                           ImageFileHandle->Source
                           );
      //
      // If success or other error happens, stop loop
      //
      if (Status != EFI_BUFFER_TOO_SMALL) {
        break;
      }
    }

    if (!EFI_ERROR (Status) || Status == EFI_ALREADY_STARTED) {
      ImageFileHandle->FreeBuffer = TRUE;
      goto Done;
    }
  }

  //
  // Nothing else to try
  //
  DEBUG ((EFI_D_LOAD|EFI_D_WARN, "CoreOpenImageFile: Device did not support a known load protocol\n"));
  Status = EFI_NOT_FOUND;

Done:

  //
  // If the file was not accessed, clean up
  //
  if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
    if (ImageFileHandle->FreeBuffer) {
      //
      // Free the source buffer if we allocated it
      //
      CoreFreePool (ImageFileHandle->Source);
    }
  }

  return Status;
}



EFI_STATUS
EFIAPI
CoreReadImageFile (
  IN     VOID    *UserHandle,
  IN     UINTN   Offset,
  IN OUT UINTN   *ReadSize,
  OUT    VOID    *Buffer
  )
/*++

Routine Description:

  Read image file (specified by UserHandle) into user specified buffer with specified offset
  and length.

Arguments:

  UserHandle      - Image file handle
  
  Offset          - Offset to the source file
  
  ReadSize        - For input, pointer of size to read;
                    For output, pointer of size actually read.
  
  Buffer          - Buffer to write into

Returns:

  EFI_SUCCESS     - Successfully read the specified part of file into buffer.

--*/
{
  UINTN               EndPosition;
  IMAGE_FILE_HANDLE  *FHand;

  FHand = (IMAGE_FILE_HANDLE  *)UserHandle;
  ASSERT (FHand->Signature == IMAGE_FILE_HANDLE_SIGNATURE);

  //
  // Move data from our local copy of the file
  //
  EndPosition = Offset + *ReadSize;
  if (EndPosition > FHand->SourceSize) {
    *ReadSize = (UINT32)(FHand->SourceSize - Offset);
  }  
  if (Offset >= FHand->SourceSize) {
      *ReadSize = 0;
  }

  CopyMem (Buffer, (CHAR8 *)FHand->Source + Offset, *ReadSize);
  return EFI_SUCCESS;
}

EFI_STATUS
CoreDevicePathToInterface (
  IN EFI_GUID                     *Protocol,
  IN EFI_DEVICE_PATH_PROTOCOL     **FilePath,
  OUT VOID                        **Interface,
  OUT EFI_HANDLE                  *Handle
  )
/*++

Routine Description:

  Search a handle to a device on a specified device path that supports a specified protocol,
  interface of that protocol on that handle is another output.

Arguments:

  Protocol      - The protocol to search for
  
  FilePath      - The specified device path
  
  Interface     - Interface of the protocol on the handle
  
  Handle        - The handle to the device on the specified device path that supports the protocol.
  
Returns:

  Status code.

--*/
{
  EFI_STATUS                      Status;

  Status = CoreLocateDevicePath (Protocol, FilePath, Handle);
  if (!EFI_ERROR (Status)) {
    Status = CoreHandleProtocol (*Handle, Protocol, Interface);
  }
  return Status;
}


VOID
CoreDevicePathToFileName (
  IN  FILEPATH_DEVICE_PATH      *FilePath,
  OUT CHAR16                    **String
  )
/*++

Routine Description:

  Transfer a device's full path a string.

Arguments:

  FilePath      - Device path
  
  String        - The string represent the device's full path

Returns:

  None

--*/
{
  UINTN                     StringSize;
  FILEPATH_DEVICE_PATH      *FilePathNode;
  CHAR16                    *Str;

  *String = NULL;
  StringSize = 0;
  FilePathNode = FilePath;
  while (!IsDevicePathEnd (&FilePathNode->Header)) {

    //
    // For filesystem access each node should be a filepath component
    //
    if (DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH ||
        DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP) {

      return;
    }

    StringSize += StrLen (FilePathNode->PathName);

    FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header);
  }

  *String = CoreAllocateBootServicesPool (StringSize);
  if (*String == NULL) {
    return;
  }

  FilePathNode = FilePath;
  Str = *String;
  while (!IsDevicePathEnd (&FilePathNode->Header)) {
    StrCat (Str, FilePathNode->PathName);
    FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header);
  }
}


BOOLEAN
CoreGrowBuffer (
  IN OUT EFI_STATUS   *Status,
  IN OUT VOID         **Buffer,
  IN UINTN            BufferSize
  )
/*++

Routine Description:

    Helper function called as part of the code needed
    to allocate the proper sized buffer for various 
    EFI interfaces.

Arguments:

    Status      - Current status

    Buffer      - Current allocated buffer, or NULL

    BufferSize  - Current buffer size needed
    
Returns:
    
    TRUE - if the buffer was reallocated and the caller 
    should try the API again.

    FALSE - buffer could not be allocated and the caller
    should not try the API again.

--*/
{
  BOOLEAN         TryAgain;

  TryAgain = FALSE;
  //
  // If this is an initial request, buffer will be null with a new buffer size
  //
  if (*Buffer == NULL) {
    *Status = EFI_BUFFER_TOO_SMALL;
  }

  if (BufferSize == 0) {
    return TRUE;
  }

  //
  // If the status code is "buffer too small", resize the buffer
  //
      
  if (*Status == EFI_BUFFER_TOO_SMALL) {
    if (*Buffer != NULL) {
      CoreFreePool (*Buffer);
    }

    *Buffer = CoreAllocateBootServicesPool (BufferSize);
    if (*Buffer != NULL) {
      TryAgain = TRUE;
    } else {    
      *Status = EFI_OUT_OF_RESOURCES;
    } 
  }

  //
  // If there's an error, free the buffer
  //
  if ((!TryAgain) && (EFI_ERROR (*Status)) && (*Buffer)) {
    CoreFreePool (*Buffer);
    *Buffer = NULL;
  }

  return TryAgain;
}