Coverage Report

Created: 2026-10-28 07:47

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.67k
G_DEFINE_TYPE(FuWacomUsbFirmware, fu_wacom_usb_firmware, FU_TYPE_FIRMWARE)
19
1.67k
20
2.07M
#define FU_WACOM_USB_FIRMWARE_TOKENS_MAX   100000 /* lines */
21
1.93k
#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.07M
{
43
2.07M
  FuWacomUsbFirmwareTokenHelper *helper = (FuWacomUsbFirmwareTokenHelper *)user_data;
44
2.07M
  g_autofree gchar *cmd = NULL;
45
46
  /* sanity check */
47
2.07M
  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.07M
  g_strdelimit(token->str, "\r\x1a", '\0');
57
2.07M
  token->len = strlen(token->str);
58
59
  /* ignore blank lines */
60
2.07M
  cmd = g_strndup(token->str, 2);
61
2.07M
  if (g_strcmp0(cmd, "") == 0)
62
1.09M
    return TRUE;
63
64
  /* custom metadata */
65
979k
  if (g_strcmp0(cmd, "WA") == 0) {
66
    /* header info record */
67
5.85k
    if (token->len > 3 && memcmp(token->str + 2, "COM", 3) == 0) {
68
2.01k
      guint8 header_image_cnt = 0;
69
2.01k
      if (token->len != 40) {
70
77
        g_set_error(error,
71
77
              FWUPD_ERROR,
72
77
              FWUPD_ERROR_INTERNAL,
73
77
              "invalid header, got %zu bytes",
74
77
              token->len);
75
77
        return FALSE;
76
77
      }
77
78
      /* sanity check */
79
1.93k
      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.93k
      if (!fu_firmware_strparse_uint4_safe(token->str,
88
1.93k
                   token->len,
89
1.93k
                   5,
90
1.93k
                   &header_image_cnt,
91
1.93k
                   error))
92
1
        return FALSE;
93
4.60k
      for (guint j = 0; j < header_image_cnt; j++) {
94
2.73k
        g_autofree FuFirmwareWacHeaderRecord *hdr = NULL;
95
2.73k
        hdr = g_new0(FuFirmwareWacHeaderRecord, 1);
96
2.73k
        if (!fu_firmware_strparse_uint32_safe(token->str,
97
2.73k
                      token->len,
98
2.73k
                      (j * 16) + 6,
99
2.73k
                      &hdr->addr,
100
2.73k
                      error))
101
36
          return FALSE;
102
2.69k
        if (!fu_firmware_strparse_uint32_safe(token->str,
103
2.69k
                      token->len,
104
2.69k
                      (j * 16) + 14,
105
2.69k
                      &hdr->sz,
106
2.69k
                      error))
107
25
          return FALSE;
108
2.67k
        g_debug("header_fw%u_addr: 0x%x", j, hdr->addr);
109
2.67k
        g_debug("header_fw%u_sz:   0x%x", j, hdr->sz);
110
2.67k
        g_ptr_array_add(helper->header_infos, g_steal_pointer(&hdr));
111
2.67k
      }
112
1.87k
      return TRUE;
113
1.93k
    }
114
115
    /* firmware headline record */
116
3.83k
    if (token->len == 13) {
117
667
      FuFirmwareWacHeaderRecord *hdr;
118
667
      guint8 idx = 0;
119
667
      if (!fu_firmware_strparse_uint4_safe(token->str,
120
667
                   token->len,
121
667
                   2,
122
667
                   &idx,
123
667
                   error))
124
4
        return FALSE;
125
663
      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
662
      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
658
      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
653
      hdr = g_ptr_array_index(helper->header_infos, idx - 1);
151
653
      if (!fu_firmware_strparse_uint32_safe(token->str,
152
653
                    token->len,
153
653
                    3,
154
653
                    &hdr->prog_start_addr,
155
653
                    error))
156
2
        return FALSE;
157
651
      if (hdr->prog_start_addr != hdr->addr) {
158
42
        g_set_error(error,
159
42
              FWUPD_ERROR,
160
42
              FWUPD_ERROR_INTERNAL,
161
42
              "programming address 0x%x != "
162
42
              "base address 0x%0x for idx %u",
163
42
              hdr->prog_start_addr,
164
42
              hdr->addr,
165
42
              idx);
166
42
        return FALSE;
167
42
      }
168
609
      g_debug("programing-start-address: 0x%x", hdr->prog_start_addr);
169
609
      return TRUE;
170
651
    }
171
172
3.17k
    g_debug("unknown Wacom-specific metadata");
173
3.17k
    return TRUE;
174
3.83k
  }
175
176
  /* start */
177
973k
  if (g_strcmp0(cmd, "S0") == 0) {
178
2.37k
    if (helper->image_buffer->len > 0) {
179
24
      g_set_error_literal(error,
180
24
              FWUPD_ERROR,
181
24
              FWUPD_ERROR_INTERNAL,
182
24
              "duplicate S0 without S7");
183
24
      return FALSE;
184
24
    }
185
2.34k
    g_string_append_printf(helper->image_buffer, "%s\n", token->str);
186
2.34k
    return TRUE;
187
2.37k
  }
188
189
  /* these are things we want to include in the image */
190
971k
  if (g_strcmp0(cmd, "S1") == 0 || g_strcmp0(cmd, "S2") == 0 || g_strcmp0(cmd, "S3") == 0 ||
191
13.6k
      g_strcmp0(cmd, "S5") == 0 || g_strcmp0(cmd, "S7") == 0 || g_strcmp0(cmd, "S8") == 0 ||
192
971k
      g_strcmp0(cmd, "S9") == 0) {
193
971k
    if (helper->image_buffer->len == 0) {
194
22
      g_set_error(error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "%s without S0", cmd);
195
22
      return FALSE;
196
22
    }
197
971k
    g_string_append_printf(helper->image_buffer, "%s\n", token->str);
198
971k
  } else {
199
72
    g_set_error(error,
200
72
          FWUPD_ERROR,
201
72
          FWUPD_ERROR_INTERNAL,
202
72
          "invalid SREC command on line %u: %s",
203
72
          token_idx + 1,
204
72
          cmd);
205
72
    return FALSE;
206
72
  }
207
208
  /* end */
209
971k
  if (g_strcmp0(cmd, "S7") == 0) {
210
2.15k
    g_autoptr(GBytes) blob = NULL;
211
2.15k
    g_autoptr(GBytes) fw_srec = NULL;
212
2.15k
    g_autoptr(FuFirmware) firmware_srec = fu_srec_firmware_new();
213
2.15k
    g_autoptr(FuFirmware) img = fu_firmware_new();
214
2.15k
    FuFirmwareWacHeaderRecord *hdr;
215
216
    /* get the correct relocated start address */
217
2.15k
    if (helper->images_cnt >= helper->header_infos->len) {
218
19
      g_set_error(error,
219
19
            FWUPD_ERROR,
220
19
            FWUPD_ERROR_INTERNAL,
221
19
            "%s without header",
222
19
            cmd);
223
19
      return FALSE;
224
19
    }
225
2.13k
    hdr = g_ptr_array_index(helper->header_infos, helper->images_cnt);
226
227
2.13k
    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.13k
    blob = g_bytes_new(helper->image_buffer->str, helper->image_buffer->len);
238
2.13k
    fu_srec_firmware_set_addr_min(FU_SREC_FIRMWARE(firmware_srec), hdr->addr);
239
2.13k
    if (!fu_firmware_parse_bytes(firmware_srec,
240
2.13k
               blob,
241
2.13k
               0x0,
242
2.13k
               helper->flags | FU_FIRMWARE_PARSE_FLAG_NO_SEARCH,
243
2.13k
               error))
244
390
      return FALSE;
245
1.74k
    fw_srec = fu_firmware_get_bytes(firmware_srec, error);
246
1.74k
    if (fw_srec == NULL)
247
0
      return FALSE;
248
1.74k
    fu_firmware_set_bytes(img, fw_srec);
249
1.74k
    fu_firmware_set_addr(img, fu_firmware_get_addr(firmware_srec));
250
1.74k
    fu_firmware_set_idx(img, helper->images_cnt);
251
1.74k
    if (!fu_firmware_add_image(helper->firmware, img, error))
252
0
      return FALSE;
253
1.74k
    helper->images_cnt++;
254
255
    /* clear the image buffer */
256
1.74k
    g_string_set_size(helper->image_buffer, 0);
257
1.74k
  }
258
259
  /* success */
260
971k
  return TRUE;
261
971k
}
262
263
static gboolean
264
fu_wacom_usb_firmware_validate(FuFirmware *firmware,
265
             FuInputStream *stream,
266
             gsize offset,
267
             GError **error)
