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