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