/*
* knowndrives.cpp
*
- * Home page of code is: http://smartmontools.sourceforge.net
- * Address of support mailing list: smartmontools-support@lists.sourceforge.net
+ * Home page of code is: http://www.smartmontools.org
*
- * Copyright (C) 2003-9 Philip Williams, Bruce Allen
- * Copyright (C) 2008-9 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
- * 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.
+ * Copyright (C) 2003-11 Philip Williams, Bruce Allen
+ * Copyright (C) 2008-18 Christian Franke
*
+ * SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
-#include "int64.h"
+
#include <stdio.h>
#include "atacmds.h"
-#include "extern.h"
#include "knowndrives.h"
#include "utility.h"
#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 4842 2018-12-02 16:07:26Z chrfranke $"
KNOWNDRIVES_H_CVSID;
#define MODEL_STRING_LENGTH 40
#include "drivedb.h"
};
+const unsigned builtin_knowndrives_size =
+ sizeof(builtin_knowndrives) / sizeof(builtin_knowndrives[0]);
/// Drive database class. Stores custom entries read from file.
/// Provides transparent access to concatenation of custom and
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;
}
static drive_database knowndrives;
+enum dbentry_type {
+ DBENTRY_ATA_DEFAULT,
+ DBENTRY_ATA,
+ DBENTRY_USB
+};
+
+// Return type of entry
+static dbentry_type get_modelfamily_type(const char * modelfamily)
+{
+ if (modelfamily[0] == 'D' && !strcmp(modelfamily, "DEFAULT"))
+ return DBENTRY_ATA_DEFAULT;
+ else if(modelfamily[0] == 'U' && str_starts_with(modelfamily, "USB:"))
+ return DBENTRY_USB;
+ else
+ return DBENTRY_ATA;
+}
+
+static inline dbentry_type get_dbentry_type(const drive_settings * dbentry)
+{
+ return get_modelfamily_type(dbentry->modelfamily);
+}
+
// Compile regular expression, print message on failure.
static bool compile(regular_expression & regex, const char *pattern)
{
- if (!regex.compile(pattern, REG_EXTENDED)) {
+ if (!regex.compile(pattern)) {
pout("Internal error: unable to compile regular expression \"%s\": %s\n"
"Please inform smartmontools developers at " PACKAGE_BUGREPORT "\n",
pattern, regex.get_errmsg());
// 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 = "";
firmware = "";
for (unsigned i = 0; i < knowndrives.size(); i++) {
+ // Skip DEFAULT and USB entries
+ if (get_dbentry_type(&knowndrives[i]) != DBENTRY_ATA)
+ continue;
+
// Check whether model matches the regular expression in knowndrives[i].
if (!match(knowndrives[i].modelregexp, model))
continue;
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') {
- // Parse "-v N,format[,name]"
- if (!parse_attribute_def(arg, defs, PRIOR_DATABASE))
+ if (opt == 'v' && defs) {
+ // Parse "-v N,format[,name[,HDD|SSD]]"
+ if (!parse_attribute_def(arg, *defs, (firmwarebugs ? PRIOR_DATABASE : PRIOR_DEFAULT)))
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;
return true;
}
+// Parse '-v' options in default preset string, return false on error.
+static inline bool parse_default_presets(const char * presets,
+ ata_vendor_attr_defs & defs)
+{
+ return parse_db_presets(presets, &defs, 0, 0);
+}
+
+// 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 (get_dbentry_type(&dbentry) != DBENTRY_USB)
+ 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)
{
"this error to smartmontools developers at " PACKAGE_BUGREPORT ".\n");
return 1;
}
-
+
+ dbentry_type type = get_dbentry_type(dbentry);
+ bool usb = (type == DBENTRY_USB);
+
// 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 (!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 (type == DBENTRY_ATA_DEFAULT) {
+ if (!parse_default_presets(dbentry->presets, defs)) {
+ pout("Syntax error in DEFAULT option string \"%s\"\n", dbentry->presets);
+ errcnt++;
+ }
+ }
+ else {
+ if (!parse_presets(dbentry->presets, defs, firmwarebugs)) {
+ pout("Syntax error in preset option string \"%s\"\n", dbentry->presets);
+ errcnt++;
+ }
+ }
- // 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++;
+ for (int i = 0; i < MAX_ATTRIBUTE_NUM; i++) {
+ if (defs[i].priority != PRIOR_DEFAULT || !defs[i].name.empty()) {
+ 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
}
// 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) {
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;
}
{
public:
explicit stdin_iterator(FILE * f)
- : m_f(f) { get(); get(); }
+ : m_f(f), m_next(0) { get(); get(); }
stdin_iterator & operator++()
{ get(); return *this; }
}
token.value += c;
}
- // Lookahead to detect string constant concatentation
+ // Lookahead to detect string constant concatenation
src = skip_white(++src, path, line);
} while (*src == '"');
break;
case 1: case 2:
if (!token.value.empty()) {
regular_expression regex;
- if (!regex.compile(token.value.c_str(), REG_EXTENDED)) {
+ if (!regex.compile(token.value.c_str())) {
pout("%s(%d): Error in regular expression: %s\n", path, token.line, regex.get_errmsg());
ok = false;
}
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;
+ // Syntax check
+ switch (get_modelfamily_type(values[0].c_str())) {
+ case DBENTRY_ATA_DEFAULT: {
+ ata_vendor_attr_defs defs;
+ if (!parse_default_presets(token.value.c_str(), defs)) {
+ pout("%s(%d): Syntax error in DEFAULT option string\n", path, token.line);
+ ok = false;
+ }
+ } break;
+ default: { // DBENTRY_ATA
+ 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;
+ }
+ } break;
+ case DBENTRY_USB: {
+ 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;
}
}
break;
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 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 const char db1[] = "./smart_drivedb.h";
+ static std::string path = get_exe_dir() + "/drivedb.h";
+ return path.c_str();
#endif
+}
+
+#endif
+
+// Read drive databases from standard places.
+static 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;
#endif
{
// Append builtin table.
- knowndrives.append(builtin_knowndrives,
- sizeof(builtin_knowndrives)/sizeof(builtin_knowndrives[0]));
+ knowndrives.append(builtin_knowndrives, builtin_knowndrives_size);
+ }
+
+ return true;
+}
+
+static ata_vendor_attr_defs default_attr_defs;
+
+// Initialize default_attr_defs.
+static bool init_default_attr_defs()
+{
+ // Lookup default entry
+ const drive_settings * entry = 0;
+ for (unsigned i = 0; i < knowndrives.size(); i++) {
+ if (get_dbentry_type(&knowndrives[i]) != DBENTRY_ATA_DEFAULT)
+ continue;
+ entry = &knowndrives[i];
+ break;
+ }
+
+ if (!entry) {
+ // Fall back to builtin database
+ for (unsigned i = 0; i < builtin_knowndrives_size; i++) {
+ if (get_dbentry_type(&builtin_knowndrives[i]) != DBENTRY_ATA_DEFAULT)
+ continue;
+ entry = &builtin_knowndrives[i];
+ break;
+ }
+
+ if (!entry)
+ throw std::logic_error("DEFAULT entry missing in builtin drive database");
+
+ pout("Warning: DEFAULT entry missing in drive database file(s)\n");
+ }
+
+ if (!parse_default_presets(entry->presets, default_attr_defs)) {
+ pout("Syntax error in DEFAULT drive database entry\n");
+ return false;
}
return true;
}
+
+// Init default db entry and optionally read drive databases from standard places.
+bool init_drive_database(bool use_default_db)
+{
+ if (use_default_db && !read_default_drive_databases())
+ return false;
+
+ return init_default_attr_defs();
+}
+
+// Get vendor attribute options from default db entry.
+const ata_vendor_attr_defs & get_default_attr_defs()
+{
+ return default_attr_defs;
+}