imagetool.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. struct image_type_params *start = ll_entry_start(
  14. struct image_type_params, image_type);
  15. struct image_type_params *end = ll_entry_end(
  16. struct image_type_params, image_type);
  17. for (curr = start; curr != end; curr++) {
  18. if (curr->check_image_type) {
  19. if (!curr->check_image_type(type))
  20. return curr;
  21. }
  22. }
  23. return NULL;
  24. }
  25. int imagetool_verify_print_header(
  26. void *ptr,
  27. struct stat *sbuf,
  28. struct image_type_params *tparams,
  29. struct image_tool_params *params)
  30. {
  31. int retval = -1;
  32. struct image_type_params *curr;
  33. struct image_type_params *start = ll_entry_start(
  34. struct image_type_params, image_type);
  35. struct image_type_params *end = ll_entry_end(
  36. struct image_type_params, image_type);
  37. for (curr = start; curr != end; curr++) {
  38. if (curr->verify_header) {
  39. retval = curr->verify_header((unsigned char *)ptr,
  40. sbuf->st_size, params);
  41. if (retval == 0) {
  42. /*
  43. * Print the image information if verify is
  44. * successful
  45. */
  46. if (curr->print_header) {
  47. curr->print_header(ptr);
  48. } else {
  49. fprintf(stderr,
  50. "%s: print_header undefined for %s\n",
  51. params->cmdname, curr->name);
  52. }
  53. break;
  54. }
  55. }
  56. }
  57. return retval;
  58. }
  59. int imagetool_save_subimage(
  60. const char *file_name,
  61. ulong file_data,
  62. ulong file_len)
  63. {
  64. int dfd;
  65. dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
  66. S_IRUSR | S_IWUSR);
  67. if (dfd < 0) {
  68. fprintf(stderr, "Can't open \"%s\": %s\n",
  69. file_name, strerror(errno));
  70. return -1;
  71. }
  72. if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
  73. fprintf(stderr, "Write error on \"%s\": %s\n",
  74. file_name, strerror(errno));
  75. close(dfd);
  76. return -1;
  77. }
  78. close(dfd);
  79. return 0;
  80. }