Coverage Report

Created: 2026-02-26 06:27

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.93k
G_DEFINE_TYPE(FuWacomUsbFirmware, fu_wacom_usb_firmware, FU_TYPE_FIRMWARE)
19
1.93k
20
1.09M
#define FU_WACOM_USB_FIRMWARE_TOKENS_MAX   100000 /* lines */
21
1.73k
#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.09M
{
43
1.09M
  FuWacomUsbFirmwareTokenHelper *helper = (FuWacomUsbFirmwareTokenHelper *)user_data;
44
1.09M
  g_autofree gchar *cmd = NULL;
45
46
  /* sanity check */
47
1.09M
  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.09M
  g_strdelimit(token->str, "\r\x1a", '\0');
57
1.09M
  token->len = strlen(token->str);
58
59
  /* ignore blank lines */
60
1.09M
  cmd = g_strndup(token->str, 2);
61
1.09M
  if (g_strcmp0(cmd, "") == 0)
62
260k
    return TRUE;
63
64
  /* custom metadata */
65
834k
  if (g_strcmp0(cmd, "WA") == 0) {
66
    /* header info record */
67
5.06k
    if (token->len > 3 && memcmp(token->str + 2, "COM", 3) == 0) {
68
1.82k
      guint8 header_image_cnt = 0;
69
1.82k
      if (token->len != 40) {
70
83
        g_set_error(error,
71
83
              FWUPD_ERROR,
72
83
              FWUPD_ERROR_INTERNAL,
73
83
              "invalid header, got %" G_GSIZE_FORMAT " bytes",
74
83
              token->len);
75
83
        return FALSE;
76
83
      }
77
78
      /* sanity check */
79
1.73k
      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.73k
      if (!fu_firmware_strparse_uint4_safe(token->str,
88
1.73k
                   token->len,
89
1.73k
                   5,
90
1.73k
                   &header_image_cnt,
91
1.73k
                   error))
92
1
        return FALSE;
93
4.13k
      for (guint j = 0; j < header_image_cnt; j++) {
94
2.46k
        g_autofree FuFirmwareWacHeaderRecord *hdr = NULL;
95
2.46k
        hdr = g_new0(FuFirmwareWacHeaderRecord, 1);
96
2.46k
        if (!fu_firmware_strparse_uint32_safe(token->str,
97
2.46k
                      token->len,
98
2.46k
                      (j * 16) + 6,
99
2.46k
                      &hdr->addr,
100
2.46k
                      error))
101
37
          return FALSE;
102
2.42k
        if (!fu_firmware_strparse_uint32_safe(token->str,
103
2.42k
                      token->len,
104
2.42k
                      (j * 16) + 14,
105
2.42k
                      &hdr->sz,
106
2.42k
                      error))
107
26
          return FALSE;
108
2.40k
        g_debug("header_fw%u_addr: 0x%x", j, hdr->addr);
109
2.40k
        g_debug("header_fw%u_sz:   0x%x", j, hdr->sz);
110
2.40k
        g_ptr_array_add(helper->header_infos, g_steal_pointer(&hdr));
111
2.40k
      }
112
1.67k
      return TRUE;
113
1.73k
    }
114
115
    /* firmware headline record */
116
3.23k
    if (token->len == 13) {
117
565
      FuFirmwareWacHeaderRecord *hdr;
118
565
      guint8 idx = 0;
119
565
      if (!fu_firmware_strparse_uint4_safe(token->str,
120
565
                   token->len,
121
565
                   2,
122
565
                   &idx,
123
565
                   error))
124
3
        return FALSE;
125
562
      if (idx == 0) {
126
2
        g_set_error(error,
127
2
              FWUPD_ERROR,
128
2
              FWUPD_ERROR_INTERNAL,
129
2
              "headline %u invalid",
130
2
              idx);
131
2
        return FALSE;
132
2
      }
133
560
      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
555
      if (idx - 1 != helper->images_cnt) {
143
5
        g_set_error(error,
144
5
              FWUPD_ERROR,
145
5
              FWUPD_ERROR_INTERNAL,
146
5
              "headline %u is not in sorted order",
147
5
              idx);
148
5
        return FALSE;
149
5
      }
150
550
      hdr = g_ptr_array_index(helper->header_infos, idx - 1);
151
550
      if (!fu_firmware_strparse_uint32_safe(token->str,
152
550
                    token->len,
153
550
                    3,
154
550
                    &hdr->prog_start_addr,
155
550
                    error))
156
2
        return FALSE;
157
548
      if (hdr->prog_start_addr != hdr->addr) {
158
44
        g_set_error(error,
159
44
              FWUPD_ERROR,
160
44
              FWUPD_ERROR_INTERNAL,
161
44
              "programming address 0x%x != "
162
44
              "base address 0x%0x for idx %u",
163
44
              hdr->prog_start_addr,
164
44
              hdr->addr,
165
44
              idx);
166
44
        return FALSE;
167
44
      }
168
504
      g_debug("programing-start-address: 0x%x", hdr->prog_start_addr);
169
504
      return TRUE;
170
548
    }
171
172
2.67k
    g_debug("unknown Wacom-specific metadata");
173
2.67k
    return TRUE;
174
3.23k
  }
175
176
  /* start */
177
829k
  if (g_strcmp0(cmd, "S0") == 0) {
178
2.16k
    if (helper->image_buffer->len > 0) {
179
7
      g_set_error_literal(error,
180
7
              FWUPD_ERROR,
181
7
              FWUPD_ERROR_INTERNAL,
182
7
              "duplicate S0 without S7");
183
7
      return FALSE;
184
7
    }
185
2.16k
    g_string_append_printf(helper->image_buffer, "%s\n", token->str);
186
2.16k
    return TRUE;
187
2.16k
  }
188
189
  /* these are things we want to include in the image */
190
827k
  if (g_strcmp0(cmd, "S1") == 0 || g_strcmp0(cmd, "S2") == 0 || g_strcmp0(cmd, "S3") == 0 ||
191
9.22k
      g_strcmp0(cmd, "S5") == 0 || g_strcmp0(cmd, "S7") == 0 || g_strcmp0(cmd, "S8") == 0 ||
192
827k
      g_strcmp0(cmd, "S9") == 0) {
193
827k
    if (helper->image_buffer->len == 0) {
194
25
      g_set_error(error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "%s without S0", cmd);
195
25
      return FALSE;
196
25
    }
197
827k
    g_string_append_printf(helper->image_buffer, "%s\n", token->str);
198
827k
  } else {
199
156
    g_set_error(error,
200
156
          FWUPD_ERROR,
201
156
          FWUPD_ERROR_INTERNAL,
202
156
          "invalid SREC command on line %u: %s",
203
156
          token_idx + 1,
204
156
          cmd);
205
156
    return FALSE;
206
156
  }
207
208
  /* end */
209
827k
  if (g_strcmp0(cmd, "S7") == 0) {
210
1.93k
    g_autoptr(GBytes) blob = NULL;
211
1.93k
    g_autoptr(GBytes) fw_srec = NULL;
212
1.93k
    g_autoptr(FuFirmware) firmware_srec = fu_srec_firmware_new();
213
1.93k
    g_autoptr(FuFirmware) img = fu_firmware_new();
214
1.93k
    FuFirmwareWacHeaderRecord *hdr;
215
216
    /* get the correct relocated start address */
217
1.93k
    if (helper->images_cnt >= helper->header_infos->len) {
218
24
      g_set_error(error,
219
24
            FWUPD_ERROR,
220
24
            FWUPD_ERROR_INTERNAL,
221
24
            "%s without header",
222
24
            cmd);
223
24
      return FALSE;
224
24
    }
225
1.90k
    hdr = g_ptr_array_index(helper->header_infos, helper->images_cnt);
226
227
1.90k
    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.90k
    blob = g_bytes_new(helper->image_buffer->str, helper->image_buffer->len);
238
1.90k
    fu_srec_firmware_set_addr_min(FU_SREC_FIRMWARE(firmware_srec), hdr->addr);
239
1.90k
    if (!fu_firmware_parse_bytes(firmware_srec,
240
1.90k
               blob,
241
1.90k
               0x0,
242
1.90k
               helper->flags | FU_FIRMWARE_PARSE_FLAG_NO_SEARCH,
243
1.90k
               error))
244
365
      return FALSE;
245
1.54k
    fw_srec = fu_firmware_get_bytes(firmware_srec, error);
246
1.54k
    if (fw_srec == NULL)
247
0
      return FALSE;
248
1.54k
    fu_firmware_set_bytes(img, fw_srec);
249
1.54k
    fu_firmware_set_addr(img, fu_firmware_get_addr(firmware_srec));
250
1.54k
    fu_firmware_set_idx(img, helper->images_cnt);
251
1.54k
    if (!fu_firmware_add_image(helper->firmware, img, error))
252
0
      return FALSE;
253
1.54k
    helper->images_cnt++;
254
255
    /* clear the image buffer */
256
1.54k
    g_string_set_size(helper->image_buffer, 0);
257
1.54k
  }
258
259
  /* success */
260
827k
  return TRUE;
261
827k
}
262
263
static gboolean
264
fu_wacom_usb_firmware_validate(FuFirmware *firmware,
265
             GInputStream *stream,
266
             gsize offset,
267
             GError **error)
