xref: /openbmc/u-boot/tools/imximage.c (revision 068abf0c)
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 	{CMD_IMAGE_VERSION,     "IMAGE_VERSION",        "image version",  },
42 	{-1,                    "",                     "",	          },
43 };
44 
45 /*
46  * Supported Boot options for configuration file
47  * this is needed to set the correct flash offset
48  */
49 static table_entry_t imximage_bootops[] = {
50 	{FLASH_OFFSET_ONENAND,	"onenand",	"OneNAND Flash",},
51 	{FLASH_OFFSET_NAND,	"nand",		"NAND Flash",	},
52 	{FLASH_OFFSET_NOR,	"nor",		"NOR Flash",	},
53 	{FLASH_OFFSET_SATA,	"sata",		"SATA Disk",	},
54 	{FLASH_OFFSET_SD,	"sd",		"SD Card",	},
55 	{FLASH_OFFSET_SPI,	"spi",		"SPI Flash",	},
56 	{-1,			"",		"Invalid",	},
57 };
58 
59 /*
60  * IMXIMAGE version definition for i.MX chips
61  */
62 static table_entry_t imximage_versions[] = {
63 	{IMXIMAGE_V1,	"",	" (i.MX25/35/51 compatible)", },
64 	{IMXIMAGE_V2,	"",	" (i.MX53/6 compatible)",     },
65 	{-1,            "",     " (Invalid)",                 },
66 };
67 
68 static struct imx_header imximage_header;
69 static uint32_t imximage_version;
70 
71 static set_dcd_val_t set_dcd_val;
72 static set_dcd_rst_t set_dcd_rst;
73 static set_imx_hdr_t set_imx_hdr;
74 
75 static uint32_t get_cfg_value(char *token, char *name,  int linenr)
76 {
77 	char *endptr;
78 	uint32_t value;
79 
80 	errno = 0;
81 	value = strtoul(token, &endptr, 16);
82 	if (errno || (token == endptr)) {
83 		fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
84 			name,  linenr, token);
85 		exit(EXIT_FAILURE);
86 	}
87 	return value;
88 }
89 
90 static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
91 {
92 	imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
93 	imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
94 	flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
95 	flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
96 
97 	/* Try to detect V1 */
98 	if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) &&
99 		(hdr_v1->dcd_table.preamble.barker == DCD_BARKER))
100 		return IMXIMAGE_V1;
101 
102 	/* Try to detect V2 */
103 	if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
104 		(hdr_v2->dcd_table.header.tag == DCD_HEADER_TAG))
105 		return IMXIMAGE_V2;
106 
107 	return IMXIMAGE_VER_INVALID;
108 }
109 
110 static void err_imximage_version(int version)
111 {
112 	fprintf(stderr,
113 		"Error: Unsupported imximage version:%d\n", version);
114 
115 	exit(EXIT_FAILURE);
116 }
117 
118 static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno,
119 					int fld, uint32_t value, uint32_t off)
120 {
121 	dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
122 
123 	switch (fld) {
124 	case CFG_REG_SIZE:
125 		/* Byte, halfword, word */
126 		if ((value != 1) && (value != 2) && (value != 4)) {
127 			fprintf(stderr, "Error: %s[%d] - "
128 				"Invalid register size " "(%d)\n",
129 				name, lineno, value);
130 			exit(EXIT_FAILURE);
131 		}
132 		dcd_v1->addr_data[off].type = value;
133 		break;
134 	case CFG_REG_ADDRESS:
135 		dcd_v1->addr_data[off].addr = value;
136 		break;
137 	case CFG_REG_VALUE:
138 		dcd_v1->addr_data[off].value = value;
139 		break;
140 	default:
141 		break;
142 
143 	}
144 }
145 
146 static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno,
147 					int fld, uint32_t value, uint32_t off)
148 {
149 	dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
150 
151 	switch (fld) {
152 	case CFG_REG_ADDRESS:
153 		dcd_v2->addr_data[off].addr = cpu_to_be32(value);
154 		break;
155 	case CFG_REG_VALUE:
156 		dcd_v2->addr_data[off].value = cpu_to_be32(value);
157 		break;
158 	default:
159 		break;
160 
161 	}
162 }
163 
164 /*
165  * Complete setting up the rest field of DCD of V1
166  * such as barker code and DCD data length.
167  */
168 static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len,
169 						char *name, int lineno)
170 {
171 	dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
172 
173 	if (dcd_len > MAX_HW_CFG_SIZE_V1) {
174 		fprintf(stderr, "Error: %s[%d] -"
175 			"DCD table exceeds maximum size(%d)\n",
176 			name, lineno, MAX_HW_CFG_SIZE_V1);
177 		exit(EXIT_FAILURE);
178 	}
179 
180 	dcd_v1->preamble.barker = DCD_BARKER;
181 	dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
182 }
183 
184 /*
185  * Complete setting up the reset field of DCD of V2
186  * such as DCD tag, version, length, etc.
187  */
188 static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len,
189 						char *name, int lineno)
190 {
191 	dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table;
192 
193 	if (dcd_len > MAX_HW_CFG_SIZE_V2) {
194 		fprintf(stderr, "Error: %s[%d] -"
195 			"DCD table exceeds maximum size(%d)\n",
196 			name, lineno, MAX_HW_CFG_SIZE_V2);
197 		exit(EXIT_FAILURE);
198 	}
199 
200 	dcd_v2->header.tag = DCD_HEADER_TAG;
201 	dcd_v2->header.length = cpu_to_be16(
202 			dcd_len * sizeof(dcd_addr_data_t) + 8);
203 	dcd_v2->header.version = DCD_VERSION;
204 	dcd_v2->write_dcd_command.tag = DCD_COMMAND_TAG;
205 	dcd_v2->write_dcd_command.length = cpu_to_be16(
206 			dcd_len * sizeof(dcd_addr_data_t) + 4);
207 	dcd_v2->write_dcd_command.param = DCD_COMMAND_PARAM;
208 }
209 
210 static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len,
211 					struct stat *sbuf,
212 					struct mkimage_params *params)
213 {
214 	imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1;
215 	flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
216 	dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
217 	uint32_t base_offset;
218 
219 	/* Set default offset */
220 	imxhdr->flash_offset = FLASH_OFFSET_STANDARD;
221 
222 	/* Set magic number */
223 	fhdr_v1->app_code_barker = APP_CODE_BARKER;
224 
225 	fhdr_v1->app_dest_ptr = params->addr;
226 	fhdr_v1->app_dest_ptr = params->ep - imxhdr->flash_offset -
227 		sizeof(struct imx_header);
228 	fhdr_v1->app_code_jump_vector = params->ep;
229 
230 	base_offset = fhdr_v1->app_dest_ptr + imxhdr->flash_offset ;
231 	fhdr_v1->dcd_ptr_ptr =
232 		(uint32_t) (offsetof(flash_header_v1_t, dcd_ptr) -
233 		offsetof(flash_header_v1_t, app_code_jump_vector) +
234 		base_offset);
235 
236 	fhdr_v1->dcd_ptr = base_offset +
237 			offsetof(imx_header_v1_t, dcd_table);
238 
239 	/* The external flash header must be at the end of the DCD table */
240 	dcd_v1->addr_data[dcd_len].type = sbuf->st_size +
241 				imxhdr->flash_offset +
242 				sizeof(struct imx_header);
243 
244 	/* Security feature are not supported */
245 	fhdr_v1->app_code_csf = 0;
246 	fhdr_v1->super_root_key = 0;
247 }
248 
249 static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
250 					struct stat *sbuf,
251 					struct mkimage_params *params)
252 {
253 	imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
254 	flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
255 
256 	/* Set default offset */
257 	imxhdr->flash_offset = FLASH_OFFSET_STANDARD;
258 
259 	/* Set magic number */
260 	fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
261 	fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
262 	fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
263 
264 	fhdr_v2->entry = params->ep;
265 	fhdr_v2->reserved1 = fhdr_v2->reserved2 = 0;
266 	fhdr_v2->self = params->ep - sizeof(struct imx_header);
267 
268 	fhdr_v2->dcd_ptr = fhdr_v2->self +
269 			offsetof(imx_header_v2_t, dcd_table);
270 
271 	fhdr_v2->boot_data_ptr = fhdr_v2->self +
272 			offsetof(imx_header_v2_t, boot_data);
273 
274 	hdr_v2->boot_data.start = fhdr_v2->self - imxhdr->flash_offset;
275 	hdr_v2->boot_data.size = sbuf->st_size +
276 			imxhdr->flash_offset +
277 			sizeof(struct imx_header);
278 
279 	/* Security feature are not supported */
280 	fhdr_v2->csf = 0;
281 }
282 
283 static void set_hdr_func(struct imx_header *imxhdr)
284 {
285 	switch (imximage_version) {
286 	case IMXIMAGE_V1:
287 		set_dcd_val = set_dcd_val_v1;
288 		set_dcd_rst = set_dcd_rst_v1;
289 		set_imx_hdr = set_imx_hdr_v1;
290 		break;
291 	case IMXIMAGE_V2:
292 		set_dcd_val = set_dcd_val_v2;
293 		set_dcd_rst = set_dcd_rst_v2;
294 		set_imx_hdr = set_imx_hdr_v2;
295 		break;
296 	default:
297 		err_imximage_version(imximage_version);
298 		break;
299 	}
300 }
301 
302 static void print_hdr_v1(struct imx_header *imx_hdr)
303 {
304 	imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
305 	flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
306 	dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
307 	uint32_t size, length, ver;
308 
309 	size = dcd_v1->preamble.length;
310 	if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
311 		fprintf(stderr,
312 			"Error: Image corrupt DCD size %d exceed maximum %d\n",
313 			(uint32_t)(size / sizeof(dcd_type_addr_data_t)),
314 			MAX_HW_CFG_SIZE_V1);
315 		exit(EXIT_FAILURE);
316 	}
317 
318 	length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
319 	ver = detect_imximage_version(imx_hdr);
320 
321 	printf("Image Type:   Freescale IMX Boot Image\n");
322 	printf("Image Ver:    %x", ver);
323 	printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
324 	printf("Data Size:    ");
325 	genimg_print_size(dcd_v1->addr_data[length].type);
326 	printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
327 	printf("Entry Point:  %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
328 }
329 
330 static void print_hdr_v2(struct imx_header *imx_hdr)
331 {
332 	imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
333 	flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
334 	dcd_v2_t *dcd_v2 = &hdr_v2->dcd_table;
335 	uint32_t size, version;
336 
337 	size = be16_to_cpu(dcd_v2->header.length) - 8;
338 	if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
339 		fprintf(stderr,
340 			"Error: Image corrupt DCD size %d exceed maximum %d\n",
341 			(uint32_t)(size / sizeof(dcd_addr_data_t)),
342 			MAX_HW_CFG_SIZE_V2);
343 		exit(EXIT_FAILURE);
344 	}
345 
346 	version = detect_imximage_version(imx_hdr);
347 
348 	printf("Image Type:   Freescale IMX Boot Image\n");
349 	printf("Image Ver:    %x", version);
350 	printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
351 	printf("Data Size:    ");
352 	genimg_print_size(hdr_v2->boot_data.size);
353 	printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
354 	printf("Entry Point:  %08x\n", (uint32_t)fhdr_v2->entry);
355 }
356 
357 static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
358 				char *name, int lineno, int fld, int dcd_len)
359 {
360 	int value;
361 	static int cmd_ver_first = ~0;
362 
363 	switch (cmd) {
364 	case CMD_IMAGE_VERSION:
365 		imximage_version = get_cfg_value(token, name, lineno);
366 		if (cmd_ver_first == 0) {
367 			fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
368 				"command need be the first before other "
369 				"valid command in the file\n", name, lineno);
370 			exit(EXIT_FAILURE);
371 		}
372 		cmd_ver_first = 1;
373 		set_hdr_func(imxhdr);
374 		break;
375 	case CMD_BOOT_FROM:
376 		imxhdr->flash_offset = get_table_entry_id(imximage_bootops,
377 					"imximage boot option", token);
378 		if (imxhdr->flash_offset == -1) {
379 			fprintf(stderr, "Error: %s[%d] -Invalid boot device"
380 				"(%s)\n", name, lineno, token);
381 			exit(EXIT_FAILURE);
382 		}
383 		if (unlikely(cmd_ver_first != 1))
384 			cmd_ver_first = 0;
385 		break;
386 	case CMD_DATA:
387 		value = get_cfg_value(token, name, lineno);
388 		(*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
389 		if (unlikely(cmd_ver_first != 1))
390 			cmd_ver_first = 0;
391 		break;
392 	}
393 }
394 
395 static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
396 		char *token, char *name, int lineno, int fld, int *dcd_len)
397 {
398 	int value;
399 
400 	switch (fld) {
401 	case CFG_COMMAND:
402 		*cmd = get_table_entry_id(imximage_cmds,
403 			"imximage commands", token);
404 		if (*cmd < 0) {
405 			fprintf(stderr, "Error: %s[%d] - Invalid command"
406 			"(%s)\n", name, lineno, token);
407 			exit(EXIT_FAILURE);
408 		}
409 		break;
410 	case CFG_REG_SIZE:
411 		parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
412 		break;
413 	case CFG_REG_ADDRESS:
414 	case CFG_REG_VALUE:
415 		if (*cmd != CMD_DATA)
416 			return;
417 
418 		value = get_cfg_value(token, name, lineno);
419 		(*set_dcd_val)(imxhdr, name, lineno, fld, value, *dcd_len);
420 
421 		if (fld == CFG_REG_VALUE)
422 			(*dcd_len)++;
423 		break;
424 	default:
425 		break;
426 	}
427 }
428 static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
429 {
430 	FILE *fd = NULL;
431 	char *line = NULL;
432 	char *token, *saveptr1, *saveptr2;
433 	int lineno = 0;
434 	int fld;
435 	size_t len;
436 	int dcd_len = 0;
437 	int32_t cmd;
438 
439 	fd = fopen(name, "r");
440 	if (fd == 0) {
441 		fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
442 		exit(EXIT_FAILURE);
443 	}
444 
445 	/* Very simple parsing, line starting with # are comments
446 	 * and are dropped
447 	 */
448 	while ((getline(&line, &len, fd)) > 0) {
449 		lineno++;
450 
451 		token = strtok_r(line, "\r\n", &saveptr1);
452 		if (token == NULL)
453 			continue;
454 
455 		/* Check inside the single line */
456 		for (fld = CFG_COMMAND, cmd = CMD_INVALID,
457 				line = token; ; line = NULL, fld++) {
458 			token = strtok_r(line, " \t", &saveptr2);
459 			if (token == NULL)
460 				break;
461 
462 			/* Drop all text starting with '#' as comments */
463 			if (token[0] == '#')
464 				break;
465 
466 			parse_cfg_fld(imxhdr, &cmd, token, name,
467 					lineno, fld, &dcd_len);
468 		}
469 
470 	}
471 
472 	(*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
473 	fclose(fd);
474 
475 	return dcd_len;
476 }
477 
478 
479 static int imximage_check_image_types(uint8_t type)
480 {
481 	if (type == IH_TYPE_IMXIMAGE)
482 		return EXIT_SUCCESS;
483 	else
484 		return EXIT_FAILURE;
485 }
486 
487 static int imximage_verify_header(unsigned char *ptr, int image_size,
488 			struct mkimage_params *params)
489 {
490 	struct imx_header *imx_hdr = (struct imx_header *) ptr;
491 
492 	if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
493 		return -FDT_ERR_BADSTRUCTURE;
494 
495 	return 0;
496 }
497 
498 static void imximage_print_header(const void *ptr)
499 {
500 	struct imx_header *imx_hdr = (struct imx_header *) ptr;
501 	uint32_t version = detect_imximage_version(imx_hdr);
502 
503 	switch (version) {
504 	case IMXIMAGE_V1:
505 		print_hdr_v1(imx_hdr);
506 		break;
507 	case IMXIMAGE_V2:
508 		print_hdr_v2(imx_hdr);
509 		break;
510 	default:
511 		err_imximage_version(version);
512 		break;
513 	}
514 }
515 
516 static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
517 				struct mkimage_params *params)
518 {
519 	struct imx_header *imxhdr = (struct imx_header *)ptr;
520 	uint32_t dcd_len;
521 
522 	/*
523 	 * In order to not change the old imx cfg file
524 	 * by adding VERSION command into it, here need
525 	 * set up function ptr group to V1 by default.
526 	 */
527 	imximage_version = IMXIMAGE_V1;
528 	set_hdr_func(imxhdr);
529 
530 	/* Parse dcd configuration file */
531 	dcd_len = parse_cfg_file(imxhdr, params->imagename);
532 
533 	/* Set the imx header */
534 	(*set_imx_hdr)(imxhdr, dcd_len, sbuf, params);
535 }
536 
537 int imximage_check_params(struct mkimage_params *params)
538 {
539 	if (!params)
540 		return CFG_INVALID;
541 	if (!strlen(params->imagename)) {
542 		fprintf(stderr, "Error: %s - Configuration file not specified, "
543 			"it is needed for imximage generation\n",
544 			params->cmdname);
545 		return CFG_INVALID;
546 	}
547 	/*
548 	 * Check parameters:
549 	 * XIP is not allowed and verify that incompatible
550 	 * parameters are not sent at the same time
551 	 * For example, if list is required a data image must not be provided
552 	 */
553 	return	(params->dflag && (params->fflag || params->lflag)) ||
554 		(params->fflag && (params->dflag || params->lflag)) ||
555 		(params->lflag && (params->dflag || params->fflag)) ||
556 		(params->xflag) || !(strlen(params->imagename));
557 }
558 
559 /*
560  * imximage parameters
561  */
562 static struct image_type_params imximage_params = {
563 	.name		= "Freescale i.MX 5x Boot Image support",
564 	.header_size	= sizeof(struct imx_header),
565 	.hdr		= (void *)&imximage_header,
566 	.check_image_type = imximage_check_image_types,
567 	.verify_header	= imximage_verify_header,
568 	.print_header	= imximage_print_header,
569 	.set_header	= imximage_set_header,
570 	.check_params	= imximage_check_params,
571 };
572 
573 void init_imx_image_type(void)
574 {
575 	mkimage_register(&imximage_params);
576 }
577