Linux Audio

Check our new training course

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