File: fu-block-device.c

package info (click to toggle)
fwupd 2.0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,504 kB
  • sloc: ansic: 277,388; python: 11,485; xml: 9,493; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (346 lines) | stat: -rw-r--r-- 9,163 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Copyright 2024 Richard Hughes <richard@hughsie.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#define G_LOG_DOMAIN "FuBlockDevice"

#include "config.h"

#ifdef HAVE_SCSI_SG_H
#include <scsi/sg.h>
#include <stddef.h>
#endif

#include "fu-block-device.h"
#include "fu-dump.h"

/**
 * FuBlockDevice
 *
 * See also: #FuUdevDevice
 */

G_DEFINE_TYPE(FuBlockDevice, fu_block_device, FU_TYPE_UDEV_DEVICE)

#define FU_BLOCK_DEVICE_SG_IO_SENSE_BUFFER_LEN 32    /* bytes */
#define FU_BLOCK_DEVICE_SG_IO_TIMEOUT	       20000 /* ms */

static gboolean
fu_block_device_probe(FuDevice *device, GError **error)
{
	g_autoptr(FuDevice) usb_device = NULL;

	/* block devices are weird in that the vendor and model are generic */
	usb_device = fu_device_get_backend_parent_with_subsystem(device, "usb:usb_device", NULL);
	if (usb_device != NULL) {
		g_autofree gchar *devpath = fu_udev_device_get_devpath(FU_UDEV_DEVICE(usb_device));

		/* copy the VID and PID, and reconstruct compatible IDs */
		if (!fu_device_probe(usb_device, error))
			return FALSE;
		fu_device_add_instance_u16(device, "VEN", fu_device_get_vid(usb_device));
		fu_device_add_instance_u16(device, "DEV", fu_device_get_pid(usb_device));
		if (!fu_device_build_instance_id_full(device,
						      FU_DEVICE_INSTANCE_FLAG_QUIRKS,
						      error,
						      "BLOCK",
						      "VEN",
						      NULL))
			return FALSE;
		if (!fu_device_build_instance_id(device, error, "BLOCK", "VEN", "DEV", NULL))
			return FALSE;
		fu_device_incorporate(device,
				      usb_device,
				      FU_DEVICE_INCORPORATE_FLAG_VENDOR_IDS |
					  FU_DEVICE_INCORPORATE_FLAG_VID |
					  FU_DEVICE_INCORPORATE_FLAG_PID);

		/* USB devpath as physical ID */
		if (devpath != NULL) {
			g_autofree gchar *physical_id = g_strdup_printf("DEVPATH=%s", devpath);
			fu_device_set_physical_id(device, physical_id);
		}
	}

	/* success */
	return TRUE;
}

#ifdef HAVE_SCSI_SG_H
static gboolean
fu_block_device_ioctl_buf_cb(FuIoctl *self, gpointer ptr, guint8 *buf, gsize bufsz, GError **error)
{
	struct sg_io_hdr *io_hdr = (struct sg_io_hdr *)ptr;
	io_hdr->dxferp = buf;
	io_hdr->dxfer_len = bufsz;
	return TRUE;
}

static gboolean
fu_block_device_ioctl_cdb_cb(FuIoctl *self, gpointer ptr, guint8 *buf, gsize bufsz, GError **error)
{
	struct sg_io_hdr *io_hdr = (struct sg_io_hdr *)ptr;
	io_hdr->cmdp = buf;
	io_hdr->cmd_len = bufsz;
	return TRUE;
}

static gboolean
fu_block_device_ioctl_sense_cb(FuIoctl *self,
			       gpointer ptr,
			       guint8 *buf,
			       gsize bufsz,
			       GError **error)
{
	struct sg_io_hdr *io_hdr = (struct sg_io_hdr *)ptr;
	io_hdr->sbp = buf;
	io_hdr->mx_sb_len = bufsz;
	return TRUE;
}
#endif

/**
 * fu_block_device_sg_io_cmd_none:
 * @self: a #FuBlockDevice
 * @cdb: a cdb command
 * @cdbsz: sizeof @cdb
 * @error: (nullable): optional return location for an error
 *
 * Performs a SCSI IO command with no parameters.
 *
 * Returns: %TRUE on success
 *
 * Since: 2.0.0
 **/
