dumpimage.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. usage();
  75. }
  76. break;
  77. case 'p':
  78. params.pflag = strtoul(optarg, &ptr, 10);
  79. if (*ptr) {
  80. fprintf(stderr,
  81. "%s: invalid file position %s\n",
  82. params.cmdname, *argv);
  83. exit(EXIT_FAILURE);
  84. }
  85. break;
  86. case 'V':
  87. printf("dumpimage version %s\n", PLAIN_VERSION);
  88. exit(EXIT_SUCCESS);
  89. default:
  90. usage();
  91. break;
  92. }
  93. }
  94. if (optind >= argc)
  95. usage();
  96. /* set tparams as per input type_id */
  97. tparams = imagetool_get_type(params.type);
  98. if (tparams == NULL) {
  99. fprintf(stderr, "%s: unsupported type: %s\n",
  100. params.cmdname, genimg_get_type_name(params.type));
  101. exit(EXIT_FAILURE);
  102. }
  103. /*
  104. * check the passed arguments parameters meets the requirements
  105. * as per image type to be generated/listed
  106. */
  107. if (tparams->check_params) {
  108. if (tparams->check_params(&params))
  109. usage();
  110. }
  111. if (params.iflag)
  112. params.datafile = argv[optind];
  113. else
  114. params.imagefile = argv[optind];
  115. if (!params.outfile)
  116. params.outfile = params.datafile;
  117. ifd = open(params.imagefile, O_RDONLY|O_BINARY);
  118. if (ifd < 0) {
  119. fprintf(stderr, "%s: Can't open \"%s\": %s\n",
  120. params.cmdname, params.imagefile,
  121. strerror(errno));
  122. exit(EXIT_FAILURE);
  123. }
  124. if (params.lflag || params.iflag) {
  125. if (fstat(ifd, &sbuf) < 0) {
  126. fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
  127. params.cmdname, params.imagefile,
  128. strerror(errno));
  129. exit(EXIT_FAILURE);
  130. }
  131. if ((uint32_t)sbuf.st_size < tparams->header_size) {
  132. fprintf(stderr,
  133. "%s: Bad size: \"%s\" is not valid image\n",
  134. params.cmdname, params.imagefile);
  135. exit(EXIT_FAILURE);
  136. }
  137. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  138. if (ptr == MAP_FAILED) {
  139. fprintf(stderr, "%s: Can't read \"%s\": %s\n",
  140. params.cmdname, params.imagefile,
  141. strerror(errno));
  142. exit(EXIT_FAILURE);
  143. }
  144. /*
  145. * Both calls bellow scan through dumpimage registry for all
  146. * supported image types and verify the input image file
  147. * header for match
  148. */
  149. if (params.iflag) {
  150. /*
  151. * Extract the data files from within the matched
  152. * image type. Returns the error code if not matched
  153. */
  154. retval = dumpimage_extract_subimage(tparams, ptr,
  155. &sbuf);
  156. } else {
  157. /*
  158. * Print the image information for matched image type
  159. * Returns the error code if not matched
  160. */
  161. retval = imagetool_verify_print_header(ptr, &sbuf,
  162. tparams, &params);
  163. }
  164. (void)munmap((void *)ptr, sbuf.st_size);
  165. (void)close(ifd);
  166. return retval;
  167. }
  168. (void)close(ifd);
  169. return EXIT_SUCCESS;
  170. }
  171. static void usage(void)
  172. {
  173. fprintf(stderr, "Usage: %s -l image\n"
  174. " -l ==> list image header information\n",
  175. params.cmdname);
  176. fprintf(stderr,
  177. " %s -i image -T type [-p position] [-o outfile] data_file\n"
  178. " -i ==> extract from the 'image' a specific 'data_file'\n"
  179. " -T ==> set image type to 'type'\n"
  180. " -p ==> 'position' (starting at 0) of the 'data_file' inside the 'image'\n",
  181. params.cmdname);
  182. fprintf(stderr,
  183. " %s -V ==> print version information and exit\n",
  184. params.cmdname);
  185. exit(EXIT_FAILURE);
  186. }