fit_common.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. if (fdt_check_header(ptr) != EXIT_SUCCESS ||
  28. fit_check_format(ptr, IMAGE_SIZE_INVAL))
  29. return EXIT_FAILURE;
  30. return EXIT_SUCCESS;
  31. }
  32. int fit_check_image_types(uint8_t type)
  33. {
  34. if (type == IH_TYPE_FLATDT)
  35. return EXIT_SUCCESS;
  36. else
  37. return EXIT_FAILURE;
  38. }
  39. int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
  40. void **blobp, struct stat *sbuf, bool delete_on_error,
  41. bool read_only)
  42. {
  43. void *ptr;
  44. int fd;
  45. /* Load FIT blob into memory (we need to write hashes/signatures) */
  46. fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY);
  47. if (fd < 0) {
  48. fprintf(stderr, "%s: Can't open %s: %s\n",
  49. cmdname, fname, strerror(errno));
  50. goto err;
  51. }
  52. if (fstat(fd, sbuf) < 0) {
  53. fprintf(stderr, "%s: Can't stat %s: %s\n",
  54. cmdname, fname, strerror(errno));
  55. goto err;
  56. }
  57. if (size_inc) {
  58. sbuf->st_size += size_inc;
  59. if (ftruncate(fd, sbuf->st_size)) {
  60. fprintf(stderr, "%s: Can't expand %s: %s\n",
  61. cmdname, fname, strerror(errno));
  62. goto err;
  63. }
  64. }
  65. errno = 0;
  66. ptr = mmap(0, sbuf->st_size,
  67. (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED,
  68. fd, 0);
  69. if ((ptr == MAP_FAILED) || (errno != 0)) {
  70. fprintf(stderr, "%s: Can't read %s: %s\n",
  71. cmdname, fname, strerror(errno));
  72. goto err;
  73. }
  74. /* check if ptr has a valid blob */
  75. if (fdt_check_header(ptr)) {
  76. fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
  77. goto err;
  78. }
  79. /* expand if needed */
  80. if (size_inc) {
  81. int ret;
  82. ret = fdt_open_into(ptr, ptr, sbuf->st_size);
  83. if (ret) {
  84. fprintf(stderr, "%s: Cannot expand FDT: %s\n",
  85. cmdname, fdt_strerror(ret));
  86. goto err;
  87. }
  88. }
  89. *blobp = ptr;
  90. return fd;
  91. err:
  92. if (fd >= 0)
  93. close(fd);
  94. if (delete_on_error)
  95. unlink(fname);
  96. return -1;
  97. }