File: fu-fastboot-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 (744 lines) | stat: -rw-r--r-- 21,006 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
 * Copyright 2018 Richard Hughes <richard@hughsie.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <string.h>

#include "fu-fastboot-device.h"

#define FASTBOOT_REMOVE_DELAY_RE_ENUMERATE 60000 /* ms */
#define FASTBOOT_TRANSACTION_TIMEOUT	   1000	 /* ms */
#define FASTBOOT_TRANSACTION_RETRY_MAX	   600
#define FASTBOOT_EP_IN			   0x81
#define FASTBOOT_EP_OUT			   0x01
#define FASTBOOT_CMD_BUFSZ		   64 /* bytes */

struct _FuFastbootDevice {
	FuUsbDevice parent_instance;
	guint blocksz;
	guint operation_delay;
};

G_DEFINE_TYPE(FuFastbootDevice, fu_fastboot_device, FU_TYPE_USB_DEVICE)

static void
fu_fastboot_device_to_string(FuDevice *device, guint idt, GString *str)
{
	FuFastbootDevice *self = FU_FASTBOOT_DEVICE(device);
	fwupd_codec_string_append_hex(str, idt, "BlockSize", self->blocksz);
}

static gboolean
fu_fastboot_device_probe(FuDevice *device, GError **error)
{
	FuFastbootDevice *self = FU_FASTBOOT_DEVICE(device);
	g_autoptr(FuUsbInterface) intf = NULL;

	/* find the correct fastboot interface */
	intf = fu_usb_device_get_interface(FU_USB_DEVICE(self), 0xff, 0x42, 0x03, error);
	if (intf == NULL)
		return FALSE;
	fu_usb_device_add_interface(FU_USB_DEVICE(self), fu_usb_interface_get_number(intf));
	return TRUE;
}

static gboolean
fu_fastboot_device_write(FuFastbootDevice *self, const guint8 *buf, gsize buflen, GError **error)
{
	gboolean ret;
	gsize actual_len = 0;
	g_autofree guint8 *buf2 = NULL;

	/* make mutable */
	fu_dump_raw(G_LOG_DOMAIN, "writing", buf, buflen);
	buf2 = fu_memdup_safe(buf, buflen, error);
	if (buf2 == NULL)
		return FALSE;
	ret = fu_usb_device_bulk_transfer(FU_USB_DEVICE(self),
					  FASTBOOT_EP_OUT,
					  buf2,
					  buflen,
					  &actual_len,
					  FASTBOOT_TRANSACTION_TIMEOUT,
					  NULL,
					  error);

	/* give device some time to handle action */
	fu_device_sleep(FU_DEVICE(self), self->operation_delay);

	if (!ret) {
		g_prefix_error_literal(error, "failed to do bulk transfer: ");
		return FALSE;
	}
	if (actual_len != buflen) {
		g_set_error(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_INVALID_DATA,
			    "only wrote %" G_GSIZE_FORMAT "bytes",
			    actual_len);
		return FALSE;
	}
	return TRUE;
}

static gboolean
fu_fastboot_device_writestr(FuFastbootDevice *self, const gchar *str, GError **error)
{
	gsize buflen = strlen(str);
	if (buflen > FASTBOOT_CMD_BUFSZ - 4) {
		g_set_error(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_INVALID_DATA,
			    "fastboot limits writes to %i bytes",
			    FASTBOOT_CMD_BUFSZ - 4);
		return FALSE;
	}
	return fu_fastboot_device_write(self, (const guint8 *)str, buflen, error);
}

typedef enum {
	FU_FASTBOOT_DEVICE_READ_FLAG_NONE,
	FU_FASTBOOT_DEVICE_READ_FLAG_STATUS_POLL,
} FuFastbootDeviceReadFlags;

