]>
Commit | Line | Data |
---|---|---|
a804b5ce GMF |
1 | /* |
2 | * Based on mkimage.c. | |
3 | * | |
4 | * Written by Guilherme Maciel Ferreira <[email protected]> | |
5 | * | |
6 | * SPDX-License-Identifier: GPL-2.0+ | |
7 | */ | |
8 | ||
9 | #include "dumpimage.h" | |
10 | #include <image.h> | |
11 | #include <version.h> | |
12 | ||
13 | static void usage(void); | |
14 | ||
15 | /* image_type_params linked list to maintain registered image types supports */ | |
16 | static struct image_type_params *dumpimage_tparams; | |
17 | ||
18 | /* parameters initialized by core will be used by the image type code */ | |
19 | static struct image_tool_params params = { | |
20 | .type = IH_TYPE_KERNEL, | |
21 | }; | |
22 | ||
23 | /** | |
24 | * dumpimage_register() - register respective image generation/list support | |
25 | * | |
26 | * the input struct image_type_params is checked and appended to the link | |
27 | * list, if the input structure is already registered, issue an error | |
28 | * | |
29 | * @tparams: Image type parameters | |
30 | */ | |
31 | static void dumpimage_register(struct image_type_params *tparams) | |
32 | { | |
33 | struct image_type_params **tp; | |
34 | ||
35 | if (!tparams) { | |
36 | fprintf(stderr, "%s: %s: Null input\n", params.cmdname, | |
37 | __func__); | |
38 | exit(EXIT_FAILURE); | |
39 | } | |
40 | ||
41 | /* scan the linked list, check for registry and point the last one */ | |
42 | for (tp = &dumpimage_tparams; *tp != NULL; tp = &(*tp)->next) { | |
43 | if (!strcmp((*tp)->name, tparams->name)) { | |
44 | fprintf(stderr, "%s: %s already registered\n", | |
45 | params.cmdname, tparams->name); | |
46 | return; | |
47 | } | |
48 | } | |
49 | ||
50 | /* add input struct entry at the end of link list */ | |
51 | *tp = tparams; | |
52 | /* mark input entry as last entry in the link list */ | |
53 | tparams->next = NULL; | |
54 | ||
55 | debug("Registered %s\n", tparams->name); | |
56 | } | |
57 | ||
a804b5ce GMF |
58 | /* |
59 | * dumpimage_extract_datafile - | |
60 | * | |
61 | * It scans all registered image types, | |
62 | * verifies image_header for each supported image type | |
63 | * if verification is successful, it extracts the desired file, | |
64 | * indexed by pflag, from the image | |
65 | * | |
66 | * returns negative if input image format does not match with any of | |
67 | * supported image types | |
68 | */ | |
69 | static int dumpimage_extract_datafile(void *ptr, struct stat *sbuf) | |
70 | { | |
71 | int retval = -1; | |
72 | struct image_type_params *curr; | |
73 | ||
74 | for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) { | |
75 | if (curr->verify_header) { | |
76 | retval = curr->verify_header((unsigned char *)ptr, | |
77 | sbuf->st_size, ¶ms); | |
78 | if (retval != 0) | |
79 | continue; | |
80 | /* | |
81 | * Extract the file from the image | |
82 | * if verify is successful | |
83 | */ | |
84 | if (curr->extract_datafile) { | |
85 | curr->extract_datafile(ptr, ¶ms); | |
86 | } else { | |
87 | fprintf(stderr, | |
88 | "%s: extract_datafile undefined for %s\n", | |
89 | params.cmdname, curr->name); | |
90 | break; | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | return retval; | |
96 | } | |
97 | ||
98 | int main(int argc, char **argv) | |
99 | { | |
100 | int opt; | |
101 | int ifd = -1; | |
102 | struct stat sbuf; | |
103 | char *ptr; | |
104 | int retval = 0; | |
105 | struct image_type_params *tparams = NULL; | |
106 | ||
107 | /* Init all image generation/list support */ | |
108 | register_image_tool(dumpimage_register); | |
109 | ||
110 | params.cmdname = *argv; | |
111 | ||
112 | while ((opt = getopt(argc, argv, "li:o:p:V")) != -1) { | |
113 | switch (opt) { | |
114 | case 'l': | |
115 | params.lflag = 1; | |
116 | break; | |
117 | case 'i': | |
118 | params.imagefile = optarg; | |
119 | params.iflag = 1; | |
120 | break; | |
121 | case 'o': | |
122 | params.outfile = optarg; | |
123 | break; | |
124 | case 'p': | |
125 | params.pflag = strtoul(optarg, &ptr, 10); | |
126 | if (*ptr) { | |
127 | fprintf(stderr, | |
128 | "%s: invalid file position %s\n", | |
129 | params.cmdname, *argv); | |
130 | exit(EXIT_FAILURE); | |
131 | } | |
132 | break; | |
133 | case 'V': | |
134 | printf("dumpimage version %s\n", PLAIN_VERSION); | |
135 | exit(EXIT_SUCCESS); | |
136 | default: | |
137 | usage(); | |
138 | } | |
139 | } | |
140 | ||
141 | if (optind >= argc) | |
142 | usage(); | |
143 | ||
144 | /* set tparams as per input type_id */ | |
0ca6691c | 145 | tparams = imagetool_get_type(params.type, dumpimage_tparams); |
a804b5ce GMF |
146 | if (tparams == NULL) { |
147 | fprintf(stderr, "%s: unsupported type %s\n", | |
148 | params.cmdname, genimg_get_type_name(params.type)); | |
149 | exit(EXIT_FAILURE); | |
150 | } | |
151 | ||
152 | /* | |
153 | * check the passed arguments parameters meets the requirements | |
154 | * as per image type to be generated/listed | |
155 | */ | |
156 | if (tparams->check_params) { | |
157 | if (tparams->check_params(¶ms)) | |
158 | usage(); | |
159 | } | |
160 | ||
161 | if (params.iflag) | |
162 | params.datafile = argv[optind]; | |
163 | else | |
164 | params.imagefile = argv[optind]; | |
165 | if (!params.outfile) | |
166 | params.outfile = params.datafile; | |
167 | ||
168 | ifd = open(params.imagefile, O_RDONLY|O_BINARY); | |
169 | if (ifd < 0) { | |
170 | fprintf(stderr, "%s: Can't open \"%s\": %s\n", | |
171 | params.cmdname, params.imagefile, | |
172 | strerror(errno)); | |
173 | exit(EXIT_FAILURE); | |
174 | } | |
175 | ||
176 | if (params.lflag || params.iflag) { | |
177 | if (fstat(ifd, &sbuf) < 0) { | |
178 | fprintf(stderr, "%s: Can't stat \"%s\": %s\n", | |
179 | params.cmdname, params.imagefile, | |
180 | strerror(errno)); | |
181 | exit(EXIT_FAILURE); | |
182 | } | |
183 | ||
184 | if ((unsigned)sbuf.st_size < tparams->header_size) { | |
185 | fprintf(stderr, | |
186 | "%s: Bad size: \"%s\" is not valid image\n", | |
187 | params.cmdname, params.imagefile); | |
188 | exit(EXIT_FAILURE); | |
189 | } | |
190 | ||
191 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); | |
192 | if (ptr == MAP_FAILED) { | |
193 | fprintf(stderr, "%s: Can't read \"%s\": %s\n", | |
194 | params.cmdname, params.imagefile, | |
195 | strerror(errno)); | |
196 | exit(EXIT_FAILURE); | |
197 | } | |
198 | ||
199 | /* | |
200 | * Both calls bellow scan through dumpimage registry for all | |
201 | * supported image types and verify the input image file | |
202 | * header for match | |
203 | */ | |
204 | if (params.iflag) { | |
205 | /* | |
206 | * Extract the data files from within the matched | |
207 | * image type. Returns the error code if not matched | |
208 | */ | |
209 | retval = dumpimage_extract_datafile(ptr, &sbuf); | |
210 | } else { | |
211 | /* | |
212 | * Print the image information for matched image type | |
213 | * Returns the error code if not matched | |
214 | */ | |
0ca6691c GMF |
215 | retval = imagetool_verify_print_header(ptr, &sbuf, |
216 | tparams, ¶ms); | |
a804b5ce GMF |
217 | } |
218 | ||
219 | (void)munmap((void *)ptr, sbuf.st_size); | |
220 | (void)close(ifd); | |
221 | ||
222 | return retval; | |
223 | } | |
224 | ||
225 | (void)close(ifd); | |
226 | ||
227 | return EXIT_SUCCESS; | |
228 | } | |
229 | ||
230 | static void usage(void) | |
231 | { | |
232 | fprintf(stderr, "Usage: %s -l image\n" | |
233 | " -l ==> list image header information\n", | |
234 | params.cmdname); | |
235 | fprintf(stderr, | |
236 | " %s -i image [-p position] [-o outfile] data_file\n" | |
237 | " -i ==> extract from the 'image' a specific 'data_file'" | |
238 | ", indexed by 'position' (starting at 0)\n", | |
239 | params.cmdname); | |
240 | fprintf(stderr, | |
241 | " %s -V ==> print version information and exit\n", | |
242 | params.cmdname); | |
243 | ||
244 | exit(EXIT_FAILURE); | |
245 | } |