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