From 8cc41cfa6f9cdd39779594133bb71c9663ced77d Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Mon, 14 Oct 2019 19:19:50 +0100 Subject: [PATCH] dfu: Make DfuFirmware derive from FuFirmware --- plugins/dfu/dfu-device.c | 24 +-- plugins/dfu/dfu-firmware.c | 285 ++------------------------------- plugins/dfu/dfu-firmware.h | 24 +-- plugins/dfu/dfu-format-dfu.c | 25 +-- plugins/dfu/dfu-format-dfuse.c | 6 +- plugins/dfu/dfu-format-raw.c | 11 +- plugins/dfu/dfu-self-test.c | 65 ++++---- plugins/dfu/dfu-tool.c | 49 +++--- 8 files changed, 105 insertions(+), 384 deletions(-) diff --git a/plugins/dfu/dfu-device.c b/plugins/dfu/dfu-device.c index 95e885fd5..5e3ec41c7 100644 --- a/plugins/dfu/dfu-device.c +++ b/plugins/dfu/dfu-device.c @@ -1518,9 +1518,9 @@ dfu_device_upload (DfuDevice *device, /* create ahead of time */ firmware = dfu_firmware_new (); - dfu_firmware_set_vid (firmware, priv->runtime_vid); - dfu_firmware_set_pid (firmware, priv->runtime_pid); - dfu_firmware_set_release (firmware, 0xffff); + fu_dfu_firmware_set_vid (FU_DFU_FIRMWARE (firmware), priv->runtime_vid); + fu_dfu_firmware_set_pid (FU_DFU_FIRMWARE (firmware), priv->runtime_pid); + fu_dfu_firmware_set_release (FU_DFU_FIRMWARE (firmware), 0xffff); /* upload from each target */ for (guint i = 0; i < priv->targets->len; i++) { @@ -1551,7 +1551,7 @@ dfu_device_upload (DfuDevice *device, g_signal_handler_disconnect (target, id2); if (image == NULL) return NULL; - dfu_firmware_add_image (firmware, image); + fu_firmware_add_image (FU_FIRMWARE (firmware), FU_FIRMWARE_IMAGE (image)); } /* do not do the dummy upload for quirked devices */ @@ -1596,9 +1596,9 @@ dfu_device_download (DfuDevice *device, GError **error) { DfuDevicePrivate *priv = GET_PRIVATE (device); - GPtrArray *images; GUsbDevice *usb_device = fu_usb_device_get_dev (FU_USB_DEVICE (device)); gboolean ret; + g_autoptr(GPtrArray) images = NULL; /* no backing USB device */ if (usb_device == NULL) { @@ -1616,7 +1616,7 @@ dfu_device_download (DfuDevice *device, /* do we allow wildcard VID:PID matches */ if ((flags & DFU_TARGET_TRANSFER_FLAG_WILDCARD_VID) == 0) { - if (dfu_firmware_get_vid (firmware) == 0xffff) { + if (fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (firmware)) == 0xffff) { g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, @@ -1625,7 +1625,7 @@ dfu_device_download (DfuDevice *device, } } if ((flags & DFU_TARGET_TRANSFER_FLAG_WILDCARD_PID) == 0) { - if (dfu_firmware_get_pid (firmware) == 0xffff) { + if (fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (firmware)) == 0xffff) { g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, @@ -1636,7 +1636,7 @@ dfu_device_download (DfuDevice *device, /* check vendor matches */ if (priv->runtime_vid != 0xffff) { - if (!dfu_device_id_compatible (dfu_firmware_get_vid (firmware), + if (!dfu_device_id_compatible (fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (firmware)), priv->runtime_vid, fu_usb_device_get_vid (FU_USB_DEVICE (device)))) { g_set_error (error, @@ -1644,7 +1644,7 @@ dfu_device_download (DfuDevice *device, FWUPD_ERROR_NOT_SUPPORTED, "vendor ID incorrect, expected 0x%04x " "got 0x%04x and 0x%04x\n", - dfu_firmware_get_vid (firmware), + fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (firmware)), priv->runtime_vid, fu_usb_device_get_vid (FU_USB_DEVICE (device))); return FALSE; @@ -1653,7 +1653,7 @@ dfu_device_download (DfuDevice *device, /* check product matches */ if (priv->runtime_pid != 0xffff) { - if (!dfu_device_id_compatible (dfu_firmware_get_pid (firmware), + if (!dfu_device_id_compatible (fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (firmware)), priv->runtime_pid, fu_usb_device_get_pid (FU_USB_DEVICE (device)))) { g_set_error (error, @@ -1661,7 +1661,7 @@ dfu_device_download (DfuDevice *device, FWUPD_ERROR_NOT_SUPPORTED, "product ID incorrect, expected 0x%04x " "got 0x%04x and 0x%04x", - dfu_firmware_get_pid (firmware), + fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (firmware)), priv->runtime_pid, fu_usb_device_get_pid (FU_USB_DEVICE (device))); return FALSE; @@ -1669,7 +1669,7 @@ dfu_device_download (DfuDevice *device, } /* download each target */ - images = dfu_firmware_get_images (firmware); + images = fu_firmware_get_images (FU_FIRMWARE (firmware)); if (images->len == 0) { g_set_error_literal (error, FWUPD_ERROR, diff --git a/plugins/dfu/dfu-firmware.c b/plugins/dfu/dfu-firmware.c index 046cc2f42..1a760745e 100644 --- a/plugins/dfu/dfu-firmware.c +++ b/plugins/dfu/dfu-firmware.c @@ -33,44 +33,21 @@ #include "fwupd-error.h" -static void dfu_firmware_finalize (GObject *object); - typedef struct { - GPtrArray *images; - guint16 vid; - guint16 pid; - guint16 release; DfuFirmwareFormat format; } DfuFirmwarePrivate; -G_DEFINE_TYPE_WITH_PRIVATE (DfuFirmware, dfu_firmware, G_TYPE_OBJECT) +G_DEFINE_TYPE_WITH_PRIVATE (DfuFirmware, dfu_firmware, FU_TYPE_DFU_FIRMWARE) #define GET_PRIVATE(o) (dfu_firmware_get_instance_private (o)) -static void -dfu_firmware_class_init (DfuFirmwareClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - object_class->finalize = dfu_firmware_finalize; -} - static void dfu_firmware_init (DfuFirmware *firmware) { - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - priv->vid = 0xffff; - priv->pid = 0xffff; - priv->release = 0xffff; - priv->images = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); } static void dfu_firmware_finalize (GObject *object) { - DfuFirmware *firmware = DFU_FIRMWARE (object); - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - - g_ptr_array_unref (priv->images); - G_OBJECT_CLASS (dfu_firmware_parent_class)->finalize (object); } @@ -89,90 +66,6 @@ dfu_firmware_new (void) return firmware; } -/** - * dfu_firmware_get_image: - * @firmware: a #DfuFirmware - * @alt_setting: an alternative setting, typically 0x00 - * - * Gets an image from the firmware file. - * - * Return value: (transfer none): a #DfuImage, or %NULL for not found - **/ -DfuImage * -dfu_firmware_get_image (DfuFirmware *firmware, guint8 alt_setting) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - - g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), NULL); - - /* find correct image */ - for (guint i = 0; i < priv->images->len; i++) { - DfuImage *im = g_ptr_array_index (priv->images, i); - if (dfu_image_get_alt_setting (im) == alt_setting) - return im; - } - return NULL; -} - -/** - * dfu_firmware_get_image_by_name: - * @firmware: a #DfuFirmware - * @name: an alternative setting name - * - * Gets an image from the firmware file. - * - * Return value: (transfer none): a #DfuImage, or %NULL for not found - **/ -DfuImage * -dfu_firmware_get_image_by_name (DfuFirmware *firmware, const gchar *name) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - - g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), NULL); - - /* find correct image */ - for (guint i = 0; i < priv->images->len; i++) { - DfuImage *im = g_ptr_array_index (priv->images, i); - if (g_strcmp0 (dfu_image_get_name (im), name) == 0) - return im; - } - return NULL; -} - -/** - * dfu_firmware_get_image_default: - * @firmware: a #DfuFirmware - * - * Gets the default image from the firmware file. - * - * Return value: (transfer none): a #DfuImage, or %NULL for not found - **/ -DfuImage * -dfu_firmware_get_image_default (DfuFirmware *firmware) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), NULL); - if (priv->images->len == 0) - return NULL; - return g_ptr_array_index (priv->images, 0); -} - -/** - * dfu_firmware_get_images: - * @firmware: a #DfuFirmware - * - * Gets all the images contained in this firmware file. - * - * Return value: (transfer none) (element-type DfuImage): list of images - **/ -GPtrArray * -dfu_firmware_get_images (DfuFirmware *firmware) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), NULL); - return priv->images; -} - /** * dfu_firmware_get_size: * @firmware: a #DfuFirmware @@ -187,80 +80,16 @@ dfu_firmware_get_images (DfuFirmware *firmware) guint32 dfu_firmware_get_size (DfuFirmware *firmware) { - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); guint32 length = 0; + g_autoptr(GPtrArray) images = fu_firmware_get_images (FU_FIRMWARE (firmware)); g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), 0); - for (guint i = 0; i < priv->images->len; i++) { - DfuImage *image = g_ptr_array_index (priv->images, i); + for (guint i = 0; i < images->len; i++) { + DfuImage *image = g_ptr_array_index (images, i); length += dfu_image_get_size (image); } return length; } -/** - * dfu_firmware_add_image: - * @firmware: a #DfuFirmware - * @image: a #DfuImage - * - * Adds an image to the list of images. - **/ -void -dfu_firmware_add_image (DfuFirmware *firmware, DfuImage *image) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - g_return_if_fail (DFU_IS_FIRMWARE (firmware)); - g_return_if_fail (DFU_IS_IMAGE (image)); - g_ptr_array_add (priv->images, g_object_ref (image)); -} - -/** - * dfu_firmware_get_vid: - * @firmware: a #DfuFirmware - * - * Gets the vendor ID. - * - * Return value: a vendor ID, or 0xffff for unset - **/ -guint16 -dfu_firmware_get_vid (DfuFirmware *firmware) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), 0xffff); - return priv->vid; -} - -/** - * dfu_firmware_get_pid: - * @firmware: a #DfuFirmware - * - * Gets the product ID. - * - * Return value: a product ID, or 0xffff for unset - **/ -guint16 -dfu_firmware_get_pid (DfuFirmware *firmware) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), 0xffff); - return priv->pid; -} - -/** - * dfu_firmware_get_release: - * @firmware: a #DfuFirmware - * - * Gets the device ID. - * - * Return value: a device ID, or 0xffff for unset - **/ -guint16 -dfu_firmware_get_release (DfuFirmware *firmware) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), 0xffff); - return priv->release; -} - /** * dfu_firmware_get_format: * @firmware: a #DfuFirmware @@ -277,51 +106,6 @@ dfu_firmware_get_format (DfuFirmware *firmware) return priv->format; } -/** - * dfu_firmware_set_vid: - * @firmware: a #DfuFirmware - * @vid: vendor ID, or 0xffff for unset - * - * Sets the vendor ID. - **/ -void -dfu_firmware_set_vid (DfuFirmware *firmware, guint16 vid) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - g_return_if_fail (DFU_IS_FIRMWARE (firmware)); - priv->vid = vid; -} - -/** - * dfu_firmware_set_pid: - * @firmware: a #DfuFirmware - * @pid: product ID, or 0xffff for unset - * - * Sets the product ID. - **/ -void -dfu_firmware_set_pid (DfuFirmware *firmware, guint16 pid) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - g_return_if_fail (DFU_IS_FIRMWARE (firmware)); - priv->pid = pid; -} - -/** - * dfu_firmware_set_release: - * @firmware: a #DfuFirmware - * @release: device ID, or 0xffff for unset - * - * Sets the device ID. - **/ -void -dfu_firmware_set_release (DfuFirmware *firmware, guint16 release) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - g_return_if_fail (DFU_IS_FIRMWARE (firmware)); - priv->release = release; -} - /** * dfu_firmware_set_format: * @firmware: a #DfuFirmware @@ -358,11 +142,6 @@ dfu_firmware_parse_data (DfuFirmware *firmware, GBytes *bytes, g_return_val_if_fail (bytes != NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - /* set defaults */ - priv->vid = 0xffff; - priv->pid = 0xffff; - priv->release = 0xffff; - /* try to get format if not already set */ if (priv->format == DFU_FIRMWARE_FORMAT_UNKNOWN) priv->format = dfu_firmware_detect_dfu (bytes); @@ -419,9 +198,10 @@ static gboolean dfu_firmware_check_acceptable_for_format (DfuFirmware *firmware, GError **error) { DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); + g_autoptr(GPtrArray) images = fu_firmware_get_images (FU_FIRMWARE (firmware)); /* always okay */ - if (priv->images->len <= 1) + if (images->len <= 1) return TRUE; if (priv->format == DFU_FIRMWARE_FORMAT_DFUSE) return TRUE; @@ -431,7 +211,7 @@ dfu_firmware_check_acceptable_for_format (DfuFirmware *firmware, GError **error) FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "multiple images (%u) not supported for %s", - priv->images->len, + images->len, dfu_firmware_format_to_string (priv->format)); return TRUE; } @@ -449,12 +229,13 @@ GBytes * dfu_firmware_write_data (DfuFirmware *firmware, GError **error) { DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); + g_autoptr(GPtrArray) images = fu_firmware_get_images (FU_FIRMWARE (firmware)); g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); /* at least one image */ - if (priv->images == 0) { + if (images->len == 0) { g_set_error_literal (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, @@ -523,47 +304,6 @@ dfu_firmware_write_file (DfuFirmware *firmware, GFile *file, GError **error) error); } -/** - * dfu_firmware_to_string: - * @firmware: a #DfuFirmware - * - * Returns a string representaiton of the object. - * - * Return value: NULL terminated string, or %NULL for invalid - **/ -gchar * -dfu_firmware_to_string (DfuFirmware *firmware) -{ - DfuFirmwarePrivate *priv = GET_PRIVATE (firmware); - GString *str; - g_autofree gchar *release_str = NULL; - - g_return_val_if_fail (DFU_IS_FIRMWARE (firmware), NULL); - - release_str = fu_common_version_from_uint16 (priv->release, - FWUPD_VERSION_FORMAT_BCD); - str = g_string_new (""); - g_string_append_printf (str, "vid: 0x%04x\n", priv->vid); - g_string_append_printf (str, "pid: 0x%04x\n", priv->pid); - g_string_append_printf (str, "release: 0x%04x [%s]\n", - priv->release, release_str); - g_string_append_printf (str, "format: %s [0x%04x]\n", - dfu_firmware_format_to_string (priv->format), - priv->format); - - /* print images */ - for (guint i = 0; i < priv->images->len; i++) { - g_autofree gchar *tmp = NULL; - FuFirmwareImage *image = g_ptr_array_index (priv->images, i); - tmp = fu_firmware_image_to_string (image); - g_string_append_printf (str, "= IMAGE %u =\n", i); - g_string_append_printf (str, "%s\n", tmp); - } - - g_string_truncate (str, str->len - 1); - return g_string_free (str, FALSE); -} - /** * dfu_firmware_format_to_string: * @format: a #DfuFirmwareFormat, e.g. %DFU_FIRMWARE_FORMAT_DFU @@ -603,3 +343,10 @@ dfu_firmware_format_from_string (const gchar *format) return DFU_FIRMWARE_FORMAT_DFUSE; return DFU_FIRMWARE_FORMAT_UNKNOWN; } + +static void +dfu_firmware_class_init (DfuFirmwareClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + object_class->finalize = dfu_firmware_finalize; +} diff --git a/plugins/dfu/dfu-firmware.h b/plugins/dfu/dfu-firmware.h index 381645880..d8b4bd18f 100644 --- a/plugins/dfu/dfu-firmware.h +++ b/plugins/dfu/dfu-firmware.h @@ -9,17 +9,19 @@ #include #include +#include "fu-dfu-firmware.h" + #include "dfu-common.h" #include "dfu-image.h" #include "fwupd-enums.h" #define DFU_TYPE_FIRMWARE (dfu_firmware_get_type ()) -G_DECLARE_DERIVABLE_TYPE (DfuFirmware, dfu_firmware, DFU, FIRMWARE, GObject) +G_DECLARE_DERIVABLE_TYPE (DfuFirmware, dfu_firmware, DFU, FIRMWARE, FuDfuFirmware) struct _DfuFirmwareClass { - GObjectClass parent_class; + FuDfuFirmwareClass parent_class; }; /** @@ -45,26 +47,9 @@ DfuFirmware *dfu_firmware_new (void); const gchar *dfu_firmware_format_to_string (DfuFirmwareFormat format); DfuFirmwareFormat dfu_firmware_format_from_string(const gchar *format); -DfuImage *dfu_firmware_get_image (DfuFirmware *firmware, - guint8 alt_setting); -DfuImage *dfu_firmware_get_image_by_name (DfuFirmware *firmware, - const gchar *name); -DfuImage *dfu_firmware_get_image_default (DfuFirmware *firmware); -GPtrArray *dfu_firmware_get_images (DfuFirmware *firmware); -guint16 dfu_firmware_get_vid (DfuFirmware *firmware); -guint16 dfu_firmware_get_pid (DfuFirmware *firmware); -guint16 dfu_firmware_get_release (DfuFirmware *firmware); guint16 dfu_firmware_get_format (DfuFirmware *firmware); guint32 dfu_firmware_get_size (DfuFirmware *firmware); -void dfu_firmware_add_image (DfuFirmware *firmware, - DfuImage *image); -void dfu_firmware_set_vid (DfuFirmware *firmware, - guint16 vid); -void dfu_firmware_set_pid (DfuFirmware *firmware, - guint16 pid); -void dfu_firmware_set_release (DfuFirmware *firmware, - guint16 release); void dfu_firmware_set_format (DfuFirmware *firmware, DfuFirmwareFormat format); @@ -82,4 +67,3 @@ GBytes *dfu_firmware_write_data (DfuFirmware *firmware, gboolean dfu_firmware_write_file (DfuFirmware *firmware, GFile *file, GError **error); -gchar *dfu_firmware_to_string (DfuFirmware *firmware); diff --git a/plugins/dfu/dfu-format-dfu.c b/plugins/dfu/dfu-format-dfu.c index cdae5fcf1..940528f92 100644 --- a/plugins/dfu/dfu-format-dfu.c +++ b/plugins/dfu/dfu-format-dfu.c @@ -67,9 +67,12 @@ dfu_firmware_from_dfu (DfuFirmware *firmware, if (!fu_firmware_parse (native, bytes, flags, error)) return FALSE; - dfu_firmware_set_vid (firmware, fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (native))); - dfu_firmware_set_pid (firmware, fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (native))); - dfu_firmware_set_release (firmware, fu_dfu_firmware_get_release (FU_DFU_FIRMWARE (native))); + fu_dfu_firmware_set_vid (FU_DFU_FIRMWARE (firmware), + fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (native))); + fu_dfu_firmware_set_pid (FU_DFU_FIRMWARE (firmware), + fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (native))); + fu_dfu_firmware_set_release (FU_DFU_FIRMWARE (firmware), + fu_dfu_firmware_get_release (FU_DFU_FIRMWARE (native))); /* parse DfuSe prefix */ contents = fu_firmware_get_image_default_bytes (native, error); @@ -97,9 +100,12 @@ dfu_firmware_add_footer (DfuFirmware *firmware, GBytes *contents, GError **error { g_autoptr(FuFirmware) native = fu_dfu_firmware_new (); g_autoptr(FuFirmwareImage) image = fu_firmware_image_new (contents); - fu_dfu_firmware_set_vid (FU_DFU_FIRMWARE (native), dfu_firmware_get_vid (firmware)); - fu_dfu_firmware_set_pid (FU_DFU_FIRMWARE (native), dfu_firmware_get_pid (firmware)); - fu_dfu_firmware_set_release (FU_DFU_FIRMWARE (native), dfu_firmware_get_release (firmware)); + fu_dfu_firmware_set_vid (FU_DFU_FIRMWARE (native), + fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (firmware))); + fu_dfu_firmware_set_pid (FU_DFU_FIRMWARE (native), + fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (firmware))); + fu_dfu_firmware_set_release (FU_DFU_FIRMWARE (native), + fu_dfu_firmware_get_release (FU_DFU_FIRMWARE (firmware))); fu_dfu_firmware_set_version (FU_DFU_FIRMWARE (native), dfu_convert_version (dfu_firmware_get_format (firmware))); fu_firmware_add_image (native, image); @@ -121,9 +127,10 @@ dfu_firmware_to_dfu (DfuFirmware *firmware, GError **error) if (dfu_firmware_get_format (firmware) == DFU_FIRMWARE_FORMAT_DFU) { GBytes *contents; DfuElement *element; - DfuImage *image; - image = dfu_firmware_get_image_default (firmware); - g_assert (image != NULL); + g_autoptr(DfuImage) image = NULL; + image = DFU_IMAGE (fu_firmware_get_image_default (FU_FIRMWARE (firmware), error)); + if (image == NULL) + return NULL; element = dfu_image_get_element (image, 0); if (element == NULL) { g_set_error (error, diff --git a/plugins/dfu/dfu-format-dfuse.c b/plugins/dfu/dfu-format-dfuse.c index 19cf1b953..58edbd862 100644 --- a/plugins/dfu/dfu-format-dfuse.c +++ b/plugins/dfu/dfu-format-dfuse.c @@ -267,15 +267,15 @@ GBytes * dfu_firmware_to_dfuse (DfuFirmware *firmware, GError **error) { DfuSePrefix *prefix; - GPtrArray *images; guint32 image_size_total = 0; guint32 offset = sizeof (DfuSePrefix); g_autofree guint8 *buf = NULL; g_autoptr(GPtrArray) dfuse_images = NULL; + g_autoptr(GPtrArray) images = NULL; /* get all the image data */ dfuse_images = g_ptr_array_new_with_free_func ((GDestroyNotify) g_bytes_unref); - images = dfu_firmware_get_images (firmware); + images = fu_firmware_get_images (FU_FIRMWARE (firmware)); for (guint i = 0; i < images->len; i++) { DfuImage *im = g_ptr_array_index (images, i); GBytes *contents; @@ -384,7 +384,7 @@ dfu_firmware_from_dfuse (DfuFirmware *firmware, &consumed, error); if (image == NULL) return FALSE; - dfu_firmware_add_image (firmware, image); + fu_firmware_add_image (FU_FIRMWARE (firmware), FU_FIRMWARE_IMAGE (image)); offset += consumed; len -= consumed; } diff --git a/plugins/dfu/dfu-format-raw.c b/plugins/dfu/dfu-format-raw.c index d3d1e8f2b..a53968ce1 100644 --- a/plugins/dfu/dfu-format-raw.c +++ b/plugins/dfu/dfu-format-raw.c @@ -37,7 +37,7 @@ dfu_firmware_from_raw (DfuFirmware *firmware, element = dfu_element_new (); dfu_element_set_contents (element, bytes); dfu_image_add_element (image, element); - dfu_firmware_add_image (firmware, image); + fu_firmware_add_image (FU_FIRMWARE (firmware), FU_FIRMWARE_IMAGE (image)); return TRUE; } @@ -57,14 +57,9 @@ dfu_firmware_to_raw (DfuFirmware *firmware, GError **error) DfuImage *image; GBytes *contents; - image = dfu_firmware_get_image_default (firmware); - if (image == NULL) { - g_set_error_literal (error, - FWUPD_ERROR, - FWUPD_ERROR_NOT_FOUND, - "no firmware image data to write"); + image = DFU_IMAGE (fu_firmware_get_image_default (FU_FIRMWARE (firmware), error)); + if (image == NULL) return NULL; - } element = dfu_image_get_element (image, 0); if (element == NULL) { g_set_error_literal (error, diff --git a/plugins/dfu/dfu-self-test.c b/plugins/dfu/dfu-self-test.c index 870613d7d..289066d1b 100644 --- a/plugins/dfu/dfu-self-test.c +++ b/plugins/dfu/dfu-self-test.c @@ -56,11 +56,11 @@ static void dfu_firmware_raw_func (void) { DfuElement *element; - DfuImage *image_tmp; GBytes *no_suffix_contents; gchar buf[256]; gboolean ret; g_autoptr(DfuFirmware) firmware = NULL; + g_autoptr(DfuImage) image_tmp = NULL; g_autoptr(GBytes) fw = NULL; g_autoptr(GBytes) roundtrip = NULL; g_autoptr(GError) error = NULL; @@ -75,13 +75,13 @@ dfu_firmware_raw_func (void) ret = dfu_firmware_parse_data (firmware, fw, FWUPD_INSTALL_FLAG_NONE, &error); g_assert_no_error (error); g_assert (ret); - g_assert_cmpint (dfu_firmware_get_vid (firmware), ==, 0xffff); - g_assert_cmpint (dfu_firmware_get_pid (firmware), ==, 0xffff); - g_assert_cmpint (dfu_firmware_get_release (firmware), ==, 0xffff); + g_assert_cmpint (fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (firmware)), ==, 0xffff); + g_assert_cmpint (fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (firmware)), ==, 0xffff); + g_assert_cmpint (fu_dfu_firmware_get_release (FU_DFU_FIRMWARE (firmware)), ==, 0xffff); g_assert_cmpint (dfu_firmware_get_format (firmware), ==, DFU_FIRMWARE_FORMAT_RAW); - image_tmp = dfu_firmware_get_image (firmware, 0xfe); + image_tmp = DFU_IMAGE (fu_firmware_get_image_by_idx (FU_FIRMWARE (firmware), 0xfe, NULL)); g_assert (image_tmp == NULL); - image_tmp = dfu_firmware_get_image (firmware, 0); + image_tmp = DFU_IMAGE (fu_firmware_get_image_by_idx (FU_FIRMWARE (firmware), 0, NULL)); g_assert (image_tmp != NULL); g_assert_cmpint (dfu_image_get_size (image_tmp), ==, 256); element = dfu_image_get_element (image_tmp, 0); @@ -105,7 +105,9 @@ dfu_firmware_dfu_func (void) gchar buf[256]; gboolean ret; g_autofree gchar *filename = NULL; - g_autoptr(DfuFirmware) firmware = NULL; + g_autoptr(DfuFirmware) firmware1 = dfu_firmware_new (); + g_autoptr(DfuFirmware) firmware2 = dfu_firmware_new (); + g_autoptr(DfuFirmware) firmware3 = dfu_firmware_new (); g_autoptr(DfuImage) image = NULL; g_autoptr(DfuElement) element = NULL; g_autoptr(GBytes) data = NULL; @@ -121,53 +123,50 @@ dfu_firmware_dfu_func (void) fw = g_bytes_new_static (buf, 256); /* write DFU format */ - firmware = dfu_firmware_new (); - dfu_firmware_set_format (firmware, DFU_FIRMWARE_FORMAT_DFU); - dfu_firmware_set_vid (firmware, 0x1234); - dfu_firmware_set_pid (firmware, 0x5678); - dfu_firmware_set_release (firmware, 0xfedc); + dfu_firmware_set_format (firmware1, DFU_FIRMWARE_FORMAT_DFU); + fu_dfu_firmware_set_vid (FU_DFU_FIRMWARE (firmware1), 0x1234); + fu_dfu_firmware_set_pid (FU_DFU_FIRMWARE (firmware1), 0x5678); + fu_dfu_firmware_set_release (FU_DFU_FIRMWARE (firmware1), 0xfedc); image = dfu_image_new (); element = dfu_element_new (); dfu_element_set_contents (element, fw); dfu_image_add_element (image, element); - dfu_firmware_add_image (firmware, image); - g_assert_cmpint (dfu_firmware_get_size (firmware), ==, 256); - data = dfu_firmware_write_data (firmware, &error); + fu_firmware_add_image (FU_FIRMWARE (firmware1), FU_FIRMWARE_IMAGE (image)); + g_assert_cmpint (dfu_firmware_get_size (firmware1), ==, 256); + data = dfu_firmware_write_data (firmware1, &error); g_assert_no_error (error); g_assert (data != NULL); /* can we load it again? */ - g_ptr_array_set_size (dfu_firmware_get_images (firmware), 0); - ret = dfu_firmware_parse_data (firmware, data, FWUPD_INSTALL_FLAG_NONE, &error); + ret = dfu_firmware_parse_data (firmware2, data, FWUPD_INSTALL_FLAG_NONE, &error); g_assert_no_error (error); g_assert (ret); - g_assert_cmpint (dfu_firmware_get_vid (firmware), ==, 0x1234); - g_assert_cmpint (dfu_firmware_get_pid (firmware), ==, 0x5678); - g_assert_cmpint (dfu_firmware_get_release (firmware), ==, 0xfedc); - g_assert_cmpint (dfu_firmware_get_format (firmware), ==, DFU_FIRMWARE_FORMAT_DFU); - g_assert_cmpint (dfu_firmware_get_size (firmware), ==, 256); + g_assert_cmpint (fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (firmware2)), ==, 0x1234); + g_assert_cmpint (fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (firmware2)), ==, 0x5678); + g_assert_cmpint (fu_dfu_firmware_get_release (FU_DFU_FIRMWARE (firmware2)), ==, 0xfedc); + g_assert_cmpint (dfu_firmware_get_format (firmware2), ==, DFU_FIRMWARE_FORMAT_DFU); + g_assert_cmpint (dfu_firmware_get_size (firmware2), ==, 256); /* load a real firmware */ filename = dfu_test_get_filename ("kiibohd.dfu.bin"); g_assert (filename != NULL); file = g_file_new_for_path (filename); - g_ptr_array_set_size (dfu_firmware_get_images (firmware), 0); - ret = dfu_firmware_parse_file (firmware, file, + ret = dfu_firmware_parse_file (firmware3, file, FWUPD_INSTALL_FLAG_NONE, &error); g_assert_no_error (error); g_assert (ret); - g_assert_cmpint (dfu_firmware_get_vid (firmware), ==, 0x1c11); - g_assert_cmpint (dfu_firmware_get_pid (firmware), ==, 0xb007); - g_assert_cmpint (dfu_firmware_get_release (firmware), ==, 0xffff); - g_assert_cmpint (dfu_firmware_get_format (firmware), ==, DFU_FIRMWARE_FORMAT_DFU); - g_assert_cmpint (dfu_firmware_get_size (firmware), ==, 0x8eB4); + g_assert_cmpint (fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (firmware3)), ==, 0x1c11); + g_assert_cmpint (fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (firmware3)), ==, 0xb007); + g_assert_cmpint (fu_dfu_firmware_get_release (FU_DFU_FIRMWARE (firmware3)), ==, 0xffff); + g_assert_cmpint (dfu_firmware_get_format (firmware3), ==, DFU_FIRMWARE_FORMAT_DFU); + g_assert_cmpint (dfu_firmware_get_size (firmware3), ==, 0x8eB4); /* can we roundtrip without losing data */ roundtrip_orig = dfu_self_test_get_bytes_for_file (file, &error); g_assert_no_error (error); g_assert (roundtrip_orig != NULL); - roundtrip = dfu_firmware_write_data (firmware, &error); + roundtrip = dfu_firmware_write_data (firmware3, &error); g_assert_no_error (error); g_assert (roundtrip != NULL); ret = fu_common_bytes_compare (roundtrip, roundtrip_orig, &error); @@ -197,9 +196,9 @@ dfu_firmware_dfuse_func (void) &error); g_assert_no_error (error); g_assert (ret); - g_assert_cmpint (dfu_firmware_get_vid (firmware), ==, 0x0483); - g_assert_cmpint (dfu_firmware_get_pid (firmware), ==, 0x0000); - g_assert_cmpint (dfu_firmware_get_release (firmware), ==, 0x0000); + g_assert_cmpint (fu_dfu_firmware_get_vid (FU_DFU_FIRMWARE (firmware)), ==, 0x0483); + g_assert_cmpint (fu_dfu_firmware_get_pid (FU_DFU_FIRMWARE (firmware)), ==, 0x0000); + g_assert_cmpint (fu_dfu_firmware_get_release (FU_DFU_FIRMWARE (firmware)), ==, 0x0000); g_assert_cmpint (dfu_firmware_get_format (firmware), ==, DFU_FIRMWARE_FORMAT_DFUSE); g_assert_cmpint (dfu_firmware_get_size (firmware), ==, 0x168d5); diff --git a/plugins/dfu/dfu-tool.c b/plugins/dfu/dfu-tool.c index 5fe1ec5c9..ea639e1c9 100644 --- a/plugins/dfu/dfu-tool.c +++ b/plugins/dfu/dfu-tool.c @@ -297,7 +297,7 @@ dfu_tool_set_vendor (DfuToolPrivate *priv, gchar **values, GError **error) values[1]); return FALSE; } - dfu_firmware_set_vid (firmware, (guint16) tmp); + fu_dfu_firmware_set_vid (FU_DFU_FIRMWARE (firmware), (guint16) tmp); /* write out new file */ return dfu_firmware_write_file (firmware, file, error); @@ -338,7 +338,7 @@ dfu_tool_set_product (DfuToolPrivate *priv, gchar **values, GError **error) "Failed to parse PID '%s'", values[1]); return FALSE; } - dfu_firmware_set_pid (firmware, (guint16) tmp); + fu_dfu_firmware_set_pid (FU_DFU_FIRMWARE (firmware), (guint16) tmp); /* write out new file */ return dfu_firmware_write_file (firmware, file, error); @@ -416,7 +416,7 @@ dfu_tool_set_release (DfuToolPrivate *priv, gchar **values, GError **error) if (tmp == 0xffff) return FALSE; } - dfu_firmware_set_release (firmware, (guint16) tmp); + fu_dfu_firmware_set_release (FU_DFU_FIRMWARE (firmware), (guint16) tmp); /* write out new file */ return dfu_firmware_write_file (firmware, file, error); @@ -493,12 +493,12 @@ dfu_tool_bytes_replace (GBytes *data, GBytes *search, GBytes *replace) static gboolean dfu_tool_replace_data (DfuToolPrivate *priv, gchar **values, GError **error) { - GPtrArray *images; guint cnt = 0; g_autoptr(DfuFirmware) firmware = NULL; g_autoptr(GFile) file = NULL; g_autoptr(GBytes) data_search = NULL; g_autoptr(GBytes) data_replace = NULL; + g_autoptr(GPtrArray) images = NULL; /* check args */ if (g_strv_length (values) < 3) { @@ -535,7 +535,7 @@ dfu_tool_replace_data (DfuToolPrivate *priv, gchar **values, GError **error) } /* get each data segment */ - images = dfu_firmware_get_images (firmware); + images = fu_firmware_get_images (FU_FIRMWARE (firmware)); for (guint i = 0; i < images->len; i++) { DfuImage *image = g_ptr_array_index (images, i); GPtrArray *elements = dfu_image_get_elements (image); @@ -611,7 +611,7 @@ dfu_tool_convert (DfuToolPrivate *priv, gchar **values, GError **error) } /* print the new object */ - str_debug = dfu_firmware_to_string (firmware); + str_debug = fu_firmware_to_string (FU_FIRMWARE (firmware)); g_debug ("DFU: %s", str_debug); /* write out new file */ @@ -713,9 +713,9 @@ dfu_tool_read_alt (DfuToolPrivate *priv, gchar **values, GError **error) /* create new firmware object */ firmware = dfu_firmware_new (); dfu_firmware_set_format (firmware, DFU_FIRMWARE_FORMAT_DFU); - dfu_firmware_set_vid (firmware, dfu_device_get_runtime_vid (device)); - dfu_firmware_set_pid (firmware, dfu_device_get_runtime_pid (device)); - dfu_firmware_add_image (firmware, image); + fu_dfu_firmware_set_vid (FU_DFU_FIRMWARE (firmware), dfu_device_get_runtime_vid (device)); + fu_dfu_firmware_set_pid (FU_DFU_FIRMWARE (firmware), dfu_device_get_runtime_pid (device)); + fu_firmware_add_image (FU_FIRMWARE (firmware), FU_FIRMWARE_IMAGE (image)); /* save file */ file = g_file_new_for_path (values[0]); @@ -723,7 +723,7 @@ dfu_tool_read_alt (DfuToolPrivate *priv, gchar **values, GError **error) return FALSE; /* print the new object */ - str_debug = dfu_firmware_to_string (firmware); + str_debug = fu_firmware_to_string (FU_FIRMWARE (firmware)); g_debug ("DFU: %s", str_debug); /* success */ @@ -825,7 +825,7 @@ dfu_tool_read (DfuToolPrivate *priv, gchar **values, GError **error) return FALSE; /* print the new object */ - str_debug = dfu_firmware_to_string (firmware); + str_debug = fu_firmware_to_string (FU_FIRMWARE (firmware)); g_debug ("DFU: %s", str_debug); /* success */ @@ -974,7 +974,7 @@ dfu_tool_dump (DfuToolPrivate *priv, gchar **values, GError **error) error_local->message); continue; } - tmp = dfu_firmware_to_string (firmware); + tmp = fu_firmware_to_string (FU_FIRMWARE (firmware)); g_print ("%s\n", tmp); } return TRUE; @@ -983,11 +983,11 @@ dfu_tool_dump (DfuToolPrivate *priv, gchar **values, GError **error) static gboolean dfu_tool_write_alt (DfuToolPrivate *priv, gchar **values, GError **error) { - DfuImage *image; DfuTargetTransferFlags flags = DFU_TARGET_TRANSFER_FLAG_VERIFY; g_autofree gchar *str_debug = NULL; g_autoptr(DfuDevice) device = NULL; g_autoptr(DfuFirmware) firmware = NULL; + g_autoptr(DfuImage) image = NULL; g_autoptr(DfuTarget) target = NULL; g_autoptr(FuDeviceLocker) locker = NULL; g_autoptr(GFile) file = NULL; @@ -1039,7 +1039,7 @@ dfu_tool_write_alt (DfuToolPrivate *priv, gchar **values, GError **error) } /* print the new object */ - str_debug = dfu_firmware_to_string (firmware); + str_debug = fu_firmware_to_string (FU_FIRMWARE (firmware)); g_debug ("DFU: %s", str_debug); /* get correct target on device */ @@ -1066,7 +1066,7 @@ dfu_tool_write_alt (DfuToolPrivate *priv, gchar **values, GError **error) /* allow overriding the firmware alt-setting */ if (g_strv_length (values) > 2) { - image = dfu_firmware_get_image_by_name (firmware, values[2]); + image = DFU_IMAGE (fu_firmware_get_image_by_id (FU_FIRMWARE (firmware), values[2], NULL)); if (image == NULL) { gchar *endptr; guint64 tmp = g_ascii_strtoull (values[2], &endptr, 10); @@ -1078,26 +1078,15 @@ dfu_tool_write_alt (DfuToolPrivate *priv, gchar **values, GError **error) values[2]); return FALSE; } - image = dfu_firmware_get_image (firmware, (guint8) tmp); - if (image == NULL) { - g_set_error (error, - FWUPD_ERROR, - FWUPD_ERROR_INVALID_FILE, - "could not locate image in firmware for %02x", - (guint) tmp); + image = DFU_IMAGE (fu_firmware_get_image_by_idx (FU_FIRMWARE (firmware), tmp, error)); + if (image == NULL) return FALSE; - } } } else { g_print ("WARNING: Using default firmware image\n"); - image = dfu_firmware_get_image_default (firmware); - if (image == NULL) { - g_set_error_literal (error, - FWUPD_ERROR, - FWUPD_ERROR_INVALID_FILE, - "no default image"); + image = DFU_IMAGE (fu_firmware_get_image_default (FU_FIRMWARE (firmware), error)); + if (image == NULL) return FALSE; - } } /* transfer */