aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmi-firmware-update/qfu-reseter.c
blob: 979294a3272a0505aae27b595143979a98197898 (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
/* -*- 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 Zodiac Inflight Innovations
 * Copyright (C) 2016-2017 Aleksander Morgado <aleksander@aleksander.es>
 */

#include <config.h>
#include <string.h>

#include <gio/gio.h>

#include <libqmi-glib.h>

#include "qfu-reseter.h"
#include "qfu-at-device.h"
#include "qfu-utils.h"

G_DEFINE_TYPE (QfuReseter, qfu_reseter, G_TYPE_OBJECT)

struct _QfuReseterPrivate {
    QfuDeviceSelection *device_selection;
    QmiClientDms       *qmi_client;
    QmiDeviceOpenFlags  device_open_flags;
};

/******************************************************************************/
/* Run */

#define MAX_RETRIES 2

typedef struct {
    /* Files to use */
    GList      *ttys;
    GFile      *cdc_wdm;
    /* QMI client amd device */
    QmiDevice    *qmi_device;
    QmiClientDms *qmi_client;
    gboolean      ignore_release_cid;
    /* List of AT devices */
    GList *at_devices;
    GList *current;
    /* Retries */
    guint retries;
} RunContext;

static void
run_context_free (RunContext *ctx)
{
    if (ctx->cdc_wdm)
        g_object_unref (ctx->cdc_wdm);
    if (ctx->qmi_client) {
        g_assert (ctx->qmi_device);
        qmi_device_release_client (ctx->qmi_device,
                                   QMI_CLIENT (ctx->qmi_client),
                                   (ctx->ignore_release_cid ?
                                    QMI_DEVICE_RELEASE_CLIENT_FLAGS_NONE :
                                    QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID),
                                   10, NULL, NULL, NULL);
        g_object_unref (ctx->qmi_client);
    }
    if (ctx->qmi_device) {
        qmi_device_close_async (ctx->qmi_device, 10, NULL, NULL, NULL);
        g_object_unref (ctx->qmi_device);
    }
    g_list_free_full (ctx->ttys, g_object_unref);
    g_list_free_full (ctx->at_devices, g_object_unref);
    g_slice_free (RunContext, ctx);
}

gboolean
qfu_reseter_run_finish (QfuReseter    *self,
                         GAsyncResult  *res,
                         GError       **error)
{
    return g_task_propagate_boolean (G_TASK (res), error);
}

static void run_context_step_at (GTask *task);

static gboolean
run_context_step_at_cb (GTask *task)
{
    run_context_step_at (task);
    return FALSE;
}

static void
run_context_step_at_next (GTask *task)
{
    RunContext *ctx;

    ctx = (RunContext *) g_task_get_task_data (task);
    ctx->current = g_list_next (ctx->current);

    if (!ctx->current) {
        if (!ctx->retries) {
            g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
                                     "couldn't run reset operation");
            g_object_unref (task);
            return;
        }

        /* Launch retry with the first device */
        ctx->retries--;
        ctx->current = ctx->at_devices;
    }

    /* Schedule next step in an idle */
    g_idle_add ((GSourceFunc) run_context_step_at_cb, task);
}

static gint
device_sort_by_name_reversed (QfuAtDevice *a, QfuAtDevice *b)
{
    return strcmp (qfu_at_device_get_name (b), qfu_at_device_get_name (a));
}

static void
run_context_step_at (GTask *task)
{
    RunContext  *ctx;
    QfuAtDevice *at_device;
    GError      *error = NULL;

    ctx = (RunContext *) g_task_get_task_data (task);

    /* If we get to AT reset after trying QMI, and we didn't find any port to
     * use, return error */
    if (!ctx->ttys) {
        g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                                 "No devices found to run reset operation");
        g_object_unref (task);
        return;
    }

    /* Initialize AT devices the first time we get here */
    if (!ctx->at_devices) {
        GList *l;

        for (l = ctx->ttys; l; l = g_list_next (l)) {
            at_device = qfu_at_device_new (G_FILE (l->data), g_task_get_cancellable (task), &error);
            if (!at_device) {
                g_task_return_error (task, error);
                g_object_unref (task);
                return;
            }
            ctx->at_devices = g_list_append (ctx->at_devices, at_device);
        }

        /* Sort by filename reversed; usually the TTY with biggest number is a
         * good AT port */
        ctx->at_devices = g_list_sort (ctx->at_devices, (GCompareFunc) device_sort_by_name_reversed);
        /* Select first TTY to start */
        ctx->current = ctx->at_devices;
    } else
        g_assert (ctx->current);

    at_device = QFU_AT_DEVICE (ctx->current->data);
    if (!qfu_at_device_boothold (at_device, g_task_get_cancellable (task), &error)) {
        g_debug ("[qfu-reseter] error: %s", error->message);
        g_error_free (error);
        run_context_step_at_next (task);
        return;
    }

    /* Success! */
    g_debug ("[qfu-reseter] successfully run 'at boothold' operation");
    ctx->ignore_release_cid = TRUE;
    g_task_return_boolean (task, TRUE);
    g_object_unref (task);
}

