X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=knowndrives.cpp;h=050733e31b8223ea98b0c97ac192ac9ccc165640;hb=413ace18d106fef69fa96d53831eebd600c9f157;hp=1f2e73470de6889b53fcc4ef6a200315861a1bb4;hpb=4d561bdbb71727621a1890b24b1ec50e21da1fe9;p=mirror_smartmontools-debian.git diff --git a/knowndrives.cpp b/knowndrives.cpp index 1f2e734..050733e 100644 --- a/knowndrives.cpp +++ b/knowndrives.cpp @@ -1,11 +1,10 @@ /* * 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-11 Philip Williams, Bruce Allen - * Copyright (C) 2008-11 Christian Franke + * Copyright (C) 2008-16 Christian Franke * * 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 +12,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 . * */ @@ -34,7 +32,7 @@ #include -const char * knowndrives_cpp_cvsid = "$Id: knowndrives.cpp 3289 2011-03-09 19:52:04Z chrfranke $" +const char * knowndrives_cpp_cvsid = "$Id: knowndrives.cpp 4208 2016-01-22 19:45:35Z chrfranke $" KNOWNDRIVES_H_CVSID; #define MODEL_STRING_LENGTH 40 @@ -52,6 +50,8 @@ const drive_settings builtin_knowndrives[] = { #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 @@ -124,14 +124,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; } @@ -139,16 +141,26 @@ 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) +enum dbentry_type { + DBENTRY_ATA_DEFAULT, + DBENTRY_ATA, + DBENTRY_USB +}; + +// Return type of entry +static dbentry_type get_modelfamily_type(const char * modelfamily) { - return !strncmp(modelfamily, "USB:", 4); + 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; } -// Return true if entry for USB ID -static inline bool is_usb_entry(const drive_settings * dbentry) +static inline dbentry_type get_dbentry_type(const drive_settings * dbentry) { - return is_usb_modelfamily(dbentry->modelfamily); + return get_modelfamily_type(dbentry->modelfamily); } // Compile regular expression, print message on failure. @@ -184,8 +196,8 @@ static const drive_settings * lookup_drive(const char * model, const char * firm firmware = ""; for (unsigned i = 0; i < knowndrives.size(); i++) { - // Skip USB entries - if (is_usb_entry(&knowndrives[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]. @@ -208,33 +220,27 @@ static const drive_settings * lookup_drive(const char * model, const char * firm // 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, - unsigned char * fix_firmwarebug, std::string * type) + 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' && defs) { - // Parse "-v N,format[,name]" - if (!parse_attribute_def(arg, *defs, PRIOR_DATABASE)) + // Parse "-v N,format[,name[,HDD|SSD]]" + if (!parse_attribute_def(arg, *defs, (firmwarebugs ? PRIOR_DATABASE : PRIOR_DEFAULT))) return false; } - else if (opt == 'F' && fix_firmwarebug) { - 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 @@ -248,12 +254,19 @@ static bool parse_db_presets(const char * presets, ata_vendor_attr_defs * defs, 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, - unsigned char & fix_firmwarebug) + firmwarebug_defs & firmwarebugs) { - return parse_db_presets(presets, &defs, &fix_firmwarebug, 0); + return parse_db_presets(presets, &defs, &firmwarebugs, 0); } // Parse '-d' option in preset string, return false on error. @@ -292,7 +305,7 @@ int lookup_usb_device(int vendor_id, int product_id, int bcd_device, const drive_settings & dbentry = knowndrives[i]; // Skip drive entries - if (!is_usb_entry(&dbentry)) + if (get_dbentry_type(&dbentry) != DBENTRY_USB) continue; // Check whether USB vendor:product ID matches @@ -346,7 +359,8 @@ static int showonepreset(const drive_settings * dbentry) return 1; } - bool usb = is_usb_entry(dbentry); + dbentry_type type = get_dbentry_type(dbentry); + bool usb = (type == DBENTRY_USB); // print and check model and firmware regular expressions int errcnt = 0; @@ -365,19 +379,35 @@ static int showonepreset(const drive_settings * dbentry) pout("%-*s %s\n", TABLEPRINTWIDTH, "MODEL FAMILY:", dbentry->modelfamily); // if there are any presets, then show them - unsigned char fix_firmwarebug = 0; + firmwarebug_defs firmwarebugs; 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 (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++; + } } + for (int i = 0; i < MAX_ATTRIBUTE_NUM; i++) { - if (defs[i].priority != PRIOR_DEFAULT) { + 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, ata_get_smart_attr_name(i, defs).c_str()); + 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; } } @@ -386,18 +416,26 @@ static int showonepreset(const drive_settings * dbentry) pout("%-*s %s\n", TABLEPRINTWIDTH, "ATTRIBUTE OPTIONS:", "None preset; no -v options are required."); // describe firmwarefix - if (fix_firmwarebug) { + for (int b = BUG_NOLOGDIR; b <= BUG_XERRORLBA; b++) { + if (!firmwarebugs.is_set((firmwarebug_t)b)) + continue; const char * fixdesc; - switch (fix_firmwarebug) { - case FIX_SAMSUNG: + 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 FIX_SAMSUNG2: + case BUG_SAMSUNG2: fixdesc = "Fixes byte order in some SMART data (same as -F samsung2)"; break; - case FIX_SAMSUNG3: + 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; @@ -518,12 +556,12 @@ void show_presets(const ata_identify_device * drive) } // Searches drive database and sets preset vendor attribute -// options in defs and fix_firmwarebug. +// 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, - unsigned char & fix_firmwarebug) + firmwarebug_defs & firmwarebugs) { // get the drive's model/firmware strings char model[MODEL_STRING_LENGTH+1], firmware[FIRMWARE_STRING_LENGTH+1]; @@ -537,7 +575,7 @@ const drive_settings * lookup_drive_apply_presets( 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 dbentry; @@ -553,7 +591,7 @@ class stdin_iterator { 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; } @@ -757,19 +795,29 @@ static bool parse_drive_database(parse_ptr src, drive_database & db, const char break; case 4: if (!token.value.empty()) { - if (!is_usb_modelfamily(values[0].c_str())) { - 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; - } - } - 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; - } + // 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; @@ -847,7 +895,7 @@ const char * get_drivedb_path_default() #endif // Read drive databases from standard places. -bool read_default_drive_databases() +static bool read_default_drive_databases() { // Read file for local additions: /{,usr/local/}etc/smart_drivedb.h const char * db1 = get_drivedb_path_add(); @@ -867,9 +915,60 @@ bool read_default_drive_databases() #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; +}