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;
241 d = data_add_marker(d, REF_PHANDLE, ref);
242 d = data_append_integer(d, 0xffffffff, 32);
244 p = build_property("target", d);
246 xasprintf(&name, "fragment@%u",
247 next_orphan_fragment++);
248 name_node(new_node, "__overlay__");
249 node = build_node(p, new_node);
250 name_node(node, name);
256 struct node *chain_node(struct node *first, struct node *list)
258 assert(first->next_sibling == NULL);
260 first->next_sibling = list;
264 void add_property(struct node *node, struct property *prop)
277 void delete_property_by_name(struct node *node, char *name)
279 struct property *prop = node->proplist;
282 if (streq(prop->name, name)) {
283 delete_property(prop);
290 void delete_property(struct property *prop)
293 delete_labels(&prop->labels);
296 void add_child(struct node *parent, struct node *child)
300 child->next_sibling = NULL;
301 child->parent = parent;
303 p = &parent->children;
305 p = &((*p)->next_sibling);
310 void delete_node_by_name(struct node *parent, char *name)
312 struct node *node = parent->children;
315 if (streq(node->name, name)) {
319 node = node->next_sibling;
323 void delete_node(struct node *node)
325 struct property *prop;
329 for_each_child(node, child)
331 for_each_property(node, prop)
332 delete_property(prop);
333 delete_labels(&node->labels);
336 void append_to_property(struct node *node,
337 char *name, const void *data, int len)
342 p = get_property(node, name);
344 d = data_append_data(p->val, data, len);
347 d = data_append_data(empty_data, data, len);
348 p = build_property(name, d);
349 add_property(node, p);
353 struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)
355 struct reserve_info *new = xmalloc(sizeof(*new));
357 memset(new, 0, sizeof(*new));
359 new->address = address;
365 struct reserve_info *chain_reserve_entry(struct reserve_info *first,
366 struct reserve_info *list)
368 assert(first->next == NULL);
374 struct reserve_info *add_reserve_entry(struct reserve_info *list,
375 struct reserve_info *new)
377 struct reserve_info *last;
384 for (last = list; last->next; last = last->next)
392 struct dt_info *build_dt_info(unsigned int dtsflags,
393 struct reserve_info *reservelist,
394 struct node *tree, uint32_t boot_cpuid_phys)
398 dti = xmalloc(sizeof(*dti));
399 dti->dtsflags = dtsflags;
400 dti->reservelist = reservelist;
402 dti->boot_cpuid_phys = boot_cpuid_phys;
408 * Tree accessor functions
411 const char *get_unitname(struct node *node)
413 if (node->name[node->basenamelen] == '\0')
416 return node->name + node->basenamelen + 1;
419 struct property *get_property(struct node *node, const char *propname)
421 struct property *prop;
423 for_each_property(node, prop)
424 if (streq(prop->name, propname))
430 cell_t propval_cell(struct property *prop)
432 assert(prop->val.len == sizeof(cell_t));
433 return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
436 cell_t propval_cell_n(struct property *prop, int n)
438 assert(prop->val.len / sizeof(cell_t) >= n);
439 return fdt32_to_cpu(*((fdt32_t *)prop->val.val + n));
442 struct property *get_property_by_label(struct node *tree, const char *label,
445 struct property *prop;
450 for_each_property(tree, prop) {
453 for_each_label(prop->labels, l)
454 if (streq(l->label, label))
458 for_each_child(tree, c) {
459 prop = get_property_by_label(c, label, node);
468 struct marker *get_marker_label(struct node *tree, const char *label,
469 struct node **node, struct property **prop)
477 for_each_property(tree, p) {
480 for_each_marker_of_type(m, LABEL)
481 if (streq(m->ref, label))
485 for_each_child(tree, c) {
486 m = get_marker_label(c, label, node, prop);
496 struct node *get_subnode(struct node *node, const char *nodename)
500 for_each_child(node, child)
501 if (streq(child->name, nodename))
507 struct node *get_node_by_path(struct node *tree, const char *path)
512 if (!path || ! (*path)) {
518 while (path[0] == '/')
521 p = strchr(path, '/');
523 for_each_child(tree, child) {
524 if (p && (strlen(child->name) == p-path) &&
525 strprefixeq(path, p - path, child->name))
526 return get_node_by_path(child, p+1);
527 else if (!p && streq(path, child->name))
534 struct node *get_node_by_label(struct node *tree, const char *label)
536 struct node *child, *node;
539 assert(label && (strlen(label) > 0));
541 for_each_label(tree->labels, l)
542 if (streq(l->label, label))
545 for_each_child(tree, child) {
546 node = get_node_by_label(child, label);
554 struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
556 struct node *child, *node;
558 if ((phandle == 0) || (phandle == -1)) {
559 assert(generate_fixups);
563 if (tree->phandle == phandle) {
569 for_each_child(tree, child) {
570 node = get_node_by_phandle(child, phandle);
578 struct node *get_node_by_ref(struct node *tree, const char *ref)
582 else if (ref[0] == '/')
583 return get_node_by_path(tree, ref);
585 return get_node_by_label(tree, ref);
588 cell_t get_node_phandle(struct node *root, struct node *node)
590 static cell_t phandle = 1; /* FIXME: ick, static local */
592 if ((node->phandle != 0) && (node->phandle != -1))
593 return node->phandle;
595 while (get_node_by_phandle(root, phandle))
598 node->phandle = phandle;
600 if (!get_property(node, "linux,phandle")
601 && (phandle_format & PHANDLE_LEGACY))
603 build_property("linux,phandle",
604 data_append_cell(empty_data, phandle)));
606 if (!get_property(node, "phandle")
607 && (phandle_format & PHANDLE_EPAPR))
609 build_property("phandle",
610 data_append_cell(empty_data, phandle)));
612 /* If the node *does* have a phandle property, we must
613 * be dealing with a self-referencing phandle, which will be
614 * fixed up momentarily in the caller */
616 return node->phandle;
619 uint32_t guess_boot_cpuid(struct node *tree)
621 struct node *cpus, *bootcpu;
622 struct property *reg;
624 cpus = get_node_by_path(tree, "/cpus");
629 bootcpu = cpus->children;
633 reg = get_property(bootcpu, "reg");
634 if (!reg || (reg->val.len != sizeof(uint32_t)))
637 /* FIXME: Sanity check node? */
639 return propval_cell(reg);
642 static int cmp_reserve_info(const void *ax, const void *bx)
644 const struct reserve_info *a, *b;
646 a = *((const struct reserve_info * const *)ax);
647 b = *((const struct reserve_info * const *)bx);
649 if (a->address < b->address)
651 else if (a->address > b->address)
653 else if (a->size < b->size)
655 else if (a->size > b->size)
661 static void sort_reserve_entries(struct dt_info *dti)
663 struct reserve_info *ri, **tbl;
666 for (ri = dti->reservelist;
674 tbl = xmalloc(n * sizeof(*tbl));
676 for (ri = dti->reservelist;
681 qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
683 dti->reservelist = tbl[0];
684 for (i = 0; i < (n-1); i++)
685 tbl[i]->next = tbl[i+1];
686 tbl[n-1]->next = NULL;
691 static int cmp_prop(const void *ax, const void *bx)
693 const struct property *a, *b;
695 a = *((const struct property * const *)ax);
696 b = *((const struct property * const *)bx);
698 return strcmp(a->name, b->name);
701 static void sort_properties(struct node *node)
704 struct property *prop, **tbl;
706 for_each_property_withdel(node, prop)
712 tbl = xmalloc(n * sizeof(*tbl));
714 for_each_property_withdel(node, prop)
717 qsort(tbl, n, sizeof(*tbl), cmp_prop);
719 node->proplist = tbl[0];
720 for (i = 0; i < (n-1); i++)
721 tbl[i]->next = tbl[i+1];
722 tbl[n-1]->next = NULL;
727 static int cmp_subnode(const void *ax, const void *bx)
729 const struct node *a, *b;
731 a = *((const struct node * const *)ax);
732 b = *((const struct node * const *)bx);
734 return strcmp(a->name, b->name);
737 static void sort_subnodes(struct node *node)
740 struct node *subnode, **tbl;
742 for_each_child_withdel(node, subnode)
748 tbl = xmalloc(n * sizeof(*tbl));
750 for_each_child_withdel(node, subnode)
753 qsort(tbl, n, sizeof(*tbl), cmp_subnode);
755 node->children = tbl[0];
756 for (i = 0; i < (n-1); i++)
757 tbl[i]->next_sibling = tbl[i+1];
758 tbl[n-1]->next_sibling = NULL;
763 static void sort_node(struct node *node)
767 sort_properties(node);
769 for_each_child_withdel(node, c)
773 void sort_tree(struct dt_info *dti)
775 sort_reserve_entries(dti);
779 /* utility helper to avoid code duplication */
780 static struct node *build_and_name_child_node(struct node *parent, char *name)
784 node = build_node(NULL, NULL);
785 name_node(node, xstrdup(name));
786 add_child(parent, node);
791 static struct node *build_root_node(struct node *dt, char *name)
795 an = get_subnode(dt, name);
797 an = build_and_name_child_node(dt, name);
800 die("Could not build root node /%s\n", name);
805 static bool any_label_tree(struct dt_info *dti, struct node *node)
812 for_each_child(node, c)
813 if (any_label_tree(dti, c))
819 static void generate_label_tree_internal(struct dt_info *dti,
820 struct node *an, struct node *node,
823 struct node *dt = dti->dt;
828 /* if there are labels */
831 /* now add the label in the node */
832 for_each_label(node->labels, l) {
834 /* check whether the label already exists */
835 p = get_property(an, l->label);
837 fprintf(stderr, "WARNING: label %s already"
838 " exists in /%s", l->label,
844 p = build_property(l->label,
845 data_copy_mem(node->fullpath,
846 strlen(node->fullpath) + 1));
850 /* force allocation of a phandle for this node */
852 (void)get_node_phandle(dt, node);
855 for_each_child(node, c)
856 generate_label_tree_internal(dti, an, c, allocph);
859 static bool any_fixup_tree(struct dt_info *dti, struct node *node)
862 struct property *prop;
865 for_each_property(node, prop) {
866 m = prop->val.markers;
867 for_each_marker_of_type(m, REF_PHANDLE) {
868 if (!get_node_by_ref(dti->dt, m->ref))
873 for_each_child(node, c) {
874 if (any_fixup_tree(dti, c))
881 static void add_fixup_entry(struct dt_info *dti, struct node *fn,
882 struct node *node, struct property *prop,
887 /* m->ref can only be a REF_PHANDLE, but check anyway */
888 assert(m->type == REF_PHANDLE);
890 /* there shouldn't be any ':' in the arguments */
891 if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
892 die("arguments should not contain ':'\n");
894 xasprintf(&entry, "%s:%s:%u",
895 node->fullpath, prop->name, m->offset);
896 append_to_property(fn, m->ref, entry, strlen(entry) + 1);
901 static void generate_fixups_tree_internal(struct dt_info *dti,
905 struct node *dt = dti->dt;
907 struct property *prop;
909 struct node *refnode;
911 for_each_property(node, prop) {
912 m = prop->val.markers;
913 for_each_marker_of_type(m, REF_PHANDLE) {
914 refnode = get_node_by_ref(dt, m->ref);
916 add_fixup_entry(dti, fn, node, prop, m);
920 for_each_child(node, c)
921 generate_fixups_tree_internal(dti, fn, c);
924 static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
927 struct property *prop;
930 for_each_property(node, prop) {
931 m = prop->val.markers;
932 for_each_marker_of_type(m, REF_PHANDLE) {
933 if (get_node_by_ref(dti->dt, m->ref))
938 for_each_child(node, c) {
939 if (any_local_fixup_tree(dti, c))
946 static void add_local_fixup_entry(struct dt_info *dti,
947 struct node *lfn, struct node *node,
948 struct property *prop, struct marker *m,
949 struct node *refnode)
951 struct node *wn, *nwn; /* local fixup node, walk node, new */
956 /* walk back retreiving depth */
958 for (wn = node; wn; wn = wn->parent)
961 /* allocate name array */
962 compp = xmalloc(sizeof(*compp) * depth);
964 /* store names in the array */
965 for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
968 /* walk the path components creating nodes if they don't exist */
969 for (wn = lfn, i = 1; i < depth; i++, wn = nwn) {
970 /* if no node exists, create it */
971 nwn = get_subnode(wn, compp[i]);
973 nwn = build_and_name_child_node(wn, compp[i]);
978 value_32 = cpu_to_fdt32(m->offset);
979 append_to_property(wn, prop->name, &value_32, sizeof(value_32));
982 static void generate_local_fixups_tree_internal(struct dt_info *dti,
986 struct node *dt = dti->dt;
988 struct property *prop;
990 struct node *refnode;
992 for_each_property(node, prop) {
993 m = prop->val.markers;
994 for_each_marker_of_type(m, REF_PHANDLE) {
995 refnode = get_node_by_ref(dt, m->ref);
997 add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
1001 for_each_child(node, c)
1002 generate_local_fixups_tree_internal(dti, lfn, c);
1005 void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
1007 if (!any_label_tree(dti, dti->dt))
1009 generate_label_tree_internal(dti, build_root_node(dti->dt, name),
1013 void generate_fixups_tree(struct dt_info *dti, char *name)
1015 if (!any_fixup_tree(dti, dti->dt))
1017 generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
1021 void generate_local_fixups_tree(struct dt_info *dti, char *name)
1023 if (!any_local_fixup_tree(dti, dti->dt))
1025 generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name),