Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fwupd/libfwupdplugin/fu-udev-device.c
Line
Count
Source
1
/*
2
 * Copyright 2017 Richard Hughes <richard@hughsie.com>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
0
#define G_LOG_DOMAIN "FuUdevDevice"
8
9
#include "config.h"
10
11
#include <fcntl.h>
12
#include <string.h>
13
#ifdef HAVE_ERRNO_H
14
#include <errno.h>
15
#endif
16
#ifdef HAVE_IOCTL_H
17
#include <sys/ioctl.h>
18
#endif
19
#include <glib/gstdio.h>
20
#include <sys/stat.h>
21
#include <sys/types.h>
22
#include <unistd.h>
23
24
#include "fu-byte-array.h"
25
#include "fu-device-event-private.h"
26
#include "fu-device-private.h"
27
#include "fu-dpaux-device.h"
28
#include "fu-ioctl-private.h"
29
#include "fu-output-stream.h"
30
#include "fu-path.h"
31
#include "fu-string.h"
32
#include "fu-udev-device-private.h"
33
34
/**
35
 * FuUdevDevice:
36
 *
37
 * A UDev device, typically only available on Linux.
38
 *
39
 * See also: [class@FuDevice]
40
 */
41
42
typedef struct {
43
  gchar *subsystem;
44
  gchar *bind_id;
45
  gchar *driver;
46
  gchar *device_file;
47
  gchar *devtype;
48
  guint64 number;
49
  FuIOChannel *io_channel;
50
  goffset emulated_offset;
51
  FuIoChannelOpenFlags open_flags;
52
  GHashTable *properties;
53
  gboolean properties_valid;
54
} FuUdevDevicePrivate;
55
56
4
G_DEFINE_TYPE_WITH_PRIVATE(FuUdevDevice, fu_udev_device, FU_TYPE_DEVICE);
57
4
58
4
enum {
59
4
  PROP_0,
60
4
  PROP_SUBSYSTEM,
61
4
  PROP_DRIVER,
62
4
  PROP_DEVICE_FILE,
63
4
  PROP_BIND_ID,
64
4
  PROP_DEVTYPE,
65
4
  PROP_LAST
66
4
};
67
4
68
4
enum { SIGNAL_CHANGED, SIGNAL_LAST };
69
4
70
4
static guint signals[SIGNAL_LAST] = {0};
71
4
72
4
enum {
73
4
  QUARK_SYSFS_USE_PHYSICAL_ID,
74
4
  QUARK_LAST,
75
4
};
76
4
77
4
static guint quarks[QUARK_LAST] = {0};
78
4
79
1.60k
#define GET_PRIVATE(o) (fu_udev_device_get_instance_private(o))
80
81
/**
82
 * fu_udev_device_emit_changed:
83
 * @self: a #FuUdevDevice
84
 *
85
 * Emits the ::changed signal for the object.
86
 **/
87
void
88
fu_udev_device_emit_changed(FuUdevDevice *self)
89
0
{
90
0
  g_autoptr(GError) error = NULL;
91
0
  g_return_if_fail(FU_IS_UDEV_DEVICE(self));
92
0
  g_debug("FuUdevDevice emit changed");
93
0
  if (!fu_device_rescan(FU_DEVICE(self), &error))
94
0
    g_debug("%s", error->message);
95
0
  g_signal_emit(self, signals[SIGNAL_CHANGED], 0);
96
0
}
97
98
static void
99
fu_udev_device_to_string(FuDevice *device, guint idt, GString *str)
100
0
{
101
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
102
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
103
0
  g_autofree gchar *open_flags = fu_io_channel_open_flags_to_string(priv->open_flags);
104
105
0
  fwupd_codec_string_append_hex(str, idt, "Number", priv->number);
106
0
  fwupd_codec_string_append(str, idt, "Subsystem", priv->subsystem);
107
0
  fwupd_codec_string_append(str, idt, "Devtype", priv->devtype);
108
0
  fwupd_codec_string_append(str, idt, "Driver", priv->driver);
109
0
  fwupd_codec_string_append(str, idt, "BindId", priv->bind_id);
110
0
  fwupd_codec_string_append(str, idt, "DeviceFile", priv->device_file);
111
0
  fwupd_codec_string_append(str, idt, "OpenFlags", open_flags);
112
0
}
113
114
static gboolean
115
fu_udev_device_ensure_bind_id(FuUdevDevice *self, GError **error)
116
0
{
117
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
118
119
  /* sanity check */
120
0
  if (priv->bind_id != NULL)
121
0
    return TRUE;
122
123
  /* automatically set the bind ID from the subsystem */
124
0
  if (g_strcmp0(priv->subsystem, "pci") == 0) {
125
0
    priv->bind_id = fu_udev_device_read_property(self, "PCI_SLOT_NAME", error);
126
0
    return priv->bind_id != NULL;
127
0
  }
128
0
  if (g_strcmp0(priv->subsystem, "hid") == 0) {
129
0
    priv->bind_id = fu_udev_device_read_property(self, "HID_PHYS", error);
130
0
    return priv->bind_id != NULL;
131
0
  }
132
0
  if (g_strcmp0(priv->subsystem, "usb") == 0 || g_strcmp0(priv->subsystem, "i2c") == 0) {
133
0
    priv->bind_id = g_path_get_basename(fu_udev_device_get_sysfs_path(self));
134
0
    return TRUE;
135
0
  }
136
137
  /* nothing found automatically */
138
0
  g_set_error(error,
139
0
        FWUPD_ERROR,
140
0
        FWUPD_ERROR_NOT_SUPPORTED,
141
0
        "cannot derive bind-id from subsystem %s",
142
0
        priv->subsystem);
143
0
  return FALSE;
144
0
}
145
146
/* private */
147
void
148
fu_udev_device_set_subsystem(FuUdevDevice *self, const gchar *subsystem)
149
0
{
150
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
151
0
  if (g_set_str(&priv->subsystem, subsystem))
152
0
    g_object_notify(G_OBJECT(self), "subsystem");
153
0
}
154
155
/**
156
 * fu_udev_device_set_bind_id:
157
 * @self: a #FuUdevDevice
158
 * @bind_id: a bind-id string, e.g. `pci:0:0:1`
159
 *
160
 * Sets the device ID used for binding the device, e.g. `pci:1:2:3`
161
 **/
162
void
163
fu_udev_device_set_bind_id(FuUdevDevice *self, const gchar *bind_id)
164
0
{
165
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
166
0
  if (g_set_str(&priv->bind_id, bind_id))
167
0
    g_object_notify(G_OBJECT(self), "bind-id");
168
0
}
169
170
static void
171
fu_udev_device_set_driver(FuUdevDevice *self, const gchar *driver)
172
0
{
173
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
174
0
  if (g_set_str(&priv->driver, driver))
175
0
    g_object_notify(G_OBJECT(self), "driver");
176
0
}
177
178
/**
179
 * fu_udev_device_set_device_file:
180
 * @self: a #FuUdevDevice
181
 * @device_file: (nullable): a device path
182
 *
183
 * Sets the device file to use for reading and writing.
184
 **/
185
void
186
fu_udev_device_set_device_file(FuUdevDevice *self, const gchar *device_file)
187
0
{
188
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
189
0
  if (g_set_str(&priv->device_file, device_file))
190
0
    g_object_notify(G_OBJECT(self), "device-file");
191
0
}
192
193
/* private */
194
gchar *
195
fu_udev_device_get_symlink_target(FuUdevDevice *self, const gchar *attr, GError **error)
196
0
{
197
0
  FuDeviceEvent *event = NULL;
198
0
  g_autofree gchar *event_id = NULL;
199
0
  g_autofree gchar *fn_attr = NULL;
200
0
  g_autofree gchar *symlink_target = NULL;
201
202
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
203
0
  g_return_val_if_fail(attr != NULL, NULL);
204
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
205
206
0
  if (fu_udev_device_get_sysfs_path(self) == NULL) {
207
0
    g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, "no sysfs path");
208
0
    return NULL;
209
0
  }
210
211
  /* need event ID */
212
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
213
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
214
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
215
0
    event_id = g_strdup_printf("GetSymlinkTarget:Attr=%s", attr);
216
0
  }
217
218
  /* emulated */
219
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
220
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
221
0
    if (event == NULL)
222
0
      return NULL;
223
0
    return g_strdup(fu_device_event_get_str(event, "Data", error));
224
0
  }
225
226
  /* save */
227
0
  if (event_id != NULL)
228
0
    event = fu_device_save_event(FU_DEVICE(self), event_id);
229
230
  /* find target */
231
0
  fn_attr = g_build_filename(fu_udev_device_get_sysfs_path(self), attr, NULL);
232
0
  symlink_target = fu_path_get_symlink_target(fn_attr, error);
233
0
  if (symlink_target == NULL)
234
0
    return NULL;
235
236
  /* save response */
237
0
  if (event != NULL)
238
0
    fu_device_event_set_str(event, "Data", symlink_target);
239
240
  /* success */
241
0
  return g_steal_pointer(&symlink_target);
