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