blob: 3c594a0f3b92f1cf73b282c85b36674838c65fa0 [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");
67
68 return 0;
69}
70
Simon Glass15310342016-02-22 22:55:37 -070071static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -070072{
Simon Glass15310342016-02-22 22:55:37 -070073 fprintf(stderr, "Error: %s\n", msg);
Simon Glassb0a487a2016-02-22 22:55:36 -070074 fprintf(stderr, "Usage: %s -l image\n"
75 " -l ==> list image header information\n",
76 params.cmdname);
77 fprintf(stderr,
78 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
79 " -A ==> set architecture to 'arch'\n"
80 " -O ==> set operating system to 'os'\n"
81 " -T ==> set image type to 'type'\n"
82 " -C ==> set compression type 'comp'\n"
83 " -a ==> set load address to 'addr' (hex)\n"
84 " -e ==> set entry point to 'ep' (hex)\n"
85 " -n ==> set image name to 'name'\n"
86 " -d ==> use image data from 'datafile'\n"
87 " -x ==> set XIP (execute in place)\n",
88 params.cmdname);
89 fprintf(stderr,
Andreas Bießmann7a439ca2016-05-01 03:01:27 +020090 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n"
91 " <dtb> file is used with -f auto, it may occour multiple times.\n",
Simon Glassb0a487a2016-02-22 22:55:36 -070092 params.cmdname);
93 fprintf(stderr,
94 " -D => set all options for device tree compiler\n"
95 " -f => input filename for FIT source\n");
96#ifdef CONFIG_FIT_SIGNATURE
97 fprintf(stderr,
Teddy Reedf8f91072016-06-09 19:38:02 -070098 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
99 " -E => place data outside of the FIT structure\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700100 " -k => set directory containing private keys\n"
101 " -K => write public keys to this .dtb file\n"
102 " -c => add comment in signature node\n"
103 " -F => re-sign existing FIT image\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700104 " -p => place external data at a static position\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700105 " -r => mark keys used as 'required' in dtb\n");
106#else
107 fprintf(stderr,
108 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
109#endif
110 fprintf(stderr, " %s -V ==> print version information and exit\n",
111 params.cmdname);
112 fprintf(stderr, "Use -T to see a list of available image types\n");
113
114 exit(EXIT_FAILURE);
115}
116
Simon Glassfb4cce02016-02-22 22:55:52 -0700117static int add_content(int type, const char *fname)
118{
119 struct content_info *cont;
120
121 cont = calloc(1, sizeof(*cont));
122 if (!cont)
123 return -1;
124 cont->type = type;
125 cont->fname = fname;
126 if (params.content_tail)
127 params.content_tail->next = cont;
128 else
129 params.content_head = cont;
130 params.content_tail = cont;
131
132 return 0;
133}
134
Simon Glass0b443de2016-02-22 22:55:33 -0700135static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000136{
Peter Tysera2513e22010-04-04 22:36:03 -0500137 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700138 int type = IH_TYPE_INVALID;
139 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700140 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000141
Simon Glassa02221f2016-02-22 22:55:34 -0700142 while ((opt = getopt(argc, argv,
Karl Beldan1b92aed2016-08-02 18:57:14 +0000143 "a:A:b:c:C:d:D:e:Ef:Fk:K:ln:p:O:rR:qsT:vVx")) != -1) {
Simon Glassa02221f2016-02-22 22:55:34 -0700144 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700145 case 'a':
146 params.addr = strtoull(optarg, &ptr, 16);
147 if (*ptr) {
148 fprintf(stderr, "%s: invalid load address %s\n",
149 params.cmdname, optarg);
150 exit(EXIT_FAILURE);
151 }
Simon Glassa02221f2016-02-22 22:55:34 -0700152 break;
153 case 'A':
154 params.arch = genimg_get_arch_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600155 if (params.arch < 0) {
156 show_valid_options(IH_ARCH);
Simon Glass15310342016-02-22 22:55:37 -0700157 usage("Invalid architecture");
Simon Glass51f03e62016-06-30 10:52:19 -0600158 }
Simon Glassa02221f2016-02-22 22:55:34 -0700159 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700160 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200161 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200162 fprintf(stderr,
163 "%s: Out of memory adding content '%s'",
164 params.cmdname, optarg);
165 exit(EXIT_FAILURE);
166 }
Simon Glassfb4cce02016-02-22 22:55:52 -0700167 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700168 case 'c':
169 params.comment = optarg;
170 break;
171 case 'C':
172 params.comp = genimg_get_comp_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600173 if (params.comp < 0) {
174 show_valid_options(IH_COMP);
Simon Glass15310342016-02-22 22:55:37 -0700175 usage("Invalid compression type");
Simon Glass51f03e62016-06-30 10:52:19 -0600176 }
Simon Glassa02221f2016-02-22 22:55:34 -0700177 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700178 case 'd':
179 params.datafile = optarg;
180 params.dflag = 1;
181 break;
Simon Glass07450082016-02-22 22:55:35 -0700182 case 'D':
183 params.dtc = optarg;
184 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700185 case 'e':
186 params.ep = strtoull(optarg, &ptr, 16);
187 if (*ptr) {
188 fprintf(stderr, "%s: invalid entry point %s\n",
189 params.cmdname, optarg);
190 exit(EXIT_FAILURE);
191 }
192 params.eflag = 1;
193 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700194 case 'E':
195 params.external_data = true;
196 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700197 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700198 datafile = optarg;
199 params.auto_its = !strcmp(datafile, "auto");
Simon Glassa02221f2016-02-22 22:55:34 -0700200 /* no break */
201 case 'F':
202 /*
203 * The flattened image tree (FIT) format
204 * requires a flattened device tree image type
205 */
206 params.type = IH_TYPE_FLATDT;
207 params.fflag = 1;
208 break;
209 case 'k':
210 params.keydir = optarg;
211 break;
212 case 'K':
213 params.keydest = optarg;
214 break;
Simon Glass07450082016-02-22 22:55:35 -0700215 case 'l':
216 params.lflag = 1;
217 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700218 case 'n':
219 params.imagename = optarg;
220 break;
Simon Glass07450082016-02-22 22:55:35 -0700221 case 'O':
222 params.os = genimg_get_os_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600223 if (params.os < 0) {
224 show_valid_options(IH_OS);
Simon Glass15310342016-02-22 22:55:37 -0700225 usage("Invalid operating system");
Simon Glass51f03e62016-06-30 10:52:19 -0600226 }
Simon Glass07450082016-02-22 22:55:35 -0700227 break;
Teddy Reedf8f91072016-06-09 19:38:02 -0700228 case 'p':
229 params.external_offset = strtoull(optarg, &ptr, 16);
230 if (*ptr) {
231 fprintf(stderr, "%s: invalid offset size %s\n",
232 params.cmdname, optarg);
233 exit(EXIT_FAILURE);
234 }
Teddy Reedb6fefa72016-07-11 22:54:26 -0700235 break;
Simon Glassbd6e1422016-05-01 13:55:38 -0600236 case 'q':
237 params.quiet = 1;
238 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700239 case 'r':
240 params.require_keys = 1;
241 break;
242 case 'R':
243 /*
244 * This entry is for the second configuration
245 * file, if only one is not enough.
246 */
247 params.imagename2 = optarg;
248 break;
249 case 's':
250 params.skipcpy = 1;
251 break;
Simon Glass07450082016-02-22 22:55:35 -0700252 case 'T':
Simon Glassd505a092016-02-22 22:55:48 -0700253 type = genimg_get_type_id(optarg);
254 if (type < 0) {
Simon Glassf24e1052016-06-30 10:52:18 -0600255 show_valid_options(IH_TYPE);
Simon Glass15310342016-02-22 22:55:37 -0700256 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700257 }
258 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700259 case 'v':
260 params.vflag++;
261 break;
262 case 'V':
263 printf("mkimage version %s\n", PLAIN_VERSION);
264 exit(EXIT_SUCCESS);
265 case 'x':
266 params.xflag++;
267 break;
268 default:
Simon Glass15310342016-02-22 22:55:37 -0700269 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000270 }
wdenk5b1d7132002-11-03 00:07:02 +0000271 }
272
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200273 /* The last parameter is expected to be the imagefile */
274 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200275 params.imagefile = argv[optind];
276
Simon Glassd505a092016-02-22 22:55:48 -0700277 /*
278 * For auto-generated FIT images we need to know the image type to put
279 * in the FIT, which is separate from the file's image type (which
280 * will always be IH_TYPE_FLATDT in this case).
281 */
282 if (params.type == IH_TYPE_FLATDT) {
Simon Glass20deadd2016-06-30 10:52:07 -0600283 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600284 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700285 if (!params.auto_its)
286 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600287 else if (!params.datafile)
288 usage("Missing data file for auto-FIT (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700289 } else if (type != IH_TYPE_INVALID) {
290 params.type = type;
291 }
292
293 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700294 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700295}
296
Simon Glass0b443de2016-02-22 22:55:33 -0700297int main(int argc, char **argv)
298{
299 int ifd = -1;
300 struct stat sbuf;
301 char *ptr;
302 int retval = 0;
303 struct image_type_params *tparams = NULL;
304 int pad_len = 0;
305 int dfd;
306
307 params.cmdname = *argv;
308 params.addr = 0;
309 params.ep = 0;
310
311 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000312
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530313 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200314 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530315 if (tparams == NULL) {
316 fprintf (stderr, "%s: unsupported type %s\n",
317 params.cmdname, genimg_get_type_name(params.type));
318 exit (EXIT_FAILURE);
319 }
320
321 /*
322 * check the passed arguments parameters meets the requirements
323 * as per image type to be generated/listed
324 */
325 if (tparams->check_params)
326 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700327 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530328
329 if (!params.eflag) {
330 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000331 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530332 if (params.xflag)
333 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000334 }
335
Peter Tyserc81296c2009-11-24 16:42:10 -0600336 if (params.fflag){
337 if (tparams->fflag_handle)
338 /*
339 * in some cases, some additional processing needs
340 * to be done if fflag is defined
341 *
342 * For ex. fit_handle_file for Fit file support
343 */
344 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000345
Peter Tyserc81296c2009-11-24 16:42:10 -0600346 if (retval != EXIT_SUCCESS)
347 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000348 }
349
Peter Tyserc81296c2009-11-24 16:42:10 -0600350 if (params.lflag || params.fflag) {
351 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
352 } else {
353 ifd = open (params.imagefile,
354 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
355 }
356
357 if (ifd < 0) {
358 fprintf (stderr, "%s: Can't open %s: %s\n",
359 params.cmdname, params.imagefile,
360 strerror(errno));
361 exit (EXIT_FAILURE);
362 }
363
364 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000365 /*
366 * list header information of existing image
367 */
368 if (fstat(ifd, &sbuf) < 0) {
369 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530370 params.cmdname, params.imagefile,
371 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000372 exit (EXIT_FAILURE);
373 }
374
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530375 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000376 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530377 "%s: Bad size: \"%s\" is not valid image\n",
378 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000379 exit (EXIT_FAILURE);
380 }
381
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400382 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
383 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000384 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530385 params.cmdname, params.imagefile,
386 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000387 exit (EXIT_FAILURE);
388 }
389
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530390 /*
391 * scan through mkimage registry for all supported image types
392 * and verify the input image file header for match
393 * Print the image information for matched image type
394 * Returns the error code if not matched
395 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200396 retval = imagetool_verify_print_header(ptr, &sbuf,
397 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000398
wdenk5b1d7132002-11-03 00:07:02 +0000399 (void) munmap((void *)ptr, sbuf.st_size);
400 (void) close (ifd);
401
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530402 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000403 }
404
Marek Vasut34633142015-12-07 18:01:54 +0100405 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200406 dfd = open(params.datafile, O_RDONLY | O_BINARY);
407 if (dfd < 0) {
408 fprintf(stderr, "%s: Can't open %s: %s\n",
409 params.cmdname, params.datafile,
410 strerror(errno));
411 exit(EXIT_FAILURE);
412 }
Simon Glass92a655c2015-06-23 15:39:12 -0600413
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200414 if (fstat(dfd, &sbuf) < 0) {
415 fprintf(stderr, "%s: Can't stat %s: %s\n",
416 params.cmdname, params.datafile,
417 strerror(errno));
418 exit(EXIT_FAILURE);
419 }
Simon Glass92a655c2015-06-23 15:39:12 -0600420
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200421 params.file_size = sbuf.st_size + tparams->header_size;
422 close(dfd);
423 }
Simon Glass92a655c2015-06-23 15:39:12 -0600424
wdenk5b1d7132002-11-03 00:07:02 +0000425 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000426 * In case there an header with a variable
427 * length will be added, the corresponding
428 * function is called. This is responsible to
429 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000430 */
Stefano Babicf0662102011-09-15 23:50:16 +0000431 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200432 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000433 else
434 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000435
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530436 if (write(ifd, tparams->hdr, tparams->header_size)
437 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000438 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530439 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000440 exit (EXIT_FAILURE);
441 }
442
Christian Rieschd1be8f92011-12-09 09:47:38 +0000443 if (!params.skipcpy) {
444 if (params.type == IH_TYPE_MULTI ||
445 params.type == IH_TYPE_SCRIPT) {
446 char *file = params.datafile;
447 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000448
Christian Rieschd1be8f92011-12-09 09:47:38 +0000449 for (;;) {
450 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000451
Christian Rieschd1be8f92011-12-09 09:47:38 +0000452 if (file) {
453 if ((sep = strchr(file, ':')) != NULL) {
454 *sep = '\0';
455 }
456
457 if (stat (file, &sbuf) < 0) {
458 fprintf (stderr, "%s: Can't stat %s: %s\n",
459 params.cmdname, file, strerror(errno));
460 exit (EXIT_FAILURE);
461 }
462 size = cpu_to_uimage (sbuf.st_size);
463 } else {
464 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000465 }
466
Christian Rieschd1be8f92011-12-09 09:47:38 +0000467 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
468 fprintf (stderr, "%s: Write error on %s: %s\n",
469 params.cmdname, params.imagefile,
470 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000471 exit (EXIT_FAILURE);
472 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000473
474 if (!file) {
475 break;
476 }
477
478 if (sep) {
479 *sep = ':';
480 file = sep + 1;
481 } else {
482 file = NULL;
483 }
wdenk5b1d7132002-11-03 00:07:02 +0000484 }
485
Christian Rieschd1be8f92011-12-09 09:47:38 +0000486 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000487
Christian Rieschd1be8f92011-12-09 09:47:38 +0000488 for (;;) {
489 char *sep = strchr(file, ':');
490 if (sep) {
491 *sep = '\0';
492 copy_file (ifd, file, 1);
493 *sep++ = ':';
494 file = sep;
495 } else {
496 copy_file (ifd, file, 0);
497 break;
498 }
wdenk5b1d7132002-11-03 00:07:02 +0000499 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000500 } else if (params.type == IH_TYPE_PBLIMAGE) {
501 /* PBL has special Image format, implements its' own */
502 pbl_load_uboot(ifd, &params);
Christian Rieschd1be8f92011-12-09 09:47:38 +0000503 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200504 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000505 }
wdenk5b1d7132002-11-03 00:07:02 +0000506 }
507
508 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530509#if defined(_POSIX_SYNCHRONIZED_IO) && \
510 !defined(__sun__) && \
511 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200512 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530513 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000514 (void) fdatasync (ifd);
515#else
516 (void) fsync (ifd);
517#endif
518
519 if (fstat(ifd, &sbuf) < 0) {
520 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530521 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000522 exit (EXIT_FAILURE);
523 }
Simon Glass92a655c2015-06-23 15:39:12 -0600524 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000525
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400526 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
527 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000528 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530529 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000530 exit (EXIT_FAILURE);
531 }
532
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530533 /* Setup the image header as per input image type*/
534 if (tparams->set_header)
535 tparams->set_header (ptr, &sbuf, ifd, &params);
536 else {
537 fprintf (stderr, "%s: Can't set header for %s: %s\n",
538 params.cmdname, tparams->name, strerror(errno));
539 exit (EXIT_FAILURE);
540 }
wdenk5b1d7132002-11-03 00:07:02 +0000541
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530542 /* Print the image information by processing image header */
543 if (tparams->print_header)
544 tparams->print_header (ptr);
545 else {
546 fprintf (stderr, "%s: Can't print header for %s: %s\n",
547 params.cmdname, tparams->name, strerror(errno));
548 exit (EXIT_FAILURE);
549 }
wdenk5b1d7132002-11-03 00:07:02 +0000550
551 (void) munmap((void *)ptr, sbuf.st_size);
552
553 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530554#if defined(_POSIX_SYNCHRONIZED_IO) && \
555 !defined(__sun__) && \
556 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200557 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530558 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000559 (void) fdatasync (ifd);
560#else
561 (void) fsync (ifd);
562#endif
563
564 if (close(ifd)) {
565 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530566 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000567 exit (EXIT_FAILURE);
568 }
569
570 exit (EXIT_SUCCESS);
571}
572
573static void
574copy_file (int ifd, const char *datafile, int pad)
575{
576 int dfd;
577 struct stat sbuf;
578 unsigned char *ptr;
579 int tail;
580 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200581 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000582 int offset = 0;
583 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200584 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000585
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200586 memset(zeros, 0, sizeof(zeros));
587
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530588 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000589 fprintf (stderr, "Adding Image %s\n", datafile);
590 }
591
wdenkef1464c2003-10-08 22:14:02 +0000592 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000593 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530594 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000595 exit (EXIT_FAILURE);
596 }
597
598 if (fstat(dfd, &sbuf) < 0) {
599 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530600 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000601 exit (EXIT_FAILURE);
602 }
603
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400604 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
605 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000606 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530607 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000608 exit (EXIT_FAILURE);
609 }
610
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530611 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000612 unsigned char *p = NULL;
613 /*
614 * XIP: do not append the image_header_t at the
615 * beginning of the file, but consume the space
616 * reserved for it.
617 */
618
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530619 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000620 fprintf (stderr,
621 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530622 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000623 exit (EXIT_FAILURE);
624 }
625
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530626 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000627 if ( *p != 0xff ) {
628 fprintf (stderr,
629 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530630 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000631 exit (EXIT_FAILURE);
632 }
633 }
634
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530635 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000636 }
637
638 size = sbuf.st_size - offset;
639 if (write(ifd, ptr + offset, size) != size) {
640 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530641 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000642 exit (EXIT_FAILURE);
643 }
644
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200645 tail = size % 4;
646 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000647
648 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
649 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530650 params.cmdname, params.imagefile,
651 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000652 exit (EXIT_FAILURE);
653 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200654 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600655 while (pad > 0) {
656 int todo = sizeof(zeros);
657
658 if (todo > pad)
659 todo = pad;
660 if (write(ifd, (char *)&zeros, todo) != todo) {
661 fprintf(stderr, "%s: Write error on %s: %s\n",
662 params.cmdname, params.imagefile,
663 strerror(errno));
664 exit(EXIT_FAILURE);
665 }
666 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200667 }
wdenk5b1d7132002-11-03 00:07:02 +0000668 }
669
670 (void) munmap((void *)ptr, sbuf.st_size);
671 (void) close (dfd);
672}