blob: 123143c48995418c1143dab371db93c16d42c98c [file] [log] [blame]
Richard Hughesd0905142016-03-13 09:46:49 +00001/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
Richard Hughes2de8f132018-01-17 09:12:02 +00003 * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com>
Richard Hughesd0905142016-03-13 09:46:49 +00004 *
Mario Limonciello51308e62018-05-28 20:05:46 -05005 * SPDX-License-Identifier: LGPL-2.1+
Richard Hughesd0905142016-03-13 09:46:49 +00006 */
7
8#include "config.h"
9
Richard Hughescff38bc2016-12-12 12:03:37 +000010#include <fwupd.h>
11#include <gmodule.h>
12#include <appstream-glib.h>
13#include <errno.h>
14#include <string.h>
15#include <gio/gunixinputstream.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060016#ifdef HAVE_VALGRIND
Richard Hughes576c0122017-02-24 09:47:00 +000017#include <valgrind.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060018#endif /* HAVE_VALGRIND */
Richard Hughesd0905142016-03-13 09:46:49 +000019
Richard Hughes9dde04f2017-09-13 12:07:15 +010020#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000021#include "fu-plugin-private.h"
Richard Hughesbc3a4e12018-01-06 22:41:47 +000022#include "fu-history.h"
Richard Hughesd0905142016-03-13 09:46:49 +000023
Richard Hughes4eada342017-10-03 21:20:32 +010024/**
25 * SECTION:fu-plugin
26 * @short_description: a daemon plugin
27 *
28 * An object that represents a plugin run by the daemon.
29 *
30 * See also: #FuDevice
31 */
32
Richard Hughesb0829032017-01-10 09:27:08 +000033#define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */
34
Richard Hughescff38bc2016-12-12 12:03:37 +000035static void fu_plugin_finalize (GObject *object);
36
37typedef struct {
38 GModule *module;
39 GUsbContext *usb_ctx;
40 gboolean enabled;
Richard Hughes08a37992017-09-12 12:57:43 +010041 guint order;
42 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughescff38bc2016-12-12 12:03:37 +000043 gchar *name;
Richard Hughesd7704d42017-08-08 20:29:09 +010044 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010045 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010046 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010047 GHashTable *compile_versions;
Richard Hughes1354ea92017-09-19 15:58:31 +010048 GPtrArray *supported_guids;
49 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000050 GHashTable *devices; /* platform_id:GObject */
Richard Hughesae3d65f2016-12-16 09:38:01 +000051 GHashTable *devices_delay; /* FuDevice:FuPluginHelper */
Richard Hughes80b79bb2018-01-11 21:11:06 +000052 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000053 FuPluginData *data;
54} FuPluginPrivate;
55
56enum {
57 SIGNAL_DEVICE_ADDED,
58 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010059 SIGNAL_DEVICE_REGISTER,
Richard Hughes362d6d72017-01-07 21:42:14 +000060 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000061 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughescff38bc2016-12-12 12:03:37 +000062 SIGNAL_LAST
63};
64
65static guint signals[SIGNAL_LAST] = { 0 };
66
67G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
68#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
69
70typedef const gchar *(*FuPluginGetNameFunc) (void);
71typedef void (*FuPluginInitFunc) (FuPlugin *plugin);
72typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin,
73 GError **error);
Richard Hughese1fd34d2017-08-24 14:19:51 +010074typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin,
75 FuDevice *device);
Richard Hughescff38bc2016-12-12 12:03:37 +000076typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin,
77 FuDevice *device,
78 GError **error);
Richard Hughesdbd8c762018-06-15 20:31:40 +010079typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *plugin,
80 GPtrArray *devices,
81 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +000082typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin,
83 FuDevice *device,
84 FuPluginVerifyFlags flags,
85 GError **error);
86typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin,
87 FuDevice *device,
88 GBytes *blob_fw,
89 FwupdInstallFlags flags,
90 GError **error);
Richard Hughes104f6512017-11-24 11:44:57 +000091typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin,
92 GUsbDevice *usb_device,
93 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +000094
Richard Hughes57d18222017-01-10 16:02:59 +000095/**
96 * fu_plugin_get_name:
97 * @plugin: A #FuPlugin
98 *
99 * Gets the plugin name.
100 *
101 * Returns: a plugin name, or %NULL for unknown.
102 *
103 * Since: 0.8.0
104 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000105const gchar *
106fu_plugin_get_name (FuPlugin *plugin)
Richard Hughesd0905142016-03-13 09:46:49 +0000107{
Richard Hughescff38bc2016-12-12 12:03:37 +0000108 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000109 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000110 return priv->name;
111}
Richard Hughesd0905142016-03-13 09:46:49 +0000112
Richard Hughes34834102017-11-21 21:55:00 +0000113void
114fu_plugin_set_name (FuPlugin *plugin, const gchar *name)
115{
116 FuPluginPrivate *priv = GET_PRIVATE (plugin);
117 g_return_if_fail (FU_IS_PLUGIN (plugin));
118 g_return_if_fail (name != NULL);
119 g_free (priv->name);
120 priv->name = g_strdup (name);
121}
122
Richard Hughes57d18222017-01-10 16:02:59 +0000123/**
124 * fu_plugin_cache_lookup:
125 * @plugin: A #FuPlugin
126 * @id: the key
127 *
128 * Finds an object in the per-plugin cache.
129 *
130 * Returns: (transfer none): a #GObject, or %NULL for unfound.
131 *
132 * Since: 0.8.0
133 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000134gpointer
135fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id)
136{
137 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000138 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
139 g_return_val_if_fail (id != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000140 return g_hash_table_lookup (priv->devices, id);
141}
Richard Hughesd0905142016-03-13 09:46:49 +0000142
Richard Hughes57d18222017-01-10 16:02:59 +0000143/**
144 * fu_plugin_cache_add:
145 * @plugin: A #FuPlugin
146 * @id: the key
147 * @dev: a #GObject, typically a #FuDevice
148 *
149 * Adds an object to the per-plugin cache.
150 *
151 * Since: 0.8.0
152 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000153void
154fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev)
155{
156 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000157 g_return_if_fail (FU_IS_PLUGIN (plugin));
158 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000159 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
160}
161
Richard Hughes57d18222017-01-10 16:02:59 +0000162/**
163 * fu_plugin_cache_remove:
164 * @plugin: A #FuPlugin
165 * @id: the key
166 *
167 * Removes an object from the per-plugin cache.
168 *
169 * Since: 0.8.0
170 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000171void
172fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id)
173{
174 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000175 g_return_if_fail (FU_IS_PLUGIN (plugin));
176 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000177 g_hash_table_remove (priv->devices, id);
178}
179
Richard Hughes57d18222017-01-10 16:02:59 +0000180/**
181 * fu_plugin_get_data:
182 * @plugin: A #FuPlugin
183 *
Richard Hughes4eada342017-10-03 21:20:32 +0100184 * Gets the per-plugin allocated private data. This will return %NULL unless
185 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000186 *
Richard Hughes4eada342017-10-03 21:20:32 +0100187 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000188 *
189 * Since: 0.8.0
190 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000191FuPluginData *
192fu_plugin_get_data (FuPlugin *plugin)
193{
194 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000195 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000196 return priv->data;
197}
198
Richard Hughes57d18222017-01-10 16:02:59 +0000199/**
200 * fu_plugin_alloc_data:
201 * @plugin: A #FuPlugin
202 * @data_sz: the size to allocate
203 *
204 * Allocates the per-plugin allocated private data.
205 *
206 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
207 *
208 * Since: 0.8.0
209 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000210FuPluginData *
211fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz)
212{
213 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000214 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000215 if (priv->data != NULL) {
216 g_critical ("fu_plugin_alloc_data() already used by plugin");
217 return priv->data;
218 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000219 priv->data = g_malloc0 (data_sz);
220 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000221}
222
Richard Hughes57d18222017-01-10 16:02:59 +0000223/**
224 * fu_plugin_get_usb_context:
225 * @plugin: A #FuPlugin
226 *
227 * Gets the shared USB context that all plugins can use.
228 *
229 * Returns: (transfer none): a #GUsbContext.
230 *
231 * Since: 0.8.0
232 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000233GUsbContext *
234fu_plugin_get_usb_context (FuPlugin *plugin)
235{
Richard Hughescff38bc2016-12-12 12:03:37 +0000236 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000237 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000238 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000239}
240
241void
242fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx)
243{
Richard Hughescff38bc2016-12-12 12:03:37 +0000244 FuPluginPrivate *priv = GET_PRIVATE (plugin);
245 g_set_object (&priv->usb_ctx, usb_ctx);
246}
247
Richard Hughes57d18222017-01-10 16:02:59 +0000248/**
249 * fu_plugin_get_enabled:
250 * @plugin: A #FuPlugin
251 *
Richard Hughes4eada342017-10-03 21:20:32 +0100252 * Returns if the plugin is enabled. Plugins may self-disable using
253 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000254 *
255 * Returns: %TRUE if the plugin is currently enabled.
256 *
257 * Since: 0.8.0
258 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000259gboolean
260fu_plugin_get_enabled (FuPlugin *plugin)
261{
262 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000263 g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000264 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000265}
266
Richard Hughes57d18222017-01-10 16:02:59 +0000267/**
268 * fu_plugin_set_enabled:
269 * @plugin: A #FuPlugin
270 * @enabled: the enabled value
271 *
272 * Enables or disables a plugin. Plugins can self-disable at any point.
273 *
274 * Since: 0.8.0
275 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000276void
Richard Hughescff38bc2016-12-12 12:03:37 +0000277fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000278{
Richard Hughescff38bc2016-12-12 12:03:37 +0000279 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000280 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughescff38bc2016-12-12 12:03:37 +0000281 priv->enabled = enabled;
282}
283
Richard Hughes1e456bc2018-05-10 20:16:16 +0100284gchar *
285fu_plugin_guess_name_from_fn (const gchar *filename)
286{
287 const gchar *prefix = "libfu_plugin_";
288 gchar *name;
289 gchar *str = g_strstr_len (filename, -1, prefix);
290 if (str == NULL)
291 return NULL;
292 name = g_strdup (str + strlen (prefix));
293 g_strdelimit (name, ".", '\0');
294 return name;
295}
296
Richard Hughescff38bc2016-12-12 12:03:37 +0000297gboolean
298fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error)
299{
300 FuPluginPrivate *priv = GET_PRIVATE (plugin);
301 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000302
303 priv->module = g_module_open (filename, 0);
304 if (priv->module == NULL) {
305 g_set_error (error,
306 G_IO_ERROR,
307 G_IO_ERROR_FAILED,
308 "failed to open plugin: %s",
309 g_module_error ());
310 return FALSE;
311 }
312
313 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100314 if (priv->name == NULL)
315 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000316
317 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000318 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
319 if (func != NULL) {
320 g_debug ("performing init() on %s", filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000321 func (plugin);
322 }
323
Richard Hughescff38bc2016-12-12 12:03:37 +0000324 return TRUE;
325}
326
Richard Hughes57d18222017-01-10 16:02:59 +0000327/**
328 * fu_plugin_device_add:
329 * @plugin: A #FuPlugin
330 * @device: A #FuDevice
331 *
332 * Asks the daemon to add a device to the exported list. If this device ID
333 * has already been added by a different plugin then this request will be
334 * ignored.
335 *
336 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
337 * actually flashing an image to the hardware so that higher-priority plugins
338 * can add the device themselves.
339 *
340 * Since: 0.8.0
341 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000342void
343fu_plugin_device_add (FuPlugin *plugin, FuDevice *device)
344{
Richard Hughes5e447292018-04-27 14:25:54 +0100345 GPtrArray *children;
346
Richard Hughesccd78a92017-01-11 16:57:41 +0000347 g_return_if_fail (FU_IS_PLUGIN (plugin));
348 g_return_if_fail (FU_IS_DEVICE (device));
349
Richard Hughescff38bc2016-12-12 12:03:37 +0000350 g_debug ("emit added from %s: %s",
351 fu_plugin_get_name (plugin),
352 fu_device_get_id (device));
353 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
354 fu_device_set_plugin (device, fu_plugin_get_name (plugin));
355 g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100356
357 /* add children */
358 children = fu_device_get_children (device);
359 for (guint i = 0; i < children->len; i++) {
360 FuDevice *child = g_ptr_array_index (children, i);
361 fu_plugin_device_add (plugin, child);
362 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000363}
364
Richard Hughese1fd34d2017-08-24 14:19:51 +0100365/**
366 * fu_plugin_device_register:
367 * @plugin: A #FuPlugin
368 * @device: A #FuDevice
369 *
370 * Registers the device with other plugins so they can set metadata.
371 *
372 * Plugins do not have to call this manually as this is done automatically
373 * when using fu_plugin_device_add(). They may wish to use this manually
374 * if for intance the coldplug should be ignored based on the metadata
375 * set from other plugins.
376 *
377 * Since: 0.9.7
378 **/
379void
380fu_plugin_device_register (FuPlugin *plugin, FuDevice *device)
381{
382 g_return_if_fail (FU_IS_PLUGIN (plugin));
383 g_return_if_fail (FU_IS_DEVICE (device));
384
385 g_debug ("emit device-register from %s: %s",
386 fu_plugin_get_name (plugin),
387 fu_device_get_id (device));
388 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device);
389}
390
Richard Hughesae3d65f2016-12-16 09:38:01 +0000391typedef struct {
392 FuPlugin *plugin;
393 FuDevice *device;
394 guint timeout_id;
Richard Hughesd4184cf2016-12-21 16:05:17 +0000395 GHashTable *devices;
Richard Hughesae3d65f2016-12-16 09:38:01 +0000396} FuPluginHelper;
397
398static void
399fu_plugin_helper_free (FuPluginHelper *helper)
400{
401 g_object_unref (helper->plugin);
402 g_object_unref (helper->device);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000403 g_hash_table_unref (helper->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000404 g_free (helper);
405}
406
407static gboolean
408fu_plugin_device_add_delay_cb (gpointer user_data)
409{
410 FuPluginHelper *helper = (FuPluginHelper *) user_data;
411 fu_plugin_device_add (helper->plugin, helper->device);
Richard Hughes0dec2742017-09-18 11:07:39 +0100412 g_hash_table_remove (helper->devices, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000413 fu_plugin_helper_free (helper);
414 return FALSE;
415}
416
Richard Hughes57d18222017-01-10 16:02:59 +0000417/**
Richard Hughesf0a799e2017-01-17 20:13:30 +0000418 * fu_plugin_has_device_delay:
419 * @plugin: A #FuPlugin
420 *
421 * Returns if the device has a pending device that is waiting to be added.
422 *
423 * Returns: %TRUE if a device is waiting to be added
424 *
425 * Since: 0.8.0
426 **/
427gboolean
428fu_plugin_has_device_delay (FuPlugin *plugin)
429{
430 FuPluginPrivate *priv = GET_PRIVATE (plugin);
431 return g_hash_table_size (priv->devices_delay) > 0;
432}
433
434/**
Richard Hughes57d18222017-01-10 16:02:59 +0000435 * fu_plugin_device_add_delay:
436 * @plugin: A #FuPlugin
437 * @device: A #FuDevice
438 *
439 * Asks the daemon to add a device to the exported list after a small delay.
440 *
441 * Since: 0.8.0
442 **/
Richard Hughesae3d65f2016-12-16 09:38:01 +0000443void
444fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device)
445{
446 FuPluginPrivate *priv = GET_PRIVATE (plugin);
447 FuPluginHelper *helper;
Richard Hughesccd78a92017-01-11 16:57:41 +0000448
449 g_return_if_fail (FU_IS_PLUGIN (plugin));
450 g_return_if_fail (FU_IS_DEVICE (device));
451
Richard Hughes6ad951d2017-02-03 10:12:12 +0000452 /* already waiting for add */
453 helper = g_hash_table_lookup (priv->devices_delay, device);
454 if (helper != NULL) {
Richard Hughes3cca1c62017-07-21 13:38:20 +0100455 g_debug ("ignoring add-delay as device %s already pending",
456 fu_device_get_id (device));
Richard Hughes6ad951d2017-02-03 10:12:12 +0000457 return;
458 }
459
460 /* add after a small delay */
Richard Hughesae3d65f2016-12-16 09:38:01 +0000461 g_debug ("waiting a small time for other plugins");
462 helper = g_new0 (FuPluginHelper, 1);
463 helper->plugin = g_object_ref (plugin);
464 helper->device = g_object_ref (device);
465 helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000466 helper->devices = g_hash_table_ref (priv->devices_delay);
467 g_hash_table_insert (helper->devices, device, helper);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000468}
469
Richard Hughes57d18222017-01-10 16:02:59 +0000470/**
Richard Hughes4eada342017-10-03 21:20:32 +0100471 * fu_plugin_device_remove:
Richard Hughes57d18222017-01-10 16:02:59 +0000472 * @plugin: A #FuPlugin
473 * @device: A #FuDevice
474 *
475 * Asks the daemon to remove a device from the exported list.
476 *
477 * Since: 0.8.0
478 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000479void
480fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device)
481{
Richard Hughesae3d65f2016-12-16 09:38:01 +0000482 FuPluginPrivate *priv = GET_PRIVATE (plugin);
483 FuPluginHelper *helper;
484
Richard Hughesccd78a92017-01-11 16:57:41 +0000485 g_return_if_fail (FU_IS_PLUGIN (plugin));
486 g_return_if_fail (FU_IS_DEVICE (device));
487
Richard Hughesae3d65f2016-12-16 09:38:01 +0000488 /* waiting for add */
489 helper = g_hash_table_lookup (priv->devices_delay, device);
490 if (helper != NULL) {
491 g_debug ("ignoring remove from delayed addition");
492 g_source_remove (helper->timeout_id);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000493 g_hash_table_remove (priv->devices_delay, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000494 fu_plugin_helper_free (helper);
495 return;
496 }
497
Richard Hughescff38bc2016-12-12 12:03:37 +0000498 g_debug ("emit removed from %s: %s",
499 fu_plugin_get_name (plugin),
500 fu_device_get_id (device));
501 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device);
502}
503
Richard Hughes57d18222017-01-10 16:02:59 +0000504/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000505 * fu_plugin_request_recoldplug:
Richard Hughes362d6d72017-01-07 21:42:14 +0000506 * @plugin: A #FuPlugin
507 *
508 * Ask all the plugins to coldplug all devices, which will include the prepare()
509 * and cleanup() phases. Duplicate devices added will be ignored.
510 *
511 * Since: 0.8.0
512 **/
513void
Richard Hughes2de8f132018-01-17 09:12:02 +0000514fu_plugin_request_recoldplug (FuPlugin *plugin)
Richard Hughes362d6d72017-01-07 21:42:14 +0000515{
Richard Hughesccd78a92017-01-11 16:57:41 +0000516 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughes362d6d72017-01-07 21:42:14 +0000517 g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0);
518}
519
Richard Hughesb0829032017-01-10 09:27:08 +0000520/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100521 * fu_plugin_check_hwid:
522 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100523 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100524 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100525 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100526 * specific system can be shown using the `fwupdmgr hwids` command.
527 *
Richard Hughes4eada342017-10-03 21:20:32 +0100528 * Returns: %TRUE if the HwId is found on the system.
529 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100530 * Since: 0.9.1
531 **/
532gboolean
533fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid)
534{
535 FuPluginPrivate *priv = GET_PRIVATE (plugin);
536 if (priv->hwids == NULL)
537 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100538 return fu_hwids_has_guid (priv->hwids, hwid);
539}
540
541/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100542 * fu_plugin_check_supported:
543 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100544 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100545 *
546 * Checks to see if a specific device GUID is supported, i.e. available in the
547 * AppStream metadata.
548 *
Richard Hughes4eada342017-10-03 21:20:32 +0100549 * Returns: %TRUE if the device is supported.
550 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100551 * Since: 1.0.0
552 **/
553gboolean
554fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid)
555{
556 FuPluginPrivate *priv = GET_PRIVATE (plugin);
557 if (priv->supported_guids == NULL)
558 return FALSE;
559 for (guint i = 0; i < priv->supported_guids->len; i++) {
560 const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i);
561 if (g_strcmp0 (guid, guid_tmp) == 0)
562 return TRUE;
563 }
564 return FALSE;
565}
566
567/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100568 * fu_plugin_get_dmi_value:
569 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100570 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100571 *
572 * Gets a hardware DMI value.
573 *
Richard Hughes4eada342017-10-03 21:20:32 +0100574 * Returns: The string, or %NULL
575 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100576 * Since: 0.9.7
577 **/
578const gchar *
579fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id)
580{
581 FuPluginPrivate *priv = GET_PRIVATE (plugin);
582 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100583 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100584 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100585}
586
Richard Hughes49e5e052017-09-03 12:15:41 +0100587/**
588 * fu_plugin_get_smbios_string:
589 * @plugin: A #FuPlugin
590 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
591 * @offset: A SMBIOS offset
592 *
593 * Gets a hardware SMBIOS string.
594 *
595 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
596 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
597 *
Richard Hughes4eada342017-10-03 21:20:32 +0100598 * Returns: A string, or %NULL
599 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100600 * Since: 0.9.8
601 **/
602const gchar *
603fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset)
604{
605 FuPluginPrivate *priv = GET_PRIVATE (plugin);
606 if (priv->smbios == NULL)
607 return NULL;
608 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
609}
610
611/**
612 * fu_plugin_get_smbios_data:
613 * @plugin: A #FuPlugin
614 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
615 *
616 * Gets a hardware SMBIOS data.
617 *
Richard Hughes4eada342017-10-03 21:20:32 +0100618 * Returns: (transfer none): A #GBytes, or %NULL
619 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100620 * Since: 0.9.8
621 **/
622GBytes *
623fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type)
624{
625 FuPluginPrivate *priv = GET_PRIVATE (plugin);
626 if (priv->smbios == NULL)
627 return NULL;
628 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
629}
630
Richard Hughesb8f8db22017-04-25 15:56:00 +0100631void
Richard Hughesd7704d42017-08-08 20:29:09 +0100632fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100633{
634 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesd7704d42017-08-08 20:29:09 +0100635 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100636}
637
Richard Hughes49e5e052017-09-03 12:15:41 +0100638void
Richard Hughes1354ea92017-09-19 15:58:31 +0100639fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids)
640{
641 FuPluginPrivate *priv = GET_PRIVATE (plugin);
642 if (priv->supported_guids != NULL)
643 g_ptr_array_unref (priv->supported_guids);
644 priv->supported_guids = g_ptr_array_ref (supported_guids);
645}
646
Richard Hughes9c028f02017-10-28 21:14:28 +0100647void
648fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks)
649{
650 FuPluginPrivate *priv = GET_PRIVATE (plugin);
651 g_set_object (&priv->quirks, quirks);
652}
653
654/**
655 * fu_plugin_get_quirks:
656 * @plugin: A #FuPlugin
657 *
658 * Returns the hardware database object. This can be used to discover device
659 * quirks or other device-specific settings.
660 *
661 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
662 *
663 * Since: 1.0.1
664 **/
665FuQuirks *
666fu_plugin_get_quirks (FuPlugin *plugin)
667{
668 FuPluginPrivate *priv = GET_PRIVATE (plugin);
669 return priv->quirks;
670}
671
Richard Hughes0eb123b2018-04-19 12:00:04 +0100672void
673fu_plugin_set_runtime_versions (FuPlugin *plugin, GHashTable *runtime_versions)
674{
675 FuPluginPrivate *priv = GET_PRIVATE (plugin);
676 priv->runtime_versions = g_hash_table_ref (runtime_versions);
677}
678
679/**
680 * fu_plugin_add_runtime_version:
681 * @plugin: A #FuPlugin
682 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
683 * @version: A version string, e.g. "1.2.3"
684 *
685 * Sets a runtime version of a specific dependancy.
686 *
687 * Since: 1.0.7
688 **/
689void
690fu_plugin_add_runtime_version (FuPlugin *plugin,
691 const gchar *component_id,
692 const gchar *version)
693{
694 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesb01b4862018-04-20 16:39:48 +0100695 if (priv->runtime_versions == NULL)
696 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100697 g_hash_table_insert (priv->runtime_versions,
698 g_strdup (component_id),
699 g_strdup (version));
700}
701
Richard Hughes34e0dab2018-04-20 16:43:00 +0100702void
703fu_plugin_set_compile_versions (FuPlugin *plugin, GHashTable *compile_versions)
704{
705 FuPluginPrivate *priv = GET_PRIVATE (plugin);
706 priv->compile_versions = g_hash_table_ref (compile_versions);
707}
708
709/**
710 * fu_plugin_add_compile_version:
711 * @plugin: A #FuPlugin
712 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
713 * @version: A version string, e.g. "1.2.3"
714 *
715 * Sets a compile-time version of a specific dependancy.
716 *
717 * Since: 1.0.7
718 **/
719void
720fu_plugin_add_compile_version (FuPlugin *plugin,
721 const gchar *component_id,
722 const gchar *version)
723{
724 FuPluginPrivate *priv = GET_PRIVATE (plugin);
725 if (priv->compile_versions == NULL)
726 return;
727 g_hash_table_insert (priv->compile_versions,
728 g_strdup (component_id),
729 g_strdup (version));
730}
731
Richard Hughes9c028f02017-10-28 21:14:28 +0100732/**
733 * fu_plugin_lookup_quirk_by_id:
734 * @plugin: A #FuPlugin
735 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
736 * @id: An ID to match the entry, e.g. "012345"
737 *
738 * Looks up an entry in the hardware database using a string value.
739 *
740 * Returns: (transfer none): values from the database, or %NULL if not found
741 *
742 * Since: 1.0.1
743 **/
744const gchar *
745fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *prefix, const gchar *id)
746{
747 FuPluginPrivate *priv = GET_PRIVATE (plugin);
748 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
749
750 /* wildcard */
751 if (g_strstr_len (id, -1, "*") != NULL)
752 return fu_quirks_lookup_by_glob (priv->quirks, prefix, id);
753
754 /* exact ID */
755 return fu_quirks_lookup_by_id (priv->quirks, prefix, id);
756}
757
758/**
759 * fu_plugin_lookup_quirk_by_usb_device:
760 * @plugin: A #FuPlugin
761 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
762 * @dev: A #GUsbDevice
763 *
764 * Looks up an entry in the hardware database using various keys generated
765 * from @dev.
766 *
767 * Returns: (transfer none): values from the database, or %NULL if not found
768 *
769 * Since: 1.0.1
770 **/
771const gchar *
772fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin, const gchar *prefix, GUsbDevice *dev)
773{
774 FuPluginPrivate *priv = GET_PRIVATE (plugin);
775 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
776 return fu_quirks_lookup_by_usb_device (priv->quirks, prefix, dev);
777}
778
Richard Hughes1354ea92017-09-19 15:58:31 +0100779/**
780 * fu_plugin_get_supported:
781 * @plugin: A #FuPlugin
782 *
783 * Gets all the device GUIDs supported by the daemon.
784 *
Richard Hughes4eada342017-10-03 21:20:32 +0100785 * Returns: (element-type utf8) (transfer none): GUIDs
786 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100787 * Since: 1.0.0
788 **/
789GPtrArray *
790fu_plugin_get_supported (FuPlugin *plugin)
791{
792 FuPluginPrivate *priv = GET_PRIVATE (plugin);
793 return priv->supported_guids;
794}
795
796void
Richard Hughes49e5e052017-09-03 12:15:41 +0100797fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios)
798{
799 FuPluginPrivate *priv = GET_PRIVATE (plugin);
800 g_set_object (&priv->smbios, smbios);
801}
802
Richard Hughesb8f8db22017-04-25 15:56:00 +0100803/**
Richard Hughesb0829032017-01-10 09:27:08 +0000804 * fu_plugin_set_coldplug_delay:
805 * @plugin: A #FuPlugin
806 * @duration: A delay in milliseconds
807 *
808 * Set the minimum time that should be waited inbetween the call to
809 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
810 * to be the minimum hardware initialisation time from a datasheet.
811 *
812 * It is better to use this function rather than using a sleep() in the plugin
813 * itself as then only one delay is done in the daemon rather than waiting for
814 * each coldplug prepare in a serial way.
815 *
816 * Additionally, very long delays should be avoided as the daemon will be
817 * blocked from processing requests whilst the coldplug delay is being
818 * performed.
819 *
820 * Since: 0.8.0
821 **/
822void
823fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration)
824{
825 g_return_if_fail (FU_IS_PLUGIN (plugin));
826 g_return_if_fail (duration > 0);
827
828 /* check sanity */
829 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
830 g_warning ("duration of %ums is crazy, truncating to %ums",
831 duration,
832 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
833 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
834 }
835
836 /* emit */
837 g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
838}
839
Richard Hughesd0905142016-03-13 09:46:49 +0000840gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000841fu_plugin_runner_startup (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000842{
Richard Hughescff38bc2016-12-12 12:03:37 +0000843 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000844 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000845
846 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000847 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000848 return TRUE;
849
Richard Hughes639da472018-01-06 22:35:04 +0000850 /* no object loaded */
851 if (priv->module == NULL)
852 return TRUE;
853
Richard Hughesd0905142016-03-13 09:46:49 +0000854 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000855 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
856 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000857 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000858 g_debug ("performing startup() on %s", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +0000859 if (!func (plugin, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000860 g_prefix_error (error, "failed to startup %s: ", priv->name);
861 return FALSE;
862 }
863 return TRUE;
864}
865
866static gboolean
867fu_plugin_runner_offline_invalidate (GError **error)
868{
869 g_autoptr(GError) error_local = NULL;
870 g_autoptr(GFile) file1 = NULL;
871
872 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
873
874 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
875 if (!g_file_query_exists (file1, NULL))
876 return TRUE;
877 if (!g_file_delete (file1, NULL, &error_local)) {
878 g_set_error (error,
879 FWUPD_ERROR,
880 FWUPD_ERROR_INTERNAL,
881 "Cannot delete %s: %s",
882 FU_OFFLINE_TRIGGER_FILENAME,
883 error_local->message);
884 return FALSE;
885 }
886 return TRUE;
887}
888
889static gboolean
890fu_plugin_runner_offline_setup (GError **error)
891{
892 gint rc;
893
894 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
895
896 /* create symlink for the systemd-system-update-generator */
897 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
898 if (rc < 0) {
899 g_set_error (error,
900 FWUPD_ERROR,
901 FWUPD_ERROR_INTERNAL,
902 "Failed to create symlink %s to %s: %s",
903 FU_OFFLINE_TRIGGER_FILENAME,
904 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000905 return FALSE;
906 }
907 return TRUE;
908}
909
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000910static gboolean
911fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device,
912 const gchar *symbol_name, GError **error)
913{
914 FuPluginPrivate *priv = GET_PRIVATE (plugin);
915 FuPluginDeviceFunc func = NULL;
916
917 /* not enabled */
918 if (!priv->enabled)
919 return TRUE;
920
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000921 /* no object loaded */
922 if (priv->module == NULL)
923 return TRUE;
924
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000925 /* optional */
926 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
927 if (func == NULL)
928 return TRUE;
929 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
930 if (!func (plugin, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000931 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000932 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000933 priv->name);
934 return FALSE;
935 }
936 return TRUE;
937}
938
Richard Hughesdbd8c762018-06-15 20:31:40 +0100939static gboolean
940fu_plugin_runner_device_array_generic (FuPlugin *plugin, GPtrArray *devices,
941 const gchar *symbol_name, GError **error)
942{
943 FuPluginPrivate *priv = GET_PRIVATE (plugin);
944 FuPluginDeviceArrayFunc func = NULL;
945
946 /* not enabled */
947 if (!priv->enabled)
948 return TRUE;
949
950 /* no object loaded */
951 if (priv->module == NULL)
952 return TRUE;
953
954 /* optional */
955 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
956 if (func == NULL)
957 return TRUE;
958 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
959 if (!func (plugin, devices, error)) {
960 g_prefix_error (error, "failed to run %s() on %s: ",
961 symbol_name + 10,
962 priv->name);
963 return FALSE;
964 }
965 return TRUE;
966}
967
Richard Hughesd0905142016-03-13 09:46:49 +0000968gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000969fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000970{
Richard Hughescff38bc2016-12-12 12:03:37 +0000971 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000972 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000973
974 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000975 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000976 return TRUE;
977
Richard Hughes639da472018-01-06 22:35:04 +0000978 /* no object loaded */
979 if (priv->module == NULL)
980 return TRUE;
981
Richard Hughesd0905142016-03-13 09:46:49 +0000982 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000983 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
984 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000985 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000986 g_debug ("performing coldplug() on %s", priv->name);
987 if (!func (plugin, error)) {
988 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
989 return FALSE;
990 }
991 return TRUE;
992}
993
Richard Hughes7b8b2022016-12-12 16:15:03 +0000994gboolean
Richard Hughes2de8f132018-01-17 09:12:02 +0000995fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error)
996{
997 FuPluginPrivate *priv = GET_PRIVATE (plugin);
998 FuPluginStartupFunc func = NULL;
999
1000 /* not enabled */
1001 if (!priv->enabled)
1002 return TRUE;
1003
1004 /* no object loaded */
1005 if (priv->module == NULL)
1006 return TRUE;
1007
1008 /* optional */
1009 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1010 if (func == NULL)
1011 return TRUE;
1012 g_debug ("performing recoldplug() on %s", priv->name);
1013 if (!func (plugin, error)) {
1014 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
1015 return FALSE;
1016 }
1017 return TRUE;
1018}
1019
1020gboolean
Richard Hughes46487c92017-01-07 21:26:34 +00001021fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error)
1022{
1023 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1024 FuPluginStartupFunc func = NULL;
1025
1026 /* not enabled */
1027 if (!priv->enabled)
1028 return TRUE;
1029
Richard Hughes639da472018-01-06 22:35:04 +00001030 /* no object loaded */
1031 if (priv->module == NULL)
1032 return TRUE;
1033
Richard Hughes46487c92017-01-07 21:26:34 +00001034 /* optional */
1035 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1036 if (func == NULL)
1037 return TRUE;
1038 g_debug ("performing coldplug_prepare() on %s", priv->name);
1039 if (!func (plugin, error)) {
1040 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
1041 return FALSE;
1042 }
1043 return TRUE;
1044}
1045
1046gboolean
1047fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error)
1048{
1049 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1050 FuPluginStartupFunc func = NULL;
1051
1052 /* not enabled */
1053 if (!priv->enabled)
1054 return TRUE;
1055
Richard Hughes639da472018-01-06 22:35:04 +00001056 /* no object loaded */
1057 if (priv->module == NULL)
1058 return TRUE;
1059
Richard Hughes46487c92017-01-07 21:26:34 +00001060 /* optional */
1061 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1062 if (func == NULL)
1063 return TRUE;
1064 g_debug ("performing coldplug_cleanup() on %s", priv->name);
1065 if (!func (plugin, error)) {
1066 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
1067 return FALSE;
1068 }
1069 return TRUE;
1070}
1071
1072gboolean
Richard Hughesdbd8c762018-06-15 20:31:40 +01001073fu_plugin_runner_composite_prepare (FuPlugin *plugin, GPtrArray *devices, GError **error)
1074{
1075 return fu_plugin_runner_device_array_generic (plugin, devices,
1076 "fu_plugin_composite_prepare",
1077 error);
1078}
1079
1080gboolean
1081fu_plugin_runner_composite_cleanup (FuPlugin *plugin, GPtrArray *devices, GError **error)
1082{
1083 return fu_plugin_runner_device_array_generic (plugin, devices,
1084 "fu_plugin_composite_cleanup",
1085 error);
1086}
1087
1088gboolean
Richard Hughes7b8b2022016-12-12 16:15:03 +00001089fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error)
1090{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001091 return fu_plugin_runner_device_generic (plugin, device,
1092 "fu_plugin_update_prepare", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001093}
1094
1095gboolean
1096fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error)
1097{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001098 return fu_plugin_runner_device_generic (plugin, device,
1099 "fu_plugin_update_cleanup", error);
1100}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001101
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001102gboolean
1103fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error)
1104{
1105 return fu_plugin_runner_device_generic (plugin, device,
1106 "fu_plugin_update_attach", error);
1107}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001108
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001109gboolean
1110fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error)
1111{
1112 return fu_plugin_runner_device_generic (plugin, device,
1113 "fu_plugin_update_detach", error);
1114}
1115
1116gboolean
1117fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error)
1118{
1119 return fu_plugin_runner_device_generic (plugin, device,
1120 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001121}
1122
Richard Hughes104f6512017-11-24 11:44:57 +00001123gboolean
1124fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error)
1125{
1126 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1127 FuPluginUsbDeviceAddedFunc func = NULL;
1128
1129 /* not enabled */
1130 if (!priv->enabled)
1131 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001132
1133 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001134 if (priv->module == NULL)
1135 return TRUE;
1136
1137 /* optional */
1138 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1139 if (func != NULL) {
1140 g_debug ("performing usb_device_added() on %s", priv->name);
1141 return func (plugin, usb_device, error);
1142 }
1143 return TRUE;
1144}
1145
Richard Hughese1fd34d2017-08-24 14:19:51 +01001146void
1147fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device)
1148{
1149 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1150 FuPluginDeviceRegisterFunc func = NULL;
1151
1152 /* not enabled */
1153 if (!priv->enabled)
1154 return;
Richard Hughes34834102017-11-21 21:55:00 +00001155 if (priv->module == NULL)
1156 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001157
1158 /* optional */
1159 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1160 if (func != NULL) {
1161 g_debug ("performing device_added() on %s", priv->name);
1162 func (plugin, device);
1163 }
1164}
1165
Richard Hughescff38bc2016-12-12 12:03:37 +00001166static gboolean
1167fu_plugin_runner_schedule_update (FuPlugin *plugin,
1168 FuDevice *device,
1169 GBytes *blob_cab,
1170 GError **error)
1171{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001172 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001173 gchar tmpname[] = {"XXXXXX.cap"};
1174 g_autofree gchar *dirname = NULL;
1175 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001176 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001177 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001178 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001179 g_autoptr(GFile) file = NULL;
1180
1181 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001182 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001183 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001184 if (res_tmp != NULL) {
1185 g_set_error (error,
1186 FWUPD_ERROR,
1187 FWUPD_ERROR_ALREADY_PENDING,
1188 "%s is already scheduled to be updated",
1189 fu_device_get_id (device));
1190 return FALSE;
1191 }
1192
1193 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001194 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001195 file = g_file_new_for_path (dirname);
1196 if (!g_file_query_exists (file, NULL)) {
1197 if (!g_file_make_directory_with_parents (file, NULL, error))
1198 return FALSE;
1199 }
1200
1201 /* get a random filename */
1202 for (guint i = 0; i < 6; i++)
1203 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1204 filename = g_build_filename (dirname, tmpname, NULL);
1205
1206 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001207 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001208 if (!g_file_set_contents (filename,
1209 g_bytes_get_data (blob_cab, NULL),
1210 (gssize) g_bytes_get_size (blob_cab),
1211 error))
1212 return FALSE;
1213
1214 /* schedule for next boot */
1215 g_debug ("schedule %s to be installed to %s on next boot",
1216 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001217 release = fu_device_get_release_default (device);
1218 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1219 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001220
1221 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001222 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001223 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001224 return FALSE;
1225
1226 /* next boot we run offline */
1227 return fu_plugin_runner_offline_setup (error);
1228}
1229
1230gboolean
1231fu_plugin_runner_verify (FuPlugin *plugin,
1232 FuDevice *device,
1233 FuPluginVerifyFlags flags,
1234 GError **error)
1235{
1236 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001237 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001238 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001239
1240 /* not enabled */
1241 if (!priv->enabled)
1242 return TRUE;
1243
Richard Hughes639da472018-01-06 22:35:04 +00001244 /* no object loaded */
1245 if (priv->module == NULL)
1246 return TRUE;
1247
Richard Hughesababbb72017-06-15 20:18:36 +01001248 /* clear any existing verification checksums */
1249 checksums = fu_device_get_checksums (device);
1250 g_ptr_array_set_size (checksums, 0);
1251
Richard Hughescff38bc2016-12-12 12:03:37 +00001252 /* optional */
1253 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1254 if (func == NULL)
1255 return TRUE;
1256 g_debug ("performing verify() on %s", priv->name);
1257 if (!func (plugin, device, flags, error)) {
1258 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001259 return FALSE;
1260 }
1261 return TRUE;
1262}
1263
Richard Hughesd0905142016-03-13 09:46:49 +00001264gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001265fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001266{
Richard Hughescff38bc2016-12-12 12:03:37 +00001267 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001268
1269 /* final check */
1270 flags = fu_device_get_flags (device);
1271 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1272 g_set_error (error,
1273 FWUPD_ERROR,
1274 FWUPD_ERROR_NOT_SUPPORTED,
1275 "Device %s is not locked",
1276 fu_device_get_id (device));
1277 return FALSE;
1278 }
1279
Richard Hughes9c4b5312017-11-14 11:34:53 +00001280 /* run vfunc */
1281 if (!fu_plugin_runner_device_generic (plugin, device,
1282 "fu_plugin_unlock", error))
1283 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001284
1285 /* update with correct flags */
1286 flags = fu_device_get_flags (device);
1287 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1288 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1289 return TRUE;
1290}
1291
1292gboolean
1293fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001294 FuDevice *device,
1295 GBytes *blob_cab,
1296 GBytes *blob_fw,
1297 FwupdInstallFlags flags,
1298 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001299{
1300 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001301 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001302 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001303 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001304 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001305 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001306
1307 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001308 if (!priv->enabled) {
1309 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001310 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001311 }
Richard Hughesd0905142016-03-13 09:46:49 +00001312
Richard Hughes639da472018-01-06 22:35:04 +00001313 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001314 if (priv->module == NULL) {
1315 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001316 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001317 }
Richard Hughes639da472018-01-06 22:35:04 +00001318
Richard Hughesd0905142016-03-13 09:46:49 +00001319 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001320 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1321 if (update_func == NULL) {
1322 g_set_error_literal (error,
1323 FWUPD_ERROR,
1324 FWUPD_ERROR_NOT_SUPPORTED,
1325 "No update possible");
1326 return FALSE;
1327 }
Richard Hughesd0905142016-03-13 09:46:49 +00001328
Richard Hughesa785a1c2017-08-25 16:00:58 +01001329 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001330 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001331 if (blob_cab == NULL) {
1332 g_set_error_literal (error,
1333 FWUPD_ERROR,
1334 FWUPD_ERROR_NOT_SUPPORTED,
1335 "No cabinet archive to schedule");
1336 return FALSE;
1337 }
Richard Hughesa785a1c2017-08-25 16:00:58 +01001338 return fu_plugin_runner_schedule_update (plugin,
1339 device,
1340 blob_cab,
1341 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001342 }
1343
1344 /* cancel the pending action */
1345 if (!fu_plugin_runner_offline_invalidate (error))
1346 return FALSE;
1347
1348 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001349 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001350 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001351 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughesc0cd0232018-01-31 15:02:00 +00001352 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001353 g_propagate_error (error, error_update);
1354 return FALSE;
1355 }
1356
Richard Hughesf556d372017-06-15 19:49:18 +01001357 /* no longer valid */
1358 checksums = fu_device_get_checksums (device);
1359 g_ptr_array_set_size (checksums, 0);
1360
Richard Hughescff38bc2016-12-12 12:03:37 +00001361 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001362 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001363 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001364 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001365
Richard Hughes780ef3f2018-01-12 16:20:31 +00001366 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001367 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1368 if (!fu_history_modify_device (history, device,
1369 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1370 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001371 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001372
1373 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001374 release = fu_device_get_release_default (device_pending);
1375 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001376 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1377 g_autoptr(GError) error_local = NULL;
1378 g_autoptr(GFile) file = NULL;
1379 file = g_file_new_for_path (tmp);
1380 if (!g_file_delete (file, NULL, &error_local)) {
1381 g_set_error (error,
1382 FWUPD_ERROR,
1383 FWUPD_ERROR_INVALID_FILE,
1384 "Failed to delete %s: %s",
1385 tmp, error_local->message);
1386 return FALSE;
1387 }
1388 }
1389 }
Richard Hughesd0905142016-03-13 09:46:49 +00001390 return TRUE;
1391}
Richard Hughescff38bc2016-12-12 12:03:37 +00001392
1393gboolean
1394fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error)
1395{
1396 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001397 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001398
1399 /* not enabled */
1400 if (!priv->enabled)
1401 return TRUE;
1402
Richard Hughes639da472018-01-06 22:35:04 +00001403 /* no object loaded */
1404 if (priv->module == NULL)
1405 return TRUE;
1406
Richard Hughes65e44ca2018-01-30 17:26:30 +00001407 /* optional */
1408 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1409 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001410 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001411 g_debug ("performing clear_result() on %s", priv->name);
1412 if (!func (plugin, device, error)) {
1413 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001414 return FALSE;
1415 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001416 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001417}
1418
1419gboolean
1420fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1421{
1422 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001423 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001424
1425 /* not enabled */
1426 if (!priv->enabled)
1427 return TRUE;
1428
Richard Hughes639da472018-01-06 22:35:04 +00001429 /* no object loaded */
1430 if (priv->module == NULL)
1431 return TRUE;
1432
Richard Hughes65e44ca2018-01-30 17:26:30 +00001433 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001434 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001435 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001436 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001437 g_debug ("performing get_results() on %s", priv->name);
1438 if (!func (plugin, device, error)) {
1439 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001440 return FALSE;
1441 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001442 return TRUE;
1443}
1444
Richard Hughes08a37992017-09-12 12:57:43 +01001445/**
1446 * fu_plugin_get_order:
1447 * @plugin: a #FuPlugin
1448 *
1449 * Gets the plugin order, where higher numbers are run after lower
1450 * numbers.
1451 *
1452 * Returns: the integer value
1453 **/
1454guint
1455fu_plugin_get_order (FuPlugin *plugin)
1456{
1457 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1458 return priv->order;
1459}
1460
1461/**
1462 * fu_plugin_set_order:
1463 * @plugin: a #FuPlugin
1464 * @order: a integer value
1465 *
1466 * Sets the plugin order, where higher numbers are run after lower
1467 * numbers.
1468 **/
1469void
1470fu_plugin_set_order (FuPlugin *plugin, guint order)
1471{
1472 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1473 priv->order = order;
1474}
1475
1476/**
1477 * fu_plugin_add_rule:
1478 * @plugin: a #FuPlugin
1479 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001480 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001481 *
1482 * If the plugin name is found, the rule will be used to sort the plugin list,
1483 * for example the plugin specified by @name will be ordered after this plugin
1484 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1485 *
1486 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1487 * If depsolving fails then fwupd will not start.
1488 **/
1489void
1490fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1491{
1492 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1493 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1494}
1495
1496/**
1497 * fu_plugin_get_rules:
1498 * @plugin: a #FuPlugin
1499 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1500 *
1501 * Gets the plugin IDs that should be run after this plugin.
1502 *
1503 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1504 **/
1505GPtrArray *
1506fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1507{
1508 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1509 return priv->rules[rule];
1510}
1511
Richard Hughes80b79bb2018-01-11 21:11:06 +00001512/**
1513 * fu_plugin_add_report_metadata:
1514 * @plugin: a #FuPlugin
1515 * @key: a string, e.g. `FwupdateVersion`
1516 * @value: a string, e.g. `10`
1517 *
1518 * Sets any additional metadata to be included in the firmware report to aid
1519 * debugging problems.
1520 *
1521 * Any data included here will be sent to the metadata server after user
1522 * confirmation.
1523 **/
1524void
1525fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1526{
1527 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1528 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1529}
1530
1531/**
1532 * fu_plugin_get_report_metadata:
1533 * @plugin: a #FuPlugin
1534 *
1535 * Returns the list of additional metadata to be added when filing a report.
1536 *
1537 * Returns: (transfer none): the map of report metadata
1538 **/
1539GHashTable *
1540fu_plugin_get_report_metadata (FuPlugin *plugin)
1541{
1542 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1543 return priv->report_metadata;
1544}
1545
Mario Limonciello963dc422018-02-27 14:26:58 -06001546/**
1547 * fu_plugin_get_config_value:
1548 * @plugin: a #FuPlugin
1549 * @key: A settings key
1550 *
1551 * Return the value of a key if it's been configured
1552 *
1553 * Since: 1.0.6
1554 **/
1555gchar *
1556fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key)
1557{
Richard Hughes4be17d12018-05-30 20:36:29 +01001558 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06001559 g_autofree gchar *conf_file = NULL;
1560 g_autofree gchar *conf_path = NULL;
1561 g_autoptr(GKeyFile) keyfile = NULL;
1562 const gchar *plugin_name;
1563
Richard Hughes4be17d12018-05-30 20:36:29 +01001564 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Mario Limonciello963dc422018-02-27 14:26:58 -06001565 plugin_name = fu_plugin_get_name (plugin);
1566 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01001567 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06001568 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1569 return NULL;
1570 keyfile = g_key_file_new ();
1571 if (!g_key_file_load_from_file (keyfile, conf_path,
1572 G_KEY_FILE_NONE, NULL))
1573 return NULL;
1574 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1575}
1576
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001577/**
1578 * fu_plugin_name_compare:
1579 * @plugin1: first #FuPlugin to compare.
1580 * @plugin2: second #FuPlugin to compare.
1581 *
1582 * Compares two plugins by their names.
1583 *
1584 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1585 **/
1586gint
1587fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1588{
1589 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1590 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1591 return g_strcmp0 (priv1->name, priv2->name);
1592}
1593
1594/**
1595 * fu_plugin_order_compare:
1596 * @plugin1: first #FuPlugin to compare.
1597 * @plugin2: second #FuPlugin to compare.
1598 *
1599 * Compares two plugins by their depsolved order.
1600 *
1601 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1602 **/
1603gint
1604fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1605{
1606 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1607 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1608 if (priv1->order < priv2->order)
1609 return -1;
1610 if (priv1->order > priv2->order)
1611 return 1;
1612 return 0;
1613}
1614
Richard Hughescff38bc2016-12-12 12:03:37 +00001615static void
1616fu_plugin_class_init (FuPluginClass *klass)
1617{
1618 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1619 object_class->finalize = fu_plugin_finalize;
1620 signals[SIGNAL_DEVICE_ADDED] =
1621 g_signal_new ("device-added",
1622 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1623 G_STRUCT_OFFSET (FuPluginClass, device_added),
1624 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1625 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1626 signals[SIGNAL_DEVICE_REMOVED] =
1627 g_signal_new ("device-removed",
1628 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1629 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1630 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1631 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001632 signals[SIGNAL_DEVICE_REGISTER] =
1633 g_signal_new ("device-register",
1634 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1635 G_STRUCT_OFFSET (FuPluginClass, device_register),
1636 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1637 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001638 signals[SIGNAL_RECOLDPLUG] =
1639 g_signal_new ("recoldplug",
1640 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1641 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1642 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1643 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001644 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1645 g_signal_new ("set-coldplug-delay",
1646 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1647 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1648 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1649 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001650}
1651
1652static void
1653fu_plugin_init (FuPlugin *plugin)
1654{
1655 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1656 priv->enabled = TRUE;
1657 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1658 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001659 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001660 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 +01001661 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1662 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001663}
1664
1665static void
1666fu_plugin_finalize (GObject *object)
1667{
1668 FuPlugin *plugin = FU_PLUGIN (object);
1669 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1670 FuPluginInitFunc func = NULL;
1671
1672 /* optional */
1673 if (priv->module != NULL) {
1674 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1675 if (func != NULL) {
1676 g_debug ("performing destroy() on %s", priv->name);
1677 func (plugin);
1678 }
1679 }
1680
Richard Hughes08a37992017-09-12 12:57:43 +01001681 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1682 g_ptr_array_unref (priv->rules[i]);
1683
Richard Hughescff38bc2016-12-12 12:03:37 +00001684 if (priv->usb_ctx != NULL)
1685 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001686 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001687 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001688 if (priv->quirks != NULL)
1689 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001690 if (priv->supported_guids != NULL)
1691 g_ptr_array_unref (priv->supported_guids);
Richard Hughes49e5e052017-09-03 12:15:41 +01001692 if (priv->smbios != NULL)
1693 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001694 if (priv->runtime_versions != NULL)
1695 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001696 if (priv->compile_versions != NULL)
1697 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00001698 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001699 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001700 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001701 g_free (priv->name);
1702 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05001703 /* Must happen as the last step to avoid prematurely
1704 * freeing memory held by the plugin */
1705#ifndef RUNNING_ON_VALGRIND
1706 if (priv->module != NULL)
1707 g_module_close (priv->module);
1708#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001709
1710 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1711}
1712
1713FuPlugin *
1714fu_plugin_new (void)
1715{
1716 FuPlugin *plugin;
1717 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1718 return plugin;
1719}