blob: 75a5d4762cda6b1e31432a1e419b3d8400e7751e [file] [log] [blame]
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -07001/*
2 * Based on mkimage.c.
3 *
4 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include "dumpimage.h"
10#include <image.h>
11#include <version.h>
12
13static void usage(void);
14
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070015/* parameters initialized by core will be used by the image type code */
16static struct image_tool_params params = {
17 .type = IH_TYPE_KERNEL,
18};
19
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070020/*
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020021 * dumpimage_extract_subimage -
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070022 *
23 * It scans all registered image types,
24 * verifies image_header for each supported image type
25 * if verification is successful, it extracts the desired file,
26 * indexed by pflag, from the image
27 *
28 * returns negative if input image format does not match with any of
29 * supported image types
30 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020031static int dumpimage_extract_subimage(struct image_type_params *tparams,
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020032 void *ptr, struct stat *sbuf)
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070033{
34 int retval = -1;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070035
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020036 if (tparams->verify_header) {
37 retval = tparams->verify_header((unsigned char *)ptr,
38 sbuf->st_size, &params);
39 if (retval != 0)
40 return -1;
41 /*
42 * Extract the file from the image
43 * if verify is successful
44 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020045 if (tparams->extract_subimage) {
46 retval = tparams->extract_subimage(ptr, &params);
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020047 } else {
48 fprintf(stderr,
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020049 "%s: extract_subimage undefined for %s\n",
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020050 params.cmdname, tparams->name);
51 return -2;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070052 }
53 }
54
55 return retval;
56}
57
58int main(int argc, char **argv)
59{
60 int opt;
61 int ifd = -1;
62 struct stat sbuf;
63 char *ptr;
64 int retval = 0;
65 struct image_type_params *tparams = NULL;
66
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070067 params.cmdname = *argv;
68
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020069 while ((opt = getopt(argc, argv, "li:o:T:p:V")) != -1) {
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070070 switch (opt) {
71 case 'l':
72 params.lflag = 1;
73 break;
74 case 'i':
75 params.imagefile = optarg;
76 params.iflag = 1;
77 break;
78 case 'o':
79 params.outfile = optarg;
80 break;
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020081 case 'T':
82 params.type = genimg_get_type_id(optarg);
83 if (params.type < 0) {
84 usage();
85 }
86 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070087 case 'p':
88 params.pflag = strtoul(optarg, &ptr, 10);
89 if (*ptr) {
90 fprintf(stderr,
91 "%s: invalid file position %s\n",
92 params.cmdname, *argv);
93 exit(EXIT_FAILURE);
94 }
95 break;
96 case 'V':
97 printf("dumpimage version %s\n", PLAIN_VERSION);
98 exit(EXIT_SUCCESS);
99 default:
100 usage();
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200101 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700102 }
103 }
104
105 if (optind >= argc)
106 usage();
107
108 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200109 tparams = imagetool_get_type(params.type);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700110 if (tparams == NULL) {
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200111 fprintf(stderr, "%s: unsupported type: %s\n",
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700112 params.cmdname, genimg_get_type_name(params.type));
113 exit(EXIT_FAILURE);
114 }
115
116 /*
117 * check the passed arguments parameters meets the requirements
118 * as per image type to be generated/listed
119 */
120 if (tparams->check_params) {
121 if (tparams->check_params(&params))
122 usage();
123 }
124
125 if (params.iflag)
126 params.datafile = argv[optind];
127 else
128 params.imagefile = argv[optind];
129 if (!params.outfile)
130 params.outfile = params.datafile;
131
132 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
133 if (ifd < 0) {
134 fprintf(stderr, "%s: Can't open \"%s\": %s\n",
135 params.cmdname, params.imagefile,
136 strerror(errno));
137 exit(EXIT_FAILURE);
138 }
139
140 if (params.lflag || params.iflag) {
141 if (fstat(ifd, &sbuf) < 0) {
142 fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
143 params.cmdname, params.imagefile,
144 strerror(errno));
145 exit(EXIT_FAILURE);
146 }
147
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200148 if ((uint32_t)sbuf.st_size < tparams->header_size) {
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700149 fprintf(stderr,
150 "%s: Bad size: \"%s\" is not valid image\n",
151 params.cmdname, params.imagefile);
152 exit(EXIT_FAILURE);
153 }
154
155 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
156 if (ptr == MAP_FAILED) {
157 fprintf(stderr, "%s: Can't read \"%s\": %s\n",
158 params.cmdname, params.imagefile,
159 strerror(errno));
160 exit(EXIT_FAILURE);
161 }
162
163 /*
164 * Both calls bellow scan through dumpimage registry for all
165 * supported image types and verify the input image file
166 * header for match
167 */
168 if (params.iflag) {
169 /*
170 * Extract the data files from within the matched
171 * image type. Returns the error code if not matched
172 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -0200173 retval = dumpimage_extract_subimage(tparams, ptr,
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200174 &sbuf);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700175 } else {
176 /*
177 * Print the image information for matched image type
178 * Returns the error code if not matched
179 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200180 retval = imagetool_verify_print_header(ptr, &sbuf,
181 tparams, &params);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700182 }
183
184 (void)munmap((void *)ptr, sbuf.st_size);
185 (void)close(ifd);
186
187 return retval;
188 }
189
190 (void)close(ifd);
191
192 return EXIT_SUCCESS;
193}
194
195static void usage(void)
196{
197 fprintf(stderr, "Usage: %s -l image\n"
198 " -l ==> list image header information\n",
199 params.cmdname);
200 fprintf(stderr,
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200201 " %s -i image -T type [-p position] [-o outfile] data_file\n"
202 " -i ==> extract from the 'image' a specific 'data_file'\n"
203 " -T ==> set image type to 'type'\n"
204 " -p ==> 'position' (starting at 0) of the 'data_file' inside the 'image'\n",
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700205 params.cmdname);
206 fprintf(stderr,
207 " %s -V ==> print version information and exit\n",
208 params.cmdname);
209
210 exit(EXIT_FAILURE);
211}