242
0
}
243
244
/* private */
245
void
246
fu_udev_device_set_number(FuUdevDevice *self, guint64 number)
247
0
{
248
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
249
0
  g_return_if_fail(FU_IS_UDEV_DEVICE(self));
250
0
  priv->number = number;
251
0
}
252
253
/* private */
254
void
255
fu_udev_device_set_devtype(FuUdevDevice *self, const gchar *devtype)
256
0
{
257
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
258
0
  if (g_set_str(&priv->devtype, devtype))
259
0
    g_object_notify(G_OBJECT(self), "devtype");
260
0
}
261
262
/* private */
263
gboolean
264
fu_udev_device_parse_number(FuUdevDevice *self, GError **error)
265
0
{
266
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
267
0
  g_autoptr(GString) path = g_string_new(fu_udev_device_get_sysfs_path(self));
268
269
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
270
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
271
272
0
  if (path->len == 0)
273
0
    return TRUE;
274
0
  for (guint i = path->len - 1; i > 0; i--) {
275
0
    if (!g_ascii_isdigit(path->str[i])) {
276
0
      g_string_erase(path, 0, i + 1);
277
0
      break;
278
0
    }
279
0
  }
280
0
  if (path->len > 0) {
281
0
    if (!fu_strtoull(path->str,
282
0
         &priv->number,
283
0
         0x0,
284
0
         G_MAXUINT64,
285
0
         FU_INTEGER_BASE_AUTO,
286
0
         error))
287
0
      return FALSE;
288
0
  }
289
290
  /* success */
291
0
  return TRUE;
292
0
}
293
294
static gboolean
295
fu_udev_device_ensure_devtype_from_modalias(FuUdevDevice *self, GError **error)
296
0
{
297
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
298
0
  const gchar *devtype_modalias[] = {"mmc", "platform", NULL};
299
0
  g_autofree gchar *prop_modalias = NULL;
300
0
  g_auto(GStrv) split_modalias = NULL;
301
302
  /* only some subsystems forget to set the DEVTYPE property */
303
0
  if (!g_strv_contains(devtype_modalias, priv->subsystem))
304
0
    return TRUE;
305
306
  /* parse out subsystem:devtype */
307
0
  prop_modalias = fu_udev_device_read_property(self, "MODALIAS", error);
308
0
  if (prop_modalias == NULL)
309
0
    return FALSE;
310
0
  split_modalias = g_strsplit(prop_modalias, ":", 2);
311
0
  if (g_strv_length(split_modalias) >= 2)
312
0
    priv->devtype = g_strdup(split_modalias[1]);
313
314
  /* success */
315
0
  return TRUE;
316
0
}
317
318
static gchar *
319
fu_udev_device_build_devfs_path(FuUdevDevice *self, const gchar *devname)
320
0
{
321
#ifdef __ANDROID__
322
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
323
324
  /* ueventd is weird */
325
  if (g_strcmp0(priv->subsystem, "block") == 0)
326
    return g_strdup_printf("/dev/block/%s", devname);
327
#endif
328
0
  return g_strdup_printf("/dev/%s", devname);
329
0
}
330
331
static gboolean
332
fu_udev_device_probe(FuDevice *device, GError **error)
333
0
{
334
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
335
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
336
0
  g_autofree gchar *subsystem = NULL;
337
0
  g_autofree gchar *attr_device = NULL;
338
0
  g_autofree gchar *attr_vendor = NULL;
339
340
  /* find the subsystem, driver and devtype */
341
0
  if (priv->subsystem == NULL) {
342
0
    g_autofree gchar *subsystem_basename = NULL;
343
0
    g_autofree gchar *subsystem_tmp =
344
0
        fu_udev_device_get_symlink_target(self, "subsystem", error);
345
0
    if (subsystem_tmp == NULL) {
346
0
      g_prefix_error_literal(error, "failed to read subsystem: ");
347
0
      return FALSE;
348
0
    }
349
0
    subsystem_basename = g_path_get_basename(subsystem_tmp);
350
0
    fu_udev_device_set_subsystem(self, subsystem_basename);
351
0
  }
352
0
  if (priv->driver == NULL) {
353
0
    g_autofree gchar *driver_tmp =
354
0
        fu_udev_device_get_symlink_target(self, "driver", NULL);
355
0
    if (driver_tmp != NULL) {
356
0
      g_autofree gchar *driver_basename = g_path_get_basename(driver_tmp);
357
0
      fu_udev_device_set_driver(self, driver_basename);
358
0
    }
359
0
  }
360
0
  if (priv->devtype == NULL) {
361
0
    priv->devtype = fu_udev_device_read_property(self, "DEVTYPE", NULL);
362
0
    if (priv->devtype == NULL) {
363
0
      if (!fu_udev_device_ensure_devtype_from_modalias(self, error))
364
0
        return FALSE;
365
0
    }
366
0
  }
367
0
  if (priv->device_file == NULL) {
368
0
    g_autofree gchar *prop_devname =
369
0
        fu_udev_device_read_property(self, "DEVNAME", NULL);
370
0
    if (prop_devname != NULL) {
371
0
      g_autofree gchar *device_file =
372
0
          fu_udev_device_build_devfs_path(self, prop_devname);
373
0
      fu_udev_device_set_device_file(self, device_file);
374
0
    }
375
0
  }
376
377
  /* get IDs */
378
0
  attr_vendor = fu_udev_device_read_sysfs(self,
379
0
            "vendor",
380
0
            FU_UDEV_DEVICE_ATTR_READ_TIMEOUT_DEFAULT,
381
0
            NULL);
382
0
  if (attr_vendor != NULL) {
383
0
    guint64 tmp64 = 0;
384
0
    if (!fu_strtoull(attr_vendor, &tmp64, 0, G_MAXUINT16, FU_INTEGER_BASE_AUTO, NULL)) {
385
0
      fu_device_set_vendor(device, attr_vendor);
386
0
    } else {
387
0
      fu_device_set_vid(device, (guint16)tmp64);
388
0
    }
389
0
  }
390
0
  attr_device = fu_udev_device_read_sysfs(self,
391
0
            "device",
392
0
            FU_UDEV_DEVICE_ATTR_READ_TIMEOUT_DEFAULT,
393
0
            NULL);
394
0
  if (attr_device != NULL) {
395
0
    guint64 tmp64 = 0;
396
0
    if (!fu_strtoull(attr_device, &tmp64, 0, G_MAXUINT16, FU_INTEGER_BASE_AUTO, error))
397
0
      return FALSE;
398
0
    fu_device_set_pid(device, (guint16)tmp64);
399
0
  }
400
401
  /* set number */
402
0
  if (fu_udev_device_get_sysfs_path(self) != NULL) {
403
0
    g_autoptr(GError) error_local = NULL;
404
0
    if (!fu_udev_device_parse_number(self, &error_local))
405
0
      g_debug("failed to convert udev number: %s", error_local->message);
406
0
  }
407
408
  /* set vendor ID */
409
0
  if (priv->subsystem != NULL)
410
0
    subsystem = g_ascii_strup(priv->subsystem, -1);
411
0
  if (subsystem != NULL)
412
0
    fu_device_build_vendor_id_u16(device, subsystem, fu_device_get_vid(device));
413
414
  /* add GUIDs in order of priority */
415
0
  if (subsystem != NULL) {
416
0
    fu_device_build_instance_id_full(device,
417
0
             FU_DEVICE_INSTANCE_FLAG_GENERIC |
418
0
                 FU_DEVICE_INSTANCE_FLAG_QUIRKS_USE_CACHE,
419
0
             NULL,
420
0
             subsystem,
421
0
             "VEN",
422
0
             NULL);
423
0
    fu_device_build_instance_id_full(device,
424
0
             FU_DEVICE_INSTANCE_FLAG_GENERIC |
425
0
                 FU_DEVICE_INSTANCE_FLAG_VISIBLE |
426
0
                 FU_DEVICE_INSTANCE_FLAG_QUIRKS_USE_CACHE,
427
0
             NULL,
428
0
             subsystem,
429
0
             "VEN",
430
0
             "DEV",
431
0
             NULL);
432
0
  }
433
434
  /* add device class */
435
0
  if (subsystem != NULL) {
436
0
    g_autofree gchar *cls =
437
0
        fu_udev_device_read_sysfs(self,
438
0
                "class",
439
0
                FU_UDEV_DEVICE_ATTR_READ_TIMEOUT_DEFAULT,
440
0
                NULL);
441
0
    g_autofree gchar *devtype = fu_udev_device_read_property(self, "DEVTYPE", NULL);
442
0
    if (cls != NULL && g_str_has_prefix(cls, "0x"))
443
0
      fu_device_add_instance_strup(device, "CLASS", cls + 2);
444
0
    else
445
0
      fu_device_add_instance_strup(device, "CLASS", cls);
446
0
    fu_device_build_instance_id_full(device,
447
0
             FU_DEVICE_INSTANCE_FLAG_GENERIC |
448
0
                 FU_DEVICE_INSTANCE_FLAG_QUIRKS_USE_CACHE,
449
0
             NULL,
450
0
             subsystem,
451
0
             "VEN",
452
0
             "CLASS",
453
0
             NULL);
454
455
    /* add devtype */
456
0
    fu_device_add_instance_strup(device, "TYPE", devtype);
457
0
    fu_device_build_instance_id_full(device,
458
0
             FU_DEVICE_INSTANCE_FLAG_GENERIC |
459
0
                 FU_DEVICE_INSTANCE_FLAG_QUIRKS_USE_CACHE,
460
0
             NULL,
461
0
             subsystem,
462
0
             "TYPE",
463
0
             NULL);
464
465
    /* add the driver */
466
0
    fu_device_add_instance_str(device, "DRIVER", priv->driver);
467
0
    fu_device_build_instance_id_full(device,
468
0
             FU_DEVICE_INSTANCE_FLAG_GENERIC |
469
0
                 FU_DEVICE_INSTANCE_FLAG_QUIRKS_USE_CACHE,
470
0
             NULL,
471
0
             subsystem,
472
0
             "DRIVER",
473
0
             NULL);
474
0
  }
475
476
  /* success */
477
0
  return TRUE;
478
0
}
479
480
/**
481
 * fu_udev_device_get_subsystem_depth:
482
 * @self: a #FuUdevDevice
483
 * @subsystem: a subsystem
484
 *
485
 * Determine how far up a chain a given device is
486
 *
487
 * Returns: unsigned integer
488
 **/
489
guint
490
fu_udev_device_get_subsystem_depth(FuUdevDevice *self, const gchar *subsystem)
491
0
{
492
0
  g_autoptr(FuDevice) device_tmp = NULL;
493
494
0
  device_tmp = fu_device_get_backend_parent_with_subsystem(FU_DEVICE(self), subsystem, NULL);
495
0
  if (device_tmp == NULL)
496
0
    return 0;
497
0
  if (g_strcmp0(fu_device_get_id(device_tmp), fu_device_get_id(FU_DEVICE(self))) == 0)
498
0
    return 0;
499
0
  for (guint i = 0;; i++) {
500
0
    g_autoptr(FuDevice) parent =
501
0
        fu_device_get_backend_parent(FU_DEVICE(device_tmp), NULL);
502
0
    if (parent == NULL)
503
0
      return i;
504
0
    g_set_object(&device_tmp, parent);
505
0
  }
506
0
  return 0;
507
0
}
508
509
static void
510
fu_udev_device_probe_complete(FuDevice *device)
511
0
{
512
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
513
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
514
0
  g_hash_table_remove_all(priv->properties);
515
0
  priv->properties_valid = FALSE;
516
0
}
517
518
static gboolean
519
fu_udev_device_unbind_driver(FuDevice *device, GError **error)
520
0
{
521
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
522
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
523
0
  g_autofree gchar *fn = NULL;
524
0
  g_autoptr(GOutputStream) stream = NULL;
525
526
  /* emulated */
527
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED))
528
0
    return TRUE;
529
530
  /* is already unbound */
531
0
  if (fu_udev_device_get_sysfs_path(self) == NULL) {
532
0
    g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, "not initialized");
533
0
    return FALSE;
534
0
  }
535
0
  fn = g_build_filename(fu_udev_device_get_sysfs_path(self), "driver", "unbind", NULL);
536
0
  if (!g_file_test(fn, G_FILE_TEST_EXISTS))
537
0
    return TRUE;
538
539
  /* write bus ID to file */
540
0
  if (!fu_udev_device_ensure_bind_id(self, error))
541
0
    return FALSE;
542
0
  stream = fu_output_stream_from_path(fn, error);
543
0
  if (stream == NULL)
544
0
    return FALSE;
545
0
  return g_output_stream_write_all(stream,
546
0
           priv->bind_id,
547
0
           strlen(priv->bind_id),
548
0
           NULL,
549
0
           NULL,
550
0
           error);
551
0
}
552
553
static gboolean
554
fu_udev_device_bind_driver(FuDevice *device,
555
         const gchar *subsystem,
556
         const gchar *driver,
557
         GError **error)
558
0
{
559
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
560
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
561
0
  g_autofree gchar *driver_safe = g_strdup(driver);
562
0
  g_autofree gchar *fn = NULL;
563
0
  g_autoptr(GFile) file = NULL;
564
0
  g_autoptr(GOutputStream) stream = NULL;
565
566
  /* emulated */
567
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED))
568
0
    return TRUE;
569
570
  /* copy the logic from modprobe */
571
0
  g_strdelimit(driver_safe, "-", '_');
572
573
  /* driver exists */
574
0
  fn = g_strdup_printf("/sys/module/%s/drivers/%s:%s/bind",
575
0
           driver_safe,
576
0
           subsystem,
577
0
           driver_safe);
578
0
  if (!g_file_test(fn, G_FILE_TEST_EXISTS)) {
579
0
    g_set_error(error,
580
0
          FWUPD_ERROR,
581
0
          FWUPD_ERROR_NOT_SUPPORTED,
582
0
          "cannot bind with %s:%s",
583
0
          subsystem,
584
0
          driver);
585
0
    return FALSE;
586
0
  }
587
588
  /* write bus ID to file */
589
0
  if (!fu_udev_device_ensure_bind_id(self, error))
590
0
    return FALSE;
591
0
  if (priv->bind_id == NULL) {
592
0
    g_set_error(error,
593
0
          FWUPD_ERROR,
594
0
          FWUPD_ERROR_NOT_SUPPORTED,
595
0
          "bind-id not set for subsystem %s",
596
0
          priv->subsystem);
597
0
    return FALSE;
598
0
  }
599
0
  file = g_file_new_for_path(fn);
600
0
  stream =
601
0
      G_OUTPUT_STREAM(g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, error));
602
0
  if (stream == NULL)
603
0
    return FALSE;
604
0
  return g_output_stream_write_all(stream,
605
0
           priv->bind_id,
606
0
           strlen(priv->bind_id),
607
0
           NULL,
608
0
           NULL,
609
0
           error);
610
0
}
611
612
static FuIoChannelOpenFlags
613
fu_udev_device_get_open_flags(FuUdevDevice *self)
614
0
{
615
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
616
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), 0);
617
0
  return priv->open_flags;
618
0
}
619
620
static void
621
fu_udev_device_invalidate(FuDevice *device)
622
0
{
623
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
624
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
625
0
  priv->properties_valid = FALSE;
626
0
  g_hash_table_remove_all(priv->properties);
627
0
}
628
629
static void
630
fu_udev_device_incorporate(FuDevice *device, FuDevice *donor)
631
0
{
632
0
  FuUdevDevice *uself = FU_UDEV_DEVICE(device);
633
0
  FuUdevDevice *udonor = FU_UDEV_DEVICE(donor);
634
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(uself);
635
636
0
  g_return_if_fail(FU_IS_UDEV_DEVICE(device));
637
0
  g_return_if_fail(FU_IS_UDEV_DEVICE(donor));
638
639
0
  if (priv->device_file == NULL)
640
0
    fu_udev_device_set_device_file(uself, fu_udev_device_get_device_file(udonor));
641
0
  if (priv->subsystem == NULL)
642
0
    fu_udev_device_set_subsystem(uself, fu_udev_device_get_subsystem(udonor));
643
0
  if (priv->bind_id == NULL)
644
0
    fu_udev_device_set_bind_id(uself, fu_udev_device_get_bind_id(udonor));
645
0
  if (priv->driver == NULL)
646
0
    fu_udev_device_set_driver(uself, fu_udev_device_get_driver(udonor));
647
0
  if (priv->devtype == NULL)
648
0
    fu_udev_device_set_devtype(uself, fu_udev_device_get_devtype(udonor));
649
0
  if (priv->number == 0x0)
650
0
    fu_udev_device_set_number(uself, fu_udev_device_get_number(udonor));
651
0
  if (priv->open_flags == FU_IO_CHANNEL_OPEN_FLAG_NONE)
652
0
    priv->open_flags = fu_udev_device_get_open_flags(udonor);
653
0
}
654
655
/**
656
 * fu_udev_device_get_subsystem:
657
 * @self: a #FuUdevDevice
658
 *
659
 * Gets the device subsystem, e.g. `pci`
660
 *
661
 * Returns: a subsystem, or NULL if unset or invalid
662
 **/
663
const gchar *
664
fu_udev_device_get_subsystem(FuUdevDevice *self)
665
0
{
666
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
667
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
668
0
  return priv->subsystem;
669
0
}
670
671
/**
672
 * fu_udev_device_get_bind_id:
673
 * @self: a #FuUdevDevice
674
 *
675
 * Gets the device ID used for binding the device, e.g. `pci:1:2:3`
676
 *
677
 * Returns: a bind_id, or NULL if unset or invalid
678
 **/
