blob: 0e1198b411325a685e60116e445fbe0158cd0d08 [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"
Sean Anderson138d2712022-04-08 16:08:39 -040087 " -T ==> parse image file as 'type'\n"
88 " -q ==> quiet\n",
Simon Glass99a7c262016-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 Anderson138d2712022-04-08 16:08:39 -040099 " -R ==> set second image name to 'name'\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700100 " -d ==> use image data from 'datafile'\n"
Sean Anderson138d2712022-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 Glass99a7c262016-02-22 22:55:36 -0700104 params.cmdname);
105 fprintf(stderr,
Joel Stanley00dffdd2020-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 Cascadian3e9e6d62016-10-23 20:45:17 -0700107 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glass99a7c262016-02-22 22:55:36 -0700108 params.cmdname);
109 fprintf(stderr,
110 " -D => set all options for device tree compiler\n"
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100111 " -f => input filename for FIT source\n"
Joel Stanley00dffdd2020-12-08 14:42:15 +1030112 " -i => input filename for ramdisk file\n"
113 " -E => place data outside of the FIT structure\n"
Sean Anderson138d2712022-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 Glass99a7c262016-02-22 22:55:36 -0700117#ifdef CONFIG_FIT_SIGNATURE
118 fprintf(stderr,
Joel Stanley00dffdd2020-12-08 14:42:15 +1030119 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700120 " -k => set directory containing private keys\n"
121 " -K => write public keys to this .dtb file\n"
Sean Anderson5f150292022-05-16 16:11:08 -0400122 " -g => set key name hint\n"
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600123 " -G => use this signing key (in lieu of -k)\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700124 " -c => add comment in signature node\n"
125 " -F => re-sign existing FIT image\n"
Teddy Reeda8457622016-06-09 19:38:02 -0700126 " -p => place external data at a static position\n"
George McCollister23d14892017-01-06 13:14:17 -0600127 " -r => mark keys used as 'required' in dtb\n"
Sean Anderson138d2712022-04-08 16:08:39 -0400128 " -N => openssl engine to use for signing\n"
129 " -o => algorithm to use for signing\n");
Simon Glass99a7c262016-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 Siachf77ab202017-06-29 20:37:08 +0300136 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700137
138 exit(EXIT_FAILURE);
139}
140
Simon Glassbd8bc5d2016-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 Glass7106bd42016-02-22 22:55:33 -0700159static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000160{
Peter Tyser632d9ca2010-04-04 22:36:03 -0500161 char *ptr;
Simon Glass04b46072016-02-22 22:55:48 -0700162 int type = IH_TYPE_INVALID;
163 char *datafile = NULL;
Simon Glasscf76cd22016-02-22 22:55:34 -0700164 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000165
Simon Glasscf76cd22016-02-22 22:55:34 -0700166 while ((opt = getopt(argc, argv,
Sean Anderson5f150292022-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 Glasscf76cd22016-02-22 22:55:34 -0700168 switch (opt) {
Simon Glassf6fe5f62016-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 Glasscf76cd22016-02-22 22:55:34 -0700176 break;
177 case 'A':
178 params.arch = genimg_get_arch_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600179 if (params.arch < 0) {
180 show_valid_options(IH_ARCH);
Simon Glass01168b32016-02-22 22:55:37 -0700181 usage("Invalid architecture");
Simon Glasseaa25402016-06-30 10:52:19 -0600182 }
Icenowy Zheng4369c972021-10-14 20:53:04 -0500183 params.Aflag = 1;
Simon Glasscf76cd22016-02-22 22:55:34 -0700184 break;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700185 case 'b':
Andreas Bießmannddd34772016-05-03 15:17:03 +0200186 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmannf4001582016-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 Glassbd8bc5d2016-02-22 22:55:52 -0700192 break;
Kever Yang8e238b52020-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 Glasscf76cd22016-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 Glasseaa25402016-06-30 10:52:19 -0600207 if (params.comp < 0) {
208 show_valid_options(IH_COMP);
Simon Glass01168b32016-02-22 22:55:37 -0700209 usage("Invalid compression type");
Simon Glasseaa25402016-06-30 10:52:19 -0600210 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700211 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700212 case 'd':
213 params.datafile = optarg;
214 params.dflag = 1;
215 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700216 case 'D':
217 params.dtc = optarg;
218 break;
Simon Glasscf76cd22016-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);
wdenk5b1d7132002-11-03 00:07:02 +0000225 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700226 params.eflag = 1;
227 break;
Simon Glassafd728c2016-02-22 22:55:53 -0700228 case 'E':
229 params.external_data = true;
230 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700231 case 'f':
Simon Glass88e31cb2016-02-22 22:55:51 -0700232 datafile = optarg;
233 params.auto_its = !strcmp(datafile, "auto");
Heinrich Schuchardtddc38e62020-05-09 17:10:41 +0200234 /* fallthrough */
Simon Glasscf76cd22016-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 Anderson5f150292022-05-16 16:11:08 -0400243 case 'g':
244 params.keyname = optarg;
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600245 case 'G':
246 params.keyfile = optarg;
247 break;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100248 case 'i':
249 params.fit_ramdisk = optarg;
250 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700251 case 'k':
252 params.keydir = optarg;
253 break;
254 case 'K':
255 params.keydest = optarg;
256 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700257 case 'l':
258 params.lflag = 1;
259 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700260 case 'n':
261 params.imagename = optarg;
262 break;
George McCollister23d14892017-01-06 13:14:17 -0600263 case 'N':
264 params.engine_id = optarg;
265 break;
Jan Kiszka4043f322022-01-14 10:21:19 +0100266 case 'o':
267 params.algo_name = optarg;
268 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700269 case 'O':
270 params.os = genimg_get_os_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600271 if (params.os < 0) {
272 show_valid_options(IH_OS);
Simon Glass01168b32016-02-22 22:55:37 -0700273 usage("Invalid operating system");
Simon Glasseaa25402016-06-30 10:52:19 -0600274 }
Simon Glassf6fe5f62016-02-22 22:55:35 -0700275 break;
Teddy Reeda8457622016-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 Reed9e3cd3c2016-07-11 22:54:26 -0700283 break;
Simon Glassda1805f2016-05-01 13:55:38 -0600284 case 'q':
285 params.quiet = 1;
286 break;
Simon Glasscf76cd22016-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 Glass472ee0c2020-07-09 18:39:43 -0600300 case 't':
301 params.reset_timestamp = 1;
302 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700303 case 'T':
Baruch Siachf77ab202017-06-29 20:37:08 +0300304 if (strcmp(optarg, "list") == 0) {
305 show_valid_options(IH_TYPE);
306 exit(EXIT_SUCCESS);
307 }
Simon Glass04b46072016-02-22 22:55:48 -0700308 type = genimg_get_type_id(optarg);
309 if (type < 0) {
Simon Glassbe421592016-06-30 10:52:18 -0600310 show_valid_options(IH_TYPE);
Simon Glass01168b32016-02-22 22:55:37 -0700311 usage("Invalid image type");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700312 }
313 break;
Simon Glasscf76cd22016-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 Glass01168b32016-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ßmannddd34772016-05-03 15:17:03 +0200328 /* The last parameter is expected to be the imagefile */
329 if (optind < argc)
Andreas Bießmannf4001582016-05-01 03:01:27 +0200330 params.imagefile = argv[optind];
331
Simon Glass04b46072016-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 Glassa89857d2016-06-30 10:52:07 -0600338 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass156c3492016-06-30 10:52:08 -0600339 /* For auto_its, datafile is always 'auto' */
Simon Glass88e31cb2016-02-22 22:55:51 -0700340 if (!params.auto_its)
341 params.datafile = datafile;
Simon Glass79b5ee52016-06-30 10:52:09 -0600342 else if (!params.datafile)
343 usage("Missing data file for auto-FIT (use -d)");
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100344 } else if (params.lflag || type != IH_TYPE_INVALID) {
Alex Kiernan9d74c702018-04-22 05:11:17 +0000345 if (type == IH_TYPE_SCRIPT && !params.datafile)
346 usage("Missing data file for script (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700347 params.type = type;
348 }
349
350 if (!params.imagefile)
Simon Glass01168b32016-02-22 22:55:37 -0700351 usage("Missing output filename");
Simon Glass7106bd42016-02-22 22:55:33 -0700352}
353
Pali Rohára55eaa52022-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 Glass7106bd42016-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 Tomlinsonfc497862018-08-29 10:51:14 +1200401 size_t map_len;
Simon Glass7106bd42016-02-22 22:55:33 -0700402
403 params.cmdname = *argv;
404 params.addr = 0;
405 params.ep = 0;
406
407 process_args(argc, argv);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530408
409 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200410 tparams = imagetool_get_type(params.type);
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100411 if (tparams == NULL && !params.lflag) {
Prafulla Wadaskarfabf3cf2009-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árcdf0d1c2022-02-13 01:09:46 +0100421 if (tparams && tparams->check_params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530422 if (tparams->check_params (&params))
Simon Glass01168b32016-02-22 22:55:37 -0700423 usage("Bad parameters for image type");
wdenk5b1d7132002-11-03 00:07:02 +0000424
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530425 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árcdf0d1c2022-02-13 01:09:46 +0100428 if (params.xflag && tparams)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530429 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000430 }
431
Peter Tyserb2f8b942009-11-24 16:42:10 -0600432 if (params.fflag){
Heinrich Schuchardt8b933b42022-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 Tyserb2f8b942009-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 Tyserb2f8b942009-11-24 16:42:10 -0600447 if (retval != EXIT_SUCCESS)
Heinrich Schuchardt8b933b42022-03-01 08:53:56 +0100448 usage("Bad parameters for FIT image type");
wdenk5b1d7132002-11-03 00:07:02 +0000449 }
450
Peter Tyserb2f8b942009-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 Dirson774aed52021-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 Wadaskarfabf3cf2009-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 Dirson774aed52021-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 Dirson774aed52021-05-18 10:59:04 +0200490 "%s: \"%s\" is block device, don't know how to get its size\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530491 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000492 exit (EXIT_FAILURE);
Yann Dirson774aed52021-05-18 10:59:04 +0200493#endif
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100494 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
Yann Dirson774aed52021-05-18 10:59:04 +0200495 fprintf (stderr,
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100496 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
Yann Dirson774aed52021-05-18 10:59:04 +0200497 params.cmdname, params.imagefile,
Heinrich Schuchardt5a3b8dc2022-01-15 20:12:56 +0100498 (unsigned long long) sbuf.st_size,
499 tparams->header_size);
Yann Dirson774aed52021-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 Dirson774aed52021-05-18 10:59:04 +0200505 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
Mike Frysingercd7e1a32008-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 Wadaskarfabf3cf2009-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árcdf0d1c2022-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 Glasse4607262021-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 Wadaskar139cb082009-08-10 18:49:37 +0530526 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000527 }
528
Marek Vasut4cc86272015-12-07 18:01:54 +0100529 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert3edd0412015-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 Glass139651f2015-06-23 15:39:12 -0600537
Philippe De Swert3edd0412015-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 Glass139651f2015-06-23 15:39:12 -0600544
Philippe De Swert3edd0412015-12-04 00:11:23 +0200545 params.file_size = sbuf.st_size + tparams->header_size;
546 close(dfd);
547 }
Simon Glass139651f2015-06-23 15:39:12 -0600548
wdenk5b1d7132002-11-03 00:07:02 +0000549 /*
Stefano Babic4a963302011-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 Babic4a963302011-09-15 23:50:16 +0000555 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200556 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000557 else
558 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000559
Prafulla Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-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 Rieschc14f2812011-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 Rieschc14f2812011-12-09 09:47:38 +0000573 for (;;) {
574 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000575
Christian Rieschc14f2812011-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 Rieschc14f2812011-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 }
wdenk5b1d7132002-11-03 00:07:02 +0000597
Christian Rieschc14f2812011-12-09 09:47:38 +0000598 if (!file) {
599 break;
600 }
wdenk5b1d7132002-11-03 00:07:02 +0000601
Christian Rieschc14f2812011-12-09 09:47:38 +0000602 if (sep) {
603 *sep = ':';
604 file = sep + 1;
605 } else {
606 file = NULL;
607 }
wdenk5b1d7132002-11-03 00:07:02 +0000608 }
wdenk5b1d7132002-11-03 00:07:02 +0000609
Christian Rieschc14f2812011-12-09 09:47:38 +0000610 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000611
Christian Rieschc14f2812011-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 Xieea65fd82012-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 Graf5329d672018-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 Fan60a56072018-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 Fan31c51da2018-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 Chenec0d2ea2019-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 Rieschc14f2812011-12-09 09:47:38 +0000656 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200657 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000658 }
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100659 if (params.type == IH_TYPE_FIRMWARE_IVT) {
660 /* Add alignment and IVT */
Kever Yang86f26012020-03-30 11:56:19 +0800661 uint32_t aligned_filesize = ALIGN(params.file_size,
662 0x1000);
Sven Ebenfeld59697a22016-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 Ebenfeld3bb71e02017-01-17 19:36:51 +0100672 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeld59697a22016-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 Wadaskarfabf3cf2009-08-19 17:36:46 +0530693#if defined(_POSIX_SYNCHRONIZED_IO) && \
694 !defined(__sun__) && \
695 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200696 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-08-19 17:36:46 +0530705 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000706 exit (EXIT_FAILURE);
707 }
Simon Glass139651f2015-06-23 15:39:12 -0600708 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000709
Mark Tomlinsonfc497862018-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 Frysingercd7e1a32008-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 Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-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 GARDETdfe25fa2018-03-30 10:28:19 +0200731 fprintf (stderr, "%s: Can't print header for %s\n",
732 params.cmdname, tparams->name);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530733 }
wdenk5b1d7132002-11-03 00:07:02 +0000734
Mark Tomlinsonfc497862018-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 Wadaskarfabf3cf2009-08-19 17:36:46 +0530738#if defined(_POSIX_SYNCHRONIZED_IO) && \
739 !defined(__sun__) && \
740 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200741 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-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ára55eaa52022-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 Babic6531bd92013-08-19 19:03:19 +0200768 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000769 int offset = 0;
Mylène Josserand9e0e9a52020-07-08 11:52:50 +0200770 int size, ret;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200771 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000772
Stefano Babic6531bd92013-08-19 19:03:19 +0200773 memset(zeros, 0, sizeof(zeros));
774
Prafulla Wadaskarfabf3cf2009-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
wdenk1ee550e2003-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 Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-08-19 17:36:46 +0530787 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000788 exit (EXIT_FAILURE);
789 }
790
Thomas Hebb13895a32021-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 Frysingercd7e1a32008-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 Wadaskarfabf3cf2009-08-19 17:36:46 +0530800 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000801 exit (EXIT_FAILURE);
802 }
803
Prafulla Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-08-19 17:36:46 +0530815 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000816 exit (EXIT_FAILURE);
817 }
818
Prafulla Wadaskarfabf3cf2009-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 Wadaskarfabf3cf2009-08-19 17:36:46 +0530823 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000824 exit (EXIT_FAILURE);
825 }
826 }
827
Prafulla Wadaskarfabf3cf2009-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 Josserand9e0e9a52020-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 Babic6531bd92013-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 Wadaskarfabf3cf2009-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 Babic6531bd92013-08-19 19:03:19 +0200854 } else if (pad > 1) {
Simon Glass46409cc2015-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 Babic6531bd92013-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}