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,
33 * Supported commands for configuration file
35 static table_entry_t imximage_cmds
[] = {
36 {CMD_BOOT_FROM
, "BOOT_FROM", "boot comand", },
37 {CMD_DATA
, "DATA", "Reg Write Data", },
42 * Supported Boot options for configuration file
43 * this is needed to set the correct flash offset
45 static table_entry_t imximage_bootops
[] = {
46 {FLASH_OFFSET_SPI
, "spi", "SPI Flash", },
47 {FLASH_OFFSET_NAND
, "nand", "NAND Flash", },
48 {FLASH_OFFSET_SD
, "sd", "SD Card", },
49 {FLASH_OFFSET_ONENAND
, "onenand", "OneNAND Flash",},
50 {-1, "", "Invalid", },
54 static struct imx_header imximage_header
;
56 static uint32_t get_cfg_value(char *token
, char *name
, int linenr
)
62 value
= strtoul(token
, &endptr
, 16);
63 if (errno
|| (token
== endptr
)) {
64 fprintf(stderr
, "Error: %s[%d] - Invalid hex data(%s)\n",
71 static int imximage_check_image_types(uint8_t type
)
73 if (type
== IH_TYPE_IMXIMAGE
)
79 static int imximage_verify_header(unsigned char *ptr
, int image_size
,
80 struct mkimage_params
*params
)
83 struct imx_header
*imx_hdr
= (struct imx_header
*) ptr
;
84 flash_header_t
*hdr
= &imx_hdr
->fhdr
;
86 /* Only a few checks can be done: search for magic numbers */
87 if (hdr
->app_code_barker
!= APP_CODE_BARKER
)
88 return -FDT_ERR_BADSTRUCTURE
;
90 if (imx_hdr
->dcd_table
.preamble
.barker
!= DCD_BARKER
)
91 return -FDT_ERR_BADSTRUCTURE
;
96 static void imximage_print_header(const void *ptr
)
98 struct imx_header
*imx_hdr
= (struct imx_header
*) ptr
;
99 flash_header_t
*hdr
= &imx_hdr
->fhdr
;
101 flash_cfg_parms_t
*ext_header
;
103 size
= imx_hdr
->dcd_table
.preamble
.length
;
104 if (size
> (MAX_HW_CFG_SIZE
* sizeof(dcd_type_addr_data_t
))) {
106 "Error: Image corrupt DCD size %d exceed maximum %d\n",
107 size
/ sizeof(dcd_type_addr_data_t
), MAX_HW_CFG_SIZE
);
111 ext_header
= (flash_cfg_parms_t
*) ((uint32_t)&imx_hdr
->dcd_table
+
112 sizeof(dcd_preamble_t
) + size
);
114 printf("Image Type: Freescale IMX Boot Image\n");
115 printf("Data Size: ");
116 genimg_print_size(ext_header
->length
);
117 printf("Load Address: %08x\n", (unsigned int)hdr
->app_dest_ptr
);
118 printf("Entry Point: %08x\n", (unsigned int)hdr
->app_code_jump_vector
);
121 static uint32_t imximage_parse_cfg_file(struct imx_header
*imxhdr
, char *name
)
125 char *token
, *saveptr1
, *saveptr2
;
130 dcd_t
*dcd
= &imxhdr
->dcd_table
;
133 fd
= fopen(name
, "r");
135 fprintf(stderr
, "Error: %s - Can't open DCD file\n", name
);
139 /* Very simple parsing, line starting with # are comments
142 while ((getline(&line
, &len
, fd
)) > 0) {
145 token
= strtok_r(line
, "\r\n", &saveptr1
);
149 /* Check inside the single line */
150 for (fld
= CFG_COMMAND
, cmd
= CMD_INVALID
,
151 line
= token
; ; line
= NULL
, fld
++) {
152 token
= strtok_r(line
, " \t", &saveptr2
);
156 /* Drop all text starting with '#' as comments */
160 /* parse all fields in a single line */
163 cmd
= get_table_entry_id(imximage_cmds
,
164 "imximage commands", token
);
168 "Invalid command (%s)\n",
169 name
, lineno
, token
);
176 /* Get flash header offset */
177 imxhdr
->flash_offset
=
180 "imximage boot option",
182 if (imxhdr
->flash_offset
== -1) {
185 "Invalid boot device"
187 name
, lineno
, token
);
192 value
= get_cfg_value(token
,
195 /* Byte, halfword, word */
197 (value
!= 2) && (value
!= 4)) {
200 "Invalid register size "
202 name
, lineno
, value
);
205 dcd
->addr_data
[dcd_len
].type
= value
;
209 case CFG_REG_ADDRESS
:
211 dcd
->addr_data
[dcd_len
].addr
=
216 if (cmd
== CMD_DATA
) {
217 dcd
->addr_data
[dcd_len
].value
=
226 if (dcd_len
> MAX_HW_CFG_SIZE
) {
229 "DCD table exceeds maximum size(%d)\n",
230 name
, lineno
, MAX_HW_CFG_SIZE
);
233 dcd
->preamble
.barker
= DCD_BARKER
;
234 dcd
->preamble
.length
= dcd_len
* sizeof(dcd_type_addr_data_t
);
237 return dcd
->preamble
.length
;
240 static void imximage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
241 struct mkimage_params
*params
)
243 struct imx_header
*hdr
= (struct imx_header
*)ptr
;
244 flash_header_t
*fhdr
= &hdr
->fhdr
;
246 flash_cfg_parms_t
*ext_header
;
247 uint32_t base_offset
;
249 /* Set default offset */
250 hdr
->flash_offset
= FLASH_OFFSET_STANDARD
;
252 /* Set magic number */
253 fhdr
->app_code_barker
= APP_CODE_BARKER
;
255 /* Parse dcd configuration file */
256 dcd_len
= imximage_parse_cfg_file(hdr
, params
->imagename
);
258 fhdr
->app_dest_ptr
= params
->addr
;
259 fhdr
->app_dest_ptr
= params
->ep
- hdr
->flash_offset
-
260 sizeof(struct imx_header
);
261 fhdr
->app_code_jump_vector
= params
->ep
;
263 base_offset
= fhdr
->app_dest_ptr
+ hdr
->flash_offset
;
264 fhdr
->dcd_ptr_ptr
= (uint32_t) ((uint32_t)&fhdr
->dcd_ptr
-
265 (uint32_t)&fhdr
->app_code_jump_vector
) + base_offset
;
267 fhdr
->dcd_ptr
= base_offset
+
268 ((uint32_t)&hdr
->dcd_table
-
269 (uint32_t)&hdr
->fhdr
);
271 /* The external flash header must be at the end of the DCD table */
272 ext_header
= (flash_cfg_parms_t
*) ((uint32_t)&hdr
->dcd_table
+
274 sizeof(dcd_preamble_t
));
275 ext_header
->length
= sbuf
->st_size
+
277 sizeof(struct imx_header
);
279 /* Security feature are not supported */
280 fhdr
->app_code_csf
= 0;
281 fhdr
->super_root_key
= NULL
;
285 int imximage_check_params(struct mkimage_params
*params
)
289 if (!strlen(params
->imagename
)) {
290 fprintf(stderr
, "Error: %s - Configuration file not specified, "
291 "it is needed for imximage generation\n",
297 * XIP is not allowed and verify that incompatible
298 * parameters are not sent at the same time
299 * For example, if list is required a data image must not be provided
301 return (params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
302 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
303 (params
->lflag
&& (params
->dflag
|| params
->fflag
)) ||
304 (params
->xflag
) || !(strlen(params
->imagename
));
308 * imximage parameters
310 static struct image_type_params imximage_params
= {
311 .name
= "Freescale i.MX 51 Boot Image support",
312 .header_size
= sizeof(struct imx_header
),
313 .hdr
= (void *)&imximage_header
,
314 .check_image_type
= imximage_check_image_types
,
315 .verify_header
= imximage_verify_header
,
316 .print_header
= imximage_print_header
,
317 .set_header
= imximage_set_header
,
318 .check_params
= imximage_check_params
,
321 void init_imx_image_type(void)
323 mkimage_register(&imximage_params
);