trivial: Add a write_firmware() vfunc to FuDevice

This commit is contained in:
Richard Hughes 2018-05-03 10:17:15 +01:00
parent 12040b51fe
commit 0a0483b2ce
21 changed files with 114 additions and 93 deletions

View File

@ -109,18 +109,6 @@ fu_altos_device_finalize (GObject *object)
G_OBJECT_CLASS (fu_altos_device_parent_class)->finalize (object);
}
static void
fu_altos_device_init (FuAltosDevice *device)
{
}
static void
fu_altos_device_class_init (FuAltosDeviceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = fu_altos_device_finalize;
}
FuAltosDeviceKind
fu_altos_device_get_kind (FuAltosDevice *device)
{
@ -432,13 +420,11 @@ fu_altos_device_write_page (FuAltosDevice *device,
return TRUE;
}
gboolean
fu_altos_device_write_firmware (FuAltosDevice *device,
GBytes *fw,
FuAltosDeviceWriteFirmwareFlag flags,
GError **error)
static gboolean
fu_altos_device_write_firmware (FuDevice *device, GBytes *fw, GError **error)
{
FuAltosDevicePrivate *priv = GET_PRIVATE (device);
FuAltosDevice *self = FU_ALTOS_DEVICE (device);
FuAltosDevicePrivate *priv = GET_PRIVATE (self);
GBytes *fw_blob;
const gchar *data;
const gsize data_len;
@ -525,7 +511,7 @@ fu_altos_device_write_firmware (FuAltosDevice *device,
}
/* verify data from device */
if (!fu_altos_device_write_page (device,
if (!fu_altos_device_write_page (self,
priv->addr_base + i,
buf_tmp,
0x100,
@ -533,7 +519,7 @@ fu_altos_device_write_firmware (FuAltosDevice *device,
return FALSE;
/* verify data written on device */
str = fu_altos_device_read_page (device,
str = fu_altos_device_read_page (self,
priv->addr_base + i,
error);
if (str == NULL)
@ -557,18 +543,16 @@ fu_altos_device_write_firmware (FuAltosDevice *device,
}
/* progress */
fu_device_set_progress_full (FU_DEVICE (device), i, flash_len);
fu_device_set_progress_full (device, i, flash_len);
g_string_append_len (buf, str->str, str->len);
}
/* go to application mode */
if (flags & FU_ALTOS_DEVICE_WRITE_FIRMWARE_FLAG_REBOOT) {
if (!fu_altos_device_tty_write (device, "a\n", -1, error))
return FALSE;
}
if (!fu_altos_device_tty_write (self, "a\n", -1, error))
return FALSE;
/* progress complete */
fu_device_set_progress_full (FU_DEVICE (device), flash_len, flash_len);
fu_device_set_progress_full (device, flash_len, flash_len);
/* success */
return TRUE;
@ -784,6 +768,20 @@ fu_altos_device_init_real (FuAltosDevice *device)
}
}
static void
fu_altos_device_init (FuAltosDevice *device)
{
}
static void
fu_altos_device_class_init (FuAltosDeviceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
FuDeviceClass *klass_device = FU_DEVICE_CLASS (klass);
klass_device->write_firmware = fu_altos_device_write_firmware;
object_class->finalize = fu_altos_device_finalize;
}
typedef struct {
guint16 vid;
guint16 pid;

View File

@ -58,10 +58,6 @@ const gchar *fu_altos_device_kind_to_string (FuAltosDeviceKind kind);
FuAltosDeviceKind fu_altos_device_get_kind (FuAltosDevice *device);
gboolean fu_altos_device_probe (FuAltosDevice *device,
GError **error);
gboolean fu_altos_device_write_firmware (FuAltosDevice *device,
GBytes *fw,
FuAltosDeviceWriteFirmwareFlag flags,
GError **error);
GBytes *fu_altos_device_read_firmware (FuAltosDevice *device,
GError **error);

View File

@ -92,9 +92,7 @@ main (int argc, char **argv)
fw = g_bytes_new (data, len);
g_signal_connect (dev, "notify::progress",
G_CALLBACK (fu_altos_tool_progress_cb), NULL);
if (!fu_altos_device_write_firmware (dev, fw,
FU_ALTOS_DEVICE_WRITE_FIRMWARE_FLAG_NONE,
&error)) {
if (!fu_device_write_firmware (FU_DEVICE (dev), fw, &error)) {
g_print ("Failed to write firmware: %s\n", error->message);
return 1;
}

View File

@ -98,11 +98,5 @@ fu_plugin_update (FuPlugin *plugin,
GError **error)
{
fu_device_set_status (dev, FWUPD_STATUS_DEVICE_WRITE);
if (!fu_altos_device_write_firmware (FU_ALTOS_DEVICE (dev),
blob_fw,
FU_ALTOS_DEVICE_WRITE_FIRMWARE_FLAG_REBOOT,
error)) {
return FALSE;
}
return TRUE;
return fu_device_write_firmware (dev, blob_fw, error);
}

View File

@ -271,15 +271,16 @@ fu_colorhug_device_verify_firmware (FuColorhugDevice *device, GError **error)
return TRUE;
}
gboolean
fu_colorhug_device_write_firmware (FuColorhugDevice *device, GBytes *fw, GError **error)
static gboolean
fu_colorhug_device_write_firmware (FuDevice *device, GBytes *fw, GError **error)
{
FuColorhugDevicePrivate *priv = GET_PRIVATE (device);
FuColorhugDevice *self = FU_COLORHUG_DEVICE (device);
FuColorhugDevicePrivate *priv = GET_PRIVATE (self);
GUsbDevice *usb_device = fu_usb_device_get_dev (FU_USB_DEVICE (device));
g_autoptr(GError) error_local = NULL;
/* write firmware */
fu_device_set_status (FU_DEVICE (device), FWUPD_STATUS_DEVICE_WRITE);
fu_device_set_status (device, FWUPD_STATUS_DEVICE_WRITE);
ch_device_queue_set_flash_success (priv->device_queue,
usb_device,
0x00);
@ -298,7 +299,7 @@ fu_colorhug_device_write_firmware (FuColorhugDevice *device, GBytes *fw, GError
}
/* verify firmware */
fu_device_set_status (FU_DEVICE (device), FWUPD_STATUS_DEVICE_VERIFY);
fu_device_set_status (device, FWUPD_STATUS_DEVICE_VERIFY);
ch_device_queue_verify_firmware (priv->device_queue, usb_device,
g_bytes_get_data (fw, NULL),
g_bytes_get_size (fw));
@ -330,7 +331,9 @@ static void
fu_colorhug_device_class_init (FuColorhugDeviceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
FuDeviceClass *klass_device = FU_DEVICE_CLASS (klass);
FuUsbDeviceClass *klass_usb_device = FU_USB_DEVICE_CLASS (klass);
klass_device->write_firmware = fu_colorhug_device_write_firmware;
object_class->finalize = fu_colorhug_device_finalize;
klass_usb_device->open = fu_colorhug_device_open;
klass_usb_device->probe = fu_colorhug_device_probe;

View File

@ -47,9 +47,6 @@ gboolean fu_colorhug_device_attach (FuColorhugDevice *device,
GError **error);
gboolean fu_colorhug_device_set_flash_success (FuColorhugDevice *device,
GError **error);
gboolean fu_colorhug_device_write_firmware (FuColorhugDevice *device,
GBytes *fw,
GError **error);
gboolean fu_colorhug_device_verify_firmware (FuColorhugDevice *device,
GError **error);

View File

@ -163,7 +163,7 @@ fu_plugin_update (FuPlugin *plugin,
locker = fu_device_locker_new (device, error);
if (locker == NULL)
return FALSE;
return fu_colorhug_device_write_firmware (colorhug_dev, blob_fw, error);
return fu_device_write_firmware (FU_DEVICE (colorhug_dev), blob_fw, error);
}
gboolean

View File

@ -1,6 +1,6 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2017 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2017-2018 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
@ -449,9 +449,10 @@ _dfu_firmware_get_default_element_data (DfuFirmware *firmware)
return dfu_element_get_contents (element);
}
gboolean
fu_csr_device_download (FuCsrDevice *self, GBytes *blob, GError **error)
static gboolean
fu_csr_device_download (FuDevice *device, GBytes *blob, GError **error)
{
FuCsrDevice *self = FU_CSR_DEVICE (device);
GBytes *blob_noftr;
const guint8 *data;
gsize sz = 0;
@ -461,7 +462,7 @@ fu_csr_device_download (FuCsrDevice *self, GBytes *blob, GError **error)
g_autoptr(GPtrArray) packets = NULL;
/* notify UI */
fu_device_set_status (FU_DEVICE (self), FWUPD_STATUS_DEVICE_WRITE);
fu_device_set_status (device, FWUPD_STATUS_DEVICE_WRITE);
/* parse the file */
if (!dfu_firmware_parse_data (dfu_firmware, blob,
@ -505,7 +506,7 @@ fu_csr_device_download (FuCsrDevice *self, GBytes *blob, GError **error)
return FALSE;
/* update progress */
fu_device_set_progress_full (FU_DEVICE (self),
fu_device_set_progress_full (device,
(gsize) idx, (gsize) packets->len);
}
@ -515,7 +516,7 @@ fu_csr_device_download (FuCsrDevice *self, GBytes *blob, GError **error)
return FALSE;
/* notify UI */
fu_device_set_status (FU_DEVICE (self), FWUPD_STATUS_IDLE);
fu_device_set_status (device, FWUPD_STATUS_IDLE);
return TRUE;
}
@ -594,6 +595,7 @@ fu_csr_device_class_init (FuCsrDeviceClass *klass)
FuDeviceClass *klass_device = FU_DEVICE_CLASS (klass);
FuUsbDeviceClass *klass_usb_device = FU_USB_DEVICE_CLASS (klass);
klass_device->to_string = fu_csr_device_to_string;
klass_device->write_firmware = fu_csr_device_download;
klass_usb_device->open = fu_csr_device_open;
klass_usb_device->close = fu_csr_device_close;
klass_usb_device->probe = fu_csr_device_probe;

View File

@ -1,6 +1,6 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2017 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2017-2018 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
@ -41,9 +41,6 @@ typedef enum {
FuCsrDevice *fu_csr_device_new (GUsbDevice *usb_device);
gboolean fu_csr_device_attach (FuCsrDevice *self,
GError **error);
gboolean fu_csr_device_download (FuCsrDevice *self,
GBytes *blob,
GError **error);
GBytes *fu_csr_device_upload (FuCsrDevice *self,
GError **error);
void fu_csr_device_set_quirks (FuCsrDevice *self,

View File

@ -296,7 +296,7 @@ fu_csr_tool_write (FuCsrToolPrivate *priv, gchar **values, GError **error)
G_CALLBACK (fu_csr_tool_progress_cb), priv);
g_signal_connect (device, "notify::progress",
G_CALLBACK (fu_csr_tool_progress_cb), priv);
return fu_csr_device_download (device, blob, error);
return fu_device_write_firmware (FU_DEVICE (device), blob, error);
}
static gboolean

View File

@ -74,7 +74,7 @@ fu_plugin_update (FuPlugin *plugin, FuDevice *device, GBytes *blob_fw,
locker = fu_device_locker_new (device, error);
if (locker == NULL)
return FALSE;
if (!fu_csr_device_download (FU_CSR_DEVICE (device), blob_fw, error))
if (!fu_device_write_firmware (device, blob_fw, error))
return FALSE;
return fu_csr_device_attach (FU_CSR_DEVICE (device), error);
}

View File

@ -367,10 +367,11 @@ fu_ebitdo_device_get_serial (FuEbitdoDevice *device)
return priv->serial;
}
gboolean
fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **error)
static gboolean
fu_ebitdo_device_write_firmware (FuDevice *device, GBytes *fw, GError **error)
{
FuEbitdoDevicePrivate *priv = GET_PRIVATE (device);
FuEbitdoDevice *self = FU_EBITDO_DEVICE (device);
FuEbitdoDevicePrivate *priv = GET_PRIVATE (self);
FuEbitdoFirmwareHeader *hdr;
const guint8 *payload_data;
const guint chunk_sz = 32;
@ -422,8 +423,8 @@ fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **er
}
/* set up the firmware header */
fu_device_set_status (FU_DEVICE (device), FWUPD_STATUS_DEVICE_WRITE);
if (!fu_ebitdo_device_send (device,
fu_device_set_status (device, FWUPD_STATUS_DEVICE_WRITE);
if (!fu_ebitdo_device_send (self,
FU_EBITDO_PKT_TYPE_USER_CMD,
FU_EBITDO_PKT_CMD_UPDATE_FIRMWARE_DATA,
FU_EBITDO_PKT_CMD_FW_UPDATE_HEADER,
@ -436,7 +437,7 @@ fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **er
error_local->message);
return FALSE;
}
if (!fu_ebitdo_device_receive (device, NULL, 0, &error_local)) {
if (!fu_ebitdo_device_receive (self, NULL, 0, &error_local)) {
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
@ -453,8 +454,8 @@ fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **er
g_debug ("writing %u bytes to 0x%04x of 0x%04x",
chunk_sz, offset, payload_len);
}
fu_device_set_progress_full (FU_DEVICE (device), offset, payload_len);
if (!fu_ebitdo_device_send (device,
fu_device_set_progress_full (device, offset, payload_len);
if (!fu_ebitdo_device_send (self,
FU_EBITDO_PKT_TYPE_USER_CMD,
FU_EBITDO_PKT_CMD_UPDATE_FIRMWARE_DATA,
FU_EBITDO_PKT_CMD_FW_UPDATE_DATA,
@ -467,7 +468,7 @@ fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **er
offset, error_local->message);
return FALSE;
}
if (!fu_ebitdo_device_receive (device, NULL, 0, &error_local)) {
if (!fu_ebitdo_device_receive (self, NULL, 0, &error_local)) {
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
@ -478,7 +479,7 @@ fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **er
}
/* mark as complete */
fu_device_set_progress_full (FU_DEVICE (device), payload_len, payload_len);
fu_device_set_progress_full (device, payload_len, payload_len);
/* set the "encode id" which is likely a checksum, bluetooth pairing
* or maybe just security-through-obscurity -- also note:
@ -486,7 +487,7 @@ fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **er
serial_new[0] = priv->serial[0] ^ app_key_index[priv->serial[0] & 0x0f];
serial_new[1] = priv->serial[1] ^ app_key_index[priv->serial[1] & 0x0f];
serial_new[2] = priv->serial[2] ^ app_key_index[priv->serial[2] & 0x0f];
if (!fu_ebitdo_device_send (device,
if (!fu_ebitdo_device_send (self,
FU_EBITDO_PKT_TYPE_USER_CMD,
FU_EBITDO_PKT_CMD_UPDATE_FIRMWARE_DATA,
FU_EBITDO_PKT_CMD_FW_SET_ENCODE_ID,
@ -502,7 +503,7 @@ fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **er
}
/* mark flash as successful */
if (!fu_ebitdo_device_send (device,
if (!fu_ebitdo_device_send (self,
FU_EBITDO_PKT_TYPE_USER_CMD,
FU_EBITDO_PKT_CMD_UPDATE_FIRMWARE_DATA,
FU_EBITDO_PKT_CMD_FW_UPDATE_OK,
@ -515,7 +516,7 @@ fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **er
error_local->message);
return FALSE;
}
if (!fu_ebitdo_device_receive (device, NULL, 0, &error_local)) {
if (!fu_ebitdo_device_receive (self, NULL, 0, &error_local)) {
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
@ -525,7 +526,7 @@ fu_ebitdo_device_write_firmware (FuEbitdoDevice *device, GBytes *fw, GError **er
}
/* success! */
fu_device_set_status (FU_DEVICE (device), FWUPD_STATUS_IDLE);
fu_device_set_status (device, FWUPD_STATUS_IDLE);
return TRUE;
}
@ -582,7 +583,9 @@ fu_ebitdo_device_init (FuEbitdoDevice *device)
static void
fu_ebitdo_device_class_init (FuEbitdoDeviceClass *klass)
{
FuDeviceClass *klass_device = FU_DEVICE_CLASS (klass);
FuUsbDeviceClass *klass_usb_device = FU_USB_DEVICE_CLASS (klass);
klass_device->write_firmware = fu_ebitdo_device_write_firmware;
klass_usb_device->open = fu_ebitdo_device_open;
klass_usb_device->probe = fu_ebitdo_device_probe;
}

View File

@ -43,11 +43,6 @@ FuEbitdoDevice *fu_ebitdo_device_new (GUsbDevice *usb_device);
gboolean fu_ebitdo_device_is_bootloader (FuEbitdoDevice *device);
const guint32 *fu_ebitdo_device_get_serial (FuEbitdoDevice *device);
/* object methods */
gboolean fu_ebitdo_device_write_firmware (FuEbitdoDevice *device,
GBytes *fw,
GError **error);
G_END_DECLS
#endif /* __FU_EBITDO_DEVICE_H */

View File

@ -142,7 +142,7 @@ main (int argc, char **argv)
fw = g_bytes_new (data, len);
g_signal_connect (dev, "notify::progress",
G_CALLBACK (fu_ebitdo_tool_progress_cb), NULL);
if (!fu_ebitdo_device_write_firmware (dev, fw, &error)) {
if (!fu_device_write_firmware (FU_DEVICE (dev), fw, &error)) {
g_print ("Failed to write firmware: %s\n", error->message);
return 1;
}

View File

@ -69,7 +69,7 @@ fu_plugin_update (FuPlugin *plugin,
locker = fu_device_locker_new (ebitdo_dev, error);
if (locker == NULL)
return FALSE;
if (!fu_ebitdo_device_write_firmware (ebitdo_dev, blob_fw, error))
if (!fu_device_write_firmware (FU_DEVICE (ebitdo_dev), blob_fw, error))
return FALSE;
/* when doing a soft-reboot the device does not re-enumerate properly

View File

@ -186,7 +186,7 @@ fu_plugin_update (FuPlugin *plugin,
/* write the firmware */
fu_device_set_status (dev, FWUPD_STATUS_DEVICE_WRITE);
if (!lu_device_write_firmware (device, blob_fw, error))
if (!fu_device_write_firmware (dev, blob_fw, error))
return FALSE;
/* success */

View File

@ -903,10 +903,11 @@ lu_device_attach (LuDevice *device, GError **error)
return TRUE;
}
gboolean
lu_device_write_firmware (LuDevice *device, GBytes *fw, GError **error)
static gboolean
lu_device_write_firmware (FuDevice *device, GBytes *fw, GError **error)
{
LuDeviceClass *klass = LU_DEVICE_GET_CLASS (device);
LuDevice *self = LU_DEVICE (device);
LuDeviceClass *klass = LU_DEVICE_GET_CLASS (self);
g_return_val_if_fail (LU_IS_DEVICE (device), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
@ -926,12 +927,12 @@ lu_device_write_firmware (LuDevice *device, GBytes *fw, GError **error)
G_IO_ERROR,
G_IO_ERROR_FAILED,
"not supported in %s",
lu_device_kind_to_string (lu_device_get_kind (device)));
lu_device_kind_to_string (lu_device_get_kind (self)));
return FALSE;
}
/* call either nordic or texas vfunc */
return klass->write_firmware (device, fw, error);
return klass->write_firmware (self, fw, error);
}
#ifndef HAVE_GUDEV_232
@ -1076,6 +1077,7 @@ lu_device_class_init (LuDeviceClass *klass)
object_class->get_property = lu_device_get_property;
object_class->set_property = lu_device_set_property;
klass_device->to_string = lu_device_to_string;
klass_device->write_firmware = lu_device_write_firmware;
pspec = g_param_spec_uint ("kind", NULL, NULL,
LU_DEVICE_KIND_UNKNOWN,

View File

@ -130,9 +130,6 @@ gboolean lu_device_probe (LuDevice *device,
GError **error);
gboolean lu_device_poll (LuDevice *device,
GError **error);
gboolean lu_device_write_firmware (LuDevice *device,
GBytes *fw,
GError **error);
gboolean lu_device_hidpp_send (LuDevice *device,
LuHidppMsg *msg,
guint timeout,

View File

@ -351,7 +351,7 @@ lu_tool_write (FuLuToolPrivate *priv, gchar **values, GError **error)
fw = g_bytes_new (data, len);
g_signal_connect (device, "notify::progress",
G_CALLBACK (lu_write_progress_cb), NULL);
if (!lu_device_write_firmware (device, fw, error))
if (!fu_device_write_firmware (FU_DEVICE (device), fw, error))
return FALSE;
/* detach back into runtime */

View File

@ -961,6 +961,39 @@ fu_device_get_release_default (FuDevice *device)
return rel;
}
/**
* fu_device_write_firmware:
* @device: A #FuDevice
* @fw: A #GBytes
* @error: A #GError
*
* Writes firmware to the device by calling a plugin-specific vfunc.
*
* Returns: %TRUE on success
*
* Since: 1.0.8
**/
gboolean
fu_device_write_firmware (FuDevice *device, GBytes *fw, GError **error)
{
FuDeviceClass *klass = FU_DEVICE_GET_CLASS (device);
g_return_val_if_fail (FU_IS_DEVICE (device), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
/* no plugin-specific method */
if (klass->write_firmware == NULL) {
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"not supported");
return FALSE;
}
/* call vfunc */
return klass->write_firmware (device, fw, error);
}
static void
fu_device_class_init (FuDeviceClass *klass)
{

View File

@ -37,8 +37,11 @@ struct _FuDeviceClass
FwupdDeviceClass parent_class;
void (*to_string) (FuDevice *device,
GString *str);
gboolean (*write_firmware) (FuDevice *device,
GBytes *fw,
GError **error);
/*< private >*/
gpointer padding[30];
gpointer padding[29];
};
/**
@ -161,6 +164,9 @@ void fu_device_set_quirks (FuDevice *device,
FuQuirks *quirks);
FuQuirks *fu_device_get_quirks (FuDevice *device);
FwupdRelease *fu_device_get_release_default (FuDevice *device);
gboolean fu_device_write_firmware (FuDevice *device,
GBytes *fw,
GError **error);
G_END_DECLS