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