ufdt_node.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. int node_cmp(const void *a, const void *b) {
  18. const struct ufdt_node *na = *(struct ufdt_node **)a;
  19. const struct ufdt_node *nb = *(struct ufdt_node **)b;
  20. return dto_strcmp(name_of(na), name_of(nb));
  21. }
  22. bool node_name_eq(const struct ufdt_node *node, const char *name, int len) {
  23. if (!node) return false;
  24. if (!name) return false;
  25. if (dto_strncmp(name_of(node), name, len) != 0) return false;
  26. if (name_of(node)[len] != '\0') return false;
  27. return true;
  28. }
  29. /*
  30. * ufdt_node methods.
  31. */
  32. struct ufdt_node *ufdt_node_construct(void *fdtp, fdt32_t *fdt_tag_ptr) {
  33. uint32_t tag = fdt32_to_cpu(*fdt_tag_ptr);
  34. if (tag == FDT_PROP) {
  35. const struct fdt_property *prop = (const struct fdt_property *)fdt_tag_ptr;
  36. struct fdt_prop_ufdt_node *res = dto_malloc(sizeof(struct fdt_prop_ufdt_node));
  37. if (res == NULL) return NULL;
  38. res->parent.fdt_tag_ptr = fdt_tag_ptr;
  39. res->parent.sibling = NULL;
  40. res->name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
  41. return (struct ufdt_node *)res;
  42. } else {
  43. struct fdt_node_ufdt_node *res = dto_malloc(sizeof(struct fdt_node_ufdt_node));
  44. if (res == NULL) return NULL;
  45. res->parent.fdt_tag_ptr = fdt_tag_ptr;
  46. res->parent.sibling = NULL;
  47. res->child = NULL;
  48. res->last_child_p = &res->child;
  49. return (struct ufdt_node *)res;
  50. }
  51. }
  52. void ufdt_node_destruct(struct ufdt_node *node) {
  53. if (node == NULL) return;
  54. if (tag_of(node) == FDT_BEGIN_NODE) {
  55. struct ufdt_node *it = ((struct fdt_node_ufdt_node *)node)->child;
  56. while (it != NULL) {
  57. struct ufdt_node *next = it->sibling;
  58. ufdt_node_destruct(it);
  59. it = next;
  60. }
  61. }
  62. dto_free(node);
  63. return;
  64. }
  65. int ufdt_node_add_child(struct ufdt_node *parent, struct ufdt_node *child) {
  66. if (!parent || !child) return -1;
  67. if (tag_of(parent) != FDT_BEGIN_NODE) return -1;
  68. int err = 0;
  69. uint32_t child_tag = tag_of(child);
  70. switch (child_tag) {
  71. case FDT_PROP:
  72. case FDT_BEGIN_NODE:
  73. // Append the child node to the last child of parant node
  74. *((struct fdt_node_ufdt_node *)parent)->last_child_p = child;
  75. ((struct fdt_node_ufdt_node *)parent)->last_child_p = &child->sibling;
  76. break;
  77. default:
  78. err = -1;
  79. dto_error("invalid children tag type\n");
  80. }
  81. return err;
  82. }
  83. /*
  84. * BEGIN of FDT_PROP related methods.
  85. */
  86. struct ufdt_node *ufdt_node_get_subnode_by_name_len(const struct ufdt_node *node,
  87. const char *name, int len) {
  88. struct ufdt_node **it = NULL;
  89. for_each_node(it, node) {
  90. if (node_name_eq(*it, name, len)) return *it;
  91. }
  92. return NULL;
  93. }
  94. struct ufdt_node *ufdt_node_get_subnode_by_name(const struct ufdt_node *node,
  95. const char *name) {
  96. return ufdt_node_get_subnode_by_name_len(node, name, strlen(name));
  97. }
  98. struct ufdt_node *ufdt_node_get_property_by_name_len(
  99. const struct ufdt_node *node, const char *name, int len) {
  100. if (!node) return NULL;
  101. struct ufdt_node **it = NULL;
  102. for_each_prop(it, node) {
  103. if (node_name_eq(*it, name, len)) return *it;
  104. }
  105. return NULL;
  106. }
  107. struct ufdt_node *ufdt_node_get_property_by_name(const struct ufdt_node *node,
  108. const char *name) {
  109. return ufdt_node_get_property_by_name_len(node, name, dto_strlen(name));
  110. }
  111. char *ufdt_node_get_fdt_prop_data(const struct ufdt_node *node, int *out_len) {
  112. if (!node || tag_of(node) != FDT_PROP) {
  113. return NULL;
  114. }
  115. const struct fdt_property *prop = (struct fdt_property *)node->fdt_tag_ptr;
  116. if (out_len != NULL) {
  117. *out_len = fdt32_to_cpu(prop->len);
  118. }
  119. return (char *)prop->data;
  120. }
  121. char *ufdt_node_get_fdt_prop_data_by_name_len(const struct ufdt_node *node,
  122. const char *name, int len,
  123. int *out_len) {
  124. return ufdt_node_get_fdt_prop_data(
  125. ufdt_node_get_property_by_name_len(node, name, len), out_len);
  126. }
  127. char *ufdt_node_get_fdt_prop_data_by_name(const struct ufdt_node *node,
  128. const char *name, int *out_len) {
  129. return ufdt_node_get_fdt_prop_data(ufdt_node_get_property_by_name(node, name),
  130. out_len);
  131. }
  132. /*
  133. * END of FDT_PROP related methods.
  134. */
  135. /*
  136. * BEGIN of searching-in-ufdt_node methods.
  137. */
  138. uint32_t ufdt_node_get_phandle(const struct ufdt_node *node) {
  139. if (!node || tag_of(node) != FDT_BEGIN_NODE) {
  140. return 0;
  141. }
  142. int len = 0;
  143. void *ptr = ufdt_node_get_fdt_prop_data_by_name(node, "phandle", &len);
  144. if (!ptr || len != sizeof(fdt32_t)) {
  145. ptr = ufdt_node_get_fdt_prop_data_by_name(node, "linux,phandle", &len);
  146. if (!ptr || len != sizeof(fdt32_t)) {
  147. return 0;
  148. }
  149. }
  150. return fdt32_to_cpu(*((fdt32_t *)ptr));
  151. }
  152. struct ufdt_node *ufdt_node_get_node_by_path_len(const struct ufdt_node *node,
  153. const char *path, int len) {
  154. const char *end = path + len;
  155. struct ufdt_node *cur = (struct ufdt_node *)node;
  156. while (path < end) {
  157. while (path[0] == '/') path++;
  158. if (path == end) return cur;
  159. const char *next_slash;
  160. next_slash = dto_memchr(path, '/', end - path);
  161. if (!next_slash) next_slash = end;
  162. struct ufdt_node *next = NULL;
  163. next = ufdt_node_get_subnode_by_name_len(cur, path, next_slash - path);
  164. cur = next;
  165. path = next_slash;
  166. if (!cur) return cur;
  167. }
  168. return cur;
  169. }
  170. struct ufdt_node *ufdt_node_get_node_by_path(const struct ufdt_node *node,
  171. const char *path) {
  172. return ufdt_node_get_node_by_path_len(node, path, dto_strlen(path));
  173. }
  174. /*
  175. * END of searching-in-ufdt_node methods.
  176. */
  177. #define TAB_SIZE 2
  178. void ufdt_node_print(const struct ufdt_node *node, int depth) {
  179. if (!node) return;
  180. int i;
  181. for (i = 0; i < depth * TAB_SIZE; i++) dto_print(" ");
  182. uint32_t tag;
  183. tag = tag_of(node);
  184. switch (tag) {
  185. case FDT_BEGIN_NODE:
  186. dto_print("NODE ");
  187. break;
  188. case FDT_PROP:
  189. dto_print("PROP ");
  190. break;
  191. default:
  192. dto_print("UNKNOWN ");
  193. break;
  194. }
  195. if (name_of(node)) {
  196. dto_print(":%s:\n", name_of(node));
  197. } else {
  198. dto_print("node name is NULL.\n");
  199. }
  200. if (tag_of(node) == FDT_BEGIN_NODE) {
  201. struct ufdt_node **it;
  202. for_each_prop(it, node) ufdt_node_print(*it, depth + 1);
  203. for_each_node(it, node) ufdt_node_print(*it, depth + 1);
  204. }
  205. return;
  206. }
  207. void ufdt_node_map(struct ufdt_node *node, struct ufdt_node_closure closure) {
  208. if (node == NULL) return;
  209. closure.func(node, closure.env);
  210. if (tag_of(node) == FDT_BEGIN_NODE) {
  211. struct ufdt_node **it;
  212. for_each_prop(it, node) ufdt_node_map(*it, closure);
  213. for_each_node(it, node) ufdt_node_map(*it, closure);
  214. }
  215. return;
  216. }