dumpimage.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_get_type() - find the image type params for a given image type
  50. *
  51. * Scan all registered image types and check the input type_id for each
  52. * supported image type
  53. *
  54. * @return respective image_type_params pointer. If the input type is not
  55. * supported by any of registered image types, returns NULL
  56. */
  57. static struct image_type_params *dumpimage_get_type(int type)
  58. {
  59. struct image_type_params *curr;
  60. for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) {
  61. if (curr->check_image_type) {
  62. if (!curr->check_image_type(type))
  63. return curr;
  64. }
  65. }
  66. return NULL;
  67. }
  68. /*
  69. * dumpimage_verify_print_header() - verifies the image header
  70. *
  71. * Scan registered image types and verify the image_header for each
  72. * supported image type. If verification is successful, this prints
  73. * the respective header.
  74. *
  75. * @return 0 on success, negative if input image format does not match with
  76. * any of supported image types
  77. */
  78. static int dumpimage_verify_print_header(void *ptr, struct stat *sbuf)
  79. {
  80. int retval = -1;
  81. struct image_type_params *curr;
  82. for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) {
  83. if (curr->verify_header) {
  84. retval = curr->verify_header((unsigned char *)ptr,
  85. sbuf->st_size, &params);
  86. if (retval != 0)
  87. continue;
  88. /*
  89. * Print the image information if verify is
  90. * successful
  91. */
  92. if (curr->print_header) {
  93. curr->print_header(ptr);
  94. } else {
  95. fprintf(stderr,
  96. "%s: print_header undefined for %s\n",
  97. params.cmdname, curr->name);
  98. }
  99. break;
  100. }
  101. }
  102. return retval;
  103. }
  104. /*
  105. * dumpimage_extract_datafile -
  106. *
  107. * It scans all registered image types,
  108. * verifies image_header for each supported image type
  109. * if verification is successful, it extracts the desired file,
  110. * indexed by pflag, from the image
  111. *
  112. * returns negative if input image format does not match with any of
  113. * supported image types
  114. */
  115. static int dumpimage_extract_datafile(void *ptr, struct stat *sbuf)
  116. {
  117. int retval = -1;
  118. struct image_type_params *curr;
  119. for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) {
  120. if (curr->verify_header) {
  121. retval = curr->verify_header((unsigned char *)ptr,
  122. sbuf->st_size, &params);
  123. if (retval != 0)
  124. continue;
  125. /*
  126. * Extract the file from the image
  127. * if verify is successful
  128. */
  129. if (curr->extract_datafile) {
  130. curr->extract_datafile(ptr, &params);
  131. } else {
  132. fprintf(stderr,
  133. "%s: extract_datafile undefined for %s\n",
  134. params.cmdname, curr->name);
  135. break;
  136. }
  137. }
  138. }
  139. return retval;
  140. }
  141. int main(int argc, char **argv)
  142. {
  143. int opt;
  144. int ifd = -1;
  145. struct stat sbuf;
  146. char *ptr;
  147. int retval = 0;
  148. struct image_type_params *tparams = NULL;
  149. /* Init all image generation/list support */
  150. register_image_tool(dumpimage_register);
  151. params.cmdname = *argv;
  152. while ((opt = getopt(argc, argv, "li:o:p:V")) != -1) {
  153. switch (opt) {
  154. case 'l':
  155. params.lflag = 1;
  156. break;
  157. case 'i':
  158. params.imagefile = optarg;
  159. params.iflag = 1;
  160. break;
  161. case 'o':
  162. params.outfile = optarg;
  163. break;
  164. case 'p':
  165. params.pflag = strtoul(optarg, &ptr, 10);
  166. if (*ptr) {
  167. fprintf(stderr,
  168. "%s: invalid file position %s\n",
  169. params.cmdname, *argv);
  170. exit(EXIT_FAILURE);
  171. }
  172. break;
  173. case 'V':
  174. printf("dumpimage version %s\n", PLAIN_VERSION);
  175. exit(EXIT_SUCCESS);
  176. default:
  177. usage();
  178. }
  179. }
  180. if (optind >= argc)
  181. usage();
  182. /* set tparams as per input type_id */
  183. tparams = dumpimage_get_type(params.type);
  184. if (tparams == NULL) {
  185. fprintf(stderr, "%s: unsupported type %s\n",
  186. params.cmdname, genimg_get_type_name(params.type));
  187. exit(EXIT_FAILURE);
  188. }
  189. /*
  190. * check the passed arguments parameters meets the requirements
  191. * as per image type to be generated/listed
  192. */
  193. if (tparams->check_params) {
  194. if (tparams->check_params(&params))
  195. usage();
  196. }
  197. if (params.iflag)
  198. params.datafile = argv[optind];
  199. else
  200. params.imagefile = argv[optind];
  201. if (!params.outfile)
  202. params.outfile = params.datafile;
  203. ifd = open(params.imagefile, O_RDONLY|O_BINARY);
  204. if (ifd < 0) {
  205. fprintf(stderr, "%s: Can't open \"%s\": %s\n",
  206. params.cmdname, params.imagefile,
  207. strerror(errno));
  208. exit(EXIT_FAILURE);
  209. }
  210. if (params.lflag || params.iflag) {
  211. if (fstat(ifd, &sbuf) < 0) {
  212. fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
  213. params.cmdname, params.imagefile,
  214. strerror(errno));
  215. exit(EXIT_FAILURE);
  216. }
  217. if ((unsigned)sbuf.st_size < tparams->header_size) {
  218. fprintf(stderr,
  219. "%s: Bad size: \"%s\" is not valid image\n",
  220. params.cmdname, params.imagefile);
  221. exit(EXIT_FAILURE);
  222. }
  223. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  224. if (ptr == MAP_FAILED) {
  225. fprintf(stderr, "%s: Can't read \"%s\": %s\n",
  226. params.cmdname, params.imagefile,
  227. strerror(errno));
  228. exit(EXIT_FAILURE);
  229. }
  230. /*
  231. * Both calls bellow scan through dumpimage registry for all
  232. * supported image types and verify the input image file
  233. * header for match
  234. */
  235. if (params.iflag) {
  236. /*
  237. * Extract the data files from within the matched
  238. * image type. Returns the error code if not matched
  239. */
  240. retval = dumpimage_extract_datafile(ptr, &sbuf);
  241. } else {
  242. /*
  243. * Print the image information for matched image type
  244. * Returns the error code if not matched
  245. */
  246. retval = dumpimage_verify_print_header(ptr, &sbuf);
  247. }
  248. (void)munmap((void *)ptr, sbuf.st_size);
  249. (void)close(ifd);
  250. return retval;
  251. }
  252. (void)close(ifd);
  253. return EXIT_SUCCESS;
  254. }
  255. static void usage(void)
  256. {
  257. fprintf(stderr, "Usage: %s -l image\n"
  258. " -l ==> list image header information\n",
  259. params.cmdname);
  260. fprintf(stderr,
  261. " %s -i image [-p position] [-o outfile] data_file\n"
  262. " -i ==> extract from the 'image' a specific 'data_file'"
  263. ", indexed by 'position' (starting at 0)\n",
  264. params.cmdname);
  265. fprintf(stderr,
  266. " %s -V ==> print version information and exit\n",
  267. params.cmdname);
  268. exit(EXIT_FAILURE);
  269. }