679
const gchar *
680
fu_udev_device_get_bind_id(FuUdevDevice *self)
681
0
{
682
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
683
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
684
0
  fu_udev_device_ensure_bind_id(self, NULL);
685
0
  return priv->bind_id;
686
0
}
687
688
/**
689
 * fu_udev_device_get_driver:
690
 * @self: a #FuUdevDevice
691
 *
692
 * Gets the device driver, e.g. `psmouse`.
693
 *
694
 * Returns: a subsystem, or NULL if unset or invalid
695
 **/
696
const gchar *
697
fu_udev_device_get_driver(FuUdevDevice *self)
698
0
{
699
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
700
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
701
0
  return priv->driver;
702
0
}
703
704
/**
705
 * fu_udev_device_get_device_file:
706
 * @self: a #FuUdevDevice
707
 *
708
 * Gets the device node.
709
 *
710
 * Returns: a device file, or NULL if unset
711
 **/
712
const gchar *
713
fu_udev_device_get_device_file(FuUdevDevice *self)
714
0
{
715
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
716
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
717
0
  return priv->device_file;
718
0
}
719
720
/**
721
 * fu_udev_device_get_sysfs_path:
722
 * @self: a #FuUdevDevice
723
 *
724
 * Gets the device sysfs path, e.g. `/sys/devices/pci0000:00/0000:00:14.0`.
725
 *
726
 * Returns: a local path, or NULL if unset or invalid
727
 **/
728
const gchar *
729
fu_udev_device_get_sysfs_path(FuUdevDevice *self)
730
0
{
731
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
732
733
0
  if (fu_device_has_private_flag_quark(FU_DEVICE(self), quarks[QUARK_SYSFS_USE_PHYSICAL_ID]))
734
0
    return fu_device_get_physical_id(FU_DEVICE(self));
735
0
  return fu_device_get_backend_id(FU_DEVICE(self));
736
0
}
737
738
/**
739
 * fu_udev_device_get_number:
740
 * @self: a #FuUdevDevice
741
 *
742
 * Gets the device number, if any.
743
 *
744
 * Returns: integer, 0 if the data is unavailable.
745
 **/
746
guint64
747
fu_udev_device_get_number(FuUdevDevice *self)
748
0
{
749
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
750
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), 0);
751
0
  return priv->number;
752
0
}
753
754
static gchar *
755
fu_udev_device_get_parent_subsystems(FuUdevDevice *self)
756
0
{
757
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
758
0
  g_autoptr(GString) str = g_string_new(NULL);
759
0
  g_autoptr(FuUdevDevice) udev_device = g_object_ref(self);
760
761
  /* not true, but good enough for emulation */
762
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED))
763
0
    return g_strdup(priv->subsystem);
764
765
  /* find subsystems of self and all parent devices */
766
0
  while (TRUE) {
767
0
    g_autoptr(FuUdevDevice) parent = NULL;
768
0
    if (fu_udev_device_get_devtype(udev_device) != NULL) {
769
0
      g_string_append_printf(str,
770
0
                 "%s:%s,",
771
0
                 fu_udev_device_get_subsystem(udev_device),
772
0
                 fu_udev_device_get_devtype(udev_device));
773
0
    } else {
774
0
      g_string_append_printf(str,
775
0
                 "%s,",
776
0
                 fu_udev_device_get_subsystem(udev_device));
777
0
    }
778
0
    parent = FU_UDEV_DEVICE(
779
0
        fu_device_get_backend_parent_with_subsystem(FU_DEVICE(udev_device),
780
0
                NULL,
781
0
                NULL));
782
0
    if (parent == NULL)
783
0
      break;
784
0
    g_set_object(&udev_device, parent);
785
0
  }
786
0
  if (str->len > 0)
787
0
    g_string_truncate(str, str->len - 1);
788
0
  return g_string_free_and_steal(g_steal_pointer(&str));
789
0
}
790
791
/* private */
792
gboolean
793
fu_udev_device_match_subsystem(FuUdevDevice *self, const gchar *subsystem)
794
0
{
795
0
  g_auto(GStrv) subsys_devtype = NULL;
796
797
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
798
799
0
  if (subsystem == NULL)
800
0
    return TRUE;
801
0
  subsys_devtype = g_strsplit(subsystem, ":", 2);
802
0
  if (g_strcmp0(fu_udev_device_get_subsystem(self), subsys_devtype[0]) != 0)
803
0
    return FALSE;
804
0
  if (subsys_devtype[1] != NULL &&
805
0
      g_strcmp0(fu_udev_device_get_devtype(self), subsys_devtype[1]) != 0) {
806
0
    return FALSE;
807
0
  }
808
0
  return TRUE;
809
0
}
810
811
/* private */
812
gchar *
813
fu_udev_device_get_device_file_from_subsystem(FuUdevDevice *self,
814
                const gchar *subsystem,
815
                GError **error)
816
0
{
817
0
  const gchar *fn;
818
0
  g_autofree gchar *subsystem_dir = NULL;
819
0
  g_autoptr(GDir) dir = NULL;
820
0
  g_autoptr(GError) error_local = NULL;
821
822
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
823
0
  g_return_val_if_fail(subsystem != NULL, NULL);
824
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
825
826
0
  subsystem_dir =
827
0
      g_build_filename(fu_udev_device_get_sysfs_path(FU_UDEV_DEVICE(self)), subsystem, NULL);
828
0
  dir = g_dir_open(subsystem_dir, 0, &error_local);
829
0
  if (dir == NULL) {
830
0
    if (g_error_matches(error_local, G_FILE_ERROR_NOENT, G_FILE_ERROR_NOENT)) {
831
0
      g_set_error(error,
832
0
            FWUPD_ERROR,
833
0
            FWUPD_ERROR_NOT_FOUND,
834
0
            "failed to find subsystem directory %s",
835
0
            subsystem_dir);
836
0
      return NULL;
837
0
    }
838
0
    g_propagate_error(error, g_steal_pointer(&error_local));
839
0
    fwupd_error_convert(error);
840
0
    return NULL;
841
0
  }
842
0
  fn = g_dir_read_name(dir);
843
0
  if (fn == NULL) {
844
0
    g_set_error(error,
845
0
          FWUPD_ERROR,
846
0
          FWUPD_ERROR_NOT_FOUND,
847
0
          "failed to find subsystem device in %s",
848
0
          subsystem_dir);
849
0
    return NULL;
850
0
  }
851
#ifdef __ANDROID__
852
  if (g_strcmp0(subsystem, "block") == 0)
853
    return g_strdup_printf("/dev/block/%s", fn);
854
#endif
855
0
  return g_strdup_printf("/dev/%s", fn);
856
0
}
857
858
/**
859
 * fu_udev_device_get_devpath:
860
 * @self: a #FuUdevDevice
861
 *
862
 * Sets the physical ID from the sysfs path, with the `/sys` prefixed removed.
863
 *
864
 * Returns: The udev-compatble device path, or %NULL on error.
865
 **/
866
gchar *
867
fu_udev_device_get_devpath(FuUdevDevice *self)
868
0
{
869
0
  gchar *full_devpath;
870
0
  if (fu_udev_device_get_sysfs_path(self) == NULL)
871
0
    return NULL;
872
0
  full_devpath = g_strrstr(fu_udev_device_get_sysfs_path(self), "/sys");
873
0
  if (full_devpath == NULL)
874
0
    return NULL;
875
0
  return g_strdup(full_devpath + 4);
876
0
}
877
878
/**
879
 * fu_udev_device_set_physical_id:
880
 * @self: a #FuUdevDevice
881
 * @subsystems: a subsystem string, e.g. `pci,usb,scsi:scsi_target`
882
 * @error: (nullable): optional return location for an error
883
 *
884
 * Sets the physical ID from the device subsystem. Plugins should choose the
885
 * subsystem that is "deepest" in the udev tree, for instance choosing `usb`
886
 * over `pci` for a mouse device.
887
 *
888
 * The devtype can also be specified for a specific device, which is useful when the
889
 * subsystem alone is not enough to identify the physical device. e.g. ignoring the
890
 * specific LUNs for a SCSI device.
891
 *
892
 * Returns: %TRUE if the physical device was set.
893
 **/
894
gboolean
895
fu_udev_device_set_physical_id(FuUdevDevice *self, const gchar *subsystems, GError **error)
896
0
{
897
0
  const gchar *subsystem = NULL;
898
0
  g_autofree gchar *physical_id = NULL;
899
0
  g_auto(GStrv) split = NULL;
900
0
  g_autoptr(FuUdevDevice) udev_device = NULL;
901
902
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
903
0
  g_return_val_if_fail(subsystems != NULL, FALSE);
904
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
905
906
  /* look for each subsystem[:devtype] in turn */
907
0
  split = g_strsplit(subsystems, ",", -1);
908
0
  for (guint i = 0; split[i] != NULL; i++) {
909
0
    g_autoptr(FuUdevDevice) device_parent = NULL;
910
911
    /* do we match */
912
0
    if (fu_udev_device_match_subsystem(self, split[i])) {
913
0
      udev_device = g_object_ref(self);
914
0
      break;
915
0
    }
916
917
    /* does a parent match? */
918
0
    device_parent = FU_UDEV_DEVICE(
919
0
        fu_device_get_backend_parent_with_subsystem(FU_DEVICE(self), split[i], NULL));
920
0
    if (device_parent != NULL) {
921
0
      udev_device = g_object_ref(device_parent);
922
0
      break;
923
0
    }
924
0
  }
925
0
  if (udev_device == NULL) {
926
0
    g_autofree gchar *str = fu_udev_device_get_parent_subsystems(self);
927
0
    g_set_error(error,
928
0
          FWUPD_ERROR,
929
0
          FWUPD_ERROR_NOT_FOUND,
930
0
          "failed to find device with subsystems %s, only got %s",
931
0
          subsystems,
932
0
          str);
933
0
    return FALSE;
934
0
  }
935
936
0
  subsystem = fu_udev_device_get_subsystem(udev_device);
937
0
  if (subsystem == NULL && fu_device_get_physical_id(FU_DEVICE(udev_device)) != NULL) {
938
0
    fu_device_set_physical_id(FU_DEVICE(self),
939
0
            fu_device_get_physical_id(FU_DEVICE(udev_device)));
940
0
    return TRUE;
941
0
  }
942
0
  if (g_strcmp0(subsystem, "pci") == 0) {
943
0
    g_autofree gchar *prop_id =
944
0
        fu_udev_device_read_property(udev_device, "PCI_SLOT_NAME", error);
945
0
    if (prop_id == NULL)
946
0
      return FALSE;
947
0
    physical_id = g_strdup_printf("PCI_SLOT_NAME=%s", prop_id);
948
0
  } else if (g_strcmp0(subsystem, "usb") == 0 || g_strcmp0(subsystem, "mmc") == 0 ||
949
0
       g_strcmp0(subsystem, "i2c") == 0 || g_strcmp0(subsystem, "platform") == 0 ||
950
0
       g_strcmp0(subsystem, "mtd") == 0 || g_strcmp0(subsystem, "block") == 0 ||
951
0
       g_strcmp0(subsystem, "gpio") == 0 || g_strcmp0(subsystem, "video4linux") == 0) {
952
0
    g_auto(GStrv) sysfs_parts =
953
0
        g_strsplit(fu_udev_device_get_sysfs_path(udev_device), "/sys", 2);
954
0
    if (sysfs_parts[1] != NULL)
955
0
      physical_id = g_strdup_printf("DEVPATH=%s", sysfs_parts[1]);
956
0
  } else if (g_strcmp0(subsystem, "hid") == 0) {
957
0
    g_autofree gchar *prop_id =
958
0
        fu_udev_device_read_property(udev_device, "HID_PHYS", error);
959
0
    if (prop_id == NULL)
960
0
      return FALSE;
961
0
    physical_id = g_strdup_printf("HID_PHYS=%s", prop_id);
962
0
  } else if (g_strcmp0(subsystem, "drm_dp_aux_dev") == 0) {
963
0
    g_autofree gchar *prop_id =
964
0
        fu_udev_device_read_property(udev_device, "DEVNAME", error);
965
0
    if (prop_id == NULL)
966
0
      return FALSE;
967
0
    physical_id = g_strdup_printf("DEVNAME=%s", prop_id);
968
0
  } else {
969
0
    g_set_error(error,
970
0
          FWUPD_ERROR,
971
0
          FWUPD_ERROR_NOT_SUPPORTED,
972
0
          "cannot handle subsystem %s",
973
0
          subsystem);
974
0
    return FALSE;
975
0
  }
976
977
  /* success */
978
0
  fu_device_set_physical_id(FU_DEVICE(self), physical_id);
979
0
  return TRUE;
980
0
}
981
982
/**
983
 * fu_udev_device_get_io_channel:
984
 * @self: a #FuUdevDevice
985
 *
986
 * Gets the IO channel.
987
 *
988
 * Returns: (transfer none): a #FuIOChannel, or %NULL if the device is not open
989
 **/
990
FuIOChannel *
991
fu_udev_device_get_io_channel(FuUdevDevice *self)
992
0
{
993
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
994
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
995
0
  return priv->io_channel;
996
0
}
997
998
/**
999
 * fu_udev_device_set_io_channel:
1000
 * @self: a #FuUdevDevice
1001
 * @io_channel: a #FuIOChannel
1002
 *
1003
 * Replace the IO channel to use when the device has already been opened.
1004
 * This object will automatically unref @io_channel when fu_device_close() is called.
1005
 **/
