ufdt_node.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "libufdt.h"
  17. #include "ufdt_node_pool.h"
  18. struct ufdt_node *ufdt_node_construct(void *fdtp, fdt32_t *fdt_tag_ptr,
  19. struct ufdt_node_pool *pool) {
  20. void *buf = ufdt_node_pool_alloc(pool);
  21. uint32_t tag = fdt32_to_cpu(*fdt_tag_ptr);
  22. if (tag == FDT_PROP) {
  23. const struct fdt_property *prop = (const struct fdt_property *)fdt_tag_ptr;
  24. struct ufdt_node_fdt_prop *res = (struct ufdt_node_fdt_prop *)buf;
  25. if (res == NULL) return NULL;
  26. res->parent.fdt_tag_ptr = fdt_tag_ptr;
  27. res->parent.sibling = NULL;
  28. res->name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
  29. return (struct ufdt_node *)res;
  30. } else {
  31. struct ufdt_node_fdt_node *res = (struct ufdt_node_fdt_node *)buf;
  32. if (res == NULL) return NULL;
  33. res->parent.fdt_tag_ptr = fdt_tag_ptr;
  34. res->parent.sibling = NULL;
  35. res->child = NULL;
  36. res->last_child_p = &res->child;
  37. return (struct ufdt_node *)res;
  38. }
  39. }
  40. void ufdt_node_destruct(struct ufdt_node *node, struct ufdt_node_pool *pool) {
  41. if (node == NULL) return;
  42. if (ufdt_node_tag(node) == FDT_BEGIN_NODE) {
  43. struct ufdt_node *it = ((struct ufdt_node_fdt_node *)node)->child;
  44. while (it != NULL) {
  45. struct ufdt_node *next = it->sibling;
  46. ufdt_node_destruct(it, pool);
  47. it = next;
  48. }
  49. }
  50. ufdt_node_pool_free(pool, node);
  51. }
  52. int ufdt_node_add_child(struct ufdt_node *parent, struct ufdt_node *child) {
  53. if (!parent || !child) return -1;
  54. if (ufdt_node_tag(parent) != FDT_BEGIN_NODE) return -1;
  55. int err = 0;
  56. uint32_t child_tag = ufdt_node_tag(child);
  57. switch (child_tag) {
  58. case FDT_PROP:
  59. case FDT_BEGIN_NODE:
  60. // Append the child node to the last child of parant node
  61. *((struct ufdt_node_fdt_node *)parent)->last_child_p = child;
  62. ((struct ufdt_node_fdt_node *)parent)->last_child_p = &child->sibling;
  63. break;
  64. default:
  65. err = -1;
  66. dto_error("invalid children tag type\n");
  67. }
  68. return err;
  69. }
  70. /*
  71. * BEGIN of FDT_PROP related methods.
  72. */
  73. struct ufdt_node *ufdt_node_get_subnode_by_name_len(const struct ufdt_node *node,
  74. const char *name, int len) {
  75. struct ufdt_node **it = NULL;
  76. for_each_node(it, node) {
  77. if (ufdt_node_name_eq(*it, name, len)) return *it;
  78. }
  79. return NULL;
  80. }
  81. struct ufdt_node *ufdt_node_get_subnode_by_name(const struct ufdt_node *node,
  82. const char *name) {
  83. return ufdt_node_get_subnode_by_name_len(node, name, strlen(name));
  84. }
  85. struct ufdt_node *ufdt_node_get_property_by_name_len(
  86. const struct ufdt_node *node, const char *name, int len) {
  87. if (!node) return NULL;
  88. struct ufdt_node **it = NULL;
  89. for_each_prop(it, node) {
  90. if (ufdt_node_name_eq(*it, name, len)) return *it;
  91. }
  92. return NULL;
  93. }
  94. struct ufdt_node *ufdt_node_get_property_by_name(const struct ufdt_node *node,
  95. const char *name) {
  96. return ufdt_node_get_property_by_name_len(node, name, dto_strlen(name));
  97. }
  98. char *ufdt_node_get_fdt_prop_data(const struct ufdt_node *node, int *out_len) {
  99. if (!node || ufdt_node_tag(node) != FDT_PROP) {
  100. return NULL;
  101. }
  102. const struct fdt_property *prop = (struct fdt_property *)node->fdt_tag_ptr;
  103. if (out_len != NULL) {
  104. *out_len = fdt32_to_cpu(prop->len);
  105. }
  106. return (char *)prop->data;
  107. }
  108. char *ufdt_node_get_fdt_prop_data_by_name_len(const struct ufdt_node *node,
  109. const char *name, int len,
  110. int *out_len) {
  111. return ufdt_node_get_fdt_prop_data(
  112. ufdt_node_get_property_by_name_len(node, name, len), out_len);
  113. }
  114. char *ufdt_node_get_fdt_prop_data_by_name(const struct ufdt_node *node,
  115. const char *name, int *out_len) {
  116. return ufdt_node_get_fdt_prop_data(ufdt_node_get_property_by_name(node, name),
  117. out_len);
  118. }
  119. /*
  120. * END of FDT_PROP related methods.
  121. */
  122. /*
  123. * BEGIN of searching-in-ufdt_node methods.
  124. */
  125. uint32_t ufdt_node_get_phandle(const struct ufdt_node *node) {
  126. if (!node || ufdt_node_tag(node) != FDT_BEGIN_NODE) {
  127. return 0;
  128. }
  129. int len = 0;
  130. void *ptr = ufdt_node_get_fdt_prop_data_by_name(node, "phandle", &len);
  131. if (!ptr || len != sizeof(fdt32_t)) {
  132. ptr = ufdt_node_get_fdt_prop_data_by_name(node, "linux,phandle", &len);
  133. if (!ptr || len != sizeof(fdt32_t)) {
  134. return 0;
  135. }
  136. }
  137. return fdt32_to_cpu(*((fdt32_t *)ptr));
  138. }
  139. struct ufdt_node *ufdt_node_get_node_by_path_len(const struct ufdt_node *node,
  140. const char *path, int len) {
  141. const char *end = path + len;
  142. struct ufdt_node *cur = (struct ufdt_node *)node;
  143. while (path < end) {
  144. while (path[0] == '/') path++;
  145. if (path == end) return cur;
  146. const char *next_slash;
  147. next_slash = dto_memchr(path, '/', end - path);
  148. if (!next_slash) next_slash = end;
  149. struct ufdt_node *next = NULL;
  150. next = ufdt_node_get_subnode_by_name_len(cur, path, next_slash - path);
  151. cur = next;
  152. path = next_slash;
  153. if (!cur) return cur;
  154. }
  155. return cur;
  156. }
  157. struct ufdt_node *ufdt_node_get_node_by_path(const struct ufdt_node *node,
  158. const char *path) {
  159. return ufdt_node_get_node_by_path_len(node, path, dto_strlen(path));
  160. }
  161. bool ufdt_node_name_eq(const struct ufdt_node *node, const char *name, int len) {
  162. if (!node) return false;
  163. if (!name) return false;
  164. if (dto_strncmp(ufdt_node_name(node), name, len) != 0) return false;
  165. if (ufdt_node_name(node)[len] != '\0') return false;
  166. return true;
  167. }
  168. /*
  169. * END of searching-in-ufdt_node methods.
  170. */
  171. static int merge_children(struct ufdt_node *node_a, struct ufdt_node *node_b,
  172. struct ufdt_node_pool *pool) {
  173. int err = 0;
  174. struct ufdt_node *it;
  175. for (it = ((struct ufdt_node_fdt_node *)node_b)->child; it;) {
  176. struct ufdt_node *cur_node = it;
  177. it = it->sibling;
  178. cur_node->sibling = NULL;
  179. struct ufdt_node *target_node = NULL;
  180. if (ufdt_node_tag(cur_node) == FDT_BEGIN_NODE) {
  181. target_node =
  182. ufdt_node_get_subnode_by_name(node_a, ufdt_node_name(cur_node));
  183. } else {
  184. target_node =
  185. ufdt_node_get_property_by_name(node_a, ufdt_node_name(cur_node));
  186. }
  187. if (target_node == NULL) {
  188. err = ufdt_node_add_child(node_a, cur_node);
  189. } else {
  190. err = ufdt_node_merge_into(target_node, cur_node, pool);
  191. ufdt_node_pool_free(pool, cur_node);
  192. }
  193. if (err < 0) return -1;
  194. }
  195. /*
  196. * The ufdt_node* in node_b will be copied to node_a.
  197. * To prevent the ufdt_node from being freed twice
  198. * (main_tree and overlay_tree) at the end of function
  199. * ufdt_apply_overlay(), set this node in node_b
  200. * (overlay_tree) to NULL.
  201. */
  202. ((struct ufdt_node_fdt_node *)node_b)->child = NULL;
  203. return 0;
  204. }
  205. int ufdt_node_merge_into(struct ufdt_node *node_a, struct ufdt_node *node_b,
  206. struct ufdt_node_pool *pool) {
  207. if (ufdt_node_tag(node_a) == FDT_PROP) {
  208. node_a->fdt_tag_ptr = node_b->fdt_tag_ptr;
  209. return 0;
  210. }
  211. int err = 0;
  212. err = merge_children(node_a, node_b, pool);
  213. if (err < 0) return -1;
  214. return 0;
  215. }
  216. #define TAB_SIZE 2
  217. void ufdt_node_print(const struct ufdt_node *node, int depth) {
  218. if (!node) return;
  219. int i;
  220. for (i = 0; i < depth * TAB_SIZE; i++) dto_print(" ");
  221. uint32_t tag;
  222. tag = ufdt_node_tag(node);
  223. switch (tag) {
  224. case FDT_BEGIN_NODE:
  225. dto_print("NODE ");
  226. break;
  227. case FDT_PROP:
  228. dto_print("PROP ");
  229. break;
  230. default:
  231. dto_print("UNKNOWN ");
  232. break;
  233. }
  234. if (ufdt_node_name(node)) {
  235. dto_print(":%s:\n", ufdt_node_name(node));
  236. } else {
  237. dto_print("node name is NULL.\n");
  238. }
  239. if (ufdt_node_tag(node) == FDT_BEGIN_NODE) {
  240. struct ufdt_node **it;
  241. for_each_prop(it, node) ufdt_node_print(*it, depth + 1);
  242. for_each_node(it, node) ufdt_node_print(*it, depth + 1);
  243. }
  244. }