dumpimage.c 5.0 KB

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