Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
Maxime Ripard | a96fe17 | 2020-01-21 10:23:17 +0000 | [diff] [blame] | 137 | struct node *omit_node_if_unused(struct node *node) |
| 138 | { |
| 139 | node->omit_if_unused = 1; |
| 140 | |
| 141 | return node; |
| 142 | } |
| 143 | |
| 144 | struct node *reference_node(struct node *node) |
| 145 | { |
| 146 | node->is_referenced = 1; |
| 147 | |
| 148 | return node; |
| 149 | } |
| 150 | |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 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 | |
Rob Herring | cd5f70d | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 233 | struct node * add_orphan_node(struct node *dt, struct node *new_node, char *ref) |
Masahiro Yamada | 09a9dca | 2017-10-17 13:42:42 +0900 | [diff] [blame] | 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 | |
Rob Herring | 3f8ede9 | 2020-03-11 18:11:16 -0400 | [diff] [blame] | 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); |
Masahiro Yamada | 09a9dca | 2017-10-17 13:42:42 +0900 | [diff] [blame] | 248 | |
Rob Herring | 3f8ede9 | 2020-03-11 18:11:16 -0400 | [diff] [blame] | 249 | p = build_property("target", d); |
| 250 | } |
Masahiro Yamada | 09a9dca | 2017-10-17 13:42:42 +0900 | [diff] [blame] | 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); |
Rob Herring | cd5f70d | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 259 | return dt; |
Masahiro Yamada | 09a9dca | 2017-10-17 13:42:42 +0900 | [diff] [blame] | 260 | } |
| 261 | |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 296 | void delete_property(struct property *prop) |
| 297 | { |
| 298 | prop->deleted = 1; |
| 299 | delete_labels(&prop->labels); |
| 300 | } |
| 301 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
Tom Rini | 56aeab5 | 2017-09-23 17:30:53 -0400 | [diff] [blame] | 365 | new->address = address; |
| 366 | new->size = size; |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 367 | |
| 368 | return new; |
| 369 | } |
| 370 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 436 | cell_t propval_cell(struct property *prop) |
| 437 | { |
| 438 | assert(prop->val.len == sizeof(cell_t)); |
Tom Rini | 56aeab5 | 2017-09-23 17:30:53 -0400 | [diff] [blame] | 439 | return fdt32_to_cpu(*((fdt32_t *)prop->val.val)); |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Masahiro Yamada | 09a9dca | 2017-10-17 13:42:42 +0900 | [diff] [blame] | 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 | |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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) { |
Tom Rini | 0eb096d | 2017-09-23 17:31:59 -0400 | [diff] [blame] | 530 | if (p && (strlen(child->name) == p-path) && |
Rob Herring | cd5f70d | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 531 | strprefixeq(path, p - path, child->name)) |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 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 | |
| 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 | |
| 560 | struct node *get_node_by_phandle(struct node *tree, cell_t phandle) |
| 561 | { |
| 562 | struct node *child, *node; |
| 563 | |
Rob Herring | cd5f70d | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 564 | if ((phandle == 0) || (phandle == -1)) { |
| 565 | assert(generate_fixups); |
| 566 | return NULL; |
| 567 | } |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 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 | |
| 584 | struct node *get_node_by_ref(struct node *tree, const char *ref) |
| 585 | { |
| 586 | if (streq(ref, "/")) |
| 587 | return tree; |
| 588 | else if (ref[0] == '/') |
| 589 | return get_node_by_path(tree, ref); |
| 590 | else |
| 591 | return get_node_by_label(tree, ref); |
| 592 | } |
| 593 | |
| 594 | cell_t get_node_phandle(struct node *root, struct node *node) |
| 595 | { |
| 596 | static cell_t phandle = 1; /* FIXME: ick, static local */ |
| 597 | |
| 598 | if ((node->phandle != 0) && (node->phandle != -1)) |
| 599 | return node->phandle; |
| 600 | |
| 601 | while (get_node_by_phandle(root, phandle)) |
| 602 | phandle++; |
| 603 | |
| 604 | node->phandle = phandle; |
| 605 | |
| 606 | if (!get_property(node, "linux,phandle") |
| 607 | && (phandle_format & PHANDLE_LEGACY)) |
| 608 | add_property(node, |
| 609 | build_property("linux,phandle", |
| 610 | data_append_cell(empty_data, phandle))); |
| 611 | |
| 612 | if (!get_property(node, "phandle") |
| 613 | && (phandle_format & PHANDLE_EPAPR)) |
| 614 | add_property(node, |
| 615 | build_property("phandle", |
| 616 | data_append_cell(empty_data, phandle))); |
| 617 | |
| 618 | /* If the node *does* have a phandle property, we must |
| 619 | * be dealing with a self-referencing phandle, which will be |
| 620 | * fixed up momentarily in the caller */ |
| 621 | |
| 622 | return node->phandle; |
| 623 | } |
| 624 | |
| 625 | uint32_t guess_boot_cpuid(struct node *tree) |
| 626 | { |
| 627 | struct node *cpus, *bootcpu; |
| 628 | struct property *reg; |
| 629 | |
| 630 | cpus = get_node_by_path(tree, "/cpus"); |
| 631 | if (!cpus) |
| 632 | return 0; |
| 633 | |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 634 | bootcpu = cpus->children; |
| 635 | if (!bootcpu) |
| 636 | return 0; |
| 637 | |
| 638 | reg = get_property(bootcpu, "reg"); |
| 639 | if (!reg || (reg->val.len != sizeof(uint32_t))) |
| 640 | return 0; |
| 641 | |
| 642 | /* FIXME: Sanity check node? */ |
| 643 | |
| 644 | return propval_cell(reg); |
| 645 | } |
| 646 | |
| 647 | static int cmp_reserve_info(const void *ax, const void *bx) |
| 648 | { |
| 649 | const struct reserve_info *a, *b; |
| 650 | |
| 651 | a = *((const struct reserve_info * const *)ax); |
| 652 | b = *((const struct reserve_info * const *)bx); |
| 653 | |
Tom Rini | 56aeab5 | 2017-09-23 17:30:53 -0400 | [diff] [blame] | 654 | if (a->address < b->address) |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 655 | return -1; |
Tom Rini | 56aeab5 | 2017-09-23 17:30:53 -0400 | [diff] [blame] | 656 | else if (a->address > b->address) |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 657 | return 1; |
Tom Rini | 56aeab5 | 2017-09-23 17:30:53 -0400 | [diff] [blame] | 658 | else if (a->size < b->size) |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 659 | return -1; |
Tom Rini | 56aeab5 | 2017-09-23 17:30:53 -0400 | [diff] [blame] | 660 | else if (a->size > b->size) |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 661 | return 1; |
| 662 | else |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static void sort_reserve_entries(struct dt_info *dti) |
| 667 | { |
| 668 | struct reserve_info *ri, **tbl; |
| 669 | int n = 0, i = 0; |
| 670 | |
| 671 | for (ri = dti->reservelist; |
| 672 | ri; |
| 673 | ri = ri->next) |
| 674 | n++; |
| 675 | |
| 676 | if (n == 0) |
| 677 | return; |
| 678 | |
| 679 | tbl = xmalloc(n * sizeof(*tbl)); |
| 680 | |
| 681 | for (ri = dti->reservelist; |
| 682 | ri; |
| 683 | ri = ri->next) |
| 684 | tbl[i++] = ri; |
| 685 | |
| 686 | qsort(tbl, n, sizeof(*tbl), cmp_reserve_info); |
| 687 | |
| 688 | dti->reservelist = tbl[0]; |
| 689 | for (i = 0; i < (n-1); i++) |
| 690 | tbl[i]->next = tbl[i+1]; |
| 691 | tbl[n-1]->next = NULL; |
| 692 | |
| 693 | free(tbl); |
| 694 | } |
| 695 | |
| 696 | static int cmp_prop(const void *ax, const void *bx) |
| 697 | { |
| 698 | const struct property *a, *b; |
| 699 | |
| 700 | a = *((const struct property * const *)ax); |
| 701 | b = *((const struct property * const *)bx); |
| 702 | |
| 703 | return strcmp(a->name, b->name); |
| 704 | } |
| 705 | |
| 706 | static void sort_properties(struct node *node) |
| 707 | { |
| 708 | int n = 0, i = 0; |
| 709 | struct property *prop, **tbl; |
| 710 | |
| 711 | for_each_property_withdel(node, prop) |
| 712 | n++; |
| 713 | |
| 714 | if (n == 0) |
| 715 | return; |
| 716 | |
| 717 | tbl = xmalloc(n * sizeof(*tbl)); |
| 718 | |
| 719 | for_each_property_withdel(node, prop) |
| 720 | tbl[i++] = prop; |
| 721 | |
| 722 | qsort(tbl, n, sizeof(*tbl), cmp_prop); |
| 723 | |
| 724 | node->proplist = tbl[0]; |
| 725 | for (i = 0; i < (n-1); i++) |
| 726 | tbl[i]->next = tbl[i+1]; |
| 727 | tbl[n-1]->next = NULL; |
| 728 | |
| 729 | free(tbl); |
| 730 | } |
| 731 | |
| 732 | static int cmp_subnode(const void *ax, const void *bx) |
| 733 | { |
| 734 | const struct node *a, *b; |
| 735 | |
| 736 | a = *((const struct node * const *)ax); |
| 737 | b = *((const struct node * const *)bx); |
| 738 | |
| 739 | return strcmp(a->name, b->name); |
| 740 | } |
| 741 | |
| 742 | static void sort_subnodes(struct node *node) |
| 743 | { |
| 744 | int n = 0, i = 0; |
| 745 | struct node *subnode, **tbl; |
| 746 | |
| 747 | for_each_child_withdel(node, subnode) |
| 748 | n++; |
| 749 | |
| 750 | if (n == 0) |
| 751 | return; |
| 752 | |
| 753 | tbl = xmalloc(n * sizeof(*tbl)); |
| 754 | |
| 755 | for_each_child_withdel(node, subnode) |
| 756 | tbl[i++] = subnode; |
| 757 | |
| 758 | qsort(tbl, n, sizeof(*tbl), cmp_subnode); |
| 759 | |
| 760 | node->children = tbl[0]; |
| 761 | for (i = 0; i < (n-1); i++) |
| 762 | tbl[i]->next_sibling = tbl[i+1]; |
| 763 | tbl[n-1]->next_sibling = NULL; |
| 764 | |
| 765 | free(tbl); |
| 766 | } |
| 767 | |
| 768 | static void sort_node(struct node *node) |
| 769 | { |
| 770 | struct node *c; |
| 771 | |
| 772 | sort_properties(node); |
| 773 | sort_subnodes(node); |
| 774 | for_each_child_withdel(node, c) |
| 775 | sort_node(c); |
| 776 | } |
| 777 | |
| 778 | void sort_tree(struct dt_info *dti) |
| 779 | { |
| 780 | sort_reserve_entries(dti); |
| 781 | sort_node(dti->dt); |
| 782 | } |
| 783 | |
| 784 | /* utility helper to avoid code duplication */ |
| 785 | static struct node *build_and_name_child_node(struct node *parent, char *name) |
| 786 | { |
| 787 | struct node *node; |
| 788 | |
| 789 | node = build_node(NULL, NULL); |
| 790 | name_node(node, xstrdup(name)); |
| 791 | add_child(parent, node); |
| 792 | |
| 793 | return node; |
| 794 | } |
| 795 | |
| 796 | static struct node *build_root_node(struct node *dt, char *name) |
| 797 | { |
| 798 | struct node *an; |
| 799 | |
| 800 | an = get_subnode(dt, name); |
| 801 | if (!an) |
| 802 | an = build_and_name_child_node(dt, name); |
| 803 | |
| 804 | if (!an) |
| 805 | die("Could not build root node /%s\n", name); |
| 806 | |
| 807 | return an; |
| 808 | } |
| 809 | |
| 810 | static bool any_label_tree(struct dt_info *dti, struct node *node) |
| 811 | { |
| 812 | struct node *c; |
| 813 | |
| 814 | if (node->labels) |
| 815 | return true; |
| 816 | |
| 817 | for_each_child(node, c) |
| 818 | if (any_label_tree(dti, c)) |
| 819 | return true; |
| 820 | |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | static void generate_label_tree_internal(struct dt_info *dti, |
| 825 | struct node *an, struct node *node, |
| 826 | bool allocph) |
| 827 | { |
| 828 | struct node *dt = dti->dt; |
| 829 | struct node *c; |
| 830 | struct property *p; |
| 831 | struct label *l; |
| 832 | |
| 833 | /* if there are labels */ |
| 834 | if (node->labels) { |
| 835 | |
| 836 | /* now add the label in the node */ |
| 837 | for_each_label(node->labels, l) { |
| 838 | |
| 839 | /* check whether the label already exists */ |
| 840 | p = get_property(an, l->label); |
| 841 | if (p) { |
| 842 | fprintf(stderr, "WARNING: label %s already" |
| 843 | " exists in /%s", l->label, |
| 844 | an->name); |
| 845 | continue; |
| 846 | } |
| 847 | |
| 848 | /* insert it */ |
| 849 | p = build_property(l->label, |
| 850 | data_copy_mem(node->fullpath, |
| 851 | strlen(node->fullpath) + 1)); |
| 852 | add_property(an, p); |
| 853 | } |
| 854 | |
| 855 | /* force allocation of a phandle for this node */ |
| 856 | if (allocph) |
| 857 | (void)get_node_phandle(dt, node); |
| 858 | } |
| 859 | |
| 860 | for_each_child(node, c) |
| 861 | generate_label_tree_internal(dti, an, c, allocph); |
| 862 | } |
| 863 | |
| 864 | static bool any_fixup_tree(struct dt_info *dti, struct node *node) |
| 865 | { |
| 866 | struct node *c; |
| 867 | struct property *prop; |
| 868 | struct marker *m; |
| 869 | |
| 870 | for_each_property(node, prop) { |
| 871 | m = prop->val.markers; |
| 872 | for_each_marker_of_type(m, REF_PHANDLE) { |
| 873 | if (!get_node_by_ref(dti->dt, m->ref)) |
| 874 | return true; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | for_each_child(node, c) { |
| 879 | if (any_fixup_tree(dti, c)) |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | static void add_fixup_entry(struct dt_info *dti, struct node *fn, |
| 887 | struct node *node, struct property *prop, |
| 888 | struct marker *m) |
| 889 | { |
| 890 | char *entry; |
| 891 | |
| 892 | /* m->ref can only be a REF_PHANDLE, but check anyway */ |
| 893 | assert(m->type == REF_PHANDLE); |
| 894 | |
| 895 | /* there shouldn't be any ':' in the arguments */ |
| 896 | if (strchr(node->fullpath, ':') || strchr(prop->name, ':')) |
| 897 | die("arguments should not contain ':'\n"); |
| 898 | |
| 899 | xasprintf(&entry, "%s:%s:%u", |
| 900 | node->fullpath, prop->name, m->offset); |
| 901 | append_to_property(fn, m->ref, entry, strlen(entry) + 1); |
| 902 | |
| 903 | free(entry); |
| 904 | } |
| 905 | |
| 906 | static void generate_fixups_tree_internal(struct dt_info *dti, |
| 907 | struct node *fn, |
| 908 | struct node *node) |
| 909 | { |
| 910 | struct node *dt = dti->dt; |
| 911 | struct node *c; |
| 912 | struct property *prop; |
| 913 | struct marker *m; |
| 914 | struct node *refnode; |
| 915 | |
| 916 | for_each_property(node, prop) { |
| 917 | m = prop->val.markers; |
| 918 | for_each_marker_of_type(m, REF_PHANDLE) { |
| 919 | refnode = get_node_by_ref(dt, m->ref); |
| 920 | if (!refnode) |
| 921 | add_fixup_entry(dti, fn, node, prop, m); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | for_each_child(node, c) |
| 926 | generate_fixups_tree_internal(dti, fn, c); |
| 927 | } |
| 928 | |
| 929 | static bool any_local_fixup_tree(struct dt_info *dti, struct node *node) |
| 930 | { |
| 931 | struct node *c; |
| 932 | struct property *prop; |
| 933 | struct marker *m; |
| 934 | |
| 935 | for_each_property(node, prop) { |
| 936 | m = prop->val.markers; |
| 937 | for_each_marker_of_type(m, REF_PHANDLE) { |
| 938 | if (get_node_by_ref(dti->dt, m->ref)) |
| 939 | return true; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | for_each_child(node, c) { |
| 944 | if (any_local_fixup_tree(dti, c)) |
| 945 | return true; |
| 946 | } |
| 947 | |
| 948 | return false; |
| 949 | } |
| 950 | |
| 951 | static void add_local_fixup_entry(struct dt_info *dti, |
| 952 | struct node *lfn, struct node *node, |
| 953 | struct property *prop, struct marker *m, |
| 954 | struct node *refnode) |
| 955 | { |
| 956 | struct node *wn, *nwn; /* local fixup node, walk node, new */ |
Tom Rini | 56aeab5 | 2017-09-23 17:30:53 -0400 | [diff] [blame] | 957 | fdt32_t value_32; |
Tom Rini | bd39173 | 2017-09-23 12:52:44 -0400 | [diff] [blame] | 958 | char **compp; |
| 959 | int i, depth; |
| 960 | |
| 961 | /* walk back retreiving depth */ |
| 962 | depth = 0; |
| 963 | for (wn = node; wn; wn = wn->parent) |
| 964 | depth++; |
| 965 | |
| 966 | /* allocate name array */ |
| 967 | compp = xmalloc(sizeof(*compp) * depth); |
| 968 | |
| 969 | /* store names in the array */ |
| 970 | for (wn = node, i = depth - 1; wn; wn = wn->parent, i--) |
| 971 | compp[i] = wn->name; |
| 972 | |
| 973 | /* walk the path components creating nodes if they don't exist */ |
| 974 | for (wn = lfn, i = 1; i < depth; i++, wn = nwn) { |
| 975 | /* if no node exists, create it */ |
| 976 | nwn = get_subnode(wn, compp[i]); |
| 977 | if (!nwn) |
| 978 | nwn = build_and_name_child_node(wn, compp[i]); |
| 979 | } |
| 980 | |
| 981 | free(compp); |
| 982 | |
| 983 | value_32 = cpu_to_fdt32(m->offset); |
| 984 | append_to_property(wn, prop->name, &value_32, sizeof(value_32)); |
| 985 | } |
| 986 | |
| 987 | static void generate_local_fixups_tree_internal(struct dt_info *dti, |
| 988 | struct node *lfn, |
| 989 | struct node *node) |
| 990 | { |
| 991 | struct node *dt = dti->dt; |
| 992 | struct node *c; |
| 993 | struct property *prop; |
| 994 | struct marker *m; |
| 995 | struct node *refnode; |
| 996 | |
| 997 | for_each_property(node, prop) { |
| 998 | m = prop->val.markers; |
| 999 | for_each_marker_of_type(m, REF_PHANDLE) { |
| 1000 | refnode = get_node_by_ref(dt, m->ref); |
| 1001 | if (refnode) |
| 1002 | add_local_fixup_entry(dti, lfn, node, prop, m, refnode); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | for_each_child(node, c) |
| 1007 | generate_local_fixups_tree_internal(dti, lfn, c); |
| 1008 | } |
| 1009 | |
| 1010 | void generate_label_tree(struct dt_info *dti, char *name, bool allocph) |
| 1011 | { |
| 1012 | if (!any_label_tree(dti, dti->dt)) |
| 1013 | return; |
| 1014 | generate_label_tree_internal(dti, build_root_node(dti->dt, name), |
| 1015 | dti->dt, allocph); |
| 1016 | } |
| 1017 | |
| 1018 | void generate_fixups_tree(struct dt_info *dti, char *name) |
| 1019 | { |
| 1020 | if (!any_fixup_tree(dti, dti->dt)) |
| 1021 | return; |
| 1022 | generate_fixups_tree_internal(dti, build_root_node(dti->dt, name), |
| 1023 | dti->dt); |
| 1024 | } |
| 1025 | |
| 1026 | void generate_local_fixups_tree(struct dt_info *dti, char *name) |
| 1027 | { |
| 1028 | if (!any_local_fixup_tree(dti, dti->dt)) |
| 1029 | return; |
| 1030 | generate_local_fixups_tree_internal(dti, build_root_node(dti->dt, name), |
| 1031 | dti->dt); |
| 1032 | } |