static gboolean
fu_fastboot_device_read(FuFastbootDevice *self,
			gchar **str,
			FuProgress *progress,
			FuFastbootDeviceReadFlags flags,
			GError **error)
{
	guint retries = 1;

	/* these commands may return INFO or take some time to complete */
	if (flags & FU_FASTBOOT_DEVICE_READ_FLAG_STATUS_POLL)
		retries = FASTBOOT_TRANSACTION_RETRY_MAX;

	for (guint i = 0; i < retries; i++) {
		gboolean ret;
		gsize actual_len = 0;
		guint8 buf[FASTBOOT_CMD_BUFSZ] = {0x00};
		g_autofree gchar *tmp = NULL;
		g_autoptr(GError) error_local = NULL;

		ret = fu_usb_device_bulk_transfer(FU_USB_DEVICE(self),
						  FASTBOOT_EP_IN,
						  buf,
						  sizeof(buf),
						  &actual_len,
						  FASTBOOT_TRANSACTION_TIMEOUT,
						  NULL,
						  &error_local);
		/* give device some time to handle action */
		fu_device_sleep(FU_DEVICE(self), self->operation_delay);

		if (!ret) {
			if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_TIMED_OUT)) {
				g_debug("ignoring %s", error_local->message);
				continue;
			}
			g_propagate_prefixed_error(error,
						   g_steal_pointer(&error_local),
						   "failed to do bulk transfer: ");
			return FALSE;
		}
		fu_dump_raw(G_LOG_DOMAIN, "read", buf, actual_len);
		if (actual_len < 4) {
			g_set_error(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INVALID_DATA,
				    "only read %" G_GSIZE_FORMAT "bytes",
				    actual_len);
			return FALSE;
		}

		/* info */
		tmp = fu_memstrsafe(buf, sizeof(buf), 4, sizeof(buf) - 4, NULL);
		if (memcmp(buf, "INFO", 4) == 0) {
			if (g_strcmp0(tmp, "erasing flash") == 0)
				fu_progress_set_status(progress, FWUPD_STATUS_DEVICE_ERASE);
			else if (g_strcmp0(tmp, "writing flash") == 0)
				fu_progress_set_status(progress, FWUPD_STATUS_DEVICE_WRITE);
			else
				g_debug("INFO returned unknown: %s", tmp);
			continue;
		}

		/* success */
		if (memcmp(buf, "OKAY", 4) == 0 || memcmp(buf, "DATA", 4) == 0) {
			if (str != NULL)
				*str = g_steal_pointer(&tmp);
			return TRUE;
		}

		/* failure */
		if (memcmp(buf, "FAIL", 4) == 0) {
			g_set_error(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_READ,
				    "failed to read response: %s",
				    tmp);
			return FALSE;
		}

		/* unknown failure */
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INTERNAL,
				    "failed to read response");
		return FALSE;
	}

	/* we timed out a *lot* */
	g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_TIMED_OUT, "no response to read");
	return FALSE;
}

static gboolean
fu_fastboot_device_getvar(FuFastbootDevice *self, const gchar *key, gchar **str, GError **error)
{
	g_autofree gchar *tmp = g_strdup_printf("getvar:%s", key);
	g_autoptr(FuProgress) progress = fu_progress_new(G_STRLOC);
	g_autoptr(GError) error_local = NULL;

	if (!fu_fastboot_device_writestr(self, tmp, error))
		return FALSE;
	if (!fu_fastboot_device_read(self,
				     str,
				     progress,
				     FU_FASTBOOT_DEVICE_READ_FLAG_NONE,
				     &error_local)) {
		if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_READ)) {
			g_debug("ignoring: %s", error_local->message);
		} else {
			g_propagate_prefixed_error(error,
						   g_steal_pointer(&error_local),
						   "failed to getvar %s: ",
						   key);
			return FALSE;
		}
	}
	return TRUE;
}

static gboolean
fu_fastboot_device_cmd(FuFastbootDevice *self,
		       const gchar *cmd,
		       FuProgress *progress,
		       FuFastbootDeviceReadFlags flags,
		       GError **error)
{
	if (!fu_fastboot_device_writestr(self, cmd, error))
		return FALSE;
	if (!fu_fastboot_device_read(self, NULL, progress, flags, error))
		return FALSE;
	return TRUE;
}

static gboolean
fu_fastboot_device_flash(FuFastbootDevice *self,
			 const gchar *partition,
			 FuProgress *progress,
			 GError **error)
{
	g_autofree gchar *tmp = g_strdup_printf("flash:%s", partition);
	return fu_fastboot_device_cmd(self,
				      tmp,
				      progress,
				      FU_FASTBOOT_DEVICE_READ_FLAG_STATUS_POLL,
				      error);
}

