Coverage Report

Created: 2026-04-12 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fwupd/plugins/wacom-usb/fu-wacom-usb-firmware.c
Line
Count
Source
1
/*
2
 * Copyright 2018 Richard Hughes <richard@hughsie.com>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
#include "config.h"
8
9
#include <string.h>
10
11
#include "fu-wacom-usb-firmware.h"
12
#include "fu-wacom-usb-struct.h"
13
14
struct _FuWacomUsbFirmware {
15
  FuFirmware parent_instance;
16
};
17
18
1.89k
G_DEFINE_TYPE(FuWacomUsbFirmware, fu_wacom_usb_firmware, FU_TYPE_FIRMWARE)
19
1.89k
20
1.36M
#define FU_WACOM_USB_FIRMWARE_TOKENS_MAX   100000 /* lines */
21
1.82k
#define FU_WACOM_USB_FIRMWARE_SECTIONS_MAX 10
22
23
typedef struct {
24
  guint32 addr;
25
  guint32 sz;
26
  guint32 prog_start_addr;
27
} FuFirmwareWacHeaderRecord;
28
29
typedef struct {
30
  FuFirmware *firmware;
31
  FuFirmwareParseFlags flags;
32
  GPtrArray *header_infos;
33
  GString *image_buffer;
34
  guint8 images_cnt;
35
} FuWacomUsbFirmwareTokenHelper;
36
37
static gboolean
38
fu_wacom_usb_firmware_tokenize_cb(GString *token,
39
          guint token_idx,
40
          gpointer user_data,
41
          GError **error)
