]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - tools/aisimage.c
6a1011194a5a5eea9dabad34d6b5bde608237989
[karo-tx-uboot.git] / tools / aisimage.c
1 /*
2  * (C) Copyright 2011
3  * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /* Required to obtain the getline prototype from stdio.h */
25 #define _GNU_SOURCE
26
27 #include "mkimage.h"
28 #include "aisimage.h"
29 #include <image.h>
30
31 #define IS_FNC_EXEC(c)  (cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD)
32 #define WORD_ALIGN0     4
33 #define WORD_ALIGN(len) (((len)+WORD_ALIGN0-1) & ~(WORD_ALIGN0-1))
34 #define MAX_CMD_BUFFER  4096
35 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
36
37 static uint32_t ais_img_size;
38
39 /*
40  * Supported commands for configuration file
41  */
42 static table_entry_t aisimage_cmds[] = {
43         {CMD_DATA,              "DATA",         "Reg Write Data"},
44         {CMD_FILL,              "FILL",         "Fill range with pattern"},
45         {CMD_CRCON,             "CRCON",        "CRC Enable"},
46         {CMD_CRCOFF,            "CRCOFF",       "CRC Disable"},
47         {CMD_CRCCHECK,          "CRCCHECK",     "CRC Validate"},
48         {CMD_JMPCLOSE,          "JMPCLOSE",     "Jump & Close"},
49         {CMD_JMP,               "JMP",          "Jump"},
50         {CMD_SEQREAD,           "SEQREAD",      "Sequential read"},
51         {CMD_PLL0,              "PLL0",         "PLL0"},
52         {CMD_PLL1,              "PLL1",         "PLL1"},
53         {CMD_CLK,               "CLK",          "Clock configuration"},
54         {CMD_DDR2,              "DDR2",         "DDR2 Configuration"},
55         {CMD_EMIFA,             "EMIFA",        "EMIFA"},
56         {CMD_EMIFA_ASYNC,       "EMIFA_ASYNC",  "EMIFA Async"},
57         {CMD_PLL,               "PLL",          "PLL & Clock configuration"},
58         {CMD_PSC,               "PSC",          "PSC setup"},
59         {CMD_PINMUX,            "PINMUX",       "Pinmux setup"},
60         {CMD_BOOTTABLE,         "BOOT_TABLE",   "Boot table command"},
61         {-1,                    "",             ""},
62 };
63
64 static struct ais_func_exec {
65         uint32_t index;
66         uint32_t argcnt;
67 } ais_func_table[] = {
68         [CMD_PLL0] = {0, 2},
69         [CMD_PLL1] = {1, 2},
70         [CMD_CLK] = {2, 1},
71         [CMD_DDR2] = {3, 8},
72         [CMD_EMIFA] = {4, 5},
73         [CMD_EMIFA_ASYNC] = {5, 5},
74         [CMD_PLL] = {6, 3},
75         [CMD_PSC] = {7, 1},
76         [CMD_PINMUX] = {8, 3}
77 };
78
79 static struct cmd_table_t {
80         uint32_t nargs;
81         uint32_t AIS_cmd;
82 } cmd_table[] = {
83         [CMD_FILL]       =      {       4,      AIS_CMD_FILL},
84         [CMD_CRCON]     =       {       0,      AIS_CMD_ENCRC},
85         [CMD_CRCOFF]    =       {       0,      AIS_CMD_DISCRC},
86         [CMD_CRCCHECK]  =       {       2,      AIS_CMD_ENCRC},
87         [CMD_JMPCLOSE]  =       {       1,      AIS_CMD_JMPCLOSE},
88         [CMD_JMP]       =       {       1,      AIS_CMD_JMP},
89         [CMD_SEQREAD]   =       {       0,      AIS_CMD_SEQREAD},
90         [CMD_PLL0]      =       {       2,      AIS_CMD_FNLOAD},
91         [CMD_PLL1]      =       {       2,      AIS_CMD_FNLOAD},
92         [CMD_CLK]       =       {       1,      AIS_CMD_FNLOAD},
93         [CMD_DDR2]      =       {       8,      AIS_CMD_FNLOAD},
94         [CMD_EMIFA]     =       {       5,      AIS_CMD_FNLOAD},
95         [CMD_EMIFA_ASYNC] =     {       5,      AIS_CMD_FNLOAD},
96         [CMD_PLL]       =       {       3,      AIS_CMD_FNLOAD},
97         [CMD_PSC]       =       {       1,      AIS_CMD_FNLOAD},
98         [CMD_PINMUX]    =       {       3,      AIS_CMD_FNLOAD},
99         [CMD_BOOTTABLE] =       {       4,      AIS_CMD_BOOTTBL},
100 };
101
102 static uint32_t get_cfg_value(char *token, char *name,  int linenr)
103 {
104         char *endptr;
105         uint32_t value;
106
107         errno = 0;
108         value = strtoul(token, &endptr, 16);
109         if (errno || (token == endptr)) {
110                 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
111                         name,  linenr, token);
112                 exit(EXIT_FAILURE);
113         }
114         return value;
115 }
116
117 static int get_ais_table_id(uint32_t *ptr)
118 {
119
120         int i;
121         int func_no;
122
123         for (i = 0; i < ARRAY_SIZE(cmd_table); i++) {
124                 if (*ptr == cmd_table[i].AIS_cmd) {
125                         if (cmd_table[i].AIS_cmd != AIS_CMD_FNLOAD)
126                                 return i;
127
128                         func_no = ((struct ais_cmd_func *)ptr)->func_args
129                                 & 0xFFFF;
130                         if (func_no == ais_func_table[i].index)
131                                 return i;
132                 }
133         }
134
135         return -1;
136 }
137
138 static void aisimage_print_header(const void *hdr)
139 {
140         struct ais_header *ais_hdr = (struct ais_header *)hdr;
141         uint32_t *ptr;
142         struct ais_cmd_load *ais_load;
143         int id;
144
145         if (ais_hdr->magic != AIS_MAGIC_WORD) {
146                 fprintf(stderr, "Error: - AIS Magic Number not found\n");
147                 return;
148         }
149         fprintf(stdout, "Image Type:   TI Davinci AIS Boot Image\n");
150         fprintf(stdout, "AIS magic :   %08x\n", ais_hdr->magic);
151         ptr = (uint32_t *)&ais_hdr->magic;
152         ptr++;
153
154         while (*ptr != AIS_CMD_JMPCLOSE) {
155                 /* Check if we find the image */
156                 if (*ptr == AIS_CMD_LOAD) {
157                         ais_load = (struct ais_cmd_load *)ptr;
158                         fprintf(stdout, "Image at  :   0x%08x size 0x%08x\n",
159                                 ais_load->addr,
160                                 ais_load->size);
161                         ptr = ais_load->data + ais_load->size / sizeof(*ptr);
162                         continue;
163                 }
164
165                 id = get_ais_table_id(ptr);
166                 if (id < 0) {
167                         fprintf(stderr, "Error: -  AIS Image corrupted\n");
168                         return;
169                 }
170                 fprintf(stdout, "AIS cmd   :   %s\n",
171                         get_table_entry_name(aisimage_cmds, NULL, id));
172                 ptr += cmd_table[id].nargs + IS_FNC_EXEC(id) + 1;
173                 if (((void *)ptr - hdr) > ais_img_size) {
174                         fprintf(stderr,
175                                 "AIS Image not terminated by JMPCLOSE\n");
176                         return;
177                 }
178         }
179 }
180
181 static uint32_t *ais_insert_cmd_header(uint32_t cmd, uint32_t nargs,
182         uint32_t *parms, struct image_type_params *tparams,
183         uint32_t *ptr, uint32_t size)
184 {
185         int i;
186
187         *ptr++ = cmd_table[cmd].AIS_cmd;
188         if (IS_FNC_EXEC(cmd))
189                 *ptr++ = ((nargs & 0xFFFF) << 16) + ais_func_table[cmd].index;
190
191         /* Copy parameters */
192         for (i = 0; i < nargs; i++)
193                 *ptr++ = cpu_to_le32(parms[i]);
194
195         return ptr;
196
197 }
198
199 static uint32_t *ais_alloc_buffer(struct mkimage_params *params)
200 {
201         int dfd;
202         struct stat sbuf;
203         char *datafile = params->datafile;
204         uint32_t *ptr;
205
206         dfd = open(datafile, O_RDONLY|O_BINARY);
207         if (dfd < 0) {
208                 fprintf(stderr, "%s: Can't open %s: %s\n",
209                         params->cmdname, datafile, strerror(errno));
210                 exit(EXIT_FAILURE);
211         }
212
213         if (fstat(dfd, &sbuf) < 0) {
214                 fprintf(stderr, "%s: Can't stat %s: %s\n",
215                         params->cmdname, datafile, strerror(errno));
216                 exit(EXIT_FAILURE);
217         }
218
219         /*
220          * Place for header is allocated. The size is taken from
221          * the size of the datafile, that the ais_image_generate()
222          * will copy into the header. Copying the datafile
223          * is not left to the main program, because after the datafile
224          * the header must be terminated with the Jump & Close command.
225          */
226         ais_img_size = WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER;
227         ptr = (uint32_t *)malloc(WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER);
228         if (!ptr) {
229                 fprintf(stderr, "%s: malloc return failure: %s\n",
230                         params->cmdname, strerror(errno));
231                 exit(EXIT_FAILURE);
232         }
233
234         close(dfd);
235
236         return ptr;
237 }
238
239 static uint32_t *ais_copy_image(struct mkimage_params *params,
240         uint32_t *aisptr)
241
242 {
243         int dfd;
244         struct stat sbuf;
245         char *datafile = params->datafile;
246         void *ptr;
247
248         dfd = open(datafile, O_RDONLY|O_BINARY);
249         if (dfd < 0) {
250                 fprintf(stderr, "%s: Can't open %s: %s\n",
251                         params->cmdname, datafile, strerror(errno));
252                 exit(EXIT_FAILURE);
253         }
254
255         if (fstat(dfd, &sbuf) < 0) {
256                 fprintf(stderr, "%s: Can't stat %s: %s\n",
257                         params->cmdname, datafile, strerror(errno));
258                 exit(EXIT_FAILURE);
259         }
260
261         ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
262         *aisptr++ = AIS_CMD_LOAD;
263         *aisptr++ = params->ep;
264         *aisptr++ = sbuf.st_size;
265         memcpy((void *)aisptr, ptr, sbuf.st_size);
266         aisptr += WORD_ALIGN(sbuf.st_size) / sizeof(uint32_t);
267
268         (void) munmap((void *)ptr, sbuf.st_size);
269         (void) close(dfd);
270
271         return aisptr;
272
273 }
274
275 static int aisimage_generate(struct mkimage_params *params,
276         struct image_type_params *tparams)
277 {
278         FILE *fd = NULL;
279         char *line = NULL;
280         char *token, *saveptr1, *saveptr2;
281         int lineno = 0;
282         int fld;
283         size_t len;
284         int32_t cmd;
285         uint32_t nargs, cmd_parms[10];
286         uint32_t value, size;
287         char *name = params->imagename;
288         uint32_t *aishdr, tsize;
289
290         fd = fopen(name, "r");
291         if (fd == 0) {
292                 fprintf(stderr,
293                         "Error: %s - Can't open AIS configuration\n", name);
294                 exit(EXIT_FAILURE);
295         }
296
297         /*
298          * the size of the header is variable and is computed
299          * scanning the configuration file.
300          */
301         tparams->header_size = 0;
302
303         /*
304          * Start allocating a buffer suitable for most command
305          * The buffer is then reallocated if it is too small
306          */
307         aishdr = ais_alloc_buffer(params);
308         tparams->hdr = aishdr;
309         *aishdr++ = AIS_MAGIC_WORD;
310
311         /* Very simple parsing, line starting with # are comments
312          * and are dropped
313          */
314         while ((getline(&line, &len, fd)) > 0) {
315                 lineno++;
316
317                 token = strtok_r(line, "\r\n", &saveptr1);
318                 if (token == NULL)
319                         continue;
320
321                 /* Check inside the single line */
322                 line = token;
323                 fld = CFG_COMMAND;
324                 cmd = CMD_INVALID;
325                 nargs = 0;
326                 while (token != NULL) {
327                         token = strtok_r(line, " \t", &saveptr2);
328                         if (token == NULL)
329                                 break;
330
331                         /* Drop all text starting with '#' as comments */
332                         if (token[0] == '#')
333                                 break;
334
335                         switch (fld) {
336                         case CFG_COMMAND:
337                                 cmd = get_table_entry_id(aisimage_cmds,
338                                         "aisimage commands", token);
339                                 if (cmd < 0) {
340                                         fprintf(stderr,
341                                         "Error: %s[%d] - Invalid command"
342                                         "(%s)\n", name, lineno, token);
343
344                                         exit(EXIT_FAILURE);
345                                 }
346                                 break;
347                         case CFG_VALUE:
348                                 value = get_cfg_value(token, name, lineno);
349                                 cmd_parms[nargs++] = value;
350                                 if (nargs > cmd_table[cmd].nargs) {
351                                         fprintf(stderr,
352                                          "Error: %s[%d] - too much arguments:"
353                                                 "(%s) for command %s\n", name,
354                                                 lineno, token,
355                                                 aisimage_cmds[cmd].sname);
356                                         exit(EXIT_FAILURE);
357                                 }
358                                 break;
359                         }
360                         line = NULL;
361                         fld = CFG_VALUE;
362                 }
363                 if (cmd != CMD_INVALID) {
364                         /* Now insert the command into the header */
365                         aishdr = ais_insert_cmd_header(cmd, nargs, cmd_parms,
366                                 tparams, aishdr, tsize);
367                 }
368
369         }
370         fclose(fd);
371
372         aishdr = ais_copy_image(params, aishdr);
373
374         /* Add Jmp & Close */
375         *aishdr++ = AIS_CMD_JMPCLOSE;
376         *aishdr++ = params->ep;
377
378         size = (aishdr - (uint32_t *)tparams->hdr) * sizeof(uint32_t);
379         tparams->header_size = size;
380
381         return 0;
382 }
383
384 static int aisimage_check_image_types(uint8_t type)
385 {
386         if (type == IH_TYPE_AISIMAGE)
387                 return EXIT_SUCCESS;
388         else
389                 return EXIT_FAILURE;
390 }
391
392 static int aisimage_verify_header(unsigned char *ptr, int image_size,
393                         struct mkimage_params *params)
394 {
395         struct ais_header *ais_hdr = (struct ais_header *)ptr;
396
397         if (ais_hdr->magic != AIS_MAGIC_WORD)
398                 return -FDT_ERR_BADSTRUCTURE;
399
400         /* Store the total size to remember in print_hdr */
401         ais_img_size = image_size;
402
403         return 0;
404 }
405
406 static void aisimage_set_header(void *ptr, struct stat *sbuf, int ifd,
407                                 struct mkimage_params *params)
408 {
409 }
410
411 int aisimage_check_params(struct mkimage_params *params)
412 {
413         if (!params)
414                 return CFG_INVALID;
415         if (!strlen(params->imagename)) {
416                 fprintf(stderr, "Error: %s - Configuration file not specified, "
417                         "it is needed for aisimage generation\n",
418                         params->cmdname);
419                 return CFG_INVALID;
420         }
421         /*
422          * Check parameters:
423          * XIP is not allowed and verify that incompatible
424          * parameters are not sent at the same time
425          * For example, if list is required a data image must not be provided
426          */
427         return  (params->dflag && (params->fflag || params->lflag)) ||
428                 (params->fflag && (params->dflag || params->lflag)) ||
429                 (params->lflag && (params->dflag || params->fflag)) ||
430                 (params->xflag) || !(strlen(params->imagename));
431 }
432
433 /*
434  * aisimage parameters
435  */
436 static struct image_type_params aisimage_params = {
437         .name           = "TI Davinci AIS Boot Image support",
438         .header_size    = 0,
439         .hdr            = NULL,
440         .check_image_type = aisimage_check_image_types,
441         .verify_header  = aisimage_verify_header,
442         .print_header   = aisimage_print_header,
443         .set_header     = aisimage_set_header,
444         .check_params   = aisimage_check_params,
445         .vrec_header    = aisimage_generate,
446 };
447
448 void init_ais_image_type(void)
449 {
450         mkimage_register(&aisimage_params);
451 }