xref: /openbmc/u-boot/tools/mkimage.c (revision 9d86f0c3)
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2009
5  * DENX Software Engineering
6  * Wolfgang Denk, wd@denx.de
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #include "mkimage.h"
25 #include <image.h>
26 #include <version.h>
27 
28 static void copy_file(int, const char *, int);
29 static void usage(void);
30 
31 /* image_type_params link list to maintain registered image type supports */
32 struct image_type_params *mkimage_tparams = NULL;
33 
34 /* parameters initialized by core will be used by the image type code */
35 struct mkimage_params params = {
36 	.os = IH_OS_LINUX,
37 	.arch = IH_ARCH_PPC,
38 	.type = IH_TYPE_KERNEL,
39 	.comp = IH_COMP_GZIP,
40 	.dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
41 	.imagename = "",
42 	.imagename2 = "",
43 };
44 
45 /*
46  * mkimage_register -
47  *
48  * It is used to register respective image generation/list support to the
49  * mkimage core
50  *
51  * the input struct image_type_params is checked and appended to the link
52  * list, if the input structure is already registered, error
53  */
54 void mkimage_register (struct image_type_params *tparams)
55 {
56 	struct image_type_params **tp;
57 
58 	if (!tparams) {
59 		fprintf (stderr, "%s: %s: Null input\n",
60 			params.cmdname, __FUNCTION__);
61 		exit (EXIT_FAILURE);
62 	}
63 
64 	/* scan the linked list, check for registry and point the last one */
65 	for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) {
66 		if (!strcmp((*tp)->name, tparams->name)) {
67 			fprintf (stderr, "%s: %s already registered\n",
68 				params.cmdname, tparams->name);
69 			return;
70 		}
71 	}
72 
73 	/* add input struct entry at the end of link list */
74 	*tp = tparams;
75 	/* mark input entry as last entry in the link list */
76 	tparams->next = NULL;
77 
78 	debug ("Registered %s\n", tparams->name);
79 }
80 
81 /*
82  * mkimage_get_type -
83  *
84  * It scans all registers image type supports
85  * checks the input type_id for each supported image type
86  *
87  * if successful,
88  * 	returns respective image_type_params pointer if success
89  * if input type_id is not supported by any of image_type_support
90  * 	returns NULL
91  */
92 struct image_type_params *mkimage_get_type(int type)
93 {
94 	struct image_type_params *curr;
95 
96 	for (curr = mkimage_tparams; curr != NULL; curr = curr->next) {
97 		if (curr->check_image_type) {
98 			if (!curr->check_image_type (type))
99 				return curr;
100 		}
101 	}
102 	return NULL;
103 }
104 
105 /*
106  * mkimage_verify_print_header -
107  *
108  * It scans mkimage_tparams link list,
109  * verifies image_header for each supported image type
110  * if verification is successful, prints respective header
111  *
112  * returns negative if input image format does not match with any of
113  * supported image types
114  */
115 int mkimage_verify_print_header (void *ptr, struct stat *sbuf)
116 {
117 	int retval = -1;
118 	struct image_type_params *curr;
119 
120 	for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) {
121 		if (curr->verify_header) {
122 			retval = curr->verify_header (
123 				(unsigned char *)ptr, sbuf->st_size,
124 				&params);
125 
126 			if (retval == 0) {
127 				/*
128 				 * Print the image information
129 				 * if verify is successful
130 				 */
131 				if (curr->print_header)
132 					curr->print_header (ptr);
133 				else {
134 					fprintf (stderr,
135 					"%s: print_header undefined for %s\n",
136 					params.cmdname, curr->name);
137 				}
138 				break;
139 			}
140 		}
141 	}
142 	return retval;
143 }
144 
145 int
146 main (int argc, char **argv)
147 {
148 	int ifd = -1;
149 	struct stat sbuf;
150 	char *ptr;
151 	int retval = 0;
152 	struct image_type_params *tparams = NULL;
153 
154 	/* Init Freescale PBL Boot image generation/list support */
155 	init_pbl_image_type();
156 	/* Init Kirkwood Boot image generation/list support */
157 	init_kwb_image_type ();
158 	/* Init Freescale imx Boot image generation/list support */
159 	init_imx_image_type ();
160 	/* Init FIT image generation/list support */
161 	init_fit_image_type ();
162 	/* Init TI OMAP Boot image generation/list support */
163 	init_omap_image_type();
164 	/* Init Default image generation/list support */
165 	init_default_image_type ();
166 	/* Init Davinci UBL support */
167 	init_ubl_image_type();
168 	/* Init Davinci AIS support */
169 	init_ais_image_type();
170 
171 	params.cmdname = *argv;
172 	params.addr = params.ep = 0;
173 
174 	while (--argc > 0 && **++argv == '-') {
175 		while (*++*argv) {
176 			switch (**argv) {
177 			case 'l':
178 				params.lflag = 1;
179 				break;
180 			case 'A':
181 				if ((--argc <= 0) ||
182 					(params.arch =
183 					genimg_get_arch_id (*++argv)) < 0)
184 					usage ();
185 				goto NXTARG;
186 			case 'C':
187 				if ((--argc <= 0) ||
188 					(params.comp =
189 					genimg_get_comp_id (*++argv)) < 0)
190 					usage ();
191 				goto NXTARG;
192 			case 'D':
193 				if (--argc <= 0)
194 					usage ();
195 				params.dtc = *++argv;
196 				goto NXTARG;
197 
198 			case 'O':
199 				if ((--argc <= 0) ||
200 					(params.os =
201 					genimg_get_os_id (*++argv)) < 0)
202 					usage ();
203 				goto NXTARG;
204 			case 'T':
205 				if ((--argc <= 0) ||
206 					(params.type =
207 					genimg_get_type_id (*++argv)) < 0)
208 					usage ();
209 				goto NXTARG;
210 
211 			case 'a':
212 				if (--argc <= 0)
213 					usage ();
214 				params.addr = strtoul (*++argv, &ptr, 16);
215 				if (*ptr) {
216 					fprintf (stderr,
217 						"%s: invalid load address %s\n",
218 						params.cmdname, *argv);
219 					exit (EXIT_FAILURE);
220 				}
221 				goto NXTARG;
222 			case 'd':
223 				if (--argc <= 0)
224 					usage ();
225 				params.datafile = *++argv;
226 				params.dflag = 1;
227 				goto NXTARG;
228 			case 'e':
229 				if (--argc <= 0)
230 					usage ();
231 				params.ep = strtoul (*++argv, &ptr, 16);
232 				if (*ptr) {
233 					fprintf (stderr,
234 						"%s: invalid entry point %s\n",
235 						params.cmdname, *argv);
236 					exit (EXIT_FAILURE);
237 				}
238 				params.eflag = 1;
239 				goto NXTARG;
240 			case 'f':
241 				if (--argc <= 0)
242 					usage ();
243 				/*
244 				 * The flattened image tree (FIT) format
245 				 * requires a flattened device tree image type
246 				 */
247 				params.type = IH_TYPE_FLATDT;
248 				params.datafile = *++argv;
249 				params.fflag = 1;
250 				goto NXTARG;
251 			case 'n':
252 				if (--argc <= 0)
253 					usage ();
254 				params.imagename = *++argv;
255 				goto NXTARG;
256 			case 'R':
257 				if (--argc <= 0)
258 					usage();
259 				/*
260 				 * This entry is for the second configuration
261 				 * file, if only one is not enough.
262 				 */
263 				params.imagename2 = *++argv;
264 				goto NXTARG;
265 			case 's':
266 				params.skipcpy = 1;
267 				break;
268 			case 'v':
269 				params.vflag++;
270 				break;
271 			case 'V':
272 				printf("mkimage version %s\n", PLAIN_VERSION);
273 				exit(EXIT_SUCCESS);
274 			case 'x':
275 				params.xflag++;
276 				break;
277 			default:
278 				usage ();
279 			}
280 		}
281 NXTARG:		;
282 	}
283 
284 	if (argc != 1)
285 		usage ();
286 
287 	/* set tparams as per input type_id */
288 	tparams = mkimage_get_type(params.type);
289 	if (tparams == NULL) {
290 		fprintf (stderr, "%s: unsupported type %s\n",
291 			params.cmdname, genimg_get_type_name(params.type));
292 		exit (EXIT_FAILURE);
293 	}
294 
295 	/*
296 	 * check the passed arguments parameters meets the requirements
297 	 * as per image type to be generated/listed
298 	 */
299 	if (tparams->check_params)
300 		if (tparams->check_params (&params))
301 			usage ();
302 
303 	if (!params.eflag) {
304 		params.ep = params.addr;
305 		/* If XIP, entry point must be after the U-Boot header */
306 		if (params.xflag)
307 			params.ep += tparams->header_size;
308 	}
309 
310 	params.imagefile = *argv;
311 
312 	if (params.fflag){
313 		if (tparams->fflag_handle)
314 			/*
315 			 * in some cases, some additional processing needs
316 			 * to be done if fflag is defined
317 			 *
318 			 * For ex. fit_handle_file for Fit file support
319 			 */
320 			retval = tparams->fflag_handle(&params);
321 
322 		if (retval != EXIT_SUCCESS)
323 			exit (retval);
324 	}
325 
326 	if (params.lflag || params.fflag) {
327 		ifd = open (params.imagefile, O_RDONLY|O_BINARY);
328 	} else {
329 		ifd = open (params.imagefile,
330 			O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
331 	}
332 
333 	if (ifd < 0) {
334 		fprintf (stderr, "%s: Can't open %s: %s\n",
335 			params.cmdname, params.imagefile,
336 			strerror(errno));
337 		exit (EXIT_FAILURE);
338 	}
339 
340 	if (params.lflag || params.fflag) {
341 		/*
342 		 * list header information of existing image
343 		 */
344 		if (fstat(ifd, &sbuf) < 0) {
345 			fprintf (stderr, "%s: Can't stat %s: %s\n",
346 				params.cmdname, params.imagefile,
347 				strerror(errno));
348 			exit (EXIT_FAILURE);
349 		}
350 
351 		if ((unsigned)sbuf.st_size < tparams->header_size) {
352 			fprintf (stderr,
353 				"%s: Bad size: \"%s\" is not valid image\n",
354 				params.cmdname, params.imagefile);
355 			exit (EXIT_FAILURE);
356 		}
357 
358 		ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
359 		if (ptr == MAP_FAILED) {
360 			fprintf (stderr, "%s: Can't read %s: %s\n",
361 				params.cmdname, params.imagefile,
362 				strerror(errno));
363 			exit (EXIT_FAILURE);
364 		}
365 
366 		/*
367 		 * scan through mkimage registry for all supported image types
368 		 * and verify the input image file header for match
369 		 * Print the image information for matched image type
370 		 * Returns the error code if not matched
371 		 */
372 		retval = mkimage_verify_print_header (ptr, &sbuf);
373 
374 		(void) munmap((void *)ptr, sbuf.st_size);
375 		(void) close (ifd);
376 
377 		exit (retval);
378 	}
379 
380 	/*
381 	 * In case there an header with a variable
382 	 * length will be added, the corresponding
383 	 * function is called. This is responsible to
384 	 * allocate memory for the header itself.
385 	 */
386 	if (tparams->vrec_header)
387 		tparams->vrec_header(&params, tparams);
388 	else
389 		memset(tparams->hdr, 0, tparams->header_size);
390 
391 	if (write(ifd, tparams->hdr, tparams->header_size)
392 					!= tparams->header_size) {
393 		fprintf (stderr, "%s: Write error on %s: %s\n",
394 			params.cmdname, params.imagefile, strerror(errno));
395 		exit (EXIT_FAILURE);
396 	}
397 
398 	if (!params.skipcpy) {
399 		if (params.type == IH_TYPE_MULTI ||
400 		    params.type == IH_TYPE_SCRIPT) {
401 			char *file = params.datafile;
402 			uint32_t size;
403 
404 			for (;;) {
405 				char *sep = NULL;
406 
407 				if (file) {
408 					if ((sep = strchr(file, ':')) != NULL) {
409 						*sep = '\0';
410 					}
411 
412 					if (stat (file, &sbuf) < 0) {
413 						fprintf (stderr, "%s: Can't stat %s: %s\n",
414 							 params.cmdname, file, strerror(errno));
415 						exit (EXIT_FAILURE);
416 					}
417 					size = cpu_to_uimage (sbuf.st_size);
418 				} else {
419 					size = 0;
420 				}
421 
422 				if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
423 					fprintf (stderr, "%s: Write error on %s: %s\n",
424 						 params.cmdname, params.imagefile,
425 						 strerror(errno));
426 					exit (EXIT_FAILURE);
427 				}
428 
429 				if (!file) {
430 					break;
431 				}
432 
433 				if (sep) {
434 					*sep = ':';
435 					file = sep + 1;
436 				} else {
437 					file = NULL;
438 				}
439 			}
440 
441 			file = params.datafile;
442 
443 			for (;;) {
444 				char *sep = strchr(file, ':');
445 				if (sep) {
446 					*sep = '\0';
447 					copy_file (ifd, file, 1);
448 					*sep++ = ':';
449 					file = sep;
450 				} else {
451 					copy_file (ifd, file, 0);
452 					break;
453 				}
454 			}
455 		} else if (params.type == IH_TYPE_PBLIMAGE) {
456 			/* PBL has special Image format, implements its' own */
457 			pbl_load_uboot(ifd, &params);
458 		} else {
459 			copy_file (ifd, params.datafile, 0);
460 		}
461 	}
462 
463 	/* We're a bit of paranoid */
464 #if defined(_POSIX_SYNCHRONIZED_IO) && \
465    !defined(__sun__) && \
466    !defined(__FreeBSD__) && \
467    !defined(__APPLE__)
468 	(void) fdatasync (ifd);
469 #else
470 	(void) fsync (ifd);
471 #endif
472 
473 	if (fstat(ifd, &sbuf) < 0) {
474 		fprintf (stderr, "%s: Can't stat %s: %s\n",
475 			params.cmdname, params.imagefile, strerror(errno));
476 		exit (EXIT_FAILURE);
477 	}
478 
479 	ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
480 	if (ptr == MAP_FAILED) {
481 		fprintf (stderr, "%s: Can't map %s: %s\n",
482 			params.cmdname, params.imagefile, strerror(errno));
483 		exit (EXIT_FAILURE);
484 	}
485 
486 	/* Setup the image header as per input image type*/
487 	if (tparams->set_header)
488 		tparams->set_header (ptr, &sbuf, ifd, &params);
489 	else {
490 		fprintf (stderr, "%s: Can't set header for %s: %s\n",
491 			params.cmdname, tparams->name, strerror(errno));
492 		exit (EXIT_FAILURE);
493 	}
494 
495 	/* Print the image information by processing image header */
496 	if (tparams->print_header)
497 		tparams->print_header (ptr);
498 	else {
499 		fprintf (stderr, "%s: Can't print header for %s: %s\n",
500 			params.cmdname, tparams->name, strerror(errno));
501 		exit (EXIT_FAILURE);
502 	}
503 
504 	(void) munmap((void *)ptr, sbuf.st_size);
505 
506 	/* We're a bit of paranoid */
507 #if defined(_POSIX_SYNCHRONIZED_IO) && \
508    !defined(__sun__) && \
509    !defined(__FreeBSD__) && \
510    !defined(__APPLE__)
511 	(void) fdatasync (ifd);
512 #else
513 	(void) fsync (ifd);
514 #endif
515 
516 	if (close(ifd)) {
517 		fprintf (stderr, "%s: Write error on %s: %s\n",
518 			params.cmdname, params.imagefile, strerror(errno));
519 		exit (EXIT_FAILURE);
520 	}
521 
522 	exit (EXIT_SUCCESS);
523 }
524 
525 static void
526 copy_file (int ifd, const char *datafile, int pad)
527 {
528 	int dfd;
529 	struct stat sbuf;
530 	unsigned char *ptr;
531 	int tail;
532 	int zero = 0;
533 	int offset = 0;
534 	int size;
535 	struct image_type_params *tparams = mkimage_get_type (params.type);
536 
537 	if (params.vflag) {
538 		fprintf (stderr, "Adding Image %s\n", datafile);
539 	}
540 
541 	if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
542 		fprintf (stderr, "%s: Can't open %s: %s\n",
543 			params.cmdname, datafile, strerror(errno));
544 		exit (EXIT_FAILURE);
545 	}
546 
547 	if (fstat(dfd, &sbuf) < 0) {
548 		fprintf (stderr, "%s: Can't stat %s: %s\n",
549 			params.cmdname, datafile, strerror(errno));
550 		exit (EXIT_FAILURE);
551 	}
552 
553 	ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
554 	if (ptr == MAP_FAILED) {
555 		fprintf (stderr, "%s: Can't read %s: %s\n",
556 			params.cmdname, datafile, strerror(errno));
557 		exit (EXIT_FAILURE);
558 	}
559 
560 	if (params.xflag) {
561 		unsigned char *p = NULL;
562 		/*
563 		 * XIP: do not append the image_header_t at the
564 		 * beginning of the file, but consume the space
565 		 * reserved for it.
566 		 */
567 
568 		if ((unsigned)sbuf.st_size < tparams->header_size) {
569 			fprintf (stderr,
570 				"%s: Bad size: \"%s\" is too small for XIP\n",
571 				params.cmdname, datafile);
572 			exit (EXIT_FAILURE);
573 		}
574 
575 		for (p = ptr; p < ptr + tparams->header_size; p++) {
576 			if ( *p != 0xff ) {
577 				fprintf (stderr,
578 					"%s: Bad file: \"%s\" has invalid buffer for XIP\n",
579 					params.cmdname, datafile);
580 				exit (EXIT_FAILURE);
581 			}
582 		}
583 
584 		offset = tparams->header_size;
585 	}
586 
587 	size = sbuf.st_size - offset;
588 	if (write(ifd, ptr + offset, size) != size) {
589 		fprintf (stderr, "%s: Write error on %s: %s\n",
590 			params.cmdname, params.imagefile, strerror(errno));
591 		exit (EXIT_FAILURE);
592 	}
593 
594 	if (pad && ((tail = size % 4) != 0)) {
595 
596 		if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
597 			fprintf (stderr, "%s: Write error on %s: %s\n",
598 				params.cmdname, params.imagefile,
599 				strerror(errno));
600 			exit (EXIT_FAILURE);
601 		}
602 	}
603 
604 	(void) munmap((void *)ptr, sbuf.st_size);
605 	(void) close (dfd);
606 }
607 
608 void
609 usage ()
610 {
611 	fprintf (stderr, "Usage: %s -l image\n"
612 			 "          -l ==> list image header information\n",
613 		params.cmdname);
614 	fprintf (stderr, "       %s [-x] -A arch -O os -T type -C comp "
615 			 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
616 			 "          -A ==> set architecture to 'arch'\n"
617 			 "          -O ==> set operating system to 'os'\n"
618 			 "          -T ==> set image type to 'type'\n"
619 			 "          -C ==> set compression type 'comp'\n"
620 			 "          -a ==> set load address to 'addr' (hex)\n"
621 			 "          -e ==> set entry point to 'ep' (hex)\n"
622 			 "          -n ==> set image name to 'name'\n"
623 			 "          -d ==> use image data from 'datafile'\n"
624 			 "          -x ==> set XIP (execute in place)\n",
625 		params.cmdname);
626 	fprintf (stderr, "       %s [-D dtc_options] -f fit-image.its fit-image\n",
627 		params.cmdname);
628 	fprintf (stderr, "       %s -V ==> print version information and exit\n",
629 		params.cmdname);
630 
631 	exit (EXIT_FAILURE);
632 }
633