summaryrefslogtreecommitdiff
path: root/Platform/BroxtonPlatformPkg/Common/SeCPolicyInitDxe/SeCPolicyInitDxe.c
blob: 874ce7299db9747dd7f1bcae87a647d9247cd55d (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
/** @file
  SEC policy initialization.

  Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>

  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.

**/

#include "SeCPolicyInitDxe.h"
#include <Guid/PlatformInfo.h>
#include <Guid/HobList.h>
#include <Library/HeciMsgLib.h>

//
// TS DIMM thermal polling Smbus Address.
// This is platform specific.
//
UINT8 mTsDimmSmbusAddress[] = {0x30, 0x34};

DXE_SEC_POLICY_PROTOCOL  mDxePlatformSeCPolicyProtocol = { 0 };

SEC_OPERATION_PROTOCOL   mSeCOperationProtocol = {
  GetPlatformSeCInfo,
  SetPlatformSeCInfo,
  PerformSeCOperation
};

UINT8 HeciHmrfpoLockResult;
EFI_PLATFORM_INFO_HOB           *mPlatformInfo = NULL;
SYSTEM_CONFIGURATION            mSystemConfiguration;

//
// Function implementations
//
EFI_STATUS
EFIAPI
GetSecureNfcInfo (
  VOID
);


/**
  Entry point for the SeC Driver.

  @param[in]  ImageHandle           Image handle of this driver.
  @param[in]  SystemTable           Global system service table.

  @retval     EFI_SUCCESS           Initialization complete.
  @retval     EFI_UNSUPPORTED       The chipset is unsupported by this driver.
  @retval     EFI_OUT_OF_RESOURCES  Do not have enough resources to initialize the driver.
  @retval     EFI_DEVICE_ERROR      Device error, driver exits abnormally.

**/
EFI_STATUS
EFIAPI
SeCPlatformPolicyEntryPoint (
  IN EFI_HANDLE               ImageHandle,
  IN EFI_SYSTEM_TABLE         *SystemTable
)
{
  EFI_STATUS                      Status;
  EFI_EVENT                       ReadyToBootEvent;
  UINTN                           VarSize;
  SYSTEM_CONFIGURATION            SystemConfiguration;

  DEBUG ((EFI_D_INFO, "SeCPlatformPolicyEntryPoint ++ \n"));

  mDxePlatformSeCPolicyProtocol.SeCConfig.TrConfig = AllocateZeroPool (sizeof (TR_CONFIG));
  if (mDxePlatformSeCPolicyProtocol.SeCConfig.TrConfig == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  //
  // if CSE non-exists, skip it.
  //
  if (HeciPciRead16 (R_SEC_DevID_VID) == 0xFFFF) {
    DEBUG ((EFI_D_INFO, "non-secure boot, skip secure init.\n"));
    return EFI_SUCCESS;
  }

  //
  // ME DXE Policy Init
  //
  mDxePlatformSeCPolicyProtocol.Revision = DXE_PLATFORM_SEC_POLICY_PROTOCOL_REVISION_7;

  //
  // Initialzie the Me Configuration
  //
  mDxePlatformSeCPolicyProtocol.SeCConfig.EndOfPostEnabled        = 1;
  mDxePlatformSeCPolicyProtocol.SeCConfig.HeciCommunication       = 1;
  mDxePlatformSeCPolicyProtocol.SeCConfig.SeCFwDownGrade          = 0;
  mDxePlatformSeCPolicyProtocol.SeCConfig.SeCLocalFwUpdEnabled    = 0;
  mDxePlatformSeCPolicyProtocol.SeCConfig.TrConfig->DimmNumber    = 2;
  mDxePlatformSeCPolicyProtocol.SeCConfig.TrConfig->TrEnabled     = 0;
  mDxePlatformSeCPolicyProtocol.SeCConfig.SeCFwImageType          = INTEL_SEC_1_5MB_FW;
  mDxePlatformSeCPolicyProtocol.SeCConfig.PlatformBrand           = INTEL_STAND_MANAGEABILITY_BRAND;
  mDxePlatformSeCPolicyProtocol.SeCReportError                    = ShowSeCReportError;
  mDxePlatformSeCPolicyProtocol.SeCConfig.ITouchEnabled           = FALSE;

  VarSize = sizeof (SYSTEM_CONFIGURATION);
  Status = gRT->GetVariable (
                  L"Setup",
                  &gEfiNormalSetupGuid,
                  NULL,
                  &VarSize,
                  &SystemConfiguration
                  );

  if (!EFI_ERROR (Status)) {
    mDxePlatformSeCPolicyProtocol.SeCConfig.EndOfPostEnabled = SystemConfiguration.SeCEOPEnable;
    if (SystemConfiguration.VirtualKbEnable == TOUCH_INTEGRATED) {
      mDxePlatformSeCPolicyProtocol.SeCConfig.ITouchEnabled=TRUE;
    }
  }

  //
  // Install the EFI_MANAGEMENT_ENGINE_PROTOCOL interface
  //
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &ImageHandle,
                  &gDxePlatformSeCPolicyGuid,
                  &mDxePlatformSeCPolicyProtocol,
                  &gEfiSeCOperationProtocolGuid,
                  &mSeCOperationProtocol,
                  NULL
                  );

  Status = EfiCreateEventReadyToBootEx (
             TPL_CALLBACK,
             SeCPolicyReadyToBootEvent,
             (VOID *) &ImageHandle,
             &ReadyToBootEvent
             );

  ASSERT_EFI_ERROR (Status);

  DEBUG ((EFI_D_INFO, "SeCPlatformPolicyEntryPoint -- \n"));

  return Status;
}


/**
  Show Me Error message.

  @param[in]  MsgId   Me error message ID.

  @retval     None.

**/
VOID
EFIAPI
ShowSeCReportError (
  IN SEC_ERROR_MSG_ID            MsgId
  )
{
  gST->ConOut->ClearScreen (gST->ConOut);

  switch (MsgId) {
    case MSG_EOP_ERROR:
      gST->ConOut->OutputString (gST->ConOut, L"Error sending End Of Post message to ME, System HALT!\n");
      break;

    case MSG_SEC_FW_UPDATE_FAILED:
      gST->ConOut->OutputString (gST->ConOut, L"ME FW Update Failed, please try again!\n");
      break;

    case MSG_ASF_BOOT_DISK_MISSING:
      gST->ConOut->OutputString (gST->ConOut, L"Boot disk missing, please insert boot disk and press ENTER\r\n");
      break;

    case MSG_KVM_TIMES_UP:
      gST->ConOut->OutputString (gST->ConOut, L"Error!! Times up and the KVM session was cancelled!!");
      break;

    case MSG_KVM_REJECTED:
      gST->ConOut->OutputString (gST->ConOut, L"Error!! The request has rejected and the KVM session was cancelled!!");
      break;

    case MSG_HMRFPO_LOCK_FAILURE:
      gST->ConOut->OutputString (gST->ConOut, L"(A7) Me FW Downgrade - Request MeSpiLock Failed\n");
      break;

    case MSG_HMRFPO_UNLOCK_FAILURE:
      gST->ConOut->OutputString (gST->ConOut, L"(A7) Me FW Downgrade - Request MeSpiEnable Failed\n");
      break;

    case MSG_SEC_FW_UPDATE_WAIT:
      gST->ConOut->OutputString (
                     gST->ConOut,
                     L"Intel(R) Firmware Update is in progress. It may take up to 90 seconds. Please wait.\n"
                     );
      break;

    case MSG_ILLEGAL_CPU_PLUGGED_IN:
      gST->ConOut->OutputString (
                     gST->ConOut,
                     L"\n\n\rAn unsupported CPU/SC configuration has been identified.\n"
                     );

      gST->ConOut->OutputString (
                     gST->ConOut,
                     L"\rPlease refer to the Huron River Platform Validation Matrix\n\rfor supported CPU/SC combinations."
                     );

      break;

    case MSG_KVM_WAIT:
      gST->ConOut->OutputString (gST->ConOut, L"Waiting Up to 8 Minutes For KVM FW.....");
      break;

    default:
      DEBUG ((EFI_D_ERROR, "This Message Id hasn't been defined yet, MsgId = %x\n", MsgId));
      break;
  }

  gBS->Stall (HECI_MSG_DELAY);
}


VOID
HmrfpoEnable(
  VOID
  )
{
  EFI_STATUS  Status;
  UINT8       HeciHmrfpoEnableResult;

  HeciHmrfpoEnableResult  = HMRFPO_ENABLE_UNKNOWN_FAILURE;
  Status                  = HeciHmrfpoEnable (0, &HeciHmrfpoEnableResult);
  if (Status == EFI_SUCCESS && HeciHmrfpoEnableResult == HMRFPO_ENABLE_SUCCESS) {
    DEBUG ((EFI_D_ERROR, "SeC FW Downgrade !!- Step A6\n"));

    HeciSendCbmResetRequest (CBM_RR_REQ_ORIGIN_BIOS_POST, CBM_HRR_GLOBAL_RESET);
    CpuDeadLoop ();
  }
  DEBUG ((EFI_D_INFO, "SeC FW Downgrade Error !!- Step A8, the Status is %r, The result is %x\n", Status, HeciHmrfpoEnableResult));

}


UINT32
GetSeCOpMode (
  )
{
  HECI_FWS_REGISTER                   SeCFirmwareStatus;

  SeCFirmwareStatus.ul = HeciPciRead32 (R_SEC_FW_STS0);

  DEBUG ((EFI_D_INFO, "R_SEC_FW_STS0 is %08x %x\n", SeCFirmwareStatus.ul, SeCFirmwareStatus.r.SeCOperationMode));

  return SeCFirmwareStatus.r.SeCOperationMode;
}


UINT32
GetSeCHMRFPOStatus (
  )
{
  UINT8         HMRFPOStatus;

  HeciHmrfpoGetStatus (&HMRFPOStatus);
  if (HMRFPOStatus == 0) {
    return 0;
  } else if (HMRFPOStatus == 1){
    return 0;
  }
  return 1;
}


UINT32
GetSeCFwUpdateStatus (
  )
{
  UINT32    Result;

  HeciGetLocalFwUpdate (&Result);

  return Result;
}


EFI_STATUS
GetSeCFwVersion (
  SEC_VERSION_INFO     *SeCVersion
  )
{
  EFI_STATUS            Status;
  GEN_GET_FW_VER_ACK    MsgGenGetFwVersionAckData;

  Status = HeciGetFwVersionMsg (&MsgGenGetFwVersionAckData);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  SeCVersion->CodeMajor = MsgGenGetFwVersionAckData.Data.CodeMajor;
  SeCVersion->CodeMinor = MsgGenGetFwVersionAckData.Data.CodeMinor;
  SeCVersion->CodeHotFix = MsgGenGetFwVersionAckData.Data.CodeHotFix;
  SeCVersion->CodeBuildNo = MsgGenGetFwVersionAckData.Data.CodeBuildNo;

  return EFI_SUCCESS;
}


EFI_STATUS
GetSeCCapability (
  UINT32                   *SeCCapability
  )
{
  EFI_STATUS               Status;
  GEN_GET_FW_CAPSKU        MsgGenGetFwCapsSku;
  GEN_GET_FW_CAPS_SKU_ACK  MsgGenGetFwCapsSkuAck;

  Status = HeciGetFwCapsSkuMsg (&MsgGenGetFwCapsSku, &MsgGenGetFwCapsSkuAck);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  *SeCCapability = MsgGenGetFwCapsSkuAck.Data.FWCapSku.Data;

  return EFI_SUCCESS;
}


EFI_STATUS
GetSeCFeature (
  UINT32           *SeCFeature
  )
{
  EFI_STATUS       Status;
  SECFWCAPS_SKU    RuleData;

  Status = HeciGetFwFeatureStateMsg (&RuleData);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  *SeCFeature = RuleData.Data;

  return EFI_SUCCESS;
}


UINT32
CheckSeCExist (
  )
{
  //
  // TODO: Need to enable after installing Protocol in Hecidrv.c file
  //
  return 0;
}


SEC_INFOMATION  mSeCInfo;
BOOLEAN         mSeCInfoInited = FALSE;
VOID
InitSeCInfo (
  VOID
  )
{
  EFI_STATUS    Status;

  mSeCInfo.SeCExist = CheckSeCExist ();
  if (mSeCInfo.SeCExist == 0) {
    mSeCInfoInited = TRUE;
    return;
  }
  mSeCInfo.SeCOpMode = GetSeCOpMode ();
  mSeCInfo.SeCEnable = (mSeCInfo.SeCOpMode == 0) ? 1 : 0;
  mSeCInfo.SeCOpEnable = (mSeCInfo.SeCOpMode == 0 || mSeCInfo.SeCOpMode == 3) ? 1 : 0;
  mSeCInfo.HmrfpoEnable = GetSeCHMRFPOStatus ();
  mSeCInfo.FwUpdate= GetSeCFwUpdateStatus ();
  Status = GetSeCFwVersion (&mSeCInfo.SeCVer);

  if (EFI_ERROR (Status)) {
    mSeCInfo.SeCVerValid = FALSE;
  } else {
    mSeCInfo.SeCVerValid = TRUE;
  }
  Status = GetSeCCapability (&mSeCInfo.SeCCapability);
  if (EFI_ERROR (Status)) {
    mSeCInfo.SeCCapabilityValid = FALSE;
  } else {
    mSeCInfo.SeCCapabilityValid = TRUE;
  }
  Status = GetSeCFeature (&mSeCInfo.SeCFeature);
  if (EFI_ERROR (Status)) {
    mSeCInfo.SeCFeatureValid = FALSE;
  } else {
    mSeCInfo.SeCFeatureValid = TRUE;
  }
  Status = HeciGetOemTagMsg (&mSeCInfo.SeCOEMTag);
  if (EFI_ERROR (Status)) {
    mSeCInfo.SeCOEMTagValid = FALSE;
  } else {
    mSeCInfo.SeCOEMTagValid = TRUE;
  }
  mSeCInfoInited = TRUE;
}


EFI_STATUS
EFIAPI
GetPlatformSeCInfo (
  OUT SEC_INFOMATION * SeCInfo
  )
{
  if (!mSeCInfoInited) {
    InitSeCInfo ();
  }

  CopyMem (SeCInfo, &mSeCInfo, sizeof (SEC_INFOMATION));

  return EFI_SUCCESS;
}


EFI_STATUS
EFIAPI
SetPlatformSeCInfo (
  IN  SEC_INFOMATION    *SeCInfo
  )
{
  if (mSeCInfo.SeCExist == 0) {
    return EFI_UNSUPPORTED;
  }
  if (mSeCInfo.SeCEnable != SeCInfo->SeCEnable) {
    if (SeCInfo->SeCEnable == 1) {
      HeciSetSeCEnableMsg ();
    } else {
      HeciSetSeCDisableMsg (0);
    }
  }

  if (mSeCInfo.FwUpdate != SeCInfo->FwUpdate) {
    HeciSetLocalFwUpdate ((UINT8) SeCInfo->FwUpdate);
    mSeCInfo.FwUpdate = SeCInfo->FwUpdate;
  }

  if (mSeCInfo.HmrfpoEnable != SeCInfo->HmrfpoEnable) {
    if (SeCInfo->HmrfpoEnable == 0) {
    } else {
      HmrfpoEnable ();
    }
    CpuDeadLoop ();
  }

  return EFI_SUCCESS;
}


EFI_STATUS
PerformSeCUnConfiguration (
  VOID
  )
{
  EFI_STATUS      Status;
  UINT32          CmdStatus;

  Status = HeciSeCUnconfigurationMsg (&CmdStatus);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "HeciSetSeCEnableMsg Status = %r\n", Status));
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  DEBUG ((EFI_D_INFO, "Command Status = %r\n", CmdStatus));

  //
  // wait for status
  //
  Status = HeciSeCUnconfigurationStatusMsg (&CmdStatus);
  DEBUG ((EFI_D_INFO, "HeciSeCUnconfigurationStatusMsg Status = %r CmdStatus = %x\n", Status, CmdStatus));
  while (CmdStatus == SEC_UNCONFIG_IN_PROGRESS) {
    Status = HeciSeCUnconfigurationStatusMsg (&CmdStatus);
    DEBUG ((EFI_D_INFO, "HeciSeCUnconfigurationStatusMsg Status = %r CmdStatus = %x\n", Status, CmdStatus));
    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_ERROR, "HeciGetFwFeatureStateMsg Status = %r\n", Status));
      ASSERT_EFI_ERROR (Status);
      return Status;
    }
  }
  if (CmdStatus == SEC_UNCONFIG_SUCCESS) {
    //
    // Send Global reset
    //
    HeciSendCbmResetRequest (CBM_RR_REQ_ORIGIN_BIOS_POST, CBM_HRR_GLOBAL_RESET);
    CpuDeadLoop ();

  } else if (CmdStatus == SEC_UNCONFIG_ERROR) {
    DEBUG ((EFI_D_ERROR, "Error ! UnConfiguration Error happened\n"));
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  return EFI_SUCCESS;
}

