imagetool.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013
  4. *
  5. * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
  6. */
  7. #include "imagetool.h"
  8. #include <image.h>
  9. struct image_type_params *imagetool_get_type(int type)
  10. {
  11. struct image_type_params **curr;
  12. INIT_SECTION(image_type);
  13. struct image_type_params **start = __start_image_type;
  14. struct image_type_params **end = __stop_image_type;
  15. for (curr = start; curr != end; curr++) {
  16. if ((*curr)->check_image_type) {
  17. if (!(*curr)->check_image_type(type))
  18. return *curr;
  19. }
  20. }
  21. return NULL;
  22. }
  23. static int imagetool_verify_print_header_by_type(
  24. void *ptr,
  25. struct stat *sbuf,
  26. struct image_type_params *tparams,
  27. struct image_tool_params *params);
  28. int imagetool_verify_print_header(
  29. void *ptr,
  30. struct stat *sbuf,
  31. struct image_type_params *tparams,
  32. struct image_tool_params *params)
  33. {
  34. int retval = -1;
  35. struct image_type_params **curr;
  36. INIT_SECTION(image_type);
  37. struct image_type_params **start = __start_image_type;
  38. struct image_type_params **end = __stop_image_type;
  39. if (tparams)
  40. return imagetool_verify_print_header_by_type(ptr, sbuf, tparams, params);
  41. for (curr = start; curr != end; curr++) {
  42. /*
  43. * Basically every data file can be guessed / verified as gpimage,
  44. * so skip autodetection of data file as gpimage as it does not work.
  45. */
  46. if ((*curr)->check_image_type && (*curr)->check_image_type(IH_TYPE_GPIMAGE) == 0)
  47. continue;
  48. if ((*curr)->verify_header) {
  49. retval = (*curr)->verify_header((unsigned char *)ptr,
  50. sbuf->st_size, params);
  51. if (retval == 0) {
  52. /*
  53. * Print the image information if verify is
  54. * successful
  55. */
  56. if ((*curr)->print_header) {
  57. if (!params->quiet)
  58. (*curr)->print_header(ptr, params);
  59. } else {
  60. fprintf(stderr,
  61. "%s: print_header undefined for %s\n",
  62. params->cmdname, (*curr)->name);
  63. }
  64. break;
  65. }
  66. }
  67. }
  68. if (retval != 0) {
  69. fprintf(stderr, "%s: cannot detect image type\n",
  70. params->cmdname);
  71. }
  72. return retval;
  73. }
  74. static int imagetool_verify_print_header_by_type(
  75. void *ptr,
  76. struct stat *sbuf,
  77. struct image_type_params *tparams,
  78. struct image_tool_params *params)
  79. {
  80. int retval = -1;
  81. if (tparams->verify_header) {
  82. retval = tparams->verify_header((unsigned char *)ptr,
  83. sbuf->st_size, params);
  84. if (retval == 0) {
  85. /*
  86. * Print the image information if verify is successful
  87. */
  88. if (tparams->print_header) {
  89. if (!params->quiet)
  90. tparams->print_header(ptr, params);
  91. } else {
  92. fprintf(stderr,
  93. "%s: print_header undefined for %s\n",
  94. params->cmdname, tparams->name);
  95. }
  96. } else {
  97. fprintf(stderr,
  98. "%s: verify_header failed for %s with exit code %d\n",
  99. params->cmdname, tparams->name, retval);
  100. }
  101. } else {
  102. fprintf(stderr, "%s: verify_header undefined for %s\n",
  103. params->cmdname, tparams->name);
  104. }
  105. return retval;
  106. }
  107. int imagetool_save_subimage(
  108. const char *file_name,
  109. ulong file_data,
  110. ulong file_len)
  111. {
  112. int dfd;
  113. dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
  114. S_IRUSR | S_IWUSR);
  115. if (dfd < 0) {
  116. fprintf(stderr, "Can't open \"%s\": %s\n",
  117. file_name, strerror(errno));
  118. return -1;
  119. }
  120. if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
  121. fprintf(stderr, "Write error on \"%s\": %s\n",
  122. file_name, strerror(errno));
  123. close(dfd);
  124. return -1;
  125. }
  126. close(dfd);
  127. return 0;
  128. }
  129. int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
  130. {
  131. struct stat sbuf;
  132. int fd;
  133. fd = open(fname, O_RDONLY | O_BINARY);
  134. if (fd < 0) {
  135. fprintf(stderr, "%s: Can't open %s: %s\n",
  136. params->cmdname, fname, strerror(errno));
  137. return -1;
  138. }
  139. if (fstat(fd, &sbuf) < 0) {
  140. fprintf(stderr, "%s: Can't stat %s: %s\n",
  141. params->cmdname, fname, strerror(errno));
  142. close(fd);
  143. return -1;
  144. }
  145. close(fd);
  146. return sbuf.st_size;
  147. }
  148. time_t imagetool_get_source_date(
  149. const char *cmdname,
  150. time_t fallback)
  151. {
  152. char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  153. if (source_date_epoch == NULL)
  154. return fallback;
  155. time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
  156. if (gmtime(&time) == NULL) {
  157. fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",
  158. cmdname);
  159. time = 0;
  160. }
  161. return time;
  162. }