]> Git Repo - J-u-boot.git/blame - tools/dumpimage.c
binman: Clean up unnecessary code related to ELF test files
[J-u-boot.git] / tools / dumpimage.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
a804b5ce
GMF
2/*
3 * Based on mkimage.c.
4 *
5 * Written by Guilherme Maciel Ferreira <[email protected]>
a804b5ce
GMF
6 */
7
8#include "dumpimage.h"
9#include <image.h>
10#include <version.h>
11
12static void usage(void);
13
a804b5ce
GMF
14/* 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
a804b5ce 19/*
67f946cd 20 * dumpimage_extract_subimage -
a804b5ce
GMF
21 *
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 */
67f946cd 30static int dumpimage_extract_subimage(struct image_type_params *tparams,
f41f5b7c 31 void *ptr, struct stat *sbuf)
a804b5ce
GMF
32{
33 int retval = -1;
f41f5b7c
GMF
34
35 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 */
67f946cd
GMF
44 if (tparams->extract_subimage) {
45 retval = tparams->extract_subimage(ptr, &params);
f41f5b7c
GMF
46 } else {
47 fprintf(stderr,
67f946cd 48 "%s: extract_subimage undefined for %s\n",
f41f5b7c
GMF
49 params.cmdname, tparams->name);
50 return -2;
a804b5ce
GMF
51 }
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;
11e3c1fd 63 int retval = EXIT_SUCCESS;
a804b5ce
GMF
64 struct image_type_params *tparams = NULL;
65
a804b5ce
GMF
66 params.cmdname = *argv;
67
65a80b43 68 while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
a804b5ce
GMF
69 switch (opt) {
70 case 'l':
71 params.lflag = 1;
72 break;
a804b5ce
GMF
73 case 'o':
74 params.outfile = optarg;
12b83187 75 params.iflag = 1;
a804b5ce 76 break;
f41f5b7c
GMF
77 case 'T':
78 params.type = genimg_get_type_id(optarg);
79 if (params.type < 0) {
57a608e9
MW
80 fprintf(stderr, "%s: Invalid type\n",
81 params.cmdname);
65a80b43 82 exit(EXIT_FAILURE);
f41f5b7c
GMF
83 }
84 break;
a804b5ce
GMF
85 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);
65a80b43
MW
97 case 'h':
98 usage();
a804b5ce
GMF
99 default:
100 usage();
f41f5b7c 101 break;
a804b5ce
GMF
102 }
103 }
104
65a80b43
MW
105 if (argc < 2)
106 usage();
107
57a608e9
MW
108 if (optind >= argc) {
109 fprintf(stderr, "%s: image file missing\n", params.cmdname);
65a80b43 110 exit(EXIT_FAILURE);
57a608e9 111 }
a804b5ce 112
12b83187
MW
113 params.imagefile = argv[optind];
114
a804b5ce 115 /* set tparams as per input type_id */
a93648d1 116 tparams = imagetool_get_type(params.type);
a804b5ce 117 if (tparams == NULL) {
f41f5b7c 118 fprintf(stderr, "%s: unsupported type: %s\n",
a804b5ce
GMF
119 params.cmdname, genimg_get_type_name(params.type));
120 exit(EXIT_FAILURE);
121 }
122
123 /*
124 * check the passed arguments parameters meets the requirements
125 * as per image type to be generated/listed
126 */
127 if (tparams->check_params) {
57a608e9
MW
128 if (tparams->check_params(&params)) {
129 fprintf(stderr, "%s: Parameter check failed\n",
130 params.cmdname);
65a80b43 131 exit(EXIT_FAILURE);
57a608e9 132 }
a804b5ce
GMF
133 }
134
12b83187
MW
135 if (!params.lflag && !params.outfile) {
136 fprintf(stderr, "%s: No output file provided\n",
137 params.cmdname);
138 exit(EXIT_FAILURE);
139 }
a804b5ce
GMF
140
141 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
142 if (ifd < 0) {
11e3c1fd
MW
143 fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname,
144 params.imagefile, strerror(errno));
a804b5ce
GMF
145 exit(EXIT_FAILURE);
146 }
147
11e3c1fd
MW
148 if (fstat(ifd, &sbuf) < 0) {
149 fprintf(stderr, "%s: Can't stat \"%s\": %s\n", params.cmdname,
150 params.imagefile, strerror(errno));
151 exit(EXIT_FAILURE);
152 }
a804b5ce 153
11e3c1fd
MW
154 if ((uint32_t)sbuf.st_size < tparams->header_size) {
155 fprintf(stderr, "%s: Bad size: \"%s\" is not valid image\n",
156 params.cmdname, params.imagefile);
157 exit(EXIT_FAILURE);
158 }
a804b5ce 159
11e3c1fd
MW
160 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
161 if (ptr == MAP_FAILED) {
162 fprintf(stderr, "%s: Can't read \"%s\": %s\n", params.cmdname,
163 params.imagefile, strerror(errno));
164 exit(EXIT_FAILURE);
165 }
a804b5ce 166
11e3c1fd
MW
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) {
a804b5ce 173 /*
11e3c1fd
MW
174 * Extract the data files from within the matched
175 * image type. Returns the error code if not matched
a804b5ce 176 */
11e3c1fd
MW
177 retval = dumpimage_extract_subimage(tparams, ptr, &sbuf);
178 } else {
179 /*
180 * Print the image information for matched image type
181 * Returns the error code if not matched
182 */
183 retval = imagetool_verify_print_header(ptr, &sbuf, tparams,
184 &params);
a804b5ce
GMF
185 }
186
11e3c1fd 187 (void)munmap((void *)ptr, sbuf.st_size);
a804b5ce
GMF
188 (void)close(ifd);
189
11e3c1fd 190 return retval;
a804b5ce
GMF
191}
192
193static void usage(void)
194{
195 fprintf(stderr, "Usage: %s -l image\n"
196 " -l ==> list image header information\n",
197 params.cmdname);
198 fprintf(stderr,
e3b4fc95
MW
199 " %s [-T type] [-p position] [-o outfile] image\n"
200 " -T ==> declare image type as 'type'\n"
201 " -p ==> 'position' (starting at 0) of the component to extract from image\n"
202 " -o ==> extract component to file 'outfile'\n",
a804b5ce 203 params.cmdname);
65a80b43
MW
204 fprintf(stderr,
205 " %s -h ==> print usage information and exit\n",
206 params.cmdname);
a804b5ce
GMF
207 fprintf(stderr,
208 " %s -V ==> print version information and exit\n",
209 params.cmdname);
210
65a80b43 211 exit(EXIT_SUCCESS);
a804b5ce 212}
This page took 0.345378 seconds and 4 git commands to generate.