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