Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Based on mkimage.c. |
| 3 | * |
| 4 | * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include "dumpimage.h" |
| 10 | #include <image.h> |
| 11 | #include <version.h> |
| 12 | |
| 13 | static void usage(void); |
| 14 | |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 15 | /* parameters initialized by core will be used by the image type code */ |
| 16 | static struct image_tool_params params = { |
| 17 | .type = IH_TYPE_KERNEL, |
| 18 | }; |
| 19 | |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 20 | /* |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 21 | * dumpimage_extract_subimage - |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 22 | * |
| 23 | * It scans all registered image types, |
| 24 | * verifies image_header for each supported image type |
| 25 | * if verification is successful, it extracts the desired file, |
| 26 | * indexed by pflag, from the image |
| 27 | * |
| 28 | * returns negative if input image format does not match with any of |
| 29 | * supported image types |
| 30 | */ |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 31 | static int dumpimage_extract_subimage(struct image_type_params *tparams, |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 32 | void *ptr, struct stat *sbuf) |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 33 | { |
| 34 | int retval = -1; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 35 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 36 | if (tparams->verify_header) { |
| 37 | retval = tparams->verify_header((unsigned char *)ptr, |
| 38 | sbuf->st_size, ¶ms); |
| 39 | if (retval != 0) |
| 40 | return -1; |
| 41 | /* |
| 42 | * Extract the file from the image |
| 43 | * if verify is successful |
| 44 | */ |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 45 | if (tparams->extract_subimage) { |
| 46 | retval = tparams->extract_subimage(ptr, ¶ms); |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 47 | } else { |
| 48 | fprintf(stderr, |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 49 | "%s: extract_subimage undefined for %s\n", |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 50 | params.cmdname, tparams->name); |
| 51 | return -2; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | return retval; |
| 56 | } |
| 57 | |
| 58 | int main(int argc, char **argv) |
| 59 | { |
| 60 | int opt; |
| 61 | int ifd = -1; |
| 62 | struct stat sbuf; |
| 63 | char *ptr; |
| 64 | int retval = 0; |
| 65 | struct image_type_params *tparams = NULL; |
| 66 | |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 67 | params.cmdname = *argv; |
| 68 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 69 | while ((opt = getopt(argc, argv, "li:o:T:p:V")) != -1) { |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 70 | switch (opt) { |
| 71 | case 'l': |
| 72 | params.lflag = 1; |
| 73 | break; |
| 74 | case 'i': |
| 75 | params.imagefile = optarg; |
| 76 | params.iflag = 1; |
| 77 | break; |
| 78 | case 'o': |
| 79 | params.outfile = optarg; |
| 80 | break; |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 81 | case 'T': |
| 82 | params.type = genimg_get_type_id(optarg); |
| 83 | if (params.type < 0) { |
| 84 | usage(); |
| 85 | } |
| 86 | break; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 87 | case 'p': |
| 88 | params.pflag = strtoul(optarg, &ptr, 10); |
| 89 | if (*ptr) { |
| 90 | fprintf(stderr, |
| 91 | "%s: invalid file position %s\n", |
| 92 | params.cmdname, *argv); |
| 93 | exit(EXIT_FAILURE); |
| 94 | } |
| 95 | break; |
| 96 | case 'V': |
| 97 | printf("dumpimage version %s\n", PLAIN_VERSION); |
| 98 | exit(EXIT_SUCCESS); |
| 99 | default: |
| 100 | usage(); |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 101 | break; |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | if (optind >= argc) |
| 106 | usage(); |
| 107 | |
| 108 | /* set tparams as per input type_id */ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 109 | tparams = imagetool_get_type(params.type); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 110 | if (tparams == NULL) { |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 111 | fprintf(stderr, "%s: unsupported type: %s\n", |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 112 | params.cmdname, genimg_get_type_name(params.type)); |
| 113 | exit(EXIT_FAILURE); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * check the passed arguments parameters meets the requirements |
| 118 | * as per image type to be generated/listed |
| 119 | */ |
| 120 | if (tparams->check_params) { |
| 121 | if (tparams->check_params(¶ms)) |
| 122 | usage(); |
| 123 | } |
| 124 | |
| 125 | if (params.iflag) |
| 126 | params.datafile = argv[optind]; |
| 127 | else |
| 128 | params.imagefile = argv[optind]; |
| 129 | if (!params.outfile) |
| 130 | params.outfile = params.datafile; |
| 131 | |
| 132 | ifd = open(params.imagefile, O_RDONLY|O_BINARY); |
| 133 | if (ifd < 0) { |
| 134 | fprintf(stderr, "%s: Can't open \"%s\": %s\n", |
| 135 | params.cmdname, params.imagefile, |
| 136 | strerror(errno)); |
| 137 | exit(EXIT_FAILURE); |
| 138 | } |
| 139 | |
| 140 | if (params.lflag || params.iflag) { |
| 141 | if (fstat(ifd, &sbuf) < 0) { |
| 142 | fprintf(stderr, "%s: Can't stat \"%s\": %s\n", |
| 143 | params.cmdname, params.imagefile, |
| 144 | strerror(errno)); |
| 145 | exit(EXIT_FAILURE); |
| 146 | } |
| 147 | |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 148 | if ((uint32_t)sbuf.st_size < tparams->header_size) { |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 149 | fprintf(stderr, |
| 150 | "%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", |
| 158 | params.cmdname, params.imagefile, |
| 159 | strerror(errno)); |
| 160 | exit(EXIT_FAILURE); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Both calls bellow scan through dumpimage registry for all |
| 165 | * supported image types and verify the input image file |
| 166 | * header for match |
| 167 | */ |
| 168 | if (params.iflag) { |
| 169 | /* |
| 170 | * Extract the data files from within the matched |
| 171 | * image type. Returns the error code if not matched |
| 172 | */ |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 173 | retval = dumpimage_extract_subimage(tparams, ptr, |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 174 | &sbuf); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 175 | } else { |
| 176 | /* |
| 177 | * Print the image information for matched image type |
| 178 | * Returns the error code if not matched |
| 179 | */ |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 180 | retval = imagetool_verify_print_header(ptr, &sbuf, |
| 181 | tparams, ¶ms); |
Guilherme Maciel Ferreira | a804b5c | 2013-12-01 12:43:11 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | (void)munmap((void *)ptr, sbuf.st_size); |
| 185 | (void)close(ifd); |
| 186 | |
| 187 | return retval; |
| 188 | } |
| 189 | |
| 190 | (void)close(ifd); |
| 191 | |
| 192 | return EXIT_SUCCESS; |
| 193 | } |
| 194 | |
| 195 | static void usage(void) |
| 196 | { |
| 197 | fprintf(stderr, "Usage: %s -l image\n" |
| 198 | " -l ==> list image header information\n", |
| 199 | params.cmdname); |
| 200 | fprintf(stderr, |
Guilherme Maciel Ferreira | f41f5b7 | 2015-01-15 02:54:40 -0200 | [diff] [blame] | 201 | " %s -i image -T type [-p position] [-o outfile] data_file\n" |
| 202 | " -i ==> extract from the 'image' a specific 'data_file'\n" |
| 203 | " -T ==> set image type to 'type'\n" |
| 204 | " -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] | 205 | params.cmdname); |
| 206 | fprintf(stderr, |
| 207 | " %s -V ==> print version information and exit\n", |
| 208 | params.cmdname); |
| 209 | |
| 210 | exit(EXIT_FAILURE); |
| 211 | } |