blob: 74bd072832c74908a8956847a374e1ddd02dab3a [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);
Pali Rohárcdf0d1c2022-02-13 01:09:46 +010085 fprintf(stderr, "Usage: %s [-T type] -l image\n"
86 " -l ==> list image header information\n"
87 " -T ==> parse image file as 'type'\n",
Simon Glass99a7c262016-02-22 22:55:36 -070088 params.cmdname);
89 fprintf(stderr,
90 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
91 " -A ==> set architecture to 'arch'\n"
92 " -O ==> set operating system to 'os'\n"
93 " -T ==> set image type to 'type'\n"
94 " -C ==> set compression type 'comp'\n"
95 " -a ==> set load address to 'addr' (hex)\n"
96 " -e ==> set entry point to 'ep' (hex)\n"
97 " -n ==> set image name to 'name'\n"
98 " -d ==> use image data from 'datafile'\n"
99 " -x ==> set XIP (execute in place)\n",
100 params.cmdname);
101 fprintf(stderr,
Joel Stanley00dffdd2020-12-08 14:42:15 +1030102 " %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 -0700103 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glass99a7c262016-02-22 22:55:36 -0700104 params.cmdname);
105 fprintf(stderr,
106 " -D => set all options for device tree compiler\n"
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100107 " -f => input filename for FIT source\n"
Joel Stanley00dffdd2020-12-08 14:42:15 +1030108 " -i => input filename for ramdisk file\n"
109 " -E => place data outside of the FIT structure\n"
110 " -B => align size in hex for FIT structure and header\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700111#ifdef CONFIG_FIT_SIGNATURE
112 fprintf(stderr,
Joel Stanley00dffdd2020-12-08 14:42:15 +1030113 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700114 " -k => set directory containing private keys\n"
115 " -K => write public keys to this .dtb file\n"
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600116 " -G => use this signing key (in lieu of -k)\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700117 " -c => add comment in signature node\n"
118 " -F => re-sign existing FIT image\n"
Teddy Reeda8457622016-06-09 19:38:02 -0700119 " -p => place external data at a static position\n"
George McCollister23d14892017-01-06 13:14:17 -0600120 " -r => mark keys used as 'required' in dtb\n"
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300121 " -N => openssl engine to use for signing\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700122#else
123 fprintf(stderr,
124 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
125#endif
126 fprintf(stderr, " %s -V ==> print version information and exit\n",
127 params.cmdname);
Baruch Siachf77ab202017-06-29 20:37:08 +0300128 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700129
130 exit(EXIT_FAILURE);
131}
132
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700133static int add_content(int type, const char *fname)
134{
135 struct content_info *cont;
136
137 cont = calloc(1, sizeof(*cont));
138 if (!cont)
139 return -1;
140 cont->type = type;
141 cont->fname = fname;
142 if (params.content_tail)
143 params.content_tail->next = cont;
144 else
145 params.content_head = cont;
146 params.content_tail = cont;
147
148 return 0;
149}
150
Simon Glass7106bd42016-02-22 22:55:33 -0700151static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000152{
Peter Tyser632d9ca2010-04-04 22:36:03 -0500153 char *ptr;
Simon Glass04b46072016-02-22 22:55:48 -0700154 int type = IH_TYPE_INVALID;
155 char *datafile = NULL;
Simon Glasscf76cd22016-02-22 22:55:34 -0700156 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000157
Simon Glasscf76cd22016-02-22 22:55:34 -0700158 while ((opt = getopt(argc, argv,
Jan Kiszka4043f322022-01-14 10:21:19 +0100159 "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 -0700160 switch (opt) {
Simon Glassf6fe5f62016-02-22 22:55:35 -0700161 case 'a':
162 params.addr = strtoull(optarg, &ptr, 16);
163 if (*ptr) {
164 fprintf(stderr, "%s: invalid load address %s\n",
165 params.cmdname, optarg);
166 exit(EXIT_FAILURE);
167 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700168 break;
169 case 'A':
170 params.arch = genimg_get_arch_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600171 if (params.arch < 0) {
172 show_valid_options(IH_ARCH);
Simon Glass01168b32016-02-22 22:55:37 -0700173 usage("Invalid architecture");
Simon Glasseaa25402016-06-30 10:52:19 -0600174 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700175 break;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700176 case 'b':
Andreas Bießmannddd34772016-05-03 15:17:03 +0200177 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmannf4001582016-05-01 03:01:27 +0200178 fprintf(stderr,
179 "%s: Out of memory adding content '%s'",
180 params.cmdname, optarg);
181 exit(EXIT_FAILURE);
182 }
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700183 break;
Kever Yang8e238b52020-03-30 11:56:24 +0800184 case 'B':
185 params.bl_len = strtoull(optarg, &ptr, 16);
186 if (*ptr) {
187 fprintf(stderr, "%s: invalid block length %s\n",
188 params.cmdname, optarg);
189 exit(EXIT_FAILURE);
190 }
191
192 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700193 case 'c':
194 params.comment = optarg;
195 break;
196 case 'C':
197 params.comp = genimg_get_comp_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600198 if (params.comp < 0) {
199 show_valid_options(IH_COMP);
Simon Glass01168b32016-02-22 22:55:37 -0700200 usage("Invalid compression type");
Simon Glasseaa25402016-06-30 10:52:19 -0600201 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700202 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700203 case 'd':
204 params.datafile = optarg;
205 params.dflag = 1;
206 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700207 case 'D':
208 params.dtc = optarg;
209 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700210 case 'e':
211 params.ep = strtoull(optarg, &ptr, 16);
212 if (*ptr) {
213 fprintf(stderr, "%s: invalid entry point %s\n",
214 params.cmdname, optarg);
215 exit(EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000216 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700217 params.eflag = 1;
218 break;
Simon Glassafd728c2016-02-22 22:55:53 -0700219 case 'E':
220 params.external_data = true;
221 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700222 case 'f':
Simon Glass88e31cb2016-02-22 22:55:51 -0700223 datafile = optarg;
224 params.auto_its = !strcmp(datafile, "auto");
Heinrich Schuchardtddc38e62020-05-09 17:10:41 +0200225 /* fallthrough */
Simon Glasscf76cd22016-02-22 22:55:34 -0700226 case 'F':
227 /*
228 * The flattened image tree (FIT) format
229 * requires a flattened device tree image type
230 */
231 params.type = IH_TYPE_FLATDT;
232 params.fflag = 1;
233 break;
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600234 case 'G':
235 params.keyfile = optarg;
236 break;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100237 case 'i':
238 params.fit_ramdisk = optarg;
239 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700240 case 'k':
241 params.keydir = optarg;
242 break;
243 case 'K':
244 params.keydest = optarg;
245 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700246 case 'l':
247 params.lflag = 1;
248 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700249 case 'n':
250 params.imagename = optarg;
251 break;
George McCollister23d14892017-01-06 13:14:17 -0600252 case 'N':
253 params.engine_id = optarg;
254 break;
Jan Kiszka4043f322022-01-14 10:21:19 +0100255 case 'o':
256 params.algo_name = optarg;
257 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700258 case 'O':
259 params.os = genimg_get_os_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600260 if (params.os < 0) {
261 show_valid_options(IH_OS);
Simon Glass01168b32016-02-22 22:55:37 -0700262 usage("Invalid operating system");
Simon Glasseaa25402016-06-30 10:52:19 -0600263 }
Simon Glassf6fe5f62016-02-22 22:55:35 -0700264 break;
Teddy Reeda8457622016-06-09 19:38:02 -0700265 case 'p':
266 params.external_offset = strtoull(optarg, &ptr, 16);
267 if (*ptr) {
268 fprintf(stderr, "%s: invalid offset size %s\n",
269 params.cmdname, optarg);
270 exit(EXIT_FAILURE);
271 }
Teddy Reed9e3cd3c2016-07-11 22:54:26 -0700272 break;
Simon Glassda1805f2016-05-01 13:55:38 -0600273 case 'q':
274 params.quiet = 1;
275 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700276 case 'r':
277 params.require_keys = 1;
278 break;
279 case 'R':
280 /*
281 * This entry is for the second configuration
282 * file, if only one is not enough.
283 */
284 params.imagename2 = optarg;
285 break;
286 case 's':
287 params.skipcpy = 1;
288 break;
Simon Glass472ee0c2020-07-09 18:39:43 -0600289 case 't':
290 params.reset_timestamp = 1;
291 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700292 case 'T':
Baruch Siachf77ab202017-06-29 20:37:08 +0300293 if (strcmp(optarg, "list") == 0) {
294 show_valid_options(IH_TYPE);
295 exit(EXIT_SUCCESS);
296 }
Simon Glass04b46072016-02-22 22:55:48 -0700297 type = genimg_get_type_id(optarg);
298 if (type < 0) {
Simon Glassbe421592016-06-30 10:52:18 -0600299 show_valid_options(IH_TYPE);
Simon Glass01168b32016-02-22 22:55:37 -0700300 usage("Invalid image type");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700301 }
302 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700303 case 'v':
304 params.vflag++;
305 break;
306 case 'V':
307 printf("mkimage version %s\n", PLAIN_VERSION);
308 exit(EXIT_SUCCESS);
309 case 'x':
310 params.xflag++;
311 break;
312 default:
Simon Glass01168b32016-02-22 22:55:37 -0700313 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000314 }
wdenk5b1d7132002-11-03 00:07:02 +0000315 }
316
Andreas Bießmannddd34772016-05-03 15:17:03 +0200317 /* The last parameter is expected to be the imagefile */
318 if (optind < argc)
Andreas Bießmannf4001582016-05-01 03:01:27 +0200319 params.imagefile = argv[optind];
320
Simon Glass04b46072016-02-22 22:55:48 -0700321 /*
322 * For auto-generated FIT images we need to know the image type to put
323 * in the FIT, which is separate from the file's image type (which
324 * will always be IH_TYPE_FLATDT in this case).
325 */
326 if (params.type == IH_TYPE_FLATDT) {
Simon Glassa89857d2016-06-30 10:52:07 -0600327 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass156c3492016-06-30 10:52:08 -0600328 /* For auto_its, datafile is always 'auto' */
Simon Glass88e31cb2016-02-22 22:55:51 -0700329 if (!params.auto_its)
330 params.datafile = datafile;
Simon Glass79b5ee52016-06-30 10:52:09 -0600331 else if (!params.datafile)
332 usage("Missing data file for auto-FIT (use -d)");
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100333 } else if (params.lflag || type != IH_TYPE_INVALID) {
Alex Kiernan9d74c702018-04-22 05:11:17 +0000334 if (type == IH_TYPE_SCRIPT && !params.datafile)
335 usage("Missing data file for script (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700336 params.type = type;
337 }
338
339 if (!params.imagefile)
Simon Glass01168b32016-02-22 22:55:37 -0700340 usage("Missing output filename");
Simon Glass7106bd42016-02-22 22:55:33 -0700341}
342
Simon Glass7106bd42016-02-22 22:55:33 -0700343int main(int argc, char **argv)
344{
345 int ifd = -1;
346 struct stat sbuf;
347 char *ptr;
348 int retval = 0;
349 struct image_type_params *tparams = NULL;
350 int pad_len = 0;
351 int dfd;
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200352 size_t map_len;
Simon Glass7106bd42016-02-22 22:55:33 -0700353
354 params.cmdname = *argv;
355 params.addr = 0;
356 params.ep = 0;
357
358 process_args(argc, argv);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530359
360 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200361 tparams = imagetool_get_type(params.type);
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100362 if (tparams == NULL && !params.lflag) {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530363 fprintf (stderr, "%s: unsupported type %s\n",
364 params.cmdname, genimg_get_type_name(params.type));
365 exit (EXIT_FAILURE);
366 }
367
368 /*
369 * check the passed arguments parameters meets the requirements
370 * as per image type to be generated/listed
371 */
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100372 if (tparams && tparams->check_params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530373 if (tparams->check_params (&params))
Simon Glass01168b32016-02-22 22:55:37 -0700374 usage("Bad parameters for image type");
wdenk5b1d7132002-11-03 00:07:02 +0000375
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530376 if (!params.eflag) {
377 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000378 /* If XIP, entry point must be after the U-Boot header */
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100379 if (params.xflag && tparams)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530380 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000381 }
382
Peter Tyserb2f8b942009-11-24 16:42:10 -0600383 if (params.fflag){
Heinrich Schuchardt8b933b42022-03-01 08:53:56 +0100384 if (!tparams) {
385 fprintf(stderr, "%s: Missing FIT support\n",
386 params.cmdname);
387 exit (EXIT_FAILURE);
388 }
Peter Tyserb2f8b942009-11-24 16:42:10 -0600389 if (tparams->fflag_handle)
390 /*
391 * in some cases, some additional processing needs
392 * to be done if fflag is defined
393 *
394 * For ex. fit_handle_file for Fit file support
395 */
396 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000397
Peter Tyserb2f8b942009-11-24 16:42:10 -0600398 if (retval != EXIT_SUCCESS)
Heinrich Schuchardt8b933b42022-03-01 08:53:56 +0100399 usage("Bad parameters for FIT image type");
wdenk5b1d7132002-11-03 00:07:02 +0000400 }
401
Peter Tyserb2f8b942009-11-24 16:42:10 -0600402 if (params.lflag || params.fflag) {
403 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
404 } else {
405 ifd = open (params.imagefile,
406 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
407 }
408
409 if (ifd < 0) {
410 fprintf (stderr, "%s: Can't open %s: %s\n",
411 params.cmdname, params.imagefile,
412 strerror(errno));
413 exit (EXIT_FAILURE);
414 }
415
416 if (params.lflag || params.fflag) {
Yann Dirson774aed52021-05-18 10:59:04 +0200417 uint64_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000418 /*
419 * list header information of existing image
420 */
421 if (fstat(ifd, &sbuf) < 0) {
422 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530423 params.cmdname, params.imagefile,
424 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000425 exit (EXIT_FAILURE);
426 }
427
Yann Dirson774aed52021-05-18 10:59:04 +0200428 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
429#ifdef __linux__
430#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
431#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
432#endif
433 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
434 fprintf (stderr,
435 "%s: failed to get size of block device \"%s\"\n",
436 params.cmdname, params.imagefile);
437 exit (EXIT_FAILURE);
438 }
439#else
wdenk5b1d7132002-11-03 00:07:02 +0000440 fprintf (stderr,
Yann Dirson774aed52021-05-18 10:59:04 +0200441 "%s: \"%s\" is block device, don't know how to get its size\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530442 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000443 exit (EXIT_FAILURE);
Yann Dirson774aed52021-05-18 10:59:04 +0200444#endif
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100445 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
Yann Dirson774aed52021-05-18 10:59:04 +0200446 fprintf (stderr,
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100447 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
Yann Dirson774aed52021-05-18 10:59:04 +0200448 params.cmdname, params.imagefile,
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100449 (unsigned long long) sbuf.st_size,
450 tparams->header_size);
Yann Dirson774aed52021-05-18 10:59:04 +0200451 exit (EXIT_FAILURE);
452 } else {
453 size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000454 }
455
Yann Dirson774aed52021-05-18 10:59:04 +0200456 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400457 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000458 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530459 params.cmdname, params.imagefile,
460 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000461 exit (EXIT_FAILURE);
462 }
463
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100464 /*
465 * Verifies the header format based on the expected header for image
466 * type in tparams. If tparams is NULL simply check all image types
467 * to find one that matches our header.
468 */
469 retval = imagetool_verify_print_header(ptr, &sbuf, tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000470
wdenk5b1d7132002-11-03 00:07:02 +0000471 (void) munmap((void *)ptr, sbuf.st_size);
472 (void) close (ifd);
Simon Glasse4607262021-11-12 12:28:13 -0700473 if (!retval)
474 summary_show(&params.summary, params.imagefile,
475 params.keydest);
wdenk5b1d7132002-11-03 00:07:02 +0000476
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530477 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000478 }
479
Marek Vasut4cc86272015-12-07 18:01:54 +0100480 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert3edd0412015-12-04 00:11:23 +0200481 dfd = open(params.datafile, O_RDONLY | O_BINARY);
482 if (dfd < 0) {
483 fprintf(stderr, "%s: Can't open %s: %s\n",
484 params.cmdname, params.datafile,
485 strerror(errno));
486 exit(EXIT_FAILURE);
487 }
Simon Glass139651f2015-06-23 15:39:12 -0600488
Philippe De Swert3edd0412015-12-04 00:11:23 +0200489 if (fstat(dfd, &sbuf) < 0) {
490 fprintf(stderr, "%s: Can't stat %s: %s\n",
491 params.cmdname, params.datafile,
492 strerror(errno));
493 exit(EXIT_FAILURE);
494 }
Simon Glass139651f2015-06-23 15:39:12 -0600495
Philippe De Swert3edd0412015-12-04 00:11:23 +0200496 params.file_size = sbuf.st_size + tparams->header_size;
497 close(dfd);
498 }
Simon Glass139651f2015-06-23 15:39:12 -0600499
wdenk5b1d7132002-11-03 00:07:02 +0000500 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000501 * In case there an header with a variable
502 * length will be added, the corresponding
503 * function is called. This is responsible to
504 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000505 */
Stefano Babic4a963302011-09-15 23:50:16 +0000506 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200507 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000508 else
509 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000510
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530511 if (write(ifd, tparams->hdr, tparams->header_size)
512 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000513 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530514 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000515 exit (EXIT_FAILURE);
516 }
517
Christian Rieschc14f2812011-12-09 09:47:38 +0000518 if (!params.skipcpy) {
519 if (params.type == IH_TYPE_MULTI ||
520 params.type == IH_TYPE_SCRIPT) {
521 char *file = params.datafile;
522 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000523
Christian Rieschc14f2812011-12-09 09:47:38 +0000524 for (;;) {
525 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000526
Christian Rieschc14f2812011-12-09 09:47:38 +0000527 if (file) {
528 if ((sep = strchr(file, ':')) != NULL) {
529 *sep = '\0';
530 }
531
532 if (stat (file, &sbuf) < 0) {
533 fprintf (stderr, "%s: Can't stat %s: %s\n",
534 params.cmdname, file, strerror(errno));
535 exit (EXIT_FAILURE);
536 }
537 size = cpu_to_uimage (sbuf.st_size);
538 } else {
539 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000540 }
541
Christian Rieschc14f2812011-12-09 09:47:38 +0000542 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
543 fprintf (stderr, "%s: Write error on %s: %s\n",
544 params.cmdname, params.imagefile,
545 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000546 exit (EXIT_FAILURE);
547 }
wdenk5b1d7132002-11-03 00:07:02 +0000548
Christian Rieschc14f2812011-12-09 09:47:38 +0000549 if (!file) {
550 break;
551 }
wdenk5b1d7132002-11-03 00:07:02 +0000552
Christian Rieschc14f2812011-12-09 09:47:38 +0000553 if (sep) {
554 *sep = ':';
555 file = sep + 1;
556 } else {
557 file = NULL;
558 }
wdenk5b1d7132002-11-03 00:07:02 +0000559 }
wdenk5b1d7132002-11-03 00:07:02 +0000560
Christian Rieschc14f2812011-12-09 09:47:38 +0000561 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000562
Christian Rieschc14f2812011-12-09 09:47:38 +0000563 for (;;) {
564 char *sep = strchr(file, ':');
565 if (sep) {
566 *sep = '\0';
567 copy_file (ifd, file, 1);
568 *sep++ = ':';
569 file = sep;
570 } else {
571 copy_file (ifd, file, 0);
572 break;
573 }
wdenk5b1d7132002-11-03 00:07:02 +0000574 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000575 } else if (params.type == IH_TYPE_PBLIMAGE) {
576 /* PBL has special Image format, implements its' own */
577 pbl_load_uboot(ifd, &params);
Alexander Graf5329d672018-04-13 14:18:52 +0200578 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
579 /* Image file is meta, walk through actual targets */
580 int ret;
581
582 ret = zynqmpbif_copy_image(ifd, &params);
583 if (ret)
584 return ret;
Peng Fan60a56072018-10-16 04:50:30 +0000585 } else if (params.type == IH_TYPE_IMX8IMAGE) {
586 /* i.MX8/8X has special Image format */
587 int ret;
588
589 ret = imx8image_copy_image(ifd, &params);
590 if (ret)
591 return ret;
Peng Fan31c51da2018-11-20 10:19:36 +0000592 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
593 /* i.MX8M has special Image format */
594 int ret;
595
596 ret = imx8mimage_copy_image(ifd, &params);
597 if (ret)
598 return ret;
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800599 } else if ((params.type == IH_TYPE_RKSD) ||
600 (params.type == IH_TYPE_RKSPI)) {
601 /* Rockchip has special Image format */
602 int ret;
603
604 ret = rockchip_copy_image(ifd, &params);
605 if (ret)
606 return ret;
Christian Rieschc14f2812011-12-09 09:47:38 +0000607 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200608 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000609 }
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100610 if (params.type == IH_TYPE_FIRMWARE_IVT) {
611 /* Add alignment and IVT */
Kever Yang86f26012020-03-30 11:56:19 +0800612 uint32_t aligned_filesize = ALIGN(params.file_size,
613 0x1000);
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100614 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
615 params.addr, 0, 0, 0, params.addr
616 + aligned_filesize
617 - tparams->header_size,
618 params.addr + aligned_filesize
619 - tparams->header_size
620 + 0x20, 0 };
621 int i = params.file_size;
622 for (; i < aligned_filesize; i++) {
Sven Ebenfeld3bb71e02017-01-17 19:36:51 +0100623 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100624 fprintf(stderr,
625 "%s: Write error on %s: %s\n",
626 params.cmdname,
627 params.imagefile,
628 strerror(errno));
629 exit(EXIT_FAILURE);
630 }
631 }
632 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
633 != sizeof(flash_header_v2_t)) {
634 fprintf(stderr, "%s: Write error on %s: %s\n",
635 params.cmdname,
636 params.imagefile,
637 strerror(errno));
638 exit(EXIT_FAILURE);
639 }
640 }
wdenk5b1d7132002-11-03 00:07:02 +0000641 }
642
643 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530644#if defined(_POSIX_SYNCHRONIZED_IO) && \
645 !defined(__sun__) && \
646 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200647 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530648 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000649 (void) fdatasync (ifd);
650#else
651 (void) fsync (ifd);
652#endif
653
654 if (fstat(ifd, &sbuf) < 0) {
655 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530656 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000657 exit (EXIT_FAILURE);
658 }
Simon Glass139651f2015-06-23 15:39:12 -0600659 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000660
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200661 map_len = sbuf.st_size;
662 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400663 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000664 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530665 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000666 exit (EXIT_FAILURE);
667 }
668
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530669 /* Setup the image header as per input image type*/
670 if (tparams->set_header)
671 tparams->set_header (ptr, &sbuf, ifd, &params);
672 else {
673 fprintf (stderr, "%s: Can't set header for %s: %s\n",
674 params.cmdname, tparams->name, strerror(errno));
675 exit (EXIT_FAILURE);
676 }
wdenk5b1d7132002-11-03 00:07:02 +0000677
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530678 /* Print the image information by processing image header */
679 if (tparams->print_header)
680 tparams->print_header (ptr);
681 else {
Guillaume GARDETdfe25fa2018-03-30 10:28:19 +0200682 fprintf (stderr, "%s: Can't print header for %s\n",
683 params.cmdname, tparams->name);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530684 }
wdenk5b1d7132002-11-03 00:07:02 +0000685
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200686 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000687
688 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530689#if defined(_POSIX_SYNCHRONIZED_IO) && \
690 !defined(__sun__) && \
691 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200692 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530693 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000694 (void) fdatasync (ifd);
695#else
696 (void) fsync (ifd);
697#endif
698
699 if (close(ifd)) {
700 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530701 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000702 exit (EXIT_FAILURE);
703 }
704
705 exit (EXIT_SUCCESS);
706}
707
708static void
709copy_file (int ifd, const char *datafile, int pad)
710{
711 int dfd;
712 struct stat sbuf;
713 unsigned char *ptr;
714 int tail;
715 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200716 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000717 int offset = 0;
Mylène Josserand9e0e9a52020-07-08 11:52:50 +0200718 int size, ret;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200719 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000720
Stefano Babic6531bd92013-08-19 19:03:19 +0200721 memset(zeros, 0, sizeof(zeros));
722
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530723 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000724 fprintf (stderr, "Adding Image %s\n", datafile);
725 }
726
wdenk1ee550e2003-10-08 22:14:02 +0000727 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000728 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530729 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000730 exit (EXIT_FAILURE);
731 }
732
733 if (fstat(dfd, &sbuf) < 0) {
734 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530735 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000736 exit (EXIT_FAILURE);
737 }
738
Thomas Hebb13895a32021-08-01 15:23:13 -0700739 if (sbuf.st_size == 0) {
740 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
741 params.cmdname, datafile);
742 exit (EXIT_FAILURE);
743 }
744
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400745 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
746 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000747 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530748 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000749 exit (EXIT_FAILURE);
750 }
751
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530752 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000753 unsigned char *p = NULL;
754 /*
755 * XIP: do not append the image_header_t at the
756 * beginning of the file, but consume the space
757 * reserved for it.
758 */
759
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530760 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000761 fprintf (stderr,
762 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530763 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000764 exit (EXIT_FAILURE);
765 }
766
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530767 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000768 if ( *p != 0xff ) {
769 fprintf (stderr,
770 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530771 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000772 exit (EXIT_FAILURE);
773 }
774 }
775
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530776 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000777 }
778
779 size = sbuf.st_size - offset;
Mylène Josserand9e0e9a52020-07-08 11:52:50 +0200780
781 ret = write(ifd, ptr + offset, size);
782 if (ret != size) {
783 if (ret < 0)
784 fprintf (stderr, "%s: Write error on %s: %s\n",
785 params.cmdname, params.imagefile, strerror(errno));
786 else if (ret < size)
787 fprintf (stderr, "%s: Write only %d/%d bytes, "\
788 "probably no space left on the device\n",
789 params.cmdname, ret, size);
wdenk5b1d7132002-11-03 00:07:02 +0000790 exit (EXIT_FAILURE);
791 }
792
Stefano Babic6531bd92013-08-19 19:03:19 +0200793 tail = size % 4;
794 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000795
796 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
797 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530798 params.cmdname, params.imagefile,
799 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000800 exit (EXIT_FAILURE);
801 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200802 } else if (pad > 1) {
Simon Glass46409cc2015-08-30 16:55:22 -0600803 while (pad > 0) {
804 int todo = sizeof(zeros);
805
806 if (todo > pad)
807 todo = pad;
808 if (write(ifd, (char *)&zeros, todo) != todo) {
809 fprintf(stderr, "%s: Write error on %s: %s\n",
810 params.cmdname, params.imagefile,
811 strerror(errno));
812 exit(EXIT_FAILURE);
813 }
814 pad -= todo;
Stefano Babic6531bd92013-08-19 19:03:19 +0200815 }
wdenk5b1d7132002-11-03 00:07:02 +0000816 }
817
818 (void) munmap((void *)ptr, sbuf.st_size);
819 (void) close (dfd);
820}