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