dumpimage.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Based on mkimage.c.
  3. *
  4. * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include "dumpimage.h"
  9. #include <image.h>
  10. #include <version.h>
  11. static void usage(void);
  12. /* image_type_params linked list to maintain registered image types supports */
  13. static struct image_type_params *dumpimage_tparams;
  14. /* parameters initialized by core will be used by the image type code */
  15. static struct image_tool_params params = {
  16. .type = IH_TYPE_KERNEL,
  17. };
  18. /**
  19. * dumpimage_register() - register respective image generation/list support
  20. *
  21. * the input struct image_type_params is checked and appended to the link
  22. * list, if the input structure is already registered, issue an error
  23. *
  24. * @tparams: Image type parameters
  25. */
  26. static void dumpimage_register(struct image_type_params *tparams)
  27. {
  28. struct image_type_params **tp;
  29. if (!tparams) {
  30. fprintf(stderr, "%s: %s: Null input\n", params.cmdname,
  31. __func__);
  32. exit(EXIT_FAILURE);
  33. }
  34. /* scan the linked list, check for registry and point the last one */
  35. for (tp = &dumpimage_tparams; *tp != NULL; tp = &(*tp)->next) {
  36. if (!strcmp((*tp)->name, tparams->name)) {
  37. fprintf(stderr, "%s: %s already registered\n",
  38. params.cmdname, tparams->name);
  39. return;
  40. }
  41. }
  42. /* add input struct entry at the end of link list */
  43. *tp = tparams;
  44. /* mark input entry as last entry in the link list */
  45. tparams->next = NULL;
  46. debug("Registered %s\n", tparams->name);
  47. }
  48. /*
  49. * dumpimage_extract_datafile -
  50. *
  51. * It scans all registered image types,
  52. * verifies image_header for each supported image type
  53. * if verification is successful, it extracts the desired file,
  54. * indexed by pflag, from the image
  55. *
  56. * returns negative if input image format does not match with any of
  57. * supported image types
  58. */
  59. static int dumpimage_extract_datafile(void *ptr, struct stat *sbuf)
  60. {
  61. int retval = -1;
  62. struct image_type_params *curr;
  63. for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) {
  64. if (curr->verify_header) {
  65. retval = curr->verify_header((unsigned char *)ptr,
  66. sbuf->st_size, &params);
  67. if (retval != 0)
  68. continue;
  69. /*
  70. * Extract the file from the image
  71. * if verify is successful
  72. */
  73. if (curr->extract_datafile) {
  74. curr->extract_datafile(ptr, &params);
  75. } else {
  76. fprintf(stderr,
  77. "%s: extract_datafile undefined for %s\n",
  78. params.cmdname, curr->name);
  79. break;
  80. }
  81. }
  82. }
  83. return retval;
  84. }
  85. int main(int argc, char **argv)
  86. {
  87. int opt;
  88. int ifd = -1;
  89. struct stat sbuf;
  90. char *ptr;
  91. int retval = 0;
  92. struct image_type_params *tparams = NULL;
  93. /* Init all image generation/list support */
  94. register_image_tool(dumpimage_register);
  95. params.cmdname = *argv;
  96. while ((opt = getopt(argc, argv, "li:o:p:V")) != -1) {
  97. switch (opt) {
  98. case 'l':
  99. params.lflag = 1;
  100. break;
  101. case 'i':
  102. params.imagefile = optarg;
  103. params.iflag = 1;
  104. break;
  105. case 'o':
  106. params.outfile = optarg;
  107. break;
  108. case 'p':
  109. params.pflag = strtoul(optarg, &ptr, 10);
  110. if (*ptr) {
  111. fprintf(stderr,
  112. "%s: invalid file position %s\n",
  113. params.cmdname, *argv);
  114. exit(EXIT_FAILURE);
  115. }
  116. break;
  117. case 'V':
  118. printf("dumpimage version %s\n", PLAIN_VERSION);
  119. exit(EXIT_SUCCESS);
  120. default:
  121. usage();
  122. }
  123. }
  124. if (optind >= argc)
  125. usage();
  126. /* set tparams as per input type_id */
  127. tparams = imagetool_get_type(params.type, dumpimage_tparams);
  128. if (tparams == NULL) {
  129. fprintf(stderr, "%s: unsupported type %s\n",
  130. params.cmdname, genimg_get_type_name(params.type));
  131. exit(EXIT_FAILURE);
  132. }
  133. /*
  134. * check the passed arguments parameters meets the requirements
  135. * as per image type to be generated/listed
  136. */
  137. if (tparams->check_params) {
  138. if (tparams->check_params(&params))
  139. usage();
  140. }
  141. if (params.iflag)
  142. params.datafile = argv[optind];
  143. else
  144. params.imagefile = argv[optind];
  145. if (!params.outfile)
  146. params.outfile = params.datafile;
  147. ifd = open(params.imagefile, O_RDONLY|O_BINARY);
  148. if (ifd < 0) {
  149. fprintf(stderr, "%s: Can't open \"%s\": %s\n",
  150. params.cmdname, params.imagefile,
  151. strerror(errno));
  152. exit(EXIT_FAILURE);
  153. }
  154. if (params.lflag || params.iflag) {
  155. if (fstat(ifd, &sbuf) < 0) {
  156. fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
  157. params.cmdname, params.imagefile,
  158. strerror(errno));
  159. exit(EXIT_FAILURE);
  160. }
  161. if ((unsigned)sbuf.st_size < tparams->header_size) {
  162. fprintf(stderr,
  163. "%s: Bad size: \"%s\" is not valid image\n",
  164. params.cmdname, params.imagefile);
  165. exit(EXIT_FAILURE);
  166. }
  167. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  168. if (ptr == MAP_FAILED) {
  169. fprintf(stderr, "%s: Can't read \"%s\": %s\n",
  170. params.cmdname, params.imagefile,
  171. strerror(errno));
  172. exit(EXIT_FAILURE);
  173. }
  174. /*
  175. * Both calls bellow scan through dumpimage registry for all
  176. * supported image types and verify the input image file
  177. * header for match
  178. */
  179. if (params.iflag) {
  180. /*
  181. * Extract the data files from within the matched
  182. * image type. Returns the error code if not matched
  183. */
  184. retval = dumpimage_extract_datafile(ptr, &sbuf);
  185. } else {
  186. /*
  187. * Print the image information for matched image type
  188. * Returns the error code if not matched
  189. */
  190. retval = imagetool_verify_print_header(ptr, &sbuf,
  191. tparams, &params);
  192. }
  193. (void)munmap((void *)ptr, sbuf.st_size);
  194. (void)close(ifd);
  195. return retval;
  196. }
  197. (void)close(ifd);
  198. return EXIT_SUCCESS;
  199. }
  200. static void usage(void)
  201. {
  202. fprintf(stderr, "Usage: %s -l image\n"
  203. " -l ==> list image header information\n",
  204. params.cmdname);
  205. fprintf(stderr,
  206. " %s -i image [-p position] [-o outfile] data_file\n"
  207. " -i ==> extract from the 'image' a specific 'data_file'"
  208. ", indexed by 'position' (starting at 0)\n",
  209. params.cmdname);
  210. fprintf(stderr,
  211. " %s -V ==> print version information and exit\n",
  212. params.cmdname);
  213. exit(EXIT_FAILURE);
  214. }