Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Based on mkimage.c. |
| 4 | * |
| 5 | * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "dumpimage.h" |
| 9 | #include <image.h> |
| 10 | #include <version.h> |
| 11 | |
| 12 | static void usage(void); |
| 13 | |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 14 | /* parameters initialized by core will be used by the image type code */ |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 15 | static struct image_tool_params params; |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 16 | |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 17 | /* |
Guilherme Maciel Ferreira | f3f4a0a | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 18 | * dumpimage_extract_subimage - |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 19 | * |
| 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 Ferreira | f3f4a0a | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 28 | static int dumpimage_extract_subimage(struct image_type_params *tparams, |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 29 | void *ptr, struct stat *sbuf) |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 30 | { |
| 31 | int retval = -1; |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 32 | |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 33 | if (tparams->verify_header) { |
| 34 | retval = tparams->verify_header((unsigned char *)ptr, |
| 35 | sbuf->st_size, ¶ms); |
Andrew F. Davis | 3f2c7da | 2019-09-17 17:09:33 -0400 | [diff] [blame] | 36 | if (retval != 0) { |
| 37 | fprintf(stderr, "%s: failed to verify header of %s\n", |
| 38 | params.cmdname, tparams->name); |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 39 | return -1; |
Andrew F. Davis | 3f2c7da | 2019-09-17 17:09:33 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 42 | /* |
| 43 | * Extract the file from the image |
| 44 | * if verify is successful |
| 45 | */ |
Guilherme Maciel Ferreira | f3f4a0a | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 46 | if (tparams->extract_subimage) { |
| 47 | retval = tparams->extract_subimage(ptr, ¶ms); |
Andrew F. Davis | 3f2c7da | 2019-09-17 17:09:33 -0400 | [diff] [blame] | 48 | if (retval != 0) { |
| 49 | fprintf(stderr, "%s: extract_subimage failed for %s\n", |
| 50 | params.cmdname, tparams->name); |
| 51 | return -3; |
| 52 | } |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 53 | } else { |
| 54 | fprintf(stderr, |
Guilherme Maciel Ferreira | f3f4a0a | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 55 | "%s: extract_subimage undefined for %s\n", |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 56 | params.cmdname, tparams->name); |
| 57 | return -2; |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | return retval; |
| 62 | } |
| 63 | |
| 64 | int main(int argc, char **argv) |
| 65 | { |
| 66 | int opt; |
| 67 | int ifd = -1; |
| 68 | struct stat sbuf; |
| 69 | char *ptr; |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 70 | int retval = EXIT_SUCCESS; |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 71 | struct image_type_params *tparams = NULL; |
| 72 | |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 73 | params.cmdname = *argv; |
| 74 | |
Martyn Welch | cfd6ade | 2019-01-26 02:31:53 +0000 | [diff] [blame] | 75 | while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) { |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 76 | switch (opt) { |
| 77 | case 'l': |
| 78 | params.lflag = 1; |
| 79 | break; |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 80 | case 'o': |
| 81 | params.outfile = optarg; |
Martyn Welch | 67a6a37 | 2019-01-26 02:31:51 +0000 | [diff] [blame] | 82 | params.iflag = 1; |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 83 | break; |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 84 | case 'T': |
| 85 | params.type = genimg_get_type_id(optarg); |
| 86 | if (params.type < 0) { |
Martyn Welch | 47d9599 | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 87 | fprintf(stderr, "%s: Invalid type\n", |
| 88 | params.cmdname); |
Martyn Welch | cfd6ade | 2019-01-26 02:31:53 +0000 | [diff] [blame] | 89 | exit(EXIT_FAILURE); |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 90 | } |
| 91 | break; |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 92 | 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 Welch | cfd6ade | 2019-01-26 02:31:53 +0000 | [diff] [blame] | 104 | case 'h': |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 105 | default: |
| 106 | usage(); |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 107 | break; |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 111 | if (argc < 2 || (params.iflag && params.lflag)) |
Martyn Welch | cfd6ade | 2019-01-26 02:31:53 +0000 | [diff] [blame] | 112 | usage(); |
| 113 | |
Martyn Welch | 47d9599 | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 114 | if (optind >= argc) { |
| 115 | fprintf(stderr, "%s: image file missing\n", params.cmdname); |
Martyn Welch | cfd6ade | 2019-01-26 02:31:53 +0000 | [diff] [blame] | 116 | exit(EXIT_FAILURE); |
Martyn Welch | 47d9599 | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 117 | } |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 118 | |
Martyn Welch | 67a6a37 | 2019-01-26 02:31:51 +0000 | [diff] [blame] | 119 | params.imagefile = argv[optind]; |
| 120 | |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 121 | /* set tparams as per input type_id */ |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 122 | tparams = imagetool_get_type(params.type); |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 123 | if (!params.lflag && tparams == NULL) { |
Guilherme Maciel Ferreira | 40bf563 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 124 | fprintf(stderr, "%s: unsupported type: %s\n", |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 125 | 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ár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 133 | if (tparams && tparams->check_params) { |
Martyn Welch | 47d9599 | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 134 | if (tparams->check_params(¶ms)) { |
| 135 | fprintf(stderr, "%s: Parameter check failed\n", |
| 136 | params.cmdname); |
Martyn Welch | cfd6ade | 2019-01-26 02:31:53 +0000 | [diff] [blame] | 137 | exit(EXIT_FAILURE); |
Martyn Welch | 47d9599 | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 138 | } |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Martyn Welch | 67a6a37 | 2019-01-26 02:31:51 +0000 | [diff] [blame] | 141 | if (!params.lflag && !params.outfile) { |
| 142 | fprintf(stderr, "%s: No output file provided\n", |
| 143 | params.cmdname); |
| 144 | exit(EXIT_FAILURE); |
| 145 | } |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 146 | |
| 147 | ifd = open(params.imagefile, O_RDONLY|O_BINARY); |
| 148 | if (ifd < 0) { |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 149 | fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname, |
| 150 | params.imagefile, strerror(errno)); |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 151 | exit(EXIT_FAILURE); |
| 152 | } |
| 153 | |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 154 | 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 Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 159 | |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 160 | if (tparams && (uint32_t)sbuf.st_size < tparams->header_size) { |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 161 | fprintf(stderr, "%s: Bad size: \"%s\" is not valid image\n", |
| 162 | params.cmdname, params.imagefile); |
| 163 | exit(EXIT_FAILURE); |
| 164 | } |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 165 | |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 166 | 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 Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 172 | |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 173 | /* |
| 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 Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 179 | /* |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 180 | * Extract the data files from within the matched |
| 181 | * image type. Returns the error code if not matched |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 182 | */ |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 183 | retval = dumpimage_extract_subimage(tparams, ptr, &sbuf); |
Andrew F. Davis | 3f2c7da | 2019-09-17 17:09:33 -0400 | [diff] [blame] | 184 | if (retval) |
| 185 | fprintf(stderr, "%s: Can't extract subimage from %s\n", |
| 186 | params.cmdname, params.imagefile); |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 187 | } 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 | ¶ms); |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 196 | (void)munmap((void *)ptr, sbuf.st_size); |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 197 | (void)close(ifd); |
| 198 | |
Martyn Welch | 387963a | 2019-01-26 02:31:52 +0000 | [diff] [blame] | 199 | return retval; |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void usage(void) |
| 203 | { |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 204 | 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 Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 207 | params.cmdname); |
| 208 | fprintf(stderr, |
Martyn Welch | 8c1957a | 2019-01-26 02:31:54 +0000 | [diff] [blame] | 209 | " %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 Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 213 | params.cmdname); |
| 214 | fprintf(stderr, |
Martyn Welch | cfd6ade | 2019-01-26 02:31:53 +0000 | [diff] [blame] | 215 | " %s -h ==> print usage information and exit\n", |
| 216 | params.cmdname); |
| 217 | fprintf(stderr, |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 218 | " %s -V ==> print version information and exit\n", |
| 219 | params.cmdname); |
| 220 | |
Martyn Welch | cfd6ade | 2019-01-26 02:31:53 +0000 | [diff] [blame] | 221 | exit(EXIT_SUCCESS); |
Guilherme Maciel Ferreira | 5155381 | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 222 | } |