]> git.ipfire.org Git - thirdparty/u-boot.git/blame - tools/mkimage.c
mkimage: Move argument processing into its own function
[thirdparty/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
0b443dee 70static void process_args(int argc, char **argv)
5b1d7137 71{
a2513e27 72 char *ptr;
5b1d7137
WD
73
74 while (--argc > 0 && **++argv == '-') {
75 while (*++*argv) {
76 switch (**argv) {
77 case 'l':
89a4d6b1 78 params.lflag = 1;
5b1d7137
WD
79 break;
80 case 'A':
81 if ((--argc <= 0) ||
89a4d6b1
PW
82 (params.arch =
83 genimg_get_arch_id (*++argv)) < 0)
0b443dee 84 usage();
5b1d7137 85 goto NXTARG;
4f610427
SG
86 case 'c':
87 if (--argc <= 0)
88 usage();
89 params.comment = *++argv;
90 goto NXTARG;
5b1d7137
WD
91 case 'C':
92 if ((--argc <= 0) ||
89a4d6b1
PW
93 (params.comp =
94 genimg_get_comp_id (*++argv)) < 0)
0b443dee 95 usage();
5b1d7137 96 goto NXTARG;
9d25438f
BS
97 case 'D':
98 if (--argc <= 0)
0b443dee 99 usage();
89a4d6b1 100 params.dtc = *++argv;
9d25438f
BS
101 goto NXTARG;
102
5b1d7137
WD
103 case 'O':
104 if ((--argc <= 0) ||
89a4d6b1
PW
105 (params.os =
106 genimg_get_os_id (*++argv)) < 0)
0b443dee 107 usage();
5b1d7137
WD
108 goto NXTARG;
109 case 'T':
5b9d44df
SG
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 }
5b1d7137 119 goto NXTARG;
5b1d7137
WD
120 case 'a':
121 if (--argc <= 0)
0b443dee 122 usage();
c190fbd0 123 params.addr = strtoull(*++argv, &ptr, 16);
5b1d7137
WD
124 if (*ptr) {
125 fprintf (stderr,
126 "%s: invalid load address %s\n",
89a4d6b1 127 params.cmdname, *argv);
5b1d7137
WD
128 exit (EXIT_FAILURE);
129 }
130 goto NXTARG;
131 case 'd':
132 if (--argc <= 0)
0b443dee 133 usage();
89a4d6b1
PW
134 params.datafile = *++argv;
135 params.dflag = 1;
5b1d7137
WD
136 goto NXTARG;
137 case 'e':
138 if (--argc <= 0)
0b443dee 139 usage();
c190fbd0 140 params.ep = strtoull(*++argv, &ptr, 16);
5b1d7137
WD
141 if (*ptr) {
142 fprintf (stderr,
143 "%s: invalid entry point %s\n",
89a4d6b1 144 params.cmdname, *argv);
5b1d7137
WD
145 exit (EXIT_FAILURE);
146 }
89a4d6b1 147 params.eflag = 1;
5b1d7137 148 goto NXTARG;
9d25438f
BS
149 case 'f':
150 if (--argc <= 0)
0b443dee 151 usage();
95d77b44
SG
152 params.datafile = *++argv;
153 /* no break */
154 case 'F':
1a99de2c
PT
155 /*
156 * The flattened image tree (FIT) format
157 * requires a flattened device tree image type
158 */
159 params.type = IH_TYPE_FLATDT;
fbc1c8f6 160 params.fflag = 1;
9d25438f 161 goto NXTARG;
80e4df8a
SG
162 case 'k':
163 if (--argc <= 0)
164 usage();
165 params.keydir = *++argv;
166 goto NXTARG;
e29495d3
SG
167 case 'K':
168 if (--argc <= 0)
169 usage();
170 params.keydest = *++argv;
171 goto NXTARG;
5b1d7137
WD
172 case 'n':
173 if (--argc <= 0)
0b443dee 174 usage();
89a4d6b1 175 params.imagename = *++argv;
5b1d7137 176 goto NXTARG;
399c744b
SG
177 case 'r':
178 params.require_keys = 1;
179 break;
5d898a00
SX
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;
f0662105
SB
189 case 's':
190 params.skipcpy = 1;
191 break;
5b1d7137 192 case 'v':
89a4d6b1 193 params.vflag++;
5b1d7137 194 break;
976b38c0
WD
195 case 'V':
196 printf("mkimage version %s\n", PLAIN_VERSION);
197 exit(EXIT_SUCCESS);
5b1d7137 198 case 'x':
89a4d6b1 199 params.xflag++;
5b1d7137
WD
200 break;
201 default:
0b443dee 202 usage();
5b1d7137
WD
203 }
204 }
205NXTARG: ;
206 }
207
89a4d6b1 208 if (argc != 1)
0b443dee
SG
209 usage();
210 params.imagefile = *argv;
211}
212
213
214int 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);
89a4d6b1
PW
229
230 /* set tparams as per input type_id */
a93648d1 231 tparams = imagetool_get_type(params.type);
89a4d6b1
PW
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 }
5b1d7137 237
89a4d6b1
PW
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))
0b443dee 244 usage();
89a4d6b1
PW
245
246 if (!params.eflag) {
247 params.ep = params.addr;
5b1d7137 248 /* If XIP, entry point must be after the U-Boot header */
89a4d6b1
PW
249 if (params.xflag)
250 params.ep += tparams->header_size;
5b1d7137
WD
251 }
252
c81296c1
PT
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);
5b1d7137 262
c81296c1
PT
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);
5b1d7137
WD
279 }
280
c81296c1 281 if (params.lflag || params.fflag) {
5b1d7137
WD
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",
89a4d6b1
PW
287 params.cmdname, params.imagefile,
288 strerror(errno));
5b1d7137
WD
289 exit (EXIT_FAILURE);
290 }
291
89a4d6b1 292 if ((unsigned)sbuf.st_size < tparams->header_size) {
5b1d7137 293 fprintf (stderr,
89a4d6b1
PW
294 "%s: Bad size: \"%s\" is not valid image\n",
295 params.cmdname, params.imagefile);
5b1d7137
WD
296 exit (EXIT_FAILURE);
297 }
298
fa956fde
MF
299 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
300 if (ptr == MAP_FAILED) {
5b1d7137 301 fprintf (stderr, "%s: Can't read %s: %s\n",
89a4d6b1
PW
302 params.cmdname, params.imagefile,
303 strerror(errno));
5b1d7137
WD
304 exit (EXIT_FAILURE);
305 }
306
89a4d6b1
PW
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 */
0ca6691c
GMF
313 retval = imagetool_verify_print_header(ptr, &sbuf,
314 tparams, &params);
5b1d7137 315
5b1d7137
WD
316 (void) munmap((void *)ptr, sbuf.st_size);
317 (void) close (ifd);
318
f7644c0b 319 exit (retval);
5b1d7137
WD
320 }
321
34633141 322 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
6ae6e160
PDS
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 }
92a655c3 330
6ae6e160
PDS
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 }
92a655c3 337
6ae6e160
PDS
338 params.file_size = sbuf.st_size + tparams->header_size;
339 close(dfd);
340 }
92a655c3 341
5b1d7137 342 /*
f0662105
SB
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.
5b1d7137 347 */
f0662105 348 if (tparams->vrec_header)
9bac0bb3 349 pad_len = tparams->vrec_header(&params, tparams);
f0662105
SB
350 else
351 memset(tparams->hdr, 0, tparams->header_size);
5b1d7137 352
89a4d6b1
PW
353 if (write(ifd, tparams->hdr, tparams->header_size)
354 != tparams->header_size) {
5b1d7137 355 fprintf (stderr, "%s: Write error on %s: %s\n",
89a4d6b1 356 params.cmdname, params.imagefile, strerror(errno));
5b1d7137
WD
357 exit (EXIT_FAILURE);
358 }
359
d1be8f92
CR
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;
5b1d7137
WD
382 }
383
d1be8f92
CR
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));
5b1d7137
WD
388 exit (EXIT_FAILURE);
389 }
5b1d7137 390
d1be8f92
CR
391 if (!file) {
392 break;
393 }
5b1d7137 394
d1be8f92
CR
395 if (sep) {
396 *sep = ':';
397 file = sep + 1;
398 } else {
399 file = NULL;
400 }
5b1d7137
WD
401 }
402
d1be8f92 403 file = params.datafile;
5b1d7137 404
d1be8f92
CR
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 }
5b1d7137 416 }
5d898a00
SX
417 } else if (params.type == IH_TYPE_PBLIMAGE) {
418 /* PBL has special Image format, implements its' own */
419 pbl_load_uboot(ifd, &params);
d1be8f92 420 } else {
9bac0bb3 421 copy_file(ifd, params.datafile, pad_len);
5b1d7137 422 }
5b1d7137
WD
423 }
424
425 /* We're a bit of paranoid */
89a4d6b1
PW
426#if defined(_POSIX_SYNCHRONIZED_IO) && \
427 !defined(__sun__) && \
428 !defined(__FreeBSD__) && \
31cbe80c 429 !defined(__OpenBSD__) && \
89a4d6b1 430 !defined(__APPLE__)
5b1d7137
WD
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",
89a4d6b1 438 params.cmdname, params.imagefile, strerror(errno));
5b1d7137
WD
439 exit (EXIT_FAILURE);
440 }
92a655c3 441 params.file_size = sbuf.st_size;
5b1d7137 442
fa956fde
MF
443 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
444 if (ptr == MAP_FAILED) {
5b1d7137 445 fprintf (stderr, "%s: Can't map %s: %s\n",
89a4d6b1 446 params.cmdname, params.imagefile, strerror(errno));
5b1d7137
WD
447 exit (EXIT_FAILURE);
448 }
449
89a4d6b1
PW
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 }
5b1d7137 458
89a4d6b1
PW
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 }
5b1d7137
WD
467
468 (void) munmap((void *)ptr, sbuf.st_size);
469
470 /* We're a bit of paranoid */
89a4d6b1
PW
471#if defined(_POSIX_SYNCHRONIZED_IO) && \
472 !defined(__sun__) && \
473 !defined(__FreeBSD__) && \
31cbe80c 474 !defined(__OpenBSD__) && \
89a4d6b1 475 !defined(__APPLE__)
5b1d7137
WD
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",
89a4d6b1 483 params.cmdname, params.imagefile, strerror(errno));
5b1d7137
WD
484 exit (EXIT_FAILURE);
485 }
486
487 exit (EXIT_SUCCESS);
488}
489
490static void
491copy_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;
9bac0bb3 498 uint8_t zeros[4096];
5b1d7137
WD
499 int offset = 0;
500 int size;
a93648d1 501 struct image_type_params *tparams = imagetool_get_type(params.type);
5b1d7137 502
9bac0bb3
SB
503 memset(zeros, 0, sizeof(zeros));
504
89a4d6b1 505 if (params.vflag) {
5b1d7137
WD
506 fprintf (stderr, "Adding Image %s\n", datafile);
507 }
508
ef1464cc 509 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
5b1d7137 510 fprintf (stderr, "%s: Can't open %s: %s\n",
89a4d6b1 511 params.cmdname, datafile, strerror(errno));
5b1d7137
WD
512 exit (EXIT_FAILURE);
513 }
514
515 if (fstat(dfd, &sbuf) < 0) {
516 fprintf (stderr, "%s: Can't stat %s: %s\n",
89a4d6b1 517 params.cmdname, datafile, strerror(errno));
5b1d7137
WD
518 exit (EXIT_FAILURE);
519 }
520
fa956fde
MF
521 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
522 if (ptr == MAP_FAILED) {
5b1d7137 523 fprintf (stderr, "%s: Can't read %s: %s\n",
89a4d6b1 524 params.cmdname, datafile, strerror(errno));
5b1d7137
WD
525 exit (EXIT_FAILURE);
526 }
527
89a4d6b1 528 if (params.xflag) {
5b1d7137
WD
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
89a4d6b1 536 if ((unsigned)sbuf.st_size < tparams->header_size) {
5b1d7137
WD
537 fprintf (stderr,
538 "%s: Bad size: \"%s\" is too small for XIP\n",
89a4d6b1 539 params.cmdname, datafile);
5b1d7137
WD
540 exit (EXIT_FAILURE);
541 }
542
89a4d6b1 543 for (p = ptr; p < ptr + tparams->header_size; p++) {
5b1d7137
WD
544 if ( *p != 0xff ) {
545 fprintf (stderr,
546 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
89a4d6b1 547 params.cmdname, datafile);
5b1d7137
WD
548 exit (EXIT_FAILURE);
549 }
550 }
551
89a4d6b1 552 offset = tparams->header_size;
5b1d7137
WD
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",
89a4d6b1 558 params.cmdname, params.imagefile, strerror(errno));
5b1d7137
WD
559 exit (EXIT_FAILURE);
560 }
561
9bac0bb3
SB
562 tail = size % 4;
563 if ((pad == 1) && (tail != 0)) {
5b1d7137
WD
564
565 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
566 fprintf (stderr, "%s: Write error on %s: %s\n",
89a4d6b1
PW
567 params.cmdname, params.imagefile,
568 strerror(errno));
5b1d7137
WD
569 exit (EXIT_FAILURE);
570 }
9bac0bb3 571 } else if (pad > 1) {
424b86ae
SG
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;
9bac0bb3 584 }
5b1d7137
WD
585 }
586
587 (void) munmap((void *)ptr, sbuf.st_size);
588 (void) close (dfd);
589}
590
f1cc458c 591static void usage(void)
5b1d7137
WD
592{
593 fprintf (stderr, "Usage: %s -l image\n"
9d25438f 594 " -l ==> list image header information\n",
89a4d6b1 595 params.cmdname);
9d25438f
BS
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"
5b1d7137
WD
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"
9d25438f 606 " -x ==> set XIP (execute in place)\n",
89a4d6b1 607 params.cmdname);
95d77b44 608 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
89a4d6b1 609 params.cmdname);
35497307 610 fprintf(stderr, " -D => set all options for device tree compiler\n"
80e4df8a
SG
611 " -f => input filename for FIT source\n");
612#ifdef CONFIG_FIT_SIGNATURE
399c744b 613 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
e29495d3 614 " -k => set directory containing private keys\n"
95d77b44 615 " -K => write public keys to this .dtb file\n"
4f610427 616 " -c => add comment in signature node\n"
399c744b
SG
617 " -F => re-sign existing FIT image\n"
618 " -r => mark keys used as 'required' in dtb\n");
80e4df8a
SG
619#else
620 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
621#endif
976b38c0
WD
622 fprintf (stderr, " %s -V ==> print version information and exit\n",
623 params.cmdname);
5b9d44df 624 fprintf(stderr, "Use -T to see a list of available image types\n");
9d25438f 625
5b1d7137
WD
626 exit (EXIT_FAILURE);
627}