gboolean
fu_block_device_sg_io_cmd_none(FuBlockDevice *self, const guint8 *cdb, guint8 cdbsz, GError **error)
{
#ifdef HAVE_SCSI_SG_H
	guint8 sense_buffer[FU_BLOCK_DEVICE_SG_IO_SENSE_BUFFER_LEN] = {0};
	struct sg_io_hdr io_hdr = {
	    .interface_id = 'S',
	    .dxfer_direction = SG_DXFER_NONE,
	    .timeout = FU_BLOCK_DEVICE_SG_IO_TIMEOUT,
	    .flags = SG_FLAG_DIRECT_IO,
	};
	gint rc = 0;
	g_autoptr(FuIoctl) ioctl = fu_udev_device_ioctl_new(FU_UDEV_DEVICE(self));

	fu_dump_raw(G_LOG_DOMAIN, "cmd", cdb, cdbsz);

	/* include these when generating the emulation event */
	fu_ioctl_add_key_as_u16(ioctl, "Request", SG_IO);
	fu_ioctl_add_key_as_u8(ioctl, "DxferDirection", io_hdr.dxfer_direction);
	fu_ioctl_add_const_buffer(ioctl, "Cdb", cdb, cdbsz, fu_block_device_ioctl_cdb_cb);
	fu_ioctl_add_mutable_buffer(ioctl,
				    "Sense",
				    sense_buffer,
				    sizeof(sense_buffer),
				    fu_block_device_ioctl_sense_cb);
	if (!fu_ioctl_execute(ioctl,
			      SG_IO,
			      (guint8 *)&io_hdr,
			      sizeof(io_hdr),
			      &rc,
			      5 * FU_BLOCK_DEVICE_SG_IO_TIMEOUT,
			      FU_IOCTL_FLAG_RETRY,
			      error))
		return FALSE;
	if (io_hdr.status) {
		g_set_error(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_INTERNAL,
			    "command fail with status %x, senseKey 0x%02x, asc 0x%02x, ascq 0x%02x",
			    io_hdr.status,
			    sense_buffer[2],
			    sense_buffer[12],
			    sense_buffer[13]);
		return FALSE;
	}

	/* success */
	return TRUE;
#else
	g_set_error_literal(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_NOT_SUPPORTED,
			    "unsupported as scsi/sg.h not found");
	return FALSE;
#endif
}

/**
 * fu_block_device_sg_io_cmd_read:
 * @self: a #FuBlockDevice
 * @cdb: a cdb command
 * @cdbsz: sizeof @cdb
 * @buf: buffer to read into
 * @bufsz: sizeof @buf
 * @error: (nullable): optional return location for an error
 *
 * Performs a SCSI IO read command.
 *
 * Returns: %TRUE on success
 *
 * Since: 2.0.0
 **/
gboolean
fu_block_device_sg_io_cmd_read(FuBlockDevice *self,
			       const guint8 *cdb,
			       gsize cdbsz,
			       guint8 *buf,
			       gsize bufsz,
			       GError **error)
{
#ifdef HAVE_SCSI_SG_H
	guint8 sense_buffer[FU_BLOCK_DEVICE_SG_IO_SENSE_BUFFER_LEN] = {0};
	struct sg_io_hdr io_hdr = {
	    .interface_id = 'S',
	    .dxfer_direction = SG_DXFER_FROM_DEV,
	    .timeout = FU_BLOCK_DEVICE_SG_IO_TIMEOUT,
	    .flags = SG_FLAG_DIRECT_IO,
	};
	gint rc = 0;
	g_autoptr(FuIoctl) ioctl = fu_udev_device_ioctl_new(FU_UDEV_DEVICE(self));

	fu_dump_raw(G_LOG_DOMAIN, "cmd", cdb, cdbsz);

	/* include these when generating the emulation event */
	fu_ioctl_add_key_as_u16(ioctl, "Request", SG_IO);
	fu_ioctl_add_key_as_u8(ioctl, "DxferDirection", io_hdr.dxfer_direction);
	fu_ioctl_add_mutable_buffer(ioctl, NULL, buf, bufsz, fu_block_device_ioctl_buf_cb);
	fu_ioctl_add_const_buffer(ioctl, "Cdb", cdb, cdbsz, fu_block_device_ioctl_cdb_cb);
	fu_ioctl_add_mutable_buffer(ioctl,
				    "Sense",
				    sense_buffer,
				    sizeof(sense_buffer),
				    fu_block_device_ioctl_sense_cb);
	if (!fu_ioctl_execute(ioctl,
			      SG_IO,
			      (guint8 *)&io_hdr,
			      sizeof(io_hdr),
			      &rc,
			      5 * FU_BLOCK_DEVICE_SG_IO_TIMEOUT,
			      FU_IOCTL_FLAG_RETRY,
			      error))
		return FALSE;
	if (io_hdr.status) {
		g_set_error(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_INTERNAL,
			    "command fail with status %x, senseKey 0x%02x, asc 0x%02x, ascq 0x%02x",
			    io_hdr.status,
			    sense_buffer[2],
			    sense_buffer[12],
			    sense_buffer[13]);
		return FALSE;
	}

	if (bufsz > 0)
		fu_dump_raw(G_LOG_DOMAIN, "cmd data", buf, bufsz);

	/* success */
	return TRUE;
#else
	g_set_error_literal(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_NOT_SUPPORTED,
			    "unsupported as scsi/sg.h not found");
	return FALSE;
#endif
}

