]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - knowndrives.cpp
Refreshed patches
[mirror_smartmontools-debian.git] / knowndrives.cpp
index 4d6901d2a6dedaffc453f372c4625f540c222cad..25d3babcb7d0a321ea1161245ebc50c97bf4ee18 100644 (file)
@@ -4,8 +4,8 @@
  * Home page of code is: http://smartmontools.sourceforge.net
  * Address of support mailing list: smartmontools-support@lists.sourceforge.net
  *
- * Copyright (C) 2003-9 Philip Williams, Bruce Allen
- * Copyright (C) 2008-9 Christian Franke <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2003-11 Philip Williams, Bruce Allen
+ * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net>
  *
  * 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
@@ -13,8 +13,7 @@
  * 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.
+ * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
  *
  */
 
@@ -22,7 +21,6 @@
 #include "int64.h"
 #include <stdio.h>
 #include "atacmds.h"
-#include "extern.h"
 #include "knowndrives.h"
 #include "utility.h"
 
@@ -35,7 +33,7 @@
 
 #include <stdexcept>
 
-const char * knowndrives_cpp_cvsid = "$Id: knowndrives.cpp 3004 2009-12-19 19:39:12Z chrfranke $"
+const char * knowndrives_cpp_cvsid = "$Id: knowndrives.cpp 3719 2012-12-03 21:19:33Z chrfranke $"
                                      KNOWNDRIVES_H_CVSID;
 
 #define MODEL_STRING_LENGTH                         40
@@ -125,14 +123,16 @@ void drive_database::push_back(const drive_settings & src)
 
 const char * drive_database::copy_string(const char * src)
 {
-  char * dest = new char[strlen(src)+1];
+  size_t len = strlen(src);
+  char * dest = new char[len+1];
+  memcpy(dest, src, len+1);
   try {
     m_custom_strings.push_back(dest);
   }
   catch (...) {
     delete [] dest; throw;
   }
-  return strcpy(dest, src);
+  return dest;
 }
 
 
@@ -140,6 +140,18 @@ const char * drive_database::copy_string(const char * src)
 static drive_database knowndrives;
 
 
