fdt.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "libfdt_env.h"
  20. #include <fdt.h>
  21. #include <libfdt.h>
  22. #include "libfdt_internal.h"
  23. int fdt_check_header(const void *fdt)
  24. {
  25. if (fdt_magic(fdt) == FDT_MAGIC) {
  26. /* Complete tree */
  27. if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
  28. return -FDT_ERR_BADVERSION;
  29. if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
  30. return -FDT_ERR_BADVERSION;
  31. } else if (fdt_magic(fdt) == SW_MAGIC) {
  32. /* Unfinished sequential-write blob */
  33. if (fdt_size_dt_struct(fdt) == 0)
  34. return -FDT_ERR_BADSTATE;
  35. } else {
  36. return -FDT_ERR_BADMAGIC;
  37. }
  38. return 0;
  39. }
  40. void *fdt_offset_ptr(const void *fdt, int offset, int len)
  41. {
  42. void *p;
  43. if (fdt_version(fdt) >= 0x11)
  44. if (((offset + len) < offset)
  45. || ((offset + len) > fdt_size_dt_struct(fdt)))
  46. return NULL;
  47. p = _fdt_offset_ptr(fdt, offset);
  48. if (p + len < p)
  49. return NULL;
  50. return p;
  51. }
  52. const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
  53. {
  54. int len = strlen(s) + 1;
  55. const char *last = strtab + tabsize - len;
  56. const char *p;
  57. for (p = strtab; p <= last; p++)
  58. if (memeq(p, s, len))
  59. return p;
  60. return NULL;
  61. }
  62. int fdt_move(const void *fdt, void *buf, int bufsize)
  63. {
  64. int err = fdt_check_header(fdt);
  65. if (err)
  66. return err;
  67. if (fdt_totalsize(fdt) > bufsize)
  68. return -FDT_ERR_NOSPACE;
  69. memmove(buf, fdt, fdt_totalsize(fdt));
  70. return 0;
  71. }