blob: c0afb952639b8884bcc1eeba0c325447e9413d1c [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 Hughes161e9b52019-06-12 14:22:45 +010055 GRWLock 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 Hughes161e9b52019-06-12 14:22:45 +0100179 g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_reader_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 Hughes161e9b52019-06-12 14:22:45 +0100200 g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_writer_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 Hughes161e9b52019-06-12 14:22:45 +0100220 g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_writer_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 Hughes19841802019-09-10 16:48:00 +0100533 * fu_plugin_has_custom_flag:
534 * @self: A #FuPlugin
535 * @flag: A custom text flag, specific to the plugin, e.g. `uefi-force-enable`
536 *
537 * Returns if a per-plugin HwId custom flag exists, typically added from a DMI quirk.
538 *
539 * Returns: %TRUE if the quirk entry exists
540 *
541 * Since: 1.3.1
542 **/
543gboolean
544fu_plugin_has_custom_flag (FuPlugin *self, const gchar *flag)
545{
546 FuPluginPrivate *priv = GET_PRIVATE (self);
547 GPtrArray *hwids = fu_plugin_get_hwids (self);
548
549 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
550 g_return_val_if_fail (flag != NULL, FALSE);
551
552 /* never set up, e.g. in tests */
553 if (hwids == NULL)
554 return FALSE;
555
556 /* search each hwid */
557 for (guint i = 0; i < hwids->len; i++) {
558 const gchar *hwid = g_ptr_array_index (hwids, i);
559 const gchar *value;
560 g_autofree gchar *key = g_strdup_printf ("HwId=%s", hwid);
561
562 /* does prefixed quirk exist */
563 value = fu_quirks_lookup_by_id (priv->quirks, key, FU_QUIRKS_FLAGS);
564 if (value != NULL) {
565 g_auto(GStrv) quirks = g_strsplit (value, ",", -1);
566 if (g_strv_contains ((const gchar * const *) quirks, flag))
567 return TRUE;
568 }
569 }
570 return FALSE;
571}
572
573/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100574 * fu_plugin_check_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100575 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100576 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100577 *
578 * Checks to see if a specific device GUID is supported, i.e. available in the
579 * AppStream metadata.
580 *
Richard Hughes4eada342017-10-03 21:20:32 +0100581 * Returns: %TRUE if the device is supported.
582 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100583 * Since: 1.0.0
584 **/
585gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100586fu_plugin_check_supported (FuPlugin *self, const gchar *guid)
Richard Hughes1354ea92017-09-19 15:58:31 +0100587{
Richard Hughesaabdc372018-11-14 10:11:08 +0000588 gboolean retval = FALSE;
589 g_signal_emit (self, signals[SIGNAL_CHECK_SUPPORTED], 0, guid, &retval);
590 return retval;
Richard Hughes1354ea92017-09-19 15:58:31 +0100591}
592
593/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100594 * fu_plugin_get_dmi_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100595 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100596 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100597 *
598 * Gets a hardware DMI value.
599 *
Richard Hughes4eada342017-10-03 21:20:32 +0100600 * Returns: The string, or %NULL
601 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100602 * Since: 0.9.7
603 **/
604const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100605fu_plugin_get_dmi_value (FuPlugin *self, const gchar *dmi_id)
Richard Hughesd7704d42017-08-08 20:29:09 +0100606{
Richard Hughes12724852018-09-04 13:53:44 +0100607 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100608 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100609 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100610 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100611}
612
Richard Hughes49e5e052017-09-03 12:15:41 +0100613/**
614 * fu_plugin_get_smbios_string:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100615 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100616 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
617 * @offset: A SMBIOS offset
618 *
619 * Gets a hardware SMBIOS string.
620 *
621 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
622 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
623 *
Richard Hughes4eada342017-10-03 21:20:32 +0100624 * Returns: A string, or %NULL
625 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100626 * Since: 0.9.8
627 **/
628const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100629fu_plugin_get_smbios_string (FuPlugin *self, guint8 structure_type, guint8 offset)
Richard Hughes49e5e052017-09-03 12:15:41 +0100630{
Richard Hughes12724852018-09-04 13:53:44 +0100631 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100632 if (priv->smbios == NULL)
633 return NULL;
634 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
635}
636
637/**
638 * fu_plugin_get_smbios_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100639 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100640 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
641 *
642 * Gets a hardware SMBIOS data.
643 *
Richard Hughesdfaca2d2019-08-01 08:08:03 +0100644 * Returns: (transfer full): A #GBytes, or %NULL
Richard Hughes4eada342017-10-03 21:20:32 +0100645 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100646 * Since: 0.9.8
647 **/
648GBytes *
Richard Hughes12724852018-09-04 13:53:44 +0100649fu_plugin_get_smbios_data (FuPlugin *self, guint8 structure_type)
Richard Hughes49e5e052017-09-03 12:15:41 +0100650{
Richard Hughes12724852018-09-04 13:53:44 +0100651 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100652 if (priv->smbios == NULL)
653 return NULL;
654 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
655}
656
Richard Hughesb8f8db22017-04-25 15:56:00 +0100657void
Richard Hughes12724852018-09-04 13:53:44 +0100658fu_plugin_set_hwids (FuPlugin *self, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100659{
Richard Hughes12724852018-09-04 13:53:44 +0100660 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100661 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100662}
663
Richard Hughes49e5e052017-09-03 12:15:41 +0100664void
Richard Hughes12724852018-09-04 13:53:44 +0100665fu_plugin_set_udev_subsystems (FuPlugin *self, GPtrArray *udev_subsystems)
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100666{
Richard Hughes12724852018-09-04 13:53:44 +0100667 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100668 if (priv->udev_subsystems != NULL)
669 g_ptr_array_unref (priv->udev_subsystems);
670 priv->udev_subsystems = g_ptr_array_ref (udev_subsystems);
671}
672
673void
Richard Hughes12724852018-09-04 13:53:44 +0100674fu_plugin_set_quirks (FuPlugin *self, FuQuirks *quirks)
Richard Hughes9c028f02017-10-28 21:14:28 +0100675{
Richard Hughes12724852018-09-04 13:53:44 +0100676 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100677 g_set_object (&priv->quirks, quirks);
678}
679
680/**
681 * fu_plugin_get_quirks:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100682 * @self: A #FuPlugin
Richard Hughes9c028f02017-10-28 21:14:28 +0100683 *
684 * Returns the hardware database object. This can be used to discover device
685 * quirks or other device-specific settings.
686 *
687 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
688 *
689 * Since: 1.0.1
690 **/
691FuQuirks *
Richard Hughes12724852018-09-04 13:53:44 +0100692fu_plugin_get_quirks (FuPlugin *self)
Richard Hughes9c028f02017-10-28 21:14:28 +0100693{
Richard Hughes12724852018-09-04 13:53:44 +0100694 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100695 return priv->quirks;
696}
697
Richard Hughes0eb123b2018-04-19 12:00:04 +0100698void
Richard Hughes12724852018-09-04 13:53:44 +0100699fu_plugin_set_runtime_versions (FuPlugin *self, GHashTable *runtime_versions)
Richard Hughes0eb123b2018-04-19 12:00:04 +0100700{
Richard Hughes12724852018-09-04 13:53:44 +0100701 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0eb123b2018-04-19 12:00:04 +0100702 priv->runtime_versions = g_hash_table_ref (runtime_versions);
703}
704
705/**
706 * fu_plugin_add_runtime_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100707 * @self: A #FuPlugin
Richard Hughes0eb123b2018-04-19 12:00:04 +0100708 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
709 * @version: A version string, e.g. "1.2.3"
710 *
Richard Hughesdce91202019-04-08 12:47:45 +0100711 * Sets a runtime version of a specific dependency.
Richard Hughes0eb123b2018-04-19 12:00:04 +0100712 *
713 * Since: 1.0.7
714 **/
715void
Richard Hughes12724852018-09-04 13:53:44 +0100716fu_plugin_add_runtime_version (FuPlugin *self,
Richard Hughes0eb123b2018-04-19 12:00:04 +0100717 const gchar *component_id,
718 const gchar *version)
719{
Richard Hughes12724852018-09-04 13:53:44 +0100720 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb01b4862018-04-20 16:39:48 +0100721 if (priv->runtime_versions == NULL)
722 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100723 g_hash_table_insert (priv->runtime_versions,
724 g_strdup (component_id),
725 g_strdup (version));
726}
727
Richard Hughes34e0dab2018-04-20 16:43:00 +0100728void
Richard Hughes12724852018-09-04 13:53:44 +0100729fu_plugin_set_compile_versions (FuPlugin *self, GHashTable *compile_versions)
Richard Hughes34e0dab2018-04-20 16:43:00 +0100730{
Richard Hughes12724852018-09-04 13:53:44 +0100731 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100732 priv->compile_versions = g_hash_table_ref (compile_versions);
733}
734
735/**
736 * fu_plugin_add_compile_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100737 * @self: A #FuPlugin
Richard Hughes34e0dab2018-04-20 16:43:00 +0100738 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
739 * @version: A version string, e.g. "1.2.3"
740 *
Richard Hughesdce91202019-04-08 12:47:45 +0100741 * Sets a compile-time version of a specific dependency.
Richard Hughes34e0dab2018-04-20 16:43:00 +0100742 *
743 * Since: 1.0.7
744 **/
745void
Richard Hughes12724852018-09-04 13:53:44 +0100746fu_plugin_add_compile_version (FuPlugin *self,
Richard Hughes34e0dab2018-04-20 16:43:00 +0100747 const gchar *component_id,
748 const gchar *version)
749{
Richard Hughes12724852018-09-04 13:53:44 +0100750 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100751 if (priv->compile_versions == NULL)
752 return;
753 g_hash_table_insert (priv->compile_versions,
754 g_strdup (component_id),
755 g_strdup (version));
756}
757
Richard Hughes9c028f02017-10-28 21:14:28 +0100758/**
759 * fu_plugin_lookup_quirk_by_id:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100760 * @self: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100761 * @group: A string, e.g. "DfuFlags"
762 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100763 *
764 * Looks up an entry in the hardware database using a string value.
765 *
766 * Returns: (transfer none): values from the database, or %NULL if not found
767 *
768 * Since: 1.0.1
769 **/
770const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100771fu_plugin_lookup_quirk_by_id (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100772{
Richard Hughes12724852018-09-04 13:53:44 +0100773 FuPluginPrivate *priv = GET_PRIVATE (self);
774 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes9c028f02017-10-28 21:14:28 +0100775
Richard Hughes9c028f02017-10-28 21:14:28 +0100776 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100777 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100778}
779
780/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100781 * fu_plugin_lookup_quirk_by_id_as_uint64:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100782 * @self: A #FuPlugin
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100783 * @group: A string, e.g. "DfuFlags"
784 * @key: An ID to match the entry, e.g. "Size"
785 *
786 * Looks up an entry in the hardware database using a string key, returning
787 * an integer value. Values are assumed base 10, unless prefixed with "0x"
788 * where they are parsed as base 16.
789 *
790 * Returns: (transfer none): value from the database, or 0 if not found
791 *
792 * Since: 1.1.2
793 **/
794guint64
Richard Hughes12724852018-09-04 13:53:44 +0100795fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100796{
Richard Hughes12724852018-09-04 13:53:44 +0100797 return fu_common_strtoull (fu_plugin_lookup_quirk_by_id (self, group, key));
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100798}
799
Richard Hughes1354ea92017-09-19 15:58:31 +0100800void
Richard Hughes12724852018-09-04 13:53:44 +0100801fu_plugin_set_smbios (FuPlugin *self, FuSmbios *smbios)
Richard Hughes49e5e052017-09-03 12:15:41 +0100802{
Richard Hughes12724852018-09-04 13:53:44 +0100803 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100804 g_set_object (&priv->smbios, smbios);
805}
806
Richard Hughesb8f8db22017-04-25 15:56:00 +0100807/**
Richard Hughesb0829032017-01-10 09:27:08 +0000808 * fu_plugin_set_coldplug_delay:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100809 * @self: A #FuPlugin
Richard Hughesb0829032017-01-10 09:27:08 +0000810 * @duration: A delay in milliseconds
811 *
812 * Set the minimum time that should be waited inbetween the call to
813 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
814 * to be the minimum hardware initialisation time from a datasheet.
815 *
816 * It is better to use this function rather than using a sleep() in the plugin
817 * itself as then only one delay is done in the daemon rather than waiting for
818 * each coldplug prepare in a serial way.
819 *
820 * Additionally, very long delays should be avoided as the daemon will be
821 * blocked from processing requests whilst the coldplug delay is being
822 * performed.
823 *
824 * Since: 0.8.0
825 **/
826void
Richard Hughes12724852018-09-04 13:53:44 +0100827fu_plugin_set_coldplug_delay (FuPlugin *self, guint duration)
Richard Hughesb0829032017-01-10 09:27:08 +0000828{
Richard Hughes12724852018-09-04 13:53:44 +0100829 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesb0829032017-01-10 09:27:08 +0000830 g_return_if_fail (duration > 0);
831
832 /* check sanity */
833 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
834 g_warning ("duration of %ums is crazy, truncating to %ums",
835 duration,
836 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
837 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
838 }
839
840 /* emit */
Richard Hughes12724852018-09-04 13:53:44 +0100841 g_signal_emit (self, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
Richard Hughesb0829032017-01-10 09:27:08 +0000842}
843
Richard Hughes4b303802019-10-04 13:22:51 +0100844static gboolean
845fu_plugin_device_attach (FuPlugin *self, FuDevice *device, GError **error)
846{
847 g_autoptr(FuDeviceLocker) locker = NULL;
848 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_IS_BOOTLOADER)) {
849 g_debug ("already in runtime mode, skipping");
850 return TRUE;
851 }
852 locker = fu_device_locker_new (device, error);
853 if (locker == NULL)
854 return FALSE;
855 return fu_device_attach (device, error);
856}
857
858static gboolean
859fu_plugin_device_detach (FuPlugin *self, FuDevice *device, GError **error)
860{
861 g_autoptr(FuDeviceLocker) locker = NULL;
862 if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_IS_BOOTLOADER)) {
863 g_debug ("already in bootloader mode, skipping");
864 return TRUE;
865 }
866 locker = fu_device_locker_new (device, error);
867 if (locker == NULL)
868 return FALSE;
869 return fu_device_detach (device, error);
870}
871
872static gboolean
873fu_plugin_device_reload (FuPlugin *self, FuDevice *device, GError **error)
874{
875 g_autoptr(FuDeviceLocker) locker = NULL;
876 locker = fu_device_locker_new (device, error);
877 if (locker == NULL)
878 return FALSE;
879 return fu_device_reload (device, error);
880}
881
882static gboolean
883fu_plugin_device_activate (FuPlugin *self, FuDevice *device, GError **error)
884{
885 g_autoptr(FuDeviceLocker) locker = NULL;
886 locker = fu_device_locker_new (device, error);
887 if (locker == NULL)
888 return FALSE;
889 return fu_device_activate (device, error);
890}
891
892static gboolean
893fu_plugin_device_write_firmware (FuPlugin *self, FuDevice *device,
894 GBytes *fw, FwupdInstallFlags flags,
895 GError **error)
896{
897 g_autoptr(FuDeviceLocker) locker = NULL;
898 locker = fu_device_locker_new (device, error);
899 if (locker == NULL)
900 return FALSE;
901 return fu_device_write_firmware (device, fw, flags, error);
902}
903
Richard Hughesd0905142016-03-13 09:46:49 +0000904gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100905fu_plugin_runner_startup (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000906{
Richard Hughes12724852018-09-04 13:53:44 +0100907 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000908 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000909 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000910
911 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000912 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000913 return TRUE;
914
Richard Hughes639da472018-01-06 22:35:04 +0000915 /* no object loaded */
916 if (priv->module == NULL)
917 return TRUE;
918
Richard Hughesd0905142016-03-13 09:46:49 +0000919 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000920 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
921 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000922 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000923 g_debug ("performing startup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000924 if (!func (self, &error_local)) {
925 if (error_local == NULL) {
926 g_critical ("unset error in plugin %s for startup()",
927 priv->name);
928 g_set_error_literal (&error_local,
929 FWUPD_ERROR,
930 FWUPD_ERROR_INTERNAL,
931 "unspecified error");
932 }
933 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
934 "failed to startup using %s: ",
935 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +0000936 return FALSE;
937 }
938 return TRUE;
939}
940
941static gboolean
942fu_plugin_runner_offline_invalidate (GError **error)
943{
944 g_autoptr(GError) error_local = NULL;
945 g_autoptr(GFile) file1 = NULL;
946
947 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
948
949 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
950 if (!g_file_query_exists (file1, NULL))
951 return TRUE;
952 if (!g_file_delete (file1, NULL, &error_local)) {
953 g_set_error (error,
954 FWUPD_ERROR,
955 FWUPD_ERROR_INTERNAL,
956 "Cannot delete %s: %s",
957 FU_OFFLINE_TRIGGER_FILENAME,
958 error_local->message);
959 return FALSE;
960 }
961 return TRUE;
962}
963
964static gboolean
965fu_plugin_runner_offline_setup (GError **error)
966{
967 gint rc;
Richard Hughes484ee292019-03-22 16:10:50 +0000968 g_autofree gchar *filename = NULL;
Mario Limoncielloe1b4b202019-04-30 10:01:19 -0500969 g_autofree gchar *symlink_target = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +0000970
971 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
972
Richard Hughes484ee292019-03-22 16:10:50 +0000973 /* does already exist */
974 filename = fu_common_realpath (FU_OFFLINE_TRIGGER_FILENAME, NULL);
975 if (g_strcmp0 (filename, symlink_target) == 0) {
976 g_debug ("%s already points to %s, skipping creation",
977 FU_OFFLINE_TRIGGER_FILENAME, symlink_target);
978 return TRUE;
979 }
980
Richard Hughescff38bc2016-12-12 12:03:37 +0000981 /* create symlink for the systemd-system-update-generator */
Richard Hughes484ee292019-03-22 16:10:50 +0000982 rc = symlink (symlink_target, FU_OFFLINE_TRIGGER_FILENAME);
Richard Hughescff38bc2016-12-12 12:03:37 +0000983 if (rc < 0) {
984 g_set_error (error,
985 FWUPD_ERROR,
986 FWUPD_ERROR_INTERNAL,
987 "Failed to create symlink %s to %s: %s",
988 FU_OFFLINE_TRIGGER_FILENAME,
989 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000990 return FALSE;
991 }
992 return TRUE;
993}
994
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000995static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100996fu_plugin_runner_device_generic (FuPlugin *self, FuDevice *device,
Richard Hughes4b303802019-10-04 13:22:51 +0100997 const gchar *symbol_name,
998 FuPluginDeviceFunc device_func,
999 GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001000{
Richard Hughes12724852018-09-04 13:53:44 +01001001 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001002 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001003 g_autoptr(GError) error_local = NULL;
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001004
1005 /* not enabled */
1006 if (!priv->enabled)
1007 return TRUE;
1008
Richard Hughesd3d96cc2017-11-14 11:34:33 +00001009 /* no object loaded */
1010 if (priv->module == NULL)
1011 return TRUE;
1012
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001013 /* optional */
1014 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
Richard Hughes4b303802019-10-04 13:22:51 +01001015 if (func == NULL) {
1016 if (device_func != NULL) {
1017 g_debug ("running superclassed %s() on %s",
1018 symbol_name + 10, priv->name);
1019 return device_func (self, device, error);
1020 }
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001021 return TRUE;
Richard Hughes4b303802019-10-04 13:22:51 +01001022 }
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001023 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001024 if (!func (self, device, &error_local)) {
1025 if (error_local == NULL) {
1026 g_critical ("unset error in plugin %s for %s()",
1027 priv->name, symbol_name + 10);
1028 g_set_error_literal (&error_local,
1029 FWUPD_ERROR,
1030 FWUPD_ERROR_INTERNAL,
1031 "unspecified error");
1032 }
1033 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1034 "failed to %s using %s: ",
1035 symbol_name + 10, priv->name);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001036 return FALSE;
1037 }
1038 return TRUE;
1039}
1040
Richard Hughesdbd8c762018-06-15 20:31:40 +01001041static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001042fu_plugin_runner_flagged_device_generic (FuPlugin *self, FwupdInstallFlags flags,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001043 FuDevice *device,
1044 const gchar *symbol_name, GError **error)
1045{
Richard Hughes12724852018-09-04 13:53:44 +01001046 FuPluginPrivate *priv = GET_PRIVATE (self);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001047 FuPluginFlaggedDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001048 g_autoptr(GError) error_local = NULL;
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001049
1050 /* not enabled */
1051 if (!priv->enabled)
1052 return TRUE;
1053
1054 /* no object loaded */
1055 if (priv->module == NULL)
1056 return TRUE;
1057
1058 /* optional */
1059 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1060 if (func == NULL)
1061 return TRUE;
1062 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001063 if (!func (self, flags, device, &error_local)) {
1064 if (error_local == NULL) {
1065 g_critical ("unset error in plugin %s for %s()",
1066 priv->name, symbol_name + 10);
1067 g_set_error_literal (&error_local,
1068 FWUPD_ERROR,
1069 FWUPD_ERROR_INTERNAL,
1070 "unspecified error");
1071 }
1072 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1073 "failed to %s using %s: ",
1074 symbol_name + 10, priv->name);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001075 return FALSE;
1076 }
1077 return TRUE;
1078
1079}
1080
1081static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001082fu_plugin_runner_device_array_generic (FuPlugin *self, GPtrArray *devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001083 const gchar *symbol_name, GError **error)
1084{
Richard Hughes12724852018-09-04 13:53:44 +01001085 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesdbd8c762018-06-15 20:31:40 +01001086 FuPluginDeviceArrayFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001087 g_autoptr(GError) error_local = NULL;
Richard Hughesdbd8c762018-06-15 20:31:40 +01001088
1089 /* not enabled */
1090 if (!priv->enabled)
1091 return TRUE;
1092
1093 /* no object loaded */
1094 if (priv->module == NULL)
1095 return TRUE;
1096
1097 /* optional */
1098 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1099 if (func == NULL)
1100 return TRUE;
1101 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001102 if (!func (self, devices, &error_local)) {
1103 if (error_local == NULL) {
1104 g_critical ("unset error in plugin %s for %s()",
1105 priv->name, symbol_name + 10);
1106 g_set_error_literal (&error_local,
1107 FWUPD_ERROR,
1108 FWUPD_ERROR_INTERNAL,
1109 "unspecified error");
1110 }
1111 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1112 "failed to %s using %s: ",
1113 symbol_name + 10, priv->name);
Richard Hughesdbd8c762018-06-15 20:31:40 +01001114 return FALSE;
1115 }
1116 return TRUE;
1117}
1118
Richard Hughesd0905142016-03-13 09:46:49 +00001119gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001120fu_plugin_runner_coldplug (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001121{
Richard Hughes12724852018-09-04 13:53:44 +01001122 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001123 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001124 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +00001125
1126 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +00001127 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +00001128 return TRUE;
1129
Richard Hughes639da472018-01-06 22:35:04 +00001130 /* no object loaded */
1131 if (priv->module == NULL)
1132 return TRUE;
1133
Richard Hughesd0905142016-03-13 09:46:49 +00001134 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001135 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
1136 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001137 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001138 g_debug ("performing coldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001139 if (!func (self, &error_local)) {
1140 if (error_local == NULL) {
1141 g_critical ("unset error in plugin %s for coldplug()",
1142 priv->name);
1143 g_set_error_literal (&error_local,
1144 FWUPD_ERROR,
1145 FWUPD_ERROR_INTERNAL,
1146 "unspecified error");
1147 }
1148 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1149 "failed to coldplug using %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001150 return FALSE;
1151 }
1152 return TRUE;
1153}
1154
Richard Hughes7b8b2022016-12-12 16:15:03 +00001155gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001156fu_plugin_runner_recoldplug (FuPlugin *self, GError **error)
Richard Hughes2de8f132018-01-17 09:12:02 +00001157{
Richard Hughes12724852018-09-04 13:53:44 +01001158 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes2de8f132018-01-17 09:12:02 +00001159 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001160 g_autoptr(GError) error_local = NULL;
Richard Hughes2de8f132018-01-17 09:12:02 +00001161
1162 /* not enabled */
1163 if (!priv->enabled)
1164 return TRUE;
1165
1166 /* no object loaded */
1167 if (priv->module == NULL)
1168 return TRUE;
1169
1170 /* optional */
1171 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1172 if (func == NULL)
1173 return TRUE;
1174 g_debug ("performing recoldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001175 if (!func (self, &error_local)) {
1176 if (error_local == NULL) {
1177 g_critical ("unset error in plugin %s for recoldplug()",
1178 priv->name);
1179 g_set_error_literal (&error_local,
1180 FWUPD_ERROR,
1181 FWUPD_ERROR_INTERNAL,
1182 "unspecified error");
1183 }
1184 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1185 "failed to recoldplug using %s: ",
1186 priv->name);
Richard Hughes2de8f132018-01-17 09:12:02 +00001187 return FALSE;
1188 }
1189 return TRUE;
1190}
1191
1192gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001193fu_plugin_runner_coldplug_prepare (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001194{
Richard Hughes12724852018-09-04 13:53:44 +01001195 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001196 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001197 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001198
1199 /* not enabled */
1200 if (!priv->enabled)
1201 return TRUE;
1202
Richard Hughes639da472018-01-06 22:35:04 +00001203 /* no object loaded */
1204 if (priv->module == NULL)
1205 return TRUE;
1206
Richard Hughes46487c92017-01-07 21:26:34 +00001207 /* optional */
1208 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1209 if (func == NULL)
1210 return TRUE;
1211 g_debug ("performing coldplug_prepare() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001212 if (!func (self, &error_local)) {
1213 if (error_local == NULL) {
1214 g_critical ("unset error in plugin %s for coldplug_prepare()",
1215 priv->name);
1216 g_set_error_literal (&error_local,
1217 FWUPD_ERROR,
1218 FWUPD_ERROR_INTERNAL,
1219 "unspecified error");
1220 }
1221 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1222 "failed to coldplug_prepare using %s: ",
1223 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001224 return FALSE;
1225 }
1226 return TRUE;
1227}
1228
1229gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001230fu_plugin_runner_coldplug_cleanup (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001231{
Richard Hughes12724852018-09-04 13:53:44 +01001232 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001233 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001234 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001235
1236 /* not enabled */
1237 if (!priv->enabled)
1238 return TRUE;
1239
Richard Hughes639da472018-01-06 22:35:04 +00001240 /* no object loaded */
1241 if (priv->module == NULL)
1242 return TRUE;
1243
Richard Hughes46487c92017-01-07 21:26:34 +00001244 /* optional */
1245 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1246 if (func == NULL)
1247 return TRUE;
1248 g_debug ("performing coldplug_cleanup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001249 if (!func (self, &error_local)) {
1250 if (error_local == NULL) {
1251 g_critical ("unset error in plugin %s for coldplug_cleanup()",
1252 priv->name);
1253 g_set_error_literal (&error_local,
1254 FWUPD_ERROR,
1255 FWUPD_ERROR_INTERNAL,
1256 "unspecified error");
1257 }
1258 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1259 "failed to coldplug_cleanup using %s: ",
1260 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001261 return FALSE;
1262 }
1263 return TRUE;
1264}
1265
1266gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001267fu_plugin_runner_composite_prepare (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001268{
Richard Hughes12724852018-09-04 13:53:44 +01001269 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001270 "fu_plugin_composite_prepare",
1271 error);
1272}
1273
1274gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001275fu_plugin_runner_composite_cleanup (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001276{
Richard Hughes12724852018-09-04 13:53:44 +01001277 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001278 "fu_plugin_composite_cleanup",
1279 error);
1280}
1281
1282gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001283fu_plugin_runner_update_prepare (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001284 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001285{
Richard Hughes12724852018-09-04 13:53:44 +01001286 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001287 "fu_plugin_update_prepare",
1288 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001289}
1290
1291gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001292fu_plugin_runner_update_cleanup (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001293 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001294{
Richard Hughes12724852018-09-04 13:53:44 +01001295 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001296 "fu_plugin_update_cleanup",
1297 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001298}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001299
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001300gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001301fu_plugin_runner_update_attach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001302{
Richard Hughes12724852018-09-04 13:53:44 +01001303 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001304 "fu_plugin_update_attach",
1305 fu_plugin_device_attach,
1306 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001307}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001308
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001309gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001310fu_plugin_runner_update_detach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001311{
Richard Hughes12724852018-09-04 13:53:44 +01001312 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001313 "fu_plugin_update_detach",
1314 fu_plugin_device_detach,
1315 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001316}
1317
1318gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001319fu_plugin_runner_update_reload (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001320{
Richard Hughes12724852018-09-04 13:53:44 +01001321 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001322 "fu_plugin_update_reload",
1323 fu_plugin_device_reload,
1324 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001325}
1326
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001327/**
1328 * fu_plugin_add_udev_subsystem:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001329 * @self: a #FuPlugin
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001330 * @subsystem: a subsystem name, e.g. `pciport`
1331 *
1332 * Registers the udev subsystem to be watched by the daemon.
1333 *
1334 * Plugins can use this method only in fu_plugin_init()
1335 **/
1336void
Richard Hughes12724852018-09-04 13:53:44 +01001337fu_plugin_add_udev_subsystem (FuPlugin *self, const gchar *subsystem)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001338{
Richard Hughes12724852018-09-04 13:53:44 +01001339 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001340 for (guint i = 0; i < priv->udev_subsystems->len; i++) {
1341 const gchar *subsystem_tmp = g_ptr_array_index (priv->udev_subsystems, i);
1342 if (g_strcmp0 (subsystem_tmp, subsystem) == 0)
1343 return;
1344 }
1345 g_debug ("added udev subsystem watch of %s", subsystem);
1346 g_ptr_array_add (priv->udev_subsystems, g_strdup (subsystem));
1347}
1348
Richard Hughes104f6512017-11-24 11:44:57 +00001349gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001350fu_plugin_runner_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
Richard Hughes104f6512017-11-24 11:44:57 +00001351{
Richard Hughes12724852018-09-04 13:53:44 +01001352 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes104f6512017-11-24 11:44:57 +00001353 FuPluginUsbDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001354 g_autoptr(GError) error_local = NULL;
Richard Hughes104f6512017-11-24 11:44:57 +00001355
1356 /* not enabled */
1357 if (!priv->enabled)
1358 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001359
1360 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001361 if (priv->module == NULL)
1362 return TRUE;
1363
1364 /* optional */
1365 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001366 if (func == NULL)
1367 return TRUE;
1368 g_debug ("performing usb_device_added() on %s", priv->name);
1369 if (!func (self, device, &error_local)) {
1370 if (error_local == NULL) {
1371 g_critical ("unset error in plugin %s for usb_device_added()",
1372 priv->name);
1373 g_set_error_literal (&error_local,
1374 FWUPD_ERROR,
1375 FWUPD_ERROR_INTERNAL,
1376 "unspecified error");
1377 }
1378 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1379 "failed to add device using on %s: ",
1380 priv->name);
1381 return FALSE;
Richard Hughes104f6512017-11-24 11:44:57 +00001382 }
1383 return TRUE;
1384}
1385
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001386gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001387fu_plugin_runner_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001388{
Richard Hughes12724852018-09-04 13:53:44 +01001389 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001390 FuPluginUdevDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001391 g_autoptr(GError) error_local = NULL;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001392
1393 /* not enabled */
1394 if (!priv->enabled)
1395 return TRUE;
1396
1397 /* no object loaded */
1398 if (priv->module == NULL)
1399 return TRUE;
1400
1401 /* optional */
1402 g_module_symbol (priv->module, "fu_plugin_udev_device_added", (gpointer *) &func);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001403 if (func == NULL)
1404 return TRUE;
1405 g_debug ("performing udev_device_added() on %s", priv->name);
1406 if (!func (self, device, &error_local)) {
1407 if (error_local == NULL) {
1408 g_critical ("unset error in plugin %s for udev_device_added()",
1409 priv->name);
1410 g_set_error_literal (&error_local,
1411 FWUPD_ERROR,
1412 FWUPD_ERROR_INTERNAL,
1413 "unspecified error");
1414 }
1415 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1416 "failed to add device using on %s: ",
1417 priv->name);
1418 return FALSE;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001419 }
1420 return TRUE;
1421}
1422
Richard Hughes5e952ce2019-08-26 11:09:46 +01001423gboolean
1424fu_plugin_runner_udev_device_changed (FuPlugin *self, FuUdevDevice *device, GError **error)
1425{
1426 FuPluginPrivate *priv = GET_PRIVATE (self);
1427 FuPluginUdevDeviceAddedFunc func = NULL;
1428 g_autoptr(GError) error_local = NULL;
1429
1430 /* not enabled */
1431 if (!priv->enabled)
1432 return TRUE;
1433
1434 /* no object loaded */
1435 if (priv->module == NULL)
1436 return TRUE;
1437
1438 /* optional */
1439 g_module_symbol (priv->module, "fu_plugin_udev_device_changed", (gpointer *) &func);
1440 if (func == NULL)
1441 return TRUE;
1442 g_debug ("performing udev_device_changed() on %s", priv->name);
1443 if (!func (self, device, &error_local)) {
1444 if (error_local == NULL) {
1445 g_critical ("unset error in plugin %s for udev_device_changed()",
1446 priv->name);
1447 g_set_error_literal (&error_local,
1448 FWUPD_ERROR,
1449 FWUPD_ERROR_INTERNAL,
1450 "unspecified error");
1451 }
1452 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1453 "failed to change device on %s: ",
1454 priv->name);
1455 return FALSE;
1456 }
1457 return TRUE;
1458}
1459
Richard Hughese1fd34d2017-08-24 14:19:51 +01001460void
Richard Hughes12724852018-09-04 13:53:44 +01001461fu_plugin_runner_device_removed (FuPlugin *self, FuDevice *device)
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001462{
1463 g_autoptr(GError) error_local= NULL;
1464
Richard Hughes12724852018-09-04 13:53:44 +01001465 if (!fu_plugin_runner_device_generic (self, device,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001466 "fu_plugin_device_removed",
Richard Hughes4b303802019-10-04 13:22:51 +01001467 NULL,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001468 &error_local))
1469 g_warning ("%s", error_local->message);
1470}
1471
1472void
Richard Hughes12724852018-09-04 13:53:44 +01001473fu_plugin_runner_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +01001474{
Richard Hughes12724852018-09-04 13:53:44 +01001475 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001476 FuPluginDeviceRegisterFunc func = NULL;
1477
1478 /* not enabled */
1479 if (!priv->enabled)
1480 return;
Richard Hughes34834102017-11-21 21:55:00 +00001481 if (priv->module == NULL)
1482 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001483
Mario Limonciello4910b242018-06-22 15:04:21 -05001484 /* don't notify plugins on their own devices */
Richard Hughes12724852018-09-04 13:53:44 +01001485 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (self)) == 0)
Mario Limonciello4910b242018-06-22 15:04:21 -05001486 return;
1487
Richard Hughese1fd34d2017-08-24 14:19:51 +01001488 /* optional */
1489 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1490 if (func != NULL) {
Richard Hughes1bf7ff92018-08-24 20:21:35 +01001491 g_debug ("performing fu_plugin_device_registered() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001492 func (self, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001493 }
1494}
1495
Richard Hughesc6c312f2019-02-01 16:37:14 +00001496gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001497fu_plugin_runner_schedule_update (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001498 FuDevice *device,
Richard Hughes994b4d92019-03-25 14:28:30 +00001499 FwupdRelease *release,
Richard Hughescff38bc2016-12-12 12:03:37 +00001500 GBytes *blob_cab,
Richard Hughes5cbb5cf2019-04-26 16:48:03 +01001501 FwupdInstallFlags flags,
Richard Hughescff38bc2016-12-12 12:03:37 +00001502 GError **error)
1503{
Richard Hughes0a906262019-05-16 13:38:47 +01001504 gchar tmpname[] = {"XXXXXX.cab"};
Richard Hughescff38bc2016-12-12 12:03:37 +00001505 g_autofree gchar *dirname = NULL;
1506 g_autofree gchar *filename = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001507 g_autoptr(FuHistory) history = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001508 g_autoptr(GFile) file = NULL;
1509
1510 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001511 history = fu_history_new ();
Richard Hughes5cbb5cf2019-04-26 16:48:03 +01001512 if ((flags & FWUPD_INSTALL_FLAG_FORCE) == 0) {
1513 g_autoptr(FuDevice) res_tmp = NULL;
1514 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
1515 if (res_tmp != NULL &&
1516 fu_device_get_update_state (res_tmp) == FWUPD_UPDATE_STATE_PENDING) {
1517 g_set_error (error,
1518 FWUPD_ERROR,
1519 FWUPD_ERROR_ALREADY_PENDING,
1520 "%s is already scheduled to be updated",
1521 fu_device_get_id (device));
1522 return FALSE;
1523 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001524 }
1525
1526 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001527 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001528 file = g_file_new_for_path (dirname);
1529 if (!g_file_query_exists (file, NULL)) {
1530 if (!g_file_make_directory_with_parents (file, NULL, error))
1531 return FALSE;
1532 }
1533
1534 /* get a random filename */
1535 for (guint i = 0; i < 6; i++)
1536 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1537 filename = g_build_filename (dirname, tmpname, NULL);
1538
1539 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001540 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001541 if (!g_file_set_contents (filename,
1542 g_bytes_get_data (blob_cab, NULL),
1543 (gssize) g_bytes_get_size (blob_cab),
1544 error))
1545 return FALSE;
1546
1547 /* schedule for next boot */
1548 g_debug ("schedule %s to be installed to %s on next boot",
1549 filename, fu_device_get_id (device));
Richard Hughes994b4d92019-03-25 14:28:30 +00001550 fwupd_release_set_filename (release, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001551
1552 /* add to database */
Richard Hughes809abea2019-03-23 11:06:18 +00001553 fu_device_add_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT);
Richard Hughes3e90a582018-01-06 22:38:09 +00001554 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes994b4d92019-03-25 14:28:30 +00001555 if (!fu_history_add_device (history, device, release, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001556 return FALSE;
1557
1558 /* next boot we run offline */
Richard Hughesdb69c812019-03-22 16:10:15 +00001559 fu_device_set_progress (device, 100);
Richard Hughescff38bc2016-12-12 12:03:37 +00001560 return fu_plugin_runner_offline_setup (error);
1561}
1562
1563gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001564fu_plugin_runner_verify (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001565 FuDevice *device,
1566 FuPluginVerifyFlags flags,
1567 GError **error)
1568{
Richard Hughes12724852018-09-04 13:53:44 +01001569 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001570 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001571 GPtrArray *checksums;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001572 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001573
1574 /* not enabled */
1575 if (!priv->enabled)
1576 return TRUE;
1577
Richard Hughes639da472018-01-06 22:35:04 +00001578 /* no object loaded */
1579 if (priv->module == NULL)
1580 return TRUE;
1581
Richard Hughescff38bc2016-12-12 12:03:37 +00001582 /* optional */
1583 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1584 if (func == NULL)
1585 return TRUE;
Richard Hughes1812fc72018-12-14 11:37:54 +00001586
1587 /* clear any existing verification checksums */
1588 checksums = fu_device_get_checksums (device);
1589 g_ptr_array_set_size (checksums, 0);
1590
Richard Hughesc9223be2019-03-18 08:46:42 +00001591 /* run additional detach */
1592 if (!fu_plugin_runner_device_generic (self, device,
1593 "fu_plugin_verify_detach",
Richard Hughes4b303802019-10-04 13:22:51 +01001594 fu_plugin_device_detach,
Richard Hughesc9223be2019-03-18 08:46:42 +00001595 error))
1596 return FALSE;
1597
Richard Hughes1812fc72018-12-14 11:37:54 +00001598 /* run vfunc */
Richard Hughescff38bc2016-12-12 12:03:37 +00001599 g_debug ("performing verify() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001600 if (!func (self, device, flags, &error_local)) {
Richard Hughesc9223be2019-03-18 08:46:42 +00001601 g_autoptr(GError) error_attach = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001602 if (error_local == NULL) {
1603 g_critical ("unset error in plugin %s for verify()",
1604 priv->name);
1605 g_set_error_literal (&error_local,
1606 FWUPD_ERROR,
1607 FWUPD_ERROR_INTERNAL,
1608 "unspecified error");
1609 }
1610 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1611 "failed to verify using %s: ",
1612 priv->name);
Richard Hughesc9223be2019-03-18 08:46:42 +00001613 /* make the device "work" again, but don't prefix the error */
1614 if (!fu_plugin_runner_device_generic (self, device,
1615 "fu_plugin_verify_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01001616 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00001617 &error_attach)) {
1618 g_warning ("failed to attach whilst aborting verify(): %s",
1619 error_attach->message);
1620 }
Richard Hughesd0905142016-03-13 09:46:49 +00001621 return FALSE;
1622 }
Richard Hughesc9223be2019-03-18 08:46:42 +00001623
1624 /* run optional attach */
1625 if (!fu_plugin_runner_device_generic (self, device,
1626 "fu_plugin_verify_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01001627 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00001628 error))
1629 return FALSE;
1630
1631 /* success */
Richard Hughesd0905142016-03-13 09:46:49 +00001632 return TRUE;
1633}
1634
Richard Hughesd0905142016-03-13 09:46:49 +00001635gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001636fu_plugin_runner_activate (FuPlugin *self, FuDevice *device, GError **error)
1637{
1638 guint64 flags;
1639
1640 /* final check */
1641 flags = fu_device_get_flags (device);
1642 if ((flags & FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION) == 0) {
1643 g_set_error (error,
1644 FWUPD_ERROR,
1645 FWUPD_ERROR_NOT_SUPPORTED,
1646 "Device %s does not need activation",
1647 fu_device_get_id (device));
1648 return FALSE;
1649 }
1650
1651 /* run vfunc */
1652 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001653 "fu_plugin_activate",
1654 fu_plugin_device_activate,
1655 error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001656 return FALSE;
1657
1658 /* update with correct flags */
1659 fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION);
1660 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1661 return TRUE;
1662}
1663
1664gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001665fu_plugin_runner_unlock (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001666{
Richard Hughescff38bc2016-12-12 12:03:37 +00001667 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001668
1669 /* final check */
1670 flags = fu_device_get_flags (device);
1671 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1672 g_set_error (error,
1673 FWUPD_ERROR,
1674 FWUPD_ERROR_NOT_SUPPORTED,
1675 "Device %s is not locked",
1676 fu_device_get_id (device));
1677 return FALSE;
1678 }
1679
Richard Hughes9c4b5312017-11-14 11:34:53 +00001680 /* run vfunc */
Richard Hughes12724852018-09-04 13:53:44 +01001681 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001682 "fu_plugin_unlock",
1683 NULL,
1684 error))
Richard Hughes9c4b5312017-11-14 11:34:53 +00001685 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001686
1687 /* update with correct flags */
1688 flags = fu_device_get_flags (device);
1689 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1690 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1691 return TRUE;
1692}
1693
1694gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001695fu_plugin_runner_update (FuPlugin *self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001696 FuDevice *device,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001697 GBytes *blob_fw,
1698 FwupdInstallFlags flags,
1699 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001700{
Richard Hughes12724852018-09-04 13:53:44 +01001701 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001702 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001703 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001704 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001705 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001706
1707 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001708 if (!priv->enabled) {
1709 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001710 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001711 }
Richard Hughesd0905142016-03-13 09:46:49 +00001712
Richard Hughes639da472018-01-06 22:35:04 +00001713 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001714 if (priv->module == NULL) {
1715 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001716 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001717 }
Richard Hughes639da472018-01-06 22:35:04 +00001718
Richard Hughesd0905142016-03-13 09:46:49 +00001719 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001720 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1721 if (update_func == NULL) {
Richard Hughes4b303802019-10-04 13:22:51 +01001722 g_debug ("running superclassed write_firmware() on %s", priv->name);
1723 return fu_plugin_device_write_firmware (self, device, blob_fw, flags, error);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001724 }
Richard Hughesd0905142016-03-13 09:46:49 +00001725
Richard Hughescff38bc2016-12-12 12:03:37 +00001726 /* cancel the pending action */
1727 if (!fu_plugin_runner_offline_invalidate (error))
1728 return FALSE;
1729
1730 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001731 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001732 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001733 if (!update_func (self, device, blob_fw, flags, &error_local)) {
1734 if (error_local == NULL) {
1735 g_critical ("unset error in plugin %s for update()",
1736 priv->name);
1737 g_set_error_literal (&error_local,
Richard Hughes3c8ada32018-10-12 10:08:58 +01001738 FWUPD_ERROR,
1739 FWUPD_ERROR_INTERNAL,
1740 "unspecified error");
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001741 return FALSE;
Richard Hughes3c8ada32018-10-12 10:08:58 +01001742 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001743 fu_device_set_update_error (device, error_local->message);
1744 g_propagate_error (error, g_steal_pointer (&error_local));
Richard Hughescff38bc2016-12-12 12:03:37 +00001745 return FALSE;
1746 }
1747
Richard Hughesf556d372017-06-15 19:49:18 +01001748 /* no longer valid */
Richard Hughesf8039642019-01-16 12:22:22 +00001749 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT) &&
1750 !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) {
Richard Hughes08435162018-12-12 10:34:16 +00001751 GPtrArray *checksums = fu_device_get_checksums (device);
1752 g_ptr_array_set_size (checksums, 0);
1753 }
Richard Hughesf556d372017-06-15 19:49:18 +01001754
Richard Hughescff38bc2016-12-12 12:03:37 +00001755 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001756 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001757 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001758 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001759
Richard Hughes780ef3f2018-01-12 16:20:31 +00001760 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001761 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1762 if (!fu_history_modify_device (history, device,
1763 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1764 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001765 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001766
1767 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001768 release = fu_device_get_release_default (device_pending);
1769 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001770 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001771 g_autoptr(GError) error_delete = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001772 g_autoptr(GFile) file = NULL;
1773 file = g_file_new_for_path (tmp);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001774 if (!g_file_delete (file, NULL, &error_delete)) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001775 g_set_error (error,
1776 FWUPD_ERROR,
1777 FWUPD_ERROR_INVALID_FILE,
1778 "Failed to delete %s: %s",
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001779 tmp, error_delete->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001780 return FALSE;
1781 }
1782 }
1783 }
Richard Hughesd0905142016-03-13 09:46:49 +00001784 return TRUE;
1785}
Richard Hughescff38bc2016-12-12 12:03:37 +00001786
1787gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001788fu_plugin_runner_clear_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001789{
Richard Hughes12724852018-09-04 13:53:44 +01001790 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001791 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001792 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001793
1794 /* not enabled */
1795 if (!priv->enabled)
1796 return TRUE;
1797
Richard Hughes639da472018-01-06 22:35:04 +00001798 /* no object loaded */
1799 if (priv->module == NULL)
1800 return TRUE;
1801
Richard Hughes65e44ca2018-01-30 17:26:30 +00001802 /* optional */
1803 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1804 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001805 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001806 g_debug ("performing clear_result() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001807 if (!func (self, device, &error_local)) {
1808 if (error_local == NULL) {
1809 g_critical ("unset error in plugin %s for clear_result()",
1810 priv->name);
1811 g_set_error_literal (&error_local,
1812 FWUPD_ERROR,
1813 FWUPD_ERROR_INTERNAL,
1814 "unspecified error");
1815 }
1816 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1817 "failed to clear_result using %s: ",
1818 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001819 return FALSE;
1820 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001821 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001822}
1823
1824gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001825fu_plugin_runner_get_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001826{
Richard Hughes12724852018-09-04 13:53:44 +01001827 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001828 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001829 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001830
1831 /* not enabled */
1832 if (!priv->enabled)
1833 return TRUE;
1834
Richard Hughes639da472018-01-06 22:35:04 +00001835 /* no object loaded */
1836 if (priv->module == NULL)
1837 return TRUE;
1838
Richard Hughes65e44ca2018-01-30 17:26:30 +00001839 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001840 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001841 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001842 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001843 g_debug ("performing get_results() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001844 if (!func (self, device, &error_local)) {
1845 if (error_local == NULL) {
1846 g_critical ("unset error in plugin %s for get_results()",
1847 priv->name);
1848 g_set_error_literal (&error_local,
1849 FWUPD_ERROR,
1850 FWUPD_ERROR_INTERNAL,
1851 "unspecified error");
1852 }
1853 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1854 "failed to get_results using %s: ",
1855 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001856 return FALSE;
1857 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001858 return TRUE;
1859}
1860
Richard Hughes08a37992017-09-12 12:57:43 +01001861/**
1862 * fu_plugin_get_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001863 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001864 *
1865 * Gets the plugin order, where higher numbers are run after lower
1866 * numbers.
1867 *
1868 * Returns: the integer value
1869 **/
1870guint
Richard Hughes12724852018-09-04 13:53:44 +01001871fu_plugin_get_order (FuPlugin *self)
Richard Hughes08a37992017-09-12 12:57:43 +01001872{
Richard Hughes12724852018-09-04 13:53:44 +01001873 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001874 return priv->order;
1875}
1876
1877/**
1878 * fu_plugin_set_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001879 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001880 * @order: a integer value
1881 *
1882 * Sets the plugin order, where higher numbers are run after lower
1883 * numbers.
1884 **/
1885void
Richard Hughes12724852018-09-04 13:53:44 +01001886fu_plugin_set_order (FuPlugin *self, guint order)
Richard Hughes08a37992017-09-12 12:57:43 +01001887{
Richard Hughes12724852018-09-04 13:53:44 +01001888 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001889 priv->order = order;
1890}
1891
1892/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001893 * fu_plugin_get_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001894 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001895 *
1896 * Gets the plugin priority, where higher numbers are better.
1897 *
1898 * Returns: the integer value
1899 **/
1900guint
Richard Hughes12724852018-09-04 13:53:44 +01001901fu_plugin_get_priority (FuPlugin *self)
Richard Hughes81c427c2018-08-06 15:20:17 +01001902{
Richard Hughes12724852018-09-04 13:53:44 +01001903 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001904 return priv->priority;
1905}
1906
1907/**
1908 * fu_plugin_set_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001909 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001910 * @priority: a integer value
1911 *
1912 * Sets the plugin priority, where higher numbers are better.
1913 **/
1914void
Richard Hughes12724852018-09-04 13:53:44 +01001915fu_plugin_set_priority (FuPlugin *self, guint priority)
Richard Hughes81c427c2018-08-06 15:20:17 +01001916{
Richard Hughes12724852018-09-04 13:53:44 +01001917 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001918 priv->priority = priority;
1919}
1920
1921/**
Richard Hughes08a37992017-09-12 12:57:43 +01001922 * fu_plugin_add_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001923 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001924 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001925 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001926 *
1927 * If the plugin name is found, the rule will be used to sort the plugin list,
1928 * for example the plugin specified by @name will be ordered after this plugin
1929 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1930 *
1931 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1932 * If depsolving fails then fwupd will not start.
1933 **/
1934void
Richard Hughes12724852018-09-04 13:53:44 +01001935fu_plugin_add_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes08a37992017-09-12 12:57:43 +01001936{
Richard Hughes12724852018-09-04 13:53:44 +01001937 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001938 g_ptr_array_add (priv->rules[rule], g_strdup (name));
Richard Hughes75b965d2018-11-15 13:51:21 +00001939 g_signal_emit (self, signals[SIGNAL_RULES_CHANGED], 0);
Richard Hughes08a37992017-09-12 12:57:43 +01001940}
1941
1942/**
1943 * fu_plugin_get_rules:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001944 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001945 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1946 *
1947 * Gets the plugin IDs that should be run after this plugin.
1948 *
1949 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1950 **/
1951GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +01001952fu_plugin_get_rules (FuPlugin *self, FuPluginRule rule)
Richard Hughes08a37992017-09-12 12:57:43 +01001953{
Richard Hughes12724852018-09-04 13:53:44 +01001954 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001955 return priv->rules[rule];
1956}
1957
Richard Hughes80b79bb2018-01-11 21:11:06 +00001958/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001959 * fu_plugin_has_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001960 * @self: a #FuPlugin
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001961 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1962 * @name: a plugin name, e.g. `upower`
1963 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001964 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001965 *
1966 * Returns: %TRUE if the name exists for the specific rule
1967 **/
1968gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001969fu_plugin_has_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001970{
Richard Hughes12724852018-09-04 13:53:44 +01001971 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001972 for (guint i = 0; i < priv->rules[rule]->len; i++) {
1973 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
1974 if (g_strcmp0 (tmp, name) == 0)
1975 return TRUE;
1976 }
1977 return FALSE;
1978}
1979
1980/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00001981 * fu_plugin_add_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001982 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00001983 * @key: a string, e.g. `FwupdateVersion`
1984 * @value: a string, e.g. `10`
1985 *
1986 * Sets any additional metadata to be included in the firmware report to aid
1987 * debugging problems.
1988 *
1989 * Any data included here will be sent to the metadata server after user
1990 * confirmation.
1991 **/
1992void
Richard Hughes12724852018-09-04 13:53:44 +01001993fu_plugin_add_report_metadata (FuPlugin *self, const gchar *key, const gchar *value)
Richard Hughes80b79bb2018-01-11 21:11:06 +00001994{
Richard Hughes12724852018-09-04 13:53:44 +01001995 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001996 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1997}
1998
1999/**
2000 * fu_plugin_get_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002001 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00002002 *
2003 * Returns the list of additional metadata to be added when filing a report.
2004 *
2005 * Returns: (transfer none): the map of report metadata
2006 **/
2007GHashTable *
Richard Hughes12724852018-09-04 13:53:44 +01002008fu_plugin_get_report_metadata (FuPlugin *self)
Richard Hughes80b79bb2018-01-11 21:11:06 +00002009{
Richard Hughes12724852018-09-04 13:53:44 +01002010 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002011 return priv->report_metadata;
2012}
2013
Mario Limonciello963dc422018-02-27 14:26:58 -06002014/**
2015 * fu_plugin_get_config_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002016 * @self: a #FuPlugin
Mario Limonciello963dc422018-02-27 14:26:58 -06002017 * @key: A settings key
2018 *
2019 * Return the value of a key if it's been configured
2020 *
2021 * Since: 1.0.6
2022 **/
2023gchar *
Richard Hughes12724852018-09-04 13:53:44 +01002024fu_plugin_get_config_value (FuPlugin *self, const gchar *key)
Mario Limonciello963dc422018-02-27 14:26:58 -06002025{
Richard Hughes4be17d12018-05-30 20:36:29 +01002026 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06002027 g_autofree gchar *conf_file = NULL;
2028 g_autofree gchar *conf_path = NULL;
2029 g_autoptr(GKeyFile) keyfile = NULL;
2030 const gchar *plugin_name;
2031
Richard Hughes4be17d12018-05-30 20:36:29 +01002032 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Richard Hughes12724852018-09-04 13:53:44 +01002033 plugin_name = fu_plugin_get_name (self);
Mario Limonciello963dc422018-02-27 14:26:58 -06002034 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01002035 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06002036 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
2037 return NULL;
2038 keyfile = g_key_file_new ();
2039 if (!g_key_file_load_from_file (keyfile, conf_path,
2040 G_KEY_FILE_NONE, NULL))
2041 return NULL;
2042 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
2043}
2044
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002045/**
2046 * fu_plugin_name_compare:
2047 * @plugin1: first #FuPlugin to compare.
2048 * @plugin2: second #FuPlugin to compare.
2049 *
2050 * Compares two plugins by their names.
2051 *
2052 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
2053 **/
2054gint
2055fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2056{
2057 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
2058 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
2059 return g_strcmp0 (priv1->name, priv2->name);
2060}
2061
2062/**
2063 * fu_plugin_order_compare:
2064 * @plugin1: first #FuPlugin to compare.
2065 * @plugin2: second #FuPlugin to compare.
2066 *
2067 * Compares two plugins by their depsolved order.
2068 *
2069 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
2070 **/
2071gint
2072fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2073{
2074 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
2075 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
2076 if (priv1->order < priv2->order)
2077 return -1;
2078 if (priv1->order > priv2->order)
2079 return 1;
2080 return 0;
2081}
2082
Richard Hughescff38bc2016-12-12 12:03:37 +00002083static void
2084fu_plugin_class_init (FuPluginClass *klass)
2085{
2086 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2087 object_class->finalize = fu_plugin_finalize;
2088 signals[SIGNAL_DEVICE_ADDED] =
2089 g_signal_new ("device-added",
2090 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2091 G_STRUCT_OFFSET (FuPluginClass, device_added),
2092 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2093 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
2094 signals[SIGNAL_DEVICE_REMOVED] =
2095 g_signal_new ("device-removed",
2096 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2097 G_STRUCT_OFFSET (FuPluginClass, device_removed),
2098 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2099 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01002100 signals[SIGNAL_DEVICE_REGISTER] =
2101 g_signal_new ("device-register",
2102 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2103 G_STRUCT_OFFSET (FuPluginClass, device_register),
2104 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2105 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00002106 signals[SIGNAL_RECOLDPLUG] =
2107 g_signal_new ("recoldplug",
2108 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2109 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
2110 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2111 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00002112 signals[SIGNAL_SET_COLDPLUG_DELAY] =
2113 g_signal_new ("set-coldplug-delay",
2114 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2115 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
2116 NULL, NULL, g_cclosure_marshal_VOID__UINT,
2117 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughesaabdc372018-11-14 10:11:08 +00002118 signals[SIGNAL_CHECK_SUPPORTED] =
2119 g_signal_new ("check-supported",
2120 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2121 G_STRUCT_OFFSET (FuPluginClass, check_supported),
2122 NULL, NULL, g_cclosure_marshal_generic,
2123 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
Richard Hughes75b965d2018-11-15 13:51:21 +00002124 signals[SIGNAL_RULES_CHANGED] =
2125 g_signal_new ("rules-changed",
2126 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2127 G_STRUCT_OFFSET (FuPluginClass, rules_changed),
2128 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2129 G_TYPE_NONE, 0);
Richard Hughescff38bc2016-12-12 12:03:37 +00002130}
2131
2132static void
Richard Hughes12724852018-09-04 13:53:44 +01002133fu_plugin_init (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +00002134{
Richard Hughes12724852018-09-04 13:53:44 +01002135 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002136 priv->enabled = TRUE;
Richard Hughesb1065422019-08-15 16:44:34 +01002137 priv->udev_subsystems = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00002138 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
2139 g_free, (GDestroyNotify) g_object_unref);
Richard Hughes161e9b52019-06-12 14:22:45 +01002140 g_rw_lock_init (&priv->devices_mutex);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002141 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 +01002142 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
2143 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00002144}
2145
2146static void
2147fu_plugin_finalize (GObject *object)
2148{
Richard Hughes12724852018-09-04 13:53:44 +01002149 FuPlugin *self = FU_PLUGIN (object);
2150 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002151 FuPluginInitFunc func = NULL;
2152
2153 /* optional */
2154 if (priv->module != NULL) {
2155 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
2156 if (func != NULL) {
2157 g_debug ("performing destroy() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01002158 func (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002159 }
2160 }
2161
Richard Hughes08a37992017-09-12 12:57:43 +01002162 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
2163 g_ptr_array_unref (priv->rules[i]);
2164
Richard Hughescff38bc2016-12-12 12:03:37 +00002165 if (priv->usb_ctx != NULL)
2166 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01002167 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01002168 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01002169 if (priv->quirks != NULL)
2170 g_object_unref (priv->quirks);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002171 if (priv->udev_subsystems != NULL)
2172 g_ptr_array_unref (priv->udev_subsystems);
Richard Hughes49e5e052017-09-03 12:15:41 +01002173 if (priv->smbios != NULL)
2174 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01002175 if (priv->runtime_versions != NULL)
2176 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01002177 if (priv->compile_versions != NULL)
2178 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00002179 g_hash_table_unref (priv->devices);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002180 g_hash_table_unref (priv->report_metadata);
Richard Hughes161e9b52019-06-12 14:22:45 +01002181 g_rw_lock_clear (&priv->devices_mutex);
Richard Hughes84999302019-05-02 10:18:32 +01002182 g_free (priv->build_hash);
Richard Hughescff38bc2016-12-12 12:03:37 +00002183 g_free (priv->name);
2184 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05002185 /* Must happen as the last step to avoid prematurely
2186 * freeing memory held by the plugin */
2187#ifndef RUNNING_ON_VALGRIND
2188 if (priv->module != NULL)
2189 g_module_close (priv->module);
2190#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00002191
2192 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
2193}
2194
2195FuPlugin *
2196fu_plugin_new (void)
2197{
Richard Hughes12724852018-09-04 13:53:44 +01002198 return FU_PLUGIN (g_object_new (FU_TYPE_PLUGIN, NULL));
Richard Hughescff38bc2016-12-12 12:03:37 +00002199}