dumpimage.c 5.3 KB

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