aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmi-firmware-update/qfu-main.c
blob: 7d53cc951379102bc13970b0675f82b3fe1d1519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * qmi-firmware-update -- Command line tool to update firmware in QMI devices
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) 2016-2017 Zodiac Inflight Innovation
 * Copyright (C) 2016-2017 Aleksander Morgado <aleksander@aleksander.es>
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>

#include <glib.h>
#include <gio/gio.h>

#include "qfu-log.h"
#include "qfu-operation.h"
#include "qfu-device-selection.h"
#include "qfu-udev-helpers.h"

#define PROGRAM_NAME    "qmi-firmware-update"
#define PROGRAM_VERSION PACKAGE_VERSION

/*****************************************************************************/
/* Options */

/* Generic device selections */
#if defined WITH_UDEV
static guint   busnum;
static guint   devnum;
static guint16 vid;
static guint16 pid;
#endif
static gchar *cdc_wdm_str;
static gchar *tty_str;

#if defined WITH_UDEV
/* Update */
static gboolean   action_update_flag;
static gchar     *firmware_version_str;
static gchar     *config_version_str;
static gchar     *carrier_str;
static gboolean   ignore_version_errors_flag;
static gboolean   override_download_flag;
static gint       modem_storage_index_int;
static gboolean   skip_validation_flag;
#endif

/* Reset */
static gboolean   action_reset_flag;

/* Update (QDL mode) */
static gboolean   action_update_qdl_flag;

/* Verify */
static gboolean   action_verify_flag;

/* Main */
static gchar    **image_strv;
static gboolean   device_open_proxy_flag;
static gboolean   device_open_qmi_flag;
static gboolean   device_open_mbim_flag;
static gboolean   device_open_auto_flag;
static gboolean   stdout_verbose_flag;
static gboolean   stdout_silent_flag;
static gchar     *verbose_log_str;
static gboolean   version_flag;
static gboolean   help_flag;
static gboolean   help_examples_flag;

#if defined WITH_UDEV

static gboolean
parse_busnum_devnum (const gchar  *option_name,
                     const gchar  *value,
                     gpointer      data,
                     GError      **error)
{
    gchar    **strv;
    gint       busnum_idx = -1;
    gint       devnum_idx = 0;
    gulong     aux;
    gboolean   result = FALSE;

    strv = g_strsplit (value, ":", -1);
    g_assert (strv[0]);
    if (strv[1]) {
        busnum_idx = 0;
        devnum_idx = 1;
        if (strv[2]) {
            g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                         "invalid busnum-devnum string: too many fields");
            goto out;
        }
    }

    if (busnum_idx != -1) {
        aux = strtoul (strv[busnum_idx], NULL, 10);
        if (aux == 0 || aux > G_MAXUINT) {
            g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                         "invalid bus number: %s", strv[busnum_idx]);
            goto out;
        }
        busnum = (guint) aux;
    }

    aux = strtoul (strv[devnum_idx], NULL, 10);
    if (aux == 0 || aux > G_MAXUINT) {
        g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                     "invalid dev number: %s", strv[devnum_idx]);
        goto out;
    }
    devnum = (guint) aux;
    result = TRUE;

out:
    g_strfreev (strv);
    return result;
}

static gboolean
parse_vid_pid (const gchar  *option_name,
               const gchar  *value,
               gpointer      data,
               GError      **error)
{
    gchar    **strv;
    gint       vid_idx = 0;
    gint       pid_idx = -1;
    gulong     aux;
    gboolean   result = FALSE;

    strv = g_strsplit (value, ":", -1);
    g_assert (strv[0]);
    if (strv[1]) {
        pid_idx = 1;
        if (strv[2]) {
            g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                         "invalid vid-pid string: too many fields");
            goto out;
        }
    }

    if (pid_idx != -1) {
        aux = strtoul (strv[pid_idx], NULL, 16);
        if (aux == 0 || aux > G_MAXUINT16) {
            g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                         "invalid product id: %s", strv[pid_idx]);
            goto out;
        }
        pid = (guint) aux;
    }

    aux = strtoul (strv[vid_idx], NULL, 16);
    if (aux == 0 || aux > G_MAXUINT16) {
        g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                     "invalid vendor id: %s", strv[vid_idx]);
        goto out;
    }
    vid = (guint16) aux;
    result = TRUE;

