blob: 169462d8d21b9347977f22e1d200083969dd2ae6 [file] [log] [blame]
Rob Herringacfe84f2019-06-20 15:19:38 -06001// SPDX-License-Identifier: GPL-2.0-or-later
David Gibsonfc14dad2005-06-08 17:18:34 +10002/*
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
David Gibsonfc14dad2005-06-08 17:18:34 +10004 */
5
6#include "dtc.h"
Julia Lawall8e20ccf2018-11-16 17:29:59 +01007#include "srcpos.h"
David Gibsonfc14dad2005-06-08 17:18:34 +10008
9/*
10 * Tree building functions
11 */
12
David Gibson05898c62010-02-24 18:22:17 +110013void add_label(struct label **labels, char *label)
14{
Grant Likely83da1b22010-02-25 09:58:29 -070015 struct label *new;
David Gibson05898c62010-02-24 18:22:17 +110016
Grant Likely83da1b22010-02-25 09:58:29 -070017 /* Make sure the label isn't already there */
Stephen Warren45013d82012-08-07 22:50:15 -060018 for_each_label_withdel(*labels, new)
19 if (streq(new->label, label)) {
20 new->deleted = 0;
Grant Likely83da1b22010-02-25 09:58:29 -070021 return;
Stephen Warren45013d82012-08-07 22:50:15 -060022 }
Grant Likely83da1b22010-02-25 09:58:29 -070023
24 new = xmalloc(sizeof(*new));
Stephen Warren317a5d92012-09-28 12:39:22 -060025 memset(new, 0, sizeof(*new));
David Gibson05898c62010-02-24 18:22:17 +110026 new->label = label;
27 new->next = *labels;
28 *labels = new;
29}
30
Stephen Warren45013d82012-08-07 22:50:15 -060031void delete_labels(struct label **labels)
32{
33 struct label *label;
34
35 for_each_label(*labels, label)
36 label->deleted = 1;
37}
38
Julia Lawall8e20ccf2018-11-16 17:29:59 +010039struct property *build_property(char *name, struct data val,
40 struct srcpos *srcpos)
David Gibsonfc14dad2005-06-08 17:18:34 +100041{
42 struct property *new = xmalloc(sizeof(*new));
43
David Gibson05898c62010-02-24 18:22:17 +110044 memset(new, 0, sizeof(*new));
45
David Gibsonfc14dad2005-06-08 17:18:34 +100046 new->name = name;
47 new->val = val;
Julia Lawall8e20ccf2018-11-16 17:29:59 +010048 new->srcpos = srcpos_copy(srcpos);
David Gibsonfc14dad2005-06-08 17:18:34 +100049
David Gibsonfc14dad2005-06-08 17:18:34 +100050 return new;
51}
52
Stephen Warren45013d82012-08-07 22:50:15 -060053struct 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
David Gibsonfc14dad2005-06-08 17:18:34 +100065struct property *chain_property(struct property *first, struct property *list)
66{
67 assert(first->next == NULL);
David Gibson63dc9c72007-09-18 11:44:04 +100068
David Gibsonfc14dad2005-06-08 17:18:34 +100069 first->next = list;
David Gibson63dc9c72007-09-18 11:44:04 +100070 return first;
David Gibsonfc14dad2005-06-08 17:18:34 +100071}
72
Jon Loeliger7b3fb782007-10-22 16:09:56 -050073struct 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
Julia Lawall8e20ccf2018-11-16 17:29:59 +010088struct node *build_node(struct property *proplist, struct node *children,
89 struct srcpos *srcpos)
David Gibsonfc14dad2005-06-08 17:18:34 +100090{
91 struct node *new = xmalloc(sizeof(*new));
92 struct node *child;
93
94 memset(new, 0, sizeof(*new));
95
Jon Loeliger7b3fb782007-10-22 16:09:56 -050096 new->proplist = reverse_properties(proplist);
David Gibsonfc14dad2005-06-08 17:18:34 +100097 new->children = children;
Julia Lawall8e20ccf2018-11-16 17:29:59 +010098 new->srcpos = srcpos_copy(srcpos);
David Gibsonfc14dad2005-06-08 17:18:34 +100099
100 for_each_child(new, child) {
101 child->parent = new;
102 }
103
104 return new;
105}
106
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100107struct node *build_node_delete(struct srcpos *srcpos)
Stephen Warren45013d82012-08-07 22:50:15 -0600108{
109 struct node *new = xmalloc(sizeof(*new));
110
111 memset(new, 0, sizeof(*new));
112
113 new->deleted = 1;
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100114 new->srcpos = srcpos_copy(srcpos);
Stephen Warren45013d82012-08-07 22:50:15 -0600115
116 return new;
117}
118
David Gibson05898c62010-02-24 18:22:17 +1100119struct node *name_node(struct node *node, char *name)
David Gibsonfc14dad2005-06-08 17:18:34 +1000120{
121 assert(node->name == NULL);
122
123 node->name = name;
David Gibson4102d842005-06-16 14:36:37 +1000124
David Gibsonfc14dad2005-06-08 17:18:34 +1000125 return node;
126}
127
Maxime Ripard4038fd92018-05-03 22:27:26 +0200128struct 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
Grant Likely83da1b22010-02-25 09:58:29 -0700142struct 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
Stephen Warren45013d82012-08-07 22:50:15 -0600148 old_node->deleted = 0;
149
Grant Likely83da1b22010-02-25 09:58:29 -0700150 /* Add new node labels to old node */
Stephen Warren45013d82012-08-07 22:50:15 -0600151 for_each_label_withdel(new_node->labels, l)
Grant Likely83da1b22010-02-25 09:58:29 -0700152 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
Stephen Warren45013d82012-08-07 22:50:15 -0600162 if (new_prop->deleted) {
163 delete_property_by_name(old_node, new_prop->name);
164 free(new_prop);
165 continue;
166 }
167
Grant Likely83da1b22010-02-25 09:58:29 -0700168 /* Look for a collision, set new value if there is */
Stephen Warren45013d82012-08-07 22:50:15 -0600169 for_each_property_withdel(old_node, old_prop) {
Grant Likely83da1b22010-02-25 09:58:29 -0700170 if (streq(old_prop->name, new_prop->name)) {
171 /* Add new labels to old property */
Stephen Warren45013d82012-08-07 22:50:15 -0600172 for_each_label_withdel(new_prop->labels, l)
Grant Likely83da1b22010-02-25 09:58:29 -0700173 add_label(&old_prop->labels, l->label);
174
175 old_prop->val = new_prop->val;
Stephen Warren45013d82012-08-07 22:50:15 -0600176 old_prop->deleted = 0;
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100177 free(old_prop->srcpos);
178 old_prop->srcpos = new_prop->srcpos;
Grant Likely83da1b22010-02-25 09:58:29 -0700179 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
Stephen Warren45013d82012-08-07 22:50:15 -0600199 if (new_child->deleted) {
200 delete_node_by_name(old_node, new_child->name);
201 free(new_child);
202 continue;
203 }
204
Grant Likely83da1b22010-02-25 09:58:29 -0700205 /* Search for a collision. Merge if there is */
Stephen Warren45013d82012-08-07 22:50:15 -0600206 for_each_child_withdel(old_node, old_child) {
Grant Likely83da1b22010-02-25 09:58:29 -0700207 if (streq(old_child->name, new_child->name)) {
208 merge_nodes(old_child, new_child);
209 new_child = NULL;
210 break;
211 }
212 }
213
Thomas Huth45fd4402016-09-26 18:12:26 +0200214 /* if no collision occurred, add child to the old node. */
Grant Likely83da1b22010-02-25 09:58:29 -0700215 if (new_child)
216 add_child(old_node, new_child);
217 }
218
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100219 old_node->srcpos = srcpos_extend(old_node->srcpos, new_node->srcpos);
220
Grant Likely83da1b22010-02-25 09:58:29 -0700221 /* 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
Grant Likelyb260c4f2017-11-20 17:12:18 +0000228struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref)
Pantelis Antoniou737b2df2017-06-14 17:53:05 +0300229{
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
David Gibson8f1b35f2018-03-06 13:27:53 +1100236 if (ref[0] == '/') {
Rob Herring87963ee2019-05-17 15:28:04 -0500237 d = data_add_marker(d, TYPE_STRING, ref);
David Gibson8f1b35f2018-03-06 13:27:53 +1100238 d = data_append_data(d, ref, strlen(ref) + 1);
Pantelis Antoniou737b2df2017-06-14 17:53:05 +0300239
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100240 p = build_property("target-path", d, NULL);
David Gibson8f1b35f2018-03-06 13:27:53 +1100241 } else {
242 d = data_add_marker(d, REF_PHANDLE, ref);
243 d = data_append_integer(d, 0xffffffff, 32);
244
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100245 p = build_property("target", d, NULL);
David Gibson8f1b35f2018-03-06 13:27:53 +1100246 }
Pantelis Antoniou737b2df2017-06-14 17:53:05 +0300247
248 xasprintf(&name, "fragment@%u",
249 next_orphan_fragment++);
250 name_node(new_node, "__overlay__");
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100251 node = build_node(p, new_node, NULL);
Pantelis Antoniou737b2df2017-06-14 17:53:05 +0300252 name_node(node, name);
253
254 add_child(dt, node);
Grant Likelyb260c4f2017-11-20 17:12:18 +0000255 return dt;
Pantelis Antoniou737b2df2017-06-14 17:53:05 +0300256}
257
David Gibsonfc14dad2005-06-08 17:18:34 +1000258struct 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{
David Gibson740a19a2005-10-21 17:26:45 +1000268 struct property **p;
269
270 prop->next = NULL;
271
272 p = &node->proplist;
273 while (*p)
274 p = &((*p)->next);
275
276 *p = prop;
David Gibsonfc14dad2005-06-08 17:18:34 +1000277}
278
Stephen Warren45013d82012-08-07 22:50:15 -0600279void delete_property_by_name(struct node *node, char *name)
280{
281 struct property *prop = node->proplist;
282
283 while (prop) {
David Gibson120775e2017-02-13 15:57:54 +1100284 if (streq(prop->name, name)) {
Stephen Warren45013d82012-08-07 22:50:15 -0600285 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
David Gibsonfc14dad2005-06-08 17:18:34 +1000298void add_child(struct node *parent, struct node *child)
299{
David Gibson740a19a2005-10-21 17:26:45 +1000300 struct node **p;
301
302 child->next_sibling = NULL;
David Gibson4d7bea72008-07-07 11:19:13 +1000303 child->parent = parent;
David Gibson740a19a2005-10-21 17:26:45 +1000304
305 p = &parent->children;
306 while (*p)
307 p = &((*p)->next_sibling);
308
309 *p = child;
David Gibsonfc14dad2005-06-08 17:18:34 +1000310}
311
Stephen Warren45013d82012-08-07 22:50:15 -0600312void delete_node_by_name(struct node *parent, char *name)
313{
314 struct node *node = parent->children;
315
316 while (node) {
David Gibson120775e2017-02-13 15:57:54 +1100317 if (streq(node->name, name)) {
Stephen Warren45013d82012-08-07 22:50:15 -0600318 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
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200338void append_to_property(struct node *node,
Rob Herring87963ee2019-05-17 15:28:04 -0500339 char *name, const void *data, int len,
340 enum markertype type)
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200341{
342 struct data d;
343 struct property *p;
344
345 p = get_property(node, name);
346 if (p) {
Rob Herring87963ee2019-05-17 15:28:04 -0500347 d = data_add_marker(p->val, type, name);
348 d = data_append_data(d, data, len);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200349 p->val = d;
350 } else {
Rob Herring87963ee2019-05-17 15:28:04 -0500351 d = data_add_marker(empty_data, type, name);
352 d = data_append_data(d, data, len);
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100353 p = build_property(name, d, NULL);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200354 add_property(node, p);
355 }
356}
357
David Gibson05898c62010-02-24 18:22:17 +1100358struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)
David Gibsonf040d952005-10-24 18:18:38 +1000359{
360 struct reserve_info *new = xmalloc(sizeof(*new));
361
David Gibson05898c62010-02-24 18:22:17 +1100362 memset(new, 0, sizeof(*new));
363
David Gibson49300f22017-03-06 12:04:45 +1100364 new->address = address;
365 new->size = size;
David Gibsonf040d952005-10-24 18:18:38 +1000366
David Gibsonf040d952005-10-24 18:18:38 +1000367 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}
David Gibsonfc14dad2005-06-08 17:18:34 +1000396
David Gibson00fbb862016-05-31 11:58:42 +1000397struct dt_info *build_dt_info(unsigned int dtsflags,
398 struct reserve_info *reservelist,
399 struct node *tree, uint32_t boot_cpuid_phys)
David Gibson169f0b12007-10-18 17:22:30 +1000400{
David Gibson00fbb862016-05-31 11:58:42 +1000401 struct dt_info *dti;
David Gibson169f0b12007-10-18 17:22:30 +1000402
David Gibson00fbb862016-05-31 11:58:42 +1000403 dti = xmalloc(sizeof(*dti));
404 dti->dtsflags = dtsflags;
405 dti->reservelist = reservelist;
406 dti->dt = tree;
407 dti->boot_cpuid_phys = boot_cpuid_phys;
David Gibson169f0b12007-10-18 17:22:30 +1000408
David Gibson00fbb862016-05-31 11:58:42 +1000409 return dti;
David Gibson169f0b12007-10-18 17:22:30 +1000410}
411
David Gibsonfc14dad2005-06-08 17:18:34 +1000412/*
413 * Tree accessor functions
414 */
415
David Gibson92cb9a22007-12-04 14:26:15 +1100416const char *get_unitname(struct node *node)
David Gibsonfc14dad2005-06-08 17:18:34 +1000417{
418 if (node->name[node->basenamelen] == '\0')
419 return "";
420 else
421 return node->name + node->basenamelen + 1;
422}
423
David Gibson92cb9a22007-12-04 14:26:15 +1100424struct property *get_property(struct node *node, const char *propname)
David Gibsonfc14dad2005-06-08 17:18:34 +1000425{
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
David Gibson2f1ccc32007-11-01 16:49:26 +1100435cell_t propval_cell(struct property *prop)
David Gibsonfc14dad2005-06-08 17:18:34 +1000436{
437 assert(prop->val.len == sizeof(cell_t));
David Gibsonbad5b282017-03-06 12:08:53 +1100438 return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
David Gibsonfc14dad2005-06-08 17:18:34 +1000439}
440
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100441cell_t propval_cell_n(struct property *prop, unsigned int n)
Rob Herringb3bbac02017-09-01 13:53:01 -0500442{
443 assert(prop->val.len / sizeof(cell_t) >= n);
444 return fdt32_to_cpu(*((fdt32_t *)prop->val.val + n));
445}
446
David Gibson329055d2010-02-23 19:56:41 +1100447struct 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) {
David Gibson05898c62010-02-24 18:22:17 +1100456 struct label *l;
457
458 for_each_label(prop->labels, l)
459 if (streq(l->label, label))
460 return prop;
David Gibson329055d2010-02-23 19:56:41 +1100461 }
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
David Gibson92cb9a22007-12-04 14:26:15 +1100501struct node *get_subnode(struct node *node, const char *nodename)
David Gibsonfc14dad2005-06-08 17:18:34 +1000502{
503 struct node *child;
504
David Gibson81f2e892005-06-16 17:04:00 +1000505 for_each_child(node, child)
506 if (streq(child->name, nodename))
507 return child;
508
509 return NULL;
510}
511
David Gibson92cb9a22007-12-04 14:26:15 +1100512struct node *get_node_by_path(struct node *tree, const char *path)
David Gibson81f2e892005-06-16 17:04:00 +1000513{
David Gibson92cb9a22007-12-04 14:26:15 +1100514 const char *p;
David Gibson81f2e892005-06-16 17:04:00 +1000515 struct node *child;
516
Stephen Warren45013d82012-08-07 22:50:15 -0600517 if (!path || ! (*path)) {
518 if (tree->deleted)
519 return NULL;
David Gibson81f2e892005-06-16 17:04:00 +1000520 return tree;
Stephen Warren45013d82012-08-07 22:50:15 -0600521 }
David Gibson81f2e892005-06-16 17:04:00 +1000522
523 while (path[0] == '/')
524 path++;
525
526 p = strchr(path, '/');
527
528 for_each_child(tree, child) {
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100529 if (p && strprefixeq(path, (size_t)(p - path), child->name))
David Gibson81f2e892005-06-16 17:04:00 +1000530 return get_node_by_path(child, p+1);
531 else if (!p && streq(path, child->name))
David Gibsonfc14dad2005-06-08 17:18:34 +1000532 return child;
533 }
534
535 return NULL;
536}
537
David Gibsonb16a2bd2007-11-22 14:38:07 +1100538struct node *get_node_by_label(struct node *tree, const char *label)
David Gibsonc226ddc2007-02-07 14:29:07 +1100539{
540 struct node *child, *node;
David Gibson05898c62010-02-24 18:22:17 +1100541 struct label *l;
David Gibsonc226ddc2007-02-07 14:29:07 +1100542
543 assert(label && (strlen(label) > 0));
544
David Gibson05898c62010-02-24 18:22:17 +1100545 for_each_label(tree->labels, l)
546 if (streq(l->label, label))
547 return tree;
David Gibsonc226ddc2007-02-07 14:29:07 +1100548
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
David Gibson2f1ccc32007-11-01 16:49:26 +1100558struct node *get_node_by_phandle(struct node *tree, cell_t phandle)
David Gibsonfc14dad2005-06-08 17:18:34 +1000559{
David Gibson63dc9c72007-09-18 11:44:04 +1000560 struct node *child, *node;
David Gibsonfc14dad2005-06-08 17:18:34 +1000561
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100562 if (!phandle_is_valid(phandle)) {
Tero Kristobba26a52017-10-24 17:14:18 +0300563 assert(generate_fixups);
564 return NULL;
565 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000566
Stephen Warren45013d82012-08-07 22:50:15 -0600567 if (tree->phandle == phandle) {
568 if (tree->deleted)
569 return NULL;
David Gibsonfc14dad2005-06-08 17:18:34 +1000570 return tree;
Stephen Warren45013d82012-08-07 22:50:15 -0600571 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000572
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}
David Gibsonc226ddc2007-02-07 14:29:07 +1100581
David Gibson92cb9a22007-12-04 14:26:15 +1100582struct node *get_node_by_ref(struct node *tree, const char *ref)
David Gibsonb16a2bd2007-11-22 14:38:07 +1100583{
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100584 struct node *target = tree;
585 const char *label = NULL, *path = NULL;
586
David Gibsonf2405272014-05-09 20:48:49 +1000587 if (streq(ref, "/"))
588 return tree;
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100589
590 if (ref[0] == '/')
591 path = ref;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100592 else
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100593 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;
David Gibsonb16a2bd2007-11-22 14:38:07 +1100617}
618
619cell_t get_node_phandle(struct node *root, struct node *node)
David Gibson169f0b12007-10-18 17:22:30 +1000620{
621 static cell_t phandle = 1; /* FIXME: ick, static local */
Rob Herring3fe0eed2018-07-12 18:20:05 -0600622 struct data d = empty_data;
David Gibson169f0b12007-10-18 17:22:30 +1000623
Pierre-Clément Tosief1978a2022-05-30 20:57:34 +0100624 if (phandle_is_valid(node->phandle))
David Gibson169f0b12007-10-18 17:22:30 +1000625 return node->phandle;
626
David Gibson169f0b12007-10-18 17:22:30 +1000627 while (get_node_by_phandle(root, phandle))
628 phandle++;
629
630 node->phandle = phandle;
David Gibsond75b33a2009-11-26 15:37:13 +1100631
Rob Herring3fe0eed2018-07-12 18:20:05 -0600632 d = data_add_marker(d, TYPE_UINT32, NULL);
633 d = data_append_cell(d, phandle);
634
David Gibsond75b33a2009-11-26 15:37:13 +1100635 if (!get_property(node, "linux,phandle")
636 && (phandle_format & PHANDLE_LEGACY))
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100637 add_property(node, build_property("linux,phandle", d, NULL));
David Gibsond75b33a2009-11-26 15:37:13 +1100638
639 if (!get_property(node, "phandle")
640 && (phandle_format & PHANDLE_EPAPR))
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100641 add_property(node, build_property("phandle", d, NULL));
David Gibsond75b33a2009-11-26 15:37:13 +1100642
643 /* If the node *does* have a phandle property, we must
David Gibson9878f302008-11-07 12:49:44 +1100644 * be dealing with a self-referencing phandle, which will be
645 * fixed up momentarily in the caller */
David Gibson169f0b12007-10-18 17:22:30 +1000646
647 return node->phandle;
648}
David Gibson15ad6d82010-02-19 15:50:50 +1100649
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}
David Gibson37c0b6a2010-11-10 09:51:09 +1100672
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
David Gibson49300f22017-03-06 12:04:45 +1100680 if (a->address < b->address)
David Gibson37c0b6a2010-11-10 09:51:09 +1100681 return -1;
David Gibson49300f22017-03-06 12:04:45 +1100682 else if (a->address > b->address)
David Gibson37c0b6a2010-11-10 09:51:09 +1100683 return 1;
David Gibson49300f22017-03-06 12:04:45 +1100684 else if (a->size < b->size)
David Gibson37c0b6a2010-11-10 09:51:09 +1100685 return -1;
David Gibson49300f22017-03-06 12:04:45 +1100686 else if (a->size > b->size)
David Gibson37c0b6a2010-11-10 09:51:09 +1100687 return 1;
688 else
689 return 0;
690}
691
David Gibson00fbb862016-05-31 11:58:42 +1000692static void sort_reserve_entries(struct dt_info *dti)
David Gibson37c0b6a2010-11-10 09:51:09 +1100693{
694 struct reserve_info *ri, **tbl;
695 int n = 0, i = 0;
696
David Gibson00fbb862016-05-31 11:58:42 +1000697 for (ri = dti->reservelist;
David Gibson37c0b6a2010-11-10 09:51:09 +1100698 ri;
699 ri = ri->next)
700 n++;
701
702 if (n == 0)
703 return;
704
705 tbl = xmalloc(n * sizeof(*tbl));
706
David Gibson00fbb862016-05-31 11:58:42 +1000707 for (ri = dti->reservelist;
David Gibson37c0b6a2010-11-10 09:51:09 +1100708 ri;
709 ri = ri->next)
710 tbl[i++] = ri;
711
712 qsort(tbl, n, sizeof(*tbl), cmp_reserve_info);
713
David Gibson00fbb862016-05-31 11:58:42 +1000714 dti->reservelist = tbl[0];
David Gibson37c0b6a2010-11-10 09:51:09 +1100715 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
Stephen Warren45013d82012-08-07 22:50:15 -0600737 for_each_property_withdel(node, prop)
David Gibson37c0b6a2010-11-10 09:51:09 +1100738 n++;
739
740 if (n == 0)
741 return;
742
743 tbl = xmalloc(n * sizeof(*tbl));
744
Stephen Warren45013d82012-08-07 22:50:15 -0600745 for_each_property_withdel(node, prop)
David Gibson37c0b6a2010-11-10 09:51:09 +1100746 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
Stephen Warren45013d82012-08-07 22:50:15 -0600773 for_each_child_withdel(node, subnode)
David Gibson37c0b6a2010-11-10 09:51:09 +1100774 n++;
775
776 if (n == 0)
777 return;
778
779 tbl = xmalloc(n * sizeof(*tbl));
780
Stephen Warren45013d82012-08-07 22:50:15 -0600781 for_each_child_withdel(node, subnode)
David Gibson37c0b6a2010-11-10 09:51:09 +1100782 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);
Stephen Warren45013d82012-08-07 22:50:15 -0600800 for_each_child_withdel(node, c)
David Gibson37c0b6a2010-11-10 09:51:09 +1100801 sort_node(c);
802}
803
David Gibson00fbb862016-05-31 11:58:42 +1000804void sort_tree(struct dt_info *dti)
David Gibson37c0b6a2010-11-10 09:51:09 +1100805{
David Gibson00fbb862016-05-31 11:58:42 +1000806 sort_reserve_entries(dti);
807 sort_node(dti->dt);
David Gibson37c0b6a2010-11-10 09:51:09 +1100808}
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200809
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
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100815 node = build_node(NULL, NULL, NULL);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200816 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
David Gibson00fbb862016-05-31 11:58:42 +1000836static bool any_label_tree(struct dt_info *dti, struct node *node)
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200837{
838 struct node *c;
839
840 if (node->labels)
841 return true;
842
843 for_each_child(node, c)
David Gibson00fbb862016-05-31 11:58:42 +1000844 if (any_label_tree(dti, c))
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200845 return true;
846
847 return false;
848}
849
David Gibson00fbb862016-05-31 11:58:42 +1000850static void generate_label_tree_internal(struct dt_info *dti,
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200851 struct node *an, struct node *node,
852 bool allocph)
853{
David Gibson00fbb862016-05-31 11:58:42 +1000854 struct node *dt = dti->dt;
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200855 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,
Rob Herring87963ee2019-05-17 15:28:04 -0500876 data_copy_escape_string(node->fullpath,
877 strlen(node->fullpath)),
Julia Lawall8e20ccf2018-11-16 17:29:59 +0100878 NULL);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200879 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)
David Gibson00fbb862016-05-31 11:58:42 +1000888 generate_label_tree_internal(dti, an, c, allocph);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200889}
890
David Gibson00fbb862016-05-31 11:58:42 +1000891static bool any_fixup_tree(struct dt_info *dti, struct node *node)
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200892{
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) {
David Gibson00fbb862016-05-31 11:58:42 +1000900 if (!get_node_by_ref(dti->dt, m->ref))
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200901 return true;
902 }
903 }
904
905 for_each_child(node, c) {
David Gibson00fbb862016-05-31 11:58:42 +1000906 if (any_fixup_tree(dti, c))
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200907 return true;
908 }
909
910 return false;
911}
912
David Gibson00fbb862016-05-31 11:58:42 +1000913static void add_fixup_entry(struct dt_info *dti, struct node *fn,
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200914 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);
Rob Herring87963ee2019-05-17 15:28:04 -0500928 append_to_property(fn, m->ref, entry, strlen(entry) + 1, TYPE_STRING);
Jean-Christophe Dubois95d57722017-02-07 22:26:25 +0100929
930 free(entry);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200931}
932
David Gibson00fbb862016-05-31 11:58:42 +1000933static void generate_fixups_tree_internal(struct dt_info *dti,
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200934 struct node *fn,
935 struct node *node)
936{
David Gibson00fbb862016-05-31 11:58:42 +1000937 struct node *dt = dti->dt;
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200938 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)
David Gibson00fbb862016-05-31 11:58:42 +1000948 add_fixup_entry(dti, fn, node, prop, m);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200949 }
950 }
951
952 for_each_child(node, c)
David Gibson00fbb862016-05-31 11:58:42 +1000953 generate_fixups_tree_internal(dti, fn, c);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200954}
955
David Gibson00fbb862016-05-31 11:58:42 +1000956static bool any_local_fixup_tree(struct dt_info *dti, struct node *node)
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200957{
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) {
David Gibson00fbb862016-05-31 11:58:42 +1000965 if (get_node_by_ref(dti->dt, m->ref))
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200966 return true;
967 }
968 }
969
970 for_each_child(node, c) {
David Gibson00fbb862016-05-31 11:58:42 +1000971 if (any_local_fixup_tree(dti, c))
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200972 return true;
973 }
974
975 return false;
976}
977
David Gibson00fbb862016-05-31 11:58:42 +1000978static void add_local_fixup_entry(struct dt_info *dti,
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200979 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 */
David Gibsonbad5b282017-03-06 12:08:53 +1100984 fdt32_t value_32;
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200985 char **compp;
986 int i, depth;
987
Thomas Huth825146d2019-05-20 10:12:09 +0200988 /* walk back retrieving depth */
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200989 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);
Rob Herring87963ee2019-05-17 15:28:04 -05001011 append_to_property(wn, prop->name, &value_32, sizeof(value_32), TYPE_UINT32);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001012}
1013
David Gibson00fbb862016-05-31 11:58:42 +10001014static void generate_local_fixups_tree_internal(struct dt_info *dti,
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001015 struct node *lfn,
1016 struct node *node)
1017{
David Gibson00fbb862016-05-31 11:58:42 +10001018 struct node *dt = dti->dt;
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001019 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)
David Gibson00fbb862016-05-31 11:58:42 +10001029 add_local_fixup_entry(dti, lfn, node, prop, m, refnode);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001030 }
1031 }
1032
1033 for_each_child(node, c)
David Gibson00fbb862016-05-31 11:58:42 +10001034 generate_local_fixups_tree_internal(dti, lfn, c);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001035}
1036
David Gibson00fbb862016-05-31 11:58:42 +10001037void generate_label_tree(struct dt_info *dti, char *name, bool allocph)
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001038{
David Gibson00fbb862016-05-31 11:58:42 +10001039 if (!any_label_tree(dti, dti->dt))
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001040 return;
David Gibson00fbb862016-05-31 11:58:42 +10001041 generate_label_tree_internal(dti, build_root_node(dti->dt, name),
1042 dti->dt, allocph);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001043}
1044
David Gibson00fbb862016-05-31 11:58:42 +10001045void generate_fixups_tree(struct dt_info *dti, char *name)
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001046{
David Gibson00fbb862016-05-31 11:58:42 +10001047 if (!any_fixup_tree(dti, dti->dt))
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001048 return;
David Gibson00fbb862016-05-31 11:58:42 +10001049 generate_fixups_tree_internal(dti, build_root_node(dti->dt, name),
1050 dti->dt);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001051}
1052
David Gibson00fbb862016-05-31 11:58:42 +10001053void generate_local_fixups_tree(struct dt_info *dti, char *name)
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001054{
David Gibson00fbb862016-05-31 11:58:42 +10001055 if (!any_local_fixup_tree(dti, dti->dt))
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001056 return;
David Gibson00fbb862016-05-31 11:58:42 +10001057 generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name),
1058 dti->dt);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +02001059}