blob: 59923ffefa5f6affc58644d18c0866df25a3cc9d [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 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307 USA
26 */
27
Kumar Gala0bea74d2010-01-27 10:16:56 -060028/* Required to obtain the getline prototype from stdio.h */
29#define _GNU_SOURCE
30
Stefano Babic7b07f092010-01-20 18:19:10 +010031#include "mkimage.h"
32#include <image.h>
33#include "imximage.h"
34
35/*
36 * Supported commands for configuration file
37 */
38static table_entry_t imximage_cmds[] = {
39 {CMD_BOOT_FROM, "BOOT_FROM", "boot comand", },
40 {CMD_DATA, "DATA", "Reg Write Data", },
41 {-1, "", "", },
42};
43
44/*
45 * Supported Boot options for configuration file
46 * this is needed to set the correct flash offset
47 */
48static 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", },
54};
55
56
57static struct imx_header imximage_header;
58
59static uint32_t get_cfg_value(char *token, char *name, int linenr)
60{
61 char *endptr;
62 uint32_t value;
63
64 errno = 0;
65 value = strtoul(token, &endptr, 16);
66 if (errno || (token == endptr)) {
67 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
68 name, linenr, token);
69 exit(EXIT_FAILURE);
70 }
71 return value;
72}
73
74static int imximage_check_image_types(uint8_t type)
75{
76 if (type == IH_TYPE_IMXIMAGE)
77 return EXIT_SUCCESS;
78 else
79 return EXIT_FAILURE;
80}
81
82static int imximage_verify_header(unsigned char *ptr, int image_size,
83 struct mkimage_params *params)
84{
85
86 struct imx_header *imx_hdr = (struct imx_header *) ptr;
87 flash_header_t *hdr = &imx_hdr->fhdr;
88
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;
92
93 if (imx_hdr->dcd_table.preamble.barker != DCD_BARKER)
94 return -FDT_ERR_BADSTRUCTURE;
95
96 return 0;
97}
98
99static void imximage_print_header(const void *ptr)
100{
101 struct imx_header *imx_hdr = (struct imx_header *) ptr;
102 flash_header_t *hdr = &imx_hdr->fhdr;
103 uint32_t size;
104 flash_cfg_parms_t *ext_header;
105
106 size = imx_hdr->dcd_table.preamble.length;
107 if (size > (MAX_HW_CFG_SIZE * sizeof(dcd_type_addr_data_t))) {
108 fprintf(stderr,
109 "Error: Image corrupt DCD size %d exceed maximum %d\n",
110 size / sizeof(dcd_type_addr_data_t), MAX_HW_CFG_SIZE);
111 exit(EXIT_FAILURE);
112 }
113
114 ext_header = (flash_cfg_parms_t *) ((uint32_t)&imx_hdr->dcd_table +
115 sizeof(dcd_preamble_t) + size);
116
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);
122}
123
124static uint32_t imximage_parse_cfg_file(struct imx_header *imxhdr, char *name)
125{
126 FILE *fd = NULL;
127 char *line = NULL;
128 char *token, *saveptr1, *saveptr2;
129 int lineno = 0;
130 int fld, value;
131 uint32_t len;
132 int dcd_len = 0;
133 dcd_t *dcd = &imxhdr->dcd_table;
134 int32_t cmd;
135
136 fd = fopen(name, "r");
137 if (fd == 0) {
138 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
139 exit(EXIT_FAILURE);
140 }
141
142 /* Very simple parsing, line starting with # are comments
143 * and are dropped
144 */
145 while ((getline(&line, &len, fd)) > 0) {
146 lineno++;
147
148 token = strtok_r(line, "\r\n", &saveptr1);
149 if (token == NULL)
150 continue;
151
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);
156 if (token == NULL)
157 break;
158
159 /* Drop all text starting with '#' as comments */
160 if (token[0] == '#')
161 break;
162
163 /* parse all fields in a single line */
164 switch (fld) {
165 case CFG_COMMAND:
166 cmd = get_table_entry_id(imximage_cmds,
167 "imximage commands", token);
168 if (cmd < 0) {
169 fprintf(stderr,
170 "Error: %s[%d] - "
171 "Invalid command (%s)\n",
172 name, lineno, token);
173 exit(EXIT_FAILURE);
174 }
175 break;
176 case CFG_REG_SIZE:
177 switch (cmd) {
178 case CMD_BOOT_FROM:
179 /* Get flash header offset */
180 imxhdr->flash_offset =
181 get_table_entry_id(
182 imximage_bootops,
183 "imximage boot option",
184 token);
185 if (imxhdr->flash_offset == -1) {
186 fprintf(stderr,
187 "Error: %s[%d] -"
188 "Invalid boot device"
189 "(%s)\n",
190 name, lineno, token);
191 exit(EXIT_FAILURE);
192 }
193 break;
194 case CMD_DATA:
195 value = get_cfg_value(token,
196 name, lineno);
197
198 /* Byte, halfword, word */
199 if ((value != 1) &&
200 (value != 2) && (value != 4)) {
201 fprintf(stderr,
202 "Error: %s[%d] - "
203 "Invalid register size "
204 "(%d)\n",
205 name, lineno, value);
206 exit(EXIT_FAILURE);
207 }
208 dcd->addr_data[dcd_len].type = value;
209 break;
210 }
211
212 case CFG_REG_ADDRESS:
213 if (cmd == CMD_DATA)
214 dcd->addr_data[dcd_len].addr =
215 get_cfg_value(token,
216 name, lineno);
217 break;
218 case CFG_REG_VALUE:
219 if (cmd == CMD_DATA) {
220 dcd->addr_data[dcd_len].value =
221 get_cfg_value(token,
222 name, lineno);
223 dcd_len++;
224 }
225 break;
226 }
227 }
228
229 if (dcd_len > MAX_HW_CFG_SIZE) {
230 fprintf(stderr,
231 "Error: %s[%d] -"
232 "DCD table exceeds maximum size(%d)\n",
233 name, lineno, MAX_HW_CFG_SIZE);
234 }
235 }
236 dcd->preamble.barker = DCD_BARKER;
237 dcd->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
238 fclose(fd);
239
240 return dcd->preamble.length;
241}
242
243static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
244 struct mkimage_params *params)
245{
246 struct imx_header *hdr = (struct imx_header *)ptr;
247 flash_header_t *fhdr = &hdr->fhdr;
248 int dcd_len;
249 flash_cfg_parms_t *ext_header;
250 uint32_t base_offset;
251
252 /* Set default offset */
253 hdr->flash_offset = FLASH_OFFSET_STANDARD;
254
255 /* Set magic number */
256 fhdr->app_code_barker = APP_CODE_BARKER;
257
258 /* Parse dcd configuration file */
259 dcd_len = imximage_parse_cfg_file(hdr, params->imagename);
260
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;
265
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 ;
269
270 fhdr->dcd_ptr = base_offset +
271 ((uint32_t)&hdr->dcd_table -
272 (uint32_t)&hdr->fhdr);
273
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 +
276 dcd_len +
277 sizeof(dcd_preamble_t));
278 ext_header->length = sbuf->st_size +
279 hdr->flash_offset +
280 sizeof(struct imx_header);
281
282 /* Security feature are not supported */
283 fhdr->app_code_csf = 0;
284 fhdr->super_root_key = NULL;
285
286}
287
288int imximage_check_params(struct mkimage_params *params)
289{
290 if (!params)
291 return CFG_INVALID;
292 if (!strlen(params->imagename)) {
293 fprintf(stderr, "Error: %s - Configuration file not specified, "
294 "it is needed for imximage generation\n",
295 params->cmdname);
296 return CFG_INVALID;
297 }
298 /*
299 * Check parameters:
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
303 */
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));
308}
309
310/*
311 * imximage parameters
312 */
313static 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,
322};
323
324void init_imx_image_type(void)
325{
326 mkimage_register(&imximage_params);
327}