/*
 * Copyright 2021 Denis Pynkin <denis.pynkin@collabora.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "fu-nordic-hid-firmware-b0.h"

#define UPDATE_IMAGE_MAGIC_COMMON 0x281ee6de
#define UPDATE_IMAGE_MAGIC_FWINFO 0x8fcebb4c
#define UPDATE_IMAGE_MAGIC_NRF52  0x00003402
#define UPDATE_IMAGE_MAGIC_NRF53  0x00003502

struct _FuNordicHidFirmwareB0 {
	FuNordicHidFirmware parent_instance;
};

G_DEFINE_TYPE(FuNordicHidFirmwareB0, fu_nordic_hid_firmware_b0, FU_TYPE_NORDIC_HID_FIRMWARE)

static GByteArray *
fu_nordic_hid_firmware_b0_write(FuFirmware *firmware, GError **error)
{
	g_autoptr(GByteArray) buf = g_byte_array_new();
	g_autoptr(GBytes) blob = NULL;
	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_with_patches(firmware, error);
	if (blob == NULL)
		return NULL;
	fu_byte_array_append_bytes(buf, blob);
	return g_steal_pointer(&buf);
}

static gboolean
fu_nordic_hid_firmware_b0_read_fwinfo(FuNordicHidFirmwareB0 *self,
				      GInputStream *stream,
				      GError **error)
{
	guint32 magic_common;
	guint32 magic_fwinfo;
	guint32 magic_compat;
	guint32 offset;
	/* nocheck:magic */
	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++) {
		offset = hdr_offset[i];
		if (!fu_input_stream_read_u32(stream,
					      offset,
					      &magic_common,
					      G_LITTLE_ENDIAN,
					      error))
			return FALSE;
		if (!fu_input_stream_read_u32(stream,
					      offset + 0x04,
					      &magic_fwinfo,
					      G_LITTLE_ENDIAN,
					      error))
			return FALSE;
		if (!fu_input_stream_read_u32(stream,
					      offset + 0x08,
					      &magic_compat,
					      G_LITTLE_ENDIAN,
					      error))
			return FALSE;
		/* version */
		if (!fu_input_stream_read_u32(stream,
					      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(FU_FIRMWARE(self), version);
			return TRUE;
		default:
			break;
		}
	}

	g_set_error_literal(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_INVALID_FILE,
			    "unable to validate the update binary");
	return FALSE;
}

static gboolean
fu_nordic_hid_firmware_b0_parse(FuFirmware *firmware,
				GInputStream *stream,
				FuFirmwareParseFlags flags,
				GError **error)
{
	FuNordicHidFirmwareB0 *self = FU_NORDIC_HID_FIRMWARE_B0(firmware);
	if (!FU_FIRMWARE_CLASS(fu_nordic_hid_firmware_b0_parent_class)
		 ->parse(firmware, stream, flags, error))
		return FALSE;
	return fu_nordic_hid_firmware_b0_read_fwinfo(self, stream, error);
}

static void
fu_nordic_hid_firmware_b0_init(FuNordicHidFirmwareB0 *self)
{
	fu_firmware_set_size_max(FU_FIRMWARE(self), 16 * FU_MB);
}

static void
fu_nordic_hid_firmware_b0_class_init(FuNordicHidFirmwareB0Class *klass)
{
	FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass);
	firmware_class->parse = fu_nordic_hid_firmware_b0_parse;
	firmware_class->write = fu_nordic_hid_firmware_b0_write;
}
