-
Notifications
You must be signed in to change notification settings - Fork 50
/
ataprint.cpp
4600 lines (4144 loc) · 162 KB
/
ataprint.cpp
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
/*
* ataprint.cpp
*
* Home page of code is: https://www.smartmontools.org
*
* Copyright (C) 2002-11 Bruce Allen
* Copyright (C) 2008-22 Christian Franke
* Copyright (C) 1999-2000 Michael Cornwell <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#define __STDC_FORMAT_MACROS 1 // enable PRI* for C++
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "atacmdnames.h"
#include "atacmds.h"
#include "ataidentify.h"
#include "dev_interface.h"
#include "ataprint.h"
#include "smartctl.h"
#include "sg_unaligned.h"
#include "utility.h"
#include "knowndrives.h"
const char * ataprint_cpp_cvsid = "$Id$"
ATAPRINT_H_CVSID;
static const char * infofound(const char *output) {
return (*output ? output : "[No Information Found]");
}
// Return true if '-T permissive' is specified,
// used to ignore missing capabilities
static bool is_permissive()
{
if (!failuretest_permissive)
return false;
failuretest_permissive--;
return true;
}
/* For the given Command Register (CR) and Features Register (FR), attempts
* to construct a string that describes the contents of the Status
* Register (ST) and Error Register (ER). If the meanings of the flags of
* the error register are not known for the given command then it returns an
* empty string.
*
* The meanings of the flags of the error register for all commands are
* described in the ATA spec and could all be supported here in theory.
* Currently, only a few commands are supported (those that have been seen
* to produce errors). If many more are to be added then this function
* should probably be redesigned.
*/
static std::string format_st_er_desc(
unsigned char CR, unsigned char FR,
unsigned char ST, unsigned char ER,
unsigned short SC,
const ata_smart_errorlog_error_struct * lba28_regs,
const ata_smart_exterrlog_error * lba48_regs
)
{
const char *error_flag[8];
int i, print_lba=0, print_sector=0;
// Set of character strings corresponding to different error codes.
// Please keep in alphabetic order if you add more.
const char *abrt = "ABRT"; // ABORTED
const char *amnf = "AMNF"; // ADDRESS MARK NOT FOUND
const char *ccto = "CCTO"; // COMMAND COMPLETION TIMED OUT
const char *eom = "EOM"; // END OF MEDIA
const char *icrc = "ICRC"; // INTERFACE CRC ERROR
const char *idnf = "IDNF"; // ID NOT FOUND
const char *ili = "ILI"; // MEANING OF THIS BIT IS COMMAND-SET SPECIFIC
const char *mc = "MC"; // MEDIA CHANGED
const char *mcr = "MCR"; // MEDIA CHANGE REQUEST
const char *nm = "NM"; // NO MEDIA
const char *obs = "obs"; // OBSOLETE
const char *tk0nf = "TK0NF"; // TRACK 0 NOT FOUND
const char *unc = "UNC"; // UNCORRECTABLE
const char *wp = "WP"; // WRITE PROTECTED
/* If for any command the Device Fault flag of the status register is
* not used then used_device_fault should be set to 0 (in the CR switch
* below)
*/
int uses_device_fault = 1;
/* A value of NULL means that the error flag isn't used */
for (i = 0; i < 8; i++)
error_flag[i] = NULL;
std::string str;
switch (CR) {
case 0x10: // RECALIBRATE
error_flag[2] = abrt;
error_flag[1] = tk0nf;
break;
case 0x20: /* READ SECTOR(S) */
case 0x21: // READ SECTOR(S)
case 0x24: // READ SECTOR(S) EXT
case 0xC4: /* READ MULTIPLE */
case 0x29: // READ MULTIPLE EXT
error_flag[6] = unc;
error_flag[5] = mc;
error_flag[4] = idnf;
error_flag[3] = mcr;
error_flag[2] = abrt;
error_flag[1] = nm;
error_flag[0] = amnf;
print_lba=1;
break;
case 0x22: // READ LONG (with retries)
case 0x23: // READ LONG (without retries)
error_flag[4] = idnf;
error_flag[2] = abrt;
error_flag[0] = amnf;
print_lba=1;
break;
case 0x2a: // READ STREAM DMA
case 0x2b: // READ STREAM PIO
if (CR==0x2a)
error_flag[7] = icrc;
error_flag[6] = unc;
error_flag[5] = mc;
error_flag[4] = idnf;
error_flag[3] = mcr;
error_flag[2] = abrt;
error_flag[1] = nm;
error_flag[0] = ccto;
print_lba=1;
print_sector=SC;
break;
case 0x3A: // WRITE STREAM DMA
case 0x3B: // WRITE STREAM PIO
if (CR==0x3A)
error_flag[7] = icrc;
error_flag[6] = wp;
error_flag[5] = mc;
error_flag[4] = idnf;
error_flag[3] = mcr;
error_flag[2] = abrt;
error_flag[1] = nm;
error_flag[0] = ccto;
print_lba=1;
print_sector=SC;
break;
case 0x25: // READ DMA EXT
case 0x26: // READ DMA QUEUED EXT
case 0xC7: // READ DMA QUEUED
case 0xC8: // READ DMA (with retries)
case 0xC9: // READ DMA (without retries, obsolete since ATA-5)
case 0x60: // READ FPDMA QUEUED (NCQ)
error_flag[7] = icrc;
error_flag[6] = unc;
error_flag[5] = mc;
error_flag[4] = idnf;
error_flag[3] = mcr;
error_flag[2] = abrt;
error_flag[1] = nm;
error_flag[0] = amnf;
print_lba=1;
if (CR==0x25 || CR==0xC8)
print_sector=SC;
break;
case 0x30: /* WRITE SECTOR(S) */
case 0x31: // WRITE SECTOR(S)
case 0x34: // WRITE SECTOR(S) EXT
case 0xC5: /* WRITE MULTIPLE */
case 0x39: // WRITE MULTIPLE EXT
case 0xCE: // WRITE MULTIPLE FUA EXT
error_flag[6] = wp;
error_flag[5] = mc;
error_flag[4] = idnf;
error_flag[3] = mcr;
error_flag[2] = abrt;
error_flag[1] = nm;
print_lba=1;
break;
case 0x32: // WRITE LONG (with retries)
case 0x33: // WRITE LONG (without retries)
error_flag[4] = idnf;
error_flag[2] = abrt;
print_lba=1;
break;
case 0x3C: // WRITE VERIFY
error_flag[6] = unc;
error_flag[4] = idnf;
error_flag[2] = abrt;
error_flag[0] = amnf;
print_lba=1;
break;
case 0x40: // READ VERIFY SECTOR(S) with retries
case 0x41: // READ VERIFY SECTOR(S) without retries
case 0x42: // READ VERIFY SECTOR(S) EXT
error_flag[6] = unc;
error_flag[5] = mc;
error_flag[4] = idnf;
error_flag[3] = mcr;
error_flag[2] = abrt;
error_flag[1] = nm;
error_flag[0] = amnf;
print_lba=1;
break;
case 0xA0: /* PACKET */
/* Bits 4-7 are all used for sense key (a 'command packet set specific error
* indication' according to the ATA/ATAPI-7 standard), so "Sense key" will
* be repeated in the error description string if more than one of those
* bits is set.
*/
error_flag[7] = "Sense key (bit 3)",
error_flag[6] = "Sense key (bit 2)",
error_flag[5] = "Sense key (bit 1)",
error_flag[4] = "Sense key (bit 0)",
error_flag[2] = abrt;
error_flag[1] = eom;
error_flag[0] = ili;
break;
case 0xA1: /* IDENTIFY PACKET DEVICE */
case 0xEF: /* SET FEATURES */
case 0x00: /* NOP */
case 0xC6: /* SET MULTIPLE MODE */
error_flag[2] = abrt;
break;
case 0x2F: // READ LOG EXT
error_flag[6] = unc;
error_flag[4] = idnf;
error_flag[2] = abrt;
error_flag[0] = obs;
break;
case 0x3F: // WRITE LOG EXT
error_flag[4] = idnf;
error_flag[2] = abrt;
error_flag[0] = obs;
break;
case 0xB0: /* SMART */
switch(FR) {
case 0xD0: // SMART READ DATA
case 0xD1: // SMART READ ATTRIBUTE THRESHOLDS
case 0xD5: /* SMART READ LOG */
error_flag[6] = unc;
error_flag[4] = idnf;
error_flag[2] = abrt;
error_flag[0] = obs;
break;
case 0xD6: /* SMART WRITE LOG */
error_flag[4] = idnf;
error_flag[2] = abrt;
error_flag[0] = obs;
break;
case 0xD2: // Enable/Disable Attribute Autosave
case 0xD3: // SMART SAVE ATTRIBUTE VALUES (ATA-3)
case 0xD8: // SMART ENABLE OPERATIONS
case 0xD9: /* SMART DISABLE OPERATIONS */
case 0xDA: /* SMART RETURN STATUS */
case 0xDB: // Enable/Disable Auto Offline (SFF)
error_flag[2] = abrt;
break;
case 0xD4: // SMART EXECUTE IMMEDIATE OFFLINE
error_flag[4] = idnf;
error_flag[2] = abrt;
break;
default:
return str; // ""
break;
}
break;
case 0xB1: /* DEVICE CONFIGURATION */
switch (FR) {
case 0xC0: /* DEVICE CONFIGURATION RESTORE */
error_flag[2] = abrt;
break;
default:
return str; // ""
break;
}
break;
case 0xCA: // WRITE DMA (with retries)
case 0xCB: // WRITE DMA (without retries, obsolete since ATA-5)
case 0x35: // WRITE DMA EXT
case 0x3D: // WRITE DMA FUA EXT
case 0xCC: // WRITE DMA QUEUED
case 0x36: // WRITE DMA QUEUED EXT
case 0x3E: // WRITE DMA QUEUED FUA EXT
case 0x61: // WRITE FPDMA QUEUED (NCQ)
error_flag[7] = icrc;
error_flag[6] = wp;
error_flag[5] = mc;
error_flag[4] = idnf;
error_flag[3] = mcr;
error_flag[2] = abrt;
error_flag[1] = nm;
error_flag[0] = amnf;
print_lba=1;
if (CR==0x35)
print_sector=SC;
break;
case 0xE4: // READ BUFFER
case 0xE8: // WRITE BUFFER
error_flag[2] = abrt;
break;
default:
return str; // ""
}
/* We ignore any status flags other than Device Fault and Error */
if (uses_device_fault && (ST & (1 << 5))) {
str = "Device Fault";
if (ST & 1) // Error flag
str += "; ";
}
if (ST & 1) { // Error flag
int count = 0;
str += "Error: ";
for (i = 7; i >= 0; i--)
if ((ER & (1 << i)) && (error_flag[i])) {
if (count++ > 0)
str += ", ";
str += error_flag[i];
}
}
// If the error was a READ or WRITE error, print the Logical Block
// Address (LBA) at which the read or write failed.
if (print_lba) {
// print number of sectors, if known, and append to print string
if (print_sector)
str += strprintf(" %d sectors", print_sector);
if (lba28_regs) {
unsigned lba;
// bits 24-27: bits 0-3 of DH
lba = 0xf & lba28_regs->drive_head;
lba <<= 8;
// bits 16-23: CH
lba |= lba28_regs->cylinder_high;
lba <<= 8;
// bits 8-15: CL
lba |= lba28_regs->cylinder_low;
lba <<= 8;
// bits 0-7: SN
lba |= lba28_regs->sector_number;
str += strprintf(" at LBA = 0x%08x = %u", lba, lba);
}
else if (lba48_regs) {
// This assumes that upper LBA registers are 0 for 28-bit commands
// (TODO: detect 48-bit commands above)
uint64_t lba48;
lba48 = lba48_regs->lba_high_register_hi;
lba48 <<= 8;
lba48 |= lba48_regs->lba_mid_register_hi;
lba48 <<= 8;
lba48 |= lba48_regs->lba_low_register_hi;
lba48 |= lba48_regs->device_register & 0xf;
lba48 <<= 8;
lba48 |= lba48_regs->lba_high_register;
lba48 <<= 8;
lba48 |= lba48_regs->lba_mid_register;
lba48 <<= 8;
lba48 |= lba48_regs->lba_low_register;
str += strprintf(" at LBA = 0x%08" PRIx64 " = %" PRIu64, lba48, lba48);
}
}
return str;
}
static inline std::string format_st_er_desc(
const ata_smart_errorlog_struct * data)
{
return format_st_er_desc(
data->commands[4].commandreg,
data->commands[4].featuresreg,
data->error_struct.status,
data->error_struct.error_register,
data->error_struct.sector_count,
&data->error_struct, (const ata_smart_exterrlog_error *)0);
}
static inline std::string format_st_er_desc(
const ata_smart_exterrlog_error_log * data)
{
return format_st_er_desc(
data->commands[4].command_register,
data->commands[4].features_register,
data->error.status_register,
data->error.error_register,
data->error.count_register_hi << 8 | data->error.count_register,
(const ata_smart_errorlog_error_struct *)0, &data->error);
}
static const char * get_form_factor(unsigned short word168)
{
// Bits 0:3 are the form factor
// Table A.32 of T13/2161-D (ACS-3) Revision 5, October 28, 2013
// Table 247 of T13/BSR INCITS 529 (ACS-4) Revision 20, October 26, 2017
// Table 254 of T13/BSR INCITS 558 (ACS-5) Revision 10, March 3, 2021
switch (word168 & 0xF) {
case 0x1: return "5.25 inches";
case 0x2: return "3.5 inches";
case 0x3: return "2.5 inches";
case 0x4: return "1.8 inches";
case 0x5: return "< 1.8 inches";
case 0x6: return "mSATA"; // ACS-4
case 0x7: return "M.2"; // ACS-4
case 0x8: return "MicroSSD"; // ACS-4
case 0x9: return "CFast"; // ACS-4
default : return 0;
}
}
static int find_msb(unsigned short word)
{
for (int bit = 15; bit >= 0; bit--)
if (word & (1 << bit))
return bit;
return -1;
}
static const char * get_ata_major_version(const ata_identify_device * drive)
{
// Table 13 of T13/1153D (ATA/ATAPI-4) revision 18, August 19, 1998
// Table 29 of T13/1699-D (ATA8-ACS) Revision 6a, September 6, 2008
// Table 55 of T13/BSR INCITS 529 (ACS-4) Revision 20, October 26, 2017
// Table 57 of T13/BSR INCITS 558 (ACS-5) Revision 10, March 3, 2021
switch (find_msb(drive->major_rev_num)) {
case 15: return "ACS >5 (15)";
case 14: return "ACS >5 (14)";
case 13: return "ACS >5 (13)";
case 12: return "ACS-5";
case 11: return "ACS-4";
case 10: return "ACS-3";
case 9: return "ACS-2";
case 8: return "ATA8-ACS";
case 7: return "ATA/ATAPI-7";
case 6: return "ATA/ATAPI-6";
case 5: return "ATA/ATAPI-5";
case 4: return "ATA/ATAPI-4";
case 3: return "ATA-3";
case 2: return "ATA-2";
case 1: return "ATA-1";
default: return 0;
}
}
static const char * get_ata_minor_version(const ata_identify_device * drive)
{
// Table 10 of X3T13/2008D (ATA-3) Revision 7b, January 27, 1997
// Table 28 of T13/1410D (ATA/ATAPI-6) Revision 3b, February 26, 2002
// Table 31 of T13/1699-D (ATA8-ACS) Revision 6a, September 6, 2008
// Table 52 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
// Table 47 of T13/2161-D (ACS-3) Revision 5, October 28, 2013
// Table 57 of T13/BSR INCITS 529 (ACS-4) Revision 20, October 26, 2017
// Table 59 of T13/BSR INCITS 558 (ACS-5) Revision 10, March 3, 2021
switch (drive->minor_rev_num) {
case 0x0001: return "ATA-1 X3T9.2/781D prior to revision 4";
case 0x0002: return "ATA-1 published, ANSI X3.221-1994";
case 0x0003: return "ATA-1 X3T9.2/781D revision 4";
case 0x0004: return "ATA-2 published, ANSI X3.279-1996";
case 0x0005: return "ATA-2 X3T10/948D prior to revision 2k";
case 0x0006: return "ATA-3 X3T10/2008D revision 1";
case 0x0007: return "ATA-2 X3T10/948D revision 2k";
case 0x0008: return "ATA-3 X3T10/2008D revision 0";
case 0x0009: return "ATA-2 X3T10/948D revision 3";
case 0x000a: return "ATA-3 published, ANSI X3.298-1997";
case 0x000b: return "ATA-3 X3T10/2008D revision 6"; // 1st ATA-3 revision with SMART
case 0x000c: return "ATA-3 X3T13/2008D revision 7 and 7a";
case 0x000d: return "ATA/ATAPI-4 X3T13/1153D revision 6";
case 0x000e: return "ATA/ATAPI-4 T13/1153D revision 13";
case 0x000f: return "ATA/ATAPI-4 X3T13/1153D revision 7";
case 0x0010: return "ATA/ATAPI-4 T13/1153D revision 18";
case 0x0011: return "ATA/ATAPI-4 T13/1153D revision 15";
case 0x0012: return "ATA/ATAPI-4 published, ANSI NCITS 317-1998";
case 0x0013: return "ATA/ATAPI-5 T13/1321D revision 3";
case 0x0014: return "ATA/ATAPI-4 T13/1153D revision 14";
case 0x0015: return "ATA/ATAPI-5 T13/1321D revision 1";
case 0x0016: return "ATA/ATAPI-5 published, ANSI NCITS 340-2000";
case 0x0017: return "ATA/ATAPI-4 T13/1153D revision 17";
case 0x0018: return "ATA/ATAPI-6 T13/1410D revision 0";
case 0x0019: return "ATA/ATAPI-6 T13/1410D revision 3a";
case 0x001a: return "ATA/ATAPI-7 T13/1532D revision 1";
case 0x001b: return "ATA/ATAPI-6 T13/1410D revision 2";
case 0x001c: return "ATA/ATAPI-6 T13/1410D revision 1";
case 0x001d: return "ATA/ATAPI-7 published, ANSI INCITS 397-2005";
case 0x001e: return "ATA/ATAPI-7 T13/1532D revision 0";
case 0x001f: return "ACS-3 T13/2161-D revision 3b";
case 0x0021: return "ATA/ATAPI-7 T13/1532D revision 4a";
case 0x0022: return "ATA/ATAPI-6 published, ANSI INCITS 361-2002";
case 0x0027: return "ATA8-ACS T13/1699-D revision 3c";
case 0x0028: return "ATA8-ACS T13/1699-D revision 6";
case 0x0029: return "ATA8-ACS T13/1699-D revision 4";
case 0x0031: return "ACS-2 T13/2015-D revision 2";
case 0x0033: return "ATA8-ACS T13/1699-D revision 3e";
case 0x0039: return "ATA8-ACS T13/1699-D revision 4c";
case 0x0042: return "ATA8-ACS T13/1699-D revision 3f";
case 0x0052: return "ATA8-ACS T13/1699-D revision 3b";
case 0x005e: return "ACS-4 T13/BSR INCITS 529 revision 5";
case 0x006d: return "ACS-3 T13/2161-D revision 5";
case 0x0082: return "ACS-2 published, ANSI INCITS 482-2012";
case 0x009c: return "ACS-4 published, ANSI INCITS 529-2018";
case 0x0107: return "ATA8-ACS T13/1699-D revision 2d";
case 0x010a: return "ACS-3 published, ANSI INCITS 522-2014";
case 0x0110: return "ACS-2 T13/2015-D revision 3";
case 0x011b: return "ACS-3 T13/2161-D revision 4";
default: return 0;
}
}
static const char * get_pata_version(unsigned short word222, char (& buf)[32])
{
// Table 29 of T13/1699-D (ATA8-ACS) Revision 6a, September 6, 2008
// Table 57 of T13/BSR INCITS 558 (ACS-5) Revision 10, March 3, 2021
switch (word222 & 0x0fff) {
default: snprintf(buf, sizeof(buf),
"Unknown (0x%03x)", word222 & 0x0fff); return buf;
case 0x001:
case 0x003: return "ATA8-APT";
case 0x002: return "ATA/ATAPI-7";
}
}
static const char * get_sata_version(unsigned short word222)
{
// Table 29 of T13/1699-D (ATA8-ACS) Revision 6a, September 6, 2008
// Table 50 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
// Table 45 of T13/2161-D (ACS-3) Revision 5, October 28, 2013
// Table 55 of T13/BSR INCITS 529 (ACS-4) Revision 20, October 26, 2017
// Table 57 of T13/BSR INCITS 558 (ACS-5) Revision 10, March 3, 2021
switch (find_msb(word222 & 0x0fff)) {
case 11: return "SATA >3.5 (11)";
case 10: return "SATA 3.5"; // ACS-5
case 9: return "SATA 3.4"; // ACS-5
case 8: return "SATA 3.3"; // ACS-4
case 7: return "SATA 3.2"; // ACS-4
case 6: return "SATA 3.1"; // ACS-3
case 5: return "SATA 3.0"; // ACS-2
case 4: return "SATA 2.6";
case 3: return "SATA 2.5";
case 2: return "SATA II Ext";
case 1: return "SATA 1.0a";
case 0: return "ATA8-AST";
default: return 0;
}
}
static const char * get_sata_speed(int speed)
{
if (speed <= 0)
return 0;
// Table 29 of T13/1699-D (ATA8-ACS) Revision 6a, September 6, 2008
// Table 50 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
// Table 45 of T13/2161-D (ACS-3) Revision 5, October 28, 2013
// Table 57 of T13/BSR INCITS 558 (ACS-5) Revision 10, March 3, 2021
switch (speed) {
default: return ">6.0 Gb/s (7)";
case 6: return ">6.0 Gb/s (6)";
case 5: return ">6.0 Gb/s (5)";
case 4: return ">6.0 Gb/s (4)";
case 3: return "6.0 Gb/s"; // ACS-3
case 2: return "3.0 Gb/s";
case 1: return "1.5 Gb/s"; // ATA8-ACS
}
}
static void jset_sata_speed(const char * key, int value, int speed, const char * str)
{
if (speed <= 0)
return;
json::ref jref = jglb["interface_speed"][key];
jref["sata_value"] = value;
if (str)
jref["string"] = str;
int ups;
switch (speed) {
case 3: ups = 60; break;
case 2: ups = 30; break;
case 1: ups = 15; break;
default: return;
}
jref["units_per_second"] = ups;
jref["bits_per_unit"] = 100000000;
}
static void print_sata_version_and_speed(unsigned short word222,
unsigned short word076,
unsigned short word077)
{
int allspeeds = (!(word076 & 0x0001) ? (word076 & 0x00fe) : 0);
int maxspeed = (allspeeds ? find_msb(allspeeds) : 0);
int curspeed = (!(word077 & 0x0001) ? ((word077 >> 1) & 0x7) : 0);
const char * verstr = get_sata_version(word222);
const char * maxstr = get_sata_speed(maxspeed);
const char * curstr = get_sata_speed(curspeed);
jout("SATA Version is: %s%s%s%s%s%s\n",
(verstr ? verstr : "Unknown"),
(maxstr ? ", " : ""), (maxstr ? maxstr : ""),
(curstr ? " (current: " : ""), (curstr ? curstr : ""),
(curstr ? ")" : ""));
if (verstr)
jglb["sata_version"]["string"] = verstr;
jglb["sata_version"]["value"] = word222 & 0x0fff;
jset_sata_speed("max", allspeeds, maxspeed, maxstr);
jset_sata_speed("current", curspeed, curspeed, curstr);
}
static void print_drive_info(const ata_identify_device * drive,
const ata_size_info & sizes, int rpm,
const drive_settings * dbentry, const char * dbversion)
{
// format drive information (with byte swapping as needed)
char model[40+1], serial[20+1], firmware[8+1];
ata_format_id_string(model, drive->model, sizeof(model)-1);
ata_format_id_string(serial, drive->serial_no, sizeof(serial)-1);
ata_format_id_string(firmware, drive->fw_rev, sizeof(firmware)-1);
// Print model family if known
if (dbentry && *dbentry->modelfamily) {
jout("Model Family: %s\n", dbentry->modelfamily);
jglb["model_family"] = dbentry->modelfamily;
}
jout("Device Model: %s\n", infofound(model));
jglb["model_name"] = model;
if (!dont_print_serial_number) {
jout("Serial Number: %s\n", infofound(serial));
jglb["serial_number"] = serial;
unsigned oui = 0; uint64_t unique_id = 0;
int naa = ata_get_wwn(drive, oui, unique_id);
if (naa >= 0) {
jout("LU WWN Device Id: %x %06x %09" PRIx64 "\n", naa, oui, unique_id);
jglb["wwn"]["naa"] = naa;
jglb["wwn"]["oui"] = oui;
jglb["wwn"]["id"] = unique_id;
}
}
// Additional Product Identifier (OEM Id) string in words 170-173
// (e08130r1, added in ACS-2 Revision 1, December 17, 2008)
if (0x2020 <= drive->words088_255[170-88] && drive->words088_255[170-88] <= 0x7e7e) {
char add[8+1];
ata_format_id_string(add, (const unsigned char *)(drive->words088_255+(170-88)), sizeof(add)-1);
if (add[0]) {
jout("Add. Product Id: %s\n", add);
jglb["ata_additional_product_id"] = add;
}
}
jout("Firmware Version: %s\n", infofound(firmware));
jglb["firmware_version"] = firmware;
if (sizes.capacity) {
// Print capacity
char num[64], cap[32];
jout("User Capacity: %s bytes [%s]\n",
format_with_thousands_sep(num, sizeof(num), sizes.capacity),
format_capacity(cap, sizeof(cap), sizes.capacity));
jglb["user_capacity"]["blocks"].set_unsafe_uint64(sizes.sectors);
jglb["user_capacity"]["bytes"].set_unsafe_uint64(sizes.capacity);
// Print sector sizes.
if (sizes.phy_sector_size == sizes.log_sector_size)
jout("Sector Size: %u bytes logical/physical\n", sizes.log_sector_size);
else {
jout("Sector Sizes: %u bytes logical, %u bytes physical",
sizes.log_sector_size, sizes.phy_sector_size);
if (sizes.log_sector_offset)
pout(" (offset %u bytes)", sizes.log_sector_offset);
jout("\n");
}
jglb["logical_block_size"] = sizes.log_sector_size;
jglb["physical_block_size"] = sizes.phy_sector_size;
}
// Print nominal media rotation rate if reported
if (rpm) {
if (rpm == 1)
jout("Rotation Rate: Solid State Device\n");
else if (rpm > 1)
jout("Rotation Rate: %d rpm\n", rpm);
else
pout("Rotation Rate: Unknown (0x%04x)\n", -rpm);
if (rpm > 0)
jglb["rotation_rate"] = (rpm == 1 ? 0 : rpm);
}
// Print form factor if reported
unsigned short word168 = drive->words088_255[168-88];
if (word168) {
const char * form_factor = get_form_factor(word168);
if (form_factor)
jout("Form Factor: %s\n", form_factor);
else
jout("Form Factor: Unknown (0x%04x)\n", word168);
jglb["form_factor"]["ata_value"] = word168;
if (form_factor)
jglb["form_factor"]["name"] = form_factor;
}
// Print TRIM support
bool trim_sup = !!(drive->words088_255[169-88] & 0x0001);
unsigned short word069 = drive->words047_079[69-47];
bool trim_det = !!(word069 & 0x4000), trim_zeroed = !!(word069 & 0x0020);
if (trim_sup || rpm == 1) // HDD: if supported (SMR), SSD: always
jout("TRIM Command: %s%s%s\n",
(!trim_sup ? "Unavailable" : "Available"),
(!(trim_sup && trim_det) ? "" : ", deterministic"),
(!(trim_sup && trim_zeroed) ? "" : ", zeroed") );
jglb["trim"]["supported"] = trim_sup;
if (trim_sup) {
jglb["trim"]["deterministic"] = trim_det;
jglb["trim"]["zeroed"] = trim_zeroed;
}
// Print Zoned Device Capabilities if reported
// (added in ACS-4, obsoleted in ACS-5)
unsigned short zoned_caps = word069 & 0x3;
if (zoned_caps) {
jout("Zoned Device: %s\n",
(zoned_caps == 0x1 ? "Host Aware Zones" :
zoned_caps == 0x2 ? "Device managed zones" : "Unknown (0x3)"));
if (zoned_caps < 0x3)
jglb["zoned_device"]["capabilities"] = (zoned_caps == 0x1 ? "host_aware" : "device_managed");
}
// See if drive is recognized
jout("Device is: %s%s%s\n",
(dbentry ? "In smartctl database" : "Not in smartctl database"),
(*dbversion ? " " : ""), (*dbversion ? dbversion : ""));
jglb["in_smartctl_database"] = !!dbentry;
// Print ATA version
std::string ataver;
if ( (drive->major_rev_num != 0x0000 && drive->major_rev_num != 0xffff)
|| (drive->minor_rev_num != 0x0000 && drive->minor_rev_num != 0xffff)) {
const char * majorver = get_ata_major_version(drive);
const char * minorver = get_ata_minor_version(drive);
if (majorver && minorver && str_starts_with(minorver, majorver)) {
// Major and minor strings match, print minor string only
ataver = minorver;
}
else {
if (majorver)
ataver = majorver;
else
ataver = strprintf("Unknown(0x%04x)", drive->major_rev_num);
if (minorver)
ataver += strprintf(", %s", minorver);
else if (drive->minor_rev_num != 0x0000 && drive->minor_rev_num != 0xffff)
ataver += strprintf(" (unknown minor revision code: 0x%04x)", drive->minor_rev_num);
else
ataver += " (minor revision not indicated)";
}
}
jout("ATA Version is: %s\n", infofound(ataver.c_str()));
if (!ataver.empty()) {
jglb["ata_version"]["string"] = ataver;
jglb["ata_version"]["major_value"] = drive->major_rev_num;
jglb["ata_version"]["minor_value"] = drive->minor_rev_num;
}
// Print Transport specific version
unsigned short word222 = drive->words088_255[222-88];
if (word222 != 0x0000 && word222 != 0xffff) switch (word222 >> 12) {
case 0x0: // PATA
{
char buf[32] = "";
pout("Transport Type: Parallel, %s\n", get_pata_version(word222, buf));
}
break;
case 0x1: // SATA
print_sata_version_and_speed(word222,
drive->words047_079[76-47],
drive->words047_079[77-47]);
break;
case 0xe: // PCIe (ACS-4)
pout("Transport Type: PCIe (0x%03x)\n", word222 & 0x0fff);
break;
default:
pout("Transport Type: Unknown (0x%04x)\n", word222);
break;
}
jout_startup_datetime("Local Time is: ");
// Print warning message, if there is one
if (dbentry && *dbentry->warningmsg)
pout("\n==> WARNING: %s\n\n", dbentry->warningmsg);
}
static const char *OfflineDataCollectionStatus(unsigned char status_byte)
{
unsigned char stat=status_byte & 0x7f;
switch(stat){
case 0x00:
return "was never started";
case 0x02:
return "was completed without error";
case 0x03:
if (status_byte == 0x03)
return "is in progress";
else
return "is in a Reserved state";
case 0x04:
return "was suspended by an interrupting command from host";
case 0x05:
return "was aborted by an interrupting command from host";
case 0x06:
return "was aborted by the device with a fatal error";
default:
if (stat >= 0x40)
return "is in a Vendor Specific state";
else
return "is in a Reserved state";
}
}
// prints verbose value Off-line data collection status byte
static void PrintSmartOfflineStatus(const ata_smart_values * data)
{
json::ref jref = jglb["ata_smart_data"]["offline_data_collection"]["status"];
jout("Offline data collection status: (0x%02x)\t",
(int)data->offline_data_collection_status);
jref["value"] = data->offline_data_collection_status;
// Off-line data collection status byte is not a reserved
// or vendor specific value
jout("Offline data collection activity\n"
"\t\t\t\t\t%s.\n", OfflineDataCollectionStatus(data->offline_data_collection_status));
jref["string"] = OfflineDataCollectionStatus(data->offline_data_collection_status);
switch (data->offline_data_collection_status & 0x7f) {
case 0x02: jref["passed"] = true; break;
case 0x06: jref["passed"] = false; break;
}
// Report on Automatic Data Collection Status. Only IBM documents
// this bit. See SFF 8035i Revision 2 for details.
if (data->offline_data_collection_status & 0x80)
pout("\t\t\t\t\tAuto Offline Data Collection: Enabled.\n");
else
pout("\t\t\t\t\tAuto Offline Data Collection: Disabled.\n");
return;
}
static void PrintSmartSelfExecStatus(const ata_smart_values * data,
firmwarebug_defs firmwarebugs)
{
unsigned char status = data->self_test_exec_status;
jout("Self-test execution status: ");
switch (data->self_test_exec_status >> 4) {
case 0:
jout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t", status);
jout("without error or no self-test has ever \n\t\t\t\t\tbeen run.\n");
break;
case 1:
jout("(%4d)\tThe self-test routine was aborted by\n\t\t\t\t\t", status);
jout("the host.\n");
break;
case 2:
jout("(%4d)\tThe self-test routine was interrupted\n\t\t\t\t\t", status);
jout("by the host with a hard or soft reset.\n");
break;
case 3:
jout("(%4d)\tA fatal error or unknown test error\n\t\t\t\t\t", status);
jout("occurred while the device was executing\n\t\t\t\t\t");
jout("its self-test routine and the device \n\t\t\t\t\t");
jout("was unable to complete the self-test \n\t\t\t\t\t");
jout("routine.\n");
break;
case 4:
jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
jout("a test element that failed and the test\n\t\t\t\t\t");
jout("element that failed is not known.\n");
break;
case 5:
jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
jout("the electrical element of the test\n\t\t\t\t\t");
jout("failed.\n");
break;
case 6:
jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
jout("the servo (and/or seek) element of the \n\t\t\t\t\t");
jout("test failed.\n");
break;
case 7:
jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
jout("the read element of the test failed.\n");
break;
case 8:
jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
jout("a test element that failed and the\n\t\t\t\t\t");
jout("device is suspected of having handling\n\t\t\t\t\t");
jout("damage.\n");
break;
case 15:
if (firmwarebugs.is_set(BUG_SAMSUNG3) && data->self_test_exec_status == 0xf0) {
pout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t", status);
pout("with unknown result or self-test in\n\t\t\t\t\t");
pout("progress with less than 10%% remaining.\n");
}
else {
jout("(%4d)\tSelf-test routine in progress...\n\t\t\t\t\t", status);
jout("%1d0%% of test remaining.\n", status & 0x0f);
}
break;
default:
jout("(%4d)\tReserved.\n", status);
break;
}
json::ref jref = jglb["ata_smart_data"]["self_test"]["status"];
jref["value"] = status;
const char * msg;
// TODO: Use common function for smartctl/smartd
switch (status >> 4) {
case 0x0: msg = "completed without error"; break;
case 0x1: msg = "was aborted by the host"; break;
case 0x2: msg = "was interrupted by the host with a reset"; break;
case 0x3: msg = "could not complete due to a fatal or unknown error"; break;
case 0x4: msg = "completed with error (unknown test element)"; break;
case 0x5: msg = "completed with error (electrical test element)"; break;
case 0x6: msg = "completed with error (servo/seek test element)"; break;
case 0x7: msg = "completed with error (read test element)"; break;
case 0x8: msg = "completed with error (handling damage?)"; break;
default: msg = 0;
}
if (msg) {
jref["string"] = msg;
switch (status >> 4) {
case 0x1: case 0x2: case 0x3: break; // aborted -> unknown
default: jref["passed"] = ((status >> 4) == 0x0);
}
}
else if ((status >> 4) == 0xf) {
jref["string"] = strprintf("in progress, %u0%% remaining", status & 0xf);
jref["remaining_percent"] = (status & 0xf) * 10;
}
}
static void PrintSmartTotalTimeCompleteOffline (const ata_smart_values * data)
{
jout("Total time to complete Offline \n");
jout("data collection: \t\t(%5d) seconds.\n",
(int)data->total_time_to_complete_off_line);
jglb["ata_smart_data"]["offline_data_collection"]["completion_seconds"] =
data->total_time_to_complete_off_line;
}
static void PrintSmartOfflineCollectCap(const ata_smart_values *data)
{
json::ref jref = jglb["ata_smart_data"]["capabilities"];
jout("Offline data collection\n");
jout("capabilities: \t\t\t (0x%02x) ",
(int)data->offline_data_collection_capability);
jref["values"][0] = data->offline_data_collection_capability;
if (data->offline_data_collection_capability == 0x00){
jout("\tOffline data collection not supported.\n");
}