Debian Codegraph @

src/qmi-firmware-update/qfu-image.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.h"
24 #include "qfu-enum-types.h"
25
26 static void initable_iface_init (GInitableIface *iface);
27
28 G_DEFINE_TYPE_EXTENDED (QfuImage, qfu_image, G_TYPE_OBJECT, 0,
29 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init))
30
31 enum {
32 PROP_0,
33 PROP_FILE,
34 PROP_IMAGE_TYPE,
35 PROP_INPUT_STREAM,
36 PROP_LAST
37 };
38
39 static GParamSpec *properties[PROP_LAST];
40
41 struct _QfuImagePrivate {
42 QfuImageType image_type;
43 GFile *file;
44 GFileInfo *info;
45 GInputStream *input_stream;
46 };
47
48 /******************************************************************************/
49
50 gsize
51 qfu_image_get_data_chunk_size (QfuImage *self,
52 guint16 chunk_i)
53 {
54 gssize chunk_size = QFU_IMAGE_CHUNK_SIZE;
55 guint n_chunks;
56
57 n_chunks = qfu_image_get_n_data_chunks (self);
58 g_assert (chunk_i < n_chunks);
59
60 if ((guint)chunk_i == (n_chunks - 1)) {
61 chunk_size = qfu_image_get_data_size (self) - ((gssize)chunk_i * QFU_IMAGE_CHUNK_SIZE);
62 g_assert (chunk_size > 0);
63 } else
64 chunk_size = QFU_IMAGE_CHUNK_SIZE;
65
66 return chunk_size;
67 }
68
69 gssize
70 qfu_image_read_data_chunk (QfuImage *self,
71 guint16 chunk_i,
72 guint8 *out_buffer,
73 gsize out_buffer_size,
74 GCancellable *cancellable,
75 GError **error)
76 {
77 gsize chunk_size;
78 guint n_chunks;
79 goffset chunk_offset;
80 gssize n_read;
81
82 g_return_val_if_fail (QFU_IS_IMAGE (self), -1);
83
84 g_debug ("[qfu-image] reading chunk #%u", chunk_i);
85
86 n_chunks = qfu_image_get_n_data_chunks (self);
87 if (chunk_i >= n_chunks) {
88 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
89 "invalid chunk index %u", chunk_i);
90 return -1;
91 }
92
93 /* Last chunk may be shorter than the default */
94 chunk_size = qfu_image_get_data_chunk_size (self, chunk_i);
95 g_debug ("[qfu-image] chunk #%u size: %" G_GSIZE_FORMAT " bytes", chunk_i, chunk_size);
96
97 /* Make sure there's enough room */
98 if (out_buffer_size < chunk_size) {
99 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
100 "buffer too small to fit chunk size: %" G_GSIZE_FORMAT, chunk_size);
101 return -1;
102 }
103
104 /* Compute chunk offset */
105 chunk_offset = qfu_image_get_header_size (self) + ((goffset)chunk_i * QFU_IMAGE_CHUNK_SIZE);
106 g_debug ("[qfu-image] chunk #%u offset: %" G_GOFFSET_FORMAT " bytes", chunk_i, chunk_offset);
107
108 /* Seek to the correct place: note that this is likely a noop if already in that offset */
109 if (!g_seekable_seek (G_SEEKABLE (self->priv->input_stream), chunk_offset, G_SEEK_SET, cancellable, error)) {
110 g_prefix_error (error, "couldn't seek input stream: ");
111 return -1;
112 }
113
114 /* Read full chunk */
115 n_read = g_input_stream_read (self->priv->input_stream,
116 out_buffer,
117 chunk_size,
118 cancellable,
119 error);
120 if (n_read < 0) {
121 g_prefix_error (error, "couldn't read chunk %u", chunk_i);
122 return -1;
123 }
124
125 if ((gsize)n_read != chunk_size) {
126 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
127 "couldn't read expected chunk %u size: %" G_GSIZE_FORMAT " (%" G_GSSIZE_FORMAT " bytes read)",
128 chunk_i, chunk_size, n_read);
129 return -1;
130 }
131
132 g_debug ("[qfu-image] chunk #%u successfully read", chunk_i);
133
134 return chunk_size;
135 }
136
137 /******************************************************************************/
138
139 gssize
140 qfu_image_read (QfuImage *self,
141 goffset offset,
142 gsize size,
143 guint8 *out_buffer,
144 gsize out_buffer_size,
145 GCancellable *cancellable,
146 GError **error)
147 {
148 gssize n_read;
149 goffset remaining_size;
150 gsize read_size;
151
152 g_return_val_if_fail (QFU_IS_IMAGE (self), -1);
153
154 remaining_size = qfu_image_get_size (self) - offset;
155 g_assert (remaining_size >= 0);
156 read_size = ((gsize)remaining_size > size) ? size : (gsize)remaining_size;
157
158 g_debug ("[qfu-image] reading [%" G_GOFFSET_FORMAT "-%" G_GOFFSET_FORMAT "]",
159 offset, offset + read_size);
160
161 /* Make sure there's enough room */
162 if (out_buffer_size < read_size) {
163 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
164 "buffer too small to fit read size: %" G_GSIZE_FORMAT, read_size);
165 return -1;
166 }
167
168 /* Seek to the correct place: note that this is likely a noop if already in that offset */
169 if (!g_seekable_seek (G_SEEKABLE (self->priv->input_stream), offset, G_SEEK_SET, cancellable, error)) {
170 g_prefix_error (error, "couldn't seek input stream: ");
171 return -1;
172 }
173
174 /* Read full chunk */
175 n_read = g_input_stream_read (self->priv->input_stream,
176 out_buffer,
177 read_size,
178 cancellable,
179 error);
180 if (n_read < 0) {
181 g_prefix_error (error, "couldn't read data at offset %" G_GOFFSET_FORMAT, offset);
182 return -1;
183 }
184
185 if ((gsize)n_read != read_size) {
186 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
187 "couldn't read expected data size: %" G_GSIZE_FORMAT " (%" G_GSSIZE_FORMAT " bytes read)",
188 read_size, n_read);
189 return -1;
190 }
191
192 g_debug ("[qfu-image] data at offset %" G_GOFFSET_FORMAT " successfully read", offset);
193
194 return read_size;
195 }
196
197 /******************************************************************************/
198
199 QfuImageType
200 qfu_image_get_image_type (QfuImage *self)
201 {
202 g_return_val_if_fail (QFU_IS_IMAGE (self), QFU_IMAGE_TYPE_UNKNOWN);
203
204 return self->priv->image_type;
205 }
206
207 const gchar *
208 qfu_image_get_display_name (QfuImage *self)
209 {
210 g_return_val_if_fail (QFU_IS_IMAGE (self), NULL);
211
212 return g_file_info_get_display_name (self->priv->info);
213 }
214
215 goffset
216 qfu_image_get_size (QfuImage *self)
217 {
218 g_return_val_if_fail (QFU_IS_IMAGE (self), 0);
219
220 return g_file_info_get_size (self->priv->info);
221 }
222
223 goffset
224 qfu_image_get_header_size (QfuImage *self)
225 {
226 g_return_val_if_fail (QFU_IS_IMAGE (self), 0);
227
228 return QFU_IMAGE_GET_CLASS (self)->get_header_size (self);
229 }
230
231 gssize
232 qfu_image_read_header (QfuImage *self,
233 guint8 *out_buffer,
234 gsize out_buffer_size,
235 GCancellable *cancellable,
236 GError **error)
237 {
238 g_return_val_if_fail (QFU_IS_IMAGE (self), 0);
239
240 return (QFU_IMAGE_GET_CLASS (self)->read_header ?
241 QFU_IMAGE_GET_CLASS (self)->read_header (self, out_buffer, out_buffer_size, cancellable, error) :
242 0);
243 }
244
245 goffset
246 qfu_image_get_data_size (QfuImage *self)
247 {
248 g_return_val_if_fail (QFU_IS_IMAGE (self), 0);
249
250 return QFU_IMAGE_GET_CLASS (self)->get_data_size (self);
251 }
252
253 guint16
254 qfu_image_get_n_data_chunks (QfuImage *self)
255 {
256 goffset data_size;
257
258 data_size = qfu_image_get_data_size (self);
259 g_assert (data_size <= ((goffset) G_MAXUINT16 * (goffset) QFU_IMAGE_CHUNK_SIZE));
260
261 return (guint16) (data_size / QFU_IMAGE_CHUNK_SIZE) + !!(data_size % QFU_IMAGE_CHUNK_SIZE);
262 }
263
264 /******************************************************************************/
265
266 static goffset
267 get_header_size (QfuImage *self)
268 {
269 return 0;
270 }
271
272 static goffset
273 get_data_size (QfuImage *self)
274 {
275 goffset file_size;
276
277 file_size = qfu_image_get_size (self);
278
279 /* some image types contain trailing garbage - from gobi-loader */
280 if (self->priv->image_type == QFU_IMAGE_TYPE_AMSS_MODEM)
281 file_size -= 8;
282
283 return file_size;
284 }
285
286 /******************************************************************************/
287
288 static gboolean
289 initable_init (GInitable *initable,
290 GCancellable *cancellable,
291 GError **error)
292 {
293 QfuImage *self;
294
295 self = QFU_IMAGE (initable);
296 g_assert (self->priv->file);
297
298 /* Load file info */
299 g_debug ("[qfu-image] loading file info...");
300 self->priv->info = g_file_query_info (self->priv->file,
301 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
302 G_FILE_QUERY_INFO_NONE,
303 cancellable,
304 error);
305 if (!self->priv->info)
306 return FALSE;
307
308 /* Check minimum file size */
309 if (qfu_image_get_size (self) < qfu_image_get_header_size (self)) {
310 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "image is too short");
311 return FALSE;
312 }
313
314 /* Open file for reading. Kept open while the input stream reference is valid. */
315 g_debug ("[qfu-image] opening file for reading...");
316 self->priv->input_stream = G_INPUT_STREAM (g_file_read (self->priv->file, cancellable, error));
317 if (!self->priv->input_stream)
318 return FALSE;
319
320 return TRUE;
321 }
322
323 /******************************************************************************/
324
325 QfuImage *
326 qfu_image_new (GFile *file,
327 QfuImageType image_type,
328 GCancellable *cancellable,
329 GError **error)
330 {
331 g_return_val_if_fail (G_IS_FILE (file), NULL);
332
333 return QFU_IMAGE (g_initable_new (QFU_TYPE_IMAGE,
334 cancellable,
335 error,
336 "file", file,
337 "image-type", image_type,
338 NULL));
339 }
340
341 static void
342 qfu_image_init (QfuImage *self)
343 {
344 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, QFU_TYPE_IMAGE, QfuImagePrivate);
345 self->priv->image_type = QFU_IMAGE_TYPE_UNKNOWN;
346 }
347
348 static void
349 set_property (GObject *object,
350 guint prop_id,
351 const GValue *value,
352 GParamSpec *pspec)
353 {
354 QfuImage *self = QFU_IMAGE (object);
355
356 switch (prop_id) {
357 case PROP_FILE:
358 self->priv->file = g_value_dup_object (value);
359 break;
360 case PROP_IMAGE_TYPE:
361 self->priv->image_type = g_value_get_enum (value);
362 break;
363 default:
364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 break;
366 }
367 }
368
369 static void
370 get_property (GObject *object,
371 guint prop_id,
372 GValue *value,
373 GParamSpec *pspec)
374 {
375 QfuImage *self = QFU_IMAGE (object);
376
377 switch (prop_id) {
378 case PROP_FILE:
379 g_value_set_object (value, self->priv->file);
380 break;
381 case PROP_IMAGE_TYPE:
382 g_value_set_enum (value, self->priv->image_type);
383 break;
384 case PROP_INPUT_STREAM:
385 g_value_set_object (value, self->priv->input_stream);
386 break;
387 default:
388 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
389 break;
390 }
391 }
392
393 static void
394 dispose (GObject *object)
395 {
396 QfuImage *self = QFU_IMAGE (object);
397
398 g_clear_object (&self->priv->input_stream);
399 g_clear_object (&self->priv->info);
400 g_clear_object (&self->priv->file);
401
402 G_OBJECT_CLASS (qfu_image_parent_class)->dispose (object);
403 }
404
405 static void
406 initable_iface_init (GInitableIface *iface)
407 {
408 iface->init = initable_init;
409 }
410
411 static void
412 qfu_image_class_init (QfuImageClass *klass)
413 {
414 GObjectClass *object_class = G_OBJECT_CLASS (klass);
415
416 g_type_class_add_private (object_class, sizeof (QfuImagePrivate));
417
418 object_class->dispose = dispose;
419 object_class->get_property = get_property;
420 object_class->set_property = set_property;
421
422 klass->get_header_size = get_header_size;
423 klass->get_data_size = get_data_size;
424
425 properties[PROP_FILE] =
426 g_param_spec_object ("file",
427 "File",
428 "File object",
429 G_TYPE_FILE,
430 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
431 g_object_class_install_property (object_class, PROP_FILE, properties[PROP_FILE]);
432
433 properties[PROP_IMAGE_TYPE] =
434 g_param_spec_enum ("image-type",
435 "Image type",
436 "Type of firmware image",
437 QFU_TYPE_IMAGE_TYPE,
438 QFU_IMAGE_TYPE_UNKNOWN,
439 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
440 g_object_class_install_property (object_class, PROP_IMAGE_TYPE, properties[PROP_IMAGE_TYPE]);
441
442 properties[PROP_INPUT_STREAM] =
443 g_param_spec_object ("input-stream",
444 "Input stream",
445 "Input stream object",
446 G_TYPE_FILE_INPUT_STREAM,
447 G_PARAM_READABLE);
448 g_object_class_install_property (object_class, PROP_INPUT_STREAM, properties[PROP_INPUT_STREAM]);
449 }