dumpimage.c 4.8 KB

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