blob: 0c43196ac1953ececd3a0161191d17f481f20703 [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 Denk3765b3e2013-10-07 13:07:26 +02009 * SPDX-License-Identifier: GPL-2.0+
Stefano Babic8edcde52010-01-20 18:19:10 +010010 */
11
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -070012#include "imagetool.h"
Stefano Babic8edcde52010-01-20 18:19:10 +010013#include <image.h>
14#include "imximage.h"
15
Stefano Babic0187c982013-06-27 11:42:38 +020016#define UNDEFINED 0xFFFFFFFF
17
Stefano Babic8edcde52010-01-20 18:19:10 +010018/*
19 * Supported commands for configuration file
20 */
21static table_entry_t imximage_cmds[] = {
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000022 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
Marek Vasut6cb83822013-04-25 10:16:02 +000023 {CMD_BOOT_OFFSET, "BOOT_OFFSET", "Boot offset", },
Adrian Alonso0b7f7c32015-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 Babic0187c982013-06-27 11:42:38 +020028 {CMD_CSF, "CSF", "Command Sequence File", },
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000029 {CMD_IMAGE_VERSION, "IMAGE_VERSION", "image version", },
Peng Fanb55e4f42016-10-11 14:29:09 +080030 {CMD_PLUGIN, "PLUGIN", "file plugin_addr", },
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000031 {-1, "", "", },
Stefano Babic8edcde52010-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 Babic377e3672013-06-26 23:50:06 +020038static table_entry_t imximage_boot_offset[] = {
Stefano Babic8edcde52010-01-20 18:19:10 +010039 {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",},
Dirk Behmebd258642012-01-11 23:28:32 +000040 {FLASH_OFFSET_NAND, "nand", "NAND Flash", },
Dirk Behme19b409c2012-01-11 23:28:31 +000041 {FLASH_OFFSET_NOR, "nor", "NOR Flash", },
42 {FLASH_OFFSET_SATA, "sata", "SATA Disk", },
Dirk Behmebd258642012-01-11 23:28:32 +000043 {FLASH_OFFSET_SD, "sd", "SD Card", },
44 {FLASH_OFFSET_SPI, "spi", "SPI Flash", },
Ye.Li9598f8c2015-01-13 15:53:06 +080045 {FLASH_OFFSET_QSPI, "qspi", "QSPI NOR Flash",},
Stefano Babic8edcde52010-01-20 18:19:10 +010046 {-1, "", "Invalid", },
47};
48
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000049/*
Stefano Babic377e3672013-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.Li9598f8c2015-01-13 15:53:06 +080060 {FLASH_LOADSIZE_QSPI, "qspi", "QSPI NOR Flash",},
Stefano Babic377e3672013-06-26 23:50:06 +020061 {-1, "", "Invalid", },
62};
63
64/*
Liu Hui-R643438a1edd72011-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 Alonso0b7f7c32015-07-20 19:04:55 -050069 {IMXIMAGE_V2, "", " (i.MX53/6/7 compatible)", },
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000070 {-1, "", " (Invalid)", },
71};
Stefano Babic8edcde52010-01-20 18:19:10 +010072
73static struct imx_header imximage_header;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000074static uint32_t imximage_version;
Stefano Babic0187c982013-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 Babic377e3672013-06-26 23:50:06 +020082/* Initial Load Region Size */
83static uint32_t imximage_init_loadsize;
Peng Fanb55e4f42016-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-R643438a1edd72011-01-19 09:40:26 +000087
88static set_dcd_val_t set_dcd_val;
Adrian Alonso0b7f7c32015-07-20 19:04:55 -050089static set_dcd_param_t set_dcd_param;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +000090static set_dcd_rst_t set_dcd_rst;
91static set_imx_hdr_t set_imx_hdr;
Troy Kisky4d5fa982012-10-03 15:47:03 +000092static uint32_t max_dcd_entries;
Troy Kisky24331982012-10-03 15:47:07 +000093static uint32_t *header_size_ptr;
Stefano Babic0187c982013-06-27 11:42:38 +020094static uint32_t *csf_ptr;
Stefano Babic8edcde52010-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-R643438a1edd72011-01-19 09:40:26 +0000111static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
Stefano Babic8edcde52010-01-20 18:19:10 +0100112{
Liu Hui-R643438a1edd72011-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 Fanb55e4f42016-10-11 14:29:09 +0800125 (hdr_v2->data.dcd_table.header.tag == DCD_HEADER_TAG))
126 return IMXIMAGE_V2;
127
128 if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
129 hdr_v2->boot_data.plugin)
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000130 return IMXIMAGE_V2;
131
132 return IMXIMAGE_VER_INVALID;
Stefano Babic8edcde52010-01-20 18:19:10 +0100133}
134
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000135static void err_imximage_version(int version)
Stefano Babic8edcde52010-01-20 18:19:10 +0100136{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000137 fprintf(stderr,
138 "Error: Unsupported imximage version:%d\n", version);
Stefano Babic8edcde52010-01-20 18:19:10 +0100139
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000140 exit(EXIT_FAILURE);
Stefano Babic8edcde52010-01-20 18:19:10 +0100141}
142
Liu Hui-R643438a1edd72011-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)
Stefano Babic8edcde52010-01-20 18:19:10 +0100145{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000146 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
Stefano Babic8edcde52010-01-20 18:19:10 +0100147
Liu Hui-R643438a1edd72011-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;
167
168 }
169}
170
Troy Kisky61903b72015-09-14 18:06:31 -0700171static struct dcd_v2_cmd *gd_last_cmd;
172
Adrian Alonso0b7f7c32015-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 Fanb55e4f42016-10-11 14:29:09 +0800176 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.data.dcd_table;
Troy Kisky61903b72015-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 Alonso0b7f7c32015-07-20 19:04:55 -0500187
188 switch (cmd) {
189 case CMD_WRITE_DATA:
Troy Kisky61903b72015-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 Alonso0b7f7c32015-07-20 19:04:55 -0500197 break;
198 case CMD_WRITE_CLR_BIT:
Troy Kisky61903b72015-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 Alonso0b7f7c32015-07-20 19:04:55 -0500206 break;
207 /*
208 * Check data command only supports one entry,
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500209 */
210 case CMD_CHECK_BITS_SET:
Troy Kisky61903b72015-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 Alonso0b7f7c32015-07-20 19:04:55 -0500215 break;
216 case CMD_CHECK_BITS_CLR:
Troy Kisky61903b72015-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 Alonso0782a882016-05-02 10:29:14 -0500220 d->write_dcd_command.param = DCD_CHECK_BITS_CLR_PARAM;
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500221 break;
222 default:
223 break;
224 }
Troy Kisky61903b72015-09-14 18:06:31 -0700225 gd_last_cmd = d;
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500226}
227
Liu Hui-R643438a1edd72011-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)
230{
Troy Kisky61903b72015-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-R643438a1edd72011-01-19 09:40:26 +0000236
237 switch (fld) {
238 case CFG_REG_ADDRESS:
Troy Kisky61903b72015-09-14 18:06:31 -0700239 d->addr_data[off].addr = cpu_to_be32(value);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000240 break;
241 case CFG_REG_VALUE:
Troy Kisky61903b72015-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-R643438a1edd72011-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-R643438a1edd72011-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);
Stefano Babic8edcde52010-01-20 18:19:10 +0100263}
264
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000265/*
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 Fanb55e4f42016-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 Kisky61903b72015-09-14 18:06:31 -0700276
Peng Fanb55e4f42016-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 Kisky61903b72015-09-14 18:06:31 -0700282
Peng Fanb55e4f42016-10-11 14:29:09 +0800283 len = (char *)d - (char *)&dcd_v2->header;
Peng Fanb55e4f42016-10-11 14:29:09 +0800284 dcd_v2->header.tag = DCD_HEADER_TAG;
285 dcd_v2->header.length = cpu_to_be16(len);
286 dcd_v2->header.version = DCD_VERSION;
287 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000288}
289
290static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kiskyad0826d2012-10-03 15:47:08 +0000291 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000292{
293 imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1;
294 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
295 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
Troy Kiskyab857f22012-10-03 15:47:09 +0000296 uint32_t hdr_base;
Troy Kisky24331982012-10-03 15:47:07 +0000297 uint32_t header_length = (((char *)&dcd_v1->addr_data[dcd_len].addr)
298 - ((char *)imxhdr));
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000299
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000300 /* Set magic number */
301 fhdr_v1->app_code_barker = APP_CODE_BARKER;
302
Martin Kaiser97f17fa2017-01-02 22:24:17 +0100303 hdr_base = entry_point - imximage_init_loadsize + flash_offset;
Troy Kiskyab857f22012-10-03 15:47:09 +0000304 fhdr_v1->app_dest_ptr = hdr_base - flash_offset;
Troy Kiskyad0826d2012-10-03 15:47:08 +0000305 fhdr_v1->app_code_jump_vector = entry_point;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000306
Troy Kiskyab857f22012-10-03 15:47:09 +0000307 fhdr_v1->dcd_ptr_ptr = hdr_base + offsetof(flash_header_v1_t, dcd_ptr);
308 fhdr_v1->dcd_ptr = hdr_base + offsetof(imx_header_v1_t, dcd_table);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000309
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000310 /* Security feature are not supported */
311 fhdr_v1->app_code_csf = 0;
312 fhdr_v1->super_root_key = 0;
Troy Kisky24331982012-10-03 15:47:07 +0000313 header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000314}
315
316static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kiskyad0826d2012-10-03 15:47:08 +0000317 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000318{
319 imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
320 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
Troy Kiskyab857f22012-10-03 15:47:09 +0000321 uint32_t hdr_base;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000322
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000323 /* Set magic number */
324 fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
325 fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
326 fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
327
Peng Fanb55e4f42016-10-11 14:29:09 +0800328 if (!hdr_v2->boot_data.plugin) {
329 fhdr_v2->entry = entry_point;
330 fhdr_v2->reserved1 = 0;
331 fhdr_v2->reserved1 = 0;
332 hdr_base = entry_point - imximage_init_loadsize +
333 flash_offset;
334 fhdr_v2->self = hdr_base;
335 if (dcd_len > 0)
336 fhdr_v2->dcd_ptr = hdr_base +
337 offsetof(imx_header_v2_t, data);
338 else
339 fhdr_v2->dcd_ptr = 0;
340 fhdr_v2->boot_data_ptr = hdr_base
341 + offsetof(imx_header_v2_t, boot_data);
342 hdr_v2->boot_data.start = entry_point - imximage_init_loadsize;
343
344 fhdr_v2->csf = 0;
345
346 header_size_ptr = &hdr_v2->boot_data.size;
347 csf_ptr = &fhdr_v2->csf;
348 } else {
349 imx_header_v2_t *next_hdr_v2;
350 flash_header_v2_t *next_fhdr_v2;
351
352 if (imximage_csf_size != 0) {
353 fprintf(stderr, "Error: Header v2: SECURE_BOOT is only supported in DCD mode!");
354 exit(EXIT_FAILURE);
355 }
356
357 fhdr_v2->entry = imximage_iram_free_start +
358 flash_offset + sizeof(flash_header_v2_t) +
359 sizeof(boot_data_t);
360
361 fhdr_v2->reserved1 = 0;
362 fhdr_v2->reserved2 = 0;
363 fhdr_v2->self = imximage_iram_free_start + flash_offset;
364
Baruch Siachb893c982015-07-09 18:19:11 +0300365 fhdr_v2->dcd_ptr = 0;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000366
Peng Fanb55e4f42016-10-11 14:29:09 +0800367 fhdr_v2->boot_data_ptr = fhdr_v2->self +
368 offsetof(imx_header_v2_t, boot_data);
Stefano Babic0187c982013-06-27 11:42:38 +0200369
Peng Fanb55e4f42016-10-11 14:29:09 +0800370 hdr_v2->boot_data.start = imximage_iram_free_start;
371 /*
372 * The actural size of plugin image is "imximage_plugin_size +
373 * sizeof(flash_header_v2_t) + sizeof(boot_data_t)", plus the
374 * flash_offset space.The ROM code only need to copy this size
375 * to run the plugin code. However, later when copy the whole
376 * U-Boot image to DDR, the ROM code use memcpy to copy the
377 * first part of the image, and use the storage read function
378 * to get the remaining part. This requires the dividing point
379 * must be multiple of storage sector size. Here we set the
380 * first section to be MAX_PLUGIN_CODE_SIZE(64KB) for this
381 * purpose.
382 */
383 hdr_v2->boot_data.size = MAX_PLUGIN_CODE_SIZE;
384
385 /* Security feature are not supported */
386 fhdr_v2->csf = 0;
387
388 next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 +
389 imximage_plugin_size);
390
391 next_fhdr_v2 = &next_hdr_v2->fhdr;
392
393 next_fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
394 next_fhdr_v2->header.length =
395 cpu_to_be16(sizeof(flash_header_v2_t));
396 next_fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
397
398 next_fhdr_v2->entry = entry_point;
399 hdr_base = entry_point - sizeof(struct imx_header);
400 next_fhdr_v2->reserved1 = 0;
401 next_fhdr_v2->reserved2 = 0;
402 next_fhdr_v2->self = hdr_base + imximage_plugin_size;
403
404 next_fhdr_v2->dcd_ptr = 0;
405 next_fhdr_v2->boot_data_ptr = next_fhdr_v2->self +
406 offsetof(imx_header_v2_t, boot_data);
407
408 next_hdr_v2->boot_data.start = hdr_base - flash_offset;
409
410 header_size_ptr = &next_hdr_v2->boot_data.size;
411
412 next_hdr_v2->boot_data.plugin = 0;
413
414 next_fhdr_v2->csf = 0;
415 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000416}
417
York Sun72048bc2013-09-20 12:24:44 -0700418static void set_hdr_func(void)
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000419{
420 switch (imximage_version) {
421 case IMXIMAGE_V1:
422 set_dcd_val = set_dcd_val_v1;
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500423 set_dcd_param = NULL;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000424 set_dcd_rst = set_dcd_rst_v1;
425 set_imx_hdr = set_imx_hdr_v1;
Troy Kisky4d5fa982012-10-03 15:47:03 +0000426 max_dcd_entries = MAX_HW_CFG_SIZE_V1;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000427 break;
428 case IMXIMAGE_V2:
Troy Kisky61903b72015-09-14 18:06:31 -0700429 gd_last_cmd = NULL;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000430 set_dcd_val = set_dcd_val_v2;
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500431 set_dcd_param = set_dcd_param_v2;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000432 set_dcd_rst = set_dcd_rst_v2;
433 set_imx_hdr = set_imx_hdr_v2;
Troy Kisky4d5fa982012-10-03 15:47:03 +0000434 max_dcd_entries = MAX_HW_CFG_SIZE_V2;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000435 break;
436 default:
437 err_imximage_version(imximage_version);
438 break;
439 }
440}
441
442static void print_hdr_v1(struct imx_header *imx_hdr)
443{
444 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
445 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
446 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
447 uint32_t size, length, ver;
448
449 size = dcd_v1->preamble.length;
450 if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
451 fprintf(stderr,
452 "Error: Image corrupt DCD size %d exceed maximum %d\n",
453 (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
454 MAX_HW_CFG_SIZE_V1);
455 exit(EXIT_FAILURE);
456 }
457
458 length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
459 ver = detect_imximage_version(imx_hdr);
460
461 printf("Image Type: Freescale IMX Boot Image\n");
462 printf("Image Ver: %x", ver);
463 printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
464 printf("Data Size: ");
465 genimg_print_size(dcd_v1->addr_data[length].type);
466 printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
467 printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
468}
469
470static void print_hdr_v2(struct imx_header *imx_hdr)
471{
472 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
473 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
Peng Fanb55e4f42016-10-11 14:29:09 +0800474 dcd_v2_t *dcd_v2 = &hdr_v2->data.dcd_table;
475 uint32_t size, version, plugin;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000476
Peng Fanb55e4f42016-10-11 14:29:09 +0800477 plugin = hdr_v2->boot_data.plugin;
478 if (!plugin) {
479 size = be16_to_cpu(dcd_v2->header.length);
480 if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
481 fprintf(stderr,
482 "Error: Image corrupt DCD size %d exceed maximum %d\n",
483 (uint32_t)(size / sizeof(dcd_addr_data_t)),
484 MAX_HW_CFG_SIZE_V2);
485 exit(EXIT_FAILURE);
486 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000487 }
488
489 version = detect_imximage_version(imx_hdr);
490
491 printf("Image Type: Freescale IMX Boot Image\n");
492 printf("Image Ver: %x", version);
493 printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
Peng Fanb55e4f42016-10-11 14:29:09 +0800494 printf("Mode: %s\n", plugin ? "PLUGIN" : "DCD");
495 if (!plugin) {
496 printf("Data Size: ");
497 genimg_print_size(hdr_v2->boot_data.size);
498 printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
499 printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
500 if (fhdr_v2->csf && (imximage_ivt_offset != UNDEFINED) &&
501 (imximage_csf_size != UNDEFINED)) {
Eric Nelsone5491f32016-11-16 17:13:41 -0700502 uint16_t dcdlen;
503 int offs;
504
505 dcdlen = hdr_v2->data.dcd_table.header.length;
506 offs = (char *)&hdr_v2->data.dcd_table
507 - (char *)hdr_v2;
508
Peng Fanb55e4f42016-10-11 14:29:09 +0800509 printf("HAB Blocks: %08x %08x %08x\n",
510 (uint32_t)fhdr_v2->self, 0,
511 hdr_v2->boot_data.size - imximage_ivt_offset -
512 imximage_csf_size);
Eric Nelsone5491f32016-11-16 17:13:41 -0700513 printf("DCD Blocks: 00910000 %08x %08x\n",
514 offs, be16_to_cpu(dcdlen));
Peng Fanb55e4f42016-10-11 14:29:09 +0800515 }
516 } else {
517 imx_header_v2_t *next_hdr_v2;
518 flash_header_v2_t *next_fhdr_v2;
519
520 /*First Header*/
521 printf("Plugin Data Size: ");
522 genimg_print_size(hdr_v2->boot_data.size);
523 printf("Plugin Code Size: ");
524 genimg_print_size(imximage_plugin_size);
525 printf("Plugin Load Address: %08x\n", hdr_v2->boot_data.start);
526 printf("Plugin Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
527
528 /*Second Header*/
529 next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 +
530 imximage_plugin_size);
531 next_fhdr_v2 = &next_hdr_v2->fhdr;
532 printf("U-Boot Data Size: ");
533 genimg_print_size(next_hdr_v2->boot_data.size);
534 printf("U-Boot Load Address: %08x\n",
535 next_hdr_v2->boot_data.start);
536 printf("U-Boot Entry Point: %08x\n",
537 (uint32_t)next_fhdr_v2->entry);
Stefano Babic0187c982013-06-27 11:42:38 +0200538 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000539}
540
Peng Fanb55e4f42016-10-11 14:29:09 +0800541static void copy_plugin_code(struct imx_header *imxhdr, char *plugin_file)
542{
Peng Fan2a380cc2016-11-04 10:33:15 +0800543 int ifd;
Peng Fanb55e4f42016-10-11 14:29:09 +0800544 struct stat sbuf;
545 char *plugin_buf = imxhdr->header.hdr_v2.data.plugin_code;
546 char *ptr;
547
548 ifd = open(plugin_file, O_RDONLY|O_BINARY);
Peng Fan2a380cc2016-11-04 10:33:15 +0800549 if (ifd < 0) {
550 fprintf(stderr, "Can't open %s: %s\n",
551 plugin_file,
552 strerror(errno));
553 exit(EXIT_FAILURE);
554 }
555
Peng Fanb55e4f42016-10-11 14:29:09 +0800556 if (fstat(ifd, &sbuf) < 0) {
557 fprintf(stderr, "Can't stat %s: %s\n",
558 plugin_file,
559 strerror(errno));
560 exit(EXIT_FAILURE);
561 }
562
563 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
564 if (ptr == MAP_FAILED) {
565 fprintf(stderr, "Can't read %s: %s\n",
566 plugin_file,
567 strerror(errno));
568 exit(EXIT_FAILURE);
569 }
570
571 if (sbuf.st_size > MAX_PLUGIN_CODE_SIZE) {
572 printf("plugin binary size too large\n");
573 exit(EXIT_FAILURE);
574 }
575
576 memcpy(plugin_buf, ptr, sbuf.st_size);
577 imximage_plugin_size = sbuf.st_size;
578
579 (void) munmap((void *)ptr, sbuf.st_size);
580 (void) close(ifd);
581
582 imxhdr->header.hdr_v2.boot_data.plugin = 1;
583}
584
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000585static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
586 char *name, int lineno, int fld, int dcd_len)
587{
588 int value;
589 static int cmd_ver_first = ~0;
590
591 switch (cmd) {
592 case CMD_IMAGE_VERSION:
593 imximage_version = get_cfg_value(token, name, lineno);
594 if (cmd_ver_first == 0) {
595 fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
596 "command need be the first before other "
597 "valid command in the file\n", name, lineno);
598 exit(EXIT_FAILURE);
599 }
600 cmd_ver_first = 1;
York Sun72048bc2013-09-20 12:24:44 -0700601 set_hdr_func();
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000602 break;
603 case CMD_BOOT_FROM:
Stefano Babic377e3672013-06-26 23:50:06 +0200604 imximage_ivt_offset = get_table_entry_id(imximage_boot_offset,
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000605 "imximage boot option", token);
Stefano Babic3150f922013-06-26 18:08:37 +0200606 if (imximage_ivt_offset == -1) {
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000607 fprintf(stderr, "Error: %s[%d] -Invalid boot device"
608 "(%s)\n", name, lineno, token);
609 exit(EXIT_FAILURE);
610 }
Stefano Babic377e3672013-06-26 23:50:06 +0200611
612 imximage_init_loadsize =
613 get_table_entry_id(imximage_boot_loadsize,
614 "imximage boot option", token);
615
616 if (imximage_init_loadsize == -1) {
617 fprintf(stderr,
618 "Error: %s[%d] -Invalid boot device(%s)\n",
619 name, lineno, token);
620 exit(EXIT_FAILURE);
621 }
Stefano Babic01390af2013-08-19 19:03:20 +0200622
623 /*
624 * The SOC loads from the storage starting at address 0
625 * then ensures that the load size contains the offset
626 */
627 if (imximage_init_loadsize < imximage_ivt_offset)
628 imximage_init_loadsize = imximage_ivt_offset;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000629 if (unlikely(cmd_ver_first != 1))
630 cmd_ver_first = 0;
631 break;
Marek Vasut6cb83822013-04-25 10:16:02 +0000632 case CMD_BOOT_OFFSET:
Stefano Babic3150f922013-06-26 18:08:37 +0200633 imximage_ivt_offset = get_cfg_value(token, name, lineno);
Marek Vasut6cb83822013-04-25 10:16:02 +0000634 if (unlikely(cmd_ver_first != 1))
635 cmd_ver_first = 0;
636 break;
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500637 case CMD_WRITE_DATA:
638 case CMD_WRITE_CLR_BIT:
639 case CMD_CHECK_BITS_SET:
640 case CMD_CHECK_BITS_CLR:
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000641 value = get_cfg_value(token, name, lineno);
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500642 if (set_dcd_param)
643 (*set_dcd_param)(imxhdr, dcd_len, cmd);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000644 (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
645 if (unlikely(cmd_ver_first != 1))
646 cmd_ver_first = 0;
647 break;
Stefano Babic0187c982013-06-27 11:42:38 +0200648 case CMD_CSF:
649 if (imximage_version != 2) {
650 fprintf(stderr,
651 "Error: %s[%d] - CSF only supported for VERSION 2(%s)\n",
652 name, lineno, token);
653 exit(EXIT_FAILURE);
654 }
655 imximage_csf_size = get_cfg_value(token, name, lineno);
656 if (unlikely(cmd_ver_first != 1))
657 cmd_ver_first = 0;
658 break;
Peng Fanb55e4f42016-10-11 14:29:09 +0800659 case CMD_PLUGIN:
660 plugin_image = 1;
661 copy_plugin_code(imxhdr, token);
662 break;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000663 }
664}
665
666static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
667 char *token, char *name, int lineno, int fld, int *dcd_len)
668{
669 int value;
670
671 switch (fld) {
672 case CFG_COMMAND:
673 *cmd = get_table_entry_id(imximage_cmds,
674 "imximage commands", token);
675 if (*cmd < 0) {
676 fprintf(stderr, "Error: %s[%d] - Invalid command"
677 "(%s)\n", name, lineno, token);
678 exit(EXIT_FAILURE);
679 }
680 break;
681 case CFG_REG_SIZE:
682 parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
683 break;
684 case CFG_REG_ADDRESS:
685 case CFG_REG_VALUE:
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500686 switch(*cmd) {
687 case CMD_WRITE_DATA:
688 case CMD_WRITE_CLR_BIT:
689 case CMD_CHECK_BITS_SET:
690 case CMD_CHECK_BITS_CLR:
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000691
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500692 value = get_cfg_value(token, name, lineno);
693 if (set_dcd_param)
694 (*set_dcd_param)(imxhdr, *dcd_len, *cmd);
695 (*set_dcd_val)(imxhdr, name, lineno, fld, value,
696 *dcd_len);
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000697
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500698 if (fld == CFG_REG_VALUE) {
699 (*dcd_len)++;
700 if (*dcd_len > max_dcd_entries) {
701 fprintf(stderr, "Error: %s[%d] -"
702 "DCD table exceeds maximum size(%d)\n",
703 name, lineno, max_dcd_entries);
704 exit(EXIT_FAILURE);
705 }
Troy Kisky4d5fa982012-10-03 15:47:03 +0000706 }
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500707 break;
Peng Fanb55e4f42016-10-11 14:29:09 +0800708 case CMD_PLUGIN:
709 value = get_cfg_value(token, name, lineno);
710 imximage_iram_free_start = value;
711 break;
Adrian Alonso0b7f7c32015-07-20 19:04:55 -0500712 default:
713 break;
Troy Kisky4d5fa982012-10-03 15:47:03 +0000714 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000715 break;
716 default:
717 break;
718 }
719}
720static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
Stefano Babic8edcde52010-01-20 18:19:10 +0100721{
722 FILE *fd = NULL;
723 char *line = NULL;
724 char *token, *saveptr1, *saveptr2;
725 int lineno = 0;
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000726 int fld;
Kim Phillips0ad22702010-02-22 19:37:56 -0600727 size_t len;
Stefano Babic8edcde52010-01-20 18:19:10 +0100728 int dcd_len = 0;
Stefano Babic8edcde52010-01-20 18:19:10 +0100729 int32_t cmd;
730
731 fd = fopen(name, "r");
732 if (fd == 0) {
733 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
734 exit(EXIT_FAILURE);
735 }
736
Stefano Babic01390af2013-08-19 19:03:20 +0200737 /*
738 * Very simple parsing, line starting with # are comments
Stefano Babic8edcde52010-01-20 18:19:10 +0100739 * and are dropped
740 */
741 while ((getline(&line, &len, fd)) > 0) {
742 lineno++;
743
744 token = strtok_r(line, "\r\n", &saveptr1);
745 if (token == NULL)
746 continue;
747
748 /* Check inside the single line */
749 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
750 line = token; ; line = NULL, fld++) {
751 token = strtok_r(line, " \t", &saveptr2);
752 if (token == NULL)
753 break;
754
755 /* Drop all text starting with '#' as comments */
756 if (token[0] == '#')
757 break;
758
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000759 parse_cfg_fld(imxhdr, &cmd, token, name,
760 lineno, fld, &dcd_len);
Stefano Babic8edcde52010-01-20 18:19:10 +0100761 }
762
Stefano Babic8edcde52010-01-20 18:19:10 +0100763 }
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000764
765 (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
Stefano Babic8edcde52010-01-20 18:19:10 +0100766 fclose(fd);
767
Troy Kisky8d8cc822012-10-03 15:47:05 +0000768 /* Exit if there is no BOOT_FROM field specifying the flash_offset */
Stefano Babic3150f922013-06-26 18:08:37 +0200769 if (imximage_ivt_offset == FLASH_OFFSET_UNDEFINED) {
Troy Kisky8d8cc822012-10-03 15:47:05 +0000770 fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name);
771 exit(EXIT_FAILURE);
772 }
Stefano Babic5b28e912010-02-05 15:16:02 +0100773 return dcd_len;
Stefano Babic8edcde52010-01-20 18:19:10 +0100774}
775
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000776
777static int imximage_check_image_types(uint8_t type)
778{
779 if (type == IH_TYPE_IMXIMAGE)
780 return EXIT_SUCCESS;
781 else
782 return EXIT_FAILURE;
783}
784
785static int imximage_verify_header(unsigned char *ptr, int image_size,
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700786 struct image_tool_params *params)
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000787{
788 struct imx_header *imx_hdr = (struct imx_header *) ptr;
789
790 if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
791 return -FDT_ERR_BADSTRUCTURE;
792
793 return 0;
794}
795
796static void imximage_print_header(const void *ptr)
797{
798 struct imx_header *imx_hdr = (struct imx_header *) ptr;
799 uint32_t version = detect_imximage_version(imx_hdr);
800
801 switch (version) {
802 case IMXIMAGE_V1:
803 print_hdr_v1(imx_hdr);
804 break;
805 case IMXIMAGE_V2:
806 print_hdr_v2(imx_hdr);
807 break;
808 default:
809 err_imximage_version(version);
810 break;
811 }
812}
813
Stefano Babic8edcde52010-01-20 18:19:10 +0100814static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700815 struct image_tool_params *params)
Stefano Babic8edcde52010-01-20 18:19:10 +0100816{
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000817 struct imx_header *imxhdr = (struct imx_header *)ptr;
818 uint32_t dcd_len;
Peng Fanb55e4f42016-10-11 14:29:09 +0800819 uint32_t header_size;
Stefano Babic8edcde52010-01-20 18:19:10 +0100820
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000821 /*
822 * In order to not change the old imx cfg file
823 * by adding VERSION command into it, here need
824 * set up function ptr group to V1 by default.
825 */
826 imximage_version = IMXIMAGE_V1;
Dirk Behme49d3e272012-02-22 22:50:19 +0000827 /* Be able to detect if the cfg file has no BOOT_FROM tag */
Stefano Babic3150f922013-06-26 18:08:37 +0200828 imximage_ivt_offset = FLASH_OFFSET_UNDEFINED;
Stefano Babic0187c982013-06-27 11:42:38 +0200829 imximage_csf_size = 0;
York Sun72048bc2013-09-20 12:24:44 -0700830 set_hdr_func();
Stefano Babic8edcde52010-01-20 18:19:10 +0100831
832 /* Parse dcd configuration file */
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000833 dcd_len = parse_cfg_file(imxhdr, params->imagename);
Stefano Babic8edcde52010-01-20 18:19:10 +0100834
Martin Kaiser97f17fa2017-01-02 22:24:17 +0100835 if (imximage_version == IMXIMAGE_V1)
836 header_size = sizeof(flash_header_v1_t);
837 else {
Peng Fanb55e4f42016-10-11 14:29:09 +0800838 header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t);
839 if (!plugin_image)
840 header_size += sizeof(dcd_v2_t);
841 else
842 header_size += MAX_PLUGIN_CODE_SIZE;
Ye.Li03ea24b2014-08-20 16:55:32 +0800843 }
844
Martin Kaiser97f17fa2017-01-02 22:24:17 +0100845 if (imximage_init_loadsize < imximage_ivt_offset + header_size)
846 imximage_init_loadsize = imximage_ivt_offset + header_size;
847
Liu Hui-R643438a1edd72011-01-19 09:40:26 +0000848 /* Set the imx header */
Stefano Babic3150f922013-06-26 18:08:37 +0200849 (*set_imx_hdr)(imxhdr, dcd_len, params->ep, imximage_ivt_offset);
Fabio Estevam1411fb32013-01-03 08:24:33 +0000850
851 /*
852 * ROM bug alert
Marek Vasut895d9962013-04-21 05:52:22 +0000853 *
854 * MX53 only loads 512 byte multiples in case of SD boot.
855 * MX53 only loads NAND page multiples in case of NAND boot and
856 * supports up to 4096 byte large pages, thus align to 4096.
857 *
858 * The remaining fraction of a block bytes would not be loaded!
Fabio Estevam1411fb32013-01-03 08:24:33 +0000859 */
Ye.Lide979802014-10-30 17:54:08 +0800860 *header_size_ptr = ROUND((sbuf->st_size + imximage_ivt_offset), 4096);
Stefano Babic0187c982013-06-27 11:42:38 +0200861
862 if (csf_ptr && imximage_csf_size) {
863 *csf_ptr = params->ep - imximage_init_loadsize +
864 *header_size_ptr;
865 *header_size_ptr += imximage_csf_size;
866 }
Stefano Babic8edcde52010-01-20 18:19:10 +0100867}
868
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700869int imximage_check_params(struct image_tool_params *params)
Stefano Babic8edcde52010-01-20 18:19:10 +0100870{
871 if (!params)
872 return CFG_INVALID;
873 if (!strlen(params->imagename)) {
874 fprintf(stderr, "Error: %s - Configuration file not specified, "
875 "it is needed for imximage generation\n",
876 params->cmdname);
877 return CFG_INVALID;
878 }
879 /*
880 * Check parameters:
881 * XIP is not allowed and verify that incompatible
882 * parameters are not sent at the same time
883 * For example, if list is required a data image must not be provided
884 */
885 return (params->dflag && (params->fflag || params->lflag)) ||
886 (params->fflag && (params->dflag || params->lflag)) ||
887 (params->lflag && (params->dflag || params->fflag)) ||
888 (params->xflag) || !(strlen(params->imagename));
889}
890
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700891static int imximage_generate(struct image_tool_params *params,
Stefano Babic01390af2013-08-19 19:03:20 +0200892 struct image_type_params *tparams)
893{
894 struct imx_header *imxhdr;
895 size_t alloc_len;
896 struct stat sbuf;
897 char *datafile = params->datafile;
Peng Fanb55e4f42016-10-11 14:29:09 +0800898 uint32_t pad_len, header_size;
Stefano Babic01390af2013-08-19 19:03:20 +0200899
900 memset(&imximage_header, 0, sizeof(imximage_header));
901
902 /*
903 * In order to not change the old imx cfg file
904 * by adding VERSION command into it, here need
905 * set up function ptr group to V1 by default.
906 */
907 imximage_version = IMXIMAGE_V1;
908 /* Be able to detect if the cfg file has no BOOT_FROM tag */
909 imximage_ivt_offset = FLASH_OFFSET_UNDEFINED;
910 imximage_csf_size = 0;
York Sun72048bc2013-09-20 12:24:44 -0700911 set_hdr_func();
Stefano Babic01390af2013-08-19 19:03:20 +0200912
913 /* Parse dcd configuration file */
914 parse_cfg_file(&imximage_header, params->imagename);
915
Martin Kaiser97f17fa2017-01-02 22:24:17 +0100916 if (imximage_version == IMXIMAGE_V1)
917 header_size = sizeof(imx_header_v1_t);
918 else {
Peng Fanb55e4f42016-10-11 14:29:09 +0800919 header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t);
920 if (!plugin_image)
921 header_size += sizeof(dcd_v2_t);
922 else
923 header_size += MAX_PLUGIN_CODE_SIZE;
Stefano Babic01390af2013-08-19 19:03:20 +0200924 }
925
Martin Kaiser97f17fa2017-01-02 22:24:17 +0100926 if (imximage_init_loadsize < imximage_ivt_offset + header_size)
927 imximage_init_loadsize = imximage_ivt_offset + header_size;
928
929 alloc_len = imximage_init_loadsize - imximage_ivt_offset;
930
Peng Fanb55e4f42016-10-11 14:29:09 +0800931 if (alloc_len < header_size) {
Stefano Babic01390af2013-08-19 19:03:20 +0200932 fprintf(stderr, "%s: header error\n",
933 params->cmdname);
934 exit(EXIT_FAILURE);
935 }
936
937 imxhdr = malloc(alloc_len);
938
939 if (!imxhdr) {
940 fprintf(stderr, "%s: malloc return failure: %s\n",
941 params->cmdname, strerror(errno));
942 exit(EXIT_FAILURE);
943 }
944
945 memset(imxhdr, 0, alloc_len);
946
947 tparams->header_size = alloc_len;
948 tparams->hdr = imxhdr;
949
950 /* determine data image file length */
951
952 if (stat(datafile, &sbuf) < 0) {
953 fprintf(stderr, "%s: Can't stat %s: %s\n",
954 params->cmdname, datafile, strerror(errno));
955 exit(EXIT_FAILURE);
956 }
957
958 pad_len = ROUND(sbuf.st_size, 4096) - sbuf.st_size;
959
Martin Kaiser97f17fa2017-01-02 22:24:17 +0100960 return pad_len;
Stefano Babic01390af2013-08-19 19:03:20 +0200961}
962
963
Stefano Babic8edcde52010-01-20 18:19:10 +0100964/*
965 * imximage parameters
966 */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200967U_BOOT_IMAGE_TYPE(
968 imximage,
969 "Freescale i.MX Boot Image support",
970 0,
971 NULL,
972 imximage_check_params,
973 imximage_verify_header,
974 imximage_print_header,
975 imximage_set_header,
976 NULL,
977 imximage_check_image_types,
978 NULL,
979 imximage_generate
980);