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