hc
2024-08-16 a24a44ff9ca902811b99aa9663d697cf452e08ef
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
/*
 * Rockchip trust image generator
 *
 * (C) Copyright 2008-2015 Fuzhou Rockchip Electronics Co., Ltd
 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
#include <sys/stat.h>
#include <u-boot/sha256.h>
#include "trust_merger.h"
#include "sha2.h"
 
/* #define DEBUG */
 
static bool gDebug =
#ifdef DEBUG
        true;
#else
        false;
#endif /* DEBUG */
 
#define LOGE(fmt, args...) fprintf(stderr, "E: [%s] " fmt, __func__, ##args)
#define LOGD(fmt, args...)                                                     \
  do {                                                                         \
    if (gDebug)                                                                \
      fprintf(stderr, "D: [%s] " fmt, __func__, ##args);                       \
  } while (0)
 
/* trust image has g_trust_max_num backups */
static uint32_t g_trust_max_num = 2;
static uint32_t g_trust_max_size = 2 * 1024 * 1024;
 
/* config sha and rsa */
#define SHA_SEL_256 3    /* little endian */
#define SHA_SEL_256_RK 2 /* big endian: only rk3368 need it */
#define SHA_SEL_160 1
#define SHA_SEL_NONE 0
 
#define RSA_SEL_2048_PSS 3 /* only RK3326, PX30, RK3308 */
#define RSA_SEL_2048 2     /* most platforms except above PSS */
#define RSA_SEL_1024 1
#define RSA_SEL_NONE 0
 
#define is_digit(c) ((c) >= '0' && (c) <= '9')
 
static char *gConfigPath;
static OPT_T gOpts;
#define BL3X_FILESIZE_MAX (512 * 1024)
static uint8_t gBuf[BL3X_FILESIZE_MAX];
static bool gSubfix;
static char *gLegacyPath;
static char *gNewPath;
static char *gPrePath;
static uint8_t gRSAmode = RSA_SEL_2048;
static uint8_t gSHAmode = SHA_SEL_256;
static bool gIgnoreBL32;
 
const uint8_t gBl3xID[BL_MAX_SEC][4] = { { 'B', 'L', '3', '0' },
   { 'B', 'L', '3', '1' },
   { 'B', 'L', '3', '2' },
   { 'B', 'L', '3', '3' }
};
 
static inline uint32_t getBCD(uint16_t value)
{
   uint8_t tmp[2] = { 0 };
   int i;
   uint32_t ret;
 
   if (value > 0xFFFF) {
       return 0;
   }
 
   for (i = 0; i < 2; i++) {
       tmp[i] = (((value / 10) % 10) << 4) | (value % 10);
       value /= 100;
   }
   ret = ((uint16_t)(tmp[1] << 8)) | tmp[0];
 
   LOGD("ret:%x\n", ret);
   return ret & 0xFF;
}
 
static inline void fixPath(char *path)
{
   int i, len = strlen(path);
   char tmp[MAX_LINE_LEN];
   char *start, *end;
 
   for (i = 0; i < len; i++) {
       if (path[i] == '\\')
           path[i] = '/';
       else if (path[i] == '\r' || path[i] == '\n')
           path[i] = '\0';
   }
 
   if (gLegacyPath && gNewPath) {
       start = strstr(path, gLegacyPath);
       if (start) {
           end = start + strlen(gLegacyPath);
           /* Backup, so tmp can be src for strcat() */
           strcpy(tmp, end);
           /* Terminate, so path can be dest for strcat() */
           *start = '\0';
           strcat(path, gNewPath);
           strcat(path, tmp);
       } else {
           strcpy(tmp, path);
           strcpy(path, gNewPath);
           strcat(path, tmp);
       }
   } else if ((ulong)path != (ulong)gOpts.outPath && /* ignore output */
          gPrePath && strncmp(path, gPrePath, strlen(gPrePath))) {
       strcpy(tmp, path);
       strcpy(path, gPrePath);
       strcat(path, tmp);
   }
}
 
static bool parseVersion(FILE *file)
{
   int d = 0;
 
   if (SCANF_EAT(file) != 0) {
       return false;
   }
   if (fscanf(file, OPT_MAJOR "=%d", &d) != 1)
       return false;
   gOpts.major = (uint16_t) d;
   if (SCANF_EAT(file) != 0) {
       return false;
   }
   if (fscanf(file, OPT_MINOR "=%d", &d) != 1)
       return false;
   gOpts.minor = (uint16_t) d;
   LOGD("major:%d, minor:%d\n", gOpts.major, gOpts.minor);
   return true;
}
 
static bool parseBL3x(FILE *file, int bl3x_id)
{
   int pos;
   int sec;
   char buf[MAX_LINE_LEN];
   bl_entry_t *pbl3x = NULL;
 
   if (bl3x_id >= BL_MAX_SEC) {
       return false;
   }
 
   pbl3x = &gOpts.bl3x[bl3x_id];
 
   /* SEC */
   if (SCANF_EAT(file) != 0) {
       return false;
   }
   if (fscanf(file, OPT_SEC "=%d", &sec) != 1) {
       return false;
   }
   if ((gSubfix) && (bl3x_id == BL32_SEC)) {
       if (sec == 0) {
           sec = 1;
           printf("BL3%d adjust sec from 0 to 1\n", bl3x_id);
       }
   } else if (gIgnoreBL32 && (bl3x_id == BL32_SEC)) {
       if (sec == 1) {
           sec = 0;
           printf("BL3%d adjust sec from 1 to 0\n", bl3x_id);
       }
   }
   pbl3x->sec = sec;
   LOGD("bl3%d sec: %d\n", bl3x_id, pbl3x->sec);
 
   /* PATH */
   if (SCANF_EAT(file) != 0) {
       return false;
   }
   memset(buf, 0, MAX_LINE_LEN);
   if (fscanf(file, OPT_PATH "=%s", buf) != 1) {
       if (pbl3x->sec)
           return false;
   } else {
       if (strlen(buf) != 0) {
           fixPath(buf);
           strcpy(pbl3x->path, buf);
           LOGD("bl3%d path:%s\n", bl3x_id, pbl3x->path);
       }
   }
 
   /* ADDR */
   if (SCANF_EAT(file) != 0) {
       return false;
   }
   memset(buf, 0, MAX_LINE_LEN);
   if (fscanf(file, OPT_ADDR "=%s", buf) != 1) {
       if (pbl3x->sec)
           return false;
   } else {
       if (strlen(buf) != 0) {
           pbl3x->addr = strtoul(buf, NULL, 16);
           LOGD("bl3%d addr:0x%x\n", bl3x_id, pbl3x->addr);
       }
   }
 
   pos = ftell(file);
   if (pos < 0) {
       return false;
   }
   if (SCANF_EAT(file) != 0) {
       return false;
   }
 
   return true;
}
 
static bool parseOut(FILE *file)
{
   if (SCANF_EAT(file) != 0) {
       return false;
   }
   if (fscanf(file, OPT_OUT_PATH "=%[^\r^\n]", gOpts.outPath) != 1)
       return false;
   /* fixPath(gOpts.outPath); */
   printf("out:%s\n", gOpts.outPath);
 
   return true;
}
 
void printOpts(FILE *out)
{
   fprintf(out, SEC_BL30 "\n" OPT_SEC "=%d\n", gOpts.bl3x[BL30_SEC].sec);
   if (gOpts.bl3x[BL30_SEC].sec) {
       fprintf(out, OPT_PATH "=%s\n", gOpts.bl3x[BL30_SEC].path);
       fprintf(out, OPT_ADDR "=0x%08x\n", gOpts.bl3x[BL30_SEC].addr);
   }
 
   fprintf(out, SEC_BL31 "\n" OPT_SEC "=%d\n", gOpts.bl3x[BL31_SEC].sec);
   if (gOpts.bl3x[BL31_SEC].sec) {
       fprintf(out, OPT_PATH "=%s\n", gOpts.bl3x[BL31_SEC].path);
       fprintf(out, OPT_ADDR "=0x%08x\n", gOpts.bl3x[BL31_SEC].addr);
   }
 
   fprintf(out, SEC_BL32 "\n" OPT_SEC "=%d\n", gOpts.bl3x[BL32_SEC].sec);
   if (gOpts.bl3x[BL32_SEC].sec) {
       fprintf(out, OPT_PATH "=%s\n", gOpts.bl3x[BL32_SEC].path);
       fprintf(out, OPT_ADDR "=0x%08x\n", gOpts.bl3x[BL32_SEC].addr);
   }
 
   fprintf(out, SEC_BL33 "\n" OPT_SEC "=%d\n", gOpts.bl3x[BL33_SEC].sec);
   if (gOpts.bl3x[BL33_SEC].sec) {
       fprintf(out, OPT_PATH "=%s\n", gOpts.bl3x[BL33_SEC].path);
       fprintf(out, OPT_ADDR "=0x%08x\n", gOpts.bl3x[BL33_SEC].addr);
   }
 
   fprintf(out, SEC_OUT "\n" OPT_OUT_PATH "=%s\n", gOpts.outPath);
}
 
static bool parseOpts(void)
{
   FILE *file = NULL;
   char *configPath = (gConfigPath == NULL) ? DEF_CONFIG_FILE : gConfigPath;
   bool bl30ok = false, bl31ok = false, bl32ok = false, bl33ok = false;
   bool outOk = false;
   bool versionOk = false;
   char buf[MAX_LINE_LEN];
   bool ret = false;
 
   file = fopen(configPath, "r");
   if (!file) {
       fprintf(stderr, "config(%s) not found!\n", configPath);
       if (configPath == (char *)DEF_CONFIG_FILE) {
           file = fopen(DEF_CONFIG_FILE, "w");
           if (file) {
               fprintf(stderr, "create defconfig\n");
               printOpts(file);
           }
       }
       goto end;
   }
 
   LOGD("start parse\n");
 
   if (SCANF_EAT(file) != 0) {
       goto end;
   }
   while (fscanf(file, "%s", buf) == 1) {
       if (!strcmp(buf, SEC_VERSION)) {
           versionOk = parseVersion(file);
           if (!versionOk) {
               LOGE("parseVersion failed!\n");
               goto end;
           }
       } else if (!strcmp(buf, SEC_BL30)) {
           bl30ok = parseBL3x(file, BL30_SEC);
           if (!bl30ok) {
               LOGE("parseBL30 failed!\n");
               goto end;
           }
       } else if (!strcmp(buf, SEC_BL31)) {
           bl31ok = parseBL3x(file, BL31_SEC);
           if (!bl31ok) {
               LOGE("parseBL31 failed!\n");
               goto end;
           }
       } else if (!strcmp(buf, SEC_BL32)) {
           bl32ok = parseBL3x(file, BL32_SEC);
           if (!bl32ok) {
               LOGE("parseBL32 failed!\n");
               goto end;
           }
       } else if (!strcmp(buf, SEC_BL33)) {
           bl33ok = parseBL3x(file, BL33_SEC);
           if (!bl33ok) {
               LOGE("parseBL33 failed!\n");
               goto end;
           }
       } else if (!strcmp(buf, SEC_OUT)) {
           outOk = parseOut(file);
           if (!outOk) {
               LOGE("parseOut failed!\n");
               goto end;
           }
       } else if (buf[0] == '#') {
           continue;
       } else {
           LOGD("unknown sec: %s!\n", buf);
           continue;
       }
       if (SCANF_EAT(file) != 0) {
           goto end;
       }
   }
 
   if (bl30ok && bl31ok && bl32ok && bl33ok && outOk)
       ret = true;
end:
   if (file)
       fclose(file);
 
   return ret;
}
 
bool initOpts(void)
{
 
   memset(&gOpts, 0, sizeof(gOpts));
 
   gOpts.major = DEF_MAJOR;
   gOpts.minor = DEF_MINOR;
 
   memcpy(&gOpts.bl3x[BL30_SEC].id, gBl3xID[BL30_SEC], 4);
   strcpy(gOpts.bl3x[BL30_SEC].path, DEF_BL30_PATH);
 
   memcpy(&gOpts.bl3x[BL31_SEC].id, gBl3xID[BL31_SEC], 4);
   strcpy(gOpts.bl3x[BL31_SEC].path, DEF_BL31_PATH);
 
   memcpy(&gOpts.bl3x[BL32_SEC].id, gBl3xID[BL32_SEC], 4);
   strcpy(gOpts.bl3x[BL32_SEC].path, DEF_BL32_PATH);
 
   memcpy(&gOpts.bl3x[BL33_SEC].id, gBl3xID[BL33_SEC], 4);
   strcpy(gOpts.bl3x[BL33_SEC].path, DEF_BL33_PATH);
 
   strcpy(gOpts.outPath, DEF_OUT_PATH);
 
   return parseOpts();
}
 
static inline bool getFileSize(const char *path, uint32_t *size)
{
   struct stat st;
 
   if (stat(path, &st) < 0)
       return false;
   *size = st.st_size;
   LOGD("path:%s, size:%d\n", path, *size);
   return true;
}
 
void fill_file(FILE *file, char ch, uint32_t fill_size)
{
   uint8_t fill_buffer[1024];
   uint32_t cur_write;
 
   memset(fill_buffer, ch, 1024);
   while (fill_size > 0) {
       cur_write = (fill_size >= 1024) ? 1024 : fill_size;
       fwrite(fill_buffer, 1, cur_write, file);
       fill_size -= cur_write;
   }
}
 
bool filter_elf(uint32_t index, uint8_t *pMeta, uint32_t *pMetaNum,
                bool *bElf)
{
   bool ret = false;
   FILE *file = NULL;
   uint8_t *file_buffer = NULL;
   uint32_t file_size, read_size, i;
   Elf32_Ehdr *pElfHeader32;
   Elf32_Phdr *pElfProgram32;
   Elf64_Ehdr *pElfHeader64;
   Elf64_Phdr *pElfProgram64;
   bl_entry_t *pEntry = (bl_entry_t *)(pMeta + sizeof(bl_entry_t) * (*pMetaNum));
   LOGD("index=%d,file=%s\n", index, gOpts.bl3x[index].path);
 
   if (!getFileSize(gOpts.bl3x[index].path, &file_size))
       goto exit_fileter_elf;
   file = fopen(gOpts.bl3x[index].path, "rb");
   if (!file) {
       LOGE("open file(%s) failed\n", gOpts.bl3x[index].path);
       goto exit_fileter_elf;
   }
   file_buffer = malloc(file_size);
   if (!file_buffer)
       goto exit_fileter_elf;
   read_size = fread(file_buffer, 1, file_size, file);
   if (read_size != file_size)
       goto exit_fileter_elf;
 
   if (*((uint32_t *)file_buffer) != ELF_MAGIC) {
       ret = true;
       *bElf = false;
       goto exit_fileter_elf;
   }
   *bElf = true;
   if (file_buffer[5] != 1) { /* only support little endian */
       goto exit_fileter_elf;
   }
   if (*((uint16_t *)(file_buffer + EI_NIDENT)) !=
       2) { /* only support executable case */
       goto exit_fileter_elf;
   }
 
   if (file_buffer[4] == 2) {
       pElfHeader64 = (Elf64_Ehdr *)file_buffer;
       for (i = 0; i < pElfHeader64->e_phnum; i++) {
           pElfProgram64 = (Elf64_Phdr *)(file_buffer + pElfHeader64->e_phoff +
                                          i * pElfHeader64->e_phentsize);
           if (pElfProgram64->p_type == 1) { /* PT_LOAD */
               pEntry->id = gOpts.bl3x[index].id;
               strcpy(pEntry->path, gOpts.bl3x[index].path);
               pEntry->size = (uint32_t) pElfProgram64->p_filesz;
               pEntry->offset = (uint32_t) pElfProgram64->p_offset;
               pEntry->align_size = DO_ALIGN(pEntry->size, ENTRY_ALIGN);
               pEntry->addr = (uint32_t) pElfProgram64->p_paddr;
               if (pEntry->align_size > BL3X_FILESIZE_MAX) {
                   LOGE("elf_file %s too large,segment=%d.\n", pEntry->path, i);
                   goto exit_fileter_elf;
               }
               LOGD("bl3%d: filesize = %d, imagesize = %d, segment=%d\n", index,
                    pEntry->size, pEntry->align_size, i);
               pEntry++;
               (*pMetaNum)++;
           }
       }
 
   } else {
       pElfHeader32 = (Elf32_Ehdr *)file_buffer;
       for (i = 0; i < pElfHeader32->e_phnum; i++) {
           pElfProgram32 = (Elf32_Phdr *)(file_buffer + pElfHeader32->e_phoff +
                                          i * pElfHeader32->e_phentsize);
           if (pElfProgram32->p_type == 1) { /* PT_LOAD */
               pEntry->id = gOpts.bl3x[index].id;
               strcpy(pEntry->path, gOpts.bl3x[index].path);
               pEntry->size = pElfProgram32->p_filesz;
               pEntry->offset = pElfProgram32->p_offset;
               pEntry->align_size = DO_ALIGN(pEntry->size, ENTRY_ALIGN);
               pEntry->addr = pElfProgram32->p_paddr;
               if (pEntry->align_size > BL3X_FILESIZE_MAX) {
                   LOGE("elf_file %s too large,segment=%d.\n", pEntry->path, i);
                   goto exit_fileter_elf;
               }
               LOGD("bl3%d: filesize = %d, imagesize = %d, segment=%d\n", index,
                    pEntry->size, pEntry->align_size, i);
               pEntry++;
               (*pMetaNum)++;
           }
       }
   }
   ret = true;
exit_fileter_elf:
   if (file)
       fclose(file);
   if (file_buffer)
       free(file_buffer);
   return ret;
}
 
#define SHA256_CHECK_SZ ((uint32_t)(256 * 1024))
static bool bl3xHash256(uint8_t *pHash, uint8_t *pData, uint32_t nDataSize)
{
   uint32_t nHashSize, nHasHashSize;
 
   if (!pHash || !pData || !nDataSize) {
       return false;
   }
 
   nHasHashSize = 0;
 
   if (gSHAmode == SHA_SEL_256_RK) {
       sha256_ctx ctx;
 
       sha256_begin(&ctx);
       while (nDataSize > 0) {
           nHashSize = (nDataSize >= SHA256_CHECK_SZ) ? SHA256_CHECK_SZ : nDataSize;
           sha256_hash(&ctx, pData + nHasHashSize, nHashSize);
           nHasHashSize += nHashSize;
           nDataSize -= nHashSize;
       }
       sha256_end(&ctx, pHash);
   } else {
       sha256_context ctx;
 
       sha256_starts(&ctx);
       while (nDataSize > 0) {
           nHashSize = (nDataSize >= SHA256_CHECK_SZ) ? SHA256_CHECK_SZ : nDataSize;
           sha256_update(&ctx, pData + nHasHashSize, nHashSize);
           nHasHashSize += nHashSize;
           nDataSize -= nHashSize;
       }
       sha256_finish(&ctx, pHash);
   }
   return true;
}
 
static bool mergetrust(void)
{
   FILE *outFile = NULL;
   uint32_t OutFileSize;
   uint32_t SrcFileNum, SignOffset, nComponentNum;
   TRUST_HEADER *pHead = NULL;
   COMPONENT_DATA *pComponentData = NULL;
   TRUST_COMPONENT *pComponent = NULL;
   bool ret = false, bElf;
   uint32_t i, n;
   uint8_t *outBuf = NULL, *pbuf = NULL, *pMetaBuf = NULL;
   bl_entry_t *pEntry = NULL;
   if (!initOpts())
       return false;
 
   if (gDebug) {
       printf("---------------\nUSING CONFIG:\n");
       printOpts(stdout);
       printf("---------------\n\n");
   }
   pMetaBuf = malloc(sizeof(bl_entry_t) * 32);
   if (!pMetaBuf) {
       LOGE("Merge trust image: malloc buffer error.\n");
       goto end;
   }
 
   nComponentNum = SrcFileNum = 0;
   for (i = BL30_SEC; i < BL_MAX_SEC; i++) {
       if (gOpts.bl3x[i].sec) {
           if (!filter_elf(i, pMetaBuf, &nComponentNum, &bElf)) {
               LOGE("filter_elf %s file failed\n", gOpts.bl3x[i].path);
               goto end;
           }
           if (!bElf) {
               pEntry = (bl_entry_t *)(pMetaBuf + sizeof(bl_entry_t) * nComponentNum);
               pEntry->id = gOpts.bl3x[i].id;
               strcpy(pEntry->path, gOpts.bl3x[i].path);
               getFileSize(pEntry->path, &pEntry->size);
               pEntry->offset = 0;
               pEntry->align_size = DO_ALIGN(pEntry->size, ENTRY_ALIGN);
               pEntry->addr = gOpts.bl3x[i].addr;
               if (pEntry->align_size > BL3X_FILESIZE_MAX) {
                   LOGE("file %s too large.\n", gOpts.bl3x[i].path);
                   goto end;
               }
               LOGD("bl3%d: filesize = %d, imagesize = %d\n", i, pEntry->size,
                    pEntry->align_size);
               pEntry++;
               nComponentNum++;
           }
 
       }
   }
   LOGD("bl3x bin sec = %d\n", nComponentNum);
 
   /* 2048bytes for head */
   memset(gBuf, 0, TRUST_HEADER_SIZE);
 
   /* Trust Head */
   pHead = (TRUST_HEADER *)gBuf;
   memcpy(&pHead->tag, TRUST_HEAD_TAG, 4);
   pHead->version = (getBCD(gOpts.major) << 8) | getBCD(gOpts.minor);
   pHead->flags = 0;
   pHead->flags |= (gSHAmode << 0);
   pHead->flags |= (gRSAmode << 4);
 
   SignOffset = sizeof(TRUST_HEADER) + nComponentNum * sizeof(COMPONENT_DATA);
   LOGD("trust bin sign offset = %d\n", SignOffset);
   pHead->size = (nComponentNum << 16) | (SignOffset >> 2);
 
   pComponent = (TRUST_COMPONENT *)(gBuf + SignOffset + SIGNATURE_SIZE);
   pComponentData = (COMPONENT_DATA *)(gBuf + sizeof(TRUST_HEADER));
 
   OutFileSize = TRUST_HEADER_SIZE;
   pEntry = (bl_entry_t *)pMetaBuf;
   for (i = 0; i < nComponentNum; i++) {
       /* bl3x load and run address */
       pComponentData->LoadAddr = pEntry->addr;
 
       pComponent->ComponentID = pEntry->id;
       pComponent->StorageAddr = (OutFileSize >> 9);
       pComponent->ImageSize = (pEntry->align_size >> 9);
       pComponentData->LoadSize = pComponent->ImageSize;
 
       LOGD("bl3%c: LoadAddr = 0x%08x, StorageAddr = %d, ImageSize = %d\n",
            (char)((pEntry->id & 0xFF000000) >> 24), pComponentData->LoadAddr,
            pComponent->StorageAddr, pComponent->ImageSize);
 
       OutFileSize += pEntry->align_size;
       pComponentData++;
       pComponent++;
       pEntry++;
   }
 
   /* create out file */
   outFile = fopen(gOpts.outPath, "wb+");
   if (!outFile) {
       LOGE("open out file(%s) failed\n", gOpts.outPath);
 
       outFile = fopen(DEF_OUT_PATH, "wb");
       if (!outFile) {
           LOGE("open default out file:%s failed!\n", DEF_OUT_PATH);
           goto end;
       }
   }
 
   /* 0 for g_trust_max_num backups */
#if 0
   /* save trust head to out file */
   if (!fwrite(gBuf, TRUST_HEADER_SIZE, 1, outFile))
       goto end;
 
   /* save trust bl3x bin */
   for (i = BL30_SEC; i < BL_MAX_SEC; i++) {
       if (gOpts.bl3x[i].sec) {
           FILE *inFile = fopen(gOpts.bl3x[i].path, "rb");
           if (!inFile)
               goto end;
 
           memset(gBuf, 0, imagesize[i]);
           if (!fread(gBuf, filesize[i], 1, inFile))
               goto end;
           fclose(inFile);
 
           if (!fwrite(gBuf, imagesize[i], 1, outFile))
               goto end;
       }
   }
#else
   /* check bin size */
   if (OutFileSize > g_trust_max_size) {
       LOGE("Merge trust image: trust bin size overfull.\n");
       goto end;
   }
 
   /* malloc buffer */
   pbuf = outBuf = calloc(g_trust_max_size, g_trust_max_num);
   if (!outBuf) {
       LOGE("Merge trust image: calloc buffer error.\n");
       goto end;
   }
   memset(outBuf, 0, (g_trust_max_size * g_trust_max_num));
 
   /* save trust head data */
   memcpy(pbuf, gBuf, TRUST_HEADER_SIZE);
   pbuf += TRUST_HEADER_SIZE;
 
   uint8_t *pHashData = NULL;
   pComponentData = (COMPONENT_DATA *)(outBuf + sizeof(TRUST_HEADER));
 
   /* save trust bl3x bin */
   pEntry = (bl_entry_t *)pMetaBuf;
   for (i = 0; i < nComponentNum; i++) {
       FILE *inFile = fopen(pEntry->path, "rb");
       if (!inFile)
           goto end;
 
       memset(gBuf, 0, pEntry->align_size);
       fseek(inFile, pEntry->offset, SEEK_SET);
       if (!fread(gBuf, pEntry->size, 1, inFile))
           goto end;
       fclose(inFile);
 
       /* bl3x bin hash256 */
       pHashData = (uint8_t *)&pComponentData->HashData[0];
       bl3xHash256(pHashData, gBuf, pEntry->align_size);
       memcpy(pbuf, gBuf, pEntry->align_size);
 
       pComponentData++;
       pbuf += pEntry->align_size;
       pEntry++;
   }
 
   /* copy other (g_trust_max_num - 1) backup bin */
   for (n = 1; n < g_trust_max_num; n++) {
       memcpy(outBuf + g_trust_max_size * n, outBuf, g_trust_max_size);
   }
 
   /* save date to file */
   if (!fwrite(outBuf, g_trust_max_size * g_trust_max_num, 1, outFile)) {
       LOGE("Merge trust image: write file error.\n");
       goto end;
   }
#endif
 
   ret = true;
 
end:
   /*
       for (i = BL30_SEC; i < BL_MAX_SEC; i++) {
           if (gOpts.bl3x[i].sec != false) {
               if (gOpts.bl3x[i].is_elf) {
                   if (stat(gOpts.bl3x[i].path, &st) >= 0)
                       remove(gOpts.bl3x[i].path);
               }
           }
       }
   */
   if (pMetaBuf)
       free(pMetaBuf);
   if (outBuf)
       free(outBuf);
   if (outFile)
       fclose(outFile);
   return ret;
}
 
static int saveDatatoFile(char *FileName, void *pBuf, uint32_t size)
{
   FILE *OutFile = NULL;
   int ret = -1;
 
   OutFile = fopen(FileName, "wb");
   if (!OutFile) {
       printf("open OutPutFlie:%s failed!\n", FileName);
       goto end;
   }
   if (1 != fwrite(pBuf, size, 1, OutFile)) {
       printf("write output file failed!\n");
       goto end;
   }
 
   ret = 0;
end:
   if (OutFile)
       fclose(OutFile);
 
   return ret;
}
 
static bool unpacktrust(char *path)
{
   FILE *FileSrc = NULL;
   uint32_t FileSize;
   uint8_t *pBuf = NULL;
   uint32_t SrcFileNum, SignOffset;
   TRUST_HEADER *pHead = NULL;
   COMPONENT_DATA *pComponentData = NULL;
   TRUST_COMPONENT *pComponent = NULL;
   char str[MAX_LINE_LEN];
   bool ret = false;
   uint32_t i;
 
   FileSrc = fopen(path, "rb");
   if (FileSrc == NULL) {
       printf("open %s failed!\n", path);
       goto end;
   }
 
   if (getFileSize(path, &FileSize) == false) {
       printf("File Size failed!\n");
       goto end;
   }
   printf("File Size = %d\n", FileSize);
 
   pBuf = (uint8_t *)malloc(FileSize);
   if (1 != fread(pBuf, FileSize, 1, FileSrc)) {
       printf("read input file failed!\n");
       goto end;
   }
 
   pHead = (TRUST_HEADER *)pBuf;
 
   memcpy(str, &pHead->tag, 4);
   str[4] = '\0';
   printf("Header Tag:%s\n", str);
   printf("Header version:%d\n", pHead->version);
   printf("Header flag:%d\n", pHead->flags);
 
   SrcFileNum = (pHead->size >> 16) & 0xffff;
   SignOffset = (pHead->size & 0xffff) << 2;
   printf("SrcFileNum:%d\n", SrcFileNum);
   printf("SignOffset:%d\n", SignOffset);
 
   pComponent = (TRUST_COMPONENT *)(pBuf + SignOffset + SIGNATURE_SIZE);
   pComponentData = (COMPONENT_DATA *)(pBuf + sizeof(TRUST_HEADER));
 
   for (i = 0; i < SrcFileNum; i++) {
       printf("Component %d:\n", i);
 
       memcpy(str, &pComponent->ComponentID, 4);
       str[4] = '\0';
       printf("ComponentID:%s\n", str);
       printf("StorageAddr:0x%x\n", pComponent->StorageAddr);
       printf("ImageSize:0x%x\n", pComponent->ImageSize);
       printf("LoadAddr:0x%x\n", pComponentData->LoadAddr);
 
       saveDatatoFile(str, pBuf + (pComponent->StorageAddr << 9),
                      pComponent->ImageSize << 9);
 
       pComponentData++;
       pComponent++;
   }
 
   ret = true;
end:
   if (FileSrc)
       fclose(FileSrc);
   if (pBuf)
       free(pBuf);
 
   return ret;
}
 
static void printHelp(void)
{
   printf("Usage: trust_merger [options]... FILE\n");
   printf(
           "Merge or unpack Rockchip's trust image (Default action is to merge.)\n");
   printf("Options:\n");
   printf("\t" OPT_MERGE "\t\t\tMerge trust with specified config.\n");
   printf("\t" OPT_UNPACK "\t\tUnpack specified trust to current dir.\n");
   printf("\t" OPT_VERBOSE "\t\tDisplay more runtime informations.\n");
   printf("\t" OPT_HELP "\t\t\tDisplay this information.\n");
   printf("\t" OPT_VERSION "\t\tDisplay version information.\n");
   printf("\t" OPT_SUBFIX "\t\tSpec subfix.\n");
   printf("\t" OPT_REPLACE "\t\tReplace some part of binary path.\n");
   printf("\t" OPT_PREPATH "\t\tAdd prefix path of binary path.\n");
   printf("\t" OPT_RSA "\t\t\tRSA mode.\"--rsa [mode]\", [mode] can be: "
          "0(none), 1(1024), 2(2048), 3(2048 pss).\n");
   printf("\t" OPT_SHA
          "\t\t\tSHA mode.\"--sha [mode]\", [mode] can be: 0(none), 1(160), "
          "2(256 RK big endian), 3(256 little endian).\n");
   printf("\t" OPT_SIZE "\t\t\tTrustImage size.\"--size [per image KB size] "
          "[copy count]\", per image must be 64KB aligned\n");
}
 
int main(int argc, char **argv)
{
   bool merge = true;
   char *optPath = NULL;
   int i;
 
   gConfigPath = NULL;
   for (i = 1; i < argc; i++) {
       if (!strcmp(OPT_VERBOSE, argv[i])) {
           gDebug = true;
       } else if (!strcmp(OPT_HELP, argv[i])) {
           printHelp();
           return 0;
       } else if (!strcmp(OPT_VERSION, argv[i])) {
           printf("trust_merger (cwz@rock-chips.com)\t" VERSION "\n");
           return 0;
       } else if (!strcmp(OPT_MERGE, argv[i])) {
           merge = true;
       } else if (!strcmp(OPT_UNPACK, argv[i])) {
           merge = false;
       } else if (!strcmp(OPT_SUBFIX, argv[i])) {
           gSubfix = true;
           printf("trust_merger: Spec subfix!\n");
       } else if (!strcmp(OPT_REPLACE, argv[i])) {
           i++;
           gLegacyPath = argv[i];
           i++;
           gNewPath = argv[i];
       } else if (!strcmp(OPT_PREPATH, argv[i])) {
           i++;
           gPrePath = argv[i];
       } else if (!strcmp(OPT_RSA, argv[i])) {
           i++;
           if (!is_digit(*(argv[i]))) {
               printHelp();
               return -1;
           }
           gRSAmode = *(argv[i]) - '0';
           LOGD("rsa mode:%d\n", gRSAmode);
       } else if (!strcmp(OPT_SHA, argv[i])) {
           i++;
           if (!is_digit(*(argv[i]))) {
               printHelp();
               return -1;
           }
           gSHAmode = *(argv[i]) - '0';
           LOGD("sha mode:%d\n", gSHAmode);
       } else if (!strcmp(OPT_SIZE, argv[i])) {
           /* Per trust image size */
           g_trust_max_size = strtoul(argv[++i], NULL, 10);
           /*
            * Usually, it must be at 512kb align due to preloader
            * detects every 512kb. But some product has critial
            * flash size requirement, we have to make it small than
            * 512KB.
            */
           if (g_trust_max_size % 64) {
               printHelp();
               return -1;
           }
           g_trust_max_size *= 1024; /* bytes */
 
           /* Total backup numbers */
           g_trust_max_num = strtoul(argv[++i], NULL, 10);
       } else if (!strcmp(OPT_IGNORE_BL32, argv[i])) {
           gIgnoreBL32 = true;
       } else {
           if (optPath) {
               fprintf(stderr, "only need one path arg, but we have:\n%s\n%s.\n",
                       optPath, argv[i]);
               printHelp();
               return -1;
           }
           optPath = argv[i];
       }
   }
   if (!merge && !optPath) {
       fprintf(stderr, "need set out path to unpack!\n");
       printHelp();
       return -1;
   }
 
   if (merge) {
       LOGD("do_merge\n");
       gConfigPath = optPath;
       if (!mergetrust()) {
           fprintf(stderr, "merge failed!\n");
           return -1;
       }
       printf("merge success(%s)\n", gOpts.outPath);
   } else {
       LOGD("do_unpack\n");
       if (!unpacktrust(optPath)) {
           fprintf(stderr, "unpack failed!\n");
           return -1;
       }
       printf("unpack success\n");
   }
 
   return 0;
}