dumpimage.c 5.0 KB

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