more verbose logging
[pandora-x-loader.git] / scripts / signGP.c
1 /**
2  * signGP.c - Read the x-load.bin file and write out the x-load.bin.ift file
3  *
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  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2  of
13  * the License as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include <stdio.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <string.h>
30 #include <malloc.h>
31 #include <linux/types.h>
32
33 #undef CH_WITH_CHRAM
34 struct chsettings {
35         __u32 section_key;
36         __u8 valid;
37         __u8 version;
38         __u16 reserved;
39         __u32 flags;
40 } __attribute__ ((__packed__));
41
42 /*    __u32  cm_clksel_core;
43     __u32  reserved1;
44     __u32  cm_autoidle_dpll_mpu;
45     __u32  cm_clksel_dpll_mpu;
46     __u32  cm_div_m2_dpll_mpu;
47     __u32  cm_autoidle_dpll_core;
48     __u32  cm_clksel_dpll_core;
49     __u32  cm_div_m2_dpll_core;
50     __u32  cm_div_m3_dpll_core;
51     __u32  cm_div_m4_dpll_core;
52     __u32  cm_div_m5_dpll_core;
53     __u32  cm_div_m6_dpll_core;
54     __u32  cm_div_m7_dpll_core;
55     __u32  cm_autoidle_dpll_per;
56     __u32  cm_clksel_dpll_per;
57     __u32  cm_div_m2_dpll_per;
58     __u32  cm_div_m3_dpll_per;
59     __u32  cm_div_m4_dpll_per;
60     __u32  cm_div_m5_dpll_per;
61     __u32  cm_div_m6_dpll_per;
62     __u32  cm_div_m7_dpll_per;
63     __u32  cm_autoidle_dpll_usb;
64     __u32  cm_clksel_dpll_usb;
65     __u32  cm_div_m2_dpll_usb;
66 }*/
67
68 struct gp_header {
69         __u32 size;
70         __u32 load_addr;
71 } __attribute__ ((__packed__));
72
73 struct ch_toc {
74         __u32 section_offset;
75         __u32 section_size;
76         __u8 unused[12];
77         __u8 section_name[12];
78 } __attribute__ ((__packed__));
79
80 struct chram {
81         /* CHRAM */
82         __u32 section_key_chr;
83         __u8 section_disable_chr;
84         __u8 pad_chr[3];
85         /* EMIF1 */
86         __u32 config_emif1;
87         __u32 refresh_emif1;
88         __u32 tim1_emif1;
89         __u32 tim2_emif1;
90         __u32 tim3_emif1;
91         __u32 pwrControl_emif1;
92         __u32 phy_cntr1_emif1;
93         __u32 phy_cntr2_emif1;
94         __u8 modereg1_emif1;
95         __u8 modereg2_emif1;
96         __u8 modereg3_emif1;
97         __u8 pad_emif1;
98         /* EMIF2 */
99         __u32 config_emif2;
100         __u32 refresh_emif2;
101         __u32 tim1_emif2;
102         __u32 tim2_emif2;
103         __u32 tim3_emif2;
104         __u32 pwrControl_emif2;
105         __u32 phy_cntr1_emif2;
106         __u32 phy_cntr2_emif2;
107         __u8 modereg1_emif2;
108         __u8 modereg2_emif2;
109         __u8 modereg3_emif2;
110         __u8 pad_emif2;
111
112         __u32 dmm_lisa_map;
113         __u8 flags;
114         __u8 pad[3];
115 } __attribute__ ((__packed__));
116
117
118 struct ch_chsettings_chram {
119         struct ch_toc toc_chsettings;
120         struct ch_toc toc_chram;
121         struct ch_toc toc_terminator;
122         struct chsettings section_chsettings;
123         struct chram section_chram;
124         __u8 padding1[512 -
125                     (sizeof(struct ch_toc) * 3 +
126                      sizeof(struct chsettings) + sizeof(struct chram))];
127         /* struct gp_header gpheader; */
128 } __attribute__ ((__packed__));
129
130 struct ch_chsettings_nochram {
131         struct ch_toc toc_chsettings;
132         struct ch_toc toc_terminator;
133         struct chsettings section_chsettings;
134         __u8 padding1[512 -
135                     (sizeof(struct ch_toc) * 2 +
136                      sizeof(struct chsettings))];
137         /* struct gp_header gpheader; */
138 } __attribute__ ((__packed__));
139
140
141 #ifdef CH_WITH_CHRAM
142 static const struct ch_chsettings_chram config_header = {
143         /* CHSETTINGS TOC */
144         {sizeof(struct ch_toc) * 4,
145          sizeof(struct chsettings),
146          "",
147          {"CHSETTINGS"}
148          },
149         /* CHRAM TOC */
150         {sizeof(struct ch_toc) * 4 + sizeof(struct chsettings),
151          sizeof(struct chram),
152          "",
153          {"CHRAM"}
154          },
155         /* toc terminator */
156         {0xFFFFFFFF,
157          0xFFFFFFFF,
158          {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
159           0xFF},
160          {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
161           0xFF}
162          },
163         /* CHSETTINGS section */
164         {
165          0xC0C0C0C1,
166          0,
167          1,
168          0,
169          0},
170         /* CHRAM section */
171         {
172          0xc0c0c0c2,
173          0x01,
174          {0x00, 0x00, 0x00},
175
176          /* EMIF1 */
177          0x80800eb2,
178          0x00000010,
179          0x110d1624,
180          0x3058161b,
181          0x030060b2,
182          0x00000200,
183          0x901ff416,
184          0x00000000,
185          0x23,
186          0x01,
187          0x02,
188          0x00,
189
190          /* EMIF2 */
191          0x80800eb2,
192          0x000002ba,
193          0x110d1624,
194          0x3058161b,
195          0x03006542,
196          0x00000200,
197          0x901ff416,
198          0x00000000,
199          0x23,
200          0x01,
201          0x02,
202          0x00,
203
204          /* LISA map */
205          0x80700100,
206          0x05,
207          {0x00, 0x00, 0x00},
208          },
209         ""
210 };
211 #else
212 static struct ch_chsettings_nochram config_header
213         __attribute__((section(".config_header"))) = {
214         /* CHSETTINGS TOC */
215         {(sizeof(struct ch_toc)) * 2,
216          sizeof(struct chsettings),
217          "",
218          {"CHSETTINGS"}
219          },
220         /* toc terminator */
221         {0xFFFFFFFF,
222          0xFFFFFFFF,
223          {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
224           0xFF},
225          {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
226           0xFF}
227          },
228         /* CHSETTINGS section */
229         {
230          0xC0C0C0C1,
231          0,
232          1,
233          0,
234          0},
235         ""
236 };
237 #endif
238
239
240 #define err(...) do { int save_errno = errno; \
241                       fprintf(stderr, __VA_ARGS__); \
242                       errno = save_errno; \
243                     } while (0);
244 #define pdie(func, ...) do { perror(func); exit(1); } while (0);
245
246 int main(int argc, char *argv[])
247 {
248         int     i;
249         char    ifname[FILENAME_MAX], ofname[FILENAME_MAX], ch;
250         FILE    *ifile, *ofile;
251         unsigned long   loadaddr, len;
252         struct stat     sinfo;
253         int ch_add = 0;
254
255
256         /* Default to x-load.bin and 0x40200800. */
257         strcpy(ifname, "x-load.bin");
258         loadaddr = 0x40200800;
259
260         if ((argc == 2) || (argc == 3) || (argc == 4))
261                 strcpy(ifname, argv[1]);
262
263         if ((argc == 3) || (argc == 4))
264                 loadaddr = strtoul(argv[2], NULL, 16);
265
266         if (argc == 4)
267                 ch_add = strtoul(argv[3], NULL, 16);
268
269         /* Form the output file name. */
270         strcpy(ofname, ifname);
271         strcat(ofname, ".ift");
272
273         /* Open the input file. */
274         ifile = fopen(ifname, "rb");
275         if (ifile == NULL) {
276                 err("Cannot open %s\n", ifname);
277                 pdie("fopen");
278         }
279
280         /* Get file length. */
281         stat(ifname, &sinfo);
282         len = sinfo.st_size;
283
284         /* Open the output file and write it. */
285         ofile = fopen(ofname, "wb");
286         if (ofile == NULL) {
287                 fclose(ifile);
288                 err("Cannot open %s\n", ofname);
289                 pdie("fopen");
290         }
291
292         if (ch_add)
293                 if (fwrite(&config_header, 1, 512, ofile) <= 0)
294                         pdie("fwrite");
295
296         if (fwrite(&len, 1, 4, ofile) <= 0)
297                 pdie("fwrite");
298         if (fwrite(&loadaddr, 1, 4, ofile) <= 0)
299                 pdie("fwrite");
300         for (i = 0; i < len; i++) {
301                 if (fread(&ch, 1, 1, ifile) <= 0)
302                     pdie("fread");
303                 if (fwrite(&ch, 1, 1, ofile) <= 0)
304                     pdie("fwrite");
305         }
306
307         if (!fclose(ifile))
308                 perror("warning: fclose");
309         if (!fclose(ofile))
310                 perror("warning: fclose");
311
312         return 0;
313 }