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 | * |
| 3 | * Copyright (C) 2016 Richard Hughes <richard@hughsie.com> |
| 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 | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 34 | #include "fu-plugin-private.h" |
| 35 | #include "fu-pending.h" |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 36 | |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 37 | #define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */ |
| 38 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 39 | static void fu_plugin_finalize (GObject *object); |
| 40 | |
| 41 | typedef struct { |
| 42 | GModule *module; |
| 43 | GUsbContext *usb_ctx; |
| 44 | gboolean enabled; |
| 45 | gchar *name; |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 46 | GHashTable *hwids; /* hwid:1 */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 47 | GHashTable *devices; /* platform_id:GObject */ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 48 | GHashTable *devices_delay; /* FuDevice:FuPluginHelper */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 49 | FuPluginData *data; |
| 50 | } FuPluginPrivate; |
| 51 | |
| 52 | enum { |
| 53 | SIGNAL_DEVICE_ADDED, |
| 54 | SIGNAL_DEVICE_REMOVED, |
| 55 | SIGNAL_STATUS_CHANGED, |
| 56 | SIGNAL_PERCENTAGE_CHANGED, |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 57 | SIGNAL_RECOLDPLUG, |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 58 | SIGNAL_SET_COLDPLUG_DELAY, |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 59 | SIGNAL_LAST |
| 60 | }; |
| 61 | |
| 62 | static guint signals[SIGNAL_LAST] = { 0 }; |
| 63 | |
| 64 | G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT) |
| 65 | #define GET_PRIVATE(o) (fu_plugin_get_instance_private (o)) |
| 66 | |
| 67 | typedef const gchar *(*FuPluginGetNameFunc) (void); |
| 68 | typedef void (*FuPluginInitFunc) (FuPlugin *plugin); |
| 69 | typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin, |
| 70 | GError **error); |
| 71 | typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin, |
| 72 | FuDevice *device, |
| 73 | GError **error); |
| 74 | typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin, |
| 75 | FuDevice *device, |
| 76 | FuPluginVerifyFlags flags, |
| 77 | GError **error); |
| 78 | typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin, |
| 79 | FuDevice *device, |
| 80 | GBytes *blob_fw, |
| 81 | FwupdInstallFlags flags, |
| 82 | GError **error); |
| 83 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 84 | /** |
| 85 | * fu_plugin_get_name: |
| 86 | * @plugin: A #FuPlugin |
| 87 | * |
| 88 | * Gets the plugin name. |
| 89 | * |
| 90 | * Returns: a plugin name, or %NULL for unknown. |
| 91 | * |
| 92 | * Since: 0.8.0 |
| 93 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 94 | const gchar * |
| 95 | fu_plugin_get_name (FuPlugin *plugin) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 96 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 97 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 98 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 99 | return priv->name; |
| 100 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 101 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 102 | /** |
| 103 | * fu_plugin_cache_lookup: |
| 104 | * @plugin: A #FuPlugin |
| 105 | * @id: the key |
| 106 | * |
| 107 | * Finds an object in the per-plugin cache. |
| 108 | * |
| 109 | * Returns: (transfer none): a #GObject, or %NULL for unfound. |
| 110 | * |
| 111 | * Since: 0.8.0 |
| 112 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 113 | gpointer |
| 114 | fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id) |
| 115 | { |
| 116 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 117 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
| 118 | g_return_val_if_fail (id != NULL, NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 119 | return g_hash_table_lookup (priv->devices, id); |
| 120 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 121 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 122 | /** |
| 123 | * fu_plugin_cache_add: |
| 124 | * @plugin: A #FuPlugin |
| 125 | * @id: the key |
| 126 | * @dev: a #GObject, typically a #FuDevice |
| 127 | * |
| 128 | * Adds an object to the per-plugin cache. |
| 129 | * |
| 130 | * Since: 0.8.0 |
| 131 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 132 | void |
| 133 | fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev) |
| 134 | { |
| 135 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 136 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 137 | g_return_if_fail (id != NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 138 | g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev)); |
| 139 | } |
| 140 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 141 | /** |
| 142 | * fu_plugin_cache_remove: |
| 143 | * @plugin: A #FuPlugin |
| 144 | * @id: the key |
| 145 | * |
| 146 | * Removes an object from the per-plugin cache. |
| 147 | * |
| 148 | * Since: 0.8.0 |
| 149 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 150 | void |
| 151 | fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id) |
| 152 | { |
| 153 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 154 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 155 | g_return_if_fail (id != NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 156 | g_hash_table_remove (priv->devices, id); |
| 157 | } |
| 158 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 159 | /** |
| 160 | * fu_plugin_get_data: |
| 161 | * @plugin: A #FuPlugin |
| 162 | * |
| 163 | * Gets the per-plugin allocated private data. |
| 164 | * |
| 165 | * Returns: (transfer full): a pointer to a structure, or %NULL for unset. |
| 166 | * |
| 167 | * Since: 0.8.0 |
| 168 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 169 | FuPluginData * |
| 170 | fu_plugin_get_data (FuPlugin *plugin) |
| 171 | { |
| 172 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 173 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 174 | return priv->data; |
| 175 | } |
| 176 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 177 | /** |
| 178 | * fu_plugin_alloc_data: |
| 179 | * @plugin: A #FuPlugin |
| 180 | * @data_sz: the size to allocate |
| 181 | * |
| 182 | * Allocates the per-plugin allocated private data. |
| 183 | * |
| 184 | * Returns: (transfer full): a pointer to a structure, or %NULL for unset. |
| 185 | * |
| 186 | * Since: 0.8.0 |
| 187 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 188 | FuPluginData * |
| 189 | fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz) |
| 190 | { |
| 191 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 192 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | 44dee88 | 2017-01-11 08:31:10 +0000 | [diff] [blame] | 193 | if (priv->data != NULL) { |
| 194 | g_critical ("fu_plugin_alloc_data() already used by plugin"); |
| 195 | return priv->data; |
| 196 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 197 | priv->data = g_malloc0 (data_sz); |
| 198 | return priv->data; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 201 | /** |
| 202 | * fu_plugin_get_usb_context: |
| 203 | * @plugin: A #FuPlugin |
| 204 | * |
| 205 | * Gets the shared USB context that all plugins can use. |
| 206 | * |
| 207 | * Returns: (transfer none): a #GUsbContext. |
| 208 | * |
| 209 | * Since: 0.8.0 |
| 210 | **/ |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 211 | GUsbContext * |
| 212 | fu_plugin_get_usb_context (FuPlugin *plugin) |
| 213 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 214 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 215 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 216 | return priv->usb_ctx; |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void |
| 220 | fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx) |
| 221 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 222 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 223 | g_set_object (&priv->usb_ctx, usb_ctx); |
| 224 | } |
| 225 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 226 | /** |
| 227 | * fu_plugin_get_enabled: |
| 228 | * @plugin: A #FuPlugin |
| 229 | * |
| 230 | * Returns if the plugin is enabled. |
| 231 | * |
| 232 | * Returns: %TRUE if the plugin is currently enabled. |
| 233 | * |
| 234 | * Since: 0.8.0 |
| 235 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 236 | gboolean |
| 237 | fu_plugin_get_enabled (FuPlugin *plugin) |
| 238 | { |
| 239 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 240 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 241 | return priv->enabled; |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 244 | /** |
| 245 | * fu_plugin_set_enabled: |
| 246 | * @plugin: A #FuPlugin |
| 247 | * @enabled: the enabled value |
| 248 | * |
| 249 | * Enables or disables a plugin. Plugins can self-disable at any point. |
| 250 | * |
| 251 | * Since: 0.8.0 |
| 252 | **/ |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 253 | void |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 254 | fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 255 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 256 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 257 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 258 | priv->enabled = enabled; |
| 259 | } |
| 260 | |
| 261 | gboolean |
| 262 | fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error) |
| 263 | { |
| 264 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 265 | FuPluginInitFunc func = NULL; |
| 266 | gchar *str; |
| 267 | |
| 268 | priv->module = g_module_open (filename, 0); |
| 269 | if (priv->module == NULL) { |
| 270 | g_set_error (error, |
| 271 | G_IO_ERROR, |
| 272 | G_IO_ERROR_FAILED, |
| 273 | "failed to open plugin: %s", |
| 274 | g_module_error ()); |
| 275 | return FALSE; |
| 276 | } |
| 277 | |
| 278 | /* set automatically */ |
| 279 | str = g_strstr_len (filename, -1, "libfu_plugin_"); |
| 280 | if (str != NULL) { |
| 281 | priv->name = g_strdup (str + 13); |
| 282 | g_strdelimit (priv->name, ".", '\0'); |
| 283 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 284 | |
| 285 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 286 | g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func); |
| 287 | if (func != NULL) { |
| 288 | g_debug ("performing init() on %s", filename); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 289 | func (plugin); |
| 290 | } |
| 291 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 292 | return TRUE; |
| 293 | } |
| 294 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 295 | /** |
| 296 | * fu_plugin_device_add: |
| 297 | * @plugin: A #FuPlugin |
| 298 | * @device: A #FuDevice |
| 299 | * |
| 300 | * Asks the daemon to add a device to the exported list. If this device ID |
| 301 | * has already been added by a different plugin then this request will be |
| 302 | * ignored. |
| 303 | * |
| 304 | * Plugins should use fu_plugin_device_add_delay() if they are not capable of |
| 305 | * actually flashing an image to the hardware so that higher-priority plugins |
| 306 | * can add the device themselves. |
| 307 | * |
| 308 | * Since: 0.8.0 |
| 309 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 310 | void |
| 311 | fu_plugin_device_add (FuPlugin *plugin, FuDevice *device) |
| 312 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 313 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 314 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 315 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 316 | g_debug ("emit added from %s: %s", |
| 317 | fu_plugin_get_name (plugin), |
| 318 | fu_device_get_id (device)); |
| 319 | fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
| 320 | fu_device_set_plugin (device, fu_plugin_get_name (plugin)); |
| 321 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device); |
| 322 | } |
| 323 | |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 324 | typedef struct { |
| 325 | FuPlugin *plugin; |
| 326 | FuDevice *device; |
| 327 | guint timeout_id; |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 328 | GHashTable *devices; |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 329 | } FuPluginHelper; |
| 330 | |
| 331 | static void |
| 332 | fu_plugin_helper_free (FuPluginHelper *helper) |
| 333 | { |
| 334 | g_object_unref (helper->plugin); |
| 335 | g_object_unref (helper->device); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 336 | g_hash_table_unref (helper->devices); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 337 | g_free (helper); |
| 338 | } |
| 339 | |
| 340 | static gboolean |
| 341 | fu_plugin_device_add_delay_cb (gpointer user_data) |
| 342 | { |
| 343 | FuPluginHelper *helper = (FuPluginHelper *) user_data; |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 344 | g_hash_table_remove (helper->devices, helper->device); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 345 | fu_plugin_device_add (helper->plugin, helper->device); |
| 346 | fu_plugin_helper_free (helper); |
| 347 | return FALSE; |
| 348 | } |
| 349 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 350 | /** |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 351 | * fu_plugin_has_device_delay: |
| 352 | * @plugin: A #FuPlugin |
| 353 | * |
| 354 | * Returns if the device has a pending device that is waiting to be added. |
| 355 | * |
| 356 | * Returns: %TRUE if a device is waiting to be added |
| 357 | * |
| 358 | * Since: 0.8.0 |
| 359 | **/ |
| 360 | gboolean |
| 361 | fu_plugin_has_device_delay (FuPlugin *plugin) |
| 362 | { |
| 363 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 364 | return g_hash_table_size (priv->devices_delay) > 0; |
| 365 | } |
| 366 | |
| 367 | /** |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 368 | * fu_plugin_device_add_delay: |
| 369 | * @plugin: A #FuPlugin |
| 370 | * @device: A #FuDevice |
| 371 | * |
| 372 | * Asks the daemon to add a device to the exported list after a small delay. |
| 373 | * |
| 374 | * Since: 0.8.0 |
| 375 | **/ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 376 | void |
| 377 | fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device) |
| 378 | { |
| 379 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 380 | FuPluginHelper *helper; |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 381 | |
| 382 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 383 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 384 | |
Richard Hughes | 6ad951d | 2017-02-03 10:12:12 +0000 | [diff] [blame] | 385 | /* already waiting for add */ |
| 386 | helper = g_hash_table_lookup (priv->devices_delay, device); |
| 387 | if (helper != NULL) { |
| 388 | g_warning ("ignoring add-delay as device %s already pending", |
| 389 | fu_device_get_id (device)); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | /* add after a small delay */ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 394 | g_debug ("waiting a small time for other plugins"); |
| 395 | helper = g_new0 (FuPluginHelper, 1); |
| 396 | helper->plugin = g_object_ref (plugin); |
| 397 | helper->device = g_object_ref (device); |
| 398 | 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] | 399 | helper->devices = g_hash_table_ref (priv->devices_delay); |
| 400 | g_hash_table_insert (helper->devices, device, helper); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 403 | /** |
| 404 | * fu_plugin_device_add: |
| 405 | * @plugin: A #FuPlugin |
| 406 | * @device: A #FuDevice |
| 407 | * |
| 408 | * Asks the daemon to remove a device from the exported list. |
| 409 | * |
| 410 | * Since: 0.8.0 |
| 411 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 412 | void |
| 413 | fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device) |
| 414 | { |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 415 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 416 | FuPluginHelper *helper; |
| 417 | |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 418 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 419 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 420 | |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 421 | /* waiting for add */ |
| 422 | helper = g_hash_table_lookup (priv->devices_delay, device); |
| 423 | if (helper != NULL) { |
| 424 | g_debug ("ignoring remove from delayed addition"); |
| 425 | g_source_remove (helper->timeout_id); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 426 | g_hash_table_remove (priv->devices_delay, helper->device); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 427 | fu_plugin_helper_free (helper); |
| 428 | return; |
| 429 | } |
| 430 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 431 | g_debug ("emit removed from %s: %s", |
| 432 | fu_plugin_get_name (plugin), |
| 433 | fu_device_get_id (device)); |
| 434 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device); |
| 435 | } |
| 436 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 437 | /** |
| 438 | * fu_plugin_set_status: |
| 439 | * @plugin: A #FuPlugin |
| 440 | * @status: A #FwupdStatus, e.g. #FWUPD_STATUS_DECOMPRESSING |
| 441 | * |
| 442 | * Sets the global state of the daemon according to the current plugin action. |
| 443 | * |
| 444 | * Since: 0.8.0 |
| 445 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 446 | void |
| 447 | fu_plugin_set_status (FuPlugin *plugin, FwupdStatus status) |
| 448 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 449 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 450 | g_signal_emit (plugin, signals[SIGNAL_STATUS_CHANGED], 0, status); |
| 451 | } |
| 452 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 453 | /** |
| 454 | * fu_plugin_set_percentage: |
| 455 | * @plugin: A #FuPlugin |
| 456 | * @percentage: the percentage complete |
| 457 | * |
| 458 | * Sets the global completion of the daemon according to the current plugin |
| 459 | * action. |
| 460 | * |
| 461 | * Since: 0.8.0 |
| 462 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 463 | void |
| 464 | fu_plugin_set_percentage (FuPlugin *plugin, guint percentage) |
| 465 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 466 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 467 | g_return_if_fail (percentage <= 100); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 468 | g_signal_emit (plugin, signals[SIGNAL_PERCENTAGE_CHANGED], 0, |
| 469 | percentage); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 472 | /** |
| 473 | * fu_plugin_recoldplug: |
| 474 | * @plugin: A #FuPlugin |
| 475 | * |
| 476 | * Ask all the plugins to coldplug all devices, which will include the prepare() |
| 477 | * and cleanup() phases. Duplicate devices added will be ignored. |
| 478 | * |
| 479 | * Since: 0.8.0 |
| 480 | **/ |
| 481 | void |
| 482 | fu_plugin_recoldplug (FuPlugin *plugin) |
| 483 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 484 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 485 | g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0); |
| 486 | } |
| 487 | |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 488 | /** |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 489 | * fu_plugin_check_hwid: |
| 490 | * @plugin: A #FuPlugin |
| 491 | * @hwid: A Hardware ID GUID, e.g. "6de5d951-d755-576b-bd09-c5cf66b27234" |
| 492 | * |
| 493 | * Checks to see if a specific hardware ID exists. All hardware IDs on a |
| 494 | * specific system can be shown using the `fwupdmgr hwids` command. |
| 495 | * |
| 496 | * Since: 0.9.1 |
| 497 | **/ |
| 498 | gboolean |
| 499 | fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid) |
| 500 | { |
| 501 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 502 | if (priv->hwids == NULL) |
| 503 | return FALSE; |
| 504 | return g_hash_table_lookup (priv->hwids, hwid) != NULL; |
| 505 | } |
| 506 | |
| 507 | void |
| 508 | fu_plugin_set_hwids (FuPlugin *plugin, GHashTable *hwids) |
| 509 | { |
| 510 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 511 | if (priv->hwids != NULL) |
| 512 | g_hash_table_unref (priv->hwids); |
| 513 | priv->hwids = g_hash_table_ref (hwids); |
| 514 | } |
| 515 | |
| 516 | /** |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 517 | * fu_plugin_set_coldplug_delay: |
| 518 | * @plugin: A #FuPlugin |
| 519 | * @duration: A delay in milliseconds |
| 520 | * |
| 521 | * Set the minimum time that should be waited inbetween the call to |
| 522 | * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going |
| 523 | * to be the minimum hardware initialisation time from a datasheet. |
| 524 | * |
| 525 | * It is better to use this function rather than using a sleep() in the plugin |
| 526 | * itself as then only one delay is done in the daemon rather than waiting for |
| 527 | * each coldplug prepare in a serial way. |
| 528 | * |
| 529 | * Additionally, very long delays should be avoided as the daemon will be |
| 530 | * blocked from processing requests whilst the coldplug delay is being |
| 531 | * performed. |
| 532 | * |
| 533 | * Since: 0.8.0 |
| 534 | **/ |
| 535 | void |
| 536 | fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration) |
| 537 | { |
| 538 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 539 | g_return_if_fail (duration > 0); |
| 540 | |
| 541 | /* check sanity */ |
| 542 | if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) { |
| 543 | g_warning ("duration of %ums is crazy, truncating to %ums", |
| 544 | duration, |
| 545 | FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM); |
| 546 | duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM; |
| 547 | } |
| 548 | |
| 549 | /* emit */ |
| 550 | g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration); |
| 551 | } |
| 552 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 553 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 554 | fu_plugin_runner_startup (FuPlugin *plugin, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 555 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 556 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 557 | FuPluginStartupFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 558 | |
| 559 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 560 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 561 | return TRUE; |
| 562 | |
| 563 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 564 | g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func); |
| 565 | if (func == NULL) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 566 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 567 | g_debug ("performing startup() on %s", priv->name); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 568 | if (!func (plugin, error)) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 569 | g_prefix_error (error, "failed to startup %s: ", priv->name); |
| 570 | return FALSE; |
| 571 | } |
| 572 | return TRUE; |
| 573 | } |
| 574 | |
| 575 | static gboolean |
| 576 | fu_plugin_runner_offline_invalidate (GError **error) |
| 577 | { |
| 578 | g_autoptr(GError) error_local = NULL; |
| 579 | g_autoptr(GFile) file1 = NULL; |
| 580 | |
| 581 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 582 | |
| 583 | file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME); |
| 584 | if (!g_file_query_exists (file1, NULL)) |
| 585 | return TRUE; |
| 586 | if (!g_file_delete (file1, NULL, &error_local)) { |
| 587 | g_set_error (error, |
| 588 | FWUPD_ERROR, |
| 589 | FWUPD_ERROR_INTERNAL, |
| 590 | "Cannot delete %s: %s", |
| 591 | FU_OFFLINE_TRIGGER_FILENAME, |
| 592 | error_local->message); |
| 593 | return FALSE; |
| 594 | } |
| 595 | return TRUE; |
| 596 | } |
| 597 | |
| 598 | static gboolean |
| 599 | fu_plugin_runner_offline_setup (GError **error) |
| 600 | { |
| 601 | gint rc; |
| 602 | |
| 603 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 604 | |
| 605 | /* create symlink for the systemd-system-update-generator */ |
| 606 | rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME); |
| 607 | if (rc < 0) { |
| 608 | g_set_error (error, |
| 609 | FWUPD_ERROR, |
| 610 | FWUPD_ERROR_INTERNAL, |
| 611 | "Failed to create symlink %s to %s: %s", |
| 612 | FU_OFFLINE_TRIGGER_FILENAME, |
| 613 | "/var/lib", strerror (errno)); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 614 | return FALSE; |
| 615 | } |
| 616 | return TRUE; |
| 617 | } |
| 618 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 619 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 620 | fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 621 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 622 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 623 | FuPluginStartupFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 624 | |
| 625 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 626 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 627 | return TRUE; |
| 628 | |
| 629 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 630 | g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func); |
| 631 | if (func == NULL) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 632 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 633 | g_debug ("performing coldplug() on %s", priv->name); |
| 634 | if (!func (plugin, error)) { |
| 635 | g_prefix_error (error, "failed to coldplug %s: ", priv->name); |
| 636 | return FALSE; |
| 637 | } |
| 638 | return TRUE; |
| 639 | } |
| 640 | |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 641 | gboolean |
Richard Hughes | 46487c9 | 2017-01-07 21:26:34 +0000 | [diff] [blame] | 642 | fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error) |
| 643 | { |
| 644 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 645 | FuPluginStartupFunc func = NULL; |
| 646 | |
| 647 | /* not enabled */ |
| 648 | if (!priv->enabled) |
| 649 | return TRUE; |
| 650 | |
| 651 | /* optional */ |
| 652 | g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func); |
| 653 | if (func == NULL) |
| 654 | return TRUE; |
| 655 | g_debug ("performing coldplug_prepare() on %s", priv->name); |
| 656 | if (!func (plugin, error)) { |
| 657 | g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name); |
| 658 | return FALSE; |
| 659 | } |
| 660 | return TRUE; |
| 661 | } |
| 662 | |
| 663 | gboolean |
| 664 | fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error) |
| 665 | { |
| 666 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 667 | FuPluginStartupFunc func = NULL; |
| 668 | |
| 669 | /* not enabled */ |
| 670 | if (!priv->enabled) |
| 671 | return TRUE; |
| 672 | |
| 673 | /* optional */ |
| 674 | g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func); |
| 675 | if (func == NULL) |
| 676 | return TRUE; |
| 677 | g_debug ("performing coldplug_cleanup() on %s", priv->name); |
| 678 | if (!func (plugin, error)) { |
| 679 | g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name); |
| 680 | return FALSE; |
| 681 | } |
| 682 | return TRUE; |
| 683 | } |
| 684 | |
| 685 | gboolean |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 686 | fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error) |
| 687 | { |
| 688 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 689 | FuPluginDeviceFunc func = NULL; |
| 690 | |
| 691 | /* not enabled */ |
| 692 | if (!priv->enabled) |
| 693 | return TRUE; |
| 694 | |
| 695 | /* optional */ |
| 696 | g_module_symbol (priv->module, "fu_plugin_update_prepare", (gpointer *) &func); |
| 697 | if (func == NULL) |
| 698 | return TRUE; |
| 699 | g_debug ("performing update_prepare() on %s", priv->name); |
| 700 | if (!func (plugin, device, error)) { |
| 701 | g_prefix_error (error, "failed to prepare for update %s: ", priv->name); |
| 702 | return FALSE; |
| 703 | } |
| 704 | return TRUE; |
| 705 | } |
| 706 | |
| 707 | gboolean |
| 708 | fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error) |
| 709 | { |
| 710 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 711 | FuPluginDeviceFunc func = NULL; |
| 712 | |
| 713 | /* not enabled */ |
| 714 | if (!priv->enabled) |
| 715 | return TRUE; |
| 716 | |
| 717 | /* optional */ |
| 718 | g_module_symbol (priv->module, "fu_plugin_update_cleanup", (gpointer *) &func); |
| 719 | if (func == NULL) |
| 720 | return TRUE; |
| 721 | g_debug ("performing update_cleanup() on %s", priv->name); |
| 722 | if (!func (plugin, device, error)) { |
| 723 | g_prefix_error (error, "failed to cleanup update %s: ", priv->name); |
| 724 | return FALSE; |
| 725 | } |
| 726 | return TRUE; |
| 727 | } |
| 728 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 729 | static gboolean |
| 730 | fu_plugin_runner_schedule_update (FuPlugin *plugin, |
| 731 | FuDevice *device, |
| 732 | GBytes *blob_cab, |
| 733 | GError **error) |
| 734 | { |
| 735 | gchar tmpname[] = {"XXXXXX.cap"}; |
| 736 | g_autofree gchar *dirname = NULL; |
| 737 | g_autofree gchar *filename = NULL; |
| 738 | g_autoptr(FwupdResult) res_tmp = NULL; |
| 739 | g_autoptr(FuPending) pending = NULL; |
| 740 | g_autoptr(GFile) file = NULL; |
| 741 | |
| 742 | /* id already exists */ |
| 743 | pending = fu_pending_new (); |
| 744 | res_tmp = fu_pending_get_device (pending, fu_device_get_id (device), NULL); |
| 745 | if (res_tmp != NULL) { |
| 746 | g_set_error (error, |
| 747 | FWUPD_ERROR, |
| 748 | FWUPD_ERROR_ALREADY_PENDING, |
| 749 | "%s is already scheduled to be updated", |
| 750 | fu_device_get_id (device)); |
| 751 | return FALSE; |
| 752 | } |
| 753 | |
| 754 | /* create directory */ |
| 755 | dirname = g_build_filename (LOCALSTATEDIR, "lib", "fwupd", NULL); |
| 756 | file = g_file_new_for_path (dirname); |
| 757 | if (!g_file_query_exists (file, NULL)) { |
| 758 | if (!g_file_make_directory_with_parents (file, NULL, error)) |
| 759 | return FALSE; |
| 760 | } |
| 761 | |
| 762 | /* get a random filename */ |
| 763 | for (guint i = 0; i < 6; i++) |
| 764 | tmpname[i] = (gchar) g_random_int_range ('A', 'Z'); |
| 765 | filename = g_build_filename (dirname, tmpname, NULL); |
| 766 | |
| 767 | /* just copy to the temp file */ |
| 768 | fu_plugin_set_status (plugin, FWUPD_STATUS_SCHEDULING); |
| 769 | if (!g_file_set_contents (filename, |
| 770 | g_bytes_get_data (blob_cab, NULL), |
| 771 | (gssize) g_bytes_get_size (blob_cab), |
| 772 | error)) |
| 773 | return FALSE; |
| 774 | |
| 775 | /* schedule for next boot */ |
| 776 | g_debug ("schedule %s to be installed to %s on next boot", |
| 777 | filename, fu_device_get_id (device)); |
| 778 | fu_device_set_update_filename (device, filename); |
| 779 | |
| 780 | /* add to database */ |
| 781 | if (!fu_pending_add_device (pending, FWUPD_RESULT (device), error)) |
| 782 | return FALSE; |
| 783 | |
| 784 | /* next boot we run offline */ |
| 785 | return fu_plugin_runner_offline_setup (error); |
| 786 | } |
| 787 | |
| 788 | gboolean |
| 789 | fu_plugin_runner_verify (FuPlugin *plugin, |
| 790 | FuDevice *device, |
| 791 | FuPluginVerifyFlags flags, |
| 792 | GError **error) |
| 793 | { |
| 794 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 795 | FuPluginVerifyFunc func = NULL; |
Richard Hughes | ababbb7 | 2017-06-15 20:18:36 +0100 | [diff] [blame] | 796 | GPtrArray *checksums; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 797 | |
| 798 | /* not enabled */ |
| 799 | if (!priv->enabled) |
| 800 | return TRUE; |
| 801 | |
Richard Hughes | ababbb7 | 2017-06-15 20:18:36 +0100 | [diff] [blame] | 802 | /* clear any existing verification checksums */ |
| 803 | checksums = fu_device_get_checksums (device); |
| 804 | g_ptr_array_set_size (checksums, 0); |
| 805 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 806 | /* optional */ |
| 807 | g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func); |
| 808 | if (func == NULL) |
| 809 | return TRUE; |
| 810 | g_debug ("performing verify() on %s", priv->name); |
| 811 | if (!func (plugin, device, flags, error)) { |
| 812 | g_prefix_error (error, "failed to verify %s: ", priv->name); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 813 | return FALSE; |
| 814 | } |
| 815 | return TRUE; |
| 816 | } |
| 817 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 818 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 819 | fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 820 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 821 | guint64 flags; |
| 822 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 823 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 824 | |
| 825 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 826 | if (!priv->enabled) |
| 827 | return TRUE; |
| 828 | |
| 829 | /* final check */ |
| 830 | flags = fu_device_get_flags (device); |
| 831 | if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) { |
| 832 | g_set_error (error, |
| 833 | FWUPD_ERROR, |
| 834 | FWUPD_ERROR_NOT_SUPPORTED, |
| 835 | "Device %s is not locked", |
| 836 | fu_device_get_id (device)); |
| 837 | return FALSE; |
| 838 | } |
| 839 | |
| 840 | /* optional */ |
| 841 | g_module_symbol (priv->module, "fu_plugin_unlock", (gpointer *) &func); |
| 842 | if (func != NULL) { |
| 843 | g_debug ("performing unlock() on %s", priv->name); |
| 844 | if (!func (plugin, device, error)) { |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 845 | g_prefix_error (error, "failed to unlock %s: ", priv->name); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 846 | return FALSE; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /* update with correct flags */ |
| 851 | flags = fu_device_get_flags (device); |
| 852 | fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED); |
| 853 | fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
| 854 | return TRUE; |
| 855 | } |
| 856 | |
| 857 | gboolean |
| 858 | fu_plugin_runner_update (FuPlugin *plugin, |
| 859 | FuDevice *device, |
| 860 | GBytes *blob_cab, |
| 861 | GBytes *blob_fw, |
| 862 | FwupdInstallFlags flags, |
| 863 | GError **error) |
| 864 | { |
| 865 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 866 | FuPluginUpdateFunc func_online; |
| 867 | FuPluginUpdateFunc func_offline; |
| 868 | g_autoptr(FuPending) pending = NULL; |
| 869 | g_autoptr(FwupdResult) res_pending = NULL; |
| 870 | GError *error_update = NULL; |
Richard Hughes | f556d37 | 2017-06-15 19:49:18 +0100 | [diff] [blame] | 871 | GPtrArray *checksums; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 872 | |
| 873 | /* not enabled */ |
| 874 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 875 | return TRUE; |
| 876 | |
| 877 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 878 | g_module_symbol (priv->module, "fu_plugin_update_online", (gpointer *) &func_online); |
| 879 | g_module_symbol (priv->module, "fu_plugin_update_offline", (gpointer *) &func_offline); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 880 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 881 | /* schedule for next reboot, or handle in the plugin */ |
| 882 | if (flags & FWUPD_INSTALL_FLAG_OFFLINE) { |
| 883 | if (func_offline == NULL) { |
| 884 | return fu_plugin_runner_schedule_update (plugin, |
| 885 | device, |
| 886 | blob_cab, |
| 887 | error); |
| 888 | } |
| 889 | return func_offline (plugin, device, blob_fw, flags, error); |
| 890 | } |
| 891 | |
| 892 | /* cancel the pending action */ |
| 893 | if (!fu_plugin_runner_offline_invalidate (error)) |
| 894 | return FALSE; |
| 895 | |
| 896 | /* online */ |
| 897 | if (func_online == NULL) { |
| 898 | g_set_error_literal (error, |
| 899 | FWUPD_ERROR, |
| 900 | FWUPD_ERROR_NOT_SUPPORTED, |
| 901 | "No online update possible"); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 902 | return FALSE; |
| 903 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 904 | pending = fu_pending_new (); |
| 905 | res_pending = fu_pending_get_device (pending, fu_device_get_id (device), NULL); |
| 906 | if (!func_online (plugin, device, blob_fw, flags, &error_update)) { |
| 907 | /* save the error to the database */ |
| 908 | if (res_pending != NULL) { |
| 909 | fu_pending_set_error_msg (pending, FWUPD_RESULT (device), |
| 910 | error_update->message, NULL); |
| 911 | } |
| 912 | g_propagate_error (error, error_update); |
| 913 | return FALSE; |
| 914 | } |
| 915 | |
Richard Hughes | f556d37 | 2017-06-15 19:49:18 +0100 | [diff] [blame] | 916 | /* no longer valid */ |
| 917 | checksums = fu_device_get_checksums (device); |
| 918 | g_ptr_array_set_size (checksums, 0); |
| 919 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 920 | /* cleanup */ |
| 921 | if (res_pending != NULL) { |
| 922 | const gchar *tmp; |
Richard Hughes | 1642b3b | 2017-06-05 17:40:08 +0100 | [diff] [blame] | 923 | FwupdRelease *rel = fwupd_result_get_release (res_pending); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 924 | |
| 925 | /* update pending database */ |
| 926 | fu_pending_set_state (pending, FWUPD_RESULT (device), |
| 927 | FWUPD_UPDATE_STATE_SUCCESS, NULL); |
| 928 | |
| 929 | /* delete cab file */ |
Richard Hughes | 1642b3b | 2017-06-05 17:40:08 +0100 | [diff] [blame] | 930 | tmp = fwupd_release_get_filename (rel); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 931 | if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) { |
| 932 | g_autoptr(GError) error_local = NULL; |
| 933 | g_autoptr(GFile) file = NULL; |
| 934 | file = g_file_new_for_path (tmp); |
| 935 | if (!g_file_delete (file, NULL, &error_local)) { |
| 936 | g_set_error (error, |
| 937 | FWUPD_ERROR, |
| 938 | FWUPD_ERROR_INVALID_FILE, |
| 939 | "Failed to delete %s: %s", |
| 940 | tmp, error_local->message); |
| 941 | return FALSE; |
| 942 | } |
| 943 | } |
| 944 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 945 | return TRUE; |
| 946 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 947 | |
| 948 | gboolean |
| 949 | fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error) |
| 950 | { |
| 951 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 952 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 953 | g_autoptr(GError) error_local = NULL; |
| 954 | g_autoptr(FwupdResult) res_pending = NULL; |
| 955 | g_autoptr(FuPending) pending = NULL; |
| 956 | |
| 957 | /* not enabled */ |
| 958 | if (!priv->enabled) |
| 959 | return TRUE; |
| 960 | |
| 961 | /* use the plugin if the vfunc is provided */ |
| 962 | g_module_symbol (priv->module, "fu_plugin_clear_result", (gpointer *) &func); |
| 963 | if (func != NULL) { |
| 964 | g_debug ("performing clear_result() on %s", priv->name); |
| 965 | if (!func (plugin, device, error)) { |
| 966 | g_prefix_error (error, "failed to clear_result %s: ", priv->name); |
| 967 | return FALSE; |
| 968 | } |
| 969 | return TRUE; |
| 970 | } |
| 971 | |
| 972 | /* handled using the database */ |
| 973 | pending = fu_pending_new (); |
| 974 | res_pending = fu_pending_get_device (pending, |
| 975 | fu_device_get_id (device), |
| 976 | &error_local); |
| 977 | if (res_pending == NULL) { |
| 978 | g_set_error (error, |
| 979 | FWUPD_ERROR, |
| 980 | FWUPD_ERROR_INVALID_FILE, |
| 981 | "Failed to find %s in pending database: %s", |
| 982 | fu_device_get_id (device), |
| 983 | error_local->message); |
| 984 | return FALSE; |
| 985 | } |
| 986 | |
| 987 | /* remove from pending database */ |
| 988 | return fu_pending_remove_device (pending, FWUPD_RESULT (device), error); |
| 989 | } |
| 990 | |
| 991 | gboolean |
| 992 | fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error) |
| 993 | { |
| 994 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 995 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 996 | FwupdUpdateState update_state; |
Richard Hughes | 1642b3b | 2017-06-05 17:40:08 +0100 | [diff] [blame] | 997 | FwupdRelease *rel; |
| 998 | FwupdDevice *dev; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 999 | const gchar *tmp; |
| 1000 | g_autoptr(GError) error_local = NULL; |
| 1001 | g_autoptr(FwupdResult) res_pending = NULL; |
| 1002 | g_autoptr(FuPending) pending = NULL; |
| 1003 | |
| 1004 | /* not enabled */ |
| 1005 | if (!priv->enabled) |
| 1006 | return TRUE; |
| 1007 | |
| 1008 | /* use the plugin if the vfunc is provided */ |
| 1009 | g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func); |
| 1010 | if (func != NULL) { |
| 1011 | g_debug ("performing get_results() on %s", priv->name); |
| 1012 | if (!func (plugin, device, error)) { |
| 1013 | g_prefix_error (error, "failed to get_results %s: ", priv->name); |
| 1014 | return FALSE; |
| 1015 | } |
| 1016 | return TRUE; |
| 1017 | } |
| 1018 | |
| 1019 | /* handled using the database */ |
| 1020 | pending = fu_pending_new (); |
| 1021 | res_pending = fu_pending_get_device (pending, |
| 1022 | fu_device_get_id (device), |
| 1023 | &error_local); |
| 1024 | if (res_pending == NULL) { |
| 1025 | g_set_error (error, |
| 1026 | FWUPD_ERROR, |
| 1027 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1028 | "Failed to find %s in pending database: %s", |
| 1029 | fu_device_get_id (device), |
| 1030 | error_local->message); |
| 1031 | return FALSE; |
| 1032 | } |
| 1033 | |
| 1034 | /* copy the important parts from the pending device to the real one */ |
| 1035 | update_state = fwupd_result_get_update_state (res_pending); |
| 1036 | if (update_state == FWUPD_UPDATE_STATE_UNKNOWN || |
| 1037 | update_state == FWUPD_UPDATE_STATE_PENDING) { |
| 1038 | g_set_error (error, |
| 1039 | FWUPD_ERROR, |
| 1040 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1041 | "Device %s has not been updated offline yet", |
| 1042 | fu_device_get_id (device)); |
| 1043 | return FALSE; |
| 1044 | } |
| 1045 | |
| 1046 | /* copy */ |
| 1047 | fu_device_set_update_state (device, update_state); |
| 1048 | tmp = fwupd_result_get_update_error (res_pending); |
| 1049 | if (tmp != NULL) |
| 1050 | fu_device_set_update_error (device, tmp); |
Richard Hughes | 1642b3b | 2017-06-05 17:40:08 +0100 | [diff] [blame] | 1051 | dev = fwupd_result_get_device (res_pending); |
| 1052 | tmp = fwupd_device_get_version (dev); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1053 | if (tmp != NULL) |
| 1054 | fu_device_set_version (device, tmp); |
Richard Hughes | 1642b3b | 2017-06-05 17:40:08 +0100 | [diff] [blame] | 1055 | rel = fwupd_result_get_release (res_pending); |
| 1056 | tmp = fwupd_release_get_version (rel); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1057 | if (tmp != NULL) |
| 1058 | fu_device_set_update_version (device, tmp); |
| 1059 | return TRUE; |
| 1060 | } |
| 1061 | |
| 1062 | static void |
| 1063 | fu_plugin_class_init (FuPluginClass *klass) |
| 1064 | { |
| 1065 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 1066 | object_class->finalize = fu_plugin_finalize; |
| 1067 | signals[SIGNAL_DEVICE_ADDED] = |
| 1068 | g_signal_new ("device-added", |
| 1069 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1070 | G_STRUCT_OFFSET (FuPluginClass, device_added), |
| 1071 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1072 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
| 1073 | signals[SIGNAL_DEVICE_REMOVED] = |
| 1074 | g_signal_new ("device-removed", |
| 1075 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1076 | G_STRUCT_OFFSET (FuPluginClass, device_removed), |
| 1077 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1078 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
| 1079 | signals[SIGNAL_STATUS_CHANGED] = |
| 1080 | g_signal_new ("status-changed", |
| 1081 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1082 | G_STRUCT_OFFSET (FuPluginClass, status_changed), |
| 1083 | NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 1084 | G_TYPE_NONE, 1, G_TYPE_UINT); |
| 1085 | signals[SIGNAL_PERCENTAGE_CHANGED] = |
| 1086 | g_signal_new ("percentage-changed", |
| 1087 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1088 | G_STRUCT_OFFSET (FuPluginClass, percentage_changed), |
| 1089 | NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 1090 | G_TYPE_NONE, 1, G_TYPE_UINT); |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 1091 | signals[SIGNAL_RECOLDPLUG] = |
| 1092 | g_signal_new ("recoldplug", |
| 1093 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1094 | G_STRUCT_OFFSET (FuPluginClass, recoldplug), |
| 1095 | NULL, NULL, g_cclosure_marshal_VOID__VOID, |
| 1096 | G_TYPE_NONE, 0); |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 1097 | signals[SIGNAL_SET_COLDPLUG_DELAY] = |
| 1098 | g_signal_new ("set-coldplug-delay", |
| 1099 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1100 | G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay), |
| 1101 | NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 1102 | G_TYPE_NONE, 1, G_TYPE_UINT); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static void |
| 1106 | fu_plugin_init (FuPlugin *plugin) |
| 1107 | { |
| 1108 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1109 | priv->enabled = TRUE; |
| 1110 | priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 1111 | g_free, (GDestroyNotify) g_object_unref); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 1112 | priv->devices_delay = g_hash_table_new (g_str_hash, g_str_equal); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static void |
| 1116 | fu_plugin_finalize (GObject *object) |
| 1117 | { |
| 1118 | FuPlugin *plugin = FU_PLUGIN (object); |
| 1119 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1120 | FuPluginInitFunc func = NULL; |
| 1121 | |
| 1122 | /* optional */ |
| 1123 | if (priv->module != NULL) { |
| 1124 | g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func); |
| 1125 | if (func != NULL) { |
| 1126 | g_debug ("performing destroy() on %s", priv->name); |
| 1127 | func (plugin); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | if (priv->usb_ctx != NULL) |
| 1132 | g_object_unref (priv->usb_ctx); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 1133 | if (priv->hwids != NULL) |
| 1134 | g_hash_table_unref (priv->hwids); |
Richard Hughes | 576c012 | 2017-02-24 09:47:00 +0000 | [diff] [blame] | 1135 | #ifndef RUNNING_ON_VALGRIND |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1136 | if (priv->module != NULL) |
| 1137 | g_module_close (priv->module); |
Richard Hughes | 576c012 | 2017-02-24 09:47:00 +0000 | [diff] [blame] | 1138 | #endif |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1139 | g_hash_table_unref (priv->devices); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 1140 | g_hash_table_unref (priv->devices_delay); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1141 | g_free (priv->name); |
| 1142 | g_free (priv->data); |
| 1143 | |
| 1144 | G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object); |
| 1145 | } |
| 1146 | |
| 1147 | FuPlugin * |
| 1148 | fu_plugin_new (void) |
| 1149 | { |
| 1150 | FuPlugin *plugin; |
| 1151 | plugin = g_object_new (FU_TYPE_PLUGIN, NULL); |
| 1152 | return plugin; |
| 1153 | } |