libufdt.h 9.9 KB

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