1006
void
1007
fu_udev_device_set_io_channel(FuUdevDevice *self, FuIOChannel *io_channel)
1008
0
{
1009
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1010
0
  g_return_if_fail(FU_IS_UDEV_DEVICE(self));
1011
0
  g_return_if_fail(FU_IS_IO_CHANNEL(io_channel));
1012
0
  g_set_object(&priv->io_channel, io_channel);
1013
0
}
1014
1015
/**
1016
 * fu_udev_device_remove_open_flag:
1017
 * @self: a #FuUdevDevice
1018
 * @flag: udev device flag, e.g. %FU_IO_CHANNEL_OPEN_FLAG_READ
1019
 *
1020
 * Removes a open flag.
1021
 **/
1022
void
1023
fu_udev_device_remove_open_flag(FuUdevDevice *self, FuIoChannelOpenFlags flag)
1024
0
{
1025
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1026
0
  g_return_if_fail(FU_IS_UDEV_DEVICE(self));
1027
0
  priv->open_flags &= ~flag;
1028
0
}
1029
1030
/**
1031
 * fu_udev_device_add_open_flag:
1032
 * @self: a #FuUdevDevice
1033
 * @flag: udev device flag, e.g. %FU_IO_CHANNEL_OPEN_FLAG_READ
1034
 *
1035
 * Sets the parameters to use when opening the device.
1036
 *
1037
 * For example %FU_IO_CHANNEL_OPEN_FLAG_READ means that fu_device_open()
1038
 * would use `O_RDONLY` rather than `O_RDWR` which is the default.
1039
 **/
1040
void
1041
fu_udev_device_add_open_flag(FuUdevDevice *self, FuIoChannelOpenFlags flag)
1042
0
{
1043
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1044
0
  g_return_if_fail(FU_IS_UDEV_DEVICE(self));
1045
1046
  /* already set */
1047
0
  if (priv->open_flags & flag)
1048
0
    return;
1049
0
  priv->open_flags |= flag;
1050
0
}
1051
1052
static gboolean
1053
fu_udev_device_open(FuDevice *device, GError **error)
1054
0
{
1055
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
1056
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1057
1058
  /* emulated */
1059
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED))
1060
0
    return TRUE;
1061
1062
  /* old versions of fwupd used to start with OPEN_READ|OPEN_WRITE and then plugins
1063
   * could add more flags, or set the flags back to NONE -- detect and fixup */
1064
0
  if (priv->device_file != NULL && priv->open_flags == FU_IO_CHANNEL_OPEN_FLAG_NONE) {
1065
0
#ifndef SUPPORTED_BUILD
1066
0
    g_autofree gchar *id_display = fu_device_get_id_display(device);
1067
0
    g_critical("%s forgot to call fu_udev_device_add_open_flag() with "
1068
0
         "FU_IO_CHANNEL_OPEN_FLAG_READ and/or FU_IO_CHANNEL_OPEN_FLAG_WRITE",
1069
0
         id_display);
1070
0
#endif
1071
0
    fu_udev_device_add_open_flag(FU_UDEV_DEVICE(self), FU_IO_CHANNEL_OPEN_FLAG_READ);
1072
0
    fu_udev_device_add_open_flag(FU_UDEV_DEVICE(self), FU_IO_CHANNEL_OPEN_FLAG_WRITE);
1073
0
  }
1074
1075
  /* open device */
1076
0
  if (priv->device_file != NULL) {
1077
0
    g_autoptr(FuIOChannel) io_channel = NULL;
1078
0
    io_channel = fu_io_channel_new_file(priv->device_file, priv->open_flags, error);
1079
0
    if (io_channel == NULL)
1080
0
      return FALSE;
1081
0
    g_set_object(&priv->io_channel, io_channel);
1082
0
  }
1083
1084
  /* success */
1085
0
  return TRUE;
1086
0
}
1087
1088
static gboolean
1089
fu_udev_device_rescan(FuDevice *device, GError **error)
1090
0
{
1091
0
  if (fu_device_has_flag(device, FWUPD_DEVICE_FLAG_EMULATED))
1092
0
    return TRUE;
1093
0
  fu_device_probe_invalidate(device);
1094
0
  return fu_device_probe(device, error);
1095
0
}
1096
1097
static gboolean
1098
fu_udev_device_close(FuDevice *device, GError **error)
1099
0
{
1100
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
1101
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1102
1103
  /* emulated */
1104
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED))
1105
0
    return TRUE;
1106
1107
  /* optional */
1108
0
  if (priv->io_channel != NULL) {
1109
0
    if (!fu_io_channel_shutdown(priv->io_channel, error))
1110
0
      return FALSE;
1111
0
  }
1112
1113
  /* success */
1114
0
  return TRUE;
1115
0
}
1116
1117
/**
1118
 * fu_udev_device_reopen:
1119
 * @self: a #FuDevice
1120
 * @error: (nullable): optional return location for an error
1121
 *
1122
 * Closes and opens the device, typically used to close() and open() the device-file which is
1123
 * required by some ioctls.
1124
 *
1125
 * Returns: %TRUE for success
1126
 **/
1127
gboolean
1128
fu_udev_device_reopen(FuUdevDevice *self, GError **error)
1129
0
{
1130
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
1131
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1132
1133
0
  if (!fu_udev_device_close(FU_DEVICE(self), error))
1134
0
    return FALSE;
1135
0
  return fu_udev_device_open(FU_DEVICE(self), error);
1136
0
}
1137
1138
/**
1139
 * fu_udev_device_ioctl_new:
1140
 * @self: a #FuUdevDevice
1141
 *
1142
 * Build a helper to control a device using a low-level request.
1143
 *
1144
 * Returns: (transfer full): a #FuIoctl, or %NULL on error
1145
 **/
1146
FuIoctl *
1147
fu_udev_device_ioctl_new(FuUdevDevice *self)
1148
0
{
1149
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
1150
0
  return fu_ioctl_new(self);
1151
0
}
1152
1153
/* private */
1154
gboolean
1155
fu_udev_device_ioctl(FuUdevDevice *self,
1156
         gulong request,
1157
         guint8 *buf,
1158
         gsize bufsz,
1159
         gint *rc,
1160
         guint timeout,
1161
         FuIoctlFlags flags,
1162
         GError **error)
1163
0
{
1164
#ifdef HAVE_IOCTL_H
1165
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1166
  gint rc_tmp;
1167
  g_autoptr(GTimer) timer = g_timer_new();
1168
1169
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
1170
  g_return_val_if_fail(request != 0x0, FALSE);
1171
  g_return_val_if_fail(buf != NULL, FALSE);
1172
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1173
1174
  /* for fuzzing */
1175
  if (fu_device_has_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_IS_FAKE)) {
1176
    memset(buf, 0x0, bufsz);
1177
    if (rc != NULL)
1178
      *rc = 0;
1179
    return TRUE;
1180
  }
1181
1182
  /* not open! */
1183
  if (priv->io_channel == NULL) {
1184
    g_autofree gchar *id_display = fu_device_get_id_display(FU_DEVICE(self));
1185
    g_set_error(error,
1186
          FWUPD_ERROR,
1187
          FWUPD_ERROR_INTERNAL,
1188
          "%s has not been opened",
1189
          id_display);
1190
    return FALSE;
1191
  }
1192
1193
  /* poll if required  up to the timeout */
1194
  do {
1195
    rc_tmp = ioctl(fu_io_channel_unix_get_fd(priv->io_channel), /* nocheck:blocked */
1196
             request,
1197
             buf);
1198
    if (rc_tmp >= 0)
1199
      break;
1200
  } while ((flags & FU_IOCTL_FLAG_RETRY) && (errno == EINTR || errno == EAGAIN) &&
1201
     g_timer_elapsed(timer, NULL) < timeout * 1000.f);
1202
  if (rc != NULL)
1203
    *rc = rc_tmp;
1204
  if (rc_tmp < 0) {
1205
#ifdef HAVE_ERRNO_H
1206
    if (errno == EPERM) {
1207
      g_set_error_literal(error,
1208
              FWUPD_ERROR,
1209
              FWUPD_ERROR_PERMISSION_DENIED,
1210
              "permission denied");
1211
      return FALSE;
1212
    }
1213
    if (errno == ENOTTY) {
1214
      g_set_error_literal(error,
1215
              FWUPD_ERROR,
1216
              FWUPD_ERROR_NOT_SUPPORTED,
1217
              "permission denied");
1218
      return FALSE;
1219
    }
1220
    if (errno == ENODEV) {
1221
      g_set_error_literal(error,
1222
              FWUPD_ERROR,
1223
              FWUPD_ERROR_NOT_FOUND,
1224
              "no such device");
1225
      return FALSE;
1226
    }
1227
    if (errno == EOPNOTSUPP) {
1228
      g_set_error_literal(error,
1229
              FWUPD_ERROR,
1230
              FWUPD_ERROR_NOT_SUPPORTED,
1231
              "not supported");
1232
      return FALSE;
1233
    }
1234
    g_set_error(error,
1235
          FWUPD_ERROR,
1236
          FWUPD_ERROR_INTERNAL,
1237
          "ioctl error: %s [%i]",
1238
          fwupd_strerror(errno),
1239
          errno);
1240
#else
1241
    g_set_error_literal(error,
1242
            FWUPD_ERROR,
1243
            FWUPD_ERROR_INTERNAL,
1244
            "unspecified ioctl error");
1245
#endif
1246
    return FALSE;
1247
  }
1248
1249
  /* success */
1250
  return TRUE;
1251
#else
1252
0
  g_set_error_literal(error,
1253
0
          FWUPD_ERROR,
1254
0
          FWUPD_ERROR_NOT_SUPPORTED,
1255
0
          "not supported as <sys/ioctl.h> not found");
1256
0
  return FALSE;
1257
0
#endif
1258
0
}
1259
1260
/**
1261
 * fu_udev_device_pread:
1262
 * @self: a #FuUdevDevice
1263
 * @port: offset address
1264
 * @buf: (in): data
1265
 * @bufsz: size of @buf
1266
 * @error: (nullable): optional return location for an error
1267
 *
1268
 * Read a buffer from a file descriptor at a given offset.
1269
 *
1270
 * Returns: %TRUE for success
1271
 **/
1272
gboolean
1273
fu_udev_device_pread(FuUdevDevice *self, goffset port, guint8 *buf, gsize bufsz, GError **error)
1274
0
{
1275
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1276
0
  FuDeviceEvent *event = NULL;
1277
0
  g_autofree gchar *event_id = NULL;
1278
1279
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
1280
0
  g_return_val_if_fail(buf != NULL, FALSE);
1281
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1282
1283
  /* need event ID */
1284
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
1285
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
1286
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
1287
0
    event_id = g_strdup_printf("Pread:"
1288
0
             "Port=0x%x,"
1289
0
             "Length=0x%x",
1290
0
             (guint)port,
1291
0
             (guint)bufsz);
1292
0
  }
1293
1294
  /* emulated */
1295
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
1296
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
1297
0
    if (event == NULL)
1298
0
      return FALSE;
1299
0
    return fu_device_event_copy_data(event, "Data", buf, bufsz, NULL, error);
1300
0
  }
1301
1302
  /* save */
1303
0
  if (event_id != NULL)
1304
0
    event = fu_device_save_event(FU_DEVICE(self), event_id);
1305
1306
  /* not open! */
1307
0
  if (priv->io_channel == NULL) {
1308
0
    g_autofree gchar *id_display = fu_device_get_id_display(FU_DEVICE(self));
1309
0
    g_set_error(error,
1310
0
          FWUPD_ERROR,
1311
0
          FWUPD_ERROR_INTERNAL,
1312
0
          "%s has not been opened",
1313
0
          id_display);
1314
0
    return FALSE;
1315
0
  }
1316
1317
#ifdef HAVE_PWRITE
1318
  if (pread(fu_io_channel_unix_get_fd(priv->io_channel), buf, bufsz, port) != (gssize)bufsz) {
1319
    g_set_error(error,
1320
          G_IO_ERROR, /* nocheck:error */
1321
#ifdef HAVE_ERRNO_H
1322
          g_io_error_from_errno(errno),
1323
#else
1324
          G_IO_ERROR_FAILED, /* nocheck:error */
1325
#endif
1326
          "failed to read from port 0x%04x: %s",
1327
          (guint)port,
1328
          fwupd_strerror(errno));
1329
    fwupd_error_convert(error);
1330
    return FALSE;
1331
  }
1332
1333
  /* save response */
1334
  if (event != NULL)
1335
    fu_device_event_set_data(event, "Data", buf, bufsz);
1336
  return TRUE;
1337
#else
1338
0
  g_set_error_literal(error,
1339
0
          FWUPD_ERROR,
1340
0
          FWUPD_ERROR_NOT_SUPPORTED,
1341
0
          "Not supported as pread() is unavailable");
1342
0
  return FALSE;
1343
0
#endif
1344
0
}
1345
1346
/**
1347
 * fu_udev_device_seek:
1348
 * @self: a #FuUdevDevice
1349
 * @offset: offset address
1350
 * @error: (nullable): optional return location for an error
1351
 *
1352
 * Seeks a file descriptor to a given offset.
1353
 *
1354
 * Returns: %TRUE for success
1355
 **/
1356
gboolean
1357
fu_udev_device_seek(FuUdevDevice *self, goffset offset, GError **error)
1358
0
{
1359
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1360
1361
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
1362
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1363
1364
  /* emulated */
1365
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
1366
0
    priv->emulated_offset = offset;
1367
0
    return TRUE;
1368
0
  }