EFI_STATUS
PerformCheckSeCUnConfiguration (
  VOID
  )
{
  UINT32        CmdStatus;
  EFI_STATUS    Status;

  Status = HeciSeCUnconfigurationStatusMsg (&CmdStatus);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  while (CmdStatus == SEC_UNCONFIG_IN_PROGRESS) {
    Status = HeciSeCUnconfigurationStatusMsg (&CmdStatus);
    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_ERROR, "HeciGetFwFeatureStateMsg Status = %r\n", Status));
      ASSERT_EFI_ERROR (Status);
      return Status;
    }
  }

  if (CmdStatus == SEC_UNCONFIG_SUCCESS) {
    DEBUG ((EFI_D_ERROR, "UNCONFIGURATION Status = SEC_UNCONFIG_SUCCESS\n"));

    //
    // Send Global reset
    //
    HeciSendCbmResetRequest (CBM_RR_REQ_ORIGIN_BIOS_POST, CBM_HRR_GLOBAL_RESET);
    CpuDeadLoop ();

  } else if (CmdStatus == SEC_UNCONFIG_ERROR) {
    DEBUG ((EFI_D_ERROR, "Error ! UnConfiguration Error happened\n"));
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  return Status;
}


#define HMRFPO_B3_ENABLE

/**
  Send the HMRFPO_DISABLE MEI message.

**/
EFI_STATUS
SeCHmrfpoDisable (
  VOID
  )
{
#ifdef HMRFPO_B3_ENABLE
  UINT32    FWstatus;
  UINT32    WriteValue;
  UINT8     StallCount;
  UINTN     Index;

  FWstatus    = 0;
  WriteValue  = 0;
  StallCount  = 0;

  DEBUG ((EFI_D_INFO, "(B3) SeC FW Downgrade - Send the HMRFPO_DISABLE MEI message\n"));

  WriteValue  = HeciPciRead32 (R_GEN_STS);
  WriteValue  = WriteValue & BRNGUP_HMRFPO_DISABLE_CMD_MASK;
  WriteValue  = WriteValue | 0x30000000;
  DEBUG ((EFI_D_INFO, "SeC FW Downgrade Writing %x to register %x of PCI space\n", WriteValue, R_GEN_STS));

  //
  // Set the highest Byte of General Status Register (Bits 28-31)
  //
  HeciPciWrite32 (R_GEN_STS, WriteValue);
  FWstatus = HeciPciRead32 (R_SEC_FW_STS0);
  while (
    ((FWstatus & BRNGUP_HMRFPO_DISABLE_OVR_MASK) != BRNGUP_HMRFPO_DISABLE_OVR_RSP) &&
    (StallCount < FW_MSG_DELAY_TIMEOUT)) {
    DEBUG ((EFI_D_ERROR, "SeC FW Downgrade - SEC HMRFPO Disable Status = 0x%x\n", FWstatus));
    FWstatus = HeciPciRead32 (R_SEC_FW_STS0);
    gBS->Stall (FW_MSG_DELAY);
    StallCount = StallCount + 1;
  }

  if ((FWstatus & BRNGUP_HMRFPO_DISABLE_OVR_MASK) == BRNGUP_HMRFPO_DISABLE_OVR_RSP) {
    DEBUG ((EFI_D_ERROR, "SeC FW Downgrade Disable Msg Received Successfully\n"));
  } else {
    DEBUG ((EFI_D_ERROR, "SeC FW Downgrade Disable Msg ACK not Received\n"));
  }

  //
  // Hide SEC devices so we don't get a yellow bang in OS with disabled devices
  //
  for (Index = 0; Index < 10; Index ++) {
  DEBUG ((EFI_D_INFO, "wait %x\n", Index));

  gBS->Stall (1000000);
    }

#endif // HMRFPO_B3_ENABLE
  return EFI_SUCCESS;
}


