2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2009
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
8 * SPDX-License-Identifier: GPL-2.0+
15 static void copy_file(int, const char *, int);
16 static void usage(void);
18 /* image_type_params link list to maintain registered image type supports */
19 struct image_type_params *mkimage_tparams = NULL;
21 /* parameters initialized by core will be used by the image type code */
22 struct image_tool_params params = {
25 .type = IH_TYPE_KERNEL,
27 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
33 main (int argc, char **argv)
39 struct image_type_params *tparams = NULL;
42 params.cmdname = *argv;
43 params.addr = params.ep = 0;
45 while (--argc > 0 && **++argv == '-') {
54 genimg_get_arch_id (*++argv)) < 0)
60 params.comment = *++argv;
65 genimg_get_comp_id (*++argv)) < 0)
77 genimg_get_os_id (*++argv)) < 0)
83 genimg_get_type_id (*++argv)) < 0)
90 params.addr = strtoul (*++argv, &ptr, 16);
93 "%s: invalid load address %s\n",
94 params.cmdname, *argv);
101 params.datafile = *++argv;
107 params.ep = strtoul (*++argv, &ptr, 16);
110 "%s: invalid entry point %s\n",
111 params.cmdname, *argv);
119 params.datafile = *++argv;
123 * The flattened image tree (FIT) format
124 * requires a flattened device tree image type
126 params.type = IH_TYPE_FLATDT;
132 params.keydir = *++argv;
137 params.keydest = *++argv;
142 params.imagename = *++argv;
145 params.require_keys = 1;
151 * This entry is for the second configuration
152 * file, if only one is not enough.
154 params.imagename2 = *++argv;
163 printf("mkimage version %s\n", PLAIN_VERSION);
178 /* set tparams as per input type_id */
179 tparams = imagetool_get_type(params.type);
180 if (tparams == NULL) {
181 fprintf (stderr, "%s: unsupported type %s\n",
182 params.cmdname, genimg_get_type_name(params.type));
187 * check the passed arguments parameters meets the requirements
188 * as per image type to be generated/listed
190 if (tparams->check_params)
191 if (tparams->check_params (¶ms))
195 params.ep = params.addr;
196 /* If XIP, entry point must be after the U-Boot header */
198 params.ep += tparams->header_size;
201 params.imagefile = *argv;
204 if (tparams->fflag_handle)
206 * in some cases, some additional processing needs
207 * to be done if fflag is defined
209 * For ex. fit_handle_file for Fit file support
211 retval = tparams->fflag_handle(¶ms);
213 if (retval != EXIT_SUCCESS)
217 if (params.lflag || params.fflag) {
218 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
220 ifd = open (params.imagefile,
221 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
225 fprintf (stderr, "%s: Can't open %s: %s\n",
226 params.cmdname, params.imagefile,
231 if (params.lflag || params.fflag) {
233 * list header information of existing image
235 if (fstat(ifd, &sbuf) < 0) {
236 fprintf (stderr, "%s: Can't stat %s: %s\n",
237 params.cmdname, params.imagefile,
242 if ((unsigned)sbuf.st_size < tparams->header_size) {
244 "%s: Bad size: \"%s\" is not valid image\n",
245 params.cmdname, params.imagefile);
249 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
250 if (ptr == MAP_FAILED) {
251 fprintf (stderr, "%s: Can't read %s: %s\n",
252 params.cmdname, params.imagefile,
258 * scan through mkimage registry for all supported image types
259 * and verify the input image file header for match
260 * Print the image information for matched image type
261 * Returns the error code if not matched
263 retval = imagetool_verify_print_header(ptr, &sbuf,
266 (void) munmap((void *)ptr, sbuf.st_size);
273 * In case there an header with a variable
274 * length will be added, the corresponding
275 * function is called. This is responsible to
276 * allocate memory for the header itself.
278 if (tparams->vrec_header)
279 pad_len = tparams->vrec_header(¶ms, tparams);
281 memset(tparams->hdr, 0, tparams->header_size);
283 if (write(ifd, tparams->hdr, tparams->header_size)
284 != tparams->header_size) {
285 fprintf (stderr, "%s: Write error on %s: %s\n",
286 params.cmdname, params.imagefile, strerror(errno));
290 if (!params.skipcpy) {
291 if (params.type == IH_TYPE_MULTI ||
292 params.type == IH_TYPE_SCRIPT) {
293 char *file = params.datafile;
300 if ((sep = strchr(file, ':')) != NULL) {
304 if (stat (file, &sbuf) < 0) {
305 fprintf (stderr, "%s: Can't stat %s: %s\n",
306 params.cmdname, file, strerror(errno));
309 size = cpu_to_uimage (sbuf.st_size);
314 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
315 fprintf (stderr, "%s: Write error on %s: %s\n",
316 params.cmdname, params.imagefile,
333 file = params.datafile;
336 char *sep = strchr(file, ':');
339 copy_file (ifd, file, 1);
343 copy_file (ifd, file, 0);
347 } else if (params.type == IH_TYPE_PBLIMAGE) {
348 /* PBL has special Image format, implements its' own */
349 pbl_load_uboot(ifd, ¶ms);
351 copy_file(ifd, params.datafile, pad_len);
355 /* We're a bit of paranoid */
356 #if defined(_POSIX_SYNCHRONIZED_IO) && \
357 !defined(__sun__) && \
358 !defined(__FreeBSD__) && \
359 !defined(__OpenBSD__) && \
361 (void) fdatasync (ifd);
366 if (fstat(ifd, &sbuf) < 0) {
367 fprintf (stderr, "%s: Can't stat %s: %s\n",
368 params.cmdname, params.imagefile, strerror(errno));
372 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
373 if (ptr == MAP_FAILED) {
374 fprintf (stderr, "%s: Can't map %s: %s\n",
375 params.cmdname, params.imagefile, strerror(errno));
379 /* Setup the image header as per input image type*/
380 if (tparams->set_header)
381 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
383 fprintf (stderr, "%s: Can't set header for %s: %s\n",
384 params.cmdname, tparams->name, strerror(errno));
388 /* Print the image information by processing image header */
389 if (tparams->print_header)
390 tparams->print_header (ptr);
392 fprintf (stderr, "%s: Can't print header for %s: %s\n",
393 params.cmdname, tparams->name, strerror(errno));
397 (void) munmap((void *)ptr, sbuf.st_size);
399 /* We're a bit of paranoid */
400 #if defined(_POSIX_SYNCHRONIZED_IO) && \
401 !defined(__sun__) && \
402 !defined(__FreeBSD__) && \
403 !defined(__OpenBSD__) && \
405 (void) fdatasync (ifd);
411 fprintf (stderr, "%s: Write error on %s: %s\n",
412 params.cmdname, params.imagefile, strerror(errno));
420 copy_file (int ifd, const char *datafile, int pad)
430 struct image_type_params *tparams = imagetool_get_type(params.type);
432 if (pad >= sizeof(zeros)) {
433 fprintf(stderr, "%s: Can't pad to %d\n",
434 params.cmdname, pad);
438 memset(zeros, 0, sizeof(zeros));
441 fprintf (stderr, "Adding Image %s\n", datafile);
444 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
445 fprintf (stderr, "%s: Can't open %s: %s\n",
446 params.cmdname, datafile, strerror(errno));
450 if (fstat(dfd, &sbuf) < 0) {
451 fprintf (stderr, "%s: Can't stat %s: %s\n",
452 params.cmdname, datafile, strerror(errno));
456 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
457 if (ptr == MAP_FAILED) {
458 fprintf (stderr, "%s: Can't read %s: %s\n",
459 params.cmdname, datafile, strerror(errno));
464 unsigned char *p = NULL;
466 * XIP: do not append the image_header_t at the
467 * beginning of the file, but consume the space
471 if ((unsigned)sbuf.st_size < tparams->header_size) {
473 "%s: Bad size: \"%s\" is too small for XIP\n",
474 params.cmdname, datafile);
478 for (p = ptr; p < ptr + tparams->header_size; p++) {
481 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
482 params.cmdname, datafile);
487 offset = tparams->header_size;
490 size = sbuf.st_size - offset;
491 if (write(ifd, ptr + offset, size) != size) {
492 fprintf (stderr, "%s: Write error on %s: %s\n",
493 params.cmdname, params.imagefile, strerror(errno));
498 if ((pad == 1) && (tail != 0)) {
500 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
501 fprintf (stderr, "%s: Write error on %s: %s\n",
502 params.cmdname, params.imagefile,
506 } else if (pad > 1) {
507 if (write(ifd, (char *)&zeros, pad) != pad) {
508 fprintf(stderr, "%s: Write error on %s: %s\n",
509 params.cmdname, params.imagefile,
515 (void) munmap((void *)ptr, sbuf.st_size);
519 static void usage(void)
521 fprintf (stderr, "Usage: %s -l image\n"
522 " -l ==> list image header information\n",
524 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
525 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
526 " -A ==> set architecture to 'arch'\n"
527 " -O ==> set operating system to 'os'\n"
528 " -T ==> set image type to 'type'\n"
529 " -C ==> set compression type 'comp'\n"
530 " -a ==> set load address to 'addr' (hex)\n"
531 " -e ==> set entry point to 'ep' (hex)\n"
532 " -n ==> set image name to 'name'\n"
533 " -d ==> use image data from 'datafile'\n"
534 " -x ==> set XIP (execute in place)\n",
536 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
538 fprintf(stderr, " -D => set options for device tree compiler\n"
539 " -f => input filename for FIT source\n");
540 #ifdef CONFIG_FIT_SIGNATURE
541 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
542 " -k => set directory containing private keys\n"
543 " -K => write public keys to this .dtb file\n"
544 " -c => add comment in signature node\n"
545 " -F => re-sign existing FIT image\n"
546 " -r => mark keys used as 'required' in dtb\n");
548 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
550 fprintf (stderr, " %s -V ==> print version information and exit\n",