]> Git Repo - J-u-boot.git/blame - tools/dumpimage.c
expo: Use standard numbering for save and discard
[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 14/* parameters initialized by core will be used by the image type code */
11f29d44 15static struct image_tool_params params;
a804b5ce 16
a804b5ce 17/*
67f946cd 18 * dumpimage_extract_subimage -
a804b5ce
GMF
19 *
20 * It scans all registered image types,
21 * verifies image_header for each supported image type
22 * if verification is successful, it extracts the desired file,
23 * indexed by pflag, from the image
24 *
25 * returns negative if input image format does not match with any of
26 * supported image types
27 */
67f946cd 28static int dumpimage_extract_subimage(struct image_type_params *tparams,
f41f5b7c 29 void *ptr, struct stat *sbuf)
a804b5ce
GMF
30{
31 int retval = -1;
f41f5b7c
GMF
32
33 if (tparams->verify_header) {
34 retval = tparams->verify_header((unsigned char *)ptr,
35 sbuf->st_size, &params);
79e984c4
AD
36 if (retval != 0) {
37 fprintf(stderr, "%s: failed to verify header of %s\n",
38 params.cmdname, tparams->name);
f41f5b7c 39 return -1;
79e984c4
AD
40 }
41
f41f5b7c
GMF
42 /*
43 * Extract the file from the image
44 * if verify is successful
45 */
67f946cd
GMF
46 if (tparams->extract_subimage) {
47 retval = tparams->extract_subimage(ptr, &params);
79e984c4
AD
48 if (retval != 0) {
49 fprintf(stderr, "%s: extract_subimage failed for %s\n",
50 params.cmdname, tparams->name);
51 return -3;
52 }
f41f5b7c
GMF
53 } else {
54 fprintf(stderr,
67f946cd 55 "%s: extract_subimage undefined for %s\n",
f41f5b7c
GMF
56 params.cmdname, tparams->name);
57 return -2;
a804b5ce
GMF
58 }
59 }
60
61 return retval;
62}
63
64int main(int argc, char **argv)
65{
66 int opt;
67 int ifd = -1;
68 struct stat sbuf;
69 char *ptr;
11e3c1fd 70 int retval = EXIT_SUCCESS;
a804b5ce
GMF
71 struct image_type_params *tparams = NULL;
72
a804b5ce
GMF
73 params.cmdname = *argv;
74
65a80b43 75 while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
a804b5ce
GMF
76 switch (opt) {
77 case 'l':
78 params.lflag = 1;
79 break;
a804b5ce
GMF
80 case 'o':
81 params.outfile = optarg;
12b83187 82 params.iflag = 1;
a804b5ce 83 break;
f41f5b7c
GMF
84 case 'T':
85 params.type = genimg_get_type_id(optarg);
86 if (params.type < 0) {
57a608e9
MW
87 fprintf(stderr, "%s: Invalid type\n",
88 params.cmdname);
65a80b43 89 exit(EXIT_FAILURE);
f41f5b7c
GMF
90 }
91 break;
a804b5ce
GMF
92 case 'p':
93 params.pflag = strtoul(optarg, &ptr, 10);
94 if (*ptr) {
95 fprintf(stderr,
96 "%s: invalid file position %s\n",
97 params.cmdname, *argv);
98 exit(EXIT_FAILURE);
99 }
100 break;
101 case 'V':
102 printf("dumpimage version %s\n", PLAIN_VERSION);
103 exit(EXIT_SUCCESS);
65a80b43 104 case 'h':
a804b5ce
GMF
105 default:
106 usage();
f41f5b7c 107 break;
a804b5ce
GMF
108 }
109 }
110
11f29d44 111 if (argc < 2 || (params.iflag && params.lflag))
65a80b43
MW
112 usage();
113
57a608e9
MW
114 if (optind >= argc) {
115 fprintf(stderr, "%s: image file missing\n", params.cmdname);
65a80b43 116 exit(EXIT_FAILURE);
57a608e9 117 }
a804b5ce 118
12b83187
MW
119 params.imagefile = argv[optind];
120
a804b5ce 121 /* set tparams as per input type_id */
a93648d1 122 tparams = imagetool_get_type(params.type);
11f29d44 123 if (!params.lflag && tparams == NULL) {
f41f5b7c 124 fprintf(stderr, "%s: unsupported type: %s\n",
a804b5ce
GMF
125 params.cmdname, genimg_get_type_name(params.type));
126 exit(EXIT_FAILURE);
127 }
128
129 /*
130 * check the passed arguments parameters meets the requirements
131 * as per image type to be generated/listed
132 */
11f29d44 133 if (tparams && tparams->check_params) {
57a608e9
MW
134 if (tparams->check_params(&params)) {
135 fprintf(stderr, "%s: Parameter check failed\n",
136 params.cmdname);
65a80b43 137 exit(EXIT_FAILURE);
57a608e9 138 }
a804b5ce
GMF
139 }
140
12b83187
MW
141 if (!params.lflag && !params.outfile) {
142 fprintf(stderr, "%s: No output file provided\n",
143 params.cmdname);
144 exit(EXIT_FAILURE);
145 }
a804b5ce
GMF
146
147 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
148 if (ifd < 0) {
11e3c1fd
MW
149 fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname,
150 params.imagefile, strerror(errno));
a804b5ce
GMF
151 exit(EXIT_FAILURE);
152 }
153
11e3c1fd
MW
154 if (fstat(ifd, &sbuf) < 0) {
155 fprintf(stderr, "%s: Can't stat \"%s\": %s\n", params.cmdname,
156 params.imagefile, strerror(errno));
157 exit(EXIT_FAILURE);
158 }
a804b5ce 159
11f29d44 160 if (tparams && (uint32_t)sbuf.st_size < tparams->header_size) {
11e3c1fd
MW
161 fprintf(stderr, "%s: Bad size: \"%s\" is not valid image\n",
162 params.cmdname, params.imagefile);
163 exit(EXIT_FAILURE);
164 }
a804b5ce 165
11e3c1fd
MW
166 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
167 if (ptr == MAP_FAILED) {
168 fprintf(stderr, "%s: Can't read \"%s\": %s\n", params.cmdname,
169 params.imagefile, strerror(errno));
170 exit(EXIT_FAILURE);
171 }
a804b5ce 172
11e3c1fd
MW
173 /*
174 * Both calls bellow scan through dumpimage registry for all
175 * supported image types and verify the input image file
176 * header for match
177 */
178 if (params.iflag) {
a804b5ce 179 /*
11e3c1fd
MW
180 * Extract the data files from within the matched
181 * image type. Returns the error code if not matched
a804b5ce 182 */
11e3c1fd 183 retval = dumpimage_extract_subimage(tparams, ptr, &sbuf);
79e984c4
AD
184 if (retval)
185 fprintf(stderr, "%s: Can't extract subimage from %s\n",
186 params.cmdname, params.imagefile);
11e3c1fd
MW
187 } else {
188 /*
189 * Print the image information for matched image type
190 * Returns the error code if not matched
191 */
192 retval = imagetool_verify_print_header(ptr, &sbuf, tparams,
193 &params);
a804b5ce
GMF
194 }
195
11e3c1fd 196 (void)munmap((void *)ptr, sbuf.st_size);
a804b5ce
GMF
197 (void)close(ifd);
198
11e3c1fd 199 return retval;
a804b5ce
GMF
200}
201
202static void usage(void)
203{
11f29d44
T
204 fprintf(stderr, "Usage: %s [-T type] -l image\n"
205 " -l ==> list image header information\n"
206 " -T ==> parse image file as 'type'\n",
a804b5ce
GMF
207 params.cmdname);
208 fprintf(stderr,
e3b4fc95
MW
209 " %s [-T type] [-p position] [-o outfile] image\n"
210 " -T ==> declare image type as 'type'\n"
211 " -p ==> 'position' (starting at 0) of the component to extract from image\n"
212 " -o ==> extract component to file 'outfile'\n",
a804b5ce 213 params.cmdname);
65a80b43
MW
214 fprintf(stderr,
215 " %s -h ==> print usage information and exit\n",
216 params.cmdname);
a804b5ce
GMF
217 fprintf(stderr,
218 " %s -V ==> print version information and exit\n",
219 params.cmdname);
220
65a80b43 221 exit(EXIT_SUCCESS);
a804b5ce 222}
This page took 0.383228 seconds and 4 git commands to generate.