Debian Codegraph @

src/qmi-firmware-update/qfu-sahara-device.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Qfu-firmware-update -- Command line tool to update firmware in QFU devices
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Copyright (C) 2019 Zodiac Inflight Innovations
19 * Copyright (C) 2019 Aleksander Morgado <[email protected]>
20 */
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <termios.h>
28 #include <unistd.h>
29
30 #include <glib-object.h>
31 #include <gio/gio.h>
32
33 #include "qfu-log.h"
34 #include "qfu-firehose-message.h"
35 #include "qfu-sahara-message.h"
36 #include "qfu-sahara-device.h"
37 #include "qfu-utils.h"
38 #include "qfu-enum-types.h"
39
40 static void initable_iface_init (GInitableIface *iface);
41
42 G_DEFINE_TYPE_EXTENDED (QfuSaharaDevice, qfu_sahara_device, G_TYPE_OBJECT, 0,
43 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init))
44
45 enum {
46 PROP_0,
47 PROP_FILE,
48 PROP_LAST
49 };
50
51 static GParamSpec *properties[PROP_LAST];
52
53 #define DEFAULT_SECTOR_SIZE_IN_BYTES 4096
54 #define DEFAULT_PAGES_IN_BLOCK 64
55 #define MAX_PRINTABLE_SIZE 80
56
57 struct _QfuSaharaDevicePrivate {
58 GFile *file;
59 gint fd;
60 GByteArray *buffer;
61
62 /* target and transfer settings */
63 guint max_payload_size_to_target_in_bytes;
64 guint sector_size_in_bytes;
65 guint pages_in_block;
66 /* computed from settings */
67 guint transfer_block_size;
68 /* number of images setup */
69 guint n_setup_images;
70 };
71
72 /******************************************************************************/
73 /* Split response into multiple XML messages */
74
75 #define XML_START_TAG "<?xml"
76
77 static gchar **
78 split_xml_document (const gchar *rsp)
79 {
80 GPtrArray *xml_docs = NULL;
81 const gchar *aux;
82 const gchar *next;
83
84 xml_docs = g_ptr_array_new ();
85 aux = strstr (rsp, XML_START_TAG);
86
87 while (aux) {
88 gchar *xmldoc;
89
90 next = strstr (aux + 1, XML_START_TAG);
91 if (next)
92 xmldoc = g_strndup (aux, next - aux);
93 else
94 xmldoc = g_strdup (aux);
95 g_strdelimit (xmldoc, "\r\n", ' ');
96 g_ptr_array_add (xml_docs, xmldoc);
97 aux = next;
98 }
99
100 if (xml_docs->len == 0) {
101 g_ptr_array_free (xml_docs, TRUE);
102 return NULL;
103 }
104 g_ptr_array_add (xml_docs, NULL);
105 return (gchar **) g_ptr_array_free (xml_docs, FALSE);
106 }
107
108 /******************************************************************************/
109 /* Send */
110
111 static gboolean
112 send_request (QfuSaharaDevice *self,
113 const guint8 *request,
114 gsize request_size,
115 GCancellable *cancellable,
116 GError **error)
117 {
118 gssize wlen;
119 fd_set wr;
120 gint aux;
121 struct timeval tv = {
122 .tv_sec = 2,
123 .tv_usec = 0,
124 };
125
126 /* Wait for the fd to be writable and don't wait forever */
127 FD_ZERO (&wr);
128 FD_SET (self->priv->fd, &wr);
129 aux = select (self->priv->fd + 1, NULL, &wr, NULL, &tv);
130
131 if (g_cancellable_set_error_if_cancelled (cancellable, error))
132 return FALSE;
133
134 if (aux < 0) {
135 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
136 "error waiting to write: %s",
137 g_strerror (errno));
138 return FALSE;
139 }
140
141 if (aux == 0) {
142 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
143 "timed out waiting to write");
144 return FALSE;
145 }
146
147 /* Debug output */
148 if (qfu_log_get_verbose ()) {
149 gchar *printable;
150 gsize printable_size = request_size;
151 gboolean shorted = FALSE;
152
153 if (printable_size > MAX_PRINTABLE_SIZE) {
154 printable_size = MAX_PRINTABLE_SIZE;
155 shorted = TRUE;
156 }
157
158 printable = qfu_utils_str_hex (request, printable_size, ':');
159 g_debug ("[qfu-sahara-device] >> %s%s [%" G_GSIZE_FORMAT "]", printable, shorted ? "..." : "", request_size);
160 g_free (printable);
161
162 if (strncmp ((const gchar *)request, XML_START_TAG, strlen (XML_START_TAG)) == 0) {
163 printable = g_strdup ((const gchar *)request);
164 g_strdelimit (printable, "\r\n", ' ');
165 g_debug ("[qfu-sahara-device] >> %s", printable);
166 g_free (printable);
167 }
168 }
169
170 wlen = write (self->priv->fd, request, request_size);
171 if (wlen < 0) {
172 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
173 "error writting: %s",
174 g_strerror (errno));
175 return FALSE;
176 }
177
178 /* We treat EINTR as an error, so we also treat as an error if not all bytes
179 * were wlen */
180 if ((gsize)wlen != request_size) {
181 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
182 "error writing: only %" G_GSSIZE_FORMAT "/%" G_GSIZE_FORMAT " bytes written",
183 wlen, request_size);
184 return FALSE;
185 }
186
187 return TRUE;
188 }
189
190 /******************************************************************************/
191 /* Receive */
192
193 static gssize
194 receive_response (QfuSaharaDevice *self,
195 guint timeout_secs,
196 guint8 **response,
197 GCancellable *cancellable,
198 GError **error)
199 {
200 fd_set rd;
201 struct timeval tv;
202 gint aux;
203 gssize rlen;
204
205 /* Use requested timeout */
206 tv.tv_sec = timeout_secs;
207 tv.tv_usec = 0;
208
209 FD_ZERO (&rd);
210 FD_SET (self->priv->fd, &rd);
211 aux = select (self->priv->fd + 1, &rd, NULL, NULL, &tv);
212
213 if (g_cancellable_set_error_if_cancelled (cancellable, error))
214 return -1;
215
216 if (aux < 0) {
217 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
218 "error waiting to read response: %s",
219 g_strerror (errno));
220 return -1;
221 }
222
223 /* we may not always get a response, so just return 0 bytes read if we timeout */
224 if (aux == 0)
225 return 0;
226
227 /* Receive in the primary buffer
228 * Always leave room for setting next byte as NUL. */
229 memset (self->priv->buffer->data, 0, self->priv->buffer->len);
230 rlen = read (self->priv->fd, self->priv->buffer->data, self->priv->buffer->len - 1);
231 if (rlen < 0) {
232 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
233 "couldn't read response: %s",
234 g_strerror (errno));
235 return -1;
236 }
237
238 if (rlen == 0) {
239 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
240 "couldn't read response: HUP detected");
241 return -1;
242 }
243
244 /* make sure that we can treat the response as a NUL-terminated string */
245 g_assert ((guint)rlen <= self->priv->buffer->len - 1);
246 self->priv->buffer->data[rlen] = '\0';
247
248 /* Debug output */
249 if (qfu_log_get_verbose ()) {
250 gchar *printable;
251 gsize printable_size = rlen;
252 gboolean shorted = FALSE;
253
254 if (printable_size > MAX_PRINTABLE_SIZE) {
255 printable_size = MAX_PRINTABLE_SIZE;
256 shorted = TRUE;
257 }
258
259 printable = qfu_utils_str_hex (self->priv->buffer->data, printable_size, ':');
260 g_debug ("[qfu-sahara-device] << %s%s [%" G_GSIZE_FORMAT "]", printable, shorted ? "..." : "", rlen);
261 g_free (printable);
262
263 if (strncmp ((const gchar *)self->priv->buffer->data, XML_START_TAG, strlen (XML_START_TAG)) == 0) {
264 printable = g_strdup ((const gchar *)self->priv->buffer->data);
265 g_strdelimit (printable, "\r\n", ' ');
266 g_debug ("[qfu-sahara-device] << %s", printable);
267 g_free (printable);
268 }
269 }
270
271 if (response)
272 *response = self->priv->buffer->data;
273
274 return rlen;
275 }
276
277 /******************************************************************************/
278 /* Send/receive */
279
280 static gssize
281 send_receive (QfuSaharaDevice *self,
282 const guint8 *request,
283 gsize request_size,
284 guint response_timeout_secs,
285 guint8 **response,
286 GCancellable *cancellable,
287 GError **error)
288 {
289 gboolean sent;
290
291 if (self->priv->fd < 0) {
292 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "device is closed");
293 return -1;
294 }
295
296 if (request_size > 0) {
297 sent = send_request (self, request, request_size, cancellable, error);
298 if (!sent)
299 return -1;
300 }
301
302 if (!response)
303 return 0;
304
305 return receive_response (self, response_timeout_secs, response, cancellable, error);
306 }
307
308 /******************************************************************************/
309 /* Common firehose state machine */
310
311 static gboolean
312 firehose_common_process_response_ack_message (const gchar *rsp,
313 const gchar *expected_value,
314 const gchar *expected_rawmode,
315 GError **error)
316 {
317 gchar *value = NULL;
318 gchar *rawmode = NULL;
319
320 g_assert (expected_value);
321
322 if (!qfu_firehose_message_parse_response_ack (rsp, &value, &rawmode))
323 return FALSE;
324
325 if ((g_strcmp0 (value, expected_value) == 0) && (!expected_rawmode || (rawmode && g_strcmp0 (rawmode, expected_rawmode) == 0)))
326 g_debug ("[qfu-sahara-device] firehose response received: value=%s, rawmode=%s",
327 value, rawmode ? rawmode : "n/a");
328 else
329 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
330 "unexpected firehose response received: value=%s, rawmode=%s",
331 value, rawmode ? rawmode : "n/a");
332
333 g_free (value);
334 g_free (rawmode);
335 return TRUE;
336 }
337
338 static gboolean
339 firehose_common_process_log_message (const gchar *rsp)
340 {
341 gchar *value = NULL;
342
343 if (!qfu_firehose_message_parse_log (rsp, &value))
344 return FALSE;
345
346 g_debug ("[qfu-sahara-device] firehose log: %s", value);
347 g_free (value);
348 return TRUE;
349 }
350
351 typedef const gchar * (* PrepareRequestCallback) (QfuSaharaDevice *self,
352 gpointer user_data);
353 typedef gboolean (* ProcessResponseCallback) (QfuSaharaDevice *self,
354 const gchar *rsp,
355 gpointer user_data,
356 GError **error);
357 typedef gboolean (* CheckCompletionCallback) (QfuSaharaDevice *self,
358 gpointer user_data);
359 typedef void (* InitRetryCallback) (QfuSaharaDevice *self,
360 gpointer user_data);
361
362 static gboolean
363 firehose_operation_run (QfuSaharaDevice *self,
364 PrepareRequestCallback prepare_request,
365 ProcessResponseCallback process_response,
366 CheckCompletionCallback check_completion,
367 InitRetryCallback init_retry,
368 guint max_retries,
369 guint timeout_secs,
370 gpointer user_data,
371 GCancellable *cancellable,
372 GError **error)
373 {
374 GTimer *timer;
375 GError *inner_error = NULL;
376 guint n_retries = 0;
377
378 g_assert ((max_retries && init_retry) || (!max_retries && !init_retry));
379
380 g_debug ("[qfu-sahara-device] running firehose operation...");
381
382 timer = g_timer_new ();
383 while (TRUE) {
384 const gchar *req = NULL;
385 gssize rsplen;
386 guint8 *rsp = NULL;
387 gchar **xmldocs;
388 guint i;
389
390 /* check timeout */
391 if (g_timer_elapsed (timer, NULL) > timeout_secs) {
392 /* retry? */
393 if (max_retries && ++n_retries < max_retries) {
394 g_timer_reset (timer);
395 g_clear_error (&inner_error);
396 init_retry (self, user_data);
397 continue;
398 }
399 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_TIMED_OUT, "operation timed out");
400 break;
401 }
402 /* check cancellation */
403 if (g_cancellable_is_cancelled (cancellable)) {
404 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED, "operation cancelled");
405 break;
406 }
407
408 /* user-provided callback to prepare request, may return NULL if there's nothing to send */
409 if (prepare_request)
410 req = prepare_request (self, user_data);
411
412 rsplen = send_receive (self,
413 (const guint8 *)req,
414 req ? strlen (req) : 0,
415 2,
416 &rsp,
417 cancellable,
418 &inner_error);
419 if (rsplen < 0)
420 break;
421
422 /* timed out without response received */
423 if (!rsplen)
424 continue;
425
426 /* we may receive multiple XML documents on a single read() */
427 xmldocs = split_xml_document ((const gchar *)rsp);
428 for (i = 0; xmldocs && xmldocs[i] && !inner_error; i++) {
429 /* user-provided callback to process response, may return FALSE and
430 * an error if the operation is detected as failed */
431 process_response (self, xmldocs[i], user_data, &inner_error);
432 }
433 g_strfreev (xmldocs);
434 if (inner_error) {
435 /* retry? */
436 if (max_retries && ++n_retries < max_retries) {
437 g_timer_reset (timer);
438 g_clear_error (&inner_error);
439 init_retry (self, user_data);
440 continue;
441 }
442 break;
443 }
444
445 /* keep on operation? */
446 if (check_completion (self, user_data))
447 break;
448 }
449
450 g_timer_destroy (timer);
451
452 if (inner_error) {
453 g_debug ("[qfu-sahara-device] firehose operation failed: %s", inner_error->message);
454 g_propagate_error (error, inner_error);
455 return FALSE;
456 }
457
458 g_debug ("[qfu-sahara-device] firehose operation finished successfully");
459 return TRUE;
460 }
461
462 /******************************************************************************/
463
464 static gboolean
465 validate_ascii_print (const guint8 *rsp,
466 gsize rsplen)
467 {
468 guint i;
469
470 for (i = 0; i < rsplen; i++) {
471 if (!g_ascii_isprint (rsp[i]))
472 return FALSE;
473 }
474
475 return TRUE;
476 }
477
478 /******************************************************************************/
479 /* Firehose setup download */
480
481 #define FIREHOSE_SETUP_DOWNLOAD_TIMEOUT_SECS 10
482 #define FIREHOSE_SETUP_DOWNLOAD_MAX_RETRIES 3
483
484 typedef struct {
485 guint n_partition_sectors;
486 gboolean sent;
487 gboolean acked;
488 } FirehoseSetupDownloadContext;
489
490 static const gchar *
491 firehose_setup_download_prepare_request (QfuSaharaDevice *self,
492 FirehoseSetupDownloadContext *ctx)
493 {
494 if (!ctx->sent) {
495 ctx->sent = TRUE;
496 g_debug ("[qfu-sahara-device] sending firehose program request...");
497 qfu_firehose_message_build_program (self->priv->buffer->data,
498 self->priv->buffer->len,
499 self->priv->pages_in_block,
500 self->priv->sector_size_in_bytes,
501 ctx->n_partition_sectors);
502 return (const gchar *)self->priv->buffer->data;
503 }
504
505 return NULL;
506 }
507
508 static gboolean
509 firehose_setup_download_process_response (QfuSaharaDevice *self,
510 const gchar *rsp,
511 FirehoseSetupDownloadContext *ctx,
512 GError **error)
513 {
514 GError *inner_error = NULL;
515
516 if (firehose_common_process_log_message (rsp))
517 return TRUE;
518
519 if (firehose_common_process_response_ack_message (rsp, "ACK", "true", &inner_error)) {
520 if (inner_error) {
521 g_propagate_error (error, inner_error);
522 return FALSE;
523 }
524
525 ctx->acked = TRUE;
526 return TRUE;
527 }
528
529 g_debug ("[qfu-sahara-device] unknown firehose message received");
530 return TRUE;
531 }
532
533 static gboolean
534 firehose_setup_download_check_completion (QfuSaharaDevice *self,
535 FirehoseSetupDownloadContext *ctx)
536 {
537 return (ctx->acked);
538 }
539
540 static void
541 firehose_setup_download_init_retry (QfuSaharaDevice *self,
542 FirehoseSetupDownloadContext *ctx)
543 {
544 /* no need to cleanup n_partition_sectors */
545
546 ctx->sent = FALSE;
547 ctx->acked = FALSE;
548 }
549
550 gboolean
551 qfu_sahara_device_firehose_setup_download (QfuSaharaDevice *self,
552 QfuImage *image,
553 guint *n_blocks,
554 GCancellable *cancellable,
555 GError **error)
556 {
557 FirehoseSetupDownloadContext ctx = {
558 .n_partition_sectors = 0,
559 .sent = FALSE,
560 .acked = FALSE,
561 };
562 goffset image_size;
563 guint n_transfer_blocks;
564
565 /* NOTE: the firmware download process in Windows sends an additional
566 * configure message before the program request when the 2nd firmware
567 * image is downloaded... but it really doesn't seem to be required
568 * for anything, so we're explicitly avoiding that. Sending the
569 * program request seems to be enough. */
570
571 /* compute how many sectors are required */
572 image_size = qfu_image_get_size (image);
573 ctx.n_partition_sectors = image_size / self->priv->sector_size_in_bytes;
574 if (image_size % self->priv->sector_size_in_bytes > 0)
575 ctx.n_partition_sectors++;
576
577 /* compute how many transfer block are required, and set them as output right away */
578 n_transfer_blocks = image_size / self->priv->transfer_block_size;
579 if (image_size % self->priv->transfer_block_size > 0)
580 n_transfer_blocks++;
581 if (n_blocks)
582 *n_blocks = n_transfer_blocks;
583
584 g_debug ("Setting up firehose download for %" G_GOFFSET_FORMAT " bytes image...", image_size);
585 g_debug (" pages in block: %u", self->priv->pages_in_block);
586 g_debug (" sector size: %u", self->priv->sector_size_in_bytes);
587 g_debug (" num partition sectors: %u", ctx.n_partition_sectors);
588 g_debug (" transfer block size: %u (%u sectors/transfer)", self->priv->transfer_block_size, self->priv->transfer_block_size / self->priv->sector_size_in_bytes);
589 g_debug (" num transfers: %u", n_transfer_blocks);
590
591 return firehose_operation_run (self,
592 (PrepareRequestCallback) firehose_setup_download_prepare_request,
593 (ProcessResponseCallback) firehose_setup_download_process_response,
594 (CheckCompletionCallback) firehose_setup_download_check_completion,
595 (InitRetryCallback) firehose_setup_download_init_retry,
596 FIREHOSE_SETUP_DOWNLOAD_MAX_RETRIES,
597 FIREHOSE_SETUP_DOWNLOAD_TIMEOUT_SECS,
598 &ctx,
599 cancellable,
600 error);
601 }
602
603 /******************************************************************************/
604 /* Firehose write block in raw mode */
605
606 #define END_OF_TRANSFER_BLOCK_SIZE 512
607
608 gboolean
609 qfu_sahara_device_firehose_write_block (QfuSaharaDevice *self,
610 QfuImage *image,
611 guint block_i,
612 GCancellable *cancellable,
613 GError **error)
614 {
615 gssize reqlen;
616 goffset offset;
617 gsize size;
618 gboolean send_last = FALSE;
619
620 g_debug ("[qfu-sahara-device] writing block %u...", block_i);
621
622 g_assert (self->priv->transfer_block_size < self->priv->buffer->len);
623 memset (self->priv->buffer->data, 0, self->priv->transfer_block_size);
624
625 offset = block_i * (goffset)self->priv->transfer_block_size;
626 size = qfu_image_get_size (image) - offset;
627 if (size >= self->priv->transfer_block_size)
628 size = self->priv->transfer_block_size;
629 else {
630 gsize last_block_size;
631
632 /* we need to send an additional packet full of 0s after the last
633 * sector is transferred. */
634 send_last = TRUE;
635
636 /* last transfer block adjusted to sector size multiple */
637 last_block_size = self->priv->sector_size_in_bytes;
638 while (last_block_size < size)
639 last_block_size += self->priv->sector_size_in_bytes;
640 size = last_block_size;
641 g_assert (size <= self->priv->transfer_block_size);
642 }
643
644 reqlen = qfu_image_read (image,
645 offset,
646 size,
647 self->priv->buffer->data,
648 self->priv->buffer->len,
649 cancellable,
650 error);
651 if (reqlen < 0) {
652 g_prefix_error (error, "couldn't read transfer block %u", block_i);
653 return FALSE;
654 }
655
656 g_assert ((guint)reqlen <= self->priv->transfer_block_size);
657 if (send_receive (self,
658 self->priv->buffer->data,
659 size,
660 0,
661 NULL,
662 cancellable,
663 error) < 0) {
664 g_prefix_error (error, "couldn't send transfer block %u", block_i);
665 return FALSE;
666 }
667
668 if (send_last) {
669 /* We're sending a last block to notify the end of the transmission,
670 * which seems to be a reliable way to tell the modem that it shouldn't
671 * expect more data.
672 *
673 * This block is full of 0s, but the modem seems to end up storing
674 * it and leaving it to be processed once the image has been processed,
675 * which will trigger a warning during the next firehose operation:
676 * ERROR: XML not formed correctly. Expected a &lt; character at loc 0
677 * And it will also fail the operation with a NAK...
678 *
679 * But, just retrying the operation (the program request for the next
680 * file to download, or the reset if no more files to download) is enough
681 * to make it work.
682 */
683 memset (self->priv->buffer->data, 0, END_OF_TRANSFER_BLOCK_SIZE);
684 if (send_receive (self,
685 self->priv->buffer->data,
686 END_OF_TRANSFER_BLOCK_SIZE,
687 0,
688 NULL,
689 cancellable,
690 error) < 0) {
691 g_prefix_error (error, "couldn't send last end-of-transfer block");
692 return FALSE;
693 }
694 }
695
696 return TRUE;
697 }
698
699 /******************************************************************************/
700 /* Firehose teardown download */
701
702 #define FIREHOSE_TEARDOWN_DOWNLOAD_TIMEOUT_SECS 300
703
704 typedef struct {
705 gboolean acked;
706 } FirehoseTeardownDownloadContext;
707
708 static gboolean
709 firehose_teardown_download_process_response (QfuSaharaDevice *self,
710 const gchar *rsp,
711 FirehoseTeardownDownloadContext *ctx,
712 GError **error)
713 {
714 GError *inner_error = NULL;
715
716 if (firehose_common_process_log_message (rsp))
717 return TRUE;
718
719 if (firehose_common_process_response_ack_message (rsp, "ACK", "false", &inner_error)) {
720 /* We've seen in a EM7511 how the response to the download operation comes
721 * followed *right away* in the same read() with the "XML not formed correctly"
722 * warning plus an additional response with a NAK. In order to avoid failing
723 * the teardown operation with that second response, we'll ignore it completely
724 * if we have already detected a successful response earlier.
725 */
726 if (ctx->acked) {
727 g_debug ("[qfu-sahara-device] ignoring additional response message detected");
728 g_clear_error (&inner_error);
729 return TRUE;
730 }
731 if (inner_error) {
732 g_propagate_error (error, inner_error);
733 return FALSE;
734 }
735 ctx->acked = TRUE;
736 return TRUE;
737 }
738
739 g_debug ("[qfu-sahara-device] unknown firehose message received");
740 return TRUE;
741 }
742
743 static gboolean
744 firehose_teardown_download_check_completion (QfuSaharaDevice *self,
745 FirehoseTeardownDownloadContext *ctx)
746 {
747 return (ctx->acked);
748 }
749
750 gboolean
751 qfu_sahara_device_firehose_teardown_download (QfuSaharaDevice *self,
752 QfuImage *image,
753 GCancellable *cancellable,
754 GError **error)
755 {
756 FirehoseTeardownDownloadContext ctx = {
757 .acked = FALSE,
758 };
759
760 return firehose_operation_run (self,
761 NULL, /* PrepareRequestCallback */
762 (ProcessResponseCallback) firehose_teardown_download_process_response,
763 (CheckCompletionCallback) firehose_teardown_download_check_completion,
764 NULL, /* InitRetryCallback */
765 0, /* max_retries */
766 FIREHOSE_TEARDOWN_DOWNLOAD_TIMEOUT_SECS,
767 &ctx,
768 cancellable,
769 error);
770 }
771
772 /******************************************************************************/
773 /* Firehose reset */
774
775 #define FIREHOSE_RESET_TIMEOUT_SECS 10
776 #define FIREHOSE_RESET_MAX_RETRIES 10
777
778 typedef struct {
779 gboolean sent;
780 gboolean acked;
781 } FirehoseResetContext;
782
783 static const gchar *
784 firehose_reset_prepare_request (QfuSaharaDevice *self,
785 FirehoseResetContext *ctx)
786 {
787 if (!ctx->sent) {
788 ctx->sent = TRUE;
789 g_debug ("[qfu-sahara-device] sending firehose reset...");
790 qfu_firehose_message_build_reset (self->priv->buffer->data, self->priv->buffer->len);
791 return (const gchar *)self->priv->buffer->data;
792 }
793
794 return NULL;
795 }
796
797 static gboolean
798 firehose_reset_process_response (QfuSaharaDevice *self,
799 const gchar *rsp,
800 FirehoseResetContext *ctx,
801 GError **error)
802 {
803 GError *inner_error = NULL;
804
805 if (firehose_common_process_log_message (rsp))
806 return TRUE;
807
808 if (firehose_common_process_response_ack_message (rsp, "ACK", NULL, &inner_error)) {
809 if (inner_error) {
810 g_propagate_error (error, inner_error);
811 return FALSE;
812 }
813 ctx->acked = TRUE;
814 return TRUE;
815 }
816
817 g_debug ("[qfu-sahara-device] unknown firehose message received");
818 return TRUE;
819 }
820
821 static gboolean
822 firehose_reset_check_completion (QfuSaharaDevice *self,
823 FirehoseResetContext *ctx)
824 {
825 return (ctx->acked);
826 }
827
828 static void
829 firehose_reset_init_retry (QfuSaharaDevice *self,
830 FirehoseResetContext *ctx)
831 {
832 ctx->sent = FALSE;
833 ctx->acked = FALSE;
834 }
835
836 gboolean
837 qfu_sahara_device_firehose_reset (QfuSaharaDevice *self,
838 GCancellable *cancellable,
839 GError **error)
840 {
841 FirehoseResetContext ctx = {
842 .sent = FALSE,
843 .acked = FALSE,
844 };
845
846 return firehose_operation_run (self,
847 (PrepareRequestCallback) firehose_reset_prepare_request,
848 (ProcessResponseCallback) firehose_reset_process_response,
849 (CheckCompletionCallback) firehose_reset_check_completion,
850 (InitRetryCallback) firehose_reset_init_retry,
851 FIREHOSE_RESET_MAX_RETRIES,
852 FIREHOSE_RESET_TIMEOUT_SECS,
853 &ctx,
854 cancellable,
855 error);
856 }
857
858 /******************************************************************************/
859 /* Firehose initialization */
860
861 #define FIREHOSE_INIT_TIMEOUT_SECS 10
862
863 typedef enum {
864 FIREHOSE_INIT_STEP_PING,
865 FIREHOSE_INIT_STEP_WAIT_PING,
866 FIREHOSE_INIT_STEP_CONFIGURE,
867 FIREHOSE_INIT_STEP_WAIT_CONFIGURE,
868 FIREHOSE_INIT_STEP_STORAGE_INFO,
869 FIREHOSE_INIT_STEP_WAIT_STORAGE_INFO,
870 FIREHOSE_INIT_STEP_LAST,
871 } FirehoseInitStep;
872
873 typedef struct {
874 FirehoseInitStep step;
875 guint max_payload_size_to_target_in_bytes;
876 guint sector_size_in_bytes;
877 guint pages_in_block;
878 } FirehoseInitContext;
879
880 static gboolean
881 firehose_init_context_process_log_message (const gchar *rsp,
882 FirehoseInitContext *ctx)
883 {
884 gchar *value = NULL;
885 gchar **strv;
886
887 if (!qfu_firehose_message_parse_log (rsp, &value))
888 return FALSE;
889
890 /* The log message may contain specific settings that we want to read */
891 strv = g_strsplit (value, "=", -1);
892 if (g_strv_length (strv) == 2) {
893 g_strstrip (strv[0]);
894 g_strstrip (strv[1]);
895 if (g_ascii_strcasecmp (strv[0], "sector_size_in_bytes") == 0)
896 ctx->sector_size_in_bytes = atoi (strv[1]);
897 else if (g_ascii_strcasecmp (strv[0], "pages_in_block") == 0)
898 ctx->pages_in_block = atoi (strv[1]);
899 }
900 g_strfreev (strv);
901
902 g_debug ("[qfu-sahara-device] firehose log: %s", value);
903 g_free (value);
904 return TRUE;
905 }
906
907 static gboolean
908 firehose_init_context_process_response_configure_message (const gchar *rsp,
909 FirehoseInitContext *ctx,
910 GError **error)
911 {
912 guint32 max_payload_size_to_target_in_bytes = 0;
913
914 if (!qfu_firehose_message_parse_response_configure (rsp, &max_payload_size_to_target_in_bytes))
915 return FALSE;
916
917 if (max_payload_size_to_target_in_bytes > 0) {
918 g_debug ("[qfu-sahara-device] firehose requested max payload size: %u bytes", max_payload_size_to_target_in_bytes);
919 ctx->max_payload_size_to_target_in_bytes = max_payload_size_to_target_in_bytes;
920 } else {
921 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
922 "unexpected max payload size: %u", max_payload_size_to_target_in_bytes);
923 }
924
925 return TRUE;
926 }
927
928 static const gchar *
929 firehose_init_prepare_request (QfuSaharaDevice *self,
930 FirehoseInitContext *ctx)
931 {
932 switch (ctx->step) {
933 case FIREHOSE_INIT_STEP_PING:
934 g_debug ("[qfu-sahara-device] sending firehose ping...");
935 qfu_firehose_message_build_ping (self->priv->buffer->data, self->priv->buffer->len);
936 ctx->step++;
937 return (const gchar *)self->priv->buffer->data;
938 case FIREHOSE_INIT_STEP_WAIT_PING:
939 /* not sending anything, just processing responses */
940 return NULL;
941 case FIREHOSE_INIT_STEP_CONFIGURE:
942 g_debug ("[qfu-sahara-device] sending firehose configure...");
943 qfu_firehose_message_build_configure (self->priv->buffer->data, self->priv->buffer->len, 0);
944 ctx->step++;
945 return (const gchar *)self->priv->buffer->data;
946 case FIREHOSE_INIT_STEP_WAIT_CONFIGURE:
947 /* not sending anything, just processing responses */
948 return NULL;
949 case FIREHOSE_INIT_STEP_STORAGE_INFO:
950 g_debug ("[qfu-sahara-device] sending firehose storage info request...");
951 qfu_firehose_message_build_get_storage_info (self->priv->buffer->data, self->priv->buffer->len);
952 ctx->step++;
953 return (const gchar *)self->priv->buffer->data;
954 case FIREHOSE_INIT_STEP_WAIT_STORAGE_INFO:
955 /* not sending anything, just processing responses */
956 return NULL;
957 case FIREHOSE_INIT_STEP_LAST:
958 default:
959 g_assert_not_reached ();
960 return NULL;
961 }
962 }
963
964 static gboolean
965 firehose_init_process_response (QfuSaharaDevice *self,
966 const gchar *rsp,
967 FirehoseInitContext *ctx,
968 GError **error)
969 {
970 GError *inner_error = NULL;
971
972 if (firehose_init_context_process_log_message (rsp, ctx))
973 return TRUE;
974
975 if (firehose_common_process_response_ack_message (rsp, "ACK", NULL, &inner_error)) {
976 if (inner_error) {
977 g_propagate_error (error, inner_error);
978 return FALSE;
979 }
980 /* if we were expecting a response, go on to next step */
981 if (ctx->step == FIREHOSE_INIT_STEP_WAIT_PING || ctx->step == FIREHOSE_INIT_STEP_WAIT_STORAGE_INFO)
982 ctx->step++;
983 return TRUE;
984 }
985
986 if (firehose_init_context_process_response_configure_message (rsp, ctx, &inner_error)) {
987 if (inner_error) {
988 g_propagate_error (error, inner_error);
989 return FALSE;
990 }
991 /* if we were expecting a response, go on to next step */
992 if (ctx->step == FIREHOSE_INIT_STEP_WAIT_CONFIGURE)
993 ctx->step++;
994 return TRUE;
995 }
996
997 g_debug ("[qfu-sahara-device] unknown firehose message received");
998 return TRUE;
999 }
1000
1001 static gboolean
1002 firehose_init_check_completion (QfuSaharaDevice *self,
1003 FirehoseInitContext *ctx)
1004 {
1005 return (ctx->step == FIREHOSE_INIT_STEP_LAST);
1006 }
1007
1008 static gboolean
1009 sahara_device_firehose_init (QfuSaharaDevice *self,
1010 GCancellable *cancellable,
1011 GError **error)
1012 {
1013 FirehoseInitContext ctx = {
1014 .step = FIREHOSE_INIT_STEP_PING,
1015 .max_payload_size_to_target_in_bytes = 0,
1016 .sector_size_in_bytes = 0,
1017 .pages_in_block = 0,
1018 };
1019
1020 if (!firehose_operation_run (self,
1021 (PrepareRequestCallback) firehose_init_prepare_request,
1022 (ProcessResponseCallback) firehose_init_process_response,
1023 (CheckCompletionCallback) firehose_init_check_completion,
1024 NULL, /* InitRetryCallback */
1025 0, /* max_retries */
1026 FIREHOSE_INIT_TIMEOUT_SECS,
1027 &ctx,
1028 cancellable,
1029 error))
1030 return FALSE;
1031
1032 if (!ctx.sector_size_in_bytes) {
1033 g_debug ("[qfu-sahara-device] using default sector size (%u bytes)", DEFAULT_SECTOR_SIZE_IN_BYTES);
1034 self->priv->sector_size_in_bytes = DEFAULT_SECTOR_SIZE_IN_BYTES;
1035 } else
1036 self->priv->sector_size_in_bytes = ctx.sector_size_in_bytes;
1037
1038 if (!ctx.pages_in_block) {
1039 g_debug ("[qfu-sahara-device] using default pages in block (%u)", DEFAULT_PAGES_IN_BLOCK);
1040 self->priv->pages_in_block = DEFAULT_PAGES_IN_BLOCK;
1041 } else
1042 self->priv->pages_in_block = ctx.pages_in_block;
1043
1044 if (!ctx.max_payload_size_to_target_in_bytes) {
1045 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "unknown max payload size");
1046 return FALSE;
1047 }
1048 self->priv->max_payload_size_to_target_in_bytes = ctx.max_payload_size_to_target_in_bytes;
1049
1050 /* compute transfer block size, which will be equal to the max payload size
1051 * to target if it's multiple of sector size */
1052 self->priv->transfer_block_size = (self->priv->max_payload_size_to_target_in_bytes / self->priv->sector_size_in_bytes) * self->priv->sector_size_in_bytes;
1053 g_assert (self->priv->transfer_block_size <= self->priv->max_payload_size_to_target_in_bytes);
1054 g_assert (self->priv->transfer_block_size > 0);
1055
1056 return TRUE;
1057 }
1058
1059 /******************************************************************************/
1060 /* Sahara initialization */
1061
1062 #define SAHARA_MAX_PROTOCOL_STEP_ATTEMPTS 5
1063
1064 typedef enum {
1065 SAHARA_PROTOCOL_STEP_UNKNOWN,
1066 SAHARA_PROTOCOL_STEP_INIT,
1067 SAHARA_PROTOCOL_STEP_HELLO,
1068 SAHARA_PROTOCOL_STEP_SWITCH,
1069 SAHARA_PROTOCOL_STEP_DATA,
1070 SAHARA_PROTOCOL_STEP_LAST,
1071 } SaharaProtocolStep;
1072
1073 static gboolean
1074 validate_firehose_switch_confirmation (const guint8 *rsp,
1075 gsize rsplen,
1076 GError **error)
1077 {
1078 if (!rsplen) {
1079 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "no confirmation received");
1080 return FALSE;
1081 }
1082
1083 if (!validate_ascii_print (rsp, rsplen)) {
1084 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "invalid confirmation data");
1085 return FALSE;
1086 }
1087
1088 return TRUE;
1089 }
1090
1091 static SaharaProtocolStep
1092 sahara_device_run_protocol_step (QfuSaharaDevice *self,
1093 SaharaProtocolStep step,
1094 GCancellable *cancellable,
1095 GError **error)
1096 {
1097 SaharaProtocolStep next_step = SAHARA_PROTOCOL_STEP_UNKNOWN;
1098 gsize reqlen = 0;
1099 gssize rsplen;
1100 guint8 *rsp = NULL;
1101
1102 memset (self->priv->buffer->data, 0, self->priv->buffer->len);
1103
1104 switch (step) {
1105 case SAHARA_PROTOCOL_STEP_INIT:
1106 /*
1107 * Just after opening the port we must NOT SEND anything to the device.
1108 * If we do that, we would get the sahara hello back, but the initialization
1109 * process would fail later on with a command-end-image-transfer message
1110 * reporting that the 0x0000ff00 command to switch to firehose protocol is
1111 * unsupported.
1112 */
1113 break;
1114 case SAHARA_PROTOCOL_STEP_HELLO:
1115 reqlen = qfu_sahara_response_hello_build (self->priv->buffer->data, self->priv->buffer->len);
1116 break;
1117 case SAHARA_PROTOCOL_STEP_SWITCH:
1118 reqlen = qfu_sahara_request_switch_build (self->priv->buffer->data, self->priv->buffer->len);
1119 break;
1120 case SAHARA_PROTOCOL_STEP_DATA:
1121 reqlen = qfu_sahara_request_switch_data_build (self->priv->buffer->data, self->priv->buffer->len);
1122 break;
1123 case SAHARA_PROTOCOL_STEP_UNKNOWN:
1124 case SAHARA_PROTOCOL_STEP_LAST:
1125 default:
1126 g_assert_not_reached ();
1127 break;
1128 }
1129
1130 rsplen = send_receive (self,
1131 reqlen ? self->priv->buffer->data : NULL,
1132 reqlen,
1133 3,
1134 &rsp,
1135 cancellable,
1136 error);
1137 if (rsplen < 0)
1138 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1139
1140 if (rsplen == 0) {
1141 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "no sahara response received");
1142 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1143 }
1144
1145 /* The sahara initialization finishes once the switch to firehose is confirmed.
1146 * The EM7565 replies "confirmed" explicitly, but we'll just accept any printable
1147 * ASCII string. */
1148 if (step == SAHARA_PROTOCOL_STEP_DATA) {
1149 if (!validate_firehose_switch_confirmation (rsp, rsplen, error))
1150 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1151
1152 /* initialization finished */
1153 g_debug ("[qfu-sahara-device] sahara initialization finished: %.*s", (gint)rsplen, rsp);
1154 return SAHARA_PROTOCOL_STEP_LAST;
1155 }
1156
1157 /* in case several messages are received together, parse and process them one by one */
1158 while (rsplen > 0) {
1159 gsize msglen;
1160 QfuSaharaHeader *msghdr;
1161
1162 if ((gsize)rsplen < QFU_SAHARA_MESSAGE_MAX_HEADER_SIZE) {
1163 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "sahara header not fully received: %" G_GSSIZE_FORMAT " < %" G_GSIZE_FORMAT,
1164 rsplen, QFU_SAHARA_MESSAGE_MAX_HEADER_SIZE);
1165 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1166 }
1167
1168 msghdr = (QfuSaharaHeader *)rsp;
1169 msglen = GUINT32_FROM_LE (msghdr->size);
1170 if ((gsize)rsplen < msglen) {
1171 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "sahara message not fully received: %" G_GSSIZE_FORMAT " < %" G_GSIZE_FORMAT,
1172 rsplen, msglen);
1173 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1174 }
1175
1176 switch (GUINT32_FROM_LE (msghdr->cmd)) {
1177 case QFU_SAHARA_CMD_HELLO_REQ:
1178 if (!qfu_sahara_request_hello_parse (rsp, rsplen, error))
1179 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1180 g_debug ("[qfu-sahara-device] sahara hello request received");
1181 next_step = SAHARA_PROTOCOL_STEP_HELLO;
1182 break;
1183 case QFU_SAHARA_CMD_COMMAND_READY:
1184 g_debug ("[qfu-sahara-device] module is ready for commands");
1185 next_step = SAHARA_PROTOCOL_STEP_SWITCH;
1186 break;
1187 case QFU_SAHARA_CMD_COMMAND_EXECUTE_RSP:
1188 g_debug ("[qfu-sahara-device] request to switch to firehose accepted");
1189 if (!qfu_sahara_response_switch_parse (rsp, rsplen, error))
1190 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1191 next_step = SAHARA_PROTOCOL_STEP_DATA;
1192 break;
1193 case QFU_SAHARA_CMD_COMMAND_END_IMAGE_TRANSFER:
1194 if (!qfu_sahara_response_end_image_transfer_parse (rsp, rsplen, error))
1195 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1196 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "unexpected sahara message");
1197 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1198 default:
1199 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "unsupported sahara message: '0x%08x'", GUINT32_FROM_LE (msghdr->cmd));
1200 return SAHARA_PROTOCOL_STEP_UNKNOWN;
1201 }
1202
1203 rsp += msglen;
1204 rsplen -= msglen;
1205 }
1206
1207 g_assert (next_step != SAHARA_PROTOCOL_STEP_UNKNOWN);
1208 return next_step;
1209 }
1210
1211 static gboolean
1212 sahara_device_initialize (QfuSaharaDevice *self,
1213 GCancellable *cancellable,
1214 GError **error)
1215 {
1216 SaharaProtocolStep step = SAHARA_PROTOCOL_STEP_INIT;
1217 guint n_attempts = 0;
1218
1219 while (step != SAHARA_PROTOCOL_STEP_LAST) {
1220 SaharaProtocolStep next_step;
1221
1222 /* check cancellation */
1223 if (g_cancellable_is_cancelled (cancellable)) {
1224 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED, "operation cancelled");
1225 return FALSE;
1226 }
1227
1228 /* check step error */
1229 next_step = sahara_device_run_protocol_step (self, step, cancellable, error);
1230 if (next_step == SAHARA_PROTOCOL_STEP_UNKNOWN)
1231 return FALSE;
1232
1233 /* retrying with same step? */
1234 if (next_step == step) {
1235 if (++n_attempts == SAHARA_MAX_PROTOCOL_STEP_ATTEMPTS) {
1236 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "too many attempts");
1237 return FALSE;
1238 }
1239 } else
1240 n_attempts = 0;
1241
1242 /* continue to next step */
1243 step = next_step;
1244 }
1245
1246 return TRUE;
1247 }
1248
1249 /******************************************************************************/
1250
1251 static gboolean
1252 initable_init (GInitable *initable,
1253 GCancellable *cancellable,
1254 GError **error)
1255 {
1256 QfuSaharaDevice *self;
1257 struct termios terminal_data;
1258 gchar *path = NULL;
1259 GError *inner_error = NULL;
1260
1261 self = QFU_SAHARA_DEVICE (initable);
1262
1263 if (g_cancellable_set_error_if_cancelled (cancellable, &inner_error))
1264 goto out;
1265
1266 path = g_file_get_path (self->priv->file);
1267 g_debug ("[qfu-sahara-device] opening TTY: %s", path);
1268
1269 self->priv->fd = open (path, O_RDWR | O_NOCTTY);
1270 if (self->priv->fd < 0) {
1271 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
1272 "error opening serial device: %s",
1273 g_strerror (errno));
1274 goto out;
1275 }
1276
1277 g_debug ("[qfu-sahara-device] setting terminal in raw mode...");
1278 if (tcgetattr (self->priv->fd, &terminal_data) < 0) {
1279 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
1280 "error getting serial port attributes: %s",
1281 g_strerror (errno));
1282 goto out;
1283 }
1284 cfmakeraw (&terminal_data);
1285 if (tcsetattr (self->priv->fd, TCSANOW, &terminal_data) < 0) {
1286 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
1287 "error setting serial port attributes: %s",
1288 g_strerror (errno));
1289 goto out;
1290 }
1291
1292 /* We need to give some time to the device before trying to initialize
1293 * the sahara protocol, because otherwise the sequence won't work. If
1294 * this wait time is not given, the initialization sequence will fail with
1295 * a command-end-image-transfer message reporting that the 0x0000ff00
1296 * command to switch to firehose protocol is unsupported.
1297 *
1298 * 2 full seconds selected a bit arbitrarily, didn't get any failure when
1299 * using this amount of time. */
1300 g_debug ("[qfu-sahara-device] waiting time for device to boot properly...");
1301 g_usleep (2 * G_USEC_PER_SEC);
1302
1303 g_debug ("[qfu-sahara-device] initializing sahara protocol...");
1304 if (!sahara_device_initialize (self, cancellable, &inner_error))
1305 goto out;
1306
1307 g_debug ("[qfu-sahara-device] initializing firehose protocol...");
1308 if (!sahara_device_firehose_init (self, cancellable, &inner_error))
1309 goto out;
1310
1311 out:
1312 g_free (path);
1313
1314 if (inner_error) {
1315 if (!(self->priv->fd < 0)) {
1316 close (self->priv->fd);
1317 self->priv->fd = -1;
1318 }
1319 g_propagate_error (error, inner_error);
1320 return FALSE;
1321 }
1322
1323 return TRUE;
1324 }
1325
1326 /******************************************************************************/
1327
1328 QfuSaharaDevice *
1329 qfu_sahara_device_new (GFile *file,
1330 GCancellable *cancellable,
1331 GError **error)
1332 {
1333 g_return_val_if_fail (G_IS_FILE (file), NULL);
1334
1335 return QFU_SAHARA_DEVICE (g_initable_new (QFU_TYPE_SAHARA_DEVICE,
1336 cancellable,
1337 error,
1338 "file", file,
1339 NULL));
1340 }
1341
1342
1343 static void
1344 qfu_sahara_device_init (QfuSaharaDevice *self)
1345 {
1346 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, QFU_TYPE_SAHARA_DEVICE, QfuSaharaDevicePrivate);
1347 self->priv->fd = -1;
1348 /* Long buffer for I/O, way more than ever needed when using sahara/firehose */
1349 self->priv->buffer = g_byte_array_new ();
1350 g_byte_array_set_size (self->priv->buffer, QFU_IMAGE_CHUNK_SIZE);
1351 }
1352
1353 static void
1354 set_property (GObject *object,
1355 guint prop_id,
1356 const GValue *value,
1357 GParamSpec *pspec)
1358 {
1359 QfuSaharaDevice *self = QFU_SAHARA_DEVICE (object);
1360
1361 switch (prop_id) {
1362 case PROP_FILE:
1363 self->priv->file = g_value_dup_object (value);
1364 break;
1365 default:
1366 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1367 break;
1368 }
1369 }
1370
1371 static void
1372 get_property (GObject *object,
1373 guint prop_id,
1374 GValue *value,
1375 GParamSpec *pspec)
1376 {
1377 QfuSaharaDevice *self = QFU_SAHARA_DEVICE (object);
1378
1379 switch (prop_id) {
1380 case PROP_FILE:
1381 g_value_set_object (value, self->priv->file);
1382 break;
1383 default:
1384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1385 break;
1386 }
1387 }
1388
1389 static void
1390 dispose (GObject *object)
1391 {
1392 QfuSaharaDevice *self = QFU_SAHARA_DEVICE (object);
1393
1394 if (!(self->priv->fd < 0)) {
1395 close (self->priv->fd);
1396 self->priv->fd = -1;
1397 }
1398
1399 G_OBJECT_CLASS (qfu_sahara_device_parent_class)->dispose (object);
1400 }
1401
1402 static void
1403 initable_iface_init (GInitableIface *iface)
1404 {
1405 iface->init = initable_init;
1406 }
1407
1408 static void
1409 qfu_sahara_device_class_init (QfuSaharaDeviceClass *klass)
1410 {
1411 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1412
1413 g_type_class_add_private (object_class, sizeof (QfuSaharaDevicePrivate));
1414
1415 object_class->dispose = dispose;
1416 object_class->get_property = get_property;
1417 object_class->set_property = set_property;
1418
1419 properties[PROP_FILE] =
1420 g_param_spec_object ("file",
1421 "File",
1422 "File object",
1423 G_TYPE_FILE,
1424 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
1425 g_object_class_install_property (object_class, PROP_FILE, properties[PROP_FILE]);
1426 }