4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2002-6 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
8 * Copyright (C) 2000 Andre Hedrick <andre@linux-ide.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/
38 const char *atacmds_c_cvsid
="$Id: atacmds.c,v 1.168 2006/04/12 17:01:46 ballen4705 Exp $"
39 ATACMDS_H_CVSID CONFIG_H_CVSID EXTERN_H_CVSID INT64_H_CVSID UTILITY_H_CVSID
;
41 // to hold onto exit code for atexit routine
42 extern int exitstatus
;
44 // for passing global control variables
45 extern smartmonctrl
*con
;
47 // These Drive Identity tables are taken from hdparm 5.2, and are also
48 // given in the ATA/ATAPI specs for the IDENTIFY DEVICE command. Note
49 // that SMART was first added into the ATA/ATAPI-3 Standard with
50 // Revision 3 of the document, July 25, 1995. Look at the "Document
51 // Status" revision commands at the beginning of
52 // http://www.t13.org/project/d2008r6.pdf to see this.
53 #define NOVAL_0 0x0000
54 #define NOVAL_1 0xffff
55 /* word 81: minor version number */
56 #define MINOR_MAX 0x22
57 const char *minor_str
[] = { /* word 81 value: */
58 "Device does not report version", /* 0x0000 */
59 "ATA-1 X3T9.2 781D prior to revision 4", /* 0x0001 */
60 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
61 "ATA-1 X3T9.2 781D revision 4", /* 0x0003 */
62 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
63 "ATA-2 X3T10 948D prior to revision 2k", /* 0x0005 */
64 "ATA-3 X3T10 2008D revision 1", /* 0x0006 */ /* SMART NOT INCLUDED */
65 "ATA-2 X3T10 948D revision 2k", /* 0x0007 */
66 "ATA-3 X3T10 2008D revision 0", /* 0x0008 */
67 "ATA-2 X3T10 948D revision 3", /* 0x0009 */
68 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
69 "ATA-3 X3T10 2008D revision 6", /* 0x000b */ /* 1st VERSION WITH SMART */
70 "ATA-3 X3T13 2008D revision 7 and 7a", /* 0x000c */
71 "ATA/ATAPI-4 X3T13 1153D revision 6", /* 0x000d */
72 "ATA/ATAPI-4 T13 1153D revision 13", /* 0x000e */
73 "ATA/ATAPI-4 X3T13 1153D revision 7", /* 0x000f */
74 "ATA/ATAPI-4 T13 1153D revision 18", /* 0x0010 */
75 "ATA/ATAPI-4 T13 1153D revision 15", /* 0x0011 */
76 "ATA/ATAPI-4 published, ANSI NCITS 317-1998", /* 0x0012 */
77 "ATA/ATAPI-5 T13 1321D revision 3", /* 0x0013 */
78 "ATA/ATAPI-4 T13 1153D revision 14", /* 0x0014 */
79 "ATA/ATAPI-5 T13 1321D revision 1", /* 0x0015 */
80 "ATA/ATAPI-5 published, ANSI NCITS 340-2000", /* 0x0016 */
81 "ATA/ATAPI-4 T13 1153D revision 17", /* 0x0017 */
82 "ATA/ATAPI-6 T13 1410D revision 0", /* 0x0018 */
83 "ATA/ATAPI-6 T13 1410D revision 3a", /* 0x0019 */
84 "ATA/ATAPI-7 T13 1532D revision 1", /* 0x001a */
85 "ATA/ATAPI-6 T13 1410D revision 2", /* 0x001b */
86 "ATA/ATAPI-6 T13 1410D revision 1", /* 0x001c */
87 "reserved", /* 0x001d */
88 "ATA/ATAPI-7 T13 1532D revision 0", /* 0x001e */
89 "reserved", /* 0x001f */
90 "reserved", /* 0x0020 */
91 "ATA/ATAPI-7 T13 1532D revision 4a", /* 0x0021 */
92 "ATA/ATAPI-6 published, ANSI INCITS 361-2002" /* 0x0022 */
95 // NOTE ATA/ATAPI-4 REV 4 was the LAST revision where the device
96 // attribute structures were NOT completely vendor specific. So any
97 // disk that is ATA/ATAPI-4 or above can not be trusted to show the
98 // vendor values in sensible format.
100 // Negative values below are because it doesn't support SMART
101 const int actual_ver
[] = {
103 0, /* 0x0000 WARNING: */
104 1, /* 0x0001 WARNING: */
105 1, /* 0x0002 WARNING: */
106 1, /* 0x0003 WARNING: */
107 2, /* 0x0004 WARNING: This array */
108 2, /* 0x0005 WARNING: corresponds */
109 -3, /*<== */ /* 0x0006 WARNING: *exactly* */
110 2, /* 0x0007 WARNING: to the ATA/ */
111 -3, /*<== */ /* 0x0008 WARNING: ATAPI version */
112 2, /* 0x0009 WARNING: listed in */
113 3, /* 0x000a WARNING: the */
114 3, /* 0x000b WARNING: minor_str */
115 3, /* 0x000c WARNING: array */
116 4, /* 0x000d WARNING: above. */
117 4, /* 0x000e WARNING: */
118 4, /* 0x000f WARNING: If you change */
119 4, /* 0x0010 WARNING: that one, */
120 4, /* 0x0011 WARNING: change this one */
121 4, /* 0x0012 WARNING: too!!! */
122 5, /* 0x0013 WARNING: */
123 4, /* 0x0014 WARNING: */
124 5, /* 0x0015 WARNING: */
125 5, /* 0x0016 WARNING: */
126 4, /* 0x0017 WARNING: */
127 6, /* 0x0018 WARNING: */
128 6, /* 0x0019 WARNING: */
129 7, /* 0x001a WARNING: */
130 6, /* 0x001b WARNING: */
131 6, /* 0x001c WARNING: */
132 0, /* 0x001d WARNING: */
133 7, /* 0x001e WARNING: */
134 0, /* 0x001f WARNING: */
135 0, /* 0x0020 WARNING: */
136 7, /* 0x0021 WARNING: */
137 6 /* 0x0022 WARNING: */
140 // When you add additional items to this list, you should then:
141 // 0 -- update this list
142 // 1 -- modify the following function parse_attribute_def()
143 // 2 -- if needed, modify ataPrintSmartAttribRawValue()
144 // 3 - if needed, modify ataPrintSmartAttribName()
145 // 4 -- add #define PRESET_N_DESCRIPTION at top of knowndrives.c
146 // 5 -- add drive in question into knowndrives[] table in knowndrives.c
147 // 6 -- update smartctl.8
148 // 7 -- update smartd.8
149 // 8 -- do "make smartd.conf.5" to update smartd.conf.5
150 // 9 -- update CHANGELOG file
151 const char *vendorattributeargs
[] = {
167 "200,writeerrorcount",
177 "201,detectedtacount",
179 "192,emergencyretractcyclect",
181 "198,offlinescanuncsectorct",
182 // NULL should always terminate the array
186 // This are the meanings of the Self-test failure checkpoint byte.
187 // This is in the self-test log at offset 4 bytes into the self-test
188 // descriptor and in the SMART READ DATA structure at byte offset
189 // 371. These codes are not well documented. The meanings returned by
190 // this routine are used (at least) by Maxtor and IBM. Returns NULL if
191 // not recognized. Currently the maximum length is 15 bytes.
192 const char *SelfTestFailureCodeName(unsigned char which
){
198 return "Servo_Basic";
200 return "Servo_Random";
202 return "G-list_Scan";
204 return "Handling_Damage";
212 // This is a utility function for parsing pairs like "9,minutes" or
213 // "220,temp", and putting the correct flag into the attributedefs
214 // array. Returns 1 if problem, 0 if pair has been recongized.
215 int parse_attribute_def(char *pair
, unsigned char **defsptr
){
220 // If array does not exist, allocate it
221 if (!*defsptr
&& !(*defsptr
=(unsigned char *)calloc(MAX_ATTRIBUTE_NUM
, 1))){
222 pout("Out of memory in parse_attribute_def\n");
228 // look along list and see if we find the pair
229 for (i
=0; vendorattributeargs
[i
] && strcmp(pair
, vendorattributeargs
[i
]); i
++);
233 // attribute 9 is power on time in minutes
237 // attribute 9 is power-on-time in seconds
241 // attribute 9 is temperature in celsius
245 // attribute 220 is temperature in celsius
249 // print all attributes in raw 8-bit form
250 for (j
=0; j
<MAX_ATTRIBUTE_NUM
; j
++)
254 // print all attributes in raw 16-bit form
255 for (j
=0; j
<MAX_ATTRIBUTE_NUM
; j
++)
259 // print all attributes in raw 48-bit form
260 for (j
=0; j
<MAX_ATTRIBUTE_NUM
; j
++)
264 // attribute 200 is write error count
268 // attribute 9 increments once every 30 seconds (power on time
273 // attribute 194 is ten times disk temp in Celsius
277 // attribute 194 is unknown
281 // Hitachi : Attributes 193 has 2 values : 1 load, 1 normal unload
300 // At this point, either the pair was not found, or it is of the
301 // form N,uninterpreted, in which case we need to parse N
302 j
=sscanf(pair
,"%d,%14s", &i
, temp
);
304 // if no match to pattern, unrecognized
305 if (j
!=2 || i
<0 || i
>255)
308 // check for recognized strings
309 if (!strcmp(temp
, "raw8")) {
314 // check for recognized strings
315 if (!strcmp(temp
, "raw16")) {
320 // check for recognized strings
321 if (!strcmp(temp
, "raw48")) {
326 // didn't recognize the string
330 // Structure used in sorting the array vendorattributeargs[].
331 typedef struct vaa_pair_s
{
333 const char *padded_vaa
;
336 // Returns a copy of s with all numbers of less than three digits padded with
337 // leading zeros. Returns NULL if there isn't enough memory available. The
338 // memory for the string is dynamically allocated and should be freed by the
340 char *pad_numbers(const char *s
)
344 int i
, len
, ndigits
= 0;
346 // Allocate the maximum possible amount of memory needed.
347 if (!(t
= (char *)malloc(strlen(s
)*2+2)))
350 // Copy the string s to t, padding any numbers of less than three digits
351 // with leading zeros. The string is copied backwards to simplify the code.
354 while (( r
-- >= s
)) {
355 if (isdigit((int)*r
))
357 else if (ndigits
> 0) {
358 while (ndigits
++ < 3)
366 // Reverse the string in t.
368 for (i
= 0; i
< len
/2; i
++) {
377 // Comparison function for qsort(). Used by sort_vendorattributeargs().
378 int compare_vaa_pairs(const void *a
, const void *b
)
380 vaa_pair
*p
= (vaa_pair
*)a
;
381 vaa_pair
*q
= (vaa_pair
*)b
;
383 return strcmp(p
->padded_vaa
, q
->padded_vaa
);
386 // Returns a sorted list of vendorattributeargs or NULL if there is not enough
387 // memory available. The memory for the list is allocated dynamically and
388 // should be freed by the caller.
389 // To perform the sort, any numbers in the strings are padded out to three
390 // digits by adding leading zeros. For example,
392 // "9,minutes" becomes "009,minutes"
393 // "N,raw16" becomes "N,raw016"
395 // and the original strings are paired with the padded strings. The list of
396 // pairs is then sorted by comparing the padded strings (using strcmp) and the
397 // result is then the list of unpadded strings.
399 const char **sort_vendorattributeargs(void) {
400 const char **ps
, **sorted_list
= NULL
;
401 vaa_pair
*pairs
, *pp
;
404 // Figure out how many strings are in vendorattributeargs[] (not including
405 // the terminating NULL).
406 count
= (sizeof vendorattributeargs
) / sizeof(char *) - 1;
408 // Construct a list of pairs of strings from vendorattributeargs[] and their
409 // padded equivalents.
410 if (!(pairs
= (vaa_pair
*)malloc(sizeof(vaa_pair
) * count
)))
412 for (ps
= vendorattributeargs
, pp
= pairs
; *ps
; ps
++, pp
++) {
414 if (!(pp
->padded_vaa
= pad_numbers(*ps
)))
418 // Sort the array of vaa_pair structures by comparing the padded strings
420 qsort(pairs
, count
, sizeof(vaa_pair
), compare_vaa_pairs
);
422 // Construct the sorted list of strings.
423 if (!(sorted_list
= (const char **)malloc(sizeof vendorattributeargs
)))
425 for (ps
= sorted_list
, pp
= pairs
, i
= 0; i
< count
; ps
++, pp
++, i
++)
431 for (i
= 0; i
< count
; i
++)
432 if (pairs
[i
].padded_vaa
)
433 free((void *)pairs
[i
].padded_vaa
);
437 // If there was a problem creating the list then sorted_list should now
442 // Function to return a multiline string containing a list of the arguments in
443 // vendorattributeargs[]. The strings are preceeded by tabs and followed
444 // (except for the last) by newlines.
445 // This function allocates the required memory for the string and the caller
446 // must use free() to free it. It returns NULL if the required memory can't
448 char *create_vendor_attribute_arg_list(void){
449 const char **ps
, **sorted
;
453 // Get a sorted list of vendor attribute arguments. If the sort fails
454 // (which should only happen if the system is really low on memory) then just
455 // use the unordered list.
456 if (!(sorted
= (const char **) sort_vendorattributeargs()))
457 sorted
= vendorattributeargs
;
459 // Calculate the required number of characters
460 len
= 1; // At least one char ('\0')
461 for (ps
= sorted
; *ps
!= NULL
; ps
++) {
462 len
+= 1; // For the tab
463 len
+= strlen(*ps
); // For the actual argument string
465 len
++; // For the newline if required
468 // Attempt to allocate memory for the string
469 if (!(s
= (char *)malloc(len
)))
472 // Construct the string
474 for (ps
= sorted
; *ps
!= NULL
; ps
++) {
481 free((char **)sorted
);
483 // Return a pointer to the string
487 // swap two bytes. Point to low address
488 void swap2(char *location
){
490 *location
=*(location
+1);
495 // swap four bytes. Point to low address
496 void swap4(char *location
){
498 *location
=*(location
+3);
504 // swap eight bytes. Points to low address
505 void swap8(char *location
){
507 *location
=*(location
+7);
510 *(location
+1)=*(location
+6);
516 static char *commandstrings
[]={
519 "SMART AUTOMATIC ATTRIBUTE SAVE",
520 "SMART IMMEDIATE OFFLINE",
521 "SMART AUTO OFFLINE",
523 "SMART STATUS CHECK",
524 "SMART READ ATTRIBUTE VALUES",
525 "SMART READ ATTRIBUTE THRESHOLDS",
528 "IDENTIFY PACKET DEVICE",
531 "WARNING (UNDEFINED COMMAND -- CONTACT DEVELOPERS AT " PACKAGE_BUGREPORT
")\n"
534 void prettyprint(unsigned char *stuff
, char *name
){
536 pout("\n===== [%s] DATA START (BASE-16) =====\n", name
);
537 for (i
=0; i
<32; i
++){
538 pout("%03d-%03d: ", 16*i
, 16*(i
+1)-1);
540 pout("%02x ",*stuff
++);
541 pout("%02x\n",*stuff
++);
543 pout("===== [%s] DATA END (512 Bytes) =====\n\n", name
);
546 // This function provides the pretty-print reporting for SMART
547 // commands: it implements the various -r "reporting" options for ATA
549 int smartcommandhandler(int device
, smart_command_set command
, int select
, char *data
){
552 // This conditional is true for commands that return data
553 int getsdata
=(command
==PIDENTIFY
||
556 command
==READ_THRESHOLDS
||
557 command
==READ_VALUES
||
558 command
==CHECK_POWER_MODE
);
560 int sendsdata
=(command
==WRITE_LOG
);
562 // If reporting is enabled, say what the command will be before it's executed
563 if (con
->reportataioctl
){
564 // conditional is true for commands that use parameters
565 int usesparam
=(command
==READ_LOG
||
566 command
==AUTO_OFFLINE
||
568 command
==IMMEDIATE_OFFLINE
||
571 pout("\nREPORT-IOCTL: DeviceFD=%d Command=%s", device
, commandstrings
[command
]);
573 pout(" InputParameter=%d\n", select
);
578 if ((getsdata
|| sendsdata
) && !data
){
579 pout("REPORT-IOCTL: Unable to execute command %s : data destination address is NULL\n", commandstrings
[command
]);
583 // The reporting is cleaner, and we will find coding bugs faster, if
584 // the commands that failed clearly return empty (zeroed) data
587 if (command
==CHECK_POWER_MODE
)
590 memset(data
, '\0', 512);
594 // If reporting is enabled, say what input was sent to the command
595 if (con
->reportataioctl
&& sendsdata
){
596 pout("REPORT-IOCTL: DeviceFD=%d Command=%s", device
, commandstrings
[command
]);
597 // if requested, pretty-print the output data structure
598 if (con
->reportataioctl
>1)
599 prettyprint((unsigned char *)data
, commandstrings
[command
]);
602 // In case the command produces an error, we'll want to know what it is:
605 // now execute the command
606 switch (con
->controller_type
) {
607 case CONTROLLER_3WARE_678K
:
608 case CONTROLLER_3WARE_678K_CHAR
:
609 case CONTROLLER_3WARE_9000_CHAR
:
610 retval
=escalade_command_interface(device
, con
->controller_port
-1, con
->controller_type
, command
, select
, data
);
611 if (retval
&& con
->controller_port
<=0)
612 pout("WARNING: apparently missing '-d 3ware,N' disk specification\n");
614 case CONTROLLER_MARVELL_SATA
:
615 retval
=marvell_command_interface(device
, command
, select
, data
);
618 retval
=ata_command_interface(device
, command
, select
, data
);
621 // If reporting is enabled, say what output was produced by the command
622 if (con
->reportataioctl
){
624 pout("REPORT-IOCTL: DeviceFD=%d Command=%s returned %d errno=%d [%s]\n",
625 device
, commandstrings
[command
], retval
, errno
, strerror(errno
));
627 pout("REPORT-IOCTL: DeviceFD=%d Command=%s returned %d\n",
628 device
, commandstrings
[command
], retval
);
630 // if requested, pretty-print the output data structure
631 if (con
->reportataioctl
>1 && getsdata
) {
632 if (command
==CHECK_POWER_MODE
)
633 pout("Sector Count Register (BASE-16): %02x\n", (unsigned char)(*data
));
635 prettyprint((unsigned char *)data
, commandstrings
[command
]);
642 // This function computes the checksum of a single disk sector (512
643 // bytes). Returns zero if checksum is OK, nonzero if the checksum is
644 // incorrect. The size (512) is correct for all SMART structures.
645 unsigned char checksum(unsigned char *buffer
){
649 for (i
=0; i
<512; i
++)
655 // returns -1 if command fails or the device is in Sleep mode, else
656 // value of Sector Count register. Sector Count result values:
657 // 00h device is in Standby mode.
658 // 80h device is in Idle mode.
659 // FFh device is in Active mode or Idle mode.
661 int ataCheckPowerMode(int device
) {
662 unsigned char result
;
664 if ((smartcommandhandler(device
, CHECK_POWER_MODE
, 0, (char *)&result
)))
667 if (result
!=0 && result
!=0x80 && result
!=0xff)
668 pout("ataCheckPowerMode(): ATA CHECK POWER MODE returned unknown Sector Count Register value %02x\n", result
);
676 // Reads current Device Identity info (512 bytes) into buf. Returns 0
677 // if all OK. Returns -1 if no ATA Device identity can be
678 // established. Returns >0 if Device is ATA Packet Device (not SMART
679 // capable). The value of the integer helps identify the type of
680 // Packet device, which is useful so that the user can connect the
681 // formal device number with whatever object is inside their computer.
682 int ataReadHDIdentity (int device
, struct ata_identify_device
*buf
){
683 unsigned short *rawshort
=(unsigned short *)buf
;
684 unsigned char *rawbyte
=(unsigned char *)buf
;
686 // See if device responds either to IDENTIFY DEVICE or IDENTIFY
688 if ((smartcommandhandler(device
, IDENTIFY
, 0, (char *)buf
))){
689 if (smartcommandhandler(device
, PIDENTIFY
, 0, (char *)buf
)){
695 // if machine is big-endian, swap byte order as needed
696 // (the NetBSD kernel does deliver the results in host byte order)
700 // swap various capability words that are needed
702 swap2((char *)(buf
->words047_079
+i
));
704 for (i
=80; i
<=87; i
++)
705 swap2((char *)(rawshort
+i
));
707 for (i
=0; i
<168; i
++)
708 swap2((char *)(buf
->words088_255
+i
));
712 // If there is a checksum there, validate it
713 if ((rawshort
[255] & 0x00ff) == 0x00a5 && checksum(rawbyte
))
714 checksumwarning("Drive Identity Structure");
716 // If this is a PACKET DEVICE, return device type
717 if (rawbyte
[1] & 0x80)
718 return 1+(rawbyte
[1] & 0x1f);
720 // Not a PACKET DEVICE
724 // Returns ATA version as an integer, and a pointer to a string
725 // describing which revision. Note that Revision 0 of ATA-3 does NOT
726 // support SMART. For this one case we return -3 rather than +3 as
727 // the version number. See notes above.
728 int ataVersionInfo (const char** description
, struct ata_identify_device
*drive
, unsigned short *minor
){
729 unsigned short major
;
732 // check that arrays at the top of this file are defined
734 if (sizeof(minor_str
) != sizeof(char *)*(1+MINOR_MAX
)){
735 pout("Internal error in ataVersionInfo(). minor_str[] size %d\n"
736 "is not consistent with value of MINOR_MAX+1 = %d\n",
737 (int)(sizeof(minor_str
)/sizeof(char *)), MINOR_MAX
+1);
741 if (sizeof(actual_ver
) != sizeof(int)*(1+MINOR_MAX
)){
742 pout("Internal error in ataVersionInfo(). actual_ver[] size %d\n"
743 "is not consistent with value of MINOR_MAX = %d\n",
744 (int)(sizeof(actual_ver
)/sizeof(int)), MINOR_MAX
+1);
749 // get major and minor ATA revision numbers
750 major
=drive
->major_rev_num
;
751 *minor
=drive
->minor_rev_num
;
753 // First check if device has ANY ATA version information in it
754 if (major
==NOVAL_0
|| major
==NOVAL_1
) {
759 // The minor revision number has more information - try there first
760 if (*minor
&& (*minor
<=MINOR_MAX
)){
761 int std
= actual_ver
[*minor
];
763 *description
=minor_str
[*minor
];
768 // HDPARM has a very complicated algorithm from here on. Since SMART only
769 // exists on ATA-3 and later standards, let's punt on this. If you don't
770 // like it, please fix it. The code's in CVS.
772 if (major
& (0x1<<i
))
782 // returns 1 if SMART supported, 0 if SMART unsupported, -1 if can't tell
783 int ataSmartSupport(struct ata_identify_device
*drive
){
784 unsigned short word82
=drive
->command_set_1
;
785 unsigned short word83
=drive
->command_set_2
;
787 // check if words 82/83 contain valid info
788 if ((word83
>>14) == 0x01)
789 // return value of SMART support bit
790 return word82
& 0x0001;
792 // since we can're rely on word 82, we don't know if SMART supported
796 // returns 1 if SMART enabled, 0 if SMART disabled, -1 if can't tell
797 int ataIsSmartEnabled(struct ata_identify_device
*drive
){
798 unsigned short word85
=drive
->cfs_enable_1
;
799 unsigned short word87
=drive
->csf_default
;
801 // check if words 85/86/87 contain valid info
802 if ((word87
>>14) == 0x01)
803 // return value of SMART enabled bit
804 return word85
& 0x0001;
806 // Since we can't rely word85, we don't know if SMART is enabled.
811 // Reads SMART attributes into *data
812 int ataReadSmartValues(int device
, struct ata_smart_values
*data
){
814 if (smartcommandhandler(device
, READ_VALUES
, 0, (char *)data
)){
815 syserror("Error SMART Values Read failed");
820 if (checksum((unsigned char *)data
))
821 checksumwarning("SMART Attribute Data Structure");
823 // byte swap if needed
826 swap2((char *)&(data
->revnumber
));
827 swap2((char *)&(data
->total_time_to_complete_off_line
));
828 swap2((char *)&(data
->smart_capability
));
829 for (i
=0; i
<NUMBER_ATA_SMART_ATTRIBUTES
; i
++){
830 struct ata_smart_attribute
*x
=data
->vendor_attributes
+i
;
831 swap2((char *)&(x
->flags
));
839 // This corrects some quantities that are byte reversed in the SMART
841 void fixsamsungselftestlog(struct ata_smart_selftestlog
*data
){
844 // bytes 508/509 (numbered from 0) swapped (swap of self-test index
845 // with one byte of reserved.
846 swap2((char *)&(data
->mostrecenttest
));
848 // LBA low register (here called 'selftestnumber", containing
849 // information about the TYPE of the self-test) is byte swapped with
850 // Self-test execution status byte. These are bytes N, N+1 in the
853 swap2((char *)&(data
->selftest_struct
[i
].selftestnumber
));
858 // Reads the Self Test Log (log #6)
859 int ataReadSelfTestLog (int device
, struct ata_smart_selftestlog
*data
){
861 // get data from device
862 if (smartcommandhandler(device
, READ_LOG
, 0x06, (char *)data
)){
863 syserror("Error SMART Error Self-Test Log Read failed");
867 // compute its checksum, and issue a warning if needed
868 if (checksum((unsigned char *)data
))
869 checksumwarning("SMART Self-Test Log Structure");
871 // fix firmware bugs in self-test log
872 if (con
->fixfirmwarebug
== FIX_SAMSUNG
)
873 fixsamsungselftestlog(data
);
875 // fix endian order, if needed
878 swap2((char*)&(data
->revnumber
));
879 for (i
=0; i
<21; i
++){
880 struct ata_smart_selftestlog_struct
*x
=data
->selftest_struct
+i
;
881 swap2((char *)&(x
->timestamp
));
882 swap4((char *)&(x
->lbafirstfailure
));
890 // Reads the Log Directory (log #0). Note: NO CHECKSUM!!
891 int ataReadLogDirectory (int device
, struct ata_smart_log_directory
*data
){
893 // get data from device
894 if (smartcommandhandler(device
, READ_LOG
, 0x00, (char *)data
)){
898 // swap endian order if needed
900 swap2((char *)&(data
->logversion
));
907 // Reads the selective self-test log (log #9)
908 int ataReadSelectiveSelfTestLog(int device
, struct ata_selective_self_test_log
*data
){
910 // get data from device
911 if (smartcommandhandler(device
, READ_LOG
, 0x09, (char *)data
)){
912 syserror("Error SMART Read Selective Self-Test Log failed");
916 // compute its checksum, and issue a warning if needed
917 if (checksum((unsigned char *)data
))
918 checksumwarning("SMART Selective Self-Test Log Structure");
920 // swap endian order if needed
923 swap2((char *)&(data
->logversion
));
925 swap8((char *)&(data
->span
[i
].start
));
926 swap8((char *)&(data
->span
[i
].end
));
928 swap8((char *)&(data
->currentlba
));
929 swap2((char *)&(data
->currentspan
));
930 swap2((char *)&(data
->flags
));
931 swap2((char *)&(data
->pendingtime
));
934 if (data
->logversion
!= 1)
935 pout("SMART Selective Self-Test Log Data Structure Revision Number (%d) should be 1\n", data
->logversion
);
940 // Writes the selective self-test log (log #9)
941 int ataWriteSelectiveSelfTestLog(int device
, struct ata_smart_values
*sv
){
943 struct ata_selective_self_test_log sstlog
, *data
=&sstlog
;
944 unsigned char cksum
=0;
945 unsigned char *ptr
=(unsigned char *)data
;
948 if (ataReadSelectiveSelfTestLog(device
, data
)) {
949 pout("Since Read failed, will not attempt to WRITE Selective Self-test Log\n");
953 // Fix logversion if needed
954 if (data
->logversion
!=1) {
955 pout("Error SMART Selective Self-Test Log Data Structure Revision not recognized\n"
956 "Revision number should be 1 but is %d. To be safe, aborting WRITE LOG\n", data
->logversion
);
960 // Host is NOT allowed to write selective self-test log if a selective
961 // self-test is in progress.
962 if (0<data
->currentspan
&& data
->currentspan
<6 && ((sv
->self_test_exec_status
)>>4)==15) {
963 pout("Error SMART Selective or other Self-Test in progress.\n");
969 memset(data
->span
+i
, 0, sizeof(struct test_span
));
971 // Set spans for testing
972 for (i
=0; i
<con
->smartselectivenumspans
; i
++){
973 data
->span
[i
].start
= con
->smartselectivespan
[i
][0];
974 data
->span
[i
].end
= con
->smartselectivespan
[i
][1];
977 // host must initialize to zero before initiating selective self-test
981 // Perform off-line scan after selective test?
982 if (1 == con
->scanafterselect
)
984 data
->flags
&= ~SELECTIVE_FLAG_DOSCAN
;
985 else if (2 == con
->scanafterselect
)
987 data
->flags
|= SELECTIVE_FLAG_DOSCAN
;
989 // Must clear active and pending flags before writing
990 data
->flags
&= ~(SELECTIVE_FLAG_ACTIVE
);
991 data
->flags
&= ~(SELECTIVE_FLAG_PENDING
);
993 // modify pending time?
994 if (con
->pendingtime
)
995 data
->pendingtime
=(unsigned short)(con
->pendingtime
-1);
997 // Set checksum to zero, then compute checksum
999 for (i
=0; i
<512; i
++)
1003 data
->checksum
=cksum
;
1005 // swap endian order if needed
1008 swap2((char *)&(data
->logversion
));
1010 swap8((char *)&(data
->span
[i
].start
));
1011 swap8((char *)&(data
->span
[i
].end
));
1013 swap8((char *)&(data
->currentlba
));
1014 swap2((char *)&(data
->currentspan
));
1015 swap2((char *)&(data
->flags
));
1016 swap2((char *)&(data
->pendingtime
));
1019 // write new selective self-test log
1020 if (smartcommandhandler(device
, WRITE_LOG
, 0x09, (char *)data
)){
1021 syserror("Error Write Selective Self-Test Log failed");
1028 // This corrects some quantities that are byte reversed in the SMART
1030 void fixsamsungerrorlog(struct ata_smart_errorlog
*data
){
1033 // FIXED IN SAMSUNG -25 FIRMWARE???
1034 // Device error count in bytes 452-3
1035 swap2((char *)&(data
->ata_error_count
));
1037 // FIXED IN SAMSUNG -22a FIRMWARE
1038 // step through 5 error log data structures
1039 for (i
=0; i
<5; i
++){
1040 // step through 5 command data structures
1042 // Command data structure 4-byte millisec timestamp. These are
1043 // bytes (N+8, N+9, N+10, N+11).
1044 swap4((char *)&(data
->errorlog_struct
[i
].commands
[j
].timestamp
));
1045 // Error data structure two-byte hour life timestamp. These are
1046 // bytes (N+28, N+29).
1047 swap2((char *)&(data
->errorlog_struct
[i
].error_struct
.timestamp
));
1052 // NEEDED ONLY FOR SAMSUNG -22 (some) -23 AND -24?? FIRMWARE
1053 void fixsamsungerrorlog2(struct ata_smart_errorlog
*data
){
1054 // Device error count in bytes 452-3
1055 swap2((char *)&(data
->ata_error_count
));
1059 // Reads the Summary SMART Error Log (log #1). The Comprehensive SMART
1060 // Error Log is #2, and the Extended Comprehensive SMART Error log is
1062 int ataReadErrorLog (int device
, struct ata_smart_errorlog
*data
){
1064 // get data from device
1065 if (smartcommandhandler(device
, READ_LOG
, 0x01, (char *)data
)){
1066 syserror("Error SMART Error Log Read failed");
1070 // compute its checksum, and issue a warning if needed
1071 if (checksum((unsigned char *)data
))
1072 checksumwarning("SMART ATA Error Log Structure");
1074 // Some disks have the byte order reversed in some SMART Summary
1075 // Error log entries
1076 if (con
->fixfirmwarebug
== FIX_SAMSUNG
)
1077 fixsamsungerrorlog(data
);
1078 else if (con
->fixfirmwarebug
== FIX_SAMSUNG2
)
1079 fixsamsungerrorlog2(data
);
1081 // Correct endian order if necessary
1085 // Device error count in bytes 452-3
1086 swap2((char *)&(data
->ata_error_count
));
1088 // step through 5 error log data structures
1089 for (i
=0; i
<5; i
++){
1090 // step through 5 command data structures
1092 // Command data structure 4-byte millisec timestamp
1093 swap4((char *)&(data
->errorlog_struct
[i
].commands
[j
].timestamp
));
1094 // Error data structure life timestamp
1095 swap2((char *)&(data
->errorlog_struct
[i
].error_struct
.timestamp
));
1102 int ataReadSmartThresholds (int device
, struct ata_smart_thresholds_pvt
*data
){
1104 // get data from device
1105 if (smartcommandhandler(device
, READ_THRESHOLDS
, 0, (char *)data
)){
1106 syserror("Error SMART Thresholds Read failed");
1110 // compute its checksum, and issue a warning if needed
1111 if (checksum((unsigned char *)data
))
1112 checksumwarning("SMART Attribute Thresholds Structure");
1114 // byte swap if needed
1116 swap2((char *)&(data
->revnumber
));
1121 int ataEnableSmart (int device
){
1122 if (smartcommandhandler(device
, ENABLE
, 0, NULL
)){
1123 syserror("Error SMART Enable failed");
1129 int ataDisableSmart (int device
){
1131 if (smartcommandhandler(device
, DISABLE
, 0, NULL
)){
1132 syserror("Error SMART Disable failed");
1138 int ataEnableAutoSave(int device
){
1139 if (smartcommandhandler(device
, AUTOSAVE
, 241, NULL
)){
1140 syserror("Error SMART Enable Auto-save failed");
1146 int ataDisableAutoSave(int device
){
1148 if (smartcommandhandler(device
, AUTOSAVE
, 0, NULL
)){
1149 syserror("Error SMART Disable Auto-save failed");
1155 // In *ALL* ATA standards the Enable/Disable AutoOffline command is
1156 // marked "OBSOLETE". It is defined in SFF-8035i Revision 2, and most
1157 // vendors still support it for backwards compatibility. IBM documents
1158 // it for some drives.
1159 int ataEnableAutoOffline (int device
){
1161 /* timer hard coded to 4 hours */
1162 if (smartcommandhandler(device
, AUTO_OFFLINE
, 248, NULL
)){
1163 syserror("Error SMART Enable Automatic Offline failed");
1169 // Another Obsolete Command. See comments directly above, associated
1170 // with the corresponding Enable command.
1171 int ataDisableAutoOffline (int device
){
1173 if (smartcommandhandler(device
, AUTO_OFFLINE
, 0, NULL
)){
1174 syserror("Error SMART Disable Automatic Offline failed");
1180 // If SMART is enabled, supported, and working, then this call is
1181 // guaranteed to return 1, else zero. Note that it should return 1
1182 // regardless of whether the disk's SMART status is 'healthy' or
1184 int ataDoesSmartWork(int device
){
1185 int retval
=smartcommandhandler(device
, STATUS
, 0, NULL
);
1193 // This function uses a different interface (DRIVE_TASK) than the
1194 // other commands in this file.
1195 int ataSmartStatus2(int device
){
1196 return smartcommandhandler(device
, STATUS_CHECK
, 0, NULL
);
1199 // This is the way to execute ALL tests: offline, short self-test,
1200 // extended self test, with and without captive mode, etc.
1201 int ataSmartTest(int device
, int testtype
, struct ata_smart_values
*sv
) {
1202 char cmdmsg
[128],*type
,*captive
;
1203 int errornum
, cap
, retval
, select
=0;
1205 // Boolean, if set, says test is captive
1206 cap
=testtype
& CAPTIVE_MASK
;
1208 // Set up strings that describe the type of test
1214 if (testtype
==OFFLINE_FULL_SCAN
)
1216 else if (testtype
==SHORT_SELF_TEST
|| testtype
==SHORT_CAPTIVE_SELF_TEST
)
1217 type
="Short self-test";
1218 else if (testtype
==EXTEND_SELF_TEST
|| testtype
==EXTEND_CAPTIVE_SELF_TEST
)
1219 type
="Extended self-test";
1220 else if (testtype
==CONVEYANCE_SELF_TEST
|| testtype
==CONVEYANCE_CAPTIVE_SELF_TEST
)
1221 type
="Conveyance self-test";
1222 else if ((select
=(testtype
==SELECTIVE_SELF_TEST
|| testtype
==SELECTIVE_CAPTIVE_SELF_TEST
)))
1223 type
="Selective self-test";
1225 type
="[Unrecognized] self-test";
1227 // If doing a selective self-test, first use WRITE_LOG to write the
1228 // selective self-test log.
1229 if (select
&& (retval
=ataWriteSelectiveSelfTestLog(device
, sv
))) {
1231 pout("Can't start selective self-test without aborting current test: use '-X' option to smartctl.\n");
1235 // Print ouf message that we are sending the command to test
1236 if (testtype
==ABORT_SELF_TEST
)
1237 sprintf(cmdmsg
,"Abort SMART off-line mode self-test routine");
1239 sprintf(cmdmsg
,"Execute SMART %s routine immediately in %s mode",type
,captive
);
1240 pout("Sending command: \"%s\".\n",cmdmsg
);
1244 pout("SPAN STARTING_LBA ENDING_LBA\n");
1245 for (i
= 0; i
< con
->smartselectivenumspans
; i
++)
1246 pout(" %d %20"PRId64
" %20"PRId64
"\n", i
,
1247 con
->smartselectivespan
[i
][0],
1248 con
->smartselectivespan
[i
][1]);
1251 // Now send the command to test
1252 errornum
=smartcommandhandler(device
, IMMEDIATE_OFFLINE
, testtype
, NULL
);
1254 if (errornum
&& !(cap
&& errno
==EIO
)){
1256 sprintf(errormsg
,"Command \"%s\" failed",cmdmsg
);
1262 // Since the command succeeded, tell user
1263 if (testtype
==ABORT_SELF_TEST
)
1264 pout("Self-testing aborted!\n");
1266 pout("Drive command \"%s\" successful.\nTesting has begun.\n",cmdmsg
);
1270 /* Test Time Functions */
1271 int TestTime(struct ata_smart_values
*data
,int testtype
){
1273 case OFFLINE_FULL_SCAN
:
1274 return (int) data
->total_time_to_complete_off_line
;
1275 case SHORT_SELF_TEST
:
1276 case SHORT_CAPTIVE_SELF_TEST
:
1277 return (int) data
->short_test_completion_time
;
1278 case EXTEND_SELF_TEST
:
1279 case EXTEND_CAPTIVE_SELF_TEST
:
1280 return (int) data
->extend_test_completion_time
;
1281 case CONVEYANCE_SELF_TEST
:
1282 case CONVEYANCE_CAPTIVE_SELF_TEST
:
1283 return (int) data
->conveyance_test_completion_time
;
1289 // This function tells you both about the ATA error log and the
1290 // self-test error log capability (introduced in ATA-5). The bit is
1291 // poorly documented in the ATA/ATAPI standard. Starting with ATA-6,
1292 // SMART error logging is also indicated in bit 0 of DEVICE IDENTIFY
1293 // word 84 and 87. Top two bits must match the pattern 01. BEFORE
1294 // ATA-6 these top two bits still had to match the pattern 01, but the
1295 // remaining bits were reserved (==0).
1296 int isSmartErrorLogCapable (struct ata_smart_values
*data
, struct ata_identify_device
*identity
){
1298 unsigned short word84
=identity
->command_set_extension
;
1299 unsigned short word87
=identity
->csf_default
;
1300 int isata6
=identity
->major_rev_num
& (0x01<<6);
1301 int isata7
=identity
->major_rev_num
& (0x01<<7);
1303 if ((isata6
|| isata7
) && (word84
>>14) == 0x01 && (word84
& 0x01))
1306 if ((isata6
|| isata7
) && (word87
>>14) == 0x01 && (word87
& 0x01))
1309 // otherwise we'll use the poorly documented capability bit
1310 return data
->errorlog_capability
& 0x01;
1313 // See previous function. If the error log exists then the self-test
1314 // log should (must?) also exist.
1315 int isSmartTestLogCapable (struct ata_smart_values
*data
, struct ata_identify_device
*identity
){
1317 unsigned short word84
=identity
->command_set_extension
;
1318 unsigned short word87
=identity
->csf_default
;
1319 int isata6
=identity
->major_rev_num
& (0x01<<6);
1320 int isata7
=identity
->major_rev_num
& (0x01<<7);
1322 if ((isata6
|| isata7
) && (word84
>>14) == 0x01 && (word84
& 0x02))
1325 if ((isata6
|| isata7
) && (word87
>>14) == 0x01 && (word87
& 0x02))
1329 // otherwise we'll use the poorly documented capability bit
1330 return data
->errorlog_capability
& 0x01;
1334 int isGeneralPurposeLoggingCapable(struct ata_identify_device
*identity
){
1335 unsigned short word84
=identity
->command_set_extension
;
1336 unsigned short word87
=identity
->csf_default
;
1338 // If bit 14 of word 84 is set to one and bit 15 of word 84 is
1339 // cleared to zero, the contents of word 84 contains valid support
1340 // information. If not, support information is not valid in this
1342 if ((word84
>>14) == 0x01)
1343 // If bit 5 of word 84 is set to one, the device supports the
1344 // General Purpose Logging feature set.
1345 return (word84
& (0x01 << 5));
1347 // If bit 14 of word 87 is set to one and bit 15 of word 87 is
1348 // cleared to zero, the contents of words (87:85) contain valid
1349 // information. If not, information is not valid in these words.
1350 if ((word87
>>14) == 0x01)
1351 // If bit 5 of word 87 is set to one, the device supports
1352 // the General Purpose Logging feature set.
1353 return (word87
& (0x01 << 5));
1360 // SMART self-test capability is also indicated in bit 1 of DEVICE
1361 // IDENTIFY word 87 (if top two bits of word 87 match pattern 01).
1362 // However this was only introduced in ATA-6 (but self-test log was in
1364 int isSupportExecuteOfflineImmediate(struct ata_smart_values
*data
){
1365 return data
->offline_data_collection_capability
& 0x01;
1367 // Note in the ATA-5 standard, the following bit is listed as "Vendor
1368 // Specific". So it may not be reliable. The only use of this that I
1369 // have found is in IBM drives, where it is well-documented. See for
1370 // example page 170, section 13.32.1.18 of the IBM Travelstar 40GNX
1371 // hard disk drive specifications page 164 Revision 1.1 22 Apr 2002.
1372 int isSupportAutomaticTimer(struct ata_smart_values
*data
){
1373 return data
->offline_data_collection_capability
& 0x02;
1375 int isSupportOfflineAbort(struct ata_smart_values
*data
){
1376 return data
->offline_data_collection_capability
& 0x04;
1378 int isSupportOfflineSurfaceScan(struct ata_smart_values
*data
){
1379 return data
->offline_data_collection_capability
& 0x08;
1381 int isSupportSelfTest (struct ata_smart_values
*data
){
1382 return data
->offline_data_collection_capability
& 0x10;
1384 int isSupportConveyanceSelfTest(struct ata_smart_values
*data
){
1385 return data
->offline_data_collection_capability
& 0x20;
1387 int isSupportSelectiveSelfTest(struct ata_smart_values
*data
){
1388 return data
->offline_data_collection_capability
& 0x40;
1393 // Loop over all valid attributes. If they are prefailure attributes
1394 // and are at or below the threshold value, then return the ID of the
1395 // first failing attribute found. Return 0 if all prefailure
1396 // attributes are in bounds. The spec says "Bit 0
1397 // -Pre-failure/advisory - If the value of this bit equals zero, an
1398 // attribute value less than or equal to its corresponding attribute
1399 // threshold indicates an advisory condition where the usage or age of
1400 // the device has exceeded its intended design life period. If the
1401 // value of this bit equals one, an atribute value less than or equal
1402 // to its corresponding attribute threshold indicates a pre-failure
1403 // condition where imminent loss of data is being predicted."
1406 // onlyfailed=0 : are or were any age or prefailure attributes <= threshold
1407 // onlyfailed=1: are any prefailure attributes <= threshold now
1408 int ataCheckSmart(struct ata_smart_values
*data
,
1409 struct ata_smart_thresholds_pvt
*thresholds
,
1413 // loop over all attributes
1414 for (i
=0; i
<NUMBER_ATA_SMART_ATTRIBUTES
; i
++){
1416 // pointers to disk's values and vendor's thresholds
1417 struct ata_smart_attribute
*disk
=data
->vendor_attributes
+i
;
1418 struct ata_smart_threshold_entry
*thre
=thresholds
->thres_entries
+i
;
1420 // consider only valid attributes
1421 if (disk
->id
&& thre
->id
){
1422 int failednow
,failedever
;
1424 failednow
=disk
->current
<= thre
->threshold
;
1425 failedever
=disk
->worst
<= thre
->threshold
;
1427 if (!onlyfailed
&& failedever
)
1430 if (onlyfailed
&& failednow
&& ATTRIBUTE_FLAGS_PREFAILURE(disk
->flags
))
1439 // This checks the n'th attribute in the attribute list, NOT the
1440 // attribute with id==n. If the attribute does not exist, or the
1441 // attribute is > threshold, then returns zero. If the attribute is
1442 // <= threshold (failing) then we the attribute number if it is a
1443 // prefail attribute. Else we return minus the attribute number if it
1444 // is a usage attribute.
1445 int ataCheckAttribute(struct ata_smart_values
*data
,
1446 struct ata_smart_thresholds_pvt
*thresholds
,
1448 struct ata_smart_attribute
*disk
;
1449 struct ata_smart_threshold_entry
*thre
;
1451 if (n
<0 || n
>=NUMBER_ATA_SMART_ATTRIBUTES
|| !data
|| !thresholds
)
1454 // pointers to disk's values and vendor's thresholds
1455 disk
=data
->vendor_attributes
+n
;
1456 thre
=thresholds
->thres_entries
+n
;
1461 // consider only valid attributes, check for failure
1462 if (!disk
->id
|| !thre
->id
|| (disk
->id
!= thre
->id
) || disk
->current
> thre
->threshold
)
1465 // We have found a failed attribute. Return positive or negative?
1466 if (ATTRIBUTE_FLAGS_PREFAILURE(disk
->flags
))
1469 return -1*(disk
->id
);
1473 // This routine prints the raw value of an attribute as a text string
1474 // into out. It also returns this 48-bit number as a long long. The
1475 // array defs[] contains non-zero values if particular attributes have
1476 // non-default interpretations.
1478 int64_t ataPrintSmartAttribRawValue(char *out
,
1479 struct ata_smart_attribute
*attribute
,
1480 unsigned char *defs
){
1484 unsigned char select
;
1486 // convert the six individual bytes to a long long (8 byte) integer.
1487 // This is the value that we'll eventually return.
1489 for (j
=0; j
<6; j
++) {
1490 // This looks a bit roundabout, but is necessary. Don't
1491 // succumb to the temptation to use raw[j]<<(8*j) since under
1492 // the normal rules this will be promoted to the native type.
1493 // On a 32 bit machine this might then overflow.
1495 temp
= attribute
->raw
[j
];
1500 // convert quantities to three two-byte words
1501 for (j
=0; j
<3; j
++){
1502 word
[j
] = attribute
->raw
[2*j
+1];
1504 word
[j
] |= attribute
->raw
[2*j
];
1507 // if no data array, Attributes have default interpretations
1509 select
=defs
[attribute
->id
];
1513 // Print six one-byte quantities.
1516 out
+=sprintf(out
, "%d ", attribute
->raw
[5-j
]);
1517 out
+=sprintf(out
, "%d ", attribute
->raw
[0]);
1521 // Print three two-byte quantities
1523 out
+=sprintf(out
, "%d %d %d", word
[2], word
[1], word
[0]);
1527 // Print one six-byte quantity
1529 out
+=sprintf(out
, "%"PRIu64
, rawvalue
);
1533 // This switch statement is where we handle Raw attributes
1534 // that are stored in an unusual vendor-specific format,
1535 switch (attribute
->id
){
1538 out
+=sprintf(out
, "%d", word
[0]);
1539 // if second nonzero then it stores the average spin-up time
1541 out
+=sprintf(out
, " (Average %d)", word
[1]);
1547 int64_t tmp1
=rawvalue
/60;
1548 int64_t tmp2
=rawvalue
%60;
1549 out
+=sprintf(out
, "%"PRIu64
"h+%02"PRIu64
"m", tmp1
, tmp2
);
1551 else if (select
==3){
1553 int64_t hours
=rawvalue
/3600;
1554 int64_t minutes
=(rawvalue
-3600*hours
)/60;
1555 int64_t seconds
=rawvalue
%60;
1556 out
+=sprintf(out
, "%"PRIu64
"h+%02"PRIu64
"m+%02"PRIu64
"s", hours
, minutes
, seconds
);
1558 else if (select
==4){
1559 // 30-second counter
1560 int64_t tmp1
=rawvalue
/120;
1561 int64_t tmp2
=(rawvalue
-120*tmp1
)/2;
1562 out
+=sprintf(out
, "%"PRIu64
"h+%02"PRIu64
"m", tmp1
, tmp2
);
1566 out
+=sprintf(out
, "%"PRIu64
, rawvalue
); //stored in hours
1568 // Load unload cycles
1572 long load
=attribute
->raw
[0] + (attribute
->raw
[1]<<8) + (attribute
->raw
[2]<<16);
1573 long unload
=attribute
->raw
[3] + (attribute
->raw
[4]<<8) + (attribute
->raw
[5]<<16);
1574 out
+=sprintf(out
, "%lu/%lu", load
, unload
);
1578 out
+=sprintf(out
, "%"PRIu64
, rawvalue
);
1583 // ten times temperature in Celsius
1585 int tenths
=word
[0]%10;
1586 out
+=sprintf(out
, "%d.%d", deg
, tenths
);
1589 // unknown attribute
1590 out
+=sprintf(out
, "%"PRIu64
, rawvalue
);
1592 out
+=sprintf(out
, "%d", word
[0]);
1593 if (!(rawvalue
==word
[0])) {
1594 int min
=word
[1]<word
[2]?word
[1]:word
[2];
1595 int max
=word
[1]>word
[2]?word
[1]:word
[2];
1596 // The other bytes are in use. Try IBM's model
1597 out
+=sprintf(out
, " (Lifetime Min/Max %d/%d)", min
, max
);
1602 out
+=sprintf(out
, "%"PRIu64
, rawvalue
);
1605 // Return the full value
1610 // Note some attribute names appear redundant because different
1611 // manufacturers use different attribute IDs for an attribute with the
1612 // same name. The variable val should contain a non-zero value if a particular
1613 // attributes has a non-default interpretation.
1614 void ataPrintSmartAttribName(char *out
, unsigned char id
, unsigned char *definitions
){
1618 // If no data array, use default interpretations
1620 val
=definitions
[id
];
1627 name
="Raw_Read_Error_Rate";
1630 name
="Throughput_Performance";
1633 name
="Spin_Up_Time";
1636 name
="Start_Stop_Count";
1639 name
="Reallocated_Sector_Ct";
1642 name
="Read_Channel_Margin";
1645 name
="Seek_Error_Rate";
1648 name
="Seek_Time_Performance";
1653 name
="Power_On_Minutes";
1656 name
="Temperature_Celsius";
1659 name
="Power_On_Seconds";
1662 name
="Power_On_Half_Minutes";
1665 name
="Power_On_Hours";
1670 name
="Spin_Retry_Count";
1673 name
="Calibration_Retry_Count";
1676 name
="Power_Cycle_Count";
1679 name
="Read_Soft_Error_Rate";
1682 name
="G-Sense_Error_Rate";
1688 name
="Emergency_Retract_Cycle_Ct";
1691 name
="Power-Off_Retract_Count";
1696 name
="Load_Cycle_Count";
1701 // Samsung SV1204H with RK100-13 firmware
1702 name
="Temperature_Celsius_x10";
1705 // for disks with no temperature Attribute
1706 name
="Unknown_Attribute";
1709 name
="Temperature_Celsius";
1714 // Fujitsu name="ECC_On_The_Fly_Count";
1715 name
="Hardware_ECC_Recovered";
1718 name
="Reallocated_Event_Count";
1721 name
="Current_Pending_Sector";
1727 name
="Off-line_Scan_UNC_Sector_Ct";
1730 name
="Offline_Uncorrectable";
1735 name
="UDMA_CRC_Error_Count";
1740 // Fujitsu MHS2020AT
1741 name
="Write_Error_Count";
1745 name
="Multi_Zone_Error_Rate";
1753 name
="Detected_TA_Count";
1756 name
="Soft_Read_Error_Rate";
1762 name
="TA_Increase_Count";
1763 // Maxtor: Data Address Mark Errors
1767 name
="Run_Out_Cancel";
1768 // Maxtor: ECC Errors
1772 name
="Shock_Count_Write_Opern";
1773 // Maxtor: Soft ECC Correction
1777 name
="Shock_Rate_Write_Opern";
1778 // Maxtor: Thermal Aspirates
1782 name
="Flying_Height";
1786 name
="Spin_High_Current";
1794 name
="Offline_Seek_Performnce";
1799 name
="Temperature_Celsius";
1807 name
="G-Sense_Error_Rate";
1810 name
="Loaded_Hours";
1813 name
="Load_Retry_Count";
1816 name
="Load_Friction";
1819 name
="Load_Cycle_Count";
1822 name
="Load-in_Time";
1825 name
="Torq-amp_Count";
1828 name
="Power-off_Retract_Count";
1831 // seen in IBM DTPA-353750
1832 name
="Head_Amplitude";
1835 name
="Temperature_Celsius";
1838 name
="Head_Flying_Hours";
1841 name
="Read_Error_Retry_Rate";
1844 name
="Unknown_Attribute";
1847 sprintf(out
,"%3hu %s",(short int)id
,name
);
1851 // Returns raw value of Attribute with ID==id. This will be in the
1852 // range 0 to 2^48-1 inclusive. If the Attribute does not exist,
1854 int64_t ATAReturnAttributeRawValue(unsigned char id
, struct ata_smart_values
*data
) {
1857 // valid Attribute IDs are in the range 1 to 255 inclusive.
1861 // loop over Attributes to see if there is one with the desired ID
1862 for (i
=0; i
<NUMBER_ATA_SMART_ATTRIBUTES
; i
++) {
1863 struct ata_smart_attribute
*this = data
->vendor_attributes
+ i
;
1864 if (this->id
== id
) {
1865 // we've found the desired Attribute. Return its value
1869 for (j
=0; j
<6; j
++) {
1870 // This looks a bit roundabout, but is necessary. Don't
1871 // succumb to the temptation to use raw[j]<<(8*j) since under
1872 // the normal rules this will be promoted to the native type.
1873 // On a 32 bit machine this might then overflow.
1875 temp
= this->raw
[j
];
1880 } // found desired Attribute
1881 } // loop over Attributes
1883 // fall-through: no such Attribute found