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