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 /* parameters initialized by core will be used by the image type code */
19 struct image_tool_params params = {
22 .type = IH_TYPE_KERNEL,
24 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
30 main (int argc, char **argv)
36 struct image_type_params *tparams = NULL;
39 params.cmdname = *argv;
40 params.addr = params.ep = 0;
42 while (--argc > 0 && **++argv == '-') {
51 genimg_get_arch_id (*++argv)) < 0)
57 params.comment = *++argv;
62 genimg_get_comp_id (*++argv)) < 0)
74 genimg_get_os_id (*++argv)) < 0)
80 genimg_get_type_id (*++argv)) < 0)
87 params.addr = strtoul (*++argv, &ptr, 16);
90 "%s: invalid load address %s\n",
91 params.cmdname, *argv);
98 params.datafile = *++argv;
104 params.ep = strtoul (*++argv, &ptr, 16);
107 "%s: invalid entry point %s\n",
108 params.cmdname, *argv);
116 params.datafile = *++argv;
120 * The flattened image tree (FIT) format
121 * requires a flattened device tree image type
123 params.type = IH_TYPE_FLATDT;
129 params.keydir = *++argv;
134 params.keydest = *++argv;
139 params.imagename = *++argv;
142 params.require_keys = 1;
148 * This entry is for the second configuration
149 * file, if only one is not enough.
151 params.imagename2 = *++argv;
160 printf("mkimage version %s\n", PLAIN_VERSION);
175 /* set tparams as per input type_id */
176 tparams = imagetool_get_type(params.type);
177 if (tparams == NULL) {
178 fprintf (stderr, "%s: unsupported type %s\n",
179 params.cmdname, genimg_get_type_name(params.type));
184 * check the passed arguments parameters meets the requirements
185 * as per image type to be generated/listed
187 if (tparams->check_params)
188 if (tparams->check_params (¶ms))
192 params.ep = params.addr;
193 /* If XIP, entry point must be after the U-Boot header */
195 params.ep += tparams->header_size;
198 params.imagefile = *argv;
201 if (tparams->fflag_handle)
203 * in some cases, some additional processing needs
204 * to be done if fflag is defined
206 * For ex. fit_handle_file for Fit file support
208 retval = tparams->fflag_handle(¶ms);
210 if (retval != EXIT_SUCCESS)
214 if (params.lflag || params.fflag) {
215 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
217 ifd = open (params.imagefile,
218 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
222 fprintf (stderr, "%s: Can't open %s: %s\n",
223 params.cmdname, params.imagefile,
228 if (params.lflag || params.fflag) {
230 * list header information of existing image
232 if (fstat(ifd, &sbuf) < 0) {
233 fprintf (stderr, "%s: Can't stat %s: %s\n",
234 params.cmdname, params.imagefile,
239 if ((unsigned)sbuf.st_size < tparams->header_size) {
241 "%s: Bad size: \"%s\" is not valid image\n",
242 params.cmdname, params.imagefile);
246 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
247 if (ptr == MAP_FAILED) {
248 fprintf (stderr, "%s: Can't read %s: %s\n",
249 params.cmdname, params.imagefile,
255 * scan through mkimage registry for all supported image types
256 * and verify the input image file header for match
257 * Print the image information for matched image type
258 * Returns the error code if not matched
260 retval = imagetool_verify_print_header(ptr, &sbuf,
263 (void) munmap((void *)ptr, sbuf.st_size);
270 * In case there an header with a variable
271 * length will be added, the corresponding
272 * function is called. This is responsible to
273 * allocate memory for the header itself.
275 if (tparams->vrec_header)
276 pad_len = tparams->vrec_header(¶ms, tparams);
278 memset(tparams->hdr, 0, tparams->header_size);
280 if (write(ifd, tparams->hdr, tparams->header_size)
281 != tparams->header_size) {
282 fprintf (stderr, "%s: Write error on %s: %s\n",
283 params.cmdname, params.imagefile, strerror(errno));
287 if (!params.skipcpy) {
288 if (params.type == IH_TYPE_MULTI ||
289 params.type == IH_TYPE_SCRIPT) {
290 char *file = params.datafile;
297 if ((sep = strchr(file, ':')) != NULL) {
301 if (stat (file, &sbuf) < 0) {
302 fprintf (stderr, "%s: Can't stat %s: %s\n",
303 params.cmdname, file, strerror(errno));
306 size = cpu_to_uimage (sbuf.st_size);
311 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
312 fprintf (stderr, "%s: Write error on %s: %s\n",
313 params.cmdname, params.imagefile,
330 file = params.datafile;
333 char *sep = strchr(file, ':');
336 copy_file (ifd, file, 1);
340 copy_file (ifd, file, 0);
344 } else if (params.type == IH_TYPE_PBLIMAGE) {
345 /* PBL has special Image format, implements its' own */
346 pbl_load_uboot(ifd, ¶ms);
348 copy_file(ifd, params.datafile, pad_len);
352 /* We're a bit of paranoid */
353 #if defined(_POSIX_SYNCHRONIZED_IO) && \
354 !defined(__sun__) && \
355 !defined(__FreeBSD__) && \
356 !defined(__OpenBSD__) && \
358 (void) fdatasync (ifd);
363 if (fstat(ifd, &sbuf) < 0) {
364 fprintf (stderr, "%s: Can't stat %s: %s\n",
365 params.cmdname, params.imagefile, strerror(errno));
369 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
370 if (ptr == MAP_FAILED) {
371 fprintf (stderr, "%s: Can't map %s: %s\n",
372 params.cmdname, params.imagefile, strerror(errno));
376 /* Setup the image header as per input image type*/
377 if (tparams->set_header)
378 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
380 fprintf (stderr, "%s: Can't set header for %s: %s\n",
381 params.cmdname, tparams->name, strerror(errno));
385 /* Print the image information by processing image header */
386 if (tparams->print_header)
387 tparams->print_header (ptr);
389 fprintf (stderr, "%s: Can't print header for %s: %s\n",
390 params.cmdname, tparams->name, strerror(errno));
394 (void) munmap((void *)ptr, sbuf.st_size);
396 /* We're a bit of paranoid */
397 #if defined(_POSIX_SYNCHRONIZED_IO) && \
398 !defined(__sun__) && \
399 !defined(__FreeBSD__) && \
400 !defined(__OpenBSD__) && \
402 (void) fdatasync (ifd);
408 fprintf (stderr, "%s: Write error on %s: %s\n",
409 params.cmdname, params.imagefile, strerror(errno));
417 copy_file (int ifd, const char *datafile, int pad)
427 struct image_type_params *tparams = imagetool_get_type(params.type);
429 if (pad >= sizeof(zeros)) {
430 fprintf(stderr, "%s: Can't pad to %d\n",
431 params.cmdname, pad);
435 memset(zeros, 0, sizeof(zeros));
438 fprintf (stderr, "Adding Image %s\n", datafile);
441 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
442 fprintf (stderr, "%s: Can't open %s: %s\n",
443 params.cmdname, datafile, strerror(errno));
447 if (fstat(dfd, &sbuf) < 0) {
448 fprintf (stderr, "%s: Can't stat %s: %s\n",
449 params.cmdname, datafile, strerror(errno));
453 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
454 if (ptr == MAP_FAILED) {
455 fprintf (stderr, "%s: Can't read %s: %s\n",
456 params.cmdname, datafile, strerror(errno));
461 unsigned char *p = NULL;
463 * XIP: do not append the image_header_t at the
464 * beginning of the file, but consume the space
468 if ((unsigned)sbuf.st_size < tparams->header_size) {
470 "%s: Bad size: \"%s\" is too small for XIP\n",
471 params.cmdname, datafile);
475 for (p = ptr; p < ptr + tparams->header_size; p++) {
478 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
479 params.cmdname, datafile);
484 offset = tparams->header_size;
487 size = sbuf.st_size - offset;
488 if (write(ifd, ptr + offset, size) != size) {
489 fprintf (stderr, "%s: Write error on %s: %s\n",
490 params.cmdname, params.imagefile, strerror(errno));
495 if ((pad == 1) && (tail != 0)) {
497 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
498 fprintf (stderr, "%s: Write error on %s: %s\n",
499 params.cmdname, params.imagefile,
503 } else if (pad > 1) {
504 if (write(ifd, (char *)&zeros, pad) != pad) {
505 fprintf(stderr, "%s: Write error on %s: %s\n",
506 params.cmdname, params.imagefile,
512 (void) munmap((void *)ptr, sbuf.st_size);
516 static void usage(void)
518 fprintf (stderr, "Usage: %s -l image\n"
519 " -l ==> list image header information\n",
521 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
522 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
523 " -A ==> set architecture to 'arch'\n"
524 " -O ==> set operating system to 'os'\n"
525 " -T ==> set image type to 'type'\n"
526 " -C ==> set compression type 'comp'\n"
527 " -a ==> set load address to 'addr' (hex)\n"
528 " -e ==> set entry point to 'ep' (hex)\n"
529 " -n ==> set image name to 'name'\n"
530 " -d ==> use image data from 'datafile'\n"
531 " -x ==> set XIP (execute in place)\n",
533 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
535 fprintf(stderr, " -D => set options for device tree compiler\n"
536 " -f => input filename for FIT source\n");
537 #ifdef CONFIG_FIT_SIGNATURE
538 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
539 " -k => set directory containing private keys\n"
540 " -K => write public keys to this .dtb file\n"
541 " -c => add comment in signature node\n"
542 " -F => re-sign existing FIT image\n"
543 " -r => mark keys used as 'required' in dtb\n");
545 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
547 fprintf (stderr, " %s -V ==> print version information and exit\n",