smartmontools  SVN Rev 4430
Utility to control and monitor storage systems with "S.M.A.R.T."
ataprint.cpp
Go to the documentation of this file.
1 /*
2  * ataprint.cpp
3  *
4  * Home page of code is: http://www.smartmontools.org
5  *
6  * Copyright (C) 2002-11 Bruce Allen
7  * Copyright (C) 2008-17 Christian Franke
8  * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * You should have received a copy of the GNU General Public License
16  * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
17  *
18  * This code was originally developed as a Senior Thesis by Michael Cornwell
19  * at the Concurrent Systems Laboratory (now part of the Storage Systems
20  * Research Center), Jack Baskin School of Engineering, University of
21  * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
22  *
23  */
24 
25 #include "config.h"
26 
27 #include <ctype.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "int64.h"
34 #include "atacmdnames.h"
35 #include "atacmds.h"
36 #include "ataidentify.h"
37 #include "dev_interface.h"
38 #include "ataprint.h"
39 #include "smartctl.h"
40 #include "utility.h"
41 #include "knowndrives.h"
42 
43 const char * ataprint_cpp_cvsid = "$Id: ataprint.cpp 4419 2017-04-17 13:20:31Z chrfranke $"
45 
46 
47 static const char * infofound(const char *output) {
48  return (*output ? output : "[No Information Found]");
49 }
50 
51 // Return true if '-T permissive' is specified,
52 // used to ignore missing capabilities
53 static bool is_permissive()
54 {
56  return false;
58  return true;
59 }
60 
61 /* For the given Command Register (CR) and Features Register (FR), attempts
62  * to construct a string that describes the contents of the Status
63  * Register (ST) and Error Register (ER). If the meanings of the flags of
64  * the error register are not known for the given command then it returns an
65  * empty string.
66  *
67  * The meanings of the flags of the error register for all commands are
68  * described in the ATA spec and could all be supported here in theory.
69  * Currently, only a few commands are supported (those that have been seen
70  * to produce errors). If many more are to be added then this function
71  * should probably be redesigned.
72  */
73 
74 static std::string format_st_er_desc(
75  unsigned char CR, unsigned char FR,
76  unsigned char ST, unsigned char ER,
77  unsigned short SC,
78  const ata_smart_errorlog_error_struct * lba28_regs,
79  const ata_smart_exterrlog_error * lba48_regs
80 )
81 {
82  const char *error_flag[8];
83  int i, print_lba=0, print_sector=0;
84 
85  // Set of character strings corresponding to different error codes.
86  // Please keep in alphabetic order if you add more.
87  const char *abrt = "ABRT"; // ABORTED
88  const char *amnf = "AMNF"; // ADDRESS MARK NOT FOUND
89  const char *ccto = "CCTO"; // COMMAND COMPLETION TIMED OUT
90  const char *eom = "EOM"; // END OF MEDIA
91  const char *icrc = "ICRC"; // INTERFACE CRC ERROR
92  const char *idnf = "IDNF"; // ID NOT FOUND
93  const char *ili = "ILI"; // MEANING OF THIS BIT IS COMMAND-SET SPECIFIC
94  const char *mc = "MC"; // MEDIA CHANGED
95  const char *mcr = "MCR"; // MEDIA CHANGE REQUEST
96  const char *nm = "NM"; // NO MEDIA
97  const char *obs = "obs"; // OBSOLETE
98  const char *tk0nf = "TK0NF"; // TRACK 0 NOT FOUND
99  const char *unc = "UNC"; // UNCORRECTABLE
100  const char *wp = "WP"; // WRITE PROTECTED
101 
102  /* If for any command the Device Fault flag of the status register is
103  * not used then used_device_fault should be set to 0 (in the CR switch
104  * below)
105  */
106  int uses_device_fault = 1;
107 
108  /* A value of NULL means that the error flag isn't used */
109  for (i = 0; i < 8; i++)
110  error_flag[i] = NULL;
111 
112  std::string str;
113 
114  switch (CR) {
115  case 0x10: // RECALIBRATE
116  error_flag[2] = abrt;
117  error_flag[1] = tk0nf;
118  break;
119  case 0x20: /* READ SECTOR(S) */
120  case 0x21: // READ SECTOR(S)
121  case 0x24: // READ SECTOR(S) EXT
122  case 0xC4: /* READ MULTIPLE */
123  case 0x29: // READ MULTIPLE EXT
124  error_flag[6] = unc;
125  error_flag[5] = mc;
126  error_flag[4] = idnf;
127  error_flag[3] = mcr;
128  error_flag[2] = abrt;
129  error_flag[1] = nm;
130  error_flag[0] = amnf;
131  print_lba=1;
132  break;
133  case 0x22: // READ LONG (with retries)
134  case 0x23: // READ LONG (without retries)
135  error_flag[4] = idnf;
136  error_flag[2] = abrt;
137  error_flag[0] = amnf;
138  print_lba=1;
139  break;
140  case 0x2a: // READ STREAM DMA
141  case 0x2b: // READ STREAM PIO
142  if (CR==0x2a)
143  error_flag[7] = icrc;
144  error_flag[6] = unc;
145  error_flag[5] = mc;
146  error_flag[4] = idnf;
147  error_flag[3] = mcr;
148  error_flag[2] = abrt;
149  error_flag[1] = nm;
150  error_flag[0] = ccto;
151  print_lba=1;
152  print_sector=SC;
153  break;
154  case 0x3A: // WRITE STREAM DMA
155  case 0x3B: // WRITE STREAM PIO
156  if (CR==0x3A)
157  error_flag[7] = icrc;
158  error_flag[6] = wp;
159  error_flag[5] = mc;
160  error_flag[4] = idnf;
161  error_flag[3] = mcr;
162  error_flag[2] = abrt;
163  error_flag[1] = nm;
164  error_flag[0] = ccto;
165  print_lba=1;
166  print_sector=SC;
167  break;
168  case 0x25: // READ DMA EXT
169  case 0x26: // READ DMA QUEUED EXT
170  case 0xC7: // READ DMA QUEUED
171  case 0xC8: // READ DMA (with retries)
172  case 0xC9: // READ DMA (without retries, obsolete since ATA-5)
173  case 0x60: // READ FPDMA QUEUED (NCQ)
174  error_flag[7] = icrc;
175  error_flag[6] = unc;
176  error_flag[5] = mc;
177  error_flag[4] = idnf;
178  error_flag[3] = mcr;
179  error_flag[2] = abrt;
180  error_flag[1] = nm;
181  error_flag[0] = amnf;
182  print_lba=1;
183  if (CR==0x25 || CR==0xC8)
184  print_sector=SC;
185  break;
186  case 0x30: /* WRITE SECTOR(S) */
187  case 0x31: // WRITE SECTOR(S)
188  case 0x34: // WRITE SECTOR(S) EXT
189  case 0xC5: /* WRITE MULTIPLE */
190  case 0x39: // WRITE MULTIPLE EXT
191  case 0xCE: // WRITE MULTIPLE FUA EXT
192  error_flag[6] = wp;
193  error_flag[5] = mc;
194  error_flag[4] = idnf;
195  error_flag[3] = mcr;
196  error_flag[2] = abrt;
197  error_flag[1] = nm;
198  print_lba=1;
199  break;
200  case 0x32: // WRITE LONG (with retries)
201  case 0x33: // WRITE LONG (without retries)
202  error_flag[4] = idnf;
203  error_flag[2] = abrt;
204  print_lba=1;
205  break;
206  case 0x3C: // WRITE VERIFY
207  error_flag[6] = unc;
208  error_flag[4] = idnf;
209  error_flag[2] = abrt;
210  error_flag[0] = amnf;
211  print_lba=1;
212  break;
213  case 0x40: // READ VERIFY SECTOR(S) with retries
214  case 0x41: // READ VERIFY SECTOR(S) without retries
215  case 0x42: // READ VERIFY SECTOR(S) EXT
216  error_flag[6] = unc;
217  error_flag[5] = mc;
218  error_flag[4] = idnf;
219  error_flag[3] = mcr;
220  error_flag[2] = abrt;
221  error_flag[1] = nm;
222  error_flag[0] = amnf;
223  print_lba=1;
224  break;
225  case 0xA0: /* PACKET */
226  /* Bits 4-7 are all used for sense key (a 'command packet set specific error
227  * indication' according to the ATA/ATAPI-7 standard), so "Sense key" will
228  * be repeated in the error description string if more than one of those
229  * bits is set.
230  */
231  error_flag[7] = "Sense key (bit 3)",
232  error_flag[6] = "Sense key (bit 2)",
233  error_flag[5] = "Sense key (bit 1)",
234  error_flag[4] = "Sense key (bit 0)",
235  error_flag[2] = abrt;
236  error_flag[1] = eom;
237  error_flag[0] = ili;
238  break;
239  case 0xA1: /* IDENTIFY PACKET DEVICE */
240  case 0xEF: /* SET FEATURES */
241  case 0x00: /* NOP */
242  case 0xC6: /* SET MULTIPLE MODE */
243  error_flag[2] = abrt;
244  break;
245  case 0x2F: // READ LOG EXT
246  error_flag[6] = unc;
247  error_flag[4] = idnf;
248  error_flag[2] = abrt;
249  error_flag[0] = obs;
250  break;
251  case 0x3F: // WRITE LOG EXT
252  error_flag[4] = idnf;
253  error_flag[2] = abrt;
254  error_flag[0] = obs;
255  break;
256  case 0xB0: /* SMART */
257  switch(FR) {
258  case 0xD0: // SMART READ DATA
259  case 0xD1: // SMART READ ATTRIBUTE THRESHOLDS
260  case 0xD5: /* SMART READ LOG */
261  error_flag[6] = unc;
262  error_flag[4] = idnf;
263  error_flag[2] = abrt;
264  error_flag[0] = obs;
265  break;
266  case 0xD6: /* SMART WRITE LOG */
267  error_flag[4] = idnf;
268  error_flag[2] = abrt;
269  error_flag[0] = obs;
270  break;
271  case 0xD2: // Enable/Disable Attribute Autosave
272  case 0xD3: // SMART SAVE ATTRIBUTE VALUES (ATA-3)
273  case 0xD8: // SMART ENABLE OPERATIONS
274  case 0xD9: /* SMART DISABLE OPERATIONS */
275  case 0xDA: /* SMART RETURN STATUS */
276  case 0xDB: // Enable/Disable Auto Offline (SFF)
277  error_flag[2] = abrt;
278  break;
279  case 0xD4: // SMART EXECUTE IMMEDIATE OFFLINE
280  error_flag[4] = idnf;
281  error_flag[2] = abrt;
282  break;
283  default:
284  return str; // ""
285  break;
286  }
287  break;
288  case 0xB1: /* DEVICE CONFIGURATION */
289  switch (FR) {
290  case 0xC0: /* DEVICE CONFIGURATION RESTORE */
291  error_flag[2] = abrt;
292  break;
293  default:
294  return str; // ""
295  break;
296  }
297  break;
298  case 0xCA: // WRITE DMA (with retries)
299  case 0xCB: // WRITE DMA (without retries, obsolete since ATA-5)
300  case 0x35: // WRITE DMA EXT
301  case 0x3D: // WRITE DMA FUA EXT
302  case 0xCC: // WRITE DMA QUEUED
303  case 0x36: // WRITE DMA QUEUED EXT
304  case 0x3E: // WRITE DMA QUEUED FUA EXT
305  case 0x61: // WRITE FPDMA QUEUED (NCQ)
306  error_flag[7] = icrc;
307  error_flag[6] = wp;
308  error_flag[5] = mc;
309  error_flag[4] = idnf;
310  error_flag[3] = mcr;
311  error_flag[2] = abrt;
312  error_flag[1] = nm;
313  error_flag[0] = amnf;
314  print_lba=1;
315  if (CR==0x35)
316  print_sector=SC;
317  break;
318  case 0xE4: // READ BUFFER
319  case 0xE8: // WRITE BUFFER
320  error_flag[2] = abrt;
321  break;
322  default:
323  return str; // ""
324  }
325 
326  /* We ignore any status flags other than Device Fault and Error */
327 
328  if (uses_device_fault && (ST & (1 << 5))) {
329  str = "Device Fault";
330  if (ST & 1) // Error flag
331  str += "; ";
332  }
333  if (ST & 1) { // Error flag
334  int count = 0;
335 
336  str += "Error: ";
337  for (i = 7; i >= 0; i--)
338  if ((ER & (1 << i)) && (error_flag[i])) {
339  if (count++ > 0)
340  str += ", ";
341  str += error_flag[i];
342  }
343  }
344 
345  // If the error was a READ or WRITE error, print the Logical Block
346  // Address (LBA) at which the read or write failed.
347  if (print_lba) {
348  // print number of sectors, if known, and append to print string
349  if (print_sector)
350  str += strprintf(" %d sectors", print_sector);
351 
352  if (lba28_regs) {
353  unsigned lba;
354  // bits 24-27: bits 0-3 of DH
355  lba = 0xf & lba28_regs->drive_head;
356  lba <<= 8;
357  // bits 16-23: CH
358  lba |= lba28_regs->cylinder_high;
359  lba <<= 8;
360  // bits 8-15: CL
361  lba |= lba28_regs->cylinder_low;
362  lba <<= 8;
363  // bits 0-7: SN
364  lba |= lba28_regs->sector_number;
365  str += strprintf(" at LBA = 0x%08x = %u", lba, lba);
366  }
367  else if (lba48_regs) {
368  // This assumes that upper LBA registers are 0 for 28-bit commands
369  // (TODO: detect 48-bit commands above)
370  uint64_t lba48;
371  lba48 = lba48_regs->lba_high_register_hi;
372  lba48 <<= 8;
373  lba48 |= lba48_regs->lba_mid_register_hi;
374  lba48 <<= 8;
375  lba48 |= lba48_regs->lba_low_register_hi;
376  lba48 |= lba48_regs->device_register & 0xf;
377  lba48 <<= 8;
378  lba48 |= lba48_regs->lba_high_register;
379  lba48 <<= 8;
380  lba48 |= lba48_regs->lba_mid_register;
381  lba48 <<= 8;
382  lba48 |= lba48_regs->lba_low_register;
383  str += strprintf(" at LBA = 0x%08" PRIx64 " = %" PRIu64, lba48, lba48);
384  }
385  }
386 
387  return str;
388 }
389 
390 static inline std::string format_st_er_desc(
392 {
393  return format_st_er_desc(
394  data->commands[4].commandreg,
395  data->commands[4].featuresreg,
396  data->error_struct.status,
399  &data->error_struct, (const ata_smart_exterrlog_error *)0);
400 }
401 
402 static inline std::string format_st_er_desc(
404 {
405  return format_st_er_desc(
406  data->commands[4].command_register,
407  data->commands[4].features_register,
408  data->error.status_register,
409  data->error.error_register,
410  data->error.count_register_hi << 8 | data->error.count_register,
411  (const ata_smart_errorlog_error_struct *)0, &data->error);
412 }
413 
414 
415 static const char * get_form_factor(unsigned short word168)
416 {
417  // Table A.32 of T13/2161-D (ACS-3) Revision 4p, September 19, 2013
418  // Table 236 of T13/BSR INCITS 529 (ACS-4) Revision 04, August 25, 2014
419  switch (word168) {
420  case 0x1: return "5.25 inches";
421  case 0x2: return "3.5 inches";
422  case 0x3: return "2.5 inches";
423  case 0x4: return "1.8 inches";
424  case 0x5: return "< 1.8 inches";
425  case 0x6: return "mSATA"; // ACS-4
426  case 0x7: return "M.2"; // ACS-4
427  case 0x8: return "MicroSSD"; // ACS-4
428  case 0x9: return "CFast"; // ACS-4
429  default : return 0;
430  }
431 }
432 
433 static int find_msb(unsigned short word)
434 {
435  for (int bit = 15; bit >= 0; bit--)
436  if (word & (1 << bit))
437  return bit;
438  return -1;
439 }
440 
441 static const char * get_ata_major_version(const ata_identify_device * drive)
442 {
443  // Table 13 of T13/1153D (ATA/ATAPI-4) revision 18, August 19, 1998
444  // Table 48 of T13/BSR INCITS 529 (ACS-4) Revision 16, February 21, 2017
445  switch (find_msb(drive->major_rev_num)) {
446  case 14: return "ACS >4 (14)";
447  case 13: return "ACS >4 (13)";
448  case 12: return "ACS >4 (12)";
449  case 11: return "ACS-4";
450  case 10: return "ACS-3";
451  case 9: return "ACS-2";
452  case 8: return "ATA8-ACS";
453  case 7: return "ATA/ATAPI-7";
454  case 6: return "ATA/ATAPI-6";
455  case 5: return "ATA/ATAPI-5";
456  case 4: return "ATA/ATAPI-4";
457  case 3: return "ATA-3";
458  case 2: return "ATA-2";
459  case 1: return "ATA-1";
460  default: return 0;
461  }
462 }
463 
464 static const char * get_ata_minor_version(const ata_identify_device * drive)
465 {
466  // Table 10 of X3T13/2008D (ATA-3) Revision 7b, January 27, 1997
467  // Table 28 of T13/1410D (ATA/ATAPI-6) Revision 3b, February 26, 2002
468  // Table 31 of T13/1699-D (ATA8-ACS) Revision 6a, September 6, 2008
469  // Table 46 of T13/BSR INCITS 529 (ACS-4) Revision 08, April 28, 2015
470  switch (drive->minor_rev_num) {
471  case 0x0001: return "ATA-1 X3T9.2/781D prior to revision 4";
472  case 0x0002: return "ATA-1 published, ANSI X3.221-1994";
473  case 0x0003: return "ATA-1 X3T9.2/781D revision 4";
474  case 0x0004: return "ATA-2 published, ANSI X3.279-1996";
475  case 0x0005: return "ATA-2 X3T10/948D prior to revision 2k";
476  case 0x0006: return "ATA-3 X3T10/2008D revision 1";
477  case 0x0007: return "ATA-2 X3T10/948D revision 2k";
478  case 0x0008: return "ATA-3 X3T10/2008D revision 0";
479  case 0x0009: return "ATA-2 X3T10/948D revision 3";
480  case 0x000a: return "ATA-3 published, ANSI X3.298-1997";
481  case 0x000b: return "ATA-3 X3T10/2008D revision 6"; // 1st ATA-3 revision with SMART
482  case 0x000c: return "ATA-3 X3T13/2008D revision 7 and 7a";
483  case 0x000d: return "ATA/ATAPI-4 X3T13/1153D revision 6";
484  case 0x000e: return "ATA/ATAPI-4 T13/1153D revision 13";
485  case 0x000f: return "ATA/ATAPI-4 X3T13/1153D revision 7";
486  case 0x0010: return "ATA/ATAPI-4 T13/1153D revision 18";
487  case 0x0011: return "ATA/ATAPI-4 T13/1153D revision 15";
488  case 0x0012: return "ATA/ATAPI-4 published, ANSI NCITS 317-1998";
489  case 0x0013: return "ATA/ATAPI-5 T13/1321D revision 3";
490  case 0x0014: return "ATA/ATAPI-4 T13/1153D revision 14";
491  case 0x0015: return "ATA/ATAPI-5 T13/1321D revision 1";
492  case 0x0016: return "ATA/ATAPI-5 published, ANSI NCITS 340-2000";
493  case 0x0017: return "ATA/ATAPI-4 T13/1153D revision 17";
494  case 0x0018: return "ATA/ATAPI-6 T13/1410D revision 0";
495  case 0x0019: return "ATA/ATAPI-6 T13/1410D revision 3a";
496  case 0x001a: return "ATA/ATAPI-7 T13/1532D revision 1";
497  case 0x001b: return "ATA/ATAPI-6 T13/1410D revision 2";
498  case 0x001c: return "ATA/ATAPI-6 T13/1410D revision 1";
499  case 0x001d: return "ATA/ATAPI-7 published, ANSI INCITS 397-2005";
500  case 0x001e: return "ATA/ATAPI-7 T13/1532D revision 0";
501  case 0x001f: return "ACS-3 T13/2161-D revision 3b";
502 
503  case 0x0021: return "ATA/ATAPI-7 T13/1532D revision 4a";
504  case 0x0022: return "ATA/ATAPI-6 published, ANSI INCITS 361-2002";
505 
506  case 0x0027: return "ATA8-ACS T13/1699-D revision 3c";
507  case 0x0028: return "ATA8-ACS T13/1699-D revision 6";
508  case 0x0029: return "ATA8-ACS T13/1699-D revision 4";
509 
510  case 0x0031: return "ACS-2 T13/2015-D revision 2";
511 
512  case 0x0033: return "ATA8-ACS T13/1699-D revision 3e";
513 
514  case 0x0039: return "ATA8-ACS T13/1699-D revision 4c";
515 
516  case 0x0042: return "ATA8-ACS T13/1699-D revision 3f";
517 
518  case 0x0052: return "ATA8-ACS T13/1699-D revision 3b";
519 
520  case 0x005e: return "ACS-4 T13/BSR INCITS 529 revision 5";
521 
522  case 0x006d: return "ACS-3 T13/2161-D revision 5";
523 
524  case 0x0082: return "ACS-2 published, ANSI INCITS 482-2012";
525 
526  case 0x0107: return "ATA8-ACS T13/1699-D revision 2d";
527 
528  case 0x010a: return "ACS-3 published, ANSI INCITS 522-2014";
529 
530  case 0x0110: return "ACS-2 T13/2015-D revision 3";
531 
532  case 0x011b: return "ACS-3 T13/2161-D revision 4";
533 
534  default: return 0;
535  }
536 }
537 
538 static const char * get_pata_version(unsigned short word222, char (& buf)[32])
539 {
540  switch (word222 & 0x0fff) {
541  default: snprintf(buf, sizeof(buf),
542  "Unknown (0x%03x)", word222 & 0x0fff); return buf;
543  case 0x001:
544  case 0x003: return "ATA8-APT";
545  case 0x002: return "ATA/ATAPI-7";
546  }
547 }
548 
549 static const char * get_sata_version(unsigned short word222)
550 {
551  switch (find_msb(word222 & 0x0fff)) {
552  case 11: return "SATA >3.3 (11)";
553  case 10: return "SATA >3.3 (10)";
554  case 9: return "SATA >3.3 (9)";
555  case 8: return "SATA 3.3";
556  case 7: return "SATA 3.2";
557  case 6: return "SATA 3.1";
558  case 5: return "SATA 3.0";
559  case 4: return "SATA 2.6";
560  case 3: return "SATA 2.5";
561  case 2: return "SATA II Ext";
562  case 1: return "SATA 1.0a";
563  case 0: return "ATA8-AST";
564  default: return "Unknown";
565  }
566 }
567 
568 static const char * get_sata_speed(int level)
569 {
570  if (level <= 0)
571  return 0;
572  switch (level) {
573  default: return ">6.0 Gb/s (7)";
574  case 6: return ">6.0 Gb/s (6)";
575  case 5: return ">6.0 Gb/s (5)";
576  case 4: return ">6.0 Gb/s (4)";
577  case 3: return "6.0 Gb/s";
578  case 2: return "3.0 Gb/s";
579  case 1: return "1.5 Gb/s";
580  }
581 }
582 
583 static const char * get_sata_maxspeed(const ata_identify_device * drive)
584 {
585  unsigned short word076 = drive->words047_079[76-47];
586  if (word076 & 0x0001)
587  return 0;
588  return get_sata_speed(find_msb(word076 & 0x00fe));
589 }
590 
591 static const char * get_sata_curspeed(const ata_identify_device * drive)
592 {
593  unsigned short word077 = drive->words047_079[77-47];
594  if (word077 & 0x0001)
595  return 0;
596  return get_sata_speed((word077 >> 1) & 0x7);
597 }
598 
599 
600 static void print_drive_info(const ata_identify_device * drive,
601  const ata_size_info & sizes, int rpm,
602  const drive_settings * dbentry)
603 {
604  // format drive information (with byte swapping as needed)
605  char model[40+1], serial[20+1], firmware[8+1];
606  ata_format_id_string(model, drive->model, sizeof(model)-1);
607  ata_format_id_string(serial, drive->serial_no, sizeof(serial)-1);
608  ata_format_id_string(firmware, drive->fw_rev, sizeof(firmware)-1);
609 
610  // Print model family if known
611  if (dbentry && *dbentry->modelfamily)
612  pout("Model Family: %s\n", dbentry->modelfamily);
613 
614  pout("Device Model: %s\n", infofound(model));
615 
617  pout("Serial Number: %s\n", infofound(serial));
618 
619  unsigned oui = 0; uint64_t unique_id = 0;
620  int naa = ata_get_wwn(drive, oui, unique_id);
621  if (naa >= 0)
622  pout("LU WWN Device Id: %x %06x %09" PRIx64 "\n", naa, oui, unique_id);
623  }
624 
625  // Additional Product Identifier (OEM Id) string in words 170-173
626  // (e08130r1, added in ACS-2 Revision 1, December 17, 2008)
627  if (0x2020 <= drive->words088_255[170-88] && drive->words088_255[170-88] <= 0x7e7e) {
628  char add[8+1];
629  ata_format_id_string(add, (const unsigned char *)(drive->words088_255+170-88), sizeof(add)-1);
630  if (add[0])
631  pout("Add. Product Id: %s\n", add);
632  }
633 
634  pout("Firmware Version: %s\n", infofound(firmware));
635 
636  if (sizes.capacity) {
637  // Print capacity
638  char num[64], cap[32];
639  pout("User Capacity: %s bytes [%s]\n",
640  format_with_thousands_sep(num, sizeof(num), sizes.capacity),
641  format_capacity(cap, sizeof(cap), sizes.capacity));
642 
643  // Print sector sizes.
644  if (sizes.phy_sector_size == sizes.log_sector_size)
645  pout("Sector Size: %u bytes logical/physical\n", sizes.log_sector_size);
646  else {
647  pout("Sector Sizes: %u bytes logical, %u bytes physical",
648  sizes.log_sector_size, sizes.phy_sector_size);
649  if (sizes.log_sector_offset)
650  pout(" (offset %u bytes)", sizes.log_sector_offset);
651  pout("\n");
652  }
653  }
654 
655  // Print nominal media rotation rate if reported
656  if (rpm) {
657  if (rpm == 1)
658  pout("Rotation Rate: Solid State Device\n");
659  else if (rpm > 1)
660  pout("Rotation Rate: %d rpm\n", rpm);
661  else
662  pout("Rotation Rate: Unknown (0x%04x)\n", -rpm);
663  }
664 
665  // Print form factor if reported
666  unsigned short word168 = drive->words088_255[168-88];
667  if (word168) {
668  const char * form_factor = get_form_factor(word168);
669  if (form_factor)
670  pout("Form Factor: %s\n", form_factor);
671  else
672  pout("Form Factor: Unknown (0x%04x)\n", word168);
673  }
674 
675  // See if drive is recognized
676  pout("Device is: %s\n", !dbentry ?
677  "Not in smartctl database [for details use: -P showall]":
678  "In smartctl database [for details use: -P show]");
679 
680  // Print ATA version
681  std::string ataver;
682  if ( (drive->major_rev_num != 0x0000 && drive->major_rev_num != 0xffff)
683  || (drive->minor_rev_num != 0x0000 && drive->minor_rev_num != 0xffff)) {
684  const char * majorver = get_ata_major_version(drive);
685  const char * minorver = get_ata_minor_version(drive);
686 
687  if (majorver && minorver && str_starts_with(minorver, majorver)) {
688  // Major and minor strings match, print minor string only
689  ataver = minorver;
690  }
691  else {
692  if (majorver)
693  ataver = majorver;
694  else
695  ataver = strprintf("Unknown(0x%04x)", drive->major_rev_num);
696 
697  if (minorver)
698  ataver += strprintf(", %s", minorver);
699  else if (drive->minor_rev_num != 0x0000 && drive->minor_rev_num != 0xffff)
700  ataver += strprintf(" (unknown minor revision code: 0x%04x)", drive->minor_rev_num);
701  else
702  ataver += " (minor revision not indicated)";
703  }
704  }
705  pout("ATA Version is: %s\n", infofound(ataver.c_str()));
706 
707  // Print Transport specific version
708  unsigned short word222 = drive->words088_255[222-88];
709  if (word222 != 0x0000 && word222 != 0xffff) switch (word222 >> 12) {
710  case 0x0: // PATA
711  {
712  char buf[32] = "";
713  pout("Transport Type: Parallel, %s\n", get_pata_version(word222, buf));
714  }
715  break;
716  case 0x1: // SATA
717  {
718  const char * sataver = get_sata_version(word222);
719  const char * maxspeed = get_sata_maxspeed(drive);
720  const char * curspeed = get_sata_curspeed(drive);
721  pout("SATA Version is: %s%s%s%s%s%s\n", sataver,
722  (maxspeed ? ", " : ""), (maxspeed ? maxspeed : ""),
723  (curspeed ? " (current: " : ""), (curspeed ? curspeed : ""),
724  (curspeed ? ")" : ""));
725  }
726  break;
727  case 0xe: // PCIe (ACS-4)
728  pout("Transport Type: PCIe (0x%03x)\n", word222 & 0x0fff);
729  break;
730  default:
731  pout("Transport Type: Unknown (0x%04x)\n", word222);
732  break;
733  }
734 
735  // print current time and date and timezone
736  char timedatetz[DATEANDEPOCHLEN]; dateandtimezone(timedatetz);
737  pout("Local Time is: %s\n", timedatetz);
738 
739  // Print warning message, if there is one
740  if (dbentry && *dbentry->warningmsg)
741  pout("\n==> WARNING: %s\n\n", dbentry->warningmsg);
742 }
743 
744 static const char *OfflineDataCollectionStatus(unsigned char status_byte)
745 {
746  unsigned char stat=status_byte & 0x7f;
747 
748  switch(stat){
749  case 0x00:
750  return "was never started";
751  case 0x02:
752  return "was completed without error";
753  case 0x03:
754  if (status_byte == 0x03)
755  return "is in progress";
756  else
757  return "is in a Reserved state";
758  case 0x04:
759  return "was suspended by an interrupting command from host";
760  case 0x05:
761  return "was aborted by an interrupting command from host";
762  case 0x06:
763  return "was aborted by the device with a fatal error";
764  default:
765  if (stat >= 0x40)
766  return "is in a Vendor Specific state";
767  else
768  return "is in a Reserved state";
769  }
770 }
771 
772 
773 // prints verbose value Off-line data collection status byte
775 {
776  pout("Offline data collection status: (0x%02x)\t",
777  (int)data->offline_data_collection_status);
778 
779  // Off-line data collection status byte is not a reserved
780  // or vendor specific value
781  pout("Offline data collection activity\n"
783 
784  // Report on Automatic Data Collection Status. Only IBM documents
785  // this bit. See SFF 8035i Revision 2 for details.
786  if (data->offline_data_collection_status & 0x80)
787  pout("\t\t\t\t\tAuto Offline Data Collection: Enabled.\n");
788  else
789  pout("\t\t\t\t\tAuto Offline Data Collection: Disabled.\n");
790 
791  return;
792 }
793 
795  firmwarebug_defs firmwarebugs)
796 {
797  pout("Self-test execution status: ");
798 
799  switch (data->self_test_exec_status >> 4)
800  {
801  case 0:
802  pout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t",
803  (int)data->self_test_exec_status);
804  pout("without error or no self-test has ever \n\t\t\t\t\tbeen run.\n");
805  break;
806  case 1:
807  pout("(%4d)\tThe self-test routine was aborted by\n\t\t\t\t\t",
808  (int)data->self_test_exec_status);
809  pout("the host.\n");
810  break;
811  case 2:
812  pout("(%4d)\tThe self-test routine was interrupted\n\t\t\t\t\t",
813  (int)data->self_test_exec_status);
814  pout("by the host with a hard or soft reset.\n");
815  break;
816  case 3:
817  pout("(%4d)\tA fatal error or unknown test error\n\t\t\t\t\t",
818  (int)data->self_test_exec_status);
819  pout("occurred while the device was executing\n\t\t\t\t\t");
820  pout("its self-test routine and the device \n\t\t\t\t\t");
821  pout("was unable to complete the self-test \n\t\t\t\t\t");
822  pout("routine.\n");
823  break;
824  case 4:
825  pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
826  (int)data->self_test_exec_status);
827  pout("a test element that failed and the test\n\t\t\t\t\t");
828  pout("element that failed is not known.\n");
829  break;
830  case 5:
831  pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
832  (int)data->self_test_exec_status);
833  pout("the electrical element of the test\n\t\t\t\t\t");
834  pout("failed.\n");
835  break;
836  case 6:
837  pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
838  (int)data->self_test_exec_status);
839  pout("the servo (and/or seek) element of the \n\t\t\t\t\t");
840  pout("test failed.\n");
841  break;
842  case 7:
843  pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
844  (int)data->self_test_exec_status);
845  pout("the read element of the test failed.\n");
846  break;
847  case 8:
848  pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
849  (int)data->self_test_exec_status);
850  pout("a test element that failed and the\n\t\t\t\t\t");
851  pout("device is suspected of having handling\n\t\t\t\t\t");
852  pout("damage.\n");
853  break;
854  case 15:
855  if (firmwarebugs.is_set(BUG_SAMSUNG3) && data->self_test_exec_status == 0xf0) {
856  pout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t",
857  (int)data->self_test_exec_status);
858  pout("with unknown result or self-test in\n\t\t\t\t\t");
859  pout("progress with less than 10%% remaining.\n");
860  }
861  else {
862  pout("(%4d)\tSelf-test routine in progress...\n\t\t\t\t\t",
863  (int)data->self_test_exec_status);
864  pout("%1d0%% of test remaining.\n",
865  (int)(data->self_test_exec_status & 0x0f));
866  }
867  break;
868  default:
869  pout("(%4d)\tReserved.\n",
870  (int)data->self_test_exec_status);
871  break;
872  }
873 
874 }
875 
877 {
878  pout("Total time to complete Offline \n");
879  pout("data collection: \t\t(%5d) seconds.\n",
881 }
882 
884 {
885  pout("Offline data collection\n");
886  pout("capabilities: \t\t\t (0x%02x) ",
888 
889  if (data->offline_data_collection_capability == 0x00){
890  pout("\tOffline data collection not supported.\n");
891  }
892  else {
893  pout( "%s\n", isSupportExecuteOfflineImmediate(data)?
894  "SMART execute Offline immediate." :
895  "No SMART execute Offline immediate.");
896 
897  pout( "\t\t\t\t\t%s\n", isSupportAutomaticTimer(data)?
898  "Auto Offline data collection on/off support.":
899  "No Auto Offline data collection support.");
900 
901  pout( "\t\t\t\t\t%s\n", isSupportOfflineAbort(data)?
902  "Abort Offline collection upon new\n\t\t\t\t\tcommand.":
903  "Suspend Offline collection upon new\n\t\t\t\t\tcommand.");
904 
905  pout( "\t\t\t\t\t%s\n", isSupportOfflineSurfaceScan(data)?
906  "Offline surface scan supported.":
907  "No Offline surface scan supported.");
908 
909  pout( "\t\t\t\t\t%s\n", isSupportSelfTest(data)?
910  "Self-test supported.":
911  "No Self-test supported.");
912 
913  pout( "\t\t\t\t\t%s\n", isSupportConveyanceSelfTest(data)?
914  "Conveyance Self-test supported.":
915  "No Conveyance Self-test supported.");
916 
917  pout( "\t\t\t\t\t%s\n", isSupportSelectiveSelfTest(data)?
918  "Selective Self-test supported.":
919  "No Selective Self-test supported.");
920  }
921 }
922 
924 {
925  pout("SMART capabilities: ");
926  pout("(0x%04x)\t", (int)data->smart_capability);
927 
928  if (data->smart_capability == 0x00)
929  {
930  pout("Automatic saving of SMART data\t\t\t\t\tis not implemented.\n");
931  }
932  else
933  {
934 
935  pout( "%s\n", (data->smart_capability & 0x01)?
936  "Saves SMART data before entering\n\t\t\t\t\tpower-saving mode.":
937  "Does not save SMART data before\n\t\t\t\t\tentering power-saving mode.");
938 
939  if ( data->smart_capability & 0x02 )
940  {
941  pout("\t\t\t\t\tSupports SMART auto save timer.\n");
942  }
943  }
944 }
945 
947 {
948  pout("Error logging capability: ");
949 
950  if ( isSmartErrorLogCapable(data, identity) )
951  {
952  pout(" (0x%02x)\tError logging supported.\n",
953  (int)data->errorlog_capability);
954  }
955  else {
956  pout(" (0x%02x)\tError logging NOT supported.\n",
957  (int)data->errorlog_capability);
958  }
959 }
960 
962 {
963  pout("Short self-test routine \n");
964  if (isSupportSelfTest(data))
965  pout("recommended polling time: \t (%4d) minutes.\n",
966  (int)data->short_test_completion_time);
967  else
968  pout("recommended polling time: \t Not Supported.\n");
969 }
970 
972 {
973  pout("Extended self-test routine\n");
974  if (isSupportSelfTest(data))
975  pout("recommended polling time: \t (%4d) minutes.\n",
976  TestTime(data, EXTEND_SELF_TEST));
977  else
978  pout("recommended polling time: \t Not Supported.\n");
979 }
980 
982 {
983  pout("Conveyance self-test routine\n");
984  if (isSupportConveyanceSelfTest(data))
985  pout("recommended polling time: \t (%4d) minutes.\n",
987  else
988  pout("recommended polling time: \t Not Supported.\n");
989 }
990 
991 // Check SMART attribute table for Threshold failure
992 // onlyfailed=0: are or were any age or prefailure attributes <= threshold
993 // onlyfailed=1: are any prefailure attributes <= threshold now
995  const ata_smart_thresholds_pvt * thresholds,
996  const ata_vendor_attr_defs & defs, int onlyfailed)
997 {
998  for (int i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
999  const ata_smart_attribute & attr = data->vendor_attributes[i];
1000 
1001  ata_attr_state state = ata_get_attr_state(attr, i, thresholds->thres_entries, defs);
1002 
1003  if (!onlyfailed) {
1004  if (state >= ATTRSTATE_FAILED_PAST)
1005  return attr.id;
1006  }
1007  else {
1009  return attr.id;
1010  }
1011  }
1012  return 0;
1013 }
1014 
1015 // onlyfailed=0 : print all attribute values
1016 // onlyfailed=1: just ones that are currently failed and have prefailure bit set
1017 // onlyfailed=2: ones that are failed, or have failed with or without prefailure bit set
1019  const ata_smart_thresholds_pvt * thresholds,
1020  const ata_vendor_attr_defs & defs, int rpm,
1021  int onlyfailed, unsigned char format)
1022 {
1023  bool brief = !!(format & ata_print_options::FMT_BRIEF);
1024  bool hexid = !!(format & ata_print_options::FMT_HEX_ID);
1025  bool hexval = !!(format & ata_print_options::FMT_HEX_VAL);
1026  bool needheader = true;
1027 
1028  // step through all vendor attributes
1029  for (int i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
1030  const ata_smart_attribute & attr = data->vendor_attributes[i];
1031 
1032  // Check attribute and threshold
1033  unsigned char threshold = 0;
1034  ata_attr_state state = ata_get_attr_state(attr, i, thresholds->thres_entries, defs, &threshold);
1035  if (state == ATTRSTATE_NON_EXISTING)
1036  continue;
1037 
1038  // These break out of the loop if we are only printing certain entries...
1039  if (onlyfailed == 1 && !(ATTRIBUTE_FLAGS_PREFAILURE(attr.flags) && state == ATTRSTATE_FAILED_NOW))
1040  continue;
1041 
1042  if (onlyfailed == 2 && state < ATTRSTATE_FAILED_PAST)
1043  continue;
1044 
1045  // print header only if needed
1046  if (needheader) {
1047  if (!onlyfailed) {
1048  pout("SMART Attributes Data Structure revision number: %d\n",(int)data->revnumber);
1049  pout("Vendor Specific SMART Attributes with Thresholds:\n");
1050  }
1051  if (!brief)
1052  pout("ID#%s ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE\n",
1053  (!hexid ? "" : " "));
1054  else
1055  pout("ID#%s ATTRIBUTE_NAME FLAGS VALUE WORST THRESH FAIL RAW_VALUE\n",
1056  (!hexid ? "" : " "));
1057  needheader = false;
1058  }
1059 
1060  // Format value, worst, threshold
1061  std::string valstr, worstr, threstr;
1062  if (state > ATTRSTATE_NO_NORMVAL)
1063  valstr = (!hexval ? strprintf("%.3d", attr.current)
1064  : strprintf("0x%02x", attr.current));
1065  else
1066  valstr = (!hexval ? "---" : "----");
1067  if (!(defs[attr.id].flags & ATTRFLAG_NO_WORSTVAL))
1068  worstr = (!hexval ? strprintf("%.3d", attr.worst)
1069  : strprintf("0x%02x", attr.worst));
1070  else
1071  worstr = (!hexval ? "---" : "----");
1072  if (state > ATTRSTATE_NO_THRESHOLD)
1073  threstr = (!hexval ? strprintf("%.3d", threshold)
1074  : strprintf("0x%02x", threshold));
1075  else
1076  threstr = (!hexval ? "---" : "----");
1077 
1078  // Print line for each valid attribute
1079  std::string idstr = (!hexid ? strprintf("%3d", attr.id)
1080  : strprintf("0x%02x", attr.id));
1081  std::string attrname = ata_get_smart_attr_name(attr.id, defs, rpm);
1082  std::string rawstr = ata_format_attr_raw_value(attr, defs);
1083 
1084  if (!brief)
1085  pout("%s %-24s0x%04x %-4s %-4s %-4s %-10s%-9s%-12s%s\n",
1086  idstr.c_str(), attrname.c_str(), attr.flags,
1087  valstr.c_str(), worstr.c_str(), threstr.c_str(),
1088  (ATTRIBUTE_FLAGS_PREFAILURE(attr.flags) ? "Pre-fail" : "Old_age"),
1089  (ATTRIBUTE_FLAGS_ONLINE(attr.flags) ? "Always" : "Offline"),
1090  (state == ATTRSTATE_FAILED_NOW ? "FAILING_NOW" :
1091  state == ATTRSTATE_FAILED_PAST ? "In_the_past"
1092  : " -" ) ,
1093  rawstr.c_str());
1094  else
1095  pout("%s %-24s%c%c%c%c%c%c%c %-4s %-4s %-4s %-5s%s\n",
1096  idstr.c_str(), attrname.c_str(),
1097  (ATTRIBUTE_FLAGS_PREFAILURE(attr.flags) ? 'P' : '-'),
1098  (ATTRIBUTE_FLAGS_ONLINE(attr.flags) ? 'O' : '-'),
1099  (ATTRIBUTE_FLAGS_PERFORMANCE(attr.flags) ? 'S' : '-'),
1100  (ATTRIBUTE_FLAGS_ERRORRATE(attr.flags) ? 'R' : '-'),
1101  (ATTRIBUTE_FLAGS_EVENTCOUNT(attr.flags) ? 'C' : '-'),
1102  (ATTRIBUTE_FLAGS_SELFPRESERVING(attr.flags) ? 'K' : '-'),
1103  (ATTRIBUTE_FLAGS_OTHER(attr.flags) ? '+' : ' '),
1104  valstr.c_str(), worstr.c_str(), threstr.c_str(),
1105  (state == ATTRSTATE_FAILED_NOW ? "NOW" :
1106  state == ATTRSTATE_FAILED_PAST ? "Past"
1107  : "-" ),
1108  rawstr.c_str());
1109 
1110  }
1111 
1112  if (!needheader) {
1113  if (!onlyfailed && brief) {
1114  int n = (!hexid ? 28 : 29);
1115  pout("%*s||||||_ K auto-keep\n"
1116  "%*s|||||__ C event count\n"
1117  "%*s||||___ R error rate\n"
1118  "%*s|||____ S speed/performance\n"
1119  "%*s||_____ O updated online\n"
1120  "%*s|______ P prefailure warning\n",
1121  n, "", n, "", n, "", n, "", n, "", n, "");
1122  }
1123  pout("\n");
1124  }
1125 }
1126 
1127 // Print SMART related SCT capabilities
1129 {
1130  unsigned short sctcaps = drive->words088_255[206-88];
1131  if (!(sctcaps & 0x01))
1132  return;
1133  pout("SCT capabilities: \t (0x%04x)\tSCT Status supported.\n", sctcaps);
1134  if (sctcaps & 0x08)
1135  pout("\t\t\t\t\tSCT Error Recovery Control supported.\n");
1136  if (sctcaps & 0x10)
1137  pout("\t\t\t\t\tSCT Feature Control supported.\n");
1138  if (sctcaps & 0x20)
1139  pout("\t\t\t\t\tSCT Data Table supported.\n");
1140 }
1141 
1142 
1144  firmwarebug_defs firmwarebugs)
1145 {
1146  pout("General SMART Values:\n");
1147 
1148  PrintSmartOfflineStatus(data);
1149 
1150  if (isSupportSelfTest(data)){
1151  PrintSmartSelfExecStatus(data, firmwarebugs);
1152  }
1153 
1156  PrintSmartCapability(data);
1157 
1158  PrintSmartErrorLogCapability(data, drive);
1159 
1160  pout( "\t\t\t\t\t%s\n", isGeneralPurposeLoggingCapable(drive)?
1161  "General Purpose Logging supported.":
1162  "No General Purpose Logging support.");
1163 
1164  if (isSupportSelfTest(data)){
1167  }
1168  if (isSupportConveyanceSelfTest(data))
1170 
1171  ataPrintSCTCapability(drive);
1172 
1173  pout("\n");
1174 }
1175 
1176 // Get # sectors of a log addr, 0 if log does not exist.
1177 static unsigned GetNumLogSectors(const ata_smart_log_directory * logdir, unsigned logaddr, bool gpl)
1178 {
1179  if (!logdir)
1180  return 0;
1181  if (logaddr > 0xff)
1182  return 0;
1183  if (logaddr == 0)
1184  return 1;
1185  unsigned n = logdir->entry[logaddr-1].numsectors;
1186  if (gpl)
1187  // GP logs may have >255 sectors
1188  n |= logdir->entry[logaddr-1].reserved << 8;
1189  return n;
1190 }
1191 
1192 // Get name of log.
1193 static const char * GetLogName(unsigned logaddr)
1194 {
1195  // Table A.2 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
1196  // Table 112 of Serial ATA Revision 3.2, August 7, 2013
1197  // Table A.2 of T13/2161-D (ACS-3) Revision 5, October 28, 2013
1198  // Table 204 of T13/BSR INCITS 529 (ACS-4) Revision 16, February 21, 2017
1199  switch (logaddr) {
1200  case 0x00: return "Log Directory";
1201  case 0x01: return "Summary SMART error log";
1202  case 0x02: return "Comprehensive SMART error log";
1203  case 0x03: return "Ext. Comprehensive SMART error log";
1204  case 0x04: return "Device Statistics log";
1205  case 0x05: return "Reserved for CFA"; // ACS-2
1206  case 0x06: return "SMART self-test log";
1207  case 0x07: return "Extended self-test log";
1208  case 0x08: return "Power Conditions log"; // ACS-2
1209  case 0x09: return "Selective self-test log";
1210  case 0x0a: return "Device Statistics Notification"; // ACS-3
1211  case 0x0b: return "Reserved for CFA"; // ACS-3
1212  case 0x0c: return "Pending Defects log"; // ACS-4
1213  case 0x0d: return "LPS Mis-alignment log"; // ACS-2
1214  case 0x0e: return "Reserved for ZAC-2"; // ACS-4
1215  case 0x0f: return "Sense Data for Successful NCQ Cmds log"; // ACS-4
1216  case 0x10: return "NCQ Command Error log";
1217  case 0x11: return "SATA Phy Event Counters log";
1218  //case 0x12: return "SATA NCQ Queue Management log"; // SATA 3.0/3.1, ACS-3
1219  case 0x12: return "SATA NCQ Non-Data log"; // SATA 3.2, ACS-4
1220  case 0x13: return "SATA NCQ Send and Receive log"; // SATA 3.1, ACS-3
1221  case 0x14: return "Hybrid Information log"; // SATA 3.2, ACS-4
1222  case 0x15: return "Rebuild Assist log"; // SATA 3.2, ACS-4
1223  case 0x16:
1224  case 0x17: return "Reserved for Serial ATA";
1225 
1226  case 0x19: return "LBA Status log"; // ACS-3
1227 
1228  case 0x20: return "Streaming performance log [OBS-8]";
1229  case 0x21: return "Write stream error log";
1230  case 0x22: return "Read stream error log";
1231  case 0x23: return "Delayed sector log [OBS-8]";
1232  case 0x24: return "Current Device Internal Status Data log"; // ACS-3
1233  case 0x25: return "Saved Device Internal Status Data log"; // ACS-3
1234 
1235  case 0x2f: return "Set Sector Configuration";; // ACS-4
1236  case 0x30: return "IDENTIFY DEVICE data log"; // ACS-3
1237 
1238  case 0xe0: return "SCT Command/Status";
1239  case 0xe1: return "SCT Data Transfer";
1240  default:
1241  if (0xa0 <= logaddr && logaddr <= 0xdf)
1242  return "Device vendor specific log";
1243  if (0x80 <= logaddr && logaddr <= 0x9f)
1244  return "Host vendor specific log";
1245  return "Reserved";
1246  }
1247  /*NOTREACHED*/
1248 }
1249 
1250 // Get log access permissions
1251 static const char * get_log_rw(unsigned logaddr)
1252 {
1253  if ( ( logaddr <= 0x08)
1254  || (0x0c <= logaddr && logaddr <= 0x0d)
1255  || (0x0f <= logaddr && logaddr <= 0x14)
1256  || (0x19 == logaddr)
1257  || (0x20 <= logaddr && logaddr <= 0x25)
1258  || (0x30 == logaddr))
1259  return "R/O";
1260 
1261  if ( (0x09 <= logaddr && logaddr <= 0x0a)
1262  || (0x15 == logaddr)
1263  || (0x80 <= logaddr && logaddr <= 0x9f)
1264  || (0xe0 <= logaddr && logaddr <= 0xe1))
1265  return "R/W";
1266 
1267  if (0xa0 <= logaddr && logaddr <= 0xdf)
1268  return "VS"; // Vendor specific
1269 
1270  return "-"; // Unknown/Reserved
1271 }
1272 
1273 // Init a fake log directory, assume that standard logs are supported
1275  const ata_print_options & options)
1276 {
1277  memset(logdir, 0, sizeof(*logdir));
1278  logdir->logversion = 255;
1279  logdir->entry[0x01-1].numsectors = 1;
1280  logdir->entry[0x03-1].numsectors = (options.smart_ext_error_log + (4-1)) / 4;
1281  logdir->entry[0x04-1].numsectors = 8;
1282  logdir->entry[0x06-1].numsectors = 1;
1283  logdir->entry[0x07-1].numsectors = (options.smart_ext_selftest_log + (19-1)) / 19;
1284  logdir->entry[0x09-1].numsectors = 1;
1285  logdir->entry[0x11-1].numsectors = 1;
1286  return logdir;
1287 }
1288 
1289 // Print SMART and/or GP Log Directory
1290 static void PrintLogDirectories(const ata_smart_log_directory * gplogdir,
1291  const ata_smart_log_directory * smartlogdir)
1292 {
1293  if (gplogdir)
1294  pout("General Purpose Log Directory Version %u\n", gplogdir->logversion);
1295  if (smartlogdir)
1296  pout("SMART %sLog Directory Version %u%s\n",
1297  (gplogdir ? " " : ""), smartlogdir->logversion,
1298  (smartlogdir->logversion==1 ? " [multi-sector log support]" : ""));
1299 
1300  pout("Address Access R/W Size Description\n");
1301 
1302  for (unsigned i = 0; i <= 0xff; i++) {
1303  // Get number of sectors
1304  unsigned smart_numsect = GetNumLogSectors(smartlogdir, i, false);
1305  unsigned gp_numsect = GetNumLogSectors(gplogdir , i, true );
1306 
1307  if (!(smart_numsect || gp_numsect))
1308  continue; // Log does not exist
1309 
1310  const char * acc; unsigned size;
1311  if (smart_numsect == gp_numsect) {
1312  acc = "GPL,SL"; size = gp_numsect;
1313  }
1314  else if (!smart_numsect) {
1315  acc = "GPL"; size = gp_numsect;
1316  }
1317  else if (!gp_numsect) {
1318  acc = " SL"; size = smart_numsect;
1319  }
1320  else {
1321  acc = 0; size = 0;
1322  }
1323 
1324  unsigned i2 = i;
1325  if (acc && ((0x80 <= i && i < 0x9f) || (0xa0 <= i && i < 0xdf))) {
1326  // Find range of Host/Device vendor specific logs with same size
1327  unsigned imax = (i < 0x9f ? 0x9f : 0xdf);
1328  for (unsigned j = i+1; j <= imax; j++) {
1329  unsigned sn = GetNumLogSectors(smartlogdir, j, false);
1330  unsigned gn = GetNumLogSectors(gplogdir , j, true );
1331 
1332  if (!(sn == smart_numsect && gn == gp_numsect))
1333  break;
1334  i2 = j;
1335  }
1336  }
1337 
1338  const char * name = GetLogName(i);
1339  const char * rw = get_log_rw(i);
1340 
1341  if (i2 > i) {
1342  pout("0x%02x-0x%02x %-6s %-3s %5u %s\n", i, i2, acc, rw, size, name);
1343  i = i2;
1344  }
1345  else if (acc)
1346  pout( "0x%02x %-6s %-3s %5u %s\n", i, acc, rw, size, name);
1347  else {
1348  // GPL and SL support different sizes
1349  pout( "0x%02x %-6s %-3s %5u %s\n", i, "GPL", rw, gp_numsect, name);
1350  pout( "0x%02x %-6s %-3s %5u %s\n", i, "SL", rw, smart_numsect, name);
1351  }
1352  }
1353  pout("\n");
1354 }
1355 
1356 // Print hexdump of log pages.
1357 // Format is compatible with 'xxd -r'.
1358 static void PrintLogPages(const char * type, const unsigned char * data,
1359  unsigned char logaddr, unsigned page,
1360  unsigned num_pages, unsigned max_pages)
1361 {
1362  pout("%s Log 0x%02x [%s], Page %u-%u (of %u)\n",
1363  type, logaddr, GetLogName(logaddr), page, page+num_pages-1, max_pages);
1364  for (unsigned i = 0; i < num_pages * 512; i += 16) {
1365  const unsigned char * p = data+i;
1366  pout("%07x: %02x %02x %02x %02x %02x %02x %02x %02x "
1367  "%02x %02x %02x %02x %02x %02x %02x %02x ",
1368  (page * 512) + i,
1369  p[ 0], p[ 1], p[ 2], p[ 3], p[ 4], p[ 5], p[ 6], p[ 7],
1370  p[ 8], p[ 9], p[10], p[11], p[12], p[13], p[14], p[15]);
1371 #define P(n) (' ' <= p[n] && p[n] <= '~' ? (int)p[n] : '.')
1372  pout("|%c%c%c%c%c%c%c%c"
1373  "%c%c%c%c%c%c%c%c|\n",
1374  P( 0), P( 1), P( 2), P( 3), P( 4), P( 5), P( 6), P( 7),
1375  P( 8), P( 9), P(10), P(11), P(12), P(13), P(14), P(15));
1376 #undef P
1377  if ((i & 0x1ff) == 0x1f0)
1378  pout("\n");
1379  }
1380 }
1381 
1382 ///////////////////////////////////////////////////////////////////////
1383 // Device statistics (Log 0x04)
1384 
1385 // Section A.5 of T13/2161-D (ACS-3) Revision 5, October 28, 2013
1386 // Section 9.5 of T13/BSR INCITS 529 (ACS-4) Revision 16, February 21, 2017
1387 
1389 {
1390  short size; // #bytes of value, -1 for signed char
1391  const char * name;
1392 };
1393 
1395  { 2, "List of supported log pages" },
1396  { 0, 0 }
1397 };
1398 
1400  { 2, "General Statistics" },
1401  { 4, "Lifetime Power-On Resets" },
1402  { 4, "Power-on Hours" },
1403  { 6, "Logical Sectors Written" },
1404  { 6, "Number of Write Commands" },
1405  { 6, "Logical Sectors Read" },
1406  { 6, "Number of Read Commands" },
1407  { 6, "Date and Time TimeStamp" }, // ACS-3
1408  { 4, "Pending Error Count" }, // ACS-4
1409  { 2, "Workload Utilization" }, // ACS-4
1410  { 6, "Utilization Usage Rate" }, // ACS-4 (TODO: field provides 3 values)
1411  { 2, "Resource Availability" }, // ACS-4
1412  { 1, "Random Write Resources Used" }, // ACS-4
1413  { 0, 0 }
1414 };
1415 
1417  { 2, "Free-Fall Statistics" },
1418  { 4, "Number of Free-Fall Events Detected" },
1419  { 4, "Overlimit Shock Events" },
1420  { 0, 0 }
1421 };
1422 
1424  { 2, "Rotating Media Statistics" },
1425  { 4, "Spindle Motor Power-on Hours" },
1426  { 4, "Head Flying Hours" },
1427  { 4, "Head Load Events" },
1428  { 4, "Number of Reallocated Logical Sectors" },
1429  { 4, "Read Recovery Attempts" },
1430  { 4, "Number of Mechanical Start Failures" },
1431  { 4, "Number of Realloc. Candidate Logical Sectors" }, // ACS-3
1432  { 4, "Number of High Priority Unload Events" }, // ACS-3
1433  { 0, 0 }
1434 };
1435 
1437  { 2, "General Errors Statistics" },
1438  { 4, "Number of Reported Uncorrectable Errors" },
1439 //{ 4, "Number of Resets Between Command Acceptance and Command Completion" },
1440  { 4, "Resets Between Cmd Acceptance and Completion" },
1441  { 0, 0 }
1442 };
1443 
1445  { 2, "Temperature Statistics" },
1446  { -1, "Current Temperature" },
1447  { -1, "Average Short Term Temperature" },
1448  { -1, "Average Long Term Temperature" },
1449  { -1, "Highest Temperature" },
1450  { -1, "Lowest Temperature" },
1451  { -1, "Highest Average Short Term Temperature" },
1452  { -1, "Lowest Average Short Term Temperature" },
1453  { -1, "Highest Average Long Term Temperature" },
1454  { -1, "Lowest Average Long Term Temperature" },
1455  { 4, "Time in Over-Temperature" },
1456  { -1, "Specified Maximum Operating Temperature" },
1457  { 4, "Time in Under-Temperature" },
1458  { -1, "Specified Minimum Operating Temperature" },
1459  { 0, 0 }
1460 };
1461 
1463  { 2, "Transport Statistics" },
1464  { 4, "Number of Hardware Resets" },
1465  { 4, "Number of ASR Events" },
1466  { 4, "Number of Interface CRC Errors" },
1467  { 0, 0 }
1468 };
1469 
1471  { 2, "Solid State Device Statistics" },
1472  { 1, "Percentage Used Endurance Indicator" },
1473  { 0, 0 }
1474 };
1475 
1484  devstat_info_0x07
1485  // TODO: 0x08 Zoned Device Statistics (T13/f16136r0, June 2016)
1486 };
1487 
1488 const int num_devstat_infos = sizeof(devstat_infos)/sizeof(devstat_infos[0]);
1489 
1490 static const char * get_device_statistics_page_name(int page)
1491 {
1492  if (page < num_devstat_infos)
1493  return devstat_infos[page][0].name;
1494  if (page == 0xff)
1495  return "Vendor Specific Statistics"; // ACS-4
1496  return "Unknown Statistics";
1497 }
1498 
1499 static void print_device_statistics_page(const unsigned char * data, int page)
1500 {
1501  const devstat_entry_info * info = (page < num_devstat_infos ? devstat_infos[page] : 0);
1502  const char * name = get_device_statistics_page_name(page);
1503 
1504  // Check page number in header
1505  static const char line[] = " ===== = = === == ";
1506  if (!data[2]) {
1507  pout("0x%02x%s%s (empty) ==\n", page, line, name);
1508  return;
1509  }
1510  if (data[2] != page) {
1511  pout("0x%02x%s%s (invalid page 0x%02x in header) ==\n", page, line, name, data[2]);
1512  return;
1513  }
1514 
1515  pout("0x%02x%s%s (rev %d) ==\n", page, line, name, data[0] | (data[1] << 8));
1516 
1517  // Print entries
1518  for (int i = 1, offset = 8; offset < 512-7; i++, offset+=8) {
1519  // Check for last known entry
1520  if (info && !info[i].size)
1521  info = 0;
1522 
1523  // Skip unsupported entries
1524  unsigned char flags = data[offset+7];
1525  if (!(flags & 0x80))
1526  continue;
1527 
1528  // Stop if unknown entries contain garbage data due to buggy firmware
1529  if (!info && (data[offset+5] || data[offset+6])) {
1530  pout("0x%02x 0x%03x - - [Trailing garbage ignored]\n", page, offset);
1531  break;
1532  }
1533 
1534  // Get value size, default to max if unknown
1535  int size = (info ? info[i].size : 7);
1536 
1537  // Format value
1538  char valstr[32];
1539  if (flags & 0x40) { // valid flag
1540  // Get value
1541  int64_t val;
1542  if (size < 0) {
1543  val = (signed char)data[offset];
1544  }
1545  else {
1546  val = 0;
1547  for (int j = 0; j < size; j++)
1548  val |= (int64_t)data[offset+j] << (j*8);
1549  }
1550  snprintf(valstr, sizeof(valstr), "%" PRId64, val);
1551  }
1552  else {
1553  // Value not known (yet)
1554  valstr[0] = '-'; valstr[1] = 0;
1555  }
1556 
1557  pout("0x%02x 0x%03x %d %15s %c%c%c%c %s\n",
1558  page, offset,
1559  abs(size),
1560  valstr,
1561  ((flags & 0x20) ? 'N' : '-'), // normalized statistics
1562  ((flags & 0x10) ? 'D' : '-'), // supports DSN (ACS-3)
1563  ((flags & 0x08) ? 'C' : '-'), // monitored condition met (ACS-3)
1564  ((flags & 0x07) ? '+' : ' '), // reserved flags
1565  ( info ? info[i].name :
1566  (page == 0xff) ? "Vendor Specific" // ACS-4
1567  : "Unknown" ));
1568  }
1569 }
1570 
1571 static bool print_device_statistics(ata_device * device, unsigned nsectors,
1572  const std::vector<int> & single_pages, bool all_pages, bool ssd_page,
1573  bool use_gplog)
1574 {
1575  // Read list of supported pages from page 0
1576  unsigned char page_0[512] = {0, };
1577  int rc;
1578 
1579  if (use_gplog)
1580  rc = ataReadLogExt(device, 0x04, 0, 0, page_0, 1);
1581  else
1582  rc = ataReadSmartLog(device, 0x04, page_0, 1);
1583  if (!rc) {
1584  pout("Read Device Statistics page 0x00 failed\n\n");
1585  return false;
1586  }
1587 
1588  unsigned char nentries = page_0[8];
1589  if (!(page_0[2] == 0 && nentries > 0)) {
1590  pout("Device Statistics page 0x00 is invalid (page=0x%02x, nentries=%d)\n\n", page_0[2], nentries);
1591  return false;
1592  }
1593 
1594  // Prepare list of pages to print
1595  std::vector<int> pages;
1596  unsigned i;
1597  if (all_pages) {
1598  // Add all supported pages
1599  for (i = 0; i < nentries; i++) {
1600  int page = page_0[8+1+i];
1601  if (page)
1602  pages.push_back(page);
1603  }
1604  ssd_page = false;
1605  }
1606  // Add manually specified pages
1607  bool print_page_0 = false;
1608  for (i = 0; i < single_pages.size() || ssd_page; i++) {
1609  int page = (i < single_pages.size() ? single_pages[i] : 0x07);
1610  if (!page)
1611  print_page_0 = true;
1612  else if (page >= (int)nsectors)
1613  pout("Device Statistics Log has only 0x%02x pages\n", nsectors);
1614  else
1615  pages.push_back(page);
1616  if (page == 0x07)
1617  ssd_page = false;
1618  }
1619 
1620  // Print list of supported pages if requested
1621  if (print_page_0) {
1622  pout("Device Statistics (%s Log 0x04) supported pages\n",
1623  use_gplog ? "GP" : "SMART");
1624  pout("Page Description\n");
1625  for (i = 0; i < nentries; i++) {
1626  int page = page_0[8+1+i];
1627  pout("0x%02x %s\n", page, get_device_statistics_page_name(page));
1628  }
1629  pout("\n");
1630  }
1631 
1632  // Read & print pages
1633  if (!pages.empty()) {
1634  pout("Device Statistics (%s Log 0x04)\n",
1635  use_gplog ? "GP" : "SMART");
1636  pout("Page Offset Size Value Flags Description\n");
1637  int max_page = 0;
1638 
1639  if (!use_gplog)
1640  for (i = 0; i < pages.size(); i++) {
1641  int page = pages[i];
1642  if (max_page < page && page < 0xff)
1643  max_page = page;
1644  }
1645 
1646  raw_buffer pages_buf((max_page+1) * 512);
1647 
1648  if (!use_gplog && !ataReadSmartLog(device, 0x04, pages_buf.data(), max_page+1)) {
1649  pout("Read Device Statistics pages 0x00-0x%02x failed\n\n", max_page);
1650  return false;
1651  }
1652 
1653  for (i = 0; i < pages.size(); i++) {
1654  int page = pages[i];
1655  if (use_gplog) {
1656  if (!ataReadLogExt(device, 0x04, 0, page, pages_buf.data(), 1)) {
1657  pout("Read Device Statistics page 0x%02x failed\n\n", page);
1658  return false;
1659  }
1660  }
1661  else if (page > max_page)
1662  continue;
1663 
1664  int offset = (use_gplog ? 0 : page * 512);
1665  print_device_statistics_page(pages_buf.data() + offset, page);
1666  }
1667 
1668  pout("%32s|||_ C monitored condition met\n", "");
1669  pout("%32s||__ D supports DSN\n", "");
1670  pout("%32s|___ N normalized value\n\n", "");
1671  }
1672 
1673  return true;
1674 }
1675 
1676 
1677 ///////////////////////////////////////////////////////////////////////
1678 
1679 // Print log 0x11
1680 static void PrintSataPhyEventCounters(const unsigned char * data, bool reset)
1681 {
1682  if (checksum(data))
1683  checksumwarning("SATA Phy Event Counters");
1684  pout("SATA Phy Event Counters (GP Log 0x11)\n");
1685  if (data[0] || data[1] || data[2] || data[3])
1686  pout("[Reserved: 0x%02x 0x%02x 0x%02x 0x%02x]\n",
1687  data[0], data[1], data[2], data[3]);
1688  pout("ID Size Value Description\n");
1689 
1690  for (unsigned i = 4; ; ) {
1691  // Get counter id and size (bits 14:12)
1692  unsigned id = data[i] | (data[i+1] << 8);
1693  unsigned size = ((id >> 12) & 0x7) << 1;
1694  id &= 0x8fff;
1695 
1696  // End of counter table ?
1697  if (!id)
1698  break;
1699  i += 2;
1700 
1701  if (!(2 <= size && size <= 8 && i + size < 512)) {
1702  pout("0x%04x %u: Invalid entry\n", id, size);
1703  break;
1704  }
1705 
1706  // Get value
1707  uint64_t val = 0, max_val = 0;
1708  for (unsigned j = 0; j < size; j+=2) {
1709  val |= (uint64_t)(data[i+j] | (data[i+j+1] << 8)) << (j*8);
1710  max_val |= (uint64_t)0xffffU << (j*8);
1711  }
1712  i += size;
1713 
1714  // Get name
1715  const char * name;
1716  switch (id) {
1717  case 0x001: name = "Command failed due to ICRC error"; break; // Mandatory
1718  case 0x002: name = "R_ERR response for data FIS"; break;
1719  case 0x003: name = "R_ERR response for device-to-host data FIS"; break;
1720  case 0x004: name = "R_ERR response for host-to-device data FIS"; break;
1721  case 0x005: name = "R_ERR response for non-data FIS"; break;
1722  case 0x006: name = "R_ERR response for device-to-host non-data FIS"; break;
1723  case 0x007: name = "R_ERR response for host-to-device non-data FIS"; break;
1724  case 0x008: name = "Device-to-host non-data FIS retries"; break;
1725  case 0x009: name = "Transition from drive PhyRdy to drive PhyNRdy"; break;
1726  case 0x00A: name = "Device-to-host register FISes sent due to a COMRESET"; break; // Mandatory
1727  case 0x00B: name = "CRC errors within host-to-device FIS"; break;
1728  case 0x00D: name = "Non-CRC errors within host-to-device FIS"; break;
1729  case 0x00F: name = "R_ERR response for host-to-device data FIS, CRC"; break;
1730  case 0x010: name = "R_ERR response for host-to-device data FIS, non-CRC"; break;
1731  case 0x012: name = "R_ERR response for host-to-device non-data FIS, CRC"; break;
1732  case 0x013: name = "R_ERR response for host-to-device non-data FIS, non-CRC"; break;
1733  default: name = ((id & 0x8000) ? "Vendor specific" : "Unknown"); break;
1734  }
1735 
1736  // Counters stop at max value, add '+' in this case
1737  pout("0x%04x %u %12" PRIu64 "%c %s\n", id, size, val,
1738  (val == max_val ? '+' : ' '), name);
1739  }
1740  if (reset)
1741  pout("All counters reset\n");
1742  pout("\n");
1743 }
1744 
1745 // Format milliseconds from error log entry as "DAYS+H:M:S.MSEC"
1746 static std::string format_milliseconds(unsigned msec)
1747 {
1748  unsigned days = msec / 86400000U;
1749  msec -= days * 86400000U;
1750  unsigned hours = msec / 3600000U;
1751  msec -= hours * 3600000U;
1752  unsigned min = msec / 60000U;
1753  msec -= min * 60000U;
1754  unsigned sec = msec / 1000U;
1755  msec -= sec * 1000U;
1756 
1757  std::string str;
1758  if (days)
1759  str = strprintf("%2ud+", days);
1760  str += strprintf("%02u:%02u:%02u.%03u", hours, min, sec, msec);
1761  return str;
1762 }
1763 
1764 // Get description for 'state' value from SMART Error Logs
1765 static const char * get_error_log_state_desc(unsigned state)
1766 {
1767  state &= 0x0f;
1768  switch (state){
1769  case 0x0: return "in an unknown state";
1770  case 0x1: return "sleeping";
1771  case 0x2: return "in standby mode";
1772  case 0x3: return "active or idle";
1773  case 0x4: return "doing SMART Offline or Self-test";
1774  default:
1775  return (state < 0xb ? "in a reserved state"
1776  : "in a vendor specific state");
1777  }
1778 }
1779 
1780 // returns number of errors
1782  firmwarebug_defs firmwarebugs)
1783 {
1784  pout("SMART Error Log Version: %d\n", (int)data->revnumber);
1785 
1786  // if no errors logged, return
1787  if (!data->error_log_pointer){
1788  pout("No Errors Logged\n\n");
1789  return 0;
1790  }
1791  print_on();
1792  // If log pointer out of range, return
1793  if (data->error_log_pointer>5){
1794  pout("Invalid Error Log index = 0x%02x (T13/1321D rev 1c "
1795  "Section 8.41.6.8.2.2 gives valid range from 1 to 5)\n\n",
1796  (int)data->error_log_pointer);
1797  return 0;
1798  }
1799 
1800  // Some internal consistency checking of the data structures
1801  if ((data->ata_error_count-data->error_log_pointer) % 5 && !firmwarebugs.is_set(BUG_SAMSUNG2)) {
1802  pout("Warning: ATA error count %d inconsistent with error log pointer %d\n\n",
1803  data->ata_error_count,data->error_log_pointer);
1804  }
1805 
1806  // starting printing error log info
1807  if (data->ata_error_count<=5)
1808  pout( "ATA Error Count: %d\n", (int)data->ata_error_count);
1809  else
1810  pout( "ATA Error Count: %d (device log contains only the most recent five errors)\n",
1811  (int)data->ata_error_count);
1812  print_off();
1813  pout("\tCR = Command Register [HEX]\n"
1814  "\tFR = Features Register [HEX]\n"
1815  "\tSC = Sector Count Register [HEX]\n"
1816  "\tSN = Sector Number Register [HEX]\n"
1817  "\tCL = Cylinder Low Register [HEX]\n"
1818  "\tCH = Cylinder High Register [HEX]\n"
1819  "\tDH = Device/Head Register [HEX]\n"
1820  "\tDC = Device Command Register [HEX]\n"
1821  "\tER = Error register [HEX]\n"
1822  "\tST = Status register [HEX]\n"
1823  "Powered_Up_Time is measured from power on, and printed as\n"
1824  "DDd+hh:mm:SS.sss where DD=days, hh=hours, mm=minutes,\n"
1825  "SS=sec, and sss=millisec. It \"wraps\" after 49.710 days.\n\n");
1826 
1827  // now step through the five error log data structures (table 39 of spec)
1828  for (int k = 4; k >= 0; k-- ) {
1829 
1830  // The error log data structure entries are a circular buffer
1831  int i = (data->error_log_pointer + k) % 5;
1832  const ata_smart_errorlog_struct * elog = data->errorlog_struct+i;
1833  const ata_smart_errorlog_error_struct * summary = &(elog->error_struct);
1834 
1835  // Spec says: unused error log structures shall be zero filled
1836  if (nonempty(elog, sizeof(*elog))){
1837  // Table 57 of T13/1532D Volume 1 Revision 3
1838  const char *msgstate = get_error_log_state_desc(summary->state);
1839  int days = (int)summary->timestamp/24;
1840 
1841  // See table 42 of ATA5 spec
1842  print_on();
1843  pout("Error %d occurred at disk power-on lifetime: %d hours (%d days + %d hours)\n",
1844  (int)(data->ata_error_count+k-4), (int)summary->timestamp, days, (int)(summary->timestamp-24*days));
1845  print_off();
1846  pout(" When the command that caused the error occurred, the device was %s.\n\n",msgstate);
1847  pout(" After command completion occurred, registers were:\n"
1848  " ER ST SC SN CL CH DH\n"
1849  " -- -- -- -- -- -- --\n"
1850  " %02x %02x %02x %02x %02x %02x %02x",
1851  (int)summary->error_register,
1852  (int)summary->status,
1853  (int)summary->sector_count,
1854  (int)summary->sector_number,
1855  (int)summary->cylinder_low,
1856  (int)summary->cylinder_high,
1857  (int)summary->drive_head);
1858  // Add a description of the contents of the status and error registers
1859  // if possible
1860  std::string st_er_desc = format_st_er_desc(elog);
1861  if (!st_er_desc.empty())
1862  pout(" %s", st_er_desc.c_str());
1863  pout("\n\n");
1864  pout(" Commands leading to the command that caused the error were:\n"
1865  " CR FR SC SN CL CH DH DC Powered_Up_Time Command/Feature_Name\n"
1866  " -- -- -- -- -- -- -- -- ---------------- --------------------\n");
1867  for (int j = 4; j >= 0; j--) {
1868  const ata_smart_errorlog_command_struct * thiscommand = elog->commands+j;
1869 
1870  // Spec says: unused data command structures shall be zero filled
1871  if (nonempty(thiscommand, sizeof(*thiscommand))) {
1872  pout(" %02x %02x %02x %02x %02x %02x %02x %02x %16s %s\n",
1873  (int)thiscommand->commandreg,
1874  (int)thiscommand->featuresreg,
1875  (int)thiscommand->sector_count,
1876  (int)thiscommand->sector_number,
1877  (int)thiscommand->cylinder_low,
1878  (int)thiscommand->cylinder_high,
1879  (int)thiscommand->drive_head,
1880  (int)thiscommand->devicecontrolreg,
1881  format_milliseconds(thiscommand->timestamp).c_str(),
1882  look_up_ata_command(thiscommand->commandreg, thiscommand->featuresreg));
1883  }
1884  }
1885  pout("\n");
1886  }
1887  }
1888  print_on();
1890  pout("\n");
1891  print_off();
1892  return data->ata_error_count;
1893 }
1894 
1895 // Print SMART Extended Comprehensive Error Log (GP Log 0x03)
1896 static int PrintSmartExtErrorLog(ata_device * device,
1897  const firmwarebug_defs & firmwarebugs,
1898  const ata_smart_exterrlog * log,
1899  unsigned nsectors, unsigned max_errors)
1900 {
1901  pout("SMART Extended Comprehensive Error Log Version: %u (%u sectors)\n",
1902  log->version, nsectors);
1903 
1904  if (!log->device_error_count) {
1905  pout("No Errors Logged\n\n");
1906  return 0;
1907  }
1908  print_on();
1909 
1910  // Check index
1911  unsigned nentries = nsectors * 4;
1912  unsigned erridx = log->error_log_index;
1913  if (!(1 <= erridx && erridx <= nentries)){
1914  // Some Samsung disks (at least SP1614C/SW100-25, HD300LJ/ZT100-12) use the
1915  // former index from Summary Error Log (byte 1, now reserved) and set byte 2-3
1916  // to 0.
1917  if (!(erridx == 0 && 1 <= log->reserved1 && log->reserved1 <= nentries)) {
1918  pout("Invalid Error Log index = 0x%04x (reserved = 0x%02x)\n", erridx, log->reserved1);
1919  return 0;
1920  }
1921  pout("Invalid Error Log index = 0x%04x, trying reserved byte (0x%02x) instead\n", erridx, log->reserved1);
1922  erridx = log->reserved1;
1923  }
1924 
1925  // Index base is not clearly specified by ATA8-ACS (T13/1699-D Revision 6a),
1926  // it is 1-based in practice.
1927  erridx--;
1928 
1929  // Calculate #errors to print
1930  unsigned errcnt = log->device_error_count;
1931 
1932  if (errcnt <= nentries)
1933  pout("Device Error Count: %u\n", log->device_error_count);
1934  else {
1935  errcnt = nentries;
1936  pout("Device Error Count: %u (device log contains only the most recent %u errors)\n",
1937  log->device_error_count, errcnt);
1938  }
1939 
1940  if (max_errors < errcnt)
1941  errcnt = max_errors;
1942 
1943  print_off();
1944  pout("\tCR = Command Register\n"
1945  "\tFEATR = Features Register\n"
1946  "\tCOUNT = Count (was: Sector Count) Register\n"
1947  "\tLBA_48 = Upper bytes of LBA High/Mid/Low Registers ] ATA-8\n"
1948  "\tLH = LBA High (was: Cylinder High) Register ] LBA\n"
1949  "\tLM = LBA Mid (was: Cylinder Low) Register ] Register\n"
1950  "\tLL = LBA Low (was: Sector Number) Register ]\n"
1951  "\tDV = Device (was: Device/Head) Register\n"
1952  "\tDC = Device Control Register\n"
1953  "\tER = Error register\n"
1954  "\tST = Status register\n"
1955  "Powered_Up_Time is measured from power on, and printed as\n"
1956  "DDd+hh:mm:SS.sss where DD=days, hh=hours, mm=minutes,\n"
1957  "SS=sec, and sss=millisec. It \"wraps\" after 49.710 days.\n\n");
1958 
1959  // Recently read log page
1960  ata_smart_exterrlog log_buf;
1961  unsigned log_buf_page = ~0;
1962 
1963  // Iterate through circular buffer in reverse direction
1964  for (unsigned i = 0, errnum = log->device_error_count;
1965  i < errcnt; i++, errnum--, erridx = (erridx > 0 ? erridx - 1 : nentries - 1)) {
1966 
1967  // Read log page if needed
1968  const ata_smart_exterrlog * log_p;
1969  unsigned page = erridx / 4;
1970  if (page == 0)
1971  log_p = log;
1972  else {
1973  if (page != log_buf_page) {
1974  memset(&log_buf, 0, sizeof(log_buf));
1975  if (!ataReadExtErrorLog(device, &log_buf, page, 1, firmwarebugs))
1976  break;
1977  log_buf_page = page;
1978  }
1979  log_p = &log_buf;
1980  }
1981 
1982  const ata_smart_exterrlog_error_log & entry = log_p->error_logs[erridx % 4];
1983 
1984  // Skip unused entries
1985  if (!nonempty(&entry, sizeof(entry))) {
1986  pout("Error %u [%u] log entry is empty\n", errnum, erridx);
1987  continue;
1988  }
1989 
1990  // Print error information
1991  print_on();
1992  const ata_smart_exterrlog_error & err = entry.error;
1993  pout("Error %u [%u] occurred at disk power-on lifetime: %u hours (%u days + %u hours)\n",
1994  errnum, erridx, err.timestamp, err.timestamp / 24, err.timestamp % 24);
1995  print_off();
1996 
1997  pout(" When the command that caused the error occurred, the device was %s.\n\n",
1999 
2000  // Print registers
2001  pout(" After command completion occurred, registers were:\n"
2002  " ER -- ST COUNT LBA_48 LH LM LL DV DC\n"
2003  " -- -- -- == -- == == == -- -- -- -- --\n"
2004  " %02x -- %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2005  err.error_register,
2006  err.status_register,
2007  err.count_register_hi,
2008  err.count_register,
2010  err.lba_mid_register_hi,
2011  err.lba_low_register_hi,
2012  err.lba_high_register,
2013  err.lba_mid_register,
2014  err.lba_low_register,
2015  err.device_register,
2017 
2018  // Add a description of the contents of the status and error registers
2019  // if possible
2020  std::string st_er_desc = format_st_er_desc(&entry);
2021  if (!st_er_desc.empty())
2022  pout(" %s", st_er_desc.c_str());
2023  pout("\n\n");
2024 
2025  // Print command history
2026  pout(" Commands leading to the command that caused the error were:\n"
2027  " CR FEATR COUNT LBA_48 LH LM LL DV DC Powered_Up_Time Command/Feature_Name\n"
2028  " -- == -- == -- == == == -- -- -- -- -- --------------- --------------------\n");
2029  for (int ci = 4; ci >= 0; ci--) {
2030  const ata_smart_exterrlog_command & cmd = entry.commands[ci];
2031 
2032  // Skip unused entries
2033  if (!nonempty(&cmd, sizeof(cmd)))
2034  continue;
2035 
2036  // Print registers, timestamp and ATA command name
2037  pout(" %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %16s %s\n",
2038  cmd.command_register,
2040  cmd.features_register,
2041  cmd.count_register_hi,
2042  cmd.count_register,
2044  cmd.lba_mid_register_hi,
2045  cmd.lba_low_register_hi,
2046  cmd.lba_high_register,
2047  cmd.lba_mid_register,
2048  cmd.lba_low_register,
2049  cmd.device_register,
2051  format_milliseconds(cmd.timestamp).c_str(),
2053  }
2054  pout("\n");
2055  }
2056 
2057  print_on();
2059  pout("\n");
2060  print_off();
2061  return log->device_error_count;
2062 }
2063 
2064 // Print SMART Extended Self-test Log (GP Log 0x07)
2066  unsigned nsectors, unsigned max_entries)
2067 {
2068  pout("SMART Extended Self-test Log Version: %u (%u sectors)\n",
2069  log->version, nsectors);
2070 
2071  if (!log->log_desc_index){
2072  pout("No self-tests have been logged. [To run self-tests, use: smartctl -t]\n\n");
2073  return 0;
2074  }
2075 
2076  // Check index
2077  unsigned nentries = nsectors * 19;
2078  unsigned logidx = log->log_desc_index;
2079  if (logidx > nentries) {
2080  pout("Invalid Self-test Log index = 0x%04x (reserved = 0x%02x)\n", logidx, log->reserved1);
2081  return 0;
2082  }
2083 
2084  // Index base is not clearly specified by ATA8-ACS (T13/1699-D Revision 6a),
2085  // it is 1-based in practice.
2086  logidx--;
2087 
2088  bool print_header = true;
2089  int errcnt = 0, igncnt = 0;
2090  int ext_ok_testnum = -1;
2091 
2092  // Iterate through circular buffer in reverse direction
2093  for (unsigned i = 0, testnum = 1;
2094  i < nentries && testnum <= max_entries;
2095  i++, logidx = (logidx > 0 ? logidx - 1 : nentries - 1)) {
2096 
2097  const ata_smart_extselftestlog_desc & entry = log[logidx / 19].log_descs[logidx % 19];
2098 
2099  // Skip unused entries
2100  if (!nonempty(&entry, sizeof(entry)))
2101  continue;
2102 
2103  // Get LBA
2104  const unsigned char * b = entry.failing_lba;
2105  uint64_t lba48 = b[0]
2106  | ( b[1] << 8)
2107  | ( b[2] << 16)
2108  | ((uint64_t)b[3] << 24)
2109  | ((uint64_t)b[4] << 32)
2110  | ((uint64_t)b[5] << 40);
2111 
2112  // Print entry
2113  int state = ataPrintSmartSelfTestEntry(testnum, entry.self_test_type,
2114  entry.self_test_status, entry.timestamp, lba48,
2115  false /*!print_error_only*/, print_header);
2116 
2117  if (state < 0) {
2118  // Self-test showed an error
2119  if (ext_ok_testnum < 0)
2120  errcnt++;
2121  else
2122  // Newer successful extended self-test exits
2123  igncnt++;
2124  }
2125  else if (state > 0 && ext_ok_testnum < 0) {
2126  // Latest successful extended self-test
2127  ext_ok_testnum = testnum;
2128  }
2129  testnum++;
2130  }
2131 
2132  if (igncnt)
2133  pout("%d of %d failed self-tests are outdated by newer successful extended offline self-test #%2d\n",
2134  igncnt, igncnt+errcnt, ext_ok_testnum);
2135 
2136  pout("\n");
2137  return errcnt;
2138 }
2139 
2141 {
2142  int i,field1,field2;
2143  const char *msg;
2144  char tmp[64];
2145  uint64_t maxl=0,maxr=0;
2146  uint64_t current=log->currentlba;
2147  uint64_t currentend=current+65535;
2148 
2149  // print data structure revision number
2150  pout("SMART Selective self-test log data structure revision number %d\n",(int)log->logversion);
2151  if (1 != log->logversion)
2152  pout("Note: revision number not 1 implies that no selective self-test has ever been run\n");
2153 
2154  switch((sv->self_test_exec_status)>>4){
2155  case 0:msg="Completed";
2156  break;
2157  case 1:msg="Aborted_by_host";
2158  break;
2159  case 2:msg="Interrupted";
2160  break;
2161  case 3:msg="Fatal_error";
2162  break;
2163  case 4:msg="Completed_unknown_failure";
2164  break;
2165  case 5:msg="Completed_electrical_failure";
2166  break;
2167  case 6:msg="Completed_servo/seek_failure";
2168  break;
2169  case 7:msg="Completed_read_failure";
2170  break;
2171  case 8:msg="Completed_handling_damage??";
2172  break;
2173  case 15:msg="Self_test_in_progress";
2174  break;
2175  default:msg="Unknown_status ";
2176  break;
2177  }
2178 
2179  // find the number of columns needed for printing. If in use, the
2180  // start/end of span being read-scanned...
2181  if (log->currentspan>5) {
2182  maxl=current;
2183  maxr=currentend;
2184  }
2185  for (i=0; i<5; i++) {
2186  uint64_t start=log->span[i].start;
2187  uint64_t end =log->span[i].end;
2188  // ... plus max start/end of each of the five test spans.
2189  if (start>maxl)
2190  maxl=start;
2191  if (end > maxr)
2192  maxr=end;
2193  }
2194 
2195  // we need at least 7 characters wide fields to accomodate the
2196  // labels
2197  if ((field1=snprintf(tmp,64, "%" PRIu64, maxl))<7)
2198  field1=7;
2199  if ((field2=snprintf(tmp,64, "%" PRIu64, maxr))<7)
2200  field2=7;
2201 
2202  // now print the five test spans
2203  pout(" SPAN %*s %*s CURRENT_TEST_STATUS\n", field1, "MIN_LBA", field2, "MAX_LBA");
2204 
2205  for (i=0; i<5; i++) {
2206  uint64_t start=log->span[i].start;
2207  uint64_t end=log->span[i].end;
2208 
2209  if ((i+1)==(int)log->currentspan)
2210  // this span is currently under test
2211  pout(" %d %*" PRIu64 " %*" PRIu64 " %s [%01d0%% left] (%" PRIu64 "-%" PRIu64 ")\n",
2212  i+1, field1, start, field2, end, msg,
2213  (int)(sv->self_test_exec_status & 0xf), current, currentend);
2214  else
2215  // this span is not currently under test
2216  pout(" %d %*" PRIu64 " %*" PRIu64 " Not_testing\n",
2217  i+1, field1, start, field2, end);
2218  }
2219 
2220  // if we are currently read-scanning, print LBAs and the status of
2221  // the read scan
2222  if (log->currentspan>5)
2223  pout("%5d %*" PRIu64 " %*" PRIu64 " Read_scanning %s\n",
2224  (int)log->currentspan, field1, current, field2, currentend,
2226 
2227  /* Print selective self-test flags. Possible flag combinations are
2228  (numbering bits from 0-15):
2229  Bit-1 Bit-3 Bit-4
2230  Scan Pending Active
2231  0 * * Don't scan
2232  1 0 0 Will carry out scan after selective test
2233  1 1 0 Waiting to carry out scan after powerup
2234  1 0 1 Currently scanning
2235  1 1 1 Currently scanning
2236  */
2237 
2238  pout("Selective self-test flags (0x%x):\n", (unsigned int)log->flags);
2239  if (log->flags & SELECTIVE_FLAG_DOSCAN) {
2240  if (log->flags & SELECTIVE_FLAG_ACTIVE)
2241  pout(" Currently read-scanning the remainder of the disk.\n");
2242  else if (log->flags & SELECTIVE_FLAG_PENDING)
2243  pout(" Read-scan of remainder of disk interrupted; will resume %d min after power-up.\n",
2244  (int)log->pendingtime);
2245  else
2246  pout(" After scanning selected spans, read-scan remainder of disk.\n");
2247  }
2248  else
2249  pout(" After scanning selected spans, do NOT read-scan remainder of disk.\n");
2250 
2251  // print pending time
2252  pout("If Selective self-test is pending on power-up, resume after %d minute delay.\n",
2253  (int)log->pendingtime);
2254 
2255  return;
2256 }
2257 
2258 // Format SCT Temperature value
2259 static const char * sct_ptemp(signed char x, char (& buf)[20])
2260 {
2261  if (x == -128 /*0x80 = unknown*/)
2262  return " ?";
2263  snprintf(buf, sizeof(buf), "%2d", x);
2264  return buf;
2265 }
2266 
2267 static const char * sct_pbar(int x, char (& buf)[64])
2268 {
2269  if (x <= 19)
2270  x = 0;
2271  else
2272  x -= 19;
2273  bool ov = false;
2274  if (x > 40) {
2275  x = 40; ov = true;
2276  }
2277  if (x > 0) {
2278  memset(buf, '*', x);
2279  if (ov)
2280  buf[x-1] = '+';
2281  buf[x] = 0;
2282  }
2283  else {
2284  buf[0] = '-'; buf[1] = 0;
2285  }
2286  return buf;
2287 }
2288 
2289 static const char * sct_device_state_msg(unsigned char state)
2290 {
2291  switch (state) {
2292  case 0: return "Active";
2293  case 1: return "Stand-by";
2294  case 2: return "Sleep";
2295  case 3: return "DST executing in background";
2296  case 4: return "SMART Off-line Data Collection executing in background";
2297  case 5: return "SCT command executing in background";
2298  default:return "Unknown";
2299  }
2300 }
2301 
2302 // Print SCT Status
2304 {
2305  pout("SCT Status Version: %u\n", sts->format_version);
2306  pout("SCT Version (vendor specific): %u (0x%04x)\n", sts->sct_version, sts->sct_version);
2307  pout("SCT Support Level: %u\n", sts->sct_spec);
2308  pout("Device State: %s (%u)\n",
2310  char buf1[20], buf2[20];
2311  if ( !sts->min_temp && !sts->life_min_temp
2312  && !sts->under_limit_count && !sts->over_limit_count) {
2313  // "Reserved" fields not set, assume "old" format version 2
2314  // Table 11 of T13/1701DT-N (SMART Command Transport) Revision 5, February 2005
2315  // Table 54 of T13/1699-D (ATA8-ACS) Revision 3e, July 2006
2316  pout("Current Temperature: %s Celsius\n",
2317  sct_ptemp(sts->hda_temp, buf1));
2318  pout("Power Cycle Max Temperature: %s Celsius\n",
2319  sct_ptemp(sts->max_temp, buf2));
2320  pout("Lifetime Max Temperature: %s Celsius\n",
2321  sct_ptemp(sts->life_max_temp, buf2));
2322  }
2323  else {
2324  // Assume "new" format version 2 or version 3
2325  // T13/e06152r0-3 (Additional SCT Temperature Statistics), August - October 2006
2326  // Table 60 of T13/1699-D (ATA8-ACS) Revision 3f, December 2006 (format version 2)
2327  // Table 80 of T13/1699-D (ATA8-ACS) Revision 6a, September 2008 (format version 3)
2328  // Table 185 of T13/BSR INCITS 529 (ACS-4) Revision 16, February 21, 2017
2329  // (smart_status, min_erc_time)
2330  pout("Current Temperature: %s Celsius\n",
2331  sct_ptemp(sts->hda_temp, buf1));
2332  pout("Power Cycle Min/Max Temperature: %s/%s Celsius\n",
2333  sct_ptemp(sts->min_temp, buf1), sct_ptemp(sts->max_temp, buf2));
2334  pout("Lifetime Min/Max Temperature: %s/%s Celsius\n",
2335  sct_ptemp(sts->life_min_temp, buf1), sct_ptemp(sts->life_max_temp, buf2));
2336  signed char avg = sts->byte205; // Average Temperature from e06152r0-2, removed in e06152r3
2337  if (0 < avg && sts->life_min_temp <= avg && avg <= sts->life_max_temp)
2338  pout("Lifetime Average Temperature: %2d Celsius\n", avg);
2339  pout("Under/Over Temperature Limit Count: %2u/%u\n",
2340  sts->under_limit_count, sts->over_limit_count);
2341 
2342  if (sts->smart_status) // ACS-4
2343  pout("SMART Status: 0x%04x (%s)\n", sts->smart_status,
2344  (sts->smart_status == 0x2cf4 ? "FAILED" :
2345  sts->smart_status == 0xc24f ? "PASSED" : "Reserved"));
2346 
2347  if (sts->min_erc_time) // ACS-4
2348  pout("Minimum supported ERC Time Limit: %d (%0.1f seconds)\n",
2349  sts->min_erc_time, sts->min_erc_time/10.0);
2350 
2351  if (nonempty(sts->vendor_specific, sizeof(sts->vendor_specific))) {
2352  pout("Vendor specific:\n");
2353  for (unsigned i = 0; i < sizeof(sts->vendor_specific); i++)
2354  pout("%02x%c", sts->vendor_specific[i], ((i & 0xf) != 0xf ? ' ' : '\n'));
2355  }
2356  }
2357  return 0;
2358 }
2359 
2360 // Print SCT Temperature History Table
2362 {
2363  char buf1[20], buf2[20], buf3[64];
2364  pout("SCT Temperature History Version: %u%s\n", tmh->format_version,
2365  (tmh->format_version != 2 ? " (Unknown, should be 2)" : ""));
2366  pout("Temperature Sampling Period: %u minute%s\n",
2367  tmh->sampling_period, (tmh->sampling_period==1?"":"s"));
2368  pout("Temperature Logging Interval: %u minute%s\n",
2369  tmh->interval, (tmh->interval==1?"":"s"));
2370  pout("Min/Max recommended Temperature: %s/%s Celsius\n",
2371  sct_ptemp(tmh->min_op_limit, buf1), sct_ptemp(tmh->max_op_limit, buf2));
2372  pout("Min/Max Temperature Limit: %s/%s Celsius\n",
2373  sct_ptemp(tmh->under_limit, buf1), sct_ptemp(tmh->over_limit, buf2));
2374  pout("Temperature History Size (Index): %u (%u)\n", tmh->cb_size, tmh->cb_index);
2375 
2376  if (!(0 < tmh->cb_size && tmh->cb_size <= sizeof(tmh->cb) && tmh->cb_index < tmh->cb_size)) {
2377  if (!tmh->cb_size)
2378  pout("Temperature History is empty\n");
2379  else
2380  pout("Invalid Temperature History Size or Index\n");
2381  return 0;
2382  }
2383 
2384  // Print table
2385  pout("\nIndex Estimated Time Temperature Celsius\n");
2386  unsigned n = 0, i = (tmh->cb_index+1) % tmh->cb_size;
2387  unsigned interval = (tmh->interval > 0 ? tmh->interval : 1);
2388  time_t t = time(0) - (tmh->cb_size-1) * interval * 60;
2389  t -= t % (interval * 60);
2390  while (n < tmh->cb_size) {
2391  // Find range of identical temperatures
2392  unsigned n1 = n, n2 = n+1, i2 = (i+1) % tmh->cb_size;
2393  while (n2 < tmh->cb_size && tmh->cb[i2] == tmh->cb[i]) {
2394  n2++; i2 = (i2+1) % tmh->cb_size;
2395  }
2396  // Print range
2397  while (n < n2) {
2398  if (n == n1 || n == n2-1 || n2 <= n1+3) {
2399  char date[30];
2400  // TODO: Don't print times < boot time
2401  strftime(date, sizeof(date), "%Y-%m-%d %H:%M", localtime(&t));
2402  pout(" %3u %s %s %s\n", i, date,
2403  sct_ptemp(tmh->cb[i], buf1), sct_pbar(tmh->cb[i], buf3));
2404  }
2405  else if (n == n1+1) {
2406  pout(" ... ..(%3u skipped). .. %s\n",
2407  n2-n1-2, sct_pbar(tmh->cb[i], buf3));
2408  }
2409  t += interval * 60; i = (i+1) % tmh->cb_size; n++;
2410  }
2411  }
2412  //assert(n == tmh->cb_size && i == (tmh->cb_index+1) % tmh->cb_size);
2413 
2414  return 0;
2415 }
2416 
2417 // Print SCT Error Recovery Control timers
2418 static void ataPrintSCTErrorRecoveryControl(bool set, unsigned short read_timer, unsigned short write_timer)
2419 {
2420  pout("SCT Error Recovery Control%s:\n", (set ? " set to" : ""));
2421  if (!read_timer)
2422  pout(" Read: Disabled\n");
2423  else
2424  pout(" Read: %6d (%0.1f seconds)\n", read_timer, read_timer/10.0);
2425  if (!write_timer)
2426  pout(" Write: Disabled\n");
2427  else
2428  pout(" Write: %6d (%0.1f seconds)\n", write_timer, write_timer/10.0);
2429 }
2430 
2431 static void print_aam_level(const char * msg, int level, int recommended = -1)
2432 {
2433  // Table 56 of T13/1699-D (ATA8-ACS) Revision 6a, September 6, 2008
2434  // Obsolete since T13/2015-D (ACS-2) Revision 4a, December 9, 2010
2435  const char * s;
2436  if (level == 0)
2437  s = "vendor specific";
2438  else if (level < 128)
2439  s = "unknown/retired";
2440  else if (level == 128)
2441  s = "quiet";
2442  else if (level < 254)
2443  s = "intermediate";
2444  else if (level == 254)
2445  s = "maximum performance";
2446  else
2447  s = "reserved";
2448 
2449  if (recommended >= 0)
2450  pout("%s%d (%s), recommended: %d\n", msg, level, s, recommended);
2451  else
2452  pout("%s%d (%s)\n", msg, level, s);
2453 }
2454 
2455 static void print_apm_level(const char * msg, int level)
2456 {
2457  // Table 120 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
2458  const char * s;
2459  if (!(1 <= level && level <= 254))
2460  s = "reserved";
2461  else if (level == 1)
2462  s = "minimum power consumption with standby";
2463  else if (level < 128)
2464  s = "intermediate level with standby";
2465  else if (level == 128)
2466  s = "minimum power consumption without standby";
2467  else if (level < 254)
2468  s = "intermediate level without standby";
2469  else
2470  s = "maximum performance";
2471 
2472  pout("%s%d (%s)\n", msg, level, s);
2473 }
2474 
2475 static void print_ata_security_status(const char * msg, unsigned short state)
2476 {
2477  const char * s1, * s2 = "", * s3 = "", * s4 = "";
2478 
2479  // Table 6 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
2480  if (!(state & 0x0001))
2481  s1 = "Unavailable";
2482  else if (!(state & 0x0002)) {
2483  s1 = "Disabled, ";
2484  if (!(state & 0x0008))
2485  s2 = "NOT FROZEN [SEC1]";
2486  else
2487  s2 = "frozen [SEC2]";
2488  }
2489  else {
2490  s1 = "ENABLED, PW level ";
2491  if (!(state & 0x0020))
2492  s2 = "HIGH";
2493  else
2494  s2 = "MAX";
2495 
2496  if (!(state & 0x0004)) {
2497  s3 = ", not locked, ";
2498  if (!(state & 0x0008))
2499  s4 = "not frozen [SEC5]";
2500  else
2501  s4 = "frozen [SEC6]";
2502  }
2503  else {
2504  s3 = ", **LOCKED** [SEC4]";
2505  if (state & 0x0010)
2506  s4 = ", PW ATTEMPTS EXCEEDED";
2507  }
2508  }
2509 
2510  pout("%s%s%s%s%s\n", msg, s1, s2, s3, s4);
2511 }
2512 
2513 static void print_standby_timer(const char * msg, int timer, const ata_identify_device & drive)
2514 {
2515  const char * s1 = 0;
2516  int hours = 0, minutes = 0 , seconds = 0;
2517 
2518  // Table 63 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
2519  if (timer == 0)
2520  s1 = "disabled";
2521  else if (timer <= 240)
2522  seconds = timer * 5, minutes = seconds / 60, seconds %= 60;
2523  else if (timer <= 251)
2524  minutes = (timer - 240) * 30, hours = minutes / 60, minutes %= 60;
2525  else if (timer == 252)
2526  minutes = 21;
2527  else if (timer == 253)
2528  s1 = "between 8 hours and 12 hours";
2529  else if (timer == 255)
2530  minutes = 21, seconds = 15;
2531  else
2532  s1 = "reserved";
2533 
2534  const char * s2 = "", * s3 = "";
2535  if (!(drive.words047_079[49-47] & 0x2000))
2536  s2 = " or vendor-specific";
2537  if (timer > 0 && (drive.words047_079[50-47] & 0xc001) == 0x4001)
2538  s3 = ", a vendor-specific minimum applies";
2539 
2540  if (s1)
2541  pout("%s%d (%s%s%s)\n", msg, timer, s1, s2, s3);
2542  else
2543  pout("%s%d (%02d:%02d:%02d%s%s)\n", msg, timer, hours, minutes, seconds, s2, s3);
2544 }
2545 
2546 
2547 int ataPrintMain (ata_device * device, const ata_print_options & options)
2548 {
2549  // If requested, check power mode first
2550  const char * powername = 0;
2551  bool powerchg = false;
2552  if (options.powermode) {
2553  unsigned char powerlimit = 0xff;
2554  int powermode = ataCheckPowerMode(device);
2555  // TODO: Move to new function used by smartctl and smartd.
2556  switch (powermode) {
2557  case -1:
2558  if (device->is_syscall_unsup()) {
2559  pout("CHECK POWER MODE not implemented, ignoring -n option\n"); break;
2560  }
2561  powername = "SLEEP"; powerlimit = 2;
2562  break;
2563  // Table 215 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
2564  // Table 293 of T13/BSR INCITS 529 (ACS-4) Revision 12, February 18, 2016
2565  case 0x00: // PM2:Standby, EPC unavailable or Standby_z power condition
2566  powername = "STANDBY"; powerlimit = 3; break;
2567  case 0x01: // PM2:Standby, Standby_y power condition
2568  powername = "STANDBY_Y"; powerlimit = 3; break;
2569  case 0x80: // PM1:Idle, EPC unavailable
2570  powername = "IDLE"; powerlimit = 4; break;
2571  case 0x81: // PM1:Idle, Idle_a power condition
2572  powername = "IDLE_A"; powerlimit = 4; break;
2573  case 0x82: // PM1:Idle, Idle_b power condition
2574  powername = "IDLE_B"; powerlimit = 4; break;
2575  case 0x83: // PM1:Idle, Idle_c power condition
2576  powername = "IDLE_C"; powerlimit = 4; break;
2577  // 0x40/41 were declared obsolete in ACS-3 Revision 1
2578  case 0x40: // PM0:Active, NV Cache power mode enabled, spun down
2579  powername = "ACTIVE_NV_DOWN"; break;
2580  case 0x41: // PM0:Active, NV Cache power mode enabled, spun up
2581  powername = "ACTIVE_NV_UP" ; break;
2582  case 0xff: // PM0:Active or PM1:Idle
2583  powername = "ACTIVE or IDLE"; break;
2584 
2585  default:
2586  pout("CHECK POWER MODE returned unknown value 0x%02x, ignoring -n option\n", powermode);
2587  break;
2588  }
2589  if (powername) {
2590  if (options.powermode >= powerlimit) {
2591  pout("Device is in %s mode, exit(%d)\n", powername, options.powerexit);
2592  return options.powerexit;
2593  }
2594  powerchg = (powermode != 0xff); // SMART tests will spin up drives
2595  }
2596  }
2597 
2598  // SMART values needed ?
2599  bool need_smart_val = (
2600  options.smart_check_status
2601  || options.smart_general_values
2602  || options.smart_vendor_attrib
2603  || options.smart_error_log
2604  || options.smart_selftest_log
2605  || options.smart_selective_selftest_log
2606  || options.smart_ext_error_log
2607  || options.smart_ext_selftest_log
2608  || options.smart_auto_offl_enable
2609  || options.smart_auto_offl_disable
2610  || options.smart_selftest_type != -1
2611  );
2612 
2613  // SMART must be enabled ?
2614  bool need_smart_enabled = (
2615  need_smart_val
2616  || options.smart_auto_save_enable
2617  || options.smart_auto_save_disable
2618  );
2619 
2620  // SMART feature set needed ?
2621  bool need_smart_support = (
2622  need_smart_enabled
2623  || options.smart_enable
2624  || options.smart_disable
2625  );
2626 
2627  // SMART and GP log directories needed ?
2628  bool need_smart_logdir = (
2629  options.smart_logdir
2630  || options.devstat_all_pages // devstat fallback to smartlog if needed
2631  || options.devstat_ssd_page
2632  || !options.devstat_pages.empty()
2633  );
2634 
2635  bool need_gp_logdir = (
2636  options.gp_logdir
2637  || options.smart_ext_error_log
2638  || options.smart_ext_selftest_log
2639  || options.devstat_all_pages
2640  || options.devstat_ssd_page
2641  || !options.devstat_pages.empty()
2642  );
2643 
2644  unsigned i;
2645  for (i = 0; i < options.log_requests.size(); i++) {
2646  if (options.log_requests[i].gpl)
2647  need_gp_logdir = true;
2648  else
2649  need_smart_logdir = true;
2650  }
2651 
2652  // SCT commands needed ?
2653  bool need_sct_support = (
2654  options.sct_temp_sts
2655  || options.sct_temp_hist
2656  || options.sct_temp_int
2657  || options.sct_erc_get
2658  || options.sct_erc_set
2659  || options.sct_wcache_reorder_get
2660  || options.sct_wcache_reorder_set
2661  || options.sct_wcache_sct_get
2662  || options.sct_wcache_sct_set
2663  );
2664 
2665  // Exit if no further options specified
2666  if (!( options.drive_info || options.show_presets
2667  || need_smart_support || need_smart_logdir
2668  || need_gp_logdir || need_sct_support
2669  || options.sataphy
2670  || options.identify_word_level >= 0
2671  || options.get_set_used )) {
2672  if (powername)
2673  pout("Device is in %s mode\n", powername);
2674  else
2675  pout("ATA device successfully opened\n\n"
2676  "Use 'smartctl -a' (or '-x') to print SMART (and more) information\n\n");
2677  return 0;
2678  }
2679 
2680  // Start by getting Drive ID information. We need this, to know if SMART is supported.
2681  int returnval = 0;
2682  ata_identify_device drive; memset(&drive, 0, sizeof(drive));
2683  unsigned char raw_drive[sizeof(drive)]; memset(&raw_drive, 0, sizeof(raw_drive));
2684 
2685  device->clear_err();
2686  int retid = ata_read_identity(device, &drive, options.fix_swapped_id, raw_drive);
2687  if (retid < 0) {
2688  pout("Read Device Identity failed: %s\n\n",
2689  (device->get_errno() ? device->get_errmsg() : "Unknown error"));
2690  failuretest(MANDATORY_CMD, returnval|=FAILID);
2691  }
2692  else if (!nonempty(&drive, sizeof(drive))) {
2693  pout("Read Device Identity failed: empty IDENTIFY data\n\n");
2694  failuretest(MANDATORY_CMD, returnval|=FAILID);
2695  }
2696 
2697  // If requested, show which presets would be used for this drive and exit.
2698  if (options.show_presets) {
2699  show_presets(&drive);
2700  return 0;
2701  }
2702 
2703  // Use preset vendor attribute options unless user has requested otherwise.
2704  ata_vendor_attr_defs attribute_defs = options.attribute_defs;
2705  firmwarebug_defs firmwarebugs = options.firmwarebugs;
2706  const drive_settings * dbentry = 0;
2707  if (!options.ignore_presets)
2708  dbentry = lookup_drive_apply_presets(&drive, attribute_defs,
2709  firmwarebugs);
2710 
2711  // Get capacity, sector sizes and rotation rate
2712  ata_size_info sizes;
2713  ata_get_size_info(&drive, sizes);
2714  int rpm = ata_get_rotation_rate(&drive);
2715 
2716  // Print ATA IDENTIFY info if requested
2717  if (options.identify_word_level >= 0) {
2718  pout("=== ATA IDENTIFY DATA ===\n");
2719  // Pass raw data without endianness adjustments
2720  ata_print_identify_data(raw_drive, (options.identify_word_level > 0), options.identify_bit_level);
2721  }
2722 
2723  // Print most drive identity information if requested
2724  if (options.drive_info) {
2725  pout("=== START OF INFORMATION SECTION ===\n");
2726  print_drive_info(&drive, sizes, rpm, dbentry);
2727  }
2728 
2729  // Check and print SMART support and state
2730  int smart_supported = -1, smart_enabled = -1;
2731  if (need_smart_support || options.drive_info) {
2732 
2733  // Packet device ?
2734  if (retid > 0) {
2735  pout("SMART support is: Unavailable - Packet Interface Devices [this device: %s] don't support ATA SMART\n",
2736  packetdevicetype(retid-1));
2737  }
2738  else {
2739  // Disk device: SMART supported and enabled ?
2740  smart_supported = ataSmartSupport(&drive);
2741  smart_enabled = ataIsSmartEnabled(&drive);
2742 
2743  if (smart_supported < 0)
2744  pout("SMART support is: Ambiguous - ATA IDENTIFY DEVICE words 82-83 don't show if SMART supported.\n");
2745  if (smart_supported && smart_enabled < 0) {
2746  pout("SMART support is: Ambiguous - ATA IDENTIFY DEVICE words 85-87 don't show if SMART is enabled.\n");
2747  if (need_smart_support) {
2748  failuretest(MANDATORY_CMD, returnval|=FAILSMART);
2749  // check SMART support by trying a command
2750  pout(" Checking to be sure by trying SMART RETURN STATUS command.\n");
2751  if (ataDoesSmartWork(device))
2752  smart_supported = smart_enabled = 1;
2753  }
2754  }
2755  else if (smart_supported < 0 && (smart_enabled > 0 || dbentry))
2756  // Assume supported if enabled or in drive database
2757  smart_supported = 1;
2758 
2759  if (smart_supported < 0)
2760  pout("SMART support is: Unknown - Try option -s with argument 'on' to enable it.");
2761  else if (!smart_supported)
2762  pout("SMART support is: Unavailable - device lacks SMART capability.\n");
2763  else {
2764  if (options.drive_info)
2765  pout("SMART support is: Available - device has SMART capability.\n");
2766  if (smart_enabled >= 0) {
2767  if (device->ata_identify_is_cached()) {
2768  if (options.drive_info)
2769  pout(" %sabled status cached by OS, trying SMART RETURN STATUS cmd.\n",
2770  (smart_enabled?"En":"Dis"));
2771  smart_enabled = ataDoesSmartWork(device);
2772  }
2773  if (options.drive_info)
2774  pout("SMART support is: %s\n",
2775  (smart_enabled ? "Enabled" : "Disabled"));
2776  }
2777  }
2778  }
2779  }
2780 
2781  // Print AAM status
2782  if (options.get_aam) {
2783  if ((drive.command_set_2 & 0xc200) != 0x4200) // word083
2784  pout("AAM feature is: Unavailable\n");
2785  else if (!(drive.word086 & 0x0200))
2786  pout("AAM feature is: Disabled\n");
2787  else
2788  print_aam_level("AAM level is: ", drive.words088_255[94-88] & 0xff,
2789  drive.words088_255[94-88] >> 8);
2790  }
2791 
2792  // Print APM status
2793  if (options.get_apm) {
2794  if ((drive.command_set_2 & 0xc008) != 0x4008) // word083
2795  pout("APM feature is: Unavailable\n");
2796  else if (!(drive.word086 & 0x0008))
2797  pout("APM feature is: Disabled\n");
2798  else
2799  print_apm_level("APM level is: ", drive.words088_255[91-88] & 0xff);
2800  }
2801 
2802  // Print read look-ahead status
2803  if (options.get_lookahead) {
2804  pout("Rd look-ahead is: %s\n",
2805  ( (drive.command_set_2 & 0xc000) != 0x4000 // word083
2806  || !(drive.command_set_1 & 0x0040)) ? "Unavailable" : // word082
2807  !(drive.cfs_enable_1 & 0x0040) ? "Disabled" : "Enabled"); // word085
2808  }
2809 
2810  // Print write cache status
2811  if (options.get_wcache) {
2812  pout("Write cache is: %s\n",
2813  ( (drive.command_set_2 & 0xc000) != 0x4000 // word083
2814  || !(drive.command_set_1 & 0x0020)) ? "Unavailable" : // word082
2815  !(drive.cfs_enable_1 & 0x0020) ? "Disabled" : "Enabled"); // word085
2816  }
2817 
2818  // Print DSN status
2819  unsigned short word120 = drive.words088_255[120-88];
2820  unsigned short word119 = drive.words088_255[119-88];
2821  if (options.get_dsn) {
2822  if (!(drive.word086 & 0x8000) // word086
2823  || ((word119 & 0xc200) != 0x4200) // word119
2824  || ((word120 & 0xc000) != 0x4000)) // word120
2825  pout("DSN feature is: Unavailable\n");
2826  else if (word120 & 0x200) // word120
2827  pout("DSN feature is: Enabled\n");
2828  else
2829  pout("DSN feature is: Disabled\n");
2830  }
2831 
2832  // Check for ATA Security LOCK
2833  unsigned short word128 = drive.words088_255[128-88];
2834  bool locked = ((word128 & 0x0007) == 0x0007); // LOCKED|ENABLED|SUPPORTED
2835 
2836  // Print ATA Security status
2837  if (options.get_security)
2838  print_ata_security_status("ATA Security is: ", word128);
2839 
2840  // Print write cache reordering status
2841  if (options.sct_wcache_reorder_get) {
2842  if (!isSCTFeatureControlCapable(&drive))
2843  pout("Wt Cache Reorder: Unavailable\n");
2844  else if (locked)
2845  pout("Wt Cache Reorder: Unknown (SCT not supported if ATA Security is LOCKED)\n");
2846  else {
2847  int wcache_reorder = ataGetSetSCTWriteCacheReordering(device,
2848  false /*enable*/, false /*persistent*/, false /*set*/);
2849 
2850  if (-1 <= wcache_reorder && wcache_reorder <= 2)
2851  pout("Wt Cache Reorder: %s\n",
2852  (wcache_reorder == -1 ? "Unknown (SCT Feature Control command failed)" :
2853  wcache_reorder == 0 ? "Unknown" : // not defined in standard but returned on some drives if not set
2854  wcache_reorder == 1 ? "Enabled" : "Disabled"));
2855  else
2856  pout("Wt Cache Reorder: Unknown (0x%02x)\n", wcache_reorder);
2857  }
2858  }
2859 
2860  const char * sct_write_cache_state_desc[4] = {
2861  "Unknown", // 0: not defined in standard but returned on some drives if not set
2862  "Controlled by ATA", // 1: controlled ATA Set Features command
2863  "Force Enabled", // 2
2864  "Force Disabled" // 3
2865  };
2866 
2867  // Print SCT feature control of write cache
2868  if (options.sct_wcache_sct_get) {
2869  if (!isSCTFeatureControlCapable(&drive))
2870  pout("SCT Write Cache Control: Unavailable\n");
2871  else if (locked)
2872  pout("SCT Write Cache Control: Unknown (SCT not supported if ATA Security is LOCKED)\n");
2873  else {
2874  int state = ataGetSetSCTWriteCache(device, 1, false /*persistent*/, false /*set*/);
2875  if (-1 <= state && state <= 3)
2876  pout("SCT Write Cache Control: %s\n",
2877  (state == -1 ? "Unknown (SCT Feature Control command failed)" :
2878  sct_write_cache_state_desc[state]));
2879  else
2880  pout("SCT Write Cache Control: Unknown (0x%02x)\n", state);
2881  }
2882  }
2883 
2884 
2885  // Print remaining drive info
2886  if (options.drive_info) {
2887  // Print the (now possibly changed) power mode if available
2888  if (powername)
2889  pout("Power mode %s %s\n", (powerchg?"was:":"is: "), powername);
2890  pout("\n");
2891  }
2892 
2893  // Exit if SMART is not supported but must be available to proceed
2894  if (smart_supported <= 0 && need_smart_support)
2895  failuretest(MANDATORY_CMD, returnval|=FAILSMART);
2896 
2897  // START OF THE ENABLE/DISABLE SECTION OF THE CODE
2898  if ( options.smart_disable || options.smart_enable
2899  || options.smart_auto_save_disable || options.smart_auto_save_enable
2900  || options.smart_auto_offl_disable || options.smart_auto_offl_enable
2901  || options.set_aam || options.set_apm || options.set_lookahead
2902  || options.set_wcache || options.set_security_freeze || options.set_standby
2903  || options.sct_wcache_reorder_set || options.sct_wcache_sct_set || options.set_dsn)
2904  pout("=== START OF ENABLE/DISABLE COMMANDS SECTION ===\n");
2905 
2906  // Enable/Disable AAM
2907  if (options.set_aam) {
2908  if (options.set_aam > 0) {
2909  if (!ata_set_features(device, ATA_ENABLE_AAM, options.set_aam-1)) {
2910  pout("AAM enable failed: %s\n", device->get_errmsg());
2911  returnval |= FAILSMART;
2912  }
2913  else
2914  print_aam_level("AAM set to level ", options.set_aam-1);
2915  }
2916  else {
2917  if (!ata_set_features(device, ATA_DISABLE_AAM)) {
2918  pout("AAM disable failed: %s\n", device->get_errmsg());
2919  returnval |= FAILSMART;
2920  }
2921  else
2922  pout("AAM disabled\n");
2923  }
2924  }
2925 
2926  // Enable/Disable APM
2927  if (options.set_apm) {
2928  if (options.set_apm > 0) {
2929  if (!ata_set_features(device, ATA_ENABLE_APM, options.set_apm-1)) {
2930  pout("APM enable failed: %s\n", device->get_errmsg());
2931  returnval |= FAILSMART;
2932  }
2933  else
2934  print_apm_level("APM set to level ", options.set_apm-1);
2935  }
2936  else {
2937  if (!ata_set_features(device, ATA_DISABLE_APM)) {
2938  pout("APM disable failed: %s\n", device->get_errmsg());
2939  returnval |= FAILSMART;
2940  }
2941  else
2942  pout("APM disabled\n");
2943  }
2944  }
2945 
2946  // Enable/Disable read look-ahead
2947  if (options.set_lookahead) {
2948  bool enable = (options.set_lookahead > 0);
2950  pout("Read look-ahead %sable failed: %s\n", (enable ? "en" : "dis"), device->get_errmsg());
2951  returnval |= FAILSMART;
2952  }
2953  else
2954  pout("Read look-ahead %sabled\n", (enable ? "en" : "dis"));
2955  }
2956 
2957  // Enable/Disable write cache
2958  if (options.set_wcache) {
2959  bool enable = (options.set_wcache > 0);
2961  pout("Write cache %sable failed: %s\n", (enable ? "en" : "dis"), device->get_errmsg());
2962  returnval |= FAILSMART;
2963  }
2964  else
2965  pout("Write cache %sabled\n", (enable ? "en" : "dis"));
2966  }
2967 
2968  // Enable/Disable DSN
2969  if (options.set_dsn) {
2970  bool enable = (options.set_dsn > 0);
2971  if (!ata_set_features(device, ATA_ENABLE_DISABLE_DSN, (enable ? 0x1 : 0x2))) {
2972  pout("DSN %sable failed: %s\n", (enable ? "en" : "dis"), device->get_errmsg());
2973  returnval |= FAILSMART;
2974  }
2975  else
2976  pout("DSN %sabled\n", (enable ? "en" : "dis"));
2977  }
2978 
2979  // Enable/Disable write cache reordering
2980  if (options.sct_wcache_reorder_set) {
2981  bool enable = (options.sct_wcache_reorder_set > 0);
2982  if (!isSCTFeatureControlCapable(&drive))
2983  pout("Write cache reordering %sable failed: SCT Feature Control command not supported\n",
2984  (enable ? "en" : "dis"));
2985  else if (locked)
2986  pout("Write cache reordering %sable failed: SCT not supported if ATA Security is LOCKED\n",
2987  (enable ? "en" : "dis"));
2988  else if (ataGetSetSCTWriteCacheReordering(device,
2989  enable, options.sct_wcache_reorder_set_pers, true /*set*/) < 0) {
2990  pout("Write cache reordering %sable failed: %s\n", (enable ? "en" : "dis"), device->get_errmsg());
2991  returnval |= FAILSMART;
2992  }
2993  else
2994  pout("Write cache reordering %sabled (%s)\n", (enable ? "en" : "dis"),
2995  (options.sct_wcache_reorder_set_pers ? "persistent" : "volatile"));
2996  }
2997 
2998  // Enable/Disable write cache in SCT
2999  if (options.sct_wcache_sct_set) {
3000  if (!isSCTFeatureControlCapable(&drive))
3001  pout("SCT Feature Control of write cache failed: SCT Feature Control command not supported\n");
3002  else if (locked)
3003  pout("SCT Feature Control of write cache failed: SCT not supported if ATA Security is LOCKED\n");
3004  else if (ataGetSetSCTWriteCache(device,
3005  options.sct_wcache_sct_set, options.sct_wcache_sct_set_pers, true /*set*/) < 0) {
3006  pout("SCT Feature Control of write cache failed: %s\n", device->get_errmsg());
3007  returnval |= FAILSMART;
3008  }
3009  else
3010  pout("Write cache SCT Feature Control is set to: %s (%s)\n",
3011  sct_write_cache_state_desc[options.sct_wcache_sct_set],
3012  (options.sct_wcache_sct_set_pers ? "persistent" : "volatile"));
3013  }
3014 
3015  // Freeze ATA security
3016  if (options.set_security_freeze) {
3018  pout("ATA SECURITY FREEZE LOCK failed: %s\n", device->get_errmsg());
3019  returnval |= FAILSMART;
3020  }
3021  else
3022  pout("ATA Security set to frozen mode\n");
3023  }
3024 
3025  // Set standby timer unless immediate standby is also requested
3026  if (options.set_standby && !options.set_standby_now) {
3027  if (!ata_nodata_command(device, ATA_IDLE, options.set_standby-1)) {
3028  pout("ATA IDLE command failed: %s\n", device->get_errmsg());
3029  returnval |= FAILSMART;
3030  }
3031  else
3032  print_standby_timer("Standby timer set to ", options.set_standby-1, drive);
3033  }
3034 
3035  // Enable/Disable SMART commands
3036  if (options.smart_enable) {
3037  if (ataEnableSmart(device)) {
3038  pout("SMART Enable failed: %s\n\n", device->get_errmsg());
3039  failuretest(MANDATORY_CMD, returnval|=FAILSMART);
3040  }
3041  else {
3042  pout("SMART Enabled.\n");
3043  smart_enabled = 1;
3044  }
3045  }
3046 
3047  // Turn off SMART on device
3048  if (options.smart_disable) {
3049  if (ataDisableSmart(device)) {
3050  pout("SMART Disable failed: %s\n\n", device->get_errmsg());
3051  failuretest(MANDATORY_CMD,returnval|=FAILSMART);
3052  }
3053  }
3054 
3055  // Exit if SMART is disabled but must be enabled to proceed
3056  if (options.smart_disable || (smart_enabled <= 0 && need_smart_enabled && !is_permissive())) {
3057  pout("SMART Disabled. Use option -s with argument 'on' to enable it.\n");
3058  if (!options.smart_disable)
3059  pout("(override with '-T permissive' option)\n");
3060  return returnval;
3061  }
3062 
3063  // Enable/Disable Auto-save attributes
3064  if (options.smart_auto_save_enable) {
3065  if (ataEnableAutoSave(device)){
3066  pout("SMART Enable Attribute Autosave failed: %s\n\n", device->get_errmsg());
3067  failuretest(MANDATORY_CMD, returnval|=FAILSMART);
3068  }
3069  else
3070  pout("SMART Attribute Autosave Enabled.\n");
3071  }
3072 
3073  if (options.smart_auto_save_disable) {
3074  if (ataDisableAutoSave(device)){
3075  pout("SMART Disable Attribute Autosave failed: %s\n\n", device->get_errmsg());
3076  failuretest(MANDATORY_CMD, returnval|=FAILSMART);
3077  }
3078  else
3079  pout("SMART Attribute Autosave Disabled.\n");
3080  }
3081 
3082  // Read SMART values and thresholds if necessary
3083  ata_smart_values smartval; memset(&smartval, 0, sizeof(smartval));
3084  ata_smart_thresholds_pvt smartthres; memset(&smartthres, 0, sizeof(smartthres));
3085  bool smart_val_ok = false, smart_thres_ok = false;
3086 
3087  if (need_smart_val) {
3088  if (ataReadSmartValues(device, &smartval)) {
3089  pout("Read SMART Data failed: %s\n\n", device->get_errmsg());
3090  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3091  }
3092  else {
3093  smart_val_ok = true;
3094 
3095  if (options.smart_check_status || options.smart_vendor_attrib) {
3096  if (ataReadSmartThresholds(device, &smartthres)){
3097  pout("Read SMART Thresholds failed: %s\n\n", device->get_errmsg());
3098  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3099  }
3100  else
3101  smart_thres_ok = true;
3102  }
3103  }
3104  }
3105 
3106  // Enable/Disable Off-line testing
3107  bool needupdate = false;
3108  if (options.smart_auto_offl_enable) {
3109  if (!isSupportAutomaticTimer(&smartval)){
3110  pout("SMART Automatic Timers not supported\n\n");
3111  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3112  }
3113  needupdate = smart_val_ok;
3114  if (ataEnableAutoOffline(device)){
3115  pout("SMART Enable Automatic Offline failed: %s\n\n", device->get_errmsg());
3116  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3117  }
3118  else
3119  pout("SMART Automatic Offline Testing Enabled every four hours.\n");
3120  }
3121 
3122  if (options.smart_auto_offl_disable) {
3123  if (!isSupportAutomaticTimer(&smartval)){
3124  pout("SMART Automatic Timers not supported\n\n");
3125  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3126  }
3127  needupdate = smart_val_ok;
3128  if (ataDisableAutoOffline(device)){
3129  pout("SMART Disable Automatic Offline failed: %s\n\n", device->get_errmsg());
3130  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3131  }
3132  else
3133  pout("SMART Automatic Offline Testing Disabled.\n");
3134  }
3135 
3136  if (needupdate && ataReadSmartValues(device, &smartval)){
3137  pout("Read SMART Data failed: %s\n\n", device->get_errmsg());
3138  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3139  smart_val_ok = false;
3140  }
3141 
3142  // all this for a newline!
3143  if ( options.smart_disable || options.smart_enable
3144  || options.smart_auto_save_disable || options.smart_auto_save_enable
3145  || options.smart_auto_offl_disable || options.smart_auto_offl_enable
3146  || options.set_aam || options.set_apm || options.set_lookahead
3147  || options.set_wcache || options.set_security_freeze || options.set_standby
3148  || options.sct_wcache_reorder_set || options.set_dsn)
3149  pout("\n");
3150 
3151  // START OF READ-ONLY OPTIONS APART FROM -V and -i
3152  if ( options.smart_check_status || options.smart_general_values
3153  || options.smart_vendor_attrib || options.smart_error_log
3154  || options.smart_selftest_log || options.smart_selective_selftest_log
3155  || options.smart_ext_error_log || options.smart_ext_selftest_log
3156  || options.sct_temp_sts || options.sct_temp_hist )
3157  pout("=== START OF READ SMART DATA SECTION ===\n");
3158 
3159  // Check SMART status
3160  if (options.smart_check_status) {
3161 
3162  switch (ataSmartStatus2(device)) {
3163 
3164  case 0:
3165  // The case where the disk health is OK
3166  pout("SMART overall-health self-assessment test result: PASSED\n");
3167  if (smart_thres_ok && find_failed_attr(&smartval, &smartthres, attribute_defs, 0)) {
3168  if (options.smart_vendor_attrib)
3169  pout("See vendor-specific Attribute list for marginal Attributes.\n\n");
3170  else {
3171  print_on();
3172  pout("Please note the following marginal Attributes:\n");
3173  PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm, 2, options.output_format);
3174  }
3175  returnval|=FAILAGE;
3176  }
3177  else
3178  pout("\n");
3179  break;
3180 
3181  case 1:
3182  // The case where the disk health is NOT OK
3183  print_on();
3184  pout("SMART overall-health self-assessment test result: FAILED!\n"
3185  "Drive failure expected in less than 24 hours. SAVE ALL DATA.\n");
3186  print_off();
3187  if (smart_thres_ok && find_failed_attr(&smartval, &smartthres, attribute_defs, 1)) {
3188  returnval|=FAILATTR;
3189  if (options.smart_vendor_attrib)
3190  pout("See vendor-specific Attribute list for failed Attributes.\n\n");
3191  else {
3192  print_on();
3193  pout("Failed Attributes:\n");
3194  PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm, 1, options.output_format);
3195  }
3196  }
3197  else
3198  pout("No failed Attributes found.\n\n");
3199  returnval|=FAILSTATUS;
3200  print_off();
3201  break;
3202 
3203  case -1:
3204  default:
3205  // Something went wrong with the SMART STATUS command.
3206  // The ATA SMART RETURN STATUS command provides the result in the ATA output
3207  // registers. Buggy ATA/SATA drivers and SAT Layers often do not properly
3208  // return the registers values.
3209  pout("SMART Status %s: %s\n",
3210  (device->is_syscall_unsup() ? "not supported" : "command failed"),
3211  device->get_errmsg());
3212  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3213 
3214  if (!(smart_val_ok && smart_thres_ok)) {
3215  print_on();
3216  pout("SMART overall-health self-assessment test result: UNKNOWN!\n"
3217  "SMART Status, Attributes and Thresholds cannot be read.\n\n");
3218  }
3219  else if (find_failed_attr(&smartval, &smartthres, attribute_defs, 1)) {
3220  print_on();
3221  pout("SMART overall-health self-assessment test result: FAILED!\n"
3222  "Drive failure expected in less than 24 hours. SAVE ALL DATA.\n");
3223  pout("Warning: This result is based on an Attribute check.\n");
3224  print_off();
3225  returnval|=FAILATTR;
3226  returnval|=FAILSTATUS;
3227  if (options.smart_vendor_attrib)
3228  pout("See vendor-specific Attribute list for failed Attributes.\n\n");
3229  else {
3230  print_on();
3231  pout("Failed Attributes:\n");
3232  PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm, 1, options.output_format);
3233  }
3234  }
3235  else {
3236  pout("SMART overall-health self-assessment test result: PASSED\n");
3237  pout("Warning: This result is based on an Attribute check.\n");
3238  if (find_failed_attr(&smartval, &smartthres, attribute_defs, 0)) {
3239  if (options.smart_vendor_attrib)
3240  pout("See vendor-specific Attribute list for marginal Attributes.\n\n");
3241  else {
3242  print_on();
3243  pout("Please note the following marginal Attributes:\n");
3244  PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm, 2, options.output_format);
3245  }
3246  returnval|=FAILAGE;
3247  }
3248  else
3249  pout("\n");
3250  }
3251  print_off();
3252  break;
3253  } // end of switch statement
3254 
3255  print_off();
3256  } // end of checking SMART Status
3257 
3258  // Print general SMART values
3259  if (smart_val_ok && options.smart_general_values)
3260  PrintGeneralSmartValues(&smartval, &drive, firmwarebugs);
3261 
3262  // Print vendor-specific attributes
3263  if (smart_val_ok && options.smart_vendor_attrib) {
3264  print_on();
3265  PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm,
3266  (printing_is_switchable ? 2 : 0), options.output_format);
3267  print_off();
3268  }
3269 
3270  // If GP Log is supported use smart log directory for
3271  // error and selftest log support check.
3272  bool gp_log_supported = !!isGeneralPurposeLoggingCapable(&drive);
3273  if ( gp_log_supported
3274  && ( options.smart_error_log || options.smart_selftest_log
3275  || options.retry_error_log || options.retry_selftest_log))
3276  need_smart_logdir = true;
3277 
3278  ata_smart_log_directory smartlogdir_buf, gplogdir_buf;
3279  const ata_smart_log_directory * smartlogdir = 0, * gplogdir = 0;
3280 
3281  // Read SMART Log directory
3282  if (need_smart_logdir) {
3283  if (firmwarebugs.is_set(BUG_NOLOGDIR))
3284  smartlogdir = fake_logdir(&smartlogdir_buf, options);
3285  else if (ataReadLogDirectory(device, &smartlogdir_buf, false)) {
3286  pout("Read SMART Log Directory failed: %s\n\n", device->get_errmsg());
3287  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3288  }
3289  else
3290  smartlogdir = &smartlogdir_buf;
3291  }
3292 
3293  // Read GP Log directory
3294  if (need_gp_logdir) {
3295  if (firmwarebugs.is_set(BUG_NOLOGDIR))
3296  gplogdir = fake_logdir(&gplogdir_buf, options);
3297  else if (!gp_log_supported && !is_permissive()) {
3298  if (options.gp_logdir)
3299  pout("General Purpose Log Directory not supported\n\n");
3300  }
3301  else if (ataReadLogDirectory(device, &gplogdir_buf, true)) {
3302  pout("Read GP Log Directory failed\n\n");
3303  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3304  }
3305  else
3306  gplogdir = &gplogdir_buf;
3307  }
3308 
3309  // Print log directories
3310  if ((options.gp_logdir && gplogdir) || (options.smart_logdir && smartlogdir)) {
3311  if (firmwarebugs.is_set(BUG_NOLOGDIR))
3312  pout("Log Directories not read due to '-F nologdir' option\n\n");
3313  else
3314  PrintLogDirectories(gplogdir, smartlogdir);
3315  }
3316 
3317  // Print log pages
3318  for (i = 0; i < options.log_requests.size(); i++) {
3319  const ata_log_request & req = options.log_requests[i];
3320 
3321  const char * type;
3322  unsigned max_nsectors;
3323  if (req.gpl) {
3324  type = "General Purpose";
3325  max_nsectors = GetNumLogSectors(gplogdir, req.logaddr, true);
3326  }
3327  else {
3328  type = "SMART";
3329  max_nsectors = GetNumLogSectors(smartlogdir, req.logaddr, false);
3330  }
3331 
3332  if (!max_nsectors) {
3333  if (!is_permissive()) {
3334  pout("%s Log 0x%02x does not exist (override with '-T permissive' option)\n", type, req.logaddr);
3335  continue;
3336  }
3337  max_nsectors = req.page+1;
3338  }
3339  if (max_nsectors <= req.page) {
3340  pout("%s Log 0x%02x has only %u sectors, output skipped\n", type, req.logaddr, max_nsectors);
3341  continue;
3342  }
3343 
3344  unsigned ns = req.nsectors;
3345  if (ns > max_nsectors - req.page) {
3346  if (req.nsectors != ~0U) // "FIRST-max"
3347  pout("%s Log 0x%02x has only %u sectors, output truncated\n", type, req.logaddr, max_nsectors);
3348  ns = max_nsectors - req.page;
3349  }
3350 
3351  // SMART log don't support sector offset, start with first sector
3352  unsigned offs = (req.gpl ? 0 : req.page);
3353 
3354  raw_buffer log_buf((offs + ns) * 512);
3355  bool ok;
3356  if (req.gpl)
3357  ok = ataReadLogExt(device, req.logaddr, 0x00, req.page, log_buf.data(), ns);
3358  else
3359  ok = ataReadSmartLog(device, req.logaddr, log_buf.data(), offs + ns);
3360  if (!ok)
3361  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3362  else
3363  PrintLogPages(type, log_buf.data() + offs*512, req.logaddr, req.page, ns, max_nsectors);
3364  }
3365 
3366  // Print SMART Extendend Comprehensive Error Log
3367  bool do_smart_error_log = options.smart_error_log;
3368  if (options.smart_ext_error_log) {
3369  bool ok = false;
3370  unsigned nsectors = GetNumLogSectors(gplogdir, 0x03, true);
3371  if (!nsectors)
3372  pout("SMART Extended Comprehensive Error Log (GP Log 0x03) not supported\n\n");
3373  else {
3374  // Read only first sector to get error count and index
3375  // Print function will read more sectors as needed
3376  ata_smart_exterrlog log_03; memset(&log_03, 0, sizeof(log_03));
3377  if (!ataReadExtErrorLog(device, &log_03, 0, 1, firmwarebugs)) {
3378  pout("Read SMART Extended Comprehensive Error Log failed\n\n");
3379  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3380  }
3381  else {
3382  if (PrintSmartExtErrorLog(device, firmwarebugs, &log_03, nsectors, options.smart_ext_error_log))
3383  returnval |= FAILERR;
3384  ok = true;
3385  }
3386  }
3387 
3388  if (!ok) {
3389  if (options.retry_error_log)
3390  do_smart_error_log = true;
3391  else if (!do_smart_error_log)
3392  pout("Try '-l [xerror,]error' to read traditional SMART Error Log\n");
3393  }
3394  }
3395 
3396  // Print SMART error log
3397  if (do_smart_error_log) {
3398  if (!( GetNumLogSectors(smartlogdir, 0x01, false)
3399  || ( !(smartlogdir && gp_log_supported)
3400  && isSmartErrorLogCapable(&smartval, &drive))
3401  || is_permissive() )) {
3402  pout("SMART Error Log not supported\n\n");
3403  }
3404  else {
3405  ata_smart_errorlog smarterror; memset(&smarterror, 0, sizeof(smarterror));
3406  if (ataReadErrorLog(device, &smarterror, firmwarebugs)) {
3407  pout("Read SMART Error Log failed: %s\n\n", device->get_errmsg());
3408  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3409  }
3410  else {
3411  // quiet mode is turned on inside PrintSmartErrorLog()
3412  if (PrintSmartErrorlog(&smarterror, firmwarebugs))
3413  returnval|=FAILERR;
3414  print_off();
3415  }
3416  }
3417  }
3418 
3419  // Print SMART Extendend Self-test Log
3420  bool do_smart_selftest_log = options.smart_selftest_log;
3421  if (options.smart_ext_selftest_log) {
3422  bool ok = false;
3423  unsigned nsectors = GetNumLogSectors(gplogdir, 0x07, true);
3424  if (!nsectors)
3425  pout("SMART Extended Self-test Log (GP Log 0x07) not supported\n\n");
3426  else if (nsectors >= 256)
3427  pout("SMART Extended Self-test Log size %u not supported\n\n", nsectors);
3428  else {
3429  raw_buffer log_07_buf(nsectors * 512);
3430  ata_smart_extselftestlog * log_07 = reinterpret_cast<ata_smart_extselftestlog *>(log_07_buf.data());
3431  if (!ataReadExtSelfTestLog(device, log_07, nsectors)) {
3432  pout("Read SMART Extended Self-test Log failed\n\n");
3433  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3434  }
3435  else {
3436  if (PrintSmartExtSelfTestLog(log_07, nsectors, options.smart_ext_selftest_log))
3437  returnval |= FAILLOG;
3438  ok = true;
3439  }
3440  }
3441 
3442  if (!ok) {
3443  if (options.retry_selftest_log)
3444  do_smart_selftest_log = true;
3445  else if (!do_smart_selftest_log)
3446  pout("Try '-l [xselftest,]selftest' to read traditional SMART Self Test Log\n");
3447  }
3448  }
3449 
3450  // Print SMART self-test log
3451  if (do_smart_selftest_log) {
3452  if (!( GetNumLogSectors(smartlogdir, 0x06, false)
3453  || ( !(smartlogdir && gp_log_supported)
3454  && isSmartTestLogCapable(&smartval, &drive))
3455  || is_permissive() )) {
3456  pout("SMART Self-test Log not supported\n\n");
3457  }
3458  else {
3459  ata_smart_selftestlog smartselftest; memset(&smartselftest, 0, sizeof(smartselftest));
3460  if (ataReadSelfTestLog(device, &smartselftest, firmwarebugs)) {
3461  pout("Read SMART Self-test Log failed: %s\n\n", device->get_errmsg());
3462  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3463  }
3464  else {
3465  print_on();
3466  if (ataPrintSmartSelfTestlog(&smartselftest, !printing_is_switchable, firmwarebugs))
3467  returnval |= FAILLOG;
3468  print_off();
3469  pout("\n");
3470  }
3471  }
3472  }
3473 
3474  // Print SMART selective self-test log
3475  if (options.smart_selective_selftest_log) {
3477 
3478  if (!isSupportSelectiveSelfTest(&smartval))
3479  pout("Selective Self-tests/Logging not supported\n\n");
3480  else if(ataReadSelectiveSelfTestLog(device, &log)) {
3481  pout("Read SMART Selective Self-test Log failed: %s\n\n", device->get_errmsg());
3482  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3483  }
3484  else {
3485  print_on();
3486  // If any errors were found, they are logged in the SMART Self-test log.
3487  // So there is no need to print the Selective Self Test log in silent
3488  // mode.
3490  ataPrintSelectiveSelfTestLog(&log, &smartval);
3491  print_off();
3492  pout("\n");
3493  }
3494  }
3495 
3496  // Check if SCT commands available
3497  bool sct_ok = isSCTCapable(&drive);
3498  if ( options.sct_temp_sts || options.sct_temp_hist || options.sct_temp_int
3499  || options.sct_erc_get || options.sct_erc_set ) {
3500  if (!sct_ok)
3501  pout("SCT Commands not supported\n\n");
3502  else if (locked) {
3503  pout("SCT Commands not supported if ATA Security is LOCKED\n\n");
3504  sct_ok = false;
3505  }
3506  }
3507 
3508  // Print SCT status and temperature history table
3509  if (sct_ok && (options.sct_temp_sts || options.sct_temp_hist || options.sct_temp_int)) {
3510  for (;;) {
3511  bool sct_temp_hist_ok = isSCTDataTableCapable(&drive);
3513 
3514  if (options.sct_temp_sts || (options.sct_temp_hist && sct_temp_hist_ok)) {
3515  // Read SCT status
3516  if (ataReadSCTStatus(device, &sts)) {
3517  pout("\n");
3518  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3519  break;
3520  }
3521  if (options.sct_temp_sts) {
3522  ataPrintSCTStatus(&sts);
3523  pout("\n");
3524  }
3525  }
3526 
3527  if (!sct_temp_hist_ok && (options.sct_temp_hist || options.sct_temp_int)) {
3528  pout("SCT Data Table command not supported\n\n");
3529  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3530  break;
3531  }
3532 
3533  if (options.sct_temp_hist) {
3534  // Read SCT temperature history,
3535  // requires initial SCT status from above
3537  if (ataReadSCTTempHist(device, &tmh, &sts)) {
3538  pout("Read SCT Temperature History failed\n\n");
3539  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3540  break;
3541  }
3542  ataPrintSCTTempHist(&tmh);
3543  pout("\n");
3544  }
3545 
3546  if (options.sct_temp_int) {
3547  // Set new temperature logging interval
3548  if (!isSCTFeatureControlCapable(&drive)) {
3549  pout("SCT Feature Control command not supported\n\n");
3550  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3551  break;
3552  }
3553  if (ataSetSCTTempInterval(device, options.sct_temp_int, options.sct_temp_int_pers)) {
3554  pout("Write Temperature Logging Interval failed\n\n");
3555  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3556  break;
3557  }
3558  pout("Temperature Logging Interval set to %u minute%s (%s)\n",
3559  options.sct_temp_int, (options.sct_temp_int == 1 ? "" : "s"),
3560  (options.sct_temp_int_pers ? "persistent" : "volatile"));
3561  }
3562  break;
3563  }
3564  }
3565 
3566  // SCT Error Recovery Control
3567  if (sct_ok && (options.sct_erc_get || options.sct_erc_set)) {
3568  if (!isSCTErrorRecoveryControlCapable(&drive)) {
3569  pout("SCT Error Recovery Control command not supported\n\n");
3570  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3571  }
3572  else {
3573  bool sct_erc_get = options.sct_erc_get;
3574  if (options.sct_erc_set) {
3575  // Set SCT Error Recovery Control
3576  if ( ataSetSCTErrorRecoveryControltime(device, 1, options.sct_erc_readtime )
3577  || ataSetSCTErrorRecoveryControltime(device, 2, options.sct_erc_writetime)) {
3578  pout("SCT (Set) Error Recovery Control command failed\n");
3579  if (!( (options.sct_erc_readtime == 70 && options.sct_erc_writetime == 70)
3580  || (options.sct_erc_readtime == 0 && options.sct_erc_writetime == 0)))
3581  pout("Retry with: 'scterc,70,70' to enable ERC or 'scterc,0,0' to disable\n");
3582  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3583  sct_erc_get = false;
3584  }
3585  else if (!sct_erc_get)
3587  options.sct_erc_writetime);
3588  }
3589 
3590  if (sct_erc_get) {
3591  // Print SCT Error Recovery Control
3592  unsigned short read_timer, write_timer;
3593  if ( ataGetSCTErrorRecoveryControltime(device, 1, read_timer )
3594  || ataGetSCTErrorRecoveryControltime(device, 2, write_timer)) {
3595  pout("SCT (Get) Error Recovery Control command failed\n");
3596  if (options.sct_erc_set) {
3597  pout("The previous SCT (Set) Error Recovery Control command succeeded\n");
3599  options.sct_erc_writetime);
3600  }
3601  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3602  }
3603  else
3604  ataPrintSCTErrorRecoveryControl(false, read_timer, write_timer);
3605  }
3606  pout("\n");
3607  }
3608  }
3609 
3610  // Print Device Statistics
3611  if (options.devstat_all_pages || options.devstat_ssd_page || !options.devstat_pages.empty()) {
3612  bool use_gplog = true;
3613  unsigned nsectors = 0;
3614  if (gplogdir)
3615  nsectors = GetNumLogSectors(gplogdir, 0x04, false);
3616  else if (smartlogdir){ // for systems without ATA_READ_LOG_EXT
3617  nsectors = GetNumLogSectors(smartlogdir, 0x04, false);
3618  use_gplog = false;
3619  }
3620  if (!nsectors)
3621  pout("Device Statistics (GP/SMART Log 0x04) not supported\n\n");
3622  else if (!print_device_statistics(device, nsectors, options.devstat_pages,
3623  options.devstat_all_pages, options.devstat_ssd_page, use_gplog))
3624  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3625  }
3626 
3627  // Print SATA Phy Event Counters
3628  if (options.sataphy) {
3629  unsigned nsectors = GetNumLogSectors(gplogdir, 0x11, true);
3630  // Packet interface devices do not provide a log directory, check support bit
3631  if (!nsectors && (drive.words047_079[76-47] & 0x0401) == 0x0400)
3632  nsectors = 1;
3633  if (!nsectors)
3634  pout("SATA Phy Event Counters (GP Log 0x11) not supported\n\n");
3635  else if (nsectors != 1)
3636  pout("SATA Phy Event Counters with %u sectors not supported\n\n", nsectors);
3637  else {
3638  unsigned char log_11[512] = {0, };
3639  unsigned char features = (options.sataphy_reset ? 0x01 : 0x00);
3640  if (!ataReadLogExt(device, 0x11, features, 0, log_11, 1)) {
3641  pout("Read SATA Phy Event Counters failed\n\n");
3642  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3643  }
3644  else
3645  PrintSataPhyEventCounters(log_11, options.sataphy_reset);
3646  }
3647  }
3648 
3649  // Set to standby (spindown) mode and set standby timer if not done above
3650  // (Above commands may spinup drive)
3651  if (options.set_standby_now) {
3652  if (options.set_standby) {
3653  if (!ata_nodata_command(device, ATA_STANDBY, options.set_standby-1)) {
3654  pout("ATA STANDBY command failed: %s\n", device->get_errmsg());
3655  returnval |= FAILSMART;
3656  }
3657  else {
3658  print_standby_timer("Standby timer set to ", options.set_standby-1, drive);
3659  pout("Device placed in STANDBY mode\n");
3660  }
3661  }
3662  else {
3663  if (!ata_nodata_command(device, ATA_STANDBY_IMMEDIATE)) {
3664  pout("ATA STANDBY IMMEDIATE command failed: %s\n", device->get_errmsg());
3665  returnval |= FAILSMART;
3666  }
3667  else
3668  pout("Device placed in STANDBY mode\n");
3669  }
3670  }
3671 
3672  // START OF THE TESTING SECTION OF THE CODE. IF NO TESTING, RETURN
3673  if (!smart_val_ok || options.smart_selftest_type == -1)
3674  return returnval;
3675 
3676  pout("=== START OF OFFLINE IMMEDIATE AND SELF-TEST SECTION ===\n");
3677  // if doing a self-test, be sure it's supported by the hardware
3678  switch (options.smart_selftest_type) {
3679  case OFFLINE_FULL_SCAN:
3680  if (!isSupportExecuteOfflineImmediate(&smartval)){
3681  pout("Execute Offline Immediate function not supported\n\n");
3682  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3683  }
3684  break;
3685  case ABORT_SELF_TEST:
3686  case SHORT_SELF_TEST:
3687  case EXTEND_SELF_TEST:
3690  if (!isSupportSelfTest(&smartval)){
3691  pout("Self-test functions not supported\n\n");
3692  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3693  }
3694  break;
3695  case CONVEYANCE_SELF_TEST:
3697  if (!isSupportConveyanceSelfTest(&smartval)){
3698  pout("Conveyance Self-test functions not supported\n\n");
3699  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3700  }
3701  break;
3702  case SELECTIVE_SELF_TEST:
3704  if (!isSupportSelectiveSelfTest(&smartval)){
3705  pout("Selective Self-test functions not supported\n\n");
3706  failuretest(MANDATORY_CMD, returnval|=FAILSMART);
3707  }
3708  break;
3709  default:
3710  break; // Vendor specific type
3711  }
3712 
3713  // Now do the test. Note ataSmartTest prints its own error/success
3714  // messages
3715  if (ataSmartTest(device, options.smart_selftest_type, options.smart_selftest_force,
3716  options.smart_selective_args, &smartval, sizes.sectors ))
3717  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3718  else {
3719  // Tell user how long test will take to complete. This is tricky
3720  // because in the case of an Offline Full Scan, the completion
3721  // timer is volatile, and needs to be read AFTER the command is
3722  // given. If this will interrupt the Offline Full Scan, we don't
3723  // do it, just warn user.
3724  if (options.smart_selftest_type == OFFLINE_FULL_SCAN) {
3725  if (isSupportOfflineAbort(&smartval))
3726  pout("Note: giving further SMART commands will abort Offline testing\n");
3727  else if (ataReadSmartValues(device, &smartval)){
3728  pout("Read SMART Data failed: %s\n\n", device->get_errmsg());
3729  failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
3730  }
3731  }
3732 
3733  // Now say how long the test will take to complete
3734  int timewait = TestTime(&smartval, options.smart_selftest_type);
3735  if (timewait) {
3736  time_t t=time(NULL);
3737  if (options.smart_selftest_type == OFFLINE_FULL_SCAN) {
3738  t+=timewait;
3739  pout("Please wait %d seconds for test to complete.\n", (int)timewait);
3740  } else {
3741  t+=timewait*60;
3742  pout("Please wait %d minutes for test to complete.\n", (int)timewait);
3743  }
3744  pout("Test will complete after %s\n", ctime(&t));
3745 
3750  pout("Use smartctl -X to abort test.\n");
3751  }
3752  }
3753 
3754  return returnval;
3755 }
static const char * get_sata_version(unsigned short word222)
Definition: ataprint.cpp:549
#define CONVEYANCE_CAPTIVE_SELF_TEST
Definition: atacmds.h:124
unsigned short minor_rev_num
Definition: atacmds.h:142
unsigned char self_test_status
Definition: atacmds.h:449
unsigned char errorlog_capability
Definition: atacmds.h:226
int isSupportSelectiveSelfTest(const ata_smart_values *data)
Definition: atacmds.cpp:1851
#define PRIu64
Definition: int64.h:76
int ataIsSmartEnabled(const ata_identify_device *drive)
Definition: atacmds.cpp:972
unsigned short words047_079[33]
Definition: atacmds.h:140
const char * packetdevicetype(int type)
Definition: utility.cpp:265
u16 flags
Definition: megaraid.h:93
const drive_settings * lookup_drive_apply_presets(const ata_identify_device *drive, ata_vendor_attr_defs &defs, firmwarebug_defs &firmwarebugs)
u16 s[6]
Definition: megaraid.h:97
const devstat_entry_info devstat_info_0x06[]
Definition: ataprint.cpp:1462
static const char * get_log_rw(unsigned logaddr)
Definition: ataprint.cpp:1251
static void PrintSmartOfflineStatus(const ata_smart_values *data)
Definition: ataprint.cpp:774
bool sct_wcache_sct_get
Definition: ataprint.h:113
bool ataReadExtErrorLog(ata_device *device, ata_smart_exterrlog *log, unsigned page, unsigned nsectors, firmwarebug_defs firmwarebugs)
Definition: atacmds.cpp:1512
unsigned page
Definition: ataprint.h:37
unsigned phy_sector_size
Definition: atacmds.h:1008
static void PrintLogPages(const char *type, const unsigned char *data, unsigned char logaddr, unsigned page, unsigned num_pages, unsigned max_pages)
Definition: ataprint.cpp:1358
uint64_t end
Definition: atacmds.h:496
#define NUMBER_ATA_SMART_ATTRIBUTES
Definition: atacmds.h:129
u32 size
Definition: megaraid.h:79
ata_vendor_attr_defs attribute_defs
Definition: ataprint.h:90
bool sct_wcache_sct_set_pers
Definition: ataprint.h:115
unsigned char reserved1
Definition: atacmds.h:464
std::string ata_get_smart_attr_name(unsigned char id, const ata_vendor_attr_defs &defs, int rpm)
Definition: atacmds.cpp:2188
static void PrintSmartAttribWithThres(const ata_smart_values *data, const ata_smart_thresholds_pvt *thresholds, const ata_vendor_attr_defs &defs, int rpm, int onlyfailed, unsigned char format)
Definition: ataprint.cpp:1018
int isSupportExecuteOfflineImmediate(const ata_smart_values *data)
Definition: atacmds.cpp:1821
unsigned char features_register
Definition: atacmds.h:340
firmwarebug_defs firmwarebugs
Definition: ataprint.h:87
struct test_span span[5]
Definition: atacmds.h:504
unsigned char checksum(const void *data)
Definition: atacmds.cpp:722
static const char * get_sata_curspeed(const ata_identify_device *drive)
Definition: ataprint.cpp:591
#define ATA_ENABLE_APM
Definition: atacmds.h:89
unsigned char current
Definition: atacmds.h:161
unsigned short currentspan
Definition: atacmds.h:508
bool smart_error_log
Definition: ataprint.h:53
bool sct_wcache_reorder_get
Definition: ataprint.h:110
int get_errno() const
Get last error number.
ata_smart_exterrlog_error error
Definition: atacmds.h:390
const char * warningmsg
Definition: knowndrives.h:29
int smart_selftest_type
Definition: ataprint.h:77
static const char * get_error_log_state_desc(unsigned state)
Definition: ataprint.cpp:1765
#define FAILERR
Definition: smartctl.h:59
unsigned char self_test_type
Definition: atacmds.h:448
void dateandtimezone(char *buffer)
Definition: utility.cpp:349
bool str_starts_with(const char *str, const char *prefix)
Definition: utility.h:57
unsigned short format_version
Definition: atacmds.h:533
int ata_read_identity(ata_device *device, ata_identify_device *buf, bool fix_swapped_id, unsigned char *raw_buf)
Definition: atacmds.cpp:834
unsigned short int ata_error_count
Definition: atacmds.h:321
#define ATA_DISABLE_READ_LOOK_AHEAD
Definition: atacmds.h:87
const devstat_entry_info devstat_info_0x00[]
Definition: ataprint.cpp:1394
unsigned char device_state
Definition: atacmds.h:537
unsigned char lba_mid_register_hi
Definition: atacmds.h:371
unsigned short smart_status
Definition: atacmds.h:553
unsigned char lba_low_register_hi
Definition: atacmds.h:345
unsigned short error_log_index
Definition: atacmds.h:402
static const char * get_sata_speed(int level)
Definition: ataprint.cpp:568
static int PrintSmartExtErrorLog(ata_device *device, const firmwarebug_defs &firmwarebugs, const ata_smart_exterrlog *log, unsigned nsectors, unsigned max_errors)
Definition: ataprint.cpp:1896
unsigned short word086
Definition: atacmds.h:147
ata_smart_exterrlog_error_log error_logs[4]
Definition: atacmds.h:403
static const char * get_ata_major_version(const ata_identify_device *drive)
Definition: ataprint.cpp:441
int ataReadSelectiveSelfTestLog(ata_device *device, struct ata_selective_self_test_log *data)
Definition: atacmds.cpp:1205
bool smart_selective_selftest_log
Definition: ataprint.h:55
int TestTime(const ata_smart_values *data, int testtype)
Definition: atacmds.cpp:1721
bool smart_disable
Definition: ataprint.h:73
unsigned char device_control_register
Definition: atacmds.h:339
bool sct_temp_hist
Definition: ataprint.h:67
void ata_get_size_info(const ata_identify_device *id, ata_size_info &sizes)
Definition: atacmds.cpp:664
#define ATTRIBUTE_FLAGS_ONLINE(x)
Definition: atacmds.h:193
unsigned char lba_high_register_hi
Definition: atacmds.h:373
int identify_bit_level
Definition: ataprint.h:49
#define ATA_DISABLE_AAM
Definition: atacmds.h:84
static const char * GetLogName(unsigned logaddr)
Definition: ataprint.cpp:1193
#define FAILAGE
Definition: smartctl.h:56
struct ata_smart_log_entry entry[255]
Definition: atacmds.h:486
unsigned char count_register
Definition: atacmds.h:342
unsigned char lba_low_register_hi
Definition: atacmds.h:369
u32 count
Definition: megaraid.h:80
#define ATA_STANDBY
Definition: atacmds.h:80
int ataGetSetSCTWriteCache(ata_device *device, unsigned short state, bool persistent, bool set)
Definition: atacmds.cpp:2417
static unsigned GetNumLogSectors(const ata_smart_log_directory *logdir, unsigned logaddr, bool gpl)
Definition: ataprint.cpp:1177
int ataGetSCTErrorRecoveryControltime(ata_device *device, unsigned type, unsigned short &time_limit)
Definition: atacmds.cpp:2560
bool smart_selftest_force
Definition: ataprint.h:78
#define EXTEND_SELF_TEST
Definition: atacmds.h:118
signed char max_temp
Definition: atacmds.h:547
int ataReadSelfTestLog(ata_device *device, ata_smart_selftestlog *data, firmwarebug_defs firmwarebugs)
Definition: atacmds.cpp:1034
#define FAILSMART
Definition: smartctl.h:46
static const char * get_form_factor(unsigned short word168)
Definition: ataprint.cpp:415
#define snprintf
Definition: utility.h:68
static const char * sct_pbar(int x, char(&buf)[64])
Definition: ataprint.cpp:2267
int ataEnableAutoSave(ata_device *device)
Definition: atacmds.cpp:1572
unsigned char self_test_exec_status
Definition: atacmds.h:221
ata_smart_exterrlog_command commands[5]
Definition: atacmds.h:389
static int ataPrintSCTTempHist(const ata_sct_temperature_history_table *tmh)
Definition: ataprint.cpp:2361
bool ataReadExtSelfTestLog(ata_device *device, ata_smart_extselftestlog *log, unsigned nsectors)
Definition: atacmds.cpp:1082
#define FAILATTR
Definition: smartctl.h:52
uint64_t sectors
Definition: atacmds.h:1005
unsigned char offline_data_collection_status
Definition: atacmds.h:220
bool is_set(firmwarebug_t bug) const
Definition: atacmds.h:747
const char * ataprint_cpp_cvsid
Definition: ataprint.cpp:43
unsigned char lba_high_register_hi
Definition: atacmds.h:349
void print_off()
Definition: smartctl.h:92
ata_attr_state ata_get_attr_state(const ata_smart_attribute &attr, int attridx, const ata_smart_threshold_entry *thresholds, const ata_vendor_attr_defs &defs, unsigned char *threshval)
Definition: atacmds.cpp:1857
struct ata_smart_extselftestlog_desc log_descs[19]
Definition: atacmds.h:466
unsigned short log_desc_index
Definition: atacmds.h:465
static void PrintSmartSelfExecStatus(const ata_smart_values *data, firmwarebug_defs firmwarebugs)
Definition: ataprint.cpp:794
std::vector< int > devstat_pages
Definition: ataprint.h:65
bool ata_nodata_command(ata_device *device, unsigned char command, int sector_count)
Definition: atacmds.cpp:804
unsigned char device_register
Definition: atacmds.h:350
int ataGetSetSCTWriteCacheReordering(ata_device *device, bool enable, bool persistent, bool set)
Definition: atacmds.cpp:2410
bool nonempty(const void *data, int size)
Definition: utility.cpp:633
unsigned short sampling_period
Definition: atacmds.h:609
static std::string format_st_er_desc(unsigned char CR, unsigned char FR, unsigned char ST, unsigned char ER, unsigned short SC, const ata_smart_errorlog_error_struct *lba28_regs, const ata_smart_exterrlog_error *lba48_regs)
Definition: ataprint.cpp:74
void ata_print_identify_data(const void *id, bool all_words, int bit_level)
static void print_drive_info(const ata_identify_device *drive, const ata_size_info &sizes, int rpm, const drive_settings *dbentry)
Definition: ataprint.cpp:600
struct ata_smart_attribute vendor_attributes[NUMBER_ATA_SMART_ATTRIBUTES]
Definition: atacmds.h:219
unsigned sct_erc_writetime
Definition: ataprint.h:70
unsigned short command_set_1
Definition: atacmds.h:143
int ataDisableSmart(ata_device *device)
Definition: atacmds.cpp:1564
unsigned smart_ext_selftest_log
Definition: ataprint.h:59
unsigned char count_register_hi
Definition: atacmds.h:343
unsigned short flags
Definition: atacmds.h:160
ptr_t data
Definition: megaraid.h:94
static const char * sct_ptemp(signed char x, char(&buf)[20])
Definition: ataprint.cpp:2259
int ata_get_rotation_rate(const ata_identify_device *id)
Definition: atacmds.cpp:941
unsigned char device_register
Definition: atacmds.h:374
std::string ata_format_attr_raw_value(const ata_smart_attribute &attr, const ata_vendor_attr_defs &defs)
Definition: atacmds.cpp:1981
unsigned short command_set_2
Definition: atacmds.h:144
#define SELECTIVE_SELF_TEST
Definition: atacmds.h:120
static void print_device_statistics_page(const unsigned char *data, int page)
Definition: ataprint.cpp:1499
const devstat_entry_info devstat_info_0x03[]
Definition: ataprint.cpp:1423
ata_selective_selftest_args smart_selective_args
Definition: ataprint.h:79
unsigned short pendingtime
Definition: atacmds.h:511
uint64_t start
Definition: atacmds.h:495
bool retry_error_log
Definition: ataprint.h:60
unsigned short timestamp
Definition: atacmds.h:379
#define DATEANDEPOCHLEN
Definition: utility.h:80
#define PRId64
Definition: int64.h:72
bool ataReadLogExt(ata_device *device, unsigned char logaddr, unsigned char features, unsigned page, void *data, unsigned nsectors)
Definition: atacmds.cpp:1132
#define ATA_STANDBY_IMMEDIATE
Definition: atacmds.h:81
unsigned char lba_mid_register
Definition: atacmds.h:346
bool fix_swapped_id
Definition: ataprint.h:88
const int num_devstat_infos
Definition: ataprint.cpp:1488
static void PrintSmartCapability(const ata_smart_values *data)
Definition: ataprint.cpp:923
int ataSmartSupport(const ata_identify_device *drive)
Definition: atacmds.cpp:957
unsigned char reserved
Definition: atacmds.h:478
unsigned short int smart_capability
Definition: atacmds.h:225
unsigned log_sector_offset
Definition: atacmds.h:1009
static void PrintSmartExtendedSelfTestPollingTime(const ata_smart_values *data)
Definition: ataprint.cpp:971
unsigned short words088_255[168]
Definition: atacmds.h:149
#define FAILSTATUS
Definition: smartctl.h:49
int ataReadSCTStatus(ata_device *device, ata_sct_status_response *sts)
Definition: atacmds.cpp:2249
bool set_security_freeze
Definition: ataprint.h:107
static void print_apm_level(const char *msg, int level)
Definition: ataprint.cpp:2455
int ataReadSmartValues(ata_device *device, struct ata_smart_values *data)
Definition: atacmds.cpp:988
unsigned char output_format
Definition: ataprint.h:85
unsigned short sct_spec
Definition: atacmds.h:535
unsigned int over_limit_count
Definition: atacmds.h:551
unsigned log_sector_size
Definition: atacmds.h:1007
static const char * get_ata_minor_version(const ata_identify_device *drive)
Definition: ataprint.cpp:464
unsigned short device_error_count
Definition: atacmds.h:404
ata_attr_state
Definition: atacmds.h:906
bool smart_auto_save_enable
Definition: ataprint.h:75
#define ABORT_SELF_TEST
Definition: atacmds.h:121
static void PrintSmartErrorLogCapability(const ata_smart_values *data, const ata_identify_device *identity)
Definition: ataprint.cpp:946
int isSupportOfflineSurfaceScan(const ata_smart_values *data)
Definition: atacmds.cpp:1839
const char * get_errmsg() const
Get last error message.
long long int64_t
Definition: int64.h:51
bool ata_set_features(ata_device *device, unsigned char features, int sector_count)
Definition: atacmds.cpp:816
bool isSCTCapable(const ata_identify_device *drive)
Definition: atacmds.h:891
static int PrintSmartExtSelfTestLog(const ata_smart_extselftestlog *log, unsigned nsectors, unsigned max_entries)
Definition: ataprint.cpp:2065
unsigned char logaddr
Definition: ataprint.h:36
unsigned char lba_low_register
Definition: atacmds.h:368
void print_on()
Definition: smartctl.h:87
unsigned char lba_low_register
Definition: atacmds.h:344
bool dont_print_serial_number
Definition: atacmds.cpp:47
#define ATTRIBUTE_FLAGS_PREFAILURE(x)
Definition: atacmds.h:183
unsigned char id
Definition: atacmds.h:157
unsigned char failuretest_permissive
Definition: smartctl.cpp:1215
static int PrintSmartErrorlog(const ata_smart_errorlog *data, firmwarebug_defs firmwarebugs)
Definition: ataprint.cpp:1781
unsigned char numsectors
Definition: atacmds.h:477
#define FAILID
Definition: smartctl.h:43
struct ata_smart_errorlog_struct errorlog_struct[5]
Definition: atacmds.h:320
bool smart_general_values
Definition: ataprint.h:51
#define ATA_ENABLE_WRITE_CACHE
Definition: atacmds.h:90
unsigned char revnumber
Definition: atacmds.h:318
unsigned short int total_time_to_complete_off_line
Definition: atacmds.h:222
const char * format_capacity(char *str, int strsize, uint64_t val, const char *decimal_point)
Definition: utility.cpp:699
unsigned int timestamp
Definition: atacmds.h:354
int isSupportConveyanceSelfTest(const ata_smart_values *data)
Definition: atacmds.cpp:1847
static void ataPrintSCTCapability(const ata_identify_device *drive)
Definition: ataprint.cpp:1128
void pout(const char *fmt,...)
Definition: smartctl.cpp:1196
#define OFFLINE_FULL_SCAN
Definition: atacmds.h:116
static void PrintLogDirectories(const ata_smart_log_directory *gplogdir, const ata_smart_log_directory *smartlogdir)
Definition: ataprint.cpp:1290
unsigned sct_temp_int
Definition: ataprint.h:81
static int ataPrintSCTStatus(const ata_sct_status_response *sts)
Definition: ataprint.cpp:2303
bool smart_selftest_log
Definition: ataprint.h:54
unsigned char fw_rev[8]
Definition: atacmds.h:138
const devstat_entry_info devstat_info_0x04[]
Definition: ataprint.cpp:1436
short size
Definition: ataprint.cpp:1390
#define ATTRIBUTE_FLAGS_SELFPRESERVING(x)
Definition: atacmds.h:208
int ataEnableAutoOffline(ata_device *device)
Definition: atacmds.cpp:1591
static const char * sct_device_state_msg(unsigned char state)
Definition: ataprint.cpp:2289
unsigned char lba_high_register
Definition: atacmds.h:372
const char * name
Definition: ataprint.cpp:1391
bool sct_temp_int_pers
Definition: ataprint.h:82
bool retry_selftest_log
Definition: ataprint.h:60
int isSupportOfflineAbort(const ata_smart_values *data)
Definition: atacmds.cpp:1835
static void print_aam_level(const char *msg, int level, int recommended=-1)
Definition: ataprint.cpp:2431
const char * modelfamily
Definition: knowndrives.h:26
const devstat_entry_info devstat_info_0x05[]
Definition: ataprint.cpp:1444
struct ata_smart_errorlog_command_struct commands[5]
Definition: atacmds.h:309
#define ATA_ENABLE_DISABLE_DSN
Definition: atacmds.h:92
int ataPrintSmartSelfTestEntry(unsigned testnum, unsigned char test_type, unsigned char test_status, unsigned short timestamp, uint64_t failing_lba, bool print_error_only, bool &print_header)
Definition: atacmds.cpp:2577
bool sct_wcache_reorder_set_pers
Definition: ataprint.h:112
int ataPrintMain(ata_device *device, const ata_print_options &options)
Definition: ataprint.cpp:2547
static const char * infofound(const char *output)
Definition: ataprint.cpp:47
int ataEnableSmart(ata_device *device)
Definition: atacmds.cpp:1557
unsigned char powerexit
Definition: ataprint.h:95
bool smart_auto_offl_disable
Definition: ataprint.h:74
#define CONVEYANCE_SELF_TEST
Definition: atacmds.h:119
static const char * OfflineDataCollectionStatus(unsigned char status_byte)
Definition: ataprint.cpp:744
unsigned char error_register
Definition: atacmds.h:275
int ataReadSmartThresholds(ata_device *device, struct ata_smart_thresholds_pvt *data)
Definition: atacmds.cpp:1539
int ataDisableAutoSave(ata_device *device)
Definition: atacmds.cpp:1579
u8 b[12]
Definition: megaraid.h:96
union @43 entry
#define SELECTIVE_FLAG_PENDING
Definition: atacmds.h:519
unsigned short cfs_enable_1
Definition: atacmds.h:146
int ataSetSCTTempInterval(ata_device *device, unsigned interval, bool persistent)
Definition: atacmds.cpp:2424
unsigned short major_rev_num
Definition: atacmds.h:141
static void PrintSmartTotalTimeCompleteOffline(const ata_smart_values *data)
Definition: ataprint.cpp:876
int isGeneralPurposeLoggingCapable(const ata_identify_device *identity)
Definition: atacmds.cpp:1790
#define ATA_ENABLE_AAM
Definition: atacmds.h:88
#define P(n)
int ata_get_wwn(const ata_identify_device *id, unsigned &oui, uint64_t &unique_id)
Definition: atacmds.cpp:921
void checksumwarning(const char *string)
Definition: smartctl.cpp:1243
bool isSCTDataTableCapable(const ata_identify_device *drive)
Definition: atacmds.h:900
#define EXTEND_CAPTIVE_SELF_TEST
Definition: atacmds.h:123
#define SELECTIVE_CAPTIVE_SELF_TEST
Definition: atacmds.h:125
unsigned char short_test_completion_time
Definition: atacmds.h:228
const ata_smart_log_directory * fake_logdir(ata_smart_log_directory *logdir, const ata_print_options &options)
Definition: ataprint.cpp:1274
static bool is_permissive()
Definition: ataprint.cpp:53
#define ATTRIBUTE_FLAGS_EVENTCOUNT(x)
Definition: atacmds.h:205
unsigned short sct_version
Definition: atacmds.h:534
unsigned char lba_mid_register
Definition: atacmds.h:370
Definition: ataprint.cpp:1388
int sct_wcache_sct_set
Definition: ataprint.h:114
static void PrintSmartShortSelfTestPollingTime(const ata_smart_values *data)
Definition: ataprint.cpp:961
const char * format_with_thousands_sep(char *str, int strsize, uint64_t val, const char *thousands_sep)
Definition: utility.cpp:667
static void print_standby_timer(const char *msg, int timer, const ata_identify_device &drive)
Definition: ataprint.cpp:2513
static int find_msb(unsigned short word)
Definition: ataprint.cpp:433
unsigned char device_control_register
Definition: atacmds.h:364
int ataSmartTest(ata_device *device, int testtype, bool force, const ata_selective_selftest_args &selargs, const ata_smart_values *sv, uint64_t num_sectors)
Definition: atacmds.cpp:1632
void ata_format_id_string(char *out, const unsigned char *in, int n)
Definition: atacmds.cpp:768
static void PrintSmartOfflineCollectCap(const ata_smart_values *data)
Definition: ataprint.cpp:883
unsigned char failing_lba[6]
Definition: atacmds.h:452
signed char min_temp
Definition: atacmds.h:546
unsigned char version
Definition: atacmds.h:400
#define ATTRIBUTE_FLAGS_OTHER(x)
Definition: atacmds.h:211
bool ignore_presets
Definition: ataprint.h:92
#define ATA_DISABLE_APM
Definition: atacmds.h:85
unsigned char version
Definition: atacmds.h:463
bool set_standby_now
Definition: ataprint.h:105
ATA device access.
#define SHORT_SELF_TEST
Definition: atacmds.h:117
static void PrintSataPhyEventCounters(const unsigned char *data, bool reset)
Definition: ataprint.cpp:1680
static void print_ata_security_status(const char *msg, unsigned short state)
Definition: ataprint.cpp:2475
static const char * get_pata_version(unsigned short word222, char(&buf)[32])
Definition: ataprint.cpp:538
signed char life_min_temp
Definition: atacmds.h:548
unsigned char state
Definition: atacmds.h:378
unsigned char serial_no[20]
Definition: atacmds.h:136
int ataPrintSmartSelfTestlog(const ata_smart_selftestlog *data, bool allentries, firmwarebug_defs firmwarebugs)
Definition: atacmds.cpp:2657
unsigned char * data()
Definition: utility.h:159
unsigned long long uint64_t
Definition: int64.h:54
static const char * get_sata_maxspeed(const ata_identify_device *drive)
Definition: ataprint.cpp:583
unsigned short timestamp
Definition: atacmds.h:450
#define ATA_IDLE
Definition: atacmds.h:74
unsigned short int logversion
Definition: atacmds.h:485
unsigned char conveyance_test_completion_time
Definition: atacmds.h:230
unsigned char reserved1
Definition: atacmds.h:401
int isSupportAutomaticTimer(const ata_smart_values *data)
Definition: atacmds.cpp:1831
struct ata_smart_threshold_entry thres_entries[NUMBER_ATA_SMART_ATTRIBUTES]
Definition: atacmds.h:263
const devstat_entry_info devstat_info_0x01[]
Definition: ataprint.cpp:1399
bool devstat_all_pages
Definition: ataprint.h:64
bool isSCTErrorRecoveryControlCapable(const ata_identify_device *drive)
Definition: atacmds.h:894
int ataSmartStatus2(ata_device *device)
Definition: atacmds.cpp:1625
unsigned int under_limit_count
Definition: atacmds.h:552
bool smart_vendor_attrib
Definition: ataprint.h:52
const devstat_entry_info devstat_info_0x07[]
Definition: ataprint.cpp:1470
unsigned char offline_data_collection_capability
Definition: atacmds.h:224
unsigned char powermode
Definition: ataprint.h:94
signed char life_max_temp
Definition: atacmds.h:549
bool smart_check_status
Definition: ataprint.h:50
static bool print_device_statistics(ata_device *device, unsigned nsectors, const std::vector< int > &single_pages, bool all_pages, bool ssd_page, bool use_gplog)
Definition: ataprint.cpp:1571
unsigned char status_register
Definition: atacmds.h:375
unsigned short flags
Definition: atacmds.h:509
int ataReadSCTTempHist(ata_device *device, ata_sct_temperature_history_table *tmh, ata_sct_status_response *sts)
Definition: atacmds.cpp:2281
static std::string format_milliseconds(unsigned msec)
Definition: ataprint.cpp:1746
unsigned sct_erc_readtime
Definition: ataprint.h:70
std::string strprintf(const char *fmt,...)
Definition: utility.cpp:750
unsigned char byte205
Definition: atacmds.h:550
static void ataPrintSCTErrorRecoveryControl(bool set, unsigned short read_timer, unsigned short write_timer)
Definition: ataprint.cpp:2418
bool smart_auto_save_disable
Definition: ataprint.h:75
unsigned nsectors
Definition: ataprint.h:38
#define ATTRIBUTE_FLAGS_PERFORMANCE(x)
Definition: atacmds.h:199
int ataDisableAutoOffline(ata_device *device)
Definition: atacmds.cpp:1602
const devstat_entry_info devstat_info_0x02[]
Definition: ataprint.cpp:1416
void failuretest(failure_type type, int returnvalue)
Definition: smartctl.cpp:1220
unsigned short int revnumber
Definition: atacmds.h:218
uint64_t capacity
Definition: atacmds.h:1006
unsigned char vendor_specific[32]
Definition: atacmds.h:556
unsigned char model[40]
Definition: atacmds.h:139
std::vector< ata_log_request > log_requests
Definition: ataprint.h:62
#define ATA_ENABLE_READ_LOOK_AHEAD
Definition: atacmds.h:91
int isSmartErrorLogCapable(const ata_smart_values *data, const ata_identify_device *identity)
Definition: atacmds.cpp:1752
int isSupportSelfTest(const ata_smart_values *data)
Definition: atacmds.cpp:1843
u8 cmd
Definition: megaraid.h:80
unsigned char worst
Definition: atacmds.h:162
static void PrintSmartConveyanceSelfTestPollingTime(const ata_smart_values *data)
Definition: ataprint.cpp:981
bool devstat_ssd_page
Definition: ataprint.h:64
static void PrintGeneralSmartValues(const ata_smart_values *data, const ata_identify_device *drive, firmwarebug_defs firmwarebugs)
Definition: ataprint.cpp:1143
int ataReadErrorLog(ata_device *device, ata_smart_errorlog *data, firmwarebug_defs firmwarebugs)
Definition: atacmds.cpp:1445
static const char * get_device_statistics_page_name(int page)
Definition: ataprint.cpp:1490
void clear_err()
Clear last error info.
unsigned smart_ext_error_log
Definition: ataprint.h:58
unsigned char error_log_pointer
Definition: atacmds.h:319
unsigned short min_erc_time
Definition: atacmds.h:554
#define ATAPRINT_H_CVSID
Definition: ataprint.h:28
static void ataPrintSelectiveSelfTestLog(const ata_selective_self_test_log *log, const ata_smart_values *sv)
Definition: ataprint.cpp:2140
unsigned char lba_mid_register_hi
Definition: atacmds.h:347
int ataSetSCTErrorRecoveryControltime(ata_device *device, unsigned type, unsigned short time_limit)
Definition: atacmds.cpp:2566
#define SELECTIVE_FLAG_ACTIVE
Definition: atacmds.h:520
int identify_word_level
Definition: ataprint.h:49
void show_presets(const ata_identify_device *drive)
int ataDoesSmartWork(ata_device *device)
Definition: atacmds.cpp:1614
unsigned char features_register_hi
Definition: atacmds.h:341
bool isSCTFeatureControlCapable(const ata_identify_device *drive)
Definition: atacmds.h:897
signed char hda_temp
Definition: atacmds.h:545
#define SELECTIVE_FLAG_DOSCAN
Definition: atacmds.h:518
int sct_wcache_reorder_set
Definition: ataprint.h:111
virtual bool ata_identify_is_cached() const
Return true if OS caches ATA identify sector.
#define ATA_SECURITY_FREEZE_LOCK
Definition: atacmds.h:76
unsigned char count_register_hi
Definition: atacmds.h:367
const devstat_entry_info * devstat_infos[]
Definition: ataprint.cpp:1476
int ataReadLogDirectory(ata_device *device, ata_smart_log_directory *data, bool gpl)
Definition: atacmds.cpp:1185
struct ata_smart_errorlog_error_struct error_struct
Definition: atacmds.h:310
bool ataReadSmartLog(ata_device *device, unsigned char logaddr, void *data, unsigned nsectors)
Definition: atacmds.cpp:1164
#define PRIx64
Definition: int64.h:80
virtual bool is_syscall_unsup() const
Return true if last error indicates an unsupported system call.
unsigned char command_register
Definition: atacmds.h:351
int ataCheckPowerMode(ata_device *device)
Definition: atacmds.cpp:794
#define ATA_DISABLE_WRITE_CACHE
Definition: atacmds.h:86
bool printing_is_switchable
Definition: smartctl.cpp:59
#define ATTRIBUTE_FLAGS_ERRORRATE(x)
Definition: atacmds.h:202
unsigned char count_register
Definition: atacmds.h:366
unsigned short logversion
Definition: atacmds.h:503
static int find_failed_attr(const ata_smart_values *data, const ata_smart_thresholds_pvt *thresholds, const ata_vendor_attr_defs &defs, int onlyfailed)
Definition: ataprint.cpp:994
#define FAILLOG
Definition: smartctl.h:62
unsigned char error_register
Definition: atacmds.h:365
bool sataphy_reset
Definition: ataprint.h:71
#define SHORT_CAPTIVE_SELF_TEST
Definition: atacmds.h:122
unsigned char lba_high_register
Definition: atacmds.h:348
bool smart_auto_offl_enable
Definition: ataprint.h:74
const char * look_up_ata_command(unsigned char c_code, unsigned char f_reg)
int isSmartTestLogCapable(const ata_smart_values *data, const ata_identify_device *identity)
Definition: atacmds.cpp:1771