static void
power_cycle_ready (QmiClientDms *qmi_client,
                   GAsyncResult *res,
                   GTask        *task)
{
    GError     *error = NULL;
    RunContext *ctx;

    ctx = (RunContext *) g_task_get_task_data (task);

    if (!qfu_utils_power_cycle_finish (qmi_client, res, &error)) {
        g_debug ("[qfu-reseter] error: couldn't power cycle: %s", error->message);
        g_error_free (error);
        g_debug ("[qfu-reseter] skipping QMI-based boothold");
        run_context_step_at (task);
        return;
    }

    g_debug ("[qfu-reseter] reset requested successfully...");

    ctx->ignore_release_cid = TRUE;
    g_task_return_boolean (task, TRUE);
    g_object_unref (task);
}

static void
set_boot_image_download_mode_ready (QmiClientDms *client,
                                    GAsyncResult *res,
                                    GTask        *task)
{
    QmiMessageDmsSetBootImageDownloadModeOutput *output;
    GError                                      *error = NULL;
    RunContext                                  *ctx;

    ctx = (RunContext *) g_task_get_task_data (task);

    output = qmi_client_dms_set_boot_image_download_mode_finish (client, res, &error);
    if (!output || !qmi_message_dms_set_boot_image_download_mode_output_get_result (output, &error)) {
        g_debug ("[qfu-reseter] error: couldn't run 'set boot image download mode' operation: %s", error->message);
        g_error_free (error);
        if (output)
            qmi_message_dms_set_boot_image_download_mode_output_unref (output);
        g_debug ("[qfu-reseter] skipping QMI-based boothold");
        run_context_step_at (task);
        return;
    }

    qmi_message_dms_set_boot_image_download_mode_output_unref (output);

    g_debug ("[qfu-reseter] successfully run 'set boot image download mode' operation");

    qfu_utils_power_cycle (ctx->qmi_client,
                           g_task_get_cancellable (task),
                           (GAsyncReadyCallback) power_cycle_ready,
                           task);
}

static void
run_context_step_qmi_boot_image_download_mode (GTask *task)
{
    RunContext                                 *ctx;
    QfuReseter                                 *self;
    QmiMessageDmsSetBootImageDownloadModeInput *input;

    ctx = (RunContext *) g_task_get_task_data (task);
    self = g_task_get_source_object (task);

    g_assert (ctx->qmi_client || self->priv->qmi_client);

    /* Try DMS 0x0050 */
    input = qmi_message_dms_set_boot_image_download_mode_input_new ();
    qmi_message_dms_set_boot_image_download_mode_input_set_mode (input, QMI_DMS_BOOT_IMAGE_DOWNLOAD_MODE_BOOT_AND_RECOVERY, NULL);
    qmi_client_dms_set_boot_image_download_mode (self->priv->qmi_client ? self->priv->qmi_client : ctx->qmi_client,
                                                 input,
                                                 10,
                                                 g_task_get_cancellable (task),
                                                 (GAsyncReadyCallback) set_boot_image_download_mode_ready,
                                                 task);
    qmi_message_dms_set_boot_image_download_mode_input_unref (input);
}

static void
set_firmware_id_ready (QmiClientDms *client,
                       GAsyncResult *res,
                       GTask        *task)
{
    QmiMessageDmsSetFirmwareIdOutput *output;
    GError                           *error = NULL;
    RunContext                       *ctx;

    ctx = (RunContext *) g_task_get_task_data (task);

    output = qmi_client_dms_set_firmware_id_finish (client, res, &error);
    if (!output || !qmi_message_dms_set_firmware_id_output_get_result (output, &error)) {
        g_debug ("[qfu-reseter] error: couldn't run 'set firmware id' operation: %s", error->message);
        g_error_free (error);
        if (output)
            qmi_message_dms_set_firmware_id_output_unref (output);
        g_debug ("[qfu-reseter] trying boot image download mode...");
        run_context_step_qmi_boot_image_download_mode (task);
        return;
    }

    qmi_message_dms_set_firmware_id_output_unref (output);

    g_debug ("[qfu-reseter] successfully run 'set firmware id' operation");
    ctx->ignore_release_cid = TRUE;
    g_task_return_boolean (task, TRUE);
    g_object_unref (task);
}