static gboolean
fu_fastboot_device_download(FuFastbootDevice *self,
			    GBytes *fw,
			    FuProgress *progress,
			    GError **error)
{
	gsize sz = g_bytes_get_size(fw);
	g_autofree gchar *tmp = g_strdup_printf("download:%08x", (guint)sz);
	g_autoptr(FuChunkArray) chunks = NULL;

	/* tell the client the size of data to expect */
	if (!fu_fastboot_device_cmd(self,
				    tmp,
				    progress,
				    FU_FASTBOOT_DEVICE_READ_FLAG_STATUS_POLL,
				    error))
		return FALSE;

	/* send the data in chunks */
	fu_progress_set_status(progress, FWUPD_STATUS_DEVICE_WRITE);
	chunks = fu_chunk_array_new_from_bytes(fw,
					       FU_CHUNK_ADDR_OFFSET_NONE,
					       FU_CHUNK_PAGESZ_NONE,
					       self->blocksz);
	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_set_steps(progress, fu_chunk_array_length(chunks));
	for (guint i = 0; i < fu_chunk_array_length(chunks); i++) {
		g_autoptr(FuChunk) chk = NULL;

		/* prepare chunk */
		chk = fu_chunk_array_index(chunks, i, error);
		if (chk == NULL)
			return FALSE;
		if (!fu_fastboot_device_write(self,
					      fu_chunk_get_data(chk),
					      fu_chunk_get_data_sz(chk),
					      error))
			return FALSE;
		fu_progress_step_done(progress);
	}
	if (!fu_fastboot_device_read(self,
				     NULL,
				     progress,
				     FU_FASTBOOT_DEVICE_READ_FLAG_STATUS_POLL,
				     error))
		return FALSE;
	return TRUE;
}

static gboolean
fu_fastboot_device_setup(FuDevice *device, GError **error)
{
	FuFastbootDevice *self = FU_FASTBOOT_DEVICE(device);
	g_autofree gchar *product = NULL;
	g_autofree gchar *serialno = NULL;
	g_autofree gchar *secure = NULL;
	g_autofree gchar *version_bootloader = NULL;

	/* FuUsbDevice->setup */
	if (!FU_DEVICE_CLASS(fu_fastboot_device_parent_class)->setup(device, error))
		return FALSE;

	/* product */
	if (!fu_fastboot_device_getvar(self, "product", &product, error))
		return FALSE;
	if (product != NULL && product[0] != '\0') {
		g_autofree gchar *tmp = g_strdup_printf("Fastboot %s", product);
		fu_device_set_name(device, tmp);
	}

	/* bootloader version */
	if (!fu_fastboot_device_getvar(self, "version-bootloader", &version_bootloader, error))
		return FALSE;
	if (version_bootloader != NULL && version_bootloader[0] != '\0')
		fu_device_set_version_bootloader(device, version_bootloader);

	/* serialno */
	if (!fu_fastboot_device_getvar(self, "serialno", &serialno, error))
		return FALSE;
	if (serialno != NULL && serialno[0] != '\0')
		fu_device_set_serial(device, serialno);

	/* secure */
	if (!fu_fastboot_device_getvar(self, "secure", &secure, error))
		return FALSE;
	if (secure != NULL && secure[0] != '\0') {
		fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_SIGNED_PAYLOAD);
	} else {
		fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_UNSIGNED_PAYLOAD);
	}

	/* success */
	return TRUE;
}

static gboolean
fu_fastboot_device_write_qfil_part(FuFastbootDevice *self,
				   FuFirmware *firmware,
				   XbNode *part,
				   FuProgress *progress,
				   GError **error)
{
	GBytes *data;
	const gchar *fn;
	const gchar *partition;

	/* not all partitions have images */
	fn = xb_node_query_text(part, "img_name", NULL);
	if (fn == NULL)
		return TRUE;

	/* find filename */
	data = fu_firmware_get_image_by_id_bytes(firmware, fn, error);
	if (data == NULL)
		return FALSE;

	/* get the partition name */
	partition = xb_node_query_text(part, "name", error);
	if (partition == NULL)
		return FALSE;
	if (g_str_has_prefix(partition, "0:"))
		partition += 2;

	/* flash the partition */
	if (!fu_fastboot_device_download(self, data, progress, error))
		return FALSE;
	return fu_fastboot_device_flash(self, partition, progress, error);
}

