Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include "dtc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "srcpos.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Tree building functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) void add_label(struct label **labels, char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 	struct label *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 	/* Make sure the label isn't already there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 	for_each_label_withdel(*labels, new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 		if (streq(new->label, label)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 			new->deleted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 	new = xmalloc(sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 	memset(new, 0, sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 	new->label = label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	new->next = *labels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	*labels = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) void delete_labels(struct label **labels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	struct label *label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	for_each_label(*labels, label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 		label->deleted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) struct property *build_property(char *name, struct data val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 				struct srcpos *srcpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	struct property *new = xmalloc(sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	memset(new, 0, sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	new->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	new->val = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	new->srcpos = srcpos_copy(srcpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) struct property *build_property_delete(char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	struct property *new = xmalloc(sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	memset(new, 0, sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	new->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	new->deleted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) struct property *chain_property(struct property *first, struct property *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	assert(first->next == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	first->next = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	return first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) struct property *reverse_properties(struct property *first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	struct property *p = first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	struct property *head = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	struct property *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 		next = p->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 		p->next = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		head = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		p = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	return head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) struct node *build_node(struct property *proplist, struct node *children,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 			struct srcpos *srcpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	struct node *new = xmalloc(sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	struct node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	memset(new, 0, sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	new->proplist = reverse_properties(proplist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	new->children = children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	new->srcpos = srcpos_copy(srcpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	for_each_child(new, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		child->parent = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) struct node *build_node_delete(struct srcpos *srcpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	struct node *new = xmalloc(sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	memset(new, 0, sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	new->deleted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	new->srcpos = srcpos_copy(srcpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) struct node *name_node(struct node *node, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	assert(node->name == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	node->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) struct node *omit_node_if_unused(struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	node->omit_if_unused = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) struct node *reference_node(struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	node->is_referenced = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) struct node *merge_nodes(struct node *old_node, struct node *new_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	struct property *new_prop, *old_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	struct node *new_child, *old_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	struct label *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	old_node->deleted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	/* Add new node labels to old node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	for_each_label_withdel(new_node->labels, l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		add_label(&old_node->labels, l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	/* Move properties from the new node to the old node.  If there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	 * is a collision, replace the old value with the new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	while (new_node->proplist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		/* Pop the property off the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		new_prop = new_node->proplist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		new_node->proplist = new_prop->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		new_prop->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		if (new_prop->deleted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 			delete_property_by_name(old_node, new_prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 			free(new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		/* Look for a collision, set new value if there is */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		for_each_property_withdel(old_node, old_prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			if (streq(old_prop->name, new_prop->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 				/* Add new labels to old property */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 				for_each_label_withdel(new_prop->labels, l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 					add_label(&old_prop->labels, l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 				old_prop->val = new_prop->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 				old_prop->deleted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 				free(old_prop->srcpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 				old_prop->srcpos = new_prop->srcpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 				free(new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 				new_prop = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		/* if no collision occurred, add property to the old node. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		if (new_prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 			add_property(old_node, new_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	/* Move the override child nodes into the primary node.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	 * there is a collision, then merge the nodes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	while (new_node->children) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		/* Pop the child node off the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		new_child = new_node->children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		new_node->children = new_child->next_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		new_child->parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		new_child->next_sibling = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		if (new_child->deleted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 			delete_node_by_name(old_node, new_child->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 			free(new_child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		/* Search for a collision.  Merge if there is */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		for_each_child_withdel(old_node, old_child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			if (streq(old_child->name, new_child->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 				merge_nodes(old_child, new_child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 				new_child = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		/* if no collision occurred, add child to the old node. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		if (new_child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 			add_child(old_node, new_child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	old_node->srcpos = srcpos_extend(old_node->srcpos, new_node->srcpos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	/* The new node contents are now merged into the old node.  Free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	 * the new node. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	free(new_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	return old_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	static unsigned int next_orphan_fragment = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	struct node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	struct property *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	struct data d = empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	if (ref[0] == '/') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		d = data_add_marker(d, TYPE_STRING, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		d = data_append_data(d, ref, strlen(ref) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		p = build_property("target-path", d, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		d = data_add_marker(d, REF_PHANDLE, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		d = data_append_integer(d, 0xffffffff, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		p = build_property("target", d, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	xasprintf(&name, "fragment@%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 			next_orphan_fragment++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	name_node(new_node, "__overlay__");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	node = build_node(p, new_node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	name_node(node, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	add_child(dt, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	return dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) struct node *chain_node(struct node *first, struct node *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	assert(first->next_sibling == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	first->next_sibling = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	return first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) void add_property(struct node *node, struct property *prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	struct property **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	prop->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	p = &node->proplist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	while (*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		p = &((*p)->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	*p = prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) void delete_property_by_name(struct node *node, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	struct property *prop = node->proplist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	while (prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		if (streq(prop->name, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			delete_property(prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		prop = prop->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) void delete_property(struct property *prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	prop->deleted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	delete_labels(&prop->labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) void add_child(struct node *parent, struct node *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	struct node **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	child->next_sibling = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	child->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	p = &parent->children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	while (*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		p = &((*p)->next_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	*p = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) void delete_node_by_name(struct node *parent, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	struct node *node = parent->children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		if (streq(node->name, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 			delete_node(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		node = node->next_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) void delete_node(struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	struct node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	node->deleted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	for_each_child(node, child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		delete_node(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	for_each_property(node, prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		delete_property(prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	delete_labels(&node->labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) void append_to_property(struct node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 			char *name, const void *data, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 			enum markertype type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	struct data d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	struct property *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	p = get_property(node, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		d = data_add_marker(p->val, type, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		d = data_append_data(d, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		p->val = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		d = data_add_marker(empty_data, type, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		d = data_append_data(d, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		p = build_property(name, d, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		add_property(node, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	struct reserve_info *new = xmalloc(sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	memset(new, 0, sizeof(*new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	new->address = address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	new->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) struct reserve_info *chain_reserve_entry(struct reserve_info *first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 					struct reserve_info *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	assert(first->next == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	first->next = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	return first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) struct reserve_info *add_reserve_entry(struct reserve_info *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 				      struct reserve_info *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	struct reserve_info *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	new->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	if (! list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		return new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	for (last = list; last->next; last = last->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	last->next = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	return list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) struct dt_info *build_dt_info(unsigned int dtsflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 			      struct reserve_info *reservelist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 			      struct node *tree, uint32_t boot_cpuid_phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	struct dt_info *dti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	dti = xmalloc(sizeof(*dti));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	dti->dtsflags = dtsflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	dti->reservelist = reservelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	dti->dt = tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	dti->boot_cpuid_phys = boot_cpuid_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	return dti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413)  * Tree accessor functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) const char *get_unitname(struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	if (node->name[node->basenamelen] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		return "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		return node->name + node->basenamelen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) struct property *get_property(struct node *node, const char *propname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	for_each_property(node, prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		if (streq(prop->name, propname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 			return prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) cell_t propval_cell(struct property *prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	assert(prop->val.len == sizeof(cell_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) cell_t propval_cell_n(struct property *prop, unsigned int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	assert(prop->val.len / sizeof(cell_t) >= n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	return fdt32_to_cpu(*((fdt32_t *)prop->val.val + n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) struct property *get_property_by_label(struct node *tree, const char *label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 				       struct node **node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	struct node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	*node = tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	for_each_property(tree, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		struct label *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		for_each_label(prop->labels, l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 			if (streq(l->label, label))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 				return prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	for_each_child(tree, c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		prop = get_property_by_label(c, label, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		if (prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 			return prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	*node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) struct marker *get_marker_label(struct node *tree, const char *label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 				struct node **node, struct property **prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	struct marker *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	struct property *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	struct node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	*node = tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	for_each_property(tree, p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		*prop = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		m = p->val.markers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		for_each_marker_of_type(m, LABEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 			if (streq(m->ref, label))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 				return m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	for_each_child(tree, c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		m = get_marker_label(c, label, node, prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		if (m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 			return m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	*prop = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	*node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) struct node *get_subnode(struct node *node, const char *nodename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	for_each_child(node, child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		if (streq(child->name, nodename))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 			return child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) struct node *get_node_by_path(struct node *tree, const char *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	struct node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	if (!path || ! (*path)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		if (tree->deleted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		return tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	while (path[0] == '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		path++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	p = strchr(path, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	for_each_child(tree, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		if (p && strprefixeq(path, p - path, child->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 			return get_node_by_path(child, p+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		else if (!p && streq(path, child->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 			return child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) struct node *get_node_by_label(struct node *tree, const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	struct node *child, *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	struct label *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	assert(label && (strlen(label) > 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	for_each_label(tree->labels, l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		if (streq(l->label, label))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			return tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	for_each_child(tree, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		node = get_node_by_label(child, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		if (node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 			return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	struct node *child, *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	if ((phandle == 0) || (phandle == -1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		assert(generate_fixups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	if (tree->phandle == phandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		if (tree->deleted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		return tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	for_each_child(tree, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		node = get_node_by_phandle(child, phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		if (node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) struct node *get_node_by_ref(struct node *tree, const char *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	if (streq(ref, "/"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		return tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	else if (ref[0] == '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		return get_node_by_path(tree, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		return get_node_by_label(tree, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) cell_t get_node_phandle(struct node *root, struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	static cell_t phandle = 1; /* FIXME: ick, static local */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	struct data d = empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	if ((node->phandle != 0) && (node->phandle != -1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		return node->phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	while (get_node_by_phandle(root, phandle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		phandle++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	node->phandle = phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	d = data_add_marker(d, TYPE_UINT32, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	d = data_append_cell(d, phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	if (!get_property(node, "linux,phandle")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	    && (phandle_format & PHANDLE_LEGACY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		add_property(node, build_property("linux,phandle", d, NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (!get_property(node, "phandle")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	    && (phandle_format & PHANDLE_EPAPR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		add_property(node, build_property("phandle", d, NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	/* If the node *does* have a phandle property, we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	 * be dealing with a self-referencing phandle, which will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	 * fixed up momentarily in the caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	return node->phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) uint32_t guess_boot_cpuid(struct node *tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	struct node *cpus, *bootcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	struct property *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	cpus = get_node_by_path(tree, "/cpus");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	if (!cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	bootcpu = cpus->children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	if (!bootcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	reg = get_property(bootcpu, "reg");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	if (!reg || (reg->val.len != sizeof(uint32_t)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	/* FIXME: Sanity check node? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	return propval_cell(reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) static int cmp_reserve_info(const void *ax, const void *bx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	const struct reserve_info *a, *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	a = *((const struct reserve_info * const *)ax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	b = *((const struct reserve_info * const *)bx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	if (a->address < b->address)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	else if (a->address > b->address)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	else if (a->size < b->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	else if (a->size > b->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) static void sort_reserve_entries(struct dt_info *dti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	struct reserve_info *ri, **tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	int n = 0, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	for (ri = dti->reservelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	     ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	     ri = ri->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	tbl = xmalloc(n * sizeof(*tbl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	for (ri = dti->reservelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	     ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	     ri = ri->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		tbl[i++] = ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	dti->reservelist = tbl[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	for (i = 0; i < (n-1); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		tbl[i]->next = tbl[i+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	tbl[n-1]->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	free(tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) static int cmp_prop(const void *ax, const void *bx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	const struct property *a, *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	a = *((const struct property * const *)ax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	b = *((const struct property * const *)bx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	return strcmp(a->name, b->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) static void sort_properties(struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	int n = 0, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	struct property *prop, **tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	for_each_property_withdel(node, prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	tbl = xmalloc(n * sizeof(*tbl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	for_each_property_withdel(node, prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		tbl[i++] = prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	qsort(tbl, n, sizeof(*tbl), cmp_prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	node->proplist = tbl[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	for (i = 0; i < (n-1); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		tbl[i]->next = tbl[i+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	tbl[n-1]->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	free(tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) static int cmp_subnode(const void *ax, const void *bx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	const struct node *a, *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	a = *((const struct node * const *)ax);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	b = *((const struct node * const *)bx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	return strcmp(a->name, b->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) static void sort_subnodes(struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	int n = 0, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	struct node *subnode, **tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	for_each_child_withdel(node, subnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	tbl = xmalloc(n * sizeof(*tbl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	for_each_child_withdel(node, subnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		tbl[i++] = subnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	qsort(tbl, n, sizeof(*tbl), cmp_subnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	node->children = tbl[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	for (i = 0; i < (n-1); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		tbl[i]->next_sibling = tbl[i+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	tbl[n-1]->next_sibling = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	free(tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) static void sort_node(struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	struct node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	sort_properties(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	sort_subnodes(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	for_each_child_withdel(node, c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		sort_node(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) void sort_tree(struct dt_info *dti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	sort_reserve_entries(dti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	sort_node(dti->dt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) /* utility helper to avoid code duplication */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) static struct node *build_and_name_child_node(struct node *parent, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	struct node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	node = build_node(NULL, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	name_node(node, xstrdup(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	add_child(parent, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) static struct node *build_root_node(struct node *dt, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	struct node *an;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	an = get_subnode(dt, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (!an)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		an = build_and_name_child_node(dt, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	if (!an)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		die("Could not build root node /%s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	return an;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) static bool any_label_tree(struct dt_info *dti, struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	struct node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (node->labels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	for_each_child(node, c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		if (any_label_tree(dti, c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) static void generate_label_tree_internal(struct dt_info *dti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 					 struct node *an, struct node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 					 bool allocph)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	struct node *dt = dti->dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	struct node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	struct property *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	struct label *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	/* if there are labels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	if (node->labels) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		/* now add the label in the node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		for_each_label(node->labels, l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			/* check whether the label already exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			p = get_property(an, l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 				fprintf(stderr, "WARNING: label %s already"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 					" exists in /%s", l->label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 					an->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 			/* insert it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 			p = build_property(l->label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 				data_copy_escape_string(node->fullpath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 						strlen(node->fullpath)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 				NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 			add_property(an, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		/* force allocation of a phandle for this node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		if (allocph)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			(void)get_node_phandle(dt, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	for_each_child(node, c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		generate_label_tree_internal(dti, an, c, allocph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) static bool any_fixup_tree(struct dt_info *dti, struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	struct node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	struct marker *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	for_each_property(node, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		m = prop->val.markers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		for_each_marker_of_type(m, REF_PHANDLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 			if (!get_node_by_ref(dti->dt, m->ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 				return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	for_each_child(node, c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		if (any_fixup_tree(dti, c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) static void add_fixup_entry(struct dt_info *dti, struct node *fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			    struct node *node, struct property *prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			    struct marker *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	char *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	/* m->ref can only be a REF_PHANDLE, but check anyway */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	assert(m->type == REF_PHANDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	/* there shouldn't be any ':' in the arguments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	if (strchr(node->fullpath, ':') || strchr(prop->name, ':'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		die("arguments should not contain ':'\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	xasprintf(&entry, "%s:%s:%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			node->fullpath, prop->name, m->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	append_to_property(fn, m->ref, entry, strlen(entry) + 1, TYPE_STRING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	free(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) static void generate_fixups_tree_internal(struct dt_info *dti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 					  struct node *fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 					  struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	struct node *dt = dti->dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	struct node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	struct marker *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	struct node *refnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	for_each_property(node, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		m = prop->val.markers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		for_each_marker_of_type(m, REF_PHANDLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			refnode = get_node_by_ref(dt, m->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			if (!refnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 				add_fixup_entry(dti, fn, node, prop, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	for_each_child(node, c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		generate_fixups_tree_internal(dti, fn, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	struct node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	struct marker *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	for_each_property(node, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		m = prop->val.markers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		for_each_marker_of_type(m, REF_PHANDLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			if (get_node_by_ref(dti->dt, m->ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 				return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	for_each_child(node, c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		if (any_local_fixup_tree(dti, c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) static void add_local_fixup_entry(struct dt_info *dti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		struct node *lfn, struct node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		struct property *prop, struct marker *m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		struct node *refnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	struct node *wn, *nwn;	/* local fixup node, walk node, new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	fdt32_t value_32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	char **compp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	int i, depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	/* walk back retrieving depth */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	for (wn = node; wn; wn = wn->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	/* allocate name array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	compp = xmalloc(sizeof(*compp) * depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	/* store names in the array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	for (wn = node, i = depth - 1; wn; wn = wn->parent, i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		compp[i] = wn->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	/* walk the path components creating nodes if they don't exist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	for (wn = lfn, i = 1; i < depth; i++, wn = nwn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		/* if no node exists, create it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		nwn = get_subnode(wn, compp[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		if (!nwn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 			nwn = build_and_name_child_node(wn, compp[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	free(compp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	value_32 = cpu_to_fdt32(m->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	append_to_property(wn, prop->name, &value_32, sizeof(value_32), TYPE_UINT32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) static void generate_local_fixups_tree_internal(struct dt_info *dti,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 						struct node *lfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 						struct node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	struct node *dt = dti->dt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	struct node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	struct marker *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	struct node *refnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	for_each_property(node, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		m = prop->val.markers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		for_each_marker_of_type(m, REF_PHANDLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			refnode = get_node_by_ref(dt, m->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			if (refnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 				add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	for_each_child(node, c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		generate_local_fixups_tree_internal(dti, lfn, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	if (!any_label_tree(dti, dti->dt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	generate_label_tree_internal(dti, build_root_node(dti->dt, name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 				     dti->dt, allocph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) void generate_fixups_tree(struct dt_info *dti, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	if (!any_fixup_tree(dti, dti->dt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 				      dti->dt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) void generate_local_fixups_tree(struct dt_info *dti, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	if (!any_local_fixup_tree(dti, dti->dt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 					    dti->dt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) }