blob: 68d5206cb4fd80b3e0e5735b9208b54d9aae7874 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00002/*
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +01003 * (C) Copyright 2008 Semihalf
4 *
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +05305 * (C) Copyright 2000-2009
wdenk5b1d7132002-11-03 00:07:02 +00006 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
wdenk5b1d7132002-11-03 00:07:02 +00008 */
9
Kever Yang86f26012020-03-30 11:56:19 +080010#include "imagetool.h"
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010011#include "mkimage.h"
Sven Ebenfeld59697a22016-11-06 16:37:56 +010012#include "imximage.h"
wdenk5b1d7132002-11-03 00:07:02 +000013#include <image.h>
Wolfgang Denke97ab722011-02-12 10:37:11 +010014#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000015
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053016static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000017
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053018/* parameters initialized by core will be used by the image type code */
Simon Glass52322d62016-02-22 22:55:38 -070019static struct image_tool_params params = {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053020 .os = IH_OS_LINUX,
21 .arch = IH_ARCH_PPC,
22 .type = IH_TYPE_KERNEL,
23 .comp = IH_COMP_GZIP,
24 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
Wolfgang Denk23416b82010-03-27 23:37:46 +010025 .imagename = "",
Shaohui Xieea65fd82012-08-10 02:49:35 +000026 .imagename2 = "",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053027};
wdenk5b1d7132002-11-03 00:07:02 +000028
Simon Glass1d138b52016-06-30 10:52:17 -060029static enum ih_category cur_category;
30
31static int h_compare_category_name(const void *vtype1, const void *vtype2)
32{
33 const int *type1 = vtype1;
34 const int *type2 = vtype2;
35 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
36 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
37
38 return strcmp(name1, name2);
39}
40
Simon Glassbe421592016-06-30 10:52:18 -060041static int show_valid_options(enum ih_category category)
Simon Glass1d138b52016-06-30 10:52:17 -060042{
43 int *order;
44 int count;
45 int item;
46 int i;
47
48 count = genimg_get_cat_count(category);
49 order = calloc(count, sizeof(*order));
50 if (!order)
51 return -ENOMEM;
52
53 /* Sort the names in order of short name for easier reading */
Naoki Hayama089fc922020-10-07 11:21:55 +090054 for (i = 0, item = 0; i < count; i++, item++) {
55 while (!genimg_cat_has_id(category, item) && i < count) {
56 item++;
57 count--;
58 }
59 order[i] = item;
60 }
Simon Glass1d138b52016-06-30 10:52:17 -060061 cur_category = category;
62 qsort(order, count, sizeof(int), h_compare_category_name);
63
64 fprintf(stderr, "\nInvalid %s, supported are:\n",
65 genimg_get_cat_desc(category));
66 for (i = 0; i < count; i++) {
67 item = order[i];
68 fprintf(stderr, "\t%-15s %s\n",
69 genimg_get_cat_short_name(category, item),
70 genimg_get_cat_name(category, item));
71 }
72 fprintf(stderr, "\n");
Simon Glassd435d9f2016-10-27 17:54:03 -060073 free(order);
Simon Glass1d138b52016-06-30 10:52:17 -060074
75 return 0;
76}
77
Simon Glass01168b32016-02-22 22:55:37 -070078static void usage(const char *msg)
Simon Glass99a7c262016-02-22 22:55:36 -070079{
Simon Glass01168b32016-02-22 22:55:37 -070080 fprintf(stderr, "Error: %s\n", msg);
Simon Glass99a7c262016-02-22 22:55:36 -070081 fprintf(stderr, "Usage: %s -l image\n"
82 " -l ==> list image header information\n",
83 params.cmdname);
84 fprintf(stderr,
85 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
86 " -A ==> set architecture to 'arch'\n"
87 " -O ==> set operating system to 'os'\n"
88 " -T ==> set image type to 'type'\n"
89 " -C ==> set compression type 'comp'\n"
90 " -a ==> set load address to 'addr' (hex)\n"
91 " -e ==> set entry point to 'ep' (hex)\n"
92 " -n ==> set image name to 'name'\n"
93 " -d ==> use image data from 'datafile'\n"
94 " -x ==> set XIP (execute in place)\n",
95 params.cmdname);
96 fprintf(stderr,
Joel Stanley00dffdd2020-12-08 14:42:15 +103097 " %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 -070098 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glass99a7c262016-02-22 22:55:36 -070099 params.cmdname);
100 fprintf(stderr,
101 " -D => set all options for device tree compiler\n"
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100102 " -f => input filename for FIT source\n"
Joel Stanley00dffdd2020-12-08 14:42:15 +1030103 " -i => input filename for ramdisk file\n"
104 " -E => place data outside of the FIT structure\n"
105 " -B => align size in hex for FIT structure and header\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700106#ifdef CONFIG_FIT_SIGNATURE
107 fprintf(stderr,
Joel Stanley00dffdd2020-12-08 14:42:15 +1030108 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700109 " -k => set directory containing private keys\n"
110 " -K => write public keys to this .dtb file\n"
111 " -c => add comment in signature node\n"
112 " -F => re-sign existing FIT image\n"
Teddy Reeda8457622016-06-09 19:38:02 -0700113 " -p => place external data at a static position\n"
George McCollister23d14892017-01-06 13:14:17 -0600114 " -r => mark keys used as 'required' in dtb\n"
Vesa Jääskeläinenabf8eb22019-06-16 20:53:38 +0300115 " -N => openssl engine to use for signing\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700116#else
117 fprintf(stderr,
118 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
119#endif
120 fprintf(stderr, " %s -V ==> print version information and exit\n",
121 params.cmdname);
Baruch Siachf77ab202017-06-29 20:37:08 +0300122 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glass99a7c262016-02-22 22:55:36 -0700123
124 exit(EXIT_FAILURE);
125}
126
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700127static int add_content(int type, const char *fname)
128{
129 struct content_info *cont;
130
131 cont = calloc(1, sizeof(*cont));
132 if (!cont)
133 return -1;
134 cont->type = type;
135 cont->fname = fname;
136 if (params.content_tail)
137 params.content_tail->next = cont;
138 else
139 params.content_head = cont;
140 params.content_tail = cont;
141
142 return 0;
143}
144
Joel Stanley00dffdd2020-12-08 14:42:15 +1030145#define OPT_STRING "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx"
Simon Glass7106bd42016-02-22 22:55:33 -0700146static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000147{
Peter Tyser632d9ca2010-04-04 22:36:03 -0500148 char *ptr;
Simon Glass04b46072016-02-22 22:55:48 -0700149 int type = IH_TYPE_INVALID;
150 char *datafile = NULL;
Simon Glasscf76cd22016-02-22 22:55:34 -0700151 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000152
Simon Glasscf76cd22016-02-22 22:55:34 -0700153 while ((opt = getopt(argc, argv,
Simon Glass472ee0c2020-07-09 18:39:43 -0600154 "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
Simon Glasscf76cd22016-02-22 22:55:34 -0700155 switch (opt) {
Simon Glassf6fe5f62016-02-22 22:55:35 -0700156 case 'a':
157 params.addr = strtoull(optarg, &ptr, 16);
158 if (*ptr) {
159 fprintf(stderr, "%s: invalid load address %s\n",
160 params.cmdname, optarg);
161 exit(EXIT_FAILURE);
162 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700163 break;
164 case 'A':
165 params.arch = genimg_get_arch_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600166 if (params.arch < 0) {
167 show_valid_options(IH_ARCH);
Simon Glass01168b32016-02-22 22:55:37 -0700168 usage("Invalid architecture");
Simon Glasseaa25402016-06-30 10:52:19 -0600169 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700170 break;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700171 case 'b':
Andreas Bießmannddd34772016-05-03 15:17:03 +0200172 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmannf4001582016-05-01 03:01:27 +0200173 fprintf(stderr,
174 "%s: Out of memory adding content '%s'",
175 params.cmdname, optarg);
176 exit(EXIT_FAILURE);
177 }
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700178 break;
Kever Yang8e238b52020-03-30 11:56:24 +0800179 case 'B':
180 params.bl_len = strtoull(optarg, &ptr, 16);
181 if (*ptr) {
182 fprintf(stderr, "%s: invalid block length %s\n",
183 params.cmdname, optarg);
184 exit(EXIT_FAILURE);
185 }
186
187 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700188 case 'c':
189 params.comment = optarg;
190 break;
191 case 'C':
192 params.comp = genimg_get_comp_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600193 if (params.comp < 0) {
194 show_valid_options(IH_COMP);
Simon Glass01168b32016-02-22 22:55:37 -0700195 usage("Invalid compression type");
Simon Glasseaa25402016-06-30 10:52:19 -0600196 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700197 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700198 case 'd':
199 params.datafile = optarg;
200 params.dflag = 1;
201 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700202 case 'D':
203 params.dtc = optarg;
204 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700205 case 'e':
206 params.ep = strtoull(optarg, &ptr, 16);
207 if (*ptr) {
208 fprintf(stderr, "%s: invalid entry point %s\n",
209 params.cmdname, optarg);
210 exit(EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000211 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700212 params.eflag = 1;
213 break;
Simon Glassafd728c2016-02-22 22:55:53 -0700214 case 'E':
215 params.external_data = true;
216 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700217 case 'f':
Simon Glass88e31cb2016-02-22 22:55:51 -0700218 datafile = optarg;
219 params.auto_its = !strcmp(datafile, "auto");
Heinrich Schuchardtddc38e62020-05-09 17:10:41 +0200220 /* fallthrough */
Simon Glasscf76cd22016-02-22 22:55:34 -0700221 case 'F':
222 /*
223 * The flattened image tree (FIT) format
224 * requires a flattened device tree image type
225 */
226 params.type = IH_TYPE_FLATDT;
227 params.fflag = 1;
228 break;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100229 case 'i':
230 params.fit_ramdisk = optarg;
231 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700232 case 'k':
233 params.keydir = optarg;
234 break;
235 case 'K':
236 params.keydest = optarg;
237 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700238 case 'l':
239 params.lflag = 1;
240 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700241 case 'n':
242 params.imagename = optarg;
243 break;
George McCollister23d14892017-01-06 13:14:17 -0600244 case 'N':
245 params.engine_id = optarg;
246 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700247 case 'O':
248 params.os = genimg_get_os_id(optarg);
Simon Glasseaa25402016-06-30 10:52:19 -0600249 if (params.os < 0) {
250 show_valid_options(IH_OS);
Simon Glass01168b32016-02-22 22:55:37 -0700251 usage("Invalid operating system");
Simon Glasseaa25402016-06-30 10:52:19 -0600252 }
Simon Glassf6fe5f62016-02-22 22:55:35 -0700253 break;
Teddy Reeda8457622016-06-09 19:38:02 -0700254 case 'p':
255 params.external_offset = strtoull(optarg, &ptr, 16);
256 if (*ptr) {
257 fprintf(stderr, "%s: invalid offset size %s\n",
258 params.cmdname, optarg);
259 exit(EXIT_FAILURE);
260 }
Teddy Reed9e3cd3c2016-07-11 22:54:26 -0700261 break;
Simon Glassda1805f2016-05-01 13:55:38 -0600262 case 'q':
263 params.quiet = 1;
264 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700265 case 'r':
266 params.require_keys = 1;
267 break;
268 case 'R':
269 /*
270 * This entry is for the second configuration
271 * file, if only one is not enough.
272 */
273 params.imagename2 = optarg;
274 break;
275 case 's':
276 params.skipcpy = 1;
277 break;
Simon Glass472ee0c2020-07-09 18:39:43 -0600278 case 't':
279 params.reset_timestamp = 1;
280 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700281 case 'T':
Baruch Siachf77ab202017-06-29 20:37:08 +0300282 if (strcmp(optarg, "list") == 0) {
283 show_valid_options(IH_TYPE);
284 exit(EXIT_SUCCESS);
285 }
Simon Glass04b46072016-02-22 22:55:48 -0700286 type = genimg_get_type_id(optarg);
287 if (type < 0) {
Simon Glassbe421592016-06-30 10:52:18 -0600288 show_valid_options(IH_TYPE);
Simon Glass01168b32016-02-22 22:55:37 -0700289 usage("Invalid image type");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700290 }
291 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700292 case 'v':
293 params.vflag++;
294 break;
295 case 'V':
296 printf("mkimage version %s\n", PLAIN_VERSION);
297 exit(EXIT_SUCCESS);
298 case 'x':
299 params.xflag++;
300 break;
301 default:
Simon Glass01168b32016-02-22 22:55:37 -0700302 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000303 }
wdenk5b1d7132002-11-03 00:07:02 +0000304 }
305
Andreas Bießmannddd34772016-05-03 15:17:03 +0200306 /* The last parameter is expected to be the imagefile */
307 if (optind < argc)
Andreas Bießmannf4001582016-05-01 03:01:27 +0200308 params.imagefile = argv[optind];
309
Simon Glass04b46072016-02-22 22:55:48 -0700310 /*
311 * For auto-generated FIT images we need to know the image type to put
312 * in the FIT, which is separate from the file's image type (which
313 * will always be IH_TYPE_FLATDT in this case).
314 */
315 if (params.type == IH_TYPE_FLATDT) {
Simon Glassa89857d2016-06-30 10:52:07 -0600316 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass156c3492016-06-30 10:52:08 -0600317 /* For auto_its, datafile is always 'auto' */
Simon Glass88e31cb2016-02-22 22:55:51 -0700318 if (!params.auto_its)
319 params.datafile = datafile;
Simon Glass79b5ee52016-06-30 10:52:09 -0600320 else if (!params.datafile)
321 usage("Missing data file for auto-FIT (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700322 } else if (type != IH_TYPE_INVALID) {
Alex Kiernan9d74c702018-04-22 05:11:17 +0000323 if (type == IH_TYPE_SCRIPT && !params.datafile)
324 usage("Missing data file for script (use -d)");
Simon Glass04b46072016-02-22 22:55:48 -0700325 params.type = type;
326 }
327
328 if (!params.imagefile)
Simon Glass01168b32016-02-22 22:55:37 -0700329 usage("Missing output filename");
Simon Glass7106bd42016-02-22 22:55:33 -0700330}
331
Simon Glass7106bd42016-02-22 22:55:33 -0700332int main(int argc, char **argv)
333{
334 int ifd = -1;
335 struct stat sbuf;
336 char *ptr;
337 int retval = 0;
338 struct image_type_params *tparams = NULL;
339 int pad_len = 0;
340 int dfd;
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200341 size_t map_len;
Simon Glass7106bd42016-02-22 22:55:33 -0700342
343 params.cmdname = *argv;
344 params.addr = 0;
345 params.ep = 0;
346
347 process_args(argc, argv);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530348
349 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200350 tparams = imagetool_get_type(params.type);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530351 if (tparams == NULL) {
352 fprintf (stderr, "%s: unsupported type %s\n",
353 params.cmdname, genimg_get_type_name(params.type));
354 exit (EXIT_FAILURE);
355 }
356
357 /*
358 * check the passed arguments parameters meets the requirements
359 * as per image type to be generated/listed
360 */
361 if (tparams->check_params)
362 if (tparams->check_params (&params))
Simon Glass01168b32016-02-22 22:55:37 -0700363 usage("Bad parameters for image type");
wdenk5b1d7132002-11-03 00:07:02 +0000364
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530365 if (!params.eflag) {
366 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000367 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530368 if (params.xflag)
369 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000370 }
371
Peter Tyserb2f8b942009-11-24 16:42:10 -0600372 if (params.fflag){
373 if (tparams->fflag_handle)
374 /*
375 * in some cases, some additional processing needs
376 * to be done if fflag is defined
377 *
378 * For ex. fit_handle_file for Fit file support
379 */
380 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000381
Peter Tyserb2f8b942009-11-24 16:42:10 -0600382 if (retval != EXIT_SUCCESS)
383 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000384 }
385
Peter Tyserb2f8b942009-11-24 16:42:10 -0600386 if (params.lflag || params.fflag) {
387 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
388 } else {
389 ifd = open (params.imagefile,
390 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
391 }
392
393 if (ifd < 0) {
394 fprintf (stderr, "%s: Can't open %s: %s\n",
395 params.cmdname, params.imagefile,
396 strerror(errno));
397 exit (EXIT_FAILURE);
398 }
399
400 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000401 /*
402 * list header information of existing image
403 */
404 if (fstat(ifd, &sbuf) < 0) {
405 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530406 params.cmdname, params.imagefile,
407 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000408 exit (EXIT_FAILURE);
409 }
410
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530411 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000412 fprintf (stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530413 "%s: Bad size: \"%s\" is not valid image\n",
414 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000415 exit (EXIT_FAILURE);
416 }
417
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400418 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
419 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000420 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530421 params.cmdname, params.imagefile,
422 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000423 exit (EXIT_FAILURE);
424 }
425
Jordan Hand408abe92019-04-09 10:36:09 -0700426 if (params.fflag) {
427 /*
428 * Verifies the header format based on the expected header for image
429 * type in tparams
430 */
431 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
432 tparams, &params);
433 } else {
434 /**
435 * When listing the image, we are not given the image type. Simply check all
436 * image types to find one that matches our header
437 */
438 retval = imagetool_verify_print_header(ptr, &sbuf,
439 tparams, &params);
440 }
wdenk5b1d7132002-11-03 00:07:02 +0000441
wdenk5b1d7132002-11-03 00:07:02 +0000442 (void) munmap((void *)ptr, sbuf.st_size);
443 (void) close (ifd);
444
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530445 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000446 }
447
Marek Vasut4cc86272015-12-07 18:01:54 +0100448 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert3edd0412015-12-04 00:11:23 +0200449 dfd = open(params.datafile, O_RDONLY | O_BINARY);
450 if (dfd < 0) {
451 fprintf(stderr, "%s: Can't open %s: %s\n",
452 params.cmdname, params.datafile,
453 strerror(errno));
454 exit(EXIT_FAILURE);
455 }
Simon Glass139651f2015-06-23 15:39:12 -0600456
Philippe De Swert3edd0412015-12-04 00:11:23 +0200457 if (fstat(dfd, &sbuf) < 0) {
458 fprintf(stderr, "%s: Can't stat %s: %s\n",
459 params.cmdname, params.datafile,
460 strerror(errno));
461 exit(EXIT_FAILURE);
462 }
Simon Glass139651f2015-06-23 15:39:12 -0600463
Philippe De Swert3edd0412015-12-04 00:11:23 +0200464 params.file_size = sbuf.st_size + tparams->header_size;
465 close(dfd);
466 }
Simon Glass139651f2015-06-23 15:39:12 -0600467
wdenk5b1d7132002-11-03 00:07:02 +0000468 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000469 * In case there an header with a variable
470 * length will be added, the corresponding
471 * function is called. This is responsible to
472 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000473 */
Stefano Babic4a963302011-09-15 23:50:16 +0000474 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200475 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000476 else
477 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000478
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530479 if (write(ifd, tparams->hdr, tparams->header_size)
480 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000481 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530482 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000483 exit (EXIT_FAILURE);
484 }
485
Christian Rieschc14f2812011-12-09 09:47:38 +0000486 if (!params.skipcpy) {
487 if (params.type == IH_TYPE_MULTI ||
488 params.type == IH_TYPE_SCRIPT) {
489 char *file = params.datafile;
490 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000491
Christian Rieschc14f2812011-12-09 09:47:38 +0000492 for (;;) {
493 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000494
Christian Rieschc14f2812011-12-09 09:47:38 +0000495 if (file) {
496 if ((sep = strchr(file, ':')) != NULL) {
497 *sep = '\0';
498 }
499
500 if (stat (file, &sbuf) < 0) {
501 fprintf (stderr, "%s: Can't stat %s: %s\n",
502 params.cmdname, file, strerror(errno));
503 exit (EXIT_FAILURE);
504 }
505 size = cpu_to_uimage (sbuf.st_size);
506 } else {
507 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000508 }
509
Christian Rieschc14f2812011-12-09 09:47:38 +0000510 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
511 fprintf (stderr, "%s: Write error on %s: %s\n",
512 params.cmdname, params.imagefile,
513 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000514 exit (EXIT_FAILURE);
515 }
wdenk5b1d7132002-11-03 00:07:02 +0000516
Christian Rieschc14f2812011-12-09 09:47:38 +0000517 if (!file) {
518 break;
519 }
wdenk5b1d7132002-11-03 00:07:02 +0000520
Christian Rieschc14f2812011-12-09 09:47:38 +0000521 if (sep) {
522 *sep = ':';
523 file = sep + 1;
524 } else {
525 file = NULL;
526 }
wdenk5b1d7132002-11-03 00:07:02 +0000527 }
wdenk5b1d7132002-11-03 00:07:02 +0000528
Christian Rieschc14f2812011-12-09 09:47:38 +0000529 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000530
Christian Rieschc14f2812011-12-09 09:47:38 +0000531 for (;;) {
532 char *sep = strchr(file, ':');
533 if (sep) {
534 *sep = '\0';
535 copy_file (ifd, file, 1);
536 *sep++ = ':';
537 file = sep;
538 } else {
539 copy_file (ifd, file, 0);
540 break;
541 }
wdenk5b1d7132002-11-03 00:07:02 +0000542 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000543 } else if (params.type == IH_TYPE_PBLIMAGE) {
544 /* PBL has special Image format, implements its' own */
545 pbl_load_uboot(ifd, &params);
Alexander Graf5329d672018-04-13 14:18:52 +0200546 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
547 /* Image file is meta, walk through actual targets */
548 int ret;
549
550 ret = zynqmpbif_copy_image(ifd, &params);
551 if (ret)
552 return ret;
Peng Fan60a56072018-10-16 04:50:30 +0000553 } else if (params.type == IH_TYPE_IMX8IMAGE) {
554 /* i.MX8/8X has special Image format */
555 int ret;
556
557 ret = imx8image_copy_image(ifd, &params);
558 if (ret)
559 return ret;
Peng Fan31c51da2018-11-20 10:19:36 +0000560 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
561 /* i.MX8M has special Image format */
562 int ret;
563
564 ret = imx8mimage_copy_image(ifd, &params);
565 if (ret)
566 return ret;
Jeffy Chenec0d2ea2019-12-27 11:24:41 +0800567 } else if ((params.type == IH_TYPE_RKSD) ||
568 (params.type == IH_TYPE_RKSPI)) {
569 /* Rockchip has special Image format */
570 int ret;
571
572 ret = rockchip_copy_image(ifd, &params);
573 if (ret)
574 return ret;
Christian Rieschc14f2812011-12-09 09:47:38 +0000575 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200576 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000577 }
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100578 if (params.type == IH_TYPE_FIRMWARE_IVT) {
579 /* Add alignment and IVT */
Kever Yang86f26012020-03-30 11:56:19 +0800580 uint32_t aligned_filesize = ALIGN(params.file_size,
581 0x1000);
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100582 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
583 params.addr, 0, 0, 0, params.addr
584 + aligned_filesize
585 - tparams->header_size,
586 params.addr + aligned_filesize
587 - tparams->header_size
588 + 0x20, 0 };
589 int i = params.file_size;
590 for (; i < aligned_filesize; i++) {
Sven Ebenfeld3bb71e02017-01-17 19:36:51 +0100591 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeld59697a22016-11-06 16:37:56 +0100592 fprintf(stderr,
593 "%s: Write error on %s: %s\n",
594 params.cmdname,
595 params.imagefile,
596 strerror(errno));
597 exit(EXIT_FAILURE);
598 }
599 }
600 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
601 != sizeof(flash_header_v2_t)) {
602 fprintf(stderr, "%s: Write error on %s: %s\n",
603 params.cmdname,
604 params.imagefile,
605 strerror(errno));
606 exit(EXIT_FAILURE);
607 }
608 }
wdenk5b1d7132002-11-03 00:07:02 +0000609 }
610
611 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530612#if defined(_POSIX_SYNCHRONIZED_IO) && \
613 !defined(__sun__) && \
614 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200615 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530616 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000617 (void) fdatasync (ifd);
618#else
619 (void) fsync (ifd);
620#endif
621
622 if (fstat(ifd, &sbuf) < 0) {
623 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530624 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000625 exit (EXIT_FAILURE);
626 }
Simon Glass139651f2015-06-23 15:39:12 -0600627 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000628
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200629 map_len = sbuf.st_size;
630 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400631 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000632 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530633 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000634 exit (EXIT_FAILURE);
635 }
636
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530637 /* Setup the image header as per input image type*/
638 if (tparams->set_header)
639 tparams->set_header (ptr, &sbuf, ifd, &params);
640 else {
641 fprintf (stderr, "%s: Can't set header for %s: %s\n",
642 params.cmdname, tparams->name, strerror(errno));
643 exit (EXIT_FAILURE);
644 }
wdenk5b1d7132002-11-03 00:07:02 +0000645
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530646 /* Print the image information by processing image header */
647 if (tparams->print_header)
648 tparams->print_header (ptr);
649 else {
Guillaume GARDETdfe25fa2018-03-30 10:28:19 +0200650 fprintf (stderr, "%s: Can't print header for %s\n",
651 params.cmdname, tparams->name);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530652 }
wdenk5b1d7132002-11-03 00:07:02 +0000653
Mark Tomlinsonfc497862018-08-29 10:51:14 +1200654 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000655
656 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530657#if defined(_POSIX_SYNCHRONIZED_IO) && \
658 !defined(__sun__) && \
659 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200660 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530661 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000662 (void) fdatasync (ifd);
663#else
664 (void) fsync (ifd);
665#endif
666
667 if (close(ifd)) {
668 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530669 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000670 exit (EXIT_FAILURE);
671 }
672
673 exit (EXIT_SUCCESS);
674}
675
676static void
677copy_file (int ifd, const char *datafile, int pad)
678{
679 int dfd;
680 struct stat sbuf;
681 unsigned char *ptr;
682 int tail;
683 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200684 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000685 int offset = 0;
Mylène Josserand9e0e9a52020-07-08 11:52:50 +0200686 int size, ret;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200687 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000688
Stefano Babic6531bd92013-08-19 19:03:19 +0200689 memset(zeros, 0, sizeof(zeros));
690
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530691 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000692 fprintf (stderr, "Adding Image %s\n", datafile);
693 }
694
wdenk1ee550e2003-10-08 22:14:02 +0000695 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000696 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530697 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000698 exit (EXIT_FAILURE);
699 }
700
701 if (fstat(dfd, &sbuf) < 0) {
702 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530703 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000704 exit (EXIT_FAILURE);
705 }
706
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400707 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
708 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000709 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530710 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000711 exit (EXIT_FAILURE);
712 }
713
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530714 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000715 unsigned char *p = NULL;
716 /*
717 * XIP: do not append the image_header_t at the
718 * beginning of the file, but consume the space
719 * reserved for it.
720 */
721
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530722 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000723 fprintf (stderr,
724 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530725 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000726 exit (EXIT_FAILURE);
727 }
728
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530729 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000730 if ( *p != 0xff ) {
731 fprintf (stderr,
732 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530733 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000734 exit (EXIT_FAILURE);
735 }
736 }
737
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530738 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000739 }
740
741 size = sbuf.st_size - offset;
Mylène Josserand9e0e9a52020-07-08 11:52:50 +0200742
743 ret = write(ifd, ptr + offset, size);
744 if (ret != size) {
745 if (ret < 0)
746 fprintf (stderr, "%s: Write error on %s: %s\n",
747 params.cmdname, params.imagefile, strerror(errno));
748 else if (ret < size)
749 fprintf (stderr, "%s: Write only %d/%d bytes, "\
750 "probably no space left on the device\n",
751 params.cmdname, ret, size);
wdenk5b1d7132002-11-03 00:07:02 +0000752 exit (EXIT_FAILURE);
753 }
754
Stefano Babic6531bd92013-08-19 19:03:19 +0200755 tail = size % 4;
756 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000757
758 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
759 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530760 params.cmdname, params.imagefile,
761 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000762 exit (EXIT_FAILURE);
763 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200764 } else if (pad > 1) {
Simon Glass46409cc2015-08-30 16:55:22 -0600765 while (pad > 0) {
766 int todo = sizeof(zeros);
767
768 if (todo > pad)
769 todo = pad;
770 if (write(ifd, (char *)&zeros, todo) != todo) {
771 fprintf(stderr, "%s: Write error on %s: %s\n",
772 params.cmdname, params.imagefile,
773 strerror(errno));
774 exit(EXIT_FAILURE);
775 }
776 pad -= todo;
Stefano Babic6531bd92013-08-19 19:03:19 +0200777 }
wdenk5b1d7132002-11-03 00:07:02 +0000778 }
779
780 (void) munmap((void *)ptr, sbuf.st_size);
781 (void) close (dfd);
782}