static gboolean
fu_fastboot_device_write_motorola_part(FuFastbootDevice *self,
				       FuFirmware *firmware,
				       XbNode *part,
				       FuProgress *progress,
				       GError **error)
{
	const gchar *op = xb_node_get_attr(part, "operation");

	/* oem */
	if (g_strcmp0(op, "oem") == 0) {
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_NOT_SUPPORTED,
				    "OEM commands are not supported");
		return FALSE;
	}

	/* getvar */
	if (g_strcmp0(op, "getvar") == 0) {
		const gchar *var = xb_node_get_attr(part, "var");
		g_autofree gchar *tmp = NULL;

		/* check required args */
		if (var == NULL) {
			tmp = xb_node_export(part, XB_NODE_EXPORT_FLAG_NONE, NULL);
			g_set_error(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INVALID_DATA,
				    "required var for part: %s",
				    tmp);
			return FALSE;
		}

		/* just has to be non-empty */
		if (!fu_fastboot_device_getvar(self, var, &tmp, error))
			return FALSE;
		if (tmp == NULL || tmp[0] == '\0') {
			g_set_error(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INVALID_DATA,
				    "failed to getvar %s",
				    var);
			return FALSE;
		}
		return TRUE;
	}

	/* erase */
	if (g_strcmp0(op, "erase") == 0) {
		const gchar *partition = xb_node_get_attr(part, "partition");
		g_autofree gchar *cmd = g_strdup_printf("erase:%s", partition);

		/* check required args */
		if (partition == NULL) {
			g_autofree gchar *tmp = NULL;
			tmp = xb_node_export(part, XB_NODE_EXPORT_FLAG_NONE, NULL);
			g_set_error(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INVALID_DATA,
				    "required partition for part: %s",
				    tmp);
			return FALSE;
		}

		/* erase the partition */
		return fu_fastboot_device_cmd(self,
					      cmd,
					      progress,
					      FU_FASTBOOT_DEVICE_READ_FLAG_NONE,
					      error);
	}

	/* flash */
	if (g_strcmp0(op, "flash") == 0) {
		GBytes *data;
		const gchar *filename = xb_node_get_attr(part, "filename");
		const gchar *partition = xb_node_get_attr(part, "partition");
		struct {
			GChecksumType kind;
			const gchar *str;
		} csum_kinds[] = {{G_CHECKSUM_MD5, "MD5"},
				  {G_CHECKSUM_SHA1, "SHA1"},
				  {G_CHECKSUM_SHA256, "SHA256"},
				  {0, NULL}};

		/* check required args */
		if (partition == NULL || filename == NULL) {
			g_autofree gchar *tmp = NULL;
			tmp = xb_node_export(part, XB_NODE_EXPORT_FLAG_NONE, NULL);
			g_set_error(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INVALID_DATA,
				    "required partition and filename: %s",
				    tmp);
			return FALSE;
		}

		/* find filename */
		data = fu_firmware_get_image_by_id_bytes(firmware, filename, error);
		if (data == NULL)
			return FALSE;

		/* checksum is optional */
		for (guint i = 0; csum_kinds[i].str != NULL; i++) {
			const gchar *csum;
			g_autofree gchar *csum_actual = NULL;

			/* not provided */
			csum = xb_node_get_attr(part, csum_kinds[i].str);
			if (csum == NULL)
				continue;

			/* check is valid */
			csum_actual = g_compute_checksum_for_bytes(csum_kinds[i].kind, data);
			if (g_strcmp0(csum, csum_actual) != 0) {
				g_set_error(error,
					    FWUPD_ERROR,
					    FWUPD_ERROR_INVALID_DATA,
					    "%s invalid, expected %s, got %s",
					    filename,
					    csum,
					    csum_actual);
				return FALSE;
			}
		}

		/* flash the partition */
		if (!fu_fastboot_device_download(self, data, progress, error))
			return FALSE;
		return fu_fastboot_device_flash(self, partition, progress, error);
	}

	/* dumb operation that doesn't expect a response */
	if (g_strcmp0(op, "boot") == 0 || g_strcmp0(op, "continue") == 0 ||
	    g_strcmp0(op, "reboot") == 0 || g_strcmp0(op, "reboot-bootloader") == 0 ||
	    g_strcmp0(op, "powerdown") == 0) {
		return fu_fastboot_device_cmd(self,
					      op,
					      progress,
					      FU_FASTBOOT_DEVICE_READ_FLAG_NONE,
					      error);
	}

	/* unknown */
	g_set_error(error, FWUPD_ERROR, FWUPD_ERROR_INVALID_DATA, "unknown operation %s", op);
	return FALSE;
}

