2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
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.
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.
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
24 * Tree building functions
27 void add_label(struct label **labels, char *label)
31 /* Make sure the label isn't already there */
32 for_each_label_withdel(*labels, new)
33 if (streq(new->label, label)) {
38 new = xmalloc(sizeof(*new));
39 memset(new, 0, sizeof(*new));
45 void delete_labels(struct label **labels)
49 for_each_label(*labels, label)
53 struct property *build_property(char *name, struct data val)
55 struct property *new = xmalloc(sizeof(*new));
57 memset(new, 0, sizeof(*new));
65 struct property *build_property_delete(char *name)
67 struct property *new = xmalloc(sizeof(*new));
69 memset(new, 0, sizeof(*new));
77 struct property *chain_property(struct property *first, struct property *list)
79 assert(first->next == NULL);
85 struct property *reverse_properties(struct property *first)
87 struct property *p = first;
88 struct property *head = NULL;
89 struct property *next;
100 struct node *build_node(struct property *proplist, struct node *children)
102 struct node *new = xmalloc(sizeof(*new));
105 memset(new, 0, sizeof(*new));
107 new->proplist = reverse_properties(proplist);
108 new->children = children;
110 for_each_child(new, child) {
117 struct node *build_node_delete(void)
119 struct node *new = xmalloc(sizeof(*new));
121 memset(new, 0, sizeof(*new));
128 struct node *name_node(struct node *node, char *name)
130 assert(node->name == NULL);
137 struct node *omit_node_if_unused(struct node *node)
139 node->omit_if_unused = 1;
144 struct node *reference_node(struct node *node)
146 node->is_referenced = 1;
151 struct node *merge_nodes(struct node *old_node, struct node *new_node)
153 struct property *new_prop, *old_prop;
154 struct node *new_child, *old_child;
157 old_node->deleted = 0;
159 /* Add new node labels to old node */
160 for_each_label_withdel(new_node->labels, l)
161 add_label(&old_node->labels, l->label);
163 /* Move properties from the new node to the old node. If there
164 * is a collision, replace the old value with the new */
165 while (new_node->proplist) {
166 /* Pop the property off the list */
167 new_prop = new_node->proplist;
168 new_node->proplist = new_prop->next;
169 new_prop->next = NULL;
171 if (new_prop->deleted) {
172 delete_property_by_name(old_node, new_prop->name);
177 /* Look for a collision, set new value if there is */
178 for_each_property_withdel(old_node, old_prop) {
179 if (streq(old_prop->name, new_prop->name)) {
180 /* Add new labels to old property */
181 for_each_label_withdel(new_prop->labels, l)
182 add_label(&old_prop->labels, l->label);
184 old_prop->val = new_prop->val;
185 old_prop->deleted = 0;
192 /* if no collision occurred, add property to the old node. */
194 add_property(old_node, new_prop);
197 /* Move the override child nodes into the primary node. If
198 * there is a collision, then merge the nodes. */
199 while (new_node->children) {
200 /* Pop the child node off the list */
201 new_child = new_node->children;
202 new_node->children = new_child->next_sibling;
203 new_child->parent = NULL;
204 new_child->next_sibling = NULL;
206 if (new_child->deleted) {
207 delete_node_by_name(old_node, new_child->name);
212 /* Search for a collision. Merge if there is */
213 for_each_child_withdel(old_node, old_child) {
214 if (streq(old_child->name, new_child->name)) {
215 merge_nodes(old_child, new_child);
221 /* if no collision occurred, add child to the old node. */
223 add_child(old_node, new_child);
226 /* The new node contents are now merged into the old node. Free
233 struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
235 static unsigned int next_orphan_fragment = 0;
238 struct data d = empty_data;
242 d = data_append_data(d, ref, strlen(ref) + 1);
244 p = build_property("target-path", d);
246 d = data_add_marker(d, REF_PHANDLE, ref);
247 d = data_append_integer(d, 0xffffffff, 32);
249 p = build_property("target", d);
252 xasprintf(&name, "fragment@%u",
253 next_orphan_fragment++);
254 name_node(new_node, "__overlay__");
255 node = build_node(p, new_node);
256 name_node(node, name);
262 struct node *chain_node(struct node *first, struct node *list)
264 assert(first->next_sibling == NULL);
266 first->next_sibling = list;
270 void add_property(struct node *node, struct property *prop)
283 void delete_property_by_name(struct node *node, char *name)
285 struct property *prop = node->proplist;
288 if (streq(prop->name, name)) {
289 delete_property(prop);
296 void delete_property(struct property *prop)
299 delete_labels(&prop->labels);
302 void add_child(struct node *parent, struct node *child)
306 child->next_sibling = NULL;
307 child->parent = parent;
309 p = &parent->children;
311 p = &((*p)->next_sibling);
316 void delete_node_by_name(struct node *parent, char *name)
318 struct node *node = parent->children;
321 if (streq(node->name, name)) {
325 node = node->next_sibling;
329 void delete_node(struct node *node)
331 struct property *prop;
335 for_each_child(node, child)
337 for_each_property(node, prop)
338 delete_property(prop);
339 delete_labels(&node->labels);
342 void append_to_property(struct node *node,
343 char *name, const void *data, int len)
348 p = get_property(node, name);
350 d = data_append_data(p->val, data, len);
353 d = data_append_data(empty_data, data, len);
354 p = build_property(name, d);
355 add_property(node, p);
359 struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)
361 struct reserve_info *new = xmalloc(sizeof(*new));
363 memset(new, 0, sizeof(*new));
365 new->address = address;
371 struct reserve_info *chain_reserve_entry(struct reserve_info *first,
372 struct reserve_info *list)
374 assert(first->next == NULL);
380 struct reserve_info *add_reserve_entry(struct reserve_info *list,
381 struct reserve_info *new)
383 struct reserve_info *last;
390 for (last = list; last->next; last = last->next)
398 struct dt_info *build_dt_info(unsigned int dtsflags,
399 struct reserve_info *reservelist,
400 struct node *tree, uint32_t boot_cpuid_phys)
404 dti = xmalloc(sizeof(*dti));
405 dti->dtsflags = dtsflags;
406 dti->reservelist = reservelist;
408 dti->boot_cpuid_phys = boot_cpuid_phys;
414 * Tree accessor functions
417 const char *get_unitname(struct node *node)
419 if (node->name[node->basenamelen] == '\0')
422 return node->name + node->basenamelen + 1;
425 struct property *get_property(struct node *node, const char *propname)
427 struct property *prop;
429 for_each_property(node, prop)
430 if (streq(prop->name, propname))
436 cell_t propval_cell(struct property *prop)
438 assert(prop->val.len == sizeof(cell_t));
439 return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
442 cell_t propval_cell_n(struct property *prop, int n)
444 assert(prop->val.len / sizeof(cell_t) >= n);
445 return fdt32_to_cpu(*((fdt32_t *)prop->val.val + n));
448 struct property *get_property_by_label(struct node *tree, const char *label,
451 struct property *prop;
456 for_each_property(tree, prop) {
459 for_each_label(prop->labels, l)
460 if (streq(l->label, label))
464 for_each_child(tree, c) {
465 prop = get_property_by_label(c, label, node);
474 struct marker *get_marker_label(struct node *tree, const char *label,
475 struct node **node, struct property **prop)
483 for_each_property(tree, p) {
486 for_each_marker_of_type(m, LABEL)
487 if (streq(m->ref, label))
491 for_each_child(tree, c) {
492 m = get_marker_label(c, label, node, prop);
502 struct node *get_subnode(struct node *node, const char *nodename)
506 for_each_child(node, child)
507 if (streq(child->name, nodename))
513 struct node *get_node_by_path(struct node *tree, const char *path)
518 if (!path || ! (*path)) {
524 while (path[0] == '/')
527 p = strchr(path, '/');
529 for_each_child(tree, child) {
530 if (p && (strlen(child->name) == p-path) &&
531 strprefixeq(path, p - path, child->name))
532 return get_node_by_path(child, p+1);
533 else if (!p && streq(path, child->name))
540 struct node *get_node_by_label(struct node *tree, const char *label)
542 struct node *child, *node;
545 assert(label && (strlen(label) > 0));
547 for_each_label(tree->labels, l)
548 if (streq(l->label, label))
551 for_each_child(tree, child) {
552 node = get_node_by_label(child, label);
560 struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
562 struct node *child, *node;
564 if ((phandle == 0) || (phandle == -1)) {
565 assert(generate_fixups);
569 if (tree->phandle == phandle) {
575 for_each_child(tree, child) {
576 node = get_node_by_phandle(child, phandle);
584 struct node *get_node_by_ref(struct node *tree, const char *ref)
588 else if (ref[0] == '/')
589 return get_node_by_path(tree, ref);
591 return get_node_by_label(tree, ref);
594 cell_t get_node_phandle(struct node *root, struct node *node)
596 static cell_t phandle = 1; /* FIXME: ick, static local */
598 if ((node->phandle != 0) && (node->phandle != -1))
599 return node->phandle;
601 while (get_node_by_phandle(root, phandle))
604 node->phandle = phandle;
606 if (!get_property(node, "linux,phandle")
607 && (phandle_format & PHANDLE_LEGACY))
609 build_property("linux,phandle",
610 data_append_cell(empty_data, phandle)));
612 if (!get_property(node, "phandle")
613 && (phandle_format & PHANDLE_EPAPR))
615 build_property("phandle",
616 data_append_cell(empty_data, phandle)));
618 /* If the node *does* have a phandle property, we must
619 * be dealing with a self-referencing phandle, which will be
620 * fixed up momentarily in the caller */
622 return node->phandle;
625 uint32_t guess_boot_cpuid(struct node *tree)
627 struct node *cpus, *bootcpu;
628 struct property *reg;
630 cpus = get_node_by_path(tree, "/cpus");
634 bootcpu = cpus->children;
638 reg = get_property(bootcpu, "reg");
639 if (!reg || (reg->val.len != sizeof(uint32_t)))
642 /* FIXME: Sanity check node? */
644 return propval_cell(reg);
647 static int cmp_reserve_info(const void *ax, const void *bx)
649 const struct reserve_info *a, *b;
651 a = *((const struct reserve_info * const *)ax);
652 b = *((const struct reserve_info * const *)bx);
654 if (a->address < b->address)
656 else if (a->address > b->address)
658 else if (a->size < b->size)
660 else if (a->size > b->size)
666 static void sort_reserve_entries(struct dt_info *dti)
668 struct reserve_info *ri, **tbl;
671 for (ri = dti->reservelist;
679 tbl = xmalloc(n * sizeof(*tbl));
681 for (ri = dti->reservelist;
686 qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
688 dti->reservelist = tbl[0];
689 for (i = 0; i < (n-1); i++)
690 tbl[i]->next = tbl[i+1];
691 tbl[n-1]->next = NULL;
696 static int cmp_prop(const void *ax, const void *bx)
698 const struct property *a, *b;
700 a = *((const struct property * const *)ax);
701 b = *((const struct property * const *)bx);
703 return strcmp(a->name, b->name);
706 static void sort_properties(struct node *node)
709 struct property *prop, **tbl;
711 for_each_property_withdel(node, prop)
717 tbl = xmalloc(n * sizeof(*tbl));
719 for_each_property_withdel(node, prop)
722 qsort(tbl, n, sizeof(*tbl), cmp_prop);
724 node->proplist = tbl[0];
725 for (i = 0; i < (n-1); i++)
726 tbl[i]->next = tbl[i+1];
727 tbl[n-1]->next = NULL;
732 static int cmp_subnode(const void *ax, const void *bx)
734 const struct node *a, *b;
736 a = *((const struct node * const *)ax);
737 b = *((const struct node * const *)bx);
739 return strcmp(a->name, b->name);
742 static void sort_subnodes(struct node *node)
745 struct node *subnode, **tbl;
747 for_each_child_withdel(node, subnode)
753 tbl = xmalloc(n * sizeof(*tbl));
755 for_each_child_withdel(node, subnode)
758 qsort(tbl, n, sizeof(*tbl), cmp_subnode);
760 node->children = tbl[0];
761 for (i = 0; i < (n-1); i++)
762 tbl[i]->next_sibling = tbl[i+1];
763 tbl[n-1]->next_sibling = NULL;
768 static void sort_node(struct node *node)
772 sort_properties(node);
774 for_each_child_withdel(node, c)
778 void sort_tree(struct dt_info *dti)
780 sort_reserve_entries(dti);
784 /* utility helper to avoid code duplication */
785 static struct node *build_and_name_child_node(struct node *parent, char *name)
789 node = build_node(NULL, NULL);
790 name_node(node, xstrdup(name));
791 add_child(parent, node);
796 static struct node *build_root_node(struct node *dt, char *name)
800 an = get_subnode(dt, name);
802 an = build_and_name_child_node(dt, name);
805 die("Could not build root node /%s\n", name);
810 static bool any_label_tree(struct dt_info *dti, struct node *node)
817 for_each_child(node, c)
818 if (any_label_tree(dti, c))
824 static void generate_label_tree_internal(struct dt_info *dti,
825 struct node *an, struct node *node,
828 struct node *dt = dti->dt;
833 /* if there are labels */
836 /* now add the label in the node */
837 for_each_label(node->labels, l) {
839 /* check whether the label already exists */
840 p = get_property(an, l->label);
842 fprintf(stderr, "WARNING: label %s already"
843 " exists in /%s", l->label,
849 p = build_property(l->label,
850 data_copy_mem(node->fullpath,
851 strlen(node->fullpath) + 1));
855 /* force allocation of a phandle for this node */
857 (void)get_node_phandle(dt, node);
860 for_each_child(node, c)
861 generate_label_tree_internal(dti, an, c, allocph);
864 static bool any_fixup_tree(struct dt_info *dti, struct node *node)
867 struct property *prop;
870 for_each_property(node, prop) {
871 m = prop->val.markers;
872 for_each_marker_of_type(m, REF_PHANDLE) {
873 if (!get_node_by_ref(dti->dt, m->ref))
878 for_each_child(node, c) {
879 if (any_fixup_tree(dti, c))
886 static void add_fixup_entry(struct dt_info *dti, struct node *fn,
887 struct node *node, struct property *prop,
892 /* m->ref can only be a REF_PHANDLE, but check anyway */
893 assert(m->type == REF_PHANDLE);
895 /* there shouldn't be any ':' in the arguments */
896 if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
897 die("arguments should not contain ':'\n");
899 xasprintf(&entry, "%s:%s:%u",
900 node->fullpath, prop->name, m->offset);
901 append_to_property(fn, m->ref, entry, strlen(entry) + 1);
906 static void generate_fixups_tree_internal(struct dt_info *dti,
910 struct node *dt = dti->dt;
912 struct property *prop;
914 struct node *refnode;
916 for_each_property(node, prop) {
917 m = prop->val.markers;
918 for_each_marker_of_type(m, REF_PHANDLE) {
919 refnode = get_node_by_ref(dt, m->ref);
921 add_fixup_entry(dti, fn, node, prop, m);
925 for_each_child(node, c)
926 generate_fixups_tree_internal(dti, fn, c);
929 static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
932 struct property *prop;
935 for_each_property(node, prop) {
936 m = prop->val.markers;
937 for_each_marker_of_type(m, REF_PHANDLE) {
938 if (get_node_by_ref(dti->dt, m->ref))
943 for_each_child(node, c) {
944 if (any_local_fixup_tree(dti, c))
951 static void add_local_fixup_entry(struct dt_info *dti,
952 struct node *lfn, struct node *node,
953 struct property *prop, struct marker *m,
954 struct node *refnode)
956 struct node *wn, *nwn; /* local fixup node, walk node, new */
961 /* walk back retreiving depth */
963 for (wn = node; wn; wn = wn->parent)
966 /* allocate name array */
967 compp = xmalloc(sizeof(*compp) * depth);
969 /* store names in the array */
970 for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
973 /* walk the path components creating nodes if they don't exist */
974 for (wn = lfn, i = 1; i < depth; i++, wn = nwn) {
975 /* if no node exists, create it */
976 nwn = get_subnode(wn, compp[i]);
978 nwn = build_and_name_child_node(wn, compp[i]);
983 value_32 = cpu_to_fdt32(m->offset);
984 append_to_property(wn, prop->name, &value_32, sizeof(value_32));
987 static void generate_local_fixups_tree_internal(struct dt_info *dti,
991 struct node *dt = dti->dt;
993 struct property *prop;
995 struct node *refnode;
997 for_each_property(node, prop) {
998 m = prop->val.markers;
999 for_each_marker_of_type(m, REF_PHANDLE) {
1000 refnode = get_node_by_ref(dt, m->ref);
1002 add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
1006 for_each_child(node, c)
1007 generate_local_fixups_tree_internal(dti, lfn, c);
1010 void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
1012 if (!any_label_tree(dti, dti->dt))
1014 generate_label_tree_internal(dti, build_root_node(dti->dt, name),
1018 void generate_fixups_tree(struct dt_info *dti, char *name)
1020 if (!any_fixup_tree(dti, dti->dt))
1022 generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
1026 void generate_local_fixups_tree(struct dt_info *dti, char *name)
1028 if (!any_local_fixup_tree(dti, dti->dt))
1030 generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name),