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