static gboolean
fu_fastboot_device_write_motorola(FuFastbootDevice *self,
				  FuFirmware *firmware,
				  FuProgress *progress,
				  GError **error)
{
	GBytes *data;
	g_autoptr(GPtrArray) parts = NULL;
	g_autoptr(XbBuilder) builder = xb_builder_new();
	g_autoptr(XbBuilderSource) source = xb_builder_source_new();
	g_autoptr(XbSilo) silo = NULL;

	/* load the manifest of operations */
	data = fu_firmware_get_image_by_id_bytes(firmware, "flashfile.xml", error);
	if (data == NULL)
		return FALSE;
	if (!xb_builder_source_load_bytes(source, data, XB_BUILDER_SOURCE_FLAG_NONE, error)) {
		fwupd_error_convert(error);
		return FALSE;
	}
	xb_builder_import_source(builder, source);
	silo = xb_builder_compile(builder, XB_BUILDER_COMPILE_FLAG_NONE, NULL, error);
	if (silo == NULL) {
		fwupd_error_convert(error);
		return FALSE;
	}

	/* get all the operation parts */
	parts = xb_silo_query(silo, "parts/part", 0, error);
	if (parts == NULL) {
		fwupd_error_convert(error);
		return FALSE;
	}
	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_set_steps(progress, parts->len);
	for (guint i = 0; i < parts->len; i++) {
		XbNode *part = g_ptr_array_index(parts, i);
		if (!fu_fastboot_device_write_motorola_part(self,
							    firmware,
							    part,
							    fu_progress_get_child(progress),
							    error))
			return FALSE;
		fu_progress_step_done(progress);
	}

	/* success */
	return TRUE;
}

static gboolean
fu_fastboot_device_write_qfil(FuFastbootDevice *self,
			      FuFirmware *firmware,
			      FuProgress *progress,
			      GError **error)
{
	GBytes *data;
	g_autoptr(GPtrArray) parts = NULL;
	g_autoptr(XbBuilder) builder = xb_builder_new();
	g_autoptr(XbBuilderSource) source = xb_builder_source_new();
	g_autoptr(XbSilo) silo = NULL;

	/* load the manifest of operations */
	data = fu_firmware_get_image_by_id_bytes(firmware, "partition_nand.xml", error);
	if (data == NULL)
		return FALSE;
	if (!xb_builder_source_load_bytes(source, data, XB_BUILDER_SOURCE_FLAG_NONE, error)) {
		fwupd_error_convert(error);
		return FALSE;
	}
	xb_builder_import_source(builder, source);
	silo = xb_builder_compile(builder, XB_BUILDER_COMPILE_FLAG_NONE, NULL, error);
	if (silo == NULL) {
		fwupd_error_convert(error);
		return FALSE;
	}

	/* get all the operation parts */
	parts = xb_silo_query(silo, "nandboot/partitions/partition", 0, error);
	if (parts == NULL) {
		fwupd_error_convert(error);
		return FALSE;
	}
	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_set_steps(progress, parts->len);
	for (guint i = 0; i < parts->len; i++) {
		XbNode *part = g_ptr_array_index(parts, i);
		if (!fu_fastboot_device_write_qfil_part(self,
							firmware,
							part,
							fu_progress_get_child(progress),
							error))
			return FALSE;
		fu_progress_step_done(progress);
	}

	/* success */
	return TRUE;
}

