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