Debian Codegraph @

src/qmi-firmware-update/qfu-main.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-2017 Zodiac Inflight Innovation
19 * Copyright (C) 2016-2017 Aleksander Morgado <[email protected]>
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <locale.h>
27 #include <string.h>
28
29 #include <glib.h>
30 #include <gio/gio.h>
31
32 #include "qfu-log.h"
33 #include "qfu-operation.h"
34 #include "qfu-device-selection.h"
35 #include "qfu-helpers.h"
36 #include "qfu-utils.h"
37
38 #define PROGRAM_NAME "qmi-firmware-update"
39 #define PROGRAM_VERSION PACKAGE_VERSION
40
41 /*****************************************************************************/
42 /* Options */
43
44 /* Generic device selections */
45 static guint busnum;
46 static guint devnum;
47 static guint16 vid;
48 static guint16 pid;
49 static gchar *cdc_wdm_str;
50 static gchar *tty_str;
51
52 #if defined MM_RUNTIME_CHECK_ENABLED
53 static gboolean ignore_mm_runtime_check_flag;
54 #endif
55
56 /* Update */
57 static gboolean action_update_flag;
58 static gchar *firmware_version_str;
59 static gchar *config_version_str;
60 static gchar *carrier_str;
61 static gboolean ignore_version_errors_flag;
62 static gboolean override_download_flag;
63 static gint modem_storage_index_int;
64 static gboolean skip_validation_flag;
65
66 /* Reset */
67 static gboolean action_reset_flag;
68
69 /* Update (in download mode) */
70 static gboolean action_update_download_flag;
71
72 /* Verify */
73 static gboolean action_verify_flag;
74
75 /* Main */
76 static gchar **image_strv;
77 static gboolean device_open_proxy_flag;
78 static gboolean device_open_qmi_flag;
79 static gboolean device_open_mbim_flag;
80 static gboolean device_open_auto_flag;
81 static gboolean stdout_verbose_flag;
82 static gboolean stdout_silent_flag;
83 static gchar *verbose_log_str;
84 static gboolean version_flag;
85 static gboolean help_flag;
86 static gboolean help_examples_flag;
87
88 static gboolean
89 parse_busnum_devnum (const gchar *option_name,
90 const gchar *value,
91 gpointer data,
92 GError **error)
93 {
94 gchar **strv;
95 gint busnum_idx = -1;
96 gint devnum_idx = 0;
97 gulong aux;
98 gboolean result = FALSE;
99
100 strv = g_strsplit (value, ":", -1);
101 g_assert (strv[0]);
102 if (strv[1]) {
103 busnum_idx = 0;
104 devnum_idx = 1;
105 if (strv[2]) {
106 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
107 "invalid busnum-devnum string: too many fields");
108 goto out;
109 }
110 }
111
112 if (busnum_idx != -1) {
113 aux = strtoul (strv[busnum_idx], NULL, 10);
114 if (aux == 0 || aux > G_MAXUINT) {
115 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
116 "invalid bus number: %s", strv[busnum_idx]);
117 goto out;
118 }
119 busnum = (guint) aux;
120 }
121
122 aux = strtoul (strv[devnum_idx], NULL, 10);
123 if (aux == 0 || aux > G_MAXUINT) {
124 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
125 "invalid dev number: %s", strv[devnum_idx]);
126 goto out;
127 }
128 devnum = (guint) aux;
129 result = TRUE;
130
131 out:
132 g_strfreev (strv);
133 return result;
134 }
135
136 static gboolean
137 parse_vid_pid (const gchar *option_name,
138 const gchar *value,
139 gpointer data,
140 GError **error)
141 {
142 gchar **strv;
143 gint vid_idx = 0;
144 gint pid_idx = -1;
145 gulong aux;
146 gboolean result = FALSE;
147
148 strv = g_strsplit (value, ":", -1);
149 g_assert (strv[0]);
150 if (strv[1]) {
151 pid_idx = 1;
152 if (strv[2]) {
153 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
154 "invalid vid-pid string: too many fields");
155 goto out;
156 }
157 }
158
159 if (pid_idx != -1) {
160 aux = strtoul (strv[pid_idx], NULL, 16);
161 if (aux == 0 || aux > G_MAXUINT16) {
162 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
163 "invalid product id: %s", strv[pid_idx]);
164 goto out;
165 }
166 pid = (guint) aux;
167 }
168
169 aux = strtoul (strv[vid_idx], NULL, 16);
170 if (aux == 0 || aux > G_MAXUINT16) {
171 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
172 "invalid vendor id: %s", strv[vid_idx]);
173 goto out;
174 }
175 vid = (guint16) aux;
176 result = TRUE;
177
178 out:
179 g_strfreev (strv);
180 return result;
181 }
182
183 static GOptionEntry context_selection_entries[] = {
184 { "busnum-devnum", 's', 0, G_OPTION_ARG_CALLBACK, &parse_busnum_devnum,
185 "Select device by bus and device number (in decimal).",
186 "[BUS:]DEV"
187 },
188 { "vid-pid", 'd', 0, G_OPTION_ARG_CALLBACK, &parse_vid_pid,
189 "Select device by device vendor and product id (in hexadecimal).",
190 "VID[:PID]"
191 },
192 { "cdc-wdm", 'w', 0, G_OPTION_ARG_FILENAME, &cdc_wdm_str,
193 "Select device by QMI/MBIM cdc-wdm device path (e.g. /dev/cdc-wdm0).",
194 "[PATH]"
195 },
196 { "tty", 't', 0, G_OPTION_ARG_FILENAME, &tty_str,
197 "Select device by serial device path (e.g. /dev/ttyUSB2).",
198 "[PATH]"
199 },
200 { NULL, 0, 0, 0, NULL, NULL, NULL }
201 };
202
203 static GOptionEntry context_update_entries[] = {
204 { "update", 'u', 0, G_OPTION_ARG_NONE, &action_update_flag,
205 "Launch firmware update process.",
206 NULL
207 },
208 { "firmware-version", 'f', 0, G_OPTION_ARG_STRING, &firmware_version_str,
209 "Firmware version (e.g. '05.05.58.00').",
210 "[VERSION]",
211 },
212 { "config-version", 'c', 0, G_OPTION_ARG_STRING, &config_version_str,
213 "Config version (e.g. '005.025_002').",
214 "[VERSION]",
215 },
216 { "carrier", 'C', 0, G_OPTION_ARG_STRING, &carrier_str,
217 "Carrier name (e.g. 'Generic').",
218 "[CARRIER]",
219 },
220 { "ignore-version-errors", 0, 0, G_OPTION_ARG_NONE, &ignore_version_errors_flag,
221 "Run update operation even with version string errors.",
222 NULL
223 },
224 { "override-download", 0, 0, G_OPTION_ARG_NONE, &override_download_flag,
225 "Download images even if module says it already has them.",
226 NULL
227 },
228 { "modem-storage-index", 0, 0, G_OPTION_ARG_INT, &modem_storage_index_int,
229 "Index storage for the modem image.",
230 "[INDEX]"
231 },
232 { "skip-validation", 0, 0, G_OPTION_ARG_NONE, &skip_validation_flag,
233 "Don't wait to validate the running firmware after update.",
234 NULL
235 },
236 { NULL, 0, 0, 0, NULL, NULL, NULL }
237 };
238
239 static GOptionEntry context_reset_entries[] = {
240 { "reset", 'b', 0, G_OPTION_ARG_NONE, &action_reset_flag,
241 "Reset device into download mode.",
242 NULL
243 },
244 { NULL, 0, 0, 0, NULL, NULL, NULL }
245 };
246
247 static GOptionEntry context_update_download_entries[] = {
248 { "update-download", 'U', 0, G_OPTION_ARG_NONE, &action_update_download_flag,
249 "Launch firmware update process while in download (boot & hold) mode.",
250 NULL
251 },
252 { NULL, 0, 0, 0, NULL, NULL, NULL }
253 };
254
255 static GOptionEntry context_verify_entries[] = {
256 { "verify", 'z', 0, G_OPTION_ARG_NONE, &action_verify_flag,
257 "Analyze and verify firmware images.",
258 NULL
259 },
260 { NULL, 0, 0, 0, NULL, NULL, NULL }
261 };
262
263 static GOptionEntry context_main_entries[] = {
264 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &image_strv, "",
265 "FILE1 FILE2..."
266 },
267 { "device-open-proxy", 'p', 0, G_OPTION_ARG_NONE, &device_open_proxy_flag,
268 "Request to use the 'qmi-proxy' proxy.",
269 NULL
270 },
271 { "device-open-qmi", 0, 0, G_OPTION_ARG_NONE, &device_open_qmi_flag,
272 "Open a cdc-wdm device explicitly in QMI mode",
273 NULL
274 },
275 { "device-open-mbim", 0, 0, G_OPTION_ARG_NONE, &device_open_mbim_flag,
276 "Open a cdc-wdm device explicitly in MBIM mode",
277 NULL
278 },
279 { "device-open-auto", 0, 0, G_OPTION_ARG_NONE, &device_open_auto_flag,
280 "Open a cdc-wdm device in either QMI or MBIM mode (default)",
281 NULL
282 },
283 #if defined MM_RUNTIME_CHECK_ENABLED
284 { "ignore-mm-runtime-check", 0, 0, G_OPTION_ARG_NONE, &ignore_mm_runtime_check_flag,
285 "Ignore ModemManager runtime check",
286 NULL
287 },
288 #endif
289 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &stdout_verbose_flag,
290 "Run action with verbose messages in standard output, including the debug ones.",
291 NULL
292 },
293 { "silent", 'S', 0, G_OPTION_ARG_NONE, &stdout_silent_flag,
294 "Run action with no messages in standard output; not even the error ones.",
295 NULL
296 },
297 { "verbose-log", 'L', 0, G_OPTION_ARG_FILENAME, &verbose_log_str,
298 "Write verbose messages to an output file.",
299 "[PATH]"
300 },
301 { "version", 'V', 0, G_OPTION_ARG_NONE, &version_flag,
302 "Print version.",
303 NULL
304 },
305 { "help", 'h', 0, G_OPTION_ARG_NONE, &help_flag,
306 "Show help.",
307 NULL
308 },
309 { "help-examples", 'H', 0, G_OPTION_ARG_NONE, &help_examples_flag,
310 "Show help examples.",
311 NULL
312 },
313 { NULL, 0, 0, 0, NULL, NULL, NULL }
314 };
315
316 static const gchar *context_description =
317 " ***************************************************************************\n"
318 " Warning!\n"
319 " ***************************************************************************\n"
320 "\n"
321 " Use this program with caution. The authors take *no* responsibility if any\n"
322 " device gets broken as a result of using this program.\n"
323 "\n"
324 " Please report issues to the libqmi mailing list at:\n"
325 " [email protected]\n";
326
327 /*****************************************************************************/
328
329 static void
330 print_version (void)
331 {
332 g_print ("\n"
333 PROGRAM_NAME " " PROGRAM_VERSION "\n"
334 "\n"
335 " Copyright (C) 2016-2022 Aleksander Morgado\n"
336 " Copyright (C) 2022 VMware, Inc.\n"
337 " Copyright (C) 2016-2019 Bjørn Mork\n"
338 " Copyright (C) 2016-2018 Zodiac Inflight Innovations\n"
339 "\n"
340 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n"
341 "This is free software: you are free to change and redistribute it.\n"
342 "There is NO WARRANTY, to the extent permitted by law.\n"
343 "\n");
344 }
345
346 static void
347 print_help (GOptionContext *context)
348 {
349 gchar *str;
350
351 /* Always print --help-all */
352 str = g_option_context_get_help (context, FALSE, NULL);
353 g_print ("%s", str);
354 g_free (str);
355 }
356
357 static void
358 print_help_examples (void)
359 {
360 g_print ("\n"
361 "********************************************************************************\n"
362 "\n"
363 " Example: Updating a Sierra Wireless MC7354.\n"
364 " (or other 9x15 or 9x30 devices, like the MC7304, MC7330, MC7455... ).\n"
365 "\n"
366 " The MC7354 is a 9x15 device which requires the firmware updater to specify the\n"
367 " firmware version string, the config version string and the carrier string, so\n"
368 " that they are included as identifiers of the firmware images downloaded. The\n"
369 " core logic in the application will try to automatically detect these strings,\n"
370 " although the user can also use specific options to override them or if the\n"
371 " automatic detection failed.\n"
372 "\n"
373 " While in normal operation, the device will expose multiple cdc-wdm ports, and\n"
374 " the updater application just needs one of those cdc-wdm ports to start the\n"
375 " operation. The user can explicitly specify the cdc-wdm port to use, or\n"
376 " otherwise use the generic device selection options (i.e. --busnum-devnum or\n"
377 " --vid-pid) to do that automatically.\n"
378 "\n"
379 " Note that the firmware for the MC7354 is usually composed of a core system image\n"
380 " (.cwe) and a carrier-specific image (.nvu). These two images need to be flashed\n"
381 " on the same operation, unless upgrading the carrier-specific image on a device\n"
382 " which already has the matching firmware version. The two images may be given\n"
383 " combined into a single image (.spk) file.\n"
384 "\n"
385 " a) An update operation specifying the vid:pid of the device (fails if multiple\n"
386 " devices with the same vid:pid are found):\n"
387 " $ sudo " PROGRAM_NAME " \\\n"
388 " --update \\\n"
389 " -d 1199:68c0 \\\n"
390 " SWI9X15C_05.05.58.00.cwe \\\n"
391 " SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
392 "\n"
393 " b) An update operation specifying an explicit QMI cdc-wdm device:\n"
394 " $ sudo " PROGRAM_NAME " \\\n"
395 " --update \\\n"
396 " --cdc-wdm /dev/cdc-wdm0 \\\n"
397 " SWI9X15C_05.05.58.00.cwe \\\n"
398 " SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
399 "\n"
400 " c) An update operation specifying explicit firmware, config and carrier strings:\n"
401 " $ sudo " PROGRAM_NAME " \\\n"
402 " --update \\\n"
403 " -d 1199:68c0 \\\n"
404 " --firmware-version 05.05.58.00 \\\n"
405 " --config-version 005.025_002 \\\n"
406 " --carrier Generic \\\n"
407 " SWI9X15C_05.05.58.00.cwe \\\n"
408 " SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
409 "\n"
410 " d) An update operation with a combined image containing both system and carrier\n"
411 " specific images::\n"
412 " $ sudo " PROGRAM_NAME " \\\n"
413 " --update \\\n"
414 " -d 1199:68c0 \\\n"
415 " 9999999_9902574_SWI9X15C_05.05.66.00_00_GENNA-UMTS_005.028_000-field.spk\n");
416
417 g_print ("\n"
418 "********************************************************************************\n"
419 "\n"
420 " Example: Updating a Sierra Wireless MC7700.\n"
421 " (or other 9200 devices, like the MC7710).\n"
422 "\n"
423 " The MC7700 is a 9200 device which doesn't require the explicit firmware, config\n"
424 " and carrier strings. Unlike the MC7354, which would reboot itself into QDL\n"
425 " download mode once these previous strings were configured, the MC7700 requires\n"
426 " a specific \"boot and hold\" command to be sent (either via QMI or AT) to request\n"
427 " the reset in QDL download mode.\n"
428 "\n"
429 " a) An update operation specifying the vid:pid of the device (fails if multiple\n"
430 " devices with the same vid:pid are found):\n"
431 " $ sudo " PROGRAM_NAME " \\\n"
432 " --update \\\n"
433 " -d 1199:68a2 \\\n"
434 " 9999999_9999999_9200_03.05.14.00_00_generic_000.000_001_SPKG_MC.cwe\n"
435 "\n"
436 " b) An update operation specifying an explicit QMI cdc-wdm device:\n"
437 " $ sudo " PROGRAM_NAME " \\\n"
438 " --update \\\n"
439 " --cdc-wdm /dev/cdc-wdm0 \\\n"
440 " 9999999_9999999_9200_03.05.14.00_00_generic_000.000_001_SPKG_MC.cwe\n");
441
442 g_print ("\n"
443 "********************************************************************************\n"
444 "\n"
445 " Example: Manual process to update a Sierra Wireless MC7354.\n"
446 " (or other 9x15 or 9x30 devices, like the MC7304, MC7330, MC7455... ).\n"
447 "\n"
448 " The upgrade of devices from the 9x15 and 9x30 families is triggered via a\n"
449 " 'firmware preference' setting. If the device accepts the setting, the user\n"
450 " can request a device power cycle, which will boot in QDL download mode:\n"
451 "\n"
452 " a) Set firmware preference setting:\n"
453 " $ sudo qmicli \\\n"
454 " -d /dev/cdc-wdm0 \\\n"
455 " --dms-set-firmware-preference=\"firmware-version=05.05.58.00, \\\n"
456 " config-version=005.025_002, \\\n"
457 " carrier=Generic\"\n"
458 "\n"
459 " The --override-download and --modem-storage-index=[INDEX] options that are\n"
460 " supported in automatic mode may be used in manual mode by passing the\n"
461 " additional 'override-download=yes' and 'modem-storage-index=[INDEX]' keys to the \n"
462 " --dms-set-firmware-preference operation.\n"
463 "\n"
464 " b) Request power cycle:\n"
465 " $ sudo qmicli \\\n"
466 " -d /dev/cdc-wdm0 \\\n"
467 " --dms-set-operating-mode=offline\n"
468 " $ sudo qmicli \\\n"
469 " -d /dev/cdc-wdm0 \\\n"
470 " --dms-set-operating-mode=reset\n"
471 "\n"
472 " c) Wait for the /dev/ttyUSB device to appear.\n"
473 "\n"
474 " d) Run updater operation while in QDL download mode:\n"
475 " $ sudo " PROGRAM_NAME " \\\n"
476 " -t /dev/ttyUSB0 \\\n"
477 " --update-download \\\n"
478 " SWI9X15C_05.05.58.00.cwe \\\n"
479 " SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
480 "\n"
481 " d) Now wait for the device to fully reboot, may take up to several minutes.\n");
482
483 g_print ("\n"
484 "********************************************************************************\n"
485 "\n"
486 " Example: Manual process to update a Sierra Wireless MC7700.\n"
487 " (or other 9200 devices, like the MC7710).\n"
488 "\n"
489 " Instead of setting a 'firmware preference', the devices from the 9200 family\n"
490 " can just be rebooted in QDL mode:\n"
491 "\n"
492 " a) Request device to go into QDL download mode:\n"
493 " $ sudo " PROGRAM_NAME " \\\n"
494 " -d 1199:68a2 \\\n"
495 " --reset\n"
496 "\n"
497 " b) Wait for the /dev/ttyUSB device to appear.\n"
498 "\n"
499 " c) Run updater operation while in QDL download mode:\n"
500 " $ sudo " PROGRAM_NAME " \\\n"
501 " -d 1199:68a2 \\\n"
502 " --update-download \\\n"
503 " 9999999_9999999_9200_03.05.14.00_00_generic_000.000_001_SPKG_MC.cwe\n"
504 "\n"
505 " d) Now wait for the device to fully reboot, may take up to several minutes.\n");
506
507
508 g_print ("\n"
509 "********************************************************************************\n"
510 "\n"
511 " Example: Verify firmware images.\n"
512 "\n"
513 " a) Verify several images at once:\n"
514 " $ " PROGRAM_NAME " \\\n"
515 " --verify \\\n"
516 " SWI9X15C_05.05.58.00.cwe \\\n"
517 " SWI9X15C_05.05.58.00_Generic_005.025_002.nvu\n"
518 "\n"
519 " b) Verify all .cwe, .nvu and .spk images inside a directory:\n"
520 " $ find . -regex \".*\\.\\(nvu\\|spk\\|cwe\\)\" -exec " PROGRAM_NAME " -v -z {} \\;\n"
521 "\n"
522 " c) Image files may be given within .exe files; extract them with 7-Zip:\n"
523 " $ 7z x SWI9200M_3.5-Release13-SWI9200X_03.05.29.03.exe\n"
524 " $ ls *.{cwe,nvu,spk} 2>/dev/null\n"
525 " 9999999_9999999_9200_03.05.29.03_00_generic_000.000_001_SPKG_MC.cwe\n"
526 "\n");
527 }
528
529 /*****************************************************************************/
530
531 int main (int argc, char **argv)
532 {
533 GError *error = NULL;
534 GOptionContext *context;
535 GOptionGroup *group;
536 guint n_actions;
537 guint n_actions_images_needed;
538 guint n_actions_device_needed;
539 guint n_actions_cdc_wdm_needed;
540 gboolean result = FALSE;
541 QfuDeviceSelection *device_selection = NULL;
542 QmiDeviceOpenFlags device_open_flags = QMI_DEVICE_OPEN_FLAGS_NONE;
543
544 setlocale (LC_ALL, "");
545
546 /* Setup option context, process it and destroy it */
547 context = g_option_context_new ("- Update firmware in QMI devices");
548 g_option_context_set_description (context, context_description);
549
550 group = g_option_group_new ("selection", "Generic device selection options:", "", NULL, NULL);
551 g_option_group_add_entries (group, context_selection_entries);
552 g_option_context_add_group (context, group);
553
554 group = g_option_group_new ("update", "Update options (normal mode):", "", NULL, NULL);
555 g_option_group_add_entries (group, context_update_entries);
556 g_option_context_add_group (context, group);
557
558 group = g_option_group_new ("reset", "Reset options (normal mode):", "", NULL, NULL);
559 g_option_group_add_entries (group, context_reset_entries);
560 g_option_context_add_group (context, group);
561
562 group = g_option_group_new ("update-download", "Update options (download mode):", "", NULL, NULL);
563 g_option_group_add_entries (group, context_update_download_entries);
564 g_option_context_add_group (context, group);
565
566 group = g_option_group_new ("verify", "Verify options:", "", NULL, NULL);
567 g_option_group_add_entries (group, context_verify_entries);
568 g_option_context_add_group (context, group);
569
570 g_option_context_add_main_entries (context, context_main_entries, NULL);
571 g_option_context_set_help_enabled (context, FALSE);
572
573 if (!g_option_context_parse (context, &argc, &argv, &error)) {
574 g_printerr ("error: couldn't parse option context: %s\n", error->message);
575 g_error_free (error);
576 goto out;
577 }
578
579 if (version_flag) {
580 print_version ();
581 result = TRUE;
582 goto out;
583 }
584
585 if (help_flag) {
586 print_help (context);
587 result = TRUE;
588 goto out;
589 }
590
591 if (help_examples_flag) {
592 print_help_examples ();
593 result = TRUE;
594 goto out;
595 }
596
597 /* Initialize logging */
598 qfu_log_init (stdout_verbose_flag, stdout_silent_flag, verbose_log_str);
599
600 /* Total actions */
601 n_actions = (action_verify_flag + action_update_download_flag + action_reset_flag);
602 /* Actions that require images specified */
603 n_actions_images_needed = (action_verify_flag + action_update_download_flag);
604 /* Actions that require a device specified */
605 n_actions_device_needed = (action_update_download_flag + action_reset_flag);
606 /* Actions that allow using a cdc-wdm device */
607 n_actions_cdc_wdm_needed = (action_reset_flag);
608
609 n_actions += action_update_flag;
610 n_actions_cdc_wdm_needed += action_update_flag;
611 n_actions_images_needed += action_update_flag;
612 n_actions_device_needed += action_update_flag;
613
614 /* We don't allow multiple actions at the same time */
615 if (n_actions == 0) {
616 g_printerr ("error: no actions specified\n");
617 goto out;
618 }
619 if (n_actions > 1) {
620 g_printerr ("error: too many actions specified\n");
621 goto out;
622 }
623
624 /* A list of images must be provided for update and verify operations */
625 if (n_actions_images_needed && !image_strv) {
626 g_printerr ("error: no firmware images specified\n");
627 goto out;
628 }
629
630 /* device selection must be performed for update and reset operations */
631 if (n_actions_device_needed) {
632 device_selection = qfu_device_selection_new (cdc_wdm_str, tty_str, vid, pid, busnum, devnum, &error);
633 if (!device_selection) {
634 g_printerr ("error: couldn't select device:: %s\n", error->message);
635 g_error_free (error);
636 goto out;
637 }
638
639 #if defined MM_RUNTIME_CHECK_ENABLED
640 /* For all those actions that require a device, we will be doing MM runtime checks */
641 if (!ignore_mm_runtime_check_flag) {
642 gboolean mm_running;
643 gboolean abort_needed = FALSE;
644
645 if (!qfu_utils_modemmanager_running (&mm_running, &error)) {
646 g_printerr ("error: couldn't check if ModemManager running: %s\n", error->message);
647 g_error_free (error);
648 abort_needed = TRUE;
649 } else if (mm_running) {
650 g_printerr ("error: ModemManager is running: cannot perform firmware upgrade operation at the same time\n");
651 abort_needed = TRUE;
652 }
653
654 if (abort_needed) {
655 g_printerr ("\n"
656 "Note: in order to do a clean firmware upgrade, ModemManager must NOT be running;\n"
657 "please stop it and retry. On a 'systemd' based system this may be done as follows:\n"
658 " # systemctl stop ModemManager\n"
659 "\n"
660 "You can also ignore this runtime check (at your own risk) using --ignore-mm-runtime-check.\n"
661 "\n");
662 goto out;
663 }
664 }
665 #endif
666 }
667
668 /* Validate device open flags */
669 if (n_actions_cdc_wdm_needed) {
670 if (device_open_mbim_flag + device_open_qmi_flag + device_open_auto_flag > 1) {
671 g_printerr ("error: cannot specify multiple mode flags to open device\n");
672 goto out;
673 }
674 if (device_open_proxy_flag)
675 device_open_flags |= QMI_DEVICE_OPEN_FLAGS_PROXY;
676 if (device_open_mbim_flag)
677 device_open_flags |= QMI_DEVICE_OPEN_FLAGS_MBIM;
678 if (device_open_auto_flag || (!device_open_qmi_flag && !device_open_mbim_flag))
679 device_open_flags |= QMI_DEVICE_OPEN_FLAGS_AUTO;
680 }
681
682 /* Run */
683
684 if (action_update_flag) {
685 g_assert (QFU_IS_DEVICE_SELECTION (device_selection));
686
687 /* Validate storage index, just (0,G_MAXUINT8] for now. The value 0 is also not
688 * valid, but we use it to flag when no specific index has been requested. */
689 if (modem_storage_index_int < 0 || modem_storage_index_int > G_MAXUINT8) {
690 g_printerr ("error: invalid modem storage index\n");
691 goto out;
692 }
693
694 result = qfu_operation_update_run ((const gchar **) image_strv,
695 device_selection,
696 firmware_version_str,
697 config_version_str,
698 carrier_str,
699 device_open_flags,
700 ignore_version_errors_flag,
701 override_download_flag,
702 (guint8) modem_storage_index_int,
703 skip_validation_flag);
704 goto out;
705 }
706
707 if (action_update_download_flag) {
708 g_assert (QFU_IS_DEVICE_SELECTION (device_selection));
709 result = qfu_operation_update_download_run ((const gchar **) image_strv,
710 device_selection);
711 goto out;
712 }
713
714 if (action_reset_flag) {
715 g_assert (QFU_IS_DEVICE_SELECTION (device_selection));
716 result = qfu_operation_reset_run (device_selection, device_open_flags);
717 goto out;
718 }
719
720 if (action_verify_flag) {
721 result = qfu_operation_verify_run ((const gchar **) image_strv);
722 goto out;
723 }
724
725 g_assert_not_reached ();
726
727 out:
728
729 qfu_log_shutdown ();
730
731 /* Clean exit for a clean memleak report */
732 if (context)
733 g_option_context_free (context);
734 if (device_selection)
735 g_object_unref (device_selection);
736
737 return (result ? EXIT_SUCCESS : EXIT_FAILURE);
738 }