183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
25b1d7137Swdenk /*
39d25438fSBartlomiej Sieka * (C) Copyright 2008 Semihalf
49d25438fSBartlomiej Sieka *
589a4d6b1SPrafulla Wadaskar * (C) Copyright 2000-2009
65b1d7137Swdenk * DENX Software Engineering
75b1d7137Swdenk * Wolfgang Denk, wd@denx.de
85b1d7137Swdenk */
95b1d7137Swdenk
10b97a2a0aSMarian Balakowicz #include "mkimage.h"
11d21bd69bSSven Ebenfeld #include "imximage.h"
125b1d7137Swdenk #include <image.h>
13976b38c0SWolfgang Denk #include <version.h>
145b1d7137Swdenk
155b1d7137Swdenk static void copy_file(int, const char *, int);
165b1d7137Swdenk
1789a4d6b1SPrafulla Wadaskar /* parameters initialized by core will be used by the image type code */
18cc7a6444SSimon Glass static struct image_tool_params params = {
1989a4d6b1SPrafulla Wadaskar .os = IH_OS_LINUX,
2089a4d6b1SPrafulla Wadaskar .arch = IH_ARCH_PPC,
2189a4d6b1SPrafulla Wadaskar .type = IH_TYPE_KERNEL,
2289a4d6b1SPrafulla Wadaskar .comp = IH_COMP_GZIP,
2389a4d6b1SPrafulla Wadaskar .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
2404387d24SWolfgang Denk .imagename = "",
255d898a00SShaohui Xie .imagename2 = "",
2689a4d6b1SPrafulla Wadaskar };
275b1d7137Swdenk
2830664225SSimon Glass static enum ih_category cur_category;
2930664225SSimon Glass
h_compare_category_name(const void * vtype1,const void * vtype2)3030664225SSimon Glass static int h_compare_category_name(const void *vtype1, const void *vtype2)
3130664225SSimon Glass {
3230664225SSimon Glass const int *type1 = vtype1;
3330664225SSimon Glass const int *type2 = vtype2;
3430664225SSimon Glass const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
3530664225SSimon Glass const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
3630664225SSimon Glass
3730664225SSimon Glass return strcmp(name1, name2);
3830664225SSimon Glass }
3930664225SSimon Glass
show_valid_options(enum ih_category category)40f24e1050SSimon Glass static int show_valid_options(enum ih_category category)
4130664225SSimon Glass {
4230664225SSimon Glass int *order;
4330664225SSimon Glass int count;
4430664225SSimon Glass int item;
4530664225SSimon Glass int i;
4630664225SSimon Glass
4730664225SSimon Glass count = genimg_get_cat_count(category);
4830664225SSimon Glass order = calloc(count, sizeof(*order));
4930664225SSimon Glass if (!order)
5030664225SSimon Glass return -ENOMEM;
5130664225SSimon Glass
5230664225SSimon Glass /* Sort the names in order of short name for easier reading */
5330664225SSimon Glass for (item = 0; item < count; item++)
5430664225SSimon Glass order[item] = item;
5530664225SSimon Glass cur_category = category;
5630664225SSimon Glass qsort(order, count, sizeof(int), h_compare_category_name);
5730664225SSimon Glass
5830664225SSimon Glass fprintf(stderr, "\nInvalid %s, supported are:\n",
5930664225SSimon Glass genimg_get_cat_desc(category));
6030664225SSimon Glass for (i = 0; i < count; i++) {
6130664225SSimon Glass item = order[i];
6230664225SSimon Glass fprintf(stderr, "\t%-15s %s\n",
6330664225SSimon Glass genimg_get_cat_short_name(category, item),
6430664225SSimon Glass genimg_get_cat_name(category, item));
6530664225SSimon Glass }
6630664225SSimon Glass fprintf(stderr, "\n");
670cd82e25SSimon Glass free(order);
6830664225SSimon Glass
6930664225SSimon Glass return 0;
7030664225SSimon Glass }
7130664225SSimon Glass
usage(const char * msg)7215310348SSimon Glass static void usage(const char *msg)
73b0a487a4SSimon Glass {
7415310348SSimon Glass fprintf(stderr, "Error: %s\n", msg);
75b0a487a4SSimon Glass fprintf(stderr, "Usage: %s -l image\n"
76b0a487a4SSimon Glass " -l ==> list image header information\n",
77b0a487a4SSimon Glass params.cmdname);
78b0a487a4SSimon Glass fprintf(stderr,
79b0a487a4SSimon Glass " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
80b0a487a4SSimon Glass " -A ==> set architecture to 'arch'\n"
81b0a487a4SSimon Glass " -O ==> set operating system to 'os'\n"
82b0a487a4SSimon Glass " -T ==> set image type to 'type'\n"
83b0a487a4SSimon Glass " -C ==> set compression type 'comp'\n"
84b0a487a4SSimon Glass " -a ==> set load address to 'addr' (hex)\n"
85b0a487a4SSimon Glass " -e ==> set entry point to 'ep' (hex)\n"
86b0a487a4SSimon Glass " -n ==> set image name to 'name'\n"
87b0a487a4SSimon Glass " -d ==> use image data from 'datafile'\n"
88b0a487a4SSimon Glass " -x ==> set XIP (execute in place)\n",
89b0a487a4SSimon Glass params.cmdname);
90b0a487a4SSimon Glass fprintf(stderr,
910f7c6cdcSTomeu Vizoso " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
9282bd2f29SVagrant Cascadian " <dtb> file is used with -f auto, it may occur multiple times.\n",
93b0a487a4SSimon Glass params.cmdname);
94b0a487a4SSimon Glass fprintf(stderr,
95b0a487a4SSimon Glass " -D => set all options for device tree compiler\n"
960f7c6cdcSTomeu Vizoso " -f => input filename for FIT source\n"
970f7c6cdcSTomeu Vizoso " -i => input filename for ramdisk file\n");
98b0a487a4SSimon Glass #ifdef CONFIG_FIT_SIGNATURE
99b0a487a4SSimon Glass fprintf(stderr,
100f1ca1fdeSGeorge McCollister "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
101f8f9107dSTeddy Reed " -E => place data outside of the FIT structure\n"
102b0a487a4SSimon Glass " -k => set directory containing private keys\n"
103b0a487a4SSimon Glass " -K => write public keys to this .dtb file\n"
104b0a487a4SSimon Glass " -c => add comment in signature node\n"
105b0a487a4SSimon Glass " -F => re-sign existing FIT image\n"
106f8f9107dSTeddy Reed " -p => place external data at a static position\n"
107f1ca1fdeSGeorge McCollister " -r => mark keys used as 'required' in dtb\n"
108f1ca1fdeSGeorge McCollister " -N => engine to use for signing (pkcs11)\n");
109b0a487a4SSimon Glass #else
110b0a487a4SSimon Glass fprintf(stderr,
111b0a487a4SSimon Glass "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
112b0a487a4SSimon Glass #endif
113b0a487a4SSimon Glass fprintf(stderr, " %s -V ==> print version information and exit\n",
114b0a487a4SSimon Glass params.cmdname);
11579aa33cdSBaruch Siach fprintf(stderr, "Use '-T list' to see a list of available image types\n");
116b0a487a4SSimon Glass
117b0a487a4SSimon Glass exit(EXIT_FAILURE);
118b0a487a4SSimon Glass }
119b0a487a4SSimon Glass
add_content(int type,const char * fname)120fb4cce0fSSimon Glass static int add_content(int type, const char *fname)
121fb4cce0fSSimon Glass {
122fb4cce0fSSimon Glass struct content_info *cont;
123fb4cce0fSSimon Glass
124fb4cce0fSSimon Glass cont = calloc(1, sizeof(*cont));
125fb4cce0fSSimon Glass if (!cont)
126fb4cce0fSSimon Glass return -1;
127fb4cce0fSSimon Glass cont->type = type;
128fb4cce0fSSimon Glass cont->fname = fname;
129fb4cce0fSSimon Glass if (params.content_tail)
130fb4cce0fSSimon Glass params.content_tail->next = cont;
131fb4cce0fSSimon Glass else
132fb4cce0fSSimon Glass params.content_head = cont;
133fb4cce0fSSimon Glass params.content_tail = cont;
134fb4cce0fSSimon Glass
135fb4cce0fSSimon Glass return 0;
136fb4cce0fSSimon Glass }
137fb4cce0fSSimon Glass
process_args(int argc,char ** argv)1380b443deeSSimon Glass static void process_args(int argc, char **argv)
1395b1d7137Swdenk {
140a2513e27SPeter Tyser char *ptr;
141d505a09cSSimon Glass int type = IH_TYPE_INVALID;
142d505a09cSSimon Glass char *datafile = NULL;
143a02221f2SSimon Glass int opt;
1445b1d7137Swdenk
145a02221f2SSimon Glass while ((opt = getopt(argc, argv,
146f1ca1fdeSGeorge McCollister "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
147a02221f2SSimon Glass switch (opt) {
14807450081SSimon Glass case 'a':
14907450081SSimon Glass params.addr = strtoull(optarg, &ptr, 16);
15007450081SSimon Glass if (*ptr) {
15107450081SSimon Glass fprintf(stderr, "%s: invalid load address %s\n",
15207450081SSimon Glass params.cmdname, optarg);
15307450081SSimon Glass exit(EXIT_FAILURE);
15407450081SSimon Glass }
1555b1d7137Swdenk break;
1565b1d7137Swdenk case 'A':
157a02221f2SSimon Glass params.arch = genimg_get_arch_id(optarg);
15851f03e6aSSimon Glass if (params.arch < 0) {
15951f03e6aSSimon Glass show_valid_options(IH_ARCH);
16015310348SSimon Glass usage("Invalid architecture");
16151f03e6aSSimon Glass }
162a02221f2SSimon Glass break;
163fb4cce0fSSimon Glass case 'b':
1648edeac86SAndreas Bießmann if (add_content(IH_TYPE_FLATDT, optarg)) {
1657a439cadSAndreas Bießmann fprintf(stderr,
1667a439cadSAndreas Bießmann "%s: Out of memory adding content '%s'",
1677a439cadSAndreas Bießmann params.cmdname, optarg);
1687a439cadSAndreas Bießmann exit(EXIT_FAILURE);
1697a439cadSAndreas Bießmann }
170fb4cce0fSSimon Glass break;
1714f610427SSimon Glass case 'c':
172a02221f2SSimon Glass params.comment = optarg;
173a02221f2SSimon Glass break;
1745b1d7137Swdenk case 'C':
175a02221f2SSimon Glass params.comp = genimg_get_comp_id(optarg);
17651f03e6aSSimon Glass if (params.comp < 0) {
17751f03e6aSSimon Glass show_valid_options(IH_COMP);
17815310348SSimon Glass usage("Invalid compression type");
17951f03e6aSSimon Glass }
180a02221f2SSimon Glass break;
1815b1d7137Swdenk case 'd':
182a02221f2SSimon Glass params.datafile = optarg;
18389a4d6b1SPrafulla Wadaskar params.dflag = 1;
184a02221f2SSimon Glass break;
18507450081SSimon Glass case 'D':
18607450081SSimon Glass params.dtc = optarg;
18707450081SSimon Glass break;
1885b1d7137Swdenk case 'e':
189a02221f2SSimon Glass params.ep = strtoull(optarg, &ptr, 16);
1905b1d7137Swdenk if (*ptr) {
191a02221f2SSimon Glass fprintf(stderr, "%s: invalid entry point %s\n",
192a02221f2SSimon Glass params.cmdname, optarg);
1935b1d7137Swdenk exit(EXIT_FAILURE);
1945b1d7137Swdenk }
19589a4d6b1SPrafulla Wadaskar params.eflag = 1;
196a02221f2SSimon Glass break;
197722ebc8fSSimon Glass case 'E':
198722ebc8fSSimon Glass params.external_data = true;
199722ebc8fSSimon Glass break;
2009d25438fSBartlomiej Sieka case 'f':
2018e35bb07SSimon Glass datafile = optarg;
2028e35bb07SSimon Glass params.auto_its = !strcmp(datafile, "auto");
20395d77b44SSimon Glass /* no break */
20495d77b44SSimon Glass case 'F':
2051a99de2cSPeter Tyser /*
2061a99de2cSPeter Tyser * The flattened image tree (FIT) format
2071a99de2cSPeter Tyser * requires a flattened device tree image type
2081a99de2cSPeter Tyser */
2091a99de2cSPeter Tyser params.type = IH_TYPE_FLATDT;
210fbc1c8f6SPeter Tyser params.fflag = 1;
211a02221f2SSimon Glass break;
2120f7c6cdcSTomeu Vizoso case 'i':
2130f7c6cdcSTomeu Vizoso params.fit_ramdisk = optarg;
2140f7c6cdcSTomeu Vizoso break;
21580e4df8aSSimon Glass case 'k':
216a02221f2SSimon Glass params.keydir = optarg;
217a02221f2SSimon Glass break;
218e29495d3SSimon Glass case 'K':
219a02221f2SSimon Glass params.keydest = optarg;
220a02221f2SSimon Glass break;
22107450081SSimon Glass case 'l':
22207450081SSimon Glass params.lflag = 1;
22307450081SSimon Glass break;
2245b1d7137Swdenk case 'n':
225a02221f2SSimon Glass params.imagename = optarg;
226a02221f2SSimon Glass break;
227f1ca1fdeSGeorge McCollister case 'N':
228f1ca1fdeSGeorge McCollister params.engine_id = optarg;
229f1ca1fdeSGeorge McCollister break;
23007450081SSimon Glass case 'O':
23107450081SSimon Glass params.os = genimg_get_os_id(optarg);
23251f03e6aSSimon Glass if (params.os < 0) {
23351f03e6aSSimon Glass show_valid_options(IH_OS);
23415310348SSimon Glass usage("Invalid operating system");
23551f03e6aSSimon Glass }
23607450081SSimon Glass break;
237f8f9107dSTeddy Reed case 'p':
238f8f9107dSTeddy Reed params.external_offset = strtoull(optarg, &ptr, 16);
239f8f9107dSTeddy Reed if (*ptr) {
240f8f9107dSTeddy Reed fprintf(stderr, "%s: invalid offset size %s\n",
241f8f9107dSTeddy Reed params.cmdname, optarg);
242f8f9107dSTeddy Reed exit(EXIT_FAILURE);
243f8f9107dSTeddy Reed }
244b6fefa76STeddy Reed break;
245bd6e1420SSimon Glass case 'q':
246bd6e1420SSimon Glass params.quiet = 1;
247bd6e1420SSimon Glass break;
248399c744bSSimon Glass case 'r':
249399c744bSSimon Glass params.require_keys = 1;
250399c744bSSimon Glass break;
2515d898a00SShaohui Xie case 'R':
2525d898a00SShaohui Xie /*
2535d898a00SShaohui Xie * This entry is for the second configuration
2545d898a00SShaohui Xie * file, if only one is not enough.
2555d898a00SShaohui Xie */
256a02221f2SSimon Glass params.imagename2 = optarg;
257a02221f2SSimon Glass break;
258f0662105SStefano Babic case 's':
259f0662105SStefano Babic params.skipcpy = 1;
260f0662105SStefano Babic break;
26107450081SSimon Glass case 'T':
26279aa33cdSBaruch Siach if (strcmp(optarg, "list") == 0) {
26379aa33cdSBaruch Siach show_valid_options(IH_TYPE);
26479aa33cdSBaruch Siach exit(EXIT_SUCCESS);
26579aa33cdSBaruch Siach }
266d505a09cSSimon Glass type = genimg_get_type_id(optarg);
267d505a09cSSimon Glass if (type < 0) {
268f24e1050SSimon Glass show_valid_options(IH_TYPE);
26915310348SSimon Glass usage("Invalid image type");
27007450081SSimon Glass }
27107450081SSimon Glass break;
2725b1d7137Swdenk case 'v':
27389a4d6b1SPrafulla Wadaskar params.vflag++;
2745b1d7137Swdenk break;
275976b38c0SWolfgang Denk case 'V':
276976b38c0SWolfgang Denk printf("mkimage version %s\n", PLAIN_VERSION);
277976b38c0SWolfgang Denk exit(EXIT_SUCCESS);
2785b1d7137Swdenk case 'x':
27989a4d6b1SPrafulla Wadaskar params.xflag++;
2805b1d7137Swdenk break;
2815b1d7137Swdenk default:
28215310348SSimon Glass usage("Invalid option");
2835b1d7137Swdenk }
2845b1d7137Swdenk }
2855b1d7137Swdenk
2868edeac86SAndreas Bießmann /* The last parameter is expected to be the imagefile */
2878edeac86SAndreas Bießmann if (optind < argc)
2887a439cadSAndreas Bießmann params.imagefile = argv[optind];
2897a439cadSAndreas Bießmann
290d505a09cSSimon Glass /*
291d505a09cSSimon Glass * For auto-generated FIT images we need to know the image type to put
292d505a09cSSimon Glass * in the FIT, which is separate from the file's image type (which
293d505a09cSSimon Glass * will always be IH_TYPE_FLATDT in this case).
294d505a09cSSimon Glass */
295d505a09cSSimon Glass if (params.type == IH_TYPE_FLATDT) {
29620deadddSSimon Glass params.fit_image_type = type ? type : IH_TYPE_KERNEL;
2973c23c0feSSimon Glass /* For auto_its, datafile is always 'auto' */
2988e35bb07SSimon Glass if (!params.auto_its)
299d505a09cSSimon Glass params.datafile = datafile;
300e324a925SSimon Glass else if (!params.datafile)
301e324a925SSimon Glass usage("Missing data file for auto-FIT (use -d)");
302d505a09cSSimon Glass } else if (type != IH_TYPE_INVALID) {
3038c84287aSAlex Kiernan if (type == IH_TYPE_SCRIPT && !params.datafile)
3048c84287aSAlex Kiernan usage("Missing data file for script (use -d)");
305d505a09cSSimon Glass params.type = type;
306d505a09cSSimon Glass }
307d505a09cSSimon Glass
308d505a09cSSimon Glass if (!params.imagefile)
30915310348SSimon Glass usage("Missing output filename");
3100b443deeSSimon Glass }
3110b443deeSSimon Glass
main(int argc,char ** argv)3120b443deeSSimon Glass int main(int argc, char **argv)
3130b443deeSSimon Glass {
3140b443deeSSimon Glass int ifd = -1;
3150b443deeSSimon Glass struct stat sbuf;
3160b443deeSSimon Glass char *ptr;
3170b443deeSSimon Glass int retval = 0;
3180b443deeSSimon Glass struct image_type_params *tparams = NULL;
3190b443deeSSimon Glass int pad_len = 0;
3200b443deeSSimon Glass int dfd;
3218961c8adSMark Tomlinson size_t map_len;
3220b443deeSSimon Glass
3230b443deeSSimon Glass params.cmdname = *argv;
3240b443deeSSimon Glass params.addr = 0;
3250b443deeSSimon Glass params.ep = 0;
3260b443deeSSimon Glass
3270b443deeSSimon Glass process_args(argc, argv);
3285b1d7137Swdenk
32989a4d6b1SPrafulla Wadaskar /* set tparams as per input type_id */
330a93648d1SGuilherme Maciel Ferreira tparams = imagetool_get_type(params.type);
33189a4d6b1SPrafulla Wadaskar if (tparams == NULL) {
33289a4d6b1SPrafulla Wadaskar fprintf (stderr, "%s: unsupported type %s\n",
33389a4d6b1SPrafulla Wadaskar params.cmdname, genimg_get_type_name(params.type));
33489a4d6b1SPrafulla Wadaskar exit (EXIT_FAILURE);
33589a4d6b1SPrafulla Wadaskar }
33689a4d6b1SPrafulla Wadaskar
33789a4d6b1SPrafulla Wadaskar /*
33889a4d6b1SPrafulla Wadaskar * check the passed arguments parameters meets the requirements
33989a4d6b1SPrafulla Wadaskar * as per image type to be generated/listed
34089a4d6b1SPrafulla Wadaskar */
34189a4d6b1SPrafulla Wadaskar if (tparams->check_params)
34289a4d6b1SPrafulla Wadaskar if (tparams->check_params (¶ms))
34315310348SSimon Glass usage("Bad parameters for image type");
34489a4d6b1SPrafulla Wadaskar
34589a4d6b1SPrafulla Wadaskar if (!params.eflag) {
34689a4d6b1SPrafulla Wadaskar params.ep = params.addr;
3475b1d7137Swdenk /* If XIP, entry point must be after the U-Boot header */
34889a4d6b1SPrafulla Wadaskar if (params.xflag)
34989a4d6b1SPrafulla Wadaskar params.ep += tparams->header_size;
3505b1d7137Swdenk }
3515b1d7137Swdenk
352c81296c1SPeter Tyser if (params.fflag){
353c81296c1SPeter Tyser if (tparams->fflag_handle)
354c81296c1SPeter Tyser /*
355c81296c1SPeter Tyser * in some cases, some additional processing needs
356c81296c1SPeter Tyser * to be done if fflag is defined
357c81296c1SPeter Tyser *
358c81296c1SPeter Tyser * For ex. fit_handle_file for Fit file support
359c81296c1SPeter Tyser */
360c81296c1SPeter Tyser retval = tparams->fflag_handle(¶ms);
361c81296c1SPeter Tyser
362c81296c1SPeter Tyser if (retval != EXIT_SUCCESS)
363c81296c1SPeter Tyser exit (retval);
364c81296c1SPeter Tyser }
365c81296c1SPeter Tyser
366c81296c1SPeter Tyser if (params.lflag || params.fflag) {
36789a4d6b1SPrafulla Wadaskar ifd = open (params.imagefile, O_RDONLY|O_BINARY);
3685b1d7137Swdenk } else {
36989a4d6b1SPrafulla Wadaskar ifd = open (params.imagefile,
3709d25438fSBartlomiej Sieka O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
3715b1d7137Swdenk }
3725b1d7137Swdenk
3735b1d7137Swdenk if (ifd < 0) {
3745b1d7137Swdenk fprintf (stderr, "%s: Can't open %s: %s\n",
37589a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile,
37689a4d6b1SPrafulla Wadaskar strerror(errno));
3775b1d7137Swdenk exit (EXIT_FAILURE);
3785b1d7137Swdenk }
3795b1d7137Swdenk
380c81296c1SPeter Tyser if (params.lflag || params.fflag) {
3815b1d7137Swdenk /*
3825b1d7137Swdenk * list header information of existing image
3835b1d7137Swdenk */
3845b1d7137Swdenk if (fstat(ifd, &sbuf) < 0) {
3855b1d7137Swdenk fprintf (stderr, "%s: Can't stat %s: %s\n",
38689a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile,
38789a4d6b1SPrafulla Wadaskar strerror(errno));
3885b1d7137Swdenk exit (EXIT_FAILURE);
3895b1d7137Swdenk }
3905b1d7137Swdenk
39189a4d6b1SPrafulla Wadaskar if ((unsigned)sbuf.st_size < tparams->header_size) {
3925b1d7137Swdenk fprintf (stderr,
39389a4d6b1SPrafulla Wadaskar "%s: Bad size: \"%s\" is not valid image\n",
39489a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile);
3955b1d7137Swdenk exit (EXIT_FAILURE);
3965b1d7137Swdenk }
3975b1d7137Swdenk
398fa956fdeSMike Frysinger ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
399fa956fdeSMike Frysinger if (ptr == MAP_FAILED) {
4005b1d7137Swdenk fprintf (stderr, "%s: Can't read %s: %s\n",
40189a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile,
40289a4d6b1SPrafulla Wadaskar strerror(errno));
4035b1d7137Swdenk exit (EXIT_FAILURE);
4045b1d7137Swdenk }
4055b1d7137Swdenk
40689a4d6b1SPrafulla Wadaskar /*
40789a4d6b1SPrafulla Wadaskar * scan through mkimage registry for all supported image types
40889a4d6b1SPrafulla Wadaskar * and verify the input image file header for match
40989a4d6b1SPrafulla Wadaskar * Print the image information for matched image type
41089a4d6b1SPrafulla Wadaskar * Returns the error code if not matched
41189a4d6b1SPrafulla Wadaskar */
412*d32aa3caSJordan Hand retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
4130ca6691cSGuilherme Maciel Ferreira tparams, ¶ms);
4145b1d7137Swdenk
4155b1d7137Swdenk (void) munmap((void *)ptr, sbuf.st_size);
4165b1d7137Swdenk (void) close (ifd);
4175b1d7137Swdenk
418f7644c0bSPrafulla Wadaskar exit (retval);
4195b1d7137Swdenk }
4205b1d7137Swdenk
42134633141SMarek Vasut if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
42292a655c3SSimon Glass dfd = open(params.datafile, O_RDONLY | O_BINARY);
42392a655c3SSimon Glass if (dfd < 0) {
42492a655c3SSimon Glass fprintf(stderr, "%s: Can't open %s: %s\n",
4256ae6e160SPhilippe De Swert params.cmdname, params.datafile,
4266ae6e160SPhilippe De Swert strerror(errno));
42792a655c3SSimon Glass exit(EXIT_FAILURE);
42892a655c3SSimon Glass }
42992a655c3SSimon Glass
43092a655c3SSimon Glass if (fstat(dfd, &sbuf) < 0) {
43192a655c3SSimon Glass fprintf(stderr, "%s: Can't stat %s: %s\n",
4326ae6e160SPhilippe De Swert params.cmdname, params.datafile,
4336ae6e160SPhilippe De Swert strerror(errno));
43492a655c3SSimon Glass exit(EXIT_FAILURE);
43592a655c3SSimon Glass }
43692a655c3SSimon Glass
43792a655c3SSimon Glass params.file_size = sbuf.st_size + tparams->header_size;
43892a655c3SSimon Glass close(dfd);
4396ae6e160SPhilippe De Swert }
44092a655c3SSimon Glass
4415b1d7137Swdenk /*
442f0662105SStefano Babic * In case there an header with a variable
443f0662105SStefano Babic * length will be added, the corresponding
444f0662105SStefano Babic * function is called. This is responsible to
445f0662105SStefano Babic * allocate memory for the header itself.
4465b1d7137Swdenk */
447f0662105SStefano Babic if (tparams->vrec_header)
4489bac0bb3SStefano Babic pad_len = tparams->vrec_header(¶ms, tparams);
449f0662105SStefano Babic else
45089a4d6b1SPrafulla Wadaskar memset(tparams->hdr, 0, tparams->header_size);
4515b1d7137Swdenk
45289a4d6b1SPrafulla Wadaskar if (write(ifd, tparams->hdr, tparams->header_size)
45389a4d6b1SPrafulla Wadaskar != tparams->header_size) {
4545b1d7137Swdenk fprintf (stderr, "%s: Write error on %s: %s\n",
45589a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile, strerror(errno));
4565b1d7137Swdenk exit (EXIT_FAILURE);
4575b1d7137Swdenk }
4585b1d7137Swdenk
459d1be8f92SChristian Riesch if (!params.skipcpy) {
460d1be8f92SChristian Riesch if (params.type == IH_TYPE_MULTI ||
461d1be8f92SChristian Riesch params.type == IH_TYPE_SCRIPT) {
46289a4d6b1SPrafulla Wadaskar char *file = params.datafile;
4633bb66806SWolfgang Denk uint32_t size;
4645b1d7137Swdenk
4655b1d7137Swdenk for (;;) {
4665b1d7137Swdenk char *sep = NULL;
4675b1d7137Swdenk
4685b1d7137Swdenk if (file) {
4695b1d7137Swdenk if ((sep = strchr(file, ':')) != NULL) {
4705b1d7137Swdenk *sep = '\0';
4715b1d7137Swdenk }
4725b1d7137Swdenk
4735b1d7137Swdenk if (stat (file, &sbuf) < 0) {
4745b1d7137Swdenk fprintf (stderr, "%s: Can't stat %s: %s\n",
47589a4d6b1SPrafulla Wadaskar params.cmdname, file, strerror(errno));
4765b1d7137Swdenk exit (EXIT_FAILURE);
4775b1d7137Swdenk }
4789a4daad0SMarian Balakowicz size = cpu_to_uimage (sbuf.st_size);
4795b1d7137Swdenk } else {
4805b1d7137Swdenk size = 0;
4815b1d7137Swdenk }
4825b1d7137Swdenk
4835b1d7137Swdenk if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
4845b1d7137Swdenk fprintf (stderr, "%s: Write error on %s: %s\n",
48589a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile,
48689a4d6b1SPrafulla Wadaskar strerror(errno));
4875b1d7137Swdenk exit (EXIT_FAILURE);
4885b1d7137Swdenk }
4895b1d7137Swdenk
4905b1d7137Swdenk if (!file) {
4915b1d7137Swdenk break;
4925b1d7137Swdenk }
4935b1d7137Swdenk
4945b1d7137Swdenk if (sep) {
4955b1d7137Swdenk *sep = ':';
4965b1d7137Swdenk file = sep + 1;
4975b1d7137Swdenk } else {
4985b1d7137Swdenk file = NULL;
4995b1d7137Swdenk }
5005b1d7137Swdenk }
5015b1d7137Swdenk
50289a4d6b1SPrafulla Wadaskar file = params.datafile;
5035b1d7137Swdenk
5045b1d7137Swdenk for (;;) {
5055b1d7137Swdenk char *sep = strchr(file, ':');
5065b1d7137Swdenk if (sep) {
5075b1d7137Swdenk *sep = '\0';
5085b1d7137Swdenk copy_file (ifd, file, 1);
5095b1d7137Swdenk *sep++ = ':';
5105b1d7137Swdenk file = sep;
5115b1d7137Swdenk } else {
5125b1d7137Swdenk copy_file (ifd, file, 0);
5135b1d7137Swdenk break;
5145b1d7137Swdenk }
5155b1d7137Swdenk }
5165d898a00SShaohui Xie } else if (params.type == IH_TYPE_PBLIMAGE) {
5175d898a00SShaohui Xie /* PBL has special Image format, implements its' own */
5185d898a00SShaohui Xie pbl_load_uboot(ifd, ¶ms);
5196915dcf3SAlexander Graf } else if (params.type == IH_TYPE_ZYNQMPBIF) {
5206915dcf3SAlexander Graf /* Image file is meta, walk through actual targets */
5216915dcf3SAlexander Graf int ret;
5226915dcf3SAlexander Graf
5236915dcf3SAlexander Graf ret = zynqmpbif_copy_image(ifd, ¶ms);
5246915dcf3SAlexander Graf if (ret)
5256915dcf3SAlexander Graf return ret;
526a2b96eceSPeng Fan } else if (params.type == IH_TYPE_IMX8IMAGE) {
527a2b96eceSPeng Fan /* i.MX8/8X has special Image format */
528a2b96eceSPeng Fan int ret;
529a2b96eceSPeng Fan
530a2b96eceSPeng Fan ret = imx8image_copy_image(ifd, ¶ms);
531a2b96eceSPeng Fan if (ret)
532a2b96eceSPeng Fan return ret;
5336609c266SPeng Fan } else if (params.type == IH_TYPE_IMX8MIMAGE) {
5346609c266SPeng Fan /* i.MX8M has special Image format */
5356609c266SPeng Fan int ret;
5366609c266SPeng Fan
5376609c266SPeng Fan ret = imx8mimage_copy_image(ifd, ¶ms);
5386609c266SPeng Fan if (ret)
5396609c266SPeng Fan return ret;
5405b1d7137Swdenk } else {
5419bac0bb3SStefano Babic copy_file(ifd, params.datafile, pad_len);
5425b1d7137Swdenk }
543d21bd69bSSven Ebenfeld if (params.type == IH_TYPE_FIRMWARE_IVT) {
544d21bd69bSSven Ebenfeld /* Add alignment and IVT */
545d21bd69bSSven Ebenfeld uint32_t aligned_filesize = (params.file_size + 0x1000
546d21bd69bSSven Ebenfeld - 1) & ~(0x1000 - 1);
547d21bd69bSSven Ebenfeld flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
548d21bd69bSSven Ebenfeld params.addr, 0, 0, 0, params.addr
549d21bd69bSSven Ebenfeld + aligned_filesize
550d21bd69bSSven Ebenfeld - tparams->header_size,
551d21bd69bSSven Ebenfeld params.addr + aligned_filesize
552d21bd69bSSven Ebenfeld - tparams->header_size
553d21bd69bSSven Ebenfeld + 0x20, 0 };
554d21bd69bSSven Ebenfeld int i = params.file_size;
555d21bd69bSSven Ebenfeld for (; i < aligned_filesize; i++) {
556b4e923a8SSven Ebenfeld if (write(ifd, (char *) &i, 1) != 1) {
557d21bd69bSSven Ebenfeld fprintf(stderr,
558d21bd69bSSven Ebenfeld "%s: Write error on %s: %s\n",
559d21bd69bSSven Ebenfeld params.cmdname,
560d21bd69bSSven Ebenfeld params.imagefile,
561d21bd69bSSven Ebenfeld strerror(errno));
562d21bd69bSSven Ebenfeld exit(EXIT_FAILURE);
563d21bd69bSSven Ebenfeld }
564d21bd69bSSven Ebenfeld }
565d21bd69bSSven Ebenfeld if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
566d21bd69bSSven Ebenfeld != sizeof(flash_header_v2_t)) {
567d21bd69bSSven Ebenfeld fprintf(stderr, "%s: Write error on %s: %s\n",
568d21bd69bSSven Ebenfeld params.cmdname,
569d21bd69bSSven Ebenfeld params.imagefile,
570d21bd69bSSven Ebenfeld strerror(errno));
571d21bd69bSSven Ebenfeld exit(EXIT_FAILURE);
572d21bd69bSSven Ebenfeld }
573d21bd69bSSven Ebenfeld }
574d1be8f92SChristian Riesch }
5755b1d7137Swdenk
5765b1d7137Swdenk /* We're a bit of paranoid */
57789a4d6b1SPrafulla Wadaskar #if defined(_POSIX_SYNCHRONIZED_IO) && \
57889a4d6b1SPrafulla Wadaskar !defined(__sun__) && \
57989a4d6b1SPrafulla Wadaskar !defined(__FreeBSD__) && \
58031cbe80cSLuka Perkov !defined(__OpenBSD__) && \
58189a4d6b1SPrafulla Wadaskar !defined(__APPLE__)
5825b1d7137Swdenk (void) fdatasync (ifd);
5835b1d7137Swdenk #else
5845b1d7137Swdenk (void) fsync (ifd);
5855b1d7137Swdenk #endif
5865b1d7137Swdenk
5875b1d7137Swdenk if (fstat(ifd, &sbuf) < 0) {
5885b1d7137Swdenk fprintf (stderr, "%s: Can't stat %s: %s\n",
58989a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile, strerror(errno));
5905b1d7137Swdenk exit (EXIT_FAILURE);
5915b1d7137Swdenk }
59292a655c3SSimon Glass params.file_size = sbuf.st_size;
5935b1d7137Swdenk
5948961c8adSMark Tomlinson map_len = sbuf.st_size;
5958961c8adSMark Tomlinson ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
596fa956fdeSMike Frysinger if (ptr == MAP_FAILED) {
5975b1d7137Swdenk fprintf (stderr, "%s: Can't map %s: %s\n",
59889a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile, strerror(errno));
5995b1d7137Swdenk exit (EXIT_FAILURE);
6005b1d7137Swdenk }
6015b1d7137Swdenk
60289a4d6b1SPrafulla Wadaskar /* Setup the image header as per input image type*/
60389a4d6b1SPrafulla Wadaskar if (tparams->set_header)
60489a4d6b1SPrafulla Wadaskar tparams->set_header (ptr, &sbuf, ifd, ¶ms);
60589a4d6b1SPrafulla Wadaskar else {
60689a4d6b1SPrafulla Wadaskar fprintf (stderr, "%s: Can't set header for %s: %s\n",
60789a4d6b1SPrafulla Wadaskar params.cmdname, tparams->name, strerror(errno));
60889a4d6b1SPrafulla Wadaskar exit (EXIT_FAILURE);
60989a4d6b1SPrafulla Wadaskar }
6105b1d7137Swdenk
61189a4d6b1SPrafulla Wadaskar /* Print the image information by processing image header */
61289a4d6b1SPrafulla Wadaskar if (tparams->print_header)
61389a4d6b1SPrafulla Wadaskar tparams->print_header (ptr);
61489a4d6b1SPrafulla Wadaskar else {
615004d0091SGuillaume GARDET fprintf (stderr, "%s: Can't print header for %s\n",
616004d0091SGuillaume GARDET params.cmdname, tparams->name);
61789a4d6b1SPrafulla Wadaskar }
6185b1d7137Swdenk
6198961c8adSMark Tomlinson (void)munmap((void *)ptr, map_len);
6205b1d7137Swdenk
6215b1d7137Swdenk /* We're a bit of paranoid */
62289a4d6b1SPrafulla Wadaskar #if defined(_POSIX_SYNCHRONIZED_IO) && \
62389a4d6b1SPrafulla Wadaskar !defined(__sun__) && \
62489a4d6b1SPrafulla Wadaskar !defined(__FreeBSD__) && \
62531cbe80cSLuka Perkov !defined(__OpenBSD__) && \
62689a4d6b1SPrafulla Wadaskar !defined(__APPLE__)
6275b1d7137Swdenk (void) fdatasync (ifd);
6285b1d7137Swdenk #else
6295b1d7137Swdenk (void) fsync (ifd);
6305b1d7137Swdenk #endif
6315b1d7137Swdenk
6325b1d7137Swdenk if (close(ifd)) {
6335b1d7137Swdenk fprintf (stderr, "%s: Write error on %s: %s\n",
63489a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile, strerror(errno));
6355b1d7137Swdenk exit (EXIT_FAILURE);
6365b1d7137Swdenk }
6375b1d7137Swdenk
6385b1d7137Swdenk exit (EXIT_SUCCESS);
6395b1d7137Swdenk }
6405b1d7137Swdenk
6415b1d7137Swdenk static void
copy_file(int ifd,const char * datafile,int pad)6425b1d7137Swdenk copy_file (int ifd, const char *datafile, int pad)
6435b1d7137Swdenk {
6445b1d7137Swdenk int dfd;
6455b1d7137Swdenk struct stat sbuf;
6465b1d7137Swdenk unsigned char *ptr;
6475b1d7137Swdenk int tail;
6485b1d7137Swdenk int zero = 0;
6499bac0bb3SStefano Babic uint8_t zeros[4096];
6505b1d7137Swdenk int offset = 0;
6515b1d7137Swdenk int size;
652a93648d1SGuilherme Maciel Ferreira struct image_type_params *tparams = imagetool_get_type(params.type);
6535b1d7137Swdenk
6549bac0bb3SStefano Babic memset(zeros, 0, sizeof(zeros));
6559bac0bb3SStefano Babic
65689a4d6b1SPrafulla Wadaskar if (params.vflag) {
6575b1d7137Swdenk fprintf (stderr, "Adding Image %s\n", datafile);
6585b1d7137Swdenk }
6595b1d7137Swdenk
660ef1464ccSwdenk if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
6615b1d7137Swdenk fprintf (stderr, "%s: Can't open %s: %s\n",
66289a4d6b1SPrafulla Wadaskar params.cmdname, datafile, strerror(errno));
6635b1d7137Swdenk exit (EXIT_FAILURE);
6645b1d7137Swdenk }
6655b1d7137Swdenk
6665b1d7137Swdenk if (fstat(dfd, &sbuf) < 0) {
6675b1d7137Swdenk fprintf (stderr, "%s: Can't stat %s: %s\n",
66889a4d6b1SPrafulla Wadaskar params.cmdname, datafile, strerror(errno));
6695b1d7137Swdenk exit (EXIT_FAILURE);
6705b1d7137Swdenk }
6715b1d7137Swdenk
672fa956fdeSMike Frysinger ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
673fa956fdeSMike Frysinger if (ptr == MAP_FAILED) {
6745b1d7137Swdenk fprintf (stderr, "%s: Can't read %s: %s\n",
67589a4d6b1SPrafulla Wadaskar params.cmdname, datafile, strerror(errno));
6765b1d7137Swdenk exit (EXIT_FAILURE);
6775b1d7137Swdenk }
6785b1d7137Swdenk
67989a4d6b1SPrafulla Wadaskar if (params.xflag) {
6805b1d7137Swdenk unsigned char *p = NULL;
6815b1d7137Swdenk /*
6825b1d7137Swdenk * XIP: do not append the image_header_t at the
6835b1d7137Swdenk * beginning of the file, but consume the space
6845b1d7137Swdenk * reserved for it.
6855b1d7137Swdenk */
6865b1d7137Swdenk
68789a4d6b1SPrafulla Wadaskar if ((unsigned)sbuf.st_size < tparams->header_size) {
6885b1d7137Swdenk fprintf (stderr,
6895b1d7137Swdenk "%s: Bad size: \"%s\" is too small for XIP\n",
69089a4d6b1SPrafulla Wadaskar params.cmdname, datafile);
6915b1d7137Swdenk exit (EXIT_FAILURE);
6925b1d7137Swdenk }
6935b1d7137Swdenk
69489a4d6b1SPrafulla Wadaskar for (p = ptr; p < ptr + tparams->header_size; p++) {
6955b1d7137Swdenk if ( *p != 0xff ) {
6965b1d7137Swdenk fprintf (stderr,
6975b1d7137Swdenk "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
69889a4d6b1SPrafulla Wadaskar params.cmdname, datafile);
6995b1d7137Swdenk exit (EXIT_FAILURE);
7005b1d7137Swdenk }
7015b1d7137Swdenk }
7025b1d7137Swdenk
70389a4d6b1SPrafulla Wadaskar offset = tparams->header_size;
7045b1d7137Swdenk }
7055b1d7137Swdenk
7065b1d7137Swdenk size = sbuf.st_size - offset;
7075b1d7137Swdenk if (write(ifd, ptr + offset, size) != size) {
7085b1d7137Swdenk fprintf (stderr, "%s: Write error on %s: %s\n",
70989a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile, strerror(errno));
7105b1d7137Swdenk exit (EXIT_FAILURE);
7115b1d7137Swdenk }
7125b1d7137Swdenk
7139bac0bb3SStefano Babic tail = size % 4;
7149bac0bb3SStefano Babic if ((pad == 1) && (tail != 0)) {
7155b1d7137Swdenk
7165b1d7137Swdenk if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
7175b1d7137Swdenk fprintf (stderr, "%s: Write error on %s: %s\n",
71889a4d6b1SPrafulla Wadaskar params.cmdname, params.imagefile,
71989a4d6b1SPrafulla Wadaskar strerror(errno));
7205b1d7137Swdenk exit (EXIT_FAILURE);
7215b1d7137Swdenk }
7229bac0bb3SStefano Babic } else if (pad > 1) {
723424b86aeSSimon Glass while (pad > 0) {
724424b86aeSSimon Glass int todo = sizeof(zeros);
725424b86aeSSimon Glass
726424b86aeSSimon Glass if (todo > pad)
727424b86aeSSimon Glass todo = pad;
728424b86aeSSimon Glass if (write(ifd, (char *)&zeros, todo) != todo) {
7299bac0bb3SStefano Babic fprintf(stderr, "%s: Write error on %s: %s\n",
7309bac0bb3SStefano Babic params.cmdname, params.imagefile,
7319bac0bb3SStefano Babic strerror(errno));
7329bac0bb3SStefano Babic exit(EXIT_FAILURE);
7339bac0bb3SStefano Babic }
734424b86aeSSimon Glass pad -= todo;
735424b86aeSSimon Glass }
7365b1d7137Swdenk }
7375b1d7137Swdenk
7385b1d7137Swdenk (void) munmap((void *)ptr, sbuf.st_size);
7395b1d7137Swdenk (void) close (dfd);
7405b1d7137Swdenk }
741