-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifwitool.c
2300 lines (1869 loc) · 57.6 KB
/
ifwitool.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
// SPDX-License-Identifier: GPL-2.0
/*
* ifwitool, CLI utility for Integrated Firmware Image (IFWI) manipulation
*
* This is taken from the Coreboot project
*/
#include <assert.h>
#include <stdbool.h>
#include <getopt.h>
#include "imagetool.h"
#include "os_support.h"
#ifndef __packed
#define __packed __attribute__((packed))
#endif
#define KiB 1024
/*
* min()/max()/clamp() macros that also do
* strict type-checking.. See the
* "unnecessary" pointer comparison.
*/
#define min(x, y) ({ \
typeof(x) _min1 = (x); \
typeof(y) _min2 = (y); \
(void)&_min1 == &_min2); \
_min1 < _min2 ? _min1 : _min2; })
#define max(x, y) ({ \
typeof(x) _max1 = (x); \
typeof(y) _max2 = (y); \
(void)(&_max1 == &_max2); \
_max1 > _max2 ? _max1 : _max2; })
static int verbose = 1;
/* Buffer and file I/O */
struct buffer {
char *name;
char *data;
size_t offset;
size_t size;
};
#define ERROR(...) { fprintf(stderr, "E: " __VA_ARGS__); }
#define INFO(...) { if (verbose > 0) fprintf(stderr, "INFO: " __VA_ARGS__); }
#define DEBUG(...) { if (verbose > 1) fprintf(stderr, "DEBUG: " __VA_ARGS__); }
/*
* BPDT is Boot Partition Descriptor Table. It is located at the start of a
* logical boot partition(LBP). It stores information about the critical
* sub-partitions present within the LBP.
*
* S-BPDT is Secondary Boot Partition Descriptor Table. It is located after the
* critical sub-partitions and contains information about the non-critical
* sub-partitions present within the LBP.
*
* Both tables are identified by BPDT_SIGNATURE stored at the start of the
* table.
*/
#define BPDT_SIGNATURE (0x000055AA)
/* Parameters passed in by caller */
static struct param {
const char *file_name;
const char *subpart_name;
const char *image_name;
bool dir_ops;
const char *dentry_name;
} param;
struct bpdt_header {
/*
* This is used to identify start of BPDT. It should always be
* BPDT_SIGNATURE.
*/
uint32_t signature;
/* Count of BPDT entries present */
uint16_t descriptor_count;
/* Version - Currently supported = 1 */
uint16_t bpdt_version;
/* Unused - Should be 0 */
uint32_t xor_redundant_block;
/* Version of IFWI build */
uint32_t ifwi_version;
/* Version of FIT tool used to create IFWI */
uint64_t fit_tool_version;
} __packed;
#define BPDT_HEADER_SIZE (sizeof(struct bpdt_header))
struct bpdt_entry {
/* Type of sub-partition */
uint16_t type;
/* Attributes of sub-partition */
uint16_t flags;
/* Offset of sub-partition from beginning of LBP */
uint32_t offset;
/* Size in bytes of sub-partition */
uint32_t size;
} __packed;
#define BPDT_ENTRY_SIZE (sizeof(struct bpdt_entry))
struct bpdt {
struct bpdt_header h;
/* In practice, this could be an array of 0 to n entries */
struct bpdt_entry e[0];
} __packed;
static inline size_t get_bpdt_size(struct bpdt_header *h)
{
return (sizeof(*h) + BPDT_ENTRY_SIZE * h->descriptor_count);
}
/* Minimum size in bytes allocated to BPDT in IFWI */
#define BPDT_MIN_SIZE ((size_t)512)
/* Header to define directory header for sub-partition */
struct subpart_dir_header {
/* Should be SUBPART_DIR_MARKER */
uint32_t marker;
/* Number of directory entries in the sub-partition */
uint32_t num_entries;
/* Currenty supported - 1 */
uint8_t header_version;
/* Currenty supported - 1 */
uint8_t entry_version;
/* Length of directory header in bytes */
uint8_t header_length;
/*
* 2s complement of 8-bit sum from first byte of header to last byte of
* last directory entry.
*/
uint8_t checksum;
/* ASCII short name of sub-partition */
uint8_t name[4];
} __packed;
#define SUBPART_DIR_HEADER_SIZE \
(sizeof(struct subpart_dir_header))
#define SUBPART_DIR_MARKER 0x44504324
#define SUBPART_DIR_HEADER_VERSION_SUPPORTED 1
#define SUBPART_DIR_ENTRY_VERSION_SUPPORTED 1
/* Structure for each directory entry for sub-partition */
struct subpart_dir_entry {
/* Name of directory entry - Not guaranteed to be NULL-terminated */
uint8_t name[12];
/* Offset of entry from beginning of sub-partition */
uint32_t offset;
/* Length in bytes of sub-directory entry */
uint32_t length;
/* Must be zero */
uint32_t rsvd;
} __packed;
#define SUBPART_DIR_ENTRY_SIZE \
(sizeof(struct subpart_dir_entry))
struct subpart_dir {
struct subpart_dir_header h;
/* In practice, this could be an array of 0 to n entries */
struct subpart_dir_entry e[0];
} __packed;
static inline size_t subpart_dir_size(struct subpart_dir_header *h)
{
return (sizeof(*h) + SUBPART_DIR_ENTRY_SIZE * h->num_entries);
}
struct manifest_header {
uint32_t header_type;
uint32_t header_length;
uint32_t header_version;
uint32_t flags;
uint32_t vendor;
uint32_t date;
uint32_t size;
uint32_t id;
uint32_t rsvd;
uint64_t version;
uint32_t svn;
uint64_t rsvd1;
uint8_t rsvd2[64];
uint32_t modulus_size;
uint32_t exponent_size;
uint8_t public_key[256];
uint32_t exponent;
uint8_t signature[256];
} __packed;
#define DWORD_SIZE 4
#define MANIFEST_HDR_SIZE (sizeof(struct manifest_header))
#define MANIFEST_ID_MAGIC (0x324e4d24)
struct module {
uint8_t name[12];
uint8_t type;
uint8_t hash_alg;
uint16_t hash_size;
uint32_t metadata_size;
uint8_t metadata_hash[32];
} __packed;
#define MODULE_SIZE (sizeof(struct module))
struct signed_pkg_info_ext {
uint32_t ext_type;
uint32_t ext_length;
uint8_t name[4];
uint32_t vcn;
uint8_t bitmap[16];
uint32_t svn;
uint8_t rsvd[16];
} __packed;
#define SIGNED_PKG_INFO_EXT_TYPE 0x15
#define SIGNED_PKG_INFO_EXT_SIZE \
(sizeof(struct signed_pkg_info_ext))
/*
* Attributes for various IFWI sub-partitions.
* LIES_WITHIN_BPDT_4K = Sub-Partition should lie within the same 4K block as
* BPDT.
* NON_CRITICAL_SUBPART = Sub-Partition entry should be present in S-BPDT.
* CONTAINS_DIR = Sub-Partition contains directory.
* AUTO_GENERATED = Sub-Partition is generated by the tool.
* MANDATORY_BPDT_ENTRY = Even if sub-partition is deleted, BPDT should contain
* an entry for it with size 0 and offset 0.
*/
enum subpart_attributes {
LIES_WITHIN_BPDT_4K = (1 << 0),
NON_CRITICAL_SUBPART = (1 << 1),
CONTAINS_DIR = (1 << 2),
AUTO_GENERATED = (1 << 3),
MANDATORY_BPDT_ENTRY = (1 << 4),
};
/* Type value for various IFWI sub-partitions */
enum bpdt_entry_type {
SMIP_TYPE = 0,
CSE_RBE_TYPE = 1,
CSE_BUP_TYPE = 2,
UCODE_TYPE = 3,
IBB_TYPE = 4,
S_BPDT_TYPE = 5,
OBB_TYPE = 6,
CSE_MAIN_TYPE = 7,
ISH_TYPE = 8,
CSE_IDLM_TYPE = 9,
IFP_OVERRIDE_TYPE = 10,
DEBUG_TOKENS_TYPE = 11,
UFS_PHY_TYPE = 12,
UFS_GPP_TYPE = 13,
PMC_TYPE = 14,
IUNIT_TYPE = 15,
NVM_CONFIG_TYPE = 16,
UEP_TYPE = 17,
UFS_RATE_B_TYPE = 18,
MAX_SUBPARTS = 19,
};
/*
* There are two order requirements for an IFWI image:
* 1. Order in which the sub-partitions lie within the BPDT entries.
* 2. Order in which the sub-partitions lie within the image.
*
* header_order defines #1 i.e. the order in which the sub-partitions should
* appear in the BPDT entries. pack_order defines #2 i.e. the order in which
* sub-partitions appear in the IFWI image. pack_order controls the offset and
* thus sub-partitions would have increasing offsets as we loop over pack_order.
*/
const enum bpdt_entry_type bpdt_header_order[MAX_SUBPARTS] = {
/* Order of the following entries is mandatory */
CSE_IDLM_TYPE,
IFP_OVERRIDE_TYPE,
S_BPDT_TYPE,
CSE_RBE_TYPE,
UFS_PHY_TYPE,
UFS_GPP_TYPE,
/* Order of the following entries is recommended */
UEP_TYPE,
NVM_CONFIG_TYPE,
UFS_RATE_B_TYPE,
IBB_TYPE,
SMIP_TYPE,
PMC_TYPE,
CSE_BUP_TYPE,
UCODE_TYPE,
DEBUG_TOKENS_TYPE,
IUNIT_TYPE,
CSE_MAIN_TYPE,
ISH_TYPE,
OBB_TYPE,
};
const enum bpdt_entry_type bpdt_pack_order[MAX_SUBPARTS] = {
/* Order of the following entries is mandatory */
UFS_GPP_TYPE,
UFS_PHY_TYPE,
IFP_OVERRIDE_TYPE,
UEP_TYPE,
NVM_CONFIG_TYPE,
UFS_RATE_B_TYPE,
/* Order of the following entries is recommended */
IBB_TYPE,
SMIP_TYPE,
CSE_RBE_TYPE,
PMC_TYPE,
CSE_BUP_TYPE,
UCODE_TYPE,
CSE_IDLM_TYPE,
DEBUG_TOKENS_TYPE,
S_BPDT_TYPE,
IUNIT_TYPE,
CSE_MAIN_TYPE,
ISH_TYPE,
OBB_TYPE,
};
/* Utility functions */
enum ifwi_ret {
COMMAND_ERR = -1,
NO_ACTION_REQUIRED = 0,
REPACK_REQUIRED = 1,
};
struct dir_ops {
enum ifwi_ret (*dir_add)(int type);
};
static enum ifwi_ret ibbp_dir_add(int type);
const struct subpart_info {
const char *name;
const char *readable_name;
uint32_t attr;
struct dir_ops dir_ops;
} subparts[MAX_SUBPARTS] = {
/* OEM SMIP */
[SMIP_TYPE] = {"SMIP", "SMIP", CONTAINS_DIR, {NULL} },
/* CSE RBE */
[CSE_RBE_TYPE] = {"RBEP", "CSE_RBE", CONTAINS_DIR |
MANDATORY_BPDT_ENTRY, {NULL} },
/* CSE BUP */
[CSE_BUP_TYPE] = {"FTPR", "CSE_BUP", CONTAINS_DIR |
MANDATORY_BPDT_ENTRY, {NULL} },
/* uCode */
[UCODE_TYPE] = {"UCOD", "Microcode", CONTAINS_DIR, {NULL} },
/* IBB */
[IBB_TYPE] = {"IBBP", "Bootblock", CONTAINS_DIR, {ibbp_dir_add} },
/* S-BPDT */
[S_BPDT_TYPE] = {"S_BPDT", "S-BPDT", AUTO_GENERATED |
MANDATORY_BPDT_ENTRY, {NULL} },
/* OBB */
[OBB_TYPE] = {"OBBP", "OEM boot block", CONTAINS_DIR |
NON_CRITICAL_SUBPART, {NULL} },
/* CSE Main */
[CSE_MAIN_TYPE] = {"NFTP", "CSE_MAIN", CONTAINS_DIR |
NON_CRITICAL_SUBPART, {NULL} },
/* ISH */
[ISH_TYPE] = {"ISHP", "ISH", NON_CRITICAL_SUBPART, {NULL} },
/* CSE IDLM */
[CSE_IDLM_TYPE] = {"DLMP", "CSE_IDLM", CONTAINS_DIR |
MANDATORY_BPDT_ENTRY, {NULL} },
/* IFP Override */
[IFP_OVERRIDE_TYPE] = {"IFP_OVERRIDE", "IFP_OVERRIDE",
LIES_WITHIN_BPDT_4K | MANDATORY_BPDT_ENTRY,
{NULL} },
/* Debug Tokens */
[DEBUG_TOKENS_TYPE] = {"DEBUG_TOKENS", "Debug Tokens", 0, {NULL} },
/* UFS Phy Configuration */
[UFS_PHY_TYPE] = {"UFS_PHY", "UFS Phy", LIES_WITHIN_BPDT_4K |
MANDATORY_BPDT_ENTRY, {NULL} },
/* UFS GPP LUN ID */
[UFS_GPP_TYPE] = {"UFS_GPP", "UFS GPP", LIES_WITHIN_BPDT_4K |
MANDATORY_BPDT_ENTRY, {NULL} },
/* PMC */
[PMC_TYPE] = {"PMCP", "PMC firmware", CONTAINS_DIR, {NULL} },
/* IUNIT */
[IUNIT_TYPE] = {"IUNP", "IUNIT", NON_CRITICAL_SUBPART, {NULL} },
/* NVM Config */
[NVM_CONFIG_TYPE] = {"NVM_CONFIG", "NVM Config", 0, {NULL} },
/* UEP */
[UEP_TYPE] = {"UEP", "UEP", LIES_WITHIN_BPDT_4K | MANDATORY_BPDT_ENTRY,
{NULL} },
/* UFS Rate B Config */
[UFS_RATE_B_TYPE] = {"UFS_RATE_B", "UFS Rate B Config", 0, {NULL} },
};
struct ifwi_image {
/* Data read from input file */
struct buffer input_buff;
/* BPDT header and entries */
struct buffer bpdt;
size_t input_ifwi_start_offset;
size_t input_ifwi_end_offset;
/* Subpartition content */
struct buffer subpart_buf[MAX_SUBPARTS];
} ifwi_image;
/* Buffer and file I/O */
static off_t get_file_size(FILE *f)
{
off_t fsize;
fseek(f, 0, SEEK_END);
fsize = ftell(f);
fseek(f, 0, SEEK_SET);
return fsize;
}
static inline void *buffer_get(const struct buffer *b)
{
return b->data;
}
static inline size_t buffer_size(const struct buffer *b)
{
return b->size;
}
static inline size_t buffer_offset(const struct buffer *b)
{
return b->offset;
}
/*
* Shrink a buffer toward the beginning of its previous space.
* Afterward, buffer_delete() remains the means of cleaning it up
*/
static inline void buffer_set_size(struct buffer *b, size_t size)
{
b->size = size;
}
/* Splice a buffer into another buffer. Note that it's up to the caller to
* bounds check the offset and size. The resulting buffer is backed by the same
* storage as the original, so although it is valid to buffer_delete() either
* one of them, doing so releases both simultaneously
*/
static void buffer_splice(struct buffer *dest, const struct buffer *src,
size_t offset, size_t size)
{
dest->name = src->name;
dest->data = src->data + offset;
dest->offset = src->offset + offset;
dest->size = size;
}
/*
* Shrink a buffer toward the end of its previous space.
* Afterward, buffer_delete() remains the means of cleaning it up
*/
static inline void buffer_seek(struct buffer *b, size_t size)
{
b->offset += size;
b->size -= size;
b->data += size;
}
/* Returns the start of the underlying buffer, with the offset undone */
static inline void *buffer_get_original_backing(const struct buffer *b)
{
if (!b)
return NULL;
return buffer_get(b) - buffer_offset(b);
}
int buffer_create(struct buffer *buffer, size_t size, const char *name)
{
buffer->name = strdup(name);
buffer->offset = 0;
buffer->size = size;
buffer->data = (char *)malloc(buffer->size);
if (!buffer->data) {
fprintf(stderr, "%s: Insufficient memory (0x%zx).\n", __func__,
size);
}
return !buffer->data;
}
int buffer_write_file(struct buffer *buffer, const char *filename)
{
FILE *fp = fopen(filename, "wb");
if (!fp) {
perror(filename);
return -1;
}
assert(buffer && buffer->data);
if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) {
fprintf(stderr, "incomplete write: %s\n", filename);
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
void buffer_delete(struct buffer *buffer)
{
assert(buffer);
if (buffer->name) {
free(buffer->name);
buffer->name = NULL;
}
if (buffer->data) {
free(buffer_get_original_backing(buffer));
buffer->data = NULL;
}
buffer->offset = 0;
buffer->size = 0;
}
int buffer_from_file(struct buffer *buffer, const char *filename)
{
FILE *fp = fopen(filename, "rb");
if (!fp) {
perror(filename);
return -1;
}
buffer->offset = 0;
off_t file_size = get_file_size(fp);
if (file_size < 0) {
fprintf(stderr, "could not determine size of %s\n", filename);
fclose(fp);
return -1;
}
buffer->size = file_size;
buffer->name = strdup(filename);
buffer->data = (char *)malloc(buffer->size);
assert(buffer->data);
if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) {
fprintf(stderr, "incomplete read: %s\n", filename);
fclose(fp);
buffer_delete(buffer);
return -1;
}
fclose(fp);
return 0;
}
static void alloc_buffer(struct buffer *b, size_t s, const char *n)
{
if (buffer_create(b, s, n) == 0)
return;
ERROR("Buffer allocation failure for %s (size = %zx).\n", n, s);
exit(-1);
}
/* Little-Endian functions */
static inline uint8_t read_ble8(const void *src)
{
const uint8_t *s = src;
return *s;
}
static inline uint8_t read_at_ble8(const void *src, size_t offset)
{
const uint8_t *s = src;
s += offset;
return read_ble8(s);
}
static inline void write_ble8(void *dest, uint8_t val)
{
*(uint8_t *)dest = val;
}
static inline void write_at_ble8(void *dest, uint8_t val, size_t offset)
{
uint8_t *d = dest;
d += offset;
write_ble8(d, val);
}
static inline uint8_t read_at_le8(const void *src, size_t offset)
{
return read_at_ble8(src, offset);
}
static inline void write_le8(void *dest, uint8_t val)
{
write_ble8(dest, val);
}
static inline void write_at_le8(void *dest, uint8_t val, size_t offset)
{
write_at_ble8(dest, val, offset);
}
static inline uint16_t read_le16(const void *src)
{
const uint8_t *s = src;
return (((uint16_t)s[1]) << 8) | (((uint16_t)s[0]) << 0);
}
static inline uint16_t read_at_le16(const void *src, size_t offset)
{
const uint8_t *s = src;
s += offset;
return read_le16(s);
}
static inline void write_le16(void *dest, uint16_t val)
{
write_le8(dest, val >> 0);
write_at_le8(dest, val >> 8, sizeof(uint8_t));
}
static inline void write_at_le16(void *dest, uint16_t val, size_t offset)
{
uint8_t *d = dest;
d += offset;
write_le16(d, val);
}
static inline uint32_t read_le32(const void *src)
{
const uint8_t *s = src;
return (((uint32_t)s[3]) << 24) | (((uint32_t)s[2]) << 16) |
(((uint32_t)s[1]) << 8) | (((uint32_t)s[0]) << 0);
}
static inline uint32_t read_at_le32(const void *src, size_t offset)
{
const uint8_t *s = src;
s += offset;
return read_le32(s);
}
static inline void write_le32(void *dest, uint32_t val)
{
write_le16(dest, val >> 0);
write_at_le16(dest, val >> 16, sizeof(uint16_t));
}
static inline void write_at_le32(void *dest, uint32_t val, size_t offset)
{
uint8_t *d = dest;
d += offset;
write_le32(d, val);
}
static inline uint64_t read_le64(const void *src)
{
uint64_t val;
val = read_at_le32(src, sizeof(uint32_t));
val <<= 32;
val |= read_le32(src);
return val;
}
static inline uint64_t read_at_le64(const void *src, size_t offset)
{
const uint8_t *s = src;
s += offset;
return read_le64(s);
}
static inline void write_le64(void *dest, uint64_t val)
{
write_le32(dest, val >> 0);
write_at_le32(dest, val >> 32, sizeof(uint32_t));
}
static inline void write_at_le64(void *dest, uint64_t val, size_t offset)
{
uint8_t *d = dest;
d += offset;
write_le64(d, val);
}
/*
* Read header/entry members in little-endian format.
* Returns the offset upto which the read was performed.
*/
static size_t read_member(void *src, size_t offset, size_t size_bytes,
void *dst)
{
switch (size_bytes) {
case 1:
*(uint8_t *)dst = read_at_le8(src, offset);
break;
case 2:
*(uint16_t *)dst = read_at_le16(src, offset);
break;
case 4:
*(uint32_t *)dst = read_at_le32(src, offset);
break;
case 8:
*(uint64_t *)dst = read_at_le64(src, offset);
break;
default:
ERROR("Read size not supported %zd\n", size_bytes);
exit(-1);
}
return (offset + size_bytes);
}
/*
* Convert to little endian format.
* Returns the offset upto which the fixup was performed.
*/
static size_t fix_member(void *data, size_t offset, size_t size_bytes)
{
uint8_t *src = (uint8_t *)data + offset;
switch (size_bytes) {
case 1:
write_at_le8(data, *(uint8_t *)src, offset);
break;
case 2:
write_at_le16(data, *(uint16_t *)src, offset);
break;
case 4:
write_at_le32(data, *(uint32_t *)src, offset);
break;
case 8:
write_at_le64(data, *(uint64_t *)src, offset);
break;
default:
ERROR("Write size not supported %zd\n", size_bytes);
exit(-1);
}
return (offset + size_bytes);
}
static void print_subpart_dir(struct subpart_dir *s)
{
if (verbose == 0)
return;
size_t i;
printf("%-25s 0x%-23.8x\n", "Marker", s->h.marker);
printf("%-25s %-25d\n", "Num entries", s->h.num_entries);
printf("%-25s %-25d\n", "Header Version", s->h.header_version);
printf("%-25s %-25d\n", "Entry Version", s->h.entry_version);
printf("%-25s 0x%-23x\n", "Header Length", s->h.header_length);
printf("%-25s 0x%-23x\n", "Checksum", s->h.checksum);
printf("%-25s ", "Name");
for (i = 0; i < sizeof(s->h.name); i++)
printf("%c", s->h.name[i]);
printf("\n");
printf("%-25s%-25s%-25s%-25s%-25s\n", "Entry #", "Name", "Offset",
"Length", "Rsvd");
printf("=========================================================================================================================\n");
for (i = 0; i < s->h.num_entries; i++) {
printf("%-25zd%-25.12s0x%-23x0x%-23x0x%-23x\n", i + 1,
s->e[i].name, s->e[i].offset, s->e[i].length,
s->e[i].rsvd);
}
printf("=========================================================================================================================\n");
}
static void bpdt_print_header(struct bpdt_header *h, const char *name)
{
if (verbose == 0)
return;
printf("%-25s %-25s\n", "Header", name);
printf("%-25s 0x%-23.8x\n", "Signature", h->signature);
printf("%-25s %-25d\n", "Descriptor count", h->descriptor_count);
printf("%-25s %-25d\n", "BPDT Version", h->bpdt_version);
printf("%-25s 0x%-23x\n", "XOR checksum", h->xor_redundant_block);
printf("%-25s 0x%-23x\n", "IFWI Version", h->ifwi_version);
printf("%-25s 0x%-23llx\n", "FIT Tool Version",
(long long)h->fit_tool_version);
}
static void bpdt_print_entries(struct bpdt_entry *e, size_t count,
const char *name)
{
size_t i;
if (verbose == 0)
return;
printf("%s entries\n", name);
printf("%-25s%-25s%-25s%-25s%-25s%-25s%-25s%-25s\n", "Entry #",
"Sub-Partition", "Name", "Type", "Flags", "Offset", "Size",
"File Offset");
printf("=========================================================================================================================================================================================================\n");
for (i = 0; i < count; i++) {
printf("%-25zd%-25s%-25s%-25d0x%-23.08x0x%-23x0x%-23x0x%-23zx\n",
i + 1, subparts[e[i].type].name,
subparts[e[i].type].readable_name, e[i].type, e[i].flags,
e[i].offset, e[i].size,
e[i].offset + ifwi_image.input_ifwi_start_offset);
}
printf("=========================================================================================================================================================================================================\n");
}
static void bpdt_validate_header(struct bpdt_header *h, const char *name)
{
assert(h->signature == BPDT_SIGNATURE);
if (h->bpdt_version != 1) {
ERROR("Invalid header : %s\n", name);
exit(-1);
}
DEBUG("Validated header : %s\n", name);
}
static void bpdt_read_header(void *data, struct bpdt_header *h,
const char *name)
{
size_t offset = 0;
offset = read_member(data, offset, sizeof(h->signature), &h->signature);
offset = read_member(data, offset, sizeof(h->descriptor_count),
&h->descriptor_count);
offset = read_member(data, offset, sizeof(h->bpdt_version),
&h->bpdt_version);
offset = read_member(data, offset, sizeof(h->xor_redundant_block),
&h->xor_redundant_block);
offset = read_member(data, offset, sizeof(h->ifwi_version),
&h->ifwi_version);
read_member(data, offset, sizeof(h->fit_tool_version),
&h->fit_tool_version);
bpdt_validate_header(h, name);
bpdt_print_header(h, name);
}
static void bpdt_read_entries(void *data, struct bpdt *bpdt, const char *name)
{
size_t i, offset = 0;
struct bpdt_entry *e = &bpdt->e[0];
size_t count = bpdt->h.descriptor_count;
for (i = 0; i < count; i++) {
offset = read_member(data, offset, sizeof(e[i].type),
&e[i].type);
offset = read_member(data, offset, sizeof(e[i].flags),
&e[i].flags);
offset = read_member(data, offset, sizeof(e[i].offset),
&e[i].offset);
offset = read_member(data, offset, sizeof(e[i].size),
&e[i].size);
}
bpdt_print_entries(e, count, name);
}
/*
* Given type of sub-partition, identify BPDT entry for it.
* Sub-Partition could lie either within BPDT or S-BPDT.
*/
static struct bpdt_entry *__find_entry_by_type(struct bpdt_entry *e,
size_t count, int type)
{
size_t i;
for (i = 0; i < count; i++) {
if (e[i].type == type)
break;
}
if (i == count)
return NULL;
return &e[i];
}
static struct bpdt_entry *find_entry_by_type(int type)
{
struct bpdt *b = buffer_get(&ifwi_image.bpdt);
if (!b)
return NULL;
struct bpdt_entry *curr = __find_entry_by_type(&b->e[0],
b->h.descriptor_count,
type);
if (curr)
return curr;
b = buffer_get(&ifwi_image.subpart_buf[S_BPDT_TYPE]);
if (!b)
return NULL;
return __find_entry_by_type(&b->e[0], b->h.descriptor_count, type);
}
/*
* Find sub-partition type given its name. If the name does not exist, returns
* -1.
*/
static int find_type_by_name(const char *name)
{
int i;
for (i = 0; i < MAX_SUBPARTS; i++) {
if ((strlen(subparts[i].name) == strlen(name)) &&
(!strcmp(subparts[i].name, name)))
break;
}
if (i == MAX_SUBPARTS) {
ERROR("Invalid sub-partition name %s.\n", name);
return -1;
}
return i;
}
/*
* Read the content of a sub-partition from input file and store it in
* ifwi_image.subpart_buf[SUB-PARTITION_TYPE].
*
* Returns the maximum offset occupied by the sub-partitions.
*/
static size_t read_subpart_buf(void *data, size_t size, struct bpdt_entry *e,
size_t count)
{
size_t i, type;
struct buffer *buf;
size_t max_offset = 0;
for (i = 0; i < count; i++) {
type = e[i].type;
if (type >= MAX_SUBPARTS) {
ERROR("Invalid sub-partition type %zd.\n", type);
exit(-1);
}
if (buffer_size(&ifwi_image.subpart_buf[type])) {
ERROR("Multiple sub-partitions of type %zd(%s).\n",
type, subparts[type].name);
exit(-1);
}
if (e[i].size == 0) {
INFO("Dummy sub-partition %zd(%s). Skipping.\n", type,
subparts[type].name);
continue;
}
assert((e[i].offset + e[i].size) <= size);
/*
* Sub-partitions in IFWI image are not in the same order as
* in BPDT entries. BPDT entires are in header_order whereas
* sub-partition offsets in the image are in pack_order.
*/
if ((e[i].offset + e[i].size) > max_offset)
max_offset = e[i].offset + e[i].size;
/*
* S-BPDT sub-partition contains information about all the
* non-critical sub-partitions. Thus, size of S-BPDT
* sub-partition equals size of S-BPDT plus size of all the
* non-critical sub-partitions. Thus, reading whole of S-BPDT
* here would be redundant as the non-critical partitions are
* read and allocated buffers separately. Also, S-BPDT requires
* special handling for reading header and entries.
*/
if (type == S_BPDT_TYPE)
continue;
buf = &ifwi_image.subpart_buf[type];
alloc_buffer(buf, e[i].size, subparts[type].name);
memcpy(buffer_get(buf), (uint8_t *)data + e[i].offset,
e[i].size);
}
assert(max_offset);
return max_offset;