fit_common.c 2.3 KB

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