blob: c8f4ecd473d2b29d82cf9ce4f23c0ac925d25225 [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"
Simon Glasse4607262021-11-12 12:28:13 -070013#include <fit_common.h>
wdenk5b1d7132002-11-03 00:07:02 +000014#include <image.h>
Wolfgang Denke97ab722011-02-12 10:37:11 +010015#include <version.h>
Yann Dirson774aed52021-05-18 10:59:04 +020016#ifdef __linux__
17#include <sys/ioctl.h>
18#endif
wdenk5b1d7132002-11-03 00:07:02 +000019
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053020static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000021
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053022/* parameters initialized by core will be used by the image type code */
Simon Glass52322d62016-02-22 22:55:38 -070023static struct image_tool_params params = {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053024 .os = IH_OS_LINUX,
25 .arch = IH_ARCH_PPC,
26 .type = IH_TYPE_KERNEL,
27 .comp = IH_COMP_GZIP,
28 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
Wolfgang Denk23416b82010-03-27 23:37:46 +010029 .imagename = "",
Shaohui Xieea65fd82012-08-10 02:49:35 +000030 .imagename2 = "",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053031};
wdenk5b1d7132002-11-03 00:07:02 +000032
Simon Glass1d138b52016-06-30 10:52:17 -060033static enum ih_category cur_category;
34
35static int h_compare_category_name(const void *vtype1, const void *vtype2)
36{
37 const int *type1 = vtype1;
38 const int *type2 = vtype2;
39 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
40 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
41
42 return strcmp(name1, name2);
43}
44
Simon Glassbe421592016-06-30 10:52:18 -060045static int show_valid_options(enum ih_category category)
Simon Glass1d138b52016-06-30 10:52:17 -060046{
47 int *order;
48 int count;
49 int item;
50 int i;
51
52 count = genimg_get_cat_count(category);
53 order = calloc(count, sizeof(*order));
54 if (!order)
55 return -ENOMEM;
56
57 /* Sort the names in order of short name for easier reading */
Naoki Hayama089fc922020-10-07 11:21:55 +090058 for (i = 0, item = 0; i < count; i++, item++) {
59 while (!genimg_cat_has_id(category, item) && i < count) {
60 item++;
61 count--;
62 }
63 order[i] = item;
64 }
Simon Glass1d138b52016-06-30 10:52:17 -060065 cur_category = category;
66 qsort(order, count, sizeof(int), h_compare_category_name);
67
68 fprintf(stderr, "\nInvalid %s, supported are:\n",
69 genimg_get_cat_desc(category));
70 for (i = 0; i < count; i++) {
71 item = order[i];
72 fprintf(stderr, "\t%-15s %s\n",
73 genimg_get_cat_short_name(category, item),
74 genimg_get_cat_name(category, item));
75 }
76 fprintf(stderr, "\n");
Simon Glassd435d9f2016-10-27 17:54:03 -060077 free(order);
Simon Glass1d138b52016-06-30 10:52:17 -060078
79 return 0;
80}
81
Simon Glass01168b32016-02-22 22:55:37 -070082static void usage(const char *msg)
Simon Glass99a7c262016-02-22 22:55:36 -070083{
Simon Glass01168b32016-02-22 22:55:37 -070084 fprintf(stderr, "Error: %s\n", msg);
Simon Glass99a7c262016-02-22 22:55:36 -070085 fprintf(stderr, "Usage: %s -l image\n"
86 " -l ==> list image header information\n",
87 params.cmdname);
88 fprintf(stderr,
89 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
90 " -A ==> set architecture to 'arch'\n"
91 " -O ==> set operating system to 'os'\n"
92 " -T ==> set image type to 'type'\n"
93 " -C ==> set compression type 'comp'\n"
94 " -a ==> set load address to 'addr' (hex)\n"
95 " -e ==> set entry point to 'ep' (hex)\n"
96 " -n ==> set image name to 'name'\n"
97 " -d ==> use image data from 'datafile'\n"
98 " -x ==> set XIP (execute in place)\n",
99 params.cmdname);
100 fprintf(stderr,
Joel Stanley00dffdd2020-12-08 14:42:15 +1030101 " %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 -0700102 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glass99a7c262016-02-22 22:55:36 -0700103 params.cmdname);
104 fprintf(stderr,
105 " -D => set all options for device tree compiler\n"
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100106 " -f => input filename for FIT source\n"
Joel Stanley00dffdd2020-12-08 14:42:15 +1030107 " -i => input filename for ramdisk file\n"
108 " -E => place data outside of the FIT structure\n"
109 " -B => align size in hex for FIT structure and header\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700110#ifdef CONFIG_FIT_SIGNATURE
111 fprintf(stderr,
Joel Stanley00dffdd2020-12-08 14:42:15 +1030112 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700113 " -k => set directory containing private keys\n"
114 " -K => write public keys to this .dtb file\n"
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600115 " -G => use this signing key (in lieu of -k)\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700116 " -c => add comment in signature node\n"
117 " -F => re-sign existing FIT image\n"
Teddy Reeda8457622016-06-09 19:38:02 -0700118 " -p => place external data at a static position\n"
George McCollister23d14892017-01-06 13:14:17 -0600119 " -r => mark keys used as 'required' in dtb\n"
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300120 " -N => openssl engine to use for signing\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700121#else
122 fprintf(stderr,
123 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
124#endif
125 fprintf(stderr, " %s -V ==> print version information and exit\n",
126 params.cmdname);
Baruch Siachf77ab202017-06-29 20:37:08 +0300127 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700128
129 exit(EXIT_FAILURE);
130}
131
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700132static int add_content(int type, const char *fname)
133{
134 struct content_info *cont;
135
136 cont = calloc(1, sizeof(*cont));
137 if (!cont)
138 return -1;
139 cont->type = type;
140 cont->fname = fname;
141 if (params.content_tail)
142 params.content_tail->next = cont;
143 else
144 params.content_head = cont;
145 params.content_tail = cont;
146
147 return 0;
148}
149
Simon Glass7106bd42016-02-22 22:55:33 -0700150static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000151{
Peter Tyser632d9ca2010-04-04 22:36:03 -0500152 char *ptr;
Simon Glass04b46072016-02-22 22:55:48 -0700153 int type = IH_TYPE_INVALID;
154 char *datafile = NULL;
Simon Glasscf76cd22016-02-22 22:55:34 -0700155 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000156
Simon Glasscf76cd22016-02-22 22:55:34 -0700157 while ((opt = getopt(argc, argv,
Jan Kiszka4043f322022-01-14 10:21:19 +0100158 "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 -0700159 switch (opt) {
Simon Glassf6fe5f62016-02-22 22:55:35 -0700160 case 'a':
161 params.addr = strtoull(optarg, &ptr, 16);
162 if (*ptr) {
163 fprintf(stderr, "%s: invalid load address %s\n",
164 params.cmdname, optarg);
165 exit(EXIT_FAILURE);
166 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700167 break;
168 case 'A':
169 params.arch = genimg_get_arch_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600170 if (params.arch < 0) {
171 show_valid_options(IH_ARCH);
Simon Glass01168b32016-02-22 22:55:37 -0700172 usage("Invalid architecture");
Simon Glasseaa25402016-06-30 10:52:19 -0600173 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700174 break;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700175 case 'b':
Andreas Bießmannddd34772016-05-03 15:17:03 +0200176 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmannf4001582016-05-01 03:01:27 +0200177 fprintf(stderr,
178 "%s: Out of memory adding content '%s'",
179 params.cmdname, optarg);
180 exit(EXIT_FAILURE);
181 }
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700182 break;
Kever Yang8e238b52020-03-30 11:56:24 +0800183 case 'B':
184 params.bl_len = strtoull(optarg, &ptr, 16);
185 if (*ptr) {
186 fprintf(stderr, "%s: invalid block length %s\n",
187 params.cmdname, optarg);
188 exit(EXIT_FAILURE);
189 }
190
191 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700192 case 'c':
193 params.comment = optarg;
194 break;
195 case 'C':
196 params.comp = genimg_get_comp_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600197 if (params.comp < 0) {
198 show_valid_options(IH_COMP);
Simon Glass01168b32016-02-22 22:55:37 -0700199 usage("Invalid compression type");
Simon Glasseaa25402016-06-30 10:52:19 -0600200 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700201 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700202 case 'd':
203 params.datafile = optarg;
204 params.dflag = 1;
205 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700206 case 'D':
207 params.dtc = optarg;
208 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700209 case 'e':
210 params.ep = strtoull(optarg, &ptr, 16);
211 if (*ptr) {
212 fprintf(stderr, "%s: invalid entry point %s\n",
213 params.cmdname, optarg);
214 exit(EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000215 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700216 params.eflag = 1;
217 break;
Simon Glassafd728c2016-02-22 22:55:53 -0700218 case 'E':
219 params.external_data = true;
220 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700221 case 'f':
Simon Glass88e31cb2016-02-22 22:55:51 -0700222 datafile = optarg;
223 params.auto_its = !strcmp(datafile, "auto");
Heinrich Schuchardtddc38e62020-05-09 17:10:41 +0200224 /* fallthrough */
Simon Glasscf76cd22016-02-22 22:55:34 -0700225 case 'F':
226 /*
227 * The flattened image tree (FIT) format
228 * requires a flattened device tree image type
229 */
230 params.type = IH_TYPE_FLATDT;
231 params.fflag = 1;
232 break;
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600233 case 'G':
234 params.keyfile = optarg;
235 break;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100236 case 'i':
237 params.fit_ramdisk = optarg;
238 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700239 case 'k':
240 params.keydir = optarg;
241 break;
242 case 'K':
243 params.keydest = optarg;
244 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700245 case 'l':
246 params.lflag = 1;
247 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700248 case 'n':
249 params.imagename = optarg;
250 break;
George McCollister23d14892017-01-06 13:14:17 -0600251 case 'N':
252 params.engine_id = optarg;
253 break;
Jan Kiszka4043f322022-01-14 10:21:19 +0100254 case 'o':
255 params.algo_name = optarg;
256 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700257 case 'O':
258 params.os = genimg_get_os_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600259 if (params.os < 0) {
260 show_valid_options(IH_OS);
Simon Glass01168b32016-02-22 22:55:37 -0700261 usage("Invalid operating system");
Simon Glasseaa25402016-06-30 10:52:19 -0600262 }
Simon Glassf6fe5f62016-02-22 22:55:35 -0700263 break;
Teddy Reeda8457622016-06-09 19:38:02 -0700264 case 'p':
265 params.external_offset = strtoull(optarg, &ptr, 16);
266 if (*ptr) {
267 fprintf(stderr, "%s: invalid offset size %s\n",
268 params.cmdname, optarg);
269 exit(EXIT_FAILURE);
270 }
Teddy Reed9e3cd3c2016-07-11 22:54:26 -0700271 break;
Simon Glassda1805f2016-05-01 13:55:38 -0600272 case 'q':
273 params.quiet = 1;
274 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700275 case 'r':
276 params.require_keys = 1;
277 break;
278 case 'R':
279 /*
280 * This entry is for the second configuration
281 * file, if only one is not enough.
282 */
283 params.imagename2 = optarg;
284 break;
285 case 's':
286 params.skipcpy = 1;
287 break;
Simon Glass472ee0c2020-07-09 18:39:43 -0600288 case 't':
289 params.reset_timestamp = 1;
290 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700291 case 'T':
Baruch Siachf77ab202017-06-29 20:37:08 +0300292 if (strcmp(optarg, "list") == 0) {
293 show_valid_options(IH_TYPE);
294 exit(EXIT_SUCCESS);
295 }
Simon Glass04b46072016-02-22 22:55:48 -0700296 type = genimg_get_type_id(optarg);
297 if (type < 0) {
Simon Glassbe421592016-06-30 10:52:18 -0600298 show_valid_options(IH_TYPE);
Simon Glass01168b32016-02-22 22:55:37 -0700299 usage("Invalid image type");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700300 }
301 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700302 case 'v':
303 params.vflag++;
304 break;
305 case 'V':
306 printf("mkimage version %s\n", PLAIN_VERSION);
307 exit(EXIT_SUCCESS);
308 case 'x':
309 params.xflag++;
310 break;
311 default:
Simon Glass01168b32016-02-22 22:55:37 -0700312 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000313 }
wdenk5b1d7132002-11-03 00:07:02 +0000314 }
315
Andreas Bießmannddd34772016-05-03 15:17:03 +0200316 /* The last parameter is expected to be the imagefile */
317 if (optind < argc)
Andreas Bießmannf4001582016-05-01 03:01:27 +0200318 params.imagefile = argv[optind];
319
Simon Glass04b46072016-02-22 22:55:48 -0700320 /*
321 * For auto-generated FIT images we need to know the image type to put
322 * in the FIT, which is separate from the file's image type (which
323 * will always be IH_TYPE_FLATDT in this case).
324 */
325 if (params.type == IH_TYPE_FLATDT) {
Simon Glassa89857d2016-06-30 10:52:07 -0600326 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass156c3492016-06-30 10:52:08 -0600327 /* For auto_its, datafile is always 'auto' */
Simon Glass88e31cb2016-02-22 22:55:51 -0700328 if (!params.auto_its)
329 params.datafile = datafile;
Simon Glass79b5ee52016-06-30 10:52:09 -0600330 else if (!params.datafile)
331 usage("Missing data file for auto-FIT (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700332 } else if (type != IH_TYPE_INVALID) {
Alex Kiernan9d74c702018-04-22 05:11:17 +0000333 if (type == IH_TYPE_SCRIPT && !params.datafile)
334 usage("Missing data file for script (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700335 params.type = type;
336 }
337
338 if (!params.imagefile)
Simon Glass01168b32016-02-22 22:55:37 -0700339 usage("Missing output filename");
Simon Glass7106bd42016-02-22 22:55:33 -0700340}
341
Simon Glass7106bd42016-02-22 22:55:33 -0700342int main(int argc, char **argv)
343{
344 int ifd = -1;
345 struct stat sbuf;
346 char *ptr;
347 int retval = 0;
348 struct image_type_params *tparams = NULL;
349 int pad_len = 0;
350 int dfd;
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200351 size_t map_len;
Simon Glass7106bd42016-02-22 22:55:33 -0700352
353 params.cmdname = *argv;
354 params.addr = 0;
355 params.ep = 0;
356
357 process_args(argc, argv);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530358
359 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200360 tparams = imagetool_get_type(params.type);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530361 if (tparams == NULL) {
362 fprintf (stderr, "%s: unsupported type %s\n",
363 params.cmdname, genimg_get_type_name(params.type));
364 exit (EXIT_FAILURE);
365 }
366
367 /*
368 * check the passed arguments parameters meets the requirements
369 * as per image type to be generated/listed
370 */
371 if (tparams->check_params)
372 if (tparams->check_params (&params))
Simon Glass01168b32016-02-22 22:55:37 -0700373 usage("Bad parameters for image type");
wdenk5b1d7132002-11-03 00:07:02 +0000374
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530375 if (!params.eflag) {
376 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000377 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530378 if (params.xflag)
379 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000380 }
381
Peter Tyserb2f8b942009-11-24 16:42:10 -0600382 if (params.fflag){
383 if (tparams->fflag_handle)
384 /*
385 * in some cases, some additional processing needs
386 * to be done if fflag is defined
387 *
388 * For ex. fit_handle_file for Fit file support
389 */
390 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000391
Peter Tyserb2f8b942009-11-24 16:42:10 -0600392 if (retval != EXIT_SUCCESS)
393 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000394 }
395
Peter Tyserb2f8b942009-11-24 16:42:10 -0600396 if (params.lflag || params.fflag) {
397 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
398 } else {
399 ifd = open (params.imagefile,
400 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
401 }
402
403 if (ifd < 0) {
404 fprintf (stderr, "%s: Can't open %s: %s\n",
405 params.cmdname, params.imagefile,
406 strerror(errno));
407 exit (EXIT_FAILURE);
408 }
409
410 if (params.lflag || params.fflag) {
Yann Dirson774aed52021-05-18 10:59:04 +0200411 uint64_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000412 /*
413 * list header information of existing image
414 */
415 if (fstat(ifd, &sbuf) < 0) {
416 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530417 params.cmdname, params.imagefile,
418 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000419 exit (EXIT_FAILURE);
420 }
421
Yann Dirson774aed52021-05-18 10:59:04 +0200422 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
423#ifdef __linux__
424#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
425#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
426#endif
427 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
428 fprintf (stderr,
429 "%s: failed to get size of block device \"%s\"\n",
430 params.cmdname, params.imagefile);
431 exit (EXIT_FAILURE);
432 }
433#else
wdenk5b1d7132002-11-03 00:07:02 +0000434 fprintf (stderr,
Yann Dirson774aed52021-05-18 10:59:04 +0200435 "%s: \"%s\" is block device, don't know how to get its size\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530436 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000437 exit (EXIT_FAILURE);
Yann Dirson774aed52021-05-18 10:59:04 +0200438#endif
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100439 } else if (sbuf.st_size < (off_t)tparams->header_size) {
Yann Dirson774aed52021-05-18 10:59:04 +0200440 fprintf (stderr,
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100441 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
Yann Dirson774aed52021-05-18 10:59:04 +0200442 params.cmdname, params.imagefile,
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100443 (unsigned long long) sbuf.st_size,
444 tparams->header_size);
Yann Dirson774aed52021-05-18 10:59:04 +0200445 exit (EXIT_FAILURE);
446 } else {
447 size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000448 }
449
Yann Dirson774aed52021-05-18 10:59:04 +0200450 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400451 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000452 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530453 params.cmdname, params.imagefile,
454 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000455 exit (EXIT_FAILURE);
456 }
457
Jordan Hand408abe92019-04-09 10:36:09 -0700458 if (params.fflag) {
459 /*
460 * Verifies the header format based on the expected header for image
461 * type in tparams
462 */
463 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
464 tparams, &params);
465 } else {
466 /**
467 * When listing the image, we are not given the image type. Simply check all
468 * image types to find one that matches our header
469 */
470 retval = imagetool_verify_print_header(ptr, &sbuf,
471 tparams, &params);
472 }
wdenk5b1d7132002-11-03 00:07:02 +0000473
wdenk5b1d7132002-11-03 00:07:02 +0000474 (void) munmap((void *)ptr, sbuf.st_size);
475 (void) close (ifd);
Simon Glasse4607262021-11-12 12:28:13 -0700476 if (!retval)
477 summary_show(&params.summary, params.imagefile,
478 params.keydest);
wdenk5b1d7132002-11-03 00:07:02 +0000479
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530480 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000481 }
482
Marek Vasut4cc86272015-12-07 18:01:54 +0100483 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert3edd0412015-12-04 00:11:23 +0200484 dfd = open(params.datafile, O_RDONLY | O_BINARY);
485 if (dfd < 0) {
486 fprintf(stderr, "%s: Can't open %s: %s\n",
487 params.cmdname, params.datafile,
488 strerror(errno));
489 exit(EXIT_FAILURE);
490 }
Simon Glass139651f2015-06-23 15:39:12 -0600491
Philippe De Swert3edd0412015-12-04 00:11:23 +0200492 if (fstat(dfd, &sbuf) < 0) {
493 fprintf(stderr, "%s: Can't stat %s: %s\n",
494 params.cmdname, params.datafile,
495 strerror(errno));
496 exit(EXIT_FAILURE);
497 }
Simon Glass139651f2015-06-23 15:39:12 -0600498
Philippe De Swert3edd0412015-12-04 00:11:23 +0200499 params.file_size = sbuf.st_size + tparams->header_size;
500 close(dfd);
501 }
Simon Glass139651f2015-06-23 15:39:12 -0600502
wdenk5b1d7132002-11-03 00:07:02 +0000503 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000504 * In case there an header with a variable
505 * length will be added, the corresponding
506 * function is called. This is responsible to
507 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000508 */
Stefano Babic4a963302011-09-15 23:50:16 +0000509 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200510 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000511 else
512 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000513
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530514 if (write(ifd, tparams->hdr, tparams->header_size)
515 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000516 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530517 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000518 exit (EXIT_FAILURE);
519 }
520
Christian Rieschc14f2812011-12-09 09:47:38 +0000521 if (!params.skipcpy) {
522 if (params.type == IH_TYPE_MULTI ||
523 params.type == IH_TYPE_SCRIPT) {
524 char *file = params.datafile;
525 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000526
Christian Rieschc14f2812011-12-09 09:47:38 +0000527 for (;;) {
528 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000529
Christian Rieschc14f2812011-12-09 09:47:38 +0000530 if (file) {
531 if ((sep = strchr(file, ':')) != NULL) {
532 *sep = '\0';
533 }
534
535 if (stat (file, &sbuf) < 0) {
536 fprintf (stderr, "%s: Can't stat %s: %s\n",
537 params.cmdname, file, strerror(errno));
538 exit (EXIT_FAILURE);
539 }
540 size = cpu_to_uimage (sbuf.st_size);
541 } else {
542 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000543 }
544
Christian Rieschc14f2812011-12-09 09:47:38 +0000545 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
546 fprintf (stderr, "%s: Write error on %s: %s\n",
547 params.cmdname, params.imagefile,
548 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000549 exit (EXIT_FAILURE);
550 }
wdenk5b1d7132002-11-03 00:07:02 +0000551
Christian Rieschc14f2812011-12-09 09:47:38 +0000552 if (!file) {
553 break;
554 }
wdenk5b1d7132002-11-03 00:07:02 +0000555
Christian Rieschc14f2812011-12-09 09:47:38 +0000556 if (sep) {
557 *sep = ':';
558 file = sep + 1;
559 } else {
560 file = NULL;
561 }
wdenk5b1d7132002-11-03 00:07:02 +0000562 }
wdenk5b1d7132002-11-03 00:07:02 +0000563
Christian Rieschc14f2812011-12-09 09:47:38 +0000564 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000565
Christian Rieschc14f2812011-12-09 09:47:38 +0000566 for (;;) {
567 char *sep = strchr(file, ':');
568 if (sep) {
569 *sep = '\0';
570 copy_file (ifd, file, 1);
571 *sep++ = ':';
572 file = sep;
573 } else {
574 copy_file (ifd, file, 0);
575 break;
576 }
wdenk5b1d7132002-11-03 00:07:02 +0000577 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000578 } else if (params.type == IH_TYPE_PBLIMAGE) {
579 /* PBL has special Image format, implements its' own */
580 pbl_load_uboot(ifd, &params);
Alexander Graf5329d672018-04-13 14:18:52 +0200581 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
582 /* Image file is meta, walk through actual targets */
583 int ret;
584
585 ret = zynqmpbif_copy_image(ifd, &params);
586 if (ret)
587 return ret;
Peng Fan60a56072018-10-16 04:50:30 +0000588 } else if (params.type == IH_TYPE_IMX8IMAGE) {
589 /* i.MX8/8X has special Image format */
590 int ret;
591
592 ret = imx8image_copy_image(ifd, &params);
593 if (ret)
594 return ret;
Peng Fan31c51da2018-11-20 10:19:36 +0000595 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
596 /* i.MX8M has special Image format */
597 int ret;
598
599 ret = imx8mimage_copy_image(ifd, &params);
600 if (ret)
601 return ret;
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800602 } else if ((params.type == IH_TYPE_RKSD) ||
603 (params.type == IH_TYPE_RKSPI)) {
604 /* Rockchip has special Image format */
605 int ret;
606
607 ret = rockchip_copy_image(ifd, &params);
608 if (ret)
609 return ret;
Christian Rieschc14f2812011-12-09 09:47:38 +0000610 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200611 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000612 }
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100613 if (params.type == IH_TYPE_FIRMWARE_IVT) {
614 /* Add alignment and IVT */
Kever Yang86f26012020-03-30 11:56:19 +0800615 uint32_t aligned_filesize = ALIGN(params.file_size,
616 0x1000);
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100617 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
618 params.addr, 0, 0, 0, params.addr
619 + aligned_filesize
620 - tparams->header_size,
621 params.addr + aligned_filesize
622 - tparams->header_size
623 + 0x20, 0 };
624 int i = params.file_size;
625 for (; i < aligned_filesize; i++) {
Sven Ebenfeld3bb71e02017-01-17 19:36:51 +0100626 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100627 fprintf(stderr,
628 "%s: Write error on %s: %s\n",
629 params.cmdname,
630 params.imagefile,
631 strerror(errno));
632 exit(EXIT_FAILURE);
633 }
634 }
635 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
636 != sizeof(flash_header_v2_t)) {
637 fprintf(stderr, "%s: Write error on %s: %s\n",
638 params.cmdname,
639 params.imagefile,
640 strerror(errno));
641 exit(EXIT_FAILURE);
642 }
643 }
wdenk5b1d7132002-11-03 00:07:02 +0000644 }
645
646 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530647#if defined(_POSIX_SYNCHRONIZED_IO) && \
648 !defined(__sun__) && \
649 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200650 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530651 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000652 (void) fdatasync (ifd);
653#else
654 (void) fsync (ifd);
655#endif
656
657 if (fstat(ifd, &sbuf) < 0) {
658 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530659 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000660 exit (EXIT_FAILURE);
661 }
Simon Glass139651f2015-06-23 15:39:12 -0600662 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000663
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200664 map_len = sbuf.st_size;
665 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400666 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000667 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530668 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000669 exit (EXIT_FAILURE);
670 }
671
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530672 /* Setup the image header as per input image type*/
673 if (tparams->set_header)
674 tparams->set_header (ptr, &sbuf, ifd, &params);
675 else {
676 fprintf (stderr, "%s: Can't set header for %s: %s\n",
677 params.cmdname, tparams->name, strerror(errno));
678 exit (EXIT_FAILURE);
679 }
wdenk5b1d7132002-11-03 00:07:02 +0000680
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530681 /* Print the image information by processing image header */
682 if (tparams->print_header)
683 tparams->print_header (ptr);
684 else {
Guillaume GARDETdfe25fa2018-03-30 10:28:19 +0200685 fprintf (stderr, "%s: Can't print header for %s\n",
686 params.cmdname, tparams->name);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530687 }
wdenk5b1d7132002-11-03 00:07:02 +0000688
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200689 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000690
691 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530692#if defined(_POSIX_SYNCHRONIZED_IO) && \
693 !defined(__sun__) && \
694 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200695 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530696 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000697 (void) fdatasync (ifd);
698#else
699 (void) fsync (ifd);
700#endif
701
702 if (close(ifd)) {
703 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530704 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000705 exit (EXIT_FAILURE);
706 }
707
708 exit (EXIT_SUCCESS);
709}
710
711static void
712copy_file (int ifd, const char *datafile, int pad)
713{
714 int dfd;
715 struct stat sbuf;
716 unsigned char *ptr;
717 int tail;
718 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200719 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000720 int offset = 0;
Mylène Josserand9e0e9a52020-07-08 11:52:50 +0200721 int size, ret;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200722 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000723
Stefano Babic6531bd92013-08-19 19:03:19 +0200724 memset(zeros, 0, sizeof(zeros));
725
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530726 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000727 fprintf (stderr, "Adding Image %s\n", datafile);
728 }
729
wdenk1ee550e2003-10-08 22:14:02 +0000730 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000731 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530732 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000733 exit (EXIT_FAILURE);
734 }
735
736 if (fstat(dfd, &sbuf) < 0) {
737 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530738 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000739 exit (EXIT_FAILURE);
740 }
741
Thomas Hebb13895a32021-08-01 15:23:13 -0700742 if (sbuf.st_size == 0) {
743 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
744 params.cmdname, datafile);
745 exit (EXIT_FAILURE);
746 }
747
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400748 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
749 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000750 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530751 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000752 exit (EXIT_FAILURE);
753 }
754
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530755 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000756 unsigned char *p = NULL;
757 /*
758 * XIP: do not append the image_header_t at the
759 * beginning of the file, but consume the space
760 * reserved for it.
761 */
762
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530763 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000764 fprintf (stderr,
765 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530766 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000767 exit (EXIT_FAILURE);
768 }
769
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530770 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000771 if ( *p != 0xff ) {
772 fprintf (stderr,
773 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530774 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000775 exit (EXIT_FAILURE);
776 }
777 }
778
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530779 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000780 }
781
782 size = sbuf.st_size - offset;
Mylène Josserand9e0e9a52020-07-08 11:52:50 +0200783
784 ret = write(ifd, ptr + offset, size);
785 if (ret != size) {
786 if (ret < 0)
787 fprintf (stderr, "%s: Write error on %s: %s\n",
788 params.cmdname, params.imagefile, strerror(errno));
789 else if (ret < size)
790 fprintf (stderr, "%s: Write only %d/%d bytes, "\
791 "probably no space left on the device\n",
792 params.cmdname, ret, size);
wdenk5b1d7132002-11-03 00:07:02 +0000793 exit (EXIT_FAILURE);
794 }
795
Stefano Babic6531bd92013-08-19 19:03:19 +0200796 tail = size % 4;
797 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000798
799 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
800 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530801 params.cmdname, params.imagefile,
802 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000803 exit (EXIT_FAILURE);
804 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200805 } else if (pad > 1) {
Simon Glass46409cc2015-08-30 16:55:22 -0600806 while (pad > 0) {
807 int todo = sizeof(zeros);
808
809 if (todo > pad)
810 todo = pad;
811 if (write(ifd, (char *)&zeros, todo) != todo) {
812 fprintf(stderr, "%s: Write error on %s: %s\n",
813 params.cmdname, params.imagefile,
814 strerror(errno));
815 exit(EXIT_FAILURE);
816 }
817 pad -= todo;
Stefano Babic6531bd92013-08-19 19:03:19 +0200818 }
wdenk5b1d7132002-11-03 00:07:02 +0000819 }
820
821 (void) munmap((void *)ptr, sbuf.st_size);
822 (void) close (dfd);
823}