blob: 49d5d1ed70a715f76033cb9aa1eb5e6a8ca4a63e [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
Simon Glassf24e1052016-06-30 10:52:18 -060040static int show_valid_options(enum ih_category category)
Simon Glass30664222016-06-30 10:52:17 -060041{
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");
Simon Glass0cd82e22016-10-27 17:54:03 -060067 free(order);
Simon Glass30664222016-06-30 10:52:17 -060068
69 return 0;
70}
71
Simon Glass15310342016-02-22 22:55:37 -070072static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -070073{
Simon Glass15310342016-02-22 22:55:37 -070074 fprintf(stderr, "Error: %s\n", msg);
Simon Glassb0a487a2016-02-22 22:55:36 -070075 fprintf(stderr, "Usage: %s -l image\n"
76 " -l ==> list image header information\n",
77 params.cmdname);
78 fprintf(stderr,
79 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
80 " -A ==> set architecture to 'arch'\n"
81 " -O ==> set operating system to 'os'\n"
82 " -T ==> set image type to 'type'\n"
83 " -C ==> set compression type 'comp'\n"
84 " -a ==> set load address to 'addr' (hex)\n"
85 " -e ==> set entry point to 'ep' (hex)\n"
86 " -n ==> set image name to 'name'\n"
87 " -d ==> use image data from 'datafile'\n"
88 " -x ==> set XIP (execute in place)\n",
89 params.cmdname);
90 fprintf(stderr,
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +010091 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
Vagrant Cascadian82bd2f22016-10-23 20:45:17 -070092 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glassb0a487a2016-02-22 22:55:36 -070093 params.cmdname);
94 fprintf(stderr,
95 " -D => set all options for device tree compiler\n"
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +010096 " -f => input filename for FIT source\n"
97 " -i => input filename for ramdisk file\n");
Simon Glassb0a487a2016-02-22 22:55:36 -070098#ifdef CONFIG_FIT_SIGNATURE
99 fprintf(stderr,
Teddy Reedf8f91072016-06-09 19:38:02 -0700100 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
101 " -E => place data outside of the FIT structure\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700102 " -k => set directory containing private keys\n"
103 " -K => write public keys to this .dtb file\n"
104 " -c => add comment in signature node\n"
105 " -F => re-sign existing FIT image\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700106 " -p => place external data at a static position\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700107 " -r => mark keys used as 'required' in dtb\n");
108#else
109 fprintf(stderr,
110 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
111#endif
112 fprintf(stderr, " %s -V ==> print version information and exit\n",
113 params.cmdname);
114 fprintf(stderr, "Use -T to see a list of available image types\n");
115
116 exit(EXIT_FAILURE);
117}
118
Simon Glassfb4cce02016-02-22 22:55:52 -0700119static int add_content(int type, const char *fname)
120{
121 struct content_info *cont;
122
123 cont = calloc(1, sizeof(*cont));
124 if (!cont)
125 return -1;
126 cont->type = type;
127 cont->fname = fname;
128 if (params.content_tail)
129 params.content_tail->next = cont;
130 else
131 params.content_head = cont;
132 params.content_tail = cont;
133
134 return 0;
135}
136
Simon Glass0b443de2016-02-22 22:55:33 -0700137static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000138{
Peter Tysera2513e22010-04-04 22:36:03 -0500139 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700140 int type = IH_TYPE_INVALID;
141 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700142 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000143
Simon Glassa02221f2016-02-22 22:55:34 -0700144 while ((opt = getopt(argc, argv,
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100145 "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:p:O:rR:qsT:vVx")) != -1) {
Simon Glassa02221f2016-02-22 22:55:34 -0700146 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700147 case 'a':
148 params.addr = strtoull(optarg, &ptr, 16);
149 if (*ptr) {
150 fprintf(stderr, "%s: invalid load address %s\n",
151 params.cmdname, optarg);
152 exit(EXIT_FAILURE);
153 }
Simon Glassa02221f2016-02-22 22:55:34 -0700154 break;
155 case 'A':
156 params.arch = genimg_get_arch_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600157 if (params.arch < 0) {
158 show_valid_options(IH_ARCH);
Simon Glass15310342016-02-22 22:55:37 -0700159 usage("Invalid architecture");
Simon Glass51f03e62016-06-30 10:52:19 -0600160 }
Simon Glassa02221f2016-02-22 22:55:34 -0700161 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700162 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200163 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200164 fprintf(stderr,
165 "%s: Out of memory adding content '%s'",
166 params.cmdname, optarg);
167 exit(EXIT_FAILURE);
168 }
Simon Glassfb4cce02016-02-22 22:55:52 -0700169 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700170 case 'c':
171 params.comment = optarg;
172 break;
173 case 'C':
174 params.comp = genimg_get_comp_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600175 if (params.comp < 0) {
176 show_valid_options(IH_COMP);
Simon Glass15310342016-02-22 22:55:37 -0700177 usage("Invalid compression type");
Simon Glass51f03e62016-06-30 10:52:19 -0600178 }
Simon Glassa02221f2016-02-22 22:55:34 -0700179 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700180 case 'd':
181 params.datafile = optarg;
182 params.dflag = 1;
183 break;
Simon Glass07450082016-02-22 22:55:35 -0700184 case 'D':
185 params.dtc = optarg;
186 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700187 case 'e':
188 params.ep = strtoull(optarg, &ptr, 16);
189 if (*ptr) {
190 fprintf(stderr, "%s: invalid entry point %s\n",
191 params.cmdname, optarg);
192 exit(EXIT_FAILURE);
193 }
194 params.eflag = 1;
195 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700196 case 'E':
197 params.external_data = true;
198 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700199 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700200 datafile = optarg;
201 params.auto_its = !strcmp(datafile, "auto");
Simon Glassa02221f2016-02-22 22:55:34 -0700202 /* no break */
203 case 'F':
204 /*
205 * The flattened image tree (FIT) format
206 * requires a flattened device tree image type
207 */
208 params.type = IH_TYPE_FLATDT;
209 params.fflag = 1;
210 break;
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100211 case 'i':
212 params.fit_ramdisk = optarg;
213 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700214 case 'k':
215 params.keydir = optarg;
216 break;
217 case 'K':
218 params.keydest = optarg;
219 break;
Simon Glass07450082016-02-22 22:55:35 -0700220 case 'l':
221 params.lflag = 1;
222 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700223 case 'n':
224 params.imagename = optarg;
225 break;
Simon Glass07450082016-02-22 22:55:35 -0700226 case 'O':
227 params.os = genimg_get_os_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600228 if (params.os < 0) {
229 show_valid_options(IH_OS);
Simon Glass15310342016-02-22 22:55:37 -0700230 usage("Invalid operating system");
Simon Glass51f03e62016-06-30 10:52:19 -0600231 }
Simon Glass07450082016-02-22 22:55:35 -0700232 break;
Teddy Reedf8f91072016-06-09 19:38:02 -0700233 case 'p':
234 params.external_offset = strtoull(optarg, &ptr, 16);
235 if (*ptr) {
236 fprintf(stderr, "%s: invalid offset size %s\n",
237 params.cmdname, optarg);
238 exit(EXIT_FAILURE);
239 }
Teddy Reedb6fefa72016-07-11 22:54:26 -0700240 break;
Simon Glassbd6e1422016-05-01 13:55:38 -0600241 case 'q':
242 params.quiet = 1;
243 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700244 case 'r':
245 params.require_keys = 1;
246 break;
247 case 'R':
248 /*
249 * This entry is for the second configuration
250 * file, if only one is not enough.
251 */
252 params.imagename2 = optarg;
253 break;
254 case 's':
255 params.skipcpy = 1;
256 break;
Simon Glass07450082016-02-22 22:55:35 -0700257 case 'T':
Simon Glassd505a092016-02-22 22:55:48 -0700258 type = genimg_get_type_id(optarg);
259 if (type < 0) {
Simon Glassf24e1052016-06-30 10:52:18 -0600260 show_valid_options(IH_TYPE);
Simon Glass15310342016-02-22 22:55:37 -0700261 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700262 }
263 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700264 case 'v':
265 params.vflag++;
266 break;
267 case 'V':
268 printf("mkimage version %s\n", PLAIN_VERSION);
269 exit(EXIT_SUCCESS);
270 case 'x':
271 params.xflag++;
272 break;
273 default:
Simon Glass15310342016-02-22 22:55:37 -0700274 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000275 }
wdenk5b1d7132002-11-03 00:07:02 +0000276 }
277
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200278 /* The last parameter is expected to be the imagefile */
279 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200280 params.imagefile = argv[optind];
281
Simon Glassd505a092016-02-22 22:55:48 -0700282 /*
283 * For auto-generated FIT images we need to know the image type to put
284 * in the FIT, which is separate from the file's image type (which
285 * will always be IH_TYPE_FLATDT in this case).
286 */
287 if (params.type == IH_TYPE_FLATDT) {
Simon Glass20deadd2016-06-30 10:52:07 -0600288 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600289 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700290 if (!params.auto_its)
291 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600292 else if (!params.datafile)
293 usage("Missing data file for auto-FIT (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700294 } else if (type != IH_TYPE_INVALID) {
295 params.type = type;
296 }
297
298 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700299 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700300}
301
Simon Glass0b443de2016-02-22 22:55:33 -0700302int main(int argc, char **argv)
303{
304 int ifd = -1;
305 struct stat sbuf;
306 char *ptr;
307 int retval = 0;
308 struct image_type_params *tparams = NULL;
309 int pad_len = 0;
310 int dfd;
311
312 params.cmdname = *argv;
313 params.addr = 0;
314 params.ep = 0;
315
316 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000317
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530318 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200319 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530320 if (tparams == NULL) {
321 fprintf (stderr, "%s: unsupported type %s\n",
322 params.cmdname, genimg_get_type_name(params.type));
323 exit (EXIT_FAILURE);
324 }
325
326 /*
327 * check the passed arguments parameters meets the requirements
328 * as per image type to be generated/listed
329 */
330 if (tparams->check_params)
331 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700332 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530333
334 if (!params.eflag) {
335 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000336 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530337 if (params.xflag)
338 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000339 }
340
Peter Tyserc81296c2009-11-24 16:42:10 -0600341 if (params.fflag){
342 if (tparams->fflag_handle)
343 /*
344 * in some cases, some additional processing needs
345 * to be done if fflag is defined
346 *
347 * For ex. fit_handle_file for Fit file support
348 */
349 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000350
Peter Tyserc81296c2009-11-24 16:42:10 -0600351 if (retval != EXIT_SUCCESS)
352 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000353 }
354
Peter Tyserc81296c2009-11-24 16:42:10 -0600355 if (params.lflag || params.fflag) {
356 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
357 } else {
358 ifd = open (params.imagefile,
359 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
360 }
361
362 if (ifd < 0) {
363 fprintf (stderr, "%s: Can't open %s: %s\n",
364 params.cmdname, params.imagefile,
365 strerror(errno));
366 exit (EXIT_FAILURE);
367 }
368
369 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000370 /*
371 * list header information of existing image
372 */
373 if (fstat(ifd, &sbuf) < 0) {
374 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530375 params.cmdname, params.imagefile,
376 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000377 exit (EXIT_FAILURE);
378 }
379
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530380 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000381 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530382 "%s: Bad size: \"%s\" is not valid image\n",
383 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000384 exit (EXIT_FAILURE);
385 }
386
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400387 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
388 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000389 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530390 params.cmdname, params.imagefile,
391 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000392 exit (EXIT_FAILURE);
393 }
394
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530395 /*
396 * scan through mkimage registry for all supported image types
397 * and verify the input image file header for match
398 * Print the image information for matched image type
399 * Returns the error code if not matched
400 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200401 retval = imagetool_verify_print_header(ptr, &sbuf,
402 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000403
wdenk5b1d7132002-11-03 00:07:02 +0000404 (void) munmap((void *)ptr, sbuf.st_size);
405 (void) close (ifd);
406
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530407 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000408 }
409
Marek Vasut34633142015-12-07 18:01:54 +0100410 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200411 dfd = open(params.datafile, O_RDONLY | O_BINARY);
412 if (dfd < 0) {
413 fprintf(stderr, "%s: Can't open %s: %s\n",
414 params.cmdname, params.datafile,
415 strerror(errno));
416 exit(EXIT_FAILURE);
417 }
Simon Glass92a655c2015-06-23 15:39:12 -0600418
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200419 if (fstat(dfd, &sbuf) < 0) {
420 fprintf(stderr, "%s: Can't stat %s: %s\n",
421 params.cmdname, params.datafile,
422 strerror(errno));
423 exit(EXIT_FAILURE);
424 }
Simon Glass92a655c2015-06-23 15:39:12 -0600425
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200426 params.file_size = sbuf.st_size + tparams->header_size;
427 close(dfd);
428 }
Simon Glass92a655c2015-06-23 15:39:12 -0600429
wdenk5b1d7132002-11-03 00:07:02 +0000430 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000431 * In case there an header with a variable
432 * length will be added, the corresponding
433 * function is called. This is responsible to
434 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000435 */
Stefano Babicf0662102011-09-15 23:50:16 +0000436 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200437 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000438 else
439 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000440
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530441 if (write(ifd, tparams->hdr, tparams->header_size)
442 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000443 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530444 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000445 exit (EXIT_FAILURE);
446 }
447
Christian Rieschd1be8f92011-12-09 09:47:38 +0000448 if (!params.skipcpy) {
449 if (params.type == IH_TYPE_MULTI ||
450 params.type == IH_TYPE_SCRIPT) {
451 char *file = params.datafile;
452 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000453
Christian Rieschd1be8f92011-12-09 09:47:38 +0000454 for (;;) {
455 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000456
Christian Rieschd1be8f92011-12-09 09:47:38 +0000457 if (file) {
458 if ((sep = strchr(file, ':')) != NULL) {
459 *sep = '\0';
460 }
461
462 if (stat (file, &sbuf) < 0) {
463 fprintf (stderr, "%s: Can't stat %s: %s\n",
464 params.cmdname, file, strerror(errno));
465 exit (EXIT_FAILURE);
466 }
467 size = cpu_to_uimage (sbuf.st_size);
468 } else {
469 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000470 }
471
Christian Rieschd1be8f92011-12-09 09:47:38 +0000472 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
473 fprintf (stderr, "%s: Write error on %s: %s\n",
474 params.cmdname, params.imagefile,
475 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000476 exit (EXIT_FAILURE);
477 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000478
479 if (!file) {
480 break;
481 }
482
483 if (sep) {
484 *sep = ':';
485 file = sep + 1;
486 } else {
487 file = NULL;
488 }
wdenk5b1d7132002-11-03 00:07:02 +0000489 }
490
Christian Rieschd1be8f92011-12-09 09:47:38 +0000491 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000492
Christian Rieschd1be8f92011-12-09 09:47:38 +0000493 for (;;) {
494 char *sep = strchr(file, ':');
495 if (sep) {
496 *sep = '\0';
497 copy_file (ifd, file, 1);
498 *sep++ = ':';
499 file = sep;
500 } else {
501 copy_file (ifd, file, 0);
502 break;
503 }
wdenk5b1d7132002-11-03 00:07:02 +0000504 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000505 } else if (params.type == IH_TYPE_PBLIMAGE) {
506 /* PBL has special Image format, implements its' own */
507 pbl_load_uboot(ifd, &params);
Christian Rieschd1be8f92011-12-09 09:47:38 +0000508 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200509 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000510 }
wdenk5b1d7132002-11-03 00:07:02 +0000511 }
512
513 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530514#if defined(_POSIX_SYNCHRONIZED_IO) && \
515 !defined(__sun__) && \
516 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200517 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530518 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000519 (void) fdatasync (ifd);
520#else
521 (void) fsync (ifd);
522#endif
523
524 if (fstat(ifd, &sbuf) < 0) {
525 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530526 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000527 exit (EXIT_FAILURE);
528 }
Simon Glass92a655c2015-06-23 15:39:12 -0600529 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000530
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400531 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
532 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000533 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530534 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000535 exit (EXIT_FAILURE);
536 }
537
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530538 /* Setup the image header as per input image type*/
539 if (tparams->set_header)
540 tparams->set_header (ptr, &sbuf, ifd, &params);
541 else {
542 fprintf (stderr, "%s: Can't set header for %s: %s\n",
543 params.cmdname, tparams->name, strerror(errno));
544 exit (EXIT_FAILURE);
545 }
wdenk5b1d7132002-11-03 00:07:02 +0000546
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530547 /* Print the image information by processing image header */
548 if (tparams->print_header)
549 tparams->print_header (ptr);
550 else {
551 fprintf (stderr, "%s: Can't print header for %s: %s\n",
552 params.cmdname, tparams->name, strerror(errno));
553 exit (EXIT_FAILURE);
554 }
wdenk5b1d7132002-11-03 00:07:02 +0000555
556 (void) munmap((void *)ptr, sbuf.st_size);
557
558 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530559#if defined(_POSIX_SYNCHRONIZED_IO) && \
560 !defined(__sun__) && \
561 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200562 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530563 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000564 (void) fdatasync (ifd);
565#else
566 (void) fsync (ifd);
567#endif
568
569 if (close(ifd)) {
570 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530571 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000572 exit (EXIT_FAILURE);
573 }
574
575 exit (EXIT_SUCCESS);
576}
577
578static void
579copy_file (int ifd, const char *datafile, int pad)
580{
581 int dfd;
582 struct stat sbuf;
583 unsigned char *ptr;
584 int tail;
585 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200586 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000587 int offset = 0;
588 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200589 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000590
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200591 memset(zeros, 0, sizeof(zeros));
592
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530593 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000594 fprintf (stderr, "Adding Image %s\n", datafile);
595 }
596
wdenkef1464c2003-10-08 22:14:02 +0000597 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000598 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530599 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000600 exit (EXIT_FAILURE);
601 }
602
603 if (fstat(dfd, &sbuf) < 0) {
604 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530605 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000606 exit (EXIT_FAILURE);
607 }
608
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400609 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
610 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000611 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530612 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000613 exit (EXIT_FAILURE);
614 }
615
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530616 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000617 unsigned char *p = NULL;
618 /*
619 * XIP: do not append the image_header_t at the
620 * beginning of the file, but consume the space
621 * reserved for it.
622 */
623
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530624 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000625 fprintf (stderr,
626 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530627 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000628 exit (EXIT_FAILURE);
629 }
630
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530631 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000632 if ( *p != 0xff ) {
633 fprintf (stderr,
634 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530635 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000636 exit (EXIT_FAILURE);
637 }
638 }
639
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530640 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000641 }
642
643 size = sbuf.st_size - offset;
644 if (write(ifd, ptr + offset, size) != size) {
645 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530646 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000647 exit (EXIT_FAILURE);
648 }
649
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200650 tail = size % 4;
651 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000652
653 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
654 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530655 params.cmdname, params.imagefile,
656 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000657 exit (EXIT_FAILURE);
658 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200659 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600660 while (pad > 0) {
661 int todo = sizeof(zeros);
662
663 if (todo > pad)
664 todo = pad;
665 if (write(ifd, (char *)&zeros, todo) != todo) {
666 fprintf(stderr, "%s: Write error on %s: %s\n",
667 params.cmdname, params.imagefile,
668 strerror(errno));
669 exit(EXIT_FAILURE);
670 }
671 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200672 }
wdenk5b1d7132002-11-03 00:07:02 +0000673 }
674
675 (void) munmap((void *)ptr, sbuf.st_size);
676 (void) close (dfd);
677}