summaryrefslogtreecommitdiff
path: root/Core/CORE_DXE/FwVolRead.c
blob: dced47f57a3e96311c9656a16ba2efb5bbd15abf (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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/*++

Copyright (c) 2004 - 2007, 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:

  FwVolRead.c

Abstract:

  Implements read firmware file

--*/

#include "FwVolDriver.h"
#include "DxeCore.h"

/*++

Required Alignment    Alignment Value in FFS       Alignment Value in
(bytes)                        Attributes Field               Firmware Volume Interfaces
1                                    0                                     0
2                                    0                                     1
4                                    0                                     2
8                                    0                                     3
16                                   1                                     4
128                                  2                                     7
512                                  3                                     9
1 KB                                 4                                     10
4 KB                                 5                                     12
32 KB                                6                                     15
64 KB                                7                                     16

--*/

UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16}; 


STATIC
EFI_FV_FILE_ATTRIBUTES
FfsAttributes2FvFileAttributes (
  IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes
  )
/*++

  Routine Description:
    Convert the FFS File Attributes to FV File Attributes
    
  Arguments:
    FfsAttributes   -   The attributes of UINT8 type.
    
  Returns:
    The attributes of EFI_FV_FILE_ATTRIBUTES
    
--*/
{
  FfsAttributes = (EFI_FFS_FILE_ATTRIBUTES)((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);
  ASSERT (FfsAttributes < 8);

  return (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[FfsAttributes];
}


#if (PI_SPECIFICATION_VERSION < 0x00010000)

EFI_STATUS
EFIAPI
FvGetNextFile (
  IN         EFI_FIRMWARE_VOLUME_PROTOCOL   *This,
  IN OUT     VOID                            *Key,
  IN OUT     EFI_FV_FILETYPE                *FileType,
  OUT        EFI_GUID                      *NameGuid,
  OUT        EFI_FV_FILE_ATTRIBUTES        *Attributes,
  OUT        UINTN                           *Size
  )
/*++

Routine Description:
    Given the input key, search for the next matching file in the volume.

Arguments:
    This          -   Indicates the calling context.
    FileType      -   FileType is a pointer to a caller allocated
                      EFI_FV_FILETYPE. The GetNextFile() API can filter it's
                      search for files based on the value of *FileType input.
                      A *FileType input of 0 causes GetNextFile() to search for
                      files of all types.  If a file is found, the file's type
                      is returned in *FileType.  *FileType is not modified if
                      no file is found.
    Key           -   Key is a pointer to a caller allocated buffer that
                      contains implementation specific data that is used to
                      track where to begin the search for the next file.
                      The size of the buffer must be at least This->KeySize
                      bytes long. To reinitialize the search and begin from
                      the beginning of the firmware volume, the entire buffer
                      must be cleared to zero. Other than clearing the buffer
                      to initiate a new search, the caller must not modify the
                      data in the buffer between calls to GetNextFile().
    NameGuid      -   NameGuid is a pointer to a caller allocated EFI_GUID.
                      If a file is found, the file's name is returned in
                      *NameGuid.  *NameGuid is not modified if no file is
                      found.
    Attributes    -   Attributes is a pointer to a caller allocated
                      EFI_FV_FILE_ATTRIBUTES.  If a file is found, the file's
                      attributes are returned in *Attributes. *Attributes is
                      not modified if no file is found.
    Size          -   Size is a pointer to a caller allocated UINTN.
                      If a file is found, the file's size is returned in *Size.
                      *Size is not modified if no file is found.

Returns:
    EFI_SUCCESS                 - Successfully find the file.
    EFI_DEVICE_ERROR            - Device error.
    EFI_ACCESS_DENIED           - Fv could not read.
    EFI_NOT_FOUND               - No matching file found.
    EFI_INVALID_PARAMETER       - Invalid parameter

--*/
{
  EFI_STATUS                                  Status;
  FV_DEVICE                                   *FvDevice;
  EFI_FV_ATTRIBUTES                           FvAttributes;
  EFI_FFS_FILE_HEADER                         *FfsFileHeader;
  UINTN                                       *KeyValue;
  EFI_LIST_ENTRY                              *Link;
  FFS_FILE_LIST_ENTRY                         *FfsFileEntry;
  UINTN                                       FileLength;

  FvDevice = FV_DEVICE_FROM_THIS (This);

  Status = FvGetVolumeAttributes (This, &FvAttributes);
  if (EFI_ERROR (Status)){
    return Status;
  }

  //
  // Check if read operation is enabled
  //
  if ((FvAttributes & EFI_FV_READ_STATUS) == 0) {
    return EFI_ACCESS_DENIED;
  }

//*** AMI PORTING BEGIN ***//
//PI 1.1 ++
#if PI_SPECIFICATION_VERSION >= 0x0001000A
  if (*FileType > EFI_FV_FILETYPE_SMM_CORE) {
#else 
  if (*FileType > EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {
#endif
//PI 1.1 --
//*** AMI PORTING END *****//
    //
    // File type needs to be in 0 - 0x0B
    //
    return EFI_NOT_FOUND;
  }

  KeyValue = (UINTN *)Key;
  for (;;) {
    if (*KeyValue == 0) {
      //
      // Search for 1st matching file
      //
      Link = &FvDevice->FfsFileListHeader;
    } else {
      //
      // Key is pointer to FFsFileEntry, so get next one
      //
      Link = (EFI_LIST_ENTRY *)(*KeyValue);
    }

    if (Link->ForwardLink == &FvDevice->FfsFileListHeader) {
      //
      // Next is end of list so we did not find data
      //
      return EFI_NOT_FOUND;
    }

    FfsFileEntry = (FFS_FILE_LIST_ENTRY *)Link->ForwardLink;
    FfsFileHeader = (EFI_FFS_FILE_HEADER *)FfsFileEntry->FfsHeader;

    //
    // remember the key
    //
    *KeyValue = (UINTN)FfsFileEntry;

    if (FfsFileHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {
      //
      // we ignore pad files
      //
      continue;
    }

    if (*FileType == 0) {
      //
      // Process all file types so we have a match
      //
      break;
    }

    if (*FileType == FfsFileHeader->Type) {
      //
      // Found a matching file type
      //
      break;
    }

  } 

  //
  // Return FileType, NameGuid, and Attributes
  //
  *FileType = FfsFileHeader->Type;
  EfiCommonLibCopyMem (NameGuid, &FfsFileHeader->Name, sizeof (EFI_GUID));
  *Attributes = FfsAttributes2FvFileAttributes (FfsFileHeader->Attributes);

  //
  // Read four bytes out of the 3 byte array and throw out extra data
  //
  FileLength = *(UINT32 *)&FfsFileHeader->Size[0] & 0x00FFFFFF;

  //
  // we need to substract the header size
  //
  *Size = FileLength - sizeof(EFI_FFS_FILE_HEADER);

  if (FfsFileHeader->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
    //
    // If tail is present substract it's size;
    //
    *Size -= sizeof(EFI_FFS_FILE_TAIL);
  }

  return EFI_SUCCESS;
}


EFI_STATUS
EFIAPI
FvReadFile (
  IN     EFI_FIRMWARE_VOLUME_PROTOCOL   *This,
  IN     EFI_GUID                       *NameGuid,
  IN OUT VOID                           **Buffer,
  IN OUT UINTN                          *BufferSize,
  OUT    EFI_FV_FILETYPE               *FoundType,
  OUT    EFI_FV_FILE_ATTRIBUTES        *FileAttributes,
  OUT    UINT32                        *AuthenticationStatus
  )
/*++

Routine Description:
    Locates a file in the firmware volume and
    copies it to the supplied buffer.

Arguments:
    This              -   Indicates the calling context.
    NameGuid          -   Pointer to an EFI_GUID, which is the filename.
    Buffer            -   Buffer is a pointer to pointer to a buffer in
                          which the file or section contents or are returned.
    BufferSize        -   BufferSize is a pointer to caller allocated
                          UINTN. On input *BufferSize indicates the size
                          in bytes of the memory region pointed to by
                          Buffer. On output, *BufferSize contains the number
                          of bytes required to read the file.
    FoundType         -   FoundType is a pointer to a caller allocated
                          EFI_FV_FILETYPE that on successful return from Read()
                          contains the type of file read.  This output reflects
                          the file type irrespective of the value of the
                          SectionType input.
    FileAttributes    -   FileAttributes is a pointer to a caller allocated
                          EFI_FV_FILE_ATTRIBUTES.  On successful return from
                          Read(), *FileAttributes contains the attributes of
                          the file read.
    AuthenticationStatus -  AuthenticationStatus is a pointer to a caller
                          allocated UINTN in which the authentication status
                          is returned.
Returns:
    EFI_SUCCESS                   - Successfully read to memory buffer.
    EFI_WARN_BUFFER_TOO_SMALL     - Buffer too small.
    EFI_NOT_FOUND                 - Not found.
    EFI_DEVICE_ERROR              - Device error.
    EFI_ACCESS_DENIED             - Could not read.
    EFI_INVALID_PARAMETER         - Invalid parameter.
    EFI_OUT_OF_RESOURCES          - Not enough buffer to be allocated.

--*/
{
  EFI_STATUS                        Status;
  FV_DEVICE                         *FvDevice;
  EFI_GUID                          SearchNameGuid;
  EFI_FV_FILETYPE                   LocalFoundType;
  EFI_FV_FILE_ATTRIBUTES            LocalAttributes;
  UINTN                             FileSize;
  UINT8                             *SrcPtr;
  EFI_FFS_FILE_HEADER               *FfsHeader;
  UINTN                             InputBufferSize;
  
  if (NULL == NameGuid) {
    return EFI_INVALID_PARAMETER;
  }

  FvDevice = FV_DEVICE_FROM_THIS (This);
  

  //
  // Keep looking until we find the matching NameGuid.
  // The Key is really an FfsFileEntry
  //
  FvDevice->LastKey = 0;
  do {
    LocalFoundType = 0;
    Status = FvGetNextFile (
              This,
              &FvDevice->LastKey,
              &LocalFoundType,
              &SearchNameGuid,
              &LocalAttributes,
              &FileSize
              );
    if (EFI_ERROR (Status)) {
      return EFI_NOT_FOUND;
    }
  } while (!EfiCompareGuid (&SearchNameGuid, NameGuid));

  //
  // Get a pointer to the header
  //
  FfsHeader = FvDevice->LastKey->FfsHeader;

  //
  // Remember callers buffer size
  //
  InputBufferSize = *BufferSize;

  //
  // Calculate return values
  //
  *FoundType = FfsHeader->Type;
  *FileAttributes = FfsAttributes2FvFileAttributes (FfsHeader->Attributes);
  *AuthenticationStatus = 0;
  *BufferSize = FileSize;

  if (Buffer == NULL) {
    //
    // If Buffer is NULL, we only want to get the information colected so far
    //
    return EFI_SUCCESS;
  }

  //
  // Skip over file header
  //
  SrcPtr = ((UINT8 *)FfsHeader) + sizeof (EFI_FFS_FILE_HEADER);

  Status = EFI_SUCCESS;
  if (*Buffer == NULL) {
    //
    // Caller passed in a pointer so allocate buffer for them
    //
    *Buffer = CoreAllocateBootServicesPool (FileSize);
    if (*Buffer == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }
  } else if (FileSize > InputBufferSize) {
    //
    // Callers buffer was not big enough
    // 
    Status = EFI_WARN_BUFFER_TOO_SMALL;
    FileSize = InputBufferSize;
  }
  
  //
  // Copy data into callers buffer 
  //
  EfiCommonLibCopyMem (*Buffer, SrcPtr, FileSize);

  return Status;
}


EFI_STATUS
EFIAPI
FvReadFileSection (
  IN     EFI_FIRMWARE_VOLUME_PROTOCOL   *This,
  IN     EFI_GUID                       *NameGuid,
  IN     EFI_SECTION_TYPE               SectionType,
  IN     UINTN                          SectionInstance,
  IN OUT VOID                           **Buffer,
  IN OUT UINTN                          *BufferSize,
  OUT    UINT32                         *AuthenticationStatus
  )
/*++

  Routine Description:
    Locates a section in a given FFS File and
    copies it to the supplied buffer (not including section header).

  Arguments:
    This              -   Indicates the calling context.
    NameGuid          -   Pointer to an EFI_GUID, which is the filename.
    SectionType       -   Indicates the section type to return.
    SectionInstance   -   Indicates which instance of sections with a type of
                          SectionType to return.
    Buffer            -   Buffer is a pointer to pointer to a buffer in which
                          the file or section contents or are returned.
    BufferSize        -   BufferSize is a pointer to caller allocated UINTN.
    AuthenticationStatus -AuthenticationStatus is a pointer to a caller
                          allocated UINT32 in which the authentication status
                          is returned.

  Returns:
    EFI_SUCCESS                     - Successfully read the file section into buffer.
    EFI_WARN_BUFFER_TOO_SMALL       - Buffer too small.
    EFI_NOT_FOUND                   - Section not found.
    EFI_DEVICE_ERROR                - Device error.
    EFI_ACCESS_DENIED               - Could not read.
    EFI_INVALID_PARAMETER           - Invalid parameter.

--*/
{
  EFI_STATUS                        Status;
  FV_DEVICE                         *FvDevice;
  EFI_FV_FILETYPE                   FileType;
  EFI_FV_FILE_ATTRIBUTES            FileAttributes;
  UINTN                             FileSize;
  UINT8                             *FileBuffer;
  EFI_SECTION_EXTRACTION_PROTOCOL   *Sep;
  FFS_FILE_LIST_ENTRY               *FfsEntry;
 
  if (NULL == NameGuid || Buffer == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  FvDevice = FV_DEVICE_FROM_THIS (This);

  //
  // Read the whole file into buffer
  //
  FileBuffer = NULL;
  Status = FvReadFile (
            This,
            NameGuid,
            &FileBuffer,
            &FileSize,
            &FileType,
            &FileAttributes,
            AuthenticationStatus
            );             
  //
  // Get the last key used by our call to FvReadFile as it is the FfsEntry for this file.
  //  
  FfsEntry = (FFS_FILE_LIST_ENTRY *)FvDevice->LastKey;

  if (EFI_ERROR (Status)) {
    return Status;
  }
  
  //
  // Check to see that the file actually HAS sections before we go any further.
  //
  if (FileType == EFI_FV_FILETYPE_RAW) {
    Status = EFI_NOT_FOUND;
    goto Done;
  }

  //
  // Use FfsEntry to cache Section Extraction Protocol Inforomation
  //
  if (FfsEntry->StreamHandle == 0) {
    //
    // Located the protocol
    //
    Status = CoreLocateProtocol (&gEfiSectionExtractionProtocolGuid, NULL, &Sep);
    //
    // Section Extraction Protocol is part of Dxe Core so this should never fail
    //
    ASSERT_EFI_ERROR (Status);

    Status = Sep->OpenSectionStream (
                    Sep,
                    FileSize,
                    FileBuffer,
                    &FfsEntry->StreamHandle
                    );
    if (EFI_ERROR (Status)) {
      goto Done;
    }

    FfsEntry->Sep = Sep;
  } else {
    //
    // Get cached copy of Sep
    //
    Sep = FfsEntry->Sep;
  }

  //
  // If SectionType == 0 We need the whole section stream
  //
  Status = Sep->GetSection (
                  Sep,
                            FfsEntry->StreamHandle,
                            (SectionType == 0) ? NULL : &SectionType,
                            NULL,
                            (SectionType == 0) ? 0 : SectionInstance,
                            Buffer,
                            BufferSize,
                            AuthenticationStatus
                            );

  //
  // Close of stream defered to close of FfsHeader list to allow SEP to cache data
  //

Done:
  CoreFreePool (FileBuffer);

  return Status;
}

#else

EFI_STATUS
EFIAPI
FvGetNextFile (
  IN CONST   EFI_FIRMWARE_VOLUME2_PROTOCOL   *This,
  IN OUT     VOID                            *Key,
  IN OUT     EFI_FV_FILETYPE                *FileType,
  OUT        EFI_GUID                      *NameGuid,
  OUT        EFI_FV_FILE_ATTRIBUTES        *Attributes,
  OUT        UINTN                           *Size
  )
/*++

Routine Description:
    Given the input key, search for the next matching file in the volume.

Arguments:
    This          -   Indicates the calling context.
    FileType      -   FileType is a pointer to a caller allocated
                      EFI_FV_FILETYPE. The GetNextFile() API can filter it's
                      search for files based on the value of *FileType input.
                      A *FileType input of 0 causes GetNextFile() to search for
                      files of all types.  If a file is found, the file's type
                      is returned in *FileType.  *FileType is not modified if
                      no file is found.
    Key           -   Key is a pointer to a caller allocated buffer that
                      contains implementation specific data that is used to
                      track where to begin the search for the next file.
                      The size of the buffer must be at least This->KeySize
                      bytes long. To reinitialize the search and begin from
                      the beginning of the firmware volume, the entire buffer
                      must be cleared to zero. Other than clearing the buffer
                      to initiate a new search, the caller must not modify the
                      data in the buffer between calls to GetNextFile().
    NameGuid      -   NameGuid is a pointer to a caller allocated EFI_GUID.
                      If a file is found, the file's name is returned in
                      *NameGuid.  *NameGuid is not modified if no file is
                      found.
    Attributes    -   Attributes is a pointer to a caller allocated
                      EFI_FV_FILE_ATTRIBUTES.  If a file is found, the file's
                      attributes are returned in *Attributes. *Attributes is
                      not modified if no file is found.
    Size          -   Size is a pointer to a caller allocated UINTN.
                      If a file is found, the file's size is returned in *Size.
                      *Size is not modified if no file is found.

Returns:
    EFI_SUCCESS                 - Successfully find the file.
    EFI_DEVICE_ERROR            - Device error.
    EFI_ACCESS_DENIED           - Fv could not read.
    EFI_NOT_FOUND               - No matching file found.
    EFI_INVALID_PARAMETER       - Invalid parameter

--*/
{
  EFI_STATUS                                  Status;
  FV_DEVICE                                   *FvDevice;
  EFI_FV_ATTRIBUTES                           FvAttributes;
  EFI_FFS_FILE_HEADER                         *FfsFileHeader;
  UINTN                                       *KeyValue;
  EFI_LIST_ENTRY                              *Link;
  FFS_FILE_LIST_ENTRY                         *FfsFileEntry;
  UINTN                                       FileLength;

  FvDevice = FV_DEVICE_FROM_THIS (This);

  Status = FvGetVolumeAttributes (This, &FvAttributes);
  if (EFI_ERROR (Status)){
    return Status;
  }

  //
  // Check if read operation is enabled
  //
  if ((FvAttributes & EFI_FV2_READ_STATUS) == 0) {
    return EFI_ACCESS_DENIED;
  }

//*** AMI PORTING BEGIN ***//
//PI 1.1 ++
#if PI_SPECIFICATION_VERSION >= 0x0001000A
  if (*FileType > EFI_FV_FILETYPE_SMM_CORE) {
#else 
  if (*FileType > EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {
#endif
//PI 1.1 --
//*** AMI PORTING END *****//
    //
    // File type needs to be in 0 - 0x0B
    //
    return EFI_NOT_FOUND;
  }

  KeyValue = (UINTN *)Key;
  for (;;) {
    if (*KeyValue == 0) {
      //
      // Search for 1st matching file
      //
      Link = &FvDevice->FfsFileListHeader;
    } else {
      //
      // Key is pointer to FFsFileEntry, so get next one
      //
      Link = (EFI_LIST_ENTRY *)(*KeyValue);
    }

    if (Link->ForwardLink == &FvDevice->FfsFileListHeader) {
      //
      // Next is end of list so we did not find data
      //
      return EFI_NOT_FOUND;
    }

    FfsFileEntry = (FFS_FILE_LIST_ENTRY *)Link->ForwardLink;
    FfsFileHeader = (EFI_FFS_FILE_HEADER *)FfsFileEntry->FfsHeader;

    //
    // remember the key
    //
    *KeyValue = (UINTN)FfsFileEntry;

    if (FfsFileHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {
      //
      // we ignore pad files
      //
      continue;
    }

    if (*FileType == 0) {
      //
      // Process all file types so we have a match
      //
      break;
    }

    if (*FileType == FfsFileHeader->Type) {
      //
      // Found a matching file type
      //
      break;
    }

  } 

  //
  // Return FileType, NameGuid, and Attributes
  //
  *FileType = FfsFileHeader->Type;
  EfiCommonLibCopyMem (NameGuid, &FfsFileHeader->Name, sizeof (EFI_GUID));
  *Attributes = FfsAttributes2FvFileAttributes (FfsFileHeader->Attributes);

  //
  // Read four bytes out of the 3 byte array and throw out extra data
  //
  FileLength = *(UINT32 *)&FfsFileHeader->Size[0] & 0x00FFFFFF;

  //
  // we need to substract the header size
  //
  *Size = FileLength - sizeof(EFI_FFS_FILE_HEADER);

//*** AMI PORTING BEGIN ***//
//Bug fix: FFS_ATTRIB_TAIL_PRESENT attribute dies not exist in PI Specification.
#if PI_SPECIFICATION_VERSION < 0x00010000
//*** AMI PORTING END *****//
  if (FfsFileHeader->Attributes & FFS_ATTRIB_TAIL_PRESENT) {
    //
    // If tail is present substract it's size;
    //
    *Size -= sizeof(EFI_FFS_FILE_TAIL);
  }
//*** AMI PORTING BEGIN ***//
//see comments above
#endif //#if PI_SPECIFICATION_VERSION < 0x00010000
//*** AMI PORTING END *****//
  return EFI_SUCCESS;
}


EFI_STATUS
EFIAPI
FvReadFile (
  IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL  *This,
  IN CONST EFI_GUID                       *NameGuid,
  IN OUT VOID                             **Buffer,
  IN OUT UINTN                            *BufferSize,
  OUT    EFI_FV_FILETYPE                  *FoundType,
  OUT    EFI_FV_FILE_ATTRIBUTES           *FileAttributes,
  OUT    UINT32                           *AuthenticationStatus
  )
/*++

Routine Description:
    Locates a file in the firmware volume and
    copies it to the supplied buffer.

Arguments:
    This              -   Indicates the calling context.
    NameGuid          -   Pointer to an EFI_GUID, which is the filename.
    Buffer            -   Buffer is a pointer to pointer to a buffer in
                          which the file or section contents or are returned.
    BufferSize        -   BufferSize is a pointer to caller allocated
                          UINTN. On input *BufferSize indicates the size
                          in bytes of the memory region pointed to by
                          Buffer. On output, *BufferSize contains the number
                          of bytes required to read the file.
    FoundType         -   FoundType is a pointer to a caller allocated
                          EFI_FV_FILETYPE that on successful return from Read()
                          contains the type of file read.  This output reflects
                          the file type irrespective of the value of the
                          SectionType input.
    FileAttributes    -   FileAttributes is a pointer to a caller allocated
                          EFI_FV_FILE_ATTRIBUTES.  On successful return from
                          Read(), *FileAttributes contains the attributes of
                          the file read.
    AuthenticationStatus -  AuthenticationStatus is a pointer to a caller
                          allocated UINTN in which the authentication status
                          is returned.
Returns:
    EFI_SUCCESS                   - Successfully read to memory buffer.
    EFI_WARN_BUFFER_TOO_SMALL     - Buffer too small.
    EFI_NOT_FOUND                 - Not found.
    EFI_DEVICE_ERROR              - Device error.
    EFI_ACCESS_DENIED             - Could not read.
    EFI_INVALID_PARAMETER         - Invalid parameter.
    EFI_OUT_OF_RESOURCES          - Not enough buffer to be allocated.

--*/
{
  EFI_STATUS                        Status;
  FV_DEVICE                         *FvDevice;
  EFI_GUID                          SearchNameGuid;
  EFI_FV_FILETYPE                   LocalFoundType;
  EFI_FV_FILE_ATTRIBUTES            LocalAttributes;
  UINTN                             FileSize;
  UINT8                             *SrcPtr;
  EFI_FFS_FILE_HEADER               *FfsHeader;
  UINTN                             InputBufferSize;
  
  if (NULL == NameGuid) {
    return EFI_INVALID_PARAMETER;
  }

  FvDevice = FV_DEVICE_FROM_THIS (This);
  

  //
  // Keep looking until we find the matching NameGuid.
  // The Key is really an FfsFileEntry
  //
  FvDevice->LastKey = 0;
  do {
    LocalFoundType = 0;
    Status = FvGetNextFile (
              This,
              &FvDevice->LastKey,
              &LocalFoundType,
              &SearchNameGuid,
              &LocalAttributes,
              &FileSize
              );
    if (EFI_ERROR (Status)) {
      return EFI_NOT_FOUND;
    }
  } while (!EfiCompareGuid (&SearchNameGuid, (EFI_GUID*)NameGuid));

  //
  // Get a pointer to the header
  //
  FfsHeader = FvDevice->LastKey->FfsHeader;

  //
  // Remember callers buffer size
  //
  InputBufferSize = *BufferSize;

  //
  // Calculate return values
  //
  *FoundType = FfsHeader->Type;
  *FileAttributes = FfsAttributes2FvFileAttributes (FfsHeader->Attributes);
  *AuthenticationStatus = 0;
  *BufferSize = FileSize;

  if (Buffer == NULL) {
    //
    // If Buffer is NULL, we only want to get the information colected so far
    //
    return EFI_SUCCESS;
  }

  //
  // Skip over file header
  //
  SrcPtr = ((UINT8 *)FfsHeader) + sizeof (EFI_FFS_FILE_HEADER);

  Status = EFI_SUCCESS;
  if (*Buffer == NULL) {
    //
    // Caller passed in a pointer so allocate buffer for them
    //
    *Buffer = CoreAllocateBootServicesPool (FileSize);
    if (*Buffer == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }
  } else if (FileSize > InputBufferSize) {
    //
    // Callers buffer was not big enough
    // 
    Status = EFI_WARN_BUFFER_TOO_SMALL;
    FileSize = InputBufferSize;
  }
  
  //
  // Copy data into callers buffer 
  //
  EfiCommonLibCopyMem (*Buffer, SrcPtr, FileSize);

  return Status;
}

EFI_STATUS
EFIAPI
FvReadFileSection (
  IN CONST EFI_FIRMWARE_VOLUME2_PROTOCOL   *This,
  IN CONST EFI_GUID                       *NameGuid,
  IN     EFI_SECTION_TYPE                 SectionType,
  IN     UINTN                            SectionInstance,
  IN OUT VOID                             **Buffer,
  IN OUT UINTN                            *BufferSize,
  OUT    UINT32                           *AuthenticationStatus
  )
/*++

  Routine Description:
    Locates a section in a given FFS File and
    copies it to the supplied buffer (not including section header).

  Arguments:
    This              -   Indicates the calling context.
    NameGuid          -   Pointer to an EFI_GUID, which is the filename.
    SectionType       -   Indicates the section type to return.
    SectionInstance   -   Indicates which instance of sections with a type of
                          SectionType to return.
    Buffer            -   Buffer is a pointer to pointer to a buffer in which
                          the file or section contents or are returned.
    BufferSize        -   BufferSize is a pointer to caller allocated UINTN.
    AuthenticationStatus -AuthenticationStatus is a pointer to a caller
                          allocated UINT32 in which the authentication status
                          is returned.

  Returns:
    EFI_SUCCESS                     - Successfully read the file section into buffer.
    EFI_WARN_BUFFER_TOO_SMALL       - Buffer too small.
    EFI_NOT_FOUND                   - Section not found.
    EFI_DEVICE_ERROR                - Device error.
    EFI_ACCESS_DENIED               - Could not read.
    EFI_INVALID_PARAMETER           - Invalid parameter.

--*/
{
  EFI_STATUS                        Status;
  FV_DEVICE                         *FvDevice;
  EFI_FV_FILETYPE                   FileType;
  EFI_FV_FILE_ATTRIBUTES            FileAttributes;
  UINTN                             FileSize;
  UINT8                             *FileBuffer;
  EFI_SECTION_EXTRACTION_PROTOCOL   *Sep;
  FFS_FILE_LIST_ENTRY               *FfsEntry;
 
  if (NULL == NameGuid || Buffer == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  FvDevice = FV_DEVICE_FROM_THIS (This);

  //
  // Read the whole file into buffer
  //
  FileBuffer = NULL;
  Status = FvReadFile (
            This,
            NameGuid,
            &FileBuffer,
            &FileSize,
            &FileType,
            &FileAttributes,
            AuthenticationStatus
            );             
  //
  // Get the last key used by our call to FvReadFile as it is the FfsEntry for this file.
  //  
  FfsEntry = (FFS_FILE_LIST_ENTRY *)FvDevice->LastKey;

  if (EFI_ERROR (Status)) {
    return Status;
  }
  
  //
  // Check to see that the file actually HAS sections before we go any further.
  //
  if (FileType == EFI_FV_FILETYPE_RAW) {
    Status = EFI_NOT_FOUND;
    goto Done;
  }

  //
  // Use FfsEntry to cache Section Extraction Protocol Inforomation
  //
  if (FfsEntry->StreamHandle == 0) {
    //
    // Located the protocol
    //
    Status = CoreLocateProtocol (&gEfiSectionExtractionProtocolGuid, NULL, &Sep);
    //
    // Section Extraction Protocol is part of Dxe Core so this should never fail
    //
    ASSERT_EFI_ERROR (Status);

    Status = Sep->OpenSectionStream (
                    Sep,
                    FileSize,
                    FileBuffer,
                    &FfsEntry->StreamHandle
                    );
    if (EFI_ERROR (Status)) {
      goto Done;
    }

    FfsEntry->Sep = Sep;
  } else {
    //
    // Get cached copy of Sep
    //
    Sep = FfsEntry->Sep;
  }

  //
  // If SectionType == 0 We need the whole section stream
  //
  Status = Sep->GetSection (
                  Sep,
                            FfsEntry->StreamHandle,
                            (SectionType == 0) ? NULL : &SectionType,
                            NULL,
                            (SectionType == 0) ? 0 : SectionInstance,
                            Buffer,
                            BufferSize,
                            AuthenticationStatus
                            );

  //
  // Close of stream defered to close of FfsHeader list to allow SEP to cache data
  //

Done:
  CoreFreePool (FileBuffer);

  return Status;
}


#endif