blob: e558c23e6f0af97955abcf92e62559a98f2ca63e [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);
Richard Hughesdbd8c762018-06-15 20:31:40 +010080typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *plugin,
81 GPtrArray *devices,
82 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +000083typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin,
84 FuDevice *device,
85 FuPluginVerifyFlags flags,
86 GError **error);
87typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin,
88 FuDevice *device,
89 GBytes *blob_fw,
90 FwupdInstallFlags flags,
91 GError **error);
Richard Hughes104f6512017-11-24 11:44:57 +000092typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin,
93 GUsbDevice *usb_device,
94 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +000095
Richard Hughes57d18222017-01-10 16:02:59 +000096/**
97 * fu_plugin_get_name:
98 * @plugin: A #FuPlugin
99 *
100 * Gets the plugin name.
101 *
102 * Returns: a plugin name, or %NULL for unknown.
103 *
104 * Since: 0.8.0
105 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000106const gchar *
107fu_plugin_get_name (FuPlugin *plugin)
Richard Hughesd0905142016-03-13 09:46:49 +0000108{
Richard Hughescff38bc2016-12-12 12:03:37 +0000109 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000110 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000111 return priv->name;
112}
Richard Hughesd0905142016-03-13 09:46:49 +0000113
Richard Hughes34834102017-11-21 21:55:00 +0000114void
115fu_plugin_set_name (FuPlugin *plugin, const gchar *name)
116{
117 FuPluginPrivate *priv = GET_PRIVATE (plugin);
118 g_return_if_fail (FU_IS_PLUGIN (plugin));
119 g_return_if_fail (name != NULL);
120 g_free (priv->name);
121 priv->name = g_strdup (name);
122}
123
Richard Hughes57d18222017-01-10 16:02:59 +0000124/**
125 * fu_plugin_cache_lookup:
126 * @plugin: A #FuPlugin
127 * @id: the key
128 *
129 * Finds an object in the per-plugin cache.
130 *
131 * Returns: (transfer none): a #GObject, or %NULL for unfound.
132 *
133 * Since: 0.8.0
134 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000135gpointer
136fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id)
137{
138 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000139 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
140 g_return_val_if_fail (id != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000141 return g_hash_table_lookup (priv->devices, id);
142}
Richard Hughesd0905142016-03-13 09:46:49 +0000143
Richard Hughes57d18222017-01-10 16:02:59 +0000144/**
145 * fu_plugin_cache_add:
146 * @plugin: A #FuPlugin
147 * @id: the key
148 * @dev: a #GObject, typically a #FuDevice
149 *
150 * Adds an object to the per-plugin cache.
151 *
152 * Since: 0.8.0
153 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000154void
155fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev)
156{
157 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000158 g_return_if_fail (FU_IS_PLUGIN (plugin));
159 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000160 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
161}
162
Richard Hughes57d18222017-01-10 16:02:59 +0000163/**
164 * fu_plugin_cache_remove:
165 * @plugin: A #FuPlugin
166 * @id: the key
167 *
168 * Removes an object from the per-plugin cache.
169 *
170 * Since: 0.8.0
171 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000172void
173fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id)
174{
175 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000176 g_return_if_fail (FU_IS_PLUGIN (plugin));
177 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000178 g_hash_table_remove (priv->devices, id);
179}
180
Richard Hughes57d18222017-01-10 16:02:59 +0000181/**
182 * fu_plugin_get_data:
183 * @plugin: A #FuPlugin
184 *
Richard Hughes4eada342017-10-03 21:20:32 +0100185 * Gets the per-plugin allocated private data. This will return %NULL unless
186 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000187 *
Richard Hughes4eada342017-10-03 21:20:32 +0100188 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000189 *
190 * Since: 0.8.0
191 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000192FuPluginData *
193fu_plugin_get_data (FuPlugin *plugin)
194{
195 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000196 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000197 return priv->data;
198}
199
Richard Hughes57d18222017-01-10 16:02:59 +0000200/**
201 * fu_plugin_alloc_data:
202 * @plugin: A #FuPlugin
203 * @data_sz: the size to allocate
204 *
205 * Allocates the per-plugin allocated private data.
206 *
207 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
208 *
209 * Since: 0.8.0
210 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000211FuPluginData *
212fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz)
213{
214 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000215 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000216 if (priv->data != NULL) {
217 g_critical ("fu_plugin_alloc_data() already used by plugin");
218 return priv->data;
219 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000220 priv->data = g_malloc0 (data_sz);
221 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000222}
223
Richard Hughes57d18222017-01-10 16:02:59 +0000224/**
225 * fu_plugin_get_usb_context:
226 * @plugin: A #FuPlugin
227 *
228 * Gets the shared USB context that all plugins can use.
229 *
230 * Returns: (transfer none): a #GUsbContext.
231 *
232 * Since: 0.8.0
233 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000234GUsbContext *
235fu_plugin_get_usb_context (FuPlugin *plugin)
236{
Richard Hughescff38bc2016-12-12 12:03:37 +0000237 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000238 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000239 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000240}
241
242void
243fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx)
244{
Richard Hughescff38bc2016-12-12 12:03:37 +0000245 FuPluginPrivate *priv = GET_PRIVATE (plugin);
246 g_set_object (&priv->usb_ctx, usb_ctx);
247}
248
Richard Hughes57d18222017-01-10 16:02:59 +0000249/**
250 * fu_plugin_get_enabled:
251 * @plugin: A #FuPlugin
252 *
Richard Hughes4eada342017-10-03 21:20:32 +0100253 * Returns if the plugin is enabled. Plugins may self-disable using
254 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000255 *
256 * Returns: %TRUE if the plugin is currently enabled.
257 *
258 * Since: 0.8.0
259 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000260gboolean
261fu_plugin_get_enabled (FuPlugin *plugin)
262{
263 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000264 g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000265 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000266}
267
Richard Hughes57d18222017-01-10 16:02:59 +0000268/**
269 * fu_plugin_set_enabled:
270 * @plugin: A #FuPlugin
271 * @enabled: the enabled value
272 *
273 * Enables or disables a plugin. Plugins can self-disable at any point.
274 *
275 * Since: 0.8.0
276 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000277void
Richard Hughescff38bc2016-12-12 12:03:37 +0000278fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000279{
Richard Hughescff38bc2016-12-12 12:03:37 +0000280 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000281 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughescff38bc2016-12-12 12:03:37 +0000282 priv->enabled = enabled;
283}
284
Richard Hughes1e456bc2018-05-10 20:16:16 +0100285gchar *
286fu_plugin_guess_name_from_fn (const gchar *filename)
287{
288 const gchar *prefix = "libfu_plugin_";
289 gchar *name;
290 gchar *str = g_strstr_len (filename, -1, prefix);
291 if (str == NULL)
292 return NULL;
293 name = g_strdup (str + strlen (prefix));
294 g_strdelimit (name, ".", '\0');
295 return name;
296}
297
Richard Hughescff38bc2016-12-12 12:03:37 +0000298gboolean
299fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error)
300{
301 FuPluginPrivate *priv = GET_PRIVATE (plugin);
302 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000303
304 priv->module = g_module_open (filename, 0);
305 if (priv->module == NULL) {
306 g_set_error (error,
307 G_IO_ERROR,
308 G_IO_ERROR_FAILED,
309 "failed to open plugin: %s",
310 g_module_error ());
311 return FALSE;
312 }
313
314 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100315 if (priv->name == NULL)
316 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000317
318 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000319 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
320 if (func != NULL) {
321 g_debug ("performing init() on %s", filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000322 func (plugin);
323 }
324
Richard Hughescff38bc2016-12-12 12:03:37 +0000325 return TRUE;
326}
327
Richard Hughes57d18222017-01-10 16:02:59 +0000328/**
329 * fu_plugin_device_add:
330 * @plugin: A #FuPlugin
331 * @device: A #FuDevice
332 *
333 * Asks the daemon to add a device to the exported list. If this device ID
334 * has already been added by a different plugin then this request will be
335 * ignored.
336 *
337 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
338 * actually flashing an image to the hardware so that higher-priority plugins
339 * can add the device themselves.
340 *
341 * Since: 0.8.0
342 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000343void
344fu_plugin_device_add (FuPlugin *plugin, FuDevice *device)
345{
Richard Hughes5e447292018-04-27 14:25:54 +0100346 GPtrArray *children;
347
Richard Hughesccd78a92017-01-11 16:57:41 +0000348 g_return_if_fail (FU_IS_PLUGIN (plugin));
349 g_return_if_fail (FU_IS_DEVICE (device));
350
Richard Hughescff38bc2016-12-12 12:03:37 +0000351 g_debug ("emit added from %s: %s",
352 fu_plugin_get_name (plugin),
353 fu_device_get_id (device));
354 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
355 fu_device_set_plugin (device, fu_plugin_get_name (plugin));
356 g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100357
358 /* add children */
359 children = fu_device_get_children (device);
360 for (guint i = 0; i < children->len; i++) {
361 FuDevice *child = g_ptr_array_index (children, i);
362 fu_plugin_device_add (plugin, child);
363 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000364}
365
Richard Hughese1fd34d2017-08-24 14:19:51 +0100366/**
367 * fu_plugin_device_register:
368 * @plugin: A #FuPlugin
369 * @device: A #FuDevice
370 *
371 * Registers the device with other plugins so they can set metadata.
372 *
373 * Plugins do not have to call this manually as this is done automatically
374 * when using fu_plugin_device_add(). They may wish to use this manually
375 * if for intance the coldplug should be ignored based on the metadata
376 * set from other plugins.
377 *
378 * Since: 0.9.7
379 **/
380void
381fu_plugin_device_register (FuPlugin *plugin, FuDevice *device)
382{
383 g_return_if_fail (FU_IS_PLUGIN (plugin));
384 g_return_if_fail (FU_IS_DEVICE (device));
385
386 g_debug ("emit device-register from %s: %s",
387 fu_plugin_get_name (plugin),
388 fu_device_get_id (device));
389 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device);
390}
391
Richard Hughesae3d65f2016-12-16 09:38:01 +0000392typedef struct {
393 FuPlugin *plugin;
394 FuDevice *device;
395 guint timeout_id;
Richard Hughesd4184cf2016-12-21 16:05:17 +0000396 GHashTable *devices;
Richard Hughesae3d65f2016-12-16 09:38:01 +0000397} FuPluginHelper;
398
399static void
400fu_plugin_helper_free (FuPluginHelper *helper)
401{
402 g_object_unref (helper->plugin);
403 g_object_unref (helper->device);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000404 g_hash_table_unref (helper->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000405 g_free (helper);
406}
407
408static gboolean
409fu_plugin_device_add_delay_cb (gpointer user_data)
410{
411 FuPluginHelper *helper = (FuPluginHelper *) user_data;
412 fu_plugin_device_add (helper->plugin, helper->device);
Richard Hughes0dec2742017-09-18 11:07:39 +0100413 g_hash_table_remove (helper->devices, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000414 fu_plugin_helper_free (helper);
415 return FALSE;
416}
417
Richard Hughes57d18222017-01-10 16:02:59 +0000418/**
Richard Hughesf0a799e2017-01-17 20:13:30 +0000419 * fu_plugin_has_device_delay:
420 * @plugin: A #FuPlugin
421 *
422 * Returns if the device has a pending device that is waiting to be added.
423 *
424 * Returns: %TRUE if a device is waiting to be added
425 *
426 * Since: 0.8.0
427 **/
428gboolean
429fu_plugin_has_device_delay (FuPlugin *plugin)
430{
431 FuPluginPrivate *priv = GET_PRIVATE (plugin);
432 return g_hash_table_size (priv->devices_delay) > 0;
433}
434
435/**
Richard Hughes57d18222017-01-10 16:02:59 +0000436 * fu_plugin_device_add_delay:
437 * @plugin: A #FuPlugin
438 * @device: A #FuDevice
439 *
440 * Asks the daemon to add a device to the exported list after a small delay.
441 *
442 * Since: 0.8.0
443 **/
Richard Hughesae3d65f2016-12-16 09:38:01 +0000444void
445fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device)
446{
447 FuPluginPrivate *priv = GET_PRIVATE (plugin);
448 FuPluginHelper *helper;
Richard Hughesccd78a92017-01-11 16:57:41 +0000449
450 g_return_if_fail (FU_IS_PLUGIN (plugin));
451 g_return_if_fail (FU_IS_DEVICE (device));
452
Richard Hughes6ad951d2017-02-03 10:12:12 +0000453 /* already waiting for add */
454 helper = g_hash_table_lookup (priv->devices_delay, device);
455 if (helper != NULL) {
Richard Hughes3cca1c62017-07-21 13:38:20 +0100456 g_debug ("ignoring add-delay as device %s already pending",
457 fu_device_get_id (device));
Richard Hughes6ad951d2017-02-03 10:12:12 +0000458 return;
459 }
460
461 /* add after a small delay */
Richard Hughesae3d65f2016-12-16 09:38:01 +0000462 g_debug ("waiting a small time for other plugins");
463 helper = g_new0 (FuPluginHelper, 1);
464 helper->plugin = g_object_ref (plugin);
465 helper->device = g_object_ref (device);
466 helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000467 helper->devices = g_hash_table_ref (priv->devices_delay);
468 g_hash_table_insert (helper->devices, device, helper);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000469}
470
Richard Hughes57d18222017-01-10 16:02:59 +0000471/**
Richard Hughes4eada342017-10-03 21:20:32 +0100472 * fu_plugin_device_remove:
Richard Hughes57d18222017-01-10 16:02:59 +0000473 * @plugin: A #FuPlugin
474 * @device: A #FuDevice
475 *
476 * Asks the daemon to remove a device from the exported list.
477 *
478 * Since: 0.8.0
479 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000480void
481fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device)
482{
Richard Hughesae3d65f2016-12-16 09:38:01 +0000483 FuPluginPrivate *priv = GET_PRIVATE (plugin);
484 FuPluginHelper *helper;
485
Richard Hughesccd78a92017-01-11 16:57:41 +0000486 g_return_if_fail (FU_IS_PLUGIN (plugin));
487 g_return_if_fail (FU_IS_DEVICE (device));
488
Richard Hughesae3d65f2016-12-16 09:38:01 +0000489 /* waiting for add */
490 helper = g_hash_table_lookup (priv->devices_delay, device);
491 if (helper != NULL) {
492 g_debug ("ignoring remove from delayed addition");
493 g_source_remove (helper->timeout_id);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000494 g_hash_table_remove (priv->devices_delay, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000495 fu_plugin_helper_free (helper);
496 return;
497 }
498
Richard Hughescff38bc2016-12-12 12:03:37 +0000499 g_debug ("emit removed from %s: %s",
500 fu_plugin_get_name (plugin),
501 fu_device_get_id (device));
502 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device);
503}
504
Richard Hughes57d18222017-01-10 16:02:59 +0000505/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000506 * fu_plugin_request_recoldplug:
Richard Hughes362d6d72017-01-07 21:42:14 +0000507 * @plugin: A #FuPlugin
508 *
509 * Ask all the plugins to coldplug all devices, which will include the prepare()
510 * and cleanup() phases. Duplicate devices added will be ignored.
511 *
512 * Since: 0.8.0
513 **/
514void
Richard Hughes2de8f132018-01-17 09:12:02 +0000515fu_plugin_request_recoldplug (FuPlugin *plugin)
Richard Hughes362d6d72017-01-07 21:42:14 +0000516{
Richard Hughesccd78a92017-01-11 16:57:41 +0000517 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughes362d6d72017-01-07 21:42:14 +0000518 g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0);
519}
520
Richard Hughesb0829032017-01-10 09:27:08 +0000521/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100522 * fu_plugin_check_hwid:
523 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100524 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100525 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100526 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100527 * specific system can be shown using the `fwupdmgr hwids` command.
528 *
Richard Hughes4eada342017-10-03 21:20:32 +0100529 * Returns: %TRUE if the HwId is found on the system.
530 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100531 * Since: 0.9.1
532 **/
533gboolean
534fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid)
535{
536 FuPluginPrivate *priv = GET_PRIVATE (plugin);
537 if (priv->hwids == NULL)
538 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100539 return fu_hwids_has_guid (priv->hwids, hwid);
540}
541
542/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100543 * fu_plugin_get_hwids:
544 * @plugin: A #FuPlugin
545 *
546 * Returns all the HWIDs defined in the system. All hardware IDs on a
547 * specific system can be shown using the `fwupdmgr hwids` command.
548 *
549 * Returns: (transfer none) (element-type utf-8): An array of GUIDs
550 *
551 * Since: 1.1.1
552 **/
553GPtrArray *
554fu_plugin_get_hwids (FuPlugin *plugin)
555{
556 FuPluginPrivate *priv = GET_PRIVATE (plugin);
557 if (priv->hwids == NULL)
558 return NULL;
559 return fu_hwids_get_guids (priv->hwids);
560}
561
562/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100563 * fu_plugin_check_supported:
564 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100565 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100566 *
567 * Checks to see if a specific device GUID is supported, i.e. available in the
568 * AppStream metadata.
569 *
Richard Hughes4eada342017-10-03 21:20:32 +0100570 * Returns: %TRUE if the device is supported.
571 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100572 * Since: 1.0.0
573 **/
574gboolean
575fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid)
576{
577 FuPluginPrivate *priv = GET_PRIVATE (plugin);
578 if (priv->supported_guids == NULL)
579 return FALSE;
580 for (guint i = 0; i < priv->supported_guids->len; i++) {
581 const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i);
582 if (g_strcmp0 (guid, guid_tmp) == 0)
583 return TRUE;
584 }
585 return FALSE;
586}
587
588/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100589 * fu_plugin_get_dmi_value:
590 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100591 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100592 *
593 * Gets a hardware DMI value.
594 *
Richard Hughes4eada342017-10-03 21:20:32 +0100595 * Returns: The string, or %NULL
596 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100597 * Since: 0.9.7
598 **/
599const gchar *
600fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id)
601{
602 FuPluginPrivate *priv = GET_PRIVATE (plugin);
603 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100604 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100605 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100606}
607
Richard Hughes49e5e052017-09-03 12:15:41 +0100608/**
609 * fu_plugin_get_smbios_string:
610 * @plugin: A #FuPlugin
611 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
612 * @offset: A SMBIOS offset
613 *
614 * Gets a hardware SMBIOS string.
615 *
616 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
617 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
618 *
Richard Hughes4eada342017-10-03 21:20:32 +0100619 * Returns: A string, or %NULL
620 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100621 * Since: 0.9.8
622 **/
623const gchar *
624fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset)
625{
626 FuPluginPrivate *priv = GET_PRIVATE (plugin);
627 if (priv->smbios == NULL)
628 return NULL;
629 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
630}
631
632/**
633 * fu_plugin_get_smbios_data:
634 * @plugin: A #FuPlugin
635 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
636 *
637 * Gets a hardware SMBIOS data.
638 *
Richard Hughes4eada342017-10-03 21:20:32 +0100639 * Returns: (transfer none): A #GBytes, or %NULL
640 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100641 * Since: 0.9.8
642 **/
643GBytes *
644fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type)
645{
646 FuPluginPrivate *priv = GET_PRIVATE (plugin);
647 if (priv->smbios == NULL)
648 return NULL;
649 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
650}
651
Richard Hughesb8f8db22017-04-25 15:56:00 +0100652void
Richard Hughesd7704d42017-08-08 20:29:09 +0100653fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100654{
655 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesd7704d42017-08-08 20:29:09 +0100656 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100657}
658
Richard Hughes49e5e052017-09-03 12:15:41 +0100659void
Richard Hughes1354ea92017-09-19 15:58:31 +0100660fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids)
661{
662 FuPluginPrivate *priv = GET_PRIVATE (plugin);
663 if (priv->supported_guids != NULL)
664 g_ptr_array_unref (priv->supported_guids);
665 priv->supported_guids = g_ptr_array_ref (supported_guids);
666}
667
Richard Hughes9c028f02017-10-28 21:14:28 +0100668void
669fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks)
670{
671 FuPluginPrivate *priv = GET_PRIVATE (plugin);
672 g_set_object (&priv->quirks, quirks);
673}
674
675/**
676 * fu_plugin_get_quirks:
677 * @plugin: A #FuPlugin
678 *
679 * Returns the hardware database object. This can be used to discover device
680 * quirks or other device-specific settings.
681 *
682 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
683 *
684 * Since: 1.0.1
685 **/
686FuQuirks *
687fu_plugin_get_quirks (FuPlugin *plugin)
688{
689 FuPluginPrivate *priv = GET_PRIVATE (plugin);
690 return priv->quirks;
691}
692
Richard Hughes0eb123b2018-04-19 12:00:04 +0100693void
694fu_plugin_set_runtime_versions (FuPlugin *plugin, GHashTable *runtime_versions)
695{
696 FuPluginPrivate *priv = GET_PRIVATE (plugin);
697 priv->runtime_versions = g_hash_table_ref (runtime_versions);
698}
699
700/**
701 * fu_plugin_add_runtime_version:
702 * @plugin: A #FuPlugin
703 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
704 * @version: A version string, e.g. "1.2.3"
705 *
706 * Sets a runtime version of a specific dependancy.
707 *
708 * Since: 1.0.7
709 **/
710void
711fu_plugin_add_runtime_version (FuPlugin *plugin,
712 const gchar *component_id,
713 const gchar *version)
714{
715 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesb01b4862018-04-20 16:39:48 +0100716 if (priv->runtime_versions == NULL)
717 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100718 g_hash_table_insert (priv->runtime_versions,
719 g_strdup (component_id),
720 g_strdup (version));
721}
722
Richard Hughes34e0dab2018-04-20 16:43:00 +0100723void
724fu_plugin_set_compile_versions (FuPlugin *plugin, GHashTable *compile_versions)
725{
726 FuPluginPrivate *priv = GET_PRIVATE (plugin);
727 priv->compile_versions = g_hash_table_ref (compile_versions);
728}
729
730/**
731 * fu_plugin_add_compile_version:
732 * @plugin: A #FuPlugin
733 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
734 * @version: A version string, e.g. "1.2.3"
735 *
736 * Sets a compile-time version of a specific dependancy.
737 *
738 * Since: 1.0.7
739 **/
740void
741fu_plugin_add_compile_version (FuPlugin *plugin,
742 const gchar *component_id,
743 const gchar *version)
744{
745 FuPluginPrivate *priv = GET_PRIVATE (plugin);
746 if (priv->compile_versions == NULL)
747 return;
748 g_hash_table_insert (priv->compile_versions,
749 g_strdup (component_id),
750 g_strdup (version));
751}
752
Richard Hughes9c028f02017-10-28 21:14:28 +0100753/**
754 * fu_plugin_lookup_quirk_by_id:
755 * @plugin: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100756 * @group: A string, e.g. "DfuFlags"
757 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100758 *
759 * Looks up an entry in the hardware database using a string value.
760 *
761 * Returns: (transfer none): values from the database, or %NULL if not found
762 *
763 * Since: 1.0.1
764 **/
765const gchar *
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100766fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100767{
768 FuPluginPrivate *priv = GET_PRIVATE (plugin);
769 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
770
Richard Hughes9c028f02017-10-28 21:14:28 +0100771 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100772 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100773}
774
775/**
776 * fu_plugin_lookup_quirk_by_usb_device:
777 * @plugin: A #FuPlugin
778 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100779 * @usb_device: A #GUsbDevice
Richard Hughes9c028f02017-10-28 21:14:28 +0100780 *
781 * Looks up an entry in the hardware database using various keys generated
782 * from @dev.
783 *
784 * Returns: (transfer none): values from the database, or %NULL if not found
785 *
786 * Since: 1.0.1
787 **/
788const gchar *
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100789fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin,
790 GUsbDevice *usb_device,
791 const gchar *prefix)
Richard Hughes9c028f02017-10-28 21:14:28 +0100792{
793 FuPluginPrivate *priv = GET_PRIVATE (plugin);
794 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100795 return fu_quirks_lookup_by_usb_device (priv->quirks, usb_device, prefix);
Richard Hughes9c028f02017-10-28 21:14:28 +0100796}
797
Richard Hughes1354ea92017-09-19 15:58:31 +0100798/**
799 * fu_plugin_get_supported:
800 * @plugin: A #FuPlugin
801 *
802 * Gets all the device GUIDs supported by the daemon.
803 *
Richard Hughes4eada342017-10-03 21:20:32 +0100804 * Returns: (element-type utf8) (transfer none): GUIDs
805 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100806 * Since: 1.0.0
807 **/
808GPtrArray *
809fu_plugin_get_supported (FuPlugin *plugin)
810{
811 FuPluginPrivate *priv = GET_PRIVATE (plugin);
812 return priv->supported_guids;
813}
814
815void
Richard Hughes49e5e052017-09-03 12:15:41 +0100816fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios)
817{
818 FuPluginPrivate *priv = GET_PRIVATE (plugin);
819 g_set_object (&priv->smbios, smbios);
820}
821
Richard Hughesb8f8db22017-04-25 15:56:00 +0100822/**
Richard Hughesb0829032017-01-10 09:27:08 +0000823 * fu_plugin_set_coldplug_delay:
824 * @plugin: A #FuPlugin
825 * @duration: A delay in milliseconds
826 *
827 * Set the minimum time that should be waited inbetween the call to
828 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
829 * to be the minimum hardware initialisation time from a datasheet.
830 *
831 * It is better to use this function rather than using a sleep() in the plugin
832 * itself as then only one delay is done in the daemon rather than waiting for
833 * each coldplug prepare in a serial way.
834 *
835 * Additionally, very long delays should be avoided as the daemon will be
836 * blocked from processing requests whilst the coldplug delay is being
837 * performed.
838 *
839 * Since: 0.8.0
840 **/
841void
842fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration)
843{
844 g_return_if_fail (FU_IS_PLUGIN (plugin));
845 g_return_if_fail (duration > 0);
846
847 /* check sanity */
848 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
849 g_warning ("duration of %ums is crazy, truncating to %ums",
850 duration,
851 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
852 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
853 }
854
855 /* emit */
856 g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
857}
858
Richard Hughesd0905142016-03-13 09:46:49 +0000859gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000860fu_plugin_runner_startup (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000861{
Richard Hughescff38bc2016-12-12 12:03:37 +0000862 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000863 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000864
865 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000866 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000867 return TRUE;
868
Richard Hughes639da472018-01-06 22:35:04 +0000869 /* no object loaded */
870 if (priv->module == NULL)
871 return TRUE;
872
Richard Hughesd0905142016-03-13 09:46:49 +0000873 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000874 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
875 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000876 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000877 g_debug ("performing startup() on %s", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +0000878 if (!func (plugin, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000879 g_prefix_error (error, "failed to startup %s: ", priv->name);
880 return FALSE;
881 }
882 return TRUE;
883}
884
885static gboolean
886fu_plugin_runner_offline_invalidate (GError **error)
887{
888 g_autoptr(GError) error_local = NULL;
889 g_autoptr(GFile) file1 = NULL;
890
891 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
892
893 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
894 if (!g_file_query_exists (file1, NULL))
895 return TRUE;
896 if (!g_file_delete (file1, NULL, &error_local)) {
897 g_set_error (error,
898 FWUPD_ERROR,
899 FWUPD_ERROR_INTERNAL,
900 "Cannot delete %s: %s",
901 FU_OFFLINE_TRIGGER_FILENAME,
902 error_local->message);
903 return FALSE;
904 }
905 return TRUE;
906}
907
908static gboolean
909fu_plugin_runner_offline_setup (GError **error)
910{
911 gint rc;
912
913 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
914
915 /* create symlink for the systemd-system-update-generator */
916 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
917 if (rc < 0) {
918 g_set_error (error,
919 FWUPD_ERROR,
920 FWUPD_ERROR_INTERNAL,
921 "Failed to create symlink %s to %s: %s",
922 FU_OFFLINE_TRIGGER_FILENAME,
923 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000924 return FALSE;
925 }
926 return TRUE;
927}
928
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000929static gboolean
930fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device,
931 const gchar *symbol_name, GError **error)
932{
933 FuPluginPrivate *priv = GET_PRIVATE (plugin);
934 FuPluginDeviceFunc func = NULL;
935
936 /* not enabled */
937 if (!priv->enabled)
938 return TRUE;
939
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000940 /* no object loaded */
941 if (priv->module == NULL)
942 return TRUE;
943
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000944 /* optional */
945 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
946 if (func == NULL)
947 return TRUE;
948 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
949 if (!func (plugin, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000950 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000951 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000952 priv->name);
953 return FALSE;
954 }
955 return TRUE;
956}
957
Richard Hughesdbd8c762018-06-15 20:31:40 +0100958static gboolean
959fu_plugin_runner_device_array_generic (FuPlugin *plugin, GPtrArray *devices,
960 const gchar *symbol_name, GError **error)
961{
962 FuPluginPrivate *priv = GET_PRIVATE (plugin);
963 FuPluginDeviceArrayFunc func = NULL;
964
965 /* not enabled */
966 if (!priv->enabled)
967 return TRUE;
968
969 /* no object loaded */
970 if (priv->module == NULL)
971 return TRUE;
972
973 /* optional */
974 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
975 if (func == NULL)
976 return TRUE;
977 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
978 if (!func (plugin, devices, error)) {
979 g_prefix_error (error, "failed to run %s() on %s: ",
980 symbol_name + 10,
981 priv->name);
982 return FALSE;
983 }
984 return TRUE;
985}
986
Richard Hughesd0905142016-03-13 09:46:49 +0000987gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000988fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000989{
Richard Hughescff38bc2016-12-12 12:03:37 +0000990 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000991 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000992
993 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000994 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000995 return TRUE;
996
Richard Hughes639da472018-01-06 22:35:04 +0000997 /* no object loaded */
998 if (priv->module == NULL)
999 return TRUE;
1000
Richard Hughesd0905142016-03-13 09:46:49 +00001001 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001002 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
1003 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001004 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001005 g_debug ("performing coldplug() on %s", priv->name);
1006 if (!func (plugin, error)) {
1007 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
1008 return FALSE;
1009 }
1010 return TRUE;
1011}
1012
Richard Hughes7b8b2022016-12-12 16:15:03 +00001013gboolean
Richard Hughes2de8f132018-01-17 09:12:02 +00001014fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error)
1015{
1016 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1017 FuPluginStartupFunc func = NULL;
1018
1019 /* not enabled */
1020 if (!priv->enabled)
1021 return TRUE;
1022
1023 /* no object loaded */
1024 if (priv->module == NULL)
1025 return TRUE;
1026
1027 /* optional */
1028 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1029 if (func == NULL)
1030 return TRUE;
1031 g_debug ("performing recoldplug() on %s", priv->name);
1032 if (!func (plugin, error)) {
1033 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
1034 return FALSE;
1035 }
1036 return TRUE;
1037}
1038
1039gboolean
Richard Hughes46487c92017-01-07 21:26:34 +00001040fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error)
1041{
1042 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1043 FuPluginStartupFunc func = NULL;
1044
1045 /* not enabled */
1046 if (!priv->enabled)
1047 return TRUE;
1048
Richard Hughes639da472018-01-06 22:35:04 +00001049 /* no object loaded */
1050 if (priv->module == NULL)
1051 return TRUE;
1052
Richard Hughes46487c92017-01-07 21:26:34 +00001053 /* optional */
1054 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1055 if (func == NULL)
1056 return TRUE;
1057 g_debug ("performing coldplug_prepare() on %s", priv->name);
1058 if (!func (plugin, error)) {
1059 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
1060 return FALSE;
1061 }
1062 return TRUE;
1063}
1064
1065gboolean
1066fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error)
1067{
1068 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1069 FuPluginStartupFunc func = NULL;
1070
1071 /* not enabled */
1072 if (!priv->enabled)
1073 return TRUE;
1074
Richard Hughes639da472018-01-06 22:35:04 +00001075 /* no object loaded */
1076 if (priv->module == NULL)
1077 return TRUE;
1078
Richard Hughes46487c92017-01-07 21:26:34 +00001079 /* optional */
1080 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1081 if (func == NULL)
1082 return TRUE;
1083 g_debug ("performing coldplug_cleanup() on %s", priv->name);
1084 if (!func (plugin, error)) {
1085 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
1086 return FALSE;
1087 }
1088 return TRUE;
1089}
1090
1091gboolean
Richard Hughesdbd8c762018-06-15 20:31:40 +01001092fu_plugin_runner_composite_prepare (FuPlugin *plugin, GPtrArray *devices, GError **error)
1093{
1094 return fu_plugin_runner_device_array_generic (plugin, devices,
1095 "fu_plugin_composite_prepare",
1096 error);
1097}
1098
1099gboolean
1100fu_plugin_runner_composite_cleanup (FuPlugin *plugin, GPtrArray *devices, GError **error)
1101{
1102 return fu_plugin_runner_device_array_generic (plugin, devices,
1103 "fu_plugin_composite_cleanup",
1104 error);
1105}
1106
1107gboolean
Richard Hughes7b8b2022016-12-12 16:15:03 +00001108fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error)
1109{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001110 return fu_plugin_runner_device_generic (plugin, device,
1111 "fu_plugin_update_prepare", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001112}
1113
1114gboolean
1115fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error)
1116{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001117 return fu_plugin_runner_device_generic (plugin, device,
1118 "fu_plugin_update_cleanup", error);
1119}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001120
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001121gboolean
1122fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error)
1123{
1124 return fu_plugin_runner_device_generic (plugin, device,
1125 "fu_plugin_update_attach", error);
1126}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001127
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001128gboolean
1129fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error)
1130{
1131 return fu_plugin_runner_device_generic (plugin, device,
1132 "fu_plugin_update_detach", error);
1133}
1134
1135gboolean
1136fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error)
1137{
1138 return fu_plugin_runner_device_generic (plugin, device,
1139 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001140}
1141
Richard Hughes104f6512017-11-24 11:44:57 +00001142gboolean
1143fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error)
1144{
1145 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1146 FuPluginUsbDeviceAddedFunc func = NULL;
1147
1148 /* not enabled */
1149 if (!priv->enabled)
1150 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001151
1152 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001153 if (priv->module == NULL)
1154 return TRUE;
1155
1156 /* optional */
1157 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1158 if (func != NULL) {
1159 g_debug ("performing usb_device_added() on %s", priv->name);
1160 return func (plugin, usb_device, error);
1161 }
1162 return TRUE;
1163}
1164
Richard Hughese1fd34d2017-08-24 14:19:51 +01001165void
1166fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device)
1167{
1168 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1169 FuPluginDeviceRegisterFunc func = NULL;
1170
1171 /* not enabled */
1172 if (!priv->enabled)
1173 return;
Richard Hughes34834102017-11-21 21:55:00 +00001174 if (priv->module == NULL)
1175 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001176
Mario Limonciello4910b242018-06-22 15:04:21 -05001177 /* don't notify plugins on their own devices */
1178 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (plugin)) == 0)
1179 return;
1180
Richard Hughese1fd34d2017-08-24 14:19:51 +01001181 /* optional */
1182 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1183 if (func != NULL) {
1184 g_debug ("performing device_added() on %s", priv->name);
1185 func (plugin, device);
1186 }
1187}
1188
Richard Hughescff38bc2016-12-12 12:03:37 +00001189static gboolean
1190fu_plugin_runner_schedule_update (FuPlugin *plugin,
1191 FuDevice *device,
1192 GBytes *blob_cab,
1193 GError **error)
1194{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001195 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001196 gchar tmpname[] = {"XXXXXX.cap"};
1197 g_autofree gchar *dirname = NULL;
1198 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001199 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001200 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001201 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001202 g_autoptr(GFile) file = NULL;
1203
1204 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001205 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001206 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001207 if (res_tmp != NULL) {
1208 g_set_error (error,
1209 FWUPD_ERROR,
1210 FWUPD_ERROR_ALREADY_PENDING,
1211 "%s is already scheduled to be updated",
1212 fu_device_get_id (device));
1213 return FALSE;
1214 }
1215
1216 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001217 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001218 file = g_file_new_for_path (dirname);
1219 if (!g_file_query_exists (file, NULL)) {
1220 if (!g_file_make_directory_with_parents (file, NULL, error))
1221 return FALSE;
1222 }
1223
1224 /* get a random filename */
1225 for (guint i = 0; i < 6; i++)
1226 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1227 filename = g_build_filename (dirname, tmpname, NULL);
1228
1229 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001230 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001231 if (!g_file_set_contents (filename,
1232 g_bytes_get_data (blob_cab, NULL),
1233 (gssize) g_bytes_get_size (blob_cab),
1234 error))
1235 return FALSE;
1236
1237 /* schedule for next boot */
1238 g_debug ("schedule %s to be installed to %s on next boot",
1239 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001240 release = fu_device_get_release_default (device);
1241 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1242 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001243
1244 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001245 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001246 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001247 return FALSE;
1248
1249 /* next boot we run offline */
1250 return fu_plugin_runner_offline_setup (error);
1251}
1252
1253gboolean
1254fu_plugin_runner_verify (FuPlugin *plugin,
1255 FuDevice *device,
1256 FuPluginVerifyFlags flags,
1257 GError **error)
1258{
1259 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001260 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001261 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001262
1263 /* not enabled */
1264 if (!priv->enabled)
1265 return TRUE;
1266
Richard Hughes639da472018-01-06 22:35:04 +00001267 /* no object loaded */
1268 if (priv->module == NULL)
1269 return TRUE;
1270
Richard Hughesababbb72017-06-15 20:18:36 +01001271 /* clear any existing verification checksums */
1272 checksums = fu_device_get_checksums (device);
1273 g_ptr_array_set_size (checksums, 0);
1274
Richard Hughescff38bc2016-12-12 12:03:37 +00001275 /* optional */
1276 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1277 if (func == NULL)
1278 return TRUE;
1279 g_debug ("performing verify() on %s", priv->name);
1280 if (!func (plugin, device, flags, error)) {
1281 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001282 return FALSE;
1283 }
1284 return TRUE;
1285}
1286
Richard Hughesd0905142016-03-13 09:46:49 +00001287gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001288fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001289{
Richard Hughescff38bc2016-12-12 12:03:37 +00001290 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001291
1292 /* final check */
1293 flags = fu_device_get_flags (device);
1294 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1295 g_set_error (error,
1296 FWUPD_ERROR,
1297 FWUPD_ERROR_NOT_SUPPORTED,
1298 "Device %s is not locked",
1299 fu_device_get_id (device));
1300 return FALSE;
1301 }
1302
Richard Hughes9c4b5312017-11-14 11:34:53 +00001303 /* run vfunc */
1304 if (!fu_plugin_runner_device_generic (plugin, device,
1305 "fu_plugin_unlock", error))
1306 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001307
1308 /* update with correct flags */
1309 flags = fu_device_get_flags (device);
1310 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1311 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1312 return TRUE;
1313}
1314
1315gboolean
1316fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001317 FuDevice *device,
1318 GBytes *blob_cab,
1319 GBytes *blob_fw,
1320 FwupdInstallFlags flags,
1321 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001322{
1323 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001324 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001325 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001326 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001327 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001328 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001329
1330 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001331 if (!priv->enabled) {
1332 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001333 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001334 }
Richard Hughesd0905142016-03-13 09:46:49 +00001335
Richard Hughes639da472018-01-06 22:35:04 +00001336 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001337 if (priv->module == NULL) {
1338 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001339 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001340 }
Richard Hughes639da472018-01-06 22:35:04 +00001341
Richard Hughesd0905142016-03-13 09:46:49 +00001342 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001343 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1344 if (update_func == NULL) {
1345 g_set_error_literal (error,
1346 FWUPD_ERROR,
1347 FWUPD_ERROR_NOT_SUPPORTED,
1348 "No update possible");
1349 return FALSE;
1350 }
Richard Hughesd0905142016-03-13 09:46:49 +00001351
Richard Hughesa785a1c2017-08-25 16:00:58 +01001352 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001353 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001354 if (blob_cab == NULL) {
1355 g_set_error_literal (error,
1356 FWUPD_ERROR,
1357 FWUPD_ERROR_NOT_SUPPORTED,
1358 "No cabinet archive to schedule");
1359 return FALSE;
1360 }
Richard Hughesa785a1c2017-08-25 16:00:58 +01001361 return fu_plugin_runner_schedule_update (plugin,
1362 device,
1363 blob_cab,
1364 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001365 }
1366
1367 /* cancel the pending action */
1368 if (!fu_plugin_runner_offline_invalidate (error))
1369 return FALSE;
1370
1371 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001372 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001373 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001374 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughesc0cd0232018-01-31 15:02:00 +00001375 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001376 g_propagate_error (error, error_update);
1377 return FALSE;
1378 }
1379
Richard Hughesf556d372017-06-15 19:49:18 +01001380 /* no longer valid */
1381 checksums = fu_device_get_checksums (device);
1382 g_ptr_array_set_size (checksums, 0);
1383
Richard Hughescff38bc2016-12-12 12:03:37 +00001384 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001385 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001386 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001387 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001388
Richard Hughes780ef3f2018-01-12 16:20:31 +00001389 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001390 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1391 if (!fu_history_modify_device (history, device,
1392 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1393 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001394 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001395
1396 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001397 release = fu_device_get_release_default (device_pending);
1398 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001399 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1400 g_autoptr(GError) error_local = NULL;
1401 g_autoptr(GFile) file = NULL;
1402 file = g_file_new_for_path (tmp);
1403 if (!g_file_delete (file, NULL, &error_local)) {
1404 g_set_error (error,
1405 FWUPD_ERROR,
1406 FWUPD_ERROR_INVALID_FILE,
1407 "Failed to delete %s: %s",
1408 tmp, error_local->message);
1409 return FALSE;
1410 }
1411 }
1412 }
Richard Hughesd0905142016-03-13 09:46:49 +00001413 return TRUE;
1414}
Richard Hughescff38bc2016-12-12 12:03:37 +00001415
1416gboolean
1417fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error)
1418{
1419 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001420 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001421
1422 /* not enabled */
1423 if (!priv->enabled)
1424 return TRUE;
1425
Richard Hughes639da472018-01-06 22:35:04 +00001426 /* no object loaded */
1427 if (priv->module == NULL)
1428 return TRUE;
1429
Richard Hughes65e44ca2018-01-30 17:26:30 +00001430 /* optional */
1431 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1432 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001433 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001434 g_debug ("performing clear_result() on %s", priv->name);
1435 if (!func (plugin, device, error)) {
1436 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001437 return FALSE;
1438 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001439 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001440}
1441
1442gboolean
1443fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1444{
1445 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001446 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001447
1448 /* not enabled */
1449 if (!priv->enabled)
1450 return TRUE;
1451
Richard Hughes639da472018-01-06 22:35:04 +00001452 /* no object loaded */
1453 if (priv->module == NULL)
1454 return TRUE;
1455
Richard Hughes65e44ca2018-01-30 17:26:30 +00001456 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001457 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001458 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001459 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001460 g_debug ("performing get_results() on %s", priv->name);
1461 if (!func (plugin, device, error)) {
1462 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001463 return FALSE;
1464 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001465 return TRUE;
1466}
1467
Richard Hughes08a37992017-09-12 12:57:43 +01001468/**
1469 * fu_plugin_get_order:
1470 * @plugin: a #FuPlugin
1471 *
1472 * Gets the plugin order, where higher numbers are run after lower
1473 * numbers.
1474 *
1475 * Returns: the integer value
1476 **/
1477guint
1478fu_plugin_get_order (FuPlugin *plugin)
1479{
1480 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1481 return priv->order;
1482}
1483
1484/**
1485 * fu_plugin_set_order:
1486 * @plugin: a #FuPlugin
1487 * @order: a integer value
1488 *
1489 * Sets the plugin order, where higher numbers are run after lower
1490 * numbers.
1491 **/
1492void
1493fu_plugin_set_order (FuPlugin *plugin, guint order)
1494{
1495 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1496 priv->order = order;
1497}
1498
1499/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001500 * fu_plugin_get_priority:
1501 * @plugin: a #FuPlugin
1502 *
1503 * Gets the plugin priority, where higher numbers are better.
1504 *
1505 * Returns: the integer value
1506 **/
1507guint
1508fu_plugin_get_priority (FuPlugin *plugin)
1509{
1510 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1511 return priv->priority;
1512}
1513
1514/**
1515 * fu_plugin_set_priority:
1516 * @plugin: a #FuPlugin
1517 * @priority: a integer value
1518 *
1519 * Sets the plugin priority, where higher numbers are better.
1520 **/
1521void
1522fu_plugin_set_priority (FuPlugin *plugin, guint priority)
1523{
1524 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1525 priv->priority = priority;
1526}
1527
1528/**
Richard Hughes08a37992017-09-12 12:57:43 +01001529 * fu_plugin_add_rule:
1530 * @plugin: a #FuPlugin
1531 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001532 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001533 *
1534 * If the plugin name is found, the rule will be used to sort the plugin list,
1535 * for example the plugin specified by @name will be ordered after this plugin
1536 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1537 *
1538 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1539 * If depsolving fails then fwupd will not start.
1540 **/
1541void
1542fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1543{
1544 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1545 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1546}
1547
1548/**
1549 * fu_plugin_get_rules:
1550 * @plugin: a #FuPlugin
1551 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1552 *
1553 * Gets the plugin IDs that should be run after this plugin.
1554 *
1555 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1556 **/
1557GPtrArray *
1558fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1559{
1560 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1561 return priv->rules[rule];
1562}
1563
Richard Hughes80b79bb2018-01-11 21:11:06 +00001564/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001565 * fu_plugin_has_rule:
1566 * @plugin: a #FuPlugin
1567 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1568 * @name: a plugin name, e.g. `upower`
1569 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001570 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001571 *
1572 * Returns: %TRUE if the name exists for the specific rule
1573 **/
1574gboolean
1575fu_plugin_has_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1576{
1577 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1578 for (guint i = 0; i < priv->rules[rule]->len; i++) {
1579 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
1580 if (g_strcmp0 (tmp, name) == 0)
1581 return TRUE;
1582 }
1583 return FALSE;
1584}
1585
1586/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00001587 * fu_plugin_add_report_metadata:
1588 * @plugin: a #FuPlugin
1589 * @key: a string, e.g. `FwupdateVersion`
1590 * @value: a string, e.g. `10`
1591 *
1592 * Sets any additional metadata to be included in the firmware report to aid
1593 * debugging problems.
1594 *
1595 * Any data included here will be sent to the metadata server after user
1596 * confirmation.
1597 **/
1598void
1599fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1600{
1601 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1602 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1603}
1604
1605/**
1606 * fu_plugin_get_report_metadata:
1607 * @plugin: a #FuPlugin
1608 *
1609 * Returns the list of additional metadata to be added when filing a report.
1610 *
1611 * Returns: (transfer none): the map of report metadata
1612 **/
1613GHashTable *
1614fu_plugin_get_report_metadata (FuPlugin *plugin)
1615{
1616 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1617 return priv->report_metadata;
1618}
1619
Mario Limonciello963dc422018-02-27 14:26:58 -06001620/**
1621 * fu_plugin_get_config_value:
1622 * @plugin: a #FuPlugin
1623 * @key: A settings key
1624 *
1625 * Return the value of a key if it's been configured
1626 *
1627 * Since: 1.0.6
1628 **/
1629gchar *
1630fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key)
1631{
Richard Hughes4be17d12018-05-30 20:36:29 +01001632 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06001633 g_autofree gchar *conf_file = NULL;
1634 g_autofree gchar *conf_path = NULL;
1635 g_autoptr(GKeyFile) keyfile = NULL;
1636 const gchar *plugin_name;
1637
Richard Hughes4be17d12018-05-30 20:36:29 +01001638 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Mario Limonciello963dc422018-02-27 14:26:58 -06001639 plugin_name = fu_plugin_get_name (plugin);
1640 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01001641 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06001642 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1643 return NULL;
1644 keyfile = g_key_file_new ();
1645 if (!g_key_file_load_from_file (keyfile, conf_path,
1646 G_KEY_FILE_NONE, NULL))
1647 return NULL;
1648 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1649}
1650
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001651/**
1652 * fu_plugin_name_compare:
1653 * @plugin1: first #FuPlugin to compare.
1654 * @plugin2: second #FuPlugin to compare.
1655 *
1656 * Compares two plugins by their names.
1657 *
1658 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1659 **/
1660gint
1661fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1662{
1663 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1664 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1665 return g_strcmp0 (priv1->name, priv2->name);
1666}
1667
1668/**
1669 * fu_plugin_order_compare:
1670 * @plugin1: first #FuPlugin to compare.
1671 * @plugin2: second #FuPlugin to compare.
1672 *
1673 * Compares two plugins by their depsolved order.
1674 *
1675 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1676 **/
1677gint
1678fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1679{
1680 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1681 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1682 if (priv1->order < priv2->order)
1683 return -1;
1684 if (priv1->order > priv2->order)
1685 return 1;
1686 return 0;
1687}
1688
Richard Hughescff38bc2016-12-12 12:03:37 +00001689static void
1690fu_plugin_class_init (FuPluginClass *klass)
1691{
1692 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1693 object_class->finalize = fu_plugin_finalize;
1694 signals[SIGNAL_DEVICE_ADDED] =
1695 g_signal_new ("device-added",
1696 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1697 G_STRUCT_OFFSET (FuPluginClass, device_added),
1698 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1699 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1700 signals[SIGNAL_DEVICE_REMOVED] =
1701 g_signal_new ("device-removed",
1702 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1703 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1704 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1705 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001706 signals[SIGNAL_DEVICE_REGISTER] =
1707 g_signal_new ("device-register",
1708 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1709 G_STRUCT_OFFSET (FuPluginClass, device_register),
1710 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1711 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001712 signals[SIGNAL_RECOLDPLUG] =
1713 g_signal_new ("recoldplug",
1714 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1715 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1716 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1717 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001718 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1719 g_signal_new ("set-coldplug-delay",
1720 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1721 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1722 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1723 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001724}
1725
1726static void
1727fu_plugin_init (FuPlugin *plugin)
1728{
1729 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1730 priv->enabled = TRUE;
1731 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1732 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001733 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001734 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 +01001735 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1736 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001737}
1738
1739static void
1740fu_plugin_finalize (GObject *object)
1741{
1742 FuPlugin *plugin = FU_PLUGIN (object);
1743 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1744 FuPluginInitFunc func = NULL;
1745
1746 /* optional */
1747 if (priv->module != NULL) {
1748 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1749 if (func != NULL) {
1750 g_debug ("performing destroy() on %s", priv->name);
1751 func (plugin);
1752 }
1753 }
1754
Richard Hughes08a37992017-09-12 12:57:43 +01001755 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1756 g_ptr_array_unref (priv->rules[i]);
1757
Richard Hughescff38bc2016-12-12 12:03:37 +00001758 if (priv->usb_ctx != NULL)
1759 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001760 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001761 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001762 if (priv->quirks != NULL)
1763 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001764 if (priv->supported_guids != NULL)
1765 g_ptr_array_unref (priv->supported_guids);
Richard Hughes49e5e052017-09-03 12:15:41 +01001766 if (priv->smbios != NULL)
1767 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001768 if (priv->runtime_versions != NULL)
1769 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001770 if (priv->compile_versions != NULL)
1771 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00001772 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001773 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001774 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001775 g_free (priv->name);
1776 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05001777 /* Must happen as the last step to avoid prematurely
1778 * freeing memory held by the plugin */
1779#ifndef RUNNING_ON_VALGRIND
1780 if (priv->module != NULL)
1781 g_module_close (priv->module);
1782#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001783
1784 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1785}
1786
1787FuPlugin *
1788fu_plugin_new (void)
1789{
1790 FuPlugin *plugin;
1791 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1792 return plugin;
1793}