1369
1370
  /* not open! */
1371
0
  if (priv->io_channel == NULL) {
1372
0
    g_autofree gchar *id_display = fu_device_get_id_display(FU_DEVICE(self));
1373
0
    g_set_error(error,
1374
0
          FWUPD_ERROR,
1375
0
          FWUPD_ERROR_INTERNAL,
1376
0
          "%s has not been opened",
1377
0
          id_display);
1378
0
    return FALSE;
1379
0
  }
1380
0
  if (!fu_io_channel_seek(priv->io_channel, offset, error))
1381
0
    return FALSE;
1382
1383
  /* used for the event ID when saving */
1384
0
  priv->emulated_offset = offset;
1385
0
  return TRUE;
1386
0
}
1387
1388
/**
1389
 * fu_udev_device_pwrite:
1390
 * @self: a #FuUdevDevice
1391
 * @port: offset address
1392
 * @buf: (out): data
1393
 * @bufsz: size of @data
1394
 * @error: (nullable): optional return location for an error
1395
 *
1396
 * Write a buffer to a file descriptor at a given offset.
1397
 *
1398
 * Returns: %TRUE for success
1399
 **/
1400
gboolean
1401
fu_udev_device_pwrite(FuUdevDevice *self,
1402
          goffset port,
1403
          const guint8 *buf,
1404
          gsize bufsz,
1405
          GError **error)
1406
0
{
1407
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1408
0
  FuDeviceEvent *event = NULL;
1409
0
  g_autofree gchar *event_id = NULL;
1410
1411
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
1412
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1413
1414
  /* emulated */
1415
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) &&
1416
0
      !fu_device_check_fwupd_version(FU_DEVICE(self), "2.0.13")) {
1417
0
    return TRUE;
1418
0
  }
1419
1420
  /* need event ID */
1421
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
1422
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
1423
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
1424
0
    g_autofree gchar *data_base64 = fu_base64_encode(buf, bufsz);
1425
0
    event_id = g_strdup_printf("Pwrite:"
1426
0
             "Port=0x%x,"
1427
0
             "Data=%s,"
1428
0
             "Length=0x%x",
1429
0
             (guint)port,
1430
0
             data_base64,
1431
0
             (guint)bufsz);
1432
0
  }
1433
1434
  /* emulated */
1435
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
1436
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
1437
0
    if (event == NULL)
1438
0
      return FALSE;
1439
0
    return TRUE;
1440
0
  }
1441
1442
  /* save */
1443
0
  if (event_id != NULL)
1444
0
    event = fu_device_save_event(FU_DEVICE(self), event_id);
1445
1446
  /* for fuzzing */
1447
0
  if (fu_device_has_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_IS_FAKE))
1448
0
    return TRUE;
1449
1450
  /* not open! */
1451
0
  if (priv->io_channel == NULL) {
1452
0
    g_autofree gchar *id_display = fu_device_get_id_display(FU_DEVICE(self));
1453
0
    g_set_error(error,
1454
0
          FWUPD_ERROR,
1455
0
          FWUPD_ERROR_INTERNAL,
1456
0
          "%s has not been opened",
1457
0
          id_display);
1458
0
    return FALSE;
1459
0
  }
1460
1461
#ifdef HAVE_PWRITE
1462
  if (pwrite(fu_io_channel_unix_get_fd(priv->io_channel), buf, bufsz, port) !=
1463
      (gssize)bufsz) {
1464
    g_set_error(error,
1465
          G_IO_ERROR, /* nocheck:error */
1466
#ifdef HAVE_ERRNO_H
1467
          g_io_error_from_errno(errno),
1468
#else
1469
          G_IO_ERROR_FAILED, /* nocheck:blocked */
1470
#endif
1471
          "failed to write to port %04x: %s",
1472
          (guint)port,
1473
          fwupd_strerror(errno));
1474
    fwupd_error_convert(error);
1475
    return FALSE;
1476
  }
1477
1478
  /* save response */
1479
  if (event != NULL)
1480
    fu_device_event_set_data(event, "Data", buf, bufsz);
1481
1482
  /* success */
1483
  return TRUE;
1484
#else
1485
0
  g_set_error_literal(error,
1486
0
          FWUPD_ERROR,
1487
0
          FWUPD_ERROR_NOT_SUPPORTED,
1488
0
          "Not supported as pwrite() is unavailable");
1489
0
  return FALSE;
1490
0
#endif
1491
0
}
1492
1493
/**
1494
 * fu_udev_device_read:
1495
 * @self: a #FuUdevDevice
1496
 * @buf: (in): data
1497
 * @bufsz: size of @buf
1498
 * @bytes_read: (out) (nullable): data written to @buf
1499
 * @timeout_ms: timeout in ms
1500
 * @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
1501
 * @error: (nullable): optional return location for an error
1502
 *
1503
 * Read a buffer from a file descriptor.
1504
 *
1505
 * Returns: %TRUE for success
1506
 **/
1507
gboolean
1508
fu_udev_device_read(FuUdevDevice *self,
1509
        guint8 *buf,
1510
        gsize bufsz,
1511
        gsize *bytes_read,
1512
        guint timeout_ms,
1513
        FuIoChannelFlags flags,
1514
        GError **error)
1515
0
{
1516
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1517
0
  FuDeviceEvent *event = NULL;
1518
0
  gsize buflen_tmp = 0;
1519
0
  g_autofree gchar *event_id = NULL;
1520
0
  g_autoptr(GError) error_local = NULL;
1521
1522
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
1523
0
  g_return_val_if_fail(buf != NULL, FALSE);
1524
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1525
1526
  /* need event ID */
1527
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
1528
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
1529
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
1530
    /* the fwupd version is only set when loading emulation data, so always use the
1531
     * newest format when saving -- the created value is 2025-10-24T15:32:39.198922Z
1532
     * i.e. just bnr-dp-0.13.zip */
1533
0
    if (!fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
1534
0
        fu_device_check_fwupd_version(FU_DEVICE(self), "2.1.2") ||
1535
0
        fu_device_get_created(FU_DEVICE(self)) == 0x68FB9C17) {
1536
0
      event_id = g_strdup_printf("Read:Length=0x%x,Offset=0x%x",
1537
0
               (guint)bufsz,
1538
0
               (guint)priv->emulated_offset);
1539
0
    } else {
1540
0
      event_id = g_strdup_printf("Read:Length=0x%x", (guint)bufsz);
1541
0
    }
1542
0
  }
1543
1544
  /* emulated */
1545
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
1546
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
1547
0
    if (event == NULL)
1548
0
      return FALSE;
1549
0
    if (!fu_device_event_check_error(event, error))
1550
0
      return FALSE;
1551
0
    return fu_device_event_copy_data(event, "Data", buf, bufsz, bytes_read, error);
1552
0
  }
1553
1554
  /* save */
1555
0
  if (event_id != NULL)
1556
0
    event = fu_device_save_event(FU_DEVICE(self), event_id);
1557
1558
  /* for fuzzing */
1559
0
  if (fu_device_has_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_IS_FAKE)) {
1560
0
    memset(buf, 0x0, bufsz);
1561
0
    if (bytes_read != NULL)
1562
0
      *bytes_read = bufsz;
1563
0
    return TRUE;
1564
0
  }
1565
1566
  /* not open! */
1567
0
  if (priv->io_channel == NULL) {
1568
0
    g_autofree gchar *id_display = fu_device_get_id_display(FU_DEVICE(self));
1569
0
    g_set_error(error,
1570
0
          FWUPD_ERROR,
1571
0
          FWUPD_ERROR_INTERNAL,
1572
0
          "%s has not been opened",
1573
0
          id_display);
1574
0
    return FALSE;
1575
0
  }
1576
0
  if (!fu_io_channel_read_raw(priv->io_channel,
1577
0
            buf,
1578
0
            bufsz,
1579
0
            &buflen_tmp,
1580
0
            timeout_ms,
1581
0
            flags,
1582
0
            &error_local)) {
1583
0
    if (event != NULL)
1584
0
      fu_device_event_set_error(event, error_local);
1585
0
    g_propagate_error(error, g_steal_pointer(&error_local));
1586
0
    return FALSE;
1587
0
  }
1588
0
  if (bytes_read != NULL)
1589
0
    *bytes_read = buflen_tmp;
1590
1591
  /* save response */
1592
0
  if (event != NULL)
1593
0
    fu_device_event_set_data(event, "Data", buf, buflen_tmp);
1594
0
  return TRUE;
1595
0
}
1596
1597
/**
1598
 * fu_udev_device_read_bytes:
1599
 * @self: a #FuUdevDevice
1600
 * @count: bytes to read
1601
 * @timeout_ms: timeout in ms
1602
 * @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
1603
 * @error: (nullable): optional return location for an error
1604
 *
1605
 * Read a buffer from a file descriptor.
1606
 *
1607
 * Returns: (transfer full): A #GBytes, or %NULL
1608
 **/
1609
GBytes *
1610
fu_udev_device_read_bytes(FuUdevDevice *self,
1611
        gsize count,
1612
        guint timeout_ms,
1613
        FuIoChannelFlags flags,
1614
        GError **error)
1615
0
{
1616
0
  gsize bytes_read = 0;
1617
0
  g_autofree guint8 *buf = NULL;
1618
1619
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
1620
0
  g_return_val_if_fail(count > 0, NULL);
1621
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
1622
1623
0
  buf = g_malloc0(count);
1624
0
  if (!fu_udev_device_read(self, buf, count, &bytes_read, timeout_ms, flags, error))
1625
0
    return NULL;
1626
0
  return g_bytes_new(buf, bytes_read);
1627
0
}
1628
1629
/**
1630
 * fu_udev_device_read_byte_array:
1631
 * @self: a #FuUdevDevice
1632
 * @count: bytes to read
1633
 * @timeout_ms: timeout in ms
1634
 * @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
1635
 * @error: (nullable): optional return location for an error
1636
 *
1637
 * Read a buffer from a file descriptor.
1638
 *
1639
 * Returns: (transfer full): A #GByteArray, or %NULL
1640
 **/
1641
GByteArray *
1642
fu_udev_device_read_byte_array(FuUdevDevice *self,
1643
             gsize count,
1644
             guint timeout_ms,
1645
             FuIoChannelFlags flags,
1646
             GError **error)
1647
0
{
1648
0
  gsize bytes_read = 0;
1649
0
  g_autoptr(GByteArray) buf = g_byte_array_new();
1650
1651
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
1652
0
  g_return_val_if_fail(count > 0, NULL);
1653
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
1654
1655
0
  fu_byte_array_set_size(buf, count, 0x0);
1656
0
  if (!fu_udev_device_read(self, buf->data, buf->len, &bytes_read, timeout_ms, flags, error))
1657
0
    return NULL;
1658
0
  g_byte_array_set_size(buf, bytes_read);
1659
0
  return g_steal_pointer(&buf);
1660
0
}
1661
1662
/**
1663
 * fu_udev_device_write:
1664
 * @self: a #FuUdevDevice
1665
 * @buf: (out): data
1666
 * @bufsz: size of @data
1667
 * @timeout_ms: timeout in ms
1668
 * @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
1669
 * @error: (nullable): optional return location for an error
1670
 *
1671
 * Write a buffer to a file descriptor.
1672
 *
1673
 * Returns: %TRUE for success
1674
 **/
1675
gboolean
1676
fu_udev_device_write(FuUdevDevice *self,
1677
         const guint8 *buf,
1678
         gsize bufsz,
1679
         guint timeout_ms,
1680
         FuIoChannelFlags flags,
1681
         GError **error)
1682
0
{
1683
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
1684
0
  FuDeviceEvent *event = NULL;
1685
0
  g_autofree gchar *event_id = NULL;
1686
0
  g_autoptr(GError) error_local = NULL;
1687
1688
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
1689
0
  g_return_val_if_fail(buf != NULL, FALSE);
1690
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1691
1692
  /* sanity check */
1693
0
  if ((priv->open_flags & FU_IO_CHANNEL_OPEN_FLAG_WRITE) == 0) {
1694
0
    g_critical("trying to write without writable device-file; "
1695
0
         "use fu_udev_device_add_open_flag(self, FU_IO_CHANNEL_OPEN_FLAG_WRITE)");
1696
0
  }
1697
1698
  /* emulated */
1699
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
1700
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
1701
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
1702
0
    g_autofree gchar *data_base64 = fu_base64_encode(buf, bufsz);
1703
0
    event_id = g_strdup_printf("Write:Data=%s,Length=0x%x", data_base64, (guint)bufsz);
1704
0
  }
1705
1706
  /* emulated */
1707
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
1708
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
1709
0
    if (event == NULL)
1710
0
      return FALSE;
1711
0
    if (!fu_device_event_check_error(event, error))
1712
0
      return FALSE;
1713
0
    return event != NULL;
1714
0
  }
1715
1716
  /* save */
1717
0
  if (event_id != NULL)
1718
0
    event = fu_device_save_event(FU_DEVICE(self), event_id);
1719
1720
  /* for fuzzing */
1721
0
  if (fu_device_has_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_IS_FAKE))
1722
0
    return TRUE;
1723
1724
  /* not open! */
1725
0
  if (priv->io_channel == NULL) {
1726
0
    g_autofree gchar *id_display = fu_device_get_id_display(FU_DEVICE(self));
1727
0
    g_set_error(error,
1728
0
          FWUPD_ERROR,
1729
0
          FWUPD_ERROR_INTERNAL,
1730
0
          "%s has not been opened",
1731
0
          id_display);
1732
0
    return FALSE;
1733
0
  }
