blob: c99c861b3f7cc6ae81617b3dbb608d60e0137b88 [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;
Richard Hughes9d6e0e72018-08-24 20:20:17 +010050 GPtrArray *udev_subsystems;
Richard Hughes1354ea92017-09-19 15:58:31 +010051 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000052 GHashTable *devices; /* platform_id:GObject */
Richard Hughesae3d65f2016-12-16 09:38:01 +000053 GHashTable *devices_delay; /* FuDevice:FuPluginHelper */
Richard Hughes80b79bb2018-01-11 21:11:06 +000054 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000055 FuPluginData *data;
56} FuPluginPrivate;
57
58enum {
59 SIGNAL_DEVICE_ADDED,
60 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010061 SIGNAL_DEVICE_REGISTER,
Richard Hughes362d6d72017-01-07 21:42:14 +000062 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000063 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughescff38bc2016-12-12 12:03:37 +000064 SIGNAL_LAST
65};
66
67static guint signals[SIGNAL_LAST] = { 0 };
68
69G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
70#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
71
72typedef const gchar *(*FuPluginGetNameFunc) (void);
73typedef void (*FuPluginInitFunc) (FuPlugin *plugin);
74typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin,
75 GError **error);
Richard Hughese1fd34d2017-08-24 14:19:51 +010076typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin,
77 FuDevice *device);
Richard Hughescff38bc2016-12-12 12:03:37 +000078typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin,
79 FuDevice *device,
80 GError **error);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -050081typedef gboolean (*FuPluginFlaggedDeviceFunc) (FuPlugin *plugin,
82 FwupdInstallFlags flags,
83 FuDevice *device,
84 GError **error);
Richard Hughesdbd8c762018-06-15 20:31:40 +010085typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *plugin,
86 GPtrArray *devices,
87 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +000088typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin,
89 FuDevice *device,
90 FuPluginVerifyFlags flags,
91 GError **error);
92typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin,
93 FuDevice *device,
94 GBytes *blob_fw,
95 FwupdInstallFlags flags,
96 GError **error);
Richard Hughes104f6512017-11-24 11:44:57 +000097typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin,
98 GUsbDevice *usb_device,
99 GError **error);
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100100typedef gboolean (*FuPluginUdevDeviceAddedFunc) (FuPlugin *plugin,
101 GUdevDevice *udev_device,
102 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +0000103
Richard Hughes57d18222017-01-10 16:02:59 +0000104/**
105 * fu_plugin_get_name:
106 * @plugin: A #FuPlugin
107 *
108 * Gets the plugin name.
109 *
110 * Returns: a plugin name, or %NULL for unknown.
111 *
112 * Since: 0.8.0
113 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000114const gchar *
115fu_plugin_get_name (FuPlugin *plugin)
Richard Hughesd0905142016-03-13 09:46:49 +0000116{
Richard Hughescff38bc2016-12-12 12:03:37 +0000117 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000118 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000119 return priv->name;
120}
Richard Hughesd0905142016-03-13 09:46:49 +0000121
Richard Hughes34834102017-11-21 21:55:00 +0000122void
123fu_plugin_set_name (FuPlugin *plugin, const gchar *name)
124{
125 FuPluginPrivate *priv = GET_PRIVATE (plugin);
126 g_return_if_fail (FU_IS_PLUGIN (plugin));
127 g_return_if_fail (name != NULL);
128 g_free (priv->name);
129 priv->name = g_strdup (name);
130}
131
Richard Hughes57d18222017-01-10 16:02:59 +0000132/**
133 * fu_plugin_cache_lookup:
134 * @plugin: A #FuPlugin
135 * @id: the key
136 *
137 * Finds an object in the per-plugin cache.
138 *
139 * Returns: (transfer none): a #GObject, or %NULL for unfound.
140 *
141 * Since: 0.8.0
142 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000143gpointer
144fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id)
145{
146 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000147 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
148 g_return_val_if_fail (id != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000149 return g_hash_table_lookup (priv->devices, id);
150}
Richard Hughesd0905142016-03-13 09:46:49 +0000151
Richard Hughes57d18222017-01-10 16:02:59 +0000152/**
153 * fu_plugin_cache_add:
154 * @plugin: A #FuPlugin
155 * @id: the key
156 * @dev: a #GObject, typically a #FuDevice
157 *
158 * Adds an object to the per-plugin cache.
159 *
160 * Since: 0.8.0
161 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000162void
163fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev)
164{
165 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000166 g_return_if_fail (FU_IS_PLUGIN (plugin));
167 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000168 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
169}
170
Richard Hughes57d18222017-01-10 16:02:59 +0000171/**
172 * fu_plugin_cache_remove:
173 * @plugin: A #FuPlugin
174 * @id: the key
175 *
176 * Removes an object from the per-plugin cache.
177 *
178 * Since: 0.8.0
179 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000180void
181fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id)
182{
183 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000184 g_return_if_fail (FU_IS_PLUGIN (plugin));
185 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000186 g_hash_table_remove (priv->devices, id);
187}
188
Richard Hughes57d18222017-01-10 16:02:59 +0000189/**
190 * fu_plugin_get_data:
191 * @plugin: A #FuPlugin
192 *
Richard Hughes4eada342017-10-03 21:20:32 +0100193 * Gets the per-plugin allocated private data. This will return %NULL unless
194 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000195 *
Richard Hughes4eada342017-10-03 21:20:32 +0100196 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000197 *
198 * Since: 0.8.0
199 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000200FuPluginData *
201fu_plugin_get_data (FuPlugin *plugin)
202{
203 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000204 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000205 return priv->data;
206}
207
Richard Hughes57d18222017-01-10 16:02:59 +0000208/**
209 * fu_plugin_alloc_data:
210 * @plugin: A #FuPlugin
211 * @data_sz: the size to allocate
212 *
213 * Allocates the per-plugin allocated private data.
214 *
215 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
216 *
217 * Since: 0.8.0
218 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000219FuPluginData *
220fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz)
221{
222 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000223 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000224 if (priv->data != NULL) {
225 g_critical ("fu_plugin_alloc_data() already used by plugin");
226 return priv->data;
227 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000228 priv->data = g_malloc0 (data_sz);
229 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000230}
231
Richard Hughes57d18222017-01-10 16:02:59 +0000232/**
233 * fu_plugin_get_usb_context:
234 * @plugin: A #FuPlugin
235 *
236 * Gets the shared USB context that all plugins can use.
237 *
238 * Returns: (transfer none): a #GUsbContext.
239 *
240 * Since: 0.8.0
241 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000242GUsbContext *
243fu_plugin_get_usb_context (FuPlugin *plugin)
244{
Richard Hughescff38bc2016-12-12 12:03:37 +0000245 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000246 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000247 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000248}
249
250void
251fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx)
252{
Richard Hughescff38bc2016-12-12 12:03:37 +0000253 FuPluginPrivate *priv = GET_PRIVATE (plugin);
254 g_set_object (&priv->usb_ctx, usb_ctx);
255}
256
Richard Hughes57d18222017-01-10 16:02:59 +0000257/**
258 * fu_plugin_get_enabled:
259 * @plugin: A #FuPlugin
260 *
Richard Hughes4eada342017-10-03 21:20:32 +0100261 * Returns if the plugin is enabled. Plugins may self-disable using
262 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000263 *
264 * Returns: %TRUE if the plugin is currently enabled.
265 *
266 * Since: 0.8.0
267 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000268gboolean
269fu_plugin_get_enabled (FuPlugin *plugin)
270{
271 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000272 g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000273 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000274}
275
Richard Hughes57d18222017-01-10 16:02:59 +0000276/**
277 * fu_plugin_set_enabled:
278 * @plugin: A #FuPlugin
279 * @enabled: the enabled value
280 *
281 * Enables or disables a plugin. Plugins can self-disable at any point.
282 *
283 * Since: 0.8.0
284 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000285void
Richard Hughescff38bc2016-12-12 12:03:37 +0000286fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000287{
Richard Hughescff38bc2016-12-12 12:03:37 +0000288 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000289 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughescff38bc2016-12-12 12:03:37 +0000290 priv->enabled = enabled;
291}
292
Richard Hughes1e456bc2018-05-10 20:16:16 +0100293gchar *
294fu_plugin_guess_name_from_fn (const gchar *filename)
295{
296 const gchar *prefix = "libfu_plugin_";
297 gchar *name;
298 gchar *str = g_strstr_len (filename, -1, prefix);
299 if (str == NULL)
300 return NULL;
301 name = g_strdup (str + strlen (prefix));
302 g_strdelimit (name, ".", '\0');
303 return name;
304}
305
Richard Hughescff38bc2016-12-12 12:03:37 +0000306gboolean
307fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error)
308{
309 FuPluginPrivate *priv = GET_PRIVATE (plugin);
310 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000311
312 priv->module = g_module_open (filename, 0);
313 if (priv->module == NULL) {
314 g_set_error (error,
315 G_IO_ERROR,
316 G_IO_ERROR_FAILED,
317 "failed to open plugin: %s",
318 g_module_error ());
319 return FALSE;
320 }
321
322 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100323 if (priv->name == NULL)
324 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000325
326 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000327 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
328 if (func != NULL) {
329 g_debug ("performing init() on %s", filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000330 func (plugin);
331 }
332
Richard Hughescff38bc2016-12-12 12:03:37 +0000333 return TRUE;
334}
335
Richard Hughes57d18222017-01-10 16:02:59 +0000336/**
337 * fu_plugin_device_add:
338 * @plugin: A #FuPlugin
339 * @device: A #FuDevice
340 *
341 * Asks the daemon to add a device to the exported list. If this device ID
342 * has already been added by a different plugin then this request will be
343 * ignored.
344 *
345 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
346 * actually flashing an image to the hardware so that higher-priority plugins
347 * can add the device themselves.
348 *
349 * Since: 0.8.0
350 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000351void
352fu_plugin_device_add (FuPlugin *plugin, FuDevice *device)
353{
Richard Hughes5e447292018-04-27 14:25:54 +0100354 GPtrArray *children;
355
Richard Hughesccd78a92017-01-11 16:57:41 +0000356 g_return_if_fail (FU_IS_PLUGIN (plugin));
357 g_return_if_fail (FU_IS_DEVICE (device));
358
Richard Hughescff38bc2016-12-12 12:03:37 +0000359 g_debug ("emit added from %s: %s",
360 fu_plugin_get_name (plugin),
361 fu_device_get_id (device));
362 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
363 fu_device_set_plugin (device, fu_plugin_get_name (plugin));
364 g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100365
Richard Hughes128c0162018-08-10 11:00:29 +0100366 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100367 children = fu_device_get_children (device);
368 for (guint i = 0; i < children->len; i++) {
369 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100370 if (fu_device_get_created (child) == 0)
371 fu_plugin_device_add (plugin, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100372 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000373}
374
Richard Hughese1fd34d2017-08-24 14:19:51 +0100375/**
376 * fu_plugin_device_register:
377 * @plugin: A #FuPlugin
378 * @device: A #FuDevice
379 *
380 * Registers the device with other plugins so they can set metadata.
381 *
382 * Plugins do not have to call this manually as this is done automatically
383 * when using fu_plugin_device_add(). They may wish to use this manually
384 * if for intance the coldplug should be ignored based on the metadata
385 * set from other plugins.
386 *
387 * Since: 0.9.7
388 **/
389void
390fu_plugin_device_register (FuPlugin *plugin, FuDevice *device)
391{
392 g_return_if_fail (FU_IS_PLUGIN (plugin));
393 g_return_if_fail (FU_IS_DEVICE (device));
394
395 g_debug ("emit device-register from %s: %s",
396 fu_plugin_get_name (plugin),
397 fu_device_get_id (device));
398 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device);
399}
400
Richard Hughesae3d65f2016-12-16 09:38:01 +0000401typedef struct {
402 FuPlugin *plugin;
403 FuDevice *device;
404 guint timeout_id;
Richard Hughesd4184cf2016-12-21 16:05:17 +0000405 GHashTable *devices;
Richard Hughesae3d65f2016-12-16 09:38:01 +0000406} FuPluginHelper;
407
408static void
409fu_plugin_helper_free (FuPluginHelper *helper)
410{
411 g_object_unref (helper->plugin);
412 g_object_unref (helper->device);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000413 g_hash_table_unref (helper->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000414 g_free (helper);
415}
416
417static gboolean
418fu_plugin_device_add_delay_cb (gpointer user_data)
419{
420 FuPluginHelper *helper = (FuPluginHelper *) user_data;
421 fu_plugin_device_add (helper->plugin, helper->device);
Richard Hughes0dec2742017-09-18 11:07:39 +0100422 g_hash_table_remove (helper->devices, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000423 fu_plugin_helper_free (helper);
424 return FALSE;
425}
426
Richard Hughes57d18222017-01-10 16:02:59 +0000427/**
Richard Hughesf0a799e2017-01-17 20:13:30 +0000428 * fu_plugin_has_device_delay:
429 * @plugin: A #FuPlugin
430 *
431 * Returns if the device has a pending device that is waiting to be added.
432 *
433 * Returns: %TRUE if a device is waiting to be added
434 *
435 * Since: 0.8.0
436 **/
437gboolean
438fu_plugin_has_device_delay (FuPlugin *plugin)
439{
440 FuPluginPrivate *priv = GET_PRIVATE (plugin);
441 return g_hash_table_size (priv->devices_delay) > 0;
442}
443
444/**
Richard Hughes57d18222017-01-10 16:02:59 +0000445 * fu_plugin_device_add_delay:
446 * @plugin: A #FuPlugin
447 * @device: A #FuDevice
448 *
449 * Asks the daemon to add a device to the exported list after a small delay.
450 *
451 * Since: 0.8.0
452 **/
Richard Hughesae3d65f2016-12-16 09:38:01 +0000453void
454fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device)
455{
456 FuPluginPrivate *priv = GET_PRIVATE (plugin);
457 FuPluginHelper *helper;
Richard Hughesccd78a92017-01-11 16:57:41 +0000458
459 g_return_if_fail (FU_IS_PLUGIN (plugin));
460 g_return_if_fail (FU_IS_DEVICE (device));
461
Richard Hughes6ad951d2017-02-03 10:12:12 +0000462 /* already waiting for add */
463 helper = g_hash_table_lookup (priv->devices_delay, device);
464 if (helper != NULL) {
Richard Hughes3cca1c62017-07-21 13:38:20 +0100465 g_debug ("ignoring add-delay as device %s already pending",
466 fu_device_get_id (device));
Richard Hughes6ad951d2017-02-03 10:12:12 +0000467 return;
468 }
469
470 /* add after a small delay */
Richard Hughesae3d65f2016-12-16 09:38:01 +0000471 g_debug ("waiting a small time for other plugins");
472 helper = g_new0 (FuPluginHelper, 1);
473 helper->plugin = g_object_ref (plugin);
474 helper->device = g_object_ref (device);
475 helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000476 helper->devices = g_hash_table_ref (priv->devices_delay);
477 g_hash_table_insert (helper->devices, device, helper);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000478}
479
Richard Hughes57d18222017-01-10 16:02:59 +0000480/**
Richard Hughes4eada342017-10-03 21:20:32 +0100481 * fu_plugin_device_remove:
Richard Hughes57d18222017-01-10 16:02:59 +0000482 * @plugin: A #FuPlugin
483 * @device: A #FuDevice
484 *
485 * Asks the daemon to remove a device from the exported list.
486 *
487 * Since: 0.8.0
488 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000489void
490fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device)
491{
Richard Hughesae3d65f2016-12-16 09:38:01 +0000492 FuPluginPrivate *priv = GET_PRIVATE (plugin);
493 FuPluginHelper *helper;
494
Richard Hughesccd78a92017-01-11 16:57:41 +0000495 g_return_if_fail (FU_IS_PLUGIN (plugin));
496 g_return_if_fail (FU_IS_DEVICE (device));
497
Richard Hughesae3d65f2016-12-16 09:38:01 +0000498 /* waiting for add */
499 helper = g_hash_table_lookup (priv->devices_delay, device);
500 if (helper != NULL) {
501 g_debug ("ignoring remove from delayed addition");
502 g_source_remove (helper->timeout_id);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000503 g_hash_table_remove (priv->devices_delay, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000504 fu_plugin_helper_free (helper);
505 return;
506 }
507
Richard Hughescff38bc2016-12-12 12:03:37 +0000508 g_debug ("emit removed from %s: %s",
509 fu_plugin_get_name (plugin),
510 fu_device_get_id (device));
511 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device);
512}
513
Richard Hughes57d18222017-01-10 16:02:59 +0000514/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000515 * fu_plugin_request_recoldplug:
Richard Hughes362d6d72017-01-07 21:42:14 +0000516 * @plugin: A #FuPlugin
517 *
518 * Ask all the plugins to coldplug all devices, which will include the prepare()
519 * and cleanup() phases. Duplicate devices added will be ignored.
520 *
521 * Since: 0.8.0
522 **/
523void
Richard Hughes2de8f132018-01-17 09:12:02 +0000524fu_plugin_request_recoldplug (FuPlugin *plugin)
Richard Hughes362d6d72017-01-07 21:42:14 +0000525{
Richard Hughesccd78a92017-01-11 16:57:41 +0000526 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughes362d6d72017-01-07 21:42:14 +0000527 g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0);
528}
529
Richard Hughesb0829032017-01-10 09:27:08 +0000530/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100531 * fu_plugin_check_hwid:
532 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100533 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100534 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100535 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100536 * specific system can be shown using the `fwupdmgr hwids` command.
537 *
Richard Hughes4eada342017-10-03 21:20:32 +0100538 * Returns: %TRUE if the HwId is found on the system.
539 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100540 * Since: 0.9.1
541 **/
542gboolean
543fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid)
544{
545 FuPluginPrivate *priv = GET_PRIVATE (plugin);
546 if (priv->hwids == NULL)
547 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100548 return fu_hwids_has_guid (priv->hwids, hwid);
549}
550
551/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100552 * fu_plugin_get_hwids:
553 * @plugin: A #FuPlugin
554 *
555 * Returns all the HWIDs defined in the system. All hardware IDs on a
556 * specific system can be shown using the `fwupdmgr hwids` command.
557 *
558 * Returns: (transfer none) (element-type utf-8): An array of GUIDs
559 *
560 * Since: 1.1.1
561 **/
562GPtrArray *
563fu_plugin_get_hwids (FuPlugin *plugin)
564{
565 FuPluginPrivate *priv = GET_PRIVATE (plugin);
566 if (priv->hwids == NULL)
567 return NULL;
568 return fu_hwids_get_guids (priv->hwids);
569}
570
571/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100572 * fu_plugin_check_supported:
573 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100574 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100575 *
576 * Checks to see if a specific device GUID is supported, i.e. available in the
577 * AppStream metadata.
578 *
Richard Hughes4eada342017-10-03 21:20:32 +0100579 * Returns: %TRUE if the device is supported.
580 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100581 * Since: 1.0.0
582 **/
583gboolean
584fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid)
585{
586 FuPluginPrivate *priv = GET_PRIVATE (plugin);
587 if (priv->supported_guids == NULL)
588 return FALSE;
589 for (guint i = 0; i < priv->supported_guids->len; i++) {
590 const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i);
591 if (g_strcmp0 (guid, guid_tmp) == 0)
592 return TRUE;
593 }
594 return FALSE;
595}
596
597/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100598 * fu_plugin_get_dmi_value:
599 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100600 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100601 *
602 * Gets a hardware DMI value.
603 *
Richard Hughes4eada342017-10-03 21:20:32 +0100604 * Returns: The string, or %NULL
605 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100606 * Since: 0.9.7
607 **/
608const gchar *
609fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id)
610{
611 FuPluginPrivate *priv = GET_PRIVATE (plugin);
612 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100613 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100614 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100615}
616
Richard Hughes49e5e052017-09-03 12:15:41 +0100617/**
618 * fu_plugin_get_smbios_string:
619 * @plugin: A #FuPlugin
620 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
621 * @offset: A SMBIOS offset
622 *
623 * Gets a hardware SMBIOS string.
624 *
625 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
626 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
627 *
Richard Hughes4eada342017-10-03 21:20:32 +0100628 * Returns: A string, or %NULL
629 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100630 * Since: 0.9.8
631 **/
632const gchar *
633fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset)
634{
635 FuPluginPrivate *priv = GET_PRIVATE (plugin);
636 if (priv->smbios == NULL)
637 return NULL;
638 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
639}
640
641/**
642 * fu_plugin_get_smbios_data:
643 * @plugin: A #FuPlugin
644 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
645 *
646 * Gets a hardware SMBIOS data.
647 *
Richard Hughes4eada342017-10-03 21:20:32 +0100648 * Returns: (transfer none): A #GBytes, or %NULL
649 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100650 * Since: 0.9.8
651 **/
652GBytes *
653fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type)
654{
655 FuPluginPrivate *priv = GET_PRIVATE (plugin);
656 if (priv->smbios == NULL)
657 return NULL;
658 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
659}
660
Richard Hughesb8f8db22017-04-25 15:56:00 +0100661void
Richard Hughesd7704d42017-08-08 20:29:09 +0100662fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100663{
664 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesd7704d42017-08-08 20:29:09 +0100665 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100666}
667
Richard Hughes49e5e052017-09-03 12:15:41 +0100668void
Richard Hughes1354ea92017-09-19 15:58:31 +0100669fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids)
670{
671 FuPluginPrivate *priv = GET_PRIVATE (plugin);
672 if (priv->supported_guids != NULL)
673 g_ptr_array_unref (priv->supported_guids);
674 priv->supported_guids = g_ptr_array_ref (supported_guids);
675}
676
Richard Hughes9c028f02017-10-28 21:14:28 +0100677void
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100678fu_plugin_set_udev_subsystems (FuPlugin *plugin, GPtrArray *udev_subsystems)
679{
680 FuPluginPrivate *priv = GET_PRIVATE (plugin);
681 if (priv->udev_subsystems != NULL)
682 g_ptr_array_unref (priv->udev_subsystems);
683 priv->udev_subsystems = g_ptr_array_ref (udev_subsystems);
684}
685
686void
Richard Hughes9c028f02017-10-28 21:14:28 +0100687fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks)
688{
689 FuPluginPrivate *priv = GET_PRIVATE (plugin);
690 g_set_object (&priv->quirks, quirks);
691}
692
693/**
694 * fu_plugin_get_quirks:
695 * @plugin: A #FuPlugin
696 *
697 * Returns the hardware database object. This can be used to discover device
698 * quirks or other device-specific settings.
699 *
700 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
701 *
702 * Since: 1.0.1
703 **/
704FuQuirks *
705fu_plugin_get_quirks (FuPlugin *plugin)
706{
707 FuPluginPrivate *priv = GET_PRIVATE (plugin);
708 return priv->quirks;
709}
710
Richard Hughes0eb123b2018-04-19 12:00:04 +0100711void
712fu_plugin_set_runtime_versions (FuPlugin *plugin, GHashTable *runtime_versions)
713{
714 FuPluginPrivate *priv = GET_PRIVATE (plugin);
715 priv->runtime_versions = g_hash_table_ref (runtime_versions);
716}
717
718/**
719 * fu_plugin_add_runtime_version:
720 * @plugin: A #FuPlugin
721 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
722 * @version: A version string, e.g. "1.2.3"
723 *
724 * Sets a runtime version of a specific dependancy.
725 *
726 * Since: 1.0.7
727 **/
728void
729fu_plugin_add_runtime_version (FuPlugin *plugin,
730 const gchar *component_id,
731 const gchar *version)
732{
733 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesb01b4862018-04-20 16:39:48 +0100734 if (priv->runtime_versions == NULL)
735 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100736 g_hash_table_insert (priv->runtime_versions,
737 g_strdup (component_id),
738 g_strdup (version));
739}
740
Richard Hughes34e0dab2018-04-20 16:43:00 +0100741void
742fu_plugin_set_compile_versions (FuPlugin *plugin, GHashTable *compile_versions)
743{
744 FuPluginPrivate *priv = GET_PRIVATE (plugin);
745 priv->compile_versions = g_hash_table_ref (compile_versions);
746}
747
748/**
749 * fu_plugin_add_compile_version:
750 * @plugin: A #FuPlugin
751 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
752 * @version: A version string, e.g. "1.2.3"
753 *
754 * Sets a compile-time version of a specific dependancy.
755 *
756 * Since: 1.0.7
757 **/
758void
759fu_plugin_add_compile_version (FuPlugin *plugin,
760 const gchar *component_id,
761 const gchar *version)
762{
763 FuPluginPrivate *priv = GET_PRIVATE (plugin);
764 if (priv->compile_versions == NULL)
765 return;
766 g_hash_table_insert (priv->compile_versions,
767 g_strdup (component_id),
768 g_strdup (version));
769}
770
Richard Hughes9c028f02017-10-28 21:14:28 +0100771/**
772 * fu_plugin_lookup_quirk_by_id:
773 * @plugin: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100774 * @group: A string, e.g. "DfuFlags"
775 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100776 *
777 * Looks up an entry in the hardware database using a string value.
778 *
779 * Returns: (transfer none): values from the database, or %NULL if not found
780 *
781 * Since: 1.0.1
782 **/
783const gchar *
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100784fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100785{
786 FuPluginPrivate *priv = GET_PRIVATE (plugin);
787 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
788
Richard Hughes9c028f02017-10-28 21:14:28 +0100789 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100790 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100791}
792
793/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100794 * fu_plugin_lookup_quirk_by_id_as_uint64:
795 * @plugin: A #FuPlugin
796 * @group: A string, e.g. "DfuFlags"
797 * @key: An ID to match the entry, e.g. "Size"
798 *
799 * Looks up an entry in the hardware database using a string key, returning
800 * an integer value. Values are assumed base 10, unless prefixed with "0x"
801 * where they are parsed as base 16.
802 *
803 * Returns: (transfer none): value from the database, or 0 if not found
804 *
805 * Since: 1.1.2
806 **/
807guint64
808fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *plugin, const gchar *group, const gchar *key)
809{
Richard Hughes73bf2332018-08-28 09:38:09 +0100810 return fu_common_strtoull (fu_plugin_lookup_quirk_by_id (plugin, group, key));
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100811}
812
813/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100814 * fu_plugin_get_supported:
815 * @plugin: A #FuPlugin
816 *
817 * Gets all the device GUIDs supported by the daemon.
818 *
Richard Hughes4eada342017-10-03 21:20:32 +0100819 * Returns: (element-type utf8) (transfer none): GUIDs
820 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100821 * Since: 1.0.0
822 **/
823GPtrArray *
824fu_plugin_get_supported (FuPlugin *plugin)
825{
826 FuPluginPrivate *priv = GET_PRIVATE (plugin);
827 return priv->supported_guids;
828}
829
830void
Richard Hughes49e5e052017-09-03 12:15:41 +0100831fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios)
832{
833 FuPluginPrivate *priv = GET_PRIVATE (plugin);
834 g_set_object (&priv->smbios, smbios);
835}
836
Richard Hughesb8f8db22017-04-25 15:56:00 +0100837/**
Richard Hughesb0829032017-01-10 09:27:08 +0000838 * fu_plugin_set_coldplug_delay:
839 * @plugin: A #FuPlugin
840 * @duration: A delay in milliseconds
841 *
842 * Set the minimum time that should be waited inbetween the call to
843 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
844 * to be the minimum hardware initialisation time from a datasheet.
845 *
846 * It is better to use this function rather than using a sleep() in the plugin
847 * itself as then only one delay is done in the daemon rather than waiting for
848 * each coldplug prepare in a serial way.
849 *
850 * Additionally, very long delays should be avoided as the daemon will be
851 * blocked from processing requests whilst the coldplug delay is being
852 * performed.
853 *
854 * Since: 0.8.0
855 **/
856void
857fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration)
858{
859 g_return_if_fail (FU_IS_PLUGIN (plugin));
860 g_return_if_fail (duration > 0);
861
862 /* check sanity */
863 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
864 g_warning ("duration of %ums is crazy, truncating to %ums",
865 duration,
866 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
867 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
868 }
869
870 /* emit */
871 g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
872}
873
Richard Hughesd0905142016-03-13 09:46:49 +0000874gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000875fu_plugin_runner_startup (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000876{
Richard Hughescff38bc2016-12-12 12:03:37 +0000877 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000878 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000879
880 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000881 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000882 return TRUE;
883
Richard Hughes639da472018-01-06 22:35:04 +0000884 /* no object loaded */
885 if (priv->module == NULL)
886 return TRUE;
887
Richard Hughesd0905142016-03-13 09:46:49 +0000888 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000889 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
890 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000891 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000892 g_debug ("performing startup() on %s", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +0000893 if (!func (plugin, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000894 g_prefix_error (error, "failed to startup %s: ", priv->name);
895 return FALSE;
896 }
897 return TRUE;
898}
899
900static gboolean
901fu_plugin_runner_offline_invalidate (GError **error)
902{
903 g_autoptr(GError) error_local = NULL;
904 g_autoptr(GFile) file1 = NULL;
905
906 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
907
908 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
909 if (!g_file_query_exists (file1, NULL))
910 return TRUE;
911 if (!g_file_delete (file1, NULL, &error_local)) {
912 g_set_error (error,
913 FWUPD_ERROR,
914 FWUPD_ERROR_INTERNAL,
915 "Cannot delete %s: %s",
916 FU_OFFLINE_TRIGGER_FILENAME,
917 error_local->message);
918 return FALSE;
919 }
920 return TRUE;
921}
922
923static gboolean
924fu_plugin_runner_offline_setup (GError **error)
925{
926 gint rc;
927
928 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
929
930 /* create symlink for the systemd-system-update-generator */
931 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
932 if (rc < 0) {
933 g_set_error (error,
934 FWUPD_ERROR,
935 FWUPD_ERROR_INTERNAL,
936 "Failed to create symlink %s to %s: %s",
937 FU_OFFLINE_TRIGGER_FILENAME,
938 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000939 return FALSE;
940 }
941 return TRUE;
942}
943
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000944static gboolean
945fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device,
946 const gchar *symbol_name, GError **error)
947{
948 FuPluginPrivate *priv = GET_PRIVATE (plugin);
949 FuPluginDeviceFunc func = NULL;
950
951 /* not enabled */
952 if (!priv->enabled)
953 return TRUE;
954
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000955 /* no object loaded */
956 if (priv->module == NULL)
957 return TRUE;
958
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000959 /* optional */
960 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
961 if (func == NULL)
962 return TRUE;
963 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
964 if (!func (plugin, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000965 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000966 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000967 priv->name);
968 return FALSE;
969 }
970 return TRUE;
971}
972
Richard Hughesdbd8c762018-06-15 20:31:40 +0100973static gboolean
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500974fu_plugin_runner_flagged_device_generic (FuPlugin *plugin, FwupdInstallFlags flags,
975 FuDevice *device,
976 const gchar *symbol_name, GError **error)
977{
978 FuPluginPrivate *priv = GET_PRIVATE (plugin);
979 FuPluginFlaggedDeviceFunc func = NULL;
980
981 /* not enabled */
982 if (!priv->enabled)
983 return TRUE;
984
985 /* no object loaded */
986 if (priv->module == NULL)
987 return TRUE;
988
989 /* optional */
990 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
991 if (func == NULL)
992 return TRUE;
993 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
994 if (!func (plugin, flags, device, error)) {
995 g_prefix_error (error, "failed to run %s() on %s: ",
996 symbol_name + 10,
997 priv->name);
998 return FALSE;
999 }
1000 return TRUE;
1001
1002}
1003
1004static gboolean
Richard Hughesdbd8c762018-06-15 20:31:40 +01001005fu_plugin_runner_device_array_generic (FuPlugin *plugin, GPtrArray *devices,
1006 const gchar *symbol_name, GError **error)
1007{
1008 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1009 FuPluginDeviceArrayFunc func = NULL;
1010
1011 /* not enabled */
1012 if (!priv->enabled)
1013 return TRUE;
1014
1015 /* no object loaded */
1016 if (priv->module == NULL)
1017 return TRUE;
1018
1019 /* optional */
1020 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1021 if (func == NULL)
1022 return TRUE;
1023 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
1024 if (!func (plugin, devices, error)) {
1025 g_prefix_error (error, "failed to run %s() on %s: ",
1026 symbol_name + 10,
1027 priv->name);
1028 return FALSE;
1029 }
1030 return TRUE;
1031}
1032
Richard Hughesd0905142016-03-13 09:46:49 +00001033gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001034fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001035{
Richard Hughescff38bc2016-12-12 12:03:37 +00001036 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001037 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +00001038
1039 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +00001040 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +00001041 return TRUE;
1042
Richard Hughes639da472018-01-06 22:35:04 +00001043 /* no object loaded */
1044 if (priv->module == NULL)
1045 return TRUE;
1046
Richard Hughesd0905142016-03-13 09:46:49 +00001047 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001048 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
1049 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001050 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001051 g_debug ("performing coldplug() on %s", priv->name);
1052 if (!func (plugin, error)) {
1053 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
1054 return FALSE;
1055 }
1056 return TRUE;
1057}
1058
Richard Hughes7b8b2022016-12-12 16:15:03 +00001059gboolean
Richard Hughes2de8f132018-01-17 09:12:02 +00001060fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error)
1061{
1062 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1063 FuPluginStartupFunc func = NULL;
1064
1065 /* not enabled */
1066 if (!priv->enabled)
1067 return TRUE;
1068
1069 /* no object loaded */
1070 if (priv->module == NULL)
1071 return TRUE;
1072
1073 /* optional */
1074 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1075 if (func == NULL)
1076 return TRUE;
1077 g_debug ("performing recoldplug() on %s", priv->name);
1078 if (!func (plugin, error)) {
1079 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
1080 return FALSE;
1081 }
1082 return TRUE;
1083}
1084
1085gboolean
Richard Hughes46487c92017-01-07 21:26:34 +00001086fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error)
1087{
1088 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1089 FuPluginStartupFunc func = NULL;
1090
1091 /* not enabled */
1092 if (!priv->enabled)
1093 return TRUE;
1094
Richard Hughes639da472018-01-06 22:35:04 +00001095 /* no object loaded */
1096 if (priv->module == NULL)
1097 return TRUE;
1098
Richard Hughes46487c92017-01-07 21:26:34 +00001099 /* optional */
1100 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1101 if (func == NULL)
1102 return TRUE;
1103 g_debug ("performing coldplug_prepare() on %s", priv->name);
1104 if (!func (plugin, error)) {
1105 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
1106 return FALSE;
1107 }
1108 return TRUE;
1109}
1110
1111gboolean
1112fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error)
1113{
1114 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1115 FuPluginStartupFunc func = NULL;
1116
1117 /* not enabled */
1118 if (!priv->enabled)
1119 return TRUE;
1120
Richard Hughes639da472018-01-06 22:35:04 +00001121 /* no object loaded */
1122 if (priv->module == NULL)
1123 return TRUE;
1124
Richard Hughes46487c92017-01-07 21:26:34 +00001125 /* optional */
1126 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1127 if (func == NULL)
1128 return TRUE;
1129 g_debug ("performing coldplug_cleanup() on %s", priv->name);
1130 if (!func (plugin, error)) {
1131 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
1132 return FALSE;
1133 }
1134 return TRUE;
1135}
1136
1137gboolean
Richard Hughesdbd8c762018-06-15 20:31:40 +01001138fu_plugin_runner_composite_prepare (FuPlugin *plugin, GPtrArray *devices, GError **error)
1139{
1140 return fu_plugin_runner_device_array_generic (plugin, devices,
1141 "fu_plugin_composite_prepare",
1142 error);
1143}
1144
1145gboolean
1146fu_plugin_runner_composite_cleanup (FuPlugin *plugin, GPtrArray *devices, GError **error)
1147{
1148 return fu_plugin_runner_device_array_generic (plugin, devices,
1149 "fu_plugin_composite_cleanup",
1150 error);
1151}
1152
1153gboolean
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001154fu_plugin_runner_update_prepare (FuPlugin *plugin, FwupdInstallFlags flags, FuDevice *device,
1155 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001156{
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001157 return fu_plugin_runner_flagged_device_generic (plugin, flags, device,
1158 "fu_plugin_update_prepare",
1159 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001160}
1161
1162gboolean
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001163fu_plugin_runner_update_cleanup (FuPlugin *plugin, FwupdInstallFlags flags, FuDevice *device,
1164 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001165{
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001166 return fu_plugin_runner_flagged_device_generic (plugin, flags, device,
1167 "fu_plugin_update_cleanup",
1168 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001169}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001170
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001171gboolean
1172fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error)
1173{
1174 return fu_plugin_runner_device_generic (plugin, device,
1175 "fu_plugin_update_attach", error);
1176}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001177
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001178gboolean
1179fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error)
1180{
1181 return fu_plugin_runner_device_generic (plugin, device,
1182 "fu_plugin_update_detach", error);
1183}
1184
1185gboolean
1186fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error)
1187{
1188 return fu_plugin_runner_device_generic (plugin, device,
1189 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001190}
1191
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001192/**
1193 * fu_plugin_add_udev_subsystem:
1194 * @plugin: a #FuPlugin
1195 * @subsystem: a subsystem name, e.g. `pciport`
1196 *
1197 * Registers the udev subsystem to be watched by the daemon.
1198 *
1199 * Plugins can use this method only in fu_plugin_init()
1200 **/
1201void
1202fu_plugin_add_udev_subsystem (FuPlugin *plugin, const gchar *subsystem)
1203{
1204 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1205 for (guint i = 0; i < priv->udev_subsystems->len; i++) {
1206 const gchar *subsystem_tmp = g_ptr_array_index (priv->udev_subsystems, i);
1207 if (g_strcmp0 (subsystem_tmp, subsystem) == 0)
1208 return;
1209 }
1210 g_debug ("added udev subsystem watch of %s", subsystem);
1211 g_ptr_array_add (priv->udev_subsystems, g_strdup (subsystem));
1212}
1213
Richard Hughes104f6512017-11-24 11:44:57 +00001214gboolean
1215fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error)
1216{
1217 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1218 FuPluginUsbDeviceAddedFunc func = NULL;
1219
1220 /* not enabled */
1221 if (!priv->enabled)
1222 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001223
1224 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001225 if (priv->module == NULL)
1226 return TRUE;
1227
1228 /* optional */
1229 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1230 if (func != NULL) {
1231 g_debug ("performing usb_device_added() on %s", priv->name);
1232 return func (plugin, usb_device, error);
1233 }
1234 return TRUE;
1235}
1236
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001237gboolean
1238fu_plugin_runner_udev_device_added (FuPlugin *plugin, GUdevDevice *udev_device, GError **error)
1239{
1240 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1241 FuPluginUdevDeviceAddedFunc func = NULL;
1242
1243 /* not enabled */
1244 if (!priv->enabled)
1245 return TRUE;
1246
1247 /* no object loaded */
1248 if (priv->module == NULL)
1249 return TRUE;
1250
1251 /* optional */
1252 g_module_symbol (priv->module, "fu_plugin_udev_device_added", (gpointer *) &func);
1253 if (func != NULL) {
1254 g_debug ("performing udev_device_added() on %s", priv->name);
1255 return func (plugin, udev_device, error);
1256 }
1257 return TRUE;
1258}
1259
Richard Hughese1fd34d2017-08-24 14:19:51 +01001260void
1261fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device)
1262{
1263 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1264 FuPluginDeviceRegisterFunc func = NULL;
1265
1266 /* not enabled */
1267 if (!priv->enabled)
1268 return;
Richard Hughes34834102017-11-21 21:55:00 +00001269 if (priv->module == NULL)
1270 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001271
Mario Limonciello4910b242018-06-22 15:04:21 -05001272 /* don't notify plugins on their own devices */
1273 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (plugin)) == 0)
1274 return;
1275
Richard Hughese1fd34d2017-08-24 14:19:51 +01001276 /* optional */
1277 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1278 if (func != NULL) {
Richard Hughes1bf7ff92018-08-24 20:21:35 +01001279 g_debug ("performing fu_plugin_device_registered() on %s", priv->name);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001280 func (plugin, device);
1281 }
1282}
1283
Richard Hughescff38bc2016-12-12 12:03:37 +00001284static gboolean
1285fu_plugin_runner_schedule_update (FuPlugin *plugin,
1286 FuDevice *device,
1287 GBytes *blob_cab,
1288 GError **error)
1289{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001290 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001291 gchar tmpname[] = {"XXXXXX.cap"};
1292 g_autofree gchar *dirname = NULL;
1293 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001294 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001295 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001296 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001297 g_autoptr(GFile) file = NULL;
1298
1299 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001300 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001301 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001302 if (res_tmp != NULL) {
1303 g_set_error (error,
1304 FWUPD_ERROR,
1305 FWUPD_ERROR_ALREADY_PENDING,
1306 "%s is already scheduled to be updated",
1307 fu_device_get_id (device));
1308 return FALSE;
1309 }
1310
1311 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001312 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001313 file = g_file_new_for_path (dirname);
1314 if (!g_file_query_exists (file, NULL)) {
1315 if (!g_file_make_directory_with_parents (file, NULL, error))
1316 return FALSE;
1317 }
1318
1319 /* get a random filename */
1320 for (guint i = 0; i < 6; i++)
1321 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1322 filename = g_build_filename (dirname, tmpname, NULL);
1323
1324 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001325 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001326 if (!g_file_set_contents (filename,
1327 g_bytes_get_data (blob_cab, NULL),
1328 (gssize) g_bytes_get_size (blob_cab),
1329 error))
1330 return FALSE;
1331
1332 /* schedule for next boot */
1333 g_debug ("schedule %s to be installed to %s on next boot",
1334 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001335 release = fu_device_get_release_default (device);
1336 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1337 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001338
1339 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001340 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001341 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001342 return FALSE;
1343
1344 /* next boot we run offline */
1345 return fu_plugin_runner_offline_setup (error);
1346}
1347
1348gboolean
1349fu_plugin_runner_verify (FuPlugin *plugin,
1350 FuDevice *device,
1351 FuPluginVerifyFlags flags,
1352 GError **error)
1353{
1354 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001355 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001356 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001357
1358 /* not enabled */
1359 if (!priv->enabled)
1360 return TRUE;
1361
Richard Hughes639da472018-01-06 22:35:04 +00001362 /* no object loaded */
1363 if (priv->module == NULL)
1364 return TRUE;
1365
Richard Hughesababbb72017-06-15 20:18:36 +01001366 /* clear any existing verification checksums */
1367 checksums = fu_device_get_checksums (device);
1368 g_ptr_array_set_size (checksums, 0);
1369
Richard Hughescff38bc2016-12-12 12:03:37 +00001370 /* optional */
1371 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1372 if (func == NULL)
1373 return TRUE;
1374 g_debug ("performing verify() on %s", priv->name);
1375 if (!func (plugin, device, flags, error)) {
1376 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001377 return FALSE;
1378 }
1379 return TRUE;
1380}
1381
Richard Hughesd0905142016-03-13 09:46:49 +00001382gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001383fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001384{
Richard Hughescff38bc2016-12-12 12:03:37 +00001385 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001386
1387 /* final check */
1388 flags = fu_device_get_flags (device);
1389 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1390 g_set_error (error,
1391 FWUPD_ERROR,
1392 FWUPD_ERROR_NOT_SUPPORTED,
1393 "Device %s is not locked",
1394 fu_device_get_id (device));
1395 return FALSE;
1396 }
1397
Richard Hughes9c4b5312017-11-14 11:34:53 +00001398 /* run vfunc */
1399 if (!fu_plugin_runner_device_generic (plugin, device,
1400 "fu_plugin_unlock", error))
1401 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001402
1403 /* update with correct flags */
1404 flags = fu_device_get_flags (device);
1405 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1406 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1407 return TRUE;
1408}
1409
1410gboolean
1411fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001412 FuDevice *device,
1413 GBytes *blob_cab,
1414 GBytes *blob_fw,
1415 FwupdInstallFlags flags,
1416 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001417{
1418 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001419 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001420 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001421 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001422 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001423 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001424
1425 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001426 if (!priv->enabled) {
1427 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001428 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001429 }
Richard Hughesd0905142016-03-13 09:46:49 +00001430
Richard Hughes639da472018-01-06 22:35:04 +00001431 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001432 if (priv->module == NULL) {
1433 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001434 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001435 }
Richard Hughes639da472018-01-06 22:35:04 +00001436
Richard Hughesd0905142016-03-13 09:46:49 +00001437 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001438 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1439 if (update_func == NULL) {
1440 g_set_error_literal (error,
1441 FWUPD_ERROR,
1442 FWUPD_ERROR_NOT_SUPPORTED,
1443 "No update possible");
1444 return FALSE;
1445 }
Richard Hughesd0905142016-03-13 09:46:49 +00001446
Richard Hughesa785a1c2017-08-25 16:00:58 +01001447 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001448 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001449 if (blob_cab == NULL) {
1450 g_set_error_literal (error,
1451 FWUPD_ERROR,
1452 FWUPD_ERROR_NOT_SUPPORTED,
1453 "No cabinet archive to schedule");
1454 return FALSE;
1455 }
Richard Hughesa785a1c2017-08-25 16:00:58 +01001456 return fu_plugin_runner_schedule_update (plugin,
1457 device,
1458 blob_cab,
1459 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001460 }
1461
1462 /* cancel the pending action */
1463 if (!fu_plugin_runner_offline_invalidate (error))
1464 return FALSE;
1465
1466 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001467 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001468 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001469 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughesc0cd0232018-01-31 15:02:00 +00001470 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001471 g_propagate_error (error, error_update);
1472 return FALSE;
1473 }
1474
Richard Hughesf556d372017-06-15 19:49:18 +01001475 /* no longer valid */
1476 checksums = fu_device_get_checksums (device);
1477 g_ptr_array_set_size (checksums, 0);
1478
Richard Hughescff38bc2016-12-12 12:03:37 +00001479 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001480 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001481 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001482 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001483
Richard Hughes780ef3f2018-01-12 16:20:31 +00001484 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001485 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1486 if (!fu_history_modify_device (history, device,
1487 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1488 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001489 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001490
1491 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001492 release = fu_device_get_release_default (device_pending);
1493 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001494 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1495 g_autoptr(GError) error_local = NULL;
1496 g_autoptr(GFile) file = NULL;
1497 file = g_file_new_for_path (tmp);
1498 if (!g_file_delete (file, NULL, &error_local)) {
1499 g_set_error (error,
1500 FWUPD_ERROR,
1501 FWUPD_ERROR_INVALID_FILE,
1502 "Failed to delete %s: %s",
1503 tmp, error_local->message);
1504 return FALSE;
1505 }
1506 }
1507 }
Richard Hughesd0905142016-03-13 09:46:49 +00001508 return TRUE;
1509}
Richard Hughescff38bc2016-12-12 12:03:37 +00001510
1511gboolean
1512fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error)
1513{
1514 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001515 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001516
1517 /* not enabled */
1518 if (!priv->enabled)
1519 return TRUE;
1520
Richard Hughes639da472018-01-06 22:35:04 +00001521 /* no object loaded */
1522 if (priv->module == NULL)
1523 return TRUE;
1524
Richard Hughes65e44ca2018-01-30 17:26:30 +00001525 /* optional */
1526 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1527 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001528 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001529 g_debug ("performing clear_result() on %s", priv->name);
1530 if (!func (plugin, device, error)) {
1531 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001532 return FALSE;
1533 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001534 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001535}
1536
1537gboolean
1538fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1539{
1540 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001541 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001542
1543 /* not enabled */
1544 if (!priv->enabled)
1545 return TRUE;
1546
Richard Hughes639da472018-01-06 22:35:04 +00001547 /* no object loaded */
1548 if (priv->module == NULL)
1549 return TRUE;
1550
Richard Hughes65e44ca2018-01-30 17:26:30 +00001551 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001552 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001553 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001554 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001555 g_debug ("performing get_results() on %s", priv->name);
1556 if (!func (plugin, device, error)) {
1557 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001558 return FALSE;
1559 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001560 return TRUE;
1561}
1562
Richard Hughes08a37992017-09-12 12:57:43 +01001563/**
1564 * fu_plugin_get_order:
1565 * @plugin: a #FuPlugin
1566 *
1567 * Gets the plugin order, where higher numbers are run after lower
1568 * numbers.
1569 *
1570 * Returns: the integer value
1571 **/
1572guint
1573fu_plugin_get_order (FuPlugin *plugin)
1574{
1575 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1576 return priv->order;
1577}
1578
1579/**
1580 * fu_plugin_set_order:
1581 * @plugin: a #FuPlugin
1582 * @order: a integer value
1583 *
1584 * Sets the plugin order, where higher numbers are run after lower
1585 * numbers.
1586 **/
1587void
1588fu_plugin_set_order (FuPlugin *plugin, guint order)
1589{
1590 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1591 priv->order = order;
1592}
1593
1594/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001595 * fu_plugin_get_priority:
1596 * @plugin: a #FuPlugin
1597 *
1598 * Gets the plugin priority, where higher numbers are better.
1599 *
1600 * Returns: the integer value
1601 **/
1602guint
1603fu_plugin_get_priority (FuPlugin *plugin)
1604{
1605 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1606 return priv->priority;
1607}
1608
1609/**
1610 * fu_plugin_set_priority:
1611 * @plugin: a #FuPlugin
1612 * @priority: a integer value
1613 *
1614 * Sets the plugin priority, where higher numbers are better.
1615 **/
1616void
1617fu_plugin_set_priority (FuPlugin *plugin, guint priority)
1618{
1619 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1620 priv->priority = priority;
1621}
1622
1623/**
Richard Hughes08a37992017-09-12 12:57:43 +01001624 * fu_plugin_add_rule:
1625 * @plugin: a #FuPlugin
1626 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001627 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001628 *
1629 * If the plugin name is found, the rule will be used to sort the plugin list,
1630 * for example the plugin specified by @name will be ordered after this plugin
1631 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1632 *
1633 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1634 * If depsolving fails then fwupd will not start.
1635 **/
1636void
1637fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1638{
1639 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1640 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1641}
1642
1643/**
1644 * fu_plugin_get_rules:
1645 * @plugin: a #FuPlugin
1646 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1647 *
1648 * Gets the plugin IDs that should be run after this plugin.
1649 *
1650 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1651 **/
1652GPtrArray *
1653fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1654{
1655 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1656 return priv->rules[rule];
1657}
1658
Richard Hughes80b79bb2018-01-11 21:11:06 +00001659/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001660 * fu_plugin_has_rule:
1661 * @plugin: a #FuPlugin
1662 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1663 * @name: a plugin name, e.g. `upower`
1664 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001665 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001666 *
1667 * Returns: %TRUE if the name exists for the specific rule
1668 **/
1669gboolean
1670fu_plugin_has_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1671{
1672 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1673 for (guint i = 0; i < priv->rules[rule]->len; i++) {
1674 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
1675 if (g_strcmp0 (tmp, name) == 0)
1676 return TRUE;
1677 }
1678 return FALSE;
1679}
1680
1681/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00001682 * fu_plugin_add_report_metadata:
1683 * @plugin: a #FuPlugin
1684 * @key: a string, e.g. `FwupdateVersion`
1685 * @value: a string, e.g. `10`
1686 *
1687 * Sets any additional metadata to be included in the firmware report to aid
1688 * debugging problems.
1689 *
1690 * Any data included here will be sent to the metadata server after user
1691 * confirmation.
1692 **/
1693void
1694fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1695{
1696 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1697 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1698}
1699
1700/**
1701 * fu_plugin_get_report_metadata:
1702 * @plugin: a #FuPlugin
1703 *
1704 * Returns the list of additional metadata to be added when filing a report.
1705 *
1706 * Returns: (transfer none): the map of report metadata
1707 **/
1708GHashTable *
1709fu_plugin_get_report_metadata (FuPlugin *plugin)
1710{
1711 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1712 return priv->report_metadata;
1713}
1714
Mario Limonciello963dc422018-02-27 14:26:58 -06001715/**
1716 * fu_plugin_get_config_value:
1717 * @plugin: a #FuPlugin
1718 * @key: A settings key
1719 *
1720 * Return the value of a key if it's been configured
1721 *
1722 * Since: 1.0.6
1723 **/
1724gchar *
1725fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key)
1726{
Richard Hughes4be17d12018-05-30 20:36:29 +01001727 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06001728 g_autofree gchar *conf_file = NULL;
1729 g_autofree gchar *conf_path = NULL;
1730 g_autoptr(GKeyFile) keyfile = NULL;
1731 const gchar *plugin_name;
1732
Richard Hughes4be17d12018-05-30 20:36:29 +01001733 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Mario Limonciello963dc422018-02-27 14:26:58 -06001734 plugin_name = fu_plugin_get_name (plugin);
1735 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01001736 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06001737 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1738 return NULL;
1739 keyfile = g_key_file_new ();
1740 if (!g_key_file_load_from_file (keyfile, conf_path,
1741 G_KEY_FILE_NONE, NULL))
1742 return NULL;
1743 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1744}
1745
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001746/**
1747 * fu_plugin_name_compare:
1748 * @plugin1: first #FuPlugin to compare.
1749 * @plugin2: second #FuPlugin to compare.
1750 *
1751 * Compares two plugins by their names.
1752 *
1753 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1754 **/
1755gint
1756fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1757{
1758 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1759 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1760 return g_strcmp0 (priv1->name, priv2->name);
1761}
1762
1763/**
1764 * fu_plugin_order_compare:
1765 * @plugin1: first #FuPlugin to compare.
1766 * @plugin2: second #FuPlugin to compare.
1767 *
1768 * Compares two plugins by their depsolved order.
1769 *
1770 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1771 **/
1772gint
1773fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1774{
1775 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1776 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1777 if (priv1->order < priv2->order)
1778 return -1;
1779 if (priv1->order > priv2->order)
1780 return 1;
1781 return 0;
1782}
1783
Richard Hughescff38bc2016-12-12 12:03:37 +00001784static void
1785fu_plugin_class_init (FuPluginClass *klass)
1786{
1787 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1788 object_class->finalize = fu_plugin_finalize;
1789 signals[SIGNAL_DEVICE_ADDED] =
1790 g_signal_new ("device-added",
1791 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1792 G_STRUCT_OFFSET (FuPluginClass, device_added),
1793 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1794 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1795 signals[SIGNAL_DEVICE_REMOVED] =
1796 g_signal_new ("device-removed",
1797 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1798 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1799 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1800 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001801 signals[SIGNAL_DEVICE_REGISTER] =
1802 g_signal_new ("device-register",
1803 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1804 G_STRUCT_OFFSET (FuPluginClass, device_register),
1805 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1806 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001807 signals[SIGNAL_RECOLDPLUG] =
1808 g_signal_new ("recoldplug",
1809 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1810 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1811 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1812 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001813 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1814 g_signal_new ("set-coldplug-delay",
1815 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1816 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1817 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1818 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001819}
1820
1821static void
1822fu_plugin_init (FuPlugin *plugin)
1823{
1824 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1825 priv->enabled = TRUE;
1826 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1827 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001828 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001829 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 +01001830 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1831 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001832}
1833
1834static void
1835fu_plugin_finalize (GObject *object)
1836{
1837 FuPlugin *plugin = FU_PLUGIN (object);
1838 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1839 FuPluginInitFunc func = NULL;
1840
1841 /* optional */
1842 if (priv->module != NULL) {
1843 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1844 if (func != NULL) {
1845 g_debug ("performing destroy() on %s", priv->name);
1846 func (plugin);
1847 }
1848 }
1849
Richard Hughes08a37992017-09-12 12:57:43 +01001850 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1851 g_ptr_array_unref (priv->rules[i]);
1852
Richard Hughescff38bc2016-12-12 12:03:37 +00001853 if (priv->usb_ctx != NULL)
1854 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001855 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001856 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001857 if (priv->quirks != NULL)
1858 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001859 if (priv->supported_guids != NULL)
1860 g_ptr_array_unref (priv->supported_guids);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001861 if (priv->udev_subsystems != NULL)
1862 g_ptr_array_unref (priv->udev_subsystems);
Richard Hughes49e5e052017-09-03 12:15:41 +01001863 if (priv->smbios != NULL)
1864 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001865 if (priv->runtime_versions != NULL)
1866 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001867 if (priv->compile_versions != NULL)
1868 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00001869 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001870 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001871 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001872 g_free (priv->name);
1873 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05001874 /* Must happen as the last step to avoid prematurely
1875 * freeing memory held by the plugin */
1876#ifndef RUNNING_ON_VALGRIND
1877 if (priv->module != NULL)
1878 g_module_close (priv->module);
1879#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001880
1881 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1882}
1883
1884FuPlugin *
1885fu_plugin_new (void)
1886{
1887 FuPlugin *plugin;
1888 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1889 return plugin;
1890}