]> git.ipfire.org Git - thirdparty/u-boot.git/blame - tools/mkimage.c
Merge tag 'efi-2025-07-rc6' of https://source.denx.de/u-boot/custodians/u-boot-efi
[thirdparty/u-boot.git] / tools / mkimage.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
5b1d7137 2/*
9d25438f
BS
3 * (C) Copyright 2008 Semihalf
4 *
89a4d6b1 5 * (C) Copyright 2000-2009
5b1d7137
WD
6 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
5b1d7137
WD
8 */
9
29e7ab01 10#include "imagetool.h"
b97a2a0a 11#include "mkimage.h"
d21bd69b 12#include "imximage.h"
2d2384bb 13#include <fit_common.h>
dc3a923e 14#include <getopt.h>
5b1d7137 15#include <image.h>
976b38c0 16#include <version.h>
331f0800
YD
17#ifdef __linux__
18#include <sys/ioctl.h>
19#endif
89a4d6b1
PW
20
21static void copy_file(int, const char *, int);
89a4d6b1 22
89a4d6b1 23/* parameters initialized by core will be used by the image type code */
cc7a6444 24static struct image_tool_params params = {
89a4d6b1
PW
25 .os = IH_OS_LINUX,
26 .arch = IH_ARCH_PPC,
27 .type = IH_TYPE_KERNEL,
28 .comp = IH_COMP_GZIP,
29 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
04387d24 30 .imagename = "",
5d898a00 31 .imagename2 = "",
89a4d6b1
PW
32};
33
30664225
SG
34static enum ih_category cur_category;
35
36static int h_compare_category_name(const void *vtype1, const void *vtype2)
37{
38 const int *type1 = vtype1;
39 const int *type2 = vtype2;
40 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
41 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
42
43 return strcmp(name1, name2);
44}
45
f24e1050 46static int show_valid_options(enum ih_category category)
30664225
SG
47{
48 int *order;
49 int count;
50 int item;
51 int i;
52
53 count = genimg_get_cat_count(category);
54 order = calloc(count, sizeof(*order));
55 if (!order)
56 return -ENOMEM;
57
58 /* Sort the names in order of short name for easier reading */
ad5fb9f2
NH
59 for (i = 0, item = 0; i < count; i++, item++) {
60 while (!genimg_cat_has_id(category, item) && i < count) {
61 item++;
62 count--;
63 }
64 order[i] = item;
65 }
30664225
SG
66 cur_category = category;
67 qsort(order, count, sizeof(int), h_compare_category_name);
68
69 fprintf(stderr, "\nInvalid %s, supported are:\n",
70 genimg_get_cat_desc(category));
71 for (i = 0; i < count; i++) {
72 item = order[i];
73 fprintf(stderr, "\t%-15s %s\n",
74 genimg_get_cat_short_name(category, item),
75 genimg_get_cat_name(category, item));
76 }
77 fprintf(stderr, "\n");
0cd82e25 78 free(order);
30664225
SG
79
80 return 0;
81}
82
15310348 83static void usage(const char *msg)
b0a487a4 84{
15310348 85 fprintf(stderr, "Error: %s\n", msg);
11f29d44
T
86 fprintf(stderr, "Usage: %s [-T type] -l image\n"
87 " -l ==> list image header information\n"
deb2638a
SA
88 " -T ==> parse image file as 'type'\n"
89 " -q ==> quiet\n",
b0a487a4
SG
90 params.cmdname);
91 fprintf(stderr,
92 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
93 " -A ==> set architecture to 'arch'\n"
94 " -O ==> set operating system to 'os'\n"
95 " -T ==> set image type to 'type'\n"
96 " -C ==> set compression type 'comp'\n"
97 " -a ==> set load address to 'addr' (hex)\n"
98 " -e ==> set entry point to 'ep' (hex)\n"
99 " -n ==> set image name to 'name'\n"
deb2638a 100 " -R ==> set second image name to 'name'\n"
b0a487a4 101 " -d ==> use image data from 'datafile'\n"
deb2638a
SA
102 " -x ==> set XIP (execute in place)\n"
103 " -s ==> create an image with no data\n"
104 " -v ==> verbose\n",
b0a487a4
SG
105 params.cmdname);
106 fprintf(stderr,
b93a6520 107 " %s [-D dtc_options] [-f fit-image.its|-f auto|-f auto-conf|-F] [-b <dtb> [-b <dtb>]] [-E] [-B size] [-i <ramdisk.cpio.gz>] fit-image\n"
82bd2f29 108 " <dtb> file is used with -f auto, it may occur multiple times.\n",
b0a487a4
SG
109 params.cmdname);
110 fprintf(stderr,
111 " -D => set all options for device tree compiler\n"
0f7c6cdc 112 " -f => input filename for FIT source\n"
603e26f7
JS
113 " -i => input filename for ramdisk file\n"
114 " -E => place data outside of the FIT structure\n"
429d59c3 115 " -B => align size in hex for FIT structure and header\n"
deb2638a
SA
116 " -b => append the device tree binary to the FIT\n"
117 " -t => update the timestamp in the FIT\n");
03e59826 118#if CONFIG_IS_ENABLED(FIT_SIGNATURE)
b0a487a4 119 fprintf(stderr,
603e26f7 120 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
b0a487a4
SG
121 " -k => set directory containing private keys\n"
122 " -K => write public keys to this .dtb file\n"
87b0af93 123 " -g => set key name hint\n"
36bfcb62 124 " -G => use this signing key (in lieu of -k)\n"
b0a487a4
SG
125 " -c => add comment in signature node\n"
126 " -F => re-sign existing FIT image\n"
f8f9107d 127 " -p => place external data at a static position\n"
f1ca1fde 128 " -r => mark keys used as 'required' in dtb\n"
deb2638a
SA
129 " -N => openssl engine to use for signing\n"
130 " -o => algorithm to use for signing\n");
b0a487a4
SG
131#else
132 fprintf(stderr,
03e59826 133 "Signing / verified boot not supported (CONFIG_TOOLS_FIT_SIGNATURE undefined)\n");
b0a487a4 134#endif
03e59826 135
b0a487a4
SG
136 fprintf(stderr, " %s -V ==> print version information and exit\n",
137 params.cmdname);
79aa33cd 138 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
dc3a923e 139 fprintf(stderr, "Long options are available; read the man page for details\n");
b0a487a4
SG
140
141 exit(EXIT_FAILURE);
142}
143
fb4cce0f
SG
144static int add_content(int type, const char *fname)
145{
146 struct content_info *cont;
147
148 cont = calloc(1, sizeof(*cont));
149 if (!cont)
150 return -1;
151 cont->type = type;
152 cont->fname = fname;
153 if (params.content_tail)
154 params.content_tail->next = cont;
155 else
156 params.content_head = cont;
157 params.content_tail = cont;
158
159 return 0;
160}
161
dc3a923e
SA
162static const char optstring[] =
163 "a:A:b:B:c:C:d:D:e:Ef:Fg:G:i:k:K:ln:N:o:O:p:qrR:stT:vVx";
164
165static const struct option longopts[] = {
166 { "load-address", required_argument, NULL, 'a' },
167 { "architecture", required_argument, NULL, 'A' },
168 { "device-tree", required_argument, NULL, 'b' },
169 { "alignment", required_argument, NULL, 'B' },
170 { "comment", required_argument, NULL, 'c' },
171 { "compression", required_argument, NULL, 'C' },
172 { "image", required_argument, NULL, 'd' },
173 { "dtcopts", required_argument, NULL, 'D' },
174 { "entry-point", required_argument, NULL, 'e' },
175 { "external", no_argument, NULL, 'E' },
176 { "fit", required_argument, NULL, 'f' },
177 { "update", no_argument, NULL, 'F' },
178 { "key-name-hint", required_argument, NULL, 'g' },
179 { "key-file", required_argument, NULL, 'G' },
180 { "help", no_argument, NULL, 'h' },
181 { "initramfs", required_argument, NULL, 'i' },
182 { "key-dir", required_argument, NULL, 'k' },
183 { "key-dest", required_argument, NULL, 'K' },
184 { "list", no_argument, NULL, 'l' },
185 { "config", required_argument, NULL, 'n' },
186 { "engine", required_argument, NULL, 'N' },
187 { "algo", required_argument, NULL, 'o' },
188 { "os", required_argument, NULL, 'O' },
189 { "position", required_argument, NULL, 'p' },
190 { "quiet", no_argument, NULL, 'q' },
191 { "key-required", no_argument, NULL, 'r' },
192 { "secondary-config", required_argument, NULL, 'R' },
193 { "no-copy", no_argument, NULL, 's' },
194 { "touch", no_argument, NULL, 't' },
195 { "type", required_argument, NULL, 'T' },
196 { "verbose", no_argument, NULL, 'v' },
197 { "version", no_argument, NULL, 'V' },
198 { "xip", no_argument, NULL, 'x' },
2c529839 199 { /* sentinel */ },
dc3a923e
SA
200};
201
0b443dee 202static void process_args(int argc, char **argv)
5b1d7137 203{
a2513e27 204 char *ptr;
d505a09c
SG
205 int type = IH_TYPE_INVALID;
206 char *datafile = NULL;
a02221f2
SG
207 int opt;
208
dc3a923e
SA
209 while ((opt = getopt_long(argc, argv, optstring,
210 longopts, NULL)) != -1) {
a02221f2 211 switch (opt) {
07450081
SG
212 case 'a':
213 params.addr = strtoull(optarg, &ptr, 16);
214 if (*ptr) {
215 fprintf(stderr, "%s: invalid load address %s\n",
216 params.cmdname, optarg);
217 exit(EXIT_FAILURE);
218 }
a02221f2
SG
219 break;
220 case 'A':
221 params.arch = genimg_get_arch_id(optarg);
51f03e6a
SG
222 if (params.arch < 0) {
223 show_valid_options(IH_ARCH);
15310348 224 usage("Invalid architecture");
51f03e6a 225 }
c2d08f01 226 params.Aflag = 1;
a02221f2 227 break;
fb4cce0f 228 case 'b':
8edeac86 229 if (add_content(IH_TYPE_FLATDT, optarg)) {
7a439cad
AB
230 fprintf(stderr,
231 "%s: Out of memory adding content '%s'",
232 params.cmdname, optarg);
233 exit(EXIT_FAILURE);
234 }
ebfe611b
KY
235 break;
236 case 'B':
237 params.bl_len = strtoull(optarg, &ptr, 16);
238 if (*ptr) {
239 fprintf(stderr, "%s: invalid block length %s\n",
240 params.cmdname, optarg);
241 exit(EXIT_FAILURE);
242 }
243
fb4cce0f 244 break;
a02221f2
SG
245 case 'c':
246 params.comment = optarg;
247 break;
248 case 'C':
249 params.comp = genimg_get_comp_id(optarg);
51f03e6a
SG
250 if (params.comp < 0) {
251 show_valid_options(IH_COMP);
15310348 252 usage("Invalid compression type");
51f03e6a 253 }
a02221f2 254 break;
a02221f2
SG
255 case 'd':
256 params.datafile = optarg;
257 params.dflag = 1;
258 break;
07450081
SG
259 case 'D':
260 params.dtc = optarg;
261 break;
a02221f2
SG
262 case 'e':
263 params.ep = strtoull(optarg, &ptr, 16);
264 if (*ptr) {
265 fprintf(stderr, "%s: invalid entry point %s\n",
266 params.cmdname, optarg);
267 exit(EXIT_FAILURE);
5b1d7137 268 }
a02221f2
SG
269 params.eflag = 1;
270 break;
722ebc8f
SG
271 case 'E':
272 params.external_data = true;
273 break;
a02221f2 274 case 'f':
8e35bb07 275 datafile = optarg;
b93a6520
MP
276 if (!strcmp(datafile, "auto"))
277 params.auto_fit = AF_HASHED_IMG;
278 else if (!strcmp(datafile, "auto-conf"))
279 params.auto_fit = AF_SIGNED_CONF;
a29162a1 280 /* fallthrough */
a02221f2
SG
281 case 'F':
282 /*
283 * The flattened image tree (FIT) format
284 * requires a flattened device tree image type
285 */
286 params.type = IH_TYPE_FLATDT;
287 params.fflag = 1;
288 break;
87b0af93
SA
289 case 'g':
290 params.keyname = optarg;
b93a6520 291 break;
36bfcb62
AG
292 case 'G':
293 params.keyfile = optarg;
294 break;
0f7c6cdc
TV
295 case 'i':
296 params.fit_ramdisk = optarg;
297 break;
a02221f2
SG
298 case 'k':
299 params.keydir = optarg;
300 break;
301 case 'K':
302 params.keydest = optarg;
303 break;
07450081
SG
304 case 'l':
305 params.lflag = 1;
306 break;
a02221f2
SG
307 case 'n':
308 params.imagename = optarg;
309 break;
f1ca1fde
GM
310 case 'N':
311 params.engine_id = optarg;
312 break;
5902a397
JK
313 case 'o':
314 params.algo_name = optarg;
315 break;
07450081
SG
316 case 'O':
317 params.os = genimg_get_os_id(optarg);
51f03e6a
SG
318 if (params.os < 0) {
319 show_valid_options(IH_OS);
15310348 320 usage("Invalid operating system");
51f03e6a 321 }
07450081 322 break;
f8f9107d
TR
323 case 'p':
324 params.external_offset = strtoull(optarg, &ptr, 16);
325 if (*ptr) {
326 fprintf(stderr, "%s: invalid offset size %s\n",
327 params.cmdname, optarg);
328 exit(EXIT_FAILURE);
329 }
b6fefa76 330 break;
bd6e1420
SG
331 case 'q':
332 params.quiet = 1;
333 break;
a02221f2
SG
334 case 'r':
335 params.require_keys = 1;
336 break;
337 case 'R':
338 /*
339 * This entry is for the second configuration
340 * file, if only one is not enough.
341 */
342 params.imagename2 = optarg;
343 break;
344 case 's':
345 params.skipcpy = 1;
346 break;
152b2462
SG
347 case 't':
348 params.reset_timestamp = 1;
349 break;
07450081 350 case 'T':
79aa33cd
BS
351 if (strcmp(optarg, "list") == 0) {
352 show_valid_options(IH_TYPE);
353 exit(EXIT_SUCCESS);
354 }
d505a09c
SG
355 type = genimg_get_type_id(optarg);
356 if (type < 0) {
f24e1050 357 show_valid_options(IH_TYPE);
15310348 358 usage("Invalid image type");
07450081
SG
359 }
360 break;
a02221f2
SG
361 case 'v':
362 params.vflag++;
363 break;
364 case 'V':
365 printf("mkimage version %s\n", PLAIN_VERSION);
366 exit(EXIT_SUCCESS);
367 case 'x':
368 params.xflag++;
369 break;
370 default:
15310348 371 usage("Invalid option");
5b1d7137 372 }
5b1d7137
WD
373 }
374
8edeac86
AB
375 /* The last parameter is expected to be the imagefile */
376 if (optind < argc)
7a439cad
AB
377 params.imagefile = argv[optind];
378
b93a6520
MP
379 if (params.auto_fit == AF_SIGNED_CONF) {
380 if (!params.keyname || !params.algo_name)
381 usage("Missing key/algo for auto-FIT with signed configs (use -g -o)");
382 } else if (params.auto_fit == AF_HASHED_IMG && params.keyname) {
383 params.auto_fit = AF_SIGNED_IMG;
384 if (!params.algo_name)
385 usage("Missing algorithm for auto-FIT with signed images (use -g)");
386 }
387
d505a09c
SG
388 /*
389 * For auto-generated FIT images we need to know the image type to put
390 * in the FIT, which is separate from the file's image type (which
391 * will always be IH_TYPE_FLATDT in this case).
392 */
393 if (params.type == IH_TYPE_FLATDT) {
20deaddd 394 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
b93a6520
MP
395 /* For auto-FIT, datafile has to be provided with -d */
396 if (!params.auto_fit)
8e35bb07 397 params.datafile = datafile;
e324a925
SG
398 else if (!params.datafile)
399 usage("Missing data file for auto-FIT (use -d)");
11f29d44 400 } else if (params.lflag || type != IH_TYPE_INVALID) {
8c84287a
AK
401 if (type == IH_TYPE_SCRIPT && !params.datafile)
402 usage("Missing data file for script (use -d)");
d505a09c
SG
403 params.type = type;
404 }
405
406 if (!params.imagefile)
15310348 407 usage("Missing output filename");
0b443dee
SG
408}
409
d3b1ca21
T
410static void verify_image(const struct image_type_params *tparams)
411{
412 struct stat sbuf;
413 void *ptr;
414 int ifd;
415
416 ifd = open(params.imagefile, O_RDONLY | O_BINARY);
417 if (ifd < 0) {
418 fprintf(stderr, "%s: Can't open %s: %s\n",
419 params.cmdname, params.imagefile,
420 strerror(errno));
421 exit(EXIT_FAILURE);
422 }
423
424 if (fstat(ifd, &sbuf) < 0) {
425 fprintf(stderr, "%s: Can't stat %s: %s\n",
426 params.cmdname, params.imagefile, strerror(errno));
427 exit(EXIT_FAILURE);
428 }
429 params.file_size = sbuf.st_size;
430
431 ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0);
432 if (ptr == MAP_FAILED) {
433 fprintf(stderr, "%s: Can't map %s: %s\n",
434 params.cmdname, params.imagefile, strerror(errno));
435 exit(EXIT_FAILURE);
436 }
437
438 if (tparams->verify_header((unsigned char *)ptr, params.file_size, &params) != 0) {
439 fprintf(stderr, "%s: Failed to verify header of %s\n",
440 params.cmdname, params.imagefile);
441 exit(EXIT_FAILURE);
442 }
443
444 (void)munmap(ptr, params.file_size);
445 (void)close(ifd);
446}
447
17f8a748
MI
448void copy_datafile(int ifd, char *file)
449{
450 if (!file)
451 return;
452 for (;;) {
453 char *sep = strchr(file, ':');
454
455 if (sep) {
456 *sep = '\0';
457 copy_file(ifd, file, 1);
458 *sep++ = ':';
459 file = sep;
460 } else {
461 copy_file(ifd, file, 0);
462 break;
463 }
464 }
465}
466
0b443dee
SG
467int main(int argc, char **argv)
468{
469 int ifd = -1;
470 struct stat sbuf;
471 char *ptr;
472 int retval = 0;
473 struct image_type_params *tparams = NULL;
474 int pad_len = 0;
475 int dfd;
8961c8ad 476 size_t map_len;
0b443dee
SG
477
478 params.cmdname = *argv;
479 params.addr = 0;
480 params.ep = 0;
481
482 process_args(argc, argv);
89a4d6b1
PW
483
484 /* set tparams as per input type_id */
a93648d1 485 tparams = imagetool_get_type(params.type);
11f29d44 486 if (tparams == NULL && !params.lflag) {
89a4d6b1
PW
487 fprintf (stderr, "%s: unsupported type %s\n",
488 params.cmdname, genimg_get_type_name(params.type));
489 exit (EXIT_FAILURE);
490 }
5b1d7137 491
89a4d6b1
PW
492 /*
493 * check the passed arguments parameters meets the requirements
494 * as per image type to be generated/listed
495 */
11f29d44 496 if (tparams && tparams->check_params)
89a4d6b1 497 if (tparams->check_params (&params))
15310348 498 usage("Bad parameters for image type");
89a4d6b1
PW
499
500 if (!params.eflag) {
501 params.ep = params.addr;
5b1d7137 502 /* If XIP, entry point must be after the U-Boot header */
11f29d44 503 if (params.xflag && tparams)
89a4d6b1 504 params.ep += tparams->header_size;
5b1d7137
WD
505 }
506
c81296c1 507 if (params.fflag){
5017f9b5
HS
508 if (!tparams) {
509 fprintf(stderr, "%s: Missing FIT support\n",
510 params.cmdname);
511 exit (EXIT_FAILURE);
512 }
c81296c1
PT
513 if (tparams->fflag_handle)
514 /*
515 * in some cases, some additional processing needs
516 * to be done if fflag is defined
517 *
518 * For ex. fit_handle_file for Fit file support
519 */
520 retval = tparams->fflag_handle(&params);
5b1d7137 521
c81296c1 522 if (retval != EXIT_SUCCESS)
5017f9b5 523 usage("Bad parameters for FIT image type");
c81296c1
PT
524 }
525
526 if (params.lflag || params.fflag) {
527 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
528 } else {
529 ifd = open (params.imagefile,
530 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
531 }
532
533 if (ifd < 0) {
534 fprintf (stderr, "%s: Can't open %s: %s\n",
535 params.cmdname, params.imagefile,
536 strerror(errno));
537 exit (EXIT_FAILURE);
5b1d7137
WD
538 }
539
c81296c1 540 if (params.lflag || params.fflag) {
331f0800 541 uint64_t size;
5b1d7137
WD
542 /*
543 * list header information of existing image
544 */
545 if (fstat(ifd, &sbuf) < 0) {
546 fprintf (stderr, "%s: Can't stat %s: %s\n",
89a4d6b1
PW
547 params.cmdname, params.imagefile,
548 strerror(errno));
5b1d7137
WD
549 exit (EXIT_FAILURE);
550 }
551
331f0800
YD
552 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
553#ifdef __linux__
554#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
555#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
556#endif
557 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
558 fprintf (stderr,
559 "%s: failed to get size of block device \"%s\"\n",
560 params.cmdname, params.imagefile);
561 exit (EXIT_FAILURE);
562 }
563#else
5b1d7137 564 fprintf (stderr,
331f0800 565 "%s: \"%s\" is block device, don't know how to get its size\n",
89a4d6b1 566 params.cmdname, params.imagefile);
5b1d7137 567 exit (EXIT_FAILURE);
331f0800 568#endif
11f29d44 569 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
331f0800 570 fprintf (stderr,
c28f2499 571 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
331f0800 572 params.cmdname, params.imagefile,
c28f2499
HS
573 (unsigned long long) sbuf.st_size,
574 tparams->header_size);
331f0800
YD
575 exit (EXIT_FAILURE);
576 } else {
577 size = sbuf.st_size;
5b1d7137
WD
578 }
579
331f0800 580 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
fa956fde 581 if (ptr == MAP_FAILED) {
5b1d7137 582 fprintf (stderr, "%s: Can't read %s: %s\n",
89a4d6b1
PW
583 params.cmdname, params.imagefile,
584 strerror(errno));
5b1d7137
WD
585 exit (EXIT_FAILURE);
586 }
587
11f29d44
T
588 /*
589 * Verifies the header format based on the expected header for image
590 * type in tparams. If tparams is NULL simply check all image types
591 * to find one that matches our header.
592 */
593 retval = imagetool_verify_print_header(ptr, &sbuf, tparams, &params);
5b1d7137 594
5b1d7137
WD
595 (void) munmap((void *)ptr, sbuf.st_size);
596 (void) close (ifd);
2d2384bb
SG
597 if (!retval)
598 summary_show(&params.summary, params.imagefile,
599 params.keydest);
5b1d7137 600
f7644c0b 601 exit (retval);
5b1d7137
WD
602 }
603
b07965b8 604 if (!params.skipcpy && params.type != IH_TYPE_MULTI && params.type != IH_TYPE_SCRIPT) {
2f6855a6
T
605 if (!params.datafile) {
606 fprintf(stderr, "%s: Option -d with image data file was not specified\n",
607 params.cmdname);
608 exit(EXIT_FAILURE);
609 }
6ae6e160
PDS
610 dfd = open(params.datafile, O_RDONLY | O_BINARY);
611 if (dfd < 0) {
612 fprintf(stderr, "%s: Can't open %s: %s\n",
613 params.cmdname, params.datafile,
614 strerror(errno));
615 exit(EXIT_FAILURE);
616 }
92a655c3 617
6ae6e160
PDS
618 if (fstat(dfd, &sbuf) < 0) {
619 fprintf(stderr, "%s: Can't stat %s: %s\n",
620 params.cmdname, params.datafile,
621 strerror(errno));
622 exit(EXIT_FAILURE);
623 }
92a655c3 624
6ae6e160
PDS
625 params.file_size = sbuf.st_size + tparams->header_size;
626 close(dfd);
627 }
92a655c3 628
5b1d7137 629 /*
f0662105
SB
630 * In case there an header with a variable
631 * length will be added, the corresponding
632 * function is called. This is responsible to
633 * allocate memory for the header itself.
5b1d7137 634 */
f0662105 635 if (tparams->vrec_header)
9bac0bb3 636 pad_len = tparams->vrec_header(&params, tparams);
f0662105
SB
637 else
638 memset(tparams->hdr, 0, tparams->header_size);
5b1d7137 639
89a4d6b1
PW
640 if (write(ifd, tparams->hdr, tparams->header_size)
641 != tparams->header_size) {
5b1d7137 642 fprintf (stderr, "%s: Write error on %s: %s\n",
89a4d6b1 643 params.cmdname, params.imagefile, strerror(errno));
5b1d7137
WD
644 exit (EXIT_FAILURE);
645 }
646
d1be8f92
CR
647 if (!params.skipcpy) {
648 if (params.type == IH_TYPE_MULTI ||
649 params.type == IH_TYPE_SCRIPT) {
650 char *file = params.datafile;
651 uint32_t size;
652
653 for (;;) {
654 char *sep = NULL;
655
656 if (file) {
657 if ((sep = strchr(file, ':')) != NULL) {
658 *sep = '\0';
659 }
660
661 if (stat (file, &sbuf) < 0) {
662 fprintf (stderr, "%s: Can't stat %s: %s\n",
663 params.cmdname, file, strerror(errno));
664 exit (EXIT_FAILURE);
665 }
666 size = cpu_to_uimage (sbuf.st_size);
667 } else {
668 size = 0;
5b1d7137
WD
669 }
670
d1be8f92
CR
671 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
672 fprintf (stderr, "%s: Write error on %s: %s\n",
673 params.cmdname, params.imagefile,
674 strerror(errno));
5b1d7137
WD
675 exit (EXIT_FAILURE);
676 }
5b1d7137 677
d1be8f92
CR
678 if (!file) {
679 break;
680 }
5b1d7137 681
d1be8f92
CR
682 if (sep) {
683 *sep = ':';
684 file = sep + 1;
685 } else {
686 file = NULL;
687 }
5b1d7137 688 }
17f8a748 689 copy_datafile(ifd, params.datafile);
5d898a00
SX
690 } else if (params.type == IH_TYPE_PBLIMAGE) {
691 /* PBL has special Image format, implements its' own */
692 pbl_load_uboot(ifd, &params);
6915dcf3
AG
693 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
694 /* Image file is meta, walk through actual targets */
695 int ret;
696
697 ret = zynqmpbif_copy_image(ifd, &params);
698 if (ret)
699 return ret;
a2b96ece
PF
700 } else if (params.type == IH_TYPE_IMX8IMAGE) {
701 /* i.MX8/8X has special Image format */
702 int ret;
703
704 ret = imx8image_copy_image(ifd, &params);
705 if (ret)
706 return ret;
6609c266
PF
707 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
708 /* i.MX8M has special Image format */
709 int ret;
710
711 ret = imx8mimage_copy_image(ifd, &params);
712 if (ret)
713 return ret;
eea6cd8d
JC
714 } else if ((params.type == IH_TYPE_RKSD) ||
715 (params.type == IH_TYPE_RKSPI)) {
716 /* Rockchip has special Image format */
717 int ret;
718
719 ret = rockchip_copy_image(ifd, &params);
720 if (ret)
721 return ret;
d1be8f92 722 } else {
9bac0bb3 723 copy_file(ifd, params.datafile, pad_len);
5b1d7137 724 }
d21bd69b
SE
725 if (params.type == IH_TYPE_FIRMWARE_IVT) {
726 /* Add alignment and IVT */
29e7ab01
KY
727 uint32_t aligned_filesize = ALIGN(params.file_size,
728 0x1000);
d21bd69b
SE
729 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
730 params.addr, 0, 0, 0, params.addr
731 + aligned_filesize
732 - tparams->header_size,
733 params.addr + aligned_filesize
734 - tparams->header_size
735 + 0x20, 0 };
736 int i = params.file_size;
737 for (; i < aligned_filesize; i++) {
b4e923a8 738 if (write(ifd, (char *) &i, 1) != 1) {
d21bd69b
SE
739 fprintf(stderr,
740 "%s: Write error on %s: %s\n",
741 params.cmdname,
742 params.imagefile,
743 strerror(errno));
744 exit(EXIT_FAILURE);
745 }
746 }
747 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
748 != sizeof(flash_header_v2_t)) {
749 fprintf(stderr, "%s: Write error on %s: %s\n",
750 params.cmdname,
751 params.imagefile,
752 strerror(errno));
753 exit(EXIT_FAILURE);
754 }
755 }
5b1d7137
WD
756 }
757
758 /* We're a bit of paranoid */
89a4d6b1
PW
759#if defined(_POSIX_SYNCHRONIZED_IO) && \
760 !defined(__sun__) && \
761 !defined(__FreeBSD__) && \
31cbe80c 762 !defined(__OpenBSD__) && \
89a4d6b1 763 !defined(__APPLE__)
5b1d7137
WD
764 (void) fdatasync (ifd);
765#else
766 (void) fsync (ifd);
767#endif
768
769 if (fstat(ifd, &sbuf) < 0) {
770 fprintf (stderr, "%s: Can't stat %s: %s\n",
89a4d6b1 771 params.cmdname, params.imagefile, strerror(errno));
5b1d7137
WD
772 exit (EXIT_FAILURE);
773 }
92a655c3 774 params.file_size = sbuf.st_size;
5b1d7137 775
8961c8ad
MT
776 map_len = sbuf.st_size;
777 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
fa956fde 778 if (ptr == MAP_FAILED) {
5b1d7137 779 fprintf (stderr, "%s: Can't map %s: %s\n",
89a4d6b1 780 params.cmdname, params.imagefile, strerror(errno));
5b1d7137
WD
781 exit (EXIT_FAILURE);
782 }
783
89a4d6b1
PW
784 /* Setup the image header as per input image type*/
785 if (tparams->set_header)
786 tparams->set_header (ptr, &sbuf, ifd, &params);
787 else {
72c3f5db
MKB
788 fprintf (stderr, "%s: Can't set header for %s\n",
789 params.cmdname, tparams->name);
89a4d6b1
PW
790 exit (EXIT_FAILURE);
791 }
5b1d7137 792
89a4d6b1
PW
793 /* Print the image information by processing image header */
794 if (tparams->print_header)
2972d7d6 795 tparams->print_header (ptr, &params);
89a4d6b1 796 else {
004d0091
GG
797 fprintf (stderr, "%s: Can't print header for %s\n",
798 params.cmdname, tparams->name);
89a4d6b1 799 }
5b1d7137 800
8961c8ad 801 (void)munmap((void *)ptr, map_len);
5b1d7137
WD
802
803 /* We're a bit of paranoid */
89a4d6b1
PW
804#if defined(_POSIX_SYNCHRONIZED_IO) && \
805 !defined(__sun__) && \
806 !defined(__FreeBSD__) && \
31cbe80c 807 !defined(__OpenBSD__) && \
89a4d6b1 808 !defined(__APPLE__)
5b1d7137
WD
809 (void) fdatasync (ifd);
810#else
811 (void) fsync (ifd);
812#endif
813
814 if (close(ifd)) {
815 fprintf (stderr, "%s: Write error on %s: %s\n",
89a4d6b1 816 params.cmdname, params.imagefile, strerror(errno));
5b1d7137
WD
817 exit (EXIT_FAILURE);
818 }
819
d3b1ca21
T
820 if (tparams->verify_header)
821 verify_image(tparams);
822
5b1d7137
WD
823 exit (EXIT_SUCCESS);
824}
825
826static void
827copy_file (int ifd, const char *datafile, int pad)
828{
829 int dfd;
830 struct stat sbuf;
831 unsigned char *ptr;
832 int tail;
833 int zero = 0;
9bac0bb3 834 uint8_t zeros[4096];
5b1d7137 835 int offset = 0;
dd85dc55 836 int size, ret;
a93648d1 837 struct image_type_params *tparams = imagetool_get_type(params.type);
5b1d7137 838
9bac0bb3
SB
839 memset(zeros, 0, sizeof(zeros));
840
89a4d6b1 841 if (params.vflag) {
5b1d7137
WD
842 fprintf (stderr, "Adding Image %s\n", datafile);
843 }
844
ef1464cc 845 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
5b1d7137 846 fprintf (stderr, "%s: Can't open %s: %s\n",
89a4d6b1 847 params.cmdname, datafile, strerror(errno));
5b1d7137
WD
848 exit (EXIT_FAILURE);
849 }
850
851 if (fstat(dfd, &sbuf) < 0) {
852 fprintf (stderr, "%s: Can't stat %s: %s\n",
89a4d6b1 853 params.cmdname, datafile, strerror(errno));
5b1d7137
WD
854 exit (EXIT_FAILURE);
855 }
856
eaa6442e
TH
857 if (sbuf.st_size == 0) {
858 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
859 params.cmdname, datafile);
860 exit (EXIT_FAILURE);
861 }
862
fa956fde
MF
863 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
864 if (ptr == MAP_FAILED) {
5b1d7137 865 fprintf (stderr, "%s: Can't read %s: %s\n",
89a4d6b1 866 params.cmdname, datafile, strerror(errno));
5b1d7137
WD
867 exit (EXIT_FAILURE);
868 }
869
27670aca
T
870 if (params.xflag &&
871 (((params.type > IH_TYPE_INVALID) && (params.type < IH_TYPE_FLATDT)) ||
872 (params.type == IH_TYPE_KERNEL_NOLOAD) || (params.type == IH_TYPE_FIRMWARE_IVT))) {
5b1d7137
WD
873 unsigned char *p = NULL;
874 /*
f3543e69 875 * XIP: do not append the struct legacy_img_hdr at the
5b1d7137
WD
876 * beginning of the file, but consume the space
877 * reserved for it.
878 */
879
89a4d6b1 880 if ((unsigned)sbuf.st_size < tparams->header_size) {
5b1d7137
WD
881 fprintf (stderr,
882 "%s: Bad size: \"%s\" is too small for XIP\n",
89a4d6b1 883 params.cmdname, datafile);
5b1d7137
WD
884 exit (EXIT_FAILURE);
885 }
886
89a4d6b1 887 for (p = ptr; p < ptr + tparams->header_size; p++) {
5b1d7137
WD
888 if ( *p != 0xff ) {
889 fprintf (stderr,
890 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
89a4d6b1 891 params.cmdname, datafile);
5b1d7137
WD
892 exit (EXIT_FAILURE);
893 }
894 }
895
89a4d6b1 896 offset = tparams->header_size;
5b1d7137
WD
897 }
898
899 size = sbuf.st_size - offset;
dd85dc55
MJ
900
901 ret = write(ifd, ptr + offset, size);
902 if (ret != size) {
903 if (ret < 0)
904 fprintf (stderr, "%s: Write error on %s: %s\n",
905 params.cmdname, params.imagefile, strerror(errno));
906 else if (ret < size)
907 fprintf (stderr, "%s: Write only %d/%d bytes, "\
908 "probably no space left on the device\n",
909 params.cmdname, ret, size);
5b1d7137
WD
910 exit (EXIT_FAILURE);
911 }
912
9bac0bb3
SB
913 tail = size % 4;
914 if ((pad == 1) && (tail != 0)) {
5b1d7137
WD
915
916 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
917 fprintf (stderr, "%s: Write error on %s: %s\n",
89a4d6b1
PW
918 params.cmdname, params.imagefile,
919 strerror(errno));
5b1d7137
WD
920 exit (EXIT_FAILURE);
921 }
9bac0bb3 922 } else if (pad > 1) {
424b86ae
SG
923 while (pad > 0) {
924 int todo = sizeof(zeros);
925
926 if (todo > pad)
927 todo = pad;
928 if (write(ifd, (char *)&zeros, todo) != todo) {
929 fprintf(stderr, "%s: Write error on %s: %s\n",
930 params.cmdname, params.imagefile,
931 strerror(errno));
932 exit(EXIT_FAILURE);
933 }
934 pad -= todo;
9bac0bb3 935 }
5b1d7137
WD
936 }
937
938 (void) munmap((void *)ptr, sbuf.st_size);
939 (void) close (dfd);
940}