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; |
| 63 | int retval = 0; |
| 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 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 68 | while ((opt = getopt(argc, argv, "li:o: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; |
| 73 | case 'i': |
| 74 | params.imagefile = optarg; |
| 75 | params.iflag = 1; |
| 76 | break; |
| 77 | case 'o': |
| 78 | params.outfile = optarg; |
| 79 | break; |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 80 | case 'T': |
| 81 | params.type = genimg_get_type_id(optarg); |
| 82 | if (params.type < 0) { |
| 83 | usage(); |
| 84 | } |
| 85 | break; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 86 | case 'p': |
| 87 | params.pflag = strtoul(optarg, &ptr, 10); |
| 88 | if (*ptr) { |
| 89 | fprintf(stderr, |
| 90 | "%s: invalid file position %s\n", |
| 91 | params.cmdname, *argv); |
| 92 | exit(EXIT_FAILURE); |
| 93 | } |
| 94 | break; |
| 95 | case 'V': |
| 96 | printf("dumpimage version %s\n", PLAIN_VERSION); |
| 97 | exit(EXIT_SUCCESS); |
| 98 | default: |
| 99 | usage(); |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 100 | break; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | if (optind >= argc) |
| 105 | usage(); |
| 106 | |
| 107 | /* set tparams as per input type_id */ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 108 | tparams = imagetool_get_type(params.type); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 109 | if (tparams == NULL) { |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 110 | fprintf(stderr, "%s: unsupported type: %s\n", |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 111 | params.cmdname, genimg_get_type_name(params.type)); |
| 112 | exit(EXIT_FAILURE); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * check the passed arguments parameters meets the requirements |
| 117 | * as per image type to be generated/listed |
| 118 | */ |
| 119 | if (tparams->check_params) { |
| 120 | if (tparams->check_params(¶ms)) |
| 121 | usage(); |
| 122 | } |
| 123 | |
| 124 | if (params.iflag) |
| 125 | params.datafile = argv[optind]; |
| 126 | else |
| 127 | params.imagefile = argv[optind]; |
| 128 | if (!params.outfile) |
| 129 | params.outfile = params.datafile; |
| 130 | |
| 131 | ifd = open(params.imagefile, O_RDONLY|O_BINARY); |
| 132 | if (ifd < 0) { |
| 133 | fprintf(stderr, "%s: Can't open \"%s\": %s\n", |
| 134 | params.cmdname, params.imagefile, |
| 135 | strerror(errno)); |
| 136 | exit(EXIT_FAILURE); |
| 137 | } |
| 138 | |
| 139 | if (params.lflag || params.iflag) { |
| 140 | if (fstat(ifd, &sbuf) < 0) { |
| 141 | fprintf(stderr, "%s: Can't stat \"%s\": %s\n", |
| 142 | params.cmdname, params.imagefile, |
| 143 | strerror(errno)); |
| 144 | exit(EXIT_FAILURE); |
| 145 | } |
| 146 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 147 | if ((uint32_t)sbuf.st_size < tparams->header_size) { |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 148 | fprintf(stderr, |
| 149 | "%s: Bad size: \"%s\" is not valid image\n", |
| 150 | params.cmdname, params.imagefile); |
| 151 | exit(EXIT_FAILURE); |
| 152 | } |
| 153 | |
| 154 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); |
| 155 | if (ptr == MAP_FAILED) { |
| 156 | fprintf(stderr, "%s: Can't read \"%s\": %s\n", |
| 157 | params.cmdname, params.imagefile, |
| 158 | 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 | */ |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 172 | retval = dumpimage_extract_subimage(tparams, ptr, |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 173 | &sbuf); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 174 | } else { |
| 175 | /* |
| 176 | * Print the image information for matched image type |
| 177 | * Returns the error code if not matched |
| 178 | */ |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 179 | retval = imagetool_verify_print_header(ptr, &sbuf, |
| 180 | tparams, ¶ms); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | (void)munmap((void *)ptr, sbuf.st_size); |
| 184 | (void)close(ifd); |
| 185 | |
| 186 | return retval; |
| 187 | } |
| 188 | |
| 189 | (void)close(ifd); |
| 190 | |
| 191 | return EXIT_SUCCESS; |
| 192 | } |
| 193 | |
| 194 | static void usage(void) |
| 195 | { |
| 196 | fprintf(stderr, "Usage: %s -l image\n" |
| 197 | " -l ==> list image header information\n", |
| 198 | params.cmdname); |
| 199 | fprintf(stderr, |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 200 | " %s -i image -T type [-p position] [-o outfile] data_file\n" |
| 201 | " -i ==> extract from the 'image' a specific 'data_file'\n" |
| 202 | " -T ==> set image type to 'type'\n" |
| 203 | " -p ==> 'position' (starting at 0) of the 'data_file' inside the 'image'\n", |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 204 | params.cmdname); |
| 205 | fprintf(stderr, |
| 206 | " %s -V ==> print version information and exit\n", |
| 207 | params.cmdname); |
| 208 | |
| 209 | exit(EXIT_FAILURE); |
| 210 | } |