blob: c9e42ec5cab54e18a60af4c44c74b7e9c72e8b06 [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 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02009 * SPDX-License-Identifier: GPL-2.0+
Stefano Babic7b07f092010-01-20 18:19:10 +010010 */
11
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070012#include "imagetool.h"
Stefano Babic7b07f092010-01-20 18:19:10 +010013#include <image.h>
14#include "imximage.h"
15
Stefano Babic4aa97492013-06-27 11:42:38 +020016#define UNDEFINED 0xFFFFFFFF
17
Stefano Babic7b07f092010-01-20 18:19:10 +010018/*
19 * Supported commands for configuration file
20 */
21static table_entry_t imximage_cmds[] = {
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000022 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
Marek Vasutd45fd732013-04-25 10:16:02 +000023 {CMD_BOOT_OFFSET, "BOOT_OFFSET", "Boot offset", },
Adrian Alonso73e57322015-07-20 19:04:55 -050024 {CMD_WRITE_DATA, "DATA", "Reg Write Data", },
25 {CMD_WRITE_CLR_BIT, "CLR_BIT", "Reg clear bit", },
26 {CMD_CHECK_BITS_SET, "CHECK_BITS_SET", "Reg Check bits set", },
27 {CMD_CHECK_BITS_CLR, "CHECK_BITS_CLR", "Reg Check bits clr", },
Stefano Babic4aa97492013-06-27 11:42:38 +020028 {CMD_CSF, "CSF", "Command Sequence File", },
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000029 {CMD_IMAGE_VERSION, "IMAGE_VERSION", "image version", },
Peng Fan334de962016-10-11 14:29:09 +080030 {CMD_PLUGIN, "PLUGIN", "file plugin_addr", },
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000031 {-1, "", "", },
Stefano Babic7b07f092010-01-20 18:19:10 +010032};
33
34/*
35 * Supported Boot options for configuration file
36 * this is needed to set the correct flash offset
37 */
Stefano Babicdc39a3e2013-06-26 23:50:06 +020038static table_entry_t imximage_boot_offset[] = {
Stefano Babic7b07f092010-01-20 18:19:10 +010039 {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",},
Dirk Behme40a9c462012-01-11 23:28:32 +000040 {FLASH_OFFSET_NAND, "nand", "NAND Flash", },
Dirk Behmedfbf6ce2012-01-11 23:28:31 +000041 {FLASH_OFFSET_NOR, "nor", "NOR Flash", },
42 {FLASH_OFFSET_SATA, "sata", "SATA Disk", },
Dirk Behme40a9c462012-01-11 23:28:32 +000043 {FLASH_OFFSET_SD, "sd", "SD Card", },
44 {FLASH_OFFSET_SPI, "spi", "SPI Flash", },
Ye.Lif16cde02015-01-13 15:53:06 +080045 {FLASH_OFFSET_QSPI, "qspi", "QSPI NOR Flash",},
Stefano Babic7b07f092010-01-20 18:19:10 +010046 {-1, "", "Invalid", },
47};
48
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000049/*
Stefano Babicdc39a3e2013-06-26 23:50:06 +020050 * Supported Boot options for configuration file
51 * this is needed to determine the initial load size
52 */
53static table_entry_t imximage_boot_loadsize[] = {
54 {FLASH_LOADSIZE_ONENAND, "onenand", "OneNAND Flash",},
55 {FLASH_LOADSIZE_NAND, "nand", "NAND Flash", },
56 {FLASH_LOADSIZE_NOR, "nor", "NOR Flash", },
57 {FLASH_LOADSIZE_SATA, "sata", "SATA Disk", },
58 {FLASH_LOADSIZE_SD, "sd", "SD Card", },
59 {FLASH_LOADSIZE_SPI, "spi", "SPI Flash", },
Ye.Lif16cde02015-01-13 15:53:06 +080060 {FLASH_LOADSIZE_QSPI, "qspi", "QSPI NOR Flash",},
Stefano Babicdc39a3e2013-06-26 23:50:06 +020061 {-1, "", "Invalid", },
62};
63
64/*
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000065 * IMXIMAGE version definition for i.MX chips
66 */
67static table_entry_t imximage_versions[] = {
68 {IMXIMAGE_V1, "", " (i.MX25/35/51 compatible)", },
Adrian Alonso73e57322015-07-20 19:04:55 -050069 {IMXIMAGE_V2, "", " (i.MX53/6/7 compatible)", },
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000070 {-1, "", " (Invalid)", },
71};
Stefano Babic7b07f092010-01-20 18:19:10 +010072
73static struct imx_header imximage_header;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000074static uint32_t imximage_version;
Stefano Babic4aa97492013-06-27 11:42:38 +020075/*
76 * Image Vector Table Offset
77 * Initialized to a wrong not 4-bytes aligned address to
78 * check if it is was set by the cfg file.
79 */
80static uint32_t imximage_ivt_offset = UNDEFINED;
81static uint32_t imximage_csf_size = UNDEFINED;
Stefano Babicdc39a3e2013-06-26 23:50:06 +020082/* Initial Load Region Size */
83static uint32_t imximage_init_loadsize;
Peng Fan334de962016-10-11 14:29:09 +080084static uint32_t imximage_iram_free_start;
85static uint32_t imximage_plugin_size;
86static uint32_t plugin_image;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000087
88static set_dcd_val_t set_dcd_val;
Adrian Alonso73e57322015-07-20 19:04:55 -050089static set_dcd_param_t set_dcd_param;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000090static set_dcd_rst_t set_dcd_rst;
91static set_imx_hdr_t set_imx_hdr;
Troy Kisky9bd5b852012-10-03 15:47:03 +000092static uint32_t max_dcd_entries;
Troy Kisky7b922cb2012-10-03 15:47:07 +000093static uint32_t *header_size_ptr;
Stefano Babic4aa97492013-06-27 11:42:38 +020094static uint32_t *csf_ptr;
Stefano Babic7b07f092010-01-20 18:19:10 +010095
96static uint32_t get_cfg_value(char *token, char *name, int linenr)
97{
98 char *endptr;
99 uint32_t value;
100
101 errno = 0;
102 value = strtoul(token, &endptr, 16);
103 if (errno || (token == endptr)) {
104 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
105 name, linenr, token);
106 exit(EXIT_FAILURE);
107 }
108 return value;
109}
110
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000111static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
Stefano Babic7b07f092010-01-20 18:19:10 +0100112{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000113 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
114 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
115 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
116 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
117
118 /* Try to detect V1 */
119 if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) &&
120 (hdr_v1->dcd_table.preamble.barker == DCD_BARKER))
121 return IMXIMAGE_V1;
122
123 /* Try to detect V2 */
124 if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
Peng Fan334de962016-10-11 14:29:09 +0800125 (hdr_v2->data.dcd_table.header.tag == DCD_HEADER_TAG))
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000126 return IMXIMAGE_V2;
127
Peng Fan334de962016-10-11 14:29:09 +0800128 if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
129 hdr_v2->boot_data.plugin)
130 return IMXIMAGE_V2;
131
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000132 return IMXIMAGE_VER_INVALID;
Stefano Babic7b07f092010-01-20 18:19:10 +0100133}
134
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000135static void err_imximage_version(int version)
Stefano Babic7b07f092010-01-20 18:19:10 +0100136{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000137 fprintf(stderr,
138 "Error: Unsupported imximage version:%d\n", version);
Stefano Babic7b07f092010-01-20 18:19:10 +0100139
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000140 exit(EXIT_FAILURE);
141}
Stefano Babic7b07f092010-01-20 18:19:10 +0100142
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000143static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno,
144 int fld, uint32_t value, uint32_t off)
145{
146 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
Stefano Babic7b07f092010-01-20 18:19:10 +0100147
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000148 switch (fld) {
149 case CFG_REG_SIZE:
150 /* Byte, halfword, word */
151 if ((value != 1) && (value != 2) && (value != 4)) {
152 fprintf(stderr, "Error: %s[%d] - "
153 "Invalid register size " "(%d)\n",
154 name, lineno, value);
155 exit(EXIT_FAILURE);
156 }
157 dcd_v1->addr_data[off].type = value;
158 break;
159 case CFG_REG_ADDRESS:
160 dcd_v1->addr_data[off].addr = value;
161 break;
162 case CFG_REG_VALUE:
163 dcd_v1->addr_data[off].value = value;
164 break;
165 default:
166 break;
Stefano Babic7b07f092010-01-20 18:19:10 +0100167
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000168 }
Stefano Babic7b07f092010-01-20 18:19:10 +0100169}
170
Troy Kiskyf575ab42015-09-14 18:06:31 -0700171static struct dcd_v2_cmd *gd_last_cmd;
172
Adrian Alonso73e57322015-07-20 19:04:55 -0500173static void set_dcd_param_v2(struct imx_header *imxhdr, uint32_t dcd_len,
174 int32_t cmd)
175{
Peng Fan334de962016-10-11 14:29:09 +0800176 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.data.dcd_table;
Troy Kiskyf575ab42015-09-14 18:06:31 -0700177 struct dcd_v2_cmd *d = gd_last_cmd;
178 struct dcd_v2_cmd *d2;
179 int len;
180
181 if (!d)
182 d = &dcd_v2->dcd_cmd;
183 d2 = d;
184 len = be16_to_cpu(d->write_dcd_command.length);
185 if (len > 4)
186 d2 = (struct dcd_v2_cmd *)(((char *)d) + len);
Adrian Alonso73e57322015-07-20 19:04:55 -0500187
188 switch (cmd) {
189 case CMD_WRITE_DATA:
Troy Kiskyf575ab42015-09-14 18:06:31 -0700190 if ((d->write_dcd_command.tag == DCD_WRITE_DATA_COMMAND_TAG) &&
191 (d->write_dcd_command.param == DCD_WRITE_DATA_PARAM))
192 break;
193 d = d2;
194 d->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG;
195 d->write_dcd_command.length = cpu_to_be16(4);
196 d->write_dcd_command.param = DCD_WRITE_DATA_PARAM;
Adrian Alonso73e57322015-07-20 19:04:55 -0500197 break;
198 case CMD_WRITE_CLR_BIT:
Troy Kiskyf575ab42015-09-14 18:06:31 -0700199 if ((d->write_dcd_command.tag == DCD_WRITE_DATA_COMMAND_TAG) &&
200 (d->write_dcd_command.param == DCD_WRITE_CLR_BIT_PARAM))
201 break;
202 d = d2;
203 d->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG;
204 d->write_dcd_command.length = cpu_to_be16(4);
205 d->write_dcd_command.param = DCD_WRITE_CLR_BIT_PARAM;
Adrian Alonso73e57322015-07-20 19:04:55 -0500206 break;
207 /*
208 * Check data command only supports one entry,
Adrian Alonso73e57322015-07-20 19:04:55 -0500209 */
210 case CMD_CHECK_BITS_SET:
Troy Kiskyf575ab42015-09-14 18:06:31 -0700211 d = d2;
212 d->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG;
213 d->write_dcd_command.length = cpu_to_be16(4);
214 d->write_dcd_command.param = DCD_CHECK_BITS_SET_PARAM;
Adrian Alonso73e57322015-07-20 19:04:55 -0500215 break;
216 case CMD_CHECK_BITS_CLR:
Troy Kiskyf575ab42015-09-14 18:06:31 -0700217 d = d2;
218 d->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG;
219 d->write_dcd_command.length = cpu_to_be16(4);
Adrian Alonso6963b332016-05-02 10:29:14 -0500220 d->write_dcd_command.param = DCD_CHECK_BITS_CLR_PARAM;
Adrian Alonso73e57322015-07-20 19:04:55 -0500221 break;
222 default:
223 break;
224 }
Troy Kiskyf575ab42015-09-14 18:06:31 -0700225 gd_last_cmd = d;
Adrian Alonso73e57322015-07-20 19:04:55 -0500226}
227
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000228static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno,
229 int fld, uint32_t value, uint32_t off)
Stefano Babic7b07f092010-01-20 18:19:10 +0100230{
Troy Kiskyf575ab42015-09-14 18:06:31 -0700231 struct dcd_v2_cmd *d = gd_last_cmd;
232 int len;
233
234 len = be16_to_cpu(d->write_dcd_command.length);
235 off = (len - 4) >> 3;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000236
237 switch (fld) {
238 case CFG_REG_ADDRESS:
Troy Kiskyf575ab42015-09-14 18:06:31 -0700239 d->addr_data[off].addr = cpu_to_be32(value);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000240 break;
241 case CFG_REG_VALUE:
Troy Kiskyf575ab42015-09-14 18:06:31 -0700242 d->addr_data[off].value = cpu_to_be32(value);
243 off++;
244 d->write_dcd_command.length = cpu_to_be16((off << 3) + 4);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000245 break;
246 default:
247 break;
248
249 }
250}
251
252/*
253 * Complete setting up the rest field of DCD of V1
254 * such as barker code and DCD data length.
255 */
256static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len,
257 char *name, int lineno)
258{
259 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
260
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000261 dcd_v1->preamble.barker = DCD_BARKER;
262 dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
263}
264
265/*
266 * Complete setting up the reset field of DCD of V2
267 * such as DCD tag, version, length, etc.
268 */
269static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len,
270 char *name, int lineno)
271{
Peng Fan334de962016-10-11 14:29:09 +0800272 if (!imxhdr->header.hdr_v2.boot_data.plugin) {
273 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.data.dcd_table;
274 struct dcd_v2_cmd *d = gd_last_cmd;
275 int len;
Troy Kiskyf575ab42015-09-14 18:06:31 -0700276
Peng Fan334de962016-10-11 14:29:09 +0800277 if (!d)
278 d = &dcd_v2->dcd_cmd;
279 len = be16_to_cpu(d->write_dcd_command.length);
280 if (len > 4)
281 d = (struct dcd_v2_cmd *)(((char *)d) + len);
Troy Kiskyf575ab42015-09-14 18:06:31 -0700282
Peng Fan334de962016-10-11 14:29:09 +0800283 len = (char *)d - (char *)&dcd_v2->header;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000284
Peng Fan334de962016-10-11 14:29:09 +0800285 dcd_v2->header.tag = DCD_HEADER_TAG;
286 dcd_v2->header.length = cpu_to_be16(len);
287 dcd_v2->header.version = DCD_VERSION;
288 }
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000289}
290
291static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kisky7bb92202012-10-03 15:47:08 +0000292 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000293{
294 imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1;
295 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
296 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
Troy Kisky2d7c29f2012-10-03 15:47:09 +0000297 uint32_t hdr_base;
Troy Kisky7b922cb2012-10-03 15:47:07 +0000298 uint32_t header_length = (((char *)&dcd_v1->addr_data[dcd_len].addr)
299 - ((char *)imxhdr));
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000300
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000301 /* Set magic number */
302 fhdr_v1->app_code_barker = APP_CODE_BARKER;
303
Stefano Babicdc39a3e2013-06-26 23:50:06 +0200304 /* TODO: check i.MX image V1 handling, for now use 'old' style */
305 hdr_base = entry_point - 4096;
Troy Kisky2d7c29f2012-10-03 15:47:09 +0000306 fhdr_v1->app_dest_ptr = hdr_base - flash_offset;
Troy Kisky7bb92202012-10-03 15:47:08 +0000307 fhdr_v1->app_code_jump_vector = entry_point;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000308
Troy Kisky2d7c29f2012-10-03 15:47:09 +0000309 fhdr_v1->dcd_ptr_ptr = hdr_base + offsetof(flash_header_v1_t, dcd_ptr);
310 fhdr_v1->dcd_ptr = hdr_base + offsetof(imx_header_v1_t, dcd_table);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000311
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000312 /* Security feature are not supported */
313 fhdr_v1->app_code_csf = 0;
314 fhdr_v1->super_root_key = 0;
Troy Kisky7b922cb2012-10-03 15:47:07 +0000315 header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000316}
317
318static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kisky7bb92202012-10-03 15:47:08 +0000319 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000320{
321 imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
322 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
Troy Kisky2d7c29f2012-10-03 15:47:09 +0000323 uint32_t hdr_base;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000324
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000325 /* Set magic number */
326 fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
327 fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
328 fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
329
Peng Fan334de962016-10-11 14:29:09 +0800330 if (!hdr_v2->boot_data.plugin) {
331 fhdr_v2->entry = entry_point;
332 fhdr_v2->reserved1 = 0;
333 fhdr_v2->reserved1 = 0;
334 hdr_base = entry_point - imximage_init_loadsize +
335 flash_offset;
336 fhdr_v2->self = hdr_base;
337 if (dcd_len > 0)
338 fhdr_v2->dcd_ptr = hdr_base +
339 offsetof(imx_header_v2_t, data);
340 else
341 fhdr_v2->dcd_ptr = 0;
342 fhdr_v2->boot_data_ptr = hdr_base
343 + offsetof(imx_header_v2_t, boot_data);
344 hdr_v2->boot_data.start = entry_point - imximage_init_loadsize;
345
346 fhdr_v2->csf = 0;
347
348 header_size_ptr = &hdr_v2->boot_data.size;
349 csf_ptr = &fhdr_v2->csf;
350 } else {
351 imx_header_v2_t *next_hdr_v2;
352 flash_header_v2_t *next_fhdr_v2;
353
354 if (imximage_csf_size != 0) {
355 fprintf(stderr, "Error: Header v2: SECURE_BOOT is only supported in DCD mode!");
356 exit(EXIT_FAILURE);
357 }
358
359 fhdr_v2->entry = imximage_iram_free_start +
360 flash_offset + sizeof(flash_header_v2_t) +
361 sizeof(boot_data_t);
362
363 fhdr_v2->reserved1 = 0;
364 fhdr_v2->reserved2 = 0;
365 fhdr_v2->self = imximage_iram_free_start + flash_offset;
366
Baruch Siach9c2cf9a2015-07-09 18:19:11 +0300367 fhdr_v2->dcd_ptr = 0;
Peng Fan334de962016-10-11 14:29:09 +0800368
369 fhdr_v2->boot_data_ptr = fhdr_v2->self +
370 offsetof(imx_header_v2_t, boot_data);
371
372 hdr_v2->boot_data.start = imximage_iram_free_start;
373 /*
374 * The actural size of plugin image is "imximage_plugin_size +
375 * sizeof(flash_header_v2_t) + sizeof(boot_data_t)", plus the
376 * flash_offset space.The ROM code only need to copy this size
377 * to run the plugin code. However, later when copy the whole
378 * U-Boot image to DDR, the ROM code use memcpy to copy the
379 * first part of the image, and use the storage read function
380 * to get the remaining part. This requires the dividing point
381 * must be multiple of storage sector size. Here we set the
382 * first section to be MAX_PLUGIN_CODE_SIZE(64KB) for this
383 * purpose.
384 */
385 hdr_v2->boot_data.size = MAX_PLUGIN_CODE_SIZE;
386
387 /* Security feature are not supported */
388 fhdr_v2->csf = 0;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000389
Peng Fan334de962016-10-11 14:29:09 +0800390 next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 +
391 imximage_plugin_size);
Stefano Babic4aa97492013-06-27 11:42:38 +0200392
Peng Fan334de962016-10-11 14:29:09 +0800393 next_fhdr_v2 = &next_hdr_v2->fhdr;
394
395 next_fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
396 next_fhdr_v2->header.length =
397 cpu_to_be16(sizeof(flash_header_v2_t));
398 next_fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
399
400 next_fhdr_v2->entry = entry_point;
401 hdr_base = entry_point - sizeof(struct imx_header);
402 next_fhdr_v2->reserved1 = 0;
403 next_fhdr_v2->reserved2 = 0;
404 next_fhdr_v2->self = hdr_base + imximage_plugin_size;
405
406 next_fhdr_v2->dcd_ptr = 0;
407 next_fhdr_v2->boot_data_ptr = next_fhdr_v2->self +
408 offsetof(imx_header_v2_t, boot_data);
409
410 next_hdr_v2->boot_data.start = hdr_base - flash_offset;
411
412 header_size_ptr = &next_hdr_v2->boot_data.size;
413
414 next_hdr_v2->boot_data.plugin = 0;
415
416 next_fhdr_v2->csf = 0;
417 }
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000418}
419
York Sund061d952013-09-20 12:24:44 -0700420static void set_hdr_func(void)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000421{
422 switch (imximage_version) {
423 case IMXIMAGE_V1:
424 set_dcd_val = set_dcd_val_v1;
Adrian Alonso73e57322015-07-20 19:04:55 -0500425 set_dcd_param = NULL;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000426 set_dcd_rst = set_dcd_rst_v1;
427 set_imx_hdr = set_imx_hdr_v1;
Troy Kisky9bd5b852012-10-03 15:47:03 +0000428 max_dcd_entries = MAX_HW_CFG_SIZE_V1;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000429 break;
430 case IMXIMAGE_V2:
Troy Kiskyf575ab42015-09-14 18:06:31 -0700431 gd_last_cmd = NULL;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000432 set_dcd_val = set_dcd_val_v2;
Adrian Alonso73e57322015-07-20 19:04:55 -0500433 set_dcd_param = set_dcd_param_v2;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000434 set_dcd_rst = set_dcd_rst_v2;
435 set_imx_hdr = set_imx_hdr_v2;
Troy Kisky9bd5b852012-10-03 15:47:03 +0000436 max_dcd_entries = MAX_HW_CFG_SIZE_V2;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000437 break;
438 default:
439 err_imximage_version(imximage_version);
440 break;
441 }
442}
Stefano Babic7b07f092010-01-20 18:19:10 +0100443
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000444static void print_hdr_v1(struct imx_header *imx_hdr)
445{
446 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
447 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
448 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
449 uint32_t size, length, ver;
450
451 size = dcd_v1->preamble.length;
452 if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
Stefano Babic7b07f092010-01-20 18:19:10 +0100453 fprintf(stderr,
454 "Error: Image corrupt DCD size %d exceed maximum %d\n",
Stefano Babic5cdde802010-02-05 15:16:02 +0100455 (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000456 MAX_HW_CFG_SIZE_V1);
457 exit(EXIT_FAILURE);
458 }
459
460 length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
461 ver = detect_imximage_version(imx_hdr);
462
463 printf("Image Type: Freescale IMX Boot Image\n");
464 printf("Image Ver: %x", ver);
465 printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
466 printf("Data Size: ");
467 genimg_print_size(dcd_v1->addr_data[length].type);
468 printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
469 printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
470}
471
472static void print_hdr_v2(struct imx_header *imx_hdr)
473{
474 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
475 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
Peng Fan334de962016-10-11 14:29:09 +0800476 dcd_v2_t *dcd_v2 = &hdr_v2->data.dcd_table;
477 uint32_t size, version, plugin;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000478
Peng Fan334de962016-10-11 14:29:09 +0800479 plugin = hdr_v2->boot_data.plugin;
480 if (!plugin) {
481 size = be16_to_cpu(dcd_v2->header.length);
482 if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
483 fprintf(stderr,
484 "Error: Image corrupt DCD size %d exceed maximum %d\n",
485 (uint32_t)(size / sizeof(dcd_addr_data_t)),
486 MAX_HW_CFG_SIZE_V2);
487 exit(EXIT_FAILURE);
488 }
Stefano Babic7b07f092010-01-20 18:19:10 +0100489 }
490
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000491 version = detect_imximage_version(imx_hdr);
Stefano Babic7b07f092010-01-20 18:19:10 +0100492
493 printf("Image Type: Freescale IMX Boot Image\n");
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000494 printf("Image Ver: %x", version);
495 printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
Peng Fan334de962016-10-11 14:29:09 +0800496 printf("Mode: %s\n", plugin ? "PLUGIN" : "DCD");
497 if (!plugin) {
498 printf("Data Size: ");
499 genimg_print_size(hdr_v2->boot_data.size);
500 printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
501 printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
502 if (fhdr_v2->csf && (imximage_ivt_offset != UNDEFINED) &&
503 (imximage_csf_size != UNDEFINED)) {
504 printf("HAB Blocks: %08x %08x %08x\n",
505 (uint32_t)fhdr_v2->self, 0,
506 hdr_v2->boot_data.size - imximage_ivt_offset -
507 imximage_csf_size);
508 }
509 } else {
510 imx_header_v2_t *next_hdr_v2;
511 flash_header_v2_t *next_fhdr_v2;
512
513 /*First Header*/
514 printf("Plugin Data Size: ");
515 genimg_print_size(hdr_v2->boot_data.size);
516 printf("Plugin Code Size: ");
517 genimg_print_size(imximage_plugin_size);
518 printf("Plugin Load Address: %08x\n", hdr_v2->boot_data.start);
519 printf("Plugin Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
520
521 /*Second Header*/
522 next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 +
523 imximage_plugin_size);
524 next_fhdr_v2 = &next_hdr_v2->fhdr;
525 printf("U-Boot Data Size: ");
526 genimg_print_size(next_hdr_v2->boot_data.size);
527 printf("U-Boot Load Address: %08x\n",
528 next_hdr_v2->boot_data.start);
529 printf("U-Boot Entry Point: %08x\n",
530 (uint32_t)next_fhdr_v2->entry);
531 }
532}
533
534static void copy_plugin_code(struct imx_header *imxhdr, char *plugin_file)
535{
Peng Fanb0034bb2016-11-04 10:33:15 +0800536 int ifd;
Peng Fan334de962016-10-11 14:29:09 +0800537 struct stat sbuf;
538 char *plugin_buf = imxhdr->header.hdr_v2.data.plugin_code;
539 char *ptr;
540
541 ifd = open(plugin_file, O_RDONLY|O_BINARY);
Peng Fanb0034bb2016-11-04 10:33:15 +0800542 if (ifd < 0) {
543 fprintf(stderr, "Can't open %s: %s\n",
544 plugin_file,
545 strerror(errno));
546 exit(EXIT_FAILURE);
547 }
548
Peng Fan334de962016-10-11 14:29:09 +0800549 if (fstat(ifd, &sbuf) < 0) {
550 fprintf(stderr, "Can't stat %s: %s\n",
551 plugin_file,
552 strerror(errno));
553 exit(EXIT_FAILURE);
554 }
555
556 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
557 if (ptr == MAP_FAILED) {
558 fprintf(stderr, "Can't read %s: %s\n",
559 plugin_file,
560 strerror(errno));
561 exit(EXIT_FAILURE);
562 }
563
564 if (sbuf.st_size > MAX_PLUGIN_CODE_SIZE) {
565 printf("plugin binary size too large\n");
566 exit(EXIT_FAILURE);
Stefano Babic4aa97492013-06-27 11:42:38 +0200567 }
Peng Fan334de962016-10-11 14:29:09 +0800568
569 memcpy(plugin_buf, ptr, sbuf.st_size);
570 imximage_plugin_size = sbuf.st_size;
571
572 (void) munmap((void *)ptr, sbuf.st_size);
573 (void) close(ifd);
574
575 imxhdr->header.hdr_v2.boot_data.plugin = 1;
Stefano Babic7b07f092010-01-20 18:19:10 +0100576}
577
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000578static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
579 char *name, int lineno, int fld, int dcd_len)
580{
581 int value;
582 static int cmd_ver_first = ~0;
583
584 switch (cmd) {
585 case CMD_IMAGE_VERSION:
586 imximage_version = get_cfg_value(token, name, lineno);
587 if (cmd_ver_first == 0) {
588 fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
589 "command need be the first before other "
590 "valid command in the file\n", name, lineno);
591 exit(EXIT_FAILURE);
592 }
593 cmd_ver_first = 1;
York Sund061d952013-09-20 12:24:44 -0700594 set_hdr_func();
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000595 break;
596 case CMD_BOOT_FROM:
Stefano Babicdc39a3e2013-06-26 23:50:06 +0200597 imximage_ivt_offset = get_table_entry_id(imximage_boot_offset,
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000598 "imximage boot option", token);
Stefano Babice74e7792013-06-26 18:08:37 +0200599 if (imximage_ivt_offset == -1) {
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000600 fprintf(stderr, "Error: %s[%d] -Invalid boot device"
601 "(%s)\n", name, lineno, token);
602 exit(EXIT_FAILURE);
603 }
Stefano Babicdc39a3e2013-06-26 23:50:06 +0200604
605 imximage_init_loadsize =
606 get_table_entry_id(imximage_boot_loadsize,
607 "imximage boot option", token);
608
609 if (imximage_init_loadsize == -1) {
610 fprintf(stderr,
611 "Error: %s[%d] -Invalid boot device(%s)\n",
612 name, lineno, token);
613 exit(EXIT_FAILURE);
614 }
Stefano Babic832eda82013-08-19 19:03:20 +0200615
616 /*
617 * The SOC loads from the storage starting at address 0
618 * then ensures that the load size contains the offset
619 */
620 if (imximage_init_loadsize < imximage_ivt_offset)
621 imximage_init_loadsize = imximage_ivt_offset;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000622 if (unlikely(cmd_ver_first != 1))
623 cmd_ver_first = 0;
624 break;
Marek Vasutd45fd732013-04-25 10:16:02 +0000625 case CMD_BOOT_OFFSET:
Stefano Babice74e7792013-06-26 18:08:37 +0200626 imximage_ivt_offset = get_cfg_value(token, name, lineno);
Marek Vasutd45fd732013-04-25 10:16:02 +0000627 if (unlikely(cmd_ver_first != 1))
628 cmd_ver_first = 0;
629 break;
Adrian Alonso73e57322015-07-20 19:04:55 -0500630 case CMD_WRITE_DATA:
631 case CMD_WRITE_CLR_BIT:
632 case CMD_CHECK_BITS_SET:
633 case CMD_CHECK_BITS_CLR:
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000634 value = get_cfg_value(token, name, lineno);
Adrian Alonso73e57322015-07-20 19:04:55 -0500635 if (set_dcd_param)
636 (*set_dcd_param)(imxhdr, dcd_len, cmd);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000637 (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
638 if (unlikely(cmd_ver_first != 1))
639 cmd_ver_first = 0;
640 break;
Stefano Babic4aa97492013-06-27 11:42:38 +0200641 case CMD_CSF:
642 if (imximage_version != 2) {
643 fprintf(stderr,
644 "Error: %s[%d] - CSF only supported for VERSION 2(%s)\n",
645 name, lineno, token);
646 exit(EXIT_FAILURE);
647 }
648 imximage_csf_size = get_cfg_value(token, name, lineno);
649 if (unlikely(cmd_ver_first != 1))
650 cmd_ver_first = 0;
651 break;
Peng Fan334de962016-10-11 14:29:09 +0800652 case CMD_PLUGIN:
653 plugin_image = 1;
654 copy_plugin_code(imxhdr, token);
655 break;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000656 }
657}
658
659static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
660 char *token, char *name, int lineno, int fld, int *dcd_len)
661{
662 int value;
663
664 switch (fld) {
665 case CFG_COMMAND:
666 *cmd = get_table_entry_id(imximage_cmds,
667 "imximage commands", token);
668 if (*cmd < 0) {
669 fprintf(stderr, "Error: %s[%d] - Invalid command"
670 "(%s)\n", name, lineno, token);
671 exit(EXIT_FAILURE);
672 }
673 break;
674 case CFG_REG_SIZE:
675 parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
676 break;
677 case CFG_REG_ADDRESS:
678 case CFG_REG_VALUE:
Adrian Alonso73e57322015-07-20 19:04:55 -0500679 switch(*cmd) {
680 case CMD_WRITE_DATA:
681 case CMD_WRITE_CLR_BIT:
682 case CMD_CHECK_BITS_SET:
683 case CMD_CHECK_BITS_CLR:
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000684
Adrian Alonso73e57322015-07-20 19:04:55 -0500685 value = get_cfg_value(token, name, lineno);
686 if (set_dcd_param)
687 (*set_dcd_param)(imxhdr, *dcd_len, *cmd);
688 (*set_dcd_val)(imxhdr, name, lineno, fld, value,
689 *dcd_len);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000690
Adrian Alonso73e57322015-07-20 19:04:55 -0500691 if (fld == CFG_REG_VALUE) {
692 (*dcd_len)++;
693 if (*dcd_len > max_dcd_entries) {
694 fprintf(stderr, "Error: %s[%d] -"
695 "DCD table exceeds maximum size(%d)\n",
696 name, lineno, max_dcd_entries);
697 exit(EXIT_FAILURE);
698 }
Troy Kisky9bd5b852012-10-03 15:47:03 +0000699 }
Adrian Alonso73e57322015-07-20 19:04:55 -0500700 break;
Peng Fan334de962016-10-11 14:29:09 +0800701 case CMD_PLUGIN:
702 value = get_cfg_value(token, name, lineno);
703 imximage_iram_free_start = value;
704 break;
Adrian Alonso73e57322015-07-20 19:04:55 -0500705 default:
706 break;
Troy Kisky9bd5b852012-10-03 15:47:03 +0000707 }
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000708 break;
709 default:
710 break;
711 }
712}
713static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
Stefano Babic7b07f092010-01-20 18:19:10 +0100714{
715 FILE *fd = NULL;
716 char *line = NULL;
717 char *token, *saveptr1, *saveptr2;
718 int lineno = 0;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000719 int fld;
Kim Phillips1c0c2502010-02-22 19:37:56 -0600720 size_t len;
Stefano Babic7b07f092010-01-20 18:19:10 +0100721 int dcd_len = 0;
Stefano Babic7b07f092010-01-20 18:19:10 +0100722 int32_t cmd;
723
724 fd = fopen(name, "r");
725 if (fd == 0) {
726 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
727 exit(EXIT_FAILURE);
728 }
729
Stefano Babic832eda82013-08-19 19:03:20 +0200730 /*
731 * Very simple parsing, line starting with # are comments
Stefano Babic7b07f092010-01-20 18:19:10 +0100732 * and are dropped
733 */
734 while ((getline(&line, &len, fd)) > 0) {
735 lineno++;
736
737 token = strtok_r(line, "\r\n", &saveptr1);
738 if (token == NULL)
739 continue;
740
741 /* Check inside the single line */
742 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
743 line = token; ; line = NULL, fld++) {
744 token = strtok_r(line, " \t", &saveptr2);
745 if (token == NULL)
746 break;
747
748 /* Drop all text starting with '#' as comments */
749 if (token[0] == '#')
750 break;
751
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000752 parse_cfg_fld(imxhdr, &cmd, token, name,
753 lineno, fld, &dcd_len);
Stefano Babic7b07f092010-01-20 18:19:10 +0100754 }
755
Stefano Babic7b07f092010-01-20 18:19:10 +0100756 }
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000757
758 (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
Stefano Babic7b07f092010-01-20 18:19:10 +0100759 fclose(fd);
760
Troy Kiskye55eaf92012-10-03 15:47:05 +0000761 /* Exit if there is no BOOT_FROM field specifying the flash_offset */
Stefano Babice74e7792013-06-26 18:08:37 +0200762 if (imximage_ivt_offset == FLASH_OFFSET_UNDEFINED) {
Troy Kiskye55eaf92012-10-03 15:47:05 +0000763 fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name);
764 exit(EXIT_FAILURE);
765 }
Stefano Babic5cdde802010-02-05 15:16:02 +0100766 return dcd_len;
Stefano Babic7b07f092010-01-20 18:19:10 +0100767}
768
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000769
770static int imximage_check_image_types(uint8_t type)
Stefano Babic7b07f092010-01-20 18:19:10 +0100771{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000772 if (type == IH_TYPE_IMXIMAGE)
773 return EXIT_SUCCESS;
774 else
775 return EXIT_FAILURE;
776}
Stefano Babic7b07f092010-01-20 18:19:10 +0100777
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000778static int imximage_verify_header(unsigned char *ptr, int image_size,
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700779 struct image_tool_params *params)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000780{
781 struct imx_header *imx_hdr = (struct imx_header *) ptr;
Stefano Babic7b07f092010-01-20 18:19:10 +0100782
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000783 if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
784 return -FDT_ERR_BADSTRUCTURE;
Stefano Babic7b07f092010-01-20 18:19:10 +0100785
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000786 return 0;
787}
Stefano Babic7b07f092010-01-20 18:19:10 +0100788
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000789static void imximage_print_header(const void *ptr)
790{
791 struct imx_header *imx_hdr = (struct imx_header *) ptr;
792 uint32_t version = detect_imximage_version(imx_hdr);
Stefano Babic7b07f092010-01-20 18:19:10 +0100793
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000794 switch (version) {
795 case IMXIMAGE_V1:
796 print_hdr_v1(imx_hdr);
797 break;
798 case IMXIMAGE_V2:
799 print_hdr_v2(imx_hdr);
800 break;
801 default:
802 err_imximage_version(version);
803 break;
804 }
805}
Stefano Babic7b07f092010-01-20 18:19:10 +0100806
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000807static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700808 struct image_tool_params *params)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000809{
810 struct imx_header *imxhdr = (struct imx_header *)ptr;
811 uint32_t dcd_len;
Peng Fan334de962016-10-11 14:29:09 +0800812 uint32_t header_size;
Stefano Babic7b07f092010-01-20 18:19:10 +0100813
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000814 /*
815 * In order to not change the old imx cfg file
816 * by adding VERSION command into it, here need
817 * set up function ptr group to V1 by default.
818 */
819 imximage_version = IMXIMAGE_V1;
Dirk Behme14a98cd2012-02-22 22:50:19 +0000820 /* Be able to detect if the cfg file has no BOOT_FROM tag */
Stefano Babice74e7792013-06-26 18:08:37 +0200821 imximage_ivt_offset = FLASH_OFFSET_UNDEFINED;
Stefano Babic4aa97492013-06-27 11:42:38 +0200822 imximage_csf_size = 0;
York Sund061d952013-09-20 12:24:44 -0700823 set_hdr_func();
Stefano Babic7b07f092010-01-20 18:19:10 +0100824
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000825 /* Parse dcd configuration file */
826 dcd_len = parse_cfg_file(imxhdr, params->imagename);
Stefano Babic7b07f092010-01-20 18:19:10 +0100827
Ye.Lif4063512014-08-20 16:55:32 +0800828 if (imximage_version == IMXIMAGE_V2) {
Peng Fan334de962016-10-11 14:29:09 +0800829 header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t);
830 if (!plugin_image)
831 header_size += sizeof(dcd_v2_t);
832 else
833 header_size += MAX_PLUGIN_CODE_SIZE;
834
835 if (imximage_init_loadsize < imximage_ivt_offset + header_size)
Ye.Lif4063512014-08-20 16:55:32 +0800836 imximage_init_loadsize = imximage_ivt_offset +
Peng Fan334de962016-10-11 14:29:09 +0800837 header_size;
Ye.Lif4063512014-08-20 16:55:32 +0800838 }
839
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000840 /* Set the imx header */
Stefano Babice74e7792013-06-26 18:08:37 +0200841 (*set_imx_hdr)(imxhdr, dcd_len, params->ep, imximage_ivt_offset);
Fabio Estevam5fd7c5d2013-01-03 08:24:33 +0000842
843 /*
844 * ROM bug alert
Marek Vasut3d1acc62013-04-21 05:52:22 +0000845 *
846 * MX53 only loads 512 byte multiples in case of SD boot.
847 * MX53 only loads NAND page multiples in case of NAND boot and
848 * supports up to 4096 byte large pages, thus align to 4096.
849 *
850 * The remaining fraction of a block bytes would not be loaded!
Fabio Estevam5fd7c5d2013-01-03 08:24:33 +0000851 */
Ye.Li49e036f2014-10-30 17:54:08 +0800852 *header_size_ptr = ROUND((sbuf->st_size + imximage_ivt_offset), 4096);
Stefano Babic4aa97492013-06-27 11:42:38 +0200853
854 if (csf_ptr && imximage_csf_size) {
855 *csf_ptr = params->ep - imximage_init_loadsize +
856 *header_size_ptr;
857 *header_size_ptr += imximage_csf_size;
858 }
Stefano Babic7b07f092010-01-20 18:19:10 +0100859}
860
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700861int imximage_check_params(struct image_tool_params *params)
Stefano Babic7b07f092010-01-20 18:19:10 +0100862{
863 if (!params)
864 return CFG_INVALID;
865 if (!strlen(params->imagename)) {
866 fprintf(stderr, "Error: %s - Configuration file not specified, "
867 "it is needed for imximage generation\n",
868 params->cmdname);
869 return CFG_INVALID;
870 }
871 /*
872 * Check parameters:
873 * XIP is not allowed and verify that incompatible
874 * parameters are not sent at the same time
875 * For example, if list is required a data image must not be provided
876 */
877 return (params->dflag && (params->fflag || params->lflag)) ||
878 (params->fflag && (params->dflag || params->lflag)) ||
879 (params->lflag && (params->dflag || params->fflag)) ||
880 (params->xflag) || !(strlen(params->imagename));
881}
882
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700883static int imximage_generate(struct image_tool_params *params,
Stefano Babic832eda82013-08-19 19:03:20 +0200884 struct image_type_params *tparams)
885{
886 struct imx_header *imxhdr;
887 size_t alloc_len;
888 struct stat sbuf;
889 char *datafile = params->datafile;
Peng Fan334de962016-10-11 14:29:09 +0800890 uint32_t pad_len, header_size;
Stefano Babic832eda82013-08-19 19:03:20 +0200891
892 memset(&imximage_header, 0, sizeof(imximage_header));
893
894 /*
895 * In order to not change the old imx cfg file
896 * by adding VERSION command into it, here need
897 * set up function ptr group to V1 by default.
898 */
899 imximage_version = IMXIMAGE_V1;
900 /* Be able to detect if the cfg file has no BOOT_FROM tag */
901 imximage_ivt_offset = FLASH_OFFSET_UNDEFINED;
902 imximage_csf_size = 0;
York Sund061d952013-09-20 12:24:44 -0700903 set_hdr_func();
Stefano Babic832eda82013-08-19 19:03:20 +0200904
905 /* Parse dcd configuration file */
906 parse_cfg_file(&imximage_header, params->imagename);
907
908 /* TODO: check i.MX image V1 handling, for now use 'old' style */
909 if (imximage_version == IMXIMAGE_V1) {
910 alloc_len = 4096;
Peng Fan334de962016-10-11 14:29:09 +0800911 header_size = 4096;
Stefano Babic832eda82013-08-19 19:03:20 +0200912 } else {
Peng Fan334de962016-10-11 14:29:09 +0800913 header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t);
914 if (!plugin_image)
915 header_size += sizeof(dcd_v2_t);
916 else
917 header_size += MAX_PLUGIN_CODE_SIZE;
918
919 if (imximage_init_loadsize < imximage_ivt_offset + header_size)
Stefano Babic832eda82013-08-19 19:03:20 +0200920 imximage_init_loadsize = imximage_ivt_offset +
Peng Fan334de962016-10-11 14:29:09 +0800921 header_size;
Stefano Babic832eda82013-08-19 19:03:20 +0200922 alloc_len = imximage_init_loadsize - imximage_ivt_offset;
923 }
924
Peng Fan334de962016-10-11 14:29:09 +0800925 if (alloc_len < header_size) {
Stefano Babic832eda82013-08-19 19:03:20 +0200926 fprintf(stderr, "%s: header error\n",
927 params->cmdname);
928 exit(EXIT_FAILURE);
929 }
930
931 imxhdr = malloc(alloc_len);
932
933 if (!imxhdr) {
934 fprintf(stderr, "%s: malloc return failure: %s\n",
935 params->cmdname, strerror(errno));
936 exit(EXIT_FAILURE);
937 }
938
939 memset(imxhdr, 0, alloc_len);
940
941 tparams->header_size = alloc_len;
942 tparams->hdr = imxhdr;
943
944 /* determine data image file length */
945
946 if (stat(datafile, &sbuf) < 0) {
947 fprintf(stderr, "%s: Can't stat %s: %s\n",
948 params->cmdname, datafile, strerror(errno));
949 exit(EXIT_FAILURE);
950 }
951
952 pad_len = ROUND(sbuf.st_size, 4096) - sbuf.st_size;
953
954 /* TODO: check i.MX image V1 handling, for now use 'old' style */
955 if (imximage_version == IMXIMAGE_V1)
956 return 0;
957 else
958 return pad_len;
959}
960
961
Stefano Babic7b07f092010-01-20 18:19:10 +0100962/*
963 * imximage parameters
964 */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200965U_BOOT_IMAGE_TYPE(
966 imximage,
967 "Freescale i.MX Boot Image support",
968 0,
969 NULL,
970 imximage_check_params,
971 imximage_verify_header,
972 imximage_print_header,
973 imximage_set_header,
974 NULL,
975 imximage_check_image_types,
976 NULL,
977 imximage_generate
978);