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