blob: 882918b83f4a222e4e745772196740e4cc65b9c0 [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>
Richard Hughescff38bc2016-12-12 12:03:37 +000013#include <errno.h>
14#include <string.h>
Richard Hughes68f12dd2018-08-09 14:43:31 +010015#include <unistd.h>
Richard Hughescff38bc2016-12-12 12:03:37 +000016#include <gio/gunixinputstream.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060017#ifdef HAVE_VALGRIND
Richard Hughes576c0122017-02-24 09:47:00 +000018#include <valgrind.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060019#endif /* HAVE_VALGRIND */
Richard Hughesd0905142016-03-13 09:46:49 +000020
Richard Hughes9dde04f2017-09-13 12:07:15 +010021#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000022#include "fu-plugin-private.h"
Richard Hughesbc3a4e12018-01-06 22:41:47 +000023#include "fu-history.h"
Richard Hughes37d09432018-09-09 10:39:45 +010024#include "fu-mutex.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 Hughes9d6e0e72018-08-24 20:20:17 +010051 GPtrArray *udev_subsystems;
Richard Hughes1354ea92017-09-19 15:58:31 +010052 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000053 GHashTable *devices; /* platform_id:GObject */
Richard Hughes37d09432018-09-09 10:39:45 +010054 FuMutex *devices_mutex;
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 Hughes75b965d2018-11-15 13:51:21 +000063 SIGNAL_RULES_CHANGED,
Richard Hughes362d6d72017-01-07 21:42:14 +000064 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000065 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughesaabdc372018-11-14 10:11:08 +000066 SIGNAL_CHECK_SUPPORTED,
Richard Hughescff38bc2016-12-12 12:03:37 +000067 SIGNAL_LAST
68};
69
70static guint signals[SIGNAL_LAST] = { 0 };
71
72G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
73#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
74
75typedef const gchar *(*FuPluginGetNameFunc) (void);
Richard Hughes12724852018-09-04 13:53:44 +010076typedef void (*FuPluginInitFunc) (FuPlugin *self);
77typedef gboolean (*FuPluginStartupFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000078 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010079typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *self,
Richard Hughese1fd34d2017-08-24 14:19:51 +010080 FuDevice *device);
Richard Hughes12724852018-09-04 13:53:44 +010081typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000082 FuDevice *device,
83 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010084typedef gboolean (*FuPluginFlaggedDeviceFunc) (FuPlugin *self,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -050085 FwupdInstallFlags flags,
86 FuDevice *device,
87 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010088typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *self,
Richard Hughesdbd8c762018-06-15 20:31:40 +010089 GPtrArray *devices,
90 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010091typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000092 FuDevice *device,
93 FuPluginVerifyFlags flags,
94 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010095typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000096 FuDevice *device,
97 GBytes *blob_fw,
98 FwupdInstallFlags flags,
99 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100100typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100101 FuUsbDevice *device,
Richard Hughes104f6512017-11-24 11:44:57 +0000102 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100103typedef gboolean (*FuPluginUdevDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100104 FuUdevDevice *device,
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100105 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +0000106
Richard Hughes57d18222017-01-10 16:02:59 +0000107/**
108 * fu_plugin_get_name:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100109 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000110 *
111 * Gets the plugin name.
112 *
113 * Returns: a plugin name, or %NULL for unknown.
114 *
115 * Since: 0.8.0
116 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000117const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100118fu_plugin_get_name (FuPlugin *self)
Richard Hughesd0905142016-03-13 09:46:49 +0000119{
Richard Hughes12724852018-09-04 13:53:44 +0100120 FuPluginPrivate *priv = GET_PRIVATE (self);
121 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000122 return priv->name;
123}
Richard Hughesd0905142016-03-13 09:46:49 +0000124
Richard Hughes34834102017-11-21 21:55:00 +0000125void
Richard Hughes12724852018-09-04 13:53:44 +0100126fu_plugin_set_name (FuPlugin *self, const gchar *name)
Richard Hughes34834102017-11-21 21:55:00 +0000127{
Richard Hughes12724852018-09-04 13:53:44 +0100128 FuPluginPrivate *priv = GET_PRIVATE (self);
129 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughes34834102017-11-21 21:55:00 +0000130 g_return_if_fail (name != NULL);
131 g_free (priv->name);
132 priv->name = g_strdup (name);
133}
134
Richard Hughes57d18222017-01-10 16:02:59 +0000135/**
136 * fu_plugin_cache_lookup:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100137 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000138 * @id: the key
139 *
140 * Finds an object in the per-plugin cache.
141 *
142 * Returns: (transfer none): a #GObject, or %NULL for unfound.
143 *
144 * Since: 0.8.0
145 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000146gpointer
Richard Hughes12724852018-09-04 13:53:44 +0100147fu_plugin_cache_lookup (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000148{
Richard Hughes12724852018-09-04 13:53:44 +0100149 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes37d09432018-09-09 10:39:45 +0100150 g_autoptr(FuMutexLocker) locker = fu_mutex_read_locker_new (priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100151 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughesccd78a92017-01-11 16:57:41 +0000152 g_return_val_if_fail (id != NULL, NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100153 g_return_val_if_fail (locker != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000154 return g_hash_table_lookup (priv->devices, id);
155}
Richard Hughesd0905142016-03-13 09:46:49 +0000156
Richard Hughes57d18222017-01-10 16:02:59 +0000157/**
158 * fu_plugin_cache_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100159 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000160 * @id: the key
161 * @dev: a #GObject, typically a #FuDevice
162 *
163 * Adds an object to the per-plugin cache.
164 *
165 * Since: 0.8.0
166 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000167void
Richard Hughes12724852018-09-04 13:53:44 +0100168fu_plugin_cache_add (FuPlugin *self, const gchar *id, gpointer dev)
Richard Hughescff38bc2016-12-12 12:03:37 +0000169{
Richard Hughes12724852018-09-04 13:53:44 +0100170 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes37d09432018-09-09 10:39:45 +0100171 g_autoptr(FuMutexLocker) locker = fu_mutex_write_locker_new (priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100172 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000173 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100174 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000175 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
176}
177
Richard Hughes57d18222017-01-10 16:02:59 +0000178/**
179 * fu_plugin_cache_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100180 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000181 * @id: the key
182 *
183 * Removes an object from the per-plugin cache.
184 *
185 * Since: 0.8.0
186 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000187void
Richard Hughes12724852018-09-04 13:53:44 +0100188fu_plugin_cache_remove (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000189{
Richard Hughes12724852018-09-04 13:53:44 +0100190 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes37d09432018-09-09 10:39:45 +0100191 g_autoptr(FuMutexLocker) locker = fu_mutex_write_locker_new (priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100192 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000193 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100194 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000195 g_hash_table_remove (priv->devices, id);
196}
197
Richard Hughes57d18222017-01-10 16:02:59 +0000198/**
199 * fu_plugin_get_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100200 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000201 *
Richard Hughes4eada342017-10-03 21:20:32 +0100202 * Gets the per-plugin allocated private data. This will return %NULL unless
203 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000204 *
Richard Hughes4eada342017-10-03 21:20:32 +0100205 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000206 *
207 * Since: 0.8.0
208 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000209FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100210fu_plugin_get_data (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000211{
Richard Hughes12724852018-09-04 13:53:44 +0100212 FuPluginPrivate *priv = GET_PRIVATE (self);
213 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000214 return priv->data;
215}
216
Richard Hughes57d18222017-01-10 16:02:59 +0000217/**
218 * fu_plugin_alloc_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100219 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000220 * @data_sz: the size to allocate
221 *
222 * Allocates the per-plugin allocated private data.
223 *
224 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
225 *
226 * Since: 0.8.0
227 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000228FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100229fu_plugin_alloc_data (FuPlugin *self, gsize data_sz)
Richard Hughescff38bc2016-12-12 12:03:37 +0000230{
Richard Hughes12724852018-09-04 13:53:44 +0100231 FuPluginPrivate *priv = GET_PRIVATE (self);
232 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000233 if (priv->data != NULL) {
234 g_critical ("fu_plugin_alloc_data() already used by plugin");
235 return priv->data;
236 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000237 priv->data = g_malloc0 (data_sz);
238 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000239}
240
Richard Hughes57d18222017-01-10 16:02:59 +0000241/**
242 * fu_plugin_get_usb_context:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100243 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000244 *
245 * Gets the shared USB context that all plugins can use.
246 *
247 * Returns: (transfer none): a #GUsbContext.
248 *
249 * Since: 0.8.0
250 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000251GUsbContext *
Richard Hughes12724852018-09-04 13:53:44 +0100252fu_plugin_get_usb_context (FuPlugin *self)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000253{
Richard Hughes12724852018-09-04 13:53:44 +0100254 FuPluginPrivate *priv = GET_PRIVATE (self);
255 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000256 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000257}
258
259void
Richard Hughes12724852018-09-04 13:53:44 +0100260fu_plugin_set_usb_context (FuPlugin *self, GUsbContext *usb_ctx)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000261{
Richard Hughes12724852018-09-04 13:53:44 +0100262 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000263 g_set_object (&priv->usb_ctx, usb_ctx);
264}
265
Richard Hughes57d18222017-01-10 16:02:59 +0000266/**
267 * fu_plugin_get_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100268 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000269 *
Richard Hughes4eada342017-10-03 21:20:32 +0100270 * Returns if the plugin is enabled. Plugins may self-disable using
271 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000272 *
273 * Returns: %TRUE if the plugin is currently enabled.
274 *
275 * Since: 0.8.0
276 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000277gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100278fu_plugin_get_enabled (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000279{
Richard Hughes12724852018-09-04 13:53:44 +0100280 FuPluginPrivate *priv = GET_PRIVATE (self);
281 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000282 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000283}
284
Richard Hughes57d18222017-01-10 16:02:59 +0000285/**
286 * fu_plugin_set_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100287 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000288 * @enabled: the enabled value
289 *
290 * Enables or disables a plugin. Plugins can self-disable at any point.
291 *
292 * Since: 0.8.0
293 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000294void
Richard Hughes12724852018-09-04 13:53:44 +0100295fu_plugin_set_enabled (FuPlugin *self, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000296{
Richard Hughes12724852018-09-04 13:53:44 +0100297 FuPluginPrivate *priv = GET_PRIVATE (self);
298 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000299 priv->enabled = enabled;
300}
301
Richard Hughes1e456bc2018-05-10 20:16:16 +0100302gchar *
303fu_plugin_guess_name_from_fn (const gchar *filename)
304{
305 const gchar *prefix = "libfu_plugin_";
306 gchar *name;
307 gchar *str = g_strstr_len (filename, -1, prefix);
308 if (str == NULL)
309 return NULL;
310 name = g_strdup (str + strlen (prefix));
311 g_strdelimit (name, ".", '\0');
312 return name;
313}
314
Richard Hughescff38bc2016-12-12 12:03:37 +0000315gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100316fu_plugin_open (FuPlugin *self, const gchar *filename, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +0000317{
Richard Hughes12724852018-09-04 13:53:44 +0100318 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000319 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000320
321 priv->module = g_module_open (filename, 0);
322 if (priv->module == NULL) {
323 g_set_error (error,
324 G_IO_ERROR,
325 G_IO_ERROR_FAILED,
326 "failed to open plugin: %s",
327 g_module_error ());
328 return FALSE;
329 }
330
331 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100332 if (priv->name == NULL)
333 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000334
335 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000336 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
337 if (func != NULL) {
338 g_debug ("performing init() on %s", filename);
Richard Hughes12724852018-09-04 13:53:44 +0100339 func (self);
Richard Hughesd0905142016-03-13 09:46:49 +0000340 }
341
Richard Hughescff38bc2016-12-12 12:03:37 +0000342 return TRUE;
343}
344
Richard Hughes57d18222017-01-10 16:02:59 +0000345/**
346 * fu_plugin_device_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100347 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000348 * @device: A #FuDevice
349 *
350 * Asks the daemon to add a device to the exported list. If this device ID
351 * has already been added by a different plugin then this request will be
352 * ignored.
353 *
354 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
355 * actually flashing an image to the hardware so that higher-priority plugins
356 * can add the device themselves.
357 *
358 * Since: 0.8.0
359 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000360void
Richard Hughes12724852018-09-04 13:53:44 +0100361fu_plugin_device_add (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000362{
Richard Hughes5e447292018-04-27 14:25:54 +0100363 GPtrArray *children;
Richard Hughesc125ec02018-09-05 19:35:17 +0100364 g_autoptr(GError) error = NULL;
Richard Hughes5e447292018-04-27 14:25:54 +0100365
Richard Hughes12724852018-09-04 13:53:44 +0100366 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000367 g_return_if_fail (FU_IS_DEVICE (device));
368
Richard Hughesc125ec02018-09-05 19:35:17 +0100369 /* ensure the device ID is set from the physical and logical IDs */
370 if (!fu_device_ensure_id (device, &error)) {
371 g_warning ("ignoring add: %s", error->message);
372 return;
373 }
374
Richard Hughescff38bc2016-12-12 12:03:37 +0000375 g_debug ("emit added from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100376 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000377 fu_device_get_id (device));
378 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
Richard Hughes12724852018-09-04 13:53:44 +0100379 fu_device_set_plugin (device, fu_plugin_get_name (self));
380 g_signal_emit (self, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100381
Richard Hughes128c0162018-08-10 11:00:29 +0100382 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100383 children = fu_device_get_children (device);
384 for (guint i = 0; i < children->len; i++) {
385 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100386 if (fu_device_get_created (child) == 0)
Richard Hughes12724852018-09-04 13:53:44 +0100387 fu_plugin_device_add (self, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100388 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000389}
390
Richard Hughese1fd34d2017-08-24 14:19:51 +0100391/**
392 * fu_plugin_device_register:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100393 * @self: A #FuPlugin
Richard Hughese1fd34d2017-08-24 14:19:51 +0100394 * @device: A #FuDevice
395 *
396 * Registers the device with other plugins so they can set metadata.
397 *
398 * Plugins do not have to call this manually as this is done automatically
399 * when using fu_plugin_device_add(). They may wish to use this manually
400 * if for intance the coldplug should be ignored based on the metadata
401 * set from other plugins.
402 *
403 * Since: 0.9.7
404 **/
405void
Richard Hughes12724852018-09-04 13:53:44 +0100406fu_plugin_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +0100407{
Richard Hughesc125ec02018-09-05 19:35:17 +0100408 g_autoptr(GError) error = NULL;
409
Richard Hughes12724852018-09-04 13:53:44 +0100410 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughese1fd34d2017-08-24 14:19:51 +0100411 g_return_if_fail (FU_IS_DEVICE (device));
412
Richard Hughesc125ec02018-09-05 19:35:17 +0100413 /* ensure the device ID is set from the physical and logical IDs */
414 if (!fu_device_ensure_id (device, &error)) {
415 g_warning ("ignoring registration: %s", error->message);
416 return;
417 }
418
Richard Hughese1fd34d2017-08-24 14:19:51 +0100419 g_debug ("emit device-register from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100420 fu_plugin_get_name (self),
Richard Hughese1fd34d2017-08-24 14:19:51 +0100421 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100422 g_signal_emit (self, signals[SIGNAL_DEVICE_REGISTER], 0, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +0100423}
424
Richard Hughes57d18222017-01-10 16:02:59 +0000425/**
Richard Hughes4eada342017-10-03 21:20:32 +0100426 * fu_plugin_device_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100427 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000428 * @device: A #FuDevice
429 *
430 * Asks the daemon to remove a device from the exported list.
431 *
432 * Since: 0.8.0
433 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000434void
Richard Hughes12724852018-09-04 13:53:44 +0100435fu_plugin_device_remove (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000436{
Richard Hughes12724852018-09-04 13:53:44 +0100437 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000438 g_return_if_fail (FU_IS_DEVICE (device));
439
Richard Hughescff38bc2016-12-12 12:03:37 +0000440 g_debug ("emit removed from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100441 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000442 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100443 g_signal_emit (self, signals[SIGNAL_DEVICE_REMOVED], 0, device);
Richard Hughescff38bc2016-12-12 12:03:37 +0000444}
445
Richard Hughes57d18222017-01-10 16:02:59 +0000446/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000447 * fu_plugin_request_recoldplug:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100448 * @self: A #FuPlugin
Richard Hughes362d6d72017-01-07 21:42:14 +0000449 *
450 * Ask all the plugins to coldplug all devices, which will include the prepare()
451 * and cleanup() phases. Duplicate devices added will be ignored.
452 *
453 * Since: 0.8.0
454 **/
455void
Richard Hughes12724852018-09-04 13:53:44 +0100456fu_plugin_request_recoldplug (FuPlugin *self)
Richard Hughes362d6d72017-01-07 21:42:14 +0000457{
Richard Hughes12724852018-09-04 13:53:44 +0100458 g_return_if_fail (FU_IS_PLUGIN (self));
459 g_signal_emit (self, signals[SIGNAL_RECOLDPLUG], 0);
Richard Hughes362d6d72017-01-07 21:42:14 +0000460}
461
Richard Hughesb0829032017-01-10 09:27:08 +0000462/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100463 * fu_plugin_check_hwid:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100464 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100465 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100466 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100467 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100468 * specific system can be shown using the `fwupdmgr hwids` command.
469 *
Richard Hughes4eada342017-10-03 21:20:32 +0100470 * Returns: %TRUE if the HwId is found on the system.
471 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100472 * Since: 0.9.1
473 **/
474gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100475fu_plugin_check_hwid (FuPlugin *self, const gchar *hwid)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100476{
Richard Hughes12724852018-09-04 13:53:44 +0100477 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100478 if (priv->hwids == NULL)
479 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100480 return fu_hwids_has_guid (priv->hwids, hwid);
481}
482
483/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100484 * fu_plugin_get_hwids:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100485 * @self: A #FuPlugin
Richard Hughes69a5f352018-08-08 11:58:15 +0100486 *
487 * Returns all the HWIDs defined in the system. All hardware IDs on a
488 * specific system can be shown using the `fwupdmgr hwids` command.
489 *
490 * Returns: (transfer none) (element-type utf-8): An array of GUIDs
491 *
492 * Since: 1.1.1
493 **/
494GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +0100495fu_plugin_get_hwids (FuPlugin *self)
Richard Hughes69a5f352018-08-08 11:58:15 +0100496{
Richard Hughes12724852018-09-04 13:53:44 +0100497 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes69a5f352018-08-08 11:58:15 +0100498 if (priv->hwids == NULL)
499 return NULL;
500 return fu_hwids_get_guids (priv->hwids);
501}
502
503/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100504 * fu_plugin_check_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100505 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100506 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100507 *
508 * Checks to see if a specific device GUID is supported, i.e. available in the
509 * AppStream metadata.
510 *
Richard Hughes4eada342017-10-03 21:20:32 +0100511 * Returns: %TRUE if the device is supported.
512 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100513 * Since: 1.0.0
514 **/
515gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100516fu_plugin_check_supported (FuPlugin *self, const gchar *guid)
Richard Hughes1354ea92017-09-19 15:58:31 +0100517{
Richard Hughesaabdc372018-11-14 10:11:08 +0000518 gboolean retval = FALSE;
519 g_signal_emit (self, signals[SIGNAL_CHECK_SUPPORTED], 0, guid, &retval);
520 return retval;
Richard Hughes1354ea92017-09-19 15:58:31 +0100521}
522
523/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100524 * fu_plugin_get_dmi_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100525 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100526 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100527 *
528 * Gets a hardware DMI value.
529 *
Richard Hughes4eada342017-10-03 21:20:32 +0100530 * Returns: The string, or %NULL
531 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100532 * Since: 0.9.7
533 **/
534const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100535fu_plugin_get_dmi_value (FuPlugin *self, const gchar *dmi_id)
Richard Hughesd7704d42017-08-08 20:29:09 +0100536{
Richard Hughes12724852018-09-04 13:53:44 +0100537 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100538 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100539 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100540 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100541}
542
Richard Hughes49e5e052017-09-03 12:15:41 +0100543/**
544 * fu_plugin_get_smbios_string:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100545 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100546 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
547 * @offset: A SMBIOS offset
548 *
549 * Gets a hardware SMBIOS string.
550 *
551 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
552 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
553 *
Richard Hughes4eada342017-10-03 21:20:32 +0100554 * Returns: A string, or %NULL
555 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100556 * Since: 0.9.8
557 **/
558const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100559fu_plugin_get_smbios_string (FuPlugin *self, guint8 structure_type, guint8 offset)
Richard Hughes49e5e052017-09-03 12:15:41 +0100560{
Richard Hughes12724852018-09-04 13:53:44 +0100561 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100562 if (priv->smbios == NULL)
563 return NULL;
564 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
565}
566
567/**
568 * fu_plugin_get_smbios_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100569 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100570 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
571 *
572 * Gets a hardware SMBIOS data.
573 *
Richard Hughes4eada342017-10-03 21:20:32 +0100574 * Returns: (transfer none): A #GBytes, or %NULL
575 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100576 * Since: 0.9.8
577 **/
578GBytes *
Richard Hughes12724852018-09-04 13:53:44 +0100579fu_plugin_get_smbios_data (FuPlugin *self, guint8 structure_type)
Richard Hughes49e5e052017-09-03 12:15:41 +0100580{
Richard Hughes12724852018-09-04 13:53:44 +0100581 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100582 if (priv->smbios == NULL)
583 return NULL;
584 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
585}
586
Richard Hughesb8f8db22017-04-25 15:56:00 +0100587void
Richard Hughes12724852018-09-04 13:53:44 +0100588fu_plugin_set_hwids (FuPlugin *self, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100589{
Richard Hughes12724852018-09-04 13:53:44 +0100590 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100591 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100592}
593
Richard Hughes49e5e052017-09-03 12:15:41 +0100594void
Richard Hughes12724852018-09-04 13:53:44 +0100595fu_plugin_set_udev_subsystems (FuPlugin *self, GPtrArray *udev_subsystems)
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100596{
Richard Hughes12724852018-09-04 13:53:44 +0100597 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100598 if (priv->udev_subsystems != NULL)
599 g_ptr_array_unref (priv->udev_subsystems);
600 priv->udev_subsystems = g_ptr_array_ref (udev_subsystems);
601}
602
603void
Richard Hughes12724852018-09-04 13:53:44 +0100604fu_plugin_set_quirks (FuPlugin *self, FuQuirks *quirks)
Richard Hughes9c028f02017-10-28 21:14:28 +0100605{
Richard Hughes12724852018-09-04 13:53:44 +0100606 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100607 g_set_object (&priv->quirks, quirks);
608}
609
610/**
611 * fu_plugin_get_quirks:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100612 * @self: A #FuPlugin
Richard Hughes9c028f02017-10-28 21:14:28 +0100613 *
614 * Returns the hardware database object. This can be used to discover device
615 * quirks or other device-specific settings.
616 *
617 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
618 *
619 * Since: 1.0.1
620 **/
621FuQuirks *
Richard Hughes12724852018-09-04 13:53:44 +0100622fu_plugin_get_quirks (FuPlugin *self)
Richard Hughes9c028f02017-10-28 21:14:28 +0100623{
Richard Hughes12724852018-09-04 13:53:44 +0100624 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100625 return priv->quirks;
626}
627
Richard Hughes0eb123b2018-04-19 12:00:04 +0100628void
Richard Hughes12724852018-09-04 13:53:44 +0100629fu_plugin_set_runtime_versions (FuPlugin *self, GHashTable *runtime_versions)
Richard Hughes0eb123b2018-04-19 12:00:04 +0100630{
Richard Hughes12724852018-09-04 13:53:44 +0100631 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0eb123b2018-04-19 12:00:04 +0100632 priv->runtime_versions = g_hash_table_ref (runtime_versions);
633}
634
635/**
636 * fu_plugin_add_runtime_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100637 * @self: A #FuPlugin
Richard Hughes0eb123b2018-04-19 12:00:04 +0100638 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
639 * @version: A version string, e.g. "1.2.3"
640 *
641 * Sets a runtime version of a specific dependancy.
642 *
643 * Since: 1.0.7
644 **/
645void
Richard Hughes12724852018-09-04 13:53:44 +0100646fu_plugin_add_runtime_version (FuPlugin *self,
Richard Hughes0eb123b2018-04-19 12:00:04 +0100647 const gchar *component_id,
648 const gchar *version)
649{
Richard Hughes12724852018-09-04 13:53:44 +0100650 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb01b4862018-04-20 16:39:48 +0100651 if (priv->runtime_versions == NULL)
652 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100653 g_hash_table_insert (priv->runtime_versions,
654 g_strdup (component_id),
655 g_strdup (version));
656}
657
Richard Hughes34e0dab2018-04-20 16:43:00 +0100658void
Richard Hughes12724852018-09-04 13:53:44 +0100659fu_plugin_set_compile_versions (FuPlugin *self, GHashTable *compile_versions)
Richard Hughes34e0dab2018-04-20 16:43:00 +0100660{
Richard Hughes12724852018-09-04 13:53:44 +0100661 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100662 priv->compile_versions = g_hash_table_ref (compile_versions);
663}
664
665/**
666 * fu_plugin_add_compile_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100667 * @self: A #FuPlugin
Richard Hughes34e0dab2018-04-20 16:43:00 +0100668 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
669 * @version: A version string, e.g. "1.2.3"
670 *
671 * Sets a compile-time version of a specific dependancy.
672 *
673 * Since: 1.0.7
674 **/
675void
Richard Hughes12724852018-09-04 13:53:44 +0100676fu_plugin_add_compile_version (FuPlugin *self,
Richard Hughes34e0dab2018-04-20 16:43:00 +0100677 const gchar *component_id,
678 const gchar *version)
679{
Richard Hughes12724852018-09-04 13:53:44 +0100680 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100681 if (priv->compile_versions == NULL)
682 return;
683 g_hash_table_insert (priv->compile_versions,
684 g_strdup (component_id),
685 g_strdup (version));
686}
687
Richard Hughes9c028f02017-10-28 21:14:28 +0100688/**
689 * fu_plugin_lookup_quirk_by_id:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100690 * @self: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100691 * @group: A string, e.g. "DfuFlags"
692 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100693 *
694 * Looks up an entry in the hardware database using a string value.
695 *
696 * Returns: (transfer none): values from the database, or %NULL if not found
697 *
698 * Since: 1.0.1
699 **/
700const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100701fu_plugin_lookup_quirk_by_id (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100702{
Richard Hughes12724852018-09-04 13:53:44 +0100703 FuPluginPrivate *priv = GET_PRIVATE (self);
704 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes9c028f02017-10-28 21:14:28 +0100705
Richard Hughes9c028f02017-10-28 21:14:28 +0100706 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100707 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100708}
709
710/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100711 * fu_plugin_lookup_quirk_by_id_as_uint64:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100712 * @self: A #FuPlugin
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100713 * @group: A string, e.g. "DfuFlags"
714 * @key: An ID to match the entry, e.g. "Size"
715 *
716 * Looks up an entry in the hardware database using a string key, returning
717 * an integer value. Values are assumed base 10, unless prefixed with "0x"
718 * where they are parsed as base 16.
719 *
720 * Returns: (transfer none): value from the database, or 0 if not found
721 *
722 * Since: 1.1.2
723 **/
724guint64
Richard Hughes12724852018-09-04 13:53:44 +0100725fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100726{
Richard Hughes12724852018-09-04 13:53:44 +0100727 return fu_common_strtoull (fu_plugin_lookup_quirk_by_id (self, group, key));
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100728}
729
Richard Hughes1354ea92017-09-19 15:58:31 +0100730void
Richard Hughes12724852018-09-04 13:53:44 +0100731fu_plugin_set_smbios (FuPlugin *self, FuSmbios *smbios)
Richard Hughes49e5e052017-09-03 12:15:41 +0100732{
Richard Hughes12724852018-09-04 13:53:44 +0100733 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100734 g_set_object (&priv->smbios, smbios);
735}
736
Richard Hughesb8f8db22017-04-25 15:56:00 +0100737/**
Richard Hughesb0829032017-01-10 09:27:08 +0000738 * fu_plugin_set_coldplug_delay:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100739 * @self: A #FuPlugin
Richard Hughesb0829032017-01-10 09:27:08 +0000740 * @duration: A delay in milliseconds
741 *
742 * Set the minimum time that should be waited inbetween the call to
743 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
744 * to be the minimum hardware initialisation time from a datasheet.
745 *
746 * It is better to use this function rather than using a sleep() in the plugin
747 * itself as then only one delay is done in the daemon rather than waiting for
748 * each coldplug prepare in a serial way.
749 *
750 * Additionally, very long delays should be avoided as the daemon will be
751 * blocked from processing requests whilst the coldplug delay is being
752 * performed.
753 *
754 * Since: 0.8.0
755 **/
756void
Richard Hughes12724852018-09-04 13:53:44 +0100757fu_plugin_set_coldplug_delay (FuPlugin *self, guint duration)
Richard Hughesb0829032017-01-10 09:27:08 +0000758{
Richard Hughes12724852018-09-04 13:53:44 +0100759 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesb0829032017-01-10 09:27:08 +0000760 g_return_if_fail (duration > 0);
761
762 /* check sanity */
763 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
764 g_warning ("duration of %ums is crazy, truncating to %ums",
765 duration,
766 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
767 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
768 }
769
770 /* emit */
Richard Hughes12724852018-09-04 13:53:44 +0100771 g_signal_emit (self, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
Richard Hughesb0829032017-01-10 09:27:08 +0000772}
773
Richard Hughesd0905142016-03-13 09:46:49 +0000774gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100775fu_plugin_runner_startup (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000776{
Richard Hughes12724852018-09-04 13:53:44 +0100777 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000778 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000779 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000780
781 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000782 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000783 return TRUE;
784
Richard Hughes639da472018-01-06 22:35:04 +0000785 /* no object loaded */
786 if (priv->module == NULL)
787 return TRUE;
788
Richard Hughesd0905142016-03-13 09:46:49 +0000789 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000790 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
791 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000792 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000793 g_debug ("performing startup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000794 if (!func (self, &error_local)) {
795 if (error_local == NULL) {
796 g_critical ("unset error in plugin %s for startup()",
797 priv->name);
798 g_set_error_literal (&error_local,
799 FWUPD_ERROR,
800 FWUPD_ERROR_INTERNAL,
801 "unspecified error");
802 }
803 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
804 "failed to startup using %s: ",
805 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +0000806 return FALSE;
807 }
808 return TRUE;
809}
810
811static gboolean
812fu_plugin_runner_offline_invalidate (GError **error)
813{
814 g_autoptr(GError) error_local = NULL;
815 g_autoptr(GFile) file1 = NULL;
816
817 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
818
819 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
820 if (!g_file_query_exists (file1, NULL))
821 return TRUE;
822 if (!g_file_delete (file1, NULL, &error_local)) {
823 g_set_error (error,
824 FWUPD_ERROR,
825 FWUPD_ERROR_INTERNAL,
826 "Cannot delete %s: %s",
827 FU_OFFLINE_TRIGGER_FILENAME,
828 error_local->message);
829 return FALSE;
830 }
831 return TRUE;
832}
833
834static gboolean
835fu_plugin_runner_offline_setup (GError **error)
836{
837 gint rc;
838
839 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
840
841 /* create symlink for the systemd-system-update-generator */
842 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
843 if (rc < 0) {
844 g_set_error (error,
845 FWUPD_ERROR,
846 FWUPD_ERROR_INTERNAL,
847 "Failed to create symlink %s to %s: %s",
848 FU_OFFLINE_TRIGGER_FILENAME,
849 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000850 return FALSE;
851 }
852 return TRUE;
853}
854
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000855static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100856fu_plugin_runner_device_generic (FuPlugin *self, FuDevice *device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000857 const gchar *symbol_name, GError **error)
858{
Richard Hughes12724852018-09-04 13:53:44 +0100859 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000860 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000861 g_autoptr(GError) error_local = NULL;
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000862
863 /* not enabled */
864 if (!priv->enabled)
865 return TRUE;
866
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000867 /* no object loaded */
868 if (priv->module == NULL)
869 return TRUE;
870
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000871 /* optional */
872 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
873 if (func == NULL)
874 return TRUE;
875 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000876 if (!func (self, device, &error_local)) {
877 if (error_local == NULL) {
878 g_critical ("unset error in plugin %s for %s()",
879 priv->name, symbol_name + 10);
880 g_set_error_literal (&error_local,
881 FWUPD_ERROR,
882 FWUPD_ERROR_INTERNAL,
883 "unspecified error");
884 }
885 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
886 "failed to %s using %s: ",
887 symbol_name + 10, priv->name);
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000888 return FALSE;
889 }
890 return TRUE;
891}
892
Richard Hughesdbd8c762018-06-15 20:31:40 +0100893static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100894fu_plugin_runner_flagged_device_generic (FuPlugin *self, FwupdInstallFlags flags,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500895 FuDevice *device,
896 const gchar *symbol_name, GError **error)
897{
Richard Hughes12724852018-09-04 13:53:44 +0100898 FuPluginPrivate *priv = GET_PRIVATE (self);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500899 FuPluginFlaggedDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000900 g_autoptr(GError) error_local = NULL;
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500901
902 /* not enabled */
903 if (!priv->enabled)
904 return TRUE;
905
906 /* no object loaded */
907 if (priv->module == NULL)
908 return TRUE;
909
910 /* optional */
911 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
912 if (func == NULL)
913 return TRUE;
914 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000915 if (!func (self, flags, device, &error_local)) {
916 if (error_local == NULL) {
917 g_critical ("unset error in plugin %s for %s()",
918 priv->name, symbol_name + 10);
919 g_set_error_literal (&error_local,
920 FWUPD_ERROR,
921 FWUPD_ERROR_INTERNAL,
922 "unspecified error");
923 }
924 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
925 "failed to %s using %s: ",
926 symbol_name + 10, priv->name);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500927 return FALSE;
928 }
929 return TRUE;
930
931}
932
933static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100934fu_plugin_runner_device_array_generic (FuPlugin *self, GPtrArray *devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +0100935 const gchar *symbol_name, GError **error)
936{
Richard Hughes12724852018-09-04 13:53:44 +0100937 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesdbd8c762018-06-15 20:31:40 +0100938 FuPluginDeviceArrayFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000939 g_autoptr(GError) error_local = NULL;
Richard Hughesdbd8c762018-06-15 20:31:40 +0100940
941 /* not enabled */
942 if (!priv->enabled)
943 return TRUE;
944
945 /* no object loaded */
946 if (priv->module == NULL)
947 return TRUE;
948
949 /* optional */
950 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
951 if (func == NULL)
952 return TRUE;
953 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000954 if (!func (self, devices, &error_local)) {
955 if (error_local == NULL) {
956 g_critical ("unset error in plugin %s for %s()",
957 priv->name, symbol_name + 10);
958 g_set_error_literal (&error_local,
959 FWUPD_ERROR,
960 FWUPD_ERROR_INTERNAL,
961 "unspecified error");
962 }
963 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
964 "failed to %s using %s: ",
965 symbol_name + 10, priv->name);
Richard Hughesdbd8c762018-06-15 20:31:40 +0100966 return FALSE;
967 }
968 return TRUE;
969}
970
Richard Hughesd0905142016-03-13 09:46:49 +0000971gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100972fu_plugin_runner_coldplug (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000973{
Richard Hughes12724852018-09-04 13:53:44 +0100974 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000975 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000976 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000977
978 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000979 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000980 return TRUE;
981
Richard Hughes639da472018-01-06 22:35:04 +0000982 /* no object loaded */
983 if (priv->module == NULL)
984 return TRUE;
985
Richard Hughesd0905142016-03-13 09:46:49 +0000986 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000987 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
988 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000989 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000990 g_debug ("performing coldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000991 if (!func (self, &error_local)) {
992 if (error_local == NULL) {
993 g_critical ("unset error in plugin %s for coldplug()",
994 priv->name);
995 g_set_error_literal (&error_local,
996 FWUPD_ERROR,
997 FWUPD_ERROR_INTERNAL,
998 "unspecified error");
999 }
1000 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1001 "failed to coldplug using %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001002 return FALSE;
1003 }
1004 return TRUE;
1005}
1006
Richard Hughes7b8b2022016-12-12 16:15:03 +00001007gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001008fu_plugin_runner_recoldplug (FuPlugin *self, GError **error)
Richard Hughes2de8f132018-01-17 09:12:02 +00001009{
Richard Hughes12724852018-09-04 13:53:44 +01001010 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes2de8f132018-01-17 09:12:02 +00001011 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001012 g_autoptr(GError) error_local = NULL;
Richard Hughes2de8f132018-01-17 09:12:02 +00001013
1014 /* not enabled */
1015 if (!priv->enabled)
1016 return TRUE;
1017
1018 /* no object loaded */
1019 if (priv->module == NULL)
1020 return TRUE;
1021
1022 /* optional */
1023 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1024 if (func == NULL)
1025 return TRUE;
1026 g_debug ("performing recoldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001027 if (!func (self, &error_local)) {
1028 if (error_local == NULL) {
1029 g_critical ("unset error in plugin %s for recoldplug()",
1030 priv->name);
1031 g_set_error_literal (&error_local,
1032 FWUPD_ERROR,
1033 FWUPD_ERROR_INTERNAL,
1034 "unspecified error");
1035 }
1036 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1037 "failed to recoldplug using %s: ",
1038 priv->name);
Richard Hughes2de8f132018-01-17 09:12:02 +00001039 return FALSE;
1040 }
1041 return TRUE;
1042}
1043
1044gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001045fu_plugin_runner_coldplug_prepare (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001046{
Richard Hughes12724852018-09-04 13:53:44 +01001047 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001048 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001049 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001050
1051 /* not enabled */
1052 if (!priv->enabled)
1053 return TRUE;
1054
Richard Hughes639da472018-01-06 22:35:04 +00001055 /* no object loaded */
1056 if (priv->module == NULL)
1057 return TRUE;
1058
Richard Hughes46487c92017-01-07 21:26:34 +00001059 /* optional */
1060 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1061 if (func == NULL)
1062 return TRUE;
1063 g_debug ("performing coldplug_prepare() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001064 if (!func (self, &error_local)) {
1065 if (error_local == NULL) {
1066 g_critical ("unset error in plugin %s for coldplug_prepare()",
1067 priv->name);
1068 g_set_error_literal (&error_local,
1069 FWUPD_ERROR,
1070 FWUPD_ERROR_INTERNAL,
1071 "unspecified error");
1072 }
1073 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1074 "failed to coldplug_prepare using %s: ",
1075 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001076 return FALSE;
1077 }
1078 return TRUE;
1079}
1080
1081gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001082fu_plugin_runner_coldplug_cleanup (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001083{
Richard Hughes12724852018-09-04 13:53:44 +01001084 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001085 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001086 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001087
1088 /* not enabled */
1089 if (!priv->enabled)
1090 return TRUE;
1091
Richard Hughes639da472018-01-06 22:35:04 +00001092 /* no object loaded */
1093 if (priv->module == NULL)
1094 return TRUE;
1095
Richard Hughes46487c92017-01-07 21:26:34 +00001096 /* optional */
1097 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1098 if (func == NULL)
1099 return TRUE;
1100 g_debug ("performing coldplug_cleanup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001101 if (!func (self, &error_local)) {
1102 if (error_local == NULL) {
1103 g_critical ("unset error in plugin %s for coldplug_cleanup()",
1104 priv->name);
1105 g_set_error_literal (&error_local,
1106 FWUPD_ERROR,
1107 FWUPD_ERROR_INTERNAL,
1108 "unspecified error");
1109 }
1110 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1111 "failed to coldplug_cleanup using %s: ",
1112 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001113 return FALSE;
1114 }
1115 return TRUE;
1116}
1117
1118gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001119fu_plugin_runner_composite_prepare (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001120{
Richard Hughes12724852018-09-04 13:53:44 +01001121 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001122 "fu_plugin_composite_prepare",
1123 error);
1124}
1125
1126gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001127fu_plugin_runner_composite_cleanup (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001128{
Richard Hughes12724852018-09-04 13:53:44 +01001129 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001130 "fu_plugin_composite_cleanup",
1131 error);
1132}
1133
1134gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001135fu_plugin_runner_update_prepare (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001136 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001137{
Richard Hughes12724852018-09-04 13:53:44 +01001138 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001139 "fu_plugin_update_prepare",
1140 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001141}
1142
1143gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001144fu_plugin_runner_update_cleanup (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001145 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001146{
Richard Hughes12724852018-09-04 13:53:44 +01001147 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001148 "fu_plugin_update_cleanup",
1149 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001150}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001151
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001152gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001153fu_plugin_runner_update_attach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001154{
Richard Hughes12724852018-09-04 13:53:44 +01001155 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001156 "fu_plugin_update_attach", error);
1157}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001158
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001159gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001160fu_plugin_runner_update_detach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001161{
Richard Hughes12724852018-09-04 13:53:44 +01001162 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001163 "fu_plugin_update_detach", error);
1164}
1165
1166gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001167fu_plugin_runner_update_reload (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001168{
Richard Hughes12724852018-09-04 13:53:44 +01001169 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001170 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001171}
1172
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001173/**
1174 * fu_plugin_add_udev_subsystem:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001175 * @self: a #FuPlugin
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001176 * @subsystem: a subsystem name, e.g. `pciport`
1177 *
1178 * Registers the udev subsystem to be watched by the daemon.
1179 *
1180 * Plugins can use this method only in fu_plugin_init()
1181 **/
1182void
Richard Hughes12724852018-09-04 13:53:44 +01001183fu_plugin_add_udev_subsystem (FuPlugin *self, const gchar *subsystem)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001184{
Richard Hughes12724852018-09-04 13:53:44 +01001185 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001186 for (guint i = 0; i < priv->udev_subsystems->len; i++) {
1187 const gchar *subsystem_tmp = g_ptr_array_index (priv->udev_subsystems, i);
1188 if (g_strcmp0 (subsystem_tmp, subsystem) == 0)
1189 return;
1190 }
1191 g_debug ("added udev subsystem watch of %s", subsystem);
1192 g_ptr_array_add (priv->udev_subsystems, g_strdup (subsystem));
1193}
1194
Richard Hughes104f6512017-11-24 11:44:57 +00001195gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001196fu_plugin_runner_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
Richard Hughes104f6512017-11-24 11:44:57 +00001197{
Richard Hughes12724852018-09-04 13:53:44 +01001198 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes104f6512017-11-24 11:44:57 +00001199 FuPluginUsbDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001200 g_autoptr(GError) error_local = NULL;
Richard Hughes104f6512017-11-24 11:44:57 +00001201
1202 /* not enabled */
1203 if (!priv->enabled)
1204 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001205
1206 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001207 if (priv->module == NULL)
1208 return TRUE;
1209
1210 /* optional */
1211 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001212 if (func == NULL)
1213 return TRUE;
1214 g_debug ("performing usb_device_added() on %s", priv->name);
1215 if (!func (self, device, &error_local)) {
1216 if (error_local == NULL) {
1217 g_critical ("unset error in plugin %s for usb_device_added()",
1218 priv->name);
1219 g_set_error_literal (&error_local,
1220 FWUPD_ERROR,
1221 FWUPD_ERROR_INTERNAL,
1222 "unspecified error");
1223 }
1224 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1225 "failed to add device using on %s: ",
1226 priv->name);
1227 return FALSE;
Richard Hughes104f6512017-11-24 11:44:57 +00001228 }
1229 return TRUE;
1230}
1231
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001232gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001233fu_plugin_runner_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001234{
Richard Hughes12724852018-09-04 13:53:44 +01001235 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001236 FuPluginUdevDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001237 g_autoptr(GError) error_local = NULL;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001238
1239 /* not enabled */
1240 if (!priv->enabled)
1241 return TRUE;
1242
1243 /* no object loaded */
1244 if (priv->module == NULL)
1245 return TRUE;
1246
1247 /* optional */
1248 g_module_symbol (priv->module, "fu_plugin_udev_device_added", (gpointer *) &func);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001249 if (func == NULL)
1250 return TRUE;
1251 g_debug ("performing udev_device_added() on %s", priv->name);
1252 if (!func (self, device, &error_local)) {
1253 if (error_local == NULL) {
1254 g_critical ("unset error in plugin %s for udev_device_added()",
1255 priv->name);
1256 g_set_error_literal (&error_local,
1257 FWUPD_ERROR,
1258 FWUPD_ERROR_INTERNAL,
1259 "unspecified error");
1260 }
1261 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1262 "failed to add device using on %s: ",
1263 priv->name);
1264 return FALSE;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001265 }
1266 return TRUE;
1267}
1268
Richard Hughese1fd34d2017-08-24 14:19:51 +01001269void
Richard Hughes12724852018-09-04 13:53:44 +01001270fu_plugin_runner_device_removed (FuPlugin *self, FuDevice *device)
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001271{
1272 g_autoptr(GError) error_local= NULL;
1273
Richard Hughes12724852018-09-04 13:53:44 +01001274 if (!fu_plugin_runner_device_generic (self, device,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001275 "fu_plugin_device_removed",
1276 &error_local))
1277 g_warning ("%s", error_local->message);
1278}
1279
1280void
Richard Hughes12724852018-09-04 13:53:44 +01001281fu_plugin_runner_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +01001282{
Richard Hughes12724852018-09-04 13:53:44 +01001283 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001284 FuPluginDeviceRegisterFunc func = NULL;
1285
1286 /* not enabled */
1287 if (!priv->enabled)
1288 return;
Richard Hughes34834102017-11-21 21:55:00 +00001289 if (priv->module == NULL)
1290 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001291
Mario Limonciello4910b242018-06-22 15:04:21 -05001292 /* don't notify plugins on their own devices */
Richard Hughes12724852018-09-04 13:53:44 +01001293 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (self)) == 0)
Mario Limonciello4910b242018-06-22 15:04:21 -05001294 return;
1295
Richard Hughese1fd34d2017-08-24 14:19:51 +01001296 /* optional */
1297 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1298 if (func != NULL) {
Richard Hughes1bf7ff92018-08-24 20:21:35 +01001299 g_debug ("performing fu_plugin_device_registered() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001300 func (self, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001301 }
1302}
1303
Richard Hughescff38bc2016-12-12 12:03:37 +00001304static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001305fu_plugin_runner_schedule_update (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001306 FuDevice *device,
1307 GBytes *blob_cab,
1308 GError **error)
1309{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001310 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001311 gchar tmpname[] = {"XXXXXX.cap"};
1312 g_autofree gchar *dirname = NULL;
1313 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001314 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001315 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001316 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001317 g_autoptr(GFile) file = NULL;
1318
1319 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001320 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001321 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001322 if (res_tmp != NULL) {
1323 g_set_error (error,
1324 FWUPD_ERROR,
1325 FWUPD_ERROR_ALREADY_PENDING,
1326 "%s is already scheduled to be updated",
1327 fu_device_get_id (device));
1328 return FALSE;
1329 }
1330
1331 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001332 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001333 file = g_file_new_for_path (dirname);
1334 if (!g_file_query_exists (file, NULL)) {
1335 if (!g_file_make_directory_with_parents (file, NULL, error))
1336 return FALSE;
1337 }
1338
1339 /* get a random filename */
1340 for (guint i = 0; i < 6; i++)
1341 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1342 filename = g_build_filename (dirname, tmpname, NULL);
1343
1344 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001345 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001346 if (!g_file_set_contents (filename,
1347 g_bytes_get_data (blob_cab, NULL),
1348 (gssize) g_bytes_get_size (blob_cab),
1349 error))
1350 return FALSE;
1351
1352 /* schedule for next boot */
1353 g_debug ("schedule %s to be installed to %s on next boot",
1354 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001355 release = fu_device_get_release_default (device);
1356 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1357 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001358
1359 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001360 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001361 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001362 return FALSE;
1363
1364 /* next boot we run offline */
1365 return fu_plugin_runner_offline_setup (error);
1366}
1367
1368gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001369fu_plugin_runner_verify (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001370 FuDevice *device,
1371 FuPluginVerifyFlags flags,
1372 GError **error)
1373{
Richard Hughes12724852018-09-04 13:53:44 +01001374 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001375 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001376 GPtrArray *checksums;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001377 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001378
1379 /* not enabled */
1380 if (!priv->enabled)
1381 return TRUE;
1382
Richard Hughes639da472018-01-06 22:35:04 +00001383 /* no object loaded */
1384 if (priv->module == NULL)
1385 return TRUE;
1386
Richard Hughescff38bc2016-12-12 12:03:37 +00001387 /* optional */
1388 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1389 if (func == NULL)
1390 return TRUE;
Richard Hughes1812fc72018-12-14 11:37:54 +00001391
1392 /* clear any existing verification checksums */
1393 checksums = fu_device_get_checksums (device);
1394 g_ptr_array_set_size (checksums, 0);
1395
1396 /* run vfunc */
Richard Hughescff38bc2016-12-12 12:03:37 +00001397 g_debug ("performing verify() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001398 if (!func (self, device, flags, &error_local)) {
1399 if (error_local == NULL) {
1400 g_critical ("unset error in plugin %s for verify()",
1401 priv->name);
1402 g_set_error_literal (&error_local,
1403 FWUPD_ERROR,
1404 FWUPD_ERROR_INTERNAL,
1405 "unspecified error");
1406 }
1407 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1408 "failed to verify using %s: ",
1409 priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001410 return FALSE;
1411 }
1412 return TRUE;
1413}
1414
Richard Hughesd0905142016-03-13 09:46:49 +00001415gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001416fu_plugin_runner_unlock (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001417{
Richard Hughescff38bc2016-12-12 12:03:37 +00001418 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001419
1420 /* final check */
1421 flags = fu_device_get_flags (device);
1422 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1423 g_set_error (error,
1424 FWUPD_ERROR,
1425 FWUPD_ERROR_NOT_SUPPORTED,
1426 "Device %s is not locked",
1427 fu_device_get_id (device));
1428 return FALSE;
1429 }
1430
Richard Hughes9c4b5312017-11-14 11:34:53 +00001431 /* run vfunc */
Richard Hughes12724852018-09-04 13:53:44 +01001432 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes9c4b5312017-11-14 11:34:53 +00001433 "fu_plugin_unlock", error))
1434 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001435
1436 /* update with correct flags */
1437 flags = fu_device_get_flags (device);
1438 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1439 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1440 return TRUE;
1441}
1442
1443gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001444fu_plugin_runner_update (FuPlugin *self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001445 FuDevice *device,
1446 GBytes *blob_cab,
1447 GBytes *blob_fw,
1448 FwupdInstallFlags flags,
1449 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001450{
Richard Hughes12724852018-09-04 13:53:44 +01001451 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001452 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001453 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001454 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001455 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001456
1457 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001458 if (!priv->enabled) {
1459 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001460 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001461 }
Richard Hughesd0905142016-03-13 09:46:49 +00001462
Richard Hughes639da472018-01-06 22:35:04 +00001463 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001464 if (priv->module == NULL) {
1465 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001466 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001467 }
Richard Hughes639da472018-01-06 22:35:04 +00001468
Richard Hughesd0905142016-03-13 09:46:49 +00001469 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001470 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1471 if (update_func == NULL) {
1472 g_set_error_literal (error,
1473 FWUPD_ERROR,
1474 FWUPD_ERROR_NOT_SUPPORTED,
1475 "No update possible");
1476 return FALSE;
1477 }
Richard Hughesd0905142016-03-13 09:46:49 +00001478
Richard Hughesa785a1c2017-08-25 16:00:58 +01001479 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001480 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001481 if (blob_cab == NULL) {
1482 g_set_error_literal (error,
1483 FWUPD_ERROR,
1484 FWUPD_ERROR_NOT_SUPPORTED,
1485 "No cabinet archive to schedule");
1486 return FALSE;
1487 }
Richard Hughes12724852018-09-04 13:53:44 +01001488 return fu_plugin_runner_schedule_update (self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001489 device,
1490 blob_cab,
1491 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001492 }
1493
1494 /* cancel the pending action */
1495 if (!fu_plugin_runner_offline_invalidate (error))
1496 return FALSE;
1497
1498 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001499 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001500 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001501 if (!update_func (self, device, blob_fw, flags, &error_local)) {
1502 if (error_local == NULL) {
1503 g_critical ("unset error in plugin %s for update()",
1504 priv->name);
1505 g_set_error_literal (&error_local,
Richard Hughes3c8ada32018-10-12 10:08:58 +01001506 FWUPD_ERROR,
1507 FWUPD_ERROR_INTERNAL,
1508 "unspecified error");
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001509 return FALSE;
Richard Hughes3c8ada32018-10-12 10:08:58 +01001510 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001511 fu_device_set_update_error (device, error_local->message);
1512 g_propagate_error (error, g_steal_pointer (&error_local));
Richard Hughescff38bc2016-12-12 12:03:37 +00001513 return FALSE;
1514 }
1515
Richard Hughesf556d372017-06-15 19:49:18 +01001516 /* no longer valid */
Richard Hughesf8039642019-01-16 12:22:22 +00001517 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT) &&
1518 !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) {
Richard Hughes08435162018-12-12 10:34:16 +00001519 GPtrArray *checksums = fu_device_get_checksums (device);
1520 g_ptr_array_set_size (checksums, 0);
1521 }
Richard Hughesf556d372017-06-15 19:49:18 +01001522
Richard Hughescff38bc2016-12-12 12:03:37 +00001523 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001524 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001525 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001526 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001527
Richard Hughes780ef3f2018-01-12 16:20:31 +00001528 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001529 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1530 if (!fu_history_modify_device (history, device,
1531 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1532 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001533 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001534
1535 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001536 release = fu_device_get_release_default (device_pending);
1537 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001538 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001539 g_autoptr(GError) error_delete = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001540 g_autoptr(GFile) file = NULL;
1541 file = g_file_new_for_path (tmp);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001542 if (!g_file_delete (file, NULL, &error_delete)) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001543 g_set_error (error,
1544 FWUPD_ERROR,
1545 FWUPD_ERROR_INVALID_FILE,
1546 "Failed to delete %s: %s",
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001547 tmp, error_delete->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001548 return FALSE;
1549 }
1550 }
1551 }
Richard Hughesd0905142016-03-13 09:46:49 +00001552 return TRUE;
1553}
Richard Hughescff38bc2016-12-12 12:03:37 +00001554
1555gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001556fu_plugin_runner_clear_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001557{
Richard Hughes12724852018-09-04 13:53:44 +01001558 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001559 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001560 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001561
1562 /* not enabled */
1563 if (!priv->enabled)
1564 return TRUE;
1565
Richard Hughes639da472018-01-06 22:35:04 +00001566 /* no object loaded */
1567 if (priv->module == NULL)
1568 return TRUE;
1569
Richard Hughes65e44ca2018-01-30 17:26:30 +00001570 /* optional */
1571 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1572 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001573 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001574 g_debug ("performing clear_result() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001575 if (!func (self, device, &error_local)) {
1576 if (error_local == NULL) {
1577 g_critical ("unset error in plugin %s for clear_result()",
1578 priv->name);
1579 g_set_error_literal (&error_local,
1580 FWUPD_ERROR,
1581 FWUPD_ERROR_INTERNAL,
1582 "unspecified error");
1583 }
1584 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1585 "failed to clear_result using %s: ",
1586 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001587 return FALSE;
1588 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001589 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001590}
1591
1592gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001593fu_plugin_runner_get_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001594{
Richard Hughes12724852018-09-04 13:53:44 +01001595 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001596 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001597 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001598
1599 /* not enabled */
1600 if (!priv->enabled)
1601 return TRUE;
1602
Richard Hughes639da472018-01-06 22:35:04 +00001603 /* no object loaded */
1604 if (priv->module == NULL)
1605 return TRUE;
1606
Richard Hughes65e44ca2018-01-30 17:26:30 +00001607 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001608 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001609 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001610 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001611 g_debug ("performing get_results() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001612 if (!func (self, device, &error_local)) {
1613 if (error_local == NULL) {
1614 g_critical ("unset error in plugin %s for get_results()",
1615 priv->name);
1616 g_set_error_literal (&error_local,
1617 FWUPD_ERROR,
1618 FWUPD_ERROR_INTERNAL,
1619 "unspecified error");
1620 }
1621 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1622 "failed to get_results using %s: ",
1623 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001624 return FALSE;
1625 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001626 return TRUE;
1627}
1628
Richard Hughes08a37992017-09-12 12:57:43 +01001629/**
1630 * fu_plugin_get_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001631 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001632 *
1633 * Gets the plugin order, where higher numbers are run after lower
1634 * numbers.
1635 *
1636 * Returns: the integer value
1637 **/
1638guint
Richard Hughes12724852018-09-04 13:53:44 +01001639fu_plugin_get_order (FuPlugin *self)
Richard Hughes08a37992017-09-12 12:57:43 +01001640{
Richard Hughes12724852018-09-04 13:53:44 +01001641 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001642 return priv->order;
1643}
1644
1645/**
1646 * fu_plugin_set_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001647 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001648 * @order: a integer value
1649 *
1650 * Sets the plugin order, where higher numbers are run after lower
1651 * numbers.
1652 **/
1653void
Richard Hughes12724852018-09-04 13:53:44 +01001654fu_plugin_set_order (FuPlugin *self, guint order)
Richard Hughes08a37992017-09-12 12:57:43 +01001655{
Richard Hughes12724852018-09-04 13:53:44 +01001656 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001657 priv->order = order;
1658}
1659
1660/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001661 * fu_plugin_get_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001662 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001663 *
1664 * Gets the plugin priority, where higher numbers are better.
1665 *
1666 * Returns: the integer value
1667 **/
1668guint
Richard Hughes12724852018-09-04 13:53:44 +01001669fu_plugin_get_priority (FuPlugin *self)
Richard Hughes81c427c2018-08-06 15:20:17 +01001670{
Richard Hughes12724852018-09-04 13:53:44 +01001671 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001672 return priv->priority;
1673}
1674
1675/**
1676 * fu_plugin_set_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001677 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001678 * @priority: a integer value
1679 *
1680 * Sets the plugin priority, where higher numbers are better.
1681 **/
1682void
Richard Hughes12724852018-09-04 13:53:44 +01001683fu_plugin_set_priority (FuPlugin *self, guint priority)
Richard Hughes81c427c2018-08-06 15:20:17 +01001684{
Richard Hughes12724852018-09-04 13:53:44 +01001685 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001686 priv->priority = priority;
1687}
1688
1689/**
Richard Hughes08a37992017-09-12 12:57:43 +01001690 * fu_plugin_add_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001691 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001692 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001693 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001694 *
1695 * If the plugin name is found, the rule will be used to sort the plugin list,
1696 * for example the plugin specified by @name will be ordered after this plugin
1697 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1698 *
1699 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1700 * If depsolving fails then fwupd will not start.
1701 **/
1702void
Richard Hughes12724852018-09-04 13:53:44 +01001703fu_plugin_add_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes08a37992017-09-12 12:57:43 +01001704{
Richard Hughes12724852018-09-04 13:53:44 +01001705 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001706 g_ptr_array_add (priv->rules[rule], g_strdup (name));
Richard Hughes75b965d2018-11-15 13:51:21 +00001707 g_signal_emit (self, signals[SIGNAL_RULES_CHANGED], 0);
Richard Hughes08a37992017-09-12 12:57:43 +01001708}
1709
1710/**
1711 * fu_plugin_get_rules:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001712 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001713 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1714 *
1715 * Gets the plugin IDs that should be run after this plugin.
1716 *
1717 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1718 **/
1719GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +01001720fu_plugin_get_rules (FuPlugin *self, FuPluginRule rule)
Richard Hughes08a37992017-09-12 12:57:43 +01001721{
Richard Hughes12724852018-09-04 13:53:44 +01001722 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001723 return priv->rules[rule];
1724}
1725
Richard Hughes80b79bb2018-01-11 21:11:06 +00001726/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001727 * fu_plugin_has_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001728 * @self: a #FuPlugin
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001729 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1730 * @name: a plugin name, e.g. `upower`
1731 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001732 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001733 *
1734 * Returns: %TRUE if the name exists for the specific rule
1735 **/
1736gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001737fu_plugin_has_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001738{
Richard Hughes12724852018-09-04 13:53:44 +01001739 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001740 for (guint i = 0; i < priv->rules[rule]->len; i++) {
1741 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
1742 if (g_strcmp0 (tmp, name) == 0)
1743 return TRUE;
1744 }
1745 return FALSE;
1746}
1747
1748/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00001749 * fu_plugin_add_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001750 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00001751 * @key: a string, e.g. `FwupdateVersion`
1752 * @value: a string, e.g. `10`
1753 *
1754 * Sets any additional metadata to be included in the firmware report to aid
1755 * debugging problems.
1756 *
1757 * Any data included here will be sent to the metadata server after user
1758 * confirmation.
1759 **/
1760void
Richard Hughes12724852018-09-04 13:53:44 +01001761fu_plugin_add_report_metadata (FuPlugin *self, const gchar *key, const gchar *value)
Richard Hughes80b79bb2018-01-11 21:11:06 +00001762{
Richard Hughes12724852018-09-04 13:53:44 +01001763 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001764 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1765}
1766
1767/**
1768 * fu_plugin_get_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001769 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00001770 *
1771 * Returns the list of additional metadata to be added when filing a report.
1772 *
1773 * Returns: (transfer none): the map of report metadata
1774 **/
1775GHashTable *
Richard Hughes12724852018-09-04 13:53:44 +01001776fu_plugin_get_report_metadata (FuPlugin *self)
Richard Hughes80b79bb2018-01-11 21:11:06 +00001777{
Richard Hughes12724852018-09-04 13:53:44 +01001778 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001779 return priv->report_metadata;
1780}
1781
Mario Limonciello963dc422018-02-27 14:26:58 -06001782/**
1783 * fu_plugin_get_config_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001784 * @self: a #FuPlugin
Mario Limonciello963dc422018-02-27 14:26:58 -06001785 * @key: A settings key
1786 *
1787 * Return the value of a key if it's been configured
1788 *
1789 * Since: 1.0.6
1790 **/
1791gchar *
Richard Hughes12724852018-09-04 13:53:44 +01001792fu_plugin_get_config_value (FuPlugin *self, const gchar *key)
Mario Limonciello963dc422018-02-27 14:26:58 -06001793{
Richard Hughes4be17d12018-05-30 20:36:29 +01001794 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06001795 g_autofree gchar *conf_file = NULL;
1796 g_autofree gchar *conf_path = NULL;
1797 g_autoptr(GKeyFile) keyfile = NULL;
1798 const gchar *plugin_name;
1799
Richard Hughes4be17d12018-05-30 20:36:29 +01001800 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Richard Hughes12724852018-09-04 13:53:44 +01001801 plugin_name = fu_plugin_get_name (self);
Mario Limonciello963dc422018-02-27 14:26:58 -06001802 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01001803 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06001804 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1805 return NULL;
1806 keyfile = g_key_file_new ();
1807 if (!g_key_file_load_from_file (keyfile, conf_path,
1808 G_KEY_FILE_NONE, NULL))
1809 return NULL;
1810 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1811}
1812
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001813/**
1814 * fu_plugin_name_compare:
1815 * @plugin1: first #FuPlugin to compare.
1816 * @plugin2: second #FuPlugin to compare.
1817 *
1818 * Compares two plugins by their names.
1819 *
1820 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1821 **/
1822gint
1823fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1824{
1825 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1826 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1827 return g_strcmp0 (priv1->name, priv2->name);
1828}
1829
1830/**
1831 * fu_plugin_order_compare:
1832 * @plugin1: first #FuPlugin to compare.
1833 * @plugin2: second #FuPlugin to compare.
1834 *
1835 * Compares two plugins by their depsolved order.
1836 *
1837 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1838 **/
1839gint
1840fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1841{
1842 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1843 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1844 if (priv1->order < priv2->order)
1845 return -1;
1846 if (priv1->order > priv2->order)
1847 return 1;
1848 return 0;
1849}
1850
Richard Hughescff38bc2016-12-12 12:03:37 +00001851static void
1852fu_plugin_class_init (FuPluginClass *klass)
1853{
1854 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1855 object_class->finalize = fu_plugin_finalize;
1856 signals[SIGNAL_DEVICE_ADDED] =
1857 g_signal_new ("device-added",
1858 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1859 G_STRUCT_OFFSET (FuPluginClass, device_added),
1860 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1861 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1862 signals[SIGNAL_DEVICE_REMOVED] =
1863 g_signal_new ("device-removed",
1864 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1865 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1866 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1867 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001868 signals[SIGNAL_DEVICE_REGISTER] =
1869 g_signal_new ("device-register",
1870 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1871 G_STRUCT_OFFSET (FuPluginClass, device_register),
1872 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1873 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001874 signals[SIGNAL_RECOLDPLUG] =
1875 g_signal_new ("recoldplug",
1876 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1877 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1878 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1879 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001880 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1881 g_signal_new ("set-coldplug-delay",
1882 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1883 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1884 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1885 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughesaabdc372018-11-14 10:11:08 +00001886 signals[SIGNAL_CHECK_SUPPORTED] =
1887 g_signal_new ("check-supported",
1888 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1889 G_STRUCT_OFFSET (FuPluginClass, check_supported),
1890 NULL, NULL, g_cclosure_marshal_generic,
1891 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
Richard Hughes75b965d2018-11-15 13:51:21 +00001892 signals[SIGNAL_RULES_CHANGED] =
1893 g_signal_new ("rules-changed",
1894 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1895 G_STRUCT_OFFSET (FuPluginClass, rules_changed),
1896 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1897 G_TYPE_NONE, 0);
Richard Hughescff38bc2016-12-12 12:03:37 +00001898}
1899
1900static void
Richard Hughes12724852018-09-04 13:53:44 +01001901fu_plugin_init (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +00001902{
Richard Hughes12724852018-09-04 13:53:44 +01001903 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001904 priv->enabled = TRUE;
1905 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1906 g_free, (GDestroyNotify) g_object_unref);
Richard Hughes37d09432018-09-09 10:39:45 +01001907 priv->devices_mutex = fu_mutex_new (G_OBJECT_TYPE_NAME(self), "devices");
Richard Hughes80b79bb2018-01-11 21:11:06 +00001908 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 +01001909 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1910 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001911}
1912
1913static void
1914fu_plugin_finalize (GObject *object)
1915{
Richard Hughes12724852018-09-04 13:53:44 +01001916 FuPlugin *self = FU_PLUGIN (object);
1917 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001918 FuPluginInitFunc func = NULL;
1919
1920 /* optional */
1921 if (priv->module != NULL) {
1922 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1923 if (func != NULL) {
1924 g_debug ("performing destroy() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001925 func (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001926 }
1927 }
1928
Richard Hughes08a37992017-09-12 12:57:43 +01001929 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1930 g_ptr_array_unref (priv->rules[i]);
1931
Richard Hughescff38bc2016-12-12 12:03:37 +00001932 if (priv->usb_ctx != NULL)
1933 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001934 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001935 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001936 if (priv->quirks != NULL)
1937 g_object_unref (priv->quirks);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001938 if (priv->udev_subsystems != NULL)
1939 g_ptr_array_unref (priv->udev_subsystems);
Richard Hughes49e5e052017-09-03 12:15:41 +01001940 if (priv->smbios != NULL)
1941 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001942 if (priv->runtime_versions != NULL)
1943 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001944 if (priv->compile_versions != NULL)
1945 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00001946 g_hash_table_unref (priv->devices);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001947 g_hash_table_unref (priv->report_metadata);
Richard Hughes37d09432018-09-09 10:39:45 +01001948 g_object_unref (priv->devices_mutex);
Richard Hughescff38bc2016-12-12 12:03:37 +00001949 g_free (priv->name);
1950 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05001951 /* Must happen as the last step to avoid prematurely
1952 * freeing memory held by the plugin */
1953#ifndef RUNNING_ON_VALGRIND
1954 if (priv->module != NULL)
1955 g_module_close (priv->module);
1956#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001957
1958 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1959}
1960
1961FuPlugin *
1962fu_plugin_new (void)
1963{
Richard Hughes12724852018-09-04 13:53:44 +01001964 return FU_PLUGIN (g_object_new (FU_TYPE_PLUGIN, NULL));
Richard Hughescff38bc2016-12-12 12:03:37 +00001965}