blob: 5f51d2cc89fa00f6cacee12bf01d924a209bcd02 [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
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010010#include "mkimage.h"
Sven Ebenfeldd21bd692016-11-06 16:37:56 +010011#include "imximage.h"
wdenk5b1d7132002-11-03 00:07:02 +000012#include <image.h>
Wolfgang Denk976b38c2011-02-12 10:37:11 +010013#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000014
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053015static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000016
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053017/* parameters initialized by core will be used by the image type code */
Simon Glasscc7a6442016-02-22 22:55:38 -070018static struct image_tool_params params = {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053019 .os = IH_OS_LINUX,
20 .arch = IH_ARCH_PPC,
21 .type = IH_TYPE_KERNEL,
22 .comp = IH_COMP_GZIP,
23 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
Wolfgang Denk04387d22010-03-27 23:37:46 +010024 .imagename = "",
Shaohui Xie5d898a02012-08-10 02:49:35 +000025 .imagename2 = "",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053026};
wdenk5b1d7132002-11-03 00:07:02 +000027
Simon Glass30664222016-06-30 10:52:17 -060028static enum ih_category cur_category;
29
30static int h_compare_category_name(const void *vtype1, const void *vtype2)
31{
32 const int *type1 = vtype1;
33 const int *type2 = vtype2;
34 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
35 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
36
37 return strcmp(name1, name2);
38}
39
Simon Glassf24e1052016-06-30 10:52:18 -060040static int show_valid_options(enum ih_category category)
Simon Glass30664222016-06-30 10:52:17 -060041{
42 int *order;
43 int count;
44 int item;
45 int i;
46
47 count = genimg_get_cat_count(category);
48 order = calloc(count, sizeof(*order));
49 if (!order)
50 return -ENOMEM;
51
52 /* Sort the names in order of short name for easier reading */
53 for (item = 0; item < count; item++)
54 order[item] = item;
55 cur_category = category;
56 qsort(order, count, sizeof(int), h_compare_category_name);
57
58 fprintf(stderr, "\nInvalid %s, supported are:\n",
59 genimg_get_cat_desc(category));
60 for (i = 0; i < count; i++) {
61 item = order[i];
62 fprintf(stderr, "\t%-15s %s\n",
63 genimg_get_cat_short_name(category, item),
64 genimg_get_cat_name(category, item));
65 }
66 fprintf(stderr, "\n");
Simon Glass0cd82e22016-10-27 17:54:03 -060067 free(order);
Simon Glass30664222016-06-30 10:52:17 -060068
69 return 0;
70}
71
Simon Glass15310342016-02-22 22:55:37 -070072static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -070073{
Simon Glass15310342016-02-22 22:55:37 -070074 fprintf(stderr, "Error: %s\n", msg);
Simon Glassb0a487a2016-02-22 22:55:36 -070075 fprintf(stderr, "Usage: %s -l image\n"
76 " -l ==> list image header information\n",
77 params.cmdname);
78 fprintf(stderr,
79 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
80 " -A ==> set architecture to 'arch'\n"
81 " -O ==> set operating system to 'os'\n"
82 " -T ==> set image type to 'type'\n"
83 " -C ==> set compression type 'comp'\n"
84 " -a ==> set load address to 'addr' (hex)\n"
85 " -e ==> set entry point to 'ep' (hex)\n"
86 " -n ==> set image name to 'name'\n"
87 " -d ==> use image data from 'datafile'\n"
88 " -x ==> set XIP (execute in place)\n",
89 params.cmdname);
90 fprintf(stderr,
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +010091 " %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 -070092 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glassb0a487a2016-02-22 22:55:36 -070093 params.cmdname);
94 fprintf(stderr,
95 " -D => set all options for device tree compiler\n"
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +010096 " -f => input filename for FIT source\n"
97 " -i => input filename for ramdisk file\n");
Simon Glassb0a487a2016-02-22 22:55:36 -070098#ifdef CONFIG_FIT_SIGNATURE
99 fprintf(stderr,
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600100 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700101 " -E => place data outside of the FIT structure\n"
Simon Glassb0a487a2016-02-22 22:55:36 -0700102 " -k => set directory containing private keys\n"
103 " -K => write public keys to this .dtb file\n"
104 " -c => add comment in signature node\n"
105 " -F => re-sign existing FIT image\n"
Teddy Reedf8f91072016-06-09 19:38:02 -0700106 " -p => place external data at a static position\n"
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600107 " -r => mark keys used as 'required' in dtb\n"
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300108 " -N => openssl engine to use for signing\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700109#else
110 fprintf(stderr,
111 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
112#endif
113 fprintf(stderr, " %s -V ==> print version information and exit\n",
114 params.cmdname);
Baruch Siach79aa33c2017-06-29 20:37:08 +0300115 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700116
117 exit(EXIT_FAILURE);
118}
119
Simon Glassfb4cce02016-02-22 22:55:52 -0700120static int add_content(int type, const char *fname)
121{
122 struct content_info *cont;
123
124 cont = calloc(1, sizeof(*cont));
125 if (!cont)
126 return -1;
127 cont->type = type;
128 cont->fname = fname;
129 if (params.content_tail)
130 params.content_tail->next = cont;
131 else
132 params.content_head = cont;
133 params.content_tail = cont;
134
135 return 0;
136}
137
Simon Glass0b443de2016-02-22 22:55:33 -0700138static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000139{
Peter Tysera2513e22010-04-04 22:36:03 -0500140 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700141 int type = IH_TYPE_INVALID;
142 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700143 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000144
Simon Glassa02221f2016-02-22 22:55:34 -0700145 while ((opt = getopt(argc, argv,
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600146 "a:A: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 -0700147 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700148 case 'a':
149 params.addr = strtoull(optarg, &ptr, 16);
150 if (*ptr) {
151 fprintf(stderr, "%s: invalid load address %s\n",
152 params.cmdname, optarg);
153 exit(EXIT_FAILURE);
154 }
Simon Glassa02221f2016-02-22 22:55:34 -0700155 break;
156 case 'A':
157 params.arch = genimg_get_arch_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600158 if (params.arch < 0) {
159 show_valid_options(IH_ARCH);
Simon Glass15310342016-02-22 22:55:37 -0700160 usage("Invalid architecture");
Simon Glass51f03e62016-06-30 10:52:19 -0600161 }
Simon Glassa02221f2016-02-22 22:55:34 -0700162 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700163 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200164 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200165 fprintf(stderr,
166 "%s: Out of memory adding content '%s'",
167 params.cmdname, optarg);
168 exit(EXIT_FAILURE);
169 }
Simon Glassfb4cce02016-02-22 22:55:52 -0700170 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700171 case 'c':
172 params.comment = optarg;
173 break;
174 case 'C':
175 params.comp = genimg_get_comp_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600176 if (params.comp < 0) {
177 show_valid_options(IH_COMP);
Simon Glass15310342016-02-22 22:55:37 -0700178 usage("Invalid compression type");
Simon Glass51f03e62016-06-30 10:52:19 -0600179 }
Simon Glassa02221f2016-02-22 22:55:34 -0700180 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700181 case 'd':
182 params.datafile = optarg;
183 params.dflag = 1;
184 break;
Simon Glass07450082016-02-22 22:55:35 -0700185 case 'D':
186 params.dtc = optarg;
187 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700188 case 'e':
189 params.ep = strtoull(optarg, &ptr, 16);
190 if (*ptr) {
191 fprintf(stderr, "%s: invalid entry point %s\n",
192 params.cmdname, optarg);
193 exit(EXIT_FAILURE);
194 }
195 params.eflag = 1;
196 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700197 case 'E':
198 params.external_data = true;
199 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700200 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700201 datafile = optarg;
202 params.auto_its = !strcmp(datafile, "auto");
Simon Glassa02221f2016-02-22 22:55:34 -0700203 /* no break */
204 case 'F':
205 /*
206 * The flattened image tree (FIT) format
207 * requires a flattened device tree image type
208 */
209 params.type = IH_TYPE_FLATDT;
210 params.fflag = 1;
211 break;
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100212 case 'i':
213 params.fit_ramdisk = optarg;
214 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700215 case 'k':
216 params.keydir = optarg;
217 break;
218 case 'K':
219 params.keydest = optarg;
220 break;
Simon Glass07450082016-02-22 22:55:35 -0700221 case 'l':
222 params.lflag = 1;
223 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700224 case 'n':
225 params.imagename = optarg;
226 break;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600227 case 'N':
228 params.engine_id = optarg;
229 break;
Simon Glass07450082016-02-22 22:55:35 -0700230 case 'O':
231 params.os = genimg_get_os_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600232 if (params.os < 0) {
233 show_valid_options(IH_OS);
Simon Glass15310342016-02-22 22:55:37 -0700234 usage("Invalid operating system");
Simon Glass51f03e62016-06-30 10:52:19 -0600235 }
Simon Glass07450082016-02-22 22:55:35 -0700236 break;
Teddy Reedf8f91072016-06-09 19:38:02 -0700237 case 'p':
238 params.external_offset = strtoull(optarg, &ptr, 16);
239 if (*ptr) {
240 fprintf(stderr, "%s: invalid offset size %s\n",
241 params.cmdname, optarg);
242 exit(EXIT_FAILURE);
243 }
Teddy Reedb6fefa72016-07-11 22:54:26 -0700244 break;
Simon Glassbd6e1422016-05-01 13:55:38 -0600245 case 'q':
246 params.quiet = 1;
247 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700248 case 'r':
249 params.require_keys = 1;
250 break;
251 case 'R':
252 /*
253 * This entry is for the second configuration
254 * file, if only one is not enough.
255 */
256 params.imagename2 = optarg;
257 break;
258 case 's':
259 params.skipcpy = 1;
260 break;
Simon Glass07450082016-02-22 22:55:35 -0700261 case 'T':
Baruch Siach79aa33c2017-06-29 20:37:08 +0300262 if (strcmp(optarg, "list") == 0) {
263 show_valid_options(IH_TYPE);
264 exit(EXIT_SUCCESS);
265 }
Simon Glassd505a092016-02-22 22:55:48 -0700266 type = genimg_get_type_id(optarg);
267 if (type < 0) {
Simon Glassf24e1052016-06-30 10:52:18 -0600268 show_valid_options(IH_TYPE);
Simon Glass15310342016-02-22 22:55:37 -0700269 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700270 }
271 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700272 case 'v':
273 params.vflag++;
274 break;
275 case 'V':
276 printf("mkimage version %s\n", PLAIN_VERSION);
277 exit(EXIT_SUCCESS);
278 case 'x':
279 params.xflag++;
280 break;
281 default:
Simon Glass15310342016-02-22 22:55:37 -0700282 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000283 }
wdenk5b1d7132002-11-03 00:07:02 +0000284 }
285
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200286 /* The last parameter is expected to be the imagefile */
287 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200288 params.imagefile = argv[optind];
289
Simon Glassd505a092016-02-22 22:55:48 -0700290 /*
291 * For auto-generated FIT images we need to know the image type to put
292 * in the FIT, which is separate from the file's image type (which
293 * will always be IH_TYPE_FLATDT in this case).
294 */
295 if (params.type == IH_TYPE_FLATDT) {
Simon Glass20deadd2016-06-30 10:52:07 -0600296 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600297 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700298 if (!params.auto_its)
299 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600300 else if (!params.datafile)
301 usage("Missing data file for auto-FIT (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700302 } else if (type != IH_TYPE_INVALID) {
Alex Kiernan8c842872018-04-22 05:11:17 +0000303 if (type == IH_TYPE_SCRIPT && !params.datafile)
304 usage("Missing data file for script (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700305 params.type = type;
306 }
307
308 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700309 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700310}
311
Simon Glass0b443de2016-02-22 22:55:33 -0700312int main(int argc, char **argv)
313{
314 int ifd = -1;
315 struct stat sbuf;
316 char *ptr;
317 int retval = 0;
318 struct image_type_params *tparams = NULL;
319 int pad_len = 0;
320 int dfd;
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200321 size_t map_len;
Simon Glass0b443de2016-02-22 22:55:33 -0700322
323 params.cmdname = *argv;
324 params.addr = 0;
325 params.ep = 0;
326
327 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000328
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530329 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200330 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530331 if (tparams == NULL) {
332 fprintf (stderr, "%s: unsupported type %s\n",
333 params.cmdname, genimg_get_type_name(params.type));
334 exit (EXIT_FAILURE);
335 }
336
337 /*
338 * check the passed arguments parameters meets the requirements
339 * as per image type to be generated/listed
340 */
341 if (tparams->check_params)
342 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700343 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530344
345 if (!params.eflag) {
346 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000347 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530348 if (params.xflag)
349 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000350 }
351
Peter Tyserc81296c2009-11-24 16:42:10 -0600352 if (params.fflag){
353 if (tparams->fflag_handle)
354 /*
355 * in some cases, some additional processing needs
356 * to be done if fflag is defined
357 *
358 * For ex. fit_handle_file for Fit file support
359 */
360 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000361
Peter Tyserc81296c2009-11-24 16:42:10 -0600362 if (retval != EXIT_SUCCESS)
363 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000364 }
365
Peter Tyserc81296c2009-11-24 16:42:10 -0600366 if (params.lflag || params.fflag) {
367 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
368 } else {
369 ifd = open (params.imagefile,
370 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
371 }
372
373 if (ifd < 0) {
374 fprintf (stderr, "%s: Can't open %s: %s\n",
375 params.cmdname, params.imagefile,
376 strerror(errno));
377 exit (EXIT_FAILURE);
378 }
379
380 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000381 /*
382 * list header information of existing image
383 */
384 if (fstat(ifd, &sbuf) < 0) {
385 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530386 params.cmdname, params.imagefile,
387 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000388 exit (EXIT_FAILURE);
389 }
390
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530391 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000392 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530393 "%s: Bad size: \"%s\" is not valid image\n",
394 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000395 exit (EXIT_FAILURE);
396 }
397
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400398 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
399 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000400 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530401 params.cmdname, params.imagefile,
402 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000403 exit (EXIT_FAILURE);
404 }
405
Jordan Hand73c02e52019-04-09 10:36:09 -0700406 if (params.fflag) {
407 /*
408 * Verifies the header format based on the expected header for image
409 * type in tparams
410 */
411 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
412 tparams, &params);
413 } else {
414 /**
415 * When listing the image, we are not given the image type. Simply check all
416 * image types to find one that matches our header
417 */
418 retval = imagetool_verify_print_header(ptr, &sbuf,
419 tparams, &params);
420 }
wdenk5b1d7132002-11-03 00:07:02 +0000421
wdenk5b1d7132002-11-03 00:07:02 +0000422 (void) munmap((void *)ptr, sbuf.st_size);
423 (void) close (ifd);
424
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530425 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000426 }
427
Marek Vasut34633142015-12-07 18:01:54 +0100428 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200429 dfd = open(params.datafile, O_RDONLY | O_BINARY);
430 if (dfd < 0) {
431 fprintf(stderr, "%s: Can't open %s: %s\n",
432 params.cmdname, params.datafile,
433 strerror(errno));
434 exit(EXIT_FAILURE);
435 }
Simon Glass92a655c2015-06-23 15:39:12 -0600436
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200437 if (fstat(dfd, &sbuf) < 0) {
438 fprintf(stderr, "%s: Can't stat %s: %s\n",
439 params.cmdname, params.datafile,
440 strerror(errno));
441 exit(EXIT_FAILURE);
442 }
Simon Glass92a655c2015-06-23 15:39:12 -0600443
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200444 params.file_size = sbuf.st_size + tparams->header_size;
445 close(dfd);
446 }
Simon Glass92a655c2015-06-23 15:39:12 -0600447
wdenk5b1d7132002-11-03 00:07:02 +0000448 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000449 * In case there an header with a variable
450 * length will be added, the corresponding
451 * function is called. This is responsible to
452 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000453 */
Stefano Babicf0662102011-09-15 23:50:16 +0000454 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200455 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000456 else
457 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000458
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530459 if (write(ifd, tparams->hdr, tparams->header_size)
460 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000461 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530462 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000463 exit (EXIT_FAILURE);
464 }
465
Christian Rieschd1be8f92011-12-09 09:47:38 +0000466 if (!params.skipcpy) {
467 if (params.type == IH_TYPE_MULTI ||
468 params.type == IH_TYPE_SCRIPT) {
469 char *file = params.datafile;
470 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000471
Christian Rieschd1be8f92011-12-09 09:47:38 +0000472 for (;;) {
473 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000474
Christian Rieschd1be8f92011-12-09 09:47:38 +0000475 if (file) {
476 if ((sep = strchr(file, ':')) != NULL) {
477 *sep = '\0';
478 }
479
480 if (stat (file, &sbuf) < 0) {
481 fprintf (stderr, "%s: Can't stat %s: %s\n",
482 params.cmdname, file, strerror(errno));
483 exit (EXIT_FAILURE);
484 }
485 size = cpu_to_uimage (sbuf.st_size);
486 } else {
487 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000488 }
489
Christian Rieschd1be8f92011-12-09 09:47:38 +0000490 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
491 fprintf (stderr, "%s: Write error on %s: %s\n",
492 params.cmdname, params.imagefile,
493 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000494 exit (EXIT_FAILURE);
495 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000496
497 if (!file) {
498 break;
499 }
500
501 if (sep) {
502 *sep = ':';
503 file = sep + 1;
504 } else {
505 file = NULL;
506 }
wdenk5b1d7132002-11-03 00:07:02 +0000507 }
508
Christian Rieschd1be8f92011-12-09 09:47:38 +0000509 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000510
Christian Rieschd1be8f92011-12-09 09:47:38 +0000511 for (;;) {
512 char *sep = strchr(file, ':');
513 if (sep) {
514 *sep = '\0';
515 copy_file (ifd, file, 1);
516 *sep++ = ':';
517 file = sep;
518 } else {
519 copy_file (ifd, file, 0);
520 break;
521 }
wdenk5b1d7132002-11-03 00:07:02 +0000522 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000523 } else if (params.type == IH_TYPE_PBLIMAGE) {
524 /* PBL has special Image format, implements its' own */
525 pbl_load_uboot(ifd, &params);
Alexander Graf6915dcf2018-04-13 14:18:52 +0200526 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
527 /* Image file is meta, walk through actual targets */
528 int ret;
529
530 ret = zynqmpbif_copy_image(ifd, &params);
531 if (ret)
532 return ret;
Peng Fana2b96ec2018-10-16 04:50:30 +0000533 } else if (params.type == IH_TYPE_IMX8IMAGE) {
534 /* i.MX8/8X has special Image format */
535 int ret;
536
537 ret = imx8image_copy_image(ifd, &params);
538 if (ret)
539 return ret;
Peng Fan6609c262018-11-20 10:19:36 +0000540 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
541 /* i.MX8M has special Image format */
542 int ret;
543
544 ret = imx8mimage_copy_image(ifd, &params);
545 if (ret)
546 return ret;
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800547 } else if ((params.type == IH_TYPE_RKSD) ||
548 (params.type == IH_TYPE_RKSPI)) {
549 /* Rockchip has special Image format */
550 int ret;
551
552 ret = rockchip_copy_image(ifd, &params);
553 if (ret)
554 return ret;
Christian Rieschd1be8f92011-12-09 09:47:38 +0000555 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200556 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000557 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100558 if (params.type == IH_TYPE_FIRMWARE_IVT) {
559 /* Add alignment and IVT */
560 uint32_t aligned_filesize = (params.file_size + 0x1000
561 - 1) & ~(0x1000 - 1);
562 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
563 params.addr, 0, 0, 0, params.addr
564 + aligned_filesize
565 - tparams->header_size,
566 params.addr + aligned_filesize
567 - tparams->header_size
568 + 0x20, 0 };
569 int i = params.file_size;
570 for (; i < aligned_filesize; i++) {
Sven Ebenfeldb4e923a2017-01-17 19:36:51 +0100571 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100572 fprintf(stderr,
573 "%s: Write error on %s: %s\n",
574 params.cmdname,
575 params.imagefile,
576 strerror(errno));
577 exit(EXIT_FAILURE);
578 }
579 }
580 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
581 != sizeof(flash_header_v2_t)) {
582 fprintf(stderr, "%s: Write error on %s: %s\n",
583 params.cmdname,
584 params.imagefile,
585 strerror(errno));
586 exit(EXIT_FAILURE);
587 }
588 }
wdenk5b1d7132002-11-03 00:07:02 +0000589 }
590
591 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530592#if defined(_POSIX_SYNCHRONIZED_IO) && \
593 !defined(__sun__) && \
594 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200595 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530596 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000597 (void) fdatasync (ifd);
598#else
599 (void) fsync (ifd);
600#endif
601
602 if (fstat(ifd, &sbuf) < 0) {
603 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530604 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000605 exit (EXIT_FAILURE);
606 }
Simon Glass92a655c2015-06-23 15:39:12 -0600607 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000608
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200609 map_len = sbuf.st_size;
610 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400611 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000612 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530613 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000614 exit (EXIT_FAILURE);
615 }
616
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530617 /* Setup the image header as per input image type*/
618 if (tparams->set_header)
619 tparams->set_header (ptr, &sbuf, ifd, &params);
620 else {
621 fprintf (stderr, "%s: Can't set header for %s: %s\n",
622 params.cmdname, tparams->name, strerror(errno));
623 exit (EXIT_FAILURE);
624 }
wdenk5b1d7132002-11-03 00:07:02 +0000625
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530626 /* Print the image information by processing image header */
627 if (tparams->print_header)
628 tparams->print_header (ptr);
629 else {
Guillaume GARDET004d0092018-03-30 10:28:19 +0200630 fprintf (stderr, "%s: Can't print header for %s\n",
631 params.cmdname, tparams->name);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530632 }
wdenk5b1d7132002-11-03 00:07:02 +0000633
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200634 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000635
636 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530637#if defined(_POSIX_SYNCHRONIZED_IO) && \
638 !defined(__sun__) && \
639 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200640 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530641 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000642 (void) fdatasync (ifd);
643#else
644 (void) fsync (ifd);
645#endif
646
647 if (close(ifd)) {
648 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530649 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000650 exit (EXIT_FAILURE);
651 }
652
653 exit (EXIT_SUCCESS);
654}
655
656static void
657copy_file (int ifd, const char *datafile, int pad)
658{
659 int dfd;
660 struct stat sbuf;
661 unsigned char *ptr;
662 int tail;
663 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200664 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000665 int offset = 0;
666 int size;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200667 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000668
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200669 memset(zeros, 0, sizeof(zeros));
670
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530671 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000672 fprintf (stderr, "Adding Image %s\n", datafile);
673 }
674
wdenkef1464c2003-10-08 22:14:02 +0000675 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000676 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530677 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000678 exit (EXIT_FAILURE);
679 }
680
681 if (fstat(dfd, &sbuf) < 0) {
682 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530683 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000684 exit (EXIT_FAILURE);
685 }
686
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400687 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
688 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000689 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530690 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000691 exit (EXIT_FAILURE);
692 }
693
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530694 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000695 unsigned char *p = NULL;
696 /*
697 * XIP: do not append the image_header_t at the
698 * beginning of the file, but consume the space
699 * reserved for it.
700 */
701
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530702 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000703 fprintf (stderr,
704 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530705 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000706 exit (EXIT_FAILURE);
707 }
708
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530709 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000710 if ( *p != 0xff ) {
711 fprintf (stderr,
712 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530713 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000714 exit (EXIT_FAILURE);
715 }
716 }
717
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530718 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000719 }
720
721 size = sbuf.st_size - offset;
722 if (write(ifd, ptr + offset, size) != size) {
723 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530724 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000725 exit (EXIT_FAILURE);
726 }
727
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200728 tail = size % 4;
729 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000730
731 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
732 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530733 params.cmdname, params.imagefile,
734 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000735 exit (EXIT_FAILURE);
736 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200737 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600738 while (pad > 0) {
739 int todo = sizeof(zeros);
740
741 if (todo > pad)
742 todo = pad;
743 if (write(ifd, (char *)&zeros, todo) != todo) {
744 fprintf(stderr, "%s: Write error on %s: %s\n",
745 params.cmdname, params.imagefile,
746 strerror(errno));
747 exit(EXIT_FAILURE);
748 }
749 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200750 }
wdenk5b1d7132002-11-03 00:07:02 +0000751 }
752
753 (void) munmap((void *)ptr, sbuf.st_size);
754 (void) close (dfd);
755}