blob: 810061ec868d5e452566ea80181a77efae43af3c [file] [log] [blame]
Richard Hughes02c90d82018-08-09 12:13:03 +01001/*
Richard Hughes2de8f132018-01-17 09:12:02 +00002 * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com>
Richard Hughesd0905142016-03-13 09:46:49 +00003 *
Mario Limonciello51308e62018-05-28 20:05:46 -05004 * SPDX-License-Identifier: LGPL-2.1+
Richard Hughesd0905142016-03-13 09:46:49 +00005 */
6
Richard Hughesb08e7bc2018-09-11 10:51:13 +01007#define G_LOG_DOMAIN "FuPlugin"
8
Richard Hughesd0905142016-03-13 09:46:49 +00009#include "config.h"
10
Richard Hughescff38bc2016-12-12 12:03:37 +000011#include <fwupd.h>
12#include <gmodule.h>
Richard Hughescff38bc2016-12-12 12:03:37 +000013#include <errno.h>
14#include <string.h>
Richard Hughes68f12dd2018-08-09 14:43:31 +010015#include <unistd.h>
Richard Hughescff38bc2016-12-12 12:03:37 +000016#include <gio/gunixinputstream.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060017#ifdef HAVE_VALGRIND
Richard Hughes576c0122017-02-24 09:47:00 +000018#include <valgrind.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060019#endif /* HAVE_VALGRIND */
Richard Hughesd0905142016-03-13 09:46:49 +000020
Richard Hughes9dde04f2017-09-13 12:07:15 +010021#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000022#include "fu-plugin-private.h"
Richard Hughesbc3a4e12018-01-06 22:41:47 +000023#include "fu-history.h"
Richard Hughes37d09432018-09-09 10:39:45 +010024#include "fu-mutex.h"
Richard Hughesd0905142016-03-13 09:46:49 +000025
Richard Hughes4eada342017-10-03 21:20:32 +010026/**
27 * SECTION:fu-plugin
28 * @short_description: a daemon plugin
29 *
30 * An object that represents a plugin run by the daemon.
31 *
32 * See also: #FuDevice
33 */
34
Richard Hughesb0829032017-01-10 09:27:08 +000035#define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */
36
Richard Hughescff38bc2016-12-12 12:03:37 +000037static void fu_plugin_finalize (GObject *object);
38
39typedef struct {
40 GModule *module;
41 GUsbContext *usb_ctx;
42 gboolean enabled;
Richard Hughes08a37992017-09-12 12:57:43 +010043 guint order;
Richard Hughes81c427c2018-08-06 15:20:17 +010044 guint priority;
Richard Hughes08a37992017-09-12 12:57:43 +010045 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughescff38bc2016-12-12 12:03:37 +000046 gchar *name;
Richard Hughesf425d292019-01-18 17:57:39 +000047 gchar *build_hash;
Richard Hughesd7704d42017-08-08 20:29:09 +010048 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010049 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010050 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010051 GHashTable *compile_versions;
Richard Hughes9d6e0e72018-08-24 20:20:17 +010052 GPtrArray *udev_subsystems;
Richard Hughes1354ea92017-09-19 15:58:31 +010053 FuSmbios *smbios;
Richard Hughes989acf12019-10-05 20:16:47 +010054 GType device_gtype;
Richard Hughescff38bc2016-12-12 12:03:37 +000055 GHashTable *devices; /* platform_id:GObject */
Richard Hughes161e9b52019-06-12 14:22:45 +010056 GRWLock devices_mutex;
Richard Hughes80b79bb2018-01-11 21:11:06 +000057 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000058 FuPluginData *data;
59} FuPluginPrivate;
60
61enum {
62 SIGNAL_DEVICE_ADDED,
63 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010064 SIGNAL_DEVICE_REGISTER,
Richard Hughes75b965d2018-11-15 13:51:21 +000065 SIGNAL_RULES_CHANGED,
Richard Hughes362d6d72017-01-07 21:42:14 +000066 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000067 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughesaabdc372018-11-14 10:11:08 +000068 SIGNAL_CHECK_SUPPORTED,
Richard Hughescff38bc2016-12-12 12:03:37 +000069 SIGNAL_LAST
70};
71
72static guint signals[SIGNAL_LAST] = { 0 };
73
74G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
75#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
76
77typedef const gchar *(*FuPluginGetNameFunc) (void);
Richard Hughes12724852018-09-04 13:53:44 +010078typedef void (*FuPluginInitFunc) (FuPlugin *self);
79typedef gboolean (*FuPluginStartupFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000080 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010081typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *self,
Richard Hughese1fd34d2017-08-24 14:19:51 +010082 FuDevice *device);
Richard Hughes12724852018-09-04 13:53:44 +010083typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000084 FuDevice *device,
85 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010086typedef gboolean (*FuPluginFlaggedDeviceFunc) (FuPlugin *self,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -050087 FwupdInstallFlags flags,
88 FuDevice *device,
89 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010090typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *self,
Richard Hughesdbd8c762018-06-15 20:31:40 +010091 GPtrArray *devices,
92 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010093typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000094 FuDevice *device,
95 FuPluginVerifyFlags flags,
96 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010097typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000098 FuDevice *device,
99 GBytes *blob_fw,
100 FwupdInstallFlags flags,
101 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100102typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100103 FuUsbDevice *device,
Richard Hughes104f6512017-11-24 11:44:57 +0000104 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100105typedef gboolean (*FuPluginUdevDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100106 FuUdevDevice *device,
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100107 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +0000108
Richard Hughes57d18222017-01-10 16:02:59 +0000109/**
110 * fu_plugin_get_name:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100111 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000112 *
113 * Gets the plugin name.
114 *
115 * Returns: a plugin name, or %NULL for unknown.
116 *
117 * Since: 0.8.0
118 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000119const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100120fu_plugin_get_name (FuPlugin *self)
Richard Hughesd0905142016-03-13 09:46:49 +0000121{
Richard Hughes12724852018-09-04 13:53:44 +0100122 FuPluginPrivate *priv = GET_PRIVATE (self);
123 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000124 return priv->name;
125}
Richard Hughesd0905142016-03-13 09:46:49 +0000126
Richard Hughes34834102017-11-21 21:55:00 +0000127void
Richard Hughes12724852018-09-04 13:53:44 +0100128fu_plugin_set_name (FuPlugin *self, const gchar *name)
Richard Hughes34834102017-11-21 21:55:00 +0000129{
Richard Hughes12724852018-09-04 13:53:44 +0100130 FuPluginPrivate *priv = GET_PRIVATE (self);
131 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughes34834102017-11-21 21:55:00 +0000132 g_return_if_fail (name != NULL);
133 g_free (priv->name);
134 priv->name = g_strdup (name);
135}
136
Richard Hughes57d18222017-01-10 16:02:59 +0000137/**
Richard Hughesf425d292019-01-18 17:57:39 +0000138 * fu_plugin_set_build_hash:
139 * @self: A #FuPlugin
140 * @build_hash: A checksum
141 *
142 * Sets the plugin build hash, typically a SHA256 checksum. All plugins must
143 * set the correct checksum to avoid the daemon being marked as tainted.
144 *
145 * Since: 1.2.4
146 **/
147void
148fu_plugin_set_build_hash (FuPlugin *self, const gchar *build_hash)
149{
150 FuPluginPrivate *priv = GET_PRIVATE (self);
151 g_return_if_fail (FU_IS_PLUGIN (self));
152 g_return_if_fail (build_hash != NULL);
153 g_free (priv->build_hash);
154 priv->build_hash = g_strdup (build_hash);
155}
156
157const gchar *
158fu_plugin_get_build_hash (FuPlugin *self)
159{
160 FuPluginPrivate *priv = GET_PRIVATE (self);
161 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
162 return priv->build_hash;
163}
164
165/**
Richard Hughes57d18222017-01-10 16:02:59 +0000166 * fu_plugin_cache_lookup:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100167 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000168 * @id: the key
169 *
170 * Finds an object in the per-plugin cache.
171 *
172 * Returns: (transfer none): a #GObject, or %NULL for unfound.
173 *
174 * Since: 0.8.0
175 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000176gpointer
Richard Hughes12724852018-09-04 13:53:44 +0100177fu_plugin_cache_lookup (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000178{
Richard Hughes12724852018-09-04 13:53:44 +0100179 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes161e9b52019-06-12 14:22:45 +0100180 g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_reader_locker_new (&priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100181 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughesccd78a92017-01-11 16:57:41 +0000182 g_return_val_if_fail (id != NULL, NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100183 g_return_val_if_fail (locker != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000184 return g_hash_table_lookup (priv->devices, id);
185}
Richard Hughesd0905142016-03-13 09:46:49 +0000186
Richard Hughes57d18222017-01-10 16:02:59 +0000187/**
188 * fu_plugin_cache_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100189 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000190 * @id: the key
191 * @dev: a #GObject, typically a #FuDevice
192 *
193 * Adds an object to the per-plugin cache.
194 *
195 * Since: 0.8.0
196 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000197void
Richard Hughes12724852018-09-04 13:53:44 +0100198fu_plugin_cache_add (FuPlugin *self, const gchar *id, gpointer dev)
Richard Hughescff38bc2016-12-12 12:03:37 +0000199{
Richard Hughes12724852018-09-04 13:53:44 +0100200 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes161e9b52019-06-12 14:22:45 +0100201 g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_writer_locker_new (&priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100202 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000203 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100204 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000205 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
206}
207
Richard Hughes57d18222017-01-10 16:02:59 +0000208/**
209 * fu_plugin_cache_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100210 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000211 * @id: the key
212 *
213 * Removes an object from the per-plugin cache.
214 *
215 * Since: 0.8.0
216 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000217void
Richard Hughes12724852018-09-04 13:53:44 +0100218fu_plugin_cache_remove (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000219{
Richard Hughes12724852018-09-04 13:53:44 +0100220 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes161e9b52019-06-12 14:22:45 +0100221 g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_writer_locker_new (&priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100222 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000223 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100224 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000225 g_hash_table_remove (priv->devices, id);
226}
227
Richard Hughes57d18222017-01-10 16:02:59 +0000228/**
229 * fu_plugin_get_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100230 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000231 *
Richard Hughes4eada342017-10-03 21:20:32 +0100232 * Gets the per-plugin allocated private data. This will return %NULL unless
233 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000234 *
Richard Hughes4eada342017-10-03 21:20:32 +0100235 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000236 *
237 * Since: 0.8.0
238 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000239FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100240fu_plugin_get_data (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000241{
Richard Hughes12724852018-09-04 13:53:44 +0100242 FuPluginPrivate *priv = GET_PRIVATE (self);
243 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000244 return priv->data;
245}
246
Richard Hughes57d18222017-01-10 16:02:59 +0000247/**
248 * fu_plugin_alloc_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100249 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000250 * @data_sz: the size to allocate
251 *
252 * Allocates the per-plugin allocated private data.
253 *
254 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
255 *
256 * Since: 0.8.0
257 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000258FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100259fu_plugin_alloc_data (FuPlugin *self, gsize data_sz)
Richard Hughescff38bc2016-12-12 12:03:37 +0000260{
Richard Hughes12724852018-09-04 13:53:44 +0100261 FuPluginPrivate *priv = GET_PRIVATE (self);
262 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000263 if (priv->data != NULL) {
264 g_critical ("fu_plugin_alloc_data() already used by plugin");
265 return priv->data;
266 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000267 priv->data = g_malloc0 (data_sz);
268 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000269}
270
Richard Hughes57d18222017-01-10 16:02:59 +0000271/**
272 * fu_plugin_get_usb_context:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100273 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000274 *
275 * Gets the shared USB context that all plugins can use.
276 *
277 * Returns: (transfer none): a #GUsbContext.
278 *
279 * Since: 0.8.0
280 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000281GUsbContext *
Richard Hughes12724852018-09-04 13:53:44 +0100282fu_plugin_get_usb_context (FuPlugin *self)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000283{
Richard Hughes12724852018-09-04 13:53:44 +0100284 FuPluginPrivate *priv = GET_PRIVATE (self);
285 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000286 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000287}
288
289void
Richard Hughes12724852018-09-04 13:53:44 +0100290fu_plugin_set_usb_context (FuPlugin *self, GUsbContext *usb_ctx)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000291{
Richard Hughes12724852018-09-04 13:53:44 +0100292 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000293 g_set_object (&priv->usb_ctx, usb_ctx);
294}
295
Richard Hughes57d18222017-01-10 16:02:59 +0000296/**
297 * fu_plugin_get_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100298 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000299 *
Richard Hughes4eada342017-10-03 21:20:32 +0100300 * Returns if the plugin is enabled. Plugins may self-disable using
301 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000302 *
303 * Returns: %TRUE if the plugin is currently enabled.
304 *
305 * Since: 0.8.0
306 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000307gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100308fu_plugin_get_enabled (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000309{
Richard Hughes12724852018-09-04 13:53:44 +0100310 FuPluginPrivate *priv = GET_PRIVATE (self);
311 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000312 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000313}
314
Richard Hughes57d18222017-01-10 16:02:59 +0000315/**
316 * fu_plugin_set_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100317 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000318 * @enabled: the enabled value
319 *
320 * Enables or disables a plugin. Plugins can self-disable at any point.
321 *
322 * Since: 0.8.0
323 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000324void
Richard Hughes12724852018-09-04 13:53:44 +0100325fu_plugin_set_enabled (FuPlugin *self, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000326{
Richard Hughes12724852018-09-04 13:53:44 +0100327 FuPluginPrivate *priv = GET_PRIVATE (self);
328 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000329 priv->enabled = enabled;
330}
331
Richard Hughes1e456bc2018-05-10 20:16:16 +0100332gchar *
333fu_plugin_guess_name_from_fn (const gchar *filename)
334{
335 const gchar *prefix = "libfu_plugin_";
336 gchar *name;
337 gchar *str = g_strstr_len (filename, -1, prefix);
338 if (str == NULL)
339 return NULL;
340 name = g_strdup (str + strlen (prefix));
341 g_strdelimit (name, ".", '\0');
342 return name;
343}
344
Richard Hughescff38bc2016-12-12 12:03:37 +0000345gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100346fu_plugin_open (FuPlugin *self, const gchar *filename, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +0000347{
Richard Hughes12724852018-09-04 13:53:44 +0100348 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000349 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000350
351 priv->module = g_module_open (filename, 0);
352 if (priv->module == NULL) {
353 g_set_error (error,
354 G_IO_ERROR,
355 G_IO_ERROR_FAILED,
356 "failed to open plugin: %s",
357 g_module_error ());
358 return FALSE;
359 }
360
361 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100362 if (priv->name == NULL)
363 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000364
365 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000366 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
367 if (func != NULL) {
368 g_debug ("performing init() on %s", filename);
Richard Hughes12724852018-09-04 13:53:44 +0100369 func (self);
Richard Hughesd0905142016-03-13 09:46:49 +0000370 }
371
Richard Hughescff38bc2016-12-12 12:03:37 +0000372 return TRUE;
373}
374
Richard Hughes57d18222017-01-10 16:02:59 +0000375/**
376 * fu_plugin_device_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100377 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000378 * @device: A #FuDevice
379 *
380 * Asks the daemon to add a device to the exported list. If this device ID
381 * has already been added by a different plugin then this request will be
382 * ignored.
383 *
384 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
385 * actually flashing an image to the hardware so that higher-priority plugins
386 * can add the device themselves.
387 *
388 * Since: 0.8.0
389 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000390void
Richard Hughes12724852018-09-04 13:53:44 +0100391fu_plugin_device_add (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000392{
Richard Hughes5e447292018-04-27 14:25:54 +0100393 GPtrArray *children;
Richard Hughesc125ec02018-09-05 19:35:17 +0100394 g_autoptr(GError) error = NULL;
Richard Hughes5e447292018-04-27 14:25:54 +0100395
Richard Hughes12724852018-09-04 13:53:44 +0100396 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000397 g_return_if_fail (FU_IS_DEVICE (device));
398
Richard Hughesc125ec02018-09-05 19:35:17 +0100399 /* ensure the device ID is set from the physical and logical IDs */
400 if (!fu_device_ensure_id (device, &error)) {
401 g_warning ("ignoring add: %s", error->message);
402 return;
403 }
404
Richard Hughescff38bc2016-12-12 12:03:37 +0000405 g_debug ("emit added from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100406 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000407 fu_device_get_id (device));
408 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
Richard Hughes12724852018-09-04 13:53:44 +0100409 fu_device_set_plugin (device, fu_plugin_get_name (self));
410 g_signal_emit (self, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100411
Richard Hughes128c0162018-08-10 11:00:29 +0100412 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100413 children = fu_device_get_children (device);
414 for (guint i = 0; i < children->len; i++) {
415 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100416 if (fu_device_get_created (child) == 0)
Richard Hughes12724852018-09-04 13:53:44 +0100417 fu_plugin_device_add (self, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100418 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000419}
420
Richard Hughese1fd34d2017-08-24 14:19:51 +0100421/**
422 * fu_plugin_device_register:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100423 * @self: A #FuPlugin
Richard Hughese1fd34d2017-08-24 14:19:51 +0100424 * @device: A #FuDevice
425 *
426 * Registers the device with other plugins so they can set metadata.
427 *
428 * Plugins do not have to call this manually as this is done automatically
429 * when using fu_plugin_device_add(). They may wish to use this manually
430 * if for intance the coldplug should be ignored based on the metadata
431 * set from other plugins.
432 *
433 * Since: 0.9.7
434 **/
435void
Richard Hughes12724852018-09-04 13:53:44 +0100436fu_plugin_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +0100437{
Richard Hughesc125ec02018-09-05 19:35:17 +0100438 g_autoptr(GError) error = NULL;
439
Richard Hughes12724852018-09-04 13:53:44 +0100440 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughese1fd34d2017-08-24 14:19:51 +0100441 g_return_if_fail (FU_IS_DEVICE (device));
442
Richard Hughesc125ec02018-09-05 19:35:17 +0100443 /* ensure the device ID is set from the physical and logical IDs */
444 if (!fu_device_ensure_id (device, &error)) {
445 g_warning ("ignoring registration: %s", error->message);
446 return;
447 }
448
Richard Hughese1fd34d2017-08-24 14:19:51 +0100449 g_debug ("emit device-register from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100450 fu_plugin_get_name (self),
Richard Hughese1fd34d2017-08-24 14:19:51 +0100451 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100452 g_signal_emit (self, signals[SIGNAL_DEVICE_REGISTER], 0, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +0100453}
454
Richard Hughes57d18222017-01-10 16:02:59 +0000455/**
Richard Hughes4eada342017-10-03 21:20:32 +0100456 * fu_plugin_device_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100457 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000458 * @device: A #FuDevice
459 *
460 * Asks the daemon to remove a device from the exported list.
461 *
462 * Since: 0.8.0
463 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000464void
Richard Hughes12724852018-09-04 13:53:44 +0100465fu_plugin_device_remove (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000466{
Richard Hughes12724852018-09-04 13:53:44 +0100467 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000468 g_return_if_fail (FU_IS_DEVICE (device));
469
Richard Hughescff38bc2016-12-12 12:03:37 +0000470 g_debug ("emit removed from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100471 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000472 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100473 g_signal_emit (self, signals[SIGNAL_DEVICE_REMOVED], 0, device);
Richard Hughescff38bc2016-12-12 12:03:37 +0000474}
475
Richard Hughes57d18222017-01-10 16:02:59 +0000476/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000477 * fu_plugin_request_recoldplug:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100478 * @self: A #FuPlugin
Richard Hughes362d6d72017-01-07 21:42:14 +0000479 *
480 * Ask all the plugins to coldplug all devices, which will include the prepare()
481 * and cleanup() phases. Duplicate devices added will be ignored.
482 *
483 * Since: 0.8.0
484 **/
485void
Richard Hughes12724852018-09-04 13:53:44 +0100486fu_plugin_request_recoldplug (FuPlugin *self)
Richard Hughes362d6d72017-01-07 21:42:14 +0000487{
Richard Hughes12724852018-09-04 13:53:44 +0100488 g_return_if_fail (FU_IS_PLUGIN (self));
489 g_signal_emit (self, signals[SIGNAL_RECOLDPLUG], 0);
Richard Hughes362d6d72017-01-07 21:42:14 +0000490}
491
Richard Hughesb0829032017-01-10 09:27:08 +0000492/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100493 * fu_plugin_check_hwid:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100494 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100495 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100496 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100497 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100498 * specific system can be shown using the `fwupdmgr hwids` command.
499 *
Richard Hughes4eada342017-10-03 21:20:32 +0100500 * Returns: %TRUE if the HwId is found on the system.
501 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100502 * Since: 0.9.1
503 **/
504gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100505fu_plugin_check_hwid (FuPlugin *self, const gchar *hwid)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100506{
Richard Hughes12724852018-09-04 13:53:44 +0100507 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100508 if (priv->hwids == NULL)
509 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100510 return fu_hwids_has_guid (priv->hwids, hwid);
511}
512
513/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100514 * fu_plugin_get_hwids:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100515 * @self: A #FuPlugin
Richard Hughes69a5f352018-08-08 11:58:15 +0100516 *
517 * Returns all the HWIDs defined in the system. All hardware IDs on a
518 * specific system can be shown using the `fwupdmgr hwids` command.
519 *
520 * Returns: (transfer none) (element-type utf-8): An array of GUIDs
521 *
522 * Since: 1.1.1
523 **/
524GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +0100525fu_plugin_get_hwids (FuPlugin *self)
Richard Hughes69a5f352018-08-08 11:58:15 +0100526{
Richard Hughes12724852018-09-04 13:53:44 +0100527 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes69a5f352018-08-08 11:58:15 +0100528 if (priv->hwids == NULL)
529 return NULL;
530 return fu_hwids_get_guids (priv->hwids);
531}
532
533/**
Richard Hughes19841802019-09-10 16:48:00 +0100534 * fu_plugin_has_custom_flag:
535 * @self: A #FuPlugin
536 * @flag: A custom text flag, specific to the plugin, e.g. `uefi-force-enable`
537 *
538 * Returns if a per-plugin HwId custom flag exists, typically added from a DMI quirk.
539 *
540 * Returns: %TRUE if the quirk entry exists
541 *
542 * Since: 1.3.1
543 **/
544gboolean
545fu_plugin_has_custom_flag (FuPlugin *self, const gchar *flag)
546{
547 FuPluginPrivate *priv = GET_PRIVATE (self);
548 GPtrArray *hwids = fu_plugin_get_hwids (self);
549
550 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
551 g_return_val_if_fail (flag != NULL, FALSE);
552
553 /* never set up, e.g. in tests */
554 if (hwids == NULL)
555 return FALSE;
556
557 /* search each hwid */
558 for (guint i = 0; i < hwids->len; i++) {
559 const gchar *hwid = g_ptr_array_index (hwids, i);
560 const gchar *value;
561 g_autofree gchar *key = g_strdup_printf ("HwId=%s", hwid);
562
563 /* does prefixed quirk exist */
564 value = fu_quirks_lookup_by_id (priv->quirks, key, FU_QUIRKS_FLAGS);
565 if (value != NULL) {
566 g_auto(GStrv) quirks = g_strsplit (value, ",", -1);
567 if (g_strv_contains ((const gchar * const *) quirks, flag))
568 return TRUE;
569 }
570 }
571 return FALSE;
572}
573
574/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100575 * fu_plugin_check_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100576 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100577 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100578 *
579 * Checks to see if a specific device GUID is supported, i.e. available in the
580 * AppStream metadata.
581 *
Richard Hughes4eada342017-10-03 21:20:32 +0100582 * Returns: %TRUE if the device is supported.
583 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100584 * Since: 1.0.0
585 **/
586gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100587fu_plugin_check_supported (FuPlugin *self, const gchar *guid)
Richard Hughes1354ea92017-09-19 15:58:31 +0100588{
Richard Hughesaabdc372018-11-14 10:11:08 +0000589 gboolean retval = FALSE;
590 g_signal_emit (self, signals[SIGNAL_CHECK_SUPPORTED], 0, guid, &retval);
591 return retval;
Richard Hughes1354ea92017-09-19 15:58:31 +0100592}
593
594/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100595 * fu_plugin_get_dmi_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100596 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100597 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100598 *
599 * Gets a hardware DMI value.
600 *
Richard Hughes4eada342017-10-03 21:20:32 +0100601 * Returns: The string, or %NULL
602 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100603 * Since: 0.9.7
604 **/
605const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100606fu_plugin_get_dmi_value (FuPlugin *self, const gchar *dmi_id)
Richard Hughesd7704d42017-08-08 20:29:09 +0100607{
Richard Hughes12724852018-09-04 13:53:44 +0100608 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100609 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100610 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100611 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100612}
613
Richard Hughes49e5e052017-09-03 12:15:41 +0100614/**
615 * fu_plugin_get_smbios_string:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100616 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100617 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
618 * @offset: A SMBIOS offset
619 *
620 * Gets a hardware SMBIOS string.
621 *
622 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
623 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
624 *
Richard Hughes4eada342017-10-03 21:20:32 +0100625 * Returns: A string, or %NULL
626 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100627 * Since: 0.9.8
628 **/
629const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100630fu_plugin_get_smbios_string (FuPlugin *self, guint8 structure_type, guint8 offset)
Richard Hughes49e5e052017-09-03 12:15:41 +0100631{
Richard Hughes12724852018-09-04 13:53:44 +0100632 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100633 if (priv->smbios == NULL)
634 return NULL;
635 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
636}
637
638/**
639 * fu_plugin_get_smbios_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100640 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100641 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
642 *
643 * Gets a hardware SMBIOS data.
644 *
Richard Hughesdfaca2d2019-08-01 08:08:03 +0100645 * Returns: (transfer full): A #GBytes, or %NULL
Richard Hughes4eada342017-10-03 21:20:32 +0100646 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100647 * Since: 0.9.8
648 **/
649GBytes *
Richard Hughes12724852018-09-04 13:53:44 +0100650fu_plugin_get_smbios_data (FuPlugin *self, guint8 structure_type)
Richard Hughes49e5e052017-09-03 12:15:41 +0100651{
Richard Hughes12724852018-09-04 13:53:44 +0100652 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100653 if (priv->smbios == NULL)
654 return NULL;
655 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
656}
657
Richard Hughesb8f8db22017-04-25 15:56:00 +0100658void
Richard Hughes12724852018-09-04 13:53:44 +0100659fu_plugin_set_hwids (FuPlugin *self, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100660{
Richard Hughes12724852018-09-04 13:53:44 +0100661 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100662 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100663}
664
Richard Hughes49e5e052017-09-03 12:15:41 +0100665void
Richard Hughes12724852018-09-04 13:53:44 +0100666fu_plugin_set_udev_subsystems (FuPlugin *self, GPtrArray *udev_subsystems)
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100667{
Richard Hughes12724852018-09-04 13:53:44 +0100668 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100669 if (priv->udev_subsystems != NULL)
670 g_ptr_array_unref (priv->udev_subsystems);
671 priv->udev_subsystems = g_ptr_array_ref (udev_subsystems);
672}
673
674void
Richard Hughes12724852018-09-04 13:53:44 +0100675fu_plugin_set_quirks (FuPlugin *self, FuQuirks *quirks)
Richard Hughes9c028f02017-10-28 21:14:28 +0100676{
Richard Hughes12724852018-09-04 13:53:44 +0100677 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100678 g_set_object (&priv->quirks, quirks);
679}
680
681/**
682 * fu_plugin_get_quirks:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100683 * @self: A #FuPlugin
Richard Hughes9c028f02017-10-28 21:14:28 +0100684 *
685 * Returns the hardware database object. This can be used to discover device
686 * quirks or other device-specific settings.
687 *
688 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
689 *
690 * Since: 1.0.1
691 **/
692FuQuirks *
Richard Hughes12724852018-09-04 13:53:44 +0100693fu_plugin_get_quirks (FuPlugin *self)
Richard Hughes9c028f02017-10-28 21:14:28 +0100694{
Richard Hughes12724852018-09-04 13:53:44 +0100695 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100696 return priv->quirks;
697}
698
Richard Hughes0eb123b2018-04-19 12:00:04 +0100699void
Richard Hughes12724852018-09-04 13:53:44 +0100700fu_plugin_set_runtime_versions (FuPlugin *self, GHashTable *runtime_versions)
Richard Hughes0eb123b2018-04-19 12:00:04 +0100701{
Richard Hughes12724852018-09-04 13:53:44 +0100702 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0eb123b2018-04-19 12:00:04 +0100703 priv->runtime_versions = g_hash_table_ref (runtime_versions);
704}
705
706/**
707 * fu_plugin_add_runtime_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100708 * @self: A #FuPlugin
Richard Hughes0eb123b2018-04-19 12:00:04 +0100709 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
710 * @version: A version string, e.g. "1.2.3"
711 *
Richard Hughesdce91202019-04-08 12:47:45 +0100712 * Sets a runtime version of a specific dependency.
Richard Hughes0eb123b2018-04-19 12:00:04 +0100713 *
714 * Since: 1.0.7
715 **/
716void
Richard Hughes12724852018-09-04 13:53:44 +0100717fu_plugin_add_runtime_version (FuPlugin *self,
Richard Hughes0eb123b2018-04-19 12:00:04 +0100718 const gchar *component_id,
719 const gchar *version)
720{
Richard Hughes12724852018-09-04 13:53:44 +0100721 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb01b4862018-04-20 16:39:48 +0100722 if (priv->runtime_versions == NULL)
723 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100724 g_hash_table_insert (priv->runtime_versions,
725 g_strdup (component_id),
726 g_strdup (version));
727}
728
Richard Hughes34e0dab2018-04-20 16:43:00 +0100729void
Richard Hughes12724852018-09-04 13:53:44 +0100730fu_plugin_set_compile_versions (FuPlugin *self, GHashTable *compile_versions)
Richard Hughes34e0dab2018-04-20 16:43:00 +0100731{
Richard Hughes12724852018-09-04 13:53:44 +0100732 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100733 priv->compile_versions = g_hash_table_ref (compile_versions);
734}
735
736/**
737 * fu_plugin_add_compile_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100738 * @self: A #FuPlugin
Richard Hughes34e0dab2018-04-20 16:43:00 +0100739 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
740 * @version: A version string, e.g. "1.2.3"
741 *
Richard Hughesdce91202019-04-08 12:47:45 +0100742 * Sets a compile-time version of a specific dependency.
Richard Hughes34e0dab2018-04-20 16:43:00 +0100743 *
744 * Since: 1.0.7
745 **/
746void
Richard Hughes12724852018-09-04 13:53:44 +0100747fu_plugin_add_compile_version (FuPlugin *self,
Richard Hughes34e0dab2018-04-20 16:43:00 +0100748 const gchar *component_id,
749 const gchar *version)
750{
Richard Hughes12724852018-09-04 13:53:44 +0100751 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100752 if (priv->compile_versions == NULL)
753 return;
754 g_hash_table_insert (priv->compile_versions,
755 g_strdup (component_id),
756 g_strdup (version));
757}
758
Richard Hughes9c028f02017-10-28 21:14:28 +0100759/**
760 * fu_plugin_lookup_quirk_by_id:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100761 * @self: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100762 * @group: A string, e.g. "DfuFlags"
763 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100764 *
765 * Looks up an entry in the hardware database using a string value.
766 *
767 * Returns: (transfer none): values from the database, or %NULL if not found
768 *
769 * Since: 1.0.1
770 **/
771const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100772fu_plugin_lookup_quirk_by_id (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100773{
Richard Hughes12724852018-09-04 13:53:44 +0100774 FuPluginPrivate *priv = GET_PRIVATE (self);
775 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes9c028f02017-10-28 21:14:28 +0100776
Richard Hughes9c028f02017-10-28 21:14:28 +0100777 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100778 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100779}
780
781/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100782 * fu_plugin_lookup_quirk_by_id_as_uint64:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100783 * @self: A #FuPlugin
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100784 * @group: A string, e.g. "DfuFlags"
785 * @key: An ID to match the entry, e.g. "Size"
786 *
787 * Looks up an entry in the hardware database using a string key, returning
788 * an integer value. Values are assumed base 10, unless prefixed with "0x"
789 * where they are parsed as base 16.
790 *
791 * Returns: (transfer none): value from the database, or 0 if not found
792 *
793 * Since: 1.1.2
794 **/
795guint64
Richard Hughes12724852018-09-04 13:53:44 +0100796fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100797{
Richard Hughes12724852018-09-04 13:53:44 +0100798 return fu_common_strtoull (fu_plugin_lookup_quirk_by_id (self, group, key));
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100799}
800
Richard Hughes1354ea92017-09-19 15:58:31 +0100801void
Richard Hughes12724852018-09-04 13:53:44 +0100802fu_plugin_set_smbios (FuPlugin *self, FuSmbios *smbios)
Richard Hughes49e5e052017-09-03 12:15:41 +0100803{
Richard Hughes12724852018-09-04 13:53:44 +0100804 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100805 g_set_object (&priv->smbios, smbios);
806}
807
Richard Hughesb8f8db22017-04-25 15:56:00 +0100808/**
Richard Hughesb0829032017-01-10 09:27:08 +0000809 * fu_plugin_set_coldplug_delay:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100810 * @self: A #FuPlugin
Richard Hughesb0829032017-01-10 09:27:08 +0000811 * @duration: A delay in milliseconds
812 *
813 * Set the minimum time that should be waited inbetween the call to
814 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
815 * to be the minimum hardware initialisation time from a datasheet.
816 *
817 * It is better to use this function rather than using a sleep() in the plugin
818 * itself as then only one delay is done in the daemon rather than waiting for
819 * each coldplug prepare in a serial way.
820 *
821 * Additionally, very long delays should be avoided as the daemon will be
822 * blocked from processing requests whilst the coldplug delay is being
823 * performed.
824 *
825 * Since: 0.8.0
826 **/
827void
Richard Hughes12724852018-09-04 13:53:44 +0100828fu_plugin_set_coldplug_delay (FuPlugin *self, guint duration)
Richard Hughesb0829032017-01-10 09:27:08 +0000829{
Richard Hughes12724852018-09-04 13:53:44 +0100830 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesb0829032017-01-10 09:27:08 +0000831 g_return_if_fail (duration > 0);
832
833 /* check sanity */
834 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
835 g_warning ("duration of %ums is crazy, truncating to %ums",
836 duration,
837 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
838 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
839 }
840
841 /* emit */
Richard Hughes12724852018-09-04 13:53:44 +0100842 g_signal_emit (self, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
Richard Hughesb0829032017-01-10 09:27:08 +0000843}
844
Richard Hughes4b303802019-10-04 13:22:51 +0100845static gboolean
846fu_plugin_device_attach (FuPlugin *self, FuDevice *device, GError **error)
847{
848 g_autoptr(FuDeviceLocker) locker = NULL;
849 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_IS_BOOTLOADER)) {
850 g_debug ("already in runtime mode, skipping");
851 return TRUE;
852 }
853 locker = fu_device_locker_new (device, error);
854 if (locker == NULL)
855 return FALSE;
856 return fu_device_attach (device, error);
857}
858
859static gboolean
860fu_plugin_device_detach (FuPlugin *self, FuDevice *device, GError **error)
861{
862 g_autoptr(FuDeviceLocker) locker = NULL;
863 if (fu_device_has_flag (device, FWUPD_DEVICE_FLAG_IS_BOOTLOADER)) {
864 g_debug ("already in bootloader mode, skipping");
865 return TRUE;
866 }
867 locker = fu_device_locker_new (device, error);
868 if (locker == NULL)
869 return FALSE;
870 return fu_device_detach (device, error);
871}
872
873static gboolean
874fu_plugin_device_reload (FuPlugin *self, FuDevice *device, GError **error)
875{
876 g_autoptr(FuDeviceLocker) locker = NULL;
877 locker = fu_device_locker_new (device, error);
878 if (locker == NULL)
879 return FALSE;
880 return fu_device_reload (device, error);
881}
882
883static gboolean
884fu_plugin_device_activate (FuPlugin *self, FuDevice *device, GError **error)
885{
886 g_autoptr(FuDeviceLocker) locker = NULL;
887 locker = fu_device_locker_new (device, error);
888 if (locker == NULL)
889 return FALSE;
890 return fu_device_activate (device, error);
891}
892
893static gboolean
894fu_plugin_device_write_firmware (FuPlugin *self, FuDevice *device,
895 GBytes *fw, FwupdInstallFlags flags,
896 GError **error)
897{
898 g_autoptr(FuDeviceLocker) locker = NULL;
899 locker = fu_device_locker_new (device, error);
900 if (locker == NULL)
901 return FALSE;
902 return fu_device_write_firmware (device, fw, flags, error);
903}
904
Richard Hughes7f677212019-10-05 16:19:40 +0100905static gboolean
906fu_plugin_device_read_firmware (FuPlugin *self, FuDevice *device, GError **error)
907{
908 g_autoptr(FuDeviceLocker) locker = NULL;
909 g_autoptr(GBytes) fw = NULL;
910 GChecksumType checksum_types[] = {
911 G_CHECKSUM_SHA1,
912 G_CHECKSUM_SHA256,
913 0 };
914 locker = fu_device_locker_new (device, error);
915 if (locker == NULL)
916 return FALSE;
917 if (!fu_device_detach (device, error))
918 return FALSE;
919 fw = fu_device_read_firmware (device, error);
920 if (fw == NULL) {
921 g_autoptr(GError) error_local = NULL;
922 if (!fu_device_attach (device, &error_local))
923 g_debug ("ignoring: %s", error_local->message);
924 return FALSE;
925 }
926 for (guint i = 0; checksum_types[i] != 0; i++) {
927 g_autofree gchar *hash = NULL;
928 hash = g_compute_checksum_for_bytes (checksum_types[i], fw);
929 fu_device_add_checksum (device, hash);
930 }
931 return fu_device_attach (device, error);
932}
933
Richard Hughesd0905142016-03-13 09:46:49 +0000934gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100935fu_plugin_runner_startup (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000936{
Richard Hughes12724852018-09-04 13:53:44 +0100937 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000938 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000939 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000940
941 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000942 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000943 return TRUE;
944
Richard Hughes639da472018-01-06 22:35:04 +0000945 /* no object loaded */
946 if (priv->module == NULL)
947 return TRUE;
948
Richard Hughesd0905142016-03-13 09:46:49 +0000949 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000950 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
951 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000952 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000953 g_debug ("performing startup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +0000954 if (!func (self, &error_local)) {
955 if (error_local == NULL) {
956 g_critical ("unset error in plugin %s for startup()",
957 priv->name);
958 g_set_error_literal (&error_local,
959 FWUPD_ERROR,
960 FWUPD_ERROR_INTERNAL,
961 "unspecified error");
962 }
963 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
964 "failed to startup using %s: ",
965 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +0000966 return FALSE;
967 }
968 return TRUE;
969}
970
971static gboolean
972fu_plugin_runner_offline_invalidate (GError **error)
973{
974 g_autoptr(GError) error_local = NULL;
975 g_autoptr(GFile) file1 = NULL;
976
977 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
978
979 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
980 if (!g_file_query_exists (file1, NULL))
981 return TRUE;
982 if (!g_file_delete (file1, NULL, &error_local)) {
983 g_set_error (error,
984 FWUPD_ERROR,
985 FWUPD_ERROR_INTERNAL,
986 "Cannot delete %s: %s",
987 FU_OFFLINE_TRIGGER_FILENAME,
988 error_local->message);
989 return FALSE;
990 }
991 return TRUE;
992}
993
994static gboolean
995fu_plugin_runner_offline_setup (GError **error)
996{
997 gint rc;
Richard Hughes484ee292019-03-22 16:10:50 +0000998 g_autofree gchar *filename = NULL;
Mario Limoncielloe1b4b202019-04-30 10:01:19 -0500999 g_autofree gchar *symlink_target = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001000
1001 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1002
Richard Hughes484ee292019-03-22 16:10:50 +00001003 /* does already exist */
1004 filename = fu_common_realpath (FU_OFFLINE_TRIGGER_FILENAME, NULL);
1005 if (g_strcmp0 (filename, symlink_target) == 0) {
1006 g_debug ("%s already points to %s, skipping creation",
1007 FU_OFFLINE_TRIGGER_FILENAME, symlink_target);
1008 return TRUE;
1009 }
1010
Richard Hughescff38bc2016-12-12 12:03:37 +00001011 /* create symlink for the systemd-system-update-generator */
Richard Hughes484ee292019-03-22 16:10:50 +00001012 rc = symlink (symlink_target, FU_OFFLINE_TRIGGER_FILENAME);
Richard Hughescff38bc2016-12-12 12:03:37 +00001013 if (rc < 0) {
1014 g_set_error (error,
1015 FWUPD_ERROR,
1016 FWUPD_ERROR_INTERNAL,
1017 "Failed to create symlink %s to %s: %s",
1018 FU_OFFLINE_TRIGGER_FILENAME,
1019 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +00001020 return FALSE;
1021 }
1022 return TRUE;
1023}
1024
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001025static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001026fu_plugin_runner_device_generic (FuPlugin *self, FuDevice *device,
Richard Hughes4b303802019-10-04 13:22:51 +01001027 const gchar *symbol_name,
1028 FuPluginDeviceFunc device_func,
1029 GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001030{
Richard Hughes12724852018-09-04 13:53:44 +01001031 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001032 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001033 g_autoptr(GError) error_local = NULL;
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001034
1035 /* not enabled */
1036 if (!priv->enabled)
1037 return TRUE;
1038
Richard Hughesd3d96cc2017-11-14 11:34:33 +00001039 /* no object loaded */
1040 if (priv->module == NULL)
1041 return TRUE;
1042
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001043 /* optional */
1044 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
Richard Hughes4b303802019-10-04 13:22:51 +01001045 if (func == NULL) {
1046 if (device_func != NULL) {
1047 g_debug ("running superclassed %s() on %s",
1048 symbol_name + 10, priv->name);
1049 return device_func (self, device, error);
1050 }
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001051 return TRUE;
Richard Hughes4b303802019-10-04 13:22:51 +01001052 }
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001053 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001054 if (!func (self, device, &error_local)) {
1055 if (error_local == NULL) {
1056 g_critical ("unset error in plugin %s for %s()",
1057 priv->name, symbol_name + 10);
1058 g_set_error_literal (&error_local,
1059 FWUPD_ERROR,
1060 FWUPD_ERROR_INTERNAL,
1061 "unspecified error");
1062 }
1063 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1064 "failed to %s using %s: ",
1065 symbol_name + 10, priv->name);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001066 return FALSE;
1067 }
1068 return TRUE;
1069}
1070
Richard Hughesdbd8c762018-06-15 20:31:40 +01001071static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001072fu_plugin_runner_flagged_device_generic (FuPlugin *self, FwupdInstallFlags flags,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001073 FuDevice *device,
1074 const gchar *symbol_name, GError **error)
1075{
Richard Hughes12724852018-09-04 13:53:44 +01001076 FuPluginPrivate *priv = GET_PRIVATE (self);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001077 FuPluginFlaggedDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001078 g_autoptr(GError) error_local = NULL;
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001079
1080 /* not enabled */
1081 if (!priv->enabled)
1082 return TRUE;
1083
1084 /* no object loaded */
1085 if (priv->module == NULL)
1086 return TRUE;
1087
1088 /* optional */
1089 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1090 if (func == NULL)
1091 return TRUE;
1092 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001093 if (!func (self, flags, device, &error_local)) {
1094 if (error_local == NULL) {
1095 g_critical ("unset error in plugin %s for %s()",
1096 priv->name, symbol_name + 10);
1097 g_set_error_literal (&error_local,
1098 FWUPD_ERROR,
1099 FWUPD_ERROR_INTERNAL,
1100 "unspecified error");
1101 }
1102 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1103 "failed to %s using %s: ",
1104 symbol_name + 10, priv->name);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001105 return FALSE;
1106 }
1107 return TRUE;
1108
1109}
1110
1111static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001112fu_plugin_runner_device_array_generic (FuPlugin *self, GPtrArray *devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001113 const gchar *symbol_name, GError **error)
1114{
Richard Hughes12724852018-09-04 13:53:44 +01001115 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesdbd8c762018-06-15 20:31:40 +01001116 FuPluginDeviceArrayFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001117 g_autoptr(GError) error_local = NULL;
Richard Hughesdbd8c762018-06-15 20:31:40 +01001118
1119 /* not enabled */
1120 if (!priv->enabled)
1121 return TRUE;
1122
1123 /* no object loaded */
1124 if (priv->module == NULL)
1125 return TRUE;
1126
1127 /* optional */
1128 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
1129 if (func == NULL)
1130 return TRUE;
1131 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001132 if (!func (self, devices, &error_local)) {
1133 if (error_local == NULL) {
1134 g_critical ("unset error in plugin %s for %s()",
1135 priv->name, symbol_name + 10);
1136 g_set_error_literal (&error_local,
1137 FWUPD_ERROR,
1138 FWUPD_ERROR_INTERNAL,
1139 "unspecified error");
1140 }
1141 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1142 "failed to %s using %s: ",
1143 symbol_name + 10, priv->name);
Richard Hughesdbd8c762018-06-15 20:31:40 +01001144 return FALSE;
1145 }
1146 return TRUE;
1147}
1148
Richard Hughesd0905142016-03-13 09:46:49 +00001149gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001150fu_plugin_runner_coldplug (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001151{
Richard Hughes12724852018-09-04 13:53:44 +01001152 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001153 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001154 g_autoptr(GError) error_local = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +00001155
1156 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +00001157 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +00001158 return TRUE;
1159
Richard Hughes639da472018-01-06 22:35:04 +00001160 /* no object loaded */
1161 if (priv->module == NULL)
1162 return TRUE;
1163
Richard Hughesd0905142016-03-13 09:46:49 +00001164 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001165 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
1166 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +00001167 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001168 g_debug ("performing coldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001169 if (!func (self, &error_local)) {
1170 if (error_local == NULL) {
1171 g_critical ("unset error in plugin %s for coldplug()",
1172 priv->name);
1173 g_set_error_literal (&error_local,
1174 FWUPD_ERROR,
1175 FWUPD_ERROR_INTERNAL,
1176 "unspecified error");
1177 }
1178 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1179 "failed to coldplug using %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001180 return FALSE;
1181 }
1182 return TRUE;
1183}
1184
Richard Hughes7b8b2022016-12-12 16:15:03 +00001185gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001186fu_plugin_runner_recoldplug (FuPlugin *self, GError **error)
Richard Hughes2de8f132018-01-17 09:12:02 +00001187{
Richard Hughes12724852018-09-04 13:53:44 +01001188 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes2de8f132018-01-17 09:12:02 +00001189 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001190 g_autoptr(GError) error_local = NULL;
Richard Hughes2de8f132018-01-17 09:12:02 +00001191
1192 /* not enabled */
1193 if (!priv->enabled)
1194 return TRUE;
1195
1196 /* no object loaded */
1197 if (priv->module == NULL)
1198 return TRUE;
1199
1200 /* optional */
1201 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
1202 if (func == NULL)
1203 return TRUE;
1204 g_debug ("performing recoldplug() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001205 if (!func (self, &error_local)) {
1206 if (error_local == NULL) {
1207 g_critical ("unset error in plugin %s for recoldplug()",
1208 priv->name);
1209 g_set_error_literal (&error_local,
1210 FWUPD_ERROR,
1211 FWUPD_ERROR_INTERNAL,
1212 "unspecified error");
1213 }
1214 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1215 "failed to recoldplug using %s: ",
1216 priv->name);
Richard Hughes2de8f132018-01-17 09:12:02 +00001217 return FALSE;
1218 }
1219 return TRUE;
1220}
1221
1222gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001223fu_plugin_runner_coldplug_prepare (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001224{
Richard Hughes12724852018-09-04 13:53:44 +01001225 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001226 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001227 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001228
1229 /* not enabled */
1230 if (!priv->enabled)
1231 return TRUE;
1232
Richard Hughes639da472018-01-06 22:35:04 +00001233 /* no object loaded */
1234 if (priv->module == NULL)
1235 return TRUE;
1236
Richard Hughes46487c92017-01-07 21:26:34 +00001237 /* optional */
1238 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1239 if (func == NULL)
1240 return TRUE;
1241 g_debug ("performing coldplug_prepare() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001242 if (!func (self, &error_local)) {
1243 if (error_local == NULL) {
1244 g_critical ("unset error in plugin %s for coldplug_prepare()",
1245 priv->name);
1246 g_set_error_literal (&error_local,
1247 FWUPD_ERROR,
1248 FWUPD_ERROR_INTERNAL,
1249 "unspecified error");
1250 }
1251 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1252 "failed to coldplug_prepare using %s: ",
1253 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001254 return FALSE;
1255 }
1256 return TRUE;
1257}
1258
1259gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001260fu_plugin_runner_coldplug_cleanup (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001261{
Richard Hughes12724852018-09-04 13:53:44 +01001262 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001263 FuPluginStartupFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001264 g_autoptr(GError) error_local = NULL;
Richard Hughes46487c92017-01-07 21:26:34 +00001265
1266 /* not enabled */
1267 if (!priv->enabled)
1268 return TRUE;
1269
Richard Hughes639da472018-01-06 22:35:04 +00001270 /* no object loaded */
1271 if (priv->module == NULL)
1272 return TRUE;
1273
Richard Hughes46487c92017-01-07 21:26:34 +00001274 /* optional */
1275 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1276 if (func == NULL)
1277 return TRUE;
1278 g_debug ("performing coldplug_cleanup() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001279 if (!func (self, &error_local)) {
1280 if (error_local == NULL) {
1281 g_critical ("unset error in plugin %s for coldplug_cleanup()",
1282 priv->name);
1283 g_set_error_literal (&error_local,
1284 FWUPD_ERROR,
1285 FWUPD_ERROR_INTERNAL,
1286 "unspecified error");
1287 }
1288 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1289 "failed to coldplug_cleanup using %s: ",
1290 priv->name);
Richard Hughes46487c92017-01-07 21:26:34 +00001291 return FALSE;
1292 }
1293 return TRUE;
1294}
1295
1296gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001297fu_plugin_runner_composite_prepare (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001298{
Richard Hughes12724852018-09-04 13:53:44 +01001299 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001300 "fu_plugin_composite_prepare",
1301 error);
1302}
1303
1304gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001305fu_plugin_runner_composite_cleanup (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001306{
Richard Hughes12724852018-09-04 13:53:44 +01001307 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001308 "fu_plugin_composite_cleanup",
1309 error);
1310}
1311
1312gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001313fu_plugin_runner_update_prepare (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001314 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001315{
Richard Hughes12724852018-09-04 13:53:44 +01001316 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001317 "fu_plugin_update_prepare",
1318 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001319}
1320
1321gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001322fu_plugin_runner_update_cleanup (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001323 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001324{
Richard Hughes12724852018-09-04 13:53:44 +01001325 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001326 "fu_plugin_update_cleanup",
1327 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001328}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001329
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001330gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001331fu_plugin_runner_update_attach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001332{
Richard Hughes12724852018-09-04 13:53:44 +01001333 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001334 "fu_plugin_update_attach",
1335 fu_plugin_device_attach,
1336 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001337}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001338
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001339gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001340fu_plugin_runner_update_detach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001341{
Richard Hughes12724852018-09-04 13:53:44 +01001342 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001343 "fu_plugin_update_detach",
1344 fu_plugin_device_detach,
1345 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001346}
1347
1348gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001349fu_plugin_runner_update_reload (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001350{
Richard Hughes12724852018-09-04 13:53:44 +01001351 return fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001352 "fu_plugin_update_reload",
1353 fu_plugin_device_reload,
1354 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001355}
1356
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001357/**
1358 * fu_plugin_add_udev_subsystem:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001359 * @self: a #FuPlugin
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001360 * @subsystem: a subsystem name, e.g. `pciport`
1361 *
1362 * Registers the udev subsystem to be watched by the daemon.
1363 *
1364 * Plugins can use this method only in fu_plugin_init()
1365 **/
1366void
Richard Hughes12724852018-09-04 13:53:44 +01001367fu_plugin_add_udev_subsystem (FuPlugin *self, const gchar *subsystem)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001368{
Richard Hughes12724852018-09-04 13:53:44 +01001369 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001370 for (guint i = 0; i < priv->udev_subsystems->len; i++) {
1371 const gchar *subsystem_tmp = g_ptr_array_index (priv->udev_subsystems, i);
1372 if (g_strcmp0 (subsystem_tmp, subsystem) == 0)
1373 return;
1374 }
1375 g_debug ("added udev subsystem watch of %s", subsystem);
1376 g_ptr_array_add (priv->udev_subsystems, g_strdup (subsystem));
1377}
1378
Richard Hughes989acf12019-10-05 20:16:47 +01001379/**
1380 * fu_plugin_set_device_gtype:
1381 * @self: a #FuPlugin
1382 * @device_gtype: a #GType `FU_TYPE_DEVICE`
1383 *
1384 * Sets the device #GType which is used when creating devices.
1385 *
1386 * If this method is used then fu_plugin_usb_device_added() is not called, and
1387 * instead the object is created in the daemon for the plugin.
1388 *
1389 * Plugins can use this method only in fu_plugin_init()
1390 *
1391 * Since: 1.3.3
1392 **/
1393void
1394fu_plugin_set_device_gtype (FuPlugin *self, GType device_gtype)
1395{
1396 FuPluginPrivate *priv = GET_PRIVATE (self);
1397 priv->device_gtype = device_gtype;
1398}
1399
1400static gboolean
1401fu_plugin_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
1402{
1403 FuPluginPrivate *priv = GET_PRIVATE (self);
1404 g_autoptr(FuDevice) dev = NULL;
1405 g_autoptr(FuDeviceLocker) locker = NULL;
1406 dev = g_object_new (priv->device_gtype, NULL);
1407 fu_device_incorporate (FU_DEVICE (dev), FU_DEVICE (device));
1408 locker = fu_device_locker_new (dev, error);
1409 if (locker == NULL)
1410 return FALSE;
1411 fu_plugin_device_add (self, FU_DEVICE (dev));
1412 return TRUE;
1413}
1414
Richard Hughes104f6512017-11-24 11:44:57 +00001415gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001416fu_plugin_runner_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
Richard Hughes104f6512017-11-24 11:44:57 +00001417{
Richard Hughes12724852018-09-04 13:53:44 +01001418 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes104f6512017-11-24 11:44:57 +00001419 FuPluginUsbDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001420 g_autoptr(GError) error_local = NULL;
Richard Hughes104f6512017-11-24 11:44:57 +00001421
1422 /* not enabled */
1423 if (!priv->enabled)
1424 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001425
1426 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001427 if (priv->module == NULL)
1428 return TRUE;
1429
1430 /* optional */
1431 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
Richard Hughes989acf12019-10-05 20:16:47 +01001432 if (func == NULL) {
1433 if (priv->device_gtype != G_TYPE_INVALID) {
1434 g_debug ("using generic usb_device_added() on %s", priv->name);
1435 return fu_plugin_usb_device_added (self, device, error);
1436 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001437 return TRUE;
Richard Hughes989acf12019-10-05 20:16:47 +01001438 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001439 g_debug ("performing usb_device_added() on %s", priv->name);
1440 if (!func (self, device, &error_local)) {
1441 if (error_local == NULL) {
1442 g_critical ("unset error in plugin %s for usb_device_added()",
1443 priv->name);
1444 g_set_error_literal (&error_local,
1445 FWUPD_ERROR,
1446 FWUPD_ERROR_INTERNAL,
1447 "unspecified error");
1448 }
1449 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1450 "failed to add device using on %s: ",
1451 priv->name);
1452 return FALSE;
Richard Hughes104f6512017-11-24 11:44:57 +00001453 }
1454 return TRUE;
1455}
1456
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001457gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001458fu_plugin_runner_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001459{
Richard Hughes12724852018-09-04 13:53:44 +01001460 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001461 FuPluginUdevDeviceAddedFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001462 g_autoptr(GError) error_local = NULL;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001463
1464 /* not enabled */
1465 if (!priv->enabled)
1466 return TRUE;
1467
1468 /* no object loaded */
1469 if (priv->module == NULL)
1470 return TRUE;
1471
1472 /* optional */
1473 g_module_symbol (priv->module, "fu_plugin_udev_device_added", (gpointer *) &func);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001474 if (func == NULL)
1475 return TRUE;
1476 g_debug ("performing udev_device_added() on %s", priv->name);
1477 if (!func (self, device, &error_local)) {
1478 if (error_local == NULL) {
1479 g_critical ("unset error in plugin %s for udev_device_added()",
1480 priv->name);
1481 g_set_error_literal (&error_local,
1482 FWUPD_ERROR,
1483 FWUPD_ERROR_INTERNAL,
1484 "unspecified error");
1485 }
1486 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1487 "failed to add device using on %s: ",
1488 priv->name);
1489 return FALSE;
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001490 }
1491 return TRUE;
1492}
1493
Richard Hughes5e952ce2019-08-26 11:09:46 +01001494gboolean
1495fu_plugin_runner_udev_device_changed (FuPlugin *self, FuUdevDevice *device, GError **error)
1496{
1497 FuPluginPrivate *priv = GET_PRIVATE (self);
1498 FuPluginUdevDeviceAddedFunc func = NULL;
1499 g_autoptr(GError) error_local = NULL;
1500
1501 /* not enabled */
1502 if (!priv->enabled)
1503 return TRUE;
1504
1505 /* no object loaded */
1506 if (priv->module == NULL)
1507 return TRUE;
1508
1509 /* optional */
1510 g_module_symbol (priv->module, "fu_plugin_udev_device_changed", (gpointer *) &func);
1511 if (func == NULL)
1512 return TRUE;
1513 g_debug ("performing udev_device_changed() on %s", priv->name);
1514 if (!func (self, device, &error_local)) {
1515 if (error_local == NULL) {
1516 g_critical ("unset error in plugin %s for udev_device_changed()",
1517 priv->name);
1518 g_set_error_literal (&error_local,
1519 FWUPD_ERROR,
1520 FWUPD_ERROR_INTERNAL,
1521 "unspecified error");
1522 }
1523 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1524 "failed to change device on %s: ",
1525 priv->name);
1526 return FALSE;
1527 }
1528 return TRUE;
1529}
1530
Richard Hughese1fd34d2017-08-24 14:19:51 +01001531void
Richard Hughes12724852018-09-04 13:53:44 +01001532fu_plugin_runner_device_removed (FuPlugin *self, FuDevice *device)
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001533{
1534 g_autoptr(GError) error_local= NULL;
1535
Richard Hughes12724852018-09-04 13:53:44 +01001536 if (!fu_plugin_runner_device_generic (self, device,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001537 "fu_plugin_device_removed",
Richard Hughes4b303802019-10-04 13:22:51 +01001538 NULL,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001539 &error_local))
1540 g_warning ("%s", error_local->message);
1541}
1542
1543void
Richard Hughes12724852018-09-04 13:53:44 +01001544fu_plugin_runner_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +01001545{
Richard Hughes12724852018-09-04 13:53:44 +01001546 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001547 FuPluginDeviceRegisterFunc func = NULL;
1548
1549 /* not enabled */
1550 if (!priv->enabled)
1551 return;
Richard Hughes34834102017-11-21 21:55:00 +00001552 if (priv->module == NULL)
1553 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001554
Mario Limonciello4910b242018-06-22 15:04:21 -05001555 /* don't notify plugins on their own devices */
Richard Hughes12724852018-09-04 13:53:44 +01001556 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (self)) == 0)
Mario Limonciello4910b242018-06-22 15:04:21 -05001557 return;
1558
Richard Hughese1fd34d2017-08-24 14:19:51 +01001559 /* optional */
1560 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1561 if (func != NULL) {
Richard Hughes1bf7ff92018-08-24 20:21:35 +01001562 g_debug ("performing fu_plugin_device_registered() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001563 func (self, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001564 }
1565}
1566
Richard Hughesc6c312f2019-02-01 16:37:14 +00001567gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001568fu_plugin_runner_schedule_update (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001569 FuDevice *device,
Richard Hughes994b4d92019-03-25 14:28:30 +00001570 FwupdRelease *release,
Richard Hughescff38bc2016-12-12 12:03:37 +00001571 GBytes *blob_cab,
Richard Hughes5cbb5cf2019-04-26 16:48:03 +01001572 FwupdInstallFlags flags,
Richard Hughescff38bc2016-12-12 12:03:37 +00001573 GError **error)
1574{
Richard Hughes0a906262019-05-16 13:38:47 +01001575 gchar tmpname[] = {"XXXXXX.cab"};
Richard Hughescff38bc2016-12-12 12:03:37 +00001576 g_autofree gchar *dirname = NULL;
1577 g_autofree gchar *filename = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001578 g_autoptr(FuHistory) history = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001579 g_autoptr(GFile) file = NULL;
1580
1581 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001582 history = fu_history_new ();
Richard Hughes5cbb5cf2019-04-26 16:48:03 +01001583 if ((flags & FWUPD_INSTALL_FLAG_FORCE) == 0) {
1584 g_autoptr(FuDevice) res_tmp = NULL;
1585 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
1586 if (res_tmp != NULL &&
1587 fu_device_get_update_state (res_tmp) == FWUPD_UPDATE_STATE_PENDING) {
1588 g_set_error (error,
1589 FWUPD_ERROR,
1590 FWUPD_ERROR_ALREADY_PENDING,
1591 "%s is already scheduled to be updated",
1592 fu_device_get_id (device));
1593 return FALSE;
1594 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001595 }
1596
1597 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001598 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001599 file = g_file_new_for_path (dirname);
1600 if (!g_file_query_exists (file, NULL)) {
1601 if (!g_file_make_directory_with_parents (file, NULL, error))
1602 return FALSE;
1603 }
1604
1605 /* get a random filename */
1606 for (guint i = 0; i < 6; i++)
1607 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1608 filename = g_build_filename (dirname, tmpname, NULL);
1609
1610 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001611 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001612 if (!g_file_set_contents (filename,
1613 g_bytes_get_data (blob_cab, NULL),
1614 (gssize) g_bytes_get_size (blob_cab),
1615 error))
1616 return FALSE;
1617
1618 /* schedule for next boot */
1619 g_debug ("schedule %s to be installed to %s on next boot",
1620 filename, fu_device_get_id (device));
Richard Hughes994b4d92019-03-25 14:28:30 +00001621 fwupd_release_set_filename (release, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001622
1623 /* add to database */
Richard Hughes809abea2019-03-23 11:06:18 +00001624 fu_device_add_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT);
Richard Hughes3e90a582018-01-06 22:38:09 +00001625 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes994b4d92019-03-25 14:28:30 +00001626 if (!fu_history_add_device (history, device, release, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001627 return FALSE;
1628
1629 /* next boot we run offline */
Richard Hughesdb69c812019-03-22 16:10:15 +00001630 fu_device_set_progress (device, 100);
Richard Hughescff38bc2016-12-12 12:03:37 +00001631 return fu_plugin_runner_offline_setup (error);
1632}
1633
1634gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001635fu_plugin_runner_verify (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001636 FuDevice *device,
1637 FuPluginVerifyFlags flags,
1638 GError **error)
1639{
Richard Hughes12724852018-09-04 13:53:44 +01001640 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001641 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001642 GPtrArray *checksums;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001643 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001644
1645 /* not enabled */
1646 if (!priv->enabled)
1647 return TRUE;
1648
Richard Hughes639da472018-01-06 22:35:04 +00001649 /* no object loaded */
1650 if (priv->module == NULL)
1651 return TRUE;
1652
Richard Hughescff38bc2016-12-12 12:03:37 +00001653 /* optional */
1654 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
Richard Hughes7f677212019-10-05 16:19:40 +01001655 if (func == NULL) {
1656 g_debug ("running superclassed read_firmware() on %s", priv->name);
1657 return fu_plugin_device_read_firmware (self, device, error);
1658 }
Richard Hughes1812fc72018-12-14 11:37:54 +00001659
1660 /* clear any existing verification checksums */
1661 checksums = fu_device_get_checksums (device);
1662 g_ptr_array_set_size (checksums, 0);
1663
Richard Hughesc9223be2019-03-18 08:46:42 +00001664 /* run additional detach */
1665 if (!fu_plugin_runner_device_generic (self, device,
1666 "fu_plugin_verify_detach",
Richard Hughes4b303802019-10-04 13:22:51 +01001667 fu_plugin_device_detach,
Richard Hughesc9223be2019-03-18 08:46:42 +00001668 error))
1669 return FALSE;
1670
Richard Hughes1812fc72018-12-14 11:37:54 +00001671 /* run vfunc */
Richard Hughescff38bc2016-12-12 12:03:37 +00001672 g_debug ("performing verify() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001673 if (!func (self, device, flags, &error_local)) {
Richard Hughesc9223be2019-03-18 08:46:42 +00001674 g_autoptr(GError) error_attach = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001675 if (error_local == NULL) {
1676 g_critical ("unset error in plugin %s for verify()",
1677 priv->name);
1678 g_set_error_literal (&error_local,
1679 FWUPD_ERROR,
1680 FWUPD_ERROR_INTERNAL,
1681 "unspecified error");
1682 }
1683 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1684 "failed to verify using %s: ",
1685 priv->name);
Richard Hughesc9223be2019-03-18 08:46:42 +00001686 /* make the device "work" again, but don't prefix the error */
1687 if (!fu_plugin_runner_device_generic (self, device,
1688 "fu_plugin_verify_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01001689 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00001690 &error_attach)) {
1691 g_warning ("failed to attach whilst aborting verify(): %s",
1692 error_attach->message);
1693 }
Richard Hughesd0905142016-03-13 09:46:49 +00001694 return FALSE;
1695 }
Richard Hughesc9223be2019-03-18 08:46:42 +00001696
1697 /* run optional attach */
1698 if (!fu_plugin_runner_device_generic (self, device,
1699 "fu_plugin_verify_attach",
Richard Hughes4b303802019-10-04 13:22:51 +01001700 fu_plugin_device_attach,
Richard Hughesc9223be2019-03-18 08:46:42 +00001701 error))
1702 return FALSE;
1703
1704 /* success */
Richard Hughesd0905142016-03-13 09:46:49 +00001705 return TRUE;
1706}
1707
Richard Hughesd0905142016-03-13 09:46:49 +00001708gboolean
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001709fu_plugin_runner_activate (FuPlugin *self, FuDevice *device, GError **error)
1710{
1711 guint64 flags;
1712
1713 /* final check */
1714 flags = fu_device_get_flags (device);
1715 if ((flags & FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION) == 0) {
1716 g_set_error (error,
1717 FWUPD_ERROR,
1718 FWUPD_ERROR_NOT_SUPPORTED,
1719 "Device %s does not need activation",
1720 fu_device_get_id (device));
1721 return FALSE;
1722 }
1723
1724 /* run vfunc */
1725 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001726 "fu_plugin_activate",
1727 fu_plugin_device_activate,
1728 error))
Mario Limonciello96a0dd52019-02-25 13:50:03 -06001729 return FALSE;
1730
1731 /* update with correct flags */
1732 fu_device_remove_flag (device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION);
1733 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1734 return TRUE;
1735}
1736
1737gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001738fu_plugin_runner_unlock (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001739{
Richard Hughescff38bc2016-12-12 12:03:37 +00001740 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001741
1742 /* final check */
1743 flags = fu_device_get_flags (device);
1744 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1745 g_set_error (error,
1746 FWUPD_ERROR,
1747 FWUPD_ERROR_NOT_SUPPORTED,
1748 "Device %s is not locked",
1749 fu_device_get_id (device));
1750 return FALSE;
1751 }
1752
Richard Hughes9c4b5312017-11-14 11:34:53 +00001753 /* run vfunc */
Richard Hughes12724852018-09-04 13:53:44 +01001754 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes4b303802019-10-04 13:22:51 +01001755 "fu_plugin_unlock",
1756 NULL,
1757 error))
Richard Hughes9c4b5312017-11-14 11:34:53 +00001758 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001759
1760 /* update with correct flags */
1761 flags = fu_device_get_flags (device);
1762 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1763 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1764 return TRUE;
1765}
1766
1767gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001768fu_plugin_runner_update (FuPlugin *self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001769 FuDevice *device,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001770 GBytes *blob_fw,
1771 FwupdInstallFlags flags,
1772 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001773{
Richard Hughes12724852018-09-04 13:53:44 +01001774 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001775 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001776 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001777 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001778 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001779
1780 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001781 if (!priv->enabled) {
1782 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001783 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001784 }
Richard Hughesd0905142016-03-13 09:46:49 +00001785
Richard Hughes639da472018-01-06 22:35:04 +00001786 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001787 if (priv->module == NULL) {
1788 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001789 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001790 }
Richard Hughes639da472018-01-06 22:35:04 +00001791
Richard Hughesd0905142016-03-13 09:46:49 +00001792 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001793 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1794 if (update_func == NULL) {
Richard Hughes4b303802019-10-04 13:22:51 +01001795 g_debug ("running superclassed write_firmware() on %s", priv->name);
1796 return fu_plugin_device_write_firmware (self, device, blob_fw, flags, error);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001797 }
Richard Hughesd0905142016-03-13 09:46:49 +00001798
Richard Hughescff38bc2016-12-12 12:03:37 +00001799 /* cancel the pending action */
1800 if (!fu_plugin_runner_offline_invalidate (error))
1801 return FALSE;
1802
1803 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001804 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001805 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001806 if (!update_func (self, device, blob_fw, flags, &error_local)) {
1807 if (error_local == NULL) {
1808 g_critical ("unset error in plugin %s for update()",
1809 priv->name);
1810 g_set_error_literal (&error_local,
Richard Hughes3c8ada32018-10-12 10:08:58 +01001811 FWUPD_ERROR,
1812 FWUPD_ERROR_INTERNAL,
1813 "unspecified error");
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001814 return FALSE;
Richard Hughes3c8ada32018-10-12 10:08:58 +01001815 }
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001816 fu_device_set_update_error (device, error_local->message);
1817 g_propagate_error (error, g_steal_pointer (&error_local));
Richard Hughescff38bc2016-12-12 12:03:37 +00001818 return FALSE;
1819 }
1820
Richard Hughesf556d372017-06-15 19:49:18 +01001821 /* no longer valid */
Richard Hughesf8039642019-01-16 12:22:22 +00001822 if (!fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT) &&
1823 !fu_device_has_flag (device, FWUPD_DEVICE_FLAG_NEEDS_SHUTDOWN)) {
Richard Hughes08435162018-12-12 10:34:16 +00001824 GPtrArray *checksums = fu_device_get_checksums (device);
1825 g_ptr_array_set_size (checksums, 0);
1826 }
Richard Hughesf556d372017-06-15 19:49:18 +01001827
Richard Hughescff38bc2016-12-12 12:03:37 +00001828 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001829 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001830 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001831 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001832
Richard Hughes780ef3f2018-01-12 16:20:31 +00001833 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001834 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1835 if (!fu_history_modify_device (history, device,
1836 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1837 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001838 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001839
1840 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001841 release = fu_device_get_release_default (device_pending);
1842 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001843 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001844 g_autoptr(GError) error_delete = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001845 g_autoptr(GFile) file = NULL;
1846 file = g_file_new_for_path (tmp);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001847 if (!g_file_delete (file, NULL, &error_delete)) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001848 g_set_error (error,
1849 FWUPD_ERROR,
1850 FWUPD_ERROR_INVALID_FILE,
1851 "Failed to delete %s: %s",
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001852 tmp, error_delete->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001853 return FALSE;
1854 }
1855 }
1856 }
Richard Hughesd0905142016-03-13 09:46:49 +00001857 return TRUE;
1858}
Richard Hughescff38bc2016-12-12 12:03:37 +00001859
1860gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001861fu_plugin_runner_clear_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001862{
Richard Hughes12724852018-09-04 13:53:44 +01001863 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001864 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001865 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001866
1867 /* not enabled */
1868 if (!priv->enabled)
1869 return TRUE;
1870
Richard Hughes639da472018-01-06 22:35:04 +00001871 /* no object loaded */
1872 if (priv->module == NULL)
1873 return TRUE;
1874
Richard Hughes65e44ca2018-01-30 17:26:30 +00001875 /* optional */
1876 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1877 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001878 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001879 g_debug ("performing clear_result() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001880 if (!func (self, device, &error_local)) {
1881 if (error_local == NULL) {
1882 g_critical ("unset error in plugin %s for clear_result()",
1883 priv->name);
1884 g_set_error_literal (&error_local,
1885 FWUPD_ERROR,
1886 FWUPD_ERROR_INTERNAL,
1887 "unspecified error");
1888 }
1889 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1890 "failed to clear_result using %s: ",
1891 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001892 return FALSE;
1893 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001894 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001895}
1896
1897gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001898fu_plugin_runner_get_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001899{
Richard Hughes12724852018-09-04 13:53:44 +01001900 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001901 FuPluginDeviceFunc func = NULL;
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001902 g_autoptr(GError) error_local = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001903
1904 /* not enabled */
1905 if (!priv->enabled)
1906 return TRUE;
1907
Richard Hughes639da472018-01-06 22:35:04 +00001908 /* no object loaded */
1909 if (priv->module == NULL)
1910 return TRUE;
1911
Richard Hughes65e44ca2018-01-30 17:26:30 +00001912 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001913 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001914 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001915 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001916 g_debug ("performing get_results() on %s", priv->name);
Richard Hughescd2fb3e2018-11-29 19:58:09 +00001917 if (!func (self, device, &error_local)) {
1918 if (error_local == NULL) {
1919 g_critical ("unset error in plugin %s for get_results()",
1920 priv->name);
1921 g_set_error_literal (&error_local,
1922 FWUPD_ERROR,
1923 FWUPD_ERROR_INTERNAL,
1924 "unspecified error");
1925 }
1926 g_propagate_prefixed_error (error, g_steal_pointer (&error_local),
1927 "failed to get_results using %s: ",
1928 priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001929 return FALSE;
1930 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001931 return TRUE;
1932}
1933
Richard Hughes08a37992017-09-12 12:57:43 +01001934/**
1935 * fu_plugin_get_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001936 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001937 *
1938 * Gets the plugin order, where higher numbers are run after lower
1939 * numbers.
1940 *
1941 * Returns: the integer value
1942 **/
1943guint
Richard Hughes12724852018-09-04 13:53:44 +01001944fu_plugin_get_order (FuPlugin *self)
Richard Hughes08a37992017-09-12 12:57:43 +01001945{
Richard Hughes12724852018-09-04 13:53:44 +01001946 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001947 return priv->order;
1948}
1949
1950/**
1951 * fu_plugin_set_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001952 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001953 * @order: a integer value
1954 *
1955 * Sets the plugin order, where higher numbers are run after lower
1956 * numbers.
1957 **/
1958void
Richard Hughes12724852018-09-04 13:53:44 +01001959fu_plugin_set_order (FuPlugin *self, guint order)
Richard Hughes08a37992017-09-12 12:57:43 +01001960{
Richard Hughes12724852018-09-04 13:53:44 +01001961 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001962 priv->order = order;
1963}
1964
1965/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001966 * fu_plugin_get_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001967 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001968 *
1969 * Gets the plugin priority, where higher numbers are better.
1970 *
1971 * Returns: the integer value
1972 **/
1973guint
Richard Hughes12724852018-09-04 13:53:44 +01001974fu_plugin_get_priority (FuPlugin *self)
Richard Hughes81c427c2018-08-06 15:20:17 +01001975{
Richard Hughes12724852018-09-04 13:53:44 +01001976 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001977 return priv->priority;
1978}
1979
1980/**
1981 * fu_plugin_set_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001982 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001983 * @priority: a integer value
1984 *
1985 * Sets the plugin priority, where higher numbers are better.
1986 **/
1987void
Richard Hughes12724852018-09-04 13:53:44 +01001988fu_plugin_set_priority (FuPlugin *self, guint priority)
Richard Hughes81c427c2018-08-06 15:20:17 +01001989{
Richard Hughes12724852018-09-04 13:53:44 +01001990 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001991 priv->priority = priority;
1992}
1993
1994/**
Richard Hughes08a37992017-09-12 12:57:43 +01001995 * fu_plugin_add_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001996 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001997 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001998 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001999 *
2000 * If the plugin name is found, the rule will be used to sort the plugin list,
2001 * for example the plugin specified by @name will be ordered after this plugin
2002 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
2003 *
2004 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
2005 * If depsolving fails then fwupd will not start.
2006 **/
2007void
Richard Hughes12724852018-09-04 13:53:44 +01002008fu_plugin_add_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes08a37992017-09-12 12:57:43 +01002009{
Richard Hughes12724852018-09-04 13:53:44 +01002010 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01002011 g_ptr_array_add (priv->rules[rule], g_strdup (name));
Richard Hughes75b965d2018-11-15 13:51:21 +00002012 g_signal_emit (self, signals[SIGNAL_RULES_CHANGED], 0);
Richard Hughes08a37992017-09-12 12:57:43 +01002013}
2014
2015/**
2016 * fu_plugin_get_rules:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002017 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01002018 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
2019 *
2020 * Gets the plugin IDs that should be run after this plugin.
2021 *
2022 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
2023 **/
2024GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +01002025fu_plugin_get_rules (FuPlugin *self, FuPluginRule rule)
Richard Hughes08a37992017-09-12 12:57:43 +01002026{
Richard Hughes12724852018-09-04 13:53:44 +01002027 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01002028 return priv->rules[rule];
2029}
2030
Richard Hughes80b79bb2018-01-11 21:11:06 +00002031/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002032 * fu_plugin_has_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002033 * @self: a #FuPlugin
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002034 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
2035 * @name: a plugin name, e.g. `upower`
2036 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01002037 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002038 *
2039 * Returns: %TRUE if the name exists for the specific rule
2040 **/
2041gboolean
Richard Hughes12724852018-09-04 13:53:44 +01002042fu_plugin_has_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002043{
Richard Hughes12724852018-09-04 13:53:44 +01002044 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes5f3a56b2018-06-28 12:13:59 +01002045 for (guint i = 0; i < priv->rules[rule]->len; i++) {
2046 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
2047 if (g_strcmp0 (tmp, name) == 0)
2048 return TRUE;
2049 }
2050 return FALSE;
2051}
2052
2053/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00002054 * fu_plugin_add_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002055 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00002056 * @key: a string, e.g. `FwupdateVersion`
2057 * @value: a string, e.g. `10`
2058 *
2059 * Sets any additional metadata to be included in the firmware report to aid
2060 * debugging problems.
2061 *
2062 * Any data included here will be sent to the metadata server after user
2063 * confirmation.
2064 **/
2065void
Richard Hughes12724852018-09-04 13:53:44 +01002066fu_plugin_add_report_metadata (FuPlugin *self, const gchar *key, const gchar *value)
Richard Hughes80b79bb2018-01-11 21:11:06 +00002067{
Richard Hughes12724852018-09-04 13:53:44 +01002068 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002069 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
2070}
2071
2072/**
2073 * fu_plugin_get_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002074 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00002075 *
2076 * Returns the list of additional metadata to be added when filing a report.
2077 *
2078 * Returns: (transfer none): the map of report metadata
2079 **/
2080GHashTable *
Richard Hughes12724852018-09-04 13:53:44 +01002081fu_plugin_get_report_metadata (FuPlugin *self)
Richard Hughes80b79bb2018-01-11 21:11:06 +00002082{
Richard Hughes12724852018-09-04 13:53:44 +01002083 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002084 return priv->report_metadata;
2085}
2086
Mario Limonciello963dc422018-02-27 14:26:58 -06002087/**
2088 * fu_plugin_get_config_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +01002089 * @self: a #FuPlugin
Mario Limonciello963dc422018-02-27 14:26:58 -06002090 * @key: A settings key
2091 *
2092 * Return the value of a key if it's been configured
2093 *
2094 * Since: 1.0.6
2095 **/
2096gchar *
Richard Hughes12724852018-09-04 13:53:44 +01002097fu_plugin_get_config_value (FuPlugin *self, const gchar *key)
Mario Limonciello963dc422018-02-27 14:26:58 -06002098{
Richard Hughes4be17d12018-05-30 20:36:29 +01002099 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06002100 g_autofree gchar *conf_file = NULL;
2101 g_autofree gchar *conf_path = NULL;
2102 g_autoptr(GKeyFile) keyfile = NULL;
2103 const gchar *plugin_name;
2104
Richard Hughes4be17d12018-05-30 20:36:29 +01002105 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Richard Hughes12724852018-09-04 13:53:44 +01002106 plugin_name = fu_plugin_get_name (self);
Mario Limonciello963dc422018-02-27 14:26:58 -06002107 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01002108 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06002109 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
2110 return NULL;
2111 keyfile = g_key_file_new ();
2112 if (!g_key_file_load_from_file (keyfile, conf_path,
2113 G_KEY_FILE_NONE, NULL))
2114 return NULL;
2115 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
2116}
2117
Richard Hughes8c71a3f2018-05-22 19:19:52 +01002118/**
2119 * fu_plugin_name_compare:
2120 * @plugin1: first #FuPlugin to compare.
2121 * @plugin2: second #FuPlugin to compare.
2122 *
2123 * Compares two plugins by their names.
2124 *
2125 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
2126 **/
2127gint
2128fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2129{
2130 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
2131 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
2132 return g_strcmp0 (priv1->name, priv2->name);
2133}
2134
2135/**
2136 * fu_plugin_order_compare:
2137 * @plugin1: first #FuPlugin to compare.
2138 * @plugin2: second #FuPlugin to compare.
2139 *
2140 * Compares two plugins by their depsolved order.
2141 *
2142 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
2143 **/
2144gint
2145fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
2146{
2147 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
2148 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
2149 if (priv1->order < priv2->order)
2150 return -1;
2151 if (priv1->order > priv2->order)
2152 return 1;
2153 return 0;
2154}
2155
Richard Hughescff38bc2016-12-12 12:03:37 +00002156static void
2157fu_plugin_class_init (FuPluginClass *klass)
2158{
2159 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2160 object_class->finalize = fu_plugin_finalize;
2161 signals[SIGNAL_DEVICE_ADDED] =
2162 g_signal_new ("device-added",
2163 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2164 G_STRUCT_OFFSET (FuPluginClass, device_added),
2165 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2166 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
2167 signals[SIGNAL_DEVICE_REMOVED] =
2168 g_signal_new ("device-removed",
2169 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2170 G_STRUCT_OFFSET (FuPluginClass, device_removed),
2171 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2172 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01002173 signals[SIGNAL_DEVICE_REGISTER] =
2174 g_signal_new ("device-register",
2175 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2176 G_STRUCT_OFFSET (FuPluginClass, device_register),
2177 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
2178 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00002179 signals[SIGNAL_RECOLDPLUG] =
2180 g_signal_new ("recoldplug",
2181 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2182 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
2183 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2184 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00002185 signals[SIGNAL_SET_COLDPLUG_DELAY] =
2186 g_signal_new ("set-coldplug-delay",
2187 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2188 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
2189 NULL, NULL, g_cclosure_marshal_VOID__UINT,
2190 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughesaabdc372018-11-14 10:11:08 +00002191 signals[SIGNAL_CHECK_SUPPORTED] =
2192 g_signal_new ("check-supported",
2193 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2194 G_STRUCT_OFFSET (FuPluginClass, check_supported),
2195 NULL, NULL, g_cclosure_marshal_generic,
2196 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
Richard Hughes75b965d2018-11-15 13:51:21 +00002197 signals[SIGNAL_RULES_CHANGED] =
2198 g_signal_new ("rules-changed",
2199 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
2200 G_STRUCT_OFFSET (FuPluginClass, rules_changed),
2201 NULL, NULL, g_cclosure_marshal_VOID__VOID,
2202 G_TYPE_NONE, 0);
Richard Hughescff38bc2016-12-12 12:03:37 +00002203}
2204
2205static void
Richard Hughes12724852018-09-04 13:53:44 +01002206fu_plugin_init (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +00002207{
Richard Hughes12724852018-09-04 13:53:44 +01002208 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002209 priv->enabled = TRUE;
Richard Hughesb1065422019-08-15 16:44:34 +01002210 priv->udev_subsystems = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00002211 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
2212 g_free, (GDestroyNotify) g_object_unref);
Richard Hughes161e9b52019-06-12 14:22:45 +01002213 g_rw_lock_init (&priv->devices_mutex);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002214 priv->report_metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
Richard Hughes08a37992017-09-12 12:57:43 +01002215 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
2216 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00002217}
2218
2219static void
2220fu_plugin_finalize (GObject *object)
2221{
Richard Hughes12724852018-09-04 13:53:44 +01002222 FuPlugin *self = FU_PLUGIN (object);
2223 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002224 FuPluginInitFunc func = NULL;
2225
2226 /* optional */
2227 if (priv->module != NULL) {
2228 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
2229 if (func != NULL) {
2230 g_debug ("performing destroy() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01002231 func (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00002232 }
2233 }
2234
Richard Hughes08a37992017-09-12 12:57:43 +01002235 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
2236 g_ptr_array_unref (priv->rules[i]);
2237
Richard Hughescff38bc2016-12-12 12:03:37 +00002238 if (priv->usb_ctx != NULL)
2239 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01002240 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01002241 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01002242 if (priv->quirks != NULL)
2243 g_object_unref (priv->quirks);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01002244 if (priv->udev_subsystems != NULL)
2245 g_ptr_array_unref (priv->udev_subsystems);
Richard Hughes49e5e052017-09-03 12:15:41 +01002246 if (priv->smbios != NULL)
2247 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01002248 if (priv->runtime_versions != NULL)
2249 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01002250 if (priv->compile_versions != NULL)
2251 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00002252 g_hash_table_unref (priv->devices);
Richard Hughes80b79bb2018-01-11 21:11:06 +00002253 g_hash_table_unref (priv->report_metadata);
Richard Hughes161e9b52019-06-12 14:22:45 +01002254 g_rw_lock_clear (&priv->devices_mutex);
Richard Hughes84999302019-05-02 10:18:32 +01002255 g_free (priv->build_hash);
Richard Hughescff38bc2016-12-12 12:03:37 +00002256 g_free (priv->name);
2257 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05002258 /* Must happen as the last step to avoid prematurely
2259 * freeing memory held by the plugin */
2260#ifndef RUNNING_ON_VALGRIND
2261 if (priv->module != NULL)
2262 g_module_close (priv->module);
2263#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00002264
2265 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
2266}
2267
2268FuPlugin *
2269fu_plugin_new (void)
2270{
Richard Hughes12724852018-09-04 13:53:44 +01002271 return FU_PLUGIN (g_object_new (FU_TYPE_PLUGIN, NULL));
Richard Hughescff38bc2016-12-12 12:03:37 +00002272}