Debian Codegraph @

src/qmi-firmware-update/qfu-qdl-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) 2016 Bjørn Mork <[email protected]>
19 * Copyright (C) 2016 Zodiac Inflight Innovations
20 * Copyright (C) 2016-2017 Aleksander Morgado <[email protected]>
21 */
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <termios.h>
29 #include <unistd.h>
30
31 #include <glib-object.h>
32 #include <gio/gio.h>
33
34 #include "qfu-log.h"
35 #include "qfu-qdl-message.h"
36 #include "qfu-dload-message.h"
37 #include "qfu-qdl-device.h"
38 #include "qfu-utils.h"
39 #include "qfu-enum-types.h"
40
41 static void initable_iface_init (GInitableIface *iface);
42
43 G_DEFINE_TYPE_EXTENDED (QfuQdlDevice, qfu_qdl_device, G_TYPE_OBJECT, 0,
44 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init))
45
46 enum {
47 PROP_0,
48 PROP_FILE,
49 PROP_LAST
50 };
51
52 static GParamSpec *properties[PROP_LAST];
53
54 #define SECONDARY_BUFFER_DEFAULT_SIZE 512
55 #define MAX_PRINTABLE_SIZE 80
56
57 struct _QfuQdlDevicePrivate {
58 GFile *file;
59 gint fd;
60 guint qdl_version;
61 GByteArray *buffer;
62 GByteArray *secondary_buffer;
63 };
64
65 /******************************************************************************/
66 /* HDLC */
67
68 #define CONTROL 0x7e
69 #define ESCAPE 0x7d
70 #define MASK 0x20
71
72 static gsize
73 escape (const guint8 *in,
74 gsize inlen,
75 guint8 *out,
76 gsize outlen)
77 {
78 gsize i, j;
79
80 for (i = 0, j = 0; i < inlen; i++) {
81 /* Caller should give a big enough buffer */
82 g_assert ((j + 1) < outlen);
83 if (in[i] == CONTROL || in[i] == ESCAPE) {
84 out[j++] = ESCAPE;
85 out[j++] = in[i] ^ MASK;
86 } else
87 out[j++] = in[i];
88 }
89 return j;
90 }
91
92 static gsize
93 unescape (const guint8 *in,
94 gsize inlen,
95 guint8 *out,
96 gsize outlen)
97 {
98 gsize i, j = 0;
99 gboolean escaping = FALSE;
100
101 for (i = 0; i < inlen; i++) {
102 /* Caller should give a big enough buffer */
103 g_assert (j < outlen);
104 if (escaping) {
105 out[j++] = in[i] ^ MASK;
106 escaping = FALSE;
107 } else if (in[i] == ESCAPE) {
108 escaping = TRUE;
109 } else {
110 out[j++] = in[i];
111 }
112 }
113
114 return j;
115 }
116
117 /* copy a possibly escaped single byte to out */
118 static gsize
119 escape_byte (guint8 byte,
120 guint8 *out,
121 gsize outlen)
122 {
123 gsize j = 0;
124
125 if (byte == CONTROL || byte == ESCAPE) {
126 out[j++] = ESCAPE;
127 byte ^= MASK;
128 }
129 out[j++] = byte;
130 return j;
131 }
132
133 static gsize
134 hdlc_max_framed_size (gsize unframed_size)
135 {
136 /* 1 header byte, (2 * input size) bytes, 2 crc bytes and 1 trailing byte */
137 return 4 + (2 * unframed_size);
138 }
139
140 static gsize
141 hdlc_frame (const guint8 *in,
142 gsize inlen,
143 guint8 *out,
144 gsize outlen)
145 {
146 guint16 crc;
147 gsize j = 0;
148
149 out[j++] = CONTROL;
150 j += escape (in, inlen, &out[j], outlen - j);
151 crc = qfu_utils_crc16 (in, inlen);
152 j += escape_byte (crc & 0xff, &out[j], outlen - j);
153 j += escape_byte (crc >> 8 & 0xff, &out[j], outlen - j);
154 out[j++] = CONTROL;
155
156 return j;
157 }
158
159 static gsize
160 hdlc_max_unframed_size (gsize framed_size)
161 {
162 /* -1 header byte, -2 crc bytes and -1 trailing byte */
163 g_assert (framed_size > 3);
164 return framed_size - 3;
165 }
166
167 static gsize
168 hdlc_unframe (const guint8 *in,
169 gsize inlen,
170 guint8 *out,
171 gsize outlen,
172 GError **error)
173 {
174 guint16 crc;
175 gsize j, i = inlen;
176
177 /* the first control char is optional */
178 if (*in == CONTROL) {
179 in++;
180 i--;
181 }
182 if (in[i - 1] == CONTROL)
183 i--;
184
185 j = unescape (in, i, out, outlen);
186 if (j < 2) {
187 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
188 "unescaping failed: too few bytes as output: %" G_GSIZE_FORMAT, j);
189 return 0;
190 }
191 j -= 2; /* remove the crc */
192
193 /* verify the crc */
194 crc = qfu_utils_crc16 (out, j);
195 if (crc != (out[j] | out[j + 1] << 8)) {
196 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
197 "crc check failed: 0x%04x != 0x%04x\n", crc, out[j] | out[j + 1] << 8);
198 return 0;
199 }
200
201 return j;
202 }
203
204 /******************************************************************************/
205 /* Send */
206
207 static gboolean
208 send_request (QfuQdlDevice *self,
209 const guint8 *request,
210 gsize request_size,
211 GCancellable *cancellable,
212 GError **error)
213 {
214 gssize wlen;
215 fd_set wr;
216 gint aux;
217 struct timeval tv = {
218 .tv_sec = 2,
219 .tv_usec = 0,
220 };
221
222 /* Wait for the fd to be writable and don't wait forever */
223 FD_ZERO (&wr);
224 FD_SET (self->priv->fd, &wr);
225 aux = select (self->priv->fd + 1, NULL, &wr, NULL, &tv);
226
227 if (g_cancellable_set_error_if_cancelled (cancellable, error))
228 return FALSE;
229
230 if (aux < 0) {
231 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
232 "error waiting to write: %s",
233 g_strerror (errno));
234 return FALSE;
235 }
236
237 if (aux == 0) {
238 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
239 "timed out waiting to write");
240 return FALSE;
241 }
242
243 /* Debug output */
244 if (qfu_log_get_verbose ()) {
245 gchar *printable;
246 gsize printable_size = request_size;
247 gboolean shorted = FALSE;
248
249 if (printable_size > MAX_PRINTABLE_SIZE) {
250 printable_size = MAX_PRINTABLE_SIZE;
251 shorted = TRUE;
252 }
253
254 printable = qfu_utils_str_hex (request, printable_size, ':');
255 g_debug ("[qfu-qdl-device] >> %s%s [%" G_GSIZE_FORMAT "]", printable, shorted ? "..." : "", request_size);
256 g_free (printable);
257 }
258
259 wlen = write (self->priv->fd, request, request_size);
260 if (wlen < 0) {
261 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
262 "error writting: %s",
263 g_strerror (errno));
264 return FALSE;
265 }
266
267 /* We treat EINTR as an error, so we also treat as an error if not all bytes
268 * were wlen */
269 if ((gsize)wlen != request_size) {
270 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
271 "error writing: only %" G_GSSIZE_FORMAT "/%" G_GSIZE_FORMAT " bytes written",
272 wlen, request_size);
273 return FALSE;
274 }
275
276 return TRUE;
277 }
278
279 static gboolean
280 send_framed_request (QfuQdlDevice *self,
281 const guint8 *request,
282 gsize request_size,
283 GCancellable *cancellable,
284 GError **error)
285 {
286 gsize max_framed_size;
287 gsize framed_size;
288
289 /* Debug output */
290 if (qfu_log_get_verbose ()) {
291 gchar *printable;
292 gsize printable_size = request_size;
293 gboolean shorted = FALSE;
294
295 if (printable_size > MAX_PRINTABLE_SIZE) {
296 printable_size = MAX_PRINTABLE_SIZE;
297 shorted = TRUE;
298 }
299
300 printable = qfu_utils_str_hex (request, printable_size, ':');
301 g_debug ("[qfu-qdl-device] >> %s%s [%" G_GSIZE_FORMAT ", unframed]", printable, shorted ? "..." : "", request_size);
302 g_free (printable);
303 }
304
305 max_framed_size = hdlc_max_framed_size (request_size);
306 if (G_UNLIKELY (max_framed_size > self->priv->secondary_buffer->len))
307 g_byte_array_set_size (self->priv->secondary_buffer, max_framed_size);
308
309 /* Pack into an HDLC frame */
310 framed_size = hdlc_frame (request, request_size, self->priv->secondary_buffer->data, self->priv->secondary_buffer->len);
311 g_assert (framed_size > 0);
312
313 return send_request (self, self->priv->secondary_buffer->data, framed_size, cancellable, error);
314 }
315
316 /******************************************************************************/
317 /* Receive */
318
319 static gssize
320 receive_response (QfuQdlDevice *self,
321 guint timeout_secs,
322 guint8 **response,
323 GCancellable *cancellable,
324 GError **error)
325 {
326 fd_set rd;
327 struct timeval tv;
328 gint aux;
329 gssize rlen;
330 guint8 *end;
331 gssize frame_size;
332 gsize max_unframed_size;
333 gsize unframed_size;
334
335 /* Use requested timeout */
336 tv.tv_sec = timeout_secs;
337 tv.tv_usec = 0;
338
339 FD_ZERO (&rd);
340 FD_SET (self->priv->fd, &rd);
341 aux = select (self->priv->fd + 1, &rd, NULL, NULL, &tv);
342
343 if (g_cancellable_set_error_if_cancelled (cancellable, error))
344 return -1;
345
346 if (aux < 0) {
347 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
348 "error waiting to read response: %s",
349 g_strerror (errno));
350 return -1;
351 }
352
353 if (aux == 0) {
354 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
355 "timed out waiting for the response");
356 return -1;
357 }
358
359 /* Receive in the primary buffer */
360 rlen = read (self->priv->fd, self->priv->buffer->data, self->priv->buffer->len);
361 if (rlen < 0) {
362 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
363 "couldn't read response: %s",
364 g_strerror (errno));
365 return -1;
366 }
367
368 if (rlen == 0) {
369 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
370 "couldn't read response: HUP detected");
371 return -1;
372 }
373
374 /* Debug output */
375 if (qfu_log_get_verbose ()) {
376 gchar *printable;
377 gsize printable_size = rlen;
378 gboolean shorted = FALSE;
379
380 if (printable_size > MAX_PRINTABLE_SIZE) {
381 printable_size = MAX_PRINTABLE_SIZE;
382 shorted = TRUE;
383 }
384
385 printable = qfu_utils_str_hex (self->priv->buffer->data, printable_size, ':');
386 g_debug ("[qfu-qdl-device] << %s%s [%" G_GSIZE_FORMAT "]", printable, shorted ? "..." : "", rlen);
387 g_free (printable);
388 }
389
390 end = memchr (self->priv->buffer->data + 1, CONTROL, rlen - 1);
391 if (!end) {
392 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
393 "HDLC trailing control character not found");
394 return -1;
395 }
396
397 frame_size = end - self->priv->buffer->data + 1;
398 g_assert (frame_size >= 0);
399 g_assert (frame_size <= rlen);
400 if (frame_size < 5) {
401 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
402 "minimum HDLC frame size not received");
403 return -1;
404 }
405
406 if (frame_size < rlen)
407 g_debug ("[qfu-qdl-device] received %" G_GSSIZE_FORMAT " trailing bytes after HDLC frame (ignored)",
408 rlen - frame_size);
409
410 max_unframed_size = hdlc_max_unframed_size (frame_size);
411 if (G_UNLIKELY (max_unframed_size > self->priv->secondary_buffer->len))
412 g_byte_array_set_size (self->priv->secondary_buffer, max_unframed_size);
413
414 unframed_size = hdlc_unframe (self->priv->buffer->data, (gsize)frame_size, self->priv->secondary_buffer->data, self->priv->secondary_buffer->len, error);
415 if (unframed_size == 0) {
416 g_prefix_error (error, "error unframing message: ");
417 return -1;
418 }
419
420 /* Debug output */
421 if (qfu_log_get_verbose ()) {
422 gchar *printable;
423 gsize printable_size = unframed_size;
424 gboolean shorted = FALSE;
425
426 if (printable_size > MAX_PRINTABLE_SIZE) {
427 printable_size = MAX_PRINTABLE_SIZE;
428 shorted = TRUE;
429 }
430
431 printable = qfu_utils_str_hex (self->priv->secondary_buffer->data, printable_size, ':');
432 g_debug ("[qfu-qdl-device] << %s%s [%" G_GSIZE_FORMAT ", unframed]", printable, shorted ? "..." : "", unframed_size);
433 g_free (printable);
434 }
435
436 if (response)
437 *response = self->priv->secondary_buffer->data;
438
439 return unframed_size;
440 }
441
442 /******************************************************************************/
443 /* Send/receive */
444
445 static gssize
446 send_receive (QfuQdlDevice *self,
447 const guint8 *request,
448 gsize request_size,
449 gboolean request_framed,
450 guint response_timeout_secs,
451 guint8 **response,
452 GCancellable *cancellable,
453 GError **error)
454 {
455 gboolean sent;
456
457 if (self->priv->fd < 0) {
458 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "device is closed");
459 return FALSE;
460 }
461
462 if (request_framed)
463 sent = send_framed_request (self, request, request_size, cancellable, error);
464 else
465 sent = send_request (self, request, request_size, cancellable, error);
466 if (!sent)
467 return -1;
468
469 if (!response)
470 return 0;
471
472 return receive_response (self, response_timeout_secs, response, cancellable, error);
473 }
474
475 /******************************************************************************/
476
477 /******************************************************************************/
478
479 gboolean
480 qfu_qdl_device_ufopen (QfuQdlDevice *self,
481 QfuImage *image,
482 GCancellable *cancellable,
483 GError **error)
484 {
485 gssize reqlen;
486 gssize rsplen;
487 guint8 *rsp = NULL;
488
489 reqlen = qfu_qdl_request_ufopen_build (self->priv->buffer->data, self->priv->buffer->len, image, cancellable, error);
490 if (reqlen < 0)
491 return FALSE;
492
493 rsplen = send_receive (self, self->priv->buffer->data, reqlen, TRUE, 1, &rsp, cancellable, error);
494 if (rsplen < 0)
495 return FALSE;
496
497 switch (rsp[0]) {
498 case QFU_QDL_CMD_OPEN_UNFRAMED_RSP:
499 return qfu_qdl_response_ufopen_parse (rsp, rsplen, error);
500 case QFU_QDL_CMD_ERROR:
501 return qfu_qdl_response_error_parse (rsp, rsplen, error);
502 default:
503 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
504 "unexpected response received in ufopen: 0x%02x (%s)",
505 rsp[0], qfu_qdl_cmd_get_string (rsp[0]) ? qfu_qdl_cmd_get_string (rsp[0]) : "unknown");
506 return FALSE;
507 }
508 }
509
510 /******************************************************************************/
511
512 gboolean
513 qfu_qdl_device_ufwrite (QfuQdlDevice *self,
514 QfuImage *image,
515 guint16 sequence,
516 GCancellable *cancellable,
517 GError **error)
518 {
519 gssize reqlen;
520 gssize rsplen;
521 guint8 *rsp = NULL;
522 guint16 ack_sequence = 0;
523
524 reqlen = qfu_qdl_request_ufwrite_build (self->priv->buffer->data, self->priv->buffer->len, image, sequence, cancellable, error);
525 if (reqlen < 0)
526 return FALSE;
527
528 /* NOTE: the last chunk will require a long timeout, so just define the
529 * same one for all chunks */
530 rsplen = send_receive (self, self->priv->buffer->data, reqlen, FALSE, 120, &rsp, cancellable, error);
531 if (rsplen < 0)
532 return FALSE;
533
534 switch (rsp[0]) {
535 case QFU_QDL_CMD_WRITE_UNFRAMED_RSP:
536 if (!qfu_qdl_response_ufwrite_parse (rsp, rsplen, &ack_sequence, error))
537 return FALSE;
538 if (ack_sequence != sequence) {
539 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
540 "received ack for chunk #%" G_GUINT16_FORMAT " instead of chunk #%" G_GUINT16_FORMAT,
541 ack_sequence, sequence);
542 return FALSE;
543 }
544 return TRUE;
545 case QFU_QDL_CMD_ERROR:
546 return qfu_qdl_response_error_parse (rsp, rsplen, error);
547 default:
548 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
549 "unexpected response received in ufwrite: 0x%02x (%s)",
550 rsp[0], qfu_qdl_cmd_get_string (rsp[0]) ? qfu_qdl_cmd_get_string (rsp[0]) : "unknown");
551 return FALSE;
552 }
553 }
554
555 /******************************************************************************/
556
557 gboolean
558 qfu_qdl_device_ufclose (QfuQdlDevice *self,
559 GCancellable *cancellable,
560 GError **error)
561 {
562 gsize reqlen;
563 gssize rsplen;
564 guint8 *rsp = NULL;
565
566 reqlen = qfu_qdl_request_ufclose_build (self->priv->buffer->data, self->priv->buffer->len);
567 rsplen = send_receive (self, self->priv->buffer->data, reqlen, TRUE, 1, &rsp, cancellable, error);
568 if (rsplen < 0)
569 return FALSE;
570
571 switch (rsp[0]) {
572 case QFU_QDL_CMD_CLOSE_UNFRAMED_RSP:
573 return qfu_qdl_response_ufclose_parse (rsp, rsplen, error);
574 case QFU_QDL_CMD_ERROR:
575 return qfu_qdl_response_error_parse (rsp, rsplen, error);
576 default:
577 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
578 "unexpected response received in ufclose: 0x%02x (%s)",
579 rsp[0], qfu_qdl_cmd_get_string (rsp[0]) ? qfu_qdl_cmd_get_string (rsp[0]) : "unknown");
580 return FALSE;
581 }
582 }
583
584 /******************************************************************************/
585
586 gboolean
587 qfu_qdl_device_hello (QfuQdlDevice *self,
588 GCancellable *cancellable,
589 GError **error)
590 {
591 gsize reqlen;
592 gssize rsplen;
593 guint8 *rsp = NULL;
594
595 g_assert (self->priv->qdl_version > 0);
596
597 /* If no error, we assume version is found */
598 reqlen = qfu_qdl_request_hello_build (self->priv->buffer->data, self->priv->buffer->len, self->priv->qdl_version, self->priv->qdl_version);
599 rsplen = send_receive (self, self->priv->buffer->data, reqlen, TRUE, 1, &rsp, cancellable, error);
600 if (rsplen < 0)
601 return FALSE;
602
603 switch (rsp[0]) {
604 case QFU_QDL_CMD_HELLO_RSP:
605 return qfu_qdl_response_hello_parse (rsp, rsplen, error);
606 case QFU_QDL_CMD_ERROR:
607 return qfu_qdl_response_error_parse (rsp, rsplen, error);
608 default:
609 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
610 "unexpected response received in hello: 0x%02x (%s)",
611 rsp[0], qfu_qdl_cmd_get_string (rsp[0]) ? qfu_qdl_cmd_get_string (rsp[0]) : "unknown");
612 return FALSE;
613 }
614 }
615
616 /******************************************************************************/
617
618 gboolean
619 qfu_qdl_device_reset (QfuQdlDevice *self,
620 GCancellable *cancellable,
621 GError **error)
622 {
623 gsize reqlen;
624 gssize rsplen;
625
626 if (self->priv->fd < 0)
627 return TRUE;
628
629 reqlen = qfu_qdl_request_reset_build (self->priv->buffer->data, self->priv->buffer->len);
630 rsplen = send_receive (self, self->priv->buffer->data, reqlen, TRUE, 0, NULL, cancellable, error);
631
632 /* Close device after a reset, even if we got an error */
633 close (self->priv->fd);
634 self->priv->fd = -1;
635
636 if (rsplen < 0)
637 return FALSE;
638
639 return TRUE;
640 }
641
642 /******************************************************************************/
643
644 static gboolean
645 qdl_device_dload_sdp (QfuQdlDevice *self,
646 GCancellable *cancellable,
647 GError **error)
648 {
649 gsize reqlen;
650 gssize rsplen;
651 guint8 *rsp = NULL;
652
653 reqlen = qfu_dload_request_sdp_build (self->priv->buffer->data, self->priv->buffer->len);
654 rsplen = send_receive (self, self->priv->buffer->data, reqlen, TRUE, 1, &rsp, cancellable, error);
655 if (rsplen < 0)
656 return FALSE;
657
658 switch (rsp[0]) {
659 case QFU_DLOAD_CMD_ACK:
660 return qfu_dload_response_ack_parse (rsp, rsplen, error);
661 case QFU_QDL_CMD_ERROR:
662 return qfu_qdl_response_error_parse (rsp, rsplen, error);
663 default:
664 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
665 "unexpected response received in dload sdp: 0x%02x", rsp[0]);
666 return FALSE;
667 }
668
669 return TRUE;
670 }
671
672 static gboolean
673 qdl_device_detect_version (QfuQdlDevice *self,
674 GCancellable *cancellable,
675 GError **error)
676 {
677 guint version;
678
679 #define MAX_VALID_VERSION 6
680
681 /* Attempt to probe supported protocol version
682 * Newer modems like Sierra Wireless MC7710 must use '6' for both fields
683 * Gobi2000 modems like HP un2420 must use '5' for both fields
684 * Gobi1000 modems must use '4' for both fields
685 */
686 for (version = 4; version <= MAX_VALID_VERSION; version++) {
687 gsize reqlen;
688 gssize rsplen;
689 guint8 *rsp = NULL;
690
691 /* If no error, we assume version is found */
692 reqlen = qfu_qdl_request_hello_build (self->priv->buffer->data, self->priv->buffer->len, version, version);
693 rsplen = send_receive (self, self->priv->buffer->data, reqlen, TRUE, 1, &rsp, cancellable, error);
694 if (rsplen < 0)
695 return FALSE;
696
697 /* Break right away on a successful parse, so that we finish with the
698 * correct version tested */
699 if (qfu_qdl_response_hello_parse (rsp, rsplen, NULL))
700 break;
701 }
702
703 if (version > MAX_VALID_VERSION) {
704 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
705 "couldn't detect QDL version");
706 return FALSE;
707 }
708
709 g_debug ("[qfu-qdl-device] QDL version detected: %u", version);
710 self->priv->qdl_version = version;
711
712 return TRUE;
713 }
714
715 static gboolean
716 initable_init (GInitable *initable,
717 GCancellable *cancellable,
718 GError **error)
719 {
720 QfuQdlDevice *self;
721 struct termios terminal_data;
722 gchar *path = NULL;
723 GError *inner_error = NULL;
724
725 self = QFU_QDL_DEVICE (initable);
726
727 if (g_cancellable_set_error_if_cancelled (cancellable, &inner_error))
728 goto out;
729
730 path = g_file_get_path (self->priv->file);
731 g_debug ("[qfu-qdl-device] opening TTY: %s", path);
732
733 self->priv->fd = open (path, O_RDWR | O_NOCTTY);
734 if (self->priv->fd < 0) {
735 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
736 "error opening serial device: %s",
737 g_strerror (errno));
738 goto out;
739 }
740
741 g_debug ("[qfu-qdl-device] setting terminal in raw mode...");
742 if (tcgetattr (self->priv->fd, &terminal_data) < 0) {
743 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
744 "error getting serial port attributes: %s",
745 g_strerror (errno));
746 goto out;
747 }
748 cfmakeraw (&terminal_data);
749 if (tcsetattr (self->priv->fd, TCSANOW, &terminal_data) < 0) {
750 inner_error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
751 "error setting serial port attributes: %s",
752 g_strerror (errno));
753 goto out;
754 }
755
756 if (!qdl_device_dload_sdp (self, cancellable, &inner_error)) {
757 if (!g_error_matches (inner_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
758 goto out;
759
760 g_debug ("[qfu-qdl-device] error (ignored): DLOAD SDP not supported");
761 g_clear_error (&inner_error);
762 }
763
764 if (!qdl_device_detect_version (self, cancellable, &inner_error))
765 goto out;
766
767 out:
768 g_free (path);
769
770 if (inner_error) {
771 if (!(self->priv->fd < 0)) {
772 close (self->priv->fd);
773 self->priv->fd = -1;
774 }
775 g_propagate_error (error, inner_error);
776 return FALSE;
777 }
778
779 return TRUE;
780 }
781
782 /******************************************************************************/
783
784 QfuQdlDevice *
785 qfu_qdl_device_new (GFile *file,
786 GCancellable *cancellable,
787 GError **error)
788 {
789 g_return_val_if_fail (G_IS_FILE (file), NULL);
790
791 return QFU_QDL_DEVICE (g_initable_new (QFU_TYPE_QDL_DEVICE,
792 cancellable,
793 error,
794 "file", file,
795 NULL));
796 }
797
798
799 static void
800 qfu_qdl_device_init (QfuQdlDevice *self)
801 {
802 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, QFU_TYPE_QDL_DEVICE, QfuQdlDevicePrivate);
803 self->priv->fd = -1;
804 /* Long buffer for I/O */
805 self->priv->buffer = g_byte_array_new ();
806 g_byte_array_set_size (self->priv->buffer, QFU_QDL_MESSAGE_MAX_SIZE);
807 /* Shorter secondary buffer for framing/unframing */
808 self->priv->secondary_buffer = g_byte_array_new ();
809 g_byte_array_set_size (self->priv->secondary_buffer, SECONDARY_BUFFER_DEFAULT_SIZE);
810 }
811
812 static void
813 set_property (GObject *object,
814 guint prop_id,
815 const GValue *value,
816 GParamSpec *pspec)
817 {
818 QfuQdlDevice *self = QFU_QDL_DEVICE (object);
819
820 switch (prop_id) {
821 case PROP_FILE:
822 self->priv->file = g_value_dup_object (value);
823 break;
824 default:
825 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
826 break;
827 }
828 }
829
830 static void
831 get_property (GObject *object,
832 guint prop_id,
833 GValue *value,
834 GParamSpec *pspec)
835 {
836 QfuQdlDevice *self = QFU_QDL_DEVICE (object);
837
838 switch (prop_id) {
839 case PROP_FILE:
840 g_value_set_object (value, self->priv->file);
841 break;
842 default:
843 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
844 break;
845 }
846 }
847
848 static void
849 dispose (GObject *object)
850 {
851 QfuQdlDevice *self = QFU_QDL_DEVICE (object);
852
853 if (!(self->priv->fd < 0)) {
854 close (self->priv->fd);
855 self->priv->fd = -1;
856 }
857 g_clear_pointer (&self->priv->buffer, g_byte_array_unref);
858 g_clear_pointer (&self->priv->secondary_buffer, g_byte_array_unref);
859 g_clear_object (&self->priv->file);
860
861 G_OBJECT_CLASS (qfu_qdl_device_parent_class)->dispose (object);
862 }
863
864 static void
865 initable_iface_init (GInitableIface *iface)
866 {
867 iface->init = initable_init;
868 }
869
870 static void
871 qfu_qdl_device_class_init (QfuQdlDeviceClass *klass)
872 {
873 GObjectClass *object_class = G_OBJECT_CLASS (klass);
874
875 g_type_class_add_private (object_class, sizeof (QfuQdlDevicePrivate));
876
877 object_class->dispose = dispose;
878 object_class->get_property = get_property;
879 object_class->set_property = set_property;
880
881 properties[PROP_FILE] =
882 g_param_spec_object ("file",
883 "File",
884 "File object",
885 G_TYPE_FILE,
886 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
887 g_object_class_install_property (object_class, PROP_FILE, properties[PROP_FILE]);
888 }