fdt_wip.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * libfdt - Flat Device Tree manipulation
  3. * Copyright (C) 2006 David Gibson, IBM Corporation.
  4. * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
  5. */
  6. #include "libfdt_env.h"
  7. #ifndef USE_HOSTCC
  8. #include <fdt.h>
  9. #include <libfdt.h>
  10. #else
  11. #include "fdt_host.h"
  12. #endif
  13. #include "libfdt_internal.h"
  14. int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
  15. const void *val, int len)
  16. {
  17. void *propval;
  18. int proplen;
  19. propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen);
  20. if (! propval)
  21. return proplen;
  22. if (proplen != len)
  23. return -FDT_ERR_NOSPACE;
  24. memcpy(propval, val, len);
  25. return 0;
  26. }
  27. static void _fdt_nop_region(void *start, int len)
  28. {
  29. fdt32_t *p;
  30. for (p = start; (char *)p < ((char *)start + len); p++)
  31. *p = cpu_to_fdt32(FDT_NOP);
  32. }
  33. int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
  34. {
  35. struct fdt_property *prop;
  36. int len;
  37. prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
  38. if (! prop)
  39. return len;
  40. _fdt_nop_region(prop, len + sizeof(*prop));
  41. return 0;
  42. }
  43. int _fdt_node_end_offset(void *fdt, int offset)
  44. {
  45. int depth = 0;
  46. while ((offset >= 0) && (depth >= 0))
  47. offset = fdt_next_node(fdt, offset, &depth);
  48. return offset;
  49. }
  50. int fdt_nop_node(void *fdt, int nodeoffset)
  51. {
  52. int endoffset;
  53. endoffset = _fdt_node_end_offset(fdt, nodeoffset);
  54. if (endoffset < 0)
  55. return endoffset;
  56. _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
  57. endoffset - nodeoffset);
  58. return 0;
  59. }
  60. #define FDT_MAX_DEPTH 32
  61. static int str_in_list(const char *str, char * const list[], int count)
  62. {
  63. int i;
  64. for (i = 0; i < count; i++)
  65. if (!strcmp(list[i], str))
  66. return 1;
  67. return 0;
  68. }
  69. int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
  70. char * const exc_prop[], int exc_prop_count,
  71. struct fdt_region region[], int max_regions,
  72. char *path, int path_len, int add_string_tab)
  73. {
  74. int stack[FDT_MAX_DEPTH];
  75. char *end;
  76. int nextoffset = 0;
  77. uint32_t tag;
  78. int count = 0;
  79. int start = -1;
  80. int depth = -1;
  81. int want = 0;
  82. int base = fdt_off_dt_struct(fdt);
  83. end = path;
  84. *end = '\0';
  85. do {
  86. const struct fdt_property *prop;
  87. const char *name;
  88. const char *str;
  89. int include = 0;
  90. int stop_at = 0;
  91. int offset;
  92. int len;
  93. offset = nextoffset;
  94. tag = fdt_next_tag(fdt, offset, &nextoffset);
  95. stop_at = nextoffset;
  96. switch (tag) {
  97. case FDT_PROP:
  98. include = want >= 2;
  99. stop_at = offset;
  100. prop = fdt_get_property_by_offset(fdt, offset, NULL);
  101. str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  102. if (str_in_list(str, exc_prop, exc_prop_count))
  103. include = 0;
  104. break;
  105. case FDT_NOP:
  106. include = want >= 2;
  107. stop_at = offset;
  108. break;
  109. case FDT_BEGIN_NODE:
  110. depth++;
  111. if (depth == FDT_MAX_DEPTH)
  112. return -FDT_ERR_BADSTRUCTURE;
  113. name = fdt_get_name(fdt, offset, &len);
  114. if (end - path + 2 + len >= path_len)
  115. return -FDT_ERR_NOSPACE;
  116. if (end != path + 1)
  117. *end++ = '/';
  118. strcpy(end, name);
  119. end += len;
  120. stack[depth] = want;
  121. if (want == 1)
  122. stop_at = offset;
  123. if (str_in_list(path, inc, inc_count))
  124. want = 2;
  125. else if (want)
  126. want--;
  127. else
  128. stop_at = offset;
  129. include = want;
  130. break;
  131. case FDT_END_NODE:
  132. include = want;
  133. want = stack[depth--];
  134. while (end > path && *--end != '/')
  135. ;
  136. *end = '\0';
  137. break;
  138. case FDT_END:
  139. include = 1;
  140. break;
  141. }
  142. if (include && start == -1) {
  143. /* Should we merge with previous? */
  144. if (count && count <= max_regions &&
  145. offset == region[count - 1].offset +
  146. region[count - 1].size - base)
  147. start = region[--count].offset - base;
  148. else
  149. start = offset;
  150. }
  151. if (!include && start != -1) {
  152. if (count < max_regions) {
  153. region[count].offset = base + start;
  154. region[count].size = stop_at - start;
  155. }
  156. count++;
  157. start = -1;
  158. }
  159. } while (tag != FDT_END);
  160. if (nextoffset != fdt_size_dt_struct(fdt))
  161. return -FDT_ERR_BADLAYOUT;
  162. /* Add a region for the END tag and the string table */
  163. if (count < max_regions) {
  164. region[count].offset = base + start;
  165. region[count].size = nextoffset - start;
  166. if (add_string_tab)
  167. region[count].size += fdt_size_dt_strings(fdt);
  168. }
  169. count++;
  170. return count;
  171. }