fit_common.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2014
  4. * DENX Software Engineering
  5. * Heiko Schocher <hs@denx.de>
  6. *
  7. * (C) Copyright 2008 Semihalf
  8. *
  9. * (C) Copyright 2000-2004
  10. * DENX Software Engineering
  11. * Wolfgang Denk, wd@denx.de
  12. *
  13. * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
  14. * FIT image specific code abstracted from mkimage.c
  15. * some functions added to address abstraction
  16. *
  17. * All rights reserved.
  18. */
  19. #include "imagetool.h"
  20. #include "mkimage.h"
  21. #include "fit_common.h"
  22. #include <image.h>
  23. #include <u-boot/crc.h>
  24. void fit_print_header(const void *fit, struct image_tool_params *params)
  25. {
  26. fit_print_contents(fit);
  27. }
  28. int fit_verify_header(unsigned char *ptr, int image_size,
  29. struct image_tool_params *params)
  30. {
  31. int ret;
  32. if (fdt_check_header(ptr) != EXIT_SUCCESS)
  33. return EXIT_FAILURE;
  34. ret = fit_check_format(ptr, IMAGE_SIZE_INVAL);
  35. if (ret) {
  36. if (ret != -EADDRNOTAVAIL)
  37. return EXIT_FAILURE;
  38. fprintf(stderr, "Image contains unit addresses @, this will break signing\n");
  39. }
  40. return EXIT_SUCCESS;
  41. }
  42. int fit_check_image_types(uint8_t type)
  43. {
  44. if (type == IH_TYPE_FLATDT)
  45. return EXIT_SUCCESS;
  46. else
  47. return EXIT_FAILURE;
  48. }
  49. int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
  50. void **blobp, struct stat *sbuf, bool delete_on_error,
  51. bool read_only)
  52. {
  53. void *ptr;
  54. int fd;
  55. /* Load FIT blob into memory (we need to write hashes/signatures) */
  56. fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY);
  57. if (fd < 0) {
  58. fprintf(stderr, "%s: Can't open %s: %s\n",
  59. cmdname, fname, strerror(errno));
  60. goto err;
  61. }
  62. if (fstat(fd, sbuf) < 0) {
  63. fprintf(stderr, "%s: Can't stat %s: %s\n",
  64. cmdname, fname, strerror(errno));
  65. goto err;
  66. }
  67. if (size_inc) {
  68. sbuf->st_size += size_inc;
  69. if (ftruncate(fd, sbuf->st_size)) {
  70. fprintf(stderr, "%s: Can't expand %s: %s\n",
  71. cmdname, fname, strerror(errno));
  72. goto err;
  73. }
  74. }
  75. errno = 0;
  76. ptr = mmap(0, sbuf->st_size,
  77. (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED,
  78. fd, 0);
  79. if ((ptr == MAP_FAILED) || (errno != 0)) {
  80. fprintf(stderr, "%s: Can't read %s: %s\n",
  81. cmdname, fname, strerror(errno));
  82. goto err;
  83. }
  84. /* check if ptr has a valid blob */
  85. if (fdt_check_header(ptr)) {
  86. fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
  87. goto err;
  88. }
  89. /* expand if needed */
  90. if (size_inc) {
  91. int ret;
  92. ret = fdt_open_into(ptr, ptr, sbuf->st_size);
  93. if (ret) {
  94. fprintf(stderr, "%s: Cannot expand FDT: %s\n",
  95. cmdname, fdt_strerror(ret));
  96. goto err;
  97. }
  98. }
  99. *blobp = ptr;
  100. return fd;
  101. err:
  102. if (fd >= 0)
  103. close(fd);
  104. if (delete_on_error)
  105. unlink(fname);
  106. return -1;
  107. }
  108. int copyfile(const char *src, const char *dst)
  109. {
  110. int fd_src = -1, fd_dst = -1;
  111. void *buf = NULL;
  112. ssize_t size;
  113. size_t count;
  114. int ret = -1;
  115. fd_src = open(src, O_RDONLY);
  116. if (fd_src < 0) {
  117. printf("Can't open file %s (%s)\n", src, strerror(errno));
  118. goto out;
  119. }
  120. fd_dst = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  121. if (fd_dst < 0) {
  122. printf("Can't open file %s (%s)\n", dst, strerror(errno));
  123. goto out;
  124. }
  125. buf = calloc(1, 512);
  126. if (!buf) {
  127. printf("Can't allocate buffer to copy file\n");
  128. goto out;
  129. }
  130. while (1) {
  131. size = read(fd_src, buf, 512);
  132. if (size < 0) {
  133. printf("Can't read file %s\n", src);
  134. goto out;
  135. }
  136. if (!size)
  137. break;
  138. count = size;
  139. size = write(fd_dst, buf, count);
  140. if (size < 0) {
  141. printf("Can't write file %s\n", dst);
  142. goto out;
  143. }
  144. }
  145. ret = 0;
  146. out:
  147. if (fd_src >= 0)
  148. close(fd_src);
  149. if (fd_dst >= 0)
  150. close(fd_dst);
  151. if (buf)
  152. free(buf);
  153. return ret;
  154. }
  155. void summary_show(struct image_summary *summary, const char *imagefile,
  156. const char *keydest)
  157. {
  158. if (summary->sig_offset) {
  159. printf("Signature written to '%s', node '%s'\n", imagefile,
  160. summary->sig_path);
  161. if (keydest) {
  162. printf("Public key written to '%s', node '%s'\n",
  163. keydest, summary->keydest_path);
  164. }
  165. }
  166. }