42
1.36M
{
43
1.36M
  FuWacomUsbFirmwareTokenHelper *helper = (FuWacomUsbFirmwareTokenHelper *)user_data;
44
1.36M
  g_autofree gchar *cmd = NULL;
45
46
  /* sanity check */
47
1.36M
  if (token_idx > FU_WACOM_USB_FIRMWARE_TOKENS_MAX) {
48
1
    g_set_error_literal(error,
49
1
            FWUPD_ERROR,
50
1
            FWUPD_ERROR_INVALID_DATA,
51
1
            "file has too many lines");
52
1
    return FALSE;
53
1
  }
54
55
  /* remove WIN32 line endings */
56
1.36M
  g_strdelimit(token->str, "\r\x1a", '\0');
57
1.36M
  token->len = strlen(token->str);
58
59
  /* ignore blank lines */
60
1.36M
  cmd = g_strndup(token->str, 2);
61
1.36M
  if (g_strcmp0(cmd, "") == 0)
62
453k
    return TRUE;
63
64
  /* custom metadata */
65
913k
  if (g_strcmp0(cmd, "WA") == 0) {
66
    /* header info record */
67
5.34k
    if (token->len > 3 && memcmp(token->str + 2, "COM", 3) == 0) {
68
1.89k
      guint8 header_image_cnt = 0;
69
1.89k
      if (token->len != 40) {
70
68
        g_set_error(error,
71
68
              FWUPD_ERROR,
72
68
              FWUPD_ERROR_INTERNAL,
73
68
              "invalid header, got %" G_GSIZE_FORMAT " bytes",
74
68
              token->len);
75
68
        return FALSE;
76
68
      }
77
78
      /* sanity check */
79
1.82k
      if (helper->header_infos->len > FU_WACOM_USB_FIRMWARE_SECTIONS_MAX) {
80
2
        g_set_error(error,
81
2
              FWUPD_ERROR,
82
2
              FWUPD_ERROR_INTERNAL,
83
2
              "too many metadata sections: %u",
84
2
              helper->header_infos->len);
85
2
        return FALSE;
86
2
      }
87
1.82k
      if (!fu_firmware_strparse_uint4_safe(token->str,
88
1.82k
                   token->len,
89
1.82k
                   5,
90
1.82k
                   &header_image_cnt,
91
1.82k
                   error))
92
1
        return FALSE;
93
4.37k
      for (guint j = 0; j < header_image_cnt; j++) {
94
2.61k
        g_autofree FuFirmwareWacHeaderRecord *hdr = NULL;
95
2.61k
        hdr = g_new0(FuFirmwareWacHeaderRecord, 1);
96
2.61k
        if (!fu_firmware_strparse_uint32_safe(token->str,
97
2.61k
                      token->len,
98
2.61k
                      (j * 16) + 6,
99
2.61k
                      &hdr->addr,
100
2.61k
                      error))
101
40
          return FALSE;
102
2.57k
        if (!fu_firmware_strparse_uint32_safe(token->str,
103
2.57k
                      token->len,
104
2.57k
                      (j * 16) + 14,
105
2.57k
                      &hdr->sz,
106
2.57k
                      error))
107
23
          return FALSE;
108
2.54k
        g_debug("header_fw%u_addr: 0x%x", j, hdr->addr);
109
2.54k
        g_debug("header_fw%u_sz:   0x%x", j, hdr->sz);
110
2.54k
        g_ptr_array_add(helper->header_infos, g_steal_pointer(&hdr));
111
2.54k
      }
112
1.76k
      return TRUE;
113
1.82k
    }
114
115
    /* firmware headline record */
116
3.45k
    if (token->len == 13) {
117
831
      FuFirmwareWacHeaderRecord *hdr;
118
831
      guint8 idx = 0;
119
831
      if (!fu_firmware_strparse_uint4_safe(token->str,
120
831
                   token->len,
121
831
                   2,
122
831
                   &idx,
123
831
                   error))
124
4
        return FALSE;
125
827
      if (idx == 0) {
126
1
        g_set_error(error,
127
1
              FWUPD_ERROR,
128
1
              FWUPD_ERROR_INTERNAL,
129
1
              "headline %u invalid",
130
1
              idx);
131
1
        return FALSE;
132
1
      }
133
826
      if (idx > helper->header_infos->len) {
134
5
        g_set_error(error,
135
5
              FWUPD_ERROR,
136
5
              FWUPD_ERROR_INTERNAL,
137
5
              "headline %u exceeds header count %u",
138
5
              idx,
139
5
              helper->header_infos->len);
140
5
        return FALSE;
141
5
      }
142
821
      if (idx - 1 != helper->images_cnt) {
143
9
        g_set_error(error,
144
9
              FWUPD_ERROR,
145
9
              FWUPD_ERROR_INTERNAL,
146
9
              "headline %u is not in sorted order",
147
9
              idx);
148
9
        return FALSE;
149
9
      }
150
812
      hdr = g_ptr_array_index(helper->header_infos, idx - 1);
151
812
      if (!fu_firmware_strparse_uint32_safe(token->str,
152
812
                    token->len,
153
812
                    3,
154
812
                    &hdr->prog_start_addr,
155
812
                    error))
156
1
        return FALSE;
157
811
      if (hdr->prog_start_addr != hdr->addr) {
158
46
        g_set_error(error,
159
46
              FWUPD_ERROR,
160
46
              FWUPD_ERROR_INTERNAL,
161
46
              "programming address 0x%x != "
162
46
              "base address 0x%0x for idx %u",
163
46
              hdr->prog_start_addr,
164
46
              hdr->addr,
165
46
              idx);
166
46
        return FALSE;
167
46
      }
168
765
      g_debug("programing-start-address: 0x%x", hdr->prog_start_addr);
169
765
      return TRUE;
170
811
    }
171
172
2.61k
    g_debug("unknown Wacom-specific metadata");
173
2.61k
    return TRUE;
174
3.45k
  }
175
176
  /* start */
177
908k
  if (g_strcmp0(cmd, "S0") == 0) {
178
2.30k
    if (helper->image_buffer->len > 0) {
179
21
      g_set_error_literal(error,
180
21
              FWUPD_ERROR,
181
21
              FWUPD_ERROR_INTERNAL,
182
21
              "duplicate S0 without S7");
183
21
      return FALSE;
184
21
    }
185
2.28k
    g_string_append_printf(helper->image_buffer, "%s\n", token->str);
186
2.28k
    return TRUE;
187
2.30k
  }
188
189
  /* these are things we want to include in the image */
190
906k
  if (g_strcmp0(cmd, "S1") == 0 || g_strcmp0(cmd, "S2") == 0 || g_strcmp0(cmd, "S3") == 0 ||
191
10.1k
      g_strcmp0(cmd, "S5") == 0 || g_strcmp0(cmd, "S7") == 0 || g_strcmp0(cmd, "S8") == 0 ||
192
906k
      g_strcmp0(cmd, "S9") == 0) {
193
906k
    if (helper->image_buffer->len == 0) {
194
20
      g_set_error(error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "%s without S0", cmd);
195
20
      return FALSE;
196
20
    }
197
906k
    g_string_append_printf(helper->image_buffer, "%s\n", token->str);
198
906k
  } else {
199
66
    g_set_error(error,
200
66
          FWUPD_ERROR,
201
66
          FWUPD_ERROR_INTERNAL,
202
66
          "invalid SREC command on line %u: %s",
203
66
          token_idx + 1,
204
66
          cmd);
205
66
    return FALSE;
206
66
  }
207
208
  /* end */
209
906k
  if (g_strcmp0(cmd, "S7") == 0) {
210
2.02k
    g_autoptr(GBytes) blob = NULL;
211
2.02k
    g_autoptr(GBytes) fw_srec = NULL;
212
2.02k
    g_autoptr(FuFirmware) firmware_srec = fu_srec_firmware_new();
213
2.02k
    g_autoptr(FuFirmware) img = fu_firmware_new();
214
2.02k
    FuFirmwareWacHeaderRecord *hdr;
215
216
    /* get the correct relocated start address */
217
2.02k
    if (helper->images_cnt >= helper->header_infos->len) {
218
32
      g_set_error(error,
219
32
            FWUPD_ERROR,
220
32
            FWUPD_ERROR_INTERNAL,
221
32
            "%s without header",
222
32
            cmd);
223
32
      return FALSE;
224
32
    }
225
1.98k
    hdr = g_ptr_array_index(helper->header_infos, helper->images_cnt);
226
227
1.98k
    if (helper->image_buffer->len == 0) {
228
0
      g_set_error(error,
229
0
            FWUPD_ERROR,
230
0
            FWUPD_ERROR_INTERNAL,
231
0
            "%s with missing image buffer",
232
0
            cmd);
233
0
      return FALSE;
234
0
    }
235
236
    /* parse SREC file and add as image */
237
1.98k
    blob = g_bytes_new(helper->image_buffer->str, helper->image_buffer->len);
238
1.98k
    fu_srec_firmware_set_addr_min(FU_SREC_FIRMWARE(firmware_srec), hdr->addr);
239
1.98k
    if (!fu_firmware_parse_bytes(firmware_srec,
240
1.98k
               blob,
241
1.98k
               0x0,
242
1.98k
               helper->flags | FU_FIRMWARE_PARSE_FLAG_NO_SEARCH,
243
1.98k
               error))
244
352
      return FALSE;
245
1.63k
    fw_srec = fu_firmware_get_bytes(firmware_srec, error);
246
1.63k
    if (fw_srec == NULL)
247
0
      return FALSE;
248
1.63k
    fu_firmware_set_bytes(img, fw_srec);
249
1.63k
    fu_firmware_set_addr(img, fu_firmware_get_addr(firmware_srec));
250
1.63k
    fu_firmware_set_idx(img, helper->images_cnt);
251
1.63k
    if (!fu_firmware_add_image(helper->firmware, img, error))
252
0
      return FALSE;
253
1.63k
    helper->images_cnt++;
254
255
    /* clear the image buffer */
256
1.63k
    g_string_set_size(helper->image_buffer, 0);
257
1.63k
  }
258
259
  /* success */
260
905k
  return TRUE;
261
906k
}
262
263
static gboolean
264
fu_wacom_usb_firmware_validate(FuFirmware *firmware,
265
             GInputStream *stream,
266
             gsize offset,
267
             GError **error)
