Lines Matching +full:a +full:- +full:child +full:- +full:node +full:- +full:property
1 // SPDX-License-Identifier: GPL-2.0-or-later
19 if (streq(new->label, label)) {
20 new->deleted = 0;
26 new->label = label;
27 new->next = *labels;
36 label->deleted = 1;
39 struct property *build_property(const char *name, struct data val,
42 struct property *new = xmalloc(sizeof(*new));
46 new->name = xstrdup(name);
47 new->val = val;
48 new->srcpos = srcpos_copy(srcpos);
53 struct property *build_property_delete(const char *name)
55 struct property *new = xmalloc(sizeof(*new));
59 new->name = xstrdup(name);
60 new->deleted = 1;
65 struct property *chain_property(struct property *first, struct property *list)
67 assert(first->next == NULL);
69 first->next = list;
73 struct property *reverse_properties(struct property *first)
75 struct property *p = first;
76 struct property *head = NULL;
77 struct property *next;
80 next = p->next;
81 p->next = head;
88 struct node *build_node(struct property *proplist, struct node *children,
91 struct node *new = xmalloc(sizeof(*new));
92 struct node *child;
96 new->proplist = reverse_properties(proplist);
97 new->children = children;
98 new->srcpos = srcpos_copy(srcpos);
100 for_each_child(new, child) {
101 child->parent = new;
107 struct node *build_node_delete(struct srcpos *srcpos)
109 struct node *new = xmalloc(sizeof(*new));
113 new->deleted = 1;
114 new->srcpos = srcpos_copy(srcpos);
119 struct node *name_node(struct node *node, const char *name)
121 assert(node->name == NULL);
123 node->name = xstrdup(name);
125 return node;
128 struct node *omit_node_if_unused(struct node *node)
130 node->omit_if_unused = 1;
132 return node;
135 struct node *reference_node(struct node *node)
137 node->is_referenced = 1;
139 return node;
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 */
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 srcpos_free(old_prop->srcpos);
178 old_prop->srcpos = new_prop->srcpos;
185 /* if no collision occurred, add property to the old node. */
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 */
207 if (streq(old_child->name, new_child->name)) {
214 /* if no collision occurred, add child to the old node. */
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
222 * the new node. */
228 struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
231 struct node *node;
232 struct property *p;
240 p = build_property("target-path", d, NULL);
251 node = build_node(p, new_node, NULL);
252 name_node(node, name);
255 add_child(dt, node);
259 struct node *chain_node(struct node *first, struct node *list)
261 assert(first->next_sibling == NULL);
263 first->next_sibling = list;
267 void add_property(struct node *node, struct property *prop)
269 struct property **p;
271 prop->next = NULL;
273 p = &node->proplist;
275 p = &((*p)->next);
280 void delete_property_by_name(struct node *node, char *name)
282 struct property *prop = node->proplist;
285 if (streq(prop->name, name)) {
289 prop = prop->next;
293 void delete_property(struct property *prop)
295 prop->deleted = 1;
296 delete_labels(&prop->labels);
299 void add_child(struct node *parent, struct node *child)
301 struct node **p;
303 child->next_sibling = NULL;
304 child->parent = parent;
306 p = &parent->children;
308 p = &((*p)->next_sibling);
310 *p = child;
313 void delete_node_by_name(struct node *parent, char *name)
315 struct node *node = parent->children;
317 while (node) {
318 if (streq(node->name, name)) {
319 delete_node(node);
322 node = node->next_sibling;
326 void delete_node(struct node *node)
328 struct property *prop;
329 struct node *child;
331 node->deleted = 1;
332 for_each_child(node, child)
333 delete_node(child);
334 for_each_property(node, prop)
336 delete_labels(&node->labels);
339 void append_to_property(struct node *node,
344 struct property *p;
346 p = get_property(node, name);
348 d = data_add_marker(p->val, type, name);
350 p->val = d;
355 add_property(node, p);
365 new->address = address;
366 new->size = size;
374 assert(first->next == NULL);
376 first->next = list;
385 new->next = NULL;
390 for (last = list; last->next; last = last->next)
393 last->next = new;
400 struct node *tree, uint32_t boot_cpuid_phys)
405 dti->dtsflags = dtsflags;
406 dti->reservelist = reservelist;
407 dti->dt = tree;
408 dti->boot_cpuid_phys = boot_cpuid_phys;
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, unsigned 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,
449 struct node **node)
451 struct property *prop;
452 struct node *c;
454 *node = tree;
459 for_each_label(prop->labels, l)
460 if (streq(l->label, label))
465 prop = get_property_by_label(c, label, node);
470 *node = NULL;
474 struct marker *get_marker_label(struct node *tree, const char *label,
475 struct node **node, struct property **prop)
478 struct property *p;
479 struct node *c;
481 *node = tree;
485 m = p->val.markers;
487 if (streq(m->ref, label))
492 m = get_marker_label(c, label, node, prop);
498 *node = NULL;
502 struct node *get_subnode(struct node *node, const char *nodename)
504 struct node *child;
506 for_each_child(node, child)
507 if (streq(child->name, nodename) && !child->deleted)
508 return child;
513 struct node *get_node_by_path(struct node *tree, const char *path)
516 struct node *child;
519 if (tree->deleted)
529 for_each_child(tree, child) {
530 if (p && strprefixeq(path, (size_t)(p - path), child->name))
531 return get_node_by_path(child, p+1);
532 else if (!p && streq(path, child->name))
533 return child;
539 struct node *get_node_by_label(struct node *tree, const char *label)
541 struct node *child, *node;
546 for_each_label(tree->labels, l)
547 if (streq(l->label, label))
550 for_each_child(tree, child) {
551 node = get_node_by_label(child, label);
552 if (node)
553 return node;
559 struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
561 struct node *child, *node;
568 if (tree->phandle == phandle) {
569 if (tree->deleted)
574 for_each_child(tree, child) {
575 node = get_node_by_phandle(child, phandle);
576 if (node)
577 return node;
583 struct node *get_node_by_ref(struct node *tree, const char *ref)
585 struct node *target = tree;
601 buf = xstrndup(label, slash - label);
620 static void add_phandle_property(struct node *node,
627 if (get_property(node, name))
631 d = data_append_cell(d, node->phandle);
633 add_property(node, build_property(name, d, NULL));
636 cell_t get_node_phandle(struct node *root, struct node *node)
640 if (phandle_is_valid(node->phandle))
641 return node->phandle;
646 node->phandle = phandle;
648 add_phandle_property(node, "linux,phandle", PHANDLE_LEGACY);
649 add_phandle_property(node, "phandle", PHANDLE_EPAPR);
651 /* If the node *does* have a phandle property, we must
652 * be dealing with a self-referencing phandle, which will be
655 return node->phandle;
658 uint32_t guess_boot_cpuid(struct node *tree)
660 struct node *cpus, *bootcpu;
661 struct property *reg;
668 bootcpu = cpus->children;
673 if (!reg || (reg->val.len != sizeof(uint32_t)))
676 /* FIXME: Sanity check node? */
683 const struct reserve_info *a, *b;
685 a = *((const struct reserve_info * const *)ax);
688 if (a->address < b->address)
689 return -1;
690 else if (a->address > b->address)
692 else if (a->size < b->size)
693 return -1;
694 else if (a->size > b->size)
705 for (ri = dti->reservelist;
707 ri = ri->next)
715 for (ri = dti->reservelist;
717 ri = ri->next)
722 dti->reservelist = tbl[0];
723 for (i = 0; i < (n-1); i++)
724 tbl[i]->next = tbl[i+1];
725 tbl[n-1]->next = NULL;
732 const struct property *a, *b;
734 a = *((const struct property * const *)ax);
735 b = *((const struct property * const *)bx);
737 return strcmp(a->name, b->name);
740 static void sort_properties(struct node *node)
743 struct property *prop, **tbl;
745 for_each_property_withdel(node, prop)
753 for_each_property_withdel(node, prop)
758 node->proplist = tbl[0];
759 for (i = 0; i < (n-1); i++)
760 tbl[i]->next = tbl[i+1];
761 tbl[n-1]->next = NULL;
768 const struct node *a, *b;
770 a = *((const struct node * const *)ax);
771 b = *((const struct node * const *)bx);
773 return strcmp(a->name, b->name);
776 static void sort_subnodes(struct node *node)
779 struct node *subnode, **tbl;
781 for_each_child_withdel(node, subnode)
789 for_each_child_withdel(node, subnode)
794 node->children = tbl[0];
795 for (i = 0; i < (n-1); i++)
796 tbl[i]->next_sibling = tbl[i+1];
797 tbl[n-1]->next_sibling = NULL;
802 static void sort_node(struct node *node)
804 struct node *c;
806 sort_properties(node);
807 sort_subnodes(node);
808 for_each_child_withdel(node, c)
815 sort_node(dti->dt);
819 static struct node *build_and_name_child_node(struct node *parent, const char *name)
821 struct node *node;
823 node = build_node(NULL, NULL, NULL);
824 name_node(node, name);
825 add_child(parent, node);
827 return node;
830 static struct node *build_root_node(struct node *dt, const char *name)
832 struct node *an;
839 die("Could not build root node /%s\n", name);
844 static bool any_label_tree(struct dt_info *dti, struct node *node)
846 struct node *c;
848 if (node->labels)
851 for_each_child(node, c)
859 struct node *an, struct node *node,
862 struct node *dt = dti->dt;
863 struct node *c;
864 struct property *p;
868 if (node->labels) {
870 /* now add the label in the node */
871 for_each_label(node->labels, l) {
874 p = get_property(an, l->label);
877 " exists in /%s", l->label,
878 an->name);
883 p = build_property(l->label,
884 data_copy_escape_string(node->fullpath,
885 strlen(node->fullpath)),
890 /* force allocation of a phandle for this node */
892 (void)get_node_phandle(dt, node);
895 for_each_child(node, c)
899 static bool any_fixup_tree(struct dt_info *dti, struct node *node)
901 struct node *c;
902 struct property *prop;
905 for_each_property(node, prop) {
906 m = prop->val.markers;
908 if (!get_node_by_ref(dti->dt, m->ref))
913 for_each_child(node, c) {
921 static void add_fixup_entry(struct dt_info *dti, struct node *fn,
922 struct node *node, struct property *prop,
927 /* m->ref can only be a REF_PHANDLE, but check anyway */
928 assert(m->type == REF_PHANDLE);
932 if (strchr(m->ref, '/'))
934 m->ref);
937 if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
941 node->fullpath, prop->name, m->offset);
942 append_to_property(fn, m->ref, entry, strlen(entry) + 1, TYPE_STRING);
948 struct node *fn,
949 struct node *node)
951 struct node *dt = dti->dt;
952 struct node *c;
953 struct property *prop;
955 struct node *refnode;
957 for_each_property(node, prop) {
958 m = prop->val.markers;
960 refnode = get_node_by_ref(dt, m->ref);
962 add_fixup_entry(dti, fn, node, prop, m);
966 for_each_child(node, c)
970 static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
972 struct node *c;
973 struct property *prop;
976 for_each_property(node, prop) {
977 m = prop->val.markers;
979 if (get_node_by_ref(dti->dt, m->ref))
984 for_each_child(node, c) {
993 struct node *lfn, struct node *node,
994 struct property *prop, struct marker *m,
995 struct node *refnode)
997 struct node *wn, *nwn; /* local fixup node, walk node, new */
1004 for (wn = node; wn; wn = wn->parent)
1011 for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
1012 compp[i] = wn->name;
1016 /* if no node exists, create it */
1022 value_32 = cpu_to_fdt32(m->offset);
1023 append_to_property(wn, prop->name, &value_32, sizeof(value_32), TYPE_UINT32);
1027 struct node *lfn,
1028 struct node *node)
1030 struct node *dt = dti->dt;
1031 struct node *c;
1032 struct property *prop;
1034 struct node *refnode;
1036 for_each_property(node, prop) {
1037 m = prop->val.markers;
1039 refnode = get_node_by_ref(dt, m->ref);
1041 add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
1045 for_each_child(node, c)
1051 if (!any_label_tree(dti, dti->dt))
1053 generate_label_tree_internal(dti, build_root_node(dti->dt, name),
1054 dti->dt, allocph);
1059 struct node *n = get_subnode(dti->dt, name);
1061 /* Start with an empty __fixups__ node to not get duplicates */
1063 n->deleted = true;
1065 if (!any_fixup_tree(dti, dti->dt))
1068 build_and_name_child_node(dti->dt, name),
1069 dti->dt);
1074 struct node *n = get_subnode(dti->dt, name);
1076 /* Start with an empty __local_fixups__ node to not get duplicates */
1078 n->deleted = true;
1079 if (!any_local_fixup_tree(dti, dti->dt))
1082 build_and_name_child_node(dti->dt, name),
1083 dti->dt);