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