blob: 70778e9442137c79d9ad23af5c32b14bae54afd7 [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
Richard Hughesb08e7bc2018-09-11 10:51:13 +01007#define G_LOG_DOMAIN "FuPlugin"
8
Richard Hughesd0905142016-03-13 09:46:49 +00009#include "config.h"
10
Richard Hughescff38bc2016-12-12 12:03:37 +000011#include <fwupd.h>
12#include <gmodule.h>
13#include <appstream-glib.h>
14#include <errno.h>
15#include <string.h>
Richard Hughes68f12dd2018-08-09 14:43:31 +010016#include <unistd.h>
Richard Hughescff38bc2016-12-12 12:03:37 +000017#include <gio/gunixinputstream.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060018#ifdef HAVE_VALGRIND
Richard Hughes576c0122017-02-24 09:47:00 +000019#include <valgrind.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060020#endif /* HAVE_VALGRIND */
Richard Hughesd0905142016-03-13 09:46:49 +000021
Richard Hughes9dde04f2017-09-13 12:07:15 +010022#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000023#include "fu-plugin-private.h"
Richard Hughesbc3a4e12018-01-06 22:41:47 +000024#include "fu-history.h"
Richard Hughesd0905142016-03-13 09:46:49 +000025
Richard Hughes4eada342017-10-03 21:20:32 +010026/**
27 * SECTION:fu-plugin
28 * @short_description: a daemon plugin
29 *
30 * An object that represents a plugin run by the daemon.
31 *
32 * See also: #FuDevice
33 */
34
Richard Hughesb0829032017-01-10 09:27:08 +000035#define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */
36
Richard Hughescff38bc2016-12-12 12:03:37 +000037static void fu_plugin_finalize (GObject *object);
38
39typedef struct {
40 GModule *module;
41 GUsbContext *usb_ctx;
42 gboolean enabled;
Richard Hughes08a37992017-09-12 12:57:43 +010043 guint order;
Richard Hughes81c427c2018-08-06 15:20:17 +010044 guint priority;
Richard Hughes08a37992017-09-12 12:57:43 +010045 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughescff38bc2016-12-12 12:03:37 +000046 gchar *name;
Richard Hughesd7704d42017-08-08 20:29:09 +010047 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010048 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010049 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010050 GHashTable *compile_versions;
Richard Hughes1354ea92017-09-19 15:58:31 +010051 GPtrArray *supported_guids;
Richard Hughes9d6e0e72018-08-24 20:20:17 +010052 GPtrArray *udev_subsystems;
Richard Hughes1354ea92017-09-19 15:58:31 +010053 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000054 GHashTable *devices; /* platform_id:GObject */
Richard Hughes80b79bb2018-01-11 21:11:06 +000055 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000056 FuPluginData *data;
57} FuPluginPrivate;
58
59enum {
60 SIGNAL_DEVICE_ADDED,
61 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010062 SIGNAL_DEVICE_REGISTER,
Richard Hughes362d6d72017-01-07 21:42:14 +000063 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000064 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughescff38bc2016-12-12 12:03:37 +000065 SIGNAL_LAST
66};
67
68static guint signals[SIGNAL_LAST] = { 0 };
69
70G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
71#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
72
73typedef const gchar *(*FuPluginGetNameFunc) (void);
Richard Hughes12724852018-09-04 13:53:44 +010074typedef void (*FuPluginInitFunc) (FuPlugin *self);
75typedef gboolean (*FuPluginStartupFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000076 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010077typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *self,
Richard Hughese1fd34d2017-08-24 14:19:51 +010078 FuDevice *device);
Richard Hughes12724852018-09-04 13:53:44 +010079typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000080 FuDevice *device,
81 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010082typedef gboolean (*FuPluginFlaggedDeviceFunc) (FuPlugin *self,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -050083 FwupdInstallFlags flags,
84 FuDevice *device,
85 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010086typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *self,
Richard Hughesdbd8c762018-06-15 20:31:40 +010087 GPtrArray *devices,
88 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010089typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000090 FuDevice *device,
91 FuPluginVerifyFlags flags,
92 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010093typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000094 FuDevice *device,
95 GBytes *blob_fw,
96 FwupdInstallFlags flags,
97 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010098typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +010099 FuUsbDevice *device,
Richard Hughes104f6512017-11-24 11:44:57 +0000100 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100101typedef gboolean (*FuPluginUdevDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100102 FuUdevDevice *device,
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100103 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +0000104
Richard Hughes57d18222017-01-10 16:02:59 +0000105/**
106 * fu_plugin_get_name:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100107 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000108 *
109 * Gets the plugin name.
110 *
111 * Returns: a plugin name, or %NULL for unknown.
112 *
113 * Since: 0.8.0
114 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000115const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100116fu_plugin_get_name (FuPlugin *self)
Richard Hughesd0905142016-03-13 09:46:49 +0000117{
Richard Hughes12724852018-09-04 13:53:44 +0100118 FuPluginPrivate *priv = GET_PRIVATE (self);
119 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000120 return priv->name;
121}
Richard Hughesd0905142016-03-13 09:46:49 +0000122
Richard Hughes34834102017-11-21 21:55:00 +0000123void
Richard Hughes12724852018-09-04 13:53:44 +0100124fu_plugin_set_name (FuPlugin *self, const gchar *name)
Richard Hughes34834102017-11-21 21:55:00 +0000125{
Richard Hughes12724852018-09-04 13:53:44 +0100126 FuPluginPrivate *priv = GET_PRIVATE (self);
127 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughes34834102017-11-21 21:55:00 +0000128 g_return_if_fail (name != NULL);
129 g_free (priv->name);
130 priv->name = g_strdup (name);
131}
132
Richard Hughes57d18222017-01-10 16:02:59 +0000133/**
134 * fu_plugin_cache_lookup:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100135 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000136 * @id: the key
137 *
138 * Finds an object in the per-plugin cache.
139 *
140 * Returns: (transfer none): a #GObject, or %NULL for unfound.
141 *
142 * Since: 0.8.0
143 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000144gpointer
Richard Hughes12724852018-09-04 13:53:44 +0100145fu_plugin_cache_lookup (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000146{
Richard Hughes12724852018-09-04 13:53:44 +0100147 FuPluginPrivate *priv = GET_PRIVATE (self);
148 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughesccd78a92017-01-11 16:57:41 +0000149 g_return_val_if_fail (id != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000150 return g_hash_table_lookup (priv->devices, id);
151}
Richard Hughesd0905142016-03-13 09:46:49 +0000152
Richard Hughes57d18222017-01-10 16:02:59 +0000153/**
154 * fu_plugin_cache_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100155 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000156 * @id: the key
157 * @dev: a #GObject, typically a #FuDevice
158 *
159 * Adds an object to the per-plugin cache.
160 *
161 * Since: 0.8.0
162 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000163void
Richard Hughes12724852018-09-04 13:53:44 +0100164fu_plugin_cache_add (FuPlugin *self, const gchar *id, gpointer dev)
Richard Hughescff38bc2016-12-12 12:03:37 +0000165{
Richard Hughes12724852018-09-04 13:53:44 +0100166 FuPluginPrivate *priv = GET_PRIVATE (self);
167 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000168 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000169 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
170}
171
Richard Hughes57d18222017-01-10 16:02:59 +0000172/**
173 * fu_plugin_cache_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100174 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000175 * @id: the key
176 *
177 * Removes an object from the per-plugin cache.
178 *
179 * Since: 0.8.0
180 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000181void
Richard Hughes12724852018-09-04 13:53:44 +0100182fu_plugin_cache_remove (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000183{
Richard Hughes12724852018-09-04 13:53:44 +0100184 FuPluginPrivate *priv = GET_PRIVATE (self);
185 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000186 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000187 g_hash_table_remove (priv->devices, id);
188}
189
Richard Hughes57d18222017-01-10 16:02:59 +0000190/**
191 * fu_plugin_get_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100192 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000193 *
Richard Hughes4eada342017-10-03 21:20:32 +0100194 * Gets the per-plugin allocated private data. This will return %NULL unless
195 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000196 *
Richard Hughes4eada342017-10-03 21:20:32 +0100197 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000198 *
199 * Since: 0.8.0
200 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000201FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100202fu_plugin_get_data (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000203{
Richard Hughes12724852018-09-04 13:53:44 +0100204 FuPluginPrivate *priv = GET_PRIVATE (self);
205 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000206 return priv->data;
207}
208
Richard Hughes57d18222017-01-10 16:02:59 +0000209/**
210 * fu_plugin_alloc_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100211 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000212 * @data_sz: the size to allocate
213 *
214 * Allocates the per-plugin allocated private data.
215 *
216 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
217 *
218 * Since: 0.8.0
219 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000220FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100221fu_plugin_alloc_data (FuPlugin *self, gsize data_sz)
Richard Hughescff38bc2016-12-12 12:03:37 +0000222{
Richard Hughes12724852018-09-04 13:53:44 +0100223 FuPluginPrivate *priv = GET_PRIVATE (self);
224 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000225 if (priv->data != NULL) {
226 g_critical ("fu_plugin_alloc_data() already used by plugin");
227 return priv->data;
228 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000229 priv->data = g_malloc0 (data_sz);
230 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000231}
232
Richard Hughes57d18222017-01-10 16:02:59 +0000233/**
234 * fu_plugin_get_usb_context:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100235 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000236 *
237 * Gets the shared USB context that all plugins can use.
238 *
239 * Returns: (transfer none): a #GUsbContext.
240 *
241 * Since: 0.8.0
242 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000243GUsbContext *
Richard Hughes12724852018-09-04 13:53:44 +0100244fu_plugin_get_usb_context (FuPlugin *self)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000245{
Richard Hughes12724852018-09-04 13:53:44 +0100246 FuPluginPrivate *priv = GET_PRIVATE (self);
247 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000248 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000249}
250
251void
Richard Hughes12724852018-09-04 13:53:44 +0100252fu_plugin_set_usb_context (FuPlugin *self, GUsbContext *usb_ctx)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000253{
Richard Hughes12724852018-09-04 13:53:44 +0100254 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000255 g_set_object (&priv->usb_ctx, usb_ctx);
256}
257
Richard Hughes57d18222017-01-10 16:02:59 +0000258/**
259 * fu_plugin_get_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100260 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000261 *
Richard Hughes4eada342017-10-03 21:20:32 +0100262 * Returns if the plugin is enabled. Plugins may self-disable using
263 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000264 *
265 * Returns: %TRUE if the plugin is currently enabled.
266 *
267 * Since: 0.8.0
268 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000269gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100270fu_plugin_get_enabled (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000271{
Richard Hughes12724852018-09-04 13:53:44 +0100272 FuPluginPrivate *priv = GET_PRIVATE (self);
273 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000274 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000275}
276
Richard Hughes57d18222017-01-10 16:02:59 +0000277/**
278 * fu_plugin_set_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100279 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000280 * @enabled: the enabled value
281 *
282 * Enables or disables a plugin. Plugins can self-disable at any point.
283 *
284 * Since: 0.8.0
285 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000286void
Richard Hughes12724852018-09-04 13:53:44 +0100287fu_plugin_set_enabled (FuPlugin *self, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000288{
Richard Hughes12724852018-09-04 13:53:44 +0100289 FuPluginPrivate *priv = GET_PRIVATE (self);
290 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000291 priv->enabled = enabled;
292}
293
Richard Hughes1e456bc2018-05-10 20:16:16 +0100294gchar *
295fu_plugin_guess_name_from_fn (const gchar *filename)
296{
297 const gchar *prefix = "libfu_plugin_";
298 gchar *name;
299 gchar *str = g_strstr_len (filename, -1, prefix);
300 if (str == NULL)
301 return NULL;
302 name = g_strdup (str + strlen (prefix));
303 g_strdelimit (name, ".", '\0');
304 return name;
305}
306
Richard Hughescff38bc2016-12-12 12:03:37 +0000307gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100308fu_plugin_open (FuPlugin *self, const gchar *filename, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +0000309{
Richard Hughes12724852018-09-04 13:53:44 +0100310 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000311 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000312
313 priv->module = g_module_open (filename, 0);
314 if (priv->module == NULL) {
315 g_set_error (error,
316 G_IO_ERROR,
317 G_IO_ERROR_FAILED,
318 "failed to open plugin: %s",
319 g_module_error ());
320 return FALSE;
321 }
322
323 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100324 if (priv->name == NULL)
325 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000326
327 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000328 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
329 if (func != NULL) {
330 g_debug ("performing init() on %s", filename);
Richard Hughes12724852018-09-04 13:53:44 +0100331 func (self);
Richard Hughesd0905142016-03-13 09:46:49 +0000332 }
333
Richard Hughescff38bc2016-12-12 12:03:37 +0000334 return TRUE;
335}
336
Richard Hughes57d18222017-01-10 16:02:59 +0000337/**
338 * fu_plugin_device_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100339 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000340 * @device: A #FuDevice
341 *
342 * Asks the daemon to add a device to the exported list. If this device ID
343 * has already been added by a different plugin then this request will be
344 * ignored.
345 *
346 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
347 * actually flashing an image to the hardware so that higher-priority plugins
348 * can add the device themselves.
349 *
350 * Since: 0.8.0
351 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000352void
Richard Hughes12724852018-09-04 13:53:44 +0100353fu_plugin_device_add (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000354{
Richard Hughes5e447292018-04-27 14:25:54 +0100355 GPtrArray *children;
Richard Hughesc125ec02018-09-05 19:35:17 +0100356 g_autoptr(GError) error = NULL;
Richard Hughes5e447292018-04-27 14:25:54 +0100357
Richard Hughes12724852018-09-04 13:53:44 +0100358 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000359 g_return_if_fail (FU_IS_DEVICE (device));
360
Richard Hughesc125ec02018-09-05 19:35:17 +0100361 /* ensure the device ID is set from the physical and logical IDs */
362 if (!fu_device_ensure_id (device, &error)) {
363 g_warning ("ignoring add: %s", error->message);
364 return;
365 }
366
Richard Hughescff38bc2016-12-12 12:03:37 +0000367 g_debug ("emit added from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100368 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000369 fu_device_get_id (device));
370 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
Richard Hughes12724852018-09-04 13:53:44 +0100371 fu_device_set_plugin (device, fu_plugin_get_name (self));
372 g_signal_emit (self, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100373
Richard Hughes128c0162018-08-10 11:00:29 +0100374 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100375 children = fu_device_get_children (device);
376 for (guint i = 0; i < children->len; i++) {
377 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100378 if (fu_device_get_created (child) == 0)
Richard Hughes12724852018-09-04 13:53:44 +0100379 fu_plugin_device_add (self, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100380 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000381}
382
Richard Hughese1fd34d2017-08-24 14:19:51 +0100383/**
384 * fu_plugin_device_register:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100385 * @self: A #FuPlugin
Richard Hughese1fd34d2017-08-24 14:19:51 +0100386 * @device: A #FuDevice
387 *
388 * Registers the device with other plugins so they can set metadata.
389 *
390 * Plugins do not have to call this manually as this is done automatically
391 * when using fu_plugin_device_add(). They may wish to use this manually
392 * if for intance the coldplug should be ignored based on the metadata
393 * set from other plugins.
394 *
395 * Since: 0.9.7
396 **/
397void
Richard Hughes12724852018-09-04 13:53:44 +0100398fu_plugin_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +0100399{
Richard Hughesc125ec02018-09-05 19:35:17 +0100400 g_autoptr(GError) error = NULL;
401
Richard Hughes12724852018-09-04 13:53:44 +0100402 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughese1fd34d2017-08-24 14:19:51 +0100403 g_return_if_fail (FU_IS_DEVICE (device));
404
Richard Hughesc125ec02018-09-05 19:35:17 +0100405 /* ensure the device ID is set from the physical and logical IDs */
406 if (!fu_device_ensure_id (device, &error)) {
407 g_warning ("ignoring registration: %s", error->message);
408 return;
409 }
410
Richard Hughese1fd34d2017-08-24 14:19:51 +0100411 g_debug ("emit device-register from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100412 fu_plugin_get_name (self),
Richard Hughese1fd34d2017-08-24 14:19:51 +0100413 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100414 g_signal_emit (self, signals[SIGNAL_DEVICE_REGISTER], 0, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +0100415}
416
Richard Hughes57d18222017-01-10 16:02:59 +0000417/**
Richard Hughes4eada342017-10-03 21:20:32 +0100418 * fu_plugin_device_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100419 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000420 * @device: A #FuDevice
421 *
422 * Asks the daemon to remove a device from the exported list.
423 *
424 * Since: 0.8.0
425 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000426void
Richard Hughes12724852018-09-04 13:53:44 +0100427fu_plugin_device_remove (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000428{
Richard Hughes12724852018-09-04 13:53:44 +0100429 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000430 g_return_if_fail (FU_IS_DEVICE (device));
431
Richard Hughescff38bc2016-12-12 12:03:37 +0000432 g_debug ("emit removed from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100433 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000434 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100435 g_signal_emit (self, signals[SIGNAL_DEVICE_REMOVED], 0, device);
Richard Hughescff38bc2016-12-12 12:03:37 +0000436}
437
Richard Hughes57d18222017-01-10 16:02:59 +0000438/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000439 * fu_plugin_request_recoldplug:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100440 * @self: A #FuPlugin
Richard Hughes362d6d72017-01-07 21:42:14 +0000441 *
442 * Ask all the plugins to coldplug all devices, which will include the prepare()
443 * and cleanup() phases. Duplicate devices added will be ignored.
444 *
445 * Since: 0.8.0
446 **/
447void
Richard Hughes12724852018-09-04 13:53:44 +0100448fu_plugin_request_recoldplug (FuPlugin *self)
Richard Hughes362d6d72017-01-07 21:42:14 +0000449{
Richard Hughes12724852018-09-04 13:53:44 +0100450 g_return_if_fail (FU_IS_PLUGIN (self));
451 g_signal_emit (self, signals[SIGNAL_RECOLDPLUG], 0);
Richard Hughes362d6d72017-01-07 21:42:14 +0000452}
453
Richard Hughesb0829032017-01-10 09:27:08 +0000454/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100455 * fu_plugin_check_hwid:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100456 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100457 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100458 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100459 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100460 * specific system can be shown using the `fwupdmgr hwids` command.
461 *
Richard Hughes4eada342017-10-03 21:20:32 +0100462 * Returns: %TRUE if the HwId is found on the system.
463 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100464 * Since: 0.9.1
465 **/
466gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100467fu_plugin_check_hwid (FuPlugin *self, const gchar *hwid)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100468{
Richard Hughes12724852018-09-04 13:53:44 +0100469 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100470 if (priv->hwids == NULL)
471 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100472 return fu_hwids_has_guid (priv->hwids, hwid);
473}
474
475/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100476 * fu_plugin_get_hwids:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100477 * @self: A #FuPlugin
Richard Hughes69a5f352018-08-08 11:58:15 +0100478 *
479 * Returns all the HWIDs defined in the system. All hardware IDs on a
480 * specific system can be shown using the `fwupdmgr hwids` command.
481 *
482 * Returns: (transfer none) (element-type utf-8): An array of GUIDs
483 *
484 * Since: 1.1.1
485 **/
486GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +0100487fu_plugin_get_hwids (FuPlugin *self)
Richard Hughes69a5f352018-08-08 11:58:15 +0100488{
Richard Hughes12724852018-09-04 13:53:44 +0100489 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes69a5f352018-08-08 11:58:15 +0100490 if (priv->hwids == NULL)
491 return NULL;
492 return fu_hwids_get_guids (priv->hwids);
493}
494
495/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100496 * fu_plugin_check_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100497 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100498 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100499 *
500 * Checks to see if a specific device GUID is supported, i.e. available in the
501 * AppStream metadata.
502 *
Richard Hughes4eada342017-10-03 21:20:32 +0100503 * Returns: %TRUE if the device is supported.
504 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100505 * Since: 1.0.0
506 **/
507gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100508fu_plugin_check_supported (FuPlugin *self, const gchar *guid)
Richard Hughes1354ea92017-09-19 15:58:31 +0100509{
Richard Hughes12724852018-09-04 13:53:44 +0100510 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes1354ea92017-09-19 15:58:31 +0100511 if (priv->supported_guids == NULL)
512 return FALSE;
513 for (guint i = 0; i < priv->supported_guids->len; i++) {
514 const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i);
515 if (g_strcmp0 (guid, guid_tmp) == 0)
516 return TRUE;
517 }
518 return FALSE;
519}
520
521/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100522 * fu_plugin_get_dmi_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100523 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100524 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100525 *
526 * Gets a hardware DMI value.
527 *
Richard Hughes4eada342017-10-03 21:20:32 +0100528 * Returns: The string, or %NULL
529 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100530 * Since: 0.9.7
531 **/
532const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100533fu_plugin_get_dmi_value (FuPlugin *self, const gchar *dmi_id)
Richard Hughesd7704d42017-08-08 20:29:09 +0100534{
Richard Hughes12724852018-09-04 13:53:44 +0100535 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100536 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100537 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100538 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100539}
540
Richard Hughes49e5e052017-09-03 12:15:41 +0100541/**
542 * fu_plugin_get_smbios_string:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100543 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100544 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
545 * @offset: A SMBIOS offset
546 *
547 * Gets a hardware SMBIOS string.
548 *
549 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
550 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
551 *
Richard Hughes4eada342017-10-03 21:20:32 +0100552 * Returns: A string, or %NULL
553 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100554 * Since: 0.9.8
555 **/
556const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100557fu_plugin_get_smbios_string (FuPlugin *self, guint8 structure_type, guint8 offset)
Richard Hughes49e5e052017-09-03 12:15:41 +0100558{
Richard Hughes12724852018-09-04 13:53:44 +0100559 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100560 if (priv->smbios == NULL)
561 return NULL;
562 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
563}
564
565/**
566 * fu_plugin_get_smbios_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100567 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100568 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
569 *
570 * Gets a hardware SMBIOS data.
571 *
Richard Hughes4eada342017-10-03 21:20:32 +0100572 * Returns: (transfer none): A #GBytes, or %NULL
573 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100574 * Since: 0.9.8
575 **/
576GBytes *
Richard Hughes12724852018-09-04 13:53:44 +0100577fu_plugin_get_smbios_data (FuPlugin *self, guint8 structure_type)
Richard Hughes49e5e052017-09-03 12:15:41 +0100578{
Richard Hughes12724852018-09-04 13:53:44 +0100579 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100580 if (priv->smbios == NULL)
581 return NULL;
582 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
583}
584
Richard Hughesb8f8db22017-04-25 15:56:00 +0100585void
Richard Hughes12724852018-09-04 13:53:44 +0100586fu_plugin_set_hwids (FuPlugin *self, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100587{
Richard Hughes12724852018-09-04 13:53:44 +0100588 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100589 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100590}
591
Richard Hughes49e5e052017-09-03 12:15:41 +0100592void
Richard Hughes12724852018-09-04 13:53:44 +0100593fu_plugin_set_supported (FuPlugin *self, GPtrArray *supported_guids)
Richard Hughes1354ea92017-09-19 15:58:31 +0100594{
Richard Hughes12724852018-09-04 13:53:44 +0100595 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes1354ea92017-09-19 15:58:31 +0100596 if (priv->supported_guids != NULL)
597 g_ptr_array_unref (priv->supported_guids);
598 priv->supported_guids = g_ptr_array_ref (supported_guids);
599}
600
Richard Hughes9c028f02017-10-28 21:14:28 +0100601void
Richard Hughes12724852018-09-04 13:53:44 +0100602fu_plugin_set_udev_subsystems (FuPlugin *self, GPtrArray *udev_subsystems)
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100603{
Richard Hughes12724852018-09-04 13:53:44 +0100604 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100605 if (priv->udev_subsystems != NULL)
606 g_ptr_array_unref (priv->udev_subsystems);
607 priv->udev_subsystems = g_ptr_array_ref (udev_subsystems);
608}
609
610void
Richard Hughes12724852018-09-04 13:53:44 +0100611fu_plugin_set_quirks (FuPlugin *self, FuQuirks *quirks)
Richard Hughes9c028f02017-10-28 21:14:28 +0100612{
Richard Hughes12724852018-09-04 13:53:44 +0100613 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100614 g_set_object (&priv->quirks, quirks);
615}
616
617/**
618 * fu_plugin_get_quirks:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100619 * @self: A #FuPlugin
Richard Hughes9c028f02017-10-28 21:14:28 +0100620 *
621 * Returns the hardware database object. This can be used to discover device
622 * quirks or other device-specific settings.
623 *
624 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
625 *
626 * Since: 1.0.1
627 **/
628FuQuirks *
Richard Hughes12724852018-09-04 13:53:44 +0100629fu_plugin_get_quirks (FuPlugin *self)
Richard Hughes9c028f02017-10-28 21:14:28 +0100630{
Richard Hughes12724852018-09-04 13:53:44 +0100631 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100632 return priv->quirks;
633}
634
Richard Hughes0eb123b2018-04-19 12:00:04 +0100635void
Richard Hughes12724852018-09-04 13:53:44 +0100636fu_plugin_set_runtime_versions (FuPlugin *self, GHashTable *runtime_versions)
Richard Hughes0eb123b2018-04-19 12:00:04 +0100637{
Richard Hughes12724852018-09-04 13:53:44 +0100638 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0eb123b2018-04-19 12:00:04 +0100639 priv->runtime_versions = g_hash_table_ref (runtime_versions);
640}
641
642/**
643 * fu_plugin_add_runtime_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100644 * @self: A #FuPlugin
Richard Hughes0eb123b2018-04-19 12:00:04 +0100645 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
646 * @version: A version string, e.g. "1.2.3"
647 *
648 * Sets a runtime version of a specific dependancy.
649 *
650 * Since: 1.0.7
651 **/
652void
Richard Hughes12724852018-09-04 13:53:44 +0100653fu_plugin_add_runtime_version (FuPlugin *self,
Richard Hughes0eb123b2018-04-19 12:00:04 +0100654 const gchar *component_id,
655 const gchar *version)
656{
Richard Hughes12724852018-09-04 13:53:44 +0100657 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb01b4862018-04-20 16:39:48 +0100658 if (priv->runtime_versions == NULL)
659 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100660 g_hash_table_insert (priv->runtime_versions,
661 g_strdup (component_id),
662 g_strdup (version));
663}
664
Richard Hughes34e0dab2018-04-20 16:43:00 +0100665void
Richard Hughes12724852018-09-04 13:53:44 +0100666fu_plugin_set_compile_versions (FuPlugin *self, GHashTable *compile_versions)
Richard Hughes34e0dab2018-04-20 16:43:00 +0100667{
Richard Hughes12724852018-09-04 13:53:44 +0100668 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100669 priv->compile_versions = g_hash_table_ref (compile_versions);
670}
671
672/**
673 * fu_plugin_add_compile_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100674 * @self: A #FuPlugin
Richard Hughes34e0dab2018-04-20 16:43:00 +0100675 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
676 * @version: A version string, e.g. "1.2.3"
677 *
678 * Sets a compile-time version of a specific dependancy.
679 *
680 * Since: 1.0.7
681 **/
682void
Richard Hughes12724852018-09-04 13:53:44 +0100683fu_plugin_add_compile_version (FuPlugin *self,
Richard Hughes34e0dab2018-04-20 16:43:00 +0100684 const gchar *component_id,
685 const gchar *version)
686{
Richard Hughes12724852018-09-04 13:53:44 +0100687 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100688 if (priv->compile_versions == NULL)
689 return;
690 g_hash_table_insert (priv->compile_versions,
691 g_strdup (component_id),
692 g_strdup (version));
693}
694
Richard Hughes9c028f02017-10-28 21:14:28 +0100695/**
696 * fu_plugin_lookup_quirk_by_id:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100697 * @self: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100698 * @group: A string, e.g. "DfuFlags"
699 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100700 *
701 * Looks up an entry in the hardware database using a string value.
702 *
703 * Returns: (transfer none): values from the database, or %NULL if not found
704 *
705 * Since: 1.0.1
706 **/
707const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100708fu_plugin_lookup_quirk_by_id (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100709{
Richard Hughes12724852018-09-04 13:53:44 +0100710 FuPluginPrivate *priv = GET_PRIVATE (self);
711 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes9c028f02017-10-28 21:14:28 +0100712
Richard Hughes9c028f02017-10-28 21:14:28 +0100713 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100714 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100715}
716
717/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100718 * fu_plugin_lookup_quirk_by_id_as_uint64:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100719 * @self: A #FuPlugin
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100720 * @group: A string, e.g. "DfuFlags"
721 * @key: An ID to match the entry, e.g. "Size"
722 *
723 * Looks up an entry in the hardware database using a string key, returning
724 * an integer value. Values are assumed base 10, unless prefixed with "0x"
725 * where they are parsed as base 16.
726 *
727 * Returns: (transfer none): value from the database, or 0 if not found
728 *
729 * Since: 1.1.2
730 **/
731guint64
Richard Hughes12724852018-09-04 13:53:44 +0100732fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100733{
Richard Hughes12724852018-09-04 13:53:44 +0100734 return fu_common_strtoull (fu_plugin_lookup_quirk_by_id (self, group, key));
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100735}
736
737/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100738 * fu_plugin_get_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100739 * @self: A #FuPlugin
Richard Hughes1354ea92017-09-19 15:58:31 +0100740 *
741 * Gets all the device GUIDs supported by the daemon.
742 *
Richard Hughes4eada342017-10-03 21:20:32 +0100743 * Returns: (element-type utf8) (transfer none): GUIDs
744 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100745 * Since: 1.0.0
746 **/
747GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +0100748fu_plugin_get_supported (FuPlugin *self)
Richard Hughes1354ea92017-09-19 15:58:31 +0100749{
Richard Hughes12724852018-09-04 13:53:44 +0100750 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes1354ea92017-09-19 15:58:31 +0100751 return priv->supported_guids;
752}
753
754void
Richard Hughes12724852018-09-04 13:53:44 +0100755fu_plugin_set_smbios (FuPlugin *self, FuSmbios *smbios)
Richard Hughes49e5e052017-09-03 12:15:41 +0100756{
Richard Hughes12724852018-09-04 13:53:44 +0100757 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100758 g_set_object (&priv->smbios, smbios);
759}
760
Richard Hughesb8f8db22017-04-25 15:56:00 +0100761/**
Richard Hughesb0829032017-01-10 09:27:08 +0000762 * fu_plugin_set_coldplug_delay:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100763 * @self: A #FuPlugin
Richard Hughesb0829032017-01-10 09:27:08 +0000764 * @duration: A delay in milliseconds
765 *
766 * Set the minimum time that should be waited inbetween the call to
767 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
768 * to be the minimum hardware initialisation time from a datasheet.
769 *
770 * It is better to use this function rather than using a sleep() in the plugin
771 * itself as then only one delay is done in the daemon rather than waiting for
772 * each coldplug prepare in a serial way.
773 *
774 * Additionally, very long delays should be avoided as the daemon will be
775 * blocked from processing requests whilst the coldplug delay is being
776 * performed.
777 *
778 * Since: 0.8.0
779 **/
780void
Richard Hughes12724852018-09-04 13:53:44 +0100781fu_plugin_set_coldplug_delay (FuPlugin *self, guint duration)
Richard Hughesb0829032017-01-10 09:27:08 +0000782{
Richard Hughes12724852018-09-04 13:53:44 +0100783 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesb0829032017-01-10 09:27:08 +0000784 g_return_if_fail (duration > 0);
785
786 /* check sanity */
787 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
788 g_warning ("duration of %ums is crazy, truncating to %ums",
789 duration,
790 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
791 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
792 }
793
794 /* emit */
Richard Hughes12724852018-09-04 13:53:44 +0100795 g_signal_emit (self, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
Richard Hughesb0829032017-01-10 09:27:08 +0000796}
797
Richard Hughesd0905142016-03-13 09:46:49 +0000798gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100799fu_plugin_runner_startup (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000800{
Richard Hughes12724852018-09-04 13:53:44 +0100801 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000802 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000803
804 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000805 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000806 return TRUE;
807
Richard Hughes639da472018-01-06 22:35:04 +0000808 /* no object loaded */
809 if (priv->module == NULL)
810 return TRUE;
811
Richard Hughesd0905142016-03-13 09:46:49 +0000812 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000813 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
814 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000815 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000816 g_debug ("performing startup() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100817 if (!func (self, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000818 g_prefix_error (error, "failed to startup %s: ", priv->name);
819 return FALSE;
820 }
821 return TRUE;
822}
823
824static gboolean
825fu_plugin_runner_offline_invalidate (GError **error)
826{
827 g_autoptr(GError) error_local = NULL;
828 g_autoptr(GFile) file1 = NULL;
829
830 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
831
832 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
833 if (!g_file_query_exists (file1, NULL))
834 return TRUE;
835 if (!g_file_delete (file1, NULL, &error_local)) {
836 g_set_error (error,
837 FWUPD_ERROR,
838 FWUPD_ERROR_INTERNAL,
839 "Cannot delete %s: %s",
840 FU_OFFLINE_TRIGGER_FILENAME,
841 error_local->message);
842 return FALSE;
843 }
844 return TRUE;
845}
846
847static gboolean
848fu_plugin_runner_offline_setup (GError **error)
849{
850 gint rc;
851
852 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
853
854 /* create symlink for the systemd-system-update-generator */
855 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
856 if (rc < 0) {
857 g_set_error (error,
858 FWUPD_ERROR,
859 FWUPD_ERROR_INTERNAL,
860 "Failed to create symlink %s to %s: %s",
861 FU_OFFLINE_TRIGGER_FILENAME,
862 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000863 return FALSE;
864 }
865 return TRUE;
866}
867
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000868static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100869fu_plugin_runner_device_generic (FuPlugin *self, FuDevice *device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000870 const gchar *symbol_name, GError **error)
871{
Richard Hughes12724852018-09-04 13:53:44 +0100872 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000873 FuPluginDeviceFunc func = NULL;
874
875 /* not enabled */
876 if (!priv->enabled)
877 return TRUE;
878
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000879 /* no object loaded */
880 if (priv->module == NULL)
881 return TRUE;
882
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000883 /* optional */
884 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
885 if (func == NULL)
886 return TRUE;
887 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100888 if (!func (self, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000889 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000890 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000891 priv->name);
892 return FALSE;
893 }
894 return TRUE;
895}
896
Richard Hughesdbd8c762018-06-15 20:31:40 +0100897static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100898fu_plugin_runner_flagged_device_generic (FuPlugin *self, FwupdInstallFlags flags,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500899 FuDevice *device,
900 const gchar *symbol_name, GError **error)
901{
Richard Hughes12724852018-09-04 13:53:44 +0100902 FuPluginPrivate *priv = GET_PRIVATE (self);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500903 FuPluginFlaggedDeviceFunc func = NULL;
904
905 /* not enabled */
906 if (!priv->enabled)
907 return TRUE;
908
909 /* no object loaded */
910 if (priv->module == NULL)
911 return TRUE;
912
913 /* optional */
914 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
915 if (func == NULL)
916 return TRUE;
917 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100918 if (!func (self, flags, device, error)) {
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500919 g_prefix_error (error, "failed to run %s() on %s: ",
920 symbol_name + 10,
921 priv->name);
922 return FALSE;
923 }
924 return TRUE;
925
926}
927
928static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100929fu_plugin_runner_device_array_generic (FuPlugin *self, GPtrArray *devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +0100930 const gchar *symbol_name, GError **error)
931{
Richard Hughes12724852018-09-04 13:53:44 +0100932 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesdbd8c762018-06-15 20:31:40 +0100933 FuPluginDeviceArrayFunc func = NULL;
934
935 /* not enabled */
936 if (!priv->enabled)
937 return TRUE;
938
939 /* no object loaded */
940 if (priv->module == NULL)
941 return TRUE;
942
943 /* optional */
944 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
945 if (func == NULL)
946 return TRUE;
947 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100948 if (!func (self, devices, error)) {
Richard Hughesdbd8c762018-06-15 20:31:40 +0100949 g_prefix_error (error, "failed to run %s() on %s: ",
950 symbol_name + 10,
951 priv->name);
952 return FALSE;
953 }
954 return TRUE;
955}
956
Richard Hughesd0905142016-03-13 09:46:49 +0000957gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100958fu_plugin_runner_coldplug (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000959{
Richard Hughes12724852018-09-04 13:53:44 +0100960 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000961 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000962
963 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000964 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000965 return TRUE;
966
Richard Hughes639da472018-01-06 22:35:04 +0000967 /* no object loaded */
968 if (priv->module == NULL)
969 return TRUE;
970
Richard Hughesd0905142016-03-13 09:46:49 +0000971 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000972 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
973 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000974 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000975 g_debug ("performing coldplug() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100976 if (!func (self, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000977 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
978 return FALSE;
979 }
980 return TRUE;
981}
982
Richard Hughes7b8b2022016-12-12 16:15:03 +0000983gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100984fu_plugin_runner_recoldplug (FuPlugin *self, GError **error)
Richard Hughes2de8f132018-01-17 09:12:02 +0000985{
Richard Hughes12724852018-09-04 13:53:44 +0100986 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes2de8f132018-01-17 09:12:02 +0000987 FuPluginStartupFunc func = NULL;
988
989 /* not enabled */
990 if (!priv->enabled)
991 return TRUE;
992
993 /* no object loaded */
994 if (priv->module == NULL)
995 return TRUE;
996
997 /* optional */
998 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
999 if (func == NULL)
1000 return TRUE;
1001 g_debug ("performing recoldplug() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001002 if (!func (self, error)) {
Richard Hughes2de8f132018-01-17 09:12:02 +00001003 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
1004 return FALSE;
1005 }
1006 return TRUE;
1007}
1008
1009gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001010fu_plugin_runner_coldplug_prepare (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001011{
Richard Hughes12724852018-09-04 13:53:44 +01001012 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001013 FuPluginStartupFunc func = NULL;
1014
1015 /* not enabled */
1016 if (!priv->enabled)
1017 return TRUE;
1018
Richard Hughes639da472018-01-06 22:35:04 +00001019 /* no object loaded */
1020 if (priv->module == NULL)
1021 return TRUE;
1022
Richard Hughes46487c92017-01-07 21:26:34 +00001023 /* optional */
1024 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1025 if (func == NULL)
1026 return TRUE;
1027 g_debug ("performing coldplug_prepare() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001028 if (!func (self, error)) {
Richard Hughes46487c92017-01-07 21:26:34 +00001029 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
1030 return FALSE;
1031 }
1032 return TRUE;
1033}
1034
1035gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001036fu_plugin_runner_coldplug_cleanup (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001037{
Richard Hughes12724852018-09-04 13:53:44 +01001038 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001039 FuPluginStartupFunc func = NULL;
1040
1041 /* not enabled */
1042 if (!priv->enabled)
1043 return TRUE;
1044
Richard Hughes639da472018-01-06 22:35:04 +00001045 /* no object loaded */
1046 if (priv->module == NULL)
1047 return TRUE;
1048
Richard Hughes46487c92017-01-07 21:26:34 +00001049 /* optional */
1050 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1051 if (func == NULL)
1052 return TRUE;
1053 g_debug ("performing coldplug_cleanup() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001054 if (!func (self, error)) {
Richard Hughes46487c92017-01-07 21:26:34 +00001055 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
1056 return FALSE;
1057 }
1058 return TRUE;
1059}
1060
1061gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001062fu_plugin_runner_composite_prepare (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001063{
Richard Hughes12724852018-09-04 13:53:44 +01001064 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001065 "fu_plugin_composite_prepare",
1066 error);
1067}
1068
1069gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001070fu_plugin_runner_composite_cleanup (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001071{
Richard Hughes12724852018-09-04 13:53:44 +01001072 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001073 "fu_plugin_composite_cleanup",
1074 error);
1075}
1076
1077gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001078fu_plugin_runner_update_prepare (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001079 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001080{
Richard Hughes12724852018-09-04 13:53:44 +01001081 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001082 "fu_plugin_update_prepare",
1083 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001084}
1085
1086gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001087fu_plugin_runner_update_cleanup (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001088 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001089{
Richard Hughes12724852018-09-04 13:53:44 +01001090 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001091 "fu_plugin_update_cleanup",
1092 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001093}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001094
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001095gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001096fu_plugin_runner_update_attach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001097{
Richard Hughes12724852018-09-04 13:53:44 +01001098 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001099 "fu_plugin_update_attach", error);
1100}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001101
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001102gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001103fu_plugin_runner_update_detach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001104{
Richard Hughes12724852018-09-04 13:53:44 +01001105 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001106 "fu_plugin_update_detach", error);
1107}
1108
1109gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001110fu_plugin_runner_update_reload (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001111{
Richard Hughes12724852018-09-04 13:53:44 +01001112 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001113 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001114}
1115
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001116/**
1117 * fu_plugin_add_udev_subsystem:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001118 * @self: a #FuPlugin
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001119 * @subsystem: a subsystem name, e.g. `pciport`
1120 *
1121 * Registers the udev subsystem to be watched by the daemon.
1122 *
1123 * Plugins can use this method only in fu_plugin_init()
1124 **/
1125void
Richard Hughes12724852018-09-04 13:53:44 +01001126fu_plugin_add_udev_subsystem (FuPlugin *self, const gchar *subsystem)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001127{
Richard Hughes12724852018-09-04 13:53:44 +01001128 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001129 for (guint i = 0; i < priv->udev_subsystems->len; i++) {
1130 const gchar *subsystem_tmp = g_ptr_array_index (priv->udev_subsystems, i);
1131 if (g_strcmp0 (subsystem_tmp, subsystem) == 0)
1132 return;
1133 }
1134 g_debug ("added udev subsystem watch of %s", subsystem);
1135 g_ptr_array_add (priv->udev_subsystems, g_strdup (subsystem));
1136}
1137
Richard Hughes104f6512017-11-24 11:44:57 +00001138gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001139fu_plugin_runner_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
Richard Hughes104f6512017-11-24 11:44:57 +00001140{
Richard Hughes12724852018-09-04 13:53:44 +01001141 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes104f6512017-11-24 11:44:57 +00001142 FuPluginUsbDeviceAddedFunc func = NULL;
1143
1144 /* not enabled */
1145 if (!priv->enabled)
1146 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001147
1148 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001149 if (priv->module == NULL)
1150 return TRUE;
1151
1152 /* optional */
1153 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1154 if (func != NULL) {
1155 g_debug ("performing usb_device_added() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001156 return func (self, device, error);
Richard Hughes104f6512017-11-24 11:44:57 +00001157 }
1158 return TRUE;
1159}
1160
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001161gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001162fu_plugin_runner_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001163{
Richard Hughes12724852018-09-04 13:53:44 +01001164 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001165 FuPluginUdevDeviceAddedFunc func = NULL;
1166
1167 /* not enabled */
1168 if (!priv->enabled)
1169 return TRUE;
1170
1171 /* no object loaded */
1172 if (priv->module == NULL)
1173 return TRUE;
1174
1175 /* optional */
1176 g_module_symbol (priv->module, "fu_plugin_udev_device_added", (gpointer *) &func);
1177 if (func != NULL) {
1178 g_debug ("performing udev_device_added() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001179 return func (self, device, error);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001180 }
1181 return TRUE;
1182}
1183
Richard Hughese1fd34d2017-08-24 14:19:51 +01001184void
Richard Hughes12724852018-09-04 13:53:44 +01001185fu_plugin_runner_device_removed (FuPlugin *self, FuDevice *device)
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001186{
1187 g_autoptr(GError) error_local= NULL;
1188
Richard Hughes12724852018-09-04 13:53:44 +01001189 if (!fu_plugin_runner_device_generic (self, device,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001190 "fu_plugin_device_removed",
1191 &error_local))
1192 g_warning ("%s", error_local->message);
1193}
1194
1195void
Richard Hughes12724852018-09-04 13:53:44 +01001196fu_plugin_runner_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +01001197{
Richard Hughes12724852018-09-04 13:53:44 +01001198 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001199 FuPluginDeviceRegisterFunc func = NULL;
1200
1201 /* not enabled */
1202 if (!priv->enabled)
1203 return;
Richard Hughes34834102017-11-21 21:55:00 +00001204 if (priv->module == NULL)
1205 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001206
Mario Limonciello4910b242018-06-22 15:04:21 -05001207 /* don't notify plugins on their own devices */
Richard Hughes12724852018-09-04 13:53:44 +01001208 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (self)) == 0)
Mario Limonciello4910b242018-06-22 15:04:21 -05001209 return;
1210
Richard Hughese1fd34d2017-08-24 14:19:51 +01001211 /* optional */
1212 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1213 if (func != NULL) {
Richard Hughes1bf7ff92018-08-24 20:21:35 +01001214 g_debug ("performing fu_plugin_device_registered() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001215 func (self, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001216 }
1217}
1218
Richard Hughescff38bc2016-12-12 12:03:37 +00001219static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001220fu_plugin_runner_schedule_update (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001221 FuDevice *device,
1222 GBytes *blob_cab,
1223 GError **error)
1224{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001225 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001226 gchar tmpname[] = {"XXXXXX.cap"};
1227 g_autofree gchar *dirname = NULL;
1228 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001229 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001230 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001231 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001232 g_autoptr(GFile) file = NULL;
1233
1234 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001235 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001236 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001237 if (res_tmp != NULL) {
1238 g_set_error (error,
1239 FWUPD_ERROR,
1240 FWUPD_ERROR_ALREADY_PENDING,
1241 "%s is already scheduled to be updated",
1242 fu_device_get_id (device));
1243 return FALSE;
1244 }
1245
1246 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001247 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001248 file = g_file_new_for_path (dirname);
1249 if (!g_file_query_exists (file, NULL)) {
1250 if (!g_file_make_directory_with_parents (file, NULL, error))
1251 return FALSE;
1252 }
1253
1254 /* get a random filename */
1255 for (guint i = 0; i < 6; i++)
1256 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1257 filename = g_build_filename (dirname, tmpname, NULL);
1258
1259 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001260 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001261 if (!g_file_set_contents (filename,
1262 g_bytes_get_data (blob_cab, NULL),
1263 (gssize) g_bytes_get_size (blob_cab),
1264 error))
1265 return FALSE;
1266
1267 /* schedule for next boot */
1268 g_debug ("schedule %s to be installed to %s on next boot",
1269 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001270 release = fu_device_get_release_default (device);
1271 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1272 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001273
1274 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001275 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001276 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001277 return FALSE;
1278
1279 /* next boot we run offline */
1280 return fu_plugin_runner_offline_setup (error);
1281}
1282
1283gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001284fu_plugin_runner_verify (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001285 FuDevice *device,
1286 FuPluginVerifyFlags flags,
1287 GError **error)
1288{
Richard Hughes12724852018-09-04 13:53:44 +01001289 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001290 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001291 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001292
1293 /* not enabled */
1294 if (!priv->enabled)
1295 return TRUE;
1296
Richard Hughes639da472018-01-06 22:35:04 +00001297 /* no object loaded */
1298 if (priv->module == NULL)
1299 return TRUE;
1300
Richard Hughesababbb72017-06-15 20:18:36 +01001301 /* clear any existing verification checksums */
1302 checksums = fu_device_get_checksums (device);
1303 g_ptr_array_set_size (checksums, 0);
1304
Richard Hughescff38bc2016-12-12 12:03:37 +00001305 /* optional */
1306 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1307 if (func == NULL)
1308 return TRUE;
1309 g_debug ("performing verify() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001310 if (!func (self, device, flags, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001311 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001312 return FALSE;
1313 }
1314 return TRUE;
1315}
1316
Richard Hughesd0905142016-03-13 09:46:49 +00001317gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001318fu_plugin_runner_unlock (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001319{
Richard Hughescff38bc2016-12-12 12:03:37 +00001320 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001321
1322 /* final check */
1323 flags = fu_device_get_flags (device);
1324 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1325 g_set_error (error,
1326 FWUPD_ERROR,
1327 FWUPD_ERROR_NOT_SUPPORTED,
1328 "Device %s is not locked",
1329 fu_device_get_id (device));
1330 return FALSE;
1331 }
1332
Richard Hughes9c4b5312017-11-14 11:34:53 +00001333 /* run vfunc */
Richard Hughes12724852018-09-04 13:53:44 +01001334 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes9c4b5312017-11-14 11:34:53 +00001335 "fu_plugin_unlock", error))
1336 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001337
1338 /* update with correct flags */
1339 flags = fu_device_get_flags (device);
1340 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1341 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1342 return TRUE;
1343}
1344
1345gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001346fu_plugin_runner_update (FuPlugin *self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001347 FuDevice *device,
1348 GBytes *blob_cab,
1349 GBytes *blob_fw,
1350 FwupdInstallFlags flags,
1351 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001352{
Richard Hughes12724852018-09-04 13:53:44 +01001353 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001354 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001355 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001356 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001357 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001358 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001359
1360 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001361 if (!priv->enabled) {
1362 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001363 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001364 }
Richard Hughesd0905142016-03-13 09:46:49 +00001365
Richard Hughes639da472018-01-06 22:35:04 +00001366 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001367 if (priv->module == NULL) {
1368 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001369 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001370 }
Richard Hughes639da472018-01-06 22:35:04 +00001371
Richard Hughesd0905142016-03-13 09:46:49 +00001372 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001373 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1374 if (update_func == NULL) {
1375 g_set_error_literal (error,
1376 FWUPD_ERROR,
1377 FWUPD_ERROR_NOT_SUPPORTED,
1378 "No update possible");
1379 return FALSE;
1380 }
Richard Hughesd0905142016-03-13 09:46:49 +00001381
Richard Hughesa785a1c2017-08-25 16:00:58 +01001382 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001383 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001384 if (blob_cab == NULL) {
1385 g_set_error_literal (error,
1386 FWUPD_ERROR,
1387 FWUPD_ERROR_NOT_SUPPORTED,
1388 "No cabinet archive to schedule");
1389 return FALSE;
1390 }
Richard Hughes12724852018-09-04 13:53:44 +01001391 return fu_plugin_runner_schedule_update (self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001392 device,
1393 blob_cab,
1394 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001395 }
1396
1397 /* cancel the pending action */
1398 if (!fu_plugin_runner_offline_invalidate (error))
1399 return FALSE;
1400
1401 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001402 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001403 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughes12724852018-09-04 13:53:44 +01001404 if (!update_func (self, device, blob_fw, flags, &error_update)) {
Richard Hughesc0cd0232018-01-31 15:02:00 +00001405 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001406 g_propagate_error (error, error_update);
1407 return FALSE;
1408 }
1409
Richard Hughesf556d372017-06-15 19:49:18 +01001410 /* no longer valid */
1411 checksums = fu_device_get_checksums (device);
1412 g_ptr_array_set_size (checksums, 0);
1413
Richard Hughescff38bc2016-12-12 12:03:37 +00001414 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001415 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001416 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001417 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001418
Richard Hughes780ef3f2018-01-12 16:20:31 +00001419 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001420 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1421 if (!fu_history_modify_device (history, device,
1422 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1423 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001424 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001425
1426 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001427 release = fu_device_get_release_default (device_pending);
1428 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001429 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1430 g_autoptr(GError) error_local = NULL;
1431 g_autoptr(GFile) file = NULL;
1432 file = g_file_new_for_path (tmp);
1433 if (!g_file_delete (file, NULL, &error_local)) {
1434 g_set_error (error,
1435 FWUPD_ERROR,
1436 FWUPD_ERROR_INVALID_FILE,
1437 "Failed to delete %s: %s",
1438 tmp, error_local->message);
1439 return FALSE;
1440 }
1441 }
1442 }
Richard Hughesd0905142016-03-13 09:46:49 +00001443 return TRUE;
1444}
Richard Hughescff38bc2016-12-12 12:03:37 +00001445
1446gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001447fu_plugin_runner_clear_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001448{
Richard Hughes12724852018-09-04 13:53:44 +01001449 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001450 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001451
1452 /* not enabled */
1453 if (!priv->enabled)
1454 return TRUE;
1455
Richard Hughes639da472018-01-06 22:35:04 +00001456 /* no object loaded */
1457 if (priv->module == NULL)
1458 return TRUE;
1459
Richard Hughes65e44ca2018-01-30 17:26:30 +00001460 /* optional */
1461 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1462 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001463 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001464 g_debug ("performing clear_result() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001465 if (!func (self, device, error)) {
Richard Hughes65e44ca2018-01-30 17:26:30 +00001466 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001467 return FALSE;
1468 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001469 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001470}
1471
1472gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001473fu_plugin_runner_get_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001474{
Richard Hughes12724852018-09-04 13:53:44 +01001475 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001476 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001477
1478 /* not enabled */
1479 if (!priv->enabled)
1480 return TRUE;
1481
Richard Hughes639da472018-01-06 22:35:04 +00001482 /* no object loaded */
1483 if (priv->module == NULL)
1484 return TRUE;
1485
Richard Hughes65e44ca2018-01-30 17:26:30 +00001486 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001487 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001488 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001489 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001490 g_debug ("performing get_results() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001491 if (!func (self, device, error)) {
Richard Hughes65e44ca2018-01-30 17:26:30 +00001492 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001493 return FALSE;
1494 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001495 return TRUE;
1496}
1497
Richard Hughes08a37992017-09-12 12:57:43 +01001498/**
1499 * fu_plugin_get_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001500 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001501 *
1502 * Gets the plugin order, where higher numbers are run after lower
1503 * numbers.
1504 *
1505 * Returns: the integer value
1506 **/
1507guint
Richard Hughes12724852018-09-04 13:53:44 +01001508fu_plugin_get_order (FuPlugin *self)
Richard Hughes08a37992017-09-12 12:57:43 +01001509{
Richard Hughes12724852018-09-04 13:53:44 +01001510 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001511 return priv->order;
1512}
1513
1514/**
1515 * fu_plugin_set_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001516 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001517 * @order: a integer value
1518 *
1519 * Sets the plugin order, where higher numbers are run after lower
1520 * numbers.
1521 **/
1522void
Richard Hughes12724852018-09-04 13:53:44 +01001523fu_plugin_set_order (FuPlugin *self, guint order)
Richard Hughes08a37992017-09-12 12:57:43 +01001524{
Richard Hughes12724852018-09-04 13:53:44 +01001525 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001526 priv->order = order;
1527}
1528
1529/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001530 * fu_plugin_get_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001531 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001532 *
1533 * Gets the plugin priority, where higher numbers are better.
1534 *
1535 * Returns: the integer value
1536 **/
1537guint
Richard Hughes12724852018-09-04 13:53:44 +01001538fu_plugin_get_priority (FuPlugin *self)
Richard Hughes81c427c2018-08-06 15:20:17 +01001539{
Richard Hughes12724852018-09-04 13:53:44 +01001540 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001541 return priv->priority;
1542}
1543
1544/**
1545 * fu_plugin_set_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001546 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001547 * @priority: a integer value
1548 *
1549 * Sets the plugin priority, where higher numbers are better.
1550 **/
1551void
Richard Hughes12724852018-09-04 13:53:44 +01001552fu_plugin_set_priority (FuPlugin *self, guint priority)
Richard Hughes81c427c2018-08-06 15:20:17 +01001553{
Richard Hughes12724852018-09-04 13:53:44 +01001554 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001555 priv->priority = priority;
1556}
1557
1558/**
Richard Hughes08a37992017-09-12 12:57:43 +01001559 * fu_plugin_add_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001560 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001561 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001562 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001563 *
1564 * If the plugin name is found, the rule will be used to sort the plugin list,
1565 * for example the plugin specified by @name will be ordered after this plugin
1566 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1567 *
1568 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1569 * If depsolving fails then fwupd will not start.
1570 **/
1571void
Richard Hughes12724852018-09-04 13:53:44 +01001572fu_plugin_add_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes08a37992017-09-12 12:57:43 +01001573{
Richard Hughes12724852018-09-04 13:53:44 +01001574 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001575 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1576}
1577
1578/**
1579 * fu_plugin_get_rules:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001580 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001581 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1582 *
1583 * Gets the plugin IDs that should be run after this plugin.
1584 *
1585 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1586 **/
1587GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +01001588fu_plugin_get_rules (FuPlugin *self, FuPluginRule rule)
Richard Hughes08a37992017-09-12 12:57:43 +01001589{
Richard Hughes12724852018-09-04 13:53:44 +01001590 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001591 return priv->rules[rule];
1592}
1593
Richard Hughes80b79bb2018-01-11 21:11:06 +00001594/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001595 * fu_plugin_has_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001596 * @self: a #FuPlugin
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001597 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1598 * @name: a plugin name, e.g. `upower`
1599 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001600 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001601 *
1602 * Returns: %TRUE if the name exists for the specific rule
1603 **/
1604gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001605fu_plugin_has_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001606{
Richard Hughes12724852018-09-04 13:53:44 +01001607 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001608 for (guint i = 0; i < priv->rules[rule]->len; i++) {
1609 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
1610 if (g_strcmp0 (tmp, name) == 0)
1611 return TRUE;
1612 }
1613 return FALSE;
1614}
1615
1616/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00001617 * fu_plugin_add_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001618 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00001619 * @key: a string, e.g. `FwupdateVersion`
1620 * @value: a string, e.g. `10`
1621 *
1622 * Sets any additional metadata to be included in the firmware report to aid
1623 * debugging problems.
1624 *
1625 * Any data included here will be sent to the metadata server after user
1626 * confirmation.
1627 **/
1628void
Richard Hughes12724852018-09-04 13:53:44 +01001629fu_plugin_add_report_metadata (FuPlugin *self, const gchar *key, const gchar *value)
Richard Hughes80b79bb2018-01-11 21:11:06 +00001630{
Richard Hughes12724852018-09-04 13:53:44 +01001631 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001632 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1633}
1634
1635/**
1636 * fu_plugin_get_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001637 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00001638 *
1639 * Returns the list of additional metadata to be added when filing a report.
1640 *
1641 * Returns: (transfer none): the map of report metadata
1642 **/
1643GHashTable *
Richard Hughes12724852018-09-04 13:53:44 +01001644fu_plugin_get_report_metadata (FuPlugin *self)
Richard Hughes80b79bb2018-01-11 21:11:06 +00001645{
Richard Hughes12724852018-09-04 13:53:44 +01001646 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001647 return priv->report_metadata;
1648}
1649
Mario Limonciello963dc422018-02-27 14:26:58 -06001650/**
1651 * fu_plugin_get_config_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001652 * @self: a #FuPlugin
Mario Limonciello963dc422018-02-27 14:26:58 -06001653 * @key: A settings key
1654 *
1655 * Return the value of a key if it's been configured
1656 *
1657 * Since: 1.0.6
1658 **/
1659gchar *
Richard Hughes12724852018-09-04 13:53:44 +01001660fu_plugin_get_config_value (FuPlugin *self, const gchar *key)
Mario Limonciello963dc422018-02-27 14:26:58 -06001661{
Richard Hughes4be17d12018-05-30 20:36:29 +01001662 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06001663 g_autofree gchar *conf_file = NULL;
1664 g_autofree gchar *conf_path = NULL;
1665 g_autoptr(GKeyFile) keyfile = NULL;
1666 const gchar *plugin_name;
1667
Richard Hughes4be17d12018-05-30 20:36:29 +01001668 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Richard Hughes12724852018-09-04 13:53:44 +01001669 plugin_name = fu_plugin_get_name (self);
Mario Limonciello963dc422018-02-27 14:26:58 -06001670 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01001671 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06001672 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1673 return NULL;
1674 keyfile = g_key_file_new ();
1675 if (!g_key_file_load_from_file (keyfile, conf_path,
1676 G_KEY_FILE_NONE, NULL))
1677 return NULL;
1678 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1679}
1680
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001681/**
1682 * fu_plugin_name_compare:
1683 * @plugin1: first #FuPlugin to compare.
1684 * @plugin2: second #FuPlugin to compare.
1685 *
1686 * Compares two plugins by their names.
1687 *
1688 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1689 **/
1690gint
1691fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1692{
1693 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1694 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1695 return g_strcmp0 (priv1->name, priv2->name);
1696}
1697
1698/**
1699 * fu_plugin_order_compare:
1700 * @plugin1: first #FuPlugin to compare.
1701 * @plugin2: second #FuPlugin to compare.
1702 *
1703 * Compares two plugins by their depsolved order.
1704 *
1705 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1706 **/
1707gint
1708fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1709{
1710 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1711 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1712 if (priv1->order < priv2->order)
1713 return -1;
1714 if (priv1->order > priv2->order)
1715 return 1;
1716 return 0;
1717}
1718
Richard Hughescff38bc2016-12-12 12:03:37 +00001719static void
1720fu_plugin_class_init (FuPluginClass *klass)
1721{
1722 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1723 object_class->finalize = fu_plugin_finalize;
1724 signals[SIGNAL_DEVICE_ADDED] =
1725 g_signal_new ("device-added",
1726 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1727 G_STRUCT_OFFSET (FuPluginClass, device_added),
1728 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1729 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1730 signals[SIGNAL_DEVICE_REMOVED] =
1731 g_signal_new ("device-removed",
1732 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1733 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1734 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1735 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001736 signals[SIGNAL_DEVICE_REGISTER] =
1737 g_signal_new ("device-register",
1738 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1739 G_STRUCT_OFFSET (FuPluginClass, device_register),
1740 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1741 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001742 signals[SIGNAL_RECOLDPLUG] =
1743 g_signal_new ("recoldplug",
1744 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1745 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1746 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1747 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001748 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1749 g_signal_new ("set-coldplug-delay",
1750 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1751 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1752 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1753 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001754}
1755
1756static void
Richard Hughes12724852018-09-04 13:53:44 +01001757fu_plugin_init (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +00001758{
Richard Hughes12724852018-09-04 13:53:44 +01001759 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001760 priv->enabled = TRUE;
1761 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1762 g_free, (GDestroyNotify) g_object_unref);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001763 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 +01001764 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1765 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001766}
1767
1768static void
1769fu_plugin_finalize (GObject *object)
1770{
Richard Hughes12724852018-09-04 13:53:44 +01001771 FuPlugin *self = FU_PLUGIN (object);
1772 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001773 FuPluginInitFunc func = NULL;
1774
1775 /* optional */
1776 if (priv->module != NULL) {
1777 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1778 if (func != NULL) {
1779 g_debug ("performing destroy() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001780 func (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001781 }
1782 }
1783
Richard Hughes08a37992017-09-12 12:57:43 +01001784 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1785 g_ptr_array_unref (priv->rules[i]);
1786
Richard Hughescff38bc2016-12-12 12:03:37 +00001787 if (priv->usb_ctx != NULL)
1788 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001789 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001790 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001791 if (priv->quirks != NULL)
1792 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001793 if (priv->supported_guids != NULL)
1794 g_ptr_array_unref (priv->supported_guids);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001795 if (priv->udev_subsystems != NULL)
1796 g_ptr_array_unref (priv->udev_subsystems);
Richard Hughes49e5e052017-09-03 12:15:41 +01001797 if (priv->smbios != NULL)
1798 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001799 if (priv->runtime_versions != NULL)
1800 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001801 if (priv->compile_versions != NULL)
1802 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00001803 g_hash_table_unref (priv->devices);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001804 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001805 g_free (priv->name);
1806 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05001807 /* Must happen as the last step to avoid prematurely
1808 * freeing memory held by the plugin */
1809#ifndef RUNNING_ON_VALGRIND
1810 if (priv->module != NULL)
1811 g_module_close (priv->module);
1812#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001813
1814 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1815}
1816
1817FuPlugin *
1818fu_plugin_new (void)
1819{
Richard Hughes12724852018-09-04 13:53:44 +01001820 return FU_PLUGIN (g_object_new (FU_TYPE_PLUGIN, NULL));
Richard Hughescff38bc2016-12-12 12:03:37 +00001821}