3 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
6 * Marvell Semiconductor <www.marvell.com>
7 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 /* Required to obtain the getline prototype from stdio.h */
36 * Supported commands for configuration file
38 static table_entry_t imximage_cmds[] = {
39 {CMD_BOOT_FROM, "BOOT_FROM", "boot comand", },
40 {CMD_DATA, "DATA", "Reg Write Data", },
45 * Supported Boot options for configuration file
46 * this is needed to set the correct flash offset
48 static table_entry_t imximage_bootops[] = {
49 {FLASH_OFFSET_SPI, "spi", "SPI Flash", },
50 {FLASH_OFFSET_NAND, "nand", "NAND Flash", },
51 {FLASH_OFFSET_SD, "sd", "SD Card", },
52 {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",},
53 {-1, "", "Invalid", },
57 static struct imx_header imximage_header;
59 static uint32_t get_cfg_value(char *token, char *name, int linenr)
65 value = strtoul(token, &endptr, 16);
66 if (errno || (token == endptr)) {
67 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
74 static int imximage_check_image_types(uint8_t type)
76 if (type == IH_TYPE_IMXIMAGE)
82 static int imximage_verify_header(unsigned char *ptr, int image_size,
83 struct mkimage_params *params)
86 struct imx_header *imx_hdr = (struct imx_header *) ptr;
87 flash_header_t *hdr = &imx_hdr->fhdr;
89 /* Only a few checks can be done: search for magic numbers */
90 if (hdr->app_code_barker != APP_CODE_BARKER)
91 return -FDT_ERR_BADSTRUCTURE;
93 if (imx_hdr->dcd_table.preamble.barker != DCD_BARKER)
94 return -FDT_ERR_BADSTRUCTURE;
99 static void imximage_print_header(const void *ptr)
101 struct imx_header *imx_hdr = (struct imx_header *) ptr;
102 flash_header_t *hdr = &imx_hdr->fhdr;
104 flash_cfg_parms_t *ext_header;
106 size = imx_hdr->dcd_table.preamble.length;
107 if (size > (MAX_HW_CFG_SIZE * sizeof(dcd_type_addr_data_t))) {
109 "Error: Image corrupt DCD size %d exceed maximum %d\n",
110 size / sizeof(dcd_type_addr_data_t), MAX_HW_CFG_SIZE);
114 ext_header = (flash_cfg_parms_t *) ((uint32_t)&imx_hdr->dcd_table +
115 sizeof(dcd_preamble_t) + size);
117 printf("Image Type: Freescale IMX Boot Image\n");
118 printf("Data Size: ");
119 genimg_print_size(ext_header->length);
120 printf("Load Address: %08x\n", (unsigned int)hdr->app_dest_ptr);
121 printf("Entry Point: %08x\n", (unsigned int)hdr->app_code_jump_vector);
124 static uint32_t imximage_parse_cfg_file(struct imx_header *imxhdr, char *name)
128 char *token, *saveptr1, *saveptr2;
133 dcd_t *dcd = &imxhdr->dcd_table;
136 fd = fopen(name, "r");
138 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
142 /* Very simple parsing, line starting with # are comments
145 while ((getline(&line, &len, fd)) > 0) {
148 token = strtok_r(line, "\r\n", &saveptr1);
152 /* Check inside the single line */
153 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
154 line = token; ; line = NULL, fld++) {
155 token = strtok_r(line, " \t", &saveptr2);
159 /* Drop all text starting with '#' as comments */
163 /* parse all fields in a single line */
166 cmd = get_table_entry_id(imximage_cmds,
167 "imximage commands", token);
171 "Invalid command (%s)\n",
172 name, lineno, token);
179 /* Get flash header offset */
180 imxhdr->flash_offset =
183 "imximage boot option",
185 if (imxhdr->flash_offset == -1) {
188 "Invalid boot device"
190 name, lineno, token);
195 value = get_cfg_value(token,
198 /* Byte, halfword, word */
200 (value != 2) && (value != 4)) {
203 "Invalid register size "
205 name, lineno, value);
208 dcd->addr_data[dcd_len].type = value;
212 case CFG_REG_ADDRESS:
214 dcd->addr_data[dcd_len].addr =
219 if (cmd == CMD_DATA) {
220 dcd->addr_data[dcd_len].value =
229 if (dcd_len > MAX_HW_CFG_SIZE) {
232 "DCD table exceeds maximum size(%d)\n",
233 name, lineno, MAX_HW_CFG_SIZE);
236 dcd->preamble.barker = DCD_BARKER;
237 dcd->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
240 return dcd->preamble.length;
243 static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
244 struct mkimage_params *params)
246 struct imx_header *hdr = (struct imx_header *)ptr;
247 flash_header_t *fhdr = &hdr->fhdr;
249 flash_cfg_parms_t *ext_header;
250 uint32_t base_offset;
252 /* Set default offset */
253 hdr->flash_offset = FLASH_OFFSET_STANDARD;
255 /* Set magic number */
256 fhdr->app_code_barker = APP_CODE_BARKER;
258 /* Parse dcd configuration file */
259 dcd_len = imximage_parse_cfg_file(hdr, params->imagename);
261 fhdr->app_dest_ptr = params->addr;
262 fhdr->app_dest_ptr = params->ep - hdr->flash_offset -
263 sizeof(struct imx_header);
264 fhdr->app_code_jump_vector = params->ep;
266 base_offset = fhdr->app_dest_ptr + hdr->flash_offset ;
267 fhdr->dcd_ptr_ptr = (uint32_t) ((uint32_t)&fhdr->dcd_ptr -
268 (uint32_t)&fhdr->app_code_jump_vector) + base_offset ;
270 fhdr->dcd_ptr = base_offset +
271 ((uint32_t)&hdr->dcd_table -
272 (uint32_t)&hdr->fhdr);
274 /* The external flash header must be at the end of the DCD table */
275 ext_header = (flash_cfg_parms_t *) ((uint32_t)&hdr->dcd_table +
277 sizeof(dcd_preamble_t));
278 ext_header->length = sbuf->st_size +
280 sizeof(struct imx_header);
282 /* Security feature are not supported */
283 fhdr->app_code_csf = 0;
284 fhdr->super_root_key = NULL;
288 int imximage_check_params(struct mkimage_params *params)
292 if (!strlen(params->imagename)) {
293 fprintf(stderr, "Error: %s - Configuration file not specified, "
294 "it is needed for imximage generation\n",
300 * XIP is not allowed and verify that incompatible
301 * parameters are not sent at the same time
302 * For example, if list is required a data image must not be provided
304 return (params->dflag && (params->fflag || params->lflag)) ||
305 (params->fflag && (params->dflag || params->lflag)) ||
306 (params->lflag && (params->dflag || params->fflag)) ||
307 (params->xflag) || !(strlen(params->imagename));
311 * imximage parameters
313 static struct image_type_params imximage_params = {
314 .name = "Freescale i.MX 51 Boot Image support",
315 .header_size = sizeof(struct imx_header),
316 .hdr = (void *)&imximage_header,
317 .check_image_type = imximage_check_image_types,
318 .verify_header = imximage_verify_header,
319 .print_header = imximage_print_header,
320 .set_header = imximage_set_header,
321 .check_params = imximage_check_params,
324 void init_imx_image_type(void)
326 mkimage_register(&imximage_params);