out:
    g_strfreev (strv);
    return result;
}

#endif /* WITH_UDEV */

static GOptionEntry context_selection_entries[] = {
#if defined WITH_UDEV
    { "busnum-devnum", 's', 0, G_OPTION_ARG_CALLBACK, &parse_busnum_devnum,
      "Select device by bus and device number (in decimal).",
      "[BUS:]DEV"
    },
    { "vid-pid", 'd', 0, G_OPTION_ARG_CALLBACK, &parse_vid_pid,
      "Select device by device vendor and product id (in hexadecimal).",
      "VID[:PID]"
    },
#endif /* WITH_UDEV */
    { "cdc-wdm", 'w', 0, G_OPTION_ARG_FILENAME, &cdc_wdm_str,
      "Select device by QMI/MBIM cdc-wdm device path (e.g. /dev/cdc-wdm0).",
      "[PATH]"
    },
    { "tty", 't', 0, G_OPTION_ARG_FILENAME, &tty_str,
      "Select device by serial device path (e.g. /dev/ttyUSB2).",
      "[PATH]"
    },
    { NULL }
};

#if defined WITH_UDEV
static GOptionEntry context_update_entries[] = {
    { "update", 'u', 0, G_OPTION_ARG_NONE, &action_update_flag,
      "Launch firmware update process.",
      NULL
    },
    { "firmware-version", 'f', 0, G_OPTION_ARG_STRING, &firmware_version_str,
      "Firmware version (e.g. '05.05.58.00').",
      "[VERSION]",
    },
    { "config-version", 'c', 0, G_OPTION_ARG_STRING, &config_version_str,
      "Config version (e.g. '005.025_002').",
      "[VERSION]",
    },
    { "carrier", 'C', 0, G_OPTION_ARG_STRING, &carrier_str,
      "Carrier name (e.g. 'Generic').",
      "[CARRIER]",
    },
    { "ignore-version-errors", 0, 0, G_OPTION_ARG_NONE, &ignore_version_errors_flag,
      "Run update operation even with version string errors.",
      NULL
    },
    { "override-download", 0, 0, G_OPTION_ARG_NONE, &override_download_flag,
      "Download images even if module says it already has them.",
      NULL
    },
    { "modem-storage-index", 0, 0, G_OPTION_ARG_INT, &modem_storage_index_int,
      "Index storage for the modem image.",
      "[INDEX]"
    },
    { "skip-validation", 0, 0, G_OPTION_ARG_NONE, &skip_validation_flag,
      "Don't wait to validate the running firmware after update.",
      NULL
    },
    { NULL }
};
#endif /* WITH_UDEV */

static GOptionEntry context_reset_entries[] = {
    { "reset", 'b', 0, G_OPTION_ARG_NONE, &action_reset_flag,
      "Reset device into QDL download mode.",
      NULL
    },
    { NULL }
};

static GOptionEntry context_update_qdl_entries[] = {
    { "update-qdl", 'U', 0, G_OPTION_ARG_NONE, &action_update_qdl_flag,
      "Launch firmware update process in QDL mode.",
      NULL
    },
    { NULL }
};

static GOptionEntry context_verify_entries[] = {
    { "verify", 'z', 0, G_OPTION_ARG_NONE, &action_verify_flag,
      "Analyze and verify firmware images.",
      NULL
    },
    { NULL }
};

