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