blob: f48135ff7988d546262185835c1bd9ea07c0eaeb [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"
Sven Ebenfeldd21bd692016-11-06 16:37:56 +010012#include "imximage.h"
wdenk5b1d7132002-11-03 00:07:02 +000013#include <image.h>
Wolfgang Denk976b38c2011-02-12 10:37:11 +010014#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000015
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053016static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000017
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053018/* parameters initialized by core will be used by the image type code */
Simon Glasscc7a6442016-02-22 22:55:38 -070019static struct image_tool_params params = {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053020 .os = IH_OS_LINUX,
21 .arch = IH_ARCH_PPC,
22 .type = IH_TYPE_KERNEL,
23 .comp = IH_COMP_GZIP,
24 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
Wolfgang Denk04387d22010-03-27 23:37:46 +010025 .imagename = "",
Shaohui Xie5d898a02012-08-10 02:49:35 +000026 .imagename2 = "",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053027};
wdenk5b1d7132002-11-03 00:07:02 +000028
Simon Glass30664222016-06-30 10:52:17 -060029static enum ih_category cur_category;
30
31static int h_compare_category_name(const void *vtype1, const void *vtype2)
32{
33 const int *type1 = vtype1;
34 const int *type2 = vtype2;
35 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
36 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
37
38 return strcmp(name1, name2);
39}
40
Simon Glassf24e1052016-06-30 10:52:18 -060041static int show_valid_options(enum ih_category category)
Simon Glass30664222016-06-30 10:52:17 -060042{
43 int *order;
44 int count;
45 int item;
46 int i;
47
48 count = genimg_get_cat_count(category);
49 order = calloc(count, sizeof(*order));
50 if (!order)
51 return -ENOMEM;
52
53 /* Sort the names in order of short name for easier reading */
54 for (item = 0; item < count; item++)
55 order[item] = item;
56 cur_category = category;
57 qsort(order, count, sizeof(int), h_compare_category_name);
58
59 fprintf(stderr, "\nInvalid %s, supported are:\n",
60 genimg_get_cat_desc(category));
61 for (i = 0; i < count; i++) {
62 item = order[i];
63 fprintf(stderr, "\t%-15s %s\n",
64 genimg_get_cat_short_name(category, item),
65 genimg_get_cat_name(category, item));
66 }
67 fprintf(stderr, "\n");
Simon Glass0cd82e22016-10-27 17:54:03 -060068 free(order);
Simon Glass30664222016-06-30 10:52:17 -060069
70 return 0;
71}
72
Simon Glass15310342016-02-22 22:55:37 -070073static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -070074{
Simon Glass15310342016-02-22 22:55:37 -070075 fprintf(stderr, "Error: %s\n", msg);
Simon Glassb0a487a2016-02-22 22:55:36 -070076 fprintf(stderr, "Usage: %s -l image\n"
77 " -l ==> list image header information\n",
78 params.cmdname);
79 fprintf(stderr,
80 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
81 " -A ==> set architecture to 'arch'\n"
82 " -O ==> set operating system to 'os'\n"
83 " -T ==> set image type to 'type'\n"
84 " -C ==> set compression type 'comp'\n"
85 " -a ==> set load address to 'addr' (hex)\n"
86 " -e ==> set entry point to 'ep' (hex)\n"
87 " -n ==> set image name to 'name'\n"
88 " -d ==> use image data from 'datafile'\n"
89 " -x ==> set XIP (execute in place)\n",
90 params.cmdname);
91 fprintf(stderr,
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +010092 " %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 -070093 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glassb0a487a2016-02-22 22:55:36 -070094 params.cmdname);
95 fprintf(stderr,
96 " -D => set all options for device tree compiler\n"
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +010097 " -f => input filename for FIT source\n"
98 " -i => input filename for ramdisk file\n");
Simon Glassb0a487a2016-02-22 22:55:36 -070099#ifdef CONFIG_FIT_SIGNATURE
100 fprintf(stderr,
Teddy Reedf8f91072016-06-09 19:38:02 -0700101 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
102 " -E => place data outside of the FIT structure\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700103 " -k => set directory containing private keys\n"
104 " -K => write public keys to this .dtb file\n"
105 " -c => add comment in signature node\n"
106 " -F => re-sign existing FIT image\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700107 " -p => place external data at a static position\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700108 " -r => mark keys used as 'required' in dtb\n");
109#else
110 fprintf(stderr,
111 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
112#endif
113 fprintf(stderr, " %s -V ==> print version information and exit\n",
114 params.cmdname);
115 fprintf(stderr, "Use -T to see a list of available image types\n");
116
117 exit(EXIT_FAILURE);
118}
119
Simon Glassfb4cce02016-02-22 22:55:52 -0700120static int add_content(int type, const char *fname)
121{
122 struct content_info *cont;
123
124 cont = calloc(1, sizeof(*cont));
125 if (!cont)
126 return -1;
127 cont->type = type;
128 cont->fname = fname;
129 if (params.content_tail)
130 params.content_tail->next = cont;
131 else
132 params.content_head = cont;
133 params.content_tail = cont;
134
135 return 0;
136}
137
Simon Glass0b443de2016-02-22 22:55:33 -0700138static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000139{
Peter Tysera2513e22010-04-04 22:36:03 -0500140 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700141 int type = IH_TYPE_INVALID;
142 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700143 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000144
Simon Glassa02221f2016-02-22 22:55:34 -0700145 while ((opt = getopt(argc, argv,
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100146 "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 -0700147 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700148 case 'a':
149 params.addr = strtoull(optarg, &ptr, 16);
150 if (*ptr) {
151 fprintf(stderr, "%s: invalid load address %s\n",
152 params.cmdname, optarg);
153 exit(EXIT_FAILURE);
154 }
Simon Glassa02221f2016-02-22 22:55:34 -0700155 break;
156 case 'A':
157 params.arch = genimg_get_arch_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600158 if (params.arch < 0) {
159 show_valid_options(IH_ARCH);
Simon Glass15310342016-02-22 22:55:37 -0700160 usage("Invalid architecture");
Simon Glass51f03e62016-06-30 10:52:19 -0600161 }
Simon Glassa02221f2016-02-22 22:55:34 -0700162 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700163 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200164 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200165 fprintf(stderr,
166 "%s: Out of memory adding content '%s'",
167 params.cmdname, optarg);
168 exit(EXIT_FAILURE);
169 }
Simon Glassfb4cce02016-02-22 22:55:52 -0700170 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700171 case 'c':
172 params.comment = optarg;
173 break;
174 case 'C':
175 params.comp = genimg_get_comp_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600176 if (params.comp < 0) {
177 show_valid_options(IH_COMP);
Simon Glass15310342016-02-22 22:55:37 -0700178 usage("Invalid compression type");
Simon Glass51f03e62016-06-30 10:52:19 -0600179 }
Simon Glassa02221f2016-02-22 22:55:34 -0700180 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700181 case 'd':
182 params.datafile = optarg;
183 params.dflag = 1;
184 break;
Simon Glass07450082016-02-22 22:55:35 -0700185 case 'D':
186 params.dtc = optarg;
187 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700188 case 'e':
189 params.ep = strtoull(optarg, &ptr, 16);
190 if (*ptr) {
191 fprintf(stderr, "%s: invalid entry point %s\n",
192 params.cmdname, optarg);
193 exit(EXIT_FAILURE);
194 }
195 params.eflag = 1;
196 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700197 case 'E':
198 params.external_data = true;
199 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700200 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700201 datafile = optarg;
202 params.auto_its = !strcmp(datafile, "auto");
Simon Glassa02221f2016-02-22 22:55:34 -0700203 /* no break */
204 case 'F':
205 /*
206 * The flattened image tree (FIT) format
207 * requires a flattened device tree image type
208 */
209 params.type = IH_TYPE_FLATDT;
210 params.fflag = 1;
211 break;
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100212 case 'i':
213 params.fit_ramdisk = optarg;
214 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700215 case 'k':
216 params.keydir = optarg;
217 break;
218 case 'K':
219 params.keydest = optarg;
220 break;
Simon Glass07450082016-02-22 22:55:35 -0700221 case 'l':
222 params.lflag = 1;
223 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700224 case 'n':
225 params.imagename = optarg;
226 break;
Simon Glass07450082016-02-22 22:55:35 -0700227 case 'O':
228 params.os = genimg_get_os_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600229 if (params.os < 0) {
230 show_valid_options(IH_OS);
Simon Glass15310342016-02-22 22:55:37 -0700231 usage("Invalid operating system");
Simon Glass51f03e62016-06-30 10:52:19 -0600232 }
Simon Glass07450082016-02-22 22:55:35 -0700233 break;
Teddy Reedf8f91072016-06-09 19:38:02 -0700234 case 'p':
235 params.external_offset = strtoull(optarg, &ptr, 16);
236 if (*ptr) {
237 fprintf(stderr, "%s: invalid offset size %s\n",
238 params.cmdname, optarg);
239 exit(EXIT_FAILURE);
240 }
Teddy Reedb6fefa72016-07-11 22:54:26 -0700241 break;
Simon Glassbd6e1422016-05-01 13:55:38 -0600242 case 'q':
243 params.quiet = 1;
244 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700245 case 'r':
246 params.require_keys = 1;
247 break;
248 case 'R':
249 /*
250 * This entry is for the second configuration
251 * file, if only one is not enough.
252 */
253 params.imagename2 = optarg;
254 break;
255 case 's':
256 params.skipcpy = 1;
257 break;
Simon Glass07450082016-02-22 22:55:35 -0700258 case 'T':
Simon Glassd505a092016-02-22 22:55:48 -0700259 type = genimg_get_type_id(optarg);
260 if (type < 0) {
Simon Glassf24e1052016-06-30 10:52:18 -0600261 show_valid_options(IH_TYPE);
Simon Glass15310342016-02-22 22:55:37 -0700262 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700263 }
264 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700265 case 'v':
266 params.vflag++;
267 break;
268 case 'V':
269 printf("mkimage version %s\n", PLAIN_VERSION);
270 exit(EXIT_SUCCESS);
271 case 'x':
272 params.xflag++;
273 break;
274 default:
Simon Glass15310342016-02-22 22:55:37 -0700275 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000276 }
wdenk5b1d7132002-11-03 00:07:02 +0000277 }
278
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200279 /* The last parameter is expected to be the imagefile */
280 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200281 params.imagefile = argv[optind];
282
Simon Glassd505a092016-02-22 22:55:48 -0700283 /*
284 * For auto-generated FIT images we need to know the image type to put
285 * in the FIT, which is separate from the file's image type (which
286 * will always be IH_TYPE_FLATDT in this case).
287 */
288 if (params.type == IH_TYPE_FLATDT) {
Simon Glass20deadd2016-06-30 10:52:07 -0600289 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600290 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700291 if (!params.auto_its)
292 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600293 else if (!params.datafile)
294 usage("Missing data file for auto-FIT (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700295 } else if (type != IH_TYPE_INVALID) {
296 params.type = type;
297 }
298
299 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700300 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700301}
302
Simon Glass0b443de2016-02-22 22:55:33 -0700303int main(int argc, char **argv)
304{
305 int ifd = -1;
306 struct stat sbuf;
307 char *ptr;
308 int retval = 0;
309 struct image_type_params *tparams = NULL;
310 int pad_len = 0;
311 int dfd;
312
313 params.cmdname = *argv;
314 params.addr = 0;
315 params.ep = 0;
316
317 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000318
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530319 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200320 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530321 if (tparams == NULL) {
322 fprintf (stderr, "%s: unsupported type %s\n",
323 params.cmdname, genimg_get_type_name(params.type));
324 exit (EXIT_FAILURE);
325 }
326
327 /*
328 * check the passed arguments parameters meets the requirements
329 * as per image type to be generated/listed
330 */
331 if (tparams->check_params)
332 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700333 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530334
335 if (!params.eflag) {
336 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000337 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530338 if (params.xflag)
339 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000340 }
341
Peter Tyserc81296c2009-11-24 16:42:10 -0600342 if (params.fflag){
343 if (tparams->fflag_handle)
344 /*
345 * in some cases, some additional processing needs
346 * to be done if fflag is defined
347 *
348 * For ex. fit_handle_file for Fit file support
349 */
350 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000351
Peter Tyserc81296c2009-11-24 16:42:10 -0600352 if (retval != EXIT_SUCCESS)
353 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000354 }
355
Peter Tyserc81296c2009-11-24 16:42:10 -0600356 if (params.lflag || params.fflag) {
357 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
358 } else {
359 ifd = open (params.imagefile,
360 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
361 }
362
363 if (ifd < 0) {
364 fprintf (stderr, "%s: Can't open %s: %s\n",
365 params.cmdname, params.imagefile,
366 strerror(errno));
367 exit (EXIT_FAILURE);
368 }
369
370 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000371 /*
372 * list header information of existing image
373 */
374 if (fstat(ifd, &sbuf) < 0) {
375 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530376 params.cmdname, params.imagefile,
377 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000378 exit (EXIT_FAILURE);
379 }
380
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530381 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000382 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530383 "%s: Bad size: \"%s\" is not valid image\n",
384 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000385 exit (EXIT_FAILURE);
386 }
387
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400388 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
389 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000390 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530391 params.cmdname, params.imagefile,
392 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000393 exit (EXIT_FAILURE);
394 }
395
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530396 /*
397 * scan through mkimage registry for all supported image types
398 * and verify the input image file header for match
399 * Print the image information for matched image type
400 * Returns the error code if not matched
401 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200402 retval = imagetool_verify_print_header(ptr, &sbuf,
403 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000404
wdenk5b1d7132002-11-03 00:07:02 +0000405 (void) munmap((void *)ptr, sbuf.st_size);
406 (void) close (ifd);
407
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530408 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000409 }
410
Marek Vasut34633142015-12-07 18:01:54 +0100411 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200412 dfd = open(params.datafile, O_RDONLY | O_BINARY);
413 if (dfd < 0) {
414 fprintf(stderr, "%s: Can't open %s: %s\n",
415 params.cmdname, params.datafile,
416 strerror(errno));
417 exit(EXIT_FAILURE);
418 }
Simon Glass92a655c2015-06-23 15:39:12 -0600419
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200420 if (fstat(dfd, &sbuf) < 0) {
421 fprintf(stderr, "%s: Can't stat %s: %s\n",
422 params.cmdname, params.datafile,
423 strerror(errno));
424 exit(EXIT_FAILURE);
425 }
Simon Glass92a655c2015-06-23 15:39:12 -0600426
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200427 params.file_size = sbuf.st_size + tparams->header_size;
428 close(dfd);
429 }
Simon Glass92a655c2015-06-23 15:39:12 -0600430
wdenk5b1d7132002-11-03 00:07:02 +0000431 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000432 * In case there an header with a variable
433 * length will be added, the corresponding
434 * function is called. This is responsible to
435 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000436 */
Stefano Babicf0662102011-09-15 23:50:16 +0000437 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200438 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000439 else
440 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000441
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530442 if (write(ifd, tparams->hdr, tparams->header_size)
443 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000444 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530445 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000446 exit (EXIT_FAILURE);
447 }
448
Christian Rieschd1be8f92011-12-09 09:47:38 +0000449 if (!params.skipcpy) {
450 if (params.type == IH_TYPE_MULTI ||
451 params.type == IH_TYPE_SCRIPT) {
452 char *file = params.datafile;
453 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000454
Christian Rieschd1be8f92011-12-09 09:47:38 +0000455 for (;;) {
456 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000457
Christian Rieschd1be8f92011-12-09 09:47:38 +0000458 if (file) {
459 if ((sep = strchr(file, ':')) != NULL) {
460 *sep = '\0';
461 }
462
463 if (stat (file, &sbuf) < 0) {
464 fprintf (stderr, "%s: Can't stat %s: %s\n",
465 params.cmdname, file, strerror(errno));
466 exit (EXIT_FAILURE);
467 }
468 size = cpu_to_uimage (sbuf.st_size);
469 } else {
470 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000471 }
472
Christian Rieschd1be8f92011-12-09 09:47:38 +0000473 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
474 fprintf (stderr, "%s: Write error on %s: %s\n",
475 params.cmdname, params.imagefile,
476 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000477 exit (EXIT_FAILURE);
478 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000479
480 if (!file) {
481 break;
482 }
483
484 if (sep) {
485 *sep = ':';
486 file = sep + 1;
487 } else {
488 file = NULL;
489 }
wdenk5b1d7132002-11-03 00:07:02 +0000490 }
491
Christian Rieschd1be8f92011-12-09 09:47:38 +0000492 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000493
Christian Rieschd1be8f92011-12-09 09:47:38 +0000494 for (;;) {
495 char *sep = strchr(file, ':');
496 if (sep) {
497 *sep = '\0';
498 copy_file (ifd, file, 1);
499 *sep++ = ':';
500 file = sep;
501 } else {
502 copy_file (ifd, file, 0);
503 break;
504 }
wdenk5b1d7132002-11-03 00:07:02 +0000505 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000506 } else if (params.type == IH_TYPE_PBLIMAGE) {
507 /* PBL has special Image format, implements its' own */
508 pbl_load_uboot(ifd, &params);
Christian Rieschd1be8f92011-12-09 09:47:38 +0000509 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200510 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000511 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100512 if (params.type == IH_TYPE_FIRMWARE_IVT) {
513 /* Add alignment and IVT */
514 uint32_t aligned_filesize = (params.file_size + 0x1000
515 - 1) & ~(0x1000 - 1);
516 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
517 params.addr, 0, 0, 0, params.addr
518 + aligned_filesize
519 - tparams->header_size,
520 params.addr + aligned_filesize
521 - tparams->header_size
522 + 0x20, 0 };
523 int i = params.file_size;
524 for (; i < aligned_filesize; i++) {
525 if (write(ifd, &i, 1) != 1) {
526 fprintf(stderr,
527 "%s: Write error on %s: %s\n",
528 params.cmdname,
529 params.imagefile,
530 strerror(errno));
531 exit(EXIT_FAILURE);
532 }
533 }
534 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
535 != sizeof(flash_header_v2_t)) {
536 fprintf(stderr, "%s: Write error on %s: %s\n",
537 params.cmdname,
538 params.imagefile,
539 strerror(errno));
540 exit(EXIT_FAILURE);
541 }
542 }
wdenk5b1d7132002-11-03 00:07:02 +0000543 }
544
545 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530546#if defined(_POSIX_SYNCHRONIZED_IO) && \
547 !defined(__sun__) && \
548 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200549 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530550 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000551 (void) fdatasync (ifd);
552#else
553 (void) fsync (ifd);
554#endif
555
556 if (fstat(ifd, &sbuf) < 0) {
557 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530558 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000559 exit (EXIT_FAILURE);
560 }
Simon Glass92a655c2015-06-23 15:39:12 -0600561 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000562
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400563 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
564 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000565 fprintf (stderr, "%s: Can't map %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
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530570 /* Setup the image header as per input image type*/
571 if (tparams->set_header)
572 tparams->set_header (ptr, &sbuf, ifd, &params);
573 else {
574 fprintf (stderr, "%s: Can't set header for %s: %s\n",
575 params.cmdname, tparams->name, strerror(errno));
576 exit (EXIT_FAILURE);
577 }
wdenk5b1d7132002-11-03 00:07:02 +0000578
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530579 /* Print the image information by processing image header */
580 if (tparams->print_header)
581 tparams->print_header (ptr);
582 else {
583 fprintf (stderr, "%s: Can't print header for %s: %s\n",
584 params.cmdname, tparams->name, strerror(errno));
585 exit (EXIT_FAILURE);
586 }
wdenk5b1d7132002-11-03 00:07:02 +0000587
588 (void) munmap((void *)ptr, sbuf.st_size);
589
590 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530591#if defined(_POSIX_SYNCHRONIZED_IO) && \
592 !defined(__sun__) && \
593 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200594 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530595 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000596 (void) fdatasync (ifd);
597#else
598 (void) fsync (ifd);
599#endif
600
601 if (close(ifd)) {
602 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530603 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000604 exit (EXIT_FAILURE);
605 }
606
607 exit (EXIT_SUCCESS);
608}
609
610static void
611copy_file (int ifd, const char *datafile, int pad)
612{
613 int dfd;
614 struct stat sbuf;
615 unsigned char *ptr;
616 int tail;
617 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200618 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000619 int offset = 0;
620 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200621 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000622
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200623 memset(zeros, 0, sizeof(zeros));
624
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530625 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000626 fprintf (stderr, "Adding Image %s\n", datafile);
627 }
628
wdenkef1464c2003-10-08 22:14:02 +0000629 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000630 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530631 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000632 exit (EXIT_FAILURE);
633 }
634
635 if (fstat(dfd, &sbuf) < 0) {
636 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530637 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000638 exit (EXIT_FAILURE);
639 }
640
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400641 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
642 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000643 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530644 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000645 exit (EXIT_FAILURE);
646 }
647
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530648 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000649 unsigned char *p = NULL;
650 /*
651 * XIP: do not append the image_header_t at the
652 * beginning of the file, but consume the space
653 * reserved for it.
654 */
655
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530656 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000657 fprintf (stderr,
658 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530659 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000660 exit (EXIT_FAILURE);
661 }
662
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530663 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000664 if ( *p != 0xff ) {
665 fprintf (stderr,
666 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530667 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000668 exit (EXIT_FAILURE);
669 }
670 }
671
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530672 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000673 }
674
675 size = sbuf.st_size - offset;
676 if (write(ifd, ptr + offset, size) != size) {
677 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530678 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000679 exit (EXIT_FAILURE);
680 }
681
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200682 tail = size % 4;
683 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000684
685 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
686 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530687 params.cmdname, params.imagefile,
688 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000689 exit (EXIT_FAILURE);
690 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200691 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600692 while (pad > 0) {
693 int todo = sizeof(zeros);
694
695 if (todo > pad)
696 todo = pad;
697 if (write(ifd, (char *)&zeros, todo) != todo) {
698 fprintf(stderr, "%s: Write error on %s: %s\n",
699 params.cmdname, params.imagefile,
700 strerror(errno));
701 exit(EXIT_FAILURE);
702 }
703 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200704 }
wdenk5b1d7132002-11-03 00:07:02 +0000705 }
706
707 (void) munmap((void *)ptr, sbuf.st_size);
708 (void) close (dfd);
709}