livetree.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. * USA
  19. */
  20. #include "dtc.h"
  21. /*
  22. * Tree building functions
  23. */
  24. struct property *build_property(char *name, struct data val, char *label)
  25. {
  26. struct property *new = xmalloc(sizeof(*new));
  27. new->name = name;
  28. new->val = val;
  29. new->next = NULL;
  30. new->label = label;
  31. return new;
  32. }
  33. struct property *chain_property(struct property *first, struct property *list)
  34. {
  35. assert(first->next == NULL);
  36. first->next = list;
  37. return first;
  38. }
  39. struct property *reverse_properties(struct property *first)
  40. {
  41. struct property *p = first;
  42. struct property *head = NULL;
  43. struct property *next;
  44. while (p) {
  45. next = p->next;
  46. p->next = head;
  47. head = p;
  48. p = next;
  49. }
  50. return head;
  51. }
  52. struct node *build_node(struct property *proplist, struct node *children)
  53. {
  54. struct node *new = xmalloc(sizeof(*new));
  55. struct node *child;
  56. memset(new, 0, sizeof(*new));
  57. new->proplist = reverse_properties(proplist);
  58. new->children = children;
  59. for_each_child(new, child) {
  60. child->parent = new;
  61. }
  62. return new;
  63. }
  64. struct node *name_node(struct node *node, char *name, char * label)
  65. {
  66. assert(node->name == NULL);
  67. node->name = name;
  68. node->label = label;
  69. return node;
  70. }
  71. struct node *chain_node(struct node *first, struct node *list)
  72. {
  73. assert(first->next_sibling == NULL);
  74. first->next_sibling = list;
  75. return first;
  76. }
  77. void add_property(struct node *node, struct property *prop)
  78. {
  79. struct property **p;
  80. prop->next = NULL;
  81. p = &node->proplist;
  82. while (*p)
  83. p = &((*p)->next);
  84. *p = prop;
  85. }
  86. void add_child(struct node *parent, struct node *child)
  87. {
  88. struct node **p;
  89. child->next_sibling = NULL;
  90. p = &parent->children;
  91. while (*p)
  92. p = &((*p)->next_sibling);
  93. *p = child;
  94. }
  95. struct reserve_info *build_reserve_entry(u64 address, u64 size, char *label)
  96. {
  97. struct reserve_info *new = xmalloc(sizeof(*new));
  98. new->re.address = address;
  99. new->re.size = size;
  100. new->next = NULL;
  101. new->label = label;
  102. return new;
  103. }
  104. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  105. struct reserve_info *list)
  106. {
  107. assert(first->next == NULL);
  108. first->next = list;
  109. return first;
  110. }
  111. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  112. struct reserve_info *new)
  113. {
  114. struct reserve_info *last;
  115. new->next = NULL;
  116. if (! list)
  117. return new;
  118. for (last = list; last->next; last = last->next)
  119. ;
  120. last->next = new;
  121. return list;
  122. }
  123. struct boot_info *build_boot_info(struct reserve_info *reservelist,
  124. struct node *tree)
  125. {
  126. struct boot_info *bi;
  127. bi = xmalloc(sizeof(*bi));
  128. bi->reservelist = reservelist;
  129. bi->dt = tree;
  130. return bi;
  131. }
  132. /*
  133. * Tree accessor functions
  134. */
  135. const char *get_unitname(struct node *node)
  136. {
  137. if (node->name[node->basenamelen] == '\0')
  138. return "";
  139. else
  140. return node->name + node->basenamelen + 1;
  141. }
  142. struct property *get_property(struct node *node, const char *propname)
  143. {
  144. struct property *prop;
  145. for_each_property(node, prop)
  146. if (streq(prop->name, propname))
  147. return prop;
  148. return NULL;
  149. }
  150. cell_t propval_cell(struct property *prop)
  151. {
  152. assert(prop->val.len == sizeof(cell_t));
  153. return be32_to_cpu(*((cell_t *)prop->val.val));
  154. }
  155. struct node *get_subnode(struct node *node, const char *nodename)
  156. {
  157. struct node *child;
  158. for_each_child(node, child)
  159. if (streq(child->name, nodename))
  160. return child;
  161. return NULL;
  162. }
  163. struct node *get_node_by_path(struct node *tree, const char *path)
  164. {
  165. const char *p;
  166. struct node *child;
  167. if (!path || ! (*path))
  168. return tree;
  169. while (path[0] == '/')
  170. path++;
  171. p = strchr(path, '/');
  172. for_each_child(tree, child) {
  173. if (p && strneq(path, child->name, p-path))
  174. return get_node_by_path(child, p+1);
  175. else if (!p && streq(path, child->name))
  176. return child;
  177. }
  178. return NULL;
  179. }
  180. struct node *get_node_by_label(struct node *tree, const char *label)
  181. {
  182. struct node *child, *node;
  183. assert(label && (strlen(label) > 0));
  184. if (tree->label && streq(tree->label, label))
  185. return tree;
  186. for_each_child(tree, child) {
  187. node = get_node_by_label(child, label);
  188. if (node)
  189. return node;
  190. }
  191. return NULL;
  192. }
  193. struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
  194. {
  195. struct node *child, *node;
  196. assert((phandle != 0) && (phandle != -1));
  197. if (tree->phandle == phandle)
  198. return tree;
  199. for_each_child(tree, child) {
  200. node = get_node_by_phandle(child, phandle);
  201. if (node)
  202. return node;
  203. }
  204. return NULL;
  205. }
  206. struct node *get_node_by_ref(struct node *tree, const char *ref)
  207. {
  208. if (ref[0] == '/')
  209. return get_node_by_path(tree, ref);
  210. else
  211. return get_node_by_label(tree, ref);
  212. }
  213. cell_t get_node_phandle(struct node *root, struct node *node)
  214. {
  215. static cell_t phandle = 1; /* FIXME: ick, static local */
  216. if ((node->phandle != 0) && (node->phandle != -1))
  217. return node->phandle;
  218. assert(! get_property(node, "linux,phandle"));
  219. while (get_node_by_phandle(root, phandle))
  220. phandle++;
  221. node->phandle = phandle;
  222. add_property(node,
  223. build_property("linux,phandle",
  224. data_append_cell(empty_data, phandle),
  225. NULL));
  226. return node->phandle;
  227. }