Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 | * |
Richard Hughes | 2de8f13 | 2018-01-17 09:12:02 +0000 | [diff] [blame] | 3 | * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com> |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the GNU General Public License Version 2 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 24 | #include <fwupd.h> |
| 25 | #include <gmodule.h> |
| 26 | #include <appstream-glib.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | #include <gio/gunixinputstream.h> |
Mario Limonciello | 6d0aa3d | 2017-02-28 08:22:27 -0600 | [diff] [blame] | 30 | #ifdef HAVE_VALGRIND |
Richard Hughes | 576c012 | 2017-02-24 09:47:00 +0000 | [diff] [blame] | 31 | #include <valgrind.h> |
Mario Limonciello | 6d0aa3d | 2017-02-28 08:22:27 -0600 | [diff] [blame] | 32 | #endif /* HAVE_VALGRIND */ |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 33 | |
Richard Hughes | 9dde04f | 2017-09-13 12:07:15 +0100 | [diff] [blame] | 34 | #include "fu-device-private.h" |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 35 | #include "fu-plugin-private.h" |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 36 | #include "fu-history.h" |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 37 | |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 38 | /** |
| 39 | * SECTION:fu-plugin |
| 40 | * @short_description: a daemon plugin |
| 41 | * |
| 42 | * An object that represents a plugin run by the daemon. |
| 43 | * |
| 44 | * See also: #FuDevice |
| 45 | */ |
| 46 | |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 47 | #define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */ |
| 48 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 49 | static void fu_plugin_finalize (GObject *object); |
| 50 | |
| 51 | typedef struct { |
| 52 | GModule *module; |
| 53 | GUsbContext *usb_ctx; |
| 54 | gboolean enabled; |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 55 | guint order; |
| 56 | GPtrArray *rules[FU_PLUGIN_RULE_LAST]; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 57 | gchar *name; |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 58 | FuHwids *hwids; |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 59 | FuQuirks *quirks; |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 60 | GHashTable *runtime_versions; |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 61 | GHashTable *compile_versions; |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 62 | GPtrArray *supported_guids; |
| 63 | FuSmbios *smbios; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 64 | GHashTable *devices; /* platform_id:GObject */ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 65 | GHashTable *devices_delay; /* FuDevice:FuPluginHelper */ |
Richard Hughes | 80b79bb | 2018-01-11 21:11:06 +0000 | [diff] [blame] | 66 | GHashTable *report_metadata; /* key:value */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 67 | FuPluginData *data; |
| 68 | } FuPluginPrivate; |
| 69 | |
| 70 | enum { |
| 71 | SIGNAL_DEVICE_ADDED, |
| 72 | SIGNAL_DEVICE_REMOVED, |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 73 | SIGNAL_DEVICE_REGISTER, |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 74 | SIGNAL_RECOLDPLUG, |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 75 | SIGNAL_SET_COLDPLUG_DELAY, |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 76 | SIGNAL_LAST |
| 77 | }; |
| 78 | |
| 79 | static guint signals[SIGNAL_LAST] = { 0 }; |
| 80 | |
| 81 | G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT) |
| 82 | #define GET_PRIVATE(o) (fu_plugin_get_instance_private (o)) |
| 83 | |
| 84 | typedef const gchar *(*FuPluginGetNameFunc) (void); |
| 85 | typedef void (*FuPluginInitFunc) (FuPlugin *plugin); |
| 86 | typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin, |
| 87 | GError **error); |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 88 | typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin, |
| 89 | FuDevice *device); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 90 | typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin, |
| 91 | FuDevice *device, |
| 92 | GError **error); |
| 93 | typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin, |
| 94 | FuDevice *device, |
| 95 | FuPluginVerifyFlags flags, |
| 96 | GError **error); |
| 97 | typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin, |
| 98 | FuDevice *device, |
| 99 | GBytes *blob_fw, |
| 100 | FwupdInstallFlags flags, |
| 101 | GError **error); |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 102 | typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin, |
| 103 | GUsbDevice *usb_device, |
| 104 | GError **error); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 105 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 106 | /** |
| 107 | * fu_plugin_get_name: |
| 108 | * @plugin: A #FuPlugin |
| 109 | * |
| 110 | * Gets the plugin name. |
| 111 | * |
| 112 | * Returns: a plugin name, or %NULL for unknown. |
| 113 | * |
| 114 | * Since: 0.8.0 |
| 115 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 116 | const gchar * |
| 117 | fu_plugin_get_name (FuPlugin *plugin) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 118 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 119 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 120 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 121 | return priv->name; |
| 122 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 123 | |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 124 | void |
| 125 | fu_plugin_set_name (FuPlugin *plugin, const gchar *name) |
| 126 | { |
| 127 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 128 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 129 | g_return_if_fail (name != NULL); |
| 130 | g_free (priv->name); |
| 131 | priv->name = g_strdup (name); |
| 132 | } |
| 133 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 134 | /** |
| 135 | * fu_plugin_cache_lookup: |
| 136 | * @plugin: A #FuPlugin |
| 137 | * @id: the key |
| 138 | * |
| 139 | * Finds an object in the per-plugin cache. |
| 140 | * |
| 141 | * Returns: (transfer none): a #GObject, or %NULL for unfound. |
| 142 | * |
| 143 | * Since: 0.8.0 |
| 144 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 145 | gpointer |
| 146 | fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id) |
| 147 | { |
| 148 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 149 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
| 150 | g_return_val_if_fail (id != NULL, NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 151 | return g_hash_table_lookup (priv->devices, id); |
| 152 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 153 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 154 | /** |
| 155 | * fu_plugin_cache_add: |
| 156 | * @plugin: A #FuPlugin |
| 157 | * @id: the key |
| 158 | * @dev: a #GObject, typically a #FuDevice |
| 159 | * |
| 160 | * Adds an object to the per-plugin cache. |
| 161 | * |
| 162 | * Since: 0.8.0 |
| 163 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 164 | void |
| 165 | fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev) |
| 166 | { |
| 167 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 168 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 169 | g_return_if_fail (id != NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 170 | g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev)); |
| 171 | } |
| 172 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 173 | /** |
| 174 | * fu_plugin_cache_remove: |
| 175 | * @plugin: A #FuPlugin |
| 176 | * @id: the key |
| 177 | * |
| 178 | * Removes an object from the per-plugin cache. |
| 179 | * |
| 180 | * Since: 0.8.0 |
| 181 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 182 | void |
| 183 | fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id) |
| 184 | { |
| 185 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 186 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 187 | g_return_if_fail (id != NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 188 | g_hash_table_remove (priv->devices, id); |
| 189 | } |
| 190 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 191 | /** |
| 192 | * fu_plugin_get_data: |
| 193 | * @plugin: A #FuPlugin |
| 194 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 195 | * Gets the per-plugin allocated private data. This will return %NULL unless |
| 196 | * fu_plugin_alloc_data() has been called by the plugin. |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 197 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 198 | * Returns: (transfer none): a pointer to a structure, or %NULL for unset. |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 199 | * |
| 200 | * Since: 0.8.0 |
| 201 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 202 | FuPluginData * |
| 203 | fu_plugin_get_data (FuPlugin *plugin) |
| 204 | { |
| 205 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 206 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 207 | return priv->data; |
| 208 | } |
| 209 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 210 | /** |
| 211 | * fu_plugin_alloc_data: |
| 212 | * @plugin: A #FuPlugin |
| 213 | * @data_sz: the size to allocate |
| 214 | * |
| 215 | * Allocates the per-plugin allocated private data. |
| 216 | * |
| 217 | * Returns: (transfer full): a pointer to a structure, or %NULL for unset. |
| 218 | * |
| 219 | * Since: 0.8.0 |
| 220 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 221 | FuPluginData * |
| 222 | fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz) |
| 223 | { |
| 224 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 225 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | 44dee88 | 2017-01-11 08:31:10 +0000 | [diff] [blame] | 226 | if (priv->data != NULL) { |
| 227 | g_critical ("fu_plugin_alloc_data() already used by plugin"); |
| 228 | return priv->data; |
| 229 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 230 | priv->data = g_malloc0 (data_sz); |
| 231 | return priv->data; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 234 | /** |
| 235 | * fu_plugin_get_usb_context: |
| 236 | * @plugin: A #FuPlugin |
| 237 | * |
| 238 | * Gets the shared USB context that all plugins can use. |
| 239 | * |
| 240 | * Returns: (transfer none): a #GUsbContext. |
| 241 | * |
| 242 | * Since: 0.8.0 |
| 243 | **/ |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 244 | GUsbContext * |
| 245 | fu_plugin_get_usb_context (FuPlugin *plugin) |
| 246 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 247 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 248 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 249 | return priv->usb_ctx; |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void |
| 253 | fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx) |
| 254 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 255 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 256 | g_set_object (&priv->usb_ctx, usb_ctx); |
| 257 | } |
| 258 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 259 | /** |
| 260 | * fu_plugin_get_enabled: |
| 261 | * @plugin: A #FuPlugin |
| 262 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 263 | * Returns if the plugin is enabled. Plugins may self-disable using |
| 264 | * fu_plugin_set_enabled() or can be disabled by the daemon. |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 265 | * |
| 266 | * Returns: %TRUE if the plugin is currently enabled. |
| 267 | * |
| 268 | * Since: 0.8.0 |
| 269 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 270 | gboolean |
| 271 | fu_plugin_get_enabled (FuPlugin *plugin) |
| 272 | { |
| 273 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 274 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 275 | return priv->enabled; |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 278 | /** |
| 279 | * fu_plugin_set_enabled: |
| 280 | * @plugin: A #FuPlugin |
| 281 | * @enabled: the enabled value |
| 282 | * |
| 283 | * Enables or disables a plugin. Plugins can self-disable at any point. |
| 284 | * |
| 285 | * Since: 0.8.0 |
| 286 | **/ |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 287 | void |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 288 | fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 289 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 290 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 291 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 292 | priv->enabled = enabled; |
| 293 | } |
| 294 | |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 295 | gchar * |
| 296 | fu_plugin_guess_name_from_fn (const gchar *filename) |
| 297 | { |
| 298 | const gchar *prefix = "libfu_plugin_"; |
| 299 | gchar *name; |
| 300 | gchar *str = g_strstr_len (filename, -1, prefix); |
| 301 | if (str == NULL) |
| 302 | return NULL; |
| 303 | name = g_strdup (str + strlen (prefix)); |
| 304 | g_strdelimit (name, ".", '\0'); |
| 305 | return name; |
| 306 | } |
| 307 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 308 | gboolean |
| 309 | fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error) |
| 310 | { |
| 311 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 312 | FuPluginInitFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 313 | |
| 314 | priv->module = g_module_open (filename, 0); |
| 315 | if (priv->module == NULL) { |
| 316 | g_set_error (error, |
| 317 | G_IO_ERROR, |
| 318 | G_IO_ERROR_FAILED, |
| 319 | "failed to open plugin: %s", |
| 320 | g_module_error ()); |
| 321 | return FALSE; |
| 322 | } |
| 323 | |
| 324 | /* set automatically */ |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 325 | if (priv->name == NULL) |
| 326 | priv->name = fu_plugin_guess_name_from_fn (filename); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 327 | |
| 328 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 329 | g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func); |
| 330 | if (func != NULL) { |
| 331 | g_debug ("performing init() on %s", filename); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 332 | func (plugin); |
| 333 | } |
| 334 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 335 | return TRUE; |
| 336 | } |
| 337 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 338 | /** |
| 339 | * fu_plugin_device_add: |
| 340 | * @plugin: A #FuPlugin |
| 341 | * @device: A #FuDevice |
| 342 | * |
| 343 | * Asks the daemon to add a device to the exported list. If this device ID |
| 344 | * has already been added by a different plugin then this request will be |
| 345 | * ignored. |
| 346 | * |
| 347 | * Plugins should use fu_plugin_device_add_delay() if they are not capable of |
| 348 | * actually flashing an image to the hardware so that higher-priority plugins |
| 349 | * can add the device themselves. |
| 350 | * |
| 351 | * Since: 0.8.0 |
| 352 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 353 | void |
| 354 | fu_plugin_device_add (FuPlugin *plugin, FuDevice *device) |
| 355 | { |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 356 | GPtrArray *children; |
| 357 | |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 358 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 359 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 360 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 361 | g_debug ("emit added from %s: %s", |
| 362 | fu_plugin_get_name (plugin), |
| 363 | fu_device_get_id (device)); |
| 364 | fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
| 365 | fu_device_set_plugin (device, fu_plugin_get_name (plugin)); |
| 366 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device); |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 367 | |
| 368 | /* add children */ |
| 369 | children = fu_device_get_children (device); |
| 370 | for (guint i = 0; i < children->len; i++) { |
| 371 | FuDevice *child = g_ptr_array_index (children, i); |
| 372 | fu_plugin_device_add (plugin, child); |
| 373 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 376 | /** |
| 377 | * fu_plugin_device_register: |
| 378 | * @plugin: A #FuPlugin |
| 379 | * @device: A #FuDevice |
| 380 | * |
| 381 | * Registers the device with other plugins so they can set metadata. |
| 382 | * |
| 383 | * Plugins do not have to call this manually as this is done automatically |
| 384 | * when using fu_plugin_device_add(). They may wish to use this manually |
| 385 | * if for intance the coldplug should be ignored based on the metadata |
| 386 | * set from other plugins. |
| 387 | * |
| 388 | * Since: 0.9.7 |
| 389 | **/ |
| 390 | void |
| 391 | fu_plugin_device_register (FuPlugin *plugin, FuDevice *device) |
| 392 | { |
| 393 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 394 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 395 | |
| 396 | g_debug ("emit device-register from %s: %s", |
| 397 | fu_plugin_get_name (plugin), |
| 398 | fu_device_get_id (device)); |
| 399 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device); |
| 400 | } |
| 401 | |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 402 | typedef struct { |
| 403 | FuPlugin *plugin; |
| 404 | FuDevice *device; |
| 405 | guint timeout_id; |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 406 | GHashTable *devices; |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 407 | } FuPluginHelper; |
| 408 | |
| 409 | static void |
| 410 | fu_plugin_helper_free (FuPluginHelper *helper) |
| 411 | { |
| 412 | g_object_unref (helper->plugin); |
| 413 | g_object_unref (helper->device); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 414 | g_hash_table_unref (helper->devices); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 415 | g_free (helper); |
| 416 | } |
| 417 | |
| 418 | static gboolean |
| 419 | fu_plugin_device_add_delay_cb (gpointer user_data) |
| 420 | { |
| 421 | FuPluginHelper *helper = (FuPluginHelper *) user_data; |
| 422 | fu_plugin_device_add (helper->plugin, helper->device); |
Richard Hughes | 0dec274 | 2017-09-18 11:07:39 +0100 | [diff] [blame] | 423 | g_hash_table_remove (helper->devices, helper->device); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 424 | fu_plugin_helper_free (helper); |
| 425 | return FALSE; |
| 426 | } |
| 427 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 428 | /** |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 429 | * fu_plugin_has_device_delay: |
| 430 | * @plugin: A #FuPlugin |
| 431 | * |
| 432 | * Returns if the device has a pending device that is waiting to be added. |
| 433 | * |
| 434 | * Returns: %TRUE if a device is waiting to be added |
| 435 | * |
| 436 | * Since: 0.8.0 |
| 437 | **/ |
| 438 | gboolean |
| 439 | fu_plugin_has_device_delay (FuPlugin *plugin) |
| 440 | { |
| 441 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 442 | return g_hash_table_size (priv->devices_delay) > 0; |
| 443 | } |
| 444 | |
| 445 | /** |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 446 | * fu_plugin_device_add_delay: |
| 447 | * @plugin: A #FuPlugin |
| 448 | * @device: A #FuDevice |
| 449 | * |
| 450 | * Asks the daemon to add a device to the exported list after a small delay. |
| 451 | * |
| 452 | * Since: 0.8.0 |
| 453 | **/ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 454 | void |
| 455 | fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device) |
| 456 | { |
| 457 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 458 | FuPluginHelper *helper; |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 459 | |
| 460 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 461 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 462 | |
Richard Hughes | 6ad951d | 2017-02-03 10:12:12 +0000 | [diff] [blame] | 463 | /* already waiting for add */ |
| 464 | helper = g_hash_table_lookup (priv->devices_delay, device); |
| 465 | if (helper != NULL) { |
Richard Hughes | 3cca1c6 | 2017-07-21 13:38:20 +0100 | [diff] [blame] | 466 | g_debug ("ignoring add-delay as device %s already pending", |
| 467 | fu_device_get_id (device)); |
Richard Hughes | 6ad951d | 2017-02-03 10:12:12 +0000 | [diff] [blame] | 468 | return; |
| 469 | } |
| 470 | |
| 471 | /* add after a small delay */ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 472 | g_debug ("waiting a small time for other plugins"); |
| 473 | helper = g_new0 (FuPluginHelper, 1); |
| 474 | helper->plugin = g_object_ref (plugin); |
| 475 | helper->device = g_object_ref (device); |
| 476 | helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 477 | helper->devices = g_hash_table_ref (priv->devices_delay); |
| 478 | g_hash_table_insert (helper->devices, device, helper); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 481 | /** |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 482 | * fu_plugin_device_remove: |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 483 | * @plugin: A #FuPlugin |
| 484 | * @device: A #FuDevice |
| 485 | * |
| 486 | * Asks the daemon to remove a device from the exported list. |
| 487 | * |
| 488 | * Since: 0.8.0 |
| 489 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 490 | void |
| 491 | fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device) |
| 492 | { |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 493 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 494 | FuPluginHelper *helper; |
| 495 | |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 496 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 497 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 498 | |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 499 | /* waiting for add */ |
| 500 | helper = g_hash_table_lookup (priv->devices_delay, device); |
| 501 | if (helper != NULL) { |
| 502 | g_debug ("ignoring remove from delayed addition"); |
| 503 | g_source_remove (helper->timeout_id); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 504 | g_hash_table_remove (priv->devices_delay, helper->device); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 505 | fu_plugin_helper_free (helper); |
| 506 | return; |
| 507 | } |
| 508 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 509 | g_debug ("emit removed from %s: %s", |
| 510 | fu_plugin_get_name (plugin), |
| 511 | fu_device_get_id (device)); |
| 512 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device); |
| 513 | } |
| 514 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 515 | /** |
Richard Hughes | 2de8f13 | 2018-01-17 09:12:02 +0000 | [diff] [blame] | 516 | * fu_plugin_request_recoldplug: |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 517 | * @plugin: A #FuPlugin |
| 518 | * |
| 519 | * Ask all the plugins to coldplug all devices, which will include the prepare() |
| 520 | * and cleanup() phases. Duplicate devices added will be ignored. |
| 521 | * |
| 522 | * Since: 0.8.0 |
| 523 | **/ |
| 524 | void |
Richard Hughes | 2de8f13 | 2018-01-17 09:12:02 +0000 | [diff] [blame] | 525 | fu_plugin_request_recoldplug (FuPlugin *plugin) |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 526 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 527 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 528 | g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0); |
| 529 | } |
| 530 | |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 531 | /** |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 532 | * fu_plugin_check_hwid: |
| 533 | * @plugin: A #FuPlugin |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 534 | * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234` |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 535 | * |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 536 | * Checks to see if a specific GUID exists. All hardware IDs on a |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 537 | * specific system can be shown using the `fwupdmgr hwids` command. |
| 538 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 539 | * Returns: %TRUE if the HwId is found on the system. |
| 540 | * |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 541 | * Since: 0.9.1 |
| 542 | **/ |
| 543 | gboolean |
| 544 | fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid) |
| 545 | { |
| 546 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 547 | if (priv->hwids == NULL) |
| 548 | return FALSE; |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 549 | return fu_hwids_has_guid (priv->hwids, hwid); |
| 550 | } |
| 551 | |
| 552 | /** |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 553 | * fu_plugin_check_supported: |
| 554 | * @plugin: A #FuPlugin |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 555 | * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234` |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 556 | * |
| 557 | * Checks to see if a specific device GUID is supported, i.e. available in the |
| 558 | * AppStream metadata. |
| 559 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 560 | * Returns: %TRUE if the device is supported. |
| 561 | * |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 562 | * Since: 1.0.0 |
| 563 | **/ |
| 564 | gboolean |
| 565 | fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid) |
| 566 | { |
| 567 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 568 | if (priv->supported_guids == NULL) |
| 569 | return FALSE; |
| 570 | for (guint i = 0; i < priv->supported_guids->len; i++) { |
| 571 | const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i); |
| 572 | if (g_strcmp0 (guid, guid_tmp) == 0) |
| 573 | return TRUE; |
| 574 | } |
| 575 | return FALSE; |
| 576 | } |
| 577 | |
| 578 | /** |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 579 | * fu_plugin_get_dmi_value: |
| 580 | * @plugin: A #FuPlugin |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 581 | * @dmi_id: A DMI ID, e.g. `BiosVersion` |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 582 | * |
| 583 | * Gets a hardware DMI value. |
| 584 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 585 | * Returns: The string, or %NULL |
| 586 | * |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 587 | * Since: 0.9.7 |
| 588 | **/ |
| 589 | const gchar * |
| 590 | fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id) |
| 591 | { |
| 592 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 593 | if (priv->hwids == NULL) |
Richard Hughes | 7ef96b8 | 2017-08-23 18:28:24 +0100 | [diff] [blame] | 594 | return NULL; |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 595 | return fu_hwids_get_value (priv->hwids, dmi_id); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 598 | /** |
| 599 | * fu_plugin_get_smbios_string: |
| 600 | * @plugin: A #FuPlugin |
| 601 | * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS |
| 602 | * @offset: A SMBIOS offset |
| 603 | * |
| 604 | * Gets a hardware SMBIOS string. |
| 605 | * |
| 606 | * The @type and @offset can be referenced from the DMTF SMBIOS specification: |
| 607 | * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf |
| 608 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 609 | * Returns: A string, or %NULL |
| 610 | * |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 611 | * Since: 0.9.8 |
| 612 | **/ |
| 613 | const gchar * |
| 614 | fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset) |
| 615 | { |
| 616 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 617 | if (priv->smbios == NULL) |
| 618 | return NULL; |
| 619 | return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * fu_plugin_get_smbios_data: |
| 624 | * @plugin: A #FuPlugin |
| 625 | * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS |
| 626 | * |
| 627 | * Gets a hardware SMBIOS data. |
| 628 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 629 | * Returns: (transfer none): A #GBytes, or %NULL |
| 630 | * |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 631 | * Since: 0.9.8 |
| 632 | **/ |
| 633 | GBytes * |
| 634 | fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type) |
| 635 | { |
| 636 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 637 | if (priv->smbios == NULL) |
| 638 | return NULL; |
| 639 | return fu_smbios_get_data (priv->smbios, structure_type, NULL); |
| 640 | } |
| 641 | |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 642 | void |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 643 | fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids) |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 644 | { |
| 645 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 646 | g_set_object (&priv->hwids, hwids); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 649 | void |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 650 | fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids) |
| 651 | { |
| 652 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 653 | if (priv->supported_guids != NULL) |
| 654 | g_ptr_array_unref (priv->supported_guids); |
| 655 | priv->supported_guids = g_ptr_array_ref (supported_guids); |
| 656 | } |
| 657 | |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 658 | void |
| 659 | fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks) |
| 660 | { |
| 661 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 662 | g_set_object (&priv->quirks, quirks); |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * fu_plugin_get_quirks: |
| 667 | * @plugin: A #FuPlugin |
| 668 | * |
| 669 | * Returns the hardware database object. This can be used to discover device |
| 670 | * quirks or other device-specific settings. |
| 671 | * |
| 672 | * Returns: (transfer none): a #FuQuirks, or %NULL if not set |
| 673 | * |
| 674 | * Since: 1.0.1 |
| 675 | **/ |
| 676 | FuQuirks * |
| 677 | fu_plugin_get_quirks (FuPlugin *plugin) |
| 678 | { |
| 679 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 680 | return priv->quirks; |
| 681 | } |
| 682 | |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 683 | void |
| 684 | fu_plugin_set_runtime_versions (FuPlugin *plugin, GHashTable *runtime_versions) |
| 685 | { |
| 686 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 687 | priv->runtime_versions = g_hash_table_ref (runtime_versions); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * fu_plugin_add_runtime_version: |
| 692 | * @plugin: A #FuPlugin |
| 693 | * @component_id: An AppStream component id, e.g. "org.gnome.Software" |
| 694 | * @version: A version string, e.g. "1.2.3" |
| 695 | * |
| 696 | * Sets a runtime version of a specific dependancy. |
| 697 | * |
| 698 | * Since: 1.0.7 |
| 699 | **/ |
| 700 | void |
| 701 | fu_plugin_add_runtime_version (FuPlugin *plugin, |
| 702 | const gchar *component_id, |
| 703 | const gchar *version) |
| 704 | { |
| 705 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | b01b486 | 2018-04-20 16:39:48 +0100 | [diff] [blame] | 706 | if (priv->runtime_versions == NULL) |
| 707 | return; |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 708 | g_hash_table_insert (priv->runtime_versions, |
| 709 | g_strdup (component_id), |
| 710 | g_strdup (version)); |
| 711 | } |
| 712 | |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 713 | void |
| 714 | fu_plugin_set_compile_versions (FuPlugin *plugin, GHashTable *compile_versions) |
| 715 | { |
| 716 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 717 | priv->compile_versions = g_hash_table_ref (compile_versions); |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * fu_plugin_add_compile_version: |
| 722 | * @plugin: A #FuPlugin |
| 723 | * @component_id: An AppStream component id, e.g. "org.gnome.Software" |
| 724 | * @version: A version string, e.g. "1.2.3" |
| 725 | * |
| 726 | * Sets a compile-time version of a specific dependancy. |
| 727 | * |
| 728 | * Since: 1.0.7 |
| 729 | **/ |
| 730 | void |
| 731 | fu_plugin_add_compile_version (FuPlugin *plugin, |
| 732 | const gchar *component_id, |
| 733 | const gchar *version) |
| 734 | { |
| 735 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 736 | if (priv->compile_versions == NULL) |
| 737 | return; |
| 738 | g_hash_table_insert (priv->compile_versions, |
| 739 | g_strdup (component_id), |
| 740 | g_strdup (version)); |
| 741 | } |
| 742 | |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 743 | /** |
| 744 | * fu_plugin_lookup_quirk_by_id: |
| 745 | * @plugin: A #FuPlugin |
| 746 | * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks" |
| 747 | * @id: An ID to match the entry, e.g. "012345" |
| 748 | * |
| 749 | * Looks up an entry in the hardware database using a string value. |
| 750 | * |
| 751 | * Returns: (transfer none): values from the database, or %NULL if not found |
| 752 | * |
| 753 | * Since: 1.0.1 |
| 754 | **/ |
| 755 | const gchar * |
| 756 | fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *prefix, const gchar *id) |
| 757 | { |
| 758 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 759 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
| 760 | |
| 761 | /* wildcard */ |
| 762 | if (g_strstr_len (id, -1, "*") != NULL) |
| 763 | return fu_quirks_lookup_by_glob (priv->quirks, prefix, id); |
| 764 | |
| 765 | /* exact ID */ |
| 766 | return fu_quirks_lookup_by_id (priv->quirks, prefix, id); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * fu_plugin_lookup_quirk_by_usb_device: |
| 771 | * @plugin: A #FuPlugin |
| 772 | * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks" |
| 773 | * @dev: A #GUsbDevice |
| 774 | * |
| 775 | * Looks up an entry in the hardware database using various keys generated |
| 776 | * from @dev. |
| 777 | * |
| 778 | * Returns: (transfer none): values from the database, or %NULL if not found |
| 779 | * |
| 780 | * Since: 1.0.1 |
| 781 | **/ |
| 782 | const gchar * |
| 783 | fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin, const gchar *prefix, GUsbDevice *dev) |
| 784 | { |
| 785 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 786 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
| 787 | return fu_quirks_lookup_by_usb_device (priv->quirks, prefix, dev); |
| 788 | } |
| 789 | |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 790 | /** |
| 791 | * fu_plugin_get_supported: |
| 792 | * @plugin: A #FuPlugin |
| 793 | * |
| 794 | * Gets all the device GUIDs supported by the daemon. |
| 795 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 796 | * Returns: (element-type utf8) (transfer none): GUIDs |
| 797 | * |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 798 | * Since: 1.0.0 |
| 799 | **/ |
| 800 | GPtrArray * |
| 801 | fu_plugin_get_supported (FuPlugin *plugin) |
| 802 | { |
| 803 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 804 | return priv->supported_guids; |
| 805 | } |
| 806 | |
| 807 | void |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 808 | fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios) |
| 809 | { |
| 810 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 811 | g_set_object (&priv->smbios, smbios); |
| 812 | } |
| 813 | |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 814 | /** |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 815 | * fu_plugin_set_coldplug_delay: |
| 816 | * @plugin: A #FuPlugin |
| 817 | * @duration: A delay in milliseconds |
| 818 | * |
| 819 | * Set the minimum time that should be waited inbetween the call to |
| 820 | * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going |
| 821 | * to be the minimum hardware initialisation time from a datasheet. |
| 822 | * |
| 823 | * It is better to use this function rather than using a sleep() in the plugin |
| 824 | * itself as then only one delay is done in the daemon rather than waiting for |
| 825 | * each coldplug prepare in a serial way. |
| 826 | * |
| 827 | * Additionally, very long delays should be avoided as the daemon will be |
| 828 | * blocked from processing requests whilst the coldplug delay is being |
| 829 | * performed. |
| 830 | * |
| 831 | * Since: 0.8.0 |
| 832 | **/ |
| 833 | void |
| 834 | fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration) |
| 835 | { |
| 836 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 837 | g_return_if_fail (duration > 0); |
| 838 | |
| 839 | /* check sanity */ |
| 840 | if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) { |
| 841 | g_warning ("duration of %ums is crazy, truncating to %ums", |
| 842 | duration, |
| 843 | FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM); |
| 844 | duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM; |
| 845 | } |
| 846 | |
| 847 | /* emit */ |
| 848 | g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration); |
| 849 | } |
| 850 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 851 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 852 | fu_plugin_runner_startup (FuPlugin *plugin, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 853 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 854 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 855 | FuPluginStartupFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 856 | |
| 857 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 858 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 859 | return TRUE; |
| 860 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 861 | /* no object loaded */ |
| 862 | if (priv->module == NULL) |
| 863 | return TRUE; |
| 864 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 865 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 866 | g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func); |
| 867 | if (func == NULL) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 868 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 869 | g_debug ("performing startup() on %s", priv->name); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 870 | if (!func (plugin, error)) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 871 | g_prefix_error (error, "failed to startup %s: ", priv->name); |
| 872 | return FALSE; |
| 873 | } |
| 874 | return TRUE; |
| 875 | } |
| 876 | |
| 877 | static gboolean |
| 878 | fu_plugin_runner_offline_invalidate (GError **error) |
| 879 | { |
| 880 | g_autoptr(GError) error_local = NULL; |
| 881 | g_autoptr(GFile) file1 = NULL; |
| 882 | |
| 883 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 884 | |
| 885 | file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME); |
| 886 | if (!g_file_query_exists (file1, NULL)) |
| 887 | return TRUE; |
| 888 | if (!g_file_delete (file1, NULL, &error_local)) { |
| 889 | g_set_error (error, |
| 890 | FWUPD_ERROR, |
| 891 | FWUPD_ERROR_INTERNAL, |
| 892 | "Cannot delete %s: %s", |
| 893 | FU_OFFLINE_TRIGGER_FILENAME, |
| 894 | error_local->message); |
| 895 | return FALSE; |
| 896 | } |
| 897 | return TRUE; |
| 898 | } |
| 899 | |
| 900 | static gboolean |
| 901 | fu_plugin_runner_offline_setup (GError **error) |
| 902 | { |
| 903 | gint rc; |
| 904 | |
| 905 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 906 | |
| 907 | /* create symlink for the systemd-system-update-generator */ |
| 908 | rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME); |
| 909 | if (rc < 0) { |
| 910 | g_set_error (error, |
| 911 | FWUPD_ERROR, |
| 912 | FWUPD_ERROR_INTERNAL, |
| 913 | "Failed to create symlink %s to %s: %s", |
| 914 | FU_OFFLINE_TRIGGER_FILENAME, |
| 915 | "/var/lib", strerror (errno)); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 916 | return FALSE; |
| 917 | } |
| 918 | return TRUE; |
| 919 | } |
| 920 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 921 | static gboolean |
| 922 | fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device, |
| 923 | const gchar *symbol_name, GError **error) |
| 924 | { |
| 925 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 926 | FuPluginDeviceFunc func = NULL; |
| 927 | |
| 928 | /* not enabled */ |
| 929 | if (!priv->enabled) |
| 930 | return TRUE; |
| 931 | |
Richard Hughes | d3d96cc | 2017-11-14 11:34:33 +0000 | [diff] [blame] | 932 | /* no object loaded */ |
| 933 | if (priv->module == NULL) |
| 934 | return TRUE; |
| 935 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 936 | /* optional */ |
| 937 | g_module_symbol (priv->module, symbol_name, (gpointer *) &func); |
| 938 | if (func == NULL) |
| 939 | return TRUE; |
| 940 | g_debug ("performing %s() on %s", symbol_name + 10, priv->name); |
| 941 | if (!func (plugin, device, error)) { |
Richard Hughes | 83e54e4 | 2018-01-31 23:27:30 +0000 | [diff] [blame] | 942 | g_prefix_error (error, "failed to run %s() on %s: ", |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 943 | symbol_name + 10, |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 944 | priv->name); |
| 945 | return FALSE; |
| 946 | } |
| 947 | return TRUE; |
| 948 | } |
| 949 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 950 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 951 | fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 952 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 953 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 954 | FuPluginStartupFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 955 | |
| 956 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 957 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 958 | return TRUE; |
| 959 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 960 | /* no object loaded */ |
| 961 | if (priv->module == NULL) |
| 962 | return TRUE; |
| 963 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 964 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 965 | g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func); |
| 966 | if (func == NULL) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 967 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 968 | g_debug ("performing coldplug() on %s", priv->name); |
| 969 | if (!func (plugin, error)) { |
| 970 | g_prefix_error (error, "failed to coldplug %s: ", priv->name); |
| 971 | return FALSE; |
| 972 | } |
| 973 | return TRUE; |
| 974 | } |
| 975 | |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 976 | gboolean |
Richard Hughes | 2de8f13 | 2018-01-17 09:12:02 +0000 | [diff] [blame] | 977 | fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error) |
| 978 | { |
| 979 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 980 | FuPluginStartupFunc func = NULL; |
| 981 | |
| 982 | /* not enabled */ |
| 983 | if (!priv->enabled) |
| 984 | return TRUE; |
| 985 | |
| 986 | /* no object loaded */ |
| 987 | if (priv->module == NULL) |
| 988 | return TRUE; |
| 989 | |
| 990 | /* optional */ |
| 991 | g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func); |
| 992 | if (func == NULL) |
| 993 | return TRUE; |
| 994 | g_debug ("performing recoldplug() on %s", priv->name); |
| 995 | if (!func (plugin, error)) { |
| 996 | g_prefix_error (error, "failed to recoldplug %s: ", priv->name); |
| 997 | return FALSE; |
| 998 | } |
| 999 | return TRUE; |
| 1000 | } |
| 1001 | |
| 1002 | gboolean |
Richard Hughes | 46487c9 | 2017-01-07 21:26:34 +0000 | [diff] [blame] | 1003 | fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error) |
| 1004 | { |
| 1005 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1006 | FuPluginStartupFunc func = NULL; |
| 1007 | |
| 1008 | /* not enabled */ |
| 1009 | if (!priv->enabled) |
| 1010 | return TRUE; |
| 1011 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1012 | /* no object loaded */ |
| 1013 | if (priv->module == NULL) |
| 1014 | return TRUE; |
| 1015 | |
Richard Hughes | 46487c9 | 2017-01-07 21:26:34 +0000 | [diff] [blame] | 1016 | /* optional */ |
| 1017 | g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func); |
| 1018 | if (func == NULL) |
| 1019 | return TRUE; |
| 1020 | g_debug ("performing coldplug_prepare() on %s", priv->name); |
| 1021 | if (!func (plugin, error)) { |
| 1022 | g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name); |
| 1023 | return FALSE; |
| 1024 | } |
| 1025 | return TRUE; |
| 1026 | } |
| 1027 | |
| 1028 | gboolean |
| 1029 | fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error) |
| 1030 | { |
| 1031 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1032 | FuPluginStartupFunc func = NULL; |
| 1033 | |
| 1034 | /* not enabled */ |
| 1035 | if (!priv->enabled) |
| 1036 | return TRUE; |
| 1037 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1038 | /* no object loaded */ |
| 1039 | if (priv->module == NULL) |
| 1040 | return TRUE; |
| 1041 | |
Richard Hughes | 46487c9 | 2017-01-07 21:26:34 +0000 | [diff] [blame] | 1042 | /* optional */ |
| 1043 | g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func); |
| 1044 | if (func == NULL) |
| 1045 | return TRUE; |
| 1046 | g_debug ("performing coldplug_cleanup() on %s", priv->name); |
| 1047 | if (!func (plugin, error)) { |
| 1048 | g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name); |
| 1049 | return FALSE; |
| 1050 | } |
| 1051 | return TRUE; |
| 1052 | } |
| 1053 | |
| 1054 | gboolean |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1055 | fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1056 | { |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 1057 | return fu_plugin_runner_device_generic (plugin, device, |
| 1058 | "fu_plugin_update_prepare", error); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | gboolean |
| 1062 | fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1063 | { |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 1064 | return fu_plugin_runner_device_generic (plugin, device, |
| 1065 | "fu_plugin_update_cleanup", error); |
| 1066 | } |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1067 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 1068 | gboolean |
| 1069 | fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1070 | { |
| 1071 | return fu_plugin_runner_device_generic (plugin, device, |
| 1072 | "fu_plugin_update_attach", error); |
| 1073 | } |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1074 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 1075 | gboolean |
| 1076 | fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1077 | { |
| 1078 | return fu_plugin_runner_device_generic (plugin, device, |
| 1079 | "fu_plugin_update_detach", error); |
| 1080 | } |
| 1081 | |
| 1082 | gboolean |
| 1083 | fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1084 | { |
| 1085 | return fu_plugin_runner_device_generic (plugin, device, |
| 1086 | "fu_plugin_update_reload", error); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 1089 | gboolean |
| 1090 | fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error) |
| 1091 | { |
| 1092 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1093 | FuPluginUsbDeviceAddedFunc func = NULL; |
| 1094 | |
| 1095 | /* not enabled */ |
| 1096 | if (!priv->enabled) |
| 1097 | return TRUE; |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1098 | |
| 1099 | /* no object loaded */ |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 1100 | if (priv->module == NULL) |
| 1101 | return TRUE; |
| 1102 | |
| 1103 | /* optional */ |
| 1104 | g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func); |
| 1105 | if (func != NULL) { |
| 1106 | g_debug ("performing usb_device_added() on %s", priv->name); |
| 1107 | return func (plugin, usb_device, error); |
| 1108 | } |
| 1109 | return TRUE; |
| 1110 | } |
| 1111 | |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 1112 | void |
| 1113 | fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device) |
| 1114 | { |
| 1115 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1116 | FuPluginDeviceRegisterFunc func = NULL; |
| 1117 | |
| 1118 | /* not enabled */ |
| 1119 | if (!priv->enabled) |
| 1120 | return; |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 1121 | if (priv->module == NULL) |
| 1122 | return; |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 1123 | |
| 1124 | /* optional */ |
| 1125 | g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func); |
| 1126 | if (func != NULL) { |
| 1127 | g_debug ("performing device_added() on %s", priv->name); |
| 1128 | func (plugin, device); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1132 | static gboolean |
| 1133 | fu_plugin_runner_schedule_update (FuPlugin *plugin, |
| 1134 | FuDevice *device, |
| 1135 | GBytes *blob_cab, |
| 1136 | GError **error) |
| 1137 | { |
Richard Hughes | 38fb56c | 2018-02-01 22:07:52 +0000 | [diff] [blame] | 1138 | FwupdRelease *release; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1139 | gchar tmpname[] = {"XXXXXX.cap"}; |
| 1140 | g_autofree gchar *dirname = NULL; |
| 1141 | g_autofree gchar *filename = NULL; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1142 | g_autoptr(FuDevice) res_tmp = NULL; |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1143 | g_autoptr(FuHistory) history = NULL; |
Richard Hughes | 38fb56c | 2018-02-01 22:07:52 +0000 | [diff] [blame] | 1144 | g_autoptr(FwupdRelease) release_tmp = fwupd_release_new (); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1145 | g_autoptr(GFile) file = NULL; |
| 1146 | |
| 1147 | /* id already exists */ |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1148 | history = fu_history_new (); |
Richard Hughes | 0b9d996 | 2018-01-12 16:31:28 +0000 | [diff] [blame] | 1149 | res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1150 | if (res_tmp != NULL) { |
| 1151 | g_set_error (error, |
| 1152 | FWUPD_ERROR, |
| 1153 | FWUPD_ERROR_ALREADY_PENDING, |
| 1154 | "%s is already scheduled to be updated", |
| 1155 | fu_device_get_id (device)); |
| 1156 | return FALSE; |
| 1157 | } |
| 1158 | |
| 1159 | /* create directory */ |
| 1160 | dirname = g_build_filename (LOCALSTATEDIR, "lib", "fwupd", NULL); |
| 1161 | file = g_file_new_for_path (dirname); |
| 1162 | if (!g_file_query_exists (file, NULL)) { |
| 1163 | if (!g_file_make_directory_with_parents (file, NULL, error)) |
| 1164 | return FALSE; |
| 1165 | } |
| 1166 | |
| 1167 | /* get a random filename */ |
| 1168 | for (guint i = 0; i < 6; i++) |
| 1169 | tmpname[i] = (gchar) g_random_int_range ('A', 'Z'); |
| 1170 | filename = g_build_filename (dirname, tmpname, NULL); |
| 1171 | |
| 1172 | /* just copy to the temp file */ |
Richard Hughes | 23135eb | 2017-11-30 21:01:25 +0000 | [diff] [blame] | 1173 | fu_device_set_status (device, FWUPD_STATUS_SCHEDULING); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1174 | if (!g_file_set_contents (filename, |
| 1175 | g_bytes_get_data (blob_cab, NULL), |
| 1176 | (gssize) g_bytes_get_size (blob_cab), |
| 1177 | error)) |
| 1178 | return FALSE; |
| 1179 | |
| 1180 | /* schedule for next boot */ |
| 1181 | g_debug ("schedule %s to be installed to %s on next boot", |
| 1182 | filename, fu_device_get_id (device)); |
Richard Hughes | 38fb56c | 2018-02-01 22:07:52 +0000 | [diff] [blame] | 1183 | release = fu_device_get_release_default (device); |
| 1184 | fwupd_release_set_version (release_tmp, fwupd_release_get_version (release)); |
| 1185 | fwupd_release_set_filename (release_tmp, filename); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1186 | |
| 1187 | /* add to database */ |
Richard Hughes | 3e90a58 | 2018-01-06 22:38:09 +0000 | [diff] [blame] | 1188 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING); |
Richard Hughes | 38fb56c | 2018-02-01 22:07:52 +0000 | [diff] [blame] | 1189 | if (!fu_history_add_device (history, device, release_tmp, error)) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1190 | return FALSE; |
| 1191 | |
| 1192 | /* next boot we run offline */ |
| 1193 | return fu_plugin_runner_offline_setup (error); |
| 1194 | } |
| 1195 | |
| 1196 | gboolean |
| 1197 | fu_plugin_runner_verify (FuPlugin *plugin, |
| 1198 | FuDevice *device, |
| 1199 | FuPluginVerifyFlags flags, |
| 1200 | GError **error) |
| 1201 | { |
| 1202 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1203 | FuPluginVerifyFunc func = NULL; |
Richard Hughes | ababbb7 | 2017-06-15 20:18:36 +0100 | [diff] [blame] | 1204 | GPtrArray *checksums; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* not enabled */ |
| 1207 | if (!priv->enabled) |
| 1208 | return TRUE; |
| 1209 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1210 | /* no object loaded */ |
| 1211 | if (priv->module == NULL) |
| 1212 | return TRUE; |
| 1213 | |
Richard Hughes | ababbb7 | 2017-06-15 20:18:36 +0100 | [diff] [blame] | 1214 | /* clear any existing verification checksums */ |
| 1215 | checksums = fu_device_get_checksums (device); |
| 1216 | g_ptr_array_set_size (checksums, 0); |
| 1217 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1218 | /* optional */ |
| 1219 | g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func); |
| 1220 | if (func == NULL) |
| 1221 | return TRUE; |
| 1222 | g_debug ("performing verify() on %s", priv->name); |
| 1223 | if (!func (plugin, device, flags, error)) { |
| 1224 | g_prefix_error (error, "failed to verify %s: ", priv->name); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1225 | return FALSE; |
| 1226 | } |
| 1227 | return TRUE; |
| 1228 | } |
| 1229 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1230 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1231 | fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1232 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1233 | guint64 flags; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1234 | |
| 1235 | /* final check */ |
| 1236 | flags = fu_device_get_flags (device); |
| 1237 | if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) { |
| 1238 | g_set_error (error, |
| 1239 | FWUPD_ERROR, |
| 1240 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1241 | "Device %s is not locked", |
| 1242 | fu_device_get_id (device)); |
| 1243 | return FALSE; |
| 1244 | } |
| 1245 | |
Richard Hughes | 9c4b531 | 2017-11-14 11:34:53 +0000 | [diff] [blame] | 1246 | /* run vfunc */ |
| 1247 | if (!fu_plugin_runner_device_generic (plugin, device, |
| 1248 | "fu_plugin_unlock", error)) |
| 1249 | return FALSE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1250 | |
| 1251 | /* update with correct flags */ |
| 1252 | flags = fu_device_get_flags (device); |
| 1253 | fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED); |
| 1254 | fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
| 1255 | return TRUE; |
| 1256 | } |
| 1257 | |
| 1258 | gboolean |
| 1259 | fu_plugin_runner_update (FuPlugin *plugin, |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1260 | FuDevice *device, |
| 1261 | GBytes *blob_cab, |
| 1262 | GBytes *blob_fw, |
| 1263 | FwupdInstallFlags flags, |
| 1264 | GError **error) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1265 | { |
| 1266 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1267 | FuPluginUpdateFunc update_func; |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1268 | g_autoptr(FuHistory) history = NULL; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1269 | g_autoptr(FuDevice) device_pending = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1270 | GError *error_update = NULL; |
Richard Hughes | f556d37 | 2017-06-15 19:49:18 +0100 | [diff] [blame] | 1271 | GPtrArray *checksums; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1272 | |
| 1273 | /* not enabled */ |
Richard Hughes | 41c1548 | 2018-02-01 22:07:21 +0000 | [diff] [blame] | 1274 | if (!priv->enabled) { |
| 1275 | g_debug ("plugin not enabled, skipping"); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1276 | return TRUE; |
Richard Hughes | 41c1548 | 2018-02-01 22:07:21 +0000 | [diff] [blame] | 1277 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1278 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1279 | /* no object loaded */ |
Richard Hughes | 41c1548 | 2018-02-01 22:07:21 +0000 | [diff] [blame] | 1280 | if (priv->module == NULL) { |
| 1281 | g_debug ("module not enabled, skipping"); |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1282 | return TRUE; |
Richard Hughes | 41c1548 | 2018-02-01 22:07:21 +0000 | [diff] [blame] | 1283 | } |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1284 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1285 | /* optional */ |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1286 | g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func); |
| 1287 | if (update_func == NULL) { |
| 1288 | g_set_error_literal (error, |
| 1289 | FWUPD_ERROR, |
| 1290 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1291 | "No update possible"); |
| 1292 | return FALSE; |
| 1293 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1294 | |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1295 | /* just schedule this for the next reboot */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1296 | if (flags & FWUPD_INSTALL_FLAG_OFFLINE) { |
Richard Hughes | 026cdd8 | 2018-05-18 10:23:19 +0100 | [diff] [blame^] | 1297 | if (blob_cab == NULL) { |
| 1298 | g_set_error_literal (error, |
| 1299 | FWUPD_ERROR, |
| 1300 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1301 | "No cabinet archive to schedule"); |
| 1302 | return FALSE; |
| 1303 | } |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1304 | return fu_plugin_runner_schedule_update (plugin, |
| 1305 | device, |
| 1306 | blob_cab, |
| 1307 | error); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | /* cancel the pending action */ |
| 1311 | if (!fu_plugin_runner_offline_invalidate (error)) |
| 1312 | return FALSE; |
| 1313 | |
| 1314 | /* online */ |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1315 | history = fu_history_new (); |
Richard Hughes | 0b9d996 | 2018-01-12 16:31:28 +0000 | [diff] [blame] | 1316 | device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL); |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1317 | if (!update_func (plugin, device, blob_fw, flags, &error_update)) { |
Richard Hughes | c0cd023 | 2018-01-31 15:02:00 +0000 | [diff] [blame] | 1318 | fu_device_set_update_error (device, error_update->message); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1319 | g_propagate_error (error, error_update); |
| 1320 | return FALSE; |
| 1321 | } |
| 1322 | |
Richard Hughes | f556d37 | 2017-06-15 19:49:18 +0100 | [diff] [blame] | 1323 | /* no longer valid */ |
| 1324 | checksums = fu_device_get_checksums (device); |
| 1325 | g_ptr_array_set_size (checksums, 0); |
| 1326 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1327 | /* cleanup */ |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1328 | if (device_pending != NULL) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1329 | const gchar *tmp; |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 1330 | FwupdRelease *release; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1331 | |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1332 | /* update history database */ |
Richard Hughes | c0cd023 | 2018-01-31 15:02:00 +0000 | [diff] [blame] | 1333 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS); |
| 1334 | if (!fu_history_modify_device (history, device, |
| 1335 | FU_HISTORY_FLAGS_MATCH_NEW_VERSION, |
| 1336 | error)) |
Richard Hughes | 0b9d996 | 2018-01-12 16:31:28 +0000 | [diff] [blame] | 1337 | return FALSE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1338 | |
| 1339 | /* delete cab file */ |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 1340 | release = fu_device_get_release_default (device_pending); |
| 1341 | tmp = fwupd_release_get_filename (release); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1342 | if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) { |
| 1343 | g_autoptr(GError) error_local = NULL; |
| 1344 | g_autoptr(GFile) file = NULL; |
| 1345 | file = g_file_new_for_path (tmp); |
| 1346 | if (!g_file_delete (file, NULL, &error_local)) { |
| 1347 | g_set_error (error, |
| 1348 | FWUPD_ERROR, |
| 1349 | FWUPD_ERROR_INVALID_FILE, |
| 1350 | "Failed to delete %s: %s", |
| 1351 | tmp, error_local->message); |
| 1352 | return FALSE; |
| 1353 | } |
| 1354 | } |
| 1355 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1356 | return TRUE; |
| 1357 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1358 | |
| 1359 | gboolean |
| 1360 | fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1361 | { |
| 1362 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1363 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1364 | |
| 1365 | /* not enabled */ |
| 1366 | if (!priv->enabled) |
| 1367 | return TRUE; |
| 1368 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1369 | /* no object loaded */ |
| 1370 | if (priv->module == NULL) |
| 1371 | return TRUE; |
| 1372 | |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1373 | /* optional */ |
| 1374 | g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func); |
| 1375 | if (func == NULL) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1376 | return TRUE; |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1377 | g_debug ("performing clear_result() on %s", priv->name); |
| 1378 | if (!func (plugin, device, error)) { |
| 1379 | g_prefix_error (error, "failed to clear_result %s: ", priv->name); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1380 | return FALSE; |
| 1381 | } |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1382 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | gboolean |
| 1386 | fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1387 | { |
| 1388 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1389 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* not enabled */ |
| 1392 | if (!priv->enabled) |
| 1393 | return TRUE; |
| 1394 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1395 | /* no object loaded */ |
| 1396 | if (priv->module == NULL) |
| 1397 | return TRUE; |
| 1398 | |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1399 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1400 | g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func); |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1401 | if (func == NULL) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1402 | return TRUE; |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1403 | g_debug ("performing get_results() on %s", priv->name); |
| 1404 | if (!func (plugin, device, error)) { |
| 1405 | g_prefix_error (error, "failed to get_results %s: ", priv->name); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1406 | return FALSE; |
| 1407 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1408 | return TRUE; |
| 1409 | } |
| 1410 | |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1411 | /** |
| 1412 | * fu_plugin_get_order: |
| 1413 | * @plugin: a #FuPlugin |
| 1414 | * |
| 1415 | * Gets the plugin order, where higher numbers are run after lower |
| 1416 | * numbers. |
| 1417 | * |
| 1418 | * Returns: the integer value |
| 1419 | **/ |
| 1420 | guint |
| 1421 | fu_plugin_get_order (FuPlugin *plugin) |
| 1422 | { |
| 1423 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1424 | return priv->order; |
| 1425 | } |
| 1426 | |
| 1427 | /** |
| 1428 | * fu_plugin_set_order: |
| 1429 | * @plugin: a #FuPlugin |
| 1430 | * @order: a integer value |
| 1431 | * |
| 1432 | * Sets the plugin order, where higher numbers are run after lower |
| 1433 | * numbers. |
| 1434 | **/ |
| 1435 | void |
| 1436 | fu_plugin_set_order (FuPlugin *plugin, guint order) |
| 1437 | { |
| 1438 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1439 | priv->order = order; |
| 1440 | } |
| 1441 | |
| 1442 | /** |
| 1443 | * fu_plugin_add_rule: |
| 1444 | * @plugin: a #FuPlugin |
| 1445 | * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 1446 | * @name: a plugin name, e.g. `upower` |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1447 | * |
| 1448 | * If the plugin name is found, the rule will be used to sort the plugin list, |
| 1449 | * for example the plugin specified by @name will be ordered after this plugin |
| 1450 | * when %FU_PLUGIN_RULE_RUN_AFTER is used. |
| 1451 | * |
| 1452 | * NOTE: The depsolver is iterative and may not solve overly-complicated rules; |
| 1453 | * If depsolving fails then fwupd will not start. |
| 1454 | **/ |
| 1455 | void |
| 1456 | fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name) |
| 1457 | { |
| 1458 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1459 | g_ptr_array_add (priv->rules[rule], g_strdup (name)); |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * fu_plugin_get_rules: |
| 1464 | * @plugin: a #FuPlugin |
| 1465 | * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS |
| 1466 | * |
| 1467 | * Gets the plugin IDs that should be run after this plugin. |
| 1468 | * |
| 1469 | * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream'] |
| 1470 | **/ |
| 1471 | GPtrArray * |
| 1472 | fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule) |
| 1473 | { |
| 1474 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1475 | return priv->rules[rule]; |
| 1476 | } |
| 1477 | |
Richard Hughes | 80b79bb | 2018-01-11 21:11:06 +0000 | [diff] [blame] | 1478 | /** |
| 1479 | * fu_plugin_add_report_metadata: |
| 1480 | * @plugin: a #FuPlugin |
| 1481 | * @key: a string, e.g. `FwupdateVersion` |
| 1482 | * @value: a string, e.g. `10` |
| 1483 | * |
| 1484 | * Sets any additional metadata to be included in the firmware report to aid |
| 1485 | * debugging problems. |
| 1486 | * |
| 1487 | * Any data included here will be sent to the metadata server after user |
| 1488 | * confirmation. |
| 1489 | **/ |
| 1490 | void |
| 1491 | fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value) |
| 1492 | { |
| 1493 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1494 | g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value)); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * fu_plugin_get_report_metadata: |
| 1499 | * @plugin: a #FuPlugin |
| 1500 | * |
| 1501 | * Returns the list of additional metadata to be added when filing a report. |
| 1502 | * |
| 1503 | * Returns: (transfer none): the map of report metadata |
| 1504 | **/ |
| 1505 | GHashTable * |
| 1506 | fu_plugin_get_report_metadata (FuPlugin *plugin) |
| 1507 | { |
| 1508 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1509 | return priv->report_metadata; |
| 1510 | } |
| 1511 | |
Mario Limonciello | 963dc42 | 2018-02-27 14:26:58 -0600 | [diff] [blame] | 1512 | /** |
| 1513 | * fu_plugin_get_config_value: |
| 1514 | * @plugin: a #FuPlugin |
| 1515 | * @key: A settings key |
| 1516 | * |
| 1517 | * Return the value of a key if it's been configured |
| 1518 | * |
| 1519 | * Since: 1.0.6 |
| 1520 | **/ |
| 1521 | gchar * |
| 1522 | fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key) |
| 1523 | { |
| 1524 | g_autofree gchar *conf_file = NULL; |
| 1525 | g_autofree gchar *conf_path = NULL; |
| 1526 | g_autoptr(GKeyFile) keyfile = NULL; |
| 1527 | const gchar *plugin_name; |
| 1528 | |
| 1529 | plugin_name = fu_plugin_get_name (plugin); |
| 1530 | conf_file = g_strdup_printf ("%s.conf", plugin_name); |
| 1531 | conf_path = g_build_filename (FWUPDCONFIGDIR, conf_file, NULL); |
| 1532 | if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR)) |
| 1533 | return NULL; |
| 1534 | keyfile = g_key_file_new (); |
| 1535 | if (!g_key_file_load_from_file (keyfile, conf_path, |
| 1536 | G_KEY_FILE_NONE, NULL)) |
| 1537 | return NULL; |
| 1538 | return g_key_file_get_string (keyfile, plugin_name, key, NULL); |
| 1539 | } |
| 1540 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1541 | static void |
| 1542 | fu_plugin_class_init (FuPluginClass *klass) |
| 1543 | { |
| 1544 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 1545 | object_class->finalize = fu_plugin_finalize; |
| 1546 | signals[SIGNAL_DEVICE_ADDED] = |
| 1547 | g_signal_new ("device-added", |
| 1548 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1549 | G_STRUCT_OFFSET (FuPluginClass, device_added), |
| 1550 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1551 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
| 1552 | signals[SIGNAL_DEVICE_REMOVED] = |
| 1553 | g_signal_new ("device-removed", |
| 1554 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1555 | G_STRUCT_OFFSET (FuPluginClass, device_removed), |
| 1556 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1557 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 1558 | signals[SIGNAL_DEVICE_REGISTER] = |
| 1559 | g_signal_new ("device-register", |
| 1560 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1561 | G_STRUCT_OFFSET (FuPluginClass, device_register), |
| 1562 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1563 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 1564 | signals[SIGNAL_RECOLDPLUG] = |
| 1565 | g_signal_new ("recoldplug", |
| 1566 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1567 | G_STRUCT_OFFSET (FuPluginClass, recoldplug), |
| 1568 | NULL, NULL, g_cclosure_marshal_VOID__VOID, |
| 1569 | G_TYPE_NONE, 0); |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 1570 | signals[SIGNAL_SET_COLDPLUG_DELAY] = |
| 1571 | g_signal_new ("set-coldplug-delay", |
| 1572 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1573 | G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay), |
| 1574 | NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 1575 | G_TYPE_NONE, 1, G_TYPE_UINT); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | static void |
| 1579 | fu_plugin_init (FuPlugin *plugin) |
| 1580 | { |
| 1581 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1582 | priv->enabled = TRUE; |
| 1583 | priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 1584 | g_free, (GDestroyNotify) g_object_unref); |
Richard Hughes | e8b5db6 | 2017-07-17 14:18:22 +0100 | [diff] [blame] | 1585 | priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal); |
Richard Hughes | 80b79bb | 2018-01-11 21:11:06 +0000 | [diff] [blame] | 1586 | priv->report_metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1587 | for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++) |
| 1588 | priv->rules[i] = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | static void |
| 1592 | fu_plugin_finalize (GObject *object) |
| 1593 | { |
| 1594 | FuPlugin *plugin = FU_PLUGIN (object); |
| 1595 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1596 | FuPluginInitFunc func = NULL; |
| 1597 | |
| 1598 | /* optional */ |
| 1599 | if (priv->module != NULL) { |
| 1600 | g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func); |
| 1601 | if (func != NULL) { |
| 1602 | g_debug ("performing destroy() on %s", priv->name); |
| 1603 | func (plugin); |
| 1604 | } |
| 1605 | } |
| 1606 | |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1607 | for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++) |
| 1608 | g_ptr_array_unref (priv->rules[i]); |
| 1609 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1610 | if (priv->usb_ctx != NULL) |
| 1611 | g_object_unref (priv->usb_ctx); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 1612 | if (priv->hwids != NULL) |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 1613 | g_object_unref (priv->hwids); |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 1614 | if (priv->quirks != NULL) |
| 1615 | g_object_unref (priv->quirks); |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 1616 | if (priv->supported_guids != NULL) |
| 1617 | g_ptr_array_unref (priv->supported_guids); |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 1618 | if (priv->smbios != NULL) |
| 1619 | g_object_unref (priv->smbios); |
Richard Hughes | 275d3b4 | 2018-04-20 16:40:37 +0100 | [diff] [blame] | 1620 | if (priv->runtime_versions != NULL) |
| 1621 | g_hash_table_unref (priv->runtime_versions); |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 1622 | if (priv->compile_versions != NULL) |
| 1623 | g_hash_table_unref (priv->compile_versions); |
Richard Hughes | 576c012 | 2017-02-24 09:47:00 +0000 | [diff] [blame] | 1624 | #ifndef RUNNING_ON_VALGRIND |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1625 | if (priv->module != NULL) |
| 1626 | g_module_close (priv->module); |
Richard Hughes | 576c012 | 2017-02-24 09:47:00 +0000 | [diff] [blame] | 1627 | #endif |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1628 | g_hash_table_unref (priv->devices); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 1629 | g_hash_table_unref (priv->devices_delay); |
Richard Hughes | 80b79bb | 2018-01-11 21:11:06 +0000 | [diff] [blame] | 1630 | g_hash_table_unref (priv->report_metadata); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1631 | g_free (priv->name); |
| 1632 | g_free (priv->data); |
| 1633 | |
| 1634 | G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object); |
| 1635 | } |
| 1636 | |
| 1637 | FuPlugin * |
| 1638 | fu_plugin_new (void) |
| 1639 | { |
| 1640 | FuPlugin *plugin; |
| 1641 | plugin = g_object_new (FU_TYPE_PLUGIN, NULL); |
| 1642 | return plugin; |
| 1643 | } |