3 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
6 * Marvell Semiconductor <www.marvell.com>
7 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
9 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 /* Required to obtain the getline prototype from stdio.h */
36 * Supported commands for configuration file
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", },
46 * Supported Boot options for configuration file
47 * this is needed to set the correct flash offset
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", },
58 * IMXIMAGE version definition for i.MX chips
60 static table_entry_t imximage_versions[] = {
61 {IMXIMAGE_V1, "", " (i.MX25/35/51 compatible)", },
62 {IMXIMAGE_V2, "", " (i.MX53 compatible)", },
63 {-1, "", " (Invalid)", },
66 static struct imx_header imximage_header;
67 static uint32_t imximage_version;
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;
73 static uint32_t get_cfg_value(char *token, char *name, int linenr)
79 value = strtoul(token, &endptr, 16);
80 if (errno || (token == endptr)) {
81 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
88 static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
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;
95 /* Try to detect V1 */
96 if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) &&
97 (hdr_v1->dcd_table.preamble.barker == DCD_BARKER))
100 /* Try to detect V2 */
101 if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
102 (hdr_v2->dcd_table.header.tag == DCD_HEADER_TAG))
105 return IMXIMAGE_VER_INVALID;
108 static void err_imximage_version(int version)
111 "Error: Unsupported imximage version:%d\n", version);
116 static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno,
117 int fld, uint32_t value, uint32_t off)
119 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
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);
130 dcd_v1->addr_data[off].type = value;
132 case CFG_REG_ADDRESS:
133 dcd_v1->addr_data[off].addr = value;
136 dcd_v1->addr_data[off].value = value;
144 static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno,
145 int fld, uint32_t value, uint32_t off)
147 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
150 case CFG_REG_ADDRESS:
151 dcd_v2->addr_data[off].addr = cpu_to_be32(value);
154 dcd_v2->addr_data[off].value = cpu_to_be32(value);
163 * Complete setting up the rest field of DCD of V1
164 * such as barker code and DCD data length.
166 static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len,
167 char *name, int lineno)
169 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
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);
178 dcd_v1->preamble.barker = DCD_BARKER;
179 dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
183 * Complete setting up the reset field of DCD of V2
184 * such as DCD tag, version, length, etc.
186 static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len,
187 char *name, int lineno)
189 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
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);
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;
208 static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len,
210 struct mkimage_params *params)
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;
217 /* Set default offset */
218 imxhdr->flash_offset = FLASH_OFFSET_STANDARD;
220 /* Set magic number */
221 fhdr_v1->app_code_barker = APP_CODE_BARKER;
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;
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) +
234 fhdr_v1->dcd_ptr = base_offset +
235 offsetof(imx_header_v1_t, dcd_table);
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);
242 /* Security feature are not supported */
243 fhdr_v1->app_code_csf = 0;
244 fhdr_v1->super_root_key = 0;
247 static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
249 struct mkimage_params *params)
251 imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
252 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
254 /* Set default offset */
255 imxhdr->flash_offset = FLASH_OFFSET_STANDARD;
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 */
262 fhdr_v2->entry = params->ep;
263 fhdr_v2->reserved1 = fhdr_v2->reserved2 = 0;
264 fhdr_v2->self = params->ep - sizeof(struct imx_header);
266 fhdr_v2->dcd_ptr = fhdr_v2->self +
267 offsetof(imx_header_v2_t, dcd_table);
269 fhdr_v2->boot_data_ptr = fhdr_v2->self +
270 offsetof(imx_header_v2_t, boot_data);
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);
277 /* Security feature are not supported */
281 static void set_hdr_func(struct imx_header *imxhdr)
283 switch (imximage_version) {
285 set_dcd_val = set_dcd_val_v1;
286 set_dcd_rst = set_dcd_rst_v1;
287 set_imx_hdr = set_imx_hdr_v1;
290 set_dcd_val = set_dcd_val_v2;
291 set_dcd_rst = set_dcd_rst_v2;
292 set_imx_hdr = set_imx_hdr_v2;
295 err_imximage_version(imximage_version);
300 static void print_hdr_v1(struct imx_header *imx_hdr)
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;
307 size = dcd_v1->preamble.length;
308 if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
310 "Error: Image corrupt DCD size %d exceed maximum %d\n",
311 (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
316 length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
317 ver = detect_imximage_version(imx_hdr);
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);
328 static void print_hdr_v2(struct imx_header *imx_hdr)
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;
335 size = be16_to_cpu(dcd_v2->header.length) - 8;
336 if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
338 "Error: Image corrupt DCD size %d exceed maximum %d\n",
339 (uint32_t)(size / sizeof(dcd_addr_data_t)),
344 version = detect_imximage_version(imx_hdr);
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);
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)
359 static int cmd_ver_first = ~0;
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);
371 set_hdr_func(imxhdr);
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);
381 if (unlikely(cmd_ver_first != 1))
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))
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)
400 *cmd = get_table_entry_id(imximage_cmds,
401 "imximage commands", token);
403 fprintf(stderr, "Error: %s[%d] - Invalid command"
404 "(%s)\n", name, lineno, token);
409 parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
411 case CFG_REG_ADDRESS:
413 if (*cmd != CMD_DATA)
416 value = get_cfg_value(token, name, lineno);
417 (*set_dcd_val)(imxhdr, name, lineno, fld, value, *dcd_len);
419 if (fld == CFG_REG_VALUE)
426 static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
430 char *token, *saveptr1, *saveptr2;
437 fd = fopen(name, "r");
439 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
443 /* Very simple parsing, line starting with # are comments
446 while ((getline(&line, &len, fd)) > 0) {
449 token = strtok_r(line, "\r\n", &saveptr1);
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);
460 /* Drop all text starting with '#' as comments */
464 parse_cfg_fld(imxhdr, &cmd, token, name,
465 lineno, fld, &dcd_len);
470 (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
477 static int imximage_check_image_types(uint8_t type)
479 if (type == IH_TYPE_IMXIMAGE)
485 static int imximage_verify_header(unsigned char *ptr, int image_size,
486 struct mkimage_params *params)
488 struct imx_header *imx_hdr = (struct imx_header *) ptr;
490 if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
491 return -FDT_ERR_BADSTRUCTURE;
496 static void imximage_print_header(const void *ptr)
498 struct imx_header *imx_hdr = (struct imx_header *) ptr;
499 uint32_t version = detect_imximage_version(imx_hdr);
503 print_hdr_v1(imx_hdr);
506 print_hdr_v2(imx_hdr);
509 err_imximage_version(version);
514 static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
515 struct mkimage_params *params)
517 struct imx_header *imxhdr = (struct imx_header *)ptr;
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.
525 imximage_version = IMXIMAGE_V1;
526 set_hdr_func(imxhdr);
528 /* Parse dcd configuration file */
529 dcd_len = parse_cfg_file(imxhdr, params->imagename);
531 /* Set the imx header */
532 (*set_imx_hdr)(imxhdr, dcd_len, sbuf, params);
535 int imximage_check_params(struct mkimage_params *params)
539 if (!strlen(params->imagename)) {
540 fprintf(stderr, "Error: %s - Configuration file not specified, "
541 "it is needed for imximage generation\n",
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
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));
558 * imximage parameters
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,
571 void init_imx_image_type(void)
573 mkimage_register(&imximage_params);