fdt.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * libfdt - Flat Device Tree manipulation
  3. * Copyright (C) 2006 David Gibson, IBM Corporation.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 2.1 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "config.h"
  20. #if CONFIG_OF_LIBFDT
  21. #include "libfdt_env.h"
  22. #include <fdt.h>
  23. #include <libfdt.h>
  24. #include "libfdt_internal.h"
  25. int fdt_check_header(const void *fdt)
  26. {
  27. if (fdt_magic(fdt) == FDT_MAGIC) {
  28. /* Complete tree */
  29. if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
  30. return -FDT_ERR_BADVERSION;
  31. if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
  32. return -FDT_ERR_BADVERSION;
  33. } else if (fdt_magic(fdt) == SW_MAGIC) {
  34. /* Unfinished sequential-write blob */
  35. if (fdt_size_dt_struct(fdt) == 0)
  36. return -FDT_ERR_BADSTATE;
  37. } else {
  38. return -FDT_ERR_BADMAGIC;
  39. }
  40. return 0;
  41. }
  42. void *fdt_offset_ptr(const void *fdt, int offset, int len)
  43. {
  44. void *p;
  45. if (fdt_version(fdt) >= 0x11)
  46. if (((offset + len) < offset)
  47. || ((offset + len) > fdt_size_dt_struct(fdt)))
  48. return NULL;
  49. p = _fdt_offset_ptr(fdt, offset);
  50. if (p + len < p)
  51. return NULL;
  52. return p;
  53. }
  54. const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
  55. {
  56. int len = strlen(s) + 1;
  57. const char *last = strtab + tabsize - len;
  58. const char *p;
  59. for (p = strtab; p <= last; p++)
  60. if (memeq(p, s, len))
  61. return p;
  62. return NULL;
  63. }
  64. int fdt_move(const void *fdt, void *buf, int bufsize)
  65. {
  66. int err = fdt_check_header(fdt);
  67. if (err)
  68. return err;
  69. if (fdt_totalsize(fdt) > bufsize)
  70. return -FDT_ERR_NOSPACE;
  71. memmove(buf, fdt, fdt_totalsize(fdt));
  72. return 0;
  73. }
  74. #endif /* CONFIG_OF_LIBFDT */