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