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
|
/* $OpenBSD: atactl.c,v 1.38 2007/09/02 15:19:23 deraadt Exp $ */
/* $NetBSD: atactl.c,v 1.4 1999/02/24 18:49:14 jwise Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Ken Hornstein.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* atactl(8) - a program to control ATA devices.
*/
#include <sys/param.h>
#include <sys/ioctl.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include <dev/ata/atareg.h>
#include <dev/ic/wdcreg.h>
#include <dev/ic/wdcevent.h>
#include <sys/ataio.h>
#include "atasec.h"
#include "atasmart.h"
struct command {
const char *cmd_name;
void (*cmd_func)(int, char *[]);
};
struct bitinfo {
u_int bitmask;
const char *string;
};
struct valinfo {
int value;
const char *string;
};
int main(int, char *[]);
__dead void usage(void);
void ata_command(struct atareq *);
void print_bitinfo(const char *, u_int, struct bitinfo *);
int strtoval(const char *, struct valinfo *);
const char *valtostr(int, struct valinfo *);
int fd; /* file descriptor for device */
extern char *__progname; /* from crt0.o */
void device_dump(int, char*[]);
void device_identify(int, char *[]);
void device_setidle(int, char *[]);
void device_idle(int, char *[]);
void device_checkpower(int, char *[]);
void device_acoustic(int, char *[]);
void device_apm(int, char *[]);
void device_feature(int, char *[]);
void device_sec_setpass(int, char *[]);
void device_sec_unlock(int, char *[]);
void device_sec_erase(int, char *[]);
void device_sec_freeze(int, char *[]);
void device_sec_disablepass(int, char *[]);
void device_smart_enable(int, char *[]);
void device_smart_disable(int, char *[]);
void device_smart_status(int, char *[]);
void device_smart_autosave(int, char *[]);
void device_smart_offline(int, char *[]);
void device_smart_read(int, char *[]);
void device_smart_readlog(int, char *[]);
void device_attr(int, char *[]);
void smart_print_errdata(struct smart_log_errdata *);
int smart_cksum(u_int8_t *, size_t);
char *sec_getpass(int, int);
struct command commands[] = {
{ "dump", device_dump },
{ "identify", device_identify },
{ "setidle", device_setidle },
{ "setstandby", device_setidle },
{ "idle", device_idle },
{ "standby", device_idle },
{ "sleep", device_idle },
{ "checkpower", device_checkpower },
{ "acousticdisable", device_feature },
{ "acousticset", device_acoustic },
{ "apmdisable", device_feature },
{ "apmset", device_apm },
{ "poddisable", device_feature },
{ "podenable", device_feature },
{ "puisdisable", device_feature },
{ "puisenable", device_feature },
{ "puisspinup", device_feature },
{ "readaheaddisable", device_feature },
{ "readaheadenable", device_feature },
{ "secsetpass", device_sec_setpass },
{ "secunlock", device_sec_unlock },
{ "secerase", device_sec_erase },
{ "secfreeze", device_sec_freeze },
{ "secdisablepass", device_sec_disablepass },
{ "smartenable", device_smart_enable },
{ "smartdisable", device_smart_disable },
{ "smartstatus", device_smart_status },
{ "smartautosave", device_smart_autosave },
{ "smartoffline", device_smart_offline },
{ "smartread", device_smart_read },
{ "smartreadlog", device_smart_readlog },
{ "readattr", device_attr },
{ "writecachedisable", device_feature },
{ "writecacheenable", device_feature },
{ NULL, NULL },
};
/*
* Tables containing bitmasks used for error reporting and
* device identification.
*/
struct bitinfo ata_caps[] = {
{ ATA_CAP_STBY, "ATA standby timer values" },
{ WDC_CAP_IORDY, "IORDY operation" },
{ WDC_CAP_IORDY_DSBL, "IORDY disabling" },
{ NULL, NULL },
};
struct bitinfo ata_vers[] = {
{ WDC_VER_ATA1, "ATA-1" },
{ WDC_VER_ATA2, "ATA-2" },
{ WDC_VER_ATA3, "ATA-3" },
{ WDC_VER_ATA4, "ATA-4" },
{ WDC_VER_ATA5, "ATA-5" },
{ WDC_VER_ATA6, "ATA-6" },
{ WDC_VER_ATA7, "ATA-7" },
{ WDC_VER_ATA8, "ATA-8" },
{ WDC_VER_ATA9, "ATA-9" },
{ WDC_VER_ATA10, "ATA-10" },
{ WDC_VER_ATA11, "ATA-11" },
{ WDC_VER_ATA12, "ATA-12" },
{ WDC_VER_ATA13, "ATA-13" },
{ WDC_VER_ATA14, "ATA-14" },
{ NULL, NULL },
};
struct bitinfo ata_cmd_set1[] = {
{ WDC_CMD1_NOP, "NOP command" },
{ WDC_CMD1_RB, "READ BUFFER command" },
{ WDC_CMD1_WB, "WRITE BUFFER command" },
{ WDC_CMD1_HPA, "Host Protected Area feature set" },
{ WDC_CMD1_DVRST, "DEVICE RESET command" },
{ WDC_CMD1_SRV, "SERVICE interrupt" },
{ WDC_CMD1_RLSE, "Release interrupt" },
{ WDC_CMD1_AHEAD, "Read look-ahead" },
{ WDC_CMD1_CACHE, "Write cache" },
{ WDC_CMD1_PKT, "PACKET command feature set" },
{ WDC_CMD1_PM, "Power Management feature set" },
{ WDC_CMD1_REMOV, "Removable Media feature set" },
{ WDC_CMD1_SEC, "Security Mode feature set" },
{ WDC_CMD1_SMART, "SMART feature set" },
{ NULL, NULL },
};
struct bitinfo ata_cmd_set2[] = {
{ ATAPI_CMD2_FCE, "Flush Cache Ext command" },
{ ATAPI_CMD2_FC, "Flush Cache command" },
{ ATAPI_CMD2_DCO, "Device Configuration Overlay feature set" },
{ ATAPI_CMD2_48AD, "48bit address feature set" },
{ ATAPI_CMD2_AAM, "Automatic Acoustic Management feature set" },
{ ATAPI_CMD2_SM, "Set Max security extension commands" },
{ ATAPI_CMD2_SF, "Set Features subcommand required" },
{ ATAPI_CMD2_PUIS, "Power-up in standby feature set" },
{ WDC_CMD2_RMSN, "Removable Media Status Notification feature set" },
{ ATA_CMD2_APM, "Advanced Power Management feature set" },
{ ATA_CMD2_CFA, "CFA feature set" },
{ ATA_CMD2_RWQ, "READ/WRITE DMA QUEUED commands" },
{ WDC_CMD2_DM, "DOWNLOAD MICROCODE command" },
{ NULL, NULL },
};
struct bitinfo ata_cmd_ext[] = {
{ ATAPI_CMDE_IIUF, "IDLE IMMEDIATE with UNLOAD FEATURE" },
{ ATAPI_CMDE_MSER, "Media serial number" },
{ ATAPI_CMDE_TEST, "SMART self-test" },
{ ATAPI_CMDE_SLOG, "SMART error logging" },
{ NULL, NULL },
};
/*
* Tables containing bitmasks and values used for
* SMART commands.
*/
struct bitinfo smart_offcap[] = {
{ SMART_OFFCAP_EXEC, "execute immediate" },
{ SMART_OFFCAP_ABORT, "abort/restart" },
{ SMART_OFFCAP_READSCAN, "read scanning" },
{ SMART_OFFCAP_SELFTEST, "self-test routines" },
{ 0, NULL}
};
struct bitinfo smart_smartcap[] = {
{ SMART_SMARTCAP_SAVE, "saving SMART data" },
{ SMART_SMARTCAP_AUTOSAVE, "enable/disable attribute autosave" },
{ 0, NULL }
};
struct valinfo smart_autosave[] = {
{ SMART_AUTOSAVE_EN, "enable" },
{ SMART_AUTOSAVE_DS, "disable" },
{ 0, NULL }
};
struct valinfo smart_offline[] = {
{ SMART_OFFLINE_COLLECT, "collect" },
{ SMART_OFFLINE_SHORTOFF, "shortoffline" },
{ SMART_OFFLINE_EXTENOFF, "extenoffline" },
{ SMART_OFFLINE_ABORT, "abort" },
{ SMART_OFFLINE_SHORTCAP, "shortcaptive" },
{ SMART_OFFLINE_EXTENCAP, "extencaptive" },
{ 0, NULL }
};
struct valinfo smart_readlog[] = {
{ SMART_READLOG_DIR, "directory" },
{ SMART_READLOG_SUM, "summary" },
{ SMART_READLOG_COMP, "comp" },
{ SMART_READLOG_SELF, "selftest" },
{ 0, NULL }
};
struct valinfo smart_offstat[] = {
{ SMART_OFFSTAT_NOTSTART, "never started" },
{ SMART_OFFSTAT_COMPLETE, "completed ok" },
{ SMART_OFFSTAT_SUSPEND, "suspended by an interrupting command" },
{ SMART_OFFSTAT_INTR, "aborted by an interrupting command" },
{ SMART_OFFSTAT_ERROR, "aborted due to fatal error" },
{ 0, NULL }
};
struct valinfo smart_selfstat[] = {
{ SMART_SELFSTAT_COMPLETE, "completed ok or not started" },
{ SMART_SELFSTAT_ABORT, "aborted" },
{ SMART_SELFSTAT_INTR, "hardware or software reset" },
{ SMART_SELFSTAT_ERROR, "fatal error" },
{ SMART_SELFSTAT_UNKFAIL, "unknown test element failed" },
{ SMART_SELFSTAT_ELFAIL, "electrical test element failed" },
{ SMART_SELFSTAT_SRVFAIL, "servo test element failed" },
{ SMART_SELFSTAT_RDFAIL, "read test element failed" },
{ 0, NULL }
};
struct valinfo smart_logstat[] = {
{ SMART_LOG_STATE_UNK, "unknown" },
{ SMART_LOG_STATE_SLEEP, "sleep" },
{ SMART_LOG_STATE_ACTIDL, "active/idle" },
{ SMART_LOG_STATE_OFFSELF, "off-line or self-test" },
{ 0, NULL }
};
/*
* Tables containing values used for reading
* device attributes.
*/
struct valinfo ibm_attr_names[] = {
{ 1, "Raw Read Error Rate" },
{ 2, "Throughput Performance" },
{ 3, "Spin Up Time" },
{ 4, "Start/Stop Count" },
{ 5, "Reallocated Sector Count" },
{ 7, "Seek Error Rate" },
{ 8, "Seek Time Performance" },
{ 9, "Power-on Hours Count" },
{ 10, "Spin Retry Count" },
{ 12, "Device Power Cycle Count" },
{ 192, "Power-off Retract Count" },
{ 193, "Load Cycle Count" },
{ 194, "Temperature" },
{ 196, "Reallocation Event Count" },
{ 197, "Current Pending Sector Count" },
{ 198, "Off-line Scan Uncorrectable Sector Count" },
{ 199, "Ultra DMA CRC Error Count" },
{ 0, NULL },
};
#define MAKEWORD(b1, b2) \
(b2 << 8 | b1)
#define MAKEDWORD(b1, b2, b3, b4) \
(b4 << 24 | b3 << 16 | b2 << 8 | b1)
int
main(int argc, char *argv[])
{
struct command *cmdp;
char dvname_store[MAXPATHLEN];
if (argc < 2)
usage();
/*
* Open the device
*/
fd = opendisk(argv[1], O_RDWR, dvname_store, sizeof(dvname_store), 0);
if (fd == -1) {
if (errno == ENOENT) {
/*
* Device doesn't exist. Probably trying to open
* a device which doesn't use disk semantics for
* device name. Try again, specifying "cooked",
* which leaves off the "r" in front of the device's
* name.
*/
fd = opendisk(argv[1], O_RDWR, dvname_store,
sizeof(dvname_store), 1);
if (fd == -1)
err(1, "%s", argv[1]);
} else
err(1, "%s", argv[1]);
}
/* Skip program name and device name. */
if (argc != 2) {
argv += 2;
argc -= 2;
} else {
argv[1] = "identify";
argv += 1;
argc -= 1;
}
/* Look up and call the command. */
for (cmdp = commands; cmdp->cmd_name != NULL; cmdp++)
if (strcmp(argv[0], cmdp->cmd_name) == 0)
break;
if (cmdp->cmd_name == NULL)
errx(1, "unknown command: %s", argv[0]);
(cmdp->cmd_func)(argc, argv);
return (0);
}
__dead void
usage(void)
{
fprintf(stderr, "usage: %s <device> [command [arg]]\n",
__progname);
exit(1);
}
/*
* Wrapper that calls ATAIOCCOMMAND and checks for errors
*/
void
ata_command(struct atareq *req)
{
if (ioctl(fd, ATAIOCCOMMAND, req) == -1)
err(1, "ATAIOCCOMMAND failed");
switch (req->retsts) {
case ATACMD_OK:
return;
case ATACMD_TIMEOUT:
errx(1, "ATA command timed out");
case ATACMD_DF:
errx(1, "ATA device returned a Device Fault");
case ATACMD_ERROR:
if (req->error & WDCE_ABRT)
errx(1, "ATA device returned Aborted Command");
else
errx(1, "ATA device returned error register %0x",
req->error);
default:
errx(1, "ATAIOCCOMMAND returned unknown result code %d",
req->retsts);
}
}
/*
* Print out strings associated with particular bitmasks
*/
void
print_bitinfo(const char *f, u_int bits, struct bitinfo *binfo)
{
for (; binfo->bitmask != NULL; binfo++)
if (bits & binfo->bitmask)
printf(f, binfo->string);
}
/*
* strtoval():
* returns value associated with given string,
* if no value found -1 is returned.
*/
int
strtoval(const char *str, struct valinfo *vinfo)
{
for (; vinfo->string != NULL; vinfo++)
if (strcmp(str, vinfo->string) == 0)
return (vinfo->value);
return (-1);
}
/*
* valtostr():
* returns string associated with given value,
* if no string found NULL is returned.
*/
const char *
valtostr(int val, struct valinfo *vinfo)
{
for (; vinfo->string != NULL; vinfo++)
if (val == vinfo->value)
return (vinfo->string);
return (NULL);
}
/*
* DEVICE COMMANDS
*/
/*
* device dump:
*
* extract issued ATA requests from the log buffer
*/
void
device_dump(int argc, char *argv[])
{
unsigned char buf[131072];
atagettrace_t agt;
unsigned int total;
unsigned int p = 0;
int type;
const char *types[] = { NULL, "status", "error", "ATAPI",
"ATAPI done", "ATA cmd", "ATA", "select slave",
"select master", "register read", "ATA LBA48" };
int num_types = sizeof(types) / sizeof(types[0]);
int info;
int entrysize;
int i;
int flags;
if (argc != 1)
goto usage;
memset(&agt, 0, sizeof(agt));
agt.buf_size = sizeof(buf);
agt.buf = buf;
if (ioctl(fd, ATAIOGETTRACE, &agt) == -1)
err(1, "ATAIOGETTRACE failed");
total = agt.bytes_copied;
/* Parse entries */
while (p < total) {
type = buf[p++];
if (p >= total)
return;
if (type <= 0 || type >= num_types)
return;
info = buf[p++];
if (p >= total)
return;
entrysize = (info & 0x1f);
printf ("ch %d", (info >> 5) & 0x7);
printf(": %s", types[type]);
switch (type) {
case WDCEVENT_STATUS:
if (entrysize != 1)
return;
printf(": 0x%x", buf[p]);
if (buf[p] & WDCS_BSY)
printf(" BSY");
if (buf[p] & WDCS_DRDY)
printf(" DRDY");
if (buf[p] & WDCS_DWF)
printf(" DWF");
if (buf[p] & WDCS_DSC)
printf(" DSC");
if (buf[p] & WDCS_DRQ)
printf(" DRQ");
if (buf[p] & WDCS_CORR)
printf(" CORR");
if (buf[p] & WDCS_IDX)
printf(" IDX");
if (buf[p] & WDCS_ERR)
printf(" ERR");
p++;
entrysize = 0;
break;
case WDCEVENT_ERROR:
if (entrysize != 1)
return;
printf(": 0x%x", buf[p]);
if (buf[p] & WDCE_BBK)
printf(" BBK/CRC");
if (buf[p] & WDCE_UNC)
printf(" UNC");
if (buf[p] & WDCE_MC)
printf(" MC");
if (buf[p] & WDCE_IDNF)
printf(" IDNF");
if (buf[p] & WDCE_MCR)
printf(" MCR");
if (buf[p] & WDCE_ABRT)
printf(" ABRT");
if (buf[p] & WDCE_TK0NF)
printf(" TK0NF");
if (buf[p] & WDCE_AMNF)
printf(" AMNF");
p++;
entrysize = 0;
break;
case WDCEVENT_ATAPI_CMD:
if (entrysize < 2 || p + 2 > total)
return;
flags = (buf[p] << 8) + buf[p + 1];
printf(": flags 0x%x", flags);
if (flags & 0x0100)
printf(" MEDIA");
if (flags & 0x0080)
printf(" SENSE");
if (flags & 0x0040)
printf(" DMA");
if (flags & 0x0020)
printf(" POLL");
if (flags & 0x0004)
printf(" TIMEOUT");
if (flags & 0x0002)
printf(" ATAPI");
p += 2;
entrysize -= 2;
break;
case WDCEVENT_ATAPI_DONE:
if (entrysize != 3 || p + 3 > total)
return;
flags = (buf[p] << 8) + buf[p + 1];
printf(": flags 0x%x", flags);
if (flags & 0x0100)
printf(" MEDIA");
if (flags & 0x0080)
printf(" SENSE");
if (flags & 0x0040)
printf(" DMA");
if (flags & 0x0020)
printf(" POLL");
if (flags & 0x0004)
printf(" TIMEOUT");
if (flags & 0x0002)
printf(" ATAPI");
printf(", error 0x%x", buf[p + 2]);
switch (buf[p + 2]) {
case 1:
printf(" (sense)");
break;
case 2:
printf(" (driver failure)");
break;
case 3:
printf(" (timeout)");
break;
case 4:
printf(" (busy)");
break;
case 5:
printf(" (ATAPI sense)");
break;
case 8:
printf(" (reset)");
break;
}
p += 3;
entrysize = 0;
break;
case WDCEVENT_ATA_LONG:
if (entrysize != 7 || p + 7 > total)
return;
printf(": ");
switch (buf[p + 6]) {
case WDCC_READDMA:
printf("READ DMA");
break;
case WDCC_WRITEDMA:
printf("WRITE DMA");
break;
default:
printf("CMD 0x%x", buf[p + 6]);
}
printf(" head %d, precomp %d, cyl_hi %d, "
"cyl_lo %d, sec %d, cnt %d",
buf[p], buf[p + 1], buf[p + 2], buf[p + 3],
buf[p + 4], buf[p + 5]);
p += 7;
entrysize = 0;
break;
case WDCEVENT_REG:
if (entrysize != 3 || p + 3 > total)
return;
switch (buf[p]) {
case 1:
printf(": error");
break;
case 2:
printf(": ireason");
break;
case 3:
printf(": lba_lo");
break;
case 4:
printf(": lba_mi");
break;
case 5:
printf(": lba_hi");
break;
case 6:
printf(": sdh");
break;
case 7:
printf(": status");
break;
case 8:
printf(": altstatus");
break;
default:
printf(": unknown register %d", buf[p]);
}
printf(": 0x%x", (buf[p + 1] << 8) + buf[p + 2]);
p += 3;
entrysize = 0;
break;
case WDCEVENT_ATA_EXT:
if (entrysize != 9 || p + 9 > total)
return;
printf(": ");
switch (buf[p + 8]) {
case WDCC_READDMA_EXT:
printf("READ DMA EXT");
break;
case WDCC_WRITEDMA_EXT:
printf("WRITE DMA EXT");
break;
default:
printf("CMD 0x%x", buf[p + 8]);
}
printf(" lba_hi1 %d, lba_hi2 %d, "
"lba_mi1 %d, lba_mi2 %d, lba_lo1 %d, lba_lo2 %d, "
"count1 %d, count2 %d",
buf[p], buf[p + 1], buf[p + 2], buf[p + 3],
buf[p + 4], buf[p + 5], buf[p + 6],
buf[p + 7]);
p += 9;
entrysize = 0;
break;
}
if (entrysize > 0)
printf(":");
for (i = 0; i < entrysize; i++) {
printf (" 0x%02x", buf[p]);
if (++p >= total)
break;
}
printf("\n");
}
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* device_identify:
*
* Display the identity of the device
*/
void
device_identify(int argc, char *argv[])
{
struct ataparams *inqbuf;
struct atareq req;
char inbuf[DEV_BSIZE];
u_int64_t capacity;
u_int8_t *s;
if (argc != 1)
goto usage;
memset(&inbuf, 0, sizeof(inbuf));
memset(&req, 0, sizeof(req));
inqbuf = (struct ataparams *) inbuf;
req.flags = ATACMD_READ;
req.command = WDCC_IDENTIFY;
req.databuf = (caddr_t) inbuf;
req.datalen = sizeof(inbuf);
req.timeout = 1000;
ata_command(&req);
if (BYTE_ORDER == BIG_ENDIAN) {
swap16_multi((u_int16_t *)inbuf, 10);
swap16_multi(((u_int16_t *)inbuf) + 20, 3);
swap16_multi(((u_int16_t *)inbuf) + 47, sizeof(inbuf) / 2 - 47);
}
if (!((inqbuf->atap_config & WDC_CFG_ATAPI_MASK) == WDC_CFG_ATAPI &&
((inqbuf->atap_model[0] == 'N' &&
inqbuf->atap_model[1] == 'E') ||
(inqbuf->atap_model[0] == 'F' &&
inqbuf->atap_model[1] == 'X')))) {
swap16_multi((u_int16_t *)(inqbuf->atap_model),
sizeof(inqbuf->atap_model) / 2);
swap16_multi((u_int16_t *)(inqbuf->atap_serial),
sizeof(inqbuf->atap_serial) / 2);
swap16_multi((u_int16_t *)(inqbuf->atap_revision),
sizeof(inqbuf->atap_revision) / 2);
}
/*
* Strip blanks off of the info strings.
*/
for (s = &inqbuf->atap_model[sizeof(inqbuf->atap_model) - 1];
s >= inqbuf->atap_model && *s == ' '; s--)
*s = '\0';
for (s = &inqbuf->atap_revision[sizeof(inqbuf->atap_revision) - 1];
s >= inqbuf->atap_revision && *s == ' '; s--)
*s = '\0';
for (s = &inqbuf->atap_serial[sizeof(inqbuf->atap_serial) - 1];
s >= inqbuf->atap_serial && *s == ' '; s--)
*s = '\0';
printf("Model: %.*s, Rev: %.*s, Serial #: %.*s\n",
(int) sizeof(inqbuf->atap_model), inqbuf->atap_model,
(int) sizeof(inqbuf->atap_revision), inqbuf->atap_revision,
(int) sizeof(inqbuf->atap_serial), inqbuf->atap_serial);
printf("Device type: %s, %s\n", inqbuf->atap_config & WDC_CFG_ATAPI ?
"ATAPI" : "ATA", inqbuf->atap_config & ATA_CFG_FIXED ? "fixed" :
"removable");
if (inqbuf->atap_cmd2_en & ATAPI_CMD2_48AD)
capacity = ((u_int64_t)inqbuf->atap_max_lba[3] << 48) |
((u_int64_t)inqbuf->atap_max_lba[2] << 32) |
((u_int64_t)inqbuf->atap_max_lba[1] << 16) |
(u_int64_t)inqbuf->atap_max_lba[0];
else
capacity = (inqbuf->atap_capacity[1] << 16) |
inqbuf->atap_capacity[0];
printf("Cylinders: %d, heads: %d, sec/track: %d, total "
"sectors: %llu\n", inqbuf->atap_cylinders,
inqbuf->atap_heads, inqbuf->atap_sectors, capacity);
if ((inqbuf->atap_cmd_set2 & ATA_CMD2_RWQ) &&
(inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK))
printf("Device supports command queue depth of %d\n",
(inqbuf->atap_queuedepth & WDC_QUEUE_DEPTH_MASK) + 1);
printf("Device capabilities:\n");
print_bitinfo("\t%s\n", inqbuf->atap_capabilities1, ata_caps);
if (inqbuf->atap_ata_major != 0 && inqbuf->atap_ata_major != 0xffff) {
printf("Device supports the following standards:\n");
print_bitinfo("%s ", inqbuf->atap_ata_major, ata_vers);
printf("\n");
}
if ((inqbuf->atap_cmd_set1 & WDC_CMD1_SEC) &&
inqbuf->atap_mpasswd_rev != 0 &&
inqbuf->atap_mpasswd_rev != 0xffff)
printf("Master password revision code 0x%04x\n",
inqbuf->atap_mpasswd_rev);
if (inqbuf->atap_cmd_set1 != 0 && inqbuf->atap_cmd_set1 != 0xffff &&
inqbuf->atap_cmd_set2 != 0 && inqbuf->atap_cmd_set2 != 0xffff) {
printf("Device supports the following command sets:\n");
print_bitinfo("\t%s\n", inqbuf->atap_cmd_set1, ata_cmd_set1);
print_bitinfo("\t%s\n", inqbuf->atap_cmd_set2, ata_cmd_set2);
print_bitinfo("\t%s\n", inqbuf->atap_cmd_ext, ata_cmd_ext);
}
if (inqbuf->atap_cmd_def != 0 && inqbuf->atap_cmd_def != 0xffff) {
printf("Device has enabled the following command "
"sets/features:\n");
print_bitinfo("\t%s\n", inqbuf->atap_cmd1_en, ata_cmd_set1);
print_bitinfo("\t%s\n", inqbuf->atap_cmd2_en, ata_cmd_set2);
}
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* device idle:
*
* issue the IDLE IMMEDIATE command to the drive
*/
void
device_idle(int argc, char *argv[])
{
struct atareq req;
if (argc != 1)
goto usage;
memset(&req, 0, sizeof(req));
if (strcmp(argv[0], "idle") == 0)
req.command = WDCC_IDLE_IMMED;
else if (strcmp(argv[0], "standby") == 0)
req.command = WDCC_STANDBY_IMMED;
else
req.command = WDCC_SLEEP;
req.timeout = 1000;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* SECURITY SET PASSWORD command
*/
void
device_sec_setpass(int argc, char *argv[])
{
struct atareq req;
struct sec_password pwd;
char *pass, inbuf[DEV_BSIZE];
struct ataparams *inqbuf = (struct ataparams *)inbuf;
if (argc < 2)
goto usage;
memset(&pwd, 0, sizeof(pwd));
if (strcmp(argv[1], "user") == 0 && argc == 3)
pwd.ctrl |= SEC_PASSWORD_USER;
else if (strcmp(argv[1], "master") == 0 && argc == 2)
pwd.ctrl |= SEC_PASSWORD_MASTER;
else
goto usage;
if (argc == 3) {
if (strcmp(argv[2], "high") == 0)
pwd.ctrl |= SEC_LEVEL_HIGH;
else if (strcmp(argv[2], "maximum") == 0)
pwd.ctrl |= SEC_LEVEL_MAX;
else
goto usage;
}
/*
* Issue IDENTIFY command to obtain master password
* revision code and decrement its value.
* The valid revision codes are 0x0001 through 0xfffe.
* If the device returns 0x0000 or 0xffff as a revision
* code then the master password revision code is not
* supported so don't touch it.
*/
memset(&inbuf, 0, sizeof(inbuf));
memset(&req, 0, sizeof(req));
req.command = WDCC_IDENTIFY;
req.timeout = 1000;
req.flags = ATACMD_READ;
req.databuf = (caddr_t)inbuf;
req.datalen = sizeof(inbuf);
ata_command(&req);
pwd.revision = inqbuf->atap_mpasswd_rev;
if (pwd.revision != 0 && pwd.revision != 0xffff && --pwd.revision == 0)
pwd.revision = 0xfffe;
pass = sec_getpass(pwd.ctrl & SEC_PASSWORD_MASTER, 1);
memcpy(pwd.password, pass, strlen(pass));
memset(&req, 0, sizeof(req));
req.command = ATA_SEC_SET_PASSWORD;
req.timeout = 1000;
req.flags = ATACMD_WRITE;
req.databuf = (caddr_t)&pwd;
req.datalen = sizeof(pwd);
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s user high | maximum\n",
__progname, argv[0]);
fprintf(stderr, "usage: %s <device> %s master\n", __progname, argv[0]);
exit(1);
}
/*
* SECURITY UNLOCK command
*/
void
device_sec_unlock(int argc, char *argv[])
{
struct atareq req;
struct sec_password pwd;
char *pass;
if (argc != 2)
goto usage;
memset(&pwd, 0, sizeof(pwd));
if (strcmp(argv[1], "user") == 0)
pwd.ctrl |= SEC_PASSWORD_USER;
else if (strcmp(argv[1], "master") == 0)
pwd.ctrl |= SEC_PASSWORD_MASTER;
else
goto usage;
pass = sec_getpass(pwd.ctrl & SEC_PASSWORD_MASTER, 0);
memcpy(pwd.password, pass, strlen(pass));
memset(&req, 0, sizeof(req));
req.command = ATA_SEC_UNLOCK;
req.timeout = 1000;
req.flags = ATACMD_WRITE;
req.databuf = (caddr_t)&pwd;
req.datalen = sizeof(pwd);
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s user | master\n", __progname,
argv[0]);
exit(1);
}
/*
* SECURITY ERASE UNIT command
*/
void
device_sec_erase(int argc, char *argv[])
{
struct atareq req;
struct sec_password pwd;
char *pass;
if (argc < 2)
goto usage;
memset(&pwd, 0, sizeof(pwd));
if (strcmp(argv[1], "user") == 0)
pwd.ctrl |= SEC_PASSWORD_USER;
else if (strcmp(argv[1], "master") == 0)
pwd.ctrl |= SEC_PASSWORD_MASTER;
else
goto usage;
if (argc == 2)
pwd.ctrl |= SEC_ERASE_NORMAL;
else if (argc == 3 && strcmp(argv[2], "enhanced") == 0)
pwd.ctrl |= SEC_ERASE_ENHANCED;
else
goto usage;
pass = sec_getpass(pwd.ctrl & SEC_PASSWORD_MASTER, 0);
memcpy(pwd.password, pass, strlen(pass));
/* Issue SECURITY ERASE PREPARE command before */
memset(&req, 0, sizeof(req));
req.command = ATA_SEC_ERASE_PREPARE;
req.timeout = 1000;
ata_command(&req);
memset(&req, 0, sizeof(req));
req.command = ATA_SEC_ERASE_UNIT;
req.timeout = 1000;
req.flags = ATACMD_WRITE;
req.databuf = (caddr_t)&pwd;
req.datalen = sizeof(pwd);
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s user | master [enhanced]\n",
__progname, argv[0]);
exit(1);
}
/*
* SECURITY FREEZE LOCK command
*/
void
device_sec_freeze(int argc, char *argv[])
{
struct atareq req;
if (argc != 1)
goto usage;
memset(&req, 0, sizeof(req));
req.command = ATA_SEC_FREEZE_LOCK;
req.timeout = 1000;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* SECURITY DISABLE PASSWORD command
*/
void
device_sec_disablepass(int argc, char *argv[])
{
struct atareq req;
struct sec_password pwd;
char *pass;
if (argc != 2)
goto usage;
memset(&pwd, 0, sizeof(pwd));
if (strcmp(argv[1], "user") == 0)
pwd.ctrl |= SEC_PASSWORD_USER;
else if (strcmp(argv[1], "master") == 0)
pwd.ctrl |= SEC_PASSWORD_MASTER;
else
goto usage;
pass = sec_getpass(pwd.ctrl & SEC_PASSWORD_MASTER, 0);
memcpy(pwd.password, pass, strlen(pass));
memset(&req, 0, sizeof(req));
req.command = ATA_SEC_DISABLE_PASSWORD;
req.timeout = 1000;
req.flags = ATACMD_WRITE;
req.databuf = (caddr_t)&pwd;
req.datalen = sizeof(pwd);
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s user | master\n", __progname,
argv[0]);
exit(1);
}
char *
sec_getpass(int ident, int confirm)
{
char *pass, buf[33];
if ((pass = getpass(ident ? "Master password:" :
"User password:")) == NULL)
err(1, "getpass()");
if (strlen(pass) > 32)
errx(1, "password too long");
if (confirm) {
strlcpy(buf, pass, sizeof(buf));
if ((pass = getpass(ident ? "Retype master password:" :
"Retype user password:")) == NULL)
err(1, "getpass()");
if (strcmp(pass, buf) != 0)
errx(1, "password mismatch");
}
return (pass);
}
/*
* SMART ENABLE OPERATIONS command
*/
void
device_smart_enable(int argc, char *argv[])
{
struct atareq req;
if (argc != 1)
goto usage;
memset(&req, 0, sizeof(req));
req.command = ATAPI_SMART;
req.cylinder = 0xc24f;
req.timeout = 1000;
req.features = ATA_SMART_EN;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* SMART DISABLE OPERATIONS command
*/
void
device_smart_disable(int argc, char *argv[])
{
struct atareq req;
if (argc != 1)
goto usage;
memset(&req, 0, sizeof(req));
req.command = ATAPI_SMART;
req.cylinder = 0xc24f;
req.timeout = 1000;
req.features = ATA_SMART_DS;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* SMART STATUS command
*/
void
device_smart_status(int argc, char *argv[])
{
struct atareq req;
if (argc != 1)
goto usage;
memset(&req, 0, sizeof(req));
req.command = ATAPI_SMART;
req.cylinder = 0xc24f;
req.timeout = 1000;
req.features = ATA_SMART_STATUS;
ata_command(&req);
if (req.cylinder == 0xc24f)
printf("No SMART threshold exceeded\n");
else if (req.cylinder == 0x2cf4) {
errx(2, "SMART threshold exceeded!");
} else {
errx(1, "Unknown response %02x!", req.cylinder);
}
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* SMART ENABLE/DISABLE ATTRIBUTE AUTOSAVE command
*/
void
device_smart_autosave(int argc, char *argv[])
{
struct atareq req;
int val;
if (argc != 2)
goto usage;
memset(&req, 0, sizeof(req));
req.command = ATAPI_SMART;
req.cylinder = 0xc24f;
req.timeout = 1000;
req.features = ATA_SMART_AUTOSAVE;
if ((val = strtoval(argv[1], smart_autosave)) == -1)
goto usage;
req.sec_num = val;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s enable | disable\n", __progname,
argv[0]);
exit(1);
}
/*
* SMART EXECUTE OFF-LINE IMMEDIATE command
*/
void
device_smart_offline(int argc, char *argv[])
{
struct atareq req;
int val;
if (argc != 2)
goto usage;
memset(&req, 0, sizeof(req));
req.command = ATAPI_SMART;
req.cylinder = 0xc24f;
req.timeout = 1000;
req.features = ATA_SMART_OFFLINE;
if ((val = strtoval(argv[1], smart_offline)) == -1)
goto usage;
req.sec_num = val;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s <subcommand>\n", __progname,
argv[0]);
exit(1);
}
/*
* SMART READ DATA command
*/
void
device_smart_read(int argc, char *argv[])
{
struct atareq req;
struct smart_read data;
if (argc != 1)
goto usage;
memset(&req, 0, sizeof(req));
memset(&data, 0, sizeof(data));
req.command = ATAPI_SMART;
req.cylinder = 0xc24f;
req.timeout = 1000;
req.features = ATA_SMART_READ;
req.flags = ATACMD_READ;
req.databuf = (caddr_t)&data;
req.datalen = sizeof(data);
ata_command(&req);
if (smart_cksum((u_int8_t *)&data, sizeof(data)) != 0)
errx(1, "Checksum mismatch");
printf("Off-line data collection:\n");
printf(" status: %s\n",
valtostr(data.offstat & 0x7f, smart_offstat));
printf(" activity completion time: %d seconds\n",
letoh16(data.time));
printf(" capabilities:\n");
print_bitinfo("\t%s\n", data.offcap, smart_offcap);
printf("Self-test execution:\n");
printf(" status: %s\n", valtostr(SMART_SELFSTAT_STAT(data.selfstat),
smart_selfstat));
if (SMART_SELFSTAT_STAT(data.selfstat) == SMART_SELFSTAT_PROGRESS)
printf("remains %d%% of total time\n",
SMART_SELFSTAT_PCNT(data.selfstat));
printf(" recommended polling time:\n");
printf("\tshort routine: %d minutes\n", data.shtime);
printf("\textended routine: %d minutes\n", data.extime);
printf("SMART capabilities:\n");
print_bitinfo(" %s\n", letoh16(data.smartcap), smart_smartcap);
printf("Error logging: ");
if (data.errcap & SMART_ERRCAP_ERRLOG)
printf("supported\n");
else
printf("not supported\n");
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* SMART READ LOG command
*/
void
device_smart_readlog(int argc, char *argv[])
{
struct atareq req;
int val;
u_int8_t inbuf[DEV_BSIZE];
if (argc != 2)
goto usage;
memset(&req, 0, sizeof(req));
memset(&inbuf, 0, sizeof(inbuf));
req.command = ATAPI_SMART;
req.cylinder = 0xc24f;
req.timeout = 1000;
req.features = ATA_SMART_READLOG;
req.flags = ATACMD_READ;
req.sec_count = 1;
req.databuf = (caddr_t)inbuf;
req.datalen = sizeof(inbuf);
if ((val = strtoval(argv[1], smart_readlog)) == -1)
goto usage;
req.sec_num = val;
ata_command(&req);
if (strcmp(argv[1], "directory") == 0) {
struct smart_log_dir *data = (struct smart_log_dir *)inbuf;
int i;
if (data->version != SMART_LOG_MSECT) {
printf("Device doesn't support multi-sector logs\n");
return;
}
for (i = 0; i < 255; i++)
printf("Log address %d: %d sectors\n", i + 1,
data->entry[i].sec_num);
} else if (strcmp(argv[1], "summary") == 0) {
struct smart_log_sum *data = (struct smart_log_sum *)inbuf;
int i, n, nerr;
if (smart_cksum(inbuf, sizeof(inbuf)) != 0)
errx(1, "Checksum mismatch");
if (data->index == 0) {
printf("No log entries\n");
return;
}
nerr = letoh16(data->err_cnt);
printf("Error count: %d\n\n", nerr);
/*
* Five error log data structures form a circular
* buffer. data->index points to the most recent
* record and err_cnt contains total error number.
* We pass from the most recent record to the
* latest one.
*/
i = data->index - 1;
n = 0;
do {
printf("Error %d:\n", n + 1);
smart_print_errdata(&data->errdata[i--]);
if (i == -1)
i = 4;
} while (++n < (nerr > 5 ? 5 : nerr));
} else if (strcmp(argv[1], "comp") == 0) {
struct smart_log_comp *data = (struct smart_log_comp *)inbuf;
u_int8_t *newbuf;
int i, n, nerr, nsect;
if (smart_cksum(inbuf, sizeof(inbuf)) != 0)
errx(1, "Checksum mismatch");
if (data->index == 0) {
printf("No log entries\n");
return;
}
i = data->index - 1;
nerr = letoh16(data->err_cnt);
printf("Error count: %d\n", nerr);
/*
* From the first sector we obtain total error number
* and calculate necessary number of sectors to read.
* All read error data structures form a circular
* buffer and we pass from the most recent record to
* the latest one.
*/
nsect = nerr / 5 + (nerr % 5 != 0 ? 1 : 0);
if ((newbuf = (u_int8_t *)calloc(nsect, DEV_BSIZE)) == NULL)
err(1, "calloc()");
memset(&req, 0, sizeof(req));
req.flags = ATACMD_READ;
req.command = ATAPI_SMART;
req.features = ATA_SMART_READLOG;
req.sec_count = nsect;
req.sec_num = SMART_READLOG_COMP;
req.cylinder = 0xc24f;
req.databuf = (caddr_t)newbuf;
req.datalen = nsect * DEV_BSIZE;
req.timeout = 1000;
ata_command(&req);
n = 0;
data = (struct smart_log_comp *)
(newbuf + (nsect - 1) * DEV_BSIZE);
do {
printf("Error %d:\n", n + 1);
smart_print_errdata(&data->errdata[i-- % 5]);
if (i == -1)
i = 254;
if (i % 5 == 4)
data = (struct smart_log_comp *)
(newbuf + (i / 5) * DEV_BSIZE);
} while (++n < nerr);
} else if (strcmp(argv[1], "selftest") == 0) {
struct smart_log_self *data = (struct smart_log_self *)inbuf;
int i, n;
if (smart_cksum(inbuf, sizeof(inbuf)) != 0)
errx(1, "Checksum mismatch");
if (data->index == 0) {
printf("No log entries\n");
return;
}
/* circular buffer of 21 entries */
i = data->index - 1;
n = 0;
do {
/* don't print empty entries */
if ((data->desc[i].time1 | data->desc[i].time2) == 0)
break;
printf("Test %d\n", n + 1);
printf(" LBA Low: 0x%x\n", data->desc[i].reg_lbalo);
printf(" status: %s\n",
valtostr(SMART_SELFSTAT_STAT(
data->desc[i].selfstat),
smart_selfstat));
printf(" timestamp: %d\n",
MAKEWORD(data->desc[i].time1,
data->desc[i].time2));
printf(" failure checkpoint byte: 0x%x\n",
data->desc[i].chkpnt);
printf(" failing LBA: 0x%x\n",
MAKEDWORD(data->desc[i].lbafail1,
data->desc[i].lbafail2,
data->desc[i].lbafail3,
data->desc[i].lbafail4));
if (--i == -1)
i = 20;
} while (++n < 21);
}
return;
usage:
fprintf(stderr, "usage: %s <device> %s <log>\n", __progname, argv[0]);
exit(1);
}
#define SMART_PRINTREG(str, reg) \
printf(str "0x%02x\t0x%02x\t0x%02x\t0x%02x\t0x%02x\n", \
data->cmd[0].reg, \
data->cmd[1].reg, \
data->cmd[2].reg, \
data->cmd[3].reg, \
data->cmd[4].reg)
void
smart_print_errdata(struct smart_log_errdata *data)
{
printf(" error register: 0x%x\n", data->err.reg_err);
printf(" sector count register: 0x%x\n", data->err.reg_seccnt);
printf(" LBA Low register: 0x%x\n", data->err.reg_lbalo);
printf(" LBA Mid register: 0x%x\n", data->err.reg_lbamid);
printf(" LBA High register: 0x%x\n", data->err.reg_lbahi);
printf(" device register: 0x%x\n", data->err.reg_dev);
printf(" status register: 0x%x\n", data->err.reg_stat);
printf(" state: %s\n", valtostr(data->err.state, smart_logstat));
printf(" timestamp: %d\n", MAKEWORD(data->err.time1,
data->err.time2));
printf(" history:\n");
SMART_PRINTREG("\tcontrol register:\t", reg_ctl);
SMART_PRINTREG("\tfeatures register:\t", reg_feat);
SMART_PRINTREG("\tsector count register:\t", reg_seccnt);
SMART_PRINTREG("\tLBA Low register:\t", reg_lbalo);
SMART_PRINTREG("\tLBA Mid register:\t", reg_lbamid);
SMART_PRINTREG("\tLBA High register:\t", reg_lbahi);
SMART_PRINTREG("\tdevice register:\t", reg_dev);
SMART_PRINTREG("\tcommand register:\t", reg_cmd);
printf("\ttimestamp:\t\t"
"%d\t%d\t%d\t%d\t%d\n",
MAKEDWORD(data->cmd[0].time1, data->cmd[0].time2,
data->cmd[0].time3, data->cmd[0].time4),
MAKEDWORD(data->cmd[1].time1, data->cmd[1].time2,
data->cmd[1].time3, data->cmd[1].time4),
MAKEDWORD(data->cmd[2].time1, data->cmd[2].time2,
data->cmd[2].time3, data->cmd[2].time4),
MAKEDWORD(data->cmd[3].time1, data->cmd[3].time2,
data->cmd[3].time3, data->cmd[3].time4),
MAKEDWORD(data->cmd[4].time1, data->cmd[4].time2,
data->cmd[4].time3, data->cmd[4].time4));
}
int
smart_cksum(u_int8_t *data, size_t len)
{
u_int8_t sum = 0;
size_t i;
for (i = 0; i < len; i++)
sum += data[i];
return (sum);
}
/*
* Read device attributes
*/
void
device_attr(int argc, char *argv[])
{
struct atareq req;
struct smart_read attr_val;
struct smart_threshold attr_thr;
struct attribute *attr;
struct threshold *thr;
const char *attr_name;
static const char hex[]="0123456789abcdef";
char raw[13], *format;
int i, k, threshold_exceeded = 0;
if (argc != 1)
goto usage;
memset(&req, 0, sizeof(req));
memset(&attr_val, 0, sizeof(attr_val)); /* XXX */
memset(&attr_thr, 0, sizeof(attr_thr)); /* XXX */
req.command = ATAPI_SMART;
req.cylinder = 0xc24f; /* LBA High = C2h, LBA Mid = 4Fh */
req.timeout = 1000;
req.features = ATA_SMART_READ;
req.flags = ATACMD_READ;
req.databuf = (caddr_t)&attr_val;
req.datalen = sizeof(attr_val);
ata_command(&req);
req.features = ATA_SMART_THRESHOLD;
req.flags = ATACMD_READ;
req.databuf = (caddr_t)&attr_thr;
req.datalen = sizeof(attr_thr);
ata_command(&req);
if (attr_val.revision != attr_thr.revision) {
/*
* Non standard vendor implementation.
* Return, since we don't know how to use this.
*/
return;
}
attr = attr_val.attribute;
thr = attr_thr.threshold;
printf("Attributes table revision: %d\n", attr_val.revision);
printf("ID\tAttribute name\t\t\tThreshold\tValue\tRaw\n");
for (i = 0; i < 30; i++) {
if (thr[i].id != 0 && thr[i].id == attr[i].id) {
attr_name = valtostr(thr[i].id, ibm_attr_names);
if (attr_name == NULL)
attr_name = "Unknown";
for (k = 0; k < 6; k++) {
u_int8_t b;
b = attr[i].raw[6 - k];
raw[k + k] = hex[b >> 4];
raw[k + k + 1] = hex[b & 0x0f];
}
raw[k + k] = '\0';
if (thr[i].value >= attr[i].value) {
++threshold_exceeded;
format = "%3d *%-32.32s %3d\t\t%3d\t0x%s\n";
} else {
format = "%3d\t%-32.32s %3d\t\t%3d\t0x%s\n";
}
printf(format, thr[i].id, attr_name,
thr[i].value, attr[i].value, raw);
}
}
if (threshold_exceeded)
fprintf(stderr, "One or more threshold values exceeded!\n");
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* Set the automatic acoustic management on the disk.
*/
void
device_acoustic(int argc, char *argv[])
{
u_char acoustic;
struct atareq req;
const char *errstr;
if (argc != 2)
goto usage;
acoustic = strtonum(argv[1], 0, 126, &errstr);
if (errstr)
errx(1, "Acoustic management value \"%s\" is %s "
"(valid values: 0 - 126)", argv[1], errstr);
memset(&req, 0, sizeof(req));
req.sec_count = acoustic + 0x80;
req.command = SET_FEATURES ;
req.features = WDSF_AAM_EN ;
req.timeout = 1000;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s <acoustic-management-value>\n",
__progname, argv[0]);
exit(1);
}
/*
* Set the advanced power managmement on the disk. Power management
* levels are translated from user-range 0-253 to ATAPI levels 1-0xFD
* to keep a uniform interface to the user.
*/
void
device_apm(int argc, char *argv[])
{
u_char power;
struct atareq req;
const char *errstr;
if (argc != 2)
goto usage;
power = strtonum(argv[1], 0, 253, &errstr);
if (errstr)
errx(1, "Advanced power management value \"%s\" is %s "
"(valid values: 0 - 253)", argv[1], errstr);
memset(&req, 0, sizeof(req));
req.sec_count = power + 0x01;
req.command = SET_FEATURES ;
req.features = WDSF_APM_EN ;
req.timeout = 1000;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s <power-management-level>\n",
__progname, argv[0]);
exit(1);
}
/*
* En/disable features (the automatic acoustic managmement, Advanced Power
* Management) on the disk.
*/
void
device_feature(int argc, char *argv[])
{
struct atareq req;
if (argc != 1)
goto usage;
memset(&req, 0, sizeof(req));
req.command = SET_FEATURES ;
if (strcmp(argv[0], "acousticdisable") == 0)
req.features = WDSF_AAM_DS;
else if (strcmp(argv[0], "readaheadenable") == 0)
req.features = WDSF_READAHEAD_EN;
else if (strcmp(argv[0], "readaheaddisable") == 0)
req.features = WDSF_READAHEAD_DS;
else if (strcmp(argv[0], "writecacheenable") == 0)
req.features = WDSF_EN_WR_CACHE;
else if (strcmp(argv[0], "writecachedisable") == 0)
req.features = WDSF_WRITE_CACHE_DS;
else if (strcmp(argv[0], "apmdisable") == 0)
req.features = WDSF_APM_DS;
else if (strcmp(argv[0], "podenable") == 0)
req.features = WDSF_POD_EN;
else if (strcmp(argv[0], "poddisable") == 0)
req.features = WDSF_POD_DS;
else if (strcmp(argv[0], "puisenable") == 0)
req.features = WDSF_PUIS_EN;
else if (strcmp(argv[0], "puisdisable") == 0)
req.features = WDSF_PUIS_DS;
else if (strcmp(argv[0], "puisspinup") == 0)
req.features = WDSF_PUIS_SPINUP;
else
goto usage;
req.timeout = 1000;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
/*
* Set the idle timer on the disk. Set it for either idle mode or
* standby mode, depending on how we were invoked.
*/
void
device_setidle(int argc, char *argv[])
{
unsigned long idle;
struct atareq req;
char *end;
if (argc != 2)
goto usage;
idle = strtoul(argv[1], &end, 0);
if (*end != '\0' || idle > 19800)
errx(1, "Invalid idle time: \"%s\" "
"(valid values: 1 - 19800)", argv[1]);
if (idle != 0 && idle < 5)
errx(1, "Idle timer must be at least 5 seconds");
memset(&req, 0, sizeof(req));
if (idle <= 240 * 5)
req.sec_count = idle / 5;
else
req.sec_count = idle / (30 * 60) + 240;
if (strcmp(argv[0], "setstandby") == 0)
req.command = WDCC_STANDBY;
else if (strcmp(argv[0], "setidle") == 0)
req.command = WDCC_IDLE;
else
goto usage;
req.timeout = 1000;
ata_command(&req);
return;
usage:
fprintf(stderr, "usage: %s <device> %s <idle-time>\n", __progname,
argv[0]);
exit(1);
}
/*
* Query the device for the current power mode
*/
void
device_checkpower(int argc, char *argv[])
{
struct atareq req;
if (argc != 1)
goto usage;
memset(&req, 0, sizeof(req));
req.command = WDCC_CHECK_PWR;
req.timeout = 1000;
req.flags = ATACMD_READREG;
ata_command(&req);
printf("Current power status: ");
switch (req.sec_count) {
case 0x00:
printf("Standby mode\n");
break;
case 0x80:
printf("Idle mode\n");
break;
case 0xff:
printf("Active mode\n");
break;
default:
printf("Unknown power code (%02x)\n", req.sec_count);
}
return;
usage:
fprintf(stderr, "usage: %s <device> %s\n", __progname, argv[0]);
exit(1);
}
|