blob: aefe22f19b219a7d339dbfdc6f92ca1afe8b1831 [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +01002 * (C) Copyright 2008 Semihalf
3 *
Prafulla Wadaskarfabf3cf2009-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
wdenkdb82c8e2004-02-26 23:01:04 +00007 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00009 */
10
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010011#include "mkimage.h"
wdenk5b1d7132002-11-03 00:07:02 +000012#include <image.h>
Wolfgang Denke97ab722011-02-12 10:37:11 +010013#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000014
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053015static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000016
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053017/* parameters initialized by core will be used by the image type code */
Simon Glass52322d62016-02-22 22:55:38 -070018static struct image_tool_params params = {
Prafulla Wadaskarfabf3cf2009-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 Denk23416b82010-03-27 23:37:46 +010024 .imagename = "",
Shaohui Xieea65fd82012-08-10 02:49:35 +000025 .imagename2 = "",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053026};
wdenk5b1d7132002-11-03 00:07:02 +000027
Simon Glass434a31b2015-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 Glass01168b32016-02-22 22:55:37 -070069static void usage(const char *msg)
Simon Glass99a7c262016-02-22 22:55:36 -070070{
Simon Glass01168b32016-02-22 22:55:37 -070071 fprintf(stderr, "Error: %s\n", msg);
Simon Glass99a7c262016-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,
Andreas Bießmannf4001582016-05-01 03:01:27 +020088 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n"
89 " <dtb> file is used with -f auto, it may occour multiple times.\n",
Simon Glass99a7c262016-02-22 22:55:36 -070090 params.cmdname);
91 fprintf(stderr,
92 " -D => set all options for device tree compiler\n"
93 " -f => input filename for FIT source\n");
94#ifdef CONFIG_FIT_SIGNATURE
95 fprintf(stderr,
96 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
97 " -k => set directory containing private keys\n"
98 " -K => write public keys to this .dtb file\n"
99 " -c => add comment in signature node\n"
100 " -F => re-sign existing FIT image\n"
101 " -r => mark keys used as 'required' in dtb\n");
102#else
103 fprintf(stderr,
104 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
105#endif
106 fprintf(stderr, " %s -V ==> print version information and exit\n",
107 params.cmdname);
108 fprintf(stderr, "Use -T to see a list of available image types\n");
109
110 exit(EXIT_FAILURE);
111}
112
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700113static int add_content(int type, const char *fname)
114{
115 struct content_info *cont;
116
117 cont = calloc(1, sizeof(*cont));
118 if (!cont)
119 return -1;
120 cont->type = type;
121 cont->fname = fname;
122 if (params.content_tail)
123 params.content_tail->next = cont;
124 else
125 params.content_head = cont;
126 params.content_tail = cont;
127
128 return 0;
129}
130
Simon Glass7106bd42016-02-22 22:55:33 -0700131static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000132{
Peter Tyser632d9ca2010-04-04 22:36:03 -0500133 char *ptr;
Simon Glass04b46072016-02-22 22:55:48 -0700134 int type = IH_TYPE_INVALID;
135 char *datafile = NULL;
Simon Glasscf76cd22016-02-22 22:55:34 -0700136 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000137
Simon Glasscf76cd22016-02-22 22:55:34 -0700138 while ((opt = getopt(argc, argv,
Simon Glassda1805f2016-05-01 13:55:38 -0600139 "a:A:b:cC:d:D:e:Ef:Fk:K:ln:O:rR:qsT:vVx")) != -1) {
Simon Glasscf76cd22016-02-22 22:55:34 -0700140 switch (opt) {
Simon Glassf6fe5f62016-02-22 22:55:35 -0700141 case 'a':
142 params.addr = strtoull(optarg, &ptr, 16);
143 if (*ptr) {
144 fprintf(stderr, "%s: invalid load address %s\n",
145 params.cmdname, optarg);
146 exit(EXIT_FAILURE);
147 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700148 break;
149 case 'A':
150 params.arch = genimg_get_arch_id(optarg);
151 if (params.arch < 0)
Simon Glass01168b32016-02-22 22:55:37 -0700152 usage("Invalid architecture");
Simon Glasscf76cd22016-02-22 22:55:34 -0700153 break;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700154 case 'b':
Andreas Bießmannddd34772016-05-03 15:17:03 +0200155 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmannf4001582016-05-01 03:01:27 +0200156 fprintf(stderr,
157 "%s: Out of memory adding content '%s'",
158 params.cmdname, optarg);
159 exit(EXIT_FAILURE);
160 }
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700161 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700162 case 'c':
163 params.comment = optarg;
164 break;
165 case 'C':
166 params.comp = genimg_get_comp_id(optarg);
167 if (params.comp < 0)
Simon Glass01168b32016-02-22 22:55:37 -0700168 usage("Invalid compression type");
Simon Glasscf76cd22016-02-22 22:55:34 -0700169 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700170 case 'd':
171 params.datafile = optarg;
172 params.dflag = 1;
173 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700174 case 'D':
175 params.dtc = optarg;
176 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700177 case 'e':
178 params.ep = strtoull(optarg, &ptr, 16);
179 if (*ptr) {
180 fprintf(stderr, "%s: invalid entry point %s\n",
181 params.cmdname, optarg);
182 exit(EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000183 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700184 params.eflag = 1;
185 break;
Simon Glassafd728c2016-02-22 22:55:53 -0700186 case 'E':
187 params.external_data = true;
188 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700189 case 'f':
Simon Glass88e31cb2016-02-22 22:55:51 -0700190 datafile = optarg;
191 params.auto_its = !strcmp(datafile, "auto");
Simon Glasscf76cd22016-02-22 22:55:34 -0700192 /* no break */
193 case 'F':
194 /*
195 * The flattened image tree (FIT) format
196 * requires a flattened device tree image type
197 */
Simon Glass04b46072016-02-22 22:55:48 -0700198 params.fit_image_type = params.type;
Simon Glasscf76cd22016-02-22 22:55:34 -0700199 params.type = IH_TYPE_FLATDT;
200 params.fflag = 1;
201 break;
202 case 'k':
203 params.keydir = optarg;
204 break;
205 case 'K':
206 params.keydest = optarg;
207 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700208 case 'l':
209 params.lflag = 1;
210 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700211 case 'n':
212 params.imagename = optarg;
213 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700214 case 'O':
215 params.os = genimg_get_os_id(optarg);
216 if (params.os < 0)
Simon Glass01168b32016-02-22 22:55:37 -0700217 usage("Invalid operating system");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700218 break;
Simon Glassda1805f2016-05-01 13:55:38 -0600219 case 'q':
220 params.quiet = 1;
221 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700222 case 'r':
223 params.require_keys = 1;
224 break;
225 case 'R':
226 /*
227 * This entry is for the second configuration
228 * file, if only one is not enough.
229 */
230 params.imagename2 = optarg;
231 break;
232 case 's':
233 params.skipcpy = 1;
234 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700235 case 'T':
Simon Glass04b46072016-02-22 22:55:48 -0700236 type = genimg_get_type_id(optarg);
237 if (type < 0) {
Simon Glassf6fe5f62016-02-22 22:55:35 -0700238 show_image_types();
Simon Glass01168b32016-02-22 22:55:37 -0700239 usage("Invalid image type");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700240 }
241 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700242 case 'v':
243 params.vflag++;
244 break;
245 case 'V':
246 printf("mkimage version %s\n", PLAIN_VERSION);
247 exit(EXIT_SUCCESS);
248 case 'x':
249 params.xflag++;
250 break;
251 default:
Simon Glass01168b32016-02-22 22:55:37 -0700252 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000253 }
wdenk5b1d7132002-11-03 00:07:02 +0000254 }
255
Andreas Bießmannddd34772016-05-03 15:17:03 +0200256 /* The last parameter is expected to be the imagefile */
257 if (optind < argc)
Andreas Bießmannf4001582016-05-01 03:01:27 +0200258 params.imagefile = argv[optind];
259
Simon Glass04b46072016-02-22 22:55:48 -0700260 /*
261 * For auto-generated FIT images we need to know the image type to put
262 * in the FIT, which is separate from the file's image type (which
263 * will always be IH_TYPE_FLATDT in this case).
264 */
265 if (params.type == IH_TYPE_FLATDT) {
266 params.fit_image_type = type;
Simon Glass88e31cb2016-02-22 22:55:51 -0700267 if (!params.auto_its)
268 params.datafile = datafile;
Simon Glass04b46072016-02-22 22:55:48 -0700269 } else if (type != IH_TYPE_INVALID) {
270 params.type = type;
271 }
272
273 if (!params.imagefile)
Simon Glass01168b32016-02-22 22:55:37 -0700274 usage("Missing output filename");
Simon Glass7106bd42016-02-22 22:55:33 -0700275}
276
277
278int main(int argc, char **argv)
279{
280 int ifd = -1;
281 struct stat sbuf;
282 char *ptr;
283 int retval = 0;
284 struct image_type_params *tparams = NULL;
285 int pad_len = 0;
286 int dfd;
287
288 params.cmdname = *argv;
289 params.addr = 0;
290 params.ep = 0;
291
292 process_args(argc, argv);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530293
294 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200295 tparams = imagetool_get_type(params.type);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530296 if (tparams == NULL) {
297 fprintf (stderr, "%s: unsupported type %s\n",
298 params.cmdname, genimg_get_type_name(params.type));
299 exit (EXIT_FAILURE);
300 }
301
302 /*
303 * check the passed arguments parameters meets the requirements
304 * as per image type to be generated/listed
305 */
306 if (tparams->check_params)
307 if (tparams->check_params (&params))
Simon Glass01168b32016-02-22 22:55:37 -0700308 usage("Bad parameters for image type");
wdenk5b1d7132002-11-03 00:07:02 +0000309
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530310 if (!params.eflag) {
311 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000312 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530313 if (params.xflag)
314 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000315 }
316
Peter Tyserb2f8b942009-11-24 16:42:10 -0600317 if (params.fflag){
318 if (tparams->fflag_handle)
319 /*
320 * in some cases, some additional processing needs
321 * to be done if fflag is defined
322 *
323 * For ex. fit_handle_file for Fit file support
324 */
325 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000326
Peter Tyserb2f8b942009-11-24 16:42:10 -0600327 if (retval != EXIT_SUCCESS)
328 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000329 }
330
Peter Tyserb2f8b942009-11-24 16:42:10 -0600331 if (params.lflag || params.fflag) {
332 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
333 } else {
334 ifd = open (params.imagefile,
335 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
336 }
337
338 if (ifd < 0) {
339 fprintf (stderr, "%s: Can't open %s: %s\n",
340 params.cmdname, params.imagefile,
341 strerror(errno));
342 exit (EXIT_FAILURE);
343 }
344
345 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000346 /*
347 * list header information of existing image
348 */
349 if (fstat(ifd, &sbuf) < 0) {
350 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530351 params.cmdname, params.imagefile,
352 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000353 exit (EXIT_FAILURE);
354 }
355
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530356 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000357 fprintf (stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530358 "%s: Bad size: \"%s\" is not valid image\n",
359 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000360 exit (EXIT_FAILURE);
361 }
362
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400363 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
364 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000365 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530366 params.cmdname, params.imagefile,
367 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000368 exit (EXIT_FAILURE);
369 }
370
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530371 /*
372 * scan through mkimage registry for all supported image types
373 * and verify the input image file header for match
374 * Print the image information for matched image type
375 * Returns the error code if not matched
376 */
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -0200377 retval = imagetool_verify_print_header(ptr, &sbuf,
378 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000379
wdenk5b1d7132002-11-03 00:07:02 +0000380 (void) munmap((void *)ptr, sbuf.st_size);
381 (void) close (ifd);
382
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530383 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000384 }
385
Marek Vasut4cc86272015-12-07 18:01:54 +0100386 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert3edd0412015-12-04 00:11:23 +0200387 dfd = open(params.datafile, O_RDONLY | O_BINARY);
388 if (dfd < 0) {
389 fprintf(stderr, "%s: Can't open %s: %s\n",
390 params.cmdname, params.datafile,
391 strerror(errno));
392 exit(EXIT_FAILURE);
393 }
Simon Glass139651f2015-06-23 15:39:12 -0600394
Philippe De Swert3edd0412015-12-04 00:11:23 +0200395 if (fstat(dfd, &sbuf) < 0) {
396 fprintf(stderr, "%s: Can't stat %s: %s\n",
397 params.cmdname, params.datafile,
398 strerror(errno));
399 exit(EXIT_FAILURE);
400 }
Simon Glass139651f2015-06-23 15:39:12 -0600401
Philippe De Swert3edd0412015-12-04 00:11:23 +0200402 params.file_size = sbuf.st_size + tparams->header_size;
403 close(dfd);
404 }
Simon Glass139651f2015-06-23 15:39:12 -0600405
wdenk5b1d7132002-11-03 00:07:02 +0000406 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000407 * In case there an header with a variable
408 * length will be added, the corresponding
409 * function is called. This is responsible to
410 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000411 */
Stefano Babic4a963302011-09-15 23:50:16 +0000412 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200413 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000414 else
415 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000416
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530417 if (write(ifd, tparams->hdr, tparams->header_size)
418 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000419 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530420 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000421 exit (EXIT_FAILURE);
422 }
423
Christian Rieschc14f2812011-12-09 09:47:38 +0000424 if (!params.skipcpy) {
425 if (params.type == IH_TYPE_MULTI ||
426 params.type == IH_TYPE_SCRIPT) {
427 char *file = params.datafile;
428 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000429
Christian Rieschc14f2812011-12-09 09:47:38 +0000430 for (;;) {
431 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000432
Christian Rieschc14f2812011-12-09 09:47:38 +0000433 if (file) {
434 if ((sep = strchr(file, ':')) != NULL) {
435 *sep = '\0';
436 }
437
438 if (stat (file, &sbuf) < 0) {
439 fprintf (stderr, "%s: Can't stat %s: %s\n",
440 params.cmdname, file, strerror(errno));
441 exit (EXIT_FAILURE);
442 }
443 size = cpu_to_uimage (sbuf.st_size);
444 } else {
445 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000446 }
447
Christian Rieschc14f2812011-12-09 09:47:38 +0000448 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
449 fprintf (stderr, "%s: Write error on %s: %s\n",
450 params.cmdname, params.imagefile,
451 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000452 exit (EXIT_FAILURE);
453 }
wdenk5b1d7132002-11-03 00:07:02 +0000454
Christian Rieschc14f2812011-12-09 09:47:38 +0000455 if (!file) {
456 break;
457 }
wdenk5b1d7132002-11-03 00:07:02 +0000458
Christian Rieschc14f2812011-12-09 09:47:38 +0000459 if (sep) {
460 *sep = ':';
461 file = sep + 1;
462 } else {
463 file = NULL;
464 }
wdenk5b1d7132002-11-03 00:07:02 +0000465 }
wdenk5b1d7132002-11-03 00:07:02 +0000466
Christian Rieschc14f2812011-12-09 09:47:38 +0000467 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000468
Christian Rieschc14f2812011-12-09 09:47:38 +0000469 for (;;) {
470 char *sep = strchr(file, ':');
471 if (sep) {
472 *sep = '\0';
473 copy_file (ifd, file, 1);
474 *sep++ = ':';
475 file = sep;
476 } else {
477 copy_file (ifd, file, 0);
478 break;
479 }
wdenk5b1d7132002-11-03 00:07:02 +0000480 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000481 } else if (params.type == IH_TYPE_PBLIMAGE) {
482 /* PBL has special Image format, implements its' own */
483 pbl_load_uboot(ifd, &params);
Christian Rieschc14f2812011-12-09 09:47:38 +0000484 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200485 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000486 }
wdenk5b1d7132002-11-03 00:07:02 +0000487 }
488
489 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530490#if defined(_POSIX_SYNCHRONIZED_IO) && \
491 !defined(__sun__) && \
492 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200493 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530494 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000495 (void) fdatasync (ifd);
496#else
497 (void) fsync (ifd);
498#endif
499
500 if (fstat(ifd, &sbuf) < 0) {
501 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530502 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000503 exit (EXIT_FAILURE);
504 }
Simon Glass139651f2015-06-23 15:39:12 -0600505 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000506
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400507 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
508 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000509 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530510 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000511 exit (EXIT_FAILURE);
512 }
513
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530514 /* Setup the image header as per input image type*/
515 if (tparams->set_header)
516 tparams->set_header (ptr, &sbuf, ifd, &params);
517 else {
518 fprintf (stderr, "%s: Can't set header for %s: %s\n",
519 params.cmdname, tparams->name, strerror(errno));
520 exit (EXIT_FAILURE);
521 }
wdenk5b1d7132002-11-03 00:07:02 +0000522
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530523 /* Print the image information by processing image header */
524 if (tparams->print_header)
525 tparams->print_header (ptr);
526 else {
527 fprintf (stderr, "%s: Can't print header for %s: %s\n",
528 params.cmdname, tparams->name, strerror(errno));
529 exit (EXIT_FAILURE);
530 }
wdenk5b1d7132002-11-03 00:07:02 +0000531
532 (void) munmap((void *)ptr, sbuf.st_size);
533
534 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530535#if defined(_POSIX_SYNCHRONIZED_IO) && \
536 !defined(__sun__) && \
537 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200538 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530539 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000540 (void) fdatasync (ifd);
541#else
542 (void) fsync (ifd);
543#endif
544
545 if (close(ifd)) {
546 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530547 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000548 exit (EXIT_FAILURE);
549 }
550
551 exit (EXIT_SUCCESS);
552}
553
554static void
555copy_file (int ifd, const char *datafile, int pad)
556{
557 int dfd;
558 struct stat sbuf;
559 unsigned char *ptr;
560 int tail;
561 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200562 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000563 int offset = 0;
564 int size;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200565 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000566
Stefano Babic6531bd92013-08-19 19:03:19 +0200567 memset(zeros, 0, sizeof(zeros));
568
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530569 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000570 fprintf (stderr, "Adding Image %s\n", datafile);
571 }
572
wdenk1ee550e2003-10-08 22:14:02 +0000573 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000574 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530575 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000576 exit (EXIT_FAILURE);
577 }
578
579 if (fstat(dfd, &sbuf) < 0) {
580 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530581 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000582 exit (EXIT_FAILURE);
583 }
584
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400585 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
586 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000587 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530588 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000589 exit (EXIT_FAILURE);
590 }
591
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530592 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000593 unsigned char *p = NULL;
594 /*
595 * XIP: do not append the image_header_t at the
596 * beginning of the file, but consume the space
597 * reserved for it.
598 */
599
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530600 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000601 fprintf (stderr,
602 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530603 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000604 exit (EXIT_FAILURE);
605 }
606
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530607 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000608 if ( *p != 0xff ) {
609 fprintf (stderr,
610 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530611 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000612 exit (EXIT_FAILURE);
613 }
614 }
615
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530616 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000617 }
618
619 size = sbuf.st_size - offset;
620 if (write(ifd, ptr + offset, size) != size) {
621 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530622 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000623 exit (EXIT_FAILURE);
624 }
625
Stefano Babic6531bd92013-08-19 19:03:19 +0200626 tail = size % 4;
627 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000628
629 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
630 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530631 params.cmdname, params.imagefile,
632 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000633 exit (EXIT_FAILURE);
634 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200635 } else if (pad > 1) {
Simon Glass46409cc2015-08-30 16:55:22 -0600636 while (pad > 0) {
637 int todo = sizeof(zeros);
638
639 if (todo > pad)
640 todo = pad;
641 if (write(ifd, (char *)&zeros, todo) != todo) {
642 fprintf(stderr, "%s: Write error on %s: %s\n",
643 params.cmdname, params.imagefile,
644 strerror(errno));
645 exit(EXIT_FAILURE);
646 }
647 pad -= todo;
Stefano Babic6531bd92013-08-19 19:03:19 +0200648 }
wdenk5b1d7132002-11-03 00:07:02 +0000649 }
650
651 (void) munmap((void *)ptr, sbuf.st_size);
652 (void) close (dfd);
653}