imagetool.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. int imagetool_verify_print_header(
  24. void *ptr,
  25. struct stat *sbuf,
  26. struct image_type_params *tparams,
  27. struct image_tool_params *params)
  28. {
  29. int retval = -1;
  30. struct image_type_params **curr;
  31. INIT_SECTION(image_type);
  32. struct image_type_params **start = __start_image_type;
  33. struct image_type_params **end = __stop_image_type;
  34. for (curr = start; curr != end; curr++) {
  35. if ((*curr)->verify_header) {
  36. retval = (*curr)->verify_header((unsigned char *)ptr,
  37. sbuf->st_size, params);
  38. if (retval == 0) {
  39. /*
  40. * Print the image information if verify is
  41. * successful
  42. */
  43. if ((*curr)->print_header) {
  44. if (!params->quiet)
  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_verify_print_header_by_type(
  58. void *ptr,
  59. struct stat *sbuf,
  60. struct image_type_params *tparams,
  61. struct image_tool_params *params)
  62. {
  63. int retval;
  64. retval = tparams->verify_header((unsigned char *)ptr, sbuf->st_size,
  65. params);
  66. if (retval == 0) {
  67. /*
  68. * Print the image information if verify is successful
  69. */
  70. if (tparams->print_header) {
  71. if (!params->quiet)
  72. tparams->print_header(ptr);
  73. } else {
  74. fprintf(stderr,
  75. "%s: print_header undefined for %s\n",
  76. params->cmdname, tparams->name);
  77. }
  78. } else {
  79. fprintf(stderr,
  80. "%s: verify_header failed for %s with exit code %d\n",
  81. params->cmdname, tparams->name, retval);
  82. }
  83. return retval;
  84. }
  85. int imagetool_save_subimage(
  86. const char *file_name,
  87. ulong file_data,
  88. ulong file_len)
  89. {
  90. int dfd;
  91. dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
  92. S_IRUSR | S_IWUSR);
  93. if (dfd < 0) {
  94. fprintf(stderr, "Can't open \"%s\": %s\n",
  95. file_name, strerror(errno));
  96. return -1;
  97. }
  98. if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
  99. fprintf(stderr, "Write error on \"%s\": %s\n",
  100. file_name, strerror(errno));
  101. close(dfd);
  102. return -1;
  103. }
  104. close(dfd);
  105. return 0;
  106. }
  107. int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
  108. {
  109. struct stat sbuf;
  110. int fd;
  111. fd = open(fname, O_RDONLY | O_BINARY);
  112. if (fd < 0) {
  113. fprintf(stderr, "%s: Can't open %s: %s\n",
  114. params->cmdname, fname, strerror(errno));
  115. return -1;
  116. }
  117. if (fstat(fd, &sbuf) < 0) {
  118. fprintf(stderr, "%s: Can't stat %s: %s\n",
  119. params->cmdname, fname, strerror(errno));
  120. close(fd);
  121. return -1;
  122. }
  123. close(fd);
  124. return sbuf.st_size;
  125. }
  126. time_t imagetool_get_source_date(
  127. const char *cmdname,
  128. time_t fallback)
  129. {
  130. char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  131. if (source_date_epoch == NULL)
  132. return fallback;
  133. time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
  134. if (gmtime(&time) == NULL) {
  135. fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",
  136. cmdname);
  137. time = 0;
  138. }
  139. return time;
  140. }