268
1.89k
{
269
1.89k
  return fu_struct_wacom_usb_firmware_hdr_validate_stream(stream, offset, error);
270
1.89k
}
271
272
static gboolean
273
fu_wacom_usb_firmware_parse(FuFirmware *firmware,
274
          GInputStream *stream,
275
          FuFirmwareParseFlags flags,
276
          GError **error)
277
1.83k
{
278
1.83k
  g_autoptr(GPtrArray) header_infos = g_ptr_array_new_with_free_func(g_free);
279
1.83k
  g_autoptr(GString) image_buffer = g_string_new(NULL);
280
1.83k
  FuWacomUsbFirmwareTokenHelper helper = {.firmware = firmware,
281
1.83k
            .flags = flags,
282
1.83k
            .header_infos = header_infos,
283
1.83k
            .image_buffer = image_buffer,
284
1.83k
            .images_cnt = 0};
285
286
  /* tokenize */
287
1.83k
  if (!fu_strsplit_stream(stream,
288
1.83k
        0x0,
289
1.83k
        "\n",
290
1.83k
        fu_wacom_usb_firmware_tokenize_cb,
291
1.83k
        &helper,
292
1.83k
        error))
293
692
    return FALSE;
294
295
  /* verify data is complete */
296
1.14k
  if (helper.image_buffer->len > 0) {
297
226
    g_set_error_literal(error,
298
226
            FWUPD_ERROR,
299
226
            FWUPD_ERROR_INTERNAL,
300
226
            "truncated data: no S7");
301
226
    return FALSE;
302
226
  }
303
304
  /* ensure this matched the header */
305
920
  if (helper.header_infos->len != helper.images_cnt) {
306
65
    g_set_error(error,
307
65
          FWUPD_ERROR,
308
65
          FWUPD_ERROR_INTERNAL,
309
65
          "not enough images %u for header count %u",
310
65
          helper.images_cnt,
311
65
          header_infos->len);
312
65
    return FALSE;
313
65
  }
314
315
  /* success */
316
855
  return TRUE;
317
920
}
318
319
static guint8
320
fu_wacom_usb_firmware_calc_checksum(GByteArray *buf)
321
1.08k
{
322
1.08k
  return fu_sum8(buf->data, buf->len) ^ 0xFF;
323
1.08k
}
324
325
static GByteArray *
326
fu_wacom_usb_firmware_write(FuFirmware *firmware, GError **error)
327
855
{
328
855
  g_autoptr(GPtrArray) images = fu_firmware_get_images(firmware);
329
855
  g_autoptr(GString) str = g_string_new(NULL);
330
855
  g_autoptr(GByteArray) buf = g_byte_array_new();
331
855
  g_autoptr(GByteArray) buf_hdr = g_byte_array_new();
332
333
  /* fw header */
334
855
  if (images->len == 0) {
335
528
    g_set_error_literal(error,
336
528
            FWUPD_ERROR,
337
528
            FWUPD_ERROR_NOT_SUPPORTED,
338
528
            "no firmware images found");
339
528
    return NULL;
340
528
  }
341
1.08k
  for (guint i = 0; i < images->len; i++) {
342
757
    FuFirmware *img = g_ptr_array_index(images, i);
343
757
    fu_byte_array_append_uint32(buf_hdr, fu_firmware_get_addr(img), G_BIG_ENDIAN);
344
757
    fu_byte_array_append_uint32(buf_hdr, fu_firmware_get_size(img), G_BIG_ENDIAN);
345
757
  }
346
327
  g_string_append_printf(str, "WACOM%u", images->len);
347
6.38k
  for (guint i = 0; i < buf_hdr->len; i++)
348
6.05k
    g_string_append_printf(str, "%02X", buf_hdr->data[i]);
349
327
  g_string_append_printf(str, "%02X\n", fu_wacom_usb_firmware_calc_checksum(buf_hdr));
350
351
  /* payload */
352
1.08k
  for (guint i = 0; i < images->len; i++) {
353
757
    FuFirmware *img = g_ptr_array_index(images, i);
354
757
    g_autoptr(GBytes) img_blob = NULL;
355
757
    g_autoptr(GByteArray) buf_img = g_byte_array_new();
356
357
    /* img header */
358
757
    g_string_append_printf(str, "WA%u", (guint)fu_firmware_get_idx(img) + 1);
359
757
    fu_byte_array_append_uint32(buf_img, fu_firmware_get_addr(img), G_BIG_ENDIAN);
360
3.78k
    for (guint j = 0; j < buf_img->len; j++)
361
3.02k
      g_string_append_printf(str, "%02X", buf_img->data[j]);
362
757
    g_string_append_printf(str, "%02X\n", fu_wacom_usb_firmware_calc_checksum(buf_img));
363
364
    /* srec */
365
757
    img_blob = fu_firmware_write(img, error);
366
757
    if (img_blob == NULL)
367
0
      return NULL;
368
757
    g_string_append_len(str,
369
757
            (const gchar *)g_bytes_get_data(img_blob, NULL),
370
757
            g_bytes_get_size(img_blob));
371
757
  }
372
373
  /* success */
374
327
  g_byte_array_append(buf, (const guint8 *)str->str, str->len);
375
327
  return g_steal_pointer(&buf);
376
327
}
377
378
static void
379
fu_wacom_usb_firmware_init(FuWacomUsbFirmware *self)
380
1.89k
{
381
1.89k
  fu_firmware_set_images_max(FU_FIRMWARE(self), 1024);
382
1.89k
  fu_firmware_set_size_max(FU_FIRMWARE(self), 256 * FU_MB);
383
1.89k
  fu_firmware_add_image_gtype(FU_FIRMWARE(self), FU_TYPE_FIRMWARE);
384
1.89k
  fu_firmware_add_image_gtype(FU_FIRMWARE(self), FU_TYPE_SREC_FIRMWARE);
385
1.89k
}
386
387
static void
388
fu_wacom_usb_firmware_class_init(FuWacomUsbFirmwareClass *klass)
389
1
{
390
1
  FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass);
391
1
  firmware_class->validate = fu_wacom_usb_firmware_validate;
392
1
  firmware_class->parse = fu_wacom_usb_firmware_parse;
393
1
  firmware_class->write = fu_wacom_usb_firmware_write;
394
1
}
395
396
FuFirmware *
397
fu_wacom_usb_firmware_new(void)
398
0
{
399
0
  return FU_FIRMWARE(g_object_new(FU_TYPE_WACOM_USB_FIRMWARE, NULL));
400
0
}