blob: 0279f6867e91893954315f98245e3e8e3edf4831 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00002/*
Bartlomiej Sieka9d254382008-03-11 12:34:47 +01003 * (C) Copyright 2008 Semihalf
4 *
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +05305 * (C) Copyright 2000-2009
wdenk5b1d7132002-11-03 00:07:02 +00006 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
wdenk5b1d7132002-11-03 00:07:02 +00008 */
9
Kever Yang29e7ab02020-03-30 11:56:19 +080010#include "imagetool.h"
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,
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600101 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700102 " -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"
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600108 " -r => mark keys used as 'required' in dtb\n"
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300109 " -N => openssl engine to use for signing\n");
Simon Glassb0a487a2016-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 Siach79aa33c2017-06-29 20:37:08 +0300116 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700117
118 exit(EXIT_FAILURE);
119}
120
Simon Glassfb4cce02016-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 Glass0b443de2016-02-22 22:55:33 -0700139static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000140{
Peter Tysera2513e22010-04-04 22:36:03 -0500141 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700142 int type = IH_TYPE_INVALID;
143 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700144 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000145
Simon Glassa02221f2016-02-22 22:55:34 -0700146 while ((opt = getopt(argc, argv,
George McCollisterf1ca1fd2017-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 Glassa02221f2016-02-22 22:55:34 -0700148 switch (opt) {
Simon Glass07450082016-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 Glassa02221f2016-02-22 22:55:34 -0700156 break;
157 case 'A':
158 params.arch = genimg_get_arch_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600159 if (params.arch < 0) {
160 show_valid_options(IH_ARCH);
Simon Glass15310342016-02-22 22:55:37 -0700161 usage("Invalid architecture");
Simon Glass51f03e62016-06-30 10:52:19 -0600162 }
Simon Glassa02221f2016-02-22 22:55:34 -0700163 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700164 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200165 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-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 Glassfb4cce02016-02-22 22:55:52 -0700171 break;
Simon Glassa02221f2016-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 Glass51f03e62016-06-30 10:52:19 -0600177 if (params.comp < 0) {
178 show_valid_options(IH_COMP);
Simon Glass15310342016-02-22 22:55:37 -0700179 usage("Invalid compression type");
Simon Glass51f03e62016-06-30 10:52:19 -0600180 }
Simon Glassa02221f2016-02-22 22:55:34 -0700181 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700182 case 'd':
183 params.datafile = optarg;
184 params.dflag = 1;
185 break;
Simon Glass07450082016-02-22 22:55:35 -0700186 case 'D':
187 params.dtc = optarg;
188 break;
Simon Glassa02221f2016-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);
195 }
196 params.eflag = 1;
197 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700198 case 'E':
199 params.external_data = true;
200 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700201 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700202 datafile = optarg;
203 params.auto_its = !strcmp(datafile, "auto");
Simon Glassa02221f2016-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 Vizoso0f7c6cd2016-11-04 14:22:15 +0100213 case 'i':
214 params.fit_ramdisk = optarg;
215 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700216 case 'k':
217 params.keydir = optarg;
218 break;
219 case 'K':
220 params.keydest = optarg;
221 break;
Simon Glass07450082016-02-22 22:55:35 -0700222 case 'l':
223 params.lflag = 1;
224 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700225 case 'n':
226 params.imagename = optarg;
227 break;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600228 case 'N':
229 params.engine_id = optarg;
230 break;
Simon Glass07450082016-02-22 22:55:35 -0700231 case 'O':
232 params.os = genimg_get_os_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600233 if (params.os < 0) {
234 show_valid_options(IH_OS);
Simon Glass15310342016-02-22 22:55:37 -0700235 usage("Invalid operating system");
Simon Glass51f03e62016-06-30 10:52:19 -0600236 }
Simon Glass07450082016-02-22 22:55:35 -0700237 break;
Teddy Reedf8f91072016-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 Reedb6fefa72016-07-11 22:54:26 -0700245 break;
Simon Glassbd6e1422016-05-01 13:55:38 -0600246 case 'q':
247 params.quiet = 1;
248 break;
Simon Glassa02221f2016-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 Glass07450082016-02-22 22:55:35 -0700262 case 'T':
Baruch Siach79aa33c2017-06-29 20:37:08 +0300263 if (strcmp(optarg, "list") == 0) {
264 show_valid_options(IH_TYPE);
265 exit(EXIT_SUCCESS);
266 }
Simon Glassd505a092016-02-22 22:55:48 -0700267 type = genimg_get_type_id(optarg);
268 if (type < 0) {
Simon Glassf24e1052016-06-30 10:52:18 -0600269 show_valid_options(IH_TYPE);
Simon Glass15310342016-02-22 22:55:37 -0700270 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700271 }
272 break;
Simon Glassa02221f2016-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 Glass15310342016-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ßmann8edeac82016-05-03 15:17:03 +0200287 /* The last parameter is expected to be the imagefile */
288 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200289 params.imagefile = argv[optind];
290
Simon Glassd505a092016-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 Glass20deadd2016-06-30 10:52:07 -0600297 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600298 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700299 if (!params.auto_its)
300 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600301 else if (!params.datafile)
302 usage("Missing data file for auto-FIT (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700303 } else if (type != IH_TYPE_INVALID) {
Alex Kiernan8c842872018-04-22 05:11:17 +0000304 if (type == IH_TYPE_SCRIPT && !params.datafile)
305 usage("Missing data file for script (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700306 params.type = type;
307 }
308
309 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700310 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700311}
312
Simon Glass0b443de2016-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;
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200322 size_t map_len;
Simon Glass0b443de2016-02-22 22:55:33 -0700323
324 params.cmdname = *argv;
325 params.addr = 0;
326 params.ep = 0;
327
328 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000329
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530330 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200331 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530332 if (tparams == NULL) {
333 fprintf (stderr, "%s: unsupported type %s\n",
334 params.cmdname, genimg_get_type_name(params.type));
335 exit (EXIT_FAILURE);
336 }
337
338 /*
339 * check the passed arguments parameters meets the requirements
340 * as per image type to be generated/listed
341 */
342 if (tparams->check_params)
343 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700344 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530345
346 if (!params.eflag) {
347 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000348 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530349 if (params.xflag)
350 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000351 }
352
Peter Tyserc81296c2009-11-24 16:42:10 -0600353 if (params.fflag){
354 if (tparams->fflag_handle)
355 /*
356 * in some cases, some additional processing needs
357 * to be done if fflag is defined
358 *
359 * For ex. fit_handle_file for Fit file support
360 */
361 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000362
Peter Tyserc81296c2009-11-24 16:42:10 -0600363 if (retval != EXIT_SUCCESS)
364 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000365 }
366
Peter Tyserc81296c2009-11-24 16:42:10 -0600367 if (params.lflag || params.fflag) {
368 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
369 } else {
370 ifd = open (params.imagefile,
371 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
372 }
373
374 if (ifd < 0) {
375 fprintf (stderr, "%s: Can't open %s: %s\n",
376 params.cmdname, params.imagefile,
377 strerror(errno));
378 exit (EXIT_FAILURE);
379 }
380
381 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000382 /*
383 * list header information of existing image
384 */
385 if (fstat(ifd, &sbuf) < 0) {
386 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530387 params.cmdname, params.imagefile,
388 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000389 exit (EXIT_FAILURE);
390 }
391
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530392 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000393 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530394 "%s: Bad size: \"%s\" is not valid image\n",
395 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000396 exit (EXIT_FAILURE);
397 }
398
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400399 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
400 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000401 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530402 params.cmdname, params.imagefile,
403 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000404 exit (EXIT_FAILURE);
405 }
406
Jordan Hand73c02e52019-04-09 10:36:09 -0700407 if (params.fflag) {
408 /*
409 * Verifies the header format based on the expected header for image
410 * type in tparams
411 */
412 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
413 tparams, &params);
414 } else {
415 /**
416 * When listing the image, we are not given the image type. Simply check all
417 * image types to find one that matches our header
418 */
419 retval = imagetool_verify_print_header(ptr, &sbuf,
420 tparams, &params);
421 }
wdenk5b1d7132002-11-03 00:07:02 +0000422
wdenk5b1d7132002-11-03 00:07:02 +0000423 (void) munmap((void *)ptr, sbuf.st_size);
424 (void) close (ifd);
425
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530426 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000427 }
428
Marek Vasut34633142015-12-07 18:01:54 +0100429 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200430 dfd = open(params.datafile, O_RDONLY | O_BINARY);
431 if (dfd < 0) {
432 fprintf(stderr, "%s: Can't open %s: %s\n",
433 params.cmdname, params.datafile,
434 strerror(errno));
435 exit(EXIT_FAILURE);
436 }
Simon Glass92a655c2015-06-23 15:39:12 -0600437
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200438 if (fstat(dfd, &sbuf) < 0) {
439 fprintf(stderr, "%s: Can't stat %s: %s\n",
440 params.cmdname, params.datafile,
441 strerror(errno));
442 exit(EXIT_FAILURE);
443 }
Simon Glass92a655c2015-06-23 15:39:12 -0600444
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200445 params.file_size = sbuf.st_size + tparams->header_size;
446 close(dfd);
447 }
Simon Glass92a655c2015-06-23 15:39:12 -0600448
wdenk5b1d7132002-11-03 00:07:02 +0000449 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000450 * In case there an header with a variable
451 * length will be added, the corresponding
452 * function is called. This is responsible to
453 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000454 */
Stefano Babicf0662102011-09-15 23:50:16 +0000455 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200456 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000457 else
458 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000459
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530460 if (write(ifd, tparams->hdr, tparams->header_size)
461 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000462 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530463 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000464 exit (EXIT_FAILURE);
465 }
466
Christian Rieschd1be8f92011-12-09 09:47:38 +0000467 if (!params.skipcpy) {
468 if (params.type == IH_TYPE_MULTI ||
469 params.type == IH_TYPE_SCRIPT) {
470 char *file = params.datafile;
471 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000472
Christian Rieschd1be8f92011-12-09 09:47:38 +0000473 for (;;) {
474 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000475
Christian Rieschd1be8f92011-12-09 09:47:38 +0000476 if (file) {
477 if ((sep = strchr(file, ':')) != NULL) {
478 *sep = '\0';
479 }
480
481 if (stat (file, &sbuf) < 0) {
482 fprintf (stderr, "%s: Can't stat %s: %s\n",
483 params.cmdname, file, strerror(errno));
484 exit (EXIT_FAILURE);
485 }
486 size = cpu_to_uimage (sbuf.st_size);
487 } else {
488 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000489 }
490
Christian Rieschd1be8f92011-12-09 09:47:38 +0000491 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
492 fprintf (stderr, "%s: Write error on %s: %s\n",
493 params.cmdname, params.imagefile,
494 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000495 exit (EXIT_FAILURE);
496 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000497
498 if (!file) {
499 break;
500 }
501
502 if (sep) {
503 *sep = ':';
504 file = sep + 1;
505 } else {
506 file = NULL;
507 }
wdenk5b1d7132002-11-03 00:07:02 +0000508 }
509
Christian Rieschd1be8f92011-12-09 09:47:38 +0000510 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000511
Christian Rieschd1be8f92011-12-09 09:47:38 +0000512 for (;;) {
513 char *sep = strchr(file, ':');
514 if (sep) {
515 *sep = '\0';
516 copy_file (ifd, file, 1);
517 *sep++ = ':';
518 file = sep;
519 } else {
520 copy_file (ifd, file, 0);
521 break;
522 }
wdenk5b1d7132002-11-03 00:07:02 +0000523 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000524 } else if (params.type == IH_TYPE_PBLIMAGE) {
525 /* PBL has special Image format, implements its' own */
526 pbl_load_uboot(ifd, &params);
Alexander Graf6915dcf2018-04-13 14:18:52 +0200527 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
528 /* Image file is meta, walk through actual targets */
529 int ret;
530
531 ret = zynqmpbif_copy_image(ifd, &params);
532 if (ret)
533 return ret;
Peng Fana2b96ec2018-10-16 04:50:30 +0000534 } else if (params.type == IH_TYPE_IMX8IMAGE) {
535 /* i.MX8/8X has special Image format */
536 int ret;
537
538 ret = imx8image_copy_image(ifd, &params);
539 if (ret)
540 return ret;
Peng Fan6609c262018-11-20 10:19:36 +0000541 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
542 /* i.MX8M has special Image format */
543 int ret;
544
545 ret = imx8mimage_copy_image(ifd, &params);
546 if (ret)
547 return ret;
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800548 } else if ((params.type == IH_TYPE_RKSD) ||
549 (params.type == IH_TYPE_RKSPI)) {
550 /* Rockchip has special Image format */
551 int ret;
552
553 ret = rockchip_copy_image(ifd, &params);
554 if (ret)
555 return ret;
Christian Rieschd1be8f92011-12-09 09:47:38 +0000556 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200557 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000558 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100559 if (params.type == IH_TYPE_FIRMWARE_IVT) {
560 /* Add alignment and IVT */
Kever Yang29e7ab02020-03-30 11:56:19 +0800561 uint32_t aligned_filesize = ALIGN(params.file_size,
562 0x1000);
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100563 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
564 params.addr, 0, 0, 0, params.addr
565 + aligned_filesize
566 - tparams->header_size,
567 params.addr + aligned_filesize
568 - tparams->header_size
569 + 0x20, 0 };
570 int i = params.file_size;
571 for (; i < aligned_filesize; i++) {
Sven Ebenfeldb4e923a2017-01-17 19:36:51 +0100572 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100573 fprintf(stderr,
574 "%s: Write error on %s: %s\n",
575 params.cmdname,
576 params.imagefile,
577 strerror(errno));
578 exit(EXIT_FAILURE);
579 }
580 }
581 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
582 != sizeof(flash_header_v2_t)) {
583 fprintf(stderr, "%s: Write error on %s: %s\n",
584 params.cmdname,
585 params.imagefile,
586 strerror(errno));
587 exit(EXIT_FAILURE);
588 }
589 }
wdenk5b1d7132002-11-03 00:07:02 +0000590 }
591
592 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530593#if defined(_POSIX_SYNCHRONIZED_IO) && \
594 !defined(__sun__) && \
595 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200596 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530597 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000598 (void) fdatasync (ifd);
599#else
600 (void) fsync (ifd);
601#endif
602
603 if (fstat(ifd, &sbuf) < 0) {
604 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530605 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000606 exit (EXIT_FAILURE);
607 }
Simon Glass92a655c2015-06-23 15:39:12 -0600608 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000609
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200610 map_len = sbuf.st_size;
611 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400612 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000613 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530614 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000615 exit (EXIT_FAILURE);
616 }
617
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530618 /* Setup the image header as per input image type*/
619 if (tparams->set_header)
620 tparams->set_header (ptr, &sbuf, ifd, &params);
621 else {
622 fprintf (stderr, "%s: Can't set header for %s: %s\n",
623 params.cmdname, tparams->name, strerror(errno));
624 exit (EXIT_FAILURE);
625 }
wdenk5b1d7132002-11-03 00:07:02 +0000626
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530627 /* Print the image information by processing image header */
628 if (tparams->print_header)
629 tparams->print_header (ptr);
630 else {
Guillaume GARDET004d0092018-03-30 10:28:19 +0200631 fprintf (stderr, "%s: Can't print header for %s\n",
632 params.cmdname, tparams->name);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530633 }
wdenk5b1d7132002-11-03 00:07:02 +0000634
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200635 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000636
637 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530638#if defined(_POSIX_SYNCHRONIZED_IO) && \
639 !defined(__sun__) && \
640 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200641 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530642 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000643 (void) fdatasync (ifd);
644#else
645 (void) fsync (ifd);
646#endif
647
648 if (close(ifd)) {
649 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530650 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000651 exit (EXIT_FAILURE);
652 }
653
654 exit (EXIT_SUCCESS);
655}
656
657static void
658copy_file (int ifd, const char *datafile, int pad)
659{
660 int dfd;
661 struct stat sbuf;
662 unsigned char *ptr;
663 int tail;
664 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200665 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000666 int offset = 0;
667 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200668 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000669
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200670 memset(zeros, 0, sizeof(zeros));
671
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530672 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000673 fprintf (stderr, "Adding Image %s\n", datafile);
674 }
675
wdenkef1464c2003-10-08 22:14:02 +0000676 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000677 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530678 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000679 exit (EXIT_FAILURE);
680 }
681
682 if (fstat(dfd, &sbuf) < 0) {
683 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530684 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000685 exit (EXIT_FAILURE);
686 }
687
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400688 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
689 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000690 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530691 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000692 exit (EXIT_FAILURE);
693 }
694
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530695 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000696 unsigned char *p = NULL;
697 /*
698 * XIP: do not append the image_header_t at the
699 * beginning of the file, but consume the space
700 * reserved for it.
701 */
702
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530703 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000704 fprintf (stderr,
705 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530706 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000707 exit (EXIT_FAILURE);
708 }
709
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530710 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000711 if ( *p != 0xff ) {
712 fprintf (stderr,
713 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530714 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000715 exit (EXIT_FAILURE);
716 }
717 }
718
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530719 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000720 }
721
722 size = sbuf.st_size - offset;
723 if (write(ifd, ptr + offset, size) != size) {
724 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530725 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000726 exit (EXIT_FAILURE);
727 }
728
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200729 tail = size % 4;
730 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000731
732 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
733 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530734 params.cmdname, params.imagefile,
735 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000736 exit (EXIT_FAILURE);
737 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200738 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600739 while (pad > 0) {
740 int todo = sizeof(zeros);
741
742 if (todo > pad)
743 todo = pad;
744 if (write(ifd, (char *)&zeros, todo) != todo) {
745 fprintf(stderr, "%s: Write error on %s: %s\n",
746 params.cmdname, params.imagefile,
747 strerror(errno));
748 exit(EXIT_FAILURE);
749 }
750 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200751 }
wdenk5b1d7132002-11-03 00:07:02 +0000752 }
753
754 (void) munmap((void *)ptr, sbuf.st_size);
755 (void) close (dfd);
756}