SignGP: add CH support
[x-loader-sniper.git] / scripts / signGP.c
1 //
2 // signGP.c
3 // Read the x-load.bin file and write out the x-load.bin.ift file.
4 // The signed image is the original pre-pended with the size of the image
5 // and the load address.  If not entered on command line, file name is
6 // assumed to be x-load.bin in current directory and load address is
7 // 0x40200800.
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <fcntl.h>
12 #include <sys/stat.h>
13 #include <string.h>
14 #include <malloc.h>
15 #include <linux/types.h>
16
17 #undef CH_WITH_CHRAM
18 struct chsettings {
19         __u32 section_key;
20         __u8 valid;
21         __u8 version;
22         __u16 reserved;
23         __u32 flags;
24 } __attribute__ ((__packed__));
25
26 /*    __u32  cm_clksel_core;
27     __u32  reserved1;
28     __u32  cm_autoidle_dpll_mpu;
29     __u32  cm_clksel_dpll_mpu;
30     __u32  cm_div_m2_dpll_mpu;
31     __u32  cm_autoidle_dpll_core;
32     __u32  cm_clksel_dpll_core;
33     __u32  cm_div_m2_dpll_core;
34     __u32  cm_div_m3_dpll_core;
35     __u32  cm_div_m4_dpll_core;
36     __u32  cm_div_m5_dpll_core;
37     __u32  cm_div_m6_dpll_core;
38     __u32  cm_div_m7_dpll_core;
39     __u32  cm_autoidle_dpll_per;
40     __u32  cm_clksel_dpll_per;
41     __u32  cm_div_m2_dpll_per;
42     __u32  cm_div_m3_dpll_per;
43     __u32  cm_div_m4_dpll_per;
44     __u32  cm_div_m5_dpll_per;
45     __u32  cm_div_m6_dpll_per;
46     __u32  cm_div_m7_dpll_per;
47     __u32  cm_autoidle_dpll_usb;
48     __u32  cm_clksel_dpll_usb;
49     __u32  cm_div_m2_dpll_usb;
50 }*/
51
52 struct gp_header {
53         __u32 size;
54         __u32 load_addr;
55 } __attribute__ ((__packed__));
56
57 struct ch_toc {
58         __u32 section_offset;
59         __u32 section_size;
60         __u8 unused[12];
61         __u8 section_name[12];
62 } __attribute__ ((__packed__));
63
64 struct chram {
65         /*CHRAM */
66         __u32 section_key_chr;
67         __u8 section_disable_chr;
68         __u8 pad_chr[3];
69         /*EMIF1 */
70         __u32 config_emif1;
71         __u32 refresh_emif1;
72         __u32 tim1_emif1;
73         __u32 tim2_emif1;
74         __u32 tim3_emif1;
75         __u32 pwrControl_emif1;
76         __u32 phy_cntr1_emif1;
77         __u32 phy_cntr2_emif1;
78         __u8 modereg1_emif1;
79         __u8 modereg2_emif1;
80         __u8 modereg3_emif1;
81         __u8 pad_emif1;
82         /*EMIF2 */
83         __u32 config_emif2;
84         __u32 refresh_emif2;
85         __u32 tim1_emif2;
86         __u32 tim2_emif2;
87         __u32 tim3_emif2;
88         __u32 pwrControl_emif2;
89         __u32 phy_cntr1_emif2;
90         __u32 phy_cntr2_emif2;
91         __u8 modereg1_emif2;
92         __u8 modereg2_emif2;
93         __u8 modereg3_emif2;
94         __u8 pad_emif2;
95
96         __u32 dmm_lisa_map;
97         __u8 flags;
98         __u8 pad[3];
99 } __attribute__ ((__packed__));
100
101
102 struct ch_chsettings_chram {
103         struct ch_toc toc_chsettings;
104         struct ch_toc toc_chram;
105         struct ch_toc toc_terminator;
106         struct chsettings section_chsettings;
107         struct chram section_chram;
108         __u8 padding1[512 -
109                     (sizeof(struct ch_toc) * 3 +
110                      sizeof(struct chsettings) + sizeof(struct chram))];
111         //struct gp_header gpheader;
112 } __attribute__ ((__packed__));
113
114 struct ch_chsettings_nochram {
115         struct ch_toc toc_chsettings;
116         struct ch_toc toc_terminator;
117         struct chsettings section_chsettings;
118         __u8 padding1[512 -
119                     (sizeof(struct ch_toc) * 2 +
120                      sizeof(struct chsettings))];
121         //struct gp_header gpheader;
122 } __attribute__ ((__packed__));
123
124
125 #ifdef CH_WITH_CHRAM
126 const struct ch_chsettings_chram config_header = {
127         //CHSETTINGS TOC
128         {sizeof(struct ch_toc) * 4,
129          sizeof(struct chsettings),
130          "",
131          {"CHSETTINGS"}
132          },
133         //CHRAM TOC
134         {sizeof(struct ch_toc) * 4 + sizeof(struct chsettings),
135          sizeof(struct chram),
136          "",
137          {"CHRAM"}
138          },
139         // toc terminator
140         {0xFFFFFFFF,
141          0xFFFFFFFF,
142          {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
143           0xFF},
144          {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
145           0xFF}
146          },
147         //CHSETTINGS section
148         {
149          0xC0C0C0C1,
150          0,
151          1,
152          0,
153          0},
154         //CHRAM section
155         {
156          0xc0c0c0c2,
157          0x01,
158          {0x00, 0x00, 0x00},
159
160          /*EMIF1 */
161          0x80800eb2,
162          0x00000010,
163          0x110d1624,
164          0x3058161b,
165          0x030060b2,
166          0x00000200,
167          0x901ff416,
168          0x00000000,
169          0x23,
170          0x01,
171          0x02,
172          0x00,
173
174          /*EMIF2 */
175          0x80800eb2,
176          0x000002ba,
177          0x110d1624,
178          0x3058161b,
179          0x03006542,
180          0x00000200,
181          0x901ff416,
182          0x00000000,
183          0x23,
184          0x01,
185          0x02,
186          0x00,
187
188          /* LISA map */
189          0x80700100,
190          0x05,
191          {0x00, 0x00, 0x00},
192          },
193         ""
194 };
195 #else
196 struct ch_chsettings_nochram config_header  __attribute__((section(".config_header"))) = {
197         //CHSETTINGS TOC
198         {(sizeof(struct ch_toc)) * 2,
199          sizeof(struct chsettings),
200          "",
201          {"CHSETTINGS"}
202          },
203         // toc terminator
204         {0xFFFFFFFF,
205          0xFFFFFFFF,
206          {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
207           0xFF},
208          {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
209           0xFF}
210          },
211         //CHSETTINGS section
212         {
213          0xC0C0C0C1,
214          0,
215          1,
216          0,
217          0},
218         ""
219 };
220 #endif
221
222
223 main(int argc, char *argv[])
224 {
225         int     i;
226         char    ifname[FILENAME_MAX], ofname[FILENAME_MAX], ch;
227         FILE    *ifile, *ofile;
228         unsigned long   loadaddr, len;
229         struct stat     sinfo;
230
231
232         // Default to x-load.bin and 0x40200800.
233         strcpy(ifname, "x-load.bin");
234         loadaddr = 0x40200800;
235
236         if ((argc == 2) || (argc == 3))
237                 strcpy(ifname, argv[1]);
238
239         if (argc == 3)
240                 loadaddr = strtoul(argv[2], NULL, 16);
241
242         // Form the output file name.
243         strcpy(ofname, ifname);
244         strcat(ofname, ".ift");
245
246         // Open the input file.
247         ifile = fopen(ifname, "rb");
248         if (ifile == NULL) {
249                 printf("Cannot open %s\n", ifname);
250                 exit(0);
251         }
252
253         // Get file length.
254         stat(ifname, &sinfo);
255         len = sinfo.st_size;
256
257         // Open the output file and write it.
258         ofile = fopen(ofname, "wb");
259         if (ofile == NULL) {
260                 printf("Cannot open %s\n", ofname);
261                 fclose(ifile);
262                 exit(0);
263         }
264
265         // Pad 1 sector of zeroes.
266         //ch = 0x00;
267         //for (i=0; i<0x200; i++)
268         //      fwrite(&ch, 1, 1, ofile);
269
270         fwrite(&config_header, 1, 512, ofile);
271         fwrite(&len, 1, 4, ofile);
272         fwrite(&loadaddr, 1, 4, ofile);
273         for (i=0; i<len; i++) {
274                 fread(&ch, 1, 1, ifile);
275                 fwrite(&ch, 1, 1, ofile);
276         }
277
278         fclose(ifile);
279         fclose(ofile);
280 }