blob: 336376f8d0a2bbe8f555ab55bb0d3795e22a379a [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"
wdenk5b1d7132002-11-03 00:07:02 +000013#include <image.h>
Wolfgang Denk976b38c2011-02-12 10:37:11 +010014#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000015
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053016static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000017
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053018/* parameters initialized by core will be used by the image type code */
Simon Glasscc7a6442016-02-22 22:55:38 -070019static struct image_tool_params params = {
Prafulla Wadaskar89a4d6b2009-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 Denk04387d22010-03-27 23:37:46 +010025 .imagename = "",
Shaohui Xie5d898a02012-08-10 02:49:35 +000026 .imagename2 = "",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053027};
wdenk5b1d7132002-11-03 00:07:02 +000028
Simon Glass30664222016-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 Glassf24e1052016-06-30 10:52:18 -060041static int show_valid_options(enum ih_category category)
Simon Glass30664222016-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 */
54 for (item = 0; item < count; item++)
55 order[item] = item;
56 cur_category = category;
57 qsort(order, count, sizeof(int), h_compare_category_name);
58
59 fprintf(stderr, "\nInvalid %s, supported are:\n",
60 genimg_get_cat_desc(category));
61 for (i = 0; i < count; i++) {
62 item = order[i];
63 fprintf(stderr, "\t%-15s %s\n",
64 genimg_get_cat_short_name(category, item),
65 genimg_get_cat_name(category, item));
66 }
67 fprintf(stderr, "\n");
Simon Glass0cd82e22016-10-27 17:54:03 -060068 free(order);
Simon Glass30664222016-06-30 10:52:17 -060069
70 return 0;
71}
72
Simon Glass15310342016-02-22 22:55:37 -070073static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -070074{
Simon Glass15310342016-02-22 22:55:37 -070075 fprintf(stderr, "Error: %s\n", msg);
Simon Glassb0a487a2016-02-22 22:55:36 -070076 fprintf(stderr, "Usage: %s -l image\n"
77 " -l ==> list image header information\n",
78 params.cmdname);
79 fprintf(stderr,
80 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
81 " -A ==> set architecture to 'arch'\n"
82 " -O ==> set operating system to 'os'\n"
83 " -T ==> set image type to 'type'\n"
84 " -C ==> set compression type 'comp'\n"
85 " -a ==> set load address to 'addr' (hex)\n"
86 " -e ==> set entry point to 'ep' (hex)\n"
87 " -n ==> set image name to 'name'\n"
88 " -d ==> use image data from 'datafile'\n"
89 " -x ==> set XIP (execute in place)\n",
90 params.cmdname);
91 fprintf(stderr,
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +010092 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
Vagrant Cascadian82bd2f22016-10-23 20:45:17 -070093 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glassb0a487a2016-02-22 22:55:36 -070094 params.cmdname);
95 fprintf(stderr,
96 " -D => set all options for device tree compiler\n"
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +010097 " -f => input filename for FIT source\n"
98 " -i => input filename for ramdisk file\n");
Simon Glassb0a487a2016-02-22 22:55:36 -070099#ifdef CONFIG_FIT_SIGNATURE
100 fprintf(stderr,
Kever Yangebfe6112020-03-30 11:56:24 +0800101 "Signing / verified boot options: [-E] [-B size] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700102 " -E => place data outside of the FIT structure\n"
Kever Yangebfe6112020-03-30 11:56:24 +0800103 " -B => align size in hex for FIT structure and header\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700104 " -k => set directory containing private keys\n"
105 " -K => write public keys to this .dtb file\n"
106 " -c => add comment in signature node\n"
107 " -F => re-sign existing FIT image\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700108 " -p => place external data at a static position\n"
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600109 " -r => mark keys used as 'required' in dtb\n"
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300110 " -N => openssl engine to use for signing\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700111#else
112 fprintf(stderr,
113 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
114#endif
115 fprintf(stderr, " %s -V ==> print version information and exit\n",
116 params.cmdname);
Baruch Siach79aa33c2017-06-29 20:37:08 +0300117 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700118
119 exit(EXIT_FAILURE);
120}
121
Simon Glassfb4cce02016-02-22 22:55:52 -0700122static int add_content(int type, const char *fname)
123{
124 struct content_info *cont;
125
126 cont = calloc(1, sizeof(*cont));
127 if (!cont)
128 return -1;
129 cont->type = type;
130 cont->fname = fname;
131 if (params.content_tail)
132 params.content_tail->next = cont;
133 else
134 params.content_head = cont;
135 params.content_tail = cont;
136
137 return 0;
138}
139
Simon Glass0b443de2016-02-22 22:55:33 -0700140static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000141{
Peter Tysera2513e22010-04-04 22:36:03 -0500142 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700143 int type = IH_TYPE_INVALID;
144 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700145 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000146
Simon Glassa02221f2016-02-22 22:55:34 -0700147 while ((opt = getopt(argc, argv,
Kever Yangebfe6112020-03-30 11:56:24 +0800148 "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
Simon Glassa02221f2016-02-22 22:55:34 -0700149 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700150 case 'a':
151 params.addr = strtoull(optarg, &ptr, 16);
152 if (*ptr) {
153 fprintf(stderr, "%s: invalid load address %s\n",
154 params.cmdname, optarg);
155 exit(EXIT_FAILURE);
156 }
Simon Glassa02221f2016-02-22 22:55:34 -0700157 break;
158 case 'A':
159 params.arch = genimg_get_arch_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600160 if (params.arch < 0) {
161 show_valid_options(IH_ARCH);
Simon Glass15310342016-02-22 22:55:37 -0700162 usage("Invalid architecture");
Simon Glass51f03e62016-06-30 10:52:19 -0600163 }
Simon Glassa02221f2016-02-22 22:55:34 -0700164 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700165 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200166 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200167 fprintf(stderr,
168 "%s: Out of memory adding content '%s'",
169 params.cmdname, optarg);
170 exit(EXIT_FAILURE);
171 }
Simon Glassfb4cce02016-02-22 22:55:52 -0700172 break;
Kever Yangebfe6112020-03-30 11:56:24 +0800173 case 'B':
174 params.bl_len = strtoull(optarg, &ptr, 16);
175 if (*ptr) {
176 fprintf(stderr, "%s: invalid block length %s\n",
177 params.cmdname, optarg);
178 exit(EXIT_FAILURE);
179 }
180
181 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700182 case 'c':
183 params.comment = optarg;
184 break;
185 case 'C':
186 params.comp = genimg_get_comp_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600187 if (params.comp < 0) {
188 show_valid_options(IH_COMP);
Simon Glass15310342016-02-22 22:55:37 -0700189 usage("Invalid compression type");
Simon Glass51f03e62016-06-30 10:52:19 -0600190 }
Simon Glassa02221f2016-02-22 22:55:34 -0700191 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700192 case 'd':
193 params.datafile = optarg;
194 params.dflag = 1;
195 break;
Simon Glass07450082016-02-22 22:55:35 -0700196 case 'D':
197 params.dtc = optarg;
198 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700199 case 'e':
200 params.ep = strtoull(optarg, &ptr, 16);
201 if (*ptr) {
202 fprintf(stderr, "%s: invalid entry point %s\n",
203 params.cmdname, optarg);
204 exit(EXIT_FAILURE);
205 }
206 params.eflag = 1;
207 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700208 case 'E':
209 params.external_data = true;
210 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700211 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700212 datafile = optarg;
213 params.auto_its = !strcmp(datafile, "auto");
Simon Glassa02221f2016-02-22 22:55:34 -0700214 /* no break */
215 case 'F':
216 /*
217 * The flattened image tree (FIT) format
218 * requires a flattened device tree image type
219 */
220 params.type = IH_TYPE_FLATDT;
221 params.fflag = 1;
222 break;
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100223 case 'i':
224 params.fit_ramdisk = optarg;
225 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700226 case 'k':
227 params.keydir = optarg;
228 break;
229 case 'K':
230 params.keydest = optarg;
231 break;
Simon Glass07450082016-02-22 22:55:35 -0700232 case 'l':
233 params.lflag = 1;
234 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700235 case 'n':
236 params.imagename = optarg;
237 break;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600238 case 'N':
239 params.engine_id = optarg;
240 break;
Simon Glass07450082016-02-22 22:55:35 -0700241 case 'O':
242 params.os = genimg_get_os_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600243 if (params.os < 0) {
244 show_valid_options(IH_OS);
Simon Glass15310342016-02-22 22:55:37 -0700245 usage("Invalid operating system");
Simon Glass51f03e62016-06-30 10:52:19 -0600246 }
Simon Glass07450082016-02-22 22:55:35 -0700247 break;
Teddy Reedf8f91072016-06-09 19:38:02 -0700248 case 'p':
249 params.external_offset = strtoull(optarg, &ptr, 16);
250 if (*ptr) {
251 fprintf(stderr, "%s: invalid offset size %s\n",
252 params.cmdname, optarg);
253 exit(EXIT_FAILURE);
254 }
Teddy Reedb6fefa72016-07-11 22:54:26 -0700255 break;
Simon Glassbd6e1422016-05-01 13:55:38 -0600256 case 'q':
257 params.quiet = 1;
258 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700259 case 'r':
260 params.require_keys = 1;
261 break;
262 case 'R':
263 /*
264 * This entry is for the second configuration
265 * file, if only one is not enough.
266 */
267 params.imagename2 = optarg;
268 break;
269 case 's':
270 params.skipcpy = 1;
271 break;
Simon Glass07450082016-02-22 22:55:35 -0700272 case 'T':
Baruch Siach79aa33c2017-06-29 20:37:08 +0300273 if (strcmp(optarg, "list") == 0) {
274 show_valid_options(IH_TYPE);
275 exit(EXIT_SUCCESS);
276 }
Simon Glassd505a092016-02-22 22:55:48 -0700277 type = genimg_get_type_id(optarg);
278 if (type < 0) {
Simon Glassf24e1052016-06-30 10:52:18 -0600279 show_valid_options(IH_TYPE);
Simon Glass15310342016-02-22 22:55:37 -0700280 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700281 }
282 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700283 case 'v':
284 params.vflag++;
285 break;
286 case 'V':
287 printf("mkimage version %s\n", PLAIN_VERSION);
288 exit(EXIT_SUCCESS);
289 case 'x':
290 params.xflag++;
291 break;
292 default:
Simon Glass15310342016-02-22 22:55:37 -0700293 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000294 }
wdenk5b1d7132002-11-03 00:07:02 +0000295 }
296
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200297 /* The last parameter is expected to be the imagefile */
298 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200299 params.imagefile = argv[optind];
300
Simon Glassd505a092016-02-22 22:55:48 -0700301 /*
302 * For auto-generated FIT images we need to know the image type to put
303 * in the FIT, which is separate from the file's image type (which
304 * will always be IH_TYPE_FLATDT in this case).
305 */
306 if (params.type == IH_TYPE_FLATDT) {
Simon Glass20deadd2016-06-30 10:52:07 -0600307 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600308 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700309 if (!params.auto_its)
310 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600311 else if (!params.datafile)
312 usage("Missing data file for auto-FIT (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700313 } else if (type != IH_TYPE_INVALID) {
Alex Kiernan8c842872018-04-22 05:11:17 +0000314 if (type == IH_TYPE_SCRIPT && !params.datafile)
315 usage("Missing data file for script (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700316 params.type = type;
317 }
318
319 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700320 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700321}
322
Simon Glass0b443de2016-02-22 22:55:33 -0700323int main(int argc, char **argv)
324{
325 int ifd = -1;
326 struct stat sbuf;
327 char *ptr;
328 int retval = 0;
329 struct image_type_params *tparams = NULL;
330 int pad_len = 0;
331 int dfd;
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200332 size_t map_len;
Simon Glass0b443de2016-02-22 22:55:33 -0700333
334 params.cmdname = *argv;
335 params.addr = 0;
336 params.ep = 0;
337
338 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000339
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530340 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200341 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530342 if (tparams == NULL) {
343 fprintf (stderr, "%s: unsupported type %s\n",
344 params.cmdname, genimg_get_type_name(params.type));
345 exit (EXIT_FAILURE);
346 }
347
348 /*
349 * check the passed arguments parameters meets the requirements
350 * as per image type to be generated/listed
351 */
352 if (tparams->check_params)
353 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700354 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530355
356 if (!params.eflag) {
357 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000358 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530359 if (params.xflag)
360 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000361 }
362
Peter Tyserc81296c2009-11-24 16:42:10 -0600363 if (params.fflag){
364 if (tparams->fflag_handle)
365 /*
366 * in some cases, some additional processing needs
367 * to be done if fflag is defined
368 *
369 * For ex. fit_handle_file for Fit file support
370 */
371 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000372
Peter Tyserc81296c2009-11-24 16:42:10 -0600373 if (retval != EXIT_SUCCESS)
374 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000375 }
376
Peter Tyserc81296c2009-11-24 16:42:10 -0600377 if (params.lflag || params.fflag) {
378 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
379 } else {
380 ifd = open (params.imagefile,
381 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
382 }
383
384 if (ifd < 0) {
385 fprintf (stderr, "%s: Can't open %s: %s\n",
386 params.cmdname, params.imagefile,
387 strerror(errno));
388 exit (EXIT_FAILURE);
389 }
390
391 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000392 /*
393 * list header information of existing image
394 */
395 if (fstat(ifd, &sbuf) < 0) {
396 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530397 params.cmdname, params.imagefile,
398 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000399 exit (EXIT_FAILURE);
400 }
401
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530402 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000403 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530404 "%s: Bad size: \"%s\" is not valid image\n",
405 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000406 exit (EXIT_FAILURE);
407 }
408
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400409 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
410 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000411 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530412 params.cmdname, params.imagefile,
413 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000414 exit (EXIT_FAILURE);
415 }
416
Jordan Hand73c02e52019-04-09 10:36:09 -0700417 if (params.fflag) {
418 /*
419 * Verifies the header format based on the expected header for image
420 * type in tparams
421 */
422 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
423 tparams, &params);
424 } else {
425 /**
426 * When listing the image, we are not given the image type. Simply check all
427 * image types to find one that matches our header
428 */
429 retval = imagetool_verify_print_header(ptr, &sbuf,
430 tparams, &params);
431 }
wdenk5b1d7132002-11-03 00:07:02 +0000432
wdenk5b1d7132002-11-03 00:07:02 +0000433 (void) munmap((void *)ptr, sbuf.st_size);
434 (void) close (ifd);
435
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530436 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000437 }
438
Marek Vasut34633142015-12-07 18:01:54 +0100439 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200440 dfd = open(params.datafile, O_RDONLY | O_BINARY);
441 if (dfd < 0) {
442 fprintf(stderr, "%s: Can't open %s: %s\n",
443 params.cmdname, params.datafile,
444 strerror(errno));
445 exit(EXIT_FAILURE);
446 }
Simon Glass92a655c2015-06-23 15:39:12 -0600447
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200448 if (fstat(dfd, &sbuf) < 0) {
449 fprintf(stderr, "%s: Can't stat %s: %s\n",
450 params.cmdname, params.datafile,
451 strerror(errno));
452 exit(EXIT_FAILURE);
453 }
Simon Glass92a655c2015-06-23 15:39:12 -0600454
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200455 params.file_size = sbuf.st_size + tparams->header_size;
456 close(dfd);
457 }
Simon Glass92a655c2015-06-23 15:39:12 -0600458
wdenk5b1d7132002-11-03 00:07:02 +0000459 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000460 * In case there an header with a variable
461 * length will be added, the corresponding
462 * function is called. This is responsible to
463 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000464 */
Stefano Babicf0662102011-09-15 23:50:16 +0000465 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200466 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000467 else
468 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000469
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530470 if (write(ifd, tparams->hdr, tparams->header_size)
471 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000472 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530473 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000474 exit (EXIT_FAILURE);
475 }
476
Christian Rieschd1be8f92011-12-09 09:47:38 +0000477 if (!params.skipcpy) {
478 if (params.type == IH_TYPE_MULTI ||
479 params.type == IH_TYPE_SCRIPT) {
480 char *file = params.datafile;
481 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000482
Christian Rieschd1be8f92011-12-09 09:47:38 +0000483 for (;;) {
484 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000485
Christian Rieschd1be8f92011-12-09 09:47:38 +0000486 if (file) {
487 if ((sep = strchr(file, ':')) != NULL) {
488 *sep = '\0';
489 }
490
491 if (stat (file, &sbuf) < 0) {
492 fprintf (stderr, "%s: Can't stat %s: %s\n",
493 params.cmdname, file, strerror(errno));
494 exit (EXIT_FAILURE);
495 }
496 size = cpu_to_uimage (sbuf.st_size);
497 } else {
498 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000499 }
500
Christian Rieschd1be8f92011-12-09 09:47:38 +0000501 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
502 fprintf (stderr, "%s: Write error on %s: %s\n",
503 params.cmdname, params.imagefile,
504 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000505 exit (EXIT_FAILURE);
506 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000507
508 if (!file) {
509 break;
510 }
511
512 if (sep) {
513 *sep = ':';
514 file = sep + 1;
515 } else {
516 file = NULL;
517 }
wdenk5b1d7132002-11-03 00:07:02 +0000518 }
519
Christian Rieschd1be8f92011-12-09 09:47:38 +0000520 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000521
Christian Rieschd1be8f92011-12-09 09:47:38 +0000522 for (;;) {
523 char *sep = strchr(file, ':');
524 if (sep) {
525 *sep = '\0';
526 copy_file (ifd, file, 1);
527 *sep++ = ':';
528 file = sep;
529 } else {
530 copy_file (ifd, file, 0);
531 break;
532 }
wdenk5b1d7132002-11-03 00:07:02 +0000533 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000534 } else if (params.type == IH_TYPE_PBLIMAGE) {
535 /* PBL has special Image format, implements its' own */
536 pbl_load_uboot(ifd, &params);
Alexander Graf6915dcf2018-04-13 14:18:52 +0200537 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
538 /* Image file is meta, walk through actual targets */
539 int ret;
540
541 ret = zynqmpbif_copy_image(ifd, &params);
542 if (ret)
543 return ret;
Peng Fana2b96ec2018-10-16 04:50:30 +0000544 } else if (params.type == IH_TYPE_IMX8IMAGE) {
545 /* i.MX8/8X has special Image format */
546 int ret;
547
548 ret = imx8image_copy_image(ifd, &params);
549 if (ret)
550 return ret;
Peng Fan6609c262018-11-20 10:19:36 +0000551 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
552 /* i.MX8M has special Image format */
553 int ret;
554
555 ret = imx8mimage_copy_image(ifd, &params);
556 if (ret)
557 return ret;
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800558 } else if ((params.type == IH_TYPE_RKSD) ||
559 (params.type == IH_TYPE_RKSPI)) {
560 /* Rockchip has special Image format */
561 int ret;
562
563 ret = rockchip_copy_image(ifd, &params);
564 if (ret)
565 return ret;
Christian Rieschd1be8f92011-12-09 09:47:38 +0000566 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200567 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000568 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100569 if (params.type == IH_TYPE_FIRMWARE_IVT) {
570 /* Add alignment and IVT */
Kever Yang29e7ab02020-03-30 11:56:19 +0800571 uint32_t aligned_filesize = ALIGN(params.file_size,
572 0x1000);
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100573 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
574 params.addr, 0, 0, 0, params.addr
575 + aligned_filesize
576 - tparams->header_size,
577 params.addr + aligned_filesize
578 - tparams->header_size
579 + 0x20, 0 };
580 int i = params.file_size;
581 for (; i < aligned_filesize; i++) {
Sven Ebenfeldb4e923a2017-01-17 19:36:51 +0100582 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100583 fprintf(stderr,
584 "%s: Write error on %s: %s\n",
585 params.cmdname,
586 params.imagefile,
587 strerror(errno));
588 exit(EXIT_FAILURE);
589 }
590 }
591 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
592 != sizeof(flash_header_v2_t)) {
593 fprintf(stderr, "%s: Write error on %s: %s\n",
594 params.cmdname,
595 params.imagefile,
596 strerror(errno));
597 exit(EXIT_FAILURE);
598 }
599 }
wdenk5b1d7132002-11-03 00:07:02 +0000600 }
601
602 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530603#if defined(_POSIX_SYNCHRONIZED_IO) && \
604 !defined(__sun__) && \
605 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200606 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530607 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000608 (void) fdatasync (ifd);
609#else
610 (void) fsync (ifd);
611#endif
612
613 if (fstat(ifd, &sbuf) < 0) {
614 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530615 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000616 exit (EXIT_FAILURE);
617 }
Simon Glass92a655c2015-06-23 15:39:12 -0600618 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000619
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200620 map_len = sbuf.st_size;
621 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400622 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000623 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530624 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000625 exit (EXIT_FAILURE);
626 }
627
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530628 /* Setup the image header as per input image type*/
629 if (tparams->set_header)
630 tparams->set_header (ptr, &sbuf, ifd, &params);
631 else {
632 fprintf (stderr, "%s: Can't set header for %s: %s\n",
633 params.cmdname, tparams->name, strerror(errno));
634 exit (EXIT_FAILURE);
635 }
wdenk5b1d7132002-11-03 00:07:02 +0000636
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530637 /* Print the image information by processing image header */
638 if (tparams->print_header)
639 tparams->print_header (ptr);
640 else {
Guillaume GARDET004d0092018-03-30 10:28:19 +0200641 fprintf (stderr, "%s: Can't print header for %s\n",
642 params.cmdname, tparams->name);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530643 }
wdenk5b1d7132002-11-03 00:07:02 +0000644
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200645 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000646
647 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530648#if defined(_POSIX_SYNCHRONIZED_IO) && \
649 !defined(__sun__) && \
650 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200651 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530652 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000653 (void) fdatasync (ifd);
654#else
655 (void) fsync (ifd);
656#endif
657
658 if (close(ifd)) {
659 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530660 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000661 exit (EXIT_FAILURE);
662 }
663
664 exit (EXIT_SUCCESS);
665}
666
667static void
668copy_file (int ifd, const char *datafile, int pad)
669{
670 int dfd;
671 struct stat sbuf;
672 unsigned char *ptr;
673 int tail;
674 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200675 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000676 int offset = 0;
677 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200678 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000679
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200680 memset(zeros, 0, sizeof(zeros));
681
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530682 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000683 fprintf (stderr, "Adding Image %s\n", datafile);
684 }
685
wdenkef1464c2003-10-08 22:14:02 +0000686 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000687 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530688 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000689 exit (EXIT_FAILURE);
690 }
691
692 if (fstat(dfd, &sbuf) < 0) {
693 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530694 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000695 exit (EXIT_FAILURE);
696 }
697
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400698 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
699 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000700 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530701 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000702 exit (EXIT_FAILURE);
703 }
704
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530705 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000706 unsigned char *p = NULL;
707 /*
708 * XIP: do not append the image_header_t at the
709 * beginning of the file, but consume the space
710 * reserved for it.
711 */
712
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530713 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000714 fprintf (stderr,
715 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530716 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000717 exit (EXIT_FAILURE);
718 }
719
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530720 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000721 if ( *p != 0xff ) {
722 fprintf (stderr,
723 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530724 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000725 exit (EXIT_FAILURE);
726 }
727 }
728
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530729 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000730 }
731
732 size = sbuf.st_size - offset;
733 if (write(ifd, ptr + offset, size) != size) {
734 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530735 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000736 exit (EXIT_FAILURE);
737 }
738
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200739 tail = size % 4;
740 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000741
742 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
743 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530744 params.cmdname, params.imagefile,
745 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000746 exit (EXIT_FAILURE);
747 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200748 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600749 while (pad > 0) {
750 int todo = sizeof(zeros);
751
752 if (todo > pad)
753 todo = pad;
754 if (write(ifd, (char *)&zeros, todo) != todo) {
755 fprintf(stderr, "%s: Write error on %s: %s\n",
756 params.cmdname, params.imagefile,
757 strerror(errno));
758 exit(EXIT_FAILURE);
759 }
760 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200761 }
wdenk5b1d7132002-11-03 00:07:02 +0000762 }
763
764 (void) munmap((void *)ptr, sbuf.st_size);
765 (void) close (dfd);
766}