2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2009
5 * DENX Software Engineering
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,
35 * It is used to register respective image generation/list support to the
38 * the input struct image_type_params is checked and appended to the link
39 * list, if the input structure is already registered, error
41 void mkimage_register (struct image_type_params *tparams)
43 struct image_type_params **tp;
46 fprintf (stderr, "%s: %s: Null input\n",
47 params.cmdname, __FUNCTION__);
51 /* scan the linked list, check for registry and point the last one */
52 for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) {
53 if (!strcmp((*tp)->name, tparams->name)) {
54 fprintf (stderr, "%s: %s already registered\n",
55 params.cmdname, tparams->name);
60 /* add input struct entry at the end of link list */
62 /* mark input entry as last entry in the link list */
65 debug ("Registered %s\n", tparams->name);
69 main (int argc, char **argv)
75 struct image_type_params *tparams = NULL;
78 /* Init all image generation/list support */
79 register_image_tool(mkimage_register);
81 params.cmdname = *argv;
82 params.addr = params.ep = 0;
84 while (--argc > 0 && **++argv == '-') {
93 genimg_get_arch_id (*++argv)) < 0)
99 params.comment = *++argv;
104 genimg_get_comp_id (*++argv)) < 0)
110 params.dtc = *++argv;
116 genimg_get_os_id (*++argv)) < 0)
122 genimg_get_type_id (*++argv)) < 0)
129 params.addr = strtoul (*++argv, &ptr, 16);
132 "%s: invalid load address %s\n",
133 params.cmdname, *argv);
140 params.datafile = *++argv;
146 params.ep = strtoul (*++argv, &ptr, 16);
149 "%s: invalid entry point %s\n",
150 params.cmdname, *argv);
158 params.datafile = *++argv;
162 * The flattened image tree (FIT) format
163 * requires a flattened device tree image type
165 params.type = IH_TYPE_FLATDT;
171 params.keydir = *++argv;
176 params.keydest = *++argv;
181 params.imagename = *++argv;
184 params.require_keys = 1;
190 * This entry is for the second configuration
191 * file, if only one is not enough.
193 params.imagename2 = *++argv;
202 printf("mkimage version %s\n", PLAIN_VERSION);
217 /* set tparams as per input type_id */
218 tparams = imagetool_get_type(params.type, mkimage_tparams);
219 if (tparams == NULL) {
220 fprintf (stderr, "%s: unsupported type %s\n",
221 params.cmdname, genimg_get_type_name(params.type));
226 * check the passed arguments parameters meets the requirements
227 * as per image type to be generated/listed
229 if (tparams->check_params)
230 if (tparams->check_params (¶ms))
234 params.ep = params.addr;
235 /* If XIP, entry point must be after the U-Boot header */
237 params.ep += tparams->header_size;
240 params.imagefile = *argv;
243 if (tparams->fflag_handle)
245 * in some cases, some additional processing needs
246 * to be done if fflag is defined
248 * For ex. fit_handle_file for Fit file support
250 retval = tparams->fflag_handle(¶ms);
252 if (retval != EXIT_SUCCESS)
256 if (params.lflag || params.fflag) {
257 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
259 ifd = open (params.imagefile,
260 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
264 fprintf (stderr, "%s: Can't open %s: %s\n",
265 params.cmdname, params.imagefile,
270 if (params.lflag || params.fflag) {
272 * list header information of existing image
274 if (fstat(ifd, &sbuf) < 0) {
275 fprintf (stderr, "%s: Can't stat %s: %s\n",
276 params.cmdname, params.imagefile,
281 if ((unsigned)sbuf.st_size < tparams->header_size) {
283 "%s: Bad size: \"%s\" is not valid image\n",
284 params.cmdname, params.imagefile);
288 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
289 if (ptr == MAP_FAILED) {
290 fprintf (stderr, "%s: Can't read %s: %s\n",
291 params.cmdname, params.imagefile,
297 * scan through mkimage registry for all supported image types
298 * and verify the input image file header for match
299 * Print the image information for matched image type
300 * Returns the error code if not matched
302 retval = imagetool_verify_print_header(ptr, &sbuf,
305 (void) munmap((void *)ptr, sbuf.st_size);
312 * In case there an header with a variable
313 * length will be added, the corresponding
314 * function is called. This is responsible to
315 * allocate memory for the header itself.
317 if (tparams->vrec_header)
318 pad_len = tparams->vrec_header(¶ms, tparams);
320 memset(tparams->hdr, 0, tparams->header_size);
322 if (write(ifd, tparams->hdr, tparams->header_size)
323 != tparams->header_size) {
324 fprintf (stderr, "%s: Write error on %s: %s\n",
325 params.cmdname, params.imagefile, strerror(errno));
329 if (!params.skipcpy) {
330 if (params.type == IH_TYPE_MULTI ||
331 params.type == IH_TYPE_SCRIPT) {
332 char *file = params.datafile;
339 if ((sep = strchr(file, ':')) != NULL) {
343 if (stat (file, &sbuf) < 0) {
344 fprintf (stderr, "%s: Can't stat %s: %s\n",
345 params.cmdname, file, strerror(errno));
348 size = cpu_to_uimage (sbuf.st_size);
353 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
354 fprintf (stderr, "%s: Write error on %s: %s\n",
355 params.cmdname, params.imagefile,
372 file = params.datafile;
375 char *sep = strchr(file, ':');
378 copy_file (ifd, file, 1);
382 copy_file (ifd, file, 0);
386 } else if (params.type == IH_TYPE_PBLIMAGE) {
387 /* PBL has special Image format, implements its' own */
388 pbl_load_uboot(ifd, ¶ms);
390 copy_file(ifd, params.datafile, pad_len);
394 /* We're a bit of paranoid */
395 #if defined(_POSIX_SYNCHRONIZED_IO) && \
396 !defined(__sun__) && \
397 !defined(__FreeBSD__) && \
398 !defined(__OpenBSD__) && \
400 (void) fdatasync (ifd);
405 if (fstat(ifd, &sbuf) < 0) {
406 fprintf (stderr, "%s: Can't stat %s: %s\n",
407 params.cmdname, params.imagefile, strerror(errno));
411 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
412 if (ptr == MAP_FAILED) {
413 fprintf (stderr, "%s: Can't map %s: %s\n",
414 params.cmdname, params.imagefile, strerror(errno));
418 /* Setup the image header as per input image type*/
419 if (tparams->set_header)
420 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
422 fprintf (stderr, "%s: Can't set header for %s: %s\n",
423 params.cmdname, tparams->name, strerror(errno));
427 /* Print the image information by processing image header */
428 if (tparams->print_header)
429 tparams->print_header (ptr);
431 fprintf (stderr, "%s: Can't print header for %s: %s\n",
432 params.cmdname, tparams->name, strerror(errno));
436 (void) munmap((void *)ptr, sbuf.st_size);
438 /* We're a bit of paranoid */
439 #if defined(_POSIX_SYNCHRONIZED_IO) && \
440 !defined(__sun__) && \
441 !defined(__FreeBSD__) && \
442 !defined(__OpenBSD__) && \
444 (void) fdatasync (ifd);
450 fprintf (stderr, "%s: Write error on %s: %s\n",
451 params.cmdname, params.imagefile, strerror(errno));
459 copy_file (int ifd, const char *datafile, int pad)
469 struct image_type_params *tparams = imagetool_get_type(params.type,
472 if (pad >= sizeof(zeros)) {
473 fprintf(stderr, "%s: Can't pad to %d\n",
474 params.cmdname, pad);
478 memset(zeros, 0, sizeof(zeros));
481 fprintf (stderr, "Adding Image %s\n", datafile);
484 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
485 fprintf (stderr, "%s: Can't open %s: %s\n",
486 params.cmdname, datafile, strerror(errno));
490 if (fstat(dfd, &sbuf) < 0) {
491 fprintf (stderr, "%s: Can't stat %s: %s\n",
492 params.cmdname, datafile, strerror(errno));
496 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
497 if (ptr == MAP_FAILED) {
498 fprintf (stderr, "%s: Can't read %s: %s\n",
499 params.cmdname, datafile, strerror(errno));
504 unsigned char *p = NULL;
506 * XIP: do not append the image_header_t at the
507 * beginning of the file, but consume the space
511 if ((unsigned)sbuf.st_size < tparams->header_size) {
513 "%s: Bad size: \"%s\" is too small for XIP\n",
514 params.cmdname, datafile);
518 for (p = ptr; p < ptr + tparams->header_size; p++) {
521 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
522 params.cmdname, datafile);
527 offset = tparams->header_size;
530 size = sbuf.st_size - offset;
531 if (write(ifd, ptr + offset, size) != size) {
532 fprintf (stderr, "%s: Write error on %s: %s\n",
533 params.cmdname, params.imagefile, strerror(errno));
538 if ((pad == 1) && (tail != 0)) {
540 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
541 fprintf (stderr, "%s: Write error on %s: %s\n",
542 params.cmdname, params.imagefile,
546 } else if (pad > 1) {
547 if (write(ifd, (char *)&zeros, pad) != pad) {
548 fprintf(stderr, "%s: Write error on %s: %s\n",
549 params.cmdname, params.imagefile,
555 (void) munmap((void *)ptr, sbuf.st_size);
559 static void usage(void)
561 fprintf (stderr, "Usage: %s -l image\n"
562 " -l ==> list image header information\n",
564 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
565 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
566 " -A ==> set architecture to 'arch'\n"
567 " -O ==> set operating system to 'os'\n"
568 " -T ==> set image type to 'type'\n"
569 " -C ==> set compression type 'comp'\n"
570 " -a ==> set load address to 'addr' (hex)\n"
571 " -e ==> set entry point to 'ep' (hex)\n"
572 " -n ==> set image name to 'name'\n"
573 " -d ==> use image data from 'datafile'\n"
574 " -x ==> set XIP (execute in place)\n",
576 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
578 fprintf(stderr, " -D => set options for device tree compiler\n"
579 " -f => input filename for FIT source\n");
580 #ifdef CONFIG_FIT_SIGNATURE
581 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
582 " -k => set directory containing private keys\n"
583 " -K => write public keys to this .dtb file\n"
584 " -c => add comment in signature node\n"
585 " -F => re-sign existing FIT image\n"
586 " -r => mark keys used as 'required' in dtb\n");
588 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
590 fprintf (stderr, " %s -V ==> print version information and exit\n",