-
Notifications
You must be signed in to change notification settings - Fork 28
/
fip.c
1773 lines (1584 loc) · 35.6 KB
/
fip.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <endian.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <sys/stat.h>
#include "gxlimg.h"
#include "fip.h"
#include "amlcblk.h"
#include "ssl.h"
#define FOUT_MODE_DFT (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
#define BL31_ENTRY_MAGIC (0x87654321)
#define BL31_MAGIC (0x12348765)
#define AMLSBLK_KEY_MAGIC (*(uint32_t *)"@KEY")
#define BL2SZ (0xc000)
#define TOC_OFFSET_V3 (0x10)
#define SHA2_SZ (0x20)
/**
* FIP Format Revision
*/
enum fip_rev {
GI_FIP_V2, /* GXL, GXM */
GI_FIP_V3, /* G12A, G12B, SM1 */
};
/**
* Read a block of data from a file
*
* @param fd: File descriptor to read a block from
* @param blk: Filled with read data
* @param sz: Size of block to read from file
* @return: Negative number on error, read size otherwise. The only reason that
* return value could be different from sz on success is when EOF has been
* encountered while reading the file.
*/
static ssize_t gi_fip_read_blk(int fd, uint8_t *blk, size_t sz)
{
size_t i;
ssize_t nr = 1;
for(i = 0; (i < sz) && (nr != 0); i += nr) {
nr = read(fd, blk + i, sz - i);
if(nr < 0)
goto out;
}
nr = i;
out:
return nr;
}
/**
* Write a block of data into a file
*
* @param fd: File descriptor to write a block into
* @param blk: Actual block data
* @param sz: Size of block to write into file
* @return: Negative number on error, sz otherwise.
*/
static ssize_t gi_fip_write_blk(int fd, uint8_t *blk, size_t sz)
{
size_t i;
ssize_t nr;
for(i = 0; i < sz; i += nr) {
nr = write(fd, blk + i, sz - i);
if(nr < 0)
goto out;
}
nr = i;
out:
return nr;
}
/**
* Safely create a temporary file
*
* @param path: Path of temporary file, should ends with 6 X. On success those
* X will be replaced with the unique file suffix created.
* @return: fd on success, negative number otherwise
*/
static int gi_fip_create_tmp(char *path)
{
mode_t oldmask;
int fd;
oldmask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
fd = mkstemp(path);
(void)umask(oldmask);
return fd;
}
/**
* List of supported boot image
*/
enum FIP_BOOT_IMG {
FBI_BL2,
FBI_BL30,
FBI_BL301,
FBI_BL31,
FBI_BL32,
FBI_BL33,
FBI_BL2_DATA,
FBI_BL30_DATA,
FBI_BL31_DATA,
FBI_BL32_DATA,
FBI_BL33_DATA,
FBI_EMPTY,
FBI_UNKNOWN,
};
typedef uint8_t uuid_t[16];
/**
* Default uuid for each boot image
*/
static uuid_t const uuid_list[] = {
[FBI_BL2] = {
0x5f, 0xf9, 0xec, 0x0b, 0x4d, 0x22, 0x3e, 0x4d,
0xa5, 0x44, 0xc3, 0x9d, 0x81, 0xc7, 0x3f, 0x0a
},
[FBI_BL30] = {
0x97, 0x66, 0xfd, 0x3d, 0x89, 0xbe, 0xe8, 0x49,
0xae, 0x5d, 0x78, 0xa1, 0x40, 0x60, 0x82, 0x13
},
[FBI_BL301] = {
0xdd, 0xcc, 0xbb, 0xaa, 0xcd, 0xab, 0xef, 0xef,
0xab, 0xcd, 0x12, 0x34, 0x56, 0x78, 0xab, 0xcd
},
[FBI_BL31] = {
0x47, 0xd4, 0x08, 0x6d, 0x4c, 0xfe, 0x98, 0x46,
0x9b, 0x95, 0x29, 0x50, 0xcb, 0xbd, 0x5a, 0x00
},
[FBI_BL32] = {
0x05, 0xd0, 0xe1, 0x89, 0x53, 0xdc, 0x13, 0x47,
0x8d, 0x2b, 0x50, 0x0a, 0x4b, 0x7a, 0x3e, 0x38
},
[FBI_BL33] = {
0xd6, 0xd0, 0xee, 0xa7, 0xfc, 0xea, 0xd5, 0x4b,
0x97, 0x82, 0x99, 0x34, 0xf2, 0x34, 0xb6, 0xe4
},
[FBI_BL2_DATA] = {
0xf4, 0x1d, 0x14, 0x86, 0xcb, 0x95, 0xe6, 0x11,
0x84, 0x88, 0x84, 0x2b, 0x2b, 0x01, 0xca, 0x38
},
[FBI_BL30_DATA] = {
0x48, 0x56, 0xcc, 0xc2, 0xcc, 0x85, 0xe6, 0x11,
0xa5, 0x36, 0x3c, 0x97, 0x0e, 0x97, 0xa0, 0xee
},
[FBI_BL31_DATA] = {
0xca, 0xaf, 0xb0, 0x33, 0xce, 0x85, 0xe6, 0x11,
0x8c, 0x32, 0x00, 0x22, 0x19, 0xc7, 0x77, 0x2f
},
[FBI_BL32_DATA] = {
0x34, 0xa1, 0x48, 0xb8, 0xbc, 0x90, 0xe6, 0x11,
0x8f, 0xef, 0xa4, 0xba, 0xdb, 0x19, 0xde, 0x03
},
[FBI_BL33_DATA] = {
0x8e, 0x59, 0xd6 ,0x5d, 0x5e, 0x8b, 0xe6, 0x11,
0xbc, 0xb5, 0xf0, 0xde, 0xf1, 0x83, 0x72, 0x96,
},
[FBI_EMPTY] = {},
};
/**
* Get FIP image type from its uuid
*
* @param uuid: UUID to find image type from
* @return: FIP image type if found FIP_UNKNOWN otherwise
*/
static enum FIP_BOOT_IMG uuid_get_type(uuid_t uuid)
{
size_t i;
for(i = 0; i < ARRAY_SIZE(uuid_list); ++i)
if(memcmp(uuid_list[i], uuid, sizeof(uuid_list[i])) == 0)
goto out;
i = FBI_UNKNOWN;
out:
return (enum FIP_BOOT_IMG)i;
}
#define FT_DATA_SIZE (0x468)
/**
* Image fixed data for V3, usage & meaning is unknown
* We find some similar data in bl32.img header
* Probably used for secure boot
*/
static uint8_t const uuid_data[][FT_DATA_SIZE] = {
[FBI_BL2_DATA] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00
/* Rest is 0x0 */
},
[FBI_BL30_DATA] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x05, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x05, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x20, 0x00,
/* Rest is 0x0 */
},
[FBI_BL31_DATA] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x30, 0x05, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x30, 0x05, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x30, 0x05, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x40, 0x01,
/* Rest is 0x0 */
},
[FBI_BL32_DATA] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
/* Rest is 0x0 */
},
[FBI_BL33_DATA] = {},
};
#define FT_DATA_START (0x188)
/**
* FIP header structure
*/
#pragma pack(push, 1)
struct fip_toc_header {
/**
* FIP magic
*/
uint32_t name;
/**
* Vendor specific number
*/
uint32_t serial_number;
/**
* Flags, reserved for later use
*/
uint64_t flags;
};
#pragma pack(pop)
#define FT_NAME 0xaa640001
#define FT_SERIAL 0x12345678
#define FIP_TOC_HEADER \
&((struct fip_toc_header){ \
.name = htole32(FT_NAME), \
.serial_number = htole32(FT_SERIAL) \
})
/**
* Header for a FIP table of content entry
*/
#pragma pack(push, 1)
struct fip_toc_entry {
/**
* uuid of the image entry
*/
uuid_t uuid;
/**
* Offset of the image from the FIP base address
*/
uint64_t offset;
/**
* Size of the FIP entry image
*/
uint64_t size;
/**
* Flags for the FIP entry image
*/
uint64_t flags;
};
#pragma pack(pop)
#define FTE_BL31HDR_SZ 0x50
#define FTE_BL31HDR_OFF(nr) (0x430 + FTE_BL31HDR_SZ * (nr))
/* Get fip entry offset from fip base */
#define FTE_OFF(nr) \
(sizeof(struct fip_toc_header) + nr * sizeof(struct fip_toc_entry))
#define FIP_SZ 0x4000
#define FIP_TEMPPATH "/tmp/fip.bin.XXXXXX"
#define FIP_TEMPPATHSZ sizeof(FIP_TEMPPATH)
/**
* FIP handler
*/
struct fip {
/**
* Current image copied data size
*/
size_t cursz;
/**
* Number of entry in FIP table of content
*/
size_t nrentries;
/**
* Temporary Fip file descriptor
*/
int fd;
/**
* Temporary file path
*/
char path[FIP_TEMPPATHSZ];
};
/**
* Init a FIP handler
*
* @param fip: FIP handler to init
* @return: 0 on success, negative number otherwise
*/
static inline int fip_init(struct fip *fip, enum fip_rev rev)
{
unsigned long long init = 0xffffffffffffffffULL;
ssize_t nr;
size_t i;
off_t off;
int ret;
strncpy(fip->path, FIP_TEMPPATH, sizeof(fip->path));
fip->cursz = FIP_SZ;
fip->nrentries = 0;
fip->fd = gi_fip_create_tmp(fip->path);
if(fip->fd < 0) {
PERR("Cannot create fip temp: ");
ret = -errno;
goto out;
}
switch (rev) {
case GI_FIP_V2:
ret = ftruncate(fip->fd, FIP_SZ - 0x200);
if(ret < 0) {
PERR("Cannot truncate fip toc header: ");
close(fip->fd);
ret = -errno;
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)FIP_TOC_HEADER,
sizeof(*FIP_TOC_HEADER));
if(nr < 0) {
PERR("Cannot write fip toc header: ");
close(fip->fd);
ret = -errno;
goto out;
}
/* End of toc entry */
off = lseek(fip->fd, 0xc00, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
for(i = 0; i < 0x80 / sizeof(init); ++i) {
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&init,
sizeof(init));
if(nr < 0) {
PERR("Cannot write fip toc last entry: ");
close(fip->fd);
ret = -errno;
goto out;
}
}
break;
case GI_FIP_V3:
ret = ftruncate(fip->fd, FIP_SZ - SHA2_SZ);
if(ret < 0) {
PERR("Cannot truncate fip toc header: ");
close(fip->fd);
ret = -errno;
goto out;
}
off = lseek(fip->fd, TOC_OFFSET_V3, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)FIP_TOC_HEADER,
sizeof(*FIP_TOC_HEADER));
if(nr < 0) {
PERR("Cannot write fip toc header: ");
close(fip->fd);
ret = -errno;
goto out;
}
break;
}
ret = 0;
out:
return ret;
}
/**
* Cleanup a FIP handler
*
* @param fip: FIP handler to clean
*/
static inline void fip_cleanup(struct fip *fip)
{
close(fip->fd);
(void)remove(fip->path);
}
/**
* Binary file info found in FIP ToC entry
*/
struct fip_entry_info {
enum FIP_BOOT_IMG type;
size_t offset;
size_t size;
};
/**
* List of binaries file info found in FIP ToC
*/
#define MAX_FIP_FILE 10
struct fip_toc_info {
size_t nr_files;
struct fip_entry_info files[MAX_FIP_FILE];
};
/**
* Read a fip header from image
*
* @param fip: fill up with FIP informations
* @param fd: FIP image to read header from
* @param rev: FIP Format revision
* @param bl2sz: BL2 Size
* @return: 0 on success, negative number otherwise
*/
static int fip_read_toc(struct fip_toc_info *toc, int fd, enum fip_rev rev,
size_t bl2sz)
{
struct fip_toc_header tochdr;
struct fip_toc_entry entry;
ssize_t nr;
size_t i;
off_t off;
enum FIP_BOOT_IMG type;
int ret;
off = lseek(fd, rev == GI_FIP_V3 ? bl2sz + TOC_OFFSET_V3 : 0,
SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
/* Verify FIP TOC header first */
ret = gi_fip_read_blk(fd, (uint8_t *)&tochdr, sizeof(tochdr));
if(ret < 0) {
PERR("Cannot read FIP header\n");
ret = -errno;
goto out;
}
if(memcmp((uint8_t *)&tochdr, (uint8_t *)FIP_TOC_HEADER,
sizeof(tochdr)) != 0) {
ERR("Invalid FIP Header\n");
ret = -EINVAL;
goto out;
}
/* Now read table of content entries */
for(i = 0; i < ARRAY_SIZE(toc->files); ++i) {
nr = gi_fip_read_blk(fd, (uint8_t *)&entry, sizeof(entry));
if(nr <= 0) {
PERR("Cannot read TOC entry\n");
ret = -errno;
goto out;
}
type = uuid_get_type(entry.uuid);
toc->files[i].type = type;
if(type == FBI_EMPTY)
continue;
if(type == FBI_UNKNOWN)
break;
toc->files[i].offset = entry.offset;
toc->files[i].size = entry.size;
}
toc->nr_files = i;
ret = 0;
out:
return ret;
}
/**
* Copy part of a file as-is in another file at specific offset
*
* @param fdin: Src file to copy
* @param fdout: Dest file to copy into
* @param size: Maximum size to copy from fdin
* @return: actual number of bytes copied from fdin on success, negative number
* otherwise.
*/
static ssize_t gi_copy_file(int fdin, int fdout, size_t len)
{
ssize_t nrd, nwr, tot;
uint8_t block[512];
tot = 0;
do {
nrd = gi_fip_read_blk(fdin, block,
MIN(len - tot, sizeof(block)));
if(nrd < 0)
continue;
nwr = gi_fip_write_blk(fdout, block, nrd);
if(nwr < 0) {
PERR("Cannot write to file\n");
tot = -errno;
goto out;
}
tot += nrd;
} while((nrd > 0) && ((size_t)tot < len));
if(nrd < 0) {
PERR("Cannot read file\n");
tot = -errno;
goto out;
}
out:
return tot;
}
/**
* Copy a file as-is in another file at specific offset
*
* @param fdin: Src file to copy
* @param fdout: Dest file to copy into
* @param off: Offset at which src file should be copy into dest file
* @return: 0 on success, negative number otherwise
*/
static int gi_fip_dump_img(int fdin, int fdout, size_t off)
{
ssize_t len;
off_t o;
int ret;
o = lseek(fdout, off, SEEK_SET);
if(o < 0) {
SEEK_ERR(o, ret);
goto out;
}
len = gi_copy_file(fdin, fdout, (size_t)-1);
if(len < 0) {
ret = (int)len;
goto out;
}
ret = 0;
out:
return ret;
}
/**
* Add a bootloder image in boot image
*
* @param fip: Fip handler
* @param fdout: Final boot image file
* @param fdin: Bootloader image to add
* @param type: Type of bootloader image
* @param rev: FIP Format revision
* @param bl2sz: BL2 Image Size
*/
static int gi_fip_add(struct fip *fip, int fdout, int fdin,
enum FIP_BOOT_IMG type, enum fip_rev rev, size_t bl2sz)
{
static uint32_t const bl31magic[] = {
BL31_ENTRY_MAGIC,
0x1,
};
struct fip_toc_entry entry;
size_t sz;
ssize_t nr;
size_t skip = 0;
off_t off;
int ret;
uint8_t buf[FTE_BL31HDR_SZ];
if (fdin >= 0) {
off = lseek(fdin, 0, SEEK_END);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
sz = (size_t)off;
/* Detect a AMLSBLK image a skip header if found on V3 */
if (rev == GI_FIP_V3) {
off = lseek(fdin, 0, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_read_blk(fdin, buf, 4);
if(nr <= 0) {
PERR("Cannot read BL image entry\n");
ret = -errno;
goto out;
}
if (le32toh(*(uint32_t *)buf) == AMLSBLK_KEY_MAGIC) {
skip = 0x490;
sz -= skip;
}
}
} else
sz = 0;
memcpy(entry.uuid, uuid_list[type], sizeof(entry.uuid));
entry.offset = fip->cursz;
entry.flags = 0;
entry.size = sz;
off = FTE_OFF(fip->nrentries);
if (rev == GI_FIP_V3)
off += TOC_OFFSET_V3;
off = lseek(fip->fd, off, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&entry, sizeof(entry));
if(nr < 0) {
PERR("Cannot write FIP entry\n");
ret = -errno;
goto out;
}
/* Skip writting data if file is not available */
if (!sz)
goto nofdin;
off = lseek(fdin, 256, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_read_blk(fdin, buf, sizeof(buf));
if(nr <= 0) {
PERR("Cannot read BL image entry\n");
ret = -errno;
goto out;
}
/*
* BL31 binary store information about load address and entry point in
* the FIP data
*/
if(le32toh(*(uint32_t *)buf) == BL31_MAGIC) {
off = lseek(fip->fd, 1024, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&bl31magic,
sizeof(bl31magic));
if(nr < 0) {
PERR("Cannot write BL31 entry header\n");
ret = -errno;
goto out;
}
off = lseek(fip->fd, FTE_BL31HDR_OFF(fip->nrentries),
SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, buf, sizeof(buf));
if(nr < 0) {
PERR("Cannot write BL31 entry header data\n");
ret = -errno;
goto out;
}
}
off = lseek(fdin, skip, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
gi_fip_dump_img(fdin, fdout, bl2sz + entry.offset);
fip->cursz += ROUNDUP(sz, 0x4000);
nofdin:
++fip->nrentries;
ret = 0;
out:
return ret;
}
/**
* Add a bootloader image data in boot image TOC
*
* @param fip: Fip handler
* @param type: Type of bootloader image data
* @param index: Index of bootloader image data
*/
static int gi_fip_data_add(struct fip *fip, enum FIP_BOOT_IMG type,
unsigned int index)
{
struct fip_toc_entry entry;
ssize_t nr;
off_t off;
int ret;
memcpy(entry.uuid, uuid_list[type], sizeof(entry.uuid));
entry.offset = FT_DATA_START + (index * FT_DATA_SIZE);
entry.flags = 0;
entry.size = FT_DATA_SIZE;
off = lseek(fip->fd, TOC_OFFSET_V3 + FTE_OFF(fip->nrentries),
SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&entry,
sizeof(entry));
if(nr < 0) {
PERR("Cannot write FIP entry\n");
ret = -errno;
goto out;
}
off = lseek(fip->fd, entry.offset, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&uuid_data[type],
FT_DATA_SIZE);
if(nr < 0) {
PERR("Cannot write FIP data entry\n");
ret = -errno;
goto out;
}
++fip->nrentries;
ret = 0;
out:
return ret;
}
/**
* DDRFW header structure
*/
#pragma pack(push, 1)
struct fip_ddrfw_toc_header {
/**
* DDRFW magic
*/
uint32_t magic;
/**
* DDR Firmware count
*/
uint32_t count;
/**
* Flags, reserved for later use
*/
uint64_t flags;
};
#pragma pack(pop)
#define DDRFW_MAGIC (0x4d464440)
#define FIP_DDRFW_TOC_HEADER(ddrfw_count) \
{ \
.magic = htole32(DDRFW_MAGIC), \
.count = htole32(ddrfw_count), \
.flags = 0 \
}
#define DDRFW_OFF (0x1790)
/**
* Initialize the DDR firmware TOC
*
* @param fip: Fip handler
* @param ddrfw_count: Number of DDR firmwares
*/
static int gi_fip_ddrfw_init(struct fip *fip, unsigned int ddrfw_count)
{
struct fip_ddrfw_toc_header toc = FIP_DDRFW_TOC_HEADER(ddrfw_count);
ssize_t nr;
off_t off;
int ret = 0;
off = lseek(fip->fd, DDRFW_OFF, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&toc, sizeof(toc));
if(nr < 0) {
PERR("Cannot write fip toc header: ");
close(fip->fd);
ret = -errno;
goto out;
}
out:
return ret;
}
/**
* DDRFW entry structure
*/
#pragma pack(push, 1)
struct fip_ddrfw_toc_entry {
/**
* DDRFW magic
*/
uint8_t magic[8];
/**
* DDRFW offset
*/
uint32_t offset;
/**
* DDRFW size
*/
uint32_t size;
/**
* DDRFW properties
*/
uint8_t props[16];
/**
* DDRFW hash
*/
uint8_t hash[SHA2_SZ];
};
#pragma pack(pop)
#define FTE_DDRFW_OFF(i) \
(DDRFW_OFF + sizeof(struct fip_ddrfw_toc_header) + \
(i * sizeof(struct fip_ddrfw_toc_entry)))
/**
* Add a DDR Firmware data in boot image TOC
*
* @param fip: Fip handler
* @param fdout: Final boot image file
* @param fdin: Bootloader image to add
* @param index: Index of DDR Firmware
* @param bl2sz: Size of BL2 image
*/
static int gi_fip_ddrfw_add(struct fip *fip, int fdout, int fdin,
size_t index, size_t bl2sz)
{
struct fip_ddrfw_toc_entry entry;
uint8_t tmp[1024];
EVP_MD_CTX *ctx;
size_t sz;
ssize_t nr;
off_t off;
size_t i;
int ret;
ctx = EVP_MD_CTX_new();
if(ctx == NULL) {
SSLERR(ret, "Cannot create digest context: ");
goto out;
}
ret = EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
if(ret != 1) {
SSLERR(ret, "Cannot init digest context: ");
goto out;
}
off = lseek(fdin, 0, SEEK_END);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
sz = (size_t)off;
entry.offset = fip->cursz;
/* Align size on 16k */
entry.size = ((sz - 0x60) + 0x3fff) & 0xffffc000;
off = lseek(fdin, SHA2_SZ, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_read_blk(fdin, (uint8_t *)&entry.magic, sizeof(entry.magic));
if(nr <= 0) {
PERR("Cannot read DDRFW magic\n");
ret = -errno;
goto out;
}
off = lseek(fdin, 0x30, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_read_blk(fdin, (uint8_t *)&entry.props, sizeof(entry.props));
if(nr <= 0) {
PERR("Cannot read DDRFW properties\n");
ret = -errno;
goto out;
}
off = lseek(fdin, 0x60, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
for(i = 0; i < (sz - 0x60); i += nr) {
nr = gi_fip_read_blk(fdin, tmp, sizeof(tmp));
if(nr < 0) {
PERR("Cannot read fd %d:", fdin);
ret = (int)nr;
goto out;
}
ret = EVP_DigestUpdate(ctx, tmp, nr);
if(ret != 1) {
SSLERR(ret, "Cannot hash data block: ");
goto out;
}
}
/* Add potential padding to digest aswell */
while (i < entry.size) {
memset(tmp, 0, sizeof(tmp));
nr = MIN(entry.size - i, sizeof(tmp));
ret = EVP_DigestUpdate(ctx, tmp, nr);
if(ret != 1) {
SSLERR(ret, "Cannot hash data block: ");
goto out;
}
i += nr;
}
ret = EVP_DigestFinal_ex(ctx, (uint8_t *)&entry.hash, NULL);
if(ret != 1) {
SSLERR(ret, "Cannot finalize hash: ");
goto out;
}
off = lseek(fip->fd, FTE_DDRFW_OFF(index), SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
nr = gi_fip_write_blk(fip->fd, (uint8_t *)&entry, sizeof(entry));
if(nr < 0) {
PERR("Cannot write DDRFW entry\n");
ret = -errno;
goto out;
}
off = lseek(fdin, 0x60, SEEK_SET);
if(off < 0) {
SEEK_ERR(off, ret);
goto out;
}
gi_fip_dump_img(fdin, fdout, bl2sz + entry.offset);
fip->cursz += entry.size;
ret = 0;
out:
return ret;
}
/**
* Calculate final FIP toc hash
*
* @param fip: Fip handler
*/
static int gi_fip_v3_fini(int fipfd)
{
EVP_MD_CTX *ctx;
uint8_t tmp[1024];
uint8_t hash[SHA2_SZ];