Debian Codegraph @

src/qmi-firmware-update/qfu-operation-verify.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 <glib-object.h>
23 #include <gio/gio.h>
24
25 #include "qfu-operation.h"
26 #include "qfu-image.h"
27 #include "qfu-image-cwe.h"
28 #include "qfu-image-factory.h"
29 #include "qfu-enum-types.h"
30
31 #define VALIDATE_STR_NA(str) (str && str[0] ? str : "n/a")
32
33 static void
34 print_image_cwe (QfuImageCwe *image,
35 const gchar *indent_prefix,
36 const gchar *id_str,
37 guint idx)
38 {
39 guint i;
40 guint j;
41
42 g_print ("%s-------------------------------------\n", indent_prefix);
43 g_print ("%s[cwe %s] type: %s\n",
44 indent_prefix, id_str, VALIDATE_STR_NA (qfu_image_cwe_embedded_header_get_type (image, idx)));
45 g_print ("%s[cwe %s] product: %s\n",
46 indent_prefix, id_str, VALIDATE_STR_NA (qfu_image_cwe_embedded_header_get_product (image, idx)));
47 g_print ("%s[cwe %s] version: %s\n",
48 indent_prefix, id_str, VALIDATE_STR_NA (qfu_image_cwe_embedded_header_get_version (image, idx)));
49 g_print ("%s[cwe %s] date: %s\n",
50 indent_prefix, id_str, VALIDATE_STR_NA (qfu_image_cwe_embedded_header_get_date (image, idx)));
51 g_print ("%s[cwe %s] size: %" G_GUINT32_FORMAT "\n",
52 indent_prefix, id_str, qfu_image_cwe_embedded_header_get_image_size (image, idx));
53
54 for (i = idx + 1, j = 0; i < qfu_image_cwe_get_n_embedded_headers (image); i++) {
55 gchar *sub_id_str;
56 gchar *sub_indent_prefix;
57
58 /* As soon as we find one which doesn't have the expected parent index, break */
59 if (qfu_image_cwe_embedded_header_get_parent_index (image, i) != (gint) idx)
60 continue;
61
62 sub_id_str = g_strdup_printf ("%s.%u", id_str, j++);
63 sub_indent_prefix = g_strdup_printf ("%s ", indent_prefix);
64
65 print_image_cwe (image, sub_indent_prefix, sub_id_str, i);
66
67 g_free (sub_id_str);
68 g_free (sub_indent_prefix);
69 }
70 }
71
72 static gboolean
73 operation_verify_run_single (const gchar *image_path)
74 {
75 QfuImage *image;
76 GFile *file;
77 GError *error = NULL;
78 gboolean result = FALSE;
79
80 file = g_file_new_for_commandline_arg (image_path);
81 image = qfu_image_factory_build (file, NULL, &error);
82 if (!image) {
83 g_printerr ("error: couldn't detect image type: %s\n", error->message);
84 g_error_free (error);
85 goto out;
86 }
87
88 g_print ("\n");
89 g_print ("Firmware image:\n");
90 g_print (" filename: %s\n", qfu_image_get_display_name (image));
91 g_print (" detected type: %s\n", qfu_image_type_get_string (qfu_image_get_image_type (image)));
92 g_print (" size: %" G_GOFFSET_FORMAT " bytes\n", qfu_image_get_size (image));
93 g_print (" header: %" G_GOFFSET_FORMAT " bytes\n", qfu_image_get_header_size (image));
94 g_print (" data: %" G_GOFFSET_FORMAT " bytes\n", qfu_image_get_data_size (image));
95 g_print (" data chunks: %" G_GUINT16_FORMAT " (%lu bytes/chunk)\n", qfu_image_get_n_data_chunks (image), (gulong) QFU_IMAGE_CHUNK_SIZE);
96
97 if (QFU_IS_IMAGE_CWE (image)) {
98 QfuImageCwe *image_cwe = QFU_IMAGE_CWE (image);
99
100 g_print (" [cwe] detected firmware version: %s\n",
101 VALIDATE_STR_NA (qfu_image_cwe_get_parsed_firmware_version (image_cwe)));
102 g_print (" [cwe] detected config version: %s\n",
103 VALIDATE_STR_NA (qfu_image_cwe_get_parsed_config_version (image_cwe)));
104 g_print (" [cwe] detected carrier: %s\n",
105 VALIDATE_STR_NA (qfu_image_cwe_get_parsed_carrier (image_cwe)));
106
107 print_image_cwe (image_cwe, " ", "0", 0);
108 }
109
110 result = TRUE;
111
112 out:
113 if (image)
114 g_object_unref (image);
115 return result;
116 }
117
118 gboolean
119 qfu_operation_verify_run (const gchar **images)
120 {
121 guint invalid_images = 0;
122 guint i;
123
124 for (i = 0; images[i] ; i++)
125 invalid_images += !operation_verify_run_single (images[i]);
126
127 return !invalid_images;
128 }