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 command", },
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
;
105 dcd_t
*dcd
= &imx_hdr
->dcd_table
;
107 size
= imx_hdr
->dcd_table
.preamble
.length
;
108 if (size
> (MAX_HW_CFG_SIZE
* sizeof(dcd_type_addr_data_t
))) {
110 "Error: Image corrupt DCD size %d exceed maximum %d\n",
111 (uint32_t)(size
/ sizeof(dcd_type_addr_data_t
)),
116 length
= dcd
->preamble
.length
/ sizeof(dcd_type_addr_data_t
);
118 printf("Image Type: Freescale IMX Boot Image\n");
119 printf("Data Size: ");
120 genimg_print_size(dcd
->addr_data
[length
].type
);
121 printf("Load Address: %08x\n", (unsigned int)hdr
->app_dest_ptr
);
122 printf("Entry Point: %08x\n", (unsigned int)hdr
->app_code_jump_vector
);
125 static uint32_t imximage_parse_cfg_file(struct imx_header
*imxhdr
, char *name
)
129 char *token
, *saveptr1
, *saveptr2
;
134 dcd_t
*dcd
= &imxhdr
->dcd_table
;
137 fd
= fopen(name
, "r");
139 fprintf(stderr
, "Error: %s - Can't open DCD file\n", name
);
143 /* Very simple parsing, line starting with # are comments
146 while ((getline(&line
, &len
, fd
)) > 0) {
149 token
= strtok_r(line
, "\r\n", &saveptr1
);
153 /* Check inside the single line */
154 for (fld
= CFG_COMMAND
, cmd
= CMD_INVALID
,
155 line
= token
; ; line
= NULL
, fld
++) {
156 token
= strtok_r(line
, " \t", &saveptr2
);
160 /* Drop all text starting with '#' as comments */
164 /* parse all fields in a single line */
167 cmd
= get_table_entry_id(imximage_cmds
,
168 "imximage commands", token
);
172 "Invalid command (%s)\n",
173 name
, lineno
, token
);
180 /* Get flash header offset */
181 imxhdr
->flash_offset
=
184 "imximage boot option",
186 if (imxhdr
->flash_offset
== -1) {
189 "Invalid boot device"
191 name
, lineno
, token
);
196 value
= get_cfg_value(token
,
199 /* Byte, halfword, word */
201 (value
!= 2) && (value
!= 4)) {
204 "Invalid register size "
206 name
, lineno
, value
);
209 dcd
->addr_data
[dcd_len
].type
= value
;
213 case CFG_REG_ADDRESS
:
215 dcd
->addr_data
[dcd_len
].addr
=
220 if (cmd
== CMD_DATA
) {
221 dcd
->addr_data
[dcd_len
].value
=
230 if (dcd_len
> MAX_HW_CFG_SIZE
) {
233 "DCD table exceeds maximum size(%d)\n",
234 name
, lineno
, MAX_HW_CFG_SIZE
);
237 dcd
->preamble
.barker
= DCD_BARKER
;
238 dcd
->preamble
.length
= dcd_len
* sizeof(dcd_type_addr_data_t
);
244 static void imximage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
245 struct mkimage_params
*params
)
247 struct imx_header
*hdr
= (struct imx_header
*)ptr
;
248 flash_header_t
*fhdr
= &hdr
->fhdr
;
250 dcd_t
*dcd
= &hdr
->dcd_table
;
251 uint32_t base_offset
;
253 /* Set default offset */
254 hdr
->flash_offset
= FLASH_OFFSET_STANDARD
;
256 /* Set magic number */
257 fhdr
->app_code_barker
= APP_CODE_BARKER
;
259 /* Parse dcd configuration file */
260 dcd_len
= imximage_parse_cfg_file(hdr
, params
->imagename
);
262 fhdr
->app_dest_ptr
= params
->addr
;
263 fhdr
->app_dest_ptr
= params
->ep
- hdr
->flash_offset
-
264 sizeof(struct imx_header
);
265 fhdr
->app_code_jump_vector
= params
->ep
;
267 base_offset
= fhdr
->app_dest_ptr
+ hdr
->flash_offset
;
268 fhdr
->dcd_ptr_ptr
= (uint32_t) (offsetof(flash_header_t
, dcd_ptr
) -
269 offsetof(flash_header_t
, app_code_jump_vector
) +
272 fhdr
->dcd_ptr
= base_offset
+
273 offsetof(struct imx_header
, dcd_table
);
275 /* The external flash header must be at the end of the DCD table */
276 dcd
->addr_data
[dcd_len
].type
= sbuf
->st_size
+
278 sizeof(struct imx_header
);
280 /* Security feature are not supported */
281 fhdr
->app_code_csf
= 0;
282 fhdr
->super_root_key
= 0;
286 int imximage_check_params(struct mkimage_params
*params
)
290 if (!strlen(params
->imagename
)) {
291 fprintf(stderr
, "Error: %s - Configuration file not specified, "
292 "it is needed for imximage generation\n",
298 * XIP is not allowed and verify that incompatible
299 * parameters are not sent at the same time
300 * For example, if list is required a data image must not be provided
302 return (params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
303 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
304 (params
->lflag
&& (params
->dflag
|| params
->fflag
)) ||
305 (params
->xflag
) || !(strlen(params
->imagename
));
309 * imximage parameters
311 static struct image_type_params imximage_params
= {
312 .name
= "Freescale i.MX 51 Boot Image support",
313 .header_size
= sizeof(struct imx_header
),
314 .hdr
= (void *)&imximage_header
,
315 .check_image_type
= imximage_check_image_types
,
316 .verify_header
= imximage_verify_header
,
317 .print_header
= imximage_print_header
,
318 .set_header
= imximage_set_header
,
319 .check_params
= imximage_check_params
,
322 void init_imx_image_type(void)
324 mkimage_register(&imximage_params
);