Debian Codegraph @

src/qmi-firmware-update/qfu-reseter.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-reseter.h"
30 #include "qfu-at-device.h"
31 #include "qfu-utils.h"
32
33 G_DEFINE_TYPE (QfuReseter, qfu_reseter, G_TYPE_OBJECT)
34
35 struct _QfuReseterPrivate {
36 QfuDeviceSelection *device_selection;
37 QmiClientDms *qmi_client;
38 QmiDeviceOpenFlags device_open_flags;
39 };
40
41 /******************************************************************************/
42 /* Run */
43
44 #define MAX_RETRIES 2
45
46 typedef struct {
47 /* Files to use */
48 GList *ttys;
49 GFile *cdc_wdm;
50 /* QMI client amd device */
51 QmiDevice *qmi_device;
52 QmiClientDms *qmi_client;
53 gboolean ignore_release_cid;
54 /* List of AT devices */
55 GList *at_devices;
56 GList *current;
57 /* Retries */
58 guint retries;
59 } RunContext;
60
61 static void
62 run_context_free (RunContext *ctx)
63 {
64 if (ctx->cdc_wdm)
65 g_object_unref (ctx->cdc_wdm);
66 if (ctx->qmi_client) {
67 g_assert (ctx->qmi_device);
68 qmi_device_release_client (ctx->qmi_device,
69 QMI_CLIENT (ctx->qmi_client),
70 (ctx->ignore_release_cid ?
71 QMI_DEVICE_RELEASE_CLIENT_FLAGS_NONE :
72 QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID),
73 10, NULL, NULL, NULL);
74 g_object_unref (ctx->qmi_client);
75 }
76 if (ctx->qmi_device) {
77 qmi_device_close_async (ctx->qmi_device, 10, NULL, NULL, NULL);
78 g_object_unref (ctx->qmi_device);
79 }
80 g_list_free_full (ctx->ttys, g_object_unref);
81 g_list_free_full (ctx->at_devices, g_object_unref);
82 g_slice_free (RunContext, ctx);
83 }
84
85 gboolean
86 qfu_reseter_run_finish (QfuReseter *self,
87 GAsyncResult *res,
88 GError **error)
89 {
90 return g_task_propagate_boolean (G_TASK (res), error);
91 }
92
93 static void run_context_step_at (GTask *task);
94
95 static gboolean
96 run_context_step_at_cb (GTask *task)
97 {
98 run_context_step_at (task);
99 return FALSE;
100 }
101
102 static void
103 run_context_step_at_next (GTask *task)
104 {
105 RunContext *ctx;
106
107 ctx = (RunContext *) g_task_get_task_data (task);
108 ctx->current = g_list_next (ctx->current);
109
110 if (!ctx->current) {
111 if (!ctx->retries) {
112 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
113 "couldn't run reset operation");
114 g_object_unref (task);
115 return;
116 }
117
118 /* Launch retry with the first device */
119 ctx->retries--;
120 ctx->current = ctx->at_devices;
121 }
122
123 /* Schedule next step in an idle */
124 g_idle_add ((GSourceFunc) run_context_step_at_cb, task);
125 }
126
127 static gint
128 device_sort_by_name_reversed (QfuAtDevice *a, QfuAtDevice *b)
129 {
130 return strcmp (qfu_at_device_get_name (b), qfu_at_device_get_name (a));
131 }
132
133 static void
134 run_context_step_at (GTask *task)
135 {
136 RunContext *ctx;
137 QfuAtDevice *at_device;
138 GError *error = NULL;
139
140 ctx = (RunContext *) g_task_get_task_data (task);
141
142 /* If we get to AT reset after trying QMI, and we didn't find any port to
143 * use, return error */
144 if (!ctx->ttys) {
145 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
146 "No devices found to run reset operation");
147 g_object_unref (task);
148 return;
149 }
150
151 /* Initialize AT devices the first time we get here */
152 if (!ctx->at_devices) {
153 GList *l;
154
155 for (l = ctx->ttys; l; l = g_list_next (l)) {
156 at_device = qfu_at_device_new (G_FILE (l->data), g_task_get_cancellable (task), &error);
157 if (!at_device) {
158 g_task_return_error (task, error);
159 g_object_unref (task);
160 return;
161 }
162 ctx->at_devices = g_list_append (ctx->at_devices, at_device);
163 }
164
165 /* Sort by filename reversed; usually the TTY with biggest number is a
166 * good AT port */
167 ctx->at_devices = g_list_sort (ctx->at_devices, (GCompareFunc) device_sort_by_name_reversed);
168 /* Select first TTY to start */
169 ctx->current = ctx->at_devices;
170 } else
171 g_assert (ctx->current);
172
173 at_device = QFU_AT_DEVICE (ctx->current->data);
174 if (!qfu_at_device_boothold (at_device, g_task_get_cancellable (task), &error)) {
175 g_debug ("[qfu-reseter] error: %s", error->message);
176 g_error_free (error);
177 run_context_step_at_next (task);
178 return;
179 }
180
181 /* Success! */
182 g_debug ("[qfu-reseter] successfully run 'at boothold' operation");
183 ctx->ignore_release_cid = TRUE;
184 g_task_return_boolean (task, TRUE);
185 g_object_unref (task);
186 }
187
188 static void
189 power_cycle_ready (QmiClientDms *qmi_client,
190 GAsyncResult *res,
191 GTask *task)
192 {
193 GError *error = NULL;
194 RunContext *ctx;
195
196 ctx = (RunContext *) g_task_get_task_data (task);
197
198 if (!qfu_utils_power_cycle_finish (qmi_client, res, &error)) {
199 g_debug ("[qfu-reseter] error: couldn't power cycle: %s", error->message);
200 g_error_free (error);
201 g_debug ("[qfu-reseter] skipping QMI-based boothold");
202 run_context_step_at (task);
203 return;
204 }
205
206 g_debug ("[qfu-reseter] reset requested successfully...");
207
208 ctx->ignore_release_cid = TRUE;
209 g_task_return_boolean (task, TRUE);
210 g_object_unref (task);
211 }
212
213 static void
214 set_boot_image_download_mode_ready (QmiClientDms *client,
215 GAsyncResult *res,
216 GTask *task)
217 {
218 QmiMessageDmsSetBootImageDownloadModeOutput *output;
219 GError *error = NULL;
220 RunContext *ctx;
221
222 ctx = (RunContext *) g_task_get_task_data (task);
223
224 output = qmi_client_dms_set_boot_image_download_mode_finish (client, res, &error);
225 if (!output || !qmi_message_dms_set_boot_image_download_mode_output_get_result (output, &error)) {
226 g_debug ("[qfu-reseter] error: couldn't run 'set boot image download mode' operation: %s", error->message);
227 g_error_free (error);
228 if (output)
229 qmi_message_dms_set_boot_image_download_mode_output_unref (output);
230 g_debug ("[qfu-reseter] skipping QMI-based boothold");
231 run_context_step_at (task);
232 return;
233 }
234
235 qmi_message_dms_set_boot_image_download_mode_output_unref (output);
236
237 g_debug ("[qfu-reseter] successfully run 'set boot image download mode' operation");
238
239 qfu_utils_power_cycle (ctx->qmi_client,
240 g_task_get_cancellable (task),
241 (GAsyncReadyCallback) power_cycle_ready,
242 task);
243 }
244
245 static void
246 run_context_step_qmi_boot_image_download_mode (GTask *task)
247 {
248 RunContext *ctx;
249 QfuReseter *self;
250 QmiMessageDmsSetBootImageDownloadModeInput *input;
251
252 ctx = (RunContext *) g_task_get_task_data (task);
253 self = g_task_get_source_object (task);
254
255 g_assert (ctx->qmi_client || self->priv->qmi_client);
256
257 /* Try DMS 0x0050 */
258 input = qmi_message_dms_set_boot_image_download_mode_input_new ();
259 qmi_message_dms_set_boot_image_download_mode_input_set_mode (input, QMI_DMS_BOOT_IMAGE_DOWNLOAD_MODE_BOOT_AND_RECOVERY, NULL);
260 qmi_client_dms_set_boot_image_download_mode (self->priv->qmi_client ? self->priv->qmi_client : ctx->qmi_client,
261 input,
262 10,
263 g_task_get_cancellable (task),
264 (GAsyncReadyCallback) set_boot_image_download_mode_ready,
265 task);
266 qmi_message_dms_set_boot_image_download_mode_input_unref (input);
267 }
268
269 static void
270 set_firmware_id_ready (QmiClientDms *client,
271 GAsyncResult *res,
272 GTask *task)
273 {
274 QmiMessageDmsSetFirmwareIdOutput *output;
275 GError *error = NULL;
276 RunContext *ctx;
277
278 ctx = (RunContext *) g_task_get_task_data (task);
279
280 output = qmi_client_dms_set_firmware_id_finish (client, res, &error);
281 if (!output || !qmi_message_dms_set_firmware_id_output_get_result (output, &error)) {
282 g_debug ("[qfu-reseter] error: couldn't run 'set firmware id' operation: %s", error->message);
283 g_error_free (error);
284 if (output)
285 qmi_message_dms_set_firmware_id_output_unref (output);
286 g_debug ("[qfu-reseter] trying boot image download mode...");
287 run_context_step_qmi_boot_image_download_mode (task);
288 return;
289 }
290
291 qmi_message_dms_set_firmware_id_output_unref (output);
292
293 g_debug ("[qfu-reseter] successfully run 'set firmware id' operation");
294 ctx->ignore_release_cid = TRUE;
295 g_task_return_boolean (task, TRUE);
296 g_object_unref (task);
297 }
298
299 static void
300 run_context_step_qmi_firmware_id (GTask *task)
301 {
302 RunContext *ctx;
303 QfuReseter *self;
304
305 ctx = (RunContext *) g_task_get_task_data (task);
306 self = g_task_get_source_object (task);
307
308 g_assert (ctx->qmi_client || self->priv->qmi_client);
309
310 /* Run DMS 0x003e to power cycle in boot & hold mode */
311 qmi_client_dms_set_firmware_id (self->priv->qmi_client ? self->priv->qmi_client : ctx->qmi_client,
312 NULL,
313 10,
314 g_task_get_cancellable (task),
315 (GAsyncReadyCallback) set_firmware_id_ready,
316 task);
317 }
318
319 static void
320 new_client_dms_ready (gpointer unused,
321 GAsyncResult *res,
322 GTask *task)
323 {
324 RunContext *ctx;
325 GError *error = NULL;
326
327 ctx = (RunContext *) g_task_get_task_data (task);
328
329 if (!qfu_utils_new_client_dms_finish (res,
330 &ctx->qmi_device,
331 &ctx->qmi_client,
332 NULL, NULL, NULL, NULL, NULL, NULL,
333 &error)) {
334 /* Jump to AT-based boothold */
335 g_debug ("[qfu-reseter] error: couldn't allocate QMI client: %s", error->message);
336 g_error_free (error);
337 g_debug ("[qfu-reseter] skipping QMI-based boothold");
338 run_context_step_at (task);
339 return;
340 }
341
342 run_context_step_qmi_firmware_id (task);
343 }
344
345 void
346 qfu_reseter_run (QfuReseter *self,
347 GCancellable *cancellable,
348 GAsyncReadyCallback callback,
349 gpointer user_data)
350 {
351 RunContext *ctx;
352 GTask *task;
353
354 ctx = g_slice_new0 (RunContext);
355 ctx->retries = MAX_RETRIES;
356
357 task = g_task_new (self, cancellable, callback, user_data);
358 g_task_set_task_data (task, ctx, (GDestroyNotify) run_context_free);
359
360 /* List devices to use */
361 if (!self->priv->qmi_client)
362 ctx->cdc_wdm = qfu_device_selection_get_single_cdc_wdm (self->priv->device_selection);
363 ctx->ttys = qfu_device_selection_get_multiple_ttys (self->priv->device_selection, QFU_HELPERS_DEVICE_MODE_MODEM);
364
365 if (!ctx->ttys && !ctx->cdc_wdm && !self->priv->qmi_client) {
366 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
367 "No devices found to run reset operation");
368 g_object_unref (task);
369 return;
370 }
371
372 /* If no cdc-wdm file available, try AT directly */
373 if (!ctx->cdc_wdm && !self->priv->qmi_client) {
374 run_context_step_at (task);
375 return;
376 }
377
378 /* If we already got a QMI client as input, try QMI directly */
379 if (self->priv->qmi_client) {
380 run_context_step_qmi_firmware_id (task);
381 return;
382 }
383
384 /* Otherwise, try to allocate a QMI client */
385 g_assert (ctx->cdc_wdm);
386 qfu_utils_new_client_dms (ctx->cdc_wdm,
387 3,
388 self->priv->device_open_flags,
389 FALSE,
390 cancellable,
391 (GAsyncReadyCallback) new_client_dms_ready,
392 task);
393 }
394
395 /******************************************************************************/
396
397 QfuReseter *
398 qfu_reseter_new (QfuDeviceSelection *device_selection,
399 QmiClientDms *qmi_client,
400 QmiDeviceOpenFlags device_open_flags)
401 {
402 QfuReseter *self;
403
404 self = g_object_new (QFU_TYPE_RESETER, NULL);
405 self->priv->device_selection = g_object_ref (device_selection);
406 self->priv->qmi_client = qmi_client ? g_object_ref (qmi_client) : NULL;
407 self->priv->device_open_flags = device_open_flags;
408
409 return self;
410 }
411
412 static void
413 qfu_reseter_init (QfuReseter *self)
414 {
415 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, QFU_TYPE_RESETER, QfuReseterPrivate);
416 }
417
418 static void
419 dispose (GObject *object)
420 {
421 QfuReseter *self = QFU_RESETER (object);
422
423 g_clear_object (&self->priv->device_selection);
424 g_clear_object (&self->priv->qmi_client);
425
426 G_OBJECT_CLASS (qfu_reseter_parent_class)->dispose (object);
427 }
428
429 static void
430 qfu_reseter_class_init (QfuReseterClass *klass)
431 {
432 GObjectClass *object_class = G_OBJECT_CLASS (klass);
433
434 g_type_class_add_private (object_class, sizeof (QfuReseterPrivate));
435
436 object_class->dispose = dispose;
437 }