mirror of
https://git.proxmox.com/git/fwupd
synced 2025-08-14 04:00:41 +00:00
Reduce more boilerplate in plugins
This commit is contained in:
parent
d6fc172ec9
commit
7f67721432
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user