blob: b403d9538241848a67c041832b32544ec0e87b47 [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 Hughesf425d292019-01-18 17:57:39 +000047 gchar *build_hash;
Richard Hughesd7704d42017-08-08 20:29:09 +010048 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010049 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010050 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010051 GHashTable *compile_versions;
Richard Hughes9d6e0e72018-08-24 20:20:17 +010052 GPtrArray *udev_subsystems;
Richard Hughes1354ea92017-09-19 15:58:31 +010053 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000054 GHashTable *devices; /* platform_id:GObject */
Richard Hughes37d09432018-09-09 10:39:45 +010055 FuMutex *devices_mutex;
Richard Hughes80b79bb2018-01-11 21:11:06 +000056 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000057 FuPluginData *data;
58} FuPluginPrivate;
59
60enum {
61 SIGNAL_DEVICE_ADDED,
62 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010063 SIGNAL_DEVICE_REGISTER,
Richard Hughes75b965d2018-11-15 13:51:21 +000064 SIGNAL_RULES_CHANGED,
Richard Hughes362d6d72017-01-07 21:42:14 +000065 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000066 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughesaabdc372018-11-14 10:11:08 +000067 SIGNAL_CHECK_SUPPORTED,
Richard Hughescff38bc2016-12-12 12:03:37 +000068 SIGNAL_LAST
69};
70
71static guint signals[SIGNAL_LAST] = { 0 };
72
73G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
74#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
75
76typedef const gchar *(*FuPluginGetNameFunc) (void);
Richard Hughes12724852018-09-04 13:53:44 +010077typedef void (*FuPluginInitFunc) (FuPlugin *self);
78typedef gboolean (*FuPluginStartupFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000079 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010080typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *self,
Richard Hughese1fd34d2017-08-24 14:19:51 +010081 FuDevice *device);
Richard Hughes12724852018-09-04 13:53:44 +010082typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000083 FuDevice *device,
84 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010085typedef gboolean (*FuPluginFlaggedDeviceFunc) (FuPlugin *self,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -050086 FwupdInstallFlags flags,
87 FuDevice *device,
88 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010089typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *self,
Richard Hughesdbd8c762018-06-15 20:31:40 +010090 GPtrArray *devices,
91 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010092typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000093 FuDevice *device,
94 FuPluginVerifyFlags flags,
95 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010096typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000097 FuDevice *device,
98 GBytes *blob_fw,
99 FwupdInstallFlags flags,
100 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100101typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100102 FuUsbDevice *device,
Richard Hughes104f6512017-11-24 11:44:57 +0000103 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100104typedef gboolean (*FuPluginUdevDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100105 FuUdevDevice *device,
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100106 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +0000107
Richard Hughes57d18222017-01-10 16:02:59 +0000108/**
109 * fu_plugin_get_name:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100110 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000111 *
112 * Gets the plugin name.
113 *
114 * Returns: a plugin name, or %NULL for unknown.
115 *
116 * Since: 0.8.0
117 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000118const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100119fu_plugin_get_name (FuPlugin *self)
Richard Hughesd0905142016-03-13 09:46:49 +0000120{
Richard Hughes12724852018-09-04 13:53:44 +0100121 FuPluginPrivate *priv = GET_PRIVATE (self);
122 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000123 return priv->name;
124}
Richard Hughesd0905142016-03-13 09:46:49 +0000125
Richard Hughes34834102017-11-21 21:55:00 +0000126void
Richard Hughes12724852018-09-04 13:53:44 +0100127fu_plugin_set_name (FuPlugin *self, const gchar *name)
Richard Hughes34834102017-11-21 21:55:00 +0000128{
Richard Hughes12724852018-09-04 13:53:44 +0100129 FuPluginPrivate *priv = GET_PRIVATE (self);
130 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughes34834102017-11-21 21:55:00 +0000131 g_return_if_fail (name != NULL);
132 g_free (priv->name);
133 priv->name = g_strdup (name);
134}
135
Richard Hughes57d18222017-01-10 16:02:59 +0000136/**
Richard Hughesf425d292019-01-18 17:57:39 +0000137 * fu_plugin_set_build_hash:
138 * @self: A #FuPlugin
139 * @build_hash: A checksum
140 *
141 * Sets the plugin build hash, typically a SHA256 checksum. All plugins must
142 * set the correct checksum to avoid the daemon being marked as tainted.
143 *
144 * Since: 1.2.4
145 **/
146void
147fu_plugin_set_build_hash (FuPlugin *self, const gchar *build_hash)
148{
149 FuPluginPrivate *priv = GET_PRIVATE (self);
150 g_return_if_fail (FU_IS_PLUGIN (self));
151 g_return_if_fail (build_hash != NULL);
152 g_free (priv->build_hash);
153 priv->build_hash = g_strdup (build_hash);
154}
155
156const gchar *
157fu_plugin_get_build_hash (FuPlugin *self)
158{
159 FuPluginPrivate *priv = GET_PRIVATE (self);
160 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
161 return priv->build_hash;
162}
163
164/**
Richard Hughes57d18222017-01-10 16:02:59 +0000165 * fu_plugin_cache_lookup:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100166 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000167 * @id: the key
168 *
169 * Finds an object in the per-plugin cache.
170 *
171 * Returns: (transfer none): a #GObject, or %NULL for unfound.
172 *
173 * Since: 0.8.0
174 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000175gpointer
Richard Hughes12724852018-09-04 13:53:44 +0100176fu_plugin_cache_lookup (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000177{
Richard Hughes12724852018-09-04 13:53:44 +0100178 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes37d09432018-09-09 10:39:45 +0100179 g_autoptr(FuMutexLocker) locker = fu_mutex_read_locker_new (priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100180 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughesccd78a92017-01-11 16:57:41 +0000181 g_return_val_if_fail (id != NULL, NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100182 g_return_val_if_fail (locker != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000183 return g_hash_table_lookup (priv->devices, id);
184}
Richard Hughesd0905142016-03-13 09:46:49 +0000185
Richard Hughes57d18222017-01-10 16:02:59 +0000186/**
187 * fu_plugin_cache_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100188 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000189 * @id: the key
190 * @dev: a #GObject, typically a #FuDevice
191 *
192 * Adds an object to the per-plugin cache.
193 *
194 * Since: 0.8.0
195 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000196void
Richard Hughes12724852018-09-04 13:53:44 +0100197fu_plugin_cache_add (FuPlugin *self, const gchar *id, gpointer dev)
Richard Hughescff38bc2016-12-12 12:03:37 +0000198{
Richard Hughes12724852018-09-04 13:53:44 +0100199 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes37d09432018-09-09 10:39:45 +0100200 g_autoptr(FuMutexLocker) locker = fu_mutex_write_locker_new (priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100201 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000202 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100203 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000204 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
205}
206
Richard Hughes57d18222017-01-10 16:02:59 +0000207/**
208 * fu_plugin_cache_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100209 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000210 * @id: the key
211 *
212 * Removes an object from the per-plugin cache.
213 *
214 * Since: 0.8.0
215 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000216void
Richard Hughes12724852018-09-04 13:53:44 +0100217fu_plugin_cache_remove (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000218{
Richard Hughes12724852018-09-04 13:53:44 +0100219 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes37d09432018-09-09 10:39:45 +0100220 g_autoptr(FuMutexLocker) locker = fu_mutex_write_locker_new (priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100221 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000222 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100223 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000224 g_hash_table_remove (priv->devices, id);
225}
226
Richard Hughes57d18222017-01-10 16:02:59 +0000227/**
228 * fu_plugin_get_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100229 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000230 *
Richard Hughes4eada342017-10-03 21:20:32 +0100231 * Gets the per-plugin allocated private data. This will return %NULL unless
232 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000233 *
Richard Hughes4eada342017-10-03 21:20:32 +0100234 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000235 *
236 * Since: 0.8.0
237 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000238FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100239fu_plugin_get_data (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000240{
Richard Hughes12724852018-09-04 13:53:44 +0100241 FuPluginPrivate *priv = GET_PRIVATE (self);
242 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000243 return priv->data;
244}
245
Richard Hughes57d18222017-01-10 16:02:59 +0000246/**
247 * fu_plugin_alloc_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100248 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000249 * @data_sz: the size to allocate
250 *
251 * Allocates the per-plugin allocated private data.
252 *
253 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
254 *
255 * Since: 0.8.0
256 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000257FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100258fu_plugin_alloc_data (FuPlugin *self, gsize data_sz)
Richard Hughescff38bc2016-12-12 12:03:37 +0000259{
Richard Hughes12724852018-09-04 13:53:44 +0100260 FuPluginPrivate *priv = GET_PRIVATE (self);
261 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000262 if (priv->data != NULL) {
263 g_critical ("fu_plugin_alloc_data() already used by plugin");
264 return priv->data;
265 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000266 priv->data = g_malloc0 (data_sz);
267 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000268}
269
Richard Hughes57d18222017-01-10 16:02:59 +0000270/**
271 * fu_plugin_get_usb_context:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100272 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000273 *
274 * Gets the shared USB context that all plugins can use.
275 *
276 * Returns: (transfer none): a #GUsbContext.
277 *
278 * Since: 0.8.0
279 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000280GUsbContext *
Richard Hughes12724852018-09-04 13:53:44 +0100281fu_plugin_get_usb_context (FuPlugin *self)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000282{
Richard Hughes12724852018-09-04 13:53:44 +0100283 FuPluginPrivate *priv = GET_PRIVATE (self);
284 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000285 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000286}
287
288void
Richard Hughes12724852018-09-04 13:53:44 +0100289fu_plugin_set_usb_context (FuPlugin *self, GUsbContext *usb_ctx)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000290{
Richard Hughes12724852018-09-04 13:53:44 +0100291 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000292 g_set_object (&priv->usb_ctx, usb_ctx);
293}
294
Richard Hughes57d18222017-01-10 16:02:59 +0000295/**
296 * fu_plugin_get_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100297 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000298 *
Richard Hughes4eada342017-10-03 21:20:32 +0100299 * Returns if the plugin is enabled. Plugins may self-disable using
300 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000301 *
302 * Returns: %TRUE if the plugin is currently enabled.
303 *
304 * Since: 0.8.0
305 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000306gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100307fu_plugin_get_enabled (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000308{
Richard Hughes12724852018-09-04 13:53:44 +0100309 FuPluginPrivate *priv = GET_PRIVATE (self);
310 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000311 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000312}
313
Richard Hughes57d18222017-01-10 16:02:59 +0000314/**
315 * fu_plugin_set_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100316 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000317 * @enabled: the enabled value
318 *
319 * Enables or disables a plugin. Plugins can self-disable at any point.
320 *
321 * Since: 0.8.0
322 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000323void
Richard Hughes12724852018-09-04 13:53:44 +0100324fu_plugin_set_enabled (FuPlugin *self, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000325{
Richard Hughes12724852018-09-04 13:53:44 +0100326 FuPluginPrivate *priv = GET_PRIVATE (self);
327 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000328 priv->enabled = enabled;
329}
330
Richard Hughes1e456bc2018-05-10 20:16:16 +0100331gchar *
332fu_plugin_guess_name_from_fn (const gchar *filename)
333{
334 const gchar *prefix = "libfu_plugin_";
335 gchar *name;
336 gchar *str = g_strstr_len (filename, -1, prefix);
337 if (str == NULL)
338 return NULL;
339 name = g_strdup (str + strlen (prefix));
340 g_strdelimit (name, ".", '\0');
341 return name;
342}
343
Richard Hughescff38bc2016-12-12 12:03:37 +0000344gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100345fu_plugin_open (FuPlugin *self, const gchar *filename, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +0000346{
Richard Hughes12724852018-09-04 13:53:44 +0100347 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000348 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000349
350 priv->module = g_module_open (filename, 0);
351 if (priv->module == NULL) {
352 g_set_error (error,
353 G_IO_ERROR,
354 G_IO_ERROR_FAILED,
355 "failed to open plugin: %s",
356 g_module_error ());
357 return FALSE;
358 }
359
360 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100361 if (priv->name == NULL)
362 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000363
364 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000365 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
366 if (func != NULL) {
367 g_debug ("performing init() on %s", filename);
Richard Hughes12724852018-09-04 13:53:44 +0100368 func (self);
Richard Hughesd0905142016-03-13 09:46:49 +0000369 }
370
Richard Hughescff38bc2016-12-12 12:03:37 +0000371 return TRUE;
372}
373
Richard Hughes57d18222017-01-10 16:02:59 +0000374/**
375 * fu_plugin_device_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100376 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000377 * @device: A #FuDevice
378 *
379 * Asks the daemon to add a device to the exported list. If this device ID
380 * has already been added by a different plugin then this request will be
381 * ignored.
382 *
383 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
384 * actually flashing an image to the hardware so that higher-priority plugins
385 * can add the device themselves.
386 *
387 * Since: 0.8.0
388 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000389void
Richard Hughes12724852018-09-04 13:53:44 +0100390fu_plugin_device_add (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000391{
Richard Hughes5e447292018-04-27 14:25:54 +0100392 GPtrArray *children;
Richard Hughesc125ec02018-09-05 19:35:17 +0100393 g_autoptr(GError) error = NULL;
Richard Hughes5e447292018-04-27 14:25:54 +0100394
Richard Hughes12724852018-09-04 13:53:44 +0100395 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000396 g_return_if_fail (FU_IS_DEVICE (device));
397
Richard Hughesc125ec02018-09-05 19:35:17 +0100398 /* ensure the device ID is set from the physical and logical IDs */
399 if (!fu_device_ensure_id (device, &error)) {
400 g_warning ("ignoring add: %s", error->message);
401 return;
402 }
403
Richard Hughescff38bc2016-12-12 12:03:37 +0000404 g_debug ("emit added from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100405 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000406 fu_device_get_id (device));
407 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
Richard Hughes12724852018-09-04 13:53:44 +0100408 fu_device_set_plugin (device, fu_plugin_get_name (self));
409 g_signal_emit (self, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100410
Richard Hughes128c0162018-08-10 11:00:29 +0100411 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100412 children = fu_device_get_children (device);
413 for (guint i = 0; i < children->len; i++) {
414 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100415 if (fu_device_get_created (child) == 0)
Richard Hughes12724852018-09-04 13:53:44 +0100416 fu_plugin_device_add (self, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100417 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000418}
419
Richard Hughese1fd34d2017-08-24 14:19:51 +0100420/**
421 * fu_plugin_device_register:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100422 * @self: A #FuPlugin
Richard Hughese1fd34d2017-08-24 14:19:51 +0100423 * @device: A #FuDevice
424 *
425 * Registers the device with other plugins so they can set metadata.
426 *
427 * Plugins do not have to call this manually as this is done automatically
428 * when using fu_plugin_device_add(). They may wish to use this manually
429 * if for intance the coldplug should be ignored based on the metadata
430 * set from other plugins.
431 *
432 * Since: 0.9.7
433 **/
434void
Richard Hughes12724852018-09-04 13:53:44 +0100435fu_plugin_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +0100436{
Richard Hughesc125ec02018-09-05 19:35:17 +0100437 g_autoptr(GError) error = NULL;
438
Richard Hughes12724852018-09-04 13:53:44 +0100439 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughese1fd34d2017-08-24 14:19:51 +0100440 g_return_if_fail (FU_IS_DEVICE (device));
441
Richard Hughesc125ec02018-09-05 19:35:17 +0100442 /* ensure the device ID is set from the physical and logical IDs */
443 if (!fu_device_ensure_id (device, &error)) {
444 g_warning ("ignoring registration: %s", error->message);
445 return;
446 }
447
Richard Hughese1fd34d2017-08-24 14:19:51 +0100448 g_debug ("emit device-register from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100449 fu_plugin_get_name (self),
Richard Hughese1fd34d2017-08-24 14:19:51 +0100450 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100451 g_signal_emit (self, signals[SIGNAL_DEVICE_REGISTER], 0, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +0100452}
453
Richard Hughes57d18222017-01-10 16:02:59 +0000454/**
Richard Hughes4eada342017-10-03 21:20:32 +0100455 * fu_plugin_device_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100456 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000457 * @device: A #FuDevice
458 *
459 * Asks the daemon to remove a device from the exported list.
460 *
461 * Since: 0.8.0
462 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000463void
Richard Hughes12724852018-09-04 13:53:44 +0100464fu_plugin_device_remove (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000465{
Richard Hughes12724852018-09-04 13:53:44 +0100466 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000467 g_return_if_fail (FU_IS_DEVICE (device));
468
Richard Hughescff38bc2016-12-12 12:03:37 +0000469 g_debug ("emit removed from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100470 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000471 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100472 g_signal_emit (self, signals[SIGNAL_DEVICE_REMOVED], 0, device);
Richard Hughescff38bc2016-12-12 12:03:37 +0000473}
474
Richard Hughes57d18222017-01-10 16:02:59 +0000475/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000476 * fu_plugin_request_recoldplug:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100477 * @self: A #FuPlugin
Richard Hughes362d6d72017-01-07 21:42:14 +0000478 *
479 * Ask all the plugins to coldplug all devices, which will include the prepare()
480 * and cleanup() phases. Duplicate devices added will be ignored.
481 *
482 * Since: 0.8.0
483 **/
484void
Richard Hughes12724852018-09-04 13:53:44 +0100485fu_plugin_request_recoldplug (FuPlugin *self)
Richard Hughes362d6d72017-01-07 21:42:14 +0000486{
Richard Hughes12724852018-09-04 13:53:44 +0100487 g_return_if_fail (FU_IS_PLUGIN (self));
488 g_signal_emit (self, signals[SIGNAL_RECOLDPLUG], 0);
Richard Hughes362d6d72017-01-07 21:42:14 +0000489}
490
Richard Hughesb0829032017-01-10 09:27:08 +0000491/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100492 * fu_plugin_check_hwid:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100493 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100494 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100495 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100496 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100497 * specific system can be shown using the `fwupdmgr hwids` command.
498 *
Richard Hughes4eada342017-10-03 21:20:32 +0100499 * Returns: %TRUE if the HwId is found on the system.
500 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100501 * Since: 0.9.1
502 **/
503gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100504fu_plugin_check_hwid (FuPlugin *self, const gchar *hwid)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100505{
Richard Hughes12724852018-09-04 13:53:44 +0100506 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100507 if (priv->hwids == NULL)
508 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100509 return fu_hwids_has_guid (priv->hwids, hwid);
510}
511
512/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100513 * fu_plugin_get_hwids:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100514 * @self: A #FuPlugin
Richard Hughes69a5f352018-08-08 11:58:15 +0100515 *
516 * Returns all the HWIDs defined in the system. All hardware IDs on a
517 * specific system can be shown using the `fwupdmgr hwids` command.
518 *
519 * Returns: (transfer none) (element-type utf-8): An array of GUIDs
520 *
521 * Since: 1.1.1
522 **/
523GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +0100524fu_plugin_get_hwids (FuPlugin *self)
Richard Hughes69a5f352018-08-08 11:58:15 +0100525{
Richard Hughes12724852018-09-04 13:53:44 +0100526 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes69a5f352018-08-08 11:58:15 +0100527 if (priv->hwids == NULL)
528 return NULL;
529 return fu_hwids_get_guids (priv->hwids);
530}
531
532/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100533 * fu_plugin_check_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100534 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100535 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100536 *
537 * Checks to see if a specific device GUID is supported, i.e. available in the
538 * AppStream metadata.
539 *
Richard Hughes4eada342017-10-03 21:20:32 +0100540 * Returns: %TRUE if the device is supported.
541 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100542 * Since: 1.0.0
543 **/
544gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100545fu_plugin_check_supported (FuPlugin *self, const gchar *guid)
Richard Hughes1354ea92017-09-19 15:58:31 +0100546{
Richard Hughesaabdc372018-11-14 10:11:08 +0000547 gboolean retval = FALSE;
548 g_signal_emit (self, signals[SIGNAL_CHECK_SUPPORTED], 0, guid, &retval);
549 return retval;
Richard Hughes1354ea92017-09-19 15:58:31 +0100550}
551
552/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100553 * fu_plugin_get_dmi_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100554 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100555 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100556 *
557 * Gets a hardware DMI value.
558 *
Richard Hughes4eada342017-10-03 21:20:32 +0100559 * Returns: The string, or %NULL
560 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100561 * Since: 0.9.7
562 **/
563const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100564fu_plugin_get_dmi_value (FuPlugin *self, const gchar *dmi_id)
Richard Hughesd7704d42017-08-08 20:29:09 +0100565{
Richard Hughes12724852018-09-04 13:53:44 +0100566 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100567 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100568 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100569 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100570}
571
Richard Hughes49e5e052017-09-03 12:15:41 +0100572/**
573 * fu_plugin_get_smbios_string:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100574 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100575 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
576 * @offset: A SMBIOS offset
577 *
578 * Gets a hardware SMBIOS string.
579 *
580 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
581 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
582 *
Richard Hughes4eada342017-10-03 21:20:32 +0100583 * Returns: A string, or %NULL
584 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100585 * Since: 0.9.8
586 **/
587const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100588fu_plugin_get_smbios_string (FuPlugin *self, guint8 structure_type, guint8 offset)
Richard Hughes49e5e052017-09-03 12:15:41 +0100589{
Richard Hughes12724852018-09-04 13:53:44 +0100590 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100591 if (priv->smbios == NULL)
592 return NULL;
593 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
594}
595
596/**
597 * fu_plugin_get_smbios_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100598 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100599 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
600 *
601 * Gets a hardware SMBIOS data.
602 *
Richard Hughes4eada342017-10-03 21:20:32 +0100603 * Returns: (transfer none): A #GBytes, or %NULL
604 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100605 * Since: 0.9.8
606 **/
607GBytes *
Richard Hughes12724852018-09-04 13:53:44 +0100608fu_plugin_get_smbios_data (FuPlugin *self, guint8 structure_type)
Richard Hughes49e5e052017-09-03 12:15:41 +0100609{
Richard Hughes12724852018-09-04 13:53:44 +0100610 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100611 if (priv->smbios == NULL)
612 return NULL;
613 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
614}
615
Richard Hughesb8f8db22017-04-25 15:56:00 +0100616void
Richard Hughes12724852018-09-04 13:53:44 +0100617fu_plugin_set_hwids (FuPlugin *self, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100618{
Richard Hughes12724852018-09-04 13:53:44 +0100619 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100620 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100621}
622
Richard Hughes49e5e052017-09-03 12:15:41 +0100623void
Richard Hughes12724852018-09-04 13:53:44 +0100624fu_plugin_set_udev_subsystems (FuPlugin *self, GPtrArray *udev_subsystems)
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100625{
Richard Hughes12724852018-09-04 13:53:44 +0100626 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100627 if (priv->udev_subsystems != NULL)
628 g_ptr_array_unref (priv->udev_subsystems);
629 priv->udev_subsystems = g_ptr_array_ref (udev_subsystems);
630}
631
632void
Richard Hughes12724852018-09-04 13:53:44 +0100633fu_plugin_set_quirks (FuPlugin *self, FuQuirks *quirks)
Richard Hughes9c028f02017-10-28 21:14:28 +0100634{
Richard Hughes12724852018-09-04 13:53:44 +0100635 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100636 g_set_object (&priv->quirks, quirks);
637}
638
639/**
640 * fu_plugin_get_quirks:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100641 * @self: A #FuPlugin
Richard Hughes9c028f02017-10-28 21:14:28 +0100642 *
643 * Returns the hardware database object. This can be used to discover device
644 * quirks or other device-specific settings.
645 *
646 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
647 *
648 * Since: 1.0.1
649 **/
650FuQuirks *
Richard Hughes12724852018-09-04 13:53:44 +0100651fu_plugin_get_quirks (FuPlugin *self)
Richard Hughes9c028f02017-10-28 21:14:28 +0100652{
Richard Hughes12724852018-09-04 13:53:44 +0100653 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100654 return priv->quirks;
655}
656
Richard Hughes0eb123b2018-04-19 12:00:04 +0100657void
Richard Hughes12724852018-09-04 13:53:44 +0100658fu_plugin_set_runtime_versions (FuPlugin *self, GHashTable *runtime_versions)
Richard Hughes0eb123b2018-04-19 12:00:04 +0100659{
Richard Hughes12724852018-09-04 13:53:44 +0100660 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0eb123b2018-04-19 12:00:04 +0100661 priv->runtime_versions = g_hash_table_ref (runtime_versions);
662}
663
664/**
665 * fu_plugin_add_runtime_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100666 * @self: A #FuPlugin
Richard Hughes0eb123b2018-04-19 12:00:04 +0100667 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
668 * @version: A version string, e.g. "1.2.3"
669 *
670 * Sets a runtime version of a specific dependancy.
671 *
672 * Since: 1.0.7
673 **/
674void
Richard Hughes12724852018-09-04 13:53:44 +0100675fu_plugin_add_runtime_version (FuPlugin *self,
Richard Hughes0eb123b2018-04-19 12:00:04 +0100676 const gchar *component_id,
677 const gchar *version)
678{
Richard Hughes12724852018-09-04 13:53:44 +0100679 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb01b4862018-04-20 16:39:48 +0100680 if (priv->runtime_versions == NULL)
681 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100682 g_hash_table_insert (priv->runtime_versions,
683 g_strdup (component_id),
684 g_strdup (version));
685}
686
Richard Hughes34e0dab2018-04-20 16:43:00 +0100687void
Richard Hughes12724852018-09-04 13:53:44 +0100688fu_plugin_set_compile_versions (FuPlugin *self, GHashTable *compile_versions)
Richard Hughes34e0dab2018-04-20 16:43:00 +0100689{
Richard Hughes12724852018-09-04 13:53:44 +0100690 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100691 priv->compile_versions = g_hash_table_ref (compile_versions);
692}
693
694/**
695 * fu_plugin_add_compile_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100696 * @self: A #FuPlugin
Richard Hughes34e0dab2018-04-20 16:43:00 +0100697 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
698 * @version: A version string, e.g. "1.2.3"
699 *
700 * Sets a compile-time version of a specific dependancy.
701 *
702 * Since: 1.0.7
703 **/
704void
Richard Hughes12724852018-09-04 13:53:44 +0100705fu_plugin_add_compile_version (FuPlugin *self,
Richard Hughes34e0dab2018-04-20 16:43:00 +0100706 const gchar *component_id,
707 const gchar *version)
708{
Richard Hughes12724852018-09-04 13:53:44 +0100709 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100710 if (priv->compile_versions == NULL)
711 return;
712 g_hash_table_insert (priv->compile_versions,
713 g_strdup (component_id),
714 g_strdup (version));
715}
716
Richard Hughes9c028f02017-10-28 21:14:28 +0100717/**
718 * fu_plugin_lookup_quirk_by_id:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100719 * @self: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100720 * @group: A string, e.g. "DfuFlags"
721 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100722 *
723 * Looks up an entry in the hardware database using a string value.
724 *
725 * Returns: (transfer none): values from the database, or %NULL if not found
726 *
727 * Since: 1.0.1
728 **/
729const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100730fu_plugin_lookup_quirk_by_id (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100731{
Richard Hughes12724852018-09-04 13:53:44 +0100732 FuPluginPrivate *priv = GET_PRIVATE (self);
733 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes9c028f02017-10-28 21:14:28 +0100734
Richard Hughes9c028f02017-10-28 21:14:28 +0100735 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100736 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100737}
738
739/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100740 * fu_plugin_lookup_quirk_by_id_as_uint64:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100741 * @self: A #FuPlugin
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100742 * @group: A string, e.g. "DfuFlags"
743 * @key: An ID to match the entry, e.g. "Size"
744 *
745 * Looks up an entry in the hardware database using a string key, returning
746 * an integer value. Values are assumed base 10, unless prefixed with "0x"
747 * where they are parsed as base 16.
748 *
749 * Returns: (transfer none): value from the database, or 0 if not found
750 *
751 * Since: 1.1.2
752 **/
753guint64
Richard Hughes12724852018-09-04 13:53:44 +0100754fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100755{
Richard Hughes12724852018-09-04 13:53:44 +0100756 return fu_common_strtoull (fu_plugin_lookup_quirk_by_id (self, group, key));
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100757}
758
Richard Hughes1354ea92017-09-19 15:58:31 +0100759void
Richard Hughes12724852018-09-04 13:53:44 +0100760fu_plugin_set_smbios (FuPlugin *self, FuSmbios *smbios)
Richard Hughes49e5e052017-09-03 12:15:41 +0100761{
Richard Hughes12724852018-09-04 13:53:44 +0100762 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100763 g_set_object (&priv->smbios, smbios);
764}
765
Richard Hughesb8f8db22017-04-25 15:56:00 +0100766/**
Richard Hughesb0829032017-01-10 09:27:08 +0000767 * fu_plugin_set_coldplug_delay:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100768 * @self: A #FuPlugin
Richard Hughesb0829032017-01-10 09:27:08 +0000769 * @duration: A delay in milliseconds
770 *
771 * Set the minimum time that should be waited inbetween the call to
772 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
773 * to be the minimum hardware initialisation time from a datasheet.
774 *
775 * It is better to use this function rather than using a sleep() in the plugin
776 * itself as then only one delay is done in the daemon rather than waiting for
777 * each coldplug prepare in a serial way.
778 *
779 * Additionally, very long delays should be avoided as the daemon will be
780 * blocked from processing requests whilst the coldplug delay is being
781 * performed.
782 *
783 * Since: 0.8.0
784 **/
785void
Richard Hughes12724852018-09-04 13:53:44 +0100786fu_plugin_set_coldplug_delay (FuPlugin *self, guint duration)
Richard Hughesb0829032017-01-10 09:27:08 +0000787{
Richard Hughes12724852018-09-04 13:53:44 +0100788 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesb0829032017-01-10 09:27:08 +0000789 g_return_if_fail (duration > 0);
790
791 /* check sanity */
792 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
793 g_warning ("duration of %ums is crazy, truncating to %ums",
794 duration,
795 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
796 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
797 }
798
799 /* emit */
Richard Hughes12724852018-09-04 13:53:44 +0100800 g_signal_emit (self, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
Richard Hughesb0829032017-01-10 09:27:08 +0000801}
802
Richard Hughesd0905142016-03-13 09:46:49 +0000803gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100804fu_plugin_runner_startup (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000805{
Richard Hughes12724852018-09-04 13:53:44 +0100806 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000807 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000808 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000809
810 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000811 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000812 return TRUE;
813
Richard Hughes639da472018-01-06 22:35:04 +0000814 /* no object loaded */
815 if (priv->module == NULL)
816 return TRUE;
817
Richard Hughesd0905142016-03-13 09:46:49 +0000818 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000819 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
820 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000821 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000822 g_debug ("performing startup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000823 if (!func (self, &error_local)) {
824 if (error_local == NULL) {
825 g_critical ("unset error in plugin %s for startup()",
826 priv->name);
827 g_set_error_literal (&error_local,
828 FWUPD_ERROR,
829 FWUPD_ERROR_INTERNAL,
830 "unspecified error");
831 }
832 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
833 "failed to startup using %s: ",
834 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +0000835 return FALSE;
836 }
837 return TRUE;
838}
839
840static gboolean
841fu_plugin_runner_offline_invalidate (GError **error)
842{
843 g_autoptr(GError) error_local = NULL;
844 g_autoptr(GFile) file1 = NULL;
845
846 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
847
848 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
849 if (!g_file_query_exists (file1, NULL))
850 return TRUE;
851 if (!g_file_delete (file1, NULL, &error_local)) {
852 g_set_error (error,
853 FWUPD_ERROR,
854 FWUPD_ERROR_INTERNAL,
855 "Cannot delete %s: %s",
856 FU_OFFLINE_TRIGGER_FILENAME,
857 error_local->message);
858 return FALSE;
859 }
860 return TRUE;
861}
862
863static gboolean
864fu_plugin_runner_offline_setup (GError **error)
865{
866 gint rc;
867
868 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
869
870 /* create symlink for the systemd-system-update-generator */
871 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
872 if (rc < 0) {
873 g_set_error (error,
874 FWUPD_ERROR,
875 FWUPD_ERROR_INTERNAL,
876 "Failed to create symlink %s to %s: %s",
877 FU_OFFLINE_TRIGGER_FILENAME,
878 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000879 return FALSE;
880 }
881 return TRUE;
882}
883
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000884static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100885fu_plugin_runner_device_generic (FuPlugin *self, FuDevice *device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000886 const gchar *symbol_name, GError **error)
887{
Richard Hughes12724852018-09-04 13:53:44 +0100888 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000889 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000890 g_autoptr(GError) error_local = NULL;
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000891
892 /* not enabled */
893 if (!priv->enabled)
894 return TRUE;
895
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000896 /* no object loaded */
897 if (priv->module == NULL)
898 return TRUE;
899
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000900 /* optional */
901 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
902 if (func == NULL)
903 return TRUE;
904 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000905 if (!func (self, device, &error_local)) {
906 if (error_local == NULL) {
907 g_critical ("unset error in plugin %s for %s()",
908 priv->name, symbol_name + 10);
909 g_set_error_literal (&error_local,
910 FWUPD_ERROR,
911 FWUPD_ERROR_INTERNAL,
912 "unspecified error");
913 }
914 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
915 "failed to %s using %s: ",
916 symbol_name + 10, priv->name);
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000917 return FALSE;
918 }
919 return TRUE;
920}
921
Richard Hughesdbd8c762018-06-15 20:31:40 +0100922static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100923fu_plugin_runner_flagged_device_generic (FuPlugin *self, FwupdInstallFlags flags,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500924 FuDevice *device,
925 const gchar *symbol_name, GError **error)
926{
Richard Hughes12724852018-09-04 13:53:44 +0100927 FuPluginPrivate *priv = GET_PRIVATE (self);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500928 FuPluginFlaggedDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000929 g_autoptr(GError) error_local = NULL;
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500930
931 /* not enabled */
932 if (!priv->enabled)
933 return TRUE;
934
935 /* no object loaded */
936 if (priv->module == NULL)
937 return TRUE;
938
939 /* optional */
940 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
941 if (func == NULL)
942 return TRUE;
943 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000944 if (!func (self, flags, device, &error_local)) {
945 if (error_local == NULL) {
946 g_critical ("unset error in plugin %s for %s()",
947 priv->name, symbol_name + 10);
948 g_set_error_literal (&error_local,
949 FWUPD_ERROR,
950 FWUPD_ERROR_INTERNAL,
951 "unspecified error");
952 }
953 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
954 "failed to %s using %s: ",
955 symbol_name + 10, priv->name);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500956 return FALSE;
957 }
958 return TRUE;
959
960}
961
962static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100963fu_plugin_runner_device_array_generic (FuPlugin *self, GPtrArray *devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +0100964 const gchar *symbol_name, GError **error)
965{
Richard Hughes12724852018-09-04 13:53:44 +0100966 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesdbd8c762018-06-15 20:31:40 +0100967 FuPluginDeviceArrayFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000968 g_autoptr(GError) error_local = NULL;
Richard Hughesdbd8c762018-06-15 20:31:40 +0100969
970 /* not enabled */
971 if (!priv->enabled)
972 return TRUE;
973
974 /* no object loaded */
975 if (priv->module == NULL)
976 return TRUE;
977
978 /* optional */
979 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
980 if (func == NULL)
981 return TRUE;
982 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000983 if (!func (self, devices, &error_local)) {
984 if (error_local == NULL) {
985 g_critical ("unset error in plugin %s for %s()",
986 priv->name, symbol_name + 10);
987 g_set_error_literal (&error_local,
988 FWUPD_ERROR,
989 FWUPD_ERROR_INTERNAL,
990 "unspecified error");
991 }
992 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
993 "failed to %s using %s: ",
994 symbol_name + 10, priv->name);
Richard Hughesdbd8c762018-06-15 20:31:40 +0100995 return FALSE;
996 }
997 return TRUE;
998}
999
Richard Hughesd0905142016-03-13 09:46:49 +00001000gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001001fu_plugin_runner_coldplug (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001002{
Richard Hughes12724852018-09-04 13:53:44 +01001003 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001004 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001005 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +00001006
1007 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +00001008 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +00001009 return TRUE;
1010
Richard Hughes639da472018-01-06 22:35:04 +00001011 /* no object loaded */
1012 if (priv->module == NULL)
1013 return TRUE;
1014
Richard Hughesd0905142016-03-13 09:46:49 +00001015 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001016 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
1017 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001018 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001019 g_debug ("performing coldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001020 if (!func (self, &error_local)) {
1021 if (error_local == NULL) {
1022 g_critical ("unset error in plugin %s for coldplug()",
1023 priv->name);
1024 g_set_error_literal (&error_local,
1025 FWUPD_ERROR,
1026 FWUPD_ERROR_INTERNAL,
1027 "unspecified error");
1028 }
1029 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1030 "failed to coldplug using %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001031 return FALSE;
1032 }
1033 return TRUE;
1034}
1035
Richard Hughes7b8b2022016-12-12 16:15:03 +00001036gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001037fu_plugin_runner_recoldplug (FuPlugin *self, GError **error)
Richard Hughes2de8f132018-01-17 09:12:02 +00001038{
Richard Hughes12724852018-09-04 13:53:44 +01001039 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes2de8f132018-01-17 09:12:02 +00001040 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001041 g_autoptr(GError) error_local = NULL;
Richard Hughes2de8f132018-01-17 09:12:02 +00001042
1043 /* not enabled */
1044 if (!priv->enabled)
1045 return TRUE;
1046
1047 /* no object loaded */
1048 if (priv->module == NULL)
1049 return TRUE;
1050
1051 /* optional */
1052 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1053 if (func == NULL)
1054 return TRUE;
1055 g_debug ("performing recoldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001056 if (!func (self, &error_local)) {
1057 if (error_local == NULL) {
1058 g_critical ("unset error in plugin %s for recoldplug()",
1059 priv->name);
1060 g_set_error_literal (&error_local,
1061 FWUPD_ERROR,
1062 FWUPD_ERROR_INTERNAL,
1063 "unspecified error");
1064 }
1065 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1066 "failed to recoldplug using %s: ",
1067 priv->name);
Richard Hughes2de8f132018-01-17 09:12:02 +00001068 return FALSE;
1069 }
1070 return TRUE;
1071}
1072
1073gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001074fu_plugin_runner_coldplug_prepare (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001075{
Richard Hughes12724852018-09-04 13:53:44 +01001076 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001077 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001078 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001079
1080 /* not enabled */
1081 if (!priv->enabled)
1082 return TRUE;
1083
Richard Hughes639da472018-01-06 22:35:04 +00001084 /* no object loaded */
1085 if (priv->module == NULL)
1086 return TRUE;
1087
Richard Hughes46487c92017-01-07 21:26:34 +00001088 /* optional */
1089 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1090 if (func == NULL)
1091 return TRUE;
1092 g_debug ("performing coldplug_prepare() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001093 if (!func (self, &error_local)) {
1094 if (error_local == NULL) {
1095 g_critical ("unset error in plugin %s for coldplug_prepare()",
1096 priv->name);
1097 g_set_error_literal (&error_local,
1098 FWUPD_ERROR,
1099 FWUPD_ERROR_INTERNAL,
1100 "unspecified error");
1101 }
1102 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1103 "failed to coldplug_prepare using %s: ",
1104 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001105 return FALSE;
1106 }
1107 return TRUE;
1108}
1109
1110gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001111fu_plugin_runner_coldplug_cleanup (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001112{
Richard Hughes12724852018-09-04 13:53:44 +01001113 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001114 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001115 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001116
1117 /* not enabled */
1118 if (!priv->enabled)
1119 return TRUE;
1120
Richard Hughes639da472018-01-06 22:35:04 +00001121 /* no object loaded */
1122 if (priv->module == NULL)
1123 return TRUE;
1124
Richard Hughes46487c92017-01-07 21:26:34 +00001125 /* optional */
1126 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1127 if (func == NULL)
1128 return TRUE;
1129 g_debug ("performing coldplug_cleanup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001130 if (!func (self, &error_local)) {
1131 if (error_local == NULL) {
1132 g_critical ("unset error in plugin %s for coldplug_cleanup()",
1133 priv->name);
1134 g_set_error_literal (&error_local,
1135 FWUPD_ERROR,
1136 FWUPD_ERROR_INTERNAL,
1137 "unspecified error");
1138 }
1139 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1140 "failed to coldplug_cleanup using %s: ",
1141 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001142 return FALSE;
1143 }
1144 return TRUE;
1145}
1146
1147gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001148fu_plugin_runner_composite_prepare (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001149{
Richard Hughes12724852018-09-04 13:53:44 +01001150 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001151 "fu_plugin_composite_prepare",
1152 error);
1153}
1154
1155gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001156fu_plugin_runner_composite_cleanup (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001157{
Richard Hughes12724852018-09-04 13:53:44 +01001158 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001159 "fu_plugin_composite_cleanup",
1160 error);
1161}
1162
1163gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001164fu_plugin_runner_update_prepare (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001165 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001166{
Richard Hughes12724852018-09-04 13:53:44 +01001167 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001168 "fu_plugin_update_prepare",
1169 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001170}
1171
1172gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001173fu_plugin_runner_update_cleanup (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001174 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001175{
Richard Hughes12724852018-09-04 13:53:44 +01001176 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001177 "fu_plugin_update_cleanup",
1178 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001179}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001180
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001181gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001182fu_plugin_runner_update_attach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001183{
Richard Hughes12724852018-09-04 13:53:44 +01001184 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001185 "fu_plugin_update_attach", error);
1186}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001187
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001188gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001189fu_plugin_runner_update_detach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001190{
Richard Hughes12724852018-09-04 13:53:44 +01001191 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001192 "fu_plugin_update_detach", error);
1193}
1194
1195gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001196fu_plugin_runner_update_reload (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001197{
Richard Hughes12724852018-09-04 13:53:44 +01001198 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001199 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001200}
1201
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001202/**
1203 * fu_plugin_add_udev_subsystem:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001204 * @self: a #FuPlugin
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001205 * @subsystem: a subsystem name, e.g. `pciport`
1206 *
1207 * Registers the udev subsystem to be watched by the daemon.
1208 *
1209 * Plugins can use this method only in fu_plugin_init()
1210 **/
1211void
Richard Hughes12724852018-09-04 13:53:44 +01001212fu_plugin_add_udev_subsystem (FuPlugin *self, const gchar *subsystem)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001213{
Richard Hughes12724852018-09-04 13:53:44 +01001214 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001215 for (guint i = 0; i < priv->udev_subsystems->len; i++) {
1216 const gchar *subsystem_tmp = g_ptr_array_index (priv->udev_subsystems, i);
1217 if (g_strcmp0 (subsystem_tmp, subsystem) == 0)
1218 return;
1219 }
1220 g_debug ("added udev subsystem watch of %s", subsystem);
1221 g_ptr_array_add (priv->udev_subsystems, g_strdup (subsystem));
1222}
1223
Richard Hughes104f6512017-11-24 11:44:57 +00001224gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001225fu_plugin_runner_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
Richard Hughes104f6512017-11-24 11:44:57 +00001226{
Richard Hughes12724852018-09-04 13:53:44 +01001227 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes104f6512017-11-24 11:44:57 +00001228 FuPluginUsbDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001229 g_autoptr(GError) error_local = NULL;
Richard Hughes104f6512017-11-24 11:44:57 +00001230
1231 /* not enabled */
1232 if (!priv->enabled)
1233 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001234
1235 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001236 if (priv->module == NULL)
1237 return TRUE;
1238
1239 /* optional */
1240 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001241 if (func == NULL)
1242 return TRUE;
1243 g_debug ("performing usb_device_added() on %s", priv->name);
1244 if (!func (self, device, &error_local)) {
1245 if (error_local == NULL) {
1246 g_critical ("unset error in plugin %s for usb_device_added()",
1247 priv->name);
1248 g_set_error_literal (&error_local,
1249 FWUPD_ERROR,
1250 FWUPD_ERROR_INTERNAL,
1251 "unspecified error");
1252 }
1253 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1254 "failed to add device using on %s: ",
1255 priv->name);
1256 return FALSE;
Richard Hughes104f6512017-11-24 11:44:57 +00001257 }
1258 return TRUE;
1259}
1260
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001261gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001262fu_plugin_runner_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001263{
Richard Hughes12724852018-09-04 13:53:44 +01001264 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001265 FuPluginUdevDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001266 g_autoptr(GError) error_local = NULL;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001267
1268 /* not enabled */
1269 if (!priv->enabled)
1270 return TRUE;
1271
1272 /* no object loaded */
1273 if (priv->module == NULL)
1274 return TRUE;
1275
1276 /* optional */
1277 g_module_symbol (priv->module, "fu_plugin_udev_device_added", (gpointer *) &func);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001278 if (func == NULL)
1279 return TRUE;
1280 g_debug ("performing udev_device_added() on %s", priv->name);
1281 if (!func (self, device, &error_local)) {
1282 if (error_local == NULL) {
1283 g_critical ("unset error in plugin %s for udev_device_added()",
1284 priv->name);
1285 g_set_error_literal (&error_local,
1286 FWUPD_ERROR,
1287 FWUPD_ERROR_INTERNAL,
1288 "unspecified error");
1289 }
1290 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1291 "failed to add device using on %s: ",
1292 priv->name);
1293 return FALSE;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001294 }
1295 return TRUE;
1296}
1297
Richard Hughese1fd34d2017-08-24 14:19:51 +01001298void
Richard Hughes12724852018-09-04 13:53:44 +01001299fu_plugin_runner_device_removed (FuPlugin *self, FuDevice *device)
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001300{
1301 g_autoptr(GError) error_local= NULL;
1302
Richard Hughes12724852018-09-04 13:53:44 +01001303 if (!fu_plugin_runner_device_generic (self, device,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001304 "fu_plugin_device_removed",
1305 &error_local))
1306 g_warning ("%s", error_local->message);
1307}
1308
1309void
Richard Hughes12724852018-09-04 13:53:44 +01001310fu_plugin_runner_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +01001311{
Richard Hughes12724852018-09-04 13:53:44 +01001312 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001313 FuPluginDeviceRegisterFunc func = NULL;
1314
1315 /* not enabled */
1316 if (!priv->enabled)
1317 return;
Richard Hughes34834102017-11-21 21:55:00 +00001318 if (priv->module == NULL)
1319 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001320
Mario Limonciello4910b242018-06-22 15:04:21 -05001321 /* don't notify plugins on their own devices */
Richard Hughes12724852018-09-04 13:53:44 +01001322 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (self)) == 0)
Mario Limonciello4910b242018-06-22 15:04:21 -05001323 return;
1324
Richard Hughese1fd34d2017-08-24 14:19:51 +01001325 /* optional */
1326 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1327 if (func != NULL) {
Richard Hughes1bf7ff92018-08-24 20:21:35 +01001328 g_debug ("performing fu_plugin_device_registered() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001329 func (self, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001330 }
1331}
1332
Richard Hughescff38bc2016-12-12 12:03:37 +00001333static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001334fu_plugin_runner_schedule_update (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001335 FuDevice *device,
1336 GBytes *blob_cab,
1337 GError **error)
1338{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001339 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001340 gchar tmpname[] = {"XXXXXX.cap"};
1341 g_autofree gchar *dirname = NULL;
1342 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001343 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001344 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001345 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001346 g_autoptr(GFile) file = NULL;
1347
1348 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001349 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001350 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001351 if (res_tmp != NULL) {
1352 g_set_error (error,
1353 FWUPD_ERROR,
1354 FWUPD_ERROR_ALREADY_PENDING,
1355 "%s is already scheduled to be updated",
1356 fu_device_get_id (device));
1357 return FALSE;
1358 }
1359
1360 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001361 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001362 file = g_file_new_for_path (dirname);
1363 if (!g_file_query_exists (file, NULL)) {
1364 if (!g_file_make_directory_with_parents (file, NULL, error))
1365 return FALSE;
1366 }
1367
1368 /* get a random filename */
1369 for (guint i = 0; i < 6; i++)
1370 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1371 filename = g_build_filename (dirname, tmpname, NULL);
1372
1373 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001374 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001375 if (!g_file_set_contents (filename,
1376 g_bytes_get_data (blob_cab, NULL),
1377 (gssize) g_bytes_get_size (blob_cab),
1378 error))
1379 return FALSE;
1380
1381 /* schedule for next boot */
1382 g_debug ("schedule %s to be installed to %s on next boot",
1383 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001384 release = fu_device_get_release_default (device);
1385 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1386 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001387
1388 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001389 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001390 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001391 return FALSE;
1392
1393 /* next boot we run offline */
1394 return fu_plugin_runner_offline_setup (error);
1395}
1396
1397gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001398fu_plugin_runner_verify (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001399 FuDevice *device,
1400 FuPluginVerifyFlags flags,
1401 GError **error)
1402{
Richard Hughes12724852018-09-04 13:53:44 +01001403 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001404 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001405 GPtrArray *checksums;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001406 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001407
1408 /* not enabled */
1409 if (!priv->enabled)
1410 return TRUE;
1411
Richard Hughes639da472018-01-06 22:35:04 +00001412 /* no object loaded */
1413 if (priv->module == NULL)
1414 return TRUE;
1415
Richard Hughescff38bc2016-12-12 12:03:37 +00001416 /* optional */
1417 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1418 if (func == NULL)
1419 return TRUE;
Richard Hughes1812fc72018-12-14 11:37:54 +00001420
1421 /* clear any existing verification checksums */
1422 checksums = fu_device_get_checksums (device);
1423 g_ptr_array_set_size (checksums, 0);
1424
1425 /* run vfunc */
Richard Hughescff38bc2016-12-12 12:03:37 +00001426 g_debug ("performing verify() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001427 if (!func (self, device, flags, &error_local)) {
1428 if (error_local == NULL) {
1429 g_critical ("unset error in plugin %s for verify()",
1430 priv->name);
1431 g_set_error_literal (&error_local,
1432 FWUPD_ERROR,
1433 FWUPD_ERROR_INTERNAL,
1434 "unspecified error");
1435 }
1436 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1437 "failed to verify using %s: ",
1438 priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001439 return FALSE;
1440 }
1441 return TRUE;
1442}
1443
Richard Hughesd0905142016-03-13 09:46:49 +00001444gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001445fu_plugin_runner_unlock (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001446{
Richard Hughescff38bc2016-12-12 12:03:37 +00001447 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001448
1449 /* final check */
1450 flags = fu_device_get_flags (device);
1451 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1452 g_set_error (error,
1453 FWUPD_ERROR,
1454 FWUPD_ERROR_NOT_SUPPORTED,
1455 "Device %s is not locked",
1456 fu_device_get_id (device));
1457 return FALSE;
1458 }
1459
Richard Hughes9c4b5312017-11-14 11:34:53 +00001460 /* run vfunc */
Richard Hughes12724852018-09-04 13:53:44 +01001461 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes9c4b5312017-11-14 11:34:53 +00001462 "fu_plugin_unlock", error))
1463 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001464
1465 /* update with correct flags */
1466 flags = fu_device_get_flags (device);
1467 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1468 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1469 return TRUE;
1470}
1471
1472gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001473fu_plugin_runner_update (FuPlugin *self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001474 FuDevice *device,
1475 GBytes *blob_cab,
1476 GBytes *blob_fw,
1477 FwupdInstallFlags flags,
1478 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001479{
Richard Hughes12724852018-09-04 13:53:44 +01001480 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001481 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001482 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001483 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001484 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001485
1486 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001487 if (!priv->enabled) {
1488 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001489 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001490 }
Richard Hughesd0905142016-03-13 09:46:49 +00001491
Richard Hughes639da472018-01-06 22:35:04 +00001492 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001493 if (priv->module == NULL) {
1494 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001495 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001496 }
Richard Hughes639da472018-01-06 22:35:04 +00001497
Richard Hughesd0905142016-03-13 09:46:49 +00001498 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001499 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1500 if (update_func == NULL) {
1501 g_set_error_literal (error,
1502 FWUPD_ERROR,
1503 FWUPD_ERROR_NOT_SUPPORTED,
1504 "No update possible");
1505 return FALSE;
1506 }
Richard Hughesd0905142016-03-13 09:46:49 +00001507
Richard Hughesa785a1c2017-08-25 16:00:58 +01001508 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001509 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001510 if (blob_cab == NULL) {
1511 g_set_error_literal (error,
1512 FWUPD_ERROR,
1513 FWUPD_ERROR_NOT_SUPPORTED,
1514 "No cabinet archive to schedule");
1515 return FALSE;
1516 }
Richard Hughes12724852018-09-04 13:53:44 +01001517 return fu_plugin_runner_schedule_update (self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001518 device,
1519 blob_cab,
1520 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001521 }
1522
1523 /* cancel the pending action */
1524 if (!fu_plugin_runner_offline_invalidate (error))
1525 return FALSE;
1526
1527 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001528 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001529 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001530 if (!update_func (self, device, blob_fw, flags, &error_local)) {
1531 if (error_local == NULL) {
1532 g_critical ("unset error in plugin %s for update()",
1533 priv->name);
1534 g_set_error_literal (&error_local,
Richard Hughes3c8ada32018-10-12 10:08:58 +01001535 FWUPD_ERROR,
1536 FWUPD_ERROR_INTERNAL,
1537 "unspecified error");
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001538 return FALSE;
Richard Hughes3c8ada32018-10-12 10:08:58 +01001539 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001540 fu_device_set_update_error (device, error_local->message);
1541 g_propagate_error (error, g_steal_pointer (&error_local));
Richard Hughescff38bc2016-12-12 12:03:37 +00001542 return FALSE;
1543 }
1544
Richard Hughesf556d372017-06-15 19:49:18 +01001545 /* no longer valid */
Richard Hughesf8039642019-01-16 12:22:22 +00001546 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT) &&
1547 !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) {
Richard Hughes08435162018-12-12 10:34:16 +00001548 GPtrArray *checksums = fu_device_get_checksums (device);
1549 g_ptr_array_set_size (checksums, 0);
1550 }
Richard Hughesf556d372017-06-15 19:49:18 +01001551
Richard Hughescff38bc2016-12-12 12:03:37 +00001552 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001553 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001554 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001555 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001556
Richard Hughes780ef3f2018-01-12 16:20:31 +00001557 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001558 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1559 if (!fu_history_modify_device (history, device,
1560 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1561 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001562 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001563
1564 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001565 release = fu_device_get_release_default (device_pending);
1566 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001567 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001568 g_autoptr(GError) error_delete = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001569 g_autoptr(GFile) file = NULL;
1570 file = g_file_new_for_path (tmp);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001571 if (!g_file_delete (file, NULL, &error_delete)) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001572 g_set_error (error,
1573 FWUPD_ERROR,
1574 FWUPD_ERROR_INVALID_FILE,
1575 "Failed to delete %s: %s",
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001576 tmp, error_delete->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001577 return FALSE;
1578 }
1579 }
1580 }
Richard Hughesd0905142016-03-13 09:46:49 +00001581 return TRUE;
1582}
Richard Hughescff38bc2016-12-12 12:03:37 +00001583
1584gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001585fu_plugin_runner_clear_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001586{
Richard Hughes12724852018-09-04 13:53:44 +01001587 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001588 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001589 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001590
1591 /* not enabled */
1592 if (!priv->enabled)
1593 return TRUE;
1594
Richard Hughes639da472018-01-06 22:35:04 +00001595 /* no object loaded */
1596 if (priv->module == NULL)
1597 return TRUE;
1598
Richard Hughes65e44ca2018-01-30 17:26:30 +00001599 /* optional */
1600 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1601 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001602 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001603 g_debug ("performing clear_result() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001604 if (!func (self, device, &error_local)) {
1605 if (error_local == NULL) {
1606 g_critical ("unset error in plugin %s for clear_result()",
1607 priv->name);
1608 g_set_error_literal (&error_local,
1609 FWUPD_ERROR,
1610 FWUPD_ERROR_INTERNAL,
1611 "unspecified error");
1612 }
1613 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1614 "failed to clear_result using %s: ",
1615 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001616 return FALSE;
1617 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001618 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001619}
1620
1621gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001622fu_plugin_runner_get_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001623{
Richard Hughes12724852018-09-04 13:53:44 +01001624 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001625 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001626 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001627
1628 /* not enabled */
1629 if (!priv->enabled)
1630 return TRUE;
1631
Richard Hughes639da472018-01-06 22:35:04 +00001632 /* no object loaded */
1633 if (priv->module == NULL)
1634 return TRUE;
1635
Richard Hughes65e44ca2018-01-30 17:26:30 +00001636 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001637 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001638 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001639 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001640 g_debug ("performing get_results() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001641 if (!func (self, device, &error_local)) {
1642 if (error_local == NULL) {
1643 g_critical ("unset error in plugin %s for get_results()",
1644 priv->name);
1645 g_set_error_literal (&error_local,
1646 FWUPD_ERROR,
1647 FWUPD_ERROR_INTERNAL,
1648 "unspecified error");
1649 }
1650 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1651 "failed to get_results using %s: ",
1652 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001653 return FALSE;
1654 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001655 return TRUE;
1656}
1657
Richard Hughes08a37992017-09-12 12:57:43 +01001658/**
1659 * fu_plugin_get_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001660 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001661 *
1662 * Gets the plugin order, where higher numbers are run after lower
1663 * numbers.
1664 *
1665 * Returns: the integer value
1666 **/
1667guint
Richard Hughes12724852018-09-04 13:53:44 +01001668fu_plugin_get_order (FuPlugin *self)
Richard Hughes08a37992017-09-12 12:57:43 +01001669{
Richard Hughes12724852018-09-04 13:53:44 +01001670 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001671 return priv->order;
1672}
1673
1674/**
1675 * fu_plugin_set_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001676 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001677 * @order: a integer value
1678 *
1679 * Sets the plugin order, where higher numbers are run after lower
1680 * numbers.
1681 **/
1682void
Richard Hughes12724852018-09-04 13:53:44 +01001683fu_plugin_set_order (FuPlugin *self, guint order)
Richard Hughes08a37992017-09-12 12:57:43 +01001684{
Richard Hughes12724852018-09-04 13:53:44 +01001685 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001686 priv->order = order;
1687}
1688
1689/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001690 * fu_plugin_get_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001691 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001692 *
1693 * Gets the plugin priority, where higher numbers are better.
1694 *
1695 * Returns: the integer value
1696 **/
1697guint
Richard Hughes12724852018-09-04 13:53:44 +01001698fu_plugin_get_priority (FuPlugin *self)
Richard Hughes81c427c2018-08-06 15:20:17 +01001699{
Richard Hughes12724852018-09-04 13:53:44 +01001700 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001701 return priv->priority;
1702}
1703
1704/**
1705 * fu_plugin_set_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001706 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001707 * @priority: a integer value
1708 *
1709 * Sets the plugin priority, where higher numbers are better.
1710 **/
1711void
Richard Hughes12724852018-09-04 13:53:44 +01001712fu_plugin_set_priority (FuPlugin *self, guint priority)
Richard Hughes81c427c2018-08-06 15:20:17 +01001713{
Richard Hughes12724852018-09-04 13:53:44 +01001714 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001715 priv->priority = priority;
1716}
1717
1718/**
Richard Hughes08a37992017-09-12 12:57:43 +01001719 * fu_plugin_add_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001720 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001721 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001722 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001723 *
1724 * If the plugin name is found, the rule will be used to sort the plugin list,
1725 * for example the plugin specified by @name will be ordered after this plugin
1726 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1727 *
1728 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1729 * If depsolving fails then fwupd will not start.
1730 **/
1731void
Richard Hughes12724852018-09-04 13:53:44 +01001732fu_plugin_add_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes08a37992017-09-12 12:57:43 +01001733{
Richard Hughes12724852018-09-04 13:53:44 +01001734 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001735 g_ptr_array_add (priv->rules[rule], g_strdup (name));
Richard Hughes75b965d2018-11-15 13:51:21 +00001736 g_signal_emit (self, signals[SIGNAL_RULES_CHANGED], 0);
Richard Hughes08a37992017-09-12 12:57:43 +01001737}
1738
1739/**
1740 * fu_plugin_get_rules:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001741 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001742 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1743 *
1744 * Gets the plugin IDs that should be run after this plugin.
1745 *
1746 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1747 **/
1748GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +01001749fu_plugin_get_rules (FuPlugin *self, FuPluginRule rule)
Richard Hughes08a37992017-09-12 12:57:43 +01001750{
Richard Hughes12724852018-09-04 13:53:44 +01001751 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001752 return priv->rules[rule];
1753}
1754
Richard Hughes80b79bb2018-01-11 21:11:06 +00001755/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001756 * fu_plugin_has_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001757 * @self: a #FuPlugin
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001758 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1759 * @name: a plugin name, e.g. `upower`
1760 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001761 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001762 *
1763 * Returns: %TRUE if the name exists for the specific rule
1764 **/
1765gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001766fu_plugin_has_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001767{
Richard Hughes12724852018-09-04 13:53:44 +01001768 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001769 for (guint i = 0; i < priv->rules[rule]->len; i++) {
1770 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
1771 if (g_strcmp0 (tmp, name) == 0)
1772 return TRUE;
1773 }
1774 return FALSE;
1775}
1776
1777/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00001778 * fu_plugin_add_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001779 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00001780 * @key: a string, e.g. `FwupdateVersion`
1781 * @value: a string, e.g. `10`
1782 *
1783 * Sets any additional metadata to be included in the firmware report to aid
1784 * debugging problems.
1785 *
1786 * Any data included here will be sent to the metadata server after user
1787 * confirmation.
1788 **/
1789void
Richard Hughes12724852018-09-04 13:53:44 +01001790fu_plugin_add_report_metadata (FuPlugin *self, const gchar *key, const gchar *value)
Richard Hughes80b79bb2018-01-11 21:11:06 +00001791{
Richard Hughes12724852018-09-04 13:53:44 +01001792 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001793 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1794}
1795
1796/**
1797 * fu_plugin_get_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001798 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00001799 *
1800 * Returns the list of additional metadata to be added when filing a report.
1801 *
1802 * Returns: (transfer none): the map of report metadata
1803 **/
1804GHashTable *
Richard Hughes12724852018-09-04 13:53:44 +01001805fu_plugin_get_report_metadata (FuPlugin *self)
Richard Hughes80b79bb2018-01-11 21:11:06 +00001806{
Richard Hughes12724852018-09-04 13:53:44 +01001807 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001808 return priv->report_metadata;
1809}
1810
Mario Limonciello963dc422018-02-27 14:26:58 -06001811/**
1812 * fu_plugin_get_config_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001813 * @self: a #FuPlugin
Mario Limonciello963dc422018-02-27 14:26:58 -06001814 * @key: A settings key
1815 *
1816 * Return the value of a key if it's been configured
1817 *
1818 * Since: 1.0.6
1819 **/
1820gchar *
Richard Hughes12724852018-09-04 13:53:44 +01001821fu_plugin_get_config_value (FuPlugin *self, const gchar *key)
Mario Limonciello963dc422018-02-27 14:26:58 -06001822{
Richard Hughes4be17d12018-05-30 20:36:29 +01001823 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06001824 g_autofree gchar *conf_file = NULL;
1825 g_autofree gchar *conf_path = NULL;
1826 g_autoptr(GKeyFile) keyfile = NULL;
1827 const gchar *plugin_name;
1828
Richard Hughes4be17d12018-05-30 20:36:29 +01001829 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Richard Hughes12724852018-09-04 13:53:44 +01001830 plugin_name = fu_plugin_get_name (self);
Mario Limonciello963dc422018-02-27 14:26:58 -06001831 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01001832 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06001833 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1834 return NULL;
1835 keyfile = g_key_file_new ();
1836 if (!g_key_file_load_from_file (keyfile, conf_path,
1837 G_KEY_FILE_NONE, NULL))
1838 return NULL;
1839 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1840}
1841
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001842/**
1843 * fu_plugin_name_compare:
1844 * @plugin1: first #FuPlugin to compare.
1845 * @plugin2: second #FuPlugin to compare.
1846 *
1847 * Compares two plugins by their names.
1848 *
1849 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1850 **/
1851gint
1852fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1853{
1854 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1855 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1856 return g_strcmp0 (priv1->name, priv2->name);
1857}
1858
1859/**
1860 * fu_plugin_order_compare:
1861 * @plugin1: first #FuPlugin to compare.
1862 * @plugin2: second #FuPlugin to compare.
1863 *
1864 * Compares two plugins by their depsolved order.
1865 *
1866 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1867 **/
1868gint
1869fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1870{
1871 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1872 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1873 if (priv1->order < priv2->order)
1874 return -1;
1875 if (priv1->order > priv2->order)
1876 return 1;
1877 return 0;
1878}
1879
Richard Hughescff38bc2016-12-12 12:03:37 +00001880static void
1881fu_plugin_class_init (FuPluginClass *klass)
1882{
1883 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1884 object_class->finalize = fu_plugin_finalize;
1885 signals[SIGNAL_DEVICE_ADDED] =
1886 g_signal_new ("device-added",
1887 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1888 G_STRUCT_OFFSET (FuPluginClass, device_added),
1889 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1890 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1891 signals[SIGNAL_DEVICE_REMOVED] =
1892 g_signal_new ("device-removed",
1893 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1894 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1895 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1896 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001897 signals[SIGNAL_DEVICE_REGISTER] =
1898 g_signal_new ("device-register",
1899 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1900 G_STRUCT_OFFSET (FuPluginClass, device_register),
1901 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1902 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001903 signals[SIGNAL_RECOLDPLUG] =
1904 g_signal_new ("recoldplug",
1905 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1906 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1907 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1908 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001909 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1910 g_signal_new ("set-coldplug-delay",
1911 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1912 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1913 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1914 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughesaabdc372018-11-14 10:11:08 +00001915 signals[SIGNAL_CHECK_SUPPORTED] =
1916 g_signal_new ("check-supported",
1917 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1918 G_STRUCT_OFFSET (FuPluginClass, check_supported),
1919 NULL, NULL, g_cclosure_marshal_generic,
1920 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
Richard Hughes75b965d2018-11-15 13:51:21 +00001921 signals[SIGNAL_RULES_CHANGED] =
1922 g_signal_new ("rules-changed",
1923 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1924 G_STRUCT_OFFSET (FuPluginClass, rules_changed),
1925 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1926 G_TYPE_NONE, 0);
Richard Hughescff38bc2016-12-12 12:03:37 +00001927}
1928
1929static void
Richard Hughes12724852018-09-04 13:53:44 +01001930fu_plugin_init (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +00001931{
Richard Hughes12724852018-09-04 13:53:44 +01001932 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001933 priv->enabled = TRUE;
1934 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1935 g_free, (GDestroyNotify) g_object_unref);
Richard Hughes37d09432018-09-09 10:39:45 +01001936 priv->devices_mutex = fu_mutex_new (G_OBJECT_TYPE_NAME(self), "devices");
Richard Hughes80b79bb2018-01-11 21:11:06 +00001937 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 +01001938 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1939 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001940}
1941
1942static void
1943fu_plugin_finalize (GObject *object)
1944{
Richard Hughes12724852018-09-04 13:53:44 +01001945 FuPlugin *self = FU_PLUGIN (object);
1946 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001947 FuPluginInitFunc func = NULL;
1948
1949 /* optional */
1950 if (priv->module != NULL) {
1951 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1952 if (func != NULL) {
1953 g_debug ("performing destroy() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001954 func (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001955 }
1956 }
1957
Richard Hughes08a37992017-09-12 12:57:43 +01001958 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1959 g_ptr_array_unref (priv->rules[i]);
1960
Richard Hughescff38bc2016-12-12 12:03:37 +00001961 if (priv->usb_ctx != NULL)
1962 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001963 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001964 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001965 if (priv->quirks != NULL)
1966 g_object_unref (priv->quirks);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001967 if (priv->udev_subsystems != NULL)
1968 g_ptr_array_unref (priv->udev_subsystems);
Richard Hughes49e5e052017-09-03 12:15:41 +01001969 if (priv->smbios != NULL)
1970 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001971 if (priv->runtime_versions != NULL)
1972 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001973 if (priv->compile_versions != NULL)
1974 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00001975 g_hash_table_unref (priv->devices);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001976 g_hash_table_unref (priv->report_metadata);
Richard Hughes37d09432018-09-09 10:39:45 +01001977 g_object_unref (priv->devices_mutex);
Richard Hughescff38bc2016-12-12 12:03:37 +00001978 g_free (priv->name);
1979 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05001980 /* Must happen as the last step to avoid prematurely
1981 * freeing memory held by the plugin */
1982#ifndef RUNNING_ON_VALGRIND
1983 if (priv->module != NULL)
1984 g_module_close (priv->module);
1985#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001986
1987 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1988}
1989
1990FuPlugin *
1991fu_plugin_new (void)
1992{
Richard Hughes12724852018-09-04 13:53:44 +01001993 return FU_PLUGIN (g_object_new (FU_TYPE_PLUGIN, NULL));
Richard Hughescff38bc2016-12-12 12:03:37 +00001994}