blob: ff3024a8f172c8557bfaa02612d995bdcdd04f2d [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Bartlomiej Siekad7816fb2008-03-11 12:34:47 +01002 * (C) Copyright 2008 Semihalf
3 *
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +05304 * (C) Copyright 2000-2009
wdenk5b1d7132002-11-03 00:07:02 +00005 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
wdenkdb82c8e2004-02-26 23:01:04 +00007 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk5b1d7132002-11-03 00:07:02 +00009 */
10
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010011#include "mkimage.h"
wdenk5b1d7132002-11-03 00:07:02 +000012#include <image.h>
Wolfgang Denke97ab722011-02-12 10:37:11 +010013#include <version.h>
wdenk5b1d7132002-11-03 00:07:02 +000014
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053015static void copy_file(int, const char *, int);
wdenk5b1d7132002-11-03 00:07:02 +000016
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053017/* parameters initialized by core will be used by the image type code */
Simon Glass52322d62016-02-22 22:55:38 -070018static struct image_tool_params params = {
Prafulla Wadaskarfabf3cf2009-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 Denk23416b82010-03-27 23:37:46 +010024 .imagename = "",
Shaohui Xieea65fd82012-08-10 02:49:35 +000025 .imagename2 = "",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053026};
wdenk5b1d7132002-11-03 00:07:02 +000027
Simon Glass434a31b2015-06-23 15:38:25 -060028static int h_compare_image_name(const void *vtype1, const void *vtype2)
29{
30 const int *type1 = vtype1;
31 const int *type2 = vtype2;
32 const char *name1 = genimg_get_type_short_name(*type1);
33 const char *name2 = genimg_get_type_short_name(*type2);
34
35 return strcmp(name1, name2);
36}
37
38/* Show all image types supported by mkimage */
39static void show_image_types(void)
40{
41 struct image_type_params *tparams;
42 int order[IH_TYPE_COUNT];
43 int count;
44 int type;
45 int i;
46
47 /* Sort the names in order of short name for easier reading */
48 memset(order, '\0', sizeof(order));
49 for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
50 tparams = imagetool_get_type(type);
51 if (tparams)
52 order[count++] = type;
53 }
54 qsort(order, count, sizeof(int), h_compare_image_name);
55
56 fprintf(stderr, "\nInvalid image type. Supported image types:\n");
57 for (i = 0; i < count; i++) {
58 type = order[i];
59 tparams = imagetool_get_type(type);
60 if (tparams) {
61 fprintf(stderr, "\t%-15s %s\n",
62 genimg_get_type_short_name(type),
63 genimg_get_type_name(type));
64 }
65 }
66 fprintf(stderr, "\n");
67}
68
Simon Glass01168b32016-02-22 22:55:37 -070069static void usage(const char *msg)
Simon Glass99a7c262016-02-22 22:55:36 -070070{
Simon Glass01168b32016-02-22 22:55:37 -070071 fprintf(stderr, "Error: %s\n", msg);
Simon Glass99a7c262016-02-22 22:55:36 -070072 fprintf(stderr, "Usage: %s -l image\n"
73 " -l ==> list image header information\n",
74 params.cmdname);
75 fprintf(stderr,
76 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
77 " -A ==> set architecture to 'arch'\n"
78 " -O ==> set operating system to 'os'\n"
79 " -T ==> set image type to 'type'\n"
80 " -C ==> set compression type 'comp'\n"
81 " -a ==> set load address to 'addr' (hex)\n"
82 " -e ==> set entry point to 'ep' (hex)\n"
83 " -n ==> set image name to 'name'\n"
84 " -d ==> use image data from 'datafile'\n"
85 " -x ==> set XIP (execute in place)\n",
86 params.cmdname);
87 fprintf(stderr,
Andreas Bießmannf4001582016-05-01 03:01:27 +020088 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n"
89 " <dtb> file is used with -f auto, it may occour multiple times.\n",
Simon Glass99a7c262016-02-22 22:55:36 -070090 params.cmdname);
91 fprintf(stderr,
92 " -D => set all options for device tree compiler\n"
93 " -f => input filename for FIT source\n");
94#ifdef CONFIG_FIT_SIGNATURE
95 fprintf(stderr,
Teddy Reeda8457622016-06-09 19:38:02 -070096 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
97 " -E => place data outside of the FIT structure\n"
Simon Glass99a7c262016-02-22 22:55:36 -070098 " -k => set directory containing private keys\n"
99 " -K => write public keys to this .dtb file\n"
100 " -c => add comment in signature node\n"
101 " -F => re-sign existing FIT image\n"
Teddy Reeda8457622016-06-09 19:38:02 -0700102 " -p => place external data at a static position\n"
Simon Glass99a7c262016-02-22 22:55:36 -0700103 " -r => mark keys used as 'required' in dtb\n");
104#else
105 fprintf(stderr,
106 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
107#endif
108 fprintf(stderr, " %s -V ==> print version information and exit\n",
109 params.cmdname);
110 fprintf(stderr, "Use -T to see a list of available image types\n");
111
112 exit(EXIT_FAILURE);
113}
114
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700115static int add_content(int type, const char *fname)
116{
117 struct content_info *cont;
118
119 cont = calloc(1, sizeof(*cont));
120 if (!cont)
121 return -1;
122 cont->type = type;
123 cont->fname = fname;
124 if (params.content_tail)
125 params.content_tail->next = cont;
126 else
127 params.content_head = cont;
128 params.content_tail = cont;
129
130 return 0;
131}
132
Simon Glass7106bd42016-02-22 22:55:33 -0700133static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000134{
Peter Tyser632d9ca2010-04-04 22:36:03 -0500135 char *ptr;
Simon Glass04b46072016-02-22 22:55:48 -0700136 int type = IH_TYPE_INVALID;
137 char *datafile = NULL;
Simon Glasscf76cd22016-02-22 22:55:34 -0700138 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000139
Simon Glasscf76cd22016-02-22 22:55:34 -0700140 while ((opt = getopt(argc, argv,
Teddy Reeda8457622016-06-09 19:38:02 -0700141 "a:A:b:cC:d:D:e:Ef:Fk:K:ln:p:O:rR:qsT:vVx")) != -1) {
Simon Glasscf76cd22016-02-22 22:55:34 -0700142 switch (opt) {
Simon Glassf6fe5f62016-02-22 22:55:35 -0700143 case 'a':
144 params.addr = strtoull(optarg, &ptr, 16);
145 if (*ptr) {
146 fprintf(stderr, "%s: invalid load address %s\n",
147 params.cmdname, optarg);
148 exit(EXIT_FAILURE);
149 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700150 break;
151 case 'A':
152 params.arch = genimg_get_arch_id(optarg);
153 if (params.arch < 0)
Simon Glass01168b32016-02-22 22:55:37 -0700154 usage("Invalid architecture");
Simon Glasscf76cd22016-02-22 22:55:34 -0700155 break;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700156 case 'b':
Andreas Bießmannddd34772016-05-03 15:17:03 +0200157 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmannf4001582016-05-01 03:01:27 +0200158 fprintf(stderr,
159 "%s: Out of memory adding content '%s'",
160 params.cmdname, optarg);
161 exit(EXIT_FAILURE);
162 }
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700163 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700164 case 'c':
165 params.comment = optarg;
166 break;
167 case 'C':
168 params.comp = genimg_get_comp_id(optarg);
169 if (params.comp < 0)
Simon Glass01168b32016-02-22 22:55:37 -0700170 usage("Invalid compression type");
Simon Glasscf76cd22016-02-22 22:55:34 -0700171 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700172 case 'd':
173 params.datafile = optarg;
174 params.dflag = 1;
175 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700176 case 'D':
177 params.dtc = optarg;
178 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700179 case 'e':
180 params.ep = strtoull(optarg, &ptr, 16);
181 if (*ptr) {
182 fprintf(stderr, "%s: invalid entry point %s\n",
183 params.cmdname, optarg);
184 exit(EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000185 }
Simon Glasscf76cd22016-02-22 22:55:34 -0700186 params.eflag = 1;
187 break;
Simon Glassafd728c2016-02-22 22:55:53 -0700188 case 'E':
189 params.external_data = true;
190 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700191 case 'f':
Simon Glass88e31cb2016-02-22 22:55:51 -0700192 datafile = optarg;
193 params.auto_its = !strcmp(datafile, "auto");
Simon Glasscf76cd22016-02-22 22:55:34 -0700194 /* no break */
195 case 'F':
196 /*
197 * The flattened image tree (FIT) format
198 * requires a flattened device tree image type
199 */
Simon Glass04b46072016-02-22 22:55:48 -0700200 params.fit_image_type = params.type;
Simon Glasscf76cd22016-02-22 22:55:34 -0700201 params.type = IH_TYPE_FLATDT;
202 params.fflag = 1;
203 break;
204 case 'k':
205 params.keydir = optarg;
206 break;
207 case 'K':
208 params.keydest = optarg;
209 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700210 case 'l':
211 params.lflag = 1;
212 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700213 case 'n':
214 params.imagename = optarg;
215 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700216 case 'O':
217 params.os = genimg_get_os_id(optarg);
218 if (params.os < 0)
Simon Glass01168b32016-02-22 22:55:37 -0700219 usage("Invalid operating system");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700220 break;
Teddy Reeda8457622016-06-09 19:38:02 -0700221 case 'p':
222 params.external_offset = strtoull(optarg, &ptr, 16);
223 if (*ptr) {
224 fprintf(stderr, "%s: invalid offset size %s\n",
225 params.cmdname, optarg);
226 exit(EXIT_FAILURE);
227 }
Simon Glassda1805f2016-05-01 13:55:38 -0600228 case 'q':
229 params.quiet = 1;
230 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700231 case 'r':
232 params.require_keys = 1;
233 break;
234 case 'R':
235 /*
236 * This entry is for the second configuration
237 * file, if only one is not enough.
238 */
239 params.imagename2 = optarg;
240 break;
241 case 's':
242 params.skipcpy = 1;
243 break;
Simon Glassf6fe5f62016-02-22 22:55:35 -0700244 case 'T':
Simon Glass04b46072016-02-22 22:55:48 -0700245 type = genimg_get_type_id(optarg);
246 if (type < 0) {
Simon Glassf6fe5f62016-02-22 22:55:35 -0700247 show_image_types();
Simon Glass01168b32016-02-22 22:55:37 -0700248 usage("Invalid image type");
Simon Glassf6fe5f62016-02-22 22:55:35 -0700249 }
250 break;
Simon Glasscf76cd22016-02-22 22:55:34 -0700251 case 'v':
252 params.vflag++;
253 break;
254 case 'V':
255 printf("mkimage version %s\n", PLAIN_VERSION);
256 exit(EXIT_SUCCESS);
257 case 'x':
258 params.xflag++;
259 break;
260 default:
Simon Glass01168b32016-02-22 22:55:37 -0700261 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000262 }
wdenk5b1d7132002-11-03 00:07:02 +0000263 }
264
Andreas Bießmannddd34772016-05-03 15:17:03 +0200265 /* The last parameter is expected to be the imagefile */
266 if (optind < argc)
Andreas Bießmannf4001582016-05-01 03:01:27 +0200267 params.imagefile = argv[optind];
268
Simon Glass04b46072016-02-22 22:55:48 -0700269 /*
270 * For auto-generated FIT images we need to know the image type to put
271 * in the FIT, which is separate from the file's image type (which
272 * will always be IH_TYPE_FLATDT in this case).
273 */
274 if (params.type == IH_TYPE_FLATDT) {
275 params.fit_image_type = type;
Simon Glass88e31cb2016-02-22 22:55:51 -0700276 if (!params.auto_its)
277 params.datafile = datafile;
Simon Glass04b46072016-02-22 22:55:48 -0700278 } else if (type != IH_TYPE_INVALID) {
279 params.type = type;
280 }
281
282 if (!params.imagefile)
Simon Glass01168b32016-02-22 22:55:37 -0700283 usage("Missing output filename");
Simon Glass7106bd42016-02-22 22:55:33 -0700284}
285
286
287int main(int argc, char **argv)
288{
289 int ifd = -1;
290 struct stat sbuf;
291 char *ptr;
292 int retval = 0;
293 struct image_type_params *tparams = NULL;
294 int pad_len = 0;
295 int dfd;
296
297 params.cmdname = *argv;
298 params.addr = 0;
299 params.ep = 0;
300
301 process_args(argc, argv);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530302
303 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200304 tparams = imagetool_get_type(params.type);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530305 if (tparams == NULL) {
306 fprintf (stderr, "%s: unsupported type %s\n",
307 params.cmdname, genimg_get_type_name(params.type));
308 exit (EXIT_FAILURE);
309 }
310
311 /*
312 * check the passed arguments parameters meets the requirements
313 * as per image type to be generated/listed
314 */
315 if (tparams->check_params)
316 if (tparams->check_params (&params))
Simon Glass01168b32016-02-22 22:55:37 -0700317 usage("Bad parameters for image type");
wdenk5b1d7132002-11-03 00:07:02 +0000318
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530319 if (!params.eflag) {
320 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000321 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530322 if (params.xflag)
323 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000324 }
325
Peter Tyserb2f8b942009-11-24 16:42:10 -0600326 if (params.fflag){
327 if (tparams->fflag_handle)
328 /*
329 * in some cases, some additional processing needs
330 * to be done if fflag is defined
331 *
332 * For ex. fit_handle_file for Fit file support
333 */
334 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000335
Peter Tyserb2f8b942009-11-24 16:42:10 -0600336 if (retval != EXIT_SUCCESS)
337 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000338 }
339
Peter Tyserb2f8b942009-11-24 16:42:10 -0600340 if (params.lflag || params.fflag) {
341 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
342 } else {
343 ifd = open (params.imagefile,
344 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
345 }
346
347 if (ifd < 0) {
348 fprintf (stderr, "%s: Can't open %s: %s\n",
349 params.cmdname, params.imagefile,
350 strerror(errno));
351 exit (EXIT_FAILURE);
352 }
353
354 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000355 /*
356 * list header information of existing image
357 */
358 if (fstat(ifd, &sbuf) < 0) {
359 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530360 params.cmdname, params.imagefile,
361 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000362 exit (EXIT_FAILURE);
363 }
364
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530365 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000366 fprintf (stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530367 "%s: Bad size: \"%s\" is not valid image\n",
368 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000369 exit (EXIT_FAILURE);
370 }
371
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400372 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
373 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000374 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530375 params.cmdname, params.imagefile,
376 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000377 exit (EXIT_FAILURE);
378 }
379
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530380 /*
381 * scan through mkimage registry for all supported image types
382 * and verify the input image file header for match
383 * Print the image information for matched image type
384 * Returns the error code if not matched
385 */
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -0200386 retval = imagetool_verify_print_header(ptr, &sbuf,
387 tparams, &params);
wdenk5b1d7132002-11-03 00:07:02 +0000388
wdenk5b1d7132002-11-03 00:07:02 +0000389 (void) munmap((void *)ptr, sbuf.st_size);
390 (void) close (ifd);
391
Prafulla Wadaskar139cb082009-08-10 18:49:37 +0530392 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000393 }
394
Marek Vasut4cc86272015-12-07 18:01:54 +0100395 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert3edd0412015-12-04 00:11:23 +0200396 dfd = open(params.datafile, O_RDONLY | O_BINARY);
397 if (dfd < 0) {
398 fprintf(stderr, "%s: Can't open %s: %s\n",
399 params.cmdname, params.datafile,
400 strerror(errno));
401 exit(EXIT_FAILURE);
402 }
Simon Glass139651f2015-06-23 15:39:12 -0600403
Philippe De Swert3edd0412015-12-04 00:11:23 +0200404 if (fstat(dfd, &sbuf) < 0) {
405 fprintf(stderr, "%s: Can't stat %s: %s\n",
406 params.cmdname, params.datafile,
407 strerror(errno));
408 exit(EXIT_FAILURE);
409 }
Simon Glass139651f2015-06-23 15:39:12 -0600410
Philippe De Swert3edd0412015-12-04 00:11:23 +0200411 params.file_size = sbuf.st_size + tparams->header_size;
412 close(dfd);
413 }
Simon Glass139651f2015-06-23 15:39:12 -0600414
wdenk5b1d7132002-11-03 00:07:02 +0000415 /*
Stefano Babic4a963302011-09-15 23:50:16 +0000416 * In case there an header with a variable
417 * length will be added, the corresponding
418 * function is called. This is responsible to
419 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000420 */
Stefano Babic4a963302011-09-15 23:50:16 +0000421 if (tparams->vrec_header)
Stefano Babic6531bd92013-08-19 19:03:19 +0200422 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babic4a963302011-09-15 23:50:16 +0000423 else
424 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000425
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530426 if (write(ifd, tparams->hdr, tparams->header_size)
427 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000428 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530429 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000430 exit (EXIT_FAILURE);
431 }
432
Christian Rieschc14f2812011-12-09 09:47:38 +0000433 if (!params.skipcpy) {
434 if (params.type == IH_TYPE_MULTI ||
435 params.type == IH_TYPE_SCRIPT) {
436 char *file = params.datafile;
437 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000438
Christian Rieschc14f2812011-12-09 09:47:38 +0000439 for (;;) {
440 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000441
Christian Rieschc14f2812011-12-09 09:47:38 +0000442 if (file) {
443 if ((sep = strchr(file, ':')) != NULL) {
444 *sep = '\0';
445 }
446
447 if (stat (file, &sbuf) < 0) {
448 fprintf (stderr, "%s: Can't stat %s: %s\n",
449 params.cmdname, file, strerror(errno));
450 exit (EXIT_FAILURE);
451 }
452 size = cpu_to_uimage (sbuf.st_size);
453 } else {
454 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000455 }
456
Christian Rieschc14f2812011-12-09 09:47:38 +0000457 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
458 fprintf (stderr, "%s: Write error on %s: %s\n",
459 params.cmdname, params.imagefile,
460 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000461 exit (EXIT_FAILURE);
462 }
wdenk5b1d7132002-11-03 00:07:02 +0000463
Christian Rieschc14f2812011-12-09 09:47:38 +0000464 if (!file) {
465 break;
466 }
wdenk5b1d7132002-11-03 00:07:02 +0000467
Christian Rieschc14f2812011-12-09 09:47:38 +0000468 if (sep) {
469 *sep = ':';
470 file = sep + 1;
471 } else {
472 file = NULL;
473 }
wdenk5b1d7132002-11-03 00:07:02 +0000474 }
wdenk5b1d7132002-11-03 00:07:02 +0000475
Christian Rieschc14f2812011-12-09 09:47:38 +0000476 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000477
Christian Rieschc14f2812011-12-09 09:47:38 +0000478 for (;;) {
479 char *sep = strchr(file, ':');
480 if (sep) {
481 *sep = '\0';
482 copy_file (ifd, file, 1);
483 *sep++ = ':';
484 file = sep;
485 } else {
486 copy_file (ifd, file, 0);
487 break;
488 }
wdenk5b1d7132002-11-03 00:07:02 +0000489 }
Shaohui Xieea65fd82012-08-10 02:49:35 +0000490 } else if (params.type == IH_TYPE_PBLIMAGE) {
491 /* PBL has special Image format, implements its' own */
492 pbl_load_uboot(ifd, &params);
Christian Rieschc14f2812011-12-09 09:47:38 +0000493 } else {
Stefano Babic6531bd92013-08-19 19:03:19 +0200494 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000495 }
wdenk5b1d7132002-11-03 00:07:02 +0000496 }
497
498 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530499#if defined(_POSIX_SYNCHRONIZED_IO) && \
500 !defined(__sun__) && \
501 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200502 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530503 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000504 (void) fdatasync (ifd);
505#else
506 (void) fsync (ifd);
507#endif
508
509 if (fstat(ifd, &sbuf) < 0) {
510 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530511 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000512 exit (EXIT_FAILURE);
513 }
Simon Glass139651f2015-06-23 15:39:12 -0600514 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000515
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400516 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
517 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000518 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530519 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000520 exit (EXIT_FAILURE);
521 }
522
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530523 /* Setup the image header as per input image type*/
524 if (tparams->set_header)
525 tparams->set_header (ptr, &sbuf, ifd, &params);
526 else {
527 fprintf (stderr, "%s: Can't set header for %s: %s\n",
528 params.cmdname, tparams->name, strerror(errno));
529 exit (EXIT_FAILURE);
530 }
wdenk5b1d7132002-11-03 00:07:02 +0000531
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530532 /* Print the image information by processing image header */
533 if (tparams->print_header)
534 tparams->print_header (ptr);
535 else {
536 fprintf (stderr, "%s: Can't print header for %s: %s\n",
537 params.cmdname, tparams->name, strerror(errno));
538 exit (EXIT_FAILURE);
539 }
wdenk5b1d7132002-11-03 00:07:02 +0000540
541 (void) munmap((void *)ptr, sbuf.st_size);
542
543 /* We're a bit of paranoid */
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530544#if defined(_POSIX_SYNCHRONIZED_IO) && \
545 !defined(__sun__) && \
546 !defined(__FreeBSD__) && \
Luka Perkov98c689b2014-08-09 18:17:47 +0200547 !defined(__OpenBSD__) && \
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530548 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000549 (void) fdatasync (ifd);
550#else
551 (void) fsync (ifd);
552#endif
553
554 if (close(ifd)) {
555 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530556 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000557 exit (EXIT_FAILURE);
558 }
559
560 exit (EXIT_SUCCESS);
561}
562
563static void
564copy_file (int ifd, const char *datafile, int pad)
565{
566 int dfd;
567 struct stat sbuf;
568 unsigned char *ptr;
569 int tail;
570 int zero = 0;
Stefano Babic6531bd92013-08-19 19:03:19 +0200571 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000572 int offset = 0;
573 int size;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200574 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000575
Stefano Babic6531bd92013-08-19 19:03:19 +0200576 memset(zeros, 0, sizeof(zeros));
577
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530578 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000579 fprintf (stderr, "Adding Image %s\n", datafile);
580 }
581
wdenk1ee550e2003-10-08 22:14:02 +0000582 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000583 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530584 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000585 exit (EXIT_FAILURE);
586 }
587
588 if (fstat(dfd, &sbuf) < 0) {
589 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530590 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000591 exit (EXIT_FAILURE);
592 }
593
Mike Frysingercd7e1a32008-05-01 04:13:05 -0400594 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
595 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000596 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530597 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000598 exit (EXIT_FAILURE);
599 }
600
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530601 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000602 unsigned char *p = NULL;
603 /*
604 * XIP: do not append the image_header_t at the
605 * beginning of the file, but consume the space
606 * reserved for it.
607 */
608
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530609 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000610 fprintf (stderr,
611 "%s: Bad size: \"%s\" is too small for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530612 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000613 exit (EXIT_FAILURE);
614 }
615
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530616 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000617 if ( *p != 0xff ) {
618 fprintf (stderr,
619 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530620 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000621 exit (EXIT_FAILURE);
622 }
623 }
624
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530625 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000626 }
627
628 size = sbuf.st_size - offset;
629 if (write(ifd, ptr + offset, size) != size) {
630 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530631 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000632 exit (EXIT_FAILURE);
633 }
634
Stefano Babic6531bd92013-08-19 19:03:19 +0200635 tail = size % 4;
636 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000637
638 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
639 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530640 params.cmdname, params.imagefile,
641 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000642 exit (EXIT_FAILURE);
643 }
Stefano Babic6531bd92013-08-19 19:03:19 +0200644 } else if (pad > 1) {
Simon Glass46409cc2015-08-30 16:55:22 -0600645 while (pad > 0) {
646 int todo = sizeof(zeros);
647
648 if (todo > pad)
649 todo = pad;
650 if (write(ifd, (char *)&zeros, todo) != todo) {
651 fprintf(stderr, "%s: Write error on %s: %s\n",
652 params.cmdname, params.imagefile,
653 strerror(errno));
654 exit(EXIT_FAILURE);
655 }
656 pad -= todo;
Stefano Babic6531bd92013-08-19 19:03:19 +0200657 }
wdenk5b1d7132002-11-03 00:07:02 +0000658 }
659
660 (void) munmap((void *)ptr, sbuf.st_size);
661 (void) close (dfd);
662}