+// Return true if modelfamily string describes entry for USB ID
+static bool is_usb_modelfamily(const char * modelfamily)
+{
+  return !strncmp(modelfamily, "USB:", 4);
+}
+
+// Return true if entry for USB ID
+static inline bool is_usb_entry(const drive_settings * dbentry)
+{
+  return is_usb_modelfamily(dbentry->modelfamily);
+}
+
 // Compile regular expression, print message on failure.
 static bool compile(regular_expression & regex, const char *pattern)
 {
@@ -165,7 +177,7 @@ static bool match(const char * pattern, const char * str)
 // string.  If either the drive's model or firmware strings are not set by the
 // manufacturer then values of NULL may be used.  Returns the entry of the
 // first match in knowndrives[] or 0 if no match if found.
-const drive_settings * lookup_drive(const char * model, const char * firmware)
+static const drive_settings * lookup_drive(const char * model, const char * firmware)
 {
   if (!model)
     model = "";
@@ -173,6 +185,10 @@ const drive_settings * lookup_drive(const char * model, const char * firmware)
     firmware = "";
 
   for (unsigned i = 0; i < knowndrives.size(); i++) {
+    // Skip USB entries
+    if (is_usb_entry(&knowndrives[i]))
+      continue;
+
     // Check whether model matches the regular expression in knowndrives[i].
     if (!match(knowndrives[i].modelregexp, model))
       continue;
@@ -190,35 +206,34 @@ const drive_settings * lookup_drive(const char * model, const char * firmware)
   return 0;
 }
 
-// Parse '-v' and '-F' options in preset string, return false on error.
-static bool parse_presets(const char * presets, ata_vendor_attr_defs & defs,
-                          unsigned char & fix_firmwarebug)
+
+// Parse drive or USB options in preset string, return false on error.
+static bool parse_db_presets(const char * presets, ata_vendor_attr_defs * defs,
+                             firmwarebug_defs * firmwarebugs, std::string * type)
 {
   for (int i = 0; ; ) {
     i += strspn(presets+i, " \t");
     if (!presets[i])
       break;
-    char opt, arg[40+1+13]; int len = -1;
-    if (!(sscanf(presets+i, "-%c %40[^ ]%n", &opt, arg, &len) >= 2 && len > 0))
+    char opt, arg[80+1+13]; int len = -1;
+    if (!(sscanf(presets+i, "-%c %80[^ ]%n", &opt, arg, &len) >= 2 && len > 0))
       return false;
-    if (opt == 'v') {
+    if (opt == 'v' && defs) {
       // Parse "-v N,format[,name]"
-      if (!parse_attribute_def(arg, defs, PRIOR_DATABASE))
+      if (!parse_attribute_def(arg, *defs, PRIOR_DATABASE))
         return false;
     }
-    else if (opt == 'F') {
-      unsigned char fix;
-      if (!strcmp(arg, "samsung"))
-        fix = FIX_SAMSUNG;
-      else if (!strcmp(arg, "samsung2"))
-        fix = FIX_SAMSUNG2;
-      else if (!strcmp(arg, "samsung3"))
-        fix = FIX_SAMSUNG3;
-      else
+    else if (opt == 'F' && firmwarebugs) {
+      firmwarebug_defs bug;
+      if (!parse_firmwarebug_def(arg, bug))
         return false;
-      // Set only if not set by user
-      if (fix_firmwarebug == FIX_NOTSPECIFIED)
-        fix_firmwarebug = fix;
+      // Don't set if user specified '-F none'.
+      if (!firmwarebugs->is_set(BUG_NONE))
+        firmwarebugs->set(bug);
+    }
+    else if (opt == 'd' && type) {
+        // TODO: Check valid types
+        *type = arg;
     }
     else
       return false;
@@ -228,6 +243,89 @@ static bool parse_presets(const char * presets, ata_vendor_attr_defs & defs,
   return true;
 }
 
+// Parse '-v' and '-F' options in preset string, return false on error.
+static inline bool parse_presets(const char * presets,
+                                 ata_vendor_attr_defs & defs,
+                                 firmwarebug_defs & firmwarebugs)
+{
+  return parse_db_presets(presets, &defs, &firmwarebugs, 0);
+}
+
+// Parse '-d' option in preset string, return false on error.
+static inline bool parse_usb_type(const char * presets, std::string & type)
+{
+  return parse_db_presets(presets, 0, 0, &type);
+}
+
+// Parse "USB: [DEVICE] ; [BRIDGE]" string
+static void parse_usb_names(const char * names, usb_dev_info & info)
+{
+  int n1 = -1, n2 = -1, n3 = -1;
+  sscanf(names, "USB: %n%*[^;]%n; %n", &n1, &n2, &n3);
+  if (0 < n1 && n1 < n2)
+    info.usb_device.assign(names+n1, n2-n1);
+  else
+    sscanf(names, "USB: ; %n", &n3);
+  if (0 < n3)
+    info.usb_bridge = names+n3;
+}
+
+// Search drivedb for USB device with vendor:product ID.
+int lookup_usb_device(int vendor_id, int product_id, int bcd_device,
+                      usb_dev_info & info, usb_dev_info & info2)
+{
+  // Format strings to match
+  char usb_id_str[16], bcd_dev_str[16];
+  snprintf(usb_id_str, sizeof(usb_id_str), "0x%04x:0x%04x", vendor_id, product_id);
+  if (bcd_device >= 0)
+    snprintf(bcd_dev_str, sizeof(bcd_dev_str), "0x%04x", bcd_device);
+  else
+    bcd_dev_str[0] = 0;
+
+  int found = 0;
+  for (unsigned i = 0; i < knowndrives.size(); i++) {
+    const drive_settings & dbentry = knowndrives[i];
+
+    // Skip drive entries
+    if (!is_usb_entry(&dbentry))
+      continue;
+
+    // Check whether USB vendor:product ID matches
+    if (!match(dbentry.modelregexp, usb_id_str))
+      continue;
+
+    // Parse '-d type'
+    usb_dev_info d;
+    if (!parse_usb_type(dbentry.presets, d.usb_type))
+      return 0; // Syntax error
+    parse_usb_names(dbentry.modelfamily, d);
+
+    // If two entries with same vendor:product ID have different
+    // types, use bcd_device (if provided by OS) to select entry.
+    if (  *dbentry.firmwareregexp && *bcd_dev_str
+        && match(dbentry.firmwareregexp, bcd_dev_str)) {
+      // Exact match including bcd_device
+      info = d; found = 1;
+      break;
+    }
+    else if (!found) {
+      // First match without bcd_device
+      info = d; found = 1;
+    }
+    else if (info.usb_type != d.usb_type) {
+      // Another possible match with different type
+      info2 = d; found = 2;
+      break;
+    }
+
+    // Stop search at first matching entry with empty bcd_device
+    if (!*dbentry.firmwareregexp)
+      break;
+  }
+
+  return found;
+}
+
 // Shows one entry of knowndrives[], returns #errors.
 static int showonepreset(const drive_settings * dbentry)
 {
@@ -242,60 +340,95 @@ static int showonepreset(const drive_settings * dbentry)
          "this error to smartmontools developers at " PACKAGE_BUGREPORT ".\n");
     return 1;
   }
-  
+
+  bool usb = is_usb_entry(dbentry);
+
   // print and check model and firmware regular expressions
   int errcnt = 0;
   regular_expression regex;
-  pout("%-*s %s\n", TABLEPRINTWIDTH, "MODEL REGEXP:", dbentry->modelregexp);
+  pout("%-*s %s\n", TABLEPRINTWIDTH, (!usb ? "MODEL REGEXP:" : "USB Vendor:Product:"),
+       dbentry->modelregexp);
   if (!compile(regex, dbentry->modelregexp))
     errcnt++;
 
-  pout("%-*s %s\n", TABLEPRINTWIDTH, "FIRMWARE REGEXP:", *dbentry->firmwareregexp ?
-    dbentry->firmwareregexp : ".*"); // preserve old output (TODO: Change)
+  pout("%-*s %s\n", TABLEPRINTWIDTH, (!usb ? "FIRMWARE REGEXP:" : "USB bcdDevice:"),
+       *dbentry->firmwareregexp ? dbentry->firmwareregexp : ".*"); // preserve old output (TODO: Change)
   if (*dbentry->firmwareregexp && !compile(regex, dbentry->firmwareregexp))
     errcnt++;
 
-  pout("%-*s %s\n", TABLEPRINTWIDTH, "MODEL FAMILY:", dbentry->modelfamily);
-
-  // if there are any presets, then show them
-  unsigned char fix_firmwarebug = 0;
-  bool first_preset = true;
-  if (*dbentry->presets) {
-    ata_vendor_attr_defs defs;
-    if (!parse_presets(dbentry->presets, defs, fix_firmwarebug)) {
-      pout("Syntax error in preset option string \"%s\"\n", dbentry->presets);
-      errcnt++;
+  if (!usb) {
+    pout("%-*s %s\n", TABLEPRINTWIDTH, "MODEL FAMILY:", dbentry->modelfamily);
+
+    // if there are any presets, then show them
+    firmwarebug_defs firmwarebugs;
+    bool first_preset = true;
+    if (*dbentry->presets) {
+      ata_vendor_attr_defs defs;
+      if (!parse_presets(dbentry->presets, defs, firmwarebugs)) {
+        pout("Syntax error in preset option string \"%s\"\n", dbentry->presets);
+        errcnt++;
+      }
+      for (int i = 0; i < MAX_ATTRIBUTE_NUM; i++) {
+        if (defs[i].priority != PRIOR_DEFAULT) {
+          std::string name = ata_get_smart_attr_name(i, defs);
+          // Use leading zeros instead of spaces so that everything lines up.
+          pout("%-*s %03d %s\n", TABLEPRINTWIDTH, first_preset ? "ATTRIBUTE OPTIONS:" : "",
+               i, name.c_str());
+          // Check max name length suitable for smartctl -A output
+          const unsigned maxlen = 23;
+          if (name.size() > maxlen) {
+            pout("%*s\n", TABLEPRINTWIDTH+6+maxlen, "Error: Attribute name too long ------^");
+            errcnt++;
+          }
+          first_preset = false;
+        }
+      }
     }
-    for (int i = 0; i < MAX_ATTRIBUTE_NUM; i++) {
-      if (defs[i].priority != PRIOR_DEFAULT) {
-        // Use leading zeros instead of spaces so that everything lines up.
-        pout("%-*s %03d %s\n", TABLEPRINTWIDTH, first_preset ? "ATTRIBUTE OPTIONS:" : "",
-             i, ata_get_smart_attr_name(i, defs).c_str());
-        first_preset = false;
+    if (first_preset)
+      pout("%-*s %s\n", TABLEPRINTWIDTH, "ATTRIBUTE OPTIONS:", "None preset; no -v options are required.");
+
+    // describe firmwarefix
+    for (int b = BUG_NOLOGDIR; b <= BUG_XERRORLBA; b++) {
+      if (!firmwarebugs.is_set((firmwarebug_t)b))
+        continue;
+      const char * fixdesc;
+      switch ((firmwarebug_t)b) {
+        case BUG_NOLOGDIR:
+          fixdesc = "Avoids reading GP/SMART Log Directories (same as -F nologdir)";
+          break;
+        case BUG_SAMSUNG:
+          fixdesc = "Fixes byte order in some SMART data (same as -F samsung)";
+          break;
+        case BUG_SAMSUNG2:
+          fixdesc = "Fixes byte order in some SMART data (same as -F samsung2)";
+          break;
+        case BUG_SAMSUNG3:
+          fixdesc = "Fixes completed self-test reported as in progress (same as -F samsung3)";
+          break;
+        case BUG_XERRORLBA:
+          fixdesc = "Fixes LBA byte ordering in Ext. Comprehensive SMART error log (same as -F xerrorlba)";
+          break;
+        default:
+          fixdesc = "UNKNOWN"; errcnt++;
+          break;
       }
+      pout("%-*s %s\n", TABLEPRINTWIDTH, "OTHER PRESETS:", fixdesc);
     }
   }
-  if (first_preset)
-    pout("%-*s %s\n", TABLEPRINTWIDTH, "ATTRIBUTE OPTIONS:", "None preset; no -v options are required.");
-
-  // describe firmwarefix
-  if (fix_firmwarebug) {
-    const char * fixdesc;
-    switch (fix_firmwarebug) {
-      case FIX_SAMSUNG:
-        fixdesc = "Fixes byte order in some SMART data (same as -F samsung)";
-        break;
-      case FIX_SAMSUNG2:
-        fixdesc = "Fixes byte order in some SMART data (same as -F samsung2)";
-        break;
-      case FIX_SAMSUNG3:
-        fixdesc = "Fixes completed self-test reported as in progress (same as -F samsung3)";
-        break;
-      default:
-        fixdesc = "UNKNOWN"; errcnt++;
-        break;
+  else {
+    // Print USB info
+    usb_dev_info info; parse_usb_names(dbentry->modelfamily, info);
+    pout("%-*s %s\n", TABLEPRINTWIDTH, "USB Device:",
+      (!info.usb_device.empty() ? info.usb_device.c_str() : "[unknown]"));
+    pout("%-*s %s\n", TABLEPRINTWIDTH, "USB Bridge:",
+      (!info.usb_bridge.empty() ? info.usb_bridge.c_str() : "[unknown]"));
+
+    if (*dbentry->presets && !parse_usb_type(dbentry->presets, info.usb_type)) {
+      pout("Syntax error in USB type string \"%s\"\n", dbentry->presets);
+      errcnt++;
     }
-    pout("%-*s %s\n", TABLEPRINTWIDTH, "OTHER PRESETS:", fixdesc);
+    pout("%-*s %s\n", TABLEPRINTWIDTH, "USB Type",
+      (!info.usb_type.empty() ? info.usb_type.c_str() : "[unsupported]"));
   }
 
   // Print any special warnings
@@ -364,14 +497,14 @@ int showmatchingpresets(const char *model, const char *firmware)
 }
 
 // Shows the presets (if any) that are available for the given drive.
-void show_presets(const ata_identify_device * drive, bool fix_swapped_id)
+void show_presets(const ata_identify_device * drive)
 {
   char model[MODEL_STRING_LENGTH+1], firmware[FIRMWARE_STRING_LENGTH+1];
 
   // get the drive's model/firmware strings
-  format_ata_string(model, drive->model, MODEL_STRING_LENGTH, fix_swapped_id);
-  format_ata_string(firmware, drive->fw_rev, FIRMWARE_STRING_LENGTH, fix_swapped_id);
-  
+  ata_format_id_string(model, drive->model, sizeof(model)-1);
+  ata_format_id_string(firmware, drive->fw_rev, sizeof(firmware)-1);
+
   // and search to see if they match values in the table
   const drive_settings * dbentry = lookup_drive(model, firmware);
   if (!dbentry) {
@@ -394,29 +527,30 @@ void show_presets(const ata_identify_device * drive, bool fix_swapped_id)
   showonepreset(dbentry);
 }
 
-// Sets preset vendor attribute options in opts by finding the entry
-// (if any) for the given drive in knowndrives[].  Values that have
-// already been set in opts will not be changed.  Returns false if drive
-// not recognized.
-bool apply_presets(const ata_identify_device *drive, ata_vendor_attr_defs & defs,
-                   unsigned char & fix_firmwarebug, bool fix_swapped_id)
+// Searches drive database and sets preset vendor attribute
+// options in defs and firmwarebugs.
+// Values that have already been set will not be changed.
+// Returns pointer to database entry or nullptr if none found
+const drive_settings * lookup_drive_apply_presets(
+  const ata_identify_device * drive, ata_vendor_attr_defs & defs,
+  firmwarebug_defs & firmwarebugs)
 {
   // get the drive's model/firmware strings
   char model[MODEL_STRING_LENGTH+1], firmware[FIRMWARE_STRING_LENGTH+1];
-  format_ata_string(model, drive->model, MODEL_STRING_LENGTH, fix_swapped_id);
-  format_ata_string(firmware, drive->fw_rev, FIRMWARE_STRING_LENGTH, fix_swapped_id);
-  
+  ata_format_id_string(model, drive->model, sizeof(model)-1);
+  ata_format_id_string(firmware, drive->fw_rev, sizeof(firmware)-1);
+
   // Look up the drive in knowndrives[].
   const drive_settings * dbentry = lookup_drive(model, firmware);
   if (!dbentry)
-    return false;
+    return 0;
 
   if (*dbentry->presets) {
     // Apply presets
-    if (!parse_presets(dbentry->presets, defs, fix_firmwarebug))
+    if (!parse_presets(dbentry->presets, defs, firmwarebugs))
       pout("Syntax error in preset option string \"%s\"\n", dbentry->presets);
   }
-  return true;
+  return dbentry;
 }
 
 
@@ -633,10 +767,19 @@ static bool parse_drive_database(parse_ptr src, drive_database & db, const char
             break;
           case 4:
             if (!token.value.empty()) {
-              ata_vendor_attr_defs defs; unsigned char fix = 0;
-              if (!parse_presets(token.value.c_str(), defs, fix)) {
-                pout("%s(%d): Syntax error in preset option string\n", path, token.line);
-                ok = false;
+              if (!is_usb_modelfamily(values[0].c_str())) {
+                ata_vendor_attr_defs defs; firmwarebug_defs fix;
+                if (!parse_presets(token.value.c_str(), defs, fix)) {
+                  pout("%s(%d): Syntax error in preset option string\n", path, token.line);
+                  ok = false;
+                }
+              }
+              else {
+                std::string type;
+                if (!parse_usb_type(token.value.c_str(), type)) {
+                  pout("%s(%d): Syntax error in USB type string\n", path, token.line);
+                  ok = false;
+                }
               }
             }
             break;
@@ -687,23 +830,45 @@ bool read_drive_database(const char * path)
   return parse_drive_database(parse_ptr(f), knowndrives, path);
 }
 
-// Read drive databases from standard places.
-bool read_default_drive_databases()
+// Get path for additional database file
+const char * get_drivedb_path_add()
 {
 #ifndef _WIN32
-  // Read file for local additions: /{,usr/local/}etc/smart_drivedb.h
-  static const char db1[] = SMARTMONTOOLS_SYSCONFDIR"/smart_drivedb.h";
+  return SMARTMONTOOLS_SYSCONFDIR"/smart_drivedb.h";
 #else
-  static const char db1[] = "./smart_drivedb.h";
+  static std::string path = get_exe_dir() + "/drivedb-add.h";
+  return path.c_str();
 #endif
+}
+
+#ifdef SMARTMONTOOLS_DRIVEDBDIR
+
+// Get path for default database file
+const char * get_drivedb_path_default()
+{
+#ifndef _WIN32
+  return SMARTMONTOOLS_DRIVEDBDIR"/drivedb.h";
+#else
+  static std::string path = get_exe_dir() + "/drivedb.h";
+  return path.c_str();
+#endif
+}
+
+#endif
+
+// Read drive databases from standard places.
+bool read_default_drive_databases()
+{
+  // Read file for local additions: /{,usr/local/}etc/smart_drivedb.h
+  const char * db1 = get_drivedb_path_add();
   if (!access(db1, 0)) {
     if (!read_drive_database(db1))
       return false;
   }
 
 #ifdef SMARTMONTOOLS_DRIVEDBDIR
-  // Read file from package: // /usr/{,local/}share/smartmontools/drivedb.h
-  static const char db2[] = SMARTMONTOOLS_DRIVEDBDIR"/drivedb.h";
+  // Read file from package: /usr/{,local/}share/smartmontools/drivedb.h
+  const char * db2 = get_drivedb_path_default();
   if (!access(db2, 0)) {
     if (!read_drive_database(db2))
       return false;