]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - ataprint.cpp
import smartmontools 7.0
[mirror_smartmontools-debian.git] / ataprint.cpp
index 4fd2724f68c735f1741c1bed60bf0f02bd154465..1ca222fcb6c36e01756e9efc9af22ed399ebeb69 100644 (file)
@@ -1,46 +1,36 @@
 /*
  * ataprint.cpp
  *
- * Home page of code is: http://smartmontools.sourceforge.net
+ * Home page of code is: http://www.smartmontools.org
  *
- * Copyright (C) 2002-11 Bruce Allen <smartmontools-support@lists.sourceforge.net>
- * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2002-11 Bruce Allen
+ * Copyright (C) 2008-18 Christian Franke
  * Copyright (C) 1999-2000 Michael Cornwell <cornwell@acm.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- *
- * You should have received a copy of the GNU General Public License
- * (for example COPYING); if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * This code was originally developed as a Senior Thesis by Michael Cornwell
- * at the Concurrent Systems Laboratory (now part of the Storage Systems
- * Research Center), Jack Baskin School of Engineering, University of
- * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
- *
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include "config.h"
+#define __STDC_FORMAT_MACROS 1 // enable PRI* for C++
 
 #include <ctype.h>
 #include <errno.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "int64.h"
 #include "atacmdnames.h"
 #include "atacmds.h"
+#include "ataidentify.h"
 #include "dev_interface.h"
 #include "ataprint.h"
 #include "smartctl.h"
+#include "sg_unaligned.h"
 #include "utility.h"
 #include "knowndrives.h"
 
-const char * ataprint_cpp_cvsid = "$Id: ataprint.cpp 3554 2012-06-01 20:11:46Z chrfranke $"
+const char * ataprint_cpp_cvsid = "$Id: ataprint.cpp 4842 2018-12-02 16:07:26Z chrfranke $"
                                   ATAPRINT_H_CVSID;
 
 
@@ -60,10 +50,9 @@ static bool is_permissive()
 
 /* For the given Command Register (CR) and Features Register (FR), attempts
  * to construct a string that describes the contents of the Status
- * Register (ST) and Error Register (ER).  The caller passes the string
- * buffer and the return value is a pointer to this string.  If the
- * meanings of the flags of the error register are not known for the given
- * command then it returns NULL.
+ * Register (ST) and Error Register (ER).  If the meanings of the flags of
+ * the error register are not known for the given command then it returns an
+ * empty string.
  *
  * The meanings of the flags of the error register for all commands are
  * described in the ATA spec and could all be supported here in theory.
@@ -72,8 +61,7 @@ static bool is_permissive()
  * should probably be redesigned.
  */
 
