blob: 0e1198b411325a685e60116e445fbe0158cd0d08 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00002/*
Bartlomiej Sieka9d254382008-03-11 12:34:47 +01003 * (C) Copyright 2008 Semihalf
4 *
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +05305 * (C) Copyright 2000-2009
wdenk5b1d7132002-11-03 00:07:02 +00006 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
wdenk5b1d7132002-11-03 00:07:02 +00008 */
9
Kever Yang29e7ab02020-03-30 11:56:19 +080010#include "imagetool.h"
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010011#include "mkimage.h"
Sven Ebenfeldd21bd692016-11-06 16:37:56 +010012#include "imximage.h"
Simon Glass2d2384b2021-11-12 12:28:13 -070013#include <fit_common.h>
wdenk5b1d7132002-11-03 00:07:02 +000014#include <image.h>
Wolfgang Denk976b38c2011-02-12 10:37:11 +010015#include <version.h>
Yann Dirson331f0802021-05-18 10:59:04 +020016#ifdef __linux__
17#include <sys/ioctl.h>
18#endif
wdenk5b1d7132002-11-03 00:07:02 +000019
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053020static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000021
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053022/* parameters initialized by core will be used by the image type code */
Simon Glasscc7a6442016-02-22 22:55:38 -070023static struct image_tool_params params = {
Prafulla Wadaskar89a4d6b2009-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 Denk04387d22010-03-27 23:37:46 +010029 .imagename = "",
Shaohui Xie5d898a02012-08-10 02:49:35 +000030 .imagename2 = "",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053031};
wdenk5b1d7132002-11-03 00:07:02 +000032
Simon Glass30664222016-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 Glassf24e1052016-06-30 10:52:18 -060045static int show_valid_options(enum ih_category category)
Simon Glass30664222016-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 Hayamaad5fb9f2020-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 Glass30664222016-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 Glass0cd82e22016-10-27 17:54:03 -060077 free(order);
Simon Glass30664222016-06-30 10:52:17 -060078
79 return 0;
80}
81
Simon Glass15310342016-02-22 22:55:37 -070082static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -070083{
Simon Glass15310342016-02-22 22:55:37 -070084 fprintf(stderr, "Error: %s\n", msg);
Pali Rohár11f29d42022-02-13 01:09:46 +010085 fprintf(stderr, "Usage: %s [-T type] -l image\n"
86 " -l ==> list image header information\n"
Sean Andersondeb26382022-04-08 16:08:39 -040087 " -T ==> parse image file as 'type'\n"
88 " -q ==> quiet\n",
Simon Glassb0a487a2016-02-22 22:55:36 -070089 params.cmdname);
90 fprintf(stderr,
91 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
92 " -A ==> set architecture to 'arch'\n"
93 " -O ==> set operating system to 'os'\n"
94 " -T ==> set image type to 'type'\n"
95 " -C ==> set compression type 'comp'\n"
96 " -a ==> set load address to 'addr' (hex)\n"
97 " -e ==> set entry point to 'ep' (hex)\n"
98 " -n ==> set image name to 'name'\n"
Sean Andersondeb26382022-04-08 16:08:39 -040099 " -R ==> set second image name to 'name'\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700100 " -d ==> use image data from 'datafile'\n"
Sean Andersondeb26382022-04-08 16:08:39 -0400101 " -x ==> set XIP (execute in place)\n"
102 " -s ==> create an image with no data\n"
103 " -v ==> verbose\n",
Simon Glassb0a487a2016-02-22 22:55:36 -0700104 params.cmdname);
105 fprintf(stderr,
Joel Stanley603e26f2020-12-08 14:42:15 +1030106 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-E] [-B size] [-i <ramdisk.cpio.gz>] fit-image\n"
Vagrant Cascadian82bd2f22016-10-23 20:45:17 -0700107 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glassb0a487a2016-02-22 22:55:36 -0700108 params.cmdname);
109 fprintf(stderr,
110 " -D => set all options for device tree compiler\n"
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100111 " -f => input filename for FIT source\n"
Joel Stanley603e26f2020-12-08 14:42:15 +1030112 " -i => input filename for ramdisk file\n"
113 " -E => place data outside of the FIT structure\n"
Sean Andersondeb26382022-04-08 16:08:39 -0400114 " -B => align size in hex for FIT structure and header\n"
115 " -b => append the device tree binary to the FIT\n"
116 " -t => update the timestamp in the FIT\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700117#ifdef CONFIG_FIT_SIGNATURE
118 fprintf(stderr,
Joel Stanley603e26f2020-12-08 14:42:15 +1030119 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700120 " -k => set directory containing private keys\n"
121 " -K => write public keys to this .dtb file\n"
Sean Anderson87b0af92022-05-16 16:11:08 -0400122 " -g => set key name hint\n"
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600123 " -G => use this signing key (in lieu of -k)\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700124 " -c => add comment in signature node\n"
125 " -F => re-sign existing FIT image\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700126 " -p => place external data at a static position\n"
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600127 " -r => mark keys used as 'required' in dtb\n"
Sean Andersondeb26382022-04-08 16:08:39 -0400128 " -N => openssl engine to use for signing\n"
129 " -o => algorithm to use for signing\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700130#else
131 fprintf(stderr,
132 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
133#endif
134 fprintf(stderr, " %s -V ==> print version information and exit\n",
135 params.cmdname);
Baruch Siach79aa33c2017-06-29 20:37:08 +0300136 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700137
138 exit(EXIT_FAILURE);
139}
140
Simon Glassfb4cce02016-02-22 22:55:52 -0700141static int add_content(int type, const char *fname)
142{
143 struct content_info *cont;
144
145 cont = calloc(1, sizeof(*cont));
146 if (!cont)
147 return -1;
148 cont->type = type;
149 cont->fname = fname;
150 if (params.content_tail)
151 params.content_tail->next = cont;
152 else
153 params.content_head = cont;
154 params.content_tail = cont;
155
156 return 0;
157}
158
Simon Glass0b443de2016-02-22 22:55:33 -0700159static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000160{
Peter Tysera2513e22010-04-04 22:36:03 -0500161 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700162 int type = IH_TYPE_INVALID;
163 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700164 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000165
Simon Glassa02221f2016-02-22 22:55:34 -0700166 while ((opt = getopt(argc, argv,
Sean Anderson87b0af92022-05-16 16:11:08 -0400167 "a:A:b:B:c:C:d:D:e:Ef:Fg:G:k:i:K:ln:N:p:o:O:rR:qstT:vVx")) != -1) {
Simon Glassa02221f2016-02-22 22:55:34 -0700168 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700169 case 'a':
170 params.addr = strtoull(optarg, &ptr, 16);
171 if (*ptr) {
172 fprintf(stderr, "%s: invalid load address %s\n",
173 params.cmdname, optarg);
174 exit(EXIT_FAILURE);
175 }
Simon Glassa02221f2016-02-22 22:55:34 -0700176 break;
177 case 'A':
178 params.arch = genimg_get_arch_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600179 if (params.arch < 0) {
180 show_valid_options(IH_ARCH);
Simon Glass15310342016-02-22 22:55:37 -0700181 usage("Invalid architecture");
Simon Glass51f03e62016-06-30 10:52:19 -0600182 }
Icenowy Zhengc2d08f02021-10-14 20:53:04 -0500183 params.Aflag = 1;
Simon Glassa02221f2016-02-22 22:55:34 -0700184 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700185 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200186 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200187 fprintf(stderr,
188 "%s: Out of memory adding content '%s'",
189 params.cmdname, optarg);
190 exit(EXIT_FAILURE);
191 }
Simon Glassfb4cce02016-02-22 22:55:52 -0700192 break;
Kever Yangebfe6112020-03-30 11:56:24 +0800193 case 'B':
194 params.bl_len = strtoull(optarg, &ptr, 16);
195 if (*ptr) {
196 fprintf(stderr, "%s: invalid block length %s\n",
197 params.cmdname, optarg);
198 exit(EXIT_FAILURE);
199 }
200
201 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700202 case 'c':
203 params.comment = optarg;
204 break;
205 case 'C':
206 params.comp = genimg_get_comp_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600207 if (params.comp < 0) {
208 show_valid_options(IH_COMP);
Simon Glass15310342016-02-22 22:55:37 -0700209 usage("Invalid compression type");
Simon Glass51f03e62016-06-30 10:52:19 -0600210 }
Simon Glassa02221f2016-02-22 22:55:34 -0700211 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700212 case 'd':
213 params.datafile = optarg;
214 params.dflag = 1;
215 break;
Simon Glass07450082016-02-22 22:55:35 -0700216 case 'D':
217 params.dtc = optarg;
218 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700219 case 'e':
220 params.ep = strtoull(optarg, &ptr, 16);
221 if (*ptr) {
222 fprintf(stderr, "%s: invalid entry point %s\n",
223 params.cmdname, optarg);
224 exit(EXIT_FAILURE);
225 }
226 params.eflag = 1;
227 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700228 case 'E':
229 params.external_data = true;
230 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700231 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700232 datafile = optarg;
233 params.auto_its = !strcmp(datafile, "auto");
Heinrich Schuchardta29162a2020-05-09 17:10:41 +0200234 /* fallthrough */
Simon Glassa02221f2016-02-22 22:55:34 -0700235 case 'F':
236 /*
237 * The flattened image tree (FIT) format
238 * requires a flattened device tree image type
239 */
240 params.type = IH_TYPE_FLATDT;
241 params.fflag = 1;
242 break;
Sean Anderson87b0af92022-05-16 16:11:08 -0400243 case 'g':
244 params.keyname = optarg;
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -0600245 case 'G':
246 params.keyfile = optarg;
247 break;
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100248 case 'i':
249 params.fit_ramdisk = optarg;
250 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700251 case 'k':
252 params.keydir = optarg;
253 break;
254 case 'K':
255 params.keydest = optarg;
256 break;
Simon Glass07450082016-02-22 22:55:35 -0700257 case 'l':
258 params.lflag = 1;
259 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700260 case 'n':
261 params.imagename = optarg;
262 break;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600263 case 'N':
264 params.engine_id = optarg;
265 break;
Jan Kiszka5902a392022-01-14 10:21:19 +0100266 case 'o':
267 params.algo_name = optarg;
268 break;
Simon Glass07450082016-02-22 22:55:35 -0700269 case 'O':
270 params.os = genimg_get_os_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600271 if (params.os < 0) {
272 show_valid_options(IH_OS);
Simon Glass15310342016-02-22 22:55:37 -0700273 usage("Invalid operating system");
Simon Glass51f03e62016-06-30 10:52:19 -0600274 }
Simon Glass07450082016-02-22 22:55:35 -0700275 break;
Teddy Reedf8f91072016-06-09 19:38:02 -0700276 case 'p':
277 params.external_offset = strtoull(optarg, &ptr, 16);
278 if (*ptr) {
279 fprintf(stderr, "%s: invalid offset size %s\n",
280 params.cmdname, optarg);
281 exit(EXIT_FAILURE);
282 }
Teddy Reedb6fefa72016-07-11 22:54:26 -0700283 break;
Simon Glassbd6e1422016-05-01 13:55:38 -0600284 case 'q':
285 params.quiet = 1;
286 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700287 case 'r':
288 params.require_keys = 1;
289 break;
290 case 'R':
291 /*
292 * This entry is for the second configuration
293 * file, if only one is not enough.
294 */
295 params.imagename2 = optarg;
296 break;
297 case 's':
298 params.skipcpy = 1;
299 break;
Simon Glass152b2462020-07-09 18:39:43 -0600300 case 't':
301 params.reset_timestamp = 1;
302 break;
Simon Glass07450082016-02-22 22:55:35 -0700303 case 'T':
Baruch Siach79aa33c2017-06-29 20:37:08 +0300304 if (strcmp(optarg, "list") == 0) {
305 show_valid_options(IH_TYPE);
306 exit(EXIT_SUCCESS);
307 }
Simon Glassd505a092016-02-22 22:55:48 -0700308 type = genimg_get_type_id(optarg);
309 if (type < 0) {
Simon Glassf24e1052016-06-30 10:52:18 -0600310 show_valid_options(IH_TYPE);
Simon Glass15310342016-02-22 22:55:37 -0700311 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700312 }
313 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700314 case 'v':
315 params.vflag++;
316 break;
317 case 'V':
318 printf("mkimage version %s\n", PLAIN_VERSION);
319 exit(EXIT_SUCCESS);
320 case 'x':
321 params.xflag++;
322 break;
323 default:
Simon Glass15310342016-02-22 22:55:37 -0700324 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000325 }
wdenk5b1d7132002-11-03 00:07:02 +0000326 }
327
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200328 /* The last parameter is expected to be the imagefile */
329 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200330 params.imagefile = argv[optind];
331
Simon Glassd505a092016-02-22 22:55:48 -0700332 /*
333 * For auto-generated FIT images we need to know the image type to put
334 * in the FIT, which is separate from the file's image type (which
335 * will always be IH_TYPE_FLATDT in this case).
336 */
337 if (params.type == IH_TYPE_FLATDT) {
Simon Glass20deadd2016-06-30 10:52:07 -0600338 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600339 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700340 if (!params.auto_its)
341 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600342 else if (!params.datafile)
343 usage("Missing data file for auto-FIT (use -d)");
Pali Rohár11f29d42022-02-13 01:09:46 +0100344 } else if (params.lflag || type != IH_TYPE_INVALID) {
Alex Kiernan8c842872018-04-22 05:11:17 +0000345 if (type == IH_TYPE_SCRIPT && !params.datafile)
346 usage("Missing data file for script (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700347 params.type = type;
348 }
349
350 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700351 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700352}
353
Pali Rohárd3b1ca22022-01-14 18:34:43 +0100354static void verify_image(const struct image_type_params *tparams)
355{
356 struct stat sbuf;
357 void *ptr;
358 int ifd;
359
360 ifd = open(params.imagefile, O_RDONLY | O_BINARY);
361 if (ifd < 0) {
362 fprintf(stderr, "%s: Can't open %s: %s\n",
363 params.cmdname, params.imagefile,
364 strerror(errno));
365 exit(EXIT_FAILURE);
366 }
367
368 if (fstat(ifd, &sbuf) < 0) {
369 fprintf(stderr, "%s: Can't stat %s: %s\n",
370 params.cmdname, params.imagefile, strerror(errno));
371 exit(EXIT_FAILURE);
372 }
373 params.file_size = sbuf.st_size;
374
375 ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0);
376 if (ptr == MAP_FAILED) {
377 fprintf(stderr, "%s: Can't map %s: %s\n",
378 params.cmdname, params.imagefile, strerror(errno));
379 exit(EXIT_FAILURE);
380 }
381
382 if (tparams->verify_header((unsigned char *)ptr, params.file_size, &params) != 0) {
383 fprintf(stderr, "%s: Failed to verify header of %s\n",
384 params.cmdname, params.imagefile);
385 exit(EXIT_FAILURE);
386 }
387
388 (void)munmap(ptr, params.file_size);
389 (void)close(ifd);
390}
391
Simon Glass0b443de2016-02-22 22:55:33 -0700392int main(int argc, char **argv)
393{
394 int ifd = -1;
395 struct stat sbuf;
396 char *ptr;
397 int retval = 0;
398 struct image_type_params *tparams = NULL;
399 int pad_len = 0;
400 int dfd;
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200401 size_t map_len;
Simon Glass0b443de2016-02-22 22:55:33 -0700402
403 params.cmdname = *argv;
404 params.addr = 0;
405 params.ep = 0;
406
407 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000408
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530409 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200410 tparams = imagetool_get_type(params.type);
Pali Rohár11f29d42022-02-13 01:09:46 +0100411 if (tparams == NULL && !params.lflag) {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530412 fprintf (stderr, "%s: unsupported type %s\n",
413 params.cmdname, genimg_get_type_name(params.type));
414 exit (EXIT_FAILURE);
415 }
416
417 /*
418 * check the passed arguments parameters meets the requirements
419 * as per image type to be generated/listed
420 */
Pali Rohár11f29d42022-02-13 01:09:46 +0100421 if (tparams && tparams->check_params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530422 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700423 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530424
425 if (!params.eflag) {
426 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000427 /* If XIP, entry point must be after the U-Boot header */
Pali Rohár11f29d42022-02-13 01:09:46 +0100428 if (params.xflag && tparams)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530429 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000430 }
431
Peter Tyserc81296c2009-11-24 16:42:10 -0600432 if (params.fflag){
Heinrich Schuchardt5017f9b2022-03-01 08:53:56 +0100433 if (!tparams) {
434 fprintf(stderr, "%s: Missing FIT support\n",
435 params.cmdname);
436 exit (EXIT_FAILURE);
437 }
Peter Tyserc81296c2009-11-24 16:42:10 -0600438 if (tparams->fflag_handle)
439 /*
440 * in some cases, some additional processing needs
441 * to be done if fflag is defined
442 *
443 * For ex. fit_handle_file for Fit file support
444 */
445 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000446
Peter Tyserc81296c2009-11-24 16:42:10 -0600447 if (retval != EXIT_SUCCESS)
Heinrich Schuchardt5017f9b2022-03-01 08:53:56 +0100448 usage("Bad parameters for FIT image type");
wdenk5b1d7132002-11-03 00:07:02 +0000449 }
450
Peter Tyserc81296c2009-11-24 16:42:10 -0600451 if (params.lflag || params.fflag) {
452 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
453 } else {
454 ifd = open (params.imagefile,
455 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
456 }
457
458 if (ifd < 0) {
459 fprintf (stderr, "%s: Can't open %s: %s\n",
460 params.cmdname, params.imagefile,
461 strerror(errno));
462 exit (EXIT_FAILURE);
463 }
464
465 if (params.lflag || params.fflag) {
Yann Dirson331f0802021-05-18 10:59:04 +0200466 uint64_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000467 /*
468 * list header information of existing image
469 */
470 if (fstat(ifd, &sbuf) < 0) {
471 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530472 params.cmdname, params.imagefile,
473 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000474 exit (EXIT_FAILURE);
475 }
476
Yann Dirson331f0802021-05-18 10:59:04 +0200477 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
478#ifdef __linux__
479#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
480#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
481#endif
482 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
483 fprintf (stderr,
484 "%s: failed to get size of block device \"%s\"\n",
485 params.cmdname, params.imagefile);
486 exit (EXIT_FAILURE);
487 }
488#else
wdenk5b1d7132002-11-03 00:07:02 +0000489 fprintf (stderr,
Yann Dirson331f0802021-05-18 10:59:04 +0200490 "%s: \"%s\" is block device, don't know how to get its size\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530491 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000492 exit (EXIT_FAILURE);
Yann Dirson331f0802021-05-18 10:59:04 +0200493#endif
Pali Rohár11f29d42022-02-13 01:09:46 +0100494 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
Yann Dirson331f0802021-05-18 10:59:04 +0200495 fprintf (stderr,
Heinrich Schuchardtc28f2492022-01-15 20:12:56 +0100496 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
Yann Dirson331f0802021-05-18 10:59:04 +0200497 params.cmdname, params.imagefile,
Heinrich Schuchardtc28f2492022-01-15 20:12:56 +0100498 (unsigned long long) sbuf.st_size,
499 tparams->header_size);
Yann Dirson331f0802021-05-18 10:59:04 +0200500 exit (EXIT_FAILURE);
501 } else {
502 size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000503 }
504
Yann Dirson331f0802021-05-18 10:59:04 +0200505 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400506 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000507 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530508 params.cmdname, params.imagefile,
509 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000510 exit (EXIT_FAILURE);
511 }
512
Pali Rohár11f29d42022-02-13 01:09:46 +0100513 /*
514 * Verifies the header format based on the expected header for image
515 * type in tparams. If tparams is NULL simply check all image types
516 * to find one that matches our header.
517 */
518 retval = imagetool_verify_print_header(ptr, &sbuf, tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000519
wdenk5b1d7132002-11-03 00:07:02 +0000520 (void) munmap((void *)ptr, sbuf.st_size);
521 (void) close (ifd);
Simon Glass2d2384b2021-11-12 12:28:13 -0700522 if (!retval)
523 summary_show(&params.summary, params.imagefile,
524 params.keydest);
wdenk5b1d7132002-11-03 00:07:02 +0000525
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530526 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000527 }
528
Marek Vasut34633142015-12-07 18:01:54 +0100529 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200530 dfd = open(params.datafile, O_RDONLY | O_BINARY);
531 if (dfd < 0) {
532 fprintf(stderr, "%s: Can't open %s: %s\n",
533 params.cmdname, params.datafile,
534 strerror(errno));
535 exit(EXIT_FAILURE);
536 }
Simon Glass92a655c2015-06-23 15:39:12 -0600537
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200538 if (fstat(dfd, &sbuf) < 0) {
539 fprintf(stderr, "%s: Can't stat %s: %s\n",
540 params.cmdname, params.datafile,
541 strerror(errno));
542 exit(EXIT_FAILURE);
543 }
Simon Glass92a655c2015-06-23 15:39:12 -0600544
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200545 params.file_size = sbuf.st_size + tparams->header_size;
546 close(dfd);
547 }
Simon Glass92a655c2015-06-23 15:39:12 -0600548
wdenk5b1d7132002-11-03 00:07:02 +0000549 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000550 * In case there an header with a variable
551 * length will be added, the corresponding
552 * function is called. This is responsible to
553 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000554 */
Stefano Babicf0662102011-09-15 23:50:16 +0000555 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200556 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000557 else
558 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000559
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530560 if (write(ifd, tparams->hdr, tparams->header_size)
561 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000562 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530563 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000564 exit (EXIT_FAILURE);
565 }
566
Christian Rieschd1be8f92011-12-09 09:47:38 +0000567 if (!params.skipcpy) {
568 if (params.type == IH_TYPE_MULTI ||
569 params.type == IH_TYPE_SCRIPT) {
570 char *file = params.datafile;
571 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000572
Christian Rieschd1be8f92011-12-09 09:47:38 +0000573 for (;;) {
574 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000575
Christian Rieschd1be8f92011-12-09 09:47:38 +0000576 if (file) {
577 if ((sep = strchr(file, ':')) != NULL) {
578 *sep = '\0';
579 }
580
581 if (stat (file, &sbuf) < 0) {
582 fprintf (stderr, "%s: Can't stat %s: %s\n",
583 params.cmdname, file, strerror(errno));
584 exit (EXIT_FAILURE);
585 }
586 size = cpu_to_uimage (sbuf.st_size);
587 } else {
588 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000589 }
590
Christian Rieschd1be8f92011-12-09 09:47:38 +0000591 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
592 fprintf (stderr, "%s: Write error on %s: %s\n",
593 params.cmdname, params.imagefile,
594 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000595 exit (EXIT_FAILURE);
596 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000597
598 if (!file) {
599 break;
600 }
601
602 if (sep) {
603 *sep = ':';
604 file = sep + 1;
605 } else {
606 file = NULL;
607 }
wdenk5b1d7132002-11-03 00:07:02 +0000608 }
609
Christian Rieschd1be8f92011-12-09 09:47:38 +0000610 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000611
Christian Rieschd1be8f92011-12-09 09:47:38 +0000612 for (;;) {
613 char *sep = strchr(file, ':');
614 if (sep) {
615 *sep = '\0';
616 copy_file (ifd, file, 1);
617 *sep++ = ':';
618 file = sep;
619 } else {
620 copy_file (ifd, file, 0);
621 break;
622 }
wdenk5b1d7132002-11-03 00:07:02 +0000623 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000624 } else if (params.type == IH_TYPE_PBLIMAGE) {
625 /* PBL has special Image format, implements its' own */
626 pbl_load_uboot(ifd, &params);
Alexander Graf6915dcf2018-04-13 14:18:52 +0200627 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
628 /* Image file is meta, walk through actual targets */
629 int ret;
630
631 ret = zynqmpbif_copy_image(ifd, &params);
632 if (ret)
633 return ret;
Peng Fana2b96ec2018-10-16 04:50:30 +0000634 } else if (params.type == IH_TYPE_IMX8IMAGE) {
635 /* i.MX8/8X has special Image format */
636 int ret;
637
638 ret = imx8image_copy_image(ifd, &params);
639 if (ret)
640 return ret;
Peng Fan6609c262018-11-20 10:19:36 +0000641 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
642 /* i.MX8M has special Image format */
643 int ret;
644
645 ret = imx8mimage_copy_image(ifd, &params);
646 if (ret)
647 return ret;
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800648 } else if ((params.type == IH_TYPE_RKSD) ||
649 (params.type == IH_TYPE_RKSPI)) {
650 /* Rockchip has special Image format */
651 int ret;
652
653 ret = rockchip_copy_image(ifd, &params);
654 if (ret)
655 return ret;
Christian Rieschd1be8f92011-12-09 09:47:38 +0000656 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200657 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000658 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100659 if (params.type == IH_TYPE_FIRMWARE_IVT) {
660 /* Add alignment and IVT */
Kever Yang29e7ab02020-03-30 11:56:19 +0800661 uint32_t aligned_filesize = ALIGN(params.file_size,
662 0x1000);
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100663 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
664 params.addr, 0, 0, 0, params.addr
665 + aligned_filesize
666 - tparams->header_size,
667 params.addr + aligned_filesize
668 - tparams->header_size
669 + 0x20, 0 };
670 int i = params.file_size;
671 for (; i < aligned_filesize; i++) {
Sven Ebenfeldb4e923a2017-01-17 19:36:51 +0100672 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100673 fprintf(stderr,
674 "%s: Write error on %s: %s\n",
675 params.cmdname,
676 params.imagefile,
677 strerror(errno));
678 exit(EXIT_FAILURE);
679 }
680 }
681 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
682 != sizeof(flash_header_v2_t)) {
683 fprintf(stderr, "%s: Write error on %s: %s\n",
684 params.cmdname,
685 params.imagefile,
686 strerror(errno));
687 exit(EXIT_FAILURE);
688 }
689 }
wdenk5b1d7132002-11-03 00:07:02 +0000690 }
691
692 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530693#if defined(_POSIX_SYNCHRONIZED_IO) && \
694 !defined(__sun__) && \
695 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200696 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530697 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000698 (void) fdatasync (ifd);
699#else
700 (void) fsync (ifd);
701#endif
702
703 if (fstat(ifd, &sbuf) < 0) {
704 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530705 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000706 exit (EXIT_FAILURE);
707 }
Simon Glass92a655c2015-06-23 15:39:12 -0600708 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000709
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200710 map_len = sbuf.st_size;
711 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400712 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000713 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530714 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000715 exit (EXIT_FAILURE);
716 }
717
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530718 /* Setup the image header as per input image type*/
719 if (tparams->set_header)
720 tparams->set_header (ptr, &sbuf, ifd, &params);
721 else {
722 fprintf (stderr, "%s: Can't set header for %s: %s\n",
723 params.cmdname, tparams->name, strerror(errno));
724 exit (EXIT_FAILURE);
725 }
wdenk5b1d7132002-11-03 00:07:02 +0000726
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530727 /* Print the image information by processing image header */
728 if (tparams->print_header)
729 tparams->print_header (ptr);
730 else {
Guillaume GARDET004d0092018-03-30 10:28:19 +0200731 fprintf (stderr, "%s: Can't print header for %s\n",
732 params.cmdname, tparams->name);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530733 }
wdenk5b1d7132002-11-03 00:07:02 +0000734
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200735 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000736
737 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530738#if defined(_POSIX_SYNCHRONIZED_IO) && \
739 !defined(__sun__) && \
740 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200741 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530742 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000743 (void) fdatasync (ifd);
744#else
745 (void) fsync (ifd);
746#endif
747
748 if (close(ifd)) {
749 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530750 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000751 exit (EXIT_FAILURE);
752 }
753
Pali Rohárd3b1ca22022-01-14 18:34:43 +0100754 if (tparams->verify_header)
755 verify_image(tparams);
756
wdenk5b1d7132002-11-03 00:07:02 +0000757 exit (EXIT_SUCCESS);
758}
759
760static void
761copy_file (int ifd, const char *datafile, int pad)
762{
763 int dfd;
764 struct stat sbuf;
765 unsigned char *ptr;
766 int tail;
767 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200768 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000769 int offset = 0;
Mylène Josseranddd85dc52020-07-08 11:52:50 +0200770 int size, ret;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200771 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000772
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200773 memset(zeros, 0, sizeof(zeros));
774
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530775 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000776 fprintf (stderr, "Adding Image %s\n", datafile);
777 }
778
wdenkef1464c2003-10-08 22:14:02 +0000779 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000780 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530781 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000782 exit (EXIT_FAILURE);
783 }
784
785 if (fstat(dfd, &sbuf) < 0) {
786 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530787 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000788 exit (EXIT_FAILURE);
789 }
790
Thomas Hebbeaa64422021-08-01 15:23:13 -0700791 if (sbuf.st_size == 0) {
792 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
793 params.cmdname, datafile);
794 exit (EXIT_FAILURE);
795 }
796
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400797 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
798 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000799 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530800 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000801 exit (EXIT_FAILURE);
802 }
803
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530804 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000805 unsigned char *p = NULL;
806 /*
807 * XIP: do not append the image_header_t at the
808 * beginning of the file, but consume the space
809 * reserved for it.
810 */
811
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530812 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000813 fprintf (stderr,
814 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530815 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000816 exit (EXIT_FAILURE);
817 }
818
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530819 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000820 if ( *p != 0xff ) {
821 fprintf (stderr,
822 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530823 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000824 exit (EXIT_FAILURE);
825 }
826 }
827
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530828 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000829 }
830
831 size = sbuf.st_size - offset;
Mylène Josseranddd85dc52020-07-08 11:52:50 +0200832
833 ret = write(ifd, ptr + offset, size);
834 if (ret != size) {
835 if (ret < 0)
836 fprintf (stderr, "%s: Write error on %s: %s\n",
837 params.cmdname, params.imagefile, strerror(errno));
838 else if (ret < size)
839 fprintf (stderr, "%s: Write only %d/%d bytes, "\
840 "probably no space left on the device\n",
841 params.cmdname, ret, size);
wdenk5b1d7132002-11-03 00:07:02 +0000842 exit (EXIT_FAILURE);
843 }
844
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200845 tail = size % 4;
846 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000847
848 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
849 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530850 params.cmdname, params.imagefile,
851 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000852 exit (EXIT_FAILURE);
853 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200854 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600855 while (pad > 0) {
856 int todo = sizeof(zeros);
857
858 if (todo > pad)
859 todo = pad;
860 if (write(ifd, (char *)&zeros, todo) != todo) {
861 fprintf(stderr, "%s: Write error on %s: %s\n",
862 params.cmdname, params.imagefile,
863 strerror(errno));
864 exit(EXIT_FAILURE);
865 }
866 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200867 }
wdenk5b1d7132002-11-03 00:07:02 +0000868 }
869
870 (void) munmap((void *)ptr, sbuf.st_size);
871 (void) close (dfd);
872}