EFI_STATUS
PerformCheckHMRFPO (
  VOID
  )
{
  EFI_STATUS         Status;
  EFI_HECI_PROTOCOL  *Heci;
  UINT32             SeCMode;
  UINT32             SeCStatus;
  UINT64             Nonce;
  UINT32             FactoryDefaultBase;
  UINT32             FactoryDefaultLimit;
  HECI_FWS_REGISTER  SeCFirmwareStatus;

  SeCFirmwareStatus.ul = HeciPciRead32 (R_SEC_FW_STS0);

  Status = gBS->LocateProtocol (
                  &gEfiHeciProtocolGuid,
                  NULL,
                  (VOID **) &Heci
                  );

  ASSERT_EFI_ERROR (Status);

  Status = Heci->GetSeCMode (HECI1_DEVICE, &SeCMode);
  ASSERT_EFI_ERROR (Status);

  Status = Heci->GetSeCStatus (&SeCStatus);
  ASSERT_EFI_ERROR (Status);

  //
  // (B1) Whcih mode ?
  //
  if (SeCMode == SEC_MODE_NORMAL) {
    //
    // (A7) The BIOS sends the HMRFPO Lock MEI message and continues the normal boot
    //
    HeciHmrfpoLockResult = HMRFPO_LOCK_SUCCESS;

    //
    // The SEC firmware will ignore the HMRFPO LOCK command if SEC is in SEC manufacturing mode
    //
    if ((SeCFirmwareStatus.r.ManufacturingMode == 0) &&
        ((SEC_STATUS_SEC_STATE_ONLY(SeCStatus) == SEC_IN_RECOVERY_MODE) ||
         (SEC_STATUS_SEC_STATE_ONLY(SeCStatus) == SEC_READY))) {

      DEBUG ((EFI_D_ERROR, "(A7) SeC FW Downgrade - The BIOS sends the HMRFPO Lock SECI message and continues the normal boot\n"));

      FactoryDefaultBase  = 0;
      FactoryDefaultLimit = 0;
      Status              = HeciHmrfpoLock (&Nonce, &FactoryDefaultBase, &FactoryDefaultLimit, &HeciHmrfpoLockResult);
      if (Status != EFI_SUCCESS) {
        HeciHmrfpoLockResult = HMRFPO_LOCK_FAILURE;
      }
    }
  } else {
    //
    // (B3) Call the HMRFPO_DISABLE
    //
    if ((SeCFirmwareStatus.r.SeCOperationMode == SEC_OPERATION_MODE_SECOVR_HECI_MSG) &&
        (SEC_STATUS_SEC_STATE_ONLY (SeCStatus) == SEC_READY)) {
      SeCHmrfpoDisable ();
    }
  }

  return EFI_SUCCESS;
}


