1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Based on mkimage.c. 4 * 5 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> 6 */ 7 8 #include "dumpimage.h" 9 #include <image.h> 10 #include <version.h> 11 12 static void usage(void); 13 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 19 /* 20 * dumpimage_extract_subimage - 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 */ 30 static int dumpimage_extract_subimage(struct image_type_params *tparams, 31 void *ptr, struct stat *sbuf) 32 { 33 int retval = -1; 34 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 */ 44 if (tparams->extract_subimage) { 45 retval = tparams->extract_subimage(ptr, ¶ms); 46 } else { 47 fprintf(stderr, 48 "%s: extract_subimage undefined for %s\n", 49 params.cmdname, tparams->name); 50 return -2; 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 66 params.cmdname = *argv; 67 68 while ((opt = getopt(argc, argv, "li:o:T:p:V")) != -1) { 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; 80 case 'T': 81 params.type = genimg_get_type_id(optarg); 82 if (params.type < 0) { 83 fprintf(stderr, "%s: Invalid type\n", 84 params.cmdname); 85 usage(); 86 } 87 break; 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); 100 default: 101 usage(); 102 break; 103 } 104 } 105 106 if (optind >= argc) { 107 fprintf(stderr, "%s: image file missing\n", params.cmdname); 108 usage(); 109 } 110 111 /* set tparams as per input type_id */ 112 tparams = imagetool_get_type(params.type); 113 if (tparams == NULL) { 114 fprintf(stderr, "%s: unsupported type: %s\n", 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 fprintf(stderr, "%s: Parameter check failed\n", 126 params.cmdname); 127 usage(); 128 } 129 } 130 131 if (params.iflag) 132 params.datafile = argv[optind]; 133 else 134 params.imagefile = argv[optind]; 135 if (!params.outfile) 136 params.outfile = params.datafile; 137 138 ifd = open(params.imagefile, O_RDONLY|O_BINARY); 139 if (ifd < 0) { 140 fprintf(stderr, "%s: Can't open \"%s\": %s\n", 141 params.cmdname, params.imagefile, 142 strerror(errno)); 143 exit(EXIT_FAILURE); 144 } 145 146 if (params.lflag || params.iflag) { 147 if (fstat(ifd, &sbuf) < 0) { 148 fprintf(stderr, "%s: Can't stat \"%s\": %s\n", 149 params.cmdname, params.imagefile, 150 strerror(errno)); 151 exit(EXIT_FAILURE); 152 } 153 154 if ((uint32_t)sbuf.st_size < tparams->header_size) { 155 fprintf(stderr, 156 "%s: Bad size: \"%s\" is not valid image\n", 157 params.cmdname, params.imagefile); 158 exit(EXIT_FAILURE); 159 } 160 161 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); 162 if (ptr == MAP_FAILED) { 163 fprintf(stderr, "%s: Can't read \"%s\": %s\n", 164 params.cmdname, params.imagefile, 165 strerror(errno)); 166 exit(EXIT_FAILURE); 167 } 168 169 /* 170 * Both calls bellow scan through dumpimage registry for all 171 * supported image types and verify the input image file 172 * header for match 173 */ 174 if (params.iflag) { 175 /* 176 * Extract the data files from within the matched 177 * image type. Returns the error code if not matched 178 */ 179 retval = dumpimage_extract_subimage(tparams, ptr, 180 &sbuf); 181 } else { 182 /* 183 * Print the image information for matched image type 184 * Returns the error code if not matched 185 */ 186 retval = imagetool_verify_print_header(ptr, &sbuf, 187 tparams, ¶ms); 188 } 189 190 (void)munmap((void *)ptr, sbuf.st_size); 191 (void)close(ifd); 192 193 return retval; 194 } 195 196 (void)close(ifd); 197 198 return EXIT_SUCCESS; 199 } 200 201 static void usage(void) 202 { 203 fprintf(stderr, "Usage: %s -l image\n" 204 " -l ==> list image header information\n", 205 params.cmdname); 206 fprintf(stderr, 207 " %s -i image -T type [-p position] [-o outfile] data_file\n" 208 " -i ==> extract from the 'image' a specific 'data_file'\n" 209 " -T ==> set image type to 'type'\n" 210 " -p ==> 'position' (starting at 0) of the 'data_file' inside the 'image'\n", 211 params.cmdname); 212 fprintf(stderr, 213 " %s -V ==> print version information and exit\n", 214 params.cmdname); 215 216 exit(EXIT_FAILURE); 217 } 218