blob: 1cc3a639815a052280579a7099621cf37ba6ff11 [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Bartlomiej Sieka9d254382008-03-11 12:34:47 +01002 * (C) Copyright 2008 Semihalf
3 *
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +05304 * (C) Copyright 2000-2009
wdenk5b1d7132002-11-03 00:07:02 +00005 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
wdenk0c852a22004-02-26 23:01:04 +00007 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00009 */
10
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010011#include "mkimage.h"
wdenk5b1d7132002-11-03 00:07:02 +000012#include <image.h>
Wolfgang Denk976b38c2011-02-12 10:37:11 +010013#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000014
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053015static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000016
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053017/* parameters initialized by core will be used by the image type code */
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -070018struct image_tool_params params = {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053019 .os = IH_OS_LINUX,
20 .arch = IH_ARCH_PPC,
21 .type = IH_TYPE_KERNEL,
22 .comp = IH_COMP_GZIP,
23 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
Wolfgang Denk04387d22010-03-27 23:37:46 +010024 .imagename = "",
Shaohui Xie5d898a02012-08-10 02:49:35 +000025 .imagename2 = "",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053026};
wdenk5b1d7132002-11-03 00:07:02 +000027
Simon Glass5b9d44d2015-06-23 15:38:25 -060028static int h_compare_image_name(const void *vtype1, const void *vtype2)
29{
30 const int *type1 = vtype1;
31 const int *type2 = vtype2;
32 const char *name1 = genimg_get_type_short_name(*type1);
33 const char *name2 = genimg_get_type_short_name(*type2);
34
35 return strcmp(name1, name2);
36}
37
38/* Show all image types supported by mkimage */
39static void show_image_types(void)
40{
41 struct image_type_params *tparams;
42 int order[IH_TYPE_COUNT];
43 int count;
44 int type;
45 int i;
46
47 /* Sort the names in order of short name for easier reading */
48 memset(order, '\0', sizeof(order));
49 for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
50 tparams = imagetool_get_type(type);
51 if (tparams)
52 order[count++] = type;
53 }
54 qsort(order, count, sizeof(int), h_compare_image_name);
55
56 fprintf(stderr, "\nInvalid image type. Supported image types:\n");
57 for (i = 0; i < count; i++) {
58 type = order[i];
59 tparams = imagetool_get_type(type);
60 if (tparams) {
61 fprintf(stderr, "\t%-15s %s\n",
62 genimg_get_type_short_name(type),
63 genimg_get_type_name(type));
64 }
65 }
66 fprintf(stderr, "\n");
67}
68
Simon Glassb0a487a2016-02-22 22:55:36 -070069static void usage(void)
70{
71 fprintf(stderr, "Usage: %s -l image\n"
72 " -l ==> list image header information\n",
73 params.cmdname);
74 fprintf(stderr,
75 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
76 " -A ==> set architecture to 'arch'\n"
77 " -O ==> set operating system to 'os'\n"
78 " -T ==> set image type to 'type'\n"
79 " -C ==> set compression type 'comp'\n"
80 " -a ==> set load address to 'addr' (hex)\n"
81 " -e ==> set entry point to 'ep' (hex)\n"
82 " -n ==> set image name to 'name'\n"
83 " -d ==> use image data from 'datafile'\n"
84 " -x ==> set XIP (execute in place)\n",
85 params.cmdname);
86 fprintf(stderr,
87 " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
88 params.cmdname);
89 fprintf(stderr,
90 " -D => set all options for device tree compiler\n"
91 " -f => input filename for FIT source\n");
92#ifdef CONFIG_FIT_SIGNATURE
93 fprintf(stderr,
94 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
95 " -k => set directory containing private keys\n"
96 " -K => write public keys to this .dtb file\n"
97 " -c => add comment in signature node\n"
98 " -F => re-sign existing FIT image\n"
99 " -r => mark keys used as 'required' in dtb\n");
100#else
101 fprintf(stderr,
102 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
103#endif
104 fprintf(stderr, " %s -V ==> print version information and exit\n",
105 params.cmdname);
106 fprintf(stderr, "Use -T to see a list of available image types\n");
107
108 exit(EXIT_FAILURE);
109}
110
Simon Glass0b443de2016-02-22 22:55:33 -0700111static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000112{
Peter Tysera2513e22010-04-04 22:36:03 -0500113 char *ptr;
Simon Glassa02221f2016-02-22 22:55:34 -0700114 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000115
Simon Glassa02221f2016-02-22 22:55:34 -0700116 while ((opt = getopt(argc, argv,
117 "a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
118 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700119 case 'a':
120 params.addr = strtoull(optarg, &ptr, 16);
121 if (*ptr) {
122 fprintf(stderr, "%s: invalid load address %s\n",
123 params.cmdname, optarg);
124 exit(EXIT_FAILURE);
125 }
Simon Glassa02221f2016-02-22 22:55:34 -0700126 break;
127 case 'A':
128 params.arch = genimg_get_arch_id(optarg);
129 if (params.arch < 0)
130 usage();
131 break;
132 case 'c':
133 params.comment = optarg;
134 break;
135 case 'C':
136 params.comp = genimg_get_comp_id(optarg);
137 if (params.comp < 0)
138 usage();
139 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700140 case 'd':
141 params.datafile = optarg;
142 params.dflag = 1;
143 break;
Simon Glass07450082016-02-22 22:55:35 -0700144 case 'D':
145 params.dtc = optarg;
146 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700147 case 'e':
148 params.ep = strtoull(optarg, &ptr, 16);
149 if (*ptr) {
150 fprintf(stderr, "%s: invalid entry point %s\n",
151 params.cmdname, optarg);
152 exit(EXIT_FAILURE);
153 }
154 params.eflag = 1;
155 break;
156 case 'f':
157 params.datafile = optarg;
158 /* no break */
159 case 'F':
160 /*
161 * The flattened image tree (FIT) format
162 * requires a flattened device tree image type
163 */
164 params.type = IH_TYPE_FLATDT;
165 params.fflag = 1;
166 break;
167 case 'k':
168 params.keydir = optarg;
169 break;
170 case 'K':
171 params.keydest = optarg;
172 break;
Simon Glass07450082016-02-22 22:55:35 -0700173 case 'l':
174 params.lflag = 1;
175 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700176 case 'n':
177 params.imagename = optarg;
178 break;
Simon Glass07450082016-02-22 22:55:35 -0700179 case 'O':
180 params.os = genimg_get_os_id(optarg);
181 if (params.os < 0)
182 usage();
183 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700184 case 'r':
185 params.require_keys = 1;
186 break;
187 case 'R':
188 /*
189 * This entry is for the second configuration
190 * file, if only one is not enough.
191 */
192 params.imagename2 = optarg;
193 break;
194 case 's':
195 params.skipcpy = 1;
196 break;
Simon Glass07450082016-02-22 22:55:35 -0700197 case 'T':
198 params.type = genimg_get_type_id(optarg);
199 if (params.type < 0) {
200 show_image_types();
201 usage();
202 }
203 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700204 case 'v':
205 params.vflag++;
206 break;
207 case 'V':
208 printf("mkimage version %s\n", PLAIN_VERSION);
209 exit(EXIT_SUCCESS);
210 case 'x':
211 params.xflag++;
212 break;
213 default:
214 usage();
wdenk5b1d7132002-11-03 00:07:02 +0000215 }
wdenk5b1d7132002-11-03 00:07:02 +0000216 }
217
Simon Glassa02221f2016-02-22 22:55:34 -0700218 if (optind >= argc)
Simon Glass0b443de2016-02-22 22:55:33 -0700219 usage();
Simon Glassa02221f2016-02-22 22:55:34 -0700220 params.imagefile = argv[optind];
Simon Glass0b443de2016-02-22 22:55:33 -0700221}
222
223
224int main(int argc, char **argv)
225{
226 int ifd = -1;
227 struct stat sbuf;
228 char *ptr;
229 int retval = 0;
230 struct image_type_params *tparams = NULL;
231 int pad_len = 0;
232 int dfd;
233
234 params.cmdname = *argv;
235 params.addr = 0;
236 params.ep = 0;
237
238 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000239
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530240 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200241 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530242 if (tparams == NULL) {
243 fprintf (stderr, "%s: unsupported type %s\n",
244 params.cmdname, genimg_get_type_name(params.type));
245 exit (EXIT_FAILURE);
246 }
247
248 /*
249 * check the passed arguments parameters meets the requirements
250 * as per image type to be generated/listed
251 */
252 if (tparams->check_params)
253 if (tparams->check_params (&params))
Simon Glass0b443de2016-02-22 22:55:33 -0700254 usage();
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530255
256 if (!params.eflag) {
257 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000258 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530259 if (params.xflag)
260 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000261 }
262
Peter Tyserc81296c2009-11-24 16:42:10 -0600263 if (params.fflag){
264 if (tparams->fflag_handle)
265 /*
266 * in some cases, some additional processing needs
267 * to be done if fflag is defined
268 *
269 * For ex. fit_handle_file for Fit file support
270 */
271 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000272
Peter Tyserc81296c2009-11-24 16:42:10 -0600273 if (retval != EXIT_SUCCESS)
274 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000275 }
276
Peter Tyserc81296c2009-11-24 16:42:10 -0600277 if (params.lflag || params.fflag) {
278 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
279 } else {
280 ifd = open (params.imagefile,
281 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
282 }
283
284 if (ifd < 0) {
285 fprintf (stderr, "%s: Can't open %s: %s\n",
286 params.cmdname, params.imagefile,
287 strerror(errno));
288 exit (EXIT_FAILURE);
289 }
290
291 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000292 /*
293 * list header information of existing image
294 */
295 if (fstat(ifd, &sbuf) < 0) {
296 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530297 params.cmdname, params.imagefile,
298 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000299 exit (EXIT_FAILURE);
300 }
301
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530302 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000303 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530304 "%s: Bad size: \"%s\" is not valid image\n",
305 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000306 exit (EXIT_FAILURE);
307 }
308
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400309 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
310 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000311 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530312 params.cmdname, params.imagefile,
313 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000314 exit (EXIT_FAILURE);
315 }
316
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530317 /*
318 * scan through mkimage registry for all supported image types
319 * and verify the input image file header for match
320 * Print the image information for matched image type
321 * Returns the error code if not matched
322 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200323 retval = imagetool_verify_print_header(ptr, &sbuf,
324 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000325
wdenk5b1d7132002-11-03 00:07:02 +0000326 (void) munmap((void *)ptr, sbuf.st_size);
327 (void) close (ifd);
328
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530329 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000330 }
331
Marek Vasut34633142015-12-07 18:01:54 +0100332 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200333 dfd = open(params.datafile, O_RDONLY | O_BINARY);
334 if (dfd < 0) {
335 fprintf(stderr, "%s: Can't open %s: %s\n",
336 params.cmdname, params.datafile,
337 strerror(errno));
338 exit(EXIT_FAILURE);
339 }
Simon Glass92a655c2015-06-23 15:39:12 -0600340
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200341 if (fstat(dfd, &sbuf) < 0) {
342 fprintf(stderr, "%s: Can't stat %s: %s\n",
343 params.cmdname, params.datafile,
344 strerror(errno));
345 exit(EXIT_FAILURE);
346 }
Simon Glass92a655c2015-06-23 15:39:12 -0600347
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200348 params.file_size = sbuf.st_size + tparams->header_size;
349 close(dfd);
350 }
Simon Glass92a655c2015-06-23 15:39:12 -0600351
wdenk5b1d7132002-11-03 00:07:02 +0000352 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000353 * In case there an header with a variable
354 * length will be added, the corresponding
355 * function is called. This is responsible to
356 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000357 */
Stefano Babicf0662102011-09-15 23:50:16 +0000358 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200359 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000360 else
361 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000362
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530363 if (write(ifd, tparams->hdr, tparams->header_size)
364 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000365 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530366 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000367 exit (EXIT_FAILURE);
368 }
369
Christian Rieschd1be8f92011-12-09 09:47:38 +0000370 if (!params.skipcpy) {
371 if (params.type == IH_TYPE_MULTI ||
372 params.type == IH_TYPE_SCRIPT) {
373 char *file = params.datafile;
374 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000375
Christian Rieschd1be8f92011-12-09 09:47:38 +0000376 for (;;) {
377 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000378
Christian Rieschd1be8f92011-12-09 09:47:38 +0000379 if (file) {
380 if ((sep = strchr(file, ':')) != NULL) {
381 *sep = '\0';
382 }
383
384 if (stat (file, &sbuf) < 0) {
385 fprintf (stderr, "%s: Can't stat %s: %s\n",
386 params.cmdname, file, strerror(errno));
387 exit (EXIT_FAILURE);
388 }
389 size = cpu_to_uimage (sbuf.st_size);
390 } else {
391 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000392 }
393
Christian Rieschd1be8f92011-12-09 09:47:38 +0000394 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
395 fprintf (stderr, "%s: Write error on %s: %s\n",
396 params.cmdname, params.imagefile,
397 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000398 exit (EXIT_FAILURE);
399 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000400
401 if (!file) {
402 break;
403 }
404
405 if (sep) {
406 *sep = ':';
407 file = sep + 1;
408 } else {
409 file = NULL;
410 }
wdenk5b1d7132002-11-03 00:07:02 +0000411 }
412
Christian Rieschd1be8f92011-12-09 09:47:38 +0000413 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000414
Christian Rieschd1be8f92011-12-09 09:47:38 +0000415 for (;;) {
416 char *sep = strchr(file, ':');
417 if (sep) {
418 *sep = '\0';
419 copy_file (ifd, file, 1);
420 *sep++ = ':';
421 file = sep;
422 } else {
423 copy_file (ifd, file, 0);
424 break;
425 }
wdenk5b1d7132002-11-03 00:07:02 +0000426 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000427 } else if (params.type == IH_TYPE_PBLIMAGE) {
428 /* PBL has special Image format, implements its' own */
429 pbl_load_uboot(ifd, &params);
Christian Rieschd1be8f92011-12-09 09:47:38 +0000430 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200431 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000432 }
wdenk5b1d7132002-11-03 00:07:02 +0000433 }
434
435 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530436#if defined(_POSIX_SYNCHRONIZED_IO) && \
437 !defined(__sun__) && \
438 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200439 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530440 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000441 (void) fdatasync (ifd);
442#else
443 (void) fsync (ifd);
444#endif
445
446 if (fstat(ifd, &sbuf) < 0) {
447 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530448 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000449 exit (EXIT_FAILURE);
450 }
Simon Glass92a655c2015-06-23 15:39:12 -0600451 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000452
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400453 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
454 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000455 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530456 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000457 exit (EXIT_FAILURE);
458 }
459
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530460 /* Setup the image header as per input image type*/
461 if (tparams->set_header)
462 tparams->set_header (ptr, &sbuf, ifd, &params);
463 else {
464 fprintf (stderr, "%s: Can't set header for %s: %s\n",
465 params.cmdname, tparams->name, strerror(errno));
466 exit (EXIT_FAILURE);
467 }
wdenk5b1d7132002-11-03 00:07:02 +0000468
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530469 /* Print the image information by processing image header */
470 if (tparams->print_header)
471 tparams->print_header (ptr);
472 else {
473 fprintf (stderr, "%s: Can't print header for %s: %s\n",
474 params.cmdname, tparams->name, strerror(errno));
475 exit (EXIT_FAILURE);
476 }
wdenk5b1d7132002-11-03 00:07:02 +0000477
478 (void) munmap((void *)ptr, sbuf.st_size);
479
480 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530481#if defined(_POSIX_SYNCHRONIZED_IO) && \
482 !defined(__sun__) && \
483 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200484 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530485 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000486 (void) fdatasync (ifd);
487#else
488 (void) fsync (ifd);
489#endif
490
491 if (close(ifd)) {
492 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530493 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000494 exit (EXIT_FAILURE);
495 }
496
497 exit (EXIT_SUCCESS);
498}
499
500static void
501copy_file (int ifd, const char *datafile, int pad)
502{
503 int dfd;
504 struct stat sbuf;
505 unsigned char *ptr;
506 int tail;
507 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200508 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000509 int offset = 0;
510 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200511 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000512
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200513 memset(zeros, 0, sizeof(zeros));
514
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530515 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000516 fprintf (stderr, "Adding Image %s\n", datafile);
517 }
518
wdenkef1464c2003-10-08 22:14:02 +0000519 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000520 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530521 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000522 exit (EXIT_FAILURE);
523 }
524
525 if (fstat(dfd, &sbuf) < 0) {
526 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530527 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000528 exit (EXIT_FAILURE);
529 }
530
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400531 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
532 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000533 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530534 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000535 exit (EXIT_FAILURE);
536 }
537
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530538 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000539 unsigned char *p = NULL;
540 /*
541 * XIP: do not append the image_header_t at the
542 * beginning of the file, but consume the space
543 * reserved for it.
544 */
545
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530546 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000547 fprintf (stderr,
548 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530549 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000550 exit (EXIT_FAILURE);
551 }
552
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530553 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000554 if ( *p != 0xff ) {
555 fprintf (stderr,
556 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530557 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000558 exit (EXIT_FAILURE);
559 }
560 }
561
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530562 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000563 }
564
565 size = sbuf.st_size - offset;
566 if (write(ifd, ptr + offset, size) != size) {
567 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530568 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000569 exit (EXIT_FAILURE);
570 }
571
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200572 tail = size % 4;
573 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000574
575 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
576 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530577 params.cmdname, params.imagefile,
578 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000579 exit (EXIT_FAILURE);
580 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200581 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600582 while (pad > 0) {
583 int todo = sizeof(zeros);
584
585 if (todo > pad)
586 todo = pad;
587 if (write(ifd, (char *)&zeros, todo) != todo) {
588 fprintf(stderr, "%s: Write error on %s: %s\n",
589 params.cmdname, params.imagefile,
590 strerror(errno));
591 exit(EXIT_FAILURE);
592 }
593 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200594 }
wdenk5b1d7132002-11-03 00:07:02 +0000595 }
596
597 (void) munmap((void *)ptr, sbuf.st_size);
598 (void) close (dfd);
599}