123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- #include "dumpimage.h"
- #include <image.h>
- #include <version.h>
- static void usage(void);
- static struct image_tool_params params = {
- .type = IH_TYPE_KERNEL,
- };
- static int dumpimage_extract_subimage(struct image_type_params *tparams,
- void *ptr, struct stat *sbuf)
- {
- int retval = -1;
- if (tparams->verify_header) {
- retval = tparams->verify_header((unsigned char *)ptr,
- sbuf->st_size, ¶ms);
- if (retval != 0)
- return -1;
-
- if (tparams->extract_subimage) {
- retval = tparams->extract_subimage(ptr, ¶ms);
- } else {
- fprintf(stderr,
- "%s: extract_subimage undefined for %s\n",
- params.cmdname, tparams->name);
- return -2;
- }
- }
- return retval;
- }
- int main(int argc, char **argv)
- {
- int opt;
- int ifd = -1;
- struct stat sbuf;
- char *ptr;
- int retval = 0;
- struct image_type_params *tparams = NULL;
- params.cmdname = *argv;
- while ((opt = getopt(argc, argv, "li:o:T:p:V")) != -1) {
- switch (opt) {
- case 'l':
- params.lflag = 1;
- break;
- case 'i':
- params.imagefile = optarg;
- params.iflag = 1;
- break;
- case 'o':
- params.outfile = optarg;
- break;
- case 'T':
- params.type = genimg_get_type_id(optarg);
- if (params.type < 0) {
- usage();
- }
- break;
- case 'p':
- params.pflag = strtoul(optarg, &ptr, 10);
- if (*ptr) {
- fprintf(stderr,
- "%s: invalid file position %s\n",
- params.cmdname, *argv);
- exit(EXIT_FAILURE);
- }
- break;
- case 'V':
- printf("dumpimage version %s\n", PLAIN_VERSION);
- exit(EXIT_SUCCESS);
- default:
- usage();
- break;
- }
- }
- if (optind >= argc)
- usage();
-
- tparams = imagetool_get_type(params.type);
- if (tparams == NULL) {
- fprintf(stderr, "%s: unsupported type: %s\n",
- params.cmdname, genimg_get_type_name(params.type));
- exit(EXIT_FAILURE);
- }
-
- if (tparams->check_params) {
- if (tparams->check_params(¶ms))
- usage();
- }
- if (params.iflag)
- params.datafile = argv[optind];
- else
- params.imagefile = argv[optind];
- if (!params.outfile)
- params.outfile = params.datafile;
- ifd = open(params.imagefile, O_RDONLY|O_BINARY);
- if (ifd < 0) {
- fprintf(stderr, "%s: Can't open \"%s\": %s\n",
- params.cmdname, params.imagefile,
- strerror(errno));
- exit(EXIT_FAILURE);
- }
- if (params.lflag || params.iflag) {
- if (fstat(ifd, &sbuf) < 0) {
- fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
- params.cmdname, params.imagefile,
- strerror(errno));
- exit(EXIT_FAILURE);
- }
- if ((uint32_t)sbuf.st_size < tparams->header_size) {
- fprintf(stderr,
- "%s: Bad size: \"%s\" is not valid image\n",
- params.cmdname, params.imagefile);
- exit(EXIT_FAILURE);
- }
- ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
- if (ptr == MAP_FAILED) {
- fprintf(stderr, "%s: Can't read \"%s\": %s\n",
- params.cmdname, params.imagefile,
- strerror(errno));
- exit(EXIT_FAILURE);
- }
-
- if (params.iflag) {
-
- retval = dumpimage_extract_subimage(tparams, ptr,
- &sbuf);
- } else {
-
- retval = imagetool_verify_print_header(ptr, &sbuf,
- tparams, ¶ms);
- }
- (void)munmap((void *)ptr, sbuf.st_size);
- (void)close(ifd);
- return retval;
- }
- (void)close(ifd);
- return EXIT_SUCCESS;
- }
- static void usage(void)
- {
- fprintf(stderr, "Usage: %s -l image\n"
- " -l ==> list image header information\n",
- params.cmdname);
- fprintf(stderr,
- " %s -i image -T type [-p position] [-o outfile] data_file\n"
- " -i ==> extract from the 'image' a specific 'data_file'\n"
- " -T ==> set image type to 'type'\n"
- " -p ==> 'position' (starting at 0) of the 'data_file' inside the 'image'\n",
- params.cmdname);
- fprintf(stderr,
- " %s -V ==> print version information and exit\n",
- params.cmdname);
- exit(EXIT_FAILURE);
- }
|