xref: /openbmc/u-boot/tools/mkimage.c (revision 1a99de2cb4d08eb3bf9fb3f60a9d533150de8c0e)
15b1d7137Swdenk /*
29d25438fSBartlomiej Sieka  * (C) Copyright 2008 Semihalf
39d25438fSBartlomiej Sieka  *
489a4d6b1SPrafulla Wadaskar  * (C) Copyright 2000-2009
55b1d7137Swdenk  * DENX Software Engineering
65b1d7137Swdenk  * Wolfgang Denk, wd@denx.de
70c852a28Swdenk  *
80c852a28Swdenk  * This program is free software; you can redistribute it and/or
90c852a28Swdenk  * modify it under the terms of the GNU General Public License as
100c852a28Swdenk  * published by the Free Software Foundation; either version 2 of
110c852a28Swdenk  * the License, or (at your option) any later version.
120c852a28Swdenk  *
130c852a28Swdenk  * This program is distributed in the hope that it will be useful,
140c852a28Swdenk  * but WITHOUT ANY WARRANTY; without even the implied warranty of
150c852a28Swdenk  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
160c852a28Swdenk  * GNU General Public License for more details.
170c852a28Swdenk  *
180c852a28Swdenk  * You should have received a copy of the GNU General Public License
190c852a28Swdenk  * along with this program; if not, write to the Free Software
200c852a28Swdenk  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
210c852a28Swdenk  * MA 02111-1307 USA
225b1d7137Swdenk  */
235b1d7137Swdenk 
24b97a2a0aSMarian Balakowicz #include "mkimage.h"
255b1d7137Swdenk #include <image.h>
265b1d7137Swdenk 
275b1d7137Swdenk static void copy_file(int, const char *, int);
285b1d7137Swdenk static void usage(void);
295b1d7137Swdenk 
3089a4d6b1SPrafulla Wadaskar /* image_type_params link list to maintain registered image type supports */
3189a4d6b1SPrafulla Wadaskar struct image_type_params *mkimage_tparams = NULL;
325b1d7137Swdenk 
3389a4d6b1SPrafulla Wadaskar /* parameters initialized by core will be used by the image type code */
3489a4d6b1SPrafulla Wadaskar struct mkimage_params params = {
3589a4d6b1SPrafulla Wadaskar 	.os = IH_OS_LINUX,
3689a4d6b1SPrafulla Wadaskar 	.arch = IH_ARCH_PPC,
3789a4d6b1SPrafulla Wadaskar 	.type = IH_TYPE_KERNEL,
3889a4d6b1SPrafulla Wadaskar 	.comp = IH_COMP_GZIP,
3989a4d6b1SPrafulla Wadaskar 	.dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
4089a4d6b1SPrafulla Wadaskar };
415b1d7137Swdenk 
4289a4d6b1SPrafulla Wadaskar /*
4389a4d6b1SPrafulla Wadaskar  * mkimage_register -
4489a4d6b1SPrafulla Wadaskar  *
4589a4d6b1SPrafulla Wadaskar  * It is used to register respective image generation/list support to the
4689a4d6b1SPrafulla Wadaskar  * mkimage core
4789a4d6b1SPrafulla Wadaskar  *
4889a4d6b1SPrafulla Wadaskar  * the input struct image_type_params is checked and appended to the link
4989a4d6b1SPrafulla Wadaskar  * list, if the input structure is already registered, error
5089a4d6b1SPrafulla Wadaskar  */
5189a4d6b1SPrafulla Wadaskar void mkimage_register (struct image_type_params *tparams)
5289a4d6b1SPrafulla Wadaskar {
5389a4d6b1SPrafulla Wadaskar 	struct image_type_params **tp;
5489a4d6b1SPrafulla Wadaskar 
5589a4d6b1SPrafulla Wadaskar 	if (!tparams) {
5689a4d6b1SPrafulla Wadaskar 		fprintf (stderr, "%s: %s: Null input\n",
5789a4d6b1SPrafulla Wadaskar 			params.cmdname, __FUNCTION__);
5889a4d6b1SPrafulla Wadaskar 		exit (EXIT_FAILURE);
5989a4d6b1SPrafulla Wadaskar 	}
6089a4d6b1SPrafulla Wadaskar 
6189a4d6b1SPrafulla Wadaskar 	/* scan the linked list, check for registry and point the last one */
6289a4d6b1SPrafulla Wadaskar 	for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) {
6389a4d6b1SPrafulla Wadaskar 		if (!strcmp((*tp)->name, tparams->name)) {
6489a4d6b1SPrafulla Wadaskar 			fprintf (stderr, "%s: %s already registered\n",
6589a4d6b1SPrafulla Wadaskar 				params.cmdname, tparams->name);
6689a4d6b1SPrafulla Wadaskar 			return;
6789a4d6b1SPrafulla Wadaskar 		}
6889a4d6b1SPrafulla Wadaskar 	}
6989a4d6b1SPrafulla Wadaskar 
7089a4d6b1SPrafulla Wadaskar 	/* add input struct entry at the end of link list */
7189a4d6b1SPrafulla Wadaskar 	*tp = tparams;
7289a4d6b1SPrafulla Wadaskar 	/* mark input entry as last entry in the link list */
7389a4d6b1SPrafulla Wadaskar 	tparams->next = NULL;
7489a4d6b1SPrafulla Wadaskar 
7589a4d6b1SPrafulla Wadaskar 	debug ("Registered %s\n", tparams->name);
7689a4d6b1SPrafulla Wadaskar }
7789a4d6b1SPrafulla Wadaskar 
7889a4d6b1SPrafulla Wadaskar /*
7989a4d6b1SPrafulla Wadaskar  * mkimage_get_type -
8089a4d6b1SPrafulla Wadaskar  *
8189a4d6b1SPrafulla Wadaskar  * It scans all registers image type supports
8289a4d6b1SPrafulla Wadaskar  * checks the input type_id for each supported image type
8389a4d6b1SPrafulla Wadaskar  *
8489a4d6b1SPrafulla Wadaskar  * if successful,
8589a4d6b1SPrafulla Wadaskar  * 	returns respective image_type_params pointer if success
8689a4d6b1SPrafulla Wadaskar  * if input type_id is not supported by any of image_type_support
8789a4d6b1SPrafulla Wadaskar  * 	returns NULL
8889a4d6b1SPrafulla Wadaskar  */
8989a4d6b1SPrafulla Wadaskar struct image_type_params *mkimage_get_type(int type)
9089a4d6b1SPrafulla Wadaskar {
9189a4d6b1SPrafulla Wadaskar 	struct image_type_params *curr;
9289a4d6b1SPrafulla Wadaskar 
9389a4d6b1SPrafulla Wadaskar 	for (curr = mkimage_tparams; curr != NULL; curr = curr->next) {
9489a4d6b1SPrafulla Wadaskar 		if (curr->check_image_type) {
9589a4d6b1SPrafulla Wadaskar 			if (!curr->check_image_type (type))
9689a4d6b1SPrafulla Wadaskar 				return curr;
9789a4d6b1SPrafulla Wadaskar 		}
9889a4d6b1SPrafulla Wadaskar 	}
9989a4d6b1SPrafulla Wadaskar 	return NULL;
10089a4d6b1SPrafulla Wadaskar }
10189a4d6b1SPrafulla Wadaskar 
10289a4d6b1SPrafulla Wadaskar /*
10389a4d6b1SPrafulla Wadaskar  * mkimage_verify_print_header -
10489a4d6b1SPrafulla Wadaskar  *
10589a4d6b1SPrafulla Wadaskar  * It scans mkimage_tparams link list,
10689a4d6b1SPrafulla Wadaskar  * verifies image_header for each supported image type
10789a4d6b1SPrafulla Wadaskar  * if verification is successful, prints respective header
10889a4d6b1SPrafulla Wadaskar  *
10989a4d6b1SPrafulla Wadaskar  * returns negative if input image format does not match with any of
11089a4d6b1SPrafulla Wadaskar  * supported image types
11189a4d6b1SPrafulla Wadaskar  */
11289a4d6b1SPrafulla Wadaskar int mkimage_verify_print_header (void *ptr, struct stat *sbuf)
11389a4d6b1SPrafulla Wadaskar {
11489a4d6b1SPrafulla Wadaskar 	int retval = -1;
11589a4d6b1SPrafulla Wadaskar 	struct image_type_params *curr;
11689a4d6b1SPrafulla Wadaskar 
11789a4d6b1SPrafulla Wadaskar 	for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) {
11889a4d6b1SPrafulla Wadaskar 		if (curr->verify_header) {
11989a4d6b1SPrafulla Wadaskar 			retval = curr->verify_header (
12089a4d6b1SPrafulla Wadaskar 				(unsigned char *)ptr, sbuf->st_size,
12189a4d6b1SPrafulla Wadaskar 				&params);
12289a4d6b1SPrafulla Wadaskar 
12389a4d6b1SPrafulla Wadaskar 			if (retval == 0) {
12489a4d6b1SPrafulla Wadaskar 				/*
12589a4d6b1SPrafulla Wadaskar 				 * Print the image information
12689a4d6b1SPrafulla Wadaskar 				 * if verify is successful
12789a4d6b1SPrafulla Wadaskar 				 */
12889a4d6b1SPrafulla Wadaskar 				if (curr->print_header)
12989a4d6b1SPrafulla Wadaskar 					curr->print_header (ptr);
13089a4d6b1SPrafulla Wadaskar 				else {
13189a4d6b1SPrafulla Wadaskar 					fprintf (stderr,
13289a4d6b1SPrafulla Wadaskar 					"%s: print_header undefined for %s\n",
13389a4d6b1SPrafulla Wadaskar 					params.cmdname, curr->name);
13489a4d6b1SPrafulla Wadaskar 				}
13589a4d6b1SPrafulla Wadaskar 				break;
13689a4d6b1SPrafulla Wadaskar 			}
13789a4d6b1SPrafulla Wadaskar 		}
13889a4d6b1SPrafulla Wadaskar 	}
13989a4d6b1SPrafulla Wadaskar 	return retval;
14089a4d6b1SPrafulla Wadaskar }
1415b1d7137Swdenk 
1425b1d7137Swdenk int
1435b1d7137Swdenk main (int argc, char **argv)
1445b1d7137Swdenk {
1459d25438fSBartlomiej Sieka 	int ifd = -1;
1465b1d7137Swdenk 	struct stat sbuf;
1475b1d7137Swdenk 	unsigned char *ptr;
148449609f5SPrafulla Wadaskar 	int retval = 0;
14989a4d6b1SPrafulla Wadaskar 	struct image_type_params *tparams = NULL;
1505b1d7137Swdenk 
151aa0c7a86SPrafulla Wadaskar 	/* Init Kirkwood Boot image generation/list support */
152aa0c7a86SPrafulla Wadaskar 	init_kwb_image_type ();
15389a4d6b1SPrafulla Wadaskar 	/* Init FIT image generation/list support */
15489a4d6b1SPrafulla Wadaskar 	init_fit_image_type ();
15589a4d6b1SPrafulla Wadaskar 	/* Init Default image generation/list support */
15689a4d6b1SPrafulla Wadaskar 	init_default_image_type ();
1575b1d7137Swdenk 
15889a4d6b1SPrafulla Wadaskar 	params.cmdname = *argv;
15989a4d6b1SPrafulla Wadaskar 	params.addr = params.ep = 0;
1605b1d7137Swdenk 
1615b1d7137Swdenk 	while (--argc > 0 && **++argv == '-') {
1625b1d7137Swdenk 		while (*++*argv) {
1635b1d7137Swdenk 			switch (**argv) {
1645b1d7137Swdenk 			case 'l':
16589a4d6b1SPrafulla Wadaskar 				params.lflag = 1;
1665b1d7137Swdenk 				break;
1675b1d7137Swdenk 			case 'A':
1685b1d7137Swdenk 				if ((--argc <= 0) ||
16989a4d6b1SPrafulla Wadaskar 					(params.arch =
17089a4d6b1SPrafulla Wadaskar 					genimg_get_arch_id (*++argv)) < 0)
1715b1d7137Swdenk 					usage ();
1725b1d7137Swdenk 				goto NXTARG;
1735b1d7137Swdenk 			case 'C':
1745b1d7137Swdenk 				if ((--argc <= 0) ||
17589a4d6b1SPrafulla Wadaskar 					(params.comp =
17689a4d6b1SPrafulla Wadaskar 					genimg_get_comp_id (*++argv)) < 0)
1775b1d7137Swdenk 					usage ();
1785b1d7137Swdenk 				goto NXTARG;
1799d25438fSBartlomiej Sieka 			case 'D':
1809d25438fSBartlomiej Sieka 				if (--argc <= 0)
1819d25438fSBartlomiej Sieka 					usage ();
18289a4d6b1SPrafulla Wadaskar 				params.dtc = *++argv;
1839d25438fSBartlomiej Sieka 				goto NXTARG;
1849d25438fSBartlomiej Sieka 
1855b1d7137Swdenk 			case 'O':
1865b1d7137Swdenk 				if ((--argc <= 0) ||
18789a4d6b1SPrafulla Wadaskar 					(params.os =
18889a4d6b1SPrafulla Wadaskar 					genimg_get_os_id (*++argv)) < 0)
1895b1d7137Swdenk 					usage ();
1905b1d7137Swdenk 				goto NXTARG;
1915b1d7137Swdenk 			case 'T':
1925b1d7137Swdenk 				if ((--argc <= 0) ||
19389a4d6b1SPrafulla Wadaskar 					(params.type =
19489a4d6b1SPrafulla Wadaskar 					genimg_get_type_id (*++argv)) < 0)
1955b1d7137Swdenk 					usage ();
1965b1d7137Swdenk 				goto NXTARG;
1975b1d7137Swdenk 
1985b1d7137Swdenk 			case 'a':
1995b1d7137Swdenk 				if (--argc <= 0)
2005b1d7137Swdenk 					usage ();
20189a4d6b1SPrafulla Wadaskar 				params.addr = strtoul (*++argv,
20289a4d6b1SPrafulla Wadaskar 					(char **)&ptr, 16);
2035b1d7137Swdenk 				if (*ptr) {
2045b1d7137Swdenk 					fprintf (stderr,
2055b1d7137Swdenk 						"%s: invalid load address %s\n",
20689a4d6b1SPrafulla Wadaskar 						params.cmdname, *argv);
2075b1d7137Swdenk 					exit (EXIT_FAILURE);
2085b1d7137Swdenk 				}
2095b1d7137Swdenk 				goto NXTARG;
2105b1d7137Swdenk 			case 'd':
2115b1d7137Swdenk 				if (--argc <= 0)
2125b1d7137Swdenk 					usage ();
21389a4d6b1SPrafulla Wadaskar 				params.datafile = *++argv;
21489a4d6b1SPrafulla Wadaskar 				params.dflag = 1;
2155b1d7137Swdenk 				goto NXTARG;
2165b1d7137Swdenk 			case 'e':
2175b1d7137Swdenk 				if (--argc <= 0)
2185b1d7137Swdenk 					usage ();
21989a4d6b1SPrafulla Wadaskar 				params.ep = strtoul (*++argv,
22089a4d6b1SPrafulla Wadaskar 						(char **)&ptr, 16);
2215b1d7137Swdenk 				if (*ptr) {
2225b1d7137Swdenk 					fprintf (stderr,
2235b1d7137Swdenk 						"%s: invalid entry point %s\n",
22489a4d6b1SPrafulla Wadaskar 						params.cmdname, *argv);
2255b1d7137Swdenk 					exit (EXIT_FAILURE);
2265b1d7137Swdenk 				}
22789a4d6b1SPrafulla Wadaskar 				params.eflag = 1;
2285b1d7137Swdenk 				goto NXTARG;
2299d25438fSBartlomiej Sieka 			case 'f':
2309d25438fSBartlomiej Sieka 				if (--argc <= 0)
2319d25438fSBartlomiej Sieka 					usage ();
2326a590c5fSRemy Bohmer 				params.type = IH_TYPE_FLATDT;
23389a4d6b1SPrafulla Wadaskar 				params.datafile = *++argv;
23489a4d6b1SPrafulla Wadaskar 				params.fflag = 1;
235*1a99de2cSPeter Tyser 
236*1a99de2cSPeter Tyser 				/*
237*1a99de2cSPeter Tyser 				 * The flattened image tree (FIT) format
238*1a99de2cSPeter Tyser 				 * requires a flattened device tree image type
239*1a99de2cSPeter Tyser 				 */
240*1a99de2cSPeter Tyser 				params.type = IH_TYPE_FLATDT;
2419d25438fSBartlomiej Sieka 				goto NXTARG;
2425b1d7137Swdenk 			case 'n':
2435b1d7137Swdenk 				if (--argc <= 0)
2445b1d7137Swdenk 					usage ();
24589a4d6b1SPrafulla Wadaskar 				params.imagename = *++argv;
2465b1d7137Swdenk 				goto NXTARG;
2475b1d7137Swdenk 			case 'v':
24889a4d6b1SPrafulla Wadaskar 				params.vflag++;
2495b1d7137Swdenk 				break;
2505b1d7137Swdenk 			case 'x':
25189a4d6b1SPrafulla Wadaskar 				params.xflag++;
2525b1d7137Swdenk 				break;
2535b1d7137Swdenk 			default:
2545b1d7137Swdenk 				usage ();
2555b1d7137Swdenk 			}
2565b1d7137Swdenk 		}
2575b1d7137Swdenk NXTARG:		;
2585b1d7137Swdenk 	}
2595b1d7137Swdenk 
26089a4d6b1SPrafulla Wadaskar 	if (argc != 1)
2615b1d7137Swdenk 		usage ();
2625b1d7137Swdenk 
26389a4d6b1SPrafulla Wadaskar 	/* set tparams as per input type_id */
26489a4d6b1SPrafulla Wadaskar 	tparams = mkimage_get_type(params.type);
26589a4d6b1SPrafulla Wadaskar 	if (tparams == NULL) {
26689a4d6b1SPrafulla Wadaskar 		fprintf (stderr, "%s: unsupported type %s\n",
26789a4d6b1SPrafulla Wadaskar 			params.cmdname, genimg_get_type_name(params.type));
26889a4d6b1SPrafulla Wadaskar 		exit (EXIT_FAILURE);
26989a4d6b1SPrafulla Wadaskar 	}
27089a4d6b1SPrafulla Wadaskar 
27189a4d6b1SPrafulla Wadaskar 	/*
27289a4d6b1SPrafulla Wadaskar 	 * check the passed arguments parameters meets the requirements
27389a4d6b1SPrafulla Wadaskar 	 * as per image type to be generated/listed
27489a4d6b1SPrafulla Wadaskar 	 */
27589a4d6b1SPrafulla Wadaskar 	if (tparams->check_params)
27689a4d6b1SPrafulla Wadaskar 		if (tparams->check_params (&params))
27789a4d6b1SPrafulla Wadaskar 			usage ();
27889a4d6b1SPrafulla Wadaskar 
27989a4d6b1SPrafulla Wadaskar 	if (!params.eflag) {
28089a4d6b1SPrafulla Wadaskar 		params.ep = params.addr;
2815b1d7137Swdenk 		/* If XIP, entry point must be after the U-Boot header */
28289a4d6b1SPrafulla Wadaskar 		if (params.xflag)
28389a4d6b1SPrafulla Wadaskar 			params.ep += tparams->header_size;
2845b1d7137Swdenk 	}
2855b1d7137Swdenk 
2865b1d7137Swdenk 	/*
2875b1d7137Swdenk 	 * If XIP, ensure the entry point is equal to the load address plus
2885b1d7137Swdenk 	 * the size of the U-Boot header.
2895b1d7137Swdenk 	 */
29089a4d6b1SPrafulla Wadaskar 	if (params.xflag) {
29189a4d6b1SPrafulla Wadaskar 		if (params.ep != params.addr + tparams->header_size) {
2923577d3a3SWolfgang Denk 			fprintf (stderr,
2933577d3a3SWolfgang Denk 				"%s: For XIP, the entry point must be the load addr + %lu\n",
29489a4d6b1SPrafulla Wadaskar 				params.cmdname,
29589a4d6b1SPrafulla Wadaskar 				(unsigned long)tparams->header_size);
2965b1d7137Swdenk 			exit (EXIT_FAILURE);
2975b1d7137Swdenk 		}
2985b1d7137Swdenk 	}
2995b1d7137Swdenk 
30089a4d6b1SPrafulla Wadaskar 	params.imagefile = *argv;
3015b1d7137Swdenk 
30289a4d6b1SPrafulla Wadaskar 	if (!params.fflag){
30389a4d6b1SPrafulla Wadaskar 		if (params.lflag) {
30489a4d6b1SPrafulla Wadaskar 			ifd = open (params.imagefile, O_RDONLY|O_BINARY);
3055b1d7137Swdenk 		} else {
30689a4d6b1SPrafulla Wadaskar 			ifd = open (params.imagefile,
3079d25438fSBartlomiej Sieka 				O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
3085b1d7137Swdenk 		}
3095b1d7137Swdenk 
3105b1d7137Swdenk 		if (ifd < 0) {
3115b1d7137Swdenk 			fprintf (stderr, "%s: Can't open %s: %s\n",
31289a4d6b1SPrafulla Wadaskar 				params.cmdname, params.imagefile,
31389a4d6b1SPrafulla Wadaskar 				strerror(errno));
3145b1d7137Swdenk 			exit (EXIT_FAILURE);
3155b1d7137Swdenk 		}
3169d25438fSBartlomiej Sieka 	}
3175b1d7137Swdenk 
31889a4d6b1SPrafulla Wadaskar 	if (params.lflag) {
3195b1d7137Swdenk 		/*
3205b1d7137Swdenk 		 * list header information of existing image
3215b1d7137Swdenk 		 */
3225b1d7137Swdenk 		if (fstat(ifd, &sbuf) < 0) {
3235b1d7137Swdenk 			fprintf (stderr, "%s: Can't stat %s: %s\n",
32489a4d6b1SPrafulla Wadaskar 				params.cmdname, params.imagefile,
32589a4d6b1SPrafulla Wadaskar 				strerror(errno));
3265b1d7137Swdenk 			exit (EXIT_FAILURE);
3275b1d7137Swdenk 		}
3285b1d7137Swdenk 
32989a4d6b1SPrafulla Wadaskar 		if ((unsigned)sbuf.st_size < tparams->header_size) {
3305b1d7137Swdenk 			fprintf (stderr,
33189a4d6b1SPrafulla Wadaskar 				"%s: Bad size: \"%s\" is not valid image\n",
33289a4d6b1SPrafulla Wadaskar 				params.cmdname, params.imagefile);
3335b1d7137Swdenk 			exit (EXIT_FAILURE);
3345b1d7137Swdenk 		}
3355b1d7137Swdenk 
336fa956fdeSMike Frysinger 		ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
337fa956fdeSMike Frysinger 		if (ptr == MAP_FAILED) {
3385b1d7137Swdenk 			fprintf (stderr, "%s: Can't read %s: %s\n",
33989a4d6b1SPrafulla Wadaskar 				params.cmdname, params.imagefile,
34089a4d6b1SPrafulla Wadaskar 				strerror(errno));
3415b1d7137Swdenk 			exit (EXIT_FAILURE);
3425b1d7137Swdenk 		}
3435b1d7137Swdenk 
34489a4d6b1SPrafulla Wadaskar 		/*
34589a4d6b1SPrafulla Wadaskar 		 * scan through mkimage registry for all supported image types
34689a4d6b1SPrafulla Wadaskar 		 * and verify the input image file header for match
34789a4d6b1SPrafulla Wadaskar 		 * Print the image information for matched image type
34889a4d6b1SPrafulla Wadaskar 		 * Returns the error code if not matched
34989a4d6b1SPrafulla Wadaskar 		 */
35089a4d6b1SPrafulla Wadaskar 		retval = mkimage_verify_print_header (ptr, &sbuf);
3515b1d7137Swdenk 
3525b1d7137Swdenk 		(void) munmap((void *)ptr, sbuf.st_size);
3535b1d7137Swdenk 		(void) close (ifd);
3545b1d7137Swdenk 
355f7644c0bSPrafulla Wadaskar 		exit (retval);
35689a4d6b1SPrafulla Wadaskar 	} else if (params.fflag) {
35789a4d6b1SPrafulla Wadaskar 		if (tparams->fflag_handle)
35889a4d6b1SPrafulla Wadaskar 			/*
35989a4d6b1SPrafulla Wadaskar 			 * in some cases, some additional processing needs
36089a4d6b1SPrafulla Wadaskar 			 * to be done if fflag is defined
36189a4d6b1SPrafulla Wadaskar 			 *
36289a4d6b1SPrafulla Wadaskar 			 * For ex. fit_handle_file for Fit file support
36389a4d6b1SPrafulla Wadaskar 			 */
36489a4d6b1SPrafulla Wadaskar 			retval = tparams->fflag_handle(&params);
36589a4d6b1SPrafulla Wadaskar 
366f7644c0bSPrafulla Wadaskar 		exit (retval);
3675b1d7137Swdenk 	}
3685b1d7137Swdenk 
3695b1d7137Swdenk 	/*
3705b1d7137Swdenk 	 * Must be -w then:
3715b1d7137Swdenk 	 *
3725b1d7137Swdenk 	 * write dummy header, to be fixed later
3735b1d7137Swdenk 	 */
37489a4d6b1SPrafulla Wadaskar 	memset (tparams->hdr, 0, tparams->header_size);
3755b1d7137Swdenk 
37689a4d6b1SPrafulla Wadaskar 	if (write(ifd, tparams->hdr, tparams->header_size)
37789a4d6b1SPrafulla Wadaskar 					!= tparams->header_size) {
3785b1d7137Swdenk 		fprintf (stderr, "%s: Write error on %s: %s\n",
37989a4d6b1SPrafulla Wadaskar 			params.cmdname, params.imagefile, strerror(errno));
3805b1d7137Swdenk 		exit (EXIT_FAILURE);
3815b1d7137Swdenk 	}
3825b1d7137Swdenk 
38389a4d6b1SPrafulla Wadaskar 	if (params.type == IH_TYPE_MULTI || params.type == IH_TYPE_SCRIPT) {
38489a4d6b1SPrafulla Wadaskar 		char *file = params.datafile;
3853bb66806SWolfgang Denk 		uint32_t size;
3865b1d7137Swdenk 
3875b1d7137Swdenk 		for (;;) {
3885b1d7137Swdenk 			char *sep = NULL;
3895b1d7137Swdenk 
3905b1d7137Swdenk 			if (file) {
3915b1d7137Swdenk 				if ((sep = strchr(file, ':')) != NULL) {
3925b1d7137Swdenk 					*sep = '\0';
3935b1d7137Swdenk 				}
3945b1d7137Swdenk 
3955b1d7137Swdenk 				if (stat (file, &sbuf) < 0) {
3965b1d7137Swdenk 					fprintf (stderr, "%s: Can't stat %s: %s\n",
39789a4d6b1SPrafulla Wadaskar 						params.cmdname, file, strerror(errno));
3985b1d7137Swdenk 					exit (EXIT_FAILURE);
3995b1d7137Swdenk 				}
4009a4daad0SMarian Balakowicz 				size = cpu_to_uimage (sbuf.st_size);
4015b1d7137Swdenk 			} else {
4025b1d7137Swdenk 				size = 0;
4035b1d7137Swdenk 			}
4045b1d7137Swdenk 
4055b1d7137Swdenk 			if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
4065b1d7137Swdenk 				fprintf (stderr, "%s: Write error on %s: %s\n",
40789a4d6b1SPrafulla Wadaskar 					params.cmdname, params.imagefile,
40889a4d6b1SPrafulla Wadaskar 					strerror(errno));
4095b1d7137Swdenk 				exit (EXIT_FAILURE);
4105b1d7137Swdenk 			}
4115b1d7137Swdenk 
4125b1d7137Swdenk 			if (!file) {
4135b1d7137Swdenk 				break;
4145b1d7137Swdenk 			}
4155b1d7137Swdenk 
4165b1d7137Swdenk 			if (sep) {
4175b1d7137Swdenk 				*sep = ':';
4185b1d7137Swdenk 				file = sep + 1;
4195b1d7137Swdenk 			} else {
4205b1d7137Swdenk 				file = NULL;
4215b1d7137Swdenk 			}
4225b1d7137Swdenk 		}
4235b1d7137Swdenk 
42489a4d6b1SPrafulla Wadaskar 		file = params.datafile;
4255b1d7137Swdenk 
4265b1d7137Swdenk 		for (;;) {
4275b1d7137Swdenk 			char *sep = strchr(file, ':');
4285b1d7137Swdenk 			if (sep) {
4295b1d7137Swdenk 				*sep = '\0';
4305b1d7137Swdenk 				copy_file (ifd, file, 1);
4315b1d7137Swdenk 				*sep++ = ':';
4325b1d7137Swdenk 				file = sep;
4335b1d7137Swdenk 			} else {
4345b1d7137Swdenk 				copy_file (ifd, file, 0);
4355b1d7137Swdenk 				break;
4365b1d7137Swdenk 			}
4375b1d7137Swdenk 		}
4385b1d7137Swdenk 	} else {
43989a4d6b1SPrafulla Wadaskar 		copy_file (ifd, params.datafile, 0);
4405b1d7137Swdenk 	}
4415b1d7137Swdenk 
4425b1d7137Swdenk 	/* We're a bit of paranoid */
44389a4d6b1SPrafulla Wadaskar #if defined(_POSIX_SYNCHRONIZED_IO) && \
44489a4d6b1SPrafulla Wadaskar    !defined(__sun__) && \
44589a4d6b1SPrafulla Wadaskar    !defined(__FreeBSD__) && \
44689a4d6b1SPrafulla Wadaskar    !defined(__APPLE__)
4475b1d7137Swdenk 	(void) fdatasync (ifd);
4485b1d7137Swdenk #else
4495b1d7137Swdenk 	(void) fsync (ifd);
4505b1d7137Swdenk #endif
4515b1d7137Swdenk 
4525b1d7137Swdenk 	if (fstat(ifd, &sbuf) < 0) {
4535b1d7137Swdenk 		fprintf (stderr, "%s: Can't stat %s: %s\n",
45489a4d6b1SPrafulla Wadaskar 			params.cmdname, params.imagefile, strerror(errno));
4555b1d7137Swdenk 		exit (EXIT_FAILURE);
4565b1d7137Swdenk 	}
4575b1d7137Swdenk 
458fa956fdeSMike Frysinger 	ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
459fa956fdeSMike Frysinger 	if (ptr == MAP_FAILED) {
4605b1d7137Swdenk 		fprintf (stderr, "%s: Can't map %s: %s\n",
46189a4d6b1SPrafulla Wadaskar 			params.cmdname, params.imagefile, strerror(errno));
4625b1d7137Swdenk 		exit (EXIT_FAILURE);
4635b1d7137Swdenk 	}
4645b1d7137Swdenk 
46589a4d6b1SPrafulla Wadaskar 	/* Setup the image header as per input image type*/
46689a4d6b1SPrafulla Wadaskar 	if (tparams->set_header)
46789a4d6b1SPrafulla Wadaskar 		tparams->set_header (ptr, &sbuf, ifd, &params);
46889a4d6b1SPrafulla Wadaskar 	else {
46989a4d6b1SPrafulla Wadaskar 		fprintf (stderr, "%s: Can't set header for %s: %s\n",
47089a4d6b1SPrafulla Wadaskar 			params.cmdname, tparams->name, strerror(errno));
47189a4d6b1SPrafulla Wadaskar 		exit (EXIT_FAILURE);
47289a4d6b1SPrafulla Wadaskar 	}
4735b1d7137Swdenk 
47489a4d6b1SPrafulla Wadaskar 	/* Print the image information by processing image header */
47589a4d6b1SPrafulla Wadaskar 	if (tparams->print_header)
47689a4d6b1SPrafulla Wadaskar 		tparams->print_header (ptr);
47789a4d6b1SPrafulla Wadaskar 	else {
47889a4d6b1SPrafulla Wadaskar 		fprintf (stderr, "%s: Can't print header for %s: %s\n",
47989a4d6b1SPrafulla Wadaskar 			params.cmdname, tparams->name, strerror(errno));
48089a4d6b1SPrafulla Wadaskar 		exit (EXIT_FAILURE);
48189a4d6b1SPrafulla Wadaskar 	}
4825b1d7137Swdenk 
4835b1d7137Swdenk 	(void) munmap((void *)ptr, sbuf.st_size);
4845b1d7137Swdenk 
4855b1d7137Swdenk 	/* We're a bit of paranoid */
48689a4d6b1SPrafulla Wadaskar #if defined(_POSIX_SYNCHRONIZED_IO) && \
48789a4d6b1SPrafulla Wadaskar    !defined(__sun__) && \
48889a4d6b1SPrafulla Wadaskar    !defined(__FreeBSD__) && \
48989a4d6b1SPrafulla Wadaskar    !defined(__APPLE__)
4905b1d7137Swdenk 	(void) fdatasync (ifd);
4915b1d7137Swdenk #else
4925b1d7137Swdenk 	(void) fsync (ifd);
4935b1d7137Swdenk #endif
4945b1d7137Swdenk 
4955b1d7137Swdenk 	if (close(ifd)) {
4965b1d7137Swdenk 		fprintf (stderr, "%s: Write error on %s: %s\n",
49789a4d6b1SPrafulla Wadaskar 			params.cmdname, params.imagefile, strerror(errno));
4985b1d7137Swdenk 		exit (EXIT_FAILURE);
4995b1d7137Swdenk 	}
5005b1d7137Swdenk 
5015b1d7137Swdenk 	exit (EXIT_SUCCESS);
5025b1d7137Swdenk }
5035b1d7137Swdenk 
5045b1d7137Swdenk static void
5055b1d7137Swdenk copy_file (int ifd, const char *datafile, int pad)
5065b1d7137Swdenk {
5075b1d7137Swdenk 	int dfd;
5085b1d7137Swdenk 	struct stat sbuf;
5095b1d7137Swdenk 	unsigned char *ptr;
5105b1d7137Swdenk 	int tail;
5115b1d7137Swdenk 	int zero = 0;
5125b1d7137Swdenk 	int offset = 0;
5135b1d7137Swdenk 	int size;
51489a4d6b1SPrafulla Wadaskar 	struct image_type_params *tparams = mkimage_get_type (params.type);
5155b1d7137Swdenk 
51689a4d6b1SPrafulla Wadaskar 	if (params.vflag) {
5175b1d7137Swdenk 		fprintf (stderr, "Adding Image %s\n", datafile);
5185b1d7137Swdenk 	}
5195b1d7137Swdenk 
520ef1464ccSwdenk 	if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
5215b1d7137Swdenk 		fprintf (stderr, "%s: Can't open %s: %s\n",
52289a4d6b1SPrafulla Wadaskar 			params.cmdname, datafile, strerror(errno));
5235b1d7137Swdenk 		exit (EXIT_FAILURE);
5245b1d7137Swdenk 	}
5255b1d7137Swdenk 
5265b1d7137Swdenk 	if (fstat(dfd, &sbuf) < 0) {
5275b1d7137Swdenk 		fprintf (stderr, "%s: Can't stat %s: %s\n",
52889a4d6b1SPrafulla Wadaskar 			params.cmdname, datafile, strerror(errno));
5295b1d7137Swdenk 		exit (EXIT_FAILURE);
5305b1d7137Swdenk 	}
5315b1d7137Swdenk 
532fa956fdeSMike Frysinger 	ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
533fa956fdeSMike Frysinger 	if (ptr == MAP_FAILED) {
5345b1d7137Swdenk 		fprintf (stderr, "%s: Can't read %s: %s\n",
53589a4d6b1SPrafulla Wadaskar 			params.cmdname, datafile, strerror(errno));
5365b1d7137Swdenk 		exit (EXIT_FAILURE);
5375b1d7137Swdenk 	}
5385b1d7137Swdenk 
53989a4d6b1SPrafulla Wadaskar 	if (params.xflag) {
5405b1d7137Swdenk 		unsigned char *p = NULL;
5415b1d7137Swdenk 		/*
5425b1d7137Swdenk 		 * XIP: do not append the image_header_t at the
5435b1d7137Swdenk 		 * beginning of the file, but consume the space
5445b1d7137Swdenk 		 * reserved for it.
5455b1d7137Swdenk 		 */
5465b1d7137Swdenk 
54789a4d6b1SPrafulla Wadaskar 		if ((unsigned)sbuf.st_size < tparams->header_size) {
5485b1d7137Swdenk 			fprintf (stderr,
5495b1d7137Swdenk 				"%s: Bad size: \"%s\" is too small for XIP\n",
55089a4d6b1SPrafulla Wadaskar 				params.cmdname, datafile);
5515b1d7137Swdenk 			exit (EXIT_FAILURE);
5525b1d7137Swdenk 		}
5535b1d7137Swdenk 
55489a4d6b1SPrafulla Wadaskar 		for (p = ptr; p < ptr + tparams->header_size; p++) {
5555b1d7137Swdenk 			if ( *p != 0xff ) {
5565b1d7137Swdenk 				fprintf (stderr,
5575b1d7137Swdenk 					"%s: Bad file: \"%s\" has invalid buffer for XIP\n",
55889a4d6b1SPrafulla Wadaskar 					params.cmdname, datafile);
5595b1d7137Swdenk 				exit (EXIT_FAILURE);
5605b1d7137Swdenk 			}
5615b1d7137Swdenk 		}
5625b1d7137Swdenk 
56389a4d6b1SPrafulla Wadaskar 		offset = tparams->header_size;
5645b1d7137Swdenk 	}
5655b1d7137Swdenk 
5665b1d7137Swdenk 	size = sbuf.st_size - offset;
5675b1d7137Swdenk 	if (write(ifd, ptr + offset, size) != size) {
5685b1d7137Swdenk 		fprintf (stderr, "%s: Write error on %s: %s\n",
56989a4d6b1SPrafulla Wadaskar 			params.cmdname, params.imagefile, strerror(errno));
5705b1d7137Swdenk 		exit (EXIT_FAILURE);
5715b1d7137Swdenk 	}
5725b1d7137Swdenk 
5735b1d7137Swdenk 	if (pad && ((tail = size % 4) != 0)) {
5745b1d7137Swdenk 
5755b1d7137Swdenk 		if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
5765b1d7137Swdenk 			fprintf (stderr, "%s: Write error on %s: %s\n",
57789a4d6b1SPrafulla Wadaskar 				params.cmdname, params.imagefile,
57889a4d6b1SPrafulla Wadaskar 				strerror(errno));
5795b1d7137Swdenk 			exit (EXIT_FAILURE);
5805b1d7137Swdenk 		}
5815b1d7137Swdenk 	}
5825b1d7137Swdenk 
5835b1d7137Swdenk 	(void) munmap((void *)ptr, sbuf.st_size);
5845b1d7137Swdenk 	(void) close (dfd);
5855b1d7137Swdenk }
5865b1d7137Swdenk 
5875b1d7137Swdenk void
5885b1d7137Swdenk usage ()
5895b1d7137Swdenk {
5905b1d7137Swdenk 	fprintf (stderr, "Usage: %s -l image\n"
5919d25438fSBartlomiej Sieka 			 "          -l ==> list image header information\n",
59289a4d6b1SPrafulla Wadaskar 		params.cmdname);
5939d25438fSBartlomiej Sieka 	fprintf (stderr, "       %s [-x] -A arch -O os -T type -C comp "
5949d25438fSBartlomiej Sieka 			 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
5959d25438fSBartlomiej Sieka 			 "          -A ==> set architecture to 'arch'\n"
5965b1d7137Swdenk 			 "          -O ==> set operating system to 'os'\n"
5975b1d7137Swdenk 			 "          -T ==> set image type to 'type'\n"
5985b1d7137Swdenk 			 "          -C ==> set compression type 'comp'\n"
5995b1d7137Swdenk 			 "          -a ==> set load address to 'addr' (hex)\n"
6005b1d7137Swdenk 			 "          -e ==> set entry point to 'ep' (hex)\n"
6015b1d7137Swdenk 			 "          -n ==> set image name to 'name'\n"
6025b1d7137Swdenk 			 "          -d ==> use image data from 'datafile'\n"
6039d25438fSBartlomiej Sieka 			 "          -x ==> set XIP (execute in place)\n",
60489a4d6b1SPrafulla Wadaskar 		params.cmdname);
6059d25438fSBartlomiej Sieka 	fprintf (stderr, "       %s [-D dtc_options] -f fit-image.its fit-image\n",
60689a4d6b1SPrafulla Wadaskar 		params.cmdname);
6079d25438fSBartlomiej Sieka 
6085b1d7137Swdenk 	exit (EXIT_FAILURE);
6095b1d7137Swdenk }
610