diff --git a/libfwupdplugin/fu-common.c b/libfwupdplugin/fu-common.c index b714b98ed..580e5271b 100644 --- a/libfwupdplugin/fu-common.c +++ b/libfwupdplugin/fu-common.c @@ -3425,6 +3425,206 @@ fu_common_crc32(const guint8 *buf, gsize bufsz) return fu_common_crc32_full(buf, bufsz, 0xFFFFFFFF, 0xEDB88320); } +/** + * fu_common_sum8: + * @buf: memory buffer + * @bufsz: size of @buf + * + * Returns the arithmetic sum of all bytes in @buf. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint8 +fu_common_sum8(const guint8 *buf, gsize bufsz) +{ + guint8 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT8); + for (gsize i = 0; i < bufsz; i++) + checksum += buf[i]; + return checksum; +} + +/** + * fu_common_sum8_bytes: + * @blob: a #GBytes + * + * Returns the arithmetic sum of all bytes in @blob. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint8 +fu_common_sum8_bytes(GBytes *blob) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT8); + return fu_common_sum8(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob)); +} + +/** + * fu_common_sum16: + * @buf: memory buffer + * @bufsz: size of @buf + * + * Returns the arithmetic sum of all bytes in @buf, adding them one byte at a time. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint16 +fu_common_sum16(const guint8 *buf, gsize bufsz) +{ + guint16 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT16); + for (gsize i = 0; i < bufsz; i++) + checksum += buf[i]; + return checksum; +} + +/** + * fu_common_sum16_bytes: + * @blob: a #GBytes + * + * Returns the arithmetic sum of all bytes in @blob, adding them one byte at a time. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint16 +fu_common_sum16_bytes(GBytes *blob) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT16); + return fu_common_sum16(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob)); +} + +/** + * fu_common_sum16w: + * @buf: memory buffer + * @bufsz: size of @buf + * @endian: an endian type, e.g. %G_LITTLE_ENDIAN + * + * Returns the arithmetic sum of all bytes in @buf, adding them one word at a time. + * The caller must ensure that @bufsz is a multiple of 2. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint16 +fu_common_sum16w(const guint8 *buf, gsize bufsz, FuEndianType endian) +{ + guint16 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT16); + g_return_val_if_fail(bufsz % 2 == 0, G_MAXUINT16); + for (gsize i = 0; i < bufsz; i += 2) + checksum += fu_common_read_uint16(&buf[i], endian); + return checksum; +} + +/** + * fu_common_sum16w_bytes: + * @blob: a #GBytes + * @endian: an endian type, e.g. %G_LITTLE_ENDIAN + * + * Returns the arithmetic sum of all bytes in @blob, adding them one word at a time. + * The caller must ensure that the size of @blob is a multiple of 2. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint16 +fu_common_sum16w_bytes(GBytes *blob, FuEndianType endian) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT16); + return fu_common_sum16w(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob), endian); +} + +/** + * fu_common_sum32: + * @buf: memory buffer + * @bufsz: size of @buf + * + * Returns the arithmetic sum of all bytes in @buf, adding them one byte at a time. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint32 +fu_common_sum32(const guint8 *buf, gsize bufsz) +{ + guint32 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT32); + for (gsize i = 0; i < bufsz; i++) + checksum += buf[i]; + return checksum; +} + +/** + * fu_common_sum32_bytes: + * @blob: a #GBytes + * + * Returns the arithmetic sum of all bytes in @blob, adding them one byte at a time. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint32 +fu_common_sum32_bytes(GBytes *blob) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT32); + return fu_common_sum32(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob)); +} + +/** + * fu_common_sum32w: + * @buf: memory buffer + * @bufsz: size of @buf + * @endian: an endian type, e.g. %G_LITTLE_ENDIAN + * + * Returns the arithmetic sum of all bytes in @buf, adding them one dword at a time. + * The caller must ensure that @bufsz is a multiple of 4. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint32 +fu_common_sum32w(const guint8 *buf, gsize bufsz, FuEndianType endian) +{ + guint32 checksum = 0; + g_return_val_if_fail(buf != NULL, G_MAXUINT32); + g_return_val_if_fail(bufsz % 4 == 0, G_MAXUINT32); + for (gsize i = 0; i < bufsz; i += 4) + checksum += fu_common_read_uint32(&buf[i], endian); + return checksum; +} + +/** + * fu_common_sum32w_bytes: + * @blob: a #GBytes + * @endian: an endian type, e.g. %G_LITTLE_ENDIAN + * + * Returns the arithmetic sum of all bytes in @blob, adding them one dword at a time. + * The caller must ensure that the size of @blob is a multiple of 4. + * + * Returns: sum value + * + * Since: 1.7.3 + **/ +guint32 +fu_common_sum32w_bytes(GBytes *blob, FuEndianType endian) +{ + g_return_val_if_fail(blob != NULL, G_MAXUINT32); + return fu_common_sum32w(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob), endian); +} + /** * fu_common_uri_get_scheme: * @uri: valid URI, e.g. `https://foo.bar/baz` diff --git a/libfwupdplugin/fu-common.h b/libfwupdplugin/fu-common.h index cef876623..602c55e31 100644 --- a/libfwupdplugin/fu-common.h +++ b/libfwupdplugin/fu-common.h @@ -420,6 +420,28 @@ guint32 fu_common_crc32(const guint8 *buf, gsize bufsz); guint32 fu_common_crc32_full(const guint8 *buf, gsize bufsz, guint32 crc, guint32 polynomial); + +guint8 +fu_common_sum8(const guint8 *buf, gsize bufsz); +guint8 +fu_common_sum8_bytes(GBytes *blob); +guint16 +fu_common_sum16(const guint8 *buf, gsize bufsz); +guint16 +fu_common_sum16_bytes(GBytes *blob); +guint16 +fu_common_sum16w(const guint8 *buf, gsize bufsz, FuEndianType endian); +guint16 +fu_common_sum16w_bytes(GBytes *blob, FuEndianType endian); +guint32 +fu_common_sum32(const guint8 *buf, gsize bufsz); +guint32 +fu_common_sum32_bytes(GBytes *blob); +guint32 +fu_common_sum32w(const guint8 *buf, gsize bufsz, FuEndianType endian); +guint32 +fu_common_sum32w_bytes(GBytes *blob, FuEndianType endian); + gchar * fu_common_uri_get_scheme(const gchar *uri); gsize diff --git a/libfwupdplugin/fu-efi-firmware-file.c b/libfwupdplugin/fu-efi-firmware-file.c index 65745a1ca..4638a4bdb 100644 --- a/libfwupdplugin/fu-efi-firmware-file.c +++ b/libfwupdplugin/fu-efi-firmware-file.c @@ -113,17 +113,6 @@ fu_efi_firmware_file_export(FuFirmware *firmware, FuFirmwareExportFlags flags, X } } -static guint8 -fu_efi_firmware_file_data_checksum8(GBytes *blob) -{ - gsize bufsz = 0; - guint8 checksum = 0; - const guint8 *buf = g_bytes_get_data(blob, &bufsz); - for (gsize i = 0; i < bufsz; i++) - checksum += buf[i]; - return 0x100 - checksum; -} - static guint8 fu_efi_firmware_file_hdr_checksum8(GBytes *blob) { @@ -265,7 +254,7 @@ fu_efi_firmware_file_parse(FuFirmware *firmware, /* verify data checksum */ if ((priv->attrib & FU_EFI_FIRMWARE_FILE_ATTRIB_CHECKSUM) > 0 && (flags & FWUPD_INSTALL_FLAG_IGNORE_CHECKSUM) == 0) { - guint8 data_checksum_verify = fu_efi_firmware_file_data_checksum8(blob); + guint8 data_checksum_verify = 0x100 - fu_common_sum8_bytes(blob); if (data_checksum_verify != data_checksum) { g_set_error(error, FWUPD_ERROR, @@ -344,7 +333,7 @@ fu_efi_firmware_file_write(FuFirmware *firmware, GError **error) return NULL; g_byte_array_append(buf, (guint8 *)&guid, sizeof(guid)); fu_byte_array_append_uint8(buf, 0x0); /* hdr_checksum */ - fu_byte_array_append_uint8(buf, fu_efi_firmware_file_data_checksum8(blob)); + fu_byte_array_append_uint8(buf, 0x100 - fu_common_sum8_bytes(blob)); fu_byte_array_append_uint8(buf, priv->type); /* data_checksum */ fu_byte_array_append_uint8(buf, priv->attrib); /* data_checksum */ fu_byte_array_append_uint32(buf, diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index 258504310..dff12a7a2 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -960,5 +960,15 @@ LIBFWUPDPLUGIN_1.7.3 { global: fu_archive_firmware_get_type; fu_archive_firmware_new; + fu_common_sum16; + fu_common_sum16_bytes; + fu_common_sum16w; + fu_common_sum16w_bytes; + fu_common_sum32; + fu_common_sum32_bytes; + fu_common_sum32w; + fu_common_sum32w_bytes; + fu_common_sum8; + fu_common_sum8_bytes; local: *; } LIBFWUPDPLUGIN_1.7.2; diff --git a/plugins/acpi-phat/fu-acpi-phat.c b/plugins/acpi-phat/fu-acpi-phat.c index cd21b655a..6eb76a564 100644 --- a/plugins/acpi-phat/fu-acpi-phat.c +++ b/plugins/acpi-phat/fu-acpi-phat.c @@ -163,9 +163,7 @@ fu_acpi_phat_parse(FuFirmware *firmware, /* verify checksum */ if ((flags & FWUPD_INSTALL_FLAG_IGNORE_CHECKSUM) == 0) { - guint8 checksum = 0; - for (gsize i = 0; i < length; i++) - checksum += buf[i]; + guint8 checksum = fu_common_sum8(buf, length); if (checksum != 0x00) { g_set_error(error, G_IO_ERROR, @@ -220,7 +218,6 @@ fu_acpi_phat_write(FuFirmware *firmware, GError **error) { FuAcpiPhat *self = FU_ACPI_PHAT(firmware); const gchar *oem_table_id_str = fu_firmware_get_id(firmware); - guint8 checksum = 0; guint8 creator_id[] = {'F', 'W', 'U', 'P'}; guint8 creator_rev[] = {'0', '0', '0', '0'}; guint8 oem_id[6] = {'\0'}; @@ -276,9 +273,7 @@ fu_acpi_phat_write(FuFirmware *firmware, GError **error) g_byte_array_append(buf, buf2->data, buf2->len); /* fixup checksum */ - for (gsize i = 0; i < buf->len; i++) - checksum += buf->data[i]; - buf->data[9] = 0xFF - checksum; + buf->data[9] = 0xFF - fu_common_sum8(buf->data, buf->len); /* success */ return g_byte_array_free_to_bytes(g_steal_pointer(&buf)); diff --git a/plugins/ccgx/fu-ccgx-firmware.c b/plugins/ccgx/fu-ccgx-firmware.c index 58b2c6638..bb407352c 100644 --- a/plugins/ccgx/fu-ccgx-firmware.c +++ b/plugins/ccgx/fu-ccgx-firmware.c @@ -162,17 +162,6 @@ fu_ccgx_firmware_add_record(FuCcgxFirmware *self, return TRUE; } -static guint8 -fu_ccgx_firmware_record_calc_checksum(FuCcgxFirmwareRecord *rcd) -{ - guint8 csum = 0x0; - gsize bufsz = 0; - const guint8 *buf = g_bytes_get_data(rcd->data, &bufsz); - for (gsize j = 0; j < bufsz; j++) - csum += buf[j]; - return csum; -} - static gboolean fu_ccgx_firmware_parse_md_block(FuCcgxFirmware *self, FwupdInstallFlags flags, GError **error) { @@ -238,7 +227,7 @@ fu_ccgx_firmware_parse_md_block(FuCcgxFirmware *self, FwupdInstallFlags flags, G } for (guint i = 0; i < self->records->len - 1; i++) { rcd = g_ptr_array_index(self->records, i); - checksum_calc += fu_ccgx_firmware_record_calc_checksum(rcd); + checksum_calc += fu_common_sum8_bytes(rcd->data); fw_size += g_bytes_get_size(rcd->data); } if (fw_size != metadata.fw_size) { diff --git a/plugins/elantp/fu-elantp-common.c b/plugins/elantp/fu-elantp-common.c deleted file mode 100644 index 4d3a12c97..000000000 --- a/plugins/elantp/fu-elantp-common.c +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (C) 2020 Richard Hughes - * - * SPDX-License-Identifier: LGPL-2.1+ - */ - -#include "config.h" - -#include "fu-elantp-common.h" - -guint16 -fu_elantp_calc_checksum(const guint8 *data, gsize length) -{ - guint16 checksum = 0; - for (gsize i = 0; i < length; i += 2) - checksum += ((guint16)(data[i + 1]) << 8) | (data[i]); - return checksum; -} diff --git a/plugins/elantp/fu-elantp-common.h b/plugins/elantp/fu-elantp-common.h index fa5a8f427..7f63baa8e 100644 --- a/plugins/elantp/fu-elantp-common.h +++ b/plugins/elantp/fu-elantp-common.h @@ -44,6 +44,3 @@ #define ELANTP_DELAY_UNLOCK 100 /* ms */ #define ELANTP_DELAY_WRITE_BLOCK 35 /* ms */ #define ELANTP_DELAY_WRITE_BLOCK_512 50 /* ms */ - -guint16 -fu_elantp_calc_checksum(const guint8 *data, gsize length); diff --git a/plugins/elantp/fu-elantp-hid-device.c b/plugins/elantp/fu-elantp-hid-device.c index 894f5f1b8..a18c46511 100644 --- a/plugins/elantp/fu-elantp-hid-device.c +++ b/plugins/elantp/fu-elantp-hid-device.c @@ -339,8 +339,9 @@ fu_elantp_hid_device_write_firmware(FuDevice *device, chunks = fu_chunk_array_new(buf + iap_addr, bufsz - iap_addr, 0x0, 0x0, self->fw_page_size); for (guint i = 0; i < chunks->len; i++) { FuChunk *chk = g_ptr_array_index(chunks, i); - guint16 csum_tmp = - fu_elantp_calc_checksum(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + guint16 csum_tmp = fu_common_sum16w(fu_chunk_get_data(chk), + fu_chunk_get_data_sz(chk), + G_LITTLE_ENDIAN); gsize blksz = self->fw_page_size + 3; g_autofree guint8 *blk = g_malloc0(blksz); diff --git a/plugins/elantp/fu-elantp-i2c-device.c b/plugins/elantp/fu-elantp-i2c-device.c index 2921da22e..07cdb75be 100644 --- a/plugins/elantp/fu-elantp-i2c-device.c +++ b/plugins/elantp/fu-elantp-i2c-device.c @@ -411,8 +411,9 @@ fu_elantp_i2c_device_write_firmware(FuDevice *device, chunks = fu_chunk_array_new(buf + iap_addr, bufsz - iap_addr, 0x0, 0x0, self->fw_page_size); for (guint i = 0; i < chunks->len; i++) { FuChunk *chk = g_ptr_array_index(chunks, i); - guint16 csum_tmp = - fu_elantp_calc_checksum(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + guint16 csum_tmp = fu_common_sum16w(fu_chunk_get_data(chk), + fu_chunk_get_data_sz(chk), + G_LITTLE_ENDIAN); gsize blksz = self->fw_page_size + 4; g_autofree guint8 *blk = g_malloc0(blksz); diff --git a/plugins/elantp/meson.build b/plugins/elantp/meson.build index 82fb06e9d..064c7423d 100644 --- a/plugins/elantp/meson.build +++ b/plugins/elantp/meson.build @@ -11,7 +11,6 @@ shared_module('fu_plugin_elantp', fu_hash, sources : [ 'fu-plugin-elantp.c', - 'fu-elantp-common.c', 'fu-elantp-firmware.c', # fuzzing 'fu-elantp-hid-device.c', 'fu-elantp-i2c-device.c', diff --git a/plugins/pixart-rf/fu-pxi-ble-device.c b/plugins/pixart-rf/fu-pxi-ble-device.c index 007774458..ecf5bb35c 100644 --- a/plugins/pixart-rf/fu-pxi-ble-device.c +++ b/plugins/pixart-rf/fu-pxi-ble-device.c @@ -315,8 +315,7 @@ fu_pxi_ble_device_check_support_resume(FuPxiBleDevice *self, /* calculate device current checksum */ for (guint i = 0; i < self->fwstate.offset; i++) { FuChunk *chk = g_ptr_array_index(chunks, i); - checksum_tmp += - fu_pxi_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + checksum_tmp += fu_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); } /* check current file is different with previous fw bin or not */ @@ -488,7 +487,7 @@ fu_pxi_ble_device_write_chunk(FuPxiBleDevice *self, FuChunk *chk, GError **error } /* the last chunk */ - checksum = fu_pxi_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + checksum = fu_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); self->fwstate.checksum += checksum; if (checksum_device != self->fwstate.checksum) { g_set_error(error, @@ -581,8 +580,6 @@ fu_pxi_ble_device_fw_upgrade(FuPxiBleDevice *self, GError **error) { const gchar *version; - const guint8 *buf; - gsize bufsz = 0; guint8 fw_version[5] = {0x0}; guint8 opcode = 0; guint16 checksum; @@ -592,11 +589,10 @@ fu_pxi_ble_device_fw_upgrade(FuPxiBleDevice *self, fw = fu_firmware_get_bytes(firmware, error); if (fw == NULL) return FALSE; - buf = g_bytes_get_data(fw, &bufsz); - checksum = fu_pxi_common_sum16(buf, bufsz); + checksum = fu_common_sum16_bytes(fw); fu_byte_array_append_uint8(req, PXI_HID_DEV_OTA_FEATURE_REPORT_ID); fu_byte_array_append_uint8(req, FU_PXI_DEVICE_CMD_FW_UPGRADE); - fu_byte_array_append_uint32(req, bufsz, G_LITTLE_ENDIAN); + fu_byte_array_append_uint32(req, g_bytes_get_size(fw), G_LITTLE_ENDIAN); fu_byte_array_append_uint16(req, checksum, G_LITTLE_ENDIAN); version = fu_firmware_get_version(firmware); if (!fu_memcpy_safe(fw_version, @@ -623,7 +619,7 @@ fu_pxi_ble_device_fw_upgrade(FuPxiBleDevice *self, "FwUpgrade command fail, " "fw-checksum: 0x%04x fw-size: %" G_GSIZE_FORMAT ": ", checksum, - bufsz); + g_bytes_get_size(fw)); return FALSE; } if (opcode != FU_PXI_DEVICE_CMD_FW_UPGRADE) { diff --git a/plugins/pixart-rf/fu-pxi-common.c b/plugins/pixart-rf/fu-pxi-common.c index 113887614..fd6abbafd 100644 --- a/plugins/pixart-rf/fu-pxi-common.c +++ b/plugins/pixart-rf/fu-pxi-common.c @@ -9,24 +9,6 @@ #include "fu-pxi-common.h" -guint8 -fu_pxi_common_sum8(const guint8 *buf, gsize bufsz) -{ - guint8 checksum = 0; - for (gsize idx = 0; idx < bufsz; idx++) - checksum += (guint8)buf[idx]; - return checksum; -} - -guint16 -fu_pxi_common_sum16(const guint8 *buf, gsize bufsz) -{ - guint16 checksum = 0; - for (gsize idx = 0; idx < bufsz; idx++) - checksum += (guint8)buf[idx]; - return checksum; -} - const gchar * fu_pxi_spec_check_result_to_string(guint8 spec_check_result) { @@ -172,7 +154,7 @@ fu_pxi_composite_receiver_cmd(guint8 opcode, g_byte_array_prepend(wireless_mod_cmd, &rf_cmd_code, 0x01); /* command code */ /* prepend checksum */ - checksum = fu_pxi_common_sum8(wireless_mod_cmd->data, wireless_mod_cmd->len); + checksum = fu_common_sum8(wireless_mod_cmd->data, wireless_mod_cmd->len); g_byte_array_prepend(wireless_mod_cmd, &checksum, 0x01); /* prepend feature report id */ diff --git a/plugins/pixart-rf/fu-pxi-common.h b/plugins/pixart-rf/fu-pxi-common.h index 48d7baa39..7dd78c86c 100644 --- a/plugins/pixart-rf/fu-pxi-common.h +++ b/plugins/pixart-rf/fu-pxi-common.h @@ -103,10 +103,6 @@ struct ota_fw_state { guint8 spec_check_result; }; -guint8 -fu_pxi_common_sum8(const guint8 *buf, gsize bufsz); -guint16 -fu_pxi_common_sum16(const guint8 *buf, gsize bufsz); gboolean fu_pxi_composite_receiver_cmd(guint8 opcode, guint8 sn, diff --git a/plugins/pixart-rf/fu-pxi-receiver-device.c b/plugins/pixart-rf/fu-pxi-receiver-device.c index f7609502c..a54c4bfab 100644 --- a/plugins/pixart-rf/fu-pxi-receiver-device.c +++ b/plugins/pixart-rf/fu-pxi-receiver-device.c @@ -385,7 +385,7 @@ fu_pxi_receiver_device_write_chunk(FuDevice *device, FuChunk *chk, GError **erro self->fwstate.mtu_size); /* the checksum of chunk */ - checksum = fu_pxi_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + checksum = fu_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); self->fwstate.checksum += checksum; for (guint i = 0; i < chunks->len; i++) { FuChunk *chk2 = g_ptr_array_index(chunks, i); @@ -414,9 +414,6 @@ fu_pxi_receiver_device_fw_upgrade(FuDevice *device, { FuPxiReceiverDevice *self = FU_PXI_RECEIVER_DEVICE(device); const gchar *version; - const guint8 *buf; - gsize bufsz = 0; - guint16 checksum = 0x0; guint8 fw_version[5] = {0x0}; guint8 res[FU_PXI_RECEIVER_DEVICE_OTA_BUF_SZ] = {0x0}; guint8 result = 0x0; @@ -434,19 +431,16 @@ fu_pxi_receiver_device_fw_upgrade(FuDevice *device, if (fw == NULL) return FALSE; - buf = g_bytes_get_data(fw, &bufsz); - checksum = fu_pxi_common_sum16(buf, bufsz); - /* ota fw upgrade command */ fu_byte_array_append_uint8(ota_cmd, 0x0c); /* ota fw upgrade command length */ fu_byte_array_append_uint8( ota_cmd, FU_PXI_DEVICE_CMD_FW_UPGRADE); /* ota fw upgrade command opccode */ fu_byte_array_append_uint32(ota_cmd, - bufsz, + g_bytes_get_size(fw), G_LITTLE_ENDIAN); /* ota fw upgrade command fw size */ fu_byte_array_append_uint16(ota_cmd, - checksum, + fu_common_sum16_bytes(fw), G_LITTLE_ENDIAN); /* ota fw upgrade command checksum */ version = fu_firmware_get_version(firmware); diff --git a/plugins/pixart-rf/fu-pxi-wireless-device.c b/plugins/pixart-rf/fu-pxi-wireless-device.c index 5d18dd657..53bf77c3a 100644 --- a/plugins/pixart-rf/fu-pxi-wireless-device.c +++ b/plugins/pixart-rf/fu-pxi-wireless-device.c @@ -334,7 +334,7 @@ fu_pxi_wireless_device_write_chunk(FuDevice *device, FuChunk *chk, GError **erro self->fwstate.mtu_size); /* calculate checksum of chunk */ - checksum = fu_pxi_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); + checksum = fu_common_sum16(fu_chunk_get_data(chk), fu_chunk_get_data_sz(chk)); self->fwstate.checksum += checksum; for (guint i = 0; i < chunks->len; i++) { @@ -461,9 +461,6 @@ fu_pxi_wireless_device_fw_upgrade(FuDevice *device, FuPxiReceiverDevice *parent; FuPxiWirelessDevice *self = FU_PXI_WIRELESS_DEVICE(device); const gchar *version; - const guint8 *buf; - gsize bufsz = 0; - guint16 checksum = 0x0; guint8 fw_version[5] = {0x0}; g_autoptr(GByteArray) ota_cmd = g_byte_array_new(); g_autoptr(GByteArray) receiver_cmd = g_byte_array_new(); @@ -484,19 +481,16 @@ fu_pxi_wireless_device_fw_upgrade(FuDevice *device, if (fw == NULL) return FALSE; - buf = g_bytes_get_data(fw, &bufsz); - checksum = fu_pxi_common_sum16(buf, bufsz); - /* ota fw upgrade command */ fu_byte_array_append_uint8(ota_cmd, 0x0c); /* ota fw upgrade command length */ fu_byte_array_append_uint8( ota_cmd, FU_PXI_DEVICE_CMD_FW_UPGRADE); /* ota fw upgrade command opccode */ fu_byte_array_append_uint32(ota_cmd, - bufsz, + g_bytes_get_size(fw), G_LITTLE_ENDIAN); /* ota fw upgrade command fw size */ fu_byte_array_append_uint16(ota_cmd, - checksum, + fu_common_sum16_bytes(fw), G_LITTLE_ENDIAN); /* ota fw upgrade command checksum */ version = fu_firmware_get_version(firmware); diff --git a/plugins/steelseries/fu-steelseries-gamepad.c b/plugins/steelseries/fu-steelseries-gamepad.c index 2dd090278..b8782b18f 100644 --- a/plugins/steelseries/fu-steelseries-gamepad.c +++ b/plugins/steelseries/fu-steelseries-gamepad.c @@ -302,7 +302,7 @@ fu_steelseries_gamepad_write_firmware_chunks(FuDevice *device, for (guint id = 0; id < chunks->len; id++) { FuChunk *chunk = g_ptr_array_index(chunks, id); - guint16 chunk_checksum = 0; + guint16 chunk_checksum; guint8 data[STEELSERIES_BUFFER_CONTROL_SIZE] = {0xA3}; /* block ID */ @@ -324,11 +324,9 @@ fu_steelseries_gamepad_write_firmware_chunks(FuDevice *device, error)) return FALSE; - for (guint i = 3; i < STEELSERIES_BUFFER_TRANSFER_SIZE + 3; i++) - chunk_checksum += data[i]; - /* block checksum */ /* probably not necessary */ + chunk_checksum = fu_common_sum16(data + 3, STEELSERIES_BUFFER_TRANSFER_SIZE); if (!fu_common_write_uint16_safe(data, STEELSERIES_BUFFER_CONTROL_SIZE, 0x03 + STEELSERIES_BUFFER_TRANSFER_SIZE, diff --git a/plugins/synaptics-mst/fu-synaptics-mst-device.c b/plugins/synaptics-mst/fu-synaptics-mst-device.c index 2a65590fb..df04b08b8 100644 --- a/plugins/synaptics-mst/fu-synaptics-mst-device.c +++ b/plugins/synaptics-mst/fu-synaptics-mst-device.c @@ -310,8 +310,6 @@ fu_synaptics_mst_device_update_esm(FuSynapticsMstDevice *self, self->layer, self->rad); - for (guint32 i = 0; i < esm_sz; i++) - checksum += *(payload_data + EEPROM_ESM_OFFSET + i); if (!fu_synaptics_mst_device_get_flash_checksum(self, esm_sz, EEPROM_ESM_OFFSET, @@ -321,6 +319,7 @@ fu_synaptics_mst_device_update_esm(FuSynapticsMstDevice *self, } /* ESM checksum same */ + checksum = fu_common_sum32(payload_data + EEPROM_ESM_OFFSET, esm_sz); if (checksum == flash_checksum) { g_debug("ESM checksum already matches"); return TRUE; @@ -368,10 +367,7 @@ fu_synaptics_mst_device_update_esm(FuSynapticsMstDevice *self, } /* check ESM checksum */ - checksum = 0; flash_checksum = 0; - for (guint32 i = 0; i < esm_sz; i++) - checksum += *(payload_data + EEPROM_ESM_OFFSET + i); if (!fu_synaptics_mst_device_get_flash_checksum(self, esm_sz, EEPROM_ESM_OFFSET, @@ -424,7 +420,7 @@ fu_synaptics_mst_device_update_tesla_leaf_firmware(FuSynapticsMstDevice *self, self->layer, self->rad); for (guint32 retries_cnt = 0;; retries_cnt++) { - guint32 checksum = 0; + guint32 checksum; guint32 flash_checksum = 0; if (!fu_synaptics_mst_device_set_flash_sector_erase(self, 0xffff, 0, error)) @@ -469,15 +465,13 @@ fu_synaptics_mst_device_update_tesla_leaf_firmware(FuSynapticsMstDevice *self, } /* check data just written */ - for (guint32 i = 0; i < payload_len; i++) - checksum += *(payload_data + i); - if (!fu_synaptics_mst_device_get_flash_checksum(self, payload_len, 0, &flash_checksum, error)) return FALSE; + checksum = fu_common_sum32(payload_data, payload_len); if (checksum == flash_checksum) break; g_debug("attempt %u: checksum %x didn't match %x", diff --git a/plugins/uefi-capsule/fu-plugin-uefi-capsule.c b/plugins/uefi-capsule/fu-plugin-uefi-capsule.c index 3f28eac3d..b09e324b4 100644 --- a/plugins/uefi-capsule/fu-plugin-uefi-capsule.c +++ b/plugins/uefi-capsule/fu-plugin-uefi-capsule.c @@ -130,15 +130,6 @@ fu_plugin_uefi_capsule_get_splash_data(guint width, guint height, GError **error return NULL; } -static guint8 -fu_plugin_uefi_capsule_calc_checksum(const guint8 *buf, gsize sz) -{ - guint8 csum = 0; - for (gsize i = 0; i < sz; i++) - csum += buf[i]; - return csum; -} - static gboolean fu_plugin_uefi_capsule_write_splash_data(FuPlugin *plugin, FuDevice *device, @@ -209,11 +200,9 @@ fu_plugin_uefi_capsule_write_splash_data(FuPlugin *plugin, }; /* header, payload and image has to add to zero */ - csum += - fu_plugin_uefi_capsule_calc_checksum((guint8 *)&capsule_header, sizeof(capsule_header)); - csum += fu_plugin_uefi_capsule_calc_checksum((guint8 *)&header, sizeof(header)); - csum += fu_plugin_uefi_capsule_calc_checksum(g_bytes_get_data(blob, NULL), - g_bytes_get_size(blob)); + csum += fu_common_sum8((guint8 *)&capsule_header, sizeof(capsule_header)); + csum += fu_common_sum8((guint8 *)&header, sizeof(header)); + csum += fu_common_sum8_bytes(blob); header.checksum = 0x100 - csum; /* write capsule file */ diff --git a/plugins/wacom-raw/fu-wacom-emr-device.c b/plugins/wacom-raw/fu-wacom-emr-device.c index ecafedf1f..f8cd16381 100644 --- a/plugins/wacom-raw/fu-wacom-emr-device.c +++ b/plugins/wacom-raw/fu-wacom-emr-device.c @@ -56,13 +56,9 @@ fu_wacom_emr_device_setup(FuDevice *device, GError **error) } static guint8 -fu_wacom_emr_device_calc_checksum(guint8 init1, const guint8 *buf, guint8 bufsz) +fu_wacom_emr_device_calc_checksum(guint8 init1, const guint8 *buf, gsize bufsz) { - guint8 sum = 0; - sum += init1; - for (guint i = 0; i < bufsz; i++) - sum += buf[i]; - return ~sum + 1; + return init1 + ~(fu_common_sum8(buf, bufsz)) + 1; } static gboolean diff --git a/plugins/wacom-usb/fu-wac-common.c b/plugins/wacom-usb/fu-wac-common.c index 44b29b413..a892f6a81 100644 --- a/plugins/wacom-usb/fu-wac-common.c +++ b/plugins/wacom-usb/fu-wac-common.c @@ -12,27 +12,6 @@ #include "fu-wac-common.h" -guint32 -fu_wac_calculate_checksum32le(const guint8 *data, gsize len) -{ - guint32 csum = 0x0; - g_return_val_if_fail(len % 4 == 0, G_MAXUINT32); - for (guint i = 0; i < len; i += 4) { - guint32 tmp; - memcpy(&tmp, &data[i], sizeof(guint32)); - csum += GUINT32_FROM_LE(tmp); - } - return GUINT32_TO_LE(csum); -} - -guint32 -fu_wac_calculate_checksum32le_bytes(GBytes *blob) -{ - gsize len = 0; - const guint8 *data = g_bytes_get_data(blob, &len); - return fu_wac_calculate_checksum32le(data, len); -} - const gchar * fu_wac_report_id_to_string(guint8 report_id) { diff --git a/plugins/wacom-usb/fu-wac-common.h b/plugins/wacom-usb/fu-wac-common.h index 9162018f5..863ea8d3f 100644 --- a/plugins/wacom-usb/fu-wac-common.h +++ b/plugins/wacom-usb/fu-wac-common.h @@ -37,10 +37,6 @@ #define FU_WAC_REPORT_ID_GET_CURRENT_FIRMWARE_IDX 0xe2 /* GET_FEATURE */ #define FU_WAC_REPORT_ID_MODULE 0xe4 -guint32 -fu_wac_calculate_checksum32le(const guint8 *data, gsize len); -guint32 -fu_wac_calculate_checksum32le_bytes(GBytes *blob); const gchar * fu_wac_report_id_to_string(guint8 report_id); void diff --git a/plugins/wacom-usb/fu-wac-device.c b/plugins/wacom-usb/fu-wac-device.c index cd60d8f9d..1e7993d83 100644 --- a/plugins/wacom-usb/fu-wac-device.c +++ b/plugins/wacom-usb/fu-wac-device.c @@ -583,7 +583,7 @@ fu_wac_device_write_firmware(FuDevice *device, } /* calculate expected checksum and save to device RAM */ - csum_local[i] = fu_wac_calculate_checksum32le_bytes(blob_block); + csum_local[i] = GUINT32_TO_LE(fu_common_sum32w_bytes(blob_block, G_LITTLE_ENDIAN)); if (g_getenv("FWUPD_WACOM_USB_VERBOSE") != NULL) g_debug("block checksum %02u: 0x%08x", i, csum_local[i]); if (!fu_wac_device_set_checksum_of_block(self, i, csum_local[i], error)) diff --git a/plugins/wacom-usb/fu-wac-firmware.c b/plugins/wacom-usb/fu-wac-firmware.c index 640991967..78026dad4 100644 --- a/plugins/wacom-usb/fu-wac-firmware.c +++ b/plugins/wacom-usb/fu-wac-firmware.c @@ -304,10 +304,7 @@ fu_wac_firmware_parse(FuFirmware *firmware, static guint8 fu_wac_firmware_calc_checksum(GByteArray *buf) { - guint8 csum = 0; - for (guint i = 0; i < buf->len; i++) - csum += buf->data[i]; - return csum ^ 0xFF; + return fu_common_sum8(buf->data, buf->len) ^ 0xFF; } static GBytes *