blob: d2a71cd759b2a1938aeac7c447f607e68ed56f9a [file] [log] [blame]
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -07001/*
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>
Sachin Sundar1ed5f5b2017-03-28 17:34:42 +053012#include "sysupgrade.h"
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070013
14static void usage(void);
15
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070016/* parameters initialized by core will be used by the image type code */
17static struct image_tool_params params = {
18 .type = IH_TYPE_KERNEL,
19};
20
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070021/*
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020022 * dumpimage_extract_subimage -
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070023 *
24 * It scans all registered image types,
25 * verifies image_header for each supported image type
26 * if verification is successful, it extracts the desired file,
27 * indexed by pflag, from the image
28 *
29 * returns negative if input image format does not match with any of
30 * supported image types
31 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020032static int dumpimage_extract_subimage(struct image_type_params *tparams,
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020033 void *ptr, struct stat *sbuf)
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070034{
35 int retval = -1;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070036
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020037 if (tparams->verify_header) {
38 retval = tparams->verify_header((unsigned char *)ptr,
39 sbuf->st_size, &params);
40 if (retval != 0)
41 return -1;
42 /*
43 * Extract the file from the image
44 * if verify is successful
45 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020046 if (tparams->extract_subimage) {
47 retval = tparams->extract_subimage(ptr, &params);
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020048 } else {
49 fprintf(stderr,
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020050 "%s: extract_subimage undefined for %s\n",
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020051 params.cmdname, tparams->name);
52 return -2;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070053 }
54 }
55
56 return retval;
57}
58
59int main(int argc, char **argv)
60{
61 int opt;
62 int ifd = -1;
63 struct stat sbuf;
64 char *ptr;
65 int retval = 0;
66 struct image_type_params *tparams = NULL;
67
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070068 params.cmdname = *argv;
69
Sachin Sundar1ed5f5b2017-03-28 17:34:42 +053070 while ((opt = getopt(argc, argv, "c:li:o:T:p:V")) != -1) {
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070071 switch (opt) {
72 case 'l':
73 params.lflag = 1;
74 break;
75 case 'i':
76 params.imagefile = optarg;
77 params.iflag = 1;
78 break;
79 case 'o':
80 params.outfile = optarg;
81 break;
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -020082 case 'T':
83 params.type = genimg_get_type_id(optarg);
84 if (params.type < 0) {
85 usage();
86 }
87 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -070088 case 'p':
89 params.pflag = strtoul(optarg, &ptr, 10);
90 if (*ptr) {
91 fprintf(stderr,
92 "%s: invalid file position %s\n",
93 params.cmdname, *argv);
94 exit(EXIT_FAILURE);
95 }
96 break;
97 case 'V':
98 printf("dumpimage version %s\n", PLAIN_VERSION);
99 exit(EXIT_SUCCESS);
Sachin Sundar1ed5f5b2017-03-28 17:34:42 +0530100 case 'c':
101 return do_board_upgrade_check(optarg);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700102 default:
103 usage();
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200104 break;
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700105 }
106 }
107
108 if (optind >= argc)
109 usage();
110
111 /* set tparams as per input type_id */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200112 tparams = imagetool_get_type(params.type);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700113 if (tparams == NULL) {
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200114 fprintf(stderr, "%s: unsupported type: %s\n",
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700115 params.cmdname, genimg_get_type_name(params.type));
116 exit(EXIT_FAILURE);
117 }
118
119 /*
120 * check the passed arguments parameters meets the requirements
121 * as per image type to be generated/listed
122 */
123 if (tparams->check_params) {
124 if (tparams->check_params(&params))
125 usage();
126 }
127
128 if (params.iflag)
129 params.datafile = argv[optind];
130 else
131 params.imagefile = argv[optind];
132 if (!params.outfile)
133 params.outfile = params.datafile;
134
135 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
136 if (ifd < 0) {
137 fprintf(stderr, "%s: Can't open \"%s\": %s\n",
138 params.cmdname, params.imagefile,
139 strerror(errno));
140 exit(EXIT_FAILURE);
141 }
142
143 if (params.lflag || params.iflag) {
144 if (fstat(ifd, &sbuf) < 0) {
145 fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
146 params.cmdname, params.imagefile,
147 strerror(errno));
148 exit(EXIT_FAILURE);
149 }
150
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200151 if ((uint32_t)sbuf.st_size < tparams->header_size) {
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700152 fprintf(stderr,
153 "%s: Bad size: \"%s\" is not valid image\n",
154 params.cmdname, params.imagefile);
155 exit(EXIT_FAILURE);
156 }
157
158 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
159 if (ptr == MAP_FAILED) {
160 fprintf(stderr, "%s: Can't read \"%s\": %s\n",
161 params.cmdname, params.imagefile,
162 strerror(errno));
163 exit(EXIT_FAILURE);
164 }
165
166 /*
167 * Both calls bellow scan through dumpimage registry for all
168 * supported image types and verify the input image file
169 * header for match
170 */
171 if (params.iflag) {
172 /*
173 * Extract the data files from within the matched
174 * image type. Returns the error code if not matched
175 */
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -0200176 retval = dumpimage_extract_subimage(tparams, ptr,
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200177 &sbuf);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700178 } else {
179 /*
180 * Print the image information for matched image type
181 * Returns the error code if not matched
182 */
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -0200183 retval = imagetool_verify_print_header(ptr, &sbuf,
184 tparams, &params);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700185 }
186
187 (void)munmap((void *)ptr, sbuf.st_size);
188 (void)close(ifd);
189
190 return retval;
191 }
192
193 (void)close(ifd);
194
195 return EXIT_SUCCESS;
196}
197
198static void usage(void)
199{
200 fprintf(stderr, "Usage: %s -l image\n"
201 " -l ==> list image header information\n",
202 params.cmdname);
203 fprintf(stderr,
Guilherme Maciel Ferreiraf41f5b72015-01-15 02:54:40 -0200204 " %s -i image -T type [-p position] [-o outfile] data_file\n"
205 " -i ==> extract from the 'image' a specific 'data_file'\n"
206 " -T ==> set image type to 'type'\n"
207 " -p ==> 'position' (starting at 0) of the 'data_file' inside the 'image'\n",
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700208 params.cmdname);
209 fprintf(stderr,
210 " %s -V ==> print version information and exit\n",
211 params.cmdname);
Sachin Sundar1ed5f5b2017-03-28 17:34:42 +0530212 fprintf(stderr,
213 " %s -c image\n"
214 " -c ==> do board upgrade check\n",
215 params.cmdname);
Guilherme Maciel Ferreiraa804b5c2013-12-01 12:43:11 -0700216
217 exit(EXIT_FAILURE);
218}