blob: 87a6b59e004ef80be6e27c1148cc2083061bb173 [file] [log] [blame]
Stefano Babic7b07f092010-01-20 18:19:10 +01001/*
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
Kumar Gala0bea74d2010-01-27 10:16:56 -060028/* Required to obtain the getline prototype from stdio.h */
29#define _GNU_SOURCE
30
Stefano Babic7b07f092010-01-20 18:19:10 +010031#include "mkimage.h"
32#include <image.h>
33#include "imximage.h"
34
35/*
36 * Supported commands for configuration file
37 */
38static table_entry_t imximage_cmds[] = {
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000039 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
40 {CMD_DATA, "DATA", "Reg Write Data", },
41 {CMD_IMAGE_VERSION, "IMAGE_VERSION", "image version", },
42 {-1, "", "", },
Stefano Babic7b07f092010-01-20 18:19:10 +010043};
44
45/*
46 * Supported Boot options for configuration file
47 * this is needed to set the correct flash offset
48 */
49static table_entry_t imximage_bootops[] = {
Stefano Babic7b07f092010-01-20 18:19:10 +010050 {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",},
Dirk Behme40a9c462012-01-11 23:28:32 +000051 {FLASH_OFFSET_NAND, "nand", "NAND Flash", },
Dirk Behmedfbf6ce2012-01-11 23:28:31 +000052 {FLASH_OFFSET_NOR, "nor", "NOR Flash", },
53 {FLASH_OFFSET_SATA, "sata", "SATA Disk", },
Dirk Behme40a9c462012-01-11 23:28:32 +000054 {FLASH_OFFSET_SD, "sd", "SD Card", },
55 {FLASH_OFFSET_SPI, "spi", "SPI Flash", },
Stefano Babic7b07f092010-01-20 18:19:10 +010056 {-1, "", "Invalid", },
57};
58
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000059/*
60 * IMXIMAGE version definition for i.MX chips
61 */
62static table_entry_t imximage_versions[] = {
63 {IMXIMAGE_V1, "", " (i.MX25/35/51 compatible)", },
Dirk Behmedfbf6ce2012-01-11 23:28:31 +000064 {IMXIMAGE_V2, "", " (i.MX53/6 compatible)", },
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000065 {-1, "", " (Invalid)", },
66};
Stefano Babic7b07f092010-01-20 18:19:10 +010067
68static struct imx_header imximage_header;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000069static uint32_t imximage_version;
70
71static set_dcd_val_t set_dcd_val;
72static set_dcd_rst_t set_dcd_rst;
73static set_imx_hdr_t set_imx_hdr;
Troy Kisky9bd5b852012-10-03 15:47:03 +000074static uint32_t max_dcd_entries;
Troy Kisky7b922cb2012-10-03 15:47:07 +000075static uint32_t *header_size_ptr;
Stefano Babic7b07f092010-01-20 18:19:10 +010076
77static uint32_t get_cfg_value(char *token, char *name, int linenr)
78{
79 char *endptr;
80 uint32_t value;
81
82 errno = 0;
83 value = strtoul(token, &endptr, 16);
84 if (errno || (token == endptr)) {
85 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
86 name, linenr, token);
87 exit(EXIT_FAILURE);
88 }
89 return value;
90}
91
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000092static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
Stefano Babic7b07f092010-01-20 18:19:10 +010093{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000094 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
95 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
96 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
97 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
98
99 /* Try to detect V1 */
100 if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) &&
101 (hdr_v1->dcd_table.preamble.barker == DCD_BARKER))
102 return IMXIMAGE_V1;
103
104 /* Try to detect V2 */
105 if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
106 (hdr_v2->dcd_table.header.tag == DCD_HEADER_TAG))
107 return IMXIMAGE_V2;
108
109 return IMXIMAGE_VER_INVALID;
Stefano Babic7b07f092010-01-20 18:19:10 +0100110}
111
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000112static void err_imximage_version(int version)
Stefano Babic7b07f092010-01-20 18:19:10 +0100113{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000114 fprintf(stderr,
115 "Error: Unsupported imximage version:%d\n", version);
Stefano Babic7b07f092010-01-20 18:19:10 +0100116
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000117 exit(EXIT_FAILURE);
118}
Stefano Babic7b07f092010-01-20 18:19:10 +0100119
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000120static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno,
121 int fld, uint32_t value, uint32_t off)
122{
123 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
Stefano Babic7b07f092010-01-20 18:19:10 +0100124
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000125 switch (fld) {
126 case CFG_REG_SIZE:
127 /* Byte, halfword, word */
128 if ((value != 1) && (value != 2) && (value != 4)) {
129 fprintf(stderr, "Error: %s[%d] - "
130 "Invalid register size " "(%d)\n",
131 name, lineno, value);
132 exit(EXIT_FAILURE);
133 }
134 dcd_v1->addr_data[off].type = value;
135 break;
136 case CFG_REG_ADDRESS:
137 dcd_v1->addr_data[off].addr = value;
138 break;
139 case CFG_REG_VALUE:
140 dcd_v1->addr_data[off].value = value;
141 break;
142 default:
143 break;
Stefano Babic7b07f092010-01-20 18:19:10 +0100144
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000145 }
Stefano Babic7b07f092010-01-20 18:19:10 +0100146}
147
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000148static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno,
149 int fld, uint32_t value, uint32_t off)
Stefano Babic7b07f092010-01-20 18:19:10 +0100150{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000151 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
152
153 switch (fld) {
154 case CFG_REG_ADDRESS:
155 dcd_v2->addr_data[off].addr = cpu_to_be32(value);
156 break;
157 case CFG_REG_VALUE:
158 dcd_v2->addr_data[off].value = cpu_to_be32(value);
159 break;
160 default:
161 break;
162
163 }
164}
165
166/*
167 * Complete setting up the rest field of DCD of V1
168 * such as barker code and DCD data length.
169 */
170static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len,
171 char *name, int lineno)
172{
173 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
174
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000175 dcd_v1->preamble.barker = DCD_BARKER;
176 dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
177}
178
179/*
180 * Complete setting up the reset field of DCD of V2
181 * such as DCD tag, version, length, etc.
182 */
183static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len,
184 char *name, int lineno)
185{
186 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
187
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000188 dcd_v2->header.tag = DCD_HEADER_TAG;
189 dcd_v2->header.length = cpu_to_be16(
190 dcd_len * sizeof(dcd_addr_data_t) + 8);
191 dcd_v2->header.version = DCD_VERSION;
192 dcd_v2->write_dcd_command.tag = DCD_COMMAND_TAG;
193 dcd_v2->write_dcd_command.length = cpu_to_be16(
194 dcd_len * sizeof(dcd_addr_data_t) + 4);
195 dcd_v2->write_dcd_command.param = DCD_COMMAND_PARAM;
196}
197
198static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kisky7bb92202012-10-03 15:47:08 +0000199 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000200{
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;
Troy Kisky7b922cb2012-10-03 15:47:07 +0000205 uint32_t header_length = (((char *)&dcd_v1->addr_data[dcd_len].addr)
206 - ((char *)imxhdr));
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000207
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000208 /* Set magic number */
209 fhdr_v1->app_code_barker = APP_CODE_BARKER;
210
Troy Kisky7bb92202012-10-03 15:47:08 +0000211 fhdr_v1->app_dest_ptr = entry_point - flash_offset -
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000212 sizeof(struct imx_header);
Troy Kisky7bb92202012-10-03 15:47:08 +0000213 fhdr_v1->app_code_jump_vector = entry_point;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000214
Troy Kisky7bb92202012-10-03 15:47:08 +0000215 base_offset = fhdr_v1->app_dest_ptr + flash_offset;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000216 fhdr_v1->dcd_ptr_ptr =
217 (uint32_t) (offsetof(flash_header_v1_t, dcd_ptr) -
218 offsetof(flash_header_v1_t, app_code_jump_vector) +
219 base_offset);
220
221 fhdr_v1->dcd_ptr = base_offset +
222 offsetof(imx_header_v1_t, dcd_table);
223
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000224 /* Security feature are not supported */
225 fhdr_v1->app_code_csf = 0;
226 fhdr_v1->super_root_key = 0;
Troy Kisky7b922cb2012-10-03 15:47:07 +0000227 header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000228}
229
230static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kisky7bb92202012-10-03 15:47:08 +0000231 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000232{
233 imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
234 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
235
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000236 /* Set magic number */
237 fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
238 fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
239 fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
240
Troy Kisky7bb92202012-10-03 15:47:08 +0000241 fhdr_v2->entry = entry_point;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000242 fhdr_v2->reserved1 = fhdr_v2->reserved2 = 0;
Troy Kisky7bb92202012-10-03 15:47:08 +0000243 fhdr_v2->self = entry_point - sizeof(struct imx_header);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000244
245 fhdr_v2->dcd_ptr = fhdr_v2->self +
246 offsetof(imx_header_v2_t, dcd_table);
247
248 fhdr_v2->boot_data_ptr = fhdr_v2->self +
249 offsetof(imx_header_v2_t, boot_data);
250
Troy Kisky7bb92202012-10-03 15:47:08 +0000251 hdr_v2->boot_data.start = fhdr_v2->self - flash_offset;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000252
253 /* Security feature are not supported */
254 fhdr_v2->csf = 0;
Troy Kisky7b922cb2012-10-03 15:47:07 +0000255 header_size_ptr = &hdr_v2->boot_data.size;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000256}
257
258static void set_hdr_func(struct imx_header *imxhdr)
259{
260 switch (imximage_version) {
261 case IMXIMAGE_V1:
262 set_dcd_val = set_dcd_val_v1;
263 set_dcd_rst = set_dcd_rst_v1;
264 set_imx_hdr = set_imx_hdr_v1;
Troy Kisky9bd5b852012-10-03 15:47:03 +0000265 max_dcd_entries = MAX_HW_CFG_SIZE_V1;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000266 break;
267 case IMXIMAGE_V2:
268 set_dcd_val = set_dcd_val_v2;
269 set_dcd_rst = set_dcd_rst_v2;
270 set_imx_hdr = set_imx_hdr_v2;
Troy Kisky9bd5b852012-10-03 15:47:03 +0000271 max_dcd_entries = MAX_HW_CFG_SIZE_V2;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000272 break;
273 default:
274 err_imximage_version(imximage_version);
275 break;
276 }
277}
Stefano Babic7b07f092010-01-20 18:19:10 +0100278
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000279static void print_hdr_v1(struct imx_header *imx_hdr)
280{
281 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
282 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
283 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
284 uint32_t size, length, ver;
285
286 size = dcd_v1->preamble.length;
287 if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
Stefano Babic7b07f092010-01-20 18:19:10 +0100288 fprintf(stderr,
289 "Error: Image corrupt DCD size %d exceed maximum %d\n",
Stefano Babic5cdde802010-02-05 15:16:02 +0100290 (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000291 MAX_HW_CFG_SIZE_V1);
292 exit(EXIT_FAILURE);
293 }
294
295 length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
296 ver = detect_imximage_version(imx_hdr);
297
298 printf("Image Type: Freescale IMX Boot Image\n");
299 printf("Image Ver: %x", ver);
300 printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
301 printf("Data Size: ");
302 genimg_print_size(dcd_v1->addr_data[length].type);
303 printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
304 printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
305}
306
307static void print_hdr_v2(struct imx_header *imx_hdr)
308{
309 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
310 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
311 dcd_v2_t *dcd_v2 = &hdr_v2->dcd_table;
312 uint32_t size, version;
313
314 size = be16_to_cpu(dcd_v2->header.length) - 8;
315 if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
316 fprintf(stderr,
317 "Error: Image corrupt DCD size %d exceed maximum %d\n",
318 (uint32_t)(size / sizeof(dcd_addr_data_t)),
319 MAX_HW_CFG_SIZE_V2);
Stefano Babic7b07f092010-01-20 18:19:10 +0100320 exit(EXIT_FAILURE);
321 }
322
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000323 version = detect_imximage_version(imx_hdr);
Stefano Babic7b07f092010-01-20 18:19:10 +0100324
325 printf("Image Type: Freescale IMX Boot Image\n");
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000326 printf("Image Ver: %x", version);
327 printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
Stefano Babic7b07f092010-01-20 18:19:10 +0100328 printf("Data Size: ");
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000329 genimg_print_size(hdr_v2->boot_data.size);
330 printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
331 printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
Stefano Babic7b07f092010-01-20 18:19:10 +0100332}
333
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000334static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
335 char *name, int lineno, int fld, int dcd_len)
336{
337 int value;
338 static int cmd_ver_first = ~0;
339
340 switch (cmd) {
341 case CMD_IMAGE_VERSION:
342 imximage_version = get_cfg_value(token, name, lineno);
343 if (cmd_ver_first == 0) {
344 fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
345 "command need be the first before other "
346 "valid command in the file\n", name, lineno);
347 exit(EXIT_FAILURE);
348 }
349 cmd_ver_first = 1;
350 set_hdr_func(imxhdr);
351 break;
352 case CMD_BOOT_FROM:
353 imxhdr->flash_offset = get_table_entry_id(imximage_bootops,
354 "imximage boot option", token);
355 if (imxhdr->flash_offset == -1) {
356 fprintf(stderr, "Error: %s[%d] -Invalid boot device"
357 "(%s)\n", name, lineno, token);
358 exit(EXIT_FAILURE);
359 }
360 if (unlikely(cmd_ver_first != 1))
361 cmd_ver_first = 0;
362 break;
363 case CMD_DATA:
364 value = get_cfg_value(token, name, lineno);
365 (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
366 if (unlikely(cmd_ver_first != 1))
367 cmd_ver_first = 0;
368 break;
369 }
370}
371
372static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
373 char *token, char *name, int lineno, int fld, int *dcd_len)
374{
375 int value;
376
377 switch (fld) {
378 case CFG_COMMAND:
379 *cmd = get_table_entry_id(imximage_cmds,
380 "imximage commands", token);
381 if (*cmd < 0) {
382 fprintf(stderr, "Error: %s[%d] - Invalid command"
383 "(%s)\n", name, lineno, token);
384 exit(EXIT_FAILURE);
385 }
386 break;
387 case CFG_REG_SIZE:
388 parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
389 break;
390 case CFG_REG_ADDRESS:
391 case CFG_REG_VALUE:
392 if (*cmd != CMD_DATA)
393 return;
394
395 value = get_cfg_value(token, name, lineno);
396 (*set_dcd_val)(imxhdr, name, lineno, fld, value, *dcd_len);
397
Troy Kisky9bd5b852012-10-03 15:47:03 +0000398 if (fld == CFG_REG_VALUE) {
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000399 (*dcd_len)++;
Troy Kisky9bd5b852012-10-03 15:47:03 +0000400 if (*dcd_len > max_dcd_entries) {
401 fprintf(stderr, "Error: %s[%d] -"
402 "DCD table exceeds maximum size(%d)\n",
403 name, lineno, max_dcd_entries);
404 exit(EXIT_FAILURE);
405 }
406 }
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000407 break;
408 default:
409 break;
410 }
411}
412static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
Stefano Babic7b07f092010-01-20 18:19:10 +0100413{
414 FILE *fd = NULL;
415 char *line = NULL;
416 char *token, *saveptr1, *saveptr2;
417 int lineno = 0;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000418 int fld;
Kim Phillips1c0c2502010-02-22 19:37:56 -0600419 size_t len;
Stefano Babic7b07f092010-01-20 18:19:10 +0100420 int dcd_len = 0;
Stefano Babic7b07f092010-01-20 18:19:10 +0100421 int32_t cmd;
422
423 fd = fopen(name, "r");
424 if (fd == 0) {
425 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
426 exit(EXIT_FAILURE);
427 }
428
429 /* Very simple parsing, line starting with # are comments
430 * and are dropped
431 */
432 while ((getline(&line, &len, fd)) > 0) {
433 lineno++;
434
435 token = strtok_r(line, "\r\n", &saveptr1);
436 if (token == NULL)
437 continue;
438
439 /* Check inside the single line */
440 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
441 line = token; ; line = NULL, fld++) {
442 token = strtok_r(line, " \t", &saveptr2);
443 if (token == NULL)
444 break;
445
446 /* Drop all text starting with '#' as comments */
447 if (token[0] == '#')
448 break;
449
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000450 parse_cfg_fld(imxhdr, &cmd, token, name,
451 lineno, fld, &dcd_len);
Stefano Babic7b07f092010-01-20 18:19:10 +0100452 }
453
Stefano Babic7b07f092010-01-20 18:19:10 +0100454 }
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000455
456 (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
Stefano Babic7b07f092010-01-20 18:19:10 +0100457 fclose(fd);
458
Troy Kiskye55eaf92012-10-03 15:47:05 +0000459 /* Exit if there is no BOOT_FROM field specifying the flash_offset */
460 if (imxhdr->flash_offset == FLASH_OFFSET_UNDEFINED) {
461 fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name);
462 exit(EXIT_FAILURE);
463 }
Stefano Babic5cdde802010-02-05 15:16:02 +0100464 return dcd_len;
Stefano Babic7b07f092010-01-20 18:19:10 +0100465}
466
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000467
468static int imximage_check_image_types(uint8_t type)
Stefano Babic7b07f092010-01-20 18:19:10 +0100469{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000470 if (type == IH_TYPE_IMXIMAGE)
471 return EXIT_SUCCESS;
472 else
473 return EXIT_FAILURE;
474}
Stefano Babic7b07f092010-01-20 18:19:10 +0100475
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000476static int imximage_verify_header(unsigned char *ptr, int image_size,
477 struct mkimage_params *params)
478{
479 struct imx_header *imx_hdr = (struct imx_header *) ptr;
Stefano Babic7b07f092010-01-20 18:19:10 +0100480
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000481 if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
482 return -FDT_ERR_BADSTRUCTURE;
Stefano Babic7b07f092010-01-20 18:19:10 +0100483
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000484 return 0;
485}
Stefano Babic7b07f092010-01-20 18:19:10 +0100486
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000487static void imximage_print_header(const void *ptr)
488{
489 struct imx_header *imx_hdr = (struct imx_header *) ptr;
490 uint32_t version = detect_imximage_version(imx_hdr);
Stefano Babic7b07f092010-01-20 18:19:10 +0100491
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000492 switch (version) {
493 case IMXIMAGE_V1:
494 print_hdr_v1(imx_hdr);
495 break;
496 case IMXIMAGE_V2:
497 print_hdr_v2(imx_hdr);
498 break;
499 default:
500 err_imximage_version(version);
501 break;
502 }
503}
Stefano Babic7b07f092010-01-20 18:19:10 +0100504
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000505static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
506 struct mkimage_params *params)
507{
508 struct imx_header *imxhdr = (struct imx_header *)ptr;
509 uint32_t dcd_len;
Stefano Babic7b07f092010-01-20 18:19:10 +0100510
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000511 /*
512 * In order to not change the old imx cfg file
513 * by adding VERSION command into it, here need
514 * set up function ptr group to V1 by default.
515 */
516 imximage_version = IMXIMAGE_V1;
Dirk Behme14a98cd2012-02-22 22:50:19 +0000517 /* Be able to detect if the cfg file has no BOOT_FROM tag */
518 imxhdr->flash_offset = FLASH_OFFSET_UNDEFINED;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000519 set_hdr_func(imxhdr);
Stefano Babic7b07f092010-01-20 18:19:10 +0100520
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000521 /* Parse dcd configuration file */
522 dcd_len = parse_cfg_file(imxhdr, params->imagename);
Stefano Babic7b07f092010-01-20 18:19:10 +0100523
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000524 /* Set the imx header */
Troy Kisky7bb92202012-10-03 15:47:08 +0000525 (*set_imx_hdr)(imxhdr, dcd_len, params->ep, imxhdr->flash_offset);
Troy Kisky7b922cb2012-10-03 15:47:07 +0000526 *header_size_ptr = sbuf->st_size + imxhdr->flash_offset;
Stefano Babic7b07f092010-01-20 18:19:10 +0100527}
528
529int imximage_check_params(struct mkimage_params *params)
530{
531 if (!params)
532 return CFG_INVALID;
533 if (!strlen(params->imagename)) {
534 fprintf(stderr, "Error: %s - Configuration file not specified, "
535 "it is needed for imximage generation\n",
536 params->cmdname);
537 return CFG_INVALID;
538 }
539 /*
540 * Check parameters:
541 * XIP is not allowed and verify that incompatible
542 * parameters are not sent at the same time
543 * For example, if list is required a data image must not be provided
544 */
545 return (params->dflag && (params->fflag || params->lflag)) ||
546 (params->fflag && (params->dflag || params->lflag)) ||
547 (params->lflag && (params->dflag || params->fflag)) ||
548 (params->xflag) || !(strlen(params->imagename));
549}
550
551/*
552 * imximage parameters
553 */
554static struct image_type_params imximage_params = {
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000555 .name = "Freescale i.MX 5x Boot Image support",
Stefano Babic7b07f092010-01-20 18:19:10 +0100556 .header_size = sizeof(struct imx_header),
557 .hdr = (void *)&imximage_header,
558 .check_image_type = imximage_check_image_types,
559 .verify_header = imximage_verify_header,
560 .print_header = imximage_print_header,
561 .set_header = imximage_set_header,
562 .check_params = imximage_check_params,
563};
564
565void init_imx_image_type(void)
566{
567 mkimage_register(&imximage_params);
568}