268
1.67k
{
269
1.67k
  return fu_struct_wacom_usb_firmware_hdr_validate_stream(stream, offset, error);
270
1.67k
}
271
272
static gboolean
273
fu_wacom_usb_firmware_parse(FuFirmware *firmware,
274
          FuInputStream *stream,
275
          FuFirmwareParseFlags flags,
276
          GError **error)
277
1.61k
{
278
1.61k
  g_autoptr(GPtrArray) header_infos = g_ptr_array_new_with_free_func(g_free);
279
1.61k
  g_autoptr(GString) image_buffer = g_string_new(NULL);
280
1.61k
  FuWacomUsbFirmwareTokenHelper helper = {.firmware = firmware,
281
1.61k
            .flags = flags,
282
1.61k
            .header_infos = header_infos,
283
1.61k
            .image_buffer = image_buffer,
284
1.61k
            .images_cnt = 0};
285
286
  /* tokenize */
287
1.61k
  if (!fu_strsplit_stream(stream,
288
1.61k
        0x0,
289
1.61k
        "\n",
290
1.61k
        fu_wacom_usb_firmware_tokenize_cb,
291
1.61k
        &helper,
292
1.61k
        error))
293
727
    return FALSE;
294
295
  /* verify data is complete */
296
891
  if (helper.image_buffer->len > 0) {
297
146
    g_set_error_literal(error,
298
146
            FWUPD_ERROR,
299
146
            FWUPD_ERROR_INTERNAL,
300
146
            "truncated data: no S7");
301
146
    return FALSE;
302
146
  }
303
304
  /* ensure this matched the header */
305
745
  if (helper.header_infos->len != helper.images_cnt) {
306
51
    g_set_error(error,
307
51
          FWUPD_ERROR,
308
51
          FWUPD_ERROR_INTERNAL,
309
51
          "not enough images %u for header count %u",
310
51
          helper.images_cnt,
311
51
          header_infos->len);
312
51
    return FALSE;
313
51
  }
314
315
  /* success */
316
694
  return TRUE;
317
745
}
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
694
{
328
694
  g_autoptr(GPtrArray) images = fu_firmware_get_images(firmware);
329
694
  g_autoptr(GString) str = g_string_new(NULL);
330
694
  g_autoptr(GByteArray) buf = g_byte_array_new();
331
694
  g_autoptr(GByteArray) buf_hdr = g_byte_array_new();
332
333
  /* fw header */
334
694
  if (images->len == 0) {
335
393
    g_set_error_literal(error,
336
393
            FWUPD_ERROR,
337
393
            FWUPD_ERROR_NOT_SUPPORTED,
338
393
            "no firmware images found");
339
393
    return NULL;
340
393
  }
341
1.08k
  for (guint i = 0; i < images->len; i++) {
342
784
    FuFirmware *img = g_ptr_array_index(images, i);
343
784
    fu_byte_array_append_uint32(buf_hdr, fu_firmware_get_addr(img), G_BIG_ENDIAN);
344
784
    fu_byte_array_append_uint32(buf_hdr, fu_firmware_get_size(img), G_BIG_ENDIAN);
345
784
  }
346
301
  g_string_append_printf(str, "WACOM%u", images->len);
347
6.57k
  for (guint i = 0; i < buf_hdr->len; i++)
348
6.27k
    g_string_append_printf(str, "%02X", buf_hdr->data[i]);
349
301
  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
784
    FuFirmware *img = g_ptr_array_index(images, i);
354
784
    g_autoptr(GBytes) img_blob = NULL;
355
784
    g_autoptr(GByteArray) buf_img = g_byte_array_new();
356
357
    /* img header */
358
784
    g_string_append_printf(str, "WA%u", (guint)fu_firmware_get_idx(img) + 1);
359
784
    fu_byte_array_append_uint32(buf_img, fu_firmware_get_addr(img), G_BIG_ENDIAN);
360
3.92k
    for (guint j = 0; j < buf_img->len; j++)
361
3.13k
      g_string_append_printf(str, "%02X", buf_img->data[j]);
362
784
    g_string_append_printf(str, "%02X\n", fu_wacom_usb_firmware_calc_checksum(buf_img));
363
364
    /* srec */
365
784
    img_blob = fu_firmware_write(img, error);
366
784
    if (img_blob == NULL)
367
0
      return NULL;
368
784
    g_string_append_len(str,
369
784
            (const gchar *)g_bytes_get_data(img_blob, NULL),
370
784
            g_bytes_get_size(img_blob));
371
784
  }
372
373
  /* success */
374
301
  g_byte_array_append(buf, (const guint8 *)str->str, str->len);
375
301
  return g_steal_pointer(&buf);
376
301
}
377
378
static void
379
fu_wacom_usb_firmware_init(FuWacomUsbFirmware *self)
380
1.67k
{
381
1.67k
}
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
}