Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

138 lines
2.7 KiB

  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. }
  99. time_t imagetool_get_source_date(
  100. struct image_tool_params *params,
  101. time_t fallback)
  102. {
  103. char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
  104. if (source_date_epoch == NULL)
  105. return fallback;
  106. time_t time = (time_t) strtol(source_date_epoch, NULL, 10);
  107. if (gmtime(&time) == NULL) {
  108. fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n",
  109. params->cmdname);
  110. time = 0;
  111. }
  112. return time;
  113. }