blob: e3077b8074601a3ab2cc2faf8e6dcd28e3c5194c [file] [log] [blame]
Richard Hughes02c90d82018-08-09 12:13:03 +01001/*
Richard Hughes2de8f132018-01-17 09:12:02 +00002 * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com>
Richard Hughesd0905142016-03-13 09:46:49 +00003 *
Mario Limonciello51308e62018-05-28 20:05:46 -05004 * SPDX-License-Identifier: LGPL-2.1+
Richard Hughesd0905142016-03-13 09:46:49 +00005 */
6
7#include "config.h"
8
Richard Hughescff38bc2016-12-12 12:03:37 +00009#include <fwupd.h>
10#include <gmodule.h>
11#include <appstream-glib.h>
12#include <errno.h>
13#include <string.h>
Richard Hughes68f12dd2018-08-09 14:43:31 +010014#include <unistd.h>
Richard Hughescff38bc2016-12-12 12:03:37 +000015#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;
Richard Hughes81c427c2018-08-06 15:20:17 +010042 guint priority;
Richard Hughes08a37992017-09-12 12:57:43 +010043 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughescff38bc2016-12-12 12:03:37 +000044 gchar *name;
Richard Hughesd7704d42017-08-08 20:29:09 +010045 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010046 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010047 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010048 GHashTable *compile_versions;
Richard Hughes1354ea92017-09-19 15:58:31 +010049 GPtrArray *supported_guids;
50 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000051 GHashTable *devices; /* platform_id:GObject */
Richard Hughesae3d65f2016-12-16 09:38:01 +000052 GHashTable *devices_delay; /* FuDevice:FuPluginHelper */
Richard Hughes80b79bb2018-01-11 21:11:06 +000053 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000054 FuPluginData *data;
55} FuPluginPrivate;
56
57enum {
58 SIGNAL_DEVICE_ADDED,
59 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010060 SIGNAL_DEVICE_REGISTER,
Richard Hughes362d6d72017-01-07 21:42:14 +000061 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000062 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughescff38bc2016-12-12 12:03:37 +000063 SIGNAL_LAST
64};
65
66static guint signals[SIGNAL_LAST] = { 0 };
67
68G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
69#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
70
71typedef const gchar *(*FuPluginGetNameFunc) (void);
72typedef void (*FuPluginInitFunc) (FuPlugin *plugin);
73typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin,
74 GError **error);
Richard Hughese1fd34d2017-08-24 14:19:51 +010075typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin,
76 FuDevice *device);
Richard Hughescff38bc2016-12-12 12:03:37 +000077typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin,
78 FuDevice *device,
79 GError **error);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -050080typedef gboolean (*FuPluginFlaggedDeviceFunc) (FuPlugin *plugin,
81 FwupdInstallFlags flags,
82 FuDevice *device,
83 GError **error);
Richard Hughesdbd8c762018-06-15 20:31:40 +010084typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *plugin,
85 GPtrArray *devices,
86 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +000087typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin,
88 FuDevice *device,
89 FuPluginVerifyFlags flags,
90 GError **error);
91typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin,
92 FuDevice *device,
93 GBytes *blob_fw,
94 FwupdInstallFlags flags,
95 GError **error);
Richard Hughes104f6512017-11-24 11:44:57 +000096typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin,
97 GUsbDevice *usb_device,
98 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +000099
Richard Hughes57d18222017-01-10 16:02:59 +0000100/**
101 * fu_plugin_get_name:
102 * @plugin: A #FuPlugin
103 *
104 * Gets the plugin name.
105 *
106 * Returns: a plugin name, or %NULL for unknown.
107 *
108 * Since: 0.8.0
109 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000110const gchar *
111fu_plugin_get_name (FuPlugin *plugin)
Richard Hughesd0905142016-03-13 09:46:49 +0000112{
Richard Hughescff38bc2016-12-12 12:03:37 +0000113 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000114 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000115 return priv->name;
116}
Richard Hughesd0905142016-03-13 09:46:49 +0000117
Richard Hughes34834102017-11-21 21:55:00 +0000118void
119fu_plugin_set_name (FuPlugin *plugin, const gchar *name)
120{
121 FuPluginPrivate *priv = GET_PRIVATE (plugin);
122 g_return_if_fail (FU_IS_PLUGIN (plugin));
123 g_return_if_fail (name != NULL);
124 g_free (priv->name);
125 priv->name = g_strdup (name);
126}
127
Richard Hughes57d18222017-01-10 16:02:59 +0000128/**
129 * fu_plugin_cache_lookup:
130 * @plugin: A #FuPlugin
131 * @id: the key
132 *
133 * Finds an object in the per-plugin cache.
134 *
135 * Returns: (transfer none): a #GObject, or %NULL for unfound.
136 *
137 * Since: 0.8.0
138 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000139gpointer
140fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id)
141{
142 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000143 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
144 g_return_val_if_fail (id != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000145 return g_hash_table_lookup (priv->devices, id);
146}
Richard Hughesd0905142016-03-13 09:46:49 +0000147
Richard Hughes57d18222017-01-10 16:02:59 +0000148/**
149 * fu_plugin_cache_add:
150 * @plugin: A #FuPlugin
151 * @id: the key
152 * @dev: a #GObject, typically a #FuDevice
153 *
154 * Adds an object to the per-plugin cache.
155 *
156 * Since: 0.8.0
157 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000158void
159fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev)
160{
161 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000162 g_return_if_fail (FU_IS_PLUGIN (plugin));
163 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000164 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
165}
166
Richard Hughes57d18222017-01-10 16:02:59 +0000167/**
168 * fu_plugin_cache_remove:
169 * @plugin: A #FuPlugin
170 * @id: the key
171 *
172 * Removes an object from the per-plugin cache.
173 *
174 * Since: 0.8.0
175 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000176void
177fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id)
178{
179 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000180 g_return_if_fail (FU_IS_PLUGIN (plugin));
181 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000182 g_hash_table_remove (priv->devices, id);
183}
184
Richard Hughes57d18222017-01-10 16:02:59 +0000185/**
186 * fu_plugin_get_data:
187 * @plugin: A #FuPlugin
188 *
Richard Hughes4eada342017-10-03 21:20:32 +0100189 * Gets the per-plugin allocated private data. This will return %NULL unless
190 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000191 *
Richard Hughes4eada342017-10-03 21:20:32 +0100192 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000193 *
194 * Since: 0.8.0
195 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000196FuPluginData *
197fu_plugin_get_data (FuPlugin *plugin)
198{
199 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000200 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000201 return priv->data;
202}
203
Richard Hughes57d18222017-01-10 16:02:59 +0000204/**
205 * fu_plugin_alloc_data:
206 * @plugin: A #FuPlugin
207 * @data_sz: the size to allocate
208 *
209 * Allocates the per-plugin allocated private data.
210 *
211 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
212 *
213 * Since: 0.8.0
214 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000215FuPluginData *
216fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz)
217{
218 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000219 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000220 if (priv->data != NULL) {
221 g_critical ("fu_plugin_alloc_data() already used by plugin");
222 return priv->data;
223 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000224 priv->data = g_malloc0 (data_sz);
225 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000226}
227
Richard Hughes57d18222017-01-10 16:02:59 +0000228/**
229 * fu_plugin_get_usb_context:
230 * @plugin: A #FuPlugin
231 *
232 * Gets the shared USB context that all plugins can use.
233 *
234 * Returns: (transfer none): a #GUsbContext.
235 *
236 * Since: 0.8.0
237 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000238GUsbContext *
239fu_plugin_get_usb_context (FuPlugin *plugin)
240{
Richard Hughescff38bc2016-12-12 12:03:37 +0000241 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000242 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000243 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000244}
245
246void
247fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx)
248{
Richard Hughescff38bc2016-12-12 12:03:37 +0000249 FuPluginPrivate *priv = GET_PRIVATE (plugin);
250 g_set_object (&priv->usb_ctx, usb_ctx);
251}
252
Richard Hughes57d18222017-01-10 16:02:59 +0000253/**
254 * fu_plugin_get_enabled:
255 * @plugin: A #FuPlugin
256 *
Richard Hughes4eada342017-10-03 21:20:32 +0100257 * Returns if the plugin is enabled. Plugins may self-disable using
258 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000259 *
260 * Returns: %TRUE if the plugin is currently enabled.
261 *
262 * Since: 0.8.0
263 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000264gboolean
265fu_plugin_get_enabled (FuPlugin *plugin)
266{
267 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000268 g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000269 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000270}
271
Richard Hughes57d18222017-01-10 16:02:59 +0000272/**
273 * fu_plugin_set_enabled:
274 * @plugin: A #FuPlugin
275 * @enabled: the enabled value
276 *
277 * Enables or disables a plugin. Plugins can self-disable at any point.
278 *
279 * Since: 0.8.0
280 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000281void
Richard Hughescff38bc2016-12-12 12:03:37 +0000282fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000283{
Richard Hughescff38bc2016-12-12 12:03:37 +0000284 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000285 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughescff38bc2016-12-12 12:03:37 +0000286 priv->enabled = enabled;
287}
288
Richard Hughes1e456bc2018-05-10 20:16:16 +0100289gchar *
290fu_plugin_guess_name_from_fn (const gchar *filename)
291{
292 const gchar *prefix = "libfu_plugin_";
293 gchar *name;
294 gchar *str = g_strstr_len (filename, -1, prefix);
295 if (str == NULL)
296 return NULL;
297 name = g_strdup (str + strlen (prefix));
298 g_strdelimit (name, ".", '\0');
299 return name;
300}
301
Richard Hughescff38bc2016-12-12 12:03:37 +0000302gboolean
303fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error)
304{
305 FuPluginPrivate *priv = GET_PRIVATE (plugin);
306 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000307
308 priv->module = g_module_open (filename, 0);
309 if (priv->module == NULL) {
310 g_set_error (error,
311 G_IO_ERROR,
312 G_IO_ERROR_FAILED,
313 "failed to open plugin: %s",
314 g_module_error ());
315 return FALSE;
316 }
317
318 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100319 if (priv->name == NULL)
320 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000321
322 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000323 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
324 if (func != NULL) {
325 g_debug ("performing init() on %s", filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000326 func (plugin);
327 }
328
Richard Hughescff38bc2016-12-12 12:03:37 +0000329 return TRUE;
330}
331
Richard Hughes57d18222017-01-10 16:02:59 +0000332/**
333 * fu_plugin_device_add:
334 * @plugin: A #FuPlugin
335 * @device: A #FuDevice
336 *
337 * Asks the daemon to add a device to the exported list. If this device ID
338 * has already been added by a different plugin then this request will be
339 * ignored.
340 *
341 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
342 * actually flashing an image to the hardware so that higher-priority plugins
343 * can add the device themselves.
344 *
345 * Since: 0.8.0
346 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000347void
348fu_plugin_device_add (FuPlugin *plugin, FuDevice *device)
349{
Richard Hughes5e447292018-04-27 14:25:54 +0100350 GPtrArray *children;
351
Richard Hughesccd78a92017-01-11 16:57:41 +0000352 g_return_if_fail (FU_IS_PLUGIN (plugin));
353 g_return_if_fail (FU_IS_DEVICE (device));
354
Richard Hughescff38bc2016-12-12 12:03:37 +0000355 g_debug ("emit added from %s: %s",
356 fu_plugin_get_name (plugin),
357 fu_device_get_id (device));
358 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
359 fu_device_set_plugin (device, fu_plugin_get_name (plugin));
360 g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100361
Richard Hughes128c0162018-08-10 11:00:29 +0100362 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100363 children = fu_device_get_children (device);
364 for (guint i = 0; i < children->len; i++) {
365 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100366 if (fu_device_get_created (child) == 0)
367 fu_plugin_device_add (plugin, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100368 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000369}
370
Richard Hughese1fd34d2017-08-24 14:19:51 +0100371/**
372 * fu_plugin_device_register:
373 * @plugin: A #FuPlugin
374 * @device: A #FuDevice
375 *
376 * Registers the device with other plugins so they can set metadata.
377 *
378 * Plugins do not have to call this manually as this is done automatically
379 * when using fu_plugin_device_add(). They may wish to use this manually
380 * if for intance the coldplug should be ignored based on the metadata
381 * set from other plugins.
382 *
383 * Since: 0.9.7
384 **/
385void
386fu_plugin_device_register (FuPlugin *plugin, FuDevice *device)
387{
388 g_return_if_fail (FU_IS_PLUGIN (plugin));
389 g_return_if_fail (FU_IS_DEVICE (device));
390
391 g_debug ("emit device-register from %s: %s",
392 fu_plugin_get_name (plugin),
393 fu_device_get_id (device));
394 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device);
395}
396
Richard Hughesae3d65f2016-12-16 09:38:01 +0000397typedef struct {
398 FuPlugin *plugin;
399 FuDevice *device;
400 guint timeout_id;
Richard Hughesd4184cf2016-12-21 16:05:17 +0000401 GHashTable *devices;
Richard Hughesae3d65f2016-12-16 09:38:01 +0000402} FuPluginHelper;
403
404static void
405fu_plugin_helper_free (FuPluginHelper *helper)
406{
407 g_object_unref (helper->plugin);
408 g_object_unref (helper->device);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000409 g_hash_table_unref (helper->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000410 g_free (helper);
411}
412
413static gboolean
414fu_plugin_device_add_delay_cb (gpointer user_data)
415{
416 FuPluginHelper *helper = (FuPluginHelper *) user_data;
417 fu_plugin_device_add (helper->plugin, helper->device);
Richard Hughes0dec2742017-09-18 11:07:39 +0100418 g_hash_table_remove (helper->devices, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000419 fu_plugin_helper_free (helper);
420 return FALSE;
421}
422
Richard Hughes57d18222017-01-10 16:02:59 +0000423/**
Richard Hughesf0a799e2017-01-17 20:13:30 +0000424 * fu_plugin_has_device_delay:
425 * @plugin: A #FuPlugin
426 *
427 * Returns if the device has a pending device that is waiting to be added.
428 *
429 * Returns: %TRUE if a device is waiting to be added
430 *
431 * Since: 0.8.0
432 **/
433gboolean
434fu_plugin_has_device_delay (FuPlugin *plugin)
435{
436 FuPluginPrivate *priv = GET_PRIVATE (plugin);
437 return g_hash_table_size (priv->devices_delay) > 0;
438}
439
440/**
Richard Hughes57d18222017-01-10 16:02:59 +0000441 * fu_plugin_device_add_delay:
442 * @plugin: A #FuPlugin
443 * @device: A #FuDevice
444 *
445 * Asks the daemon to add a device to the exported list after a small delay.
446 *
447 * Since: 0.8.0
448 **/
Richard Hughesae3d65f2016-12-16 09:38:01 +0000449void
450fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device)
451{
452 FuPluginPrivate *priv = GET_PRIVATE (plugin);
453 FuPluginHelper *helper;
Richard Hughesccd78a92017-01-11 16:57:41 +0000454
455 g_return_if_fail (FU_IS_PLUGIN (plugin));
456 g_return_if_fail (FU_IS_DEVICE (device));
457
Richard Hughes6ad951d2017-02-03 10:12:12 +0000458 /* already waiting for add */
459 helper = g_hash_table_lookup (priv->devices_delay, device);
460 if (helper != NULL) {
Richard Hughes3cca1c62017-07-21 13:38:20 +0100461 g_debug ("ignoring add-delay as device %s already pending",
462 fu_device_get_id (device));
Richard Hughes6ad951d2017-02-03 10:12:12 +0000463 return;
464 }
465
466 /* add after a small delay */
Richard Hughesae3d65f2016-12-16 09:38:01 +0000467 g_debug ("waiting a small time for other plugins");
468 helper = g_new0 (FuPluginHelper, 1);
469 helper->plugin = g_object_ref (plugin);
470 helper->device = g_object_ref (device);
471 helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000472 helper->devices = g_hash_table_ref (priv->devices_delay);
473 g_hash_table_insert (helper->devices, device, helper);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000474}
475
Richard Hughes57d18222017-01-10 16:02:59 +0000476/**
Richard Hughes4eada342017-10-03 21:20:32 +0100477 * fu_plugin_device_remove:
Richard Hughes57d18222017-01-10 16:02:59 +0000478 * @plugin: A #FuPlugin
479 * @device: A #FuDevice
480 *
481 * Asks the daemon to remove a device from the exported list.
482 *
483 * Since: 0.8.0
484 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000485void
486fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device)
487{
Richard Hughesae3d65f2016-12-16 09:38:01 +0000488 FuPluginPrivate *priv = GET_PRIVATE (plugin);
489 FuPluginHelper *helper;
490
Richard Hughesccd78a92017-01-11 16:57:41 +0000491 g_return_if_fail (FU_IS_PLUGIN (plugin));
492 g_return_if_fail (FU_IS_DEVICE (device));
493
Richard Hughesae3d65f2016-12-16 09:38:01 +0000494 /* waiting for add */
495 helper = g_hash_table_lookup (priv->devices_delay, device);
496 if (helper != NULL) {
497 g_debug ("ignoring remove from delayed addition");
498 g_source_remove (helper->timeout_id);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000499 g_hash_table_remove (priv->devices_delay, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000500 fu_plugin_helper_free (helper);
501 return;
502 }
503
Richard Hughescff38bc2016-12-12 12:03:37 +0000504 g_debug ("emit removed from %s: %s",
505 fu_plugin_get_name (plugin),
506 fu_device_get_id (device));
507 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device);
508}
509
Richard Hughes57d18222017-01-10 16:02:59 +0000510/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000511 * fu_plugin_request_recoldplug:
Richard Hughes362d6d72017-01-07 21:42:14 +0000512 * @plugin: A #FuPlugin
513 *
514 * Ask all the plugins to coldplug all devices, which will include the prepare()
515 * and cleanup() phases. Duplicate devices added will be ignored.
516 *
517 * Since: 0.8.0
518 **/
519void
Richard Hughes2de8f132018-01-17 09:12:02 +0000520fu_plugin_request_recoldplug (FuPlugin *plugin)
Richard Hughes362d6d72017-01-07 21:42:14 +0000521{
Richard Hughesccd78a92017-01-11 16:57:41 +0000522 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughes362d6d72017-01-07 21:42:14 +0000523 g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0);
524}
525
Richard Hughesb0829032017-01-10 09:27:08 +0000526/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100527 * fu_plugin_check_hwid:
528 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100529 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100530 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100531 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100532 * specific system can be shown using the `fwupdmgr hwids` command.
533 *
Richard Hughes4eada342017-10-03 21:20:32 +0100534 * Returns: %TRUE if the HwId is found on the system.
535 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100536 * Since: 0.9.1
537 **/
538gboolean
539fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid)
540{
541 FuPluginPrivate *priv = GET_PRIVATE (plugin);
542 if (priv->hwids == NULL)
543 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100544 return fu_hwids_has_guid (priv->hwids, hwid);
545}
546
547/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100548 * fu_plugin_get_hwids:
549 * @plugin: A #FuPlugin
550 *
551 * Returns all the HWIDs defined in the system. All hardware IDs on a
552 * specific system can be shown using the `fwupdmgr hwids` command.
553 *
554 * Returns: (transfer none) (element-type utf-8): An array of GUIDs
555 *
556 * Since: 1.1.1
557 **/
558GPtrArray *
559fu_plugin_get_hwids (FuPlugin *plugin)
560{
561 FuPluginPrivate *priv = GET_PRIVATE (plugin);
562 if (priv->hwids == NULL)
563 return NULL;
564 return fu_hwids_get_guids (priv->hwids);
565}
566
567/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100568 * fu_plugin_check_supported:
569 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100570 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100571 *
572 * Checks to see if a specific device GUID is supported, i.e. available in the
573 * AppStream metadata.
574 *
Richard Hughes4eada342017-10-03 21:20:32 +0100575 * Returns: %TRUE if the device is supported.
576 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100577 * Since: 1.0.0
578 **/
579gboolean
580fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid)
581{
582 FuPluginPrivate *priv = GET_PRIVATE (plugin);
583 if (priv->supported_guids == NULL)
584 return FALSE;
585 for (guint i = 0; i < priv->supported_guids->len; i++) {
586 const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i);
587 if (g_strcmp0 (guid, guid_tmp) == 0)
588 return TRUE;
589 }
590 return FALSE;
591}
592
593/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100594 * fu_plugin_get_dmi_value:
595 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100596 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100597 *
598 * Gets a hardware DMI value.
599 *
Richard Hughes4eada342017-10-03 21:20:32 +0100600 * Returns: The string, or %NULL
601 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100602 * Since: 0.9.7
603 **/
604const gchar *
605fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id)
606{
607 FuPluginPrivate *priv = GET_PRIVATE (plugin);
608 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100609 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100610 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100611}
612
Richard Hughes49e5e052017-09-03 12:15:41 +0100613/**
614 * fu_plugin_get_smbios_string:
615 * @plugin: A #FuPlugin
616 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
617 * @offset: A SMBIOS offset
618 *
619 * Gets a hardware SMBIOS string.
620 *
621 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
622 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
623 *
Richard Hughes4eada342017-10-03 21:20:32 +0100624 * Returns: A string, or %NULL
625 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100626 * Since: 0.9.8
627 **/
628const gchar *
629fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset)
630{
631 FuPluginPrivate *priv = GET_PRIVATE (plugin);
632 if (priv->smbios == NULL)
633 return NULL;
634 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
635}
636
637/**
638 * fu_plugin_get_smbios_data:
639 * @plugin: A #FuPlugin
640 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
641 *
642 * Gets a hardware SMBIOS data.
643 *
Richard Hughes4eada342017-10-03 21:20:32 +0100644 * Returns: (transfer none): A #GBytes, or %NULL
645 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100646 * Since: 0.9.8
647 **/
648GBytes *
649fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type)
650{
651 FuPluginPrivate *priv = GET_PRIVATE (plugin);
652 if (priv->smbios == NULL)
653 return NULL;
654 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
655}
656
Richard Hughesb8f8db22017-04-25 15:56:00 +0100657void
Richard Hughesd7704d42017-08-08 20:29:09 +0100658fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100659{
660 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesd7704d42017-08-08 20:29:09 +0100661 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100662}
663
Richard Hughes49e5e052017-09-03 12:15:41 +0100664void
Richard Hughes1354ea92017-09-19 15:58:31 +0100665fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids)
666{
667 FuPluginPrivate *priv = GET_PRIVATE (plugin);
668 if (priv->supported_guids != NULL)
669 g_ptr_array_unref (priv->supported_guids);
670 priv->supported_guids = g_ptr_array_ref (supported_guids);
671}
672
Richard Hughes9c028f02017-10-28 21:14:28 +0100673void
674fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks)
675{
676 FuPluginPrivate *priv = GET_PRIVATE (plugin);
677 g_set_object (&priv->quirks, quirks);
678}
679
680/**
681 * fu_plugin_get_quirks:
682 * @plugin: A #FuPlugin
683 *
684 * Returns the hardware database object. This can be used to discover device
685 * quirks or other device-specific settings.
686 *
687 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
688 *
689 * Since: 1.0.1
690 **/
691FuQuirks *
692fu_plugin_get_quirks (FuPlugin *plugin)
693{
694 FuPluginPrivate *priv = GET_PRIVATE (plugin);
695 return priv->quirks;
696}
697
Richard Hughes0eb123b2018-04-19 12:00:04 +0100698void
699fu_plugin_set_runtime_versions (FuPlugin *plugin, GHashTable *runtime_versions)
700{
701 FuPluginPrivate *priv = GET_PRIVATE (plugin);
702 priv->runtime_versions = g_hash_table_ref (runtime_versions);
703}
704
705/**
706 * fu_plugin_add_runtime_version:
707 * @plugin: A #FuPlugin
708 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
709 * @version: A version string, e.g. "1.2.3"
710 *
711 * Sets a runtime version of a specific dependancy.
712 *
713 * Since: 1.0.7
714 **/
715void
716fu_plugin_add_runtime_version (FuPlugin *plugin,
717 const gchar *component_id,
718 const gchar *version)
719{
720 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesb01b4862018-04-20 16:39:48 +0100721 if (priv->runtime_versions == NULL)
722 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100723 g_hash_table_insert (priv->runtime_versions,
724 g_strdup (component_id),
725 g_strdup (version));
726}
727
Richard Hughes34e0dab2018-04-20 16:43:00 +0100728void
729fu_plugin_set_compile_versions (FuPlugin *plugin, GHashTable *compile_versions)
730{
731 FuPluginPrivate *priv = GET_PRIVATE (plugin);
732 priv->compile_versions = g_hash_table_ref (compile_versions);
733}
734
735/**
736 * fu_plugin_add_compile_version:
737 * @plugin: A #FuPlugin
738 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
739 * @version: A version string, e.g. "1.2.3"
740 *
741 * Sets a compile-time version of a specific dependancy.
742 *
743 * Since: 1.0.7
744 **/
745void
746fu_plugin_add_compile_version (FuPlugin *plugin,
747 const gchar *component_id,
748 const gchar *version)
749{
750 FuPluginPrivate *priv = GET_PRIVATE (plugin);
751 if (priv->compile_versions == NULL)
752 return;
753 g_hash_table_insert (priv->compile_versions,
754 g_strdup (component_id),
755 g_strdup (version));
756}
757
Richard Hughes9c028f02017-10-28 21:14:28 +0100758/**
759 * fu_plugin_lookup_quirk_by_id:
760 * @plugin: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100761 * @group: A string, e.g. "DfuFlags"
762 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100763 *
764 * Looks up an entry in the hardware database using a string value.
765 *
766 * Returns: (transfer none): values from the database, or %NULL if not found
767 *
768 * Since: 1.0.1
769 **/
770const gchar *
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100771fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100772{
773 FuPluginPrivate *priv = GET_PRIVATE (plugin);
774 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
775
Richard Hughes9c028f02017-10-28 21:14:28 +0100776 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100777 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100778}
779
780/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100781 * fu_plugin_lookup_quirk_by_id_as_uint64:
782 * @plugin: A #FuPlugin
783 * @group: A string, e.g. "DfuFlags"
784 * @key: An ID to match the entry, e.g. "Size"
785 *
786 * Looks up an entry in the hardware database using a string key, returning
787 * an integer value. Values are assumed base 10, unless prefixed with "0x"
788 * where they are parsed as base 16.
789 *
790 * Returns: (transfer none): value from the database, or 0 if not found
791 *
792 * Since: 1.1.2
793 **/
794guint64
795fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *plugin, const gchar *group, const gchar *key)
796{
797 const gchar *str;
798 guint base = 10;
799
800 /* exact ID */
801 str = fu_plugin_lookup_quirk_by_id (plugin, group, key);
802 if (str == NULL)
803 return 0;
804 if (g_str_has_prefix (str, "0x")) {
805 str += 2;
806 base = 16;
807 }
808 return g_ascii_strtoull (str, NULL, base);
809}
810
811/**
Richard Hughes9c028f02017-10-28 21:14:28 +0100812 * fu_plugin_lookup_quirk_by_usb_device:
813 * @plugin: A #FuPlugin
814 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100815 * @usb_device: A #GUsbDevice
Richard Hughes9c028f02017-10-28 21:14:28 +0100816 *
817 * Looks up an entry in the hardware database using various keys generated
818 * from @dev.
819 *
820 * Returns: (transfer none): values from the database, or %NULL if not found
821 *
822 * Since: 1.0.1
823 **/
824const gchar *
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100825fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin,
826 GUsbDevice *usb_device,
827 const gchar *prefix)
Richard Hughes9c028f02017-10-28 21:14:28 +0100828{
829 FuPluginPrivate *priv = GET_PRIVATE (plugin);
830 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100831 return fu_quirks_lookup_by_usb_device (priv->quirks, usb_device, prefix);
Richard Hughes9c028f02017-10-28 21:14:28 +0100832}
833
Richard Hughes1354ea92017-09-19 15:58:31 +0100834/**
835 * fu_plugin_get_supported:
836 * @plugin: A #FuPlugin
837 *
838 * Gets all the device GUIDs supported by the daemon.
839 *
Richard Hughes4eada342017-10-03 21:20:32 +0100840 * Returns: (element-type utf8) (transfer none): GUIDs
841 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100842 * Since: 1.0.0
843 **/
844GPtrArray *
845fu_plugin_get_supported (FuPlugin *plugin)
846{
847 FuPluginPrivate *priv = GET_PRIVATE (plugin);
848 return priv->supported_guids;
849}
850
851void
Richard Hughes49e5e052017-09-03 12:15:41 +0100852fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios)
853{
854 FuPluginPrivate *priv = GET_PRIVATE (plugin);
855 g_set_object (&priv->smbios, smbios);
856}
857
Richard Hughesb8f8db22017-04-25 15:56:00 +0100858/**
Richard Hughesb0829032017-01-10 09:27:08 +0000859 * fu_plugin_set_coldplug_delay:
860 * @plugin: A #FuPlugin
861 * @duration: A delay in milliseconds
862 *
863 * Set the minimum time that should be waited inbetween the call to
864 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
865 * to be the minimum hardware initialisation time from a datasheet.
866 *
867 * It is better to use this function rather than using a sleep() in the plugin
868 * itself as then only one delay is done in the daemon rather than waiting for
869 * each coldplug prepare in a serial way.
870 *
871 * Additionally, very long delays should be avoided as the daemon will be
872 * blocked from processing requests whilst the coldplug delay is being
873 * performed.
874 *
875 * Since: 0.8.0
876 **/
877void
878fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration)
879{
880 g_return_if_fail (FU_IS_PLUGIN (plugin));
881 g_return_if_fail (duration > 0);
882
883 /* check sanity */
884 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
885 g_warning ("duration of %ums is crazy, truncating to %ums",
886 duration,
887 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
888 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
889 }
890
891 /* emit */
892 g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
893}
894
Richard Hughesd0905142016-03-13 09:46:49 +0000895gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000896fu_plugin_runner_startup (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000897{
Richard Hughescff38bc2016-12-12 12:03:37 +0000898 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000899 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000900
901 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000902 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000903 return TRUE;
904
Richard Hughes639da472018-01-06 22:35:04 +0000905 /* no object loaded */
906 if (priv->module == NULL)
907 return TRUE;
908
Richard Hughesd0905142016-03-13 09:46:49 +0000909 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000910 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
911 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000912 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000913 g_debug ("performing startup() on %s", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +0000914 if (!func (plugin, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000915 g_prefix_error (error, "failed to startup %s: ", priv->name);
916 return FALSE;
917 }
918 return TRUE;
919}
920
921static gboolean
922fu_plugin_runner_offline_invalidate (GError **error)
923{
924 g_autoptr(GError) error_local = NULL;
925 g_autoptr(GFile) file1 = NULL;
926
927 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
928
929 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
930 if (!g_file_query_exists (file1, NULL))
931 return TRUE;
932 if (!g_file_delete (file1, NULL, &error_local)) {
933 g_set_error (error,
934 FWUPD_ERROR,
935 FWUPD_ERROR_INTERNAL,
936 "Cannot delete %s: %s",
937 FU_OFFLINE_TRIGGER_FILENAME,
938 error_local->message);
939 return FALSE;
940 }
941 return TRUE;
942}
943
944static gboolean
945fu_plugin_runner_offline_setup (GError **error)
946{
947 gint rc;
948
949 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
950
951 /* create symlink for the systemd-system-update-generator */
952 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
953 if (rc < 0) {
954 g_set_error (error,
955 FWUPD_ERROR,
956 FWUPD_ERROR_INTERNAL,
957 "Failed to create symlink %s to %s: %s",
958 FU_OFFLINE_TRIGGER_FILENAME,
959 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000960 return FALSE;
961 }
962 return TRUE;
963}
964
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000965static gboolean
966fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device,
967 const gchar *symbol_name, GError **error)
968{
969 FuPluginPrivate *priv = GET_PRIVATE (plugin);
970 FuPluginDeviceFunc func = NULL;
971
972 /* not enabled */
973 if (!priv->enabled)
974 return TRUE;
975
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000976 /* no object loaded */
977 if (priv->module == NULL)
978 return TRUE;
979
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000980 /* optional */
981 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
982 if (func == NULL)
983 return TRUE;
984 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
985 if (!func (plugin, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000986 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000987 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000988 priv->name);
989 return FALSE;
990 }
991 return TRUE;
992}
993
Richard Hughesdbd8c762018-06-15 20:31:40 +0100994static gboolean
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500995fu_plugin_runner_flagged_device_generic (FuPlugin *plugin, FwupdInstallFlags flags,
996 FuDevice *device,
997 const gchar *symbol_name, GError **error)
998{
999 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1000 FuPluginFlaggedDeviceFunc func = NULL;
1001
1002 /* not enabled */
1003 if (!priv->enabled)
1004 return TRUE;
1005
1006 /* no object loaded */
1007 if (priv->module == NULL)
1008 return TRUE;
1009
1010 /* optional */
1011 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1012 if (func == NULL)
1013 return TRUE;
1014 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
1015 if (!func (plugin, flags, device, error)) {
1016 g_prefix_error (error, "failed to run %s() on %s: ",
1017 symbol_name + 10,
1018 priv->name);
1019 return FALSE;
1020 }
1021 return TRUE;
1022
1023}
1024
1025static gboolean
Richard Hughesdbd8c762018-06-15 20:31:40 +01001026fu_plugin_runner_device_array_generic (FuPlugin *plugin, GPtrArray *devices,
1027 const gchar *symbol_name, GError **error)
1028{
1029 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1030 FuPluginDeviceArrayFunc func = NULL;
1031
1032 /* not enabled */
1033 if (!priv->enabled)
1034 return TRUE;
1035
1036 /* no object loaded */
1037 if (priv->module == NULL)
1038 return TRUE;
1039
1040 /* optional */
1041 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1042 if (func == NULL)
1043 return TRUE;
1044 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
1045 if (!func (plugin, devices, error)) {
1046 g_prefix_error (error, "failed to run %s() on %s: ",
1047 symbol_name + 10,
1048 priv->name);
1049 return FALSE;
1050 }
1051 return TRUE;
1052}
1053
Richard Hughesd0905142016-03-13 09:46:49 +00001054gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001055fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001056{
Richard Hughescff38bc2016-12-12 12:03:37 +00001057 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001058 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +00001059
1060 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +00001061 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +00001062 return TRUE;
1063
Richard Hughes639da472018-01-06 22:35:04 +00001064 /* no object loaded */
1065 if (priv->module == NULL)
1066 return TRUE;
1067
Richard Hughesd0905142016-03-13 09:46:49 +00001068 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001069 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
1070 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001071 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001072 g_debug ("performing coldplug() on %s", priv->name);
1073 if (!func (plugin, error)) {
1074 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
1075 return FALSE;
1076 }
1077 return TRUE;
1078}
1079
Richard Hughes7b8b2022016-12-12 16:15:03 +00001080gboolean
Richard Hughes2de8f132018-01-17 09:12:02 +00001081fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error)
1082{
1083 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1084 FuPluginStartupFunc func = NULL;
1085
1086 /* not enabled */
1087 if (!priv->enabled)
1088 return TRUE;
1089
1090 /* no object loaded */
1091 if (priv->module == NULL)
1092 return TRUE;
1093
1094 /* optional */
1095 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1096 if (func == NULL)
1097 return TRUE;
1098 g_debug ("performing recoldplug() on %s", priv->name);
1099 if (!func (plugin, error)) {
1100 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
1101 return FALSE;
1102 }
1103 return TRUE;
1104}
1105
1106gboolean
Richard Hughes46487c92017-01-07 21:26:34 +00001107fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error)
1108{
1109 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1110 FuPluginStartupFunc func = NULL;
1111
1112 /* not enabled */
1113 if (!priv->enabled)
1114 return TRUE;
1115
Richard Hughes639da472018-01-06 22:35:04 +00001116 /* no object loaded */
1117 if (priv->module == NULL)
1118 return TRUE;
1119
Richard Hughes46487c92017-01-07 21:26:34 +00001120 /* optional */
1121 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1122 if (func == NULL)
1123 return TRUE;
1124 g_debug ("performing coldplug_prepare() on %s", priv->name);
1125 if (!func (plugin, error)) {
1126 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
1127 return FALSE;
1128 }
1129 return TRUE;
1130}
1131
1132gboolean
1133fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error)
1134{
1135 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1136 FuPluginStartupFunc func = NULL;
1137
1138 /* not enabled */
1139 if (!priv->enabled)
1140 return TRUE;
1141
Richard Hughes639da472018-01-06 22:35:04 +00001142 /* no object loaded */
1143 if (priv->module == NULL)
1144 return TRUE;
1145
Richard Hughes46487c92017-01-07 21:26:34 +00001146 /* optional */
1147 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1148 if (func == NULL)
1149 return TRUE;
1150 g_debug ("performing coldplug_cleanup() on %s", priv->name);
1151 if (!func (plugin, error)) {
1152 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
1153 return FALSE;
1154 }
1155 return TRUE;
1156}
1157
1158gboolean
Richard Hughesdbd8c762018-06-15 20:31:40 +01001159fu_plugin_runner_composite_prepare (FuPlugin *plugin, GPtrArray *devices, GError **error)
1160{
1161 return fu_plugin_runner_device_array_generic (plugin, devices,
1162 "fu_plugin_composite_prepare",
1163 error);
1164}
1165
1166gboolean
1167fu_plugin_runner_composite_cleanup (FuPlugin *plugin, GPtrArray *devices, GError **error)
1168{
1169 return fu_plugin_runner_device_array_generic (plugin, devices,
1170 "fu_plugin_composite_cleanup",
1171 error);
1172}
1173
1174gboolean
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001175fu_plugin_runner_update_prepare (FuPlugin *plugin, FwupdInstallFlags flags, FuDevice *device,
1176 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001177{
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001178 return fu_plugin_runner_flagged_device_generic (plugin, flags, device,
1179 "fu_plugin_update_prepare",
1180 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001181}
1182
1183gboolean
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001184fu_plugin_runner_update_cleanup (FuPlugin *plugin, FwupdInstallFlags flags, FuDevice *device,
1185 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001186{
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001187 return fu_plugin_runner_flagged_device_generic (plugin, flags, device,
1188 "fu_plugin_update_cleanup",
1189 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001190}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001191
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001192gboolean
1193fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error)
1194{
1195 return fu_plugin_runner_device_generic (plugin, device,
1196 "fu_plugin_update_attach", error);
1197}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001198
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001199gboolean
1200fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error)
1201{
1202 return fu_plugin_runner_device_generic (plugin, device,
1203 "fu_plugin_update_detach", error);
1204}
1205
1206gboolean
1207fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error)
1208{
1209 return fu_plugin_runner_device_generic (plugin, device,
1210 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001211}
1212
Richard Hughes104f6512017-11-24 11:44:57 +00001213gboolean
1214fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error)
1215{
1216 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1217 FuPluginUsbDeviceAddedFunc func = NULL;
1218
1219 /* not enabled */
1220 if (!priv->enabled)
1221 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001222
1223 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001224 if (priv->module == NULL)
1225 return TRUE;
1226
1227 /* optional */
1228 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1229 if (func != NULL) {
1230 g_debug ("performing usb_device_added() on %s", priv->name);
1231 return func (plugin, usb_device, error);
1232 }
1233 return TRUE;
1234}
1235
Richard Hughese1fd34d2017-08-24 14:19:51 +01001236void
1237fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device)
1238{
1239 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1240 FuPluginDeviceRegisterFunc func = NULL;
1241
1242 /* not enabled */
1243 if (!priv->enabled)
1244 return;
Richard Hughes34834102017-11-21 21:55:00 +00001245 if (priv->module == NULL)
1246 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001247
Mario Limonciello4910b242018-06-22 15:04:21 -05001248 /* don't notify plugins on their own devices */
1249 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (plugin)) == 0)
1250 return;
1251
Richard Hughese1fd34d2017-08-24 14:19:51 +01001252 /* optional */
1253 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1254 if (func != NULL) {
Richard Hughes1bf7ff92018-08-24 20:21:35 +01001255 g_debug ("performing fu_plugin_device_registered() on %s", priv->name);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001256 func (plugin, device);
1257 }
1258}
1259
Richard Hughescff38bc2016-12-12 12:03:37 +00001260static gboolean
1261fu_plugin_runner_schedule_update (FuPlugin *plugin,
1262 FuDevice *device,
1263 GBytes *blob_cab,
1264 GError **error)
1265{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001266 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001267 gchar tmpname[] = {"XXXXXX.cap"};
1268 g_autofree gchar *dirname = NULL;
1269 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001270 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001271 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001272 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001273 g_autoptr(GFile) file = NULL;
1274
1275 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001276 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001277 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001278 if (res_tmp != NULL) {
1279 g_set_error (error,
1280 FWUPD_ERROR,
1281 FWUPD_ERROR_ALREADY_PENDING,
1282 "%s is already scheduled to be updated",
1283 fu_device_get_id (device));
1284 return FALSE;
1285 }
1286
1287 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001288 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001289 file = g_file_new_for_path (dirname);
1290 if (!g_file_query_exists (file, NULL)) {
1291 if (!g_file_make_directory_with_parents (file, NULL, error))
1292 return FALSE;
1293 }
1294
1295 /* get a random filename */
1296 for (guint i = 0; i < 6; i++)
1297 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1298 filename = g_build_filename (dirname, tmpname, NULL);
1299
1300 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001301 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001302 if (!g_file_set_contents (filename,
1303 g_bytes_get_data (blob_cab, NULL),
1304 (gssize) g_bytes_get_size (blob_cab),
1305 error))
1306 return FALSE;
1307
1308 /* schedule for next boot */
1309 g_debug ("schedule %s to be installed to %s on next boot",
1310 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001311 release = fu_device_get_release_default (device);
1312 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1313 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001314
1315 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001316 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001317 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001318 return FALSE;
1319
1320 /* next boot we run offline */
1321 return fu_plugin_runner_offline_setup (error);
1322}
1323
1324gboolean
1325fu_plugin_runner_verify (FuPlugin *plugin,
1326 FuDevice *device,
1327 FuPluginVerifyFlags flags,
1328 GError **error)
1329{
1330 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001331 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001332 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001333
1334 /* not enabled */
1335 if (!priv->enabled)
1336 return TRUE;
1337
Richard Hughes639da472018-01-06 22:35:04 +00001338 /* no object loaded */
1339 if (priv->module == NULL)
1340 return TRUE;
1341
Richard Hughesababbb72017-06-15 20:18:36 +01001342 /* clear any existing verification checksums */
1343 checksums = fu_device_get_checksums (device);
1344 g_ptr_array_set_size (checksums, 0);
1345
Richard Hughescff38bc2016-12-12 12:03:37 +00001346 /* optional */
1347 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1348 if (func == NULL)
1349 return TRUE;
1350 g_debug ("performing verify() on %s", priv->name);
1351 if (!func (plugin, device, flags, error)) {
1352 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001353 return FALSE;
1354 }
1355 return TRUE;
1356}
1357
Richard Hughesd0905142016-03-13 09:46:49 +00001358gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001359fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001360{
Richard Hughescff38bc2016-12-12 12:03:37 +00001361 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001362
1363 /* final check */
1364 flags = fu_device_get_flags (device);
1365 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1366 g_set_error (error,
1367 FWUPD_ERROR,
1368 FWUPD_ERROR_NOT_SUPPORTED,
1369 "Device %s is not locked",
1370 fu_device_get_id (device));
1371 return FALSE;
1372 }
1373
Richard Hughes9c4b5312017-11-14 11:34:53 +00001374 /* run vfunc */
1375 if (!fu_plugin_runner_device_generic (plugin, device,
1376 "fu_plugin_unlock", error))
1377 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001378
1379 /* update with correct flags */
1380 flags = fu_device_get_flags (device);
1381 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1382 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1383 return TRUE;
1384}
1385
1386gboolean
1387fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001388 FuDevice *device,
1389 GBytes *blob_cab,
1390 GBytes *blob_fw,
1391 FwupdInstallFlags flags,
1392 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001393{
1394 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001395 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001396 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001397 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001398 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001399 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001400
1401 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001402 if (!priv->enabled) {
1403 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001404 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001405 }
Richard Hughesd0905142016-03-13 09:46:49 +00001406
Richard Hughes639da472018-01-06 22:35:04 +00001407 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001408 if (priv->module == NULL) {
1409 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001410 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001411 }
Richard Hughes639da472018-01-06 22:35:04 +00001412
Richard Hughesd0905142016-03-13 09:46:49 +00001413 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001414 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1415 if (update_func == NULL) {
1416 g_set_error_literal (error,
1417 FWUPD_ERROR,
1418 FWUPD_ERROR_NOT_SUPPORTED,
1419 "No update possible");
1420 return FALSE;
1421 }
Richard Hughesd0905142016-03-13 09:46:49 +00001422
Richard Hughesa785a1c2017-08-25 16:00:58 +01001423 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001424 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001425 if (blob_cab == NULL) {
1426 g_set_error_literal (error,
1427 FWUPD_ERROR,
1428 FWUPD_ERROR_NOT_SUPPORTED,
1429 "No cabinet archive to schedule");
1430 return FALSE;
1431 }
Richard Hughesa785a1c2017-08-25 16:00:58 +01001432 return fu_plugin_runner_schedule_update (plugin,
1433 device,
1434 blob_cab,
1435 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001436 }
1437
1438 /* cancel the pending action */
1439 if (!fu_plugin_runner_offline_invalidate (error))
1440 return FALSE;
1441
1442 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001443 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001444 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001445 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughesc0cd0232018-01-31 15:02:00 +00001446 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001447 g_propagate_error (error, error_update);
1448 return FALSE;
1449 }
1450
Richard Hughesf556d372017-06-15 19:49:18 +01001451 /* no longer valid */
1452 checksums = fu_device_get_checksums (device);
1453 g_ptr_array_set_size (checksums, 0);
1454
Richard Hughescff38bc2016-12-12 12:03:37 +00001455 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001456 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001457 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001458 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001459
Richard Hughes780ef3f2018-01-12 16:20:31 +00001460 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001461 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1462 if (!fu_history_modify_device (history, device,
1463 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1464 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001465 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001466
1467 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001468 release = fu_device_get_release_default (device_pending);
1469 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001470 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1471 g_autoptr(GError) error_local = NULL;
1472 g_autoptr(GFile) file = NULL;
1473 file = g_file_new_for_path (tmp);
1474 if (!g_file_delete (file, NULL, &error_local)) {
1475 g_set_error (error,
1476 FWUPD_ERROR,
1477 FWUPD_ERROR_INVALID_FILE,
1478 "Failed to delete %s: %s",
1479 tmp, error_local->message);
1480 return FALSE;
1481 }
1482 }
1483 }
Richard Hughesd0905142016-03-13 09:46:49 +00001484 return TRUE;
1485}
Richard Hughescff38bc2016-12-12 12:03:37 +00001486
1487gboolean
1488fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error)
1489{
1490 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001491 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001492
1493 /* not enabled */
1494 if (!priv->enabled)
1495 return TRUE;
1496
Richard Hughes639da472018-01-06 22:35:04 +00001497 /* no object loaded */
1498 if (priv->module == NULL)
1499 return TRUE;
1500
Richard Hughes65e44ca2018-01-30 17:26:30 +00001501 /* optional */
1502 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1503 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001504 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001505 g_debug ("performing clear_result() on %s", priv->name);
1506 if (!func (plugin, device, error)) {
1507 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001508 return FALSE;
1509 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001510 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001511}
1512
1513gboolean
1514fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1515{
1516 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001517 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001518
1519 /* not enabled */
1520 if (!priv->enabled)
1521 return TRUE;
1522
Richard Hughes639da472018-01-06 22:35:04 +00001523 /* no object loaded */
1524 if (priv->module == NULL)
1525 return TRUE;
1526
Richard Hughes65e44ca2018-01-30 17:26:30 +00001527 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001528 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001529 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001530 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001531 g_debug ("performing get_results() on %s", priv->name);
1532 if (!func (plugin, device, error)) {
1533 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001534 return FALSE;
1535 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001536 return TRUE;
1537}
1538
Richard Hughes08a37992017-09-12 12:57:43 +01001539/**
1540 * fu_plugin_get_order:
1541 * @plugin: a #FuPlugin
1542 *
1543 * Gets the plugin order, where higher numbers are run after lower
1544 * numbers.
1545 *
1546 * Returns: the integer value
1547 **/
1548guint
1549fu_plugin_get_order (FuPlugin *plugin)
1550{
1551 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1552 return priv->order;
1553}
1554
1555/**
1556 * fu_plugin_set_order:
1557 * @plugin: a #FuPlugin
1558 * @order: a integer value
1559 *
1560 * Sets the plugin order, where higher numbers are run after lower
1561 * numbers.
1562 **/
1563void
1564fu_plugin_set_order (FuPlugin *plugin, guint order)
1565{
1566 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1567 priv->order = order;
1568}
1569
1570/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001571 * fu_plugin_get_priority:
1572 * @plugin: a #FuPlugin
1573 *
1574 * Gets the plugin priority, where higher numbers are better.
1575 *
1576 * Returns: the integer value
1577 **/
1578guint
1579fu_plugin_get_priority (FuPlugin *plugin)
1580{
1581 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1582 return priv->priority;
1583}
1584
1585/**
1586 * fu_plugin_set_priority:
1587 * @plugin: a #FuPlugin
1588 * @priority: a integer value
1589 *
1590 * Sets the plugin priority, where higher numbers are better.
1591 **/
1592void
1593fu_plugin_set_priority (FuPlugin *plugin, guint priority)
1594{
1595 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1596 priv->priority = priority;
1597}
1598
1599/**
Richard Hughes08a37992017-09-12 12:57:43 +01001600 * fu_plugin_add_rule:
1601 * @plugin: a #FuPlugin
1602 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001603 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001604 *
1605 * If the plugin name is found, the rule will be used to sort the plugin list,
1606 * for example the plugin specified by @name will be ordered after this plugin
1607 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1608 *
1609 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1610 * If depsolving fails then fwupd will not start.
1611 **/
1612void
1613fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1614{
1615 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1616 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1617}
1618
1619/**
1620 * fu_plugin_get_rules:
1621 * @plugin: a #FuPlugin
1622 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1623 *
1624 * Gets the plugin IDs that should be run after this plugin.
1625 *
1626 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1627 **/
1628GPtrArray *
1629fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1630{
1631 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1632 return priv->rules[rule];
1633}
1634
Richard Hughes80b79bb2018-01-11 21:11:06 +00001635/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001636 * fu_plugin_has_rule:
1637 * @plugin: a #FuPlugin
1638 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1639 * @name: a plugin name, e.g. `upower`
1640 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001641 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001642 *
1643 * Returns: %TRUE if the name exists for the specific rule
1644 **/
1645gboolean
1646fu_plugin_has_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1647{
1648 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1649 for (guint i = 0; i < priv->rules[rule]->len; i++) {
1650 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
1651 if (g_strcmp0 (tmp, name) == 0)
1652 return TRUE;
1653 }
1654 return FALSE;
1655}
1656
1657/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00001658 * fu_plugin_add_report_metadata:
1659 * @plugin: a #FuPlugin
1660 * @key: a string, e.g. `FwupdateVersion`
1661 * @value: a string, e.g. `10`
1662 *
1663 * Sets any additional metadata to be included in the firmware report to aid
1664 * debugging problems.
1665 *
1666 * Any data included here will be sent to the metadata server after user
1667 * confirmation.
1668 **/
1669void
1670fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1671{
1672 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1673 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1674}
1675
1676/**
1677 * fu_plugin_get_report_metadata:
1678 * @plugin: a #FuPlugin
1679 *
1680 * Returns the list of additional metadata to be added when filing a report.
1681 *
1682 * Returns: (transfer none): the map of report metadata
1683 **/
1684GHashTable *
1685fu_plugin_get_report_metadata (FuPlugin *plugin)
1686{
1687 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1688 return priv->report_metadata;
1689}
1690
Mario Limonciello963dc422018-02-27 14:26:58 -06001691/**
1692 * fu_plugin_get_config_value:
1693 * @plugin: a #FuPlugin
1694 * @key: A settings key
1695 *
1696 * Return the value of a key if it's been configured
1697 *
1698 * Since: 1.0.6
1699 **/
1700gchar *
1701fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key)
1702{
Richard Hughes4be17d12018-05-30 20:36:29 +01001703 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06001704 g_autofree gchar *conf_file = NULL;
1705 g_autofree gchar *conf_path = NULL;
1706 g_autoptr(GKeyFile) keyfile = NULL;
1707 const gchar *plugin_name;
1708
Richard Hughes4be17d12018-05-30 20:36:29 +01001709 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Mario Limonciello963dc422018-02-27 14:26:58 -06001710 plugin_name = fu_plugin_get_name (plugin);
1711 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01001712 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06001713 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1714 return NULL;
1715 keyfile = g_key_file_new ();
1716 if (!g_key_file_load_from_file (keyfile, conf_path,
1717 G_KEY_FILE_NONE, NULL))
1718 return NULL;
1719 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1720}
1721
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001722/**
1723 * fu_plugin_name_compare:
1724 * @plugin1: first #FuPlugin to compare.
1725 * @plugin2: second #FuPlugin to compare.
1726 *
1727 * Compares two plugins by their names.
1728 *
1729 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1730 **/
1731gint
1732fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1733{
1734 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1735 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1736 return g_strcmp0 (priv1->name, priv2->name);
1737}
1738
1739/**
1740 * fu_plugin_order_compare:
1741 * @plugin1: first #FuPlugin to compare.
1742 * @plugin2: second #FuPlugin to compare.
1743 *
1744 * Compares two plugins by their depsolved order.
1745 *
1746 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1747 **/
1748gint
1749fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1750{
1751 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1752 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1753 if (priv1->order < priv2->order)
1754 return -1;
1755 if (priv1->order > priv2->order)
1756 return 1;
1757 return 0;
1758}
1759
Richard Hughescff38bc2016-12-12 12:03:37 +00001760static void
1761fu_plugin_class_init (FuPluginClass *klass)
1762{
1763 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1764 object_class->finalize = fu_plugin_finalize;
1765 signals[SIGNAL_DEVICE_ADDED] =
1766 g_signal_new ("device-added",
1767 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1768 G_STRUCT_OFFSET (FuPluginClass, device_added),
1769 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1770 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1771 signals[SIGNAL_DEVICE_REMOVED] =
1772 g_signal_new ("device-removed",
1773 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1774 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1775 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1776 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001777 signals[SIGNAL_DEVICE_REGISTER] =
1778 g_signal_new ("device-register",
1779 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1780 G_STRUCT_OFFSET (FuPluginClass, device_register),
1781 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1782 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001783 signals[SIGNAL_RECOLDPLUG] =
1784 g_signal_new ("recoldplug",
1785 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1786 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1787 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1788 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001789 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1790 g_signal_new ("set-coldplug-delay",
1791 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1792 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1793 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1794 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001795}
1796
1797static void
1798fu_plugin_init (FuPlugin *plugin)
1799{
1800 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1801 priv->enabled = TRUE;
1802 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1803 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001804 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001805 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 +01001806 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1807 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001808}
1809
1810static void
1811fu_plugin_finalize (GObject *object)
1812{
1813 FuPlugin *plugin = FU_PLUGIN (object);
1814 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1815 FuPluginInitFunc func = NULL;
1816
1817 /* optional */
1818 if (priv->module != NULL) {
1819 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1820 if (func != NULL) {
1821 g_debug ("performing destroy() on %s", priv->name);
1822 func (plugin);
1823 }
1824 }
1825
Richard Hughes08a37992017-09-12 12:57:43 +01001826 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1827 g_ptr_array_unref (priv->rules[i]);
1828
Richard Hughescff38bc2016-12-12 12:03:37 +00001829 if (priv->usb_ctx != NULL)
1830 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001831 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001832 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001833 if (priv->quirks != NULL)
1834 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001835 if (priv->supported_guids != NULL)
1836 g_ptr_array_unref (priv->supported_guids);
Richard Hughes49e5e052017-09-03 12:15:41 +01001837 if (priv->smbios != NULL)
1838 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001839 if (priv->runtime_versions != NULL)
1840 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001841 if (priv->compile_versions != NULL)
1842 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00001843 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001844 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001845 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001846 g_free (priv->name);
1847 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05001848 /* Must happen as the last step to avoid prematurely
1849 * freeing memory held by the plugin */
1850#ifndef RUNNING_ON_VALGRIND
1851 if (priv->module != NULL)
1852 g_module_close (priv->module);
1853#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001854
1855 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1856}
1857
1858FuPlugin *
1859fu_plugin_new (void)
1860{
1861 FuPlugin *plugin;
1862 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1863 return plugin;
1864}