4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2002-10 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2008-10 Christian Franke <smartmontools-support@lists.sourceforge.net>
8 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * You should have received a copy of the GNU General Public License
16 * (for example COPYING); if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * This code was originally developed as a Senior Thesis by Michael Cornwell
20 * at the Concurrent Systems Laboratory (now part of the Storage Systems
21 * Research Center), Jack Baskin School of Engineering, University of
22 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
35 #endif // #ifdef HAVE_LOCALE_H
38 #include "atacmdnames.h"
40 #include "dev_interface.h"
45 #include "knowndrives.h"
47 const char * ataprint_cpp_cvsid
= "$Id: ataprint.cpp 3081 2010-04-03 19:39:11Z chrfranke $"
50 // for passing global control variables
51 extern smartmonctrl
*con
;
53 static const char * infofound(const char *output
) {
54 return (*output
? output
: "[No Information Found]");
58 /* For the given Command Register (CR) and Features Register (FR), attempts
59 * to construct a string that describes the contents of the Status
60 * Register (ST) and Error Register (ER). The caller passes the string
61 * buffer and the return value is a pointer to this string. If the
62 * meanings of the flags of the error register are not known for the given
63 * command then it returns NULL.
65 * The meanings of the flags of the error register for all commands are
66 * described in the ATA spec and could all be supported here in theory.
67 * Currently, only a few commands are supported (those that have been seen
68 * to produce errors). If many more are to be added then this function
69 * should probably be redesigned.
72 static const char * construct_st_er_desc(
74 unsigned char CR
, unsigned char FR
,
75 unsigned char ST
, unsigned char ER
,
77 const ata_smart_errorlog_error_struct
* lba28_regs
,
78 const ata_smart_exterrlog_error
* lba48_regs
81 const char *error_flag
[8];
82 int i
, print_lba
=0, print_sector
=0;
84 // Set of character strings corresponding to different error codes.
85 // Please keep in alphabetic order if you add more.
86 const char *abrt
= "ABRT"; // ABORTED
87 const char *amnf
= "AMNF"; // ADDRESS MARK NOT FOUND
88 const char *ccto
= "CCTO"; // COMMAND COMPLETION TIMED OUT
89 const char *eom
= "EOM"; // END OF MEDIA
90 const char *icrc
= "ICRC"; // INTERFACE CRC ERROR
91 const char *idnf
= "IDNF"; // ID NOT FOUND
92 const char *ili
= "ILI"; // MEANING OF THIS BIT IS COMMAND-SET SPECIFIC
93 const char *mc
= "MC"; // MEDIA CHANGED
94 const char *mcr
= "MCR"; // MEDIA CHANGE REQUEST
95 const char *nm
= "NM"; // NO MEDIA
96 const char *obs
= "obs"; // OBSOLETE
97 const char *tk0nf
= "TK0NF"; // TRACK 0 NOT FOUND
98 const char *unc
= "UNC"; // UNCORRECTABLE
99 const char *wp
= "WP"; // WRITE PROTECTED
101 /* If for any command the Device Fault flag of the status register is
102 * not used then used_device_fault should be set to 0 (in the CR switch
105 int uses_device_fault
= 1;
107 /* A value of NULL means that the error flag isn't used */
108 for (i
= 0; i
< 8; i
++)
109 error_flag
[i
] = NULL
;
112 case 0x10: // RECALIBRATE
113 error_flag
[2] = abrt
;
114 error_flag
[1] = tk0nf
;
116 case 0x20: /* READ SECTOR(S) */
117 case 0x21: // READ SECTOR(S)
118 case 0x24: // READ SECTOR(S) EXT
119 case 0xC4: /* READ MULTIPLE */
120 case 0x29: // READ MULTIPLE EXT
123 error_flag
[4] = idnf
;
125 error_flag
[2] = abrt
;
127 error_flag
[0] = amnf
;
130 case 0x22: // READ LONG (with retries)
131 case 0x23: // READ LONG (without retries)
132 error_flag
[4] = idnf
;
133 error_flag
[2] = abrt
;
134 error_flag
[0] = amnf
;
137 case 0x2a: // READ STREAM DMA
138 case 0x2b: // READ STREAM PIO
140 error_flag
[7] = icrc
;
143 error_flag
[4] = idnf
;
145 error_flag
[2] = abrt
;
147 error_flag
[0] = ccto
;
151 case 0x3A: // WRITE STREAM DMA
152 case 0x3B: // WRITE STREAM PIO
154 error_flag
[7] = icrc
;
157 error_flag
[4] = idnf
;
159 error_flag
[2] = abrt
;
161 error_flag
[0] = ccto
;
165 case 0x25: /* READ DMA EXT */
166 case 0x26: // READ DMA QUEUED EXT
167 case 0xC7: // READ DMA QUEUED
168 case 0xC8: /* READ DMA */
170 error_flag
[7] = icrc
;
173 error_flag
[4] = idnf
;
175 error_flag
[2] = abrt
;
177 error_flag
[0] = amnf
;
179 if (CR
==0x25 || CR
==0xC8)
182 case 0x30: /* WRITE SECTOR(S) */
183 case 0x31: // WRITE SECTOR(S)
184 case 0x34: // WRITE SECTOR(S) EXT
185 case 0xC5: /* WRITE MULTIPLE */
186 case 0x39: // WRITE MULTIPLE EXT
187 case 0xCE: // WRITE MULTIPLE FUA EXT
190 error_flag
[4] = idnf
;
192 error_flag
[2] = abrt
;
196 case 0x32: // WRITE LONG (with retries)
197 case 0x33: // WRITE LONG (without retries)
198 error_flag
[4] = idnf
;
199 error_flag
[2] = abrt
;
202 case 0x3C: // WRITE VERIFY
204 error_flag
[4] = idnf
;
205 error_flag
[2] = abrt
;
206 error_flag
[0] = amnf
;
209 case 0x40: // READ VERIFY SECTOR(S) with retries
210 case 0x41: // READ VERIFY SECTOR(S) without retries
211 case 0x42: // READ VERIFY SECTOR(S) EXT
214 error_flag
[4] = idnf
;
216 error_flag
[2] = abrt
;
218 error_flag
[0] = amnf
;
221 case 0xA0: /* PACKET */
222 /* Bits 4-7 are all used for sense key (a 'command packet set specific error
223 * indication' according to the ATA/ATAPI-7 standard), so "Sense key" will
224 * be repeated in the error description string if more than one of those
227 error_flag
[7] = "Sense key (bit 3)",
228 error_flag
[6] = "Sense key (bit 2)",
229 error_flag
[5] = "Sense key (bit 1)",
230 error_flag
[4] = "Sense key (bit 0)",
231 error_flag
[2] = abrt
;
235 case 0xA1: /* IDENTIFY PACKET DEVICE */
236 case 0xEF: /* SET FEATURES */
238 case 0xC6: /* SET MULTIPLE MODE */
239 error_flag
[2] = abrt
;
241 case 0x2F: // READ LOG EXT
243 error_flag
[4] = idnf
;
244 error_flag
[2] = abrt
;
247 case 0x3F: // WRITE LOG EXT
248 error_flag
[4] = idnf
;
249 error_flag
[2] = abrt
;
252 case 0xB0: /* SMART */
254 case 0xD0: // SMART READ DATA
255 case 0xD1: // SMART READ ATTRIBUTE THRESHOLDS
256 case 0xD5: /* SMART READ LOG */
258 error_flag
[4] = idnf
;
259 error_flag
[2] = abrt
;
262 case 0xD6: /* SMART WRITE LOG */
263 error_flag
[4] = idnf
;
264 error_flag
[2] = abrt
;
267 case 0xD2: // Enable/Disable Attribute Autosave
268 case 0xD3: // SMART SAVE ATTRIBUTE VALUES (ATA-3)
269 case 0xD8: // SMART ENABLE OPERATIONS
270 case 0xD9: /* SMART DISABLE OPERATIONS */
271 case 0xDA: /* SMART RETURN STATUS */
272 case 0xDB: // Enable/Disable Auto Offline (SFF)
273 error_flag
[2] = abrt
;
275 case 0xD4: // SMART EXECUTE IMMEDIATE OFFLINE
276 error_flag
[4] = idnf
;
277 error_flag
[2] = abrt
;
284 case 0xB1: /* DEVICE CONFIGURATION */
286 case 0xC0: /* DEVICE CONFIGURATION RESTORE */
287 error_flag
[2] = abrt
;
294 case 0xCA: /* WRITE DMA */
296 case 0x35: // WRITE DMA EXT
297 case 0x3D: // WRITE DMA FUA EXT
298 case 0xCC: // WRITE DMA QUEUED
299 case 0x36: // WRITE DMA QUEUED EXT
300 case 0x3E: // WRITE DMA QUEUED FUA EXT
301 error_flag
[7] = icrc
;
304 error_flag
[4] = idnf
;
306 error_flag
[2] = abrt
;
308 error_flag
[0] = amnf
;
313 case 0xE4: // READ BUFFER
314 case 0xE8: // WRITE BUFFER
315 error_flag
[2] = abrt
;
323 /* We ignore any status flags other than Device Fault and Error */
325 if (uses_device_fault
&& (ST
& (1 << 5))) {
326 strcat(s
, "Device Fault");
327 if (ST
& 1) // Error flag
330 if (ST
& 1) { // Error flag
333 strcat(s
, "Error: ");
334 for (i
= 7; i
>= 0; i
--)
335 if ((ER
& (1 << i
)) && (error_flag
[i
])) {
338 strcat(s
, error_flag
[i
]);
342 // If the error was a READ or WRITE error, print the Logical Block
343 // Address (LBA) at which the read or write failed.
346 // print number of sectors, if known, and append to print string
348 snprintf(tmp
, 128, " %d sectors", print_sector
);
354 // bits 24-27: bits 0-3 of DH
355 lba
= 0xf & lba28_regs
->drive_head
;
358 lba
|= lba28_regs
->cylinder_high
;
361 lba
|= lba28_regs
->cylinder_low
;
364 lba
|= lba28_regs
->sector_number
;
365 snprintf(tmp
, 128, " at LBA = 0x%08x = %u", lba
, lba
);
368 else if (lba48_regs
) {
369 // This assumes that upper LBA registers are 0 for 28-bit commands
370 // (TODO: detect 48-bit commands above)
372 lba48
= lba48_regs
->lba_high_register_hi
;
374 lba48
|= lba48_regs
->lba_mid_register_hi
;
376 lba48
|= lba48_regs
->lba_low_register_hi
;
377 lba48
|= lba48_regs
->device_register
& 0xf;
379 lba48
|= lba48_regs
->lba_high_register
;
381 lba48
|= lba48_regs
->lba_mid_register
;
383 lba48
|= lba48_regs
->lba_low_register
;
384 snprintf(tmp
, 128, " at LBA = 0x%08"PRIx64
" = %"PRIu64
, lba48
, lba48
);
392 static inline const char * construct_st_er_desc(char * s
,
393 const ata_smart_errorlog_struct
* data
)
395 return construct_st_er_desc(s
,
396 data
->commands
[4].commandreg
,
397 data
->commands
[4].featuresreg
,
398 data
->error_struct
.status
,
399 data
->error_struct
.error_register
,
400 data
->error_struct
.sector_count
,
401 &data
->error_struct
, (const ata_smart_exterrlog_error
*)0);
404 static inline const char * construct_st_er_desc(char * s
,
405 const ata_smart_exterrlog_error_log
* data
)
407 return construct_st_er_desc(s
,
408 data
->commands
[4].command_register
,
409 data
->commands
[4].features_register
,
410 data
->error
.status_register
,
411 data
->error
.error_register
,
412 data
->error
.count_register_hi
<< 8 | data
->error
.count_register
,
413 (const ata_smart_errorlog_error_struct
*)0, &data
->error
);
417 // This returns the capacity of a disk drive and also prints this into
418 // a string, using comma separators to make it easier to read. If the
419 // drive doesn't support LBA addressing or has no user writable
420 // sectors (eg, CDROM or DVD) then routine returns zero.
421 static uint64_t determine_capacity(const ata_identify_device
* drive
, char * pstring
)
423 // get correct character to use as thousands separator
424 const char *separator
= ",";
426 struct lconv
*currentlocale
=NULL
;
427 setlocale (LC_ALL
, "");
428 currentlocale
=localeconv();
429 if (*(currentlocale
->thousands_sep
))
430 separator
=(char *)currentlocale
->thousands_sep
;
431 #endif // #ifdef HAVE_LOCALE_H
433 // get #sectors and turn into bytes
434 uint64_t capacity
= get_num_sectors(drive
) * 512;
435 uint64_t retval
= capacity
;
437 // print with locale-specific separators (default is comma)
438 int started
=0, k
=1000000000;
439 uint64_t power_of_ten
= k
;
442 for (k
=0; k
<7; k
++) {
443 uint64_t threedigits
= capacity
/power_of_ten
;
444 capacity
-= threedigits
*power_of_ten
;
446 // we have already printed some digits
447 pstring
+= sprintf(pstring
, "%s%03"PRIu64
, separator
, threedigits
);
448 else if (threedigits
|| k
==6) {
449 // these are the first digits that we are printing
450 pstring
+= sprintf(pstring
, "%"PRIu64
, threedigits
);
454 power_of_ten
/= 1000;
460 static bool PrintDriveInfo(const ata_identify_device
* drive
, bool fix_swapped_id
)
462 // format drive information (with byte swapping as needed)
463 char model
[64], serial
[64], firm
[64];
464 format_ata_string(model
, drive
->model
, 40, fix_swapped_id
);
465 format_ata_string(serial
, drive
->serial_no
, 20, fix_swapped_id
);
466 format_ata_string(firm
, drive
->fw_rev
, 8, fix_swapped_id
);
468 // print out model, serial # and firmware versions (byte-swap ASCI strings)
469 const drive_settings
* dbentry
= lookup_drive(model
, firm
);
471 // Print model family if known
472 if (dbentry
&& *dbentry
->modelfamily
)
473 pout("Model Family: %s\n", dbentry
->modelfamily
);
475 pout("Device Model: %s\n", infofound(model
));
476 if (!con
->dont_print_serial
)
477 pout("Serial Number: %s\n", infofound(serial
));
478 pout("Firmware Version: %s\n", infofound(firm
));
481 if (determine_capacity(drive
, capacity
))
482 pout("User Capacity: %s bytes\n", capacity
);
484 // See if drive is recognized
485 pout("Device is: %s\n", !dbentry
?
486 "Not in smartctl database [for details use: -P showall]":
487 "In smartctl database [for details use: -P show]");
489 // now get ATA version info
490 const char *description
; unsigned short minorrev
;
491 int version
= ataVersionInfo(&description
, drive
, &minorrev
);
493 // SMART Support was first added into the ATA/ATAPI-3 Standard with
494 // Revision 3 of the document, July 25, 1995. Look at the "Document
495 // Status" revision commands at the beginning of
496 // http://www.t13.org/Documents/UploadedDocuments/project/d2008r7b-ATA-3.pdf
497 // to see this. So it's not enough to check if we are ATA-3.
498 // Version=-3 indicates ATA-3 BEFORE Revision 3.
499 // Version=0 indicates that no info is found. This may happen if
500 // the OS provides only part of the IDENTIFY data.
502 std::string majorstr
, minorstr
;
504 majorstr
= strprintf("%d", abs(version
));
506 minorstr
= description
;
508 minorstr
= "Exact ATA specification draft version not indicated";
510 minorstr
= strprintf("Not recognized. Minor revision code: 0x%04x", minorrev
);
513 pout("ATA Version is: %s\n", infofound(majorstr
.c_str()));
514 pout("ATA Standard is: %s\n", infofound(minorstr
.c_str()));
516 // print current time and date and timezone
517 char timedatetz
[DATEANDEPOCHLEN
]; dateandtimezone(timedatetz
);
518 pout("Local Time is: %s\n", timedatetz
);
520 // Print warning message, if there is one
521 if (dbentry
&& *dbentry
->warningmsg
)
522 pout("\n==> WARNING: %s\n\n", dbentry
->warningmsg
);
524 if (!version
|| version
>= 3)
527 pout("SMART is only available in ATA Version 3 Revision 3 or greater.\n");
528 pout("We will try to proceed in spite of this.\n");
532 static const char *OfflineDataCollectionStatus(unsigned char status_byte
)
534 unsigned char stat
=status_byte
& 0x7f;
538 return "was never started";
540 return "was completed without error";
542 if (status_byte
== 0x03)
543 return "is in progress";
545 return "is in a Reserved state";
547 return "was suspended by an interrupting command from host";
549 return "was aborted by an interrupting command from host";
551 return "was aborted by the device with a fatal error";
554 return "is in a Vendor Specific state";
556 return "is in a Reserved state";
561 // prints verbose value Off-line data collection status byte
562 static void PrintSmartOfflineStatus(const ata_smart_values
* data
)
564 pout("Offline data collection status: (0x%02x)\t",
565 (int)data
->offline_data_collection_status
);
567 // Off-line data collection status byte is not a reserved
568 // or vendor specific value
569 pout("Offline data collection activity\n"
570 "\t\t\t\t\t%s.\n", OfflineDataCollectionStatus(data
->offline_data_collection_status
));
572 // Report on Automatic Data Collection Status. Only IBM documents
573 // this bit. See SFF 8035i Revision 2 for details.
574 if (data
->offline_data_collection_status
& 0x80)
575 pout("\t\t\t\t\tAuto Offline Data Collection: Enabled.\n");
577 pout("\t\t\t\t\tAuto Offline Data Collection: Disabled.\n");
582 static void PrintSmartSelfExecStatus(const ata_smart_values
* data
,
583 unsigned char fix_firmwarebug
)
585 pout("Self-test execution status: ");
587 switch (data
->self_test_exec_status
>> 4)
590 pout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t",
591 (int)data
->self_test_exec_status
);
592 pout("without error or no self-test has ever \n\t\t\t\t\tbeen run.\n");
595 pout("(%4d)\tThe self-test routine was aborted by\n\t\t\t\t\t",
596 (int)data
->self_test_exec_status
);
600 pout("(%4d)\tThe self-test routine was interrupted\n\t\t\t\t\t",
601 (int)data
->self_test_exec_status
);
602 pout("by the host with a hard or soft reset.\n");
605 pout("(%4d)\tA fatal error or unknown test error\n\t\t\t\t\t",
606 (int)data
->self_test_exec_status
);
607 pout("occurred while the device was executing\n\t\t\t\t\t");
608 pout("its self-test routine and the device \n\t\t\t\t\t");
609 pout("was unable to complete the self-test \n\t\t\t\t\t");
613 pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
614 (int)data
->self_test_exec_status
);
615 pout("a test element that failed and the test\n\t\t\t\t\t");
616 pout("element that failed is not known.\n");
619 pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
620 (int)data
->self_test_exec_status
);
621 pout("the electrical element of the test\n\t\t\t\t\t");
625 pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
626 (int)data
->self_test_exec_status
);
627 pout("the servo (and/or seek) element of the \n\t\t\t\t\t");
628 pout("test failed.\n");
631 pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
632 (int)data
->self_test_exec_status
);
633 pout("the read element of the test failed.\n");
636 pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
637 (int)data
->self_test_exec_status
);
638 pout("a test element that failed and the\n\t\t\t\t\t");
639 pout("device is suspected of having handling\n\t\t\t\t\t");
643 if (fix_firmwarebug
== FIX_SAMSUNG3
&& data
->self_test_exec_status
== 0xf0) {
644 pout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t",
645 (int)data
->self_test_exec_status
);
646 pout("with unknown result or self-test in\n\t\t\t\t\t");
647 pout("progress with less than 10%% remaining.\n");
650 pout("(%4d)\tSelf-test routine in progress...\n\t\t\t\t\t",
651 (int)data
->self_test_exec_status
);
652 pout("%1d0%% of test remaining.\n",
653 (int)(data
->self_test_exec_status
& 0x0f));
657 pout("(%4d)\tReserved.\n",
658 (int)data
->self_test_exec_status
);
664 static void PrintSmartTotalTimeCompleteOffline (const ata_smart_values
* data
)
666 pout("Total time to complete Offline \n");
667 pout("data collection: \t\t (%4d) seconds.\n",
668 (int)data
->total_time_to_complete_off_line
);
671 static void PrintSmartOfflineCollectCap(const ata_smart_values
*data
)
673 pout("Offline data collection\n");
674 pout("capabilities: \t\t\t (0x%02x) ",
675 (int)data
->offline_data_collection_capability
);
677 if (data
->offline_data_collection_capability
== 0x00){
678 pout("\tOffline data collection not supported.\n");
681 pout( "%s\n", isSupportExecuteOfflineImmediate(data
)?
682 "SMART execute Offline immediate." :
683 "No SMART execute Offline immediate.");
685 pout( "\t\t\t\t\t%s\n", isSupportAutomaticTimer(data
)?
686 "Auto Offline data collection on/off support.":
687 "No Auto Offline data collection support.");
689 pout( "\t\t\t\t\t%s\n", isSupportOfflineAbort(data
)?
690 "Abort Offline collection upon new\n\t\t\t\t\tcommand.":
691 "Suspend Offline collection upon new\n\t\t\t\t\tcommand.");
693 pout( "\t\t\t\t\t%s\n", isSupportOfflineSurfaceScan(data
)?
694 "Offline surface scan supported.":
695 "No Offline surface scan supported.");
697 pout( "\t\t\t\t\t%s\n", isSupportSelfTest(data
)?
698 "Self-test supported.":
699 "No Self-test supported.");
701 pout( "\t\t\t\t\t%s\n", isSupportConveyanceSelfTest(data
)?
702 "Conveyance Self-test supported.":
703 "No Conveyance Self-test supported.");
705 pout( "\t\t\t\t\t%s\n", isSupportSelectiveSelfTest(data
)?
706 "Selective Self-test supported.":
707 "No Selective Self-test supported.");
711 static void PrintSmartCapability(const ata_smart_values
*data
)
713 pout("SMART capabilities: ");
714 pout("(0x%04x)\t", (int)data
->smart_capability
);
716 if (data
->smart_capability
== 0x00)
718 pout("Automatic saving of SMART data\t\t\t\t\tis not implemented.\n");
723 pout( "%s\n", (data
->smart_capability
& 0x01)?
724 "Saves SMART data before entering\n\t\t\t\t\tpower-saving mode.":
725 "Does not save SMART data before\n\t\t\t\t\tentering power-saving mode.");
727 if ( data
->smart_capability
& 0x02 )
729 pout("\t\t\t\t\tSupports SMART auto save timer.\n");
734 static void PrintSmartErrorLogCapability(const ata_smart_values
* data
, const ata_identify_device
* identity
)
736 pout("Error logging capability: ");
738 if ( isSmartErrorLogCapable(data
, identity
) )
740 pout(" (0x%02x)\tError logging supported.\n",
741 (int)data
->errorlog_capability
);
744 pout(" (0x%02x)\tError logging NOT supported.\n",
745 (int)data
->errorlog_capability
);
749 static void PrintSmartShortSelfTestPollingTime(const ata_smart_values
* data
)
751 pout("Short self-test routine \n");
752 if (isSupportSelfTest(data
))
753 pout("recommended polling time: \t (%4d) minutes.\n",
754 (int)data
->short_test_completion_time
);
756 pout("recommended polling time: \t Not Supported.\n");
759 static void PrintSmartExtendedSelfTestPollingTime(const ata_smart_values
* data
)
761 pout("Extended self-test routine\n");
762 if (isSupportSelfTest(data
))
763 pout("recommended polling time: \t (%4d) minutes.\n",
764 (int)data
->extend_test_completion_time
);
766 pout("recommended polling time: \t Not Supported.\n");
769 static void PrintSmartConveyanceSelfTestPollingTime(const ata_smart_values
* data
)
771 pout("Conveyance self-test routine\n");
772 if (isSupportConveyanceSelfTest(data
))
773 pout("recommended polling time: \t (%4d) minutes.\n",
774 (int)data
->conveyance_test_completion_time
);
776 pout("recommended polling time: \t Not Supported.\n");
779 // Check SMART attribute table for Threshold failure
780 // onlyfailed=0: are or were any age or prefailure attributes <= threshold
781 // onlyfailed=1: are any prefailure attributes <= threshold now
782 static int find_failed_attr(const ata_smart_values
* data
,
783 const ata_smart_thresholds_pvt
* thresholds
,
784 const ata_vendor_attr_defs
& defs
, int onlyfailed
)
786 for (int i
= 0; i
< NUMBER_ATA_SMART_ATTRIBUTES
; i
++) {
787 const ata_smart_attribute
& attr
= data
->vendor_attributes
[i
];
789 ata_attr_state state
= ata_get_attr_state(attr
,
790 thresholds
->thres_entries
[i
], defs
);
793 if (state
>= ATTRSTATE_FAILED_PAST
)
797 if (state
== ATTRSTATE_FAILED_NOW
&& ATTRIBUTE_FLAGS_PREFAILURE(attr
.flags
))
804 // onlyfailed=0 : print all attribute values
805 // onlyfailed=1: just ones that are currently failed and have prefailure bit set
806 // onlyfailed=2: ones that are failed, or have failed with or without prefailure bit set
807 static void PrintSmartAttribWithThres(const ata_smart_values
* data
,
808 const ata_smart_thresholds_pvt
* thresholds
,
809 const ata_vendor_attr_defs
& defs
,
812 bool needheader
= true;
814 // step through all vendor attributes
815 for (int i
= 0; i
< NUMBER_ATA_SMART_ATTRIBUTES
; i
++) {
816 const ata_smart_attribute
& attr
= data
->vendor_attributes
[i
];
817 const ata_smart_threshold_entry
& thre
= thresholds
->thres_entries
[i
];
819 // Check attribute and threshold
820 ata_attr_state state
= ata_get_attr_state(attr
, thre
, defs
);
821 if (state
== ATTRSTATE_NON_EXISTING
)
824 // These break out of the loop if we are only printing certain entries...
825 if (onlyfailed
== 1 && !(ATTRIBUTE_FLAGS_PREFAILURE(attr
.flags
) && state
== ATTRSTATE_FAILED_NOW
))
828 if (onlyfailed
== 2 && state
< ATTRSTATE_FAILED_PAST
)
831 // print header only if needed
834 pout("SMART Attributes Data Structure revision number: %d\n",(int)data
->revnumber
);
835 pout("Vendor Specific SMART Attributes with Thresholds:\n");
837 pout("ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE\n");
841 // Format value, worst, threshold
842 std::string valstr
, worstr
, threstr
;
843 if (state
> ATTRSTATE_NO_NORMVAL
)
844 valstr
= strprintf("%.3d", attr
.current
);
847 if (!(defs
[attr
.id
].flags
& ATTRFLAG_NO_WORSTVAL
))
848 worstr
= strprintf("%.3d", attr
.worst
);
851 if (state
> ATTRSTATE_NO_THRESHOLD
)
852 threstr
= strprintf("%.3d", thre
.threshold
);
856 // Print line for each valid attribute
857 std::string attrname
= ata_get_smart_attr_name(attr
.id
, defs
);
858 pout("%3d %-24s0x%04x %-3s %-3s %-3s %-10s%-9s%-12s%s\n",
859 attr
.id
, attrname
.c_str(), attr
.flags
,
860 valstr
.c_str(), worstr
.c_str(), threstr
.c_str(),
861 (ATTRIBUTE_FLAGS_PREFAILURE(attr
.flags
)? "Pre-fail" : "Old_age"),
862 (ATTRIBUTE_FLAGS_ONLINE(attr
.flags
)? "Always" : "Offline"),
863 (state
== ATTRSTATE_FAILED_NOW
? "FAILING_NOW" :
864 state
== ATTRSTATE_FAILED_PAST
? "In_the_past" :
866 ata_format_attr_raw_value(attr
, defs
).c_str());
868 // Print a warning if there is inconsistency here
869 if (state
== ATTRSTATE_BAD_THRESHOLD
) {
870 pout("%3d %-24s<== Data Page | WARNING: PREVIOUS ATTRIBUTE HAS TWO\n",
871 attr
.id
, attrname
.c_str());
872 pout("%3d %-24s<== Threshold Page | INCONSISTENT IDENTITIES IN THE DATA\n",
873 thre
.id
, ata_get_smart_attr_name(thre
.id
, defs
).c_str());
876 if (!needheader
) pout("\n");
879 // Print SMART related SCT capabilities
880 static void ataPrintSCTCapability(const ata_identify_device
*drive
)
882 unsigned short sctcaps
= drive
->words088_255
[206-88];
883 if (!(sctcaps
& 0x01))
885 pout("SCT capabilities: \t (0x%04x)\tSCT Status supported.\n", sctcaps
);
887 pout("\t\t\t\t\tSCT Error Recovery Control supported.\n");
889 pout("\t\t\t\t\tSCT Feature Control supported.\n");
891 pout("\t\t\t\t\tSCT Data Table supported.\n");
895 static void PrintGeneralSmartValues(const ata_smart_values
*data
, const ata_identify_device
*drive
,
896 unsigned char fix_firmwarebug
)
898 pout("General SMART Values:\n");
900 PrintSmartOfflineStatus(data
);
902 if (isSupportSelfTest(data
)){
903 PrintSmartSelfExecStatus(data
, fix_firmwarebug
);
906 PrintSmartTotalTimeCompleteOffline(data
);
907 PrintSmartOfflineCollectCap(data
);
908 PrintSmartCapability(data
);
910 PrintSmartErrorLogCapability(data
, drive
);
912 pout( "\t\t\t\t\t%s\n", isGeneralPurposeLoggingCapable(drive
)?
913 "General Purpose Logging supported.":
914 "No General Purpose Logging support.");
916 if (isSupportSelfTest(data
)){
917 PrintSmartShortSelfTestPollingTime (data
);
918 PrintSmartExtendedSelfTestPollingTime (data
);
920 if (isSupportConveyanceSelfTest(data
))
921 PrintSmartConveyanceSelfTestPollingTime (data
);
923 ataPrintSCTCapability(drive
);
928 // Get # sectors of a log addr, 0 if log does not exist.
929 static unsigned GetNumLogSectors(const ata_smart_log_directory
* logdir
, unsigned logaddr
, bool gpl
)
937 unsigned n
= logdir
->entry
[logaddr
-1].numsectors
;
939 // GP logs may have >255 sectors
940 n
|= logdir
->entry
[logaddr
-1].reserved
<< 8;
945 // Table A.2 of T13/1699-D Revision 6
946 static const char * GetLogName(unsigned logaddr
)
949 case 0x00: return "Log Directory";
950 case 0x01: return "Summary SMART error log";
951 case 0x02: return "Comprehensive SMART error log";
952 case 0x03: return "Ext. Comprehensive SMART error log";
953 case 0x04: return "Device Statistics";
954 case 0x06: return "SMART self-test log";
955 case 0x07: return "Extended self-test log";
956 case 0x09: return "Selective self-test log";
957 case 0x10: return "NCQ Command Error";
958 case 0x11: return "SATA Phy Event Counters";
959 case 0x20: return "Streaming performance log"; // Obsolete
960 case 0x21: return "Write stream error log";
961 case 0x22: return "Read stream error log";
962 case 0x23: return "Delayed sector log"; // Obsolete
963 case 0xe0: return "SCT Command/Status";
964 case 0xe1: return "SCT Data Transfer";
966 if (0xa0 <= logaddr
&& logaddr
<= 0xdf)
967 return "Device vendor specific log";
968 if (0x80 <= logaddr
&& logaddr
<= 0x9f)
969 return "Host vendor specific log";
970 if (0x12 <= logaddr
&& logaddr
<= 0x17)
971 return "Reserved for Serial ATA";
977 // Print SMART and/or GP Log Directory
978 static void PrintLogDirectories(const ata_smart_log_directory
* gplogdir
,
979 const ata_smart_log_directory
* smartlogdir
)
982 pout("General Purpose Log Directory Version %u\n", gplogdir
->logversion
);
984 pout("SMART %sLog Directory Version %u%s\n",
985 (gplogdir
? " " : ""), smartlogdir
->logversion
,
986 (smartlogdir
->logversion
==1 ? " [multi-sector log support]" : ""));
988 for (unsigned i
= 0; i
<= 0xff; i
++) {
989 // Get number of sectors
990 unsigned smart_numsect
= GetNumLogSectors(smartlogdir
, i
, false);
991 unsigned gp_numsect
= GetNumLogSectors(gplogdir
, i
, true );
993 if (!(smart_numsect
|| gp_numsect
))
994 continue; // Log does not exist
996 const char * name
= GetLogName(i
);
998 // Print name and length of log.
999 // If both SMART and GP exist, print separate entries if length differ.
1000 if (smart_numsect
== gp_numsect
)
1001 pout( "GP/S Log at address 0x%02x has %4d sectors [%s]\n", i
, smart_numsect
, name
);
1004 pout("GP %sLog at address 0x%02x has %4d sectors [%s]\n", (smartlogdir
?" ":""),
1005 i
, gp_numsect
, name
);
1007 pout("SMART Log at address 0x%02x has %4d sectors [%s]\n", i
, smart_numsect
, name
);
1013 // Print hexdump of log pages.
1014 // Format is compatible with 'xxd -r'.
1015 static void PrintLogPages(const char * type
, const unsigned char * data
,
1016 unsigned char logaddr
, unsigned page
,
1017 unsigned num_pages
, unsigned max_pages
)
1019 pout("%s Log 0x%02x [%s], Page %u-%u (of %u)\n",
1020 type
, logaddr
, GetLogName(logaddr
), page
, page
+num_pages
-1, max_pages
);
1021 for (unsigned i
= 0; i
< num_pages
* 512; i
+= 16) {
1022 const unsigned char * p
= data
+i
;
1023 pout("%07x: %02x %02x %02x %02x %02x %02x %02x %02x "
1024 "%02x %02x %02x %02x %02x %02x %02x %02x ",
1026 p
[ 0], p
[ 1], p
[ 2], p
[ 3], p
[ 4], p
[ 5], p
[ 6], p
[ 7],
1027 p
[ 8], p
[ 9], p
[10], p
[11], p
[12], p
[13], p
[14], p
[15]);
1028 #define P(n) (isprint((int)(p[n]))?(int)(p[n]):'.')
1029 pout("|%c%c%c%c%c%c%c%c"
1030 "%c%c%c%c%c%c%c%c|\n",
1031 P( 0), P( 1), P( 2), P( 3), P( 4), P( 5), P( 6), P( 7),
1032 P( 8), P( 9), P(10), P(11), P(12), P(13), P(14), P(15));
1034 if ((i
& 0x1ff) == 0x1f0)
1040 static void PrintSataPhyEventCounters(const unsigned char * data
, bool reset
)
1043 checksumwarning("SATA Phy Event Counters");
1044 pout("SATA Phy Event Counters (GP Log 0x11)\n");
1045 if (data
[0] || data
[1] || data
[2] || data
[3])
1046 pout("[Reserved: 0x%02x 0x%02x 0x%02x 0x%02x]\n",
1047 data
[0], data
[1], data
[2], data
[3]);
1048 pout("ID Size Value Description\n");
1050 for (unsigned i
= 4; ; ) {
1051 // Get counter id and size (bits 14:12)
1052 unsigned id
= data
[i
] | (data
[i
+1] << 8);
1053 unsigned size
= ((id
>> 12) & 0x7) << 1;
1056 // End of counter table ?
1061 if (!(2 <= size
&& size
<= 8 && i
+ size
< 512)) {
1062 pout("0x%04x %u: Invalid entry\n", id
, size
);
1067 uint64_t val
= 0, max_val
= 0;
1068 for (unsigned j
= 0; j
< size
; j
+=2) {
1069 val
|= (uint64_t)(data
[i
+j
] | (data
[i
+j
+1] << 8)) << (j
*8);
1070 max_val
|= (uint64_t)0xffffU
<< (j
*8);
1077 case 0x001: name
= "Command failed due to ICRC error"; break; // Mandatory
1078 case 0x002: name
= "R_ERR response for data FIS"; break;
1079 case 0x003: name
= "R_ERR response for device-to-host data FIS"; break;
1080 case 0x004: name
= "R_ERR response for host-to-device data FIS"; break;
1081 case 0x005: name
= "R_ERR response for non-data FIS"; break;
1082 case 0x006: name
= "R_ERR response for device-to-host non-data FIS"; break;
1083 case 0x007: name
= "R_ERR response for host-to-device non-data FIS"; break;
1084 case 0x008: name
= "Device-to-host non-data FIS retries"; break;
1085 case 0x009: name
= "Transition from drive PhyRdy to drive PhyNRdy"; break;
1086 case 0x00A: name
= "Device-to-host register FISes sent due to a COMRESET"; break; // Mandatory
1087 case 0x00B: name
= "CRC errors within host-to-device FIS"; break;
1088 case 0x00D: name
= "Non-CRC errors within host-to-device FIS"; break;
1089 case 0x00F: name
= "R_ERR response for host-to-device data FIS, CRC"; break;
1090 case 0x010: name
= "R_ERR response for host-to-device data FIS, non-CRC"; break;
1091 case 0x012: name
= "R_ERR response for host-to-device non-data FIS, CRC"; break;
1092 case 0x013: name
= "R_ERR response for host-to-device non-data FIS, non-CRC"; break;
1093 default: name
= (id
& 0x8000 ? "Vendor specific" : "Unknown"); break;
1096 // Counters stop at max value, add '+' in this case
1097 pout("0x%04x %u %12"PRIu64
"%c %s\n", id
, size
, val
,
1098 (val
== max_val
? '+' : ' '), name
);
1101 pout("All counters reset\n");
1105 // Get description for 'state' value from SMART Error Logs
1106 static const char * get_error_log_state_desc(unsigned state
)
1110 case 0x0: return "in an unknown state";
1111 case 0x1: return "sleeping";
1112 case 0x2: return "in standby mode";
1113 case 0x3: return "active or idle";
1114 case 0x4: return "doing SMART Offline or Self-test";
1116 return (state
< 0xb ? "in a reserved state"
1117 : "in a vendor specific state");
1121 // returns number of errors
1122 static int PrintSmartErrorlog(const ata_smart_errorlog
*data
,
1123 unsigned char fix_firmwarebug
)
1125 pout("SMART Error Log Version: %d\n", (int)data
->revnumber
);
1127 // if no errors logged, return
1128 if (!data
->error_log_pointer
){
1129 pout("No Errors Logged\n\n");
1133 // If log pointer out of range, return
1134 if (data
->error_log_pointer
>5){
1135 pout("Invalid Error Log index = 0x%02x (T13/1321D rev 1c "
1136 "Section 8.41.6.8.2.2 gives valid range from 1 to 5)\n\n",
1137 (int)data
->error_log_pointer
);
1141 // Some internal consistency checking of the data structures
1142 if ((data
->ata_error_count
-data
->error_log_pointer
)%5 && fix_firmwarebug
!= FIX_SAMSUNG2
) {
1143 pout("Warning: ATA error count %d inconsistent with error log pointer %d\n\n",
1144 data
->ata_error_count
,data
->error_log_pointer
);
1147 // starting printing error log info
1148 if (data
->ata_error_count
<=5)
1149 pout( "ATA Error Count: %d\n", (int)data
->ata_error_count
);
1151 pout( "ATA Error Count: %d (device log contains only the most recent five errors)\n",
1152 (int)data
->ata_error_count
);
1154 pout("\tCR = Command Register [HEX]\n"
1155 "\tFR = Features Register [HEX]\n"
1156 "\tSC = Sector Count Register [HEX]\n"
1157 "\tSN = Sector Number Register [HEX]\n"
1158 "\tCL = Cylinder Low Register [HEX]\n"
1159 "\tCH = Cylinder High Register [HEX]\n"
1160 "\tDH = Device/Head Register [HEX]\n"
1161 "\tDC = Device Command Register [HEX]\n"
1162 "\tER = Error register [HEX]\n"
1163 "\tST = Status register [HEX]\n"
1164 "Powered_Up_Time is measured from power on, and printed as\n"
1165 "DDd+hh:mm:SS.sss where DD=days, hh=hours, mm=minutes,\n"
1166 "SS=sec, and sss=millisec. It \"wraps\" after 49.710 days.\n\n");
1168 // now step through the five error log data structures (table 39 of spec)
1169 for (int k
= 4; k
>= 0; k
-- ) {
1171 // The error log data structure entries are a circular buffer
1172 int j
, i
=(data
->error_log_pointer
+k
)%5;
1173 const ata_smart_errorlog_struct
* elog
= data
->errorlog_struct
+i
;
1174 const ata_smart_errorlog_error_struct
* summary
= &(elog
->error_struct
);
1176 // Spec says: unused error log structures shall be zero filled
1177 if (nonempty(elog
, sizeof(*elog
))){
1178 // Table 57 of T13/1532D Volume 1 Revision 3
1179 const char *msgstate
= get_error_log_state_desc(summary
->state
);
1180 int days
= (int)summary
->timestamp
/24;
1182 // See table 42 of ATA5 spec
1184 pout("Error %d occurred at disk power-on lifetime: %d hours (%d days + %d hours)\n",
1185 (int)(data
->ata_error_count
+k
-4), (int)summary
->timestamp
, days
, (int)(summary
->timestamp
-24*days
));
1187 pout(" When the command that caused the error occurred, the device was %s.\n\n",msgstate
);
1188 pout(" After command completion occurred, registers were:\n"
1189 " ER ST SC SN CL CH DH\n"
1190 " -- -- -- -- -- -- --\n"
1191 " %02x %02x %02x %02x %02x %02x %02x",
1192 (int)summary
->error_register
,
1193 (int)summary
->status
,
1194 (int)summary
->sector_count
,
1195 (int)summary
->sector_number
,
1196 (int)summary
->cylinder_low
,
1197 (int)summary
->cylinder_high
,
1198 (int)summary
->drive_head
);
1199 // Add a description of the contents of the status and error registers
1202 const char * st_er_desc
= construct_st_er_desc(descbuf
, elog
);
1204 pout(" %s", st_er_desc
);
1206 pout(" Commands leading to the command that caused the error were:\n"
1207 " CR FR SC SN CL CH DH DC Powered_Up_Time Command/Feature_Name\n"
1208 " -- -- -- -- -- -- -- -- ---------------- --------------------\n");
1209 for ( j
= 4; j
>= 0; j
--){
1210 const ata_smart_errorlog_command_struct
* thiscommand
= elog
->commands
+j
;
1212 // Spec says: unused data command structures shall be zero filled
1213 if (nonempty(thiscommand
, sizeof(*thiscommand
))) {
1214 char timestring
[32];
1216 // Convert integer milliseconds to a text-format string
1217 MsecToText(thiscommand
->timestamp
, timestring
);
1219 pout(" %02x %02x %02x %02x %02x %02x %02x %02x %16s %s\n",
1220 (int)thiscommand
->commandreg
,
1221 (int)thiscommand
->featuresreg
,
1222 (int)thiscommand
->sector_count
,
1223 (int)thiscommand
->sector_number
,
1224 (int)thiscommand
->cylinder_low
,
1225 (int)thiscommand
->cylinder_high
,
1226 (int)thiscommand
->drive_head
,
1227 (int)thiscommand
->devicecontrolreg
,
1229 look_up_ata_command(thiscommand
->commandreg
, thiscommand
->featuresreg
));
1236 if (con
->printing_switchable
)
1239 return data
->ata_error_count
;
1242 // Print SMART Extended Comprehensive Error Log (GP Log 0x03)
1243 static int PrintSmartExtErrorLog(const ata_smart_exterrlog
* log
,
1244 unsigned nsectors
, unsigned max_errors
)
1246 pout("SMART Extended Comprehensive Error Log Version: %u (%u sectors)\n",
1247 log
->version
, nsectors
);
1249 if (!log
->device_error_count
) {
1250 pout("No Errors Logged\n\n");
1256 unsigned nentries
= nsectors
* 4;
1257 unsigned erridx
= log
->error_log_index
;
1258 if (!(1 <= erridx
&& erridx
<= nentries
)){
1259 // Some Samsung disks (at least SP1614C/SW100-25, HD300LJ/ZT100-12) use the
1260 // former index from Summary Error Log (byte 1, now reserved) and set byte 2-3
1262 if (!(erridx
== 0 && 1 <= log
->reserved1
&& log
->reserved1
<= nentries
)) {
1263 pout("Invalid Error Log index = 0x%04x (reserved = 0x%02x)\n", erridx
, log
->reserved1
);
1266 pout("Invalid Error Log index = 0x%04x, trying reserved byte (0x%02x) instead\n", erridx
, log
->reserved1
);
1267 erridx
= log
->reserved1
;
1270 // Index base is not clearly specified by ATA8-ACS (T13/1699-D Revision 6a),
1271 // it is 1-based in practice.
1274 // Calculate #errors to print
1275 unsigned errcnt
= log
->device_error_count
;
1277 if (errcnt
<= nentries
)
1278 pout("Device Error Count: %u\n", log
->device_error_count
);
1281 pout("Device Error Count: %u (device log contains only the most recent %u errors)\n",
1282 log
->device_error_count
, errcnt
);
1285 if (max_errors
< errcnt
)
1286 errcnt
= max_errors
;
1289 pout("\tCR = Command Register\n"
1290 "\tFEATR = Features Register\n"
1291 "\tCOUNT = Count (was: Sector Count) Register\n"
1292 "\tLBA_48 = Upper bytes of LBA High/Mid/Low Registers ] ATA-8\n"
1293 "\tLH = LBA High (was: Cylinder High) Register ] LBA\n"
1294 "\tLM = LBA Mid (was: Cylinder Low) Register ] Register\n"
1295 "\tLL = LBA Low (was: Sector Number) Register ]\n"
1296 "\tDV = Device (was: Device/Head) Register\n"
1297 "\tDC = Device Control Register\n"
1298 "\tER = Error register\n"
1299 "\tST = Status register\n"
1300 "Powered_Up_Time is measured from power on, and printed as\n"
1301 "DDd+hh:mm:SS.sss where DD=days, hh=hours, mm=minutes,\n"
1302 "SS=sec, and sss=millisec. It \"wraps\" after 49.710 days.\n\n");
1304 // Iterate through circular buffer in reverse direction
1305 for (unsigned i
= 0, errnum
= log
->device_error_count
;
1306 i
< errcnt
; i
++, errnum
--, erridx
= (erridx
> 0 ? erridx
- 1 : nentries
- 1)) {
1308 const ata_smart_exterrlog_error_log
& entry
= log
[erridx
/ 4].error_logs
[erridx
% 4];
1310 // Skip unused entries
1311 if (!nonempty(&entry
, sizeof(entry
))) {
1312 pout("Error %u [%u] log entry is empty\n", errnum
, erridx
);
1316 // Print error information
1318 const ata_smart_exterrlog_error
& err
= entry
.error
;
1319 pout("Error %u [%u] occurred at disk power-on lifetime: %u hours (%u days + %u hours)\n",
1320 errnum
, erridx
, err
.timestamp
, err
.timestamp
/ 24, err
.timestamp
% 24);
1323 pout(" When the command that caused the error occurred, the device was %s.\n\n",
1324 get_error_log_state_desc(err
.state
));
1327 pout(" After command completion occurred, registers were:\n"
1328 " ER -- ST COUNT LBA_48 LH LM LL DV DC\n"
1329 " -- -- -- == -- == == == -- -- -- -- --\n"
1330 " %02x -- %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1332 err
.status_register
,
1333 err
.count_register_hi
,
1335 err
.lba_high_register_hi
,
1336 err
.lba_mid_register_hi
,
1337 err
.lba_low_register_hi
,
1338 err
.lba_high_register
,
1339 err
.lba_mid_register
,
1340 err
.lba_low_register
,
1341 err
.device_register
,
1342 err
.device_control_register
);
1344 // Add a description of the contents of the status and error registers
1347 const char * st_er_desc
= construct_st_er_desc(descbuf
, &entry
);
1349 pout(" %s", st_er_desc
);
1352 // Print command history
1353 pout(" Commands leading to the command that caused the error were:\n"
1354 " CR FEATR COUNT LBA_48 LH LM LL DV DC Powered_Up_Time Command/Feature_Name\n"
1355 " -- == -- == -- == == == -- -- -- -- -- --------------- --------------------\n");
1356 for (int ci
= 4; ci
>= 0; ci
--) {
1357 const ata_smart_exterrlog_command
& cmd
= entry
.commands
[ci
];
1359 // Skip unused entries
1360 if (!nonempty(&cmd
, sizeof(cmd
)))
1363 // Print registers, timestamp and ATA command name
1364 char timestring
[32];
1365 MsecToText(cmd
.timestamp
, timestring
);
1367 pout(" %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %16s %s\n",
1368 cmd
.command_register
,
1369 cmd
.features_register_hi
,
1370 cmd
.features_register
,
1371 cmd
.count_register_hi
,
1373 cmd
.lba_high_register_hi
,
1374 cmd
.lba_mid_register_hi
,
1375 cmd
.lba_low_register_hi
,
1376 cmd
.lba_high_register
,
1377 cmd
.lba_mid_register
,
1378 cmd
.lba_low_register
,
1379 cmd
.device_register
,
1380 cmd
.device_control_register
,
1382 look_up_ata_command(cmd
.command_register
, cmd
.features_register
));
1388 if (con
->printing_switchable
)
1391 return log
->device_error_count
;
1394 // Print SMART Extended Self-test Log (GP Log 0x07)
1395 static bool PrintSmartExtSelfTestLog(const ata_smart_extselftestlog
* log
,
1396 unsigned nsectors
, unsigned max_entries
)
1398 pout("SMART Extended Self-test Log Version: %u (%u sectors)\n",
1399 log
->version
, nsectors
);
1401 if (!log
->log_desc_index
){
1402 pout("No self-tests have been logged. [To run self-tests, use: smartctl -t]\n\n");
1407 unsigned nentries
= nsectors
* 19;
1408 unsigned logidx
= log
->log_desc_index
;
1409 if (logidx
> nentries
) {
1410 pout("Invalid Self-test Log index = 0x%04x (reserved = 0x%02x)\n", logidx
, log
->reserved1
);
1414 // Index base is not clearly specified by ATA8-ACS (T13/1699-D Revision 6a),
1415 // it is 1-based in practice.
1418 bool print_header
= true;
1420 // Iterate through circular buffer in reverse direction
1421 for (unsigned i
= 0, testnum
= 1;
1422 i
< nentries
&& testnum
<= max_entries
;
1423 i
++, logidx
= (logidx
> 0 ? logidx
- 1 : nentries
- 1)) {
1425 const ata_smart_extselftestlog_desc
& entry
= log
[logidx
/ 19].log_descs
[logidx
% 19];
1427 // Skip unused entries
1428 if (!nonempty(&entry
, sizeof(entry
)))
1432 const unsigned char * b
= entry
.failing_lba
;
1433 uint64_t lba48
= b
[0]
1436 | ((uint64_t)b
[3] << 24)
1437 | ((uint64_t)b
[4] << 32)
1438 | ((uint64_t)b
[5] << 40);
1441 ataPrintSmartSelfTestEntry(testnum
++, entry
.self_test_type
,
1442 entry
.self_test_status
, entry
.timestamp
, lba48
,
1443 false /*!print_error_only*/, print_header
);
1449 static void ataPrintSelectiveSelfTestLog(const ata_selective_self_test_log
* log
, const ata_smart_values
* sv
)
1451 int i
,field1
,field2
;
1454 uint64_t maxl
=0,maxr
=0;
1455 uint64_t current
=log
->currentlba
;
1456 uint64_t currentend
=current
+65535;
1458 // print data structure revision number
1459 pout("SMART Selective self-test log data structure revision number %d\n",(int)log
->logversion
);
1460 if (1 != log
->logversion
)
1461 pout("Note: revision number not 1 implies that no selective self-test has ever been run\n");
1463 switch((sv
->self_test_exec_status
)>>4){
1464 case 0:msg
="Completed";
1466 case 1:msg
="Aborted_by_host";
1468 case 2:msg
="Interrupted";
1470 case 3:msg
="Fatal_error";
1472 case 4:msg
="Completed_unknown_failure";
1474 case 5:msg
="Completed_electrical_failure";
1476 case 6:msg
="Completed_servo/seek_failure";
1478 case 7:msg
="Completed_read_failure";
1480 case 8:msg
="Completed_handling_damage??";
1482 case 15:msg
="Self_test_in_progress";
1484 default:msg
="Unknown_status ";
1488 // find the number of columns needed for printing. If in use, the
1489 // start/end of span being read-scanned...
1490 if (log
->currentspan
>5) {
1494 for (i
=0; i
<5; i
++) {
1495 uint64_t start
=log
->span
[i
].start
;
1496 uint64_t end
=log
->span
[i
].end
;
1497 // ... plus max start/end of each of the five test spans.
1504 // we need at least 7 characters wide fields to accomodate the
1506 if ((field1
=snprintf(tmp
,64, "%"PRIu64
, maxl
))<7)
1508 if ((field2
=snprintf(tmp
,64, "%"PRIu64
, maxr
))<7)
1511 // now print the five test spans
1512 pout(" SPAN %*s %*s CURRENT_TEST_STATUS\n", field1
, "MIN_LBA", field2
, "MAX_LBA");
1514 for (i
=0; i
<5; i
++) {
1515 uint64_t start
=log
->span
[i
].start
;
1516 uint64_t end
=log
->span
[i
].end
;
1518 if ((i
+1)==(int)log
->currentspan
)
1519 // this span is currently under test
1520 pout(" %d %*"PRIu64
" %*"PRIu64
" %s [%01d0%% left] (%"PRIu64
"-%"PRIu64
")\n",
1521 i
+1, field1
, start
, field2
, end
, msg
,
1522 (int)(sv
->self_test_exec_status
& 0xf), current
, currentend
);
1524 // this span is not currently under test
1525 pout(" %d %*"PRIu64
" %*"PRIu64
" Not_testing\n",
1526 i
+1, field1
, start
, field2
, end
);
1529 // if we are currently read-scanning, print LBAs and the status of
1531 if (log
->currentspan
>5)
1532 pout("%5d %*"PRIu64
" %*"PRIu64
" Read_scanning %s\n",
1533 (int)log
->currentspan
, field1
, current
, field2
, currentend
,
1534 OfflineDataCollectionStatus(sv
->offline_data_collection_status
));
1536 /* Print selective self-test flags. Possible flag combinations are
1537 (numbering bits from 0-15):
1541 1 0 0 Will carry out scan after selective test
1542 1 1 0 Waiting to carry out scan after powerup
1543 1 0 1 Currently scanning
1544 1 1 1 Currently scanning
1547 pout("Selective self-test flags (0x%x):\n", (unsigned int)log
->flags
);
1548 if (log
->flags
& SELECTIVE_FLAG_DOSCAN
) {
1549 if (log
->flags
& SELECTIVE_FLAG_ACTIVE
)
1550 pout(" Currently read-scanning the remainder of the disk.\n");
1551 else if (log
->flags
& SELECTIVE_FLAG_PENDING
)
1552 pout(" Read-scan of remainder of disk interrupted; will resume %d min after power-up.\n",
1553 (int)log
->pendingtime
);
1555 pout(" After scanning selected spans, read-scan remainder of disk.\n");
1558 pout(" After scanning selected spans, do NOT read-scan remainder of disk.\n");
1560 // print pending time
1561 pout("If Selective self-test is pending on power-up, resume after %d minute delay.\n",
1562 (int)log
->pendingtime
);
1567 // Format SCT Temperature value
1568 static const char * sct_ptemp(signed char x
, char * buf
)
1570 if (x
== -128 /*0x80 = unknown*/)
1573 sprintf(buf
, "%2d", x
);
1577 static const char * sct_pbar(int x
, char * buf
)
1588 memset(buf
, '*', x
);
1594 buf
[0] = '-'; buf
[1] = 0;
1599 static const char * sct_device_state_msg(unsigned char state
)
1602 case 0: return "Active";
1603 case 1: return "Stand-by";
1604 case 2: return "Sleep";
1605 case 3: return "DST executing in background";
1606 case 4: return "SMART Off-line Data Collection executing in background";
1607 case 5: return "SCT command executing in background";
1608 default:return "Unknown";
1613 static int ataPrintSCTStatus(const ata_sct_status_response
* sts
)
1615 pout("SCT Status Version: %u\n", sts
->format_version
);
1616 pout("SCT Version (vendor specific): %u (0x%04x)\n", sts
->sct_version
, sts
->sct_version
);
1617 pout("SCT Support Level: %u\n", sts
->sct_spec
);
1618 pout("Device State: %s (%u)\n",
1619 sct_device_state_msg(sts
->device_state
), sts
->device_state
);
1620 char buf1
[20], buf2
[20];
1621 if ( !sts
->min_temp
&& !sts
->life_min_temp
&& !sts
->byte205
1622 && !sts
->under_limit_count
&& !sts
->over_limit_count
) {
1623 // "Reserved" fields not set, assume "old" format version 2
1624 // Table 11 of T13/1701DT Revision 5
1625 // Table 54 of T13/1699-D Revision 3e
1626 pout("Current Temperature: %s Celsius\n",
1627 sct_ptemp(sts
->hda_temp
, buf1
));
1628 pout("Power Cycle Max Temperature: %s Celsius\n",
1629 sct_ptemp(sts
->max_temp
, buf2
));
1630 pout("Lifetime Max Temperature: %s Celsius\n",
1631 sct_ptemp(sts
->life_max_temp
, buf2
));
1634 // Assume "new" format version 2 or version 3
1635 // T13/e06152r0-3 (Additional SCT Temperature Statistics)
1636 // Table 60 of T13/1699-D Revision 3f
1637 pout("Current Temperature: %s Celsius\n",
1638 sct_ptemp(sts
->hda_temp
, buf1
));
1639 pout("Power Cycle Min/Max Temperature: %s/%s Celsius\n",
1640 sct_ptemp(sts
->min_temp
, buf1
), sct_ptemp(sts
->max_temp
, buf2
));
1641 pout("Lifetime Min/Max Temperature: %s/%s Celsius\n",
1642 sct_ptemp(sts
->life_min_temp
, buf1
), sct_ptemp(sts
->life_max_temp
, buf2
));
1643 if (sts
->byte205
) // e06152r0-2, removed in e06152r3
1644 pout("Lifetime Average Temperature: %s Celsius\n",
1645 sct_ptemp((signed char)sts
->byte205
, buf1
));
1646 pout("Under/Over Temperature Limit Count: %2u/%u\n",
1647 sts
->under_limit_count
, sts
->over_limit_count
);
1652 // Print SCT Temperature History Table
1653 static int ataPrintSCTTempHist(const ata_sct_temperature_history_table
* tmh
)
1655 char buf1
[20], buf2
[80];
1656 pout("SCT Temperature History Version: %u\n", tmh
->format_version
);
1657 pout("Temperature Sampling Period: %u minute%s\n",
1658 tmh
->sampling_period
, (tmh
->sampling_period
==1?"":"s"));
1659 pout("Temperature Logging Interval: %u minute%s\n",
1660 tmh
->interval
, (tmh
->interval
==1?"":"s"));
1661 pout("Min/Max recommended Temperature: %s/%s Celsius\n",
1662 sct_ptemp(tmh
->min_op_limit
, buf1
), sct_ptemp(tmh
->max_op_limit
, buf2
));
1663 pout("Min/Max Temperature Limit: %s/%s Celsius\n",
1664 sct_ptemp(tmh
->under_limit
, buf1
), sct_ptemp(tmh
->over_limit
, buf2
));
1665 pout("Temperature History Size (Index): %u (%u)\n", tmh
->cb_size
, tmh
->cb_index
);
1666 if (!(0 < tmh
->cb_size
&& tmh
->cb_size
<= sizeof(tmh
->cb
) && tmh
->cb_index
< tmh
->cb_size
)) {
1667 pout("Error invalid Temperature History Size or Index\n");
1672 pout("\nIndex Estimated Time Temperature Celsius\n");
1673 unsigned n
= 0, i
= (tmh
->cb_index
+1) % tmh
->cb_size
;
1674 unsigned interval
= (tmh
->interval
> 0 ? tmh
->interval
: 1);
1675 time_t t
= time(0) - (tmh
->cb_size
-1) * interval
* 60;
1676 t
-= t
% (interval
* 60);
1677 while (n
< tmh
->cb_size
) {
1678 // Find range of identical temperatures
1679 unsigned n1
= n
, n2
= n
+1, i2
= (i
+1) % tmh
->cb_size
;
1680 while (n2
< tmh
->cb_size
&& tmh
->cb
[i2
] == tmh
->cb
[i
]) {
1681 n2
++; i2
= (i2
+1) % tmh
->cb_size
;
1685 if (n
== n1
|| n
== n2
-1 || n2
<= n1
+3) {
1687 // TODO: Don't print times < boot time
1688 strftime(date
, sizeof(date
), "%Y-%m-%d %H:%M", localtime(&t
));
1689 pout(" %3u %s %s %s\n", i
, date
,
1690 sct_ptemp(tmh
->cb
[i
], buf1
), sct_pbar(tmh
->cb
[i
], buf2
));
1692 else if (n
== n1
+1) {
1693 pout(" ... ..(%3u skipped). .. %s\n",
1694 n2
-n1
-2, sct_pbar(tmh
->cb
[i
], buf2
));
1696 t
+= interval
* 60; i
= (i
+1) % tmh
->cb_size
; n
++;
1699 //assert(n == tmh->cb_size && i == (tmh->cb_index+1) % tmh->cb_size);
1704 // Print SCT Error Recovery Control timers
1705 static void ataPrintSCTErrorRecoveryControl(unsigned short read_timer
, unsigned short write_timer
)
1707 pout("SCT Error Recovery Control:\n");
1709 pout(" Read: Disabled\n");
1711 pout(" Read: %6d (%0.1f seconds)\n", read_timer
, read_timer
/10.0);
1713 pout(" Write: Disabled\n");
1715 pout(" Write: %6d (%0.1f seconds)\n", write_timer
, write_timer
/10.0);
1719 // Compares failure type to policy in effect, and either exits or
1720 // simply returns to the calling routine.
1721 void failuretest(int type
, int returnvalue
){
1723 // If this is an error in an "optional" SMART command
1724 if (type
==OPTIONAL_CMD
){
1725 if (con
->conservative
){
1726 pout("An optional SMART command failed: exiting. Remove '-T conservative' option to continue.\n");
1732 // If this is an error in a "mandatory" SMART command
1733 if (type
==MANDATORY_CMD
){
1734 if (con
->permissive
--)
1736 pout("A mandatory SMART command failed: exiting. To continue, add one or more '-T permissive' options.\n");
1740 pout("Smartctl internal error in failuretest(type=%d). Please contact developers at " PACKAGE_HOMEPAGE
"\n",type
);
1741 EXIT(returnvalue
|FAILCMD
);
1744 int ataPrintMain (ata_device
* device
, const ata_print_options
& options
)
1748 // If requested, check power mode first
1749 const char * powername
= 0;
1750 bool powerchg
= false;
1751 if (options
.powermode
) {
1752 unsigned char powerlimit
= 0xff;
1753 int powermode
= ataCheckPowerMode(device
);
1754 switch (powermode
) {
1756 if (errno
== ENOSYS
) {
1757 pout("CHECK POWER STATUS not implemented, ignoring -n Option\n"); break;
1759 powername
= "SLEEP"; powerlimit
= 2;
1762 powername
= "STANDBY"; powerlimit
= 3; break;
1764 powername
= "IDLE"; powerlimit
= 4; break;
1766 powername
= "ACTIVE or IDLE"; break;
1768 pout("CHECK POWER STATUS returned %d, not ATA compliant, ignoring -n Option\n", powermode
);
1772 if (options
.powermode
>= powerlimit
) {
1773 pout("Device is in %s mode, exit(%d)\n", powername
, FAILPOWER
);
1776 powerchg
= (powermode
!= 0xff); // SMART tests will spin up drives
1780 // SMART values needed ?
1781 bool need_smart_val
= (
1782 options
.smart_check_status
1783 || options
.smart_general_values
1784 || options
.smart_vendor_attrib
1785 || options
.smart_error_log
1786 || options
.smart_selftest_log
1787 || options
.smart_selective_selftest_log
1788 || options
.smart_ext_error_log
1789 || options
.smart_ext_selftest_log
1790 || options
.smart_auto_offl_enable
1791 || options
.smart_auto_offl_disable
1792 || options
.smart_selftest_type
!= -1
1795 // SMART must be enabled ?
1796 bool need_smart_enabled
= (
1798 || options
.smart_auto_save_enable
1799 || options
.smart_auto_save_disable
1802 // SMART feature set needed ?
1803 bool need_smart_support
= (
1805 || options
.smart_enable
1806 || options
.smart_disable
1809 // Start by getting Drive ID information. We need this, to know if SMART is supported.
1810 ata_identify_device drive
; memset(&drive
, 0, sizeof(drive
));
1811 int retid
= ataReadHDIdentity(device
,&drive
);
1813 pout("Smartctl: Device Read Identity Failed (not an ATA/ATAPI device)\n\n");
1814 failuretest(MANDATORY_CMD
, returnval
|=FAILID
);
1817 // If requested, show which presets would be used for this drive and exit.
1818 if (options
.show_presets
) {
1819 show_presets(&drive
, options
.fix_swapped_id
);
1823 // Use preset vendor attribute options unless user has requested otherwise.
1824 ata_vendor_attr_defs attribute_defs
= options
.attribute_defs
;
1825 unsigned char fix_firmwarebug
= options
.fix_firmwarebug
;
1826 if (!options
.ignore_presets
)
1827 apply_presets(&drive
, attribute_defs
, fix_firmwarebug
, options
.fix_swapped_id
);
1829 // Print most drive identity information if requested
1831 if (options
.drive_info
) {
1832 pout("=== START OF INFORMATION SECTION ===\n");
1833 known
= PrintDriveInfo(&drive
, options
.fix_swapped_id
);
1836 // Check and print SMART support and state
1837 int smart_supported
= -1, smart_enabled
= -1;
1838 if (need_smart_support
|| options
.drive_info
) {
1842 pout("SMART support is: Unavailable - Packet Interface Devices [this device: %s] don't support ATA SMART\n",
1843 packetdevicetype(retid
-1));
1846 // Disk device: SMART supported and enabled ?
1847 smart_supported
= ataSmartSupport(&drive
);
1848 smart_enabled
= ataIsSmartEnabled(&drive
);
1850 if (smart_supported
< 0)
1851 pout("SMART support is: Ambiguous - ATA IDENTIFY DEVICE words 82-83 don't show if SMART supported.\n");
1852 if (smart_supported
&& smart_enabled
< 0) {
1853 pout("SMART support is: Ambiguous - ATA IDENTIFY DEVICE words 85-87 don't show if SMART is enabled.\n");
1854 if (need_smart_support
) {
1855 failuretest(MANDATORY_CMD
, returnval
|=FAILSMART
);
1856 // check SMART support by trying a command
1857 pout(" Checking to be sure by trying SMART RETURN STATUS command.\n");
1858 if (ataDoesSmartWork(device
))
1859 smart_supported
= smart_enabled
= 1;
1862 else if (smart_supported
< 0 && (smart_enabled
> 0 || known
))
1863 // Assume supported if enabled or in drive database
1864 smart_supported
= 1;
1866 if (smart_supported
< 0)
1867 pout("SMART support is: Unknown - Try option -s with argument 'on' to enable it.");
1868 else if (!smart_supported
)
1869 pout("SMART support is: Unavailable - device lacks SMART capability.\n");
1871 if (options
.drive_info
)
1872 pout("SMART support is: Available - device has SMART capability.\n");
1873 if (smart_enabled
>= 0) {
1874 if (device
->ata_identify_is_cached()) {
1875 if (options
.drive_info
)
1876 pout(" %sabled status cached by OS, trying SMART RETURN STATUS cmd.\n",
1877 (smart_enabled
?"En":"Dis"));
1878 smart_enabled
= ataDoesSmartWork(device
);
1880 if (options
.drive_info
)
1881 pout("SMART support is: %s\n",
1882 (smart_enabled
? "Enabled" : "Disabled"));
1888 // Print remaining drive info
1889 if (options
.drive_info
) {
1890 // Print the (now possibly changed) power mode if available
1892 pout("Power mode %s %s\n", (powerchg
?"was:":"is: "), powername
);
1896 // Exit if SMART is not supported but must be available to proceed
1897 if (smart_supported
<= 0 && need_smart_support
)
1898 failuretest(MANDATORY_CMD
, returnval
|=FAILSMART
);
1900 // START OF THE ENABLE/DISABLE SECTION OF THE CODE
1901 if ( options
.smart_disable
|| options
.smart_enable
1902 || options
.smart_auto_save_disable
|| options
.smart_auto_save_enable
1903 || options
.smart_auto_offl_disable
|| options
.smart_auto_offl_enable
)
1904 pout("=== START OF ENABLE/DISABLE COMMANDS SECTION ===\n");
1906 // Enable/Disable SMART commands
1907 if (options
.smart_enable
) {
1908 if (ataEnableSmart(device
)) {
1909 pout("Smartctl: SMART Enable Failed.\n\n");
1910 failuretest(MANDATORY_CMD
, returnval
|=FAILSMART
);
1913 pout("SMART Enabled.\n");
1918 // Turn off SMART on device
1919 if (options
.smart_disable
) {
1920 if (ataDisableSmart(device
)) {
1921 pout( "Smartctl: SMART Disable Failed.\n\n");
1922 failuretest(MANDATORY_CMD
,returnval
|=FAILSMART
);
1926 // Exit if SMART is disabled but must be enabled to proceed
1927 if (options
.smart_disable
|| (smart_enabled
<= 0 && need_smart_enabled
)) {
1928 pout("SMART Disabled. Use option -s with argument 'on' to enable it.\n");
1932 // Enable/Disable Auto-save attributes
1933 if (options
.smart_auto_save_enable
) {
1934 if (ataEnableAutoSave(device
)){
1935 pout( "Smartctl: SMART Enable Attribute Autosave Failed.\n\n");
1936 failuretest(MANDATORY_CMD
, returnval
|=FAILSMART
);
1939 pout("SMART Attribute Autosave Enabled.\n");
1942 if (options
.smart_auto_save_disable
) {
1943 if (ataDisableAutoSave(device
)){
1944 pout( "Smartctl: SMART Disable Attribute Autosave Failed.\n\n");
1945 failuretest(MANDATORY_CMD
, returnval
|=FAILSMART
);
1948 pout("SMART Attribute Autosave Disabled.\n");
1951 // Read SMART values and thresholds if necessary
1952 ata_smart_values smartval
; memset(&smartval
, 0, sizeof(smartval
));
1953 ata_smart_thresholds_pvt smartthres
; memset(&smartthres
, 0, sizeof(smartthres
));
1954 bool smart_val_ok
= false, smart_thres_ok
= false;
1956 if (need_smart_val
) {
1957 if (ataReadSmartValues(device
, &smartval
)) {
1958 pout("Smartctl: SMART Read Values failed.\n\n");
1959 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
1962 smart_val_ok
= true;
1964 if (options
.smart_check_status
|| options
.smart_vendor_attrib
) {
1965 if (ataReadSmartThresholds(device
, &smartthres
)){
1966 pout("Smartctl: SMART Read Thresholds failed.\n\n");
1967 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
1970 smart_thres_ok
= true;
1975 // Enable/Disable Off-line testing
1976 bool needupdate
= false;
1977 if (options
.smart_auto_offl_enable
) {
1978 if (!isSupportAutomaticTimer(&smartval
)){
1979 pout("Warning: device does not support SMART Automatic Timers.\n\n");
1980 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
1982 needupdate
= smart_val_ok
;
1983 if (ataEnableAutoOffline(device
)){
1984 pout( "Smartctl: SMART Enable Automatic Offline Failed.\n\n");
1985 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
1988 pout("SMART Automatic Offline Testing Enabled every four hours.\n");
1991 if (options
.smart_auto_offl_disable
) {
1992 if (!isSupportAutomaticTimer(&smartval
)){
1993 pout("Warning: device does not support SMART Automatic Timers.\n\n");
1994 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
1996 needupdate
= smart_val_ok
;
1997 if (ataDisableAutoOffline(device
)){
1998 pout("Smartctl: SMART Disable Automatic Offline Failed.\n\n");
1999 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2002 pout("SMART Automatic Offline Testing Disabled.\n");
2005 if (needupdate
&& ataReadSmartValues(device
, &smartval
)){
2006 pout("Smartctl: SMART Read Values failed.\n\n");
2007 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2008 smart_val_ok
= false;
2011 // all this for a newline!
2012 if ( options
.smart_disable
|| options
.smart_enable
2013 || options
.smart_auto_save_disable
|| options
.smart_auto_save_enable
2014 || options
.smart_auto_offl_disable
|| options
.smart_auto_offl_enable
)
2017 // START OF READ-ONLY OPTIONS APART FROM -V and -i
2018 if ( options
.smart_check_status
|| options
.smart_general_values
2019 || options
.smart_vendor_attrib
|| options
.smart_error_log
2020 || options
.smart_selftest_log
|| options
.smart_selective_selftest_log
2021 || options
.smart_ext_error_log
|| options
.smart_ext_selftest_log
2022 || options
.sct_temp_sts
|| options
.sct_temp_hist
)
2023 pout("=== START OF READ SMART DATA SECTION ===\n");
2025 // Check SMART status
2026 if (options
.smart_check_status
) {
2028 switch (ataSmartStatus2(device
)) {
2031 // The case where the disk health is OK
2032 pout("SMART overall-health self-assessment test result: PASSED\n");
2033 if (smart_thres_ok
&& find_failed_attr(&smartval
, &smartthres
, attribute_defs
, 0)) {
2034 if (options
.smart_vendor_attrib
)
2035 pout("See vendor-specific Attribute list for marginal Attributes.\n\n");
2038 pout("Please note the following marginal Attributes:\n");
2039 PrintSmartAttribWithThres(&smartval
, &smartthres
, attribute_defs
, 2);
2048 // The case where the disk health is NOT OK
2050 pout("SMART overall-health self-assessment test result: FAILED!\n"
2051 "Drive failure expected in less than 24 hours. SAVE ALL DATA.\n");
2053 if (smart_thres_ok
&& find_failed_attr(&smartval
, &smartthres
, attribute_defs
, 1)) {
2054 returnval
|=FAILATTR
;
2055 if (options
.smart_vendor_attrib
)
2056 pout("See vendor-specific Attribute list for failed Attributes.\n\n");
2059 pout("Failed Attributes:\n");
2060 PrintSmartAttribWithThres(&smartval
, &smartthres
, attribute_defs
, 1);
2064 pout("No failed Attributes found.\n\n");
2065 returnval
|=FAILSTATUS
;
2071 // Something went wrong with the SMART STATUS command.
2072 // The ATA SMART RETURN STATUS command provides the result in the ATA output
2073 // registers. Buggy ATA/SATA drivers and SAT Layers often do not properly
2074 // return the registers values.
2075 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2076 if (!(smart_val_ok
&& smart_thres_ok
)) {
2078 pout("SMART overall-health self-assessment test result: UNKNOWN!\n"
2079 "SMART Status, Attributes and Thresholds cannot be read.\n\n");
2081 else if (find_failed_attr(&smartval
, &smartthres
, attribute_defs
, 1)) {
2083 pout("SMART overall-health self-assessment test result: FAILED!\n"
2084 "Drive failure expected in less than 24 hours. SAVE ALL DATA.\n");
2086 returnval
|=FAILATTR
;
2087 returnval
|=FAILSTATUS
;
2088 if (options
.smart_vendor_attrib
)
2089 pout("See vendor-specific Attribute list for failed Attributes.\n\n");
2092 pout("Failed Attributes:\n");
2093 PrintSmartAttribWithThres(&smartval
, &smartthres
, attribute_defs
, 1);
2097 pout("SMART overall-health self-assessment test result: PASSED\n");
2098 pout("Warning: This result is based on an Attribute check.\n");
2099 if (find_failed_attr(&smartval
, &smartthres
, attribute_defs
, 0)) {
2100 if (options
.smart_vendor_attrib
)
2101 pout("See vendor-specific Attribute list for marginal Attributes.\n\n");
2104 pout("Please note the following marginal Attributes:\n");
2105 PrintSmartAttribWithThres(&smartval
, &smartthres
, attribute_defs
, 2);
2114 } // end of switch statement
2117 } // end of checking SMART Status
2119 // Print general SMART values
2120 if (smart_val_ok
&& options
.smart_general_values
)
2121 PrintGeneralSmartValues(&smartval
, &drive
, fix_firmwarebug
);
2123 // Print vendor-specific attributes
2124 if (smart_val_ok
&& options
.smart_vendor_attrib
) {
2126 PrintSmartAttribWithThres(&smartval
, &smartthres
, attribute_defs
,
2127 (con
->printing_switchable
? 2 : 0));
2131 // Print SMART and/or GP log Directory and/or logs
2132 // Get #pages for extended SMART logs
2133 ata_smart_log_directory smartlogdir_buf
, gplogdir_buf
;
2134 const ata_smart_log_directory
* smartlogdir
= 0, * gplogdir
= 0;
2136 if ( options
.gp_logdir
2137 || options
.smart_logdir
2138 || options
.smart_ext_error_log
2139 || options
.smart_ext_selftest_log
2141 || !options
.log_requests
.empty() ) {
2142 if (isGeneralPurposeLoggingCapable(&drive
))
2143 pout("General Purpose Logging (GPL) feature set supported\n");
2145 // Detect directories needed
2146 bool need_smart_logdir
= options
.smart_logdir
;
2147 bool need_gp_logdir
= ( options
.gp_logdir
2148 || options
.smart_ext_error_log
2149 || options
.smart_ext_selftest_log
2150 || options
.sataphy
);
2152 for (i
= 0; i
< options
.log_requests
.size(); i
++) {
2153 if (options
.log_requests
[i
].gpl
)
2154 need_gp_logdir
= true;
2156 need_smart_logdir
= true;
2159 // Read SMART Log directory
2160 if (need_smart_logdir
) {
2161 if (ataReadLogDirectory(device
, &smartlogdir_buf
, false)){
2162 pout("Read SMART Log Directory failed.\n\n");
2163 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2166 smartlogdir
= &smartlogdir_buf
;
2169 // Read GP Log directory
2170 if (need_gp_logdir
) {
2171 if (ataReadLogDirectory(device
, &gplogdir_buf
, true)){
2172 pout("Read GP Log Directory failed.\n\n");
2173 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2176 gplogdir
= &gplogdir_buf
;
2179 // Print log directories
2180 if ((options
.gp_logdir
&& gplogdir
) || (options
.smart_logdir
&& smartlogdir
))
2181 PrintLogDirectories(gplogdir
, smartlogdir
);
2184 for (i
= 0; i
< options
.log_requests
.size(); i
++) {
2185 const ata_log_request
& req
= options
.log_requests
[i
];
2188 unsigned max_nsectors
;
2190 type
= "General Purpose";
2191 max_nsectors
= GetNumLogSectors(gplogdir
, req
.logaddr
, true);
2195 max_nsectors
= GetNumLogSectors(smartlogdir
, req
.logaddr
, false);
2198 if (!max_nsectors
) {
2199 if (!con
->permissive
) {
2200 pout("%s Log 0x%02x does not exist (override with '-T permissive' option)\n", type
, req
.logaddr
);
2204 max_nsectors
= req
.page
+1;
2206 if (max_nsectors
<= req
.page
) {
2207 pout("%s Log 0x%02x has only %u sectors, output skipped\n", type
, req
.logaddr
, max_nsectors
);
2211 unsigned ns
= req
.nsectors
;
2212 if (ns
> max_nsectors
- req
.page
) {
2213 if (req
.nsectors
!= ~0U) // "FIRST-max"
2214 pout("%s Log 0x%02x has only %u sectors, output truncated\n", type
, req
.logaddr
, max_nsectors
);
2215 ns
= max_nsectors
- req
.page
;
2218 // SMART log don't support sector offset, start with first sector
2219 unsigned offs
= (req
.gpl
? 0 : req
.page
);
2221 raw_buffer
log_buf((offs
+ ns
) * 512);
2224 ok
= ataReadLogExt(device
, req
.logaddr
, 0x00, req
.page
, log_buf
.data(), ns
);
2226 ok
= ataReadSmartLog(device
, req
.logaddr
, log_buf
.data(), offs
+ ns
);
2228 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2230 PrintLogPages(type
, log_buf
.data() + offs
*512, req
.logaddr
, req
.page
, ns
, max_nsectors
);
2234 // Print SMART Extendend Comprehensive Error Log
2235 bool do_smart_error_log
= options
.smart_error_log
;
2236 if (options
.smart_ext_error_log
) {
2238 unsigned nsectors
= GetNumLogSectors(gplogdir
, 0x03, true);
2240 pout("SMART Extended Comprehensive Error Log (GP Log 0x03) not supported\n");
2241 else if (nsectors
>= 256)
2242 pout("SMART Extended Comprehensive Error Log size %u not supported\n", nsectors
);
2244 raw_buffer
log_03_buf(nsectors
* 512);
2245 ata_smart_exterrlog
* log_03
= (ata_smart_exterrlog
*)log_03_buf
.data();
2246 if (!ataReadExtErrorLog(device
, log_03
, nsectors
))
2247 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2249 PrintSmartExtErrorLog(log_03
, nsectors
, options
.smart_ext_error_log
);
2255 if (options
.retry_error_log
)
2256 do_smart_error_log
= true;
2257 else if (!do_smart_error_log
)
2258 pout("Try '-l [xerror,]error' to read traditional SMART Error Log\n");
2262 // Print SMART error log
2263 if (do_smart_error_log
) {
2264 if (!isSmartErrorLogCapable(&smartval
, &drive
)){
2265 pout("Warning: device does not support Error Logging\n");
2266 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2268 ata_smart_errorlog smarterror
; memset(&smarterror
, 0, sizeof(smarterror
));
2269 if (ataReadErrorLog(device
, &smarterror
, fix_firmwarebug
)){
2270 pout("Smartctl: SMART Error Log Read Failed\n");
2271 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2274 // quiet mode is turned on inside ataPrintSmartErrorLog()
2275 if (PrintSmartErrorlog(&smarterror
, fix_firmwarebug
))
2281 // Print SMART Extendend Self-test Log
2282 bool do_smart_selftest_log
= options
.smart_selftest_log
;
2283 if (options
.smart_ext_selftest_log
) {
2285 unsigned nsectors
= GetNumLogSectors(gplogdir
, 0x07, true);
2287 pout("SMART Extended Self-test Log (GP Log 0x07) not supported\n");
2288 else if (nsectors
>= 256)
2289 pout("SMART Extended Self-test Log size %u not supported\n", nsectors
);
2291 raw_buffer
log_07_buf(nsectors
* 512);
2292 ata_smart_extselftestlog
* log_07
= (ata_smart_extselftestlog
*)log_07_buf
.data();
2293 if (!ataReadExtSelfTestLog(device
, log_07
, nsectors
))
2294 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2296 if (!PrintSmartExtSelfTestLog(log_07
, nsectors
, options
.smart_ext_selftest_log
))
2297 returnval
|= FAILLOG
;
2304 if (options
.retry_selftest_log
)
2305 do_smart_selftest_log
= true;
2306 else if (!do_smart_selftest_log
)
2307 pout("Try '-l [xselftest,]selftest' to read traditional SMART Self Test Log\n");
2311 // Print SMART self-test log
2312 if (do_smart_selftest_log
) {
2313 if (!isSmartTestLogCapable(&smartval
, &drive
)){
2314 pout("Warning: device does not support Self Test Logging\n");
2315 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2317 ata_smart_selftestlog smartselftest
; memset(&smartselftest
, 0, sizeof(smartselftest
));
2318 if(ataReadSelfTestLog(device
, &smartselftest
, fix_firmwarebug
)){
2319 pout("Smartctl: SMART Self Test Log Read Failed\n");
2320 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2324 if (ataPrintSmartSelfTestlog(&smartselftest
, !con
->printing_switchable
, fix_firmwarebug
))
2331 // Print SMART selective self-test log
2332 if (options
.smart_selective_selftest_log
) {
2333 ata_selective_self_test_log log
;
2335 if (!isSupportSelectiveSelfTest(&smartval
))
2336 pout("Device does not support Selective Self Tests/Logging\n");
2337 else if(ataReadSelectiveSelfTestLog(device
, &log
)) {
2338 pout("Smartctl: SMART Selective Self Test Log Read Failed\n");
2339 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2343 // If any errors were found, they are logged in the SMART Self-test log.
2344 // So there is no need to print the Selective Self Test log in silent
2346 if (!con
->printing_switchable
) ataPrintSelectiveSelfTestLog(&log
, &smartval
);
2353 bool sct_ok
= false;
2354 if ( options
.sct_temp_sts
|| options
.sct_temp_hist
|| options
.sct_temp_int
2355 || options
.sct_erc_get
|| options
.sct_erc_set
) {
2356 if (!isSCTCapable(&drive
)) {
2357 pout("Warning: device does not support SCT Commands\n");
2358 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2364 // Print SCT status and temperature history table
2365 if (sct_ok
&& (options
.sct_temp_sts
|| options
.sct_temp_hist
|| options
.sct_temp_int
)) {
2367 if (options
.sct_temp_sts
|| options
.sct_temp_hist
) {
2368 ata_sct_status_response sts
;
2369 ata_sct_temperature_history_table tmh
;
2370 if (!options
.sct_temp_hist
) {
2371 // Read SCT status only
2372 if (ataReadSCTStatus(device
, &sts
)) {
2373 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2378 if (!isSCTDataTableCapable(&drive
)) {
2379 pout("Warning: device does not support SCT Data Table command\n");
2380 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2383 // Read SCT status and temperature history
2384 if (ataReadSCTTempHist(device
, &tmh
, &sts
)) {
2385 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2389 if (options
.sct_temp_sts
)
2390 ataPrintSCTStatus(&sts
);
2391 if (options
.sct_temp_hist
)
2392 ataPrintSCTTempHist(&tmh
);
2395 if (options
.sct_temp_int
) {
2396 // Set new temperature logging interval
2397 if (!isSCTFeatureControlCapable(&drive
)) {
2398 pout("Warning: device does not support SCT Feature Control command\n");
2399 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2402 if (ataSetSCTTempInterval(device
, options
.sct_temp_int
, options
.sct_temp_int_pers
)) {
2403 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2406 pout("Temperature Logging Interval set to %u minute%s (%s)\n",
2407 options
.sct_temp_int
, (options
.sct_temp_int
== 1 ? "" : "s"),
2408 (options
.sct_temp_int_pers
? "persistent" : "volatile"));
2414 // SCT Error Recovery Control
2415 if (sct_ok
&& (options
.sct_erc_get
|| options
.sct_erc_set
)) {
2416 if (!isSCTErrorRecoveryControlCapable(&drive
)) {
2417 pout("Warning: device does not support SCT Error Recovery Control command\n");
2418 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2421 bool sct_erc_get
= options
.sct_erc_get
;
2422 if (options
.sct_erc_set
) {
2423 // Set SCT Error Recovery Control
2424 if ( ataSetSCTErrorRecoveryControltime(device
, 1, options
.sct_erc_readtime
)
2425 || ataSetSCTErrorRecoveryControltime(device
, 2, options
.sct_erc_writetime
)) {
2426 pout("Warning: device does not support SCT (Set) Error Recovery Control command\n");
2427 pout("Suggest common arguments: scterc,70,70 to enable ERC or sct,0,0 to disable\n");
2428 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2429 sct_erc_get
= false;
2436 // Print SCT Error Recovery Control
2437 unsigned short read_timer
, write_timer
;
2438 if ( ataGetSCTErrorRecoveryControltime(device
, 1, read_timer
)
2439 || ataGetSCTErrorRecoveryControltime(device
, 2, write_timer
)) {
2440 pout("Warning: device does not support SCT (Get) Error Recovery Control command\n");
2441 if (options
.sct_erc_set
)
2442 pout("The previous SCT (Set) Error Recovery Control command succeeded\n");
2443 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2446 ataPrintSCTErrorRecoveryControl(read_timer
, write_timer
);
2452 // Print SATA Phy Event Counters
2453 if (options
.sataphy
) {
2454 unsigned nsectors
= GetNumLogSectors(gplogdir
, 0x11, true);
2456 pout("SATA Phy Event Counters (GP Log 0x11) not supported\n");
2457 else if (nsectors
!= 1)
2458 pout("SATA Phy Event Counters with %u sectors not supported\n", nsectors
);
2460 unsigned char log_11
[512] = {0, };
2461 unsigned char features
= (options
.sataphy_reset
? 0x01 : 0x00);
2462 if (!ataReadLogExt(device
, 0x11, features
, 0, log_11
, 1))
2463 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2465 PrintSataPhyEventCounters(log_11
, options
.sataphy_reset
);
2469 // START OF THE TESTING SECTION OF THE CODE. IF NO TESTING, RETURN
2470 if (!smart_val_ok
|| options
.smart_selftest_type
== -1)
2473 pout("=== START OF OFFLINE IMMEDIATE AND SELF-TEST SECTION ===\n");
2474 // if doing a self-test, be sure it's supported by the hardware
2475 switch (options
.smart_selftest_type
) {
2476 case OFFLINE_FULL_SCAN
:
2477 if (!isSupportExecuteOfflineImmediate(&smartval
)){
2478 pout("Warning: device does not support Execute Offline Immediate function.\n\n");
2479 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2482 case ABORT_SELF_TEST
:
2483 case SHORT_SELF_TEST
:
2484 case EXTEND_SELF_TEST
:
2485 case SHORT_CAPTIVE_SELF_TEST
:
2486 case EXTEND_CAPTIVE_SELF_TEST
:
2487 if (!isSupportSelfTest(&smartval
)){
2488 pout("Warning: device does not support Self-Test functions.\n\n");
2489 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2492 case CONVEYANCE_SELF_TEST
:
2493 case CONVEYANCE_CAPTIVE_SELF_TEST
:
2494 if (!isSupportConveyanceSelfTest(&smartval
)){
2495 pout("Warning: device does not support Conveyance Self-Test functions.\n\n");
2496 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2499 case SELECTIVE_SELF_TEST
:
2500 case SELECTIVE_CAPTIVE_SELF_TEST
:
2501 if (!isSupportSelectiveSelfTest(&smartval
)){
2502 pout("Warning: device does not support Selective Self-Test functions.\n\n");
2503 failuretest(MANDATORY_CMD
, returnval
|=FAILSMART
);
2507 pout("Internal error in smartctl: smart_test_type==%d not recognized\n", options
.smart_selftest_type
);
2508 pout("Please contact smartmontools developers at %s.\n", PACKAGE_BUGREPORT
);
2509 EXIT(returnval
|=FAILCMD
);
2512 // Now do the test. Note ataSmartTest prints its own error/success
2514 if (ataSmartTest(device
, options
.smart_selftest_type
, options
.smart_selective_args
,
2515 &smartval
, get_num_sectors(&drive
) ))
2516 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2518 // Tell user how long test will take to complete. This is tricky
2519 // because in the case of an Offline Full Scan, the completion
2520 // timer is volatile, and needs to be read AFTER the command is
2521 // given. If this will interrupt the Offline Full Scan, we don't
2522 // do it, just warn user.
2523 if (options
.smart_selftest_type
== OFFLINE_FULL_SCAN
) {
2524 if (isSupportOfflineAbort(&smartval
))
2525 pout("Note: giving further SMART commands will abort Offline testing\n");
2526 else if (ataReadSmartValues(device
, &smartval
)){
2527 pout("Smartctl: SMART Read Values failed.\n");
2528 failuretest(OPTIONAL_CMD
, returnval
|=FAILSMART
);
2532 // Now say how long the test will take to complete
2533 int timewait
= TestTime(&smartval
, options
.smart_selftest_type
);
2535 time_t t
=time(NULL
);
2536 if (options
.smart_selftest_type
== OFFLINE_FULL_SCAN
) {
2538 pout("Please wait %d seconds for test to complete.\n", (int)timewait
);
2541 pout("Please wait %d minutes for test to complete.\n", (int)timewait
);
2543 pout("Test will complete after %s\n", ctime(&t
));
2545 if ( options
.smart_selftest_type
!= SHORT_CAPTIVE_SELF_TEST
2546 && options
.smart_selftest_type
!= EXTEND_CAPTIVE_SELF_TEST
2547 && options
.smart_selftest_type
!= CONVEYANCE_CAPTIVE_SELF_TEST
2548 && options
.smart_selftest_type
!= SELECTIVE_CAPTIVE_SELF_TEST
)
2549 pout("Use smartctl -X to abort test.\n");