blob: cab208b5ac889f5e5235e1ca79dc5c9c2c82f205 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Stefano Babic8edcde52010-01-20 18:19:10 +010010 */
11
Stefano Babic8edcde52010-01-20 18:19:10 +010012#include "mkimage.h"
13#include <image.h>
14#include "imximage.h"
15
16/*
17 * Supported commands for configuration file
18 */
19static table_entry_t imximage_cmds[] = {
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000020 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
Marek Vasut6cb83822013-04-25 10:16:02 +000021 {CMD_BOOT_OFFSET, "BOOT_OFFSET", "Boot offset", },
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000022 {CMD_DATA, "DATA", "Reg Write Data", },
23 {CMD_IMAGE_VERSION, "IMAGE_VERSION", "image version", },
24 {-1, "", "", },
Stefano Babic8edcde52010-01-20 18:19:10 +010025};
26
27/*
28 * Supported Boot options for configuration file
29 * this is needed to set the correct flash offset
30 */
31static table_entry_t imximage_bootops[] = {
Stefano Babic8edcde52010-01-20 18:19:10 +010032 {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",},
Dirk Behmebd258642012-01-11 23:28:32 +000033 {FLASH_OFFSET_NAND, "nand", "NAND Flash", },
Dirk Behme19b409c2012-01-11 23:28:31 +000034 {FLASH_OFFSET_NOR, "nor", "NOR Flash", },
35 {FLASH_OFFSET_SATA, "sata", "SATA Disk", },
Dirk Behmebd258642012-01-11 23:28:32 +000036 {FLASH_OFFSET_SD, "sd", "SD Card", },
37 {FLASH_OFFSET_SPI, "spi", "SPI Flash", },
Stefano Babic8edcde52010-01-20 18:19:10 +010038 {-1, "", "Invalid", },
39};
40
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000041/*
42 * IMXIMAGE version definition for i.MX chips
43 */
44static table_entry_t imximage_versions[] = {
45 {IMXIMAGE_V1, "", " (i.MX25/35/51 compatible)", },
Dirk Behme19b409c2012-01-11 23:28:31 +000046 {IMXIMAGE_V2, "", " (i.MX53/6 compatible)", },
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000047 {-1, "", " (Invalid)", },
48};
Stefano Babic8edcde52010-01-20 18:19:10 +010049
50static struct imx_header imximage_header;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000051static uint32_t imximage_version;
52
53static set_dcd_val_t set_dcd_val;
54static set_dcd_rst_t set_dcd_rst;
55static set_imx_hdr_t set_imx_hdr;
Troy Kisky4d5fa982012-10-03 15:47:03 +000056static uint32_t max_dcd_entries;
Troy Kisky24331982012-10-03 15:47:07 +000057static uint32_t *header_size_ptr;
Stefano Babic8edcde52010-01-20 18:19:10 +010058
59static uint32_t get_cfg_value(char *token, char *name, int linenr)
60{
61 char *endptr;
62 uint32_t value;
63
64 errno = 0;
65 value = strtoul(token, &endptr, 16);
66 if (errno || (token == endptr)) {
67 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
68 name, linenr, token);
69 exit(EXIT_FAILURE);
70 }
71 return value;
72}
73
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000074static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
Stefano Babic8edcde52010-01-20 18:19:10 +010075{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000076 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
77 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
78 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
79 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
80
81 /* Try to detect V1 */
82 if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) &&
83 (hdr_v1->dcd_table.preamble.barker == DCD_BARKER))
84 return IMXIMAGE_V1;
85
86 /* Try to detect V2 */
87 if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
88 (hdr_v2->dcd_table.header.tag == DCD_HEADER_TAG))
89 return IMXIMAGE_V2;
90
91 return IMXIMAGE_VER_INVALID;
Stefano Babic8edcde52010-01-20 18:19:10 +010092}
93
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000094static void err_imximage_version(int version)
Stefano Babic8edcde52010-01-20 18:19:10 +010095{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000096 fprintf(stderr,
97 "Error: Unsupported imximage version:%d\n", version);
Stefano Babic8edcde52010-01-20 18:19:10 +010098
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000099 exit(EXIT_FAILURE);
Stefano Babic8edcde52010-01-20 18:19:10 +0100100}
101
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000102static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno,
103 int fld, uint32_t value, uint32_t off)
Stefano Babic8edcde52010-01-20 18:19:10 +0100104{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000105 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
Stefano Babic8edcde52010-01-20 18:19:10 +0100106
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000107 switch (fld) {
108 case CFG_REG_SIZE:
109 /* Byte, halfword, word */
110 if ((value != 1) && (value != 2) && (value != 4)) {
111 fprintf(stderr, "Error: %s[%d] - "
112 "Invalid register size " "(%d)\n",
113 name, lineno, value);
114 exit(EXIT_FAILURE);
115 }
116 dcd_v1->addr_data[off].type = value;
117 break;
118 case CFG_REG_ADDRESS:
119 dcd_v1->addr_data[off].addr = value;
120 break;
121 case CFG_REG_VALUE:
122 dcd_v1->addr_data[off].value = value;
123 break;
124 default:
125 break;
126
127 }
128}
129
130static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno,
131 int fld, uint32_t value, uint32_t off)
132{
133 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
134
135 switch (fld) {
136 case CFG_REG_ADDRESS:
137 dcd_v2->addr_data[off].addr = cpu_to_be32(value);
138 break;
139 case CFG_REG_VALUE:
140 dcd_v2->addr_data[off].value = cpu_to_be32(value);
141 break;
142 default:
143 break;
144
145 }
146}
147
148/*
149 * Complete setting up the rest field of DCD of V1
150 * such as barker code and DCD data length.
151 */
152static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len,
153 char *name, int lineno)
154{
155 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
156
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000157 dcd_v1->preamble.barker = DCD_BARKER;
158 dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
Stefano Babic8edcde52010-01-20 18:19:10 +0100159}
160
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000161/*
162 * Complete setting up the reset field of DCD of V2
163 * such as DCD tag, version, length, etc.
164 */
165static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len,
166 char *name, int lineno)
167{
168 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
169
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000170 dcd_v2->header.tag = DCD_HEADER_TAG;
171 dcd_v2->header.length = cpu_to_be16(
172 dcd_len * sizeof(dcd_addr_data_t) + 8);
173 dcd_v2->header.version = DCD_VERSION;
174 dcd_v2->write_dcd_command.tag = DCD_COMMAND_TAG;
175 dcd_v2->write_dcd_command.length = cpu_to_be16(
176 dcd_len * sizeof(dcd_addr_data_t) + 4);
177 dcd_v2->write_dcd_command.param = DCD_COMMAND_PARAM;
178}
179
180static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kiskyad0826d2012-10-03 15:47:08 +0000181 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000182{
183 imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1;
184 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
185 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
Troy Kiskyab857f22012-10-03 15:47:09 +0000186 uint32_t hdr_base;
Troy Kisky24331982012-10-03 15:47:07 +0000187 uint32_t header_length = (((char *)&dcd_v1->addr_data[dcd_len].addr)
188 - ((char *)imxhdr));
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000189
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000190 /* Set magic number */
191 fhdr_v1->app_code_barker = APP_CODE_BARKER;
192
Troy Kiskyab857f22012-10-03 15:47:09 +0000193 hdr_base = entry_point - sizeof(struct imx_header);
194 fhdr_v1->app_dest_ptr = hdr_base - flash_offset;
Troy Kiskyad0826d2012-10-03 15:47:08 +0000195 fhdr_v1->app_code_jump_vector = entry_point;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000196
Troy Kiskyab857f22012-10-03 15:47:09 +0000197 fhdr_v1->dcd_ptr_ptr = hdr_base + offsetof(flash_header_v1_t, dcd_ptr);
198 fhdr_v1->dcd_ptr = hdr_base + offsetof(imx_header_v1_t, dcd_table);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000199
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000200 /* Security feature are not supported */
201 fhdr_v1->app_code_csf = 0;
202 fhdr_v1->super_root_key = 0;
Troy Kisky24331982012-10-03 15:47:07 +0000203 header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000204}
205
206static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kiskyad0826d2012-10-03 15:47:08 +0000207 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000208{
209 imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
210 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
Troy Kiskyab857f22012-10-03 15:47:09 +0000211 uint32_t hdr_base;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000212
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000213 /* Set magic number */
214 fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
215 fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
216 fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
217
Troy Kiskyad0826d2012-10-03 15:47:08 +0000218 fhdr_v2->entry = entry_point;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000219 fhdr_v2->reserved1 = fhdr_v2->reserved2 = 0;
Troy Kiskyab857f22012-10-03 15:47:09 +0000220 fhdr_v2->self = hdr_base = entry_point - sizeof(struct imx_header);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000221
Troy Kiskyab857f22012-10-03 15:47:09 +0000222 fhdr_v2->dcd_ptr = hdr_base + offsetof(imx_header_v2_t, dcd_table);
223 fhdr_v2->boot_data_ptr = hdr_base
224 + offsetof(imx_header_v2_t, boot_data);
225 hdr_v2->boot_data.start = hdr_base - flash_offset;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000226
227 /* Security feature are not supported */
228 fhdr_v2->csf = 0;
Troy Kisky24331982012-10-03 15:47:07 +0000229 header_size_ptr = &hdr_v2->boot_data.size;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000230}
231
232static void set_hdr_func(struct imx_header *imxhdr)
233{
234 switch (imximage_version) {
235 case IMXIMAGE_V1:
236 set_dcd_val = set_dcd_val_v1;
237 set_dcd_rst = set_dcd_rst_v1;
238 set_imx_hdr = set_imx_hdr_v1;
Troy Kisky4d5fa982012-10-03 15:47:03 +0000239 max_dcd_entries = MAX_HW_CFG_SIZE_V1;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000240 break;
241 case IMXIMAGE_V2:
242 set_dcd_val = set_dcd_val_v2;
243 set_dcd_rst = set_dcd_rst_v2;
244 set_imx_hdr = set_imx_hdr_v2;
Troy Kisky4d5fa982012-10-03 15:47:03 +0000245 max_dcd_entries = MAX_HW_CFG_SIZE_V2;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000246 break;
247 default:
248 err_imximage_version(imximage_version);
249 break;
250 }
251}
252
253static void print_hdr_v1(struct imx_header *imx_hdr)
254{
255 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
256 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
257 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
258 uint32_t size, length, ver;
259
260 size = dcd_v1->preamble.length;
261 if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
262 fprintf(stderr,
263 "Error: Image corrupt DCD size %d exceed maximum %d\n",
264 (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
265 MAX_HW_CFG_SIZE_V1);
266 exit(EXIT_FAILURE);
267 }
268
269 length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
270 ver = detect_imximage_version(imx_hdr);
271
272 printf("Image Type: Freescale IMX Boot Image\n");
273 printf("Image Ver: %x", ver);
274 printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
275 printf("Data Size: ");
276 genimg_print_size(dcd_v1->addr_data[length].type);
277 printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
278 printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
279}
280
281static void print_hdr_v2(struct imx_header *imx_hdr)
282{
283 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
284 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
285 dcd_v2_t *dcd_v2 = &hdr_v2->dcd_table;
286 uint32_t size, version;
287
288 size = be16_to_cpu(dcd_v2->header.length) - 8;
289 if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
290 fprintf(stderr,
291 "Error: Image corrupt DCD size %d exceed maximum %d\n",
292 (uint32_t)(size / sizeof(dcd_addr_data_t)),
293 MAX_HW_CFG_SIZE_V2);
294 exit(EXIT_FAILURE);
295 }
296
297 version = detect_imximage_version(imx_hdr);
298
299 printf("Image Type: Freescale IMX Boot Image\n");
300 printf("Image Ver: %x", version);
301 printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
302 printf("Data Size: ");
303 genimg_print_size(hdr_v2->boot_data.size);
304 printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
305 printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
306}
307
308static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
309 char *name, int lineno, int fld, int dcd_len)
310{
311 int value;
312 static int cmd_ver_first = ~0;
313
314 switch (cmd) {
315 case CMD_IMAGE_VERSION:
316 imximage_version = get_cfg_value(token, name, lineno);
317 if (cmd_ver_first == 0) {
318 fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
319 "command need be the first before other "
320 "valid command in the file\n", name, lineno);
321 exit(EXIT_FAILURE);
322 }
323 cmd_ver_first = 1;
324 set_hdr_func(imxhdr);
325 break;
326 case CMD_BOOT_FROM:
327 imxhdr->flash_offset = get_table_entry_id(imximage_bootops,
328 "imximage boot option", token);
329 if (imxhdr->flash_offset == -1) {
330 fprintf(stderr, "Error: %s[%d] -Invalid boot device"
331 "(%s)\n", name, lineno, token);
332 exit(EXIT_FAILURE);
333 }
334 if (unlikely(cmd_ver_first != 1))
335 cmd_ver_first = 0;
336 break;
Marek Vasut6cb83822013-04-25 10:16:02 +0000337 case CMD_BOOT_OFFSET:
338 imxhdr->flash_offset = get_cfg_value(token, name, lineno);
339 if (unlikely(cmd_ver_first != 1))
340 cmd_ver_first = 0;
341 break;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000342 case CMD_DATA:
343 value = get_cfg_value(token, name, lineno);
344 (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
345 if (unlikely(cmd_ver_first != 1))
346 cmd_ver_first = 0;
347 break;
348 }
349}
350
351static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
352 char *token, char *name, int lineno, int fld, int *dcd_len)
353{
354 int value;
355
356 switch (fld) {
357 case CFG_COMMAND:
358 *cmd = get_table_entry_id(imximage_cmds,
359 "imximage commands", token);
360 if (*cmd < 0) {
361 fprintf(stderr, "Error: %s[%d] - Invalid command"
362 "(%s)\n", name, lineno, token);
363 exit(EXIT_FAILURE);
364 }
365 break;
366 case CFG_REG_SIZE:
367 parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
368 break;
369 case CFG_REG_ADDRESS:
370 case CFG_REG_VALUE:
371 if (*cmd != CMD_DATA)
372 return;
373
374 value = get_cfg_value(token, name, lineno);
375 (*set_dcd_val)(imxhdr, name, lineno, fld, value, *dcd_len);
376
Troy Kisky4d5fa982012-10-03 15:47:03 +0000377 if (fld == CFG_REG_VALUE) {
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000378 (*dcd_len)++;
Troy Kisky4d5fa982012-10-03 15:47:03 +0000379 if (*dcd_len > max_dcd_entries) {
380 fprintf(stderr, "Error: %s[%d] -"
381 "DCD table exceeds maximum size(%d)\n",
382 name, lineno, max_dcd_entries);
383 exit(EXIT_FAILURE);
384 }
385 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000386 break;
387 default:
388 break;
389 }
390}
391static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
Stefano Babic8edcde52010-01-20 18:19:10 +0100392{
393 FILE *fd = NULL;
394 char *line = NULL;
395 char *token, *saveptr1, *saveptr2;
396 int lineno = 0;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000397 int fld;
Kim Phillips0ad22702010-02-22 19:37:56 -0600398 size_t len;
Stefano Babic8edcde52010-01-20 18:19:10 +0100399 int dcd_len = 0;
Stefano Babic8edcde52010-01-20 18:19:10 +0100400 int32_t cmd;
401
402 fd = fopen(name, "r");
403 if (fd == 0) {
404 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
405 exit(EXIT_FAILURE);
406 }
407
408 /* Very simple parsing, line starting with # are comments
409 * and are dropped
410 */
411 while ((getline(&line, &len, fd)) > 0) {
412 lineno++;
413
414 token = strtok_r(line, "\r\n", &saveptr1);
415 if (token == NULL)
416 continue;
417
418 /* Check inside the single line */
419 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
420 line = token; ; line = NULL, fld++) {
421 token = strtok_r(line, " \t", &saveptr2);
422 if (token == NULL)
423 break;
424
425 /* Drop all text starting with '#' as comments */
426 if (token[0] == '#')
427 break;
428
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000429 parse_cfg_fld(imxhdr, &cmd, token, name,
430 lineno, fld, &dcd_len);
Stefano Babic8edcde52010-01-20 18:19:10 +0100431 }
432
Stefano Babic8edcde52010-01-20 18:19:10 +0100433 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000434
435 (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
Stefano Babic8edcde52010-01-20 18:19:10 +0100436 fclose(fd);
437
Troy Kisky8d8cc822012-10-03 15:47:05 +0000438 /* Exit if there is no BOOT_FROM field specifying the flash_offset */
439 if (imxhdr->flash_offset == FLASH_OFFSET_UNDEFINED) {
440 fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name);
441 exit(EXIT_FAILURE);
442 }
Stefano Babic5b28e912010-02-05 15:16:02 +0100443 return dcd_len;
Stefano Babic8edcde52010-01-20 18:19:10 +0100444}
445
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000446
447static int imximage_check_image_types(uint8_t type)
448{
449 if (type == IH_TYPE_IMXIMAGE)
450 return EXIT_SUCCESS;
451 else
452 return EXIT_FAILURE;
453}
454
455static int imximage_verify_header(unsigned char *ptr, int image_size,
456 struct mkimage_params *params)
457{
458 struct imx_header *imx_hdr = (struct imx_header *) ptr;
459
460 if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
461 return -FDT_ERR_BADSTRUCTURE;
462
463 return 0;
464}
465
466static void imximage_print_header(const void *ptr)
467{
468 struct imx_header *imx_hdr = (struct imx_header *) ptr;
469 uint32_t version = detect_imximage_version(imx_hdr);
470
471 switch (version) {
472 case IMXIMAGE_V1:
473 print_hdr_v1(imx_hdr);
474 break;
475 case IMXIMAGE_V2:
476 print_hdr_v2(imx_hdr);
477 break;
478 default:
479 err_imximage_version(version);
480 break;
481 }
482}
483
Stefano Babic8edcde52010-01-20 18:19:10 +0100484static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
485 struct mkimage_params *params)
486{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000487 struct imx_header *imxhdr = (struct imx_header *)ptr;
488 uint32_t dcd_len;
Stefano Babic8edcde52010-01-20 18:19:10 +0100489
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000490 /*
491 * In order to not change the old imx cfg file
492 * by adding VERSION command into it, here need
493 * set up function ptr group to V1 by default.
494 */
495 imximage_version = IMXIMAGE_V1;
Dirk Behme49d3e272012-02-22 22:50:19 +0000496 /* Be able to detect if the cfg file has no BOOT_FROM tag */
497 imxhdr->flash_offset = FLASH_OFFSET_UNDEFINED;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000498 set_hdr_func(imxhdr);
Stefano Babic8edcde52010-01-20 18:19:10 +0100499
500 /* Parse dcd configuration file */
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000501 dcd_len = parse_cfg_file(imxhdr, params->imagename);
Stefano Babic8edcde52010-01-20 18:19:10 +0100502
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000503 /* Set the imx header */
Troy Kiskyad0826d2012-10-03 15:47:08 +0000504 (*set_imx_hdr)(imxhdr, dcd_len, params->ep, imxhdr->flash_offset);
Fabio Estevam1411fb32013-01-03 08:24:33 +0000505
506 /*
507 * ROM bug alert
Marek Vasut895d9962013-04-21 05:52:22 +0000508 *
509 * MX53 only loads 512 byte multiples in case of SD boot.
510 * MX53 only loads NAND page multiples in case of NAND boot and
511 * supports up to 4096 byte large pages, thus align to 4096.
512 *
513 * The remaining fraction of a block bytes would not be loaded!
Fabio Estevam1411fb32013-01-03 08:24:33 +0000514 */
Marek Vasut895d9962013-04-21 05:52:22 +0000515 *header_size_ptr = ROUND(sbuf->st_size + imxhdr->flash_offset, 4096);
Stefano Babic8edcde52010-01-20 18:19:10 +0100516}
517
518int imximage_check_params(struct mkimage_params *params)
519{
520 if (!params)
521 return CFG_INVALID;
522 if (!strlen(params->imagename)) {
523 fprintf(stderr, "Error: %s - Configuration file not specified, "
524 "it is needed for imximage generation\n",
525 params->cmdname);
526 return CFG_INVALID;
527 }
528 /*
529 * Check parameters:
530 * XIP is not allowed and verify that incompatible
531 * parameters are not sent at the same time
532 * For example, if list is required a data image must not be provided
533 */
534 return (params->dflag && (params->fflag || params->lflag)) ||
535 (params->fflag && (params->dflag || params->lflag)) ||
536 (params->lflag && (params->dflag || params->fflag)) ||
537 (params->xflag) || !(strlen(params->imagename));
538}
539
540/*
541 * imximage parameters
542 */
543static struct image_type_params imximage_params = {
Fabio Estevam6e083852013-01-02 08:48:34 +0000544 .name = "Freescale i.MX Boot Image support",
Stefano Babic8edcde52010-01-20 18:19:10 +0100545 .header_size = sizeof(struct imx_header),
546 .hdr = (void *)&imximage_header,
547 .check_image_type = imximage_check_image_types,
548 .verify_header = imximage_verify_header,
549 .print_header = imximage_print_header,
550 .set_header = imximage_set_header,
551 .check_params = imximage_check_params,
552};
553
554void init_imx_image_type(void)
555{
556 mkimage_register(&imximage_params);
557}