blob: 9534a06e2613fd0ec3e11a3a63e3eea2f999752f [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 Hughesb333e002021-04-01 10:40:02 +010020#include "fu-context-private.h"
Richard Hughes9dde04f2017-09-13 12:07:15 +010021#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000022#include "fu-plugin-private.h"
Richard Hughes37d09432018-09-09 10:39:45 +010023#include "fu-mutex.h"
Richard Hughesd0905142016-03-13 09:46:49 +000024
Richard Hughes4eada342017-10-03 21:20:32 +010025/**
26 * SECTION:fu-plugin
27 * @short_description: a daemon plugin
28 *
29 * An object that represents a plugin run by the daemon.
30 *
31 * See also: #FuDevice
32 */
33
Richard Hughescff38bc2016-12-12 12:03:37 +000034static void fu_plugin_finalize (GObject *object);
35
36typedef struct {
37 GModule *module;
Richard Hughes08a37992017-09-12 12:57:43 +010038 guint order;
Richard Hughes81c427c2018-08-06 15:20:17 +010039 guint priority;
Richard Hughes08a37992017-09-12 12:57:43 +010040 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughes68ab1e42021-01-13 14:01:17 +000041 GPtrArray *devices; /* (nullable) (element-type FuDevice) */
Richard Hughesf425d292019-01-18 17:57:39 +000042 gchar *build_hash;
Richard Hughes0eb123b2018-04-19 12:00:04 +010043 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010044 GHashTable *compile_versions;
Richard Hughesb333e002021-04-01 10:40:02 +010045 FuContext *ctx;
Richard Hughes989acf12019-10-05 20:16:47 +010046 GType device_gtype;
Richard Hughesfaf2afe2021-01-13 14:00:20 +000047 GHashTable *cache; /* (nullable): platform_id:GObject */
48 GRWLock cache_mutex;
Richard Hughes1d900f72020-06-22 15:17:39 +010049 GHashTable *report_metadata; /* (nullable): key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000050 FuPluginData *data;
51} FuPluginPrivate;
52
53enum {
54 SIGNAL_DEVICE_ADDED,
55 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010056 SIGNAL_DEVICE_REGISTER,
Richard Hughes75b965d2018-11-15 13:51:21 +000057 SIGNAL_RULES_CHANGED,
Richard Hughesaabdc372018-11-14 10:11:08 +000058 SIGNAL_CHECK_SUPPORTED,
Richard Hughescff38bc2016-12-12 12:03:37 +000059 SIGNAL_LAST
60};
61
62static guint signals[SIGNAL_LAST] = { 0 };
63
Richard Hughes7bcb8d42020-10-08 15:47:47 +010064G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, FWUPD_TYPE_PLUGIN)
Richard Hughescff38bc2016-12-12 12:03:37 +000065#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
66
67typedef const gchar *(*FuPluginGetNameFunc) (void);
Richard Hughes12724852018-09-04 13:53:44 +010068typedef void (*FuPluginInitFunc) (FuPlugin *self);
69typedef gboolean (*FuPluginStartupFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000070 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010071typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *self,
Richard Hughese1fd34d2017-08-24 14:19:51 +010072 FuDevice *device);
Richard Hughes12724852018-09-04 13:53:44 +010073typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000074 FuDevice *device,
75 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010076typedef gboolean (*FuPluginFlaggedDeviceFunc) (FuPlugin *self,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -050077 FwupdInstallFlags flags,
78 FuDevice *device,
79 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010080typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *self,
Richard Hughesdbd8c762018-06-15 20:31:40 +010081 GPtrArray *devices,
82 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010083typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000084 FuDevice *device,
85 FuPluginVerifyFlags flags,
86 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010087typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000088 FuDevice *device,
89 GBytes *blob_fw,
90 FwupdInstallFlags flags,
91 GError **error);
Richard Hughesf58ac732020-05-12 15:23:44 +010092typedef void (*FuPluginSecurityAttrsFunc) (FuPlugin *self,
93 FuSecurityAttrs *attrs);
Richard Hughescff38bc2016-12-12 12:03:37 +000094
Richard Hughes57d18222017-01-10 16:02:59 +000095/**
Mario Limonciello52e75ba2019-11-22 13:21:19 -060096 * fu_plugin_is_open:
97 * @self: A #FuPlugin
98 *
99 * Determines if the plugin is opened
100 *
101 * Returns: TRUE for opened, FALSE for not
102 *
103 * Since: 1.3.5
104 **/
105gboolean
106fu_plugin_is_open (FuPlugin *self)
107{
108 FuPluginPrivate *priv = GET_PRIVATE (self);
109 return priv->module != NULL;
110}
111
112/**
Richard Hughes57d18222017-01-10 16:02:59 +0000113 * fu_plugin_get_name:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100114 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000115 *
116 * Gets the plugin name.
117 *
118 * Returns: a plugin name, or %NULL for unknown.
119 *
120 * Since: 0.8.0
121 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000122const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100123fu_plugin_get_name (FuPlugin *self)
Richard Hughesd0905142016-03-13 09:46:49 +0000124{
Richard Hughes12724852018-09-04 13:53:44 +0100125 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100126 return fwupd_plugin_get_name (FWUPD_PLUGIN (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000127}
Richard Hughesd0905142016-03-13 09:46:49 +0000128
Mario Limonciello1a680f32019-11-25 19:44:53 -0600129/**
130 * fu_plugin_set_name:
131 * @self: A #FuPlugin
132 * @name: A string
133 *
134 * Sets the plugin name.
135 *
136 * Since: 0.8.0
137 **/
Richard Hughes34834102017-11-21 21:55:00 +0000138void
Richard Hughes12724852018-09-04 13:53:44 +0100139fu_plugin_set_name (FuPlugin *self, const gchar *name)
Richard Hughes34834102017-11-21 21:55:00 +0000140{
Richard Hughes12724852018-09-04 13:53:44 +0100141 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100142 fwupd_plugin_set_name (FWUPD_PLUGIN (self), name);
Richard Hughes34834102017-11-21 21:55:00 +0000143}
144
Richard Hughes57d18222017-01-10 16:02:59 +0000145/**
Richard Hughesf425d292019-01-18 17:57:39 +0000146 * fu_plugin_set_build_hash:
147 * @self: A #FuPlugin
148 * @build_hash: A checksum
149 *
150 * Sets the plugin build hash, typically a SHA256 checksum. All plugins must
151 * set the correct checksum to avoid the daemon being marked as tainted.
152 *
153 * Since: 1.2.4
154 **/
155void
156fu_plugin_set_build_hash (FuPlugin *self, const gchar *build_hash)
157{
158 FuPluginPrivate *priv = GET_PRIVATE (self);
159 g_return_if_fail (FU_IS_PLUGIN (self));
160 g_return_if_fail (build_hash != NULL);
Richard Hughes382524d2021-01-28 13:14:35 +0000161
162 /* not changed */
163 if (g_strcmp0 (priv->build_hash, build_hash) == 0)
164 return;
165
Richard Hughesf425d292019-01-18 17:57:39 +0000166 g_free (priv->build_hash);
167 priv->build_hash = g_strdup (build_hash);
168}
169
Mario Limonciello1a680f32019-11-25 19:44:53 -0600170/**
171 * fu_plugin_get_build_hash:
172 * @self: A #FuPlugin
173 *
174 * Gets the build hash a plugin was generated with.
175 *
176 * Returns: (transfer none): a #gchar, or %NULL for unset.
177 *
178 * Since: 1.2.4
179 **/
Richard Hughesf425d292019-01-18 17:57:39 +0000180const gchar *
181fu_plugin_get_build_hash (FuPlugin *self)
182{
183 FuPluginPrivate *priv = GET_PRIVATE (self);
184 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
185 return priv->build_hash;
186}
187
188/**
Richard Hughes57d18222017-01-10 16:02:59 +0000189 * fu_plugin_cache_lookup:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100190 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000191 * @id: the key
192 *
193 * Finds an object in the per-plugin cache.
194 *
195 * Returns: (transfer none): a #GObject, or %NULL for unfound.
196 *
197 * Since: 0.8.0
198 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000199gpointer
Richard Hughes12724852018-09-04 13:53:44 +0100200fu_plugin_cache_lookup (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000201{
Richard Hughes12724852018-09-04 13:53:44 +0100202 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000203 g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_reader_locker_new (&priv->cache_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100204 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughesccd78a92017-01-11 16:57:41 +0000205 g_return_val_if_fail (id != NULL, NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100206 g_return_val_if_fail (locker != NULL, NULL);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000207 if (priv->cache == NULL)
Richard Hughes371f6b22020-06-22 15:21:17 +0100208 return NULL;
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000209 return g_hash_table_lookup (priv->cache, id);
Richard Hughescff38bc2016-12-12 12:03:37 +0000210}
Richard Hughesd0905142016-03-13 09:46:49 +0000211
Richard Hughes57d18222017-01-10 16:02:59 +0000212/**
213 * fu_plugin_cache_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100214 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000215 * @id: the key
216 * @dev: a #GObject, typically a #FuDevice
217 *
218 * Adds an object to the per-plugin cache.
219 *
220 * Since: 0.8.0
221 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000222void
Richard Hughes12724852018-09-04 13:53:44 +0100223fu_plugin_cache_add (FuPlugin *self, const gchar *id, gpointer dev)
Richard Hughescff38bc2016-12-12 12:03:37 +0000224{
Richard Hughes12724852018-09-04 13:53:44 +0100225 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000226 g_autoptr(GRWLockWriterLocker) locker = g_rw_lock_writer_locker_new (&priv->cache_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100227 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000228 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100229 g_return_if_fail (locker != NULL);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000230 if (priv->cache == NULL) {
231 priv->cache = g_hash_table_new_full (g_str_hash,
232 g_str_equal,
233 g_free,
234 (GDestroyNotify) g_object_unref);
Richard Hughes371f6b22020-06-22 15:21:17 +0100235 }
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000236 g_hash_table_insert (priv->cache, g_strdup (id), g_object_ref (dev));
Richard Hughescff38bc2016-12-12 12:03:37 +0000237}
238
Richard Hughes57d18222017-01-10 16:02:59 +0000239/**
240 * fu_plugin_cache_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100241 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000242 * @id: the key
243 *
244 * Removes an object from the per-plugin cache.
245 *
246 * Since: 0.8.0
247 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000248void
Richard Hughes12724852018-09-04 13:53:44 +0100249fu_plugin_cache_remove (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000250{
Richard Hughes12724852018-09-04 13:53:44 +0100251 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000252 g_autoptr(GRWLockWriterLocker) locker = g_rw_lock_writer_locker_new (&priv->cache_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100253 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000254 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100255 g_return_if_fail (locker != NULL);
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000256 if (priv->cache == NULL)
Richard Hughes371f6b22020-06-22 15:21:17 +0100257 return;
Richard Hughesfaf2afe2021-01-13 14:00:20 +0000258 g_hash_table_remove (priv->cache, id);
Richard Hughescff38bc2016-12-12 12:03:37 +0000259}
260
Richard Hughes57d18222017-01-10 16:02:59 +0000261/**
262 * fu_plugin_get_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100263 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000264 *
Richard Hughes4eada342017-10-03 21:20:32 +0100265 * Gets the per-plugin allocated private data. This will return %NULL unless
266 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000267 *
Richard Hughes4eada342017-10-03 21:20:32 +0100268 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000269 *
270 * Since: 0.8.0
271 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000272FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100273fu_plugin_get_data (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000274{
Richard Hughes12724852018-09-04 13:53:44 +0100275 FuPluginPrivate *priv = GET_PRIVATE (self);
276 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000277 return priv->data;
278}
279
Richard Hughes57d18222017-01-10 16:02:59 +0000280/**
Richard Hughes00f66f62019-11-27 11:42:53 +0000281 * fu_plugin_alloc_data: (skip):
Richard Hughes2c0635a2018-09-04 14:52:46 +0100282 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000283 * @data_sz: the size to allocate
284 *
285 * Allocates the per-plugin allocated private data.
286 *
287 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
288 *
289 * Since: 0.8.0
290 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000291FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100292fu_plugin_alloc_data (FuPlugin *self, gsize data_sz)
Richard Hughescff38bc2016-12-12 12:03:37 +0000293{
Richard Hughes12724852018-09-04 13:53:44 +0100294 FuPluginPrivate *priv = GET_PRIVATE (self);
295 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000296 if (priv->data != NULL) {
297 g_critical ("fu_plugin_alloc_data() already used by plugin");
298 return priv->data;
299 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000300 priv->data = g_malloc0 (data_sz);
301 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000302}
303
Richard Hughes57d18222017-01-10 16:02:59 +0000304/**
Mario Limonciello1a680f32019-11-25 19:44:53 -0600305 * fu_plugin_guess_name_from_fn:
306 * @filename: filename to guess
307 *
308 * Tries to guess the name of the plugin from a filename
309 *
310 * Returns: (transfer full): the guessed name of the plugin
311 *
312 * Since: 1.0.8
313 **/
Richard Hughes1e456bc2018-05-10 20:16:16 +0100314gchar *
315fu_plugin_guess_name_from_fn (const gchar *filename)
316{
317 const gchar *prefix = "libfu_plugin_";
318 gchar *name;
319 gchar *str = g_strstr_len (filename, -1, prefix);
320 if (str == NULL)
321 return NULL;
322 name = g_strdup (str + strlen (prefix));
323 g_strdelimit (name, ".", '\0');
324 return name;
325}
326
Mario Limonciello1a680f32019-11-25 19:44:53 -0600327/**
328 * fu_plugin_open:
329 * @self: A #FuPlugin
330 * @filename: The shared object filename to open
331 * @error: A #GError or NULL
332 *
333 * Opens the plugin module
334 *
335 * Returns: TRUE for success, FALSE for fail
336 *
337 * Since: 0.8.0
338 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000339gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100340fu_plugin_open (FuPlugin *self, const gchar *filename, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +0000341{
Richard Hughes12724852018-09-04 13:53:44 +0100342 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000343 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000344
Richard Hughes6a489a92020-12-22 10:32:06 +0000345 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
346 g_return_val_if_fail (filename != NULL, FALSE);
347 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
348
Richard Hughescff38bc2016-12-12 12:03:37 +0000349 priv->module = g_module_open (filename, 0);
350 if (priv->module == NULL) {
351 g_set_error (error,
352 G_IO_ERROR,
353 G_IO_ERROR_FAILED,
Mario Limonciellof5605532019-11-04 07:49:50 -0600354 "failed to open plugin %s: %s",
355 filename, g_module_error ());
Mario Limoncielloc3a81732020-10-20 09:16:18 -0500356 fu_plugin_add_flag (self, FWUPD_PLUGIN_FLAG_FAILED_OPEN);
357 fu_plugin_add_flag (self, FWUPD_PLUGIN_FLAG_USER_WARNING);
Richard Hughescff38bc2016-12-12 12:03:37 +0000358 return FALSE;
359 }
360
361 /* set automatically */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100362 if (fu_plugin_get_name (self) == NULL) {
363 g_autofree gchar *str = fu_plugin_guess_name_from_fn (filename);
364 fu_plugin_set_name (self, str);
365 }
Richard Hughesd0905142016-03-13 09:46:49 +0000366
367 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000368 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
369 if (func != NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -0500370 g_debug ("init(%s)", filename);
Richard Hughes12724852018-09-04 13:53:44 +0100371 func (self);
Richard Hughesd0905142016-03-13 09:46:49 +0000372 }
373
Richard Hughescff38bc2016-12-12 12:03:37 +0000374 return TRUE;
375}
376
Richard Hughes203ed842020-11-02 14:25:13 +0000377/* order of usefulness to the user */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100378static const gchar *
379fu_plugin_build_device_update_error (FuPlugin *self)
380{
381 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_NO_HARDWARE))
382 return "Not updatable as required hardware was not found";
383 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_LEGACY_BIOS))
384 return "Not updatable in legacy BIOS mode";
385 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_CAPSULES_UNSUPPORTED))
Mario Limonciello797da4f2021-01-12 12:38:51 -0600386 return "Not updatable as UEFI capsule updates not enabled in firmware setup";
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100387 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_UNLOCK_REQUIRED))
388 return "Not updatable as requires unlock";
389 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_EFIVAR_NOT_MOUNTED))
390 return "Not updatable as efivarfs was not found";
391 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_ESP_NOT_FOUND))
392 return "Not updatable as UEFI ESP partition not detected";
393 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
394 return "Not updatable as plugin was disabled";
395 return NULL;
396}
397
Richard Hughes68ab1e42021-01-13 14:01:17 +0000398static void
399fu_plugin_ensure_devices (FuPlugin *self)
400{
401 FuPluginPrivate *priv = GET_PRIVATE (self);
402 if (priv->devices != NULL)
403 return;
404 priv->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
405}
406
Richard Hughes57d18222017-01-10 16:02:59 +0000407/**
408 * fu_plugin_device_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100409 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000410 * @device: A #FuDevice
411 *
412 * Asks the daemon to add a device to the exported list. If this device ID
413 * has already been added by a different plugin then this request will be
414 * ignored.
415 *
416 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
417 * actually flashing an image to the hardware so that higher-priority plugins
418 * can add the device themselves.
419 *
420 * Since: 0.8.0
421 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000422void
Richard Hughes12724852018-09-04 13:53:44 +0100423fu_plugin_device_add (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000424{
Richard Hughes68ab1e42021-01-13 14:01:17 +0000425 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes5e447292018-04-27 14:25:54 +0100426 GPtrArray *children;
Richard Hughesc125ec02018-09-05 19:35:17 +0100427 g_autoptr(GError) error = NULL;
Richard Hughes5e447292018-04-27 14:25:54 +0100428
Richard Hughes12724852018-09-04 13:53:44 +0100429 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000430 g_return_if_fail (FU_IS_DEVICE (device));
431
Richard Hughesc125ec02018-09-05 19:35:17 +0100432 /* ensure the device ID is set from the physical and logical IDs */
433 if (!fu_device_ensure_id (device, &error)) {
434 g_warning ("ignoring add: %s", error->message);
435 return;
436 }
437
Richard Hughes68ab1e42021-01-13 14:01:17 +0000438 /* add to array */
439 fu_plugin_ensure_devices (self);
440 g_ptr_array_add (priv->devices, g_object_ref (device));
441
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100442 /* proxy to device where required */
443 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_CLEAR_UPDATABLE)) {
444 g_debug ("plugin %s has _CLEAR_UPDATABLE, so removing from %s",
445 fu_plugin_get_name (self),
446 fu_device_get_id (device));
447 fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE);
448 }
449 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_USER_WARNING) &&
450 fu_device_get_update_error (device) == NULL) {
451 const gchar *tmp = fu_plugin_build_device_update_error (self);
452 g_debug ("setting %s update error to '%s' from %s",
453 fu_device_get_id (device), tmp, fu_plugin_get_name (self));
454 fu_device_set_update_error (device, tmp);
455 }
456
Richard Hughescff38bc2016-12-12 12:03:37 +0000457 g_debug ("emit added from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100458 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000459 fu_device_get_id (device));
460 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
Richard Hughes12724852018-09-04 13:53:44 +0100461 fu_device_set_plugin (device, fu_plugin_get_name (self));
462 g_signal_emit (self, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100463
Richard Hughes128c0162018-08-10 11:00:29 +0100464 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100465 children = fu_device_get_children (device);
466 for (guint i = 0; i < children->len; i++) {
467 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100468 if (fu_device_get_created (child) == 0)
Richard Hughes12724852018-09-04 13:53:44 +0100469 fu_plugin_device_add (self, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100470 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000471}
472
Richard Hughese1fd34d2017-08-24 14:19:51 +0100473/**
Richard Hughes68ab1e42021-01-13 14:01:17 +0000474 * fu_plugin_get_devices:
475 * @self: A #FuPlugin
476 *
477 * Returns all devices added by the plugin using fu_plugin_device_add() and
478 * not yet removed with fu_plugin_device_remove().
479 *
480 * Returns: (transfer none) (element-type FuDevice): devices
481 *
482 * Since: 1.5.6
483 **/
484GPtrArray *
485fu_plugin_get_devices (FuPlugin *self)
486{
487 FuPluginPrivate *priv = GET_PRIVATE (self);
488 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
489 fu_plugin_ensure_devices (self);
490 return priv->devices;
491}
492
493/**
Richard Hughese1fd34d2017-08-24 14:19:51 +0100494 * fu_plugin_device_register:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100495 * @self: A #FuPlugin
Richard Hughese1fd34d2017-08-24 14:19:51 +0100496 * @device: A #FuDevice
497 *
498 * Registers the device with other plugins so they can set metadata.
499 *
500 * Plugins do not have to call this manually as this is done automatically
501 * when using fu_plugin_device_add(). They may wish to use this manually
Richard Hughes21eaeef2020-01-14 12:10:01 +0000502 * if for instance the coldplug should be ignored based on the metadata
Richard Hughese1fd34d2017-08-24 14:19:51 +0100503 * set from other plugins.
504 *
505 * Since: 0.9.7
506 **/
507void
Richard Hughes12724852018-09-04 13:53:44 +0100508fu_plugin_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +0100509{
Richard Hughesc125ec02018-09-05 19:35:17 +0100510 g_autoptr(GError) error = NULL;
511
Richard Hughes12724852018-09-04 13:53:44 +0100512 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughese1fd34d2017-08-24 14:19:51 +0100513 g_return_if_fail (FU_IS_DEVICE (device));
514
Richard Hughesc125ec02018-09-05 19:35:17 +0100515 /* ensure the device ID is set from the physical and logical IDs */
516 if (!fu_device_ensure_id (device, &error)) {
517 g_warning ("ignoring registration: %s", error->message);
518 return;
519 }
520
Richard Hughese1fd34d2017-08-24 14:19:51 +0100521 g_debug ("emit device-register from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100522 fu_plugin_get_name (self),
Richard Hughese1fd34d2017-08-24 14:19:51 +0100523 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100524 g_signal_emit (self, signals[SIGNAL_DEVICE_REGISTER], 0, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +0100525}
526
Richard Hughes57d18222017-01-10 16:02:59 +0000527/**
Richard Hughes4eada342017-10-03 21:20:32 +0100528 * fu_plugin_device_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100529 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000530 * @device: A #FuDevice
531 *
532 * Asks the daemon to remove a device from the exported list.
533 *
534 * Since: 0.8.0
535 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000536void
Richard Hughes12724852018-09-04 13:53:44 +0100537fu_plugin_device_remove (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000538{
Richard Hughes68ab1e42021-01-13 14:01:17 +0000539 FuPluginPrivate *priv = GET_PRIVATE (self);
540
Richard Hughes12724852018-09-04 13:53:44 +0100541 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000542 g_return_if_fail (FU_IS_DEVICE (device));
543
Richard Hughes68ab1e42021-01-13 14:01:17 +0000544 /* remove from array */
545 if (priv->devices != NULL)
546 g_ptr_array_remove (priv->devices, device);
547
Richard Hughescff38bc2016-12-12 12:03:37 +0000548 g_debug ("emit removed from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100549 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000550 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100551 g_signal_emit (self, signals[SIGNAL_DEVICE_REMOVED], 0, device);
Richard Hughescff38bc2016-12-12 12:03:37 +0000552}
553
Richard Hughes57d18222017-01-10 16:02:59 +0000554/**
Richard Hughes19841802019-09-10 16:48:00 +0100555 * fu_plugin_has_custom_flag:
556 * @self: A #FuPlugin
557 * @flag: A custom text flag, specific to the plugin, e.g. `uefi-force-enable`
558 *
559 * Returns if a per-plugin HwId custom flag exists, typically added from a DMI quirk.
560 *
561 * Returns: %TRUE if the quirk entry exists
562 *
563 * Since: 1.3.1
564 **/
565gboolean
566fu_plugin_has_custom_flag (FuPlugin *self, const gchar *flag)
567{
568 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb333e002021-04-01 10:40:02 +0100569 GPtrArray *guids;
Richard Hughes19841802019-09-10 16:48:00 +0100570
571 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
572 g_return_val_if_fail (flag != NULL, FALSE);
573
574 /* never set up, e.g. in tests */
Richard Hughesb333e002021-04-01 10:40:02 +0100575 if (priv->ctx == NULL)
Richard Hughes19841802019-09-10 16:48:00 +0100576 return FALSE;
577
578 /* search each hwid */
Richard Hughesb333e002021-04-01 10:40:02 +0100579 guids = fu_context_get_hwid_guids (priv->ctx);
580 for (guint i = 0; i < guids->len; i++) {
581 const gchar *guid = g_ptr_array_index (guids, i);
Richard Hughes19841802019-09-10 16:48:00 +0100582 const gchar *value;
Richard Hughes19841802019-09-10 16:48:00 +0100583
584 /* does prefixed quirk exist */
Richard Hughesb333e002021-04-01 10:40:02 +0100585 value = fu_context_lookup_quirk_by_id (priv->ctx, guid, FU_QUIRKS_FLAGS);
Richard Hughes19841802019-09-10 16:48:00 +0100586 if (value != NULL) {
Richard Hughesb333e002021-04-01 10:40:02 +0100587 g_auto(GStrv) values = g_strsplit (value, ",", -1);
588 if (g_strv_contains ((const gchar * const *) values, flag))
Richard Hughes19841802019-09-10 16:48:00 +0100589 return TRUE;
590 }
591 }
592 return FALSE;
593}
594
595/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100596 * fu_plugin_check_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100597 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100598 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100599 *
600 * Checks to see if a specific device GUID is supported, i.e. available in the
601 * AppStream metadata.
602 *
Richard Hughes4eada342017-10-03 21:20:32 +0100603 * Returns: %TRUE if the device is supported.
604 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100605 * Since: 1.0.0
606 **/
Richard Hughesd8a8d5e2019-10-08 13:05:02 +0100607static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100608fu_plugin_check_supported (FuPlugin *self, const gchar *guid)
Richard Hughes1354ea92017-09-19 15:58:31 +0100609{
Richard Hughesaabdc372018-11-14 10:11:08 +0000610 gboolean retval = FALSE;
611 g_signal_emit (self, signals[SIGNAL_CHECK_SUPPORTED], 0, guid, &retval);
612 return retval;
Richard Hughes1354ea92017-09-19 15:58:31 +0100613}
614
615/**
Richard Hughesb333e002021-04-01 10:40:02 +0100616 * fu_plugin_get_context:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100617 * @self: A #FuPlugin
Richard Hughesd7704d42017-08-08 20:29:09 +0100618 *
Richard Hughesb333e002021-04-01 10:40:02 +0100619 * Gets the context for a plugin.
Richard Hughesd7704d42017-08-08 20:29:09 +0100620 *
Richard Hughesb333e002021-04-01 10:40:02 +0100621 * Returns: (transfer none): a #FuContext or %NULL if not set
Richard Hughes4eada342017-10-03 21:20:32 +0100622 *
Richard Hughesb333e002021-04-01 10:40:02 +0100623 * Since: 1.6.0
Richard Hughesd7704d42017-08-08 20:29:09 +0100624 **/
Richard Hughesb333e002021-04-01 10:40:02 +0100625FuContext *
626fu_plugin_get_context (FuPlugin *self)
Richard Hughesd7704d42017-08-08 20:29:09 +0100627{
Richard Hughes12724852018-09-04 13:53:44 +0100628 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb333e002021-04-01 10:40:02 +0100629 return priv->ctx;
Richard Hughes49e5e052017-09-03 12:15:41 +0100630}
631
Richard Hughes4b303802019-10-04 13:22:51 +0100632static gboolean
633fu_plugin_device_attach (FuPlugin *self, FuDevice *device, GError **error)
634{
635 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes4b303802019-10-04 13:22:51 +0100636 locker = fu_device_locker_new (device, error);
637 if (locker == NULL)
638 return FALSE;
639 return fu_device_attach (device, error);
640}
641
642static gboolean
643fu_plugin_device_detach (FuPlugin *self, FuDevice *device, GError **error)
644{
645 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughes4b303802019-10-04 13:22:51 +0100646 locker = fu_device_locker_new (device, error);
647 if (locker == NULL)
648 return FALSE;
649 return fu_device_detach (device, error);
650}
651
652static gboolean
Richard Hughes4b303802019-10-04 13:22:51 +0100653fu_plugin_device_activate (FuPlugin *self, FuDevice *device, GError **error)
654{
655 g_autoptr(FuDeviceLocker) locker = NULL;
656 locker = fu_device_locker_new (device, error);
657 if (locker == NULL)
658 return FALSE;
659 return fu_device_activate (device, error);
660}
661
662static gboolean
663fu_plugin_device_write_firmware (FuPlugin *self, FuDevice *device,
664 GBytes *fw, FwupdInstallFlags flags,
665 GError **error)
666{
667 g_autoptr(FuDeviceLocker) locker = NULL;
668 locker = fu_device_locker_new (device, error);
669 if (locker == NULL)
670 return FALSE;
Richard Hughes1a612582020-09-29 19:39:38 +0100671
672 /* back the old firmware up to /var/lib/fwupd */
673 if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_BACKUP_BEFORE_INSTALL)) {
674 g_autoptr(GBytes) fw_old = NULL;
675 g_autofree gchar *path = NULL;
676 g_autofree gchar *fn = NULL;
677 g_autofree gchar *localstatedir = NULL;
678
679 fw_old = fu_device_dump_firmware (device, error);
680 if (fw_old == NULL) {
681 g_prefix_error (error, "failed to backup old firmware: ");
682 return FALSE;
683 }
684 localstatedir = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
685 fn = g_strdup_printf ("%s.bin", fu_device_get_version (device));
686 path = g_build_filename (localstatedir,
687 "backup",
688 fu_device_get_id (device),
689 fu_device_get_serial (device) != NULL ?
690 fu_device_get_serial (device) :
691 "default",
692 fn, NULL);
693 if (!fu_common_set_contents_bytes (path, fw_old, error))
694 return FALSE;
695 }
696
Richard Hughes4b303802019-10-04 13:22:51 +0100697 return fu_device_write_firmware (device, fw, flags, error);
698}
699
Richard Hughes7f677212019-10-05 16:19:40 +0100700static gboolean
701fu_plugin_device_read_firmware (FuPlugin *self, FuDevice *device, GError **error)
702{
703 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughesf0eb0912019-10-10 11:37:22 +0100704 g_autoptr(FuFirmware) firmware = NULL;
Richard Hughes7f677212019-10-05 16:19:40 +0100705 g_autoptr(GBytes) fw = NULL;
706 GChecksumType checksum_types[] = {
707 G_CHECKSUM_SHA1,
708 G_CHECKSUM_SHA256,
709 0 };
710 locker = fu_device_locker_new (device, error);
711 if (locker == NULL)
712 return FALSE;
713 if (!fu_device_detach (device, error))
714 return FALSE;
Richard Hughesf0eb0912019-10-10 11:37:22 +0100715 firmware = fu_device_read_firmware (device, error);
716 if (firmware == NULL) {
717 g_autoptr(GError) error_local = NULL;
718 if (!fu_device_attach (device, &error_local))
719 g_debug ("ignoring attach failure: %s", error_local->message);
720 g_prefix_error (error, "failed to read firmware: ");
721 return FALSE;
722 }
723 fw = fu_firmware_write (firmware, error);
Richard Hughes7f677212019-10-05 16:19:40 +0100724 if (fw == NULL) {
725 g_autoptr(GError) error_local = NULL;
726 if (!fu_device_attach (device, &error_local))
Richard Hughesf0eb0912019-10-10 11:37:22 +0100727 g_debug ("ignoring attach failure: %s", error_local->message);
728 g_prefix_error (error, "failed to write firmware: ");
Richard Hughes7f677212019-10-05 16:19:40 +0100729 return FALSE;
730 }
731 for (guint i = 0; checksum_types[i] != 0; i++) {
732 g_autofree gchar *hash = NULL;
733 hash = g_compute_checksum_for_bytes (checksum_types[i], fw);
734 fu_device_add_checksum (device, hash);
735 }
736 return fu_device_attach (device, error);
737}
738
Mario Limonciello1a680f32019-11-25 19:44:53 -0600739/**
740 * fu_plugin_runner_startup:
741 * @self: a #FuPlugin
742 * @error: a #GError or NULL
743 *
744 * Runs the startup routine for the plugin
745 *
746 * Returns: #TRUE for success, #FALSE for failure
747 *
748 * Since: 0.8.0
749 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000750gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100751fu_plugin_runner_startup (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000752{
Richard Hughes12724852018-09-04 13:53:44 +0100753 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000754 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000755 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000756
757 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100758 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughesd0905142016-03-13 09:46:49 +0000759 return TRUE;
760
Richard Hughes639da472018-01-06 22:35:04 +0000761 /* no object loaded */
762 if (priv->module == NULL)
763 return TRUE;
764
Richard Hughesd0905142016-03-13 09:46:49 +0000765 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000766 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
767 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000768 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100769 g_debug ("startup(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000770 if (!func (self, &error_local)) {
771 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -0500772 g_critical ("unset plugin error in startup(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100773 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000774 g_set_error_literal (&error_local,
775 FWUPD_ERROR,
776 FWUPD_ERROR_INTERNAL,
777 "unspecified error");
778 }
779 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
780 "failed to startup using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100781 fu_plugin_get_name (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000782 return FALSE;
783 }
784 return TRUE;
785}
786
787static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100788fu_plugin_runner_device_generic (FuPlugin *self, FuDevice *device,
Richard Hughes4b303802019-10-04 13:22:51 +0100789 const gchar *symbol_name,
790 FuPluginDeviceFunc device_func,
791 GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000792{
Richard Hughes12724852018-09-04 13:53:44 +0100793 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000794 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000795 g_autoptr(GError) error_local = NULL;
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000796
797 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100798 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000799 return TRUE;
800
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000801 /* no object loaded */
802 if (priv->module == NULL)
803 return TRUE;
804
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000805 /* optional */
806 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
Richard Hughes4b303802019-10-04 13:22:51 +0100807 if (func == NULL) {
808 if (device_func != NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -0500809 g_debug ("running superclassed %s(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100810 symbol_name + 10, fu_plugin_get_name (self));
Richard Hughes4b303802019-10-04 13:22:51 +0100811 return device_func (self, device, error);
812 }
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000813 return TRUE;
Richard Hughes4b303802019-10-04 13:22:51 +0100814 }
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100815 g_debug ("%s(%s)", symbol_name + 10, fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000816 if (!func (self, device, &error_local)) {
817 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -0500818 g_critical ("unset plugin error in %s(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100819 fu_plugin_get_name (self), symbol_name + 10);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000820 g_set_error_literal (&error_local,
821 FWUPD_ERROR,
822 FWUPD_ERROR_INTERNAL,
823 "unspecified error");
824 }
825 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
826 "failed to %s using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100827 symbol_name + 10, fu_plugin_get_name (self));
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000828 return FALSE;
829 }
830 return TRUE;
831}
832
Richard Hughesdbd8c762018-06-15 20:31:40 +0100833static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100834fu_plugin_runner_flagged_device_generic (FuPlugin *self, FwupdInstallFlags flags,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500835 FuDevice *device,
836 const gchar *symbol_name, GError **error)
837{
Richard Hughes12724852018-09-04 13:53:44 +0100838 FuPluginPrivate *priv = GET_PRIVATE (self);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500839 FuPluginFlaggedDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000840 g_autoptr(GError) error_local = NULL;
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500841
842 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100843 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500844 return TRUE;
845
846 /* no object loaded */
847 if (priv->module == NULL)
848 return TRUE;
849
850 /* optional */
851 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
852 if (func == NULL)
853 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100854 g_debug ("%s(%s)", symbol_name + 10, fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000855 if (!func (self, flags, device, &error_local)) {
856 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -0500857 g_critical ("unset plugin error in %s(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100858 fu_plugin_get_name (self), symbol_name + 10);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000859 g_set_error_literal (&error_local,
860 FWUPD_ERROR,
861 FWUPD_ERROR_INTERNAL,
862 "unspecified error");
863 }
864 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
865 "failed to %s using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100866 symbol_name + 10, fu_plugin_get_name (self));
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500867 return FALSE;
868 }
869 return TRUE;
870
871}
872
873static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100874fu_plugin_runner_device_array_generic (FuPlugin *self, GPtrArray *devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +0100875 const gchar *symbol_name, GError **error)
876{
Richard Hughes12724852018-09-04 13:53:44 +0100877 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesdbd8c762018-06-15 20:31:40 +0100878 FuPluginDeviceArrayFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000879 g_autoptr(GError) error_local = NULL;
Richard Hughesdbd8c762018-06-15 20:31:40 +0100880
881 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100882 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughesdbd8c762018-06-15 20:31:40 +0100883 return TRUE;
884
885 /* no object loaded */
886 if (priv->module == NULL)
887 return TRUE;
888
889 /* optional */
890 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
891 if (func == NULL)
892 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100893 g_debug ("%s(%s)", symbol_name + 10, fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000894 if (!func (self, devices, &error_local)) {
895 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -0500896 g_critical ("unset plugin error in for %s(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100897 fu_plugin_get_name (self), symbol_name + 10);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000898 g_set_error_literal (&error_local,
899 FWUPD_ERROR,
900 FWUPD_ERROR_INTERNAL,
901 "unspecified error");
902 }
903 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
904 "failed to %s using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100905 symbol_name + 10, fu_plugin_get_name (self));
Richard Hughesdbd8c762018-06-15 20:31:40 +0100906 return FALSE;
907 }
908 return TRUE;
909}
910
Mario Limonciello1a680f32019-11-25 19:44:53 -0600911/**
912 * fu_plugin_runner_coldplug:
913 * @self: a #FuPlugin
914 * @error: a #GError or NULL
915 *
916 * Runs the coldplug routine for the plugin
917 *
918 * Returns: #TRUE for success, #FALSE for failure
919 *
920 * Since: 0.8.0
921 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000922gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100923fu_plugin_runner_coldplug (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000924{
Richard Hughes12724852018-09-04 13:53:44 +0100925 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000926 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000927 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000928
929 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100930 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughesd0905142016-03-13 09:46:49 +0000931 return TRUE;
932
Daniel Campello0b9b7ec2021-04-08 16:32:25 -0600933 /* no HwId */
934 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_REQUIRE_HWID))
935 return TRUE;
936
Richard Hughes639da472018-01-06 22:35:04 +0000937 /* no object loaded */
938 if (priv->module == NULL)
939 return TRUE;
940
Richard Hughesd0905142016-03-13 09:46:49 +0000941 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000942 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
943 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000944 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100945 g_debug ("coldplug(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000946 if (!func (self, &error_local)) {
947 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -0500948 g_critical ("unset plugin error in coldplug(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100949 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000950 g_set_error_literal (&error_local,
951 FWUPD_ERROR,
952 FWUPD_ERROR_INTERNAL,
953 "unspecified error");
954 }
955 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100956 "failed to coldplug using %s: ", fu_plugin_get_name (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000957 return FALSE;
958 }
959 return TRUE;
960}
961
Mario Limonciello1a680f32019-11-25 19:44:53 -0600962/**
Mario Limonciello1a680f32019-11-25 19:44:53 -0600963 * fu_plugin_runner_coldplug_prepare:
964 * @self: a #FuPlugin
965 * @error: a #GError or NULL
966 *
967 * Runs the coldplug_prepare routine for the plugin
968 *
969 * Returns: #TRUE for success, #FALSE for failure
970 *
971 * Since: 0.8.0
972 **/
Richard Hughes2de8f132018-01-17 09:12:02 +0000973gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100974fu_plugin_runner_coldplug_prepare (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +0000975{
Richard Hughes12724852018-09-04 13:53:44 +0100976 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +0000977 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000978 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +0000979
980 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100981 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes46487c92017-01-07 21:26:34 +0000982 return TRUE;
983
Richard Hughes639da472018-01-06 22:35:04 +0000984 /* no object loaded */
985 if (priv->module == NULL)
986 return TRUE;
987
Richard Hughes46487c92017-01-07 21:26:34 +0000988 /* optional */
989 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
990 if (func == NULL)
991 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100992 g_debug ("coldplug_prepare(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000993 if (!func (self, &error_local)) {
994 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -0500995 g_critical ("unset plugin error in coldplug_prepare(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +0100996 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000997 g_set_error_literal (&error_local,
998 FWUPD_ERROR,
999 FWUPD_ERROR_INTERNAL,
1000 "unspecified error");
1001 }
1002 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1003 "failed to coldplug_prepare using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001004 fu_plugin_get_name (self));
Richard Hughes46487c92017-01-07 21:26:34 +00001005 return FALSE;
1006 }
1007 return TRUE;
1008}
1009
Mario Limonciello1a680f32019-11-25 19:44:53 -06001010/**
1011 * fu_plugin_runner_coldplug_cleanup:
1012 * @self: a #FuPlugin
1013 * @error: a #GError or NULL
1014 *
1015 * Runs the coldplug_cleanup routine for the plugin
1016 *
1017 * Returns: #TRUE for success, #FALSE for failure
1018 *
1019 * Since: 0.8.0
1020 **/
Richard Hughes46487c92017-01-07 21:26:34 +00001021gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001022fu_plugin_runner_coldplug_cleanup (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001023{
Richard Hughes12724852018-09-04 13:53:44 +01001024 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001025 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001026 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001027
1028 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001029 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes46487c92017-01-07 21:26:34 +00001030 return TRUE;
1031
Richard Hughes639da472018-01-06 22:35:04 +00001032 /* no object loaded */
1033 if (priv->module == NULL)
1034 return TRUE;
1035
Richard Hughes46487c92017-01-07 21:26:34 +00001036 /* optional */
1037 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1038 if (func == NULL)
1039 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001040 g_debug ("coldplug_cleanup(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001041 if (!func (self, &error_local)) {
1042 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001043 g_critical ("unset plugin error in coldplug_cleanup(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001044 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001045 g_set_error_literal (&error_local,
1046 FWUPD_ERROR,
1047 FWUPD_ERROR_INTERNAL,
1048 "unspecified error");
1049 }
1050 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1051 "failed to coldplug_cleanup using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001052 fu_plugin_get_name (self));
Richard Hughes46487c92017-01-07 21:26:34 +00001053 return FALSE;
1054 }
1055 return TRUE;
1056}
1057
Mario Limonciello1a680f32019-11-25 19:44:53 -06001058/**
1059 * fu_plugin_runner_composite_prepare:
1060 * @self: a #FuPlugin
Richard Hughesa0d81c72019-11-27 11:41:54 +00001061 * @devices: (element-type FuDevice): a #GPtrArray of devices
Mario Limonciello1a680f32019-11-25 19:44:53 -06001062 * @error: a #GError or NULL
1063 *
1064 * Runs the composite_prepare routine for the plugin
1065 *
1066 * Returns: #TRUE for success, #FALSE for failure
1067 *
1068 * Since: 1.0.9
1069 **/
Richard Hughes46487c92017-01-07 21:26:34 +00001070gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001071fu_plugin_runner_composite_prepare (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001072{
Richard Hughes12724852018-09-04 13:53:44 +01001073 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001074 "fu_plugin_composite_prepare",
1075 error);
1076}
1077
Mario Limonciello1a680f32019-11-25 19:44:53 -06001078/**
1079 * fu_plugin_runner_composite_cleanup:
1080 * @self: a #FuPlugin
Richard Hughesa0d81c72019-11-27 11:41:54 +00001081 * @devices: (element-type FuDevice): a #GPtrArray of devices
Mario Limonciello1a680f32019-11-25 19:44:53 -06001082 * @error: a #GError or NULL
1083 *
1084 * Runs the composite_cleanup routine for the plugin
1085 *
1086 * Returns: #TRUE for success, #FALSE for failure
1087 *
1088 * Since: 1.0.9
1089 **/
Richard Hughesdbd8c762018-06-15 20:31:40 +01001090gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001091fu_plugin_runner_composite_cleanup (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001092{
Richard Hughes12724852018-09-04 13:53:44 +01001093 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001094 "fu_plugin_composite_cleanup",
1095 error);
1096}
1097
Mario Limonciello1a680f32019-11-25 19:44:53 -06001098/**
1099 * fu_plugin_runner_update_prepare:
1100 * @self: a #FuPlugin
Richard Hughes2bbb7d22020-11-30 09:18:45 +00001101 * @flags: #FwupdInstallFlags
1102 * @device: a #FuDevice
Mario Limonciello1a680f32019-11-25 19:44:53 -06001103 * @error: a #GError or NULL
1104 *
1105 * Runs the update_prepare routine for the plugin
1106 *
1107 * Returns: #TRUE for success, #FALSE for failure
1108 *
1109 * Since: 1.1.2
1110 **/
Richard Hughesdbd8c762018-06-15 20:31:40 +01001111gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001112fu_plugin_runner_update_prepare (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001113 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001114{
Richard Hughes12724852018-09-04 13:53:44 +01001115 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001116 "fu_plugin_update_prepare",
1117 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001118}
1119
Mario Limonciello1a680f32019-11-25 19:44:53 -06001120/**
1121 * fu_plugin_runner_update_cleanup:
1122 * @self: a #FuPlugin
Richard Hughes2bbb7d22020-11-30 09:18:45 +00001123 * @flags: #FwupdInstallFlags
1124 * @device: a #FuDevice
Mario Limonciello1a680f32019-11-25 19:44:53 -06001125 * @error: a #GError or NULL
1126 *
1127 * Runs the update_cleanup routine for the plugin
1128 *
1129 * Returns: #TRUE for success, #FALSE for failure
1130 *
1131 * Since: 1.1.2
1132 **/
Richard Hughes7b8b2022016-12-12 16:15:03 +00001133gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001134fu_plugin_runner_update_cleanup (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001135 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001136{
Richard Hughes12724852018-09-04 13:53:44 +01001137 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001138 "fu_plugin_update_cleanup",
1139 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001140}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001141
Mario Limonciello1a680f32019-11-25 19:44:53 -06001142/**
1143 * fu_plugin_runner_update_attach:
1144 * @self: a #FuPlugin
1145 * @device: a #FuDevice
1146 * @error: a #GError or NULL
1147 *
1148 * Runs the update_attach routine for the plugin
1149 *
1150 * Returns: #TRUE for success, #FALSE for failure
1151 *
1152 * Since: 1.1.2
1153 **/
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001154gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001155fu_plugin_runner_update_attach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001156{
Richard Hughes12724852018-09-04 13:53:44 +01001157 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001158 "fu_plugin_update_attach",
1159 fu_plugin_device_attach,
1160 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001161}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001162
Mario Limonciello1a680f32019-11-25 19:44:53 -06001163/**
1164 * fu_plugin_runner_update_detach:
1165 * @self: a #FuPlugin
1166 * @device: A #FuDevice
1167 * @error: a #GError or NULL
1168 *
1169 * Runs the update_detach routine for the plugin
1170 *
1171 * Returns: #TRUE for success, #FALSE for failure
1172 *
1173 * Since: 1.1.2
1174 **/
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001175gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001176fu_plugin_runner_update_detach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001177{
Richard Hughes12724852018-09-04 13:53:44 +01001178 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001179 "fu_plugin_update_detach",
1180 fu_plugin_device_detach,
1181 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001182}
1183
Mario Limonciello1a680f32019-11-25 19:44:53 -06001184/**
1185 * fu_plugin_runner_update_reload:
1186 * @self: a #FuPlugin
Richard Hughes2bbb7d22020-11-30 09:18:45 +00001187 * @device: A #FuDevice
Mario Limonciello1a680f32019-11-25 19:44:53 -06001188 * @error: a #GError or NULL
1189 *
1190 * Runs reload routine for a device
1191 *
1192 * Returns: #TRUE for success, #FALSE for failure
1193 *
1194 * Since: 1.1.2
1195 **/
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001196gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001197fu_plugin_runner_update_reload (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001198{
Richard Hughes42f33df2019-10-05 20:52:33 +01001199 g_autoptr(FuDeviceLocker) locker = NULL;
1200
1201 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001202 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes42f33df2019-10-05 20:52:33 +01001203 return TRUE;
1204
1205 /* no object loaded */
1206 locker = fu_device_locker_new (device, error);
1207 if (locker == NULL)
1208 return FALSE;
1209 return fu_device_reload (device, error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001210}
1211
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001212/**
Richard Hughes196c6c62020-05-11 19:42:47 +01001213 * fu_plugin_runner_add_security_attrs:
1214 * @self: a #FuPlugin
Richard Hughes3ecd22c2020-05-19 20:13:47 +01001215 * @attrs: a #FuSecurityAttrs
Richard Hughes196c6c62020-05-11 19:42:47 +01001216 *
Richard Hughes3ecd22c2020-05-19 20:13:47 +01001217 * Runs the `add_security_attrs()` routine for the plugin
Richard Hughes196c6c62020-05-11 19:42:47 +01001218 *
1219 * Since: 1.5.0
1220 **/
Richard Hughesf58ac732020-05-12 15:23:44 +01001221void
1222fu_plugin_runner_add_security_attrs (FuPlugin *self, FuSecurityAttrs *attrs)
Richard Hughes196c6c62020-05-11 19:42:47 +01001223{
Richard Hughesf58ac732020-05-12 15:23:44 +01001224 FuPluginPrivate *priv = GET_PRIVATE (self);
1225 FuPluginSecurityAttrsFunc func = NULL;
1226 const gchar *symbol_name = "fu_plugin_add_security_attrs";
1227
1228 /* no object loaded */
1229 if (priv->module == NULL)
1230 return;
1231
1232 /* optional, but gets called even for disabled plugins */
1233 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1234 if (func == NULL)
1235 return;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001236 g_debug ("%s(%s)", symbol_name + 10, fu_plugin_get_name (self));
Richard Hughesf58ac732020-05-12 15:23:44 +01001237 func (self, attrs);
Richard Hughes196c6c62020-05-11 19:42:47 +01001238}
1239
1240/**
Richard Hughes989acf12019-10-05 20:16:47 +01001241 * fu_plugin_set_device_gtype:
1242 * @self: a #FuPlugin
1243 * @device_gtype: a #GType `FU_TYPE_DEVICE`
1244 *
1245 * Sets the device #GType which is used when creating devices.
1246 *
Richard Hughes525f71f2021-02-09 14:52:47 +00001247 * If this method is used then fu_plugin_backend_device_added() is not called, and
Richard Hughes989acf12019-10-05 20:16:47 +01001248 * instead the object is created in the daemon for the plugin.
1249 *
1250 * Plugins can use this method only in fu_plugin_init()
1251 *
1252 * Since: 1.3.3
1253 **/
1254void
1255fu_plugin_set_device_gtype (FuPlugin *self, GType device_gtype)
1256{
1257 FuPluginPrivate *priv = GET_PRIVATE (self);
1258 priv->device_gtype = device_gtype;
1259}
1260
Richard Hughes9f71fe32021-01-03 20:35:36 +00001261static gchar *
1262fu_common_string_uncamelcase (const gchar *str)
1263{
1264 GString *tmp = g_string_new (NULL);
1265 for (guint i = 0; str[i] != '\0'; i++) {
1266 if (g_ascii_islower (str[i]) ||
1267 g_ascii_isdigit (str[i])) {
1268 g_string_append_c (tmp, str[i]);
1269 continue;
1270 }
1271 if (i > 0)
1272 g_string_append_c (tmp, '-');
1273 g_string_append_c (tmp, g_ascii_tolower (str[i]));
1274 }
1275 return g_string_free (tmp, FALSE);
1276}
1277
Mario Limonciello1a680f32019-11-25 19:44:53 -06001278/**
1279 * fu_plugin_add_firmware_gtype:
1280 * @self: a #FuPlugin
Richard Hughes9f71fe32021-01-03 20:35:36 +00001281 * @id: (nullable): An optional string describing the type, e.g. "ihex"
1282 * @gtype: a #GType e.g. `FU_TYPE_FOO_FIRMWARE`
Mario Limonciello1a680f32019-11-25 19:44:53 -06001283 *
Richard Hughes9f71fe32021-01-03 20:35:36 +00001284 * Adds a firmware #GType which is used when creating devices. If @id is not
1285 * specified then it is guessed using the #GType name.
1286 *
Mario Limonciello1a680f32019-11-25 19:44:53 -06001287 * Plugins can use this method only in fu_plugin_init()
1288 *
1289 * Since: 1.3.3
1290 **/
Richard Hughes95c98a92019-10-22 16:03:15 +01001291void
1292fu_plugin_add_firmware_gtype (FuPlugin *self, const gchar *id, GType gtype)
1293{
Richard Hughesb333e002021-04-01 10:40:02 +01001294 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9f71fe32021-01-03 20:35:36 +00001295 g_autofree gchar *id_safe = NULL;
1296 if (id != NULL) {
1297 id_safe = g_strdup (id);
1298 } else {
Richard Hughesebb10a52021-01-05 13:34:39 +00001299 g_autoptr(GString) str = g_string_new (g_type_name (gtype));
Richard Hughes9f71fe32021-01-03 20:35:36 +00001300 if (g_str_has_prefix (str->str, "Fu"))
1301 g_string_erase (str, 0, 2);
1302 fu_common_string_replace (str, "Firmware", "");
1303 id_safe = fu_common_string_uncamelcase (str->str);
1304 }
Richard Hughesb333e002021-04-01 10:40:02 +01001305 fu_context_add_firmware_gtype (priv->ctx, id_safe, gtype);
Richard Hughes95c98a92019-10-22 16:03:15 +01001306}
1307
Richard Hughes989acf12019-10-05 20:16:47 +01001308static gboolean
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001309fu_plugin_check_supported_device (FuPlugin *self, FuDevice *device)
1310{
1311 GPtrArray *instance_ids = fu_device_get_instance_ids (device);
1312 for (guint i = 0; i < instance_ids->len; i++) {
1313 const gchar *instance_id = g_ptr_array_index (instance_ids, i);
1314 g_autofree gchar *guid = fwupd_guid_hash_string (instance_id);
1315 if (fu_plugin_check_supported (self, guid))
1316 return TRUE;
1317 }
1318 return FALSE;
1319}
1320
1321static gboolean
Richard Hughes525f71f2021-02-09 14:52:47 +00001322fu_plugin_backend_device_added (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes989acf12019-10-05 20:16:47 +01001323{
1324 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001325 GType device_gtype = fu_device_get_specialized_gtype (FU_DEVICE (device));
Richard Hughes989acf12019-10-05 20:16:47 +01001326 g_autoptr(FuDevice) dev = NULL;
1327 g_autoptr(FuDeviceLocker) locker = NULL;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001328
1329 /* fall back to plugin default */
1330 if (device_gtype == G_TYPE_INVALID)
1331 device_gtype = priv->device_gtype;
1332
1333 /* create new device and incorporate existing properties */
1334 dev = g_object_new (device_gtype, NULL);
1335 fu_device_incorporate (dev, FU_DEVICE (device));
Richard Hughes0f66a022020-02-19 18:54:38 +00001336 if (!fu_plugin_runner_device_created (self, dev, error))
1337 return FALSE;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001338
1339 /* there are a lot of different devices that match, but not all respond
1340 * well to opening -- so limit some ones with issued updates */
Richard Hughescf100292021-01-04 10:36:37 +00001341 if (fu_device_has_internal_flag (dev, FU_DEVICE_INTERNAL_FLAG_ONLY_SUPPORTED)) {
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001342 if (!fu_device_probe (dev, error))
1343 return FALSE;
1344 fu_device_convert_instance_ids (dev);
1345 if (!fu_plugin_check_supported_device (self, dev)) {
1346 g_autofree gchar *guids = fu_device_get_guids_as_str (dev);
1347 g_debug ("%s has no updates, so ignoring device", guids);
1348 return TRUE;
1349 }
1350 }
1351
1352 /* open and add */
1353 locker = fu_device_locker_new (dev, error);
1354 if (locker == NULL)
1355 return FALSE;
1356 fu_plugin_device_add (self, dev);
Richard Hughes6a078702020-05-09 20:36:33 +01001357 fu_plugin_runner_device_added (self, dev);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001358 return TRUE;
1359}
1360
Mario Limonciello1a680f32019-11-25 19:44:53 -06001361/**
Richard Hughes525f71f2021-02-09 14:52:47 +00001362 * fu_plugin_runner_backend_device_added:
1363 * @self: a #FuPlugin
1364 * @device: a #FuDevice
1365 * @error: a #GError or NULL
1366 *
1367 * Call the backend_device_added routine for the plugin
1368 *
1369 * Returns: #TRUE for success, #FALSE for failure
1370 *
1371 * Since: 1.5.6
1372 **/
1373gboolean
1374fu_plugin_runner_backend_device_added (FuPlugin *self, FuDevice *device, GError **error)
1375{
Richard Hughes12724852018-09-04 13:53:44 +01001376 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes525f71f2021-02-09 14:52:47 +00001377 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001378 g_autoptr(GError) error_local = NULL;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001379
Richard Hughes6a489a92020-12-22 10:32:06 +00001380 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
Richard Hughes525f71f2021-02-09 14:52:47 +00001381 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
Richard Hughes6a489a92020-12-22 10:32:06 +00001382 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1383
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001384 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001385 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001386 return TRUE;
1387
1388 /* no object loaded */
1389 if (priv->module == NULL)
1390 return TRUE;
1391
1392 /* optional */
Richard Hughes525f71f2021-02-09 14:52:47 +00001393 g_module_symbol (priv->module, "fu_plugin_backend_device_added", (gpointer *) &func);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001394 if (func == NULL) {
1395 if (priv->device_gtype != G_TYPE_INVALID ||
Richard Hughes525f71f2021-02-09 14:52:47 +00001396 fu_device_get_specialized_gtype (device) != G_TYPE_INVALID) {
1397 return fu_plugin_backend_device_added (self, device, error);
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001398 }
Richard Hughesa21e0252020-12-01 12:21:33 +00001399 g_set_error_literal (error,
1400 FWUPD_ERROR,
1401 FWUPD_ERROR_INTERNAL,
1402 "No device GType set");
1403 return FALSE;
Richard Hughesd8a8d5e2019-10-08 13:05:02 +01001404 }
Richard Hughes525f71f2021-02-09 14:52:47 +00001405 g_debug ("backend_device_added(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001406 if (!func (self, device, &error_local)) {
1407 if (error_local == NULL) {
Richard Hughes525f71f2021-02-09 14:52:47 +00001408 g_critical ("unset plugin error in backend_device_added(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001409 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001410 g_set_error_literal (&error_local,
1411 FWUPD_ERROR,
1412 FWUPD_ERROR_INTERNAL,
1413 "unspecified error");
1414 }
1415 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1416 "failed to add device using on %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001417 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001418 return FALSE;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001419 }
1420 return TRUE;
1421}
1422
Mario Limonciello1a680f32019-11-25 19:44:53 -06001423/**
Richard Hughes525f71f2021-02-09 14:52:47 +00001424 * fu_plugin_runner_backend_device_changed:
1425 * @self: a #FuPlugin
1426 * @device: a #FuDevice
1427 * @error: a #GError or NULL
1428 *
1429 * Call the backend_device_changed routine for the plugin
1430 *
1431 * Returns: #TRUE for success, #FALSE for failure
1432 *
1433 * Since: 1.5.6
1434 **/
1435gboolean
1436fu_plugin_runner_backend_device_changed (FuPlugin *self, FuDevice *device, GError **error)
1437{
Richard Hughes5e952ce2019-08-26 11:09:46 +01001438 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes525f71f2021-02-09 14:52:47 +00001439 FuPluginDeviceFunc func = NULL;
Richard Hughes5e952ce2019-08-26 11:09:46 +01001440 g_autoptr(GError) error_local = NULL;
1441
Richard Hughes6a489a92020-12-22 10:32:06 +00001442 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
Richard Hughes525f71f2021-02-09 14:52:47 +00001443 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
Richard Hughes6a489a92020-12-22 10:32:06 +00001444 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1445
Richard Hughes5e952ce2019-08-26 11:09:46 +01001446 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001447 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes5e952ce2019-08-26 11:09:46 +01001448 return TRUE;
1449
1450 /* no object loaded */
1451 if (priv->module == NULL)
1452 return TRUE;
1453
1454 /* optional */
Richard Hughes525f71f2021-02-09 14:52:47 +00001455 g_module_symbol (priv->module, "fu_plugin_backend_device_changed", (gpointer *) &func);
Mario Limonciello6d235142020-09-10 21:35:17 -05001456 if (func == NULL)
Richard Hughes5e952ce2019-08-26 11:09:46 +01001457 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001458 g_debug ("udev_device_changed(%s)", fu_plugin_get_name (self));
Richard Hughes5e952ce2019-08-26 11:09:46 +01001459 if (!func (self, device, &error_local)) {
1460 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001461 g_critical ("unset plugin error in udev_device_changed(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001462 fu_plugin_get_name (self));
Richard Hughes5e952ce2019-08-26 11:09:46 +01001463 g_set_error_literal (&error_local,
1464 FWUPD_ERROR,
1465 FWUPD_ERROR_INTERNAL,
1466 "unspecified error");
1467 }
1468 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1469 "failed to change device on %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001470 fu_plugin_get_name (self));
Richard Hughes5e952ce2019-08-26 11:09:46 +01001471 return FALSE;
1472 }
1473 return TRUE;
1474}
1475
Mario Limonciello1a680f32019-11-25 19:44:53 -06001476/**
Richard Hughes6a078702020-05-09 20:36:33 +01001477 * fu_plugin_runner_device_added:
1478 * @self: a #FuPlugin
1479 * @device: a #FuDevice
1480 *
1481 * Call the device_added routine for the plugin
1482 *
1483 * Since: 1.5.0
1484 **/
1485void
1486fu_plugin_runner_device_added (FuPlugin *self, FuDevice *device)
1487{
1488 FuPluginPrivate *priv = GET_PRIVATE (self);
1489 FuPluginDeviceRegisterFunc func = NULL;
1490
1491 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001492 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes6a078702020-05-09 20:36:33 +01001493 return;
1494 if (priv->module == NULL)
1495 return;
1496
1497 /* optional */
1498 g_module_symbol (priv->module, "fu_plugin_device_added", (gpointer *) &func);
1499 if (func == NULL)
1500 return;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001501 g_debug ("fu_plugin_device_added(%s)", fu_plugin_get_name (self));
Richard Hughes6a078702020-05-09 20:36:33 +01001502 func (self, device);
1503}
1504
1505/**
Mario Limonciello1a680f32019-11-25 19:44:53 -06001506 * fu_plugin_runner_device_removed:
1507 * @self: a #FuPlugin
1508 * @device: a #FuDevice
1509 *
1510 * Call the device_removed routine for the plugin
1511 *
1512 * Since: 1.1.2
1513 **/
Richard Hughese1fd34d2017-08-24 14:19:51 +01001514void
Richard Hughes12724852018-09-04 13:53:44 +01001515fu_plugin_runner_device_removed (FuPlugin *self, FuDevice *device)
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001516{
1517 g_autoptr(GError) error_local= NULL;
1518
Richard Hughes12724852018-09-04 13:53:44 +01001519 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes525f71f2021-02-09 14:52:47 +00001520 "fu_plugin_backend_device_removed",
Richard Hughes4b303802019-10-04 13:22:51 +01001521 NULL,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001522 &error_local))
1523 g_warning ("%s", error_local->message);
1524}
1525
Mario Limonciello1a680f32019-11-25 19:44:53 -06001526/**
1527 * fu_plugin_runner_device_register:
1528 * @self: a #FuPlugin
1529 * @device: a #FuDevice
1530 *
1531 * Call the device_registered routine for the plugin
1532 *
1533 * Since: 0.9.7
1534 **/
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001535void
Richard Hughes12724852018-09-04 13:53:44 +01001536fu_plugin_runner_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +01001537{
Richard Hughes12724852018-09-04 13:53:44 +01001538 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001539 FuPluginDeviceRegisterFunc func = NULL;
1540
1541 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001542 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughese1fd34d2017-08-24 14:19:51 +01001543 return;
Richard Hughes34834102017-11-21 21:55:00 +00001544 if (priv->module == NULL)
1545 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001546
1547 /* optional */
1548 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1549 if (func != NULL) {
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001550 g_debug ("fu_plugin_device_registered(%s)", fu_plugin_get_name (self));
Richard Hughes12724852018-09-04 13:53:44 +01001551 func (self, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001552 }
1553}
1554
Mario Limonciello1a680f32019-11-25 19:44:53 -06001555/**
Richard Hughes0f66a022020-02-19 18:54:38 +00001556 * fu_plugin_runner_device_created:
1557 * @self: a #FuPlugin
1558 * @device: a #FuDevice
1559 * @error: a #GError or NULL
1560 *
1561 * Call the device_created routine for the plugin
1562 *
1563 * Returns: #TRUE for success, #FALSE for failure
1564 *
Mario Limonciello96117d12020-02-28 10:17:56 -06001565 * Since: 1.4.0
Richard Hughes0f66a022020-02-19 18:54:38 +00001566 **/
1567gboolean
1568fu_plugin_runner_device_created (FuPlugin *self, FuDevice *device, GError **error)
1569{
1570 FuPluginPrivate *priv = GET_PRIVATE (self);
1571 FuPluginDeviceFunc func = NULL;
1572
Richard Hughes6a489a92020-12-22 10:32:06 +00001573 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
1574 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
1575 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1576
Richard Hughes0f66a022020-02-19 18:54:38 +00001577 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001578 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughes0f66a022020-02-19 18:54:38 +00001579 return TRUE;
1580 if (priv->module == NULL)
1581 return TRUE;
1582
1583 /* optional */
1584 g_module_symbol (priv->module, "fu_plugin_device_created", (gpointer *) &func);
1585 if (func == NULL)
1586 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001587 g_debug ("fu_plugin_device_created(%s)", fu_plugin_get_name (self));
Richard Hughes0f66a022020-02-19 18:54:38 +00001588 return func (self, device, error);
1589}
1590
1591/**
Mario Limonciello1a680f32019-11-25 19:44:53 -06001592 * fu_plugin_runner_verify:
1593 * @self: a #FuPlugin
1594 * @device: a #FuDevice
1595 * @flags: #FuPluginVerifyFlags
1596 * @error: A #GError or NULL
1597 *
1598 * Call into the plugin's verify routine
1599 *
1600 * Returns: #TRUE for success, #FALSE for failure
1601 *
1602 * Since: 0.8.0
1603 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00001604gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001605fu_plugin_runner_verify (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001606 FuDevice *device,
1607 FuPluginVerifyFlags flags,
1608 GError **error)
1609{
Richard Hughes12724852018-09-04 13:53:44 +01001610 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001611 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001612 GPtrArray *checksums;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001613 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001614
Richard Hughes6a489a92020-12-22 10:32:06 +00001615 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
1616 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
1617 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1618
Richard Hughescff38bc2016-12-12 12:03:37 +00001619 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001620 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughescff38bc2016-12-12 12:03:37 +00001621 return TRUE;
1622
Richard Hughes639da472018-01-06 22:35:04 +00001623 /* no object loaded */
1624 if (priv->module == NULL)
1625 return TRUE;
1626
Richard Hughescff38bc2016-12-12 12:03:37 +00001627 /* optional */
1628 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
Richard Hughes7f677212019-10-05 16:19:40 +01001629 if (func == NULL) {
Richard Hughes53de7e22021-02-23 14:28:28 +00001630 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_CAN_VERIFY)) {
1631 g_set_error (error,
1632 FWUPD_ERROR,
1633 FWUPD_ERROR_NOT_SUPPORTED,
1634 "device %s does not support verification",
1635 fu_device_get_id (device));
1636 return FALSE;
1637 }
Richard Hughes7f677212019-10-05 16:19:40 +01001638 return fu_plugin_device_read_firmware (self, device, error);
1639 }
Richard Hughes1812fc72018-12-14 11:37:54 +00001640
1641 /* clear any existing verification checksums */
1642 checksums = fu_device_get_checksums (device);
1643 g_ptr_array_set_size (checksums, 0);
1644
Richard Hughesc9223be2019-03-18 08:46:42 +00001645 /* run additional detach */
1646 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes42f33df2019-10-05 20:52:33 +01001647 "fu_plugin_update_detach",
Richard Hughes4b303802019-10-04 13:22:51 +01001648 fu_plugin_device_detach,
Richard Hughesc9223be2019-03-18 08:46:42 +00001649 error))
1650 return FALSE;
1651
Richard Hughes1812fc72018-12-14 11:37:54 +00001652 /* run vfunc */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001653 g_debug ("verify(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001654 if (!func (self, device, flags, &error_local)) {
Richard Hughesc9223be2019-03-18 08:46:42 +00001655 g_autoptr(GError) error_attach = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001656 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001657 g_critical ("unset plugin error in verify(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001658 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001659 g_set_error_literal (&error_local,
1660 FWUPD_ERROR,
1661 FWUPD_ERROR_INTERNAL,
1662 "unspecified error");
1663 }
1664 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1665 "failed to verify using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001666 fu_plugin_get_name (self));
Richard Hughesc9223be2019-03-18 08:46:42 +00001667 /* make the device "work" again, but don't prefix the error */
1668 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes42f33df2019-10-05 20:52:33 +01001669 "fu_plugin_update_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01001670 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00001671 &error_attach)) {
1672 g_warning ("failed to attach whilst aborting verify(): %s",
1673 error_attach->message);
1674 }
Richard Hughesd0905142016-03-13 09:46:49 +00001675 return FALSE;
1676 }
Richard Hughesc9223be2019-03-18 08:46:42 +00001677
1678 /* run optional attach */
1679 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes42f33df2019-10-05 20:52:33 +01001680 "fu_plugin_update_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01001681 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00001682 error))
1683 return FALSE;
1684
1685 /* success */
Richard Hughesd0905142016-03-13 09:46:49 +00001686 return TRUE;
1687}
1688
Mario Limonciello1a680f32019-11-25 19:44:53 -06001689/**
1690 * fu_plugin_runner_activate:
1691 * @self: a #FuPlugin
1692 * @device: a #FuDevice
1693 * @error: A #GError or NULL
1694 *
1695 * Call into the plugin's activate routine
1696 *
1697 * Returns: #TRUE for success, #FALSE for failure
1698 *
1699 * Since: 1.2.6
1700 **/
Richard Hughesd0905142016-03-13 09:46:49 +00001701gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001702fu_plugin_runner_activate (FuPlugin *self, FuDevice *device, GError **error)
1703{
1704 guint64 flags;
1705
Richard Hughes6a489a92020-12-22 10:32:06 +00001706 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
1707 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
1708 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1709
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001710 /* final check */
1711 flags = fu_device_get_flags (device);
1712 if ((flags & FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION) == 0) {
1713 g_set_error (error,
1714 FWUPD_ERROR,
1715 FWUPD_ERROR_NOT_SUPPORTED,
1716 "Device %s does not need activation",
1717 fu_device_get_id (device));
1718 return FALSE;
1719 }
1720
1721 /* run vfunc */
1722 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001723 "fu_plugin_activate",
1724 fu_plugin_device_activate,
1725 error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001726 return FALSE;
1727
1728 /* update with correct flags */
1729 fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION);
1730 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1731 return TRUE;
1732}
1733
Mario Limonciello1a680f32019-11-25 19:44:53 -06001734/**
1735 * fu_plugin_runner_unlock:
1736 * @self: a #FuPlugin
1737 * @device: a #FuDevice
1738 * @error: A #GError or NULL
1739 *
1740 * Call into the plugin's unlock routine
1741 *
1742 * Returns: #TRUE for success, #FALSE for failure
1743 *
1744 * Since: 0.8.0
1745 **/
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001746gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001747fu_plugin_runner_unlock (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001748{
Richard Hughescff38bc2016-12-12 12:03:37 +00001749 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001750
Richard Hughes6a489a92020-12-22 10:32:06 +00001751 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
1752 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
1753 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1754
Richard Hughescff38bc2016-12-12 12:03:37 +00001755 /* final check */
1756 flags = fu_device_get_flags (device);
1757 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1758 g_set_error (error,
1759 FWUPD_ERROR,
1760 FWUPD_ERROR_NOT_SUPPORTED,
1761 "Device %s is not locked",
1762 fu_device_get_id (device));
1763 return FALSE;
1764 }
1765
Richard Hughes9c4b5312017-11-14 11:34:53 +00001766 /* run vfunc */
Richard Hughes12724852018-09-04 13:53:44 +01001767 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001768 "fu_plugin_unlock",
1769 NULL,
1770 error))
Richard Hughes9c4b5312017-11-14 11:34:53 +00001771 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001772
1773 /* update with correct flags */
Richard Hughesed0af242021-02-22 21:27:16 +00001774 fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_LOCKED);
Richard Hughescff38bc2016-12-12 12:03:37 +00001775 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1776 return TRUE;
1777}
1778
Mario Limonciello1a680f32019-11-25 19:44:53 -06001779/**
1780 * fu_plugin_runner_update:
1781 * @self: a #FuPlugin
1782 * @device: a #FuDevice
1783 * @blob_fw: A #GBytes
1784 * @flags: A #FwupdInstallFlags
1785 * @error: A #GError or NULL
1786 *
1787 * Call into the plugin's update routine
1788 *
1789 * Returns: #TRUE for success, #FALSE for failure
1790 *
1791 * Since: 0.8.0
1792 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00001793gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001794fu_plugin_runner_update (FuPlugin *self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001795 FuDevice *device,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001796 GBytes *blob_fw,
1797 FwupdInstallFlags flags,
1798 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001799{
Richard Hughes12724852018-09-04 13:53:44 +01001800 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001801 FuPluginUpdateFunc update_func;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001802 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001803
Richard Hughes6a489a92020-12-22 10:32:06 +00001804 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
1805 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
1806 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1807
Richard Hughescff38bc2016-12-12 12:03:37 +00001808 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001809 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED)) {
Richard Hughes41c15482018-02-01 22:07:21 +00001810 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001811 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001812 }
Richard Hughesd0905142016-03-13 09:46:49 +00001813
Richard Hughes639da472018-01-06 22:35:04 +00001814 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001815 if (priv->module == NULL) {
1816 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001817 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001818 }
Richard Hughes639da472018-01-06 22:35:04 +00001819
Richard Hughesd0905142016-03-13 09:46:49 +00001820 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001821 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1822 if (update_func == NULL) {
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001823 g_debug ("superclassed write_firmware(%s)", fu_plugin_get_name (self));
Richard Hughes4b303802019-10-04 13:22:51 +01001824 return fu_plugin_device_write_firmware (self, device, blob_fw, flags, error);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001825 }
Richard Hughesd0905142016-03-13 09:46:49 +00001826
Richard Hughescff38bc2016-12-12 12:03:37 +00001827 /* online */
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001828 if (!update_func (self, device, blob_fw, flags, &error_local)) {
1829 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001830 g_critical ("unset plugin error in update(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001831 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001832 g_set_error_literal (&error_local,
Richard Hughes3c8ada32018-10-12 10:08:58 +01001833 FWUPD_ERROR,
1834 FWUPD_ERROR_INTERNAL,
1835 "unspecified error");
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001836 return FALSE;
Richard Hughes3c8ada32018-10-12 10:08:58 +01001837 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001838 fu_device_set_update_error (device, error_local->message);
1839 g_propagate_error (error, g_steal_pointer (&error_local));
Richard Hughescff38bc2016-12-12 12:03:37 +00001840 return FALSE;
1841 }
1842
Richard Hughesf556d372017-06-15 19:49:18 +01001843 /* no longer valid */
Richard Hughesf8039642019-01-16 12:22:22 +00001844 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT) &&
1845 !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) {
Richard Hughes08435162018-12-12 10:34:16 +00001846 GPtrArray *checksums = fu_device_get_checksums (device);
1847 g_ptr_array_set_size (checksums, 0);
1848 }
Richard Hughesf556d372017-06-15 19:49:18 +01001849
Richard Hughes019a1bc2019-11-26 10:19:33 +00001850 /* success */
Richard Hughesd0905142016-03-13 09:46:49 +00001851 return TRUE;
1852}
Richard Hughescff38bc2016-12-12 12:03:37 +00001853
Mario Limonciello1a680f32019-11-25 19:44:53 -06001854/**
1855 * fu_plugin_runner_clear_results:
1856 * @self: a #FuPlugin
1857 * @device: a #FuDevice
1858 * @error: A #GError or NULL
1859 *
1860 * Call into the plugin's clear results routine
1861 *
1862 * Returns: #TRUE for success, #FALSE for failure
1863 *
1864 * Since: 0.8.0
1865 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00001866gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001867fu_plugin_runner_clear_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001868{
Richard Hughes12724852018-09-04 13:53:44 +01001869 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001870 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001871 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001872
Richard Hughes6a489a92020-12-22 10:32:06 +00001873 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
1874 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
1875 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1876
Richard Hughescff38bc2016-12-12 12:03:37 +00001877 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001878 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughescff38bc2016-12-12 12:03:37 +00001879 return TRUE;
1880
Richard Hughes639da472018-01-06 22:35:04 +00001881 /* no object loaded */
1882 if (priv->module == NULL)
1883 return TRUE;
1884
Richard Hughes65e44ca2018-01-30 17:26:30 +00001885 /* optional */
Richard Hughescd644902019-11-01 12:35:17 +00001886 g_module_symbol (priv->module, "fu_plugin_clear_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001887 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001888 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001889 g_debug ("clear_result(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001890 if (!func (self, device, &error_local)) {
1891 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001892 g_critical ("unset plugin error in clear_result(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001893 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001894 g_set_error_literal (&error_local,
1895 FWUPD_ERROR,
1896 FWUPD_ERROR_INTERNAL,
1897 "unspecified error");
1898 }
1899 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1900 "failed to clear_result using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001901 fu_plugin_get_name (self));
Richard Hughescff38bc2016-12-12 12:03:37 +00001902 return FALSE;
1903 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001904 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001905}
1906
Mario Limonciello1a680f32019-11-25 19:44:53 -06001907/**
1908 * fu_plugin_runner_get_results:
1909 * @self: a #FuPlugin
1910 * @device: a #FuDevice
1911 * @error: A #GError or NULL
1912 *
1913 * Call into the plugin's get results routine
1914 *
1915 * Returns: #TRUE for success, #FALSE for failure
1916 *
1917 * Since: 0.8.0
1918 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00001919gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001920fu_plugin_runner_get_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001921{
Richard Hughes12724852018-09-04 13:53:44 +01001922 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001923 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001924 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001925
Richard Hughes6a489a92020-12-22 10:32:06 +00001926 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
1927 g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
1928 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1929
Richard Hughescff38bc2016-12-12 12:03:37 +00001930 /* not enabled */
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001931 if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_DISABLED))
Richard Hughescff38bc2016-12-12 12:03:37 +00001932 return TRUE;
1933
Richard Hughes639da472018-01-06 22:35:04 +00001934 /* no object loaded */
1935 if (priv->module == NULL)
1936 return TRUE;
1937
Richard Hughes65e44ca2018-01-30 17:26:30 +00001938 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001939 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001940 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001941 return TRUE;
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001942 g_debug ("get_results(%s)", fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001943 if (!func (self, device, &error_local)) {
1944 if (error_local == NULL) {
Mario Limonciello67a8b892020-09-28 13:44:39 -05001945 g_critical ("unset plugin error in get_results(%s)",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001946 fu_plugin_get_name (self));
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001947 g_set_error_literal (&error_local,
1948 FWUPD_ERROR,
1949 FWUPD_ERROR_INTERNAL,
1950 "unspecified error");
1951 }
1952 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1953 "failed to get_results using %s: ",
Richard Hughes7bcb8d42020-10-08 15:47:47 +01001954 fu_plugin_get_name (self));
Richard Hughescff38bc2016-12-12 12:03:37 +00001955 return FALSE;
1956 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001957 return TRUE;
1958}
1959
Richard Hughes08a37992017-09-12 12:57:43 +01001960/**
1961 * fu_plugin_get_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001962 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001963 *
1964 * Gets the plugin order, where higher numbers are run after lower
1965 * numbers.
1966 *
1967 * Returns: the integer value
Mario Limonciello1a680f32019-11-25 19:44:53 -06001968 *
1969 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01001970 **/
1971guint
Richard Hughes12724852018-09-04 13:53:44 +01001972fu_plugin_get_order (FuPlugin *self)
Richard Hughes08a37992017-09-12 12:57:43 +01001973{
Richard Hughes12724852018-09-04 13:53:44 +01001974 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001975 return priv->order;
1976}
1977
1978/**
1979 * fu_plugin_set_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001980 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001981 * @order: a integer value
1982 *
1983 * Sets the plugin order, where higher numbers are run after lower
1984 * numbers.
Mario Limonciello1a680f32019-11-25 19:44:53 -06001985 *
1986 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01001987 **/
1988void
Richard Hughes12724852018-09-04 13:53:44 +01001989fu_plugin_set_order (FuPlugin *self, guint order)
Richard Hughes08a37992017-09-12 12:57:43 +01001990{
Richard Hughes12724852018-09-04 13:53:44 +01001991 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001992 priv->order = order;
1993}
1994
1995/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001996 * fu_plugin_get_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001997 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001998 *
1999 * Gets the plugin priority, where higher numbers are better.
2000 *
2001 * Returns: the integer value
Mario Limonciello1a680f32019-11-25 19:44:53 -06002002 *
2003 * Since: 1.1.1
Richard Hughes81c427c2018-08-06 15:20:17 +01002004 **/
2005guint
Richard Hughes12724852018-09-04 13:53:44 +01002006fu_plugin_get_priority (FuPlugin *self)
Richard Hughes81c427c2018-08-06 15:20:17 +01002007{
Richard Hughes12724852018-09-04 13:53:44 +01002008 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01002009 return priv->priority;
2010}
2011
2012/**
2013 * fu_plugin_set_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002014 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01002015 * @priority: a integer value
2016 *
2017 * Sets the plugin priority, where higher numbers are better.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002018 *
2019 * Since: 1.0.0
Richard Hughes81c427c2018-08-06 15:20:17 +01002020 **/
2021void
Richard Hughes12724852018-09-04 13:53:44 +01002022fu_plugin_set_priority (FuPlugin *self, guint priority)
Richard Hughes81c427c2018-08-06 15:20:17 +01002023{
Richard Hughes12724852018-09-04 13:53:44 +01002024 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01002025 priv->priority = priority;
2026}
2027
2028/**
Richard Hughes08a37992017-09-12 12:57:43 +01002029 * fu_plugin_add_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002030 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002031 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01002032 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01002033 *
2034 * If the plugin name is found, the rule will be used to sort the plugin list,
2035 * for example the plugin specified by @name will be ordered after this plugin
2036 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
2037 *
2038 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
2039 * If depsolving fails then fwupd will not start.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002040 *
2041 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002042 **/
2043void
Richard Hughes12724852018-09-04 13:53:44 +01002044fu_plugin_add_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes08a37992017-09-12 12:57:43 +01002045{
Richard Hughes12724852018-09-04 13:53:44 +01002046 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes11c59412020-06-22 15:29:48 +01002047 if (priv->rules[rule] == NULL)
2048 priv->rules[rule] = g_ptr_array_new_with_free_func (g_free);
Richard Hughes08a37992017-09-12 12:57:43 +01002049 g_ptr_array_add (priv->rules[rule], g_strdup (name));
Richard Hughes75b965d2018-11-15 13:51:21 +00002050 g_signal_emit (self, signals[SIGNAL_RULES_CHANGED], 0);
Richard Hughes08a37992017-09-12 12:57:43 +01002051}
2052
2053/**
2054 * fu_plugin_get_rules:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002055 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002056 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
2057 *
2058 * Gets the plugin IDs that should be run after this plugin.
2059 *
Richard Hughes11c59412020-06-22 15:29:48 +01002060 * Returns: (element-type utf8) (transfer none) (nullable): the list of plugin names, e.g. ['appstream']
Mario Limonciello1a680f32019-11-25 19:44:53 -06002061 *
2062 * Since: 1.0.0
Richard Hughes08a37992017-09-12 12:57:43 +01002063 **/
2064GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +01002065fu_plugin_get_rules (FuPlugin *self, FuPluginRule rule)
Richard Hughes08a37992017-09-12 12:57:43 +01002066{
Richard Hughes12724852018-09-04 13:53:44 +01002067 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughesdad35972019-12-06 11:00:25 +00002068 g_return_val_if_fail (rule < FU_PLUGIN_RULE_LAST, NULL);
Richard Hughes08a37992017-09-12 12:57:43 +01002069 return priv->rules[rule];
2070}
2071
Richard Hughes80b79bb2018-01-11 21:11:06 +00002072/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002073 * fu_plugin_has_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002074 * @self: a #FuPlugin
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002075 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
2076 * @name: a plugin name, e.g. `upower`
2077 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01002078 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002079 *
2080 * Returns: %TRUE if the name exists for the specific rule
Mario Limonciello1a680f32019-11-25 19:44:53 -06002081 *
2082 * Since: 1.0.0
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002083 **/
2084gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002085fu_plugin_has_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002086{
Richard Hughes12724852018-09-04 13:53:44 +01002087 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes11c59412020-06-22 15:29:48 +01002088 if (priv->rules[rule] == NULL)
2089 return FALSE;
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002090 for (guint i = 0; i < priv->rules[rule]->len; i++) {
2091 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
2092 if (g_strcmp0 (tmp, name) == 0)
2093 return TRUE;
2094 }
2095 return FALSE;
2096}
2097
2098/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00002099 * fu_plugin_add_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002100 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00002101 * @key: a string, e.g. `FwupdateVersion`
2102 * @value: a string, e.g. `10`
2103 *
2104 * Sets any additional metadata to be included in the firmware report to aid
2105 * debugging problems.
2106 *
2107 * Any data included here will be sent to the metadata server after user
2108 * confirmation.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002109 *
2110 * Since: 1.0.4
Richard Hughes80b79bb2018-01-11 21:11:06 +00002111 **/
2112void
Richard Hughes12724852018-09-04 13:53:44 +01002113fu_plugin_add_report_metadata (FuPlugin *self, const gchar *key, const gchar *value)
Richard Hughes80b79bb2018-01-11 21:11:06 +00002114{
Richard Hughes12724852018-09-04 13:53:44 +01002115 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes1d900f72020-06-22 15:17:39 +01002116 if (priv->report_metadata == NULL) {
2117 priv->report_metadata = g_hash_table_new_full (g_str_hash,
2118 g_str_equal,
2119 g_free,
2120 g_free);
2121 }
Richard Hughes80b79bb2018-01-11 21:11:06 +00002122 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
2123}
2124
2125/**
2126 * fu_plugin_get_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002127 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00002128 *
2129 * Returns the list of additional metadata to be added when filing a report.
2130 *
Richard Hughes1d900f72020-06-22 15:17:39 +01002131 * Returns: (transfer none) (nullable): the map of report metadata
Mario Limonciello1a680f32019-11-25 19:44:53 -06002132 *
2133 * Since: 1.0.4
Richard Hughes80b79bb2018-01-11 21:11:06 +00002134 **/
2135GHashTable *
Richard Hughes12724852018-09-04 13:53:44 +01002136fu_plugin_get_report_metadata (FuPlugin *self)
Richard Hughes80b79bb2018-01-11 21:11:06 +00002137{
Richard Hughes12724852018-09-04 13:53:44 +01002138 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002139 return priv->report_metadata;
2140}
2141
Mario Limonciello963dc422018-02-27 14:26:58 -06002142/**
2143 * fu_plugin_get_config_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002144 * @self: a #FuPlugin
Mario Limonciello963dc422018-02-27 14:26:58 -06002145 * @key: A settings key
2146 *
2147 * Return the value of a key if it's been configured
2148 *
2149 * Since: 1.0.6
2150 **/
2151gchar *
Richard Hughes12724852018-09-04 13:53:44 +01002152fu_plugin_get_config_value (FuPlugin *self, const gchar *key)
Mario Limonciello963dc422018-02-27 14:26:58 -06002153{
Richard Hughes4be17d12018-05-30 20:36:29 +01002154 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06002155 g_autofree gchar *conf_file = NULL;
2156 g_autofree gchar *conf_path = NULL;
2157 g_autoptr(GKeyFile) keyfile = NULL;
2158 const gchar *plugin_name;
2159
Richard Hughes4be17d12018-05-30 20:36:29 +01002160 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Richard Hughes12724852018-09-04 13:53:44 +01002161 plugin_name = fu_plugin_get_name (self);
Mario Limonciello963dc422018-02-27 14:26:58 -06002162 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01002163 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06002164 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
2165 return NULL;
2166 keyfile = g_key_file_new ();
2167 if (!g_key_file_load_from_file (keyfile, conf_path,
2168 G_KEY_FILE_NONE, NULL))
2169 return NULL;
2170 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
2171}
2172
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002173/**
Richard Hughes334ba792020-02-19 20:44:56 +00002174 * fu_plugin_get_config_value_boolean:
2175 * @self: a #FuPlugin
2176 * @key: A settings key
2177 *
2178 * Return the boolean value of a key if it's been configured
2179 *
2180 * Returns: %TRUE if the value is `true` (case insensitive), %FALSE otherwise
2181 *
Mario Limonciello96117d12020-02-28 10:17:56 -06002182 * Since: 1.4.0
Richard Hughes334ba792020-02-19 20:44:56 +00002183 **/
2184gboolean
2185fu_plugin_get_config_value_boolean (FuPlugin *self, const gchar *key)
2186{
2187 g_autofree gchar *tmp = fu_plugin_get_config_value (self, key);
2188 if (tmp == NULL)
2189 return FALSE;
Richard Hughes5337a432020-02-21 12:04:32 +00002190 return g_ascii_strcasecmp (tmp, "true") == 0;
Richard Hughes334ba792020-02-19 20:44:56 +00002191}
2192
2193/**
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002194 * fu_plugin_name_compare:
2195 * @plugin1: first #FuPlugin to compare.
2196 * @plugin2: second #FuPlugin to compare.
2197 *
2198 * Compares two plugins by their names.
2199 *
2200 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002201 *
2202 * Since: 1.0.8
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002203 **/
2204gint
2205fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2206{
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002207 return g_strcmp0 (fu_plugin_get_name (plugin1), fu_plugin_get_name (plugin2));
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002208}
2209
2210/**
2211 * fu_plugin_order_compare:
2212 * @plugin1: first #FuPlugin to compare.
2213 * @plugin2: second #FuPlugin to compare.
2214 *
2215 * Compares two plugins by their depsolved order.
2216 *
2217 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
Mario Limonciello1a680f32019-11-25 19:44:53 -06002218 *
2219 * Since: 1.0.8
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002220 **/
2221gint
2222fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2223{
2224 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
2225 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
2226 if (priv1->order < priv2->order)
2227 return -1;
2228 if (priv1->order > priv2->order)
2229 return 1;
2230 return 0;
2231}
2232
Richard Hughescff38bc2016-12-12 12:03:37 +00002233static void
2234fu_plugin_class_init (FuPluginClass *klass)
2235{
2236 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2237 object_class->finalize = fu_plugin_finalize;
2238 signals[SIGNAL_DEVICE_ADDED] =
2239 g_signal_new ("device-added",
2240 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2241 G_STRUCT_OFFSET (FuPluginClass, device_added),
2242 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2243 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
2244 signals[SIGNAL_DEVICE_REMOVED] =
2245 g_signal_new ("device-removed",
2246 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2247 G_STRUCT_OFFSET (FuPluginClass, device_removed),
2248 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2249 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01002250 signals[SIGNAL_DEVICE_REGISTER] =
2251 g_signal_new ("device-register",
2252 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2253 G_STRUCT_OFFSET (FuPluginClass, device_register),
2254 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2255 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughesaabdc372018-11-14 10:11:08 +00002256 signals[SIGNAL_CHECK_SUPPORTED] =
2257 g_signal_new ("check-supported",
2258 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2259 G_STRUCT_OFFSET (FuPluginClass, check_supported),
2260 NULL, NULL, g_cclosure_marshal_generic,
2261 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
Richard Hughes75b965d2018-11-15 13:51:21 +00002262 signals[SIGNAL_RULES_CHANGED] =
2263 g_signal_new ("rules-changed",
2264 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2265 G_STRUCT_OFFSET (FuPluginClass, rules_changed),
2266 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2267 G_TYPE_NONE, 0);
Richard Hughescff38bc2016-12-12 12:03:37 +00002268}
2269
2270static void
Richard Hughes12724852018-09-04 13:53:44 +01002271fu_plugin_init (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +00002272{
Richard Hughes12724852018-09-04 13:53:44 +01002273 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesfaf2afe2021-01-13 14:00:20 +00002274 g_rw_lock_init (&priv->cache_mutex);
Richard Hughescff38bc2016-12-12 12:03:37 +00002275}
2276
2277static void
2278fu_plugin_finalize (GObject *object)
2279{
Richard Hughes12724852018-09-04 13:53:44 +01002280 FuPlugin *self = FU_PLUGIN (object);
2281 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002282 FuPluginInitFunc func = NULL;
2283
Richard Hughesfaf2afe2021-01-13 14:00:20 +00002284 g_rw_lock_clear (&priv->cache_mutex);
Richard Hughesaae22e42020-06-22 21:32:59 +01002285
Richard Hughescff38bc2016-12-12 12:03:37 +00002286 /* optional */
2287 if (priv->module != NULL) {
2288 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
2289 if (func != NULL) {
Richard Hughes7bcb8d42020-10-08 15:47:47 +01002290 g_debug ("destroy(%s)", fu_plugin_get_name (self));
Richard Hughes12724852018-09-04 13:53:44 +01002291 func (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002292 }
2293 }
2294
Richard Hughes11c59412020-06-22 15:29:48 +01002295 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++) {
2296 if (priv->rules[i] != NULL)
2297 g_ptr_array_unref (priv->rules[i]);
2298 }
Richard Hughes68ab1e42021-01-13 14:01:17 +00002299 if (priv->devices != NULL)
2300 g_ptr_array_unref (priv->devices);
Richard Hughesb333e002021-04-01 10:40:02 +01002301 if (priv->ctx != NULL)
2302 g_object_unref (priv->ctx);
Richard Hughes275d3b42018-04-20 16:40:37 +01002303 if (priv->runtime_versions != NULL)
2304 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01002305 if (priv->compile_versions != NULL)
2306 g_hash_table_unref (priv->compile_versions);
Richard Hughes1d900f72020-06-22 15:17:39 +01002307 if (priv->report_metadata != NULL)
2308 g_hash_table_unref (priv->report_metadata);
Richard Hughesfaf2afe2021-01-13 14:00:20 +00002309 if (priv->cache != NULL)
2310 g_hash_table_unref (priv->cache);
Richard Hughes84999302019-05-02 10:18:32 +01002311 g_free (priv->build_hash);
Richard Hughescff38bc2016-12-12 12:03:37 +00002312 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05002313 /* Must happen as the last step to avoid prematurely
2314 * freeing memory held by the plugin */
Richard Hughes862ec5c2020-05-22 14:38:02 +01002315#ifdef RUNNING_ON_VALGRIND
2316 if (priv->module != NULL && RUNNING_ON_VALGRIND == 0)
2317#else
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05002318 if (priv->module != NULL)
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05002319#endif
Richard Hughes862ec5c2020-05-22 14:38:02 +01002320 g_module_close (priv->module);
Richard Hughescff38bc2016-12-12 12:03:37 +00002321
2322 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
2323}
2324
Mario Limonciello1a680f32019-11-25 19:44:53 -06002325/**
2326 * fu_plugin_new:
2327 *
2328 * Creates a new #FuPlugin
2329 *
2330 * Since: 0.8.0
2331 **/
Richard Hughescff38bc2016-12-12 12:03:37 +00002332FuPlugin *
Richard Hughesb333e002021-04-01 10:40:02 +01002333fu_plugin_new (FuContext *ctx)
Richard Hughescff38bc2016-12-12 12:03:37 +00002334{
Richard Hughesb333e002021-04-01 10:40:02 +01002335 FuPlugin *self = FU_PLUGIN (g_object_new (FU_TYPE_PLUGIN, NULL));
2336 FuPluginPrivate *priv = GET_PRIVATE (self);
2337 if (ctx != NULL)
2338 priv->ctx = g_object_ref (ctx);
2339 return self;
Richard Hughescff38bc2016-12-12 12:03:37 +00002340}