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