blob: cc7b242faf223c929b02a77a8d7df09717b6e4b7 [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 */
Naoki Hayamaad5fb9f2020-10-07 11:21:55 +090054 for (i = 0, item = 0; i < count; i++, item++) {
55 while (!genimg_cat_has_id(category, item) && i < count) {
56 item++;
57 count--;
58 }
59 order[i] = item;
60 }
Simon Glass30664222016-06-30 10:52:17 -060061 cur_category = category;
62 qsort(order, count, sizeof(int), h_compare_category_name);
63
64 fprintf(stderr, "\nInvalid %s, supported are:\n",
65 genimg_get_cat_desc(category));
66 for (i = 0; i < count; i++) {
67 item = order[i];
68 fprintf(stderr, "\t%-15s %s\n",
69 genimg_get_cat_short_name(category, item),
70 genimg_get_cat_name(category, item));
71 }
72 fprintf(stderr, "\n");
Simon Glass0cd82e22016-10-27 17:54:03 -060073 free(order);
Simon Glass30664222016-06-30 10:52:17 -060074
75 return 0;
76}
77
Simon Glass15310342016-02-22 22:55:37 -070078static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -070079{
Simon Glass15310342016-02-22 22:55:37 -070080 fprintf(stderr, "Error: %s\n", msg);
Simon Glassb0a487a2016-02-22 22:55:36 -070081 fprintf(stderr, "Usage: %s -l image\n"
82 " -l ==> list image header information\n",
83 params.cmdname);
84 fprintf(stderr,
85 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
86 " -A ==> set architecture to 'arch'\n"
87 " -O ==> set operating system to 'os'\n"
88 " -T ==> set image type to 'type'\n"
89 " -C ==> set compression type 'comp'\n"
90 " -a ==> set load address to 'addr' (hex)\n"
91 " -e ==> set entry point to 'ep' (hex)\n"
92 " -n ==> set image name to 'name'\n"
93 " -d ==> use image data from 'datafile'\n"
94 " -x ==> set XIP (execute in place)\n",
95 params.cmdname);
96 fprintf(stderr,
Joel Stanley603e26f2020-12-08 14:42:15 +103097 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-E] [-B size] [-i <ramdisk.cpio.gz>] fit-image\n"
Vagrant Cascadian82bd2f22016-10-23 20:45:17 -070098 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glassb0a487a2016-02-22 22:55:36 -070099 params.cmdname);
100 fprintf(stderr,
101 " -D => set all options for device tree compiler\n"
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100102 " -f => input filename for FIT source\n"
Joel Stanley603e26f2020-12-08 14:42:15 +1030103 " -i => input filename for ramdisk file\n"
104 " -E => place data outside of the FIT structure\n"
105 " -B => align size in hex for FIT structure and header\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700106#ifdef CONFIG_FIT_SIGNATURE
107 fprintf(stderr,
Joel Stanley603e26f2020-12-08 14:42:15 +1030108 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700109 " -k => set directory containing private keys\n"
110 " -K => write public keys to this .dtb file\n"
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600111 " -G => use this signing key (in lieu of -k)\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700112 " -c => add comment in signature node\n"
113 " -F => re-sign existing FIT image\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700114 " -p => place external data at a static position\n"
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600115 " -r => mark keys used as 'required' in dtb\n"
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300116 " -N => openssl engine to use for signing\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700117#else
118 fprintf(stderr,
119 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
120#endif
121 fprintf(stderr, " %s -V ==> print version information and exit\n",
122 params.cmdname);
Baruch Siach79aa33c2017-06-29 20:37:08 +0300123 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700124
125 exit(EXIT_FAILURE);
126}
127
Simon Glassfb4cce02016-02-22 22:55:52 -0700128static int add_content(int type, const char *fname)
129{
130 struct content_info *cont;
131
132 cont = calloc(1, sizeof(*cont));
133 if (!cont)
134 return -1;
135 cont->type = type;
136 cont->fname = fname;
137 if (params.content_tail)
138 params.content_tail->next = cont;
139 else
140 params.content_head = cont;
141 params.content_tail = cont;
142
143 return 0;
144}
145
Joel Stanley603e26f2020-12-08 14:42:15 +1030146#define OPT_STRING "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx"
Simon Glass0b443de2016-02-22 22:55:33 -0700147static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000148{
Peter Tysera2513e22010-04-04 22:36:03 -0500149 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700150 int type = IH_TYPE_INVALID;
151 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700152 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000153
Simon Glassa02221f2016-02-22 22:55:34 -0700154 while ((opt = getopt(argc, argv,
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600155 "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
Simon Glassa02221f2016-02-22 22:55:34 -0700156 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700157 case 'a':
158 params.addr = strtoull(optarg, &ptr, 16);
159 if (*ptr) {
160 fprintf(stderr, "%s: invalid load address %s\n",
161 params.cmdname, optarg);
162 exit(EXIT_FAILURE);
163 }
Simon Glassa02221f2016-02-22 22:55:34 -0700164 break;
165 case 'A':
166 params.arch = genimg_get_arch_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600167 if (params.arch < 0) {
168 show_valid_options(IH_ARCH);
Simon Glass15310342016-02-22 22:55:37 -0700169 usage("Invalid architecture");
Simon Glass51f03e62016-06-30 10:52:19 -0600170 }
Simon Glassa02221f2016-02-22 22:55:34 -0700171 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700172 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200173 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200174 fprintf(stderr,
175 "%s: Out of memory adding content '%s'",
176 params.cmdname, optarg);
177 exit(EXIT_FAILURE);
178 }
Simon Glassfb4cce02016-02-22 22:55:52 -0700179 break;
Kever Yangebfe6112020-03-30 11:56:24 +0800180 case 'B':
181 params.bl_len = strtoull(optarg, &ptr, 16);
182 if (*ptr) {
183 fprintf(stderr, "%s: invalid block length %s\n",
184 params.cmdname, optarg);
185 exit(EXIT_FAILURE);
186 }
187
188 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700189 case 'c':
190 params.comment = optarg;
191 break;
192 case 'C':
193 params.comp = genimg_get_comp_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600194 if (params.comp < 0) {
195 show_valid_options(IH_COMP);
Simon Glass15310342016-02-22 22:55:37 -0700196 usage("Invalid compression type");
Simon Glass51f03e62016-06-30 10:52:19 -0600197 }
Simon Glassa02221f2016-02-22 22:55:34 -0700198 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700199 case 'd':
200 params.datafile = optarg;
201 params.dflag = 1;
202 break;
Simon Glass07450082016-02-22 22:55:35 -0700203 case 'D':
204 params.dtc = optarg;
205 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700206 case 'e':
207 params.ep = strtoull(optarg, &ptr, 16);
208 if (*ptr) {
209 fprintf(stderr, "%s: invalid entry point %s\n",
210 params.cmdname, optarg);
211 exit(EXIT_FAILURE);
212 }
213 params.eflag = 1;
214 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700215 case 'E':
216 params.external_data = true;
217 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700218 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700219 datafile = optarg;
220 params.auto_its = !strcmp(datafile, "auto");
Heinrich Schuchardta29162a2020-05-09 17:10:41 +0200221 /* fallthrough */
Simon Glassa02221f2016-02-22 22:55:34 -0700222 case 'F':
223 /*
224 * The flattened image tree (FIT) format
225 * requires a flattened device tree image type
226 */
227 params.type = IH_TYPE_FLATDT;
228 params.fflag = 1;
229 break;
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600230 case 'G':
231 params.keyfile = optarg;
232 break;
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100233 case 'i':
234 params.fit_ramdisk = optarg;
235 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700236 case 'k':
237 params.keydir = optarg;
238 break;
239 case 'K':
240 params.keydest = optarg;
241 break;
Simon Glass07450082016-02-22 22:55:35 -0700242 case 'l':
243 params.lflag = 1;
244 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700245 case 'n':
246 params.imagename = optarg;
247 break;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600248 case 'N':
249 params.engine_id = optarg;
250 break;
Simon Glass07450082016-02-22 22:55:35 -0700251 case 'O':
252 params.os = genimg_get_os_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600253 if (params.os < 0) {
254 show_valid_options(IH_OS);
Simon Glass15310342016-02-22 22:55:37 -0700255 usage("Invalid operating system");
Simon Glass51f03e62016-06-30 10:52:19 -0600256 }
Simon Glass07450082016-02-22 22:55:35 -0700257 break;
Teddy Reedf8f91072016-06-09 19:38:02 -0700258 case 'p':
259 params.external_offset = strtoull(optarg, &ptr, 16);
260 if (*ptr) {
261 fprintf(stderr, "%s: invalid offset size %s\n",
262 params.cmdname, optarg);
263 exit(EXIT_FAILURE);
264 }
Teddy Reedb6fefa72016-07-11 22:54:26 -0700265 break;
Simon Glassbd6e1422016-05-01 13:55:38 -0600266 case 'q':
267 params.quiet = 1;
268 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700269 case 'r':
270 params.require_keys = 1;
271 break;
272 case 'R':
273 /*
274 * This entry is for the second configuration
275 * file, if only one is not enough.
276 */
277 params.imagename2 = optarg;
278 break;
279 case 's':
280 params.skipcpy = 1;
281 break;
Simon Glass152b2462020-07-09 18:39:43 -0600282 case 't':
283 params.reset_timestamp = 1;
284 break;
Simon Glass07450082016-02-22 22:55:35 -0700285 case 'T':
Baruch Siach79aa33c2017-06-29 20:37:08 +0300286 if (strcmp(optarg, "list") == 0) {
287 show_valid_options(IH_TYPE);
288 exit(EXIT_SUCCESS);
289 }
Simon Glassd505a092016-02-22 22:55:48 -0700290 type = genimg_get_type_id(optarg);
291 if (type < 0) {
Simon Glassf24e1052016-06-30 10:52:18 -0600292 show_valid_options(IH_TYPE);
Simon Glass15310342016-02-22 22:55:37 -0700293 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700294 }
295 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700296 case 'v':
297 params.vflag++;
298 break;
299 case 'V':
300 printf("mkimage version %s\n", PLAIN_VERSION);
301 exit(EXIT_SUCCESS);
302 case 'x':
303 params.xflag++;
304 break;
305 default:
Simon Glass15310342016-02-22 22:55:37 -0700306 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000307 }
wdenk5b1d7132002-11-03 00:07:02 +0000308 }
309
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200310 /* The last parameter is expected to be the imagefile */
311 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200312 params.imagefile = argv[optind];
313
Simon Glassd505a092016-02-22 22:55:48 -0700314 /*
315 * For auto-generated FIT images we need to know the image type to put
316 * in the FIT, which is separate from the file's image type (which
317 * will always be IH_TYPE_FLATDT in this case).
318 */
319 if (params.type == IH_TYPE_FLATDT) {
Simon Glass20deadd2016-06-30 10:52:07 -0600320 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600321 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700322 if (!params.auto_its)
323 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600324 else if (!params.datafile)
325 usage("Missing data file for auto-FIT (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700326 } else if (type != IH_TYPE_INVALID) {
Alex Kiernan8c842872018-04-22 05:11:17 +0000327 if (type == IH_TYPE_SCRIPT && !params.datafile)
328 usage("Missing data file for script (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700329 params.type = type;
330 }
331
332 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700333 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700334}
335
Simon Glass0b443de2016-02-22 22:55:33 -0700336int main(int argc, char **argv)
337{
338 int ifd = -1;
339 struct stat sbuf;
340 char *ptr;
341 int retval = 0;
342 struct image_type_params *tparams = NULL;
343 int pad_len = 0;
344 int dfd;
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200345 size_t map_len;
Simon Glass0b443de2016-02-22 22:55:33 -0700346
347 params.cmdname = *argv;
348 params.addr = 0;
349 params.ep = 0;
350
351 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000352
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530353 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200354 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530355 if (tparams == NULL) {
356 fprintf (stderr, "%s: unsupported type %s\n",
357 params.cmdname, genimg_get_type_name(params.type));
358 exit (EXIT_FAILURE);
359 }
360
361 /*
362 * check the passed arguments parameters meets the requirements
363 * as per image type to be generated/listed
364 */
365 if (tparams->check_params)
366 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700367 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530368
369 if (!params.eflag) {
370 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000371 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530372 if (params.xflag)
373 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000374 }
375
Peter Tyserc81296c2009-11-24 16:42:10 -0600376 if (params.fflag){
377 if (tparams->fflag_handle)
378 /*
379 * in some cases, some additional processing needs
380 * to be done if fflag is defined
381 *
382 * For ex. fit_handle_file for Fit file support
383 */
384 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000385
Peter Tyserc81296c2009-11-24 16:42:10 -0600386 if (retval != EXIT_SUCCESS)
387 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000388 }
389
Peter Tyserc81296c2009-11-24 16:42:10 -0600390 if (params.lflag || params.fflag) {
391 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
392 } else {
393 ifd = open (params.imagefile,
394 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
395 }
396
397 if (ifd < 0) {
398 fprintf (stderr, "%s: Can't open %s: %s\n",
399 params.cmdname, params.imagefile,
400 strerror(errno));
401 exit (EXIT_FAILURE);
402 }
403
404 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000405 /*
406 * list header information of existing image
407 */
408 if (fstat(ifd, &sbuf) < 0) {
409 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530410 params.cmdname, params.imagefile,
411 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000412 exit (EXIT_FAILURE);
413 }
414
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530415 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000416 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530417 "%s: Bad size: \"%s\" is not valid image\n",
418 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000419 exit (EXIT_FAILURE);
420 }
421
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400422 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
423 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000424 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530425 params.cmdname, params.imagefile,
426 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000427 exit (EXIT_FAILURE);
428 }
429
Jordan Hand73c02e52019-04-09 10:36:09 -0700430 if (params.fflag) {
431 /*
432 * Verifies the header format based on the expected header for image
433 * type in tparams
434 */
435 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
436 tparams, &params);
437 } else {
438 /**
439 * When listing the image, we are not given the image type. Simply check all
440 * image types to find one that matches our header
441 */
442 retval = imagetool_verify_print_header(ptr, &sbuf,
443 tparams, &params);
444 }
wdenk5b1d7132002-11-03 00:07:02 +0000445
wdenk5b1d7132002-11-03 00:07:02 +0000446 (void) munmap((void *)ptr, sbuf.st_size);
447 (void) close (ifd);
448
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530449 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000450 }
451
Marek Vasut34633142015-12-07 18:01:54 +0100452 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200453 dfd = open(params.datafile, O_RDONLY | O_BINARY);
454 if (dfd < 0) {
455 fprintf(stderr, "%s: Can't open %s: %s\n",
456 params.cmdname, params.datafile,
457 strerror(errno));
458 exit(EXIT_FAILURE);
459 }
Simon Glass92a655c2015-06-23 15:39:12 -0600460
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200461 if (fstat(dfd, &sbuf) < 0) {
462 fprintf(stderr, "%s: Can't stat %s: %s\n",
463 params.cmdname, params.datafile,
464 strerror(errno));
465 exit(EXIT_FAILURE);
466 }
Simon Glass92a655c2015-06-23 15:39:12 -0600467
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200468 params.file_size = sbuf.st_size + tparams->header_size;
469 close(dfd);
470 }
Simon Glass92a655c2015-06-23 15:39:12 -0600471
wdenk5b1d7132002-11-03 00:07:02 +0000472 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000473 * In case there an header with a variable
474 * length will be added, the corresponding
475 * function is called. This is responsible to
476 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000477 */
Stefano Babicf0662102011-09-15 23:50:16 +0000478 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200479 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000480 else
481 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000482
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530483 if (write(ifd, tparams->hdr, tparams->header_size)
484 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000485 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530486 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000487 exit (EXIT_FAILURE);
488 }
489
Christian Rieschd1be8f92011-12-09 09:47:38 +0000490 if (!params.skipcpy) {
491 if (params.type == IH_TYPE_MULTI ||
492 params.type == IH_TYPE_SCRIPT) {
493 char *file = params.datafile;
494 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000495
Christian Rieschd1be8f92011-12-09 09:47:38 +0000496 for (;;) {
497 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000498
Christian Rieschd1be8f92011-12-09 09:47:38 +0000499 if (file) {
500 if ((sep = strchr(file, ':')) != NULL) {
501 *sep = '\0';
502 }
503
504 if (stat (file, &sbuf) < 0) {
505 fprintf (stderr, "%s: Can't stat %s: %s\n",
506 params.cmdname, file, strerror(errno));
507 exit (EXIT_FAILURE);
508 }
509 size = cpu_to_uimage (sbuf.st_size);
510 } else {
511 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000512 }
513
Christian Rieschd1be8f92011-12-09 09:47:38 +0000514 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
515 fprintf (stderr, "%s: Write error on %s: %s\n",
516 params.cmdname, params.imagefile,
517 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000518 exit (EXIT_FAILURE);
519 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000520
521 if (!file) {
522 break;
523 }
524
525 if (sep) {
526 *sep = ':';
527 file = sep + 1;
528 } else {
529 file = NULL;
530 }
wdenk5b1d7132002-11-03 00:07:02 +0000531 }
532
Christian Rieschd1be8f92011-12-09 09:47:38 +0000533 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000534
Christian Rieschd1be8f92011-12-09 09:47:38 +0000535 for (;;) {
536 char *sep = strchr(file, ':');
537 if (sep) {
538 *sep = '\0';
539 copy_file (ifd, file, 1);
540 *sep++ = ':';
541 file = sep;
542 } else {
543 copy_file (ifd, file, 0);
544 break;
545 }
wdenk5b1d7132002-11-03 00:07:02 +0000546 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000547 } else if (params.type == IH_TYPE_PBLIMAGE) {
548 /* PBL has special Image format, implements its' own */
549 pbl_load_uboot(ifd, &params);
Alexander Graf6915dcf2018-04-13 14:18:52 +0200550 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
551 /* Image file is meta, walk through actual targets */
552 int ret;
553
554 ret = zynqmpbif_copy_image(ifd, &params);
555 if (ret)
556 return ret;
Peng Fana2b96ec2018-10-16 04:50:30 +0000557 } else if (params.type == IH_TYPE_IMX8IMAGE) {
558 /* i.MX8/8X has special Image format */
559 int ret;
560
561 ret = imx8image_copy_image(ifd, &params);
562 if (ret)
563 return ret;
Peng Fan6609c262018-11-20 10:19:36 +0000564 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
565 /* i.MX8M has special Image format */
566 int ret;
567
568 ret = imx8mimage_copy_image(ifd, &params);
569 if (ret)
570 return ret;
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800571 } else if ((params.type == IH_TYPE_RKSD) ||
572 (params.type == IH_TYPE_RKSPI)) {
573 /* Rockchip has special Image format */
574 int ret;
575
576 ret = rockchip_copy_image(ifd, &params);
577 if (ret)
578 return ret;
Christian Rieschd1be8f92011-12-09 09:47:38 +0000579 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200580 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000581 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100582 if (params.type == IH_TYPE_FIRMWARE_IVT) {
583 /* Add alignment and IVT */
Kever Yang29e7ab02020-03-30 11:56:19 +0800584 uint32_t aligned_filesize = ALIGN(params.file_size,
585 0x1000);
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100586 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
587 params.addr, 0, 0, 0, params.addr
588 + aligned_filesize
589 - tparams->header_size,
590 params.addr + aligned_filesize
591 - tparams->header_size
592 + 0x20, 0 };
593 int i = params.file_size;
594 for (; i < aligned_filesize; i++) {
Sven Ebenfeldb4e923a2017-01-17 19:36:51 +0100595 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100596 fprintf(stderr,
597 "%s: Write error on %s: %s\n",
598 params.cmdname,
599 params.imagefile,
600 strerror(errno));
601 exit(EXIT_FAILURE);
602 }
603 }
604 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
605 != sizeof(flash_header_v2_t)) {
606 fprintf(stderr, "%s: Write error on %s: %s\n",
607 params.cmdname,
608 params.imagefile,
609 strerror(errno));
610 exit(EXIT_FAILURE);
611 }
612 }
wdenk5b1d7132002-11-03 00:07:02 +0000613 }
614
615 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530616#if defined(_POSIX_SYNCHRONIZED_IO) && \
617 !defined(__sun__) && \
618 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200619 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530620 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000621 (void) fdatasync (ifd);
622#else
623 (void) fsync (ifd);
624#endif
625
626 if (fstat(ifd, &sbuf) < 0) {
627 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530628 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000629 exit (EXIT_FAILURE);
630 }
Simon Glass92a655c2015-06-23 15:39:12 -0600631 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000632
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200633 map_len = sbuf.st_size;
634 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400635 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000636 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530637 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000638 exit (EXIT_FAILURE);
639 }
640
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530641 /* Setup the image header as per input image type*/
642 if (tparams->set_header)
643 tparams->set_header (ptr, &sbuf, ifd, &params);
644 else {
645 fprintf (stderr, "%s: Can't set header for %s: %s\n",
646 params.cmdname, tparams->name, strerror(errno));
647 exit (EXIT_FAILURE);
648 }
wdenk5b1d7132002-11-03 00:07:02 +0000649
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530650 /* Print the image information by processing image header */
651 if (tparams->print_header)
652 tparams->print_header (ptr);
653 else {
Guillaume GARDET004d0092018-03-30 10:28:19 +0200654 fprintf (stderr, "%s: Can't print header for %s\n",
655 params.cmdname, tparams->name);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530656 }
wdenk5b1d7132002-11-03 00:07:02 +0000657
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200658 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000659
660 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530661#if defined(_POSIX_SYNCHRONIZED_IO) && \
662 !defined(__sun__) && \
663 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200664 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530665 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000666 (void) fdatasync (ifd);
667#else
668 (void) fsync (ifd);
669#endif
670
671 if (close(ifd)) {
672 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530673 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000674 exit (EXIT_FAILURE);
675 }
676
677 exit (EXIT_SUCCESS);
678}
679
680static void
681copy_file (int ifd, const char *datafile, int pad)
682{
683 int dfd;
684 struct stat sbuf;
685 unsigned char *ptr;
686 int tail;
687 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200688 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000689 int offset = 0;
Mylène Josseranddd85dc52020-07-08 11:52:50 +0200690 int size, ret;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200691 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000692
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200693 memset(zeros, 0, sizeof(zeros));
694
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530695 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000696 fprintf (stderr, "Adding Image %s\n", datafile);
697 }
698
wdenkef1464c2003-10-08 22:14:02 +0000699 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000700 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530701 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000702 exit (EXIT_FAILURE);
703 }
704
705 if (fstat(dfd, &sbuf) < 0) {
706 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530707 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000708 exit (EXIT_FAILURE);
709 }
710
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400711 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
712 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000713 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530714 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000715 exit (EXIT_FAILURE);
716 }
717
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530718 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000719 unsigned char *p = NULL;
720 /*
721 * XIP: do not append the image_header_t at the
722 * beginning of the file, but consume the space
723 * reserved for it.
724 */
725
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530726 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000727 fprintf (stderr,
728 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530729 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000730 exit (EXIT_FAILURE);
731 }
732
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530733 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000734 if ( *p != 0xff ) {
735 fprintf (stderr,
736 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530737 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000738 exit (EXIT_FAILURE);
739 }
740 }
741
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530742 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000743 }
744
745 size = sbuf.st_size - offset;
Mylène Josseranddd85dc52020-07-08 11:52:50 +0200746
747 ret = write(ifd, ptr + offset, size);
748 if (ret != size) {
749 if (ret < 0)
750 fprintf (stderr, "%s: Write error on %s: %s\n",
751 params.cmdname, params.imagefile, strerror(errno));
752 else if (ret < size)
753 fprintf (stderr, "%s: Write only %d/%d bytes, "\
754 "probably no space left on the device\n",
755 params.cmdname, ret, size);
wdenk5b1d7132002-11-03 00:07:02 +0000756 exit (EXIT_FAILURE);
757 }
758
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200759 tail = size % 4;
760 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000761
762 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
763 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530764 params.cmdname, params.imagefile,
765 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000766 exit (EXIT_FAILURE);
767 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200768 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600769 while (pad > 0) {
770 int todo = sizeof(zeros);
771
772 if (todo > pad)
773 todo = pad;
774 if (write(ifd, (char *)&zeros, todo) != todo) {
775 fprintf(stderr, "%s: Write error on %s: %s\n",
776 params.cmdname, params.imagefile,
777 strerror(errno));
778 exit(EXIT_FAILURE);
779 }
780 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200781 }
wdenk5b1d7132002-11-03 00:07:02 +0000782 }
783
784 (void) munmap((void *)ptr, sbuf.st_size);
785 (void) close (dfd);
786}