Coverage Report

Created: 2025-10-10 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fwupd/plugins/ebitdo/fu-ebitdo-firmware.c
Line
Count
Source
1
/*
2
 * Copyright 2016 Richard Hughes <richard@hughsie.com>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
#include "config.h"
8
9
#include "fu-ebitdo-firmware.h"
10
#include "fu-ebitdo-struct.h"
11
12
struct _FuEbitdoFirmware {
13
  FuFirmware parent_instance;
14
};
15
16
318
G_DEFINE_TYPE(FuEbitdoFirmware, fu_ebitdo_firmware, FU_TYPE_FIRMWARE)
17
318
18
318
static gboolean
19
318
fu_ebitdo_firmware_parse(FuFirmware *firmware,
20
318
       GInputStream *stream,
21
318
       FuFirmwareParseFlags flags,
22
318
       GError **error)
23
318
{
24
318
  guint32 payload_len;
25
318
  guint32 version;
26
318
  gsize streamsz = 0;
27
318
  g_autoptr(FuFirmware) img_hdr = fu_firmware_new();
28
318
  g_autoptr(FuStructEbitdoHdr) st = NULL;
29
318
  g_autoptr(GInputStream) stream_hdr = NULL;
30
318
  g_autoptr(GInputStream) stream_payload = NULL;
31
32
  /* check the file size */
33
318
  st = fu_struct_ebitdo_hdr_parse_stream(stream, 0x0, error);
34
318
  if (st == NULL)
35
23
    return FALSE;
36
295
  if (!fu_input_stream_size(stream, &streamsz, error))
37
0
    return FALSE;
38
295
  payload_len = (guint32)(streamsz - st->len);
39
295
  if (payload_len != fu_struct_ebitdo_hdr_get_destination_len(st)) {
40
71
    g_set_error(error,
41
71
          FWUPD_ERROR,
42
71
          FWUPD_ERROR_INVALID_DATA,
43
71
          "file size incorrect, expected 0x%04x got 0x%04x",
44
71
          (guint)fu_struct_ebitdo_hdr_get_destination_len(st),
45
71
          (guint)payload_len);
46
71
    return FALSE;
47
71
  }
48
49
  /* parse version */
50
224
  version = fu_struct_ebitdo_hdr_get_version(st);
51
224
  fu_firmware_set_version_raw(firmware, version);
52
53
  /* add header */
54
224
  stream_hdr = fu_partial_input_stream_new(stream, 0x0, st->len, error);
55
224
  if (stream_hdr == NULL)
56
0
    return FALSE;
57
224
  if (!fu_firmware_parse_stream(img_hdr, stream_hdr, 0x0, flags, error))
58
0
    return FALSE;
59
224
  fu_firmware_set_id(img_hdr, FU_FIRMWARE_ID_HEADER);
60
224
  if (!fu_firmware_add_image(firmware, img_hdr, error))
61
0
    return FALSE;
62
63
  /* add payload */
64
224
  stream_payload = fu_partial_input_stream_new(stream, st->len, payload_len, error);
65
224
  if (stream_payload == NULL)
66
0
    return FALSE;
67
224
  if (!fu_firmware_set_stream(firmware, stream_payload, error))
68
0
    return FALSE;
69
224
  fu_firmware_set_id(firmware, FU_FIRMWARE_ID_PAYLOAD);
70
224
  fu_firmware_set_addr(firmware, fu_struct_ebitdo_hdr_get_destination_addr(st));
71
224
  return TRUE;
72
224
}
73
74
static GByteArray *
75
fu_ebitdo_firmware_write(FuFirmware *firmware, GError **error)
76
224
{
77
224
  g_autoptr(FuStructEbitdoHdr) st = fu_struct_ebitdo_hdr_new();
78
224
  g_autoptr(GBytes) blob = NULL;
79
80
  /* header then payload */
81
224
  blob = fu_firmware_get_bytes_with_patches(firmware, error);
82
224
  if (blob == NULL)
83
75
    return NULL;
84
149
  fu_struct_ebitdo_hdr_set_version(st, fu_firmware_get_version_raw(firmware));
85
149
  fu_struct_ebitdo_hdr_set_destination_addr(st, fu_firmware_get_addr(firmware));
86
149
  fu_struct_ebitdo_hdr_set_destination_len(st, g_bytes_get_size(blob));
87
149
  fu_byte_array_append_bytes(st, blob);
88
149
  return g_steal_pointer(&st);
89
224
}
90
91
static gchar *
92
fu_ebitdo_firmware_convert_version(FuFirmware *firmware, guint64 version_raw)
93
224
{
94
224
  return g_strdup_printf("%.2f", version_raw / 100.f);
95
224
}
96
97
static void
98
fu_ebitdo_firmware_init(FuEbitdoFirmware *self)
99
318
{
100
318
  fu_firmware_set_version_format(FU_FIRMWARE(self), FWUPD_VERSION_FORMAT_PAIR);
101
318
}
102
103
static void
104
fu_ebitdo_firmware_class_init(FuEbitdoFirmwareClass *klass)
105
1
{
106
1
  FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass);
107
1
  firmware_class->convert_version = fu_ebitdo_firmware_convert_version;
108
1
  firmware_class->parse = fu_ebitdo_firmware_parse;
109
1
  firmware_class->write = fu_ebitdo_firmware_write;
110
1
}