imagetool.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. if (!params->quiet)
  46. (*curr)->print_header(ptr);
  47. } else {
  48. fprintf(stderr,
  49. "%s: print_header undefined for %s\n",
  50. params->cmdname, (*curr)->name);
  51. }
  52. break;
  53. }
  54. }
  55. }
  56. return retval;
  57. }
  58. int imagetool_save_subimage(
  59. const char *file_name,
  60. ulong file_data,
  61. ulong file_len)
  62. {
  63. int dfd;
  64. dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
  65. S_IRUSR | S_IWUSR);
  66. if (dfd < 0) {
  67. fprintf(stderr, "Can't open \"%s\": %s\n",
  68. file_name, strerror(errno));
  69. return -1;
  70. }
  71. if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
  72. fprintf(stderr, "Write error on \"%s\": %s\n",
  73. file_name, strerror(errno));
  74. close(dfd);
  75. return -1;
  76. }
  77. close(dfd);
  78. return 0;
  79. }
  80. int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
  81. {
  82. struct stat sbuf;
  83. int fd;
  84. fd = open(fname, O_RDONLY | O_BINARY);
  85. if (fd < 0) {
  86. fprintf(stderr, "%s: Can't open %s: %s\n",
  87. params->cmdname, fname, strerror(errno));
  88. return -1;
  89. }
  90. if (fstat(fd, &sbuf) < 0) {
  91. fprintf(stderr, "%s: Can't stat %s: %s\n",
  92. params->cmdname, fname, strerror(errno));
  93. close(fd);
  94. return -1;
  95. }
  96. close(fd);
  97. return sbuf.st_size;
  98. }