summaryrefslogtreecommitdiff
path: root/Core/EM/HddSmart/IdeSMART.c
blob: 4212b0f9b81506597167e50b7be3c1c9713fc1a2 (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
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/HddSmart/IdeSMART.c 26    12/22/11 4:29a Lavanyap $
//
// $Revision: 26 $
//
// $Date: 12/22/11 4:29a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/HddSmart/IdeSMART.c $
// 
// 26    12/22/11 4:29a Lavanyap
// [TAG]  		EIP77259
// [Category]  	Improvement
// [Description]  	Included rollbacked check-ins from ver 14, 15, 16, 17
// and made it compatible with core 4.6.5.3.
// [Files]  		IdeSMART.c, IdeSMART.h, IdeSMART.mak, IdeSMART.uni,
// IdeSMART.dxs
// 
// 25    12/19/11 1:10a Rameshr
// [TAG]  		EIP77527
// [Category]  	Improvement
// [Description]  	IdeSecurity, IdeAcoustic and IdeSmart changes should be
// done based on the Core Version checking.
// [Files]  		IdeSecurity.c, IdeSmart.c , Acoustic.c 
// 
// 24    12/07/11 1:29a Rameshr
// EIP 77142 changes checked-in.
// 
// 22    12/05/11 6:25p Rajkumarkc
// [TAG]         EIP77142 
// [Category]    Improvement
// [Description] Added the function 'IdeNonDataCommandExp' in the
// 'IDE_BUS_PROTOCOL' and removed the existing function
// 'IdeNonDataCommand' for supporting the upper 24bits of LBA.
// [Files]
//     Ata.c, IdeBus.c, Idebus.h, PIDEBus.h, Acoustic.c, IdeSecurity.c,
// IdeSMART.c
// 
// 21    11/16/11 1:40a Lavanyap
//  [TAG]  		EIP45383
//  [Category]  	New Feature
//  [Description]  	Added Setup option in Advanced setup page to Check HDD
// SMART status in POST.
//  [Files]  		IdeSMART.c, IdeSMART.sdl, IdeSMART.uni, IdeSMART.sd,
// IdeSMART.cif
// 
// 20    4/28/11 9:24a Mirk
// [TAG]  		EIP55708
// [Category]	Improvement
// [Description]  	If IdeSmart Error is detected and Quiet Boot is
// enabled,the system waits for F1, while Quiet Boot Logo is displayed.
// [Solution]	If the IdeSmart error found, switch to PostScreen and
// display the error msg.
// [Files] - IdeSmart.c
// 
// 18    3/17/11 10:37a Krishnakumarg
// [TAG] - EIP 55534
// [Category]- Defect
// [Symptom] - Component HddSmart not labeled for CORE 4.6.4.1
// [Solution] - IdeSmart is updated for Core 4.6.4.1
// [Files] - IdeSmart.c,Idesmart.h,IdeSmart.mak,IdeSmart.dxs,IdeSmart.uni,
// IdeSmart.chm
// 
// 13    8/20/10 4:30p Krishnakumarg
// Ide Smart doesn't support the ATA 8 Read Smart Data. EIP#42492
// Corrected datasize for PollPeriod in Selftest funtion
// 
// 12    8/13/10 4:47p Krishnakumarg
// Ide Smart doesn't support the ATA 8 Read Smart Data. EIP#42492 
// 
// 11    6/13/10 4:14p Krishnakumarg
// Ide Smart Protocol reports errors and prompts for continuing when
// SMARTReturnStatus function is called. EIP#39414
// 
// 10    5/07/10 11:20a Krishnakumarg
// Added ELINK for OEM to override SMART error handling. EIP#31559
// Also has coding standard changes
// 
// 9     5/05/10 5:23p Yul
// EIP 37065 
// System hang or reset when set AHCI mode and install a SMART failed HDD
// with AHCI mode.
// 
// 8     5/03/10 11:03a Krishnakumarg
// Request additional generic functionality to be added to the IdeSMART
// component.EIP#31559
// 
// 7     3/26/10 5:41p Krishnakumarg
// UEFI2.1 compliance change EIP#34744.
// 
// 6     1/15/10 9:56a Yul
// coding standard
//
// 5     3/30/09 9:24a Fredericko
//
// 4     3/11/09 6:49p Fredericko
// EIP19618 removed dependency on foundation.lib functions
//
// 3     4/18/08 10:04a Rameshraju
// Updated proper comments.
//
// 2     10/03/08 6:56p Anandakrishnanl
// Fixed the Left Shift, which was accidently checked in with extra LT
// symbol.
//
// 1     7/03/08 5:19p Anandakrishnanl
// Added IdeSmart Module after removing IdeSmart.c from Idebus Src Module
//
// 2     13/04/07 3:05p Anandakrishnanl
// Ide Bus Module - Update source files to comply with "AMI coding
// standard"!!!
//
// 1     12/01/05 9:43a Felixp
//
// 5     8/22/05 4:32p Srinin
// ATA/ATAPI identification changed.
//
// 5     8/17/05 3:28p Srinin
// ATA/ATAPI device identification modified.
//
// 4     3/04/05 11:34a Mandal
//
// 3     1/18/05 3:22p Felixp
// PrintDebugMessage renamed to Trace
//
// 1     1/10/05 11:30a Srinin
// SMART support added.
//
//<AMI_FHDR_START>
//---------------------------------------------------------------------------
//
// Name: IDESMART.c
//
// Description: IDE SMART Support
//
//---------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "IdeSMART.h"
#include "IdeSMARTStrTokens.h"
#include <Setup.h>

#if EFI_SPECIFICATION_VERSION > 0x20000
#include "Include\UefiHii.h"
#include "Protocol\HiiDatabase.h"
#include "Protocol\HiiString.h"
#endif

static EFI_GUID gHddSmartInitProtocolGuid = HDD_SMART_INIT_PROTOCOL_GUID;
EFI_GUID gSMARTProtocolGuid = IDE_SMART_INTERFACE_GUID;
EFI_GUID gSetupGuid         = SETUP_GUID; 

#if EFI_SPECIFICATION_VERSION > 0x20000
#else
EFI_GUID gEfiHiiProtocolGuid = EFI_HII_PROTOCOL_GUID;
#endif

EFI_GUID gAmiPostManagerProtocolGuid = AMI_POST_MANAGER_PROTOCOL_GUID;

EFI_HII_HANDLE            HiiPostHandle;

#if EFI_SPECIFICATION_VERSION > 0x20000
#define LANGUAGE_CODE_ENGLISH    "en-US"

EFI_HII_STRING_PROTOCOL *HiiString=NULL;
CHAR8 Language[] = LANGUAGE_CODE_ENGLISH;

#else
#define LANGUAGE_CODE_ENGLISH    "eng"

EFI_HII_PROTOCOL          *pHii;
#endif

AMI_POST_MANAGER_PROTOCOL *pAmiPostMgr;
SETUP_DATA                gSetupData;

// Generic function in this file that will call function specified via elink
VOID ReportSmartFailure(BOOLEAN Mode, VOID *Interface);

// ELINK defined function that will be called.  Function name 'OemReportSmartFailureFunction'
//   is #defined via the makefile /D to be the ELINK function name
VOID OemReportSmartFailureFunction(BOOLEAN Mode, VOID *Interface);

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   IdeSMARTEntryPoint
//
// Description: This function is the entry point for IDE SMART Driver.
//
// Input:
//  EFI_HANDLE          ImageHandle     Image handle.
//  EFI_SYSTEM_TABLE    *SystemTable    Pointer to the EFI system table.
//
// Output:  EFI_STATUS
//  EFI_SUCCESS         When everything is going on fine!
//
// Notes:
//  Entry Points are used to locate or install protocol interfaces and
// notification events.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS IdeSMARTEntryPoint(
    IN EFI_HANDLE       ImageHandle,
    IN EFI_SYSTEM_TABLE *SystemTable )
{
    EFI_HANDLE Handle = NULL;
    EFI_STATUS Status;

    InitAmiLib( ImageHandle, SystemTable );

    Status = LoadStrings( ImageHandle, &HiiPostHandle );

    if ( EFI_ERROR( Status )) {
        return Status;
    }

    Status = pBS->AllocatePool( EfiBootServicesData,
                                sizeof(HDD_SMART_INIT_PROTOCOL),
                                (VOID**)&gHddSmartInitProtocol );

    if ( EFI_ERROR( Status )) {
        return Status;
    }

    gHddSmartInitProtocol->InstallSMARTInterface   = InstallSMARTInterface;
    gHddSmartInitProtocol->UnInstallSMARTInterface = UnInstallSMARTInterface;
    gHddSmartInitProtocol->InitSMARTSupport        = InitSMARTSupport;
    gHddSmartInitProtocol->SmartDiagonasticFlag    = FALSE;
    #if SMART_DIAGNOSTICS_SUPPORT
    gHddSmartInitProtocol->SmartDiagonasticFlag = TRUE;
    #endif

    Status = pBS->InstallProtocolInterface(
        &Handle,
        &gHddSmartInitProtocolGuid,
        EFI_NATIVE_INTERFACE,
        gHddSmartInitProtocol
        );
    ASSERT_EFI_ERROR( Status );

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   InitSMARTSupport
//
// Description: Initialize SMART functionality
//
// Input:
//  IN VOID             *BusInterface,
//  IN BOOLEAN          ModeFlag
//
// Output:
//  EFI_STATUS
//
// Referrals: ConfigureIdeDeviceAndController, ConfigureIdeDevice
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS InitSMARTSupport(
    IN VOID    *BusInterface,
    IN BOOLEAN ModeFlag )

{
    UINT8                 bData;
    EFI_STATUS            Status;
    IDENTIFY_DATA         IdentifyData;
    IDE_BUS_PROTOCOL      *IdeBusInterface;
    SATA_DEVICE_INTERFACE *SataDevInterface;
    DEVICE_TYPE           DeviceType;
    COMMAND_STRUCTURE     CommandStructure;
    UINTN                 SetupDataSize = sizeof(SETUP_DATA);  

    IdeBusInterface  = (IDE_BUS_PROTOCOL*)BusInterface;
    SataDevInterface = (SATA_DEVICE_INTERFACE*)BusInterface;

    if ( ModeFlag == FALSE ){
        IdentifyData = IdeBusInterface->IdeDevice.IdentifyData;
        DeviceType   = IdeBusInterface->IdeDevice.DeviceType;
    } else {
        IdentifyData = SataDevInterface->IdentifyData;
        DeviceType   = SataDevInterface->DeviceType;
    }

    //
    // Check for ATA device
    //
    if  ( DeviceType == ATA ){
        //
        // Check if SMART is supported, if no exit
        //
        if ( IdentifyData.Command_Set_Supported_82 & 0x1 ){
            //
            // Whether to Enable or Disable SMART Support
            //
            bData = ENABLE_SMART == 1 ? SMART_ENABLE_CMD : SMART_DISABLE_CMD;

            //
            // Check if current SMART status is what is required
            //
            Status = EFI_SUCCESS;

            if ( !((IdentifyData.Command_Set_Enabled_85 & 0x1) == ENABLE_SMART)) {
                ZeroMemory( &CommandStructure, sizeof(COMMAND_STRUCTURE));
                CommandStructure.Features = bData;
                CommandStructure.LBAMid   = 0x4F;
                CommandStructure.LBAHigh  = 0xC2;
                CommandStructure.Command  = SMART_COMMAND;

                Status = CommonNonDataHook( BusInterface,
                                            CommandStructure,
                                            ModeFlag );

                if ( EFI_ERROR( Status )) {
                    return Status;
                }
            }

            //
            // Enable Autosave feature
            //
            if ((Status == EFI_SUCCESS) && (bData == SMART_ENABLE_CMD)) {
                ZeroMemory( &CommandStructure, sizeof(COMMAND_STRUCTURE));
                CommandStructure.Features    = SMART_AUTOSAVE;
                CommandStructure.SectorCount = 0xF1;
                CommandStructure.LBAMid      = 0x4F;
                CommandStructure.LBAHigh     = 0xC2;
                CommandStructure.Command     = SMART_COMMAND;

                Status = CommonNonDataHook( BusInterface,
                                            CommandStructure,
                                            ModeFlag );

                if ( EFI_ERROR( Status )) {
                    return Status;
                }
											
				//
				// Get SMART Self test setup option
				//
                Status = pRS->GetVariable(L"Setup", &gSetupGuid, NULL,
                                    &SetupDataSize, &gSetupData);

                if (EFI_ERROR(Status)) {
                    //
                    // If Setup Variable is not found, set Default values
                    //
                    gSetupData.SmartSelfTest = DEFAULT_SMART_SELFTEST;
                }

                if (gSetupData.SmartSelfTest) {

                    //
                    // If SMART threshold exceeded, report it to the user.
                    //
                    Status = SMARTReturnStatusWrapper( BusInterface, ModeFlag );

                    if (EFI_ERROR(Status)) {
                        ERROR_CODE( DXE_IDE_SMART_ERROR, EFI_ERROR_MAJOR );
                        ReportSmartFailure(ModeFlag, BusInterface);
                    }
                }
            }
        }
    }
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SMARTReturnStatusWrapper
//
// Description: Return SMART Status
//
// Input:
//      IN VOID             *BusInterface,
//      IN BOOLEAN          ModeFlag
//
// Output:
//  EFI_STATUS
//
// Referrals: ConfigureIdeDeviceAndController, ConfigureIdeDevice
//
// Notes:   Returns EFI_SUCCESS if threshold is not exceeded.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SMARTReturnStatusWrapper(
    IN VOID    *BusInterface,
    IN BOOLEAN ModeFlag )
{
    UINT8                 bData;
    UINT16                wData = THRESHOLD_NOT_EXCEEDED;
    EFI_STATUS            Status;
    COMMAND_STRUCTURE     CommandStructure;
    IDE_BUS_PROTOCOL      *IdeBusInterface;
    SATA_DEVICE_INTERFACE *SataDevInterface;
    EFI_PCI_IO_PROTOCOL   *PciIo            = NULL;

    IdeBusInterface  = (IDE_BUS_PROTOCOL*)BusInterface;
    SataDevInterface = (SATA_DEVICE_INTERFACE*)BusInterface;

    //
    // Read the SMART return Status
    //
    ZeroMemory( &CommandStructure, sizeof(COMMAND_STRUCTURE));
    CommandStructure.Features = SMART_RETURN_STATUS;
    CommandStructure.LBAMid   = 0x4F;
    CommandStructure.LBAHigh  = 0xC2;
    CommandStructure.Command  = SMART_COMMAND;

    Status = CommonNonDataHook( BusInterface, CommandStructure, ModeFlag );

    if ( EFI_ERROR( Status )) {
        return Status;
    }

    if ( ModeFlag == FALSE ){
        if ( !EFI_ERROR( Status )){
            PciIo = IdeBusInterface->PciIO;

            PciIo->Io.Read(
                IdeBusInterface->PciIO,
                EfiPciIoWidthFifoUint8,
                EFI_PCI_IO_PASS_THROUGH_BAR,
                IdeBusInterface->IdeDevice.Regs.CommandBlock.
                LBAHighReg,
                1,
                &bData
                );

            //
            // Get the LBA Mid/High register contends.
            //
            wData = bData << 8;
            PciIo->Io.Read(
                IdeBusInterface->PciIO,
                EfiPciIoWidthFifoUint8,
                EFI_PCI_IO_PASS_THROUGH_BAR,
                IdeBusInterface->IdeDevice.Regs.CommandBlock.
                LBAMidReg,
                1,
                &bData
                );
            wData |= bData;
        }
    }else{
        volatile AHCI_RECEIVED_FIS *FISReceiveAddress;
        FISReceiveAddress = (AHCI_RECEIVED_FIS*)SataDevInterface->PortFISBaseAddr;

        // Get the pointer to RFIS
        if ( !EFI_ERROR( Status ) && (FISReceiveAddress->Ahci_Rfis[0] == FIS_REGISTER_D2H))
            wData = (FISReceiveAddress->Ahci_Rfis[6] << 8) + FISReceiveAddress->Ahci_Rfis[5];
    }

    if ( wData == THRESHOLD_NOT_EXCEEDED )
        Status = EFI_SUCCESS;
    else
        Status = EFI_DEVICE_ERROR;

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SmartErrorThresholdExceeded
//
// Description: Default function to report SMART failures to the POST screen
//
// Input:   BOOLEAN Mode - TRUE - Device is in IDE mode
//                        FALSE - Device is in AHCI mode
//          VOID *Interface - Interface.  Should be cast into SATA_DEVICE_INTERFACE
//              or IDE_BUS_PROTOCOL depending on Mode parameter
//
// Output:  none
//
// Returns: none
//
// Referrals: SMARTReturnStatusWrapper 
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID SmartErrorThresholdExceeded(BOOLEAN ModeFlag, VOID *Interface)
{
    EFI_STATUS Status;

    UINTN StringSize = 0;
    CHAR16 *StrBuffer = NULL;

    CHAR16 *Str1 = NULL;
    CHAR16 *Str2 = NULL;
    CHAR16 *DeviceName = NULL;

    UINT8 ChannelorPort;
    UINT8 DeviceorPMPort;

    STRING_REF Token;

    EFI_INPUT_KEY Key;

    // Get the Port/Channel information from the interface
    if(ModeFlag){
        // Sata Mode
        DeviceName = ((SATA_DEVICE_INTERFACE*)Interface)->UDeviceName->UnicodeString;
        ChannelorPort = ((SATA_DEVICE_INTERFACE*)Interface)->PortNumber;
        DeviceorPMPort = ((SATA_DEVICE_INTERFACE*)Interface)->PMPortNumber;
    }else{
        // IDE Mode
        DeviceName = ((IDE_BUS_PROTOCOL*)Interface)->IdeDevice.UDeviceName->UnicodeString;
        ChannelorPort = ((IDE_BUS_PROTOCOL*)Interface)->IdeDevice.Channel;
        DeviceorPMPort = ((IDE_BUS_PROTOCOL*)Interface)->IdeDevice.Device;
    }

    // Locate the Hii Protocol for Getting strings from our string package
#if EFI_SPECIFICATION_VERSION > 0x20000
    Status = pBS->LocateProtocol( &gEfiHiiStringProtocolGuid, NULL, &HiiString);
#else
    Status = pBS->LocateProtocol( &gEfiHiiProtocolGuid, NULL, &pHii );
#endif
    ASSERT_EFI_ERROR (Status);

    // Locate the AMI Post Manger protocol for displaying information on the post screen
    Status = pBS->LocateProtocol(   &gAmiPostManagerProtocolGuid,
                                    NULL,
                                    &pAmiPostMgr );

    // Check if we could locate the ami post manager
    if(!EFI_ERROR(Status) && pST->ConOut != NULL){
        // Depending on the mode, get different strings to identify the port the failing
        //  device is connected to
        if(ModeFlag){
            // Sata Mode

            // Print the SATA port that the device is connected
            //  so get the "Port" string
            Status = GetHiiString(  HiiPostHandle,
                                    STRING_TOKEN( STR_SMART_PORT ),
                                    &StringSize,
                                    &Str2);

	    	if ( EFI_ERROR( Status )) {
 	           return;
   	    	}

            // allocate enough space to print the "port %d" string 
            Status = pBS->AllocatePool(EfiBootServicesData, sizeof(CHAR16)*(Wcslen(Str2)+4), &Str1);
	    	if ( EFI_ERROR( Status )) {
 	           return;
   	    	}

            // Create the "Port <0|1|2|3|4|5>" string in Str1
            Swprintf(Str1, L"%s %01d", Str2, ChannelorPort);

            // Fill string 2 with a null
            Str2[0] = L'\0';
        } else {
            // IDE Mode

            // Determine if we need to get the "Primary" or "Secondary" string
            if (ChannelorPort == PRIMARY_CHANNEL) Token = STRING_TOKEN(STR_PRIMARY);  
            else Token = STRING_TOKEN(STR_SECONDARY);  

            // Get the string out of the Hiidatabase into Str1
            Status = GetHiiString(  HiiPostHandle,
                                    Token,
                                    &StringSize,
                                    &Str1);

	    	if ( EFI_ERROR( Status )) {
 	           return ;
   	    	}

            // Determine if we need to get the "Master" or "Slave" string
            if (DeviceorPMPort == MASTER_DRIVE) Token = STRING_TOKEN(STR_MASTER);  
            else Token = STRING_TOKEN(STR_SLAVE);  

            // Get the string out of the Hiidatabase into Str1
            Status = GetHiiString(  HiiPostHandle,
                                    Token,
                                    &StringSize,
                                    &Str2);
	    	if ( EFI_ERROR( Status )) {
 	           return ;
   	    	}
        }

        // Determine the size of all the compents we are trying to print
        //  and additional spaces for the newline, Carrage return, the two spaces
        //  the colon and the null terminiation
        StringSize = Wcslen(Str1) + Wcslen(Str2) + Wcslen(DeviceName) + 7;

        Status = pBS->AllocatePool(EfiBootServicesData, StringSize*sizeof(CHAR16), &StrBuffer);
        if ( EFI_ERROR( Status )) {
            return;
        }

        // Build the string for IDE or SATA: 
        // ide string looks like - "<Primary|Secondary> <Master|Slave> : <Device Name String>"
        // sata string looks like - "Port <0|1|2|3|4|5> : <Device Name String>"
        Swprintf(StrBuffer, L"\n\r%s %s: %s", Str1, Str2, DeviceName);

        // free the helper buffers
        pBS->FreePool(Str1);
        Str1 = NULL;
        pBS->FreePool(Str2);
        Str2 = NULL;

        // Display the post message
        pAmiPostMgr->SwitchToPostScreen();
        pAmiPostMgr->DisplayPostMessage(StrBuffer);

        // and free the buffer that was used
        pBS->FreePool(StrBuffer);    
        StrBuffer = NULL;

        // get the smart failure string from the Hii Database
        Status = GetHiiString(HiiPostHandle,
                            STRING_TOKEN(STR_SMART_FAILURE),
                            &StringSize,
                            &Str1);

		if ( EFI_ERROR( Status )) {
            return;
 		}

        // And display it below the drive information strings, then free the space allocated
        pAmiPostMgr->DisplayPostMessage(Str1);
        pBS->FreePool(Str1);
        Str1 = NULL;

        // If keyboard input is available, pause for user input
        if(pST->ConIn != NULL){
            // Get the pause string from the Hii Database
            Status = GetHiiString(HiiPostHandle,
                                   STRING_TOKEN(STR_SMART_FAILURE_PROMPT),
                                   &StringSize,
                                   &Str1);

	    	if ( EFI_ERROR( Status )) {
 	           return;
   	    	}

            // Display the pause message on the screen
            pAmiPostMgr->DisplayPostMessage( Str1 );

            // Free the space allocated by GetHiiString
            pBS->FreePool(Str1);
            
            // And wait for the user to press the input key
            Key.UnicodeChar = 0;
            do
            {
                // Pause for 1/2 a second between key press reads
                pBS->Stall(50000);

                pST->ConIn->ReadKeyStroke( pST->ConIn, &Key );
            } while ( Key.ScanCode != WAIT_FOR_ERROR_KEY );
        }//if(pST->ConIn != NULL)
    }//if(!EFI_ERROR(Status) && pST->ConOut != NULL)
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   InstallSMARTInterface
//
// Description: Installs SMARTInterface Protocol
//
// Input:
//  IN VOID             *BusInterface
//  IN BOOLEAN          ModeFlag
//
// Output:
//  EFI_STATUS
//
// Notes:
//      Already SMART support capability has been established.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS InstallSMARTInterface(
    IN VOID    *BusInterface,
    IN BOOLEAN ModeFlag )
{
    EFI_STATUS            Status = EFI_UNSUPPORTED;
    SMART_INTERFACE       *SMARTInterface;
    UINT8                 *Buffer;
    IDENTIFY_DATA         IdentifyData;
    EFI_HANDLE            IdeDeviceHandle;
    IDE_BUS_PROTOCOL      *IdeBusInterface;
    SATA_DEVICE_INTERFACE *SataDevInterface;
    DEVICE_TYPE           DeviceType;
    COMMAND_STRUCTURE     CommandStructure;

    IdeBusInterface  = (IDE_BUS_PROTOCOL*)BusInterface;
    SataDevInterface = (SATA_DEVICE_INTERFACE*)BusInterface;

    if ( ModeFlag == FALSE ){
        IdentifyData    = IdeBusInterface->IdeDevice.IdentifyData;
        DeviceType      = IdeBusInterface->IdeDevice.DeviceType;
        IdeDeviceHandle = IdeBusInterface->IdeDeviceHandle;
    } else {
        IdentifyData    = SataDevInterface->IdentifyData;
        DeviceType      = SataDevInterface->DeviceType;
        IdeDeviceHandle = SataDevInterface->IdeDeviceHandle;
    }

    //
    // Check for HDD and SMART support
    //
    if  ((DeviceType == ATA)        // It is HDD
         && (IdentifyData.Command_Set_Supported_82 & 0x1)   // SMART supported
         && (IdentifyData.Command_Set_Supported_84 & 0x2))  // SMART Enabled

    {
        Status = pBS->AllocatePool( EfiBootServicesData,
                                    512,
                                    (VOID**)&Buffer );

        if ( EFI_ERROR( Status )){
            return EFI_OUT_OF_RESOURCES;
        }

        ZeroMemory( &CommandStructure, sizeof(COMMAND_STRUCTURE));
        CommandStructure.Buffer    = Buffer;
        CommandStructure.ByteCount = 512;
        CommandStructure.Features  = SMART_READ_DATA;
        CommandStructure.LBAMid    = 0x4F;
        CommandStructure.LBAHigh   = 0xC2;
        CommandStructure.Command   = SMART_COMMAND;

        if ( ModeFlag == FALSE ){
            CommandStructure.Device = IdeBusInterface->IdeDevice.Device << 4;
            Status                  = IdeBusInterface->AtaPioDataIn(
                            IdeBusInterface,
                            CommandStructure.Buffer,
                            CommandStructure.ByteCount,
                            CommandStructure.Features,
                            (UINT8)CommandStructure.SectorCount,
                            CommandStructure.LBALow,
                            CommandStructure.LBAMid,
                            CommandStructure.LBAHigh,
                            CommandStructure.Device,
                            CommandStructure.Command,
                            FALSE);
        } else {
            Status = SataDevInterface->AhciBusInterface->ExecutePioDataCommand(
                SataDevInterface,
                &CommandStructure,
                FALSE );
        }

        if ( EFI_ERROR( Status )){
            pBS->FreePool( Buffer );
            return Status;
        }

        //
        // Install the Interface
        //
        Status = pBS->AllocatePool( EfiBootServicesData,
                                    sizeof(SMART_INTERFACE),
                                    (VOID**)&SMARTInterface );

        if ( EFI_ERROR( Status )){
            pBS->FreePool( Buffer );
            return Status;
        }

        SMARTInterface->SMARTSelfTest       = SMARTSelfTest;
        SMARTInterface->SMARTSelfTestStatus = SMARTSelfTestStatus;
        SMARTInterface->SMARTReturnStatus   = SMARTReturnStatus;
        SMARTInterface->SendSmartCommand    = SendSmartCommand;
        SMARTInterface->SmartReadData       = SmartReadData;
        SMARTInterface->SmartWriteData      = SmartWriteData;
        SMARTInterface->SMARTAbortSelfTest  = SMARTAbortSelfTest;
        SMARTInterface->SMARTGetSmartData   = SMARTGetSmartData;
        SMARTInterface->ShortPollingTime    = Buffer[372];
        if(Buffer[373] == 0xFF)
            SMARTInterface->ExtPollingTime  = (UINT16)(Buffer[376]<<8 | Buffer[375]);
        else
            SMARTInterface->ExtPollingTime  = (UINT16)Buffer[373];

        if ( ModeFlag == FALSE ){
            IdeBusInterface->SMARTInterface = SMARTInterface;
            SMARTInterface->BusInterface    = IdeBusInterface;
        }else{
            SataDevInterface->SMARTInterface = SMARTInterface;
            SMARTInterface->BusInterface     = SataDevInterface;
        }

        //
        // Set ModeFlag to FALSE if IdeBus else TRUE for Ahci Bus.
        //
        SMARTInterface->ModeFlag = ModeFlag;  // Ide Bus

        Status = pBS->InstallMultipleProtocolInterfaces(&IdeDeviceHandle,
                                                        &gSMARTProtocolGuid,
                                                        SMARTInterface,
                                                        NULL );

        pBS->FreePool( Buffer );
    }
    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   UnInstallSMARTInterface
//
// Description: Uninstall SMARTInterface Protocol
//
// Input:
//  IN VOID             *BusInterface
//  IN BOOLEAN          ModeFlag
//
// Output:
//  EFI_STATUS
//
// Referrals: IdeBusStop
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS UnInstallSMARTInterface(
    IN VOID    *BusInterface,
    IN BOOLEAN ModeFlag )
{
    EFI_STATUS            Status;
    SMART_INTERFACE       *SMARTInterface;
    IDE_BUS_PROTOCOL      *IdeBusInterface;
    SATA_DEVICE_INTERFACE *SataDevInterface;
    EFI_HANDLE            IdeDeviceHandle;

    IdeBusInterface  = (IDE_BUS_PROTOCOL*)BusInterface;
    SataDevInterface = (SATA_DEVICE_INTERFACE*)BusInterface;

    if ( ModeFlag == FALSE ){
        SMARTInterface  = IdeBusInterface->SMARTInterface;
        IdeDeviceHandle = IdeBusInterface->IdeDeviceHandle;
    }else{
        SMARTInterface  = SataDevInterface->SMARTInterface;
        IdeDeviceHandle = SataDevInterface->IdeDeviceHandle;
    }

    if ( SMARTInterface == NULL ){
        return EFI_SUCCESS;
    }
    Status = pBS->UninstallMultipleProtocolInterfaces(IdeDeviceHandle,
                                                        &gSMARTProtocolGuid,
                                                        SMARTInterface,
                                                        NULL );

    if ( Status == EFI_SUCCESS ){
        pBS->FreePool( SMARTInterface );
        SMARTInterface = NULL;
    }
    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SMARTSelfTest
//
// Description: Starts SMART SelfTest. SelfTestType will indicate whether to run
//              short or extended selftest.
//
// Input:
//  IN  IDE_SMART_INTERFACE     *This,
//  IN  UINT8                   SelfTestType, ( 0 : short, 1 :extended)
//  OUT UINT16                   *PollPeriod (Minutes)
//
// Output:
//  EFI_STATUS
//  PollPeriod will indicate the recommended time interval between polling.
//  Frequent polling will either abort the self test or may prolong it.
//
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SMARTSelfTest(
    IN IDE_SMART_INTERFACE *This,
    IN UINT8               SelfTestType,
    OUT UINT16              *PollPeriod )
{
    EFI_STATUS            Status;
    UINT8                 bData;

    VOID *BusInterface = ((SMART_INTERFACE* )This)->BusInterface;
    SMART_INTERFACE       *SMARTInterface = (SMART_INTERFACE*)This;
    BOOLEAN               ModeFlag        = ((SMART_INTERFACE* )This)->ModeFlag;
    IDE_BUS_PROTOCOL      *IdeBusInterface;
    SATA_DEVICE_INTERFACE *SataDevInterface;

    IdeBusInterface  = (IDE_BUS_PROTOCOL*)BusInterface;
    SataDevInterface = (SATA_DEVICE_INTERFACE*)BusInterface;

    //
    // Check for Short or Extended
    //
    bData = SelfTestType == 0 ? 1 : 2;
    Status = SendSmartCommand( This, SMART_EXECUTE_OFFLINE_IMMEDIATE, NULL, bData); 

    *PollPeriod = SelfTestType == 0 ? SMARTInterface->ShortPollingTime : SMARTInterface->ExtPollingTime;

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SMARTAbortSelfTest
//
// Description: Aborts SMART Self Test. 
//
// Input:
//  IN  IDE_SMART_INTERFACE *This
//
// Output:
//  EFI_STATUS
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
SMARTAbortSelfTest (
    IN  IDE_SMART_INTERFACE         *This
 )
{
    VOID                            *BusInterface = ((SMART_INTERFACE * )This)->BusInterface;
    SMART_INTERFACE                 *SMARTInterface = (SMART_INTERFACE *)This;
    BOOLEAN                         ModeFlag = ((SMART_INTERFACE * )This)->ModeFlag;
    IDE_BUS_PROTOCOL                *IdeBusInterface;
    SATA_DEVICE_INTERFACE           *SataDevInterface;
    COMMAND_STRUCTURE               CommandStructure;

    IdeBusInterface         = (IDE_BUS_PROTOCOL *)BusInterface;
    SataDevInterface        = (SATA_DEVICE_INTERFACE *)BusInterface;

    ZeroMemory(&CommandStructure, sizeof(COMMAND_STRUCTURE));
    CommandStructure.Features = SMART_EXECUTE_OFFLINE_IMMEDIATE;
    CommandStructure.LBALow   = SMART_ABORT_SELF_TEST_SUBROUTINE;
    CommandStructure.LBAMid   = 0x4F;
    CommandStructure.LBAHigh  = 0xC2;
    CommandStructure.Command  = SMART_COMMAND;
    
    return CommonNonDataHook (BusInterface, CommandStructure, ModeFlag);

}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SMARTGetSmartData
//
// Description:  Get SMART data of the harddisk.
//
// Input:
//  IN  IDE_SMART_INTERFACE This
//
// Output: 
//  OUT UINT8   **SmartData
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS SMARTGetSmartData (
    IN  IDE_SMART_INTERFACE         *This,
    OUT UINT8                       **SmartData
)
{
    EFI_STATUS                      Status;
    SMART_INTERFACE                 *SMARTInterface = (SMART_INTERFACE *)This;
    VOID                            *BusInterface = ((SMART_INTERFACE * )This)->BusInterface;
    BOOLEAN                         ModeFlag = ((SMART_INTERFACE * )This)->ModeFlag;
    IDE_BUS_PROTOCOL                *IdeBusInterface;
    SATA_DEVICE_INTERFACE           *SataDevInterface;
    COMMAND_STRUCTURE               CommandStructure;

    IdeBusInterface         = (IDE_BUS_PROTOCOL *)BusInterface;
    SataDevInterface        = (SATA_DEVICE_INTERFACE *)BusInterface;

    Status = pBS->AllocatePool (EfiBootServicesData, 512, (VOID**)SmartData);

    if (EFI_ERROR(Status)){
        return EFI_OUT_OF_RESOURCES;
    }

    ZeroMemory(&CommandStructure, sizeof(COMMAND_STRUCTURE)); 
    CommandStructure.Buffer     = *SmartData; 
    CommandStructure.ByteCount  = 512;
    CommandStructure.Features   = SMART_READ_DATA;
    CommandStructure.LBAMid     = 0x4F;
    CommandStructure.LBAHigh    = 0xC2;   
    CommandStructure.Command    = SMART_COMMAND;
    
    if (ModeFlag == FALSE) {
        CommandStructure.Device = IdeBusInterface->IdeDevice.Device << 4;
        Status = IdeBusInterface->AtaPioDataIn(  IdeBusInterface, 
                                                 CommandStructure.Buffer,
                                                 CommandStructure.ByteCount,
                                                 CommandStructure.Features, 
                                                 (UINT8)CommandStructure.SectorCount,
                                                 CommandStructure.LBALow, 
                                                 CommandStructure.LBAMid, 
                                                 CommandStructure.LBAHigh,
                                                 CommandStructure.Device, 
                                                 CommandStructure.Command,
                                                 FALSE
                                              );
          
    } else {
        Status = SataDevInterface->AhciBusInterface->ExecutePioDataCommand( 
                                                SataDevInterface, 
                                                &CommandStructure, 
                                                FALSE
                                              );
    }

    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SMARTSelfTestStatus
//
// Description: Starts SMART SelfTest. SelfTestType will indicate whether to run
//              short or extended selftest.
//
// Input:
//  IN      IDE_SMART_INTERFACE         *This,
//  OUT     UINT8                       *TestStatus
//
// Output:
//  EFI_STATUS : TestStatus is valid only when EFI_STATUS is EFI_SUCCESS
//  TestStatus : Will indicate the % of test completed.
//  TestStatus = 0xFF Indicates Self Test Failed and EFI_STATUS = EFI_ERROR
//  Example: When TestStatus = 10, 10% of the test has been done.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SMARTSelfTestStatus(
    IN IDE_SMART_INTERFACE *This,
    OUT UINT8              *TestStatus )
{
    EFI_STATUS            Status;
    UINT8                 *Buffer;
    SMART_INTERFACE       *SMARTInterface = (SMART_INTERFACE*)This;
    VOID *BusInterface = ((SMART_INTERFACE* )This)->BusInterface;
    BOOLEAN               ModeFlag = ((SMART_INTERFACE* )This)->ModeFlag;
    IDE_BUS_PROTOCOL      *IdeBusInterface;
    SATA_DEVICE_INTERFACE *SataDevInterface;
    COMMAND_STRUCTURE     CommandStructure;

    IdeBusInterface  = (IDE_BUS_PROTOCOL*)BusInterface;
    SataDevInterface = (SATA_DEVICE_INTERFACE*)BusInterface;

    Status = pBS->AllocatePool( EfiBootServicesData,
                                512,
                                (VOID**)&Buffer );

    if ( EFI_ERROR( Status )){
        return EFI_OUT_OF_RESOURCES;
    }

    ZeroMemory( &CommandStructure, sizeof(COMMAND_STRUCTURE));
    CommandStructure.Buffer    = Buffer;
    CommandStructure.ByteCount = 512;
    CommandStructure.Features  = SMART_READ_DATA;
    CommandStructure.LBAMid    = 0x4F;
    CommandStructure.LBAHigh   = 0xC2;
    CommandStructure.Command   = SMART_COMMAND;

    if ( ModeFlag == FALSE ){
        CommandStructure.Device = IdeBusInterface->IdeDevice.Device << 4;
        Status = IdeBusInterface->AtaPioDataIn(IdeBusInterface,
                                                CommandStructure.Buffer,
                                                CommandStructure.ByteCount,
                                                CommandStructure.Features,
                                                (UINT8)CommandStructure.SectorCount,
                                                CommandStructure.LBALow,
                                                CommandStructure.LBAMid,
                                                CommandStructure.LBAHigh,
                                                CommandStructure.Device,
                                                CommandStructure.Command,
                                                FALSE);
    }else{
        Status = SataDevInterface->AhciBusInterface->ExecutePioDataCommand(
            SataDevInterface,
            &CommandStructure,
            FALSE );
    }

    if ( EFI_ERROR( Status )){
        *TestStatus = 0xff;
        pBS->FreePool( Buffer );
        return EFI_DEVICE_ERROR;
    }

    //
    // If Bits 7:4 is not equal to 0 or 0xf then there was an error
    // in the previous selftest.
    //
    if (((Buffer[363] >> 4) != 0) && ((Buffer[363] >> 4) != 0xf)) {
        *TestStatus = 0xff;
        Status      = EFI_DEVICE_ERROR;
    }else{
        *TestStatus = Buffer[363] & 0xf;
        *TestStatus = 10 - (*TestStatus);
        *TestStatus = 10 * (*TestStatus);
    }
    pBS->FreePool( Buffer );
    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SMARTReturnStatus
//
// Description: Returns SMART status
//
// Input:
//      IN      IDE_SMART_INTERFACE         *This
//
// Output:
//  EFI_STATUS
//
// Notes:   Returns EFI_SUCCESS if threshold is not exceeded.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SMARTReturnStatus(
    IN IDE_SMART_INTERFACE *This )
{
    VOID    *BusInterface = ((SMART_INTERFACE* )This)->BusInterface;
    BOOLEAN ModeFlag      = ((SMART_INTERFACE* )This)->ModeFlag;

    return (SMARTReturnStatusWrapper( BusInterface, ModeFlag ));
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SendSMARTCommand
//
// Description: Sends any Non-Data SMART Command. 
//
// Input:
//      IN      IDE_SMART_INTERFACE         *This
//      IN      UINT8                       SubCommand,
//      IN      UINT8                       AutoSaveEnable,      //OPTIONAL
//      IN      UINT8                       SelfTestType         //OPTIONAL
//
// Output:
//      EFI_STATUS
//
// Referrals: IdeNonDataCommand, ExecuteNonDataCommand
//
// Notes:   Returns EFI_UNSUPPORTED when SMART feature is not enabled.
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS SendSmartCommand ( 
    IN IDE_SMART_INTERFACE  *This,
    IN UINT8                SubCommand,
    IN UINT8                AutoSaveEnable,      
    IN UINT8                SelfTestType      
)
{
    EFI_STATUS                  Status;
    SMART_INTERFACE             *SMARTInterface = (SMART_INTERFACE *)This;
    VOID                        *BusInterface = ((SMART_INTERFACE * )This)->BusInterface;
    BOOLEAN                     ModeFlag = ((SMART_INTERFACE * )This)->ModeFlag;
    IDE_BUS_PROTOCOL            *IdeBusInterface;
    SATA_DEVICE_INTERFACE       *SataDevInterface;
    COMMAND_STRUCTURE           CommandStructure;
    IDENTIFY_DATA               IdentifyData;
    BOOLEAN                     EnableSmart;

    IdeBusInterface  = (IDE_BUS_PROTOCOL*)BusInterface;
    SataDevInterface = (SATA_DEVICE_INTERFACE*)BusInterface;

    if ( ModeFlag == FALSE ){
        IdentifyData = IdeBusInterface->IdeDevice.IdentifyData;
    } else {
        IdentifyData = SataDevInterface->IdentifyData;
    }

    EnableSmart = SubCommand == SMART_ENABLE_CMD ? 1 : 0; 

    if(!((IdentifyData.Command_Set_Enabled_85 & 0x1) == EnableSmart)) {

   	    ZeroMemory(&CommandStructure, sizeof(COMMAND_STRUCTURE));
        CommandStructure.Features = SubCommand;
        CommandStructure.LBAMid   = 0x4F;
        CommandStructure.LBAHigh  = 0xC2;
        CommandStructure.Command  = SMART_COMMAND;

        switch (SubCommand) {

        case SMART_AUTOSAVE:
          CommandStructure.SectorCount = AutoSaveEnable;
          break;

        case SMART_EXECUTE_OFFLINE_IMMEDIATE:
          CommandStructure.LBALow   = SelfTestType;
          break;

        default:
          break;
        }

        if ( ModeFlag == FALSE ){
	        CommandStructure.Device = IdeBusInterface->IdeDevice.Device << 4;

	        //
            // Idebus API changed from Core 4.6.5.2. Added Core Version check for the OLD 
            // Core support.
            //
#if defined CORE_COMBINED_VERSION && (CORE_COMBINED_VERSION > 0x4028b) 
            Status = IdeBusInterface->IdeNonDataCommand(IdeBusInterface,
                                                    CommandStructure.Features,
                                                    (UINT8)CommandStructure.SectorCount,
                                                    (UINT8)(CommandStructure.SectorCount >> 8),
                                                    CommandStructure.LBALow,
                                                    CommandStructure.LBALowExp,
                                                    CommandStructure.LBAMid,
                                                    CommandStructure.LBAMidExp,
                                                    CommandStructure.LBAHigh,
                                                    CommandStructure.LBAHighExp,
                                                    CommandStructure.Device,
                                                    CommandStructure.Command);
#else
           Status = IdeBusInterface->IdeNonDataCommand(IdeBusInterface,
                                                    CommandStructure.Features,
                                                    (UINT8)CommandStructure.SectorCount,
                                                    CommandStructure.LBALow,
                                                    CommandStructure.LBAMid,
                                                    CommandStructure.LBAHigh,
                                                    CommandStructure.Device,
                                                    CommandStructure.Command);
#endif
        } else {
            Status = SataDevInterface->AhciBusInterface->ExecuteNonDataCommand(
                                                            SataDevInterface,
                                                            CommandStructure );
        }
        return Status;
                                                
    } else {
        return EFI_UNSUPPORTED;
    }
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SmartReadData
//
// Description: Sends any Data In SMART command. 
//
// Input:
//      IN      IDE_SMART_INTERFACE         *This
//      IN      UINT8                       SubCommand,
//      IN      VOID                        *Buffer,
//      IN      UINT8                       LogAddress,   //OPTIONAL    
//      IN      UINT8                       SectorCount   //OPTIONAL  
// Output:
//      EFI_STATUS
//
// Referrals: AtaPioDataIn, SataPioDataOut
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SmartReadData (
    IN  IDE_SMART_INTERFACE  *This,
    IN  UINT8                SubCommand,
    OUT VOID                 *Buffer,
    IN  UINT8                LogAddress,
    IN  UINT8                SectorCount
)
{
    EFI_STATUS              Status;
    SMART_INTERFACE         *SMARTInterface = (SMART_INTERFACE *)This;
    VOID                    *BusInterface = ((SMART_INTERFACE * )This)->BusInterface;
    BOOLEAN                 ModeFlag = ((SMART_INTERFACE * )This)->ModeFlag;
    IDE_BUS_PROTOCOL        *IdeBusInterface;
    SATA_DEVICE_INTERFACE   *SataDevInterface;
    COMMAND_STRUCTURE       CommandStructure;
    BOOLEAN                 MultipleSectors = FALSE;

   IdeBusInterface         = (IDE_BUS_PROTOCOL *)BusInterface;
   SataDevInterface        = (SATA_DEVICE_INTERFACE *)BusInterface;

    //
    //If SubCommand = SMART_READ_DATA then set SectorCount to 1 
    //
    if (SectorCount == NULL) {
        SectorCount = 1;
    }

    ZeroMemory(&CommandStructure, sizeof(COMMAND_STRUCTURE));
    CommandStructure.Buffer     = Buffer; 

    if ( SubCommand == SMART_READLOG ) {
        CommandStructure.LBALow       = LogAddress ;      // LOG Address to read
        CommandStructure.SectorCount  = SectorCount;      // No.of sectors to read
        CommandStructure.ByteCount    = SectorCount*ATA_SECTOR_BYTES;
        if ( SectorCount>1 ) {
            MultipleSectors = TRUE;
        }
    } else {
        //
        // SubCommand SMART_READ_DATA reads only 512 bytes.
        //
        CommandStructure.ByteCount    = 512;
    }
    CommandStructure.Features   = SubCommand;
    CommandStructure.LBAMid     = 0x4F;
    CommandStructure.LBAHigh    = 0xC2;
    CommandStructure.Command    = SMART_COMMAND;
 
    if (ModeFlag == FALSE) {

        Status = IdeBusInterface->AtaPioDataOut ( 
                                       IdeBusInterface,
                                       CommandStructure.Buffer,
                                       CommandStructure.ByteCount, 
                                       CommandStructure.Features,
                                       CommandStructure.SectorCount, 
                                       CommandStructure.LBALow, 
                                       CommandStructure.LBALowExp, 
                                       CommandStructure.LBAMid, 
                                       CommandStructure.LBAMidExp, 
                                       CommandStructure.LBAHigh,
                                       CommandStructure.LBAHighExp, 
                                       CommandStructure.Device, 
                                       CommandStructure.Command,
                                       FALSE, 
                                       MultipleSectors );
          
    } else {

        Status = SataDevInterface->AhciBusInterface->SataPioDataOut(
                                                        SataDevInterface,
                                                        CommandStructure.Buffer,
                                                        CommandStructure.ByteCount,
                                                        CommandStructure.Features,
                                                        CommandStructure.LBALow, 
                                                        CommandStructure.LBALowExp, 
                                                        CommandStructure.LBAMid, 
                                                        CommandStructure.LBAMidExp, 
                                                        CommandStructure.LBAHigh,
                                                        CommandStructure.LBAHighExp,
                                                        CommandStructure.Command,
                                                        FALSE );
    }
	if (EFI_ERROR(Status)) {
        pBS->FreePool(Buffer);
	}
    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   SmartWriteData
//
// Description: Sends any Data Out SMART command.
//
// Input:
//      IN      IDE_SMART_INTERFACE         *This
//      IN      UINT8                       SubCommand,
//      IN      VOID                        *Buffer,
//      IN      UINT8                       LogAddress,
//      IN      UINT8                       SectorCount
// Output:
//      EFI_STATUS
//
// Referrals: AtaPioDataOut, SataPioDataOut
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SmartWriteData (
    IN IDE_SMART_INTERFACE  *This,
    IN UINT8                SubCommand,
    IN VOID                 *Buffer,
    IN UINT8                LogAddress,
    IN UINT8                SectorCount
)
{
    EFI_STATUS                  Status;
    IDE_BUS_PROTOCOL            *IdeBusInterface;
    SATA_DEVICE_INTERFACE       *SataDevInterface;
    COMMAND_STRUCTURE           CommandStructure;
    SMART_INTERFACE             *SMARTInterface = (SMART_INTERFACE *)This;
    VOID                        *BusInterface = ((SMART_INTERFACE * )This)->BusInterface;
    BOOLEAN                     ModeFlag = ((SMART_INTERFACE * )This)->ModeFlag;
    BOOLEAN                     MultipleSectors = FALSE;

   	IdeBusInterface         = (IDE_BUS_PROTOCOL *)BusInterface;
   	SataDevInterface        = (SATA_DEVICE_INTERFACE *)BusInterface;

    ZeroMemory( &CommandStructure, sizeof(COMMAND_STRUCTURE));
    CommandStructure.Buffer      = Buffer;
    CommandStructure.Features    = SubCommand;
    CommandStructure.ByteCount   = SectorCount * ATA_SECTOR_BYTES;
    CommandStructure.SectorCount = SectorCount;
    CommandStructure.LBALow      = LogAddress;       //LOG address to write on
    CommandStructure.LBAMid      = 0x4F;
    CommandStructure.LBAHigh     = 0xC2;
    CommandStructure.Command     = SMART_COMMAND;

    if ( SectorCount>1 )
        MultipleSectors = TRUE;

    if (ModeFlag == FALSE) {
        Status = IdeBusInterface->AtaPioDataOut ( 
                                       IdeBusInterface,
                                       CommandStructure.Buffer,
                                       CommandStructure.ByteCount, 
                                       CommandStructure.Features,
                                       CommandStructure.SectorCount, 
                                       CommandStructure.LBALow, 
                                       CommandStructure.LBALowExp, 
                                       CommandStructure.LBAMid, 
                                       CommandStructure.LBAMidExp, 
                                       CommandStructure.LBAHigh,
                                       CommandStructure.LBAHighExp, 
                                       CommandStructure.Device, 
                                       CommandStructure.Command,
                                       TRUE, 
                                       MultipleSectors ); 
    } else {

        Status = SataDevInterface->AhciBusInterface->SataPioDataOut (
                                                        SataDevInterface, 
                                                        CommandStructure.Buffer,
                                                        CommandStructure.ByteCount, 
                                                        CommandStructure.Features,
                                                        CommandStructure.LBALow, 
                                                        CommandStructure.LBALowExp, 
                                                        CommandStructure.LBAMid, 
                                                        CommandStructure.LBAMidExp, 
                                                        CommandStructure.LBAHigh,
                                                        CommandStructure.LBAHighExp, 
                                                        CommandStructure.Command,
                                                        TRUE ); 
    }
    
    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
//
// Procedure:   CommonNonDataHook
//
// Description: Common Hook which Calls Non Data Command.
//
// Input:       IN    VOID*                 BusInterface,
//              IN    COMMAND_STRUCTURE     CommandStructure,
//              IN    BOOLEAN               ModeFlag
//
// Output:  
//  EFI_STATUS
//
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS CommonNonDataHook(
    IN VOID              *BusInterface,
    IN COMMAND_STRUCTURE CommandStructure,
    IN BOOLEAN           ModeFlag )
{
    EFI_STATUS            Status;
    IDE_BUS_PROTOCOL      *IdeBusInterface;
    SATA_DEVICE_INTERFACE *SataDevInterface;

    IdeBusInterface  = (IDE_BUS_PROTOCOL*)BusInterface;
    SataDevInterface = (SATA_DEVICE_INTERFACE*)BusInterface;

    if ( ModeFlag == FALSE ){
        CommandStructure.Device = IdeBusInterface->IdeDevice.Device << 4;
        //
        // Idebus API changed from Core 4.6.5.2. Added Core Version check for the OLD 
        // Core support.
        //
#if defined CORE_COMBINED_VERSION && (CORE_COMBINED_VERSION > 0x4028b) 
        Status = IdeBusInterface->IdeNonDataCommand(IdeBusInterface,
                                                    CommandStructure.Features,
                                                    (UINT8)CommandStructure.SectorCount,
                                                    (UINT8)(CommandStructure.SectorCount >> 8),
                                                    CommandStructure.LBALow,
                                                    CommandStructure.LBALowExp,
                                                    CommandStructure.LBAMid,
                                                    CommandStructure.LBAMidExp,
                                                    CommandStructure.LBAHigh,
                                                    CommandStructure.LBAHighExp,
                                                    CommandStructure.Device,
                                                    CommandStructure.Command);
#else
        Status = IdeBusInterface->IdeNonDataCommand(IdeBusInterface,
                                                    CommandStructure.Features,
                                                    (UINT8)CommandStructure.SectorCount,
                                                    CommandStructure.LBALow,
                                                    CommandStructure.LBAMid,
                                                    CommandStructure.LBAHigh,
                                                    CommandStructure.Device,
                                                    CommandStructure.Command);
#endif
    }else {
        Status = SataDevInterface->AhciBusInterface->ExecuteNonDataCommand(
            SataDevInterface,
            CommandStructure );
    }

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   GetHiiString
//
// Description: This function Reads a String from HII
//
// Input:       HiiHandle - Efi Hii Handle
//              Token     - String Token
//
// Output:      Returns  Pointer to allocated String Buffer
//              NULL -  if could not get the data
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS GetHiiString(
    IN EFI_HII_HANDLE HiiHandle,
    IN STRING_REF     Token,
    IN OUT UINTN      *pDataSize,
    OUT EFI_STRING    *ppData )
{
    EFI_STATUS Status;

    if ( !*ppData ){
        *pDataSize = 0;
    }

#if EFI_SPECIFICATION_VERSION>0x20000
         Status = HiiString->GetString (HiiString,
                                (CHAR8*) &Language,
                                HiiHandle,
                                Token,
                                *ppData,
                                pDataSize,
                                NULL);

#else
    Status = pHii->GetString (  pHii,
                                HiiHandle,
                                Token,
                                TRUE,
                                L"   ",
                                pDataSize,
                                *ppData);
#endif

    if ( !EFI_ERROR( Status )){
        return Status;
    }

    if ( Status == EFI_BUFFER_TOO_SMALL ){

        if ( *ppData ){
            pBS->FreePool( *ppData );
        }

        if ( !(*ppData = Malloc( *pDataSize ))){
            return EFI_OUT_OF_RESOURCES;
        }
#if EFI_SPECIFICATION_VERSION>0x20000
         Status = HiiString->GetString (HiiString,
                                (CHAR8*) &Language,
                                HiiHandle,
                                Token,
                                *ppData,
                                pDataSize,
                                NULL);

#else
        Status = pHii->GetString (  pHii, 
                                    HiiHandle, 
                                    Token, 
                                    TRUE, 
                                    L"   ",
                                    pDataSize, 
                                    *ppData);
#endif
    }
    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   ReportSmartFailure
//
// Description: Wrapper Function to call a ELINKed function
//
// Input:   BOOLEAN Mode - TRUE - Device is in IDE mode
//                        FALSE - Device is in AHCI mode
//          VOID *Interface - Interface.  Should be cast into SATA_DEVICE_INTERFACE
//              or IDE_BUS_PROTOCOL depending on Mode parameter
//
// Output:  none
//
// Returns: none
//
// Referrals: SmartErrorThresholdExceeded 
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID ReportSmartFailure(BOOLEAN Mode, VOID *Interface)
{
    OemReportSmartFailureFunction(Mode, Interface);
}
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************