EFI_STATUS
EFIAPI
PerformSeCOperation (
  IN  UINTN  SeCOpId
  )
{
  if (CheckSeCExist() == 0) {
    return EFI_UNSUPPORTED;
  }
  switch (SeCOpId) {
    case SEC_OP_UNCONFIGURATION:
      return PerformSeCUnConfiguration ();
      break;

    case SEC_OP_CHECK_UNCONFIG:
      return PerformCheckSeCUnConfiguration ();
      break;

    case SEC_OP_CHECK_HMRFPO:
      return PerformCheckHMRFPO ();
      break;

    default:
      ASSERT (FALSE);
      break;
  }

  return EFI_SUCCESS;
}


/**
  Signal a event for SeC ready to boot.

  @param[in]  Event               The event that triggered this notification function
  @param[in]  ParentImageHandle   Pointer to the notification functions context

  @retval     Status.

**/
VOID
EFIAPI
SeCPolicyReadyToBootEvent (
  EFI_EVENT               Event,
  VOID                    *ParentImageHandle
  )
{
  EFI_HECI_PROTOCOL       *Heci;
  EFI_STATUS              Status;
  UINT32                  SeCMode;
  UINT32                  SeCStatus;

  DEBUG ((EFI_D_INFO, "SeCPolicyReadyToBootEvent ++\n"));

  Status = gBS->LocateProtocol (
                  &gEfiHeciProtocolGuid,
                  NULL,
                  (VOID **) &Heci
                  );

  if (EFI_ERROR (Status)) {
    return;
  }

  Status = Heci->GetSeCMode (HECI1_DEVICE, &SeCMode);
  if (EFI_ERROR(Status)) {
    return;
  }

  Status = Heci->GetSeCStatus (&SeCStatus);
  if (EFI_ERROR (Status)) {
    return;
  }

  if ((SeCMode == SEC_MODE_NORMAL) && (SEC_STATUS_SEC_STATE_ONLY (SeCStatus) == SEC_READY)) {
    if (mDxePlatformSeCPolicyProtocol.SeCConfig.EndOfPostEnabled == 1) {
      mSeCInfo.SeCEOPDone = 1;
    }
  }
  ASSERT_EFI_ERROR (Status);

  gBS->CloseEvent (Event);

  DEBUG ((EFI_D_INFO, "SeCPolicyReadyToBootEvent --\n"));
  return;
}