From 7f67721432c406a43f52ba7adbd3ba9aa808c973 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Sat, 5 Oct 2019 16:19:40 +0100 Subject: [PATCH] Reduce more boilerplate in plugins --- plugins/altos/fu-plugin-altos.c | 25 ---------------- plugins/csr/fu-plugin-csr.c | 26 ----------------- plugins/dfu/dfu-device.c | 21 ++++++++++++++ plugins/dfu/fu-plugin-dfu.c | 45 ----------------------------- plugins/superio/fu-plugin-superio.c | 26 ----------------- src/fu-plugin.c | 35 ++++++++++++++++++++-- 6 files changed, 54 insertions(+), 124 deletions(-) diff --git a/plugins/altos/fu-plugin-altos.c b/plugins/altos/fu-plugin-altos.c index 4496974b3..04561e2fb 100644 --- a/plugins/altos/fu-plugin-altos.c +++ b/plugins/altos/fu-plugin-altos.c @@ -57,28 +57,3 @@ fu_plugin_usb_device_added (FuPlugin *plugin, FuUsbDevice *device, GError **erro fu_plugin_device_add (plugin, FU_DEVICE (dev)); return TRUE; } - -gboolean -fu_plugin_verify (FuPlugin *plugin, - FuDevice *dev, - FuPluginVerifyFlags flags, - GError **error) -{ - g_autoptr(GBytes) blob_fw = NULL; - GChecksumType checksum_types[] = { - G_CHECKSUM_SHA1, - G_CHECKSUM_SHA256, - 0 }; - - /* get data */ - fu_device_set_status (dev, FWUPD_STATUS_DEVICE_VERIFY); - blob_fw = fu_device_read_firmware (dev, error); - if (blob_fw == NULL) - return FALSE; - for (guint i = 0; checksum_types[i] != 0; i++) { - g_autofree gchar *hash = NULL; - hash = g_compute_checksum_for_bytes (checksum_types[i], blob_fw); - fu_device_add_checksum (dev, hash); - } - return TRUE; -} diff --git a/plugins/csr/fu-plugin-csr.c b/plugins/csr/fu-plugin-csr.c index e07a0d51e..0d350dd5a 100644 --- a/plugins/csr/fu-plugin-csr.c +++ b/plugins/csr/fu-plugin-csr.c @@ -30,29 +30,3 @@ fu_plugin_usb_device_added (FuPlugin *plugin, FuUsbDevice *device, GError **erro fu_plugin_device_add (plugin, FU_DEVICE (dev)); return TRUE; } - -gboolean -fu_plugin_verify (FuPlugin *plugin, FuDevice *device, - FuPluginVerifyFlags flags, GError **error) -{ - g_autoptr(GBytes) blob_fw = NULL; - g_autoptr(FuDeviceLocker) locker = NULL; - GChecksumType checksum_types[] = { - G_CHECKSUM_SHA1, - G_CHECKSUM_SHA256, - 0 }; - - /* get data */ - locker = fu_device_locker_new (device, error); - if (locker == NULL) - return FALSE; - blob_fw = fu_device_read_firmware (device, error); - if (blob_fw == NULL) - return FALSE; - for (guint i = 0; checksum_types[i] != 0; i++) { - g_autofree gchar *hash = NULL; - hash = g_compute_checksum_for_bytes (checksum_types[i], blob_fw); - fu_device_add_checksum (device, hash); - } - return TRUE; -} diff --git a/plugins/dfu/dfu-device.c b/plugins/dfu/dfu-device.c index 4a50d4ad0..66ffbf254 100644 --- a/plugins/dfu/dfu-device.c +++ b/plugins/dfu/dfu-device.c @@ -2027,6 +2027,26 @@ dfu_device_get_quirks_as_string (DfuDevice *device) return g_string_free (str, FALSE); } +static GBytes * +dfu_device_read_firmware (FuDevice *device, GError **error) +{ + DfuDevice *self = DFU_DEVICE (device); + g_autoptr(DfuFirmware) dfu_firmware = NULL; + + /* get data from hardware */ + g_debug ("uploading from device->host"); + if (!dfu_device_refresh_and_clear (self, error)) + return NULL; + dfu_firmware = dfu_device_upload (self, + DFU_TARGET_TRANSFER_FLAG_NONE, + error); + if (dfu_firmware == NULL) + return NULL; + + /* get the checksum */ + return dfu_firmware_write_data (dfu_firmware, error); +} + static gboolean dfu_device_set_quirk_kv (FuDevice *device, const gchar *key, @@ -2125,6 +2145,7 @@ dfu_device_class_init (DfuDeviceClass *klass) FuDeviceClass *klass_device = FU_DEVICE_CLASS (klass); FuUsbDeviceClass *klass_usb_device = FU_USB_DEVICE_CLASS (klass); klass_device->set_quirk_kv = dfu_device_set_quirk_kv; + klass_device->read_firmware = dfu_device_read_firmware; klass_usb_device->open = dfu_device_open; klass_usb_device->close = dfu_device_close; klass_usb_device->probe = dfu_device_probe; diff --git a/plugins/dfu/fu-plugin-dfu.c b/plugins/dfu/fu-plugin-dfu.c index 4b0682edc..28163aa88 100644 --- a/plugins/dfu/fu-plugin-dfu.c +++ b/plugins/dfu/fu-plugin-dfu.c @@ -157,48 +157,3 @@ fu_plugin_update (FuPlugin *plugin, /* success */ return TRUE; } - -gboolean -fu_plugin_verify (FuPlugin *plugin, - FuDevice *dev, - FuPluginVerifyFlags flags, - GError **error) -{ - GBytes *blob_fw; - DfuDevice *device = DFU_DEVICE (dev); - g_autoptr(DfuFirmware) dfu_firmware = NULL; - g_autoptr(FuDeviceLocker) locker = NULL; - g_autoptr(GError) error_local = NULL; - GChecksumType checksum_types[] = { - G_CHECKSUM_SHA1, - G_CHECKSUM_SHA256, - 0 }; - - /* open it */ - locker = fu_device_locker_new (device, &error_local); - if (locker == NULL) - return FALSE; - if (!dfu_device_refresh_and_clear (device, error)) - return FALSE; - - /* get data from hardware */ - g_debug ("uploading from device->host"); - dfu_firmware = dfu_device_upload (device, - DFU_TARGET_TRANSFER_FLAG_NONE, - error); - if (dfu_firmware == NULL) - return FALSE; - - /* get the checksum */ - blob_fw = dfu_firmware_write_data (dfu_firmware, error); - if (blob_fw == NULL) - return FALSE; - for (guint i = 0; checksum_types[i] != 0; i++) { - g_autofree gchar *hash = NULL; - hash = g_compute_checksum_for_bytes (checksum_types[i], blob_fw); - fu_device_add_checksum (dev, hash); - } - - /* success */ - return TRUE; -} diff --git a/plugins/superio/fu-plugin-superio.c b/plugins/superio/fu-plugin-superio.c index 16be03c71..e63112140 100644 --- a/plugins/superio/fu-plugin-superio.c +++ b/plugins/superio/fu-plugin-superio.c @@ -106,29 +106,3 @@ fu_plugin_coldplug (FuPlugin *plugin, GError **error) } return TRUE; } - -gboolean -fu_plugin_verify (FuPlugin *plugin, FuDevice *device, - FuPluginVerifyFlags flags, GError **error) -{ - g_autoptr(GBytes) fw = NULL; - g_autoptr(FuDeviceLocker) locker = NULL; - GChecksumType checksum_types[] = { - G_CHECKSUM_SHA1, - G_CHECKSUM_SHA256, - 0 }; - - /* get data */ - locker = fu_device_locker_new (device, error); - if (locker == NULL) - return FALSE; - fw = fu_device_read_firmware (device, error); - if (fw == NULL) - return FALSE; - for (guint i = 0; checksum_types[i] != 0; i++) { - g_autofree gchar *hash = NULL; - hash = g_compute_checksum_for_bytes (checksum_types[i], fw); - fu_device_add_checksum (device, hash); - } - return TRUE; -} diff --git a/src/fu-plugin.c b/src/fu-plugin.c index c0afb9526..7850b9aec 100644 --- a/src/fu-plugin.c +++ b/src/fu-plugin.c @@ -901,6 +901,35 @@ fu_plugin_device_write_firmware (FuPlugin *self, FuDevice *device, return fu_device_write_firmware (device, fw, flags, error); } +static gboolean +fu_plugin_device_read_firmware (FuPlugin *self, FuDevice *device, GError **error) +{ + g_autoptr(FuDeviceLocker) locker = NULL; + g_autoptr(GBytes) fw = NULL; + GChecksumType checksum_types[] = { + G_CHECKSUM_SHA1, + G_CHECKSUM_SHA256, + 0 }; + locker = fu_device_locker_new (device, error); + if (locker == NULL) + return FALSE; + if (!fu_device_detach (device, error)) + return FALSE; + fw = fu_device_read_firmware (device, error); + if (fw == NULL) { + g_autoptr(GError) error_local = NULL; + if (!fu_device_attach (device, &error_local)) + g_debug ("ignoring: %s", error_local->message); + return FALSE; + } + for (guint i = 0; checksum_types[i] != 0; i++) { + g_autofree gchar *hash = NULL; + hash = g_compute_checksum_for_bytes (checksum_types[i], fw); + fu_device_add_checksum (device, hash); + } + return fu_device_attach (device, error); +} + gboolean fu_plugin_runner_startup (FuPlugin *self, GError **error) { @@ -1581,8 +1610,10 @@ fu_plugin_runner_verify (FuPlugin *self, /* optional */ g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func); - if (func == NULL) - return TRUE; + if (func == NULL) { + g_debug ("running superclassed read_firmware() on %s", priv->name); + return fu_plugin_device_read_firmware (self, device, error); + } /* clear any existing verification checksums */ checksums = fu_device_get_checksums (device);