blob: fa308c94b0e3f31cff28b45e61faef9264353af8 [file] [log] [blame]
Stefano Babic8edcde52010-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 Gala249d4de2010-01-27 10:16:56 -060028/* Required to obtain the getline prototype from stdio.h */
29#define _GNU_SOURCE
30
Stefano Babic8edcde52010-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-R643438a1edd72011-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 Babic8edcde52010-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 Babic8edcde52010-01-20 18:19:10 +010050 {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",},
Dirk Behmebd258642012-01-11 23:28:32 +000051 {FLASH_OFFSET_NAND, "nand", "NAND Flash", },
Dirk Behme19b409c2012-01-11 23:28:31 +000052 {FLASH_OFFSET_NOR, "nor", "NOR Flash", },
53 {FLASH_OFFSET_SATA, "sata", "SATA Disk", },
Dirk Behmebd258642012-01-11 23:28:32 +000054 {FLASH_OFFSET_SD, "sd", "SD Card", },
55 {FLASH_OFFSET_SPI, "spi", "SPI Flash", },
Stefano Babic8edcde52010-01-20 18:19:10 +010056 {-1, "", "Invalid", },
57};
58
Liu Hui-R643438a1edd72011-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 Behme19b409c2012-01-11 23:28:31 +000064 {IMXIMAGE_V2, "", " (i.MX53/6 compatible)", },
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000065 {-1, "", " (Invalid)", },
66};
Stefano Babic8edcde52010-01-20 18:19:10 +010067
68static struct imx_header imximage_header;
Liu Hui-R643438a1edd72011-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 Kisky4d5fa982012-10-03 15:47:03 +000074static uint32_t max_dcd_entries;
Troy Kisky24331982012-10-03 15:47:07 +000075static uint32_t *header_size_ptr;
Stefano Babic8edcde52010-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-R643438a1edd72011-01-19 09:40:26 +000092static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
Stefano Babic8edcde52010-01-20 18:19:10 +010093{
Liu Hui-R643438a1edd72011-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 Babic8edcde52010-01-20 18:19:10 +0100110}
111
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000112static void err_imximage_version(int version)
Stefano Babic8edcde52010-01-20 18:19:10 +0100113{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000114 fprintf(stderr,
115 "Error: Unsupported imximage version:%d\n", version);
Stefano Babic8edcde52010-01-20 18:19:10 +0100116
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000117 exit(EXIT_FAILURE);
Stefano Babic8edcde52010-01-20 18:19:10 +0100118}
119
Liu Hui-R643438a1edd72011-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)
Stefano Babic8edcde52010-01-20 18:19:10 +0100122{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000123 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
Stefano Babic8edcde52010-01-20 18:19:10 +0100124
Liu Hui-R643438a1edd72011-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;
144
145 }
146}
147
148static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno,
149 int fld, uint32_t value, uint32_t off)
150{
151 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-R643438a1edd72011-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);
Stefano Babic8edcde52010-01-20 18:19:10 +0100177}
178
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000179/*
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-R643438a1edd72011-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 Kiskyad0826d2012-10-03 15:47:08 +0000199 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643438a1edd72011-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;
Troy Kiskyab857f22012-10-03 15:47:09 +0000204 uint32_t hdr_base;
Troy Kisky24331982012-10-03 15:47:07 +0000205 uint32_t header_length = (((char *)&dcd_v1->addr_data[dcd_len].addr)
206 - ((char *)imxhdr));
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000207
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000208 /* Set magic number */
209 fhdr_v1->app_code_barker = APP_CODE_BARKER;
210
Troy Kiskyab857f22012-10-03 15:47:09 +0000211 hdr_base = entry_point - sizeof(struct imx_header);
212 fhdr_v1->app_dest_ptr = hdr_base - flash_offset;
Troy Kiskyad0826d2012-10-03 15:47:08 +0000213 fhdr_v1->app_code_jump_vector = entry_point;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000214
Troy Kiskyab857f22012-10-03 15:47:09 +0000215 fhdr_v1->dcd_ptr_ptr = hdr_base + offsetof(flash_header_v1_t, dcd_ptr);
216 fhdr_v1->dcd_ptr = hdr_base + offsetof(imx_header_v1_t, dcd_table);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000217
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000218 /* Security feature are not supported */
219 fhdr_v1->app_code_csf = 0;
220 fhdr_v1->super_root_key = 0;
Troy Kisky24331982012-10-03 15:47:07 +0000221 header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000222}
223
224static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kiskyad0826d2012-10-03 15:47:08 +0000225 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000226{
227 imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
228 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
Troy Kiskyab857f22012-10-03 15:47:09 +0000229 uint32_t hdr_base;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000230
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000231 /* Set magic number */
232 fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
233 fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
234 fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
235
Troy Kiskyad0826d2012-10-03 15:47:08 +0000236 fhdr_v2->entry = entry_point;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000237 fhdr_v2->reserved1 = fhdr_v2->reserved2 = 0;
Troy Kiskyab857f22012-10-03 15:47:09 +0000238 fhdr_v2->self = hdr_base = entry_point - sizeof(struct imx_header);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000239
Troy Kiskyab857f22012-10-03 15:47:09 +0000240 fhdr_v2->dcd_ptr = hdr_base + offsetof(imx_header_v2_t, dcd_table);
241 fhdr_v2->boot_data_ptr = hdr_base
242 + offsetof(imx_header_v2_t, boot_data);
243 hdr_v2->boot_data.start = hdr_base - flash_offset;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000244
245 /* Security feature are not supported */
246 fhdr_v2->csf = 0;
Troy Kisky24331982012-10-03 15:47:07 +0000247 header_size_ptr = &hdr_v2->boot_data.size;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000248}
249
250static void set_hdr_func(struct imx_header *imxhdr)
251{
252 switch (imximage_version) {
253 case IMXIMAGE_V1:
254 set_dcd_val = set_dcd_val_v1;
255 set_dcd_rst = set_dcd_rst_v1;
256 set_imx_hdr = set_imx_hdr_v1;
Troy Kisky4d5fa982012-10-03 15:47:03 +0000257 max_dcd_entries = MAX_HW_CFG_SIZE_V1;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000258 break;
259 case IMXIMAGE_V2:
260 set_dcd_val = set_dcd_val_v2;
261 set_dcd_rst = set_dcd_rst_v2;
262 set_imx_hdr = set_imx_hdr_v2;
Troy Kisky4d5fa982012-10-03 15:47:03 +0000263 max_dcd_entries = MAX_HW_CFG_SIZE_V2;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000264 break;
265 default:
266 err_imximage_version(imximage_version);
267 break;
268 }
269}
270
271static void print_hdr_v1(struct imx_header *imx_hdr)
272{
273 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
274 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
275 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
276 uint32_t size, length, ver;
277
278 size = dcd_v1->preamble.length;
279 if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
280 fprintf(stderr,
281 "Error: Image corrupt DCD size %d exceed maximum %d\n",
282 (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
283 MAX_HW_CFG_SIZE_V1);
284 exit(EXIT_FAILURE);
285 }
286
287 length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
288 ver = detect_imximage_version(imx_hdr);
289
290 printf("Image Type: Freescale IMX Boot Image\n");
291 printf("Image Ver: %x", ver);
292 printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
293 printf("Data Size: ");
294 genimg_print_size(dcd_v1->addr_data[length].type);
295 printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
296 printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
297}
298
299static void print_hdr_v2(struct imx_header *imx_hdr)
300{
301 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
302 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
303 dcd_v2_t *dcd_v2 = &hdr_v2->dcd_table;
304 uint32_t size, version;
305
306 size = be16_to_cpu(dcd_v2->header.length) - 8;
307 if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
308 fprintf(stderr,
309 "Error: Image corrupt DCD size %d exceed maximum %d\n",
310 (uint32_t)(size / sizeof(dcd_addr_data_t)),
311 MAX_HW_CFG_SIZE_V2);
312 exit(EXIT_FAILURE);
313 }
314
315 version = detect_imximage_version(imx_hdr);
316
317 printf("Image Type: Freescale IMX Boot Image\n");
318 printf("Image Ver: %x", version);
319 printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
320 printf("Data Size: ");
321 genimg_print_size(hdr_v2->boot_data.size);
322 printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
323 printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
324}
325
326static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
327 char *name, int lineno, int fld, int dcd_len)
328{
329 int value;
330 static int cmd_ver_first = ~0;
331
332 switch (cmd) {
333 case CMD_IMAGE_VERSION:
334 imximage_version = get_cfg_value(token, name, lineno);
335 if (cmd_ver_first == 0) {
336 fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
337 "command need be the first before other "
338 "valid command in the file\n", name, lineno);
339 exit(EXIT_FAILURE);
340 }
341 cmd_ver_first = 1;
342 set_hdr_func(imxhdr);
343 break;
344 case CMD_BOOT_FROM:
345 imxhdr->flash_offset = get_table_entry_id(imximage_bootops,
346 "imximage boot option", token);
347 if (imxhdr->flash_offset == -1) {
348 fprintf(stderr, "Error: %s[%d] -Invalid boot device"
349 "(%s)\n", name, lineno, token);
350 exit(EXIT_FAILURE);
351 }
352 if (unlikely(cmd_ver_first != 1))
353 cmd_ver_first = 0;
354 break;
355 case CMD_DATA:
356 value = get_cfg_value(token, name, lineno);
357 (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
358 if (unlikely(cmd_ver_first != 1))
359 cmd_ver_first = 0;
360 break;
361 }
362}
363
364static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
365 char *token, char *name, int lineno, int fld, int *dcd_len)
366{
367 int value;
368
369 switch (fld) {
370 case CFG_COMMAND:
371 *cmd = get_table_entry_id(imximage_cmds,
372 "imximage commands", token);
373 if (*cmd < 0) {
374 fprintf(stderr, "Error: %s[%d] - Invalid command"
375 "(%s)\n", name, lineno, token);
376 exit(EXIT_FAILURE);
377 }
378 break;
379 case CFG_REG_SIZE:
380 parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
381 break;
382 case CFG_REG_ADDRESS:
383 case CFG_REG_VALUE:
384 if (*cmd != CMD_DATA)
385 return;
386
387 value = get_cfg_value(token, name, lineno);
388 (*set_dcd_val)(imxhdr, name, lineno, fld, value, *dcd_len);
389
Troy Kisky4d5fa982012-10-03 15:47:03 +0000390 if (fld == CFG_REG_VALUE) {
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000391 (*dcd_len)++;
Troy Kisky4d5fa982012-10-03 15:47:03 +0000392 if (*dcd_len > max_dcd_entries) {
393 fprintf(stderr, "Error: %s[%d] -"
394 "DCD table exceeds maximum size(%d)\n",
395 name, lineno, max_dcd_entries);
396 exit(EXIT_FAILURE);
397 }
398 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000399 break;
400 default:
401 break;
402 }
403}
404static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
Stefano Babic8edcde52010-01-20 18:19:10 +0100405{
406 FILE *fd = NULL;
407 char *line = NULL;
408 char *token, *saveptr1, *saveptr2;
409 int lineno = 0;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000410 int fld;
Kim Phillips0ad22702010-02-22 19:37:56 -0600411 size_t len;
Stefano Babic8edcde52010-01-20 18:19:10 +0100412 int dcd_len = 0;
Stefano Babic8edcde52010-01-20 18:19:10 +0100413 int32_t cmd;
414
415 fd = fopen(name, "r");
416 if (fd == 0) {
417 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
418 exit(EXIT_FAILURE);
419 }
420
421 /* Very simple parsing, line starting with # are comments
422 * and are dropped
423 */
424 while ((getline(&line, &len, fd)) > 0) {
425 lineno++;
426
427 token = strtok_r(line, "\r\n", &saveptr1);
428 if (token == NULL)
429 continue;
430
431 /* Check inside the single line */
432 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
433 line = token; ; line = NULL, fld++) {
434 token = strtok_r(line, " \t", &saveptr2);
435 if (token == NULL)
436 break;
437
438 /* Drop all text starting with '#' as comments */
439 if (token[0] == '#')
440 break;
441
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000442 parse_cfg_fld(imxhdr, &cmd, token, name,
443 lineno, fld, &dcd_len);
Stefano Babic8edcde52010-01-20 18:19:10 +0100444 }
445
Stefano Babic8edcde52010-01-20 18:19:10 +0100446 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000447
448 (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
Stefano Babic8edcde52010-01-20 18:19:10 +0100449 fclose(fd);
450
Troy Kisky8d8cc822012-10-03 15:47:05 +0000451 /* Exit if there is no BOOT_FROM field specifying the flash_offset */
452 if (imxhdr->flash_offset == FLASH_OFFSET_UNDEFINED) {
453 fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name);
454 exit(EXIT_FAILURE);
455 }
Stefano Babic5b28e912010-02-05 15:16:02 +0100456 return dcd_len;
Stefano Babic8edcde52010-01-20 18:19:10 +0100457}
458
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000459
460static int imximage_check_image_types(uint8_t type)
461{
462 if (type == IH_TYPE_IMXIMAGE)
463 return EXIT_SUCCESS;
464 else
465 return EXIT_FAILURE;
466}
467
468static int imximage_verify_header(unsigned char *ptr, int image_size,
469 struct mkimage_params *params)
470{
471 struct imx_header *imx_hdr = (struct imx_header *) ptr;
472
473 if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
474 return -FDT_ERR_BADSTRUCTURE;
475
476 return 0;
477}
478
479static void imximage_print_header(const void *ptr)
480{
481 struct imx_header *imx_hdr = (struct imx_header *) ptr;
482 uint32_t version = detect_imximage_version(imx_hdr);
483
484 switch (version) {
485 case IMXIMAGE_V1:
486 print_hdr_v1(imx_hdr);
487 break;
488 case IMXIMAGE_V2:
489 print_hdr_v2(imx_hdr);
490 break;
491 default:
492 err_imximage_version(version);
493 break;
494 }
495}
496
Stefano Babic8edcde52010-01-20 18:19:10 +0100497static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
498 struct mkimage_params *params)
499{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000500 struct imx_header *imxhdr = (struct imx_header *)ptr;
501 uint32_t dcd_len;
Stefano Babic8edcde52010-01-20 18:19:10 +0100502
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000503 /*
504 * In order to not change the old imx cfg file
505 * by adding VERSION command into it, here need
506 * set up function ptr group to V1 by default.
507 */
508 imximage_version = IMXIMAGE_V1;
Dirk Behme49d3e272012-02-22 22:50:19 +0000509 /* Be able to detect if the cfg file has no BOOT_FROM tag */
510 imxhdr->flash_offset = FLASH_OFFSET_UNDEFINED;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000511 set_hdr_func(imxhdr);
Stefano Babic8edcde52010-01-20 18:19:10 +0100512
513 /* Parse dcd configuration file */
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000514 dcd_len = parse_cfg_file(imxhdr, params->imagename);
Stefano Babic8edcde52010-01-20 18:19:10 +0100515
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000516 /* Set the imx header */
Troy Kiskyad0826d2012-10-03 15:47:08 +0000517 (*set_imx_hdr)(imxhdr, dcd_len, params->ep, imxhdr->flash_offset);
Fabio Estevam1411fb32013-01-03 08:24:33 +0000518
519 /*
520 * ROM bug alert
521 * mx53 only loads 512 byte multiples.
522 * The remaining fraction of a block bytes would
523 * not be loaded.
524 */
525 *header_size_ptr = ROUND(sbuf->st_size + imxhdr->flash_offset, 512);
Stefano Babic8edcde52010-01-20 18:19:10 +0100526}
527
528int imximage_check_params(struct mkimage_params *params)
529{
530 if (!params)
531 return CFG_INVALID;
532 if (!strlen(params->imagename)) {
533 fprintf(stderr, "Error: %s - Configuration file not specified, "
534 "it is needed for imximage generation\n",
535 params->cmdname);
536 return CFG_INVALID;
537 }
538 /*
539 * Check parameters:
540 * XIP is not allowed and verify that incompatible
541 * parameters are not sent at the same time
542 * For example, if list is required a data image must not be provided
543 */
544 return (params->dflag && (params->fflag || params->lflag)) ||
545 (params->fflag && (params->dflag || params->lflag)) ||
546 (params->lflag && (params->dflag || params->fflag)) ||
547 (params->xflag) || !(strlen(params->imagename));
548}
549
550/*
551 * imximage parameters
552 */
553static struct image_type_params imximage_params = {
Fabio Estevam6e083852013-01-02 08:48:34 +0000554 .name = "Freescale i.MX Boot Image support",
Stefano Babic8edcde52010-01-20 18:19:10 +0100555 .header_size = sizeof(struct imx_header),
556 .hdr = (void *)&imximage_header,
557 .check_image_type = imximage_check_image_types,
558 .verify_header = imximage_verify_header,
559 .print_header = imximage_print_header,
560 .set_header = imximage_set_header,
561 .check_params = imximage_check_params,
562};
563
564void init_imx_image_type(void)
565{
566 mkimage_register(&imximage_params);
567}