Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Functions for dealing with DT resolution |
| 3 | * |
| 4 | * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com> |
| 5 | * Copyright (C) 2012 Texas Instruments Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 12 | #define pr_fmt(fmt) "OF: resolver: " fmt |
| 13 | |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/ctype.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/slab.h> |
| 23 | |
| 24 | /* illegal phandle value (set when unresolved) */ |
| 25 | #define OF_PHANDLE_ILLEGAL 0xdeadbeef |
| 26 | |
| 27 | /** |
| 28 | * Find a node with the give full name by recursively following any of |
| 29 | * the child node links. |
| 30 | */ |
Frank Rowand | fad556b | 2016-10-28 23:26:25 -0700 | [diff] [blame] | 31 | static struct device_node *find_node_by_full_name(struct device_node *node, |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 32 | const char *full_name) |
| 33 | { |
| 34 | struct device_node *child, *found; |
| 35 | |
Frank Rowand | 9f27ede | 2016-10-28 23:26:23 -0700 | [diff] [blame] | 36 | if (!node) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 37 | return NULL; |
| 38 | |
Frank Rowand | 9f27ede | 2016-10-28 23:26:23 -0700 | [diff] [blame] | 39 | if (!of_node_cmp(node->full_name, full_name)) |
Amitoj Kaur Chawla | 82f6875 | 2016-02-03 23:39:01 +0530 | [diff] [blame] | 40 | return of_node_get(node); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 41 | |
| 42 | for_each_child_of_node(node, child) { |
Frank Rowand | fad556b | 2016-10-28 23:26:25 -0700 | [diff] [blame] | 43 | found = find_node_by_full_name(child, full_name); |
Amitoj Kaur Chawla | 82f6875 | 2016-02-03 23:39:01 +0530 | [diff] [blame] | 44 | if (found != NULL) { |
| 45 | of_node_put(child); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 46 | return found; |
Amitoj Kaur Chawla | 82f6875 | 2016-02-03 23:39:01 +0530 | [diff] [blame] | 47 | } |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Find live tree's maximum phandle value. |
| 55 | */ |
Frank Rowand | f94823f | 2016-10-28 23:26:24 -0700 | [diff] [blame] | 56 | static phandle live_tree_max_phandle(void) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 57 | { |
| 58 | struct device_node *node; |
| 59 | phandle phandle; |
| 60 | unsigned long flags; |
| 61 | |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 62 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 63 | phandle = 0; |
| 64 | for_each_of_allnodes(node) { |
| 65 | if (node->phandle != OF_PHANDLE_ILLEGAL && |
| 66 | node->phandle > phandle) |
| 67 | phandle = node->phandle; |
| 68 | } |
| 69 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 70 | |
| 71 | return phandle; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Adjust a subtree's phandle values by a given delta. |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 76 | */ |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 77 | static void adjust_overlay_phandles(struct device_node *overlay, |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 78 | int phandle_delta) |
| 79 | { |
| 80 | struct device_node *child; |
| 81 | struct property *prop; |
| 82 | phandle phandle; |
| 83 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 84 | if (overlay->phandle != 0 && overlay->phandle != OF_PHANDLE_ILLEGAL) |
| 85 | overlay->phandle += phandle_delta; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 86 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 87 | for_each_property_of_node(overlay, prop) { |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 88 | |
Frank Rowand | 9f27ede | 2016-10-28 23:26:23 -0700 | [diff] [blame] | 89 | if (of_prop_cmp(prop->name, "phandle") && |
| 90 | of_prop_cmp(prop->name, "linux,phandle")) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 91 | continue; |
| 92 | |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 93 | if (prop->length < 4) |
| 94 | continue; |
| 95 | |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 96 | phandle = be32_to_cpup(prop->value); |
Frank Rowand | a67976e | 2016-10-28 23:26:21 -0700 | [diff] [blame] | 97 | if (phandle == OF_PHANDLE_ILLEGAL) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 98 | continue; |
| 99 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 100 | *(uint32_t *)prop->value = cpu_to_be32(overlay->phandle); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 101 | } |
| 102 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 103 | for_each_child_of_node(overlay, child) |
Frank Rowand | f94823f | 2016-10-28 23:26:24 -0700 | [diff] [blame] | 104 | adjust_overlay_phandles(child, phandle_delta); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 105 | } |
| 106 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 107 | static int update_usages_of_a_phandle_reference(struct device_node *overlay, |
| 108 | struct property *prop_fixup, phandle phandle) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 109 | { |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 110 | struct device_node *refnode; |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 111 | struct property *prop; |
| 112 | char *value, *cur, *end, *node_path, *prop_name, *s; |
| 113 | int offset, len; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 114 | int err = 0; |
| 115 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 116 | value = kmalloc(prop_fixup->length, GFP_KERNEL); |
| 117 | if (!value) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 118 | return -ENOMEM; |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 119 | memcpy(value, prop_fixup->value, prop_fixup->length); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 120 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 121 | end = value + prop_fixup->length; |
| 122 | for (cur = value; cur < end; cur += len + 1) { |
| 123 | len = strlen(cur); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 124 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 125 | node_path = cur; |
| 126 | s = strchr(cur, ':'); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 127 | if (!s) { |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 128 | err = -EINVAL; |
| 129 | goto err_fail; |
| 130 | } |
| 131 | *s++ = '\0'; |
| 132 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 133 | prop_name = s; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 134 | s = strchr(s, ':'); |
| 135 | if (!s) { |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 136 | err = -EINVAL; |
| 137 | goto err_fail; |
| 138 | } |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 139 | *s++ = '\0'; |
Frank Rowand | 624ab2a | 2016-10-28 23:26:27 -0700 | [diff] [blame] | 140 | |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 141 | err = kstrtoint(s, 10, &offset); |
Frank Rowand | 9f27ede | 2016-10-28 23:26:23 -0700 | [diff] [blame] | 142 | if (err) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 143 | goto err_fail; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 144 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 145 | refnode = find_node_by_full_name(overlay, node_path); |
Frank Rowand | 96d1c8e | 2016-10-28 23:26:22 -0700 | [diff] [blame] | 146 | if (!refnode) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 147 | continue; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 148 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 149 | for_each_property_of_node(refnode, prop) { |
| 150 | if (!of_prop_cmp(prop->name, prop_name)) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 151 | break; |
| 152 | } |
Amitoj Kaur Chawla | 82f6875 | 2016-02-03 23:39:01 +0530 | [diff] [blame] | 153 | of_node_put(refnode); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 154 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 155 | if (!prop) { |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 156 | err = -ENOENT; |
| 157 | goto err_fail; |
| 158 | } |
| 159 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 160 | *(__be32 *)(prop->value + offset) = cpu_to_be32(phandle); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | err_fail: |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 164 | kfree(value); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 165 | return err; |
| 166 | } |
| 167 | |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 168 | /* compare nodes taking into account that 'name' strips out the @ part */ |
Frank Rowand | fad556b | 2016-10-28 23:26:25 -0700 | [diff] [blame] | 169 | static int node_name_cmp(const struct device_node *dn1, |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 170 | const struct device_node *dn2) |
| 171 | { |
| 172 | const char *n1 = strrchr(dn1->full_name, '/') ? : "/"; |
| 173 | const char *n2 = strrchr(dn2->full_name, '/') ? : "/"; |
| 174 | |
| 175 | return of_node_cmp(n1, n2); |
| 176 | } |
| 177 | |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 178 | /* |
| 179 | * Adjust the local phandle references by the given phandle delta. |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 180 | * Assumes the existances of a __local_fixups__ node at the root. |
| 181 | * Assumes that __of_verify_tree_phandle_references has been called. |
| 182 | * Does not take any devtree locks so make sure you call this on a tree |
| 183 | * which is at the detached state. |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 184 | */ |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 185 | static int adjust_local_phandle_references(struct device_node *local_fixups, |
| 186 | struct device_node *overlay, int phandle_delta) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 187 | { |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 188 | struct device_node *child, *overlay_child; |
| 189 | struct property *prop_fix, *prop; |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 190 | int err, i, count; |
| 191 | unsigned int off; |
| 192 | phandle phandle; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 193 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 194 | if (!local_fixups) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 195 | return 0; |
| 196 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 197 | for_each_property_of_node(local_fixups, prop_fix) { |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 198 | |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 199 | /* skip properties added automatically */ |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 200 | if (!of_prop_cmp(prop_fix->name, "name") || |
| 201 | !of_prop_cmp(prop_fix->name, "phandle") || |
| 202 | !of_prop_cmp(prop_fix->name, "linux,phandle")) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 203 | continue; |
| 204 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 205 | if ((prop_fix->length % 4) != 0 || prop_fix->length == 0) |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 206 | return -EINVAL; |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 207 | count = prop_fix->length / sizeof(__be32); |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 208 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 209 | for_each_property_of_node(overlay, prop) { |
| 210 | if (!of_prop_cmp(prop->name, prop_fix->name)) |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 214 | if (!prop) |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 215 | return -EINVAL; |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 216 | |
| 217 | for (i = 0; i < count; i++) { |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 218 | off = be32_to_cpu(((__be32 *)prop_fix->value)[i]); |
Frank Rowand | ea8229b | 2016-10-28 23:26:28 -0700 | [diff] [blame^] | 219 | if ((off + 4) > prop->length) |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 220 | return -EINVAL; |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 221 | |
Frank Rowand | 624ab2a | 2016-10-28 23:26:27 -0700 | [diff] [blame] | 222 | phandle = be32_to_cpu(*(__be32 *)(prop->value + off)); |
| 223 | phandle += phandle_delta; |
| 224 | *(__be32 *)(prop->value + off) = cpu_to_be32(phandle); |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 228 | for_each_child_of_node(local_fixups, child) { |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 229 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 230 | for_each_child_of_node(overlay, overlay_child) |
| 231 | if (!node_name_cmp(child, overlay_child)) |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 232 | break; |
| 233 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 234 | if (!overlay_child) |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 235 | return -EINVAL; |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 236 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 237 | err = adjust_local_phandle_references(child, overlay_child, |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 238 | phandle_delta); |
Frank Rowand | 9f27ede | 2016-10-28 23:26:23 -0700 | [diff] [blame] | 239 | if (err) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 240 | return err; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * of_resolve - Resolve the given node against the live tree. |
| 248 | * |
| 249 | * @resolve: Node to resolve |
| 250 | * |
| 251 | * Perform dynamic Device Tree resolution against the live tree |
| 252 | * to the given node to resolve. This depends on the live tree |
| 253 | * having a __symbols__ node, and the resolve node the __fixups__ & |
| 254 | * __local_fixups__ nodes (if needed). |
| 255 | * The result of the operation is a resolve node that it's contents |
| 256 | * are fit to be inserted or operate upon the live tree. |
| 257 | * Returns 0 on success or a negative error value on error. |
| 258 | */ |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 259 | int of_resolve_phandles(struct device_node *overlay) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 260 | { |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 261 | struct device_node *child, *local_fixups, *refnode; |
| 262 | struct device_node *tree_symbols, *overlay_symbols, *overlay_fixups; |
| 263 | struct property *prop; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 264 | const char *refpath; |
| 265 | phandle phandle, phandle_delta; |
| 266 | int err; |
| 267 | |
Frank Rowand | 624ab2a | 2016-10-28 23:26:27 -0700 | [diff] [blame] | 268 | if (!overlay) { |
| 269 | pr_err("null overlay\n"); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 270 | return -EINVAL; |
Frank Rowand | 624ab2a | 2016-10-28 23:26:27 -0700 | [diff] [blame] | 271 | } |
| 272 | if (!of_node_check_flag(overlay, OF_DETACHED)) { |
| 273 | pr_err("overlay not detached\n"); |
| 274 | return -EINVAL; |
| 275 | } |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 276 | |
Frank Rowand | f94823f | 2016-10-28 23:26:24 -0700 | [diff] [blame] | 277 | phandle_delta = live_tree_max_phandle() + 1; |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 278 | adjust_overlay_phandles(overlay, phandle_delta); |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 279 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 280 | for_each_child_of_node(overlay, local_fixups) |
| 281 | if (!of_node_cmp(local_fixups->name, "__local_fixups__")) |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 282 | break; |
| 283 | |
Frank Rowand | 624ab2a | 2016-10-28 23:26:27 -0700 | [diff] [blame] | 284 | err = adjust_local_phandle_references(local_fixups, overlay, phandle_delta); |
| 285 | if (err) |
| 286 | return err; |
Pantelis Antoniou | da56d04 | 2014-10-28 22:33:49 +0200 | [diff] [blame] | 287 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 288 | overlay_symbols = NULL; |
| 289 | overlay_fixups = NULL; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 290 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 291 | tree_symbols = of_find_node_by_path("/__symbols__"); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 292 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 293 | for_each_child_of_node(overlay, child) { |
Frank Rowand | 624ab2a | 2016-10-28 23:26:27 -0700 | [diff] [blame] | 294 | if (!of_node_cmp(child->name, "__symbols__")) |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 295 | overlay_symbols = child; |
Frank Rowand | 624ab2a | 2016-10-28 23:26:27 -0700 | [diff] [blame] | 296 | if (!of_node_cmp(child->name, "__fixups__")) |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 297 | overlay_fixups = child; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 298 | } |
| 299 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 300 | if (!overlay_fixups) { |
Frank Rowand | a67976e | 2016-10-28 23:26:21 -0700 | [diff] [blame] | 301 | err = 0; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 302 | goto out; |
| 303 | } |
| 304 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 305 | if (!tree_symbols) { |
Frank Rowand | 624ab2a | 2016-10-28 23:26:27 -0700 | [diff] [blame] | 306 | pr_err("no symbols in root of device tree.\n"); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 307 | err = -EINVAL; |
| 308 | goto out; |
| 309 | } |
| 310 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 311 | for_each_property_of_node(overlay_fixups, prop) { |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 312 | |
| 313 | /* skip properties added automatically */ |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 314 | if (!of_prop_cmp(prop->name, "name")) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 315 | continue; |
| 316 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 317 | err = of_property_read_string(tree_symbols, |
| 318 | prop->name, &refpath); |
Frank Rowand | 9f27ede | 2016-10-28 23:26:23 -0700 | [diff] [blame] | 319 | if (err) |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 320 | goto out; |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 321 | |
| 322 | refnode = of_find_node_by_path(refpath); |
| 323 | if (!refnode) { |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 324 | err = -ENOENT; |
| 325 | goto out; |
| 326 | } |
| 327 | |
| 328 | phandle = refnode->phandle; |
| 329 | of_node_put(refnode); |
| 330 | |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 331 | err = update_usages_of_a_phandle_reference(overlay, prop, phandle); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 332 | if (err) |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | out: |
Frank Rowand | 25e1687 | 2016-10-28 23:26:26 -0700 | [diff] [blame] | 337 | of_node_put(tree_symbols); |
Pantelis Antoniou | 7941b27 | 2014-07-04 19:59:20 +0300 | [diff] [blame] | 338 | |
| 339 | return err; |
| 340 | } |
| 341 | EXPORT_SYMBOL_GPL(of_resolve_phandles); |