| 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 Bjørn Mork <[email protected]> |
| 19 |
* Copyright (C) 2016 Zodiac Inflight Innovations |
| 20 |
* Copyright (C) 2016-2017 Aleksander Morgado <[email protected]> |
| 21 |
* |
| 22 |
* crc16 and HDLC escape code borrowed from modemmanager/libqcdm |
| 23 |
* Copyright (C) 2010 Red Hat, Inc. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#include <glib.h> |
| 30 |
#include <gio/gio.h> |
| 31 |
|
| 32 |
#include "qfu-utils.h" |
| 33 |
|
| 34 |
/******************************************************************************/ |
| 35 |
|
| 36 |
gchar * |
| 37 |
qfu_utils_str_hex (gconstpointer mem, |
| 38 |
gsize size, |
| 39 |
gchar delimiter) |
| 40 |
{ |
| 41 |
const guint8 *data = mem; |
| 42 |
gsize i; |
| 43 |
gsize j; |
| 44 |
gsize new_str_length; |
| 45 |
gchar *new_str; |
| 46 |
|
| 47 |
/* Get new string length. If input string has N bytes, we need: |
| 48 |
* - 1 byte for last NUL char |
| 49 |
* - 2N bytes for hexadecimal char representation of each byte... |
| 50 |
* - N-1 bytes for the separator ':' |
| 51 |
* So... a total of (1+2N+N-1) = 3N bytes are needed... */ |
| 52 |
new_str_length = 3 * size; |
| 53 |
|
| 54 |
/* Allocate memory for new array and initialize contents to NUL */ |
| 55 |
new_str = g_malloc0 (new_str_length); |
| 56 |
|
| 57 |
/* Print hexadecimal representation of each byte... */ |
| 58 |
for (i = 0, j = 0; i < size; i++, j += 3) { |
| 59 |
/* Print character in output string... */ |
| 60 |
snprintf (&new_str[j], 3, "%02X", data[i]); |
| 61 |
/* And if needed, add separator */ |
| 62 |
if (i != (size - 1) ) |
| 63 |
new_str[j + 2] = delimiter; |
| 64 |
} |
| 65 |
|
| 66 |
/* Set output string */ |
| 67 |
return new_str; |
| 68 |
} |
| 69 |
|
| 70 |
/******************************************************************************/ |
| 71 |
|
| 72 |
gchar * |
| 73 |
qfu_utils_get_firmware_image_unique_id_printable (const GArray *unique_id) |
| 74 |
{ |
| 75 |
gchar *unique_id_str; |
| 76 |
guint i; |
| 77 |
guint n_ascii = 0; |
| 78 |
gboolean end = FALSE; |
| 79 |
|
| 80 |
#define UNIQUE_ID_LEN 16 |
| 81 |
|
| 82 |
g_warn_if_fail (unique_id->len <= UNIQUE_ID_LEN); |
| 83 |
unique_id_str = g_malloc0 (UNIQUE_ID_LEN + 1); |
| 84 |
memcpy (unique_id_str, unique_id->data, UNIQUE_ID_LEN); |
| 85 |
|
| 86 |
/* We want an ASCII string that, if finished before the 16 bytes, |
| 87 |
* is suffixed with NUL bytes. */ |
| 88 |
for (i = 0; i < UNIQUE_ID_LEN; i++) { |
| 89 |
/* If a byte isn't ASCII, stop */ |
| 90 |
if (unique_id_str[i] & 0x80) |
| 91 |
break; |
| 92 |
/* If string isn't finished yet... */ |
| 93 |
if (!end) { |
| 94 |
/* String finished now */ |
| 95 |
if (unique_id_str[i] == '\0') |
| 96 |
end = TRUE; |
| 97 |
else |
| 98 |
n_ascii++; |
| 99 |
} else { |
| 100 |
/* String finished but we then got |
| 101 |
* another ASCII byte? not possible */ |
| 102 |
if (unique_id_str[i] != '\0') |
| 103 |
break; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (i == UNIQUE_ID_LEN && n_ascii > 0) |
| 108 |
return unique_id_str; |
| 109 |
|
| 110 |
#undef UNIQUE_ID_LEN |
| 111 |
|
| 112 |
g_free (unique_id_str); |
| 113 |
|
| 114 |
/* Get a raw hex string otherwise */ |
| 115 |
unique_id_str = qfu_utils_str_hex (unique_id->data, unique_id->len, ':'); |
| 116 |
|
| 117 |
return unique_id_str; |
| 118 |
} |
| 119 |
|
| 120 |
/******************************************************************************/ |
| 121 |
|
| 122 |
/* Table of CRCs for each possible byte, with a generator polynomial of 0x8408 */ |
| 123 |
static const guint16 crc_table[256] = { |
| 124 |
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, |
| 125 |
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, |
| 126 |
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, |
| 127 |
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, |
| 128 |
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, |
| 129 |
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, |
| 130 |
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, |
| 131 |
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, |
| 132 |
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, |
| 133 |
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, |
| 134 |
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, |
| 135 |
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, |
| 136 |
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, |
| 137 |
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, |
| 138 |
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, |
| 139 |
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, |
| 140 |
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, |
| 141 |
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, |
| 142 |
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, |
| 143 |
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, |
| 144 |
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, |
| 145 |
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, |
| 146 |
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, |
| 147 |
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, |
| 148 |
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, |
| 149 |
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, |
| 150 |
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, |
| 151 |
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, |
| 152 |
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, |
| 153 |
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, |
| 154 |
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, |
| 155 |
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 |
| 156 |
}; |
| 157 |
|
| 158 |
/* Calculate the CRC for a buffer using a seed of 0xffff */ |
| 159 |
guint16 |
| 160 |
qfu_utils_crc16 (const guint8 *buffer, |
| 161 |
gsize len) |
| 162 |
{ |
| 163 |
guint16 crc = 0xffff; |
| 164 |
|
| 165 |
while (len--) |
| 166 |
crc = crc_table[(crc ^ *buffer++) & 0xff] ^ (crc >> 8); |
| 167 |
return ~crc; |
| 168 |
} |
| 169 |
|
| 170 |
/******************************************************************************/ |
| 171 |
|
| 172 |
gboolean |
| 173 |
qfu_utils_parse_cwe_version_string (const gchar *version, |
| 174 |
gchar **firmware_version, |
| 175 |
gchar **config_version, |
| 176 |
gchar **carrier, |
| 177 |
GError **error) |
| 178 |
{ |
| 179 |
GRegex *regex; |
| 180 |
GMatchInfo *match_info = NULL; |
| 181 |
gboolean result = FALSE; |
| 182 |
|
| 183 |
regex = g_regex_new ("(?:.*)" |
| 184 |
"_([0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9])" /* firmware version */ |
| 185 |
"(?:" |
| 186 |
"(?:.*)" |
| 187 |
"_([a-zA-Z\\-]+)" /* carrier */ |
| 188 |
"_([0-9][0-9][0-9]\\.[0-9][0-9][0-9]_[0-9][0-9][0-9])" /* config version */ |
| 189 |
")?", |
| 190 |
0, 0, NULL); |
| 191 |
g_assert (regex); |
| 192 |
|
| 193 |
if (!g_regex_match_full (regex, version, strlen (version), 0, 0, &match_info, NULL)) { |
| 194 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 195 |
"couldn't parse CWE version string '%s': didn't match", |
| 196 |
version); |
| 197 |
goto out; |
| 198 |
} |
| 199 |
|
| 200 |
if (firmware_version) |
| 201 |
*firmware_version = g_match_info_fetch (match_info, 1); |
| 202 |
if (carrier) |
| 203 |
*carrier = (g_match_info_get_match_count (match_info) > 2 ? g_match_info_fetch (match_info, 2) : NULL); |
| 204 |
if (config_version) |
| 205 |
*config_version = (g_match_info_get_match_count (match_info) > 3 ? g_match_info_fetch (match_info, 3) : NULL); |
| 206 |
|
| 207 |
result = TRUE; |
| 208 |
|
| 209 |
out: |
| 210 |
g_match_info_free (match_info); |
| 211 |
g_regex_unref (regex); |
| 212 |
|
| 213 |
return result; |
| 214 |
} |
| 215 |
|
| 216 |
/******************************************************************************/ |
| 217 |
|
| 218 |
typedef struct { |
| 219 |
QmiDevice *qmi_device; |
| 220 |
QmiDeviceOpenFlags device_open_flags; |
| 221 |
gint qmi_client_retries; |
| 222 |
QmiClientDms *qmi_client; |
| 223 |
|
| 224 |
gboolean load_capabilities; |
| 225 |
gchar *revision; |
| 226 |
gboolean revision_done; |
| 227 |
gboolean supports_stored_image_management; |
| 228 |
gboolean supports_stored_image_management_done; |
| 229 |
guint8 max_storage_index; |
| 230 |
gboolean supports_firmware_preference_management; |
| 231 |
QmiMessageDmsGetFirmwarePreferenceOutput *firmware_preference; |
| 232 |
gboolean supports_firmware_preference_management_done; |
| 233 |
QmiMessageDmsSwiGetCurrentFirmwareOutput *current_firmware; |
| 234 |
gboolean supports_current_firmware_done; |
| 235 |
} NewClientDmsContext; |
| 236 |
|
| 237 |
static void |
| 238 |
new_client_dms_context_free (NewClientDmsContext *ctx) |
| 239 |
{ |
| 240 |
if (ctx->current_firmware) |
| 241 |
qmi_message_dms_swi_get_current_firmware_output_unref (ctx->current_firmware); |
| 242 |
if (ctx->firmware_preference) |
| 243 |
qmi_message_dms_get_firmware_preference_output_unref (ctx->firmware_preference); |
| 244 |
if (ctx->qmi_client) |
| 245 |
g_object_unref (ctx->qmi_client); |
| 246 |
if (ctx->qmi_device) |
| 247 |
g_object_unref (ctx->qmi_device); |
| 248 |
g_free (ctx->revision); |
| 249 |
g_slice_free (NewClientDmsContext, ctx); |
| 250 |
} |
| 251 |
|
| 252 |
gboolean |
| 253 |
qfu_utils_new_client_dms_finish (GAsyncResult *res, |
| 254 |
QmiDevice **qmi_device, |
| 255 |
QmiClientDms **qmi_client, |
| 256 |
gchar **revision, |
| 257 |
gboolean *supports_stored_image_management, |
| 258 |
guint8 *max_storage_index, |
| 259 |
gboolean *supports_firmware_preference_management, |
| 260 |
QmiMessageDmsGetFirmwarePreferenceOutput **firmware_preference, |
| 261 |
QmiMessageDmsSwiGetCurrentFirmwareOutput **current_firmware, |
| 262 |
GError **error) |
| 263 |
{ |
| 264 |
NewClientDmsContext *ctx; |
| 265 |
|
| 266 |
if (!g_task_propagate_boolean (G_TASK (res), error)) |
| 267 |
return FALSE; |
| 268 |
|
| 269 |
ctx = g_task_get_task_data (G_TASK (res)); |
| 270 |
if (qmi_device) |
| 271 |
*qmi_device = g_object_ref (ctx->qmi_device); |
| 272 |
if (qmi_client) |
| 273 |
*qmi_client = g_object_ref (ctx->qmi_client); |
| 274 |
if (revision) |
| 275 |
*revision = (ctx->revision ? g_strdup (ctx->revision) : NULL); |
| 276 |
if (supports_stored_image_management) |
| 277 |
*supports_stored_image_management = ctx->supports_stored_image_management; |
| 278 |
if (max_storage_index) |
| 279 |
*max_storage_index = ctx->max_storage_index; |
| 280 |
if (supports_firmware_preference_management) |
| 281 |
*supports_firmware_preference_management = ctx->supports_firmware_preference_management; |
| 282 |
if (firmware_preference) |
| 283 |
*firmware_preference = (ctx->firmware_preference ? qmi_message_dms_get_firmware_preference_output_ref (ctx->firmware_preference) : NULL); |
| 284 |
if (current_firmware) |
| 285 |
*current_firmware = (ctx->current_firmware ? qmi_message_dms_swi_get_current_firmware_output_ref (ctx->current_firmware) : NULL); |
| 286 |
return TRUE; |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
check_capabilities_done (GTask *task) |
| 291 |
{ |
| 292 |
NewClientDmsContext *ctx; |
| 293 |
|
| 294 |
ctx = g_task_get_task_data (task); |
| 295 |
|
| 296 |
if (!ctx->revision_done || |
| 297 |
!ctx->supports_stored_image_management_done || |
| 298 |
!ctx->supports_firmware_preference_management_done || |
| 299 |
!ctx->supports_current_firmware_done) |
| 300 |
return; |
| 301 |
|
| 302 |
/* All done! */ |
| 303 |
g_task_return_boolean (task, TRUE); |
| 304 |
g_object_unref (task); |
| 305 |
} |
| 306 |
|
| 307 |
static void |
| 308 |
dms_get_revision_ready (QmiClientDms *client, |
| 309 |
GAsyncResult *res, |
| 310 |
GTask *task) |
| 311 |
{ |
| 312 |
QmiMessageDmsGetRevisionOutput *output; |
| 313 |
const gchar *str = NULL; |
| 314 |
NewClientDmsContext *ctx; |
| 315 |
|
| 316 |
ctx = g_task_get_task_data (task); |
| 317 |
|
| 318 |
output = qmi_client_dms_get_revision_finish (client, res, NULL); |
| 319 |
if (output && qmi_message_dms_get_revision_output_get_result (output, NULL)) |
| 320 |
qmi_message_dms_get_revision_output_get_revision (output, &str, NULL); |
| 321 |
|
| 322 |
if (str) { |
| 323 |
g_debug ("[qfu,utils] current revision loaded: %s", str); |
| 324 |
ctx->revision = g_strdup (str); |
| 325 |
} |
| 326 |
|
| 327 |
if (output) |
| 328 |
qmi_message_dms_get_revision_output_unref (output); |
| 329 |
|
| 330 |
ctx->revision_done = TRUE; |
| 331 |
check_capabilities_done (task); |
| 332 |
} |
| 333 |
|
| 334 |
static void |
| 335 |
dms_list_stored_images_ready (QmiClientDms *client, |
| 336 |
GAsyncResult *res, |
| 337 |
GTask *task) |
| 338 |
{ |
| 339 |
QmiMessageDmsListStoredImagesOutput *output; |
| 340 |
NewClientDmsContext *ctx; |
| 341 |
|
| 342 |
ctx = g_task_get_task_data (task); |
| 343 |
|
| 344 |
output = qmi_client_dms_list_stored_images_finish (client, res, NULL); |
| 345 |
ctx->supports_stored_image_management = (output && qmi_message_dms_list_stored_images_output_get_result (output, NULL)); |
| 346 |
|
| 347 |
if (ctx->supports_stored_image_management) { |
| 348 |
GArray *array; |
| 349 |
guint i; |
| 350 |
|
| 351 |
qmi_message_dms_list_stored_images_output_get_list (output, &array, NULL); |
| 352 |
|
| 353 |
for (i = 0; i < array->len; i++) { |
| 354 |
QmiMessageDmsListStoredImagesOutputListImage *image; |
| 355 |
|
| 356 |
image = &g_array_index (array, QmiMessageDmsListStoredImagesOutputListImage, i); |
| 357 |
if (image->type == QMI_DMS_FIRMWARE_IMAGE_TYPE_MODEM) { |
| 358 |
ctx->max_storage_index = image->maximum_images; |
| 359 |
break; |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
if (output) |
| 365 |
qmi_message_dms_list_stored_images_output_unref (output); |
| 366 |
|
| 367 |
ctx->supports_stored_image_management_done = TRUE; |
| 368 |
check_capabilities_done (task); |
| 369 |
} |
| 370 |
|
| 371 |
static void |
| 372 |
dms_get_firmware_preference_ready (QmiClientDms *client, |
| 373 |
GAsyncResult *res, |
| 374 |
GTask *task) |
| 375 |
{ |
| 376 |
QmiMessageDmsGetFirmwarePreferenceOutput *output; |
| 377 |
NewClientDmsContext *ctx; |
| 378 |
|
| 379 |
ctx = g_task_get_task_data (task); |
| 380 |
|
| 381 |
output = qmi_client_dms_get_firmware_preference_finish (client, res, NULL); |
| 382 |
ctx->supports_firmware_preference_management = (output && qmi_message_dms_get_firmware_preference_output_get_result (output, NULL)); |
| 383 |
|
| 384 |
/* Log the current firmware preference */ |
| 385 |
if (ctx->supports_firmware_preference_management) { |
| 386 |
GArray *array; |
| 387 |
guint i; |
| 388 |
|
| 389 |
g_debug ("[qfu,utils] current firmware preference loaded:"); |
| 390 |
|
| 391 |
/* Store */ |
| 392 |
ctx->firmware_preference = qmi_message_dms_get_firmware_preference_output_ref (output); |
| 393 |
|
| 394 |
qmi_message_dms_get_firmware_preference_output_get_list (output, &array, NULL); |
| 395 |
|
| 396 |
if (array->len > 0) { |
| 397 |
for (i = 0; i < array->len; i++) { |
| 398 |
QmiMessageDmsGetFirmwarePreferenceOutputListImage *image; |
| 399 |
gchar *unique_id_str; |
| 400 |
|
| 401 |
image = &g_array_index (array, QmiMessageDmsGetFirmwarePreferenceOutputListImage, i); |
| 402 |
unique_id_str = qfu_utils_get_firmware_image_unique_id_printable (image->unique_id); |
| 403 |
|
| 404 |
g_debug ("[qfu,utils] [image %u]", i); |
| 405 |
g_debug ("[qfu,utils] \tImage type: '%s'", qmi_dms_firmware_image_type_get_string (image->type)); |
| 406 |
g_debug ("[qfu,utils] \tUnique ID: '%s'", unique_id_str); |
| 407 |
g_debug ("[qfu,utils] \tBuild ID: '%s'", image->build_id); |
| 408 |
|
| 409 |
g_free (unique_id_str); |
| 410 |
} |
| 411 |
} else |
| 412 |
g_debug ("[qfu,utils] no images specified"); |
| 413 |
} |
| 414 |
|
| 415 |
if (output) |
| 416 |
qmi_message_dms_get_firmware_preference_output_unref (output); |
| 417 |
|
| 418 |
ctx->supports_firmware_preference_management_done = TRUE; |
| 419 |
check_capabilities_done (task); |
| 420 |
} |
| 421 |
|
| 422 |
static void |
| 423 |
swi_get_current_firmware_ready (QmiClientDms *client, |
| 424 |
GAsyncResult *res, |
| 425 |
GTask *task) |
| 426 |
{ |
| 427 |
QmiMessageDmsSwiGetCurrentFirmwareOutput *output; |
| 428 |
NewClientDmsContext *ctx; |
| 429 |
|
| 430 |
ctx = g_task_get_task_data (task); |
| 431 |
|
| 432 |
output = qmi_client_dms_swi_get_current_firmware_finish (client, res, NULL); |
| 433 |
if (output) { |
| 434 |
if (qmi_message_dms_swi_get_current_firmware_output_get_result (output, NULL)) |
| 435 |
ctx->current_firmware = output; |
| 436 |
else |
| 437 |
qmi_message_dms_swi_get_current_firmware_output_unref (output); |
| 438 |
} |
| 439 |
|
| 440 |
ctx->supports_current_firmware_done = TRUE; |
| 441 |
check_capabilities_done (task); |
| 442 |
} |
| 443 |
|
| 444 |
static void retry_allocate_qmi_client (GTask *task); |
| 445 |
|
| 446 |
static void |
| 447 |
qmi_client_ready (QmiDevice *device, |
| 448 |
GAsyncResult *res, |
| 449 |
GTask *task) |
| 450 |
{ |
| 451 |
NewClientDmsContext *ctx; |
| 452 |
GError *error = NULL; |
| 453 |
|
| 454 |
ctx = (NewClientDmsContext *) g_task_get_task_data (task); |
| 455 |
|
| 456 |
ctx->qmi_client = QMI_CLIENT_DMS (qmi_device_allocate_client_finish (device, res, &error)); |
| 457 |
if (!ctx->qmi_client) { |
| 458 |
/* If this was the last attempt, error out */ |
| 459 |
if (!ctx->qmi_client_retries) { |
| 460 |
g_prefix_error (&error, "couldn't allocate DMS QMI client: "); |
| 461 |
g_task_return_error (task, error); |
| 462 |
g_object_unref (task); |
| 463 |
return; |
| 464 |
} |
| 465 |
|
| 466 |
/* Retry allocation */ |
| 467 |
g_debug ("[qfu,utils] DMS QMI client allocation failed: %s", error->message); |
| 468 |
g_error_free (error); |
| 469 |
|
| 470 |
g_debug ("[qfu,utils] retrying..."); |
| 471 |
retry_allocate_qmi_client (task); |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
g_debug ("[qfu,utils] DMS QMI client allocated"); |
| 476 |
|
| 477 |
/* If loading capabilities not required, we're done */ |
| 478 |
if (!ctx->load_capabilities) { |
| 479 |
g_task_return_boolean (task, TRUE); |
| 480 |
g_object_unref (task); |
| 481 |
return; |
| 482 |
} |
| 483 |
|
| 484 |
/* Query capabilities */ |
| 485 |
qmi_client_dms_get_revision (ctx->qmi_client, |
| 486 |
NULL, 10, g_task_get_cancellable (task), |
| 487 |
(GAsyncReadyCallback) dms_get_revision_ready, task); |
| 488 |
qmi_client_dms_list_stored_images (ctx->qmi_client, |
| 489 |
NULL, 10, g_task_get_cancellable (task), |
| 490 |
(GAsyncReadyCallback) dms_list_stored_images_ready, task); |
| 491 |
qmi_client_dms_get_firmware_preference (ctx->qmi_client, |
| 492 |
NULL, 10, g_task_get_cancellable (task), |
| 493 |
(GAsyncReadyCallback) dms_get_firmware_preference_ready, task); |
| 494 |
qmi_client_dms_swi_get_current_firmware (ctx->qmi_client, |
| 495 |
NULL, 10, g_task_get_cancellable (task), |
| 496 |
(GAsyncReadyCallback) swi_get_current_firmware_ready, task); |
| 497 |
} |
| 498 |
|
| 499 |
static void |
| 500 |
retry_allocate_qmi_client (GTask *task) |
| 501 |
{ |
| 502 |
NewClientDmsContext *ctx; |
| 503 |
|
| 504 |
ctx = (NewClientDmsContext *) g_task_get_task_data (task); |
| 505 |
|
| 506 |
/* Consume one retry attempt */ |
| 507 |
ctx->qmi_client_retries--; |
| 508 |
g_assert (ctx->qmi_client_retries >= 0); |
| 509 |
|
| 510 |
g_debug ("[qfu,utils] allocating new DMS QMI client..."); |
| 511 |
qmi_device_allocate_client (ctx->qmi_device, |
| 512 |
QMI_SERVICE_DMS, |
| 513 |
QMI_CID_NONE, |
| 514 |
10, |
| 515 |
g_task_get_cancellable (task), |
| 516 |
(GAsyncReadyCallback) qmi_client_ready, |
| 517 |
task); |
| 518 |
} |
| 519 |
|
| 520 |
static void |
| 521 |
qmi_device_open_ready (QmiDevice *device, |
| 522 |
GAsyncResult *res, |
| 523 |
GTask *task) |
| 524 |
{ |
| 525 |
GError *error = NULL; |
| 526 |
|
| 527 |
if (!qmi_device_open_finish (device, res, &error)) { |
| 528 |
g_prefix_error (&error, "couldn't open QMI device: "); |
| 529 |
g_task_return_error (task, error); |
| 530 |
g_object_unref (task); |
| 531 |
return; |
| 532 |
} |
| 533 |
|
| 534 |
g_debug ("[qfu,utils] QMI device open"); |
| 535 |
retry_allocate_qmi_client (task); |
| 536 |
} |
| 537 |
|
| 538 |
static void |
| 539 |
qmi_device_ready (GObject *source, |
| 540 |
GAsyncResult *res, |
| 541 |
GTask *task) |
| 542 |
{ |
| 543 |
NewClientDmsContext *ctx; |
| 544 |
GError *error = NULL; |
| 545 |
|
| 546 |
ctx = (NewClientDmsContext *) g_task_get_task_data (task); |
| 547 |
|
| 548 |
ctx->qmi_device = qmi_device_new_finish (res, &error); |
| 549 |
if (!ctx->qmi_device) { |
| 550 |
g_prefix_error (&error, "couldn't create QMI device: "); |
| 551 |
g_task_return_error (task, error); |
| 552 |
g_object_unref (task); |
| 553 |
return; |
| 554 |
} |
| 555 |
|
| 556 |
g_debug ("[qfu,utils] QMI device created"); |
| 557 |
|
| 558 |
g_debug ("[qfu,utils] opening QMI device (%s proxy, %s mode)...", |
| 559 |
(ctx->device_open_flags & QMI_DEVICE_OPEN_FLAGS_PROXY) ? "with" : "without", |
| 560 |
(ctx->device_open_flags & QMI_DEVICE_OPEN_FLAGS_MBIM) ? "mbim" : "qmi"); |
| 561 |
|
| 562 |
qmi_device_open (ctx->qmi_device, |
| 563 |
ctx->device_open_flags | QMI_DEVICE_OPEN_FLAGS_SYNC, |
| 564 |
20, |
| 565 |
g_task_get_cancellable (task), |
| 566 |
(GAsyncReadyCallback) qmi_device_open_ready, |
| 567 |
task); |
| 568 |
} |
| 569 |
|
| 570 |
void |
| 571 |
qfu_utils_new_client_dms (GFile *cdc_wdm_file, |
| 572 |
guint retries, |
| 573 |
QmiDeviceOpenFlags device_open_flags, |
| 574 |
gboolean load_capabilities, |
| 575 |
GCancellable *cancellable, |
| 576 |
GAsyncReadyCallback callback, |
| 577 |
gpointer user_data) |
| 578 |
{ |
| 579 |
GTask *task; |
| 580 |
NewClientDmsContext *ctx; |
| 581 |
|
| 582 |
ctx = g_slice_new0 (NewClientDmsContext); |
| 583 |
ctx->qmi_client_retries = retries; |
| 584 |
ctx->device_open_flags = device_open_flags; |
| 585 |
ctx->load_capabilities = load_capabilities; |
| 586 |
|
| 587 |
task = g_task_new (NULL, cancellable, callback, user_data); |
| 588 |
g_task_set_task_data (task, ctx, (GDestroyNotify) new_client_dms_context_free); |
| 589 |
|
| 590 |
g_debug ("[qfu,utils] creating QMI device..."); |
| 591 |
qmi_device_new (cdc_wdm_file, |
| 592 |
g_task_get_cancellable (task), |
| 593 |
(GAsyncReadyCallback) qmi_device_ready, |
| 594 |
task); |
| 595 |
} |
| 596 |
|
| 597 |
/******************************************************************************/ |
| 598 |
|
| 599 |
typedef enum { |
| 600 |
POWER_CYCLE_STEP_OFFLINE, |
| 601 |
POWER_CYCLE_STEP_RESET, |
| 602 |
POWER_CYCLE_STEP_DONE, |
| 603 |
} PowerCycleStep; |
| 604 |
|
| 605 |
typedef struct { |
| 606 |
QmiClientDms *qmi_client; |
| 607 |
PowerCycleStep step; |
| 608 |
} PowerCycleContext; |
| 609 |
|
| 610 |
static void |
| 611 |
power_cycle_context_free (PowerCycleContext *ctx) |
| 612 |
{ |
| 613 |
g_object_unref (ctx->qmi_client); |
| 614 |
g_slice_free (PowerCycleContext, ctx); |
| 615 |
} |
| 616 |
|
| 617 |
gboolean |
| 618 |
qfu_utils_power_cycle_finish (QmiClientDms *qmi_client, |
| 619 |
GAsyncResult *res, |
| 620 |
GError **error) |
| 621 |
{ |
| 622 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 623 |
} |
| 624 |
|
| 625 |
static void power_cycle_step (GTask *task); |
| 626 |
|
| 627 |
static void |
| 628 |
power_cycle_step_ready (QmiClientDms *qmi_client, |
| 629 |
GAsyncResult *res, |
| 630 |
GTask *task) |
| 631 |
{ |
| 632 |
QmiMessageDmsSetOperatingModeOutput *output; |
| 633 |
GError *error = NULL; |
| 634 |
PowerCycleContext *ctx; |
| 635 |
|
| 636 |
ctx = (PowerCycleContext *) g_task_get_task_data (task); |
| 637 |
|
| 638 |
output = qmi_client_dms_set_operating_mode_finish (qmi_client, res, &error); |
| 639 |
if (!output) { |
| 640 |
g_prefix_error (&error, "QMI operation failed: couldn't set operating mode: "); |
| 641 |
g_task_return_error (task, error); |
| 642 |
g_object_unref (task); |
| 643 |
return; |
| 644 |
} |
| 645 |
|
| 646 |
if (!qmi_message_dms_set_operating_mode_output_get_result (output, &error)) { |
| 647 |
g_prefix_error (&error, "couldn't set operating mode: "); |
| 648 |
g_task_return_error (task, error); |
| 649 |
g_object_unref (task); |
| 650 |
qmi_message_dms_set_operating_mode_output_unref (output); |
| 651 |
return; |
| 652 |
} |
| 653 |
|
| 654 |
g_debug ("[qfu,utils] operating mode set successfully..."); |
| 655 |
qmi_message_dms_set_operating_mode_output_unref (output); |
| 656 |
|
| 657 |
/* Go on */ |
| 658 |
ctx->step++; |
| 659 |
power_cycle_step (task); |
| 660 |
} |
| 661 |
|
| 662 |
static void |
| 663 |
power_cycle_step (GTask *task) |
| 664 |
{ |
| 665 |
QmiMessageDmsSetOperatingModeInput *input; |
| 666 |
QmiDmsOperatingMode mode; |
| 667 |
PowerCycleContext *ctx; |
| 668 |
|
| 669 |
ctx = (PowerCycleContext *) g_task_get_task_data (task); |
| 670 |
|
| 671 |
switch (ctx->step) { |
| 672 |
case POWER_CYCLE_STEP_OFFLINE: |
| 673 |
mode = QMI_DMS_OPERATING_MODE_OFFLINE; |
| 674 |
break; |
| 675 |
case POWER_CYCLE_STEP_RESET: |
| 676 |
mode = QMI_DMS_OPERATING_MODE_RESET; |
| 677 |
break; |
| 678 |
case POWER_CYCLE_STEP_DONE: |
| 679 |
/* Finished! */ |
| 680 |
g_task_return_boolean (task, TRUE); |
| 681 |
g_object_unref (task); |
| 682 |
return; |
| 683 |
default: |
| 684 |
g_assert_not_reached (); |
| 685 |
} |
| 686 |
|
| 687 |
input = qmi_message_dms_set_operating_mode_input_new (); |
| 688 |
qmi_message_dms_set_operating_mode_input_set_mode (input, mode, NULL); |
| 689 |
qmi_client_dms_set_operating_mode (ctx->qmi_client, |
| 690 |
input, |
| 691 |
10, |
| 692 |
g_task_get_cancellable (task), |
| 693 |
(GAsyncReadyCallback) power_cycle_step_ready, |
| 694 |
task); |
| 695 |
qmi_message_dms_set_operating_mode_input_unref (input); |
| 696 |
} |
| 697 |
|
| 698 |
void |
| 699 |
qfu_utils_power_cycle (QmiClientDms *qmi_client, |
| 700 |
GCancellable *cancellable, |
| 701 |
GAsyncReadyCallback callback, |
| 702 |
gpointer user_data) |
| 703 |
{ |
| 704 |
GTask *task; |
| 705 |
PowerCycleContext *ctx; |
| 706 |
|
| 707 |
ctx = g_slice_new0 (PowerCycleContext); |
| 708 |
ctx->step = POWER_CYCLE_STEP_OFFLINE; |
| 709 |
ctx->qmi_client = g_object_ref (qmi_client); |
| 710 |
|
| 711 |
task = g_task_new (NULL, cancellable, callback, user_data); |
| 712 |
g_task_set_task_data (task, ctx, (GDestroyNotify) power_cycle_context_free); |
| 713 |
|
| 714 |
power_cycle_step (task); |
| 715 |
} |
| 716 |
|
| 717 |
/******************************************************************************/ |
| 718 |
|
| 719 |
#if defined MM_RUNTIME_CHECK_ENABLED |
| 720 |
|
| 721 |
gboolean |
| 722 |
qfu_utils_modemmanager_running (gboolean *mm_running, |
| 723 |
GError **error) |
| 724 |
{ |
| 725 |
GDBusConnection *connection; |
| 726 |
GVariant *response; |
| 727 |
GError *inner_error = NULL; |
| 728 |
|
| 729 |
g_assert (mm_running); |
| 730 |
|
| 731 |
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error); |
| 732 |
if (!connection) { |
| 733 |
g_prefix_error (error, "Couldn't get system bus: "); |
| 734 |
return FALSE; |
| 735 |
} |
| 736 |
|
| 737 |
response = g_dbus_connection_call_sync (connection, |
| 738 |
"org.freedesktop.ModemManager1", |
| 739 |
"/org/freedesktop/ModemManager1", |
| 740 |
"org.freedesktop.DBus.Peer", |
| 741 |
"Ping", |
| 742 |
NULL, |
| 743 |
NULL, |
| 744 |
G_DBUS_CALL_FLAGS_NO_AUTO_START, |
| 745 |
-1, |
| 746 |
NULL, |
| 747 |
&inner_error); |
| 748 |
if (!response) { |
| 749 |
g_debug ("[qfu-utils] couldn't ping ModemManager: %s", inner_error->message); |
| 750 |
g_error_free (inner_error); |
| 751 |
*mm_running = FALSE; |
| 752 |
} else { |
| 753 |
*mm_running = TRUE; |
| 754 |
g_variant_unref (response); |
| 755 |
} |
| 756 |
|
| 757 |
g_object_unref (connection); |
| 758 |
return TRUE; |
| 759 |
} |
| 760 |
|
| 761 |
#endif |