blob: 32e07be94af3b0e809e5807ef12d3ce8e01a48b0 [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +01002 * (C) Copyright 2008 Semihalf
3 *
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +05304 * (C) Copyright 2000-2009
wdenk5b1d7132002-11-03 00:07:02 +00005 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
wdenkdb82c8e2004-02-26 23:01:04 +00007 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00009 */
10
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010011#include "mkimage.h"
Sven Ebenfeld59697a22016-11-06 16:37:56 +010012#include "imximage.h"
wdenk5b1d7132002-11-03 00:07:02 +000013#include <image.h>
Wolfgang Denke97ab722011-02-12 10:37:11 +010014#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000015
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053016static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000017
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053018/* parameters initialized by core will be used by the image type code */
Simon Glass52322d62016-02-22 22:55:38 -070019static struct image_tool_params params = {
Prafulla Wadaskarfabf3cf2009-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 Denk23416b82010-03-27 23:37:46 +010025 .imagename = "",
Shaohui Xieea65fd82012-08-10 02:49:35 +000026 .imagename2 = "",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053027};
wdenk5b1d7132002-11-03 00:07:02 +000028
Simon Glass1d138b52016-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 Glassbe421592016-06-30 10:52:18 -060041static int show_valid_options(enum ih_category category)
Simon Glass1d138b52016-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 Glassd435d9f2016-10-27 17:54:03 -060068 free(order);
Simon Glass1d138b52016-06-30 10:52:17 -060069
70 return 0;
71}
72
Simon Glass01168b32016-02-22 22:55:37 -070073static void usage(const char *msg)
Simon Glass99a7c262016-02-22 22:55:36 -070074{
Simon Glass01168b32016-02-22 22:55:37 -070075 fprintf(stderr, "Error: %s\n", msg);
Simon Glass99a7c262016-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 Vizoso8d83ed22016-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 Cascadian3e9e6d62016-10-23 20:45:17 -070093 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glass99a7c262016-02-22 22:55:36 -070094 params.cmdname);
95 fprintf(stderr,
96 " -D => set all options for device tree compiler\n"
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +010097 " -f => input filename for FIT source\n"
98 " -i => input filename for ramdisk file\n");
Simon Glass99a7c262016-02-22 22:55:36 -070099#ifdef CONFIG_FIT_SIGNATURE
100 fprintf(stderr,
George McCollister23d14892017-01-06 13:14:17 -0600101 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Teddy Reeda8457622016-06-09 19:38:02 -0700102 " -E => place data outside of the FIT structure\n"
Simon Glass99a7c262016-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 Reeda8457622016-06-09 19:38:02 -0700107 " -p => place external data at a static position\n"
George McCollister23d14892017-01-06 13:14:17 -0600108 " -r => mark keys used as 'required' in dtb\n"
109 " -N => engine to use for signing (pkcs11)\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700110#else
111 fprintf(stderr,
112 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
113#endif
114 fprintf(stderr, " %s -V ==> print version information and exit\n",
115 params.cmdname);
Baruch Siachf77ab202017-06-29 20:37:08 +0300116 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700117
118 exit(EXIT_FAILURE);
119}
120
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700121static int add_content(int type, const char *fname)
122{
123 struct content_info *cont;
124
125 cont = calloc(1, sizeof(*cont));
126 if (!cont)
127 return -1;
128 cont->type = type;
129 cont->fname = fname;
130 if (params.content_tail)
131 params.content_tail->next = cont;
132 else
133 params.content_head = cont;
134 params.content_tail = cont;
135
136 return 0;
137}
138
Simon Glass7106bd42016-02-22 22:55:33 -0700139static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000140{
Peter Tyser632d9ca2010-04-04 22:36:03 -0500141 char *ptr;
Simon Glass04b46072016-02-22 22:55:48 -0700142 int type = IH_TYPE_INVALID;
143 char *datafile = NULL;
Simon Glasscf76cd22016-02-22 22:55:34 -0700144 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000145
Simon Glasscf76cd22016-02-22 22:55:34 -0700146 while ((opt = getopt(argc, argv,
George McCollister23d14892017-01-06 13:14:17 -0600147 "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
Simon Glasscf76cd22016-02-22 22:55:34 -0700148 switch (opt) {
Simon Glassf6fe5f62016-02-22 22:55:35 -0700149 case 'a':
150 params.addr = strtoull(optarg, &ptr, 16);
151 if (*ptr) {
152 fprintf(stderr, "%s: invalid load address %s\n",
153 params.cmdname, optarg);
154 exit(EXIT_FAILURE);
155 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700156 break;
157 case 'A':
158 params.arch = genimg_get_arch_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600159 if (params.arch < 0) {
160 show_valid_options(IH_ARCH);
Simon Glass01168b32016-02-22 22:55:37 -0700161 usage("Invalid architecture");
Simon Glasseaa25402016-06-30 10:52:19 -0600162 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700163 break;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700164 case 'b':
Andreas Bießmannddd34772016-05-03 15:17:03 +0200165 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmannf4001582016-05-01 03:01:27 +0200166 fprintf(stderr,
167 "%s: Out of memory adding content '%s'",
168 params.cmdname, optarg);
169 exit(EXIT_FAILURE);
170 }
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700171 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700172 case 'c':
173 params.comment = optarg;
174 break;
175 case 'C':
176 params.comp = genimg_get_comp_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600177 if (params.comp < 0) {
178 show_valid_options(IH_COMP);
Simon Glass01168b32016-02-22 22:55:37 -0700179 usage("Invalid compression type");
Simon Glasseaa25402016-06-30 10:52:19 -0600180 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700181 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700182 case 'd':
183 params.datafile = optarg;
184 params.dflag = 1;
185 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700186 case 'D':
187 params.dtc = optarg;
188 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700189 case 'e':
190 params.ep = strtoull(optarg, &ptr, 16);
191 if (*ptr) {
192 fprintf(stderr, "%s: invalid entry point %s\n",
193 params.cmdname, optarg);
194 exit(EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000195 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700196 params.eflag = 1;
197 break;
Simon Glassafd728c2016-02-22 22:55:53 -0700198 case 'E':
199 params.external_data = true;
200 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700201 case 'f':
Simon Glass88e31cb2016-02-22 22:55:51 -0700202 datafile = optarg;
203 params.auto_its = !strcmp(datafile, "auto");
Simon Glasscf76cd22016-02-22 22:55:34 -0700204 /* no break */
205 case 'F':
206 /*
207 * The flattened image tree (FIT) format
208 * requires a flattened device tree image type
209 */
210 params.type = IH_TYPE_FLATDT;
211 params.fflag = 1;
212 break;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100213 case 'i':
214 params.fit_ramdisk = optarg;
215 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700216 case 'k':
217 params.keydir = optarg;
218 break;
219 case 'K':
220 params.keydest = optarg;
221 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700222 case 'l':
223 params.lflag = 1;
224 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700225 case 'n':
226 params.imagename = optarg;
227 break;
George McCollister23d14892017-01-06 13:14:17 -0600228 case 'N':
229 params.engine_id = optarg;
230 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700231 case 'O':
232 params.os = genimg_get_os_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600233 if (params.os < 0) {
234 show_valid_options(IH_OS);
Simon Glass01168b32016-02-22 22:55:37 -0700235 usage("Invalid operating system");
Simon Glasseaa25402016-06-30 10:52:19 -0600236 }
Simon Glassf6fe5f62016-02-22 22:55:35 -0700237 break;
Teddy Reeda8457622016-06-09 19:38:02 -0700238 case 'p':
239 params.external_offset = strtoull(optarg, &ptr, 16);
240 if (*ptr) {
241 fprintf(stderr, "%s: invalid offset size %s\n",
242 params.cmdname, optarg);
243 exit(EXIT_FAILURE);
244 }
Teddy Reed9e3cd3c2016-07-11 22:54:26 -0700245 break;
Simon Glassda1805f2016-05-01 13:55:38 -0600246 case 'q':
247 params.quiet = 1;
248 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700249 case 'r':
250 params.require_keys = 1;
251 break;
252 case 'R':
253 /*
254 * This entry is for the second configuration
255 * file, if only one is not enough.
256 */
257 params.imagename2 = optarg;
258 break;
259 case 's':
260 params.skipcpy = 1;
261 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700262 case 'T':
Baruch Siachf77ab202017-06-29 20:37:08 +0300263 if (strcmp(optarg, "list") == 0) {
264 show_valid_options(IH_TYPE);
265 exit(EXIT_SUCCESS);
266 }
Simon Glass04b46072016-02-22 22:55:48 -0700267 type = genimg_get_type_id(optarg);
268 if (type < 0) {
Simon Glassbe421592016-06-30 10:52:18 -0600269 show_valid_options(IH_TYPE);
Simon Glass01168b32016-02-22 22:55:37 -0700270 usage("Invalid image type");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700271 }
272 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700273 case 'v':
274 params.vflag++;
275 break;
276 case 'V':
277 printf("mkimage version %s\n", PLAIN_VERSION);
278 exit(EXIT_SUCCESS);
279 case 'x':
280 params.xflag++;
281 break;
282 default:
Simon Glass01168b32016-02-22 22:55:37 -0700283 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000284 }
wdenk5b1d7132002-11-03 00:07:02 +0000285 }
286
Andreas Bießmannddd34772016-05-03 15:17:03 +0200287 /* The last parameter is expected to be the imagefile */
288 if (optind < argc)
Andreas Bießmannf4001582016-05-01 03:01:27 +0200289 params.imagefile = argv[optind];
290
Simon Glass04b46072016-02-22 22:55:48 -0700291 /*
292 * For auto-generated FIT images we need to know the image type to put
293 * in the FIT, which is separate from the file's image type (which
294 * will always be IH_TYPE_FLATDT in this case).
295 */
296 if (params.type == IH_TYPE_FLATDT) {
Simon Glassa89857d2016-06-30 10:52:07 -0600297 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass156c3492016-06-30 10:52:08 -0600298 /* For auto_its, datafile is always 'auto' */
Simon Glass88e31cb2016-02-22 22:55:51 -0700299 if (!params.auto_its)
300 params.datafile = datafile;
Simon Glass79b5ee52016-06-30 10:52:09 -0600301 else if (!params.datafile)
302 usage("Missing data file for auto-FIT (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700303 } else if (type != IH_TYPE_INVALID) {
Alex Kiernan9d74c702018-04-22 05:11:17 +0000304 if (type == IH_TYPE_SCRIPT && !params.datafile)
305 usage("Missing data file for script (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700306 params.type = type;
307 }
308
309 if (!params.imagefile)
Simon Glass01168b32016-02-22 22:55:37 -0700310 usage("Missing output filename");
Simon Glass7106bd42016-02-22 22:55:33 -0700311}
312
Simon Glass7106bd42016-02-22 22:55:33 -0700313int main(int argc, char **argv)
314{
315 int ifd = -1;
316 struct stat sbuf;
317 char *ptr;
318 int retval = 0;
319 struct image_type_params *tparams = NULL;
320 int pad_len = 0;
321 int dfd;
322
323 params.cmdname = *argv;
324 params.addr = 0;
325 params.ep = 0;
326
327 process_args(argc, argv);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530328
329 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200330 tparams = imagetool_get_type(params.type);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530331 if (tparams == NULL) {
332 fprintf (stderr, "%s: unsupported type %s\n",
333 params.cmdname, genimg_get_type_name(params.type));
334 exit (EXIT_FAILURE);
335 }
336
337 /*
338 * check the passed arguments parameters meets the requirements
339 * as per image type to be generated/listed
340 */
341 if (tparams->check_params)
342 if (tparams->check_params (&params))
Simon Glass01168b32016-02-22 22:55:37 -0700343 usage("Bad parameters for image type");
wdenk5b1d7132002-11-03 00:07:02 +0000344
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530345 if (!params.eflag) {
346 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000347 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530348 if (params.xflag)
349 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000350 }
351
Peter Tyserb2f8b942009-11-24 16:42:10 -0600352 if (params.fflag){
353 if (tparams->fflag_handle)
354 /*
355 * in some cases, some additional processing needs
356 * to be done if fflag is defined
357 *
358 * For ex. fit_handle_file for Fit file support
359 */
360 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000361
Peter Tyserb2f8b942009-11-24 16:42:10 -0600362 if (retval != EXIT_SUCCESS)
363 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000364 }
365
Peter Tyserb2f8b942009-11-24 16:42:10 -0600366 if (params.lflag || params.fflag) {
367 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
368 } else {
369 ifd = open (params.imagefile,
370 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
371 }
372
373 if (ifd < 0) {
374 fprintf (stderr, "%s: Can't open %s: %s\n",
375 params.cmdname, params.imagefile,
376 strerror(errno));
377 exit (EXIT_FAILURE);
378 }
379
380 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000381 /*
382 * list header information of existing image
383 */
384 if (fstat(ifd, &sbuf) < 0) {
385 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530386 params.cmdname, params.imagefile,
387 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000388 exit (EXIT_FAILURE);
389 }
390
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530391 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000392 fprintf (stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530393 "%s: Bad size: \"%s\" is not valid image\n",
394 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000395 exit (EXIT_FAILURE);
396 }
397
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400398 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
399 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000400 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530401 params.cmdname, params.imagefile,
402 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000403 exit (EXIT_FAILURE);
404 }
405
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530406 /*
407 * scan through mkimage registry for all supported image types
408 * and verify the input image file header for match
409 * Print the image information for matched image type
410 * Returns the error code if not matched
411 */
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -0200412 retval = imagetool_verify_print_header(ptr, &sbuf,
413 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000414
wdenk5b1d7132002-11-03 00:07:02 +0000415 (void) munmap((void *)ptr, sbuf.st_size);
416 (void) close (ifd);
417
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530418 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000419 }
420
Marek Vasut4cc86272015-12-07 18:01:54 +0100421 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert3edd0412015-12-04 00:11:23 +0200422 dfd = open(params.datafile, O_RDONLY | O_BINARY);
423 if (dfd < 0) {
424 fprintf(stderr, "%s: Can't open %s: %s\n",
425 params.cmdname, params.datafile,
426 strerror(errno));
427 exit(EXIT_FAILURE);
428 }
Simon Glass139651f2015-06-23 15:39:12 -0600429
Philippe De Swert3edd0412015-12-04 00:11:23 +0200430 if (fstat(dfd, &sbuf) < 0) {
431 fprintf(stderr, "%s: Can't stat %s: %s\n",
432 params.cmdname, params.datafile,
433 strerror(errno));
434 exit(EXIT_FAILURE);
435 }
Simon Glass139651f2015-06-23 15:39:12 -0600436
Philippe De Swert3edd0412015-12-04 00:11:23 +0200437 params.file_size = sbuf.st_size + tparams->header_size;
438 close(dfd);
439 }
Simon Glass139651f2015-06-23 15:39:12 -0600440
wdenk5b1d7132002-11-03 00:07:02 +0000441 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000442 * In case there an header with a variable
443 * length will be added, the corresponding
444 * function is called. This is responsible to
445 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000446 */
Stefano Babic4a963302011-09-15 23:50:16 +0000447 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200448 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000449 else
450 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000451
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530452 if (write(ifd, tparams->hdr, tparams->header_size)
453 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000454 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530455 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000456 exit (EXIT_FAILURE);
457 }
458
Christian Rieschc14f2812011-12-09 09:47:38 +0000459 if (!params.skipcpy) {
460 if (params.type == IH_TYPE_MULTI ||
461 params.type == IH_TYPE_SCRIPT) {
462 char *file = params.datafile;
463 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000464
Christian Rieschc14f2812011-12-09 09:47:38 +0000465 for (;;) {
466 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000467
Christian Rieschc14f2812011-12-09 09:47:38 +0000468 if (file) {
469 if ((sep = strchr(file, ':')) != NULL) {
470 *sep = '\0';
471 }
472
473 if (stat (file, &sbuf) < 0) {
474 fprintf (stderr, "%s: Can't stat %s: %s\n",
475 params.cmdname, file, strerror(errno));
476 exit (EXIT_FAILURE);
477 }
478 size = cpu_to_uimage (sbuf.st_size);
479 } else {
480 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000481 }
482
Christian Rieschc14f2812011-12-09 09:47:38 +0000483 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
484 fprintf (stderr, "%s: Write error on %s: %s\n",
485 params.cmdname, params.imagefile,
486 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000487 exit (EXIT_FAILURE);
488 }
wdenk5b1d7132002-11-03 00:07:02 +0000489
Christian Rieschc14f2812011-12-09 09:47:38 +0000490 if (!file) {
491 break;
492 }
wdenk5b1d7132002-11-03 00:07:02 +0000493
Christian Rieschc14f2812011-12-09 09:47:38 +0000494 if (sep) {
495 *sep = ':';
496 file = sep + 1;
497 } else {
498 file = NULL;
499 }
wdenk5b1d7132002-11-03 00:07:02 +0000500 }
wdenk5b1d7132002-11-03 00:07:02 +0000501
Christian Rieschc14f2812011-12-09 09:47:38 +0000502 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000503
Christian Rieschc14f2812011-12-09 09:47:38 +0000504 for (;;) {
505 char *sep = strchr(file, ':');
506 if (sep) {
507 *sep = '\0';
508 copy_file (ifd, file, 1);
509 *sep++ = ':';
510 file = sep;
511 } else {
512 copy_file (ifd, file, 0);
513 break;
514 }
wdenk5b1d7132002-11-03 00:07:02 +0000515 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000516 } else if (params.type == IH_TYPE_PBLIMAGE) {
517 /* PBL has special Image format, implements its' own */
518 pbl_load_uboot(ifd, &params);
Christian Rieschc14f2812011-12-09 09:47:38 +0000519 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200520 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000521 }
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100522 if (params.type == IH_TYPE_FIRMWARE_IVT) {
523 /* Add alignment and IVT */
524 uint32_t aligned_filesize = (params.file_size + 0x1000
525 - 1) & ~(0x1000 - 1);
526 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
527 params.addr, 0, 0, 0, params.addr
528 + aligned_filesize
529 - tparams->header_size,
530 params.addr + aligned_filesize
531 - tparams->header_size
532 + 0x20, 0 };
533 int i = params.file_size;
534 for (; i < aligned_filesize; i++) {
Sven Ebenfeld3bb71e02017-01-17 19:36:51 +0100535 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100536 fprintf(stderr,
537 "%s: Write error on %s: %s\n",
538 params.cmdname,
539 params.imagefile,
540 strerror(errno));
541 exit(EXIT_FAILURE);
542 }
543 }
544 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
545 != sizeof(flash_header_v2_t)) {
546 fprintf(stderr, "%s: Write error on %s: %s\n",
547 params.cmdname,
548 params.imagefile,
549 strerror(errno));
550 exit(EXIT_FAILURE);
551 }
552 }
wdenk5b1d7132002-11-03 00:07:02 +0000553 }
554
555 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530556#if defined(_POSIX_SYNCHRONIZED_IO) && \
557 !defined(__sun__) && \
558 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200559 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530560 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000561 (void) fdatasync (ifd);
562#else
563 (void) fsync (ifd);
564#endif
565
566 if (fstat(ifd, &sbuf) < 0) {
567 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530568 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000569 exit (EXIT_FAILURE);
570 }
Simon Glass139651f2015-06-23 15:39:12 -0600571 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000572
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400573 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
574 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000575 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530576 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000577 exit (EXIT_FAILURE);
578 }
579
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530580 /* Setup the image header as per input image type*/
581 if (tparams->set_header)
582 tparams->set_header (ptr, &sbuf, ifd, &params);
583 else {
584 fprintf (stderr, "%s: Can't set header for %s: %s\n",
585 params.cmdname, tparams->name, strerror(errno));
586 exit (EXIT_FAILURE);
587 }
wdenk5b1d7132002-11-03 00:07:02 +0000588
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530589 /* Print the image information by processing image header */
590 if (tparams->print_header)
591 tparams->print_header (ptr);
592 else {
Guillaume GARDETdfe25fa2018-03-30 10:28:19 +0200593 fprintf (stderr, "%s: Can't print header for %s\n",
594 params.cmdname, tparams->name);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530595 }
wdenk5b1d7132002-11-03 00:07:02 +0000596
597 (void) munmap((void *)ptr, sbuf.st_size);
598
599 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530600#if defined(_POSIX_SYNCHRONIZED_IO) && \
601 !defined(__sun__) && \
602 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200603 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530604 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000605 (void) fdatasync (ifd);
606#else
607 (void) fsync (ifd);
608#endif
609
610 if (close(ifd)) {
611 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530612 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000613 exit (EXIT_FAILURE);
614 }
615
616 exit (EXIT_SUCCESS);
617}
618
619static void
620copy_file (int ifd, const char *datafile, int pad)
621{
622 int dfd;
623 struct stat sbuf;
624 unsigned char *ptr;
625 int tail;
626 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200627 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000628 int offset = 0;
629 int size;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200630 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000631
Stefano Babic6531bd92013-08-19 19:03:19 +0200632 memset(zeros, 0, sizeof(zeros));
633
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530634 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000635 fprintf (stderr, "Adding Image %s\n", datafile);
636 }
637
wdenk1ee550e2003-10-08 22:14:02 +0000638 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000639 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530640 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000641 exit (EXIT_FAILURE);
642 }
643
644 if (fstat(dfd, &sbuf) < 0) {
645 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530646 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000647 exit (EXIT_FAILURE);
648 }
649
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400650 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
651 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000652 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530653 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000654 exit (EXIT_FAILURE);
655 }
656
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530657 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000658 unsigned char *p = NULL;
659 /*
660 * XIP: do not append the image_header_t at the
661 * beginning of the file, but consume the space
662 * reserved for it.
663 */
664
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530665 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000666 fprintf (stderr,
667 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530668 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000669 exit (EXIT_FAILURE);
670 }
671
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530672 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000673 if ( *p != 0xff ) {
674 fprintf (stderr,
675 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530676 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000677 exit (EXIT_FAILURE);
678 }
679 }
680
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530681 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000682 }
683
684 size = sbuf.st_size - offset;
685 if (write(ifd, ptr + offset, size) != size) {
686 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530687 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000688 exit (EXIT_FAILURE);
689 }
690
Stefano Babic6531bd92013-08-19 19:03:19 +0200691 tail = size % 4;
692 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000693
694 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
695 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530696 params.cmdname, params.imagefile,
697 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000698 exit (EXIT_FAILURE);
699 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200700 } else if (pad > 1) {
Simon Glass46409cc2015-08-30 16:55:22 -0600701 while (pad > 0) {
702 int todo = sizeof(zeros);
703
704 if (todo > pad)
705 todo = pad;
706 if (write(ifd, (char *)&zeros, todo) != todo) {
707 fprintf(stderr, "%s: Write error on %s: %s\n",
708 params.cmdname, params.imagefile,
709 strerror(errno));
710 exit(EXIT_FAILURE);
711 }
712 pad -= todo;
Stefano Babic6531bd92013-08-19 19:03:19 +0200713 }
wdenk5b1d7132002-11-03 00:07:02 +0000714 }
715
716 (void) munmap((void *)ptr, sbuf.st_size);
717 (void) close (dfd);
718}