mirror of
https://git.proxmox.com/git/fwupd
synced 2025-08-02 22:03:48 +00:00
plugins/nordic-hid: support images for MCUBoot
- added images for MCUBoot bootloader
This commit is contained in:
parent
25025936a2
commit
dabc9657bc
@ -9,6 +9,7 @@
|
||||
|
||||
#include "fu-nordic-hid-archive.h"
|
||||
#include "fu-nordic-hid-firmware-b0.h"
|
||||
#include "fu-nordic-hid-firmware-mcuboot.h"
|
||||
|
||||
/* current version format is 0 */
|
||||
#define MAX_VERSION_FORMAT 0
|
||||
@ -123,13 +124,8 @@ fu_nordic_hid_archive_parse(FuFirmware *firmware,
|
||||
bootloader_name = "B0";
|
||||
image = g_object_new(FU_TYPE_NORDIC_HID_FIRMWARE_B0, NULL);
|
||||
} else if (json_object_has_member(obj, "version_MCUBOOT")) {
|
||||
/* TODO: add MCUboot format */
|
||||
bootloader_name = "MCUBOOT";
|
||||
g_set_error_literal(error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_INVALID_FILE,
|
||||
"MCUboot bootloader is not supported");
|
||||
return FALSE;
|
||||
image = g_object_new(FU_TYPE_NORDIC_HID_FIRMWARE_MCUBOOT, NULL);
|
||||
} else {
|
||||
g_set_error_literal(error,
|
||||
FWUPD_ERROR,
|
||||
@ -154,8 +150,10 @@ fu_nordic_hid_archive_parse(FuFirmware *firmware,
|
||||
"manifest invalid as has no target board information");
|
||||
return FALSE;
|
||||
}
|
||||
/* images are listed in strict order: this is guaranteed by producer
|
||||
* set the id format as <board>_<bl>_<bank>N, i.e "nrf52840dk_B0_bank0" */
|
||||
/* images for B0 bootloader are listed in strict order:
|
||||
* this is guaranteed by producer set the id format as
|
||||
* <board>_<bl>_<bank>N, i.e "nrf52840dk_B0_bank0".
|
||||
* For MCUBoot bootloader only the single image is available */
|
||||
image_id = g_strdup_printf("%s_%s_bank%01u", board_split[0], bootloader_name, i);
|
||||
if (!fu_firmware_parse(image, blob, flags, error))
|
||||
return FALSE;
|
||||
|
@ -6,8 +6,6 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <fwupdplugin.h>
|
||||
|
||||
#include "fu-nordic-hid-firmware-b0.h"
|
||||
|
||||
#define UPDATE_IMAGE_MAGIC_COMMON 0x281ee6de
|
||||
@ -16,20 +14,10 @@
|
||||
#define UPDATE_IMAGE_MAGIC_NRF53 0x00003502
|
||||
|
||||
struct _FuNordicHidFirmwareB0 {
|
||||
FuIhexFirmwareClass parent_instance;
|
||||
guint32 crc32;
|
||||
FuNordicHidFirmwareClass parent_instance;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE(FuNordicHidFirmwareB0, fu_nordic_hid_firmware_b0, FU_TYPE_FIRMWARE)
|
||||
|
||||
static void
|
||||
fu_nordic_hid_firmware_b0_export(FuFirmware *firmware,
|
||||
FuFirmwareExportFlags flags,
|
||||
XbBuilderNode *bn)
|
||||
{
|
||||
FuNordicHidFirmwareB0 *self = FU_NORDIC_HID_FIRMWARE_B0(firmware);
|
||||
fu_xmlb_builder_insert_kx(bn, "crc32", self->crc32);
|
||||
}
|
||||
G_DEFINE_TYPE(FuNordicHidFirmwareB0, fu_nordic_hid_firmware_b0, FU_TYPE_NORDIC_HID_FIRMWARE)
|
||||
|
||||
static GBytes *
|
||||
fu_nordic_hid_firmware_b0_write(FuFirmware *firmware, GError **error)
|
||||
@ -39,6 +27,10 @@ fu_nordic_hid_firmware_b0_write(FuFirmware *firmware, GError **error)
|
||||
fu_byte_array_append_uint32(buf, UPDATE_IMAGE_MAGIC_COMMON, G_LITTLE_ENDIAN);
|
||||
fu_byte_array_append_uint32(buf, UPDATE_IMAGE_MAGIC_FWINFO, G_LITTLE_ENDIAN);
|
||||
fu_byte_array_append_uint32(buf, UPDATE_IMAGE_MAGIC_NRF52, G_LITTLE_ENDIAN);
|
||||
fu_byte_array_append_uint32(buf, 0x00, G_LITTLE_ENDIAN);
|
||||
fu_byte_array_append_uint32(buf, 0x00, G_LITTLE_ENDIAN);
|
||||
/* version */
|
||||
fu_byte_array_append_uint32(buf, 0x63, G_LITTLE_ENDIAN);
|
||||
blob = fu_firmware_get_bytes(firmware, error);
|
||||
if (blob == NULL)
|
||||
return NULL;
|
||||
@ -47,13 +39,21 @@ fu_nordic_hid_firmware_b0_write(FuFirmware *firmware, GError **error)
|
||||
}
|
||||
|
||||
static gboolean
|
||||
fu_nordic_hid_firmware_b0_read_fwinfo(guint8 const *buf, gsize bufsz, GError **error)
|
||||
fu_nordic_hid_firmware_b0_read_fwinfo(FuFirmware *firmware,
|
||||
guint8 const *buf,
|
||||
gsize bufsz,
|
||||
GError **error)
|
||||
{
|
||||
guint32 magic_common;
|
||||
guint32 magic_fwinfo;
|
||||
guint32 magic_compat;
|
||||
guint32 offset;
|
||||
guint32 hdr_offset[5] = {0x0000, 0x0200, 0x400, 0x800, 0x1000};
|
||||
guint8 ver_major = 0;
|
||||
guint8 ver_minor = 0;
|
||||
guint16 ver_rev = 0;
|
||||
guint32 ver_build_nr = 0;
|
||||
g_autofree gchar *version = NULL;
|
||||
|
||||
/* find correct offset to fwinfo */
|
||||
for (guint32 i = 0; i < G_N_ELEMENTS(hdr_offset); i++) {
|
||||
@ -79,12 +79,28 @@ fu_nordic_hid_firmware_b0_read_fwinfo(guint8 const *buf, gsize bufsz, GError **e
|
||||
G_LITTLE_ENDIAN,
|
||||
error))
|
||||
return FALSE;
|
||||
/* version */
|
||||
if (!fu_common_read_uint32_safe(buf,
|
||||
bufsz,
|
||||
offset + 0x14,
|
||||
&ver_build_nr,
|
||||
G_LITTLE_ENDIAN,
|
||||
error))
|
||||
return FALSE;
|
||||
|
||||
if (magic_common != UPDATE_IMAGE_MAGIC_COMMON ||
|
||||
magic_fwinfo != UPDATE_IMAGE_MAGIC_FWINFO)
|
||||
continue;
|
||||
switch (magic_compat) {
|
||||
case UPDATE_IMAGE_MAGIC_NRF52:
|
||||
case UPDATE_IMAGE_MAGIC_NRF53:
|
||||
/* currently only the build number is saved into the image */
|
||||
version = g_strdup_printf("%u.%u.%u.%u",
|
||||
ver_major,
|
||||
ver_minor,
|
||||
ver_rev,
|
||||
ver_build_nr);
|
||||
fu_firmware_set_version(firmware, version);
|
||||
return TRUE;
|
||||
default:
|
||||
break;
|
||||
@ -98,32 +114,6 @@ fu_nordic_hid_firmware_b0_read_fwinfo(guint8 const *buf, gsize bufsz, GError **e
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static guint32
|
||||
fu_nordic_hid_firmware_b0_crc32(const guint8 *buf, gsize bufsz)
|
||||
{
|
||||
guint crc32 = 0x01;
|
||||
/* maybe skipped "^" step in fu_common_crc32_full()?
|
||||
* according https://github.com/madler/zlib/blob/master/crc32.c#L225 */
|
||||
crc32 ^= 0xFFFFFFFFUL;
|
||||
return fu_common_crc32_full(buf, bufsz, crc32, 0xEDB88320);
|
||||
}
|
||||
|
||||
static gchar *
|
||||
fu_nordic_hid_firmware_b0_get_checksum(FuFirmware *firmware,
|
||||
GChecksumType csum_kind,
|
||||
GError **error)
|
||||
{
|
||||
FuNordicHidFirmwareB0 *self = FU_NORDIC_HID_FIRMWARE_B0(firmware);
|
||||
if (!fu_firmware_has_flag(firmware, FU_FIRMWARE_FLAG_HAS_CHECKSUM)) {
|
||||
g_set_error_literal(error,
|
||||
G_IO_ERROR,
|
||||
G_IO_ERROR_NOT_SUPPORTED,
|
||||
"unable to calculate the checksum of the update binary");
|
||||
return NULL;
|
||||
}
|
||||
return g_strdup_printf("%x", self->crc32);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
fu_nordic_hid_firmware_b0_parse(FuFirmware *firmware,
|
||||
GBytes *fw,
|
||||
@ -132,10 +122,13 @@ fu_nordic_hid_firmware_b0_parse(FuFirmware *firmware,
|
||||
FwupdInstallFlags flags,
|
||||
GError **error)
|
||||
{
|
||||
FuNordicHidFirmwareB0 *self = FU_NORDIC_HID_FIRMWARE_B0(firmware);
|
||||
const guint8 *buf;
|
||||
gsize bufsz = 0;
|
||||
|
||||
if (!FU_FIRMWARE_CLASS(fu_nordic_hid_firmware_b0_parent_class)
|
||||
->parse(firmware, fw, addr_start, addr_end, flags, error))
|
||||
return FALSE;
|
||||
|
||||
buf = g_bytes_get_data(fw, &bufsz);
|
||||
if (buf == NULL) {
|
||||
g_set_error_literal(error,
|
||||
@ -144,16 +137,8 @@ fu_nordic_hid_firmware_b0_parse(FuFirmware *firmware,
|
||||
"unable to get the image binary");
|
||||
return FALSE;
|
||||
}
|
||||
if (!fu_nordic_hid_firmware_b0_read_fwinfo(buf, bufsz, error))
|
||||
return FALSE;
|
||||
self->crc32 = fu_nordic_hid_firmware_b0_crc32(buf, bufsz);
|
||||
fu_firmware_add_flag(FU_FIRMWARE(self), FU_FIRMWARE_FLAG_HAS_CHECKSUM);
|
||||
|
||||
/* do not strip the header */
|
||||
fu_firmware_set_bytes(firmware, fw);
|
||||
|
||||
/* success */
|
||||
return TRUE;
|
||||
return fu_nordic_hid_firmware_b0_read_fwinfo(firmware, buf, bufsz, error);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -165,8 +150,6 @@ static void
|
||||
fu_nordic_hid_firmware_b0_class_init(FuNordicHidFirmwareB0Class *klass)
|
||||
{
|
||||
FuFirmwareClass *klass_firmware = FU_FIRMWARE_CLASS(klass);
|
||||
klass_firmware->get_checksum = fu_nordic_hid_firmware_b0_get_checksum;
|
||||
klass_firmware->export = fu_nordic_hid_firmware_b0_export;
|
||||
klass_firmware->parse = fu_nordic_hid_firmware_b0_parse;
|
||||
klass_firmware->write = fu_nordic_hid_firmware_b0_write;
|
||||
}
|
||||
|
@ -6,14 +6,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <fwupdplugin.h>
|
||||
#include "fu-nordic-hid-firmware.h"
|
||||
|
||||
#define FU_TYPE_NORDIC_HID_FIRMWARE_B0 (fu_nordic_hid_firmware_b0_get_type())
|
||||
G_DECLARE_FINAL_TYPE(FuNordicHidFirmwareB0,
|
||||
fu_nordic_hid_firmware_b0,
|
||||
FU,
|
||||
NORDIC_HID_FIRMWARE_B0,
|
||||
FuFirmware)
|
||||
FuNordicHidFirmware)
|
||||
|
||||
FuFirmware *
|
||||
fu_nordic_hid_firmware_b0_new(void);
|
||||
|
172
plugins/nordic-hid/fu-nordic-hid-firmware-mcuboot.c
Normal file
172
plugins/nordic-hid/fu-nordic-hid-firmware-mcuboot.c
Normal file
@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Denis Pynkin <denis.pynkin@collabora.com>
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1+
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "fu-nordic-hid-firmware-mcuboot.h"
|
||||
|
||||
#define IMAGE_MAGIC 0x96f3b83d
|
||||
#define IMAGE_TLV_INFO_MAGIC 0x6907
|
||||
#define IMAGE_TLV_PROT_INFO_MAGIC 0x6908
|
||||
|
||||
struct _FuNordicHidFirmwareMcuboot {
|
||||
FuNordicHidFirmwareClass parent_instance;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE(FuNordicHidFirmwareMcuboot,
|
||||
fu_nordic_hid_firmware_mcuboot,
|
||||
FU_TYPE_NORDIC_HID_FIRMWARE)
|
||||
|
||||
static GBytes *
|
||||
fu_nordic_hid_firmware_mcuboot_write(FuFirmware *firmware, GError **error)
|
||||
{
|
||||
g_autoptr(GByteArray) buf = g_byte_array_new();
|
||||
g_autoptr(GBytes) blob = fu_firmware_get_bytes(firmware, error);
|
||||
|
||||
if (blob == NULL)
|
||||
return NULL;
|
||||
|
||||
/* https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/mcuboot/design.html#image-format
|
||||
*/
|
||||
fu_byte_array_append_uint32(buf, IMAGE_MAGIC, G_LITTLE_ENDIAN);
|
||||
/* load_addr */
|
||||
fu_byte_array_append_uint32(buf, 0x00, G_LITTLE_ENDIAN);
|
||||
/* hdr_size */
|
||||
fu_byte_array_append_uint16(buf, 0x20, G_LITTLE_ENDIAN);
|
||||
/* protect_tlv_size */
|
||||
fu_byte_array_append_uint16(buf, 0x00, G_LITTLE_ENDIAN);
|
||||
/* img_size */
|
||||
fu_byte_array_append_uint32(buf, (guint32)g_bytes_get_size(blob), G_LITTLE_ENDIAN);
|
||||
/* flags */
|
||||
fu_byte_array_append_uint32(buf, 0x00, G_LITTLE_ENDIAN);
|
||||
/* version */
|
||||
fu_byte_array_append_uint8(buf, 0x01);
|
||||
fu_byte_array_append_uint8(buf, 0x02);
|
||||
fu_byte_array_append_uint16(buf, 0x03, G_LITTLE_ENDIAN);
|
||||
fu_byte_array_append_uint32(buf, 0x63, G_LITTLE_ENDIAN);
|
||||
/* pad */
|
||||
fu_byte_array_append_uint32(buf, 0xffffffff, G_LITTLE_ENDIAN);
|
||||
/* payload */
|
||||
fu_byte_array_append_bytes(buf, blob);
|
||||
/* TLV magic and total */
|
||||
fu_byte_array_append_uint16(buf, IMAGE_TLV_INFO_MAGIC, G_LITTLE_ENDIAN);
|
||||
fu_byte_array_append_uint16(buf, 0x00, G_LITTLE_ENDIAN);
|
||||
|
||||
return g_byte_array_free_to_bytes(g_steal_pointer(&buf));
|
||||
}
|
||||
|
||||
/* simple validation of the image */
|
||||
static gboolean
|
||||
fu_nordic_hid_firmware_mcuboot_validate(FuFirmware *firmware,
|
||||
guint8 const *buf,
|
||||
gsize bufsz,
|
||||
GError **error)
|
||||
{
|
||||
guint32 magic;
|
||||
guint16 hdr_size;
|
||||
guint32 img_size;
|
||||
guint8 ver_major;
|
||||
guint8 ver_minor;
|
||||
guint16 ver_rev;
|
||||
guint32 ver_build_nr;
|
||||
guint16 magic_tlv;
|
||||
g_autofree gchar *version = NULL;
|
||||
|
||||
if (!fu_common_read_uint32_safe(buf, bufsz, 0, &magic, G_LITTLE_ENDIAN, error))
|
||||
return FALSE;
|
||||
if (magic != IMAGE_MAGIC) {
|
||||
g_set_error_literal(error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_INVALID_FILE,
|
||||
"incorrect image magic");
|
||||
return FALSE;
|
||||
}
|
||||
/* ignore load_addr */
|
||||
if (!fu_common_read_uint16_safe(buf, bufsz, 8, &hdr_size, G_LITTLE_ENDIAN, error))
|
||||
return FALSE;
|
||||
/* ignore protect_tlv_size */
|
||||
if (!fu_common_read_uint32_safe(buf, bufsz, 12, &img_size, G_LITTLE_ENDIAN, error))
|
||||
return FALSE;
|
||||
|
||||
/* ignore TLVs themselves
|
||||
* https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/mcuboot/design.html#protected-tlvs
|
||||
* check the magic values only */
|
||||
if (!fu_common_read_uint16_safe(buf,
|
||||
bufsz,
|
||||
hdr_size + img_size,
|
||||
&magic_tlv,
|
||||
G_LITTLE_ENDIAN,
|
||||
error))
|
||||
return FALSE;
|
||||
if (magic_tlv != IMAGE_TLV_INFO_MAGIC && magic_tlv != IMAGE_TLV_PROT_INFO_MAGIC) {
|
||||
g_set_error_literal(error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_INVALID_FILE,
|
||||
"incorrect TLV info magic");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* version */
|
||||
if (!fu_common_read_uint8_safe(buf, bufsz, 0x14, &ver_major, error))
|
||||
return FALSE;
|
||||
if (!fu_common_read_uint8_safe(buf, bufsz, 0x15, &ver_minor, error))
|
||||
return FALSE;
|
||||
if (!fu_common_read_uint16_safe(buf, bufsz, 0x16, &ver_rev, G_LITTLE_ENDIAN, error))
|
||||
return FALSE;
|
||||
if (!fu_common_read_uint32_safe(buf, bufsz, 0x18, &ver_build_nr, G_LITTLE_ENDIAN, error))
|
||||
return FALSE;
|
||||
version = g_strdup_printf("%u.%u.%u.%u", ver_major, ver_minor, ver_rev, ver_build_nr);
|
||||
|
||||
fu_firmware_set_version(firmware, version);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
fu_nordic_hid_firmware_mcuboot_parse(FuFirmware *firmware,
|
||||
GBytes *fw,
|
||||
guint64 addr_start,
|
||||
guint64 addr_end,
|
||||
FwupdInstallFlags flags,
|
||||
GError **error)
|
||||
{
|
||||
const guint8 *buf;
|
||||
gsize bufsz = 0;
|
||||
|
||||
if (!FU_FIRMWARE_CLASS(fu_nordic_hid_firmware_mcuboot_parent_class)
|
||||
->parse(firmware, fw, addr_start, addr_end, flags, error))
|
||||
return FALSE;
|
||||
|
||||
buf = g_bytes_get_data(fw, &bufsz);
|
||||
if (buf == NULL) {
|
||||
g_set_error_literal(error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_INVALID_FILE,
|
||||
"unable to get the image binary");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return fu_nordic_hid_firmware_mcuboot_validate(firmware, buf, bufsz, error);
|
||||
}
|
||||
|
||||
static void
|
||||
fu_nordic_hid_firmware_mcuboot_init(FuNordicHidFirmwareMcuboot *self)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
fu_nordic_hid_firmware_mcuboot_class_init(FuNordicHidFirmwareMcubootClass *klass)
|
||||
{
|
||||
FuFirmwareClass *klass_firmware = FU_FIRMWARE_CLASS(klass);
|
||||
klass_firmware->parse = fu_nordic_hid_firmware_mcuboot_parse;
|
||||
klass_firmware->write = fu_nordic_hid_firmware_mcuboot_write;
|
||||
}
|
||||
|
||||
FuFirmware *
|
||||
fu_nordic_hid_firmware_mcuboot_new(void)
|
||||
{
|
||||
return FU_FIRMWARE(g_object_new(FU_TYPE_NORDIC_HID_FIRMWARE_MCUBOOT, NULL));
|
||||
}
|
19
plugins/nordic-hid/fu-nordic-hid-firmware-mcuboot.h
Normal file
19
plugins/nordic-hid/fu-nordic-hid-firmware-mcuboot.h
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Denis Pynkin <denis.pynkin@collabora.com>
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1+
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "fu-nordic-hid-firmware.h"
|
||||
|
||||
#define FU_TYPE_NORDIC_HID_FIRMWARE_MCUBOOT (fu_nordic_hid_firmware_mcuboot_get_type())
|
||||
G_DECLARE_FINAL_TYPE(FuNordicHidFirmwareMcuboot,
|
||||
fu_nordic_hid_firmware_mcuboot,
|
||||
FU,
|
||||
NORDIC_HID_FIRMWARE_MCUBOOT,
|
||||
FuNordicHidFirmware)
|
||||
|
||||
FuFirmware *
|
||||
fu_nordic_hid_firmware_mcuboot_new(void);
|
96
plugins/nordic-hid/fu-nordic-hid-firmware.c
Normal file
96
plugins/nordic-hid/fu-nordic-hid-firmware.c
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Denis Pynkin <denis.pynkin@collabora.com>
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1+
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <fwupdplugin.h>
|
||||
|
||||
#include "fu-nordic-hid-firmware.h"
|
||||
|
||||
typedef struct {
|
||||
guint32 crc32;
|
||||
} FuNordicHidFirmwarePrivate;
|
||||
|
||||
G_DEFINE_TYPE_WITH_PRIVATE(FuNordicHidFirmware, fu_nordic_hid_firmware, FU_TYPE_FIRMWARE)
|
||||
#define GET_PRIVATE(o) (fu_nordic_hid_firmware_get_instance_private(o))
|
||||
|
||||
static void
|
||||
fu_nordic_hid_firmware_export(FuFirmware *firmware, FuFirmwareExportFlags flags, XbBuilderNode *bn)
|
||||
{
|
||||
FuNordicHidFirmware *self = FU_NORDIC_HID_FIRMWARE(firmware);
|
||||
FuNordicHidFirmwarePrivate *priv = GET_PRIVATE(self);
|
||||
fu_xmlb_builder_insert_kx(bn, "crc32", priv->crc32);
|
||||
}
|
||||
|
||||
static gchar *
|
||||
fu_nordic_hid_firmware_get_checksum(FuFirmware *firmware, GChecksumType csum_kind, GError **error)
|
||||
{
|
||||
FuNordicHidFirmware *self = FU_NORDIC_HID_FIRMWARE(firmware);
|
||||
FuNordicHidFirmwarePrivate *priv = GET_PRIVATE(self);
|
||||
if (!fu_firmware_has_flag(firmware, FU_FIRMWARE_FLAG_HAS_CHECKSUM)) {
|
||||
g_set_error_literal(error,
|
||||
G_IO_ERROR,
|
||||
G_IO_ERROR_NOT_SUPPORTED,
|
||||
"unable to calculate the checksum of the update binary");
|
||||
return NULL;
|
||||
}
|
||||
return g_strdup_printf("%x", priv->crc32);
|
||||
}
|
||||
|
||||
static guint32
|
||||
fu_nordic_hid_firmware_crc32(const guint8 *buf, gsize bufsz)
|
||||
{
|
||||
guint crc32 = 0x01;
|
||||
/* maybe skipped "^" step in fu_common_crc32_full()?
|
||||
* according https://github.com/madler/zlib/blob/master/crc32.c#L225 */
|
||||
crc32 ^= 0xFFFFFFFFUL;
|
||||
return fu_common_crc32_full(buf, bufsz, crc32, 0xEDB88320);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
fu_nordic_hid_firmware_parse(FuFirmware *firmware,
|
||||
GBytes *fw,
|
||||
guint64 addr_start,
|
||||
guint64 addr_end,
|
||||
FwupdInstallFlags flags,
|
||||
GError **error)
|
||||
{
|
||||
FuNordicHidFirmware *self = FU_NORDIC_HID_FIRMWARE(firmware);
|
||||
FuNordicHidFirmwarePrivate *priv = GET_PRIVATE(self);
|
||||
const guint8 *buf;
|
||||
gsize bufsz = 0;
|
||||
|
||||
buf = g_bytes_get_data(fw, &bufsz);
|
||||
if (buf == NULL) {
|
||||
g_set_error_literal(error,
|
||||
FWUPD_ERROR,
|
||||
FWUPD_ERROR_INVALID_FILE,
|
||||
"unable to get the image binary");
|
||||
return FALSE;
|
||||
}
|
||||
fu_firmware_add_flag(FU_FIRMWARE(self), FU_FIRMWARE_FLAG_HAS_CHECKSUM);
|
||||
priv->crc32 = fu_nordic_hid_firmware_crc32(buf, bufsz);
|
||||
|
||||
/* do not strip the header */
|
||||
fu_firmware_set_bytes(firmware, fw);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
fu_nordic_hid_firmware_init(FuNordicHidFirmware *self)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
fu_nordic_hid_firmware_class_init(FuNordicHidFirmwareClass *klass)
|
||||
{
|
||||
FuFirmwareClass *klass_firmware = FU_FIRMWARE_CLASS(klass);
|
||||
|
||||
klass_firmware->export = fu_nordic_hid_firmware_export;
|
||||
klass_firmware->get_checksum = fu_nordic_hid_firmware_get_checksum;
|
||||
klass_firmware->parse = fu_nordic_hid_firmware_parse;
|
||||
}
|
20
plugins/nordic-hid/fu-nordic-hid-firmware.h
Normal file
20
plugins/nordic-hid/fu-nordic-hid-firmware.h
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Denis Pynkin <denis.pynkin@collabora.com>
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1+
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <fwupdplugin.h>
|
||||
|
||||
#define FU_TYPE_NORDIC_HID_FIRMWARE (fu_nordic_hid_firmware_get_type())
|
||||
G_DECLARE_DERIVABLE_TYPE(FuNordicHidFirmware,
|
||||
fu_nordic_hid_firmware,
|
||||
FU,
|
||||
NORDIC_HID_FIRMWARE,
|
||||
FuFirmware)
|
||||
|
||||
struct _FuNordicHidFirmwareClass {
|
||||
FuFirmwareClass parent_class;
|
||||
};
|
@ -11,6 +11,7 @@
|
||||
#include "fu-nordic-hid-archive.h"
|
||||
#include "fu-nordic-hid-cfg-channel.h"
|
||||
#include "fu-nordic-hid-firmware-b0.h"
|
||||
#include "fu-nordic-hid-firmware-mcuboot.h"
|
||||
|
||||
static void
|
||||
fu_plugin_nordic_hid_init(FuPlugin *plugin)
|
||||
@ -21,6 +22,7 @@ fu_plugin_nordic_hid_init(FuPlugin *plugin)
|
||||
fu_plugin_add_device_gtype(plugin, FU_TYPE_NORDIC_HID_CFG_CHANNEL);
|
||||
fu_plugin_add_firmware_gtype(plugin, NULL, FU_TYPE_NORDIC_HID_ARCHIVE);
|
||||
fu_plugin_add_firmware_gtype(plugin, NULL, FU_TYPE_NORDIC_HID_FIRMWARE_B0);
|
||||
fu_plugin_add_firmware_gtype(plugin, NULL, FU_TYPE_NORDIC_HID_FIRMWARE_MCUBOOT);
|
||||
fu_context_add_quirk_key(ctx, "NordicHidBootloader");
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,9 @@ shared_module('fu_plugin_nordic_hid',
|
||||
sources : [
|
||||
'fu-plugin-nordic-hid.c',
|
||||
'fu-nordic-hid-cfg-channel.c',
|
||||
'fu-nordic-hid-firmware.c',
|
||||
'fu-nordic-hid-firmware-b0.c',
|
||||
'fu-nordic-hid-firmware-mcuboot.c',
|
||||
'fu-nordic-hid-archive.c',
|
||||
],
|
||||
include_directories : [
|
||||
|
BIN
plugins/nordic-hid/tests/nordic-hid-b0.bin
Normal file
BIN
plugins/nordic-hid/tests/nordic-hid-b0.bin
Normal file
Binary file not shown.
BIN
plugins/nordic-hid/tests/nordic-hid-mcuboot.bin
Normal file
BIN
plugins/nordic-hid/tests/nordic-hid-mcuboot.bin
Normal file
Binary file not shown.
3
plugins/nordic-hid/tests/nordic-hid-mcuboot.builder.xml
Normal file
3
plugins/nordic-hid/tests/nordic-hid-mcuboot.builder.xml
Normal file
@ -0,0 +1,3 @@
|
||||
<firmware gtype="FuNordicHidFirmwareMcuboot">
|
||||
<data>aGVsbG8gd29ybGQ=</data> <!-- base64 -->
|
||||
</firmware>
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user