imagetool.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * (C) Copyright 2013
  3. *
  4. * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include "imagetool.h"
  9. #include <image.h>
  10. struct image_type_params *imagetool_get_type(int type)
  11. {
  12. struct image_type_params **curr;
  13. INIT_SECTION(image_type);
  14. struct image_type_params **start = __start_image_type;
  15. struct image_type_params **end = __stop_image_type;
  16. for (curr = start; curr != end; curr++) {
  17. if ((*curr)->check_image_type) {
  18. if (!(*curr)->check_image_type(type))
  19. return *curr;
  20. }
  21. }
  22. return NULL;
  23. }
  24. int imagetool_verify_print_header(
  25. void *ptr,
  26. struct stat *sbuf,
  27. struct image_type_params *tparams,
  28. struct image_tool_params *params)
  29. {
  30. int retval = -1;
  31. struct image_type_params **curr;
  32. INIT_SECTION(image_type);
  33. struct image_type_params **start = __start_image_type;
  34. struct image_type_params **end = __stop_image_type;
  35. for (curr = start; curr != end; curr++) {
  36. if ((*curr)->verify_header) {
  37. retval = (*curr)->verify_header((unsigned char *)ptr,
  38. sbuf->st_size, params);
  39. if (retval == 0) {
  40. /*
  41. * Print the image information if verify is
  42. * successful
  43. */
  44. if ((*curr)->print_header) {
  45. (*curr)->print_header(ptr);
  46. } else {
  47. fprintf(stderr,
  48. "%s: print_header undefined for %s\n",
  49. params->cmdname, (*curr)->name);
  50. }
  51. break;
  52. }
  53. }
  54. }
  55. return retval;
  56. }
  57. int imagetool_save_subimage(
  58. const char *file_name,
  59. ulong file_data,
  60. ulong file_len)
  61. {
  62. int dfd;
  63. dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
  64. S_IRUSR | S_IWUSR);
  65. if (dfd < 0) {
  66. fprintf(stderr, "Can't open \"%s\": %s\n",
  67. file_name, strerror(errno));
  68. return -1;
  69. }
  70. if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
  71. fprintf(stderr, "Write error on \"%s\": %s\n",
  72. file_name, strerror(errno));
  73. close(dfd);
  74. return -1;
  75. }
  76. close(dfd);
  77. return 0;
  78. }