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