livetree.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. child->parent = parent;
  91. p = &parent->children;
  92. while (*p)
  93. p = &((*p)->next_sibling);
  94. *p = child;
  95. }
  96. struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size,
  97. char *label)
  98. {
  99. struct reserve_info *new = xmalloc(sizeof(*new));
  100. new->re.address = address;
  101. new->re.size = size;
  102. new->next = NULL;
  103. new->label = label;
  104. return new;
  105. }
  106. struct reserve_info *chain_reserve_entry(struct reserve_info *first,
  107. struct reserve_info *list)
  108. {
  109. assert(first->next == NULL);
  110. first->next = list;
  111. return first;
  112. }
  113. struct reserve_info *add_reserve_entry(struct reserve_info *list,
  114. struct reserve_info *new)
  115. {
  116. struct reserve_info *last;
  117. new->next = NULL;
  118. if (! list)
  119. return new;
  120. for (last = list; last->next; last = last->next)
  121. ;
  122. last->next = new;
  123. return list;
  124. }
  125. struct boot_info *build_boot_info(struct reserve_info *reservelist,
  126. struct node *tree, uint32_t boot_cpuid_phys)
  127. {
  128. struct boot_info *bi;
  129. bi = xmalloc(sizeof(*bi));
  130. bi->reservelist = reservelist;
  131. bi->dt = tree;
  132. bi->boot_cpuid_phys = boot_cpuid_phys;
  133. return bi;
  134. }
  135. /*
  136. * Tree accessor functions
  137. */
  138. const char *get_unitname(struct node *node)
  139. {
  140. if (node->name[node->basenamelen] == '\0')
  141. return "";
  142. else
  143. return node->name + node->basenamelen + 1;
  144. }
  145. struct property *get_property(struct node *node, const char *propname)
  146. {
  147. struct property *prop;
  148. for_each_property(node, prop)
  149. if (streq(prop->name, propname))
  150. return prop;
  151. return NULL;
  152. }
  153. cell_t propval_cell(struct property *prop)
  154. {
  155. assert(prop->val.len == sizeof(cell_t));
  156. return fdt32_to_cpu(*((cell_t *)prop->val.val));
  157. }
  158. struct node *get_subnode(struct node *node, const char *nodename)
  159. {
  160. struct node *child;
  161. for_each_child(node, child)
  162. if (streq(child->name, nodename))
  163. return child;
  164. return NULL;
  165. }
  166. struct node *get_node_by_path(struct node *tree, const char *path)
  167. {
  168. const char *p;
  169. struct node *child;
  170. if (!path || ! (*path))
  171. return tree;
  172. while (path[0] == '/')
  173. path++;
  174. p = strchr(path, '/');
  175. for_each_child(tree, child) {
  176. if (p && strneq(path, child->name, p-path))
  177. return get_node_by_path(child, p+1);
  178. else if (!p && streq(path, child->name))
  179. return child;
  180. }
  181. return NULL;
  182. }
  183. struct node *get_node_by_label(struct node *tree, const char *label)
  184. {
  185. struct node *child, *node;
  186. assert(label && (strlen(label) > 0));
  187. if (tree->label && streq(tree->label, label))
  188. return tree;
  189. for_each_child(tree, child) {
  190. node = get_node_by_label(child, label);
  191. if (node)
  192. return node;
  193. }
  194. return NULL;
  195. }
  196. struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
  197. {
  198. struct node *child, *node;
  199. assert((phandle != 0) && (phandle != -1));
  200. if (tree->phandle == phandle)
  201. return tree;
  202. for_each_child(tree, child) {
  203. node = get_node_by_phandle(child, phandle);
  204. if (node)
  205. return node;
  206. }
  207. return NULL;
  208. }
  209. struct node *get_node_by_ref(struct node *tree, const char *ref)
  210. {
  211. if (ref[0] == '/')
  212. return get_node_by_path(tree, ref);
  213. else
  214. return get_node_by_label(tree, ref);
  215. }
  216. cell_t get_node_phandle(struct node *root, struct node *node)
  217. {
  218. static cell_t phandle = 1; /* FIXME: ick, static local */
  219. if ((node->phandle != 0) && (node->phandle != -1))
  220. return node->phandle;
  221. assert(! get_property(node, "linux,phandle"));
  222. while (get_node_by_phandle(root, phandle))
  223. phandle++;
  224. node->phandle = phandle;
  225. add_property(node,
  226. build_property("linux,phandle",
  227. data_append_cell(empty_data, phandle),
  228. NULL));
  229. return node->phandle;
  230. }