blob: e5481435a7644add00810f7d6a7548762d2cb79a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -07002/*
3 * Based on mkimage.c.
4 *
5 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -07006 */
7
8#include "dumpimage.h"
9#include <image.h>
10#include <version.h>
11
12static void usage(void);
13
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070014/* parameters initialized by core will be used by the image type code */
15static struct image_tool_params params = {
16 .type = IH_TYPE_KERNEL,
17};
18
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070019/*
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020020 * dumpimage_extract_subimage -
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070021 *
22 * It scans all registered image types,
23 * verifies image_header for each supported image type
24 * if verification is successful, it extracts the desired file,
25 * indexed by pflag, from the image
26 *
27 * returns negative if input image format does not match with any of
28 * supported image types
29 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020030static int dumpimage_extract_subimage(struct image_type_params *tparams,
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020031 void *ptr, struct stat *sbuf)
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070032{
33 int retval = -1;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070034
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020035 if (tparams->verify_header) {
36 retval = tparams->verify_header((unsigned char *)ptr,
37 sbuf->st_size, &params);
Andrew F. Davis79e984c2019-09-17 17:09:33 -040038 if (retval != 0) {
39 fprintf(stderr, "%s: failed to verify header of %s\n",
40 params.cmdname, tparams->name);
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020041 return -1;
Andrew F. Davis79e984c2019-09-17 17:09:33 -040042 }
43
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020044 /*
45 * Extract the file from the image
46 * if verify is successful
47 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020048 if (tparams->extract_subimage) {
49 retval = tparams->extract_subimage(ptr, &params);
Andrew F. Davis79e984c2019-09-17 17:09:33 -040050 if (retval != 0) {
51 fprintf(stderr, "%s: extract_subimage failed for %s\n",
52 params.cmdname, tparams->name);
53 return -3;
54 }
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020055 } else {
56 fprintf(stderr,
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020057 "%s: extract_subimage undefined for %s\n",
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020058 params.cmdname, tparams->name);
59 return -2;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070060 }
61 }
62
63 return retval;
64}
65
66int main(int argc, char **argv)
67{
68 int opt;
69 int ifd = -1;
70 struct stat sbuf;
71 char *ptr;
Martyn Welch11e3c1f2019-01-26 02:31:52 +000072 int retval = EXIT_SUCCESS;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070073 struct image_type_params *tparams = NULL;
74
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070075 params.cmdname = *argv;
76
Martyn Welch65a80b42019-01-26 02:31:53 +000077 while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070078 switch (opt) {
79 case 'l':
80 params.lflag = 1;
81 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070082 case 'o':
83 params.outfile = optarg;
Martyn Welch12b83182019-01-26 02:31:51 +000084 params.iflag = 1;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070085 break;
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020086 case 'T':
87 params.type = genimg_get_type_id(optarg);
88 if (params.type < 0) {
Martyn Welch57a608e2019-01-26 02:31:50 +000089 fprintf(stderr, "%s: Invalid type\n",
90 params.cmdname);
Martyn Welch65a80b42019-01-26 02:31:53 +000091 exit(EXIT_FAILURE);
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020092 }
93 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070094 case 'p':
95 params.pflag = strtoul(optarg, &ptr, 10);
96 if (*ptr) {
97 fprintf(stderr,
98 "%s: invalid file position %s\n",
99 params.cmdname, *argv);
100 exit(EXIT_FAILURE);
101 }
102 break;
103 case 'V':
104 printf("dumpimage version %s\n", PLAIN_VERSION);
105 exit(EXIT_SUCCESS);
Martyn Welch65a80b42019-01-26 02:31:53 +0000106 case 'h':
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700107 default:
108 usage();
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200109 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700110 }
111 }
112
Martyn Welch65a80b42019-01-26 02:31:53 +0000113 if (argc < 2)
114 usage();
115
Martyn Welch57a608e2019-01-26 02:31:50 +0000116 if (optind >= argc) {
117 fprintf(stderr, "%s: image file missing\n", params.cmdname);
Martyn Welch65a80b42019-01-26 02:31:53 +0000118 exit(EXIT_FAILURE);
Martyn Welch57a608e2019-01-26 02:31:50 +0000119 }
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700120
Martyn Welch12b83182019-01-26 02:31:51 +0000121 params.imagefile = argv[optind];
122
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700123 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200124 tparams = imagetool_get_type(params.type);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700125 if (tparams == NULL) {
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200126 fprintf(stderr, "%s: unsupported type: %s\n",
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700127 params.cmdname, genimg_get_type_name(params.type));
128 exit(EXIT_FAILURE);
129 }
130
131 /*
132 * check the passed arguments parameters meets the requirements
133 * as per image type to be generated/listed
134 */
135 if (tparams->check_params) {
Martyn Welch57a608e2019-01-26 02:31:50 +0000136 if (tparams->check_params(&params)) {
137 fprintf(stderr, "%s: Parameter check failed\n",
138 params.cmdname);
Martyn Welch65a80b42019-01-26 02:31:53 +0000139 exit(EXIT_FAILURE);
Martyn Welch57a608e2019-01-26 02:31:50 +0000140 }
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700141 }
142
Martyn Welch12b83182019-01-26 02:31:51 +0000143 if (!params.lflag && !params.outfile) {
144 fprintf(stderr, "%s: No output file provided\n",
145 params.cmdname);
146 exit(EXIT_FAILURE);
147 }
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700148
149 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
150 if (ifd < 0) {
Martyn Welch11e3c1f2019-01-26 02:31:52 +0000151 fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname,
152 params.imagefile, strerror(errno));
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700153 exit(EXIT_FAILURE);
154 }
155
Martyn Welch11e3c1f2019-01-26 02:31:52 +0000156 if (fstat(ifd, &sbuf) < 0) {
157 fprintf(stderr, "%s: Can't stat \"%s\": %s\n", params.cmdname,
158 params.imagefile, strerror(errno));
159 exit(EXIT_FAILURE);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700160 }
161
Martyn Welch11e3c1f2019-01-26 02:31:52 +0000162 if ((uint32_t)sbuf.st_size < tparams->header_size) {
163 fprintf(stderr, "%s: Bad size: \"%s\" is not valid image\n",
164 params.cmdname, params.imagefile);
165 exit(EXIT_FAILURE);
166 }
167
168 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
169 if (ptr == MAP_FAILED) {
170 fprintf(stderr, "%s: Can't read \"%s\": %s\n", params.cmdname,
171 params.imagefile, strerror(errno));
172 exit(EXIT_FAILURE);
173 }
174
175 /*
176 * Both calls bellow scan through dumpimage registry for all
177 * supported image types and verify the input image file
178 * header for match
179 */
180 if (params.iflag) {
181 /*
182 * Extract the data files from within the matched
183 * image type. Returns the error code if not matched
184 */
185 retval = dumpimage_extract_subimage(tparams, ptr, &sbuf);
Andrew F. Davis79e984c2019-09-17 17:09:33 -0400186 if (retval)
187 fprintf(stderr, "%s: Can't extract subimage from %s\n",
188 params.cmdname, params.imagefile);
Martyn Welch11e3c1f2019-01-26 02:31:52 +0000189 } else {
190 /*
191 * Print the image information for matched image type
192 * Returns the error code if not matched
193 */
194 retval = imagetool_verify_print_header(ptr, &sbuf, tparams,
195 &params);
196 }
197
198 (void)munmap((void *)ptr, sbuf.st_size);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700199 (void)close(ifd);
200
Martyn Welch11e3c1f2019-01-26 02:31:52 +0000201 return retval;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700202}
203
204static void usage(void)
205{
206 fprintf(stderr, "Usage: %s -l image\n"
207 " -l ==> list image header information\n",
208 params.cmdname);
209 fprintf(stderr,
Martyn Welche3b4fc92019-01-26 02:31:54 +0000210 " %s [-T type] [-p position] [-o outfile] image\n"
211 " -T ==> declare image type as 'type'\n"
212 " -p ==> 'position' (starting at 0) of the component to extract from image\n"
213 " -o ==> extract component to file 'outfile'\n",
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700214 params.cmdname);
215 fprintf(stderr,
Martyn Welch65a80b42019-01-26 02:31:53 +0000216 " %s -h ==> print usage information and exit\n",
217 params.cmdname);
218 fprintf(stderr,
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700219 " %s -V ==> print version information and exit\n",
220 params.cmdname);
221
Martyn Welch65a80b42019-01-26 02:31:53 +0000222 exit(EXIT_SUCCESS);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700223}