1734
0
  if (!fu_io_channel_write_raw(priv->io_channel,
1735
0
             buf,
1736
0
             bufsz,
1737
0
             timeout_ms,
1738
0
             flags,
1739
0
             &error_local)) {
1740
0
    if (event != NULL)
1741
0
      fu_device_event_set_error(event, error_local);
1742
0
    g_propagate_error(error, g_steal_pointer(&error_local));
1743
0
    return FALSE;
1744
0
  }
1745
1746
  /* success */
1747
0
  return TRUE;
1748
0
}
1749
1750
/**
1751
 * fu_udev_device_write_bytes:
1752
 * @self: a #FuUdevDevice
1753
 * @blob: a #GBytes
1754
 * @timeout_ms: timeout in ms
1755
 * @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
1756
 * @error: (nullable): optional return location for an error
1757
 *
1758
 * Write a buffer to a file descriptor.
1759
 *
1760
 * Returns: %TRUE for success
1761
 **/
1762
gboolean
1763
fu_udev_device_write_bytes(FuUdevDevice *self,
1764
         GBytes *blob,
1765
         guint timeout_ms,
1766
         FuIoChannelFlags flags,
1767
         GError **error)
1768
0
{
1769
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
1770
0
  g_return_val_if_fail(blob != NULL, FALSE);
1771
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1772
0
  return fu_udev_device_write(self,
1773
0
            g_bytes_get_data(blob, NULL),
1774
0
            g_bytes_get_size(blob),
1775
0
            timeout_ms,
1776
0
            flags,
1777
0
            error);
1778
0
}
1779
1780
/**
1781
 * fu_udev_device_write_byte_array:
1782
 * @self: a #FuUdevDevice
1783
 * @buf: a #GByteArray
1784
 * @timeout_ms: timeout in ms
1785
 * @flags: channel flags, e.g. %FU_IO_CHANNEL_FLAG_SINGLE_SHOT
1786
 * @error: (nullable): optional return location for an error
1787
 *
1788
 * Write a buffer to a file descriptor.
1789
 *
1790
 * Returns: %TRUE for success
1791
 **/
1792
gboolean
1793
fu_udev_device_write_byte_array(FuUdevDevice *self,
1794
        GByteArray *buf,
1795
        guint timeout_ms,
1796
        FuIoChannelFlags flags,
1797
        GError **error)
1798
0
{
1799
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
1800
0
  g_return_val_if_fail(buf != NULL, FALSE);
1801
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
1802
0
  return fu_udev_device_write(self, buf->data, buf->len, timeout_ms, flags, error);
1803
0
}
1804
1805
/**
1806
 * fu_udev_device_list_sysfs:
1807
 * @self: a #FuUdevDevice
1808
 * @error: (nullable): optional return location for an error
1809
 *
1810
 * Lists all the sysfs attributes.
1811
 *
1812
 * Returns: (transfer container) (element-type utf8): basenames, or %NULL
1813
 **/
1814
GPtrArray *
1815
fu_udev_device_list_sysfs(FuUdevDevice *self, GError **error)
1816
0
{
1817
0
  FuDeviceEvent *event = NULL;
1818
0
  const gchar *basename;
1819
0
  g_autofree gchar *event_id = NULL;
1820
0
  g_autoptr(GDir) dir = NULL;
1821
0
  g_autoptr(GPtrArray) attrs = g_ptr_array_new_with_free_func(g_free);
1822
1823
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
1824
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
1825
1826
  /* need event ID */
1827
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
1828
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
1829
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
1830
0
    event_id = g_strdup("ListAttr");
1831
0
  }
1832
1833
  /* emulated */
1834
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
1835
0
    const gchar *value;
1836
0
    g_auto(GStrv) attrs_strv = NULL;
1837
1838
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
1839
0
    if (event == NULL)
1840
0
      return NULL;
1841
0
    value = fu_device_event_get_str(event, "Data", error);
1842
0
    if (value == NULL)
1843
0
      return NULL;
1844
0
    attrs_strv = g_strsplit(value, "\n", -1);
1845
0
    for (guint i = 0; attrs_strv[i] != NULL; i++)
1846
0
      g_ptr_array_add(attrs, g_strdup(attrs_strv[i]));
1847
0
    return g_steal_pointer(&attrs);
1848
0
  }
1849
1850
  /* save */
1851
0
  if (event_id != NULL)
1852
0
    event = fu_device_save_event(FU_DEVICE(self), event_id);
1853
1854
  /* list the files and directories */
1855
0
  if (fu_udev_device_get_sysfs_path(self) == NULL) {
1856
0
    g_set_error_literal(error,
1857
0
            FWUPD_ERROR,
1858
0
            FWUPD_ERROR_INTERNAL,
1859
0
            "sysfs_path undefined");
1860
0
    return NULL;
1861
0
  }
1862
0
  dir = g_dir_open(fu_udev_device_get_sysfs_path(self), 0, error);
1863
0
  if (dir == NULL) {
1864
0
    fwupd_error_convert(error);
1865
0
    return NULL;
1866
0
  }
1867
0
  while ((basename = g_dir_read_name(dir)) != NULL)
1868
0
    g_ptr_array_add(attrs, g_strdup(basename));
1869
1870
  /* save for emulation */
1871
0
  if (event != NULL) {
1872
0
    g_autofree gchar *value = fu_strjoin("\n", attrs);
1873
0
    fu_device_event_set_str(event, "Data", value);
1874
0
  }
1875
1876
  /* success */
1877
0
  return g_steal_pointer(&attrs);
1878
0
}
1879
1880
/**
1881
 * fu_udev_device_read_sysfs:
1882
 * @self: a #FuUdevDevice
1883
 * @attr: sysfs attribute name
1884
 * @timeout_ms: IO timeout in milliseconds
1885
 * @error: (nullable): optional return location for an error
1886
 *
1887
 * Reads data from a sysfs attribute, removing any newline trailing chars.
1888
 *
1889
 * Returns: (transfer full): string value, or %NULL
1890
 **/
1891
gchar *
1892
fu_udev_device_read_sysfs(FuUdevDevice *self, const gchar *attr, guint timeout_ms, GError **error)
1893
0
{
1894
0
  FuDeviceEvent *event = NULL;
1895
0
  g_autofree gchar *event_id = NULL;
1896
0
  g_autofree gchar *path = NULL;
1897
0
  g_autofree gchar *value = NULL;
1898
0
  g_autoptr(FuIOChannel) io_channel = NULL;
1899
0
  g_autoptr(GByteArray) buf = NULL;
1900
0
  g_autoptr(GError) error_local = NULL;
1901
1902
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
1903
0
  g_return_val_if_fail(attr != NULL, NULL);
1904
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
1905
1906
  /* need event ID */
1907
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
1908
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
1909
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
1910
0
    event_id = g_strdup_printf("ReadAttr:Attr=%s", attr);
1911
0
  }
1912
1913
  /* emulated */
1914
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
1915
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
1916
0
    if (event == NULL)
1917
0
      return NULL;
1918
0
    if (!fu_device_event_check_error(event, error))
1919
0
      return NULL;
1920
0
    return g_strdup(fu_device_event_get_str(event, "Data", error));
1921
0
  }
1922
1923
  /* save */
1924
0
  if (event_id != NULL)
1925
0
    event = fu_device_save_event(FU_DEVICE(self), event_id);
1926
1927
  /* open the file */
1928
0
  if (fu_udev_device_get_sysfs_path(self) == NULL) {
1929
0
    g_set_error_literal(error,
1930
0
            FWUPD_ERROR,
1931
0
            FWUPD_ERROR_INTERNAL,
1932
0
            "sysfs_path undefined");
1933
0
    return NULL;
1934
0
  }
1935
0
  path = g_build_filename(fu_udev_device_get_sysfs_path(self), attr, NULL);
1936
0
  io_channel = fu_io_channel_new_file(path, FU_IO_CHANNEL_OPEN_FLAG_READ, error);
1937
0
  if (io_channel == NULL)
1938
0
    return NULL;
1939
0
  buf = fu_io_channel_read_byte_array(io_channel,
1940
0
              -1,
1941
0
              timeout_ms,
1942
0
              FU_IO_CHANNEL_FLAG_NONE,
1943
0
              &error_local);
1944
0
  if (buf == NULL) {
1945
0
    g_prefix_error(&error_local, "failed read of %s: ", path);
1946
0
    if (event != NULL)
1947
0
      fu_device_event_set_error(event, error_local);
1948
0
    g_propagate_error(error, g_steal_pointer(&error_local));
1949
0
    return NULL;
1950
0
  }
1951
0
  if (!g_utf8_validate((const gchar *)buf->data, buf->len, NULL)) {
1952
0
    g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_INVALID_DATA, "non UTF-8 data");
1953
0
    return NULL;
1954
0
  }
1955
1956
  /* save response */
1957
0
  value = g_strndup((const gchar *)buf->data, buf->len);
1958
1959
  /* remove the trailing newline */
1960
0
  if (buf->len > 0) {
1961
0
    if (value[buf->len - 1] == '\n')
1962
0
      value[buf->len - 1] = '\0';
1963
0
  }
1964
1965
  /* save for emulation */
1966
0
  if (event != NULL)
1967
0
    fu_device_event_set_str(event, "Data", value);
1968
1969
  /* success */
1970
0
  return g_steal_pointer(&value);
1971
0
}
1972
1973
/**
1974
 * fu_udev_device_read_sysfs_bytes:
1975
 * @self: a #FuUdevDevice
1976
 * @attr: sysfs attribute name
1977
 * @count: maximum bytes to read, or -1 for no limit
1978
 * @timeout_ms: IO timeout in milliseconds
1979
 * @error: (nullable): optional return location for an error
1980
 *
1981
 * Reads raw data from a sysfs attribute.
1982
 *
1983
 * Returns: (transfer full): string value, or %NULL
1984
 **/
1985
GBytes *
1986
fu_udev_device_read_sysfs_bytes(FuUdevDevice *self,
1987
        const gchar *attr,
1988
        gssize count,
1989
        guint timeout_ms,
1990
        GError **error)
1991
0
{
1992
0
  FuDeviceEvent *event = NULL;
1993
0
  g_autofree gchar *event_id = NULL;
1994
0
  g_autofree gchar *path = NULL;
1995
0
  g_autoptr(FuIOChannel) io_channel = NULL;
1996
0
  g_autoptr(GBytes) blob = NULL;
1997
1998
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
1999
0
  g_return_val_if_fail(attr != NULL, NULL);
2000
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
2001
2002
  /* need event ID */
2003
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
2004
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
2005
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
2006
0
    event_id = g_strdup_printf("ReadAttr:Attr=%s", attr);
2007
0
  }
2008
2009
  /* emulated */
2010
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
2011
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
2012
0
    if (event == NULL)
2013
0
      return NULL;
2014
0
    return fu_device_event_get_bytes(event, "Data", error);
2015
0
  }
2016
2017
  /* save */
2018
0
  if (event_id != NULL)
2019
0
    event = fu_device_save_event(FU_DEVICE(self), event_id);
2020
2021
  /* open the file */
2022
0
  if (fu_udev_device_get_sysfs_path(self) == NULL) {
2023
0
    g_set_error_literal(error,
2024
0
            FWUPD_ERROR,
2025
0
            FWUPD_ERROR_INTERNAL,
2026
0
            "sysfs_path undefined");
2027
0
    return NULL;
2028
0
  }
2029
0
  path = g_build_filename(fu_udev_device_get_sysfs_path(self), attr, NULL);
2030
0
  io_channel = fu_io_channel_new_file(path, FU_IO_CHANNEL_OPEN_FLAG_READ, error);
2031
0
  if (io_channel == NULL)
2032
0
    return NULL;
2033
0
  blob =
2034
0
      fu_io_channel_read_bytes(io_channel, count, timeout_ms, FU_IO_CHANNEL_FLAG_NONE, error);
2035
0
  if (blob == NULL) {
2036
0
    g_prefix_error(error, "failed read of %s: ", path);
2037
0
    return NULL;
2038
0
  }
2039
2040
  /* save for emulation */
2041
0
  if (event != NULL)
2042
0
    fu_device_event_set_bytes(event, "Data", blob);
2043
2044
  /* success */
2045
0
  return g_steal_pointer(&blob);
2046
0
}
2047
2048
/**
2049
 * fu_udev_device_write_sysfs:
2050
 * @self: a #FuUdevDevice
2051
 * @attr: sysfs attribute name
2052
 * @val: data to write into the attribute
2053
 * @timeout_ms: IO timeout in milliseconds
2054
 * @error: (nullable): optional return location for an error
2055
 *
2056
 * Writes data into a sysfs attribute
2057
 *
2058
 * Returns: %TRUE for success
2059
 **/
2060
gboolean
2061
fu_udev_device_write_sysfs(FuUdevDevice *self,
2062
         const gchar *attr,
2063
         const gchar *val,
2064
         guint timeout_ms,
2065
         GError **error)
