Debian Codegraph @

src/qmi-firmware-update/qfu-image-factory.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 Bjørn Mork <[email protected]>
19 * Copyright (C) 2016 Zodiac Inflight Innovations
20 * Copyright (C) 2016-2017 Aleksander Morgado <[email protected]>
21 */
22
23 #include "qfu-image-factory.h"
24 #include "qfu-image.h"
25 #include "qfu-image-cwe.h"
26
27 QfuImage *
28 qfu_image_factory_build (GFile *file,
29 GCancellable *cancellable,
30 GError **error)
31 {
32 gchar *basename;
33 QfuImage *image = NULL;
34 GError *inner_error = NULL;
35
36 g_assert (G_IS_FILE (file));
37 basename = g_file_get_basename (file);
38
39 /* guessing image type based on the well known Gobi 1k and 2k
40 * filenames, and assumes anything else could be a CWE image
41 *
42 * This is based on the types in gobi-loader's snooped magic strings:
43 * 0x05 => "amss.mbn"
44 * 0x06 => "apps.mbn"
45 * 0x0d => "uqcn.mbn" (Gobi 2000 only)
46 */
47 if (g_ascii_strcasecmp (basename, "amss.mbn") == 0)
48 image = qfu_image_new (file, QFU_IMAGE_TYPE_AMSS_MODEM, cancellable, error);
49 else if (g_ascii_strcasecmp (basename, "apps.mbn") == 0)
50 image = qfu_image_new (file, QFU_IMAGE_TYPE_AMSS_APPLICATION, cancellable, error);
51 else if (g_ascii_strcasecmp (basename, "uqcn.mbn") == 0)
52 image = qfu_image_new (file, QFU_IMAGE_TYPE_AMSS_UQCN, cancellable, error);
53 else {
54 /* Try to autodetect format */
55
56 /* Maybe a CWE image? */
57 image = qfu_image_cwe_new (file, cancellable, &inner_error);
58 if (!image) {
59 g_debug ("[qfu-image-factory] not a CWE file: %s", inner_error->message);
60 g_error_free (inner_error);
61
62 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
63 "unknown firmware image file");
64 }
65 }
66
67 g_free (basename);
68 return image;
69 }