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