static GOptionEntry context_main_entries[] = {
    { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &image_strv, "",
      "FILE1 FILE2..."
    },
    { "device-open-proxy", 'p', 0, G_OPTION_ARG_NONE, &device_open_proxy_flag,
      "Request to use the 'qmi-proxy' proxy.",
      NULL
    },
    { "device-open-qmi", 0, 0, G_OPTION_ARG_NONE, &device_open_qmi_flag,
      "Open a cdc-wdm device explicitly in QMI mode",
      NULL
    },
    { "device-open-mbim", 0, 0, G_OPTION_ARG_NONE, &device_open_mbim_flag,
      "Open a cdc-wdm device explicitly in MBIM mode",
      NULL
    },
    { "device-open-auto", 0, 0, G_OPTION_ARG_NONE, &device_open_auto_flag,
      "Open a cdc-wdm device in either QMI or MBIM mode (default)",
      NULL
    },
    { "verbose", 'v', 0, G_OPTION_ARG_NONE, &stdout_verbose_flag,
      "Run action with verbose messages in standard output, including the debug ones.",
      NULL
    },
    { "silent", 'S', 0, G_OPTION_ARG_NONE, &stdout_silent_flag,
      "Run action with no messages in standard output; not even the error ones.",
      NULL
    },
    { "verbose-log", 'L', 0, G_OPTION_ARG_FILENAME, &verbose_log_str,
      "Write verbose messages to an output file.",
      "[PATH]"
    },
    { "version", 'V', 0, G_OPTION_ARG_NONE, &version_flag,
      "Print version.",
      NULL
    },
    { "help", 'h', 0, G_OPTION_ARG_NONE, &help_flag,
      "Show help.",
      NULL
    },
    { "help-examples", 'H', 0, G_OPTION_ARG_NONE, &help_examples_flag,
      "Show help examples.",
      NULL
    },
    { NULL }
};

static const gchar *context_description =
    "   ***************************************************************************\n"
    "                                Warning!\n"
    "   ***************************************************************************\n"
    "\n"
    "   Use this program with caution. The authors take *no* responsibility if any\n"
    "   device gets broken as a result of using this program.\n"
    "\n"
    "   Please report issues to the libqmi mailing list at:\n"
    "     libqmi-devel@lists.freedesktop.org\n";

/*****************************************************************************/

static void
print_version (void)
{
    g_print ("\n"
             PROGRAM_NAME " " PROGRAM_VERSION "\n"
             "\n"
             "  Copyright (C) 2016-2017 Bjørn Mork\n"
             "  Copyright (C) 2016-2017 Zodiac Inflight Innovations\n"
             "  Copyright (C) 2016-2017 Aleksander Morgado\n"
             "\n"
             "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n"
             "This is free software: you are free to change and redistribute it.\n"
             "There is NO WARRANTY, to the extent permitted by law.\n"
             "\n");
}

static void
print_help (GOptionContext *context)
{
    gchar *str;

    /* Always print --help-all */
    str = g_option_context_get_help (context, FALSE, NULL);
    g_print ("%s", str);
    g_free (str);
}

