imximage: check dcd_len as entries added
[kernel/u-boot.git] / tools / imximage.c
1 /*
2  * (C) Copyright 2009
3  * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
4  *
5  * (C) Copyright 2008
6  * Marvell Semiconductor <www.marvell.com>
7  * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 /* Required to obtain the getline prototype from stdio.h */
29 #define _GNU_SOURCE
30
31 #include "mkimage.h"
32 #include <image.h>
33 #include "imximage.h"
34
35 /*
36  * Supported commands for configuration file
37  */
38 static table_entry_t imximage_cmds[] = {
39         {CMD_BOOT_FROM,         "BOOT_FROM",            "boot command",   },
40         {CMD_DATA,              "DATA",                 "Reg Write Data", },
41         {CMD_IMAGE_VERSION,     "IMAGE_VERSION",        "image version",  },
42         {-1,                    "",                     "",               },
43 };
44
45 /*
46  * Supported Boot options for configuration file
47  * this is needed to set the correct flash offset
48  */
49 static table_entry_t imximage_bootops[] = {
50         {FLASH_OFFSET_ONENAND,  "onenand",      "OneNAND Flash",},
51         {FLASH_OFFSET_NAND,     "nand",         "NAND Flash",   },
52         {FLASH_OFFSET_NOR,      "nor",          "NOR Flash",    },
53         {FLASH_OFFSET_SATA,     "sata",         "SATA Disk",    },
54         {FLASH_OFFSET_SD,       "sd",           "SD Card",      },
55         {FLASH_OFFSET_SPI,      "spi",          "SPI Flash",    },
56         {-1,                    "",             "Invalid",      },
57 };
58
59 /*
60  * IMXIMAGE version definition for i.MX chips
61  */
62 static table_entry_t imximage_versions[] = {
63         {IMXIMAGE_V1,   "",     " (i.MX25/35/51 compatible)", },
64         {IMXIMAGE_V2,   "",     " (i.MX53/6 compatible)",     },
65         {-1,            "",     " (Invalid)",                 },
66 };
67
68 static struct imx_header imximage_header;
69 static uint32_t imximage_version;
70
71 static set_dcd_val_t set_dcd_val;
72 static set_dcd_rst_t set_dcd_rst;
73 static set_imx_hdr_t set_imx_hdr;
74 static uint32_t max_dcd_entries;
75
76 static uint32_t get_cfg_value(char *token, char *name,  int linenr)
77 {
78         char *endptr;
79         uint32_t value;
80
81         errno = 0;
82         value = strtoul(token, &endptr, 16);
83         if (errno || (token == endptr)) {
84                 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
85                         name,  linenr, token);
86                 exit(EXIT_FAILURE);
87         }
88         return value;
89 }
90
91 static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
92 {
93         imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
94         imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
95         flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
96         flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
97
98         /* Try to detect V1 */
99         if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) &&
100                 (hdr_v1->dcd_table.preamble.barker == DCD_BARKER))
101                 return IMXIMAGE_V1;
102
103         /* Try to detect V2 */
104         if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
105                 (hdr_v2->dcd_table.header.tag == DCD_HEADER_TAG))
106                 return IMXIMAGE_V2;
107
108         return IMXIMAGE_VER_INVALID;
109 }
110
111 static void err_imximage_version(int version)
112 {
113         fprintf(stderr,
114                 "Error: Unsupported imximage version:%d\n", version);
115
116         exit(EXIT_FAILURE);
117 }
118
119 static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno,
120                                         int fld, uint32_t value, uint32_t off)
121 {
122         dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
123
124         switch (fld) {
125         case CFG_REG_SIZE:
126                 /* Byte, halfword, word */
127                 if ((value != 1) && (value != 2) && (value != 4)) {
128                         fprintf(stderr, "Error: %s[%d] - "
129                                 "Invalid register size " "(%d)\n",
130                                 name, lineno, value);
131                         exit(EXIT_FAILURE);
132                 }
133                 dcd_v1->addr_data[off].type = value;
134                 break;
135         case CFG_REG_ADDRESS:
136                 dcd_v1->addr_data[off].addr = value;
137                 break;
138         case CFG_REG_VALUE:
139                 dcd_v1->addr_data[off].value = value;
140                 break;
141         default:
142                 break;
143
144         }
145 }
146
147 static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno,
148                                         int fld, uint32_t value, uint32_t off)
149 {
150         dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
151
152         switch (fld) {
153         case CFG_REG_ADDRESS:
154                 dcd_v2->addr_data[off].addr = cpu_to_be32(value);
155                 break;
156         case CFG_REG_VALUE:
157                 dcd_v2->addr_data[off].value = cpu_to_be32(value);
158                 break;
159         default:
160                 break;
161
162         }
163 }
164
165 /*
166  * Complete setting up the rest field of DCD of V1
167  * such as barker code and DCD data length.
168  */
169 static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len,
170                                                 char *name, int lineno)
171 {
172         dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
173
174         dcd_v1->preamble.barker = DCD_BARKER;
175         dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
176 }
177
178 /*
179  * Complete setting up the reset field of DCD of V2
180  * such as DCD tag, version, length, etc.
181  */
182 static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len,
183                                                 char *name, int lineno)
184 {
185         dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
186
187         dcd_v2->header.tag = DCD_HEADER_TAG;
188         dcd_v2->header.length = cpu_to_be16(
189                         dcd_len * sizeof(dcd_addr_data_t) + 8);
190         dcd_v2->header.version = DCD_VERSION;
191         dcd_v2->write_dcd_command.tag = DCD_COMMAND_TAG;
192         dcd_v2->write_dcd_command.length = cpu_to_be16(
193                         dcd_len * sizeof(dcd_addr_data_t) + 4);
194         dcd_v2->write_dcd_command.param = DCD_COMMAND_PARAM;
195 }
196
197 static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len,
198                                         struct stat *sbuf,
199                                         struct mkimage_params *params)
200 {
201         imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1;
202         flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
203         dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
204         uint32_t base_offset;
205
206         /* Exit if there is no BOOT_FROM field specifying the flash_offset */
207         if(imxhdr->flash_offset == FLASH_OFFSET_UNDEFINED) {
208                 fprintf(stderr, "Error: Header v1: No BOOT_FROM tag in %s\n",
209                         params->imagename);
210                 exit(EXIT_FAILURE);
211         }
212
213         /* Set magic number */
214         fhdr_v1->app_code_barker = APP_CODE_BARKER;
215
216         fhdr_v1->app_dest_ptr = params->addr;
217         fhdr_v1->app_dest_ptr = params->ep - imxhdr->flash_offset -
218                 sizeof(struct imx_header);
219         fhdr_v1->app_code_jump_vector = params->ep;
220
221         base_offset = fhdr_v1->app_dest_ptr + imxhdr->flash_offset ;
222         fhdr_v1->dcd_ptr_ptr =
223                 (uint32_t) (offsetof(flash_header_v1_t, dcd_ptr) -
224                 offsetof(flash_header_v1_t, app_code_jump_vector) +
225                 base_offset);
226
227         fhdr_v1->dcd_ptr = base_offset +
228                         offsetof(imx_header_v1_t, dcd_table);
229
230         /* The external flash header must be at the end of the DCD table */
231         dcd_v1->addr_data[dcd_len].type = sbuf->st_size +
232                                 imxhdr->flash_offset +
233                                 sizeof(struct imx_header);
234
235         /* Security feature are not supported */
236         fhdr_v1->app_code_csf = 0;
237         fhdr_v1->super_root_key = 0;
238 }
239
240 static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
241                                         struct stat *sbuf,
242                                         struct mkimage_params *params)
243 {
244         imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
245         flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
246
247         /* Exit if there is no BOOT_FROM field specifying the flash_offset */
248         if(imxhdr->flash_offset == FLASH_OFFSET_UNDEFINED) {
249                 fprintf(stderr, "Error: Header v2: No BOOT_FROM tag in %s\n",
250                         params->imagename);
251                 exit(EXIT_FAILURE);
252         }
253
254         /* Set magic number */
255         fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
256         fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
257         fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
258
259         fhdr_v2->entry = params->ep;
260         fhdr_v2->reserved1 = fhdr_v2->reserved2 = 0;
261         fhdr_v2->self = params->ep - sizeof(struct imx_header);
262
263         fhdr_v2->dcd_ptr = fhdr_v2->self +
264                         offsetof(imx_header_v2_t, dcd_table);
265
266         fhdr_v2->boot_data_ptr = fhdr_v2->self +
267                         offsetof(imx_header_v2_t, boot_data);
268
269         hdr_v2->boot_data.start = fhdr_v2->self - imxhdr->flash_offset;
270         hdr_v2->boot_data.size = sbuf->st_size +
271                         imxhdr->flash_offset +
272                         sizeof(struct imx_header);
273
274         /* Security feature are not supported */
275         fhdr_v2->csf = 0;
276 }
277
278 static void set_hdr_func(struct imx_header *imxhdr)
279 {
280         switch (imximage_version) {
281         case IMXIMAGE_V1:
282                 set_dcd_val = set_dcd_val_v1;
283                 set_dcd_rst = set_dcd_rst_v1;
284                 set_imx_hdr = set_imx_hdr_v1;
285                 max_dcd_entries = MAX_HW_CFG_SIZE_V1;
286                 break;
287         case IMXIMAGE_V2:
288                 set_dcd_val = set_dcd_val_v2;
289                 set_dcd_rst = set_dcd_rst_v2;
290                 set_imx_hdr = set_imx_hdr_v2;
291                 max_dcd_entries = MAX_HW_CFG_SIZE_V2;
292                 break;
293         default:
294                 err_imximage_version(imximage_version);
295                 break;
296         }
297 }
298
299 static void print_hdr_v1(struct imx_header *imx_hdr)
300 {
301         imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
302         flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
303         dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
304         uint32_t size, length, ver;
305
306         size = dcd_v1->preamble.length;
307         if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
308                 fprintf(stderr,
309                         "Error: Image corrupt DCD size %d exceed maximum %d\n",
310                         (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
311                         MAX_HW_CFG_SIZE_V1);
312                 exit(EXIT_FAILURE);
313         }
314
315         length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
316         ver = detect_imximage_version(imx_hdr);
317
318         printf("Image Type:   Freescale IMX Boot Image\n");
319         printf("Image Ver:    %x", ver);
320         printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
321         printf("Data Size:    ");
322         genimg_print_size(dcd_v1->addr_data[length].type);
323         printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
324         printf("Entry Point:  %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
325 }
326
327 static void print_hdr_v2(struct imx_header *imx_hdr)
328 {
329         imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
330         flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
331         dcd_v2_t *dcd_v2 = &hdr_v2->dcd_table;
332         uint32_t size, version;
333
334         size = be16_to_cpu(dcd_v2->header.length) - 8;
335         if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
336                 fprintf(stderr,
337                         "Error: Image corrupt DCD size %d exceed maximum %d\n",
338                         (uint32_t)(size / sizeof(dcd_addr_data_t)),
339                         MAX_HW_CFG_SIZE_V2);
340                 exit(EXIT_FAILURE);
341         }
342
343         version = detect_imximage_version(imx_hdr);
344
345         printf("Image Type:   Freescale IMX Boot Image\n");
346         printf("Image Ver:    %x", version);
347         printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
348         printf("Data Size:    ");
349         genimg_print_size(hdr_v2->boot_data.size);
350         printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
351         printf("Entry Point:  %08x\n", (uint32_t)fhdr_v2->entry);
352 }
353
354 static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
355                                 char *name, int lineno, int fld, int dcd_len)
356 {
357         int value;
358         static int cmd_ver_first = ~0;
359
360         switch (cmd) {
361         case CMD_IMAGE_VERSION:
362                 imximage_version = get_cfg_value(token, name, lineno);
363                 if (cmd_ver_first == 0) {
364                         fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
365                                 "command need be the first before other "
366                                 "valid command in the file\n", name, lineno);
367                         exit(EXIT_FAILURE);
368                 }
369                 cmd_ver_first = 1;
370                 set_hdr_func(imxhdr);
371                 break;
372         case CMD_BOOT_FROM:
373                 imxhdr->flash_offset = get_table_entry_id(imximage_bootops,
374                                         "imximage boot option", token);
375                 if (imxhdr->flash_offset == -1) {
376                         fprintf(stderr, "Error: %s[%d] -Invalid boot device"
377                                 "(%s)\n", name, lineno, token);
378                         exit(EXIT_FAILURE);
379                 }
380                 if (unlikely(cmd_ver_first != 1))
381                         cmd_ver_first = 0;
382                 break;
383         case CMD_DATA:
384                 value = get_cfg_value(token, name, lineno);
385                 (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
386                 if (unlikely(cmd_ver_first != 1))
387                         cmd_ver_first = 0;
388                 break;
389         }
390 }
391
392 static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
393                 char *token, char *name, int lineno, int fld, int *dcd_len)
394 {
395         int value;
396
397         switch (fld) {
398         case CFG_COMMAND:
399                 *cmd = get_table_entry_id(imximage_cmds,
400                         "imximage commands", token);
401                 if (*cmd < 0) {
402                         fprintf(stderr, "Error: %s[%d] - Invalid command"
403                         "(%s)\n", name, lineno, token);
404                         exit(EXIT_FAILURE);
405                 }
406                 break;
407         case CFG_REG_SIZE:
408                 parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
409                 break;
410         case CFG_REG_ADDRESS:
411         case CFG_REG_VALUE:
412                 if (*cmd != CMD_DATA)
413                         return;
414
415                 value = get_cfg_value(token, name, lineno);
416                 (*set_dcd_val)(imxhdr, name, lineno, fld, value, *dcd_len);
417
418                 if (fld == CFG_REG_VALUE) {
419                         (*dcd_len)++;
420                         if (*dcd_len > max_dcd_entries) {
421                                 fprintf(stderr, "Error: %s[%d] -"
422                                         "DCD table exceeds maximum size(%d)\n",
423                                         name, lineno, max_dcd_entries);
424                                 exit(EXIT_FAILURE);
425                         }
426                 }
427                 break;
428         default:
429                 break;
430         }
431 }
432 static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
433 {
434         FILE *fd = NULL;
435         char *line = NULL;
436         char *token, *saveptr1, *saveptr2;
437         int lineno = 0;
438         int fld;
439         size_t len;
440         int dcd_len = 0;
441         int32_t cmd;
442
443         fd = fopen(name, "r");
444         if (fd == 0) {
445                 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
446                 exit(EXIT_FAILURE);
447         }
448
449         /* Very simple parsing, line starting with # are comments
450          * and are dropped
451          */
452         while ((getline(&line, &len, fd)) > 0) {
453                 lineno++;
454
455                 token = strtok_r(line, "\r\n", &saveptr1);
456                 if (token == NULL)
457                         continue;
458
459                 /* Check inside the single line */
460                 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
461                                 line = token; ; line = NULL, fld++) {
462                         token = strtok_r(line, " \t", &saveptr2);
463                         if (token == NULL)
464                                 break;
465
466                         /* Drop all text starting with '#' as comments */
467                         if (token[0] == '#')
468                                 break;
469
470                         parse_cfg_fld(imxhdr, &cmd, token, name,
471                                         lineno, fld, &dcd_len);
472                 }
473
474         }
475
476         (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
477         fclose(fd);
478
479         return dcd_len;
480 }
481
482
483 static int imximage_check_image_types(uint8_t type)
484 {
485         if (type == IH_TYPE_IMXIMAGE)
486                 return EXIT_SUCCESS;
487         else
488                 return EXIT_FAILURE;
489 }
490
491 static int imximage_verify_header(unsigned char *ptr, int image_size,
492                         struct mkimage_params *params)
493 {
494         struct imx_header *imx_hdr = (struct imx_header *) ptr;
495
496         if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
497                 return -FDT_ERR_BADSTRUCTURE;
498
499         return 0;
500 }
501
502 static void imximage_print_header(const void *ptr)
503 {
504         struct imx_header *imx_hdr = (struct imx_header *) ptr;
505         uint32_t version = detect_imximage_version(imx_hdr);
506
507         switch (version) {
508         case IMXIMAGE_V1:
509                 print_hdr_v1(imx_hdr);
510                 break;
511         case IMXIMAGE_V2:
512                 print_hdr_v2(imx_hdr);
513                 break;
514         default:
515                 err_imximage_version(version);
516                 break;
517         }
518 }
519
520 static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
521                                 struct mkimage_params *params)
522 {
523         struct imx_header *imxhdr = (struct imx_header *)ptr;
524         uint32_t dcd_len;
525
526         /*
527          * In order to not change the old imx cfg file
528          * by adding VERSION command into it, here need
529          * set up function ptr group to V1 by default.
530          */
531         imximage_version = IMXIMAGE_V1;
532         /* Be able to detect if the cfg file has no BOOT_FROM tag */
533         imxhdr->flash_offset = FLASH_OFFSET_UNDEFINED;
534         set_hdr_func(imxhdr);
535
536         /* Parse dcd configuration file */
537         dcd_len = parse_cfg_file(imxhdr, params->imagename);
538
539         /* Set the imx header */
540         (*set_imx_hdr)(imxhdr, dcd_len, sbuf, params);
541 }
542
543 int imximage_check_params(struct mkimage_params *params)
544 {
545         if (!params)
546                 return CFG_INVALID;
547         if (!strlen(params->imagename)) {
548                 fprintf(stderr, "Error: %s - Configuration file not specified, "
549                         "it is needed for imximage generation\n",
550                         params->cmdname);
551                 return CFG_INVALID;
552         }
553         /*
554          * Check parameters:
555          * XIP is not allowed and verify that incompatible
556          * parameters are not sent at the same time
557          * For example, if list is required a data image must not be provided
558          */
559         return  (params->dflag && (params->fflag || params->lflag)) ||
560                 (params->fflag && (params->dflag || params->lflag)) ||
561                 (params->lflag && (params->dflag || params->fflag)) ||
562                 (params->xflag) || !(strlen(params->imagename));
563 }
564
565 /*
566  * imximage parameters
567  */
568 static struct image_type_params imximage_params = {
569         .name           = "Freescale i.MX 5x Boot Image support",
570         .header_size    = sizeof(struct imx_header),
571         .hdr            = (void *)&imximage_header,
572         .check_image_type = imximage_check_image_types,
573         .verify_header  = imximage_verify_header,
574         .print_header   = imximage_print_header,
575         .set_header     = imximage_set_header,
576         .check_params   = imximage_check_params,
577 };
578
579 void init_imx_image_type(void)
580 {
581         mkimage_register(&imximage_params);
582 }