| 1 |
/* |
| 2 |
* Copyright (C) 2014-2024 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Tomas Bzatek <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <glib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <errno.h> |
| 25 |
|
| 26 |
#include "smart.h" |
| 27 |
#include "smart-private.h" |
| 28 |
|
| 29 |
|
| 30 |
void free_drivedb_attrs (DriveDBAttr **attrs) { |
| 31 |
DriveDBAttr **a; |
| 32 |
|
| 33 |
if (attrs == NULL) |
| 34 |
return; |
| 35 |
for (a = attrs; *a; a++) { |
| 36 |
g_free ((*a)->name); |
| 37 |
g_free (*a); |
| 38 |
} |
| 39 |
g_free (attrs); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
#ifndef HAVE_DRIVEDB_H |
| 44 |
DriveDBAttr** drivedb_lookup_drive (G_GNUC_UNUSED const gchar *model, G_GNUC_UNUSED const gchar *fw, G_GNUC_UNUSED gboolean include_defaults) { |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
#else |
| 48 |
|
| 49 |
struct drive_settings { |
| 50 |
const char* modelfamily; |
| 51 |
const char* modelregexp; |
| 52 |
const char* firmwareregexp; |
| 53 |
const char* warningmsg; |
| 54 |
const char* presets; |
| 55 |
}; |
| 56 |
|
| 57 |
static const struct drive_settings builtin_knowndrives[] = { |
| 58 |
#include <drivedb.h> |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
static gboolean parse_attribute_def (const char *arg, gint *attr_id, gchar **attr_name) { |
| 63 |
int format_str_len = 0; |
| 64 |
int attrname_str_len = 0; |
| 65 |
int hddssd_str_len = 0; |
| 66 |
char format_str[33] = {0,}; |
| 67 |
char attrname_str[33] = {0,}; |
| 68 |
char hddssd_str[4] = {0,}; |
| 69 |
|
| 70 |
if (arg[0] == 'N') |
| 71 |
/* ignoring "N,format[,name]" as it doesn't provide attribute ID */ |
| 72 |
return FALSE; |
| 73 |
|
| 74 |
/* parse "id,format[+][,name[,HDD|SSD]]" */ |
| 75 |
if (sscanf (arg, "%d,%32[^,]%n,%32[^,]%n,%3[DHS]%n", |
| 76 |
attr_id, |
| 77 |
format_str, &format_str_len, |
| 78 |
attrname_str, &attrname_str_len, |
| 79 |
hddssd_str, &hddssd_str_len) < 2) |
| 80 |
return FALSE; |
| 81 |
if (*attr_id < 1 || *attr_id > 255) |
| 82 |
return FALSE; |
| 83 |
if (attrname_str_len < 1) |
| 84 |
return FALSE; |
| 85 |
|
| 86 |
/* ignoring format_str */ |
| 87 |
*attr_name = g_strndup (attrname_str, attrname_str_len); |
| 88 |
/* ignoring hddssd_str */ |
| 89 |
|
| 90 |
return TRUE; |
| 91 |
} |
| 92 |
|
| 93 |
static void parse_presets_str (const char *presets, GHashTable *attrs) { |
| 94 |
while (TRUE) { |
| 95 |
char opt; |
| 96 |
char arg[94] = {0,}; |
| 97 |
int len = 0; |
| 98 |
gint attr_id = 0; |
| 99 |
gchar *attr_name = NULL; |
| 100 |
|
| 101 |
presets += strspn (presets, " \t"); |
| 102 |
if (presets[0] == '\0') |
| 103 |
break; |
| 104 |
if (sscanf (presets, "-%c %80[^ ]%n", &opt, arg, &len) < 2) |
| 105 |
break; |
| 106 |
if (len < 1) |
| 107 |
break; |
| 108 |
if (opt == 'v') { |
| 109 |
/* parse "-v N,format[,name[,HDD|SSD]]" */ |
| 110 |
if (parse_attribute_def (arg, &attr_id, &attr_name)) { |
| 111 |
g_hash_table_replace (attrs, GINT_TO_POINTER (attr_id), attr_name); |
| 112 |
} |
| 113 |
} |
| 114 |
/* ignoring other switches like 'F' and 'd' */ |
| 115 |
|
| 116 |
presets += len; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
DriveDBAttr** drivedb_lookup_drive (const gchar *model, const gchar *fw, gboolean include_defaults) { |
| 121 |
gulong i; |
| 122 |
GHashTable *attrs; |
| 123 |
DriveDBAttr **ret = NULL; |
| 124 |
|
| 125 |
if (G_N_ELEMENTS (builtin_knowndrives) == 0) |
| 126 |
return NULL; |
| 127 |
|
| 128 |
attrs = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free); |
| 129 |
|
| 130 |
/* first parse the DEFAULTS definitions */ |
| 131 |
if (include_defaults) |
| 132 |
for (i = 0; i < G_N_ELEMENTS (builtin_knowndrives); i++) |
| 133 |
if (builtin_knowndrives[i].modelfamily && |
| 134 |
builtin_knowndrives[i].presets && |
| 135 |
g_ascii_strncasecmp (builtin_knowndrives[i].modelfamily, "DEFAULT", 7) == 0) |
| 136 |
parse_presets_str (builtin_knowndrives[i].presets, attrs); |
| 137 |
|
| 138 |
/* now overlay/replace with drive specific keys */ |
| 139 |
for (i = 0; i < G_N_ELEMENTS (builtin_knowndrives); i++) { |
| 140 |
GRegex *regex; |
| 141 |
GError *error = NULL; |
| 142 |
gboolean match = FALSE; |
| 143 |
|
| 144 |
/* check the modelfamily string */ |
| 145 |
if (builtin_knowndrives[i].modelfamily == NULL || |
| 146 |
builtin_knowndrives[i].modelregexp == NULL || |
| 147 |
builtin_knowndrives[i].presets == NULL || strlen (builtin_knowndrives[i].presets) < 5 || |
| 148 |
g_ascii_strncasecmp (builtin_knowndrives[i].modelfamily, "VERSION", 7) == 0 || |
| 149 |
g_ascii_strncasecmp (builtin_knowndrives[i].modelfamily, "USB", 3) == 0 || |
| 150 |
g_ascii_strncasecmp (builtin_knowndrives[i].modelfamily, "DEFAULT", 7) == 0) |
| 151 |
continue; |
| 152 |
/* assuming modelfamily=ATA from now on... */ |
| 153 |
|
| 154 |
/* match the model string */ |
| 155 |
regex = g_regex_new (builtin_knowndrives[i].modelregexp, |
| 156 |
0, 0, &error); |
| 157 |
if (regex == NULL) { |
| 158 |
bd_utils_log_format (BD_UTILS_LOG_DEBUG, |
| 159 |
"drivedb-parser: regex compilation failed for '%s': %s", |
| 160 |
builtin_knowndrives[i].modelregexp, |
| 161 |
error->message); |
| 162 |
g_error_free (error); |
| 163 |
continue; |
| 164 |
} |
| 165 |
if (g_regex_match (regex, model, 0, NULL)) |
| 166 |
match = TRUE; |
| 167 |
g_regex_unref (regex); |
| 168 |
if (!match) |
| 169 |
continue; |
| 170 |
|
| 171 |
/* match the firmware string */ |
| 172 |
if (builtin_knowndrives[i].firmwareregexp && |
| 173 |
strlen (builtin_knowndrives[i].firmwareregexp) > 0 && |
| 174 |
fw && strlen (fw) > 0) |
| 175 |
{ |
| 176 |
regex = g_regex_new (builtin_knowndrives[i].firmwareregexp, |
| 177 |
0, 0, &error); |
| 178 |
if (regex == NULL) { |
| 179 |
bd_utils_log_format (BD_UTILS_LOG_DEBUG, |
| 180 |
"drivedb-parser: regex compilation failed for '%s': %s", |
| 181 |
builtin_knowndrives[i].firmwareregexp, |
| 182 |
error->message); |
| 183 |
g_error_free (error); |
| 184 |
continue; |
| 185 |
} |
| 186 |
if (!g_regex_match (regex, model, 0, NULL)) |
| 187 |
match = FALSE; |
| 188 |
g_regex_unref (regex); |
| 189 |
} |
| 190 |
|
| 191 |
if (match) |
| 192 |
parse_presets_str (builtin_knowndrives[i].presets, attrs); |
| 193 |
} |
| 194 |
|
| 195 |
if (g_hash_table_size (attrs) > 0) { |
| 196 |
GHashTableIter iter; |
| 197 |
gpointer key, val; |
| 198 |
|
| 199 |
/* convert to NULL-terminated array */ |
| 200 |
i = 0; |
| 201 |
ret = g_new0 (DriveDBAttr *, g_hash_table_size (attrs) + 1); |
| 202 |
g_hash_table_iter_init (&iter, attrs); |
| 203 |
while (g_hash_table_iter_next (&iter, &key, &val)) { |
| 204 |
DriveDBAttr *attr; |
| 205 |
|
| 206 |
attr = g_new0 (DriveDBAttr, 1); |
| 207 |
attr->id = GPOINTER_TO_INT (key); |
| 208 |
attr->name = g_strdup (val); |
| 209 |
ret[i++] = attr; |
| 210 |
} |
| 211 |
} |
| 212 |
g_hash_table_destroy (attrs); |
| 213 |
|
| 214 |
return ret; |
| 215 |
} |
| 216 |
#endif /* HAVE_DRIVEDB_H */ |