summaryrefslogtreecommitdiff
path: root/Chipset/eM/ME/MEUD/CSP_MEUD.c
blob: 2036deefd327f47627666e9f43e9af9be82f2336 (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
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2009, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************
//*************************************************************************
// $Header: /Alaska/SOURCE/Modules/OFBD Intel ME Update/CSP_MEUD/ME80/CSP_MEUD.c 14    5/14/15 4:33a Tristinchou $
//
// $Revision: 14 $
//
// $Date: 5/14/15 4:33a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/OFBD Intel ME Update/CSP_MEUD/ME80/CSP_MEUD.c $
// 
// 14    5/14/15 4:33a Tristinchou
// [TAG]  		EIP215437
// [Category]  	New Feature
// [Description]  	ME capsule update support on SharkBay
// 
// 13    3/05/14 2:51a Tristinchou
// [TAG]  		EIP147099
// [Category]  	New Feature
// [Description]  	Support ME FWUpdate API
// 
// 12    10/01/13 1:40a Klzhan
// [TAG]  		EIPNone
// [Category]  	Improvement
// [Description]  	backward compatible
// 
// 11    9/30/13 6:05a Klzhan
// [TAG]  		EIPNone
// [Category]  	Improvement
// [Description]  	Support PI 1.2
// 
// 10    8/23/13 4:14a Tristinchou
// [TAG]  		EIPNone
// [Category]  	Improvement
// [Description]  	Fix build error with CrescentBay project.
// 
// 9     5/16/13 6:24a Klzhan
// [TAG]  		EIPNone
// [Category]  	Improvement
// [Description]  	1. Pre-allocate Memory while ME is disable(Avoid
// allocate memory error message).
// 2. Support PDR reiong is on top of SPI(AFU 3.05 and
// 4.6.3_Flash_Combined_2_36)
// 
// 8     3/15/13 5:18a Klzhan
// [TAG]  		EIPNone
// [Category]  	Improvement
// [Description]  	Add new token BITS_OF_SPI_DENSITY
// [Files]  		CSP_MEUD.c
// CSP_MEUD.h
// CSP_MEUD.sdl
// CSP_MEUD.mak
// CSP_MEUD.cif
// 
// 7     11/29/12 4:42a Klzhan
// BugFix : Can't update ME when 2 SPI installed. A token is added
// (Lynx Point spec updated, BIT define chaged).
// 
// 6     11/29/12 2:24a Klzhan
// BugFix : Mac address restored to wrong address.
// 
// 5     11/28/12 4:03a Klzhan
// Getting wrong Flash capacity when 2 flash component.
// 
// 4     9/07/12 4:46a Klzhan
// Support ME 9.
// 
// 1     12/12/11 2:38a Wesleychen
// Update to rev.2 for FD region length correction.
// 
// 2     5/09/11 3:25a Klzhan
// Correct FD region length.
// 
// 1     4/22/11 2:47a Klzhan
// Initial check - in
// 
//  
//
//**********************************************************************
//<AMI_FHDR_START>
//
// Name:	CSP_MEUD.c
//
// Description:
//
//<AMI_FHDR_END>
//**********************************************************************
#include "Efi.h"
#include "token.h"
#include <AmiLib.h>
#include <AmiDxeLib.h>
#include <SB.h>
#include "CoreBiosMsg.h"
#include "ReferenceCode\ME\Protocol\Heci\Heci.h"
#include "MeChipset.h"
#include "PchRegs\PchRegsRcrb.h"
#include "CSP_MEUD.h"
#include <MEUD\MEUD.h>
#include "Flash.h"
#include "MEFwUpdLcl\MeFwUpdLclProtocol.h"

#if defined SecSMIFlash_SUPPORT && SecSMIFlash_SUPPORT == 1
#include <Protocol\SecSmiFlash.h>
#endif

#if PI_SPECIFICATION_VERSION >= 0x1000A
#include <Protocol\SmmBase2.h>
#define RETURN(status) {return status;}

extern EFI_GUID gEfiSmmBase2ProtocolGuid;
EFI_SMM_BASE2_PROTOCOL          *gSmmBase2;
EFI_SMM_SYSTEM_TABLE2	        *gSmst;
#endif
#if defined SecSMIFlash_SUPPORT && SecSMIFlash_SUPPORT == 1
static EFI_GUID	gEfiSecSmiFlashProtocolGuid = SEC_SMI_FLASH_GUID;
UINT32 *gFwCapsuleAddress = NULL;
#endif

EFI_GUID gEfiHeciProtocolGuid       = HECI_PROTOCOL_GUID;

BOOLEAN	IsIgnition;
BOOLEAN	Is_SECOVR_JMPR;
BOOLEAN	Is_MEFW;

EFI_PHYSICAL_ADDRESS   Phy_Address;
OFBD_TC_55_ME_PROCESS_STRUCT  *StructPtr;
UINT8   MacAddr[6];
UINT8	Nounce[8];
UINT32	Factory_Base;
UINT32	Factory_Limit;
#ifdef _HECI_PROTOCOL_H
HECI_PROTOCOL     *mHeci = NULL;
#else
EFI_HECI_PROTOCOL *mHeci = NULL;
#endif
EFI_PHYSICAL_ADDRESS RomBuffer = NULL;
EFI_PHYSICAL_ADDRESS BlockBuffer = NULL;

EFI_GUID mMeFwUpdLclProtocolGuid = ME_FW_UPD_LOCAL_PROTOCOL_GUID;
ME_FW_UPDATE_LOCAL_PROTOCOL     *mMeFwUpdateLocalProtocol = NULL;
//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   GET_FW_VERSION
//
// Description: Get ME FW Version.
//
// Input:   NONE
//
// Output:  EFI_STATUS
//
// Returns:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS GET_FW_VERSION(
    IN UINT16 *MeFwVersionData
)
{
  EFI_STATUS        Status;
  UINT32            Length;
  GEN_GET_FW_VER    *MsgGenGetFwVersion;
  GEN_GET_FW_VER    GenGetFwVersion;
  UINT32            MeMode;
  GEN_GET_FW_VER_ACK     MsgGenGetFwVersionAck;

  Status = mHeci->GetMeMode (&MeMode);
  if (EFI_ERROR (Status) || (MeMode != ME_MODE_NORMAL)) {
    return EFI_UNSUPPORTED;
  }

  //
  // Allocate MsgGenGetFwVersion data structure
  //
  MsgGenGetFwVersion = &GenGetFwVersion;
  MsgGenGetFwVersion->MKHIHeader.Data = 0;
  MsgGenGetFwVersion->MKHIHeader.Fields.GroupId = MKHI_GEN_GROUP_ID;
  MsgGenGetFwVersion->MKHIHeader.Fields.Command = GEN_GET_FW_VERSION_CMD;
  MsgGenGetFwVersion->MKHIHeader.Fields.IsResponse = 0;
  Length = sizeof (GEN_GET_FW_VER);
  //
  // Send Get Firmware Version Request to ME
  Status = mHeci->SendMsg (
                  (UINT32 *) MsgGenGetFwVersion,
                  Length,
                  BIOS_FIXED_HOST_ADDR,
                  HECI_CORE_MESSAGE_ADDR
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Length = sizeof (GEN_GET_FW_VER_ACK);
  Status = mHeci->ReadMsg (
                  BLOCKING,
                  (UINT32 *) &MsgGenGetFwVersionAck,
                  &Length
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  MeFwVersionData[0] = MsgGenGetFwVersionAck.Data.CodeMajor;
  MeFwVersionData[1] = MsgGenGetFwVersionAck.Data.CodeMinor;
  MeFwVersionData[2] = MsgGenGetFwVersionAck.Data.CodeHotFix;
  MeFwVersionData[3] = MsgGenGetFwVersionAck.Data.CodeBuildNo;

  return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   GetHFS
//
// Description: Get Host Firmware Status pass to MEUD
//
// Input:   NONE
//
// Output:  Host Firmware Status
//
// Returns:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
UINT32 GetHFS(VOID)
{
    UINT32  R_HFS;
    EFI_STATUS  Status;
    if(mHeci == NULL)
    {
        R_HFS = 0;
        return R_HFS;
    }
    Status = mHeci->GetMeStatus(&R_HFS);
    if (EFI_ERROR (Status)) {
        R_HFS = 0;
    }
    return R_HFS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   HMRFPO_ENABLE_MSG
//
// Description: Send Enable HECI message to enable Ignition Firmwate update.
//
// Input:   NONE
//
// Output:  NONE
//
// Returns:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS HMRFPO_ENABLE_MSG(VOID)
{
    EFI_STATUS                        Status = EFI_SUCCESS;
    MKHI_HMRFPO_ENABLE                HmrfpoEnableRequest;
    MKHI_HMRFPO_ENABLE_RESPONSE       HmrfpoEnableResponse;
    UINT32                            HeciLength;

    // Function 1 and 4 will call this function, check is ME or Ignition.
    if(Is_MEFW)
    {
        // If ME FW check GPIO33 assert or not
        if(Is_SECOVR_JMPR)
            return EFI_SUCCESS;
        else
            return EFI_UNSUPPORTED;
    }
    if(mHeci == NULL)
        return EFI_NOT_READY;

    HmrfpoEnableRequest.MkhiHeader.Fields.GroupId     = MKHI_SPI_GROUP_ID;
    HmrfpoEnableRequest.MkhiHeader.Fields.Command     = HMRFPO_ENABLE_CMD_ID;
    HmrfpoEnableRequest.MkhiHeader.Fields.IsResponse  = 1;

    MemSet( &HmrfpoEnableRequest.Nonce ,8,0);

    HeciLength = sizeof (MKHI_HMRFPO_ENABLE);

    Status = mHeci->SendMsg (
                 (UINT32 *) &HmrfpoEnableRequest,
                 HeciLength,
                 BIOS_FIXED_HOST_ADDR,
                 HECI_CORE_MESSAGE_ADDR
             );
    if (EFI_ERROR (Status)) {
        IoWrite8(0x80, 0xA0);
        return Status;
    }

    HeciLength = sizeof (MKHI_HMRFPO_ENABLE_RESPONSE);

    Status = mHeci->ReadMsg (
                 BLOCKING,
                 (UINT32 *) &HmrfpoEnableResponse,
                 &HeciLength
             );
    if (EFI_ERROR (Status)) {
        IoWrite8(0x80, 0xA1);
        return Status;
    }

    return Status;

}
//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   HMRFPO_LOCK_MSG
//
// Description: Send LOCK HECI message and lock ME.
//
// Input:   NONE
//
// Output:  NONE
//
// Returns:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS HMRFPO_LOCK_MSG(VOID)
{
    EFI_STATUS                      Status;
    MKHI_HMRFPO_LOCK                HmrfpoLockRequest;
    MKHI_HMRFPO_LOCK_RESPONSE       HmrfpoLockResponse;
    UINT32                          HeciLength;
    // Function 1 and 4 will call this function, check is ME or Ignition.
    if(Is_MEFW)
    {
        // If ME FW check GPIO33 assert or not
        if(Is_SECOVR_JMPR)
            return EFI_SUCCESS;
        else
            return EFI_UNSUPPORTED;
    }
    if(mHeci == NULL)
        return EFI_NOT_READY;
    HmrfpoLockRequest.MkhiHeader.Fields.GroupId     = MKHI_SPI_GROUP_ID;
    HmrfpoLockRequest.MkhiHeader.Fields.Command     = HMRFPO_LOCK_CMD_ID;
    HmrfpoLockRequest.MkhiHeader.Fields.IsResponse  = 1;

    HeciLength = sizeof (MKHI_HMRFPO_LOCK);
    Status = mHeci->SendMsg (
                 (UINT32 *) &HmrfpoLockRequest,
                 HeciLength,
                 BIOS_FIXED_HOST_ADDR,
                 HECI_CORE_MESSAGE_ADDR
             );
    if (EFI_ERROR (Status)) {
        IoWrite8(0x80, 0xA0);
        return Status;
    }

    HeciLength = sizeof (MKHI_HMRFPO_LOCK_RESPONSE);
    Status = mHeci->ReadMsg (
                 BLOCKING,
                 (UINT32 *) &HmrfpoLockResponse,
                 &HeciLength
             );
    if (EFI_ERROR (Status)) {
        IoWrite8(0x80, 0xA1);
        return Status;
    } else {
        Factory_Base   = HmrfpoLockResponse.FactoryDefaultBase;
        Factory_Limit  = HmrfpoLockResponse.FactoryDefaultLimit;
    }

    return Status;

}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	GetRegionOffset
//
// Description:	Get GBE Region Offet of whole FlashPart
//
// Input:
//      VOID
// Output:
//      UINT32  The offset of GBE Region
//
// Returns:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
GetRegionOffset(
    UINT8    Region,
    UINT32*  Offset,
    UINT32*  Length
)
{
    volatile UINT32	*FDOC;
    volatile UINT32	*FDOD;
    UINT32	FlashDescriptorSig = 0x0FF0A55A;
    UINT32	Buffer32, RegionStart, RegionEnd;


    FDOC = (UINT32*)(pSPIBASE + 0xB0);
    FDOD = (UINT32*)(pSPIBASE + 0xB4);
    *FDOC = 0;

    if (*FDOD != FlashDescriptorSig)
        return EFI_UNSUPPORTED;

    switch(Region)
    {
        // Flash Descriptor
        case 0:
        *FDOC = 0x2000;
        break;

        // BIOS
        case 1:
        *FDOC = 0x2004;
        break;

        // ME
        case 2:
        *FDOC = 0x2008;
        break;

        // GBE
        case 3:
        *FDOC = 0x200C;
        break;

        // Platform Data
        case 4:
        *FDOC = 0x2010;
        break;

        default:
        return EFI_UNSUPPORTED;
        break;
    }
    Buffer32 = *FDOD;
    RegionEnd = Buffer32 >> 16;
    RegionStart = Buffer32 & 0xFFFF;

    *Offset = RegionStart << 12;
    *Length = (RegionEnd - RegionStart + 1) << 12;
    if((Region == 0) && (RegionEnd == 0))
    {
        *Length = 0x1000;
        return EFI_SUCCESS;
    }
    if(RegionEnd == 0)
    {
        *Length = 0;
        return EFI_NOT_FOUND;
    }

    return EFI_SUCCESS;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   GetFlashCapacity
//
// Description: Send a HECI message to lock ME.
//
// Input:   NONE
//
// Output:  FlashDensity - Real Flash Size
//
// Returns:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
UINT32 GetFlashCapacity(VOID)
{
    volatile UINT32	*FDOC;
    volatile UINT32	*FDOD;
    UINT32	FlashDescriptorSig = 0x0FF0A55A;
    UINT16	Components;
    UINT8	i,j;
    static      UINT32 FlashDensity = 0;

    if(FlashDensity)
        return FlashDensity;

    FDOC = (UINT32*)(pSPIBASE + 0xB0);
    FDOD = (UINT32*)(pSPIBASE + 0xB4);
    *FDOC = 0;

    if (*FDOD != FlashDescriptorSig)
        return 0;

    *FDOC = 0x04;
    Components = (*FDOD >> 8) & 0x03;

    *FDOC = 0x1000;
    j = *FDOD;


    for (i=0; i<(Components + 1); i++)
    {
        switch (j & 0x07)
        {
        case 0:
            FlashDensity += 0x80000;
            break;
        case 1:
            FlashDensity += 0x100000;
            break;
        case 2:
            FlashDensity += 0x200000;
            break;
        case 3:
            FlashDensity += 0x400000;
            break;
        case 4:
            FlashDensity += 0x800000;
            break;
        case 5:
            FlashDensity += 0x1000000;
            break;
        default:
            break;
        }
#if BITS_OF_SPI_DENSITY
        j = j >> 4;
#else
        j = j >> 3;
#endif
    }
    return	FlashDensity;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   CSP_ReportMEInfo
//
// Description: Report ME Base address and Length to AFU
//
// Input:   BASE_Address - address of ME region to be updated
//          Length - Length of ME region to be updated
//
// Output:
//
// Returns:
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CSP_ReportMEInfo
(
    IN UINT8 Func_Num,
    IN OUT UINT32* BASE_Address,
    IN OUT UINT32* Length
)
{
    switch (Func_Num)
    {
    case 0:

        *BASE_Address = 0;
        *Length = GetFlashCapacity() - FLASH_SIZE;
        return EFI_UNSUPPORTED;

        break;
    case 3:
        // Flash the whole SPI but BIOS region
        *BASE_Address = 0;
        *Length = GetFlashCapacity() - FLASH_SIZE;
        if (Is_SECOVR_JMPR && Is_MEFW)
            return EFI_SUCCESS;
        else
            return EFI_UNSUPPORTED;
        break;
    default:
        return EFI_UNSUPPORTED;
        break;
    }
    return EFI_SUCCESS;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	HeciCallback
//
// Description:	Locate Heci protocol callback
//
// Input:
//      IN EFI_EVENT    Event
//      IN VOID         *Context
//
// Output:
//      VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID HeciCallback(
    IN EFI_EVENT Event,
    IN VOID *Context
)
{
    EFI_STATUS Status;

    Status = pBS->LocateProtocol (
                  &gEfiHeciProtocolGuid,
                  NULL,
                  &mHeci
                  );

    if(EFI_ERROR(Status))
        mHeci = NULL;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	MeFwUpdLclCallback
//
// Description:	Locate ME Firmware update local protocol callback
//
// Input:
//      IN CONST EFI_GUID *Protocol
//      IN VOID *Interface
//      IN EFI_HANDLE Handle
//
// Output:
//      VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS MeFwUpdLclCallback (
	IN CONST EFI_GUID   *Protocol,
	IN VOID             *Interface,
	IN EFI_HANDLE       Handle
)
{
    EFI_STATUS Status;
    
    Status = gSmst->SmmLocateProtocol(
                        &mMeFwUpdLclProtocolGuid , NULL,
                        &mMeFwUpdateLocalProtocol );
    if( EFI_ERROR(Status) )
        mMeFwUpdateLocalProtocol = NULL;

    return Status;
}
#if defined SecSMIFlash_SUPPORT && SecSMIFlash_SUPPORT == 1
/**
 * Callback funciotn on Secure SmiFlash Protocol for saving FW Capsule Buffer address.
 * @param Protocol OPTIONAL 
 * @param Interface OPTIONAL
 * @param Handle OPTIONAL
 * @retval EFI_SUCCESS Secure SmiFlash Protocol installed.
 * @retval EFI_NOT_FOUND Secure SmiFlash Protocol not install yet.
**/
static
EFI_STATUS 
SecSmiFlashProtocolCallback (
  IN const EFI_GUID	    *Protocol,
  IN VOID               *Interface,
  IN EFI_HANDLE         Handle
)
{
    EFI_SEC_SMI_FLASH_PROTOCOL *SecSmiFlash = NULL;
	if (EFI_ERROR(pBS->LocateProtocol( \
            &gEfiSecSmiFlashProtocolGuid, NULL, &SecSmiFlash)))
        return EFI_NOT_FOUND;
    gFwCapsuleAddress = SecSmiFlash->pFwCapsule;
	return EFI_SUCCESS;
}
#endif
//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Procedure:   CSP_MEUDInSmm
//
// Description: Get Host Firmware Status.
//              If needed, Send LOCK if needed in SMM.
//
// Input:   NONE
//
// Output:  NONE
//
// Returns: NONE
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
VOID CSP_MEUDInSmm(VOID)
{
    UINT32	Buffer32;
    EFI_STATUS  Status;
    VOID        *MeFwUpdLclReg = NULL;

#if PI_SPECIFICATION_VERSION >= 0x1000A
    
    Status = pBS->LocateProtocol(&gEfiSmmBase2ProtocolGuid, NULL, &gSmmBase2);
    if (EFI_ERROR(Status)) return;

    Status = gSmmBase2->GetSmstLocation (gSmmBase2, &gSmst);
    if (EFI_ERROR(Status)) return;

#endif

    Buffer32 = *(volatile UINT32 *)((UINTN)PCIEX_BASE_ADDRESS + \
               (UINTN)(HECI_BUS << 20) + (UINTN)(HECI_DEV << 15) + \
               (UINTN)(HECI_FUN << 12) + (UINTN)(0x40));

    if(Buffer32 == 0xFFFFFFFF)
    {
        UINT8* pRCBA_DIS2 = (UINT8*)(SB_RCRB_BASE_ADDRESS + R_PCH_RCRB_FD2);
        // Enable HECI Device
        *pRCBA_DIS2 &= 0xFD;
        // Read Again
        pBS->Stall (1000);//1ms
        Buffer32 = *(volatile UINT32 *)((UINTN)PCIEX_BASE_ADDRESS + \
                   (UINTN)(HECI_BUS << 20) + (UINTN)(HECI_DEV << 15) + \
                   (UINTN)(HECI_FUN << 12) + (UINTN)(0x40));
        // Follow Spec ,Disable HECI Device
        *pRCBA_DIS2 |= 0x02;
    }
    // Check If Ignition FW
    // ME 8.0 is no more Ignition FW, It should alway be FALSE
    if (((Buffer32 >> 16) & 0x0F) == 1)
        IsIgnition = TRUE;
    else
        IsIgnition = FALSE;

    // Check If GPIO33 Assert
    if ( (((Buffer32 >> 16) & 0x0F) == 4) ||  (((Buffer32 >> 16) & 0x0F) == 5))
    {
        Is_SECOVR_JMPR = TRUE;
        Status = pBS->AllocatePages(AllocateAnyPages, 
                                    EfiReservedMemoryType, 
                                    EFI_SIZE_TO_PAGES (GetFlashCapacity()), 
                                    &RomBuffer);
        if(EFI_ERROR(Status))
            RomBuffer = NULL;

        Status = pBS->AllocatePages(AllocateAnyPages, 
                                    EfiReservedMemoryType, 
                                    EFI_SIZE_TO_PAGES (FLASH_BLOCK_SIZE), 
                                    &BlockBuffer);
        if(EFI_ERROR(Status))
            BlockBuffer = NULL;
    }else
        Is_SECOVR_JMPR = FALSE;

    // Check If ME FW
    if ((((Buffer32 >> 16) & 0x0F) == 0) ||
        (((Buffer32 >> 16) & 0x0F) == 3) ||
        (((Buffer32 >> 16) & 0x0F) == 4) ||
        (((Buffer32 >> 16) & 0x0F) == 5))
        Is_MEFW = TRUE;
    else
        Is_MEFW = FALSE;

    //ME Firmware update local
    Status = gSmst->SmmLocateProtocol( &mMeFwUpdLclProtocolGuid , NULL,
                                       &mMeFwUpdateLocalProtocol );
    if( EFI_ERROR(Status) )
    {
        gSmst->SmmRegisterProtocolNotify( &mMeFwUpdLclProtocolGuid,
                                          MeFwUpdLclCallback,
                                          &MeFwUpdLclReg );
    }
    
#if defined SecSMIFlash_SUPPORT && SecSMIFlash_SUPPORT == 1
    {
   		//VOID		    *Registration;
        // Create SecSmiFlash Protocol Callback to get and save the FwCapsule 
        // Address, because of the address could be cleared after calling the 
        // functions of Secure SMI Flash protocol.
        //pSmst->SmmRegisterProtocolNotify(&gEfiSecSmiFlashProtocolGuid, \
        //                            SecSmiFlashProtocolCallback, &Registration);
		SecSmiFlashProtocolCallback(NULL, NULL, NULL);					        
    }    
#endif  
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	MEProcessHandleResult
//
// Description:	Handle ME Process
//
// Input:
//      UpdateResult
//      Message
// Output:
//      VOID
//
// Returns:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
MEProcessHandleResult(
    IN UINT16   Result,
    IN CHAR8*   Message
)
{
    StructPtr->UpdateResult = Result;
    MemCpy((UINT8*)(StructPtr->ddMessageBuffer), 
                    Message, Strlen(Message));

    *(CHAR8*)(StructPtr->ddMessageBuffer + Strlen(Message)) = 0;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	HandleBuffer
//
// Description:	Init the Length and Offset need to be updated
//              If needed, send ENABLE MESSAGE
//
// Input:
//      UpdateResult
//      Message
// Output:
//      VOID
//
// Returns:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
HandleBuffer(
    IN OUT UINT32*   ProgramOffset,
    IN OUT UINT32*   ProgramLength,
    IN OUT UINT8*    Step,
    IN     BOOLEAN   InSmm
)
{
    EFI_STATUS    Status;
    UINT32        Offset, Length;
    UINT32        HFS = GetHFS();

    switch(StructPtr->bBlockType)
    {
        case FDT_BLK:
            Status = GetRegionOffset(0, &Offset, &Length);
            *Step = 0;
        break;

        case PDR_BLK:
            Status = GetRegionOffset(4, &Offset, &Length);
            if(Status == EFI_NOT_FOUND)
                return EFI_UNSUPPORTED;
            *Step = 0;
        break;

        case GBE_BLK:
            Status = GetRegionOffset(3, &Offset, &Length);
            if(Status == EFI_NOT_FOUND)
                return EFI_UNSUPPORTED;
            // Store Mac address
            if(Length)
            {
                UINT8* Address = (UINT8*)FLASH_BASE_ADDRESS(Offset);
                FlashRead(Address, MacAddr, 6);
            }
            *Step = 0;
        break;

        case ME_BLK:
            Status = GetRegionOffset(2, &Offset, &Length);
            if((HFS & BIT05) || (HFS & BIT10))
                *Step = 2;
            else
                *Step = 1;

        break;
        default:
            *Step = 0;
            return EFI_UNSUPPORTED;
        break;
    }
    *ProgramOffset = Offset;
    *ProgramLength = Length;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	UpdateRegions
//
// Description:	UpdateRegions
//
// Input:
//      Buffer
//
// Output:
//      VOID
//
// Returns:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
UpdateRegions(
    IN UINT8*    Buffer,
    IN BOOLEAN   InSmm
)
{
    static UINT32          Offset, Length;
    UINT8*                 Address;
    EFI_STATUS             Status;
    BOOLEAN                FlashStatus = TRUE, NeedToVerify = FALSE;
    static UINT8           Step = 0;
    static BOOLEAN         NewRegion;
    UINTN                  Counter = 0;
    static UINT8*          ProgramBuffer;
    UINT8*                 BufferForVerify = (UINT8*)BlockBuffer;
    UINT32                 i;
    // Prepare Offset and Length to be updated
    // If BIT02 , update buffer
    if((StructPtr->bHandleRequest & BIT02))
    {
        Status = HandleBuffer(&Offset, &Length, &Step, InSmm);
        if(EFI_ERROR(Status))
        {
            MEProcessHandleResult(BIT03, 
                      "UN SUPPORT TYPE");
            return Status;
        }
        // Frist In
        NewRegion = TRUE;
        ProgramBuffer = (UINT8*)(Phy_Address + Offset);
    }

    // Set MAC address to buffer
    if(((StructPtr->bBlockType) == GBE_BLK) && NewRegion)
        MemCpy((Buffer + Offset),MacAddr,6);

    if(NewRegion)
    {
        NewRegion = FALSE;
    }

    Address = (UINT8*)FLASH_BASE_ADDRESS(Offset);
    FlashBlockWriteEnable(Address);

    FlashEraseCommand(Address);

    // System hangs when using physical address.
    // So, verify erase complete or not.
    FlashStatus = TRUE;
    FlashReadCommand(Address, BufferForVerify, FLASH_BLOCK_SIZE);

    for(i = 0 ; (i < FLASH_BLOCK_SIZE) && (*(BufferForVerify + i) == 0xFF) ; i++);
    if(i != FLASH_BLOCK_SIZE)
        FlashStatus = FALSE;
    if(FlashStatus)
    {
        FlashProgramCommand(Address, ProgramBuffer, FLASH_BLOCK_SIZE);
        FlashReadCommand(Address, BufferForVerify, FLASH_BLOCK_SIZE);
        for(i = 0 ; (i < FLASH_BLOCK_SIZE) && (*(BufferForVerify + i) == *(BufferForVerify + i)) ; i++);
        if(i != FLASH_BLOCK_SIZE)
            FlashStatus = FALSE;
        if(FlashStatus)
            Status = EFI_SUCCESS;
        else
            Status = EFI_DEVICE_ERROR;
    }else
        Status = EFI_DEVICE_ERROR;
    FlashBlockWriteDisable(Address);
    ProgramBuffer = ProgramBuffer + FLASH_BLOCK_SIZE;
    Length -= FLASH_BLOCK_SIZE;
    Offset += FLASH_BLOCK_SIZE;

    // End of Region Update
    if(Length == 0)
    {
        NewRegion = TRUE;
    }
    // TODO :
    // OEM can output message here in every block updated.
    // Remember to Set BIT02
    else
    {
        MEProcessHandleResult((BIT01), 
                         " ");
        return EFI_SUCCESS;
    }
    // Show Strings
    if(!EFI_ERROR(Status))
    {

        switch(StructPtr->bBlockType)
        {
            case FDT_BLK:
                MEProcessHandleResult((BIT03 | BIT02), 
                          "Update success for /FDT!!");
            break;
            case PDR_BLK:
                MEProcessHandleResult((BIT03 | BIT02), 
                          "Update success for /PDR!!");
            break;
            case GBE_BLK:
                MEProcessHandleResult((BIT03 | BIT02), 
                          "Update success for /GBE!!");
            break;

            case ME_OPR_BLK:
                MEProcessHandleResult((BIT03 | BIT02), 
                          "Update success for /OPR!!");
            break;

            default:
                MEProcessHandleResult((BIT03 | BIT02), 
                          "Update success for /MER!!");
            break;
        }
    }else
    {
        switch(StructPtr->bBlockType)
        {
            case FDT_BLK:
                MEProcessHandleResult((BIT03 | BIT02), 
                          "/FDT is Locked !!");
            break;

            default:
                MEProcessHandleResult((BIT00 | BIT02), 
                          "Update Fail !!");
            break;
        }
    }

    return EFI_SUCCESS;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	MEProcessHandler
//
// Description:	Handle ME Process
//
// Input:
//      VOID
// Output:
//      OFBD_TC_55_ME_PROCESS_STRUCT
//
// Returns:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
MEProcessHandler
(
    IN OUT OFBD_TC_55_ME_PROCESS_STRUCT  **MEProcessStructPtr
)
{
    EFI_STATUS                    Status;
    static UINTN                  NumberOfPages;
    static UINT32                 SizeCopied;
    static BOOLEAN                UseSmmMem = FALSE;
    static EFI_PHYSICAL_ADDRESS   SMM_Address;
    static UINT8                  HeciIsHide = 0;
    static BOOLEAN                UseMeFwUpdLcl = FALSE;
    static UINTN                  BufferLength = 0;

    StructPtr = *MEProcessStructPtr;
    switch(StructPtr->bHandleRequest)
    {
        // Allocate Buffer
        case 1:
/*
            if ((Mmio32(SB_RCBA, 0x3428) & BIT01) != 0)
            {
                MmioRW32((SB_RCBA+0x3428), 0, BIT01);
                HeciIsHide = 1;
            }
*/
            NumberOfPages = StructPtr->TotalBlocks;
#if PI_SPECIFICATION_VERSION >= 0x1000A
            Status = gSmst->SmmAllocatePages(AllocateAnyPages, 
#else
            Status = pSmst->SmmAllocatePages(AllocateAnyPages, 
#endif
                     EfiRuntimeServicesData, NumberOfPages, &SMM_Address);
            if(!EFI_ERROR(Status))
            {
                UseSmmMem = TRUE;
                Phy_Address = SMM_Address;
            }else
                Phy_Address = RomBuffer;
            // No memory allocated
            if(!Phy_Address)
            {
                if( ( mMeFwUpdateLocalProtocol != NULL ) &&
                    ( mMeFwUpdateLocalProtocol->FwUpdBufferAddress != NULL ) )
                {
                    UseMeFwUpdLcl = TRUE;
                    Phy_Address = mMeFwUpdateLocalProtocol->FwUpdBufferAddress;
                    BufferLength = mMeFwUpdateLocalProtocol->FwUpdBufferLength;
                    SizeCopied = 0;
                    break;
                }
                MEProcessHandleResult((BIT00 | BIT02), 
                              "Error : No Memory Allocated!!");
            }
            SizeCopied = 0;
        break;

        // Recieve Data from AFU
        case 2:
            if( UseMeFwUpdLcl && 
                ( SizeCopied + StructPtr->ddMeDataSize > BufferLength ) )
            {
                MEProcessHandleResult((BIT00 | BIT02), 
                              "Error : No Memory Allocated!!");
                SizeCopied = 0;
                break;
            }
            MemCpy((UINT8*)(Phy_Address + SizeCopied),
                    (UINT8*)StructPtr->ddMeDataBuffer,StructPtr->ddMeDataSize);
            SizeCopied += StructPtr->ddMeDataSize;

        break;

        // Update
        case 4:
#if (OFBD_VERSION >= 0x0220)
            DoNotConvert = TRUE;
#endif
            UpdateRegions((UINT8*)Phy_Address, TRUE);
        break;

        // Continue....
        case 8:
            UpdateRegions((UINT8*)Phy_Address, TRUE);
        break;

        // Free Buffer
        case 0x10:
#if (OFBD_VERSION >= 0x0220)
            DoNotConvert = FALSE;
#endif
            if(UseSmmMem)
#if PI_SPECIFICATION_VERSION >= 0x1000A
                gSmst->SmmFreePages(Phy_Address, NumberOfPages);
#else
                pSmst->SmmFreePages(Phy_Address, NumberOfPages);
#endif


//            if (HeciIsHide)
//               MmioRW32((SB_RCBA+0x3428), BIT01, 0);
        break;
        
        case 0x20:
            if( UseMeFwUpdLcl )
                mMeFwUpdateLocalProtocol->FwUpdLcl( mMeFwUpdateLocalProtocol, (UINT8*)Phy_Address, SizeCopied );
        break;
        
#if defined SecSMIFlash_SUPPORT && SecSMIFlash_SUPPORT == 1
        // ME FW Capsule Update functions.
        case 3: // ME FW Capsule Update. (ME FW only) 
        case 5: // ME FW Capsule Update. (BIOS + ME FW) 
            if (!gFwCapsuleAddress) {
                MEProcessHandleResult((BIT00 | BIT02), "Error : Functon Not Supported!!");
                break;
            }    
            Phy_Address = 0;
            if ((StructPtr->bHandleRequest == 3) && \
                (StructPtr->TotalBlocks <= EFI_SIZE_TO_PAGES(FWCAPSULE_IMAGE_SIZE))) {
                // Function#3 : ME FW Update only, upload ME FW capsule from the beggining.
                Phy_Address = (EFI_PHYSICAL_ADDRESS)gFwCapsuleAddress;
            }
            if ((StructPtr->bHandleRequest == 5) && \
                (StructPtr->TotalBlocks <= \
                        EFI_SIZE_TO_PAGES(FWCAPSULE_IMAGE_SIZE - FLASH_SIZE))) {
                // Function#5 : BIOS + ME FW Update, upload capsule from the end of BIOS.
                Phy_Address = (EFI_PHYSICAL_ADDRESS)gFwCapsuleAddress + FLASH_SIZE;
            }
            if (!Phy_Address) MEProcessHandleResult((BIT00 | BIT02), "Error : Buffer Too Small!!");   
            break;
#endif  // #if SecSMIFlash_SUPPORT == 1
    }
}
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2005, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**             6145-F Northbelt Pkwy, Norcross, GA 30071            **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************