/**
 * fu_block_device_sg_io_cmd_write:
 * @self: a #FuBlockDevice
 * @cdb: a cdb command
 * @cdbsz: sizeof @cdb
 * @buf: buffer to read from
 * @bufsz: sizeof @buf
 * @error: (nullable): optional return location for an error
 *
 * Performs a SCSI IO write command.
 *
 * Returns: %TRUE on success
 *
 * Since: 2.0.0
 **/
gboolean
fu_block_device_sg_io_cmd_write(FuBlockDevice *self,
				const guint8 *cdb,
				gsize cdbsz,
				const guint8 *buf,
				gsize bufsz,
				GError **error)
{
#ifdef HAVE_SCSI_SG_H
	guint8 sense_buffer[FU_BLOCK_DEVICE_SG_IO_SENSE_BUFFER_LEN] = {0};
	struct sg_io_hdr io_hdr = {
	    .interface_id = 'S',
	    .dxfer_direction = SG_DXFER_TO_DEV,
	    .timeout = FU_BLOCK_DEVICE_SG_IO_TIMEOUT,
	    .flags = SG_FLAG_DIRECT_IO,
	};
	gint rc = 0;
	g_autoptr(FuIoctl) ioctl = fu_udev_device_ioctl_new(FU_UDEV_DEVICE(self));

	fu_dump_raw(G_LOG_DOMAIN, "cmd", cdb, cdbsz);

	/* include these when generating the emulation event */
	fu_ioctl_add_key_as_u16(ioctl, "Request", SG_IO);
	fu_ioctl_add_key_as_u8(ioctl, "DxferDirection", io_hdr.dxfer_direction);
	fu_ioctl_add_const_buffer(ioctl, NULL, buf, bufsz, fu_block_device_ioctl_buf_cb);
	fu_ioctl_add_const_buffer(ioctl, "Cdb", cdb, cdbsz, fu_block_device_ioctl_cdb_cb);
	fu_ioctl_add_mutable_buffer(ioctl,
				    "Sense",
				    sense_buffer,
				    sizeof(sense_buffer),
				    fu_block_device_ioctl_sense_cb);
	if (!fu_ioctl_execute(ioctl,
			      SG_IO,
			      (guint8 *)&io_hdr,
			      sizeof(io_hdr),
			      &rc,
			      5 * FU_BLOCK_DEVICE_SG_IO_TIMEOUT,
			      FU_IOCTL_FLAG_RETRY,
			      error))
		return FALSE;
	if (io_hdr.status) {
		g_set_error(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_INTERNAL,
			    "command fail with status %x, senseKey 0x%02x, asc 0x%02x, ascq 0x%02x",
			    io_hdr.status,
			    sense_buffer[2],
			    sense_buffer[12],
			    sense_buffer[13]);
		return FALSE;
	}

	if (bufsz > 0)
		fu_dump_raw(G_LOG_DOMAIN, "cmd data", buf, bufsz);

	/* success */
	return TRUE;
#else
	g_set_error_literal(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_NOT_SUPPORTED,
			    "unsupported as scsi/sg.h not found");
	return FALSE;
#endif
}

static void
fu_block_device_init(FuBlockDevice *self)
{
}

static void
fu_block_device_class_init(FuBlockDeviceClass *klass)
{
	FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
	device_class->probe = fu_block_device_probe;
}