blob: e5494451934bfb42683c9a265993a4c08d27f799 [file] [log] [blame]
Richard Hughesd0905142016-03-13 09:46:49 +00001/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
Richard Hughes2de8f132018-01-17 09:12:02 +00003 * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com>
Richard Hughesd0905142016-03-13 09:46:49 +00004 *
Mario Limonciello51308e62018-05-28 20:05:46 -05005 * SPDX-License-Identifier: LGPL-2.1+
Richard Hughesd0905142016-03-13 09:46:49 +00006 */
7
8#include "config.h"
9
Richard Hughescff38bc2016-12-12 12:03:37 +000010#include <fwupd.h>
11#include <gmodule.h>
12#include <appstream-glib.h>
13#include <errno.h>
14#include <string.h>
15#include <gio/gunixinputstream.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060016#ifdef HAVE_VALGRIND
Richard Hughes576c0122017-02-24 09:47:00 +000017#include <valgrind.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060018#endif /* HAVE_VALGRIND */
Richard Hughesd0905142016-03-13 09:46:49 +000019
Richard Hughes9dde04f2017-09-13 12:07:15 +010020#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000021#include "fu-plugin-private.h"
Richard Hughesbc3a4e12018-01-06 22:41:47 +000022#include "fu-history.h"
Richard Hughesd0905142016-03-13 09:46:49 +000023
Richard Hughes4eada342017-10-03 21:20:32 +010024/**
25 * SECTION:fu-plugin
26 * @short_description: a daemon plugin
27 *
28 * An object that represents a plugin run by the daemon.
29 *
30 * See also: #FuDevice
31 */
32
Richard Hughesb0829032017-01-10 09:27:08 +000033#define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */
34
Richard Hughescff38bc2016-12-12 12:03:37 +000035static void fu_plugin_finalize (GObject *object);
36
37typedef struct {
38 GModule *module;
39 GUsbContext *usb_ctx;
40 gboolean enabled;
Richard Hughes08a37992017-09-12 12:57:43 +010041 guint order;
42 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughescff38bc2016-12-12 12:03:37 +000043 gchar *name;
Richard Hughesd7704d42017-08-08 20:29:09 +010044 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010045 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010046 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010047 GHashTable *compile_versions;
Richard Hughes1354ea92017-09-19 15:58:31 +010048 GPtrArray *supported_guids;
49 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000050 GHashTable *devices; /* platform_id:GObject */
Richard Hughesae3d65f2016-12-16 09:38:01 +000051 GHashTable *devices_delay; /* FuDevice:FuPluginHelper */
Richard Hughes80b79bb2018-01-11 21:11:06 +000052 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000053 FuPluginData *data;
54} FuPluginPrivate;
55
56enum {
57 SIGNAL_DEVICE_ADDED,
58 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010059 SIGNAL_DEVICE_REGISTER,
Richard Hughes362d6d72017-01-07 21:42:14 +000060 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000061 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughescff38bc2016-12-12 12:03:37 +000062 SIGNAL_LAST
63};
64
65static guint signals[SIGNAL_LAST] = { 0 };
66
67G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
68#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
69
70typedef const gchar *(*FuPluginGetNameFunc) (void);
71typedef void (*FuPluginInitFunc) (FuPlugin *plugin);
72typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin,
73 GError **error);
Richard Hughese1fd34d2017-08-24 14:19:51 +010074typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin,
75 FuDevice *device);
Richard Hughescff38bc2016-12-12 12:03:37 +000076typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin,
77 FuDevice *device,
78 GError **error);
79typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin,
80 FuDevice *device,
81 FuPluginVerifyFlags flags,
82 GError **error);
83typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin,
84 FuDevice *device,
85 GBytes *blob_fw,
86 FwupdInstallFlags flags,
87 GError **error);
Richard Hughes104f6512017-11-24 11:44:57 +000088typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin,
89 GUsbDevice *usb_device,
90 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +000091
Richard Hughes57d18222017-01-10 16:02:59 +000092/**
93 * fu_plugin_get_name:
94 * @plugin: A #FuPlugin
95 *
96 * Gets the plugin name.
97 *
98 * Returns: a plugin name, or %NULL for unknown.
99 *
100 * Since: 0.8.0
101 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000102const gchar *
103fu_plugin_get_name (FuPlugin *plugin)
Richard Hughesd0905142016-03-13 09:46:49 +0000104{
Richard Hughescff38bc2016-12-12 12:03:37 +0000105 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000106 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000107 return priv->name;
108}
Richard Hughesd0905142016-03-13 09:46:49 +0000109
Richard Hughes34834102017-11-21 21:55:00 +0000110void
111fu_plugin_set_name (FuPlugin *plugin, const gchar *name)
112{
113 FuPluginPrivate *priv = GET_PRIVATE (plugin);
114 g_return_if_fail (FU_IS_PLUGIN (plugin));
115 g_return_if_fail (name != NULL);
116 g_free (priv->name);
117 priv->name = g_strdup (name);
118}
119
Richard Hughes57d18222017-01-10 16:02:59 +0000120/**
121 * fu_plugin_cache_lookup:
122 * @plugin: A #FuPlugin
123 * @id: the key
124 *
125 * Finds an object in the per-plugin cache.
126 *
127 * Returns: (transfer none): a #GObject, or %NULL for unfound.
128 *
129 * Since: 0.8.0
130 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000131gpointer
132fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id)
133{
134 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000135 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
136 g_return_val_if_fail (id != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000137 return g_hash_table_lookup (priv->devices, id);
138}
Richard Hughesd0905142016-03-13 09:46:49 +0000139
Richard Hughes57d18222017-01-10 16:02:59 +0000140/**
141 * fu_plugin_cache_add:
142 * @plugin: A #FuPlugin
143 * @id: the key
144 * @dev: a #GObject, typically a #FuDevice
145 *
146 * Adds an object to the per-plugin cache.
147 *
148 * Since: 0.8.0
149 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000150void
151fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev)
152{
153 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000154 g_return_if_fail (FU_IS_PLUGIN (plugin));
155 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000156 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
157}
158
Richard Hughes57d18222017-01-10 16:02:59 +0000159/**
160 * fu_plugin_cache_remove:
161 * @plugin: A #FuPlugin
162 * @id: the key
163 *
164 * Removes an object from the per-plugin cache.
165 *
166 * Since: 0.8.0
167 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000168void
169fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id)
170{
171 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000172 g_return_if_fail (FU_IS_PLUGIN (plugin));
173 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000174 g_hash_table_remove (priv->devices, id);
175}
176
Richard Hughes57d18222017-01-10 16:02:59 +0000177/**
178 * fu_plugin_get_data:
179 * @plugin: A #FuPlugin
180 *
Richard Hughes4eada342017-10-03 21:20:32 +0100181 * Gets the per-plugin allocated private data. This will return %NULL unless
182 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000183 *
Richard Hughes4eada342017-10-03 21:20:32 +0100184 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000185 *
186 * Since: 0.8.0
187 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000188FuPluginData *
189fu_plugin_get_data (FuPlugin *plugin)
190{
191 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000192 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000193 return priv->data;
194}
195
Richard Hughes57d18222017-01-10 16:02:59 +0000196/**
197 * fu_plugin_alloc_data:
198 * @plugin: A #FuPlugin
199 * @data_sz: the size to allocate
200 *
201 * Allocates the per-plugin allocated private data.
202 *
203 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
204 *
205 * Since: 0.8.0
206 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000207FuPluginData *
208fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz)
209{
210 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000211 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000212 if (priv->data != NULL) {
213 g_critical ("fu_plugin_alloc_data() already used by plugin");
214 return priv->data;
215 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000216 priv->data = g_malloc0 (data_sz);
217 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000218}
219
Richard Hughes57d18222017-01-10 16:02:59 +0000220/**
221 * fu_plugin_get_usb_context:
222 * @plugin: A #FuPlugin
223 *
224 * Gets the shared USB context that all plugins can use.
225 *
226 * Returns: (transfer none): a #GUsbContext.
227 *
228 * Since: 0.8.0
229 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000230GUsbContext *
231fu_plugin_get_usb_context (FuPlugin *plugin)
232{
Richard Hughescff38bc2016-12-12 12:03:37 +0000233 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000234 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000235 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000236}
237
238void
239fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx)
240{
Richard Hughescff38bc2016-12-12 12:03:37 +0000241 FuPluginPrivate *priv = GET_PRIVATE (plugin);
242 g_set_object (&priv->usb_ctx, usb_ctx);
243}
244
Richard Hughes57d18222017-01-10 16:02:59 +0000245/**
246 * fu_plugin_get_enabled:
247 * @plugin: A #FuPlugin
248 *
Richard Hughes4eada342017-10-03 21:20:32 +0100249 * Returns if the plugin is enabled. Plugins may self-disable using
250 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000251 *
252 * Returns: %TRUE if the plugin is currently enabled.
253 *
254 * Since: 0.8.0
255 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000256gboolean
257fu_plugin_get_enabled (FuPlugin *plugin)
258{
259 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000260 g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000261 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000262}
263
Richard Hughes57d18222017-01-10 16:02:59 +0000264/**
265 * fu_plugin_set_enabled:
266 * @plugin: A #FuPlugin
267 * @enabled: the enabled value
268 *
269 * Enables or disables a plugin. Plugins can self-disable at any point.
270 *
271 * Since: 0.8.0
272 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000273void
Richard Hughescff38bc2016-12-12 12:03:37 +0000274fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000275{
Richard Hughescff38bc2016-12-12 12:03:37 +0000276 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000277 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughescff38bc2016-12-12 12:03:37 +0000278 priv->enabled = enabled;
279}
280
Richard Hughes1e456bc2018-05-10 20:16:16 +0100281gchar *
282fu_plugin_guess_name_from_fn (const gchar *filename)
283{
284 const gchar *prefix = "libfu_plugin_";
285 gchar *name;
286 gchar *str = g_strstr_len (filename, -1, prefix);
287 if (str == NULL)
288 return NULL;
289 name = g_strdup (str + strlen (prefix));
290 g_strdelimit (name, ".", '\0');
291 return name;
292}
293
Richard Hughescff38bc2016-12-12 12:03:37 +0000294gboolean
295fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error)
296{
297 FuPluginPrivate *priv = GET_PRIVATE (plugin);
298 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000299
300 priv->module = g_module_open (filename, 0);
301 if (priv->module == NULL) {
302 g_set_error (error,
303 G_IO_ERROR,
304 G_IO_ERROR_FAILED,
305 "failed to open plugin: %s",
306 g_module_error ());
307 return FALSE;
308 }
309
310 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100311 if (priv->name == NULL)
312 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000313
314 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000315 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
316 if (func != NULL) {
317 g_debug ("performing init() on %s", filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000318 func (plugin);
319 }
320
Richard Hughescff38bc2016-12-12 12:03:37 +0000321 return TRUE;
322}
323
Richard Hughes57d18222017-01-10 16:02:59 +0000324/**
325 * fu_plugin_device_add:
326 * @plugin: A #FuPlugin
327 * @device: A #FuDevice
328 *
329 * Asks the daemon to add a device to the exported list. If this device ID
330 * has already been added by a different plugin then this request will be
331 * ignored.
332 *
333 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
334 * actually flashing an image to the hardware so that higher-priority plugins
335 * can add the device themselves.
336 *
337 * Since: 0.8.0
338 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000339void
340fu_plugin_device_add (FuPlugin *plugin, FuDevice *device)
341{
Richard Hughes5e447292018-04-27 14:25:54 +0100342 GPtrArray *children;
343
Richard Hughesccd78a92017-01-11 16:57:41 +0000344 g_return_if_fail (FU_IS_PLUGIN (plugin));
345 g_return_if_fail (FU_IS_DEVICE (device));
346
Richard Hughescff38bc2016-12-12 12:03:37 +0000347 g_debug ("emit added from %s: %s",
348 fu_plugin_get_name (plugin),
349 fu_device_get_id (device));
350 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
351 fu_device_set_plugin (device, fu_plugin_get_name (plugin));
352 g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100353
354 /* add children */
355 children = fu_device_get_children (device);
356 for (guint i = 0; i < children->len; i++) {
357 FuDevice *child = g_ptr_array_index (children, i);
358 fu_plugin_device_add (plugin, child);
359 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000360}
361
Richard Hughese1fd34d2017-08-24 14:19:51 +0100362/**
363 * fu_plugin_device_register:
364 * @plugin: A #FuPlugin
365 * @device: A #FuDevice
366 *
367 * Registers the device with other plugins so they can set metadata.
368 *
369 * Plugins do not have to call this manually as this is done automatically
370 * when using fu_plugin_device_add(). They may wish to use this manually
371 * if for intance the coldplug should be ignored based on the metadata
372 * set from other plugins.
373 *
374 * Since: 0.9.7
375 **/
376void
377fu_plugin_device_register (FuPlugin *plugin, FuDevice *device)
378{
379 g_return_if_fail (FU_IS_PLUGIN (plugin));
380 g_return_if_fail (FU_IS_DEVICE (device));
381
382 g_debug ("emit device-register from %s: %s",
383 fu_plugin_get_name (plugin),
384 fu_device_get_id (device));
385 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device);
386}
387
Richard Hughesae3d65f2016-12-16 09:38:01 +0000388typedef struct {
389 FuPlugin *plugin;
390 FuDevice *device;
391 guint timeout_id;
Richard Hughesd4184cf2016-12-21 16:05:17 +0000392 GHashTable *devices;
Richard Hughesae3d65f2016-12-16 09:38:01 +0000393} FuPluginHelper;
394
395static void
396fu_plugin_helper_free (FuPluginHelper *helper)
397{
398 g_object_unref (helper->plugin);
399 g_object_unref (helper->device);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000400 g_hash_table_unref (helper->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000401 g_free (helper);
402}
403
404static gboolean
405fu_plugin_device_add_delay_cb (gpointer user_data)
406{
407 FuPluginHelper *helper = (FuPluginHelper *) user_data;
408 fu_plugin_device_add (helper->plugin, helper->device);
Richard Hughes0dec2742017-09-18 11:07:39 +0100409 g_hash_table_remove (helper->devices, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000410 fu_plugin_helper_free (helper);
411 return FALSE;
412}
413
Richard Hughes57d18222017-01-10 16:02:59 +0000414/**
Richard Hughesf0a799e2017-01-17 20:13:30 +0000415 * fu_plugin_has_device_delay:
416 * @plugin: A #FuPlugin
417 *
418 * Returns if the device has a pending device that is waiting to be added.
419 *
420 * Returns: %TRUE if a device is waiting to be added
421 *
422 * Since: 0.8.0
423 **/
424gboolean
425fu_plugin_has_device_delay (FuPlugin *plugin)
426{
427 FuPluginPrivate *priv = GET_PRIVATE (plugin);
428 return g_hash_table_size (priv->devices_delay) > 0;
429}
430
431/**
Richard Hughes57d18222017-01-10 16:02:59 +0000432 * fu_plugin_device_add_delay:
433 * @plugin: A #FuPlugin
434 * @device: A #FuDevice
435 *
436 * Asks the daemon to add a device to the exported list after a small delay.
437 *
438 * Since: 0.8.0
439 **/
Richard Hughesae3d65f2016-12-16 09:38:01 +0000440void
441fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device)
442{
443 FuPluginPrivate *priv = GET_PRIVATE (plugin);
444 FuPluginHelper *helper;
Richard Hughesccd78a92017-01-11 16:57:41 +0000445
446 g_return_if_fail (FU_IS_PLUGIN (plugin));
447 g_return_if_fail (FU_IS_DEVICE (device));
448
Richard Hughes6ad951d2017-02-03 10:12:12 +0000449 /* already waiting for add */
450 helper = g_hash_table_lookup (priv->devices_delay, device);
451 if (helper != NULL) {
Richard Hughes3cca1c62017-07-21 13:38:20 +0100452 g_debug ("ignoring add-delay as device %s already pending",
453 fu_device_get_id (device));
Richard Hughes6ad951d2017-02-03 10:12:12 +0000454 return;
455 }
456
457 /* add after a small delay */
Richard Hughesae3d65f2016-12-16 09:38:01 +0000458 g_debug ("waiting a small time for other plugins");
459 helper = g_new0 (FuPluginHelper, 1);
460 helper->plugin = g_object_ref (plugin);
461 helper->device = g_object_ref (device);
462 helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000463 helper->devices = g_hash_table_ref (priv->devices_delay);
464 g_hash_table_insert (helper->devices, device, helper);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000465}
466
Richard Hughes57d18222017-01-10 16:02:59 +0000467/**
Richard Hughes4eada342017-10-03 21:20:32 +0100468 * fu_plugin_device_remove:
Richard Hughes57d18222017-01-10 16:02:59 +0000469 * @plugin: A #FuPlugin
470 * @device: A #FuDevice
471 *
472 * Asks the daemon to remove a device from the exported list.
473 *
474 * Since: 0.8.0
475 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000476void
477fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device)
478{
Richard Hughesae3d65f2016-12-16 09:38:01 +0000479 FuPluginPrivate *priv = GET_PRIVATE (plugin);
480 FuPluginHelper *helper;
481
Richard Hughesccd78a92017-01-11 16:57:41 +0000482 g_return_if_fail (FU_IS_PLUGIN (plugin));
483 g_return_if_fail (FU_IS_DEVICE (device));
484
Richard Hughesae3d65f2016-12-16 09:38:01 +0000485 /* waiting for add */
486 helper = g_hash_table_lookup (priv->devices_delay, device);
487 if (helper != NULL) {
488 g_debug ("ignoring remove from delayed addition");
489 g_source_remove (helper->timeout_id);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000490 g_hash_table_remove (priv->devices_delay, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000491 fu_plugin_helper_free (helper);
492 return;
493 }
494
Richard Hughescff38bc2016-12-12 12:03:37 +0000495 g_debug ("emit removed from %s: %s",
496 fu_plugin_get_name (plugin),
497 fu_device_get_id (device));
498 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device);
499}
500
Richard Hughes57d18222017-01-10 16:02:59 +0000501/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000502 * fu_plugin_request_recoldplug:
Richard Hughes362d6d72017-01-07 21:42:14 +0000503 * @plugin: A #FuPlugin
504 *
505 * Ask all the plugins to coldplug all devices, which will include the prepare()
506 * and cleanup() phases. Duplicate devices added will be ignored.
507 *
508 * Since: 0.8.0
509 **/
510void
Richard Hughes2de8f132018-01-17 09:12:02 +0000511fu_plugin_request_recoldplug (FuPlugin *plugin)
Richard Hughes362d6d72017-01-07 21:42:14 +0000512{
Richard Hughesccd78a92017-01-11 16:57:41 +0000513 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughes362d6d72017-01-07 21:42:14 +0000514 g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0);
515}
516
Richard Hughesb0829032017-01-10 09:27:08 +0000517/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100518 * fu_plugin_check_hwid:
519 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100520 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100521 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100522 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100523 * specific system can be shown using the `fwupdmgr hwids` command.
524 *
Richard Hughes4eada342017-10-03 21:20:32 +0100525 * Returns: %TRUE if the HwId is found on the system.
526 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100527 * Since: 0.9.1
528 **/
529gboolean
530fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid)
531{
532 FuPluginPrivate *priv = GET_PRIVATE (plugin);
533 if (priv->hwids == NULL)
534 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100535 return fu_hwids_has_guid (priv->hwids, hwid);
536}
537
538/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100539 * fu_plugin_check_supported:
540 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100541 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100542 *
543 * Checks to see if a specific device GUID is supported, i.e. available in the
544 * AppStream metadata.
545 *
Richard Hughes4eada342017-10-03 21:20:32 +0100546 * Returns: %TRUE if the device is supported.
547 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100548 * Since: 1.0.0
549 **/
550gboolean
551fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid)
552{
553 FuPluginPrivate *priv = GET_PRIVATE (plugin);
554 if (priv->supported_guids == NULL)
555 return FALSE;
556 for (guint i = 0; i < priv->supported_guids->len; i++) {
557 const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i);
558 if (g_strcmp0 (guid, guid_tmp) == 0)
559 return TRUE;
560 }
561 return FALSE;
562}
563
564/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100565 * fu_plugin_get_dmi_value:
566 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100567 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100568 *
569 * Gets a hardware DMI value.
570 *
Richard Hughes4eada342017-10-03 21:20:32 +0100571 * Returns: The string, or %NULL
572 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100573 * Since: 0.9.7
574 **/
575const gchar *
576fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id)
577{
578 FuPluginPrivate *priv = GET_PRIVATE (plugin);
579 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100580 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100581 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100582}
583
Richard Hughes49e5e052017-09-03 12:15:41 +0100584/**
585 * fu_plugin_get_smbios_string:
586 * @plugin: A #FuPlugin
587 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
588 * @offset: A SMBIOS offset
589 *
590 * Gets a hardware SMBIOS string.
591 *
592 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
593 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
594 *
Richard Hughes4eada342017-10-03 21:20:32 +0100595 * Returns: A string, or %NULL
596 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100597 * Since: 0.9.8
598 **/
599const gchar *
600fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset)
601{
602 FuPluginPrivate *priv = GET_PRIVATE (plugin);
603 if (priv->smbios == NULL)
604 return NULL;
605 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
606}
607
608/**
609 * fu_plugin_get_smbios_data:
610 * @plugin: A #FuPlugin
611 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
612 *
613 * Gets a hardware SMBIOS data.
614 *
Richard Hughes4eada342017-10-03 21:20:32 +0100615 * Returns: (transfer none): A #GBytes, or %NULL
616 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100617 * Since: 0.9.8
618 **/
619GBytes *
620fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type)
621{
622 FuPluginPrivate *priv = GET_PRIVATE (plugin);
623 if (priv->smbios == NULL)
624 return NULL;
625 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
626}
627
Richard Hughesb8f8db22017-04-25 15:56:00 +0100628void
Richard Hughesd7704d42017-08-08 20:29:09 +0100629fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100630{
631 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesd7704d42017-08-08 20:29:09 +0100632 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100633}
634
Richard Hughes49e5e052017-09-03 12:15:41 +0100635void
Richard Hughes1354ea92017-09-19 15:58:31 +0100636fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids)
637{
638 FuPluginPrivate *priv = GET_PRIVATE (plugin);
639 if (priv->supported_guids != NULL)
640 g_ptr_array_unref (priv->supported_guids);
641 priv->supported_guids = g_ptr_array_ref (supported_guids);
642}
643
Richard Hughes9c028f02017-10-28 21:14:28 +0100644void
645fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks)
646{
647 FuPluginPrivate *priv = GET_PRIVATE (plugin);
648 g_set_object (&priv->quirks, quirks);
649}
650
651/**
652 * fu_plugin_get_quirks:
653 * @plugin: A #FuPlugin
654 *
655 * Returns the hardware database object. This can be used to discover device
656 * quirks or other device-specific settings.
657 *
658 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
659 *
660 * Since: 1.0.1
661 **/
662FuQuirks *
663fu_plugin_get_quirks (FuPlugin *plugin)
664{
665 FuPluginPrivate *priv = GET_PRIVATE (plugin);
666 return priv->quirks;
667}
668
Richard Hughes0eb123b2018-04-19 12:00:04 +0100669void
670fu_plugin_set_runtime_versions (FuPlugin *plugin, GHashTable *runtime_versions)
671{
672 FuPluginPrivate *priv = GET_PRIVATE (plugin);
673 priv->runtime_versions = g_hash_table_ref (runtime_versions);
674}
675
676/**
677 * fu_plugin_add_runtime_version:
678 * @plugin: A #FuPlugin
679 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
680 * @version: A version string, e.g. "1.2.3"
681 *
682 * Sets a runtime version of a specific dependancy.
683 *
684 * Since: 1.0.7
685 **/
686void
687fu_plugin_add_runtime_version (FuPlugin *plugin,
688 const gchar *component_id,
689 const gchar *version)
690{
691 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesb01b4862018-04-20 16:39:48 +0100692 if (priv->runtime_versions == NULL)
693 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100694 g_hash_table_insert (priv->runtime_versions,
695 g_strdup (component_id),
696 g_strdup (version));
697}
698
Richard Hughes34e0dab2018-04-20 16:43:00 +0100699void
700fu_plugin_set_compile_versions (FuPlugin *plugin, GHashTable *compile_versions)
701{
702 FuPluginPrivate *priv = GET_PRIVATE (plugin);
703 priv->compile_versions = g_hash_table_ref (compile_versions);
704}
705
706/**
707 * fu_plugin_add_compile_version:
708 * @plugin: A #FuPlugin
709 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
710 * @version: A version string, e.g. "1.2.3"
711 *
712 * Sets a compile-time version of a specific dependancy.
713 *
714 * Since: 1.0.7
715 **/
716void
717fu_plugin_add_compile_version (FuPlugin *plugin,
718 const gchar *component_id,
719 const gchar *version)
720{
721 FuPluginPrivate *priv = GET_PRIVATE (plugin);
722 if (priv->compile_versions == NULL)
723 return;
724 g_hash_table_insert (priv->compile_versions,
725 g_strdup (component_id),
726 g_strdup (version));
727}
728
Richard Hughes9c028f02017-10-28 21:14:28 +0100729/**
730 * fu_plugin_lookup_quirk_by_id:
731 * @plugin: A #FuPlugin
732 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
733 * @id: An ID to match the entry, e.g. "012345"
734 *
735 * Looks up an entry in the hardware database using a string value.
736 *
737 * Returns: (transfer none): values from the database, or %NULL if not found
738 *
739 * Since: 1.0.1
740 **/
741const gchar *
742fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *prefix, const gchar *id)
743{
744 FuPluginPrivate *priv = GET_PRIVATE (plugin);
745 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
746
747 /* wildcard */
748 if (g_strstr_len (id, -1, "*") != NULL)
749 return fu_quirks_lookup_by_glob (priv->quirks, prefix, id);
750
751 /* exact ID */
752 return fu_quirks_lookup_by_id (priv->quirks, prefix, id);
753}
754
755/**
756 * fu_plugin_lookup_quirk_by_usb_device:
757 * @plugin: A #FuPlugin
758 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
759 * @dev: A #GUsbDevice
760 *
761 * Looks up an entry in the hardware database using various keys generated
762 * from @dev.
763 *
764 * Returns: (transfer none): values from the database, or %NULL if not found
765 *
766 * Since: 1.0.1
767 **/
768const gchar *
769fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin, const gchar *prefix, GUsbDevice *dev)
770{
771 FuPluginPrivate *priv = GET_PRIVATE (plugin);
772 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
773 return fu_quirks_lookup_by_usb_device (priv->quirks, prefix, dev);
774}
775
Richard Hughes1354ea92017-09-19 15:58:31 +0100776/**
777 * fu_plugin_get_supported:
778 * @plugin: A #FuPlugin
779 *
780 * Gets all the device GUIDs supported by the daemon.
781 *
Richard Hughes4eada342017-10-03 21:20:32 +0100782 * Returns: (element-type utf8) (transfer none): GUIDs
783 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100784 * Since: 1.0.0
785 **/
786GPtrArray *
787fu_plugin_get_supported (FuPlugin *plugin)
788{
789 FuPluginPrivate *priv = GET_PRIVATE (plugin);
790 return priv->supported_guids;
791}
792
793void
Richard Hughes49e5e052017-09-03 12:15:41 +0100794fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios)
795{
796 FuPluginPrivate *priv = GET_PRIVATE (plugin);
797 g_set_object (&priv->smbios, smbios);
798}
799
Richard Hughesb8f8db22017-04-25 15:56:00 +0100800/**
Richard Hughesb0829032017-01-10 09:27:08 +0000801 * fu_plugin_set_coldplug_delay:
802 * @plugin: A #FuPlugin
803 * @duration: A delay in milliseconds
804 *
805 * Set the minimum time that should be waited inbetween the call to
806 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
807 * to be the minimum hardware initialisation time from a datasheet.
808 *
809 * It is better to use this function rather than using a sleep() in the plugin
810 * itself as then only one delay is done in the daemon rather than waiting for
811 * each coldplug prepare in a serial way.
812 *
813 * Additionally, very long delays should be avoided as the daemon will be
814 * blocked from processing requests whilst the coldplug delay is being
815 * performed.
816 *
817 * Since: 0.8.0
818 **/
819void
820fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration)
821{
822 g_return_if_fail (FU_IS_PLUGIN (plugin));
823 g_return_if_fail (duration > 0);
824
825 /* check sanity */
826 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
827 g_warning ("duration of %ums is crazy, truncating to %ums",
828 duration,
829 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
830 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
831 }
832
833 /* emit */
834 g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
835}
836
Richard Hughesd0905142016-03-13 09:46:49 +0000837gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000838fu_plugin_runner_startup (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000839{
Richard Hughescff38bc2016-12-12 12:03:37 +0000840 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000841 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000842
843 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000844 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000845 return TRUE;
846
Richard Hughes639da472018-01-06 22:35:04 +0000847 /* no object loaded */
848 if (priv->module == NULL)
849 return TRUE;
850
Richard Hughesd0905142016-03-13 09:46:49 +0000851 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000852 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
853 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000854 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000855 g_debug ("performing startup() on %s", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +0000856 if (!func (plugin, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000857 g_prefix_error (error, "failed to startup %s: ", priv->name);
858 return FALSE;
859 }
860 return TRUE;
861}
862
863static gboolean
864fu_plugin_runner_offline_invalidate (GError **error)
865{
866 g_autoptr(GError) error_local = NULL;
867 g_autoptr(GFile) file1 = NULL;
868
869 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
870
871 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
872 if (!g_file_query_exists (file1, NULL))
873 return TRUE;
874 if (!g_file_delete (file1, NULL, &error_local)) {
875 g_set_error (error,
876 FWUPD_ERROR,
877 FWUPD_ERROR_INTERNAL,
878 "Cannot delete %s: %s",
879 FU_OFFLINE_TRIGGER_FILENAME,
880 error_local->message);
881 return FALSE;
882 }
883 return TRUE;
884}
885
886static gboolean
887fu_plugin_runner_offline_setup (GError **error)
888{
889 gint rc;
890
891 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
892
893 /* create symlink for the systemd-system-update-generator */
894 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
895 if (rc < 0) {
896 g_set_error (error,
897 FWUPD_ERROR,
898 FWUPD_ERROR_INTERNAL,
899 "Failed to create symlink %s to %s: %s",
900 FU_OFFLINE_TRIGGER_FILENAME,
901 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000902 return FALSE;
903 }
904 return TRUE;
905}
906
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000907static gboolean
908fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device,
909 const gchar *symbol_name, GError **error)
910{
911 FuPluginPrivate *priv = GET_PRIVATE (plugin);
912 FuPluginDeviceFunc func = NULL;
913
914 /* not enabled */
915 if (!priv->enabled)
916 return TRUE;
917
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000918 /* no object loaded */
919 if (priv->module == NULL)
920 return TRUE;
921
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000922 /* optional */
923 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
924 if (func == NULL)
925 return TRUE;
926 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
927 if (!func (plugin, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000928 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000929 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000930 priv->name);
931 return FALSE;
932 }
933 return TRUE;
934}
935
Richard Hughesd0905142016-03-13 09:46:49 +0000936gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000937fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000938{
Richard Hughescff38bc2016-12-12 12:03:37 +0000939 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000940 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000941
942 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000943 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000944 return TRUE;
945
Richard Hughes639da472018-01-06 22:35:04 +0000946 /* no object loaded */
947 if (priv->module == NULL)
948 return TRUE;
949
Richard Hughesd0905142016-03-13 09:46:49 +0000950 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000951 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
952 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000953 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000954 g_debug ("performing coldplug() on %s", priv->name);
955 if (!func (plugin, error)) {
956 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
957 return FALSE;
958 }
959 return TRUE;
960}
961
Richard Hughes7b8b2022016-12-12 16:15:03 +0000962gboolean
Richard Hughes2de8f132018-01-17 09:12:02 +0000963fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error)
964{
965 FuPluginPrivate *priv = GET_PRIVATE (plugin);
966 FuPluginStartupFunc func = NULL;
967
968 /* not enabled */
969 if (!priv->enabled)
970 return TRUE;
971
972 /* no object loaded */
973 if (priv->module == NULL)
974 return TRUE;
975
976 /* optional */
977 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
978 if (func == NULL)
979 return TRUE;
980 g_debug ("performing recoldplug() on %s", priv->name);
981 if (!func (plugin, error)) {
982 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
983 return FALSE;
984 }
985 return TRUE;
986}
987
988gboolean
Richard Hughes46487c92017-01-07 21:26:34 +0000989fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error)
990{
991 FuPluginPrivate *priv = GET_PRIVATE (plugin);
992 FuPluginStartupFunc func = NULL;
993
994 /* not enabled */
995 if (!priv->enabled)
996 return TRUE;
997
Richard Hughes639da472018-01-06 22:35:04 +0000998 /* no object loaded */
999 if (priv->module == NULL)
1000 return TRUE;
1001
Richard Hughes46487c92017-01-07 21:26:34 +00001002 /* optional */
1003 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1004 if (func == NULL)
1005 return TRUE;
1006 g_debug ("performing coldplug_prepare() on %s", priv->name);
1007 if (!func (plugin, error)) {
1008 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
1009 return FALSE;
1010 }
1011 return TRUE;
1012}
1013
1014gboolean
1015fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error)
1016{
1017 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1018 FuPluginStartupFunc func = NULL;
1019
1020 /* not enabled */
1021 if (!priv->enabled)
1022 return TRUE;
1023
Richard Hughes639da472018-01-06 22:35:04 +00001024 /* no object loaded */
1025 if (priv->module == NULL)
1026 return TRUE;
1027
Richard Hughes46487c92017-01-07 21:26:34 +00001028 /* optional */
1029 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1030 if (func == NULL)
1031 return TRUE;
1032 g_debug ("performing coldplug_cleanup() on %s", priv->name);
1033 if (!func (plugin, error)) {
1034 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
1035 return FALSE;
1036 }
1037 return TRUE;
1038}
1039
1040gboolean
Richard Hughes7b8b2022016-12-12 16:15:03 +00001041fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error)
1042{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001043 return fu_plugin_runner_device_generic (plugin, device,
1044 "fu_plugin_update_prepare", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001045}
1046
1047gboolean
1048fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error)
1049{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001050 return fu_plugin_runner_device_generic (plugin, device,
1051 "fu_plugin_update_cleanup", error);
1052}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001053
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001054gboolean
1055fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error)
1056{
1057 return fu_plugin_runner_device_generic (plugin, device,
1058 "fu_plugin_update_attach", error);
1059}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001060
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001061gboolean
1062fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error)
1063{
1064 return fu_plugin_runner_device_generic (plugin, device,
1065 "fu_plugin_update_detach", error);
1066}
1067
1068gboolean
1069fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error)
1070{
1071 return fu_plugin_runner_device_generic (plugin, device,
1072 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001073}
1074
Richard Hughes104f6512017-11-24 11:44:57 +00001075gboolean
1076fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error)
1077{
1078 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1079 FuPluginUsbDeviceAddedFunc func = NULL;
1080
1081 /* not enabled */
1082 if (!priv->enabled)
1083 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001084
1085 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001086 if (priv->module == NULL)
1087 return TRUE;
1088
1089 /* optional */
1090 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1091 if (func != NULL) {
1092 g_debug ("performing usb_device_added() on %s", priv->name);
1093 return func (plugin, usb_device, error);
1094 }
1095 return TRUE;
1096}
1097
Richard Hughese1fd34d2017-08-24 14:19:51 +01001098void
1099fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device)
1100{
1101 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1102 FuPluginDeviceRegisterFunc func = NULL;
1103
1104 /* not enabled */
1105 if (!priv->enabled)
1106 return;
Richard Hughes34834102017-11-21 21:55:00 +00001107 if (priv->module == NULL)
1108 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001109
1110 /* optional */
1111 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1112 if (func != NULL) {
1113 g_debug ("performing device_added() on %s", priv->name);
1114 func (plugin, device);
1115 }
1116}
1117
Richard Hughescff38bc2016-12-12 12:03:37 +00001118static gboolean
1119fu_plugin_runner_schedule_update (FuPlugin *plugin,
1120 FuDevice *device,
1121 GBytes *blob_cab,
1122 GError **error)
1123{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001124 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001125 gchar tmpname[] = {"XXXXXX.cap"};
1126 g_autofree gchar *dirname = NULL;
1127 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001128 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001129 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001130 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001131 g_autoptr(GFile) file = NULL;
1132
1133 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001134 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001135 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001136 if (res_tmp != NULL) {
1137 g_set_error (error,
1138 FWUPD_ERROR,
1139 FWUPD_ERROR_ALREADY_PENDING,
1140 "%s is already scheduled to be updated",
1141 fu_device_get_id (device));
1142 return FALSE;
1143 }
1144
1145 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001146 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001147 file = g_file_new_for_path (dirname);
1148 if (!g_file_query_exists (file, NULL)) {
1149 if (!g_file_make_directory_with_parents (file, NULL, error))
1150 return FALSE;
1151 }
1152
1153 /* get a random filename */
1154 for (guint i = 0; i < 6; i++)
1155 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1156 filename = g_build_filename (dirname, tmpname, NULL);
1157
1158 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001159 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001160 if (!g_file_set_contents (filename,
1161 g_bytes_get_data (blob_cab, NULL),
1162 (gssize) g_bytes_get_size (blob_cab),
1163 error))
1164 return FALSE;
1165
1166 /* schedule for next boot */
1167 g_debug ("schedule %s to be installed to %s on next boot",
1168 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001169 release = fu_device_get_release_default (device);
1170 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1171 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001172
1173 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001174 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001175 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001176 return FALSE;
1177
1178 /* next boot we run offline */
1179 return fu_plugin_runner_offline_setup (error);
1180}
1181
1182gboolean
1183fu_plugin_runner_verify (FuPlugin *plugin,
1184 FuDevice *device,
1185 FuPluginVerifyFlags flags,
1186 GError **error)
1187{
1188 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001189 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001190 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001191
1192 /* not enabled */
1193 if (!priv->enabled)
1194 return TRUE;
1195
Richard Hughes639da472018-01-06 22:35:04 +00001196 /* no object loaded */
1197 if (priv->module == NULL)
1198 return TRUE;
1199
Richard Hughesababbb72017-06-15 20:18:36 +01001200 /* clear any existing verification checksums */
1201 checksums = fu_device_get_checksums (device);
1202 g_ptr_array_set_size (checksums, 0);
1203
Richard Hughescff38bc2016-12-12 12:03:37 +00001204 /* optional */
1205 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1206 if (func == NULL)
1207 return TRUE;
1208 g_debug ("performing verify() on %s", priv->name);
1209 if (!func (plugin, device, flags, error)) {
1210 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001211 return FALSE;
1212 }
1213 return TRUE;
1214}
1215
Richard Hughesd0905142016-03-13 09:46:49 +00001216gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001217fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001218{
Richard Hughescff38bc2016-12-12 12:03:37 +00001219 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001220
1221 /* final check */
1222 flags = fu_device_get_flags (device);
1223 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1224 g_set_error (error,
1225 FWUPD_ERROR,
1226 FWUPD_ERROR_NOT_SUPPORTED,
1227 "Device %s is not locked",
1228 fu_device_get_id (device));
1229 return FALSE;
1230 }
1231
Richard Hughes9c4b5312017-11-14 11:34:53 +00001232 /* run vfunc */
1233 if (!fu_plugin_runner_device_generic (plugin, device,
1234 "fu_plugin_unlock", error))
1235 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001236
1237 /* update with correct flags */
1238 flags = fu_device_get_flags (device);
1239 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1240 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1241 return TRUE;
1242}
1243
1244gboolean
1245fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001246 FuDevice *device,
1247 GBytes *blob_cab,
1248 GBytes *blob_fw,
1249 FwupdInstallFlags flags,
1250 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001251{
1252 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001253 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001254 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001255 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001256 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001257 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001258
1259 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001260 if (!priv->enabled) {
1261 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001262 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001263 }
Richard Hughesd0905142016-03-13 09:46:49 +00001264
Richard Hughes639da472018-01-06 22:35:04 +00001265 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001266 if (priv->module == NULL) {
1267 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001268 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001269 }
Richard Hughes639da472018-01-06 22:35:04 +00001270
Richard Hughesd0905142016-03-13 09:46:49 +00001271 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001272 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1273 if (update_func == NULL) {
1274 g_set_error_literal (error,
1275 FWUPD_ERROR,
1276 FWUPD_ERROR_NOT_SUPPORTED,
1277 "No update possible");
1278 return FALSE;
1279 }
Richard Hughesd0905142016-03-13 09:46:49 +00001280
Richard Hughesa785a1c2017-08-25 16:00:58 +01001281 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001282 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001283 if (blob_cab == NULL) {
1284 g_set_error_literal (error,
1285 FWUPD_ERROR,
1286 FWUPD_ERROR_NOT_SUPPORTED,
1287 "No cabinet archive to schedule");
1288 return FALSE;
1289 }
Richard Hughesa785a1c2017-08-25 16:00:58 +01001290 return fu_plugin_runner_schedule_update (plugin,
1291 device,
1292 blob_cab,
1293 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001294 }
1295
1296 /* cancel the pending action */
1297 if (!fu_plugin_runner_offline_invalidate (error))
1298 return FALSE;
1299
1300 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001301 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001302 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001303 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughesc0cd0232018-01-31 15:02:00 +00001304 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001305 g_propagate_error (error, error_update);
1306 return FALSE;
1307 }
1308
Richard Hughesf556d372017-06-15 19:49:18 +01001309 /* no longer valid */
1310 checksums = fu_device_get_checksums (device);
1311 g_ptr_array_set_size (checksums, 0);
1312
Richard Hughescff38bc2016-12-12 12:03:37 +00001313 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001314 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001315 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001316 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001317
Richard Hughes780ef3f2018-01-12 16:20:31 +00001318 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001319 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1320 if (!fu_history_modify_device (history, device,
1321 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1322 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001323 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001324
1325 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001326 release = fu_device_get_release_default (device_pending);
1327 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001328 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1329 g_autoptr(GError) error_local = NULL;
1330 g_autoptr(GFile) file = NULL;
1331 file = g_file_new_for_path (tmp);
1332 if (!g_file_delete (file, NULL, &error_local)) {
1333 g_set_error (error,
1334 FWUPD_ERROR,
1335 FWUPD_ERROR_INVALID_FILE,
1336 "Failed to delete %s: %s",
1337 tmp, error_local->message);
1338 return FALSE;
1339 }
1340 }
1341 }
Richard Hughesd0905142016-03-13 09:46:49 +00001342 return TRUE;
1343}
Richard Hughescff38bc2016-12-12 12:03:37 +00001344
1345gboolean
1346fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error)
1347{
1348 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001349 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001350
1351 /* not enabled */
1352 if (!priv->enabled)
1353 return TRUE;
1354
Richard Hughes639da472018-01-06 22:35:04 +00001355 /* no object loaded */
1356 if (priv->module == NULL)
1357 return TRUE;
1358
Richard Hughes65e44ca2018-01-30 17:26:30 +00001359 /* optional */
1360 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1361 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001362 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001363 g_debug ("performing clear_result() on %s", priv->name);
1364 if (!func (plugin, device, error)) {
1365 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001366 return FALSE;
1367 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001368 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001369}
1370
1371gboolean
1372fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1373{
1374 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001375 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001376
1377 /* not enabled */
1378 if (!priv->enabled)
1379 return TRUE;
1380
Richard Hughes639da472018-01-06 22:35:04 +00001381 /* no object loaded */
1382 if (priv->module == NULL)
1383 return TRUE;
1384
Richard Hughes65e44ca2018-01-30 17:26:30 +00001385 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001386 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001387 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001388 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001389 g_debug ("performing get_results() on %s", priv->name);
1390 if (!func (plugin, device, error)) {
1391 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001392 return FALSE;
1393 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001394 return TRUE;
1395}
1396
Richard Hughes08a37992017-09-12 12:57:43 +01001397/**
1398 * fu_plugin_get_order:
1399 * @plugin: a #FuPlugin
1400 *
1401 * Gets the plugin order, where higher numbers are run after lower
1402 * numbers.
1403 *
1404 * Returns: the integer value
1405 **/
1406guint
1407fu_plugin_get_order (FuPlugin *plugin)
1408{
1409 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1410 return priv->order;
1411}
1412
1413/**
1414 * fu_plugin_set_order:
1415 * @plugin: a #FuPlugin
1416 * @order: a integer value
1417 *
1418 * Sets the plugin order, where higher numbers are run after lower
1419 * numbers.
1420 **/
1421void
1422fu_plugin_set_order (FuPlugin *plugin, guint order)
1423{
1424 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1425 priv->order = order;
1426}
1427
1428/**
1429 * fu_plugin_add_rule:
1430 * @plugin: a #FuPlugin
1431 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001432 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001433 *
1434 * If the plugin name is found, the rule will be used to sort the plugin list,
1435 * for example the plugin specified by @name will be ordered after this plugin
1436 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1437 *
1438 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1439 * If depsolving fails then fwupd will not start.
1440 **/
1441void
1442fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1443{
1444 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1445 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1446}
1447
1448/**
1449 * fu_plugin_get_rules:
1450 * @plugin: a #FuPlugin
1451 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1452 *
1453 * Gets the plugin IDs that should be run after this plugin.
1454 *
1455 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1456 **/
1457GPtrArray *
1458fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1459{
1460 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1461 return priv->rules[rule];
1462}
1463
Richard Hughes80b79bb2018-01-11 21:11:06 +00001464/**
1465 * fu_plugin_add_report_metadata:
1466 * @plugin: a #FuPlugin
1467 * @key: a string, e.g. `FwupdateVersion`
1468 * @value: a string, e.g. `10`
1469 *
1470 * Sets any additional metadata to be included in the firmware report to aid
1471 * debugging problems.
1472 *
1473 * Any data included here will be sent to the metadata server after user
1474 * confirmation.
1475 **/
1476void
1477fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1478{
1479 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1480 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1481}
1482
1483/**
1484 * fu_plugin_get_report_metadata:
1485 * @plugin: a #FuPlugin
1486 *
1487 * Returns the list of additional metadata to be added when filing a report.
1488 *
1489 * Returns: (transfer none): the map of report metadata
1490 **/
1491GHashTable *
1492fu_plugin_get_report_metadata (FuPlugin *plugin)
1493{
1494 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1495 return priv->report_metadata;
1496}
1497
Mario Limonciello963dc422018-02-27 14:26:58 -06001498/**
1499 * fu_plugin_get_config_value:
1500 * @plugin: a #FuPlugin
1501 * @key: A settings key
1502 *
1503 * Return the value of a key if it's been configured
1504 *
1505 * Since: 1.0.6
1506 **/
1507gchar *
1508fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key)
1509{
Richard Hughes4be17d12018-05-30 20:36:29 +01001510 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06001511 g_autofree gchar *conf_file = NULL;
1512 g_autofree gchar *conf_path = NULL;
1513 g_autoptr(GKeyFile) keyfile = NULL;
1514 const gchar *plugin_name;
1515
Richard Hughes4be17d12018-05-30 20:36:29 +01001516 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Mario Limonciello963dc422018-02-27 14:26:58 -06001517 plugin_name = fu_plugin_get_name (plugin);
1518 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01001519 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06001520 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1521 return NULL;
1522 keyfile = g_key_file_new ();
1523 if (!g_key_file_load_from_file (keyfile, conf_path,
1524 G_KEY_FILE_NONE, NULL))
1525 return NULL;
1526 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1527}
1528
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001529/**
1530 * fu_plugin_name_compare:
1531 * @plugin1: first #FuPlugin to compare.
1532 * @plugin2: second #FuPlugin to compare.
1533 *
1534 * Compares two plugins by their names.
1535 *
1536 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1537 **/
1538gint
1539fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1540{
1541 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1542 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1543 return g_strcmp0 (priv1->name, priv2->name);
1544}
1545
1546/**
1547 * fu_plugin_order_compare:
1548 * @plugin1: first #FuPlugin to compare.
1549 * @plugin2: second #FuPlugin to compare.
1550 *
1551 * Compares two plugins by their depsolved order.
1552 *
1553 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1554 **/
1555gint
1556fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1557{
1558 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1559 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1560 if (priv1->order < priv2->order)
1561 return -1;
1562 if (priv1->order > priv2->order)
1563 return 1;
1564 return 0;
1565}
1566
Richard Hughescff38bc2016-12-12 12:03:37 +00001567static void
1568fu_plugin_class_init (FuPluginClass *klass)
1569{
1570 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1571 object_class->finalize = fu_plugin_finalize;
1572 signals[SIGNAL_DEVICE_ADDED] =
1573 g_signal_new ("device-added",
1574 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1575 G_STRUCT_OFFSET (FuPluginClass, device_added),
1576 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1577 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1578 signals[SIGNAL_DEVICE_REMOVED] =
1579 g_signal_new ("device-removed",
1580 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1581 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1582 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1583 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001584 signals[SIGNAL_DEVICE_REGISTER] =
1585 g_signal_new ("device-register",
1586 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1587 G_STRUCT_OFFSET (FuPluginClass, device_register),
1588 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1589 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001590 signals[SIGNAL_RECOLDPLUG] =
1591 g_signal_new ("recoldplug",
1592 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1593 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1594 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1595 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001596 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1597 g_signal_new ("set-coldplug-delay",
1598 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1599 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1600 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1601 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001602}
1603
1604static void
1605fu_plugin_init (FuPlugin *plugin)
1606{
1607 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1608 priv->enabled = TRUE;
1609 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1610 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001611 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001612 priv->report_metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
Richard Hughes08a37992017-09-12 12:57:43 +01001613 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1614 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001615}
1616
1617static void
1618fu_plugin_finalize (GObject *object)
1619{
1620 FuPlugin *plugin = FU_PLUGIN (object);
1621 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1622 FuPluginInitFunc func = NULL;
1623
1624 /* optional */
1625 if (priv->module != NULL) {
1626 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1627 if (func != NULL) {
1628 g_debug ("performing destroy() on %s", priv->name);
1629 func (plugin);
1630 }
1631 }
1632
Richard Hughes08a37992017-09-12 12:57:43 +01001633 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1634 g_ptr_array_unref (priv->rules[i]);
1635
Richard Hughescff38bc2016-12-12 12:03:37 +00001636 if (priv->usb_ctx != NULL)
1637 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001638 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001639 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001640 if (priv->quirks != NULL)
1641 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001642 if (priv->supported_guids != NULL)
1643 g_ptr_array_unref (priv->supported_guids);
Richard Hughes49e5e052017-09-03 12:15:41 +01001644 if (priv->smbios != NULL)
1645 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001646 if (priv->runtime_versions != NULL)
1647 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001648 if (priv->compile_versions != NULL)
1649 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00001650 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001651 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001652 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001653 g_free (priv->name);
1654 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05001655 /* Must happen as the last step to avoid prematurely
1656 * freeing memory held by the plugin */
1657#ifndef RUNNING_ON_VALGRIND
1658 if (priv->module != NULL)
1659 g_module_close (priv->module);
1660#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001661
1662 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1663}
1664
1665FuPlugin *
1666fu_plugin_new (void)
1667{
1668 FuPlugin *plugin;
1669 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1670 return plugin;
1671}