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