static void
print_help_examples (void)
{
#if defined WITH_UDEV
    g_print ("\n"
             "********************************************************************************\n"
             "\n"
             " Example: Updating a Sierra Wireless MC7354.\n"
             "  (or other 9x15 or 9x30 devices, like the MC7304, MC7330, MC7455... ).\n"
             "\n"
             " The MC7354 is a 9x15 device which requires the firmware updater to specify the\n"
             " firmware version string, the config version string and the carrier string, so\n"
             " that they are included as identifiers of the firmware images downloaded. The\n"
             " core logic in the application will try to automatically detect these strings,\n"
             " although the user can also use specific options to override them or if the\n"
             " automatic detection failed.\n"
             "\n"
             " While in normal operation, the device will expose multiple cdc-wdm ports, and\n"
             " the updater application just needs one of those cdc-wdm ports to start the\n"
             " operation. The user can explicitly specify the cdc-wdm port to use, or\n"
             " otherwise use the generic device selection options (i.e. --busnum-devnum or\n"
             " --vid-pid) to do that automatically.\n"
             "\n"
             " Note that the firmware for the MC7354 is usually composed of a core system image\n"
             " (.cwe) and a carrier-specific image (.nvu). These two images need to be flashed\n"
             " on the same operation, unless upgrading the carrier-specific image on a device\n"
             " which already has the matching firmware version. The two images may be given\n"
             " combined into a single image (.spk) file.\n"
             "\n"
             " a) An update operation specifying the vid:pid of the device (fails if multiple\n"
             "    devices with the same vid:pid are found):\n"
             "    $ sudo " PROGRAM_NAME " \\\n"
             "          --update \\\n"
             "          -d 1199:68c0 \\\n"
             "          SWI9X15C_05.05.58.00.cwe \\\n"
             "          SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
             "\n"
             " b) An update operation specifying an explicit QMI cdc-wdm device:\n"
             "    $ sudo " PROGRAM_NAME " \\\n"
             "          --update \\\n"
             "          --cdc-wdm /dev/cdc-wdm0 \\\n"
             "          SWI9X15C_05.05.58.00.cwe \\\n"
             "          SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
             "\n"
             " c) An update operation specifying explicit firmware, config and carrier strings:\n"
             "    $ sudo " PROGRAM_NAME " \\\n"
             "          --update \\\n"
             "          -d 1199:68c0 \\\n"
             "          --firmware-version 05.05.58.00 \\\n"
             "          --config-version 005.025_002 \\\n"
             "          --carrier Generic \\\n"
             "          SWI9X15C_05.05.58.00.cwe \\\n"
             "          SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
             "\n"
             " d) An update operation with a combined image containing both system and carrier\n"
             "    specific images::\n"
             "    $ sudo " PROGRAM_NAME " \\\n"
             "          --update \\\n"
             "          -d 1199:68c0 \\\n"
             "          9999999_9902574_SWI9X15C_05.05.66.00_00_GENNA-UMTS_005.028_000-field.spk\n");

    g_print ("\n"
             "********************************************************************************\n"
             "\n"
             " Example: Updating a Sierra Wireless MC7700.\n"
             "  (or other 9200 devices, like the MC7710).\n"
             "\n"
             " The MC7700 is a 9200 device which doesn't require the explicit firmware, config\n"
             " and carrier strings. Unlike the MC7354, which would reboot itself into QDL\n"
             " download mode once these previous strings were configured, the MC7700 requires\n"
             " a specific \"boot and hold\" command to be sent (either via QMI or AT) to request\n"
             " the reset in QDL download mode.\n"
             "\n"
             " a) An update operation specifying the vid:pid of the device (fails if multiple\n"
             "    devices with the same vid:pid are found):\n"
             "    $ sudo " PROGRAM_NAME " \\\n"
             "          --update \\\n"
             "          -d 1199:68a2 \\\n"
             "          9999999_9999999_9200_03.05.14.00_00_generic_000.000_001_SPKG_MC.cwe\n"
             "\n"
             " b) An update operation specifying an explicit QMI cdc-wdm device:\n"
             "    $ sudo " PROGRAM_NAME " \\\n"
             "          --update \\\n"
             "          --cdc-wdm /dev/cdc-wdm0 \\\n"
             "          9999999_9999999_9200_03.05.14.00_00_generic_000.000_001_SPKG_MC.cwe\n");

#endif /* WITH_UDEV */

    g_print ("\n"
             "********************************************************************************\n"
             "\n"
             " Example: Manual process to update a Sierra Wireless MC7354.\n"
             "  (or other 9x15 or 9x30 devices, like the MC7304, MC7330, MC7455... ).\n"
             "\n"
             " The upgrade of devices from the 9x15 and 9x30 families is triggered via a\n"
             " 'firmware preference' setting. If the device accepts the setting, the user\n"
             " can request a device power cycle, which will boot in QDL download mode:\n"
             "\n"
             " a) Set firmware preference setting:\n"
             "    $ sudo qmicli \\\n"
             "          -d /dev/cdc-wdm0 \\\n"
             "          --dms-set-firmware-preference=\"05.05.58.00,005.025_002,Generic\"\n"
             "\n"
             " b) Request power cycle:\n"
             "    $ sudo qmicli \\\n"
             "          -d /dev/cdc-wdm0 \\\n"
             "          --dms-set-operating-mode=offline\n"
             "    $ sudo qmicli \\\n"
             "          -d /dev/cdc-wdm0 \\\n"
             "          --dms-set-operating-mode=reset\n"
             "\n"
             " c) Wait for the /dev/ttyUSB device to appear.\n"
             "\n"
             " d) Run updater operation while in QDL download mode:\n"
             "    $ sudo " PROGRAM_NAME " \\\n"
             "          -t /dev/ttyUSB0 \\\n"
             "          --update-qdl \\\n"
             "          SWI9X15C_05.05.58.00.cwe \\\n"
             "          SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
             "\n"
             " d) Now wait for the device to fully reboot, may take up to several minutes.\n");

    g_print ("\n"
             "********************************************************************************\n"
             "\n"
             " Example: Manual process to update a Sierra Wireless MC7700.\n"
             "  (or other 9200 devices, like the MC7710).\n"
             "\n"
             " Instead of setting a 'firmware preference', the devices from the 9200 family\n"
             " can just be rebooted in QDL mode:\n"
             "\n"
             " a) Request device to go into QDL download mode:\n"
             "    $ sudo " PROGRAM_NAME " \\\n"
             "          -d 1199:68a2 \\\n"
             "          --reset\n"
             "\n"
             " b) Wait for the /dev/ttyUSB device to appear.\n"
             "\n"
             " c) Run updater operation while in QDL download mode:\n"
             "    $ sudo " PROGRAM_NAME " \\\n"
             "          -d 1199:68a2 \\\n"
             "          --update-qdl \\\n"
             "          9999999_9999999_9200_03.05.14.00_00_generic_000.000_001_SPKG_MC.cwe\n"
             "\n"
             " d) Now wait for the device to fully reboot, may take up to several minutes.\n");


    g_print ("\n"
             "********************************************************************************\n"
             "\n"
             " Example: Verify firmware images.\n"
             "\n"
             " a) Verify several images at once:\n"
             "    $ " PROGRAM_NAME " \\\n"
             "          --verify \\\n"
             "          SWI9X15C_05.05.58.00.cwe \\\n"
             "          SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
             "\n"
             " b) Verify all .cwe, .nvu and .spk images inside a directory:\n"
             "    $ find . -regex \".*\\.\\(nvu\\|spk\\|cwe\\)\" -exec " PROGRAM_NAME " -v -z {} \\;\n"
             "\n"
             " c) Image files may be given within .exe files; extract them with 7-Zip:\n"
             "    $ 7z x SWI9200M_3.5-Release13-SWI9200X_03.05.29.03.exe\n"
             "    $ ls *.{cwe,nvu,spk} 2>/dev/null\n"
             "      9999999_9999999_9200_03.05.29.03_00_generic_000.000_001_SPKG_MC.cwe\n"
             "\n");
}

