]>
Commit | Line | Data |
---|---|---|
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 | ||
b97a2a0a | 10 | #include "mkimage.h" |
d21bd69b | 11 | #include "imximage.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"); | |
0cd82e25 | 67 | free(order); |
30664225 SG |
68 | |
69 | return 0; | |
70 | } | |
71 | ||
15310348 | 72 | static void usage(const char *msg) |
b0a487a4 | 73 | { |
15310348 | 74 | fprintf(stderr, "Error: %s\n", msg); |
b0a487a4 SG |
75 | fprintf(stderr, "Usage: %s -l image\n" |
76 | " -l ==> list image header information\n", | |
77 | params.cmdname); | |
78 | fprintf(stderr, | |
79 | " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n" | |
80 | " -A ==> set architecture to 'arch'\n" | |
81 | " -O ==> set operating system to 'os'\n" | |
82 | " -T ==> set image type to 'type'\n" | |
83 | " -C ==> set compression type 'comp'\n" | |
84 | " -a ==> set load address to 'addr' (hex)\n" | |
85 | " -e ==> set entry point to 'ep' (hex)\n" | |
86 | " -n ==> set image name to 'name'\n" | |
87 | " -d ==> use image data from 'datafile'\n" | |
88 | " -x ==> set XIP (execute in place)\n", | |
89 | params.cmdname); | |
90 | fprintf(stderr, | |
0f7c6cdc | 91 | " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n" |
82bd2f29 | 92 | " <dtb> file is used with -f auto, it may occur multiple times.\n", |
b0a487a4 SG |
93 | params.cmdname); |
94 | fprintf(stderr, | |
95 | " -D => set all options for device tree compiler\n" | |
0f7c6cdc TV |
96 | " -f => input filename for FIT source\n" |
97 | " -i => input filename for ramdisk file\n"); | |
b0a487a4 SG |
98 | #ifdef CONFIG_FIT_SIGNATURE |
99 | fprintf(stderr, | |
f1ca1fde | 100 | "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n" |
f8f9107d | 101 | " -E => place data outside of the FIT structure\n" |
b0a487a4 SG |
102 | " -k => set directory containing private keys\n" |
103 | " -K => write public keys to this .dtb file\n" | |
104 | " -c => add comment in signature node\n" | |
105 | " -F => re-sign existing FIT image\n" | |
f8f9107d | 106 | " -p => place external data at a static position\n" |
f1ca1fde GM |
107 | " -r => mark keys used as 'required' in dtb\n" |
108 | " -N => engine to use for signing (pkcs11)\n"); | |
b0a487a4 SG |
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); | |
79aa33cd | 115 | fprintf(stderr, "Use '-T list' to see a list of available image types\n"); |
b0a487a4 SG |
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, | |
f1ca1fde | 146 | "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N: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; | |
f1ca1fde GM |
227 | case 'N': |
228 | params.engine_id = optarg; | |
229 | break; | |
07450081 SG |
230 | case 'O': |
231 | params.os = genimg_get_os_id(optarg); | |
51f03e6a SG |
232 | if (params.os < 0) { |
233 | show_valid_options(IH_OS); | |
15310348 | 234 | usage("Invalid operating system"); |
51f03e6a | 235 | } |
07450081 | 236 | break; |
f8f9107d TR |
237 | case 'p': |
238 | params.external_offset = strtoull(optarg, &ptr, 16); | |
239 | if (*ptr) { | |
240 | fprintf(stderr, "%s: invalid offset size %s\n", | |
241 | params.cmdname, optarg); | |
242 | exit(EXIT_FAILURE); | |
243 | } | |
b6fefa76 | 244 | break; |
bd6e1420 SG |
245 | case 'q': |
246 | params.quiet = 1; | |
247 | break; | |
a02221f2 SG |
248 | case 'r': |
249 | params.require_keys = 1; | |
250 | break; | |
251 | case 'R': | |
252 | /* | |
253 | * This entry is for the second configuration | |
254 | * file, if only one is not enough. | |
255 | */ | |
256 | params.imagename2 = optarg; | |
257 | break; | |
258 | case 's': | |
259 | params.skipcpy = 1; | |
260 | break; | |
07450081 | 261 | case 'T': |
79aa33cd BS |
262 | if (strcmp(optarg, "list") == 0) { |
263 | show_valid_options(IH_TYPE); | |
264 | exit(EXIT_SUCCESS); | |
265 | } | |
d505a09c SG |
266 | type = genimg_get_type_id(optarg); |
267 | if (type < 0) { | |
f24e1050 | 268 | show_valid_options(IH_TYPE); |
15310348 | 269 | usage("Invalid image type"); |
07450081 SG |
270 | } |
271 | break; | |
a02221f2 SG |
272 | case 'v': |
273 | params.vflag++; | |
274 | break; | |
275 | case 'V': | |
276 | printf("mkimage version %s\n", PLAIN_VERSION); | |
277 | exit(EXIT_SUCCESS); | |
278 | case 'x': | |
279 | params.xflag++; | |
280 | break; | |
281 | default: | |
15310348 | 282 | usage("Invalid option"); |
5b1d7137 | 283 | } |
5b1d7137 WD |
284 | } |
285 | ||
8edeac86 AB |
286 | /* The last parameter is expected to be the imagefile */ |
287 | if (optind < argc) | |
7a439cad AB |
288 | params.imagefile = argv[optind]; |
289 | ||
d505a09c SG |
290 | /* |
291 | * For auto-generated FIT images we need to know the image type to put | |
292 | * in the FIT, which is separate from the file's image type (which | |
293 | * will always be IH_TYPE_FLATDT in this case). | |
294 | */ | |
295 | if (params.type == IH_TYPE_FLATDT) { | |
20deaddd | 296 | params.fit_image_type = type ? type : IH_TYPE_KERNEL; |
3c23c0fe | 297 | /* For auto_its, datafile is always 'auto' */ |
8e35bb07 SG |
298 | if (!params.auto_its) |
299 | params.datafile = datafile; | |
e324a925 SG |
300 | else if (!params.datafile) |
301 | usage("Missing data file for auto-FIT (use -d)"); | |
d505a09c | 302 | } else if (type != IH_TYPE_INVALID) { |
8c84287a AK |
303 | if (type == IH_TYPE_SCRIPT && !params.datafile) |
304 | usage("Missing data file for script (use -d)"); | |
d505a09c SG |
305 | params.type = type; |
306 | } | |
307 | ||
308 | if (!params.imagefile) | |
15310348 | 309 | usage("Missing output filename"); |
0b443dee SG |
310 | } |
311 | ||
0b443dee SG |
312 | int main(int argc, char **argv) |
313 | { | |
314 | int ifd = -1; | |
315 | struct stat sbuf; | |
316 | char *ptr; | |
317 | int retval = 0; | |
318 | struct image_type_params *tparams = NULL; | |
319 | int pad_len = 0; | |
320 | int dfd; | |
321 | ||
322 | params.cmdname = *argv; | |
323 | params.addr = 0; | |
324 | params.ep = 0; | |
325 | ||
326 | process_args(argc, argv); | |
89a4d6b1 PW |
327 | |
328 | /* set tparams as per input type_id */ | |
a93648d1 | 329 | tparams = imagetool_get_type(params.type); |
89a4d6b1 PW |
330 | if (tparams == NULL) { |
331 | fprintf (stderr, "%s: unsupported type %s\n", | |
332 | params.cmdname, genimg_get_type_name(params.type)); | |
333 | exit (EXIT_FAILURE); | |
334 | } | |
5b1d7137 | 335 | |
89a4d6b1 PW |
336 | /* |
337 | * check the passed arguments parameters meets the requirements | |
338 | * as per image type to be generated/listed | |
339 | */ | |
340 | if (tparams->check_params) | |
341 | if (tparams->check_params (¶ms)) | |
15310348 | 342 | usage("Bad parameters for image type"); |
89a4d6b1 PW |
343 | |
344 | if (!params.eflag) { | |
345 | params.ep = params.addr; | |
5b1d7137 | 346 | /* If XIP, entry point must be after the U-Boot header */ |
89a4d6b1 PW |
347 | if (params.xflag) |
348 | params.ep += tparams->header_size; | |
5b1d7137 WD |
349 | } |
350 | ||
c81296c1 PT |
351 | if (params.fflag){ |
352 | if (tparams->fflag_handle) | |
353 | /* | |
354 | * in some cases, some additional processing needs | |
355 | * to be done if fflag is defined | |
356 | * | |
357 | * For ex. fit_handle_file for Fit file support | |
358 | */ | |
359 | retval = tparams->fflag_handle(¶ms); | |
5b1d7137 | 360 | |
c81296c1 PT |
361 | if (retval != EXIT_SUCCESS) |
362 | exit (retval); | |
363 | } | |
364 | ||
365 | if (params.lflag || params.fflag) { | |
366 | ifd = open (params.imagefile, O_RDONLY|O_BINARY); | |
367 | } else { | |
368 | ifd = open (params.imagefile, | |
369 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); | |
370 | } | |
371 | ||
372 | if (ifd < 0) { | |
373 | fprintf (stderr, "%s: Can't open %s: %s\n", | |
374 | params.cmdname, params.imagefile, | |
375 | strerror(errno)); | |
376 | exit (EXIT_FAILURE); | |
5b1d7137 WD |
377 | } |
378 | ||
c81296c1 | 379 | if (params.lflag || params.fflag) { |
5b1d7137 WD |
380 | /* |
381 | * list header information of existing image | |
382 | */ | |
383 | if (fstat(ifd, &sbuf) < 0) { | |
384 | fprintf (stderr, "%s: Can't stat %s: %s\n", | |
89a4d6b1 PW |
385 | params.cmdname, params.imagefile, |
386 | strerror(errno)); | |
5b1d7137 WD |
387 | exit (EXIT_FAILURE); |
388 | } | |
389 | ||
89a4d6b1 | 390 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
5b1d7137 | 391 | fprintf (stderr, |
89a4d6b1 PW |
392 | "%s: Bad size: \"%s\" is not valid image\n", |
393 | params.cmdname, params.imagefile); | |
5b1d7137 WD |
394 | exit (EXIT_FAILURE); |
395 | } | |
396 | ||
fa956fde MF |
397 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); |
398 | if (ptr == MAP_FAILED) { | |
5b1d7137 | 399 | fprintf (stderr, "%s: Can't read %s: %s\n", |
89a4d6b1 PW |
400 | params.cmdname, params.imagefile, |
401 | strerror(errno)); | |
5b1d7137 WD |
402 | exit (EXIT_FAILURE); |
403 | } | |
404 | ||
89a4d6b1 PW |
405 | /* |
406 | * scan through mkimage registry for all supported image types | |
407 | * and verify the input image file header for match | |
408 | * Print the image information for matched image type | |
409 | * Returns the error code if not matched | |
410 | */ | |
0ca6691c GMF |
411 | retval = imagetool_verify_print_header(ptr, &sbuf, |
412 | tparams, ¶ms); | |
5b1d7137 | 413 | |
5b1d7137 WD |
414 | (void) munmap((void *)ptr, sbuf.st_size); |
415 | (void) close (ifd); | |
416 | ||
f7644c0b | 417 | exit (retval); |
5b1d7137 WD |
418 | } |
419 | ||
34633141 | 420 | if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) { |
6ae6e160 PDS |
421 | dfd = open(params.datafile, O_RDONLY | O_BINARY); |
422 | if (dfd < 0) { | |
423 | fprintf(stderr, "%s: Can't open %s: %s\n", | |
424 | params.cmdname, params.datafile, | |
425 | strerror(errno)); | |
426 | exit(EXIT_FAILURE); | |
427 | } | |
92a655c3 | 428 | |
6ae6e160 PDS |
429 | if (fstat(dfd, &sbuf) < 0) { |
430 | fprintf(stderr, "%s: Can't stat %s: %s\n", | |
431 | params.cmdname, params.datafile, | |
432 | strerror(errno)); | |
433 | exit(EXIT_FAILURE); | |
434 | } | |
92a655c3 | 435 | |
6ae6e160 PDS |
436 | params.file_size = sbuf.st_size + tparams->header_size; |
437 | close(dfd); | |
438 | } | |
92a655c3 | 439 | |
5b1d7137 | 440 | /* |
f0662105 SB |
441 | * In case there an header with a variable |
442 | * length will be added, the corresponding | |
443 | * function is called. This is responsible to | |
444 | * allocate memory for the header itself. | |
5b1d7137 | 445 | */ |
f0662105 | 446 | if (tparams->vrec_header) |
9bac0bb3 | 447 | pad_len = tparams->vrec_header(¶ms, tparams); |
f0662105 SB |
448 | else |
449 | memset(tparams->hdr, 0, tparams->header_size); | |
5b1d7137 | 450 | |
89a4d6b1 PW |
451 | if (write(ifd, tparams->hdr, tparams->header_size) |
452 | != tparams->header_size) { | |
5b1d7137 | 453 | fprintf (stderr, "%s: Write error on %s: %s\n", |
89a4d6b1 | 454 | params.cmdname, params.imagefile, strerror(errno)); |
5b1d7137 WD |
455 | exit (EXIT_FAILURE); |
456 | } | |
457 | ||
d1be8f92 CR |
458 | if (!params.skipcpy) { |
459 | if (params.type == IH_TYPE_MULTI || | |
460 | params.type == IH_TYPE_SCRIPT) { | |
461 | char *file = params.datafile; | |
462 | uint32_t size; | |
463 | ||
464 | for (;;) { | |
465 | char *sep = NULL; | |
466 | ||
467 | if (file) { | |
468 | if ((sep = strchr(file, ':')) != NULL) { | |
469 | *sep = '\0'; | |
470 | } | |
471 | ||
472 | if (stat (file, &sbuf) < 0) { | |
473 | fprintf (stderr, "%s: Can't stat %s: %s\n", | |
474 | params.cmdname, file, strerror(errno)); | |
475 | exit (EXIT_FAILURE); | |
476 | } | |
477 | size = cpu_to_uimage (sbuf.st_size); | |
478 | } else { | |
479 | size = 0; | |
5b1d7137 WD |
480 | } |
481 | ||
d1be8f92 CR |
482 | if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { |
483 | fprintf (stderr, "%s: Write error on %s: %s\n", | |
484 | params.cmdname, params.imagefile, | |
485 | strerror(errno)); | |
5b1d7137 WD |
486 | exit (EXIT_FAILURE); |
487 | } | |
5b1d7137 | 488 | |
d1be8f92 CR |
489 | if (!file) { |
490 | break; | |
491 | } | |
5b1d7137 | 492 | |
d1be8f92 CR |
493 | if (sep) { |
494 | *sep = ':'; | |
495 | file = sep + 1; | |
496 | } else { | |
497 | file = NULL; | |
498 | } | |
5b1d7137 WD |
499 | } |
500 | ||
d1be8f92 | 501 | file = params.datafile; |
5b1d7137 | 502 | |
d1be8f92 CR |
503 | for (;;) { |
504 | char *sep = strchr(file, ':'); | |
505 | if (sep) { | |
506 | *sep = '\0'; | |
507 | copy_file (ifd, file, 1); | |
508 | *sep++ = ':'; | |
509 | file = sep; | |
510 | } else { | |
511 | copy_file (ifd, file, 0); | |
512 | break; | |
513 | } | |
5b1d7137 | 514 | } |
5d898a00 SX |
515 | } else if (params.type == IH_TYPE_PBLIMAGE) { |
516 | /* PBL has special Image format, implements its' own */ | |
517 | pbl_load_uboot(ifd, ¶ms); | |
6915dcf3 AG |
518 | } else if (params.type == IH_TYPE_ZYNQMPBIF) { |
519 | /* Image file is meta, walk through actual targets */ | |
520 | int ret; | |
521 | ||
522 | ret = zynqmpbif_copy_image(ifd, ¶ms); | |
523 | if (ret) | |
524 | return ret; | |
d1be8f92 | 525 | } else { |
9bac0bb3 | 526 | copy_file(ifd, params.datafile, pad_len); |
5b1d7137 | 527 | } |
d21bd69b SE |
528 | if (params.type == IH_TYPE_FIRMWARE_IVT) { |
529 | /* Add alignment and IVT */ | |
530 | uint32_t aligned_filesize = (params.file_size + 0x1000 | |
531 | - 1) & ~(0x1000 - 1); | |
532 | flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 }, | |
533 | params.addr, 0, 0, 0, params.addr | |
534 | + aligned_filesize | |
535 | - tparams->header_size, | |
536 | params.addr + aligned_filesize | |
537 | - tparams->header_size | |
538 | + 0x20, 0 }; | |
539 | int i = params.file_size; | |
540 | for (; i < aligned_filesize; i++) { | |
b4e923a8 | 541 | if (write(ifd, (char *) &i, 1) != 1) { |
d21bd69b SE |
542 | fprintf(stderr, |
543 | "%s: Write error on %s: %s\n", | |
544 | params.cmdname, | |
545 | params.imagefile, | |
546 | strerror(errno)); | |
547 | exit(EXIT_FAILURE); | |
548 | } | |
549 | } | |
550 | if (write(ifd, &ivt_header, sizeof(flash_header_v2_t)) | |
551 | != sizeof(flash_header_v2_t)) { | |
552 | fprintf(stderr, "%s: Write error on %s: %s\n", | |
553 | params.cmdname, | |
554 | params.imagefile, | |
555 | strerror(errno)); | |
556 | exit(EXIT_FAILURE); | |
557 | } | |
558 | } | |
5b1d7137 WD |
559 | } |
560 | ||
561 | /* We're a bit of paranoid */ | |
89a4d6b1 PW |
562 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
563 | !defined(__sun__) && \ | |
564 | !defined(__FreeBSD__) && \ | |
31cbe80c | 565 | !defined(__OpenBSD__) && \ |
89a4d6b1 | 566 | !defined(__APPLE__) |
5b1d7137 WD |
567 | (void) fdatasync (ifd); |
568 | #else | |
569 | (void) fsync (ifd); | |
570 | #endif | |
571 | ||
572 | if (fstat(ifd, &sbuf) < 0) { | |
573 | fprintf (stderr, "%s: Can't stat %s: %s\n", | |
89a4d6b1 | 574 | params.cmdname, params.imagefile, strerror(errno)); |
5b1d7137 WD |
575 | exit (EXIT_FAILURE); |
576 | } | |
92a655c3 | 577 | params.file_size = sbuf.st_size; |
5b1d7137 | 578 | |
fa956fde MF |
579 | ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); |
580 | if (ptr == MAP_FAILED) { | |
5b1d7137 | 581 | fprintf (stderr, "%s: Can't map %s: %s\n", |
89a4d6b1 | 582 | params.cmdname, params.imagefile, strerror(errno)); |
5b1d7137 WD |
583 | exit (EXIT_FAILURE); |
584 | } | |
585 | ||
89a4d6b1 PW |
586 | /* Setup the image header as per input image type*/ |
587 | if (tparams->set_header) | |
588 | tparams->set_header (ptr, &sbuf, ifd, ¶ms); | |
589 | else { | |
590 | fprintf (stderr, "%s: Can't set header for %s: %s\n", | |
591 | params.cmdname, tparams->name, strerror(errno)); | |
592 | exit (EXIT_FAILURE); | |
593 | } | |
5b1d7137 | 594 | |
89a4d6b1 PW |
595 | /* Print the image information by processing image header */ |
596 | if (tparams->print_header) | |
597 | tparams->print_header (ptr); | |
598 | else { | |
004d0091 GG |
599 | fprintf (stderr, "%s: Can't print header for %s\n", |
600 | params.cmdname, tparams->name); | |
89a4d6b1 | 601 | } |
5b1d7137 WD |
602 | |
603 | (void) munmap((void *)ptr, sbuf.st_size); | |
604 | ||
605 | /* We're a bit of paranoid */ | |
89a4d6b1 PW |
606 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
607 | !defined(__sun__) && \ | |
608 | !defined(__FreeBSD__) && \ | |
31cbe80c | 609 | !defined(__OpenBSD__) && \ |
89a4d6b1 | 610 | !defined(__APPLE__) |
5b1d7137 WD |
611 | (void) fdatasync (ifd); |
612 | #else | |
613 | (void) fsync (ifd); | |
614 | #endif | |
615 | ||
616 | if (close(ifd)) { | |
617 | fprintf (stderr, "%s: Write error on %s: %s\n", | |
89a4d6b1 | 618 | params.cmdname, params.imagefile, strerror(errno)); |
5b1d7137 WD |
619 | exit (EXIT_FAILURE); |
620 | } | |
621 | ||
622 | exit (EXIT_SUCCESS); | |
623 | } | |
624 | ||
625 | static void | |
626 | copy_file (int ifd, const char *datafile, int pad) | |
627 | { | |
628 | int dfd; | |
629 | struct stat sbuf; | |
630 | unsigned char *ptr; | |
631 | int tail; | |
632 | int zero = 0; | |
9bac0bb3 | 633 | uint8_t zeros[4096]; |
5b1d7137 WD |
634 | int offset = 0; |
635 | int size; | |
a93648d1 | 636 | struct image_type_params *tparams = imagetool_get_type(params.type); |
5b1d7137 | 637 | |
9bac0bb3 SB |
638 | memset(zeros, 0, sizeof(zeros)); |
639 | ||
89a4d6b1 | 640 | if (params.vflag) { |
5b1d7137 WD |
641 | fprintf (stderr, "Adding Image %s\n", datafile); |
642 | } | |
643 | ||
ef1464cc | 644 | if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { |
5b1d7137 | 645 | fprintf (stderr, "%s: Can't open %s: %s\n", |
89a4d6b1 | 646 | params.cmdname, datafile, strerror(errno)); |
5b1d7137 WD |
647 | exit (EXIT_FAILURE); |
648 | } | |
649 | ||
650 | if (fstat(dfd, &sbuf) < 0) { | |
651 | fprintf (stderr, "%s: Can't stat %s: %s\n", | |
89a4d6b1 | 652 | params.cmdname, datafile, strerror(errno)); |
5b1d7137 WD |
653 | exit (EXIT_FAILURE); |
654 | } | |
655 | ||
fa956fde MF |
656 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
657 | if (ptr == MAP_FAILED) { | |
5b1d7137 | 658 | fprintf (stderr, "%s: Can't read %s: %s\n", |
89a4d6b1 | 659 | params.cmdname, datafile, strerror(errno)); |
5b1d7137 WD |
660 | exit (EXIT_FAILURE); |
661 | } | |
662 | ||
89a4d6b1 | 663 | if (params.xflag) { |
5b1d7137 WD |
664 | unsigned char *p = NULL; |
665 | /* | |
666 | * XIP: do not append the image_header_t at the | |
667 | * beginning of the file, but consume the space | |
668 | * reserved for it. | |
669 | */ | |
670 | ||
89a4d6b1 | 671 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
5b1d7137 WD |
672 | fprintf (stderr, |
673 | "%s: Bad size: \"%s\" is too small for XIP\n", | |
89a4d6b1 | 674 | params.cmdname, datafile); |
5b1d7137 WD |
675 | exit (EXIT_FAILURE); |
676 | } | |
677 | ||
89a4d6b1 | 678 | for (p = ptr; p < ptr + tparams->header_size; p++) { |
5b1d7137 WD |
679 | if ( *p != 0xff ) { |
680 | fprintf (stderr, | |
681 | "%s: Bad file: \"%s\" has invalid buffer for XIP\n", | |
89a4d6b1 | 682 | params.cmdname, datafile); |
5b1d7137 WD |
683 | exit (EXIT_FAILURE); |
684 | } | |
685 | } | |
686 | ||
89a4d6b1 | 687 | offset = tparams->header_size; |
5b1d7137 WD |
688 | } |
689 | ||
690 | size = sbuf.st_size - offset; | |
691 | if (write(ifd, ptr + offset, size) != size) { | |
692 | fprintf (stderr, "%s: Write error on %s: %s\n", | |
89a4d6b1 | 693 | params.cmdname, params.imagefile, strerror(errno)); |
5b1d7137 WD |
694 | exit (EXIT_FAILURE); |
695 | } | |
696 | ||
9bac0bb3 SB |
697 | tail = size % 4; |
698 | if ((pad == 1) && (tail != 0)) { | |
5b1d7137 WD |
699 | |
700 | if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { | |
701 | fprintf (stderr, "%s: Write error on %s: %s\n", | |
89a4d6b1 PW |
702 | params.cmdname, params.imagefile, |
703 | strerror(errno)); | |
5b1d7137 WD |
704 | exit (EXIT_FAILURE); |
705 | } | |
9bac0bb3 | 706 | } else if (pad > 1) { |
424b86ae SG |
707 | while (pad > 0) { |
708 | int todo = sizeof(zeros); | |
709 | ||
710 | if (todo > pad) | |
711 | todo = pad; | |
712 | if (write(ifd, (char *)&zeros, todo) != todo) { | |
713 | fprintf(stderr, "%s: Write error on %s: %s\n", | |
714 | params.cmdname, params.imagefile, | |
715 | strerror(errno)); | |
716 | exit(EXIT_FAILURE); | |
717 | } | |
718 | pad -= todo; | |
9bac0bb3 | 719 | } |
5b1d7137 WD |
720 | } |
721 | ||
722 | (void) munmap((void *)ptr, sbuf.st_size); | |
723 | (void) close (dfd); | |
724 | } |