Reduce more boilerplate in plugins

This commit is contained in:
Richard Hughes 2019-10-05 16:19:40 +01:00
parent d6fc172ec9
commit 7f67721432
6 changed files with 54 additions and 124 deletions

View File

@ -57,28 +57,3 @@ fu_plugin_usb_device_added (FuPlugin *plugin, FuUsbDevice *device, GError **erro
fu_plugin_device_add (plugin, FU_DEVICE (dev)); fu_plugin_device_add (plugin, FU_DEVICE (dev));
return TRUE; 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;
}

View File

@ -30,29 +30,3 @@ fu_plugin_usb_device_added (FuPlugin *plugin, FuUsbDevice *device, GError **erro
fu_plugin_device_add (plugin, FU_DEVICE (dev)); fu_plugin_device_add (plugin, FU_DEVICE (dev));
return TRUE; 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;
}

View File

@ -2027,6 +2027,26 @@ dfu_device_get_quirks_as_string (DfuDevice *device)
return g_string_free (str, FALSE); 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 static gboolean
dfu_device_set_quirk_kv (FuDevice *device, dfu_device_set_quirk_kv (FuDevice *device,
const gchar *key, const gchar *key,
@ -2125,6 +2145,7 @@ dfu_device_class_init (DfuDeviceClass *klass)
FuDeviceClass *klass_device = FU_DEVICE_CLASS (klass); FuDeviceClass *klass_device = FU_DEVICE_CLASS (klass);
FuUsbDeviceClass *klass_usb_device = FU_USB_DEVICE_CLASS (klass); FuUsbDeviceClass *klass_usb_device = FU_USB_DEVICE_CLASS (klass);
klass_device->set_quirk_kv = dfu_device_set_quirk_kv; 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->open = dfu_device_open;
klass_usb_device->close = dfu_device_close; klass_usb_device->close = dfu_device_close;
klass_usb_device->probe = dfu_device_probe; klass_usb_device->probe = dfu_device_probe;

View File

@ -157,48 +157,3 @@ fu_plugin_update (FuPlugin *plugin,
/* success */ /* success */
return TRUE; 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;
}

View File

@ -106,29 +106,3 @@ fu_plugin_coldplug (FuPlugin *plugin, GError **error)
} }
return TRUE; 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;
}

View File

@ -901,6 +901,35 @@ fu_plugin_device_write_firmware (FuPlugin *self, FuDevice *device,
return fu_device_write_firmware (device, fw, flags, error); 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 gboolean
fu_plugin_runner_startup (FuPlugin *self, GError **error) fu_plugin_runner_startup (FuPlugin *self, GError **error)
{ {
@ -1581,8 +1610,10 @@ fu_plugin_runner_verify (FuPlugin *self,
/* optional */ /* optional */
g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func); g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
if (func == NULL) if (func == NULL) {
return TRUE; g_debug ("running superclassed read_firmware() on %s", priv->name);
return fu_plugin_device_read_firmware (self, device, error);
}
/* clear any existing verification checksums */ /* clear any existing verification checksums */
checksums = fu_device_get_checksums (device); checksums = fu_device_get_checksums (device);