blob: e1900bd7ab1efd7c1871c8c2ed037700d2c9d186 [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 */
Simon Glasscc7a6442016-02-22 22:55:38 -070018static struct 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 Glass15310342016-02-22 22:55:37 -070069static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -070070{
Simon Glass15310342016-02-22 22:55:37 -070071 fprintf(stderr, "Error: %s\n", msg);
Simon Glassb0a487a2016-02-22 22:55:36 -070072 fprintf(stderr, "Usage: %s -l image\n"
73 " -l ==> list image header information\n",
74 params.cmdname);
75 fprintf(stderr,
76 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
77 " -A ==> set architecture to 'arch'\n"
78 " -O ==> set operating system to 'os'\n"
79 " -T ==> set image type to 'type'\n"
80 " -C ==> set compression type 'comp'\n"
81 " -a ==> set load address to 'addr' (hex)\n"
82 " -e ==> set entry point to 'ep' (hex)\n"
83 " -n ==> set image name to 'name'\n"
84 " -d ==> use image data from 'datafile'\n"
85 " -x ==> set XIP (execute in place)\n",
86 params.cmdname);
87 fprintf(stderr,
88 " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
89 params.cmdname);
90 fprintf(stderr,
91 " -D => set all options for device tree compiler\n"
92 " -f => input filename for FIT source\n");
93#ifdef CONFIG_FIT_SIGNATURE
94 fprintf(stderr,
95 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
96 " -k => set directory containing private keys\n"
97 " -K => write public keys to this .dtb file\n"
98 " -c => add comment in signature node\n"
99 " -F => re-sign existing FIT image\n"
100 " -r => mark keys used as 'required' in dtb\n");
101#else
102 fprintf(stderr,
103 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
104#endif
105 fprintf(stderr, " %s -V ==> print version information and exit\n",
106 params.cmdname);
107 fprintf(stderr, "Use -T to see a list of available image types\n");
108
109 exit(EXIT_FAILURE);
110}
111
Simon Glass0b443de2016-02-22 22:55:33 -0700112static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000113{
Peter Tysera2513e22010-04-04 22:36:03 -0500114 char *ptr;
Simon Glassa02221f2016-02-22 22:55:34 -0700115 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000116
Simon Glassa02221f2016-02-22 22:55:34 -0700117 while ((opt = getopt(argc, argv,
118 "a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
119 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700120 case 'a':
121 params.addr = strtoull(optarg, &ptr, 16);
122 if (*ptr) {
123 fprintf(stderr, "%s: invalid load address %s\n",
124 params.cmdname, optarg);
125 exit(EXIT_FAILURE);
126 }
Simon Glassa02221f2016-02-22 22:55:34 -0700127 break;
128 case 'A':
129 params.arch = genimg_get_arch_id(optarg);
130 if (params.arch < 0)
Simon Glass15310342016-02-22 22:55:37 -0700131 usage("Invalid architecture");
Simon Glassa02221f2016-02-22 22:55:34 -0700132 break;
133 case 'c':
134 params.comment = optarg;
135 break;
136 case 'C':
137 params.comp = genimg_get_comp_id(optarg);
138 if (params.comp < 0)
Simon Glass15310342016-02-22 22:55:37 -0700139 usage("Invalid compression type");
Simon Glassa02221f2016-02-22 22:55:34 -0700140 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700141 case 'd':
142 params.datafile = optarg;
143 params.dflag = 1;
144 break;
Simon Glass07450082016-02-22 22:55:35 -0700145 case 'D':
146 params.dtc = optarg;
147 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700148 case 'e':
149 params.ep = strtoull(optarg, &ptr, 16);
150 if (*ptr) {
151 fprintf(stderr, "%s: invalid entry point %s\n",
152 params.cmdname, optarg);
153 exit(EXIT_FAILURE);
154 }
155 params.eflag = 1;
156 break;
157 case 'f':
158 params.datafile = optarg;
159 /* no break */
160 case 'F':
161 /*
162 * The flattened image tree (FIT) format
163 * requires a flattened device tree image type
164 */
165 params.type = IH_TYPE_FLATDT;
166 params.fflag = 1;
167 break;
168 case 'k':
169 params.keydir = optarg;
170 break;
171 case 'K':
172 params.keydest = optarg;
173 break;
Simon Glass07450082016-02-22 22:55:35 -0700174 case 'l':
175 params.lflag = 1;
176 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700177 case 'n':
178 params.imagename = optarg;
179 break;
Simon Glass07450082016-02-22 22:55:35 -0700180 case 'O':
181 params.os = genimg_get_os_id(optarg);
182 if (params.os < 0)
Simon Glass15310342016-02-22 22:55:37 -0700183 usage("Invalid operating system");
Simon Glass07450082016-02-22 22:55:35 -0700184 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700185 case 'r':
186 params.require_keys = 1;
187 break;
188 case 'R':
189 /*
190 * This entry is for the second configuration
191 * file, if only one is not enough.
192 */
193 params.imagename2 = optarg;
194 break;
195 case 's':
196 params.skipcpy = 1;
197 break;
Simon Glass07450082016-02-22 22:55:35 -0700198 case 'T':
199 params.type = genimg_get_type_id(optarg);
200 if (params.type < 0) {
201 show_image_types();
Simon Glass15310342016-02-22 22:55:37 -0700202 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700203 }
204 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700205 case 'v':
206 params.vflag++;
207 break;
208 case 'V':
209 printf("mkimage version %s\n", PLAIN_VERSION);
210 exit(EXIT_SUCCESS);
211 case 'x':
212 params.xflag++;
213 break;
214 default:
Simon Glass15310342016-02-22 22:55:37 -0700215 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000216 }
wdenk5b1d7132002-11-03 00:07:02 +0000217 }
218
Simon Glassa02221f2016-02-22 22:55:34 -0700219 if (optind >= argc)
Simon Glass15310342016-02-22 22:55:37 -0700220 usage("Missing output filename");
Simon Glassa02221f2016-02-22 22:55:34 -0700221 params.imagefile = argv[optind];
Simon Glass0b443de2016-02-22 22:55:33 -0700222}
223
224
225int main(int argc, char **argv)
226{
227 int ifd = -1;
228 struct stat sbuf;
229 char *ptr;
230 int retval = 0;
231 struct image_type_params *tparams = NULL;
232 int pad_len = 0;
233 int dfd;
234
235 params.cmdname = *argv;
236 params.addr = 0;
237 params.ep = 0;
238
239 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000240
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530241 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200242 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530243 if (tparams == NULL) {
244 fprintf (stderr, "%s: unsupported type %s\n",
245 params.cmdname, genimg_get_type_name(params.type));
246 exit (EXIT_FAILURE);
247 }
248
249 /*
250 * check the passed arguments parameters meets the requirements
251 * as per image type to be generated/listed
252 */
253 if (tparams->check_params)
254 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700255 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530256
257 if (!params.eflag) {
258 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000259 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530260 if (params.xflag)
261 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000262 }
263
Peter Tyserc81296c2009-11-24 16:42:10 -0600264 if (params.fflag){
265 if (tparams->fflag_handle)
266 /*
267 * in some cases, some additional processing needs
268 * to be done if fflag is defined
269 *
270 * For ex. fit_handle_file for Fit file support
271 */
272 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000273
Peter Tyserc81296c2009-11-24 16:42:10 -0600274 if (retval != EXIT_SUCCESS)
275 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000276 }
277
Peter Tyserc81296c2009-11-24 16:42:10 -0600278 if (params.lflag || params.fflag) {
279 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
280 } else {
281 ifd = open (params.imagefile,
282 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
283 }
284
285 if (ifd < 0) {
286 fprintf (stderr, "%s: Can't open %s: %s\n",
287 params.cmdname, params.imagefile,
288 strerror(errno));
289 exit (EXIT_FAILURE);
290 }
291
292 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000293 /*
294 * list header information of existing image
295 */
296 if (fstat(ifd, &sbuf) < 0) {
297 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530298 params.cmdname, params.imagefile,
299 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000300 exit (EXIT_FAILURE);
301 }
302
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530303 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000304 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530305 "%s: Bad size: \"%s\" is not valid image\n",
306 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000307 exit (EXIT_FAILURE);
308 }
309
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400310 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
311 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000312 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530313 params.cmdname, params.imagefile,
314 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000315 exit (EXIT_FAILURE);
316 }
317
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530318 /*
319 * scan through mkimage registry for all supported image types
320 * and verify the input image file header for match
321 * Print the image information for matched image type
322 * Returns the error code if not matched
323 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200324 retval = imagetool_verify_print_header(ptr, &sbuf,
325 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000326
wdenk5b1d7132002-11-03 00:07:02 +0000327 (void) munmap((void *)ptr, sbuf.st_size);
328 (void) close (ifd);
329
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530330 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000331 }
332
Marek Vasut34633142015-12-07 18:01:54 +0100333 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200334 dfd = open(params.datafile, O_RDONLY | O_BINARY);
335 if (dfd < 0) {
336 fprintf(stderr, "%s: Can't open %s: %s\n",
337 params.cmdname, params.datafile,
338 strerror(errno));
339 exit(EXIT_FAILURE);
340 }
Simon Glass92a655c2015-06-23 15:39:12 -0600341
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200342 if (fstat(dfd, &sbuf) < 0) {
343 fprintf(stderr, "%s: Can't stat %s: %s\n",
344 params.cmdname, params.datafile,
345 strerror(errno));
346 exit(EXIT_FAILURE);
347 }
Simon Glass92a655c2015-06-23 15:39:12 -0600348
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200349 params.file_size = sbuf.st_size + tparams->header_size;
350 close(dfd);
351 }
Simon Glass92a655c2015-06-23 15:39:12 -0600352
wdenk5b1d7132002-11-03 00:07:02 +0000353 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000354 * In case there an header with a variable
355 * length will be added, the corresponding
356 * function is called. This is responsible to
357 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000358 */
Stefano Babicf0662102011-09-15 23:50:16 +0000359 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200360 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000361 else
362 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000363
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530364 if (write(ifd, tparams->hdr, tparams->header_size)
365 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000366 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530367 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000368 exit (EXIT_FAILURE);
369 }
370
Christian Rieschd1be8f92011-12-09 09:47:38 +0000371 if (!params.skipcpy) {
372 if (params.type == IH_TYPE_MULTI ||
373 params.type == IH_TYPE_SCRIPT) {
374 char *file = params.datafile;
375 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000376
Christian Rieschd1be8f92011-12-09 09:47:38 +0000377 for (;;) {
378 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000379
Christian Rieschd1be8f92011-12-09 09:47:38 +0000380 if (file) {
381 if ((sep = strchr(file, ':')) != NULL) {
382 *sep = '\0';
383 }
384
385 if (stat (file, &sbuf) < 0) {
386 fprintf (stderr, "%s: Can't stat %s: %s\n",
387 params.cmdname, file, strerror(errno));
388 exit (EXIT_FAILURE);
389 }
390 size = cpu_to_uimage (sbuf.st_size);
391 } else {
392 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000393 }
394
Christian Rieschd1be8f92011-12-09 09:47:38 +0000395 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
396 fprintf (stderr, "%s: Write error on %s: %s\n",
397 params.cmdname, params.imagefile,
398 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000399 exit (EXIT_FAILURE);
400 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000401
402 if (!file) {
403 break;
404 }
405
406 if (sep) {
407 *sep = ':';
408 file = sep + 1;
409 } else {
410 file = NULL;
411 }
wdenk5b1d7132002-11-03 00:07:02 +0000412 }
413
Christian Rieschd1be8f92011-12-09 09:47:38 +0000414 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000415
Christian Rieschd1be8f92011-12-09 09:47:38 +0000416 for (;;) {
417 char *sep = strchr(file, ':');
418 if (sep) {
419 *sep = '\0';
420 copy_file (ifd, file, 1);
421 *sep++ = ':';
422 file = sep;
423 } else {
424 copy_file (ifd, file, 0);
425 break;
426 }
wdenk5b1d7132002-11-03 00:07:02 +0000427 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000428 } else if (params.type == IH_TYPE_PBLIMAGE) {
429 /* PBL has special Image format, implements its' own */
430 pbl_load_uboot(ifd, &params);
Christian Rieschd1be8f92011-12-09 09:47:38 +0000431 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200432 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000433 }
wdenk5b1d7132002-11-03 00:07:02 +0000434 }
435
436 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530437#if defined(_POSIX_SYNCHRONIZED_IO) && \
438 !defined(__sun__) && \
439 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200440 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530441 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000442 (void) fdatasync (ifd);
443#else
444 (void) fsync (ifd);
445#endif
446
447 if (fstat(ifd, &sbuf) < 0) {
448 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530449 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000450 exit (EXIT_FAILURE);
451 }
Simon Glass92a655c2015-06-23 15:39:12 -0600452 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000453
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400454 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
455 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000456 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530457 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000458 exit (EXIT_FAILURE);
459 }
460
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530461 /* Setup the image header as per input image type*/
462 if (tparams->set_header)
463 tparams->set_header (ptr, &sbuf, ifd, &params);
464 else {
465 fprintf (stderr, "%s: Can't set header for %s: %s\n",
466 params.cmdname, tparams->name, strerror(errno));
467 exit (EXIT_FAILURE);
468 }
wdenk5b1d7132002-11-03 00:07:02 +0000469
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530470 /* Print the image information by processing image header */
471 if (tparams->print_header)
472 tparams->print_header (ptr);
473 else {
474 fprintf (stderr, "%s: Can't print header for %s: %s\n",
475 params.cmdname, tparams->name, strerror(errno));
476 exit (EXIT_FAILURE);
477 }
wdenk5b1d7132002-11-03 00:07:02 +0000478
479 (void) munmap((void *)ptr, sbuf.st_size);
480
481 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530482#if defined(_POSIX_SYNCHRONIZED_IO) && \
483 !defined(__sun__) && \
484 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200485 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530486 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000487 (void) fdatasync (ifd);
488#else
489 (void) fsync (ifd);
490#endif
491
492 if (close(ifd)) {
493 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530494 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000495 exit (EXIT_FAILURE);
496 }
497
498 exit (EXIT_SUCCESS);
499}
500
501static void
502copy_file (int ifd, const char *datafile, int pad)
503{
504 int dfd;
505 struct stat sbuf;
506 unsigned char *ptr;
507 int tail;
508 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200509 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000510 int offset = 0;
511 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200512 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000513
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200514 memset(zeros, 0, sizeof(zeros));
515
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530516 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000517 fprintf (stderr, "Adding Image %s\n", datafile);
518 }
519
wdenkef1464c2003-10-08 22:14:02 +0000520 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000521 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530522 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000523 exit (EXIT_FAILURE);
524 }
525
526 if (fstat(dfd, &sbuf) < 0) {
527 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530528 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000529 exit (EXIT_FAILURE);
530 }
531
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400532 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
533 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000534 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530535 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000536 exit (EXIT_FAILURE);
537 }
538
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530539 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000540 unsigned char *p = NULL;
541 /*
542 * XIP: do not append the image_header_t at the
543 * beginning of the file, but consume the space
544 * reserved for it.
545 */
546
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530547 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000548 fprintf (stderr,
549 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530550 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000551 exit (EXIT_FAILURE);
552 }
553
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530554 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000555 if ( *p != 0xff ) {
556 fprintf (stderr,
557 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530558 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000559 exit (EXIT_FAILURE);
560 }
561 }
562
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530563 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000564 }
565
566 size = sbuf.st_size - offset;
567 if (write(ifd, ptr + offset, size) != size) {
568 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530569 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000570 exit (EXIT_FAILURE);
571 }
572
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200573 tail = size % 4;
574 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000575
576 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
577 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530578 params.cmdname, params.imagefile,
579 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000580 exit (EXIT_FAILURE);
581 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200582 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600583 while (pad > 0) {
584 int todo = sizeof(zeros);
585
586 if (todo > pad)
587 todo = pad;
588 if (write(ifd, (char *)&zeros, todo) != todo) {
589 fprintf(stderr, "%s: Write error on %s: %s\n",
590 params.cmdname, params.imagefile,
591 strerror(errno));
592 exit(EXIT_FAILURE);
593 }
594 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200595 }
wdenk5b1d7132002-11-03 00:07:02 +0000596 }
597
598 (void) munmap((void *)ptr, sbuf.st_size);
599 (void) close (dfd);
600}