-
Notifications
You must be signed in to change notification settings - Fork 411
/
fu-firmware.c
2051 lines (1884 loc) · 52.5 KB
/
fu-firmware.c
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2019 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#define G_LOG_DOMAIN "FuFirmware"
#include "config.h"
#include "fu-byte-array.h"
#include "fu-bytes.h"
#include "fu-chunk-private.h"
#include "fu-common.h"
#include "fu-firmware.h"
#include "fu-mem.h"
#include "fu-string.h"
/**
* FuFirmware:
*
* A firmware file which can have children which represent the images within.
*
* See also: [class@FuDfuFirmware], [class@FuIhexFirmware], [class@FuSrecFirmware]
*/
typedef struct {
FuFirmwareFlags flags;
FuFirmware *parent; /* noref */
GPtrArray *images; /* FuFirmware */
gchar *version;
guint64 version_raw;
GBytes *bytes;
guint8 alignment;
gchar *id;
gchar *filename;
guint64 idx;
guint64 addr;
guint64 offset;
gsize size;
GPtrArray *chunks; /* nullable, element-type FuChunk */
GPtrArray *patches; /* nullable, element-type FuFirmwarePatch */
} FuFirmwarePrivate;
G_DEFINE_TYPE_WITH_PRIVATE(FuFirmware, fu_firmware, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (fu_firmware_get_instance_private(o))
enum { PROP_0, PROP_PARENT, PROP_LAST };
#define FU_FIRMWARE_SEARCH_MAGIC_BUFSZ_MAX (32 * 1024 * 1024)
/**
* fu_firmware_flag_to_string:
* @flag: a #FuFirmwareFlags, e.g. %FU_FIRMWARE_FLAG_DEDUPE_ID
*
* Converts a #FuFirmwareFlags to a string.
*
* Returns: identifier string
*
* Since: 1.5.0
**/
const gchar *
fu_firmware_flag_to_string(FuFirmwareFlags flag)
{
if (flag == FU_FIRMWARE_FLAG_NONE)
return "none";
if (flag == FU_FIRMWARE_FLAG_DEDUPE_ID)
return "dedupe-id";
if (flag == FU_FIRMWARE_FLAG_DEDUPE_IDX)
return "dedupe-idx";
if (flag == FU_FIRMWARE_FLAG_HAS_CHECKSUM)
return "has-checksum";
if (flag == FU_FIRMWARE_FLAG_HAS_VID_PID)
return "has-vid-pid";
if (flag == FU_FIRMWARE_FLAG_DONE_PARSE)
return "done-parse";
if (flag == FU_FIRMWARE_FLAG_HAS_STORED_SIZE)
return "has-stored-size";
if (flag == FU_FIRMWARE_FLAG_ALWAYS_SEARCH)
return "always-search";
return NULL;
}
/**
* fu_firmware_flag_from_string:
* @flag: a string, e.g. `dedupe-id`
*
* Converts a string to a #FuFirmwareFlags.
*
* Returns: enumerated value
*
* Since: 1.5.0
**/
FuFirmwareFlags
fu_firmware_flag_from_string(const gchar *flag)
{
if (g_strcmp0(flag, "dedupe-id") == 0)
return FU_FIRMWARE_FLAG_DEDUPE_ID;
if (g_strcmp0(flag, "dedupe-idx") == 0)
return FU_FIRMWARE_FLAG_DEDUPE_IDX;
if (g_strcmp0(flag, "has-checksum") == 0)
return FU_FIRMWARE_FLAG_HAS_CHECKSUM;
if (g_strcmp0(flag, "has-vid-pid") == 0)
return FU_FIRMWARE_FLAG_HAS_VID_PID;
if (g_strcmp0(flag, "done-parse") == 0)
return FU_FIRMWARE_FLAG_DONE_PARSE;
if (g_strcmp0(flag, "has-stored-size") == 0)
return FU_FIRMWARE_FLAG_HAS_STORED_SIZE;
if (g_strcmp0(flag, "always-search") == 0)
return FU_FIRMWARE_FLAG_ALWAYS_SEARCH;
return FU_FIRMWARE_FLAG_NONE;
}
typedef struct {
gsize offset;
GBytes *blob;
} FuFirmwarePatch;
static void
fu_firmware_patch_free(FuFirmwarePatch *ptch)
{
g_bytes_unref(ptch->blob);
g_free(ptch);
}
/**
* fu_firmware_add_flag:
* @firmware: a #FuFirmware
* @flag: the firmware flag
*
* Adds a specific firmware flag to the firmware.
*
* Since: 1.5.0
**/
void
fu_firmware_add_flag(FuFirmware *firmware, FuFirmwareFlags flag)
{
FuFirmwarePrivate *priv = GET_PRIVATE(firmware);
g_return_if_fail(FU_IS_FIRMWARE(firmware));
priv->flags |= flag;
}
/**
* fu_firmware_has_flag:
* @firmware: a #FuFirmware
* @flag: the firmware flag
*
* Finds if the firmware has a specific firmware flag.
*
* Returns: %TRUE if the flag is set
*
* Since: 1.5.0
**/
gboolean
fu_firmware_has_flag(FuFirmware *firmware, FuFirmwareFlags flag)
{
FuFirmwarePrivate *priv = GET_PRIVATE(firmware);
g_return_val_if_fail(FU_IS_FIRMWARE(firmware), FALSE);
return (priv->flags & flag) > 0;
}
/**
* fu_firmware_get_version:
* @self: a #FuFirmware
*
* Gets an optional version that represents the firmware.
*
* Returns: a string, or %NULL
*
* Since: 1.3.3
**/
const gchar *
fu_firmware_get_version(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), NULL);
return priv->version;
}
/**
* fu_firmware_set_version:
* @self: a #FuFirmware
* @version: (nullable): optional string version
*
* Sets an optional version that represents the firmware.
*
* Since: 1.3.3
**/
void
fu_firmware_set_version(FuFirmware *self, const gchar *version)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
/* not changed */
if (g_strcmp0(priv->version, version) == 0)
return;
g_free(priv->version);
priv->version = g_strdup(version);
}
/**
* fu_firmware_get_version_raw:
* @self: a #FuFirmware
*
* Gets an raw version that represents the firmware. This is most frequently
* used when building firmware with `<version_raw>0x123456</version_raw>` in a
* `firmware.builder.xml` file to avoid string splitting and sanity checks.
*
* Returns: an integer, or %G_MAXUINT64 for invalid
*
* Since: 1.5.7
**/
guint64
fu_firmware_get_version_raw(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), G_MAXUINT64);
return priv->version_raw;
}
/**
* fu_firmware_set_version_raw:
* @self: a #FuFirmware
* @version_raw: a raw version, or %G_MAXUINT64 for invalid
*
* Sets an raw version that represents the firmware.
*
* This is optional, and is typically only used for debugging.
*
* Since: 1.5.7
**/
void
fu_firmware_set_version_raw(FuFirmware *self, guint64 version_raw)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
priv->version_raw = version_raw;
}
/**
* fu_firmware_get_filename:
* @self: a #FuFirmware
*
* Gets an optional filename that represents the image source or destination.
*
* Returns: a string, or %NULL
*
* Since: 1.6.0
**/
const gchar *
fu_firmware_get_filename(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), NULL);
return priv->filename;
}
/**
* fu_firmware_set_filename:
* @self: a #FuFirmware
* @filename: (nullable): a string filename
*
* Sets an optional filename that represents the image source or destination.
*
* Since: 1.6.0
**/
void
fu_firmware_set_filename(FuFirmware *self, const gchar *filename)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
/* not changed */
if (g_strcmp0(priv->filename, filename) == 0)
return;
g_free(priv->filename);
priv->filename = g_strdup(filename);
}
/**
* fu_firmware_set_id:
* @self: a #FuPlugin
* @id: (nullable): image ID, e.g. `config`
*
* Since: 1.6.0
**/
void
fu_firmware_set_id(FuFirmware *self, const gchar *id)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
/* not changed */
if (g_strcmp0(priv->id, id) == 0)
return;
g_free(priv->id);
priv->id = g_strdup(id);
}
/**
* fu_firmware_get_id:
* @self: a #FuPlugin
*
* Gets the image ID, typically set at construction.
*
* Returns: image ID, e.g. `config`
*
* Since: 1.6.0
**/
const gchar *
fu_firmware_get_id(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), NULL);
return priv->id;
}
/**
* fu_firmware_set_addr:
* @self: a #FuPlugin
* @addr: integer
*
* Sets the base address of the image.
*
* Since: 1.6.0
**/
void
fu_firmware_set_addr(FuFirmware *self, guint64 addr)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
priv->addr = addr;
}
/**
* fu_firmware_get_addr:
* @self: a #FuPlugin
*
* Gets the base address of the image.
*
* Returns: integer
*
* Since: 1.6.0
**/
guint64
fu_firmware_get_addr(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), G_MAXUINT64);
return priv->addr;
}
/**
* fu_firmware_set_offset:
* @self: a #FuPlugin
* @offset: integer
*
* Sets the base offset of the image.
*
* Since: 1.6.0
**/
void
fu_firmware_set_offset(FuFirmware *self, guint64 offset)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
priv->offset = offset;
}
/**
* fu_firmware_get_offset:
* @self: a #FuPlugin
*
* Gets the base offset of the image.
*
* Returns: integer
*
* Since: 1.6.0
**/
guint64
fu_firmware_get_offset(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), G_MAXUINT64);
return priv->offset;
}
/**
* fu_firmware_get_parent:
* @self: a #FuFirmware
*
* Gets the parent.
*
* Returns: (transfer none): the parent firmware, or %NULL if unset
*
* Since: 1.8.2
**/
FuFirmware *
fu_firmware_get_parent(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), NULL);
return priv->parent;
}
/**
* fu_firmware_set_parent:
* @self: a #FuFirmware
* @parent: (nullable): another #FuFirmware
*
* Sets the parent. Only used internally.
*
* Since: 1.8.2
**/
void
fu_firmware_set_parent(FuFirmware *self, FuFirmware *parent)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
if (priv->parent != NULL)
g_object_remove_weak_pointer(G_OBJECT(priv->parent), (gpointer *)&priv->parent);
if (parent != NULL)
g_object_add_weak_pointer(G_OBJECT(parent), (gpointer *)&priv->parent);
priv->parent = parent;
}
/**
* fu_firmware_set_size:
* @self: a #FuPlugin
* @size: integer
*
* Sets the total size of the image, which should be the same size as the
* data from fu_firmware_write().
*
* Since: 1.6.0
**/
void
fu_firmware_set_size(FuFirmware *self, gsize size)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
priv->size = size;
}
/**
* fu_firmware_get_size:
* @self: a #FuPlugin
*
* Gets the total size of the image, which is typically the same size as the
* data from fu_firmware_write().
*
* If the size has not been explicitly set, and fu_firmware_set_bytes() has been
* used then the size of this is used instead.
*
* Returns: integer
*
* Since: 1.6.0
**/
gsize
fu_firmware_get_size(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), G_MAXSIZE);
if (priv->size != 0)
return priv->size;
if (priv->bytes != NULL)
return g_bytes_get_size(priv->bytes);
return 0;
}
/**
* fu_firmware_set_idx:
* @self: a #FuPlugin
* @idx: integer
*
* Sets the index of the image which is used for ordering.
*
* Since: 1.6.0
**/
void
fu_firmware_set_idx(FuFirmware *self, guint64 idx)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
priv->idx = idx;
}
/**
* fu_firmware_get_idx:
* @self: a #FuPlugin
*
* Gets the index of the image which is used for ordering.
*
* Returns: integer
*
* Since: 1.6.0
**/
guint64
fu_firmware_get_idx(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), G_MAXUINT64);
return priv->idx;
}
/**
* fu_firmware_set_bytes:
* @self: a #FuPlugin
* @bytes: data blob
*
* Sets the contents of the image if not created with fu_firmware_new_from_bytes().
*
* Since: 1.6.0
**/
void
fu_firmware_set_bytes(FuFirmware *self, GBytes *bytes)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
g_return_if_fail(bytes != NULL);
if (priv->bytes != NULL)
g_bytes_unref(priv->bytes);
priv->bytes = g_bytes_ref(bytes);
}
/**
* fu_firmware_get_bytes:
* @self: a #FuPlugin
* @error: (nullable): optional return location for an error
*
* Gets the firmware payload, which does not have any header or footer included.
*
* If there is more than one potential payload or image section then fu_firmware_add_image()
* should be used instead.
*
* Returns: (transfer full): a #GBytes, or %NULL if the payload has never been set
*
* Since: 1.6.0
**/
GBytes *
fu_firmware_get_bytes(FuFirmware *self, GError **error)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), NULL);
if (priv->bytes == NULL) {
g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, "no payload set");
return NULL;
}
return g_bytes_ref(priv->bytes);
}
/**
* fu_firmware_get_bytes_with_patches:
* @self: a #FuPlugin
* @error: (nullable): optional return location for an error
*
* Gets the firmware payload, with any defined patches applied.
*
* Returns: (transfer full): a #GBytes, or %NULL if the payload has never been set
*
* Since: 1.7.4
**/
GBytes *
fu_firmware_get_bytes_with_patches(FuFirmware *self, GError **error)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_autoptr(GByteArray) buf = g_byte_array_new();
g_return_val_if_fail(FU_IS_FIRMWARE(self), NULL);
if (priv->bytes == NULL) {
g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, "no payload set");
return NULL;
}
/* usual case */
if (priv->patches == NULL)
return g_bytes_ref(priv->bytes);
/* convert to a mutable buffer, apply each patch, aborting if the offset isn't valid */
fu_byte_array_append_bytes(buf, priv->bytes);
for (guint i = 0; i < priv->patches->len; i++) {
FuFirmwarePatch *ptch = g_ptr_array_index(priv->patches, i);
if (!fu_memcpy_safe(buf->data,
buf->len,
ptch->offset, /* dst */
g_bytes_get_data(ptch->blob, NULL),
g_bytes_get_size(ptch->blob),
0x0, /* src */
g_bytes_get_size(ptch->blob),
error)) {
g_prefix_error(error, "failed to apply patch @0x%x: ", (guint)ptch->offset);
return NULL;
}
}
/* success */
return g_byte_array_free_to_bytes(g_steal_pointer(&buf));
}
/**
* fu_firmware_set_alignment:
* @self: a #FuFirmware
* @alignment: integer, or 0 to disable
*
* Sets the alignment of the firmware.
*
* This allows a firmware to pad to a power of 2 boundary, where @alignment
* is the bit position to align to.
*
* Since: 1.6.0
**/
void
fu_firmware_set_alignment(FuFirmware *self, guint8 alignment)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
priv->alignment = alignment;
}
/**
* fu_firmware_get_alignment:
* @self: a #FuFirmware
*
* Gets the alignment of the firmware.
*
* This allows a firmware to pad to a power of 2 boundary, where @alignment
* is the bit position to align to.
*
* Returns: integer
*
* Since: 1.6.0
**/
guint8
fu_firmware_get_alignment(FuFirmware *self)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), G_MAXUINT8);
return priv->alignment;
}
/**
* fu_firmware_get_chunks:
* @self: a #FuFirmware
* @error: (nullable): optional return location for an error
*
* Gets the optional image chunks.
*
* Returns: (transfer container) (element-type FuChunk) (nullable): chunk data, or %NULL
*
* Since: 1.6.0
**/
GPtrArray *
fu_firmware_get_chunks(FuFirmware *self, GError **error)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
/* set */
if (priv->chunks != NULL)
return g_ptr_array_ref(priv->chunks);
/* lets build something plausible */
if (priv->bytes != NULL) {
g_autoptr(GPtrArray) chunks = NULL;
g_autoptr(FuChunk) chk = NULL;
chunks = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref);
chk = fu_chunk_bytes_new(priv->bytes);
fu_chunk_set_idx(chk, priv->idx);
fu_chunk_set_address(chk, priv->addr);
g_ptr_array_add(chunks, g_steal_pointer(&chk));
return g_steal_pointer(&chunks);
}
/* nothing to do */
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_FOUND,
"no bytes or chunks found in firmware");
return NULL;
}
/**
* fu_firmware_add_chunk:
* @self: a #FuFirmware
* @chk: a #FuChunk
*
* Adds a chunk to the image.
*
* Since: 1.6.0
**/
void
fu_firmware_add_chunk(FuFirmware *self, FuChunk *chk)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_if_fail(FU_IS_FIRMWARE(self));
g_return_if_fail(FU_IS_CHUNK(chk));
if (priv->chunks == NULL)
priv->chunks = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref);
g_ptr_array_add(priv->chunks, g_object_ref(chk));
}
/**
* fu_firmware_get_checksum:
* @self: a #FuPlugin
* @csum_kind: a checksum type, e.g. %G_CHECKSUM_SHA256
* @error: (nullable): optional return location for an error
*
* Returns a checksum of the payload data.
*
* Returns: (transfer full): a checksum string, or %NULL if the checksum is not available
*
* Since: 1.6.0
**/
gchar *
fu_firmware_get_checksum(FuFirmware *self, GChecksumType csum_kind, GError **error)
{
FuFirmwarePrivate *priv = GET_PRIVATE(self);
FuFirmwareClass *klass = FU_FIRMWARE_GET_CLASS(self);
g_autoptr(GBytes) blob = NULL;
g_return_val_if_fail(FU_IS_FIRMWARE(self), NULL);
g_return_val_if_fail(error == NULL || *error == NULL, NULL);
/* subclassed */
if (klass->get_checksum != NULL)
return klass->get_checksum(self, csum_kind, error);
/* internal data */
if (priv->bytes != NULL)
return g_compute_checksum_for_bytes(csum_kind, priv->bytes);
/* write */
blob = fu_firmware_write(self, error);
if (blob == NULL)
return NULL;
return g_compute_checksum_for_bytes(csum_kind, blob);
}
/**
* fu_firmware_tokenize:
* @self: a #FuFirmware
* @fw: firmware blob
* @flags: install flags, e.g. %FWUPD_INSTALL_FLAG_FORCE
* @error: (nullable): optional return location for an error
*
* Tokenizes a firmware, typically breaking the firmware into records.
*
* Records can be enumerated using subclass-specific functionality, for example
* using fu_srec_firmware_get_records().
*
* Returns: %TRUE for success
*
* Since: 1.3.2
**/
gboolean
fu_firmware_tokenize(FuFirmware *self, GBytes *fw, FwupdInstallFlags flags, GError **error)
{
FuFirmwareClass *klass = FU_FIRMWARE_GET_CLASS(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), FALSE);
g_return_val_if_fail(fw != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* optionally subclassed */
if (klass->tokenize != NULL)
return klass->tokenize(self, fw, flags, error);
return TRUE;
}
/**
* fu_firmware_check_compatible:
* @self: a #FuFirmware
* @other: a #FuFirmware
* @flags: install flags, e.g. %FWUPD_INSTALL_FLAG_FORCE
* @error: (nullable): optional return location for an error
*
* Check a new firmware is compatible with the existing firmware.
*
* Returns: %TRUE for success
*
* Since: 1.8.4
**/
gboolean
fu_firmware_check_compatible(FuFirmware *self,
FuFirmware *other,
FwupdInstallFlags flags,
GError **error)
{
FuFirmwareClass *klass = FU_FIRMWARE_GET_CLASS(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), FALSE);
g_return_val_if_fail(FU_IS_FIRMWARE(other), FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* optionally subclassed */
if (klass->check_compatible == NULL)
return TRUE;
return klass->check_compatible(self, other, flags, error);
}
static gboolean
fu_firmware_check_magic_for_offset(FuFirmware *self,
GBytes *fw,
gsize *offset,
FwupdInstallFlags flags,
GError **error)
{
FuFirmwareClass *klass = FU_FIRMWARE_GET_CLASS(self);
/* not implemented */
if (klass->check_magic == NULL)
return TRUE;
/* fuzzing */
if (!fu_firmware_has_flag(self, FU_FIRMWARE_FLAG_ALWAYS_SEARCH) &&
(flags & FWUPD_INSTALL_FLAG_NO_SEARCH) > 0) {
if (!klass->check_magic(self, fw, *offset, error)) {
g_prefix_error(error, "not searching magic due to install flags: ");
return FALSE;
}
return TRUE;
}
/* limit the size of firmware we search */
if (g_bytes_get_size(fw) > FU_FIRMWARE_SEARCH_MAGIC_BUFSZ_MAX) {
if (!klass->check_magic(self, fw, *offset, error)) {
g_prefix_error(error,
"failed to search for magic as firmware size was 0x%x and "
"limit was 0x%x: ",
(guint)g_bytes_get_size(fw),
(guint)FU_FIRMWARE_SEARCH_MAGIC_BUFSZ_MAX);
return FALSE;
}
return TRUE;
}
/* increment the offset, looking for the magic */
for (gsize offset_tmp = *offset; offset_tmp < g_bytes_get_size(fw); offset_tmp++) {
if (klass->check_magic(self, fw, offset_tmp, NULL)) {
fu_firmware_set_offset(self, offset_tmp);
*offset = offset_tmp;
return TRUE;
}
}
/* did not find what we were looking for */
g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_INVALID_FILE, "did not find magic");
return FALSE;
}
/**
* fu_firmware_parse_full:
* @self: a #FuFirmware
* @fw: firmware blob
* @offset: start offset, useful for ignoring a bootloader
* @flags: install flags, e.g. %FWUPD_INSTALL_FLAG_FORCE
* @error: (nullable): optional return location for an error
*
* Parses a firmware, typically breaking the firmware into images.
*
* Returns: %TRUE for success
*
* Since: 1.8.2
**/
gboolean
fu_firmware_parse_full(FuFirmware *self,
GBytes *fw,
gsize offset,
FwupdInstallFlags flags,
GError **error)
{
FuFirmwareClass *klass = FU_FIRMWARE_GET_CLASS(self);
FuFirmwarePrivate *priv = GET_PRIVATE(self);
g_return_val_if_fail(FU_IS_FIRMWARE(self), FALSE);
g_return_val_if_fail(fw != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
/* sanity check */
if (fu_firmware_has_flag(self, FU_FIRMWARE_FLAG_DONE_PARSE)) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"firmware object cannot be reused");
return FALSE;
}
if (g_bytes_get_size(fw) == 0) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"invalid firmware as zero sized");
return FALSE;
}
/* any FuFirmware subclass that gets past this point might have allocated memory in
* ->tokenize() or ->parse() and needs to be destroyed before parsing again */
fu_firmware_add_flag(self, FU_FIRMWARE_FLAG_DONE_PARSE);
/* subclassed */
if (klass->tokenize != NULL) {
if (!klass->tokenize(self, fw, flags, error))
return FALSE;
}
if (!fu_firmware_check_magic_for_offset(self, fw, &offset, flags, error))
return FALSE;
/* always set by default */
fu_firmware_set_bytes(self, fw);
/* handled by the subclass */
if (klass->parse != NULL)
return klass->parse(self, fw, offset, flags, error);
/* verify alignment */
if (g_bytes_get_size(fw) % (1ull << priv->alignment) != 0) {
g_autofree gchar *str = NULL;
str = g_format_size_full(1ull << priv->alignment, G_FORMAT_SIZE_IEC_UNITS);
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_FILE,
"raw firmware is not aligned to 0x%x (%s)",
(guint)(1ull << priv->alignment),
str);
return FALSE;
}
/* success */
return TRUE;
}
/**
* fu_firmware_parse:
* @self: a #FuFirmware
* @fw: firmware blob
* @flags: install flags, e.g. %FWUPD_INSTALL_FLAG_FORCE
* @error: (nullable): optional return location for an error
*
* Parses a firmware, typically breaking the firmware into images.
*
* Returns: %TRUE for success
*
* Since: 1.3.1
**/
gboolean
fu_firmware_parse(FuFirmware *self, GBytes *fw, FwupdInstallFlags flags, GError **error)
{
return fu_firmware_parse_full(self, fw, 0x0, flags, error);
}
/**
* fu_firmware_build:
* @self: a #FuFirmware
* @n: a Xmlb node
* @error: (nullable): optional return location for an error
*
* Builds a firmware from an XML manifest. The manifest would typically have the
* following form:
*
* |[<!-- language="XML" -->
* <?xml version="1.0" encoding="UTF-8"?>
* <firmware gtype="FuBcm57xxFirmware">
* <version>1.2.3</version>
* <firmware gtype="FuBcm57xxStage1Image">
* <version>7.8.9</version>
* <id>stage1</id>
* <idx>0x01</idx>
* <filename>stage1.bin</filename>
* </firmware>
* <firmware gtype="FuBcm57xxStage2Image">
* <id>stage2</id>
* <data/> <!-- empty! -->
* </firmware>
* <firmware gtype="FuBcm57xxDictImage">
* <id>ape</id>
* <addr>0x7</addr>
* <data>aGVsbG8gd29ybGQ=</data> <!-- base64 -->
* </firmware>
* </firmware>
* ]|
*
* This would be used in a build-system to merge images from generated files:
* `fwupdtool firmware-build fw.builder.xml test.fw`
*
* Static binary content can be specified in the `<firmware>/<data>` section and
* is encoded as base64 text if not empty.
*
* Additionally, extra nodes can be included under nested `<firmware>` objects
* which can be parsed by the subclassed objects. You should verify the
* subclassed object `FuFirmware->build` vfunc for the specific additional
* options supported.
*
* Plugins should manually g_type_ensure() subclassed image objects if not