]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - atacmds.cpp
Merge tag 'upstream/5.42+svn3521'
[mirror_smartmontools-debian.git] / atacmds.cpp
1 /*
2 * atacmds.cpp
3 *
4 * Home page of code is: http://smartmontools.sourceforge.net
5 *
6 * Copyright (C) 2002-11 Bruce Allen <smartmontools-support@lists.sourceforge.net>
7 * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net>
8 * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
9 * Copyright (C) 2000 Andre Hedrick <andre@linux-ide.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * This code was originally developed as a Senior Thesis by Michael Cornwell
21 * at the Concurrent Systems Laboratory (now part of the Storage Systems
22 * Research Center), Jack Baskin School of Engineering, University of
23 * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
24 *
25 */
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32
33 #include "config.h"
34 #include "int64.h"
35 #include "atacmds.h"
36 #include "utility.h"
37 #include "dev_ata_cmd_set.h" // for parsed_ata_device
38
39 const char * atacmds_cpp_cvsid = "$Id: atacmds.cpp 3507 2012-02-11 20:16:13Z chrfranke $"
40 ATACMDS_H_CVSID;
41
42 // Print ATA debug messages?
43 unsigned char ata_debugmode = 0;
44
45 // Suppress serial number?
46 // (also used in scsiprint.cpp)
47 bool dont_print_serial_number = false;
48
49
50 #define SMART_CYL_LOW 0x4F
51 #define SMART_CYL_HI 0xC2
52
53 // SMART RETURN STATUS yields SMART_CYL_HI,SMART_CYL_LOW to indicate drive
54 // is healthy and SRET_STATUS_HI_EXCEEDED,SRET_STATUS_MID_EXCEEDED to
55 // indicate that a threshhold exceeded condition has been detected.
56 // Those values (byte pairs) are placed in ATA register "LBA 23:8".
57 #define SRET_STATUS_HI_EXCEEDED 0x2C
58 #define SRET_STATUS_MID_EXCEEDED 0xF4
59
60 // These Drive Identity tables are taken from hdparm 5.2, and are also
61 // given in the ATA/ATAPI specs for the IDENTIFY DEVICE command. Note
62 // that SMART was first added into the ATA/ATAPI-3 Standard with
63 // Revision 3 of the document, July 25, 1995. Look at the "Document
64 // Status" revision commands at the beginning of
65 // http://www.t13.org/Documents/UploadedDocuments/project/d2008r7b-ATA-3.pdf
66 // to see this.
67 #define NOVAL_0 0x0000
68 #define NOVAL_1 0xffff
69 /* word 81: minor version number */
70 #define MINOR_MAX 0x22
71 static const char * const minor_str[] = { /* word 81 value: */
72 "Device does not report version", /* 0x0000 */
73 "ATA-1 X3T9.2 781D prior to revision 4", /* 0x0001 */
74 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
75 "ATA-1 X3T9.2 781D revision 4", /* 0x0003 */
76 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
77 "ATA-2 X3T10 948D prior to revision 2k", /* 0x0005 */
78 "ATA-3 X3T10 2008D revision 1", /* 0x0006 */ /* SMART NOT INCLUDED */
79 "ATA-2 X3T10 948D revision 2k", /* 0x0007 */
80 "ATA-3 X3T10 2008D revision 0", /* 0x0008 */
81 "ATA-2 X3T10 948D revision 3", /* 0x0009 */
82 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
83 "ATA-3 X3T10 2008D revision 6", /* 0x000b */ /* 1st VERSION WITH SMART */
84 "ATA-3 X3T13 2008D revision 7 and 7a", /* 0x000c */
85 "ATA/ATAPI-4 X3T13 1153D revision 6", /* 0x000d */
86 "ATA/ATAPI-4 T13 1153D revision 13", /* 0x000e */
87 "ATA/ATAPI-4 X3T13 1153D revision 7", /* 0x000f */
88 "ATA/ATAPI-4 T13 1153D revision 18", /* 0x0010 */
89 "ATA/ATAPI-4 T13 1153D revision 15", /* 0x0011 */
90 "ATA/ATAPI-4 published, ANSI NCITS 317-1998", /* 0x0012 */
91 "ATA/ATAPI-5 T13 1321D revision 3", /* 0x0013 */
92 "ATA/ATAPI-4 T13 1153D revision 14", /* 0x0014 */
93 "ATA/ATAPI-5 T13 1321D revision 1", /* 0x0015 */
94 "ATA/ATAPI-5 published, ANSI NCITS 340-2000", /* 0x0016 */
95 "ATA/ATAPI-4 T13 1153D revision 17", /* 0x0017 */
96 "ATA/ATAPI-6 T13 1410D revision 0", /* 0x0018 */
97 "ATA/ATAPI-6 T13 1410D revision 3a", /* 0x0019 */
98 "ATA/ATAPI-7 T13 1532D revision 1", /* 0x001a */
99 "ATA/ATAPI-6 T13 1410D revision 2", /* 0x001b */
100 "ATA/ATAPI-6 T13 1410D revision 1", /* 0x001c */
101 "ATA/ATAPI-7 published, ANSI INCITS 397-2005",/* 0x001d */
102 "ATA/ATAPI-7 T13 1532D revision 0", /* 0x001e */
103 "reserved", /* 0x001f */
104 "reserved", /* 0x0020 */
105 "ATA/ATAPI-7 T13 1532D revision 4a", /* 0x0021 */
106 "ATA/ATAPI-6 published, ANSI INCITS 361-2002" /* 0x0022 */
107 };
108
109 // NOTE ATA/ATAPI-4 REV 4 was the LAST revision where the device
110 // attribute structures were NOT completely vendor specific. So any
111 // disk that is ATA/ATAPI-4 or above can not be trusted to show the
112 // vendor values in sensible format.
113
114 // Negative values below are because it doesn't support SMART
115 static const int actual_ver[] = {
116 /* word 81 value: */
117 0, /* 0x0000 WARNING: */
118 1, /* 0x0001 WARNING: */
119 1, /* 0x0002 WARNING: */
120 1, /* 0x0003 WARNING: */
121 2, /* 0x0004 WARNING: This array */
122 2, /* 0x0005 WARNING: corresponds */
123 -3, /*<== */ /* 0x0006 WARNING: *exactly* */
124 2, /* 0x0007 WARNING: to the ATA/ */
125 -3, /*<== */ /* 0x0008 WARNING: ATAPI version */
126 2, /* 0x0009 WARNING: listed in */
127 3, /* 0x000a WARNING: the */
128 3, /* 0x000b WARNING: minor_str */
129 3, /* 0x000c WARNING: array */
130 4, /* 0x000d WARNING: above. */
131 4, /* 0x000e WARNING: */
132 4, /* 0x000f WARNING: If you change */
133 4, /* 0x0010 WARNING: that one, */
134 4, /* 0x0011 WARNING: change this one */
135 4, /* 0x0012 WARNING: too!!! */
136 5, /* 0x0013 WARNING: */
137 4, /* 0x0014 WARNING: */
138 5, /* 0x0015 WARNING: */
139 5, /* 0x0016 WARNING: */
140 4, /* 0x0017 WARNING: */
141 6, /* 0x0018 WARNING: */
142 6, /* 0x0019 WARNING: */
143 7, /* 0x001a WARNING: */
144 6, /* 0x001b WARNING: */
145 6, /* 0x001c WARNING: */
146 7, /* 0x001d WARNING: */
147 7, /* 0x001e WARNING: */
148 0, /* 0x001f WARNING: */
149 0, /* 0x0020 WARNING: */
150 7, /* 0x0021 WARNING: */
151 6 /* 0x0022 WARNING: */
152 };
153
154 // Compile time check of above array sizes
155 typedef char assert_sizeof_minor_str [sizeof(minor_str) /sizeof(minor_str[0]) == MINOR_MAX+1 ? 1 : -1];
156 typedef char assert_sizeof_actual_ver[sizeof(actual_ver)/sizeof(actual_ver[0]) == MINOR_MAX+1 ? 1 : -1];
157
158 // Get ID and increase flag of current pending or offline
159 // uncorrectable attribute.
160 unsigned char get_unc_attr_id(bool offline, const ata_vendor_attr_defs & defs,
161 bool & increase)
162 {
163 unsigned char id = (!offline ? 197 : 198);
164 const ata_vendor_attr_defs::entry & def = defs[id];
165 if (def.flags & ATTRFLAG_INCREASING)
166 increase = true; // '-v 19[78],increasing' option
167 else if (def.name.empty() || (id == 198 && def.name == "Offline_Scan_UNC_SectCt"))
168 increase = false; // no or '-v 198,offlinescanuncsectorct' option
169 else
170 id = 0; // other '-v 19[78],...' option
171 return id;
172 }
173
174 #if 0 // TODO: never used
175 // This are the meanings of the Self-test failure checkpoint byte.
176 // This is in the self-test log at offset 4 bytes into the self-test
177 // descriptor and in the SMART READ DATA structure at byte offset
178 // 371. These codes are not well documented. The meanings returned by
179 // this routine are used (at least) by Maxtor and IBM. Returns NULL if
180 // not recognized. Currently the maximum length is 15 bytes.
181 const char *SelfTestFailureCodeName(unsigned char which){
182
183 switch (which) {
184 case 0:
185 return "Write_Test";
186 case 1:
187 return "Servo_Basic";
188 case 2:
189 return "Servo_Random";
190 case 3:
191 return "G-list_Scan";
192 case 4:
193 return "Handling_Damage";
194 case 5:
195 return "Read_Scan";
196 default:
197 return NULL;
198 }
199 }
200 #endif
201
202
203 // Table of raw print format names
204 struct format_name_entry
205 {
206 const char * name;
207 ata_attr_raw_format format;
208 };
209
210 const format_name_entry format_names[] = {
211 {"raw8" , RAWFMT_RAW8},
212 {"raw16" , RAWFMT_RAW16},
213 {"raw48" , RAWFMT_RAW48},
214 {"hex48" , RAWFMT_HEX48},
215 {"raw64" , RAWFMT_RAW64},
216 {"hex64" , RAWFMT_HEX64},
217 {"raw16(raw16)" , RAWFMT_RAW16_OPT_RAW16},
218 {"raw16(avg16)" , RAWFMT_RAW16_OPT_AVG16},
219 {"raw24/raw24" , RAWFMT_RAW24_DIV_RAW24},
220 {"raw24/raw32" , RAWFMT_RAW24_DIV_RAW32},
221 {"sec2hour" , RAWFMT_SEC2HOUR},
222 {"min2hour" , RAWFMT_MIN2HOUR},
223 {"halfmin2hour" , RAWFMT_HALFMIN2HOUR},
224 {"msec24hour32" , RAWFMT_MSEC24_HOUR32},
225 {"tempminmax" , RAWFMT_TEMPMINMAX},
226 {"temp10x" , RAWFMT_TEMP10X},
227 };
228
229 const unsigned num_format_names = sizeof(format_names)/sizeof(format_names[0]);
230
231 // Table to map old to new '-v' option arguments
232 const char * map_old_vendor_opts[][2] = {
233 { "9,halfminutes" , "9,halfmin2hour,Power_On_Half_Minutes"},
234 { "9,minutes" , "9,min2hour,Power_On_Minutes"},
235 { "9,seconds" , "9,sec2hour,Power_On_Seconds"},
236 { "9,temp" , "9,tempminmax,Temperature_Celsius"},
237 {"192,emergencyretractcyclect" , "192,raw48,Emerg_Retract_Cycle_Ct"},
238 {"193,loadunload" , "193,raw24/raw24"},
239 {"194,10xCelsius" , "194,temp10x,Temperature_Celsius_x10"},
240 {"194,unknown" , "194,raw48,Unknown_Attribute"},
241 {"197,increasing" , "197,raw48+,Total_Pending_Sectors"}, // '+' sets flag
242 {"198,offlinescanuncsectorct" , "198,raw48,Offline_Scan_UNC_SectCt"}, // see also get_unc_attr_id() above
243 {"198,increasing" , "198,raw48+,Total_Offl_Uncorrectabl"}, // '+' sets flag
244 {"200,writeerrorcount" , "200,raw48,Write_Error_Count"},
245 {"201,detectedtacount" , "201,raw48,Detected_TA_Count"},
246 {"220,temp" , "220,raw48,Temperature_Celsius"},
247 };
248
249 const unsigned num_old_vendor_opts = sizeof(map_old_vendor_opts)/sizeof(map_old_vendor_opts[0]);
250
251 // Parse vendor attribute display def (-v option).
252 // Return false on error.
253 bool parse_attribute_def(const char * opt, ata_vendor_attr_defs & defs,
254 ata_vendor_def_prior priority)
255 {
256 // Map old -> new options
257 unsigned i;
258 for (i = 0; i < num_old_vendor_opts; i++) {
259 if (!strcmp(opt, map_old_vendor_opts[i][0])) {
260 opt = map_old_vendor_opts[i][1];
261 break;
262 }
263 }
264
265 // Parse option
266 int len = strlen(opt);
267 int id = 0, n1 = -1, n2 = -1;
268 char fmtname[32+1], attrname[32+1];
269 if (opt[0] == 'N') {
270 // "N,format"
271 if (!( sscanf(opt, "N,%32[^,]%n,%32[^,]%n", fmtname, &n1, attrname, &n2) >= 1
272 && (n1 == len || n2 == len)))
273 return false;
274 }
275 else {
276 // "id,format[+][,name]"
277 if (!( sscanf(opt, "%d,%32[^,]%n,%32[^,]%n", &id, fmtname, &n1, attrname, &n2) >= 2
278 && 1 <= id && id <= 255 && (n1 == len || n2 == len)))
279 return false;
280 }
281 if (n1 == len)
282 attrname[0] = 0;
283
284 unsigned flags = 0;
285 // For "-v 19[78],increasing" above
286 if (fmtname[strlen(fmtname)-1] == '+') {
287 fmtname[strlen(fmtname)-1] = 0;
288 flags = ATTRFLAG_INCREASING;
289 }
290
291 // Split "format[:byteorder]"
292 char byteorder[8+1] = "";
293 if (strchr(fmtname, ':')) {
294 if (!( sscanf(fmtname, "%*[^:]%n:%8[012345rvwz]%n", &n1, byteorder, &n2) >= 1
295 && n2 == (int)strlen(fmtname)))
296 return false;
297 fmtname[n1] = 0;
298 if (strchr(byteorder, 'v'))
299 flags |= (ATTRFLAG_NO_NORMVAL|ATTRFLAG_NO_WORSTVAL);
300 if (strchr(byteorder, 'w'))
301 flags |= ATTRFLAG_NO_WORSTVAL;
302 }
303
304 // Find format name
305 for (i = 0; ; i++) {
306 if (i >= num_format_names)
307 return false; // Not found
308 if (!strcmp(fmtname, format_names[i].name))
309 break;
310 }
311 ata_attr_raw_format format = format_names[i].format;
312
313 // 64-bit formats use the normalized and worst value bytes.
314 if (!*byteorder && (format == RAWFMT_RAW64 || format == RAWFMT_HEX64))
315 flags |= (ATTRFLAG_NO_NORMVAL|ATTRFLAG_NO_WORSTVAL);
316
317 if (!id) {
318 // "N,format" -> set format for all entries
319 for (i = 0; i < MAX_ATTRIBUTE_NUM; i++) {
320 if (defs[i].priority >= priority)
321 continue;
322 if (attrname[0])
323 defs[i].name = attrname;
324 defs[i].priority = priority;
325 defs[i].raw_format = format;
326 defs[i].flags = flags;
327 strcpy(defs[i].byteorder, byteorder);
328 }
329 }
330 else if (defs[id].priority <= priority) {
331 // "id,format[,name]"
332 if (attrname[0])
333 defs[id].name = attrname;
334 defs[id].raw_format = format;
335 defs[id].priority = priority;
336 defs[id].flags = flags;
337 strcpy(defs[id].byteorder, byteorder);
338 }
339
340 return true;
341 }
342
343
344 // Return a multiline string containing a list of valid arguments for
345 // parse_attribute_def(). The strings are preceeded by tabs and followed
346 // (except for the last) by newlines.
347 std::string create_vendor_attribute_arg_list()
348 {
349 std::string s;
350 unsigned i;
351 for (i = 0; i < num_format_names; i++)
352 s += strprintf("%s\tN,%s[:012345rvwz][,ATTR_NAME]",
353 (i>0 ? "\n" : ""), format_names[i].name);
354 for (i = 0; i < num_old_vendor_opts; i++)
355 s += strprintf("\n\t%s", map_old_vendor_opts[i][0]);
356 return s;
357 }
358
359 // swap two bytes. Point to low address
360 void swap2(char *location){
361 char tmp=*location;
362 *location=*(location+1);
363 *(location+1)=tmp;
364 return;
365 }
366
367 // swap four bytes. Point to low address
368 void swap4(char *location){
369 char tmp=*location;
370 *location=*(location+3);
371 *(location+3)=tmp;
372 swap2(location+1);
373 return;
374 }
375
376 // swap eight bytes. Points to low address
377 void swap8(char *location){
378 char tmp=*location;
379 *location=*(location+7);
380 *(location+7)=tmp;
381 tmp=*(location+1);
382 *(location+1)=*(location+6);
383 *(location+6)=tmp;
384 swap4(location+2);
385 return;
386 }
387
388 // Invalidate serial number and WWN and adjust checksum in IDENTIFY data
389 static void invalidate_serno(ata_identify_device * id)
390 {
391 unsigned char sum = 0;
392 unsigned i;
393 for (i = 0; i < sizeof(id->serial_no); i++) {
394 sum += id->serial_no[i]; sum -= id->serial_no[i] = 'X';
395 }
396 unsigned char * b = (unsigned char *)id;
397 for (i = 2*108; i < 2*112; i++) { // words108-111: WWN
398 sum += b[i]; sum -= b[i] = 0x00;
399 }
400
401 #ifndef __NetBSD__
402 bool must_swap = !!isbigendian();
403 if (must_swap)
404 swapx(id->words088_255+255-88);
405 #endif
406 if ((id->words088_255[255-88] & 0x00ff) == 0x00a5)
407 id->words088_255[255-88] += sum << 8;
408 #ifndef __NetBSD__
409 if (must_swap)
410 swapx(id->words088_255+255-88);
411 #endif
412 }
413
414 static const char * const commandstrings[]={
415 "SMART ENABLE",
416 "SMART DISABLE",
417 "SMART AUTOMATIC ATTRIBUTE SAVE",
418 "SMART IMMEDIATE OFFLINE",
419 "SMART AUTO OFFLINE",
420 "SMART STATUS",
421 "SMART STATUS CHECK",
422 "SMART READ ATTRIBUTE VALUES",
423 "SMART READ ATTRIBUTE THRESHOLDS",
424 "SMART READ LOG",
425 "IDENTIFY DEVICE",
426 "IDENTIFY PACKET DEVICE",
427 "CHECK POWER MODE",
428 "SMART WRITE LOG",
429 "WARNING (UNDEFINED COMMAND -- CONTACT DEVELOPERS AT " PACKAGE_BUGREPORT ")\n"
430 };
431
432
433 static const char * preg(const ata_register & r, char * buf)
434 {
435 if (!r.is_set())
436 //return "n/a ";
437 return "....";
438 sprintf(buf, "0x%02x", r.val()); return buf;
439 }
440
441 static void print_regs(const char * prefix, const ata_in_regs & r, const char * suffix = "\n")
442 {
443 char bufs[7][4+1+13];
444 pout("%s FR=%s, SC=%s, LL=%s, LM=%s, LH=%s, DEV=%s, CMD=%s%s", prefix,
445 preg(r.features, bufs[0]), preg(r.sector_count, bufs[1]), preg(r.lba_low, bufs[2]),
446 preg(r.lba_mid, bufs[3]), preg(r.lba_high, bufs[4]), preg(r.device, bufs[5]),
447 preg(r.command, bufs[6]), suffix);
448 }
449
450 static void print_regs(const char * prefix, const ata_out_regs & r, const char * suffix = "\n")
451 {
452 char bufs[7][4+1+13];
453 pout("%sERR=%s, SC=%s, LL=%s, LM=%s, LH=%s, DEV=%s, STS=%s%s", prefix,
454 preg(r.error, bufs[0]), preg(r.sector_count, bufs[1]), preg(r.lba_low, bufs[2]),
455 preg(r.lba_mid, bufs[3]), preg(r.lba_high, bufs[4]), preg(r.device, bufs[5]),
456 preg(r.status, bufs[6]), suffix);
457 }
458
459 static void prettyprint(const unsigned char *p, const char *name){
460 pout("\n===== [%s] DATA START (BASE-16) =====\n", name);
461 for (int i=0; i<512; i+=16, p+=16)
462 #define P(n) (' ' <= p[n] && p[n] <= '~' ? (int)p[n] : '.')
463 // print complete line to avoid slow tty output and extra lines in syslog.
464 pout("%03d-%03d: %02x %02x %02x %02x %02x %02x %02x %02x "
465 "%02x %02x %02x %02x %02x %02x %02x %02x"
466 " |%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c|"
467 "%c",
468 i, i+16-1,
469 p[ 0], p[ 1], p[ 2], p[ 3], p[ 4], p[ 5], p[ 6], p[ 7],
470 p[ 8], p[ 9], p[10], p[11], p[12], p[13], p[14], p[15],
471 P( 0), P( 1), P( 2), P( 3), P( 4), P( 5), P( 6), P( 7),
472 P( 8), P( 9), P(10), P(11), P(12), P(13), P(14), P(15),
473 '\n');
474 #undef P
475 pout("===== [%s] DATA END (512 Bytes) =====\n\n", name);
476 }
477
478 // This function provides the pretty-print reporting for SMART
479 // commands: it implements the various -r "reporting" options for ATA
480 // ioctls.
481 int smartcommandhandler(ata_device * device, smart_command_set command, int select, char *data){
482 // TODO: Rework old stuff below
483 // This conditional is true for commands that return data
484 int getsdata=(command==PIDENTIFY ||
485 command==IDENTIFY ||
486 command==READ_LOG ||
487 command==READ_THRESHOLDS ||
488 command==READ_VALUES ||
489 command==CHECK_POWER_MODE);
490
491 int sendsdata=(command==WRITE_LOG);
492
493 // If reporting is enabled, say what the command will be before it's executed
494 if (ata_debugmode) {
495 // conditional is true for commands that use parameters
496 int usesparam=(command==READ_LOG ||
497 command==AUTO_OFFLINE ||
498 command==AUTOSAVE ||
499 command==IMMEDIATE_OFFLINE ||
500 command==WRITE_LOG);
501
502 pout("\nREPORT-IOCTL: Device=%s Command=%s", device->get_dev_name(), commandstrings[command]);
503 if (usesparam)
504 pout(" InputParameter=%d\n", select);
505 else
506 pout("\n");
507 }
508
509 if ((getsdata || sendsdata) && !data){
510 pout("REPORT-IOCTL: Unable to execute command %s : data destination address is NULL\n", commandstrings[command]);
511 return -1;
512 }
513
514 // The reporting is cleaner, and we will find coding bugs faster, if
515 // the commands that failed clearly return empty (zeroed) data
516 // structures
517 if (getsdata) {
518 if (command==CHECK_POWER_MODE)
519 data[0]=0;
520 else
521 memset(data, '\0', 512);
522 }
523
524
525 // if requested, pretty-print the input data structure
526 if (ata_debugmode > 1 && sendsdata)
527 //pout("REPORT-IOCTL: Device=%s Command=%s\n", device->get_dev_name(), commandstrings[command]);
528 prettyprint((unsigned char *)data, commandstrings[command]);
529
530 // now execute the command
531 int retval = -1;
532 {
533 ata_cmd_in in;
534 // Set common register values
535 switch (command) {
536 default: // SMART commands
537 in.in_regs.command = ATA_SMART_CMD;
538 in.in_regs.lba_high = SMART_CYL_HI; in.in_regs.lba_mid = SMART_CYL_LOW;
539 break;
540 case IDENTIFY: case PIDENTIFY: case CHECK_POWER_MODE: // Non SMART commands
541 break;
542 }
543 // Set specific values
544 switch (command) {
545 case IDENTIFY:
546 in.in_regs.command = ATA_IDENTIFY_DEVICE;
547 in.set_data_in(data, 1);
548 break;
549 case PIDENTIFY:
550 in.in_regs.command = ATA_IDENTIFY_PACKET_DEVICE;
551 in.set_data_in(data, 1);
552 break;
553 case CHECK_POWER_MODE:
554 in.in_regs.command = ATA_CHECK_POWER_MODE;
555 in.out_needed.sector_count = true; // Powermode returned here
556 break;
557 case READ_VALUES:
558 in.in_regs.features = ATA_SMART_READ_VALUES;
559 in.set_data_in(data, 1);
560 break;
561 case READ_THRESHOLDS:
562 in.in_regs.features = ATA_SMART_READ_THRESHOLDS;
563 in.in_regs.lba_low = 1; // TODO: CORRECT ???
564 in.set_data_in(data, 1);
565 break;
566 case READ_LOG:
567 in.in_regs.features = ATA_SMART_READ_LOG_SECTOR;
568 in.in_regs.lba_low = select;
569 in.set_data_in(data, 1);
570 break;
571 case WRITE_LOG:
572 in.in_regs.features = ATA_SMART_WRITE_LOG_SECTOR;
573 in.in_regs.lba_low = select;
574 in.set_data_out(data, 1);
575 break;
576 case ENABLE:
577 in.in_regs.features = ATA_SMART_ENABLE;
578 in.in_regs.lba_low = 1; // TODO: CORRECT ???
579 break;
580 case DISABLE:
581 in.in_regs.features = ATA_SMART_DISABLE;
582 in.in_regs.lba_low = 1; // TODO: CORRECT ???
583 break;
584 case STATUS_CHECK:
585 in.out_needed.lba_high = in.out_needed.lba_mid = true; // Status returned here
586 case STATUS:
587 in.in_regs.features = ATA_SMART_STATUS;
588 break;
589 case AUTO_OFFLINE:
590 in.in_regs.features = ATA_SMART_AUTO_OFFLINE;
591 in.in_regs.sector_count = select; // Caution: Non-DATA command!
592 break;
593 case AUTOSAVE:
594 in.in_regs.features = ATA_SMART_AUTOSAVE;
595 in.in_regs.sector_count = select; // Caution: Non-DATA command!
596 break;
597 case IMMEDIATE_OFFLINE:
598 in.in_regs.features = ATA_SMART_IMMEDIATE_OFFLINE;
599 in.in_regs.lba_low = select;
600 break;
601 default:
602 pout("Unrecognized command %d in smartcommandhandler()\n"
603 "Please contact " PACKAGE_BUGREPORT "\n", command);
604 device->set_err(ENOSYS);
605 return -1;
606 }
607
608 if (ata_debugmode)
609 print_regs(" Input: ", in.in_regs,
610 (in.direction==ata_cmd_in::data_in ? " IN\n":
611 in.direction==ata_cmd_in::data_out ? " OUT\n":"\n"));
612
613 ata_cmd_out out;
614 bool ok = device->ata_pass_through(in, out);
615
616 if (ata_debugmode && out.out_regs.is_set())
617 print_regs(" Output: ", out.out_regs);
618
619 if (ok) switch (command) {
620 default:
621 retval = 0;
622 break;
623 case CHECK_POWER_MODE:
624 if (out.out_regs.sector_count.is_set()) {
625 data[0] = out.out_regs.sector_count;
626 retval = 0;
627 }
628 else {
629 pout("CHECK POWER MODE: incomplete response, ATA output registers missing\n");
630 device->set_err(ENOSYS);
631 retval = -1;
632 }
633 break;
634 case STATUS_CHECK:
635 // Cyl low and Cyl high unchanged means "Good SMART status"
636 if ((out.out_regs.lba_high == SMART_CYL_HI) &&
637 (out.out_regs.lba_mid == SMART_CYL_LOW))
638 retval = 0;
639 // These values mean "Bad SMART status"
640 else if ((out.out_regs.lba_high == SRET_STATUS_HI_EXCEEDED) &&
641 (out.out_regs.lba_mid == SRET_STATUS_MID_EXCEEDED))
642 retval = 1;
643 else if (out.out_regs.lba_mid == SMART_CYL_LOW) {
644 retval = 0;
645 if (ata_debugmode)
646 pout("SMART STATUS RETURN: half healthy response sequence, "
647 "probable SAT/USB truncation\n");
648 } else if (out.out_regs.lba_mid == SRET_STATUS_MID_EXCEEDED) {
649 retval = 1;
650 if (ata_debugmode)
651 pout("SMART STATUS RETURN: half unhealthy response sequence, "
652 "probable SAT/USB truncation\n");
653 }
654 else if (!out.out_regs.is_set()) {
655 pout("SMART STATUS RETURN: incomplete response, ATA output registers missing\n");
656 device->set_err(ENOSYS);
657 retval = -1;
658 }
659 else {
660 // We haven't gotten output that makes sense; print out some debugging info
661 pout("Error SMART Status command failed\n");
662 pout("Please get assistance from %s\n", PACKAGE_HOMEPAGE);
663 pout("Register values returned from SMART Status command are:\n");
664 print_regs(" ", out.out_regs);
665 device->set_err(EIO);
666 retval = -1;
667 }
668 break;
669 }
670 }
671
672 // If requested, invalidate serial number before any printing is done
673 if ((command == IDENTIFY || command == PIDENTIFY) && !retval && dont_print_serial_number)
674 invalidate_serno((ata_identify_device *)data);
675
676 // If reporting is enabled, say what output was produced by the command
677 if (ata_debugmode) {
678 if (device->get_errno())
679 pout("REPORT-IOCTL: Device=%s Command=%s returned %d errno=%d [%s]\n",
680 device->get_dev_name(), commandstrings[command], retval,
681 device->get_errno(), device->get_errmsg());
682 else
683 pout("REPORT-IOCTL: Device=%s Command=%s returned %d\n",
684 device->get_dev_name(), commandstrings[command], retval);
685
686 // if requested, pretty-print the output data structure
687 if (ata_debugmode > 1 && getsdata) {
688 if (command==CHECK_POWER_MODE)
689 pout("Sector Count Register (BASE-16): %02x\n", (unsigned char)(*data));
690 else
691 prettyprint((unsigned char *)data, commandstrings[command]);
692 }
693 }
694
695 return retval;
696 }
697
698 // Get capacity and sector sizes from IDENTIFY data
699 void ata_get_size_info(const ata_identify_device * id, ata_size_info & sizes)
700 {
701 sizes.sectors = sizes.capacity = 0;
702 sizes.log_sector_size = sizes.phy_sector_size = 0;
703 sizes.log_sector_offset = 0;
704
705 // Return if no LBA support
706 if (!(id->words047_079[49-47] & 0x0200))
707 return;
708
709 // Determine 28-bit LBA capacity
710 unsigned lba28 = (unsigned)id->words047_079[61-47] << 16
711 | (unsigned)id->words047_079[60-47] ;
712
713 // Determine 48-bit LBA capacity if supported
714 uint64_t lba48 = 0;
715 if ((id->command_set_2 & 0xc400) == 0x4400)
716 lba48 = (uint64_t)id->words088_255[103-88] << 48
717 | (uint64_t)id->words088_255[102-88] << 32
718 | (uint64_t)id->words088_255[101-88] << 16
719 | (uint64_t)id->words088_255[100-88] ;
720
721 // Return if capacity unknown (ATAPI CD/DVD)
722 if (!(lba28 || lba48))
723 return;
724
725 // Determine sector sizes
726 sizes.log_sector_size = sizes.phy_sector_size = 512;
727
728 unsigned short word106 = id->words088_255[106-88];
729 if ((word106 & 0xc000) == 0x4000) {
730 // Long Logical/Physical Sectors (LLS/LPS) ?
731 if (word106 & 0x1000)
732 // Logical sector size is specified in 16-bit words
733 sizes.log_sector_size = sizes.phy_sector_size =
734 ((id->words088_255[118-88] << 16) | id->words088_255[117-88]) << 1;
735
736 if (word106 & 0x2000)
737 // Physical sector size is multiple of logical sector size
738 sizes.phy_sector_size <<= (word106 & 0x0f);
739
740 unsigned short word209 = id->words088_255[209-88];
741 if ((word209 & 0xc000) == 0x4000)
742 sizes.log_sector_offset = (word209 & 0x3fff) * sizes.log_sector_size;
743 }
744
745 // Some early 4KiB LLS disks (Samsung N3U-3) return bogus lba28 value
746 if (lba48 >= lba28 || (lba48 && sizes.log_sector_size > 512))
747 sizes.sectors = lba48;
748 else
749 sizes.sectors = lba28;
750
751 sizes.capacity = sizes.sectors * sizes.log_sector_size;
752 }
753
754 // This function computes the checksum of a single disk sector (512
755 // bytes). Returns zero if checksum is OK, nonzero if the checksum is
756 // incorrect. The size (512) is correct for all SMART structures.
757 unsigned char checksum(const void * data)
758 {
759 unsigned char sum = 0;
760 for (int i = 0; i < 512; i++)
761 sum += ((const unsigned char *)data)[i];
762 return sum;
763 }
764
765 // Copies n bytes (or n-1 if n is odd) from in to out, but swaps adjacents
766 // bytes.
767 static void swapbytes(char * out, const char * in, size_t n)
768 {
769 for (size_t i = 0; i < n; i += 2) {
770 out[i] = in[i+1];
771 out[i+1] = in[i];
772 }
773 }
774
775 // Copies in to out, but removes leading and trailing whitespace.
776 static void trim(char * out, const char * in)
777 {
778 // Find the first non-space character (maybe none).
779 int first = -1;
780 int i;
781 for (i = 0; in[i]; i++)
782 if (!isspace((int)in[i])) {
783 first = i;
784 break;
785 }
786
787 if (first == -1) {
788 // There are no non-space characters.
789 out[0] = '\0';
790 return;
791 }
792
793 // Find the last non-space character.
794 for (i = strlen(in)-1; i >= first && isspace((int)in[i]); i--)
795 ;
796 int last = i;
797
798 strncpy(out, in+first, last-first+1);
799 out[last-first+1] = '\0';
800 }
801
802 // Convenience function for formatting strings from ata_identify_device
803 void ata_format_id_string(char * out, const unsigned char * in, int n)
804 {
805 bool must_swap = true;
806 #ifdef __NetBSD__
807 /* NetBSD kernel delivers IDENTIFY data in host byte order (but all else is LE) */
808 // TODO: Handle NetBSD case in os_netbsd.cpp
809 if (isbigendian())
810 must_swap = !must_swap;
811 #endif
812
813 char tmp[65];
814 n = n > 64 ? 64 : n;
815 if (!must_swap)
816 strncpy(tmp, (const char *)in, n);
817 else
818 swapbytes(tmp, (const char *)in, n);
819 tmp[n] = '\0';
820 trim(out, tmp);
821 }
822
823 // returns -1 if command fails or the device is in Sleep mode, else
824 // value of Sector Count register. Sector Count result values:
825 // 00h device is in Standby mode.
826 // 80h device is in Idle mode.
827 // FFh device is in Active mode or Idle mode.
828
829 int ataCheckPowerMode(ata_device * device) {
830 unsigned char result;
831
832 if ((smartcommandhandler(device, CHECK_POWER_MODE, 0, (char *)&result)))
833 return -1;
834
835 if (result!=0 && result!=0x80 && result!=0xff)
836 pout("ataCheckPowerMode(): ATA CHECK POWER MODE returned unknown Sector Count Register value %02x\n", result);
837
838 return (int)result;
839 }
840
841 // Issue a no-data ATA command with optional sector count register value
842 bool ata_nodata_command(ata_device * device, unsigned char command,
843 int sector_count /* = -1 */)
844 {
845 ata_cmd_in in;
846 in.in_regs.command = command;
847 if (sector_count >= 0)
848 in.in_regs.sector_count = sector_count;
849
850 return device->ata_pass_through(in);
851 }
852
853 // Issue SET FEATURES command with optional sector count register value
854 bool ata_set_features(ata_device * device, unsigned char features,
855 int sector_count /* = -1 */)
856 {
857 ata_cmd_in in;
858 in.in_regs.command = ATA_SET_FEATURES;
859 in.in_regs.features = features;
860 if (sector_count >= 0)
861 in.in_regs.sector_count = sector_count;
862
863 return device->ata_pass_through(in);
864 }
865
866 // Reads current Device Identity info (512 bytes) into buf. Returns 0
867 // if all OK. Returns -1 if no ATA Device identity can be
868 // established. Returns >0 if Device is ATA Packet Device (not SMART
869 // capable). The value of the integer helps identify the type of
870 // Packet device, which is useful so that the user can connect the
871 // formal device number with whatever object is inside their computer.
872 int ata_read_identity(ata_device * device, ata_identify_device * buf, bool fix_swapped_id)
873 {
874 unsigned short *rawshort=(unsigned short *)buf;
875 unsigned char *rawbyte =(unsigned char *)buf;
876
877 // See if device responds either to IDENTIFY DEVICE or IDENTIFY
878 // PACKET DEVICE
879 bool packet = false;
880 if ((smartcommandhandler(device, IDENTIFY, 0, (char *)buf))){
881 if (smartcommandhandler(device, PIDENTIFY, 0, (char *)buf)){
882 return -1;
883 }
884 packet = true;
885 }
886
887 unsigned i;
888 if (fix_swapped_id) {
889 // Swap ID strings
890 for (i = 0; i < sizeof(buf->serial_no)-1; i += 2)
891 swap2((char *)(buf->serial_no+i));
892 for (i = 0; i < sizeof(buf->fw_rev)-1; i += 2)
893 swap2((char *)(buf->fw_rev+i));
894 for (i = 0; i < sizeof(buf->model)-1; i += 2)
895 swap2((char *)(buf->model+i));
896 }
897
898 #ifndef __NetBSD__
899 // if machine is big-endian, swap byte order as needed
900 // NetBSD kernel delivers IDENTIFY data in host byte order
901 // TODO: Handle NetBSD case in os_netbsd.cpp
902 if (isbigendian()){
903
904 // swap various capability words that are needed
905 for (i=0; i<33; i++)
906 swap2((char *)(buf->words047_079+i));
907
908 for (i=80; i<=87; i++)
909 swap2((char *)(rawshort+i));
910
911 for (i=0; i<168; i++)
912 swap2((char *)(buf->words088_255+i));
913 }
914 #endif
915
916 // If there is a checksum there, validate it
917 if ((rawshort[255] & 0x00ff) == 0x00a5 && checksum(rawbyte))
918 checksumwarning("Drive Identity Structure");
919
920 // AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS)
921 // T13/1699-D Revision 6a (Final Draft), September 6, 2008.
922 // Sections 7.16.7 and 7.17.6:
923 //
924 // Word 0 of IDENTIFY DEVICE data:
925 // Bit 15 = 0 : ATA device
926 //
927 // Word 0 of IDENTIFY PACKET DEVICE data:
928 // Bits 15:14 = 10b : ATAPI device
929 // Bits 15:14 = 11b : Reserved
930 // Bits 12:8 : Device type (SPC-4, e.g 0x05 = CD/DVD)
931
932 // CF+ and CompactFlash Specification Revision 4.0, May 24, 2006.
933 // Section 6.2.1.6:
934 //
935 // Word 0 of IDENTIFY DEVICE data:
936 // 848Ah = Signature for CompactFlash Storage Card
937 // 044Ah = Alternate value turns on ATA device while preserving all retired bits
938 // 0040h = Alternate value turns on ATA device while zeroing all retired bits
939
940 // Assume ATA if IDENTIFY DEVICE returns CompactFlash Signature
941 if (!packet && rawbyte[1] == 0x84 && rawbyte[0] == 0x8a)
942 return 0;
943
944 // If this is a PACKET DEVICE, return device type
945 if (rawbyte[1] & 0x80)
946 return 1+(rawbyte[1] & 0x1f);
947
948 // Not a PACKET DEVICE
949 return 0;
950 }
951
952 // Returns ATA version as an integer, and a pointer to a string
953 // describing which revision. Note that Revision 0 of ATA-3 does NOT
954 // support SMART. For this one case we return -3 rather than +3 as
955 // the version number. See notes above.
956 int ataVersionInfo(const char ** description, const ata_identify_device * drive, unsigned short * minor)
957 {
958 // get major and minor ATA revision numbers
959 unsigned short major = drive->major_rev_num;
960 *minor=drive->minor_rev_num;
961
962 // First check if device has ANY ATA version information in it
963 if (major==NOVAL_0 || major==NOVAL_1) {
964 *description=NULL;
965 return 0; // No info found
966 }
967
968 // The minor revision number has more information - try there first
969 if (*minor && (*minor<=MINOR_MAX)){
970 int std = actual_ver[*minor];
971 if (std) {
972 *description=minor_str[*minor];
973 return std;
974 }
975 }
976
977 // Try new ATA-8 ACS minor revision numbers.
978 // Table 55 of T13/2015-D Revision 4a (ACS-2), December 9, 2010.
979 // (not in actual_ver/minor_str to avoid large sparse tables)
980 const char *desc;
981 switch (*minor) {
982 case 0x0027: desc = "ATA-8-ACS revision 3c"; break;
983 case 0x0028: desc = "ATA-8-ACS revision 6"; break;
984 case 0x0029: desc = "ATA-8-ACS revision 4"; break;
985 case 0x0031: desc = "ACS-2 revision 2"; break;
986 case 0x0033: desc = "ATA-8-ACS revision 3e"; break;
987 case 0x0039: desc = "ATA-8-ACS revision 4c"; break;
988 case 0x0042: desc = "ATA-8-ACS revision 3f"; break;
989 case 0x0052: desc = "ATA-8-ACS revision 3b"; break;
990 case 0x0107: desc = "ATA-8-ACS revision 2d"; break;
991 case 0x0110: desc = "ACS-2 revision 3"; break;
992 default: desc = 0; break;
993 }
994 if (desc) {
995 *description = desc;
996 return 8;
997 }
998
999 // HDPARM has a very complicated algorithm from here on. Since SMART only
1000 // exists on ATA-3 and later standards, let's punt on this. If you don't
1001 // like it, please fix it. The code's in CVS.
1002 int i;
1003 for (i=15; i>0; i--)
1004 if (major & (0x1<<i))
1005 break;
1006
1007 *description=NULL;
1008 if (i==0)
1009 return 1;
1010 else
1011 return i;
1012 }
1013
1014 // Get World Wide Name (WWN) fields.
1015 // Return NAA field or -1 if WWN is unsupported.
1016 // Table 34 of T13/1699-D Revision 6a (ATA8-ACS), September 6, 2008.
1017 // (WWN was introduced in ATA/ATAPI-7 and is mandatory since ATA8-ACS Revision 3b)
1018 int ata_get_wwn(const ata_identify_device * id, unsigned & oui, uint64_t & unique_id)
1019 {
1020 // Don't use word 84 to be compatible with some older ATA-7 disks
1021 unsigned short word087 = id->csf_default;
1022 if ((word087 & 0xc100) != 0x4100)
1023 return -1; // word not valid or WWN support bit 8 not set
1024
1025 unsigned short word108 = id->words088_255[108-88];
1026 unsigned short word109 = id->words088_255[109-88];
1027 unsigned short word110 = id->words088_255[110-88];
1028 unsigned short word111 = id->words088_255[111-88];
1029
1030 oui = ((word108 & 0x0fff) << 12) | (word109 >> 4);
1031 unique_id = ((uint64_t)(word109 & 0xf) << 32)
1032 | (unsigned)((word110 << 16) | word111);
1033 return (word108 >> 12);
1034 }
1035
1036 // returns 1 if SMART supported, 0 if SMART unsupported, -1 if can't tell
1037 int ataSmartSupport(const ata_identify_device * drive)
1038 {
1039 unsigned short word82=drive->command_set_1;
1040 unsigned short word83=drive->command_set_2;
1041
1042 // check if words 82/83 contain valid info
1043 if ((word83>>14) == 0x01)
1044 // return value of SMART support bit
1045 return word82 & 0x0001;
1046
1047 // since we can're rely on word 82, we don't know if SMART supported
1048 return -1;
1049 }
1050
1051 // returns 1 if SMART enabled, 0 if SMART disabled, -1 if can't tell
1052 int ataIsSmartEnabled(const ata_identify_device * drive)
1053 {
1054 unsigned short word85=drive->cfs_enable_1;
1055 unsigned short word87=drive->csf_default;
1056
1057 // check if words 85/86/87 contain valid info
1058 if ((word87>>14) == 0x01)
1059 // return value of SMART enabled bit
1060 return word85 & 0x0001;
1061
1062 // Since we can't rely word85, we don't know if SMART is enabled.
1063 return -1;
1064 }
1065
1066
1067 // Reads SMART attributes into *data
1068 int ataReadSmartValues(ata_device * device, struct ata_smart_values *data){
1069
1070 if (smartcommandhandler(device, READ_VALUES, 0, (char *)data)){
1071 pout("Error SMART Values Read failed: %s\n", device->get_errmsg());
1072 return -1;
1073 }
1074
1075 // compute checksum
1076 if (checksum(data))
1077 checksumwarning("SMART Attribute Data Structure");
1078
1079 // swap endian order if needed
1080 if (isbigendian()){
1081 int i;
1082 swap2((char *)&(data->revnumber));
1083 swap2((char *)&(data->total_time_to_complete_off_line));
1084 swap2((char *)&(data->smart_capability));
1085 swapx(&data->extend_test_completion_time_w);
1086 for (i=0; i<NUMBER_ATA_SMART_ATTRIBUTES; i++){
1087 struct ata_smart_attribute *x=data->vendor_attributes+i;
1088 swap2((char *)&(x->flags));
1089 }
1090 }
1091
1092 return 0;
1093 }
1094
1095
1096 // This corrects some quantities that are byte reversed in the SMART
1097 // SELF TEST LOG
1098 static void fixsamsungselftestlog(ata_smart_selftestlog * data)
1099 {
1100 // bytes 508/509 (numbered from 0) swapped (swap of self-test index
1101 // with one byte of reserved.
1102 swap2((char *)&(data->mostrecenttest));
1103
1104 // LBA low register (here called 'selftestnumber", containing
1105 // information about the TYPE of the self-test) is byte swapped with
1106 // Self-test execution status byte. These are bytes N, N+1 in the
1107 // entries.
1108 for (int i = 0; i < 21; i++)
1109 swap2((char *)&(data->selftest_struct[i].selftestnumber));
1110
1111 return;
1112 }
1113
1114 // Reads the Self Test Log (log #6)
1115 int ataReadSelfTestLog (ata_device * device, ata_smart_selftestlog * data,
1116 unsigned char fix_firmwarebug)
1117 {
1118
1119 // get data from device
1120 if (smartcommandhandler(device, READ_LOG, 0x06, (char *)data)){
1121 pout("Error SMART Error Self-Test Log Read failed: %s\n", device->get_errmsg());
1122 return -1;
1123 }
1124
1125 // compute its checksum, and issue a warning if needed
1126 if (checksum(data))
1127 checksumwarning("SMART Self-Test Log Structure");
1128
1129 // fix firmware bugs in self-test log
1130 if (fix_firmwarebug == FIX_SAMSUNG)
1131 fixsamsungselftestlog(data);
1132
1133 // swap endian order if needed
1134 if (isbigendian()){
1135 int i;
1136 swap2((char*)&(data->revnumber));
1137 for (i=0; i<21; i++){
1138 struct ata_smart_selftestlog_struct *x=data->selftest_struct+i;
1139 swap2((char *)&(x->timestamp));
1140 swap4((char *)&(x->lbafirstfailure));
1141 }
1142 }
1143
1144 return 0;
1145 }
1146
1147 // Print checksum warning for multi sector log
1148 static void check_multi_sector_sum(const void * data, unsigned nsectors, const char * msg)
1149 {
1150 unsigned errs = 0;
1151 for (unsigned i = 0; i < nsectors; i++) {
1152 if (checksum((const unsigned char *)data + i*512))
1153 errs++;
1154 }
1155 if (errs > 0) {
1156 if (nsectors == 1)
1157 checksumwarning(msg);
1158 else
1159 checksumwarning(strprintf("%s (%u/%u)", msg, errs, nsectors).c_str());
1160 }
1161 }
1162
1163 // Read SMART Extended Self-test Log
1164 bool ataReadExtSelfTestLog(ata_device * device, ata_smart_extselftestlog * log,
1165 unsigned nsectors)
1166 {
1167 if (!ataReadLogExt(device, 0x07, 0x00, 0, log, nsectors))
1168 return false;
1169
1170 check_multi_sector_sum(log, nsectors, "SMART Extended Self-test Log Structure");
1171
1172 if (isbigendian()) {
1173 swapx(&log->log_desc_index);
1174 for (unsigned i = 0; i < nsectors; i++) {
1175 for (unsigned j = 0; j < 19; j++)
1176 swapx(&log->log_descs[i].timestamp);
1177 }
1178 }
1179 return true;
1180 }
1181
1182
1183 // Read GP Log page(s)
1184 bool ataReadLogExt(ata_device * device, unsigned char logaddr,
1185 unsigned char features, unsigned page,
1186 void * data, unsigned nsectors)
1187 {
1188 ata_cmd_in in;
1189 in.in_regs.command = ATA_READ_LOG_EXT;
1190 in.in_regs.features = features; // log specific
1191 in.set_data_in_48bit(data, nsectors);
1192 in.in_regs.lba_low = logaddr;
1193 in.in_regs.lba_mid_16 = page;
1194
1195 if (!device->ata_pass_through(in)) { // TODO: Debug output
1196 if (nsectors <= 1) {
1197 pout("ATA_READ_LOG_EXT (addr=0x%02x:0x%02x, page=%u, n=%u) failed: %s\n",
1198 logaddr, features, page, nsectors, device->get_errmsg());
1199 return false;
1200 }
1201
1202 // Recurse to retry with single sectors,
1203 // multi-sector reads may not be supported by ioctl.
1204 for (unsigned i = 0; i < nsectors; i++) {
1205 if (!ataReadLogExt(device, logaddr,
1206 features, page + i,
1207 (char *)data + 512*i, 1))
1208 return false;
1209 }
1210 }
1211
1212 return true;
1213 }
1214
1215 // Read SMART Log page(s)
1216 bool ataReadSmartLog(ata_device * device, unsigned char logaddr,
1217 void * data, unsigned nsectors)
1218 {
1219 ata_cmd_in in;
1220 in.in_regs.command = ATA_SMART_CMD;
1221 in.in_regs.features = ATA_SMART_READ_LOG_SECTOR;
1222 in.set_data_in(data, nsectors);
1223 in.in_regs.lba_high = SMART_CYL_HI;
1224 in.in_regs.lba_mid = SMART_CYL_LOW;
1225 in.in_regs.lba_low = logaddr;
1226
1227 if (!device->ata_pass_through(in)) { // TODO: Debug output
1228 pout("ATA_SMART_READ_LOG failed: %s\n", device->get_errmsg());
1229 return false;
1230 }
1231 return true;
1232 }
1233
1234
1235
1236 // Reads the SMART or GPL Log Directory (log #0)
1237 int ataReadLogDirectory(ata_device * device, ata_smart_log_directory * data, bool gpl)
1238 {
1239 if (!gpl) { // SMART Log directory
1240 if (smartcommandhandler(device, READ_LOG, 0x00, (char *)data))
1241 return -1;
1242 }
1243 else { // GP Log directory
1244 if (!ataReadLogExt(device, 0x00, 0x00, 0, data, 1))
1245 return -1;
1246 }
1247
1248 // swap endian order if needed
1249 if (isbigendian())
1250 swapx(&data->logversion);
1251
1252 return 0;
1253 }
1254
1255
1256 // Reads the selective self-test log (log #9)
1257 int ataReadSelectiveSelfTestLog(ata_device * device, struct ata_selective_self_test_log *data){
1258
1259 // get data from device
1260 if (smartcommandhandler(device, READ_LOG, 0x09, (char *)data)){
1261 pout("Error SMART Read Selective Self-Test Log failed: %s\n", device->get_errmsg());
1262 return -1;
1263 }
1264
1265 // compute its checksum, and issue a warning if needed
1266 if (checksum(data))
1267 checksumwarning("SMART Selective Self-Test Log Structure");
1268
1269 // swap endian order if needed
1270 if (isbigendian()){
1271 int i;
1272 swap2((char *)&(data->logversion));
1273 for (i=0;i<5;i++){
1274 swap8((char *)&(data->span[i].start));
1275 swap8((char *)&(data->span[i].end));
1276 }
1277 swap8((char *)&(data->currentlba));
1278 swap2((char *)&(data->currentspan));
1279 swap2((char *)&(data->flags));
1280 swap2((char *)&(data->pendingtime));
1281 }
1282
1283 if (data->logversion != 1)
1284 pout("Note: selective self-test log revision number (%d) not 1 implies that no selective self-test has ever been run\n", data->logversion);
1285
1286 return 0;
1287 }
1288
1289 // Writes the selective self-test log (log #9)
1290 int ataWriteSelectiveSelfTestLog(ata_device * device, ata_selective_selftest_args & args,
1291 const ata_smart_values * sv, uint64_t num_sectors,
1292 const ata_selective_selftest_args * prev_args)
1293 {
1294 // Disk size must be known
1295 if (!num_sectors) {
1296 pout("Disk size is unknown, unable to check selective self-test spans\n");
1297 return -1;
1298 }
1299
1300 // Read log
1301 struct ata_selective_self_test_log sstlog, *data=&sstlog;
1302 unsigned char *ptr=(unsigned char *)data;
1303 if (ataReadSelectiveSelfTestLog(device, data)) {
1304 pout("Since Read failed, will not attempt to WRITE Selective Self-test Log\n");
1305 return -1;
1306 }
1307
1308 // Set log version
1309 data->logversion = 1;
1310
1311 // Host is NOT allowed to write selective self-test log if a selective
1312 // self-test is in progress.
1313 if (0<data->currentspan && data->currentspan<6 && ((sv->self_test_exec_status)>>4)==15) {
1314 pout("Error SMART Selective or other Self-Test in progress.\n");
1315 return -4;
1316 }
1317
1318 // Set start/end values based on old spans for special -t select,... options
1319 int i;
1320 for (i = 0; i < args.num_spans; i++) {
1321 int mode = args.span[i].mode;
1322 uint64_t start = args.span[i].start;
1323 uint64_t end = args.span[i].end;
1324 if (mode == SEL_CONT) {// redo or next dependig on last test status
1325 switch (sv->self_test_exec_status >> 4) {
1326 case 1: case 2: // Aborted/Interrupted by host
1327 pout("Continue Selective Self-Test: Redo last span\n");
1328 mode = SEL_REDO;
1329 break;
1330 default: // All others
1331 pout("Continue Selective Self-Test: Start next span\n");
1332 mode = SEL_NEXT;
1333 break;
1334 }
1335 }
1336
1337 if ( (mode == SEL_REDO || mode == SEL_NEXT)
1338 && prev_args && i < prev_args->num_spans
1339 && !data->span[i].start && !data->span[i].end) {
1340 // Some drives do not preserve the selective self-test log accross
1341 // power-cyles. If old span on drive is cleared use span provided
1342 // by caller. This is used by smartd (first span only).
1343 data->span[i].start = prev_args->span[i].start;
1344 data->span[i].end = prev_args->span[i].end;
1345 }
1346
1347 switch (mode) {
1348 case SEL_RANGE: // -t select,START-END
1349 break;
1350 case SEL_REDO: // -t select,redo... => Redo current
1351 start = data->span[i].start;
1352 if (end > 0) { // -t select,redo+SIZE
1353 end--; end += start; // [oldstart, oldstart+SIZE)
1354 }
1355 else // -t select,redo
1356 end = data->span[i].end; // [oldstart, oldend]
1357 break;
1358 case SEL_NEXT: // -t select,next... => Do next
1359 if (data->span[i].end == 0) {
1360 start = end = 0; break; // skip empty spans
1361 }
1362 start = data->span[i].end + 1;
1363 if (start >= num_sectors)
1364 start = 0; // wrap around
1365 if (end > 0) { // -t select,next+SIZE
1366 end--; end += start; // (oldend, oldend+SIZE]
1367 }
1368 else { // -t select,next
1369 uint64_t oldsize = data->span[i].end - data->span[i].start + 1;
1370 end = start + oldsize - 1; // (oldend, oldend+oldsize]
1371 if (end >= num_sectors) {
1372 // Adjust size to allow round-robin testing without future size decrease
1373 uint64_t spans = (num_sectors + oldsize-1) / oldsize;
1374 uint64_t newsize = (num_sectors + spans-1) / spans;
1375 uint64_t newstart = num_sectors - newsize, newend = num_sectors - 1;
1376 pout("Span %d changed from %"PRIu64"-%"PRIu64" (%"PRIu64" sectors)\n",
1377 i, start, end, oldsize);
1378 pout(" to %"PRIu64"-%"PRIu64" (%"PRIu64" sectors) (%"PRIu64" spans)\n",
1379 newstart, newend, newsize, spans);
1380 start = newstart; end = newend;
1381 }
1382 }
1383 break;
1384 default:
1385 pout("ataWriteSelectiveSelfTestLog: Invalid mode %d\n", mode);
1386 return -1;
1387 }
1388 // Range check
1389 if (start < num_sectors && num_sectors <= end) {
1390 if (end != ~(uint64_t)0) // -t select,N-max
1391 pout("Size of self-test span %d decreased according to disk size\n", i);
1392 end = num_sectors - 1;
1393 }
1394 if (!(start <= end && end < num_sectors)) {
1395 pout("Invalid selective self-test span %d: %"PRIu64"-%"PRIu64" (%"PRIu64" sectors)\n",
1396 i, start, end, num_sectors);
1397 return -1;
1398 }
1399 // Return the actual mode and range to caller.
1400 args.span[i].mode = mode;
1401 args.span[i].start = start;
1402 args.span[i].end = end;
1403 }
1404
1405 // Clear spans
1406 for (i=0; i<5; i++)
1407 memset(data->span+i, 0, sizeof(struct test_span));
1408
1409 // Set spans for testing
1410 for (i = 0; i < args.num_spans; i++){
1411 data->span[i].start = args.span[i].start;
1412 data->span[i].end = args.span[i].end;
1413 }
1414
1415 // host must initialize to zero before initiating selective self-test
1416 data->currentlba=0;
1417 data->currentspan=0;
1418
1419 // Perform off-line scan after selective test?
1420 if (args.scan_after_select == 1)
1421 // NO
1422 data->flags &= ~SELECTIVE_FLAG_DOSCAN;
1423 else if (args.scan_after_select == 2)
1424 // YES
1425 data->flags |= SELECTIVE_FLAG_DOSCAN;
1426
1427 // Must clear active and pending flags before writing
1428 data->flags &= ~(SELECTIVE_FLAG_ACTIVE);
1429 data->flags &= ~(SELECTIVE_FLAG_PENDING);
1430
1431 // modify pending time?
1432 if (args.pending_time)
1433 data->pendingtime = (unsigned short)(args.pending_time-1);
1434
1435 // Set checksum to zero, then compute checksum
1436 data->checksum=0;
1437 unsigned char cksum=0;
1438 for (i=0; i<512; i++)
1439 cksum+=ptr[i];
1440 cksum=~cksum;
1441 cksum+=1;
1442 data->checksum=cksum;
1443
1444 // swap endian order if needed
1445 if (isbigendian()){
1446 swap2((char *)&(data->logversion));
1447 for (int b = 0; b < 5; b++) {
1448 swap8((char *)&(data->span[b].start));
1449 swap8((char *)&(data->span[b].end));
1450 }
1451 swap8((char *)&(data->currentlba));
1452 swap2((char *)&(data->currentspan));
1453 swap2((char *)&(data->flags));
1454 swap2((char *)&(data->pendingtime));
1455 }
1456
1457 // write new selective self-test log
1458 if (smartcommandhandler(device, WRITE_LOG, 0x09, (char *)data)){
1459 pout("Error Write Selective Self-Test Log failed: %s\n", device->get_errmsg());
1460 return -3;
1461 }
1462
1463 return 0;
1464 }
1465
1466 // This corrects some quantities that are byte reversed in the SMART
1467 // ATA ERROR LOG.
1468 static void fixsamsungerrorlog(ata_smart_errorlog * data)
1469 {
1470 // FIXED IN SAMSUNG -25 FIRMWARE???
1471 // Device error count in bytes 452-3
1472 swap2((char *)&(data->ata_error_count));
1473
1474 // FIXED IN SAMSUNG -22a FIRMWARE
1475 // step through 5 error log data structures
1476 for (int i = 0; i < 5; i++){
1477 // step through 5 command data structures
1478 for (int j = 0; j < 5; j++)
1479 // Command data structure 4-byte millisec timestamp. These are
1480 // bytes (N+8, N+9, N+10, N+11).
1481 swap4((char *)&(data->errorlog_struct[i].commands[j].timestamp));
1482 // Error data structure two-byte hour life timestamp. These are
1483 // bytes (N+28, N+29).
1484 swap2((char *)&(data->errorlog_struct[i].error_struct.timestamp));
1485 }
1486 return;
1487 }
1488
1489 // NEEDED ONLY FOR SAMSUNG -22 (some) -23 AND -24?? FIRMWARE
1490 static void fixsamsungerrorlog2(ata_smart_errorlog * data)
1491 {
1492 // Device error count in bytes 452-3
1493 swap2((char *)&(data->ata_error_count));
1494 return;
1495 }
1496
1497 // Reads the Summary SMART Error Log (log #1). The Comprehensive SMART
1498 // Error Log is #2, and the Extended Comprehensive SMART Error log is
1499 // #3
1500 int ataReadErrorLog (ata_device * device, ata_smart_errorlog *data,
1501 unsigned char fix_firmwarebug)
1502 {
1503
1504 // get data from device
1505 if (smartcommandhandler(device, READ_LOG, 0x01, (char *)data)){
1506 pout("Error SMART Error Log Read failed: %s\n", device->get_errmsg());
1507 return -1;
1508 }
1509
1510 // compute its checksum, and issue a warning if needed
1511 if (checksum(data))
1512 checksumwarning("SMART ATA Error Log Structure");
1513
1514 // Some disks have the byte order reversed in some SMART Summary
1515 // Error log entries
1516 if (fix_firmwarebug == FIX_SAMSUNG)
1517 fixsamsungerrorlog(data);
1518 else if (fix_firmwarebug == FIX_SAMSUNG2)
1519 fixsamsungerrorlog2(data);
1520
1521 // swap endian order if needed
1522 if (isbigendian()){
1523 int i,j;
1524
1525 // Device error count in bytes 452-3
1526 swap2((char *)&(data->ata_error_count));
1527
1528 // step through 5 error log data structures
1529 for (i=0; i<5; i++){
1530 // step through 5 command data structures
1531 for (j=0; j<5; j++)
1532 // Command data structure 4-byte millisec timestamp
1533 swap4((char *)&(data->errorlog_struct[i].commands[j].timestamp));
1534 // Error data structure life timestamp
1535 swap2((char *)&(data->errorlog_struct[i].error_struct.timestamp));
1536 }
1537 }
1538
1539 return 0;
1540 }
1541
1542 // Read Extended Comprehensive Error Log
1543 bool ataReadExtErrorLog(ata_device * device, ata_smart_exterrlog * log,
1544 unsigned nsectors)
1545 {
1546 if (!ataReadLogExt(device, 0x03, 0x00, 0, log, nsectors))
1547 return false;
1548
1549 check_multi_sector_sum(log, nsectors, "SMART Extended Comprehensive Error Log Structure");
1550
1551 if (isbigendian()) {
1552 swapx(&log->device_error_count);
1553 swapx(&log->error_log_index);
1554
1555 for (unsigned i = 0; i < nsectors; i++) {
1556 for (unsigned j = 0; j < 4; j++)
1557 swapx(&log->error_logs[i].commands[j].timestamp);
1558 swapx(&log->error_logs[i].error.timestamp);
1559 }
1560 }
1561
1562 return true;
1563 }
1564
1565
1566 int ataReadSmartThresholds (ata_device * device, struct ata_smart_thresholds_pvt *data){
1567
1568 // get data from device
1569 if (smartcommandhandler(device, READ_THRESHOLDS, 0, (char *)data)){
1570 pout("Error SMART Thresholds Read failed: %s\n", device->get_errmsg());
1571 return -1;
1572 }
1573
1574 // compute its checksum, and issue a warning if needed
1575 if (checksum(data))
1576 checksumwarning("SMART Attribute Thresholds Structure");
1577
1578 // swap endian order if needed
1579 if (isbigendian())
1580 swap2((char *)&(data->revnumber));
1581
1582 return 0;
1583 }
1584
1585 int ataEnableSmart (ata_device * device ){
1586 if (smartcommandhandler(device, ENABLE, 0, NULL)){
1587 pout("Error SMART Enable failed: %s\n", device->get_errmsg());
1588 return -1;
1589 }
1590 return 0;
1591 }
1592
1593 int ataDisableSmart (ata_device * device ){
1594
1595 if (smartcommandhandler(device, DISABLE, 0, NULL)){
1596 pout("Error SMART Disable failed: %s\n", device->get_errmsg());
1597 return -1;
1598 }
1599 return 0;
1600 }
1601
1602 int ataEnableAutoSave(ata_device * device){
1603 if (smartcommandhandler(device, AUTOSAVE, 241, NULL)){
1604 pout("Error SMART Enable Auto-save failed: %s\n", device->get_errmsg());
1605 return -1;
1606 }
1607 return 0;
1608 }
1609
1610 int ataDisableAutoSave(ata_device * device){
1611
1612 if (smartcommandhandler(device, AUTOSAVE, 0, NULL)){
1613 pout("Error SMART Disable Auto-save failed: %s\n", device->get_errmsg());
1614 return -1;
1615 }
1616 return 0;
1617 }
1618
1619 // In *ALL* ATA standards the Enable/Disable AutoOffline command is
1620 // marked "OBSOLETE". It is defined in SFF-8035i Revision 2, and most
1621 // vendors still support it for backwards compatibility. IBM documents
1622 // it for some drives.
1623 int ataEnableAutoOffline (ata_device * device){
1624
1625 /* timer hard coded to 4 hours */
1626 if (smartcommandhandler(device, AUTO_OFFLINE, 248, NULL)){
1627 pout("Error SMART Enable Automatic Offline failed: %s\n", device->get_errmsg());
1628 return -1;
1629 }
1630 return 0;
1631 }
1632
1633 // Another Obsolete Command. See comments directly above, associated
1634 // with the corresponding Enable command.
1635 int ataDisableAutoOffline (ata_device * device){
1636
1637 if (smartcommandhandler(device, AUTO_OFFLINE, 0, NULL)){
1638 pout("Error SMART Disable Automatic Offline failed: %s\n", device->get_errmsg());
1639 return -1;
1640 }
1641 return 0;
1642 }
1643
1644 // If SMART is enabled, supported, and working, then this call is
1645 // guaranteed to return 1, else zero. Note that it should return 1
1646 // regardless of whether the disk's SMART status is 'healthy' or
1647 // 'failing'.
1648 int ataDoesSmartWork(ata_device * device){
1649 int retval=smartcommandhandler(device, STATUS, 0, NULL);
1650
1651 if (-1 == retval)
1652 return 0;
1653
1654 return 1;
1655 }
1656
1657 // This function uses a different interface (DRIVE_TASK) than the
1658 // other commands in this file.
1659 int ataSmartStatus2(ata_device * device){
1660 return smartcommandhandler(device, STATUS_CHECK, 0, NULL);
1661 }
1662
1663 // This is the way to execute ALL tests: offline, short self-test,
1664 // extended self test, with and without captive mode, etc.
1665 // TODO: Move to ataprint.cpp ?
1666 int ataSmartTest(ata_device * device, int testtype, bool force,
1667 const ata_selective_selftest_args & selargs,
1668 const ata_smart_values * sv, uint64_t num_sectors)
1669 {
1670 char cmdmsg[128]; const char *type, *captive;
1671 int cap, retval, select=0;
1672
1673 // Boolean, if set, says test is captive
1674 cap=testtype & CAPTIVE_MASK;
1675
1676 // Set up strings that describe the type of test
1677 if (cap)
1678 captive="captive";
1679 else
1680 captive="off-line";
1681
1682 if (testtype==OFFLINE_FULL_SCAN)
1683 type="off-line";
1684 else if (testtype==SHORT_SELF_TEST || testtype==SHORT_CAPTIVE_SELF_TEST)
1685 type="Short self-test";
1686 else if (testtype==EXTEND_SELF_TEST || testtype==EXTEND_CAPTIVE_SELF_TEST)
1687 type="Extended self-test";
1688 else if (testtype==CONVEYANCE_SELF_TEST || testtype==CONVEYANCE_CAPTIVE_SELF_TEST)
1689 type="Conveyance self-test";
1690 else if ((select=(testtype==SELECTIVE_SELF_TEST || testtype==SELECTIVE_CAPTIVE_SELF_TEST)))
1691 type="Selective self-test";
1692 else
1693 type = 0;
1694
1695 // Check whether another test is already running
1696 if (type && (sv->self_test_exec_status >> 4) == 0xf) {
1697 if (!force) {
1698 pout("Can't start self-test without aborting current test (%d0%% remaining),\n"
1699 "%srun 'smartctl -X' to abort test.\n",
1700 sv->self_test_exec_status & 0x0f,
1701 (!select ? "add '-t force' option to override, or " : ""));
1702 return -1;
1703 }
1704 }
1705 else
1706 force = false;
1707
1708 // If doing a selective self-test, first use WRITE_LOG to write the
1709 // selective self-test log.
1710 ata_selective_selftest_args selargs_io = selargs; // filled with info about actual spans
1711 if (select && (retval = ataWriteSelectiveSelfTestLog(device, selargs_io, sv, num_sectors))) {
1712 if (retval==-4)
1713 pout("Can't start selective self-test without aborting current test: use '-X' option to smartctl.\n");
1714 return retval;
1715 }
1716
1717 // Print ouf message that we are sending the command to test
1718 if (testtype==ABORT_SELF_TEST)
1719 sprintf(cmdmsg,"Abort SMART off-line mode self-test routine");
1720 else if (!type)
1721 sprintf(cmdmsg, "SMART EXECUTE OFF-LINE IMMEDIATE subcommand 0x%02x", testtype);
1722 else
1723 sprintf(cmdmsg,"Execute SMART %s routine immediately in %s mode",type,captive);
1724 pout("Sending command: \"%s\".\n",cmdmsg);
1725
1726 if (select) {
1727 int i;
1728 pout("SPAN STARTING_LBA ENDING_LBA\n");
1729 for (i = 0; i < selargs_io.num_spans; i++)
1730 pout(" %d %20"PRId64" %20"PRId64"\n", i,
1731 selargs_io.span[i].start,
1732 selargs_io.span[i].end);
1733 }
1734
1735 // Now send the command to test
1736 if (smartcommandhandler(device, IMMEDIATE_OFFLINE, testtype, NULL)) {
1737 if (!(cap && device->get_errno() == EIO)) {
1738 pout("Command \"%s\" failed: %s\n", cmdmsg, device->get_errmsg());
1739 return -1;
1740 }
1741 }
1742
1743 // Since the command succeeded, tell user
1744 if (testtype==ABORT_SELF_TEST)
1745 pout("Self-testing aborted!\n");
1746 else {
1747 pout("Drive command \"%s\" successful.\n", cmdmsg);
1748 if (type)
1749 pout("Testing has begun%s.\n", (force ? " (previous test aborted)" : ""));
1750 }
1751 return 0;
1752 }
1753
1754 /* Test Time Functions */
1755 int TestTime(const ata_smart_values *data, int testtype)
1756 {
1757 switch (testtype){
1758 case OFFLINE_FULL_SCAN:
1759 return (int) data->total_time_to_complete_off_line;
1760 case SHORT_SELF_TEST:
1761 case SHORT_CAPTIVE_SELF_TEST:
1762 return (int) data->short_test_completion_time;
1763 case EXTEND_SELF_TEST:
1764 case EXTEND_CAPTIVE_SELF_TEST:
1765 if (data->extend_test_completion_time_b == 0xff
1766 && data->extend_test_completion_time_w != 0x0000
1767 && data->extend_test_completion_time_w != 0xffff)
1768 return data->extend_test_completion_time_w; // ATA-8
1769 else
1770 return data->extend_test_completion_time_b;
1771 case CONVEYANCE_SELF_TEST:
1772 case CONVEYANCE_CAPTIVE_SELF_TEST:
1773 return (int) data->conveyance_test_completion_time;
1774 default:
1775 return 0;
1776 }
1777 }
1778
1779 // This function tells you both about the ATA error log and the
1780 // self-test error log capability (introduced in ATA-5). The bit is
1781 // poorly documented in the ATA/ATAPI standard. Starting with ATA-6,
1782 // SMART error logging is also indicated in bit 0 of DEVICE IDENTIFY
1783 // word 84 and 87. Top two bits must match the pattern 01. BEFORE
1784 // ATA-6 these top two bits still had to match the pattern 01, but the
1785 // remaining bits were reserved (==0).
1786 int isSmartErrorLogCapable (const ata_smart_values * data, const ata_identify_device * identity)
1787 {
1788 unsigned short word84=identity->command_set_extension;
1789 unsigned short word87=identity->csf_default;
1790 int isata6=identity->major_rev_num & (0x01<<6);
1791 int isata7=identity->major_rev_num & (0x01<<7);
1792
1793 if ((isata6 || isata7) && (word84>>14) == 0x01 && (word84 & 0x01))
1794 return 1;
1795
1796 if ((isata6 || isata7) && (word87>>14) == 0x01 && (word87 & 0x01))
1797 return 1;
1798
1799 // otherwise we'll use the poorly documented capability bit
1800 return data->errorlog_capability & 0x01;
1801 }
1802
1803 // See previous function. If the error log exists then the self-test
1804 // log should (must?) also exist.
1805 int isSmartTestLogCapable (const ata_smart_values * data, const ata_identify_device *identity)
1806 {
1807 unsigned short word84=identity->command_set_extension;
1808 unsigned short word87=identity->csf_default;
1809 int isata6=identity->major_rev_num & (0x01<<6);
1810 int isata7=identity->major_rev_num & (0x01<<7);
1811
1812 if ((isata6 || isata7) && (word84>>14) == 0x01 && (word84 & 0x02))
1813 return 1;
1814
1815 if ((isata6 || isata7) && (word87>>14) == 0x01 && (word87 & 0x02))
1816 return 1;
1817
1818
1819 // otherwise we'll use the poorly documented capability bit
1820 return data->errorlog_capability & 0x01;
1821 }
1822
1823
1824 int isGeneralPurposeLoggingCapable(const ata_identify_device *identity)
1825 {
1826 unsigned short word84=identity->command_set_extension;
1827 unsigned short word87=identity->csf_default;
1828
1829 // If bit 14 of word 84 is set to one and bit 15 of word 84 is
1830 // cleared to zero, the contents of word 84 contains valid support
1831 // information. If not, support information is not valid in this
1832 // word.
1833 if ((word84>>14) == 0x01)
1834 // If bit 5 of word 84 is set to one, the device supports the
1835 // General Purpose Logging feature set.
1836 return (word84 & (0x01 << 5));
1837
1838 // If bit 14 of word 87 is set to one and bit 15 of word 87 is
1839 // cleared to zero, the contents of words (87:85) contain valid
1840 // information. If not, information is not valid in these words.
1841 if ((word87>>14) == 0x01)
1842 // If bit 5 of word 87 is set to one, the device supports
1843 // the General Purpose Logging feature set.
1844 return (word87 & (0x01 << 5));
1845
1846 // not capable
1847 return 0;
1848 }
1849
1850
1851 // SMART self-test capability is also indicated in bit 1 of DEVICE
1852 // IDENTIFY word 87 (if top two bits of word 87 match pattern 01).
1853 // However this was only introduced in ATA-6 (but self-test log was in
1854 // ATA-5).
1855 int isSupportExecuteOfflineImmediate(const ata_smart_values *data)
1856 {
1857 return data->offline_data_collection_capability & 0x01;
1858 }
1859
1860 // Note in the ATA-5 standard, the following bit is listed as "Vendor
1861 // Specific". So it may not be reliable. The only use of this that I
1862 // have found is in IBM drives, where it is well-documented. See for
1863 // example page 170, section 13.32.1.18 of the IBM Travelstar 40GNX
1864 // hard disk drive specifications page 164 Revision 1.1 22 Apr 2002.
1865 int isSupportAutomaticTimer(const ata_smart_values * data)
1866 {
1867 return data->offline_data_collection_capability & 0x02;
1868 }
1869 int isSupportOfflineAbort(const ata_smart_values *data)
1870 {
1871 return data->offline_data_collection_capability & 0x04;
1872 }
1873 int isSupportOfflineSurfaceScan(const ata_smart_values * data)
1874 {
1875 return data->offline_data_collection_capability & 0x08;
1876 }
1877 int isSupportSelfTest (const ata_smart_values * data)
1878 {
1879 return data->offline_data_collection_capability & 0x10;
1880 }
1881 int isSupportConveyanceSelfTest(const ata_smart_values * data)
1882 {
1883 return data->offline_data_collection_capability & 0x20;
1884 }
1885 int isSupportSelectiveSelfTest(const ata_smart_values * data)
1886 {
1887 return data->offline_data_collection_capability & 0x40;
1888 }
1889
1890 // Get attribute state
1891 ata_attr_state ata_get_attr_state(const ata_smart_attribute & attr,
1892 int attridx,
1893 const ata_smart_threshold_entry * thresholds,
1894 const ata_vendor_attr_defs & defs,
1895 unsigned char * threshval /* = 0 */)
1896 {
1897 if (!attr.id)
1898 return ATTRSTATE_NON_EXISTING;
1899
1900 // Normalized values (current,worst,threshold) not valid
1901 // if specified by '-v' option.
1902 // (Some SSD disks uses these bytes to store raw value).
1903 if (defs[attr.id].flags & ATTRFLAG_NO_NORMVAL)
1904 return ATTRSTATE_NO_NORMVAL;
1905
1906 // Normally threshold is at same index as attribute
1907 int i = attridx;
1908 if (thresholds[i].id != attr.id) {
1909 // Find threshold id in table
1910 for (i = 0; thresholds[i].id != attr.id; ) {
1911 if (++i >= NUMBER_ATA_SMART_ATTRIBUTES)
1912 // Threshold id missing or thresholds cannot be read
1913 return ATTRSTATE_NO_THRESHOLD;
1914 }
1915 }
1916 unsigned char threshold = thresholds[i].threshold;
1917
1918 // Return threshold if requested
1919 if (threshval)
1920 *threshval = threshold;
1921
1922 // Don't report a failed attribute if its threshold is 0.
1923 // ATA-3 (X3T13/2008D Revision 7b) declares 0x00 as the "always passing"
1924 // threshold (Later ATA versions declare all thresholds as "obsolete").
1925 // In practice, threshold value 0 is often used for usage attributes.
1926 if (!threshold)
1927 return ATTRSTATE_OK;
1928
1929 // Failed now if current value is below threshold
1930 if (attr.current <= threshold)
1931 return ATTRSTATE_FAILED_NOW;
1932
1933 // Failed in the past if worst value is below threshold
1934 if (!(defs[attr.id].flags & ATTRFLAG_NO_WORSTVAL) && attr.worst <= threshold)
1935 return ATTRSTATE_FAILED_PAST;
1936
1937 return ATTRSTATE_OK;
1938 }
1939
1940 // Get default raw value print format
1941 static ata_attr_raw_format get_default_raw_format(unsigned char id)
1942 {
1943 switch (id) {
1944 case 3: // Spin-up time
1945 return RAWFMT_RAW16_OPT_AVG16;
1946
1947 case 5: // Reallocated sector count
1948 case 196: // Reallocated event count
1949 return RAWFMT_RAW16_OPT_RAW16;
1950
1951 case 190: // Temperature
1952 case 194:
1953 return RAWFMT_TEMPMINMAX;
1954
1955 default:
1956 return RAWFMT_RAW48;
1957 }
1958 }
1959
1960 // Get attribute raw value.
1961 uint64_t ata_get_attr_raw_value(const ata_smart_attribute & attr,
1962 const ata_vendor_attr_defs & defs)
1963 {
1964 const ata_vendor_attr_defs::entry & def = defs[attr.id];
1965
1966 // Use default byteorder if not specified
1967 const char * byteorder = def.byteorder;
1968 if (!*byteorder) {
1969 switch (def.raw_format) {
1970 case RAWFMT_RAW64:
1971 case RAWFMT_HEX64:
1972 byteorder = "543210wv"; break;
1973 case RAWFMT_RAW24_DIV_RAW32:
1974 case RAWFMT_MSEC24_HOUR32:
1975 byteorder = "r543210"; break;
1976 default:
1977 byteorder = "543210"; break;
1978 }
1979 }
1980
1981 // Build 64-bit value from selected bytes
1982 uint64_t rawvalue = 0;
1983 for (int i = 0; byteorder[i]; i++) {
1984 unsigned char b;
1985 switch (byteorder[i]) {
1986 case '0': b = attr.raw[0]; break;
1987 case '1': b = attr.raw[1]; break;
1988 case '2': b = attr.raw[2]; break;
1989 case '3': b = attr.raw[3]; break;
1990 case '4': b = attr.raw[4]; break;
1991 case '5': b = attr.raw[5]; break;
1992 case 'r': b = attr.reserv; break;
1993 case 'v': b = attr.current; break;
1994 case 'w': b = attr.worst; break;
1995 default : b = 0; break;
1996 }
1997 rawvalue <<= 8; rawvalue |= b;
1998 }
1999
2000 return rawvalue;
2001 }
2002
2003
2004 // Format attribute raw value.
2005 std::string ata_format_attr_raw_value(const ata_smart_attribute & attr,
2006 const ata_vendor_attr_defs & defs)
2007 {
2008 // Get 48 bit or 64 bit raw value
2009 uint64_t rawvalue = ata_get_attr_raw_value(attr, defs);
2010
2011 // Split into bytes and words
2012 unsigned char raw[6];
2013 raw[0] = (unsigned char) rawvalue;
2014 raw[1] = (unsigned char)(rawvalue >> 8);
2015 raw[2] = (unsigned char)(rawvalue >> 16);
2016 raw[3] = (unsigned char)(rawvalue >> 24);
2017 raw[4] = (unsigned char)(rawvalue >> 32);
2018 raw[5] = (unsigned char)(rawvalue >> 40);
2019 unsigned word[3];
2020 word[0] = raw[0] | (raw[1] << 8);
2021 word[1] = raw[2] | (raw[3] << 8);
2022 word[2] = raw[4] | (raw[5] << 8);
2023
2024 // Get print format
2025 ata_attr_raw_format format = defs[attr.id].raw_format;
2026 if (format == RAWFMT_DEFAULT)
2027 format = get_default_raw_format(attr.id);
2028
2029 // Print
2030 std::string s;
2031 switch (format) {
2032 case RAWFMT_RAW8:
2033 s = strprintf("%d %d %d %d %d %d",
2034 raw[5], raw[4], raw[3], raw[2], raw[1], raw[0]);
2035 break;
2036
2037 case RAWFMT_RAW16:
2038 s = strprintf("%u %u %u", word[2], word[1], word[0]);
2039 break;
2040
2041 case RAWFMT_RAW48:
2042 case RAWFMT_RAW64:
2043 s = strprintf("%"PRIu64, rawvalue);
2044 break;
2045
2046 case RAWFMT_HEX48:
2047 s = strprintf("0x%012"PRIx64, rawvalue);
2048 break;
2049
2050 case RAWFMT_HEX64:
2051 s = strprintf("0x%016"PRIx64, rawvalue);
2052 break;
2053
2054 case RAWFMT_RAW16_OPT_RAW16:
2055 s = strprintf("%u", word[0]);
2056 if (word[1] || word[2])
2057 s += strprintf(" (%u, %u)", word[2], word[1]);
2058 break;
2059
2060 case RAWFMT_RAW16_OPT_AVG16:
2061 s = strprintf("%u", word[0]);
2062 if (word[1])
2063 s += strprintf(" (Average %u)", word[1]);
2064 break;
2065
2066 case RAWFMT_RAW24_DIV_RAW24:
2067 s = strprintf("%u/%u",
2068 (unsigned)(rawvalue >> 24), (unsigned)(rawvalue & 0x00ffffffULL));
2069 break;
2070
2071 case RAWFMT_RAW24_DIV_RAW32:
2072 s = strprintf("%u/%u",
2073 (unsigned)(rawvalue >> 32), (unsigned)(rawvalue & 0xffffffffULL));
2074 break;
2075
2076 case RAWFMT_MIN2HOUR:
2077 {
2078 // minutes
2079 int64_t temp = word[0]+(word[1]<<16);
2080 int64_t tmp1 = temp/60;
2081 int64_t tmp2 = temp%60;
2082 s = strprintf("%"PRIu64"h+%02"PRIu64"m", tmp1, tmp2);
2083 if (word[2])
2084 s += strprintf(" (%u)", word[2]);
2085 }
2086 break;
2087
2088 case RAWFMT_SEC2HOUR:
2089 {
2090 // seconds
2091 int64_t hours = rawvalue/3600;
2092 int64_t minutes = (rawvalue-3600*hours)/60;
2093 int64_t seconds = rawvalue%60;
2094 s = strprintf("%"PRIu64"h+%02"PRIu64"m+%02"PRIu64"s", hours, minutes, seconds);
2095 }
2096 break;
2097
2098 case RAWFMT_HALFMIN2HOUR:
2099 {
2100 // 30-second counter
2101 int64_t hours = rawvalue/120;
2102 int64_t minutes = (rawvalue-120*hours)/2;
2103 s += strprintf("%"PRIu64"h+%02"PRIu64"m", hours, minutes);
2104 }
2105 break;
2106
2107 case RAWFMT_MSEC24_HOUR32:
2108 {
2109 // hours + milliseconds
2110 unsigned hours = (unsigned)(rawvalue & 0xffffffffULL);
2111 unsigned milliseconds = (unsigned)(rawvalue >> 32);
2112 unsigned seconds = milliseconds / 1000;
2113 s = strprintf("%uh+%02um+%02u.%03us",
2114 hours, seconds / 60, seconds % 60, milliseconds % 1000);
2115 }
2116 break;
2117
2118 case RAWFMT_TEMPMINMAX:
2119 // Temperature
2120 {
2121 // Search for possible min/max values
2122 // 00 HH 00 LL 00 TT (Hitachi/IBM)
2123 // 00 00 HH LL 00 TT (Maxtor, Samsung)
2124 // 00 00 00 HH LL TT (WDC)
2125 unsigned char lo = 0, hi = 0;
2126 int cnt = 0;
2127 for (int i = 1; i < 6; i++) {
2128 if (raw[i])
2129 switch (cnt++) {
2130 case 0:
2131 lo = raw[i];
2132 break;
2133 case 1:
2134 if (raw[i] < lo) {
2135 hi = lo; lo = raw[i];
2136 }
2137 else
2138 hi = raw[i];
2139 break;
2140 }
2141 }
2142
2143 unsigned char t = raw[0];
2144 if (cnt == 0)
2145 s = strprintf("%d", t);
2146 else if (cnt == 2 && 0 < lo && lo <= t && t <= hi && hi < 128)
2147 s = strprintf("%d (Min/Max %d/%d)", t, lo, hi);
2148 else
2149 s = strprintf("%d (%d %d %d %d %d)", t, raw[5], raw[4], raw[3], raw[2], raw[1]);
2150 }
2151 break;
2152
2153 case RAWFMT_TEMP10X:
2154 // ten times temperature in Celsius
2155 s = strprintf("%d.%d", word[0]/10, word[0]%10);
2156 break;
2157
2158 default:
2159 s = "?"; // Should not happen
2160 break;
2161 }
2162
2163 return s;
2164 }
2165
2166 // Attribute names shouldn't be longer than 23 chars, otherwise they break the
2167 // output of smartctl.
2168 static const char * get_default_attr_name(unsigned char id)
2169 {
2170 switch (id) {
2171 case 1:
2172 return "Raw_Read_Error_Rate";
2173 case 2:
2174 return "Throughput_Performance";
2175 case 3:
2176 return "Spin_Up_Time";
2177 case 4:
2178 return "Start_Stop_Count";
2179 case 5:
2180 return "Reallocated_Sector_Ct";
2181 case 6:
2182 return "Read_Channel_Margin";
2183 case 7:
2184 return "Seek_Error_Rate";
2185 case 8:
2186 return "Seek_Time_Performance";
2187 case 9:
2188 return "Power_On_Hours";
2189 case 10:
2190 return "Spin_Retry_Count";
2191 case 11:
2192 return "Calibration_Retry_Count";
2193 case 12:
2194 return "Power_Cycle_Count";
2195 case 13:
2196 return "Read_Soft_Error_Rate";
2197 case 175:
2198 return "Program_Fail_Count_Chip";
2199 case 176:
2200 return "Erase_Fail_Count_Chip";
2201 case 177:
2202 return "Wear_Leveling_Count";
2203 case 178:
2204 return "Used_Rsvd_Blk_Cnt_Chip";
2205 case 179:
2206 return "Used_Rsvd_Blk_Cnt_Tot";
2207 case 180:
2208 return "Unused_Rsvd_Blk_Cnt_Tot";
2209 case 181:
2210 return "Program_Fail_Cnt_Total";
2211 case 182:
2212 return "Erase_Fail_Count_Total";
2213 case 183:
2214 return "Runtime_Bad_Block";
2215 case 184:
2216 return "End-to-End_Error";
2217 case 187:
2218 return "Reported_Uncorrect";
2219 case 188:
2220 return "Command_Timeout";
2221 case 189:
2222 return "High_Fly_Writes";
2223 case 190:
2224 // Western Digital uses this for temperature.
2225 // It's identical to Attribute 194 except that it
2226 // has a failure threshold set to correspond to the
2227 // max allowed operating temperature of the drive, which
2228 // is typically 55C. So if this attribute has failed
2229 // in the past, it indicates that the drive temp exceeded
2230 // 55C sometime in the past.
2231 return "Airflow_Temperature_Cel";
2232 case 191:
2233 return "G-Sense_Error_Rate";
2234 case 192:
2235 return "Power-Off_Retract_Count";
2236 case 193:
2237 return "Load_Cycle_Count";
2238 case 194:
2239 return "Temperature_Celsius";
2240 case 195:
2241 // Fujitsu: "ECC_On_The_Fly_Count";
2242 return "Hardware_ECC_Recovered";
2243 case 196:
2244 return "Reallocated_Event_Count";
2245 case 197:
2246 return "Current_Pending_Sector";
2247 case 198:
2248 return "Offline_Uncorrectable";
2249 case 199:
2250 return "UDMA_CRC_Error_Count";
2251 case 200:
2252 // Western Digital
2253 return "Multi_Zone_Error_Rate";
2254 case 201:
2255 return "Soft_Read_Error_Rate";
2256 case 202:
2257 // Fujitsu: "TA_Increase_Count"
2258 return "Data_Address_Mark_Errs";
2259 case 203:
2260 // Fujitsu
2261 return "Run_Out_Cancel";
2262 // Maxtor: ECC Errors
2263 case 204:
2264 // Fujitsu: "Shock_Count_Write_Opern"
2265 return "Soft_ECC_Correction";
2266 case 205:
2267 // Fujitsu: "Shock_Rate_Write_Opern"
2268 return "Thermal_Asperity_Rate";
2269 case 206:
2270 // Fujitsu
2271 return "Flying_Height";
2272 case 207:
2273 // Maxtor
2274 return "Spin_High_Current";
2275 case 208:
2276 // Maxtor
2277 return "Spin_Buzz";
2278 case 209:
2279 // Maxtor
2280 return "Offline_Seek_Performnce";
2281 case 220:
2282 return "Disk_Shift";
2283 case 221:
2284 return "G-Sense_Error_Rate";
2285 case 222:
2286 return "Loaded_Hours";
2287 case 223:
2288 return "Load_Retry_Count";
2289 case 224:
2290 return "Load_Friction";
2291 case 225:
2292 return "Load_Cycle_Count";
2293 case 226:
2294 return "Load-in_Time";
2295 case 227:
2296 return "Torq-amp_Count";
2297 case 228:
2298 return "Power-off_Retract_Count";
2299 case 230:
2300 // seen in IBM DTPA-353750
2301 return "Head_Amplitude";
2302 case 231:
2303 return "Temperature_Celsius";
2304 case 232:
2305 // seen in Intel X25-E SSD
2306 return "Available_Reservd_Space";
2307 case 233:
2308 // seen in Intel X25-E SSD
2309 return "Media_Wearout_Indicator";
2310 case 240:
2311 return "Head_Flying_Hours";
2312 case 241:
2313 return "Total_LBAs_Written";
2314 case 242:
2315 return "Total_LBAs_Read";
2316 case 250:
2317 return "Read_Error_Retry_Rate";
2318 case 254:
2319 return "Free_Fall_Sensor";
2320 default:
2321 return "Unknown_Attribute";
2322 }
2323 }
2324
2325 // Get attribute name
2326 std::string ata_get_smart_attr_name(unsigned char id, const ata_vendor_attr_defs & defs)
2327 {
2328 if (!defs[id].name.empty())
2329 return defs[id].name;
2330 else
2331 return get_default_attr_name(id);
2332 }
2333
2334 // Find attribute index for attribute id, -1 if not found.
2335 int ata_find_attr_index(unsigned char id, const ata_smart_values & smartval)
2336 {
2337 if (!id)
2338 return -1;
2339 for (int i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
2340 if (smartval.vendor_attributes[i].id == id)
2341 return i;
2342 }
2343 return -1;
2344 }
2345
2346 // Return Temperature Attribute raw value selected according to possible
2347 // non-default interpretations. If the Attribute does not exist, return 0
2348 unsigned char ata_return_temperature_value(const ata_smart_values * data, const ata_vendor_attr_defs & defs)
2349 {
2350 for (int i = 0; i < 3; i++) {
2351 static const unsigned char ids[3] = {194, 9, 220};
2352 unsigned char id = ids[i];
2353 const ata_attr_raw_format format = defs[id].raw_format;
2354 if (!( (id == 194 && format == RAWFMT_DEFAULT)
2355 || format == RAWFMT_TEMPMINMAX || format == RAWFMT_TEMP10X))
2356 continue;
2357 int idx = ata_find_attr_index(id, *data);
2358 if (idx < 0)
2359 continue;
2360 uint64_t raw = ata_get_attr_raw_value(data->vendor_attributes[idx], defs);
2361 unsigned temp;
2362 // ignore possible min/max values in high words
2363 if (format == RAWFMT_TEMP10X) // -v N,temp10x
2364 temp = ((unsigned short)raw + 5) / 10;
2365 else
2366 temp = (unsigned char)raw;
2367 if (!(0 < temp && temp < 128))
2368 continue;
2369 return temp;
2370 }
2371 // No valid attribute found
2372 return 0;
2373 }
2374
2375
2376 // Read SCT Status
2377 int ataReadSCTStatus(ata_device * device, ata_sct_status_response * sts)
2378 {
2379 // read SCT status via SMART log 0xe0
2380 memset(sts, 0, sizeof(*sts));
2381 if (smartcommandhandler(device, READ_LOG, 0xe0, (char *)sts)){
2382 pout("Error Read SCT Status failed: %s\n", device->get_errmsg());
2383 return -1;
2384 }
2385
2386 // swap endian order if needed
2387 if (isbigendian()){
2388 swapx(&sts->format_version);
2389 swapx(&sts->sct_version);
2390 swapx(&sts->sct_spec);
2391 swapx(&sts->ext_status_code);
2392 swapx(&sts->action_code);
2393 swapx(&sts->function_code);
2394 swapx(&sts->over_limit_count);
2395 swapx(&sts->under_limit_count);
2396 }
2397
2398 // Check format version
2399 if (!(sts->format_version == 2 || sts->format_version == 3)) {
2400 pout("Error unknown SCT Status format version %u, should be 2 or 3.\n", sts->format_version);
2401 return -1;
2402 }
2403 return 0;
2404 }
2405
2406 // Read SCT Temperature History Table and Status
2407 int ataReadSCTTempHist(ata_device * device, ata_sct_temperature_history_table * tmh,
2408 ata_sct_status_response * sts)
2409 {
2410 // Check initial status
2411 if (ataReadSCTStatus(device, sts))
2412 return -1;
2413
2414 // Do nothing if other SCT command is executing
2415 if (sts->ext_status_code == 0xffff) {
2416 pout("Another SCT command is executing, abort Read Data Table\n"
2417 "(SCT ext_status_code 0x%04x, action_code=%u, function_code=%u)\n",
2418 sts->ext_status_code, sts->action_code, sts->function_code);
2419 return -1;
2420 }
2421
2422 ata_sct_data_table_command cmd; memset(&cmd, 0, sizeof(cmd));
2423 // CAUTION: DO NOT CHANGE THIS VALUE (SOME ACTION CODES MAY ERASE DISK)
2424 cmd.action_code = 5; // Data table command
2425 cmd.function_code = 1; // Read table
2426 cmd.table_id = 2; // Temperature History Table
2427
2428 // swap endian order if needed
2429 if (isbigendian()) {
2430 swapx(&cmd.action_code);
2431 swapx(&cmd.function_code);
2432 swapx(&cmd.table_id);
2433 }
2434
2435 // write command via SMART log page 0xe0
2436 if (smartcommandhandler(device, WRITE_LOG, 0xe0, (char *)&cmd)){
2437 pout("Error Write SCT Data Table command failed: %s\n", device->get_errmsg());
2438 return -1;
2439 }
2440
2441 // read SCT data via SMART log page 0xe1
2442 memset(tmh, 0, sizeof(*tmh));
2443 if (smartcommandhandler(device, READ_LOG, 0xe1, (char *)tmh)){
2444 pout("Error Read SCT Data Table failed: %s\n", device->get_errmsg());
2445 return -1;
2446 }
2447
2448 // re-read and check SCT status
2449 if (ataReadSCTStatus(device, sts))
2450 return -1;
2451
2452 if (!(sts->ext_status_code == 0 && sts->action_code == 5 && sts->function_code == 1)) {
2453 pout("Error unexpected SCT status 0x%04x (action_code=%u, function_code=%u)\n",
2454 sts->ext_status_code, sts->action_code, sts->function_code);
2455 return -1;
2456 }
2457
2458 // swap endian order if needed
2459 if (isbigendian()){
2460 swapx(&tmh->format_version);
2461 swapx(&tmh->sampling_period);
2462 swapx(&tmh->interval);
2463 }
2464
2465 // Check format version
2466 if (tmh->format_version != 2) {
2467 pout("Error unknown SCT Temperature History Format Version (%u), should be 2.\n", tmh->format_version);
2468 return -1;
2469 }
2470 return 0;
2471 }
2472
2473 // Set SCT Temperature Logging Interval
2474 int ataSetSCTTempInterval(ata_device * device, unsigned interval, bool persistent)
2475 {
2476 // Check initial status
2477 ata_sct_status_response sts;
2478 if (ataReadSCTStatus(device, &sts))
2479 return -1;
2480
2481 // Do nothing if other SCT command is executing
2482 if (sts.ext_status_code == 0xffff) {
2483 pout("Another SCT command is executing, abort Feature Control\n"
2484 "(SCT ext_status_code 0x%04x, action_code=%u, function_code=%u)\n",
2485 sts.ext_status_code, sts.action_code, sts.function_code);
2486 return -1;
2487 }
2488
2489 ata_sct_feature_control_command cmd; memset(&cmd, 0, sizeof(cmd));
2490 // CAUTION: DO NOT CHANGE THIS VALUE (SOME ACTION CODES MAY ERASE DISK)
2491 cmd.action_code = 4; // Feature Control command
2492 cmd.function_code = 1; // Set state
2493 cmd.feature_code = 3; // Temperature logging interval
2494 cmd.state = interval;
2495 cmd.option_flags = (persistent ? 0x01 : 0x00);
2496
2497 // swap endian order if needed
2498 if (isbigendian()) {
2499 swapx(&cmd.action_code);
2500 swapx(&cmd.function_code);
2501 swapx(&cmd.feature_code);
2502 swapx(&cmd.state);
2503 swapx(&cmd.option_flags);
2504 }
2505
2506 // write command via SMART log page 0xe0
2507 if (smartcommandhandler(device, WRITE_LOG, 0xe0, (char *)&cmd)){
2508 pout("Error Write SCT Feature Control Command failed: %s\n", device->get_errmsg());
2509 return -1;
2510 }
2511
2512 // re-read and check SCT status
2513 if (ataReadSCTStatus(device, &sts))
2514 return -1;
2515
2516 if (!(sts.ext_status_code == 0 && sts.action_code == 4 && sts.function_code == 1)) {
2517 pout("Error unexcepted SCT status 0x%04x (action_code=%u, function_code=%u)\n",
2518 sts.ext_status_code, sts.action_code, sts.function_code);
2519 return -1;
2520 }
2521 return 0;
2522 }
2523
2524 // Get/Set SCT Error Recovery Control
2525 static int ataGetSetSCTErrorRecoveryControltime(ata_device * device, unsigned type,
2526 bool set, unsigned short & time_limit)
2527 {
2528 // Check initial status
2529 ata_sct_status_response sts;
2530 if (ataReadSCTStatus(device, &sts))
2531 return -1;
2532
2533 // Do nothing if other SCT command is executing
2534 if (sts.ext_status_code == 0xffff) {
2535 pout("Another SCT command is executing, abort Error Recovery Control\n"
2536 "(SCT ext_status_code 0x%04x, action_code=%u, function_code=%u)\n",
2537 sts.ext_status_code, sts.action_code, sts.function_code);
2538 return -1;
2539 }
2540
2541 ata_sct_error_recovery_control_command cmd; memset(&cmd, 0, sizeof(cmd));
2542 // CAUTION: DO NOT CHANGE THIS VALUE (SOME ACTION CODES MAY ERASE DISK)
2543 cmd.action_code = 3; // Error Recovery Control command
2544 cmd.function_code = (set ? 1 : 2); // 1=Set timer, 2=Get timer
2545 cmd.selection_code = type; // 1=Read timer, 2=Write timer
2546 if (set)
2547 cmd.time_limit = time_limit;
2548
2549 // swap endian order if needed
2550 if (isbigendian()) {
2551 swapx(&cmd.action_code);
2552 swapx(&cmd.function_code);
2553 swapx(&cmd.selection_code);
2554 swapx(&cmd.time_limit);
2555 }
2556
2557 // write command via SMART log page 0xe0
2558 // TODO: Debug output
2559 ata_cmd_in in;
2560 in.in_regs.command = ATA_SMART_CMD;
2561 in.in_regs.lba_high = SMART_CYL_HI; in.in_regs.lba_mid = SMART_CYL_LOW;
2562 in.in_regs.features = ATA_SMART_WRITE_LOG_SECTOR;
2563 in.in_regs.lba_low = 0xe0;
2564 in.set_data_out(&cmd, 1);
2565
2566 if (!set)
2567 // Time limit returned in ATA registers
2568 in.out_needed.sector_count = in.out_needed.lba_low = true;
2569
2570 ata_cmd_out out;
2571 if (!device->ata_pass_through(in, out)) {
2572 pout("Error Write SCT (%cet) Error Recovery Control Command failed: %s\n",
2573 (!set ? 'G' : 'S'), device->get_errmsg());
2574 return -1;
2575 }
2576
2577 // re-read and check SCT status
2578 if (ataReadSCTStatus(device, &sts))
2579 return -1;
2580
2581 if (!(sts.ext_status_code == 0 && sts.action_code == 3 && sts.function_code == (set ? 1 : 2))) {
2582 pout("Error unexcepted SCT status 0x%04x (action_code=%u, function_code=%u)\n",
2583 sts.ext_status_code, sts.action_code, sts.function_code);
2584 return -1;
2585 }
2586
2587 if (!set) {
2588 // Check whether registers are properly returned by ioctl()
2589 if (!(out.out_regs.sector_count.is_set() && out.out_regs.lba_low.is_set())) {
2590 // TODO: Output register support should be checked within each ata_pass_through()
2591 // implementation before command is issued.
2592 pout("Error SMART WRITE LOG does not return COUNT and LBA_LOW register\n");
2593 return -1;
2594 }
2595 // Return value to caller
2596 time_limit = out.out_regs.sector_count | (out.out_regs.lba_low << 8);
2597 }
2598
2599 return 0;
2600 }
2601
2602 // Get SCT Error Recovery Control
2603 int ataGetSCTErrorRecoveryControltime(ata_device * device, unsigned type, unsigned short & time_limit)
2604 {
2605 return ataGetSetSCTErrorRecoveryControltime(device, type, false/*get*/, time_limit);
2606 }
2607
2608 // Set SCT Error Recovery Control
2609 int ataSetSCTErrorRecoveryControltime(ata_device * device, unsigned type, unsigned short time_limit)
2610 {
2611 return ataGetSetSCTErrorRecoveryControltime(device, type, true/*set*/, time_limit);
2612 }
2613
2614
2615 // Print one self-test log entry.
2616 // Returns:
2617 // -1: self-test failed
2618 // 1: extended self-test completed without error
2619 // 0: otherwise
2620 int ataPrintSmartSelfTestEntry(unsigned testnum, unsigned char test_type,
2621 unsigned char test_status,
2622 unsigned short timestamp,
2623 uint64_t failing_lba,
2624 bool print_error_only, bool & print_header)
2625 {
2626 // Check status and type for return value
2627 int retval = 0;
2628 switch (test_status >> 4) {
2629 case 0x0:
2630 if ((test_type & 0x0f) == 0x02)
2631 retval = 1; // extended self-test completed without error
2632 break;
2633 case 0x3: case 0x4:
2634 case 0x5: case 0x6:
2635 case 0x7: case 0x8:
2636 retval = -1; // self-test failed
2637 break;
2638 }
2639
2640 if (retval >= 0 && print_error_only)
2641 return retval;
2642
2643 std::string msgtest;
2644 switch (test_type) {
2645 case 0x00: msgtest = "Offline"; break;
2646 case 0x01: msgtest = "Short offline"; break;
2647 case 0x02: msgtest = "Extended offline"; break;
2648 case 0x03: msgtest = "Conveyance offline"; break;
2649 case 0x04: msgtest = "Selective offline"; break;
2650 case 0x7f: msgtest = "Abort offline test"; break;
2651 case 0x81: msgtest = "Short captive"; break;
2652 case 0x82: msgtest = "Extended captive"; break;
2653 case 0x83: msgtest = "Conveyance captive"; break;
2654 case 0x84: msgtest = "Selective captive"; break;
2655 default:
2656 if ((0x40 <= test_type && test_type <= 0x7e) || 0x90 <= test_type)
2657 msgtest = strprintf("Vendor (0x%02x)", test_type);
2658 else
2659 msgtest = strprintf("Reserved (0x%02x)", test_type);
2660 }
2661
2662 std::string msgstat;
2663 switch (test_status >> 4) {
2664 case 0x0: msgstat = "Completed without error"; break;
2665 case 0x1: msgstat = "Aborted by host"; break;
2666 case 0x2: msgstat = "Interrupted (host reset)"; break;
2667 case 0x3: msgstat = "Fatal or unknown error"; break;
2668 case 0x4: msgstat = "Completed: unknown failure"; break;
2669 case 0x5: msgstat = "Completed: electrical failure"; break;
2670 case 0x6: msgstat = "Completed: servo/seek failure"; break;
2671 case 0x7: msgstat = "Completed: read failure"; break;
2672 case 0x8: msgstat = "Completed: handling damage??"; break;
2673 case 0xf: msgstat = "Self-test routine in progress"; break;
2674 default: msgstat = strprintf("Unknown status (0x%x)", test_status >> 4);
2675 }
2676
2677 // Print header once
2678 if (print_header) {
2679 print_header = false;
2680 pout("Num Test_Description Status Remaining LifeTime(hours) LBA_of_first_error\n");
2681 }
2682
2683 char msglba[32];
2684 if (retval < 0 && failing_lba < 0xffffffffffffULL)
2685 snprintf(msglba, sizeof(msglba), "%"PRIu64, failing_lba);
2686 else
2687 strcpy(msglba, "-");
2688
2689 pout("#%2u %-19s %-29s %1d0%% %8u %s\n", testnum,
2690 msgtest.c_str(), msgstat.c_str(), test_status & 0x0f, timestamp, msglba);
2691
2692 return retval;
2693 }
2694
2695 // Print Smart self-test log, used by smartctl and smartd.
2696 // return value is:
2697 // bottom 8 bits: number of entries found where self-test showed an error
2698 // remaining bits: if nonzero, power on hours of last self-test where error was found
2699 int ataPrintSmartSelfTestlog(const ata_smart_selftestlog * data, bool allentries,
2700 unsigned char fix_firmwarebug)
2701 {
2702 if (allentries)
2703 pout("SMART Self-test log structure revision number %d\n",(int)data->revnumber);
2704 if ((data->revnumber!=0x0001) && allentries && fix_firmwarebug != FIX_SAMSUNG)
2705 pout("Warning: ATA Specification requires self-test log structure revision number = 1\n");
2706 if (data->mostrecenttest==0){
2707 if (allentries)
2708 pout("No self-tests have been logged. [To run self-tests, use: smartctl -t]\n\n");
2709 return 0;
2710 }
2711
2712 bool noheaderprinted = true;
2713 int errcnt = 0, hours = 0, igncnt = 0;
2714 int testno = 0, ext_ok_testno = -1;
2715
2716 // print log
2717 for (int i = 20; i >= 0; i--) {
2718 // log is a circular buffer
2719 int j = (i+data->mostrecenttest)%21;
2720 const ata_smart_selftestlog_struct * log = data->selftest_struct+j;
2721
2722 if (nonempty(log, sizeof(*log))) {
2723 // count entry based on non-empty structures -- needed for
2724 // Seagate only -- other vendors don't have blank entries 'in
2725 // the middle'
2726 testno++;
2727
2728 // T13/1321D revision 1c: (Data structure Rev #1)
2729
2730 //The failing LBA shall be the LBA of the uncorrectable sector
2731 //that caused the test to fail. If the device encountered more
2732 //than one uncorrectable sector during the test, this field
2733 //shall indicate the LBA of the first uncorrectable sector
2734 //encountered. If the test passed or the test failed for some
2735 //reason other than an uncorrectable sector, the value of this
2736 //field is undefined.
2737
2738 // This is true in ALL ATA-5 specs
2739 uint64_t lba48 = (log->lbafirstfailure < 0xffffffff ? log->lbafirstfailure : 0xffffffffffffULL);
2740
2741 // Print entry
2742 int state = ataPrintSmartSelfTestEntry(testno,
2743 log->selftestnumber, log->selfteststatus,
2744 log->timestamp, lba48, !allentries, noheaderprinted);
2745
2746 if (state < 0) {
2747 // Self-test showed an error
2748 if (ext_ok_testno < 0) {
2749 errcnt++;
2750
2751 // keep track of time of most recent error
2752 if (!hours)
2753 hours = log->timestamp;
2754 }
2755 else
2756 // Newer successful extended self-test exits
2757 igncnt++;
2758 }
2759 else if (state > 0 && ext_ok_testno < 0) {
2760 // Latest successful extended self-test
2761 ext_ok_testno = testno;
2762 }
2763 }
2764 }
2765
2766 if (igncnt)
2767 pout("%d of %d failed self-tests are outdated by newer successful extended offline self-test #%2d\n",
2768 igncnt, igncnt+errcnt, ext_ok_testno);
2769
2770 if (!allentries && !noheaderprinted)
2771 pout("\n");
2772
2773 return ((hours << 8) | errcnt);
2774 }
2775
2776
2777 /////////////////////////////////////////////////////////////////////////////
2778 // Pseudo-device to parse "smartctl -r ataioctl,2 ..." output and simulate
2779 // an ATA device with same behaviour
2780
2781 namespace {
2782
2783 class parsed_ata_device
2784 : public /*implements*/ ata_device_with_command_set
2785 {
2786 public:
2787 parsed_ata_device(smart_interface * intf, const char * dev_name);
2788
2789 virtual ~parsed_ata_device() throw();
2790
2791 virtual bool is_open() const;
2792
2793 virtual bool open();
2794
2795 virtual bool close();
2796
2797 virtual bool ata_identify_is_cached() const;
2798
2799 protected:
2800 virtual int ata_command_interface(smart_command_set command, int select, char * data);
2801
2802 private:
2803 // Table of parsed commands, return value, data
2804 struct parsed_ata_command
2805 {
2806 smart_command_set command;
2807 int select;
2808 int retval, errval;
2809 char * data;
2810 };
2811
2812 enum { max_num_commands = 32 };
2813 parsed_ata_command m_command_table[max_num_commands];
2814
2815 int m_num_commands;
2816 int m_next_replay_command;
2817 bool m_replay_out_of_sync;
2818 bool m_ata_identify_is_cached;
2819 };
2820
2821 static const char * nextline(const char * s, int & lineno)
2822 {
2823 for (s += strcspn(s, "\r\n"); *s == '\r' || *s == '\n'; s++) {
2824 if (*s == '\r' && s[1] == '\n')
2825 s++;
2826 lineno++;
2827 }
2828 return s;
2829 }
2830
2831 static int name2command(const char * s)
2832 {
2833 for (int i = 0; i < (int)(sizeof(commandstrings)/sizeof(commandstrings[0])); i++) {
2834 if (!strcmp(s, commandstrings[i]))
2835 return i;
2836 }
2837 return -1;
2838 }
2839
2840 static bool matchcpy(char * dest, size_t size, const char * src, const regmatch_t & srcmatch)
2841 {
2842 if (srcmatch.rm_so < 0)
2843 return false;
2844 size_t n = srcmatch.rm_eo - srcmatch.rm_so;
2845 if (n >= size)
2846 n = size-1;
2847 memcpy(dest, src + srcmatch.rm_so, n);
2848 dest[n] = 0;
2849 return true;
2850 }
2851
2852 static inline int matchtoi(const char * src, const regmatch_t & srcmatch, int defval)
2853 {
2854 if (srcmatch.rm_so < 0)
2855 return defval;
2856 return atoi(src + srcmatch.rm_so);
2857 }
2858
2859 parsed_ata_device::parsed_ata_device(smart_interface * intf, const char * dev_name)
2860 : smart_device(intf, dev_name, "ata", ""),
2861 m_num_commands(0),
2862 m_next_replay_command(0),
2863 m_replay_out_of_sync(false),
2864 m_ata_identify_is_cached(false)
2865 {
2866 memset(m_command_table, 0, sizeof(m_command_table));
2867 }
2868
2869 parsed_ata_device::~parsed_ata_device() throw()
2870 {
2871 close();
2872 }
2873
2874 bool parsed_ata_device::is_open() const
2875 {
2876 return (m_num_commands > 0);
2877 }
2878
2879 // Parse stdin and build command table
2880 bool parsed_ata_device::open()
2881 {
2882 const char * pathname = get_dev_name();
2883 if (strcmp(pathname, "-"))
2884 return set_err(EINVAL);
2885 pathname = "<stdin>";
2886 // Fill buffer
2887 char buffer[64*1024];
2888 int size = 0;
2889 while (size < (int)sizeof(buffer)) {
2890 int nr = fread(buffer, 1, sizeof(buffer), stdin);
2891 if (nr <= 0)
2892 break;
2893 size += nr;
2894 }
2895 if (size <= 0)
2896 return set_err(ENOENT, "%s: Unexpected EOF", pathname);
2897 if (size >= (int)sizeof(buffer))
2898 return set_err(EIO, "%s: Buffer overflow", pathname);
2899 buffer[size] = 0;
2900
2901 // Regex to match output from "-r ataioctl,2"
2902 static const char pattern[] = "^"
2903 "(" // (1
2904 "REPORT-IOCTL: DeviceF?D?=[^ ]+ Command=([A-Z ]*[A-Z])" // (2)
2905 "(" // (3
2906 "( InputParameter=([0-9]+))?" // (4 (5))
2907 "|"
2908 "( returned (-?[0-9]+)( errno=([0-9]+)[^\r\n]*)?)" // (6 (7) (8 (9)))
2909 ")" // )
2910 "[\r\n]" // EOL match necessary to match optional parts above
2911 "|"
2912 "===== \\[([A-Z ]*[A-Z])\\] DATA START " // (10)
2913 "|"
2914 " *(En|Dis)abled status cached by OS, " // (11)
2915 ")"; // )
2916
2917 // Compile regex
2918 const regular_expression regex(pattern, REG_EXTENDED);
2919
2920 // Parse buffer
2921 const char * errmsg = 0;
2922 int i = -1, state = 0, lineno = 1;
2923 for (const char * line = buffer; *line; line = nextline(line, lineno)) {
2924 // Match line
2925 if (!(line[0] == 'R' || line[0] == '=' || line[0] == ' '))
2926 continue;
2927 const int nmatch = 1+11;
2928 regmatch_t match[nmatch];
2929 if (!regex.execute(line, nmatch, match))
2930 continue;
2931
2932 char cmdname[40];
2933 if (matchcpy(cmdname, sizeof(cmdname), line, match[2])) { // "REPORT-IOCTL:... Command=%s ..."
2934 int nc = name2command(cmdname);
2935 if (nc < 0) {
2936 errmsg = "Unknown ATA command name"; break;
2937 }
2938 if (match[7].rm_so < 0) { // "returned %d"
2939 // Start of command
2940 if (!(state == 0 || state == 2)) {
2941 errmsg = "Missing REPORT-IOCTL result"; break;
2942 }
2943 if (++i >= max_num_commands) {
2944 errmsg = "Too many ATA commands"; break;
2945 }
2946 m_command_table[i].command = (smart_command_set)nc;
2947 m_command_table[i].select = matchtoi(line, match[5], 0); // "InputParameter=%d"
2948 state = 1;
2949 }
2950 else {
2951 // End of command
2952 if (!(state == 1 && (int)m_command_table[i].command == nc)) {
2953 errmsg = "Missing REPORT-IOCTL start"; break;
2954 }
2955 m_command_table[i].retval = matchtoi(line, match[7], -1); // "returned %d"
2956 m_command_table[i].errval = matchtoi(line, match[9], 0); // "errno=%d"
2957 state = 2;
2958 }
2959 }
2960 else if (matchcpy(cmdname, sizeof(cmdname), line, match[10])) { // "===== [%s] DATA START "
2961 // Start of sector hexdump
2962 int nc = name2command(cmdname);
2963 if (!(state == (nc == WRITE_LOG ? 1 : 2) && (int)m_command_table[i].command == nc)) {
2964 errmsg = "Unexpected DATA START"; break;
2965 }
2966 line = nextline(line, lineno);
2967 char * data = (char *)malloc(512);
2968 unsigned j;
2969 for (j = 0; j < 32; j++) {
2970 unsigned b[16];
2971 unsigned u1, u2; int n1 = -1;
2972 if (!(sscanf(line, "%3u-%3u: "
2973 "%2x %2x %2x %2x %2x %2x %2x %2x "
2974 "%2x %2x %2x %2x %2x %2x %2x %2x%n",
2975 &u1, &u2,
2976 b+ 0, b+ 1, b+ 2, b+ 3, b+ 4, b+ 5, b+ 6, b+ 7,
2977 b+ 8, b+ 9, b+10, b+11, b+12, b+13, b+14, b+15, &n1) == 18
2978 && n1 >= 56 && u1 == j*16 && u2 == j*16+15))
2979 break;
2980 for (unsigned k = 0; k < 16; k++)
2981 data[j*16+k] = b[k];
2982 line = nextline(line, lineno);
2983 }
2984 if (j < 32) {
2985 free(data);
2986 errmsg = "Incomplete sector hex dump"; break;
2987 }
2988 m_command_table[i].data = data;
2989 if (nc != WRITE_LOG)
2990 state = 0;
2991 }
2992 else if (match[11].rm_so > 0) { // "(En|Dis)abled status cached by OS"
2993 m_ata_identify_is_cached = true;
2994 }
2995 }
2996
2997 if (!(state == 0 || state == 2))
2998 errmsg = "Missing REPORT-IOCTL result";
2999
3000 if (!errmsg && i < 0)
3001 errmsg = "No information found";
3002
3003 m_num_commands = i+1;
3004 m_next_replay_command = 0;
3005 m_replay_out_of_sync = false;
3006
3007 if (errmsg) {
3008 close();
3009 return set_err(EIO, "%s(%d): Syntax error: %s", pathname, lineno, errmsg);
3010 }
3011 return true;
3012 }
3013
3014 // Report warnings and free command table
3015 bool parsed_ata_device::close()
3016 {
3017 if (m_replay_out_of_sync)
3018 pout("REPLAY-IOCTL: Warning: commands replayed out of sync\n");
3019 else if (m_next_replay_command != 0)
3020 pout("REPLAY-IOCTL: Warning: %d command(s) not replayed\n", m_num_commands-m_next_replay_command);
3021
3022 for (int i = 0; i < m_num_commands; i++) {
3023 if (m_command_table[i].data) {
3024 free(m_command_table[i].data); m_command_table[i].data = 0;
3025 }
3026 }
3027 m_num_commands = 0;
3028 m_next_replay_command = 0;
3029 m_replay_out_of_sync = false;
3030 return true;
3031 }
3032
3033
3034 bool parsed_ata_device::ata_identify_is_cached() const
3035 {
3036 return m_ata_identify_is_cached;
3037 }
3038
3039
3040 // Simulate ATA command from command table
3041 int parsed_ata_device::ata_command_interface(smart_command_set command, int select, char * data)
3042 {
3043 // Find command, try round-robin if out of sync
3044 int i = m_next_replay_command;
3045 for (int j = 0; ; j++) {
3046 if (j >= m_num_commands) {
3047 pout("REPLAY-IOCTL: Warning: Command not found\n");
3048 errno = ENOSYS;
3049 return -1;
3050 }
3051 if (m_command_table[i].command == command && m_command_table[i].select == select)
3052 break;
3053 if (!m_replay_out_of_sync) {
3054 m_replay_out_of_sync = true;
3055 pout("REPLAY-IOCTL: Warning: Command #%d is out of sync\n", i+1);
3056 }
3057 if (++i >= m_num_commands)
3058 i = 0;
3059 }
3060 m_next_replay_command = i;
3061 if (++m_next_replay_command >= m_num_commands)
3062 m_next_replay_command = 0;
3063
3064 // Return command data
3065 switch (command) {
3066 case IDENTIFY:
3067 case PIDENTIFY:
3068 case READ_VALUES:
3069 case READ_THRESHOLDS:
3070 case READ_LOG:
3071 if (m_command_table[i].data)
3072 memcpy(data, m_command_table[i].data, 512);
3073 break;
3074 case WRITE_LOG:
3075 if (!(m_command_table[i].data && !memcmp(data, m_command_table[i].data, 512)))
3076 pout("REPLAY-IOCTL: Warning: WRITE LOG data does not match\n");
3077 break;
3078 case CHECK_POWER_MODE:
3079 data[0] = (char)0xff;
3080 default:
3081 break;
3082 }
3083
3084 if (m_command_table[i].errval)
3085 errno = m_command_table[i].errval;
3086 return m_command_table[i].retval;
3087 }
3088
3089 } // namespace
3090
3091 ata_device * get_parsed_ata_device(smart_interface * intf, const char * dev_name)
3092 {
3093 return new parsed_ata_device(intf, dev_name);
3094 }