/src/fwupd/libfwupdplugin/fu-quirks.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2017 Richard Hughes <richard@hughsie.com> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | 0 | #define G_LOG_DOMAIN "FuQuirks" |
8 | | |
9 | | #include "config.h" |
10 | | |
11 | | #include <gio/gio.h> |
12 | | #include <string.h> |
13 | | |
14 | | #ifdef HAVE_SQLITE |
15 | | #include <sqlite3.h> |
16 | | #endif |
17 | | |
18 | | #include "fwupd-common.h" |
19 | | #include "fwupd-enums-private.h" |
20 | | #include "fwupd-error.h" |
21 | | |
22 | | #include "fu-context-private.h" |
23 | | #include "fu-input-stream.h" |
24 | | #include "fu-path-store.h" |
25 | | #include "fu-path.h" |
26 | | #include "fu-quirks.h" |
27 | | #include "fu-string.h" |
28 | | |
29 | | /** |
30 | | * FuQuirks: |
31 | | * |
32 | | * Quirks can be used to modify device behavior. |
33 | | * When fwupd is installed in long-term support distros it's very hard to |
34 | | * backport new versions as new hardware is released. |
35 | | * |
36 | | * There are several reasons why we can't just include the mapping and quirk |
37 | | * information in the AppStream metadata: |
38 | | * |
39 | | * * The extra data is hugely specific to the installed fwupd plugin versions |
40 | | * * The device-id is per-device, and the mapping is usually per-plugin |
41 | | * * Often the information is needed before the FuDevice is created |
42 | | * * There are security implications in allowing plugins to handle new devices |
43 | | * |
44 | | * The idea with quirks is that the end user can drop an additional (or replace |
45 | | * an existing) file in a .d director with a simple format and the hardware will |
46 | | * magically start working. This assumes no new quirks are required, as this would |
47 | | * obviously need code changes, but allows us to get most existing devices working |
48 | | * in an easy way without the user compiling anything. |
49 | | * |
50 | | * Plugins may add support for additional quirks that are relevant only for those plugins, |
51 | | * and should be documented in the per-plugin `README.md` files. |
52 | | * |
53 | | * You can add quirk files in `/usr/share/fwupd/quirks.d` or `/var/lib/fwupd/quirks.d/`. |
54 | | * |
55 | | * Here is an example as seen in the CSR plugin: |
56 | | * |
57 | | * |[ |
58 | | * [USB\VID_0A12&PID_1337] |
59 | | * Plugin = dfu_csr |
60 | | * Name = H05 |
61 | | * Summary = Bluetooth Headphones |
62 | | * Icon = audio-headphones |
63 | | * Vendor = AIAIAI |
64 | | * [USB\VID_0A12&PID_1337&REV_2520] |
65 | | * Version = 1.2 |
66 | | * ]| |
67 | | * |
68 | | * See also: [class@FuDevice], [class@FuPlugin] |
69 | | */ |
70 | | |
71 | | static void |
72 | | fu_quirks_finalize(GObject *obj); |
73 | | |
74 | | struct _FuQuirks { |
75 | | GObject parent_instance; |
76 | | FuContext *ctx; |
77 | | GHashTable *possible_keys; |
78 | | GPtrArray *invalid_keys; |
79 | | XbSilo *silo; |
80 | | XbQuery *query_kv; |
81 | | XbQuery *query_vs; |
82 | | gboolean verbose; |
83 | | gboolean loaded; |
84 | | GHashTable *cache; /* of utf8:GPtrArray */ |
85 | | #ifdef HAVE_SQLITE |
86 | | sqlite3 *db; |
87 | | #endif |
88 | | }; |
89 | | |
90 | 0 | G_DEFINE_TYPE(FuQuirks, fu_quirks, G_TYPE_OBJECT) |
91 | 0 |
|
92 | 0 | #ifdef HAVE_SQLITE |
93 | 0 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(sqlite3_stmt, sqlite3_finalize); |
94 | 0 | #endif |
95 | 0 |
|
96 | 0 | static gchar * |
97 | 0 | fu_quirks_build_group_key(const gchar *group) |
98 | 0 | { |
99 | 0 | if (fwupd_guid_is_valid(group)) |
100 | 0 | return g_strdup(group); |
101 | 0 | return fwupd_guid_hash_string(group); |
102 | 0 | } |
103 | | |
104 | | static gboolean |
105 | | fu_quirks_validate_flags(const gchar *value, GError **error) |
106 | 0 | { |
107 | 0 | g_return_val_if_fail(value != NULL, FALSE); |
108 | 0 | for (gsize i = 0; value[i] != '\0'; i++) { |
109 | 0 | gchar tmp = value[i]; |
110 | | |
111 | | /* allowed special chars */ |
112 | 0 | if (tmp == ',' || tmp == '~' || tmp == '-') |
113 | 0 | continue; |
114 | 0 | if (!g_ascii_isalnum(tmp)) { |
115 | 0 | g_set_error(error, |
116 | 0 | FWUPD_ERROR, |
117 | 0 | FWUPD_ERROR_INVALID_DATA, |
118 | 0 | "%c is not alphanumeric", |
119 | 0 | tmp); |
120 | 0 | return FALSE; |
121 | 0 | } |
122 | 0 | if (g_ascii_isalpha(tmp) && !g_ascii_islower(tmp)) { |
123 | 0 | g_set_error(error, |
124 | 0 | FWUPD_ERROR, |
125 | 0 | FWUPD_ERROR_INVALID_DATA, |
126 | 0 | "%c is not lowercase", |
127 | 0 | tmp); |
128 | 0 | return FALSE; |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | | /* success */ |
133 | 0 | return TRUE; |
134 | 0 | } |
135 | | |
136 | | typedef struct { |
137 | | GString *group; |
138 | | XbBuilderNode *bn; |
139 | | XbBuilderNode *root; |
140 | | } FuQuirksConvertHelper; |
141 | | |
142 | | static void |
143 | | fu_quirks_convert_helper_free(FuQuirksConvertHelper *helper) |
144 | 0 | { |
145 | 0 | g_string_free(helper->group, TRUE); |
146 | 0 | g_object_unref(helper->root); |
147 | 0 | if (helper->bn != NULL) |
148 | 0 | g_object_unref(helper->bn); |
149 | 0 | g_free(helper); |
150 | 0 | } |
151 | | |
152 | | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuQuirksConvertHelper, fu_quirks_convert_helper_free) |
153 | | |
154 | | static gboolean |
155 | | fu_quirks_convert_keyfile_to_xml_cb(GString *token, |
156 | | guint token_idx, |
157 | | gpointer user_data, |
158 | | GError **error) |
159 | 0 | { |
160 | 0 | FuQuirksConvertHelper *helper = (FuQuirksConvertHelper *)user_data; |
161 | 0 | g_autofree gchar *key = NULL; |
162 | 0 | g_autofree gchar *value = NULL; |
163 | 0 | g_auto(GStrv) kv = NULL; |
164 | | |
165 | | /* blank line */ |
166 | 0 | if (token->len == 0) |
167 | 0 | return TRUE; |
168 | | |
169 | | /* comment */ |
170 | 0 | if (token->str[0] == '#') |
171 | 0 | return TRUE; |
172 | | |
173 | | /* neither a key=value or [group] */ |
174 | 0 | if (token->len < 3) { |
175 | 0 | g_set_error(error, |
176 | 0 | FWUPD_ERROR, |
177 | 0 | FWUPD_ERROR_INVALID_DATA, |
178 | 0 | "invalid line: %s", |
179 | 0 | token->str); |
180 | 0 | return FALSE; |
181 | 0 | } |
182 | | |
183 | | /* a group */ |
184 | 0 | if (token->str[0] == '[' && token->str[token->len - 1] == ']') { |
185 | 0 | g_autofree gchar *group_id = NULL; |
186 | 0 | g_autofree gchar *group_tmp = NULL; |
187 | 0 | g_autoptr(XbBuilderNode) bn_tmp = NULL; |
188 | | |
189 | | /* trim off the [] and convert to a GUID */ |
190 | 0 | group_tmp = g_strndup(token->str + 1, token->len - 2); |
191 | 0 | group_id = fu_quirks_build_group_key(group_tmp); |
192 | 0 | bn_tmp = xb_builder_node_insert(helper->root, "device", "id", group_id, NULL); |
193 | 0 | g_set_object(&helper->bn, bn_tmp); |
194 | 0 | g_string_assign(helper->group, group_tmp); |
195 | 0 | return TRUE; |
196 | 0 | } |
197 | | |
198 | | /* no current group */ |
199 | 0 | if (helper->bn == NULL) { |
200 | 0 | g_set_error(error, |
201 | 0 | FWUPD_ERROR, |
202 | 0 | FWUPD_ERROR_INVALID_DATA, |
203 | 0 | "invalid line when group unset: %s", |
204 | 0 | token->str); |
205 | 0 | return FALSE; |
206 | 0 | } |
207 | | |
208 | | /* parse as key=value */ |
209 | 0 | kv = g_strsplit(token->str, "=", 2); |
210 | 0 | if (kv[1] == NULL) { |
211 | 0 | g_set_error(error, |
212 | 0 | FWUPD_ERROR, |
213 | 0 | FWUPD_ERROR_INVALID_DATA, |
214 | 0 | "invalid line: not key=value: %s", |
215 | 0 | token->str); |
216 | 0 | return FALSE; |
217 | 0 | } |
218 | | |
219 | | /* sanity check flags */ |
220 | 0 | key = fu_strstrip(kv[0]); |
221 | 0 | value = fu_strstrip(kv[1]); |
222 | 0 | if (g_strcmp0(key, FU_QUIRKS_FLAGS) == 0) { |
223 | 0 | g_autoptr(GError) error_local = NULL; |
224 | 0 | if (!fu_quirks_validate_flags(value, &error_local)) { |
225 | 0 | g_warning("[%s] %s = %s is invalid: %s", |
226 | 0 | helper->group->str, |
227 | 0 | key, |
228 | 0 | value, |
229 | 0 | error_local->message); |
230 | 0 | } |
231 | 0 | } |
232 | | |
233 | | /* add */ |
234 | 0 | xb_builder_node_insert_text(helper->bn, "value", value, "key", key, NULL); |
235 | 0 | return TRUE; |
236 | 0 | } |
237 | | |
238 | | static GBytes * |
239 | | fu_quirks_convert_keyfile_to_xml(FuQuirks *self, GBytes *bytes, GError **error) |
240 | 0 | { |
241 | 0 | gsize xmlsz; |
242 | 0 | g_autofree gchar *xml = NULL; |
243 | 0 | g_autoptr(FuQuirksConvertHelper) helper = g_new0(FuQuirksConvertHelper, 1); |
244 | | |
245 | | /* split into lines */ |
246 | 0 | helper->root = xb_builder_node_new("quirk"); |
247 | 0 | helper->group = g_string_new(NULL); |
248 | 0 | if (g_bytes_get_size(bytes) > 0 && |
249 | 0 | !fu_strsplit_full((const gchar *)g_bytes_get_data(bytes, NULL), |
250 | 0 | g_bytes_get_size(bytes), |
251 | 0 | "\n", |
252 | 0 | fu_quirks_convert_keyfile_to_xml_cb, |
253 | 0 | helper, |
254 | 0 | error)) |
255 | 0 | return NULL; |
256 | | |
257 | | /* export as XML blob */ |
258 | 0 | xml = xb_builder_node_export(helper->root, XB_NODE_EXPORT_FLAG_ADD_HEADER, error); |
259 | 0 | if (xml == NULL) |
260 | 0 | return NULL; |
261 | 0 | xmlsz = strlen(xml); |
262 | 0 | return g_bytes_new_take(g_steal_pointer(&xml), xmlsz); |
263 | 0 | } |
264 | | |
265 | | static GInputStream * |
266 | | fu_quirks_convert_quirk_to_xml_cb(XbBuilderSource *source, |
267 | | XbBuilderSourceCtx *ctx, |
268 | | gpointer user_data, |
269 | | GCancellable *cancellable, |
270 | | GError **error) |
271 | 0 | { |
272 | 0 | FuQuirks *self = FU_QUIRKS(user_data); |
273 | 0 | g_autoptr(GBytes) bytes = NULL; |
274 | 0 | g_autoptr(GBytes) bytes_xml = NULL; |
275 | 0 | g_autoptr(FwupdMemoryInputStream) stream = NULL; |
276 | |
|
277 | 0 | bytes = xb_builder_source_ctx_get_bytes(ctx, cancellable, error); |
278 | 0 | if (bytes == NULL) |
279 | 0 | return NULL; |
280 | 0 | bytes_xml = fu_quirks_convert_keyfile_to_xml(self, bytes, error); |
281 | 0 | if (bytes_xml == NULL) |
282 | 0 | return NULL; |
283 | 0 | stream = fwupd_memory_input_stream_new_from_bytes(bytes_xml); |
284 | 0 | return fu_input_stream_as_g_input_stream(FWUPD_INPUT_STREAM(stream)); |
285 | 0 | } |
286 | | |
287 | | static gint |
288 | | fu_quirks_filename_sort_cb(gconstpointer a, gconstpointer b) |
289 | 0 | { |
290 | 0 | const gchar *stra = *((const gchar **)a); |
291 | 0 | const gchar *strb = *((const gchar **)b); |
292 | 0 | return g_strcmp0(stra, strb); |
293 | 0 | } |
294 | | |
295 | | static gboolean |
296 | | fu_quirks_add_quirks_for_path(FuQuirks *self, XbBuilder *builder, const gchar *path, GError **error) |
297 | 0 | { |
298 | 0 | const gchar *tmp; |
299 | 0 | g_autoptr(GDir) dir = NULL; |
300 | 0 | g_autoptr(GPtrArray) filenames = g_ptr_array_new_with_free_func(g_free); |
301 | |
|
302 | 0 | g_info("loading quirks from %s", path); |
303 | | |
304 | | /* add valid files to the array */ |
305 | 0 | if (!g_file_test(path, G_FILE_TEST_EXISTS)) |
306 | 0 | return TRUE; |
307 | 0 | dir = g_dir_open(path, 0, error); |
308 | 0 | if (dir == NULL) |
309 | 0 | return FALSE; |
310 | 0 | while ((tmp = g_dir_read_name(dir)) != NULL) { |
311 | 0 | if (!g_str_has_suffix(tmp, ".quirk") && !g_str_has_suffix(tmp, ".quirk.gz")) { |
312 | 0 | g_debug("skipping invalid file %s", tmp); |
313 | 0 | continue; |
314 | 0 | } |
315 | 0 | g_ptr_array_add(filenames, g_build_filename(path, tmp, NULL)); |
316 | 0 | } |
317 | | |
318 | | /* sort */ |
319 | 0 | g_ptr_array_sort(filenames, fu_quirks_filename_sort_cb); |
320 | | |
321 | | /* process files */ |
322 | 0 | for (guint i = 0; i < filenames->len; i++) { |
323 | 0 | const gchar *filename = g_ptr_array_index(filenames, i); |
324 | 0 | g_autoptr(GFile) file = g_file_new_for_path(filename); |
325 | 0 | g_autoptr(XbBuilderSource) source = xb_builder_source_new(); |
326 | | |
327 | | /* load from keyfile */ |
328 | 0 | xb_builder_source_add_simple_adapter(source, |
329 | 0 | "text/plain,application/octet-stream,.quirk", |
330 | 0 | fu_quirks_convert_quirk_to_xml_cb, |
331 | 0 | self, |
332 | 0 | NULL); |
333 | 0 | if (!xb_builder_source_load_file(source, |
334 | 0 | file, |
335 | 0 | XB_BUILDER_SOURCE_FLAG_WATCH_FILE | |
336 | 0 | XB_BUILDER_SOURCE_FLAG_LITERAL_TEXT, |
337 | 0 | NULL, |
338 | 0 | error)) { |
339 | 0 | g_prefix_error(error, "failed to load %s: ", filename); |
340 | 0 | fwupd_error_convert(error); |
341 | 0 | return FALSE; |
342 | 0 | } |
343 | | |
344 | | /* watch the file for changes */ |
345 | 0 | xb_builder_import_source(builder, source); |
346 | 0 | } |
347 | | |
348 | | /* success */ |
349 | 0 | return TRUE; |
350 | 0 | } |
351 | | |
352 | | static gint |
353 | | fu_quirks_strcasecmp_cb(gconstpointer a, gconstpointer b) |
354 | 0 | { |
355 | 0 | const gchar *entry1 = *((const gchar **)a); |
356 | 0 | const gchar *entry2 = *((const gchar **)b); |
357 | 0 | return g_ascii_strcasecmp(entry1, entry2); |
358 | 0 | } |
359 | | |
360 | | static gboolean |
361 | | fu_quirks_check_silo(FuQuirks *self, GError **error) |
362 | 0 | { |
363 | 0 | XbBuilderCompileFlags compile_flags = XB_BUILDER_COMPILE_FLAG_WATCH_BLOB; |
364 | 0 | const gchar *datadir; |
365 | 0 | const gchar *localstatedir = NULL; |
366 | 0 | g_autoptr(GFile) file = NULL; |
367 | 0 | g_autoptr(XbBuilder) builder = NULL; |
368 | 0 | g_autoptr(XbNode) n_any = NULL; |
369 | | |
370 | | /* everything is okay */ |
371 | 0 | if (self->silo != NULL && xb_silo_is_valid(self->silo)) |
372 | 0 | return TRUE; |
373 | | |
374 | | /* no longer valid */ |
375 | 0 | g_hash_table_remove_all(self->cache); |
376 | | |
377 | | /* system datadir */ |
378 | 0 | builder = xb_builder_new(); |
379 | 0 | datadir = fu_context_get_path(self->ctx, FU_PATH_KIND_DATADIR_QUIRKS, NULL); |
380 | 0 | if (datadir != NULL) { |
381 | 0 | if (!fu_quirks_add_quirks_for_path(self, builder, datadir, error)) |
382 | 0 | return FALSE; |
383 | 0 | } |
384 | | |
385 | | /* something we can write when using Ostree */ |
386 | 0 | localstatedir = fu_context_get_path(self->ctx, FU_PATH_KIND_LOCALSTATEDIR_QUIRKS, NULL); |
387 | 0 | if (localstatedir != NULL) { |
388 | 0 | if (!fu_quirks_add_quirks_for_path(self, builder, localstatedir, error)) |
389 | 0 | return FALSE; |
390 | 0 | } |
391 | | |
392 | | /* load silo */ |
393 | 0 | if (fu_context_has_flag(self->ctx, FU_CONTEXT_FLAG_NO_CACHE)) { |
394 | 0 | g_autoptr(GFileIOStream) iostr = NULL; |
395 | 0 | file = g_file_new_tmp(NULL, &iostr, error); |
396 | 0 | if (file == NULL) |
397 | 0 | return FALSE; |
398 | 0 | } else { |
399 | 0 | g_autofree gchar *xmlbfn = NULL; |
400 | 0 | xmlbfn = fu_context_build_filename(self->ctx, |
401 | 0 | error, |
402 | 0 | FU_PATH_KIND_CACHEDIR_PKG, |
403 | 0 | "quirks.xmlb", |
404 | 0 | NULL); |
405 | 0 | if (xmlbfn == NULL) |
406 | 0 | return FALSE; |
407 | 0 | file = g_file_new_for_path(xmlbfn); |
408 | 0 | } |
409 | 0 | if (g_getenv("FWUPD_XMLB_VERBOSE") != NULL) { |
410 | 0 | xb_builder_set_profile_flags(builder, |
411 | 0 | XB_SILO_PROFILE_FLAG_XPATH | |
412 | 0 | XB_SILO_PROFILE_FLAG_DEBUG); |
413 | 0 | } |
414 | 0 | if (fu_context_has_flag(self->ctx, FU_CONTEXT_FLAG_READONLY_FS)) |
415 | 0 | compile_flags |= XB_BUILDER_COMPILE_FLAG_IGNORE_GUID; |
416 | 0 | self->silo = xb_builder_ensure(builder, file, compile_flags, NULL, error); |
417 | 0 | if (self->silo == NULL) |
418 | 0 | return FALSE; |
419 | | |
420 | | /* dump warnings to console, just once */ |
421 | 0 | if (self->invalid_keys->len > 0) { |
422 | 0 | g_autofree gchar *str = NULL; |
423 | 0 | g_ptr_array_sort(self->invalid_keys, fu_quirks_strcasecmp_cb); |
424 | 0 | str = fu_strjoin(",", self->invalid_keys); |
425 | 0 | g_info("invalid key names: %s", str); |
426 | 0 | } |
427 | | |
428 | | /* check if there is any quirk data to load, as older libxmlb versions will not be able to |
429 | | * create the prepared query with an unknown text ID */ |
430 | 0 | n_any = xb_silo_query_first(self->silo, "quirk", NULL); |
431 | 0 | if (n_any == NULL) { |
432 | 0 | g_debug("no quirk data, not creating prepared queries"); |
433 | 0 | return TRUE; |
434 | 0 | } |
435 | | |
436 | | /* create prepared queries to save time later */ |
437 | 0 | self->query_kv = xb_query_new_full(self->silo, |
438 | 0 | "quirk/device[@id=?]/value[@key=?]", |
439 | 0 | XB_QUERY_FLAG_OPTIMIZE, |
440 | 0 | error); |
441 | 0 | if (self->query_kv == NULL) { |
442 | 0 | g_prefix_error_literal(error, "failed to prepare query: "); |
443 | 0 | return FALSE; |
444 | 0 | } |
445 | 0 | self->query_vs = xb_query_new_full(self->silo, |
446 | 0 | "quirk/device[@id=?]/value", |
447 | 0 | XB_QUERY_FLAG_OPTIMIZE, |
448 | 0 | error); |
449 | 0 | if (self->query_vs == NULL) { |
450 | 0 | g_prefix_error_literal(error, "failed to prepare query: "); |
451 | 0 | return FALSE; |
452 | 0 | } |
453 | 0 | if (!xb_silo_query_build_index(self->silo, "quirk/device", "id", error)) { |
454 | 0 | fwupd_error_convert(error); |
455 | 0 | return FALSE; |
456 | 0 | } |
457 | 0 | if (!xb_silo_query_build_index(self->silo, "quirk/device/value", "key", error)) { |
458 | 0 | fwupd_error_convert(error); |
459 | 0 | return FALSE; |
460 | 0 | } |
461 | | |
462 | | /* success */ |
463 | 0 | return TRUE; |
464 | 0 | } |
465 | | |
466 | | /** |
467 | | * fu_quirks_lookup_by_id: |
468 | | * @self: a #FuQuirks |
469 | | * @guid: GUID to lookup |
470 | | * @key: an ID to match the entry, e.g. `Name` |
471 | | * |
472 | | * Looks up an entry in the hardware database using a string value. |
473 | | * |
474 | | * Returns: (transfer none): values from the database, or %NULL if not found |
475 | | **/ |
476 | | const gchar * |
477 | | fu_quirks_lookup_by_id(FuQuirks *self, const gchar *guid, const gchar *key) |
478 | 0 | { |
479 | 0 | g_autoptr(GError) error = NULL; |
480 | 0 | g_autoptr(XbNode) n = NULL; |
481 | 0 | g_auto(XbQueryContext) context = XB_QUERY_CONTEXT_INIT(); |
482 | |
|
483 | 0 | g_return_val_if_fail(FU_IS_QUIRKS(self), NULL); |
484 | 0 | g_return_val_if_fail(self->loaded, NULL); |
485 | 0 | g_return_val_if_fail(guid != NULL, NULL); |
486 | 0 | g_return_val_if_fail(key != NULL, NULL); |
487 | | |
488 | | #ifdef HAVE_SQLITE |
489 | | /* this is generated from usb.ids and other static sources */ |
490 | | if (self->db != NULL && !fu_context_has_flag(self->ctx, FU_CONTEXT_FLAG_NO_CACHE)) { |
491 | | g_autoptr(sqlite3_stmt) stmt = NULL; |
492 | | if (sqlite3_prepare_v2(self->db, |
493 | | "SELECT key, value FROM quirks WHERE guid = ?1 " |
494 | | "AND key = ?2 LIMIT 1", |
495 | | -1, |
496 | | &stmt, |
497 | | NULL) != SQLITE_OK) { |
498 | | g_warning("failed to prepare SQL: %s", sqlite3_errmsg(self->db)); |
499 | | return NULL; |
500 | | } |
501 | | sqlite3_bind_text(stmt, 1, guid, -1, SQLITE_STATIC); |
502 | | sqlite3_bind_text(stmt, 2, key, -1, SQLITE_STATIC); |
503 | | if (sqlite3_step(stmt) == SQLITE_ROW) { |
504 | | const gchar *value = (const gchar *)sqlite3_column_text(stmt, 1); |
505 | | if (value != NULL) |
506 | | return g_intern_string(value); |
507 | | } |
508 | | } |
509 | | #endif |
510 | | |
511 | | /* ensure up to date */ |
512 | 0 | if (!fu_quirks_check_silo(self, &error)) { |
513 | 0 | g_warning("failed to build silo: %s", error->message); |
514 | 0 | return NULL; |
515 | 0 | } |
516 | | |
517 | | /* no quirk data */ |
518 | 0 | if (self->query_kv == NULL) |
519 | 0 | return NULL; |
520 | | |
521 | | /* query */ |
522 | 0 | xb_query_context_set_flags(&context, XB_QUERY_FLAG_USE_INDEXES); |
523 | 0 | xb_value_bindings_bind_str(xb_query_context_get_bindings(&context), 0, guid, NULL); |
524 | 0 | xb_value_bindings_bind_str(xb_query_context_get_bindings(&context), 1, key, NULL); |
525 | 0 | n = xb_silo_query_first_with_context(self->silo, self->query_kv, &context, &error); |
526 | 0 | if (n == NULL) { |
527 | 0 | if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) |
528 | 0 | return NULL; |
529 | 0 | if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT)) |
530 | 0 | return NULL; |
531 | 0 | g_warning("failed to query: %s", error->message); |
532 | 0 | return NULL; |
533 | 0 | } |
534 | 0 | if (self->verbose) |
535 | 0 | g_debug("%s:%s → %s", guid, key, xb_node_get_text(n)); |
536 | 0 | return xb_node_get_text(n); |
537 | 0 | } |
538 | | |
539 | | typedef struct { |
540 | | // gchar *guid; |
541 | | gchar *key; |
542 | | gchar *value; |
543 | | FuContextQuirkSource source; |
544 | | } FuQuirksCacheItem; |
545 | | |
546 | | static void |
547 | | fu_quirks_cache_item_free(FuQuirksCacheItem *item) |
548 | 0 | { |
549 | 0 | g_free(item->key); |
550 | 0 | g_free(item->value); |
551 | 0 | g_free(item); |
552 | 0 | } |
553 | | |
554 | | /** |
555 | | * fu_quirks_lookup_by_id_iter: |
556 | | * @self: a #FuQuirks |
557 | | * @guid: GUID to lookup |
558 | | * @key: (nullable): an ID to match the entry, e.g. `Name`, or %NULL for all keys |
559 | | * @iter_cb: (scope call) (closure user_data): a function to call for each result |
560 | | * @user_data: user data passed to @iter_cb |
561 | | * @flags: some #FuQuirksLookupFlags, e.g. #FU_QUIRKS_LOOKUP_FLAG_ALLOW_CACHE |
562 | | * |
563 | | * Looks up all entries in the hardware database using a GUID value. |
564 | | * |
565 | | * Returns: %TRUE if the ID was found, and @iter was called |
566 | | **/ |
567 | | gboolean |
568 | | fu_quirks_lookup_by_id_iter(FuQuirks *self, |
569 | | const gchar *guid, |
570 | | const gchar *key, |
571 | | FuQuirksIter iter_cb, |
572 | | FuQuirksLookupFlags flags, |
573 | | gpointer user_data) |
574 | 0 | { |
575 | 0 | GPtrArray *cache_items = NULL; |
576 | 0 | g_autoptr(GError) error = NULL; |
577 | 0 | g_autoptr(GPtrArray) results = NULL; |
578 | 0 | g_auto(XbQueryContext) context = XB_QUERY_CONTEXT_INIT(); |
579 | |
|
580 | 0 | g_return_val_if_fail(FU_IS_QUIRKS(self), FALSE); |
581 | 0 | g_return_val_if_fail(self->loaded, FALSE); |
582 | 0 | g_return_val_if_fail(guid != NULL, FALSE); |
583 | 0 | g_return_val_if_fail(iter_cb != NULL, FALSE); |
584 | | |
585 | | /* ensure up to date */ |
586 | 0 | if (!fu_quirks_check_silo(self, &error)) { |
587 | 0 | g_warning("failed to build silo: %s", error->message); |
588 | 0 | return FALSE; |
589 | 0 | } |
590 | | |
591 | | /* only for no key */ |
592 | 0 | if (key == NULL && (flags & FU_QUIRKS_LOOKUP_FLAG_ALLOW_CACHE) > 0) { |
593 | 0 | cache_items = g_hash_table_lookup(self->cache, guid); |
594 | 0 | if (cache_items != NULL) { |
595 | 0 | if (cache_items->len == 0) |
596 | 0 | return FALSE; |
597 | 0 | for (guint i = 0; i < cache_items->len; i++) { |
598 | 0 | FuQuirksCacheItem *item = g_ptr_array_index(cache_items, i); |
599 | 0 | iter_cb(self, item->key, item->value, item->source, user_data); |
600 | 0 | } |
601 | 0 | return TRUE; |
602 | 0 | } |
603 | 0 | cache_items = |
604 | 0 | g_ptr_array_new_with_free_func((GDestroyNotify)fu_quirks_cache_item_free); |
605 | 0 | g_hash_table_insert(self->cache, g_strdup(guid), cache_items); |
606 | 0 | } |
607 | | |
608 | | #ifdef HAVE_SQLITE |
609 | | /* this is generated from usb.ids and other static sources */ |
610 | | if (self->db != NULL && !fu_context_has_flag(self->ctx, FU_CONTEXT_FLAG_NO_CACHE)) { |
611 | | g_autoptr(sqlite3_stmt) stmt = NULL; |
612 | | if (key == NULL) { |
613 | | if (sqlite3_prepare_v2(self->db, |
614 | | "SELECT key, value FROM quirks WHERE guid = ?1", |
615 | | -1, |
616 | | &stmt, |
617 | | NULL) != SQLITE_OK) { |
618 | | g_warning("failed to prepare SQL: %s", sqlite3_errmsg(self->db)); |
619 | | return FALSE; |
620 | | } |
621 | | sqlite3_bind_text(stmt, 1, guid, -1, SQLITE_STATIC); |
622 | | } else { |
623 | | if (sqlite3_prepare_v2(self->db, |
624 | | "SELECT key, value FROM quirks WHERE guid = ?1 " |
625 | | "AND key = ?2", |
626 | | -1, |
627 | | &stmt, |
628 | | NULL) != SQLITE_OK) { |
629 | | g_warning("failed to prepare SQL: %s", sqlite3_errmsg(self->db)); |
630 | | return FALSE; |
631 | | } |
632 | | sqlite3_bind_text(stmt, 1, guid, -1, SQLITE_STATIC); |
633 | | sqlite3_bind_text(stmt, 2, key, -1, SQLITE_STATIC); |
634 | | } |
635 | | while (sqlite3_step(stmt) == SQLITE_ROW) { |
636 | | const gchar *key_tmp = (const gchar *)sqlite3_column_text(stmt, 0); |
637 | | const gchar *value = (const gchar *)sqlite3_column_text(stmt, 1); |
638 | | iter_cb(self, key_tmp, value, FU_CONTEXT_QUIRK_SOURCE_DB, user_data); |
639 | | if (cache_items != NULL) { |
640 | | FuQuirksCacheItem *item = g_new0(FuQuirksCacheItem, 1); |
641 | | item->key = g_strdup(key_tmp); |
642 | | item->value = g_strdup(value); |
643 | | item->source = FU_CONTEXT_QUIRK_SOURCE_DB; |
644 | | g_ptr_array_add(cache_items, item); |
645 | | } |
646 | | } |
647 | | } |
648 | | #endif |
649 | | |
650 | | /* no quirk data */ |
651 | 0 | if (self->query_vs == NULL) |
652 | 0 | return FALSE; |
653 | | |
654 | | /* query */ |
655 | 0 | xb_query_context_set_flags(&context, XB_QUERY_FLAG_USE_INDEXES); |
656 | 0 | xb_value_bindings_bind_str(xb_query_context_get_bindings(&context), 0, guid, NULL); |
657 | 0 | if (key != NULL) { |
658 | 0 | xb_value_bindings_bind_str(xb_query_context_get_bindings(&context), 1, key, NULL); |
659 | 0 | results = xb_silo_query_with_context(self->silo, self->query_kv, &context, &error); |
660 | 0 | } else { |
661 | 0 | results = xb_silo_query_with_context(self->silo, self->query_vs, &context, &error); |
662 | 0 | } |
663 | 0 | if (results == NULL) { |
664 | 0 | if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) |
665 | 0 | return FALSE; |
666 | 0 | if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT)) |
667 | 0 | return FALSE; |
668 | 0 | g_warning("failed to query: %s", error->message); |
669 | 0 | return FALSE; |
670 | 0 | } |
671 | 0 | for (guint i = 0; i < results->len; i++) { |
672 | 0 | XbNode *n = g_ptr_array_index(results, i); |
673 | 0 | if (self->verbose) |
674 | 0 | g_debug("%s → %s", guid, xb_node_get_text(n)); |
675 | 0 | iter_cb(self, |
676 | 0 | xb_node_get_attr(n, "key"), |
677 | 0 | xb_node_get_text(n), |
678 | 0 | FU_CONTEXT_QUIRK_SOURCE_FILE, |
679 | 0 | user_data); |
680 | 0 | if (cache_items != NULL) { |
681 | 0 | FuQuirksCacheItem *item = g_new0(FuQuirksCacheItem, 1); |
682 | 0 | item->key = g_strdup(xb_node_get_attr(n, "key")); |
683 | 0 | item->value = g_strdup(xb_node_get_text(n)); |
684 | 0 | item->source = FU_CONTEXT_QUIRK_SOURCE_FILE; |
685 | 0 | g_ptr_array_add(cache_items, item); |
686 | 0 | } |
687 | 0 | } |
688 | |
|
689 | 0 | return TRUE; |
690 | 0 | } |
691 | | |
692 | | #ifdef HAVE_SQLITE |
693 | | |
694 | | typedef struct { |
695 | | FuQuirks *self; |
696 | | sqlite3_stmt *stmt; |
697 | | const gchar *subsystem; |
698 | | const gchar *title_vid; |
699 | | const gchar *title_pid; |
700 | | GString *vid; |
701 | | } FuQuirksDbHelper; |
702 | | |
703 | | static void |
704 | | fu_quirks_db_helper_free(FuQuirksDbHelper *helper) |
705 | | { |
706 | | if (helper->vid != NULL) |
707 | | g_string_free(helper->vid, TRUE); |
708 | | g_free(helper); |
709 | | } |
710 | | |
711 | | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuQuirksDbHelper, fu_quirks_db_helper_free) |
712 | | |
713 | | static gboolean |
714 | | fu_quirks_db_add_vendor_entry(FuQuirksDbHelper *helper, |
715 | | const gchar *vid, |
716 | | const gchar *name, |
717 | | GError **error) |
718 | | { |
719 | | FuQuirks *self = FU_QUIRKS(helper->self); |
720 | | g_autofree gchar *guid = NULL; |
721 | | g_autofree gchar *instance_id = NULL; |
722 | | g_autofree gchar *vid_strup = g_ascii_strup(vid, -1); |
723 | | |
724 | | instance_id = g_strdup_printf("%s\\%s_%s", helper->subsystem, helper->title_vid, vid_strup); |
725 | | guid = fwupd_guid_hash_string(instance_id); |
726 | | sqlite3_reset(helper->stmt); |
727 | | sqlite3_bind_text(helper->stmt, 1, guid, -1, SQLITE_STATIC); |
728 | | sqlite3_bind_text(helper->stmt, 2, FWUPD_RESULT_KEY_VENDOR, -1, SQLITE_STATIC); |
729 | | sqlite3_bind_text(helper->stmt, 3, name, -1, SQLITE_STATIC); |
730 | | if (sqlite3_step(helper->stmt) != SQLITE_DONE) { |
731 | | g_set_error(error, |
732 | | FWUPD_ERROR, |
733 | | FWUPD_ERROR_WRITE, |
734 | | "failed to execute prepared statement: %s", |
735 | | sqlite3_errmsg(self->db)); |
736 | | return FALSE; |
737 | | } |
738 | | |
739 | | /* success */ |
740 | | return TRUE; |
741 | | } |
742 | | |
743 | | static gboolean |
744 | | fu_quirks_db_add_name_entry(FuQuirksDbHelper *helper, |
745 | | const gchar *vid, |
746 | | const gchar *pid, |
747 | | const gchar *name, |
748 | | GError **error) |
749 | | { |
750 | | FuQuirks *self = FU_QUIRKS(helper->self); |
751 | | g_autofree gchar *guid = NULL; |
752 | | g_autofree gchar *instance_id = NULL; |
753 | | g_autofree gchar *vid_strup = g_ascii_strup(vid, -1); |
754 | | g_autofree gchar *pid_strup = g_ascii_strup(pid, -1); |
755 | | |
756 | | instance_id = g_strdup_printf("%s\\%s_%s&%s_%s", |
757 | | helper->subsystem, |
758 | | helper->title_vid, |
759 | | vid_strup, |
760 | | helper->title_pid, |
761 | | pid_strup); |
762 | | guid = fwupd_guid_hash_string(instance_id); |
763 | | sqlite3_reset(helper->stmt); |
764 | | sqlite3_bind_text(helper->stmt, 1, guid, -1, SQLITE_STATIC); |
765 | | sqlite3_bind_text(helper->stmt, 2, FWUPD_RESULT_KEY_NAME, -1, SQLITE_STATIC); |
766 | | sqlite3_bind_text(helper->stmt, 3, name, -1, SQLITE_STATIC); |
767 | | if (sqlite3_step(helper->stmt) != SQLITE_DONE) { |
768 | | g_set_error(error, |
769 | | FWUPD_ERROR, |
770 | | FWUPD_ERROR_WRITE, |
771 | | "failed to execute prepared statement: %s", |
772 | | sqlite3_errmsg(self->db)); |
773 | | return FALSE; |
774 | | } |
775 | | |
776 | | /* success */ |
777 | | return TRUE; |
778 | | } |
779 | | |
780 | | static gboolean |
781 | | _g_ascii_isxstrn(const gchar *str, gsize n) |
782 | | { |
783 | | for (gsize i = 0; i < n; i++) { |
784 | | if (!g_ascii_isxdigit(str[i])) |
785 | | return FALSE; |
786 | | } |
787 | | return TRUE; |
788 | | } |
789 | | |
790 | | static gboolean |
791 | | fu_quirks_db_add_usbids_cb(GString *token, guint token_idx, gpointer user_data, GError **error) |
792 | | { |
793 | | FuQuirksDbHelper *helper = (FuQuirksDbHelper *)user_data; |
794 | | |
795 | | /* not vendor lines */ |
796 | | if (token->len < 7) |
797 | | return TRUE; |
798 | | |
799 | | /* ignore the wrong ones */ |
800 | | if (g_strstr_len(token->str, -1, "Wrong ID") != NULL || |
801 | | g_strstr_len(token->str, -1, "wrong ID") != NULL) |
802 | | return TRUE; |
803 | | |
804 | | /* 4 hex digits */ |
805 | | if (_g_ascii_isxstrn(token->str, 4)) { |
806 | | g_string_set_size(helper->vid, 0); |
807 | | g_string_append_len(helper->vid, token->str, 4); |
808 | | return fu_quirks_db_add_vendor_entry(helper, |
809 | | helper->vid->str, |
810 | | token->str + 6, |
811 | | error); |
812 | | } |
813 | | |
814 | | /* tab, then 4 hex digits */ |
815 | | if (helper->vid->len > 0 && token->str[0] == '\t' && _g_ascii_isxstrn(token->str + 1, 4)) { |
816 | | g_autofree gchar *pid = g_strndup(token->str + 1, 4); |
817 | | return fu_quirks_db_add_name_entry(helper, |
818 | | helper->vid->str, |
819 | | pid, |
820 | | token->str + 7, |
821 | | error); |
822 | | } |
823 | | |
824 | | /* build into XML */ |
825 | | return TRUE; |
826 | | } |
827 | | |
828 | | static gboolean |
829 | | fu_quirks_db_add_ouitxt_cb(GString *token, guint token_idx, gpointer user_data, GError **error) |
830 | | { |
831 | | FuQuirksDbHelper *helper = (FuQuirksDbHelper *)user_data; |
832 | | g_autofree gchar *vid = NULL; |
833 | | |
834 | | /* not vendor lines */ |
835 | | if (token->len < 22) |
836 | | return TRUE; |
837 | | |
838 | | /* not 6 hex digits */ |
839 | | if (!_g_ascii_isxstrn(token->str, 6)) |
840 | | return TRUE; |
841 | | |
842 | | /* build into XML */ |
843 | | vid = g_strndup(token->str, 6); |
844 | | return fu_quirks_db_add_vendor_entry(helper, vid, token->str + 22, error); |
845 | | } |
846 | | |
847 | | static gboolean |
848 | | fu_quirks_db_add_pnpids_cb(GString *token, guint token_idx, gpointer user_data, GError **error) |
849 | | { |
850 | | FuQuirksDbHelper *helper = (FuQuirksDbHelper *)user_data; |
851 | | g_autofree gchar *vid = NULL; |
852 | | |
853 | | /* not vendor lines */ |
854 | | if (token->len < 5) |
855 | | return TRUE; |
856 | | |
857 | | /* ignore the wrong ones */ |
858 | | if (g_strstr_len(token->str, -1, "DO NOT USE") != NULL) |
859 | | return TRUE; |
860 | | |
861 | | /* build into XML */ |
862 | | vid = g_strndup(token->str, 3); |
863 | | return fu_quirks_db_add_vendor_entry(helper, vid, token->str + 4, error); |
864 | | } |
865 | | |
866 | | typedef struct { |
867 | | const gchar *fn; |
868 | | const gchar *subsystem; |
869 | | const gchar *title_vid; |
870 | | const gchar *title_pid; |
871 | | FuStrsplitFunc func; |
872 | | } FuQuirksDbItem; |
873 | | |
874 | | static gboolean |
875 | | fu_quirks_db_sqlite3_exec(FuQuirks *self, const gchar *sql, GError **error) |
876 | | { |
877 | | gint rc; |
878 | | |
879 | | /* if we're running all the tests in parallel it is possible to hit this... */ |
880 | | for (guint i = 0; i < 10; i++) { |
881 | | rc = sqlite3_exec(self->db, sql, NULL, NULL, NULL); |
882 | | if (rc != SQLITE_LOCKED) |
883 | | break; |
884 | | g_usleep(50 * 1000); |
885 | | } |
886 | | if (rc != SQLITE_OK) { |
887 | | g_set_error(error, |
888 | | FWUPD_ERROR, |
889 | | FWUPD_ERROR_INTERNAL, |
890 | | "failed to run %s: %s", |
891 | | sql, |
892 | | sqlite3_errmsg(self->db)); |
893 | | return FALSE; |
894 | | } |
895 | | /* success */ |
896 | | return TRUE; |
897 | | } |
898 | | |
899 | | static gboolean |
900 | | fu_quirks_db_load(FuQuirks *self, GError **error) |
901 | | { |
902 | | g_autoptr(sqlite3_stmt) stmt_insert = NULL; |
903 | | g_autoptr(sqlite3_stmt) stmt_query = NULL; |
904 | | g_autoptr(GString) fn_mtimes = g_string_new("quirks"); |
905 | | g_autofree gchar *guid_fwupd = fwupd_guid_hash_string("fwupd"); |
906 | | const FuQuirksDbItem map[] = { |
907 | | {"pci.ids", "PCI", "VEN", "DEV", fu_quirks_db_add_usbids_cb}, |
908 | | {"usb.ids", "USB", "VID", "PID", fu_quirks_db_add_usbids_cb}, |
909 | | {"pnp.ids", "PNP", "VID", "PID", fu_quirks_db_add_pnpids_cb}, |
910 | | {"oui.txt", "OUI", "VID", "PID", fu_quirks_db_add_ouitxt_cb}, |
911 | | }; |
912 | | |
913 | | /* nothing to do */ |
914 | | if (fu_context_has_flag(self->ctx, FU_CONTEXT_FLAG_NO_CACHE)) |
915 | | return TRUE; |
916 | | |
917 | | /* create tables and indexes */ |
918 | | if (!fu_quirks_db_sqlite3_exec( |
919 | | self, |
920 | | "BEGIN TRANSACTION;" |
921 | | "CREATE TABLE IF NOT EXISTS quirks(guid, key, value);" |
922 | | "CREATE INDEX IF NOT EXISTS idx_quirks_guid ON quirks(guid);" |
923 | | "CREATE INDEX IF NOT EXISTS idx_quirks_guid_key ON quirks(guid, key);" |
924 | | "COMMIT;", |
925 | | error)) { |
926 | | return FALSE; |
927 | | } |
928 | | |
929 | | /* find out the mtimes of each of the files we want to load into the db */ |
930 | | for (guint i = 0; i < G_N_ELEMENTS(map); i++) { |
931 | | const FuQuirksDbItem *item = &map[i]; |
932 | | guint64 mtime; |
933 | | g_autofree gchar *fn = NULL; |
934 | | g_autoptr(GFile) file = NULL; |
935 | | g_autoptr(GFileInfo) info = NULL; |
936 | | |
937 | | fn = fu_context_build_filename(self->ctx, |
938 | | NULL, |
939 | | FU_PATH_KIND_DATADIR_VENDOR_IDS, |
940 | | item->fn, |
941 | | NULL); |
942 | | if (fn == NULL) |
943 | | continue; |
944 | | file = g_file_new_for_path(fn); |
945 | | if (!g_file_query_exists(file, NULL)) |
946 | | continue; |
947 | | info = g_file_query_info(file, |
948 | | G_FILE_ATTRIBUTE_TIME_MODIFIED, |
949 | | G_FILE_QUERY_INFO_NONE, |
950 | | NULL, |
951 | | error); |
952 | | if (info == NULL) |
953 | | return FALSE; |
954 | | mtime = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED); |
955 | | g_string_append_printf(fn_mtimes, ",%s:%" G_GUINT64_FORMAT, item->fn, mtime); |
956 | | } |
957 | | |
958 | | /* check if the mtimes match */ |
959 | | if (sqlite3_prepare_v2(self->db, |
960 | | "SELECT value FROM quirks WHERE guid = ?1 and key = ?2", |
961 | | -1, |
962 | | &stmt_query, |
963 | | NULL) != SQLITE_OK) { |
964 | | g_set_error(error, |
965 | | FWUPD_ERROR, |
966 | | FWUPD_ERROR_INTERNAL, |
967 | | "failed to prepare SQL: %s", |
968 | | sqlite3_errmsg(self->db)); |
969 | | return FALSE; |
970 | | } |
971 | | sqlite3_bind_text(stmt_query, 1, guid_fwupd, -1, SQLITE_STATIC); |
972 | | sqlite3_bind_text(stmt_query, 2, FWUPD_RESULT_KEY_VERSION, -1, SQLITE_STATIC); |
973 | | while (sqlite3_step(stmt_query) == SQLITE_ROW) { |
974 | | const gchar *fn_mtimes_old = (const gchar *)sqlite3_column_text(stmt_query, 0); |
975 | | if (g_strcmp0(fn_mtimes->str, fn_mtimes_old) == 0) { |
976 | | g_debug("mtimes unchanged: %s, doing nothing", fn_mtimes->str); |
977 | | return TRUE; |
978 | | } |
979 | | g_debug("mtimes changed %s vs %s -- regenerating", fn_mtimes_old, fn_mtimes->str); |
980 | | } |
981 | | |
982 | | /* delete any existing data */ |
983 | | if (!fu_quirks_db_sqlite3_exec(self, "BEGIN TRANSACTION;", error)) |
984 | | return FALSE; |
985 | | if (!fu_quirks_db_sqlite3_exec(self, "DELETE FROM quirks;", error)) |
986 | | return FALSE; |
987 | | |
988 | | /* prepared statement for speed */ |
989 | | if (sqlite3_prepare_v3(self->db, |
990 | | "INSERT INTO quirks (guid, key, value) VALUES (?1,?2,?3)", |
991 | | -1, |
992 | | SQLITE_PREPARE_PERSISTENT, |
993 | | &stmt_insert, |
994 | | NULL) != SQLITE_OK) { |
995 | | g_set_error(error, |
996 | | FWUPD_ERROR, |
997 | | FWUPD_ERROR_INTERNAL, |
998 | | "failed to prepare SQL to insert history: %s", |
999 | | sqlite3_errmsg(self->db)); |
1000 | | return FALSE; |
1001 | | } |
1002 | | |
1003 | | /* populate database */ |
1004 | | for (guint i = 0; i < G_N_ELEMENTS(map); i++) { |
1005 | | const FuQuirksDbItem *item = &map[i]; |
1006 | | g_autofree gchar *fn = NULL; |
1007 | | g_autoptr(FuQuirksDbHelper) helper = g_new0(FuQuirksDbHelper, 1); |
1008 | | g_autoptr(GFile) file = NULL; |
1009 | | g_autoptr(FwupdFileInputStream) stream = NULL; |
1010 | | |
1011 | | fn = fu_context_build_filename(self->ctx, |
1012 | | NULL, |
1013 | | FU_PATH_KIND_DATADIR_VENDOR_IDS, |
1014 | | item->fn, |
1015 | | NULL); |
1016 | | if (fn == NULL) |
1017 | | continue; |
1018 | | |
1019 | | /* split into lines */ |
1020 | | file = g_file_new_for_path(fn); |
1021 | | if (!g_file_query_exists(file, NULL)) { |
1022 | | g_debug("%s not found", fn); |
1023 | | continue; |
1024 | | } |
1025 | | g_debug("indexing vendor IDs from %s", fn); |
1026 | | stream = fwupd_file_input_stream_new(fn, error); |
1027 | | if (stream == NULL) |
1028 | | return FALSE; |
1029 | | helper->self = self; |
1030 | | helper->subsystem = item->subsystem; |
1031 | | helper->title_vid = item->title_vid; |
1032 | | helper->title_pid = item->title_pid; |
1033 | | helper->stmt = stmt_insert; |
1034 | | helper->vid = g_string_new(NULL); |
1035 | | if (!fu_strsplit_stream(FWUPD_INPUT_STREAM(stream), |
1036 | | 0x0, |
1037 | | "\n", |
1038 | | item->func, |
1039 | | helper, |
1040 | | error)) |
1041 | | return FALSE; |
1042 | | } |
1043 | | |
1044 | | /* set schema */ |
1045 | | sqlite3_reset(stmt_insert); |
1046 | | sqlite3_bind_text(stmt_insert, 1, guid_fwupd, -1, SQLITE_STATIC); |
1047 | | sqlite3_bind_text(stmt_insert, 2, FWUPD_RESULT_KEY_VERSION, -1, SQLITE_STATIC); |
1048 | | sqlite3_bind_text(stmt_insert, 3, fn_mtimes->str, -1, SQLITE_STATIC); |
1049 | | if (sqlite3_step(stmt_insert) != SQLITE_DONE) { |
1050 | | g_set_error(error, |
1051 | | FWUPD_ERROR, |
1052 | | FWUPD_ERROR_WRITE, |
1053 | | "failed to execute prepared statement: %s", |
1054 | | sqlite3_errmsg(self->db)); |
1055 | | return FALSE; |
1056 | | } |
1057 | | if (!fu_quirks_db_sqlite3_exec(self, "COMMIT;", error)) |
1058 | | return FALSE; |
1059 | | |
1060 | | /* success */ |
1061 | | return TRUE; |
1062 | | } |
1063 | | #endif |
1064 | | |
1065 | | /** |
1066 | | * fu_quirks_load: (skip) |
1067 | | * @self: a #FuQuirks |
1068 | | * @error: (nullable): optional return location for an error |
1069 | | * |
1070 | | * Loads the various files that define the hardware quirks used in plugins. |
1071 | | * |
1072 | | * Returns: %TRUE for success |
1073 | | **/ |
1074 | | gboolean |
1075 | | fu_quirks_load(FuQuirks *self, GError **error) |
1076 | 0 | { |
1077 | 0 | g_return_val_if_fail(FU_IS_QUIRKS(self), FALSE); |
1078 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, FALSE); |
1079 | | |
1080 | 0 | self->loaded = TRUE; |
1081 | 0 | self->verbose = g_getenv("FWUPD_XMLB_VERBOSE") != NULL; |
1082 | |
|
1083 | | #ifdef HAVE_SQLITE |
1084 | | if (self->db == NULL && !fu_context_has_flag(self->ctx, FU_CONTEXT_FLAG_NO_CACHE)) { |
1085 | | g_autofree gchar *quirksdb = NULL; |
1086 | | |
1087 | | quirksdb = fu_context_build_filename(self->ctx, |
1088 | | error, |
1089 | | FU_PATH_KIND_CACHEDIR_PKG, |
1090 | | "quirks.db", |
1091 | | NULL); |
1092 | | if (quirksdb == NULL) |
1093 | | return FALSE; |
1094 | | g_debug("open database %s", quirksdb); |
1095 | | if (!fu_path_mkdir_parent(quirksdb, error)) |
1096 | | return FALSE; |
1097 | | if (sqlite3_open(quirksdb, &self->db) != SQLITE_OK) { |
1098 | | g_set_error(error, |
1099 | | FWUPD_ERROR, |
1100 | | FWUPD_ERROR_READ, |
1101 | | "cannot open %s: %s", |
1102 | | quirksdb, |
1103 | | sqlite3_errmsg(self->db)); |
1104 | | return FALSE; |
1105 | | } |
1106 | | if (!fu_quirks_db_load(self, error)) |
1107 | | return FALSE; |
1108 | | } |
1109 | | #endif |
1110 | | |
1111 | | /* now silo */ |
1112 | 0 | return fu_quirks_check_silo(self, error); |
1113 | 0 | } |
1114 | | |
1115 | | /** |
1116 | | * fu_quirks_add_possible_key: |
1117 | | * @self: a #FuQuirks |
1118 | | * @possible_key: a key name, e.g. `Flags` |
1119 | | * |
1120 | | * Adds a possible quirk key. If added by a plugin it should be namespaced |
1121 | | * using the plugin name, where possible. |
1122 | | **/ |
1123 | | void |
1124 | | fu_quirks_add_possible_key(FuQuirks *self, const gchar *possible_key) |
1125 | 0 | { |
1126 | 0 | g_return_if_fail(FU_IS_QUIRKS(self)); |
1127 | 0 | g_return_if_fail(possible_key != NULL); |
1128 | 0 | g_hash_table_add(self->possible_keys, g_strdup(possible_key)); |
1129 | 0 | } |
1130 | | |
1131 | | static void |
1132 | | fu_quirks_housekeeping_cb(FuContext *ctx, FuQuirks *self) |
1133 | 0 | { |
1134 | | #ifdef HAVE_SQLITE |
1135 | | sqlite3_release_memory(G_MAXINT32); |
1136 | | if (self->db != NULL) |
1137 | | sqlite3_db_release_memory(self->db); |
1138 | | #endif |
1139 | 0 | } |
1140 | | |
1141 | | static void |
1142 | | fu_quirks_dispose(GObject *object) |
1143 | 0 | { |
1144 | 0 | FuQuirks *self = FU_QUIRKS(object); |
1145 | 0 | if (self->ctx != NULL) |
1146 | 0 | g_signal_handlers_disconnect_by_data(self->ctx, self); |
1147 | 0 | g_clear_object(&self->ctx); |
1148 | 0 | G_OBJECT_CLASS(fu_quirks_parent_class)->dispose(object); |
1149 | 0 | } |
1150 | | |
1151 | | static void |
1152 | | fu_quirks_class_init(FuQuirksClass *klass) |
1153 | 0 | { |
1154 | 0 | GObjectClass *object_class = G_OBJECT_CLASS(klass); |
1155 | 0 | object_class->dispose = fu_quirks_dispose; |
1156 | 0 | object_class->finalize = fu_quirks_finalize; |
1157 | 0 | } |
1158 | | |
1159 | | static void |
1160 | | fu_quirks_init(FuQuirks *self) |
1161 | 0 | { |
1162 | 0 | self->possible_keys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); |
1163 | 0 | self->invalid_keys = g_ptr_array_new_with_free_func(g_free); |
1164 | 0 | self->cache = g_hash_table_new_full(g_str_hash, |
1165 | 0 | g_str_equal, |
1166 | 0 | g_free, |
1167 | 0 | (GDestroyNotify)g_ptr_array_unref); |
1168 | | |
1169 | | /* built in */ |
1170 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_BRANCH); |
1171 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CHILDREN); |
1172 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_COUNTERPART_GUID); |
1173 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_FIRMWARE_SIZE); |
1174 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_FIRMWARE_SIZE_MAX); |
1175 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_FIRMWARE_SIZE_MIN); |
1176 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_FLAGS); |
1177 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_GTYPE); |
1178 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_FIRMWARE_GTYPE); |
1179 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_GUID); |
1180 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_ICON); |
1181 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_INHIBIT); |
1182 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_INSTALL_DURATION); |
1183 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_ISSUE); |
1184 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_NAME); |
1185 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_PARENT_GUID); |
1186 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_PLUGIN); |
1187 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_PRIORITY); |
1188 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_PROTOCOL); |
1189 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_PROBLEM); |
1190 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_PROXY_GUID); |
1191 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_APPSTREAM_ID); |
1192 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_BATTERY_THRESHOLD); |
1193 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_REMOVE_DELAY); |
1194 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_SUMMARY); |
1195 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_UPDATE_IMAGE); |
1196 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_UPDATE_MESSAGE); |
1197 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_VENDOR); |
1198 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_VENDOR_ID); |
1199 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_VERSION); |
1200 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_VERSION_FORMAT); |
1201 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_VERSION_RAW); |
1202 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_READ_ID); |
1203 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_READ_ID_SZ); |
1204 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_CHIP_ERASE); |
1205 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_BLOCK_ERASE); |
1206 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_SECTOR_ERASE); |
1207 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_WRITE_STATUS); |
1208 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_PAGE_PROG); |
1209 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_READ_DATA); |
1210 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_READ_STATUS); |
1211 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_CMD_WRITE_EN); |
1212 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_PAGE_SIZE); |
1213 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_SECTOR_SIZE); |
1214 | 0 | fu_quirks_add_possible_key(self, FU_QUIRKS_CFI_DEVICE_BLOCK_SIZE); |
1215 | 0 | } |
1216 | | |
1217 | | static void |
1218 | | fu_quirks_finalize(GObject *obj) |
1219 | 0 | { |
1220 | 0 | FuQuirks *self = FU_QUIRKS(obj); |
1221 | 0 | if (self->query_kv != NULL) |
1222 | 0 | g_object_unref(self->query_kv); |
1223 | 0 | if (self->query_vs != NULL) |
1224 | 0 | g_object_unref(self->query_vs); |
1225 | 0 | if (self->silo != NULL) |
1226 | 0 | g_object_unref(self->silo); |
1227 | | #ifdef HAVE_SQLITE |
1228 | | if (self->db != NULL) |
1229 | | sqlite3_close(self->db); |
1230 | | #endif |
1231 | 0 | g_hash_table_unref(self->possible_keys); |
1232 | 0 | g_hash_table_unref(self->cache); |
1233 | 0 | g_ptr_array_unref(self->invalid_keys); |
1234 | 0 | G_OBJECT_CLASS(fu_quirks_parent_class)->finalize(obj); |
1235 | 0 | } |
1236 | | |
1237 | | /** |
1238 | | * fu_quirks_new: (skip) |
1239 | | * |
1240 | | * Creates a new quirks object. |
1241 | | * |
1242 | | * Returns: a new #FuQuirks |
1243 | | **/ |
1244 | | FuQuirks * |
1245 | | fu_quirks_new(FuContext *ctx) |
1246 | 0 | { |
1247 | 0 | FuQuirks *self; |
1248 | 0 | self = g_object_new(FU_TYPE_QUIRKS, NULL); |
1249 | 0 | self->ctx = g_object_ref(ctx); |
1250 | | g_signal_connect(self->ctx, "housekeeping", G_CALLBACK(fu_quirks_housekeeping_cb), self); |
1251 | 0 | return FU_QUIRKS(self); |
1252 | 0 | } |