| 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 <glib-object.h> |
| 23 |
#include <gio/gio.h> |
| 24 |
#include <glib-unix.h> |
| 25 |
|
| 26 |
#include "qfu-operation.h" |
| 27 |
#include "qfu-updater.h" |
| 28 |
|
| 29 |
typedef struct { |
| 30 |
GMainLoop *loop; |
| 31 |
GCancellable *cancellable; |
| 32 |
gboolean result; |
| 33 |
} UpdateOperation; |
| 34 |
|
| 35 |
static gboolean |
| 36 |
signal_handler (UpdateOperation *operation) |
| 37 |
{ |
| 38 |
operation->result = FALSE; |
| 39 |
|
| 40 |
/* Ignore consecutive requests of cancellation */ |
| 41 |
if (!g_cancellable_is_cancelled (operation->cancellable)) { |
| 42 |
g_printerr ("cancelling the operation...\n"); |
| 43 |
g_cancellable_cancel (operation->cancellable); |
| 44 |
/* Reset the signal handler to allow main loop cancellation on |
| 45 |
* second signal */ |
| 46 |
return G_SOURCE_CONTINUE; |
| 47 |
} |
| 48 |
|
| 49 |
if (g_main_loop_is_running (operation->loop)) { |
| 50 |
g_printerr ("cancelling the main loop...\n"); |
| 51 |
g_main_loop_quit (operation->loop); |
| 52 |
} |
| 53 |
|
| 54 |
return G_SOURCE_REMOVE; |
| 55 |
} |
| 56 |
|
| 57 |
static void |
| 58 |
run_ready (QfuUpdater *updater, |
| 59 |
GAsyncResult *res, |
| 60 |
UpdateOperation *operation) |
| 61 |
{ |
| 62 |
GError *error = NULL; |
| 63 |
|
| 64 |
if (!qfu_updater_run_finish (updater, res, &error)) { |
| 65 |
g_printerr ("error: %s\n", error->message); |
| 66 |
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED)) |
| 67 |
g_printerr ("note: you can ignore this error using --ignore-version-errors\n"); |
| 68 |
g_error_free (error); |
| 69 |
} else { |
| 70 |
g_print ("firmware update operation finished successfully\n"); |
| 71 |
operation->result = TRUE; |
| 72 |
} |
| 73 |
|
| 74 |
g_idle_add ((GSourceFunc) g_main_loop_quit, operation->loop); |
| 75 |
} |
| 76 |
|
| 77 |
static gboolean |
| 78 |
operation_update_run (QfuUpdater *updater, |
| 79 |
const gchar **images) |
| 80 |
{ |
| 81 |
UpdateOperation operation = { |
| 82 |
.loop = NULL, |
| 83 |
.cancellable = NULL, |
| 84 |
.result = FALSE, |
| 85 |
}; |
| 86 |
GList *image_file_list = NULL; |
| 87 |
guint i; |
| 88 |
|
| 89 |
g_assert (images); |
| 90 |
g_assert (QFU_IS_UPDATER (updater)); |
| 91 |
|
| 92 |
/* Create runtime context */ |
| 93 |
operation.loop = g_main_loop_new (NULL, FALSE); |
| 94 |
operation.cancellable = g_cancellable_new (); |
| 95 |
|
| 96 |
/* Setup signals */ |
| 97 |
g_unix_signal_add (SIGINT, (GSourceFunc) signal_handler, &operation); |
| 98 |
g_unix_signal_add (SIGHUP, (GSourceFunc) signal_handler, &operation); |
| 99 |
g_unix_signal_add (SIGTERM, (GSourceFunc) signal_handler, &operation); |
| 100 |
|
| 101 |
/* Create list of image files */ |
| 102 |
for (i = 0; images[i]; i++) |
| 103 |
image_file_list = g_list_append (image_file_list, g_file_new_for_commandline_arg (images[i])); |
| 104 |
|
| 105 |
/* Run! */ |
| 106 |
qfu_updater_run (updater, image_file_list, operation.cancellable, (GAsyncReadyCallback) run_ready, &operation); |
| 107 |
g_list_free_full (image_file_list, g_object_unref); |
| 108 |
g_main_loop_run (operation.loop); |
| 109 |
|
| 110 |
if (operation.cancellable) |
| 111 |
g_object_unref (operation.cancellable); |
| 112 |
if (operation.loop) |
| 113 |
g_main_loop_unref (operation.loop); |
| 114 |
|
| 115 |
return operation.result; |
| 116 |
} |
| 117 |
|
| 118 |
gboolean |
| 119 |
qfu_operation_update_run (const gchar **images, |
| 120 |
QfuDeviceSelection *device_selection, |
| 121 |
const gchar *firmware_version, |
| 122 |
const gchar *config_version, |
| 123 |
const gchar *carrier, |
| 124 |
QmiDeviceOpenFlags device_open_flags, |
| 125 |
gboolean ignore_version_errors, |
| 126 |
gboolean override_download, |
| 127 |
guint8 modem_storage_index, |
| 128 |
gboolean skip_validation) |
| 129 |
{ |
| 130 |
QfuUpdater *updater = NULL; |
| 131 |
gboolean result; |
| 132 |
|
| 133 |
g_assert (images); |
| 134 |
|
| 135 |
updater = qfu_updater_new (device_selection, |
| 136 |
firmware_version, |
| 137 |
config_version, |
| 138 |
carrier, |
| 139 |
device_open_flags, |
| 140 |
ignore_version_errors, |
| 141 |
override_download, |
| 142 |
modem_storage_index, |
| 143 |
skip_validation); |
| 144 |
result = operation_update_run (updater, images); |
| 145 |
g_object_unref (updater); |
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
gboolean |
| 150 |
qfu_operation_update_download_run (const gchar **images, |
| 151 |
QfuDeviceSelection *device_selection) |
| 152 |
{ |
| 153 |
QfuUpdater *updater = NULL; |
| 154 |
gboolean result; |
| 155 |
|
| 156 |
g_assert (images); |
| 157 |
|
| 158 |
updater = qfu_updater_new_download (device_selection); |
| 159 |
result = operation_update_run (updater, images); |
| 160 |
g_object_unref (updater); |
| 161 |
return result; |
| 162 |
} |