static gboolean
fu_fastboot_device_write_firmware(FuDevice *device,
				  FuFirmware *firmware,
				  FuProgress *progress,
				  FwupdInstallFlags flags,
				  GError **error)
{
	FuFastbootDevice *self = FU_FASTBOOT_DEVICE(device);
	g_autoptr(FuFirmware) manifest = NULL;

	/* load the manifest of operations */
	manifest = fu_firmware_get_image_by_id(firmware, "partition_nand.xml", NULL);
	if (manifest != NULL)
		return fu_fastboot_device_write_qfil(self, firmware, progress, error);
	manifest = fu_firmware_get_image_by_id(firmware, "flashfile.xml", NULL);
	if (manifest != NULL)
		return fu_fastboot_device_write_motorola(self, firmware, progress, error);

	/* not supported */
	g_set_error_literal(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_NOT_SUPPORTED,
			    "manifest not supported");
	return FALSE;
}

static gboolean
fu_fastboot_device_set_quirk_kv(FuDevice *device,
				const gchar *key,
				const gchar *value,
				GError **error)
{
	FuFastbootDevice *self = FU_FASTBOOT_DEVICE(device);
	guint64 tmp = 0;

	/* load from quirks */
	if (g_strcmp0(key, "FastbootBlockSize") == 0) {
		if (!fu_strtoull(value, &tmp, 0x40, 0x100000, FU_INTEGER_BASE_AUTO, error))
			return FALSE;
		self->blocksz = tmp;
		return TRUE;
	}
	if (g_strcmp0(key, "FastbootOperationDelay") == 0) {
		if (!fu_strtoull(value, &tmp, 0, G_MAXSIZE, FU_INTEGER_BASE_AUTO, error))
			return FALSE;
		self->operation_delay = tmp;
		return TRUE;
	}

	/* failed */
	g_set_error_literal(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_NOT_SUPPORTED,
			    "quirk key not supported");
	return FALSE;
}

static gboolean
fu_fastboot_device_attach(FuDevice *device, FuProgress *progress, GError **error)
{
	FuFastbootDevice *self = FU_FASTBOOT_DEVICE(device);
	if (!fu_fastboot_device_cmd(self,
				    "reboot",
				    progress,
				    FU_FASTBOOT_DEVICE_READ_FLAG_NONE,
				    error))
		return FALSE;
	fu_device_add_flag(device, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG);
	return TRUE;
}

static void
fu_fastboot_device_set_progress(FuDevice *device, FuProgress *progress)
{
	fu_progress_set_id(progress, G_STRLOC);
	fu_progress_add_step(progress, FWUPD_STATUS_DECOMPRESSING, 0, "prepare-fw");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 1, "detach");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_WRITE, 97, "write");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 1, "attach");
	fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 1, "reload");
}

static void
fu_fastboot_device_init(FuFastbootDevice *self)
{
	/* this is a safe default, even using USBv1 */
	self->blocksz = 512;
	/* no delay is applied by default after a read or write operation */
	self->operation_delay = 0;
	fu_device_add_protocol(FU_DEVICE(self), "com.google.fastboot");
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_UPDATABLE);
	fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_IS_BOOTLOADER);
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_ADD_COUNTERPART_GUIDS);
	fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_REPLUG_MATCH_GUID);
	fu_device_set_remove_delay(FU_DEVICE(self), FASTBOOT_REMOVE_DELAY_RE_ENUMERATE);
	fu_device_set_firmware_gtype(FU_DEVICE(self), FU_TYPE_ARCHIVE_FIRMWARE);
	fu_usb_device_set_claim_retry_count(FU_USB_DEVICE(self), 5);
}

static void
fu_fastboot_device_class_init(FuFastbootDeviceClass *klass)
{
	FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
	device_class->probe = fu_fastboot_device_probe;
	device_class->setup = fu_fastboot_device_setup;
	device_class->write_firmware = fu_fastboot_device_write_firmware;
	device_class->attach = fu_fastboot_device_attach;
	device_class->to_string = fu_fastboot_device_to_string;
	device_class->set_quirk_kv = fu_fastboot_device_set_quirk_kv;
	device_class->set_progress = fu_fastboot_device_set_progress;
}