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