dumpimage.c 5.4 KB

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