blob: f744ab4610e55e1ca83004d78bc9d0eb64011f9d [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 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "config.h"
23
Richard Hughescff38bc2016-12-12 12:03:37 +000024#include <fwupd.h>
25#include <gmodule.h>
26#include <appstream-glib.h>
27#include <errno.h>
28#include <string.h>
29#include <gio/gunixinputstream.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060030#ifdef HAVE_VALGRIND
Richard Hughes576c0122017-02-24 09:47:00 +000031#include <valgrind.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060032#endif /* HAVE_VALGRIND */
Richard Hughesd0905142016-03-13 09:46:49 +000033
Richard Hughes9dde04f2017-09-13 12:07:15 +010034#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000035#include "fu-plugin-private.h"
Richard Hughesbc3a4e12018-01-06 22:41:47 +000036#include "fu-history.h"
Richard Hughesd0905142016-03-13 09:46:49 +000037
Richard Hughes4eada342017-10-03 21:20:32 +010038/**
39 * SECTION:fu-plugin
40 * @short_description: a daemon plugin
41 *
42 * An object that represents a plugin run by the daemon.
43 *
44 * See also: #FuDevice
45 */
46
Richard Hughesb0829032017-01-10 09:27:08 +000047#define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */
48
Richard Hughescff38bc2016-12-12 12:03:37 +000049static void fu_plugin_finalize (GObject *object);
50
51typedef struct {
52 GModule *module;
53 GUsbContext *usb_ctx;
54 gboolean enabled;
Richard Hughes08a37992017-09-12 12:57:43 +010055 guint order;
56 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughescff38bc2016-12-12 12:03:37 +000057 gchar *name;
Richard Hughesd7704d42017-08-08 20:29:09 +010058 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010059 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010060 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010061 GHashTable *compile_versions;
Richard Hughes1354ea92017-09-19 15:58:31 +010062 GPtrArray *supported_guids;
63 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000064 GHashTable *devices; /* platform_id:GObject */
Richard Hughesae3d65f2016-12-16 09:38:01 +000065 GHashTable *devices_delay; /* FuDevice:FuPluginHelper */
Richard Hughes80b79bb2018-01-11 21:11:06 +000066 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000067 FuPluginData *data;
68} FuPluginPrivate;
69
70enum {
71 SIGNAL_DEVICE_ADDED,
72 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010073 SIGNAL_DEVICE_REGISTER,
Richard Hughes362d6d72017-01-07 21:42:14 +000074 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000075 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughescff38bc2016-12-12 12:03:37 +000076 SIGNAL_LAST
77};
78
79static guint signals[SIGNAL_LAST] = { 0 };
80
81G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
82#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
83
84typedef const gchar *(*FuPluginGetNameFunc) (void);
85typedef void (*FuPluginInitFunc) (FuPlugin *plugin);
86typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin,
87 GError **error);
Richard Hughese1fd34d2017-08-24 14:19:51 +010088typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin,
89 FuDevice *device);
Richard Hughescff38bc2016-12-12 12:03:37 +000090typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin,
91 FuDevice *device,
92 GError **error);
93typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin,
94 FuDevice *device,
95 FuPluginVerifyFlags flags,
96 GError **error);
97typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin,
98 FuDevice *device,
99 GBytes *blob_fw,
100 FwupdInstallFlags flags,
101 GError **error);
Richard Hughes104f6512017-11-24 11:44:57 +0000102typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin,
103 GUsbDevice *usb_device,
104 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +0000105
Richard Hughes57d18222017-01-10 16:02:59 +0000106/**
107 * fu_plugin_get_name:
108 * @plugin: A #FuPlugin
109 *
110 * Gets the plugin name.
111 *
112 * Returns: a plugin name, or %NULL for unknown.
113 *
114 * Since: 0.8.0
115 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000116const gchar *
117fu_plugin_get_name (FuPlugin *plugin)
Richard Hughesd0905142016-03-13 09:46:49 +0000118{
Richard Hughescff38bc2016-12-12 12:03:37 +0000119 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000120 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000121 return priv->name;
122}
Richard Hughesd0905142016-03-13 09:46:49 +0000123
Richard Hughes34834102017-11-21 21:55:00 +0000124void
125fu_plugin_set_name (FuPlugin *plugin, const gchar *name)
126{
127 FuPluginPrivate *priv = GET_PRIVATE (plugin);
128 g_return_if_fail (FU_IS_PLUGIN (plugin));
129 g_return_if_fail (name != NULL);
130 g_free (priv->name);
131 priv->name = g_strdup (name);
132}
133
Richard Hughes57d18222017-01-10 16:02:59 +0000134/**
135 * fu_plugin_cache_lookup:
136 * @plugin: A #FuPlugin
137 * @id: the key
138 *
139 * Finds an object in the per-plugin cache.
140 *
141 * Returns: (transfer none): a #GObject, or %NULL for unfound.
142 *
143 * Since: 0.8.0
144 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000145gpointer
146fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id)
147{
148 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000149 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
150 g_return_val_if_fail (id != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000151 return g_hash_table_lookup (priv->devices, id);
152}
Richard Hughesd0905142016-03-13 09:46:49 +0000153
Richard Hughes57d18222017-01-10 16:02:59 +0000154/**
155 * fu_plugin_cache_add:
156 * @plugin: A #FuPlugin
157 * @id: the key
158 * @dev: a #GObject, typically a #FuDevice
159 *
160 * Adds an object to the per-plugin cache.
161 *
162 * Since: 0.8.0
163 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000164void
165fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev)
166{
167 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000168 g_return_if_fail (FU_IS_PLUGIN (plugin));
169 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000170 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
171}
172
Richard Hughes57d18222017-01-10 16:02:59 +0000173/**
174 * fu_plugin_cache_remove:
175 * @plugin: A #FuPlugin
176 * @id: the key
177 *
178 * Removes an object from the per-plugin cache.
179 *
180 * Since: 0.8.0
181 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000182void
183fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id)
184{
185 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000186 g_return_if_fail (FU_IS_PLUGIN (plugin));
187 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000188 g_hash_table_remove (priv->devices, id);
189}
190
Richard Hughes57d18222017-01-10 16:02:59 +0000191/**
192 * fu_plugin_get_data:
193 * @plugin: A #FuPlugin
194 *
Richard Hughes4eada342017-10-03 21:20:32 +0100195 * Gets the per-plugin allocated private data. This will return %NULL unless
196 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000197 *
Richard Hughes4eada342017-10-03 21:20:32 +0100198 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000199 *
200 * Since: 0.8.0
201 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000202FuPluginData *
203fu_plugin_get_data (FuPlugin *plugin)
204{
205 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000206 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000207 return priv->data;
208}
209
Richard Hughes57d18222017-01-10 16:02:59 +0000210/**
211 * fu_plugin_alloc_data:
212 * @plugin: A #FuPlugin
213 * @data_sz: the size to allocate
214 *
215 * Allocates the per-plugin allocated private data.
216 *
217 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
218 *
219 * Since: 0.8.0
220 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000221FuPluginData *
222fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz)
223{
224 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000225 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000226 if (priv->data != NULL) {
227 g_critical ("fu_plugin_alloc_data() already used by plugin");
228 return priv->data;
229 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000230 priv->data = g_malloc0 (data_sz);
231 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000232}
233
Richard Hughes57d18222017-01-10 16:02:59 +0000234/**
235 * fu_plugin_get_usb_context:
236 * @plugin: A #FuPlugin
237 *
238 * Gets the shared USB context that all plugins can use.
239 *
240 * Returns: (transfer none): a #GUsbContext.
241 *
242 * Since: 0.8.0
243 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000244GUsbContext *
245fu_plugin_get_usb_context (FuPlugin *plugin)
246{
Richard Hughescff38bc2016-12-12 12:03:37 +0000247 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000248 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000249 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000250}
251
252void
253fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx)
254{
Richard Hughescff38bc2016-12-12 12:03:37 +0000255 FuPluginPrivate *priv = GET_PRIVATE (plugin);
256 g_set_object (&priv->usb_ctx, usb_ctx);
257}
258
Richard Hughes57d18222017-01-10 16:02:59 +0000259/**
260 * fu_plugin_get_enabled:
261 * @plugin: A #FuPlugin
262 *
Richard Hughes4eada342017-10-03 21:20:32 +0100263 * Returns if the plugin is enabled. Plugins may self-disable using
264 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000265 *
266 * Returns: %TRUE if the plugin is currently enabled.
267 *
268 * Since: 0.8.0
269 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000270gboolean
271fu_plugin_get_enabled (FuPlugin *plugin)
272{
273 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000274 g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000275 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000276}
277
Richard Hughes57d18222017-01-10 16:02:59 +0000278/**
279 * fu_plugin_set_enabled:
280 * @plugin: A #FuPlugin
281 * @enabled: the enabled value
282 *
283 * Enables or disables a plugin. Plugins can self-disable at any point.
284 *
285 * Since: 0.8.0
286 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000287void
Richard Hughescff38bc2016-12-12 12:03:37 +0000288fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000289{
Richard Hughescff38bc2016-12-12 12:03:37 +0000290 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000291 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughescff38bc2016-12-12 12:03:37 +0000292 priv->enabled = enabled;
293}
294
Richard Hughes1e456bc2018-05-10 20:16:16 +0100295gchar *
296fu_plugin_guess_name_from_fn (const gchar *filename)
297{
298 const gchar *prefix = "libfu_plugin_";
299 gchar *name;
300 gchar *str = g_strstr_len (filename, -1, prefix);
301 if (str == NULL)
302 return NULL;
303 name = g_strdup (str + strlen (prefix));
304 g_strdelimit (name, ".", '\0');
305 return name;
306}
307
Richard Hughescff38bc2016-12-12 12:03:37 +0000308gboolean
309fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error)
310{
311 FuPluginPrivate *priv = GET_PRIVATE (plugin);
312 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000313
314 priv->module = g_module_open (filename, 0);
315 if (priv->module == NULL) {
316 g_set_error (error,
317 G_IO_ERROR,
318 G_IO_ERROR_FAILED,
319 "failed to open plugin: %s",
320 g_module_error ());
321 return FALSE;
322 }
323
324 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100325 if (priv->name == NULL)
326 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000327
328 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000329 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
330 if (func != NULL) {
331 g_debug ("performing init() on %s", filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000332 func (plugin);
333 }
334
Richard Hughescff38bc2016-12-12 12:03:37 +0000335 return TRUE;
336}
337
Richard Hughes57d18222017-01-10 16:02:59 +0000338/**
339 * fu_plugin_device_add:
340 * @plugin: A #FuPlugin
341 * @device: A #FuDevice
342 *
343 * Asks the daemon to add a device to the exported list. If this device ID
344 * has already been added by a different plugin then this request will be
345 * ignored.
346 *
347 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
348 * actually flashing an image to the hardware so that higher-priority plugins
349 * can add the device themselves.
350 *
351 * Since: 0.8.0
352 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000353void
354fu_plugin_device_add (FuPlugin *plugin, FuDevice *device)
355{
Richard Hughes5e447292018-04-27 14:25:54 +0100356 GPtrArray *children;
357
Richard Hughesccd78a92017-01-11 16:57:41 +0000358 g_return_if_fail (FU_IS_PLUGIN (plugin));
359 g_return_if_fail (FU_IS_DEVICE (device));
360
Richard Hughescff38bc2016-12-12 12:03:37 +0000361 g_debug ("emit added from %s: %s",
362 fu_plugin_get_name (plugin),
363 fu_device_get_id (device));
364 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
365 fu_device_set_plugin (device, fu_plugin_get_name (plugin));
366 g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100367
368 /* add children */
369 children = fu_device_get_children (device);
370 for (guint i = 0; i < children->len; i++) {
371 FuDevice *child = g_ptr_array_index (children, i);
372 fu_plugin_device_add (plugin, child);
373 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000374}
375
Richard Hughese1fd34d2017-08-24 14:19:51 +0100376/**
377 * fu_plugin_device_register:
378 * @plugin: A #FuPlugin
379 * @device: A #FuDevice
380 *
381 * Registers the device with other plugins so they can set metadata.
382 *
383 * Plugins do not have to call this manually as this is done automatically
384 * when using fu_plugin_device_add(). They may wish to use this manually
385 * if for intance the coldplug should be ignored based on the metadata
386 * set from other plugins.
387 *
388 * Since: 0.9.7
389 **/
390void
391fu_plugin_device_register (FuPlugin *plugin, FuDevice *device)
392{
393 g_return_if_fail (FU_IS_PLUGIN (plugin));
394 g_return_if_fail (FU_IS_DEVICE (device));
395
396 g_debug ("emit device-register from %s: %s",
397 fu_plugin_get_name (plugin),
398 fu_device_get_id (device));
399 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device);
400}
401
Richard Hughesae3d65f2016-12-16 09:38:01 +0000402typedef struct {
403 FuPlugin *plugin;
404 FuDevice *device;
405 guint timeout_id;
Richard Hughesd4184cf2016-12-21 16:05:17 +0000406 GHashTable *devices;
Richard Hughesae3d65f2016-12-16 09:38:01 +0000407} FuPluginHelper;
408
409static void
410fu_plugin_helper_free (FuPluginHelper *helper)
411{
412 g_object_unref (helper->plugin);
413 g_object_unref (helper->device);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000414 g_hash_table_unref (helper->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000415 g_free (helper);
416}
417
418static gboolean
419fu_plugin_device_add_delay_cb (gpointer user_data)
420{
421 FuPluginHelper *helper = (FuPluginHelper *) user_data;
422 fu_plugin_device_add (helper->plugin, helper->device);
Richard Hughes0dec2742017-09-18 11:07:39 +0100423 g_hash_table_remove (helper->devices, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000424 fu_plugin_helper_free (helper);
425 return FALSE;
426}
427
Richard Hughes57d18222017-01-10 16:02:59 +0000428/**
Richard Hughesf0a799e2017-01-17 20:13:30 +0000429 * fu_plugin_has_device_delay:
430 * @plugin: A #FuPlugin
431 *
432 * Returns if the device has a pending device that is waiting to be added.
433 *
434 * Returns: %TRUE if a device is waiting to be added
435 *
436 * Since: 0.8.0
437 **/
438gboolean
439fu_plugin_has_device_delay (FuPlugin *plugin)
440{
441 FuPluginPrivate *priv = GET_PRIVATE (plugin);
442 return g_hash_table_size (priv->devices_delay) > 0;
443}
444
445/**
Richard Hughes57d18222017-01-10 16:02:59 +0000446 * fu_plugin_device_add_delay:
447 * @plugin: A #FuPlugin
448 * @device: A #FuDevice
449 *
450 * Asks the daemon to add a device to the exported list after a small delay.
451 *
452 * Since: 0.8.0
453 **/
Richard Hughesae3d65f2016-12-16 09:38:01 +0000454void
455fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device)
456{
457 FuPluginPrivate *priv = GET_PRIVATE (plugin);
458 FuPluginHelper *helper;
Richard Hughesccd78a92017-01-11 16:57:41 +0000459
460 g_return_if_fail (FU_IS_PLUGIN (plugin));
461 g_return_if_fail (FU_IS_DEVICE (device));
462
Richard Hughes6ad951d2017-02-03 10:12:12 +0000463 /* already waiting for add */
464 helper = g_hash_table_lookup (priv->devices_delay, device);
465 if (helper != NULL) {
Richard Hughes3cca1c62017-07-21 13:38:20 +0100466 g_debug ("ignoring add-delay as device %s already pending",
467 fu_device_get_id (device));
Richard Hughes6ad951d2017-02-03 10:12:12 +0000468 return;
469 }
470
471 /* add after a small delay */
Richard Hughesae3d65f2016-12-16 09:38:01 +0000472 g_debug ("waiting a small time for other plugins");
473 helper = g_new0 (FuPluginHelper, 1);
474 helper->plugin = g_object_ref (plugin);
475 helper->device = g_object_ref (device);
476 helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000477 helper->devices = g_hash_table_ref (priv->devices_delay);
478 g_hash_table_insert (helper->devices, device, helper);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000479}
480
Richard Hughes57d18222017-01-10 16:02:59 +0000481/**
Richard Hughes4eada342017-10-03 21:20:32 +0100482 * fu_plugin_device_remove:
Richard Hughes57d18222017-01-10 16:02:59 +0000483 * @plugin: A #FuPlugin
484 * @device: A #FuDevice
485 *
486 * Asks the daemon to remove a device from the exported list.
487 *
488 * Since: 0.8.0
489 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000490void
491fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device)
492{
Richard Hughesae3d65f2016-12-16 09:38:01 +0000493 FuPluginPrivate *priv = GET_PRIVATE (plugin);
494 FuPluginHelper *helper;
495
Richard Hughesccd78a92017-01-11 16:57:41 +0000496 g_return_if_fail (FU_IS_PLUGIN (plugin));
497 g_return_if_fail (FU_IS_DEVICE (device));
498
Richard Hughesae3d65f2016-12-16 09:38:01 +0000499 /* waiting for add */
500 helper = g_hash_table_lookup (priv->devices_delay, device);
501 if (helper != NULL) {
502 g_debug ("ignoring remove from delayed addition");
503 g_source_remove (helper->timeout_id);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000504 g_hash_table_remove (priv->devices_delay, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000505 fu_plugin_helper_free (helper);
506 return;
507 }
508
Richard Hughescff38bc2016-12-12 12:03:37 +0000509 g_debug ("emit removed from %s: %s",
510 fu_plugin_get_name (plugin),
511 fu_device_get_id (device));
512 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device);
513}
514
Richard Hughes57d18222017-01-10 16:02:59 +0000515/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000516 * fu_plugin_request_recoldplug:
Richard Hughes362d6d72017-01-07 21:42:14 +0000517 * @plugin: A #FuPlugin
518 *
519 * Ask all the plugins to coldplug all devices, which will include the prepare()
520 * and cleanup() phases. Duplicate devices added will be ignored.
521 *
522 * Since: 0.8.0
523 **/
524void
Richard Hughes2de8f132018-01-17 09:12:02 +0000525fu_plugin_request_recoldplug (FuPlugin *plugin)
Richard Hughes362d6d72017-01-07 21:42:14 +0000526{
Richard Hughesccd78a92017-01-11 16:57:41 +0000527 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughes362d6d72017-01-07 21:42:14 +0000528 g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0);
529}
530
Richard Hughesb0829032017-01-10 09:27:08 +0000531/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100532 * fu_plugin_check_hwid:
533 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100534 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100535 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100536 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100537 * specific system can be shown using the `fwupdmgr hwids` command.
538 *
Richard Hughes4eada342017-10-03 21:20:32 +0100539 * Returns: %TRUE if the HwId is found on the system.
540 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100541 * Since: 0.9.1
542 **/
543gboolean
544fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid)
545{
546 FuPluginPrivate *priv = GET_PRIVATE (plugin);
547 if (priv->hwids == NULL)
548 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100549 return fu_hwids_has_guid (priv->hwids, hwid);
550}
551
552/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100553 * fu_plugin_check_supported:
554 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100555 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100556 *
557 * Checks to see if a specific device GUID is supported, i.e. available in the
558 * AppStream metadata.
559 *
Richard Hughes4eada342017-10-03 21:20:32 +0100560 * Returns: %TRUE if the device is supported.
561 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100562 * Since: 1.0.0
563 **/
564gboolean
565fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid)
566{
567 FuPluginPrivate *priv = GET_PRIVATE (plugin);
568 if (priv->supported_guids == NULL)
569 return FALSE;
570 for (guint i = 0; i < priv->supported_guids->len; i++) {
571 const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i);
572 if (g_strcmp0 (guid, guid_tmp) == 0)
573 return TRUE;
574 }
575 return FALSE;
576}
577
578/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100579 * fu_plugin_get_dmi_value:
580 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100581 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100582 *
583 * Gets a hardware DMI value.
584 *
Richard Hughes4eada342017-10-03 21:20:32 +0100585 * Returns: The string, or %NULL
586 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100587 * Since: 0.9.7
588 **/
589const gchar *
590fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id)
591{
592 FuPluginPrivate *priv = GET_PRIVATE (plugin);
593 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100594 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100595 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100596}
597
Richard Hughes49e5e052017-09-03 12:15:41 +0100598/**
599 * fu_plugin_get_smbios_string:
600 * @plugin: A #FuPlugin
601 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
602 * @offset: A SMBIOS offset
603 *
604 * Gets a hardware SMBIOS string.
605 *
606 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
607 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
608 *
Richard Hughes4eada342017-10-03 21:20:32 +0100609 * Returns: A string, or %NULL
610 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100611 * Since: 0.9.8
612 **/
613const gchar *
614fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset)
615{
616 FuPluginPrivate *priv = GET_PRIVATE (plugin);
617 if (priv->smbios == NULL)
618 return NULL;
619 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
620}
621
622/**
623 * fu_plugin_get_smbios_data:
624 * @plugin: A #FuPlugin
625 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
626 *
627 * Gets a hardware SMBIOS data.
628 *
Richard Hughes4eada342017-10-03 21:20:32 +0100629 * Returns: (transfer none): A #GBytes, or %NULL
630 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100631 * Since: 0.9.8
632 **/
633GBytes *
634fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type)
635{
636 FuPluginPrivate *priv = GET_PRIVATE (plugin);
637 if (priv->smbios == NULL)
638 return NULL;
639 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
640}
641
Richard Hughesb8f8db22017-04-25 15:56:00 +0100642void
Richard Hughesd7704d42017-08-08 20:29:09 +0100643fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100644{
645 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesd7704d42017-08-08 20:29:09 +0100646 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100647}
648
Richard Hughes49e5e052017-09-03 12:15:41 +0100649void
Richard Hughes1354ea92017-09-19 15:58:31 +0100650fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids)
651{
652 FuPluginPrivate *priv = GET_PRIVATE (plugin);
653 if (priv->supported_guids != NULL)
654 g_ptr_array_unref (priv->supported_guids);
655 priv->supported_guids = g_ptr_array_ref (supported_guids);
656}
657
Richard Hughes9c028f02017-10-28 21:14:28 +0100658void
659fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks)
660{
661 FuPluginPrivate *priv = GET_PRIVATE (plugin);
662 g_set_object (&priv->quirks, quirks);
663}
664
665/**
666 * fu_plugin_get_quirks:
667 * @plugin: A #FuPlugin
668 *
669 * Returns the hardware database object. This can be used to discover device
670 * quirks or other device-specific settings.
671 *
672 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
673 *
674 * Since: 1.0.1
675 **/
676FuQuirks *
677fu_plugin_get_quirks (FuPlugin *plugin)
678{
679 FuPluginPrivate *priv = GET_PRIVATE (plugin);
680 return priv->quirks;
681}
682
Richard Hughes0eb123b2018-04-19 12:00:04 +0100683void
684fu_plugin_set_runtime_versions (FuPlugin *plugin, GHashTable *runtime_versions)
685{
686 FuPluginPrivate *priv = GET_PRIVATE (plugin);
687 priv->runtime_versions = g_hash_table_ref (runtime_versions);
688}
689
690/**
691 * fu_plugin_add_runtime_version:
692 * @plugin: A #FuPlugin
693 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
694 * @version: A version string, e.g. "1.2.3"
695 *
696 * Sets a runtime version of a specific dependancy.
697 *
698 * Since: 1.0.7
699 **/
700void
701fu_plugin_add_runtime_version (FuPlugin *plugin,
702 const gchar *component_id,
703 const gchar *version)
704{
705 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesb01b4862018-04-20 16:39:48 +0100706 if (priv->runtime_versions == NULL)
707 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100708 g_hash_table_insert (priv->runtime_versions,
709 g_strdup (component_id),
710 g_strdup (version));
711}
712
Richard Hughes34e0dab2018-04-20 16:43:00 +0100713void
714fu_plugin_set_compile_versions (FuPlugin *plugin, GHashTable *compile_versions)
715{
716 FuPluginPrivate *priv = GET_PRIVATE (plugin);
717 priv->compile_versions = g_hash_table_ref (compile_versions);
718}
719
720/**
721 * fu_plugin_add_compile_version:
722 * @plugin: A #FuPlugin
723 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
724 * @version: A version string, e.g. "1.2.3"
725 *
726 * Sets a compile-time version of a specific dependancy.
727 *
728 * Since: 1.0.7
729 **/
730void
731fu_plugin_add_compile_version (FuPlugin *plugin,
732 const gchar *component_id,
733 const gchar *version)
734{
735 FuPluginPrivate *priv = GET_PRIVATE (plugin);
736 if (priv->compile_versions == NULL)
737 return;
738 g_hash_table_insert (priv->compile_versions,
739 g_strdup (component_id),
740 g_strdup (version));
741}
742
Richard Hughes9c028f02017-10-28 21:14:28 +0100743/**
744 * fu_plugin_lookup_quirk_by_id:
745 * @plugin: A #FuPlugin
746 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
747 * @id: An ID to match the entry, e.g. "012345"
748 *
749 * Looks up an entry in the hardware database using a string value.
750 *
751 * Returns: (transfer none): values from the database, or %NULL if not found
752 *
753 * Since: 1.0.1
754 **/
755const gchar *
756fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *prefix, const gchar *id)
757{
758 FuPluginPrivate *priv = GET_PRIVATE (plugin);
759 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
760
761 /* wildcard */
762 if (g_strstr_len (id, -1, "*") != NULL)
763 return fu_quirks_lookup_by_glob (priv->quirks, prefix, id);
764
765 /* exact ID */
766 return fu_quirks_lookup_by_id (priv->quirks, prefix, id);
767}
768
769/**
770 * fu_plugin_lookup_quirk_by_usb_device:
771 * @plugin: A #FuPlugin
772 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
773 * @dev: A #GUsbDevice
774 *
775 * Looks up an entry in the hardware database using various keys generated
776 * from @dev.
777 *
778 * Returns: (transfer none): values from the database, or %NULL if not found
779 *
780 * Since: 1.0.1
781 **/
782const gchar *
783fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin, const gchar *prefix, GUsbDevice *dev)
784{
785 FuPluginPrivate *priv = GET_PRIVATE (plugin);
786 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
787 return fu_quirks_lookup_by_usb_device (priv->quirks, prefix, dev);
788}
789
Richard Hughes1354ea92017-09-19 15:58:31 +0100790/**
791 * fu_plugin_get_supported:
792 * @plugin: A #FuPlugin
793 *
794 * Gets all the device GUIDs supported by the daemon.
795 *
Richard Hughes4eada342017-10-03 21:20:32 +0100796 * Returns: (element-type utf8) (transfer none): GUIDs
797 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100798 * Since: 1.0.0
799 **/
800GPtrArray *
801fu_plugin_get_supported (FuPlugin *plugin)
802{
803 FuPluginPrivate *priv = GET_PRIVATE (plugin);
804 return priv->supported_guids;
805}
806
807void
Richard Hughes49e5e052017-09-03 12:15:41 +0100808fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios)
809{
810 FuPluginPrivate *priv = GET_PRIVATE (plugin);
811 g_set_object (&priv->smbios, smbios);
812}
813
Richard Hughesb8f8db22017-04-25 15:56:00 +0100814/**
Richard Hughesb0829032017-01-10 09:27:08 +0000815 * fu_plugin_set_coldplug_delay:
816 * @plugin: A #FuPlugin
817 * @duration: A delay in milliseconds
818 *
819 * Set the minimum time that should be waited inbetween the call to
820 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
821 * to be the minimum hardware initialisation time from a datasheet.
822 *
823 * It is better to use this function rather than using a sleep() in the plugin
824 * itself as then only one delay is done in the daemon rather than waiting for
825 * each coldplug prepare in a serial way.
826 *
827 * Additionally, very long delays should be avoided as the daemon will be
828 * blocked from processing requests whilst the coldplug delay is being
829 * performed.
830 *
831 * Since: 0.8.0
832 **/
833void
834fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration)
835{
836 g_return_if_fail (FU_IS_PLUGIN (plugin));
837 g_return_if_fail (duration > 0);
838
839 /* check sanity */
840 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
841 g_warning ("duration of %ums is crazy, truncating to %ums",
842 duration,
843 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
844 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
845 }
846
847 /* emit */
848 g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
849}
850
Richard Hughesd0905142016-03-13 09:46:49 +0000851gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000852fu_plugin_runner_startup (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000853{
Richard Hughescff38bc2016-12-12 12:03:37 +0000854 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000855 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000856
857 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000858 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000859 return TRUE;
860
Richard Hughes639da472018-01-06 22:35:04 +0000861 /* no object loaded */
862 if (priv->module == NULL)
863 return TRUE;
864
Richard Hughesd0905142016-03-13 09:46:49 +0000865 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000866 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
867 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000868 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000869 g_debug ("performing startup() on %s", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +0000870 if (!func (plugin, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000871 g_prefix_error (error, "failed to startup %s: ", priv->name);
872 return FALSE;
873 }
874 return TRUE;
875}
876
877static gboolean
878fu_plugin_runner_offline_invalidate (GError **error)
879{
880 g_autoptr(GError) error_local = NULL;
881 g_autoptr(GFile) file1 = NULL;
882
883 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
884
885 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
886 if (!g_file_query_exists (file1, NULL))
887 return TRUE;
888 if (!g_file_delete (file1, NULL, &error_local)) {
889 g_set_error (error,
890 FWUPD_ERROR,
891 FWUPD_ERROR_INTERNAL,
892 "Cannot delete %s: %s",
893 FU_OFFLINE_TRIGGER_FILENAME,
894 error_local->message);
895 return FALSE;
896 }
897 return TRUE;
898}
899
900static gboolean
901fu_plugin_runner_offline_setup (GError **error)
902{
903 gint rc;
904
905 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
906
907 /* create symlink for the systemd-system-update-generator */
908 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
909 if (rc < 0) {
910 g_set_error (error,
911 FWUPD_ERROR,
912 FWUPD_ERROR_INTERNAL,
913 "Failed to create symlink %s to %s: %s",
914 FU_OFFLINE_TRIGGER_FILENAME,
915 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000916 return FALSE;
917 }
918 return TRUE;
919}
920
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000921static gboolean
922fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device,
923 const gchar *symbol_name, GError **error)
924{
925 FuPluginPrivate *priv = GET_PRIVATE (plugin);
926 FuPluginDeviceFunc func = NULL;
927
928 /* not enabled */
929 if (!priv->enabled)
930 return TRUE;
931
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000932 /* no object loaded */
933 if (priv->module == NULL)
934 return TRUE;
935
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000936 /* optional */
937 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
938 if (func == NULL)
939 return TRUE;
940 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
941 if (!func (plugin, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000942 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000943 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000944 priv->name);
945 return FALSE;
946 }
947 return TRUE;
948}
949
Richard Hughesd0905142016-03-13 09:46:49 +0000950gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000951fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000952{
Richard Hughescff38bc2016-12-12 12:03:37 +0000953 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000954 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000955
956 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000957 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000958 return TRUE;
959
Richard Hughes639da472018-01-06 22:35:04 +0000960 /* no object loaded */
961 if (priv->module == NULL)
962 return TRUE;
963
Richard Hughesd0905142016-03-13 09:46:49 +0000964 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000965 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
966 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000967 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000968 g_debug ("performing coldplug() on %s", priv->name);
969 if (!func (plugin, error)) {
970 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
971 return FALSE;
972 }
973 return TRUE;
974}
975
Richard Hughes7b8b2022016-12-12 16:15:03 +0000976gboolean
Richard Hughes2de8f132018-01-17 09:12:02 +0000977fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error)
978{
979 FuPluginPrivate *priv = GET_PRIVATE (plugin);
980 FuPluginStartupFunc func = NULL;
981
982 /* not enabled */
983 if (!priv->enabled)
984 return TRUE;
985
986 /* no object loaded */
987 if (priv->module == NULL)
988 return TRUE;
989
990 /* optional */
991 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
992 if (func == NULL)
993 return TRUE;
994 g_debug ("performing recoldplug() on %s", priv->name);
995 if (!func (plugin, error)) {
996 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
997 return FALSE;
998 }
999 return TRUE;
1000}
1001
1002gboolean
Richard Hughes46487c92017-01-07 21:26:34 +00001003fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error)
1004{
1005 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1006 FuPluginStartupFunc func = NULL;
1007
1008 /* not enabled */
1009 if (!priv->enabled)
1010 return TRUE;
1011
Richard Hughes639da472018-01-06 22:35:04 +00001012 /* no object loaded */
1013 if (priv->module == NULL)
1014 return TRUE;
1015
Richard Hughes46487c92017-01-07 21:26:34 +00001016 /* optional */
1017 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1018 if (func == NULL)
1019 return TRUE;
1020 g_debug ("performing coldplug_prepare() on %s", priv->name);
1021 if (!func (plugin, error)) {
1022 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
1023 return FALSE;
1024 }
1025 return TRUE;
1026}
1027
1028gboolean
1029fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error)
1030{
1031 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1032 FuPluginStartupFunc func = NULL;
1033
1034 /* not enabled */
1035 if (!priv->enabled)
1036 return TRUE;
1037
Richard Hughes639da472018-01-06 22:35:04 +00001038 /* no object loaded */
1039 if (priv->module == NULL)
1040 return TRUE;
1041
Richard Hughes46487c92017-01-07 21:26:34 +00001042 /* optional */
1043 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1044 if (func == NULL)
1045 return TRUE;
1046 g_debug ("performing coldplug_cleanup() on %s", priv->name);
1047 if (!func (plugin, error)) {
1048 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
1049 return FALSE;
1050 }
1051 return TRUE;
1052}
1053
1054gboolean
Richard Hughes7b8b2022016-12-12 16:15:03 +00001055fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error)
1056{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001057 return fu_plugin_runner_device_generic (plugin, device,
1058 "fu_plugin_update_prepare", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001059}
1060
1061gboolean
1062fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error)
1063{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001064 return fu_plugin_runner_device_generic (plugin, device,
1065 "fu_plugin_update_cleanup", error);
1066}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001067
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001068gboolean
1069fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error)
1070{
1071 return fu_plugin_runner_device_generic (plugin, device,
1072 "fu_plugin_update_attach", error);
1073}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001074
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001075gboolean
1076fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error)
1077{
1078 return fu_plugin_runner_device_generic (plugin, device,
1079 "fu_plugin_update_detach", error);
1080}
1081
1082gboolean
1083fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error)
1084{
1085 return fu_plugin_runner_device_generic (plugin, device,
1086 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001087}
1088
Richard Hughes104f6512017-11-24 11:44:57 +00001089gboolean
1090fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error)
1091{
1092 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1093 FuPluginUsbDeviceAddedFunc func = NULL;
1094
1095 /* not enabled */
1096 if (!priv->enabled)
1097 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001098
1099 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001100 if (priv->module == NULL)
1101 return TRUE;
1102
1103 /* optional */
1104 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1105 if (func != NULL) {
1106 g_debug ("performing usb_device_added() on %s", priv->name);
1107 return func (plugin, usb_device, error);
1108 }
1109 return TRUE;
1110}
1111
Richard Hughese1fd34d2017-08-24 14:19:51 +01001112void
1113fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device)
1114{
1115 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1116 FuPluginDeviceRegisterFunc func = NULL;
1117
1118 /* not enabled */
1119 if (!priv->enabled)
1120 return;
Richard Hughes34834102017-11-21 21:55:00 +00001121 if (priv->module == NULL)
1122 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001123
1124 /* optional */
1125 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1126 if (func != NULL) {
1127 g_debug ("performing device_added() on %s", priv->name);
1128 func (plugin, device);
1129 }
1130}
1131
Richard Hughescff38bc2016-12-12 12:03:37 +00001132static gboolean
1133fu_plugin_runner_schedule_update (FuPlugin *plugin,
1134 FuDevice *device,
1135 GBytes *blob_cab,
1136 GError **error)
1137{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001138 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001139 gchar tmpname[] = {"XXXXXX.cap"};
1140 g_autofree gchar *dirname = NULL;
1141 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001142 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001143 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001144 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001145 g_autoptr(GFile) file = NULL;
1146
1147 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001148 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001149 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001150 if (res_tmp != NULL) {
1151 g_set_error (error,
1152 FWUPD_ERROR,
1153 FWUPD_ERROR_ALREADY_PENDING,
1154 "%s is already scheduled to be updated",
1155 fu_device_get_id (device));
1156 return FALSE;
1157 }
1158
1159 /* create directory */
1160 dirname = g_build_filename (LOCALSTATEDIR, "lib", "fwupd", NULL);
1161 file = g_file_new_for_path (dirname);
1162 if (!g_file_query_exists (file, NULL)) {
1163 if (!g_file_make_directory_with_parents (file, NULL, error))
1164 return FALSE;
1165 }
1166
1167 /* get a random filename */
1168 for (guint i = 0; i < 6; i++)
1169 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1170 filename = g_build_filename (dirname, tmpname, NULL);
1171
1172 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001173 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001174 if (!g_file_set_contents (filename,
1175 g_bytes_get_data (blob_cab, NULL),
1176 (gssize) g_bytes_get_size (blob_cab),
1177 error))
1178 return FALSE;
1179
1180 /* schedule for next boot */
1181 g_debug ("schedule %s to be installed to %s on next boot",
1182 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001183 release = fu_device_get_release_default (device);
1184 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1185 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001186
1187 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001188 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001189 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001190 return FALSE;
1191
1192 /* next boot we run offline */
1193 return fu_plugin_runner_offline_setup (error);
1194}
1195
1196gboolean
1197fu_plugin_runner_verify (FuPlugin *plugin,
1198 FuDevice *device,
1199 FuPluginVerifyFlags flags,
1200 GError **error)
1201{
1202 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001203 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001204 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001205
1206 /* not enabled */
1207 if (!priv->enabled)
1208 return TRUE;
1209
Richard Hughes639da472018-01-06 22:35:04 +00001210 /* no object loaded */
1211 if (priv->module == NULL)
1212 return TRUE;
1213
Richard Hughesababbb72017-06-15 20:18:36 +01001214 /* clear any existing verification checksums */
1215 checksums = fu_device_get_checksums (device);
1216 g_ptr_array_set_size (checksums, 0);
1217
Richard Hughescff38bc2016-12-12 12:03:37 +00001218 /* optional */
1219 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1220 if (func == NULL)
1221 return TRUE;
1222 g_debug ("performing verify() on %s", priv->name);
1223 if (!func (plugin, device, flags, error)) {
1224 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001225 return FALSE;
1226 }
1227 return TRUE;
1228}
1229
Richard Hughesd0905142016-03-13 09:46:49 +00001230gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001231fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001232{
Richard Hughescff38bc2016-12-12 12:03:37 +00001233 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001234
1235 /* final check */
1236 flags = fu_device_get_flags (device);
1237 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1238 g_set_error (error,
1239 FWUPD_ERROR,
1240 FWUPD_ERROR_NOT_SUPPORTED,
1241 "Device %s is not locked",
1242 fu_device_get_id (device));
1243 return FALSE;
1244 }
1245
Richard Hughes9c4b5312017-11-14 11:34:53 +00001246 /* run vfunc */
1247 if (!fu_plugin_runner_device_generic (plugin, device,
1248 "fu_plugin_unlock", error))
1249 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001250
1251 /* update with correct flags */
1252 flags = fu_device_get_flags (device);
1253 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1254 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1255 return TRUE;
1256}
1257
1258gboolean
1259fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001260 FuDevice *device,
1261 GBytes *blob_cab,
1262 GBytes *blob_fw,
1263 FwupdInstallFlags flags,
1264 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001265{
1266 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001267 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001268 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001269 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001270 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001271 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001272
1273 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001274 if (!priv->enabled) {
1275 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001276 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001277 }
Richard Hughesd0905142016-03-13 09:46:49 +00001278
Richard Hughes639da472018-01-06 22:35:04 +00001279 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001280 if (priv->module == NULL) {
1281 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001282 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001283 }
Richard Hughes639da472018-01-06 22:35:04 +00001284
Richard Hughesd0905142016-03-13 09:46:49 +00001285 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001286 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1287 if (update_func == NULL) {
1288 g_set_error_literal (error,
1289 FWUPD_ERROR,
1290 FWUPD_ERROR_NOT_SUPPORTED,
1291 "No update possible");
1292 return FALSE;
1293 }
Richard Hughesd0905142016-03-13 09:46:49 +00001294
Richard Hughesa785a1c2017-08-25 16:00:58 +01001295 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001296 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001297 if (blob_cab == NULL) {
1298 g_set_error_literal (error,
1299 FWUPD_ERROR,
1300 FWUPD_ERROR_NOT_SUPPORTED,
1301 "No cabinet archive to schedule");
1302 return FALSE;
1303 }
Richard Hughesa785a1c2017-08-25 16:00:58 +01001304 return fu_plugin_runner_schedule_update (plugin,
1305 device,
1306 blob_cab,
1307 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001308 }
1309
1310 /* cancel the pending action */
1311 if (!fu_plugin_runner_offline_invalidate (error))
1312 return FALSE;
1313
1314 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001315 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001316 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001317 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughesc0cd0232018-01-31 15:02:00 +00001318 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001319 g_propagate_error (error, error_update);
1320 return FALSE;
1321 }
1322
Richard Hughesf556d372017-06-15 19:49:18 +01001323 /* no longer valid */
1324 checksums = fu_device_get_checksums (device);
1325 g_ptr_array_set_size (checksums, 0);
1326
Richard Hughescff38bc2016-12-12 12:03:37 +00001327 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001328 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001329 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001330 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001331
Richard Hughes780ef3f2018-01-12 16:20:31 +00001332 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001333 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1334 if (!fu_history_modify_device (history, device,
1335 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1336 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001337 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001338
1339 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001340 release = fu_device_get_release_default (device_pending);
1341 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001342 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1343 g_autoptr(GError) error_local = NULL;
1344 g_autoptr(GFile) file = NULL;
1345 file = g_file_new_for_path (tmp);
1346 if (!g_file_delete (file, NULL, &error_local)) {
1347 g_set_error (error,
1348 FWUPD_ERROR,
1349 FWUPD_ERROR_INVALID_FILE,
1350 "Failed to delete %s: %s",
1351 tmp, error_local->message);
1352 return FALSE;
1353 }
1354 }
1355 }
Richard Hughesd0905142016-03-13 09:46:49 +00001356 return TRUE;
1357}
Richard Hughescff38bc2016-12-12 12:03:37 +00001358
1359gboolean
1360fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error)
1361{
1362 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001363 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001364
1365 /* not enabled */
1366 if (!priv->enabled)
1367 return TRUE;
1368
Richard Hughes639da472018-01-06 22:35:04 +00001369 /* no object loaded */
1370 if (priv->module == NULL)
1371 return TRUE;
1372
Richard Hughes65e44ca2018-01-30 17:26:30 +00001373 /* optional */
1374 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1375 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001376 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001377 g_debug ("performing clear_result() on %s", priv->name);
1378 if (!func (plugin, device, error)) {
1379 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001380 return FALSE;
1381 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001382 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001383}
1384
1385gboolean
1386fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1387{
1388 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001389 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001390
1391 /* not enabled */
1392 if (!priv->enabled)
1393 return TRUE;
1394
Richard Hughes639da472018-01-06 22:35:04 +00001395 /* no object loaded */
1396 if (priv->module == NULL)
1397 return TRUE;
1398
Richard Hughes65e44ca2018-01-30 17:26:30 +00001399 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001400 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001401 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001402 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001403 g_debug ("performing get_results() on %s", priv->name);
1404 if (!func (plugin, device, error)) {
1405 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001406 return FALSE;
1407 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001408 return TRUE;
1409}
1410
Richard Hughes08a37992017-09-12 12:57:43 +01001411/**
1412 * fu_plugin_get_order:
1413 * @plugin: a #FuPlugin
1414 *
1415 * Gets the plugin order, where higher numbers are run after lower
1416 * numbers.
1417 *
1418 * Returns: the integer value
1419 **/
1420guint
1421fu_plugin_get_order (FuPlugin *plugin)
1422{
1423 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1424 return priv->order;
1425}
1426
1427/**
1428 * fu_plugin_set_order:
1429 * @plugin: a #FuPlugin
1430 * @order: a integer value
1431 *
1432 * Sets the plugin order, where higher numbers are run after lower
1433 * numbers.
1434 **/
1435void
1436fu_plugin_set_order (FuPlugin *plugin, guint order)
1437{
1438 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1439 priv->order = order;
1440}
1441
1442/**
1443 * fu_plugin_add_rule:
1444 * @plugin: a #FuPlugin
1445 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001446 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001447 *
1448 * If the plugin name is found, the rule will be used to sort the plugin list,
1449 * for example the plugin specified by @name will be ordered after this plugin
1450 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1451 *
1452 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1453 * If depsolving fails then fwupd will not start.
1454 **/
1455void
1456fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1457{
1458 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1459 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1460}
1461
1462/**
1463 * fu_plugin_get_rules:
1464 * @plugin: a #FuPlugin
1465 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1466 *
1467 * Gets the plugin IDs that should be run after this plugin.
1468 *
1469 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1470 **/
1471GPtrArray *
1472fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1473{
1474 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1475 return priv->rules[rule];
1476}
1477
Richard Hughes80b79bb2018-01-11 21:11:06 +00001478/**
1479 * fu_plugin_add_report_metadata:
1480 * @plugin: a #FuPlugin
1481 * @key: a string, e.g. `FwupdateVersion`
1482 * @value: a string, e.g. `10`
1483 *
1484 * Sets any additional metadata to be included in the firmware report to aid
1485 * debugging problems.
1486 *
1487 * Any data included here will be sent to the metadata server after user
1488 * confirmation.
1489 **/
1490void
1491fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1492{
1493 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1494 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1495}
1496
1497/**
1498 * fu_plugin_get_report_metadata:
1499 * @plugin: a #FuPlugin
1500 *
1501 * Returns the list of additional metadata to be added when filing a report.
1502 *
1503 * Returns: (transfer none): the map of report metadata
1504 **/
1505GHashTable *
1506fu_plugin_get_report_metadata (FuPlugin *plugin)
1507{
1508 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1509 return priv->report_metadata;
1510}
1511
Mario Limonciello963dc422018-02-27 14:26:58 -06001512/**
1513 * fu_plugin_get_config_value:
1514 * @plugin: a #FuPlugin
1515 * @key: A settings key
1516 *
1517 * Return the value of a key if it's been configured
1518 *
1519 * Since: 1.0.6
1520 **/
1521gchar *
1522fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key)
1523{
1524 g_autofree gchar *conf_file = NULL;
1525 g_autofree gchar *conf_path = NULL;
1526 g_autoptr(GKeyFile) keyfile = NULL;
1527 const gchar *plugin_name;
1528
1529 plugin_name = fu_plugin_get_name (plugin);
1530 conf_file = g_strdup_printf ("%s.conf", plugin_name);
1531 conf_path = g_build_filename (FWUPDCONFIGDIR, conf_file, NULL);
1532 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1533 return NULL;
1534 keyfile = g_key_file_new ();
1535 if (!g_key_file_load_from_file (keyfile, conf_path,
1536 G_KEY_FILE_NONE, NULL))
1537 return NULL;
1538 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1539}
1540
Richard Hughescff38bc2016-12-12 12:03:37 +00001541static void
1542fu_plugin_class_init (FuPluginClass *klass)
1543{
1544 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1545 object_class->finalize = fu_plugin_finalize;
1546 signals[SIGNAL_DEVICE_ADDED] =
1547 g_signal_new ("device-added",
1548 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1549 G_STRUCT_OFFSET (FuPluginClass, device_added),
1550 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1551 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1552 signals[SIGNAL_DEVICE_REMOVED] =
1553 g_signal_new ("device-removed",
1554 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1555 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1556 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1557 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001558 signals[SIGNAL_DEVICE_REGISTER] =
1559 g_signal_new ("device-register",
1560 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1561 G_STRUCT_OFFSET (FuPluginClass, device_register),
1562 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1563 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001564 signals[SIGNAL_RECOLDPLUG] =
1565 g_signal_new ("recoldplug",
1566 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1567 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1568 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1569 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001570 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1571 g_signal_new ("set-coldplug-delay",
1572 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1573 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1574 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1575 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001576}
1577
1578static void
1579fu_plugin_init (FuPlugin *plugin)
1580{
1581 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1582 priv->enabled = TRUE;
1583 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1584 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001585 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001586 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 +01001587 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1588 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001589}
1590
1591static void
1592fu_plugin_finalize (GObject *object)
1593{
1594 FuPlugin *plugin = FU_PLUGIN (object);
1595 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1596 FuPluginInitFunc func = NULL;
1597
1598 /* optional */
1599 if (priv->module != NULL) {
1600 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1601 if (func != NULL) {
1602 g_debug ("performing destroy() on %s", priv->name);
1603 func (plugin);
1604 }
1605 }
1606
Richard Hughes08a37992017-09-12 12:57:43 +01001607 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1608 g_ptr_array_unref (priv->rules[i]);
1609
Richard Hughescff38bc2016-12-12 12:03:37 +00001610 if (priv->usb_ctx != NULL)
1611 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001612 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001613 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001614 if (priv->quirks != NULL)
1615 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001616 if (priv->supported_guids != NULL)
1617 g_ptr_array_unref (priv->supported_guids);
Richard Hughes49e5e052017-09-03 12:15:41 +01001618 if (priv->smbios != NULL)
1619 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001620 if (priv->runtime_versions != NULL)
1621 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001622 if (priv->compile_versions != NULL)
1623 g_hash_table_unref (priv->compile_versions);
Richard Hughes576c0122017-02-24 09:47:00 +00001624#ifndef RUNNING_ON_VALGRIND
Richard Hughescff38bc2016-12-12 12:03:37 +00001625 if (priv->module != NULL)
1626 g_module_close (priv->module);
Richard Hughes576c0122017-02-24 09:47:00 +00001627#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001628 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001629 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001630 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001631 g_free (priv->name);
1632 g_free (priv->data);
1633
1634 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1635}
1636
1637FuPlugin *
1638fu_plugin_new (void)
1639{
1640 FuPlugin *plugin;
1641 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1642 return plugin;
1643}