blob: 58721ec3c0512fe9d93f9ba52dfd3055676968f9 [file] [log] [blame]
Richard Hughes02c90d82018-08-09 12:13:03 +01001/*
Richard Hughes5c9b1fc2021-01-07 14:20:49 +00002 * Copyright (C) 2016 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;
Richard Hughes08a37992017-09-12 12:57:43 +010039 guint order;
Richard Hughes81c427c2018-08-06 15:20:17 +010040 guint priority;
Richard Hughes08a37992017-09-12 12:57:43 +010041 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughes68ab1e42021-01-13 14:01:17 +000042 GPtrArray *devices; /* (nullable) (element-type FuDevice) */
Richard Hughesf425d292019-01-18 17:57:39 +000043 gchar *build_hash;
Richard Hughesd7704d42017-08-08 20:29:09 +010044 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010045 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010046 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010047 GHashTable *compile_versions;
Richard Hughes9d6e0e72018-08-24 20:20:17 +010048 GPtrArray *udev_subsystems;
Richard Hughes1354ea92017-09-19 15:58:31 +010049 FuSmbios *smbios;
Richard Hughes989acf12019-10-05 20:16:47 +010050 GType device_gtype;
Richard Hughesfaf2afe2021-01-13 14:00:20 +000051 GHashTable *cache; /* (nullable): platform_id:GObject */
52 GRWLock cache_mutex;
Richard Hughes1d900f72020-06-22 15:17:39 +010053 GHashTable *report_metadata; /* (nullable): key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000054 FuPluginData *data;
55} FuPluginPrivate;
56
57enum {
58 SIGNAL_DEVICE_ADDED,
59 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010060 SIGNAL_DEVICE_REGISTER,
Richard Hughes75b965d2018-11-15 13:51:21 +000061 SIGNAL_RULES_CHANGED,
Richard Hughes362d6d72017-01-07 21:42:14 +000062 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000063 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughesaabdc372018-11-14 10:11:08 +000064 SIGNAL_CHECK_SUPPORTED,
Richard Hughes95c98a92019-10-22 16:03:15 +010065 SIGNAL_ADD_FIRMWARE_GTYPE,
Richard Hughes399859e2020-05-11 19:44:03 +010066 SIGNAL_SECURITY_CHANGED,
Richard Hughescff38bc2016-12-12 12:03:37 +000067 SIGNAL_LAST
68};
69
70static guint signals[SIGNAL_LAST] = { 0 };
71
Richard Hughes7bcb8d42020-10-08 15:47:47 +010072G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, FWUPD_TYPE_PLUGIN)
Richard Hughescff38bc2016-12-12 12:03:37 +000073#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
74
75typedef const gchar *(*FuPluginGetNameFunc) (void);
Richard Hughes12724852018-09-04 13:53:44 +010076typedef void (*FuPluginInitFunc) (FuPlugin *self);
77typedef gboolean (*FuPluginStartupFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000078 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010079typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *self,
Richard Hughese1fd34d2017-08-24 14:19:51 +010080 FuDevice *device);
Richard Hughes12724852018-09-04 13:53:44 +010081typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000082 FuDevice *device,
83 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010084typedef gboolean (*FuPluginFlaggedDeviceFunc) (FuPlugin *self,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -050085 FwupdInstallFlags flags,
86 FuDevice *device,
87 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010088typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *self,
Richard Hughesdbd8c762018-06-15 20:31:40 +010089 GPtrArray *devices,
90 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010091typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000092 FuDevice *device,
93 FuPluginVerifyFlags flags,
94 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010095typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000096 FuDevice *device,
97 GBytes *blob_fw,
98 FwupdInstallFlags flags,
99 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100100typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100101 FuUsbDevice *device,
Richard Hughes104f6512017-11-24 11:44:57 +0000102 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100103typedef gboolean (*FuPluginUdevDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100104 FuUdevDevice *device,
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100105 GError **error);
Richard Hughesf58ac732020-05-12 15:23:44 +0100106typedef void (*FuPluginSecurityAttrsFunc) (FuPlugin *self,
107 FuSecurityAttrs *attrs);
Richard Hughescff38bc2016-12-12 12:03:37 +0000108
Richard Hughes57d18222017-01-10 16:02:59 +0000109/**
Mario Limonciello52e75ba2019-11-22 13:21:19 -0600110 * fu_plugin_is_open:
111 * @self: A #FuPlugin
112 *
113 * Determines if the plugin is opened
114 *
115 * Returns: TRUE for opened, FALSE for not
116 *
117 * Since: 1.3.5
118 **/
119gboolean
120fu_plugin_is_open (FuPlugin *self)
121{
122 FuPluginPrivate *priv = GET_PRIVATE (self);
123 return priv->module != NULL;
124}
125
126/**
Richard Hughes57d18222017-01-10 16:02:59 +0000127 * fu_plugin_get_name:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100128 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000129 *
130 * Gets the plugin name.
131 *
132 * Returns: a plugin name, or %NULL for unknown.
133 *
134 * Since: 0.8.0
135 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000136const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100137fu_plugin_get_name (FuPlugin *self)
Richard Hughesd0905142016-03-13 09:46:49 +0000138{
Richard Hughes12724852018-09-04 13:53:44 +0100139 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100140 return fwupd_plugin_get_name (FWUPD_PLUGIN (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000141}
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 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100156 fwupd_plugin_set_name (FWUPD_PLUGIN (self), name);
Richard Hughes34834102017-11-21 21:55:00 +0000157}
158
Richard Hughes57d18222017-01-10 16:02:59 +0000159/**
Richard Hughesf425d292019-01-18 17:57:39 +0000160 * fu_plugin_set_build_hash:
161 * @self: A #FuPlugin
162 * @build_hash: A checksum
163 *
164 * Sets the plugin build hash, typically a SHA256 checksum. All plugins must
165 * set the correct checksum to avoid the daemon being marked as tainted.
166 *
167 * Since: 1.2.4
168 **/
169void
170fu_plugin_set_build_hash (FuPlugin *self, const gchar *build_hash)
171{
172 FuPluginPrivate *priv = GET_PRIVATE (self);
173 g_return_if_fail (FU_IS_PLUGIN (self));
174 g_return_if_fail (build_hash != NULL);
Richard Hughes382524d2021-01-28 13:14:35 +0000175
176 /* not changed */
177 if (g_strcmp0 (priv->build_hash, build_hash) == 0)
178 return;
179
Richard Hughesf425d292019-01-18 17:57:39 +0000180 g_free (priv->build_hash);
181 priv->build_hash = g_strdup (build_hash);
182}
183
Mario Limonciello1a680f32019-11-25 19:44:53 -0600184/**
185 * fu_plugin_get_build_hash:
186 * @self: A #FuPlugin
187 *
188 * Gets the build hash a plugin was generated with.
189 *
190 * Returns: (transfer none): a #gchar, or %NULL for unset.
191 *
192 * Since: 1.2.4
193 **/
Richard Hughesf425d292019-01-18 17:57:39 +0000194const gchar *
195fu_plugin_get_build_hash (FuPlugin *self)
196{
197 FuPluginPrivate *priv = GET_PRIVATE (self);
198 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
199 return priv->build_hash;
200}
201
202/**
Richard Hughes57d18222017-01-10 16:02:59 +0000203 * fu_plugin_cache_lookup:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100204 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000205 * @id: the key
206 *
207 * Finds an object in the per-plugin cache.
208 *
209 * Returns: (transfer none): a #GObject, or %NULL for unfound.
210 *
211 * Since: 0.8.0
212 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000213gpointer
Richard Hughes12724852018-09-04 13:53:44 +0100214fu_plugin_cache_lookup (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000215{
Richard Hughes12724852018-09-04 13:53:44 +0100216 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000217 g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_reader_locker_new (&priv->cache_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100218 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughesccd78a92017-01-11 16:57:41 +0000219 g_return_val_if_fail (id != NULL, NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100220 g_return_val_if_fail (locker != NULL, NULL);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000221 if (priv->cache == NULL)
Richard Hughes371f6b22020-06-22 15:21:17 +0100222 return NULL;
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000223 return g_hash_table_lookup (priv->cache, id);
Richard Hughescff38bc2016-12-12 12:03:37 +0000224}
Richard Hughesd0905142016-03-13 09:46:49 +0000225
Richard Hughes57d18222017-01-10 16:02:59 +0000226/**
227 * fu_plugin_cache_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100228 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000229 * @id: the key
230 * @dev: a #GObject, typically a #FuDevice
231 *
232 * Adds an object to the per-plugin cache.
233 *
234 * Since: 0.8.0
235 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000236void
Richard Hughes12724852018-09-04 13:53:44 +0100237fu_plugin_cache_add (FuPlugin *self, const gchar *id, gpointer dev)
Richard Hughescff38bc2016-12-12 12:03:37 +0000238{
Richard Hughes12724852018-09-04 13:53:44 +0100239 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000240 g_autoptr(GRWLockWriterLocker) locker = g_rw_lock_writer_locker_new (&priv->cache_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100241 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000242 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100243 g_return_if_fail (locker != NULL);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000244 if (priv->cache == NULL) {
245 priv->cache = g_hash_table_new_full (g_str_hash,
246 g_str_equal,
247 g_free,
248 (GDestroyNotify) g_object_unref);
Richard Hughes371f6b22020-06-22 15:21:17 +0100249 }
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000250 g_hash_table_insert (priv->cache, g_strdup (id), g_object_ref (dev));
Richard Hughescff38bc2016-12-12 12:03:37 +0000251}
252
Richard Hughes57d18222017-01-10 16:02:59 +0000253/**
254 * fu_plugin_cache_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100255 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000256 * @id: the key
257 *
258 * Removes an object from the per-plugin cache.
259 *
260 * Since: 0.8.0
261 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000262void
Richard Hughes12724852018-09-04 13:53:44 +0100263fu_plugin_cache_remove (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000264{
Richard Hughes12724852018-09-04 13:53:44 +0100265 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000266 g_autoptr(GRWLockWriterLocker) locker = g_rw_lock_writer_locker_new (&priv->cache_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100267 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000268 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100269 g_return_if_fail (locker != NULL);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000270 if (priv->cache == NULL)
Richard Hughes371f6b22020-06-22 15:21:17 +0100271 return;
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000272 g_hash_table_remove (priv->cache, id);
Richard Hughescff38bc2016-12-12 12:03:37 +0000273}
274
Richard Hughes57d18222017-01-10 16:02:59 +0000275/**
276 * fu_plugin_get_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100277 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000278 *
Richard Hughes4eada342017-10-03 21:20:32 +0100279 * Gets the per-plugin allocated private data. This will return %NULL unless
280 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000281 *
Richard Hughes4eada342017-10-03 21:20:32 +0100282 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000283 *
284 * Since: 0.8.0
285 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000286FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100287fu_plugin_get_data (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000288{
Richard Hughes12724852018-09-04 13:53:44 +0100289 FuPluginPrivate *priv = GET_PRIVATE (self);
290 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000291 return priv->data;
292}
293
Richard Hughes57d18222017-01-10 16:02:59 +0000294/**
Richard Hughes00f66f62019-11-27 11:42:53 +0000295 * fu_plugin_alloc_data: (skip):
Richard Hughes2c0635a2018-09-04 14:52:46 +0100296 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000297 * @data_sz: the size to allocate
298 *
299 * Allocates the per-plugin allocated private data.
300 *
301 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
302 *
303 * Since: 0.8.0
304 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000305FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100306fu_plugin_alloc_data (FuPlugin *self, gsize data_sz)
Richard Hughescff38bc2016-12-12 12:03:37 +0000307{
Richard Hughes12724852018-09-04 13:53:44 +0100308 FuPluginPrivate *priv = GET_PRIVATE (self);
309 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000310 if (priv->data != NULL) {
311 g_critical ("fu_plugin_alloc_data() already used by plugin");
312 return priv->data;
313 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000314 priv->data = g_malloc0 (data_sz);
315 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000316}
317
Richard Hughes57d18222017-01-10 16:02:59 +0000318/**
319 * fu_plugin_get_usb_context:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100320 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000321 *
Richard Hughes9e9b73f2021-02-09 13:37:41 +0000322 * This used to get the shared USB context that all plugins can use; it now
323 * returns %NULL;
Richard Hughes57d18222017-01-10 16:02:59 +0000324 *
325 * Returns: (transfer none): a #GUsbContext.
326 *
327 * Since: 0.8.0
328 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000329GUsbContext *
Richard Hughes12724852018-09-04 13:53:44 +0100330fu_plugin_get_usb_context (FuPlugin *self)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000331{
Richard Hughes12724852018-09-04 13:53:44 +0100332 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes9e9b73f2021-02-09 13:37:41 +0000333 return NULL;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000334}
335
Mario Limonciello1a680f32019-11-25 19:44:53 -0600336/**
337 * fu_plugin_set_usb_context:
338 * @self: A #FuPlugin
339 * @usb_ctx: A #FGUsbContext
340 *
Richard Hughes9e9b73f2021-02-09 13:37:41 +0000341 * This used to set the shared USB context for a plugin. It now does nothing.
Mario Limonciello1a680f32019-11-25 19:44:53 -0600342 *
343 * Since: 0.8.0
344 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000345void
Richard Hughes12724852018-09-04 13:53:44 +0100346fu_plugin_set_usb_context (FuPlugin *self, GUsbContext *usb_ctx)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000347{
Richard Hughescff38bc2016-12-12 12:03:37 +0000348}
349
Richard Hughes57d18222017-01-10 16:02:59 +0000350/**
351 * fu_plugin_get_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100352 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000353 *
Richard Hughes4eada342017-10-03 21:20:32 +0100354 * Returns if the plugin is enabled. Plugins may self-disable using
355 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000356 *
357 * Returns: %TRUE if the plugin is currently enabled.
358 *
359 * Since: 0.8.0
360 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000361gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100362fu_plugin_get_enabled (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000363{
Richard Hughes12724852018-09-04 13:53:44 +0100364 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100365 return !fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED);
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000366}
367
Richard Hughes57d18222017-01-10 16:02:59 +0000368/**
369 * fu_plugin_set_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100370 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000371 * @enabled: the enabled value
372 *
373 * Enables or disables a plugin. Plugins can self-disable at any point.
374 *
375 * Since: 0.8.0
376 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000377void
Richard Hughes12724852018-09-04 13:53:44 +0100378fu_plugin_set_enabled (FuPlugin *self, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000379{
Richard Hughes12724852018-09-04 13:53:44 +0100380 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100381 if (enabled) {
382 fwupd_plugin_remove_flag (FWUPD_PLUGIN (self),
383 FWUPD_PLUGIN_FLAG_DISABLED);
384 } else {
385 fu_plugin_add_flag (self, FWUPD_PLUGIN_FLAG_DISABLED);
386 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000387}
388
Mario Limonciello1a680f32019-11-25 19:44:53 -0600389/**
390 * fu_plugin_guess_name_from_fn:
391 * @filename: filename to guess
392 *
393 * Tries to guess the name of the plugin from a filename
394 *
395 * Returns: (transfer full): the guessed name of the plugin
396 *
397 * Since: 1.0.8
398 **/
Richard Hughes1e456bc2018-05-10 20:16:16 +0100399gchar *
400fu_plugin_guess_name_from_fn (const gchar *filename)
401{
402 const gchar *prefix = "libfu_plugin_";
403 gchar *name;
404 gchar *str = g_strstr_len (filename, -1, prefix);
405 if (str == NULL)
406 return NULL;
407 name = g_strdup (str + strlen (prefix));
408 g_strdelimit (name, ".", '\0');
409 return name;
410}
411
Mario Limonciello1a680f32019-11-25 19:44:53 -0600412/**
413 * fu_plugin_open:
414 * @self: A #FuPlugin
415 * @filename: The shared object filename to open
416 * @error: A #GError or NULL
417 *
418 * Opens the plugin module
419 *
420 * Returns: TRUE for success, FALSE for fail
421 *
422 * Since: 0.8.0
423 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000424gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100425fu_plugin_open (FuPlugin *self, const gchar *filename, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +0000426{
Richard Hughes12724852018-09-04 13:53:44 +0100427 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000428 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000429
Richard Hughes6a489a92020-12-22 10:32:06 +0000430 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
431 g_return_val_if_fail (filename != NULL, FALSE);
432 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
433
Richard Hughescff38bc2016-12-12 12:03:37 +0000434 priv->module = g_module_open (filename, 0);
435 if (priv->module == NULL) {
436 g_set_error (error,
437 G_IO_ERROR,
438 G_IO_ERROR_FAILED,
Mario Limonciellof5605532019-11-04 07:49:50 -0600439 "failed to open plugin %s: %s",
440 filename, g_module_error ());
Mario Limoncielloc3a81732020-10-20 09:16:18 -0500441 fu_plugin_add_flag (self, FWUPD_PLUGIN_FLAG_FAILED_OPEN);
442 fu_plugin_add_flag (self, FWUPD_PLUGIN_FLAG_USER_WARNING);
Richard Hughescff38bc2016-12-12 12:03:37 +0000443 return FALSE;
444 }
445
446 /* set automatically */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100447 if (fu_plugin_get_name (self) == NULL) {
448 g_autofree gchar *str = fu_plugin_guess_name_from_fn (filename);
449 fu_plugin_set_name (self, str);
450 }
Richard Hughesd0905142016-03-13 09:46:49 +0000451
452 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000453 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
454 if (func != NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -0500455 g_debug ("init(%s)", filename);
Richard Hughes12724852018-09-04 13:53:44 +0100456 func (self);
Richard Hughesd0905142016-03-13 09:46:49 +0000457 }
458
Richard Hughescff38bc2016-12-12 12:03:37 +0000459 return TRUE;
460}
461
Richard Hughes203ed842020-11-02 14:25:13 +0000462/* order of usefulness to the user */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100463static const gchar *
464fu_plugin_build_device_update_error (FuPlugin *self)
465{
466 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_NO_HARDWARE))
467 return "Not updatable as required hardware was not found";
468 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_LEGACY_BIOS))
469 return "Not updatable in legacy BIOS mode";
470 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_CAPSULES_UNSUPPORTED))
Mario Limonciello797da4f2021-01-12 12:38:51 -0600471 return "Not updatable as UEFI capsule updates not enabled in firmware setup";
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100472 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_UNLOCK_REQUIRED))
473 return "Not updatable as requires unlock";
474 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_EFIVAR_NOT_MOUNTED))
475 return "Not updatable as efivarfs was not found";
476 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_ESP_NOT_FOUND))
477 return "Not updatable as UEFI ESP partition not detected";
478 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
479 return "Not updatable as plugin was disabled";
480 return NULL;
481}
482
Richard Hughes68ab1e42021-01-13 14:01:17 +0000483static void
484fu_plugin_ensure_devices (FuPlugin *self)
485{
486 FuPluginPrivate *priv = GET_PRIVATE (self);
487 if (priv->devices != NULL)
488 return;
489 priv->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
490}
491
Richard Hughes57d18222017-01-10 16:02:59 +0000492/**
493 * fu_plugin_device_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100494 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000495 * @device: A #FuDevice
496 *
497 * Asks the daemon to add a device to the exported list. If this device ID
498 * has already been added by a different plugin then this request will be
499 * ignored.
500 *
501 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
502 * actually flashing an image to the hardware so that higher-priority plugins
503 * can add the device themselves.
504 *
505 * Since: 0.8.0
506 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000507void
Richard Hughes12724852018-09-04 13:53:44 +0100508fu_plugin_device_add (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000509{
Richard Hughes68ab1e42021-01-13 14:01:17 +0000510 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes5e447292018-04-27 14:25:54 +0100511 GPtrArray *children;
Richard Hughesc125ec02018-09-05 19:35:17 +0100512 g_autoptr(GError) error = NULL;
Richard Hughes5e447292018-04-27 14:25:54 +0100513
Richard Hughes12724852018-09-04 13:53:44 +0100514 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000515 g_return_if_fail (FU_IS_DEVICE (device));
516
Richard Hughesc125ec02018-09-05 19:35:17 +0100517 /* ensure the device ID is set from the physical and logical IDs */
518 if (!fu_device_ensure_id (device, &error)) {
519 g_warning ("ignoring add: %s", error->message);
520 return;
521 }
522
Richard Hughes68ab1e42021-01-13 14:01:17 +0000523 /* add to array */
524 fu_plugin_ensure_devices (self);
525 g_ptr_array_add (priv->devices, g_object_ref (device));
526
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100527 /* proxy to device where required */
528 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_CLEAR_UPDATABLE)) {
529 g_debug ("plugin %s has _CLEAR_UPDATABLE, so removing from %s",
530 fu_plugin_get_name (self),
531 fu_device_get_id (device));
532 fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE);
533 }
534 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_USER_WARNING) &&
535 fu_device_get_update_error (device) == NULL) {
536 const gchar *tmp = fu_plugin_build_device_update_error (self);
537 g_debug ("setting %s update error to '%s' from %s",
538 fu_device_get_id (device), tmp, fu_plugin_get_name (self));
539 fu_device_set_update_error (device, tmp);
540 }
541
Richard Hughescff38bc2016-12-12 12:03:37 +0000542 g_debug ("emit added from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100543 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000544 fu_device_get_id (device));
545 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
Richard Hughes12724852018-09-04 13:53:44 +0100546 fu_device_set_plugin (device, fu_plugin_get_name (self));
547 g_signal_emit (self, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100548
Richard Hughes128c0162018-08-10 11:00:29 +0100549 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100550 children = fu_device_get_children (device);
551 for (guint i = 0; i < children->len; i++) {
552 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100553 if (fu_device_get_created (child) == 0)
Richard Hughes12724852018-09-04 13:53:44 +0100554 fu_plugin_device_add (self, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100555 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000556}
557
Richard Hughese1fd34d2017-08-24 14:19:51 +0100558/**
Richard Hughes68ab1e42021-01-13 14:01:17 +0000559 * fu_plugin_get_devices:
560 * @self: A #FuPlugin
561 *
562 * Returns all devices added by the plugin using fu_plugin_device_add() and
563 * not yet removed with fu_plugin_device_remove().
564 *
565 * Returns: (transfer none) (element-type FuDevice): devices
566 *
567 * Since: 1.5.6
568 **/
569GPtrArray *
570fu_plugin_get_devices (FuPlugin *self)
571{
572 FuPluginPrivate *priv = GET_PRIVATE (self);
573 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
574 fu_plugin_ensure_devices (self);
575 return priv->devices;
576}
577
578/**
Richard Hughese1fd34d2017-08-24 14:19:51 +0100579 * fu_plugin_device_register:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100580 * @self: A #FuPlugin
Richard Hughese1fd34d2017-08-24 14:19:51 +0100581 * @device: A #FuDevice
582 *
583 * Registers the device with other plugins so they can set metadata.
584 *
585 * Plugins do not have to call this manually as this is done automatically
586 * when using fu_plugin_device_add(). They may wish to use this manually
Richard Hughes21eaeef2020-01-14 12:10:01 +0000587 * if for instance the coldplug should be ignored based on the metadata
Richard Hughese1fd34d2017-08-24 14:19:51 +0100588 * set from other plugins.
589 *
590 * Since: 0.9.7
591 **/
592void
Richard Hughes12724852018-09-04 13:53:44 +0100593fu_plugin_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +0100594{
Richard Hughesc125ec02018-09-05 19:35:17 +0100595 g_autoptr(GError) error = NULL;
596
Richard Hughes12724852018-09-04 13:53:44 +0100597 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughese1fd34d2017-08-24 14:19:51 +0100598 g_return_if_fail (FU_IS_DEVICE (device));
599
Richard Hughesc125ec02018-09-05 19:35:17 +0100600 /* ensure the device ID is set from the physical and logical IDs */
601 if (!fu_device_ensure_id (device, &error)) {
602 g_warning ("ignoring registration: %s", error->message);
603 return;
604 }
605
Richard Hughese1fd34d2017-08-24 14:19:51 +0100606 g_debug ("emit device-register from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100607 fu_plugin_get_name (self),
Richard Hughese1fd34d2017-08-24 14:19:51 +0100608 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100609 g_signal_emit (self, signals[SIGNAL_DEVICE_REGISTER], 0, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +0100610}
611
Richard Hughes57d18222017-01-10 16:02:59 +0000612/**
Richard Hughes4eada342017-10-03 21:20:32 +0100613 * fu_plugin_device_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100614 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000615 * @device: A #FuDevice
616 *
617 * Asks the daemon to remove a device from the exported list.
618 *
619 * Since: 0.8.0
620 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000621void
Richard Hughes12724852018-09-04 13:53:44 +0100622fu_plugin_device_remove (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000623{
Richard Hughes68ab1e42021-01-13 14:01:17 +0000624 FuPluginPrivate *priv = GET_PRIVATE (self);
625
Richard Hughes12724852018-09-04 13:53:44 +0100626 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000627 g_return_if_fail (FU_IS_DEVICE (device));
628
Richard Hughes68ab1e42021-01-13 14:01:17 +0000629 /* remove from array */
630 if (priv->devices != NULL)
631 g_ptr_array_remove (priv->devices, device);
632
Richard Hughescff38bc2016-12-12 12:03:37 +0000633 g_debug ("emit removed from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100634 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000635 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100636 g_signal_emit (self, signals[SIGNAL_DEVICE_REMOVED], 0, device);
Richard Hughescff38bc2016-12-12 12:03:37 +0000637}
638
Richard Hughes57d18222017-01-10 16:02:59 +0000639/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000640 * fu_plugin_request_recoldplug:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100641 * @self: A #FuPlugin
Richard Hughes362d6d72017-01-07 21:42:14 +0000642 *
643 * Ask all the plugins to coldplug all devices, which will include the prepare()
644 * and cleanup() phases. Duplicate devices added will be ignored.
645 *
646 * Since: 0.8.0
647 **/
648void
Richard Hughes12724852018-09-04 13:53:44 +0100649fu_plugin_request_recoldplug (FuPlugin *self)
Richard Hughes362d6d72017-01-07 21:42:14 +0000650{
Richard Hughes12724852018-09-04 13:53:44 +0100651 g_return_if_fail (FU_IS_PLUGIN (self));
652 g_signal_emit (self, signals[SIGNAL_RECOLDPLUG], 0);
Richard Hughes362d6d72017-01-07 21:42:14 +0000653}
654
Richard Hughesb0829032017-01-10 09:27:08 +0000655/**
Richard Hughes399859e2020-05-11 19:44:03 +0100656 * fu_plugin_security_changed:
657 * @self: A #FuPlugin
658 *
659 * Informs the daemon that the HSI state may have changed.
660 *
661 * Since: 1.5.0
662 **/
663void
664fu_plugin_security_changed (FuPlugin *self)
665{
666 g_return_if_fail (FU_IS_PLUGIN (self));
667 g_signal_emit (self, signals[SIGNAL_SECURITY_CHANGED], 0);
668}
669
670/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100671 * fu_plugin_check_hwid:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100672 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100673 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100674 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100675 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100676 * specific system can be shown using the `fwupdmgr hwids` command.
677 *
Richard Hughes4eada342017-10-03 21:20:32 +0100678 * Returns: %TRUE if the HwId is found on the system.
679 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100680 * Since: 0.9.1
681 **/
682gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100683fu_plugin_check_hwid (FuPlugin *self, const gchar *hwid)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100684{
Richard Hughes12724852018-09-04 13:53:44 +0100685 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100686 if (priv->hwids == NULL)
687 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100688 return fu_hwids_has_guid (priv->hwids, hwid);
689}
690
691/**
Patrick Rudolpha60b5472019-10-16 10:43:03 +0200692 * fu_plugin_get_hwid_replace_value:
693 * @self: A #FuPlugin
694 * @keys: A key, e.g. `HardwareID-3` or %FU_HWIDS_KEY_PRODUCT_SKU
695 * @error: A #GError or %NULL
696 *
697 * Gets the replacement value for a specific key. All hardware IDs on a
698 * specific system can be shown using the `fwupdmgr hwids` command.
699 *
700 * Returns: (transfer full): a string, or %NULL for error.
701 *
702 * Since: 1.3.3
703 **/
704gchar *
705fu_plugin_get_hwid_replace_value (FuPlugin *self, const gchar *keys, GError **error)
706{
707 FuPluginPrivate *priv = GET_PRIVATE (self);
708 if (priv->hwids == NULL)
709 return NULL;
710
711 return fu_hwids_get_replace_values (priv->hwids, keys, error);
712}
713
714/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100715 * fu_plugin_get_hwids:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100716 * @self: A #FuPlugin
Richard Hughes69a5f352018-08-08 11:58:15 +0100717 *
718 * Returns all the HWIDs defined in the system. All hardware IDs on a
719 * specific system can be shown using the `fwupdmgr hwids` command.
720 *
Mario Limonciello1a680f32019-11-25 19:44:53 -0600721 * Returns: (transfer none) (element-type utf8): An array of GUIDs
Richard Hughes69a5f352018-08-08 11:58:15 +0100722 *
723 * Since: 1.1.1
724 **/
725GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +0100726fu_plugin_get_hwids (FuPlugin *self)
Richard Hughes69a5f352018-08-08 11:58:15 +0100727{
Richard Hughes12724852018-09-04 13:53:44 +0100728 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes69a5f352018-08-08 11:58:15 +0100729 if (priv->hwids == NULL)
730 return NULL;
731 return fu_hwids_get_guids (priv->hwids);
732}
733
734/**
Richard Hughes19841802019-09-10 16:48:00 +0100735 * fu_plugin_has_custom_flag:
736 * @self: A #FuPlugin
737 * @flag: A custom text flag, specific to the plugin, e.g. `uefi-force-enable`
738 *
739 * Returns if a per-plugin HwId custom flag exists, typically added from a DMI quirk.
740 *
741 * Returns: %TRUE if the quirk entry exists
742 *
743 * Since: 1.3.1
744 **/
745gboolean
746fu_plugin_has_custom_flag (FuPlugin *self, const gchar *flag)
747{
748 FuPluginPrivate *priv = GET_PRIVATE (self);
749 GPtrArray *hwids = fu_plugin_get_hwids (self);
750
751 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
752 g_return_val_if_fail (flag != NULL, FALSE);
753
754 /* never set up, e.g. in tests */
755 if (hwids == NULL)
756 return FALSE;
757
758 /* search each hwid */
759 for (guint i = 0; i < hwids->len; i++) {
760 const gchar *hwid = g_ptr_array_index (hwids, i);
761 const gchar *value;
762 g_autofree gchar *key = g_strdup_printf ("HwId=%s", hwid);
763
764 /* does prefixed quirk exist */
765 value = fu_quirks_lookup_by_id (priv->quirks, key, FU_QUIRKS_FLAGS);
766 if (value != NULL) {
767 g_auto(GStrv) quirks = g_strsplit (value, ",", -1);
768 if (g_strv_contains ((const gchar * const *) quirks, flag))
769 return TRUE;
770 }
771 }
772 return FALSE;
773}
774
775/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100776 * fu_plugin_check_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100777 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100778 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100779 *
780 * Checks to see if a specific device GUID is supported, i.e. available in the
781 * AppStream metadata.
782 *
Richard Hughes4eada342017-10-03 21:20:32 +0100783 * Returns: %TRUE if the device is supported.
784 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100785 * Since: 1.0.0
786 **/
Richard Hughesd8a8d5e2019-10-08 13:05:02 +0100787static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100788fu_plugin_check_supported (FuPlugin *self, const gchar *guid)
Richard Hughes1354ea92017-09-19 15:58:31 +0100789{
Richard Hughesaabdc372018-11-14 10:11:08 +0000790 gboolean retval = FALSE;
791 g_signal_emit (self, signals[SIGNAL_CHECK_SUPPORTED], 0, guid, &retval);
792 return retval;
Richard Hughes1354ea92017-09-19 15:58:31 +0100793}
794
795/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100796 * fu_plugin_get_dmi_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100797 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100798 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100799 *
800 * Gets a hardware DMI value.
801 *
Richard Hughes4eada342017-10-03 21:20:32 +0100802 * Returns: The string, or %NULL
803 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100804 * Since: 0.9.7
805 **/
806const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100807fu_plugin_get_dmi_value (FuPlugin *self, const gchar *dmi_id)
Richard Hughesd7704d42017-08-08 20:29:09 +0100808{
Richard Hughes12724852018-09-04 13:53:44 +0100809 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100810 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100811 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100812 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100813}
814
Richard Hughes49e5e052017-09-03 12:15:41 +0100815/**
816 * fu_plugin_get_smbios_string:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100817 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100818 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
819 * @offset: A SMBIOS offset
820 *
821 * Gets a hardware SMBIOS string.
822 *
823 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
824 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
825 *
Richard Hughes4eada342017-10-03 21:20:32 +0100826 * Returns: A string, or %NULL
827 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100828 * Since: 0.9.8
829 **/
830const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100831fu_plugin_get_smbios_string (FuPlugin *self, guint8 structure_type, guint8 offset)
Richard Hughes49e5e052017-09-03 12:15:41 +0100832{
Richard Hughes12724852018-09-04 13:53:44 +0100833 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100834 if (priv->smbios == NULL)
835 return NULL;
836 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
837}
838
839/**
840 * fu_plugin_get_smbios_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100841 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100842 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
843 *
844 * Gets a hardware SMBIOS data.
845 *
Richard Hughesdfaca2d2019-08-01 08:08:03 +0100846 * Returns: (transfer full): A #GBytes, or %NULL
Richard Hughes4eada342017-10-03 21:20:32 +0100847 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100848 * Since: 0.9.8
849 **/
850GBytes *
Richard Hughes12724852018-09-04 13:53:44 +0100851fu_plugin_get_smbios_data (FuPlugin *self, guint8 structure_type)
Richard Hughes49e5e052017-09-03 12:15:41 +0100852{
Richard Hughes12724852018-09-04 13:53:44 +0100853 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100854 if (priv->smbios == NULL)
855 return NULL;
856 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
857}
858
Mario Limonciello1a680f32019-11-25 19:44:53 -0600859/**
860 * fu_plugin_set_hwids:
861 * @self: A #FuPlugin
862 * @hwids: A #FuHwids
863 *
864 * Sets the hwids for a plugin
865 *
866 * Since: 0.9.7
867 **/
Richard Hughesb8f8db22017-04-25 15:56:00 +0100868void
Richard Hughes12724852018-09-04 13:53:44 +0100869fu_plugin_set_hwids (FuPlugin *self, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100870{
Richard Hughes12724852018-09-04 13:53:44 +0100871 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100872 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100873}
874
Mario Limonciello1a680f32019-11-25 19:44:53 -0600875/**
876 * fu_plugin_set_udev_subsystems:
877 * @self: A #FuPlugin
Richard Hughesa0d81c72019-11-27 11:41:54 +0000878 * @udev_subsystems: (element-type utf8): A #GPtrArray
Mario Limonciello1a680f32019-11-25 19:44:53 -0600879 *
880 * Sets the udev subsystems used by a plugin
881 *
882 * Since: 1.1.2
883 **/
Richard Hughes49e5e052017-09-03 12:15:41 +0100884void
Richard Hughes12724852018-09-04 13:53:44 +0100885fu_plugin_set_udev_subsystems (FuPlugin *self, GPtrArray *udev_subsystems)
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100886{
Richard Hughes12724852018-09-04 13:53:44 +0100887 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100888 if (priv->udev_subsystems != NULL)
889 g_ptr_array_unref (priv->udev_subsystems);
890 priv->udev_subsystems = g_ptr_array_ref (udev_subsystems);
891}
892
Mario Limonciello1a680f32019-11-25 19:44:53 -0600893/**
894 * fu_plugin_set_quirks:
895 * @self: A #FuPlugin
896 * @quirks: A #FuQuirks
897 *
898 * Sets the quirks for a plugin
899 *
900 * Since: 1.0.1
901 **/
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100902void
Richard Hughes12724852018-09-04 13:53:44 +0100903fu_plugin_set_quirks (FuPlugin *self, FuQuirks *quirks)
Richard Hughes9c028f02017-10-28 21:14:28 +0100904{
Richard Hughes12724852018-09-04 13:53:44 +0100905 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100906 g_set_object (&priv->quirks, quirks);
907}
908
909/**
910 * fu_plugin_get_quirks:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100911 * @self: A #FuPlugin
Richard Hughes9c028f02017-10-28 21:14:28 +0100912 *
913 * Returns the hardware database object. This can be used to discover device
914 * quirks or other device-specific settings.
915 *
916 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
917 *
918 * Since: 1.0.1
919 **/
920FuQuirks *
Richard Hughes12724852018-09-04 13:53:44 +0100921fu_plugin_get_quirks (FuPlugin *self)
Richard Hughes9c028f02017-10-28 21:14:28 +0100922{
Richard Hughes12724852018-09-04 13:53:44 +0100923 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100924 return priv->quirks;
925}
926
Mario Limonciello1a680f32019-11-25 19:44:53 -0600927/**
928 * fu_plugin_set_runtime_versions:
929 * @self: A #FuPlugin
930 * @runtime_versions: A #GHashTables
931 *
932 * Sets the runtime versions for a plugin
933 *
934 * Since: 1.0.7
935 **/
Richard Hughes0eb123b2018-04-19 12:00:04 +0100936void
Richard Hughes12724852018-09-04 13:53:44 +0100937fu_plugin_set_runtime_versions (FuPlugin *self, GHashTable *runtime_versions)
Richard Hughes0eb123b2018-04-19 12:00:04 +0100938{
Richard Hughes12724852018-09-04 13:53:44 +0100939 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0eb123b2018-04-19 12:00:04 +0100940 priv->runtime_versions = g_hash_table_ref (runtime_versions);
941}
942
943/**
944 * fu_plugin_add_runtime_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100945 * @self: A #FuPlugin
Richard Hughes0eb123b2018-04-19 12:00:04 +0100946 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
947 * @version: A version string, e.g. "1.2.3"
948 *
Richard Hughesdce91202019-04-08 12:47:45 +0100949 * Sets a runtime version of a specific dependency.
Richard Hughes0eb123b2018-04-19 12:00:04 +0100950 *
951 * Since: 1.0.7
952 **/
953void
Richard Hughes12724852018-09-04 13:53:44 +0100954fu_plugin_add_runtime_version (FuPlugin *self,
Richard Hughes0eb123b2018-04-19 12:00:04 +0100955 const gchar *component_id,
956 const gchar *version)
957{
Richard Hughes12724852018-09-04 13:53:44 +0100958 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb01b4862018-04-20 16:39:48 +0100959 if (priv->runtime_versions == NULL)
960 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100961 g_hash_table_insert (priv->runtime_versions,
962 g_strdup (component_id),
963 g_strdup (version));
964}
965
Mario Limonciello1a680f32019-11-25 19:44:53 -0600966/**
967 * fu_plugin_set_compile_versions:
968 * @self: A #FuPlugin
969 * @compile_versions: A #GHashTables
970 *
971 * Sets the compile time versions for a plugin
972 *
973 * Since: 1.0.7
974 **/
Richard Hughes34e0dab2018-04-20 16:43:00 +0100975void
Richard Hughes12724852018-09-04 13:53:44 +0100976fu_plugin_set_compile_versions (FuPlugin *self, GHashTable *compile_versions)
Richard Hughes34e0dab2018-04-20 16:43:00 +0100977{
Richard Hughes12724852018-09-04 13:53:44 +0100978 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100979 priv->compile_versions = g_hash_table_ref (compile_versions);
980}
981
982/**
983 * fu_plugin_add_compile_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100984 * @self: A #FuPlugin
Richard Hughes34e0dab2018-04-20 16:43:00 +0100985 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
986 * @version: A version string, e.g. "1.2.3"
987 *
Richard Hughesdce91202019-04-08 12:47:45 +0100988 * Sets a compile-time version of a specific dependency.
Richard Hughes34e0dab2018-04-20 16:43:00 +0100989 *
990 * Since: 1.0.7
991 **/
992void
Richard Hughes12724852018-09-04 13:53:44 +0100993fu_plugin_add_compile_version (FuPlugin *self,
Richard Hughes34e0dab2018-04-20 16:43:00 +0100994 const gchar *component_id,
995 const gchar *version)
996{
Richard Hughes12724852018-09-04 13:53:44 +0100997 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100998 if (priv->compile_versions == NULL)
999 return;
1000 g_hash_table_insert (priv->compile_versions,
1001 g_strdup (component_id),
1002 g_strdup (version));
1003}
1004
Richard Hughes9c028f02017-10-28 21:14:28 +01001005/**
1006 * fu_plugin_lookup_quirk_by_id:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001007 * @self: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001008 * @group: A string, e.g. "DfuFlags"
1009 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +01001010 *
1011 * Looks up an entry in the hardware database using a string value.
1012 *
1013 * Returns: (transfer none): values from the database, or %NULL if not found
1014 *
1015 * Since: 1.0.1
1016 **/
1017const gchar *
Richard Hughes12724852018-09-04 13:53:44 +01001018fu_plugin_lookup_quirk_by_id (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +01001019{
Richard Hughes12724852018-09-04 13:53:44 +01001020 FuPluginPrivate *priv = GET_PRIVATE (self);
1021 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes9c028f02017-10-28 21:14:28 +01001022
Richard Hughes9c028f02017-10-28 21:14:28 +01001023 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001024 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +01001025}
1026
1027/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +01001028 * fu_plugin_lookup_quirk_by_id_as_uint64:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001029 * @self: A #FuPlugin
Richard Hughes8fe7cdd2018-08-23 10:02:44 +01001030 * @group: A string, e.g. "DfuFlags"
1031 * @key: An ID to match the entry, e.g. "Size"
1032 *
1033 * Looks up an entry in the hardware database using a string key, returning
1034 * an integer value. Values are assumed base 10, unless prefixed with "0x"
1035 * where they are parsed as base 16.
1036 *
Mario Limonciello1a680f32019-11-25 19:44:53 -06001037 * Returns: guint64 id or 0 if not found
Richard Hughes8fe7cdd2018-08-23 10:02:44 +01001038 *
1039 * Since: 1.1.2
1040 **/
1041guint64
Richard Hughes12724852018-09-04 13:53:44 +01001042fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes8fe7cdd2018-08-23 10:02:44 +01001043{
Richard Hughes12724852018-09-04 13:53:44 +01001044 return fu_common_strtoull (fu_plugin_lookup_quirk_by_id (self, group, key));
Richard Hughes8fe7cdd2018-08-23 10:02:44 +01001045}
1046
Mario Limonciello1a680f32019-11-25 19:44:53 -06001047/**
1048 * fu_plugin_set_smbios:
1049 * @self: A #FuPlugin
1050 * @smbios: A #FuSmbios
1051 *
1052 * Sets the smbios for a plugin
1053 *
1054 * Since: 1.0.0
1055 **/
Richard Hughes1354ea92017-09-19 15:58:31 +01001056void
Richard Hughes12724852018-09-04 13:53:44 +01001057fu_plugin_set_smbios (FuPlugin *self, FuSmbios *smbios)
Richard Hughes49e5e052017-09-03 12:15:41 +01001058{
Richard Hughes12724852018-09-04 13:53:44 +01001059 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +01001060 g_set_object (&priv->smbios, smbios);
1061}
1062
Richard Hughesb8f8db22017-04-25 15:56:00 +01001063/**
Richard Hughesb0829032017-01-10 09:27:08 +00001064 * fu_plugin_set_coldplug_delay:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001065 * @self: A #FuPlugin
Richard Hughesb0829032017-01-10 09:27:08 +00001066 * @duration: A delay in milliseconds
1067 *
Richard Hughes21eaeef2020-01-14 12:10:01 +00001068 * Set the minimum time that should be waited in-between the call to
Richard Hughesb0829032017-01-10 09:27:08 +00001069 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
Richard Hughes203ed842020-11-02 14:25:13 +00001070 * to be the minimum hardware initialization time from a datasheet.
Richard Hughesb0829032017-01-10 09:27:08 +00001071 *
1072 * It is better to use this function rather than using a sleep() in the plugin
1073 * itself as then only one delay is done in the daemon rather than waiting for
1074 * each coldplug prepare in a serial way.
1075 *
1076 * Additionally, very long delays should be avoided as the daemon will be
1077 * blocked from processing requests whilst the coldplug delay is being
1078 * performed.
1079 *
1080 * Since: 0.8.0
1081 **/
1082void
Richard Hughes12724852018-09-04 13:53:44 +01001083fu_plugin_set_coldplug_delay (FuPlugin *self, guint duration)
Richard Hughesb0829032017-01-10 09:27:08 +00001084{
Richard Hughes12724852018-09-04 13:53:44 +01001085 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesb0829032017-01-10 09:27:08 +00001086 g_return_if_fail (duration > 0);
1087
1088 /* check sanity */
1089 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
1090 g_warning ("duration of %ums is crazy, truncating to %ums",
1091 duration,
1092 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
1093 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
1094 }
1095
1096 /* emit */
Richard Hughes12724852018-09-04 13:53:44 +01001097 g_signal_emit (self, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
Richard Hughesb0829032017-01-10 09:27:08 +00001098}
1099
Richard Hughes4b303802019-10-04 13:22:51 +01001100static gboolean
1101fu_plugin_device_attach (FuPlugin *self, FuDevice *device, GError **error)
1102{
1103 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes4b303802019-10-04 13:22:51 +01001104 locker = fu_device_locker_new (device, error);
1105 if (locker == NULL)
1106 return FALSE;
1107 return fu_device_attach (device, error);
1108}
1109
1110static gboolean
1111fu_plugin_device_detach (FuPlugin *self, FuDevice *device, GError **error)
1112{
1113 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes4b303802019-10-04 13:22:51 +01001114 locker = fu_device_locker_new (device, error);
1115 if (locker == NULL)
1116 return FALSE;
1117 return fu_device_detach (device, error);
1118}
1119
1120static gboolean
Richard Hughes4b303802019-10-04 13:22:51 +01001121fu_plugin_device_activate (FuPlugin *self, FuDevice *device, GError **error)
1122{
1123 g_autoptr(FuDeviceLocker) locker = NULL;
1124 locker = fu_device_locker_new (device, error);
1125 if (locker == NULL)
1126 return FALSE;
1127 return fu_device_activate (device, error);
1128}
1129
1130static gboolean
1131fu_plugin_device_write_firmware (FuPlugin *self, FuDevice *device,
1132 GBytes *fw, FwupdInstallFlags flags,
1133 GError **error)
1134{
1135 g_autoptr(FuDeviceLocker) locker = NULL;
1136 locker = fu_device_locker_new (device, error);
1137 if (locker == NULL)
1138 return FALSE;
Richard Hughes1a612582020-09-29 19:39:38 +01001139
1140 /* back the old firmware up to /var/lib/fwupd */
1141 if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_BACKUP_BEFORE_INSTALL)) {
1142 g_autoptr(GBytes) fw_old = NULL;
1143 g_autofree gchar *path = NULL;
1144 g_autofree gchar *fn = NULL;
1145 g_autofree gchar *localstatedir = NULL;
1146
1147 fw_old = fu_device_dump_firmware (device, error);
1148 if (fw_old == NULL) {
1149 g_prefix_error (error, "failed to backup old firmware: ");
1150 return FALSE;
1151 }
1152 localstatedir = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
1153 fn = g_strdup_printf ("%s.bin", fu_device_get_version (device));
1154 path = g_build_filename (localstatedir,
1155 "backup",
1156 fu_device_get_id (device),
1157 fu_device_get_serial (device) != NULL ?
1158 fu_device_get_serial (device) :
1159 "default",
1160 fn, NULL);
1161 if (!fu_common_set_contents_bytes (path, fw_old, error))
1162 return FALSE;
1163 }
1164
Richard Hughes4b303802019-10-04 13:22:51 +01001165 return fu_device_write_firmware (device, fw, flags, error);
1166}
1167
Richard Hughes7f677212019-10-05 16:19:40 +01001168static gboolean
1169fu_plugin_device_read_firmware (FuPlugin *self, FuDevice *device, GError **error)
1170{
1171 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughesf0eb0912019-10-10 11:37:22 +01001172 g_autoptr(FuFirmware) firmware = NULL;
Richard Hughes7f677212019-10-05 16:19:40 +01001173 g_autoptr(GBytes) fw = NULL;
1174 GChecksumType checksum_types[] = {
1175 G_CHECKSUM_SHA1,
1176 G_CHECKSUM_SHA256,
1177 0 };
1178 locker = fu_device_locker_new (device, error);
1179 if (locker == NULL)
1180 return FALSE;
1181 if (!fu_device_detach (device, error))
1182 return FALSE;
Richard Hughesf0eb0912019-10-10 11:37:22 +01001183 firmware = fu_device_read_firmware (device, error);
1184 if (firmware == NULL) {
1185 g_autoptr(GError) error_local = NULL;
1186 if (!fu_device_attach (device, &error_local))
1187 g_debug ("ignoring attach failure: %s", error_local->message);
1188 g_prefix_error (error, "failed to read firmware: ");
1189 return FALSE;
1190 }
1191 fw = fu_firmware_write (firmware, error);
Richard Hughes7f677212019-10-05 16:19:40 +01001192 if (fw == NULL) {
1193 g_autoptr(GError) error_local = NULL;
1194 if (!fu_device_attach (device, &error_local))
Richard Hughesf0eb0912019-10-10 11:37:22 +01001195 g_debug ("ignoring attach failure: %s", error_local->message);
1196 g_prefix_error (error, "failed to write firmware: ");
Richard Hughes7f677212019-10-05 16:19:40 +01001197 return FALSE;
1198 }
1199 for (guint i = 0; checksum_types[i] != 0; i++) {
1200 g_autofree gchar *hash = NULL;
1201 hash = g_compute_checksum_for_bytes (checksum_types[i], fw);
1202 fu_device_add_checksum (device, hash);
1203 }
1204 return fu_device_attach (device, error);
1205}
1206
Mario Limonciello1a680f32019-11-25 19:44:53 -06001207/**
1208 * fu_plugin_runner_startup:
1209 * @self: a #FuPlugin
1210 * @error: a #GError or NULL
1211 *
1212 * Runs the startup routine for the plugin
1213 *
1214 * Returns: #TRUE for success, #FALSE for failure
1215 *
1216 * Since: 0.8.0
1217 **/
Richard Hughesd0905142016-03-13 09:46:49 +00001218gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001219fu_plugin_runner_startup (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001220{
Richard Hughes12724852018-09-04 13:53:44 +01001221 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001222 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001223 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +00001224
1225 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001226 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughesd0905142016-03-13 09:46:49 +00001227 return TRUE;
1228
Richard Hughes639da472018-01-06 22:35:04 +00001229 /* no object loaded */
1230 if (priv->module == NULL)
1231 return TRUE;
1232
Richard Hughesd0905142016-03-13 09:46:49 +00001233 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001234 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
1235 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001236 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001237 g_debug ("startup(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001238 if (!func (self, &error_local)) {
1239 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001240 g_critical ("unset plugin error in startup(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001241 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001242 g_set_error_literal (&error_local,
1243 FWUPD_ERROR,
1244 FWUPD_ERROR_INTERNAL,
1245 "unspecified error");
1246 }
1247 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1248 "failed to startup using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001249 fu_plugin_get_name (self));
Richard Hughescff38bc2016-12-12 12:03:37 +00001250 return FALSE;
1251 }
1252 return TRUE;
1253}
1254
1255static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001256fu_plugin_runner_device_generic (FuPlugin *self, FuDevice *device,
Richard Hughes4b303802019-10-04 13:22:51 +01001257 const gchar *symbol_name,
1258 FuPluginDeviceFunc device_func,
1259 GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001260{
Richard Hughes12724852018-09-04 13:53:44 +01001261 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001262 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001263 g_autoptr(GError) error_local = NULL;
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001264
1265 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001266 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001267 return TRUE;
1268
Richard Hughesd3d96cc2017-11-14 11:34:33 +00001269 /* no object loaded */
1270 if (priv->module == NULL)
1271 return TRUE;
1272
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001273 /* optional */
1274 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
Richard Hughes4b303802019-10-04 13:22:51 +01001275 if (func == NULL) {
1276 if (device_func != NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001277 g_debug ("running superclassed %s(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001278 symbol_name + 10, fu_plugin_get_name (self));
Richard Hughes4b303802019-10-04 13:22:51 +01001279 return device_func (self, device, error);
1280 }
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001281 return TRUE;
Richard Hughes4b303802019-10-04 13:22:51 +01001282 }
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001283 g_debug ("%s(%s)", symbol_name + 10, fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001284 if (!func (self, device, &error_local)) {
1285 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001286 g_critical ("unset plugin error in %s(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001287 fu_plugin_get_name (self), symbol_name + 10);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001288 g_set_error_literal (&error_local,
1289 FWUPD_ERROR,
1290 FWUPD_ERROR_INTERNAL,
1291 "unspecified error");
1292 }
1293 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1294 "failed to %s using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001295 symbol_name + 10, fu_plugin_get_name (self));
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001296 return FALSE;
1297 }
1298 return TRUE;
1299}
1300
Richard Hughesdbd8c762018-06-15 20:31:40 +01001301static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001302fu_plugin_runner_flagged_device_generic (FuPlugin *self, FwupdInstallFlags flags,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001303 FuDevice *device,
1304 const gchar *symbol_name, GError **error)
1305{
Richard Hughes12724852018-09-04 13:53:44 +01001306 FuPluginPrivate *priv = GET_PRIVATE (self);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001307 FuPluginFlaggedDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001308 g_autoptr(GError) error_local = NULL;
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001309
1310 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001311 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001312 return TRUE;
1313
1314 /* no object loaded */
1315 if (priv->module == NULL)
1316 return TRUE;
1317
1318 /* optional */
1319 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1320 if (func == NULL)
1321 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001322 g_debug ("%s(%s)", symbol_name + 10, fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001323 if (!func (self, flags, device, &error_local)) {
1324 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001325 g_critical ("unset plugin error in %s(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001326 fu_plugin_get_name (self), symbol_name + 10);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001327 g_set_error_literal (&error_local,
1328 FWUPD_ERROR,
1329 FWUPD_ERROR_INTERNAL,
1330 "unspecified error");
1331 }
1332 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1333 "failed to %s using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001334 symbol_name + 10, fu_plugin_get_name (self));
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001335 return FALSE;
1336 }
1337 return TRUE;
1338
1339}
1340
1341static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001342fu_plugin_runner_device_array_generic (FuPlugin *self, GPtrArray *devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001343 const gchar *symbol_name, GError **error)
1344{
Richard Hughes12724852018-09-04 13:53:44 +01001345 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesdbd8c762018-06-15 20:31:40 +01001346 FuPluginDeviceArrayFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001347 g_autoptr(GError) error_local = NULL;
Richard Hughesdbd8c762018-06-15 20:31:40 +01001348
1349 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001350 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughesdbd8c762018-06-15 20:31:40 +01001351 return TRUE;
1352
1353 /* no object loaded */
1354 if (priv->module == NULL)
1355 return TRUE;
1356
1357 /* optional */
1358 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1359 if (func == NULL)
1360 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001361 g_debug ("%s(%s)", symbol_name + 10, fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001362 if (!func (self, devices, &error_local)) {
1363 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001364 g_critical ("unset plugin error in for %s(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001365 fu_plugin_get_name (self), symbol_name + 10);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001366 g_set_error_literal (&error_local,
1367 FWUPD_ERROR,
1368 FWUPD_ERROR_INTERNAL,
1369 "unspecified error");
1370 }
1371 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1372 "failed to %s using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001373 symbol_name + 10, fu_plugin_get_name (self));
Richard Hughesdbd8c762018-06-15 20:31:40 +01001374 return FALSE;
1375 }
1376 return TRUE;
1377}
1378
Mario Limonciello1a680f32019-11-25 19:44:53 -06001379/**
1380 * fu_plugin_runner_coldplug:
1381 * @self: a #FuPlugin
1382 * @error: a #GError or NULL
1383 *
1384 * Runs the coldplug routine for the plugin
1385 *
1386 * Returns: #TRUE for success, #FALSE for failure
1387 *
1388 * Since: 0.8.0
1389 **/
Richard Hughesd0905142016-03-13 09:46:49 +00001390gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001391fu_plugin_runner_coldplug (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001392{
Richard Hughes12724852018-09-04 13:53:44 +01001393 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001394 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001395 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +00001396
1397 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001398 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughesd0905142016-03-13 09:46:49 +00001399 return TRUE;
1400
Richard Hughes639da472018-01-06 22:35:04 +00001401 /* no object loaded */
1402 if (priv->module == NULL)
1403 return TRUE;
1404
Richard Hughesd0905142016-03-13 09:46:49 +00001405 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001406 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
1407 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001408 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001409 g_debug ("coldplug(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001410 if (!func (self, &error_local)) {
1411 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001412 g_critical ("unset plugin error in coldplug(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001413 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001414 g_set_error_literal (&error_local,
1415 FWUPD_ERROR,
1416 FWUPD_ERROR_INTERNAL,
1417 "unspecified error");
1418 }
1419 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001420 "failed to coldplug using %s: ", fu_plugin_get_name (self));
Richard Hughescff38bc2016-12-12 12:03:37 +00001421 return FALSE;
1422 }
1423 return TRUE;
1424}
1425
Mario Limonciello1a680f32019-11-25 19:44:53 -06001426/**
1427 * fu_plugin_runner_recoldplug:
1428 * @self: a #FuPlugin
1429 * @error: a #GError or NULL
1430 *
1431 * Runs the recoldplug routine for the plugin
1432 *
1433 * Returns: #TRUE for success, #FALSE for failure
1434 *
1435 * Since: 1.0.4
1436 **/
Richard Hughes7b8b2022016-12-12 16:15:03 +00001437gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001438fu_plugin_runner_recoldplug (FuPlugin *self, GError **error)
Richard Hughes2de8f132018-01-17 09:12:02 +00001439{
Richard Hughes12724852018-09-04 13:53:44 +01001440 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes2de8f132018-01-17 09:12:02 +00001441 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001442 g_autoptr(GError) error_local = NULL;
Richard Hughes2de8f132018-01-17 09:12:02 +00001443
1444 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001445 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes2de8f132018-01-17 09:12:02 +00001446 return TRUE;
1447
1448 /* no object loaded */
1449 if (priv->module == NULL)
1450 return TRUE;
1451
1452 /* optional */
1453 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1454 if (func == NULL)
1455 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001456 g_debug ("recoldplug(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001457 if (!func (self, &error_local)) {
1458 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001459 g_critical ("unset plugin error in recoldplug(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001460 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001461 g_set_error_literal (&error_local,
1462 FWUPD_ERROR,
1463 FWUPD_ERROR_INTERNAL,
1464 "unspecified error");
1465 }
1466 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1467 "failed to recoldplug using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001468 fu_plugin_get_name (self));
Richard Hughes2de8f132018-01-17 09:12:02 +00001469 return FALSE;
1470 }
1471 return TRUE;
1472}
1473
Mario Limonciello1a680f32019-11-25 19:44:53 -06001474/**
1475 * fu_plugin_runner_coldplug_prepare:
1476 * @self: a #FuPlugin
1477 * @error: a #GError or NULL
1478 *
1479 * Runs the coldplug_prepare routine for the plugin
1480 *
1481 * Returns: #TRUE for success, #FALSE for failure
1482 *
1483 * Since: 0.8.0
1484 **/
Richard Hughes2de8f132018-01-17 09:12:02 +00001485gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001486fu_plugin_runner_coldplug_prepare (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001487{
Richard Hughes12724852018-09-04 13:53:44 +01001488 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001489 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001490 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001491
1492 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001493 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes46487c92017-01-07 21:26:34 +00001494 return TRUE;
1495
Richard Hughes639da472018-01-06 22:35:04 +00001496 /* no object loaded */
1497 if (priv->module == NULL)
1498 return TRUE;
1499
Richard Hughes46487c92017-01-07 21:26:34 +00001500 /* optional */
1501 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1502 if (func == NULL)
1503 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001504 g_debug ("coldplug_prepare(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001505 if (!func (self, &error_local)) {
1506 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001507 g_critical ("unset plugin error in coldplug_prepare(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001508 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001509 g_set_error_literal (&error_local,
1510 FWUPD_ERROR,
1511 FWUPD_ERROR_INTERNAL,
1512 "unspecified error");
1513 }
1514 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1515 "failed to coldplug_prepare using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001516 fu_plugin_get_name (self));
Richard Hughes46487c92017-01-07 21:26:34 +00001517 return FALSE;
1518 }
1519 return TRUE;
1520}
1521
Mario Limonciello1a680f32019-11-25 19:44:53 -06001522/**
1523 * fu_plugin_runner_coldplug_cleanup:
1524 * @self: a #FuPlugin
1525 * @error: a #GError or NULL
1526 *
1527 * Runs the coldplug_cleanup routine for the plugin
1528 *
1529 * Returns: #TRUE for success, #FALSE for failure
1530 *
1531 * Since: 0.8.0
1532 **/
Richard Hughes46487c92017-01-07 21:26:34 +00001533gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001534fu_plugin_runner_coldplug_cleanup (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001535{
Richard Hughes12724852018-09-04 13:53:44 +01001536 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001537 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001538 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001539
1540 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001541 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes46487c92017-01-07 21:26:34 +00001542 return TRUE;
1543
Richard Hughes639da472018-01-06 22:35:04 +00001544 /* no object loaded */
1545 if (priv->module == NULL)
1546 return TRUE;
1547
Richard Hughes46487c92017-01-07 21:26:34 +00001548 /* optional */
1549 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1550 if (func == NULL)
1551 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001552 g_debug ("coldplug_cleanup(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001553 if (!func (self, &error_local)) {
1554 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001555 g_critical ("unset plugin error in coldplug_cleanup(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001556 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001557 g_set_error_literal (&error_local,
1558 FWUPD_ERROR,
1559 FWUPD_ERROR_INTERNAL,
1560 "unspecified error");
1561 }
1562 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1563 "failed to coldplug_cleanup using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001564 fu_plugin_get_name (self));
Richard Hughes46487c92017-01-07 21:26:34 +00001565 return FALSE;
1566 }
1567 return TRUE;
1568}
1569
Mario Limonciello1a680f32019-11-25 19:44:53 -06001570/**
1571 * fu_plugin_runner_composite_prepare:
1572 * @self: a #FuPlugin
Richard Hughesa0d81c72019-11-27 11:41:54 +00001573 * @devices: (element-type FuDevice): a #GPtrArray of devices
Mario Limonciello1a680f32019-11-25 19:44:53 -06001574 * @error: a #GError or NULL
1575 *
1576 * Runs the composite_prepare routine for the plugin
1577 *
1578 * Returns: #TRUE for success, #FALSE for failure
1579 *
1580 * Since: 1.0.9
1581 **/
Richard Hughes46487c92017-01-07 21:26:34 +00001582gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001583fu_plugin_runner_composite_prepare (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001584{
Richard Hughes12724852018-09-04 13:53:44 +01001585 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001586 "fu_plugin_composite_prepare",
1587 error);
1588}
1589
Mario Limonciello1a680f32019-11-25 19:44:53 -06001590/**
1591 * fu_plugin_runner_composite_cleanup:
1592 * @self: a #FuPlugin
Richard Hughesa0d81c72019-11-27 11:41:54 +00001593 * @devices: (element-type FuDevice): a #GPtrArray of devices
Mario Limonciello1a680f32019-11-25 19:44:53 -06001594 * @error: a #GError or NULL
1595 *
1596 * Runs the composite_cleanup routine for the plugin
1597 *
1598 * Returns: #TRUE for success, #FALSE for failure
1599 *
1600 * Since: 1.0.9
1601 **/
Richard Hughesdbd8c762018-06-15 20:31:40 +01001602gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001603fu_plugin_runner_composite_cleanup (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001604{
Richard Hughes12724852018-09-04 13:53:44 +01001605 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001606 "fu_plugin_composite_cleanup",
1607 error);
1608}
1609
Mario Limonciello1a680f32019-11-25 19:44:53 -06001610/**
1611 * fu_plugin_runner_update_prepare:
1612 * @self: a #FuPlugin
Richard Hughes2bbb7d22020-11-30 09:18:45 +00001613 * @flags: #FwupdInstallFlags
1614 * @device: a #FuDevice
Mario Limonciello1a680f32019-11-25 19:44:53 -06001615 * @error: a #GError or NULL
1616 *
1617 * Runs the update_prepare routine for the plugin
1618 *
1619 * Returns: #TRUE for success, #FALSE for failure
1620 *
1621 * Since: 1.1.2
1622 **/
Richard Hughesdbd8c762018-06-15 20:31:40 +01001623gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001624fu_plugin_runner_update_prepare (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001625 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001626{
Richard Hughes12724852018-09-04 13:53:44 +01001627 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001628 "fu_plugin_update_prepare",
1629 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001630}
1631
Mario Limonciello1a680f32019-11-25 19:44:53 -06001632/**
1633 * fu_plugin_runner_update_cleanup:
1634 * @self: a #FuPlugin
Richard Hughes2bbb7d22020-11-30 09:18:45 +00001635 * @flags: #FwupdInstallFlags
1636 * @device: a #FuDevice
Mario Limonciello1a680f32019-11-25 19:44:53 -06001637 * @error: a #GError or NULL
1638 *
1639 * Runs the update_cleanup routine for the plugin
1640 *
1641 * Returns: #TRUE for success, #FALSE for failure
1642 *
1643 * Since: 1.1.2
1644 **/
Richard Hughes7b8b2022016-12-12 16:15:03 +00001645gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001646fu_plugin_runner_update_cleanup (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001647 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001648{
Richard Hughes12724852018-09-04 13:53:44 +01001649 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001650 "fu_plugin_update_cleanup",
1651 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001652}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001653
Mario Limonciello1a680f32019-11-25 19:44:53 -06001654/**
1655 * fu_plugin_runner_update_attach:
1656 * @self: a #FuPlugin
1657 * @device: a #FuDevice
1658 * @error: a #GError or NULL
1659 *
1660 * Runs the update_attach routine for the plugin
1661 *
1662 * Returns: #TRUE for success, #FALSE for failure
1663 *
1664 * Since: 1.1.2
1665 **/
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001666gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001667fu_plugin_runner_update_attach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001668{
Richard Hughes12724852018-09-04 13:53:44 +01001669 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001670 "fu_plugin_update_attach",
1671 fu_plugin_device_attach,
1672 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001673}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001674
Mario Limonciello1a680f32019-11-25 19:44:53 -06001675/**
1676 * fu_plugin_runner_update_detach:
1677 * @self: a #FuPlugin
1678 * @device: A #FuDevice
1679 * @error: a #GError or NULL
1680 *
1681 * Runs the update_detach routine for the plugin
1682 *
1683 * Returns: #TRUE for success, #FALSE for failure
1684 *
1685 * Since: 1.1.2
1686 **/
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001687gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001688fu_plugin_runner_update_detach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001689{
Richard Hughes12724852018-09-04 13:53:44 +01001690 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001691 "fu_plugin_update_detach",
1692 fu_plugin_device_detach,
1693 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001694}
1695
Mario Limonciello1a680f32019-11-25 19:44:53 -06001696/**
1697 * fu_plugin_runner_update_reload:
1698 * @self: a #FuPlugin
Richard Hughes2bbb7d22020-11-30 09:18:45 +00001699 * @device: A #FuDevice
Mario Limonciello1a680f32019-11-25 19:44:53 -06001700 * @error: a #GError or NULL
1701 *
1702 * Runs reload routine for a device
1703 *
1704 * Returns: #TRUE for success, #FALSE for failure
1705 *
1706 * Since: 1.1.2
1707 **/
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001708gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001709fu_plugin_runner_update_reload (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001710{
Richard Hughes42f33df2019-10-05 20:52:33 +01001711 g_autoptr(FuDeviceLocker) locker = NULL;
1712
1713 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001714 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes42f33df2019-10-05 20:52:33 +01001715 return TRUE;
1716
1717 /* no object loaded */
1718 locker = fu_device_locker_new (device, error);
1719 if (locker == NULL)
1720 return FALSE;
1721 return fu_device_reload (device, error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001722}
1723
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001724/**
Richard Hughes196c6c62020-05-11 19:42:47 +01001725 * fu_plugin_runner_add_security_attrs:
1726 * @self: a #FuPlugin
Richard Hughes3ecd22c2020-05-19 20:13:47 +01001727 * @attrs: a #FuSecurityAttrs
Richard Hughes196c6c62020-05-11 19:42:47 +01001728 *
Richard Hughes3ecd22c2020-05-19 20:13:47 +01001729 * Runs the `add_security_attrs()` routine for the plugin
Richard Hughes196c6c62020-05-11 19:42:47 +01001730 *
1731 * Since: 1.5.0
1732 **/
Richard Hughesf58ac732020-05-12 15:23:44 +01001733void
1734fu_plugin_runner_add_security_attrs (FuPlugin *self, FuSecurityAttrs *attrs)
Richard Hughes196c6c62020-05-11 19:42:47 +01001735{
Richard Hughesf58ac732020-05-12 15:23:44 +01001736 FuPluginPrivate *priv = GET_PRIVATE (self);
1737 FuPluginSecurityAttrsFunc func = NULL;
1738 const gchar *symbol_name = "fu_plugin_add_security_attrs";
1739
1740 /* no object loaded */
1741 if (priv->module == NULL)
1742 return;
1743
1744 /* optional, but gets called even for disabled plugins */
1745 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1746 if (func == NULL)
1747 return;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001748 g_debug ("%s(%s)", symbol_name + 10, fu_plugin_get_name (self));
Richard Hughesf58ac732020-05-12 15:23:44 +01001749 func (self, attrs);
Richard Hughes196c6c62020-05-11 19:42:47 +01001750}
1751
1752/**
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001753 * fu_plugin_add_udev_subsystem:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001754 * @self: a #FuPlugin
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001755 * @subsystem: a subsystem name, e.g. `pciport`
1756 *
1757 * Registers the udev subsystem to be watched by the daemon.
1758 *
1759 * Plugins can use this method only in fu_plugin_init()
Mario Limonciello1a680f32019-11-25 19:44:53 -06001760 *
1761 * Since: 1.1.2
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001762 **/
1763void
Richard Hughes12724852018-09-04 13:53:44 +01001764fu_plugin_add_udev_subsystem (FuPlugin *self, const gchar *subsystem)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001765{
Richard Hughes12724852018-09-04 13:53:44 +01001766 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesea327fc2020-06-22 15:23:29 +01001767 if (priv->udev_subsystems == NULL)
1768 priv->udev_subsystems = g_ptr_array_new_with_free_func (g_free);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001769 for (guint i = 0; i < priv->udev_subsystems->len; i++) {
1770 const gchar *subsystem_tmp = g_ptr_array_index (priv->udev_subsystems, i);
1771 if (g_strcmp0 (subsystem_tmp, subsystem) == 0)
1772 return;
1773 }
1774 g_debug ("added udev subsystem watch of %s", subsystem);
1775 g_ptr_array_add (priv->udev_subsystems, g_strdup (subsystem));
1776}
1777
Richard Hughes989acf12019-10-05 20:16:47 +01001778/**
1779 * fu_plugin_set_device_gtype:
1780 * @self: a #FuPlugin
1781 * @device_gtype: a #GType `FU_TYPE_DEVICE`
1782 *
1783 * Sets the device #GType which is used when creating devices.
1784 *
1785 * If this method is used then fu_plugin_usb_device_added() is not called, and
1786 * instead the object is created in the daemon for the plugin.
1787 *
1788 * Plugins can use this method only in fu_plugin_init()
1789 *
1790 * Since: 1.3.3
1791 **/
1792void
1793fu_plugin_set_device_gtype (FuPlugin *self, GType device_gtype)
1794{
1795 FuPluginPrivate *priv = GET_PRIVATE (self);
1796 priv->device_gtype = device_gtype;
1797}
1798
Richard Hughes9f71fe32021-01-03 20:35:36 +00001799static gchar *
1800fu_common_string_uncamelcase (const gchar *str)
1801{
1802 GString *tmp = g_string_new (NULL);
1803 for (guint i = 0; str[i] != '\0'; i++) {
1804 if (g_ascii_islower (str[i]) ||
1805 g_ascii_isdigit (str[i])) {
1806 g_string_append_c (tmp, str[i]);
1807 continue;
1808 }
1809 if (i > 0)
1810 g_string_append_c (tmp, '-');
1811 g_string_append_c (tmp, g_ascii_tolower (str[i]));
1812 }
1813 return g_string_free (tmp, FALSE);
1814}
1815
Mario Limonciello1a680f32019-11-25 19:44:53 -06001816/**
1817 * fu_plugin_add_firmware_gtype:
1818 * @self: a #FuPlugin
Richard Hughes9f71fe32021-01-03 20:35:36 +00001819 * @id: (nullable): An optional string describing the type, e.g. "ihex"
1820 * @gtype: a #GType e.g. `FU_TYPE_FOO_FIRMWARE`
Mario Limonciello1a680f32019-11-25 19:44:53 -06001821 *
Richard Hughes9f71fe32021-01-03 20:35:36 +00001822 * Adds a firmware #GType which is used when creating devices. If @id is not
1823 * specified then it is guessed using the #GType name.
1824 *
Mario Limonciello1a680f32019-11-25 19:44:53 -06001825 * Plugins can use this method only in fu_plugin_init()
1826 *
1827 * Since: 1.3.3
1828 **/
Richard Hughes95c98a92019-10-22 16:03:15 +01001829void
1830fu_plugin_add_firmware_gtype (FuPlugin *self, const gchar *id, GType gtype)
1831{
Richard Hughes9f71fe32021-01-03 20:35:36 +00001832 g_autofree gchar *id_safe = NULL;
1833 if (id != NULL) {
1834 id_safe = g_strdup (id);
1835 } else {
Richard Hughesebb10a52021-01-05 13:34:39 +00001836 g_autoptr(GString) str = g_string_new (g_type_name (gtype));
Richard Hughes9f71fe32021-01-03 20:35:36 +00001837 if (g_str_has_prefix (str->str, "Fu"))
1838 g_string_erase (str, 0, 2);
1839 fu_common_string_replace (str, "Firmware", "");
1840 id_safe = fu_common_string_uncamelcase (str->str);
1841 }
1842 g_signal_emit (self, signals[SIGNAL_ADD_FIRMWARE_GTYPE], 0, id_safe, gtype);
Richard Hughes95c98a92019-10-22 16:03:15 +01001843}
1844
Richard Hughes989acf12019-10-05 20:16:47 +01001845static gboolean
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001846fu_plugin_check_supported_device (FuPlugin *self, FuDevice *device)
1847{
1848 GPtrArray *instance_ids = fu_device_get_instance_ids (device);
1849 for (guint i = 0; i < instance_ids->len; i++) {
1850 const gchar *instance_id = g_ptr_array_index (instance_ids, i);
1851 g_autofree gchar *guid = fwupd_guid_hash_string (instance_id);
1852 if (fu_plugin_check_supported (self, guid))
1853 return TRUE;
1854 }
1855 return FALSE;
1856}
1857
1858static gboolean
Richard Hughes989acf12019-10-05 20:16:47 +01001859fu_plugin_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
1860{
1861 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001862 GType device_gtype = fu_device_get_specialized_gtype (FU_DEVICE (device));
Richard Hughes989acf12019-10-05 20:16:47 +01001863 g_autoptr(FuDevice) dev = NULL;
1864 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001865
1866 /* fall back to plugin default */
1867 if (device_gtype == G_TYPE_INVALID)
1868 device_gtype = priv->device_gtype;
1869
1870 /* create new device and incorporate existing properties */
1871 dev = g_object_new (device_gtype, NULL);
1872 fu_device_incorporate (dev, FU_DEVICE (device));
Richard Hughes0f66a022020-02-19 18:54:38 +00001873 if (!fu_plugin_runner_device_created (self, dev, error))
1874 return FALSE;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001875
1876 /* there are a lot of different devices that match, but not all respond
1877 * well to opening -- so limit some ones with issued updates */
Richard Hughescf100292021-01-04 10:36:37 +00001878 if (fu_device_has_internal_flag (dev, FU_DEVICE_INTERNAL_FLAG_ONLY_SUPPORTED)) {
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001879 if (!fu_device_probe (dev, error))
1880 return FALSE;
1881 fu_device_convert_instance_ids (dev);
1882 if (!fu_plugin_check_supported_device (self, dev)) {
1883 g_autofree gchar *guids = fu_device_get_guids_as_str (dev);
1884 g_debug ("%s has no updates, so ignoring device", guids);
1885 return TRUE;
1886 }
1887 }
1888
1889 /* open and add */
1890 locker = fu_device_locker_new (dev, error);
1891 if (locker == NULL)
1892 return FALSE;
1893 fu_plugin_device_add (self, dev);
Richard Hughes6a078702020-05-09 20:36:33 +01001894 fu_plugin_runner_device_added (self, dev);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001895 return TRUE;
1896}
1897
1898static gboolean
1899fu_plugin_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
1900{
1901 FuPluginPrivate *priv = GET_PRIVATE (self);
1902 GType device_gtype = fu_device_get_specialized_gtype (FU_DEVICE (device));
1903 g_autoptr(FuDevice) dev = NULL;
1904 g_autoptr(FuDeviceLocker) locker = NULL;
1905
1906 /* fall back to plugin default */
1907 if (device_gtype == G_TYPE_INVALID)
1908 device_gtype = priv->device_gtype;
1909
1910 /* create new device and incorporate existing properties */
1911 dev = g_object_new (device_gtype, NULL);
Richard Hughes989acf12019-10-05 20:16:47 +01001912 fu_device_incorporate (FU_DEVICE (dev), FU_DEVICE (device));
Richard Hughes0f66a022020-02-19 18:54:38 +00001913 if (!fu_plugin_runner_device_created (self, dev, error))
1914 return FALSE;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001915
1916 /* there are a lot of different devices that match, but not all respond
1917 * well to opening -- so limit some ones with issued updates */
Richard Hughescf100292021-01-04 10:36:37 +00001918 if (fu_device_has_internal_flag (dev, FU_DEVICE_INTERNAL_FLAG_ONLY_SUPPORTED)) {
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001919 if (!fu_device_probe (dev, error))
1920 return FALSE;
1921 fu_device_convert_instance_ids (dev);
1922 if (!fu_plugin_check_supported_device (self, dev)) {
1923 g_autofree gchar *guids = fu_device_get_guids_as_str (dev);
1924 g_debug ("%s has no updates, so ignoring device", guids);
1925 return TRUE;
1926 }
1927 }
1928
1929 /* open and add */
Richard Hughes989acf12019-10-05 20:16:47 +01001930 locker = fu_device_locker_new (dev, error);
1931 if (locker == NULL)
1932 return FALSE;
1933 fu_plugin_device_add (self, FU_DEVICE (dev));
Richard Hughes6a078702020-05-09 20:36:33 +01001934 fu_plugin_runner_device_added (self, dev);
Richard Hughes989acf12019-10-05 20:16:47 +01001935 return TRUE;
1936}
1937
Mario Limonciello1a680f32019-11-25 19:44:53 -06001938/**
1939 * fu_plugin_runner_usb_device_added:
1940 * @self: a #FuPlugin
1941 * @device: a #FuUsbDevice
1942 * @error: a #GError or NULL
1943 *
1944 * Call the usb_device_added routine for the plugin
1945 *
1946 * Returns: #TRUE for success, #FALSE for failure
1947 *
1948 * Since: 1.0.2
1949 **/
Richard Hughes104f6512017-11-24 11:44:57 +00001950gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001951fu_plugin_runner_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
Richard Hughes104f6512017-11-24 11:44:57 +00001952{
Richard Hughes12724852018-09-04 13:53:44 +01001953 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes104f6512017-11-24 11:44:57 +00001954 FuPluginUsbDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001955 g_autoptr(GError) error_local = NULL;
Richard Hughes104f6512017-11-24 11:44:57 +00001956
Richard Hughes6a489a92020-12-22 10:32:06 +00001957 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
1958 g_return_val_if_fail (FU_IS_USB_DEVICE (device), FALSE);
1959 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1960
Richard Hughes104f6512017-11-24 11:44:57 +00001961 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001962 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes104f6512017-11-24 11:44:57 +00001963 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001964
1965 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001966 if (priv->module == NULL)
1967 return TRUE;
1968
1969 /* optional */
1970 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
Richard Hughes989acf12019-10-05 20:16:47 +01001971 if (func == NULL) {
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001972 if (priv->device_gtype != G_TYPE_INVALID ||
1973 fu_device_get_specialized_gtype (FU_DEVICE (device)) != G_TYPE_INVALID) {
Mario Limonciello0e500322019-10-17 18:41:04 -05001974 if (!fu_plugin_usb_device_added (self, device, error))
Mario Limoncielloa9be5362019-10-12 13:17:45 -05001975 return FALSE;
Richard Hughes989acf12019-10-05 20:16:47 +01001976 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001977 return TRUE;
Richard Hughes989acf12019-10-05 20:16:47 +01001978 }
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001979 g_debug ("usb_device_added(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001980 if (!func (self, device, &error_local)) {
1981 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001982 g_critical ("unset plugin error in usb_device_added(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001983 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001984 g_set_error_literal (&error_local,
1985 FWUPD_ERROR,
1986 FWUPD_ERROR_INTERNAL,
1987 "unspecified error");
1988 }
1989 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1990 "failed to add device using on %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001991 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001992 return FALSE;
Richard Hughes104f6512017-11-24 11:44:57 +00001993 }
1994 return TRUE;
1995}
1996
Mario Limonciello1a680f32019-11-25 19:44:53 -06001997/**
1998 * fu_plugin_runner_udev_device_added:
1999 * @self: a #FuPlugin
2000 * @device: a #FuUdevDevice
2001 * @error: a #GError or NULL
2002 *
2003 * Call the udev_device_added routine for the plugin
2004 *
2005 * Returns: #TRUE for success, #FALSE for failure
2006 *
2007 * Since: 1.0.2
2008 **/
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002009gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002010fu_plugin_runner_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002011{
Richard Hughes12724852018-09-04 13:53:44 +01002012 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002013 FuPluginUdevDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002014 g_autoptr(GError) error_local = NULL;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002015
Richard Hughes6a489a92020-12-22 10:32:06 +00002016 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
2017 g_return_val_if_fail (FU_IS_UDEV_DEVICE (device), FALSE);
2018 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2019
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002020 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002021 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002022 return TRUE;
2023
2024 /* no object loaded */
2025 if (priv->module == NULL)
2026 return TRUE;
2027
2028 /* optional */
2029 g_module_symbol (priv->module, "fu_plugin_udev_device_added", (gpointer *) &func);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01002030 if (func == NULL) {
2031 if (priv->device_gtype != G_TYPE_INVALID ||
2032 fu_device_get_specialized_gtype (FU_DEVICE (device)) != G_TYPE_INVALID) {
Richard Hughes6fca4692020-12-01 18:22:46 +00002033 return fu_plugin_udev_device_added (self, device, error);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01002034 }
Richard Hughesa21e0252020-12-01 12:21:33 +00002035 g_set_error_literal (error,
2036 FWUPD_ERROR,
2037 FWUPD_ERROR_INTERNAL,
2038 "No device GType set");
2039 return FALSE;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01002040 }
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002041 g_debug ("udev_device_added(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002042 if (!func (self, device, &error_local)) {
2043 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05002044 g_critical ("unset plugin error in udev_device_added(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002045 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002046 g_set_error_literal (&error_local,
2047 FWUPD_ERROR,
2048 FWUPD_ERROR_INTERNAL,
2049 "unspecified error");
2050 }
2051 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
2052 "failed to add device using on %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002053 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002054 return FALSE;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002055 }
2056 return TRUE;
2057}
2058
Mario Limonciello1a680f32019-11-25 19:44:53 -06002059/**
2060 * fu_plugin_runner_udev_device_changed:
2061 * @self: a #FuPlugin
2062 * @device: a #FuUdevDevice
2063 * @error: a #GError or NULL
2064 *
2065 * Call the udev_device_changed routine for the plugin
2066 *
2067 * Returns: #TRUE for success, #FALSE for failure
2068 *
2069 * Since: 1.0.2
2070 **/
Richard Hughes5e952ce2019-08-26 11:09:46 +01002071gboolean
2072fu_plugin_runner_udev_device_changed (FuPlugin *self, FuUdevDevice *device, GError **error)
2073{
2074 FuPluginPrivate *priv = GET_PRIVATE (self);
2075 FuPluginUdevDeviceAddedFunc func = NULL;
2076 g_autoptr(GError) error_local = NULL;
2077
Richard Hughes6a489a92020-12-22 10:32:06 +00002078 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
2079 g_return_val_if_fail (FU_IS_UDEV_DEVICE (device), FALSE);
2080 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2081
Richard Hughes5e952ce2019-08-26 11:09:46 +01002082 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002083 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes5e952ce2019-08-26 11:09:46 +01002084 return TRUE;
2085
2086 /* no object loaded */
2087 if (priv->module == NULL)
2088 return TRUE;
2089
2090 /* optional */
2091 g_module_symbol (priv->module, "fu_plugin_udev_device_changed", (gpointer *) &func);
Mario Limonciello6d235142020-09-10 21:35:17 -05002092 if (func == NULL)
Richard Hughes5e952ce2019-08-26 11:09:46 +01002093 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002094 g_debug ("udev_device_changed(%s)", fu_plugin_get_name (self));
Richard Hughes5e952ce2019-08-26 11:09:46 +01002095 if (!func (self, device, &error_local)) {
2096 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05002097 g_critical ("unset plugin error in udev_device_changed(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002098 fu_plugin_get_name (self));
Richard Hughes5e952ce2019-08-26 11:09:46 +01002099 g_set_error_literal (&error_local,
2100 FWUPD_ERROR,
2101 FWUPD_ERROR_INTERNAL,
2102 "unspecified error");
2103 }
2104 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
2105 "failed to change device on %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002106 fu_plugin_get_name (self));
Richard Hughes5e952ce2019-08-26 11:09:46 +01002107 return FALSE;
2108 }
2109 return TRUE;
2110}
2111
Mario Limonciello1a680f32019-11-25 19:44:53 -06002112/**
Richard Hughes6a078702020-05-09 20:36:33 +01002113 * fu_plugin_runner_device_added:
2114 * @self: a #FuPlugin
2115 * @device: a #FuDevice
2116 *
2117 * Call the device_added routine for the plugin
2118 *
2119 * Since: 1.5.0
2120 **/
2121void
2122fu_plugin_runner_device_added (FuPlugin *self, FuDevice *device)
2123{
2124 FuPluginPrivate *priv = GET_PRIVATE (self);
2125 FuPluginDeviceRegisterFunc func = NULL;
2126
2127 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002128 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes6a078702020-05-09 20:36:33 +01002129 return;
2130 if (priv->module == NULL)
2131 return;
2132
2133 /* optional */
2134 g_module_symbol (priv->module, "fu_plugin_device_added", (gpointer *) &func);
2135 if (func == NULL)
2136 return;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002137 g_debug ("fu_plugin_device_added(%s)", fu_plugin_get_name (self));
Richard Hughes6a078702020-05-09 20:36:33 +01002138 func (self, device);
2139}
2140
2141/**
Mario Limonciello1a680f32019-11-25 19:44:53 -06002142 * fu_plugin_runner_device_removed:
2143 * @self: a #FuPlugin
2144 * @device: a #FuDevice
2145 *
2146 * Call the device_removed routine for the plugin
2147 *
2148 * Since: 1.1.2
2149 **/
Richard Hughese1fd34d2017-08-24 14:19:51 +01002150void
Richard Hughes12724852018-09-04 13:53:44 +01002151fu_plugin_runner_device_removed (FuPlugin *self, FuDevice *device)
Mario Limoncielloe260ead2018-09-01 09:19:24 -05002152{
2153 g_autoptr(GError) error_local= NULL;
2154
Richard Hughes12724852018-09-04 13:53:44 +01002155 if (!fu_plugin_runner_device_generic (self, device,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05002156 "fu_plugin_device_removed",
Richard Hughes4b303802019-10-04 13:22:51 +01002157 NULL,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05002158 &error_local))
2159 g_warning ("%s", error_local->message);
2160}
2161
Mario Limonciello1a680f32019-11-25 19:44:53 -06002162/**
2163 * fu_plugin_runner_device_register:
2164 * @self: a #FuPlugin
2165 * @device: a #FuDevice
2166 *
2167 * Call the device_registered routine for the plugin
2168 *
2169 * Since: 0.9.7
2170 **/
Mario Limoncielloe260ead2018-09-01 09:19:24 -05002171void
Richard Hughes12724852018-09-04 13:53:44 +01002172fu_plugin_runner_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +01002173{
Richard Hughes12724852018-09-04 13:53:44 +01002174 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughese1fd34d2017-08-24 14:19:51 +01002175 FuPluginDeviceRegisterFunc func = NULL;
2176
2177 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002178 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughese1fd34d2017-08-24 14:19:51 +01002179 return;
Richard Hughes34834102017-11-21 21:55:00 +00002180 if (priv->module == NULL)
2181 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01002182
2183 /* optional */
2184 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
2185 if (func != NULL) {
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002186 g_debug ("fu_plugin_device_registered(%s)", fu_plugin_get_name (self));
Richard Hughes12724852018-09-04 13:53:44 +01002187 func (self, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +01002188 }
2189}
2190
Mario Limonciello1a680f32019-11-25 19:44:53 -06002191/**
Richard Hughes0f66a022020-02-19 18:54:38 +00002192 * fu_plugin_runner_device_created:
2193 * @self: a #FuPlugin
2194 * @device: a #FuDevice
2195 * @error: a #GError or NULL
2196 *
2197 * Call the device_created routine for the plugin
2198 *
2199 * Returns: #TRUE for success, #FALSE for failure
2200 *
Mario Limonciello96117d12020-02-28 10:17:56 -06002201 * Since: 1.4.0
Richard Hughes0f66a022020-02-19 18:54:38 +00002202 **/
2203gboolean
2204fu_plugin_runner_device_created (FuPlugin *self, FuDevice *device, GError **error)
2205{
2206 FuPluginPrivate *priv = GET_PRIVATE (self);
2207 FuPluginDeviceFunc func = NULL;
2208
Richard Hughes6a489a92020-12-22 10:32:06 +00002209 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
2210 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
2211 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2212
Richard Hughes0f66a022020-02-19 18:54:38 +00002213 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002214 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes0f66a022020-02-19 18:54:38 +00002215 return TRUE;
2216 if (priv->module == NULL)
2217 return TRUE;
2218
2219 /* optional */
2220 g_module_symbol (priv->module, "fu_plugin_device_created", (gpointer *) &func);
2221 if (func == NULL)
2222 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002223 g_debug ("fu_plugin_device_created(%s)", fu_plugin_get_name (self));
Richard Hughes0f66a022020-02-19 18:54:38 +00002224 return func (self, device, error);
2225}
2226
2227/**
Mario Limonciello1a680f32019-11-25 19:44:53 -06002228 * fu_plugin_runner_verify:
2229 * @self: a #FuPlugin
2230 * @device: a #FuDevice
2231 * @flags: #FuPluginVerifyFlags
2232 * @error: A #GError or NULL
2233 *
2234 * Call into the plugin's verify routine
2235 *
2236 * Returns: #TRUE for success, #FALSE for failure
2237 *
2238 * Since: 0.8.0
2239 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002240gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002241fu_plugin_runner_verify (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00002242 FuDevice *device,
2243 FuPluginVerifyFlags flags,
2244 GError **error)
2245{
Richard Hughes12724852018-09-04 13:53:44 +01002246 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00002247 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01002248 GPtrArray *checksums;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002249 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00002250
Richard Hughes6a489a92020-12-22 10:32:06 +00002251 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
2252 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
2253 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2254
Richard Hughescff38bc2016-12-12 12:03:37 +00002255 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002256 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughescff38bc2016-12-12 12:03:37 +00002257 return TRUE;
2258
Richard Hughes639da472018-01-06 22:35:04 +00002259 /* no object loaded */
2260 if (priv->module == NULL)
2261 return TRUE;
2262
Richard Hughescff38bc2016-12-12 12:03:37 +00002263 /* optional */
2264 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
Richard Hughes7f677212019-10-05 16:19:40 +01002265 if (func == NULL) {
Richard Hughes7f677212019-10-05 16:19:40 +01002266 return fu_plugin_device_read_firmware (self, device, error);
2267 }
Richard Hughes1812fc72018-12-14 11:37:54 +00002268
2269 /* clear any existing verification checksums */
2270 checksums = fu_device_get_checksums (device);
2271 g_ptr_array_set_size (checksums, 0);
2272
Richard Hughesc9223be2019-03-18 08:46:42 +00002273 /* run additional detach */
2274 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes42f33df2019-10-05 20:52:33 +01002275 "fu_plugin_update_detach",
Richard Hughes4b303802019-10-04 13:22:51 +01002276 fu_plugin_device_detach,
Richard Hughesc9223be2019-03-18 08:46:42 +00002277 error))
2278 return FALSE;
2279
Richard Hughes1812fc72018-12-14 11:37:54 +00002280 /* run vfunc */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002281 g_debug ("verify(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002282 if (!func (self, device, flags, &error_local)) {
Richard Hughesc9223be2019-03-18 08:46:42 +00002283 g_autoptr(GError) error_attach = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002284 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05002285 g_critical ("unset plugin error in verify(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002286 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002287 g_set_error_literal (&error_local,
2288 FWUPD_ERROR,
2289 FWUPD_ERROR_INTERNAL,
2290 "unspecified error");
2291 }
2292 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
2293 "failed to verify using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002294 fu_plugin_get_name (self));
Richard Hughesc9223be2019-03-18 08:46:42 +00002295 /* make the device "work" again, but don't prefix the error */
2296 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes42f33df2019-10-05 20:52:33 +01002297 "fu_plugin_update_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01002298 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00002299 &error_attach)) {
2300 g_warning ("failed to attach whilst aborting verify(): %s",
2301 error_attach->message);
2302 }
Richard Hughesd0905142016-03-13 09:46:49 +00002303 return FALSE;
2304 }
Richard Hughesc9223be2019-03-18 08:46:42 +00002305
2306 /* run optional attach */
2307 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes42f33df2019-10-05 20:52:33 +01002308 "fu_plugin_update_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01002309 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00002310 error))
2311 return FALSE;
2312
2313 /* success */
Richard Hughesd0905142016-03-13 09:46:49 +00002314 return TRUE;
2315}
2316
Mario Limonciello1a680f32019-11-25 19:44:53 -06002317/**
2318 * fu_plugin_runner_activate:
2319 * @self: a #FuPlugin
2320 * @device: a #FuDevice
2321 * @error: A #GError or NULL
2322 *
2323 * Call into the plugin's activate routine
2324 *
2325 * Returns: #TRUE for success, #FALSE for failure
2326 *
2327 * Since: 1.2.6
2328 **/
Richard Hughesd0905142016-03-13 09:46:49 +00002329gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002330fu_plugin_runner_activate (FuPlugin *self, FuDevice *device, GError **error)
2331{
2332 guint64 flags;
2333
Richard Hughes6a489a92020-12-22 10:32:06 +00002334 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
2335 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
2336 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2337
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002338 /* final check */
2339 flags = fu_device_get_flags (device);
2340 if ((flags & FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION) == 0) {
2341 g_set_error (error,
2342 FWUPD_ERROR,
2343 FWUPD_ERROR_NOT_SUPPORTED,
2344 "Device %s does not need activation",
2345 fu_device_get_id (device));
2346 return FALSE;
2347 }
2348
2349 /* run vfunc */
2350 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01002351 "fu_plugin_activate",
2352 fu_plugin_device_activate,
2353 error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002354 return FALSE;
2355
2356 /* update with correct flags */
2357 fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION);
2358 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
2359 return TRUE;
2360}
2361
Mario Limonciello1a680f32019-11-25 19:44:53 -06002362/**
2363 * fu_plugin_runner_unlock:
2364 * @self: a #FuPlugin
2365 * @device: a #FuDevice
2366 * @error: A #GError or NULL
2367 *
2368 * Call into the plugin's unlock routine
2369 *
2370 * Returns: #TRUE for success, #FALSE for failure
2371 *
2372 * Since: 0.8.0
2373 **/
Mario Limonciello96a0dd52019-02-25 13:50:03 -06002374gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002375fu_plugin_runner_unlock (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00002376{
Richard Hughescff38bc2016-12-12 12:03:37 +00002377 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00002378
Richard Hughes6a489a92020-12-22 10:32:06 +00002379 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
2380 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
2381 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2382
Richard Hughescff38bc2016-12-12 12:03:37 +00002383 /* final check */
2384 flags = fu_device_get_flags (device);
2385 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
2386 g_set_error (error,
2387 FWUPD_ERROR,
2388 FWUPD_ERROR_NOT_SUPPORTED,
2389 "Device %s is not locked",
2390 fu_device_get_id (device));
2391 return FALSE;
2392 }
2393
Richard Hughes9c4b5312017-11-14 11:34:53 +00002394 /* run vfunc */
Richard Hughes12724852018-09-04 13:53:44 +01002395 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01002396 "fu_plugin_unlock",
2397 NULL,
2398 error))
Richard Hughes9c4b5312017-11-14 11:34:53 +00002399 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00002400
2401 /* update with correct flags */
2402 flags = fu_device_get_flags (device);
2403 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
2404 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
2405 return TRUE;
2406}
2407
Mario Limonciello1a680f32019-11-25 19:44:53 -06002408/**
2409 * fu_plugin_runner_update:
2410 * @self: a #FuPlugin
2411 * @device: a #FuDevice
2412 * @blob_fw: A #GBytes
2413 * @flags: A #FwupdInstallFlags
2414 * @error: A #GError or NULL
2415 *
2416 * Call into the plugin's update routine
2417 *
2418 * Returns: #TRUE for success, #FALSE for failure
2419 *
2420 * Since: 0.8.0
2421 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002422gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002423fu_plugin_runner_update (FuPlugin *self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01002424 FuDevice *device,
Richard Hughesa785a1c2017-08-25 16:00:58 +01002425 GBytes *blob_fw,
2426 FwupdInstallFlags flags,
2427 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00002428{
Richard Hughes12724852018-09-04 13:53:44 +01002429 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesa785a1c2017-08-25 16:00:58 +01002430 FuPluginUpdateFunc update_func;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002431 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00002432
Richard Hughes6a489a92020-12-22 10:32:06 +00002433 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
2434 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
2435 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2436
Richard Hughescff38bc2016-12-12 12:03:37 +00002437 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002438 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED)) {
Richard Hughes41c15482018-02-01 22:07:21 +00002439 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00002440 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00002441 }
Richard Hughesd0905142016-03-13 09:46:49 +00002442
Richard Hughes639da472018-01-06 22:35:04 +00002443 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00002444 if (priv->module == NULL) {
2445 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00002446 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00002447 }
Richard Hughes639da472018-01-06 22:35:04 +00002448
Richard Hughesd0905142016-03-13 09:46:49 +00002449 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01002450 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
2451 if (update_func == NULL) {
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002452 g_debug ("superclassed write_firmware(%s)", fu_plugin_get_name (self));
Richard Hughes4b303802019-10-04 13:22:51 +01002453 return fu_plugin_device_write_firmware (self, device, blob_fw, flags, error);
Richard Hughesa785a1c2017-08-25 16:00:58 +01002454 }
Richard Hughesd0905142016-03-13 09:46:49 +00002455
Richard Hughescff38bc2016-12-12 12:03:37 +00002456 /* online */
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002457 if (!update_func (self, device, blob_fw, flags, &error_local)) {
2458 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05002459 g_critical ("unset plugin error in update(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002460 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002461 g_set_error_literal (&error_local,
Richard Hughes3c8ada32018-10-12 10:08:58 +01002462 FWUPD_ERROR,
2463 FWUPD_ERROR_INTERNAL,
2464 "unspecified error");
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002465 return FALSE;
Richard Hughes3c8ada32018-10-12 10:08:58 +01002466 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002467 fu_device_set_update_error (device, error_local->message);
2468 g_propagate_error (error, g_steal_pointer (&error_local));
Richard Hughescff38bc2016-12-12 12:03:37 +00002469 return FALSE;
2470 }
2471
Richard Hughesf556d372017-06-15 19:49:18 +01002472 /* no longer valid */
Richard Hughesf8039642019-01-16 12:22:22 +00002473 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT) &&
2474 !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) {
Richard Hughes08435162018-12-12 10:34:16 +00002475 GPtrArray *checksums = fu_device_get_checksums (device);
2476 g_ptr_array_set_size (checksums, 0);
2477 }
Richard Hughesf556d372017-06-15 19:49:18 +01002478
Richard Hughes019a1bc2019-11-26 10:19:33 +00002479 /* success */
Richard Hughesd0905142016-03-13 09:46:49 +00002480 return TRUE;
2481}
Richard Hughescff38bc2016-12-12 12:03:37 +00002482
Mario Limonciello1a680f32019-11-25 19:44:53 -06002483/**
2484 * fu_plugin_runner_clear_results:
2485 * @self: a #FuPlugin
2486 * @device: a #FuDevice
2487 * @error: A #GError or NULL
2488 *
2489 * Call into the plugin's clear results routine
2490 *
2491 * Returns: #TRUE for success, #FALSE for failure
2492 *
2493 * Since: 0.8.0
2494 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002495gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002496fu_plugin_runner_clear_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00002497{
Richard Hughes12724852018-09-04 13:53:44 +01002498 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00002499 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002500 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00002501
Richard Hughes6a489a92020-12-22 10:32:06 +00002502 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
2503 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
2504 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2505
Richard Hughescff38bc2016-12-12 12:03:37 +00002506 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002507 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughescff38bc2016-12-12 12:03:37 +00002508 return TRUE;
2509
Richard Hughes639da472018-01-06 22:35:04 +00002510 /* no object loaded */
2511 if (priv->module == NULL)
2512 return TRUE;
2513
Richard Hughes65e44ca2018-01-30 17:26:30 +00002514 /* optional */
Richard Hughescd644902019-11-01 12:35:17 +00002515 g_module_symbol (priv->module, "fu_plugin_clear_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00002516 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00002517 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002518 g_debug ("clear_result(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002519 if (!func (self, device, &error_local)) {
2520 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05002521 g_critical ("unset plugin error in clear_result(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002522 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002523 g_set_error_literal (&error_local,
2524 FWUPD_ERROR,
2525 FWUPD_ERROR_INTERNAL,
2526 "unspecified error");
2527 }
2528 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
2529 "failed to clear_result using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002530 fu_plugin_get_name (self));
Richard Hughescff38bc2016-12-12 12:03:37 +00002531 return FALSE;
2532 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00002533 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00002534}
2535
Mario Limonciello1a680f32019-11-25 19:44:53 -06002536/**
2537 * fu_plugin_runner_get_results:
2538 * @self: a #FuPlugin
2539 * @device: a #FuDevice
2540 * @error: A #GError or NULL
2541 *
2542 * Call into the plugin's get results routine
2543 *
2544 * Returns: #TRUE for success, #FALSE for failure
2545 *
2546 * Since: 0.8.0
2547 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002548gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002549fu_plugin_runner_get_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00002550{
Richard Hughes12724852018-09-04 13:53:44 +01002551 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00002552 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002553 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00002554
Richard Hughes6a489a92020-12-22 10:32:06 +00002555 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
2556 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
2557 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2558
Richard Hughescff38bc2016-12-12 12:03:37 +00002559 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002560 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughescff38bc2016-12-12 12:03:37 +00002561 return TRUE;
2562
Richard Hughes639da472018-01-06 22:35:04 +00002563 /* no object loaded */
2564 if (priv->module == NULL)
2565 return TRUE;
2566
Richard Hughes65e44ca2018-01-30 17:26:30 +00002567 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00002568 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00002569 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00002570 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002571 g_debug ("get_results(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002572 if (!func (self, device, &error_local)) {
2573 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05002574 g_critical ("unset plugin error in get_results(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002575 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00002576 g_set_error_literal (&error_local,
2577 FWUPD_ERROR,
2578 FWUPD_ERROR_INTERNAL,
2579 "unspecified error");
2580 }
2581 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
2582 "failed to get_results using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002583 fu_plugin_get_name (self));
Richard Hughescff38bc2016-12-12 12:03:37 +00002584 return FALSE;
2585 }
Richard Hughescff38bc2016-12-12 12:03:37 +00002586 return TRUE;
2587}
2588
Richard Hughes08a37992017-09-12 12:57:43 +01002589/**
2590 * fu_plugin_get_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002591 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002592 *
2593 * Gets the plugin order, where higher numbers are run after lower
2594 * numbers.
2595 *
2596 * Returns: the integer value
Mario Limonciello1a680f32019-11-25 19:44:53 -06002597 *
2598 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002599 **/
2600guint
Richard Hughes12724852018-09-04 13:53:44 +01002601fu_plugin_get_order (FuPlugin *self)
Richard Hughes08a37992017-09-12 12:57:43 +01002602{
Richard Hughes12724852018-09-04 13:53:44 +01002603 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01002604 return priv->order;
2605}
2606
2607/**
2608 * fu_plugin_set_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002609 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002610 * @order: a integer value
2611 *
2612 * Sets the plugin order, where higher numbers are run after lower
2613 * numbers.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002614 *
2615 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002616 **/
2617void
Richard Hughes12724852018-09-04 13:53:44 +01002618fu_plugin_set_order (FuPlugin *self, guint order)
Richard Hughes08a37992017-09-12 12:57:43 +01002619{
Richard Hughes12724852018-09-04 13:53:44 +01002620 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01002621 priv->order = order;
2622}
2623
2624/**
Richard Hughes81c427c2018-08-06 15:20:17 +01002625 * fu_plugin_get_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002626 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01002627 *
2628 * Gets the plugin priority, where higher numbers are better.
2629 *
2630 * Returns: the integer value
Mario Limonciello1a680f32019-11-25 19:44:53 -06002631 *
2632 * Since: 1.1.1
Richard Hughes81c427c2018-08-06 15:20:17 +01002633 **/
2634guint
Richard Hughes12724852018-09-04 13:53:44 +01002635fu_plugin_get_priority (FuPlugin *self)
Richard Hughes81c427c2018-08-06 15:20:17 +01002636{
Richard Hughes12724852018-09-04 13:53:44 +01002637 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01002638 return priv->priority;
2639}
2640
2641/**
2642 * fu_plugin_set_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002643 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01002644 * @priority: a integer value
2645 *
2646 * Sets the plugin priority, where higher numbers are better.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002647 *
2648 * Since: 1.0.0
Richard Hughes81c427c2018-08-06 15:20:17 +01002649 **/
2650void
Richard Hughes12724852018-09-04 13:53:44 +01002651fu_plugin_set_priority (FuPlugin *self, guint priority)
Richard Hughes81c427c2018-08-06 15:20:17 +01002652{
Richard Hughes12724852018-09-04 13:53:44 +01002653 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01002654 priv->priority = priority;
2655}
2656
2657/**
Richard Hughes08a37992017-09-12 12:57:43 +01002658 * fu_plugin_add_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002659 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002660 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01002661 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01002662 *
2663 * If the plugin name is found, the rule will be used to sort the plugin list,
2664 * for example the plugin specified by @name will be ordered after this plugin
2665 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
2666 *
2667 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
2668 * If depsolving fails then fwupd will not start.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002669 *
2670 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002671 **/
2672void
Richard Hughes12724852018-09-04 13:53:44 +01002673fu_plugin_add_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes08a37992017-09-12 12:57:43 +01002674{
Richard Hughes12724852018-09-04 13:53:44 +01002675 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes11c59412020-06-22 15:29:48 +01002676 if (priv->rules[rule] == NULL)
2677 priv->rules[rule] = g_ptr_array_new_with_free_func (g_free);
Richard Hughes08a37992017-09-12 12:57:43 +01002678 g_ptr_array_add (priv->rules[rule], g_strdup (name));
Richard Hughes75b965d2018-11-15 13:51:21 +00002679 g_signal_emit (self, signals[SIGNAL_RULES_CHANGED], 0);
Richard Hughes08a37992017-09-12 12:57:43 +01002680}
2681
2682/**
2683 * fu_plugin_get_rules:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002684 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002685 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
2686 *
2687 * Gets the plugin IDs that should be run after this plugin.
2688 *
Richard Hughes11c59412020-06-22 15:29:48 +01002689 * Returns: (element-type utf8) (transfer none) (nullable): the list of plugin names, e.g. ['appstream']
Mario Limonciello1a680f32019-11-25 19:44:53 -06002690 *
2691 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002692 **/
2693GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +01002694fu_plugin_get_rules (FuPlugin *self, FuPluginRule rule)
Richard Hughes08a37992017-09-12 12:57:43 +01002695{
Richard Hughes12724852018-09-04 13:53:44 +01002696 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughesdad35972019-12-06 11:00:25 +00002697 g_return_val_if_fail (rule < FU_PLUGIN_RULE_LAST, NULL);
Richard Hughes08a37992017-09-12 12:57:43 +01002698 return priv->rules[rule];
2699}
2700
Richard Hughes80b79bb2018-01-11 21:11:06 +00002701/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002702 * fu_plugin_has_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002703 * @self: a #FuPlugin
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002704 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
2705 * @name: a plugin name, e.g. `upower`
2706 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01002707 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002708 *
2709 * Returns: %TRUE if the name exists for the specific rule
Mario Limonciello1a680f32019-11-25 19:44:53 -06002710 *
2711 * Since: 1.0.0
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002712 **/
2713gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002714fu_plugin_has_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002715{
Richard Hughes12724852018-09-04 13:53:44 +01002716 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes11c59412020-06-22 15:29:48 +01002717 if (priv->rules[rule] == NULL)
2718 return FALSE;
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002719 for (guint i = 0; i < priv->rules[rule]->len; i++) {
2720 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
2721 if (g_strcmp0 (tmp, name) == 0)
2722 return TRUE;
2723 }
2724 return FALSE;
2725}
2726
2727/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00002728 * fu_plugin_add_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002729 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00002730 * @key: a string, e.g. `FwupdateVersion`
2731 * @value: a string, e.g. `10`
2732 *
2733 * Sets any additional metadata to be included in the firmware report to aid
2734 * debugging problems.
2735 *
2736 * Any data included here will be sent to the metadata server after user
2737 * confirmation.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002738 *
2739 * Since: 1.0.4
Richard Hughes80b79bb2018-01-11 21:11:06 +00002740 **/
2741void
Richard Hughes12724852018-09-04 13:53:44 +01002742fu_plugin_add_report_metadata (FuPlugin *self, const gchar *key, const gchar *value)
Richard Hughes80b79bb2018-01-11 21:11:06 +00002743{
Richard Hughes12724852018-09-04 13:53:44 +01002744 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes1d900f72020-06-22 15:17:39 +01002745 if (priv->report_metadata == NULL) {
2746 priv->report_metadata = g_hash_table_new_full (g_str_hash,
2747 g_str_equal,
2748 g_free,
2749 g_free);
2750 }
Richard Hughes80b79bb2018-01-11 21:11:06 +00002751 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
2752}
2753
2754/**
2755 * fu_plugin_get_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002756 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00002757 *
2758 * Returns the list of additional metadata to be added when filing a report.
2759 *
Richard Hughes1d900f72020-06-22 15:17:39 +01002760 * Returns: (transfer none) (nullable): the map of report metadata
Mario Limonciello1a680f32019-11-25 19:44:53 -06002761 *
2762 * Since: 1.0.4
Richard Hughes80b79bb2018-01-11 21:11:06 +00002763 **/
2764GHashTable *
Richard Hughes12724852018-09-04 13:53:44 +01002765fu_plugin_get_report_metadata (FuPlugin *self)
Richard Hughes80b79bb2018-01-11 21:11:06 +00002766{
Richard Hughes12724852018-09-04 13:53:44 +01002767 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002768 return priv->report_metadata;
2769}
2770
Mario Limonciello963dc422018-02-27 14:26:58 -06002771/**
2772 * fu_plugin_get_config_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002773 * @self: a #FuPlugin
Mario Limonciello963dc422018-02-27 14:26:58 -06002774 * @key: A settings key
2775 *
2776 * Return the value of a key if it's been configured
2777 *
2778 * Since: 1.0.6
2779 **/
2780gchar *
Richard Hughes12724852018-09-04 13:53:44 +01002781fu_plugin_get_config_value (FuPlugin *self, const gchar *key)
Mario Limonciello963dc422018-02-27 14:26:58 -06002782{
Richard Hughes4be17d12018-05-30 20:36:29 +01002783 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06002784 g_autofree gchar *conf_file = NULL;
2785 g_autofree gchar *conf_path = NULL;
2786 g_autoptr(GKeyFile) keyfile = NULL;
2787 const gchar *plugin_name;
2788
Richard Hughes4be17d12018-05-30 20:36:29 +01002789 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Richard Hughes12724852018-09-04 13:53:44 +01002790 plugin_name = fu_plugin_get_name (self);
Mario Limonciello963dc422018-02-27 14:26:58 -06002791 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01002792 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06002793 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
2794 return NULL;
2795 keyfile = g_key_file_new ();
2796 if (!g_key_file_load_from_file (keyfile, conf_path,
2797 G_KEY_FILE_NONE, NULL))
2798 return NULL;
2799 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
2800}
2801
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002802/**
Richard Hughes334ba792020-02-19 20:44:56 +00002803 * fu_plugin_get_config_value_boolean:
2804 * @self: a #FuPlugin
2805 * @key: A settings key
2806 *
2807 * Return the boolean value of a key if it's been configured
2808 *
2809 * Returns: %TRUE if the value is `true` (case insensitive), %FALSE otherwise
2810 *
Mario Limonciello96117d12020-02-28 10:17:56 -06002811 * Since: 1.4.0
Richard Hughes334ba792020-02-19 20:44:56 +00002812 **/
2813gboolean
2814fu_plugin_get_config_value_boolean (FuPlugin *self, const gchar *key)
2815{
2816 g_autofree gchar *tmp = fu_plugin_get_config_value (self, key);
2817 if (tmp == NULL)
2818 return FALSE;
Richard Hughes5337a432020-02-21 12:04:32 +00002819 return g_ascii_strcasecmp (tmp, "true") == 0;
Richard Hughes334ba792020-02-19 20:44:56 +00002820}
2821
2822/**
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002823 * fu_plugin_name_compare:
2824 * @plugin1: first #FuPlugin to compare.
2825 * @plugin2: second #FuPlugin to compare.
2826 *
2827 * Compares two plugins by their names.
2828 *
2829 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002830 *
2831 * Since: 1.0.8
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002832 **/
2833gint
2834fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2835{
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002836 return g_strcmp0 (fu_plugin_get_name (plugin1), fu_plugin_get_name (plugin2));
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002837}
2838
2839/**
2840 * fu_plugin_order_compare:
2841 * @plugin1: first #FuPlugin to compare.
2842 * @plugin2: second #FuPlugin to compare.
2843 *
2844 * Compares two plugins by their depsolved order.
2845 *
2846 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002847 *
2848 * Since: 1.0.8
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002849 **/
2850gint
2851fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2852{
2853 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
2854 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
2855 if (priv1->order < priv2->order)
2856 return -1;
2857 if (priv1->order > priv2->order)
2858 return 1;
2859 return 0;
2860}
2861
Richard Hughescff38bc2016-12-12 12:03:37 +00002862static void
2863fu_plugin_class_init (FuPluginClass *klass)
2864{
2865 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2866 object_class->finalize = fu_plugin_finalize;
2867 signals[SIGNAL_DEVICE_ADDED] =
2868 g_signal_new ("device-added",
2869 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2870 G_STRUCT_OFFSET (FuPluginClass, device_added),
2871 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2872 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
2873 signals[SIGNAL_DEVICE_REMOVED] =
2874 g_signal_new ("device-removed",
2875 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2876 G_STRUCT_OFFSET (FuPluginClass, device_removed),
2877 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2878 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01002879 signals[SIGNAL_DEVICE_REGISTER] =
2880 g_signal_new ("device-register",
2881 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2882 G_STRUCT_OFFSET (FuPluginClass, device_register),
2883 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2884 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00002885 signals[SIGNAL_RECOLDPLUG] =
2886 g_signal_new ("recoldplug",
2887 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2888 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
2889 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2890 G_TYPE_NONE, 0);
Richard Hughes399859e2020-05-11 19:44:03 +01002891 signals[SIGNAL_SECURITY_CHANGED] =
2892 g_signal_new ("security-changed",
2893 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2894 G_STRUCT_OFFSET (FuPluginClass, security_changed),
2895 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2896 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00002897 signals[SIGNAL_SET_COLDPLUG_DELAY] =
2898 g_signal_new ("set-coldplug-delay",
2899 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2900 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
2901 NULL, NULL, g_cclosure_marshal_VOID__UINT,
2902 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughesaabdc372018-11-14 10:11:08 +00002903 signals[SIGNAL_CHECK_SUPPORTED] =
2904 g_signal_new ("check-supported",
2905 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2906 G_STRUCT_OFFSET (FuPluginClass, check_supported),
2907 NULL, NULL, g_cclosure_marshal_generic,
2908 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
Richard Hughes75b965d2018-11-15 13:51:21 +00002909 signals[SIGNAL_RULES_CHANGED] =
2910 g_signal_new ("rules-changed",
2911 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2912 G_STRUCT_OFFSET (FuPluginClass, rules_changed),
2913 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2914 G_TYPE_NONE, 0);
Richard Hughes95c98a92019-10-22 16:03:15 +01002915 signals[SIGNAL_ADD_FIRMWARE_GTYPE] =
2916 g_signal_new ("add-firmware-gtype",
2917 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2918 G_STRUCT_OFFSET (FuPluginClass, add_firmware_gtype),
2919 NULL, NULL, g_cclosure_marshal_generic,
2920 G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_GTYPE);
Richard Hughescff38bc2016-12-12 12:03:37 +00002921}
2922
2923static void
Richard Hughes12724852018-09-04 13:53:44 +01002924fu_plugin_init (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +00002925{
Richard Hughes12724852018-09-04 13:53:44 +01002926 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesfaf2afe2021-01-13 14:00:20 +00002927 g_rw_lock_init (&priv->cache_mutex);
Richard Hughescff38bc2016-12-12 12:03:37 +00002928}
2929
2930static void
2931fu_plugin_finalize (GObject *object)
2932{
Richard Hughes12724852018-09-04 13:53:44 +01002933 FuPlugin *self = FU_PLUGIN (object);
2934 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002935 FuPluginInitFunc func = NULL;
2936
Richard Hughesfaf2afe2021-01-13 14:00:20 +00002937 g_rw_lock_clear (&priv->cache_mutex);
Richard Hughesaae22e42020-06-22 21:32:59 +01002938
Richard Hughescff38bc2016-12-12 12:03:37 +00002939 /* optional */
2940 if (priv->module != NULL) {
2941 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
2942 if (func != NULL) {
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002943 g_debug ("destroy(%s)", fu_plugin_get_name (self));
Richard Hughes12724852018-09-04 13:53:44 +01002944 func (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002945 }
2946 }
2947
Richard Hughes11c59412020-06-22 15:29:48 +01002948 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++) {
2949 if (priv->rules[i] != NULL)
2950 g_ptr_array_unref (priv->rules[i]);
2951 }
Richard Hughes68ab1e42021-01-13 14:01:17 +00002952 if (priv->devices != NULL)
2953 g_ptr_array_unref (priv->devices);
Richard Hughesb8f8db22017-04-25 15:56:00 +01002954 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01002955 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01002956 if (priv->quirks != NULL)
2957 g_object_unref (priv->quirks);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002958 if (priv->udev_subsystems != NULL)
2959 g_ptr_array_unref (priv->udev_subsystems);
Richard Hughes49e5e052017-09-03 12:15:41 +01002960 if (priv->smbios != NULL)
2961 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01002962 if (priv->runtime_versions != NULL)
2963 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01002964 if (priv->compile_versions != NULL)
2965 g_hash_table_unref (priv->compile_versions);
Richard Hughes1d900f72020-06-22 15:17:39 +01002966 if (priv->report_metadata != NULL)
2967 g_hash_table_unref (priv->report_metadata);
Richard Hughesfaf2afe2021-01-13 14:00:20 +00002968 if (priv->cache != NULL)
2969 g_hash_table_unref (priv->cache);
Richard Hughes84999302019-05-02 10:18:32 +01002970 g_free (priv->build_hash);
Richard Hughescff38bc2016-12-12 12:03:37 +00002971 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05002972 /* Must happen as the last step to avoid prematurely
2973 * freeing memory held by the plugin */
Richard Hughes862ec5c2020-05-22 14:38:02 +01002974#ifdef RUNNING_ON_VALGRIND
2975 if (priv->module != NULL && RUNNING_ON_VALGRIND == 0)
2976#else
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05002977 if (priv->module != NULL)
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05002978#endif
Richard Hughes862ec5c2020-05-22 14:38:02 +01002979 g_module_close (priv->module);
Richard Hughescff38bc2016-12-12 12:03:37 +00002980
2981 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
2982}
2983
Mario Limonciello1a680f32019-11-25 19:44:53 -06002984/**
2985 * fu_plugin_new:
2986 *
2987 * Creates a new #FuPlugin
2988 *
2989 * Since: 0.8.0
2990 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002991FuPlugin *
2992fu_plugin_new (void)
2993{
Richard Hughes12724852018-09-04 13:53:44 +01002994 return FU_PLUGIN (g_object_new (FU_TYPE_PLUGIN, NULL));
Richard Hughescff38bc2016-12-12 12:03:37 +00002995}