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
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
|
/** @file
PEIM to produce gEfiPeiVirtualBlockIoPpiGuid PPI for ATA controllers in the platform.
This PPI canl be consumed by PEIM which produce gEfiPeiDeviceRecoveryModulePpiGuid
for Atapi CD ROM device.
Copyright (c) 2006 - 2014, 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 "AtapiPeim.h"
/**
Initializes the Atapi Block Io PPI.
@param[in] FileHandle Handle of the file being invoked.
@param[in] PeiServices Describes the list of possible PEI Services.
@retval EFI_SUCCESS Operation performed successfully.
@retval EFI_OUT_OF_RESOURCES Not enough memory to allocate.
**/
EFI_STATUS
EFIAPI
AtapiPeimEntry (
IN EFI_PEI_FILE_HANDLE FileHandle,
IN CONST EFI_PEI_SERVICES **PeiServices
)
{
PEI_ATA_CONTROLLER_PPI *AtaControllerPpi;
EFI_STATUS Status;
ATAPI_BLK_IO_DEV *AtapiBlkIoDev;
Status = PeiServicesRegisterForShadow (FileHandle);
if (!EFI_ERROR (Status)) {
return Status;
}
Status = PeiServicesLocatePpi (
&gPeiAtaControllerPpiGuid,
0,
NULL,
(VOID **) &AtaControllerPpi
);
ASSERT_EFI_ERROR (Status);
AtapiBlkIoDev = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (*AtapiBlkIoDev)));
if (AtapiBlkIoDev == NULL) {
return EFI_OUT_OF_RESOURCES;
}
AtapiBlkIoDev->Signature = ATAPI_BLK_IO_DEV_SIGNATURE;
AtapiBlkIoDev->AtaControllerPpi = AtaControllerPpi;
//
// atapi device enumeration and build private data
//
AtapiEnumerateDevices (AtapiBlkIoDev);
AtapiBlkIoDev->AtapiBlkIo.GetNumberOfBlockDevices = AtapiGetNumberOfBlockDevices;
AtapiBlkIoDev->AtapiBlkIo.GetBlockDeviceMediaInfo = AtapiGetBlockDeviceMediaInfo;
AtapiBlkIoDev->AtapiBlkIo.ReadBlocks = AtapiReadBlocks;
AtapiBlkIoDev->PpiDescriptor.Flags = (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);
AtapiBlkIoDev->PpiDescriptor.Guid = &gEfiPeiVirtualBlockIoPpiGuid;
AtapiBlkIoDev->PpiDescriptor.Ppi = &AtapiBlkIoDev->AtapiBlkIo;
DEBUG ((EFI_D_INFO, "Atatpi Device Count is %d\n", AtapiBlkIoDev->DeviceCount));
if (AtapiBlkIoDev->DeviceCount != 0) {
Status = PeiServicesInstallPpi (&AtapiBlkIoDev->PpiDescriptor);
if (EFI_ERROR (Status)) {
return EFI_OUT_OF_RESOURCES;
}
}
return EFI_SUCCESS;
}
/**
Gets the count of block I/O devices that one specific block driver detects.
This function is used for getting the count of block I/O devices that one
specific block driver detects. To the PEI ATAPI driver, it returns the number
of all the detected ATAPI devices it detects during the enumeration process.
To the PEI legacy floppy driver, it returns the number of all the legacy
devices it finds during its enumeration process. If no device is detected,
then the function will return zero.
@param[in] PeiServices General-purpose services that are available
to every PEIM.
@param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
instance.
@param[out] NumberBlockDevices The number of block I/O devices discovered.
@retval EFI_SUCCESS Operation performed successfully.
**/
EFI_STATUS
EFIAPI
AtapiGetNumberOfBlockDevices (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
OUT UINTN *NumberBlockDevices
)
{
ATAPI_BLK_IO_DEV *AtapiBlkIoDev;
AtapiBlkIoDev = NULL;
AtapiBlkIoDev = PEI_RECOVERY_ATAPI_FROM_BLKIO_THIS (This);
*NumberBlockDevices = AtapiBlkIoDev->DeviceCount;
return EFI_SUCCESS;
}
/**
Gets a block device's media information.
This function will provide the caller with the specified block device's media
information. If the media changes, calling this function will update the media
information accordingly.
@param[in] PeiServices General-purpose services that are available to every
PEIM
@param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
@param[in] DeviceIndex Specifies the block device to which the function wants
to talk. Because the driver that implements Block I/O
PPIs will manage multiple block devices, the PPIs that
want to talk to a single device must specify the
device index that was assigned during the enumeration
process. This index is a number from one to
NumberBlockDevices.
@param[out] MediaInfo The media information of the specified block media.
The caller is responsible for the ownership of this
data structure.
@retval EFI_SUCCESS Media information about the specified block device
was obtained successfully.
@retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
error.
@retval Others Other failure occurs.
**/
EFI_STATUS
EFIAPI
AtapiGetBlockDeviceMediaInfo (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
IN UINTN DeviceIndex,
OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo
)
{
UINTN DeviceCount;
ATAPI_BLK_IO_DEV *AtapiBlkIoDev;
EFI_STATUS Status;
UINTN Index;
AtapiBlkIoDev = NULL;
if (This == NULL || MediaInfo == NULL) {
return EFI_INVALID_PARAMETER;
}
AtapiBlkIoDev = PEI_RECOVERY_ATAPI_FROM_BLKIO_THIS (This);
DeviceCount = AtapiBlkIoDev->DeviceCount;
//
// DeviceIndex is a value from 1 to NumberBlockDevices.
//
if ((DeviceIndex < 1) || (DeviceIndex > DeviceCount) || (DeviceIndex > MAX_IDE_DEVICES)) {
return EFI_INVALID_PARAMETER;
}
Index = DeviceIndex - 1;
//
// probe media and retrieve latest media information
//
DEBUG ((EFI_D_INFO, "Atatpi GetInfo DevicePosition is %d\n", AtapiBlkIoDev->DeviceInfo[Index].DevicePosition));
DEBUG ((EFI_D_INFO, "Atatpi GetInfo DeviceType is %d\n", AtapiBlkIoDev->DeviceInfo[Index].MediaInfo.DeviceType));
DEBUG ((EFI_D_INFO, "Atatpi GetInfo MediaPresent is %d\n", AtapiBlkIoDev->DeviceInfo[Index].MediaInfo.MediaPresent));
DEBUG ((EFI_D_INFO, "Atatpi GetInfo BlockSize is 0x%x\n", AtapiBlkIoDev->DeviceInfo[Index].MediaInfo.BlockSize));
DEBUG ((EFI_D_INFO, "Atatpi GetInfo LastBlock is 0x%x\n", AtapiBlkIoDev->DeviceInfo[Index].MediaInfo.LastBlock));
Status = DetectMedia (
AtapiBlkIoDev,
AtapiBlkIoDev->DeviceInfo[Index].DevicePosition,
&AtapiBlkIoDev->DeviceInfo[Index].MediaInfo
);
if (Status != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
DEBUG ((EFI_D_INFO, "Atatpi GetInfo DevicePosition is %d\n", AtapiBlkIoDev->DeviceInfo[Index].DevicePosition));
DEBUG ((EFI_D_INFO, "Atatpi GetInfo DeviceType is %d\n", AtapiBlkIoDev->DeviceInfo[Index].MediaInfo.DeviceType));
DEBUG ((EFI_D_INFO, "Atatpi GetInfo MediaPresent is %d\n", AtapiBlkIoDev->DeviceInfo[Index].MediaInfo.MediaPresent));
DEBUG ((EFI_D_INFO, "Atatpi GetInfo BlockSize is 0x%x\n", AtapiBlkIoDev->DeviceInfo[Index].MediaInfo.BlockSize));
DEBUG ((EFI_D_INFO, "Atatpi GetInfo LastBlock is 0x%x\n", AtapiBlkIoDev->DeviceInfo[Index].MediaInfo.LastBlock));
//
// Get media info from AtapiBlkIoDev
//
CopyMem (MediaInfo, &AtapiBlkIoDev->DeviceInfo[Index].MediaInfo, sizeof(EFI_PEI_BLOCK_IO_MEDIA));
return EFI_SUCCESS;
}
/**
Reads the requested number of blocks from the specified block device.
The function reads the requested number of blocks from the device. All the
blocks are read, or an error is returned. If there is no media in the device,
the function returns EFI_NO_MEDIA.
@param[in] PeiServices General-purpose services that are available to
every PEIM.
@param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
@param[in] DeviceIndex Specifies the block device to which the function wants
to talk. Because the driver that implements Block I/O
PPIs will manage multiple block devices, the PPIs that
want to talk to a single device must specify the device
index that was assigned during the enumeration process.
This index is a number from one to NumberBlockDevices.
@param[in] StartLBA The starting logical block address (LBA) to read from
on the device
@param[in] BufferSize The size of the Buffer in bytes. This number must be
a multiple of the intrinsic block size of the device.
@param[out] Buffer A pointer to the destination buffer for the data.
The caller is responsible for the ownership of the
buffer.
@retval EFI_SUCCESS The data was read correctly from the device.
@retval EFI_DEVICE_ERROR The device reported an error while attempting
to perform the read operation.
@retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
valid, or the buffer is not properly aligned.
@retval EFI_NO_MEDIA There is no media in the device.
@retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
the intrinsic block size of the device.
**/
EFI_STATUS
EFIAPI
AtapiReadBlocks (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
IN UINTN DeviceIndex,
IN EFI_PEI_LBA StartLBA,
IN UINTN BufferSize,
OUT VOID *Buffer
)
{
EFI_PEI_BLOCK_IO_MEDIA MediaInfo;
EFI_STATUS Status;
UINTN NumberOfBlocks;
UINTN BlockSize;
ATAPI_BLK_IO_DEV *AtapiBlkIoDev;
AtapiBlkIoDev = NULL;
if (This == NULL) {
return EFI_INVALID_PARAMETER;
}
AtapiBlkIoDev = PEI_RECOVERY_ATAPI_FROM_BLKIO_THIS (This);
if (Buffer == NULL) {
return EFI_INVALID_PARAMETER;
}
if (BufferSize == 0) {
return EFI_SUCCESS;
}
Status = AtapiGetBlockDeviceMediaInfo (
PeiServices,
This,
DeviceIndex,
&MediaInfo
);
if (Status != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
if (!MediaInfo.MediaPresent) {
return EFI_NO_MEDIA;
}
BlockSize = MediaInfo.BlockSize;
if (BufferSize % BlockSize != 0) {
return EFI_BAD_BUFFER_SIZE;
}
NumberOfBlocks = BufferSize / BlockSize;
if ((StartLBA + NumberOfBlocks - 1) > AtapiBlkIoDev->DeviceInfo[DeviceIndex - 1].MediaInfo.LastBlock) {
return EFI_INVALID_PARAMETER;
}
Status = ReadSectors (
AtapiBlkIoDev,
AtapiBlkIoDev->DeviceInfo[DeviceIndex - 1].DevicePosition,
Buffer,
StartLBA,
NumberOfBlocks,
BlockSize
);
if (EFI_ERROR (Status)) {
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Enumerate Atapi devices.
This function is used to enumerate Atatpi device in Ide channel.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device
**/
VOID
AtapiEnumerateDevices (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev
)
{
UINT8 Index1;
UINT8 Index2;
UINTN DevicePosition;
EFI_PEI_BLOCK_IO_MEDIA MediaInfo;
EFI_STATUS Status;
UINTN DeviceCount;
UINT16 CommandBlockBaseAddr;
UINT16 ControlBlockBaseAddr;
UINT32 IdeEnabledNumber;
IDE_REGS_BASE_ADDR IdeRegsBaseAddr[MAX_IDE_CHANNELS];
DeviceCount = 0;
DevicePosition = 0;
//
// Scan IDE bus for ATAPI devices
//
//
// Enable Sata and IDE controller.
//
AtapiBlkIoDev->AtaControllerPpi->EnableAtaChannel (
(EFI_PEI_SERVICES **) GetPeiServicesTablePointer(),
AtapiBlkIoDev->AtaControllerPpi,
PEI_ICH_IDE_PRIMARY | PEI_ICH_IDE_SECONDARY
);
//
// Allow SATA Devices to spin-up. This is needed if
// SEC and PEI phase is too short, for example Release Build.
//
DEBUG ((EFI_D_INFO, "Delay for %d seconds for SATA devices to spin-up\n", PcdGet16 (PcdSataSpinUpDelayInSecForRecoveryPath)));
MicroSecondDelay (PcdGet16 (PcdSataSpinUpDelayInSecForRecoveryPath) * 1000 * 1000); //
//
// Get four channels (primary or secondary Pata, Sata Channel) Command and Control Regs Base address.
//
IdeEnabledNumber = AtapiBlkIoDev->AtaControllerPpi->GetIdeRegsBaseAddr (
(EFI_PEI_SERVICES **) GetPeiServicesTablePointer(),
AtapiBlkIoDev->AtaControllerPpi,
IdeRegsBaseAddr
);
//
// Using Command and Control Regs Base Address to fill other registers.
//
for (Index1 = 0; Index1 < IdeEnabledNumber; Index1 ++) {
CommandBlockBaseAddr = IdeRegsBaseAddr[Index1].CommandBlockBaseAddr;
AtapiBlkIoDev->IdeIoPortReg[Index1].Data = CommandBlockBaseAddr;
AtapiBlkIoDev->IdeIoPortReg[Index1].Reg1.Feature = (UINT16) (CommandBlockBaseAddr + 0x1);
AtapiBlkIoDev->IdeIoPortReg[Index1].SectorCount = (UINT16) (CommandBlockBaseAddr + 0x2);
AtapiBlkIoDev->IdeIoPortReg[Index1].SectorNumber = (UINT16) (CommandBlockBaseAddr + 0x3);
AtapiBlkIoDev->IdeIoPortReg[Index1].CylinderLsb = (UINT16) (CommandBlockBaseAddr + 0x4);
AtapiBlkIoDev->IdeIoPortReg[Index1].CylinderMsb = (UINT16) (CommandBlockBaseAddr + 0x5);
AtapiBlkIoDev->IdeIoPortReg[Index1].Head = (UINT16) (CommandBlockBaseAddr + 0x6);
AtapiBlkIoDev->IdeIoPortReg[Index1].Reg.Command = (UINT16) (CommandBlockBaseAddr + 0x7);
ControlBlockBaseAddr = IdeRegsBaseAddr[Index1].ControlBlockBaseAddr;
AtapiBlkIoDev->IdeIoPortReg[Index1].Alt.DeviceControl = ControlBlockBaseAddr;
AtapiBlkIoDev->IdeIoPortReg[Index1].DriveAddress = (UINT16) (ControlBlockBaseAddr + 0x1);
//
// Scan IDE bus for ATAPI devices IDE or Sata device
//
for (Index2 = IdeMaster; Index2 < IdeMaxDevice; Index2++) {
//
// Pata & Sata, Primary & Secondary channel, Master & Slave device
//
DevicePosition = (UINTN) (Index1 * 2 + Index2);
if (DiscoverAtapiDevice (AtapiBlkIoDev, DevicePosition, &MediaInfo)) {
//
// ATAPI Device at DevicePosition is found.
//
AtapiBlkIoDev->DeviceInfo[DeviceCount].DevicePosition = DevicePosition;
//
// Retrieve Media Info
//
Status = DetectMedia (AtapiBlkIoDev, DevicePosition, &MediaInfo);
CopyMem (&(AtapiBlkIoDev->DeviceInfo[DeviceCount].MediaInfo), &MediaInfo, sizeof (MediaInfo));
DEBUG ((EFI_D_INFO, "Atatpi Device Position is %d\n", DevicePosition));
DEBUG ((EFI_D_INFO, "Atatpi DeviceType is %d\n", MediaInfo.DeviceType));
DEBUG ((EFI_D_INFO, "Atatpi MediaPresent is %d\n", MediaInfo.MediaPresent));
DEBUG ((EFI_D_INFO, "Atatpi BlockSize is 0x%x\n", MediaInfo.BlockSize));
if (EFI_ERROR (Status)) {
AtapiBlkIoDev->DeviceInfo[DeviceCount].MediaInfo.MediaPresent = FALSE;
AtapiBlkIoDev->DeviceInfo[DeviceCount].MediaInfo.LastBlock = 0;
}
DeviceCount += 1;
}
}
}
AtapiBlkIoDev->DeviceCount = DeviceCount;
}
/**
Detect Atapi devices.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@param[out] MediaInfo The media information of the specified block media.
@retval TRUE Atapi device exists in specified position.
@retval FALSE Atapi device does not exist in specified position.
**/
BOOLEAN
DiscoverAtapiDevice (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition,
OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo
)
{
EFI_STATUS Status;
if (!DetectIDEController (AtapiBlkIoDev, DevicePosition)) {
return FALSE;
}
//
// test if it is an ATAPI device (only supported device)
//
if (ATAPIIdentify (AtapiBlkIoDev, DevicePosition) == EFI_SUCCESS) {
Status = Inquiry (AtapiBlkIoDev, DevicePosition, MediaInfo);
if (!EFI_ERROR (Status)) {
return TRUE;
}
}
return FALSE;
}
/**
Check power mode of Atapi devices.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@param[in] AtaCommand The Ata Command passed in.
@retval EFI_SUCCESS The Atapi device support power mode.
@retval EFI_NOT_FOUND The Atapi device not found.
@retval EFI_TIMEOUT Atapi command transaction is time out.
@retval EFI_ABORTED Atapi command abort.
**/
EFI_STATUS
CheckPowerMode (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition,
IN UINT8 AtaCommand
)
{
UINT8 Channel;
UINT8 Device;
UINT16 StatusRegister;
UINT16 HeadRegister;
UINT16 CommandRegister;
UINT16 ErrorRegister;
UINT16 SectorCountRegister;
EFI_STATUS Status;
UINT8 StatusValue;
UINT8 ErrorValue;
UINT8 SectorCountValue;
Channel = (UINT8) (DevicePosition / 2);
Device = (UINT8) (DevicePosition % 2);
ASSERT (Channel < MAX_IDE_CHANNELS);
StatusRegister = AtapiBlkIoDev->IdeIoPortReg[Channel].Reg.Status;
HeadRegister = AtapiBlkIoDev->IdeIoPortReg[Channel].Head;
CommandRegister = AtapiBlkIoDev->IdeIoPortReg[Channel].Reg.Command;
ErrorRegister = AtapiBlkIoDev->IdeIoPortReg[Channel].Reg1.Error;
SectorCountRegister = AtapiBlkIoDev->IdeIoPortReg[Channel].SectorCount;
//
// select device
//
IoWrite8 (HeadRegister, (UINT8) ((Device << 4) | 0xe0));
//
// refresh the SectorCount register
//
SectorCountValue = 0x55;
IoWrite8 (SectorCountRegister, SectorCountValue);
//
// select device
//
IoWrite8 (HeadRegister, (UINT8) ((Device << 4) | 0xe0));
Status = DRDYReady (AtapiBlkIoDev, &(AtapiBlkIoDev->IdeIoPortReg[Channel]), 100);
//
// select device
//
IoWrite8 (HeadRegister, (UINT8) ((Device << 4) | 0xe0));
//
// send 'check power' commandd via Command Register
//
IoWrite8 (CommandRegister, AtaCommand);
Status = WaitForBSYClear (AtapiBlkIoDev, &(AtapiBlkIoDev->IdeIoPortReg[Channel]), 3000);
if (EFI_ERROR (Status)) {
return EFI_TIMEOUT;
}
StatusValue = IoRead8 (StatusRegister);
//
// command returned status is DRDY, indicating device supports the command,
// so device is present.
//
if ((StatusValue & ATA_STSREG_DRDY) == ATA_STSREG_DRDY) {
return EFI_SUCCESS;
}
SectorCountValue = IoRead8 (SectorCountRegister);
//
// command returned status is ERR & ABRT_ERR, indicating device does not support
// the command, so device is present.
//
if ((StatusValue & ATA_STSREG_ERR) == ATA_STSREG_ERR) {
ErrorValue = IoRead8 (ErrorRegister);
if ((ErrorValue & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) {
return EFI_ABORTED;
} else {
//
// According to spec, no other error code is valid
//
return EFI_NOT_FOUND;
}
}
if ((SectorCountValue == 0x00) || (SectorCountValue == 0x80) || (SectorCountValue == 0xff)) {
//
// Write SectorCount 0x55 but return valid state value. Maybe no device
// exists or some slow kind of ATAPI device exists.
//
IoWrite8 (HeadRegister, (UINT8) ((Device << 4) | 0xe0));
//
// write 0x55 and 0xaa to SectorCounter register,
// if the data could be written into the register,
// indicating the device is present, otherwise the device is not present.
//
SectorCountValue = 0x55;
IoWrite8 (SectorCountRegister, SectorCountValue);
MicroSecondDelay (10000);
SectorCountValue = IoRead8 (SectorCountRegister);
if (SectorCountValue != 0x55) {
return EFI_NOT_FOUND;
}
//
// Send a "ATAPI TEST UNIT READY" command ... slow but accurate
//
Status = TestUnitReady (AtapiBlkIoDev, DevicePosition);
return Status;
}
return EFI_NOT_FOUND;
}
/**
Detect if an IDE controller exists in specified position.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@retval TRUE The Atapi device exists.
@retval FALSE The Atapi device does not present.
**/
BOOLEAN
DetectIDEController (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition
)
{
UINT8 Channel;
EFI_STATUS Status;
UINT8 AtaCommand;
Channel = (UINT8) (DevicePosition / 2);
ASSERT (Channel < MAX_IDE_CHANNELS);
//
// Wait 31 seconds for BSY clear
//
Status = WaitForBSYClear (AtapiBlkIoDev, &(AtapiBlkIoDev->IdeIoPortReg[Channel]), 31000);
if (EFI_ERROR (Status)) {
return FALSE;
}
//
// Send 'check power' command for IDE device
//
AtaCommand = 0xE5;
Status = CheckPowerMode (AtapiBlkIoDev, DevicePosition, AtaCommand);
if ((Status == EFI_ABORTED) || (Status == EFI_SUCCESS)) {
return TRUE;
}
return FALSE;
}
/**
Wait specified time interval to poll for BSY bit clear in the Status Register.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] IdeIoRegisters A pointer to IDE IO registers.
@param[in] TimeoutInMilliSeconds Time specified in milliseconds.
@retval EFI_SUCCESS BSY bit is cleared in the specified time interval.
@retval EFI_TIMEOUT BSY bit is not cleared in the specified time interval.
**/
EFI_STATUS
WaitForBSYClear (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN IDE_BASE_REGISTERS *IdeIoRegisters,
IN UINTN TimeoutInMilliSeconds
)
{
UINTN Delay;
UINT16 StatusRegister;
UINT8 StatusValue;
StatusValue = 0;
StatusRegister = IdeIoRegisters->Reg.Status;
Delay = ((TimeoutInMilliSeconds * STALL_1_MILLI_SECOND) / 250) + 1;
do {
StatusValue = IoRead8 (StatusRegister);
if ((StatusValue & ATA_STSREG_BSY) == 0x00) {
break;
}
MicroSecondDelay (250);
Delay--;
} while (Delay != 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
Wait specified time interval to poll for DRDY bit set in the Status register.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] IdeIoRegisters A pointer to IDE IO registers.
@param[in] TimeoutInMilliSeconds Time specified in milliseconds.
@retval EFI_SUCCESS DRDY bit is set in the specified time interval.
@retval EFI_TIMEOUT DRDY bit is not set in the specified time interval.
**/
EFI_STATUS
DRDYReady (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN IDE_BASE_REGISTERS *IdeIoRegisters,
IN UINTN TimeoutInMilliSeconds
)
{
UINTN Delay;
UINT16 StatusRegister;
UINT8 StatusValue;
UINT8 ErrValue;
StatusValue = 0;
StatusRegister = IdeIoRegisters->Reg.Status;
Delay = ((TimeoutInMilliSeconds * STALL_1_MILLI_SECOND) / 250) + 1;
do {
StatusValue = IoRead8 (StatusRegister);
//
// BSY == 0 , DRDY == 1
//
if ((StatusValue & (ATA_STSREG_DRDY | ATA_STSREG_BSY)) == ATA_STSREG_DRDY) {
break;
}
if ((StatusValue & (ATA_STSREG_ERR | ATA_STSREG_BSY)) == ATA_STSREG_ERR) {
ErrValue = IoRead8 (IdeIoRegisters->Reg1.Error);
if ((ErrValue & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) {
return EFI_ABORTED;
}
}
MicroSecondDelay (250);
Delay--;
} while (Delay != 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
Wait specified time interval to poll for DRQ bit clear in the Status Register.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] IdeIoRegisters A pointer to IDE IO registers.
@param[in] TimeoutInMilliSeconds Time specified in milliseconds.
@retval EFI_SUCCESS DRQ bit is cleared in the specified time interval.
@retval EFI_TIMEOUT DRQ bit is not cleared in the specified time interval.
**/
EFI_STATUS
DRQClear (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN IDE_BASE_REGISTERS *IdeIoRegisters,
IN UINTN TimeoutInMilliSeconds
)
{
UINTN Delay;
UINT16 StatusRegister;
UINT8 StatusValue;
UINT8 ErrValue;
StatusValue = 0;
StatusRegister = IdeIoRegisters->Reg.Status;
Delay = ((TimeoutInMilliSeconds * STALL_1_MILLI_SECOND) / 250) + 1;
do {
StatusValue = IoRead8 (StatusRegister);
//
// wait for BSY == 0 and DRQ == 0
//
if ((StatusValue & (ATA_STSREG_DRQ | ATA_STSREG_BSY)) == 0) {
break;
}
if ((StatusValue & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) {
ErrValue = IoRead8 (IdeIoRegisters->Reg1.Error);
if ((ErrValue & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) {
return EFI_ABORTED;
}
}
MicroSecondDelay (250);
Delay--;
} while (Delay != 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
Wait specified time interval to poll for DRQ bit clear in the Alternate Status Register.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] IdeIoRegisters A pointer to IDE IO registers.
@param[in] TimeoutInMilliSeconds Time specified in milliseconds.
@retval EFI_SUCCESS DRQ bit is cleared in the specified time interval.
@retval EFI_TIMEOUT DRQ bit is not cleared in the specified time interval.
**/
EFI_STATUS
DRQClear2 (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN IDE_BASE_REGISTERS *IdeIoRegisters,
IN UINTN TimeoutInMilliSeconds
)
{
UINTN Delay;
UINT16 AltStatusRegister;
UINT8 AltStatusValue;
UINT8 ErrValue;
AltStatusValue = 0;
AltStatusRegister = IdeIoRegisters->Alt.AltStatus;
Delay = ((TimeoutInMilliSeconds * STALL_1_MILLI_SECOND) / 250) + 1;
do {
AltStatusValue = IoRead8 (AltStatusRegister);
//
// wait for BSY == 0 and DRQ == 0
//
if ((AltStatusValue & (ATA_STSREG_DRQ | ATA_STSREG_BSY)) == 0) {
break;
}
if ((AltStatusValue & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) {
ErrValue = IoRead8 (IdeIoRegisters->Reg1.Error);
if ((ErrValue & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) {
return EFI_ABORTED;
}
}
MicroSecondDelay (250);
Delay--;
} while (Delay != 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
Wait specified time interval to poll for DRQ bit set in the Status Register.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] IdeIoRegisters A pointer to IDE IO registers.
@param[in] TimeoutInMilliSeconds Time specified in milliseconds.
@retval EFI_SUCCESS DRQ bit is set in the specified time interval.
@retval EFI_TIMEOUT DRQ bit is not set in the specified time interval.
@retval EFI_ABORTED Operation Aborted.
**/
EFI_STATUS
DRQReady (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN IDE_BASE_REGISTERS *IdeIoRegisters,
IN UINTN TimeoutInMilliSeconds
)
{
UINTN Delay;
UINT16 StatusRegister;
UINT8 StatusValue;
UINT8 ErrValue;
StatusValue = 0;
ErrValue = 0;
StatusRegister = IdeIoRegisters->Reg.Status;
Delay = ((TimeoutInMilliSeconds * STALL_1_MILLI_SECOND) / 250) + 1;
do {
//
// read Status Register will clear interrupt
//
StatusValue = IoRead8 (StatusRegister);
//
// BSY==0,DRQ==1
//
if ((StatusValue & (ATA_STSREG_BSY | ATA_STSREG_DRQ)) == ATA_STSREG_DRQ) {
break;
}
if ((StatusValue & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) {
ErrValue = IoRead8 (IdeIoRegisters->Reg1.Error);
if ((ErrValue & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) {
return EFI_ABORTED;
}
}
MicroSecondDelay (250);
Delay--;
} while (Delay != 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
Wait specified time interval to poll for DRQ bit set in the Alternate Status Register.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] IdeIoRegisters A pointer to IDE IO registers.
@param[in] TimeoutInMilliSeconds Time specified in milliseconds.
@retval EFI_SUCCESS DRQ bit is set in the specified time interval.
@retval EFI_TIMEOUT DRQ bit is not set in the specified time interval.
@retval EFI_ABORTED Operation Aborted.
**/
EFI_STATUS
DRQReady2 (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN IDE_BASE_REGISTERS *IdeIoRegisters,
IN UINTN TimeoutInMilliSeconds
)
{
UINTN Delay;
UINT16 AltStatusRegister;
UINT8 AltStatusValue;
UINT8 ErrValue;
AltStatusValue = 0;
AltStatusRegister = IdeIoRegisters->Alt.AltStatus;
Delay = ((TimeoutInMilliSeconds * STALL_1_MILLI_SECOND) / 250) + 1;
do {
AltStatusValue = IoRead8 (AltStatusRegister);
//
// BSY==0,DRQ==1
//
if ((AltStatusValue & (ATA_STSREG_BSY | ATA_STSREG_DRQ)) == ATA_STSREG_DRQ) {
break;
}
if ((AltStatusValue & (ATA_STSREG_BSY | ATA_STSREG_ERR)) == ATA_STSREG_ERR) {
ErrValue = IoRead8 (IdeIoRegisters->Reg1.Error);
if ((ErrValue & ATA_ERRREG_ABRT) == ATA_ERRREG_ABRT) {
return EFI_ABORTED;
}
}
MicroSecondDelay (250);
Delay--;
} while (Delay != 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
Check if there is an error in Status Register.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] StatusReg The address to IDE IO registers.
@retval EFI_SUCCESS Operation success.
@retval EFI_DEVICE_ERROR Device error.
**/
EFI_STATUS
CheckErrorStatus (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINT16 StatusReg
)
{
UINT8 StatusValue;
StatusValue = IoRead8 (StatusReg);
if ((StatusValue & (ATA_STSREG_ERR | ATA_STSREG_DWF | ATA_STSREG_CORR)) == 0) {
return EFI_SUCCESS;
}
return EFI_DEVICE_ERROR;
}
/**
Idendify Atapi devices.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@retval EFI_SUCCESS Identify successfully.
@retval EFI_DEVICE_ERROR Device cannot be identified successfully.
**/
EFI_STATUS
ATAPIIdentify (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition
)
{
ATAPI_IDENTIFY_DATA AtapiIdentifyData;
UINT8 Channel;
UINT8 Device;
UINT16 StatusReg;
UINT16 HeadReg;
UINT16 CommandReg;
UINT16 DataReg;
UINT16 SectorCountReg;
UINT16 SectorNumberReg;
UINT16 CylinderLsbReg;
UINT16 CylinderMsbReg;
UINT32 WordCount;
UINT32 Increment;
UINT32 Index;
UINT32 ByteCount;
UINT16 *Buffer16;
EFI_STATUS Status;
ByteCount = sizeof (AtapiIdentifyData);
Buffer16 = (UINT16 *) &AtapiIdentifyData;
Channel = (UINT8) (DevicePosition / 2);
Device = (UINT8) (DevicePosition % 2);
ASSERT (Channel < MAX_IDE_CHANNELS);
StatusReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Reg.Status;
HeadReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Head;
CommandReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Reg.Command;
DataReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Data;
SectorCountReg = AtapiBlkIoDev->IdeIoPortReg[Channel].SectorCount;
SectorNumberReg = AtapiBlkIoDev->IdeIoPortReg[Channel].SectorNumber;
CylinderLsbReg = AtapiBlkIoDev->IdeIoPortReg[Channel].CylinderLsb;
CylinderMsbReg = AtapiBlkIoDev->IdeIoPortReg[Channel].CylinderMsb;
//
// Send ATAPI Identify Command to get IDENTIFY data.
//
if (WaitForBSYClear (
AtapiBlkIoDev,
&(AtapiBlkIoDev->IdeIoPortReg[Channel]),
ATATIMEOUT
) != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
//
// select device via Head/Device register.
// Before write Head/Device register, BSY and DRQ must be 0.
//
if (DRQClear2 (AtapiBlkIoDev, &(AtapiBlkIoDev->IdeIoPortReg[Channel]), ATATIMEOUT) != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
//
// e0:1110,0000-- bit7 and bit5 are reserved bits.
// bit6 set means LBA mode
//
IoWrite8 (HeadReg, (UINT8) ((Device << 4) | 0xe0));
//
// set all the command parameters
// Before write to all the following registers, BSY and DRQ must be 0.
//
if (DRQClear2 (
AtapiBlkIoDev,
&(AtapiBlkIoDev->IdeIoPortReg[Channel]),
ATATIMEOUT
) != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
IoWrite8 (SectorCountReg, 0);
IoWrite8 (SectorNumberReg, 0);
IoWrite8 (CylinderLsbReg, 0);
IoWrite8 (CylinderMsbReg, 0);
//
// send command via Command Register
//
IoWrite8 (CommandReg, ATA_CMD_IDENTIFY_DEVICE);
//
// According to PIO data in protocol, host can perform a series of reads to the
// data register after each time device set DRQ ready;
// The data size of "a series of read" is command specific.
// For most ATA command, data size received from device will not exceed 1 sector,
// hense the data size for "a series of read" can be the whole data size of one command request.
// For ATA command such as Read Sector command, whole data size of one ATA command request is often larger
// than 1 sector, according to the Read Sector command, the data size of "a series of read" is exactly
// 1 sector.
// Here for simplification reason, we specify the data size for "a series of read" to
// 1 sector (256 words) if whole data size of one ATA commmand request is larger than 256 words.
//
Increment = 256;
//
// 256 words
//
WordCount = 0;
//
// WordCount is used to record bytes of currently transfered data
//
while (WordCount < ByteCount / 2) {
//
// Poll DRQ bit set, data transfer can be performed only when DRQ is ready.
//
Status = DRQReady2 (AtapiBlkIoDev, &(AtapiBlkIoDev->IdeIoPortReg[Channel]), ATATIMEOUT);
if (Status != EFI_SUCCESS) {
return Status;
}
if (CheckErrorStatus (AtapiBlkIoDev, StatusReg) != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
//
// Get the byte count for one series of read
//
if ((WordCount + Increment) > ByteCount / 2) {
Increment = ByteCount / 2 - WordCount;
}
//
// perform a series of read without check DRQ ready
//
for (Index = 0; Index < Increment; Index++) {
*Buffer16++ = IoRead16 (DataReg);
}
WordCount += Increment;
}
//
// while
//
if (DRQClear (
AtapiBlkIoDev,
&(AtapiBlkIoDev->IdeIoPortReg[Channel]),
ATATIMEOUT
) != EFI_SUCCESS) {
return CheckErrorStatus (AtapiBlkIoDev, StatusReg);
}
return EFI_SUCCESS;
}
/**
Sends out ATAPI Test Unit Ready Packet Command to the specified device
to find out whether device is accessible.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@retval EFI_SUCCESS TestUnit command executed successfully.
@retval EFI_DEVICE_ERROR Device cannot be executed TestUnit command successfully.
**/
EFI_STATUS
TestUnitReady (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition
)
{
ATAPI_PACKET_COMMAND Packet;
EFI_STATUS Status;
//
// fill command packet
//
ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
Packet.TestUnitReady.opcode = ATA_CMD_TEST_UNIT_READY;
//
// send command packet
//
Status = AtapiPacketCommandIn (AtapiBlkIoDev, DevicePosition, &Packet, NULL, 0, ATAPITIMEOUT);
return Status;
}
/**
Send out ATAPI commands conforms to the Packet Command with PIO Data In Protocol.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@param[in] Packet A pointer to ATAPI command packet.
@param[in] Buffer Buffer to contain requested transfer data from device.
@param[in] ByteCount Requested transfer data length.
@param[in] TimeoutInMilliSeconds Time out value, in unit of milliseconds.
@retval EFI_SUCCESS Command executed successfully.
@retval EFI_DEVICE_ERROR Device cannot be executed command successfully.
**/
EFI_STATUS
AtapiPacketCommandIn (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition,
IN ATAPI_PACKET_COMMAND *Packet,
IN UINT16 *Buffer,
IN UINT32 ByteCount,
IN UINTN TimeoutInMilliSeconds
)
{
UINT8 Channel;
UINT8 Device;
UINT16 StatusReg;
UINT16 HeadReg;
UINT16 CommandReg;
UINT16 FeatureReg;
UINT16 CylinderLsbReg;
UINT16 CylinderMsbReg;
UINT16 DeviceControlReg;
UINT16 DataReg;
EFI_STATUS Status;
UINT32 Count;
UINT16 *CommandIndex;
UINT16 *PtrBuffer;
UINT32 Index;
UINT8 StatusValue;
UINT32 WordCount;
//
// required transfer data in word unit.
//
UINT32 RequiredWordCount;
//
// actual transfer data in word unit.
//
UINT32 ActualWordCount;
Channel = (UINT8) (DevicePosition / 2);
Device = (UINT8) (DevicePosition % 2);
ASSERT (Channel < MAX_IDE_CHANNELS);
StatusReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Reg.Status;
HeadReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Head;
CommandReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Reg.Command;
FeatureReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Reg1.Feature;
CylinderLsbReg = AtapiBlkIoDev->IdeIoPortReg[Channel].CylinderLsb;
CylinderMsbReg = AtapiBlkIoDev->IdeIoPortReg[Channel].CylinderMsb;
DeviceControlReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Alt.DeviceControl;
DataReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Data;
//
// Set all the command parameters by fill related registers.
// Before write to all the following registers, BSY and DRQ must be 0.
//
if (DRQClear2 (
AtapiBlkIoDev,
&(AtapiBlkIoDev->IdeIoPortReg[Channel]),
ATATIMEOUT
) != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
//
// Select device via Device/Head Register.
// DEFAULT_CMD: 0xa0 (1010,0000)
//
IoWrite8 (HeadReg, (UINT8) ((Device << 4) | ATA_DEFAULT_CMD));
//
// No OVL; No DMA
//
IoWrite8 (FeatureReg, 0x00);
//
// set the transfersize to MAX_ATAPI_BYTE_COUNT to let the device
// determine how many data should be transfered.
//
IoWrite8 (CylinderLsbReg, (UINT8) (ATAPI_MAX_BYTE_COUNT & 0x00ff));
IoWrite8 (CylinderMsbReg, (UINT8) (ATAPI_MAX_BYTE_COUNT >> 8));
//
// DEFAULT_CTL:0x0a (0000,1010)
// Disable interrupt
//
IoWrite8 (DeviceControlReg, ATA_DEFAULT_CTL);
//
// Send Packet command to inform device
// that the following data bytes are command packet.
//
IoWrite8 (CommandReg, ATA_CMD_PACKET);
Status = DRQReady (AtapiBlkIoDev, &(AtapiBlkIoDev->IdeIoPortReg[Channel]), TimeoutInMilliSeconds);
if (Status != EFI_SUCCESS) {
return Status;
}
//
// Send out command packet
//
CommandIndex = Packet->Data16;
for (Count = 0; Count < 6; Count++, CommandIndex++) {
IoWrite16 (DataReg, *CommandIndex);
MicroSecondDelay (10);
}
StatusValue = IoRead8 (StatusReg);
if ((StatusValue & ATA_STSREG_ERR) == ATA_STSREG_ERR) {
//
// Trouble! Something's wrong here... Wait some time and return. 3 second is
// supposed to be long enough for a device reset latency or error recovery
//
MicroSecondDelay (3000000);
return EFI_DEVICE_ERROR;
}
if (Buffer == NULL || ByteCount == 0) {
return EFI_SUCCESS;
}
//
// call PioReadWriteData() function to get
// requested transfer data form device.
//
PtrBuffer = Buffer;
RequiredWordCount = ByteCount / 2;
//
// ActuralWordCount means the word count of data really transfered.
//
ActualWordCount = 0;
Status = EFI_SUCCESS;
while ((Status == EFI_SUCCESS) && (ActualWordCount < RequiredWordCount)) {
//
// before each data transfer stream, the host should poll DRQ bit ready,
// which informs device is ready to transfer data.
//
if (DRQReady2 (
AtapiBlkIoDev,
&(AtapiBlkIoDev->IdeIoPortReg[Channel]),
TimeoutInMilliSeconds
) != EFI_SUCCESS) {
return CheckErrorStatus (AtapiBlkIoDev, StatusReg);
}
//
// read Status Register will clear interrupt
//
StatusValue = IoRead8 (StatusReg);
//
// get current data transfer size from Cylinder Registers.
//
WordCount = IoRead8 (CylinderMsbReg) << 8;
WordCount = WordCount | IoRead8 (CylinderLsbReg);
WordCount = WordCount & 0xffff;
WordCount /= 2;
//
// perform a series data In/Out.
//
for (Index = 0; (Index < WordCount) && (ActualWordCount < RequiredWordCount); Index++, ActualWordCount++) {
*PtrBuffer = IoRead16 (DataReg);
PtrBuffer++;
}
if (((ATAPI_REQUEST_SENSE_CMD *) Packet)->opcode == ATA_CMD_REQUEST_SENSE && ActualWordCount >= 4) {
RequiredWordCount = MIN (
RequiredWordCount,
(UINT32) (4 + (((ATAPI_REQUEST_SENSE_DATA *) Buffer)->addnl_sense_length / 2))
);
}
}
//
// After data transfer is completed, normally, DRQ bit should clear.
//
Status = DRQClear2 (AtapiBlkIoDev, &(AtapiBlkIoDev->IdeIoPortReg[Channel]), TimeoutInMilliSeconds);
if (Status != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
//
// read status register to check whether error happens.
//
Status = CheckErrorStatus (AtapiBlkIoDev, StatusReg);
return Status;
}
/**
Sends out ATAPI Inquiry Packet Command to the specified device.
This command will return INQUIRY data of the device.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@param[out] MediaInfo The media information of the specified block media.
@retval EFI_SUCCESS Command executed successfully.
@retval EFI_DEVICE_ERROR Device cannot be executed command successfully.
@retval EFI_UNSUPPORTED Unsupported device type.
**/
EFI_STATUS
Inquiry (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition,
OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo
)
{
ATAPI_PACKET_COMMAND Packet;
EFI_STATUS Status;
ATAPI_INQUIRY_DATA Idata;
//
// prepare command packet for the ATAPI Inquiry Packet Command.
//
ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
ZeroMem (&Idata, sizeof (ATAPI_INQUIRY_DATA));
Packet.Inquiry.opcode = ATA_CMD_INQUIRY;
Packet.Inquiry.page_code = 0;
Packet.Inquiry.allocation_length = (UINT8) sizeof (ATAPI_INQUIRY_DATA);
//
// Send command packet and get requested Inquiry data.
//
Status = AtapiPacketCommandIn (
AtapiBlkIoDev,
DevicePosition,
&Packet,
(UINT16 *) (&Idata),
sizeof (ATAPI_INQUIRY_DATA),
ATAPITIMEOUT
//50
);
if (Status != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
//
// Identify device type via INQUIRY data.
//
switch (Idata.peripheral_type & 0x1f) {
case 0x00:
//
// Magnetic Disk
//
MediaInfo->DeviceType = IdeLS120;
MediaInfo->MediaPresent = FALSE;
MediaInfo->LastBlock = 0;
MediaInfo->BlockSize = 0x200;
break;
case 0x05:
//
// CD-ROM
//
MediaInfo->DeviceType = IdeCDROM;
MediaInfo->MediaPresent = FALSE;
MediaInfo->LastBlock = 0;
MediaInfo->BlockSize = 0x800;
break;
default:
return EFI_UNSUPPORTED;
}
return EFI_SUCCESS;
}
/**
Used before read/write blocks from/to ATAPI device media.
Since ATAPI device media is removable, it is necessary to detect
whether media is present and get current present media's information.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@param[in, out] MediaInfo The media information of the specified block media.
@retval EFI_SUCCESS Command executed successfully.
@retval EFI_DEVICE_ERROR Some device errors happen.
@retval EFI_OUT_OF_RESOURCES Can not allocate required resources.
**/
EFI_STATUS
DetectMedia (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition,
IN OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo
)
{
UINTN Index;
UINTN RetryNum;
UINTN MaxRetryNum;
ATAPI_REQUEST_SENSE_DATA *SenseBuffers;
BOOLEAN NeedReadCapacity;
BOOLEAN NeedRetry;
EFI_STATUS Status;
UINT8 SenseCounts;
SenseBuffers = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (*SenseBuffers)));
if (SenseBuffers == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Test Unit Ready command is used to detect whether device is accessible,
// the device will produce corresponding Sense data.
//
for (Index = 0; Index < 2; Index++) {
Status = TestUnitReady (AtapiBlkIoDev, DevicePosition);
if (Status != EFI_SUCCESS) {
Status = ResetDevice (AtapiBlkIoDev, DevicePosition, FALSE);
if (Status != EFI_SUCCESS) {
ResetDevice (AtapiBlkIoDev, DevicePosition, TRUE);
}
} else {
break;
}
}
SenseCounts = MAX_SENSE_KEY_COUNT;
Status = EFI_SUCCESS;
NeedReadCapacity = TRUE;
for (Index = 0; Index < 5; Index++) {
SenseCounts = MAX_SENSE_KEY_COUNT;
Status = RequestSense (
AtapiBlkIoDev,
DevicePosition,
SenseBuffers,
&SenseCounts
);
DEBUG ((EFI_D_INFO, "Atapi Request Sense Count is %d\n", SenseCounts));
if (IsDeviceStateUnclear (SenseBuffers, SenseCounts) || IsNoMedia (SenseBuffers, SenseCounts)) {
//
// We are not sure whether the media is present or not, try again
//
TestUnitReady (AtapiBlkIoDev, DevicePosition);
} else {
break;
}
}
if (Status == EFI_SUCCESS) {
if (IsNoMedia (SenseBuffers, SenseCounts)) {
NeedReadCapacity = FALSE;
MediaInfo->MediaPresent = FALSE;
MediaInfo->LastBlock = 0;
}
if (IsMediaError (SenseBuffers, SenseCounts)) {
return EFI_DEVICE_ERROR;
}
}
if (NeedReadCapacity) {
//
// at most retry 5 times
//
MaxRetryNum = 5;
RetryNum = 1;
//
// initial retry once
//
for (Index = 0; (Index < RetryNum) && (Index < MaxRetryNum); Index++) {
Status = ReadCapacity (AtapiBlkIoDev, DevicePosition, MediaInfo);
MicroSecondDelay (200000);
SenseCounts = MAX_SENSE_KEY_COUNT;
if (Status != EFI_SUCCESS) {
Status = RequestSense (AtapiBlkIoDev, DevicePosition, SenseBuffers, &SenseCounts);
//
// If Request Sense data failed, reset the device and retry.
//
if (Status != EFI_SUCCESS) {
Status = ResetDevice (AtapiBlkIoDev, DevicePosition, FALSE);
//
// if ATAPI soft reset fail,
// use stronger reset mechanism -- ATA soft reset.
//
if (Status != EFI_SUCCESS) {
ResetDevice (AtapiBlkIoDev, DevicePosition, TRUE);
}
RetryNum++;
//
// retry once more
//
continue;
}
//
// No Media
//
if (IsNoMedia (SenseBuffers, SenseCounts)) {
MediaInfo->MediaPresent = FALSE;
MediaInfo->LastBlock = 0;
break;
}
if (IsMediaError (SenseBuffers, SenseCounts)) {
return EFI_DEVICE_ERROR;
}
if (!IsDriveReady (SenseBuffers, SenseCounts, &NeedRetry)) {
//
// Drive not ready: if NeedRetry, then retry once more;
// else return error
//
if (NeedRetry) {
RetryNum++;
continue;
} else {
return EFI_DEVICE_ERROR;
}
}
//
// if read capacity fail not for above reasons, retry once more
//
RetryNum++;
}
}
}
return EFI_SUCCESS;
}
/**
Reset specified Atapi device.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@param[in] Extensive If TRUE, use ATA soft reset, otherwise use Atapi soft reset.
@retval EFI_SUCCESS Command executed successfully.
@retval EFI_DEVICE_ERROR Some device errors happen.
**/
EFI_STATUS
ResetDevice (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition,
IN BOOLEAN Extensive
)
{
UINT8 DevControl;
UINT8 Command;
UINT8 DeviceSelect;
UINT16 DeviceControlReg;
UINT16 CommandReg;
UINT16 HeadReg;
UINT8 Channel;
UINT8 Device;
Channel = (UINT8) (DevicePosition / 2);
Device = (UINT8) (DevicePosition % 2);
ASSERT (Channel < MAX_IDE_CHANNELS);
DeviceControlReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Alt.DeviceControl;
CommandReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Reg.Command;
HeadReg = AtapiBlkIoDev->IdeIoPortReg[Channel].Head;
if (Extensive) {
DevControl = 0;
DevControl |= ATA_CTLREG_SRST;
//
// set SRST bit to initiate soft reset
//
DevControl |= BIT1;
//
// disable Interrupt
//
IoWrite8 (DeviceControlReg, DevControl);
//
// Wait 10us
//
MicroSecondDelay (10);
//
// Clear SRST bit
//
DevControl &= 0xfb;
//
// 0xfb:1111,1011
//
IoWrite8 (DeviceControlReg, DevControl);
//
// slave device needs at most 31s to clear BSY
//
if (WaitForBSYClear (AtapiBlkIoDev, &(AtapiBlkIoDev->IdeIoPortReg[Channel]), 31000) == EFI_TIMEOUT) {
return EFI_DEVICE_ERROR;
}
} else {
//
// for ATAPI device, no need to wait DRDY ready after device selecting.
// bit7 and bit5 are both set to 1 for backward compatibility
//
DeviceSelect = (UINT8) (((BIT7 | BIT5) | (Device << 4)));
IoWrite8 (HeadReg, DeviceSelect);
Command = ATA_CMD_SOFT_RESET;
IoWrite8 (CommandReg, Command);
//
// BSY cleared is the only status return to the host by the device when reset is completed
// slave device needs at most 31s to clear BSY
//
if (WaitForBSYClear (AtapiBlkIoDev, &(AtapiBlkIoDev->IdeIoPortReg[Channel]), 31000) != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
//
// stall 5 seconds to make the device status stable
//
MicroSecondDelay (STALL_1_SECONDS * 5);
}
return EFI_SUCCESS;
}
/**
Sends out ATAPI Request Sense Packet Command to the specified device.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@param[in] SenseBuffers Pointer to sense buffer.
@param[in, out] SenseCounts Length of sense buffer.
@retval EFI_SUCCESS Command executed successfully.
@retval EFI_DEVICE_ERROR Some device errors happen.
**/
EFI_STATUS
RequestSense (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition,
IN ATAPI_REQUEST_SENSE_DATA *SenseBuffers,
IN OUT UINT8 *SenseCounts
)
{
EFI_STATUS Status;
ATAPI_REQUEST_SENSE_DATA *Sense;
UINT16 *Ptr;
BOOLEAN SenseReq;
ATAPI_PACKET_COMMAND Packet;
ZeroMem (SenseBuffers, sizeof (ATAPI_REQUEST_SENSE_DATA) * (*SenseCounts));
//
// fill command packet for Request Sense Packet Command
//
ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
Packet.RequestSence.opcode = ATA_CMD_REQUEST_SENSE;
Packet.RequestSence.allocation_length = (UINT8) sizeof (ATAPI_REQUEST_SENSE_DATA);
Ptr = (UINT16 *) SenseBuffers;
//
// initialize pointer
//
*SenseCounts = 0;
//
// request sense data from device continiously until no sense data exists in the device.
//
for (SenseReq = TRUE; SenseReq;) {
Sense = (ATAPI_REQUEST_SENSE_DATA *) Ptr;
//
// send out Request Sense Packet Command and get one Sense data form device
//
Status = AtapiPacketCommandIn (
AtapiBlkIoDev,
DevicePosition,
&Packet,
Ptr,
sizeof (ATAPI_REQUEST_SENSE_DATA),
ATAPITIMEOUT
);
//
// failed to get Sense data
//
if (Status != EFI_SUCCESS) {
if (*SenseCounts == 0) {
return EFI_DEVICE_ERROR;
} else {
return EFI_SUCCESS;
}
}
(*SenseCounts)++;
if (*SenseCounts > MAX_SENSE_KEY_COUNT) {
return EFI_SUCCESS;
}
//
// We limit MAX sense data count to 20 in order to avoid dead loop. Some
// incompatible ATAPI devices don't retrive NO_SENSE when there is no media.
// In this case, dead loop occurs if we don't have a gatekeeper. 20 is
// supposed to be large enough for any ATAPI device.
//
if ((Sense->sense_key != ATA_SK_NO_SENSE) && ((*SenseCounts) < 20)) {
Ptr += sizeof (ATAPI_REQUEST_SENSE_DATA) / 2;
//
// Ptr is word based pointer
//
} else {
//
// when no sense key, skip out the loop
//
SenseReq = FALSE;
}
}
return EFI_SUCCESS;
}
/**
Sends out ATAPI Read Capacity Packet Command to the specified device.
This command will return the information regarding the capacity of the
media in the device.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@param[in, out] MediaInfo The media information of the specified block media.
@retval EFI_SUCCESS Command executed successfully.
@retval EFI_DEVICE_ERROR Some device errors happen.
**/
EFI_STATUS
ReadCapacity (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition,
IN OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo
)
{
EFI_STATUS Status;
ATAPI_PACKET_COMMAND Packet;
//
// used for capacity data returned from ATAPI device
//
ATAPI_READ_CAPACITY_DATA Data;
ATAPI_READ_FORMAT_CAPACITY_DATA FormatData;
ZeroMem (&Data, sizeof (Data));
ZeroMem (&FormatData, sizeof (FormatData));
if (MediaInfo->DeviceType == IdeCDROM) {
ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
Packet.Inquiry.opcode = ATA_CMD_READ_CAPACITY;
Status = AtapiPacketCommandIn (
AtapiBlkIoDev,
DevicePosition,
&Packet,
(UINT16 *) (&Data),
sizeof (ATAPI_READ_CAPACITY_DATA),
ATAPITIMEOUT
);
} else {
//
// DeviceType == IdeLS120
//
ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
Packet.ReadFormatCapacity.opcode = ATA_CMD_READ_FORMAT_CAPACITY;
Packet.ReadFormatCapacity.allocation_length_lo = 12;
Status = AtapiPacketCommandIn (
AtapiBlkIoDev,
DevicePosition,
&Packet,
(UINT16 *) (&FormatData),
sizeof (ATAPI_READ_FORMAT_CAPACITY_DATA),
ATAPITIMEOUT*10
);
}
if (Status == EFI_SUCCESS) {
if (MediaInfo->DeviceType == IdeCDROM) {
MediaInfo->LastBlock = (Data.LastLba3 << 24) | (Data.LastLba2 << 16) | (Data.LastLba1 << 8) | Data.LastLba0;
MediaInfo->MediaPresent = TRUE;
//
// Because the user data portion in the sector of the Data CD supported
// is always 800h
//
MediaInfo->BlockSize = 0x800;
}
if (MediaInfo->DeviceType == IdeLS120) {
if (FormatData.DesCode == 3) {
MediaInfo->MediaPresent = FALSE;
MediaInfo->LastBlock = 0;
} else {
MediaInfo->LastBlock = (FormatData.LastLba3 << 24) |
(FormatData.LastLba2 << 16) |
(FormatData.LastLba1 << 8) |
FormatData.LastLba0;
MediaInfo->LastBlock--;
MediaInfo->MediaPresent = TRUE;
MediaInfo->BlockSize = 0x200;
}
}
return EFI_SUCCESS;
} else {
return EFI_DEVICE_ERROR;
}
}
/**
Perform read from disk in block unit.
@param[in] AtapiBlkIoDev A pointer to atapi block IO device.
@param[in] DevicePosition An integer to signify device position.
@param[in] Buffer Buffer to contain read data.
@param[in] StartLba Starting LBA address.
@param[in] NumberOfBlocks Number of blocks to read.
@param[in] BlockSize Size of each block.
@retval EFI_SUCCESS Command executed successfully.
@retval EFI_DEVICE_ERROR Some device errors happen.
**/
EFI_STATUS
ReadSectors (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev,
IN UINTN DevicePosition,
IN VOID *Buffer,
IN EFI_PEI_LBA StartLba,
IN UINTN NumberOfBlocks,
IN UINTN BlockSize
)
{
ATAPI_PACKET_COMMAND Packet;
ATAPI_READ10_CMD *Read10Packet;
EFI_STATUS Status;
UINTN BlocksRemaining;
UINT32 Lba32;
UINT32 ByteCount;
UINT16 SectorCount;
VOID *PtrBuffer;
UINT16 MaxBlock;
//
// fill command packet for Read(10) command
//
ZeroMem (&Packet, sizeof (ATAPI_PACKET_COMMAND));
Read10Packet = &Packet.Read10;
Lba32 = (UINT32) StartLba;
PtrBuffer = Buffer;
//
// limit the data bytes that can be transfered by one Read(10) Command
//
MaxBlock = (UINT16) (0x10000 / BlockSize);
//
// (64k bytes)
//
BlocksRemaining = NumberOfBlocks;
Status = EFI_SUCCESS;
while (BlocksRemaining > 0) {
if (BlocksRemaining <= MaxBlock) {
SectorCount = (UINT16) BlocksRemaining;
} else {
SectorCount = MaxBlock;
}
//
// fill the Packet data sturcture
//
Read10Packet->opcode = ATA_CMD_READ_10;
//
// Lba0 ~ Lba3 specify the start logical block address of the data transfer.
// Lba0 is MSB, Lba3 is LSB
//
Read10Packet->Lba3 = (UINT8) (Lba32 & 0xff);
Read10Packet->Lba2 = (UINT8) (Lba32 >> 8);
Read10Packet->Lba1 = (UINT8) (Lba32 >> 16);
Read10Packet->Lba0 = (UINT8) (Lba32 >> 24);
//
// TranLen0 ~ TranLen1 specify the transfer length in block unit.
// TranLen0 is MSB, TranLen is LSB
//
Read10Packet->TranLen1 = (UINT8) (SectorCount & 0xff);
Read10Packet->TranLen0 = (UINT8) (SectorCount >> 8);
ByteCount = (UINT32) (SectorCount * BlockSize);
Status = AtapiPacketCommandIn (
AtapiBlkIoDev,
DevicePosition,
&Packet,
(UINT16 *) PtrBuffer,
ByteCount,
ATAPILONGTIMEOUT
);
if (Status != EFI_SUCCESS) {
return Status;
}
Lba32 += SectorCount;
PtrBuffer = (UINT8 *) PtrBuffer + SectorCount * BlockSize;
BlocksRemaining -= SectorCount;
}
return Status;
}
/**
Check if there is media according to sense data.
@param[in] SenseData Pointer to sense data.
@param[in] SenseCounts Count of sense data.
@retval TRUE No media
@retval FALSE Media exists
**/
BOOLEAN
IsNoMedia (
IN ATAPI_REQUEST_SENSE_DATA *SenseData,
IN UINTN SenseCounts
)
{
ATAPI_REQUEST_SENSE_DATA *SensePtr;
UINTN Index;
BOOLEAN IsNoMedia;
IsNoMedia = FALSE;
SensePtr = SenseData;
for (Index = 0; Index < SenseCounts; Index++) {
if ((SensePtr->sense_key == ATA_SK_NOT_READY) && (SensePtr->addnl_sense_code == ATA_ASC_NO_MEDIA)) {
IsNoMedia = TRUE;
}
SensePtr++;
}
return IsNoMedia;
}
/**
Check if device state is unclear according to sense data.
@param[in] SenseData Pointer to sense data.
@param[in] SenseCounts Count of sense data.
@retval TRUE Device state is unclear
@retval FALSE Device state is clear
**/
BOOLEAN
IsDeviceStateUnclear (
IN ATAPI_REQUEST_SENSE_DATA *SenseData,
IN UINTN SenseCounts
)
{
ATAPI_REQUEST_SENSE_DATA *SensePtr;
UINTN Index;
BOOLEAN Unclear;
Unclear = FALSE;
SensePtr = SenseData;
for (Index = 0; Index < SenseCounts; Index++) {
if (SensePtr->sense_key == 0x06) {
//
// Sense key is 0x06 means the device is just be reset or media just
// changed. The current state of the device is unclear.
//
Unclear = TRUE;
break;
}
SensePtr++;
}
return Unclear;
}
/**
Check if there is media error according to sense data.
@param[in] SenseData Pointer to sense data.
@param[in] SenseCounts Count of sense data.
@retval TRUE Media error
@retval FALSE No media error
**/
BOOLEAN
IsMediaError (
IN ATAPI_REQUEST_SENSE_DATA *SenseData,
IN UINTN SenseCounts
)
{
ATAPI_REQUEST_SENSE_DATA *SensePtr;
UINTN Index;
BOOLEAN IsError;
IsError = FALSE;
SensePtr = SenseData;
for (Index = 0; Index < SenseCounts; Index++) {
switch (SensePtr->sense_key) {
case ATA_SK_MEDIUM_ERROR:
switch (SensePtr->addnl_sense_code) {
case ATA_ASC_MEDIA_ERR1:
//
// fall through
//
case ATA_ASC_MEDIA_ERR2:
//
// fall through
//
case ATA_ASC_MEDIA_ERR3:
//
// fall through
//
case ATA_ASC_MEDIA_ERR4:
IsError = TRUE;
break;
default:
break;
}
break;
case ATA_SK_NOT_READY:
switch (SensePtr->addnl_sense_code) {
case ATA_ASC_MEDIA_UPSIDE_DOWN:
IsError = TRUE;
break;
default:
break;
}
break;
default:
break;
}
SensePtr++;
}
return IsError;
}
/**
Check if drive is ready according to sense data.
@param[in] SenseData Pointer to sense data.
@param[in] SenseCounts Count of sense data.
@param[out] NeedRetry Indicate if retry is needed.
@retval TRUE Drive ready
@retval FALSE Drive not ready
**/
BOOLEAN
IsDriveReady (
IN ATAPI_REQUEST_SENSE_DATA *SenseData,
IN UINTN SenseCounts,
OUT BOOLEAN *NeedRetry
)
{
ATAPI_REQUEST_SENSE_DATA *SensePtr;
UINTN Index;
BOOLEAN IsReady;
IsReady = TRUE;
*NeedRetry = FALSE;
SensePtr = SenseData;
for (Index = 0; Index < SenseCounts; Index++) {
switch (SensePtr->sense_key) {
case ATA_SK_NOT_READY:
switch (SensePtr->addnl_sense_code) {
case ATA_ASC_NOT_READY:
switch (SensePtr->addnl_sense_code_qualifier) {
case ATA_ASCQ_IN_PROGRESS:
IsReady = FALSE;
*NeedRetry = TRUE;
break;
default:
IsReady = FALSE;
*NeedRetry = FALSE;
break;
}
break;
default:
break;
}
break;
default:
break;
}
SensePtr++;
}
return IsReady;
}
|