blob: b806e8112a2ee24843cdf160c68365e7c97c81ec [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>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060016#ifdef HAVE_VALGRIND
Richard Hughes576c0122017-02-24 09:47:00 +000017#include <valgrind.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060018#endif /* HAVE_VALGRIND */
Richard Hughesd0905142016-03-13 09:46:49 +000019
Richard Hughes9dde04f2017-09-13 12:07:15 +010020#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000021#include "fu-plugin-private.h"
Richard Hughes37d09432018-09-09 10:39:45 +010022#include "fu-mutex.h"
Richard Hughesd0905142016-03-13 09:46:49 +000023
Richard Hughes4eada342017-10-03 21:20:32 +010024/**
25 * SECTION:fu-plugin
26 * @short_description: a daemon plugin
27 *
28 * An object that represents a plugin run by the daemon.
29 *
30 * See also: #FuDevice
31 */
32
Richard Hughesb0829032017-01-10 09:27:08 +000033#define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */
34
Richard Hughescff38bc2016-12-12 12:03:37 +000035static void fu_plugin_finalize (GObject *object);
36
37typedef struct {
38 GModule *module;
39 GUsbContext *usb_ctx;
40 gboolean enabled;
Richard Hughes08a37992017-09-12 12:57:43 +010041 guint order;
Richard Hughes81c427c2018-08-06 15:20:17 +010042 guint priority;
Richard Hughes08a37992017-09-12 12:57:43 +010043 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughescff38bc2016-12-12 12:03:37 +000044 gchar *name;
Richard Hughesf425d292019-01-18 17:57:39 +000045 gchar *build_hash;
Richard Hughesd7704d42017-08-08 20:29:09 +010046 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010047 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010048 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010049 GHashTable *compile_versions;
Richard Hughes9d6e0e72018-08-24 20:20:17 +010050 GPtrArray *udev_subsystems;
Richard Hughes1354ea92017-09-19 15:58:31 +010051 FuSmbios *smbios;
Richard Hughes989acf12019-10-05 20:16:47 +010052 GType device_gtype;
Richard Hughescff38bc2016-12-12 12:03:37 +000053 GHashTable *devices; /* platform_id:GObject */
Richard Hughes161e9b52019-06-12 14:22:45 +010054 GRWLock devices_mutex;
Richard Hughes80b79bb2018-01-11 21:11:06 +000055 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000056 FuPluginData *data;
57} FuPluginPrivate;
58
59enum {
60 SIGNAL_DEVICE_ADDED,
61 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010062 SIGNAL_DEVICE_REGISTER,
Richard Hughes75b965d2018-11-15 13:51:21 +000063 SIGNAL_RULES_CHANGED,
Richard Hughes362d6d72017-01-07 21:42:14 +000064 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000065 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughesaabdc372018-11-14 10:11:08 +000066 SIGNAL_CHECK_SUPPORTED,
Richard Hughes95c98a92019-10-22 16:03:15 +010067 SIGNAL_ADD_FIRMWARE_GTYPE,
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/**
Mario Limonciello52e75ba2019-11-22 13:21:19 -0600109 * fu_plugin_is_open:
110 * @self: A #FuPlugin
111 *
112 * Determines if the plugin is opened
113 *
114 * Returns: TRUE for opened, FALSE for not
115 *
116 * Since: 1.3.5
117 **/
118gboolean
119fu_plugin_is_open (FuPlugin *self)
120{
121 FuPluginPrivate *priv = GET_PRIVATE (self);
122 return priv->module != NULL;
123}
124
125/**
Richard Hughes57d18222017-01-10 16:02:59 +0000126 * fu_plugin_get_name:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100127 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000128 *
129 * Gets the plugin name.
130 *
131 * Returns: a plugin name, or %NULL for unknown.
132 *
133 * Since: 0.8.0
134 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000135const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100136fu_plugin_get_name (FuPlugin *self)
Richard Hughesd0905142016-03-13 09:46:49 +0000137{
Richard Hughes12724852018-09-04 13:53:44 +0100138 FuPluginPrivate *priv = GET_PRIVATE (self);
139 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000140 return priv->name;
141}
Richard Hughesd0905142016-03-13 09:46:49 +0000142
Mario Limonciello1a680f32019-11-25 19:44:53 -0600143/**
144 * fu_plugin_set_name:
145 * @self: A #FuPlugin
146 * @name: A string
147 *
148 * Sets the plugin name.
149 *
150 * Since: 0.8.0
151 **/
Richard Hughes34834102017-11-21 21:55:00 +0000152void
Richard Hughes12724852018-09-04 13:53:44 +0100153fu_plugin_set_name (FuPlugin *self, const gchar *name)
Richard Hughes34834102017-11-21 21:55:00 +0000154{
Richard Hughes12724852018-09-04 13:53:44 +0100155 FuPluginPrivate *priv = GET_PRIVATE (self);
156 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughes34834102017-11-21 21:55:00 +0000157 g_return_if_fail (name != NULL);
158 g_free (priv->name);
159 priv->name = g_strdup (name);
160}
161
Richard Hughes57d18222017-01-10 16:02:59 +0000162/**
Richard Hughesf425d292019-01-18 17:57:39 +0000163 * fu_plugin_set_build_hash:
164 * @self: A #FuPlugin
165 * @build_hash: A checksum
166 *
167 * Sets the plugin build hash, typically a SHA256 checksum. All plugins must
168 * set the correct checksum to avoid the daemon being marked as tainted.
169 *
170 * Since: 1.2.4
171 **/
172void
173fu_plugin_set_build_hash (FuPlugin *self, const gchar *build_hash)
174{
175 FuPluginPrivate *priv = GET_PRIVATE (self);
176 g_return_if_fail (FU_IS_PLUGIN (self));
177 g_return_if_fail (build_hash != NULL);
178 g_free (priv->build_hash);
179 priv->build_hash = g_strdup (build_hash);
180}
181
Mario Limonciello1a680f32019-11-25 19:44:53 -0600182/**
183 * fu_plugin_get_build_hash:
184 * @self: A #FuPlugin
185 *
186 * Gets the build hash a plugin was generated with.
187 *
188 * Returns: (transfer none): a #gchar, or %NULL for unset.
189 *
190 * Since: 1.2.4
191 **/
Richard Hughesf425d292019-01-18 17:57:39 +0000192const gchar *
193fu_plugin_get_build_hash (FuPlugin *self)
194{
195 FuPluginPrivate *priv = GET_PRIVATE (self);
196 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
197 return priv->build_hash;
198}
199
200/**
Richard Hughes57d18222017-01-10 16:02:59 +0000201 * fu_plugin_cache_lookup:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100202 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000203 * @id: the key
204 *
205 * Finds an object in the per-plugin cache.
206 *
207 * Returns: (transfer none): a #GObject, or %NULL for unfound.
208 *
209 * Since: 0.8.0
210 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000211gpointer
Richard Hughes12724852018-09-04 13:53:44 +0100212fu_plugin_cache_lookup (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000213{
Richard Hughes12724852018-09-04 13:53:44 +0100214 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes161e9b52019-06-12 14:22:45 +0100215 g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_reader_locker_new (&priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100216 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughesccd78a92017-01-11 16:57:41 +0000217 g_return_val_if_fail (id != NULL, NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100218 g_return_val_if_fail (locker != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000219 return g_hash_table_lookup (priv->devices, id);
220}
Richard Hughesd0905142016-03-13 09:46:49 +0000221
Richard Hughes57d18222017-01-10 16:02:59 +0000222/**
223 * fu_plugin_cache_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100224 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000225 * @id: the key
226 * @dev: a #GObject, typically a #FuDevice
227 *
228 * Adds an object to the per-plugin cache.
229 *
230 * Since: 0.8.0
231 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000232void
Richard Hughes12724852018-09-04 13:53:44 +0100233fu_plugin_cache_add (FuPlugin *self, const gchar *id, gpointer dev)
Richard Hughescff38bc2016-12-12 12:03:37 +0000234{
Richard Hughes12724852018-09-04 13:53:44 +0100235 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0fe49142019-11-22 16:56:38 +0000236 g_autoptr(GRWLockWriterLocker) locker = g_rw_lock_writer_locker_new (&priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100237 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000238 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100239 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000240 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
241}
242
Richard Hughes57d18222017-01-10 16:02:59 +0000243/**
244 * fu_plugin_cache_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100245 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000246 * @id: the key
247 *
248 * Removes an object from the per-plugin cache.
249 *
250 * Since: 0.8.0
251 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000252void
Richard Hughes12724852018-09-04 13:53:44 +0100253fu_plugin_cache_remove (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000254{
Richard Hughes12724852018-09-04 13:53:44 +0100255 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0fe49142019-11-22 16:56:38 +0000256 g_autoptr(GRWLockWriterLocker) locker = g_rw_lock_writer_locker_new (&priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100257 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000258 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100259 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000260 g_hash_table_remove (priv->devices, id);
261}
262
Richard Hughes57d18222017-01-10 16:02:59 +0000263/**
264 * fu_plugin_get_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100265 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000266 *
Richard Hughes4eada342017-10-03 21:20:32 +0100267 * Gets the per-plugin allocated private data. This will return %NULL unless
268 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000269 *
Richard Hughes4eada342017-10-03 21:20:32 +0100270 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000271 *
272 * Since: 0.8.0
273 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000274FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100275fu_plugin_get_data (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000276{
Richard Hughes12724852018-09-04 13:53:44 +0100277 FuPluginPrivate *priv = GET_PRIVATE (self);
278 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000279 return priv->data;
280}
281
Richard Hughes57d18222017-01-10 16:02:59 +0000282/**
Richard Hughes00f66f62019-11-27 11:42:53 +0000283 * fu_plugin_alloc_data: (skip):
Richard Hughes2c0635a2018-09-04 14:52:46 +0100284 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000285 * @data_sz: the size to allocate
286 *
287 * Allocates the per-plugin allocated private data.
288 *
289 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
290 *
291 * Since: 0.8.0
292 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000293FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100294fu_plugin_alloc_data (FuPlugin *self, gsize data_sz)
Richard Hughescff38bc2016-12-12 12:03:37 +0000295{
Richard Hughes12724852018-09-04 13:53:44 +0100296 FuPluginPrivate *priv = GET_PRIVATE (self);
297 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000298 if (priv->data != NULL) {
299 g_critical ("fu_plugin_alloc_data() already used by plugin");
300 return priv->data;
301 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000302 priv->data = g_malloc0 (data_sz);
303 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000304}
305
Richard Hughes57d18222017-01-10 16:02:59 +0000306/**
307 * fu_plugin_get_usb_context:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100308 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000309 *
310 * Gets the shared USB context that all plugins can use.
311 *
312 * Returns: (transfer none): a #GUsbContext.
313 *
314 * Since: 0.8.0
315 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000316GUsbContext *
Richard Hughes12724852018-09-04 13:53:44 +0100317fu_plugin_get_usb_context (FuPlugin *self)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000318{
Richard Hughes12724852018-09-04 13:53:44 +0100319 FuPluginPrivate *priv = GET_PRIVATE (self);
320 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000321 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000322}
323
Mario Limonciello1a680f32019-11-25 19:44:53 -0600324/**
325 * fu_plugin_set_usb_context:
326 * @self: A #FuPlugin
327 * @usb_ctx: A #FGUsbContext
328 *
329 * Sets the shared USB context for a plugin
330 *
331 * Since: 0.8.0
332 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000333void
Richard Hughes12724852018-09-04 13:53:44 +0100334fu_plugin_set_usb_context (FuPlugin *self, GUsbContext *usb_ctx)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000335{
Richard Hughes12724852018-09-04 13:53:44 +0100336 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000337 g_set_object (&priv->usb_ctx, usb_ctx);
338}
339
Richard Hughes57d18222017-01-10 16:02:59 +0000340/**
341 * fu_plugin_get_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100342 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000343 *
Richard Hughes4eada342017-10-03 21:20:32 +0100344 * Returns if the plugin is enabled. Plugins may self-disable using
345 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000346 *
347 * Returns: %TRUE if the plugin is currently enabled.
348 *
349 * Since: 0.8.0
350 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000351gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100352fu_plugin_get_enabled (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000353{
Richard Hughes12724852018-09-04 13:53:44 +0100354 FuPluginPrivate *priv = GET_PRIVATE (self);
355 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000356 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000357}
358
Richard Hughes57d18222017-01-10 16:02:59 +0000359/**
360 * fu_plugin_set_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100361 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000362 * @enabled: the enabled value
363 *
364 * Enables or disables a plugin. Plugins can self-disable at any point.
365 *
366 * Since: 0.8.0
367 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000368void
Richard Hughes12724852018-09-04 13:53:44 +0100369fu_plugin_set_enabled (FuPlugin *self, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000370{
Richard Hughes12724852018-09-04 13:53:44 +0100371 FuPluginPrivate *priv = GET_PRIVATE (self);
372 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000373 priv->enabled = enabled;
374}
375
Mario Limonciello1a680f32019-11-25 19:44:53 -0600376/**
377 * fu_plugin_guess_name_from_fn:
378 * @filename: filename to guess
379 *
380 * Tries to guess the name of the plugin from a filename
381 *
382 * Returns: (transfer full): the guessed name of the plugin
383 *
384 * Since: 1.0.8
385 **/
Richard Hughes1e456bc2018-05-10 20:16:16 +0100386gchar *
387fu_plugin_guess_name_from_fn (const gchar *filename)
388{
389 const gchar *prefix = "libfu_plugin_";
390 gchar *name;
391 gchar *str = g_strstr_len (filename, -1, prefix);
392 if (str == NULL)
393 return NULL;
394 name = g_strdup (str + strlen (prefix));
395 g_strdelimit (name, ".", '\0');
396 return name;
397}
398
Mario Limonciello1a680f32019-11-25 19:44:53 -0600399/**
400 * fu_plugin_open:
401 * @self: A #FuPlugin
402 * @filename: The shared object filename to open
403 * @error: A #GError or NULL
404 *
405 * Opens the plugin module
406 *
407 * Returns: TRUE for success, FALSE for fail
408 *
409 * Since: 0.8.0
410 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000411gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100412fu_plugin_open (FuPlugin *self, const gchar *filename, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +0000413{
Richard Hughes12724852018-09-04 13:53:44 +0100414 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000415 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000416
417 priv->module = g_module_open (filename, 0);
418 if (priv->module == NULL) {
419 g_set_error (error,
420 G_IO_ERROR,
421 G_IO_ERROR_FAILED,
Mario Limonciellof5605532019-11-04 07:49:50 -0600422 "failed to open plugin %s: %s",
423 filename, g_module_error ());
Richard Hughescff38bc2016-12-12 12:03:37 +0000424 return FALSE;
425 }
426
427 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100428 if (priv->name == NULL)
429 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000430
431 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000432 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
433 if (func != NULL) {
434 g_debug ("performing init() on %s", filename);
Richard Hughes12724852018-09-04 13:53:44 +0100435 func (self);
Richard Hughesd0905142016-03-13 09:46:49 +0000436 }
437
Richard Hughescff38bc2016-12-12 12:03:37 +0000438 return TRUE;
439}
440
Richard Hughes57d18222017-01-10 16:02:59 +0000441/**
442 * fu_plugin_device_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100443 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000444 * @device: A #FuDevice
445 *
446 * Asks the daemon to add a device to the exported list. If this device ID
447 * has already been added by a different plugin then this request will be
448 * ignored.
449 *
450 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
451 * actually flashing an image to the hardware so that higher-priority plugins
452 * can add the device themselves.
453 *
454 * Since: 0.8.0
455 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000456void
Richard Hughes12724852018-09-04 13:53:44 +0100457fu_plugin_device_add (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000458{
Richard Hughes5e447292018-04-27 14:25:54 +0100459 GPtrArray *children;
Richard Hughesc125ec02018-09-05 19:35:17 +0100460 g_autoptr(GError) error = NULL;
Richard Hughes5e447292018-04-27 14:25:54 +0100461
Richard Hughes12724852018-09-04 13:53:44 +0100462 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000463 g_return_if_fail (FU_IS_DEVICE (device));
464
Richard Hughesc125ec02018-09-05 19:35:17 +0100465 /* ensure the device ID is set from the physical and logical IDs */
466 if (!fu_device_ensure_id (device, &error)) {
467 g_warning ("ignoring add: %s", error->message);
468 return;
469 }
470
Richard Hughescff38bc2016-12-12 12:03:37 +0000471 g_debug ("emit added from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100472 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000473 fu_device_get_id (device));
474 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
Richard Hughes12724852018-09-04 13:53:44 +0100475 fu_device_set_plugin (device, fu_plugin_get_name (self));
476 g_signal_emit (self, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100477
Richard Hughes128c0162018-08-10 11:00:29 +0100478 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100479 children = fu_device_get_children (device);
480 for (guint i = 0; i < children->len; i++) {
481 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100482 if (fu_device_get_created (child) == 0)
Richard Hughes12724852018-09-04 13:53:44 +0100483 fu_plugin_device_add (self, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100484 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000485}
486
Richard Hughese1fd34d2017-08-24 14:19:51 +0100487/**
488 * fu_plugin_device_register:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100489 * @self: A #FuPlugin
Richard Hughese1fd34d2017-08-24 14:19:51 +0100490 * @device: A #FuDevice
491 *
492 * Registers the device with other plugins so they can set metadata.
493 *
494 * Plugins do not have to call this manually as this is done automatically
495 * when using fu_plugin_device_add(). They may wish to use this manually
Richard Hughes21eaeef2020-01-14 12:10:01 +0000496 * if for instance the coldplug should be ignored based on the metadata
Richard Hughese1fd34d2017-08-24 14:19:51 +0100497 * set from other plugins.
498 *
499 * Since: 0.9.7
500 **/
501void
Richard Hughes12724852018-09-04 13:53:44 +0100502fu_plugin_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +0100503{
Richard Hughesc125ec02018-09-05 19:35:17 +0100504 g_autoptr(GError) error = NULL;
505
Richard Hughes12724852018-09-04 13:53:44 +0100506 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughese1fd34d2017-08-24 14:19:51 +0100507 g_return_if_fail (FU_IS_DEVICE (device));
508
Richard Hughesc125ec02018-09-05 19:35:17 +0100509 /* ensure the device ID is set from the physical and logical IDs */
510 if (!fu_device_ensure_id (device, &error)) {
511 g_warning ("ignoring registration: %s", error->message);
512 return;
513 }
514
Richard Hughese1fd34d2017-08-24 14:19:51 +0100515 g_debug ("emit device-register from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100516 fu_plugin_get_name (self),
Richard Hughese1fd34d2017-08-24 14:19:51 +0100517 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100518 g_signal_emit (self, signals[SIGNAL_DEVICE_REGISTER], 0, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +0100519}
520
Richard Hughes57d18222017-01-10 16:02:59 +0000521/**
Richard Hughes4eada342017-10-03 21:20:32 +0100522 * fu_plugin_device_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100523 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000524 * @device: A #FuDevice
525 *
526 * Asks the daemon to remove a device from the exported list.
527 *
528 * Since: 0.8.0
529 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000530void
Richard Hughes12724852018-09-04 13:53:44 +0100531fu_plugin_device_remove (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000532{
Richard Hughes12724852018-09-04 13:53:44 +0100533 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000534 g_return_if_fail (FU_IS_DEVICE (device));
535
Richard Hughescff38bc2016-12-12 12:03:37 +0000536 g_debug ("emit removed from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100537 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000538 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100539 g_signal_emit (self, signals[SIGNAL_DEVICE_REMOVED], 0, device);
Richard Hughescff38bc2016-12-12 12:03:37 +0000540}
541
Richard Hughes57d18222017-01-10 16:02:59 +0000542/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000543 * fu_plugin_request_recoldplug:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100544 * @self: A #FuPlugin
Richard Hughes362d6d72017-01-07 21:42:14 +0000545 *
546 * Ask all the plugins to coldplug all devices, which will include the prepare()
547 * and cleanup() phases. Duplicate devices added will be ignored.
548 *
549 * Since: 0.8.0
550 **/
551void
Richard Hughes12724852018-09-04 13:53:44 +0100552fu_plugin_request_recoldplug (FuPlugin *self)
Richard Hughes362d6d72017-01-07 21:42:14 +0000553{
Richard Hughes12724852018-09-04 13:53:44 +0100554 g_return_if_fail (FU_IS_PLUGIN (self));
555 g_signal_emit (self, signals[SIGNAL_RECOLDPLUG], 0);
Richard Hughes362d6d72017-01-07 21:42:14 +0000556}
557
Richard Hughesb0829032017-01-10 09:27:08 +0000558/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100559 * fu_plugin_check_hwid:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100560 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100561 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100562 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100563 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100564 * specific system can be shown using the `fwupdmgr hwids` command.
565 *
Richard Hughes4eada342017-10-03 21:20:32 +0100566 * Returns: %TRUE if the HwId is found on the system.
567 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100568 * Since: 0.9.1
569 **/
570gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100571fu_plugin_check_hwid (FuPlugin *self, const gchar *hwid)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100572{
Richard Hughes12724852018-09-04 13:53:44 +0100573 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100574 if (priv->hwids == NULL)
575 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100576 return fu_hwids_has_guid (priv->hwids, hwid);
577}
578
579/**
Patrick Rudolpha60b5472019-10-16 10:43:03 +0200580 * fu_plugin_get_hwid_replace_value:
581 * @self: A #FuPlugin
582 * @keys: A key, e.g. `HardwareID-3` or %FU_HWIDS_KEY_PRODUCT_SKU
583 * @error: A #GError or %NULL
584 *
585 * Gets the replacement value for a specific key. All hardware IDs on a
586 * specific system can be shown using the `fwupdmgr hwids` command.
587 *
588 * Returns: (transfer full): a string, or %NULL for error.
589 *
590 * Since: 1.3.3
591 **/
592gchar *
593fu_plugin_get_hwid_replace_value (FuPlugin *self, const gchar *keys, GError **error)
594{
595 FuPluginPrivate *priv = GET_PRIVATE (self);
596 if (priv->hwids == NULL)
597 return NULL;
598
599 return fu_hwids_get_replace_values (priv->hwids, keys, error);
600}
601
602/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100603 * fu_plugin_get_hwids:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100604 * @self: A #FuPlugin
Richard Hughes69a5f352018-08-08 11:58:15 +0100605 *
606 * Returns all the HWIDs defined in the system. All hardware IDs on a
607 * specific system can be shown using the `fwupdmgr hwids` command.
608 *
Mario Limonciello1a680f32019-11-25 19:44:53 -0600609 * Returns: (transfer none) (element-type utf8): An array of GUIDs
Richard Hughes69a5f352018-08-08 11:58:15 +0100610 *
611 * Since: 1.1.1
612 **/
613GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +0100614fu_plugin_get_hwids (FuPlugin *self)
Richard Hughes69a5f352018-08-08 11:58:15 +0100615{
Richard Hughes12724852018-09-04 13:53:44 +0100616 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes69a5f352018-08-08 11:58:15 +0100617 if (priv->hwids == NULL)
618 return NULL;
619 return fu_hwids_get_guids (priv->hwids);
620}
621
622/**
Richard Hughes19841802019-09-10 16:48:00 +0100623 * fu_plugin_has_custom_flag:
624 * @self: A #FuPlugin
625 * @flag: A custom text flag, specific to the plugin, e.g. `uefi-force-enable`
626 *
627 * Returns if a per-plugin HwId custom flag exists, typically added from a DMI quirk.
628 *
629 * Returns: %TRUE if the quirk entry exists
630 *
631 * Since: 1.3.1
632 **/
633gboolean
634fu_plugin_has_custom_flag (FuPlugin *self, const gchar *flag)
635{
636 FuPluginPrivate *priv = GET_PRIVATE (self);
637 GPtrArray *hwids = fu_plugin_get_hwids (self);
638
639 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
640 g_return_val_if_fail (flag != NULL, FALSE);
641
642 /* never set up, e.g. in tests */
643 if (hwids == NULL)
644 return FALSE;
645
646 /* search each hwid */
647 for (guint i = 0; i < hwids->len; i++) {
648 const gchar *hwid = g_ptr_array_index (hwids, i);
649 const gchar *value;
650 g_autofree gchar *key = g_strdup_printf ("HwId=%s", hwid);
651
652 /* does prefixed quirk exist */
653 value = fu_quirks_lookup_by_id (priv->quirks, key, FU_QUIRKS_FLAGS);
654 if (value != NULL) {
655 g_auto(GStrv) quirks = g_strsplit (value, ",", -1);
656 if (g_strv_contains ((const gchar * const *) quirks, flag))
657 return TRUE;
658 }
659 }
660 return FALSE;
661}
662
663/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100664 * fu_plugin_check_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100665 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100666 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100667 *
668 * Checks to see if a specific device GUID is supported, i.e. available in the
669 * AppStream metadata.
670 *
Richard Hughes4eada342017-10-03 21:20:32 +0100671 * Returns: %TRUE if the device is supported.
672 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100673 * Since: 1.0.0
674 **/
Richard Hughesd8a8d5e2019-10-08 13:05:02 +0100675static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100676fu_plugin_check_supported (FuPlugin *self, const gchar *guid)
Richard Hughes1354ea92017-09-19 15:58:31 +0100677{
Richard Hughesaabdc372018-11-14 10:11:08 +0000678 gboolean retval = FALSE;
679 g_signal_emit (self, signals[SIGNAL_CHECK_SUPPORTED], 0, guid, &retval);
680 return retval;
Richard Hughes1354ea92017-09-19 15:58:31 +0100681}
682
683/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100684 * fu_plugin_get_dmi_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100685 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100686 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100687 *
688 * Gets a hardware DMI value.
689 *
Richard Hughes4eada342017-10-03 21:20:32 +0100690 * Returns: The string, or %NULL
691 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100692 * Since: 0.9.7
693 **/
694const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100695fu_plugin_get_dmi_value (FuPlugin *self, const gchar *dmi_id)
Richard Hughesd7704d42017-08-08 20:29:09 +0100696{
Richard Hughes12724852018-09-04 13:53:44 +0100697 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100698 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100699 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100700 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100701}
702
Richard Hughes49e5e052017-09-03 12:15:41 +0100703/**
704 * fu_plugin_get_smbios_string:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100705 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100706 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
707 * @offset: A SMBIOS offset
708 *
709 * Gets a hardware SMBIOS string.
710 *
711 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
712 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
713 *
Richard Hughes4eada342017-10-03 21:20:32 +0100714 * Returns: A string, or %NULL
715 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100716 * Since: 0.9.8
717 **/
718const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100719fu_plugin_get_smbios_string (FuPlugin *self, guint8 structure_type, guint8 offset)
Richard Hughes49e5e052017-09-03 12:15:41 +0100720{
Richard Hughes12724852018-09-04 13:53:44 +0100721 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100722 if (priv->smbios == NULL)
723 return NULL;
724 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
725}
726
727/**
728 * fu_plugin_get_smbios_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100729 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100730 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
731 *
732 * Gets a hardware SMBIOS data.
733 *
Richard Hughesdfaca2d2019-08-01 08:08:03 +0100734 * Returns: (transfer full): A #GBytes, or %NULL
Richard Hughes4eada342017-10-03 21:20:32 +0100735 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100736 * Since: 0.9.8
737 **/
738GBytes *
Richard Hughes12724852018-09-04 13:53:44 +0100739fu_plugin_get_smbios_data (FuPlugin *self, guint8 structure_type)
Richard Hughes49e5e052017-09-03 12:15:41 +0100740{
Richard Hughes12724852018-09-04 13:53:44 +0100741 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100742 if (priv->smbios == NULL)
743 return NULL;
744 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
745}
746
Mario Limonciello1a680f32019-11-25 19:44:53 -0600747/**
748 * fu_plugin_set_hwids:
749 * @self: A #FuPlugin
750 * @hwids: A #FuHwids
751 *
752 * Sets the hwids for a plugin
753 *
754 * Since: 0.9.7
755 **/
Richard Hughesb8f8db22017-04-25 15:56:00 +0100756void
Richard Hughes12724852018-09-04 13:53:44 +0100757fu_plugin_set_hwids (FuPlugin *self, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100758{
Richard Hughes12724852018-09-04 13:53:44 +0100759 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100760 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100761}
762
Mario Limonciello1a680f32019-11-25 19:44:53 -0600763/**
764 * fu_plugin_set_udev_subsystems:
765 * @self: A #FuPlugin
Richard Hughesa0d81c72019-11-27 11:41:54 +0000766 * @udev_subsystems: (element-type utf8): A #GPtrArray
Mario Limonciello1a680f32019-11-25 19:44:53 -0600767 *
768 * Sets the udev subsystems used by a plugin
769 *
770 * Since: 1.1.2
771 **/
Richard Hughes49e5e052017-09-03 12:15:41 +0100772void
Richard Hughes12724852018-09-04 13:53:44 +0100773fu_plugin_set_udev_subsystems (FuPlugin *self, GPtrArray *udev_subsystems)
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100774{
Richard Hughes12724852018-09-04 13:53:44 +0100775 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100776 if (priv->udev_subsystems != NULL)
777 g_ptr_array_unref (priv->udev_subsystems);
778 priv->udev_subsystems = g_ptr_array_ref (udev_subsystems);
779}
780
Mario Limonciello1a680f32019-11-25 19:44:53 -0600781/**
782 * fu_plugin_set_quirks:
783 * @self: A #FuPlugin
784 * @quirks: A #FuQuirks
785 *
786 * Sets the quirks for a plugin
787 *
788 * Since: 1.0.1
789 **/
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100790void
Richard Hughes12724852018-09-04 13:53:44 +0100791fu_plugin_set_quirks (FuPlugin *self, FuQuirks *quirks)
Richard Hughes9c028f02017-10-28 21:14:28 +0100792{
Richard Hughes12724852018-09-04 13:53:44 +0100793 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100794 g_set_object (&priv->quirks, quirks);
795}
796
797/**
798 * fu_plugin_get_quirks:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100799 * @self: A #FuPlugin
Richard Hughes9c028f02017-10-28 21:14:28 +0100800 *
801 * Returns the hardware database object. This can be used to discover device
802 * quirks or other device-specific settings.
803 *
804 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
805 *
806 * Since: 1.0.1
807 **/
808FuQuirks *
Richard Hughes12724852018-09-04 13:53:44 +0100809fu_plugin_get_quirks (FuPlugin *self)
Richard Hughes9c028f02017-10-28 21:14:28 +0100810{
Richard Hughes12724852018-09-04 13:53:44 +0100811 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100812 return priv->quirks;
813}
814
Mario Limonciello1a680f32019-11-25 19:44:53 -0600815/**
816 * fu_plugin_set_runtime_versions:
817 * @self: A #FuPlugin
818 * @runtime_versions: A #GHashTables
819 *
820 * Sets the runtime versions for a plugin
821 *
822 * Since: 1.0.7
823 **/
Richard Hughes0eb123b2018-04-19 12:00:04 +0100824void
Richard Hughes12724852018-09-04 13:53:44 +0100825fu_plugin_set_runtime_versions (FuPlugin *self, GHashTable *runtime_versions)
Richard Hughes0eb123b2018-04-19 12:00:04 +0100826{
Richard Hughes12724852018-09-04 13:53:44 +0100827 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0eb123b2018-04-19 12:00:04 +0100828 priv->runtime_versions = g_hash_table_ref (runtime_versions);
829}
830
831/**
832 * fu_plugin_add_runtime_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100833 * @self: A #FuPlugin
Richard Hughes0eb123b2018-04-19 12:00:04 +0100834 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
835 * @version: A version string, e.g. "1.2.3"
836 *
Richard Hughesdce91202019-04-08 12:47:45 +0100837 * Sets a runtime version of a specific dependency.
Richard Hughes0eb123b2018-04-19 12:00:04 +0100838 *
839 * Since: 1.0.7
840 **/
841void
Richard Hughes12724852018-09-04 13:53:44 +0100842fu_plugin_add_runtime_version (FuPlugin *self,
Richard Hughes0eb123b2018-04-19 12:00:04 +0100843 const gchar *component_id,
844 const gchar *version)
845{
Richard Hughes12724852018-09-04 13:53:44 +0100846 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb01b4862018-04-20 16:39:48 +0100847 if (priv->runtime_versions == NULL)
848 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100849 g_hash_table_insert (priv->runtime_versions,
850 g_strdup (component_id),
851 g_strdup (version));
852}
853
Mario Limonciello1a680f32019-11-25 19:44:53 -0600854/**
855 * fu_plugin_set_compile_versions:
856 * @self: A #FuPlugin
857 * @compile_versions: A #GHashTables
858 *
859 * Sets the compile time versions for a plugin
860 *
861 * Since: 1.0.7
862 **/
Richard Hughes34e0dab2018-04-20 16:43:00 +0100863void
Richard Hughes12724852018-09-04 13:53:44 +0100864fu_plugin_set_compile_versions (FuPlugin *self, GHashTable *compile_versions)
Richard Hughes34e0dab2018-04-20 16:43:00 +0100865{
Richard Hughes12724852018-09-04 13:53:44 +0100866 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100867 priv->compile_versions = g_hash_table_ref (compile_versions);
868}
869
870/**
871 * fu_plugin_add_compile_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100872 * @self: A #FuPlugin
Richard Hughes34e0dab2018-04-20 16:43:00 +0100873 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
874 * @version: A version string, e.g. "1.2.3"
875 *
Richard Hughesdce91202019-04-08 12:47:45 +0100876 * Sets a compile-time version of a specific dependency.
Richard Hughes34e0dab2018-04-20 16:43:00 +0100877 *
878 * Since: 1.0.7
879 **/
880void
Richard Hughes12724852018-09-04 13:53:44 +0100881fu_plugin_add_compile_version (FuPlugin *self,
Richard Hughes34e0dab2018-04-20 16:43:00 +0100882 const gchar *component_id,
883 const gchar *version)
884{
Richard Hughes12724852018-09-04 13:53:44 +0100885 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100886 if (priv->compile_versions == NULL)
887 return;
888 g_hash_table_insert (priv->compile_versions,
889 g_strdup (component_id),
890 g_strdup (version));
891}
892
Richard Hughes9c028f02017-10-28 21:14:28 +0100893/**
894 * fu_plugin_lookup_quirk_by_id:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100895 * @self: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100896 * @group: A string, e.g. "DfuFlags"
897 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100898 *
899 * Looks up an entry in the hardware database using a string value.
900 *
901 * Returns: (transfer none): values from the database, or %NULL if not found
902 *
903 * Since: 1.0.1
904 **/
905const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100906fu_plugin_lookup_quirk_by_id (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100907{
Richard Hughes12724852018-09-04 13:53:44 +0100908 FuPluginPrivate *priv = GET_PRIVATE (self);
909 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes9c028f02017-10-28 21:14:28 +0100910
Richard Hughes9c028f02017-10-28 21:14:28 +0100911 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100912 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100913}
914
915/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100916 * fu_plugin_lookup_quirk_by_id_as_uint64:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100917 * @self: A #FuPlugin
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100918 * @group: A string, e.g. "DfuFlags"
919 * @key: An ID to match the entry, e.g. "Size"
920 *
921 * Looks up an entry in the hardware database using a string key, returning
922 * an integer value. Values are assumed base 10, unless prefixed with "0x"
923 * where they are parsed as base 16.
924 *
Mario Limonciello1a680f32019-11-25 19:44:53 -0600925 * Returns: guint64 id or 0 if not found
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100926 *
927 * Since: 1.1.2
928 **/
929guint64
Richard Hughes12724852018-09-04 13:53:44 +0100930fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100931{
Richard Hughes12724852018-09-04 13:53:44 +0100932 return fu_common_strtoull (fu_plugin_lookup_quirk_by_id (self, group, key));
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100933}
934
Mario Limonciello1a680f32019-11-25 19:44:53 -0600935/**
936 * fu_plugin_set_smbios:
937 * @self: A #FuPlugin
938 * @smbios: A #FuSmbios
939 *
940 * Sets the smbios for a plugin
941 *
942 * Since: 1.0.0
943 **/
Richard Hughes1354ea92017-09-19 15:58:31 +0100944void
Richard Hughes12724852018-09-04 13:53:44 +0100945fu_plugin_set_smbios (FuPlugin *self, FuSmbios *smbios)
Richard Hughes49e5e052017-09-03 12:15:41 +0100946{
Richard Hughes12724852018-09-04 13:53:44 +0100947 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100948 g_set_object (&priv->smbios, smbios);
949}
950
Richard Hughesb8f8db22017-04-25 15:56:00 +0100951/**
Richard Hughesb0829032017-01-10 09:27:08 +0000952 * fu_plugin_set_coldplug_delay:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100953 * @self: A #FuPlugin
Richard Hughesb0829032017-01-10 09:27:08 +0000954 * @duration: A delay in milliseconds
955 *
Richard Hughes21eaeef2020-01-14 12:10:01 +0000956 * Set the minimum time that should be waited in-between the call to
Richard Hughesb0829032017-01-10 09:27:08 +0000957 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
958 * to be the minimum hardware initialisation time from a datasheet.
959 *
960 * It is better to use this function rather than using a sleep() in the plugin
961 * itself as then only one delay is done in the daemon rather than waiting for
962 * each coldplug prepare in a serial way.
963 *
964 * Additionally, very long delays should be avoided as the daemon will be
965 * blocked from processing requests whilst the coldplug delay is being
966 * performed.
967 *
968 * Since: 0.8.0
969 **/
970void
Richard Hughes12724852018-09-04 13:53:44 +0100971fu_plugin_set_coldplug_delay (FuPlugin *self, guint duration)
Richard Hughesb0829032017-01-10 09:27:08 +0000972{
Richard Hughes12724852018-09-04 13:53:44 +0100973 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesb0829032017-01-10 09:27:08 +0000974 g_return_if_fail (duration > 0);
975
976 /* check sanity */
977 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
978 g_warning ("duration of %ums is crazy, truncating to %ums",
979 duration,
980 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
981 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
982 }
983
984 /* emit */
Richard Hughes12724852018-09-04 13:53:44 +0100985 g_signal_emit (self, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
Richard Hughesb0829032017-01-10 09:27:08 +0000986}
987
Richard Hughes4b303802019-10-04 13:22:51 +0100988static gboolean
989fu_plugin_device_attach (FuPlugin *self, FuDevice *device, GError **error)
990{
991 g_autoptr(FuDeviceLocker) locker = NULL;
992 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_IS_BOOTLOADER)) {
993 g_debug ("already in runtime mode, skipping");
994 return TRUE;
995 }
996 locker = fu_device_locker_new (device, error);
997 if (locker == NULL)
998 return FALSE;
999 return fu_device_attach (device, error);
1000}
1001
1002static gboolean
1003fu_plugin_device_detach (FuPlugin *self, FuDevice *device, GError **error)
1004{
1005 g_autoptr(FuDeviceLocker) locker = NULL;
1006 if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_IS_BOOTLOADER)) {
1007 g_debug ("already in bootloader mode, skipping");
1008 return TRUE;
1009 }
1010 locker = fu_device_locker_new (device, error);
1011 if (locker == NULL)
1012 return FALSE;
1013 return fu_device_detach (device, error);
1014}
1015
1016static gboolean
Richard Hughes4b303802019-10-04 13:22:51 +01001017fu_plugin_device_activate (FuPlugin *self, FuDevice *device, GError **error)
1018{
1019 g_autoptr(FuDeviceLocker) locker = NULL;
1020 locker = fu_device_locker_new (device, error);
1021 if (locker == NULL)
1022 return FALSE;
1023 return fu_device_activate (device, error);
1024}
1025
1026static gboolean
1027fu_plugin_device_write_firmware (FuPlugin *self, FuDevice *device,
1028 GBytes *fw, FwupdInstallFlags flags,
1029 GError **error)
1030{
1031 g_autoptr(FuDeviceLocker) locker = NULL;
1032 locker = fu_device_locker_new (device, error);
1033 if (locker == NULL)
1034 return FALSE;
1035 return fu_device_write_firmware (device, fw, flags, error);
1036}
1037
Richard Hughes7f677212019-10-05 16:19:40 +01001038static gboolean
1039fu_plugin_device_read_firmware (FuPlugin *self, FuDevice *device, GError **error)
1040{
1041 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughesf0eb0912019-10-10 11:37:22 +01001042 g_autoptr(FuFirmware) firmware = NULL;
Richard Hughes7f677212019-10-05 16:19:40 +01001043 g_autoptr(GBytes) fw = NULL;
1044 GChecksumType checksum_types[] = {
1045 G_CHECKSUM_SHA1,
1046 G_CHECKSUM_SHA256,
1047 0 };
1048 locker = fu_device_locker_new (device, error);
1049 if (locker == NULL)
1050 return FALSE;
1051 if (!fu_device_detach (device, error))
1052 return FALSE;
Richard Hughesf0eb0912019-10-10 11:37:22 +01001053 firmware = fu_device_read_firmware (device, error);
1054 if (firmware == NULL) {
1055 g_autoptr(GError) error_local = NULL;
1056 if (!fu_device_attach (device, &error_local))
1057 g_debug ("ignoring attach failure: %s", error_local->message);
1058 g_prefix_error (error, "failed to read firmware: ");
1059 return FALSE;
1060 }
1061 fw = fu_firmware_write (firmware, error);
Richard Hughes7f677212019-10-05 16:19:40 +01001062 if (fw == NULL) {
1063 g_autoptr(GError) error_local = NULL;
1064 if (!fu_device_attach (device, &error_local))
Richard Hughesf0eb0912019-10-10 11:37:22 +01001065 g_debug ("ignoring attach failure: %s", error_local->message);
1066 g_prefix_error (error, "failed to write firmware: ");
Richard Hughes7f677212019-10-05 16:19:40 +01001067 return FALSE;
1068 }
1069 for (guint i = 0; checksum_types[i] != 0; i++) {
1070 g_autofree gchar *hash = NULL;
1071 hash = g_compute_checksum_for_bytes (checksum_types[i], fw);
1072 fu_device_add_checksum (device, hash);
1073 }
1074 return fu_device_attach (device, error);
1075}
1076
Mario Limonciello1a680f32019-11-25 19:44:53 -06001077/**
1078 * fu_plugin_runner_startup:
1079 * @self: a #FuPlugin
1080 * @error: a #GError or NULL
1081 *
1082 * Runs the startup routine for the plugin
1083 *
1084 * Returns: #TRUE for success, #FALSE for failure
1085 *
1086 * Since: 0.8.0
1087 **/
Richard Hughesd0905142016-03-13 09:46:49 +00001088gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001089fu_plugin_runner_startup (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001090{
Richard Hughes12724852018-09-04 13:53:44 +01001091 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001092 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001093 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +00001094
1095 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +00001096 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +00001097 return TRUE;
1098
Richard Hughes639da472018-01-06 22:35:04 +00001099 /* no object loaded */
1100 if (priv->module == NULL)
1101 return TRUE;
1102
Richard Hughesd0905142016-03-13 09:46:49 +00001103 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001104 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
1105 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001106 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001107 g_debug ("performing startup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001108 if (!func (self, &error_local)) {
1109 if (error_local == NULL) {
1110 g_critical ("unset error in plugin %s for startup()",
1111 priv->name);
1112 g_set_error_literal (&error_local,
1113 FWUPD_ERROR,
1114 FWUPD_ERROR_INTERNAL,
1115 "unspecified error");
1116 }
1117 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1118 "failed to startup using %s: ",
1119 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001120 return FALSE;
1121 }
1122 return TRUE;
1123}
1124
1125static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001126fu_plugin_runner_device_generic (FuPlugin *self, FuDevice *device,
Richard Hughes4b303802019-10-04 13:22:51 +01001127 const gchar *symbol_name,
1128 FuPluginDeviceFunc device_func,
1129 GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001130{
Richard Hughes12724852018-09-04 13:53:44 +01001131 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001132 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001133 g_autoptr(GError) error_local = NULL;
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001134
1135 /* not enabled */
1136 if (!priv->enabled)
1137 return TRUE;
1138
Richard Hughesd3d96cc2017-11-14 11:34:33 +00001139 /* no object loaded */
1140 if (priv->module == NULL)
1141 return TRUE;
1142
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001143 /* optional */
1144 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
Richard Hughes4b303802019-10-04 13:22:51 +01001145 if (func == NULL) {
1146 if (device_func != NULL) {
1147 g_debug ("running superclassed %s() on %s",
1148 symbol_name + 10, priv->name);
1149 return device_func (self, device, error);
1150 }
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001151 return TRUE;
Richard Hughes4b303802019-10-04 13:22:51 +01001152 }
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001153 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001154 if (!func (self, device, &error_local)) {
1155 if (error_local == NULL) {
1156 g_critical ("unset error in plugin %s for %s()",
1157 priv->name, symbol_name + 10);
1158 g_set_error_literal (&error_local,
1159 FWUPD_ERROR,
1160 FWUPD_ERROR_INTERNAL,
1161 "unspecified error");
1162 }
1163 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1164 "failed to %s using %s: ",
1165 symbol_name + 10, priv->name);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001166 return FALSE;
1167 }
1168 return TRUE;
1169}
1170
Richard Hughesdbd8c762018-06-15 20:31:40 +01001171static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001172fu_plugin_runner_flagged_device_generic (FuPlugin *self, FwupdInstallFlags flags,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001173 FuDevice *device,
1174 const gchar *symbol_name, GError **error)
1175{
Richard Hughes12724852018-09-04 13:53:44 +01001176 FuPluginPrivate *priv = GET_PRIVATE (self);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001177 FuPluginFlaggedDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001178 g_autoptr(GError) error_local = NULL;
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001179
1180 /* not enabled */
1181 if (!priv->enabled)
1182 return TRUE;
1183
1184 /* no object loaded */
1185 if (priv->module == NULL)
1186 return TRUE;
1187
1188 /* optional */
1189 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1190 if (func == NULL)
1191 return TRUE;
1192 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001193 if (!func (self, flags, device, &error_local)) {
1194 if (error_local == NULL) {
1195 g_critical ("unset error in plugin %s for %s()",
1196 priv->name, symbol_name + 10);
1197 g_set_error_literal (&error_local,
1198 FWUPD_ERROR,
1199 FWUPD_ERROR_INTERNAL,
1200 "unspecified error");
1201 }
1202 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1203 "failed to %s using %s: ",
1204 symbol_name + 10, priv->name);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001205 return FALSE;
1206 }
1207 return TRUE;
1208
1209}
1210
1211static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001212fu_plugin_runner_device_array_generic (FuPlugin *self, GPtrArray *devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001213 const gchar *symbol_name, GError **error)
1214{
Richard Hughes12724852018-09-04 13:53:44 +01001215 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesdbd8c762018-06-15 20:31:40 +01001216 FuPluginDeviceArrayFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001217 g_autoptr(GError) error_local = NULL;
Richard Hughesdbd8c762018-06-15 20:31:40 +01001218
1219 /* not enabled */
1220 if (!priv->enabled)
1221 return TRUE;
1222
1223 /* no object loaded */
1224 if (priv->module == NULL)
1225 return TRUE;
1226
1227 /* optional */
1228 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1229 if (func == NULL)
1230 return TRUE;
1231 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001232 if (!func (self, devices, &error_local)) {
1233 if (error_local == NULL) {
1234 g_critical ("unset error in plugin %s for %s()",
1235 priv->name, symbol_name + 10);
1236 g_set_error_literal (&error_local,
1237 FWUPD_ERROR,
1238 FWUPD_ERROR_INTERNAL,
1239 "unspecified error");
1240 }
1241 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1242 "failed to %s using %s: ",
1243 symbol_name + 10, priv->name);
Richard Hughesdbd8c762018-06-15 20:31:40 +01001244 return FALSE;
1245 }
1246 return TRUE;
1247}
1248
Mario Limonciello1a680f32019-11-25 19:44:53 -06001249/**
1250 * fu_plugin_runner_coldplug:
1251 * @self: a #FuPlugin
1252 * @error: a #GError or NULL
1253 *
1254 * Runs the coldplug routine for the plugin
1255 *
1256 * Returns: #TRUE for success, #FALSE for failure
1257 *
1258 * Since: 0.8.0
1259 **/
Richard Hughesd0905142016-03-13 09:46:49 +00001260gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001261fu_plugin_runner_coldplug (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001262{
Richard Hughes12724852018-09-04 13:53:44 +01001263 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001264 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001265 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +00001266
1267 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +00001268 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +00001269 return TRUE;
1270
Richard Hughes639da472018-01-06 22:35:04 +00001271 /* no object loaded */
1272 if (priv->module == NULL)
1273 return TRUE;
1274
Richard Hughesd0905142016-03-13 09:46:49 +00001275 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001276 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
1277 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001278 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001279 g_debug ("performing coldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001280 if (!func (self, &error_local)) {
1281 if (error_local == NULL) {
1282 g_critical ("unset error in plugin %s for coldplug()",
1283 priv->name);
1284 g_set_error_literal (&error_local,
1285 FWUPD_ERROR,
1286 FWUPD_ERROR_INTERNAL,
1287 "unspecified error");
1288 }
1289 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1290 "failed to coldplug using %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001291 return FALSE;
1292 }
1293 return TRUE;
1294}
1295
Mario Limonciello1a680f32019-11-25 19:44:53 -06001296/**
1297 * fu_plugin_runner_recoldplug:
1298 * @self: a #FuPlugin
1299 * @error: a #GError or NULL
1300 *
1301 * Runs the recoldplug routine for the plugin
1302 *
1303 * Returns: #TRUE for success, #FALSE for failure
1304 *
1305 * Since: 1.0.4
1306 **/
Richard Hughes7b8b2022016-12-12 16:15:03 +00001307gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001308fu_plugin_runner_recoldplug (FuPlugin *self, GError **error)
Richard Hughes2de8f132018-01-17 09:12:02 +00001309{
Richard Hughes12724852018-09-04 13:53:44 +01001310 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes2de8f132018-01-17 09:12:02 +00001311 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001312 g_autoptr(GError) error_local = NULL;
Richard Hughes2de8f132018-01-17 09:12:02 +00001313
1314 /* not enabled */
1315 if (!priv->enabled)
1316 return TRUE;
1317
1318 /* no object loaded */
1319 if (priv->module == NULL)
1320 return TRUE;
1321
1322 /* optional */
1323 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1324 if (func == NULL)
1325 return TRUE;
1326 g_debug ("performing recoldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001327 if (!func (self, &error_local)) {
1328 if (error_local == NULL) {
1329 g_critical ("unset error in plugin %s for recoldplug()",
1330 priv->name);
1331 g_set_error_literal (&error_local,
1332 FWUPD_ERROR,
1333 FWUPD_ERROR_INTERNAL,
1334 "unspecified error");
1335 }
1336 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1337 "failed to recoldplug using %s: ",
1338 priv->name);
Richard Hughes2de8f132018-01-17 09:12:02 +00001339 return FALSE;
1340 }
1341 return TRUE;
1342}
1343
Mario Limonciello1a680f32019-11-25 19:44:53 -06001344/**
1345 * fu_plugin_runner_coldplug_prepare:
1346 * @self: a #FuPlugin
1347 * @error: a #GError or NULL
1348 *
1349 * Runs the coldplug_prepare routine for the plugin
1350 *
1351 * Returns: #TRUE for success, #FALSE for failure
1352 *
1353 * Since: 0.8.0
1354 **/
Richard Hughes2de8f132018-01-17 09:12:02 +00001355gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001356fu_plugin_runner_coldplug_prepare (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001357{
Richard Hughes12724852018-09-04 13:53:44 +01001358 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001359 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001360 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001361
1362 /* not enabled */
1363 if (!priv->enabled)
1364 return TRUE;
1365
Richard Hughes639da472018-01-06 22:35:04 +00001366 /* no object loaded */
1367 if (priv->module == NULL)
1368 return TRUE;
1369
Richard Hughes46487c92017-01-07 21:26:34 +00001370 /* optional */
1371 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1372 if (func == NULL)
1373 return TRUE;
1374 g_debug ("performing coldplug_prepare() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001375 if (!func (self, &error_local)) {
1376 if (error_local == NULL) {
1377 g_critical ("unset error in plugin %s for coldplug_prepare()",
1378 priv->name);
1379 g_set_error_literal (&error_local,
1380 FWUPD_ERROR,
1381 FWUPD_ERROR_INTERNAL,
1382 "unspecified error");
1383 }
1384 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1385 "failed to coldplug_prepare using %s: ",
1386 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001387 return FALSE;
1388 }
1389 return TRUE;
1390}
1391
Mario Limonciello1a680f32019-11-25 19:44:53 -06001392/**
1393 * fu_plugin_runner_coldplug_cleanup:
1394 * @self: a #FuPlugin
1395 * @error: a #GError or NULL
1396 *
1397 * Runs the coldplug_cleanup routine for the plugin
1398 *
1399 * Returns: #TRUE for success, #FALSE for failure
1400 *
1401 * Since: 0.8.0
1402 **/
Richard Hughes46487c92017-01-07 21:26:34 +00001403gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001404fu_plugin_runner_coldplug_cleanup (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001405{
Richard Hughes12724852018-09-04 13:53:44 +01001406 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001407 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001408 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001409
1410 /* not enabled */
1411 if (!priv->enabled)
1412 return TRUE;
1413
Richard Hughes639da472018-01-06 22:35:04 +00001414 /* no object loaded */
1415 if (priv->module == NULL)
1416 return TRUE;
1417
Richard Hughes46487c92017-01-07 21:26:34 +00001418 /* optional */
1419 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1420 if (func == NULL)
1421 return TRUE;
1422 g_debug ("performing coldplug_cleanup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001423 if (!func (self, &error_local)) {
1424 if (error_local == NULL) {
1425 g_critical ("unset error in plugin %s for coldplug_cleanup()",
1426 priv->name);
1427 g_set_error_literal (&error_local,
1428 FWUPD_ERROR,
1429 FWUPD_ERROR_INTERNAL,
1430 "unspecified error");
1431 }
1432 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1433 "failed to coldplug_cleanup using %s: ",
1434 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001435 return FALSE;
1436 }
1437 return TRUE;
1438}
1439
Mario Limonciello1a680f32019-11-25 19:44:53 -06001440/**
1441 * fu_plugin_runner_composite_prepare:
1442 * @self: a #FuPlugin
Richard Hughesa0d81c72019-11-27 11:41:54 +00001443 * @devices: (element-type FuDevice): a #GPtrArray of devices
Mario Limonciello1a680f32019-11-25 19:44:53 -06001444 * @error: a #GError or NULL
1445 *
1446 * Runs the composite_prepare routine for the plugin
1447 *
1448 * Returns: #TRUE for success, #FALSE for failure
1449 *
1450 * Since: 1.0.9
1451 **/
Richard Hughes46487c92017-01-07 21:26:34 +00001452gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001453fu_plugin_runner_composite_prepare (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001454{
Richard Hughes12724852018-09-04 13:53:44 +01001455 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001456 "fu_plugin_composite_prepare",
1457 error);
1458}
1459
Mario Limonciello1a680f32019-11-25 19:44:53 -06001460/**
1461 * fu_plugin_runner_composite_cleanup:
1462 * @self: a #FuPlugin
Richard Hughesa0d81c72019-11-27 11:41:54 +00001463 * @devices: (element-type FuDevice): a #GPtrArray of devices
Mario Limonciello1a680f32019-11-25 19:44:53 -06001464 * @error: a #GError or NULL
1465 *
1466 * Runs the composite_cleanup routine for the plugin
1467 *
1468 * Returns: #TRUE for success, #FALSE for failure
1469 *
1470 * Since: 1.0.9
1471 **/
Richard Hughesdbd8c762018-06-15 20:31:40 +01001472gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001473fu_plugin_runner_composite_cleanup (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001474{
Richard Hughes12724852018-09-04 13:53:44 +01001475 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001476 "fu_plugin_composite_cleanup",
1477 error);
1478}
1479
Mario Limonciello1a680f32019-11-25 19:44:53 -06001480/**
1481 * fu_plugin_runner_update_prepare:
1482 * @self: a #FuPlugin
1483 * @error: a #GError or NULL
1484 *
1485 * Runs the update_prepare routine for the plugin
1486 *
1487 * Returns: #TRUE for success, #FALSE for failure
1488 *
1489 * Since: 1.1.2
1490 **/
Richard Hughesdbd8c762018-06-15 20:31:40 +01001491gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001492fu_plugin_runner_update_prepare (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001493 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001494{
Richard Hughes12724852018-09-04 13:53:44 +01001495 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001496 "fu_plugin_update_prepare",
1497 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001498}
1499
Mario Limonciello1a680f32019-11-25 19:44:53 -06001500/**
1501 * fu_plugin_runner_update_cleanup:
1502 * @self: a #FuPlugin
1503 * @error: a #GError or NULL
1504 *
1505 * Runs the update_cleanup routine for the plugin
1506 *
1507 * Returns: #TRUE for success, #FALSE for failure
1508 *
1509 * Since: 1.1.2
1510 **/
Richard Hughes7b8b2022016-12-12 16:15:03 +00001511gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001512fu_plugin_runner_update_cleanup (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001513 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001514{
Richard Hughes12724852018-09-04 13:53:44 +01001515 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001516 "fu_plugin_update_cleanup",
1517 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001518}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001519
Mario Limonciello1a680f32019-11-25 19:44:53 -06001520/**
1521 * fu_plugin_runner_update_attach:
1522 * @self: a #FuPlugin
1523 * @device: a #FuDevice
1524 * @error: a #GError or NULL
1525 *
1526 * Runs the update_attach routine for the plugin
1527 *
1528 * Returns: #TRUE for success, #FALSE for failure
1529 *
1530 * Since: 1.1.2
1531 **/
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001532gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001533fu_plugin_runner_update_attach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001534{
Richard Hughes12724852018-09-04 13:53:44 +01001535 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001536 "fu_plugin_update_attach",
1537 fu_plugin_device_attach,
1538 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001539}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001540
Mario Limonciello1a680f32019-11-25 19:44:53 -06001541/**
1542 * fu_plugin_runner_update_detach:
1543 * @self: a #FuPlugin
1544 * @device: A #FuDevice
1545 * @error: a #GError or NULL
1546 *
1547 * Runs the update_detach routine for the plugin
1548 *
1549 * Returns: #TRUE for success, #FALSE for failure
1550 *
1551 * Since: 1.1.2
1552 **/
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001553gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001554fu_plugin_runner_update_detach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001555{
Richard Hughes12724852018-09-04 13:53:44 +01001556 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001557 "fu_plugin_update_detach",
1558 fu_plugin_device_detach,
1559 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001560}
1561
Mario Limonciello1a680f32019-11-25 19:44:53 -06001562/**
1563 * fu_plugin_runner_update_reload:
1564 * @self: a #FuPlugin
1565 * @error: a #GError or NULL
1566 *
1567 * Runs reload routine for a device
1568 *
1569 * Returns: #TRUE for success, #FALSE for failure
1570 *
1571 * Since: 1.1.2
1572 **/
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001573gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001574fu_plugin_runner_update_reload (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001575{
Richard Hughes42f33df2019-10-05 20:52:33 +01001576 FuPluginPrivate *priv = GET_PRIVATE (self);
1577 g_autoptr(FuDeviceLocker) locker = NULL;
1578
1579 /* not enabled */
1580 if (!priv->enabled)
1581 return TRUE;
1582
1583 /* no object loaded */
1584 locker = fu_device_locker_new (device, error);
1585 if (locker == NULL)
1586 return FALSE;
1587 return fu_device_reload (device, error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001588}
1589
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001590/**
1591 * fu_plugin_add_udev_subsystem:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001592 * @self: a #FuPlugin
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001593 * @subsystem: a subsystem name, e.g. `pciport`
1594 *
1595 * Registers the udev subsystem to be watched by the daemon.
1596 *
1597 * Plugins can use this method only in fu_plugin_init()
Mario Limonciello1a680f32019-11-25 19:44:53 -06001598 *
1599 * Since: 1.1.2
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001600 **/
1601void
Richard Hughes12724852018-09-04 13:53:44 +01001602fu_plugin_add_udev_subsystem (FuPlugin *self, const gchar *subsystem)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001603{
Richard Hughes12724852018-09-04 13:53:44 +01001604 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001605 for (guint i = 0; i < priv->udev_subsystems->len; i++) {
1606 const gchar *subsystem_tmp = g_ptr_array_index (priv->udev_subsystems, i);
1607 if (g_strcmp0 (subsystem_tmp, subsystem) == 0)
1608 return;
1609 }
1610 g_debug ("added udev subsystem watch of %s", subsystem);
1611 g_ptr_array_add (priv->udev_subsystems, g_strdup (subsystem));
1612}
1613
Richard Hughes989acf12019-10-05 20:16:47 +01001614/**
1615 * fu_plugin_set_device_gtype:
1616 * @self: a #FuPlugin
1617 * @device_gtype: a #GType `FU_TYPE_DEVICE`
1618 *
1619 * Sets the device #GType which is used when creating devices.
1620 *
1621 * If this method is used then fu_plugin_usb_device_added() is not called, and
1622 * instead the object is created in the daemon for the plugin.
1623 *
1624 * Plugins can use this method only in fu_plugin_init()
1625 *
1626 * Since: 1.3.3
1627 **/
1628void
1629fu_plugin_set_device_gtype (FuPlugin *self, GType device_gtype)
1630{
1631 FuPluginPrivate *priv = GET_PRIVATE (self);
1632 priv->device_gtype = device_gtype;
1633}
1634
Mario Limonciello1a680f32019-11-25 19:44:53 -06001635/**
1636 * fu_plugin_add_firmware_gtype:
1637 * @self: a #FuPlugin
1638 * @id: A string describing the type
1639 * @gtype: a #GType `FU_TYPE_DEVICE`
1640 *
1641 * Adds a firmware #GType which is used when creating devices.
1642 * *
1643 * Plugins can use this method only in fu_plugin_init()
1644 *
1645 * Since: 1.3.3
1646 **/
Richard Hughes95c98a92019-10-22 16:03:15 +01001647void
1648fu_plugin_add_firmware_gtype (FuPlugin *self, const gchar *id, GType gtype)
1649{
1650 g_signal_emit (self, signals[SIGNAL_ADD_FIRMWARE_GTYPE], 0, id, gtype);
1651}
1652
Richard Hughes989acf12019-10-05 20:16:47 +01001653static gboolean
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001654fu_plugin_check_supported_device (FuPlugin *self, FuDevice *device)
1655{
1656 GPtrArray *instance_ids = fu_device_get_instance_ids (device);
1657 for (guint i = 0; i < instance_ids->len; i++) {
1658 const gchar *instance_id = g_ptr_array_index (instance_ids, i);
1659 g_autofree gchar *guid = fwupd_guid_hash_string (instance_id);
1660 if (fu_plugin_check_supported (self, guid))
1661 return TRUE;
1662 }
1663 return FALSE;
1664}
1665
1666static gboolean
Richard Hughes989acf12019-10-05 20:16:47 +01001667fu_plugin_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
1668{
1669 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001670 GType device_gtype = fu_device_get_specialized_gtype (FU_DEVICE (device));
Richard Hughes989acf12019-10-05 20:16:47 +01001671 g_autoptr(FuDevice) dev = NULL;
1672 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001673
1674 /* fall back to plugin default */
1675 if (device_gtype == G_TYPE_INVALID)
1676 device_gtype = priv->device_gtype;
1677
1678 /* create new device and incorporate existing properties */
1679 dev = g_object_new (device_gtype, NULL);
1680 fu_device_incorporate (dev, FU_DEVICE (device));
Richard Hughes0f66a022020-02-19 18:54:38 +00001681 if (!fu_plugin_runner_device_created (self, dev, error))
1682 return FALSE;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001683
1684 /* there are a lot of different devices that match, but not all respond
1685 * well to opening -- so limit some ones with issued updates */
1686 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_ONLY_SUPPORTED)) {
1687 if (!fu_device_probe (dev, error))
1688 return FALSE;
1689 fu_device_convert_instance_ids (dev);
1690 if (!fu_plugin_check_supported_device (self, dev)) {
1691 g_autofree gchar *guids = fu_device_get_guids_as_str (dev);
1692 g_debug ("%s has no updates, so ignoring device", guids);
1693 return TRUE;
1694 }
1695 }
1696
1697 /* open and add */
1698 locker = fu_device_locker_new (dev, error);
1699 if (locker == NULL)
1700 return FALSE;
1701 fu_plugin_device_add (self, dev);
1702 return TRUE;
1703}
1704
1705static gboolean
1706fu_plugin_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
1707{
1708 FuPluginPrivate *priv = GET_PRIVATE (self);
1709 GType device_gtype = fu_device_get_specialized_gtype (FU_DEVICE (device));
1710 g_autoptr(FuDevice) dev = NULL;
1711 g_autoptr(FuDeviceLocker) locker = NULL;
1712
1713 /* fall back to plugin default */
1714 if (device_gtype == G_TYPE_INVALID)
1715 device_gtype = priv->device_gtype;
1716
1717 /* create new device and incorporate existing properties */
1718 dev = g_object_new (device_gtype, NULL);
Richard Hughes989acf12019-10-05 20:16:47 +01001719 fu_device_incorporate (FU_DEVICE (dev), FU_DEVICE (device));
Richard Hughes0f66a022020-02-19 18:54:38 +00001720 if (!fu_plugin_runner_device_created (self, dev, error))
1721 return FALSE;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001722
1723 /* there are a lot of different devices that match, but not all respond
1724 * well to opening -- so limit some ones with issued updates */
1725 if (fu_device_has_flag (dev, FWUPD_DEVICE_FLAG_ONLY_SUPPORTED)) {
1726 if (!fu_device_probe (dev, error))
1727 return FALSE;
1728 fu_device_convert_instance_ids (dev);
1729 if (!fu_plugin_check_supported_device (self, dev)) {
1730 g_autofree gchar *guids = fu_device_get_guids_as_str (dev);
1731 g_debug ("%s has no updates, so ignoring device", guids);
1732 return TRUE;
1733 }
1734 }
1735
1736 /* open and add */
Richard Hughes989acf12019-10-05 20:16:47 +01001737 locker = fu_device_locker_new (dev, error);
1738 if (locker == NULL)
1739 return FALSE;
1740 fu_plugin_device_add (self, FU_DEVICE (dev));
1741 return TRUE;
1742}
1743
Mario Limonciello1a680f32019-11-25 19:44:53 -06001744/**
1745 * fu_plugin_runner_usb_device_added:
1746 * @self: a #FuPlugin
1747 * @device: a #FuUsbDevice
1748 * @error: a #GError or NULL
1749 *
1750 * Call the usb_device_added routine for the plugin
1751 *
1752 * Returns: #TRUE for success, #FALSE for failure
1753 *
1754 * Since: 1.0.2
1755 **/
Richard Hughes104f6512017-11-24 11:44:57 +00001756gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001757fu_plugin_runner_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
Richard Hughes104f6512017-11-24 11:44:57 +00001758{
Richard Hughes12724852018-09-04 13:53:44 +01001759 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes104f6512017-11-24 11:44:57 +00001760 FuPluginUsbDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001761 g_autoptr(GError) error_local = NULL;
Richard Hughes104f6512017-11-24 11:44:57 +00001762
1763 /* not enabled */
1764 if (!priv->enabled)
1765 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001766
1767 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001768 if (priv->module == NULL)
1769 return TRUE;
1770
1771 /* optional */
1772 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
Richard Hughes989acf12019-10-05 20:16:47 +01001773 if (func == NULL) {
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001774 if (priv->device_gtype != G_TYPE_INVALID ||
1775 fu_device_get_specialized_gtype (FU_DEVICE (device)) != G_TYPE_INVALID) {
Mario Limonciello0e500322019-10-17 18:41:04 -05001776 if (!fu_plugin_usb_device_added (self, device, error))
Mario Limoncielloa9be5362019-10-12 13:17:45 -05001777 return FALSE;
Richard Hughes989acf12019-10-05 20:16:47 +01001778 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001779 return TRUE;
Richard Hughes989acf12019-10-05 20:16:47 +01001780 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001781 g_debug ("performing usb_device_added() on %s", priv->name);
1782 if (!func (self, device, &error_local)) {
1783 if (error_local == NULL) {
1784 g_critical ("unset error in plugin %s for usb_device_added()",
1785 priv->name);
1786 g_set_error_literal (&error_local,
1787 FWUPD_ERROR,
1788 FWUPD_ERROR_INTERNAL,
1789 "unspecified error");
1790 }
1791 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1792 "failed to add device using on %s: ",
1793 priv->name);
1794 return FALSE;
Richard Hughes104f6512017-11-24 11:44:57 +00001795 }
1796 return TRUE;
1797}
1798
Mario Limonciello1a680f32019-11-25 19:44:53 -06001799/**
1800 * fu_plugin_runner_udev_device_added:
1801 * @self: a #FuPlugin
1802 * @device: a #FuUdevDevice
1803 * @error: a #GError or NULL
1804 *
1805 * Call the udev_device_added routine for the plugin
1806 *
1807 * Returns: #TRUE for success, #FALSE for failure
1808 *
1809 * Since: 1.0.2
1810 **/
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001811gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001812fu_plugin_runner_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001813{
Richard Hughes12724852018-09-04 13:53:44 +01001814 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001815 FuPluginUdevDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001816 g_autoptr(GError) error_local = NULL;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001817
1818 /* not enabled */
1819 if (!priv->enabled)
1820 return TRUE;
1821
1822 /* no object loaded */
1823 if (priv->module == NULL)
1824 return TRUE;
1825
1826 /* optional */
1827 g_module_symbol (priv->module, "fu_plugin_udev_device_added", (gpointer *) &func);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001828 if (func == NULL) {
1829 if (priv->device_gtype != G_TYPE_INVALID ||
1830 fu_device_get_specialized_gtype (FU_DEVICE (device)) != G_TYPE_INVALID) {
Mario Limonciello0e500322019-10-17 18:41:04 -05001831 if (!fu_plugin_udev_device_added (self, device, error))
Mario Limoncielloa9be5362019-10-12 13:17:45 -05001832 return FALSE;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001833 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001834 return TRUE;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001835 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001836 g_debug ("performing udev_device_added() on %s", priv->name);
1837 if (!func (self, device, &error_local)) {
1838 if (error_local == NULL) {
1839 g_critical ("unset error in plugin %s for udev_device_added()",
1840 priv->name);
1841 g_set_error_literal (&error_local,
1842 FWUPD_ERROR,
1843 FWUPD_ERROR_INTERNAL,
1844 "unspecified error");
1845 }
1846 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1847 "failed to add device using on %s: ",
1848 priv->name);
1849 return FALSE;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001850 }
1851 return TRUE;
1852}
1853
Mario Limonciello1a680f32019-11-25 19:44:53 -06001854/**
1855 * fu_plugin_runner_udev_device_changed:
1856 * @self: a #FuPlugin
1857 * @device: a #FuUdevDevice
1858 * @error: a #GError or NULL
1859 *
1860 * Call the udev_device_changed routine for the plugin
1861 *
1862 * Returns: #TRUE for success, #FALSE for failure
1863 *
1864 * Since: 1.0.2
1865 **/
Richard Hughes5e952ce2019-08-26 11:09:46 +01001866gboolean
1867fu_plugin_runner_udev_device_changed (FuPlugin *self, FuUdevDevice *device, GError **error)
1868{
1869 FuPluginPrivate *priv = GET_PRIVATE (self);
1870 FuPluginUdevDeviceAddedFunc func = NULL;
1871 g_autoptr(GError) error_local = NULL;
1872
1873 /* not enabled */
1874 if (!priv->enabled)
1875 return TRUE;
1876
1877 /* no object loaded */
1878 if (priv->module == NULL)
1879 return TRUE;
1880
1881 /* optional */
1882 g_module_symbol (priv->module, "fu_plugin_udev_device_changed", (gpointer *) &func);
1883 if (func == NULL)
1884 return TRUE;
1885 g_debug ("performing udev_device_changed() on %s", priv->name);
1886 if (!func (self, device, &error_local)) {
1887 if (error_local == NULL) {
1888 g_critical ("unset error in plugin %s for udev_device_changed()",
1889 priv->name);
1890 g_set_error_literal (&error_local,
1891 FWUPD_ERROR,
1892 FWUPD_ERROR_INTERNAL,
1893 "unspecified error");
1894 }
1895 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1896 "failed to change device on %s: ",
1897 priv->name);
1898 return FALSE;
1899 }
1900 return TRUE;
1901}
1902
Mario Limonciello1a680f32019-11-25 19:44:53 -06001903/**
1904 * fu_plugin_runner_device_removed:
1905 * @self: a #FuPlugin
1906 * @device: a #FuDevice
1907 *
1908 * Call the device_removed routine for the plugin
1909 *
1910 * Since: 1.1.2
1911 **/
Richard Hughese1fd34d2017-08-24 14:19:51 +01001912void
Richard Hughes12724852018-09-04 13:53:44 +01001913fu_plugin_runner_device_removed (FuPlugin *self, FuDevice *device)
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001914{
1915 g_autoptr(GError) error_local= NULL;
1916
Richard Hughes12724852018-09-04 13:53:44 +01001917 if (!fu_plugin_runner_device_generic (self, device,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001918 "fu_plugin_device_removed",
Richard Hughes4b303802019-10-04 13:22:51 +01001919 NULL,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001920 &error_local))
1921 g_warning ("%s", error_local->message);
1922}
1923
Mario Limonciello1a680f32019-11-25 19:44:53 -06001924/**
1925 * fu_plugin_runner_device_register:
1926 * @self: a #FuPlugin
1927 * @device: a #FuDevice
1928 *
1929 * Call the device_registered routine for the plugin
1930 *
1931 * Since: 0.9.7
1932 **/
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001933void
Richard Hughes12724852018-09-04 13:53:44 +01001934fu_plugin_runner_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +01001935{
Richard Hughes12724852018-09-04 13:53:44 +01001936 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001937 FuPluginDeviceRegisterFunc func = NULL;
1938
1939 /* not enabled */
1940 if (!priv->enabled)
1941 return;
Richard Hughes34834102017-11-21 21:55:00 +00001942 if (priv->module == NULL)
1943 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001944
Mario Limonciello4910b242018-06-22 15:04:21 -05001945 /* don't notify plugins on their own devices */
Richard Hughes12724852018-09-04 13:53:44 +01001946 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (self)) == 0)
Mario Limonciello4910b242018-06-22 15:04:21 -05001947 return;
1948
Richard Hughese1fd34d2017-08-24 14:19:51 +01001949 /* optional */
1950 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1951 if (func != NULL) {
Richard Hughes1bf7ff92018-08-24 20:21:35 +01001952 g_debug ("performing fu_plugin_device_registered() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001953 func (self, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001954 }
1955}
1956
Mario Limonciello1a680f32019-11-25 19:44:53 -06001957/**
Richard Hughes0f66a022020-02-19 18:54:38 +00001958 * fu_plugin_runner_device_created:
1959 * @self: a #FuPlugin
1960 * @device: a #FuDevice
1961 * @error: a #GError or NULL
1962 *
1963 * Call the device_created routine for the plugin
1964 *
1965 * Returns: #TRUE for success, #FALSE for failure
1966 *
Mario Limonciello96117d12020-02-28 10:17:56 -06001967 * Since: 1.4.0
Richard Hughes0f66a022020-02-19 18:54:38 +00001968 **/
1969gboolean
1970fu_plugin_runner_device_created (FuPlugin *self, FuDevice *device, GError **error)
1971{
1972 FuPluginPrivate *priv = GET_PRIVATE (self);
1973 FuPluginDeviceFunc func = NULL;
1974
1975 /* not enabled */
1976 if (!priv->enabled)
1977 return TRUE;
1978 if (priv->module == NULL)
1979 return TRUE;
1980
1981 /* optional */
1982 g_module_symbol (priv->module, "fu_plugin_device_created", (gpointer *) &func);
1983 if (func == NULL)
1984 return TRUE;
1985 g_debug ("performing fu_plugin_device_created() on %s", priv->name);
1986 return func (self, device, error);
1987}
1988
1989/**
Mario Limonciello1a680f32019-11-25 19:44:53 -06001990 * fu_plugin_runner_verify:
1991 * @self: a #FuPlugin
1992 * @device: a #FuDevice
1993 * @flags: #FuPluginVerifyFlags
1994 * @error: A #GError or NULL
1995 *
1996 * Call into the plugin's verify routine
1997 *
1998 * Returns: #TRUE for success, #FALSE for failure
1999 *
2000 * Since: 0.8.0
2001 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002002gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002003fu_plugin_runner_verify (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00002004 FuDevice *device,
2005 FuPluginVerifyFlags flags,
2006 GError **error)
2007{
Richard Hughes12724852018-09-04 13:53:44 +01002008 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00002009 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01002010 GPtrArray *checksums;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002011 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00002012
2013 /* not enabled */
2014 if (!priv->enabled)
2015 return TRUE;
2016
Richard Hughes639da472018-01-06 22:35:04 +00002017 /* no object loaded */
2018 if (priv->module == NULL)
2019 return TRUE;
2020
Richard Hughescff38bc2016-12-12 12:03:37 +00002021 /* optional */
2022 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
Richard Hughes7f677212019-10-05 16:19:40 +01002023 if (func == NULL) {
Richard Hughes7f677212019-10-05 16:19:40 +01002024 return fu_plugin_device_read_firmware (self, device, error);
2025 }
Richard Hughes1812fc72018-12-14 11:37:54 +00002026
2027 /* clear any existing verification checksums */
2028 checksums = fu_device_get_checksums (device);
2029 g_ptr_array_set_size (checksums, 0);
2030
Richard Hughesc9223be2019-03-18 08:46:42 +00002031 /* run additional detach */
2032 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes42f33df2019-10-05 20:52:33 +01002033 "fu_plugin_update_detach",
Richard Hughes4b303802019-10-04 13:22:51 +01002034 fu_plugin_device_detach,
Richard Hughesc9223be2019-03-18 08:46:42 +00002035 error))
2036 return FALSE;
2037
Richard Hughes1812fc72018-12-14 11:37:54 +00002038 /* run vfunc */
Richard Hughescff38bc2016-12-12 12:03:37 +00002039 g_debug ("performing verify() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002040 if (!func (self, device, flags, &error_local)) {
Richard Hughesc9223be2019-03-18 08:46:42 +00002041 g_autoptr(GError) error_attach = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002042 if (error_local == NULL) {
2043 g_critical ("unset error in plugin %s for verify()",
2044 priv->name);
2045 g_set_error_literal (&error_local,
2046 FWUPD_ERROR,
2047 FWUPD_ERROR_INTERNAL,
2048 "unspecified error");
2049 }
2050 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
2051 "failed to verify using %s: ",
2052 priv->name);
Richard Hughesc9223be2019-03-18 08:46:42 +00002053 /* make the device "work" again, but don't prefix the error */
2054 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes42f33df2019-10-05 20:52:33 +01002055 "fu_plugin_update_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01002056 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00002057 &error_attach)) {
2058 g_warning ("failed to attach whilst aborting verify(): %s",
2059 error_attach->message);
2060 }
Richard Hughesd0905142016-03-13 09:46:49 +00002061 return FALSE;
2062 }
Richard Hughesc9223be2019-03-18 08:46:42 +00002063
2064 /* run optional attach */
2065 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes42f33df2019-10-05 20:52:33 +01002066 "fu_plugin_update_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01002067 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00002068 error))
2069 return FALSE;
2070
2071 /* success */
Richard Hughesd0905142016-03-13 09:46:49 +00002072 return TRUE;
2073}
2074
Mario Limonciello1a680f32019-11-25 19:44:53 -06002075/**
2076 * fu_plugin_runner_activate:
2077 * @self: a #FuPlugin
2078 * @device: a #FuDevice
2079 * @error: A #GError or NULL
2080 *
2081 * Call into the plugin's activate routine
2082 *
2083 * Returns: #TRUE for success, #FALSE for failure
2084 *
2085 * Since: 1.2.6
2086 **/
Richard Hughesd0905142016-03-13 09:46:49 +00002087gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002088fu_plugin_runner_activate (FuPlugin *self, FuDevice *device, GError **error)
2089{
2090 guint64 flags;
2091
2092 /* final check */
2093 flags = fu_device_get_flags (device);
2094 if ((flags & FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION) == 0) {
2095 g_set_error (error,
2096 FWUPD_ERROR,
2097 FWUPD_ERROR_NOT_SUPPORTED,
2098 "Device %s does not need activation",
2099 fu_device_get_id (device));
2100 return FALSE;
2101 }
2102
2103 /* run vfunc */
2104 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01002105 "fu_plugin_activate",
2106 fu_plugin_device_activate,
2107 error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002108 return FALSE;
2109
2110 /* update with correct flags */
2111 fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION);
2112 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
2113 return TRUE;
2114}
2115
Mario Limonciello1a680f32019-11-25 19:44:53 -06002116/**
2117 * fu_plugin_runner_unlock:
2118 * @self: a #FuPlugin
2119 * @device: a #FuDevice
2120 * @error: A #GError or NULL
2121 *
2122 * Call into the plugin's unlock routine
2123 *
2124 * Returns: #TRUE for success, #FALSE for failure
2125 *
2126 * Since: 0.8.0
2127 **/
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002128gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002129fu_plugin_runner_unlock (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00002130{
Richard Hughescff38bc2016-12-12 12:03:37 +00002131 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00002132
2133 /* final check */
2134 flags = fu_device_get_flags (device);
2135 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
2136 g_set_error (error,
2137 FWUPD_ERROR,
2138 FWUPD_ERROR_NOT_SUPPORTED,
2139 "Device %s is not locked",
2140 fu_device_get_id (device));
2141 return FALSE;
2142 }
2143
Richard Hughes9c4b5312017-11-14 11:34:53 +00002144 /* run vfunc */
Richard Hughes12724852018-09-04 13:53:44 +01002145 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01002146 "fu_plugin_unlock",
2147 NULL,
2148 error))
Richard Hughes9c4b5312017-11-14 11:34:53 +00002149 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00002150
2151 /* update with correct flags */
2152 flags = fu_device_get_flags (device);
2153 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
2154 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
2155 return TRUE;
2156}
2157
Mario Limonciello1a680f32019-11-25 19:44:53 -06002158/**
2159 * fu_plugin_runner_update:
2160 * @self: a #FuPlugin
2161 * @device: a #FuDevice
2162 * @blob_fw: A #GBytes
2163 * @flags: A #FwupdInstallFlags
2164 * @error: A #GError or NULL
2165 *
2166 * Call into the plugin's update routine
2167 *
2168 * Returns: #TRUE for success, #FALSE for failure
2169 *
2170 * Since: 0.8.0
2171 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002172gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002173fu_plugin_runner_update (FuPlugin *self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01002174 FuDevice *device,
Richard Hughesa785a1c2017-08-25 16:00:58 +01002175 GBytes *blob_fw,
2176 FwupdInstallFlags flags,
2177 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00002178{
Richard Hughes12724852018-09-04 13:53:44 +01002179 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesa785a1c2017-08-25 16:00:58 +01002180 FuPluginUpdateFunc update_func;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002181 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00002182
2183 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00002184 if (!priv->enabled) {
2185 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00002186 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00002187 }
Richard Hughesd0905142016-03-13 09:46:49 +00002188
Richard Hughes639da472018-01-06 22:35:04 +00002189 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00002190 if (priv->module == NULL) {
2191 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00002192 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00002193 }
Richard Hughes639da472018-01-06 22:35:04 +00002194
Richard Hughesd0905142016-03-13 09:46:49 +00002195 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01002196 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
2197 if (update_func == NULL) {
Richard Hughes4b303802019-10-04 13:22:51 +01002198 g_debug ("running superclassed write_firmware() on %s", priv->name);
2199 return fu_plugin_device_write_firmware (self, device, blob_fw, flags, error);
Richard Hughesa785a1c2017-08-25 16:00:58 +01002200 }
Richard Hughesd0905142016-03-13 09:46:49 +00002201
Richard Hughescff38bc2016-12-12 12:03:37 +00002202 /* online */
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002203 if (!update_func (self, device, blob_fw, flags, &error_local)) {
2204 if (error_local == NULL) {
2205 g_critical ("unset error in plugin %s for update()",
2206 priv->name);
2207 g_set_error_literal (&error_local,
Richard Hughes3c8ada32018-10-12 10:08:58 +01002208 FWUPD_ERROR,
2209 FWUPD_ERROR_INTERNAL,
2210 "unspecified error");
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002211 return FALSE;
Richard Hughes3c8ada32018-10-12 10:08:58 +01002212 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002213 fu_device_set_update_error (device, error_local->message);
2214 g_propagate_error (error, g_steal_pointer (&error_local));
Richard Hughescff38bc2016-12-12 12:03:37 +00002215 return FALSE;
2216 }
2217
Richard Hughesf556d372017-06-15 19:49:18 +01002218 /* no longer valid */
Richard Hughesf8039642019-01-16 12:22:22 +00002219 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT) &&
2220 !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) {
Richard Hughes08435162018-12-12 10:34:16 +00002221 GPtrArray *checksums = fu_device_get_checksums (device);
2222 g_ptr_array_set_size (checksums, 0);
2223 }
Richard Hughesf556d372017-06-15 19:49:18 +01002224
Richard Hughes019a1bc2019-11-26 10:19:33 +00002225 /* success */
Richard Hughesd0905142016-03-13 09:46:49 +00002226 return TRUE;
2227}
Richard Hughescff38bc2016-12-12 12:03:37 +00002228
Mario Limonciello1a680f32019-11-25 19:44:53 -06002229/**
2230 * fu_plugin_runner_clear_results:
2231 * @self: a #FuPlugin
2232 * @device: a #FuDevice
2233 * @error: A #GError or NULL
2234 *
2235 * Call into the plugin's clear results routine
2236 *
2237 * Returns: #TRUE for success, #FALSE for failure
2238 *
2239 * Since: 0.8.0
2240 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002241gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002242fu_plugin_runner_clear_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00002243{
Richard Hughes12724852018-09-04 13:53:44 +01002244 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00002245 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002246 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00002247
2248 /* not enabled */
2249 if (!priv->enabled)
2250 return TRUE;
2251
Richard Hughes639da472018-01-06 22:35:04 +00002252 /* no object loaded */
2253 if (priv->module == NULL)
2254 return TRUE;
2255
Richard Hughes65e44ca2018-01-30 17:26:30 +00002256 /* optional */
Richard Hughescd644902019-11-01 12:35:17 +00002257 g_module_symbol (priv->module, "fu_plugin_clear_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00002258 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00002259 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00002260 g_debug ("performing clear_result() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002261 if (!func (self, device, &error_local)) {
2262 if (error_local == NULL) {
2263 g_critical ("unset error in plugin %s for clear_result()",
2264 priv->name);
2265 g_set_error_literal (&error_local,
2266 FWUPD_ERROR,
2267 FWUPD_ERROR_INTERNAL,
2268 "unspecified error");
2269 }
2270 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
2271 "failed to clear_result using %s: ",
2272 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00002273 return FALSE;
2274 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00002275 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00002276}
2277
Mario Limonciello1a680f32019-11-25 19:44:53 -06002278/**
2279 * fu_plugin_runner_get_results:
2280 * @self: a #FuPlugin
2281 * @device: a #FuDevice
2282 * @error: A #GError or NULL
2283 *
2284 * Call into the plugin's get results routine
2285 *
2286 * Returns: #TRUE for success, #FALSE for failure
2287 *
2288 * Since: 0.8.0
2289 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002290gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002291fu_plugin_runner_get_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00002292{
Richard Hughes12724852018-09-04 13:53:44 +01002293 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00002294 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002295 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00002296
2297 /* not enabled */
2298 if (!priv->enabled)
2299 return TRUE;
2300
Richard Hughes639da472018-01-06 22:35:04 +00002301 /* no object loaded */
2302 if (priv->module == NULL)
2303 return TRUE;
2304
Richard Hughes65e44ca2018-01-30 17:26:30 +00002305 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00002306 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00002307 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00002308 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00002309 g_debug ("performing get_results() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002310 if (!func (self, device, &error_local)) {
2311 if (error_local == NULL) {
2312 g_critical ("unset error in plugin %s for get_results()",
2313 priv->name);
2314 g_set_error_literal (&error_local,
2315 FWUPD_ERROR,
2316 FWUPD_ERROR_INTERNAL,
2317 "unspecified error");
2318 }
2319 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
2320 "failed to get_results using %s: ",
2321 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00002322 return FALSE;
2323 }
Richard Hughescff38bc2016-12-12 12:03:37 +00002324 return TRUE;
2325}
2326
Richard Hughes08a37992017-09-12 12:57:43 +01002327/**
2328 * fu_plugin_get_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002329 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002330 *
2331 * Gets the plugin order, where higher numbers are run after lower
2332 * numbers.
2333 *
2334 * Returns: the integer value
Mario Limonciello1a680f32019-11-25 19:44:53 -06002335 *
2336 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002337 **/
2338guint
Richard Hughes12724852018-09-04 13:53:44 +01002339fu_plugin_get_order (FuPlugin *self)
Richard Hughes08a37992017-09-12 12:57:43 +01002340{
Richard Hughes12724852018-09-04 13:53:44 +01002341 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01002342 return priv->order;
2343}
2344
2345/**
2346 * fu_plugin_set_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002347 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002348 * @order: a integer value
2349 *
2350 * Sets the plugin order, where higher numbers are run after lower
2351 * numbers.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002352 *
2353 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002354 **/
2355void
Richard Hughes12724852018-09-04 13:53:44 +01002356fu_plugin_set_order (FuPlugin *self, guint order)
Richard Hughes08a37992017-09-12 12:57:43 +01002357{
Richard Hughes12724852018-09-04 13:53:44 +01002358 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01002359 priv->order = order;
2360}
2361
2362/**
Richard Hughes81c427c2018-08-06 15:20:17 +01002363 * fu_plugin_get_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002364 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01002365 *
2366 * Gets the plugin priority, where higher numbers are better.
2367 *
2368 * Returns: the integer value
Mario Limonciello1a680f32019-11-25 19:44:53 -06002369 *
2370 * Since: 1.1.1
Richard Hughes81c427c2018-08-06 15:20:17 +01002371 **/
2372guint
Richard Hughes12724852018-09-04 13:53:44 +01002373fu_plugin_get_priority (FuPlugin *self)
Richard Hughes81c427c2018-08-06 15:20:17 +01002374{
Richard Hughes12724852018-09-04 13:53:44 +01002375 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01002376 return priv->priority;
2377}
2378
2379/**
2380 * fu_plugin_set_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002381 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01002382 * @priority: a integer value
2383 *
2384 * Sets the plugin priority, where higher numbers are better.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002385 *
2386 * Since: 1.0.0
Richard Hughes81c427c2018-08-06 15:20:17 +01002387 **/
2388void
Richard Hughes12724852018-09-04 13:53:44 +01002389fu_plugin_set_priority (FuPlugin *self, guint priority)
Richard Hughes81c427c2018-08-06 15:20:17 +01002390{
Richard Hughes12724852018-09-04 13:53:44 +01002391 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01002392 priv->priority = priority;
2393}
2394
2395/**
Richard Hughes08a37992017-09-12 12:57:43 +01002396 * fu_plugin_add_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002397 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002398 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01002399 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01002400 *
2401 * If the plugin name is found, the rule will be used to sort the plugin list,
2402 * for example the plugin specified by @name will be ordered after this plugin
2403 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
2404 *
2405 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
2406 * If depsolving fails then fwupd will not start.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002407 *
2408 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002409 **/
2410void
Richard Hughes12724852018-09-04 13:53:44 +01002411fu_plugin_add_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes08a37992017-09-12 12:57:43 +01002412{
Richard Hughes12724852018-09-04 13:53:44 +01002413 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01002414 g_ptr_array_add (priv->rules[rule], g_strdup (name));
Richard Hughes75b965d2018-11-15 13:51:21 +00002415 g_signal_emit (self, signals[SIGNAL_RULES_CHANGED], 0);
Richard Hughes08a37992017-09-12 12:57:43 +01002416}
2417
2418/**
2419 * fu_plugin_get_rules:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002420 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002421 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
2422 *
2423 * Gets the plugin IDs that should be run after this plugin.
2424 *
2425 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
Mario Limonciello1a680f32019-11-25 19:44:53 -06002426 *
2427 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002428 **/
2429GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +01002430fu_plugin_get_rules (FuPlugin *self, FuPluginRule rule)
Richard Hughes08a37992017-09-12 12:57:43 +01002431{
Richard Hughes12724852018-09-04 13:53:44 +01002432 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughesdad35972019-12-06 11:00:25 +00002433 g_return_val_if_fail (rule < FU_PLUGIN_RULE_LAST, NULL);
Richard Hughes08a37992017-09-12 12:57:43 +01002434 return priv->rules[rule];
2435}
2436
Richard Hughes80b79bb2018-01-11 21:11:06 +00002437/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002438 * fu_plugin_has_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002439 * @self: a #FuPlugin
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002440 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
2441 * @name: a plugin name, e.g. `upower`
2442 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01002443 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002444 *
2445 * Returns: %TRUE if the name exists for the specific rule
Mario Limonciello1a680f32019-11-25 19:44:53 -06002446 *
2447 * Since: 1.0.0
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002448 **/
2449gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002450fu_plugin_has_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002451{
Richard Hughes12724852018-09-04 13:53:44 +01002452 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002453 for (guint i = 0; i < priv->rules[rule]->len; i++) {
2454 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
2455 if (g_strcmp0 (tmp, name) == 0)
2456 return TRUE;
2457 }
2458 return FALSE;
2459}
2460
2461/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00002462 * fu_plugin_add_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002463 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00002464 * @key: a string, e.g. `FwupdateVersion`
2465 * @value: a string, e.g. `10`
2466 *
2467 * Sets any additional metadata to be included in the firmware report to aid
2468 * debugging problems.
2469 *
2470 * Any data included here will be sent to the metadata server after user
2471 * confirmation.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002472 *
2473 * Since: 1.0.4
Richard Hughes80b79bb2018-01-11 21:11:06 +00002474 **/
2475void
Richard Hughes12724852018-09-04 13:53:44 +01002476fu_plugin_add_report_metadata (FuPlugin *self, const gchar *key, const gchar *value)
Richard Hughes80b79bb2018-01-11 21:11:06 +00002477{
Richard Hughes12724852018-09-04 13:53:44 +01002478 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002479 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
2480}
2481
2482/**
2483 * fu_plugin_get_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002484 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00002485 *
2486 * Returns the list of additional metadata to be added when filing a report.
2487 *
2488 * Returns: (transfer none): the map of report metadata
Mario Limonciello1a680f32019-11-25 19:44:53 -06002489 *
2490 * Since: 1.0.4
Richard Hughes80b79bb2018-01-11 21:11:06 +00002491 **/
2492GHashTable *
Richard Hughes12724852018-09-04 13:53:44 +01002493fu_plugin_get_report_metadata (FuPlugin *self)
Richard Hughes80b79bb2018-01-11 21:11:06 +00002494{
Richard Hughes12724852018-09-04 13:53:44 +01002495 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002496 return priv->report_metadata;
2497}
2498
Mario Limonciello963dc422018-02-27 14:26:58 -06002499/**
2500 * fu_plugin_get_config_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002501 * @self: a #FuPlugin
Mario Limonciello963dc422018-02-27 14:26:58 -06002502 * @key: A settings key
2503 *
2504 * Return the value of a key if it's been configured
2505 *
2506 * Since: 1.0.6
2507 **/
2508gchar *
Richard Hughes12724852018-09-04 13:53:44 +01002509fu_plugin_get_config_value (FuPlugin *self, const gchar *key)
Mario Limonciello963dc422018-02-27 14:26:58 -06002510{
Richard Hughes4be17d12018-05-30 20:36:29 +01002511 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06002512 g_autofree gchar *conf_file = NULL;
2513 g_autofree gchar *conf_path = NULL;
2514 g_autoptr(GKeyFile) keyfile = NULL;
2515 const gchar *plugin_name;
2516
Richard Hughes4be17d12018-05-30 20:36:29 +01002517 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Richard Hughes12724852018-09-04 13:53:44 +01002518 plugin_name = fu_plugin_get_name (self);
Mario Limonciello963dc422018-02-27 14:26:58 -06002519 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01002520 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06002521 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
2522 return NULL;
2523 keyfile = g_key_file_new ();
2524 if (!g_key_file_load_from_file (keyfile, conf_path,
2525 G_KEY_FILE_NONE, NULL))
2526 return NULL;
2527 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
2528}
2529
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002530/**
Richard Hughes334ba792020-02-19 20:44:56 +00002531 * fu_plugin_get_config_value_boolean:
2532 * @self: a #FuPlugin
2533 * @key: A settings key
2534 *
2535 * Return the boolean value of a key if it's been configured
2536 *
2537 * Returns: %TRUE if the value is `true` (case insensitive), %FALSE otherwise
2538 *
Mario Limonciello96117d12020-02-28 10:17:56 -06002539 * Since: 1.4.0
Richard Hughes334ba792020-02-19 20:44:56 +00002540 **/
2541gboolean
2542fu_plugin_get_config_value_boolean (FuPlugin *self, const gchar *key)
2543{
2544 g_autofree gchar *tmp = fu_plugin_get_config_value (self, key);
2545 if (tmp == NULL)
2546 return FALSE;
Richard Hughes5337a432020-02-21 12:04:32 +00002547 return g_ascii_strcasecmp (tmp, "true") == 0;
Richard Hughes334ba792020-02-19 20:44:56 +00002548}
2549
2550/**
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002551 * fu_plugin_name_compare:
2552 * @plugin1: first #FuPlugin to compare.
2553 * @plugin2: second #FuPlugin to compare.
2554 *
2555 * Compares two plugins by their names.
2556 *
2557 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002558 *
2559 * Since: 1.0.8
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002560 **/
2561gint
2562fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2563{
2564 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
2565 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
2566 return g_strcmp0 (priv1->name, priv2->name);
2567}
2568
2569/**
2570 * fu_plugin_order_compare:
2571 * @plugin1: first #FuPlugin to compare.
2572 * @plugin2: second #FuPlugin to compare.
2573 *
2574 * Compares two plugins by their depsolved order.
2575 *
2576 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002577 *
2578 * Since: 1.0.8
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002579 **/
2580gint
2581fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2582{
2583 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
2584 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
2585 if (priv1->order < priv2->order)
2586 return -1;
2587 if (priv1->order > priv2->order)
2588 return 1;
2589 return 0;
2590}
2591
Richard Hughescff38bc2016-12-12 12:03:37 +00002592static void
2593fu_plugin_class_init (FuPluginClass *klass)
2594{
2595 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2596 object_class->finalize = fu_plugin_finalize;
2597 signals[SIGNAL_DEVICE_ADDED] =
2598 g_signal_new ("device-added",
2599 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2600 G_STRUCT_OFFSET (FuPluginClass, device_added),
2601 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2602 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
2603 signals[SIGNAL_DEVICE_REMOVED] =
2604 g_signal_new ("device-removed",
2605 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2606 G_STRUCT_OFFSET (FuPluginClass, device_removed),
2607 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2608 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01002609 signals[SIGNAL_DEVICE_REGISTER] =
2610 g_signal_new ("device-register",
2611 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2612 G_STRUCT_OFFSET (FuPluginClass, device_register),
2613 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2614 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00002615 signals[SIGNAL_RECOLDPLUG] =
2616 g_signal_new ("recoldplug",
2617 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2618 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
2619 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2620 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00002621 signals[SIGNAL_SET_COLDPLUG_DELAY] =
2622 g_signal_new ("set-coldplug-delay",
2623 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2624 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
2625 NULL, NULL, g_cclosure_marshal_VOID__UINT,
2626 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughesaabdc372018-11-14 10:11:08 +00002627 signals[SIGNAL_CHECK_SUPPORTED] =
2628 g_signal_new ("check-supported",
2629 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2630 G_STRUCT_OFFSET (FuPluginClass, check_supported),
2631 NULL, NULL, g_cclosure_marshal_generic,
2632 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
Richard Hughes75b965d2018-11-15 13:51:21 +00002633 signals[SIGNAL_RULES_CHANGED] =
2634 g_signal_new ("rules-changed",
2635 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2636 G_STRUCT_OFFSET (FuPluginClass, rules_changed),
2637 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2638 G_TYPE_NONE, 0);
Richard Hughes95c98a92019-10-22 16:03:15 +01002639 signals[SIGNAL_ADD_FIRMWARE_GTYPE] =
2640 g_signal_new ("add-firmware-gtype",
2641 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2642 G_STRUCT_OFFSET (FuPluginClass, add_firmware_gtype),
2643 NULL, NULL, g_cclosure_marshal_generic,
2644 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_GTYPE);
Richard Hughescff38bc2016-12-12 12:03:37 +00002645}
2646
2647static void
Richard Hughes12724852018-09-04 13:53:44 +01002648fu_plugin_init (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +00002649{
Richard Hughes12724852018-09-04 13:53:44 +01002650 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002651 priv->enabled = TRUE;
Richard Hughesb1065422019-08-15 16:44:34 +01002652 priv->udev_subsystems = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00002653 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
2654 g_free, (GDestroyNotify) g_object_unref);
Richard Hughes161e9b52019-06-12 14:22:45 +01002655 g_rw_lock_init (&priv->devices_mutex);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002656 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 +01002657 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
2658 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00002659}
2660
2661static void
2662fu_plugin_finalize (GObject *object)
2663{
Richard Hughes12724852018-09-04 13:53:44 +01002664 FuPlugin *self = FU_PLUGIN (object);
2665 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002666 FuPluginInitFunc func = NULL;
2667
2668 /* optional */
2669 if (priv->module != NULL) {
2670 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
2671 if (func != NULL) {
2672 g_debug ("performing destroy() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01002673 func (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002674 }
2675 }
2676
Richard Hughes08a37992017-09-12 12:57:43 +01002677 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
2678 g_ptr_array_unref (priv->rules[i]);
2679
Richard Hughescff38bc2016-12-12 12:03:37 +00002680 if (priv->usb_ctx != NULL)
2681 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01002682 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01002683 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01002684 if (priv->quirks != NULL)
2685 g_object_unref (priv->quirks);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002686 if (priv->udev_subsystems != NULL)
2687 g_ptr_array_unref (priv->udev_subsystems);
Richard Hughes49e5e052017-09-03 12:15:41 +01002688 if (priv->smbios != NULL)
2689 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01002690 if (priv->runtime_versions != NULL)
2691 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01002692 if (priv->compile_versions != NULL)
2693 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00002694 g_hash_table_unref (priv->devices);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002695 g_hash_table_unref (priv->report_metadata);
Richard Hughes161e9b52019-06-12 14:22:45 +01002696 g_rw_lock_clear (&priv->devices_mutex);
Richard Hughes84999302019-05-02 10:18:32 +01002697 g_free (priv->build_hash);
Richard Hughescff38bc2016-12-12 12:03:37 +00002698 g_free (priv->name);
2699 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05002700 /* Must happen as the last step to avoid prematurely
2701 * freeing memory held by the plugin */
2702#ifndef RUNNING_ON_VALGRIND
2703 if (priv->module != NULL)
2704 g_module_close (priv->module);
2705#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00002706
2707 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
2708}
2709
Mario Limonciello1a680f32019-11-25 19:44:53 -06002710/**
2711 * fu_plugin_new:
2712 *
2713 * Creates a new #FuPlugin
2714 *
2715 * Since: 0.8.0
2716 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002717FuPlugin *
2718fu_plugin_new (void)
2719{
Richard Hughes12724852018-09-04 13:53:44 +01002720 return FU_PLUGIN (g_object_new (FU_TYPE_PLUGIN, NULL));
Richard Hughescff38bc2016-12-12 12:03:37 +00002721}