| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * (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 | 4 | */ |
|---|
| 20 | 5 | |
|---|
| 21 | 6 | #include "dtc.h" |
|---|
| 7 | +#include "srcpos.h" |
|---|
| 22 | 8 | |
|---|
| 23 | 9 | /* |
|---|
| 24 | 10 | * Tree building functions |
|---|
| .. | .. |
|---|
| 50 | 36 | label->deleted = 1; |
|---|
| 51 | 37 | } |
|---|
| 52 | 38 | |
|---|
| 53 | | -struct property *build_property(char *name, struct data val) |
|---|
| 39 | +struct property *build_property(char *name, struct data val, |
|---|
| 40 | + struct srcpos *srcpos) |
|---|
| 54 | 41 | { |
|---|
| 55 | 42 | struct property *new = xmalloc(sizeof(*new)); |
|---|
| 56 | 43 | |
|---|
| .. | .. |
|---|
| 58 | 45 | |
|---|
| 59 | 46 | new->name = name; |
|---|
| 60 | 47 | new->val = val; |
|---|
| 48 | + new->srcpos = srcpos_copy(srcpos); |
|---|
| 61 | 49 | |
|---|
| 62 | 50 | return new; |
|---|
| 63 | 51 | } |
|---|
| .. | .. |
|---|
| 97 | 85 | return head; |
|---|
| 98 | 86 | } |
|---|
| 99 | 87 | |
|---|
| 100 | | -struct node *build_node(struct property *proplist, struct node *children) |
|---|
| 88 | +struct node *build_node(struct property *proplist, struct node *children, |
|---|
| 89 | + struct srcpos *srcpos) |
|---|
| 101 | 90 | { |
|---|
| 102 | 91 | struct node *new = xmalloc(sizeof(*new)); |
|---|
| 103 | 92 | struct node *child; |
|---|
| .. | .. |
|---|
| 106 | 95 | |
|---|
| 107 | 96 | new->proplist = reverse_properties(proplist); |
|---|
| 108 | 97 | new->children = children; |
|---|
| 98 | + new->srcpos = srcpos_copy(srcpos); |
|---|
| 109 | 99 | |
|---|
| 110 | 100 | for_each_child(new, child) { |
|---|
| 111 | 101 | child->parent = new; |
|---|
| .. | .. |
|---|
| 114 | 104 | return new; |
|---|
| 115 | 105 | } |
|---|
| 116 | 106 | |
|---|
| 117 | | -struct node *build_node_delete(void) |
|---|
| 107 | +struct node *build_node_delete(struct srcpos *srcpos) |
|---|
| 118 | 108 | { |
|---|
| 119 | 109 | struct node *new = xmalloc(sizeof(*new)); |
|---|
| 120 | 110 | |
|---|
| 121 | 111 | memset(new, 0, sizeof(*new)); |
|---|
| 122 | 112 | |
|---|
| 123 | 113 | new->deleted = 1; |
|---|
| 114 | + new->srcpos = srcpos_copy(srcpos); |
|---|
| 124 | 115 | |
|---|
| 125 | 116 | return new; |
|---|
| 126 | 117 | } |
|---|
| .. | .. |
|---|
| 183 | 174 | |
|---|
| 184 | 175 | old_prop->val = new_prop->val; |
|---|
| 185 | 176 | old_prop->deleted = 0; |
|---|
| 177 | + free(old_prop->srcpos); |
|---|
| 178 | + old_prop->srcpos = new_prop->srcpos; |
|---|
| 186 | 179 | free(new_prop); |
|---|
| 187 | 180 | new_prop = NULL; |
|---|
| 188 | 181 | break; |
|---|
| .. | .. |
|---|
| 223 | 216 | add_child(old_node, new_child); |
|---|
| 224 | 217 | } |
|---|
| 225 | 218 | |
|---|
| 219 | + old_node->srcpos = srcpos_extend(old_node->srcpos, new_node->srcpos); |
|---|
| 220 | + |
|---|
| 226 | 221 | /* The new node contents are now merged into the old node. Free |
|---|
| 227 | 222 | * the new node. */ |
|---|
| 228 | 223 | free(new_node); |
|---|
| .. | .. |
|---|
| 239 | 234 | char *name; |
|---|
| 240 | 235 | |
|---|
| 241 | 236 | if (ref[0] == '/') { |
|---|
| 237 | + d = data_add_marker(d, TYPE_STRING, ref); |
|---|
| 242 | 238 | d = data_append_data(d, ref, strlen(ref) + 1); |
|---|
| 243 | 239 | |
|---|
| 244 | | - p = build_property("target-path", d); |
|---|
| 240 | + p = build_property("target-path", d, NULL); |
|---|
| 245 | 241 | } else { |
|---|
| 246 | 242 | d = data_add_marker(d, REF_PHANDLE, ref); |
|---|
| 247 | 243 | d = data_append_integer(d, 0xffffffff, 32); |
|---|
| 248 | 244 | |
|---|
| 249 | | - p = build_property("target", d); |
|---|
| 245 | + p = build_property("target", d, NULL); |
|---|
| 250 | 246 | } |
|---|
| 251 | 247 | |
|---|
| 252 | 248 | xasprintf(&name, "fragment@%u", |
|---|
| 253 | 249 | next_orphan_fragment++); |
|---|
| 254 | 250 | name_node(new_node, "__overlay__"); |
|---|
| 255 | | - node = build_node(p, new_node); |
|---|
| 251 | + node = build_node(p, new_node, NULL); |
|---|
| 256 | 252 | name_node(node, name); |
|---|
| 257 | 253 | |
|---|
| 258 | 254 | add_child(dt, node); |
|---|
| .. | .. |
|---|
| 340 | 336 | } |
|---|
| 341 | 337 | |
|---|
| 342 | 338 | void append_to_property(struct node *node, |
|---|
| 343 | | - char *name, const void *data, int len) |
|---|
| 339 | + char *name, const void *data, int len, |
|---|
| 340 | + enum markertype type) |
|---|
| 344 | 341 | { |
|---|
| 345 | 342 | struct data d; |
|---|
| 346 | 343 | struct property *p; |
|---|
| 347 | 344 | |
|---|
| 348 | 345 | p = get_property(node, name); |
|---|
| 349 | 346 | if (p) { |
|---|
| 350 | | - d = data_append_data(p->val, data, len); |
|---|
| 347 | + d = data_add_marker(p->val, type, name); |
|---|
| 348 | + d = data_append_data(d, data, len); |
|---|
| 351 | 349 | p->val = d; |
|---|
| 352 | 350 | } else { |
|---|
| 353 | | - d = data_append_data(empty_data, data, len); |
|---|
| 354 | | - p = build_property(name, d); |
|---|
| 351 | + d = data_add_marker(empty_data, type, name); |
|---|
| 352 | + d = data_append_data(d, data, len); |
|---|
| 353 | + p = build_property(name, d, NULL); |
|---|
| 355 | 354 | add_property(node, p); |
|---|
| 356 | 355 | } |
|---|
| 357 | 356 | } |
|---|
| .. | .. |
|---|
| 527 | 526 | p = strchr(path, '/'); |
|---|
| 528 | 527 | |
|---|
| 529 | 528 | for_each_child(tree, child) { |
|---|
| 530 | | - if (p && (strlen(child->name) == p-path) && |
|---|
| 531 | | - strprefixeq(path, p - path, child->name)) |
|---|
| 529 | + if (p && strprefixeq(path, p - path, child->name)) |
|---|
| 532 | 530 | return get_node_by_path(child, p+1); |
|---|
| 533 | 531 | else if (!p && streq(path, child->name)) |
|---|
| 534 | 532 | return child; |
|---|
| .. | .. |
|---|
| 594 | 592 | cell_t get_node_phandle(struct node *root, struct node *node) |
|---|
| 595 | 593 | { |
|---|
| 596 | 594 | static cell_t phandle = 1; /* FIXME: ick, static local */ |
|---|
| 595 | + struct data d = empty_data; |
|---|
| 597 | 596 | |
|---|
| 598 | 597 | if ((node->phandle != 0) && (node->phandle != -1)) |
|---|
| 599 | 598 | return node->phandle; |
|---|
| .. | .. |
|---|
| 603 | 602 | |
|---|
| 604 | 603 | node->phandle = phandle; |
|---|
| 605 | 604 | |
|---|
| 605 | + d = data_add_marker(d, TYPE_UINT32, NULL); |
|---|
| 606 | + d = data_append_cell(d, phandle); |
|---|
| 607 | + |
|---|
| 606 | 608 | if (!get_property(node, "linux,phandle") |
|---|
| 607 | 609 | && (phandle_format & PHANDLE_LEGACY)) |
|---|
| 608 | | - add_property(node, |
|---|
| 609 | | - build_property("linux,phandle", |
|---|
| 610 | | - data_append_cell(empty_data, phandle))); |
|---|
| 610 | + add_property(node, build_property("linux,phandle", d, NULL)); |
|---|
| 611 | 611 | |
|---|
| 612 | 612 | if (!get_property(node, "phandle") |
|---|
| 613 | 613 | && (phandle_format & PHANDLE_EPAPR)) |
|---|
| 614 | | - add_property(node, |
|---|
| 615 | | - build_property("phandle", |
|---|
| 616 | | - data_append_cell(empty_data, phandle))); |
|---|
| 614 | + add_property(node, build_property("phandle", d, NULL)); |
|---|
| 617 | 615 | |
|---|
| 618 | 616 | /* If the node *does* have a phandle property, we must |
|---|
| 619 | 617 | * be dealing with a self-referencing phandle, which will be |
|---|
| .. | .. |
|---|
| 787 | 785 | { |
|---|
| 788 | 786 | struct node *node; |
|---|
| 789 | 787 | |
|---|
| 790 | | - node = build_node(NULL, NULL); |
|---|
| 788 | + node = build_node(NULL, NULL, NULL); |
|---|
| 791 | 789 | name_node(node, xstrdup(name)); |
|---|
| 792 | 790 | add_child(parent, node); |
|---|
| 793 | 791 | |
|---|
| .. | .. |
|---|
| 848 | 846 | |
|---|
| 849 | 847 | /* insert it */ |
|---|
| 850 | 848 | p = build_property(l->label, |
|---|
| 851 | | - data_copy_mem(node->fullpath, |
|---|
| 852 | | - strlen(node->fullpath) + 1)); |
|---|
| 849 | + data_copy_escape_string(node->fullpath, |
|---|
| 850 | + strlen(node->fullpath)), |
|---|
| 851 | + NULL); |
|---|
| 853 | 852 | add_property(an, p); |
|---|
| 854 | 853 | } |
|---|
| 855 | 854 | |
|---|
| .. | .. |
|---|
| 899 | 898 | |
|---|
| 900 | 899 | xasprintf(&entry, "%s:%s:%u", |
|---|
| 901 | 900 | node->fullpath, prop->name, m->offset); |
|---|
| 902 | | - append_to_property(fn, m->ref, entry, strlen(entry) + 1); |
|---|
| 901 | + append_to_property(fn, m->ref, entry, strlen(entry) + 1, TYPE_STRING); |
|---|
| 903 | 902 | |
|---|
| 904 | 903 | free(entry); |
|---|
| 905 | 904 | } |
|---|
| .. | .. |
|---|
| 959 | 958 | char **compp; |
|---|
| 960 | 959 | int i, depth; |
|---|
| 961 | 960 | |
|---|
| 962 | | - /* walk back retreiving depth */ |
|---|
| 961 | + /* walk back retrieving depth */ |
|---|
| 963 | 962 | depth = 0; |
|---|
| 964 | 963 | for (wn = node; wn; wn = wn->parent) |
|---|
| 965 | 964 | depth++; |
|---|
| .. | .. |
|---|
| 982 | 981 | free(compp); |
|---|
| 983 | 982 | |
|---|
| 984 | 983 | value_32 = cpu_to_fdt32(m->offset); |
|---|
| 985 | | - append_to_property(wn, prop->name, &value_32, sizeof(value_32)); |
|---|
| 984 | + append_to_property(wn, prop->name, &value_32, sizeof(value_32), TYPE_UINT32); |
|---|
| 986 | 985 | } |
|---|
| 987 | 986 | |
|---|
| 988 | 987 | static void generate_local_fixups_tree_internal(struct dt_info *dti, |
|---|