fdt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. uint32_t _fdt_next_tag(const void *fdt, int offset, int *nextoffset)
  53. {
  54. const uint32_t *tagp, *lenp;
  55. uint32_t tag;
  56. const char *p;
  57. if (offset % FDT_TAGSIZE)
  58. return -1;
  59. tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
  60. if (! tagp)
  61. return FDT_END; /* premature end */
  62. tag = fdt32_to_cpu(*tagp);
  63. offset += FDT_TAGSIZE;
  64. switch (tag) {
  65. case FDT_BEGIN_NODE:
  66. /* skip name */
  67. do {
  68. p = fdt_offset_ptr(fdt, offset++, 1);
  69. } while (p && (*p != '\0'));
  70. if (! p)
  71. return FDT_END;
  72. break;
  73. case FDT_PROP:
  74. lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
  75. if (! lenp)
  76. return FDT_END;
  77. /* skip name offset, length and value */
  78. offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
  79. break;
  80. }
  81. if (nextoffset)
  82. *nextoffset = ALIGN(offset, FDT_TAGSIZE);
  83. return tag;
  84. }
  85. const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
  86. {
  87. int len = strlen(s) + 1;
  88. const char *last = strtab + tabsize - len;
  89. const char *p;
  90. for (p = strtab; p <= last; p++)
  91. if (memeq(p, s, len))
  92. return p;
  93. return NULL;
  94. }
  95. int fdt_move(const void *fdt, void *buf, int bufsize)
  96. {
  97. int err = _fdt_check_header(fdt);
  98. if (err)
  99. return err;
  100. if (fdt_totalsize(fdt) > bufsize)
  101. return -FDT_ERR_NOSPACE;
  102. memmove(buf, fdt, fdt_totalsize(fdt));
  103. return 0;
  104. }