4 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
6 * SPDX-License-Identifier: GPL-2.0+
13 static void usage(void);
15 /* image_type_params linked list to maintain registered image types supports */
16 static struct image_type_params *dumpimage_tparams;
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,
24 * dumpimage_register() - register respective image generation/list support
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
29 * @tparams: Image type parameters
31 static void dumpimage_register(struct image_type_params *tparams)
33 struct image_type_params **tp;
36 fprintf(stderr, "%s: %s: Null input\n", params.cmdname,
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);
50 /* add input struct entry at the end of link list */
52 /* mark input entry as last entry in the link list */
55 debug("Registered %s\n", tparams->name);
59 * dumpimage_extract_datafile -
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
66 * returns negative if input image format does not match with any of
67 * supported image types
69 static int dumpimage_extract_datafile(void *ptr, struct stat *sbuf)
72 struct image_type_params *curr;
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);
81 * Extract the file from the image
82 * if verify is successful
84 if (curr->extract_datafile) {
85 curr->extract_datafile(ptr, ¶ms);
88 "%s: extract_datafile undefined for %s\n",
89 params.cmdname, curr->name);
98 int main(int argc, char **argv)
105 struct image_type_params *tparams = NULL;
107 /* Init all image generation/list support */
108 register_image_tool(dumpimage_register);
110 params.cmdname = *argv;
112 while ((opt = getopt(argc, argv, "li:o:p:V")) != -1) {
118 params.imagefile = optarg;
122 params.outfile = optarg;
125 params.pflag = strtoul(optarg, &ptr, 10);
128 "%s: invalid file position %s\n",
129 params.cmdname, *argv);
134 printf("dumpimage version %s\n", PLAIN_VERSION);
144 /* set tparams as per input type_id */
145 tparams = imagetool_get_type(params.type, dumpimage_tparams);
146 if (tparams == NULL) {
147 fprintf(stderr, "%s: unsupported type %s\n",
148 params.cmdname, genimg_get_type_name(params.type));
153 * check the passed arguments parameters meets the requirements
154 * as per image type to be generated/listed
156 if (tparams->check_params) {
157 if (tparams->check_params(¶ms))
162 params.datafile = argv[optind];
164 params.imagefile = argv[optind];
166 params.outfile = params.datafile;
168 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
170 fprintf(stderr, "%s: Can't open \"%s\": %s\n",
171 params.cmdname, params.imagefile,
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,
184 if ((unsigned)sbuf.st_size < tparams->header_size) {
186 "%s: Bad size: \"%s\" is not valid image\n",
187 params.cmdname, params.imagefile);
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,
200 * Both calls bellow scan through dumpimage registry for all
201 * supported image types and verify the input image file
206 * Extract the data files from within the matched
207 * image type. Returns the error code if not matched
209 retval = dumpimage_extract_datafile(ptr, &sbuf);
212 * Print the image information for matched image type
213 * Returns the error code if not matched
215 retval = imagetool_verify_print_header(ptr, &sbuf,
219 (void)munmap((void *)ptr, sbuf.st_size);
230 static void usage(void)
232 fprintf(stderr, "Usage: %s -l image\n"
233 " -l ==> list image header information\n",
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",
241 " %s -V ==> print version information and exit\n",