Coverage Report

Created: 2026-08-13 07:13

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