summaryrefslogtreecommitdiff
path: root/Board/ReportFV2.c
blob: b4ed5894208c35fcf5dc25053daa39b9dbe4943f (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
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************

//**********************************************************************
// $Header: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/Board/ReportFV2.c 2     10/25/12 9:20a Wesleychen $
//
// $Revision: 2 $
//
// $Date: 10/25/12 9:20a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/Projects/Intel/Haswell/LynxPoint_SharkBay-DT_Crb_1AQQW/Board/ReportFV2.c $
// 
// 2     10/25/12 9:20a Wesleychen
// Update Board to 4.6.5.4_Board_31.
// 
// 1     2/24/12 7:59a Victortu
// [TAG]  		EIPEIP71398
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	BIOS Crisis Recovery halt on F1/3b very long time if
// LZMA_SUPPORT = 1
// [Solution]  	Make sure all recovery-related FFS are outside of nested
// FV and do not publish it.
// 
// 11    8/16/12 12:30p Artems
// [TAG]  		EIP N/A
// [Category]  	Improvement
// [Description]  	Added OEM hook to PublishNestedFv function
// [Files]  		Board.sdl Board.mak ReportFv2.c
// 
// 10    8/03/12 5:26p Artems
// [TAG]  		EIP N/A
// [Category]  	Bug Fix
// [Severity]  	Critical
// [Symptom]  	System hangs randomly
// [RootCause]  	Rom layout section beginning is marked with unique GUID.
// However same GUID defined in code also, which leads to presence
// of two instances of the GUID in ROM file. Depending on which
// will be picked system hangs as underlying data can be incorrect
// [Solution]  	Added additional verification of validity of ROM layout
// section
// [Files]  		ReportFV2.c
// 
// 9     11/11/11 3:19p Artems
// Publish ROM layout hob on recovery boot path
// Add verification of parent of nested FV
// 
// 8     11/01/11 4:34p Artems
// Enaabled CheckBeforePublishing for Core 4.6.5.2 compatibility.
// 
// 7     10/05/11 5:25p Artems
// Disabled CheckBeforePublishing for Core 4.6.5.1 compatibility.
// 
// 6     10/05/11 4:49p Artems
// EIP 65805 - fixed problem with too many FVs published on recovery boot
// path
// 
// 5     3/23/11 12:27p Artems
// Enhancement - do not terminate publishing of FVs when corrupted volume
// encountered
// 
// 4     3/04/11 4:15p Artems
// Moved publishing of nested FV to DxeIpl stage for performance reasons
// 
// 3     2/15/11 12:38p Artems
// Added RomLayoutHob publishing
// 
// 2     2/07/11 5:22p Artems
// EIP 53374: Coding standard changes - replaced tabs with spaces, added
// functions headers
// 
// 1     2/04/11 7:56p Artems
// 
// 6     1/13/10 2:13p Felixp
// 
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:  ReportFv.c
//
// Description: Implementation of flexible ROM layout infrastructure support
//
//<AMI_FHDR_END>
//**********************************************************************

#include <Token.h>
#include <AmiPeiLib.h>
#include <AmiHobs.h>
#include <RomLayout.h>
#include <Ppi/FirmwareVolumeInfo.h>

#define ROM_LAYOUT_FFS_GUID \
    { 0x0DCA793A, 0xEA96, 0x42d8, 0xBD, 0x7B, 0xDC, 0x7F, 0x68, 0x4E, 0x38, 0xC1 }

#define ROM_LAYOUT_SECTION_GUID \
    { 0x88A15A4F, 0x977D, 0x4682, 0xB1, 0x7C, 0xDA, 0x1F, 0x31, 0x6C, 0x1F, 0x32 }

#define NULL_GUID \
    { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }

typedef struct _ROM_LAYOUT_SECTION {
    EFI_GUID FfsGuid;
    UINT8 Reserved[12];
    EFI_GUID SectionGuid;
    ROM_AREA Layout[1];
} ROM_LAYOUT_SECTION;

static EFI_GUID NullGuid = NULL_GUID;
static EFI_GUID RomLayoutFfsGuid = ROM_LAYOUT_FFS_GUID;
static EFI_GUID RomLayoutSectionGuid = ROM_LAYOUT_SECTION_GUID;
static EFI_GUID gEfiFirmwareVolumeInfoPpiGuid = EFI_PEI_FIRMWARE_VOLUME_INFO_PPI_GUID;

typedef BOOLEAN (PROCESS_FV_BEFORE_PUBLISHING)(
    IN EFI_PEI_SERVICES **PeiServices, IN OUT ROM_AREA *Area, IN UINT32 FvType
);
typedef BOOLEAN (PROCESS_DXEFV_IN_DXEIPL)(
    IN EFI_PEI_SERVICES **PeiServices, IN OUT ROM_AREA *Area
);

extern PROCESS_FV_BEFORE_PUBLISHING ProcessFvBeforePublishing EndOfFvHookList1;
extern PROCESS_DXEFV_IN_DXEIPL ProcessDxeFvInDxeIpl EndOfFvHookList2;

PROCESS_FV_BEFORE_PUBLISHING* ProcessFvBeforePublishingList[]=
    { ProcessFvBeforePublishing NULL };
PROCESS_DXEFV_IN_DXEIPL* ProcessDxeFvInDxeIplList[]=
    { ProcessDxeFvInDxeIpl NULL };

typedef EFI_STATUS (PROCESS_NESTED_FV_BEFORE_PUBLISHING)(
    IN EFI_PEI_SERVICES **PeiServices, 
    IN OUT ROM_AREA *Area,
    OUT EFI_FIRMWARE_VOLUME_HEADER **Fv, 
    OUT EFI_FIRMWARE_VOLUME_HEADER **NewFv, 
    OUT EFI_FFS_FILE_HEADER **Nfv  
);

extern PROCESS_NESTED_FV_BEFORE_PUBLISHING ProcessNestedFvBeforePublishing EndOfFvHookList3;
PROCESS_NESTED_FV_BEFORE_PUBLISHING* ProcessNestedFvBeforePublishingList[]=
    { ProcessNestedFvBeforePublishing NULL };
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ProcessFvBeforePublishingHook
//
// Description: Porting hook to support OEM-specific FV validation control flow
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN OUT ROM_AREA *Area - pointer to ROM Area, which contains FV
//                                      to be processed
//              IN UINT32 FvType - FV type
//
// Output:      TRUE if validation is passed, FALSE otherwise
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ProcessNestedFvBeforePublishingHook(
    IN      EFI_PEI_SERVICES            **PeiServices, 
    IN OUT  ROM_AREA                    *Area,
    OUT     EFI_FIRMWARE_VOLUME_HEADER  **Fv,
    OUT     EFI_FIRMWARE_VOLUME_HEADER  **NewFv,
    OUT     EFI_FFS_FILE_HEADER         **Nfv)
{
    UINTN i;
    EFI_STATUS  Status = EFI_NOT_FOUND;

    for(i = 0; ProcessNestedFvBeforePublishingList[i] && EFI_ERROR(Status); i++)
        Status = ProcessNestedFvBeforePublishingList[i](PeiServices, Area, Fv, NewFv, Nfv);

    return Status;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ProcessFvBeforePublishingHook
//
// Description: Porting hook to support OEM-specific FV validation control flow
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN OUT ROM_AREA *Area - pointer to ROM Area, which contains FV
//                                      to be processed
//              IN UINT32 FvType - FV type
//
// Output:      TRUE if validation is passed, FALSE otherwise
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN ProcessFvBeforePublishingHook(
    IN EFI_PEI_SERVICES **PeiServices, 
    IN OUT ROM_AREA *Area, 
    IN UINT32 FvType)
{
    UINTN i;
    BOOLEAN Result = TRUE;
    for(i=0; ProcessFvBeforePublishingList[i] && Result; i++) 
        Result = ProcessFvBeforePublishingList[i](PeiServices, Area, FvType);
    return Result;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ProcessDxeFvInDxeIplHook
//
// Description: Porting hook to support OEM-specific FV validation control flow
//              before passing control to DXE
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN OUT ROM_AREA *Area - pointer to ROM Area, which contains FV
//                                      to be processed
//
// Output:      TRUE if validation is passed, FALSE otherwise
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOLEAN ProcessDxeFvInDxeIplHook(
    IN EFI_PEI_SERVICES **PeiServices, 
    IN OUT ROM_AREA *Area)
{
    UINTN i;
    BOOLEAN Result = TRUE;
    for(i=0; ProcessDxeFvInDxeIplList[i] && Result; i++) 
        Result = ProcessDxeFvInDxeIplList[i](PeiServices, Area);
    return Result;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   EfiReadSection
//
// Description: Function to read section of specific type from given FFS file
//
// Input:       IN EFI_PEI_SERVICES **ppPS - pointer to PEI services
//              IN EFI_SECTION_TYPE SectionType - type of section to read
//              IN EFI_FFS_FILE_HEADER *pFile - pointer to FFS file to read from
//              OUT VOID **ppData - pointer for output buffer
//
// Output:      EFI_SUCCESS if reading completed successfully
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS EfiReadSection(
    IN EFI_PEI_SERVICES **ppPS, 
    IN EFI_SECTION_TYPE SectionType,
    IN EFI_FFS_FILE_HEADER *pFile, 
    OUT VOID **ppData)
{
    EFI_STATUS Status;
    UINT32 SrcSize, DstSize, TmpSize;
    VOID *pSrc;
    EFI_PHYSICAL_ADDRESS DstAddress;
    EFI_HOB_HANDOFF_INFO_TABLE *pHob;
    EFI_COMPRESSION_SECTION_HEADER *pCs;

    if (!ppData) 
        return EFI_INVALID_PARAMETER;

    //TODO: Support for GUIDed sections
    Status = (*ppPS)->FfsFindSectionData(ppPS, SectionType, pFile, &pSrc);
    if (!EFI_ERROR(Status)) {
        *ppData = pSrc; 
        return Status; 
    }

    Status = (*ppPS)->FfsFindSectionData(ppPS, EFI_SECTION_COMPRESSION, pFile, &pCs);
    if (EFI_ERROR(Status)) 
        return Status;

    SrcSize = FVSECTION_SIZE(pCs) - sizeof(EFI_COMPRESSION_SECTION_HEADER);
    pSrc = pCs + 1;
    if (pCs->CompressionType != EFI_NOT_COMPRESSED) {
        GET_INFO GetInfoPtr;
        DECOMPRESS DecompressPtr;
        BOOLEAN KnownCompressionType;

        KnownCompressionType = GetDecompressInterface(pCs->CompressionType, &GetInfoPtr, &DecompressPtr);
        if (!KnownCompressionType) 
            return EFI_UNSUPPORTED;

        Status = GetInfoPtr(pSrc, SrcSize, &DstSize, &TmpSize);
        if (EFI_ERROR(Status)) 
            return Status;

        //No FreePool, so the memory is never freed
        Status = (*ppPS)->AllocatePages(ppPS, EfiBootServicesCode,( DstSize >> 12) + 1, &DstAddress);
        if (EFI_ERROR(Status)) 
            return Status;

        (*ppPS)->GetHobList(ppPS,&pHob);
        if (pHob->EfiFreeMemoryTop - pHob->EfiFreeMemoryBottom <  TmpSize)
            return EFI_OUT_OF_RESOURCES;

        Status = DecompressPtr(pSrc, SrcSize, (VOID*)DstAddress, DstSize, (VOID*)pHob->EfiFreeMemoryBottom, TmpSize);
        if (EFI_ERROR(Status)) 
            return Status;

        pSrc = (VOID*)(UINTN)DstAddress;
    }

    do {
        if (((EFI_COMMON_SECTION_HEADER*)pSrc)->Type == SectionType) {
            *ppData = (EFI_COMMON_SECTION_HEADER*)pSrc+1;
            return EFI_SUCCESS;
        }

        TmpSize = FVSECTION_SIZE(pSrc);
        TmpSize += (4 - (TmpSize & 3)) & 3;
        if (TmpSize>=DstSize) 
            return EFI_NOT_FOUND;

        DstSize -= TmpSize;
        pSrc =  (UINT8*)pSrc + TmpSize;
    }while(TRUE);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetRomLayout
//
// Description: Function to read ROM_LAYOUT data from FFS file
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              OUT ROM_AREA **Layout - pointer to output buffer
//
// Output:      EFI_SUCCESS if ROM layout is retrieved
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS GetRomLayout(
    IN  EFI_PEI_SERVICES **PeiServices,
    OUT ROM_AREA         **Layout
)
{
    EFI_STATUS Status;
    EFI_FIRMWARE_VOLUME_HEADER *BootFv;
    EFI_FFS_FILE_HEADER *File = NULL;
    ROM_LAYOUT_SECTION *Section;

    Status = (*PeiServices)->FfsFindNextVolume(PeiServices, 0, &BootFv);
    if (EFI_ERROR(Status)) 
        return Status;

    do {
        Status = (*PeiServices)->FfsFindNextFile(PeiServices, EFI_FV_FILETYPE_FREEFORM, BootFv, &File);
        if (!EFI_ERROR(Status) && !guidcmp(&(File->Name), &RomLayoutFfsGuid)) {
            break;
        }
    } while(Status == EFI_SUCCESS);

    if (EFI_ERROR(Status)) 
        return Status;

    Section = (ROM_LAYOUT_SECTION *)File;

    *Layout = Section->Layout;
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetRomLayout
//
// Description: Function to read ROM_LAYOUT data from given data buffer
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              OUT ROM_AREA **Layout - pointer to output buffer
//              IN  VOID *RecoveryCapsule - pointer to data buffer to read from
//
// Output:      EFI_SUCCESS if ROM layout is retrieved
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS GetRecoveryRomLayout(
    IN  EFI_PEI_SERVICES **PeiServices,
    OUT ROM_AREA         **Layout,
    IN  VOID             *RecoveryCapsule
)
{
    UINT32 Signature;
    UINT32 *SearchPointer;
    ROM_LAYOUT_SECTION *Section;

    SearchPointer = (UINT32 *)((UINT8 *)RecoveryCapsule - sizeof(EFI_GUID) + FLASH_SIZE);
    Signature = RomLayoutFfsGuid.Data1;

    do {
        if(*SearchPointer == Signature) {
            Section = (ROM_LAYOUT_SECTION *)SearchPointer;
            if(!guidcmp(&RomLayoutFfsGuid, &(Section->FfsGuid)) &&
               !guidcmp(&RomLayoutSectionGuid, &(Section->SectionGuid))) {
                *Layout = Section->Layout;
                return EFI_SUCCESS;
            }
        }
    } while(SearchPointer-- != RecoveryCapsule);

    return EFI_NOT_FOUND;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   VerifyFwVolume
//
// Description: Function to verify that FV is not corrupted
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN EFI_FIRMWARE_VOLUME_HEADER *Fv - pointer to FV to verify
//
// Output:      EFI_SUCCESS if verification passed
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS VerifyFwVolume(
    IN EFI_PEI_SERVICES **PeiServices,
    IN EFI_FIRMWARE_VOLUME_HEADER *Fv
)
{
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   CreateFvHob
//
// Description: Function to create FV hob for given ROM layout unit
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN ROM_AREA *Area - pointer to ROM layout unit to process
//
// Output:      EFI_SUCCESS if Hob created successfully
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CreateFvHob(
    IN EFI_PEI_SERVICES **PeiServices,
    IN ROM_AREA         *Area
)
{
    EFI_STATUS Status;
    EFI_HOB_FIRMWARE_VOLUME *FVHob;

    Status = (*PeiServices)->CreateHob(
                                PeiServices, 
                                EFI_HOB_TYPE_FV,
                                sizeof(EFI_HOB_FIRMWARE_VOLUME),
                                &FVHob);
    if (EFI_ERROR(Status)) 
        return Status;

    FVHob->BaseAddress = Area->Address;
    FVHob->Length = (UINT64)Area->Size;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   CreateFvHob2
//
// Description: Function to create FV hob type 2 for given ROM layout unit
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN ROM_AREA *Area - pointer to ROM layout unit to process
//              IN EFI_GUID *FvName - pointer to FV name
//              IN EFI_GUID *FfsName - pointer to parent FFS file
//
// Output:      EFI_SUCCESS if Hob created successfully
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CreateFvHob2(
    IN EFI_PEI_SERVICES **PeiServices,
    IN ROM_AREA         *Area,
    IN EFI_GUID         *FvName,
    IN EFI_GUID         *FfsName
)
{
    EFI_STATUS Status;
    EFI_HOB_FIRMWARE_VOLUME2 *FVHob;

    Status = (*PeiServices)->CreateHob(
                                PeiServices, 
                                EFI_HOB_TYPE_FV2,
                                sizeof(EFI_HOB_FIRMWARE_VOLUME2),
                                &FVHob);
    if (EFI_ERROR(Status)) 
        return Status;

    FVHob->BaseAddress = Area->Address;
    FVHob->Length = (UINT64)Area->Size;
    FVHob->FvName = *FvName;
    FVHob->FileName = *FfsName;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   CheckBeforePublishing
//
// Description: Function to verify if FvPPI should be installed.
//              On recovery only PPI for FV with DXE_CORE FFS should be installed 
//              from recovery capsule
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN EFI_FIRMWARE_VOLUME_HEADER *Fv - pointer to FV to check
//
// Output:      EFI_SUCCESS if FV should be published, EFI_ERROR otherwise
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CheckBeforePublishing(
    IN EFI_PEI_SERVICES           **PeiServices,
    IN EFI_FIRMWARE_VOLUME_HEADER *Fv
)
{
    EFI_STATUS Status;
    RECOVERY_IMAGE_HOB *RecoveryHob;
    EFI_FFS_FILE_HEADER *File = NULL;
    static EFI_GUID RecoveryHobGuid = AMI_RECOVERY_IMAGE_HOB_GUID;

//check if we are on recovery boot path, with recovery capsule already loaded

    Status = (*PeiServices)->GetHobList(PeiServices, &RecoveryHob);
    if(EFI_ERROR(Status))
        return EFI_SUCCESS;     //we are not on recovery boot path

    Status = FindNextHobByGuid(&RecoveryHobGuid, &RecoveryHob);
    if(EFI_ERROR(Status))
        return EFI_SUCCESS;     //we are not on recovery boot path

    if(RecoveryHob->Status == EFI_SUCCESS && RecoveryHob->Address != NULL) { //we're on recovery boot path
        Status = (*PeiServices)->FfsFindNextFile(PeiServices, EFI_FV_FILETYPE_DXE_CORE, Fv, &File);
        return Status;
    }

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   CreateFvInfoPpi
//
// Description: Function to install FwVolume info PPI for given ROM layout unit
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN ROM_AREA *Area - pointer to ROM layout unit to process
//              IN EFI_GUID *FvName - pointer to parent FV name
//              IN EFI_GUID *FfsName - pointer to parent FFS file
//
// Output:      EFI_SUCCESS if PPI installed
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CreateFvInfoPpi(
    IN EFI_PEI_SERVICES **PeiServices,
    IN ROM_AREA         *Area,
    IN EFI_GUID         *FvName OPTIONAL,
    IN EFI_GUID         *FfsName OPTIONAL
)
{
    EFI_STATUS Status;

    EFI_PEI_PPI_DESCRIPTOR *FirmwareVolume;
    EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *FirmwareVolumeInfo;

    Status = CheckBeforePublishing(PeiServices, (EFI_FIRMWARE_VOLUME_HEADER*)Area->Address);
    if(EFI_ERROR(Status))
        return EFI_VOLUME_FULL;

    Status = (*PeiServices)->AllocatePool(
                                PeiServices,
                                sizeof(EFI_PEI_PPI_DESCRIPTOR) + sizeof(EFI_PEI_FIRMWARE_VOLUME_INFO_PPI),
                                &FirmwareVolume);
    if(EFI_ERROR(Status))
        return EFI_VOLUME_FULL;

    FirmwareVolumeInfo = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI*)(FirmwareVolume + 1);

    FirmwareVolumeInfo->FvFormat = ((EFI_FIRMWARE_VOLUME_HEADER*)Area->Address)->FileSystemGuid;
    FirmwareVolumeInfo->FvInfo = (VOID *)(UINTN)Area->Address;
    FirmwareVolumeInfo->FvInfoSize = Area->Size;
    FirmwareVolumeInfo->ParentFvName = FvName;
    FirmwareVolumeInfo->ParentFileName = FfsName;


    FirmwareVolume->Ppi =   FirmwareVolumeInfo;
    FirmwareVolume->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
    FirmwareVolume->Guid =  &gEfiFirmwareVolumeInfoPpiGuid;
  
    return (*PeiServices)->InstallPpi(PeiServices, FirmwareVolume);
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   FindFvHob
//
// Description: Function to find FV Hob for corresponding ROM layout unit
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN ROM_AREA *Area - pointer to ROM layout unit to find matching Hob
//              OUT EFI_HOB_FIRMWARE_VOLUME **FvHob - pointer to output buffer
//
// Output:      EFI_SUCCESS if corresponding Hob found
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS FindFvHob(
    IN  EFI_PEI_SERVICES **PeiServices, 
    IN  ROM_AREA *Area,
    OUT EFI_HOB_FIRMWARE_VOLUME **FvHob
)
{
    EFI_HOB_FIRMWARE_VOLUME *FvHob2;

    (*PeiServices)->GetHobList(PeiServices, FvHob);

    FvHob2 = *FvHob;
    while(!EFI_ERROR(FindNextHobByType(EFI_HOB_TYPE_FV, FvHob))){
        if ((*FvHob)->BaseAddress == Area->Address) 
            return EFI_SUCCESS;
    }

    while(!EFI_ERROR(FindNextHobByType(EFI_HOB_TYPE_FV2, &FvHob2))){
        if (FvHob2->BaseAddress == Area->Address) {
            *FvHob = FvHob2;
            return EFI_SUCCESS;
        }
    }
    return EFI_NOT_FOUND;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   RemoveFvHob
//
// Description: Function to remove FV Hob for corresponding ROM layout unit
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN ROM_AREA *Area - pointer to ROM layout unit to remove matching Hob
//                                  or NULL to remove all FV Hobs
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID RemoveFvHob(
    IN EFI_PEI_SERVICES **PeiServices,
    IN ROM_AREA         *Area OPTIONAL
)
{
    EFI_STATUS Status;
    EFI_HOB_FIRMWARE_VOLUME *FvHob;
    EFI_HOB_FIRMWARE_VOLUME2 *FvHob2;

    if(Area == NULL) { //delete all Fv hobs
        (*PeiServices)->GetHobList(PeiServices, &FvHob);
        FvHob2 = (EFI_HOB_FIRMWARE_VOLUME2 *)FvHob;
        while(!EFI_ERROR(FindNextHobByType(EFI_HOB_TYPE_FV, &FvHob))) {
            FvHob->Header.HobType = EFI_HOB_TYPE_UNUSED;
        }

        while(!EFI_ERROR(FindNextHobByType(EFI_HOB_TYPE_FV2, &FvHob2))) {
            FvHob2->Header.HobType = EFI_HOB_TYPE_UNUSED;
        }
    } else {
        Status = FindFvHob(PeiServices, Area, &FvHob);
        if(!EFI_ERROR(Status))
            FvHob->Header.HobType = EFI_HOB_TYPE_UNUSED;
    }
    return;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   PublishFv
//
// Description: Function to publish Firmware Volume for corresponding ROM layout unit
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN ROM_AREA *Area - pointer to ROM layout unit describing FV
//              IN BOOLEAN Nested - TRUE if published FV is nested, FALSE otherwise
//
// Output:      EFI_SUCCESS if FV published successfully
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS PublishFv(
    IN EFI_PEI_SERVICES **PeiServices,
    IN ROM_AREA         *Area,
    IN BOOLEAN          Nested
)
{
    EFI_FIRMWARE_VOLUME_HEADER *Fv;
    EFI_STATUS Status = EFI_SUCCESS;

    if(!ProcessFvBeforePublishingHook(PeiServices, Area, Area->Attributes))
        return EFI_VOLUME_CORRUPTED;

    Fv = (EFI_FIRMWARE_VOLUME_HEADER*)Area->Address;
    if(Fv->Signature != FV_SIGNATURE)
        return EFI_VOLUME_CORRUPTED;

    if(Area->Attributes & ROM_AREA_FV_VERIFY)
        Status = VerifyFwVolume(PeiServices, Fv);
    
    if(EFI_ERROR(Status) || Nested) //for nested FV PublishNestedFv will call CreateFvInfoPpi and CreateHob
        return Status;

    Status = CreateFvHob(PeiServices, Area);
    if(EFI_ERROR(Status))
        return Status;

#if PI_SPECIFICATION_VERSION >= 0x00010000
    Status = CreateFvInfoPpi(PeiServices, Area, NULL, NULL);
#endif

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InitRecovery
//
// Description: Function to initialize recovery process
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//
// Output:      None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID InitRecovery(
    IN EFI_PEI_SERVICES **PeiServices
)
{
    static EFI_GUID RecoveryBootModeGuid = EFI_PEI_BOOT_IN_RECOVERY_MODE_PEIM_PPI;
    static EFI_PEI_PPI_DESCRIPTOR RecoveryModePpi = {
        EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
        &RecoveryBootModeGuid, NULL
    };

    EFI_BOOT_MODE BootMode;
    EFI_STATUS Status;

    Status = (*PeiServices)->GetBootMode(PeiServices, &BootMode);

    if(EFI_ERROR(Status) || (BootMode != BOOT_IN_RECOVERY_MODE)) {
        (*PeiServices)->SetBootMode(PeiServices, BOOT_IN_RECOVERY_MODE);
        (*PeiServices)->InstallPpi(PeiServices, &RecoveryModePpi);
        PEI_PROGRESS_CODE(PeiServices, PEI_RECOVERY_AUTO);
    }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ReportFV2Pei
//
// Description: Function to publish PEI Firmware Volumes early in PEI phase
//
// Input:       IN EFI_FFS_FILE_HEADER *FfsHeader - pointer to image FFS file
//              IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//
// Output:      EFI_SUCCESS if all FVs published successfully
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ReportFV2Pei(
    IN EFI_FFS_FILE_HEADER *FfsHeader,
    IN EFI_PEI_SERVICES **PeiServices
)
{
    ROM_AREA *Area;
    EFI_STATUS Status;
    ROM_AREA Current;

    Status = GetRomLayout(PeiServices, &Area);

    while(Area->Size != 0) {
        Current = *Area;
        if(Area->Attributes & ROM_AREA_FV_PEI) {
            Status = PublishFv(PeiServices, &Current, FALSE);
            if(EFI_ERROR(Status) && !(Area->Attributes & ROM_AREA_FV_NON_CRITICAL)) {
                InitRecovery(PeiServices);
            }
        }
        Area++;
    }
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   RelocateFv
//
// Description: Function to relocate Firmware Volume into memory for corresponding 
//              ROM layout unit
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN ROM_AREA *Area - pointer to ROM layout unit describing FV
//
// Output:      EFI_SUCCESS if FV relocated successfully
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS RelocateFv(
    IN EFI_PEI_SERVICES **PeiServices,
    IN ROM_AREA         *Area
)
{
    EFI_STATUS Status;
    EFI_PHYSICAL_ADDRESS Dest;
    UINTN Pages;

    Pages = EFI_SIZE_TO_PAGES(Area->Size);
    Status = (*PeiServices)->AllocatePages(PeiServices, EfiBootServicesData, Pages, &Dest);
    if(EFI_ERROR(Status))
        return Status;

    MemCpy((VOID *)(UINTN)Dest, (VOID *)(UINTN)Area->Address, Area->Size);
    Area->Address = Dest;
    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   PublishNestedFv
//
// Description: Function to publish Nested Firmware Volume for corresponding ROM layout unit
//
// Input:       IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//              IN ROM_AREA *Area - pointer to ROM layout unit describing FV
//
// Output:      EFI_SUCCESS if FV published successfully
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS PublishNestedFv(
    IN EFI_PEI_SERVICES **PeiServices,
    IN ROM_AREA         *Area
)
{
    EFI_FIRMWARE_VOLUME_HEADER *Fv;
    EFI_FIRMWARE_VOLUME_HEADER *NewFv;
    EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtFv;
    EFI_FFS_FILE_HEADER *Nfv = NULL;
    ROM_AREA NewArea;
    EFI_STATUS Status;

    Fv = (EFI_FIRMWARE_VOLUME_HEADER*)Area->Address;
    if(Fv->Signature != FV_SIGNATURE)
        return EFI_VOLUME_CORRUPTED;

    Status = ProcessNestedFvBeforePublishingHook(PeiServices, Area, &Fv, &NewFv, &Nfv);
    if(EFI_ERROR(Status)) {
        Status = (*PeiServices)->FfsFindNextFile (PeiServices, EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, Fv, &Nfv);
        if(EFI_ERROR(Status))
            return Status;

        Status = EfiReadSection(PeiServices, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, Nfv, &NewFv);
        if(EFI_ERROR(Status))
            return Status;
    }

    NewArea.Address = (EFI_PHYSICAL_ADDRESS)(UINTN)NewFv;
    NewArea.Size = (UINT32)NewFv->FvLength;
    NewArea.Type = RomAreaTypeFv;
    NewArea.Attributes = Area->Attributes;

    Status = PublishFv(PeiServices, &NewArea, TRUE);
    if(EFI_ERROR(Status))
        return Status;

    if(NewFv->ExtHeaderOffset == 0) { //no FvName Guid
        Status = CreateFvHob2(PeiServices, &NewArea, &NullGuid, &(Nfv->Name));
    } else {
        ExtFv = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)((UINT8 *)NewFv + NewFv->ExtHeaderOffset);
        Status = CreateFvHob2(PeiServices, &NewArea, &(ExtFv->FvName), &(Nfv->Name));
    }
    if(EFI_ERROR(Status))
        return Status;

#if PI_SPECIFICATION_VERSION >= 0x00010000
    if(Fv->ExtHeaderOffset == 0) { //no FvName Guid
        Status = CreateFvInfoPpi(PeiServices, &NewArea, NULL, &(Nfv->Name));
    } else {
        ExtFv = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)((UINT8 *)Fv + Fv->ExtHeaderOffset);
        Status = CreateFvInfoPpi(PeiServices, &NewArea, &(ExtFv->FvName), &(Nfv->Name));
    }
#endif

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ReportFV2PeiAfterMem
//
// Description: Function to publish PEI Firmware Volumes in PEI phase after
//              memory is available
//
// Input:       IN EFI_FFS_FILE_HEADER *FfsHeader - pointer to image FFS file
//              IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//
// Output:      EFI_SUCCESS if all FVs published successfully
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ReportFV2PeiAfterMem(
    IN EFI_FFS_FILE_HEADER *FfsHeader,
    IN EFI_PEI_SERVICES **PeiServices
)
{
    ROM_AREA *Area;
    EFI_STATUS Status;
    EFI_BOOT_MODE BootMode;
    BOOLEAN CorruptedVolume;
    ROM_AREA Current;

    Status = (*PeiServices)->GetBootMode(PeiServices, &BootMode);
    Status = GetRomLayout(PeiServices, &Area);

    while(Area->Size != 0) {
        Current = *Area;
        CorruptedVolume = FALSE;
        if(Area->Attributes & ROM_AREA_FV_PEI_MEM) {
            Status = PublishFv(PeiServices, &Current, FALSE);
            if(EFI_ERROR(Status) && !(Area->Attributes & ROM_AREA_FV_NON_CRITICAL)) {
                InitRecovery(PeiServices);
                CorruptedVolume = TRUE;
            }
        } else if(Area->Attributes & ROM_AREA_FV_PEI_SHADOW && BootMode != BOOT_ON_S3_RESUME) {
            Status = RelocateFv(PeiServices, &Current);
            if(!EFI_ERROR(Status))
                Status = PublishFv(PeiServices, &Current, FALSE);

            if(EFI_ERROR(Status) && !(Area->Attributes & ROM_AREA_FV_NON_CRITICAL)) {
                InitRecovery(PeiServices);
                CorruptedVolume = TRUE;
            }
        }
/* (EIP71398)>
        if(!CorruptedVolume &&
           Area->Attributes & ROM_AREA_FV_NFV_PRESENT && 
           Area->Attributes & ROM_AREA_FV_PEI_ACCESS && 
           BootMode == BOOT_IN_RECOVERY_MODE) {
            Status = PublishNestedFv(PeiServices, &Current);
            if(EFI_ERROR(Status) && !(Area->Attributes & ROM_AREA_FV_NON_CRITICAL)) {
                InitRecovery(PeiServices);
            }
        }
<(EIP71398) */ 
        Area++;
    }
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ReportFV2Dxe
//
// Description: Function to publish PEI Firmware Volumes in PEI phase right before
//              passing control to DXE core
//
// Input:       IN VOID* RecoveryCapsule - pointer to recovery capsule if in Recovery mode
//              IN EFI_PEI_SERVICES **PeiServices - pointer to PEI services
//
// Output:      EFI_SUCCESS if all FVs published successfully
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS ReportFV2Dxe(
    IN VOID* RecoveryCapsule OPTIONAL,
    IN EFI_PEI_SERVICES **PeiServices
)
{
    ROM_AREA *Area;
    EFI_STATUS Status;
    ROM_AREA Current;

    static EFI_GUID AmiRomLayoutHobGuid = AMI_ROM_LAYOUT_HOB_GUID;
    ROM_LAYOUT_HOB *RomLayoutHob;
    UINTN RomLayoutSize;
    UINT8 *RomLayoutStart;

    if(RecoveryCapsule == NULL) {
        Status = GetRomLayout(PeiServices, &Area);
    } else {
        Status = GetRecoveryRomLayout(PeiServices, &Area, RecoveryCapsule);
         if(EFI_ERROR(Status))
            return Status;              //recovery will fail and we will boot from existing ROM
        RemoveFvHob(PeiServices, NULL);
    }

    RomLayoutStart = (UINT8*)Area;

    while(Area->Size != 0) {
        Current = *Area;
        if(RecoveryCapsule != NULL)
            Current.Address = Area->Offset + (UINTN)RecoveryCapsule;
            
        if(Area->Attributes & ROM_AREA_FV_DXE) {
            if((Area->Attributes & ROM_AREA_FV_PEI_ACCESS) && RecoveryCapsule == NULL) {
                if(Area->Attributes & ROM_AREA_FV_NFV_PRESENT) {
                    Status = PublishNestedFv(PeiServices, &Current);
                }
                Area++;
                continue;           //all PEI-related HOBs were processed earlier
            }

            Status = PublishFv(PeiServices, &Current, FALSE);
            
            if(Area->Attributes & ROM_AREA_FV_NFV_PRESENT) {
                Status = PublishNestedFv(PeiServices, &Current);
            }

        } else {
            if((Area->Attributes & ROM_AREA_FV_PEI_ACCESS) && RecoveryCapsule == NULL) {
                RemoveFvHob(PeiServices, Area);
            }
        }
        Area++;
    }

    // Create a HOB for the entire ROM layout defined in the RomLayout Variable
    RomLayoutSize = (UINT8*)(Area + 1) - RomLayoutStart;
    Status = (*PeiServices)->CreateHob(PeiServices, 
                                       EFI_HOB_TYPE_GUID_EXTENSION,
                                       sizeof(ROM_LAYOUT_HOB) + RomLayoutSize,
                                       &RomLayoutHob);
    if (!EFI_ERROR(Status)) {
        RomLayoutHob->Header.Name = AmiRomLayoutHobGuid;
        MemCpy(RomLayoutHob + 1, RomLayoutStart, RomLayoutSize);
    }

    return EFI_SUCCESS;    
}    


//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************