blob: 4791dd0dfe188bb08cd810bf6edf88a8cf06d33e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -07002/*
3 * Based on mkimage.c.
4 *
5 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -07006 */
7
8#include "dumpimage.h"
9#include <image.h>
10#include <version.h>
11
12static void usage(void);
13
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070014/* parameters initialized by core will be used by the image type code */
Pali Rohárcdf0d1c2022-02-13 01:09:46 +010015static struct image_tool_params params;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070016
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070017/*
Guilherme Maciel Ferreiraf3f4a0a2015-01-15 02:54:41 -020018 * dumpimage_extract_subimage -
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070019 *
20 * It scans all registered image types,
21 * verifies image_header for each supported image type
22 * if verification is successful, it extracts the desired file,
23 * indexed by pflag, from the image
24 *
25 * returns negative if input image format does not match with any of
26 * supported image types
27 */
Guilherme Maciel Ferreiraf3f4a0a2015-01-15 02:54:41 -020028static int dumpimage_extract_subimage(struct image_type_params *tparams,
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020029 void *ptr, struct stat *sbuf)
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070030{
31 int retval = -1;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070032
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020033 if (tparams->verify_header) {
34 retval = tparams->verify_header((unsigned char *)ptr,
35 sbuf->st_size, &params);
Andrew F. Davis3f2c7da2019-09-17 17:09:33 -040036 if (retval != 0) {
37 fprintf(stderr, "%s: failed to verify header of %s\n",
38 params.cmdname, tparams->name);
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020039 return -1;
Andrew F. Davis3f2c7da2019-09-17 17:09:33 -040040 }
41
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020042 /*
43 * Extract the file from the image
44 * if verify is successful
45 */
Guilherme Maciel Ferreiraf3f4a0a2015-01-15 02:54:41 -020046 if (tparams->extract_subimage) {
47 retval = tparams->extract_subimage(ptr, &params);
Andrew F. Davis3f2c7da2019-09-17 17:09:33 -040048 if (retval != 0) {
49 fprintf(stderr, "%s: extract_subimage failed for %s\n",
50 params.cmdname, tparams->name);
51 return -3;
52 }
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020053 } else {
54 fprintf(stderr,
Guilherme Maciel Ferreiraf3f4a0a2015-01-15 02:54:41 -020055 "%s: extract_subimage undefined for %s\n",
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020056 params.cmdname, tparams->name);
57 return -2;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070058 }
59 }
60
61 return retval;
62}
63
64int main(int argc, char **argv)
65{
66 int opt;
67 int ifd = -1;
68 struct stat sbuf;
69 char *ptr;
Martyn Welch387963a2019-01-26 02:31:52 +000070 int retval = EXIT_SUCCESS;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070071 struct image_type_params *tparams = NULL;
72
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070073 params.cmdname = *argv;
74
Martyn Welchcfd6ade2019-01-26 02:31:53 +000075 while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070076 switch (opt) {
77 case 'l':
78 params.lflag = 1;
79 break;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070080 case 'o':
81 params.outfile = optarg;
Martyn Welch67a6a372019-01-26 02:31:51 +000082 params.iflag = 1;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070083 break;
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020084 case 'T':
85 params.type = genimg_get_type_id(optarg);
86 if (params.type < 0) {
Martyn Welch47d95992019-01-26 02:31:50 +000087 fprintf(stderr, "%s: Invalid type\n",
88 params.cmdname);
Martyn Welchcfd6ade2019-01-26 02:31:53 +000089 exit(EXIT_FAILURE);
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -020090 }
91 break;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -070092 case 'p':
93 params.pflag = strtoul(optarg, &ptr, 10);
94 if (*ptr) {
95 fprintf(stderr,
96 "%s: invalid file position %s\n",
97 params.cmdname, *argv);
98 exit(EXIT_FAILURE);
99 }
100 break;
101 case 'V':
102 printf("dumpimage version %s\n", PLAIN_VERSION);
103 exit(EXIT_SUCCESS);
Martyn Welchcfd6ade2019-01-26 02:31:53 +0000104 case 'h':
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700105 default:
106 usage();
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -0200107 break;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700108 }
109 }
110
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100111 if (argc < 2 || (params.iflag && params.lflag))
Martyn Welchcfd6ade2019-01-26 02:31:53 +0000112 usage();
113
Martyn Welch47d95992019-01-26 02:31:50 +0000114 if (optind >= argc) {
115 fprintf(stderr, "%s: image file missing\n", params.cmdname);
Martyn Welchcfd6ade2019-01-26 02:31:53 +0000116 exit(EXIT_FAILURE);
Martyn Welch47d95992019-01-26 02:31:50 +0000117 }
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700118
Martyn Welch67a6a372019-01-26 02:31:51 +0000119 params.imagefile = argv[optind];
120
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700121 /* set tparams as per input type_id */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200122 tparams = imagetool_get_type(params.type);
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100123 if (!params.lflag && tparams == NULL) {
Guilherme Maciel Ferreira40bf5632015-01-15 02:54:40 -0200124 fprintf(stderr, "%s: unsupported type: %s\n",
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700125 params.cmdname, genimg_get_type_name(params.type));
126 exit(EXIT_FAILURE);
127 }
128
129 /*
130 * check the passed arguments parameters meets the requirements
131 * as per image type to be generated/listed
132 */
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100133 if (tparams && tparams->check_params) {
Martyn Welch47d95992019-01-26 02:31:50 +0000134 if (tparams->check_params(&params)) {
135 fprintf(stderr, "%s: Parameter check failed\n",
136 params.cmdname);
Martyn Welchcfd6ade2019-01-26 02:31:53 +0000137 exit(EXIT_FAILURE);
Martyn Welch47d95992019-01-26 02:31:50 +0000138 }
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700139 }
140
Martyn Welch67a6a372019-01-26 02:31:51 +0000141 if (!params.lflag && !params.outfile) {
142 fprintf(stderr, "%s: No output file provided\n",
143 params.cmdname);
144 exit(EXIT_FAILURE);
145 }
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700146
147 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
148 if (ifd < 0) {
Martyn Welch387963a2019-01-26 02:31:52 +0000149 fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname,
150 params.imagefile, strerror(errno));
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700151 exit(EXIT_FAILURE);
152 }
153
Martyn Welch387963a2019-01-26 02:31:52 +0000154 if (fstat(ifd, &sbuf) < 0) {
155 fprintf(stderr, "%s: Can't stat \"%s\": %s\n", params.cmdname,
156 params.imagefile, strerror(errno));
157 exit(EXIT_FAILURE);
158 }
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700159
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100160 if (tparams && (uint32_t)sbuf.st_size < tparams->header_size) {
Martyn Welch387963a2019-01-26 02:31:52 +0000161 fprintf(stderr, "%s: Bad size: \"%s\" is not valid image\n",
162 params.cmdname, params.imagefile);
163 exit(EXIT_FAILURE);
164 }
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700165
Martyn Welch387963a2019-01-26 02:31:52 +0000166 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
167 if (ptr == MAP_FAILED) {
168 fprintf(stderr, "%s: Can't read \"%s\": %s\n", params.cmdname,
169 params.imagefile, strerror(errno));
170 exit(EXIT_FAILURE);
171 }
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700172
Martyn Welch387963a2019-01-26 02:31:52 +0000173 /*
174 * Both calls bellow scan through dumpimage registry for all
175 * supported image types and verify the input image file
176 * header for match
177 */
178 if (params.iflag) {
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700179 /*
Martyn Welch387963a2019-01-26 02:31:52 +0000180 * Extract the data files from within the matched
181 * image type. Returns the error code if not matched
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700182 */
Martyn Welch387963a2019-01-26 02:31:52 +0000183 retval = dumpimage_extract_subimage(tparams, ptr, &sbuf);
Andrew F. Davis3f2c7da2019-09-17 17:09:33 -0400184 if (retval)
185 fprintf(stderr, "%s: Can't extract subimage from %s\n",
186 params.cmdname, params.imagefile);
Martyn Welch387963a2019-01-26 02:31:52 +0000187 } else {
188 /*
189 * Print the image information for matched image type
190 * Returns the error code if not matched
191 */
192 retval = imagetool_verify_print_header(ptr, &sbuf, tparams,
193 &params);
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700194 }
195
Martyn Welch387963a2019-01-26 02:31:52 +0000196 (void)munmap((void *)ptr, sbuf.st_size);
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700197 (void)close(ifd);
198
Martyn Welch387963a2019-01-26 02:31:52 +0000199 return retval;
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700200}
201
202static void usage(void)
203{
Pali Rohárcdf0d1c2022-02-13 01:09:46 +0100204 fprintf(stderr, "Usage: %s [-T type] -l image\n"
205 " -l ==> list image header information\n"
206 " -T ==> parse image file as 'type'\n",
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700207 params.cmdname);
208 fprintf(stderr,
Martyn Welch8c1957a2019-01-26 02:31:54 +0000209 " %s [-T type] [-p position] [-o outfile] image\n"
210 " -T ==> declare image type as 'type'\n"
211 " -p ==> 'position' (starting at 0) of the component to extract from image\n"
212 " -o ==> extract component to file 'outfile'\n",
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700213 params.cmdname);
214 fprintf(stderr,
Martyn Welchcfd6ade2019-01-26 02:31:53 +0000215 " %s -h ==> print usage information and exit\n",
216 params.cmdname);
217 fprintf(stderr,
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700218 " %s -V ==> print version information and exit\n",
219 params.cmdname);
220
Martyn Welchcfd6ade2019-01-26 02:31:53 +0000221 exit(EXIT_SUCCESS);
Guilherme Maciel Ferreira51553812013-12-01 12:43:11 -0700222}