/*****************************************************************************/

int main (int argc, char **argv)
{
    GError             *error = NULL;
    GOptionContext     *context;
    GOptionGroup       *group;
    guint               n_actions;
    guint               n_actions_images_needed;
    guint               n_actions_device_needed;
    guint               n_actions_cdc_wdm_needed;
    gboolean            result = FALSE;
    QfuDeviceSelection *device_selection = NULL;
    QmiDeviceOpenFlags  device_open_flags = QMI_DEVICE_OPEN_FLAGS_NONE;

    setlocale (LC_ALL, "");

    /* Setup option context, process it and destroy it */
    context = g_option_context_new ("- Update firmware in QMI devices");
    g_option_context_set_description (context, context_description);

    group = g_option_group_new ("selection", "Generic device selection options", "", NULL, NULL);
    g_option_group_add_entries (group, context_selection_entries);
    g_option_context_add_group (context, group);

#if defined WITH_UDEV
    group = g_option_group_new ("update", "Update options (normal mode)", "", NULL, NULL);
    g_option_group_add_entries (group, context_update_entries);
    g_option_context_add_group (context, group);
#endif

    group = g_option_group_new ("reset", "Reset options (normal mode)", "", NULL, NULL);
    g_option_group_add_entries (group, context_reset_entries);
    g_option_context_add_group (context, group);

    group = g_option_group_new ("update-qdl", "Update options (QDL mode)", "", NULL, NULL);
    g_option_group_add_entries (group, context_update_qdl_entries);
    g_option_context_add_group (context, group);

    group = g_option_group_new ("verify", "Verify options", "", NULL, NULL);
    g_option_group_add_entries (group, context_verify_entries);
    g_option_context_add_group (context, group);

    g_option_context_add_main_entries (context, context_main_entries, NULL);
    g_option_context_set_help_enabled (context, FALSE);

    if (!g_option_context_parse (context, &argc, &argv, &error)) {
        g_printerr ("error: couldn't parse option context: %s\n", error->message);
        g_error_free (error);
        goto out;
    }

    if (version_flag) {
        print_version ();
        result = TRUE;
        goto out;
    }

    if (help_flag) {
        print_help (context);
        result = TRUE;
        goto out;
    }

    if (help_examples_flag) {
        print_help_examples ();
        result = TRUE;
        goto out;
    }

    /* Initialize logging */
    qfu_log_init (stdout_verbose_flag, stdout_silent_flag, verbose_log_str);


    /* Total actions */
    n_actions = (action_verify_flag + action_update_qdl_flag + action_reset_flag);
    /* Actions that require images specified */
    n_actions_images_needed = (action_verify_flag + action_update_qdl_flag);
    /* Actions that require a device specified */
    n_actions_device_needed = (action_update_qdl_flag + action_reset_flag);
    /* Actions that allow using a cdc-wdm device */
    n_actions_cdc_wdm_needed = (action_reset_flag);

#if defined WITH_UDEV
    n_actions                += action_update_flag;
    n_actions_cdc_wdm_needed += action_update_flag;
    n_actions_images_needed  += action_update_flag;
    n_actions_device_needed  += action_update_flag;
#endif

    /* We don't allow multiple actions at the same time */
    if (n_actions == 0) {
        g_printerr ("error: no actions specified\n");
        goto out;
    }
    if (n_actions > 1) {
        g_printerr ("error: too many actions specified\n");
        goto out;
    }

    /* A list of images must be provided for update and verify operations */
    if (n_actions_images_needed && !image_strv) {
        g_printerr ("error: no firmware images specified\n");
        goto out;
    }

    /* device selection must be performed for update and reset operations */
    if (n_actions_device_needed) {
#if defined WITH_UDEV
        device_selection = qfu_device_selection_new (cdc_wdm_str, tty_str, vid, pid, busnum, devnum, &error);
#else
        device_selection = qfu_device_selection_new (cdc_wdm_str, tty_str, 0, 0, 0, 0, &error);
#endif
        if (!device_selection) {
            g_printerr ("error: couldn't select device:: %s\n", error->message);
            g_error_free (error);
            goto out;
        }
    }

    /* Validate device open flags */
    if (n_actions_cdc_wdm_needed) {
        if (device_open_mbim_flag + device_open_qmi_flag + device_open_auto_flag > 1) {
            g_printerr ("error: cannot specify multiple mode flags to open device\n");
            goto out;
        }
        if (device_open_proxy_flag)
            device_open_flags |= QMI_DEVICE_OPEN_FLAGS_PROXY;
        if (device_open_mbim_flag)
            device_open_flags |= QMI_DEVICE_OPEN_FLAGS_MBIM;
        if (device_open_auto_flag || (!device_open_qmi_flag && !device_open_mbim_flag))
            device_open_flags |= QMI_DEVICE_OPEN_FLAGS_AUTO;
    }

    /* Run */

#if defined WITH_UDEV
    if (action_update_flag) {
        g_assert (QFU_IS_DEVICE_SELECTION (device_selection));

        /* Validate storage index, just (0,G_MAXUINT8] for now. The value 0 is also not
         * valid, but we use it to flag when no specific index has been requested. */
        if (modem_storage_index_int < 0 || modem_storage_index_int > G_MAXUINT8) {
            g_printerr ("error: invalid modem storage index\n");
            goto out;
        }

        result = qfu_operation_update_run ((const gchar **) image_strv,
                                           device_selection,
                                           firmware_version_str,
                                           config_version_str,
                                           carrier_str,
                                           device_open_flags,
                                           ignore_version_errors_flag,
                                           override_download_flag,
                                           (guint8) modem_storage_index_int,
                                           skip_validation_flag);
        goto out;
    }
#endif /* WITH_UDEV */

    if (action_update_qdl_flag) {
        g_assert (QFU_IS_DEVICE_SELECTION (device_selection));
        result = qfu_operation_update_qdl_run ((const gchar **) image_strv,
                                               device_selection);
        goto out;
    }

    if (action_reset_flag) {
        g_assert (QFU_IS_DEVICE_SELECTION (device_selection));
        result = qfu_operation_reset_run (device_selection, device_open_flags);
        goto out;
    }

    if (action_verify_flag) {
        result = qfu_operation_verify_run ((const gchar **) image_strv);
        goto out;
    }

    g_assert_not_reached ();

out:

    qfu_log_shutdown ();

    /* Clean exit for a clean memleak report */
    if (context)
        g_option_context_free (context);
    if (device_selection)
        g_object_unref (device_selection);

    return (result ? EXIT_SUCCESS : EXIT_FAILURE);
}