blob: d375c2abecff602de73d605c35f478a3ffb22d68 [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 Glass30664222016-06-30 10:52:17 -060028static enum ih_category cur_category;
29
30static int h_compare_category_name(const void *vtype1, const void *vtype2)
31{
32 const int *type1 = vtype1;
33 const int *type2 = vtype2;
34 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
35 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
36
37 return strcmp(name1, name2);
38}
39
40int show_valid_options(enum ih_category category)
41{
42 int *order;
43 int count;
44 int item;
45 int i;
46
47 count = genimg_get_cat_count(category);
48 order = calloc(count, sizeof(*order));
49 if (!order)
50 return -ENOMEM;
51
52 /* Sort the names in order of short name for easier reading */
53 for (item = 0; item < count; item++)
54 order[item] = item;
55 cur_category = category;
56 qsort(order, count, sizeof(int), h_compare_category_name);
57
58 fprintf(stderr, "\nInvalid %s, supported are:\n",
59 genimg_get_cat_desc(category));
60 for (i = 0; i < count; i++) {
61 item = order[i];
62 fprintf(stderr, "\t%-15s %s\n",
63 genimg_get_cat_short_name(category, item),
64 genimg_get_cat_name(category, item));
65 }
66 fprintf(stderr, "\n");
67
68 return 0;
69}
70
Simon Glass5b9d44d2015-06-23 15:38:25 -060071static int h_compare_image_name(const void *vtype1, const void *vtype2)
72{
73 const int *type1 = vtype1;
74 const int *type2 = vtype2;
75 const char *name1 = genimg_get_type_short_name(*type1);
76 const char *name2 = genimg_get_type_short_name(*type2);
77
78 return strcmp(name1, name2);
79}
80
81/* Show all image types supported by mkimage */
82static void show_image_types(void)
83{
84 struct image_type_params *tparams;
85 int order[IH_TYPE_COUNT];
86 int count;
87 int type;
88 int i;
89
90 /* Sort the names in order of short name for easier reading */
91 memset(order, '\0', sizeof(order));
92 for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
93 tparams = imagetool_get_type(type);
94 if (tparams)
95 order[count++] = type;
96 }
97 qsort(order, count, sizeof(int), h_compare_image_name);
98
99 fprintf(stderr, "\nInvalid image type. Supported image types:\n");
100 for (i = 0; i < count; i++) {
101 type = order[i];
102 tparams = imagetool_get_type(type);
103 if (tparams) {
104 fprintf(stderr, "\t%-15s %s\n",
105 genimg_get_type_short_name(type),
106 genimg_get_type_name(type));
107 }
108 }
109 fprintf(stderr, "\n");
110}
111
Simon Glass15310342016-02-22 22:55:37 -0700112static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -0700113{
Simon Glass15310342016-02-22 22:55:37 -0700114 fprintf(stderr, "Error: %s\n", msg);
Simon Glassb0a487a2016-02-22 22:55:36 -0700115 fprintf(stderr, "Usage: %s -l image\n"
116 " -l ==> list image header information\n",
117 params.cmdname);
118 fprintf(stderr,
119 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
120 " -A ==> set architecture to 'arch'\n"
121 " -O ==> set operating system to 'os'\n"
122 " -T ==> set image type to 'type'\n"
123 " -C ==> set compression type 'comp'\n"
124 " -a ==> set load address to 'addr' (hex)\n"
125 " -e ==> set entry point to 'ep' (hex)\n"
126 " -n ==> set image name to 'name'\n"
127 " -d ==> use image data from 'datafile'\n"
128 " -x ==> set XIP (execute in place)\n",
129 params.cmdname);
130 fprintf(stderr,
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200131 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n"
132 " <dtb> file is used with -f auto, it may occour multiple times.\n",
Simon Glassb0a487a2016-02-22 22:55:36 -0700133 params.cmdname);
134 fprintf(stderr,
135 " -D => set all options for device tree compiler\n"
136 " -f => input filename for FIT source\n");
137#ifdef CONFIG_FIT_SIGNATURE
138 fprintf(stderr,
Teddy Reedf8f91072016-06-09 19:38:02 -0700139 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
140 " -E => place data outside of the FIT structure\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700141 " -k => set directory containing private keys\n"
142 " -K => write public keys to this .dtb file\n"
143 " -c => add comment in signature node\n"
144 " -F => re-sign existing FIT image\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700145 " -p => place external data at a static position\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700146 " -r => mark keys used as 'required' in dtb\n");
147#else
148 fprintf(stderr,
149 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
150#endif
151 fprintf(stderr, " %s -V ==> print version information and exit\n",
152 params.cmdname);
153 fprintf(stderr, "Use -T to see a list of available image types\n");
154
155 exit(EXIT_FAILURE);
156}
157
Simon Glassfb4cce02016-02-22 22:55:52 -0700158static int add_content(int type, const char *fname)
159{
160 struct content_info *cont;
161
162 cont = calloc(1, sizeof(*cont));
163 if (!cont)
164 return -1;
165 cont->type = type;
166 cont->fname = fname;
167 if (params.content_tail)
168 params.content_tail->next = cont;
169 else
170 params.content_head = cont;
171 params.content_tail = cont;
172
173 return 0;
174}
175
Simon Glass0b443de2016-02-22 22:55:33 -0700176static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000177{
Peter Tysera2513e22010-04-04 22:36:03 -0500178 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700179 int type = IH_TYPE_INVALID;
180 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700181 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000182
Simon Glassa02221f2016-02-22 22:55:34 -0700183 while ((opt = getopt(argc, argv,
Teddy Reedf8f91072016-06-09 19:38:02 -0700184 "a:A:b:cC:d:D:e:Ef:Fk:K:ln:p:O:rR:qsT:vVx")) != -1) {
Simon Glassa02221f2016-02-22 22:55:34 -0700185 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700186 case 'a':
187 params.addr = strtoull(optarg, &ptr, 16);
188 if (*ptr) {
189 fprintf(stderr, "%s: invalid load address %s\n",
190 params.cmdname, optarg);
191 exit(EXIT_FAILURE);
192 }
Simon Glassa02221f2016-02-22 22:55:34 -0700193 break;
194 case 'A':
195 params.arch = genimg_get_arch_id(optarg);
196 if (params.arch < 0)
Simon Glass15310342016-02-22 22:55:37 -0700197 usage("Invalid architecture");
Simon Glassa02221f2016-02-22 22:55:34 -0700198 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700199 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200200 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200201 fprintf(stderr,
202 "%s: Out of memory adding content '%s'",
203 params.cmdname, optarg);
204 exit(EXIT_FAILURE);
205 }
Simon Glassfb4cce02016-02-22 22:55:52 -0700206 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700207 case 'c':
208 params.comment = optarg;
209 break;
210 case 'C':
211 params.comp = genimg_get_comp_id(optarg);
212 if (params.comp < 0)
Simon Glass15310342016-02-22 22:55:37 -0700213 usage("Invalid compression type");
Simon Glassa02221f2016-02-22 22:55:34 -0700214 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700215 case 'd':
216 params.datafile = optarg;
217 params.dflag = 1;
218 break;
Simon Glass07450082016-02-22 22:55:35 -0700219 case 'D':
220 params.dtc = optarg;
221 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700222 case 'e':
223 params.ep = strtoull(optarg, &ptr, 16);
224 if (*ptr) {
225 fprintf(stderr, "%s: invalid entry point %s\n",
226 params.cmdname, optarg);
227 exit(EXIT_FAILURE);
228 }
229 params.eflag = 1;
230 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700231 case 'E':
232 params.external_data = true;
233 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700234 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700235 datafile = optarg;
236 params.auto_its = !strcmp(datafile, "auto");
Simon Glassa02221f2016-02-22 22:55:34 -0700237 /* no break */
238 case 'F':
239 /*
240 * The flattened image tree (FIT) format
241 * requires a flattened device tree image type
242 */
243 params.type = IH_TYPE_FLATDT;
244 params.fflag = 1;
245 break;
246 case 'k':
247 params.keydir = optarg;
248 break;
249 case 'K':
250 params.keydest = optarg;
251 break;
Simon Glass07450082016-02-22 22:55:35 -0700252 case 'l':
253 params.lflag = 1;
254 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700255 case 'n':
256 params.imagename = optarg;
257 break;
Simon Glass07450082016-02-22 22:55:35 -0700258 case 'O':
259 params.os = genimg_get_os_id(optarg);
260 if (params.os < 0)
Simon Glass15310342016-02-22 22:55:37 -0700261 usage("Invalid operating system");
Simon Glass07450082016-02-22 22:55:35 -0700262 break;
Teddy Reedf8f91072016-06-09 19:38:02 -0700263 case 'p':
264 params.external_offset = strtoull(optarg, &ptr, 16);
265 if (*ptr) {
266 fprintf(stderr, "%s: invalid offset size %s\n",
267 params.cmdname, optarg);
268 exit(EXIT_FAILURE);
269 }
Simon Glassbd6e1422016-05-01 13:55:38 -0600270 case 'q':
271 params.quiet = 1;
272 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700273 case 'r':
274 params.require_keys = 1;
275 break;
276 case 'R':
277 /*
278 * This entry is for the second configuration
279 * file, if only one is not enough.
280 */
281 params.imagename2 = optarg;
282 break;
283 case 's':
284 params.skipcpy = 1;
285 break;
Simon Glass07450082016-02-22 22:55:35 -0700286 case 'T':
Simon Glassd505a092016-02-22 22:55:48 -0700287 type = genimg_get_type_id(optarg);
288 if (type < 0) {
Simon Glass07450082016-02-22 22:55:35 -0700289 show_image_types();
Simon Glass15310342016-02-22 22:55:37 -0700290 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700291 }
292 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700293 case 'v':
294 params.vflag++;
295 break;
296 case 'V':
297 printf("mkimage version %s\n", PLAIN_VERSION);
298 exit(EXIT_SUCCESS);
299 case 'x':
300 params.xflag++;
301 break;
302 default:
Simon Glass15310342016-02-22 22:55:37 -0700303 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000304 }
wdenk5b1d7132002-11-03 00:07:02 +0000305 }
306
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200307 /* The last parameter is expected to be the imagefile */
308 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200309 params.imagefile = argv[optind];
310
Simon Glassd505a092016-02-22 22:55:48 -0700311 /*
312 * For auto-generated FIT images we need to know the image type to put
313 * in the FIT, which is separate from the file's image type (which
314 * will always be IH_TYPE_FLATDT in this case).
315 */
316 if (params.type == IH_TYPE_FLATDT) {
Simon Glass20deadd2016-06-30 10:52:07 -0600317 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600318 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700319 if (!params.auto_its)
320 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600321 else if (!params.datafile)
322 usage("Missing data file for auto-FIT (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700323 } else if (type != IH_TYPE_INVALID) {
324 params.type = type;
325 }
326
327 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700328 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700329}
330
Simon Glass0b443de2016-02-22 22:55:33 -0700331int main(int argc, char **argv)
332{
333 int ifd = -1;
334 struct stat sbuf;
335 char *ptr;
336 int retval = 0;
337 struct image_type_params *tparams = NULL;
338 int pad_len = 0;
339 int dfd;
340
341 params.cmdname = *argv;
342 params.addr = 0;
343 params.ep = 0;
344
345 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000346
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530347 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200348 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530349 if (tparams == NULL) {
350 fprintf (stderr, "%s: unsupported type %s\n",
351 params.cmdname, genimg_get_type_name(params.type));
352 exit (EXIT_FAILURE);
353 }
354
355 /*
356 * check the passed arguments parameters meets the requirements
357 * as per image type to be generated/listed
358 */
359 if (tparams->check_params)
360 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700361 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530362
363 if (!params.eflag) {
364 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000365 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530366 if (params.xflag)
367 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000368 }
369
Peter Tyserc81296c2009-11-24 16:42:10 -0600370 if (params.fflag){
371 if (tparams->fflag_handle)
372 /*
373 * in some cases, some additional processing needs
374 * to be done if fflag is defined
375 *
376 * For ex. fit_handle_file for Fit file support
377 */
378 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000379
Peter Tyserc81296c2009-11-24 16:42:10 -0600380 if (retval != EXIT_SUCCESS)
381 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000382 }
383
Peter Tyserc81296c2009-11-24 16:42:10 -0600384 if (params.lflag || params.fflag) {
385 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
386 } else {
387 ifd = open (params.imagefile,
388 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
389 }
390
391 if (ifd < 0) {
392 fprintf (stderr, "%s: Can't open %s: %s\n",
393 params.cmdname, params.imagefile,
394 strerror(errno));
395 exit (EXIT_FAILURE);
396 }
397
398 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000399 /*
400 * list header information of existing image
401 */
402 if (fstat(ifd, &sbuf) < 0) {
403 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530404 params.cmdname, params.imagefile,
405 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000406 exit (EXIT_FAILURE);
407 }
408
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530409 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000410 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530411 "%s: Bad size: \"%s\" is not valid image\n",
412 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000413 exit (EXIT_FAILURE);
414 }
415
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400416 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
417 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000418 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530419 params.cmdname, params.imagefile,
420 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000421 exit (EXIT_FAILURE);
422 }
423
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530424 /*
425 * scan through mkimage registry for all supported image types
426 * and verify the input image file header for match
427 * Print the image information for matched image type
428 * Returns the error code if not matched
429 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200430 retval = imagetool_verify_print_header(ptr, &sbuf,
431 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000432
wdenk5b1d7132002-11-03 00:07:02 +0000433 (void) munmap((void *)ptr, sbuf.st_size);
434 (void) close (ifd);
435
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530436 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000437 }
438
Marek Vasut34633142015-12-07 18:01:54 +0100439 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200440 dfd = open(params.datafile, O_RDONLY | O_BINARY);
441 if (dfd < 0) {
442 fprintf(stderr, "%s: Can't open %s: %s\n",
443 params.cmdname, params.datafile,
444 strerror(errno));
445 exit(EXIT_FAILURE);
446 }
Simon Glass92a655c2015-06-23 15:39:12 -0600447
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200448 if (fstat(dfd, &sbuf) < 0) {
449 fprintf(stderr, "%s: Can't stat %s: %s\n",
450 params.cmdname, params.datafile,
451 strerror(errno));
452 exit(EXIT_FAILURE);
453 }
Simon Glass92a655c2015-06-23 15:39:12 -0600454
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200455 params.file_size = sbuf.st_size + tparams->header_size;
456 close(dfd);
457 }
Simon Glass92a655c2015-06-23 15:39:12 -0600458
wdenk5b1d7132002-11-03 00:07:02 +0000459 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000460 * In case there an header with a variable
461 * length will be added, the corresponding
462 * function is called. This is responsible to
463 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000464 */
Stefano Babicf0662102011-09-15 23:50:16 +0000465 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200466 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000467 else
468 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000469
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530470 if (write(ifd, tparams->hdr, tparams->header_size)
471 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000472 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530473 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000474 exit (EXIT_FAILURE);
475 }
476
Christian Rieschd1be8f92011-12-09 09:47:38 +0000477 if (!params.skipcpy) {
478 if (params.type == IH_TYPE_MULTI ||
479 params.type == IH_TYPE_SCRIPT) {
480 char *file = params.datafile;
481 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000482
Christian Rieschd1be8f92011-12-09 09:47:38 +0000483 for (;;) {
484 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000485
Christian Rieschd1be8f92011-12-09 09:47:38 +0000486 if (file) {
487 if ((sep = strchr(file, ':')) != NULL) {
488 *sep = '\0';
489 }
490
491 if (stat (file, &sbuf) < 0) {
492 fprintf (stderr, "%s: Can't stat %s: %s\n",
493 params.cmdname, file, strerror(errno));
494 exit (EXIT_FAILURE);
495 }
496 size = cpu_to_uimage (sbuf.st_size);
497 } else {
498 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000499 }
500
Christian Rieschd1be8f92011-12-09 09:47:38 +0000501 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
502 fprintf (stderr, "%s: Write error on %s: %s\n",
503 params.cmdname, params.imagefile,
504 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000505 exit (EXIT_FAILURE);
506 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000507
508 if (!file) {
509 break;
510 }
511
512 if (sep) {
513 *sep = ':';
514 file = sep + 1;
515 } else {
516 file = NULL;
517 }
wdenk5b1d7132002-11-03 00:07:02 +0000518 }
519
Christian Rieschd1be8f92011-12-09 09:47:38 +0000520 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000521
Christian Rieschd1be8f92011-12-09 09:47:38 +0000522 for (;;) {
523 char *sep = strchr(file, ':');
524 if (sep) {
525 *sep = '\0';
526 copy_file (ifd, file, 1);
527 *sep++ = ':';
528 file = sep;
529 } else {
530 copy_file (ifd, file, 0);
531 break;
532 }
wdenk5b1d7132002-11-03 00:07:02 +0000533 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000534 } else if (params.type == IH_TYPE_PBLIMAGE) {
535 /* PBL has special Image format, implements its' own */
536 pbl_load_uboot(ifd, &params);
Christian Rieschd1be8f92011-12-09 09:47:38 +0000537 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200538 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000539 }
wdenk5b1d7132002-11-03 00:07:02 +0000540 }
541
542 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530543#if defined(_POSIX_SYNCHRONIZED_IO) && \
544 !defined(__sun__) && \
545 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200546 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530547 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000548 (void) fdatasync (ifd);
549#else
550 (void) fsync (ifd);
551#endif
552
553 if (fstat(ifd, &sbuf) < 0) {
554 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530555 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000556 exit (EXIT_FAILURE);
557 }
Simon Glass92a655c2015-06-23 15:39:12 -0600558 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000559
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400560 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
561 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000562 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530563 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000564 exit (EXIT_FAILURE);
565 }
566
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530567 /* Setup the image header as per input image type*/
568 if (tparams->set_header)
569 tparams->set_header (ptr, &sbuf, ifd, &params);
570 else {
571 fprintf (stderr, "%s: Can't set header for %s: %s\n",
572 params.cmdname, tparams->name, strerror(errno));
573 exit (EXIT_FAILURE);
574 }
wdenk5b1d7132002-11-03 00:07:02 +0000575
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530576 /* Print the image information by processing image header */
577 if (tparams->print_header)
578 tparams->print_header (ptr);
579 else {
580 fprintf (stderr, "%s: Can't print header for %s: %s\n",
581 params.cmdname, tparams->name, strerror(errno));
582 exit (EXIT_FAILURE);
583 }
wdenk5b1d7132002-11-03 00:07:02 +0000584
585 (void) munmap((void *)ptr, sbuf.st_size);
586
587 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530588#if defined(_POSIX_SYNCHRONIZED_IO) && \
589 !defined(__sun__) && \
590 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200591 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530592 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000593 (void) fdatasync (ifd);
594#else
595 (void) fsync (ifd);
596#endif
597
598 if (close(ifd)) {
599 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530600 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000601 exit (EXIT_FAILURE);
602 }
603
604 exit (EXIT_SUCCESS);
605}
606
607static void
608copy_file (int ifd, const char *datafile, int pad)
609{
610 int dfd;
611 struct stat sbuf;
612 unsigned char *ptr;
613 int tail;
614 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200615 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000616 int offset = 0;
617 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200618 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000619
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200620 memset(zeros, 0, sizeof(zeros));
621
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530622 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000623 fprintf (stderr, "Adding Image %s\n", datafile);
624 }
625
wdenkef1464c2003-10-08 22:14:02 +0000626 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000627 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530628 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000629 exit (EXIT_FAILURE);
630 }
631
632 if (fstat(dfd, &sbuf) < 0) {
633 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530634 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000635 exit (EXIT_FAILURE);
636 }
637
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400638 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
639 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000640 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530641 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000642 exit (EXIT_FAILURE);
643 }
644
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530645 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000646 unsigned char *p = NULL;
647 /*
648 * XIP: do not append the image_header_t at the
649 * beginning of the file, but consume the space
650 * reserved for it.
651 */
652
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530653 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000654 fprintf (stderr,
655 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530656 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000657 exit (EXIT_FAILURE);
658 }
659
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530660 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000661 if ( *p != 0xff ) {
662 fprintf (stderr,
663 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530664 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000665 exit (EXIT_FAILURE);
666 }
667 }
668
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530669 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000670 }
671
672 size = sbuf.st_size - offset;
673 if (write(ifd, ptr + offset, size) != size) {
674 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530675 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000676 exit (EXIT_FAILURE);
677 }
678
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200679 tail = size % 4;
680 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000681
682 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
683 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530684 params.cmdname, params.imagefile,
685 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000686 exit (EXIT_FAILURE);
687 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200688 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600689 while (pad > 0) {
690 int todo = sizeof(zeros);
691
692 if (todo > pad)
693 todo = pad;
694 if (write(ifd, (char *)&zeros, todo) != todo) {
695 fprintf(stderr, "%s: Write error on %s: %s\n",
696 params.cmdname, params.imagefile,
697 strerror(errno));
698 exit(EXIT_FAILURE);
699 }
700 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200701 }
wdenk5b1d7132002-11-03 00:07:02 +0000702 }
703
704 (void) munmap((void *)ptr, sbuf.st_size);
705 (void) close (dfd);
706}