blob: e78608293e722953b9c2e6846f47b915955b6f50 [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 */
Naoki Hayamaad5fb9f2020-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 Glass30664222016-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 Glass0cd82e22016-10-27 17:54:03 -060073 free(order);
Simon Glass30664222016-06-30 10:52:17 -060074
75 return 0;
76}
77
Simon Glass15310342016-02-22 22:55:37 -070078static void usage(const char *msg)
Simon Glassb0a487a2016-02-22 22:55:36 -070079{
Simon Glass15310342016-02-22 22:55:37 -070080 fprintf(stderr, "Error: %s\n", msg);
Simon Glassb0a487a2016-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,
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +010097 " %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 -070098 " <dtb> file is used with -f auto, it may occur multiple times.\n",
Simon Glassb0a487a2016-02-22 22:55:36 -070099 params.cmdname);
100 fprintf(stderr,
101 " -D => set all options for device tree compiler\n"
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100102 " -f => input filename for FIT source\n"
103 " -i => input filename for ramdisk file\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700104#ifdef CONFIG_FIT_SIGNATURE
105 fprintf(stderr,
Kever Yangebfe6112020-03-30 11:56:24 +0800106 "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 -0700107 " -E => place data outside of the FIT structure\n"
Kever Yangebfe6112020-03-30 11:56:24 +0800108 " -B => align size in hex for FIT structure and header\n"
Simon Glassb0a487a2016-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 Reedf8f91072016-06-09 19:38:02 -0700113 " -p => place external data at a static position\n"
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600114 " -r => mark keys used as 'required' in dtb\n"
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300115 " -N => openssl engine to use for signing\n");
Simon Glassb0a487a2016-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 Siach79aa33c2017-06-29 20:37:08 +0300122 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
Simon Glassb0a487a2016-02-22 22:55:36 -0700123
124 exit(EXIT_FAILURE);
125}
126
Simon Glassfb4cce02016-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
Simon Glass0b443de2016-02-22 22:55:33 -0700145static void process_args(int argc, char **argv)
wdenk5b1d7132002-11-03 00:07:02 +0000146{
Peter Tysera2513e22010-04-04 22:36:03 -0500147 char *ptr;
Simon Glassd505a092016-02-22 22:55:48 -0700148 int type = IH_TYPE_INVALID;
149 char *datafile = NULL;
Simon Glassa02221f2016-02-22 22:55:34 -0700150 int opt;
wdenk5b1d7132002-11-03 00:07:02 +0000151
Simon Glassa02221f2016-02-22 22:55:34 -0700152 while ((opt = getopt(argc, argv,
Simon Glass152b2462020-07-09 18:39:43 -0600153 "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
Simon Glassa02221f2016-02-22 22:55:34 -0700154 switch (opt) {
Simon Glass07450082016-02-22 22:55:35 -0700155 case 'a':
156 params.addr = strtoull(optarg, &ptr, 16);
157 if (*ptr) {
158 fprintf(stderr, "%s: invalid load address %s\n",
159 params.cmdname, optarg);
160 exit(EXIT_FAILURE);
161 }
Simon Glassa02221f2016-02-22 22:55:34 -0700162 break;
163 case 'A':
164 params.arch = genimg_get_arch_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600165 if (params.arch < 0) {
166 show_valid_options(IH_ARCH);
Simon Glass15310342016-02-22 22:55:37 -0700167 usage("Invalid architecture");
Simon Glass51f03e62016-06-30 10:52:19 -0600168 }
Simon Glassa02221f2016-02-22 22:55:34 -0700169 break;
Simon Glassfb4cce02016-02-22 22:55:52 -0700170 case 'b':
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200171 if (add_content(IH_TYPE_FLATDT, optarg)) {
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200172 fprintf(stderr,
173 "%s: Out of memory adding content '%s'",
174 params.cmdname, optarg);
175 exit(EXIT_FAILURE);
176 }
Simon Glassfb4cce02016-02-22 22:55:52 -0700177 break;
Kever Yangebfe6112020-03-30 11:56:24 +0800178 case 'B':
179 params.bl_len = strtoull(optarg, &ptr, 16);
180 if (*ptr) {
181 fprintf(stderr, "%s: invalid block length %s\n",
182 params.cmdname, optarg);
183 exit(EXIT_FAILURE);
184 }
185
186 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700187 case 'c':
188 params.comment = optarg;
189 break;
190 case 'C':
191 params.comp = genimg_get_comp_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600192 if (params.comp < 0) {
193 show_valid_options(IH_COMP);
Simon Glass15310342016-02-22 22:55:37 -0700194 usage("Invalid compression type");
Simon Glass51f03e62016-06-30 10:52:19 -0600195 }
Simon Glassa02221f2016-02-22 22:55:34 -0700196 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700197 case 'd':
198 params.datafile = optarg;
199 params.dflag = 1;
200 break;
Simon Glass07450082016-02-22 22:55:35 -0700201 case 'D':
202 params.dtc = optarg;
203 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700204 case 'e':
205 params.ep = strtoull(optarg, &ptr, 16);
206 if (*ptr) {
207 fprintf(stderr, "%s: invalid entry point %s\n",
208 params.cmdname, optarg);
209 exit(EXIT_FAILURE);
210 }
211 params.eflag = 1;
212 break;
Simon Glass722ebc82016-02-22 22:55:53 -0700213 case 'E':
214 params.external_data = true;
215 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700216 case 'f':
Simon Glass8e35bb02016-02-22 22:55:51 -0700217 datafile = optarg;
218 params.auto_its = !strcmp(datafile, "auto");
Heinrich Schuchardta29162a2020-05-09 17:10:41 +0200219 /* fallthrough */
Simon Glassa02221f2016-02-22 22:55:34 -0700220 case 'F':
221 /*
222 * The flattened image tree (FIT) format
223 * requires a flattened device tree image type
224 */
225 params.type = IH_TYPE_FLATDT;
226 params.fflag = 1;
227 break;
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100228 case 'i':
229 params.fit_ramdisk = optarg;
230 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700231 case 'k':
232 params.keydir = optarg;
233 break;
234 case 'K':
235 params.keydest = optarg;
236 break;
Simon Glass07450082016-02-22 22:55:35 -0700237 case 'l':
238 params.lflag = 1;
239 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700240 case 'n':
241 params.imagename = optarg;
242 break;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600243 case 'N':
244 params.engine_id = optarg;
245 break;
Simon Glass07450082016-02-22 22:55:35 -0700246 case 'O':
247 params.os = genimg_get_os_id(optarg);
Simon Glass51f03e62016-06-30 10:52:19 -0600248 if (params.os < 0) {
249 show_valid_options(IH_OS);
Simon Glass15310342016-02-22 22:55:37 -0700250 usage("Invalid operating system");
Simon Glass51f03e62016-06-30 10:52:19 -0600251 }
Simon Glass07450082016-02-22 22:55:35 -0700252 break;
Teddy Reedf8f91072016-06-09 19:38:02 -0700253 case 'p':
254 params.external_offset = strtoull(optarg, &ptr, 16);
255 if (*ptr) {
256 fprintf(stderr, "%s: invalid offset size %s\n",
257 params.cmdname, optarg);
258 exit(EXIT_FAILURE);
259 }
Teddy Reedb6fefa72016-07-11 22:54:26 -0700260 break;
Simon Glassbd6e1422016-05-01 13:55:38 -0600261 case 'q':
262 params.quiet = 1;
263 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700264 case 'r':
265 params.require_keys = 1;
266 break;
267 case 'R':
268 /*
269 * This entry is for the second configuration
270 * file, if only one is not enough.
271 */
272 params.imagename2 = optarg;
273 break;
274 case 's':
275 params.skipcpy = 1;
276 break;
Simon Glass152b2462020-07-09 18:39:43 -0600277 case 't':
278 params.reset_timestamp = 1;
279 break;
Simon Glass07450082016-02-22 22:55:35 -0700280 case 'T':
Baruch Siach79aa33c2017-06-29 20:37:08 +0300281 if (strcmp(optarg, "list") == 0) {
282 show_valid_options(IH_TYPE);
283 exit(EXIT_SUCCESS);
284 }
Simon Glassd505a092016-02-22 22:55:48 -0700285 type = genimg_get_type_id(optarg);
286 if (type < 0) {
Simon Glassf24e1052016-06-30 10:52:18 -0600287 show_valid_options(IH_TYPE);
Simon Glass15310342016-02-22 22:55:37 -0700288 usage("Invalid image type");
Simon Glass07450082016-02-22 22:55:35 -0700289 }
290 break;
Simon Glassa02221f2016-02-22 22:55:34 -0700291 case 'v':
292 params.vflag++;
293 break;
294 case 'V':
295 printf("mkimage version %s\n", PLAIN_VERSION);
296 exit(EXIT_SUCCESS);
297 case 'x':
298 params.xflag++;
299 break;
300 default:
Simon Glass15310342016-02-22 22:55:37 -0700301 usage("Invalid option");
wdenk5b1d7132002-11-03 00:07:02 +0000302 }
wdenk5b1d7132002-11-03 00:07:02 +0000303 }
304
Andreas Bießmann8edeac82016-05-03 15:17:03 +0200305 /* The last parameter is expected to be the imagefile */
306 if (optind < argc)
Andreas Bießmann7a439ca2016-05-01 03:01:27 +0200307 params.imagefile = argv[optind];
308
Simon Glassd505a092016-02-22 22:55:48 -0700309 /*
310 * For auto-generated FIT images we need to know the image type to put
311 * in the FIT, which is separate from the file's image type (which
312 * will always be IH_TYPE_FLATDT in this case).
313 */
314 if (params.type == IH_TYPE_FLATDT) {
Simon Glass20deadd2016-06-30 10:52:07 -0600315 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
Simon Glass3c23c0f2016-06-30 10:52:08 -0600316 /* For auto_its, datafile is always 'auto' */
Simon Glass8e35bb02016-02-22 22:55:51 -0700317 if (!params.auto_its)
318 params.datafile = datafile;
Simon Glasse324a922016-06-30 10:52:09 -0600319 else if (!params.datafile)
320 usage("Missing data file for auto-FIT (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700321 } else if (type != IH_TYPE_INVALID) {
Alex Kiernan8c842872018-04-22 05:11:17 +0000322 if (type == IH_TYPE_SCRIPT && !params.datafile)
323 usage("Missing data file for script (use -d)");
Simon Glassd505a092016-02-22 22:55:48 -0700324 params.type = type;
325 }
326
327 if (!params.imagefile)
Simon Glass15310342016-02-22 22:55:37 -0700328 usage("Missing output filename");
Simon Glass0b443de2016-02-22 22:55:33 -0700329}
330
Simon Glass0b443de2016-02-22 22:55:33 -0700331int main(int argc, char **argv)
332{
333 int ifd = -1;
334 struct stat sbuf;
335 char *ptr;
336 int retval = 0;
337 struct image_type_params *tparams = NULL;
338 int pad_len = 0;
339 int dfd;
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200340 size_t map_len;
Simon Glass0b443de2016-02-22 22:55:33 -0700341
342 params.cmdname = *argv;
343 params.addr = 0;
344 params.ep = 0;
345
346 process_args(argc, argv);
wdenk5b1d7132002-11-03 00:07:02 +0000347
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530348 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200349 tparams = imagetool_get_type(params.type);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530350 if (tparams == NULL) {
351 fprintf (stderr, "%s: unsupported type %s\n",
352 params.cmdname, genimg_get_type_name(params.type));
353 exit (EXIT_FAILURE);
354 }
355
356 /*
357 * check the passed arguments parameters meets the requirements
358 * as per image type to be generated/listed
359 */
360 if (tparams->check_params)
361 if (tparams->check_params (&params))
Simon Glass15310342016-02-22 22:55:37 -0700362 usage("Bad parameters for image type");
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530363
364 if (!params.eflag) {
365 params.ep = params.addr;
wdenk5b1d7132002-11-03 00:07:02 +0000366 /* If XIP, entry point must be after the U-Boot header */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530367 if (params.xflag)
368 params.ep += tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000369 }
370
Peter Tyserc81296c2009-11-24 16:42:10 -0600371 if (params.fflag){
372 if (tparams->fflag_handle)
373 /*
374 * in some cases, some additional processing needs
375 * to be done if fflag is defined
376 *
377 * For ex. fit_handle_file for Fit file support
378 */
379 retval = tparams->fflag_handle(&params);
wdenk5b1d7132002-11-03 00:07:02 +0000380
Peter Tyserc81296c2009-11-24 16:42:10 -0600381 if (retval != EXIT_SUCCESS)
382 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000383 }
384
Peter Tyserc81296c2009-11-24 16:42:10 -0600385 if (params.lflag || params.fflag) {
386 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
387 } else {
388 ifd = open (params.imagefile,
389 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
390 }
391
392 if (ifd < 0) {
393 fprintf (stderr, "%s: Can't open %s: %s\n",
394 params.cmdname, params.imagefile,
395 strerror(errno));
396 exit (EXIT_FAILURE);
397 }
398
399 if (params.lflag || params.fflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000400 /*
401 * list header information of existing image
402 */
403 if (fstat(ifd, &sbuf) < 0) {
404 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530405 params.cmdname, params.imagefile,
406 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000407 exit (EXIT_FAILURE);
408 }
409
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530410 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000411 fprintf (stderr,
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530412 "%s: Bad size: \"%s\" is not valid image\n",
413 params.cmdname, params.imagefile);
wdenk5b1d7132002-11-03 00:07:02 +0000414 exit (EXIT_FAILURE);
415 }
416
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400417 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
418 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000419 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530420 params.cmdname, params.imagefile,
421 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000422 exit (EXIT_FAILURE);
423 }
424
Jordan Hand73c02e52019-04-09 10:36:09 -0700425 if (params.fflag) {
426 /*
427 * Verifies the header format based on the expected header for image
428 * type in tparams
429 */
430 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
431 tparams, &params);
432 } else {
433 /**
434 * When listing the image, we are not given the image type. Simply check all
435 * image types to find one that matches our header
436 */
437 retval = imagetool_verify_print_header(ptr, &sbuf,
438 tparams, &params);
439 }
wdenk5b1d7132002-11-03 00:07:02 +0000440
wdenk5b1d7132002-11-03 00:07:02 +0000441 (void) munmap((void *)ptr, sbuf.st_size);
442 (void) close (ifd);
443
Prafulla Wadaskarf7644c02009-08-10 18:49:37 +0530444 exit (retval);
wdenk5b1d7132002-11-03 00:07:02 +0000445 }
446
Marek Vasut34633142015-12-07 18:01:54 +0100447 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200448 dfd = open(params.datafile, O_RDONLY | O_BINARY);
449 if (dfd < 0) {
450 fprintf(stderr, "%s: Can't open %s: %s\n",
451 params.cmdname, params.datafile,
452 strerror(errno));
453 exit(EXIT_FAILURE);
454 }
Simon Glass92a655c2015-06-23 15:39:12 -0600455
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200456 if (fstat(dfd, &sbuf) < 0) {
457 fprintf(stderr, "%s: Can't stat %s: %s\n",
458 params.cmdname, params.datafile,
459 strerror(errno));
460 exit(EXIT_FAILURE);
461 }
Simon Glass92a655c2015-06-23 15:39:12 -0600462
Philippe De Swert6ae6e162015-12-04 00:11:23 +0200463 params.file_size = sbuf.st_size + tparams->header_size;
464 close(dfd);
465 }
Simon Glass92a655c2015-06-23 15:39:12 -0600466
wdenk5b1d7132002-11-03 00:07:02 +0000467 /*
Stefano Babicf0662102011-09-15 23:50:16 +0000468 * In case there an header with a variable
469 * length will be added, the corresponding
470 * function is called. This is responsible to
471 * allocate memory for the header itself.
wdenk5b1d7132002-11-03 00:07:02 +0000472 */
Stefano Babicf0662102011-09-15 23:50:16 +0000473 if (tparams->vrec_header)
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200474 pad_len = tparams->vrec_header(&params, tparams);
Stefano Babicf0662102011-09-15 23:50:16 +0000475 else
476 memset(tparams->hdr, 0, tparams->header_size);
wdenk5b1d7132002-11-03 00:07:02 +0000477
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530478 if (write(ifd, tparams->hdr, tparams->header_size)
479 != tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000480 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530481 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000482 exit (EXIT_FAILURE);
483 }
484
Christian Rieschd1be8f92011-12-09 09:47:38 +0000485 if (!params.skipcpy) {
486 if (params.type == IH_TYPE_MULTI ||
487 params.type == IH_TYPE_SCRIPT) {
488 char *file = params.datafile;
489 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000490
Christian Rieschd1be8f92011-12-09 09:47:38 +0000491 for (;;) {
492 char *sep = NULL;
wdenk5b1d7132002-11-03 00:07:02 +0000493
Christian Rieschd1be8f92011-12-09 09:47:38 +0000494 if (file) {
495 if ((sep = strchr(file, ':')) != NULL) {
496 *sep = '\0';
497 }
498
499 if (stat (file, &sbuf) < 0) {
500 fprintf (stderr, "%s: Can't stat %s: %s\n",
501 params.cmdname, file, strerror(errno));
502 exit (EXIT_FAILURE);
503 }
504 size = cpu_to_uimage (sbuf.st_size);
505 } else {
506 size = 0;
wdenk5b1d7132002-11-03 00:07:02 +0000507 }
508
Christian Rieschd1be8f92011-12-09 09:47:38 +0000509 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
510 fprintf (stderr, "%s: Write error on %s: %s\n",
511 params.cmdname, params.imagefile,
512 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000513 exit (EXIT_FAILURE);
514 }
Christian Rieschd1be8f92011-12-09 09:47:38 +0000515
516 if (!file) {
517 break;
518 }
519
520 if (sep) {
521 *sep = ':';
522 file = sep + 1;
523 } else {
524 file = NULL;
525 }
wdenk5b1d7132002-11-03 00:07:02 +0000526 }
527
Christian Rieschd1be8f92011-12-09 09:47:38 +0000528 file = params.datafile;
wdenk5b1d7132002-11-03 00:07:02 +0000529
Christian Rieschd1be8f92011-12-09 09:47:38 +0000530 for (;;) {
531 char *sep = strchr(file, ':');
532 if (sep) {
533 *sep = '\0';
534 copy_file (ifd, file, 1);
535 *sep++ = ':';
536 file = sep;
537 } else {
538 copy_file (ifd, file, 0);
539 break;
540 }
wdenk5b1d7132002-11-03 00:07:02 +0000541 }
Shaohui Xie5d898a02012-08-10 02:49:35 +0000542 } else if (params.type == IH_TYPE_PBLIMAGE) {
543 /* PBL has special Image format, implements its' own */
544 pbl_load_uboot(ifd, &params);
Alexander Graf6915dcf2018-04-13 14:18:52 +0200545 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
546 /* Image file is meta, walk through actual targets */
547 int ret;
548
549 ret = zynqmpbif_copy_image(ifd, &params);
550 if (ret)
551 return ret;
Peng Fana2b96ec2018-10-16 04:50:30 +0000552 } else if (params.type == IH_TYPE_IMX8IMAGE) {
553 /* i.MX8/8X has special Image format */
554 int ret;
555
556 ret = imx8image_copy_image(ifd, &params);
557 if (ret)
558 return ret;
Peng Fan6609c262018-11-20 10:19:36 +0000559 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
560 /* i.MX8M has special Image format */
561 int ret;
562
563 ret = imx8mimage_copy_image(ifd, &params);
564 if (ret)
565 return ret;
Jeffy Cheneea6cd82019-12-27 11:24:41 +0800566 } else if ((params.type == IH_TYPE_RKSD) ||
567 (params.type == IH_TYPE_RKSPI)) {
568 /* Rockchip has special Image format */
569 int ret;
570
571 ret = rockchip_copy_image(ifd, &params);
572 if (ret)
573 return ret;
Christian Rieschd1be8f92011-12-09 09:47:38 +0000574 } else {
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200575 copy_file(ifd, params.datafile, pad_len);
wdenk5b1d7132002-11-03 00:07:02 +0000576 }
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100577 if (params.type == IH_TYPE_FIRMWARE_IVT) {
578 /* Add alignment and IVT */
Kever Yang29e7ab02020-03-30 11:56:19 +0800579 uint32_t aligned_filesize = ALIGN(params.file_size,
580 0x1000);
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100581 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
582 params.addr, 0, 0, 0, params.addr
583 + aligned_filesize
584 - tparams->header_size,
585 params.addr + aligned_filesize
586 - tparams->header_size
587 + 0x20, 0 };
588 int i = params.file_size;
589 for (; i < aligned_filesize; i++) {
Sven Ebenfeldb4e923a2017-01-17 19:36:51 +0100590 if (write(ifd, (char *) &i, 1) != 1) {
Sven Ebenfeldd21bd692016-11-06 16:37:56 +0100591 fprintf(stderr,
592 "%s: Write error on %s: %s\n",
593 params.cmdname,
594 params.imagefile,
595 strerror(errno));
596 exit(EXIT_FAILURE);
597 }
598 }
599 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
600 != sizeof(flash_header_v2_t)) {
601 fprintf(stderr, "%s: Write error on %s: %s\n",
602 params.cmdname,
603 params.imagefile,
604 strerror(errno));
605 exit(EXIT_FAILURE);
606 }
607 }
wdenk5b1d7132002-11-03 00:07:02 +0000608 }
609
610 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530611#if defined(_POSIX_SYNCHRONIZED_IO) && \
612 !defined(__sun__) && \
613 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200614 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530615 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000616 (void) fdatasync (ifd);
617#else
618 (void) fsync (ifd);
619#endif
620
621 if (fstat(ifd, &sbuf) < 0) {
622 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530623 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000624 exit (EXIT_FAILURE);
625 }
Simon Glass92a655c2015-06-23 15:39:12 -0600626 params.file_size = sbuf.st_size;
wdenk5b1d7132002-11-03 00:07:02 +0000627
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200628 map_len = sbuf.st_size;
629 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400630 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000631 fprintf (stderr, "%s: Can't map %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530632 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000633 exit (EXIT_FAILURE);
634 }
635
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530636 /* Setup the image header as per input image type*/
637 if (tparams->set_header)
638 tparams->set_header (ptr, &sbuf, ifd, &params);
639 else {
640 fprintf (stderr, "%s: Can't set header for %s: %s\n",
641 params.cmdname, tparams->name, strerror(errno));
642 exit (EXIT_FAILURE);
643 }
wdenk5b1d7132002-11-03 00:07:02 +0000644
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530645 /* Print the image information by processing image header */
646 if (tparams->print_header)
647 tparams->print_header (ptr);
648 else {
Guillaume GARDET004d0092018-03-30 10:28:19 +0200649 fprintf (stderr, "%s: Can't print header for %s\n",
650 params.cmdname, tparams->name);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530651 }
wdenk5b1d7132002-11-03 00:07:02 +0000652
Mark Tomlinson8961c8a2018-08-29 10:51:14 +1200653 (void)munmap((void *)ptr, map_len);
wdenk5b1d7132002-11-03 00:07:02 +0000654
655 /* We're a bit of paranoid */
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530656#if defined(_POSIX_SYNCHRONIZED_IO) && \
657 !defined(__sun__) && \
658 !defined(__FreeBSD__) && \
Luka Perkov31cbe802014-08-09 18:17:47 +0200659 !defined(__OpenBSD__) && \
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530660 !defined(__APPLE__)
wdenk5b1d7132002-11-03 00:07:02 +0000661 (void) fdatasync (ifd);
662#else
663 (void) fsync (ifd);
664#endif
665
666 if (close(ifd)) {
667 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530668 params.cmdname, params.imagefile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000669 exit (EXIT_FAILURE);
670 }
671
672 exit (EXIT_SUCCESS);
673}
674
675static void
676copy_file (int ifd, const char *datafile, int pad)
677{
678 int dfd;
679 struct stat sbuf;
680 unsigned char *ptr;
681 int tail;
682 int zero = 0;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200683 uint8_t zeros[4096];
wdenk5b1d7132002-11-03 00:07:02 +0000684 int offset = 0;
Mylène Josseranddd85dc52020-07-08 11:52:50 +0200685 int size, ret;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200686 struct image_type_params *tparams = imagetool_get_type(params.type);
wdenk5b1d7132002-11-03 00:07:02 +0000687
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200688 memset(zeros, 0, sizeof(zeros));
689
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530690 if (params.vflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000691 fprintf (stderr, "Adding Image %s\n", datafile);
692 }
693
wdenkef1464c2003-10-08 22:14:02 +0000694 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000695 fprintf (stderr, "%s: Can't open %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530696 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000697 exit (EXIT_FAILURE);
698 }
699
700 if (fstat(dfd, &sbuf) < 0) {
701 fprintf (stderr, "%s: Can't stat %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530702 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000703 exit (EXIT_FAILURE);
704 }
705
Mike Frysingerfa956fd2008-05-01 04:13:05 -0400706 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
707 if (ptr == MAP_FAILED) {
wdenk5b1d7132002-11-03 00:07:02 +0000708 fprintf (stderr, "%s: Can't read %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530709 params.cmdname, datafile, strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000710 exit (EXIT_FAILURE);
711 }
712
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530713 if (params.xflag) {
wdenk5b1d7132002-11-03 00:07:02 +0000714 unsigned char *p = NULL;
715 /*
716 * XIP: do not append the image_header_t at the
717 * beginning of the file, but consume the space
718 * reserved for it.
719 */
720
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530721 if ((unsigned)sbuf.st_size < tparams->header_size) {
wdenk5b1d7132002-11-03 00:07:02 +0000722 fprintf (stderr,
723 "%s: Bad size: \"%s\" is too small 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
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530728 for (p = ptr; p < ptr + tparams->header_size; p++) {
wdenk5b1d7132002-11-03 00:07:02 +0000729 if ( *p != 0xff ) {
730 fprintf (stderr,
731 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530732 params.cmdname, datafile);
wdenk5b1d7132002-11-03 00:07:02 +0000733 exit (EXIT_FAILURE);
734 }
735 }
736
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530737 offset = tparams->header_size;
wdenk5b1d7132002-11-03 00:07:02 +0000738 }
739
740 size = sbuf.st_size - offset;
Mylène Josseranddd85dc52020-07-08 11:52:50 +0200741
742 ret = write(ifd, ptr + offset, size);
743 if (ret != size) {
744 if (ret < 0)
745 fprintf (stderr, "%s: Write error on %s: %s\n",
746 params.cmdname, params.imagefile, strerror(errno));
747 else if (ret < size)
748 fprintf (stderr, "%s: Write only %d/%d bytes, "\
749 "probably no space left on the device\n",
750 params.cmdname, ret, size);
wdenk5b1d7132002-11-03 00:07:02 +0000751 exit (EXIT_FAILURE);
752 }
753
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200754 tail = size % 4;
755 if ((pad == 1) && (tail != 0)) {
wdenk5b1d7132002-11-03 00:07:02 +0000756
757 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
758 fprintf (stderr, "%s: Write error on %s: %s\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530759 params.cmdname, params.imagefile,
760 strerror(errno));
wdenk5b1d7132002-11-03 00:07:02 +0000761 exit (EXIT_FAILURE);
762 }
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200763 } else if (pad > 1) {
Simon Glass424b86a2015-08-30 16:55:22 -0600764 while (pad > 0) {
765 int todo = sizeof(zeros);
766
767 if (todo > pad)
768 todo = pad;
769 if (write(ifd, (char *)&zeros, todo) != todo) {
770 fprintf(stderr, "%s: Write error on %s: %s\n",
771 params.cmdname, params.imagefile,
772 strerror(errno));
773 exit(EXIT_FAILURE);
774 }
775 pad -= todo;
Stefano Babic9bac0bb2013-08-19 19:03:19 +0200776 }
wdenk5b1d7132002-11-03 00:07:02 +0000777 }
778
779 (void) munmap((void *)ptr, sbuf.st_size);
780 (void) close (dfd);
781}