blob: 0ec28da33cbea434789f0bf4103ec58807196179 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00002/*
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +01003 * (C) Copyright 2008 Semihalf
4 *
Prafulla Wadaskarfabf3cf2009-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 Yang86f26012020-03-30 11:56:19 +080010#include "imagetool.h"
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>
Yann Dirson774aed52021-05-18 10:59:04 +020015#ifdef __linux__
16#include <sys/ioctl.h>
17#endif
wdenk5b1d7132002-11-03 00:07:02 +000018
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053019static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000020
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053021/* parameters initialized by core will be used by the image type code */
Simon Glass52322d62016-02-22 22:55:38 -070022static struct image_tool_params params = {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053023 .os = IH_OS_LINUX,
24 .arch = IH_ARCH_PPC,
25 .type = IH_TYPE_KERNEL,
26 .comp = IH_COMP_GZIP,
27 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
Wolfgang Denk23416b82010-03-27 23:37:46 +010028 .imagename = "",
Shaohui Xieea65fd82012-08-10 02:49:35 +000029 .imagename2 = "",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053030};
wdenk5b1d7132002-11-03 00:07:02 +000031
Simon Glass1d138b52016-06-30 10:52:17 -060032static enum ih_category cur_category;
33
34static int h_compare_category_name(const void *vtype1, const void *vtype2)
35{
36 const int *type1 = vtype1;
37 const int *type2 = vtype2;
38 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
39 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
40
41 return strcmp(name1, name2);
42}
43
Simon Glassbe421592016-06-30 10:52:18 -060044static int show_valid_options(enum ih_category category)
Simon Glass1d138b52016-06-30 10:52:17 -060045{
46 int *order;
47 int count;
48 int item;
49 int i;
50
51 count = genimg_get_cat_count(category);
52 order = calloc(count, sizeof(*order));
53 if (!order)
54 return -ENOMEM;
55
56 /* Sort the names in order of short name for easier reading */
Naoki Hayama089fc922020-10-07 11:21:55 +090057 for (i = 0, item = 0; i < count; i++, item++) {
58 while (!genimg_cat_has_id(category, item) && i < count) {
59 item++;
60 count--;
61 }
62 order[i] = item;
63 }
Simon Glass1d138b52016-06-30 10:52:17 -060064 cur_category = category;
65 qsort(order, count, sizeof(int), h_compare_category_name);
66
67 fprintf(stderr, "\nInvalid %s, supported are:\n",
68 genimg_get_cat_desc(category));
69 for (i = 0; i < count; i++) {
70 item = order[i];
71 fprintf(stderr, "\t%-15s %s\n",
72 genimg_get_cat_short_name(category, item),
73 genimg_get_cat_name(category, item));
74 }
75 fprintf(stderr, "\n");
Simon Glassd435d9f2016-10-27 17:54:03 -060076 free(order);
Simon Glass1d138b52016-06-30 10:52:17 -060077
78 return 0;
79}
80
Simon Glass01168b32016-02-22 22:55:37 -070081static void usage(const char *msg)
Simon Glass99a7c262016-02-22 22:55:36 -070082{
Simon Glass01168b32016-02-22 22:55:37 -070083 fprintf(stderr, "Error: %s\n", msg);
Simon Glass99a7c262016-02-22 22:55:36 -070084 fprintf(stderr, "Usage: %s -l image\n"
85 " -l ==> list image header information\n",
86 params.cmdname);
87 fprintf(stderr,
88 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
89 " -A ==> set architecture to 'arch'\n"
90 " -O ==> set operating system to 'os'\n"
91 " -T ==> set image type to 'type'\n"
92 " -C ==> set compression type 'comp'\n"
93 " -a ==> set load address to 'addr' (hex)\n"
94 " -e ==> set entry point to 'ep' (hex)\n"
95 " -n ==> set image name to 'name'\n"
96 " -d ==> use image data from 'datafile'\n"
97 " -x ==> set XIP (execute in place)\n",
98 params.cmdname);
99 fprintf(stderr,
Joel Stanley00dffdd2020-12-08 14:42:15 +1030100 " %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 Cascadian3e9e6d62016-10-23 20:45:17 -0700101 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glass99a7c262016-02-22 22:55:36 -0700102 params.cmdname);
103 fprintf(stderr,
104 " -D => set all options for device tree compiler\n"
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100105 " -f => input filename for FIT source\n"
Joel Stanley00dffdd2020-12-08 14:42:15 +1030106 " -i => input filename for ramdisk file\n"
107 " -E => place data outside of the FIT structure\n"
108 " -B => align size in hex for FIT structure and header\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700109#ifdef CONFIG_FIT_SIGNATURE
110 fprintf(stderr,
Joel Stanley00dffdd2020-12-08 14:42:15 +1030111 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700112 " -k => set directory containing private keys\n"
113 " -K => write public keys to this .dtb file\n"
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600114 " -G => use this signing key (in lieu of -k)\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700115 " -c => add comment in signature node\n"
116 " -F => re-sign existing FIT image\n"
Teddy Reeda8457622016-06-09 19:38:02 -0700117 " -p => place external data at a static position\n"
George McCollister23d14892017-01-06 13:14:17 -0600118 " -r => mark keys used as 'required' in dtb\n"
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300119 " -N => openssl engine to use for signing\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700120#else
121 fprintf(stderr,
122 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
123#endif
124 fprintf(stderr, " %s -V ==> print version information and exit\n",
125 params.cmdname);
Baruch Siachf77ab202017-06-29 20:37:08 +0300126 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700127
128 exit(EXIT_FAILURE);
129}
130
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700131static int add_content(int type, const char *fname)
132{
133 struct content_info *cont;
134
135 cont = calloc(1, sizeof(*cont));
136 if (!cont)
137 return -1;
138 cont->type = type;
139 cont->fname = fname;
140 if (params.content_tail)
141 params.content_tail->next = cont;
142 else
143 params.content_head = cont;
144 params.content_tail = cont;
145
146 return 0;
147}
148
Simon Glass7106bd42016-02-22 22:55:33 -0700149static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000150{
Peter Tyser632d9ca2010-04-04 22:36:03 -0500151 char *ptr;
Simon Glass04b46072016-02-22 22:55:48 -0700152 int type = IH_TYPE_INVALID;
153 char *datafile = NULL;
Simon Glasscf76cd22016-02-22 22:55:34 -0700154 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000155
Simon Glasscf76cd22016-02-22 22:55:34 -0700156 while ((opt = getopt(argc, argv,
Jan Kiszka4043f322022-01-14 10:21:19 +0100157 "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:o:O:rR:qstT:vVx")) != -1) {
Simon Glasscf76cd22016-02-22 22:55:34 -0700158 switch (opt) {
Simon Glassf6fe5f62016-02-22 22:55:35 -0700159 case 'a':
160 params.addr = strtoull(optarg, &ptr, 16);
161 if (*ptr) {
162 fprintf(stderr, "%s: invalid load address %s\n",
163 params.cmdname, optarg);
164 exit(EXIT_FAILURE);
165 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700166 break;
167 case 'A':
168 params.arch = genimg_get_arch_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600169 if (params.arch < 0) {
170 show_valid_options(IH_ARCH);
Simon Glass01168b32016-02-22 22:55:37 -0700171 usage("Invalid architecture");
Simon Glasseaa25402016-06-30 10:52:19 -0600172 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700173 break;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700174 case 'b':
Andreas Bießmannddd34772016-05-03 15:17:03 +0200175 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmannf4001582016-05-01 03:01:27 +0200176 fprintf(stderr,
177 "%s: Out of memory adding content '%s'",
178 params.cmdname, optarg);
179 exit(EXIT_FAILURE);
180 }
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700181 break;
Kever Yang8e238b52020-03-30 11:56:24 +0800182 case 'B':
183 params.bl_len = strtoull(optarg, &ptr, 16);
184 if (*ptr) {
185 fprintf(stderr, "%s: invalid block length %s\n",
186 params.cmdname, optarg);
187 exit(EXIT_FAILURE);
188 }
189
190 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700191 case 'c':
192 params.comment = optarg;
193 break;
194 case 'C':
195 params.comp = genimg_get_comp_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600196 if (params.comp < 0) {
197 show_valid_options(IH_COMP);
Simon Glass01168b32016-02-22 22:55:37 -0700198 usage("Invalid compression type");
Simon Glasseaa25402016-06-30 10:52:19 -0600199 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700200 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700201 case 'd':
202 params.datafile = optarg;
203 params.dflag = 1;
204 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700205 case 'D':
206 params.dtc = optarg;
207 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700208 case 'e':
209 params.ep = strtoull(optarg, &ptr, 16);
210 if (*ptr) {
211 fprintf(stderr, "%s: invalid entry point %s\n",
212 params.cmdname, optarg);
213 exit(EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000214 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700215 params.eflag = 1;
216 break;
Simon Glassafd728c2016-02-22 22:55:53 -0700217 case 'E':
218 params.external_data = true;
219 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700220 case 'f':
Simon Glass88e31cb2016-02-22 22:55:51 -0700221 datafile = optarg;
222 params.auto_its = !strcmp(datafile, "auto");
Heinrich Schuchardtddc38e62020-05-09 17:10:41 +0200223 /* fallthrough */
Simon Glasscf76cd22016-02-22 22:55:34 -0700224 case 'F':
225 /*
226 * The flattened image tree (FIT) format
227 * requires a flattened device tree image type
228 */
229 params.type = IH_TYPE_FLATDT;
230 params.fflag = 1;
231 break;
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600232 case 'G':
233 params.keyfile = optarg;
234 break;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100235 case 'i':
236 params.fit_ramdisk = optarg;
237 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700238 case 'k':
239 params.keydir = optarg;
240 break;
241 case 'K':
242 params.keydest = optarg;
243 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700244 case 'l':
245 params.lflag = 1;
246 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700247 case 'n':
248 params.imagename = optarg;
249 break;
George McCollister23d14892017-01-06 13:14:17 -0600250 case 'N':
251 params.engine_id = optarg;
252 break;
Jan Kiszka4043f322022-01-14 10:21:19 +0100253 case 'o':
254 params.algo_name = optarg;
255 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700256 case 'O':
257 params.os = genimg_get_os_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600258 if (params.os < 0) {
259 show_valid_options(IH_OS);
Simon Glass01168b32016-02-22 22:55:37 -0700260 usage("Invalid operating system");
Simon Glasseaa25402016-06-30 10:52:19 -0600261 }
Simon Glassf6fe5f62016-02-22 22:55:35 -0700262 break;
Teddy Reeda8457622016-06-09 19:38:02 -0700263 case 'p':
264 params.external_offset = strtoull(optarg, &ptr, 16);
265 if (*ptr) {
266 fprintf(stderr, "%s: invalid offset size %s\n",
267 params.cmdname, optarg);
268 exit(EXIT_FAILURE);
269 }
Teddy Reed9e3cd3c2016-07-11 22:54:26 -0700270 break;
Simon Glassda1805f2016-05-01 13:55:38 -0600271 case 'q':
272 params.quiet = 1;
273 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700274 case 'r':
275 params.require_keys = 1;
276 break;
277 case 'R':
278 /*
279 * This entry is for the second configuration
280 * file, if only one is not enough.
281 */
282 params.imagename2 = optarg;
283 break;
284 case 's':
285 params.skipcpy = 1;
286 break;
Simon Glass472ee0c2020-07-09 18:39:43 -0600287 case 't':
288 params.reset_timestamp = 1;
289 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700290 case 'T':
Baruch Siachf77ab202017-06-29 20:37:08 +0300291 if (strcmp(optarg, "list") == 0) {
292 show_valid_options(IH_TYPE);
293 exit(EXIT_SUCCESS);
294 }
Simon Glass04b46072016-02-22 22:55:48 -0700295 type = genimg_get_type_id(optarg);
296 if (type < 0) {
Simon Glassbe421592016-06-30 10:52:18 -0600297 show_valid_options(IH_TYPE);
Simon Glass01168b32016-02-22 22:55:37 -0700298 usage("Invalid image type");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700299 }
300 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700301 case 'v':
302 params.vflag++;
303 break;
304 case 'V':
305 printf("mkimage version %s\n", PLAIN_VERSION);
306 exit(EXIT_SUCCESS);
307 case 'x':
308 params.xflag++;
309 break;
310 default:
Simon Glass01168b32016-02-22 22:55:37 -0700311 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000312 }
wdenk5b1d7132002-11-03 00:07:02 +0000313 }
314
Andreas Bießmannddd34772016-05-03 15:17:03 +0200315 /* The last parameter is expected to be the imagefile */
316 if (optind < argc)
Andreas Bießmannf4001582016-05-01 03:01:27 +0200317 params.imagefile = argv[optind];
318
Simon Glass04b46072016-02-22 22:55:48 -0700319 /*
320 * For auto-generated FIT images we need to know the image type to put
321 * in the FIT, which is separate from the file's image type (which
322 * will always be IH_TYPE_FLATDT in this case).
323 */
324 if (params.type == IH_TYPE_FLATDT) {
Simon Glassa89857d2016-06-30 10:52:07 -0600325 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass156c3492016-06-30 10:52:08 -0600326 /* For auto_its, datafile is always 'auto' */
Simon Glass88e31cb2016-02-22 22:55:51 -0700327 if (!params.auto_its)
328 params.datafile = datafile;
Simon Glass79b5ee52016-06-30 10:52:09 -0600329 else if (!params.datafile)
330 usage("Missing data file for auto-FIT (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700331 } else if (type != IH_TYPE_INVALID) {
Alex Kiernan9d74c702018-04-22 05:11:17 +0000332 if (type == IH_TYPE_SCRIPT && !params.datafile)
333 usage("Missing data file for script (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700334 params.type = type;
335 }
336
337 if (!params.imagefile)
Simon Glass01168b32016-02-22 22:55:37 -0700338 usage("Missing output filename");
Simon Glass7106bd42016-02-22 22:55:33 -0700339}
340
Simon Glass7106bd42016-02-22 22:55:33 -0700341int main(int argc, char **argv)
342{
343 int ifd = -1;
344 struct stat sbuf;
345 char *ptr;
346 int retval = 0;
347 struct image_type_params *tparams = NULL;
348 int pad_len = 0;
349 int dfd;
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200350 size_t map_len;
Simon Glass7106bd42016-02-22 22:55:33 -0700351
352 params.cmdname = *argv;
353 params.addr = 0;
354 params.ep = 0;
355
356 process_args(argc, argv);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530357
358 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200359 tparams = imagetool_get_type(params.type);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530360 if (tparams == NULL) {
361 fprintf (stderr, "%s: unsupported type %s\n",
362 params.cmdname, genimg_get_type_name(params.type));
363 exit (EXIT_FAILURE);
364 }
365
366 /*
367 * check the passed arguments parameters meets the requirements
368 * as per image type to be generated/listed
369 */
370 if (tparams->check_params)
371 if (tparams->check_params (&params))
Simon Glass01168b32016-02-22 22:55:37 -0700372 usage("Bad parameters for image type");
wdenk5b1d7132002-11-03 00:07:02 +0000373
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530374 if (!params.eflag) {
375 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000376 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530377 if (params.xflag)
378 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000379 }
380
Peter Tyserb2f8b942009-11-24 16:42:10 -0600381 if (params.fflag){
382 if (tparams->fflag_handle)
383 /*
384 * in some cases, some additional processing needs
385 * to be done if fflag is defined
386 *
387 * For ex. fit_handle_file for Fit file support
388 */
389 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000390
Peter Tyserb2f8b942009-11-24 16:42:10 -0600391 if (retval != EXIT_SUCCESS)
392 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000393 }
394
Peter Tyserb2f8b942009-11-24 16:42:10 -0600395 if (params.lflag || params.fflag) {
396 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
397 } else {
398 ifd = open (params.imagefile,
399 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
400 }
401
402 if (ifd < 0) {
403 fprintf (stderr, "%s: Can't open %s: %s\n",
404 params.cmdname, params.imagefile,
405 strerror(errno));
406 exit (EXIT_FAILURE);
407 }
408
409 if (params.lflag || params.fflag) {
Yann Dirson774aed52021-05-18 10:59:04 +0200410 uint64_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000411 /*
412 * list header information of existing image
413 */
414 if (fstat(ifd, &sbuf) < 0) {
415 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530416 params.cmdname, params.imagefile,
417 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000418 exit (EXIT_FAILURE);
419 }
420
Yann Dirson774aed52021-05-18 10:59:04 +0200421 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
422#ifdef __linux__
423#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
424#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
425#endif
426 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
427 fprintf (stderr,
428 "%s: failed to get size of block device \"%s\"\n",
429 params.cmdname, params.imagefile);
430 exit (EXIT_FAILURE);
431 }
432#else
wdenk5b1d7132002-11-03 00:07:02 +0000433 fprintf (stderr,
Yann Dirson774aed52021-05-18 10:59:04 +0200434 "%s: \"%s\" is block device, don't know how to get its size\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530435 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000436 exit (EXIT_FAILURE);
Yann Dirson774aed52021-05-18 10:59:04 +0200437#endif
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100438 } else if (sbuf.st_size < (off_t)tparams->header_size) {
Yann Dirson774aed52021-05-18 10:59:04 +0200439 fprintf (stderr,
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100440 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
Yann Dirson774aed52021-05-18 10:59:04 +0200441 params.cmdname, params.imagefile,
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100442 (unsigned long long) sbuf.st_size,
443 tparams->header_size);
Yann Dirson774aed52021-05-18 10:59:04 +0200444 exit (EXIT_FAILURE);
445 } else {
446 size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000447 }
448
Yann Dirson774aed52021-05-18 10:59:04 +0200449 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400450 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000451 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530452 params.cmdname, params.imagefile,
453 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000454 exit (EXIT_FAILURE);
455 }
456
Jordan Hand408abe92019-04-09 10:36:09 -0700457 if (params.fflag) {
458 /*
459 * Verifies the header format based on the expected header for image
460 * type in tparams
461 */
462 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
463 tparams, &params);
464 } else {
465 /**
466 * When listing the image, we are not given the image type. Simply check all
467 * image types to find one that matches our header
468 */
469 retval = imagetool_verify_print_header(ptr, &sbuf,
470 tparams, &params);
471 }
wdenk5b1d7132002-11-03 00:07:02 +0000472
wdenk5b1d7132002-11-03 00:07:02 +0000473 (void) munmap((void *)ptr, sbuf.st_size);
474 (void) close (ifd);
475
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530476 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000477 }
478
Marek Vasut4cc86272015-12-07 18:01:54 +0100479 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert3edd0412015-12-04 00:11:23 +0200480 dfd = open(params.datafile, O_RDONLY | O_BINARY);
481 if (dfd < 0) {
482 fprintf(stderr, "%s: Can't open %s: %s\n",
483 params.cmdname, params.datafile,
484 strerror(errno));
485 exit(EXIT_FAILURE);
486 }
Simon Glass139651f2015-06-23 15:39:12 -0600487
Philippe De Swert3edd0412015-12-04 00:11:23 +0200488 if (fstat(dfd, &sbuf) < 0) {
489 fprintf(stderr, "%s: Can't stat %s: %s\n",
490 params.cmdname, params.datafile,
491 strerror(errno));
492 exit(EXIT_FAILURE);
493 }
Simon Glass139651f2015-06-23 15:39:12 -0600494
Philippe De Swert3edd0412015-12-04 00:11:23 +0200495 params.file_size = sbuf.st_size + tparams->header_size;
496 close(dfd);
497 }
Simon Glass139651f2015-06-23 15:39:12 -0600498
wdenk5b1d7132002-11-03 00:07:02 +0000499 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000500 * In case there an header with a variable
501 * length will be added, the corresponding
502 * function is called. This is responsible to
503 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000504 */
Stefano Babic4a963302011-09-15 23:50:16 +0000505 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200506 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000507 else
508 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000509
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530510 if (write(ifd, tparams->hdr, tparams->header_size)
511 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000512 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530513 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000514 exit (EXIT_FAILURE);
515 }
516
Christian Rieschc14f2812011-12-09 09:47:38 +0000517 if (!params.skipcpy) {
518 if (params.type == IH_TYPE_MULTI ||
519 params.type == IH_TYPE_SCRIPT) {
520 char *file = params.datafile;
521 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000522
Christian Rieschc14f2812011-12-09 09:47:38 +0000523 for (;;) {
524 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000525
Christian Rieschc14f2812011-12-09 09:47:38 +0000526 if (file) {
527 if ((sep = strchr(file, ':')) != NULL) {
528 *sep = '\0';
529 }
530
531 if (stat (file, &sbuf) < 0) {
532 fprintf (stderr, "%s: Can't stat %s: %s\n",
533 params.cmdname, file, strerror(errno));
534 exit (EXIT_FAILURE);
535 }
536 size = cpu_to_uimage (sbuf.st_size);
537 } else {
538 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000539 }
540
Christian Rieschc14f2812011-12-09 09:47:38 +0000541 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
542 fprintf (stderr, "%s: Write error on %s: %s\n",
543 params.cmdname, params.imagefile,
544 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000545 exit (EXIT_FAILURE);
546 }
wdenk5b1d7132002-11-03 00:07:02 +0000547
Christian Rieschc14f2812011-12-09 09:47:38 +0000548 if (!file) {
549 break;
550 }
wdenk5b1d7132002-11-03 00:07:02 +0000551
Christian Rieschc14f2812011-12-09 09:47:38 +0000552 if (sep) {
553 *sep = ':';
554 file = sep + 1;
555 } else {
556 file = NULL;
557 }
wdenk5b1d7132002-11-03 00:07:02 +0000558 }
wdenk5b1d7132002-11-03 00:07:02 +0000559
Christian Rieschc14f2812011-12-09 09:47:38 +0000560 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000561
Christian Rieschc14f2812011-12-09 09:47:38 +0000562 for (;;) {
563 char *sep = strchr(file, ':');
564 if (sep) {
565 *sep = '\0';
566 copy_file (ifd, file, 1);
567 *sep++ = ':';
568 file = sep;
569 } else {
570 copy_file (ifd, file, 0);
571 break;
572 }
wdenk5b1d7132002-11-03 00:07:02 +0000573 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000574 } else if (params.type == IH_TYPE_PBLIMAGE) {
575 /* PBL has special Image format, implements its' own */
576 pbl_load_uboot(ifd, &params);
Alexander Graf5329d672018-04-13 14:18:52 +0200577 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
578 /* Image file is meta, walk through actual targets */
579 int ret;
580
581 ret = zynqmpbif_copy_image(ifd, &params);
582 if (ret)
583 return ret;
Peng Fan60a56072018-10-16 04:50:30 +0000584 } else if (params.type == IH_TYPE_IMX8IMAGE) {
585 /* i.MX8/8X has special Image format */
586 int ret;
587
588 ret = imx8image_copy_image(ifd, &params);
589 if (ret)
590 return ret;
Peng Fan31c51da2018-11-20 10:19:36 +0000591 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
592 /* i.MX8M has special Image format */
593 int ret;
594
595 ret = imx8mimage_copy_image(ifd, &params);
596 if (ret)
597 return ret;
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800598 } else if ((params.type == IH_TYPE_RKSD) ||
599 (params.type == IH_TYPE_RKSPI)) {
600 /* Rockchip has special Image format */
601 int ret;
602
603 ret = rockchip_copy_image(ifd, &params);
604 if (ret)
605 return ret;
Christian Rieschc14f2812011-12-09 09:47:38 +0000606 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200607 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000608 }
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100609 if (params.type == IH_TYPE_FIRMWARE_IVT) {
610 /* Add alignment and IVT */
Kever Yang86f26012020-03-30 11:56:19 +0800611 uint32_t aligned_filesize = ALIGN(params.file_size,
612 0x1000);
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100613 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
614 params.addr, 0, 0, 0, params.addr
615 + aligned_filesize
616 - tparams->header_size,
617 params.addr + aligned_filesize
618 - tparams->header_size
619 + 0x20, 0 };
620 int i = params.file_size;
621 for (; i < aligned_filesize; i++) {
Sven Ebenfeld3bb71e02017-01-17 19:36:51 +0100622 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100623 fprintf(stderr,
624 "%s: Write error on %s: %s\n",
625 params.cmdname,
626 params.imagefile,
627 strerror(errno));
628 exit(EXIT_FAILURE);
629 }
630 }
631 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
632 != sizeof(flash_header_v2_t)) {
633 fprintf(stderr, "%s: Write error on %s: %s\n",
634 params.cmdname,
635 params.imagefile,
636 strerror(errno));
637 exit(EXIT_FAILURE);
638 }
639 }
wdenk5b1d7132002-11-03 00:07:02 +0000640 }
641
642 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530643#if defined(_POSIX_SYNCHRONIZED_IO) && \
644 !defined(__sun__) && \
645 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200646 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530647 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000648 (void) fdatasync (ifd);
649#else
650 (void) fsync (ifd);
651#endif
652
653 if (fstat(ifd, &sbuf) < 0) {
654 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530655 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000656 exit (EXIT_FAILURE);
657 }
Simon Glass139651f2015-06-23 15:39:12 -0600658 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000659
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200660 map_len = sbuf.st_size;
661 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400662 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000663 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530664 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000665 exit (EXIT_FAILURE);
666 }
667
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530668 /* Setup the image header as per input image type*/
669 if (tparams->set_header)
670 tparams->set_header (ptr, &sbuf, ifd, &params);
671 else {
672 fprintf (stderr, "%s: Can't set header for %s: %s\n",
673 params.cmdname, tparams->name, strerror(errno));
674 exit (EXIT_FAILURE);
675 }
wdenk5b1d7132002-11-03 00:07:02 +0000676
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530677 /* Print the image information by processing image header */
678 if (tparams->print_header)
679 tparams->print_header (ptr);
680 else {
Guillaume GARDETdfe25fa2018-03-30 10:28:19 +0200681 fprintf (stderr, "%s: Can't print header for %s\n",
682 params.cmdname, tparams->name);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530683 }
wdenk5b1d7132002-11-03 00:07:02 +0000684
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200685 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000686
687 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530688#if defined(_POSIX_SYNCHRONIZED_IO) && \
689 !defined(__sun__) && \
690 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200691 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530692 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000693 (void) fdatasync (ifd);
694#else
695 (void) fsync (ifd);
696#endif
697
698 if (close(ifd)) {
699 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530700 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000701 exit (EXIT_FAILURE);
702 }
703
704 exit (EXIT_SUCCESS);
705}
706
707static void
708copy_file (int ifd, const char *datafile, int pad)
709{
710 int dfd;
711 struct stat sbuf;
712 unsigned char *ptr;
713 int tail;
714 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200715 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000716 int offset = 0;
Mylène Josserand9e0e9a52020-07-08 11:52:50 +0200717 int size, ret;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200718 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000719
Stefano Babic6531bd92013-08-19 19:03:19 +0200720 memset(zeros, 0, sizeof(zeros));
721
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530722 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000723 fprintf (stderr, "Adding Image %s\n", datafile);
724 }
725
wdenk1ee550e2003-10-08 22:14:02 +0000726 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000727 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530728 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000729 exit (EXIT_FAILURE);
730 }
731
732 if (fstat(dfd, &sbuf) < 0) {
733 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530734 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000735 exit (EXIT_FAILURE);
736 }
737
Thomas Hebb13895a32021-08-01 15:23:13 -0700738 if (sbuf.st_size == 0) {
739 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
740 params.cmdname, datafile);
741 exit (EXIT_FAILURE);
742 }
743
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400744 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
745 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000746 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530747 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000748 exit (EXIT_FAILURE);
749 }
750
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530751 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000752 unsigned char *p = NULL;
753 /*
754 * XIP: do not append the image_header_t at the
755 * beginning of the file, but consume the space
756 * reserved for it.
757 */
758
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530759 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000760 fprintf (stderr,
761 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530762 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000763 exit (EXIT_FAILURE);
764 }
765
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530766 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000767 if ( *p != 0xff ) {
768 fprintf (stderr,
769 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530770 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000771 exit (EXIT_FAILURE);
772 }
773 }
774
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530775 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000776 }
777
778 size = sbuf.st_size - offset;
Mylène Josserand9e0e9a52020-07-08 11:52:50 +0200779
780 ret = write(ifd, ptr + offset, size);
781 if (ret != size) {
782 if (ret < 0)
783 fprintf (stderr, "%s: Write error on %s: %s\n",
784 params.cmdname, params.imagefile, strerror(errno));
785 else if (ret < size)
786 fprintf (stderr, "%s: Write only %d/%d bytes, "\
787 "probably no space left on the device\n",
788 params.cmdname, ret, size);
wdenk5b1d7132002-11-03 00:07:02 +0000789 exit (EXIT_FAILURE);
790 }
791
Stefano Babic6531bd92013-08-19 19:03:19 +0200792 tail = size % 4;
793 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000794
795 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
796 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530797 params.cmdname, params.imagefile,
798 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000799 exit (EXIT_FAILURE);
800 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200801 } else if (pad > 1) {
Simon Glass46409cc2015-08-30 16:55:22 -0600802 while (pad > 0) {
803 int todo = sizeof(zeros);
804
805 if (todo > pad)
806 todo = pad;
807 if (write(ifd, (char *)&zeros, todo) != todo) {
808 fprintf(stderr, "%s: Write error on %s: %s\n",
809 params.cmdname, params.imagefile,
810 strerror(errno));
811 exit(EXIT_FAILURE);
812 }
813 pad -= todo;
Stefano Babic6531bd92013-08-19 19:03:19 +0200814 }
wdenk5b1d7132002-11-03 00:07:02 +0000815 }
816
817 (void) munmap((void *)ptr, sbuf.st_size);
818 (void) close (dfd);
819}