2066
0
{
2067
0
  FuDeviceEvent *event = NULL;
2068
0
  g_autofree gchar *event_id = NULL;
2069
0
  g_autofree gchar *path = NULL;
2070
0
  g_autoptr(FuIOChannel) io_channel = NULL;
2071
2072
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
2073
0
  g_return_val_if_fail(attr != NULL, FALSE);
2074
0
  g_return_val_if_fail(val != NULL, FALSE);
2075
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
2076
2077
  /* need event ID */
2078
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
2079
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
2080
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
2081
0
    event_id = g_strdup_printf("WriteAttr:Attr=%s,Data=%s", attr, val);
2082
0
  }
2083
2084
  /* emulated */
2085
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
2086
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
2087
0
    return event != NULL;
2088
0
  }
2089
2090
  /* for fuzzing */
2091
0
  if (fu_device_has_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_IS_FAKE))
2092
0
    return TRUE;
2093
2094
  /* open the file */
2095
0
  if (fu_udev_device_get_sysfs_path(self) == NULL) {
2096
0
    g_set_error_literal(error,
2097
0
            FWUPD_ERROR,
2098
0
            FWUPD_ERROR_INTERNAL,
2099
0
            "sysfs_path undefined");
2100
0
    return FALSE;
2101
0
  }
2102
0
  path = g_build_filename(fu_udev_device_get_sysfs_path(self), attr, NULL);
2103
0
  io_channel = fu_io_channel_new_file(path, FU_IO_CHANNEL_OPEN_FLAG_WRITE, error);
2104
0
  if (io_channel == NULL)
2105
0
    return FALSE;
2106
2107
  /* save */
2108
0
  if (event_id != NULL)
2109
0
    fu_device_save_event(FU_DEVICE(self), event_id);
2110
0
  if (!fu_io_channel_write_raw(io_channel,
2111
0
             (const guint8 *)val,
2112
0
             strlen(val),
2113
0
             timeout_ms,
2114
0
             FU_IO_CHANNEL_FLAG_NONE,
2115
0
             error)) {
2116
0
    g_prefix_error(error, "failed write of %s: ", path);
2117
0
    return FALSE;
2118
0
  }
2119
2120
  /* success */
2121
0
  return TRUE;
2122
0
}
2123
2124
/**
2125
 * fu_udev_device_write_sysfs_byte_array:
2126
 * @self: a #FuUdevDevice
2127
 * @attr: sysfs attribute name
2128
 * @buf: data to write into the attribute
2129
 * @timeout_ms: IO timeout in milliseconds
2130
 * @error: (nullable): optional return location for an error
2131
 *
2132
 * Writes raw data into a sysfs attribute.
2133
 *
2134
 * Returns: %TRUE for success
2135
 **/
2136
gboolean
2137
fu_udev_device_write_sysfs_byte_array(FuUdevDevice *self,
2138
              const gchar *attr,
2139
              GByteArray *buf,
2140
              guint timeout_ms,
2141
              GError **error)
2142
0
{
2143
0
  FuDeviceEvent *event = NULL;
2144
0
  g_autofree gchar *event_id = NULL;
2145
0
  g_autofree gchar *path = NULL;
2146
0
  g_autoptr(FuIOChannel) io_channel = NULL;
2147
2148
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
2149
0
  g_return_val_if_fail(attr != NULL, FALSE);
2150
0
  g_return_val_if_fail(buf != NULL, FALSE);
2151
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
2152
2153
  /* need event ID */
2154
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
2155
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
2156
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
2157
0
    g_autofree gchar *buf_base64 = fu_base64_encode(buf->data, buf->len);
2158
0
    event_id = g_strdup_printf("WriteAttr:Attr=%s,Data=%s", attr, buf_base64);
2159
0
  }
2160
2161
  /* emulated */
2162
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
2163
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
2164
0
    return event != NULL;
2165
0
  }
2166
2167
  /* for fuzzing */
2168
0
  if (fu_device_has_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_IS_FAKE))
2169
0
    return TRUE;
2170
2171
  /* open the file */
2172
0
  if (fu_udev_device_get_sysfs_path(self) == NULL) {
2173
0
    g_set_error_literal(error,
2174
0
            FWUPD_ERROR,
2175
0
            FWUPD_ERROR_INTERNAL,
2176
0
            "sysfs_path undefined");
2177
0
    return FALSE;
2178
0
  }
2179
0
  path = g_build_filename(fu_udev_device_get_sysfs_path(self), attr, NULL);
2180
0
  io_channel = fu_io_channel_new_file(path, FU_IO_CHANNEL_OPEN_FLAG_WRITE, error);
2181
0
  if (io_channel == NULL)
2182
0
    return FALSE;
2183
2184
  /* save */
2185
0
  if (event_id != NULL)
2186
0
    fu_device_save_event(FU_DEVICE(self), event_id);
2187
0
  if (!fu_io_channel_write_byte_array(io_channel,
2188
0
              buf,
2189
0
              timeout_ms,
2190
0
              FU_IO_CHANNEL_FLAG_NONE,
2191
0
              error)) {
2192
0
    g_prefix_error(error, "failed write of %s: ", path);
2193
0
    return FALSE;
2194
0
  }
2195
2196
  /* success */
2197
0
  return TRUE;
2198
0
}
2199
2200
/**
2201
 * fu_udev_device_write_sysfs_bytes:
2202
 * @self: a #FuUdevDevice
2203
 * @attr: sysfs attribute name
2204
 * @blob: data to write into the attribute
2205
 * @timeout_ms: IO timeout in milliseconds
2206
 * @error: (nullable): optional return location for an error
2207
 *
2208
 * Writes raw data into a sysfs attribute.
2209
 *
2210
 * Returns: %TRUE for success
2211
 **/
2212
gboolean
2213
fu_udev_device_write_sysfs_bytes(FuUdevDevice *self,
2214
         const gchar *attr,
2215
         GBytes *blob,
2216
         guint timeout_ms,
2217
         GError **error)
2218
0
{
2219
0
  FuDeviceEvent *event = NULL;
2220
0
  g_autofree gchar *event_id = NULL;
2221
0
  g_autofree gchar *path = NULL;
2222
0
  g_autoptr(FuIOChannel) io_channel = NULL;
2223
2224
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE);
2225
0
  g_return_val_if_fail(attr != NULL, FALSE);
2226
0
  g_return_val_if_fail(blob != NULL, FALSE);
2227
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
2228
2229
  /* need event ID */
2230
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
2231
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
2232
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
2233
0
    g_autofree gchar *buf_base64 =
2234
0
        fu_base64_encode(g_bytes_get_data(blob, NULL), g_bytes_get_size(blob));
2235
0
    event_id = g_strdup_printf("WriteAttr:Attr=%s,Data=%s", attr, buf_base64);
2236
0
  }
2237
2238
  /* emulated */
2239
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
2240
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
2241
0
    return event != NULL;
2242
0
  }
2243
2244
  /* for fuzzing */
2245
0
  if (fu_device_has_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_IS_FAKE))
2246
0
    return TRUE;
2247
2248
  /* open the file */
2249
0
  if (fu_udev_device_get_sysfs_path(self) == NULL) {
2250
0
    g_set_error_literal(error,
2251
0
            FWUPD_ERROR,
2252
0
            FWUPD_ERROR_INTERNAL,
2253
0
            "sysfs_path undefined");
2254
0
    return FALSE;
2255
0
  }
2256
0
  path = g_build_filename(fu_udev_device_get_sysfs_path(self), attr, NULL);
2257
0
  io_channel = fu_io_channel_new_file(path, FU_IO_CHANNEL_OPEN_FLAG_WRITE, error);
2258
0
  if (io_channel == NULL)
2259
0
    return FALSE;
2260
2261
  /* save */
2262
0
  if (event_id != NULL)
2263
0
    fu_device_save_event(FU_DEVICE(self), event_id);
2264
0
  if (!fu_io_channel_write_bytes(io_channel,
2265
0
               blob,
2266
0
               timeout_ms,
2267
0
               FU_IO_CHANNEL_FLAG_NONE,
2268
0
               error)) {
2269
0
    g_prefix_error(error, "failed write of %s: ", path);
2270
0
    return FALSE;
2271
0
  }
2272
2273
  /* success */
2274
0
  return TRUE;
2275
0
}
2276
2277
/**
2278
 * fu_udev_device_get_devtype:
2279
 * @self: a #FuUdevDevice
2280
 *
2281
 * Returns the Udev device type
2282
 *
2283
 * Returns: device type specified in the uevent
2284
 **/
2285
const gchar *
2286
fu_udev_device_get_devtype(FuUdevDevice *self)
2287
0
{
2288
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
2289
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
2290
0
  return priv->devtype;
2291
0
}
2292
2293
/**
2294
 * fu_udev_device_get_subsystem_devtype:
2295
 * @self: a #FuUdevDevice
2296
 *
2297
 * Returns the Udev subsystem and device type, as a string.
2298
 *
2299
 * Returns: (transfer full): `subsystem:devtype`, or just `subsystem` if the latter is unset
2300
 **/
2301
gchar *
2302
fu_udev_device_get_subsystem_devtype(FuUdevDevice *self)
2303
0
{
2304
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
2305
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
2306
0
  if (priv->devtype != NULL)
2307
0
    return g_strdup_printf("%s:%s", priv->subsystem, priv->devtype);
2308
0
  return g_strdup(priv->subsystem);
2309
0
}
2310
2311
/* private */
2312
void
2313
fu_udev_device_add_property(FuUdevDevice *self, const gchar *key, const gchar *value)
2314
0
{
2315
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
2316
0
  g_return_if_fail(FU_IS_UDEV_DEVICE(self));
2317
0
  g_return_if_fail(key != NULL);
2318
2319
  /* this are explicit properties too */
2320
0
  if (g_strcmp0(key, "DEVNAME") == 0) {
2321
0
    if (value != NULL && !g_path_is_absolute(value)) {
2322
0
      g_autofree gchar *device_file =
2323
0
          fu_udev_device_build_devfs_path(self, value);
2324
0
      fu_udev_device_set_device_file(self, device_file);
2325
0
    } else {
2326
0
      fu_udev_device_set_device_file(self, value);
2327
0
    }
2328
0
  }
2329
0
  if (g_strcmp0(key, "DEVTYPE") == 0)
2330
0
    fu_udev_device_set_devtype(self, value);
2331
2332
0
  g_hash_table_insert(priv->properties, g_strdup(key), g_strdup(value));
2333
0
}
2334
2335
/**
2336
 * fu_udev_device_read_property:
2337
 * @self: a #FuUdevDevice
2338
 * @key: uevent key name, e.g. `HID_PHYS`
2339
 * @error: (nullable): optional return location for an error
2340
 *
2341
 * Gets a value from the `uevent` file.
2342
 *
2343
 * Returns: %TRUE for success
2344
 **/
2345
gchar *
2346
fu_udev_device_read_property(FuUdevDevice *self, const gchar *key, GError **error)
2347
0
{
2348
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
2349
0
  FuDeviceEvent *event = NULL;
2350
0
  g_autofree gchar *event_id = NULL;
2351
0
  g_autofree gchar *value = NULL;
2352
2353
0
  g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), NULL);
2354
0
  g_return_val_if_fail(key != NULL, NULL);
2355
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
2356
2357
  /* need event ID */
2358
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
2359
0
      fu_context_has_flag(fu_device_get_context(FU_DEVICE(self)),
2360
0
        FU_CONTEXT_FLAG_SAVE_EVENTS)) {
2361
0
    event_id = g_strdup_printf("ReadProp:Key=%s", key);
2362
0
  }
2363
2364
  /* emulated */
2365
0
  if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED)) {
2366
0
    event = fu_device_load_event(FU_DEVICE(self), event_id, error);
2367
0
    if (event == NULL)
2368
0
      return NULL;
2369
0
    return g_strdup(fu_device_event_get_str(event, "Data", error));
2370
0
  }
2371
2372
  /* save */
2373
0
  if (event_id != NULL)
2374
0
    event = fu_device_save_event(FU_DEVICE(self), event_id);
2375
2376
  /* parse key */
2377
0
  if (!priv->properties_valid) {
2378
0
    g_autofree gchar *str = NULL;
2379
0
    g_auto(GStrv) uevent_lines = NULL;
2380
0
    str = fu_udev_device_read_sysfs(self,
2381
0
            "uevent",
2382
0
            FU_UDEV_DEVICE_ATTR_READ_TIMEOUT_DEFAULT,
2383
0
            error);
2384
0
    if (str == NULL)
2385
0
      return NULL;
2386
0
    uevent_lines = g_strsplit(str, "\n", -1);
2387
0
    for (guint i = 0; uevent_lines[i] != NULL; i++) {
2388
      /* only split KEY=VALUE */
2389
0
      if (g_strstr_len(uevent_lines[i], -1, "=") != NULL) {
2390
0
        g_autofree gchar **kvs = g_strsplit(uevent_lines[i], "=", 2);
2391
0
        g_hash_table_insert(priv->properties,
2392
0
                g_steal_pointer(&kvs[0]),
2393
0
                g_steal_pointer(&kvs[1]));
2394
0
      }
2395
0
    }
2396
0
    priv->properties_valid = TRUE;
2397
0
  }
2398
0
  value = g_strdup(g_hash_table_lookup(priv->properties, key));
2399
0
  if (value == NULL) {
2400
0
    g_set_error(error,
2401
0
          FWUPD_ERROR,
2402
0
          FWUPD_ERROR_NOT_FOUND,
2403
0
          "property key %s was not found",
2404
0
          key);
2405
0
    return NULL;
2406
0
  }
2407
2408
  /* save response */
2409
0
  if (event != NULL)
2410
0
    fu_device_event_set_str(event, "Data", value);
