Debian Codegraph @

src/qmi-firmware-update/qfu-updater.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * qmi-firmware-update -- Command line tool to update firmware in QMI 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 Zodiac Inflight Innovations
19 * Copyright (C) 2016-2017 Aleksander Morgado <[email protected]>
20 */
21
22 #include <config.h>
23 #include <string.h>
24
25 #include <gio/gio.h>
26
27 #include <libqmi-glib.h>
28
29 #include "qfu-log.h"
30 #include "qfu-image-factory.h"
31 #include "qfu-updater.h"
32 #include "qfu-reseter.h"
33 #include "qfu-utils.h"
34 #include "qfu-helpers.h"
35 #include "qfu-qdl-device.h"
36 #include "qfu-sahara-device.h"
37 #include "qfu-enum-types.h"
38
39 G_DEFINE_TYPE (QfuUpdater, qfu_updater, G_TYPE_OBJECT)
40
41 #define CLEAR_LINE "\33[2K\r"
42
43 typedef enum {
44 UPDATER_TYPE_UNKNOWN,
45 UPDATER_TYPE_GENERIC,
46 UPDATER_TYPE_DOWNLOAD,
47 } UpdaterType;
48
49 struct _QfuUpdaterPrivate {
50 UpdaterType type;
51 QfuDeviceSelection *device_selection;
52 gchar *firmware_version;
53 gchar *config_version;
54 gchar *carrier;
55 QmiDeviceOpenFlags device_open_flags;
56 gboolean ignore_version_errors;
57 gboolean override_download;
58 guint8 modem_storage_index;
59 gboolean skip_validation;
60 };
61
62 /******************************************************************************/
63
64 static const gchar *progress[] = {
65 "(*-----)",
66 "(-*----)",
67 "(--*---)",
68 "(---*--)",
69 "(----*-)",
70 "(-----*)",
71 "(----*-)",
72 "(---*--)",
73 "(--*---)",
74 "(-*----)"
75 };
76
77 /******************************************************************************/
78 /* Run */
79
80 /* Wait time after the upgrade has been done, before using the cdc-wdm port */
81 #define WAIT_FOR_BOOT_TIMEOUT_SECS 5
82 #define WAIT_FOR_BOOT_RETRIES 12
83
84 typedef enum {
85 RUN_CONTEXT_STEP_QMI_CLIENT,
86 RUN_CONTEXT_STEP_GET_FIRMWARE_PREFERENCE,
87 RUN_CONTEXT_STEP_SET_FIRMWARE_PREFERENCE,
88 RUN_CONTEXT_STEP_POWER_CYCLE,
89 RUN_CONTEXT_STEP_CLEANUP_QMI_DEVICE,
90 RUN_CONTEXT_STEP_WAIT_FOR_TTY,
91 RUN_CONTEXT_STEP_SELECT_DEVICE,
92 RUN_CONTEXT_STEP_SELECT_IMAGE,
93 RUN_CONTEXT_STEP_DOWNLOAD_IMAGE,
94 RUN_CONTEXT_STEP_CLEANUP_IMAGE,
95 RUN_CONTEXT_STEP_CLEANUP_DEVICE,
96 RUN_CONTEXT_STEP_WAIT_FOR_CDC_WDM,
97 RUN_CONTEXT_STEP_WAIT_FOR_BOOT,
98 RUN_CONTEXT_STEP_QMI_CLIENT_AFTER,
99 RUN_CONTEXT_STEP_CLEANUP_QMI_DEVICE_FULL,
100 RUN_CONTEXT_STEP_LAST
101 } RunContextStep;
102
103 typedef struct {
104 /* Device selection */
105 GFile *cdc_wdm_file;
106 GFile *serial_file;
107
108 /* Context step */
109 RunContextStep step;
110
111 /* Old/New info and capabilities */
112 gchar *revision;
113 gboolean supports_stored_image_management;
114 guint8 max_modem_storage_index;
115 gboolean supports_firmware_preference_management;
116 QmiMessageDmsGetFirmwarePreferenceOutput *firmware_preference;
117 QmiMessageDmsSwiGetCurrentFirmwareOutput *current_firmware;
118 gchar *new_revision;
119 gboolean new_supports_stored_image_management;
120 gboolean new_supports_firmware_preference_management;
121 QmiMessageDmsGetFirmwarePreferenceOutput *new_firmware_preference;
122 QmiMessageDmsSwiGetCurrentFirmwareOutput *new_current_firmware;
123
124 /* List of pending QfuImages to download, and the current one being
125 * processed. */
126 GList *pending_images;
127 QfuImage *current_image;
128
129 /* QMI device and client */
130 QmiDevice *qmi_device;
131 QmiClientDms *qmi_client;
132
133 /* Reset configuration */
134 gboolean boothold_reset;
135
136 /* Information gathered from the firmware images themselves */
137 gchar *firmware_version;
138 gchar *config_version;
139 gchar *carrier;
140
141 /* Waiting for boot */
142 guint wait_for_boot_seconds_elapsed;
143 guint wait_for_boot_retries;
144
145 /* Device to use while already in download mode */
146 QfuQdlDevice *qdl_device;
147 QfuSaharaDevice *sahara_device;
148 } RunContext;
149
150 static void
151 run_context_free (RunContext *ctx)
152 {
153 if (ctx->current_firmware)
154 qmi_message_dms_swi_get_current_firmware_output_unref (ctx->current_firmware);
155 if (ctx->new_current_firmware)
156 qmi_message_dms_swi_get_current_firmware_output_unref (ctx->new_current_firmware);
157 if (ctx->firmware_preference)
158 qmi_message_dms_get_firmware_preference_output_unref (ctx->firmware_preference);
159 if (ctx->new_firmware_preference)
160 qmi_message_dms_get_firmware_preference_output_unref (ctx->new_firmware_preference);
161 g_free (ctx->new_revision);
162 g_free (ctx->revision);
163 g_free (ctx->firmware_version);
164 g_free (ctx->config_version);
165 g_free (ctx->carrier);
166
167 if (ctx->qmi_client) {
168 g_assert (ctx->qmi_device);
169 /* This release only happens when cleaning up from an error,
170 * therefore always release CID */
171 qmi_device_release_client (ctx->qmi_device,
172 QMI_CLIENT (ctx->qmi_client),
173 QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID,
174 10, NULL, NULL, NULL);
175 g_object_unref (ctx->qmi_client);
176 }
177 if (ctx->qmi_device) {
178 qmi_device_close_async (ctx->qmi_device, 10, NULL, NULL, NULL);
179 g_object_unref (ctx->qmi_device);
180 }
181 if (ctx->cdc_wdm_file)
182 g_object_unref (ctx->cdc_wdm_file);
183
184 g_clear_object (&ctx->qdl_device);
185 g_clear_object (&ctx->sahara_device);
186 g_clear_object (&ctx->serial_file);
187 g_clear_object (&ctx->current_image);
188 g_list_free_full (ctx->pending_images, g_object_unref);
189 g_slice_free (RunContext, ctx);
190 }
191
192 gboolean
193 qfu_updater_run_finish (QfuUpdater *self,
194 GAsyncResult *res,
195 GError **error)
196 {
197 return g_task_propagate_boolean (G_TASK (res), error);
198 }
199
200 static void
201 print_firmware_preference (QmiMessageDmsGetFirmwarePreferenceOutput *firmware_preference,
202 const gchar *prefix)
203 {
204 GArray *array;
205 guint i;
206
207 qmi_message_dms_get_firmware_preference_output_get_list (firmware_preference, &array, NULL);
208
209 if (array->len > 0) {
210 for (i = 0; i < array->len; i++) {
211 QmiMessageDmsGetFirmwarePreferenceOutputListImage *image;
212 gchar *unique_id_str;
213
214 image = &g_array_index (array, QmiMessageDmsGetFirmwarePreferenceOutputListImage, i);
215 unique_id_str = qfu_utils_get_firmware_image_unique_id_printable (image->unique_id);
216 g_print ("%simage '%s': unique id '%s', build id '%s'\n",
217 prefix,
218 qmi_dms_firmware_image_type_get_string (image->type),
219 unique_id_str,
220 image->build_id);
221 g_free (unique_id_str);
222 }
223 } else
224 g_debug ("%sno preference specified\n", prefix);
225 }
226
227 static void
228 print_current_firmware (QmiMessageDmsSwiGetCurrentFirmwareOutput *current_firmware,
229 const gchar *prefix)
230 {
231 const gchar *model = NULL;
232 const gchar *boot_version = NULL;
233 const gchar *amss_version = NULL;
234 const gchar *sku_id = NULL;
235 const gchar *package_id = NULL;
236 const gchar *carrier_id = NULL;
237 const gchar *pri_version = NULL;
238 const gchar *carrier = NULL;
239 const gchar *config_version = NULL;
240
241 qmi_message_dms_swi_get_current_firmware_output_get_model (current_firmware, &model, NULL);
242 qmi_message_dms_swi_get_current_firmware_output_get_boot_version (current_firmware, &boot_version, NULL);
243 qmi_message_dms_swi_get_current_firmware_output_get_amss_version (current_firmware, &amss_version, NULL);
244 qmi_message_dms_swi_get_current_firmware_output_get_sku_id (current_firmware, &sku_id, NULL);
245 qmi_message_dms_swi_get_current_firmware_output_get_package_id (current_firmware, &package_id, NULL);
246 qmi_message_dms_swi_get_current_firmware_output_get_carrier_id (current_firmware, &carrier_id, NULL);
247 qmi_message_dms_swi_get_current_firmware_output_get_pri_version (current_firmware, &pri_version, NULL);
248 qmi_message_dms_swi_get_current_firmware_output_get_carrier (current_firmware, &carrier, NULL);
249 qmi_message_dms_swi_get_current_firmware_output_get_config_version (current_firmware, &config_version, NULL);
250
251 if (model)
252 g_print ("%sModel: %s\n", prefix, model);
253 if (boot_version)
254 g_print ("%sBoot version: %s\n", prefix, boot_version);
255 if (amss_version)
256 g_print ("%sAMSS version: %s\n", prefix, amss_version);
257 if (sku_id)
258 g_print ("%sSKU ID: %s\n", prefix, sku_id);
259 if (package_id)
260 g_print ("%sPackage ID: %s\n", prefix, package_id);
261 if (carrier_id)
262 g_print ("%sCarrier ID: %s\n", prefix, carrier_id);
263 if (config_version)
264 g_print ("%sConfig version: %s\n", prefix, config_version);
265 }
266
267 static void
268 run_context_step_last (GTask *task)
269 {
270 RunContext *ctx;
271 QfuUpdater *self;
272
273 ctx = (RunContext *) g_task_get_task_data (task);
274 g_assert (ctx);
275
276 self = g_task_get_source_object (task);
277
278 if (self->priv->type == UPDATER_TYPE_DOWNLOAD) {
279 g_task_return_boolean (task, TRUE);
280 g_object_unref (task);
281 return;
282 }
283
284 if (self->priv->type == UPDATER_TYPE_GENERIC) {
285 g_print ("\n"
286 "------------------------------------------------------------------------\n");
287
288 g_print ("\n"
289 " original firmware revision was:\n"
290 " %s\n", ctx->revision ? ctx->revision : "unknown");
291 if (ctx->current_firmware) {
292 g_print (" original running firmware details:\n");
293 print_current_firmware (ctx->current_firmware, " ");
294 }
295 if (ctx->firmware_preference) {
296 g_print (" original firmware preference details:\n");
297 print_firmware_preference (ctx->firmware_preference, " ");
298 }
299
300 g_print ("\n"
301 " new firmware revision is:\n"
302 " %s\n", ctx->new_revision ? ctx->new_revision : "unknown");
303 if (ctx->new_current_firmware) {
304 g_print (" new running firmware details:\n");
305 print_current_firmware (ctx->new_current_firmware, " ");
306 }
307 if (ctx->new_firmware_preference) {
308 g_print (" new firmware preference details:\n");
309 print_firmware_preference (ctx->new_firmware_preference, " ");
310 }
311
312 if (ctx->new_supports_stored_image_management)
313 g_print ("\n"
314 " NOTE: this device supports stored image management\n"
315 " with qmicli operations:\n"
316 " --dms-list-stored-images\n"
317 " --dms-select-stored-image\n"
318 " --dms-delete-stored-image\n");
319
320 if (ctx->new_supports_firmware_preference_management)
321 g_print ("\n"
322 " NOTE: this device supports firmware preference management\n"
323 " with qmicli operations:\n"
324 " --dms-get-firmware-preference\n"
325 " --dms-set-firmware-preference\n");
326
327 g_print ("\n"
328 "------------------------------------------------------------------------\n"
329 "\n");
330
331 g_task_return_boolean (task, TRUE);
332 g_object_unref (task);
333 return;
334 }
335
336 g_assert_not_reached ();
337 }
338
339 static void run_context_step (GTask *task);
340
341 static gboolean
342 run_context_step_cb (GTask *task)
343 {
344 run_context_step (task);
345 return FALSE;
346 }
347
348 static void
349 run_context_step_next (GTask *task, RunContextStep next)
350 {
351 RunContext *ctx;
352
353 ctx = (RunContext *) g_task_get_task_data (task);
354 ctx->step = next;
355
356 /* Schedule next step in an idle */
357 g_idle_add ((GSourceFunc) run_context_step_cb, task);
358 }
359
360 static void
361 run_context_step_next_no_idle (GTask *task, RunContextStep next)
362 {
363 RunContext *ctx;
364
365 ctx = (RunContext *) g_task_get_task_data (task);
366 ctx->step = next;
367
368 /* Run right away */
369 run_context_step (task);
370 }
371
372 static void
373 close_ready (QmiDevice *dev,
374 GAsyncResult *res,
375 GTask *task)
376 {
377 RunContext *ctx;
378 GError *error = NULL;
379
380 ctx = (RunContext *) g_task_get_task_data (task);
381
382 if (!qmi_device_close_finish (dev, res, &error)) {
383 g_warning ("[qfu-updater] couldn't close device: %s", error->message);
384 g_error_free (error);
385 } else
386 g_debug ("[qfu-updater] closed");
387
388 /* Go on */
389 run_context_step_next (task, ctx->step + 1);
390 }
391
392 static void
393 release_client_ready (QmiDevice *dev,
394 GAsyncResult *res,
395 GTask *task)
396 {
397 RunContext *ctx;
398 GError *error = NULL;
399
400 ctx = (RunContext *) g_task_get_task_data (task);
401
402 if (!qmi_device_release_client_finish (dev, res, &error)) {
403 g_warning ("[qfu-updater] couldn't release client: %s", error->message);
404 g_error_free (error);
405 } else
406 g_debug ("[qfu-updater] client released");
407
408 qmi_device_close_async (ctx->qmi_device,
409 10,
410 g_task_get_cancellable (task),
411 (GAsyncReadyCallback) close_ready,
412 task);
413 g_clear_object (&ctx->qmi_device);
414 }
415
416 static void
417 run_context_step_cleanup_qmi_device_full (GTask *task)
418 {
419 RunContext *ctx;
420
421 ctx = (RunContext *) g_task_get_task_data (task);
422
423 qmi_device_release_client (ctx->qmi_device,
424 QMI_CLIENT (ctx->qmi_client),
425 QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID,
426 10,
427 g_task_get_cancellable (task),
428 (GAsyncReadyCallback) release_client_ready,
429 task);
430 g_clear_object (&ctx->qmi_client);
431 }
432
433 static void
434 new_client_dms_after_ready (gpointer unused,
435 GAsyncResult *res,
436 GTask *task)
437 {
438 RunContext *ctx;
439 GError *error = NULL;
440
441 ctx = (RunContext *) g_task_get_task_data (task);
442
443 g_assert (!ctx->qmi_device);
444 g_assert (!ctx->qmi_client);
445
446 /* After the upgrade, non-fatal error if DMS client couldn't be created */
447 if (!qfu_utils_new_client_dms_finish (res,
448 &ctx->qmi_device,
449 &ctx->qmi_client,
450 &ctx->new_revision,
451 &ctx->new_supports_stored_image_management,
452 NULL, /* we don't care about the max number of images */
453 &ctx->new_supports_firmware_preference_management,
454 &ctx->new_firmware_preference,
455 &ctx->new_current_firmware,
456 &error)) {
457 if (ctx->wait_for_boot_retries == WAIT_FOR_BOOT_RETRIES) {
458 g_warning ("couldn't create DMS client after upgrade: %s", error->message);
459 run_context_step_next (task, ctx->step + 1);
460 } else {
461 g_debug ("couldn't create DMS client after upgrade: %s (will retry)", error->message);
462 run_context_step_next (task, RUN_CONTEXT_STEP_WAIT_FOR_BOOT);
463 }
464 g_error_free (error);
465 return;
466 }
467
468 /* Go on */
469 run_context_step_next (task, ctx->step + 1);
470 }
471
472 static void
473 run_context_step_qmi_client_after (GTask *task)
474 {
475 RunContext *ctx;
476 QfuUpdater *self;
477
478 ctx = (RunContext *) g_task_get_task_data (task);
479 self = g_task_get_source_object (task);
480
481 ctx->wait_for_boot_retries++;
482 g_print ("loading device information after the update (%u/%u)...\n",
483 ctx->wait_for_boot_retries, WAIT_FOR_BOOT_RETRIES);
484
485 g_debug ("[qfu-updater] creating QMI DMS client after upgrade...");
486 g_assert (ctx->cdc_wdm_file);
487 qfu_utils_new_client_dms (ctx->cdc_wdm_file,
488 1, /* single try to allocate DMS client */
489 self->priv->device_open_flags,
490 TRUE,
491 g_task_get_cancellable (task),
492 (GAsyncReadyCallback) new_client_dms_after_ready,
493 task);
494 }
495
496 static gboolean
497 wait_for_boot_ready (GTask *task)
498 {
499 RunContext *ctx;
500
501 ctx = (RunContext *) g_task_get_task_data (task);
502 ctx->wait_for_boot_seconds_elapsed++;
503
504 if (ctx->wait_for_boot_seconds_elapsed < WAIT_FOR_BOOT_TIMEOUT_SECS) {
505 if (!qfu_log_get_verbose_stdout ())
506 g_print (CLEAR_LINE "%s %u",
507 progress[ctx->wait_for_boot_seconds_elapsed % G_N_ELEMENTS (progress)],
508 WAIT_FOR_BOOT_TIMEOUT_SECS - ctx->wait_for_boot_seconds_elapsed);
509 return G_SOURCE_CONTINUE;
510 }
511
512 if (!qfu_log_get_verbose_stdout ())
513 g_print (CLEAR_LINE);
514
515 /* Go on */
516 run_context_step_next (task, ctx->step + 1);
517 return G_SOURCE_REMOVE;
518 }
519
520 static void
521 run_context_step_wait_for_boot (GTask *task)
522 {
523 RunContext *ctx;
524
525 ctx = (RunContext *) g_task_get_task_data (task);
526 ctx->wait_for_boot_seconds_elapsed = 0;
527
528 g_debug ("[qfu-updater] waiting some time (%us) before accessing the cdc-wdm device...",
529 WAIT_FOR_BOOT_TIMEOUT_SECS);
530
531 if (!qfu_log_get_verbose_stdout ()) {
532 g_print ("waiting some time for the device to boot...\n");
533 g_print ("%s %u", progress[0], WAIT_FOR_BOOT_TIMEOUT_SECS);
534 }
535
536 g_timeout_add_seconds (1, (GSourceFunc) wait_for_boot_ready, task);
537 }
538
539 static void
540 wait_for_cdc_wdm_ready (QfuDeviceSelection *device_selection,
541 GAsyncResult *res,
542 GTask *task)
543 {
544 GError *error = NULL;
545 RunContext *ctx;
546 QfuUpdater *self;
547 gchar *path;
548
549 ctx = (RunContext *) g_task_get_task_data (task);
550 self = g_task_get_source_object (task);
551
552 g_assert (!ctx->cdc_wdm_file);
553 ctx->cdc_wdm_file = qfu_device_selection_wait_for_cdc_wdm_finish (device_selection, res, &error);
554 if (!ctx->cdc_wdm_file) {
555 g_prefix_error (&error, "error waiting for cdc-wdm: ");
556 g_task_return_error (task, error);
557 g_object_unref (task);
558 return;
559 }
560
561 path = g_file_get_path (ctx->cdc_wdm_file);
562 g_debug ("[qfu-updater] cdc-wdm device found: %s", path);
563 g_free (path);
564
565 g_print ("normal mode detected\n");
566
567 /* If no need to validate, we're done */
568 if (self->priv->skip_validation) {
569 run_context_step_next (task, RUN_CONTEXT_STEP_LAST);
570 return;
571 }
572
573 g_print ("\n"
574 "------------------------------------------------------------------------\n"
575 " NOTE: in order to validate which is the firmware running in the\n"
576 " module, the program will wait for a complete boot; this process\n"
577 " may take some time and several retries.\n"
578 "------------------------------------------------------------------------\n"
579 "\n");
580
581 /* Go on */
582 run_context_step_next (task, ctx->step + 1);
583 }
584
585 static void
586 run_context_step_wait_for_cdc_wdm (GTask *task)
587 {
588 QfuUpdater *self;
589
590 self = g_task_get_source_object (task);
591
592 g_debug ("[qfu-updater] now waiting for cdc-wdm device...");
593
594 qfu_device_selection_wait_for_cdc_wdm (self->priv->device_selection,
595 g_task_get_cancellable (task),
596 (GAsyncReadyCallback) wait_for_cdc_wdm_ready,
597 task);
598 }
599
600 static void
601 run_context_step_cleanup_device (GTask *task)
602 {
603 RunContext *ctx;
604 QfuUpdater *self;
605
606 ctx = (RunContext *) g_task_get_task_data (task);
607 self = g_task_get_source_object (task);
608
609 g_assert (ctx->serial_file);
610
611 if (ctx->qdl_device) {
612 g_debug ("[qfu-updater] QDL reset");
613 qfu_qdl_device_reset (ctx->qdl_device, g_task_get_cancellable (task), NULL);
614 g_clear_object (&ctx->qdl_device);
615 } else if (ctx->sahara_device) {
616 g_debug ("[qfu-updater] firehose reset");
617 qfu_sahara_device_firehose_reset (ctx->sahara_device, g_task_get_cancellable (task), NULL);
618 g_clear_object (&ctx->sahara_device);
619 } else
620 g_assert_not_reached ();
621
622 g_clear_object (&ctx->serial_file);
623
624 g_print ("rebooting in normal mode...\n");
625
626 /* If we were running in download mode, we don't even wait for the reboot to finish */
627 if (self->priv->type == UPDATER_TYPE_DOWNLOAD) {
628 run_context_step_next (task, RUN_CONTEXT_STEP_LAST);
629 return;
630 }
631
632 run_context_step_next (task, ctx->step + 1);
633 }
634
635 static void
636 run_context_step_cleanup_image (GTask *task)
637 {
638 RunContext *ctx;
639
640 ctx = (RunContext *) g_task_get_task_data (task);
641
642 g_assert (ctx->current_image);
643 g_clear_object (&ctx->current_image);
644
645 /* Select next image */
646 if (ctx->pending_images) {
647 run_context_step_next (task, RUN_CONTEXT_STEP_SELECT_IMAGE);
648 return;
649 }
650
651 /* If no more files to download, we're done! */
652 g_debug ("[qfu-updater] no more files to download");
653 run_context_step_next (task, ctx->step + 1);
654 }
655
656 static gboolean
657 download_image_firehose (QfuSaharaDevice *device,
658 QfuImage *image,
659 GCancellable *cancellable,
660 GError **error)
661 {
662 guint sequence;
663 guint n_blocks;
664
665 if (!qfu_sahara_device_firehose_setup_download (device, image, &n_blocks, cancellable, error)) {
666 g_prefix_error (error, "couldn't prepare download: ");
667 return FALSE;
668 }
669
670 for (sequence = 0; sequence < n_blocks; sequence++) {
671 if (!qfu_log_get_verbose_stdout ()) {
672 if (n_blocks > 1) {
673 g_print (CLEAR_LINE "%s %04.1lf%%",
674 progress[sequence % G_N_ELEMENTS (progress)],
675 100.0 * ((gdouble) sequence / (gdouble) (n_blocks - 1)));
676 }
677 }
678 if (!qfu_sahara_device_firehose_write_block (device, image, sequence, cancellable, error)) {
679 g_prefix_error (error, "couldn't write in session: ");
680 return FALSE;
681 }
682 }
683
684 g_debug ("[qfu-updater] all blocks downloaded");
685
686 if (!qfu_log_get_verbose_stdout ())
687 g_print (CLEAR_LINE "finalizing download... (may take several minutes, be patient)\n");
688
689 if (!qfu_sahara_device_firehose_teardown_download (device, image, cancellable, error)) {
690 g_prefix_error (error, "couldn't teardown download: ");
691 return FALSE;
692 }
693
694 if (!qfu_log_get_verbose_stdout ())
695 g_print (CLEAR_LINE);
696
697 g_debug ("[qfu-updater] sahara/firehose download finished");
698 return TRUE;
699 }
700
701 static gboolean
702 download_image_qdl (QfuQdlDevice *device,
703 QfuImage *image,
704 GCancellable *cancellable,
705 GError **error)
706 {
707 guint16 sequence;
708 guint16 n_chunks;
709
710 if (!qfu_qdl_device_hello (device, cancellable, error)) {
711 g_prefix_error (error, "couldn't send greetings to device: ");
712 return FALSE;
713 }
714
715 if (!qfu_qdl_device_ufopen (device, image, cancellable, error)) {
716 g_prefix_error (error, "couldn't open session: ");
717 return FALSE;
718 }
719
720 n_chunks = qfu_image_get_n_data_chunks (image);
721 for (sequence = 0; sequence < n_chunks; sequence++) {
722 if (!qfu_log_get_verbose_stdout ()) {
723 /* Use n-1 chunks for progress reporting; because the last one will take
724 * a lot longer. */
725 if (n_chunks > 1 && sequence < (n_chunks - 1))
726 g_print (CLEAR_LINE "%s %04.1lf%%",
727 progress[sequence % G_N_ELEMENTS (progress)],
728 100.0 * ((gdouble) sequence / (gdouble) (n_chunks - 1)));
729 else if (sequence == (n_chunks - 1))
730 g_print (CLEAR_LINE "finalizing download... (may take more than one minute, be patient)\n");
731 }
732 if (!qfu_qdl_device_ufwrite (device, image, sequence, cancellable, error)) {
733 g_prefix_error (error, "couldn't write in session: ");
734 return FALSE;
735 }
736 }
737
738 g_debug ("[qfu-updater] all chunks ack-ed");
739
740 if (!qfu_log_get_verbose_stdout ())
741 g_print (CLEAR_LINE);
742
743 if (!qfu_qdl_device_ufclose (device, cancellable, error)) {
744 g_prefix_error (error, "couldn't close session: ");
745 return FALSE;
746 }
747
748 g_debug ("[qfu-updater] qdl/sdp download finished");
749 return TRUE;
750 }
751
752 static void
753 run_context_step_download_image (GTask *task)
754 {
755 RunContext *ctx;
756 GError *error = NULL;
757 GCancellable *cancellable;
758 GTimer *timer;
759 gdouble elapsed;
760 gchar *aux;
761
762 ctx = (RunContext *) g_task_get_task_data (task);
763 cancellable = g_task_get_cancellable (task);
764
765 timer = g_timer_new ();
766
767 aux = g_format_size ((guint64) qfu_image_get_size (ctx->current_image));
768 g_print ("downloading %s image: %s (%s)...\n",
769 qfu_image_type_get_string (qfu_image_get_image_type (ctx->current_image)),
770 qfu_image_get_display_name (ctx->current_image),
771 aux);
772 g_free (aux);
773
774 /* QDL/SDP based download */
775 if (ctx->qdl_device)
776 download_image_qdl (ctx->qdl_device,
777 ctx->current_image,
778 cancellable,
779 &error);
780 /* Sahara based download */
781 else if (ctx->sahara_device)
782 download_image_firehose (ctx->sahara_device,
783 ctx->current_image,
784 cancellable,
785 &error);
786 else
787 g_assert_not_reached ();
788
789 elapsed = g_timer_elapsed (timer, NULL);
790
791 g_timer_destroy (timer);
792
793 if (error) {
794 g_prefix_error (&error, "error downloading image: ");
795 g_task_return_error (task, error);
796 return;
797 }
798
799 aux = g_format_size ((guint64) ((qfu_image_get_size (ctx->current_image)) / elapsed));
800 g_print ("successfully downloaded in %.2lfs (%s/s)\n", elapsed, aux);
801 g_free (aux);
802
803 /* Go on */
804 run_context_step_next (task, ctx->step + 1);
805 }
806
807 static void
808 run_context_step_select_image (GTask *task)
809 {
810 RunContext *ctx;
811
812 ctx = (RunContext *) g_task_get_task_data (task);
813
814 g_assert (!ctx->current_image);
815 g_assert (ctx->pending_images);
816
817 /* Select new current image */
818 ctx->current_image = QFU_IMAGE (ctx->pending_images->data);
819 ctx->pending_images = g_list_delete_link (ctx->pending_images, ctx->pending_images);
820
821 g_debug ("[qfu-updater] selected file '%s' (%" G_GOFFSET_FORMAT " bytes)",
822 qfu_image_get_display_name (ctx->current_image),
823 qfu_image_get_size (ctx->current_image));
824
825 /* Go on */
826 run_context_step_next (task, ctx->step + 1);
827 }
828
829 static void
830 run_context_step_select_device (GTask *task)
831 {
832 RunContext *ctx;
833 GError *error = NULL;
834
835 ctx = (RunContext *) g_task_get_task_data (task);
836
837 g_assert (ctx->serial_file);
838 g_assert (!ctx->qdl_device);
839 g_assert (!ctx->sahara_device);
840
841 /* Check if we can setup a Sahara device. Always check this first, because
842 * the sahara stack is very sensitive to any kind of data sent to the port. */
843 ctx->sahara_device = qfu_sahara_device_new (ctx->serial_file, g_task_get_cancellable (task), &error);
844 if (!ctx->sahara_device) {
845 g_debug ("[qfu-updater] sahara device creation failed: %s", error->message);
846 g_clear_error (&error);
847
848 /* Check if we can setup a QDL device */
849 ctx->qdl_device = qfu_qdl_device_new (ctx->serial_file, g_task_get_cancellable (task), &error);
850 if (!ctx->qdl_device) {
851 g_debug ("[qfu-updater] qdl device creation failed: %s", error->message);
852 g_clear_error (&error);
853 }
854 }
855
856 if (!ctx->qdl_device && !ctx->sahara_device) {
857 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "unsupported download protocol");
858 g_object_unref (task);
859 return;
860 }
861
862 run_context_step_next (task, ctx->step + 1);
863 }
864
865 static void
866 wait_for_tty_ready (QfuDeviceSelection *device_selection,
867 GAsyncResult *res,
868 GTask *task)
869 {
870 GError *error = NULL;
871 RunContext *ctx;
872 gchar *path;
873
874 ctx = (RunContext *) g_task_get_task_data (task);
875
876 g_assert (!ctx->serial_file);
877 ctx->serial_file = qfu_device_selection_wait_for_tty_finish (device_selection, res, &error);
878 if (!ctx->serial_file) {
879 g_prefix_error (&error, "error waiting for serial device in download mode: ");
880 g_task_return_error (task, error);
881 g_object_unref (task);
882 return;
883 }
884
885 path = g_file_get_path (ctx->serial_file);
886 g_debug ("[qfu-updater] serial device in download mode found: %s", path);
887 g_free (path);
888
889 g_print ("download mode detected\n");
890
891 /* Go on */
892 run_context_step_next (task, ctx->step + 1);
893 }
894
895 static void
896 run_context_step_wait_for_tty (GTask *task)
897 {
898 QfuUpdater *self;
899
900 self = g_task_get_source_object (task);
901
902 g_print ("rebooting in download mode...\n");
903
904 g_debug ("[qfu-updater] reset requested, now waiting for serial device...");
905 qfu_device_selection_wait_for_tty (self->priv->device_selection,
906 QFU_HELPERS_DEVICE_MODE_DOWNLOAD,
907 g_task_get_cancellable (task),
908 (GAsyncReadyCallback) wait_for_tty_ready,
909 task);
910 }
911
912 static void
913 run_context_step_cleanup_qmi_device (GTask *task)
914 {
915 RunContext *ctx;
916 QmiDevice *tmp;
917
918 ctx = (RunContext *) g_task_get_task_data (task);
919
920 g_debug ("[qfu-updater] cleaning up QMI device...");
921
922 /* We don't release CID as we're going to reset anyway */
923 qmi_device_release_client (ctx->qmi_device,
924 QMI_CLIENT (ctx->qmi_client),
925 QMI_DEVICE_RELEASE_CLIENT_FLAGS_NONE,
926 10, NULL, NULL, NULL);
927 g_clear_object (&ctx->qmi_client);
928
929 /* We want to close and unref the QmiDevice only AFTER having set the wait
930 * tasks for cdc-wdm or tty devices. This is because the close operation may
931 * take a long time if doing QMI over MBIM (as the MBIM close async
932 * operation is run internally). If we don't do this in this sequence, we
933 * may end up getting udev events reported before the wait have started. */
934 tmp = g_object_ref (ctx->qmi_device);
935
936 g_clear_object (&ctx->qmi_device);
937 g_clear_object (&ctx->cdc_wdm_file);
938
939 /* If nothing to download we don't need to wait for QDL download mode */
940 if (!ctx->pending_images)
941 run_context_step_next_no_idle (task, RUN_CONTEXT_STEP_WAIT_FOR_CDC_WDM);
942 else
943 /* Go on */
944 run_context_step_next_no_idle (task, ctx->step + 1);
945
946 /* After the wait operation has been started, we do run the close */
947 qmi_device_close_async (tmp, 10, NULL, NULL, NULL);
948 g_object_unref (tmp);
949 }
950
951 static void
952 reseter_run_ready (QfuReseter *reseter,
953 GAsyncResult *res,
954 GTask *task)
955 {
956 GError *error = NULL;
957 RunContext *ctx;
958
959 ctx = (RunContext *) g_task_get_task_data (task);
960
961 if (!qfu_reseter_run_finish (reseter, res, &error)) {
962 g_prefix_error (&error, "boothold reseter operation failed: ");
963 g_task_return_error (task, error);
964 g_object_unref (task);
965 return;
966 }
967
968 g_debug ("[qfu-updater] boothold reset requested successfully...");
969
970 /* Go on */
971 run_context_step_next (task, ctx->step + 1);
972 }
973
974 static void
975 power_cycle_ready (QmiClientDms *qmi_client,
976 GAsyncResult *res,
977 GTask *task)
978 {
979 GError *error = NULL;
980 RunContext *ctx;
981
982 ctx = (RunContext *) g_task_get_task_data (task);
983
984 if (!qfu_utils_power_cycle_finish (qmi_client, res, &error)) {
985 g_task_return_error (task, error);
986 g_object_unref (task);
987 return;
988 }
989
990 g_debug ("[qfu-updater] reset requested successfully...");
991
992 /* Go on */
993 run_context_step_next (task, ctx->step + 1);
994 }
995
996 static void
997 run_context_step_power_cycle (GTask *task)
998 {
999 RunContext *ctx;
1000 QfuUpdater *self;
1001 QfuReseter *reseter;
1002
1003 ctx = (RunContext *) g_task_get_task_data (task);
1004 self = g_task_get_source_object (task);
1005
1006 g_debug ("[qfu-updater] power cycling...");
1007 if (!ctx->boothold_reset) {
1008 qfu_utils_power_cycle (ctx->qmi_client,
1009 g_task_get_cancellable (task),
1010 (GAsyncReadyCallback) power_cycle_ready,
1011 task);
1012 return;
1013 }
1014
1015 /* Boothold is required when firmware preference isn't supported; and if so,
1016 * there must always be images to download. The only case in which we don't
1017 * have images listed for download is when we're told that there is nothing
1018 * to download via firmware preference. */
1019 g_assert (ctx->pending_images != NULL);
1020
1021 /* Boothold reset */
1022 reseter = qfu_reseter_new (self->priv->device_selection, ctx->qmi_client, self->priv->device_open_flags);
1023 qfu_reseter_run (reseter,
1024 g_task_get_cancellable (task),
1025 (GAsyncReadyCallback) reseter_run_ready,
1026 task);
1027 g_object_unref (reseter);
1028 }
1029
1030 static void
1031 set_firmware_preference_ready (QmiClientDms *client,
1032 GAsyncResult *res,
1033 GTask *task)
1034 {
1035 RunContext *ctx;
1036 QmiMessageDmsSetFirmwarePreferenceOutput *output;
1037 GError *error = NULL;
1038 GArray *array = NULL;
1039 guint next_step;
1040
1041 ctx = (RunContext *) g_task_get_task_data (task);
1042
1043 output = qmi_client_dms_set_firmware_preference_finish (client, res, &error);
1044 if (!output) {
1045 g_prefix_error (&error, "QMI operation failed, couldn't set firmware preference: ");
1046 g_task_return_error (task, error);
1047 g_object_unref (task);
1048 return;
1049 }
1050
1051 if (!qmi_message_dms_set_firmware_preference_output_get_result (output, &error)) {
1052 g_prefix_error (&error, "couldn't set firmware preference: ");
1053 g_task_return_error (task, error);
1054 g_object_unref (task);
1055 qmi_message_dms_set_firmware_preference_output_unref (output);
1056 return;
1057 }
1058
1059 /* We'll go to next step, unless told to finish early */
1060 next_step = ctx->step + 1;
1061
1062 /* list images we need to download? */
1063 if (qmi_message_dms_set_firmware_preference_output_get_image_download_list (output, &array, &error)) {
1064 if (!array->len) {
1065 g_print ("device already contains the given firmware/config version: no download needed\n");
1066 g_print ("forcing the download may be requested with the --override-download option\n");
1067 g_print ("now power cycling to apply the new firmware preference...\n");
1068 g_list_free_full (ctx->pending_images, g_object_unref);
1069 ctx->pending_images = NULL;
1070 } else {
1071 GString *images = NULL;
1072 QmiDmsFirmwareImageType type;
1073 guint i;
1074
1075 images = g_string_new ("");
1076 for (i = 0; i < array->len; i++) {
1077 type = g_array_index (array, QmiDmsFirmwareImageType, i);
1078 g_string_append (images, qmi_dms_firmware_image_type_get_string (type));
1079 if (i < array->len -1)
1080 g_string_append (images, ", ");
1081 }
1082
1083 g_debug ("[qfu-updater] need to download the following images: %s", images->str);
1084 g_string_free (images, TRUE);
1085 }
1086 }
1087
1088 qmi_message_dms_set_firmware_preference_output_unref (output);
1089
1090 /* Go on */
1091 run_context_step_next (task, next_step);
1092 }
1093
1094 static void
1095 run_context_step_set_firmware_preference (GTask *task)
1096 {
1097 QfuUpdater *self;
1098 RunContext *ctx;
1099 QmiMessageDmsSetFirmwarePreferenceInput *input;
1100 GArray *array;
1101 QmiMessageDmsSetFirmwarePreferenceInputListImage modem_image_id;
1102 QmiMessageDmsSetFirmwarePreferenceInputListImage pri_image_id;
1103 const gchar *firmware_version;
1104 const gchar *config_version;
1105 const gchar *carrier;
1106
1107 ctx = (RunContext *) g_task_get_task_data (task);
1108 self = g_task_get_source_object (task);
1109
1110 firmware_version = self->priv->firmware_version ? self->priv->firmware_version : ctx->firmware_version;
1111 config_version = self->priv->config_version ? self->priv->config_version : ctx->config_version;
1112 carrier = self->priv->carrier ? self->priv->carrier : ctx->carrier;
1113
1114 g_assert (firmware_version);
1115 g_assert (config_version);
1116 g_assert (carrier);
1117
1118 g_print ("setting firmware preference:\n");
1119 g_print (" firmware version: '%s'\n", firmware_version);
1120 g_print (" config version: '%s'\n", config_version);
1121 g_print (" carrier: '%s'\n", carrier);
1122
1123 /* Set modem image info */
1124 modem_image_id.type = QMI_DMS_FIRMWARE_IMAGE_TYPE_MODEM;
1125 modem_image_id.unique_id = g_array_sized_new (FALSE, TRUE, sizeof (gchar), 16);
1126 g_array_insert_vals (modem_image_id.unique_id, 0, "?_?", 3);
1127 g_array_set_size (modem_image_id.unique_id, 16);
1128 modem_image_id.build_id = g_strdup_printf ("%s_?", firmware_version);
1129
1130 /* Set pri image info */
1131 pri_image_id.type = QMI_DMS_FIRMWARE_IMAGE_TYPE_PRI;
1132 pri_image_id.unique_id = g_array_sized_new (FALSE, TRUE, sizeof (gchar), 16);
1133 g_array_insert_vals (pri_image_id.unique_id, 0, config_version, strlen (config_version));
1134 g_array_set_size (pri_image_id.unique_id, 16);
1135 pri_image_id.build_id = g_strdup_printf ("%s_%s", firmware_version, carrier);
1136
1137 array = g_array_sized_new (FALSE, FALSE, sizeof (QmiMessageDmsSetFirmwarePreferenceInputListImage), 2);
1138 g_array_append_val (array, modem_image_id);
1139 g_array_append_val (array, pri_image_id);
1140
1141 input = qmi_message_dms_set_firmware_preference_input_new ();
1142 qmi_message_dms_set_firmware_preference_input_set_list (input, array, NULL);
1143 g_array_unref (array);
1144
1145 if (self->priv->override_download)
1146 qmi_message_dms_set_firmware_preference_input_set_download_override (input, TRUE, NULL);
1147
1148 if (self->priv->modem_storage_index > 0)
1149 qmi_message_dms_set_firmware_preference_input_set_modem_storage_index (input, (guint8) self->priv->modem_storage_index, NULL);
1150
1151 g_debug ("[qfu-updater] setting firmware preference...");
1152 g_debug ("[qfu-updater] modem image: unique id '%.16s', build id '%s'",
1153 (gchar *) (modem_image_id.unique_id->data), modem_image_id.build_id);
1154 g_debug ("[qfu-updater] pri image: unique id '%.16s', build id '%s'",
1155 (gchar *) (pri_image_id.unique_id->data), pri_image_id.build_id);
1156 g_debug ("[qfu-updater] override download: %s",
1157 self->priv->override_download ? "yes" : "no");
1158
1159 qmi_client_dms_set_firmware_preference (ctx->qmi_client,
1160 input,
1161 10,
1162 g_task_get_cancellable (task),
1163 (GAsyncReadyCallback) set_firmware_preference_ready,
1164 task);
1165
1166 g_array_unref (modem_image_id.unique_id);
1167 g_free (modem_image_id.build_id);
1168 g_array_unref (pri_image_id.unique_id);
1169 g_free (pri_image_id.build_id);
1170
1171 qmi_message_dms_set_firmware_preference_input_unref (input);
1172 }
1173
1174 static gboolean
1175 validate_firmware_config_carrier (QfuUpdater *self,
1176 RunContext *ctx,
1177 GError **error)
1178 {
1179 GList *l;
1180
1181 /* Try to preload information like firmware/config/carrier from CWE images */
1182 for (l = ctx->pending_images; l; l = g_list_next (l)) {
1183 const gchar *firmware_version;
1184 const gchar *config_version;
1185 const gchar *carrier;
1186 QfuImageCwe *image;
1187
1188 if (!QFU_IS_IMAGE_CWE (l->data))
1189 continue;
1190
1191 image = QFU_IMAGE_CWE (l->data);
1192 firmware_version = qfu_image_cwe_get_parsed_firmware_version (image);
1193 config_version = qfu_image_cwe_get_parsed_config_version (image);
1194 carrier = qfu_image_cwe_get_parsed_carrier (image);
1195
1196 if (firmware_version) {
1197 if (!ctx->firmware_version)
1198 ctx->firmware_version = g_strdup (firmware_version);
1199 else if (!g_str_equal (firmware_version, ctx->firmware_version)) {
1200 if (!self->priv->ignore_version_errors) {
1201 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
1202 "couldn't detect firmware version: "
1203 "firmware version strings don't match on specified images: "
1204 "'%s' != '%s'",
1205 firmware_version, ctx->firmware_version);
1206 return FALSE;
1207 }
1208 g_warning ("firmware version strings don't match on specified images: "
1209 "'%s' != '%s' (IGNORED with --ignore-version-errors)",
1210 firmware_version, ctx->firmware_version);
1211 }
1212 }
1213
1214 if (config_version) {
1215 if (!ctx->config_version)
1216 ctx->config_version = g_strdup (config_version);
1217 else if (!g_str_equal (config_version, ctx->config_version)) {
1218 if (!self->priv->ignore_version_errors) {
1219 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
1220 "couldn't detect config version: "
1221 "config version strings don't match on specified images: "
1222 "'%s' != '%s'",
1223 config_version, ctx->config_version);
1224 return FALSE;
1225 }
1226 g_warning ("[qfu-updater] config version strings don't match on specified images: "
1227 "'%s' != '%s' (IGNORED with --ignore-version-errors)",
1228 config_version, ctx->config_version);
1229 }
1230 }
1231
1232 if (carrier) {
1233 if (!ctx->carrier)
1234 ctx->carrier = g_strdup (carrier);
1235 else if (!g_str_equal (carrier, ctx->carrier)) {
1236 if (!self->priv->ignore_version_errors) {
1237 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
1238 "couldn't detect carrier: "
1239 "carrier strings don't match on specified images: "
1240 "'%s' != '%s'",
1241 carrier, ctx->carrier);
1242 return FALSE;
1243 }
1244 g_warning ("[qfu-updater] carrier strings don't match on specified images: "
1245 "'%s' != '%s' (IGNORED with --ignore-version-errors)",
1246 carrier, ctx->carrier);
1247 }
1248 }
1249 }
1250
1251 /* If given firmware version doesn't match the one in the image, error out */
1252 if (self->priv->firmware_version && (g_strcmp0 (self->priv->firmware_version, ctx->firmware_version) != 0)) {
1253 if (!self->priv->ignore_version_errors) {
1254 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
1255 "error validating firmware version: "
1256 "user provided firmware version doesn't match the one in the specified images: "
1257 "'%s' != '%s'",
1258 self->priv->firmware_version, ctx->firmware_version);
1259 return FALSE;
1260 }
1261 g_warning ("[qfu-updater] user provided firmware version doesn't match the one in the specified images: "
1262 "'%s' != '%s' (IGNORED with --ignore-version-errors)",
1263 self->priv->firmware_version, ctx->firmware_version);
1264 }
1265
1266 /* If given config version doesn't match the one in the image, error out */
1267 if (self->priv->config_version && (g_strcmp0 (self->priv->config_version, ctx->config_version) != 0)) {
1268 if (!self->priv->ignore_version_errors) {
1269 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
1270 "error validating firmware version: "
1271 "user provided firmware version doesn't match the one in the specified images: "
1272 "'%s' != '%s'",
1273 self->priv->config_version, ctx->config_version);
1274 return FALSE;
1275 }
1276 g_warning ("[qfu-updater] user provided config version doesn't match the one in the specified images: "
1277 "'%s' != '%s' (IGNORED with --ignore-version-errors)",
1278 self->priv->firmware_version, ctx->firmware_version);
1279 }
1280
1281 /* If given carrier doesn't match the one in the image, error out */
1282 if (self->priv->carrier && (g_strcmp0 (self->priv->carrier, ctx->carrier) != 0)) {
1283 if (!self->priv->ignore_version_errors) {
1284 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
1285 "error validating carrier: "
1286 "user provided carrier doesn't match the one in the specified images: "
1287 "'%s' != '%s'",
1288 self->priv->carrier, ctx->carrier);
1289 return FALSE;
1290 }
1291 g_warning ("[qfu-updater] user provided carrier doesn't match the one in the specified images: "
1292 "'%s' != '%s' (IGNORED with --ignore-version-errors)",
1293 self->priv->carrier, ctx->carrier);
1294 }
1295
1296 /* No firmware version? */
1297 if (!self->priv->firmware_version && !ctx->firmware_version) {
1298 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1299 "firmware version required");
1300 return FALSE;
1301 }
1302
1303 /* No config version? */
1304 if (!self->priv->config_version && !ctx->config_version) {
1305 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1306 "config version required");
1307 return FALSE;
1308 }
1309
1310 /* No carrier? */
1311 if (!self->priv->carrier && !ctx->carrier) {
1312 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1313 "carrier required");
1314 return FALSE;
1315 }
1316
1317 return TRUE;
1318 }
1319
1320 static void
1321 run_context_step_get_firmware_preference (GTask *task)
1322 {
1323 RunContext *ctx;
1324 QfuUpdater *self;
1325 GError *error = NULL;
1326
1327 ctx = (RunContext *) g_task_get_task_data (task);
1328 self = g_task_get_source_object (task);
1329
1330 if (!ctx->supports_firmware_preference_management) {
1331 /* Firmware preference setting not supported; fail if we got those settings explicitly */
1332 if (self->priv->firmware_version || self->priv->config_version || self->priv->carrier) {
1333 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
1334 "setting firmware/config/carrier is not supported by this device");
1335 g_object_unref (task);
1336 return;
1337 }
1338
1339 /* Jump to the reset step and run boothold there */
1340 ctx->boothold_reset = TRUE;
1341 run_context_step_next (task, RUN_CONTEXT_STEP_POWER_CYCLE);
1342 return;
1343 }
1344
1345 /* Firmware preference setting is supported so we require firmware/config/carrier */
1346 if (!validate_firmware_config_carrier (self, ctx, &error)) {
1347 g_task_return_error (task, error);
1348 g_object_unref (task);
1349 return;
1350 }
1351
1352 /* Go on */
1353 run_context_step_next (task, ctx->step + 1);
1354 }
1355
1356 static void
1357 new_client_dms_ready (gpointer unused,
1358 GAsyncResult *res,
1359 GTask *task)
1360 {
1361 RunContext *ctx;
1362 QfuUpdater *self;
1363 GError *error = NULL;
1364
1365 ctx = (RunContext *) g_task_get_task_data (task);
1366 self = g_task_get_source_object (task);
1367
1368 g_assert (!ctx->qmi_device);
1369 g_assert (!ctx->qmi_client);
1370
1371 if (!qfu_utils_new_client_dms_finish (res,
1372 &ctx->qmi_device,
1373 &ctx->qmi_client,
1374 &ctx->revision,
1375 &ctx->supports_stored_image_management,
1376 &ctx->max_modem_storage_index,
1377 &ctx->supports_firmware_preference_management,
1378 &ctx->firmware_preference,
1379 &ctx->current_firmware,
1380 &error)) {
1381 g_task_return_error (task, error);
1382 g_object_unref (task);
1383 return;
1384 }
1385
1386 /* Validate modem storage index, if one specified */
1387 if (self->priv->modem_storage_index > ctx->max_modem_storage_index) {
1388 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
1389 "modem storage index out of bounds (%u > %u)",
1390 self->priv->modem_storage_index,
1391 ctx->max_modem_storage_index);
1392 g_object_unref (task);
1393 return;
1394 }
1395
1396 /* Go on */
1397 run_context_step_next (task, ctx->step + 1);
1398 }
1399
1400 static void
1401 run_context_step_qmi_client (GTask *task)
1402 {
1403 RunContext *ctx;
1404 QfuUpdater *self;
1405
1406 ctx = (RunContext *) g_task_get_task_data (task);
1407 self = g_task_get_source_object (task);
1408
1409 g_print ("loading device information before the update...\n");
1410
1411 g_debug ("[qfu-updater] creating QMI DMS client...");
1412 g_assert (ctx->cdc_wdm_file);
1413 qfu_utils_new_client_dms (ctx->cdc_wdm_file,
1414 3, /* initially, up to 3 tries to allocate DMS client */
1415 self->priv->device_open_flags,
1416 TRUE,
1417 g_task_get_cancellable (task),
1418 (GAsyncReadyCallback) new_client_dms_ready,
1419 task);
1420 }
1421
1422 typedef void (* RunContextStepFunc) (GTask *task);
1423 static const RunContextStepFunc run_context_step_func[] = {
1424 [RUN_CONTEXT_STEP_QMI_CLIENT] = run_context_step_qmi_client,
1425 [RUN_CONTEXT_STEP_GET_FIRMWARE_PREFERENCE] = run_context_step_get_firmware_preference,
1426 [RUN_CONTEXT_STEP_SET_FIRMWARE_PREFERENCE] = run_context_step_set_firmware_preference,
1427 [RUN_CONTEXT_STEP_POWER_CYCLE] = run_context_step_power_cycle,
1428 [RUN_CONTEXT_STEP_CLEANUP_QMI_DEVICE] = run_context_step_cleanup_qmi_device,
1429 [RUN_CONTEXT_STEP_WAIT_FOR_TTY] = run_context_step_wait_for_tty,
1430 [RUN_CONTEXT_STEP_SELECT_DEVICE] = run_context_step_select_device,
1431 [RUN_CONTEXT_STEP_SELECT_IMAGE] = run_context_step_select_image,
1432 [RUN_CONTEXT_STEP_DOWNLOAD_IMAGE] = run_context_step_download_image,
1433 [RUN_CONTEXT_STEP_CLEANUP_IMAGE] = run_context_step_cleanup_image,
1434 [RUN_CONTEXT_STEP_CLEANUP_DEVICE] = run_context_step_cleanup_device,
1435 [RUN_CONTEXT_STEP_WAIT_FOR_CDC_WDM] = run_context_step_wait_for_cdc_wdm,
1436 [RUN_CONTEXT_STEP_WAIT_FOR_BOOT] = run_context_step_wait_for_boot,
1437 [RUN_CONTEXT_STEP_QMI_CLIENT_AFTER] = run_context_step_qmi_client_after,
1438 [RUN_CONTEXT_STEP_CLEANUP_QMI_DEVICE_FULL] = run_context_step_cleanup_qmi_device_full,
1439 };
1440
1441 G_STATIC_ASSERT (G_N_ELEMENTS (run_context_step_func) == RUN_CONTEXT_STEP_LAST);
1442
1443 static void
1444 run_context_step (GTask *task)
1445 {
1446 RunContext *ctx;
1447
1448 ctx = (RunContext *) g_task_get_task_data (task);
1449
1450 /* Early halt operation if cancelled */
1451 if (g_task_return_error_if_cancelled (task)) {
1452 g_object_unref (task);
1453 return;
1454 }
1455
1456 if (ctx->step < G_N_ELEMENTS (run_context_step_func)) {
1457 run_context_step_func [ctx->step] (task);
1458 return;
1459 }
1460
1461 g_debug ("[qfu-updater] operation finished");
1462 run_context_step_last (task);
1463 }
1464
1465 static gint
1466 image_sort_by_size (QfuImage *a, QfuImage *b)
1467 {
1468 return qfu_image_get_size (b) - qfu_image_get_size (a);
1469 }
1470
1471 static gboolean
1472 preload_images (RunContext *ctx,
1473 GList *image_file_list,
1474 GCancellable *cancellable,
1475 GError **error)
1476 {
1477 GList *l;
1478
1479 /* Build QfuImage objects for each image file given */
1480 for (l = image_file_list; l; l = g_list_next (l)) {
1481 QfuImage *image;
1482
1483 image = qfu_image_factory_build (G_FILE (l->data), cancellable, error);
1484 if (!image)
1485 return FALSE;
1486
1487 ctx->pending_images = g_list_append (ctx->pending_images, image);
1488 }
1489
1490 /* Sort by size, we want to download bigger images first, as that is usually
1491 * the use case anyway, first flash e.g. the .cwe file, then the .nvu one. */
1492 ctx->pending_images = g_list_sort (ctx->pending_images, (GCompareFunc) image_sort_by_size);
1493
1494 return TRUE;
1495 }
1496
1497 void
1498 qfu_updater_run (QfuUpdater *self,
1499 GList *image_file_list,
1500 GCancellable *cancellable,
1501 GAsyncReadyCallback callback,
1502 gpointer user_data)
1503 {
1504 RunContext *ctx;
1505 GTask *task;
1506 GError *error = NULL;
1507
1508 g_assert (image_file_list);
1509
1510 ctx = g_slice_new0 (RunContext);
1511
1512 task = g_task_new (self, cancellable, callback, user_data);
1513 g_task_set_task_data (task, ctx, (GDestroyNotify) run_context_free);
1514
1515 if (!preload_images (ctx, image_file_list, cancellable, &error)) {
1516 g_task_return_error (task, error);
1517 g_object_unref (task);
1518 return;
1519 }
1520
1521 switch (self->priv->type) {
1522 case UPDATER_TYPE_GENERIC:
1523 ctx->step = RUN_CONTEXT_STEP_QMI_CLIENT;
1524 ctx->cdc_wdm_file = qfu_device_selection_get_single_cdc_wdm (self->priv->device_selection);
1525 if (!ctx->cdc_wdm_file) {
1526 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1527 "No cdc-wdm device found to run update operation");
1528 g_object_unref (task);
1529 return;
1530 }
1531 break;
1532 case UPDATER_TYPE_DOWNLOAD:
1533 ctx->step = RUN_CONTEXT_STEP_SELECT_DEVICE;
1534 ctx->serial_file = qfu_device_selection_get_single_tty (self->priv->device_selection, QFU_HELPERS_DEVICE_MODE_DOWNLOAD);
1535 if (!ctx->serial_file) {
1536 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
1537 "No serial device in download mode found to run QDL update operation");
1538 g_object_unref (task);
1539 return;
1540 }
1541 break;
1542 case UPDATER_TYPE_UNKNOWN:
1543 default:
1544 g_assert_not_reached ();
1545 }
1546
1547 run_context_step (task);
1548 }
1549
1550 /******************************************************************************/
1551
1552 QfuUpdater *
1553 qfu_updater_new (QfuDeviceSelection *device_selection,
1554 const gchar *firmware_version,
1555 const gchar *config_version,
1556 const gchar *carrier,
1557 QmiDeviceOpenFlags device_open_flags,
1558 gboolean ignore_version_errors,
1559 gboolean override_download,
1560 guint8 modem_storage_index,
1561 gboolean skip_validation)
1562 {
1563 QfuUpdater *self;
1564
1565 g_assert (QFU_IS_DEVICE_SELECTION (device_selection));
1566
1567 self = g_object_new (QFU_TYPE_UPDATER, NULL);
1568 self->priv->type = UPDATER_TYPE_GENERIC;
1569 self->priv->device_selection = g_object_ref (device_selection);
1570 self->priv->device_open_flags = device_open_flags;
1571 self->priv->firmware_version = (firmware_version ? g_strdup (firmware_version) : NULL);
1572 self->priv->config_version = (config_version ? g_strdup (config_version) : NULL);
1573 self->priv->carrier = (carrier ? g_strdup (carrier) : NULL);
1574 self->priv->ignore_version_errors = ignore_version_errors;
1575 self->priv->override_download = override_download;
1576 self->priv->modem_storage_index = modem_storage_index;
1577 self->priv->skip_validation = skip_validation;
1578
1579 return self;
1580 }
1581
1582 QfuUpdater *
1583 qfu_updater_new_download (QfuDeviceSelection *device_selection)
1584 {
1585 QfuUpdater *self;
1586
1587 g_assert (QFU_IS_DEVICE_SELECTION (device_selection));
1588
1589 self = g_object_new (QFU_TYPE_UPDATER, NULL);
1590 self->priv->type = UPDATER_TYPE_DOWNLOAD;
1591 self->priv->device_selection = g_object_ref (device_selection);
1592
1593 return self;
1594 }
1595
1596 static void
1597 qfu_updater_init (QfuUpdater *self)
1598 {
1599 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, QFU_TYPE_UPDATER, QfuUpdaterPrivate);
1600 self->priv->type = UPDATER_TYPE_UNKNOWN;
1601 }
1602
1603 static void
1604 dispose (GObject *object)
1605 {
1606 QfuUpdater *self = QFU_UPDATER (object);
1607
1608 g_clear_object (&self->priv->device_selection);
1609
1610 G_OBJECT_CLASS (qfu_updater_parent_class)->dispose (object);
1611 }
1612
1613 static void
1614 finalize (GObject *object)
1615 {
1616 QfuUpdater *self = QFU_UPDATER (object);
1617
1618 g_free (self->priv->firmware_version);
1619 g_free (self->priv->config_version);
1620 g_free (self->priv->carrier);
1621
1622 G_OBJECT_CLASS (qfu_updater_parent_class)->finalize (object);
1623 }
1624
1625 static void
1626 qfu_updater_class_init (QfuUpdaterClass *klass)
1627 {
1628 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1629
1630 g_type_class_add_private (object_class, sizeof (QfuUpdaterPrivate));
1631
1632 object_class->dispose = dispose;
1633 object_class->finalize = finalize;
1634 }