imagetool.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_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. }
  79. int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
  80. {
  81. struct stat sbuf;
  82. int fd;
  83. fd = open(fname, O_RDONLY | O_BINARY);
  84. if (fd < 0) {
  85. fprintf(stderr, "%s: Can't open %s: %s\n",
  86. params->cmdname, fname, strerror(errno));
  87. return -1;
  88. }
  89. if (fstat(fd, &sbuf) < 0) {
  90. fprintf(stderr, "%s: Can't stat %s: %s\n",
  91. params->cmdname, fname, strerror(errno));
  92. close(fd);
  93. return -1;
  94. }
  95. close(fd);
  96. return sbuf.st_size;
  97. }
  98. time_t imagetool_get_source_date(
  99. struct image_tool_params *params,
  100. time_t fallback)
  101. {
  102. char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  103. if (source_date_epoch == NULL)
  104. return fallback;
  105. time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
  106. if (gmtime(&time) == NULL) {
  107. fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",
  108. params->cmdname);
  109. time = 0;
  110. }
  111. return time;
  112. }