1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
10 * Tree building functions
13 void add_label(struct label **labels, char *label)
17 /* Make sure the label isn't already there */
18 for_each_label_withdel(*labels, new)
19 if (streq(new->label, label)) {
24 new = xmalloc(sizeof(*new));
25 memset(new, 0, sizeof(*new));
31 void delete_labels(struct label **labels)
35 for_each_label(*labels, label)
39 struct property *build_property(char *name, struct data val,
40 struct srcpos *srcpos)
42 struct property *new = xmalloc(sizeof(*new));
44 memset(new, 0, sizeof(*new));
48 new->srcpos = srcpos_copy(srcpos);
53 struct property *build_property_delete(char *name)
55 struct property *new = xmalloc(sizeof(*new));
57 memset(new, 0, sizeof(*new));
65 struct property *chain_property(struct property *first, struct property *list)
67 assert(first->next == NULL);
73 struct property *reverse_properties(struct property *first)
75 struct property *p = first;
76 struct property *head = NULL;
77 struct property *next;
88 struct node *build_node(struct property *proplist, struct node *children,
89 struct srcpos *srcpos)
91 struct node *new = xmalloc(sizeof(*new));
94 memset(new, 0, sizeof(*new));
96 new->proplist = reverse_properties(proplist);
97 new->children = children;
98 new->srcpos = srcpos_copy(srcpos);
100 for_each_child(new, child) {
107 struct node *build_node_delete(struct srcpos *srcpos)
109 struct node *new = xmalloc(sizeof(*new));
111 memset(new, 0, sizeof(*new));
114 new->srcpos = srcpos_copy(srcpos);
119 struct node *name_node(struct node *node, char *name)
121 assert(node->name == NULL);
128 struct node *omit_node_if_unused(struct node *node)
130 node->omit_if_unused = 1;
135 struct node *reference_node(struct node *node)
137 node->is_referenced = 1;
142 struct node *merge_nodes(struct node *old_node, struct node *new_node)
144 struct property *new_prop, *old_prop;
145 struct node *new_child, *old_child;
148 old_node->deleted = 0;
150 /* Add new node labels to old node */
151 for_each_label_withdel(new_node->labels, l)
152 add_label(&old_node->labels, l->label);
154 /* Move properties from the new node to the old node. If there
155 * is a collision, replace the old value with the new */
156 while (new_node->proplist) {
157 /* Pop the property off the list */
158 new_prop = new_node->proplist;
159 new_node->proplist = new_prop->next;
160 new_prop->next = NULL;
162 if (new_prop->deleted) {
163 delete_property_by_name(old_node, new_prop->name);
168 /* Look for a collision, set new value if there is */
169 for_each_property_withdel(old_node, old_prop) {
170 if (streq(old_prop->name, new_prop->name)) {
171 /* Add new labels to old property */
172 for_each_label_withdel(new_prop->labels, l)
173 add_label(&old_prop->labels, l->label);
175 old_prop->val = new_prop->val;
176 old_prop->deleted = 0;
177 free(old_prop->srcpos);
178 old_prop->srcpos = new_prop->srcpos;
185 /* if no collision occurred, add property to the old node. */
187 add_property(old_node, new_prop);
190 /* Move the override child nodes into the primary node. If
191 * there is a collision, then merge the nodes. */
192 while (new_node->children) {
193 /* Pop the child node off the list */
194 new_child = new_node->children;
195 new_node->children = new_child->next_sibling;
196 new_child->parent = NULL;
197 new_child->next_sibling = NULL;
199 if (new_child->deleted) {
200 delete_node_by_name(old_node, new_child->name);
205 /* Search for a collision. Merge if there is */
206 for_each_child_withdel(old_node, old_child) {
207 if (streq(old_child->name, new_child->name)) {
208 merge_nodes(old_child, new_child);
214 /* if no collision occurred, add child to the old node. */
216 add_child(old_node, new_child);
219 old_node->srcpos = srcpos_extend(old_node->srcpos, new_node->srcpos);
221 /* The new node contents are now merged into the old node. Free
228 struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
230 static unsigned int next_orphan_fragment = 0;
233 struct data d = empty_data;
237 d = data_add_marker(d, TYPE_STRING, ref);
238 d = data_append_data(d, ref, strlen(ref) + 1);
240 p = build_property("target-path", d, NULL);
242 d = data_add_marker(d, REF_PHANDLE, ref);
243 d = data_append_integer(d, 0xffffffff, 32);
245 p = build_property("target", d, NULL);
248 xasprintf(&name, "fragment@%u",
249 next_orphan_fragment++);
250 name_node(new_node, "__overlay__");
251 node = build_node(p, new_node, NULL);
252 name_node(node, name);
258 struct node *chain_node(struct node *first, struct node *list)
260 assert(first->next_sibling == NULL);
262 first->next_sibling = list;
266 void add_property(struct node *node, struct property *prop)
279 void delete_property_by_name(struct node *node, char *name)
281 struct property *prop = node->proplist;
284 if (streq(prop->name, name)) {
285 delete_property(prop);
292 void delete_property(struct property *prop)
295 delete_labels(&prop->labels);
298 void add_child(struct node *parent, struct node *child)
302 child->next_sibling = NULL;
303 child->parent = parent;
305 p = &parent->children;
307 p = &((*p)->next_sibling);
312 void delete_node_by_name(struct node *parent, char *name)
314 struct node *node = parent->children;
317 if (streq(node->name, name)) {
321 node = node->next_sibling;
325 void delete_node(struct node *node)
327 struct property *prop;
331 for_each_child(node, child)
333 for_each_property(node, prop)
334 delete_property(prop);
335 delete_labels(&node->labels);
338 void append_to_property(struct node *node,
339 char *name, const void *data, int len,
340 enum markertype type)
345 p = get_property(node, name);
347 d = data_add_marker(p->val, type, name);
348 d = data_append_data(d, data, len);
351 d = data_add_marker(empty_data, type, name);
352 d = data_append_data(d, data, len);
353 p = build_property(name, d, NULL);
354 add_property(node, p);
358 struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)
360 struct reserve_info *new = xmalloc(sizeof(*new));
362 memset(new, 0, sizeof(*new));
364 new->address = address;
370 struct reserve_info *chain_reserve_entry(struct reserve_info *first,
371 struct reserve_info *list)
373 assert(first->next == NULL);
379 struct reserve_info *add_reserve_entry(struct reserve_info *list,
380 struct reserve_info *new)
382 struct reserve_info *last;
389 for (last = list; last->next; last = last->next)
397 struct dt_info *build_dt_info(unsigned int dtsflags,
398 struct reserve_info *reservelist,
399 struct node *tree, uint32_t boot_cpuid_phys)
403 dti = xmalloc(sizeof(*dti));
404 dti->dtsflags = dtsflags;
405 dti->reservelist = reservelist;
407 dti->boot_cpuid_phys = boot_cpuid_phys;
413 * Tree accessor functions
416 const char *get_unitname(struct node *node)
418 if (node->name[node->basenamelen] == '\0')
421 return node->name + node->basenamelen + 1;
424 struct property *get_property(struct node *node, const char *propname)
426 struct property *prop;
428 for_each_property(node, prop)
429 if (streq(prop->name, propname))
435 cell_t propval_cell(struct property *prop)
437 assert(prop->val.len == sizeof(cell_t));
438 return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
441 cell_t propval_cell_n(struct property *prop, unsigned int n)
443 assert(prop->val.len / sizeof(cell_t) >= n);
444 return fdt32_to_cpu(*((fdt32_t *)prop->val.val + n));
447 struct property *get_property_by_label(struct node *tree, const char *label,
450 struct property *prop;
455 for_each_property(tree, prop) {
458 for_each_label(prop->labels, l)
459 if (streq(l->label, label))
463 for_each_child(tree, c) {
464 prop = get_property_by_label(c, label, node);
473 struct marker *get_marker_label(struct node *tree, const char *label,
474 struct node **node, struct property **prop)
482 for_each_property(tree, p) {
485 for_each_marker_of_type(m, LABEL)
486 if (streq(m->ref, label))
490 for_each_child(tree, c) {
491 m = get_marker_label(c, label, node, prop);
501 struct node *get_subnode(struct node *node, const char *nodename)
505 for_each_child(node, child)
506 if (streq(child->name, nodename))
512 struct node *get_node_by_path(struct node *tree, const char *path)
517 if (!path || ! (*path)) {
523 while (path[0] == '/')
526 p = strchr(path, '/');
528 for_each_child(tree, child) {
529 if (p && strprefixeq(path, (size_t)(p - path), child->name))
530 return get_node_by_path(child, p+1);
531 else if (!p && streq(path, child->name))
538 struct node *get_node_by_label(struct node *tree, const char *label)
540 struct node *child, *node;
543 assert(label && (strlen(label) > 0));
545 for_each_label(tree->labels, l)
546 if (streq(l->label, label))
549 for_each_child(tree, child) {
550 node = get_node_by_label(child, label);
558 struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
560 struct node *child, *node;
562 if (!phandle_is_valid(phandle)) {
563 assert(generate_fixups);
567 if (tree->phandle == phandle) {
573 for_each_child(tree, child) {
574 node = get_node_by_phandle(child, phandle);
582 struct node *get_node_by_ref(struct node *tree, const char *ref)
584 struct node *target = tree;
585 const char *label = NULL, *path = NULL;
596 const char *slash = strchr(label, '/');
600 buf = xstrndup(label, slash - label);
605 target = get_node_by_label(tree, label);
614 target = get_node_by_path(target, path);
619 cell_t get_node_phandle(struct node *root, struct node *node)
621 static cell_t phandle = 1; /* FIXME: ick, static local */
622 struct data d = empty_data;
624 if (phandle_is_valid(node->phandle))
625 return node->phandle;
627 while (get_node_by_phandle(root, phandle))
630 node->phandle = phandle;
632 d = data_add_marker(d, TYPE_UINT32, NULL);
633 d = data_append_cell(d, phandle);
635 if (!get_property(node, "linux,phandle")
636 && (phandle_format & PHANDLE_LEGACY))
637 add_property(node, build_property("linux,phandle", d, NULL));
639 if (!get_property(node, "phandle")
640 && (phandle_format & PHANDLE_EPAPR))
641 add_property(node, build_property("phandle", d, NULL));
643 /* If the node *does* have a phandle property, we must
644 * be dealing with a self-referencing phandle, which will be
645 * fixed up momentarily in the caller */
647 return node->phandle;
650 uint32_t guess_boot_cpuid(struct node *tree)
652 struct node *cpus, *bootcpu;
653 struct property *reg;
655 cpus = get_node_by_path(tree, "/cpus");
660 bootcpu = cpus->children;
664 reg = get_property(bootcpu, "reg");
665 if (!reg || (reg->val.len != sizeof(uint32_t)))
668 /* FIXME: Sanity check node? */
670 return propval_cell(reg);
673 static int cmp_reserve_info(const void *ax, const void *bx)
675 const struct reserve_info *a, *b;
677 a = *((const struct reserve_info * const *)ax);
678 b = *((const struct reserve_info * const *)bx);
680 if (a->address < b->address)
682 else if (a->address > b->address)
684 else if (a->size < b->size)
686 else if (a->size > b->size)
692 static void sort_reserve_entries(struct dt_info *dti)
694 struct reserve_info *ri, **tbl;
697 for (ri = dti->reservelist;
705 tbl = xmalloc(n * sizeof(*tbl));
707 for (ri = dti->reservelist;
712 qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
714 dti->reservelist = tbl[0];
715 for (i = 0; i < (n-1); i++)
716 tbl[i]->next = tbl[i+1];
717 tbl[n-1]->next = NULL;
722 static int cmp_prop(const void *ax, const void *bx)
724 const struct property *a, *b;
726 a = *((const struct property * const *)ax);
727 b = *((const struct property * const *)bx);
729 return strcmp(a->name, b->name);
732 static void sort_properties(struct node *node)
735 struct property *prop, **tbl;
737 for_each_property_withdel(node, prop)
743 tbl = xmalloc(n * sizeof(*tbl));
745 for_each_property_withdel(node, prop)
748 qsort(tbl, n, sizeof(*tbl), cmp_prop);
750 node->proplist = tbl[0];
751 for (i = 0; i < (n-1); i++)
752 tbl[i]->next = tbl[i+1];
753 tbl[n-1]->next = NULL;
758 static int cmp_subnode(const void *ax, const void *bx)
760 const struct node *a, *b;
762 a = *((const struct node * const *)ax);
763 b = *((const struct node * const *)bx);
765 return strcmp(a->name, b->name);
768 static void sort_subnodes(struct node *node)
771 struct node *subnode, **tbl;
773 for_each_child_withdel(node, subnode)
779 tbl = xmalloc(n * sizeof(*tbl));
781 for_each_child_withdel(node, subnode)
784 qsort(tbl, n, sizeof(*tbl), cmp_subnode);
786 node->children = tbl[0];
787 for (i = 0; i < (n-1); i++)
788 tbl[i]->next_sibling = tbl[i+1];
789 tbl[n-1]->next_sibling = NULL;
794 static void sort_node(struct node *node)
798 sort_properties(node);
800 for_each_child_withdel(node, c)
804 void sort_tree(struct dt_info *dti)
806 sort_reserve_entries(dti);
810 /* utility helper to avoid code duplication */
811 static struct node *build_and_name_child_node(struct node *parent, char *name)
815 node = build_node(NULL, NULL, NULL);
816 name_node(node, xstrdup(name));
817 add_child(parent, node);
822 static struct node *build_root_node(struct node *dt, char *name)
826 an = get_subnode(dt, name);
828 an = build_and_name_child_node(dt, name);
831 die("Could not build root node /%s\n", name);
836 static bool any_label_tree(struct dt_info *dti, struct node *node)
843 for_each_child(node, c)
844 if (any_label_tree(dti, c))
850 static void generate_label_tree_internal(struct dt_info *dti,
851 struct node *an, struct node *node,
854 struct node *dt = dti->dt;
859 /* if there are labels */
862 /* now add the label in the node */
863 for_each_label(node->labels, l) {
865 /* check whether the label already exists */
866 p = get_property(an, l->label);
868 fprintf(stderr, "WARNING: label %s already"
869 " exists in /%s", l->label,
875 p = build_property(l->label,
876 data_copy_escape_string(node->fullpath,
877 strlen(node->fullpath)),
882 /* force allocation of a phandle for this node */
884 (void)get_node_phandle(dt, node);
887 for_each_child(node, c)
888 generate_label_tree_internal(dti, an, c, allocph);
891 static bool any_fixup_tree(struct dt_info *dti, struct node *node)
894 struct property *prop;
897 for_each_property(node, prop) {
898 m = prop->val.markers;
899 for_each_marker_of_type(m, REF_PHANDLE) {
900 if (!get_node_by_ref(dti->dt, m->ref))
905 for_each_child(node, c) {
906 if (any_fixup_tree(dti, c))
913 static void add_fixup_entry(struct dt_info *dti, struct node *fn,
914 struct node *node, struct property *prop,
919 /* m->ref can only be a REF_PHANDLE, but check anyway */
920 assert(m->type == REF_PHANDLE);
922 /* The format only permits fixups for references to label, not
923 * references to path */
924 if (strchr(m->ref, '/'))
925 die("Can't generate fixup for reference to path &{%s}\n",
928 /* there shouldn't be any ':' in the arguments */
929 if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
930 die("arguments should not contain ':'\n");
932 xasprintf(&entry, "%s:%s:%u",
933 node->fullpath, prop->name, m->offset);
934 append_to_property(fn, m->ref, entry, strlen(entry) + 1, TYPE_STRING);
939 static void generate_fixups_tree_internal(struct dt_info *dti,
943 struct node *dt = dti->dt;
945 struct property *prop;
947 struct node *refnode;
949 for_each_property(node, prop) {
950 m = prop->val.markers;
951 for_each_marker_of_type(m, REF_PHANDLE) {
952 refnode = get_node_by_ref(dt, m->ref);
954 add_fixup_entry(dti, fn, node, prop, m);
958 for_each_child(node, c)
959 generate_fixups_tree_internal(dti, fn, c);
962 static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
965 struct property *prop;
968 for_each_property(node, prop) {
969 m = prop->val.markers;
970 for_each_marker_of_type(m, REF_PHANDLE) {
971 if (get_node_by_ref(dti->dt, m->ref))
976 for_each_child(node, c) {
977 if (any_local_fixup_tree(dti, c))
984 static void add_local_fixup_entry(struct dt_info *dti,
985 struct node *lfn, struct node *node,
986 struct property *prop, struct marker *m,
987 struct node *refnode)
989 struct node *wn, *nwn; /* local fixup node, walk node, new */
994 /* walk back retrieving depth */
996 for (wn = node; wn; wn = wn->parent)
999 /* allocate name array */
1000 compp = xmalloc(sizeof(*compp) * depth);
1002 /* store names in the array */
1003 for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
1004 compp[i] = wn->name;
1006 /* walk the path components creating nodes if they don't exist */
1007 for (wn = lfn, i = 1; i < depth; i++, wn = nwn) {
1008 /* if no node exists, create it */
1009 nwn = get_subnode(wn, compp[i]);
1011 nwn = build_and_name_child_node(wn, compp[i]);
1016 value_32 = cpu_to_fdt32(m->offset);
1017 append_to_property(wn, prop->name, &value_32, sizeof(value_32), TYPE_UINT32);
1020 static void generate_local_fixups_tree_internal(struct dt_info *dti,
1024 struct node *dt = dti->dt;
1026 struct property *prop;
1028 struct node *refnode;
1030 for_each_property(node, prop) {
1031 m = prop->val.markers;
1032 for_each_marker_of_type(m, REF_PHANDLE) {
1033 refnode = get_node_by_ref(dt, m->ref);
1035 add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
1039 for_each_child(node, c)
1040 generate_local_fixups_tree_internal(dti, lfn, c);
1043 void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
1045 if (!any_label_tree(dti, dti->dt))
1047 generate_label_tree_internal(dti, build_root_node(dti->dt, name),
1051 void generate_fixups_tree(struct dt_info *dti, char *name)
1053 if (!any_fixup_tree(dti, dti->dt))
1055 generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
1059 void generate_local_fixups_tree(struct dt_info *dti, char *name)
1061 if (!any_local_fixup_tree(dti, dti->dt))
1063 generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name),