libufdt.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #ifndef LIBUFDT_H
  17. #define LIBUFDT_H
  18. #include "libufdt_sysdeps.h"
  19. #include "ufdt_types.h"
  20. /*
  21. * BEGIN of ufdt_node methods
  22. */
  23. /*
  24. * Allocates spaces for new ufdt_node who represents a fdt node at fdt_tag_ptr.
  25. * In order to get name pointer, it's necessary to give the pointer to the
  26. * entire fdt it belongs to.
  27. *
  28. *
  29. * @return: a pointer to the newly created ufdt_node or
  30. * NULL if dto_malloc failed
  31. */
  32. struct ufdt_node *ufdt_node_construct(void *fdtp, fdt32_t *fdt_tag_ptr,
  33. struct ufdt_node_pool *pool);
  34. /*
  35. * Frees all nodes in the subtree rooted at *node.
  36. */
  37. void ufdt_node_destruct(struct ufdt_node *node, struct ufdt_node_pool *pool);
  38. /*
  39. * Adds the child as a subnode of the parent.
  40. * It's been done by add entries in parent->prop_list or node_list depending on
  41. * the tag type of child.
  42. *
  43. * @return: 0 if success
  44. * < 0 otherwise
  45. *
  46. * @Time: O(1) w.h.p.
  47. */
  48. int ufdt_node_add_child(struct ufdt_node *parent, struct ufdt_node *child);
  49. /* BEGIN of FDT_PROP related functions .*/
  50. /*
  51. * Gets pointer to FDT_PROP subnode of node with name equals to name[0..len-1]
  52. *
  53. * @return: a pointer to the subnode or
  54. * NULL if no such subnode.
  55. *
  56. * @Time: O(len = length of name) w.h.p.
  57. */
  58. struct ufdt_node *ufdt_node_get_property_by_name_len(
  59. const struct ufdt_node *node, const char *name, int len);
  60. struct ufdt_node *ufdt_node_get_property_by_name(const struct ufdt_node *node,
  61. const char *name);
  62. /*
  63. * Gets the pointer to the FDT_PROP node's data in the corresponding fdt.
  64. * Also writes the length of such data to *out_len if out_len is not NULL.
  65. *
  66. * @return: a pointer to some data located in fdt or
  67. * NULL if *node is not a FDT_PROP
  68. */
  69. char *ufdt_node_get_fdt_prop_data(const struct ufdt_node *node, int *out_len);
  70. /*
  71. * Gets pointer to FDT_PROP node's data in fdt with name equals to
  72. * name[0..len-1], which is a subnode of *node.
  73. * It's actually a composition of ufdt_node_get_property_by_name and
  74. * ufdt_node_get_fdt_prop_data
  75. *
  76. * @return: a pointer to some data located in fdt or
  77. * NULL if no such subnode.
  78. *
  79. * @Time: O(len = length of name) w.h.p.
  80. */
  81. char *ufdt_node_get_fdt_prop_data_by_name_len(const struct ufdt_node *node,
  82. const char *name, int len,
  83. int *out_len);
  84. char *ufdt_node_get_fdt_prop_data_by_name(const struct ufdt_node *node,
  85. const char *name, int *out_len);
  86. /* END of FDT_PROP related functions .*/
  87. /*
  88. * Gets pointer to FDT_BEGIN_NODE subnode of node with name equals to
  89. * name[0..len-1].
  90. *
  91. * @return: a pointer to the subnode or
  92. * NULL if no such subnode.
  93. *
  94. * @Time: O(len = length of name) w.h.p.
  95. */
  96. struct ufdt_node *ufdt_node_get_subnode_by_name_len(const struct ufdt_node *node,
  97. const char *name, int len);
  98. struct ufdt_node *ufdt_node_get_subnode_by_name(const struct ufdt_node *node,
  99. const char *name);
  100. /*
  101. * Gets the pointer to FDT_NODE node whose relative path to *node is
  102. * path[0..len-1].
  103. * Note that the relative path doesn't support parent node like:
  104. * "../path/to/node".
  105. *
  106. * @return: a pointer to the node or
  107. * NULL if no such node.
  108. *
  109. * @Time: O(len = length of path) w.h.p.
  110. */
  111. struct ufdt_node *ufdt_node_get_node_by_path_len(const struct ufdt_node *node,
  112. const char *path, int len);
  113. struct ufdt_node *ufdt_node_get_node_by_path(const struct ufdt_node *node,
  114. const char *path);
  115. /*
  116. * Gets the phandle value of the node if it has.
  117. *
  118. * @return: phandle value of that node or
  119. * 0 if *node is not FDT_NODE or there's no "phandle"/"linux,phandle"
  120. * property.
  121. *
  122. * @Time: O(1) w.h.p.
  123. */
  124. uint32_t ufdt_node_get_phandle(const struct ufdt_node *node);
  125. /*
  126. * END of ufdt_node methods
  127. */
  128. /*
  129. * BEGIN of ufdt methods.
  130. */
  131. /*
  132. * Constructs a ufdt whose base fdt is fdtp.
  133. * Note that this function doesn't construct the entire tree.
  134. * To get the whole tree please call `ufdt_from_fdt(fdtp, fdt_size)`
  135. *
  136. * @return: an empty ufdt with base fdtp = fdtp
  137. */
  138. struct ufdt *ufdt_construct(void *fdtp, struct ufdt_node_pool *pool);
  139. /*
  140. * Frees the space occupied by the ufdt, including all ufdt_nodes
  141. * with ufdt_static_phandle_table.
  142. */
  143. void ufdt_destruct(struct ufdt *tree, struct ufdt_node_pool *pool);
  144. /*
  145. * Add a fdt into this ufdt.
  146. * Note that this function just add the given fdtp into this ufdt,
  147. * and doesn't create any node.
  148. *
  149. * @return: 0 if success.
  150. */
  151. int ufdt_add_fdt(struct ufdt *tree, void *fdtp);
  152. /*
  153. * Calculate the offset in the string tables of the given string.
  154. * All string tables will be concatenated in reversed order.
  155. *
  156. * @return: The offset is a negative number, base on the end position of
  157. * all concatenated string tables
  158. * Return 0 if not in any string table.
  159. */
  160. int ufdt_get_string_off(const struct ufdt *tree, const char *s);
  161. /*
  162. * Gets the pointer to the ufdt_node in tree with phandle = phandle.
  163. * The function do a binary search in tree->phandle_table.
  164. *
  165. * @return: a pointer to the target ufdt_node
  166. * NULL if no ufdt_node has phandle = phandle
  167. *
  168. * @Time: O(log(# of nodes in tree)) = O(log(size of underlying fdt))
  169. */
  170. struct ufdt_node *ufdt_get_node_by_phandle(struct ufdt *tree, uint32_t phandle);
  171. /*
  172. * Gets the pointer to the ufdt_node in tree with absolute path =
  173. * path[0..len-1].
  174. * Absolute path has form "/path/to/node" or "some_alias/to/node".
  175. * In later example, some_alias is a property in "/aliases" with data is a path
  176. * to some node X. Then the funcion will return node with relative
  177. * path = "to/node" w.r.t. X.
  178. *
  179. * @return: a pointer to the target ufdt_node or
  180. * NULL if such dnt doesn't exist.
  181. *
  182. * @Time: O(len = length of path) w.h.p.
  183. */
  184. struct ufdt_node *ufdt_get_node_by_path_len(struct ufdt *tree, const char *path,
  185. int len);
  186. struct ufdt_node *ufdt_get_node_by_path(struct ufdt *tree, const char *path);
  187. /*
  188. * Determines whether node->name equals to name[0..len-1]
  189. *
  190. * @return: true if they're equal.
  191. * false otherwise
  192. */
  193. bool ufdt_node_name_eq(const struct ufdt_node *node, const char *name, int len);
  194. /*
  195. * Merges tree_b into tree_a with tree_b has all nodes except root disappeared.
  196. * Overwrite property in tree_a if there's one with same name in tree_b.
  197. * Otherwise add the property to tree_a.
  198. * For subnodes with the same name, recursively run this function.
  199. *
  200. * Ex:
  201. * tree_a : ta {
  202. * b = "b";
  203. * c = "c";
  204. * d {
  205. * e = "g";
  206. * };
  207. * };
  208. *
  209. * tree_b : tb {
  210. * c = "C";
  211. * g = "G";
  212. * d {
  213. * da = "dad";
  214. * };
  215. * h {
  216. * hh = "HH";
  217. * };
  218. * };
  219. *
  220. * The resulting trees will be:
  221. *
  222. * tree_a : ta {
  223. * b = "b";
  224. * c = "C";
  225. * g = "G";
  226. * d {
  227. * da = "dad";
  228. * e = "g";
  229. * };
  230. * h {
  231. * hh = "HH";
  232. * };
  233. * };
  234. *
  235. * tree_b : tb {
  236. * };
  237. *
  238. *
  239. * @return: 0 if merge success
  240. * < 0 otherwise
  241. *
  242. * @Time: O(# of nodes in tree_b + total length of all names in tree_b) w.h.p.
  243. */
  244. int ufdt_node_merge_into(struct ufdt_node *node_a, struct ufdt_node *node_b,
  245. struct ufdt_node_pool *pool);
  246. /*
  247. * END of ufdt methods.
  248. */
  249. /*
  250. * BEGIN of ufdt output functions
  251. */
  252. /*
  253. * Builds the ufdt for FDT pointed by fdtp.
  254. *
  255. * @return: the ufdt T representing fdtp or
  256. * T with T.fdtp == NULL if fdtp is unvalid.
  257. *
  258. * @Time: O(fdt_size + nlogn) where n = # of nodes in fdt.
  259. */
  260. struct ufdt *ufdt_from_fdt(void *fdtp, size_t fdt_size,
  261. struct ufdt_node_pool *pool);
  262. /*
  263. * Sequentially dumps the whole ufdt to FDT buffer fdtp with buffer size
  264. * buf_size.
  265. *
  266. * Basically using functions provided by libfdt/fdt_sw.c.
  267. *
  268. * @return: 0 if successfully dump or
  269. * < 0 otherwise
  270. *
  271. * @Time: O(total length of all names + # of nodes in tree)
  272. */
  273. int ufdt_to_fdt(const struct ufdt *tree, void *buf, int buf_size);
  274. /*
  275. * prints the entire subtree rooted at *node in form:
  276. * NODE :[node name]:
  277. * PROP :[prop name]:
  278. * ...
  279. * NODE :[subnode1 name]:
  280. * ...
  281. * NODE :[subnode1 name]:
  282. * ...
  283. * ...
  284. * There're (depth * TAB_SIZE) spaces in front of each line.
  285. */
  286. void ufdt_node_print(const struct ufdt_node *node, int depth);
  287. /*
  288. * It's just ufdt_node_print(tree->root, 0).
  289. */
  290. void ufdt_print(struct ufdt *tree);
  291. /*
  292. * END of ufdt output functions
  293. */
  294. #endif /* LIBUFDT_H */