blob: 0da48a733d4296f521cc3cbc8b2a0513e4bf7e69 [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", },
30 {-1, "", "", },
Stefano Babic7b07f092010-01-20 18:19:10 +010031};
32
33/*
34 * Supported Boot options for configuration file
35 * this is needed to set the correct flash offset
36 */
Stefano Babicdc39a3e2013-06-26 23:50:06 +020037static table_entry_t imximage_boot_offset[] = {
Stefano Babic7b07f092010-01-20 18:19:10 +010038 {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",},
Dirk Behme40a9c462012-01-11 23:28:32 +000039 {FLASH_OFFSET_NAND, "nand", "NAND Flash", },
Dirk Behmedfbf6ce2012-01-11 23:28:31 +000040 {FLASH_OFFSET_NOR, "nor", "NOR Flash", },
41 {FLASH_OFFSET_SATA, "sata", "SATA Disk", },
Dirk Behme40a9c462012-01-11 23:28:32 +000042 {FLASH_OFFSET_SD, "sd", "SD Card", },
43 {FLASH_OFFSET_SPI, "spi", "SPI Flash", },
Ye.Lif16cde02015-01-13 15:53:06 +080044 {FLASH_OFFSET_QSPI, "qspi", "QSPI NOR Flash",},
Stefano Babic7b07f092010-01-20 18:19:10 +010045 {-1, "", "Invalid", },
46};
47
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000048/*
Stefano Babicdc39a3e2013-06-26 23:50:06 +020049 * Supported Boot options for configuration file
50 * this is needed to determine the initial load size
51 */
52static table_entry_t imximage_boot_loadsize[] = {
53 {FLASH_LOADSIZE_ONENAND, "onenand", "OneNAND Flash",},
54 {FLASH_LOADSIZE_NAND, "nand", "NAND Flash", },
55 {FLASH_LOADSIZE_NOR, "nor", "NOR Flash", },
56 {FLASH_LOADSIZE_SATA, "sata", "SATA Disk", },
57 {FLASH_LOADSIZE_SD, "sd", "SD Card", },
58 {FLASH_LOADSIZE_SPI, "spi", "SPI Flash", },
Ye.Lif16cde02015-01-13 15:53:06 +080059 {FLASH_LOADSIZE_QSPI, "qspi", "QSPI NOR Flash",},
Stefano Babicdc39a3e2013-06-26 23:50:06 +020060 {-1, "", "Invalid", },
61};
62
63/*
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000064 * IMXIMAGE version definition for i.MX chips
65 */
66static table_entry_t imximage_versions[] = {
67 {IMXIMAGE_V1, "", " (i.MX25/35/51 compatible)", },
Adrian Alonso73e57322015-07-20 19:04:55 -050068 {IMXIMAGE_V2, "", " (i.MX53/6/7 compatible)", },
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000069 {-1, "", " (Invalid)", },
70};
Stefano Babic7b07f092010-01-20 18:19:10 +010071
72static struct imx_header imximage_header;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000073static uint32_t imximage_version;
Stefano Babic4aa97492013-06-27 11:42:38 +020074/*
75 * Image Vector Table Offset
76 * Initialized to a wrong not 4-bytes aligned address to
77 * check if it is was set by the cfg file.
78 */
79static uint32_t imximage_ivt_offset = UNDEFINED;
80static uint32_t imximage_csf_size = UNDEFINED;
Stefano Babicdc39a3e2013-06-26 23:50:06 +020081/* Initial Load Region Size */
82static uint32_t imximage_init_loadsize;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000083
84static set_dcd_val_t set_dcd_val;
Adrian Alonso73e57322015-07-20 19:04:55 -050085static set_dcd_param_t set_dcd_param;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +000086static set_dcd_rst_t set_dcd_rst;
87static set_imx_hdr_t set_imx_hdr;
Troy Kisky9bd5b852012-10-03 15:47:03 +000088static uint32_t max_dcd_entries;
Troy Kisky7b922cb2012-10-03 15:47:07 +000089static uint32_t *header_size_ptr;
Stefano Babic4aa97492013-06-27 11:42:38 +020090static uint32_t *csf_ptr;
Stefano Babic7b07f092010-01-20 18:19:10 +010091
92static uint32_t get_cfg_value(char *token, char *name, int linenr)
93{
94 char *endptr;
95 uint32_t value;
96
97 errno = 0;
98 value = strtoul(token, &endptr, 16);
99 if (errno || (token == endptr)) {
100 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
101 name, linenr, token);
102 exit(EXIT_FAILURE);
103 }
104 return value;
105}
106
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000107static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
Stefano Babic7b07f092010-01-20 18:19:10 +0100108{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000109 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
110 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
111 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
112 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
113
114 /* Try to detect V1 */
115 if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) &&
116 (hdr_v1->dcd_table.preamble.barker == DCD_BARKER))
117 return IMXIMAGE_V1;
118
119 /* Try to detect V2 */
120 if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
121 (hdr_v2->dcd_table.header.tag == DCD_HEADER_TAG))
122 return IMXIMAGE_V2;
123
124 return IMXIMAGE_VER_INVALID;
Stefano Babic7b07f092010-01-20 18:19:10 +0100125}
126
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000127static void err_imximage_version(int version)
Stefano Babic7b07f092010-01-20 18:19:10 +0100128{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000129 fprintf(stderr,
130 "Error: Unsupported imximage version:%d\n", version);
Stefano Babic7b07f092010-01-20 18:19:10 +0100131
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000132 exit(EXIT_FAILURE);
133}
Stefano Babic7b07f092010-01-20 18:19:10 +0100134
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000135static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno,
136 int fld, uint32_t value, uint32_t off)
137{
138 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
Stefano Babic7b07f092010-01-20 18:19:10 +0100139
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000140 switch (fld) {
141 case CFG_REG_SIZE:
142 /* Byte, halfword, word */
143 if ((value != 1) && (value != 2) && (value != 4)) {
144 fprintf(stderr, "Error: %s[%d] - "
145 "Invalid register size " "(%d)\n",
146 name, lineno, value);
147 exit(EXIT_FAILURE);
148 }
149 dcd_v1->addr_data[off].type = value;
150 break;
151 case CFG_REG_ADDRESS:
152 dcd_v1->addr_data[off].addr = value;
153 break;
154 case CFG_REG_VALUE:
155 dcd_v1->addr_data[off].value = value;
156 break;
157 default:
158 break;
Stefano Babic7b07f092010-01-20 18:19:10 +0100159
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000160 }
Stefano Babic7b07f092010-01-20 18:19:10 +0100161}
162
Adrian Alonso73e57322015-07-20 19:04:55 -0500163static void set_dcd_param_v2(struct imx_header *imxhdr, uint32_t dcd_len,
164 int32_t cmd)
165{
166 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
167
168 switch (cmd) {
169 case CMD_WRITE_DATA:
170 dcd_v2->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG;
171 dcd_v2->write_dcd_command.length = cpu_to_be16(
172 dcd_len * sizeof(dcd_addr_data_t) + 4);
173 dcd_v2->write_dcd_command.param = DCD_WRITE_DATA_PARAM;
174 break;
175 case CMD_WRITE_CLR_BIT:
176 dcd_v2->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG;
177 dcd_v2->write_dcd_command.length = cpu_to_be16(
178 dcd_len * sizeof(dcd_addr_data_t) + 4);
179 dcd_v2->write_dcd_command.param = DCD_WRITE_CLR_BIT_PARAM;
180 break;
181 /*
182 * Check data command only supports one entry,
183 * so use 0xC = size(address + value + command).
184 */
185 case CMD_CHECK_BITS_SET:
186 dcd_v2->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG;
187 dcd_v2->write_dcd_command.length = cpu_to_be16(0xC);
188 dcd_v2->write_dcd_command.param = DCD_CHECK_BITS_SET_PARAM;
189 break;
190 case CMD_CHECK_BITS_CLR:
191 dcd_v2->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG;
192 dcd_v2->write_dcd_command.length = cpu_to_be16(0xC);
193 dcd_v2->write_dcd_command.param = DCD_CHECK_BITS_SET_PARAM;
194 break;
195 default:
196 break;
197 }
198}
199
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000200static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno,
201 int fld, uint32_t value, uint32_t off)
Stefano Babic7b07f092010-01-20 18:19:10 +0100202{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000203 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
204
205 switch (fld) {
206 case CFG_REG_ADDRESS:
207 dcd_v2->addr_data[off].addr = cpu_to_be32(value);
208 break;
209 case CFG_REG_VALUE:
210 dcd_v2->addr_data[off].value = cpu_to_be32(value);
211 break;
212 default:
213 break;
214
215 }
216}
217
218/*
219 * Complete setting up the rest field of DCD of V1
220 * such as barker code and DCD data length.
221 */
222static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len,
223 char *name, int lineno)
224{
225 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
226
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000227 dcd_v1->preamble.barker = DCD_BARKER;
228 dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
229}
230
231/*
232 * Complete setting up the reset field of DCD of V2
233 * such as DCD tag, version, length, etc.
234 */
235static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len,
236 char *name, int lineno)
237{
238 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
239
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000240 dcd_v2->header.tag = DCD_HEADER_TAG;
241 dcd_v2->header.length = cpu_to_be16(
242 dcd_len * sizeof(dcd_addr_data_t) + 8);
243 dcd_v2->header.version = DCD_VERSION;
Adrian Alonso73e57322015-07-20 19:04:55 -0500244 set_dcd_param_v2(imxhdr, dcd_len, CMD_WRITE_DATA);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000245}
246
247static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kisky7bb92202012-10-03 15:47:08 +0000248 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000249{
250 imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1;
251 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
252 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
Troy Kisky2d7c29f2012-10-03 15:47:09 +0000253 uint32_t hdr_base;
Troy Kisky7b922cb2012-10-03 15:47:07 +0000254 uint32_t header_length = (((char *)&dcd_v1->addr_data[dcd_len].addr)
255 - ((char *)imxhdr));
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000256
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000257 /* Set magic number */
258 fhdr_v1->app_code_barker = APP_CODE_BARKER;
259
Stefano Babicdc39a3e2013-06-26 23:50:06 +0200260 /* TODO: check i.MX image V1 handling, for now use 'old' style */
261 hdr_base = entry_point - 4096;
Troy Kisky2d7c29f2012-10-03 15:47:09 +0000262 fhdr_v1->app_dest_ptr = hdr_base - flash_offset;
Troy Kisky7bb92202012-10-03 15:47:08 +0000263 fhdr_v1->app_code_jump_vector = entry_point;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000264
Troy Kisky2d7c29f2012-10-03 15:47:09 +0000265 fhdr_v1->dcd_ptr_ptr = hdr_base + offsetof(flash_header_v1_t, dcd_ptr);
266 fhdr_v1->dcd_ptr = hdr_base + offsetof(imx_header_v1_t, dcd_table);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000267
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000268 /* Security feature are not supported */
269 fhdr_v1->app_code_csf = 0;
270 fhdr_v1->super_root_key = 0;
Troy Kisky7b922cb2012-10-03 15:47:07 +0000271 header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000272}
273
274static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
Troy Kisky7bb92202012-10-03 15:47:08 +0000275 uint32_t entry_point, uint32_t flash_offset)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000276{
277 imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
278 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
Troy Kisky2d7c29f2012-10-03 15:47:09 +0000279 uint32_t hdr_base;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000280
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000281 /* Set magic number */
282 fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
283 fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
284 fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
285
Troy Kisky7bb92202012-10-03 15:47:08 +0000286 fhdr_v2->entry = entry_point;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000287 fhdr_v2->reserved1 = fhdr_v2->reserved2 = 0;
Stefano Babicdc39a3e2013-06-26 23:50:06 +0200288 hdr_base = entry_point - imximage_init_loadsize +
289 flash_offset;
290 fhdr_v2->self = hdr_base;
Baruch Siach9c2cf9a2015-07-09 18:19:11 +0300291 if (dcd_len > 0)
292 fhdr_v2->dcd_ptr = hdr_base
293 + offsetof(imx_header_v2_t, dcd_table);
294 else
295 fhdr_v2->dcd_ptr = 0;
Troy Kisky2d7c29f2012-10-03 15:47:09 +0000296 fhdr_v2->boot_data_ptr = hdr_base
297 + offsetof(imx_header_v2_t, boot_data);
Stefano Babicdc39a3e2013-06-26 23:50:06 +0200298 hdr_v2->boot_data.start = entry_point - imximage_init_loadsize;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000299
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000300 fhdr_v2->csf = 0;
Stefano Babic4aa97492013-06-27 11:42:38 +0200301
Troy Kisky7b922cb2012-10-03 15:47:07 +0000302 header_size_ptr = &hdr_v2->boot_data.size;
Stefano Babic4aa97492013-06-27 11:42:38 +0200303 csf_ptr = &fhdr_v2->csf;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000304}
305
York Sund061d952013-09-20 12:24:44 -0700306static void set_hdr_func(void)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000307{
308 switch (imximage_version) {
309 case IMXIMAGE_V1:
310 set_dcd_val = set_dcd_val_v1;
Adrian Alonso73e57322015-07-20 19:04:55 -0500311 set_dcd_param = NULL;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000312 set_dcd_rst = set_dcd_rst_v1;
313 set_imx_hdr = set_imx_hdr_v1;
Troy Kisky9bd5b852012-10-03 15:47:03 +0000314 max_dcd_entries = MAX_HW_CFG_SIZE_V1;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000315 break;
316 case IMXIMAGE_V2:
317 set_dcd_val = set_dcd_val_v2;
Adrian Alonso73e57322015-07-20 19:04:55 -0500318 set_dcd_param = set_dcd_param_v2;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000319 set_dcd_rst = set_dcd_rst_v2;
320 set_imx_hdr = set_imx_hdr_v2;
Troy Kisky9bd5b852012-10-03 15:47:03 +0000321 max_dcd_entries = MAX_HW_CFG_SIZE_V2;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000322 break;
323 default:
324 err_imximage_version(imximage_version);
325 break;
326 }
327}
Stefano Babic7b07f092010-01-20 18:19:10 +0100328
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000329static void print_hdr_v1(struct imx_header *imx_hdr)
330{
331 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
332 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
333 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
334 uint32_t size, length, ver;
335
336 size = dcd_v1->preamble.length;
337 if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
Stefano Babic7b07f092010-01-20 18:19:10 +0100338 fprintf(stderr,
339 "Error: Image corrupt DCD size %d exceed maximum %d\n",
Stefano Babic5cdde802010-02-05 15:16:02 +0100340 (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000341 MAX_HW_CFG_SIZE_V1);
342 exit(EXIT_FAILURE);
343 }
344
345 length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
346 ver = detect_imximage_version(imx_hdr);
347
348 printf("Image Type: Freescale IMX Boot Image\n");
349 printf("Image Ver: %x", ver);
350 printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
351 printf("Data Size: ");
352 genimg_print_size(dcd_v1->addr_data[length].type);
353 printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
354 printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
355}
356
357static void print_hdr_v2(struct imx_header *imx_hdr)
358{
359 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
360 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
361 dcd_v2_t *dcd_v2 = &hdr_v2->dcd_table;
362 uint32_t size, version;
363
364 size = be16_to_cpu(dcd_v2->header.length) - 8;
365 if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
366 fprintf(stderr,
367 "Error: Image corrupt DCD size %d exceed maximum %d\n",
368 (uint32_t)(size / sizeof(dcd_addr_data_t)),
369 MAX_HW_CFG_SIZE_V2);
Stefano Babic7b07f092010-01-20 18:19:10 +0100370 exit(EXIT_FAILURE);
371 }
372
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000373 version = detect_imximage_version(imx_hdr);
Stefano Babic7b07f092010-01-20 18:19:10 +0100374
375 printf("Image Type: Freescale IMX Boot Image\n");
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000376 printf("Image Ver: %x", version);
377 printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
Stefano Babic7b07f092010-01-20 18:19:10 +0100378 printf("Data Size: ");
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000379 genimg_print_size(hdr_v2->boot_data.size);
380 printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
381 printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
Stefano Babic4aa97492013-06-27 11:42:38 +0200382 if (fhdr_v2->csf && (imximage_ivt_offset != UNDEFINED) &&
383 (imximage_csf_size != UNDEFINED)) {
384 printf("HAB Blocks: %08x %08x %08x\n",
385 (uint32_t)fhdr_v2->self, 0,
386 hdr_v2->boot_data.size - imximage_ivt_offset -
387 imximage_csf_size);
388 }
Stefano Babic7b07f092010-01-20 18:19:10 +0100389}
390
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000391static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
392 char *name, int lineno, int fld, int dcd_len)
393{
394 int value;
395 static int cmd_ver_first = ~0;
396
397 switch (cmd) {
398 case CMD_IMAGE_VERSION:
399 imximage_version = get_cfg_value(token, name, lineno);
400 if (cmd_ver_first == 0) {
401 fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
402 "command need be the first before other "
403 "valid command in the file\n", name, lineno);
404 exit(EXIT_FAILURE);
405 }
406 cmd_ver_first = 1;
York Sund061d952013-09-20 12:24:44 -0700407 set_hdr_func();
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000408 break;
409 case CMD_BOOT_FROM:
Stefano Babicdc39a3e2013-06-26 23:50:06 +0200410 imximage_ivt_offset = get_table_entry_id(imximage_boot_offset,
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000411 "imximage boot option", token);
Stefano Babice74e7792013-06-26 18:08:37 +0200412 if (imximage_ivt_offset == -1) {
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000413 fprintf(stderr, "Error: %s[%d] -Invalid boot device"
414 "(%s)\n", name, lineno, token);
415 exit(EXIT_FAILURE);
416 }
Stefano Babicdc39a3e2013-06-26 23:50:06 +0200417
418 imximage_init_loadsize =
419 get_table_entry_id(imximage_boot_loadsize,
420 "imximage boot option", token);
421
422 if (imximage_init_loadsize == -1) {
423 fprintf(stderr,
424 "Error: %s[%d] -Invalid boot device(%s)\n",
425 name, lineno, token);
426 exit(EXIT_FAILURE);
427 }
Stefano Babic832eda82013-08-19 19:03:20 +0200428
429 /*
430 * The SOC loads from the storage starting at address 0
431 * then ensures that the load size contains the offset
432 */
433 if (imximage_init_loadsize < imximage_ivt_offset)
434 imximage_init_loadsize = imximage_ivt_offset;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000435 if (unlikely(cmd_ver_first != 1))
436 cmd_ver_first = 0;
437 break;
Marek Vasutd45fd732013-04-25 10:16:02 +0000438 case CMD_BOOT_OFFSET:
Stefano Babice74e7792013-06-26 18:08:37 +0200439 imximage_ivt_offset = get_cfg_value(token, name, lineno);
Marek Vasutd45fd732013-04-25 10:16:02 +0000440 if (unlikely(cmd_ver_first != 1))
441 cmd_ver_first = 0;
442 break;
Adrian Alonso73e57322015-07-20 19:04:55 -0500443 case CMD_WRITE_DATA:
444 case CMD_WRITE_CLR_BIT:
445 case CMD_CHECK_BITS_SET:
446 case CMD_CHECK_BITS_CLR:
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000447 value = get_cfg_value(token, name, lineno);
Adrian Alonso73e57322015-07-20 19:04:55 -0500448 if (set_dcd_param)
449 (*set_dcd_param)(imxhdr, dcd_len, cmd);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000450 (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
451 if (unlikely(cmd_ver_first != 1))
452 cmd_ver_first = 0;
453 break;
Stefano Babic4aa97492013-06-27 11:42:38 +0200454 case CMD_CSF:
455 if (imximage_version != 2) {
456 fprintf(stderr,
457 "Error: %s[%d] - CSF only supported for VERSION 2(%s)\n",
458 name, lineno, token);
459 exit(EXIT_FAILURE);
460 }
461 imximage_csf_size = get_cfg_value(token, name, lineno);
462 if (unlikely(cmd_ver_first != 1))
463 cmd_ver_first = 0;
464 break;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000465 }
466}
467
468static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
469 char *token, char *name, int lineno, int fld, int *dcd_len)
470{
471 int value;
472
473 switch (fld) {
474 case CFG_COMMAND:
475 *cmd = get_table_entry_id(imximage_cmds,
476 "imximage commands", token);
477 if (*cmd < 0) {
478 fprintf(stderr, "Error: %s[%d] - Invalid command"
479 "(%s)\n", name, lineno, token);
480 exit(EXIT_FAILURE);
481 }
482 break;
483 case CFG_REG_SIZE:
484 parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
485 break;
486 case CFG_REG_ADDRESS:
487 case CFG_REG_VALUE:
Adrian Alonso73e57322015-07-20 19:04:55 -0500488 switch(*cmd) {
489 case CMD_WRITE_DATA:
490 case CMD_WRITE_CLR_BIT:
491 case CMD_CHECK_BITS_SET:
492 case CMD_CHECK_BITS_CLR:
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000493
Adrian Alonso73e57322015-07-20 19:04:55 -0500494 value = get_cfg_value(token, name, lineno);
495 if (set_dcd_param)
496 (*set_dcd_param)(imxhdr, *dcd_len, *cmd);
497 (*set_dcd_val)(imxhdr, name, lineno, fld, value,
498 *dcd_len);
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000499
Adrian Alonso73e57322015-07-20 19:04:55 -0500500 if (fld == CFG_REG_VALUE) {
501 (*dcd_len)++;
502 if (*dcd_len > max_dcd_entries) {
503 fprintf(stderr, "Error: %s[%d] -"
504 "DCD table exceeds maximum size(%d)\n",
505 name, lineno, max_dcd_entries);
506 exit(EXIT_FAILURE);
507 }
Troy Kisky9bd5b852012-10-03 15:47:03 +0000508 }
Adrian Alonso73e57322015-07-20 19:04:55 -0500509 break;
510 default:
511 break;
Troy Kisky9bd5b852012-10-03 15:47:03 +0000512 }
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000513 break;
514 default:
515 break;
516 }
517}
518static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
Stefano Babic7b07f092010-01-20 18:19:10 +0100519{
520 FILE *fd = NULL;
521 char *line = NULL;
522 char *token, *saveptr1, *saveptr2;
523 int lineno = 0;
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000524 int fld;
Kim Phillips1c0c2502010-02-22 19:37:56 -0600525 size_t len;
Stefano Babic7b07f092010-01-20 18:19:10 +0100526 int dcd_len = 0;
Stefano Babic7b07f092010-01-20 18:19:10 +0100527 int32_t cmd;
528
529 fd = fopen(name, "r");
530 if (fd == 0) {
531 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
532 exit(EXIT_FAILURE);
533 }
534
Stefano Babic832eda82013-08-19 19:03:20 +0200535 /*
536 * Very simple parsing, line starting with # are comments
Stefano Babic7b07f092010-01-20 18:19:10 +0100537 * and are dropped
538 */
539 while ((getline(&line, &len, fd)) > 0) {
540 lineno++;
541
542 token = strtok_r(line, "\r\n", &saveptr1);
543 if (token == NULL)
544 continue;
545
546 /* Check inside the single line */
547 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
548 line = token; ; line = NULL, fld++) {
549 token = strtok_r(line, " \t", &saveptr2);
550 if (token == NULL)
551 break;
552
553 /* Drop all text starting with '#' as comments */
554 if (token[0] == '#')
555 break;
556
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000557 parse_cfg_fld(imxhdr, &cmd, token, name,
558 lineno, fld, &dcd_len);
Stefano Babic7b07f092010-01-20 18:19:10 +0100559 }
560
Stefano Babic7b07f092010-01-20 18:19:10 +0100561 }
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000562
563 (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
Stefano Babic7b07f092010-01-20 18:19:10 +0100564 fclose(fd);
565
Troy Kiskye55eaf92012-10-03 15:47:05 +0000566 /* Exit if there is no BOOT_FROM field specifying the flash_offset */
Stefano Babice74e7792013-06-26 18:08:37 +0200567 if (imximage_ivt_offset == FLASH_OFFSET_UNDEFINED) {
Troy Kiskye55eaf92012-10-03 15:47:05 +0000568 fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name);
569 exit(EXIT_FAILURE);
570 }
Stefano Babic5cdde802010-02-05 15:16:02 +0100571 return dcd_len;
Stefano Babic7b07f092010-01-20 18:19:10 +0100572}
573
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000574
575static int imximage_check_image_types(uint8_t type)
Stefano Babic7b07f092010-01-20 18:19:10 +0100576{
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000577 if (type == IH_TYPE_IMXIMAGE)
578 return EXIT_SUCCESS;
579 else
580 return EXIT_FAILURE;
581}
Stefano Babic7b07f092010-01-20 18:19:10 +0100582
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000583static int imximage_verify_header(unsigned char *ptr, int image_size,
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700584 struct image_tool_params *params)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000585{
586 struct imx_header *imx_hdr = (struct imx_header *) ptr;
Stefano Babic7b07f092010-01-20 18:19:10 +0100587
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000588 if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
589 return -FDT_ERR_BADSTRUCTURE;
Stefano Babic7b07f092010-01-20 18:19:10 +0100590
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000591 return 0;
592}
Stefano Babic7b07f092010-01-20 18:19:10 +0100593
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000594static void imximage_print_header(const void *ptr)
595{
596 struct imx_header *imx_hdr = (struct imx_header *) ptr;
597 uint32_t version = detect_imximage_version(imx_hdr);
Stefano Babic7b07f092010-01-20 18:19:10 +0100598
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000599 switch (version) {
600 case IMXIMAGE_V1:
601 print_hdr_v1(imx_hdr);
602 break;
603 case IMXIMAGE_V2:
604 print_hdr_v2(imx_hdr);
605 break;
606 default:
607 err_imximage_version(version);
608 break;
609 }
610}
Stefano Babic7b07f092010-01-20 18:19:10 +0100611
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000612static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700613 struct image_tool_params *params)
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000614{
615 struct imx_header *imxhdr = (struct imx_header *)ptr;
616 uint32_t dcd_len;
Stefano Babic7b07f092010-01-20 18:19:10 +0100617
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000618 /*
619 * In order to not change the old imx cfg file
620 * by adding VERSION command into it, here need
621 * set up function ptr group to V1 by default.
622 */
623 imximage_version = IMXIMAGE_V1;
Dirk Behme14a98cd2012-02-22 22:50:19 +0000624 /* Be able to detect if the cfg file has no BOOT_FROM tag */
Stefano Babice74e7792013-06-26 18:08:37 +0200625 imximage_ivt_offset = FLASH_OFFSET_UNDEFINED;
Stefano Babic4aa97492013-06-27 11:42:38 +0200626 imximage_csf_size = 0;
York Sund061d952013-09-20 12:24:44 -0700627 set_hdr_func();
Stefano Babic7b07f092010-01-20 18:19:10 +0100628
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000629 /* Parse dcd configuration file */
630 dcd_len = parse_cfg_file(imxhdr, params->imagename);
Stefano Babic7b07f092010-01-20 18:19:10 +0100631
Ye.Lif4063512014-08-20 16:55:32 +0800632 if (imximage_version == IMXIMAGE_V2) {
633 if (imximage_init_loadsize < imximage_ivt_offset +
634 sizeof(imx_header_v2_t))
635 imximage_init_loadsize = imximage_ivt_offset +
636 sizeof(imx_header_v2_t);
637 }
638
Liu Hui-R643434aa360a2011-01-19 09:40:26 +0000639 /* Set the imx header */
Stefano Babice74e7792013-06-26 18:08:37 +0200640 (*set_imx_hdr)(imxhdr, dcd_len, params->ep, imximage_ivt_offset);
Fabio Estevam5fd7c5d2013-01-03 08:24:33 +0000641
642 /*
643 * ROM bug alert
Marek Vasut3d1acc62013-04-21 05:52:22 +0000644 *
645 * MX53 only loads 512 byte multiples in case of SD boot.
646 * MX53 only loads NAND page multiples in case of NAND boot and
647 * supports up to 4096 byte large pages, thus align to 4096.
648 *
649 * The remaining fraction of a block bytes would not be loaded!
Fabio Estevam5fd7c5d2013-01-03 08:24:33 +0000650 */
Ye.Li49e036f2014-10-30 17:54:08 +0800651 *header_size_ptr = ROUND((sbuf->st_size + imximage_ivt_offset), 4096);
Stefano Babic4aa97492013-06-27 11:42:38 +0200652
653 if (csf_ptr && imximage_csf_size) {
654 *csf_ptr = params->ep - imximage_init_loadsize +
655 *header_size_ptr;
656 *header_size_ptr += imximage_csf_size;
657 }
Stefano Babic7b07f092010-01-20 18:19:10 +0100658}
659
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700660int imximage_check_params(struct image_tool_params *params)
Stefano Babic7b07f092010-01-20 18:19:10 +0100661{
662 if (!params)
663 return CFG_INVALID;
664 if (!strlen(params->imagename)) {
665 fprintf(stderr, "Error: %s - Configuration file not specified, "
666 "it is needed for imximage generation\n",
667 params->cmdname);
668 return CFG_INVALID;
669 }
670 /*
671 * Check parameters:
672 * XIP is not allowed and verify that incompatible
673 * parameters are not sent at the same time
674 * For example, if list is required a data image must not be provided
675 */
676 return (params->dflag && (params->fflag || params->lflag)) ||
677 (params->fflag && (params->dflag || params->lflag)) ||
678 (params->lflag && (params->dflag || params->fflag)) ||
679 (params->xflag) || !(strlen(params->imagename));
680}
681
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700682static int imximage_generate(struct image_tool_params *params,
Stefano Babic832eda82013-08-19 19:03:20 +0200683 struct image_type_params *tparams)
684{
685 struct imx_header *imxhdr;
686 size_t alloc_len;
687 struct stat sbuf;
688 char *datafile = params->datafile;
689 uint32_t pad_len;
690
691 memset(&imximage_header, 0, sizeof(imximage_header));
692
693 /*
694 * In order to not change the old imx cfg file
695 * by adding VERSION command into it, here need
696 * set up function ptr group to V1 by default.
697 */
698 imximage_version = IMXIMAGE_V1;
699 /* Be able to detect if the cfg file has no BOOT_FROM tag */
700 imximage_ivt_offset = FLASH_OFFSET_UNDEFINED;
701 imximage_csf_size = 0;
York Sund061d952013-09-20 12:24:44 -0700702 set_hdr_func();
Stefano Babic832eda82013-08-19 19:03:20 +0200703
704 /* Parse dcd configuration file */
705 parse_cfg_file(&imximage_header, params->imagename);
706
707 /* TODO: check i.MX image V1 handling, for now use 'old' style */
708 if (imximage_version == IMXIMAGE_V1) {
709 alloc_len = 4096;
710 } else {
711 if (imximage_init_loadsize < imximage_ivt_offset +
712 sizeof(imx_header_v2_t))
713 imximage_init_loadsize = imximage_ivt_offset +
714 sizeof(imx_header_v2_t);
715 alloc_len = imximage_init_loadsize - imximage_ivt_offset;
716 }
717
718 if (alloc_len < sizeof(struct imx_header)) {
719 fprintf(stderr, "%s: header error\n",
720 params->cmdname);
721 exit(EXIT_FAILURE);
722 }
723
724 imxhdr = malloc(alloc_len);
725
726 if (!imxhdr) {
727 fprintf(stderr, "%s: malloc return failure: %s\n",
728 params->cmdname, strerror(errno));
729 exit(EXIT_FAILURE);
730 }
731
732 memset(imxhdr, 0, alloc_len);
733
734 tparams->header_size = alloc_len;
735 tparams->hdr = imxhdr;
736
737 /* determine data image file length */
738
739 if (stat(datafile, &sbuf) < 0) {
740 fprintf(stderr, "%s: Can't stat %s: %s\n",
741 params->cmdname, datafile, strerror(errno));
742 exit(EXIT_FAILURE);
743 }
744
745 pad_len = ROUND(sbuf.st_size, 4096) - sbuf.st_size;
746
747 /* TODO: check i.MX image V1 handling, for now use 'old' style */
748 if (imximage_version == IMXIMAGE_V1)
749 return 0;
750 else
751 return pad_len;
752}
753
754
Stefano Babic7b07f092010-01-20 18:19:10 +0100755/*
756 * imximage parameters
757 */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200758U_BOOT_IMAGE_TYPE(
759 imximage,
760 "Freescale i.MX Boot Image support",
761 0,
762 NULL,
763 imximage_check_params,
764 imximage_verify_header,
765 imximage_print_header,
766 imximage_set_header,
767 NULL,
768 imximage_check_image_types,
769 NULL,
770 imximage_generate
771);