xref: /openbmc/u-boot/tools/imximage.c (revision 54841ab5)
1 /*
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 
28 /* Required to obtain the getline prototype from stdio.h */
29 #define _GNU_SOURCE
30 
31 #include "mkimage.h"
32 #include <image.h>
33 #include "imximage.h"
34 
35 /*
36  * Supported commands for configuration file
37  */
38 static table_entry_t imximage_cmds[] = {
39 	{CMD_BOOT_FROM,		"BOOT_FROM",		"boot command",	},
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  */
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",	},
54 };
55 
56 
57 static struct imx_header imximage_header;
58 
59 static 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 
74 static 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 
82 static 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 
99 static 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 	uint32_t length;
105 	dcd_t *dcd = &imx_hdr->dcd_table;
106 
107 	size = imx_hdr->dcd_table.preamble.length;
108 	if (size > (MAX_HW_CFG_SIZE * sizeof(dcd_type_addr_data_t))) {
109 		fprintf(stderr,
110 			"Error: Image corrupt DCD size %d exceed maximum %d\n",
111 			(uint32_t)(size / sizeof(dcd_type_addr_data_t)),
112 			MAX_HW_CFG_SIZE);
113 		exit(EXIT_FAILURE);
114 	}
115 
116 	length =  dcd->preamble.length / sizeof(dcd_type_addr_data_t);
117 
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);
123 }
124 
125 static uint32_t imximage_parse_cfg_file(struct imx_header *imxhdr, char *name)
126 {
127 	FILE *fd = NULL;
128 	char *line = NULL;
129 	char *token, *saveptr1, *saveptr2;
130 	int lineno = 0;
131 	int fld, value;
132 	size_t len;
133 	int dcd_len = 0;
134 	dcd_t *dcd = &imxhdr->dcd_table;
135 	int32_t cmd;
136 
137 	fd = fopen(name, "r");
138 	if (fd == 0) {
139 		fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
140 		exit(EXIT_FAILURE);
141 	}
142 
143 	/* Very simple parsing, line starting with # are comments
144 	 * and are dropped
145 	 */
146 	while ((getline(&line, &len, fd)) > 0) {
147 		lineno++;
148 
149 		token = strtok_r(line, "\r\n", &saveptr1);
150 		if (token == NULL)
151 			continue;
152 
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);
157 			if (token == NULL)
158 				break;
159 
160 			/* Drop all text starting with '#' as comments */
161 			if (token[0] == '#')
162 				break;
163 
164 			/* parse all fields in a single line */
165 			switch (fld) {
166 			case CFG_COMMAND:
167 				cmd = get_table_entry_id(imximage_cmds,
168 					"imximage commands", token);
169 				if (cmd < 0) {
170 					fprintf(stderr,
171 						"Error: %s[%d] - "
172 						"Invalid command (%s)\n",
173 						name, lineno, token);
174 					exit(EXIT_FAILURE);
175 				}
176 				break;
177 			case CFG_REG_SIZE:
178 				switch (cmd) {
179 				case CMD_BOOT_FROM:
180 					/* Get flash header offset */
181 					imxhdr->flash_offset =
182 						get_table_entry_id(
183 							imximage_bootops,
184 							"imximage boot option",
185 							token);
186 					if (imxhdr->flash_offset == -1) {
187 						fprintf(stderr,
188 							"Error: %s[%d] -"
189 							"Invalid boot device"
190 							"(%s)\n",
191 							name, lineno, token);
192 						exit(EXIT_FAILURE);
193 					}
194 					break;
195 				case CMD_DATA:
196 					value = get_cfg_value(token,
197 							name, lineno);
198 
199 					/* Byte, halfword, word */
200 					if ((value != 1) &&
201 						(value != 2) && (value != 4)) {
202 						fprintf(stderr,
203 							"Error: %s[%d] - "
204 							"Invalid register size "
205 							"(%d)\n",
206 							name, lineno, value);
207 						exit(EXIT_FAILURE);
208 					}
209 					dcd->addr_data[dcd_len].type = value;
210 					break;
211 				}
212 
213 			case CFG_REG_ADDRESS:
214 				if (cmd == CMD_DATA)
215 					dcd->addr_data[dcd_len].addr =
216 						get_cfg_value(token,
217 							name, lineno);
218 				break;
219 			case CFG_REG_VALUE:
220 				if (cmd == CMD_DATA) {
221 					dcd->addr_data[dcd_len].value =
222 						get_cfg_value(token,
223 							name, lineno);
224 					dcd_len++;
225 				}
226 				break;
227 			}
228 		}
229 
230 		if (dcd_len > MAX_HW_CFG_SIZE) {
231 			fprintf(stderr,
232 				"Error: %s[%d] -"
233 				"DCD table exceeds maximum size(%d)\n",
234 				name, lineno, MAX_HW_CFG_SIZE);
235 		}
236 	}
237 	dcd->preamble.barker = DCD_BARKER;
238 	dcd->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
239 	fclose(fd);
240 
241 	return dcd_len;
242 }
243 
244 static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
245 				struct mkimage_params *params)
246 {
247 	struct imx_header *hdr = (struct imx_header *)ptr;
248 	flash_header_t *fhdr = &hdr->fhdr;
249 	int dcd_len;
250 	dcd_t *dcd = &hdr->dcd_table;
251 	uint32_t base_offset;
252 
253 	/* Set default offset */
254 	hdr->flash_offset = FLASH_OFFSET_STANDARD;
255 
256 	/* Set magic number */
257 	fhdr->app_code_barker = APP_CODE_BARKER;
258 
259 	/* Parse dcd configuration file */
260 	dcd_len = imximage_parse_cfg_file(hdr, params->imagename);
261 
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;
266 
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) +
270 		base_offset);
271 
272 	fhdr->dcd_ptr = base_offset +
273 			offsetof(struct imx_header, dcd_table);
274 
275 	/* The external flash header must be at the end of the DCD table */
276 	dcd->addr_data[dcd_len].type = sbuf->st_size +
277 				hdr->flash_offset +
278 				sizeof(struct imx_header);
279 
280 	/* Security feature are not supported */
281 	fhdr->app_code_csf = 0;
282 	fhdr->super_root_key = 0;
283 
284 }
285 
286 int imximage_check_params(struct mkimage_params *params)
287 {
288 	if (!params)
289 		return CFG_INVALID;
290 	if (!strlen(params->imagename)) {
291 		fprintf(stderr, "Error: %s - Configuration file not specified, "
292 			"it is needed for imximage generation\n",
293 			params->cmdname);
294 		return CFG_INVALID;
295 	}
296 	/*
297 	 * Check parameters:
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
301 	 */
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));
306 }
307 
308 /*
309  * imximage parameters
310  */
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,
320 };
321 
322 void init_imx_image_type(void)
323 {
324 	mkimage_register(&imximage_params);
325 }
326