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