2411
2412
  /* success */
2413
0
  return g_steal_pointer(&value);
2414
0
}
2415
2416
static void
2417
fu_udev_device_export(FuDevice *device, FwupdJsonObject *json_obj, FwupdCodecFlags flags)
2418
0
{
2419
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
2420
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
2421
2422
  /* optional properties */
2423
0
  fwupd_json_object_add_string(json_obj, "GType", "FuUdevDevice");
2424
0
  if (fu_udev_device_get_sysfs_path(self) != NULL) {
2425
0
    fwupd_json_object_add_string(json_obj,
2426
0
               "BackendId",
2427
0
               fu_udev_device_get_sysfs_path(self));
2428
0
  }
2429
0
  if (priv->device_file != NULL)
2430
0
    fwupd_json_object_add_string(json_obj, "DeviceFile", priv->device_file);
2431
0
  if (priv->subsystem != NULL)
2432
0
    fwupd_json_object_add_string(json_obj, "Subsystem", priv->subsystem);
2433
0
  if (priv->devtype != NULL)
2434
0
    fwupd_json_object_add_string(json_obj, "Devtype", priv->devtype);
2435
0
  if (priv->driver != NULL)
2436
0
    fwupd_json_object_add_string(json_obj, "Driver", priv->driver);
2437
0
  if (priv->bind_id != NULL)
2438
0
    fwupd_json_object_add_string(json_obj, "BindId", priv->bind_id);
2439
0
  if (fu_device_get_vid(device) != 0)
2440
0
    fwupd_json_object_add_integer(json_obj, "Vendor", fu_device_get_vid(device));
2441
0
  if (fu_device_get_pid(device) != 0)
2442
0
    fwupd_json_object_add_integer(json_obj, "Model", fu_device_get_pid(device));
2443
0
}
2444
2445
static gboolean
2446
fu_udev_device_build(FuDevice *device, FwupdJsonObject *json_obj, GError **error)
2447
0
{
2448
0
  FuUdevDevice *self = FU_UDEV_DEVICE(device);
2449
0
  const gchar *tmp;
2450
0
  gint64 tmp64 = 0;
2451
2452
0
  if (!fwupd_json_object_get_string_optional(json_obj, "BackendId", &tmp, error))
2453
0
    return FALSE;
2454
0
  if (tmp != NULL)
2455
0
    fu_device_set_backend_id(device, tmp);
2456
0
  if (!fwupd_json_object_get_string_optional(json_obj, "Subsystem", &tmp, error))
2457
0
    return FALSE;
2458
0
  if (tmp != NULL)
2459
0
    fu_udev_device_set_subsystem(self, tmp);
2460
0
  if (!fwupd_json_object_get_string_optional(json_obj, "Devtype", &tmp, error))
2461
0
    return FALSE;
2462
0
  if (tmp != NULL)
2463
0
    fu_udev_device_set_devtype(self, tmp);
2464
0
  if (!fwupd_json_object_get_string_optional(json_obj, "Driver", &tmp, error))
2465
0
    return FALSE;
2466
0
  if (tmp != NULL)
2467
0
    fu_udev_device_set_driver(self, tmp);
2468
0
  if (!fwupd_json_object_get_string_optional(json_obj, "BindId", &tmp, error))
2469
0
    return FALSE;
2470
0
  if (tmp != NULL)
2471
0
    fu_udev_device_set_bind_id(self, tmp);
2472
0
  if (!fwupd_json_object_get_string_optional(json_obj, "DeviceFile", &tmp, error))
2473
0
    return FALSE;
2474
0
  if (tmp != NULL)
2475
0
    fu_udev_device_set_device_file(self, tmp);
2476
0
  if (!fwupd_json_object_get_integer_with_default(json_obj, "Vendor", &tmp64, 0x0, error))
2477
0
    return FALSE;
2478
0
  if (tmp64 != 0)
2479
0
    fu_device_set_vid(device, tmp64);
2480
0
  if (!fwupd_json_object_get_integer_with_default(json_obj, "Model", &tmp64, 0x0, error))
2481
0
    return FALSE;
2482
0
  if (tmp64 != 0)
2483
0
    fu_device_set_pid(device, tmp64);
2484
2485
  /* success */
2486
0
  return TRUE;
2487
0
}
2488
2489
static void
2490
fu_udev_device_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
2491
0
{
2492
0
  FuUdevDevice *self = FU_UDEV_DEVICE(object);
2493
0
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
2494
0
  switch (prop_id) {
2495
0
  case PROP_SUBSYSTEM:
2496
0
    g_value_set_string(value, priv->subsystem);
2497
0
    break;
2498
0
  case PROP_BIND_ID:
2499
0
    g_value_set_string(value, priv->bind_id);
2500
0
    break;
2501
0
  case PROP_DRIVER:
2502
0
    g_value_set_string(value, priv->driver);
2503
0
    break;
2504
0
  case PROP_DEVICE_FILE:
2505
0
    g_value_set_string(value, priv->device_file);
2506
0
    break;
2507
0
  case PROP_DEVTYPE:
2508
0
    g_value_set_string(value, priv->devtype);
2509
0
    break;
2510
0
  default:
2511
0
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
2512
0
    break;
2513
0
  }
2514
0
}
2515
2516
static void
2517
fu_udev_device_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
2518
0
{
2519
0
  FuUdevDevice *self = FU_UDEV_DEVICE(object);
2520
0
  switch (prop_id) {
2521
0
  case PROP_SUBSYSTEM:
2522
0
    fu_udev_device_set_subsystem(self, g_value_get_string(value));
2523
0
    break;
2524
0
  case PROP_BIND_ID:
2525
0
    fu_udev_device_set_bind_id(self, g_value_get_string(value));
2526
0
    break;
2527
0
  case PROP_DRIVER:
2528
0
    fu_udev_device_set_driver(self, g_value_get_string(value));
2529
0
    break;
2530
0
  case PROP_DEVICE_FILE:
2531
0
    fu_udev_device_set_device_file(self, g_value_get_string(value));
2532
0
    break;
2533
0
  case PROP_DEVTYPE:
2534
0
    fu_udev_device_set_devtype(self, g_value_get_string(value));
2535
0
    break;
2536
0
  default:
2537
0
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
2538
0
    break;
2539
0
  }
2540
0
}
2541
2542
static void
2543
fu_udev_device_finalize(GObject *object)
2544
800
{
2545
800
  FuUdevDevice *self = FU_UDEV_DEVICE(object);
2546
800
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
2547
2548
800
  g_hash_table_unref(priv->properties);
2549
800
  g_free(priv->subsystem);
2550
800
  g_free(priv->devtype);
2551
800
  g_free(priv->bind_id);
2552
800
  g_free(priv->driver);
2553
800
  g_free(priv->device_file);
2554
800
  if (priv->io_channel != NULL)
2555
0
    g_object_unref(priv->io_channel);
2556
2557
800
  G_OBJECT_CLASS(fu_udev_device_parent_class)->finalize(object);
2558
800
}
2559
2560
static void
2561
fu_udev_device_vid_notify_cb(FuDevice *device, GParamSpec *pspec, gpointer user_data)
2562
598
{
2563
598
  fu_device_add_instance_u16(device, "VEN", fu_device_get_vid(device));
2564
598
}
2565
2566
static void
2567
fu_udev_device_pid_notify_cb(FuDevice *device, GParamSpec *pspec, gpointer user_data)
2568
622
{
2569
622
  fu_device_add_instance_u16(device, "DEV", fu_device_get_pid(device));
2570
622
}
2571
2572
static void
2573
fu_udev_device_init(FuUdevDevice *self)
2574
800
{
2575
800
  FuUdevDevicePrivate *priv = GET_PRIVATE(self);
2576
800
  priv->properties = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
2577
800
  fu_device_set_acquiesce_delay(FU_DEVICE(self), 2500);
2578
800
  fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_CAN_EMULATION_TAG);
2579
800
  g_signal_connect(FU_DEVICE(self),
2580
800
       "notify::vid",
2581
800
       G_CALLBACK(fu_udev_device_vid_notify_cb),
2582
800
       NULL);
2583
800
  g_signal_connect(FU_DEVICE(self),
2584
800
       "notify::pid",
2585
800
       G_CALLBACK(fu_udev_device_pid_notify_cb),
2586
800
       NULL);
2587
800
}
2588
2589
static void
2590
fu_udev_device_class_init(FuUdevDeviceClass *klass)
2591
2
{
2592
2
  FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
2593
2
  GObjectClass *object_class = G_OBJECT_CLASS(klass);
2594
2
  GParamSpec *pspec;
2595
2596
2
  object_class->finalize = fu_udev_device_finalize;
2597
2
  object_class->get_property = fu_udev_device_get_property;
2598
2
  object_class->set_property = fu_udev_device_set_property;
2599
2
  device_class->probe = fu_udev_device_probe;
2600
2
  device_class->rescan = fu_udev_device_rescan;
2601
2
  device_class->incorporate = fu_udev_device_incorporate;
2602
2
  device_class->invalidate = fu_udev_device_invalidate;
2603
2
  device_class->open = fu_udev_device_open;
2604
2
  device_class->close = fu_udev_device_close;
2605
2
  device_class->to_string = fu_udev_device_to_string;
2606
2
  device_class->bind_driver = fu_udev_device_bind_driver;
2607
2
  device_class->unbind_driver = fu_udev_device_unbind_driver;
2608
2
  device_class->probe_complete = fu_udev_device_probe_complete;
2609
2
  device_class->build = fu_udev_device_build;
2610
2
  device_class->export = fu_udev_device_export;
2611
2
  quarks[QUARK_SYSFS_USE_PHYSICAL_ID] =
2612
2
      fu_device_register_private_flag_safe(device_class,
2613
2
             FU_UDEV_DEVICE_FLAG_SYSFS_USE_PHYSICAL_ID);
2614
2615
  /**
2616
   * FuUdevDevice::changed:
2617
   * @self: the #FuUdevDevice instance that emitted the signal
2618
   *
2619
   * The ::changed signal is emitted when the low-level GUdevDevice has changed.
2620
   **/
2621
2
  signals[SIGNAL_CHANGED] = g_signal_new("changed",
2622
2
                 G_TYPE_FROM_CLASS(object_class),
2623
2
                 G_SIGNAL_RUN_LAST,
2624
2
                 0,
2625
2
                 NULL,
2626
2
                 NULL,
2627
2
                 g_cclosure_marshal_VOID__VOID,
2628
2
                 G_TYPE_NONE,
2629
2
                 0);
2630
2631
  /**
2632
   * FuUdevDevice:subsystem:
2633
   *
2634
   * The device subsystem.
2635
   */
2636
2
  pspec = g_param_spec_string("subsystem",
2637
2
            NULL,
2638
2
            NULL,
2639
2
            NULL,
2640
2
            G_PARAM_READWRITE | G_PARAM_STATIC_NAME);
2641
2
  g_object_class_install_property(object_class, PROP_SUBSYSTEM, pspec);
2642
2643
  /**
2644
   * FuUdevDevice:bind-id:
2645
   *
2646
   * The bind ID to use when binding a new driver.
2647
   */
2648
2
  pspec = g_param_spec_string("bind-id",
2649
2
            NULL,
2650
2
            NULL,
2651
2
            NULL,
2652
2
            G_PARAM_READWRITE | G_PARAM_STATIC_NAME);
2653
2
  g_object_class_install_property(object_class, PROP_BIND_ID, pspec);
2654
2655
  /**
2656
   * FuUdevDevice:driver:
2657
   *
2658
   * The driver being used for the device.
2659
   */
2660
2
  pspec = g_param_spec_string("driver",
2661
2
            NULL,
2662
2
            NULL,
2663
2
            NULL,
2664
2
            G_PARAM_READWRITE | G_PARAM_STATIC_NAME);
2665
2
  g_object_class_install_property(object_class, PROP_DRIVER, pspec);
2666
2667
  /**
2668
   * FuUdevDevice:device-file:
2669
   *
2670
   * The low level file to use for device access.
2671
   */
2672
2
  pspec = g_param_spec_string("device-file",
2673
2
            NULL,
2674
2
            NULL,
2675
2
            NULL,
2676
2
            G_PARAM_READWRITE | G_PARAM_STATIC_NAME);
2677
2
  g_object_class_install_property(object_class, PROP_DEVICE_FILE, pspec);
2678
2679
  /**
2680
   * FuUdevDevice:devtype:
2681
   *
2682
   * The device type.
2683
   */
2684
2
  pspec = g_param_spec_string("devtype",
2685
2
            NULL,
2686
2
            NULL,
2687
2
            NULL,
2688
2
            G_PARAM_READWRITE | G_PARAM_STATIC_NAME);
2689
2
  g_object_class_install_property(object_class, PROP_DEVTYPE, pspec);
2690
2
}
2691
2692
/**
2693
 * fu_udev_device_new:
2694
 * @ctx: (nullable): a #FuContext
2695
 * @sysfs_path: a sysfs path
2696
 *
2697
 * Creates a new #FuUdevDevice.
2698
 *
2699
 * Returns: (transfer full): a #FuUdevDevice
2700
 **/
2701
FuUdevDevice *
2702
fu_udev_device_new(FuContext *ctx, const gchar *sysfs_path)
2703
0
{
2704
0
  return g_object_new(FU_TYPE_UDEV_DEVICE, "context", ctx, "backend-id", sysfs_path, NULL);
2705
0
}