-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmkimage_imx8.c
1557 lines (1314 loc) · 47.4 KB
/
mkimage_imx8.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) 2016 Freescale Semiconductor, Inc.
*
* SPDX-License-Identifier: GPL-2.0+
* derived from u-boot's mkimage utility
*
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <sys/stat.h>
#include <getopt.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <zlib.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif
#undef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
typedef struct {
uint32_t addr;
uint32_t value;
} dcd_addr_data_t;
typedef struct {
uint8_t tag;
uint16_t length;
uint8_t version;
} __attribute__((packed)) ivt_header_t;
typedef struct {
uint8_t tag;
uint16_t length;
uint8_t param;
} __attribute__((packed)) write_dcd_command_t;
#define MAX_HW_CFG_SIZE_V2 1017
struct dcd_v2_cmd {
write_dcd_command_t write_dcd_command; /*4*/
dcd_addr_data_t addr_data[MAX_HW_CFG_SIZE_V2]; /*8136*/
} __attribute__((packed));
typedef struct {
ivt_header_t header;
struct dcd_v2_cmd dcd_cmd;
uint32_t padding[1]; /* end up on an 8-byte boundary */
} dcd_v2_t;
typedef struct {
uint32_t start;
uint32_t size;
uint32_t plugin;
uint32_t padding[1];
} boot_data_t;
typedef struct {
ivt_header_t header;
uint32_t entry;
uint32_t reserved1;
uint32_t dcd_ptr;
uint32_t boot_data_ptr;
uint32_t self;
uint32_t csf;
uint32_t reserved2;
} flash_header_v2_t;
typedef struct {
flash_header_v2_t fhdr;
boot_data_t boot_data;
} imx_header_v2_t;
#define IH_MAGIC 0x27051956 /* Image Magic Number */
#define IH_NMLEN 32 /* Image Name Length */
typedef struct uimage_header {
uint32_t ih_magic; /* Image Header Magic Number */
uint32_t ih_hcrc; /* Image Header CRC Checksum */
uint32_t ih_time; /* Image Creation Timestamp */
uint32_t ih_size; /* Image Data Size */
uint32_t ih_load; /* Data Load Address */
uint32_t ih_ep; /* Entry Point Address */
uint32_t ih_dcrc; /* Image Data CRC Checksum */
uint8_t ih_os; /* Operating System */
uint8_t ih_arch; /* CPU architecture */
uint8_t ih_type; /* Image Type */
uint8_t ih_comp; /* Compression Type */
uint8_t ih_name[IH_NMLEN]; /* Image Name */
} uimage_header_t;
struct fdt_header {
uint32_t magic; /* magic word FDT_MAGIC */
uint32_t totalsize; /* total size of DT block */
uint32_t off_dt_struct; /* offset to structure */
uint32_t off_dt_strings; /* offset to strings */
uint32_t off_mem_rsvmap; /* offset to memory reserve map */
uint32_t version; /* format version */
uint32_t last_comp_version; /* last compatible version */
/* version 2 fields below */
uint32_t boot_cpuid_phys; /* Which physical CPU id we're
booting on */
/* version 3 fields below */
uint32_t size_dt_strings; /* size of the strings block */
/* version 17 fields below */
uint32_t size_dt_struct; /* size of the structure block */
};
/* Command tags and parameters */
#define HAB_DATA_WIDTH_BYTE 1 /* 8-bit value */
#define HAB_DATA_WIDTH_HALF 2 /* 16-bit value */
#define HAB_DATA_WIDTH_WORD 4 /* 32-bit value */
#define HAB_CMD_WRT_DAT_MSK 1 /* mask/value flag */
#define HAB_CMD_WRT_DAT_SET 2 /* set/clear flag */
#define HAB_CMD_CHK_DAT_SET 2 /* set/clear flag */
#define HAB_CMD_CHK_DAT_ANY 4 /* any/all flag */
#define HAB_CMD_WRT_DAT_FLAGS_WIDTH 5 /* flags field width */
#define HAB_CMD_WRT_DAT_FLAGS_SHIFT 3 /* flags field offset */
#define HAB_CMD_WRT_DAT_BYTES_WIDTH 3 /* bytes field width */
#define HAB_CMD_WRT_DAT_BYTES_SHIFT 0 /* bytes field offset */
#define IVT_HEADER_TAG 0xD1
#define IVT_VERSION 0x41
#define DCD_HEADER_TAG 0xD2
#define DCD_VERSION 0x41
#define DCD_WRITE_DATA_COMMAND_TAG 0xCC
#define DCD_WRITE_DATA_PARAM (HAB_DATA_WIDTH_WORD << HAB_CMD_WRT_DAT_BYTES_SHIFT) /* 0x4 */
#define DCD_WRITE_CLR_BIT_PARAM ((HAB_CMD_WRT_DAT_MSK << HAB_CMD_WRT_DAT_FLAGS_SHIFT) | (HAB_DATA_WIDTH_WORD << HAB_CMD_WRT_DAT_BYTES_SHIFT)) /* 0xC */
#define DCD_WRITE_SET_BIT_PARAM ((HAB_CMD_WRT_DAT_MSK << HAB_CMD_WRT_DAT_FLAGS_SHIFT) | (HAB_CMD_WRT_DAT_SET << HAB_CMD_WRT_DAT_FLAGS_SHIFT) | (HAB_DATA_WIDTH_WORD << HAB_CMD_WRT_DAT_BYTES_SHIFT)) /* 0x1C */
#define DCD_CHECK_DATA_COMMAND_TAG 0xCF
#define DCD_CHECK_BITS_CLR_PARAM (HAB_DATA_WIDTH_WORD << HAB_CMD_WRT_DAT_BYTES_SHIFT) /* 0x04 */
#define DCD_CHECK_BITS_SET_PARAM ((HAB_CMD_CHK_DAT_SET << HAB_CMD_WRT_DAT_FLAGS_SHIFT) | (HAB_DATA_WIDTH_WORD << HAB_CMD_WRT_DAT_BYTES_SHIFT)) /* 0x14 */
#define DCD_CHECK_ANY_BIT_CLR_PARAM ((HAB_CMD_CHK_DAT_ANY << HAB_CMD_WRT_DAT_FLAGS_SHIFT) | (HAB_DATA_WIDTH_WORD << HAB_CMD_WRT_DAT_BYTES_SHIFT)) /* 0x24 */
#define DCD_CHECK_ANY_BIT_SET_PARAM ((HAB_CMD_CHK_DAT_ANY << HAB_CMD_WRT_DAT_FLAGS_SHIFT) | (HAB_CMD_CHK_DAT_SET << HAB_CMD_WRT_DAT_FLAGS_SHIFT) | (HAB_DATA_WIDTH_WORD << HAB_CMD_WRT_DAT_BYTES_SHIFT)) /* 0x34 */
#define IVT_OFFSET_NAND (0x400)
#define IVT_OFFSET_I2C (0x400)
#define IVT_OFFSET_FLEXSPI (0x1000)
#define IVT_OFFSET_SD (0x400)
#define IVT_OFFSET_SATA (0x400)
#define IMAGE_OFFSET_SD (0x8000)
#define ROM_INITIAL_LOAD_SIZE (0x2000)
#define CSF_DATA_SIZE (0x4000)
#define INITIAL_LOAD_ADDR_SCU_ROM 0x3100e000
#define INITIAL_LOAD_ADDR_AP_ROM 0x00110000
#define INITIAL_LOAD_ADDR_FLEXSPI 0x08000000
#define IMG_AUTO_ALIGN 0x10
#define PLUGIN_IMAGE_FLAG_MASK (0x0001) /* bit 0 is plugin image indicator */
#define HDMI_IMAGE_FLAG_MASK (0x0002) /* bit 1 is HDMI image indicator */
#define HDMI_IVT_ID 0
#define PLUGIN_IVT_ID 1
#define IMAGE_IVT_ID 2
#define HDMI_FW_SIZE 0x17000 /* Use Last 0x1000 for IVT and CSF */
#define HDMI_FW_ADDR 0x32c10000
#define IH_OS_U_BOOT 17
#define IH_ARCH_ARM 2
#define IH_TYPE_FIRMWARE 5
#define IH_COMP_NONE 0
#define FDT_MAGIC 0xd00dfeed
#define CSF_SIZE 0x2000
#define ALIGN(x,a) __ALIGN_MASK((x),(__typeof__(x))(a)-1, a)
#define __ALIGN_MASK(x,mask,mask2) (((x)+(mask))/(mask2)*(mask2))
#define uswap_16(x) \
((((x) & 0xff00) >> 8) | \
(((x) & 0x00ff) << 8))
#define uswap_32(x) \
((((x) & 0xff000000) >> 24) | \
(((x) & 0x00ff0000) >> 8) | \
(((x) & 0x0000ff00) << 8) | \
(((x) & 0x000000ff) << 24))
#define _uswap_64(x, sfx) \
((((x) & 0xff00000000000000##sfx) >> 56) | \
(((x) & 0x00ff000000000000##sfx) >> 40) | \
(((x) & 0x0000ff0000000000##sfx) >> 24) | \
(((x) & 0x000000ff00000000##sfx) >> 8) | \
(((x) & 0x00000000ff000000##sfx) << 8) | \
(((x) & 0x0000000000ff0000##sfx) << 24) | \
(((x) & 0x000000000000ff00##sfx) << 40) | \
(((x) & 0x00000000000000ff##sfx) << 56))
#if defined(__GNUC__)
# define uswap_64(x) _uswap_64(x, ull)
#else
#error
# define uswap_64(x) _uswap_64(x, )
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
# define cpu_to_le16(x) (x)
# define cpu_to_le32(x) (x)
# define cpu_to_le64(x) (x)
# define le16_to_cpu(x) (x)
# define le32_to_cpu(x) (x)
# define le64_to_cpu(x) (x)
# define cpu_to_be16(x) uswap_16(x)
# define cpu_to_be32(x) uswap_32(x)
# define cpu_to_be64(x) uswap_64(x)
# define be16_to_cpu(x) uswap_16(x)
# define be32_to_cpu(x) uswap_32(x)
# define be64_to_cpu(x) uswap_64(x)
#else
#error
# define cpu_to_le16(x) uswap_16(x)
# define cpu_to_le32(x) uswap_32(x)
# define cpu_to_le64(x) uswap_64(x)
# define le16_to_cpu(x) uswap_16(x)
# define le32_to_cpu(x) uswap_32(x)
# define le64_to_cpu(x) uswap_64(x)
# define cpu_to_be16(x) (x)
# define cpu_to_be32(x) (x)
# define cpu_to_be64(x) (x)
# define be16_to_cpu(x) (x)
# define be32_to_cpu(x) (x)
# define be64_to_cpu(x) (x)
#endif
#define fdt32_to_cpu(x) be32_to_cpu(x)
#define fdt_get_header(fdt, field) \
(fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
#define fdt_magic(fdt) (fdt_get_header(fdt, magic))
#define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
#define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
#define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
#define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
#define fdt_version(fdt) (fdt_get_header(fdt, version))
#define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
#define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
#define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
#define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
#define UNDEFINED 0xFFFFFFFF
static void fill_zero(int ifd, int size, int offset)
{
int fill_size;
uint8_t zeros[4096];
memset(zeros, 0, sizeof(zeros));
lseek(ifd, offset, SEEK_SET);
while (size) {
if (size > 4096)
fill_size = 4096;
else
fill_size = size;
if (write(ifd, (char *)&zeros, fill_size) != fill_size) {
fprintf(stderr, "Write error: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
size -= fill_size;
};
}
static void
copy_file (int ifd, const char *datafile, int pad, int offset, int datafile_offset)
{
int dfd;
struct stat sbuf;
unsigned char *ptr;
int tail;
int zero = 0;
uint8_t zeros[4096];
int size;
memset(zeros, 0, sizeof(zeros));
if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
fprintf (stderr, "Can't open %s: %s\n",
datafile, strerror(errno));
exit (EXIT_FAILURE);
}
if (fstat(dfd, &sbuf) < 0) {
fprintf (stderr, "Can't stat %s: %s\n",
datafile, strerror(errno));
exit (EXIT_FAILURE);
}
ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
if (ptr == MAP_FAILED) {
fprintf (stderr, "Can't read %s: %s\n",
datafile, strerror(errno));
exit (EXIT_FAILURE);
}
size = sbuf.st_size - datafile_offset;
lseek(ifd, offset, SEEK_SET);
if (write(ifd, ptr + datafile_offset, size) != size) {
fprintf (stderr, "Write error %s\n",
strerror(errno));
exit (EXIT_FAILURE);
}
tail = size % 4;
pad = pad - size;
if ((pad == 1) && (tail != 0)) {
if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
fprintf (stderr, "Write error on %s\n",
strerror(errno));
exit (EXIT_FAILURE);
}
} else if (pad > 1) {
while (pad > 0) {
int todo = sizeof(zeros);
if (todo > pad)
todo = pad;
if (write(ifd, (char *)&zeros, todo) != todo) {
fprintf(stderr, "Write error: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
pad -= todo;
}
}
(void) munmap((void *)ptr, sbuf.st_size);
(void) close (dfd);
}
enum imximage_fld_types {
CFG_INVALID = -1,
CFG_COMMAND,
CFG_REG_SIZE,
CFG_REG_ADDRESS,
CFG_REG_VALUE
};
enum imximage_cmd {
CMD_INVALID,
CMD_IMAGE_VERSION,
CMD_BOOT_FROM,
CMD_BOOT_OFFSET,
CMD_WRITE_DATA,
CMD_WRITE_CLR_BIT,
CMD_WRITE_SET_BIT,
CMD_CHECK_BITS_SET,
CMD_CHECK_BITS_CLR,
CMD_CHECK_ANY_BIT_SET,
CMD_CHECK_ANY_BIT_CLR,
CMD_CSF,
CMD_PLUGIN,
};
typedef struct table_entry {
int id;
char *sname; /* short (input) name to find table entry */
char *lname; /* long (output) name to print for messages */
} table_entry_t;
/*
* Supported commands for configuration file
*/
static table_entry_t imximage_cmds[] = {
{CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
{CMD_BOOT_OFFSET, "BOOT_OFFSET", "Boot offset", },
{CMD_WRITE_DATA, "DATA", "Reg Write Data", },
{CMD_WRITE_CLR_BIT, "CLR_BIT", "Reg clear bit", },
{CMD_WRITE_SET_BIT, "SET_BIT", "Reg set bit", },
{CMD_CHECK_BITS_SET, "CHECK_BITS_SET", "Reg Check all bits set", },
{CMD_CHECK_BITS_CLR, "CHECK_BITS_CLR", "Reg Check all bits clr", },
{CMD_CHECK_ANY_BIT_SET, "CHECK_ANY_BIT_SET", "Reg Check any bit set", },
{CMD_CHECK_ANY_BIT_CLR, "CHECK_ANY_BIT_CLR", "Reg Check any bit clr", },
{CMD_CSF, "CSF", "Command Sequence File", },
{CMD_IMAGE_VERSION, "IMAGE_VERSION", "image version", },
{-1, "", "", },
};
static uint32_t imximage_version;
static struct dcd_v2_cmd *gd_last_cmd;
static uint32_t imximage_ivt_offset = UNDEFINED;
static uint32_t imximage_csf_size = UNDEFINED;
int get_table_entry_id(const table_entry_t *table,
const char *table_name, const char *name)
{
const table_entry_t *t;
for (t = table; t->id >= 0; ++t) {
if (t->sname && strcasecmp(t->sname, name) == 0)
return (t->id);
}
return -1;
}
static uint32_t get_cfg_value(char *token, char *name, int linenr)
{
char *endptr;
uint32_t value;
errno = 0;
value = strtoul(token, &endptr, 16);
if (errno || (token == endptr)) {
fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
name, linenr, token);
exit(EXIT_FAILURE);
}
return value;
}
static void set_dcd_param_v2(dcd_v2_t *dcd_v2, uint32_t dcd_len,
int32_t cmd)
{
struct dcd_v2_cmd *d = gd_last_cmd;
struct dcd_v2_cmd *d2;
int len;
if (!d)
d = &dcd_v2->dcd_cmd;
d2 = d;
len = be16_to_cpu(d->write_dcd_command.length);
if (len > 4)
d2 = (struct dcd_v2_cmd *)(((char *)d) + len);
switch (cmd) {
/* Write value: *address = val_msk */
case CMD_WRITE_DATA:
if ((d->write_dcd_command.tag == DCD_WRITE_DATA_COMMAND_TAG) &&
(d->write_dcd_command.param == DCD_WRITE_DATA_PARAM))
break;
d = d2;
d->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG;
d->write_dcd_command.length = cpu_to_be16(4);
d->write_dcd_command.param = DCD_WRITE_DATA_PARAM;
break;
/* Clear bitmask: *address &= ~val_msk */
case CMD_WRITE_CLR_BIT:
if ((d->write_dcd_command.tag == DCD_WRITE_DATA_COMMAND_TAG) &&
(d->write_dcd_command.param == DCD_WRITE_CLR_BIT_PARAM))
break;
d = d2;
d->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG;
d->write_dcd_command.length = cpu_to_be16(4);
d->write_dcd_command.param = DCD_WRITE_CLR_BIT_PARAM;
break;
/* Set bitmask: *address |= val_msk */
case CMD_WRITE_SET_BIT:
if ((d->write_dcd_command.tag == DCD_WRITE_DATA_COMMAND_TAG) &&
(d->write_dcd_command.param == DCD_WRITE_SET_BIT_PARAM))
break;
d = d2;
d->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG;
d->write_dcd_command.length = cpu_to_be16(4);
d->write_dcd_command.param = DCD_WRITE_SET_BIT_PARAM;
break;
/*
* Check data command only supports one entry,
*/
/* All bits set: (*address & mask) == mask */
case CMD_CHECK_BITS_SET:
d = d2;
d->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG;
d->write_dcd_command.length = cpu_to_be16(4);
d->write_dcd_command.param = DCD_CHECK_BITS_SET_PARAM;
break;
/* All bits clear: (*address & mask) == 0 */
case CMD_CHECK_BITS_CLR:
d = d2;
d->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG;
d->write_dcd_command.length = cpu_to_be16(4);
d->write_dcd_command.param = DCD_CHECK_BITS_CLR_PARAM;
break;
default:
break;
}
gd_last_cmd = d;
}
static void set_dcd_val_v2(dcd_v2_t *dcd_v2, char *name, int lineno,
int fld, uint32_t value, uint32_t off)
{
struct dcd_v2_cmd *d = gd_last_cmd;
int len;
len = be16_to_cpu(d->write_dcd_command.length);
off = (len - 4) >> 3;
switch (fld) {
case CFG_REG_ADDRESS:
d->addr_data[off].addr = cpu_to_be32(value);
break;
case CFG_REG_VALUE:
d->addr_data[off].value = cpu_to_be32(value);
off++;
d->write_dcd_command.length = cpu_to_be16((off << 3) + 4);
break;
default:
break;
}
}
static uint32_t set_dcd_rst_v2(dcd_v2_t *dcd_v2, uint32_t dcd_len,
char *name, int lineno)
{
struct dcd_v2_cmd *d = gd_last_cmd;
int len;
if (!d)
d = &dcd_v2->dcd_cmd;
len = be16_to_cpu(d->write_dcd_command.length);
if (len > 4)
d = (struct dcd_v2_cmd *)(((char *)d) + len);
len = (char *)d - (char *)&dcd_v2->header;
dcd_v2->header.tag = DCD_HEADER_TAG;
dcd_v2->header.length = cpu_to_be16(len);
dcd_v2->header.version = DCD_VERSION;
return len;
}
static void parse_cfg_cmd(dcd_v2_t *dcd_v2, int32_t cmd, char *token,
char *name, int lineno, int fld, int dcd_len)
{
int value;
static int cmd_ver_first = ~0;
switch (cmd) {
case CMD_IMAGE_VERSION:
imximage_version = get_cfg_value(token, name, lineno);
if (cmd_ver_first == 0) {
fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
"command need be the first before other "
"valid command in the file\n", name, lineno);
exit(EXIT_FAILURE);
}
cmd_ver_first = 1;
break;
case CMD_BOOT_OFFSET:
imximage_ivt_offset = get_cfg_value(token, name, lineno);
if (cmd_ver_first != 1)
cmd_ver_first = 0;
break;
case CMD_WRITE_DATA:
case CMD_WRITE_CLR_BIT:
case CMD_WRITE_SET_BIT:
case CMD_CHECK_BITS_SET:
case CMD_CHECK_BITS_CLR:
value = get_cfg_value(token, name, lineno);
set_dcd_param_v2(dcd_v2, dcd_len, cmd);
set_dcd_val_v2(dcd_v2, name, lineno, fld, value, dcd_len); /*nothing to do for v2, because we are in CFG_REG_SIZE fld */
if (cmd_ver_first != 1)
cmd_ver_first = 0;
break;
case CMD_CSF:
if (imximage_version != 2) {
fprintf(stderr,
"Error: %s[%d] - CSF only supported for VERSION 2(%s)\n",
name, lineno, token);
exit(EXIT_FAILURE);
}
imximage_csf_size = get_cfg_value(token, name, lineno);
if (cmd_ver_first != 1)
cmd_ver_first = 0;
break;
}
}
static void parse_cfg_fld(dcd_v2_t *dcd_v2, int32_t *cmd,
char *token, char *name, int lineno, int fld, int *dcd_len)
{
int value;
switch (fld) {
case CFG_COMMAND:
*cmd = get_table_entry_id(imximage_cmds,
"imximage commands", token);
if (*cmd < 0) {
fprintf(stderr, "Error: %s[%d] - Invalid command"
"(%s)\n", name, lineno, token);
exit(EXIT_FAILURE);
}
break;
case CFG_REG_SIZE:
parse_cfg_cmd(dcd_v2, *cmd, token, name, lineno, fld, *dcd_len);
break;
case CFG_REG_ADDRESS:
case CFG_REG_VALUE:
switch(*cmd) {
case CMD_WRITE_DATA:
case CMD_WRITE_CLR_BIT:
case CMD_WRITE_SET_BIT:
case CMD_CHECK_BITS_SET:
case CMD_CHECK_BITS_CLR:
value = get_cfg_value(token, name, lineno);
set_dcd_param_v2(dcd_v2, *dcd_len, *cmd);
set_dcd_val_v2(dcd_v2, name, lineno, fld, value,
*dcd_len);
if (fld == CFG_REG_VALUE) {
(*dcd_len)++;
if (*dcd_len > MAX_HW_CFG_SIZE_V2) {
fprintf(stderr, "Error: %s[%d] -"
"DCD table exceeds maximum size(%d)\n",
name, lineno, MAX_HW_CFG_SIZE_V2);
exit(EXIT_FAILURE);
}
}
break;
default:
break;
}
break;
default:
break;
}
}
static uint32_t parse_cfg_file(dcd_v2_t *dcd_v2, char *name)
{
FILE *fd = NULL;
char *line = NULL;
char *token, *saveptr1, *saveptr2;
int lineno = 0;
int fld;
size_t len;
int dcd_len = 0, dcd_size = 0;
int32_t cmd;
fd = fopen(name, "r");
if (fd == 0) {
fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
exit(EXIT_FAILURE);
}
/*
* Very simple parsing, line starting with # are comments
* and are dropped
*/
while ((getline(&line, &len, fd)) > 0) {
lineno++;
token = strtok_r(line, "\r\n", &saveptr1);
if (token == NULL)
continue;
/* Check inside the single line */
for (fld = CFG_COMMAND, cmd = CMD_INVALID,
line = token; ; line = NULL, fld++) {
token = strtok_r(line, " \t", &saveptr2);
if (token == NULL)
break;
/* Drop all text starting with '#' as comments */
if (token[0] == '#')
break;
parse_cfg_fld(dcd_v2, &cmd, token, name,
lineno, fld, &dcd_len);
}
}
dcd_size = set_dcd_rst_v2(dcd_v2, dcd_len, name, lineno);
fclose(fd);
return dcd_size;
}
void dump_header_v2(imx_header_v2_t *imx_header, int index)
{
const char *ivt_name[3] = {"HDMI FW", "PLUGIN", "LOADER IMAGE"};
fprintf(stderr, "========= IVT HEADER [%s] =========\n", ivt_name[index]);
fprintf(stderr, "header.tag: \t\t0x%x\n", imx_header[index].fhdr.header.tag);
fprintf(stderr, "header.length: \t\t0x%x\n", imx_header[index].fhdr.header.length);
fprintf(stderr, "header.version: \t0x%x\n", imx_header[index].fhdr.header.version);
fprintf(stderr, "entry: \t\t\t0x%x\n", imx_header[index].fhdr.entry);
fprintf(stderr, "reserved1: \t\t0x%x\n", imx_header[index].fhdr.reserved1);
fprintf(stderr, "dcd_ptr: \t\t0x%x\n", imx_header[index].fhdr.dcd_ptr);
fprintf(stderr, "boot_data_ptr: \t\t0x%x\n", imx_header[index].fhdr.boot_data_ptr);
fprintf(stderr, "self: \t\t\t0x%x\n", imx_header[index].fhdr.self);
fprintf(stderr, "csf: \t\t\t0x%x\n", imx_header[index].fhdr.csf);
fprintf(stderr, "reserved2: \t\t0x%x\n", imx_header[index].fhdr.reserved2);
fprintf(stderr, "boot_data.start: \t0x%x\n", imx_header[index].boot_data.start);
fprintf(stderr, "boot_data.size: \t0x%x\n", imx_header[index].boot_data.size);
fprintf(stderr, "boot_data.plugin: \t0x%x\n", imx_header[index].boot_data.plugin);
}
void dump_uimage_header(uimage_header_t * uimage_hd_ptr)
{
fprintf(stderr, "========= UIMAGE HEADER =========\n");
fprintf(stderr, "ih_magic: \t\t0x%x\n", be32_to_cpu(uimage_hd_ptr->ih_magic));
fprintf(stderr, "ih_hcrc: \t\t0x%x\n", be32_to_cpu(uimage_hd_ptr->ih_hcrc));
fprintf(stderr, "ih_time: \t\t0x%x\n", be32_to_cpu(uimage_hd_ptr->ih_time));
fprintf(stderr, "ih_size: \t\t0x%x\n", be32_to_cpu(uimage_hd_ptr->ih_size));
fprintf(stderr, "ih_load: \t\t0x%x\n", be32_to_cpu(uimage_hd_ptr->ih_load));
fprintf(stderr, "ih_ep: \t\t\t0x%x\n", be32_to_cpu(uimage_hd_ptr->ih_ep));
fprintf(stderr, "ih_dcrc: \t\t0x%x\n", be32_to_cpu(uimage_hd_ptr->ih_dcrc));
fprintf(stderr, "ih_os: \t\t\t0x%x\n", uimage_hd_ptr->ih_os);
fprintf(stderr, "ih_arch: \t\t0x%x\n", uimage_hd_ptr->ih_arch);
fprintf(stderr, "ih_type: \t\t0x%x\n", uimage_hd_ptr->ih_type);
fprintf(stderr, "ih_comp: \t\t0x%x\n", uimage_hd_ptr->ih_comp);
fprintf(stderr, "ih_name: \t\t%s\n", uimage_hd_ptr->ih_name);
}
void set_uimage_header(uimage_header_t * uimage_hd_ptr, int fd, uint32_t ep)
{
uint32_t checksum;
time_t time;
struct stat sbuf;
void *file_ptr;
if (fstat(fd, &sbuf) < 0) {
fprintf(stderr, "set_uimage_header error: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
time = sbuf.st_mtime;
file_ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (file_ptr == MAP_FAILED) {
fprintf (stderr, "set_uimage_header, File can't read %s\n",
strerror(errno));
exit (EXIT_FAILURE);
}
checksum = crc32(0,
(const unsigned char *)(file_ptr), sbuf.st_size); /* crc for image and ivt, not include CSF and uimage header */
uimage_hd_ptr->ih_magic = cpu_to_be32(IH_MAGIC);
uimage_hd_ptr->ih_time = cpu_to_be32(time);
uimage_hd_ptr->ih_size = cpu_to_be32((sbuf.st_size + 0x2000 - sizeof(flash_header_v2_t))); /* The st_size already contain the flash_header */
uimage_hd_ptr->ih_load = cpu_to_be32(ep);
uimage_hd_ptr->ih_ep = cpu_to_be32(ep);
uimage_hd_ptr->ih_dcrc = cpu_to_be32(checksum);
uimage_hd_ptr->ih_os = IH_OS_U_BOOT;
uimage_hd_ptr->ih_arch = IH_ARCH_ARM;
uimage_hd_ptr->ih_type = IH_TYPE_FIRMWARE;
uimage_hd_ptr->ih_comp = IH_COMP_NONE;
strncpy((char *)uimage_hd_ptr->ih_name, "Second uimage loader", IH_NMLEN);
checksum = crc32(0, (const unsigned char *)uimage_hd_ptr,
sizeof(uimage_header_t));
uimage_hd_ptr->ih_hcrc = cpu_to_be32(checksum);
}
void generate_sld_with_ivt(char * input_file, uint32_t ep, char *out_file)
{
#define IVT_ALIGN 0x1000
struct stat sbuf;
void *file_ptr;
int ivt_fd, input_fd;
int aligned_size;
int i;
char pad = 0;
input_fd = open(input_file, O_RDONLY | O_BINARY);
if (input_fd < 0) {
fprintf(stderr, "%s: Can't open: %s\n",
input_file, strerror(errno));
exit(EXIT_FAILURE);
}
if (fstat(input_fd, &sbuf) < 0) {
fprintf(stderr, "generate_sld_with_ivt error: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
file_ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, input_fd, 0);
if (file_ptr == MAP_FAILED) {
fprintf (stderr, "generate_sld_with_ivt, File can't read %s\n",
strerror(errno));
exit (EXIT_FAILURE);
}
ivt_fd = open (out_file, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
if (ivt_fd < 0) {
fprintf(stderr, "%s: Can't open: %s\n",
"sld-ivt.bin", strerror(errno));
exit(EXIT_FAILURE);
}
if (write(ivt_fd, file_ptr, sbuf.st_size) != sbuf.st_size) {
fprintf(stderr, "error writing sld-ivt image\n");
exit(EXIT_FAILURE);
}
aligned_size = (sbuf.st_size + sizeof(uimage_header_t) + IVT_ALIGN - 1) & ~(IVT_ALIGN - 1);
i = sbuf.st_size + sizeof(uimage_header_t);
for (; i < aligned_size; i++) {
if (write(ivt_fd, (char *) &pad, 1) != 1) {
fprintf(stderr,
"Pad error on sld-ivt image\n");
exit(EXIT_FAILURE);
}
}
flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
ep, 0, 0, 0,
(ep + aligned_size - sizeof(uimage_header_t)),
(ep + aligned_size - sizeof(uimage_header_t) + 0x20),
0 };
if (write(ivt_fd, &ivt_header, sizeof(flash_header_v2_t)) != sizeof(flash_header_v2_t)) {
fprintf(stderr, "IVT writing error on sld-ivt image\n");
exit(EXIT_FAILURE);
}
close(ivt_fd);
close(input_fd);
}
/* Return this IVT offset in the final output file */
int generate_ivt_for_fit(int fd, int fit_offset, uint32_t ep, uint32_t *fit_load_addr)
{
uimage_header_t image_header;
uint32_t fit_size, load_addr;
int align_len = 64 - 1; /* 64 is cacheline size */
lseek(fd, fit_offset, SEEK_SET);
if (read(fd, (char *)&image_header, sizeof(uimage_header_t)) != sizeof(uimage_header_t)) {
fprintf (stderr, "generate_ivt_for_fit read failed: %s\n",
strerror(errno));
exit (EXIT_FAILURE);
}
if (be32_to_cpu(image_header.ih_magic) != FDT_MAGIC){
fprintf (stderr, "generate_ivt_for_fit error: not a FIT file\n");
exit (EXIT_FAILURE);
}
fit_size = fdt_totalsize(&image_header);
fit_size = (fit_size + 3) & ~3;
#define ALIGN_SIZE 0x1000
fit_size = ALIGN(fit_size, ALIGN_SIZE);
lseek(fd, fit_offset + fit_size, SEEK_SET);
/* ep is the u-boot entry. SPL loads the FIT before the u-boot address. 0x2000 is for CSF_SIZE */
load_addr = (ep - (fit_size + CSF_SIZE) - 512 -
align_len) & ~align_len;
flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
load_addr, 0, 0, 0,
(load_addr + fit_size),
(load_addr + fit_size + 0x20),
0 };
if (write(fd, &ivt_header, sizeof(flash_header_v2_t)) != sizeof(flash_header_v2_t)) {
fprintf(stderr, "IVT writing error on fit image\n");
exit(EXIT_FAILURE);
}
*fit_load_addr = load_addr;
return fit_offset + fit_size;
}
int main(int argc, char **argv)
{
int c, file_off, plugin_fd = -1, hdmi_fd = -1, ap_fd = -1, csf_hdmi_fd = -1, csf_fd = -1, ofd = -1, csf_plugin_fd = -1, sld_fd = -1;
unsigned int dcd_size = 0, plugin_start_addr = 0, ap_start_addr = 0, sld_start_addr = 0, sld_src_off = 0, sld_csf_off = 0, sld_load_addr = 0;
char *ofname = NULL, *hdmi_img = NULL, *dcd_img = NULL, *plugin_img = NULL, *ap_img = NULL, *csf_img = NULL, *csf_plugin_img = NULL, *csf_hdmi_img = NULL, *sld_img = NULL;
char *signed_hdmi = NULL;
static imx_header_v2_t imx_header[3]; /* At most there are 3 IVT headers */
uint32_t ivt_offset = IVT_OFFSET_SD;
uint32_t rom_image_offset = IMAGE_OFFSET_SD;
uint32_t sector_size = 0x200;
struct stat sbuf;
uint32_t plugin_off = 0, hdmi_off = 0, image_off = 0, csf_plugin_off = 0, csf_hdmi_off = 0, csf_off = 0;
uint32_t header_hdmi_off = 0, header_hdmi_2_off = 0, header_plugin_off = 0, header_image_off = 0, dcd_off = 0;
uint32_t sld_header_off = 0;
int using_fit = 0;
dcd_v2_t dcd_table;
uimage_header_t uimage_hdr;
static struct option long_options[] =
{
{"loader", required_argument, NULL, 'i'},
{"dcd", required_argument, NULL, 'd'},
{"fit", no_argument, NULL, 'f'},
{"out", required_argument, NULL, 'o'},
{"plugin", required_argument, NULL, 'p'},
{"hdmi", required_argument, NULL, 'h'},
{"signed_hdmi", required_argument, NULL, 's'},
{"csf_plugin", required_argument, NULL, 'q'},
{"csf_hdmi", required_argument, NULL, 'm'},
{"dev", required_argument, NULL, 'e'},
{"csf", required_argument, NULL, 'c'},
{"second_loader", required_argument, NULL, 'u'},
{NULL, 0, NULL, 0}
};
memset((char*)&imx_header, 0, sizeof(imx_header_v2_t) * 3);
fprintf(stderr, "Platform:\ti.MX8M (mScale)\n");
while(1)
{
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long_only (argc, argv, ":i:f:d:o:p:h:q:m:e:b:",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 0:
fprintf(stderr, "option %s", long_options[option_index].name);
if (optarg)
fprintf(stderr, " with arg %s", optarg);
fprintf(stderr, "\n");
break;
case 'd':
fprintf(stderr, "DCD:\t%s\n", optarg);
dcd_img = optarg;
break;
case 'f':
fprintf(stderr, "Using FIT image\n");
using_fit = 1;
break;
case 'p':
fprintf(stderr, "PLUGIN:\t%s", optarg);
plugin_img = optarg;
if (optind < argc && *argv[optind] != '-') {
plugin_start_addr = (uint32_t) strtoll(argv[optind++], NULL, 0);
fprintf(stderr, " start addr: 0x%08x\n", plugin_start_addr);
} else {
fprintf(stderr, "\n-plugin option require TWO arguments: filename, start address in hex\n\n");
exit(1);
}
break;
case 'i':
fprintf(stderr, "LOADER IMAGE:\t%s", optarg);
ap_img = optarg;
if (optind < argc && *argv[optind] != '-') {
ap_start_addr = (uint32_t) strtoll(argv[optind++], NULL, 0);
fprintf(stderr, " start addr: 0x%08x\n", ap_start_addr);
} else {
fprintf(stderr, "\n-loader option require TWO arguments: filename, start address in hex\n\n");
exit(1);
}
break;
case 'h':
fprintf(stderr, "HDMI FW:\t%s\n", optarg); /* Fixed address for HDMI Firmware */
hdmi_img = optarg;
break;
case 's':
fprintf(stderr, "SIGNED HDMI FW:\t%s\n", optarg); /* Fixed address for HDMI Firmware */
signed_hdmi = optarg;
break;
case 'o':
fprintf(stderr, "Output:\t\t%s\n", optarg);
ofname = optarg;
break;
case 'c':
fprintf(stderr, "CSF:\t%s\n", optarg);