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