-static const char * construct_st_er_desc(
-  char * s,
+static std::string format_st_er_desc(
   unsigned char CR, unsigned char FR,
   unsigned char ST, unsigned char ER,
   unsigned short SC,
@@ -111,6 +99,8 @@ static const char * construct_st_er_desc(
   for (i = 0; i < 8; i++)
     error_flag[i] = NULL;
 
+  std::string str;
+
   switch (CR) {
   case 0x10:  // RECALIBRATE
     error_flag[2] = abrt;
@@ -281,7 +271,7 @@ static const char * construct_st_er_desc(
       error_flag[2] = abrt;
       break;
     default:
-      return NULL;
+      return str; // ""
       break;
     }
     break;
@@ -291,7 +281,7 @@ static const char * construct_st_er_desc(
       error_flag[2] = abrt;
       break;
     default:
-      return NULL;
+      return str; // ""
       break;
     }
     break;
@@ -320,39 +310,34 @@ static const char * construct_st_er_desc(
     error_flag[2] = abrt;
     break;
   default:
-    return NULL;
+    return str; // ""
   }
 
-  s[0] = '\0';
-
   /* We ignore any status flags other than Device Fault and Error */
 
   if (uses_device_fault && (ST & (1 << 5))) {
-    strcat(s, "Device Fault");
+    str = "Device Fault";
     if (ST & 1)  // Error flag
-      strcat(s, "; ");
+      str += "; ";
   }
   if (ST & 1) {  // Error flag
     int count = 0;
 
-    strcat(s, "Error: ");
+    str += "Error: ";
     for (i = 7; i >= 0; i--)
       if ((ER & (1 << i)) && (error_flag[i])) {
         if (count++ > 0)
-           strcat(s, ", ");
-        strcat(s, error_flag[i]);
+           str += ", ";
+        str += error_flag[i];
       }
   }
 
   // If the error was a READ or WRITE error, print the Logical Block
   // Address (LBA) at which the read or write failed.
   if (print_lba) {
-    char tmp[128];
     // print number of sectors, if known, and append to print string
-    if (print_sector) {
-      snprintf(tmp, 128, " %d sectors", print_sector);
-      strcat(s, tmp);
-    }
+    if (print_sector)
+      str += strprintf(" %d sectors", print_sector);
 
     if (lba28_regs) {
       unsigned lba;
@@ -367,8 +352,7 @@ static const char * construct_st_er_desc(
       lba <<= 8;
       // bits 0-7:   SN
       lba  |= lba28_regs->sector_number;
-      snprintf(tmp, 128, " at LBA = 0x%08x = %u", lba, lba);
-      strcat(s, tmp);
+      str += strprintf(" at LBA = 0x%08x = %u", lba, lba);
     }
     else if (lba48_regs) {
       // This assumes that upper LBA registers are 0 for 28-bit commands
@@ -386,18 +370,17 @@ static const char * construct_st_er_desc(
       lba48  |= lba48_regs->lba_mid_register;
       lba48 <<= 8;
       lba48  |= lba48_regs->lba_low_register;
-      snprintf(tmp, 128, " at LBA = 0x%08"PRIx64" = %"PRIu64, lba48, lba48);
-      strcat(s, tmp);
+      str += strprintf(" at LBA = 0x%08" PRIx64 " = %" PRIu64, lba48, lba48);
     }
   }
 
-  return s;
+  return str;
 }
 
-static inline const char * construct_st_er_desc(char * s,
+static inline std::string format_st_er_desc(
   const ata_smart_errorlog_struct * data)
 {
-  return construct_st_er_desc(s,
+  return format_st_er_desc(
     data->commands[4].commandreg,
     data->commands[4].featuresreg,
     data->error_struct.status,
@@ -406,10 +389,10 @@ static inline const char * construct_st_er_desc(char * s,
     &data->error_struct, (const ata_smart_exterrlog_error *)0);
 }
 
-static inline const char * construct_st_er_desc(char * s,
+static inline std::string format_st_er_desc(
   const ata_smart_exterrlog_error_log * data)
 {
-  return construct_st_er_desc(s,
+  return format_st_er_desc(
     data->commands[4].command_register,
     data->commands[4].features_register,
     data->error.status_register,
@@ -418,8 +401,220 @@ static inline const char * construct_st_er_desc(char * s,
     (const ata_smart_errorlog_error_struct *)0, &data->error);
 }
 
+
+static const char * get_form_factor(unsigned short word168)
+{
+  // Bits 0:3 are the form factor
+  // Table A.32 of T13/2161-D (ACS-3) Revision 4p, September 19, 2013
+  // Table 236 of T13/BSR INCITS 529 (ACS-4) Revision 04, August 25, 2014
+  switch (word168 & 0xF) {
+    case 0x1: return "5.25 inches";
+    case 0x2: return "3.5 inches";
+    case 0x3: return "2.5 inches";
+    case 0x4: return "1.8 inches";
+    case 0x5: return "< 1.8 inches";
+    case 0x6: return "mSATA"; // ACS-4
+    case 0x7: return "M.2"; // ACS-4
+    case 0x8: return "MicroSSD"; // ACS-4
+    case 0x9: return "CFast"; // ACS-4
+    default : return 0;
+  }
+}
+
+static int find_msb(unsigned short word)
+{
+  for (int bit = 15; bit >= 0; bit--)
+    if (word & (1 << bit))
+      return bit;
+  return -1;
+}
+
+static const char * get_ata_major_version(const ata_identify_device * drive)
+{
+  // Table 13 of T13/1153D (ATA/ATAPI-4) revision 18, August 19, 1998
+  // Table 48 of T13/BSR INCITS 529 (ACS-4) Revision 16, February 21, 2017
+  switch (find_msb(drive->major_rev_num)) {
+    case 14: return "ACS >4 (14)";
+    case 13: return "ACS >4 (13)";
+    case 12: return "ACS >4 (12)";
+    case 11: return "ACS-4";
+    case 10: return "ACS-3";
+    case  9: return "ACS-2";
+    case  8: return "ATA8-ACS";
+    case  7: return "ATA/ATAPI-7";
+    case  6: return "ATA/ATAPI-6";
+    case  5: return "ATA/ATAPI-5";
+    case  4: return "ATA/ATAPI-4";
+    case  3: return "ATA-3";
+    case  2: return "ATA-2";
+    case  1: return "ATA-1";
+    default: return 0;
+  }
+}
+
+static const char * get_ata_minor_version(const ata_identify_device * drive)
+{
+  // Table 10 of X3T13/2008D (ATA-3) Revision 7b, January 27, 1997
+  // Table 28 of T13/1410D (ATA/ATAPI-6) Revision 3b, February 26, 2002
+  // Table 31 of T13/1699-D (ATA8-ACS) Revision 6a, September 6, 2008
+  // Table 46 of T13/BSR INCITS 529 (ACS-4) Revision 08, April 28, 2015
+  switch (drive->minor_rev_num) {
+    case 0x0001: return "ATA-1 X3T9.2/781D prior to revision 4";
+    case 0x0002: return "ATA-1 published, ANSI X3.221-1994";
+    case 0x0003: return "ATA-1 X3T9.2/781D revision 4";
+    case 0x0004: return "ATA-2 published, ANSI X3.279-1996";
+    case 0x0005: return "ATA-2 X3T10/948D prior to revision 2k";
+    case 0x0006: return "ATA-3 X3T10/2008D revision 1";
+    case 0x0007: return "ATA-2 X3T10/948D revision 2k";
+    case 0x0008: return "ATA-3 X3T10/2008D revision 0";
+    case 0x0009: return "ATA-2 X3T10/948D revision 3";
+    case 0x000a: return "ATA-3 published, ANSI X3.298-1997";
+    case 0x000b: return "ATA-3 X3T10/2008D revision 6"; // 1st ATA-3 revision with SMART
+    case 0x000c: return "ATA-3 X3T13/2008D revision 7 and 7a";
+    case 0x000d: return "ATA/ATAPI-4 X3T13/1153D revision 6";
+    case 0x000e: return "ATA/ATAPI-4 T13/1153D revision 13";
+    case 0x000f: return "ATA/ATAPI-4 X3T13/1153D revision 7";
+    case 0x0010: return "ATA/ATAPI-4 T13/1153D revision 18";
+    case 0x0011: return "ATA/ATAPI-4 T13/1153D revision 15";
+    case 0x0012: return "ATA/ATAPI-4 published, ANSI NCITS 317-1998";
+    case 0x0013: return "ATA/ATAPI-5 T13/1321D revision 3";
+    case 0x0014: return "ATA/ATAPI-4 T13/1153D revision 14";
+    case 0x0015: return "ATA/ATAPI-5 T13/1321D revision 1";
+    case 0x0016: return "ATA/ATAPI-5 published, ANSI NCITS 340-2000";
+    case 0x0017: return "ATA/ATAPI-4 T13/1153D revision 17";
+    case 0x0018: return "ATA/ATAPI-6 T13/1410D revision 0";
+    case 0x0019: return "ATA/ATAPI-6 T13/1410D revision 3a";
+    case 0x001a: return "ATA/ATAPI-7 T13/1532D revision 1";
+    case 0x001b: return "ATA/ATAPI-6 T13/1410D revision 2";
+    case 0x001c: return "ATA/ATAPI-6 T13/1410D revision 1";
+    case 0x001d: return "ATA/ATAPI-7 published, ANSI INCITS 397-2005";
+    case 0x001e: return "ATA/ATAPI-7 T13/1532D revision 0";
+    case 0x001f: return "ACS-3 T13/2161-D revision 3b";
+
+    case 0x0021: return "ATA/ATAPI-7 T13/1532D revision 4a";
+    case 0x0022: return "ATA/ATAPI-6 published, ANSI INCITS 361-2002";
+
+    case 0x0027: return "ATA8-ACS T13/1699-D revision 3c";
+    case 0x0028: return "ATA8-ACS T13/1699-D revision 6";
+    case 0x0029: return "ATA8-ACS T13/1699-D revision 4";
+
+    case 0x0031: return "ACS-2 T13/2015-D revision 2";
+
+    case 0x0033: return "ATA8-ACS T13/1699-D revision 3e";
+
+    case 0x0039: return "ATA8-ACS T13/1699-D revision 4c";
+
+    case 0x0042: return "ATA8-ACS T13/1699-D revision 3f";
+
+    case 0x0052: return "ATA8-ACS T13/1699-D revision 3b";
+
+    case 0x005e: return "ACS-4 T13/BSR INCITS 529 revision 5";
+
+    case 0x006d: return "ACS-3 T13/2161-D revision 5";
+
+    case 0x0082: return "ACS-2 published, ANSI INCITS 482-2012";
+
+    case 0x0107: return "ATA8-ACS T13/1699-D revision 2d";
+
+    case 0x010a: return "ACS-3 published, ANSI INCITS 522-2014";
+
+    case 0x0110: return "ACS-2 T13/2015-D revision 3";
+
+    case 0x011b: return "ACS-3 T13/2161-D revision 4";
+
+    default:     return 0;
+  }
+}
+
+static const char * get_pata_version(unsigned short word222, char (& buf)[32])
+{
+  switch (word222 & 0x0fff) {
+    default: snprintf(buf, sizeof(buf),
+                       "Unknown (0x%03x)", word222 & 0x0fff); return buf;
+    case 0x001:
+    case 0x003: return "ATA8-APT";
+    case 0x002: return "ATA/ATAPI-7";
+  }
+}
+
+static const char * get_sata_version(unsigned short word222)
+{
+  switch (find_msb(word222 & 0x0fff)) {
+    case 11: return "SATA >3.3 (11)";
+    case 10: return "SATA >3.3 (10)";
+    case  9: return "SATA >3.3 (9)";
+    case  8: return "SATA 3.3";
+    case  7: return "SATA 3.2";
+    case  6: return "SATA 3.1";
+    case  5: return "SATA 3.0";
+    case  4: return "SATA 2.6";
+    case  3: return "SATA 2.5";
+    case  2: return "SATA II Ext";
+    case  1: return "SATA 1.0a";
+    case  0: return "ATA8-AST";
+    default: return 0;
+  }
+}
+
+static const char * get_sata_speed(int speed)
+{
+  if (speed <= 0)
+    return 0;
+  switch (speed) {
+    default: return ">6.0 Gb/s (7)";
+    case 6:  return ">6.0 Gb/s (6)";
+    case 5:  return ">6.0 Gb/s (5)";
+    case 4:  return ">6.0 Gb/s (4)";
+    case 3:  return "6.0 Gb/s";
+    case 2:  return "3.0 Gb/s";
+    case 1:  return "1.5 Gb/s";
+  }
+}
+
+static void jset_sata_speed(const char * key, int value, int speed, const char * str)
+{
+  if (speed <= 0)
+    return;
+  json::ref jref = jglb["interface_speed"][key];
+  jref["sata_value"] = value;
+  if (str)
+    jref["string"] = str;
+  int ups;
+  switch (speed) {
+    case 3: ups = 60; break;
+    case 2: ups = 30; break;
+    case 1: ups = 15; break;
+    default: return;
+  }
+  jref["units_per_second"] = ups;
+  jref["bits_per_unit"] = 100000000;
+}
+
+static void print_sata_version_and_speed(unsigned short word222,
+                                         unsigned short word076,
+                                         unsigned short word077)
+{
+  int allspeeds = (!(word076 & 0x0001) ? (word076 & 0x00fe) : 0);
+  int maxspeed = (allspeeds ? find_msb(allspeeds) : 0);
+  int curspeed = (!(word077 & 0x0001) ? ((word077 >> 1) & 0x7) : 0);
+
+  const char * verstr = get_sata_version(word222);
+  const char * maxstr = get_sata_speed(maxspeed);
+  const char * curstr = get_sata_speed(curspeed);
+  jout("SATA Version is:  %s%s%s%s%s%s\n",
+       (verstr ? verstr : "Unknown"),
+       (maxstr ? ", " : ""), (maxstr ? maxstr : ""),
+       (curstr ? " (current: " : ""), (curstr ? curstr : ""),
+       (curstr ? ")" : ""));
+  if (verstr)
+    jglb["sata_version"]["string"] = verstr;
+  jglb["sata_version"]["value"] = word222 & 0x0fff;
+  jset_sata_speed("max", allspeeds, maxspeed, maxstr);
+  jset_sata_speed("current", curspeed, curspeed, curstr);
+}
+
 static void print_drive_info(const ata_identify_device * drive,
-                             const ata_size_info & sizes,
+                             const ata_size_info & sizes, int rpm,
                              const drive_settings * dbentry)
 {
   // format drive information (with byte swapping as needed)
@@ -429,97 +624,159 @@ static void print_drive_info(const ata_identify_device * drive,
   ata_format_id_string(firmware, drive->fw_rev, sizeof(firmware)-1);
 
   // Print model family if known
-  if (dbentry && *dbentry->modelfamily)
-    pout("Model Family:     %s\n", dbentry->modelfamily);
+  if (dbentry && *dbentry->modelfamily) {
+    jout("Model Family:     %s\n", dbentry->modelfamily);
+    jglb["model_family"] = dbentry->modelfamily;
+  }
+
+  jout("Device Model:     %s\n", infofound(model));
+  jglb["model_name"] = model;
 
-  pout("Device Model:     %s\n", infofound(model));
   if (!dont_print_serial_number) {
-    pout("Serial Number:    %s\n", infofound(serial));
+    jout("Serial Number:    %s\n", infofound(serial));
+    jglb["serial_number"] = serial;
 
     unsigned oui = 0; uint64_t unique_id = 0;
     int naa = ata_get_wwn(drive, oui, unique_id);
-    if (naa >= 0)
-      pout("LU WWN Device Id: %x %06x %09"PRIx64"\n", naa, oui, unique_id);
+    if (naa >= 0) {
+      jout("LU WWN Device Id: %x %06x %09" PRIx64 "\n", naa, oui, unique_id);
+      jglb["wwn"]["naa"] = naa;
+      jglb["wwn"]["oui"] = oui;
+      jglb["wwn"]["id"]  = unique_id;
+    }
   }
-  pout("Firmware Version: %s\n", infofound(firmware));
+
+  // Additional Product Identifier (OEM Id) string in words 170-173
+  // (e08130r1, added in ACS-2 Revision 1, December 17, 2008)
+  if (0x2020 <= drive->words088_255[170-88] && drive->words088_255[170-88] <= 0x7e7e) {
+    char add[8+1];
+    ata_format_id_string(add, (const unsigned char *)(drive->words088_255+170-88), sizeof(add)-1);
+    if (add[0]) {
+      jout("Add. Product Id:  %s\n", add);
+      jglb["ata_additional_product_id"] = add;
+    }
+  }
+
+  jout("Firmware Version: %s\n", infofound(firmware));
+  jglb["firmware_version"] = firmware;
 
   if (sizes.capacity) {
     // Print capacity
     char num[64], cap[32];
-    pout("User Capacity:    %s bytes [%s]\n",
+    jout("User Capacity:    %s bytes [%s]\n",
       format_with_thousands_sep(num, sizeof(num), sizes.capacity),
       format_capacity(cap, sizeof(cap), sizes.capacity));
+    jglb["user_capacity"]["blocks"].set_unsafe_uint64(sizes.sectors);
+    jglb["user_capacity"]["bytes"].set_unsafe_uint64(sizes.capacity);
 
     // Print sector sizes.
     if (sizes.phy_sector_size == sizes.log_sector_size)
-      pout("Sector Size:      %u bytes logical/physical\n", sizes.log_sector_size);
+      jout("Sector Size:      %u bytes logical/physical\n", sizes.log_sector_size);
     else {
-      pout("Sector Sizes:     %u bytes logical, %u bytes physical",
-         sizes.log_sector_size, sizes.phy_sector_size);
+      jout("Sector Sizes:     %u bytes logical, %u bytes physical",
+        sizes.log_sector_size, sizes.phy_sector_size);
       if (sizes.log_sector_offset)
         pout(" (offset %u bytes)", sizes.log_sector_offset);
-      pout("\n");
+      jout("\n");
     }
+    jglb["logical_block_size"]  = sizes.log_sector_size;
+    jglb["physical_block_size"] = sizes.phy_sector_size;
+  }
+
+  // Print nominal media rotation rate if reported
+  if (rpm) {
+    if (rpm == 1)
+      jout("Rotation Rate:    Solid State Device\n");
+    else if (rpm > 1)
+      jout("Rotation Rate:    %d rpm\n", rpm);
+    else
+      pout("Rotation Rate:    Unknown (0x%04x)\n", -rpm);
+    if (rpm > 0)
+      jglb["rotation_rate"] = (rpm == 1 ? 0 : rpm);
+  }
+
+  // Print form factor if reported
+  unsigned short word168 = drive->words088_255[168-88];
+  if (word168) {
+    const char * form_factor = get_form_factor(word168);
+    if (form_factor)
+      jout("Form Factor:      %s\n", form_factor);
+    else
+      jout("Form Factor:      Unknown (0x%04x)\n", word168);
+    jglb["form_factor"]["ata_value"] = word168;
+    jglb["form_factor"]["name"] = form_factor;
   }
 
   // See if drive is recognized
-  pout("Device is:        %s\n", !dbentry ?
+  jout("Device is:        %s\n", !dbentry ?
        "Not in smartctl database [for details use: -P showall]":
        "In smartctl database [for details use: -P show]");
-
-  // now get ATA version info
-  const char *description; unsigned short minorrev;
-  int version = ataVersionInfo(&description, drive, &minorrev);
-
-  // SMART Support was first added into the ATA/ATAPI-3 Standard with
-  // Revision 3 of the document, July 25, 1995.  Look at the "Document
-  // Status" revision commands at the beginning of
-  // http://www.t13.org/Documents/UploadedDocuments/project/d2008r7b-ATA-3.pdf
-  // to see this.  So it's not enough to check if we are ATA-3.
-  // Version=-3 indicates ATA-3 BEFORE Revision 3.
-  // Version=0 indicates that no info is found. This may happen if
-  // the OS provides only part of the IDENTIFY data.
-
-  std::string majorstr, minorstr;
-  if (version) {
-    if (version <= 8) {
-      majorstr = strprintf("%d", abs(version));
-      if (description)
-        minorstr = description;
-      else if (!minorrev)
-        minorstr = "Exact ATA specification draft version not indicated";
-      else
-        minorstr = strprintf("Not recognized. Minor revision code: 0x%04x", minorrev);
+  jglb["in_smartctl_database"] = !!dbentry;
+
+  // Print ATA version
+  std::string ataver;
+  if (   (drive->major_rev_num != 0x0000 && drive->major_rev_num != 0xffff)
+      || (drive->minor_rev_num != 0x0000 && drive->minor_rev_num != 0xffff)) {
+    const char * majorver = get_ata_major_version(drive);
+    const char * minorver = get_ata_minor_version(drive);
+
+    if (majorver && minorver && str_starts_with(minorver, majorver)) {
+      // Major and minor strings match, print minor string only
+      ataver = minorver;
     }
     else {
-      // Bit 9 in word 80 of ATA IDENTIFY data does not mean "ATA-9" but "ACS-2"
-      // TODO: handle this in ataVersionInfo()
-      majorstr = "8";
-      if (description)
-        minorstr = description;
-      else if (!minorrev)
-        minorstr = strprintf("ACS-%d (revision not indicated)", version-9+2);
+      if (majorver)
+        ataver = majorver;
+      else
+        ataver = strprintf("Unknown(0x%04x)", drive->major_rev_num);
+
+      if (minorver)
+        ataver += strprintf(", %s", minorver);
+      else if (drive->minor_rev_num != 0x0000 && drive->minor_rev_num != 0xffff)
+        ataver += strprintf(" (unknown minor revision code: 0x%04x)", drive->minor_rev_num);
       else
-        minorstr = strprintf("ACS-%d (unknown minor revision code: 0x%04x)", version-9+2, minorrev);
+        ataver += " (minor revision not indicated)";
     }
   }
+  jout("ATA Version is:   %s\n", infofound(ataver.c_str()));
+  if (!ataver.empty()) {
+    jglb["ata_version"]["string"] = ataver;
+    jglb["ata_version"]["major_value"] = drive->major_rev_num;
+    jglb["ata_version"]["minor_value"] = drive->minor_rev_num;
+  }
 
-  pout("ATA Version is:   %s\n", infofound(majorstr.c_str()));
-  pout("ATA Standard is:  %s\n", infofound(minorstr.c_str()));
+  // Print Transport specific version
+  unsigned short word222 = drive->words088_255[222-88];
+  if (word222 != 0x0000 && word222 != 0xffff) switch (word222 >> 12) {
+    case 0x0: // PATA
+      {
+        char buf[32] = "";
+        pout("Transport Type:   Parallel, %s\n", get_pata_version(word222, buf));
+      }
+      break;
+    case 0x1: // SATA
+      print_sata_version_and_speed(word222,
+                                   drive->words047_079[76-47],
+                                   drive->words047_079[77-47]);
+      break;
+    case 0xe: // PCIe (ACS-4)
+      pout("Transport Type:   PCIe (0x%03x)\n", word222 & 0x0fff);
+      break;
+    default:
+      pout("Transport Type:   Unknown (0x%04x)\n", word222);
+      break;
+  }
 
   // print current time and date and timezone
-  char timedatetz[DATEANDEPOCHLEN]; dateandtimezone(timedatetz);
-  pout("Local Time is:    %s\n", timedatetz);
+  time_t now = time(0);
+  char timedatetz[DATEANDEPOCHLEN]; dateandtimezoneepoch(timedatetz, now);
+  jout("Local Time is:    %s\n", timedatetz);
+  jglb["local_time"]["time_t"] = now;
+  jglb["local_time"]["asctime"] = timedatetz;
 
   // Print warning message, if there is one
   if (dbentry && *dbentry->warningmsg)
     pout("\n==> WARNING: %s\n\n", dbentry->warningmsg);
-
-  if (!version || version >= 3)
-    return;
-  
-  pout("SMART is only available in ATA Version 3 Revision 3 or greater.\n");
-  pout("We will try to proceed in spite of this.\n");
 }
 
 static const char *OfflineDataCollectionStatus(unsigned char status_byte)
@@ -554,14 +811,22 @@ static const char *OfflineDataCollectionStatus(unsigned char status_byte)
 //  prints verbose value Off-line data collection status byte
 static void PrintSmartOfflineStatus(const ata_smart_values * data)
 {
-  pout("Offline data collection status:  (0x%02x)\t",
+  json::ref jref = jglb["ata_smart_data"]["offline_data_collection"]["status"];
+
+  jout("Offline data collection status:  (0x%02x)\t",
        (int)data->offline_data_collection_status);
+  jref["value"] = data->offline_data_collection_status;
     
   // Off-line data collection status byte is not a reserved
   // or vendor specific value
-  pout("Offline data collection activity\n"
+  jout("Offline data collection activity\n"
        "\t\t\t\t\t%s.\n", OfflineDataCollectionStatus(data->offline_data_collection_status));
-  
+  jref["string"] = OfflineDataCollectionStatus(data->offline_data_collection_status);
+  switch (data->offline_data_collection_status & 0x7f) {
+    case 0x02: jref["passed"] = true; break;
+    case 0x06: jref["passed"] = false; break;
+  }
+
   // Report on Automatic Data Collection Status.  Only IBM documents
   // this bit.  See SFF 8035i Revision 2 for details.
   if (data->offline_data_collection_status & 0x80)
@@ -573,200 +838,232 @@ static void PrintSmartOfflineStatus(const ata_smart_values * data)
 }
 
 static void PrintSmartSelfExecStatus(const ata_smart_values * data,
-                                     unsigned char fix_firmwarebug)
+                                     firmwarebug_defs firmwarebugs)
 {
-   pout("Self-test execution status:      ");
-   
-   switch (data->self_test_exec_status >> 4)
-   {
-      case 0:
-        pout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t",
-                (int)data->self_test_exec_status);
-        pout("without error or no self-test has ever \n\t\t\t\t\tbeen run.\n");
-        break;
-       case 1:
-         pout("(%4d)\tThe self-test routine was aborted by\n\t\t\t\t\t",
-                 (int)data->self_test_exec_status);
-         pout("the host.\n");
-         break;
-       case 2:
-         pout("(%4d)\tThe self-test routine was interrupted\n\t\t\t\t\t",
-                 (int)data->self_test_exec_status);
-         pout("by the host with a hard or soft reset.\n");
-         break;
-       case 3:
-          pout("(%4d)\tA fatal error or unknown test error\n\t\t\t\t\t",
-                  (int)data->self_test_exec_status);
-          pout("occurred while the device was executing\n\t\t\t\t\t");
-          pout("its self-test routine and the device \n\t\t\t\t\t");
-          pout("was unable to complete the self-test \n\t\t\t\t\t");
-          pout("routine.\n");
-          break;
-       case 4:
-          pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
-                  (int)data->self_test_exec_status);
-          pout("a test element that failed and the test\n\t\t\t\t\t");
-          pout("element that failed is not known.\n");
-          break;
-       case 5:
-          pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
-                  (int)data->self_test_exec_status);
-          pout("the electrical element of the test\n\t\t\t\t\t");
-          pout("failed.\n");
-          break;
-       case 6:
-          pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
-                  (int)data->self_test_exec_status);
-          pout("the servo (and/or seek) element of the \n\t\t\t\t\t");
-          pout("test failed.\n");
-          break;
-       case 7:
-          pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
-                  (int)data->self_test_exec_status);
-          pout("the read element of the test failed.\n");
-          break;
-       case 8:
-          pout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t",
-                  (int)data->self_test_exec_status);
-          pout("a test element that failed and the\n\t\t\t\t\t");
-          pout("device is suspected of having handling\n\t\t\t\t\t");
-          pout("damage.\n");
-          break;
-       case 15:
-          if (fix_firmwarebug == FIX_SAMSUNG3 && data->self_test_exec_status == 0xf0) {
-            pout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t",
-                    (int)data->self_test_exec_status);
-            pout("with unknown result or self-test in\n\t\t\t\t\t");
-            pout("progress with less than 10%% remaining.\n");
-          }
-          else {
-            pout("(%4d)\tSelf-test routine in progress...\n\t\t\t\t\t",
-                    (int)data->self_test_exec_status);
-            pout("%1d0%% of test remaining.\n", 
-                  (int)(data->self_test_exec_status & 0x0f));
-          }
-          break;
-       default:
-          pout("(%4d)\tReserved.\n",
-                  (int)data->self_test_exec_status);
-          break;
-   }
-        
+  unsigned char status = data->self_test_exec_status;
+  jout("Self-test execution status:      ");
+
+  switch (data->self_test_exec_status >> 4) {
+    case 0:
+      jout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t", status);
+      jout("without error or no self-test has ever \n\t\t\t\t\tbeen run.\n");
+      break;
+    case 1:
+      jout("(%4d)\tThe self-test routine was aborted by\n\t\t\t\t\t", status);
+      jout("the host.\n");
+      break;
+    case 2:
+      jout("(%4d)\tThe self-test routine was interrupted\n\t\t\t\t\t", status);
+      jout("by the host with a hard or soft reset.\n");
+      break;
+    case 3:
+      jout("(%4d)\tA fatal error or unknown test error\n\t\t\t\t\t", status);
+      jout("occurred while the device was executing\n\t\t\t\t\t");
+      jout("its self-test routine and the device \n\t\t\t\t\t");
+      jout("was unable to complete the self-test \n\t\t\t\t\t");
+      jout("routine.\n");
+      break;
+    case 4:
+      jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
+      jout("a test element that failed and the test\n\t\t\t\t\t");
+      jout("element that failed is not known.\n");
+      break;
+    case 5:
+      jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
+      jout("the electrical element of the test\n\t\t\t\t\t");
+      jout("failed.\n");
+      break;
+    case 6:
+      jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
+      jout("the servo (and/or seek) element of the \n\t\t\t\t\t");
+      jout("test failed.\n");
+      break;
+    case 7:
+      jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
+      jout("the read element of the test failed.\n");
+      break;
+    case 8:
+      jout("(%4d)\tThe previous self-test completed having\n\t\t\t\t\t", status);
+      jout("a test element that failed and the\n\t\t\t\t\t");
+      jout("device is suspected of having handling\n\t\t\t\t\t");
+      jout("damage.\n");
+      break;
+    case 15:
+      if (firmwarebugs.is_set(BUG_SAMSUNG3) && data->self_test_exec_status == 0xf0) {
+        pout("(%4d)\tThe previous self-test routine completed\n\t\t\t\t\t", status);
+        pout("with unknown result or self-test in\n\t\t\t\t\t");
+        pout("progress with less than 10%% remaining.\n");
+      }
+      else {
+        jout("(%4d)\tSelf-test routine in progress...\n\t\t\t\t\t", status);
+        jout("%1d0%% of test remaining.\n", status & 0x0f);
+      }
+      break;
+    default:
+      jout("(%4d)\tReserved.\n", status);
+      break;
+  }
+
+  json::ref jref = jglb["ata_smart_data"]["self_test"]["status"];
+
+  jref["value"] = status;
+  const char * msg;
+  // TODO: Use common function for smartctl/smartd
+  switch (status >> 4) {
+    case 0x0: msg = "completed without error"; break;
+    case 0x1: msg = "was aborted by the host"; break;
+    case 0x2: msg = "was interrupted by the host with a reset"; break;
+    case 0x3: msg = "could not complete due to a fatal or unknown error"; break;
+    case 0x4: msg = "completed with error (unknown test element)"; break;
+    case 0x5: msg = "completed with error (electrical test element)"; break;
+    case 0x6: msg = "completed with error (servo/seek test element)"; break;
+    case 0x7: msg = "completed with error (read test element)"; break;
+    case 0x8: msg = "completed with error (handling damage?)"; break;
+    default:  msg = 0;
+  }
+  if (msg) {
+    jref["string"] = msg;
+    switch (status >> 4) {
+      case 0x1: case 0x2: case 0x3: break; // aborted -> unknown
+      default: jref["passed"] = ((status >> 4) == 0x0);
+    }
+  }
+  else if ((status >> 4) == 0xf) {
+    jref["string"] = strprintf("in progress, %u0%% remaining", status & 0xf);
+    jref["remaining_percent"] = (status & 0xf) * 10;
+  }
 }
 
 static void PrintSmartTotalTimeCompleteOffline (const ata_smart_values * data)
 {
-  pout("Total time to complete Offline \n");
-  pout("data collection: \t\t(%5d) seconds.\n", 
+  jout("Total time to complete Offline \n");
+  jout("data collection: \t\t(%5d) seconds.\n",
        (int)data->total_time_to_complete_off_line);
+
+  jglb["ata_smart_data"]["offline_data_collection"]["completion_seconds"] =
+      data->total_time_to_complete_off_line;
 }
 
 static void PrintSmartOfflineCollectCap(const ata_smart_values *data)
 {
-  pout("Offline data collection\n");
-  pout("capabilities: \t\t\t (0x%02x) ",
+  json::ref jref = jglb["ata_smart_data"]["capabilities"];
+
+  jout("Offline data collection\n");
+  jout("capabilities: \t\t\t (0x%02x) ",
        (int)data->offline_data_collection_capability);
+  jref["values"][0] = data->offline_data_collection_capability;
   
   if (data->offline_data_collection_capability == 0x00){
-    pout("\tOffline data collection not supported.\n");
+    jout("\tOffline data collection not supported.\n");
   } 
   else {
-    pout( "%s\n", isSupportExecuteOfflineImmediate(data)?
+    jout( "%s\n", isSupportExecuteOfflineImmediate(data)?
           "SMART execute Offline immediate." :
           "No SMART execute Offline immediate.");
-    
+    jref["exec_offline_immediate_supported"] = isSupportExecuteOfflineImmediate(data);
+
+    // TODO: Bit 1 is vendor specific
     pout( "\t\t\t\t\t%s\n", isSupportAutomaticTimer(data)? 
           "Auto Offline data collection on/off support.":
           "No Auto Offline data collection support.");
-    
-    pout( "\t\t\t\t\t%s\n", isSupportOfflineAbort(data)? 
+
+    jout( "\t\t\t\t\t%s\n", isSupportOfflineAbort(data)?
           "Abort Offline collection upon new\n\t\t\t\t\tcommand.":
           "Suspend Offline collection upon new\n\t\t\t\t\tcommand.");
-    
-    pout( "\t\t\t\t\t%s\n", isSupportOfflineSurfaceScan(data)? 
+    jref["offline_is_aborted_upon_new_cmd"] = isSupportOfflineAbort(data);
+
+    jout( "\t\t\t\t\t%s\n", isSupportOfflineSurfaceScan(data)?
           "Offline surface scan supported.":
           "No Offline surface scan supported.");
-    
-    pout( "\t\t\t\t\t%s\n", isSupportSelfTest(data)? 
+    jref["offline_surface_scan_supported"] = isSupportOfflineSurfaceScan(data);
+
+    jout( "\t\t\t\t\t%s\n", isSupportSelfTest(data)?
           "Self-test supported.":
           "No Self-test supported.");
+    jref["self_tests_supported"] = isSupportSelfTest(data);
 
-    pout( "\t\t\t\t\t%s\n", isSupportConveyanceSelfTest(data)? 
+    jout( "\t\t\t\t\t%s\n", isSupportConveyanceSelfTest(data)?
           "Conveyance Self-test supported.":
           "No Conveyance Self-test supported.");
+    jref["conveyance_self_test_supported"] = isSupportConveyanceSelfTest(data);
 
-    pout( "\t\t\t\t\t%s\n", isSupportSelectiveSelfTest(data)? 
+    jout( "\t\t\t\t\t%s\n", isSupportSelectiveSelfTest(data)?
           "Selective Self-test supported.":
           "No Selective Self-test supported.");
+    jref["selective_self_test_supported"] = isSupportSelectiveSelfTest(data);
   }
 }
 
 static void PrintSmartCapability(const ata_smart_values *data)
 {
-   pout("SMART capabilities:            ");
-   pout("(0x%04x)\t", (int)data->smart_capability);
-   
-   if (data->smart_capability == 0x00)
-   {
-       pout("Automatic saving of SMART data\t\t\t\t\tis not implemented.\n");
-   } 
-   else 
-   {
-        
-      pout( "%s\n", (data->smart_capability & 0x01)? 
-              "Saves SMART data before entering\n\t\t\t\t\tpower-saving mode.":
-              "Does not save SMART data before\n\t\t\t\t\tentering power-saving mode.");
-                
-      if ( data->smart_capability & 0x02 )
-      {
-          pout("\t\t\t\t\tSupports SMART auto save timer.\n");
-      }
-   }
+  json::ref jref = jglb["ata_smart_data"]["capabilities"];
+
+  jout("SMART capabilities:            ");
+  jout("(0x%04x)\t", (int)data->smart_capability);
+  jref["values"][1] = data->smart_capability;
+
+  if (data->smart_capability == 0x00)
+    jout("Automatic saving of SMART data\t\t\t\t\tis not implemented.\n");
+  else {
+    jout("%s\n", (data->smart_capability & 0x01)?
+         "Saves SMART data before entering\n\t\t\t\t\tpower-saving mode.":
+         "Does not save SMART data before\n\t\t\t\t\tentering power-saving mode.");
+    jref["attribute_autosave_enabled"] = !!(data->smart_capability & 0x01);
+
+    // TODO: Info possibly invalid or misleading
+    // ATA-3 - ATA-5: Bit shall be set
+    // ATA-6 - ACS-3: Bit shall be set to indicate support for
+    // SMART ENABLE/DISABLE ATTRIBUTE AUTOSAVE
+    if (data->smart_capability & 0x02)
+      pout("\t\t\t\t\tSupports SMART auto save timer.\n");
+  }
 }
 
 static void PrintSmartErrorLogCapability(const ata_smart_values * data, const ata_identify_device * identity)
 {
-   pout("Error logging capability:       ");
-    
-   if ( isSmartErrorLogCapable(data, identity) )
-   {
-      pout(" (0x%02x)\tError logging supported.\n",
-               (int)data->errorlog_capability);
-   }
-   else {
-       pout(" (0x%02x)\tError logging NOT supported.\n",
-                (int)data->errorlog_capability);
-   }
+  bool capable = isSmartErrorLogCapable(data, identity);
+  jout("Error logging capability:        (0x%02x)\tError logging %ssupported.\n",
+       data->errorlog_capability, (capable ? "" : "NOT "));
+  jglb["ata_smart_data"]["capabilities"]["error_logging_supported"] = capable;
 }
 
 static void PrintSmartShortSelfTestPollingTime(const ata_smart_values * data)
 {
-  pout("Short self-test routine \n");
-  if (isSupportSelfTest(data))
-    pout("recommended polling time: \t (%4d) minutes.\n", 
+  jout("Short self-test routine \n");
+  if (isSupportSelfTest(data)) {
+    jout("recommended polling time: \t (%4d) minutes.\n",
          (int)data->short_test_completion_time);
+    jglb["ata_smart_data"]["self_test"]["polling_minutes"]["short"] =
+        data->short_test_completion_time;
+  }
   else
-    pout("recommended polling time: \t        Not Supported.\n");
+    jout("recommended polling time: \t        Not Supported.\n");
 }
 
 static void PrintSmartExtendedSelfTestPollingTime(const ata_smart_values * data)
 {
-  pout("Extended self-test routine\n");
-  if (isSupportSelfTest(data))
-    pout("recommended polling time: \t (%4d) minutes.\n", 
+  jout("Extended self-test routine\n");
+  if (isSupportSelfTest(data)) {
+    jout("recommended polling time: \t (%4d) minutes.\n",
          TestTime(data, EXTEND_SELF_TEST));
+    jglb["ata_smart_data"]["self_test"]["polling_minutes"]["extended"] =
+        TestTime(data, EXTEND_SELF_TEST);
+  }
   else
-    pout("recommended polling time: \t        Not Supported.\n");
+    jout("recommended polling time: \t        Not Supported.\n");
 }
 
 static void PrintSmartConveyanceSelfTestPollingTime(const ata_smart_values * data)
 {
-  pout("Conveyance self-test routine\n");
-  if (isSupportConveyanceSelfTest(data))
-    pout("recommended polling time: \t (%4d) minutes.\n", 
+  jout("Conveyance self-test routine\n");
+  if (isSupportConveyanceSelfTest(data)) {
+    jout("recommended polling time: \t (%4d) minutes.\n",
          (int)data->conveyance_test_completion_time);
+    jglb["ata_smart_data"]["self_test"]["polling_minutes"]["conveyance"] =
+        data->conveyance_test_completion_time;
+  }
   else
-    pout("recommended polling time: \t        Not Supported.\n");
+    jout("recommended polling time: \t        Not Supported.\n");
 }
 
 // Check SMART attribute table for Threshold failure
@@ -793,12 +1090,60 @@ static int find_failed_attr(const ata_smart_values * data,
   return 0;
 }
 
+static void set_json_globals_from_smart_attrib(int id, const char * name,
+                                               const ata_vendor_attr_defs & defs,
+                                               uint64_t rawval)
+{
+  switch (id) {
+    case 9:
+      if (!str_starts_with(name, "Power_On_"))
+        return;
+      {
+        int minutes = -1;
+        switch (defs[id].raw_format) {
+          case RAWFMT_DEFAULT: case RAWFMT_RAW48: case RAWFMT_RAW64:
+          case RAWFMT_RAW16_OPT_RAW16: case RAWFMT_RAW24_OPT_RAW8: break;
+          case RAWFMT_SEC2HOUR: minutes = (rawval / 60) % 60; rawval /= 60*60; break;
+          case RAWFMT_MIN2HOUR: minutes = rawval % 60; rawval /= 60; break;
+          case RAWFMT_HALFMIN2HOUR: minutes = (rawval / 2) % 60; rawval /= 2*60; break;
+          case RAWFMT_MSEC24_HOUR32:
+            minutes = (int)(rawval >> 32) / (1000*60);
+            if (minutes >= 60)
+              minutes = -1;
+            rawval &= 0xffffffffULL;
+            break;
+          default: return;
+        }
+        if (rawval > 0x00ffffffULL)
+          return; // assume bogus value
+        jglb["power_on_time"]["hours"] = rawval;
+        if (minutes >= 0)
+          jglb["power_on_time"]["minutes"] = minutes;
+      }
+      break;
+    case 12:
+      if (strcmp(name, "Power_Cycle_Count"))
+        return;
+      switch (defs[id].raw_format) {
+        case RAWFMT_DEFAULT: case RAWFMT_RAW48: case RAWFMT_RAW64:
+        case RAWFMT_RAW16_OPT_RAW16: case RAWFMT_RAW24_OPT_RAW8: break;
+        default: return;
+      }
+      if (rawval > 0x00ffffffULL)
+        return; // assume bogus value
+      jglb["power_cycle_count"] = rawval;
+      break;
+    //case 194:
+    // Temperature set separately from ata_return_temperature_value() below
+  }
+}
+
 // onlyfailed=0 : print all attribute values
 // onlyfailed=1:  just ones that are currently failed and have prefailure bit set
 // onlyfailed=2:  ones that are failed, or have failed with or without prefailure bit set
 static void PrintSmartAttribWithThres(const ata_smart_values * data,
                                       const ata_smart_thresholds_pvt * thresholds,
-                                      const ata_vendor_attr_defs & defs,
+                                      const ata_vendor_attr_defs & defs, int rpm,
                                       int onlyfailed, unsigned char format)
 {
   bool brief  = !!(format & ata_print_options::FMT_BRIEF);
@@ -807,7 +1152,7 @@ static void PrintSmartAttribWithThres(const ata_smart_values * data,
   bool needheader = true;
 
   // step through all vendor attributes
-  for (int i = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
+  for (int i = 0, ji = 0; i < NUMBER_ATA_SMART_ATTRIBUTES; i++) {
     const ata_smart_attribute & attr = data->vendor_attributes[i];
 
     // Check attribute and threshold
@@ -826,14 +1171,15 @@ static void PrintSmartAttribWithThres(const ata_smart_values * data,
     // print header only if needed
     if (needheader) {
       if (!onlyfailed) {
-        pout("SMART Attributes Data Structure revision number: %d\n",(int)data->revnumber);
-        pout("Vendor Specific SMART Attributes with Thresholds:\n");
+        jout("SMART Attributes Data Structure revision number: %d\n",(int)data->revnumber);
+        jglb["ata_smart_attributes"]["revision"] = data->revnumber;
+        jout("Vendor Specific SMART Attributes with Thresholds:\n");
       }
       if (!brief)
-        pout("ID#%s ATTRIBUTE_NAME          FLAG     VALUE WORST THRESH TYPE      UPDATED  WHEN_FAILED RAW_VALUE\n",
+        jout("ID#%s ATTRIBUTE_NAME          FLAG     VALUE WORST THRESH TYPE      UPDATED  WHEN_FAILED RAW_VALUE\n",
              (!hexid ? "" : " "));
       else
-        pout("ID#%s ATTRIBUTE_NAME          FLAGS    VALUE WORST THRESH FAIL RAW_VALUE\n",
+        jout("ID#%s ATTRIBUTE_NAME          FLAGS    VALUE WORST THRESH FAIL RAW_VALUE\n",
              (!hexid ? "" : " "));
       needheader = false;
     }
@@ -859,11 +1205,22 @@ static void PrintSmartAttribWithThres(const ata_smart_values * data,
     // Print line for each valid attribute
     std::string idstr = (!hexid ? strprintf("%3d",    attr.id)
                                 : strprintf("0x%02x", attr.id));
-    std::string attrname = ata_get_smart_attr_name(attr.id, defs);
+    std::string attrname = ata_get_smart_attr_name(attr.id, defs, rpm);
     std::string rawstr = ata_format_attr_raw_value(attr, defs);
 
+    char flagstr[] = {
+      (ATTRIBUTE_FLAGS_PREFAILURE(attr.flags)     ? 'P' : '-'),
+      (ATTRIBUTE_FLAGS_ONLINE(attr.flags)         ? 'O' : '-'),
+      (ATTRIBUTE_FLAGS_PERFORMANCE(attr.flags)    ? 'S' : '-'),
+      (ATTRIBUTE_FLAGS_ERRORRATE(attr.flags)      ? 'R' : '-'),
+      (ATTRIBUTE_FLAGS_EVENTCOUNT(attr.flags)     ? 'C' : '-'),
+      (ATTRIBUTE_FLAGS_SELFPRESERVING(attr.flags) ? 'K' : '-'),
+      (ATTRIBUTE_FLAGS_OTHER(attr.flags)          ? '+' : ' '),
+      0
+    };
+
     if (!brief)
-      pout("%s %-24s0x%04x   %-4s  %-4s  %-4s   %-10s%-9s%-12s%s\n",
+      jout("%s %-24s0x%04x   %-4s  %-4s  %-4s   %-10s%-9s%-12s%s\n",
            idstr.c_str(), attrname.c_str(), attr.flags,
            valstr.c_str(), worstr.c_str(), threstr.c_str(),
            (ATTRIBUTE_FLAGS_PREFAILURE(attr.flags) ? "Pre-fail" : "Old_age"),
@@ -873,27 +1230,54 @@ static void PrintSmartAttribWithThres(const ata_smart_values * data,
                                            : "    -"        ) ,
             rawstr.c_str());
     else
-      pout("%s %-24s%c%c%c%c%c%c%c  %-4s  %-4s  %-4s   %-5s%s\n",
-           idstr.c_str(), attrname.c_str(),
-           (ATTRIBUTE_FLAGS_PREFAILURE(attr.flags)     ? 'P' : '-'),
-           (ATTRIBUTE_FLAGS_ONLINE(attr.flags)         ? 'O' : '-'),
-           (ATTRIBUTE_FLAGS_PERFORMANCE(attr.flags)    ? 'S' : '-'),
-           (ATTRIBUTE_FLAGS_ERRORRATE(attr.flags)      ? 'R' : '-'),
-           (ATTRIBUTE_FLAGS_EVENTCOUNT(attr.flags)     ? 'C' : '-'),
-           (ATTRIBUTE_FLAGS_SELFPRESERVING(attr.flags) ? 'K' : '-'),
-           (ATTRIBUTE_FLAGS_OTHER(attr.flags)          ? '+' : ' '),
+      jout("%s %-24s%s  %-4s  %-4s  %-4s   %-5s%s\n",
+           idstr.c_str(), attrname.c_str(), flagstr,
            valstr.c_str(), worstr.c_str(), threstr.c_str(),
            (state == ATTRSTATE_FAILED_NOW  ? "NOW"  :
             state == ATTRSTATE_FAILED_PAST ? "Past"
                                            : "-"     ),
             rawstr.c_str());
 
+    if (!jglb.is_enabled())
+      continue;
+
+    json::ref jref = jglb["ata_smart_attributes"]["table"][ji++];
+    jref["id"] = attr.id;
+    jref["name"] = attrname;
+    if (state > ATTRSTATE_NO_NORMVAL)
+      jref["value"] = attr.current;
+    if (!(defs[attr.id].flags & ATTRFLAG_NO_WORSTVAL))
+      jref["worst"] = attr.worst;
+    if (state > ATTRSTATE_NO_THRESHOLD) {
+      jref["thresh"] = threshold;
+      jref["when_failed"] = (state == ATTRSTATE_FAILED_NOW  ? "now" :
+                             state == ATTRSTATE_FAILED_PAST ? "past"
+                                                            : ""     );
+    }
+
+    json::ref jreff = jref["flags"];
+    jreff["value"] = attr.flags;
+    jreff["string"] = flagstr;
+    jreff["prefailure"]     = !!ATTRIBUTE_FLAGS_PREFAILURE(attr.flags);
+    jreff["updated_online"] = !!ATTRIBUTE_FLAGS_ONLINE(attr.flags);
+    jreff["performance"]    = !!ATTRIBUTE_FLAGS_PERFORMANCE(attr.flags);
+    jreff["error_rate"]     = !!ATTRIBUTE_FLAGS_ERRORRATE(attr.flags);
+    jreff["event_count"]    = !!ATTRIBUTE_FLAGS_EVENTCOUNT(attr.flags);
+    jreff["auto_keep"]      = !!ATTRIBUTE_FLAGS_SELFPRESERVING(attr.flags);
+    if (ATTRIBUTE_FLAGS_OTHER(attr.flags))
+      jreff["other"] = ATTRIBUTE_FLAGS_OTHER(attr.flags);
+
+    uint64_t rawval = ata_get_attr_raw_value(attr, defs);
+    jref["raw"]["value"] = rawval;
+    jref["raw"]["string"] = rawstr;
+
+    set_json_globals_from_smart_attrib(attr.id, attrname.c_str(), defs, rawval);
   }
 
   if (!needheader) {
     if (!onlyfailed && brief) {
         int n = (!hexid ? 28 : 29);
-        pout("%*s||||||_ K auto-keep\n"
+        jout("%*s||||||_ K auto-keep\n"
              "%*s|||||__ C event count\n"
              "%*s||||___ R error rate\n"
              "%*s|||____ S speed/performance\n"
@@ -903,6 +1287,14 @@ static void PrintSmartAttribWithThres(const ata_smart_values * data,
     }
     pout("\n");
   }
+
+  if (!jglb.is_enabled())
+    return;
+
+  // Protocol independent temperature
+  unsigned char t = ata_return_temperature_value(data, defs);
+  if (t)
+    jglb["temperature"]["current"] = t;
 }
 
 // Print SMART related SCT capabilities
@@ -911,25 +1303,30 @@ static void ataPrintSCTCapability(const ata_identify_device *drive)
   unsigned short sctcaps = drive->words088_255[206-88];
   if (!(sctcaps & 0x01))
     return;
-  pout("SCT capabilities: \t       (0x%04x)\tSCT Status supported.\n", sctcaps);
+  json::ref jref = jglb["ata_sct_capabilities"];
+  jout("SCT capabilities: \t       (0x%04x)\tSCT Status supported.\n", sctcaps);
+  jref["value"] = sctcaps;
   if (sctcaps & 0x08)
-    pout("\t\t\t\t\tSCT Error Recovery Control supported.\n");
+    jout("\t\t\t\t\tSCT Error Recovery Control supported.\n");
+  jref["error_recovery_control_supported"] = !!(sctcaps & 0x08);
   if (sctcaps & 0x10)
-    pout("\t\t\t\t\tSCT Feature Control supported.\n");
+    jout("\t\t\t\t\tSCT Feature Control supported.\n");
+  jref["feature_control_supported"] = !!(sctcaps & 0x10);
   if (sctcaps & 0x20)
-    pout("\t\t\t\t\tSCT Data Table supported.\n");
+    jout("\t\t\t\t\tSCT Data Table supported.\n");
+  jref["data_table_supported"] = !!(sctcaps & 0x20);
 }
 
 
 static void PrintGeneralSmartValues(const ata_smart_values *data, const ata_identify_device *drive,
-                                    unsigned char fix_firmwarebug)
+                                    firmwarebug_defs firmwarebugs)
 {
-  pout("General SMART Values:\n");
+  jout("General SMART Values:\n");
   
   PrintSmartOfflineStatus(data); 
   
   if (isSupportSelfTest(data)){
-    PrintSmartSelfExecStatus(data, fix_firmwarebug);
+    PrintSmartSelfExecStatus(data, firmwarebugs);
   }
   
   PrintSmartTotalTimeCompleteOffline(data);
@@ -938,9 +1335,11 @@ static void PrintGeneralSmartValues(const ata_smart_values *data, const ata_iden
   
   PrintSmartErrorLogCapability(data, drive);
 
-  pout( "\t\t\t\t\t%s\n", isGeneralPurposeLoggingCapable(drive)?
+  jout( "\t\t\t\t\t%s\n", isGeneralPurposeLoggingCapable(drive)?
         "General Purpose Logging supported.":
         "No General Purpose Logging support.");
+  jglb["ata_smart_data"]["capabilities"]["gp_logging_supported"] =
+       isGeneralPurposeLoggingCapable(drive);
 
   if (isSupportSelfTest(data)){
     PrintSmartShortSelfTestPollingTime (data);
@@ -951,7 +1350,7 @@ static void PrintGeneralSmartValues(const ata_smart_values *data, const ata_iden
 
   ataPrintSCTCapability(drive);
 
-  pout("\n");
+  jout("\n");
 }
 
 // Get # sectors of a log addr, 0 if log does not exist.
@@ -971,36 +1370,51 @@ static unsigned GetNumLogSectors(const ata_smart_log_directory * logdir, unsigne
 }
 
 // Get name of log.
-// Table A.2 of T13/2161-D Revision 2 (ACS-3), February 21, 2012.
 static const char * GetLogName(unsigned logaddr)
 {
+    // Table A.2 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
+    // Table 112 of Serial ATA Revision 3.2, August 7, 2013
+    // Table A.2 of T13/2161-D (ACS-3) Revision 5, October 28, 2013
+    // Table 204 of T13/BSR INCITS 529 (ACS-4) Revision 16, February 21, 2017
     switch (logaddr) {
       case 0x00: return "Log Directory";
       case 0x01: return "Summary SMART error log";
       case 0x02: return "Comprehensive SMART error log";
       case 0x03: return "Ext. Comprehensive SMART error log";
       case 0x04: return "Device Statistics log";
-      case 0x05: return "Reserved for the CFA"; // ACS-2
+      case 0x05: return "Reserved for CFA"; // ACS-2
       case 0x06: return "SMART self-test log";
       case 0x07: return "Extended self-test log";
       case 0x08: return "Power Conditions log"; // ACS-2
       case 0x09: return "Selective self-test log";
+      case 0x0a: return "Device Statistics Notification"; // ACS-3
+      case 0x0b: return "Reserved for CFA"; // ACS-3
+      case 0x0c: return "Pending Defects log"; // ACS-4
       case 0x0d: return "LPS Mis-alignment log"; // ACS-2
+      case 0x0e: return "Reserved for ZAC-2"; // ACS-4
+      case 0x0f: return "Sense Data for Successful NCQ Cmds log"; // ACS-4
       case 0x10: return "NCQ Command Error log";
-      case 0x11: return "SATA Phy Event Counters";
-      case 0x12: return "SATA NCQ Queue Management log"; // ACS-3
-      case 0x13: return "SATA NCQ Send and Receive log"; // ACS-3
-      case 0x14:
-      case 0x15:
-      case 0x16: return "Reserved for Serial ATA";
+      case 0x11: return "SATA Phy Event Counters log";
+    //case 0x12: return "SATA NCQ Queue Management log"; // SATA 3.0/3.1, ACS-3
+      case 0x12: return "SATA NCQ Non-Data log"; // SATA 3.2, ACS-4
+      case 0x13: return "SATA NCQ Send and Receive log"; // SATA 3.1, ACS-3
+      case 0x14: return "Hybrid Information log"; // SATA 3.2, ACS-4
+      case 0x15: return "Rebuild Assist log"; // SATA 3.2, ACS-4
+      case 0x16:
+      case 0x17: return "Reserved for Serial ATA";
+
       case 0x19: return "LBA Status log"; // ACS-3
-      case 0x20: return "Streaming performance log"; // Obsolete
+
+      case 0x20: return "Streaming performance log [OBS-8]";
       case 0x21: return "Write stream error log";
       case 0x22: return "Read stream error log";
-      case 0x23: return "Delayed sector log"; // Obsolete
+      case 0x23: return "Delayed sector log [OBS-8]";
       case 0x24: return "Current Device Internal Status Data log"; // ACS-3
       case 0x25: return "Saved Device Internal Status Data log"; // ACS-3
+
+      case 0x2f: return "Set Sector Configuration";; // ACS-4
       case 0x30: return "IDENTIFY DEVICE data log"; // ACS-3
+
       case 0xe0: return "SCT Command/Status";
       case 0xe1: return "SCT Data Transfer";
       default:
@@ -1013,18 +1427,65 @@ static const char * GetLogName(unsigned logaddr)
     /*NOTREACHED*/
 }
 
+// Get log access permissions
+static const char * get_log_rw(unsigned logaddr)
+{
+   if (   (                   logaddr <= 0x08)
+       || (0x0c <= logaddr && logaddr <= 0x0d)
+       || (0x0f <= logaddr && logaddr <= 0x14)
+       || (0x19 == logaddr)
+       || (0x20 <= logaddr && logaddr <= 0x25)
+       || (0x30 == logaddr))
+      return "R/O";
+
+   if (   (0x09 <= logaddr && logaddr <= 0x0a)
+       || (0x15 == logaddr)
+       || (0x80 <= logaddr && logaddr <= 0x9f)
+       || (0xe0 <= logaddr && logaddr <= 0xe1))
+      return "R/W";
+
+   if (0xa0 <= logaddr && logaddr <= 0xdf)
+      return "VS"; // Vendor specific
+
+   return "-"; // Unknown/Reserved
+}
+
+// Init a fake log directory, assume that standard logs are supported
+const ata_smart_log_directory * fake_logdir(ata_smart_log_directory * logdir,
+  const ata_print_options & options)
+{
+  memset(logdir, 0, sizeof(*logdir));
+  logdir->logversion = 255;
+  logdir->entry[0x01-1].numsectors = 1;
+  logdir->entry[0x03-1].numsectors = (options.smart_ext_error_log + (4-1)) / 4;
+  logdir->entry[0x04-1].numsectors = 8;
+  logdir->entry[0x06-1].numsectors = 1;
+  logdir->entry[0x07-1].numsectors = (options.smart_ext_selftest_log + (19-1)) / 19;
+  logdir->entry[0x09-1].numsectors = 1;
+  logdir->entry[0x11-1].numsectors = 1;
+  return logdir;
+}
+
 // Print SMART and/or GP Log Directory
 static void PrintLogDirectories(const ata_smart_log_directory * gplogdir,
                                 const ata_smart_log_directory * smartlogdir)
 {
-  if (gplogdir)
-    pout("General Purpose Log Directory Version %u\n", gplogdir->logversion);
-  if (smartlogdir)
-    pout("SMART %sLog Directory Version %u%s\n",
+  json::ref jref = jglb["ata_log_directory"];
+  if (gplogdir) {
+    jout("General Purpose Log Directory Version %u\n", gplogdir->logversion);
+    jref["gp_dir_version"] = gplogdir->logversion;
+  }
+  if (smartlogdir) {
+    jout("SMART %sLog Directory Version %u%s\n",
          (gplogdir ? "          " : ""), smartlogdir->logversion,
          (smartlogdir->logversion==1 ? " [multi-sector log support]" : ""));
+    jref["smart_dir_version"] = smartlogdir->logversion;
+    jref["smart_dir_multi_sector"] = (smartlogdir->logversion == 1);
+  }
+
+  jout("Address    Access  R/W   Size  Description\n");
 
-  for (unsigned i = 0; i <= 0xff; i++) {
+  for (unsigned i = 0, ji = 0; i <= 0xff; i++) {
     // Get number of sectors
     unsigned smart_numsect = GetNumLogSectors(smartlogdir, i, false);
     unsigned gp_numsect    = GetNumLogSectors(gplogdir   , i, true );
@@ -1032,21 +1493,65 @@ static void PrintLogDirectories(const ata_smart_log_directory * gplogdir,
     if (!(smart_numsect || gp_numsect))
       continue; // Log does not exist
 
+    const char * acc; unsigned size;
+    if (smart_numsect == gp_numsect) {
+      acc = "GPL,SL"; size = gp_numsect;
+    }
+    else if (!smart_numsect) {
+      acc = "GPL"; size = gp_numsect;
+    }
+    else if (!gp_numsect) {
+      acc = "    SL"; size = smart_numsect;
+    }
+    else {
+      acc = 0; size = 0;
+    }
+
+    unsigned i2 = i;
+    if (acc && ((0x80 <= i && i < 0x9f) || (0xa0 <= i && i < 0xdf))) {
+      // Find range of Host/Device vendor specific logs with same size
+      unsigned imax = (i < 0x9f ? 0x9f : 0xdf);
+      for (unsigned j = i+1; j <= imax; j++) {
+          unsigned sn = GetNumLogSectors(smartlogdir, j, false);
+          unsigned gn = GetNumLogSectors(gplogdir   , j, true );
+
+          if (!(sn == smart_numsect && gn == gp_numsect))
+            break;
+          i2 = j;
+      }
+    }
+
     const char * name = GetLogName(i);
+    const char * rw = get_log_rw(i);
 
-    // Print name and length of log.
-    // If both SMART and GP exist, print separate entries if length differ.
-    if (smart_numsect == gp_numsect)
-      pout(  "GP/S  Log at address 0x%02x has %4d sectors [%s]\n", i, smart_numsect, name);
+    if (i2 > i)
+      jout("0x%02x-0x%02x  %-6s  %-3s  %5u  %s\n", i, i2, acc, rw, size, name);
+    else if (acc)
+      jout(  "0x%02x       %-6s  %-3s  %5u  %s\n", i, acc, rw, size, name);
     else {
+      // GPL and SL support different sizes
+      jout(  "0x%02x       %-6s  %-3s  %5u  %s\n", i, "GPL", rw, gp_numsect, name);
+      jout(  "0x%02x       %-6s  %-3s  %5u  %s\n", i, "SL", rw, smart_numsect, name);
+    }
+
+    for (;;) {
+      json::ref jrefi = jref["table"][ji++];
+      jrefi["address"] = i;
+      jrefi["name"] = name;
+      if (rw[0] == 'R' && rw[1] && rw[2]) {
+        jrefi["read"] = true;
+        jrefi["write"] = (rw[2] == 'W');
+      }
       if (gp_numsect)
-        pout("GP %sLog at address 0x%02x has %4d sectors [%s]\n", (smartlogdir?"   ":""),
-             i, gp_numsect, name);
+        jrefi["gp_sectors"] = gp_numsect;
       if (smart_numsect)
-        pout("SMART Log at address 0x%02x has %4d sectors [%s]\n", i, smart_numsect, name);
+        jrefi["smart_sectors"] = smart_numsect;
+      if (i >= i2)
+        break;
+      i++;
     }
   }
-  pout("\n");
+  jout("\n");
 }
 
 // Print hexdump of log pages.
@@ -1078,9 +1583,8 @@ static void PrintLogPages(const char * type, const unsigned char * data,
 ///////////////////////////////////////////////////////////////////////
 // Device statistics (Log 0x04)
 
-// See Section A.5 of
-//   ATA/ATAPI Command Set - 3 (ACS-3)
-//   T13/2161-D Revision 2, February 21, 2012.
+// Section A.5 of T13/2161-D (ACS-3) Revision 5, October 28, 2013
+// Section 9.5 of T13/BSR INCITS 529 (ACS-4) Revision 20, October 26, 2017
 
 struct devstat_entry_info
 {
@@ -1096,12 +1600,17 @@ const devstat_entry_info devstat_info_0x00[] = {
 const devstat_entry_info devstat_info_0x01[] = {
   {  2, "General Statistics" },
   {  4, "Lifetime Power-On Resets" },
-  {  4, "Power-on Hours" }, // spec says no flags(?)
+  {  4, "Power-on Hours" },
   {  6, "Logical Sectors Written" },
   {  6, "Number of Write Commands" },
   {  6, "Logical Sectors Read" },
   {  6, "Number of Read Commands" },
   {  6, "Date and Time TimeStamp" }, // ACS-3
+  {  4, "Pending Error Count" }, // ACS-4
+  {  2, "Workload Utilization" }, // ACS-4
+  {  6, "Utilization Usage Rate" }, // ACS-4 (TODO: field provides 3 values)
+  {  2, "Resource Availability" }, // ACS-4
+  {  1, "Random Write Resources Used" }, // ACS-4
   {  0, 0 }
 };
 
@@ -1121,6 +1630,7 @@ const devstat_entry_info devstat_info_0x03[] = {
   {  4, "Read Recovery Attempts" },
   {  4, "Number of Mechanical Start Failures" },
   {  4, "Number of Realloc. Candidate Logical Sectors" }, // ACS-3
+  {  4, "Number of High Priority Unload Events" }, // ACS-3
   {  0, 0 }
 };
 
@@ -1129,6 +1639,7 @@ const devstat_entry_info devstat_info_0x04[] = {
   {  4, "Number of Reported Uncorrectable Errors" },
 //{  4, "Number of Resets Between Command Acceptance and Command Completion" },
   {  4, "Resets Between Cmd Acceptance and Completion" },
+  {  4, "Physical Element Status Changed" }, // ACS-4
   {  0, 0 }
 };
 
@@ -1173,30 +1684,67 @@ const devstat_entry_info * devstat_infos[] = {
   devstat_info_0x05,
   devstat_info_0x06,
   devstat_info_0x07
+  // TODO: 0x08 Zoned Device Statistics (T13/f16136r7, January 2017)
 };
 
 const int num_devstat_infos = sizeof(devstat_infos)/sizeof(devstat_infos[0]);
 
-static void print_device_statistics_page(const unsigned char * data, int page,
-  bool & need_trailer)
+static const char * get_device_statistics_page_name(int page)
+{
+  if (page < num_devstat_infos)
+    return devstat_infos[page][0].name;
+  if (page == 0xff)
+    return "Vendor Specific Statistics"; // ACS-4
+  return "Unknown Statistics";
+}
+
+static void set_json_globals_from_device_statistics(int page, int offset, int64_t val)
+{
+  switch (page) {
+    case 1:
+      switch (offset) {
+        case 0x008: jglb["power_cycle_count"] = val; break; // ~= Lifetime Power-On Resets
+        case 0x010: jglb["power_on_time"]["hours"]= val; break;
+      }
+      break;
+    case 5:
+      switch (offset) {
+        case 0x008: jglb["temperature"]["current"] = val; break;
+        case 0x020: jglb["temperature"]["lifetime_max"] = val; break;
+        case 0x028: jglb["temperature"]["lifetime_min"] = val; break;
+        case 0x050: jglb["temperature"]["lifetime_over_limit_minutes"] = val; break;
+        case 0x058: jglb["temperature"]["op_limit_max"] = val; break;
+        case 0x060: jglb["temperature"]["lifetime_under_limit_minutes"] = val; break;
+        case 0x068: jglb["temperature"]["op_limit_min"] = val; break;
+      }
+      break;
+  }
+}
+
+static void print_device_statistics_page(const json::ref & jref, const unsigned char * data, int page)
 {
   const devstat_entry_info * info = (page < num_devstat_infos ? devstat_infos[page] : 0);
-  const char * name = (info ? info[0].name : "Unknown Statistics");
+  const char * name = get_device_statistics_page_name(page);
 
   // Check page number in header
-  static const char line[] = "  =====  =                =  == ";
+  static const char line[] = "  =====  =               =  ===  == ";
   if (!data[2]) {
-    pout("%3d%s%s (empty) ==\n", page, line, name);
+    pout("0x%02x%s%s (empty) ==\n", page, line, name);
     return;
   }
   if (data[2] != page) {
-    pout("%3d%s%s (invalid page %d in header) ==\n", page, line, name, data[2]);
+    pout("0x%02x%s%s (invalid page 0x%02x in header) ==\n", page, line, name, data[2]);
     return;
   }
 
-  pout("%3d%s%s (rev %d) ==\n", page, line, name, data[0]);
+  int rev = data[0] | (data[1] << 8);
+  jout("0x%02x%s%s (rev %d) ==\n", page, line, name, rev);
+  jref["number"] = page;
+  jref["name"] = name;
+  jref["revision"] = rev;
 
   // Print entries
+  int ji = 0;
   for (int i = 1, offset = 8; offset < 512-7; i++, offset+=8) {
     // Check for last known entry
     if (info && !info[i].size)
@@ -1207,52 +1755,103 @@ static void print_device_statistics_page(const unsigned char * data, int page,
     if (!(flags & 0x80))
       continue;
 
+    // Stop if unknown entries contain garbage data due to buggy firmware
+    if (!info && (data[offset+5] || data[offset+6])) {
+      pout("0x%02x  0x%03x  -               -  [Trailing garbage ignored]\n", page, offset);
+      break;
+    }
+
+    // Get value name
+    const char * valname = (info           ? info[i].name :
+                            (page == 0xff) ? "Vendor Specific" // ACS-4
+                                           : "Unknown"        );
+
     // Get value size, default to max if unknown
     int size = (info ? info[i].size : 7);
 
+    // Get flags (supported flag already checked above)
+    bool valid = !!(flags & 0x40);
+    bool normalized = !!(flags & 0x20);
+    bool supports_dsn = !!(flags & 0x10); // ACS-3
+    bool monitored_condition_met = !!(flags & 0x08); // ACS-3
+    unsigned char reserved_flags = (flags & 0x07);
+
     // Format value
+    int64_t val = 0;
     char valstr[32];
-    if (flags & 0x40) { // valid flag
+    if (valid) {
       // Get value
-      int64_t val;
       if (size < 0) {
         val = (signed char)data[offset];
       }
       else {
-        val = 0;
         for (int j = 0; j < size; j++)
           val |= (int64_t)data[offset+j] << (j*8);
       }
-      snprintf(valstr, sizeof(valstr), "%"PRId64, val);
+      snprintf(valstr, sizeof(valstr), "%" PRId64, val);
     }
     else {
       // Value not known (yet)
-      strcpy(valstr, "-");
+      valstr[0] = '-'; valstr[1] = 0;
     }
 
-    pout("%3d  0x%03x  %d%c %15s%c %s\n",
-      page, offset,
-      abs(size),
-      (flags & 0x1f ? '+' : ' '), // unknown flags
-      valstr,
-      (flags & 0x20 ? '~' : ' '), // normalized flag
-      (info ? info[i].name : "Unknown"));
-    if (flags & 0x20)
-      need_trailer = true;
+    char flagstr[] = {
+      (valid ? 'V' : '-'), // JSON only
+      (normalized ? 'N' : '-'),
+      (supports_dsn ? 'D' : '-'),
+      (monitored_condition_met ? 'C' : '-'),
+      (reserved_flags ? '+' : ' '),
+      0
+    };
+
+    jout("0x%02x  0x%03x  %d %15s  %s %s\n",
+      page, offset, abs(size), valstr, flagstr+1, valname);
+
+    if (!jglb.is_enabled())
+      continue;
+
+    json::ref jrefi = jref["table"][ji++];
+    jrefi["offset"] = offset;
+    jrefi["name"] = valname;
+    jrefi["size"] = abs(size);
+    if (valid)
+      jrefi["value"] = val; // TODO: May be unsafe JSON int if size > 6
+
+    json::ref jreff = jrefi["flags"];
+    jreff["value"] = flags;
+    jreff["string"] = flagstr;
+    jreff["valid"] = valid;
+    jreff["normalized"] = normalized;
+    jreff["supports_dsn"] = supports_dsn;
+    jreff["monitored_condition_met"] = monitored_condition_met;
+    if (reserved_flags)
+      jreff["other"] = reserved_flags;
+
+    if (valid)
+      set_json_globals_from_device_statistics(page, offset, val);
   }
 }
 
 static bool print_device_statistics(ata_device * device, unsigned nsectors,
-  const std::vector<int> & single_pages, bool all_pages, bool ssd_page)
+  const std::vector<int> & single_pages, bool all_pages, bool ssd_page,
+  bool use_gplog)
 {
   // Read list of supported pages from page 0
   unsigned char page_0[512] = {0, };
-  if (!ataReadLogExt(device, 0x04, 0, 0, page_0, 1))
+  int rc;
+  
+  if (use_gplog)
+    rc = ataReadLogExt(device, 0x04, 0, 0, page_0, 1);
+  else
+    rc = ataReadSmartLog(device, 0x04, page_0, 1);
+  if (!rc) {
+    jerr("Read Device Statistics page 0x00 failed\n\n");
     return false;
+  }
 
   unsigned char nentries = page_0[8];
   if (!(page_0[2] == 0 && nentries > 0)) {
-    pout("Device Statistics page 0 is invalid (page=%d, nentries=%d)\n", page_0[2], nentries);
+    jerr("Device Statistics page 0x00 is invalid (page=0x%02x, nentries=%d)\n\n", page_0[2], nentries);
     return false;
   }
 
@@ -1271,48 +1870,141 @@ static bool print_device_statistics(ata_device * device, unsigned nsectors,
   // Add manually specified pages
   bool print_page_0 = false;
   for (i = 0; i < single_pages.size() || ssd_page; i++) {
-    int page = (i < single_pages.size() ? single_pages[i] : 7);
+    int page = (i < single_pages.size() ? single_pages[i] : 0x07);
     if (!page)
       print_page_0 = true;
     else if (page >= (int)nsectors)
-      pout("Device Statistics Log has only %u pages\n", nsectors);
+      pout("Device Statistics Log has only 0x%02x pages\n", nsectors);
     else
       pages.push_back(page);
-    if (page == 7)
+    if (page == 0x07)
       ssd_page = false;
   }
 
+  json::ref jref = jglb["ata_device_statistics"];
+
   // Print list of supported pages if requested
   if (print_page_0) {
-    pout("Device Statistics (GP Log 0x04) supported pages\n");
-    pout("Page Description\n");
+    pout("Device Statistics (%s Log 0x04) supported pages\n", 
+      use_gplog ? "GP" : "SMART");
+    jout("Page  Description\n");
     for (i = 0; i < nentries; i++) {
       int page = page_0[8+1+i];
-      pout("%3d  %s\n", page,
-        (page < num_devstat_infos ? devstat_infos[page][0].name : "Unknown Statistics"));
+      const char * name = get_device_statistics_page_name(page);
+      jout("0x%02x  %s\n", page, name);
+      jref["supported_pages"][i]["number"] = page;
+      jref["supported_pages"][i]["name"] = name;
     }
-    pout("\n");
+    jout("\n");
   }
 
   // Read & print pages
   if (!pages.empty()) {
-    pout("Device Statistics (GP Log 0x04)\n");
-    pout("Page Offset Size         Value  Description\n");
-    bool need_trailer = false;
+    pout("Device Statistics (%s Log 0x04)\n",
+      use_gplog ? "GP" : "SMART");
+    jout("Page  Offset Size        Value Flags Description\n");
+    int max_page = 0;
+
+    if (!use_gplog)
+      for (i = 0; i < pages.size(); i++) {
+        int page = pages[i];
+        if (max_page < page && page < 0xff)
+          max_page = page;
+      }
 
+    raw_buffer pages_buf((max_page+1) * 512);
+
+    if (!use_gplog && !ataReadSmartLog(device, 0x04, pages_buf.data(), max_page+1)) {
+      jerr("Read Device Statistics pages 0x00-0x%02x failed\n\n", max_page);
+      return false;
+    }
+
+    int ji = 0;
     for (i = 0; i <  pages.size(); i++) {
       int page = pages[i];
-      unsigned char page_n[512] = {0, };
-      if (!ataReadLogExt(device, 0x04, 0, page, page_n, 1))
+      if (use_gplog) {
+        if (!ataReadLogExt(device, 0x04, 0, page, pages_buf.data(), 1)) {
+          jerr("Read Device Statistics page 0x%02x failed\n\n", page);
+          return false;
+        }
+      }
+      else if (page > max_page)
+        continue;
+
+      int offset = (use_gplog ? 0 : page * 512);
+      print_device_statistics_page(jref["pages"][ji++], pages_buf.data() + offset, page);
+    }
+
+    jout("%32s|||_ C monitored condition met\n", "");
+    jout("%32s||__ D supports DSN\n", "");
+    jout("%32s|___ N normalized value\n\n", "");
+  }
+
+  return true;
+}
+
+
+///////////////////////////////////////////////////////////////////////
+// Pending Defects log (Log 0x0c)
+
+// Section 9.26 of T13/BSR INCITS 529 (ACS-4) Revision 20, October 26, 2017
+
+static bool print_pending_defects_log(ata_device * device, unsigned nsectors,
+  unsigned max_entries)
+{
+  // Read #entries from page 0
+  unsigned char page_buf[512] = {0, };
+  if (!ataReadLogExt(device, 0x0c, 0, 0, page_buf, 1)) {
+    pout("Read Pending Defects log page 0x00 failed\n\n");
+    return false;
+  }
+
+  jout("Pending Defects log (GP Log 0x0c)\n");
+  unsigned nentries = sg_get_unaligned_le32(page_buf);
+  json::ref jref = jglb["ata_pending_defects_log"];
+  jref["size"] = nsectors * 32 - 1;
+  jref["count"] = nentries;
+  if (!nentries) {
+    jout("No Defects Logged\n\n");
+    return true;
+  }
+
+  // Print entries
+  jout("Index                LBA    Hours\n");
+  for (unsigned i = 0, pi = 1, page = 0; i < nentries && i < max_entries; i++, pi++) {
+    // Read new page if required
+    if (pi >= 32) {
+      if (++page >= nsectors) {
+        pout("Pending Defects count %u exceeds log size (#pages=%u)\n\n",
+             nentries, nsectors);
+        return false;
+      }
+      if (!ataReadLogExt(device, 0x0c, 0, page, page_buf, 1)) {
+        pout("Read Pending Defects log page 0x%02x failed\n\n", page);
         return false;
-      print_device_statistics_page(page_n, page, need_trailer);
+      }
+      pi = 0;
     }
 
-    if (need_trailer)
-      pout("%30s|_ ~ normalized value\n", "");
-    pout("\n");
+    const unsigned char * entry = page_buf + 16 * pi;
+    unsigned hours = sg_get_unaligned_le32(entry);
+    char hourstr[32];
+    if (hours != 0xffffffffU)
+      snprintf(hourstr, sizeof(hourstr), "%u", hours);
+    else
+      hourstr[0] = '-', hourstr[1] = 0;
+    uint64_t lba = sg_get_unaligned_le64(entry + 8);
+    jout("%5u %18" PRIu64 " %8s\n", i, lba, hourstr);
+
+    json::ref jrefi = jref["table"][i];
+    jrefi["lba"].set_unsafe_uint64(lba);
+    if (hours != 0xffffffffU)
+      jrefi["power_on_hours"] = hours;
   }
 
+  if (nentries > max_entries)
+    pout("... (%u entries not shown)\n", nentries - max_entries);
+  jout("\n");
   return true;
 }
 
@@ -1324,13 +2016,13 @@ static void PrintSataPhyEventCounters(const unsigned char * data, bool reset)
 {
   if (checksum(data))
     checksumwarning("SATA Phy Event Counters");
-  pout("SATA Phy Event Counters (GP Log 0x11)\n");
+  jout("SATA Phy Event Counters (GP Log 0x11)\n");
   if (data[0] || data[1] || data[2] || data[3])
     pout("[Reserved: 0x%02x 0x%02x 0x%02x 0x%02x]\n",
     data[0], data[1], data[2], data[3]);
-  pout("ID      Size     Value  Description\n");
+  jout("ID      Size     Value  Description\n");
 
-  for (unsigned i = 4; ; ) {
+  for (unsigned i = 4, ji = 0; ; ) {
     // Get counter id and size (bits 14:12)
     unsigned id = data[i] | (data[i+1] << 8);
     unsigned size = ((id >> 12) & 0x7) << 1;
@@ -1373,16 +2065,43 @@ static void PrintSataPhyEventCounters(const unsigned char * data, bool reset)
       case 0x010: name = "R_ERR response for host-to-device data FIS, non-CRC"; break;
       case 0x012: name = "R_ERR response for host-to-device non-data FIS, CRC"; break;
       case 0x013: name = "R_ERR response for host-to-device non-data FIS, non-CRC"; break;
-      default:    name = (id & 0x8000 ? "Vendor specific" : "Unknown"); break;
+      default:    name = ((id & 0x8000) ? "Vendor specific" : "Unknown"); break;
     }
 
     // Counters stop at max value, add '+' in this case
-    pout("0x%04x  %u %12"PRIu64"%c %s\n", id, size, val,
+    jout("0x%04x  %u %12" PRIu64 "%c %s\n", id, size, val,
       (val == max_val ? '+' : ' '), name);
+
+    json::ref jref = jglb["sata_phy_event_counters"]["table"][ji++];
+    jref["id"] = id;
+    jref["name"] = name;
+    jref["size"] = size;
+    jref["value"] = val;
+    jref["overflow"] = (val == max_val);
   }
   if (reset)
-    pout("All counters reset\n");
-  pout("\n");
+    jout("All counters reset\n");
+  jout("\n");
+  jglb["sata_phy_event_counters"]["reset"] = reset;
+}
+
+// Format milliseconds from error log entry as "DAYS+H:M:S.MSEC"
+static std::string format_milliseconds(unsigned msec)
+{
+  unsigned days  = msec  / 86400000U;
+  msec          -= days  * 86400000U;
+  unsigned hours = msec  / 3600000U;
+  msec          -= hours * 3600000U;
+  unsigned min   = msec  / 60000U;
+  msec          -= min   * 60000U;
+  unsigned sec   = msec  / 1000U;
+  msec          -= sec   * 1000U;
+
+  std::string str;
+  if (days)
+    str = strprintf("%2ud+", days);
+  str += strprintf("%02u:%02u:%02u.%03u", hours, min, sec, msec);
+  return str;
 }
 
 // Get description for 'state' value from SMART Error Logs
@@ -1403,13 +2122,16 @@ static const char * get_error_log_state_desc(unsigned state)
 
 // returns number of errors
 static int PrintSmartErrorlog(const ata_smart_errorlog *data,
-                              unsigned char fix_firmwarebug)
+                              firmwarebug_defs firmwarebugs)
 {
-  pout("SMART Error Log Version: %d\n", (int)data->revnumber);
-  
+  json::ref jref = jglb["ata_smart_error_log"]["summary"];
+  jout("SMART Error Log Version: %d\n", (int)data->revnumber);
+  jref["revision"] = data->revnumber;
+
   // if no errors logged, return
   if (!data->error_log_pointer){
-    pout("No Errors Logged\n\n");
+    jout("No Errors Logged\n\n");
+    jref["count"] = 0;
     return 0;
   }
   print_on();
@@ -1422,19 +2144,22 @@ static int PrintSmartErrorlog(const ata_smart_errorlog *data,
   }
 
   // Some internal consistency checking of the data structures
-  if ((data->ata_error_count-data->error_log_pointer)%5 && fix_firmwarebug != FIX_SAMSUNG2) {
+  if ((data->ata_error_count-data->error_log_pointer) % 5 && !firmwarebugs.is_set(BUG_SAMSUNG2)) {
     pout("Warning: ATA error count %d inconsistent with error log pointer %d\n\n",
          data->ata_error_count,data->error_log_pointer);
   }
   
   // starting printing error log info
   if (data->ata_error_count<=5)
-    pout( "ATA Error Count: %d\n", (int)data->ata_error_count);
+    jout( "ATA Error Count: %d\n", (int)data->ata_error_count);
   else
-    pout( "ATA Error Count: %d (device log contains only the most recent five errors)\n",
+    jout( "ATA Error Count: %d (device log contains only the most recent five errors)\n",
            (int)data->ata_error_count);
+  jref["count"] = data->ata_error_count;
+  jref["logged_count"] = (data->ata_error_count <= 5 ? data->ata_error_count : 5);
+
   print_off();
-  pout("\tCR = Command Register [HEX]\n"
+  jout("\tCR = Command Register [HEX]\n"
        "\tFR = Features Register [HEX]\n"
        "\tSC = Sector Count Register [HEX]\n"
        "\tSN = Sector Number Register [HEX]\n"
@@ -1449,10 +2174,10 @@ static int PrintSmartErrorlog(const ata_smart_errorlog *data,
        "SS=sec, and sss=millisec. It \"wraps\" after 49.710 days.\n\n");
   
   // now step through the five error log data structures (table 39 of spec)
-  for (int k = 4; k >= 0; k-- ) {
+  for (int k = 4, ji = 0; k >= 0; k--) {
 
     // The error log data structure entries are a circular buffer
-    int j, i=(data->error_log_pointer+k)%5;
+    int i = (data->error_log_pointer + k) % 5;
     const ata_smart_errorlog_struct * elog = data->errorlog_struct+i;
     const ata_smart_errorlog_error_struct * summary = &(elog->error_struct);
 
@@ -1464,11 +2189,16 @@ static int PrintSmartErrorlog(const ata_smart_errorlog *data,
 
       // See table 42 of ATA5 spec
       print_on();
-      pout("Error %d occurred at disk power-on lifetime: %d hours (%d days + %d hours)\n",
+      jout("Error %d occurred at disk power-on lifetime: %d hours (%d days + %d hours)\n",
              (int)(data->ata_error_count+k-4), (int)summary->timestamp, days, (int)(summary->timestamp-24*days));
       print_off();
-      pout("  When the command that caused the error occurred, the device was %s.\n\n",msgstate);
-      pout("  After command completion occurred, registers were:\n"
+
+      json::ref jrefi = jref["table"][ji++];
+      jrefi["error_number"] = data->ata_error_count + k - 4;
+      jrefi["lifetime_hours"] = summary->timestamp;
+
+      jout("  When the command that caused the error occurred, the device was %s.\n\n", msgstate);
+      jout("  After command completion occurred, registers were:\n"
            "  ER ST SC SN CL CH DH\n"
            "  -- -- -- -- -- -- --\n"
            "  %02x %02x %02x %02x %02x %02x %02x",
@@ -1479,27 +2209,36 @@ static int PrintSmartErrorlog(const ata_smart_errorlog *data,
            (int)summary->cylinder_low,
            (int)summary->cylinder_high,
            (int)summary->drive_head);
+
+      {
+        json::ref jrefir = jrefi["completion_registers"];
+        jrefir["error"] = summary->error_register;
+        jrefir["status"] = summary->status;
+        jrefir["count"] = summary->sector_count;
+        jrefir["lba"] = (summary->sector_number      )
+                      | (summary->cylinder_low  <<  8)
+                      | (summary->cylinder_high << 16);
+        jrefir["device"] = summary->drive_head;
+      }
+
       // Add a description of the contents of the status and error registers
       // if possible
-      char descbuf[256];
-      const char * st_er_desc = construct_st_er_desc(descbuf, elog);
-      if (st_er_desc)
-        pout("  %s", st_er_desc);
-      pout("\n\n");
-      pout("  Commands leading to the command that caused the error were:\n"
+      std::string st_er_desc = format_st_er_desc(elog);
+      if (!st_er_desc.empty()) {
+        jout("  %s", st_er_desc.c_str());
+        jrefi["error_description"] = st_er_desc;
+      }
+      jout("\n\n");
+      jout("  Commands leading to the command that caused the error were:\n"
            "  CR FR SC SN CL CH DH DC   Powered_Up_Time  Command/Feature_Name\n"
            "  -- -- -- -- -- -- -- --  ----------------  --------------------\n");
-      for ( j = 4; j >= 0; j--){
+      for (int j = 4, jj = 0; j >= 0; j--) {
         const ata_smart_errorlog_command_struct * thiscommand = elog->commands+j;
 
         // Spec says: unused data command structures shall be zero filled
         if (nonempty(thiscommand, sizeof(*thiscommand))) {
-         char timestring[32];
-         
-         // Convert integer milliseconds to a text-format string
-         MsecToText(thiscommand->timestamp, timestring);
-         
-          pout("  %02x %02x %02x %02x %02x %02x %02x %02x  %16s  %s\n",
+          const char * atacmd = look_up_ata_command(thiscommand->commandreg, thiscommand->featuresreg);
+          jout("  %02x %02x %02x %02x %02x %02x %02x %02x  %16s  %s\n",
                (int)thiscommand->commandreg,
                (int)thiscommand->featuresreg,
                (int)thiscommand->sector_count,
@@ -1508,11 +2247,24 @@ static int PrintSmartErrorlog(const ata_smart_errorlog *data,
                (int)thiscommand->cylinder_high,
                (int)thiscommand->drive_head,
                (int)thiscommand->devicecontrolreg,
-              timestring,
-               look_up_ata_command(thiscommand->commandreg, thiscommand->featuresreg));
-       }
+               format_milliseconds(thiscommand->timestamp).c_str(),
+               atacmd);
+
+          json::ref jrefic = jrefi["previous_commands"][jj++];
+          json::ref jreficr = jrefic["registers"];
+          jreficr["command"] = thiscommand->commandreg;
+          jreficr["features"] = thiscommand->featuresreg,
+          jreficr["count"] = thiscommand->sector_count;
+          jreficr["lba"] = (thiscommand->sector_number      )
+                         | (thiscommand->cylinder_low   << 8)
+                         | (thiscommand->cylinder_high << 16);
+          jreficr["device"] = thiscommand->drive_head;
+          jreficr["device_control"] = thiscommand->devicecontrolreg;
+          jrefic["powerup_milliseconds"] = thiscommand->timestamp;
+          jrefic["command_name"] = atacmd;
+        }
       }
-      pout("\n");
+      jout("\n");
     }
   }
   print_on();
@@ -1523,14 +2275,20 @@ static int PrintSmartErrorlog(const ata_smart_errorlog *data,
 }
 
 // Print SMART Extended Comprehensive Error Log (GP Log 0x03)
-static int PrintSmartExtErrorLog(const ata_smart_exterrlog * log,
+static int PrintSmartExtErrorLog(ata_device * device,
+                                 const firmwarebug_defs & firmwarebugs,
+                                 const ata_smart_exterrlog * log,
                                  unsigned nsectors, unsigned max_errors)
 {
-  pout("SMART Extended Comprehensive Error Log Version: %u (%u sectors)\n",
+  json::ref jref = jglb["ata_smart_error_log"]["extended"];
+  jout("SMART Extended Comprehensive Error Log Version: %u (%u sectors)\n",
        log->version, nsectors);
+  jref["revision"] = log->version;
+  jref["sectors"] = nsectors;
 
   if (!log->device_error_count) {
-    pout("No Errors Logged\n\n");
+    jout("No Errors Logged\n\n");
+    jref["count"] = 0;
     return 0;
   }
   print_on();
@@ -1558,18 +2316,20 @@ static int PrintSmartExtErrorLog(const ata_smart_exterrlog * log,
   unsigned errcnt = log->device_error_count;
 
   if (errcnt <= nentries)
-    pout("Device Error Count: %u\n", log->device_error_count);
+    jout("Device Error Count: %u\n", log->device_error_count);
   else {
     errcnt = nentries;
-    pout("Device Error Count: %u (device log contains only the most recent %u errors)\n",
+    jout("Device Error Count: %u (device log contains only the most recent %u errors)\n",
          log->device_error_count, errcnt);
   }
+  jref["count"] = log->device_error_count;
+  jref["logged_count"] = errcnt;
 
   if (max_errors < errcnt)
     errcnt = max_errors;
 
   print_off();
-  pout("\tCR     = Command Register\n"
+  jout("\tCR     = Command Register\n"
        "\tFEATR  = Features Register\n"
        "\tCOUNT  = Count (was: Sector Count) Register\n"
        "\tLBA_48 = Upper bytes of LBA High/Mid/Low Registers ]  ATA-8\n"
@@ -1584,30 +2344,56 @@ static int PrintSmartExtErrorLog(const ata_smart_exterrlog * log,
        "DDd+hh:mm:SS.sss where DD=days, hh=hours, mm=minutes,\n"
        "SS=sec, and sss=millisec. It \"wraps\" after 49.710 days.\n\n");
 
+  // Recently read log page
+  ata_smart_exterrlog log_buf;
+  unsigned log_buf_page = ~0;
+
   // Iterate through circular buffer in reverse direction
   for (unsigned i = 0, errnum = log->device_error_count;
        i < errcnt; i++, errnum--, erridx = (erridx > 0 ? erridx - 1 : nentries - 1)) {
 
-    const ata_smart_exterrlog_error_log & entry = log[erridx / 4].error_logs[erridx % 4];
+    // Read log page if needed
+    const ata_smart_exterrlog * log_p;
+    unsigned page = erridx / 4;
+    if (page == 0)
+      log_p = log;
+    else {
+      if (page != log_buf_page) {
+        memset(&log_buf, 0, sizeof(log_buf));
+        if (!ataReadExtErrorLog(device, &log_buf, page, 1, firmwarebugs))
+          break;
+        log_buf_page = page;
+      }
+      log_p = &log_buf;
+    }
+
+    const ata_smart_exterrlog_error_log & entry = log_p->error_logs[erridx % 4];
+
+    json::ref jrefi = jref["table"][i];
+    jrefi["error_number"] = errnum;
+    jrefi["log_index"] = erridx;
 
     // Skip unused entries
     if (!nonempty(&entry, sizeof(entry))) {
-      pout("Error %u [%u] log entry is empty\n", errnum, erridx);
+      jout("Error %u [%u] log entry is empty\n", errnum, erridx);
       continue;
     }
 
     // Print error information
     print_on();
     const ata_smart_exterrlog_error & err = entry.error;
-    pout("Error %u [%u] occurred at disk power-on lifetime: %u hours (%u days + %u hours)\n",
+    jout("Error %u [%u] occurred at disk power-on lifetime: %u hours (%u days + %u hours)\n",
          errnum, erridx, err.timestamp, err.timestamp / 24, err.timestamp % 24);
     print_off();
+    jrefi["lifetime_hours"] = err.timestamp;
 
-    pout("  When the command that caused the error occurred, the device was %s.\n\n",
-      get_error_log_state_desc(err.state));
+    const char * msgstate = get_error_log_state_desc(err.state);
+    jout("  When the command that caused the error occurred, the device was %s.\n\n", msgstate);
+    jrefi["device_state"]["value"] = err.state;
+    jrefi["device_state"]["string"] = msgstate;
 
     // Print registers
-    pout("  After command completion occurred, registers were:\n"
+    jout("  After command completion occurred, registers were:\n"
          "  ER -- ST COUNT  LBA_48  LH LM LL DV DC\n"
          "  -- -- -- == -- == == == -- -- -- -- --\n"
          "  %02x -- %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
@@ -1624,19 +2410,35 @@ static int PrintSmartExtErrorLog(const ata_smart_exterrlog * log,
          err.device_register,
          err.device_control_register);
 
+    {
+      json::ref jrefir = jrefi["completion_registers"];
+      jrefir["error"] = err.error_register;
+      jrefir["status"] = err.status_register,
+      jrefir["count"] = (err.count_register_hi << 8) | err.count_register;
+      jrefir["lba"] = ((uint64_t)err.lba_high_register_hi << 40)
+                    | ((uint64_t)err.lba_mid_register_hi  << 32)
+                    | ((uint64_t)err.lba_low_register_hi  << 24)
+                    | ((unsigned)err.lba_high_register    << 16)
+                    | ((unsigned)err.lba_mid_register     <<  8)
+                    | ((unsigned)err.lba_low_register          );
+      jrefir["device"] = err.device_register;
+      jrefir["device_control"] = err.device_control_register;
+    }
+
     // Add a description of the contents of the status and error registers
     // if possible
-    char descbuf[256];
-    const char * st_er_desc = construct_st_er_desc(descbuf, &entry);
-    if (st_er_desc)
-      pout("  %s", st_er_desc);
-    pout("\n\n");
+    std::string st_er_desc = format_st_er_desc(&entry);
+    if (!st_er_desc.empty()) {
+      jout("  %s", st_er_desc.c_str());
+      jrefi["error_description"] = st_er_desc;
+    }
+    jout("\n\n");
 
     // Print command history
-    pout("  Commands leading to the command that caused the error were:\n"
+    jout("  Commands leading to the command that caused the error were:\n"
          "  CR FEATR COUNT  LBA_48  LH LM LL DV DC  Powered_Up_Time  Command/Feature_Name\n"
          "  -- == -- == -- == == == -- -- -- -- --  ---------------  --------------------\n");
-    for (int ci = 4; ci >= 0; ci--) {
+    for (int ci = 4, cji = 0; ci >= 0; ci--) {
       const ata_smart_exterrlog_command & cmd = entry.commands[ci];
 
       // Skip unused entries
@@ -1644,10 +2446,8 @@ static int PrintSmartExtErrorLog(const ata_smart_exterrlog * log,
         continue;
 
       // Print registers, timestamp and ATA command name
-      char timestring[32];
-      MsecToText(cmd.timestamp, timestring);
-
-      pout("  %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %16s  %s\n",
+      const char * atacmd = look_up_ata_command(cmd.command_register, cmd.features_register);
+      jout("  %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %16s  %s\n",
            cmd.command_register,
            cmd.features_register_hi,
            cmd.features_register,
@@ -1661,10 +2461,26 @@ static int PrintSmartExtErrorLog(const ata_smart_exterrlog * log,
            cmd.lba_low_register,
            cmd.device_register,
            cmd.device_control_register,
-           timestring,
-           look_up_ata_command(cmd.command_register, cmd.features_register));
+           format_milliseconds(cmd.timestamp).c_str(),
+           atacmd);
+
+      json::ref jrefic = jrefi["previous_commands"][cji++];
+      json::ref jreficr = jrefic["registers"];
+      jreficr["command"] = cmd.command_register;
+      jreficr["features"] = (cmd.features_register_hi << 8) | cmd.features_register;
+      jreficr["count"] = (cmd.count_register_hi << 8) | cmd.count_register;
+      jreficr["lba"] = ((uint64_t)cmd.lba_high_register_hi << 40)
+                     | ((uint64_t)cmd.lba_mid_register_hi  << 32)
+                     | ((uint64_t)cmd.lba_low_register_hi  << 24)
+                     | ((unsigned)cmd.lba_high_register    << 16)
+                     | ((unsigned)cmd.lba_mid_register     <<  8)
+                     | ((unsigned)cmd.lba_low_register          );
+      jreficr["device"] = cmd.device_register;
+      jreficr["device_control"] = cmd.device_control_register;
+      jrefic["powerup_milliseconds"] = cmd.timestamp;
+      jrefic["command_name"] = atacmd;
     }
-    pout("\n");
+    jout("\n");
   }
 
   print_on();
@@ -1674,15 +2490,187 @@ static int PrintSmartExtErrorLog(const ata_smart_exterrlog * log,
   return log->device_error_count;
 }
 
+// Print one self-test log entry.
+// Returns:
+// -1: self-test failed
+//  1: extended self-test completed without error
+//  0: otherwise
+static int ataPrintSmartSelfTestEntry(const json::ref & jref,
+                                      unsigned testnum, unsigned char test_type,
+                                      unsigned char test_status,
+                                      unsigned short timestamp,
+                                      uint64_t failing_lba,
+                                      bool print_error_only, bool & print_header)
+{
+  // Check status and type for return value
+  int retval = 0;
+  switch (test_status >> 4) {
+    case 0x0:
+      if ((test_type & 0x7f) == 0x02)
+        retval = 1; // extended self-test completed without error
+      break;
+    case 0x3: case 0x4:
+    case 0x5: case 0x6:
+    case 0x7: case 0x8:
+      retval = -1; // self-test failed
+      break;
+  }
+
+  if (retval >= 0 && print_error_only)
+    return retval;
+
+  std::string msgtest;
+  switch (test_type) {
+    case 0x00: msgtest = "Offline";            break;
+    case 0x01: msgtest = "Short offline";      break;
+    case 0x02: msgtest = "Extended offline";   break;
+    case 0x03: msgtest = "Conveyance offline"; break;
+    case 0x04: msgtest = "Selective offline";  break;
+    case 0x7f: msgtest = "Abort offline test"; break;
+    case 0x81: msgtest = "Short captive";      break;
+    case 0x82: msgtest = "Extended captive";   break;
+    case 0x83: msgtest = "Conveyance captive"; break;
+    case 0x84: msgtest = "Selective captive";  break;
+    default:
+      if ((0x40 <= test_type && test_type <= 0x7e) || 0x90 <= test_type)
+        msgtest = strprintf("Vendor (0x%02x)", test_type);
+      else
+        msgtest = strprintf("Reserved (0x%02x)", test_type);
+  }
+
+  std::string msgstat;
+  switch (test_status >> 4) {
+    case 0x0: msgstat = "Completed without error";       break;
+    case 0x1: msgstat = "Aborted by host";               break;
+    case 0x2: msgstat = "Interrupted (host reset)";      break;
+    case 0x3: msgstat = "Fatal or unknown error";        break;
+    case 0x4: msgstat = "Completed: unknown failure";    break;
+    case 0x5: msgstat = "Completed: electrical failure"; break;
+    case 0x6: msgstat = "Completed: servo/seek failure"; break;
+    case 0x7: msgstat = "Completed: read failure";       break;
+    case 0x8: msgstat = "Completed: handling damage??";  break;
+    case 0xf: msgstat = "Self-test routine in progress"; break;
+    default:  msgstat = strprintf("Unknown status (0x%x)", test_status >> 4);
+  }
+
+  // Print header once
+  if (print_header) {
+    print_header = false;
+    jout("Num  Test_Description    Status                  Remaining  LifeTime(hours)  LBA_of_first_error\n");
+  }
+
+  char msglba[32];
+  if (retval < 0 && failing_lba < 0xffffffffffffULL)
+    snprintf(msglba, sizeof(msglba), "%" PRIu64, failing_lba);
+  else {
+    msglba[0] = '-'; msglba[1] = 0;
+  }
+
+  jout("#%2u  %-19s %-29s %1d0%%  %8u         %s\n", testnum,
+       msgtest.c_str(), msgstat.c_str(), test_status & 0x0f, timestamp, msglba);
+
+  jref["type"]["value"] = test_type;
+  jref["type"]["string"] = msgtest;
+
+  jref["status"]["value"] = test_status;
+  jref["status"]["string"] = msgstat;
+  if (test_status & 0x0f)
+    jref["status"]["remaining_percent"] = (test_status & 0x0f) * 10;
+  switch (test_status >> 4) {
+    case 0x1: case 0x2: case 0x3: break; // aborted -> unknown
+    default: jref["status"]["passed"] = (retval >= 0);
+  }
+
+  jref["lifetime_hours"] = timestamp;
+
+  if (retval < 0 && failing_lba < 0xffffffffffffULL)
+    jref["lba"] = failing_lba;
+
+  return retval;
+}
+
+// Print SMART Self-test log, return error count
+static int ataPrintSmartSelfTestlog(const ata_smart_selftestlog * log, bool allentries,
+                                    firmwarebug_defs firmwarebugs)
+{
+  json::ref jref = jglb["ata_smart_self_test_log"]["standard"];
+
+  if (allentries)
+    jout("SMART Self-test log structure revision number %d\n", log->revnumber);
+  jref["revision"] = log->revnumber;
+  if (log->revnumber != 0x0001 && allentries && !firmwarebugs.is_set(BUG_SAMSUNG))
+    pout("Warning: ATA Specification requires self-test log structure revision number = 1\n");
+  if (!log->mostrecenttest){
+    if (allentries)
+      jout("No self-tests have been logged.  [To run self-tests, use: smartctl -t]\n");
+    jref["count"] = 0;
+    return 0;
+  }
+
+  bool noheaderprinted = true;
+  int errcnt = 0, igncnt = 0;
+  int testnum = 1, ext_ok_testnum = -1;
+
+  // Iterate through circular buffer in reverse direction
+  for (int i = 20, ji = 0; i >= 0; i--) {
+    int j = (i + log->mostrecenttest) % 21;
+    const ata_smart_selftestlog_struct & entry = log->selftest_struct[j];
+
+    // Skip unused entries
+    if (!nonempty(&entry, sizeof(entry)))
+      continue;
+
+    // Get LBA if valid
+    uint64_t lba48 = (entry.lbafirstfailure < 0xffffffff ?
+                      entry.lbafirstfailure : 0xffffffffffffULL);
+
+    // Print entry
+    int state = ataPrintSmartSelfTestEntry(jref["table"][ji++],
+      testnum, entry.selftestnumber, entry.selfteststatus,
+      entry.timestamp, lba48, !allentries, noheaderprinted);
+
+    if (state < 0) {
+      // Self-test showed an error
+      if (ext_ok_testnum < 0)
+        errcnt++;
+      else
+        // Newer successful extended self-test exits
+        igncnt++;
+    }
+    else if (state > 0 && ext_ok_testnum < 0) {
+      // Latest successful extended self-test
+      ext_ok_testnum = testnum;
+    }
+    testnum++;
+  }
+
+  if (igncnt)
+    jout("%d of %d failed self-tests are outdated by newer successful extended offline self-test #%2d\n",
+      igncnt, igncnt+errcnt, ext_ok_testnum);
+  jref["count"] = testnum - 1;
+  jref["error_count_total"] = igncnt + errcnt;
+  jref["error_count_outdated"] = igncnt;
+
+  if (!allentries && !noheaderprinted)
+    jout("\n");
+
+  return errcnt;
+}
+
 // Print SMART Extended Self-test Log (GP Log 0x07)
 static int PrintSmartExtSelfTestLog(const ata_smart_extselftestlog * log,
                                     unsigned nsectors, unsigned max_entries)
 {
-  pout("SMART Extended Self-test Log Version: %u (%u sectors)\n",
+  json::ref jref = jglb["ata_smart_self_test_log"]["extended"];
+
+  jout("SMART Extended Self-test Log Version: %u (%u sectors)\n",
        log->version, nsectors);
+  jref["revision"] = log->version;
+  jref["sectors"] = nsectors;
 
   if (!log->log_desc_index){
-    pout("No self-tests have been logged.  [To run self-tests, use: smartctl -t]\n\n");
+    jout("No self-tests have been logged.  [To run self-tests, use: smartctl -t]\n\n");
+    jref["count"] = 0;
     return 0;
   }
 
@@ -1701,10 +2689,10 @@ static int PrintSmartExtSelfTestLog(const ata_smart_extselftestlog * log,
   bool print_header = true;
   int errcnt = 0, igncnt = 0;
   int ext_ok_testnum = -1;
+  unsigned testnum = 1;
 
   // Iterate through circular buffer in reverse direction
-  for (unsigned i = 0, testnum = 1;
-       i < nentries && testnum <= max_entries;
+  for (unsigned i = 0, ji = 0; i < nentries && testnum <= max_entries;
        i++, logidx = (logidx > 0 ? logidx - 1 : nentries - 1)) {
 
     const ata_smart_extselftestlog_desc & entry = log[logidx / 19].log_descs[logidx % 19];
@@ -1723,7 +2711,8 @@ static int PrintSmartExtSelfTestLog(const ata_smart_extselftestlog * log,
         | ((uint64_t)b[5] << 40);
 
     // Print entry
-    int state = ataPrintSmartSelfTestEntry(testnum, entry.self_test_type,
+    int state = ataPrintSmartSelfTestEntry(jref["table"][ji++],
+      testnum, entry.self_test_type,
       entry.self_test_status, entry.timestamp, lba48,
       false /*!print_error_only*/, print_header);
 
@@ -1743,27 +2732,27 @@ static int PrintSmartExtSelfTestLog(const ata_smart_extselftestlog * log,
   }
 
   if (igncnt)
-    pout("%d of %d failed self-tests are outdated by newer successful extended offline self-test #%2d\n",
+    jout("%d of %d failed self-tests are outdated by newer successful extended offline self-test #%2d\n",
       igncnt, igncnt+errcnt, ext_ok_testnum);
+  jref["count"] = testnum - 1;
+  jref["error_count_total"] = igncnt + errcnt;
+  jref["error_count_outdated"] = igncnt;
 
-  pout("\n");
+  jout("\n");
   return errcnt;
 }
 
 static void ataPrintSelectiveSelfTestLog(const ata_selective_self_test_log * log, const ata_smart_values * sv)
 {
-  int i,field1,field2;
-  const char *msg;
-  char tmp[64];
-  uint64_t maxl=0,maxr=0;
-  uint64_t current=log->currentlba;
-  uint64_t currentend=current+65535;
+  json::ref jref = jglb["ata_smart_selective_self_test_log"];
 
   // print data structure revision number
-  pout("SMART Selective self-test log data structure revision number %d\n",(int)log->logversion);
+  jout("SMART Selective self-test log data structure revision number %d\n", log->logversion);
+  jref["revision"] = log->logversion;
   if (1 != log->logversion)
     pout("Note: revision number not 1 implies that no selective self-test has ever been run\n");
   
+  const char *msg;
   switch((sv->self_test_exec_status)>>4){
   case  0:msg="Completed";
     break;
@@ -1791,11 +2780,14 @@ static void ataPrintSelectiveSelfTestLog(const ata_selective_self_test_log * log
 
   // find the number of columns needed for printing. If in use, the
   // start/end of span being read-scanned...
+  uint64_t maxl = 0, maxr = 0;
+  uint64_t current = log->currentlba;
+  uint64_t currentend = current + 0xffff;
   if (log->currentspan>5) {
     maxl=current;
     maxr=currentend;
   }
-  for (i=0; i<5; i++) {
+  for (int i = 0; i < 5; i++) {
     uint64_t start=log->span[i].start;
     uint64_t end  =log->span[i].end; 
     // ... plus max start/end of each of the five test spans.
@@ -1805,37 +2797,57 @@ static void ataPrintSelectiveSelfTestLog(const ata_selective_self_test_log * log
       maxr=end;
   }
   
-  // we need at least 7 characters wide fields to accomodate the
+  // we need at least 7 characters wide fields to accommodate the
   // labels
-  if ((field1=snprintf(tmp,64, "%"PRIu64, maxl))<7)
+  int field1,field2;
+  char tmp[64];
+  if ((field1=snprintf(tmp,64, "%" PRIu64, maxl))<7)
     field1=7;
-  if ((field2=snprintf(tmp,64, "%"PRIu64, maxr))<7)
+  if ((field2=snprintf(tmp,64, "%" PRIu64, maxr))<7)
     field2=7;
 
   // now print the five test spans
-  pout(" SPAN  %*s  %*s  CURRENT_TEST_STATUS\n", field1, "MIN_LBA", field2, "MAX_LBA");
+  jout(" SPAN  %*s  %*s  CURRENT_TEST_STATUS\n", field1, "MIN_LBA", field2, "MAX_LBA");
 
-  for (i=0; i<5; i++) {
+  for (int i = 0; i < 5; i++) {
     uint64_t start=log->span[i].start;
     uint64_t end=log->span[i].end;
-    
-    if ((i+1)==(int)log->currentspan)
+    bool active = (i + 1 == log->currentspan);
+
+    if (active)
       // this span is currently under test
-      pout("    %d  %*"PRIu64"  %*"PRIu64"  %s [%01d0%% left] (%"PRIu64"-%"PRIu64")\n",
-          i+1, field1, start, field2, end, msg,
-          (int)(sv->self_test_exec_status & 0xf), current, currentend);
+      jout("    %d  %*" PRIu64 "  %*" PRIu64 "  %s [%01d0%% left] (%" PRIu64 "-%" PRIu64 ")\n",
+           i + 1, field1, start, field2, end, msg,
+           (sv->self_test_exec_status & 0xf), current, currentend);
     else
       // this span is not currently under test
-      pout("    %d  %*"PRIu64"  %*"PRIu64"  Not_testing\n",
-          i+1, field1, start, field2, end);
-  }  
-  
+      jout("    %d  %*" PRIu64 "  %*" PRIu64 "  Not_testing\n",
+           i + 1, field1, start, field2, end);
+
+    json::ref jrefi = jref["table"][i];
+    jrefi["lba_min"] = start;
+    jrefi["lba_max"] = end;
+    jrefi["status"]["value"] = sv->self_test_exec_status;
+    jrefi["status"]["string"] = (active ? msg : "Not_testing");
+    if (active) {
+      jrefi["status"]["remaining_percent"] = sv->self_test_exec_status & 0xf;
+      jrefi["current_lba_min"] = current;
+      jrefi["current_lba_max"] = currentend;
+    }
+  }
+
   // if we are currently read-scanning, print LBAs and the status of
   // the read scan
-  if (log->currentspan>5)
-    pout("%5d  %*"PRIu64"  %*"PRIu64"  Read_scanning %s\n",
-        (int)log->currentspan, field1, current, field2, currentend,
-        OfflineDataCollectionStatus(sv->offline_data_collection_status));
+  if (log->currentspan > 5) {
+    const char * ost = OfflineDataCollectionStatus(sv->offline_data_collection_status);
+    jout("%5d  %*" PRIu64 "  %*" PRIu64 "  Read_scanning %s\n",
+         log->currentspan, field1, current, field2, currentend, ost);
+    json::ref jrefc = jref["current_read_scan"];
+    jrefc["lba_min"] = current;
+    jrefc["lba_max"] = currentend;
+    jrefc["status"]["value"] = sv->offline_data_collection_status;
+    jrefc["status"]["string"] = ost;
+  }
   
   /* Print selective self-test flags.  Possible flag combinations are
      (numbering bits from 0-15):
@@ -1848,37 +2860,48 @@ static void ataPrintSelectiveSelfTestLog(const ata_selective_self_test_log * log
      1     1       1       Currently scanning
   */
   
-  pout("Selective self-test flags (0x%x):\n", (unsigned int)log->flags);
+  jout("Selective self-test flags (0x%x):\n", (unsigned)log->flags);
+  json::ref jreff = jref["flags"];
+  jreff["value"] = log->flags;
+  jreff["remainder_scan_enabled"] = !!(log->flags & SELECTIVE_FLAG_DOSCAN);
   if (log->flags & SELECTIVE_FLAG_DOSCAN) {
-    if (log->flags & SELECTIVE_FLAG_ACTIVE)
-      pout("  Currently read-scanning the remainder of the disk.\n");
-    else if (log->flags & SELECTIVE_FLAG_PENDING)
-      pout("  Read-scan of remainder of disk interrupted; will resume %d min after power-up.\n",
-          (int)log->pendingtime);
-    else
-      pout("  After scanning selected spans, read-scan remainder of disk.\n");
+   if (log->flags & SELECTIVE_FLAG_ACTIVE)
+     jout("  Currently read-scanning the remainder of the disk.\n");
+   else if (log->flags & SELECTIVE_FLAG_PENDING)
+     jout("  Read-scan of remainder of disk interrupted; will resume %d min after power-up.\n",
+          log->pendingtime);
+   else
+     jout("  After scanning selected spans, read-scan remainder of disk.\n");
+   jreff["remainder_scan_active"] = !!(log->flags & SELECTIVE_FLAG_ACTIVE);
+   jreff["power_up_scan_pending"] = !!(log->flags & SELECTIVE_FLAG_PENDING);
   }
   else
-    pout("  After scanning selected spans, do NOT read-scan remainder of disk.\n");
+    jout("  After scanning selected spans, do NOT read-scan remainder of disk.\n");
   
   // print pending time
-  pout("If Selective self-test is pending on power-up, resume after %d minute delay.\n",
-       (int)log->pendingtime);
-
-  return; 
+  jout("If Selective self-test is pending on power-up, resume after %d minute delay.\n",
+       log->pendingtime);
+  jref["power_up_scan_resume_minutes"] = log->pendingtime;
 }
 
 // Format SCT Temperature value
-static const char * sct_ptemp(signed char x, char * buf)
+static const char * sct_ptemp(signed char x, char (& buf)[20])
 {
   if (x == -128 /*0x80 = unknown*/)
-    strcpy(buf, " ?");
-  else
-    sprintf(buf, "%2d", x);
+    return " ?";
+  snprintf(buf, sizeof(buf), "%2d", x);
   return buf;
 }
 
-static const char * sct_pbar(int x, char * buf)
+static void sct_jtemp2(const json::ref & jref, const char * name, signed char x)
+{
+  if (x == -128 /*0x80 = unknown*/)
+    return;
+  jglb["temperature"][name] = x;
+  jref["temperature"][name] = x;
+}
+
+static const char * sct_pbar(int x, char (& buf)[64])
 {
   if (x <= 19)
     x = 0;
@@ -1916,40 +2939,84 @@ static const char * sct_device_state_msg(unsigned char state)
 // Print SCT Status
 static int ataPrintSCTStatus(const ata_sct_status_response * sts)
 {
-  pout("SCT Status Version:                  %u\n", sts->format_version);
-  pout("SCT Version (vendor specific):       %u (0x%04x)\n", sts->sct_version, sts->sct_version);
-  pout("SCT Support Level:                   %u\n", sts->sct_spec);
-  pout("Device State:                        %s (%u)\n",
-    sct_device_state_msg(sts->device_state), sts->device_state);
+  json::ref jref = jglb["ata_sct_status"];
+
+  jout("SCT Status Version:                  %u\n", sts->format_version);
+  jref["format_version"] = sts->format_version;
+  jout("SCT Version (vendor specific):       %u (0x%04x)\n", sts->sct_version, sts->sct_version);
+  jref["sct_version"] = sts->sct_version;
+  // SCT Support Level (1) from original SCT draft was later declared obsolete in ATA-8 ACS.
+  // Drives typically return 0 or 1.  Print only if unknown value is returned.
+  if (sts->sct_spec > 1)
+    pout("SCT Support Level:                   %u\n", sts->sct_spec);
+  const char * statestr = sct_device_state_msg(sts->device_state);
+  jout("Device State:                        %s (%u)\n", statestr, sts->device_state);
+  jref["device_state"]["value"] = sts->device_state;
+  jref["device_state"]["string"] = statestr;
+
+  // If "Reserved" fields not set, assume "old" format version 2:
+  // Table 11 of T13/1701DT-N (SMART Command Transport) Revision 5, February 2005
+  // Table 54 of T13/1699-D (ATA8-ACS) Revision 3e, July 2006
+  // ... else assume "new" format version 2 or version 3:
+  // T13/e06152r0-3 (Additional SCT Temperature Statistics), August - October 2006
+  // Table 60 of T13/1699-D (ATA8-ACS) Revision 3f, December 2006  (format version 2)
+  // Table 80 of T13/1699-D (ATA8-ACS) Revision 6a, September 2008 (format version 3)
+  // Table 194 of T13/BSR INCITS 529 (ACS-4) Revision 20, October 26, 2017
+  // (max_op_limit, smart_status, min_erc_time)
+  bool old_format_2 = (   !sts->min_temp && !sts->life_min_temp
+                       && !sts->under_limit_count && !sts->over_limit_count);
+
   char buf1[20], buf2[20];
-  if (   !sts->min_temp && !sts->life_min_temp
-      && !sts->under_limit_count && !sts->over_limit_count) {
-    // "Reserved" fields not set, assume "old" format version 2
-    // Table 11 of T13/1701DT-N (SMART Command Transport) Revision 5, February 2005
-    // Table 54 of T13/1699-D (ATA8-ACS) Revision 3e, July 2006
-    pout("Current Temperature:                 %s Celsius\n",
-      sct_ptemp(sts->hda_temp, buf1));
-    pout("Power Cycle Max Temperature:         %s Celsius\n",
-      sct_ptemp(sts->max_temp, buf2));
-    pout("Lifetime    Max Temperature:         %s Celsius\n",
-      sct_ptemp(sts->life_max_temp, buf2));
+  jout("Current Temperature:                    %s Celsius\n",
+  sct_ptemp(sts->hda_temp, buf1));
+  sct_jtemp2(jref, "current", sts->hda_temp);
+  jout("Power Cycle Min/Max Temperature:     %s/%s Celsius\n",
+    (!old_format_2 ? sct_ptemp(sts->min_temp, buf1) : "--"),
+    sct_ptemp(sts->max_temp, buf2));
+  if (!old_format_2)
+    sct_jtemp2(jref, "power_cycle_min", sts->min_temp);
+  sct_jtemp2(jref, "power_cycle_max", sts->max_temp);
+  jout("Lifetime    Min/Max Temperature:     %s/%s Celsius\n",
+    (!old_format_2 ? sct_ptemp(sts->life_min_temp, buf1) : "--"),
+    sct_ptemp(sts->life_max_temp, buf2));
+  if (!old_format_2)
+    sct_jtemp2(jref, "lifetime_min", sts->life_min_temp);
+  sct_jtemp2(jref, "lifetime_max", sts->life_max_temp);
+  if (old_format_2)
+    return 0;
+
+  if (sts->max_op_limit > 0) { // e06152r0-2: "Average Temperature"
+    jout("Specified Max Operating Temperature:   %3d Celsius\n", sts->max_op_limit);
+    sct_jtemp2(jref, "op_limit_max", sts->max_op_limit);
   }
-  else {
-    // Assume "new" format version 2 or version 3
-    // T13/e06152r0-3 (Additional SCT Temperature Statistics), August - October 2006
-    // Table 60 of T13/1699-D (ATA8-ACS) Revision 3f, December 2006  (format version 2)
-    // Table 80 of T13/1699-D (ATA8-ACS) Revision 6a, September 2008 (format version 3)
-    pout("Current Temperature:                    %s Celsius\n",
-      sct_ptemp(sts->hda_temp, buf1));
-    pout("Power Cycle Min/Max Temperature:     %s/%s Celsius\n",
-      sct_ptemp(sts->min_temp, buf1), sct_ptemp(sts->max_temp, buf2));
-    pout("Lifetime    Min/Max Temperature:     %s/%s Celsius\n",
-      sct_ptemp(sts->life_min_temp, buf1), sct_ptemp(sts->life_max_temp, buf2));
-    signed char avg = sts->byte205; // Average Temperature from e06152r0-2, removed in e06152r3
-    if (0 < avg && sts->life_min_temp <= avg && avg <= sts->life_max_temp)
-      pout("Lifetime    Average Temperature:        %2d Celsius\n", avg);
-    pout("Under/Over Temperature Limit Count:  %2u/%u\n",
-      sts->under_limit_count, sts->over_limit_count);
+  jout("Under/Over Temperature Limit Count:  %2u/%u\n",
+    sts->under_limit_count, sts->over_limit_count);
+  jref["temperature"]["under_limit_count"] = sts->under_limit_count;
+  jref["temperature"]["over_limit_count"] = sts->over_limit_count;
+
+  if (sts->smart_status) { // ACS-4
+    int passed = (sts->smart_status == 0x2cf4 ? 0 :
+                  sts->smart_status == 0xc24f ? 1 : -1);
+    jout("SMART Status:                        0x%04x (%s)\n", sts->smart_status,
+          (passed == 0 ? "FAILED" : passed > 0 ? "PASSED" : "Reserved"));
+    if (passed >= 0) {
+      jref["smart_status"]["passed"] = !!passed;
+      jglb["smart_status"]["passed"] = !!passed;
+    }
+    else
+      jref["smart_status"]["reserved_value"] = sts->smart_status;
+  }
+
+  if (sts->min_erc_time) // ACS-4
+    pout("Minimum supported ERC Time Limit:    %d (%0.1f seconds)\n",
+          sts->min_erc_time, sts->min_erc_time/10.0);
+
+  if (nonempty(sts->vendor_specific, sizeof(sts->vendor_specific))) {
+    jout("Vendor specific:\n");
+    for (unsigned i = 0; i < sizeof(sts->vendor_specific); i++) {
+      jout("%02x%c", sts->vendor_specific[i], ((i & 0xf) != 0xf ? ' ' : '\n'));
+      jref["vendor_specific"][i] = sts->vendor_specific[i];
+    }
   }
   return 0;
 }
@@ -1957,24 +3024,41 @@ static int ataPrintSCTStatus(const ata_sct_status_response * sts)
 // Print SCT Temperature History Table
 static int ataPrintSCTTempHist(const ata_sct_temperature_history_table * tmh)
 {
-  char buf1[20], buf2[80];
-  pout("SCT Temperature History Version:     %u\n", tmh->format_version);
-  pout("Temperature Sampling Period:         %u minute%s\n",
+  json::ref jref = jglb["ata_sct_temperature_history"];
+
+  char buf1[20], buf2[20], buf3[64];
+  jout("SCT Temperature History Version:     %u%s\n", tmh->format_version,
+       (tmh->format_version != 2 ? " (Unknown, should be 2)" : ""));
+  jref["version"] = tmh->format_version;
+  jout("Temperature Sampling Period:         %u minute%s\n",
     tmh->sampling_period, (tmh->sampling_period==1?"":"s"));
-  pout("Temperature Logging Interval:        %u minute%s\n",
+  jref["sampling_period_minutes"] = tmh->sampling_period;
+  jout("Temperature Logging Interval:        %u minute%s\n",
     tmh->interval,        (tmh->interval==1?"":"s"));
-  pout("Min/Max recommended Temperature:     %s/%s Celsius\n",
+  jref["logging_interval_minutes"] = tmh->interval;
+
+  jout("Min/Max recommended Temperature:     %s/%s Celsius\n",
     sct_ptemp(tmh->min_op_limit, buf1), sct_ptemp(tmh->max_op_limit, buf2));
-  pout("Min/Max Temperature Limit:           %s/%s Celsius\n",
+  sct_jtemp2(jref, "op_limit_min", tmh->min_op_limit);
+  sct_jtemp2(jref, "op_limit_max", tmh->max_op_limit);
+  jout("Min/Max Temperature Limit:           %s/%s Celsius\n",
     sct_ptemp(tmh->under_limit, buf1), sct_ptemp(tmh->over_limit, buf2));
-  pout("Temperature History Size (Index):    %u (%u)\n", tmh->cb_size, tmh->cb_index);
+  sct_jtemp2(jref, "limit_min", tmh->under_limit);
+  sct_jtemp2(jref, "limit_max", tmh->over_limit);
+  jout("Temperature History Size (Index):    %u (%u)\n", tmh->cb_size, tmh->cb_index);
+  jref["size"] = tmh->cb_size;
+  jref["index"] = tmh->cb_index;
+
   if (!(0 < tmh->cb_size && tmh->cb_size <= sizeof(tmh->cb) && tmh->cb_index < tmh->cb_size)) {
-    pout("Error invalid Temperature History Size or Index\n");
+    if (!tmh->cb_size)
+      pout("Temperature History is empty\n");
+    else
+      pout("Invalid Temperature History Size or Index\n");
     return 0;
   }
 
   // Print table
-  pout("\nIndex    Estimated Time   Temperature Celsius\n");
+  jout("\nIndex    Estimated Time   Temperature Celsius\n");
   unsigned n = 0, i = (tmh->cb_index+1) % tmh->cb_size;
   unsigned interval = (tmh->interval > 0 ? tmh->interval : 1);
   time_t t = time(0) - (tmh->cb_size-1) * interval * 60;
@@ -1991,13 +3075,15 @@ static int ataPrintSCTTempHist(const ata_sct_temperature_history_table * tmh)
         char date[30];
         // TODO: Don't print times < boot time
         strftime(date, sizeof(date), "%Y-%m-%d %H:%M", localtime(&t));
-        pout(" %3u    %s    %s  %s\n", i, date,
-          sct_ptemp(tmh->cb[i], buf1), sct_pbar(tmh->cb[i], buf2));
+        jout(" %3u    %s    %s  %s\n", i, date,
+          sct_ptemp(tmh->cb[i], buf1), sct_pbar(tmh->cb[i], buf3));
       }
       else if (n == n1+1) {
-        pout(" ...    ..(%3u skipped).    ..  %s\n",
-          n2-n1-2, sct_pbar(tmh->cb[i], buf2));
+        jout(" ...    ..(%3u skipped).    ..  %s\n",
+          n2-n1-2, sct_pbar(tmh->cb[i], buf3));
       }
+      if (tmh->cb[i] != -128)
+        jref["table"][n] = tmh->cb[i];
       t += interval * 60; i = (i+1) % tmh->cb_size; n++;
     }
   }
@@ -2009,15 +3095,24 @@ static int ataPrintSCTTempHist(const ata_sct_temperature_history_table * tmh)
 // Print SCT Error Recovery Control timers
 static void ataPrintSCTErrorRecoveryControl(bool set, unsigned short read_timer, unsigned short write_timer)
 {
-  pout("SCT Error Recovery Control%s:\n", (set ? " set to" : ""));
+  json::ref jref = jglb["ata_sct_erc"];
+  jout("SCT Error Recovery Control%s:\n", (set ? " set to" : ""));
+
+  jref["read"]["enabled"] = !!read_timer;
   if (!read_timer)
-    pout("           Read: Disabled\n");
-  else
-    pout("           Read: %6d (%0.1f seconds)\n", read_timer, read_timer/10.0);
+    jout("           Read: Disabled\n");
+  else {
+    jout("           Read: %6d (%0.1f seconds)\n", read_timer, read_timer/10.0);
+    jref["read"]["deciseconds"] = read_timer;
+  }
+
+  jref["write"]["enabled"] = !!write_timer;
   if (!write_timer)
-    pout("          Write: Disabled\n");
-  else
-    pout("          Write: %6d (%0.1f seconds)\n", write_timer, write_timer/10.0);
+    jout("          Write: Disabled\n");
+  else {
+    jout("          Write: %6d (%0.1f seconds)\n", write_timer, write_timer/10.0);
+    jref["write"]["deciseconds"] = write_timer;
+  }
 }
 
 static void print_aam_level(const char * msg, int level, int recommended = -1)
@@ -2039,9 +3134,16 @@ static void print_aam_level(const char * msg, int level, int recommended = -1)
     s = "reserved";
 
   if (recommended >= 0)
-    pout("%s%d (%s), recommended: %d\n", msg, level, s, recommended);
+    jout("%s%d (%s), recommended: %d\n", msg, level, s, recommended);
   else
-    pout("%s%d (%s)\n", msg, level, s);
+    jout("%s%d (%s)\n", msg, level, s);
+
+  json::ref jref = jglb["ata_aam"];
+  jref["enabled"] = true;
+  jref["level"] = level;
+  jref["string"] = s;
+  if (recommended >= 0)
+    jref["recommended_level"] = recommended;
 }
 
 static void print_apm_level(const char * msg, int level)
@@ -2061,45 +3163,73 @@ static void print_apm_level(const char * msg, int level)
   else
     s = "maximum performance";
 
-  pout("%s%d (%s)\n", msg, level, s);
+  jout("%s%d (%s)\n", msg, level, s);
+
+  json::ref jref = jglb["ata_apm"];
+  jref["enabled"] = true;
+  jref["level"] = level;
+  jref["string"] = s;
+  if (1 <= level && level <= 254) {
+    jref["max_performance"] = (level == 254);
+    jref["min_power"] = (level == 1 || level == 128);
+    jref["with_standby"] = (level < 128);
+  }
 }
 
 static void print_ata_security_status(const char * msg, unsigned short state)
 {
-    const char * s1, * s2 = "", * s3 = "", * s4 = "";
+  // Table 6 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
+  if (!(state & 0x0001)) {
+    pout("%sUnavailable\n", msg);
+    return;
+  }
+
+  const char * s1, * s2 = "", * s3 = "", * s4 = "";
+  bool enabled = false, locked = false;
+  if (!(state & 0x0002)) {
+    s1 = "Disabled, ";
+    if (!(state & 0x0008))
+      s2 = "NOT FROZEN [SEC1]";
+    else
+      s2 = "frozen [SEC2]";
+  }
+  else {
+    enabled = true;
+    s1 = "ENABLED, PW level ";
+    if (!(state & 0x0100))
+      s2 = "HIGH";
+    else
+      s2 = "MAX";
 
-    // Table 6 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
-    if (!(state & 0x0001))
-      s1 = "Unavailable";
-    else if (!(state & 0x0002)) {
-      s1 = "Disabled, ";
+    if (!(state & 0x0004)) {
+      s3 = ", not locked, ";
       if (!(state & 0x0008))
-        s2 = "NOT FROZEN [SEC1]";
+        s4 = "not frozen [SEC5]";
       else
-        s2 = "frozen [SEC2]";
+        s4 = "frozen [SEC6]";
     }
     else {
-      s1 = "ENABLED, PW level ";
-      if (!(state & 0x0020))
-        s2 = "HIGH";
-      else
-        s2 = "MAX";
-
-      if (!(state & 0x0004)) {
-         s3 = ", not locked, ";
-        if (!(state & 0x0008))
-          s4 = "not frozen [SEC5]";
-        else
-          s4 = "frozen [SEC6]";
-      }
-      else {
-        s3 = ", **LOCKED** [SEC4]";
-        if (state & 0x0010)
-          s4 = ", PW ATTEMPTS EXCEEDED";
-      }
+      locked = true;
+      s3 = ", **LOCKED** [SEC4]";
+      if (state & 0x0010)
+        s4 = ", PW ATTEMPTS EXCEEDED";
     }
+  }
 
-    pout("%s%s%s%s%s\n", msg, s1, s2, s3, s4);
+  jout("%s%s%s%s%s\n", msg, s1, s2, s3, s4);
+
+  json::ref jref = jglb["ata_security"];
+  jref["state"] = state;
+  jref["string"] = strprintf("%s%s%s%s", s1, s2, s3, s4);
+  jref["enabled"] = enabled;
+  if (!enabled || !locked)
+    jref["frozen"] = !!(state & 0x0008);
+  if (enabled) {
+    jref["pw_level_max"] = !!(state & 0x0100);
+    jref["locked"] = locked;
+    if (locked)
+      jref["pw_attempts_exceeded"] = !!(state & 0x0010);
+  }
 }
 
 static void print_standby_timer(const char * msg, int timer, const ata_identify_device & drive)
@@ -2144,6 +3274,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   if (options.powermode) {
     unsigned char powerlimit = 0xff;
     int powermode = ataCheckPowerMode(device);
+    // TODO: Move to new function used by smartctl and smartd.
     switch (powermode) {
       case -1:
         if (device->is_syscall_unsup()) {
@@ -2151,20 +3282,36 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
         }
         powername = "SLEEP";   powerlimit = 2;
         break;
-      case 0:
+      // Table 215 of T13/2015-D (ACS-2) Revision 7, June 22, 2011
+      // Table 293 of T13/BSR INCITS 529 (ACS-4) Revision 12, February 18, 2016
+      case 0x00: // PM2:Standby, EPC unavailable or Standby_z power condition
         powername = "STANDBY"; powerlimit = 3; break;
-      case 0x80:
+      case 0x01: // PM2:Standby, Standby_y power condition
+        powername = "STANDBY_Y"; powerlimit = 3; break;
+      case 0x80: // PM1:Idle, EPC unavailable
         powername = "IDLE";    powerlimit = 4; break;
-      case 0xff:
+      case 0x81: // PM1:Idle, Idle_a power condition
+        powername = "IDLE_A";  powerlimit = 4; break;
+      case 0x82: // PM1:Idle, Idle_b power condition
+        powername = "IDLE_B";  powerlimit = 4; break;
+      case 0x83: // PM1:Idle, Idle_c power condition
+        powername = "IDLE_C";  powerlimit = 4; break;
+      // 0x40/41 were declared obsolete in ACS-3 Revision 1
+      case 0x40: // PM0:Active, NV Cache power mode enabled, spun down
+        powername = "ACTIVE_NV_DOWN"; break;
+      case 0x41: // PM0:Active, NV Cache power mode enabled, spun up
+        powername = "ACTIVE_NV_UP"  ; break;
+      case 0xff: // PM0:Active or PM1:Idle
         powername = "ACTIVE or IDLE"; break;
+
       default:
         pout("CHECK POWER MODE returned unknown value 0x%02x, ignoring -n option\n", powermode);
         break;
     }
     if (powername) {
       if (options.powermode >= powerlimit) {
-        pout("Device is in %s mode, exit(%d)\n", powername, FAILPOWER);
-        return FAILPOWER;
+        jinf("Device is in %s mode, exit(%d)\n", powername, options.powerexit);
+        return options.powerexit;
       }
       powerchg = (powermode != 0xff); // SMART tests will spin up drives
     }
@@ -2200,16 +3347,21 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   );
 
   // SMART and GP log directories needed ?
-  bool need_smart_logdir = options.smart_logdir;
+  bool need_smart_logdir = ( 
+          options.smart_logdir
+       || options.devstat_all_pages // devstat fallback to smartlog if needed
+       || options.devstat_ssd_page
+       || !options.devstat_pages.empty()
+    );
 
   bool need_gp_logdir  = (
           options.gp_logdir
        || options.smart_ext_error_log
        || options.smart_ext_selftest_log
-       || options.sataphy
        || options.devstat_all_pages
        || options.devstat_ssd_page
        || !options.devstat_pages.empty()
+       || options.pending_defects_log
   );
 
   unsigned i;
@@ -2227,12 +3379,19 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
        || options.sct_temp_int
        || options.sct_erc_get
        || options.sct_erc_set
+       || options.sct_wcache_reorder_get
+       || options.sct_wcache_reorder_set
+       || options.sct_wcache_sct_get
+       || options.sct_wcache_sct_set
   );
 
   // Exit if no further options specified
-  if (!(   options.drive_info || need_smart_support
-        || need_smart_logdir  || need_gp_logdir
-        || need_sct_support || options.get_set_used)) {
+  if (!(   options.drive_info || options.show_presets
+        || need_smart_support || need_smart_logdir
+        || need_gp_logdir     || need_sct_support
+        || options.sataphy
+        || options.identify_word_level >= 0
+        || options.get_set_used                      )) {
     if (powername)
       pout("Device is in %s mode\n", powername);
     else
@@ -2244,15 +3403,17 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   // Start by getting Drive ID information.  We need this, to know if SMART is supported.
   int returnval = 0;
   ata_identify_device drive; memset(&drive, 0, sizeof(drive));
+  unsigned char raw_drive[sizeof(drive)]; memset(&raw_drive, 0, sizeof(raw_drive));
+
   device->clear_err();
-  int retid = ata_read_identity(device, &drive, options.fix_swapped_id);
+  int retid = ata_read_identity(device, &drive, options.fix_swapped_id, raw_drive);
   if (retid < 0) {
-    pout("Smartctl: Device Read Identity Failed: %s\n\n",
+    pout("Read Device Identity failed: %s\n\n",
          (device->get_errno() ? device->get_errmsg() : "Unknown error"));
     failuretest(MANDATORY_CMD, returnval|=FAILID);
   }
   else if (!nonempty(&drive, sizeof(drive))) {
-    pout("Smartctl: Device Read Identity Failed: empty IDENTIFY data\n\n");
+    pout("Read Device Identity failed: empty IDENTIFY data\n\n");
     failuretest(MANDATORY_CMD, returnval|=FAILID);
   }
 
@@ -2264,20 +3425,28 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
   // Use preset vendor attribute options unless user has requested otherwise.
   ata_vendor_attr_defs attribute_defs = options.attribute_defs;
-  unsigned char fix_firmwarebug = options.fix_firmwarebug;
+  firmwarebug_defs firmwarebugs = options.firmwarebugs;
   const drive_settings * dbentry = 0;
   if (!options.ignore_presets)
     dbentry = lookup_drive_apply_presets(&drive, attribute_defs,
-      fix_firmwarebug);
+      firmwarebugs);
 
-  // Get capacity and sector sizes
+  // Get capacity, sector sizes and rotation rate
   ata_size_info sizes;
   ata_get_size_info(&drive, sizes);
+  int rpm = ata_get_rotation_rate(&drive);
+
+  // Print ATA IDENTIFY info if requested
+  if (options.identify_word_level >= 0) {
+    pout("=== ATA IDENTIFY DATA ===\n");
+    // Pass raw data without endianness adjustments
+    ata_print_identify_data(raw_drive, (options.identify_word_level > 0), options.identify_bit_level);
+  }
 
   // Print most drive identity information if requested
   if (options.drive_info) {
     pout("=== START OF INFORMATION SECTION ===\n");
-    print_drive_info(&drive, sizes, dbentry);
+    print_drive_info(&drive, sizes, rpm, dbentry);
   }
 
   // Check and print SMART support and state
@@ -2336,8 +3505,10 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   if (options.get_aam) {
     if ((drive.command_set_2 & 0xc200) != 0x4200) // word083
       pout("AAM feature is:   Unavailable\n");
-    else if (!(drive.word086 & 0x0200))
-      pout("AAM feature is:   Disabled\n");
+    else if (!(drive.word086 & 0x0200)) {
+      jout("AAM feature is:   Disabled\n");
+      jglb["ata_aam"]["enabled"] = false;
+    }
     else
       print_aam_level("AAM level is:     ", drive.words088_255[94-88] & 0xff,
         drive.words088_255[94-88] >> 8);
@@ -2347,31 +3518,105 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   if (options.get_apm) {
     if ((drive.command_set_2 & 0xc008) != 0x4008) // word083
       pout("APM feature is:   Unavailable\n");
-    else if (!(drive.word086 & 0x0008))
-      pout("APM feature is:   Disabled\n");
+    else if (!(drive.word086 & 0x0008)) {
+      jout("APM feature is:   Disabled\n");
+      jglb["ata_apm"]["enabled"] = false;
+    }
     else
       print_apm_level("APM level is:     ", drive.words088_255[91-88] & 0xff);
   }
 
   // Print read look-ahead status
   if (options.get_lookahead) {
-    pout("Rd look-ahead is: %s\n",
-      (   (drive.command_set_2 & 0xc000) != 0x4000 // word083
-       || !(drive.command_set_1 & 0x0040)) ? "Unavailable" : // word082
-       !(drive.cfs_enable_1 & 0x0040) ? "Disabled" : "Enabled"); // word085
+    if (   (drive.command_set_2 & 0xc000) != 0x4000 // word083
+        || !(drive.command_set_1 & 0x0040)         ) // word082
+      pout("Rd look-ahead is: Unavailable\n");
+    else {
+      bool enabled = !!(drive.cfs_enable_1 & 0x0040); // word085
+      jout("Rd look-ahead is: %sabled\n", (enabled ? "En" : "Dis"));
+      jglb["read_lookahead"]["enabled"] = enabled;
+    }
   }
 
   // Print write cache status
   if (options.get_wcache) {
-    pout("Write cache is:   %s\n",
-      (   (drive.command_set_2 & 0xc000) != 0x4000 // word083
-       || !(drive.command_set_1 & 0x0020)) ? "Unavailable" : // word082
-       !(drive.cfs_enable_1 & 0x0020) ? "Disabled" : "Enabled"); // word085
+    if (   (drive.command_set_2 & 0xc000) != 0x4000 // word083
+        || !(drive.command_set_1 & 0x0020)         ) // word082
+      pout("Write cache is:   Unavailable\n");
+    else {
+      bool enabled = !!(drive.cfs_enable_1 & 0x0020); // word085
+      jout("Write cache is:   %sabled\n", (enabled ? "En" : "Dis"));
+      jglb["write_cache"]["enabled"] = enabled;
+    }
+  }
+
+  // Print DSN status
+  unsigned short word120 = drive.words088_255[120-88];
+  unsigned short word119 = drive.words088_255[119-88];
+  if (options.get_dsn) {
+    if (!(drive.word086 & 0x8000) // word086
+       || ((word119 & 0xc200) != 0x4200) // word119
+       || ((word120 & 0xc000) != 0x4000)) // word120
+      pout("DSN feature is:   Unavailable\n");
+    else {
+      bool enabled = !!(word120 & 0x200);
+      jout("DSN feature is:   %sabled\n", (enabled ? "En" : "Dis"));
+      jglb["ata_dsn"]["enabled"] = enabled;
+    }
   }
 
-  // Print ATA security status
+  // Check for ATA Security LOCK
+  unsigned short word128 = drive.words088_255[128-88];
+  bool locked = ((word128 & 0x0007) == 0x0007); // LOCKED|ENABLED|SUPPORTED
+
+  // Print ATA Security status
   if (options.get_security)
-    print_ata_security_status("ATA Security is:  ", drive.words088_255[128-88]);
+    print_ata_security_status("ATA Security is:  ", word128);
+
+  // Print write cache reordering status
+  if (options.sct_wcache_reorder_get) {
+    if (!isSCTFeatureControlCapable(&drive))
+      pout("Wt Cache Reorder: Unavailable\n");
+    else if (locked)
+      pout("Wt Cache Reorder: Unknown (SCT not supported if ATA Security is LOCKED)\n");
+    else {
+      int wcache_reorder = ataGetSetSCTWriteCacheReordering(device,
+        false /*enable*/, false /*persistent*/, false /*set*/);
+
+      if (-1 <= wcache_reorder && wcache_reorder <= 2)
+        pout("Wt Cache Reorder: %s\n",
+          (wcache_reorder == -1 ? "Unknown (SCT Feature Control command failed)" :
+           wcache_reorder == 0  ? "Unknown" : // not defined in standard but returned on some drives if not set
+           wcache_reorder == 1  ? "Enabled" : "Disabled"));
+      else
+        pout("Wt Cache Reorder: Unknown (0x%02x)\n", wcache_reorder);
+    }
+  }
+
+  const char * sct_write_cache_state_desc[4] = {
+    "Unknown",            // 0: not defined in standard but returned on some drives if not set
+    "Controlled by ATA",  // 1: controlled ATA Set Features command
+    "Force Enabled",      // 2
+    "Force Disabled"      // 3
+  };
+
+  // Print SCT feature control of write cache
+  if (options.sct_wcache_sct_get) {
+    if (!isSCTFeatureControlCapable(&drive))
+      pout("SCT Write Cache Control: Unavailable\n");
+    else if (locked)
+      pout("SCT Write Cache Control: Unknown (SCT not supported if ATA Security is LOCKED)\n");
+    else {
+      int state = ataGetSetSCTWriteCache(device, 1, false /*persistent*/, false /*set*/);
+      if (-1 <= state && state <= 3)
+        pout("SCT Write Cache Control: %s\n",
+             (state == -1 ? "Unknown (SCT Feature Control command failed)" :
+              sct_write_cache_state_desc[state]));
+      else
+        pout("SCT Write Cache Control: Unknown (0x%02x)\n", state);
+    }
+  }
+
 
   // Print remaining drive info
   if (options.drive_info) {
@@ -2388,7 +3633,10 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   // START OF THE ENABLE/DISABLE SECTION OF THE CODE
   if (   options.smart_disable           || options.smart_enable
       || options.smart_auto_save_disable || options.smart_auto_save_enable
-      || options.smart_auto_offl_disable || options.smart_auto_offl_enable)
+      || options.smart_auto_offl_disable || options.smart_auto_offl_enable
+      || options.set_aam || options.set_apm || options.set_lookahead
+      || options.set_wcache || options.set_security_freeze || options.set_standby
+      || options.sct_wcache_reorder_set || options.sct_wcache_sct_set || options.set_dsn)
     pout("=== START OF ENABLE/DISABLE COMMANDS SECTION ===\n");
   
   // Enable/Disable AAM
@@ -2453,6 +3701,53 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
       pout("Write cache %sabled\n", (enable ? "en" : "dis"));
   }
 
+  // Enable/Disable DSN
+  if (options.set_dsn) {
+    bool enable = (options.set_dsn > 0);
+    if (!ata_set_features(device, ATA_ENABLE_DISABLE_DSN, (enable ? 0x1 : 0x2))) {
+        pout("DSN %sable failed: %s\n", (enable ? "en" : "dis"), device->get_errmsg());
+        returnval |= FAILSMART;
+    }
+    else
+      pout("DSN %sabled\n", (enable ? "en" : "dis"));
+  }
+
+  // Enable/Disable write cache reordering
+  if (options.sct_wcache_reorder_set) {
+    bool enable = (options.sct_wcache_reorder_set > 0);
+    if (!isSCTFeatureControlCapable(&drive))
+      pout("Write cache reordering %sable failed: SCT Feature Control command not supported\n",
+        (enable ? "en" : "dis"));
+    else if (locked)
+      pout("Write cache reordering %sable failed: SCT not supported if ATA Security is LOCKED\n",
+        (enable ? "en" : "dis"));
+    else if (ataGetSetSCTWriteCacheReordering(device,
+               enable, options.sct_wcache_reorder_set_pers, true /*set*/) < 0) {
+      pout("Write cache reordering %sable failed: %s\n", (enable ? "en" : "dis"), device->get_errmsg());
+      returnval |= FAILSMART;
+    }
+    else
+      pout("Write cache reordering %sabled (%s)\n", (enable ? "en" : "dis"),
+           (options.sct_wcache_reorder_set_pers ? "persistent" : "volatile"));
+  }
+
+  // Enable/Disable write cache in SCT
+  if (options.sct_wcache_sct_set) {
+    if (!isSCTFeatureControlCapable(&drive))
+      pout("SCT Feature Control of write cache failed: SCT Feature Control command not supported\n");
+    else if (locked)
+      pout("SCT Feature Control of write cache failed: SCT not supported if ATA Security is LOCKED\n");
+    else if (ataGetSetSCTWriteCache(device,
+               options.sct_wcache_sct_set, options.sct_wcache_sct_set_pers, true /*set*/) < 0) {
+      pout("SCT Feature Control of write cache failed: %s\n", device->get_errmsg());
+      returnval |= FAILSMART;
+    }
+    else
+      pout("Write cache SCT Feature Control is set to: %s (%s)\n",
+           sct_write_cache_state_desc[options.sct_wcache_sct_set],
+           (options.sct_wcache_sct_set_pers ? "persistent" : "volatile"));
+  }
+
   // Freeze ATA security
   if (options.set_security_freeze) {
     if (!ata_nodata_command(device, ATA_SECURITY_FREEZE_LOCK)) {
@@ -2463,11 +3758,11 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
       pout("ATA Security set to frozen mode\n");
   }
 
-  // Set standby timer
-  if (options.set_standby) {
+  // Set standby timer unless immediate standby is also requested
+  if (options.set_standby && !options.set_standby_now) {
     if (!ata_nodata_command(device, ATA_IDLE, options.set_standby-1)) {
-        pout("ATA IDLE command failed: %s\n", device->get_errmsg());
-        returnval |= FAILSMART;
+      pout("ATA IDLE command failed: %s\n", device->get_errmsg());
+      returnval |= FAILSMART;
     }
     else
       print_standby_timer("Standby timer set to ", options.set_standby-1, drive);
@@ -2476,7 +3771,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   // Enable/Disable SMART commands
   if (options.smart_enable) {
     if (ataEnableSmart(device)) {
-      pout("Smartctl: SMART Enable Failed.\n\n");
+      pout("SMART Enable failed: %s\n\n", device->get_errmsg());
       failuretest(MANDATORY_CMD, returnval|=FAILSMART);
     }
     else {
@@ -2488,21 +3783,23 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   // Turn off SMART on device
   if (options.smart_disable) {
     if (ataDisableSmart(device)) {
-      pout( "Smartctl: SMART Disable Failed.\n\n");
+      pout("SMART Disable failed: %s\n\n", device->get_errmsg());
       failuretest(MANDATORY_CMD,returnval|=FAILSMART);
     }
   }
 
   // Exit if SMART is disabled but must be enabled to proceed
-  if (options.smart_disable || (smart_enabled <= 0 && need_smart_enabled)) {
+  if (options.smart_disable || (smart_enabled <= 0 && need_smart_enabled && !is_permissive())) {
     pout("SMART Disabled. Use option -s with argument 'on' to enable it.\n");
+    if (!options.smart_disable)
+      pout("(override with '-T permissive' option)\n");
     return returnval;
   }
 
   // Enable/Disable Auto-save attributes
   if (options.smart_auto_save_enable) {
     if (ataEnableAutoSave(device)){
-      pout( "Smartctl: SMART Enable Attribute Autosave Failed.\n\n");
+      pout("SMART Enable Attribute Autosave failed: %s\n\n", device->get_errmsg());
       failuretest(MANDATORY_CMD, returnval|=FAILSMART);
     }
     else
@@ -2511,7 +3808,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
   if (options.smart_auto_save_disable) {
     if (ataDisableAutoSave(device)){
-      pout( "Smartctl: SMART Disable Attribute Autosave Failed.\n\n");
+      pout("SMART Disable Attribute Autosave failed: %s\n\n", device->get_errmsg());
       failuretest(MANDATORY_CMD, returnval|=FAILSMART);
     }
     else
@@ -2525,7 +3822,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
   if (need_smart_val) {
     if (ataReadSmartValues(device, &smartval)) {
-      pout("Smartctl: SMART Read Values failed.\n\n");
+      pout("Read SMART Data failed: %s\n\n", device->get_errmsg());
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     else {
@@ -2533,7 +3830,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
       if (options.smart_check_status || options.smart_vendor_attrib) {
         if (ataReadSmartThresholds(device, &smartthres)){
-          pout("Smartctl: SMART Read Thresholds failed.\n\n");
+          pout("Read SMART Thresholds failed: %s\n\n", device->get_errmsg());
           failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
         }
         else
@@ -2546,12 +3843,12 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   bool needupdate = false;
   if (options.smart_auto_offl_enable) {
     if (!isSupportAutomaticTimer(&smartval)){
-      pout("Warning: device does not support SMART Automatic Timers.\n\n");
+      pout("SMART Automatic Timers not supported\n\n");
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     needupdate = smart_val_ok;
     if (ataEnableAutoOffline(device)){
-      pout( "Smartctl: SMART Enable Automatic Offline Failed.\n\n");
+      pout("SMART Enable Automatic Offline failed: %s\n\n", device->get_errmsg());
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     else
@@ -2560,12 +3857,12 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
   if (options.smart_auto_offl_disable) {
     if (!isSupportAutomaticTimer(&smartval)){
-      pout("Warning: device does not support SMART Automatic Timers.\n\n");
+      pout("SMART Automatic Timers not supported\n\n");
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     needupdate = smart_val_ok;
     if (ataDisableAutoOffline(device)){
-      pout("Smartctl: SMART Disable Automatic Offline Failed.\n\n");
+      pout("SMART Disable Automatic Offline failed: %s\n\n", device->get_errmsg());
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     else
@@ -2573,7 +3870,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   }
 
   if (needupdate && ataReadSmartValues(device, &smartval)){
-    pout("Smartctl: SMART Read Values failed.\n\n");
+    pout("Read SMART Data failed: %s\n\n", device->get_errmsg());
     failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     smart_val_ok = false;
   }
@@ -2581,7 +3878,10 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   // all this for a newline!
   if (   options.smart_disable           || options.smart_enable
       || options.smart_auto_save_disable || options.smart_auto_save_enable
-      || options.smart_auto_offl_disable || options.smart_auto_offl_enable)
+      || options.smart_auto_offl_disable || options.smart_auto_offl_enable
+      || options.set_aam || options.set_apm || options.set_lookahead
+      || options.set_wcache || options.set_security_freeze || options.set_standby
+      || options.sct_wcache_reorder_set || options.set_dsn)
     pout("\n");
 
   // START OF READ-ONLY OPTIONS APART FROM -V and -i
@@ -2599,14 +3899,15 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
     case 0:
       // The case where the disk health is OK
-      pout("SMART overall-health self-assessment test result: PASSED\n");
+      jout("SMART overall-health self-assessment test result: PASSED\n");
+      jglb["smart_status"]["passed"] = true;
       if (smart_thres_ok && find_failed_attr(&smartval, &smartthres, attribute_defs, 0)) {
         if (options.smart_vendor_attrib)
           pout("See vendor-specific Attribute list for marginal Attributes.\n\n");
         else {
           print_on();
           pout("Please note the following marginal Attributes:\n");
-          PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, 2, options.output_format);
+          PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm, 2, options.output_format);
         } 
         returnval|=FAILAGE;
       }
@@ -2617,8 +3918,9 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
     case 1:
       // The case where the disk health is NOT OK
       print_on();
-      pout("SMART overall-health self-assessment test result: FAILED!\n"
+      jout("SMART overall-health self-assessment test result: FAILED!\n"
            "Drive failure expected in less than 24 hours. SAVE ALL DATA.\n");
+      jglb["smart_status"]["passed"] = false;
       print_off();
       if (smart_thres_ok && find_failed_attr(&smartval, &smartthres, attribute_defs, 1)) {
         returnval|=FAILATTR;
@@ -2627,7 +3929,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
         else {
           print_on();
           pout("Failed Attributes:\n");
-          PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, 1, options.output_format);
+          PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm, 1, options.output_format);
         }
       }
       else
@@ -2642,7 +3944,11 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
       // The ATA SMART RETURN STATUS command provides the result in the ATA output
       // registers. Buggy ATA/SATA drivers and SAT Layers often do not properly
       // return the registers values.
+      pout("SMART Status %s: %s\n",
+           (device->is_syscall_unsup() ? "not supported" : "command failed"),
+           device->get_errmsg());
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
+
       if (!(smart_val_ok && smart_thres_ok)) {
         print_on();
         pout("SMART overall-health self-assessment test result: UNKNOWN!\n"
@@ -2650,8 +3956,10 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
       }
       else if (find_failed_attr(&smartval, &smartthres, attribute_defs, 1)) {
         print_on();
-        pout("SMART overall-health self-assessment test result: FAILED!\n"
+        jout("SMART overall-health self-assessment test result: FAILED!\n"
              "Drive failure expected in less than 24 hours. SAVE ALL DATA.\n");
+        jwrn("Warning: This result is based on an Attribute check.\n");
+        jglb["smart_status"]["passed"] = false;
         print_off();
         returnval|=FAILATTR;
         returnval|=FAILSTATUS;
@@ -2660,19 +3968,20 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
         else {
           print_on();
           pout("Failed Attributes:\n");
-          PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, 1, options.output_format);
+          PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm, 1, options.output_format);
         }
       }
       else {
-        pout("SMART overall-health self-assessment test result: PASSED\n");
-        pout("Warning: This result is based on an Attribute check.\n");
+        jout("SMART overall-health self-assessment test result: PASSED\n");
+        jwrn("Warning: This result is based on an Attribute check.\n");
+        jglb["smart_status"]["passed"] = true;
         if (find_failed_attr(&smartval, &smartthres, attribute_defs, 0)) {
           if (options.smart_vendor_attrib)
             pout("See vendor-specific Attribute list for marginal Attributes.\n\n");
           else {
             print_on();
             pout("Please note the following marginal Attributes:\n");
-            PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, 2, options.output_format);
+            PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm, 2, options.output_format);
           } 
           returnval|=FAILAGE;
         }
@@ -2688,19 +3997,20 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   
   // Print general SMART values
   if (smart_val_ok && options.smart_general_values)
-    PrintGeneralSmartValues(&smartval, &drive, fix_firmwarebug);
+    PrintGeneralSmartValues(&smartval, &drive, firmwarebugs);
 
   // Print vendor-specific attributes
   if (smart_val_ok && options.smart_vendor_attrib) {
     print_on();
-    PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs,
+    PrintSmartAttribWithThres(&smartval, &smartthres, attribute_defs, rpm,
                               (printing_is_switchable ? 2 : 0), options.output_format);
     print_off();
   }
 
   // If GP Log is supported use smart log directory for
   // error and selftest log support check.
-  if (   isGeneralPurposeLoggingCapable(&drive)
+  bool gp_log_supported = isGeneralPurposeLoggingCapable(&drive);
+  if (   gp_log_supported
       && (   options.smart_error_log || options.smart_selftest_log
           || options.retry_error_log || options.retry_selftest_log))
     need_smart_logdir = true;
@@ -2710,8 +4020,10 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
   // Read SMART Log directory
   if (need_smart_logdir) {
-    if (ataReadLogDirectory(device, &smartlogdir_buf, false)) {
-      pout("Read SMART Log Directory failed.\n\n");
+    if (firmwarebugs.is_set(BUG_NOLOGDIR))
+      smartlogdir = fake_logdir(&smartlogdir_buf, options);
+    else if (ataReadLogDirectory(device, &smartlogdir_buf, false)) {
+      pout("Read SMART Log Directory failed: %s\n\n", device->get_errmsg());
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     else
@@ -2720,8 +4032,14 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
   // Read GP Log directory
   if (need_gp_logdir) {
-    if (ataReadLogDirectory(device, &gplogdir_buf, true)) {
-      pout("Read GP Log Directory failed.\n\n");
+    if (firmwarebugs.is_set(BUG_NOLOGDIR))
+      gplogdir = fake_logdir(&gplogdir_buf, options);
+    else if (!gp_log_supported && !is_permissive()) {
+      if (options.gp_logdir)
+        pout("General Purpose Log Directory not supported\n\n");
+    }
+    else if (ataReadLogDirectory(device, &gplogdir_buf, true)) {
+      pout("Read GP Log Directory failed\n\n");
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     else
@@ -2729,8 +4047,12 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   }
 
   // Print log directories
-  if ((options.gp_logdir && gplogdir) || (options.smart_logdir && smartlogdir))
-    PrintLogDirectories(gplogdir, smartlogdir);
+  if ((options.gp_logdir && gplogdir) || (options.smart_logdir && smartlogdir)) {
+    if (firmwarebugs.is_set(BUG_NOLOGDIR))
+      pout("Log Directories not read due to '-F nologdir' option\n\n");
+    else
+      PrintLogDirectories(gplogdir, smartlogdir);
+  }
 
   // Print log pages
   for (i = 0; i < options.log_requests.size(); i++) {
@@ -2787,16 +4109,17 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
     bool ok = false;
     unsigned nsectors = GetNumLogSectors(gplogdir, 0x03, true);
     if (!nsectors)
-      pout("SMART Extended Comprehensive Error Log (GP Log 0x03) not supported\n");
-    else if (nsectors >= 256)
-      pout("SMART Extended Comprehensive Error Log size %u not supported\n", nsectors);
+      pout("SMART Extended Comprehensive Error Log (GP Log 0x03) not supported\n\n");
     else {
-      raw_buffer log_03_buf(nsectors * 512);
-      ata_smart_exterrlog * log_03 = (ata_smart_exterrlog *)log_03_buf.data();
-      if (!ataReadExtErrorLog(device, log_03, nsectors))
+      // Read only first sector to get error count and index
+      // Print function will read more sectors as needed
+      ata_smart_exterrlog log_03; memset(&log_03, 0, sizeof(log_03));
+      if (!ataReadExtErrorLog(device, &log_03, 0, 1, firmwarebugs)) {
+        pout("Read SMART Extended Comprehensive Error Log failed\n\n");
         failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
+      }
       else {
-        if (PrintSmartExtErrorLog(log_03, nsectors, options.smart_ext_error_log))
+        if (PrintSmartExtErrorLog(device, firmwarebugs, &log_03, nsectors, options.smart_ext_error_log))
           returnval |= FAILERR;
         ok = true;
       }
@@ -2812,20 +4135,21 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
   // Print SMART error log
   if (do_smart_error_log) {
-    if (!(   ( smartlogdir && GetNumLogSectors(smartlogdir, 0x01, false))
-          || (!smartlogdir && isSmartErrorLogCapable(&smartval, &drive) )
-          || is_permissive()                                             )) {
-      pout("SMART Error Log not supported\n");
+    if (!(   GetNumLogSectors(smartlogdir, 0x01, false)
+          || (   !(smartlogdir && gp_log_supported)
+              && isSmartErrorLogCapable(&smartval, &drive))
+          || is_permissive()                               )) {
+      pout("SMART Error Log not supported\n\n");
     }
     else {
       ata_smart_errorlog smarterror; memset(&smarterror, 0, sizeof(smarterror));
-      if (ataReadErrorLog(device, &smarterror, fix_firmwarebug)) {
-        pout("Smartctl: SMART Error Log Read Failed\n");
+      if (ataReadErrorLog(device, &smarterror, firmwarebugs)) {
+        pout("Read SMART Error Log failed: %s\n\n", device->get_errmsg());
         failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
       }
       else {
         // quiet mode is turned on inside PrintSmartErrorLog()
-        if (PrintSmartErrorlog(&smarterror, fix_firmwarebug))
+        if (PrintSmartErrorlog(&smarterror, firmwarebugs))
          returnval|=FAILERR;
         print_off();
       }
@@ -2838,14 +4162,16 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
     bool ok = false;
     unsigned nsectors = GetNumLogSectors(gplogdir, 0x07, true);
     if (!nsectors)
-      pout("SMART Extended Self-test Log (GP Log 0x07) not supported\n");
+      pout("SMART Extended Self-test Log (GP Log 0x07) not supported\n\n");
     else if (nsectors >= 256)
-      pout("SMART Extended Self-test Log size %u not supported\n", nsectors);
+      pout("SMART Extended Self-test Log size %u not supported\n\n", nsectors);
     else {
       raw_buffer log_07_buf(nsectors * 512);
-      ata_smart_extselftestlog * log_07 = (ata_smart_extselftestlog *)log_07_buf.data();
-      if (!ataReadExtSelfTestLog(device, log_07, nsectors))
+      ata_smart_extselftestlog * log_07 = reinterpret_cast<ata_smart_extselftestlog *>(log_07_buf.data());
+      if (!ataReadExtSelfTestLog(device, log_07, nsectors)) {
+        pout("Read SMART Extended Self-test Log failed\n\n");
         failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
+      }
       else {
         if (PrintSmartExtSelfTestLog(log_07, nsectors, options.smart_ext_selftest_log))
           returnval |= FAILLOG;
@@ -2863,20 +4189,21 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
   // Print SMART self-test log
   if (do_smart_selftest_log) {
-    if (!(   ( smartlogdir && GetNumLogSectors(smartlogdir, 0x06, false))
-          || (!smartlogdir && isSmartTestLogCapable(&smartval, &drive)  )
-          || is_permissive()                                             )) {
-      pout("SMART Self-test Log not supported\n");
+    if (!(   GetNumLogSectors(smartlogdir, 0x06, false)
+          || (   !(smartlogdir && gp_log_supported)
+              && isSmartTestLogCapable(&smartval, &drive))
+          || is_permissive()                              )) {
+      pout("SMART Self-test Log not supported\n\n");
     }
     else {
       ata_smart_selftestlog smartselftest; memset(&smartselftest, 0, sizeof(smartselftest));
-      if (ataReadSelfTestLog(device, &smartselftest, fix_firmwarebug)) {
-        pout("Smartctl: SMART Self Test Log Read Failed\n");
+      if (ataReadSelfTestLog(device, &smartselftest, firmwarebugs)) {
+        pout("Read SMART Self-test Log failed: %s\n\n", device->get_errmsg());
         failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
       }
       else {
         print_on();
-        if (ataPrintSmartSelfTestlog(&smartselftest, !printing_is_switchable, fix_firmwarebug))
+        if (ataPrintSmartSelfTestlog(&smartselftest, !printing_is_switchable, firmwarebugs))
           returnval |= FAILLOG;
         print_off();
         pout("\n");
@@ -2889,9 +4216,9 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
     ata_selective_self_test_log log;
 
     if (!isSupportSelectiveSelfTest(&smartval))
-      pout("Device does not support Selective Self Tests/Logging\n");
+      pout("Selective Self-tests/Logging not supported\n\n");
     else if(ataReadSelectiveSelfTestLog(device, &log)) {
-      pout("Smartctl: SMART Selective Self Test Log Read Failed\n");
+      pout("Read SMART Selective Self-test Log failed: %s\n\n", device->get_errmsg());
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     else {
@@ -2906,56 +4233,65 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
     }
   }
 
-  // SCT commands
-  bool sct_ok = false;
-  if (need_sct_support) {
-    if (!isSCTCapable(&drive)) {
-      pout("Warning: device does not support SCT Commands\n");
-      failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
+  // Check if SCT commands available
+  bool sct_ok = isSCTCapable(&drive);
+  if (   options.sct_temp_sts || options.sct_temp_hist || options.sct_temp_int
+      || options.sct_erc_get  || options.sct_erc_set                          ) {
+    if (!sct_ok)
+      pout("SCT Commands not supported\n\n");
+    else if (locked) {
+      pout("SCT Commands not supported if ATA Security is LOCKED\n\n");
+      sct_ok = false;
     }
-    else
-      sct_ok = true;
   }
 
   // Print SCT status and temperature history table
   if (sct_ok && (options.sct_temp_sts || options.sct_temp_hist || options.sct_temp_int)) {
     for (;;) {
-      if (options.sct_temp_sts || options.sct_temp_hist) {
-        ata_sct_status_response sts;
-        ata_sct_temperature_history_table tmh;
-        if (!options.sct_temp_hist) {
-          // Read SCT status only
-          if (ataReadSCTStatus(device, &sts)) {
-            failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
-            break;
-          }
-        }
-        else {
-          if (!isSCTDataTableCapable(&drive)) {
-            pout("Warning: device does not support SCT Data Table command\n");
-            failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
-            break;
-          }
-          // Read SCT status and temperature history
-          if (ataReadSCTTempHist(device, &tmh, &sts)) {
-            failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
-            break;
-          }
+      bool sct_temp_hist_ok = isSCTDataTableCapable(&drive);
+      ata_sct_status_response sts;
+
+      if (options.sct_temp_sts || (options.sct_temp_hist && sct_temp_hist_ok)) {
+        // Read SCT status
+        if (ataReadSCTStatus(device, &sts)) {
+          pout("\n");
+          failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
+          break;
         }
-        if (options.sct_temp_sts)
+        if (options.sct_temp_sts) {
           ataPrintSCTStatus(&sts);
-        if (options.sct_temp_hist)
-          ataPrintSCTTempHist(&tmh);
+          pout("\n");
+        }
+      }
+
+      if (!sct_temp_hist_ok && (options.sct_temp_hist || options.sct_temp_int)) {
+        pout("SCT Data Table command not supported\n\n");
+        failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
+        break;
+      }
+
+      if (options.sct_temp_hist) {
+        // Read SCT temperature history,
+        // requires initial SCT status from above
+        ata_sct_temperature_history_table tmh;
+        if (ataReadSCTTempHist(device, &tmh, &sts)) {
+          pout("Read SCT Temperature History failed\n\n");
+          failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
+          break;
+        }
+        ataPrintSCTTempHist(&tmh);
         pout("\n");
       }
+
       if (options.sct_temp_int) {
         // Set new temperature logging interval
         if (!isSCTFeatureControlCapable(&drive)) {
-          pout("Warning: device does not support SCT Feature Control command\n");
+          pout("SCT Feature Control command not supported\n\n");
           failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
           break;
         }
         if (ataSetSCTTempInterval(device, options.sct_temp_int, options.sct_temp_int_pers)) {
+          pout("Write Temperature Logging Interval failed\n\n");
           failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
           break;
         }
@@ -2970,7 +4306,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   // SCT Error Recovery Control
   if (sct_ok && (options.sct_erc_get || options.sct_erc_set)) {
     if (!isSCTErrorRecoveryControlCapable(&drive)) {
-      pout("Warning: device does not support SCT Error Recovery Control command\n");
+      pout("SCT Error Recovery Control command not supported\n\n");
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     else {
@@ -2979,7 +4315,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
         // Set SCT Error Recovery Control
         if (   ataSetSCTErrorRecoveryControltime(device, 1, options.sct_erc_readtime )
             || ataSetSCTErrorRecoveryControltime(device, 2, options.sct_erc_writetime)) {
-          pout("Warning: device does not support SCT (Set) Error Recovery Control command\n");
+          pout("SCT (Set) Error Recovery Control command failed\n");
           if (!(   (options.sct_erc_readtime == 70 && options.sct_erc_writetime == 70)
                 || (options.sct_erc_readtime ==  0 && options.sct_erc_writetime ==  0)))
             pout("Retry with: 'scterc,70,70' to enable ERC or 'scterc,0,0' to disable\n");
@@ -2996,7 +4332,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
         unsigned short read_timer, write_timer;
         if (   ataGetSCTErrorRecoveryControltime(device, 1, read_timer )
             || ataGetSCTErrorRecoveryControltime(device, 2, write_timer)) {
-          pout("Warning: device does not support SCT (Get) Error Recovery Control command\n");
+          pout("SCT (Get) Error Recovery Control command failed\n");
           if (options.sct_erc_set) {
             pout("The previous SCT (Set) Error Recovery Control command succeeded\n");
             ataPrintSCTErrorRecoveryControl(true, options.sct_erc_readtime,
@@ -3013,40 +4349,73 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
 
   // Print Device Statistics
   if (options.devstat_all_pages || options.devstat_ssd_page || !options.devstat_pages.empty()) {
-    unsigned nsectors = GetNumLogSectors(gplogdir, 0x04, true);
+    bool use_gplog = true;
+    unsigned nsectors = 0;
+    if (gplogdir) 
+      nsectors = GetNumLogSectors(gplogdir, 0x04, true);
+    else if (smartlogdir){ // for systems without ATA_READ_LOG_EXT
+      nsectors = GetNumLogSectors(smartlogdir, 0x04, false);
+      use_gplog = false;
+    }
     if (!nsectors)
-      pout("Device Statistics (GP Log 0x04) not supported\n");
+      pout("Device Statistics (GP/SMART Log 0x04) not supported\n\n");
     else if (!print_device_statistics(device, nsectors, options.devstat_pages,
-               options.devstat_all_pages, options.devstat_ssd_page))
+               options.devstat_all_pages, options.devstat_ssd_page, use_gplog))
+      failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
+  }
+
+  // Print Pending Defects log
+  if (options.pending_defects_log) {
+    unsigned nsectors = GetNumLogSectors(gplogdir, 0x0c, true);
+    if (!nsectors)
+      pout("Pending Defects log (GP Log 0x0c) not supported\n\n");
+    else if (!print_pending_defects_log(device, nsectors, options.pending_defects_log))
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
   }
 
   // Print SATA Phy Event Counters
   if (options.sataphy) {
     unsigned nsectors = GetNumLogSectors(gplogdir, 0x11, true);
+    // Packet interface devices do not provide a log directory, check support bit
+    if (!nsectors && (drive.words047_079[76-47] & 0x0401) == 0x0400)
+      nsectors = 1;
     if (!nsectors)
-      pout("SATA Phy Event Counters (GP Log 0x11) not supported\n");
+      pout("SATA Phy Event Counters (GP Log 0x11) not supported\n\n");
     else if (nsectors != 1)
-      pout("SATA Phy Event Counters with %u sectors not supported\n", nsectors);
+      pout("SATA Phy Event Counters with %u sectors not supported\n\n", nsectors);
     else {
       unsigned char log_11[512] = {0, };
       unsigned char features = (options.sataphy_reset ? 0x01 : 0x00);
-      if (!ataReadLogExt(device, 0x11, features, 0, log_11, 1))
+      if (!ataReadLogExt(device, 0x11, features, 0, log_11, 1)) {
+        pout("Read SATA Phy Event Counters failed\n\n");
         failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
+      }
       else
         PrintSataPhyEventCounters(log_11, options.sataphy_reset);
     }
   }
 
-  // Set to standby (spindown) mode
+  // Set to standby (spindown) mode and set standby timer if not done above
   // (Above commands may spinup drive)
   if (options.set_standby_now) {
-    if (!ata_nodata_command(device, ATA_STANDBY_IMMEDIATE)) {
+    if (options.set_standby) {
+      if (!ata_nodata_command(device, ATA_STANDBY, options.set_standby-1)) {
+        pout("ATA STANDBY command failed: %s\n", device->get_errmsg());
+        returnval |= FAILSMART;
+      }
+      else {
+        print_standby_timer("Standby timer set to ", options.set_standby-1, drive);
+        pout("Device placed in STANDBY mode\n");
+      }
+    }
+    else {
+      if (!ata_nodata_command(device, ATA_STANDBY_IMMEDIATE)) {
         pout("ATA STANDBY IMMEDIATE command failed: %s\n", device->get_errmsg());
         returnval |= FAILSMART;
+      }
+      else
+        pout("Device placed in STANDBY mode\n");
     }
-    else
-      pout("Device placed in STANDBY mode\n");
   }
 
   // START OF THE TESTING SECTION OF THE CODE.  IF NO TESTING, RETURN
@@ -3058,7 +4427,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   switch (options.smart_selftest_type) {
   case OFFLINE_FULL_SCAN:
     if (!isSupportExecuteOfflineImmediate(&smartval)){
-      pout("Warning: device does not support Execute Offline Immediate function.\n\n");
+      pout("Execute Offline Immediate function not supported\n\n");
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     break;
@@ -3068,21 +4437,21 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
   case SHORT_CAPTIVE_SELF_TEST:
   case EXTEND_CAPTIVE_SELF_TEST:
     if (!isSupportSelfTest(&smartval)){
-      pout("Warning: device does not support Self-Test functions.\n\n");
+      pout("Self-test functions not supported\n\n");
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     break;
   case CONVEYANCE_SELF_TEST:
   case CONVEYANCE_CAPTIVE_SELF_TEST:
     if (!isSupportConveyanceSelfTest(&smartval)){
-      pout("Warning: device does not support Conveyance Self-Test functions.\n\n");
+      pout("Conveyance Self-test functions not supported\n\n");
       failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
     }
     break;
   case SELECTIVE_SELF_TEST:
   case SELECTIVE_CAPTIVE_SELF_TEST:
     if (!isSupportSelectiveSelfTest(&smartval)){
-      pout("Warning: device does not support Selective Self-Test functions.\n\n");
+      pout("Selective Self-test functions not supported\n\n");
       failuretest(MANDATORY_CMD, returnval|=FAILSMART);
     }
     break;
@@ -3105,7 +4474,7 @@ int ataPrintMain (ata_device * device, const ata_print_options & options)
       if (isSupportOfflineAbort(&smartval))
        pout("Note: giving further SMART commands will abort Offline testing\n");
       else if (ataReadSmartValues(device, &smartval)){
-       pout("Smartctl: SMART Read Values failed.\n");
+        pout("Read SMART Data failed: %s\n\n", device->get_errmsg());
        failuretest(OPTIONAL_CMD, returnval|=FAILSMART);
       }
     }