Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Guilherme Maciel Ferreira | a804b5c | 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 | a804b5c | 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 | a804b5c | 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 | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 19 | /* |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 20 | * dumpimage_extract_subimage - |
Guilherme Maciel Ferreira | a804b5c | 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 | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 30 | static int dumpimage_extract_subimage(struct image_type_params *tparams, |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 31 | void *ptr, struct stat *sbuf) |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 32 | { |
| 33 | int retval = -1; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 34 | |
Guilherme Maciel Ferreira | f41f5b7 | 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); |
| 38 | if (retval != 0) |
| 39 | return -1; |
| 40 | /* |
| 41 | * Extract the file from the image |
| 42 | * if verify is successful |
| 43 | */ |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 44 | if (tparams->extract_subimage) { |
| 45 | retval = tparams->extract_subimage(ptr, ¶ms); |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 46 | } else { |
| 47 | fprintf(stderr, |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 48 | "%s: extract_subimage undefined for %s\n", |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 49 | params.cmdname, tparams->name); |
| 50 | return -2; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | return retval; |
| 55 | } |
| 56 | |
| 57 | int main(int argc, char **argv) |
| 58 | { |
| 59 | int opt; |
| 60 | int ifd = -1; |
| 61 | struct stat sbuf; |
| 62 | char *ptr; |
Martyn Welch | 11e3c1f | 2019-01-26 02:31:52 +0000 | [diff] [blame^] | 63 | int retval = EXIT_SUCCESS; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 64 | struct image_type_params *tparams = NULL; |
| 65 | |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 66 | params.cmdname = *argv; |
| 67 | |
Martyn Welch | 12b8318 | 2019-01-26 02:31:51 +0000 | [diff] [blame] | 68 | while ((opt = getopt(argc, argv, "lo:T:p:V")) != -1) { |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 69 | switch (opt) { |
| 70 | case 'l': |
| 71 | params.lflag = 1; |
| 72 | break; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 73 | case 'o': |
| 74 | params.outfile = optarg; |
Martyn Welch | 12b8318 | 2019-01-26 02:31:51 +0000 | [diff] [blame] | 75 | params.iflag = 1; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 76 | break; |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 77 | case 'T': |
| 78 | params.type = genimg_get_type_id(optarg); |
| 79 | if (params.type < 0) { |
Martyn Welch | 57a608e | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 80 | fprintf(stderr, "%s: Invalid type\n", |
| 81 | params.cmdname); |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 82 | usage(); |
| 83 | } |
| 84 | break; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 85 | case 'p': |
| 86 | params.pflag = strtoul(optarg, &ptr, 10); |
| 87 | if (*ptr) { |
| 88 | fprintf(stderr, |
| 89 | "%s: invalid file position %s\n", |
| 90 | params.cmdname, *argv); |
| 91 | exit(EXIT_FAILURE); |
| 92 | } |
| 93 | break; |
| 94 | case 'V': |
| 95 | printf("dumpimage version %s\n", PLAIN_VERSION); |
| 96 | exit(EXIT_SUCCESS); |
| 97 | default: |
| 98 | usage(); |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 99 | break; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Martyn Welch | 57a608e | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 103 | if (optind >= argc) { |
| 104 | fprintf(stderr, "%s: image file missing\n", params.cmdname); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 105 | usage(); |
Martyn Welch | 57a608e | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 106 | } |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 107 | |
Martyn Welch | 12b8318 | 2019-01-26 02:31:51 +0000 | [diff] [blame] | 108 | params.imagefile = argv[optind]; |
| 109 | |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 110 | /* set tparams as per input type_id */ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 111 | tparams = imagetool_get_type(params.type); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 112 | if (tparams == NULL) { |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 113 | fprintf(stderr, "%s: unsupported type: %s\n", |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 114 | params.cmdname, genimg_get_type_name(params.type)); |
| 115 | exit(EXIT_FAILURE); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * check the passed arguments parameters meets the requirements |
| 120 | * as per image type to be generated/listed |
| 121 | */ |
| 122 | if (tparams->check_params) { |
Martyn Welch | 57a608e | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 123 | if (tparams->check_params(¶ms)) { |
| 124 | fprintf(stderr, "%s: Parameter check failed\n", |
| 125 | params.cmdname); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 126 | usage(); |
Martyn Welch | 57a608e | 2019-01-26 02:31:50 +0000 | [diff] [blame] | 127 | } |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Martyn Welch | 12b8318 | 2019-01-26 02:31:51 +0000 | [diff] [blame] | 130 | if (!params.lflag && !params.outfile) { |
| 131 | fprintf(stderr, "%s: No output file provided\n", |
| 132 | params.cmdname); |
| 133 | exit(EXIT_FAILURE); |
| 134 | } |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 135 | |
| 136 | ifd = open(params.imagefile, O_RDONLY|O_BINARY); |
| 137 | if (ifd < 0) { |
Martyn Welch | 11e3c1f | 2019-01-26 02:31:52 +0000 | [diff] [blame^] | 138 | fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname, |
| 139 | params.imagefile, strerror(errno)); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 140 | exit(EXIT_FAILURE); |
| 141 | } |
| 142 | |
Martyn Welch | 11e3c1f | 2019-01-26 02:31:52 +0000 | [diff] [blame^] | 143 | if (fstat(ifd, &sbuf) < 0) { |
| 144 | fprintf(stderr, "%s: Can't stat \"%s\": %s\n", params.cmdname, |
| 145 | params.imagefile, strerror(errno)); |
| 146 | exit(EXIT_FAILURE); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Martyn Welch | 11e3c1f | 2019-01-26 02:31:52 +0000 | [diff] [blame^] | 149 | if ((uint32_t)sbuf.st_size < tparams->header_size) { |
| 150 | fprintf(stderr, "%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", params.cmdname, |
| 158 | params.imagefile, strerror(errno)); |
| 159 | exit(EXIT_FAILURE); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Both calls bellow scan through dumpimage registry for all |
| 164 | * supported image types and verify the input image file |
| 165 | * header for match |
| 166 | */ |
| 167 | if (params.iflag) { |
| 168 | /* |
| 169 | * Extract the data files from within the matched |
| 170 | * image type. Returns the error code if not matched |
| 171 | */ |
| 172 | retval = dumpimage_extract_subimage(tparams, ptr, &sbuf); |
| 173 | } else { |
| 174 | /* |
| 175 | * Print the image information for matched image type |
| 176 | * Returns the error code if not matched |
| 177 | */ |
| 178 | retval = imagetool_verify_print_header(ptr, &sbuf, tparams, |
| 179 | ¶ms); |
| 180 | } |
| 181 | |
| 182 | (void)munmap((void *)ptr, sbuf.st_size); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 183 | (void)close(ifd); |
| 184 | |
Martyn Welch | 11e3c1f | 2019-01-26 02:31:52 +0000 | [diff] [blame^] | 185 | return retval; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void usage(void) |
| 189 | { |
| 190 | fprintf(stderr, "Usage: %s -l image\n" |
| 191 | " -l ==> list image header information\n", |
| 192 | params.cmdname); |
| 193 | fprintf(stderr, |
Martyn Welch | 12b8318 | 2019-01-26 02:31:51 +0000 | [diff] [blame] | 194 | " %s -T type [-p position] [-o outfile] image\n" |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 195 | " -T ==> set image type to 'type'\n" |
Martyn Welch | 12b8318 | 2019-01-26 02:31:51 +0000 | [diff] [blame] | 196 | " -p ==> 'position' (starting at 0) of the component to extract from image\n", |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 197 | params.cmdname); |
| 198 | fprintf(stderr, |
| 199 | " %s -V ==> print version information and exit\n", |
| 200 | params.cmdname); |
| 201 | |
| 202 | exit(EXIT_FAILURE); |
| 203 | } |