blob: 1b92dec364eb5770f8f08884bf0e8f633967b9c2 [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);
38 if (retval != 0)
39 return -1;
40 /*
41 * Extract the file from the image
42 * if verify is successful
43 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020044 if (tparams->extract_subimage) {
45 retval = tparams->extract_subimage(ptr, &params);
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020046 } else {
47 fprintf(stderr,
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020048 "%s: extract_subimage undefined for %s\n",
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020049 params.cmdname, tparams->name);
50 return -2;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070051 }
52 }
53
54 return retval;
55}
56
57int main(int argc, char **argv)
58{
59 int opt;
60 int ifd = -1;
61 struct stat sbuf;
62 char *ptr;
63 int retval = 0;
64 struct image_type_params *tparams = NULL;
65
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070066 params.cmdname = *argv;
67
Martyn Welch12b83182019-01-26 02:31:51 +000068 while ((opt = getopt(argc, argv, "lo:T:p:V")) != -1) {
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070069 switch (opt) {
70 case 'l':
71 params.lflag = 1;
72 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070073 case 'o':
74 params.outfile = optarg;
Martyn Welch12b83182019-01-26 02:31:51 +000075 params.iflag = 1;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070076 break;
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020077 case 'T':
78 params.type = genimg_get_type_id(optarg);
79 if (params.type < 0) {
Martyn Welch57a608e2019-01-26 02:31:50 +000080 fprintf(stderr, "%s: Invalid type\n",
81 params.cmdname);
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020082 usage();
83 }
84 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070085 case 'p':
86 params.pflag = strtoul(optarg, &ptr, 10);
87 if (*ptr) {
88 fprintf(stderr,
89 "%s: invalid file position %s\n",
90 params.cmdname, *argv);
91 exit(EXIT_FAILURE);
92 }
93 break;
94 case 'V':
95 printf("dumpimage version %s\n", PLAIN_VERSION);
96 exit(EXIT_SUCCESS);
97 default:
98 usage();
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020099 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700100 }
101 }
102
Martyn Welch57a608e2019-01-26 02:31:50 +0000103 if (optind >= argc) {
104 fprintf(stderr, "%s: image file missing\n", params.cmdname);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700105 usage();
Martyn Welch57a608e2019-01-26 02:31:50 +0000106 }
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700107
Martyn Welch12b83182019-01-26 02:31:51 +0000108 params.imagefile = argv[optind];
109
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700110 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200111 tparams = imagetool_get_type(params.type);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700112 if (tparams == NULL) {
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200113 fprintf(stderr, "%s: unsupported type: %s\n",
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700114 params.cmdname, genimg_get_type_name(params.type));
115 exit(EXIT_FAILURE);
116 }
117
118 /*
119 * check the passed arguments parameters meets the requirements
120 * as per image type to be generated/listed
121 */
122 if (tparams->check_params) {
Martyn Welch57a608e2019-01-26 02:31:50 +0000123 if (tparams->check_params(&params)) {
124 fprintf(stderr, "%s: Parameter check failed\n",
125 params.cmdname);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700126 usage();
Martyn Welch57a608e2019-01-26 02:31:50 +0000127 }
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700128 }
129
Martyn Welch12b83182019-01-26 02:31:51 +0000130 if (!params.lflag && !params.outfile) {
131 fprintf(stderr, "%s: No output file provided\n",
132 params.cmdname);
133 exit(EXIT_FAILURE);
134 }
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700135
136 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
137 if (ifd < 0) {
138 fprintf(stderr, "%s: Can't open \"%s\": %s\n",
139 params.cmdname, params.imagefile,
140 strerror(errno));
141 exit(EXIT_FAILURE);
142 }
143
144 if (params.lflag || params.iflag) {
145 if (fstat(ifd, &sbuf) < 0) {
146 fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
147 params.cmdname, params.imagefile,
148 strerror(errno));
149 exit(EXIT_FAILURE);
150 }
151
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200152 if ((uint32_t)sbuf.st_size < tparams->header_size) {
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700153 fprintf(stderr,
154 "%s: Bad size: \"%s\" is not valid image\n",
155 params.cmdname, params.imagefile);
156 exit(EXIT_FAILURE);
157 }
158
159 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
160 if (ptr == MAP_FAILED) {
161 fprintf(stderr, "%s: Can't read \"%s\": %s\n",
162 params.cmdname, params.imagefile,
163 strerror(errno));
164 exit(EXIT_FAILURE);
165 }
166
167 /*
168 * Both calls bellow scan through dumpimage registry for all
169 * supported image types and verify the input image file
170 * header for match
171 */
172 if (params.iflag) {
173 /*
174 * Extract the data files from within the matched
175 * image type. Returns the error code if not matched
176 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -0200177 retval = dumpimage_extract_subimage(tparams, ptr,
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200178 &sbuf);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700179 } else {
180 /*
181 * Print the image information for matched image type
182 * Returns the error code if not matched
183 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200184 retval = imagetool_verify_print_header(ptr, &sbuf,
185 tparams, &params);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700186 }
187
188 (void)munmap((void *)ptr, sbuf.st_size);
189 (void)close(ifd);
190
191 return retval;
192 }
193
194 (void)close(ifd);
195
196 return EXIT_SUCCESS;
197}
198
199static void usage(void)
200{
201 fprintf(stderr, "Usage: %s -l image\n"
202 " -l ==> list image header information\n",
203 params.cmdname);
204 fprintf(stderr,
Martyn Welch12b83182019-01-26 02:31:51 +0000205 " %s -T type [-p position] [-o outfile] image\n"
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200206 " -T ==> set image type to 'type'\n"
Martyn Welch12b83182019-01-26 02:31:51 +0000207 " -p ==> 'position' (starting at 0) of the component to extract from image\n",
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700208 params.cmdname);
209 fprintf(stderr,
210 " %s -V ==> print version information and exit\n",
211 params.cmdname);
212
213 exit(EXIT_FAILURE);
214}