fit_common.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. int fit_verify_header(unsigned char *ptr, int image_size,
  25. struct image_tool_params *params)
  26. {
  27. int ret;
  28. if (fdt_check_header(ptr) != EXIT_SUCCESS)
  29. return EXIT_FAILURE;
  30. ret = fit_check_format(ptr, IMAGE_SIZE_INVAL);
  31. if (ret) {
  32. if (ret != -EADDRNOTAVAIL)
  33. return EXIT_FAILURE;
  34. fprintf(stderr, "Image contains unit addresses @, this will break signing\n");
  35. }
  36. return EXIT_SUCCESS;
  37. }
  38. int fit_check_image_types(uint8_t type)
  39. {
  40. if (type == IH_TYPE_FLATDT)
  41. return EXIT_SUCCESS;
  42. else
  43. return EXIT_FAILURE;
  44. }
  45. int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
  46. void **blobp, struct stat *sbuf, bool delete_on_error,
  47. bool read_only)
  48. {
  49. void *ptr;
  50. int fd;
  51. /* Load FIT blob into memory (we need to write hashes/signatures) */
  52. fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY);
  53. if (fd < 0) {
  54. fprintf(stderr, "%s: Can't open %s: %s\n",
  55. cmdname, fname, strerror(errno));
  56. goto err;
  57. }
  58. if (fstat(fd, sbuf) < 0) {
  59. fprintf(stderr, "%s: Can't stat %s: %s\n",
  60. cmdname, fname, strerror(errno));
  61. goto err;
  62. }
  63. if (size_inc) {
  64. sbuf->st_size += size_inc;
  65. if (ftruncate(fd, sbuf->st_size)) {
  66. fprintf(stderr, "%s: Can't expand %s: %s\n",
  67. cmdname, fname, strerror(errno));
  68. goto err;
  69. }
  70. }
  71. errno = 0;
  72. ptr = mmap(0, sbuf->st_size,
  73. (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED,
  74. fd, 0);
  75. if ((ptr == MAP_FAILED) || (errno != 0)) {
  76. fprintf(stderr, "%s: Can't read %s: %s\n",
  77. cmdname, fname, strerror(errno));
  78. goto err;
  79. }
  80. /* check if ptr has a valid blob */
  81. if (fdt_check_header(ptr)) {
  82. fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
  83. goto err;
  84. }
  85. /* expand if needed */
  86. if (size_inc) {
  87. int ret;
  88. ret = fdt_open_into(ptr, ptr, sbuf->st_size);
  89. if (ret) {
  90. fprintf(stderr, "%s: Cannot expand FDT: %s\n",
  91. cmdname, fdt_strerror(ret));
  92. goto err;
  93. }
  94. }
  95. *blobp = ptr;
  96. return fd;
  97. err:
  98. if (fd >= 0)
  99. close(fd);
  100. if (delete_on_error)
  101. unlink(fname);
  102. return -1;
  103. }
  104. int copyfile(const char *src, const char *dst)
  105. {
  106. int fd_src = -1, fd_dst = -1;
  107. void *buf = NULL;
  108. ssize_t size;
  109. size_t count;
  110. int ret = -1;
  111. fd_src = open(src, O_RDONLY);
  112. if (fd_src < 0) {
  113. printf("Can't open file %s (%s)\n", src, strerror(errno));
  114. goto out;
  115. }
  116. fd_dst = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  117. if (fd_dst < 0) {
  118. printf("Can't open file %s (%s)\n", dst, strerror(errno));
  119. goto out;
  120. }
  121. buf = calloc(1, 512);
  122. if (!buf) {
  123. printf("Can't allocate buffer to copy file\n");
  124. goto out;
  125. }
  126. while (1) {
  127. size = read(fd_src, buf, 512);
  128. if (size < 0) {
  129. printf("Can't read file %s\n", src);
  130. goto out;
  131. }
  132. if (!size)
  133. break;
  134. count = size;
  135. size = write(fd_dst, buf, count);
  136. if (size < 0) {
  137. printf("Can't write file %s\n", dst);
  138. goto out;
  139. }
  140. }
  141. ret = 0;
  142. out:
  143. if (fd_src >= 0)
  144. close(fd_src);
  145. if (fd_dst >= 0)
  146. close(fd_dst);
  147. if (buf)
  148. free(buf);
  149. return ret;
  150. }
  151. void summary_show(struct image_summary *summary, const char *imagefile,
  152. const char *keydest)
  153. {
  154. if (summary->sig_offset) {
  155. printf("Signature written to '%s', node '%s'\n", imagefile,
  156. summary->sig_path);
  157. if (keydest) {
  158. printf("Public key written to '%s', node '%s'\n",
  159. keydest, summary->keydest_path);
  160. }
  161. }
  162. }