268
1.93k
{
269
1.93k
  return fu_struct_wacom_usb_firmware_hdr_validate_stream(stream, offset, error);
270
1.93k
}
271
272
static gboolean
273
fu_wacom_usb_firmware_parse(FuFirmware *firmware,
274
          GInputStream *stream,
275
          FuFirmwareParseFlags flags,
276
          GError **error)
277
1.87k
{
278
1.87k
  g_autoptr(GPtrArray) header_infos = g_ptr_array_new_with_free_func(g_free);
279
1.87k
  g_autoptr(GString) image_buffer = g_string_new(NULL);
280
1.87k
  FuWacomUsbFirmwareTokenHelper helper = {.firmware = firmware,
281
1.87k
            .flags = flags,
282
1.87k
            .header_infos = header_infos,
283
1.87k
            .image_buffer = image_buffer,
284
1.87k
            .images_cnt = 0};
285
286
  /* tokenize */
287
1.87k
  if (!fu_strsplit_stream(stream,
288
1.87k
        0x0,
289
1.87k
        "\n",
290
1.87k
        fu_wacom_usb_firmware_tokenize_cb,
291
1.87k
        &helper,
292
1.87k
        error))
293
788
    return FALSE;
294
295
  /* verify data is complete */
296
1.08k
  if (helper.image_buffer->len > 0) {
297
199
    g_set_error_literal(error,
298
199
            FWUPD_ERROR,
299
199
            FWUPD_ERROR_INTERNAL,
300
199
            "truncated data: no S7");
301
199
    return FALSE;
302
199
  }
303
304
  /* ensure this matched the header */
305
885
  if (helper.header_infos->len != helper.images_cnt) {
306
58
    g_set_error(error,
307
58
          FWUPD_ERROR,
308
58
          FWUPD_ERROR_INTERNAL,
309
58
          "not enough images %u for header count %u",
310
58
          helper.images_cnt,
311
58
          header_infos->len);
312
58
    return FALSE;
313
58
  }
314
315
  /* success */
316
827
  return TRUE;
317
885
}
318
319
static guint8
320
fu_wacom_usb_firmware_calc_checksum(GByteArray *buf)
321
1.00k
{
322
1.00k
  return fu_sum8(buf->data, buf->len) ^ 0xFF;
323
1.00k
}
324
325
static GByteArray *
326
fu_wacom_usb_firmware_write(FuFirmware *firmware, GError **error)
327
827
{
328
827
  g_autoptr(GPtrArray) images = fu_firmware_get_images(firmware);
329
827
  g_autoptr(GString) str = g_string_new(NULL);
330
827
  g_autoptr(GByteArray) buf = g_byte_array_new();
331
827
  g_autoptr(GByteArray) buf_hdr = g_byte_array_new();
332
333
  /* fw header */
334
827
  if (images->len == 0) {
335
520
    g_set_error_literal(error,
336
520
            FWUPD_ERROR,
337
520
            FWUPD_ERROR_NOT_SUPPORTED,
338
520
            "no firmware images found");
339
520
    return NULL;
340
520
  }
341
1.00k
  for (guint i = 0; i < images->len; i++) {
342
694
    FuFirmware *img = g_ptr_array_index(images, i);
343
694
    fu_byte_array_append_uint32(buf_hdr, fu_firmware_get_addr(img), G_BIG_ENDIAN);
344
694
    fu_byte_array_append_uint32(buf_hdr, fu_firmware_get_size(img), G_BIG_ENDIAN);
345
694
  }
346
307
  g_string_append_printf(str, "WACOM%u", images->len);
347
5.85k
  for (guint i = 0; i < buf_hdr->len; i++)
348
5.55k
    g_string_append_printf(str, "%02X", buf_hdr->data[i]);
349
307
  g_string_append_printf(str, "%02X\n", fu_wacom_usb_firmware_calc_checksum(buf_hdr));
350
351
  /* payload */
352
1.00k
  for (guint i = 0; i < images->len; i++) {
353
694
    FuFirmware *img = g_ptr_array_index(images, i);
354
694
    g_autoptr(GBytes) img_blob = NULL;
355
694
    g_autoptr(GByteArray) buf_img = g_byte_array_new();
356
357
    /* img header */
358
694
    g_string_append_printf(str, "WA%u", (guint)fu_firmware_get_idx(img) + 1);
359
694
    fu_byte_array_append_uint32(buf_img, fu_firmware_get_addr(img), G_BIG_ENDIAN);
360
3.47k
    for (guint j = 0; j < buf_img->len; j++)
361
2.77k
      g_string_append_printf(str, "%02X", buf_img->data[j]);
362
694
    g_string_append_printf(str, "%02X\n", fu_wacom_usb_firmware_calc_checksum(buf_img));
363
364
    /* srec */
365
694
    img_blob = fu_firmware_write(img, error);
366
694
    if (img_blob == NULL)
367
0
      return NULL;
368
694
    g_string_append_len(str,
369
694
            (const gchar *)g_bytes_get_data(img_blob, NULL),
370
694
            g_bytes_get_size(img_blob));
371
694
  }
372
373
  /* success */
374
307
  g_byte_array_append(buf, (const guint8 *)str->str, str->len);
375
307
  return g_steal_pointer(&buf);
376
307
}
377
378
static void
379
fu_wacom_usb_firmware_init(FuWacomUsbFirmware *self)
380
1.93k
{
381
1.93k
  fu_firmware_set_images_max(FU_FIRMWARE(self), 1024);
382
1.93k
  fu_firmware_add_image_gtype(FU_FIRMWARE(self), FU_TYPE_FIRMWARE);
383
1.93k
  fu_firmware_add_image_gtype(FU_FIRMWARE(self), FU_TYPE_SREC_FIRMWARE);
384
1.93k
}
385
386
static void
387
fu_wacom_usb_firmware_class_init(FuWacomUsbFirmwareClass *klass)
388
1
{
389
1
  FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass);
390
1
  firmware_class->validate = fu_wacom_usb_firmware_validate;
391
1
  firmware_class->parse = fu_wacom_usb_firmware_parse;
392
1
  firmware_class->write = fu_wacom_usb_firmware_write;
393
1
}
394
395
FuFirmware *
396
fu_wacom_usb_firmware_new(void)
397
0
{
398
0
  return FU_FIRMWARE(g_object_new(FU_TYPE_WACOM_USB_FIRMWARE, NULL));
399
0
}