static void
run_context_step_qmi_firmware_id (GTask *task)
{
    RunContext *ctx;
    QfuReseter *self;

    ctx = (RunContext *) g_task_get_task_data (task);
    self = g_task_get_source_object (task);

    g_assert (ctx->qmi_client || self->priv->qmi_client);

    /* Run DMS 0x003e to power cycle in boot & hold mode */
    qmi_client_dms_set_firmware_id (self->priv->qmi_client ? self->priv->qmi_client : ctx->qmi_client,
                                    NULL,
                                    10,
                                    g_task_get_cancellable (task),
                                    (GAsyncReadyCallback) set_firmware_id_ready,
                                    task);
}

static void
new_client_dms_ready (gpointer      unused,
                      GAsyncResult *res,
                      GTask        *task)
{
    RunContext *ctx;
    GError     *error = NULL;

    ctx = (RunContext *) g_task_get_task_data (task);

    if (!qfu_utils_new_client_dms_finish (res,
                                          &ctx->qmi_device,
                                          &ctx->qmi_client,
                                          NULL, NULL, NULL, NULL, NULL, NULL,
                                          &error)) {
        /* Jump to AT-based boothold */
        g_debug ("[qfu-reseter] error: couldn't allocate QMI client: %s", error->message);
        g_error_free (error);
        g_debug ("[qfu-reseter] skipping QMI-based boothold");
        run_context_step_at (task);
        return;
    }

    run_context_step_qmi_firmware_id (task);
}

void
qfu_reseter_run (QfuReseter          *self,
                 GCancellable        *cancellable,
                 GAsyncReadyCallback  callback,
                 gpointer             user_data)
{
    RunContext *ctx;
    GTask      *task;

    ctx = g_slice_new0 (RunContext);
    ctx->retries = MAX_RETRIES;

    task = g_task_new (self, cancellable, callback, user_data);
    g_task_set_task_data (task, ctx, (GDestroyNotify) run_context_free);

    /* List devices to use */
    if (!self->priv->qmi_client)
        ctx->cdc_wdm = qfu_device_selection_get_single_cdc_wdm (self->priv->device_selection);
    ctx->ttys = qfu_device_selection_get_multiple_ttys  (self->priv->device_selection);

    if (!ctx->ttys && !ctx->cdc_wdm && !self->priv->qmi_client) {
        g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                                 "No devices found to run reset operation");
        g_object_unref (task);
        return;
    }

    /* If no cdc-wdm file available, try AT directly */
    if (!ctx->cdc_wdm && !self->priv->qmi_client) {
        run_context_step_at (task);
        return;
    }

    /* If we already got a QMI client as input, try QMI directly */
    if (self->priv->qmi_client) {
        run_context_step_qmi_firmware_id (task);
        return;
    }

    /* Otherwise, try to allocate a QMI client */
    g_assert (ctx->cdc_wdm);
    qfu_utils_new_client_dms (ctx->cdc_wdm,
                              3,
                              self->priv->device_open_flags,
                              FALSE,
                              cancellable,
                              (GAsyncReadyCallback) new_client_dms_ready,
                              task);
}

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

QfuReseter *
qfu_reseter_new (QfuDeviceSelection *device_selection,
                 QmiClientDms       *qmi_client,
                 QmiDeviceOpenFlags  device_open_flags)
{
    QfuReseter *self;

    self = g_object_new (QFU_TYPE_RESETER, NULL);
    self->priv->device_selection = g_object_ref (device_selection);
    self->priv->qmi_client = qmi_client ? g_object_ref (qmi_client) : NULL;
    self->priv->device_open_flags = device_open_flags;

    return self;
}

static void
qfu_reseter_init (QfuReseter *self)
{
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, QFU_TYPE_RESETER, QfuReseterPrivate);
}

static void
dispose (GObject *object)
{
    QfuReseter *self = QFU_RESETER (object);

    g_clear_object (&self->priv->device_selection);
    g_clear_object (&self->priv->qmi_client);

    G_OBJECT_CLASS (qfu_reseter_parent_class)->dispose (object);
}

static void
qfu_reseter_class_init (QfuReseterClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (QfuReseterPrivate));

    object_class->dispose = dispose;
}