fit_common.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * (C) Copyright 2014
  3. * DENX Software Engineering
  4. * Heiko Schocher <hs@denx.de>
  5. *
  6. * (C) Copyright 2008 Semihalf
  7. *
  8. * (C) Copyright 2000-2004
  9. * DENX Software Engineering
  10. * Wolfgang Denk, wd@denx.de
  11. *
  12. * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
  13. * FIT image specific code abstracted from mkimage.c
  14. * some functions added to address abstraction
  15. *
  16. * All rights reserved.
  17. *
  18. * SPDX-License-Identifier: GPL-2.0+
  19. */
  20. #include "imagetool.h"
  21. #include "mkimage.h"
  22. #include "fit_common.h"
  23. #include <image.h>
  24. #include <u-boot/crc.h>
  25. int fit_verify_header(unsigned char *ptr, int image_size,
  26. struct image_tool_params *params)
  27. {
  28. return fdt_check_header(ptr);
  29. }
  30. int fit_check_image_types(uint8_t type)
  31. {
  32. if (type == IH_TYPE_FLATDT)
  33. return EXIT_SUCCESS;
  34. else
  35. return EXIT_FAILURE;
  36. }
  37. int mmap_fdt(char *cmdname, const char *fname, void **blobp,
  38. struct stat *sbuf, int useunlink)
  39. {
  40. void *ptr;
  41. int fd;
  42. /* Load FIT blob into memory (we need to write hashes/signatures) */
  43. fd = open(fname, O_RDWR | O_BINARY);
  44. if (fd < 0) {
  45. fprintf(stderr, "%s: Can't open %s: %s\n",
  46. cmdname, fname, strerror(errno));
  47. if (useunlink)
  48. unlink(fname);
  49. return -1;
  50. }
  51. if (fstat(fd, sbuf) < 0) {
  52. fprintf(stderr, "%s: Can't stat %s: %s\n",
  53. cmdname, fname, strerror(errno));
  54. if (useunlink)
  55. unlink(fname);
  56. return -1;
  57. }
  58. errno = 0;
  59. ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  60. if ((ptr == MAP_FAILED) || (errno != 0)) {
  61. fprintf(stderr, "%s: Can't read %s: %s\n",
  62. cmdname, fname, strerror(errno));
  63. if (useunlink)
  64. unlink(fname);
  65. return -1;
  66. }
  67. /* check if ptr has a valid blob */
  68. if (fdt_check_header(ptr)) {
  69. fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
  70. if (useunlink)
  71. unlink(fname);
  72. return -1;
  73. }
  74. *blobp = ptr;
  75. return fd;
  76. }