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");
635 bootcpu
= cpus
->children
;
639 reg
= get_property(bootcpu
, "reg");
640 if (!reg
|| (reg
->val
.len
!= sizeof(uint32_t)))
643 /* FIXME: Sanity check node? */
645 return propval_cell(reg
);
648 static int cmp_reserve_info(const void *ax
, const void *bx
)
650 const struct reserve_info
*a
, *b
;
652 a
= *((const struct reserve_info
* const *)ax
);
653 b
= *((const struct reserve_info
* const *)bx
);
655 if (a
->address
< b
->address
)
657 else if (a
->address
> b
->address
)
659 else if (a
->size
< b
->size
)
661 else if (a
->size
> b
->size
)
667 static void sort_reserve_entries(struct dt_info
*dti
)
669 struct reserve_info
*ri
, **tbl
;
672 for (ri
= dti
->reservelist
;
680 tbl
= xmalloc(n
* sizeof(*tbl
));
682 for (ri
= dti
->reservelist
;
687 qsort(tbl
, n
, sizeof(*tbl
), cmp_reserve_info
);
689 dti
->reservelist
= tbl
[0];
690 for (i
= 0; i
< (n
-1); i
++)
691 tbl
[i
]->next
= tbl
[i
+1];
692 tbl
[n
-1]->next
= NULL
;
697 static int cmp_prop(const void *ax
, const void *bx
)
699 const struct property
*a
, *b
;
701 a
= *((const struct property
* const *)ax
);
702 b
= *((const struct property
* const *)bx
);
704 return strcmp(a
->name
, b
->name
);
707 static void sort_properties(struct node
*node
)
710 struct property
*prop
, **tbl
;
712 for_each_property_withdel(node
, prop
)
718 tbl
= xmalloc(n
* sizeof(*tbl
));
720 for_each_property_withdel(node
, prop
)
723 qsort(tbl
, n
, sizeof(*tbl
), cmp_prop
);
725 node
->proplist
= tbl
[0];
726 for (i
= 0; i
< (n
-1); i
++)
727 tbl
[i
]->next
= tbl
[i
+1];
728 tbl
[n
-1]->next
= NULL
;
733 static int cmp_subnode(const void *ax
, const void *bx
)
735 const struct node
*a
, *b
;
737 a
= *((const struct node
* const *)ax
);
738 b
= *((const struct node
* const *)bx
);
740 return strcmp(a
->name
, b
->name
);
743 static void sort_subnodes(struct node
*node
)
746 struct node
*subnode
, **tbl
;
748 for_each_child_withdel(node
, subnode
)
754 tbl
= xmalloc(n
* sizeof(*tbl
));
756 for_each_child_withdel(node
, subnode
)
759 qsort(tbl
, n
, sizeof(*tbl
), cmp_subnode
);
761 node
->children
= tbl
[0];
762 for (i
= 0; i
< (n
-1); i
++)
763 tbl
[i
]->next_sibling
= tbl
[i
+1];
764 tbl
[n
-1]->next_sibling
= NULL
;
769 static void sort_node(struct node
*node
)
773 sort_properties(node
);
775 for_each_child_withdel(node
, c
)
779 void sort_tree(struct dt_info
*dti
)
781 sort_reserve_entries(dti
);
785 /* utility helper to avoid code duplication */
786 static struct node
*build_and_name_child_node(struct node
*parent
, char *name
)
790 node
= build_node(NULL
, NULL
);
791 name_node(node
, xstrdup(name
));
792 add_child(parent
, node
);
797 static struct node
*build_root_node(struct node
*dt
, char *name
)
801 an
= get_subnode(dt
, name
);
803 an
= build_and_name_child_node(dt
, name
);
806 die("Could not build root node /%s\n", name
);
811 static bool any_label_tree(struct dt_info
*dti
, struct node
*node
)
818 for_each_child(node
, c
)
819 if (any_label_tree(dti
, c
))
825 static void generate_label_tree_internal(struct dt_info
*dti
,
826 struct node
*an
, struct node
*node
,
829 struct node
*dt
= dti
->dt
;
834 /* if there are labels */
837 /* now add the label in the node */
838 for_each_label(node
->labels
, l
) {
840 /* check whether the label already exists */
841 p
= get_property(an
, l
->label
);
843 fprintf(stderr
, "WARNING: label %s already"
844 " exists in /%s", l
->label
,
850 p
= build_property(l
->label
,
851 data_copy_mem(node
->fullpath
,
852 strlen(node
->fullpath
) + 1));
856 /* force allocation of a phandle for this node */
858 (void)get_node_phandle(dt
, node
);
861 for_each_child(node
, c
)
862 generate_label_tree_internal(dti
, an
, c
, allocph
);
865 static bool any_fixup_tree(struct dt_info
*dti
, struct node
*node
)
868 struct property
*prop
;
871 for_each_property(node
, prop
) {
872 m
= prop
->val
.markers
;
873 for_each_marker_of_type(m
, REF_PHANDLE
) {
874 if (!get_node_by_ref(dti
->dt
, m
->ref
))
879 for_each_child(node
, c
) {
880 if (any_fixup_tree(dti
, c
))
887 static void add_fixup_entry(struct dt_info
*dti
, struct node
*fn
,
888 struct node
*node
, struct property
*prop
,
893 /* m->ref can only be a REF_PHANDLE, but check anyway */
894 assert(m
->type
== REF_PHANDLE
);
896 /* there shouldn't be any ':' in the arguments */
897 if (strchr(node
->fullpath
, ':') || strchr(prop
->name
, ':'))
898 die("arguments should not contain ':'\n");
900 xasprintf(&entry
, "%s:%s:%u",
901 node
->fullpath
, prop
->name
, m
->offset
);
902 append_to_property(fn
, m
->ref
, entry
, strlen(entry
) + 1);
907 static void generate_fixups_tree_internal(struct dt_info
*dti
,
911 struct node
*dt
= dti
->dt
;
913 struct property
*prop
;
915 struct node
*refnode
;
917 for_each_property(node
, prop
) {
918 m
= prop
->val
.markers
;
919 for_each_marker_of_type(m
, REF_PHANDLE
) {
920 refnode
= get_node_by_ref(dt
, m
->ref
);
922 add_fixup_entry(dti
, fn
, node
, prop
, m
);
926 for_each_child(node
, c
)
927 generate_fixups_tree_internal(dti
, fn
, c
);
930 static bool any_local_fixup_tree(struct dt_info
*dti
, struct node
*node
)
933 struct property
*prop
;
936 for_each_property(node
, prop
) {
937 m
= prop
->val
.markers
;
938 for_each_marker_of_type(m
, REF_PHANDLE
) {
939 if (get_node_by_ref(dti
->dt
, m
->ref
))
944 for_each_child(node
, c
) {
945 if (any_local_fixup_tree(dti
, c
))
952 static void add_local_fixup_entry(struct dt_info
*dti
,
953 struct node
*lfn
, struct node
*node
,
954 struct property
*prop
, struct marker
*m
,
955 struct node
*refnode
)
957 struct node
*wn
, *nwn
; /* local fixup node, walk node, new */
962 /* walk back retreiving depth */
964 for (wn
= node
; wn
; wn
= wn
->parent
)
967 /* allocate name array */
968 compp
= xmalloc(sizeof(*compp
) * depth
);
970 /* store names in the array */
971 for (wn
= node
, i
= depth
- 1; wn
; wn
= wn
->parent
, i
--)
974 /* walk the path components creating nodes if they don't exist */
975 for (wn
= lfn
, i
= 1; i
< depth
; i
++, wn
= nwn
) {
976 /* if no node exists, create it */
977 nwn
= get_subnode(wn
, compp
[i
]);
979 nwn
= build_and_name_child_node(wn
, compp
[i
]);
984 value_32
= cpu_to_fdt32(m
->offset
);
985 append_to_property(wn
, prop
->name
, &value_32
, sizeof(value_32
));
988 static void generate_local_fixups_tree_internal(struct dt_info
*dti
,
992 struct node
*dt
= dti
->dt
;
994 struct property
*prop
;
996 struct node
*refnode
;
998 for_each_property(node
, prop
) {
999 m
= prop
->val
.markers
;
1000 for_each_marker_of_type(m
, REF_PHANDLE
) {
1001 refnode
= get_node_by_ref(dt
, m
->ref
);
1003 add_local_fixup_entry(dti
, lfn
, node
, prop
, m
, refnode
);
1007 for_each_child(node
, c
)
1008 generate_local_fixups_tree_internal(dti
, lfn
, c
);
1011 void generate_label_tree(struct dt_info
*dti
, char *name
, bool allocph
)
1013 if (!any_label_tree(dti
, dti
->dt
))
1015 generate_label_tree_internal(dti
, build_root_node(dti
->dt
, name
),
1019 void generate_fixups_tree(struct dt_info
*dti
, char *name
)
1021 if (!any_fixup_tree(dti
, dti
->dt
))
1023 generate_fixups_tree_internal(dti
, build_root_node(dti
->dt
, name
),
1027 void generate_local_fixups_tree(struct dt_info
*dti
, char *name
)
1029 if (!any_local_fixup_tree(dti
, dti
->dt
))
1031 generate_local_fixups_tree_internal(dti
, build_root_node(dti
->dt
, name
),